xfrm_policy.c 49 KB

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