xfrm_policy.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419
  1. /*
  2. * xfrm_policy.c
  3. *
  4. * Changes:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  8. * IPv6 support
  9. * Kazunori MIYAZAWA @USAGI
  10. * YOSHIFUJI Hideaki
  11. * Split up af-specific portion
  12. * Derek Atkins <derek@ihtfp.com> Add the post_input processor
  13. *
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/kmod.h>
  17. #include <linux/list.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/notifier.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/netfilter.h>
  23. #include <linux/module.h>
  24. #include <linux/cache.h>
  25. #include <net/xfrm.h>
  26. #include <net/ip.h>
  27. #include "xfrm_hash.h"
  28. int sysctl_xfrm_larval_drop __read_mostly;
  29. DEFINE_MUTEX(xfrm_cfg_mutex);
  30. EXPORT_SYMBOL(xfrm_cfg_mutex);
  31. static DEFINE_RWLOCK(xfrm_policy_lock);
  32. unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
  33. EXPORT_SYMBOL(xfrm_policy_count);
  34. static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
  35. static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
  36. static struct kmem_cache *xfrm_dst_cache __read_mostly;
  37. static struct work_struct xfrm_policy_gc_work;
  38. static HLIST_HEAD(xfrm_policy_gc_list);
  39. static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
  40. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
  41. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
  42. static inline int
  43. __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  44. {
  45. return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
  46. addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
  47. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  48. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  49. (fl->proto == sel->proto || !sel->proto) &&
  50. (fl->oif == sel->ifindex || !sel->ifindex);
  51. }
  52. static inline int
  53. __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  54. {
  55. return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
  56. addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
  57. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  58. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  59. (fl->proto == sel->proto || !sel->proto) &&
  60. (fl->oif == sel->ifindex || !sel->ifindex);
  61. }
  62. int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
  63. unsigned short family)
  64. {
  65. switch (family) {
  66. case AF_INET:
  67. return __xfrm4_selector_match(sel, fl);
  68. case AF_INET6:
  69. return __xfrm6_selector_match(sel, fl);
  70. }
  71. return 0;
  72. }
  73. int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
  74. unsigned short family)
  75. {
  76. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  77. int err = 0;
  78. if (unlikely(afinfo == NULL))
  79. return -EAFNOSUPPORT;
  80. if (likely(afinfo->dst_lookup != NULL))
  81. err = afinfo->dst_lookup(dst, fl);
  82. else
  83. err = -EINVAL;
  84. xfrm_policy_put_afinfo(afinfo);
  85. return err;
  86. }
  87. EXPORT_SYMBOL(xfrm_dst_lookup);
  88. static inline unsigned long make_jiffies(long secs)
  89. {
  90. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  91. return MAX_SCHEDULE_TIMEOUT-1;
  92. else
  93. return secs*HZ;
  94. }
  95. static void xfrm_policy_timer(unsigned long data)
  96. {
  97. struct xfrm_policy *xp = (struct xfrm_policy*)data;
  98. unsigned long now = get_seconds();
  99. long next = LONG_MAX;
  100. int warn = 0;
  101. int dir;
  102. read_lock(&xp->lock);
  103. if (xp->dead)
  104. goto out;
  105. dir = xfrm_policy_id2dir(xp->index);
  106. if (xp->lft.hard_add_expires_seconds) {
  107. long tmo = xp->lft.hard_add_expires_seconds +
  108. xp->curlft.add_time - now;
  109. if (tmo <= 0)
  110. goto expired;
  111. if (tmo < next)
  112. next = tmo;
  113. }
  114. if (xp->lft.hard_use_expires_seconds) {
  115. long tmo = xp->lft.hard_use_expires_seconds +
  116. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  117. if (tmo <= 0)
  118. goto expired;
  119. if (tmo < next)
  120. next = tmo;
  121. }
  122. if (xp->lft.soft_add_expires_seconds) {
  123. long tmo = xp->lft.soft_add_expires_seconds +
  124. xp->curlft.add_time - now;
  125. if (tmo <= 0) {
  126. warn = 1;
  127. tmo = XFRM_KM_TIMEOUT;
  128. }
  129. if (tmo < next)
  130. next = tmo;
  131. }
  132. if (xp->lft.soft_use_expires_seconds) {
  133. long tmo = xp->lft.soft_use_expires_seconds +
  134. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  135. if (tmo <= 0) {
  136. warn = 1;
  137. tmo = XFRM_KM_TIMEOUT;
  138. }
  139. if (tmo < next)
  140. next = tmo;
  141. }
  142. if (warn)
  143. km_policy_expired(xp, dir, 0, 0);
  144. if (next != LONG_MAX &&
  145. !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
  146. xfrm_pol_hold(xp);
  147. out:
  148. read_unlock(&xp->lock);
  149. xfrm_pol_put(xp);
  150. return;
  151. expired:
  152. read_unlock(&xp->lock);
  153. if (!xfrm_policy_delete(xp, dir))
  154. km_policy_expired(xp, dir, 1, 0);
  155. xfrm_pol_put(xp);
  156. }
  157. /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
  158. * SPD calls.
  159. */
  160. struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
  161. {
  162. struct xfrm_policy *policy;
  163. policy = kzalloc(sizeof(struct xfrm_policy), gfp);
  164. if (policy) {
  165. INIT_HLIST_NODE(&policy->bydst);
  166. INIT_HLIST_NODE(&policy->byidx);
  167. rwlock_init(&policy->lock);
  168. atomic_set(&policy->refcnt, 1);
  169. init_timer(&policy->timer);
  170. policy->timer.data = (unsigned long)policy;
  171. policy->timer.function = xfrm_policy_timer;
  172. }
  173. return policy;
  174. }
  175. EXPORT_SYMBOL(xfrm_policy_alloc);
  176. /* Destroy xfrm_policy: descendant resources must be released to this moment. */
  177. void __xfrm_policy_destroy(struct xfrm_policy *policy)
  178. {
  179. BUG_ON(!policy->dead);
  180. BUG_ON(policy->bundles);
  181. if (del_timer(&policy->timer))
  182. BUG();
  183. security_xfrm_policy_free(policy);
  184. kfree(policy);
  185. }
  186. EXPORT_SYMBOL(__xfrm_policy_destroy);
  187. static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
  188. {
  189. struct dst_entry *dst;
  190. while ((dst = policy->bundles) != NULL) {
  191. policy->bundles = dst->next;
  192. dst_free(dst);
  193. }
  194. if (del_timer(&policy->timer))
  195. atomic_dec(&policy->refcnt);
  196. if (atomic_read(&policy->refcnt) > 1)
  197. flow_cache_flush();
  198. xfrm_pol_put(policy);
  199. }
  200. static void xfrm_policy_gc_task(struct work_struct *work)
  201. {
  202. struct xfrm_policy *policy;
  203. struct hlist_node *entry, *tmp;
  204. struct hlist_head gc_list;
  205. spin_lock_bh(&xfrm_policy_gc_lock);
  206. gc_list.first = xfrm_policy_gc_list.first;
  207. INIT_HLIST_HEAD(&xfrm_policy_gc_list);
  208. spin_unlock_bh(&xfrm_policy_gc_lock);
  209. hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
  210. xfrm_policy_gc_kill(policy);
  211. }
  212. /* Rule must be locked. Release descentant resources, announce
  213. * entry dead. The rule must be unlinked from lists to the moment.
  214. */
  215. static void xfrm_policy_kill(struct xfrm_policy *policy)
  216. {
  217. int dead;
  218. write_lock_bh(&policy->lock);
  219. dead = policy->dead;
  220. policy->dead = 1;
  221. write_unlock_bh(&policy->lock);
  222. if (unlikely(dead)) {
  223. WARN_ON(1);
  224. return;
  225. }
  226. spin_lock(&xfrm_policy_gc_lock);
  227. hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
  228. spin_unlock(&xfrm_policy_gc_lock);
  229. schedule_work(&xfrm_policy_gc_work);
  230. }
  231. struct xfrm_policy_hash {
  232. struct hlist_head *table;
  233. unsigned int hmask;
  234. };
  235. static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
  236. static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
  237. static struct hlist_head *xfrm_policy_byidx __read_mostly;
  238. static unsigned int xfrm_idx_hmask __read_mostly;
  239. static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
  240. static inline unsigned int idx_hash(u32 index)
  241. {
  242. return __idx_hash(index, xfrm_idx_hmask);
  243. }
  244. static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
  245. {
  246. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  247. unsigned int hash = __sel_hash(sel, family, hmask);
  248. return (hash == hmask + 1 ?
  249. &xfrm_policy_inexact[dir] :
  250. xfrm_policy_bydst[dir].table + hash);
  251. }
  252. static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
  253. {
  254. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  255. unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
  256. return xfrm_policy_bydst[dir].table + hash;
  257. }
  258. static void xfrm_dst_hash_transfer(struct hlist_head *list,
  259. struct hlist_head *ndsttable,
  260. unsigned int nhashmask)
  261. {
  262. struct hlist_node *entry, *tmp;
  263. struct xfrm_policy *pol;
  264. hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
  265. unsigned int h;
  266. h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
  267. pol->family, nhashmask);
  268. hlist_add_head(&pol->bydst, ndsttable+h);
  269. }
  270. }
  271. static void xfrm_idx_hash_transfer(struct hlist_head *list,
  272. struct hlist_head *nidxtable,
  273. unsigned int nhashmask)
  274. {
  275. struct hlist_node *entry, *tmp;
  276. struct xfrm_policy *pol;
  277. hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
  278. unsigned int h;
  279. h = __idx_hash(pol->index, nhashmask);
  280. hlist_add_head(&pol->byidx, nidxtable+h);
  281. }
  282. }
  283. static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
  284. {
  285. return ((old_hmask + 1) << 1) - 1;
  286. }
  287. static void xfrm_bydst_resize(int dir)
  288. {
  289. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  290. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  291. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  292. struct hlist_head *odst = xfrm_policy_bydst[dir].table;
  293. struct hlist_head *ndst = xfrm_hash_alloc(nsize);
  294. int i;
  295. if (!ndst)
  296. return;
  297. write_lock_bh(&xfrm_policy_lock);
  298. for (i = hmask; i >= 0; i--)
  299. xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
  300. xfrm_policy_bydst[dir].table = ndst;
  301. xfrm_policy_bydst[dir].hmask = nhashmask;
  302. write_unlock_bh(&xfrm_policy_lock);
  303. xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
  304. }
  305. static void xfrm_byidx_resize(int total)
  306. {
  307. unsigned int hmask = xfrm_idx_hmask;
  308. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  309. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  310. struct hlist_head *oidx = xfrm_policy_byidx;
  311. struct hlist_head *nidx = xfrm_hash_alloc(nsize);
  312. int i;
  313. if (!nidx)
  314. return;
  315. write_lock_bh(&xfrm_policy_lock);
  316. for (i = hmask; i >= 0; i--)
  317. xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
  318. xfrm_policy_byidx = nidx;
  319. xfrm_idx_hmask = nhashmask;
  320. write_unlock_bh(&xfrm_policy_lock);
  321. xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
  322. }
  323. static inline int xfrm_bydst_should_resize(int dir, int *total)
  324. {
  325. unsigned int cnt = xfrm_policy_count[dir];
  326. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  327. if (total)
  328. *total += cnt;
  329. if ((hmask + 1) < xfrm_policy_hashmax &&
  330. cnt > hmask)
  331. return 1;
  332. return 0;
  333. }
  334. static inline int xfrm_byidx_should_resize(int total)
  335. {
  336. unsigned int hmask = xfrm_idx_hmask;
  337. if ((hmask + 1) < xfrm_policy_hashmax &&
  338. total > hmask)
  339. return 1;
  340. return 0;
  341. }
  342. void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
  343. {
  344. read_lock_bh(&xfrm_policy_lock);
  345. si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
  346. si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
  347. si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
  348. si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
  349. si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
  350. si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
  351. si->spdhcnt = xfrm_idx_hmask;
  352. si->spdhmcnt = xfrm_policy_hashmax;
  353. read_unlock_bh(&xfrm_policy_lock);
  354. }
  355. EXPORT_SYMBOL(xfrm_spd_getinfo);
  356. static DEFINE_MUTEX(hash_resize_mutex);
  357. static void xfrm_hash_resize(struct work_struct *__unused)
  358. {
  359. int dir, total;
  360. mutex_lock(&hash_resize_mutex);
  361. total = 0;
  362. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  363. if (xfrm_bydst_should_resize(dir, &total))
  364. xfrm_bydst_resize(dir);
  365. }
  366. if (xfrm_byidx_should_resize(total))
  367. xfrm_byidx_resize(total);
  368. mutex_unlock(&hash_resize_mutex);
  369. }
  370. static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
  371. /* Generate new index... KAME seems to generate them ordered by cost
  372. * of an absolute inpredictability of ordering of rules. This will not pass. */
  373. static u32 xfrm_gen_index(u8 type, int dir)
  374. {
  375. static u32 idx_generator;
  376. for (;;) {
  377. struct hlist_node *entry;
  378. struct hlist_head *list;
  379. struct xfrm_policy *p;
  380. u32 idx;
  381. int found;
  382. idx = (idx_generator | dir);
  383. idx_generator += 8;
  384. if (idx == 0)
  385. idx = 8;
  386. list = xfrm_policy_byidx + idx_hash(idx);
  387. found = 0;
  388. hlist_for_each_entry(p, entry, list, byidx) {
  389. if (p->index == idx) {
  390. found = 1;
  391. break;
  392. }
  393. }
  394. if (!found)
  395. return idx;
  396. }
  397. }
  398. static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
  399. {
  400. u32 *p1 = (u32 *) s1;
  401. u32 *p2 = (u32 *) s2;
  402. int len = sizeof(struct xfrm_selector) / sizeof(u32);
  403. int i;
  404. for (i = 0; i < len; i++) {
  405. if (p1[i] != p2[i])
  406. return 1;
  407. }
  408. return 0;
  409. }
  410. int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
  411. {
  412. struct xfrm_policy *pol;
  413. struct xfrm_policy *delpol;
  414. struct hlist_head *chain;
  415. struct hlist_node *entry, *newpos;
  416. struct dst_entry *gc_list;
  417. write_lock_bh(&xfrm_policy_lock);
  418. chain = policy_hash_bysel(&policy->selector, policy->family, dir);
  419. delpol = NULL;
  420. newpos = NULL;
  421. hlist_for_each_entry(pol, entry, chain, bydst) {
  422. if (pol->type == policy->type &&
  423. !selector_cmp(&pol->selector, &policy->selector) &&
  424. xfrm_sec_ctx_match(pol->security, policy->security) &&
  425. !WARN_ON(delpol)) {
  426. if (excl) {
  427. write_unlock_bh(&xfrm_policy_lock);
  428. return -EEXIST;
  429. }
  430. delpol = pol;
  431. if (policy->priority > pol->priority)
  432. continue;
  433. } else if (policy->priority >= pol->priority) {
  434. newpos = &pol->bydst;
  435. continue;
  436. }
  437. if (delpol)
  438. break;
  439. }
  440. if (newpos)
  441. hlist_add_after(newpos, &policy->bydst);
  442. else
  443. hlist_add_head(&policy->bydst, chain);
  444. xfrm_pol_hold(policy);
  445. xfrm_policy_count[dir]++;
  446. atomic_inc(&flow_cache_genid);
  447. if (delpol) {
  448. hlist_del(&delpol->bydst);
  449. hlist_del(&delpol->byidx);
  450. xfrm_policy_count[dir]--;
  451. }
  452. policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
  453. hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
  454. policy->curlft.add_time = get_seconds();
  455. policy->curlft.use_time = 0;
  456. if (!mod_timer(&policy->timer, jiffies + HZ))
  457. xfrm_pol_hold(policy);
  458. write_unlock_bh(&xfrm_policy_lock);
  459. if (delpol)
  460. xfrm_policy_kill(delpol);
  461. else if (xfrm_bydst_should_resize(dir, NULL))
  462. schedule_work(&xfrm_hash_work);
  463. read_lock_bh(&xfrm_policy_lock);
  464. gc_list = NULL;
  465. entry = &policy->bydst;
  466. hlist_for_each_entry_continue(policy, entry, bydst) {
  467. struct dst_entry *dst;
  468. write_lock(&policy->lock);
  469. dst = policy->bundles;
  470. if (dst) {
  471. struct dst_entry *tail = dst;
  472. while (tail->next)
  473. tail = tail->next;
  474. tail->next = gc_list;
  475. gc_list = dst;
  476. policy->bundles = NULL;
  477. }
  478. write_unlock(&policy->lock);
  479. }
  480. read_unlock_bh(&xfrm_policy_lock);
  481. while (gc_list) {
  482. struct dst_entry *dst = gc_list;
  483. gc_list = dst->next;
  484. dst_free(dst);
  485. }
  486. return 0;
  487. }
  488. EXPORT_SYMBOL(xfrm_policy_insert);
  489. struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
  490. struct xfrm_selector *sel,
  491. struct xfrm_sec_ctx *ctx, int delete,
  492. int *err)
  493. {
  494. struct xfrm_policy *pol, *ret;
  495. struct hlist_head *chain;
  496. struct hlist_node *entry;
  497. *err = 0;
  498. write_lock_bh(&xfrm_policy_lock);
  499. chain = policy_hash_bysel(sel, sel->family, dir);
  500. ret = NULL;
  501. hlist_for_each_entry(pol, entry, chain, bydst) {
  502. if (pol->type == type &&
  503. !selector_cmp(sel, &pol->selector) &&
  504. xfrm_sec_ctx_match(ctx, pol->security)) {
  505. xfrm_pol_hold(pol);
  506. if (delete) {
  507. *err = security_xfrm_policy_delete(pol);
  508. if (*err) {
  509. write_unlock_bh(&xfrm_policy_lock);
  510. return pol;
  511. }
  512. hlist_del(&pol->bydst);
  513. hlist_del(&pol->byidx);
  514. xfrm_policy_count[dir]--;
  515. }
  516. ret = pol;
  517. break;
  518. }
  519. }
  520. write_unlock_bh(&xfrm_policy_lock);
  521. if (ret && delete) {
  522. atomic_inc(&flow_cache_genid);
  523. xfrm_policy_kill(ret);
  524. }
  525. return ret;
  526. }
  527. EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
  528. struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
  529. int *err)
  530. {
  531. struct xfrm_policy *pol, *ret;
  532. struct hlist_head *chain;
  533. struct hlist_node *entry;
  534. *err = -ENOENT;
  535. if (xfrm_policy_id2dir(id) != dir)
  536. return NULL;
  537. *err = 0;
  538. write_lock_bh(&xfrm_policy_lock);
  539. chain = xfrm_policy_byidx + idx_hash(id);
  540. ret = NULL;
  541. hlist_for_each_entry(pol, entry, chain, byidx) {
  542. if (pol->type == type && pol->index == id) {
  543. xfrm_pol_hold(pol);
  544. if (delete) {
  545. *err = security_xfrm_policy_delete(pol);
  546. if (*err) {
  547. write_unlock_bh(&xfrm_policy_lock);
  548. return pol;
  549. }
  550. hlist_del(&pol->bydst);
  551. hlist_del(&pol->byidx);
  552. xfrm_policy_count[dir]--;
  553. }
  554. ret = pol;
  555. break;
  556. }
  557. }
  558. write_unlock_bh(&xfrm_policy_lock);
  559. if (ret && delete) {
  560. atomic_inc(&flow_cache_genid);
  561. xfrm_policy_kill(ret);
  562. }
  563. return ret;
  564. }
  565. EXPORT_SYMBOL(xfrm_policy_byid);
  566. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  567. static inline int
  568. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  569. {
  570. int dir, err = 0;
  571. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  572. struct xfrm_policy *pol;
  573. struct hlist_node *entry;
  574. int i;
  575. hlist_for_each_entry(pol, entry,
  576. &xfrm_policy_inexact[dir], bydst) {
  577. if (pol->type != type)
  578. continue;
  579. err = security_xfrm_policy_delete(pol);
  580. if (err) {
  581. xfrm_audit_policy_delete(pol, 0,
  582. audit_info->loginuid,
  583. audit_info->secid);
  584. return err;
  585. }
  586. }
  587. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  588. hlist_for_each_entry(pol, entry,
  589. xfrm_policy_bydst[dir].table + i,
  590. bydst) {
  591. if (pol->type != type)
  592. continue;
  593. err = security_xfrm_policy_delete(pol);
  594. if (err) {
  595. xfrm_audit_policy_delete(pol, 0,
  596. audit_info->loginuid,
  597. audit_info->secid);
  598. return err;
  599. }
  600. }
  601. }
  602. }
  603. return err;
  604. }
  605. #else
  606. static inline int
  607. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  608. {
  609. return 0;
  610. }
  611. #endif
  612. int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
  613. {
  614. int dir, err = 0;
  615. write_lock_bh(&xfrm_policy_lock);
  616. err = xfrm_policy_flush_secctx_check(type, audit_info);
  617. if (err)
  618. goto out;
  619. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  620. struct xfrm_policy *pol;
  621. struct hlist_node *entry;
  622. int i, killed;
  623. killed = 0;
  624. again1:
  625. hlist_for_each_entry(pol, entry,
  626. &xfrm_policy_inexact[dir], bydst) {
  627. if (pol->type != type)
  628. continue;
  629. hlist_del(&pol->bydst);
  630. hlist_del(&pol->byidx);
  631. write_unlock_bh(&xfrm_policy_lock);
  632. xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
  633. audit_info->secid);
  634. xfrm_policy_kill(pol);
  635. killed++;
  636. write_lock_bh(&xfrm_policy_lock);
  637. goto again1;
  638. }
  639. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  640. again2:
  641. hlist_for_each_entry(pol, entry,
  642. xfrm_policy_bydst[dir].table + i,
  643. bydst) {
  644. if (pol->type != type)
  645. continue;
  646. hlist_del(&pol->bydst);
  647. hlist_del(&pol->byidx);
  648. write_unlock_bh(&xfrm_policy_lock);
  649. xfrm_audit_policy_delete(pol, 1,
  650. audit_info->loginuid,
  651. audit_info->secid);
  652. xfrm_policy_kill(pol);
  653. killed++;
  654. write_lock_bh(&xfrm_policy_lock);
  655. goto again2;
  656. }
  657. }
  658. xfrm_policy_count[dir] -= killed;
  659. }
  660. atomic_inc(&flow_cache_genid);
  661. out:
  662. write_unlock_bh(&xfrm_policy_lock);
  663. return err;
  664. }
  665. EXPORT_SYMBOL(xfrm_policy_flush);
  666. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  667. void *data)
  668. {
  669. struct xfrm_policy *pol, *last = NULL;
  670. struct hlist_node *entry;
  671. int dir, last_dir = 0, count, error;
  672. read_lock_bh(&xfrm_policy_lock);
  673. count = 0;
  674. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  675. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  676. int i;
  677. hlist_for_each_entry(pol, entry,
  678. &xfrm_policy_inexact[dir], bydst) {
  679. if (pol->type != type)
  680. continue;
  681. if (last) {
  682. error = func(last, last_dir % XFRM_POLICY_MAX,
  683. count, data);
  684. if (error)
  685. goto out;
  686. }
  687. last = pol;
  688. last_dir = dir;
  689. count++;
  690. }
  691. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  692. hlist_for_each_entry(pol, entry, table + i, bydst) {
  693. if (pol->type != type)
  694. continue;
  695. if (last) {
  696. error = func(last, last_dir % XFRM_POLICY_MAX,
  697. count, data);
  698. if (error)
  699. goto out;
  700. }
  701. last = pol;
  702. last_dir = dir;
  703. count++;
  704. }
  705. }
  706. }
  707. if (count == 0) {
  708. error = -ENOENT;
  709. goto out;
  710. }
  711. error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
  712. out:
  713. read_unlock_bh(&xfrm_policy_lock);
  714. return error;
  715. }
  716. EXPORT_SYMBOL(xfrm_policy_walk);
  717. /*
  718. * Find policy to apply to this flow.
  719. *
  720. * Returns 0 if policy found, else an -errno.
  721. */
  722. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  723. u8 type, u16 family, int dir)
  724. {
  725. struct xfrm_selector *sel = &pol->selector;
  726. int match, ret = -ESRCH;
  727. if (pol->family != family ||
  728. pol->type != type)
  729. return ret;
  730. match = xfrm_selector_match(sel, fl, family);
  731. if (match)
  732. ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
  733. return ret;
  734. }
  735. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  736. u16 family, u8 dir)
  737. {
  738. int err;
  739. struct xfrm_policy *pol, *ret;
  740. xfrm_address_t *daddr, *saddr;
  741. struct hlist_node *entry;
  742. struct hlist_head *chain;
  743. u32 priority = ~0U;
  744. daddr = xfrm_flowi_daddr(fl, family);
  745. saddr = xfrm_flowi_saddr(fl, family);
  746. if (unlikely(!daddr || !saddr))
  747. return NULL;
  748. read_lock_bh(&xfrm_policy_lock);
  749. chain = policy_hash_direct(daddr, saddr, family, dir);
  750. ret = NULL;
  751. hlist_for_each_entry(pol, entry, chain, bydst) {
  752. err = xfrm_policy_match(pol, fl, type, family, dir);
  753. if (err) {
  754. if (err == -ESRCH)
  755. continue;
  756. else {
  757. ret = ERR_PTR(err);
  758. goto fail;
  759. }
  760. } else {
  761. ret = pol;
  762. priority = ret->priority;
  763. break;
  764. }
  765. }
  766. chain = &xfrm_policy_inexact[dir];
  767. hlist_for_each_entry(pol, entry, chain, bydst) {
  768. err = xfrm_policy_match(pol, fl, type, family, dir);
  769. if (err) {
  770. if (err == -ESRCH)
  771. continue;
  772. else {
  773. ret = ERR_PTR(err);
  774. goto fail;
  775. }
  776. } else if (pol->priority < priority) {
  777. ret = pol;
  778. break;
  779. }
  780. }
  781. if (ret)
  782. xfrm_pol_hold(ret);
  783. fail:
  784. read_unlock_bh(&xfrm_policy_lock);
  785. return ret;
  786. }
  787. static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  788. void **objp, atomic_t **obj_refp)
  789. {
  790. struct xfrm_policy *pol;
  791. int err = 0;
  792. #ifdef CONFIG_XFRM_SUB_POLICY
  793. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  794. if (IS_ERR(pol)) {
  795. err = PTR_ERR(pol);
  796. pol = NULL;
  797. }
  798. if (pol || err)
  799. goto end;
  800. #endif
  801. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  802. if (IS_ERR(pol)) {
  803. err = PTR_ERR(pol);
  804. pol = NULL;
  805. }
  806. #ifdef CONFIG_XFRM_SUB_POLICY
  807. end:
  808. #endif
  809. if ((*objp = (void *) pol) != NULL)
  810. *obj_refp = &pol->refcnt;
  811. return err;
  812. }
  813. static inline int policy_to_flow_dir(int dir)
  814. {
  815. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  816. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  817. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  818. return dir;
  819. switch (dir) {
  820. default:
  821. case XFRM_POLICY_IN:
  822. return FLOW_DIR_IN;
  823. case XFRM_POLICY_OUT:
  824. return FLOW_DIR_OUT;
  825. case XFRM_POLICY_FWD:
  826. return FLOW_DIR_FWD;
  827. }
  828. }
  829. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  830. {
  831. struct xfrm_policy *pol;
  832. read_lock_bh(&xfrm_policy_lock);
  833. if ((pol = sk->sk_policy[dir]) != NULL) {
  834. int match = xfrm_selector_match(&pol->selector, fl,
  835. sk->sk_family);
  836. int err = 0;
  837. if (match) {
  838. err = security_xfrm_policy_lookup(pol, fl->secid,
  839. policy_to_flow_dir(dir));
  840. if (!err)
  841. xfrm_pol_hold(pol);
  842. else if (err == -ESRCH)
  843. pol = NULL;
  844. else
  845. pol = ERR_PTR(err);
  846. } else
  847. pol = NULL;
  848. }
  849. read_unlock_bh(&xfrm_policy_lock);
  850. return pol;
  851. }
  852. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  853. {
  854. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  855. pol->family, dir);
  856. hlist_add_head(&pol->bydst, chain);
  857. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  858. xfrm_policy_count[dir]++;
  859. xfrm_pol_hold(pol);
  860. if (xfrm_bydst_should_resize(dir, NULL))
  861. schedule_work(&xfrm_hash_work);
  862. }
  863. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  864. int dir)
  865. {
  866. if (hlist_unhashed(&pol->bydst))
  867. return NULL;
  868. hlist_del(&pol->bydst);
  869. hlist_del(&pol->byidx);
  870. xfrm_policy_count[dir]--;
  871. return pol;
  872. }
  873. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  874. {
  875. write_lock_bh(&xfrm_policy_lock);
  876. pol = __xfrm_policy_unlink(pol, dir);
  877. write_unlock_bh(&xfrm_policy_lock);
  878. if (pol) {
  879. if (dir < XFRM_POLICY_MAX)
  880. atomic_inc(&flow_cache_genid);
  881. xfrm_policy_kill(pol);
  882. return 0;
  883. }
  884. return -ENOENT;
  885. }
  886. EXPORT_SYMBOL(xfrm_policy_delete);
  887. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  888. {
  889. struct xfrm_policy *old_pol;
  890. #ifdef CONFIG_XFRM_SUB_POLICY
  891. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  892. return -EINVAL;
  893. #endif
  894. write_lock_bh(&xfrm_policy_lock);
  895. old_pol = sk->sk_policy[dir];
  896. sk->sk_policy[dir] = pol;
  897. if (pol) {
  898. pol->curlft.add_time = get_seconds();
  899. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  900. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  901. }
  902. if (old_pol)
  903. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  904. write_unlock_bh(&xfrm_policy_lock);
  905. if (old_pol) {
  906. xfrm_policy_kill(old_pol);
  907. }
  908. return 0;
  909. }
  910. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  911. {
  912. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  913. if (newp) {
  914. newp->selector = old->selector;
  915. if (security_xfrm_policy_clone(old, newp)) {
  916. kfree(newp);
  917. return NULL; /* ENOMEM */
  918. }
  919. newp->lft = old->lft;
  920. newp->curlft = old->curlft;
  921. newp->action = old->action;
  922. newp->flags = old->flags;
  923. newp->xfrm_nr = old->xfrm_nr;
  924. newp->index = old->index;
  925. newp->type = old->type;
  926. memcpy(newp->xfrm_vec, old->xfrm_vec,
  927. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  928. write_lock_bh(&xfrm_policy_lock);
  929. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  930. write_unlock_bh(&xfrm_policy_lock);
  931. xfrm_pol_put(newp);
  932. }
  933. return newp;
  934. }
  935. int __xfrm_sk_clone_policy(struct sock *sk)
  936. {
  937. struct xfrm_policy *p0 = sk->sk_policy[0],
  938. *p1 = sk->sk_policy[1];
  939. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  940. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  941. return -ENOMEM;
  942. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  943. return -ENOMEM;
  944. return 0;
  945. }
  946. static int
  947. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  948. unsigned short family)
  949. {
  950. int err;
  951. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  952. if (unlikely(afinfo == NULL))
  953. return -EINVAL;
  954. err = afinfo->get_saddr(local, remote);
  955. xfrm_policy_put_afinfo(afinfo);
  956. return err;
  957. }
  958. /* Resolve list of templates for the flow, given policy. */
  959. static int
  960. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  961. struct xfrm_state **xfrm,
  962. unsigned short family)
  963. {
  964. int nx;
  965. int i, error;
  966. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  967. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  968. xfrm_address_t tmp;
  969. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  970. struct xfrm_state *x;
  971. xfrm_address_t *remote = daddr;
  972. xfrm_address_t *local = saddr;
  973. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  974. if (tmpl->mode == XFRM_MODE_TUNNEL ||
  975. tmpl->mode == XFRM_MODE_BEET) {
  976. remote = &tmpl->id.daddr;
  977. local = &tmpl->saddr;
  978. family = tmpl->encap_family;
  979. if (xfrm_addr_any(local, family)) {
  980. error = xfrm_get_saddr(&tmp, remote, family);
  981. if (error)
  982. goto fail;
  983. local = &tmp;
  984. }
  985. }
  986. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  987. if (x && x->km.state == XFRM_STATE_VALID) {
  988. xfrm[nx++] = x;
  989. daddr = remote;
  990. saddr = local;
  991. continue;
  992. }
  993. if (x) {
  994. error = (x->km.state == XFRM_STATE_ERROR ?
  995. -EINVAL : -EAGAIN);
  996. xfrm_state_put(x);
  997. }
  998. if (!tmpl->optional)
  999. goto fail;
  1000. }
  1001. return nx;
  1002. fail:
  1003. for (nx--; nx>=0; nx--)
  1004. xfrm_state_put(xfrm[nx]);
  1005. return error;
  1006. }
  1007. static int
  1008. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1009. struct xfrm_state **xfrm,
  1010. unsigned short family)
  1011. {
  1012. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1013. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1014. int cnx = 0;
  1015. int error;
  1016. int ret;
  1017. int i;
  1018. for (i = 0; i < npols; i++) {
  1019. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1020. error = -ENOBUFS;
  1021. goto fail;
  1022. }
  1023. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1024. if (ret < 0) {
  1025. error = ret;
  1026. goto fail;
  1027. } else
  1028. cnx += ret;
  1029. }
  1030. /* found states are sorted for outbound processing */
  1031. if (npols > 1)
  1032. xfrm_state_sort(xfrm, tpp, cnx, family);
  1033. return cnx;
  1034. fail:
  1035. for (cnx--; cnx>=0; cnx--)
  1036. xfrm_state_put(tpp[cnx]);
  1037. return error;
  1038. }
  1039. /* Check that the bundle accepts the flow and its components are
  1040. * still valid.
  1041. */
  1042. static struct dst_entry *
  1043. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1044. {
  1045. struct dst_entry *x;
  1046. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1047. if (unlikely(afinfo == NULL))
  1048. return ERR_PTR(-EINVAL);
  1049. x = afinfo->find_bundle(fl, policy);
  1050. xfrm_policy_put_afinfo(afinfo);
  1051. return x;
  1052. }
  1053. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1054. * all the metrics... Shortly, bundle a bundle.
  1055. */
  1056. static int
  1057. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1058. struct flowi *fl, struct dst_entry **dst_p,
  1059. unsigned short family)
  1060. {
  1061. int err;
  1062. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1063. if (unlikely(afinfo == NULL))
  1064. return -EINVAL;
  1065. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1066. xfrm_policy_put_afinfo(afinfo);
  1067. return err;
  1068. }
  1069. static int inline
  1070. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1071. {
  1072. if (!*target) {
  1073. *target = kmalloc(size, GFP_ATOMIC);
  1074. if (!*target)
  1075. return -ENOMEM;
  1076. }
  1077. memcpy(*target, src, size);
  1078. return 0;
  1079. }
  1080. static int inline
  1081. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1082. {
  1083. #ifdef CONFIG_XFRM_SUB_POLICY
  1084. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1085. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1086. sel, sizeof(*sel));
  1087. #else
  1088. return 0;
  1089. #endif
  1090. }
  1091. static int inline
  1092. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1093. {
  1094. #ifdef CONFIG_XFRM_SUB_POLICY
  1095. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1096. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1097. #else
  1098. return 0;
  1099. #endif
  1100. }
  1101. static int stale_bundle(struct dst_entry *dst);
  1102. /* Main function: finds/creates a bundle for given flow.
  1103. *
  1104. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1105. * on interfaces with disabled IPsec.
  1106. */
  1107. int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1108. struct sock *sk, int flags)
  1109. {
  1110. struct xfrm_policy *policy;
  1111. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1112. int npols;
  1113. int pol_dead;
  1114. int xfrm_nr;
  1115. int pi;
  1116. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1117. struct dst_entry *dst, *dst_orig = *dst_p;
  1118. int nx = 0;
  1119. int err;
  1120. u32 genid;
  1121. u16 family;
  1122. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1123. restart:
  1124. genid = atomic_read(&flow_cache_genid);
  1125. policy = NULL;
  1126. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1127. pols[pi] = NULL;
  1128. npols = 0;
  1129. pol_dead = 0;
  1130. xfrm_nr = 0;
  1131. if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
  1132. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1133. if (IS_ERR(policy))
  1134. return PTR_ERR(policy);
  1135. }
  1136. if (!policy) {
  1137. /* To accelerate a bit... */
  1138. if ((dst_orig->flags & DST_NOXFRM) ||
  1139. !xfrm_policy_count[XFRM_POLICY_OUT])
  1140. return 0;
  1141. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1142. dir, xfrm_policy_lookup);
  1143. if (IS_ERR(policy))
  1144. return PTR_ERR(policy);
  1145. }
  1146. if (!policy)
  1147. return 0;
  1148. family = dst_orig->ops->family;
  1149. policy->curlft.use_time = get_seconds();
  1150. pols[0] = policy;
  1151. npols ++;
  1152. xfrm_nr += pols[0]->xfrm_nr;
  1153. switch (policy->action) {
  1154. default:
  1155. case XFRM_POLICY_BLOCK:
  1156. /* Prohibit the flow */
  1157. err = -EPERM;
  1158. goto error;
  1159. case XFRM_POLICY_ALLOW:
  1160. #ifndef CONFIG_XFRM_SUB_POLICY
  1161. if (policy->xfrm_nr == 0) {
  1162. /* Flow passes not transformed. */
  1163. xfrm_pol_put(policy);
  1164. return 0;
  1165. }
  1166. #endif
  1167. /* Try to find matching bundle.
  1168. *
  1169. * LATER: help from flow cache. It is optional, this
  1170. * is required only for output policy.
  1171. */
  1172. dst = xfrm_find_bundle(fl, policy, family);
  1173. if (IS_ERR(dst)) {
  1174. err = PTR_ERR(dst);
  1175. goto error;
  1176. }
  1177. if (dst)
  1178. break;
  1179. #ifdef CONFIG_XFRM_SUB_POLICY
  1180. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1181. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1182. fl, family,
  1183. XFRM_POLICY_OUT);
  1184. if (pols[1]) {
  1185. if (IS_ERR(pols[1])) {
  1186. err = PTR_ERR(pols[1]);
  1187. goto error;
  1188. }
  1189. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1190. err = -EPERM;
  1191. goto error;
  1192. }
  1193. npols ++;
  1194. xfrm_nr += pols[1]->xfrm_nr;
  1195. }
  1196. }
  1197. /*
  1198. * Because neither flowi nor bundle information knows about
  1199. * transformation template size. On more than one policy usage
  1200. * we can realize whether all of them is bypass or not after
  1201. * they are searched. See above not-transformed bypass
  1202. * is surrounded by non-sub policy configuration, too.
  1203. */
  1204. if (xfrm_nr == 0) {
  1205. /* Flow passes not transformed. */
  1206. xfrm_pols_put(pols, npols);
  1207. return 0;
  1208. }
  1209. #endif
  1210. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1211. if (unlikely(nx<0)) {
  1212. err = nx;
  1213. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1214. /* EREMOTE tells the caller to generate
  1215. * a one-shot blackhole route.
  1216. */
  1217. xfrm_pol_put(policy);
  1218. return -EREMOTE;
  1219. }
  1220. if (err == -EAGAIN && flags) {
  1221. DECLARE_WAITQUEUE(wait, current);
  1222. add_wait_queue(&km_waitq, &wait);
  1223. set_current_state(TASK_INTERRUPTIBLE);
  1224. schedule();
  1225. set_current_state(TASK_RUNNING);
  1226. remove_wait_queue(&km_waitq, &wait);
  1227. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1228. if (nx == -EAGAIN && signal_pending(current)) {
  1229. err = -ERESTART;
  1230. goto error;
  1231. }
  1232. if (nx == -EAGAIN ||
  1233. genid != atomic_read(&flow_cache_genid)) {
  1234. xfrm_pols_put(pols, npols);
  1235. goto restart;
  1236. }
  1237. err = nx;
  1238. }
  1239. if (err < 0)
  1240. goto error;
  1241. }
  1242. if (nx == 0) {
  1243. /* Flow passes not transformed. */
  1244. xfrm_pols_put(pols, npols);
  1245. return 0;
  1246. }
  1247. dst = dst_orig;
  1248. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1249. if (unlikely(err)) {
  1250. int i;
  1251. for (i=0; i<nx; i++)
  1252. xfrm_state_put(xfrm[i]);
  1253. goto error;
  1254. }
  1255. for (pi = 0; pi < npols; pi++) {
  1256. read_lock_bh(&pols[pi]->lock);
  1257. pol_dead |= pols[pi]->dead;
  1258. read_unlock_bh(&pols[pi]->lock);
  1259. }
  1260. write_lock_bh(&policy->lock);
  1261. if (unlikely(pol_dead || stale_bundle(dst))) {
  1262. /* Wow! While we worked on resolving, this
  1263. * policy has gone. Retry. It is not paranoia,
  1264. * we just cannot enlist new bundle to dead object.
  1265. * We can't enlist stable bundles either.
  1266. */
  1267. write_unlock_bh(&policy->lock);
  1268. if (dst)
  1269. dst_free(dst);
  1270. err = -EHOSTUNREACH;
  1271. goto error;
  1272. }
  1273. if (npols > 1)
  1274. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1275. else
  1276. err = xfrm_dst_update_origin(dst, fl);
  1277. if (unlikely(err)) {
  1278. write_unlock_bh(&policy->lock);
  1279. if (dst)
  1280. dst_free(dst);
  1281. goto error;
  1282. }
  1283. dst->next = policy->bundles;
  1284. policy->bundles = dst;
  1285. dst_hold(dst);
  1286. write_unlock_bh(&policy->lock);
  1287. }
  1288. *dst_p = dst;
  1289. dst_release(dst_orig);
  1290. xfrm_pols_put(pols, npols);
  1291. return 0;
  1292. error:
  1293. dst_release(dst_orig);
  1294. xfrm_pols_put(pols, npols);
  1295. *dst_p = NULL;
  1296. return err;
  1297. }
  1298. EXPORT_SYMBOL(__xfrm_lookup);
  1299. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1300. struct sock *sk, int flags)
  1301. {
  1302. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1303. if (err == -EREMOTE) {
  1304. dst_release(*dst_p);
  1305. *dst_p = NULL;
  1306. err = -EAGAIN;
  1307. }
  1308. return err;
  1309. }
  1310. EXPORT_SYMBOL(xfrm_lookup);
  1311. static inline int
  1312. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1313. {
  1314. struct xfrm_state *x;
  1315. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1316. return 0;
  1317. x = skb->sp->xvec[idx];
  1318. if (!x->type->reject)
  1319. return 0;
  1320. return x->type->reject(x, skb, fl);
  1321. }
  1322. /* When skb is transformed back to its "native" form, we have to
  1323. * check policy restrictions. At the moment we make this in maximally
  1324. * stupid way. Shame on me. :-) Of course, connected sockets must
  1325. * have policy cached at them.
  1326. */
  1327. static inline int
  1328. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1329. unsigned short family)
  1330. {
  1331. if (xfrm_state_kern(x))
  1332. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1333. return x->id.proto == tmpl->id.proto &&
  1334. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1335. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1336. x->props.mode == tmpl->mode &&
  1337. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1338. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1339. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1340. xfrm_state_addr_cmp(tmpl, x, family));
  1341. }
  1342. /*
  1343. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1344. * because of optional transport mode, or next index of the mathced secpath
  1345. * state with the template.
  1346. * -1 is returned when no matching template is found.
  1347. * Otherwise "-2 - errored_index" is returned.
  1348. */
  1349. static inline int
  1350. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1351. unsigned short family)
  1352. {
  1353. int idx = start;
  1354. if (tmpl->optional) {
  1355. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1356. return start;
  1357. } else
  1358. start = -1;
  1359. for (; idx < sp->len; idx++) {
  1360. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1361. return ++idx;
  1362. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1363. if (start == -1)
  1364. start = -2-idx;
  1365. break;
  1366. }
  1367. }
  1368. return start;
  1369. }
  1370. int
  1371. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1372. {
  1373. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1374. int err;
  1375. if (unlikely(afinfo == NULL))
  1376. return -EAFNOSUPPORT;
  1377. afinfo->decode_session(skb, fl);
  1378. err = security_xfrm_decode_session(skb, &fl->secid);
  1379. xfrm_policy_put_afinfo(afinfo);
  1380. return err;
  1381. }
  1382. EXPORT_SYMBOL(xfrm_decode_session);
  1383. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1384. {
  1385. for (; k < sp->len; k++) {
  1386. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1387. *idxp = k;
  1388. return 1;
  1389. }
  1390. }
  1391. return 0;
  1392. }
  1393. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1394. unsigned short family)
  1395. {
  1396. struct xfrm_policy *pol;
  1397. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1398. int npols = 0;
  1399. int xfrm_nr;
  1400. int pi;
  1401. struct flowi fl;
  1402. u8 fl_dir = policy_to_flow_dir(dir);
  1403. int xerr_idx = -1;
  1404. if (xfrm_decode_session(skb, &fl, family) < 0)
  1405. return 0;
  1406. nf_nat_decode_session(skb, &fl, family);
  1407. /* First, check used SA against their selectors. */
  1408. if (skb->sp) {
  1409. int i;
  1410. for (i=skb->sp->len-1; i>=0; i--) {
  1411. struct xfrm_state *x = skb->sp->xvec[i];
  1412. if (!xfrm_selector_match(&x->sel, &fl, family))
  1413. return 0;
  1414. }
  1415. }
  1416. pol = NULL;
  1417. if (sk && sk->sk_policy[dir]) {
  1418. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1419. if (IS_ERR(pol))
  1420. return 0;
  1421. }
  1422. if (!pol)
  1423. pol = flow_cache_lookup(&fl, family, fl_dir,
  1424. xfrm_policy_lookup);
  1425. if (IS_ERR(pol))
  1426. return 0;
  1427. if (!pol) {
  1428. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1429. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1430. return 0;
  1431. }
  1432. return 1;
  1433. }
  1434. pol->curlft.use_time = get_seconds();
  1435. pols[0] = pol;
  1436. npols ++;
  1437. #ifdef CONFIG_XFRM_SUB_POLICY
  1438. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1439. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1440. &fl, family,
  1441. XFRM_POLICY_IN);
  1442. if (pols[1]) {
  1443. if (IS_ERR(pols[1]))
  1444. return 0;
  1445. pols[1]->curlft.use_time = get_seconds();
  1446. npols ++;
  1447. }
  1448. }
  1449. #endif
  1450. if (pol->action == XFRM_POLICY_ALLOW) {
  1451. struct sec_path *sp;
  1452. static struct sec_path dummy;
  1453. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1454. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1455. struct xfrm_tmpl **tpp = tp;
  1456. int ti = 0;
  1457. int i, k;
  1458. if ((sp = skb->sp) == NULL)
  1459. sp = &dummy;
  1460. for (pi = 0; pi < npols; pi++) {
  1461. if (pols[pi] != pol &&
  1462. pols[pi]->action != XFRM_POLICY_ALLOW)
  1463. goto reject;
  1464. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1465. goto reject_error;
  1466. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1467. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1468. }
  1469. xfrm_nr = ti;
  1470. if (npols > 1) {
  1471. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1472. tpp = stp;
  1473. }
  1474. /* For each tunnel xfrm, find the first matching tmpl.
  1475. * For each tmpl before that, find corresponding xfrm.
  1476. * Order is _important_. Later we will implement
  1477. * some barriers, but at the moment barriers
  1478. * are implied between each two transformations.
  1479. */
  1480. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1481. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1482. if (k < 0) {
  1483. if (k < -1)
  1484. /* "-2 - errored_index" returned */
  1485. xerr_idx = -(2+k);
  1486. goto reject;
  1487. }
  1488. }
  1489. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1490. goto reject;
  1491. xfrm_pols_put(pols, npols);
  1492. return 1;
  1493. }
  1494. reject:
  1495. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1496. reject_error:
  1497. xfrm_pols_put(pols, npols);
  1498. return 0;
  1499. }
  1500. EXPORT_SYMBOL(__xfrm_policy_check);
  1501. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1502. {
  1503. struct flowi fl;
  1504. if (xfrm_decode_session(skb, &fl, family) < 0)
  1505. return 0;
  1506. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1507. }
  1508. EXPORT_SYMBOL(__xfrm_route_forward);
  1509. /* Optimize later using cookies and generation ids. */
  1510. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1511. {
  1512. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1513. * to "-1" to force all XFRM destinations to get validated by
  1514. * dst_ops->check on every use. We do this because when a
  1515. * normal route referenced by an XFRM dst is obsoleted we do
  1516. * not go looking around for all parent referencing XFRM dsts
  1517. * so that we can invalidate them. It is just too much work.
  1518. * Instead we make the checks here on every use. For example:
  1519. *
  1520. * XFRM dst A --> IPv4 dst X
  1521. *
  1522. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1523. * in this example). If X is marked obsolete, "A" will not
  1524. * notice. That's what we are validating here via the
  1525. * stale_bundle() check.
  1526. *
  1527. * When a policy's bundle is pruned, we dst_free() the XFRM
  1528. * dst which causes it's ->obsolete field to be set to a
  1529. * positive non-zero integer. If an XFRM dst has been pruned
  1530. * like this, we want to force a new route lookup.
  1531. */
  1532. if (dst->obsolete < 0 && !stale_bundle(dst))
  1533. return dst;
  1534. return NULL;
  1535. }
  1536. static int stale_bundle(struct dst_entry *dst)
  1537. {
  1538. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1539. }
  1540. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1541. {
  1542. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1543. dst->dev = init_net.loopback_dev;
  1544. dev_hold(dst->dev);
  1545. dev_put(dev);
  1546. }
  1547. }
  1548. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1549. static void xfrm_link_failure(struct sk_buff *skb)
  1550. {
  1551. /* Impossible. Such dst must be popped before reaches point of failure. */
  1552. return;
  1553. }
  1554. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1555. {
  1556. if (dst) {
  1557. if (dst->obsolete) {
  1558. dst_release(dst);
  1559. dst = NULL;
  1560. }
  1561. }
  1562. return dst;
  1563. }
  1564. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1565. {
  1566. struct dst_entry *dst, **dstp;
  1567. write_lock(&pol->lock);
  1568. dstp = &pol->bundles;
  1569. while ((dst=*dstp) != NULL) {
  1570. if (func(dst)) {
  1571. *dstp = dst->next;
  1572. dst->next = *gc_list_p;
  1573. *gc_list_p = dst;
  1574. } else {
  1575. dstp = &dst->next;
  1576. }
  1577. }
  1578. write_unlock(&pol->lock);
  1579. }
  1580. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1581. {
  1582. struct dst_entry *gc_list = NULL;
  1583. int dir;
  1584. read_lock_bh(&xfrm_policy_lock);
  1585. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1586. struct xfrm_policy *pol;
  1587. struct hlist_node *entry;
  1588. struct hlist_head *table;
  1589. int i;
  1590. hlist_for_each_entry(pol, entry,
  1591. &xfrm_policy_inexact[dir], bydst)
  1592. prune_one_bundle(pol, func, &gc_list);
  1593. table = xfrm_policy_bydst[dir].table;
  1594. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1595. hlist_for_each_entry(pol, entry, table + i, bydst)
  1596. prune_one_bundle(pol, func, &gc_list);
  1597. }
  1598. }
  1599. read_unlock_bh(&xfrm_policy_lock);
  1600. while (gc_list) {
  1601. struct dst_entry *dst = gc_list;
  1602. gc_list = dst->next;
  1603. dst_free(dst);
  1604. }
  1605. }
  1606. static int unused_bundle(struct dst_entry *dst)
  1607. {
  1608. return !atomic_read(&dst->__refcnt);
  1609. }
  1610. static void __xfrm_garbage_collect(void)
  1611. {
  1612. xfrm_prune_bundles(unused_bundle);
  1613. }
  1614. static int xfrm_flush_bundles(void)
  1615. {
  1616. xfrm_prune_bundles(stale_bundle);
  1617. return 0;
  1618. }
  1619. void xfrm_init_pmtu(struct dst_entry *dst)
  1620. {
  1621. do {
  1622. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1623. u32 pmtu, route_mtu_cached;
  1624. pmtu = dst_mtu(dst->child);
  1625. xdst->child_mtu_cached = pmtu;
  1626. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1627. route_mtu_cached = dst_mtu(xdst->route);
  1628. xdst->route_mtu_cached = route_mtu_cached;
  1629. if (pmtu > route_mtu_cached)
  1630. pmtu = route_mtu_cached;
  1631. dst->metrics[RTAX_MTU-1] = pmtu;
  1632. } while ((dst = dst->next));
  1633. }
  1634. EXPORT_SYMBOL(xfrm_init_pmtu);
  1635. /* Check that the bundle accepts the flow and its components are
  1636. * still valid.
  1637. */
  1638. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1639. struct flowi *fl, int family, int strict)
  1640. {
  1641. struct dst_entry *dst = &first->u.dst;
  1642. struct xfrm_dst *last;
  1643. u32 mtu;
  1644. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1645. (dst->dev && !netif_running(dst->dev)))
  1646. return 0;
  1647. #ifdef CONFIG_XFRM_SUB_POLICY
  1648. if (fl) {
  1649. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1650. return 0;
  1651. if (first->partner &&
  1652. !xfrm_selector_match(first->partner, fl, family))
  1653. return 0;
  1654. }
  1655. #endif
  1656. last = NULL;
  1657. do {
  1658. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1659. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1660. return 0;
  1661. if (fl && pol &&
  1662. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1663. return 0;
  1664. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1665. return 0;
  1666. if (xdst->genid != dst->xfrm->genid)
  1667. return 0;
  1668. if (strict && fl &&
  1669. !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1670. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1671. return 0;
  1672. mtu = dst_mtu(dst->child);
  1673. if (xdst->child_mtu_cached != mtu) {
  1674. last = xdst;
  1675. xdst->child_mtu_cached = mtu;
  1676. }
  1677. if (!dst_check(xdst->route, xdst->route_cookie))
  1678. return 0;
  1679. mtu = dst_mtu(xdst->route);
  1680. if (xdst->route_mtu_cached != mtu) {
  1681. last = xdst;
  1682. xdst->route_mtu_cached = mtu;
  1683. }
  1684. dst = dst->child;
  1685. } while (dst->xfrm);
  1686. if (likely(!last))
  1687. return 1;
  1688. mtu = last->child_mtu_cached;
  1689. for (;;) {
  1690. dst = &last->u.dst;
  1691. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1692. if (mtu > last->route_mtu_cached)
  1693. mtu = last->route_mtu_cached;
  1694. dst->metrics[RTAX_MTU-1] = mtu;
  1695. if (last == first)
  1696. break;
  1697. last = (struct xfrm_dst *)last->u.dst.next;
  1698. last->child_mtu_cached = mtu;
  1699. }
  1700. return 1;
  1701. }
  1702. EXPORT_SYMBOL(xfrm_bundle_ok);
  1703. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1704. {
  1705. int err = 0;
  1706. if (unlikely(afinfo == NULL))
  1707. return -EINVAL;
  1708. if (unlikely(afinfo->family >= NPROTO))
  1709. return -EAFNOSUPPORT;
  1710. write_lock_bh(&xfrm_policy_afinfo_lock);
  1711. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1712. err = -ENOBUFS;
  1713. else {
  1714. struct dst_ops *dst_ops = afinfo->dst_ops;
  1715. if (likely(dst_ops->kmem_cachep == NULL))
  1716. dst_ops->kmem_cachep = xfrm_dst_cache;
  1717. if (likely(dst_ops->check == NULL))
  1718. dst_ops->check = xfrm_dst_check;
  1719. if (likely(dst_ops->negative_advice == NULL))
  1720. dst_ops->negative_advice = xfrm_negative_advice;
  1721. if (likely(dst_ops->link_failure == NULL))
  1722. dst_ops->link_failure = xfrm_link_failure;
  1723. if (likely(afinfo->garbage_collect == NULL))
  1724. afinfo->garbage_collect = __xfrm_garbage_collect;
  1725. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1726. }
  1727. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1728. return err;
  1729. }
  1730. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1731. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1732. {
  1733. int err = 0;
  1734. if (unlikely(afinfo == NULL))
  1735. return -EINVAL;
  1736. if (unlikely(afinfo->family >= NPROTO))
  1737. return -EAFNOSUPPORT;
  1738. write_lock_bh(&xfrm_policy_afinfo_lock);
  1739. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1740. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1741. err = -EINVAL;
  1742. else {
  1743. struct dst_ops *dst_ops = afinfo->dst_ops;
  1744. xfrm_policy_afinfo[afinfo->family] = NULL;
  1745. dst_ops->kmem_cachep = NULL;
  1746. dst_ops->check = NULL;
  1747. dst_ops->negative_advice = NULL;
  1748. dst_ops->link_failure = NULL;
  1749. afinfo->garbage_collect = NULL;
  1750. }
  1751. }
  1752. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1753. return err;
  1754. }
  1755. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1756. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1757. {
  1758. struct xfrm_policy_afinfo *afinfo;
  1759. if (unlikely(family >= NPROTO))
  1760. return NULL;
  1761. read_lock(&xfrm_policy_afinfo_lock);
  1762. afinfo = xfrm_policy_afinfo[family];
  1763. if (unlikely(!afinfo))
  1764. read_unlock(&xfrm_policy_afinfo_lock);
  1765. return afinfo;
  1766. }
  1767. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1768. {
  1769. read_unlock(&xfrm_policy_afinfo_lock);
  1770. }
  1771. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1772. {
  1773. struct net_device *dev = ptr;
  1774. if (dev->nd_net != &init_net)
  1775. return NOTIFY_DONE;
  1776. switch (event) {
  1777. case NETDEV_DOWN:
  1778. xfrm_flush_bundles();
  1779. }
  1780. return NOTIFY_DONE;
  1781. }
  1782. static struct notifier_block xfrm_dev_notifier = {
  1783. xfrm_dev_event,
  1784. NULL,
  1785. 0
  1786. };
  1787. static void __init xfrm_policy_init(void)
  1788. {
  1789. unsigned int hmask, sz;
  1790. int dir;
  1791. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1792. sizeof(struct xfrm_dst),
  1793. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1794. NULL);
  1795. hmask = 8 - 1;
  1796. sz = (hmask+1) * sizeof(struct hlist_head);
  1797. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1798. xfrm_idx_hmask = hmask;
  1799. if (!xfrm_policy_byidx)
  1800. panic("XFRM: failed to allocate byidx hash\n");
  1801. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1802. struct xfrm_policy_hash *htab;
  1803. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1804. htab = &xfrm_policy_bydst[dir];
  1805. htab->table = xfrm_hash_alloc(sz);
  1806. htab->hmask = hmask;
  1807. if (!htab->table)
  1808. panic("XFRM: failed to allocate bydst hash\n");
  1809. }
  1810. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  1811. register_netdevice_notifier(&xfrm_dev_notifier);
  1812. }
  1813. void __init xfrm_init(void)
  1814. {
  1815. xfrm_state_init();
  1816. xfrm_policy_init();
  1817. xfrm_input_init();
  1818. }
  1819. #ifdef CONFIG_AUDITSYSCALL
  1820. static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  1821. struct audit_buffer *audit_buf)
  1822. {
  1823. if (xp->security)
  1824. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1825. xp->security->ctx_alg, xp->security->ctx_doi,
  1826. xp->security->ctx_str);
  1827. switch(xp->selector.family) {
  1828. case AF_INET:
  1829. audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
  1830. NIPQUAD(xp->selector.saddr.a4),
  1831. NIPQUAD(xp->selector.daddr.a4));
  1832. break;
  1833. case AF_INET6:
  1834. {
  1835. struct in6_addr saddr6, daddr6;
  1836. memcpy(&saddr6, xp->selector.saddr.a6,
  1837. sizeof(struct in6_addr));
  1838. memcpy(&daddr6, xp->selector.daddr.a6,
  1839. sizeof(struct in6_addr));
  1840. audit_log_format(audit_buf,
  1841. " src=" NIP6_FMT " dst=" NIP6_FMT,
  1842. NIP6(saddr6), NIP6(daddr6));
  1843. }
  1844. break;
  1845. }
  1846. }
  1847. void
  1848. xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1849. {
  1850. struct audit_buffer *audit_buf;
  1851. extern int audit_enabled;
  1852. if (audit_enabled == 0)
  1853. return;
  1854. audit_buf = xfrm_audit_start(sid, auid);
  1855. if (audit_buf == NULL)
  1856. return;
  1857. audit_log_format(audit_buf, " op=SPD-add res=%u", result);
  1858. xfrm_audit_common_policyinfo(xp, audit_buf);
  1859. audit_log_end(audit_buf);
  1860. }
  1861. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  1862. void
  1863. xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1864. {
  1865. struct audit_buffer *audit_buf;
  1866. extern int audit_enabled;
  1867. if (audit_enabled == 0)
  1868. return;
  1869. audit_buf = xfrm_audit_start(sid, auid);
  1870. if (audit_buf == NULL)
  1871. return;
  1872. audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
  1873. xfrm_audit_common_policyinfo(xp, audit_buf);
  1874. audit_log_end(audit_buf);
  1875. }
  1876. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  1877. #endif
  1878. #ifdef CONFIG_XFRM_MIGRATE
  1879. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  1880. struct xfrm_selector *sel_tgt)
  1881. {
  1882. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  1883. if (sel_tgt->family == sel_cmp->family &&
  1884. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  1885. sel_cmp->family) == 0 &&
  1886. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  1887. sel_cmp->family) == 0 &&
  1888. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  1889. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  1890. return 1;
  1891. }
  1892. } else {
  1893. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  1894. return 1;
  1895. }
  1896. }
  1897. return 0;
  1898. }
  1899. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  1900. u8 dir, u8 type)
  1901. {
  1902. struct xfrm_policy *pol, *ret = NULL;
  1903. struct hlist_node *entry;
  1904. struct hlist_head *chain;
  1905. u32 priority = ~0U;
  1906. read_lock_bh(&xfrm_policy_lock);
  1907. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  1908. hlist_for_each_entry(pol, entry, chain, bydst) {
  1909. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1910. pol->type == type) {
  1911. ret = pol;
  1912. priority = ret->priority;
  1913. break;
  1914. }
  1915. }
  1916. chain = &xfrm_policy_inexact[dir];
  1917. hlist_for_each_entry(pol, entry, chain, bydst) {
  1918. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1919. pol->type == type &&
  1920. pol->priority < priority) {
  1921. ret = pol;
  1922. break;
  1923. }
  1924. }
  1925. if (ret)
  1926. xfrm_pol_hold(ret);
  1927. read_unlock_bh(&xfrm_policy_lock);
  1928. return ret;
  1929. }
  1930. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  1931. {
  1932. int match = 0;
  1933. if (t->mode == m->mode && t->id.proto == m->proto &&
  1934. (m->reqid == 0 || t->reqid == m->reqid)) {
  1935. switch (t->mode) {
  1936. case XFRM_MODE_TUNNEL:
  1937. case XFRM_MODE_BEET:
  1938. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  1939. m->old_family) == 0 &&
  1940. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  1941. m->old_family) == 0) {
  1942. match = 1;
  1943. }
  1944. break;
  1945. case XFRM_MODE_TRANSPORT:
  1946. /* in case of transport mode, template does not store
  1947. any IP addresses, hence we just compare mode and
  1948. protocol */
  1949. match = 1;
  1950. break;
  1951. default:
  1952. break;
  1953. }
  1954. }
  1955. return match;
  1956. }
  1957. /* update endpoint address(es) of template(s) */
  1958. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  1959. struct xfrm_migrate *m, int num_migrate)
  1960. {
  1961. struct xfrm_migrate *mp;
  1962. struct dst_entry *dst;
  1963. int i, j, n = 0;
  1964. write_lock_bh(&pol->lock);
  1965. if (unlikely(pol->dead)) {
  1966. /* target policy has been deleted */
  1967. write_unlock_bh(&pol->lock);
  1968. return -ENOENT;
  1969. }
  1970. for (i = 0; i < pol->xfrm_nr; i++) {
  1971. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  1972. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  1973. continue;
  1974. n++;
  1975. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
  1976. pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
  1977. continue;
  1978. /* update endpoints */
  1979. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  1980. sizeof(pol->xfrm_vec[i].id.daddr));
  1981. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  1982. sizeof(pol->xfrm_vec[i].saddr));
  1983. pol->xfrm_vec[i].encap_family = mp->new_family;
  1984. /* flush bundles */
  1985. while ((dst = pol->bundles) != NULL) {
  1986. pol->bundles = dst->next;
  1987. dst_free(dst);
  1988. }
  1989. }
  1990. }
  1991. write_unlock_bh(&pol->lock);
  1992. if (!n)
  1993. return -ENODATA;
  1994. return 0;
  1995. }
  1996. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  1997. {
  1998. int i, j;
  1999. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2000. return -EINVAL;
  2001. for (i = 0; i < num_migrate; i++) {
  2002. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2003. m[i].old_family) == 0) &&
  2004. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2005. m[i].old_family) == 0))
  2006. return -EINVAL;
  2007. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2008. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2009. return -EINVAL;
  2010. /* check if there is any duplicated entry */
  2011. for (j = i + 1; j < num_migrate; j++) {
  2012. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2013. sizeof(m[i].old_daddr)) &&
  2014. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2015. sizeof(m[i].old_saddr)) &&
  2016. m[i].proto == m[j].proto &&
  2017. m[i].mode == m[j].mode &&
  2018. m[i].reqid == m[j].reqid &&
  2019. m[i].old_family == m[j].old_family)
  2020. return -EINVAL;
  2021. }
  2022. }
  2023. return 0;
  2024. }
  2025. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2026. struct xfrm_migrate *m, int num_migrate)
  2027. {
  2028. int i, err, nx_cur = 0, nx_new = 0;
  2029. struct xfrm_policy *pol = NULL;
  2030. struct xfrm_state *x, *xc;
  2031. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2032. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2033. struct xfrm_migrate *mp;
  2034. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2035. goto out;
  2036. /* Stage 1 - find policy */
  2037. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2038. err = -ENOENT;
  2039. goto out;
  2040. }
  2041. /* Stage 2 - find and update state(s) */
  2042. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2043. if ((x = xfrm_migrate_state_find(mp))) {
  2044. x_cur[nx_cur] = x;
  2045. nx_cur++;
  2046. if ((xc = xfrm_state_migrate(x, mp))) {
  2047. x_new[nx_new] = xc;
  2048. nx_new++;
  2049. } else {
  2050. err = -ENODATA;
  2051. goto restore_state;
  2052. }
  2053. }
  2054. }
  2055. /* Stage 3 - update policy */
  2056. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2057. goto restore_state;
  2058. /* Stage 4 - delete old state(s) */
  2059. if (nx_cur) {
  2060. xfrm_states_put(x_cur, nx_cur);
  2061. xfrm_states_delete(x_cur, nx_cur);
  2062. }
  2063. /* Stage 5 - announce */
  2064. km_migrate(sel, dir, type, m, num_migrate);
  2065. xfrm_pol_put(pol);
  2066. return 0;
  2067. out:
  2068. return err;
  2069. restore_state:
  2070. if (pol)
  2071. xfrm_pol_put(pol);
  2072. if (nx_cur)
  2073. xfrm_states_put(x_cur, nx_cur);
  2074. if (nx_new)
  2075. xfrm_states_delete(x_new, nx_new);
  2076. return err;
  2077. }
  2078. EXPORT_SYMBOL(xfrm_migrate);
  2079. #endif