xfrm_policy.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422
  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. err = PTR_ERR(policy);
  1134. if (IS_ERR(policy))
  1135. goto dropdst;
  1136. }
  1137. if (!policy) {
  1138. /* To accelerate a bit... */
  1139. if ((dst_orig->flags & DST_NOXFRM) ||
  1140. !xfrm_policy_count[XFRM_POLICY_OUT])
  1141. return 0;
  1142. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1143. dir, xfrm_policy_lookup);
  1144. err = PTR_ERR(policy);
  1145. if (IS_ERR(policy))
  1146. goto dropdst;
  1147. }
  1148. if (!policy)
  1149. return 0;
  1150. family = dst_orig->ops->family;
  1151. policy->curlft.use_time = get_seconds();
  1152. pols[0] = policy;
  1153. npols ++;
  1154. xfrm_nr += pols[0]->xfrm_nr;
  1155. switch (policy->action) {
  1156. default:
  1157. case XFRM_POLICY_BLOCK:
  1158. /* Prohibit the flow */
  1159. err = -EPERM;
  1160. goto error;
  1161. case XFRM_POLICY_ALLOW:
  1162. #ifndef CONFIG_XFRM_SUB_POLICY
  1163. if (policy->xfrm_nr == 0) {
  1164. /* Flow passes not transformed. */
  1165. xfrm_pol_put(policy);
  1166. return 0;
  1167. }
  1168. #endif
  1169. /* Try to find matching bundle.
  1170. *
  1171. * LATER: help from flow cache. It is optional, this
  1172. * is required only for output policy.
  1173. */
  1174. dst = xfrm_find_bundle(fl, policy, family);
  1175. if (IS_ERR(dst)) {
  1176. err = PTR_ERR(dst);
  1177. goto error;
  1178. }
  1179. if (dst)
  1180. break;
  1181. #ifdef CONFIG_XFRM_SUB_POLICY
  1182. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1183. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1184. fl, family,
  1185. XFRM_POLICY_OUT);
  1186. if (pols[1]) {
  1187. if (IS_ERR(pols[1])) {
  1188. err = PTR_ERR(pols[1]);
  1189. goto error;
  1190. }
  1191. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1192. err = -EPERM;
  1193. goto error;
  1194. }
  1195. npols ++;
  1196. xfrm_nr += pols[1]->xfrm_nr;
  1197. }
  1198. }
  1199. /*
  1200. * Because neither flowi nor bundle information knows about
  1201. * transformation template size. On more than one policy usage
  1202. * we can realize whether all of them is bypass or not after
  1203. * they are searched. See above not-transformed bypass
  1204. * is surrounded by non-sub policy configuration, too.
  1205. */
  1206. if (xfrm_nr == 0) {
  1207. /* Flow passes not transformed. */
  1208. xfrm_pols_put(pols, npols);
  1209. return 0;
  1210. }
  1211. #endif
  1212. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1213. if (unlikely(nx<0)) {
  1214. err = nx;
  1215. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1216. /* EREMOTE tells the caller to generate
  1217. * a one-shot blackhole route.
  1218. */
  1219. xfrm_pol_put(policy);
  1220. return -EREMOTE;
  1221. }
  1222. if (err == -EAGAIN && flags) {
  1223. DECLARE_WAITQUEUE(wait, current);
  1224. add_wait_queue(&km_waitq, &wait);
  1225. set_current_state(TASK_INTERRUPTIBLE);
  1226. schedule();
  1227. set_current_state(TASK_RUNNING);
  1228. remove_wait_queue(&km_waitq, &wait);
  1229. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1230. if (nx == -EAGAIN && signal_pending(current)) {
  1231. err = -ERESTART;
  1232. goto error;
  1233. }
  1234. if (nx == -EAGAIN ||
  1235. genid != atomic_read(&flow_cache_genid)) {
  1236. xfrm_pols_put(pols, npols);
  1237. goto restart;
  1238. }
  1239. err = nx;
  1240. }
  1241. if (err < 0)
  1242. goto error;
  1243. }
  1244. if (nx == 0) {
  1245. /* Flow passes not transformed. */
  1246. xfrm_pols_put(pols, npols);
  1247. return 0;
  1248. }
  1249. dst = dst_orig;
  1250. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1251. if (unlikely(err)) {
  1252. int i;
  1253. for (i=0; i<nx; i++)
  1254. xfrm_state_put(xfrm[i]);
  1255. goto error;
  1256. }
  1257. for (pi = 0; pi < npols; pi++) {
  1258. read_lock_bh(&pols[pi]->lock);
  1259. pol_dead |= pols[pi]->dead;
  1260. read_unlock_bh(&pols[pi]->lock);
  1261. }
  1262. write_lock_bh(&policy->lock);
  1263. if (unlikely(pol_dead || stale_bundle(dst))) {
  1264. /* Wow! While we worked on resolving, this
  1265. * policy has gone. Retry. It is not paranoia,
  1266. * we just cannot enlist new bundle to dead object.
  1267. * We can't enlist stable bundles either.
  1268. */
  1269. write_unlock_bh(&policy->lock);
  1270. if (dst)
  1271. dst_free(dst);
  1272. err = -EHOSTUNREACH;
  1273. goto error;
  1274. }
  1275. if (npols > 1)
  1276. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1277. else
  1278. err = xfrm_dst_update_origin(dst, fl);
  1279. if (unlikely(err)) {
  1280. write_unlock_bh(&policy->lock);
  1281. if (dst)
  1282. dst_free(dst);
  1283. goto error;
  1284. }
  1285. dst->next = policy->bundles;
  1286. policy->bundles = dst;
  1287. dst_hold(dst);
  1288. write_unlock_bh(&policy->lock);
  1289. }
  1290. *dst_p = dst;
  1291. dst_release(dst_orig);
  1292. xfrm_pols_put(pols, npols);
  1293. return 0;
  1294. error:
  1295. xfrm_pols_put(pols, npols);
  1296. dropdst:
  1297. dst_release(dst_orig);
  1298. *dst_p = NULL;
  1299. return err;
  1300. }
  1301. EXPORT_SYMBOL(__xfrm_lookup);
  1302. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1303. struct sock *sk, int flags)
  1304. {
  1305. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1306. if (err == -EREMOTE) {
  1307. dst_release(*dst_p);
  1308. *dst_p = NULL;
  1309. err = -EAGAIN;
  1310. }
  1311. return err;
  1312. }
  1313. EXPORT_SYMBOL(xfrm_lookup);
  1314. static inline int
  1315. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1316. {
  1317. struct xfrm_state *x;
  1318. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1319. return 0;
  1320. x = skb->sp->xvec[idx];
  1321. if (!x->type->reject)
  1322. return 0;
  1323. return x->type->reject(x, skb, fl);
  1324. }
  1325. /* When skb is transformed back to its "native" form, we have to
  1326. * check policy restrictions. At the moment we make this in maximally
  1327. * stupid way. Shame on me. :-) Of course, connected sockets must
  1328. * have policy cached at them.
  1329. */
  1330. static inline int
  1331. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1332. unsigned short family)
  1333. {
  1334. if (xfrm_state_kern(x))
  1335. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1336. return x->id.proto == tmpl->id.proto &&
  1337. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1338. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1339. x->props.mode == tmpl->mode &&
  1340. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1341. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1342. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1343. xfrm_state_addr_cmp(tmpl, x, family));
  1344. }
  1345. /*
  1346. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1347. * because of optional transport mode, or next index of the mathced secpath
  1348. * state with the template.
  1349. * -1 is returned when no matching template is found.
  1350. * Otherwise "-2 - errored_index" is returned.
  1351. */
  1352. static inline int
  1353. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1354. unsigned short family)
  1355. {
  1356. int idx = start;
  1357. if (tmpl->optional) {
  1358. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1359. return start;
  1360. } else
  1361. start = -1;
  1362. for (; idx < sp->len; idx++) {
  1363. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1364. return ++idx;
  1365. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1366. if (start == -1)
  1367. start = -2-idx;
  1368. break;
  1369. }
  1370. }
  1371. return start;
  1372. }
  1373. int
  1374. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1375. {
  1376. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1377. int err;
  1378. if (unlikely(afinfo == NULL))
  1379. return -EAFNOSUPPORT;
  1380. afinfo->decode_session(skb, fl);
  1381. err = security_xfrm_decode_session(skb, &fl->secid);
  1382. xfrm_policy_put_afinfo(afinfo);
  1383. return err;
  1384. }
  1385. EXPORT_SYMBOL(xfrm_decode_session);
  1386. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1387. {
  1388. for (; k < sp->len; k++) {
  1389. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1390. *idxp = k;
  1391. return 1;
  1392. }
  1393. }
  1394. return 0;
  1395. }
  1396. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1397. unsigned short family)
  1398. {
  1399. struct xfrm_policy *pol;
  1400. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1401. int npols = 0;
  1402. int xfrm_nr;
  1403. int pi;
  1404. struct flowi fl;
  1405. u8 fl_dir = policy_to_flow_dir(dir);
  1406. int xerr_idx = -1;
  1407. if (xfrm_decode_session(skb, &fl, family) < 0)
  1408. return 0;
  1409. nf_nat_decode_session(skb, &fl, family);
  1410. /* First, check used SA against their selectors. */
  1411. if (skb->sp) {
  1412. int i;
  1413. for (i=skb->sp->len-1; i>=0; i--) {
  1414. struct xfrm_state *x = skb->sp->xvec[i];
  1415. if (!xfrm_selector_match(&x->sel, &fl, family))
  1416. return 0;
  1417. }
  1418. }
  1419. pol = NULL;
  1420. if (sk && sk->sk_policy[dir]) {
  1421. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1422. if (IS_ERR(pol))
  1423. return 0;
  1424. }
  1425. if (!pol)
  1426. pol = flow_cache_lookup(&fl, family, fl_dir,
  1427. xfrm_policy_lookup);
  1428. if (IS_ERR(pol))
  1429. return 0;
  1430. if (!pol) {
  1431. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1432. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1433. return 0;
  1434. }
  1435. return 1;
  1436. }
  1437. pol->curlft.use_time = get_seconds();
  1438. pols[0] = pol;
  1439. npols ++;
  1440. #ifdef CONFIG_XFRM_SUB_POLICY
  1441. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1442. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1443. &fl, family,
  1444. XFRM_POLICY_IN);
  1445. if (pols[1]) {
  1446. if (IS_ERR(pols[1]))
  1447. return 0;
  1448. pols[1]->curlft.use_time = get_seconds();
  1449. npols ++;
  1450. }
  1451. }
  1452. #endif
  1453. if (pol->action == XFRM_POLICY_ALLOW) {
  1454. struct sec_path *sp;
  1455. static struct sec_path dummy;
  1456. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1457. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1458. struct xfrm_tmpl **tpp = tp;
  1459. int ti = 0;
  1460. int i, k;
  1461. if ((sp = skb->sp) == NULL)
  1462. sp = &dummy;
  1463. for (pi = 0; pi < npols; pi++) {
  1464. if (pols[pi] != pol &&
  1465. pols[pi]->action != XFRM_POLICY_ALLOW)
  1466. goto reject;
  1467. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1468. goto reject_error;
  1469. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1470. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1471. }
  1472. xfrm_nr = ti;
  1473. if (npols > 1) {
  1474. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1475. tpp = stp;
  1476. }
  1477. /* For each tunnel xfrm, find the first matching tmpl.
  1478. * For each tmpl before that, find corresponding xfrm.
  1479. * Order is _important_. Later we will implement
  1480. * some barriers, but at the moment barriers
  1481. * are implied between each two transformations.
  1482. */
  1483. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1484. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1485. if (k < 0) {
  1486. if (k < -1)
  1487. /* "-2 - errored_index" returned */
  1488. xerr_idx = -(2+k);
  1489. goto reject;
  1490. }
  1491. }
  1492. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1493. goto reject;
  1494. xfrm_pols_put(pols, npols);
  1495. return 1;
  1496. }
  1497. reject:
  1498. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1499. reject_error:
  1500. xfrm_pols_put(pols, npols);
  1501. return 0;
  1502. }
  1503. EXPORT_SYMBOL(__xfrm_policy_check);
  1504. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1505. {
  1506. struct flowi fl;
  1507. if (xfrm_decode_session(skb, &fl, family) < 0)
  1508. return 0;
  1509. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1510. }
  1511. EXPORT_SYMBOL(__xfrm_route_forward);
  1512. /* Optimize later using cookies and generation ids. */
  1513. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1514. {
  1515. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1516. * to "-1" to force all XFRM destinations to get validated by
  1517. * dst_ops->check on every use. We do this because when a
  1518. * normal route referenced by an XFRM dst is obsoleted we do
  1519. * not go looking around for all parent referencing XFRM dsts
  1520. * so that we can invalidate them. It is just too much work.
  1521. * Instead we make the checks here on every use. For example:
  1522. *
  1523. * XFRM dst A --> IPv4 dst X
  1524. *
  1525. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1526. * in this example). If X is marked obsolete, "A" will not
  1527. * notice. That's what we are validating here via the
  1528. * stale_bundle() check.
  1529. *
  1530. * When a policy's bundle is pruned, we dst_free() the XFRM
  1531. * dst which causes it's ->obsolete field to be set to a
  1532. * positive non-zero integer. If an XFRM dst has been pruned
  1533. * like this, we want to force a new route lookup.
  1534. */
  1535. if (dst->obsolete < 0 && !stale_bundle(dst))
  1536. return dst;
  1537. return NULL;
  1538. }
  1539. static int stale_bundle(struct dst_entry *dst)
  1540. {
  1541. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1542. }
  1543. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1544. {
  1545. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1546. dst->dev = init_net.loopback_dev;
  1547. dev_hold(dst->dev);
  1548. dev_put(dev);
  1549. }
  1550. }
  1551. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1552. static void xfrm_link_failure(struct sk_buff *skb)
  1553. {
  1554. /* Impossible. Such dst must be popped before reaches point of failure. */
  1555. return;
  1556. }
  1557. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1558. {
  1559. if (dst) {
  1560. if (dst->obsolete) {
  1561. dst_release(dst);
  1562. dst = NULL;
  1563. }
  1564. }
  1565. return dst;
  1566. }
  1567. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1568. {
  1569. struct dst_entry *dst, **dstp;
  1570. write_lock(&pol->lock);
  1571. dstp = &pol->bundles;
  1572. while ((dst=*dstp) != NULL) {
  1573. if (func(dst)) {
  1574. *dstp = dst->next;
  1575. dst->next = *gc_list_p;
  1576. *gc_list_p = dst;
  1577. } else {
  1578. dstp = &dst->next;
  1579. }
  1580. }
  1581. write_unlock(&pol->lock);
  1582. }
  1583. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1584. {
  1585. struct dst_entry *gc_list = NULL;
  1586. int dir;
  1587. read_lock_bh(&xfrm_policy_lock);
  1588. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1589. struct xfrm_policy *pol;
  1590. struct hlist_node *entry;
  1591. struct hlist_head *table;
  1592. int i;
  1593. hlist_for_each_entry(pol, entry,
  1594. &xfrm_policy_inexact[dir], bydst)
  1595. prune_one_bundle(pol, func, &gc_list);
  1596. table = xfrm_policy_bydst[dir].table;
  1597. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1598. hlist_for_each_entry(pol, entry, table + i, bydst)
  1599. prune_one_bundle(pol, func, &gc_list);
  1600. }
  1601. }
  1602. read_unlock_bh(&xfrm_policy_lock);
  1603. while (gc_list) {
  1604. struct dst_entry *dst = gc_list;
  1605. gc_list = dst->next;
  1606. dst_free(dst);
  1607. }
  1608. }
  1609. static int unused_bundle(struct dst_entry *dst)
  1610. {
  1611. return !atomic_read(&dst->__refcnt);
  1612. }
  1613. static void __xfrm_garbage_collect(void)
  1614. {
  1615. xfrm_prune_bundles(unused_bundle);
  1616. }
  1617. static int xfrm_flush_bundles(void)
  1618. {
  1619. xfrm_prune_bundles(stale_bundle);
  1620. return 0;
  1621. }
  1622. void xfrm_init_pmtu(struct dst_entry *dst)
  1623. {
  1624. do {
  1625. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1626. u32 pmtu, route_mtu_cached;
  1627. pmtu = dst_mtu(dst->child);
  1628. xdst->child_mtu_cached = pmtu;
  1629. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1630. route_mtu_cached = dst_mtu(xdst->route);
  1631. xdst->route_mtu_cached = route_mtu_cached;
  1632. if (pmtu > route_mtu_cached)
  1633. pmtu = route_mtu_cached;
  1634. dst->metrics[RTAX_MTU-1] = pmtu;
  1635. } while ((dst = dst->next));
  1636. }
  1637. EXPORT_SYMBOL(xfrm_init_pmtu);
  1638. /* Check that the bundle accepts the flow and its components are
  1639. * still valid.
  1640. */
  1641. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1642. struct flowi *fl, int family, int strict)
  1643. {
  1644. struct dst_entry *dst = &first->u.dst;
  1645. struct xfrm_dst *last;
  1646. u32 mtu;
  1647. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1648. (dst->dev && !netif_running(dst->dev)))
  1649. return 0;
  1650. #ifdef CONFIG_XFRM_SUB_POLICY
  1651. if (fl) {
  1652. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1653. return 0;
  1654. if (first->partner &&
  1655. !xfrm_selector_match(first->partner, fl, family))
  1656. return 0;
  1657. }
  1658. #endif
  1659. last = NULL;
  1660. do {
  1661. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1662. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1663. return 0;
  1664. if (fl && pol &&
  1665. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1666. return 0;
  1667. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1668. return 0;
  1669. if (xdst->genid != dst->xfrm->genid)
  1670. return 0;
  1671. if (strict && fl &&
  1672. !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1673. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1674. return 0;
  1675. mtu = dst_mtu(dst->child);
  1676. if (xdst->child_mtu_cached != mtu) {
  1677. last = xdst;
  1678. xdst->child_mtu_cached = mtu;
  1679. }
  1680. if (!dst_check(xdst->route, xdst->route_cookie))
  1681. return 0;
  1682. mtu = dst_mtu(xdst->route);
  1683. if (xdst->route_mtu_cached != mtu) {
  1684. last = xdst;
  1685. xdst->route_mtu_cached = mtu;
  1686. }
  1687. dst = dst->child;
  1688. } while (dst->xfrm);
  1689. if (likely(!last))
  1690. return 1;
  1691. mtu = last->child_mtu_cached;
  1692. for (;;) {
  1693. dst = &last->u.dst;
  1694. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1695. if (mtu > last->route_mtu_cached)
  1696. mtu = last->route_mtu_cached;
  1697. dst->metrics[RTAX_MTU-1] = mtu;
  1698. if (last == first)
  1699. break;
  1700. last = (struct xfrm_dst *)last->u.dst.next;
  1701. last->child_mtu_cached = mtu;
  1702. }
  1703. return 1;
  1704. }
  1705. EXPORT_SYMBOL(xfrm_bundle_ok);
  1706. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1707. {
  1708. int err = 0;
  1709. if (unlikely(afinfo == NULL))
  1710. return -EINVAL;
  1711. if (unlikely(afinfo->family >= NPROTO))
  1712. return -EAFNOSUPPORT;
  1713. write_lock_bh(&xfrm_policy_afinfo_lock);
  1714. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1715. err = -ENOBUFS;
  1716. else {
  1717. struct dst_ops *dst_ops = afinfo->dst_ops;
  1718. if (likely(dst_ops->kmem_cachep == NULL))
  1719. dst_ops->kmem_cachep = xfrm_dst_cache;
  1720. if (likely(dst_ops->check == NULL))
  1721. dst_ops->check = xfrm_dst_check;
  1722. if (likely(dst_ops->negative_advice == NULL))
  1723. dst_ops->negative_advice = xfrm_negative_advice;
  1724. if (likely(dst_ops->link_failure == NULL))
  1725. dst_ops->link_failure = xfrm_link_failure;
  1726. if (likely(afinfo->garbage_collect == NULL))
  1727. afinfo->garbage_collect = __xfrm_garbage_collect;
  1728. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1729. }
  1730. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1731. return err;
  1732. }
  1733. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1734. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1735. {
  1736. int err = 0;
  1737. if (unlikely(afinfo == NULL))
  1738. return -EINVAL;
  1739. if (unlikely(afinfo->family >= NPROTO))
  1740. return -EAFNOSUPPORT;
  1741. write_lock_bh(&xfrm_policy_afinfo_lock);
  1742. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1743. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1744. err = -EINVAL;
  1745. else {
  1746. struct dst_ops *dst_ops = afinfo->dst_ops;
  1747. xfrm_policy_afinfo[afinfo->family] = NULL;
  1748. dst_ops->kmem_cachep = NULL;
  1749. dst_ops->check = NULL;
  1750. dst_ops->negative_advice = NULL;
  1751. dst_ops->link_failure = NULL;
  1752. afinfo->garbage_collect = NULL;
  1753. }
  1754. }
  1755. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1756. return err;
  1757. }
  1758. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1759. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1760. {
  1761. struct xfrm_policy_afinfo *afinfo;
  1762. if (unlikely(family >= NPROTO))
  1763. return NULL;
  1764. read_lock(&xfrm_policy_afinfo_lock);
  1765. afinfo = xfrm_policy_afinfo[family];
  1766. if (unlikely(!afinfo))
  1767. read_unlock(&xfrm_policy_afinfo_lock);
  1768. return afinfo;
  1769. }
  1770. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1771. {
  1772. read_unlock(&xfrm_policy_afinfo_lock);
  1773. }
  1774. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1775. {
  1776. struct net_device *dev = ptr;
  1777. if (dev->nd_net != &init_net)
  1778. return NOTIFY_DONE;
  1779. switch (event) {
  1780. case NETDEV_DOWN:
  1781. xfrm_flush_bundles();
  1782. }
  1783. return NOTIFY_DONE;
  1784. }
  1785. static struct notifier_block xfrm_dev_notifier = {
  1786. xfrm_dev_event,
  1787. NULL,
  1788. 0
  1789. };
  1790. static void __init xfrm_policy_init(void)
  1791. {
  1792. unsigned int hmask, sz;
  1793. int dir;
  1794. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1795. sizeof(struct xfrm_dst),
  1796. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1797. NULL);
  1798. hmask = 8 - 1;
  1799. sz = (hmask+1) * sizeof(struct hlist_head);
  1800. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1801. xfrm_idx_hmask = hmask;
  1802. if (!xfrm_policy_byidx)
  1803. panic("XFRM: failed to allocate byidx hash\n");
  1804. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1805. struct xfrm_policy_hash *htab;
  1806. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1807. htab = &xfrm_policy_bydst[dir];
  1808. htab->table = xfrm_hash_alloc(sz);
  1809. htab->hmask = hmask;
  1810. if (!htab->table)
  1811. panic("XFRM: failed to allocate bydst hash\n");
  1812. }
  1813. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  1814. register_netdevice_notifier(&xfrm_dev_notifier);
  1815. }
  1816. void __init xfrm_init(void)
  1817. {
  1818. xfrm_state_init();
  1819. xfrm_policy_init();
  1820. xfrm_input_init();
  1821. }
  1822. #ifdef CONFIG_AUDITSYSCALL
  1823. static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  1824. struct audit_buffer *audit_buf)
  1825. {
  1826. if (xp->security)
  1827. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1828. xp->security->ctx_alg, xp->security->ctx_doi,
  1829. xp->security->ctx_str);
  1830. switch(xp->selector.family) {
  1831. case AF_INET:
  1832. audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
  1833. NIPQUAD(xp->selector.saddr.a4),
  1834. NIPQUAD(xp->selector.daddr.a4));
  1835. break;
  1836. case AF_INET6:
  1837. {
  1838. struct in6_addr saddr6, daddr6;
  1839. memcpy(&saddr6, xp->selector.saddr.a6,
  1840. sizeof(struct in6_addr));
  1841. memcpy(&daddr6, xp->selector.daddr.a6,
  1842. sizeof(struct in6_addr));
  1843. audit_log_format(audit_buf,
  1844. " src=" NIP6_FMT " dst=" NIP6_FMT,
  1845. NIP6(saddr6), NIP6(daddr6));
  1846. }
  1847. break;
  1848. }
  1849. }
  1850. void
  1851. xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1852. {
  1853. struct audit_buffer *audit_buf;
  1854. extern int audit_enabled;
  1855. if (audit_enabled == 0)
  1856. return;
  1857. audit_buf = xfrm_audit_start(auid, sid);
  1858. if (audit_buf == NULL)
  1859. return;
  1860. audit_log_format(audit_buf, " op=SPD-add res=%u", result);
  1861. xfrm_audit_common_policyinfo(xp, audit_buf);
  1862. audit_log_end(audit_buf);
  1863. }
  1864. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  1865. void
  1866. xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1867. {
  1868. struct audit_buffer *audit_buf;
  1869. extern int audit_enabled;
  1870. if (audit_enabled == 0)
  1871. return;
  1872. audit_buf = xfrm_audit_start(auid, sid);
  1873. if (audit_buf == NULL)
  1874. return;
  1875. audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
  1876. xfrm_audit_common_policyinfo(xp, audit_buf);
  1877. audit_log_end(audit_buf);
  1878. }
  1879. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  1880. #endif
  1881. #ifdef CONFIG_XFRM_MIGRATE
  1882. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  1883. struct xfrm_selector *sel_tgt)
  1884. {
  1885. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  1886. if (sel_tgt->family == sel_cmp->family &&
  1887. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  1888. sel_cmp->family) == 0 &&
  1889. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  1890. sel_cmp->family) == 0 &&
  1891. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  1892. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  1893. return 1;
  1894. }
  1895. } else {
  1896. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  1897. return 1;
  1898. }
  1899. }
  1900. return 0;
  1901. }
  1902. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  1903. u8 dir, u8 type)
  1904. {
  1905. struct xfrm_policy *pol, *ret = NULL;
  1906. struct hlist_node *entry;
  1907. struct hlist_head *chain;
  1908. u32 priority = ~0U;
  1909. read_lock_bh(&xfrm_policy_lock);
  1910. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  1911. hlist_for_each_entry(pol, entry, chain, bydst) {
  1912. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1913. pol->type == type) {
  1914. ret = pol;
  1915. priority = ret->priority;
  1916. break;
  1917. }
  1918. }
  1919. chain = &xfrm_policy_inexact[dir];
  1920. hlist_for_each_entry(pol, entry, chain, bydst) {
  1921. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1922. pol->type == type &&
  1923. pol->priority < priority) {
  1924. ret = pol;
  1925. break;
  1926. }
  1927. }
  1928. if (ret)
  1929. xfrm_pol_hold(ret);
  1930. read_unlock_bh(&xfrm_policy_lock);
  1931. return ret;
  1932. }
  1933. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  1934. {
  1935. int match = 0;
  1936. if (t->mode == m->mode && t->id.proto == m->proto &&
  1937. (m->reqid == 0 || t->reqid == m->reqid)) {
  1938. switch (t->mode) {
  1939. case XFRM_MODE_TUNNEL:
  1940. case XFRM_MODE_BEET:
  1941. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  1942. m->old_family) == 0 &&
  1943. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  1944. m->old_family) == 0) {
  1945. match = 1;
  1946. }
  1947. break;
  1948. case XFRM_MODE_TRANSPORT:
  1949. /* in case of transport mode, template does not store
  1950. any IP addresses, hence we just compare mode and
  1951. protocol */
  1952. match = 1;
  1953. break;
  1954. default:
  1955. break;
  1956. }
  1957. }
  1958. return match;
  1959. }
  1960. /* update endpoint address(es) of template(s) */
  1961. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  1962. struct xfrm_migrate *m, int num_migrate)
  1963. {
  1964. struct xfrm_migrate *mp;
  1965. struct dst_entry *dst;
  1966. int i, j, n = 0;
  1967. write_lock_bh(&pol->lock);
  1968. if (unlikely(pol->dead)) {
  1969. /* target policy has been deleted */
  1970. write_unlock_bh(&pol->lock);
  1971. return -ENOENT;
  1972. }
  1973. for (i = 0; i < pol->xfrm_nr; i++) {
  1974. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  1975. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  1976. continue;
  1977. n++;
  1978. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
  1979. pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
  1980. continue;
  1981. /* update endpoints */
  1982. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  1983. sizeof(pol->xfrm_vec[i].id.daddr));
  1984. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  1985. sizeof(pol->xfrm_vec[i].saddr));
  1986. pol->xfrm_vec[i].encap_family = mp->new_family;
  1987. /* flush bundles */
  1988. while ((dst = pol->bundles) != NULL) {
  1989. pol->bundles = dst->next;
  1990. dst_free(dst);
  1991. }
  1992. }
  1993. }
  1994. write_unlock_bh(&pol->lock);
  1995. if (!n)
  1996. return -ENODATA;
  1997. return 0;
  1998. }
  1999. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  2000. {
  2001. int i, j;
  2002. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2003. return -EINVAL;
  2004. for (i = 0; i < num_migrate; i++) {
  2005. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2006. m[i].old_family) == 0) &&
  2007. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2008. m[i].old_family) == 0))
  2009. return -EINVAL;
  2010. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2011. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2012. return -EINVAL;
  2013. /* check if there is any duplicated entry */
  2014. for (j = i + 1; j < num_migrate; j++) {
  2015. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2016. sizeof(m[i].old_daddr)) &&
  2017. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2018. sizeof(m[i].old_saddr)) &&
  2019. m[i].proto == m[j].proto &&
  2020. m[i].mode == m[j].mode &&
  2021. m[i].reqid == m[j].reqid &&
  2022. m[i].old_family == m[j].old_family)
  2023. return -EINVAL;
  2024. }
  2025. }
  2026. return 0;
  2027. }
  2028. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2029. struct xfrm_migrate *m, int num_migrate)
  2030. {
  2031. int i, err, nx_cur = 0, nx_new = 0;
  2032. struct xfrm_policy *pol = NULL;
  2033. struct xfrm_state *x, *xc;
  2034. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2035. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2036. struct xfrm_migrate *mp;
  2037. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2038. goto out;
  2039. /* Stage 1 - find policy */
  2040. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2041. err = -ENOENT;
  2042. goto out;
  2043. }
  2044. /* Stage 2 - find and update state(s) */
  2045. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2046. if ((x = xfrm_migrate_state_find(mp))) {
  2047. x_cur[nx_cur] = x;
  2048. nx_cur++;
  2049. if ((xc = xfrm_state_migrate(x, mp))) {
  2050. x_new[nx_new] = xc;
  2051. nx_new++;
  2052. } else {
  2053. err = -ENODATA;
  2054. goto restore_state;
  2055. }
  2056. }
  2057. }
  2058. /* Stage 3 - update policy */
  2059. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2060. goto restore_state;
  2061. /* Stage 4 - delete old state(s) */
  2062. if (nx_cur) {
  2063. xfrm_states_put(x_cur, nx_cur);
  2064. xfrm_states_delete(x_cur, nx_cur);
  2065. }
  2066. /* Stage 5 - announce */
  2067. km_migrate(sel, dir, type, m, num_migrate);
  2068. xfrm_pol_put(pol);
  2069. return 0;
  2070. out:
  2071. return err;
  2072. restore_state:
  2073. if (pol)
  2074. xfrm_pol_put(pol);
  2075. if (nx_cur)
  2076. xfrm_states_put(x_cur, nx_cur);
  2077. if (nx_new)
  2078. xfrm_states_delete(x_new, nx_new);
  2079. return err;
  2080. }
  2081. EXPORT_SYMBOL(xfrm_migrate);
  2082. #endif