xfrm_policy.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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 <net/xfrm.h>
  25. #include <net/ip.h>
  26. DEFINE_MUTEX(xfrm_cfg_mutex);
  27. EXPORT_SYMBOL(xfrm_cfg_mutex);
  28. static DEFINE_RWLOCK(xfrm_policy_lock);
  29. struct xfrm_policy *xfrm_policy_list[XFRM_POLICY_MAX*2];
  30. EXPORT_SYMBOL(xfrm_policy_list);
  31. static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
  32. static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
  33. static kmem_cache_t *xfrm_dst_cache __read_mostly;
  34. static struct work_struct xfrm_policy_gc_work;
  35. static struct list_head xfrm_policy_gc_list =
  36. LIST_HEAD_INIT(xfrm_policy_gc_list);
  37. static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
  38. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
  39. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
  40. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
  41. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
  42. int xfrm_register_type(struct xfrm_type *type, unsigned short family)
  43. {
  44. struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
  45. struct xfrm_type **typemap;
  46. int err = 0;
  47. if (unlikely(afinfo == NULL))
  48. return -EAFNOSUPPORT;
  49. typemap = afinfo->type_map;
  50. if (likely(typemap[type->proto] == NULL))
  51. typemap[type->proto] = type;
  52. else
  53. err = -EEXIST;
  54. xfrm_policy_unlock_afinfo(afinfo);
  55. return err;
  56. }
  57. EXPORT_SYMBOL(xfrm_register_type);
  58. int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
  59. {
  60. struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
  61. struct xfrm_type **typemap;
  62. int err = 0;
  63. if (unlikely(afinfo == NULL))
  64. return -EAFNOSUPPORT;
  65. typemap = afinfo->type_map;
  66. if (unlikely(typemap[type->proto] != type))
  67. err = -ENOENT;
  68. else
  69. typemap[type->proto] = NULL;
  70. xfrm_policy_unlock_afinfo(afinfo);
  71. return err;
  72. }
  73. EXPORT_SYMBOL(xfrm_unregister_type);
  74. struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
  75. {
  76. struct xfrm_policy_afinfo *afinfo;
  77. struct xfrm_type **typemap;
  78. struct xfrm_type *type;
  79. int modload_attempted = 0;
  80. retry:
  81. afinfo = xfrm_policy_get_afinfo(family);
  82. if (unlikely(afinfo == NULL))
  83. return NULL;
  84. typemap = afinfo->type_map;
  85. type = typemap[proto];
  86. if (unlikely(type && !try_module_get(type->owner)))
  87. type = NULL;
  88. if (!type && !modload_attempted) {
  89. xfrm_policy_put_afinfo(afinfo);
  90. request_module("xfrm-type-%d-%d",
  91. (int) family, (int) proto);
  92. modload_attempted = 1;
  93. goto retry;
  94. }
  95. xfrm_policy_put_afinfo(afinfo);
  96. return type;
  97. }
  98. int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
  99. unsigned short family)
  100. {
  101. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  102. int err = 0;
  103. if (unlikely(afinfo == NULL))
  104. return -EAFNOSUPPORT;
  105. if (likely(afinfo->dst_lookup != NULL))
  106. err = afinfo->dst_lookup(dst, fl);
  107. else
  108. err = -EINVAL;
  109. xfrm_policy_put_afinfo(afinfo);
  110. return err;
  111. }
  112. EXPORT_SYMBOL(xfrm_dst_lookup);
  113. void xfrm_put_type(struct xfrm_type *type)
  114. {
  115. module_put(type->owner);
  116. }
  117. int xfrm_register_mode(struct xfrm_mode *mode, int family)
  118. {
  119. struct xfrm_policy_afinfo *afinfo;
  120. struct xfrm_mode **modemap;
  121. int err;
  122. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  123. return -EINVAL;
  124. afinfo = xfrm_policy_lock_afinfo(family);
  125. if (unlikely(afinfo == NULL))
  126. return -EAFNOSUPPORT;
  127. err = -EEXIST;
  128. modemap = afinfo->mode_map;
  129. if (likely(modemap[mode->encap] == NULL)) {
  130. modemap[mode->encap] = mode;
  131. err = 0;
  132. }
  133. xfrm_policy_unlock_afinfo(afinfo);
  134. return err;
  135. }
  136. EXPORT_SYMBOL(xfrm_register_mode);
  137. int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
  138. {
  139. struct xfrm_policy_afinfo *afinfo;
  140. struct xfrm_mode **modemap;
  141. int err;
  142. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  143. return -EINVAL;
  144. afinfo = xfrm_policy_lock_afinfo(family);
  145. if (unlikely(afinfo == NULL))
  146. return -EAFNOSUPPORT;
  147. err = -ENOENT;
  148. modemap = afinfo->mode_map;
  149. if (likely(modemap[mode->encap] == mode)) {
  150. modemap[mode->encap] = NULL;
  151. err = 0;
  152. }
  153. xfrm_policy_unlock_afinfo(afinfo);
  154. return err;
  155. }
  156. EXPORT_SYMBOL(xfrm_unregister_mode);
  157. struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
  158. {
  159. struct xfrm_policy_afinfo *afinfo;
  160. struct xfrm_mode *mode;
  161. int modload_attempted = 0;
  162. if (unlikely(encap >= XFRM_MODE_MAX))
  163. return NULL;
  164. retry:
  165. afinfo = xfrm_policy_get_afinfo(family);
  166. if (unlikely(afinfo == NULL))
  167. return NULL;
  168. mode = afinfo->mode_map[encap];
  169. if (unlikely(mode && !try_module_get(mode->owner)))
  170. mode = NULL;
  171. if (!mode && !modload_attempted) {
  172. xfrm_policy_put_afinfo(afinfo);
  173. request_module("xfrm-mode-%d-%d", family, encap);
  174. modload_attempted = 1;
  175. goto retry;
  176. }
  177. xfrm_policy_put_afinfo(afinfo);
  178. return mode;
  179. }
  180. void xfrm_put_mode(struct xfrm_mode *mode)
  181. {
  182. module_put(mode->owner);
  183. }
  184. static inline unsigned long make_jiffies(long secs)
  185. {
  186. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  187. return MAX_SCHEDULE_TIMEOUT-1;
  188. else
  189. return secs*HZ;
  190. }
  191. static void xfrm_policy_timer(unsigned long data)
  192. {
  193. struct xfrm_policy *xp = (struct xfrm_policy*)data;
  194. unsigned long now = (unsigned long)xtime.tv_sec;
  195. long next = LONG_MAX;
  196. int warn = 0;
  197. int dir;
  198. read_lock(&xp->lock);
  199. if (xp->dead)
  200. goto out;
  201. dir = xfrm_policy_id2dir(xp->index);
  202. if (xp->lft.hard_add_expires_seconds) {
  203. long tmo = xp->lft.hard_add_expires_seconds +
  204. xp->curlft.add_time - now;
  205. if (tmo <= 0)
  206. goto expired;
  207. if (tmo < next)
  208. next = tmo;
  209. }
  210. if (xp->lft.hard_use_expires_seconds) {
  211. long tmo = xp->lft.hard_use_expires_seconds +
  212. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  213. if (tmo <= 0)
  214. goto expired;
  215. if (tmo < next)
  216. next = tmo;
  217. }
  218. if (xp->lft.soft_add_expires_seconds) {
  219. long tmo = xp->lft.soft_add_expires_seconds +
  220. xp->curlft.add_time - now;
  221. if (tmo <= 0) {
  222. warn = 1;
  223. tmo = XFRM_KM_TIMEOUT;
  224. }
  225. if (tmo < next)
  226. next = tmo;
  227. }
  228. if (xp->lft.soft_use_expires_seconds) {
  229. long tmo = xp->lft.soft_use_expires_seconds +
  230. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  231. if (tmo <= 0) {
  232. warn = 1;
  233. tmo = XFRM_KM_TIMEOUT;
  234. }
  235. if (tmo < next)
  236. next = tmo;
  237. }
  238. if (warn)
  239. km_policy_expired(xp, dir, 0, 0);
  240. if (next != LONG_MAX &&
  241. !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
  242. xfrm_pol_hold(xp);
  243. out:
  244. read_unlock(&xp->lock);
  245. xfrm_pol_put(xp);
  246. return;
  247. expired:
  248. read_unlock(&xp->lock);
  249. if (!xfrm_policy_delete(xp, dir))
  250. km_policy_expired(xp, dir, 1, 0);
  251. xfrm_pol_put(xp);
  252. }
  253. /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
  254. * SPD calls.
  255. */
  256. struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
  257. {
  258. struct xfrm_policy *policy;
  259. policy = kzalloc(sizeof(struct xfrm_policy), gfp);
  260. if (policy) {
  261. atomic_set(&policy->refcnt, 1);
  262. rwlock_init(&policy->lock);
  263. init_timer(&policy->timer);
  264. policy->timer.data = (unsigned long)policy;
  265. policy->timer.function = xfrm_policy_timer;
  266. }
  267. return policy;
  268. }
  269. EXPORT_SYMBOL(xfrm_policy_alloc);
  270. /* Destroy xfrm_policy: descendant resources must be released to this moment. */
  271. void __xfrm_policy_destroy(struct xfrm_policy *policy)
  272. {
  273. BUG_ON(!policy->dead);
  274. BUG_ON(policy->bundles);
  275. if (del_timer(&policy->timer))
  276. BUG();
  277. security_xfrm_policy_free(policy);
  278. kfree(policy);
  279. }
  280. EXPORT_SYMBOL(__xfrm_policy_destroy);
  281. static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
  282. {
  283. struct dst_entry *dst;
  284. while ((dst = policy->bundles) != NULL) {
  285. policy->bundles = dst->next;
  286. dst_free(dst);
  287. }
  288. if (del_timer(&policy->timer))
  289. atomic_dec(&policy->refcnt);
  290. if (atomic_read(&policy->refcnt) > 1)
  291. flow_cache_flush();
  292. xfrm_pol_put(policy);
  293. }
  294. static void xfrm_policy_gc_task(void *data)
  295. {
  296. struct xfrm_policy *policy;
  297. struct list_head *entry, *tmp;
  298. struct list_head gc_list = LIST_HEAD_INIT(gc_list);
  299. spin_lock_bh(&xfrm_policy_gc_lock);
  300. list_splice_init(&xfrm_policy_gc_list, &gc_list);
  301. spin_unlock_bh(&xfrm_policy_gc_lock);
  302. list_for_each_safe(entry, tmp, &gc_list) {
  303. policy = list_entry(entry, struct xfrm_policy, list);
  304. xfrm_policy_gc_kill(policy);
  305. }
  306. }
  307. /* Rule must be locked. Release descentant resources, announce
  308. * entry dead. The rule must be unlinked from lists to the moment.
  309. */
  310. static void xfrm_policy_kill(struct xfrm_policy *policy)
  311. {
  312. int dead;
  313. write_lock_bh(&policy->lock);
  314. dead = policy->dead;
  315. policy->dead = 1;
  316. write_unlock_bh(&policy->lock);
  317. if (unlikely(dead)) {
  318. WARN_ON(1);
  319. return;
  320. }
  321. spin_lock(&xfrm_policy_gc_lock);
  322. list_add(&policy->list, &xfrm_policy_gc_list);
  323. spin_unlock(&xfrm_policy_gc_lock);
  324. schedule_work(&xfrm_policy_gc_work);
  325. }
  326. /* Generate new index... KAME seems to generate them ordered by cost
  327. * of an absolute inpredictability of ordering of rules. This will not pass. */
  328. static u32 xfrm_gen_index(int dir)
  329. {
  330. u32 idx;
  331. struct xfrm_policy *p;
  332. static u32 idx_generator;
  333. for (;;) {
  334. idx = (idx_generator | dir);
  335. idx_generator += 8;
  336. if (idx == 0)
  337. idx = 8;
  338. for (p = xfrm_policy_list[dir]; p; p = p->next) {
  339. if (p->index == idx)
  340. break;
  341. }
  342. if (!p)
  343. return idx;
  344. }
  345. }
  346. int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
  347. {
  348. struct xfrm_policy *pol, **p;
  349. struct xfrm_policy *delpol = NULL;
  350. struct xfrm_policy **newpos = NULL;
  351. struct dst_entry *gc_list;
  352. write_lock_bh(&xfrm_policy_lock);
  353. for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL;) {
  354. if (!delpol && memcmp(&policy->selector, &pol->selector, sizeof(pol->selector)) == 0 &&
  355. xfrm_sec_ctx_match(pol->security, policy->security)) {
  356. if (excl) {
  357. write_unlock_bh(&xfrm_policy_lock);
  358. return -EEXIST;
  359. }
  360. *p = pol->next;
  361. delpol = pol;
  362. if (policy->priority > pol->priority)
  363. continue;
  364. } else if (policy->priority >= pol->priority) {
  365. p = &pol->next;
  366. continue;
  367. }
  368. if (!newpos)
  369. newpos = p;
  370. if (delpol)
  371. break;
  372. p = &pol->next;
  373. }
  374. if (newpos)
  375. p = newpos;
  376. xfrm_pol_hold(policy);
  377. policy->next = *p;
  378. *p = policy;
  379. atomic_inc(&flow_cache_genid);
  380. policy->index = delpol ? delpol->index : xfrm_gen_index(dir);
  381. policy->curlft.add_time = (unsigned long)xtime.tv_sec;
  382. policy->curlft.use_time = 0;
  383. if (!mod_timer(&policy->timer, jiffies + HZ))
  384. xfrm_pol_hold(policy);
  385. write_unlock_bh(&xfrm_policy_lock);
  386. if (delpol)
  387. xfrm_policy_kill(delpol);
  388. read_lock_bh(&xfrm_policy_lock);
  389. gc_list = NULL;
  390. for (policy = policy->next; policy; policy = policy->next) {
  391. struct dst_entry *dst;
  392. write_lock(&policy->lock);
  393. dst = policy->bundles;
  394. if (dst) {
  395. struct dst_entry *tail = dst;
  396. while (tail->next)
  397. tail = tail->next;
  398. tail->next = gc_list;
  399. gc_list = dst;
  400. policy->bundles = NULL;
  401. }
  402. write_unlock(&policy->lock);
  403. }
  404. read_unlock_bh(&xfrm_policy_lock);
  405. while (gc_list) {
  406. struct dst_entry *dst = gc_list;
  407. gc_list = dst->next;
  408. dst_free(dst);
  409. }
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(xfrm_policy_insert);
  413. struct xfrm_policy *xfrm_policy_bysel_ctx(int dir, struct xfrm_selector *sel,
  414. struct xfrm_sec_ctx *ctx, int delete)
  415. {
  416. struct xfrm_policy *pol, **p;
  417. write_lock_bh(&xfrm_policy_lock);
  418. for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
  419. if ((memcmp(sel, &pol->selector, sizeof(*sel)) == 0) &&
  420. (xfrm_sec_ctx_match(ctx, pol->security))) {
  421. xfrm_pol_hold(pol);
  422. if (delete)
  423. *p = pol->next;
  424. break;
  425. }
  426. }
  427. write_unlock_bh(&xfrm_policy_lock);
  428. if (pol && delete) {
  429. atomic_inc(&flow_cache_genid);
  430. xfrm_policy_kill(pol);
  431. }
  432. return pol;
  433. }
  434. EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
  435. struct xfrm_policy *xfrm_policy_byid(int dir, u32 id, int delete)
  436. {
  437. struct xfrm_policy *pol, **p;
  438. write_lock_bh(&xfrm_policy_lock);
  439. for (p = &xfrm_policy_list[dir]; (pol=*p)!=NULL; p = &pol->next) {
  440. if (pol->index == id) {
  441. xfrm_pol_hold(pol);
  442. if (delete)
  443. *p = pol->next;
  444. break;
  445. }
  446. }
  447. write_unlock_bh(&xfrm_policy_lock);
  448. if (pol && delete) {
  449. atomic_inc(&flow_cache_genid);
  450. xfrm_policy_kill(pol);
  451. }
  452. return pol;
  453. }
  454. EXPORT_SYMBOL(xfrm_policy_byid);
  455. void xfrm_policy_flush(void)
  456. {
  457. struct xfrm_policy *xp;
  458. int dir;
  459. write_lock_bh(&xfrm_policy_lock);
  460. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  461. while ((xp = xfrm_policy_list[dir]) != NULL) {
  462. xfrm_policy_list[dir] = xp->next;
  463. write_unlock_bh(&xfrm_policy_lock);
  464. xfrm_policy_kill(xp);
  465. write_lock_bh(&xfrm_policy_lock);
  466. }
  467. }
  468. atomic_inc(&flow_cache_genid);
  469. write_unlock_bh(&xfrm_policy_lock);
  470. }
  471. EXPORT_SYMBOL(xfrm_policy_flush);
  472. int xfrm_policy_walk(int (*func)(struct xfrm_policy *, int, int, void*),
  473. void *data)
  474. {
  475. struct xfrm_policy *xp;
  476. int dir;
  477. int count = 0;
  478. int error = 0;
  479. read_lock_bh(&xfrm_policy_lock);
  480. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  481. for (xp = xfrm_policy_list[dir]; xp; xp = xp->next)
  482. count++;
  483. }
  484. if (count == 0) {
  485. error = -ENOENT;
  486. goto out;
  487. }
  488. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  489. for (xp = xfrm_policy_list[dir]; xp; xp = xp->next) {
  490. error = func(xp, dir%XFRM_POLICY_MAX, --count, data);
  491. if (error)
  492. goto out;
  493. }
  494. }
  495. out:
  496. read_unlock_bh(&xfrm_policy_lock);
  497. return error;
  498. }
  499. EXPORT_SYMBOL(xfrm_policy_walk);
  500. /* Find policy to apply to this flow. */
  501. static void xfrm_policy_lookup(struct flowi *fl, u32 sk_sid, u16 family, u8 dir,
  502. void **objp, atomic_t **obj_refp)
  503. {
  504. struct xfrm_policy *pol;
  505. read_lock_bh(&xfrm_policy_lock);
  506. for (pol = xfrm_policy_list[dir]; pol; pol = pol->next) {
  507. struct xfrm_selector *sel = &pol->selector;
  508. int match;
  509. if (pol->family != family)
  510. continue;
  511. match = xfrm_selector_match(sel, fl, family);
  512. if (match) {
  513. if (!security_xfrm_policy_lookup(pol, sk_sid, dir)) {
  514. xfrm_pol_hold(pol);
  515. break;
  516. }
  517. }
  518. }
  519. read_unlock_bh(&xfrm_policy_lock);
  520. if ((*objp = (void *) pol) != NULL)
  521. *obj_refp = &pol->refcnt;
  522. }
  523. static inline int policy_to_flow_dir(int dir)
  524. {
  525. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  526. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  527. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  528. return dir;
  529. switch (dir) {
  530. default:
  531. case XFRM_POLICY_IN:
  532. return FLOW_DIR_IN;
  533. case XFRM_POLICY_OUT:
  534. return FLOW_DIR_OUT;
  535. case XFRM_POLICY_FWD:
  536. return FLOW_DIR_FWD;
  537. };
  538. }
  539. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl, u32 sk_sid)
  540. {
  541. struct xfrm_policy *pol;
  542. read_lock_bh(&xfrm_policy_lock);
  543. if ((pol = sk->sk_policy[dir]) != NULL) {
  544. int match = xfrm_selector_match(&pol->selector, fl,
  545. sk->sk_family);
  546. int err = 0;
  547. if (match)
  548. err = security_xfrm_policy_lookup(pol, sk_sid, policy_to_flow_dir(dir));
  549. if (match && !err)
  550. xfrm_pol_hold(pol);
  551. else
  552. pol = NULL;
  553. }
  554. read_unlock_bh(&xfrm_policy_lock);
  555. return pol;
  556. }
  557. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  558. {
  559. pol->next = xfrm_policy_list[dir];
  560. xfrm_policy_list[dir] = pol;
  561. xfrm_pol_hold(pol);
  562. }
  563. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  564. int dir)
  565. {
  566. struct xfrm_policy **polp;
  567. for (polp = &xfrm_policy_list[dir];
  568. *polp != NULL; polp = &(*polp)->next) {
  569. if (*polp == pol) {
  570. *polp = pol->next;
  571. return pol;
  572. }
  573. }
  574. return NULL;
  575. }
  576. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  577. {
  578. write_lock_bh(&xfrm_policy_lock);
  579. pol = __xfrm_policy_unlink(pol, dir);
  580. write_unlock_bh(&xfrm_policy_lock);
  581. if (pol) {
  582. if (dir < XFRM_POLICY_MAX)
  583. atomic_inc(&flow_cache_genid);
  584. xfrm_policy_kill(pol);
  585. return 0;
  586. }
  587. return -ENOENT;
  588. }
  589. EXPORT_SYMBOL(xfrm_policy_delete);
  590. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  591. {
  592. struct xfrm_policy *old_pol;
  593. write_lock_bh(&xfrm_policy_lock);
  594. old_pol = sk->sk_policy[dir];
  595. sk->sk_policy[dir] = pol;
  596. if (pol) {
  597. pol->curlft.add_time = (unsigned long)xtime.tv_sec;
  598. pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
  599. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  600. }
  601. if (old_pol)
  602. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  603. write_unlock_bh(&xfrm_policy_lock);
  604. if (old_pol) {
  605. xfrm_policy_kill(old_pol);
  606. }
  607. return 0;
  608. }
  609. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  610. {
  611. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  612. if (newp) {
  613. newp->selector = old->selector;
  614. if (security_xfrm_policy_clone(old, newp)) {
  615. kfree(newp);
  616. return NULL; /* ENOMEM */
  617. }
  618. newp->lft = old->lft;
  619. newp->curlft = old->curlft;
  620. newp->action = old->action;
  621. newp->flags = old->flags;
  622. newp->xfrm_nr = old->xfrm_nr;
  623. newp->index = old->index;
  624. memcpy(newp->xfrm_vec, old->xfrm_vec,
  625. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  626. write_lock_bh(&xfrm_policy_lock);
  627. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  628. write_unlock_bh(&xfrm_policy_lock);
  629. xfrm_pol_put(newp);
  630. }
  631. return newp;
  632. }
  633. int __xfrm_sk_clone_policy(struct sock *sk)
  634. {
  635. struct xfrm_policy *p0 = sk->sk_policy[0],
  636. *p1 = sk->sk_policy[1];
  637. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  638. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  639. return -ENOMEM;
  640. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  641. return -ENOMEM;
  642. return 0;
  643. }
  644. /* Resolve list of templates for the flow, given policy. */
  645. static int
  646. xfrm_tmpl_resolve(struct xfrm_policy *policy, struct flowi *fl,
  647. struct xfrm_state **xfrm,
  648. unsigned short family)
  649. {
  650. int nx;
  651. int i, error;
  652. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  653. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  654. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  655. struct xfrm_state *x;
  656. xfrm_address_t *remote = daddr;
  657. xfrm_address_t *local = saddr;
  658. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  659. if (tmpl->mode) {
  660. remote = &tmpl->id.daddr;
  661. local = &tmpl->saddr;
  662. }
  663. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  664. if (x && x->km.state == XFRM_STATE_VALID) {
  665. xfrm[nx++] = x;
  666. daddr = remote;
  667. saddr = local;
  668. continue;
  669. }
  670. if (x) {
  671. error = (x->km.state == XFRM_STATE_ERROR ?
  672. -EINVAL : -EAGAIN);
  673. xfrm_state_put(x);
  674. }
  675. if (!tmpl->optional)
  676. goto fail;
  677. }
  678. return nx;
  679. fail:
  680. for (nx--; nx>=0; nx--)
  681. xfrm_state_put(xfrm[nx]);
  682. return error;
  683. }
  684. /* Check that the bundle accepts the flow and its components are
  685. * still valid.
  686. */
  687. static struct dst_entry *
  688. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  689. {
  690. struct dst_entry *x;
  691. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  692. if (unlikely(afinfo == NULL))
  693. return ERR_PTR(-EINVAL);
  694. x = afinfo->find_bundle(fl, policy);
  695. xfrm_policy_put_afinfo(afinfo);
  696. return x;
  697. }
  698. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  699. * all the metrics... Shortly, bundle a bundle.
  700. */
  701. static int
  702. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  703. struct flowi *fl, struct dst_entry **dst_p,
  704. unsigned short family)
  705. {
  706. int err;
  707. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  708. if (unlikely(afinfo == NULL))
  709. return -EINVAL;
  710. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  711. xfrm_policy_put_afinfo(afinfo);
  712. return err;
  713. }
  714. static int stale_bundle(struct dst_entry *dst);
  715. /* Main function: finds/creates a bundle for given flow.
  716. *
  717. * At the moment we eat a raw IP route. Mostly to speed up lookups
  718. * on interfaces with disabled IPsec.
  719. */
  720. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  721. struct sock *sk, int flags)
  722. {
  723. struct xfrm_policy *policy;
  724. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  725. struct dst_entry *dst, *dst_orig = *dst_p;
  726. int nx = 0;
  727. int err;
  728. u32 genid;
  729. u16 family;
  730. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  731. u32 sk_sid = security_sk_sid(sk, fl, dir);
  732. restart:
  733. genid = atomic_read(&flow_cache_genid);
  734. policy = NULL;
  735. if (sk && sk->sk_policy[1])
  736. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, sk_sid);
  737. if (!policy) {
  738. /* To accelerate a bit... */
  739. if ((dst_orig->flags & DST_NOXFRM) || !xfrm_policy_list[XFRM_POLICY_OUT])
  740. return 0;
  741. policy = flow_cache_lookup(fl, sk_sid, dst_orig->ops->family,
  742. dir, xfrm_policy_lookup);
  743. }
  744. if (!policy)
  745. return 0;
  746. family = dst_orig->ops->family;
  747. policy->curlft.use_time = (unsigned long)xtime.tv_sec;
  748. switch (policy->action) {
  749. case XFRM_POLICY_BLOCK:
  750. /* Prohibit the flow */
  751. err = -EPERM;
  752. goto error;
  753. case XFRM_POLICY_ALLOW:
  754. if (policy->xfrm_nr == 0) {
  755. /* Flow passes not transformed. */
  756. xfrm_pol_put(policy);
  757. return 0;
  758. }
  759. /* Try to find matching bundle.
  760. *
  761. * LATER: help from flow cache. It is optional, this
  762. * is required only for output policy.
  763. */
  764. dst = xfrm_find_bundle(fl, policy, family);
  765. if (IS_ERR(dst)) {
  766. err = PTR_ERR(dst);
  767. goto error;
  768. }
  769. if (dst)
  770. break;
  771. nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
  772. if (unlikely(nx<0)) {
  773. err = nx;
  774. if (err == -EAGAIN && flags) {
  775. DECLARE_WAITQUEUE(wait, current);
  776. add_wait_queue(&km_waitq, &wait);
  777. set_current_state(TASK_INTERRUPTIBLE);
  778. schedule();
  779. set_current_state(TASK_RUNNING);
  780. remove_wait_queue(&km_waitq, &wait);
  781. nx = xfrm_tmpl_resolve(policy, fl, xfrm, family);
  782. if (nx == -EAGAIN && signal_pending(current)) {
  783. err = -ERESTART;
  784. goto error;
  785. }
  786. if (nx == -EAGAIN ||
  787. genid != atomic_read(&flow_cache_genid)) {
  788. xfrm_pol_put(policy);
  789. goto restart;
  790. }
  791. err = nx;
  792. }
  793. if (err < 0)
  794. goto error;
  795. }
  796. if (nx == 0) {
  797. /* Flow passes not transformed. */
  798. xfrm_pol_put(policy);
  799. return 0;
  800. }
  801. dst = dst_orig;
  802. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  803. if (unlikely(err)) {
  804. int i;
  805. for (i=0; i<nx; i++)
  806. xfrm_state_put(xfrm[i]);
  807. goto error;
  808. }
  809. write_lock_bh(&policy->lock);
  810. if (unlikely(policy->dead || stale_bundle(dst))) {
  811. /* Wow! While we worked on resolving, this
  812. * policy has gone. Retry. It is not paranoia,
  813. * we just cannot enlist new bundle to dead object.
  814. * We can't enlist stable bundles either.
  815. */
  816. write_unlock_bh(&policy->lock);
  817. if (dst)
  818. dst_free(dst);
  819. err = -EHOSTUNREACH;
  820. goto error;
  821. }
  822. dst->next = policy->bundles;
  823. policy->bundles = dst;
  824. dst_hold(dst);
  825. write_unlock_bh(&policy->lock);
  826. }
  827. *dst_p = dst;
  828. dst_release(dst_orig);
  829. xfrm_pol_put(policy);
  830. return 0;
  831. error:
  832. dst_release(dst_orig);
  833. xfrm_pol_put(policy);
  834. *dst_p = NULL;
  835. return err;
  836. }
  837. EXPORT_SYMBOL(xfrm_lookup);
  838. /* When skb is transformed back to its "native" form, we have to
  839. * check policy restrictions. At the moment we make this in maximally
  840. * stupid way. Shame on me. :-) Of course, connected sockets must
  841. * have policy cached at them.
  842. */
  843. static inline int
  844. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  845. unsigned short family)
  846. {
  847. if (xfrm_state_kern(x))
  848. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
  849. return x->id.proto == tmpl->id.proto &&
  850. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  851. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  852. x->props.mode == tmpl->mode &&
  853. (tmpl->aalgos & (1<<x->props.aalgo)) &&
  854. !(x->props.mode && xfrm_state_addr_cmp(tmpl, x, family));
  855. }
  856. static inline int
  857. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  858. unsigned short family)
  859. {
  860. int idx = start;
  861. if (tmpl->optional) {
  862. if (!tmpl->mode)
  863. return start;
  864. } else
  865. start = -1;
  866. for (; idx < sp->len; idx++) {
  867. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  868. return ++idx;
  869. if (sp->xvec[idx]->props.mode)
  870. break;
  871. }
  872. return start;
  873. }
  874. int
  875. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  876. {
  877. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  878. if (unlikely(afinfo == NULL))
  879. return -EAFNOSUPPORT;
  880. afinfo->decode_session(skb, fl);
  881. xfrm_policy_put_afinfo(afinfo);
  882. return 0;
  883. }
  884. EXPORT_SYMBOL(xfrm_decode_session);
  885. static inline int secpath_has_tunnel(struct sec_path *sp, int k)
  886. {
  887. for (; k < sp->len; k++) {
  888. if (sp->xvec[k]->props.mode)
  889. return 1;
  890. }
  891. return 0;
  892. }
  893. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  894. unsigned short family)
  895. {
  896. struct xfrm_policy *pol;
  897. struct flowi fl;
  898. u8 fl_dir = policy_to_flow_dir(dir);
  899. u32 sk_sid;
  900. if (xfrm_decode_session(skb, &fl, family) < 0)
  901. return 0;
  902. nf_nat_decode_session(skb, &fl, family);
  903. sk_sid = security_sk_sid(sk, &fl, fl_dir);
  904. /* First, check used SA against their selectors. */
  905. if (skb->sp) {
  906. int i;
  907. for (i=skb->sp->len-1; i>=0; i--) {
  908. struct xfrm_state *x = skb->sp->xvec[i];
  909. if (!xfrm_selector_match(&x->sel, &fl, family))
  910. return 0;
  911. }
  912. }
  913. pol = NULL;
  914. if (sk && sk->sk_policy[dir])
  915. pol = xfrm_sk_policy_lookup(sk, dir, &fl, sk_sid);
  916. if (!pol)
  917. pol = flow_cache_lookup(&fl, sk_sid, family, fl_dir,
  918. xfrm_policy_lookup);
  919. if (!pol)
  920. return !skb->sp || !secpath_has_tunnel(skb->sp, 0);
  921. pol->curlft.use_time = (unsigned long)xtime.tv_sec;
  922. if (pol->action == XFRM_POLICY_ALLOW) {
  923. struct sec_path *sp;
  924. static struct sec_path dummy;
  925. int i, k;
  926. if ((sp = skb->sp) == NULL)
  927. sp = &dummy;
  928. /* For each tunnel xfrm, find the first matching tmpl.
  929. * For each tmpl before that, find corresponding xfrm.
  930. * Order is _important_. Later we will implement
  931. * some barriers, but at the moment barriers
  932. * are implied between each two transformations.
  933. */
  934. for (i = pol->xfrm_nr-1, k = 0; i >= 0; i--) {
  935. k = xfrm_policy_ok(pol->xfrm_vec+i, sp, k, family);
  936. if (k < 0)
  937. goto reject;
  938. }
  939. if (secpath_has_tunnel(sp, k))
  940. goto reject;
  941. xfrm_pol_put(pol);
  942. return 1;
  943. }
  944. reject:
  945. xfrm_pol_put(pol);
  946. return 0;
  947. }
  948. EXPORT_SYMBOL(__xfrm_policy_check);
  949. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  950. {
  951. struct flowi fl;
  952. if (xfrm_decode_session(skb, &fl, family) < 0)
  953. return 0;
  954. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  955. }
  956. EXPORT_SYMBOL(__xfrm_route_forward);
  957. /* Optimize later using cookies and generation ids. */
  958. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  959. {
  960. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  961. * to "-1" to force all XFRM destinations to get validated by
  962. * dst_ops->check on every use. We do this because when a
  963. * normal route referenced by an XFRM dst is obsoleted we do
  964. * not go looking around for all parent referencing XFRM dsts
  965. * so that we can invalidate them. It is just too much work.
  966. * Instead we make the checks here on every use. For example:
  967. *
  968. * XFRM dst A --> IPv4 dst X
  969. *
  970. * X is the "xdst->route" of A (X is also the "dst->path" of A
  971. * in this example). If X is marked obsolete, "A" will not
  972. * notice. That's what we are validating here via the
  973. * stale_bundle() check.
  974. *
  975. * When a policy's bundle is pruned, we dst_free() the XFRM
  976. * dst which causes it's ->obsolete field to be set to a
  977. * positive non-zero integer. If an XFRM dst has been pruned
  978. * like this, we want to force a new route lookup.
  979. */
  980. if (dst->obsolete < 0 && !stale_bundle(dst))
  981. return dst;
  982. return NULL;
  983. }
  984. static int stale_bundle(struct dst_entry *dst)
  985. {
  986. return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC);
  987. }
  988. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  989. {
  990. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  991. dst->dev = &loopback_dev;
  992. dev_hold(&loopback_dev);
  993. dev_put(dev);
  994. }
  995. }
  996. EXPORT_SYMBOL(xfrm_dst_ifdown);
  997. static void xfrm_link_failure(struct sk_buff *skb)
  998. {
  999. /* Impossible. Such dst must be popped before reaches point of failure. */
  1000. return;
  1001. }
  1002. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1003. {
  1004. if (dst) {
  1005. if (dst->obsolete) {
  1006. dst_release(dst);
  1007. dst = NULL;
  1008. }
  1009. }
  1010. return dst;
  1011. }
  1012. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1013. {
  1014. int i;
  1015. struct xfrm_policy *pol;
  1016. struct dst_entry *dst, **dstp, *gc_list = NULL;
  1017. read_lock_bh(&xfrm_policy_lock);
  1018. for (i=0; i<2*XFRM_POLICY_MAX; i++) {
  1019. for (pol = xfrm_policy_list[i]; pol; pol = pol->next) {
  1020. write_lock(&pol->lock);
  1021. dstp = &pol->bundles;
  1022. while ((dst=*dstp) != NULL) {
  1023. if (func(dst)) {
  1024. *dstp = dst->next;
  1025. dst->next = gc_list;
  1026. gc_list = dst;
  1027. } else {
  1028. dstp = &dst->next;
  1029. }
  1030. }
  1031. write_unlock(&pol->lock);
  1032. }
  1033. }
  1034. read_unlock_bh(&xfrm_policy_lock);
  1035. while (gc_list) {
  1036. dst = gc_list;
  1037. gc_list = dst->next;
  1038. dst_free(dst);
  1039. }
  1040. }
  1041. static int unused_bundle(struct dst_entry *dst)
  1042. {
  1043. return !atomic_read(&dst->__refcnt);
  1044. }
  1045. static void __xfrm_garbage_collect(void)
  1046. {
  1047. xfrm_prune_bundles(unused_bundle);
  1048. }
  1049. int xfrm_flush_bundles(void)
  1050. {
  1051. xfrm_prune_bundles(stale_bundle);
  1052. return 0;
  1053. }
  1054. static int always_true(struct dst_entry *dst)
  1055. {
  1056. return 1;
  1057. }
  1058. void xfrm_flush_all_bundles(void)
  1059. {
  1060. xfrm_prune_bundles(always_true);
  1061. }
  1062. void xfrm_init_pmtu(struct dst_entry *dst)
  1063. {
  1064. do {
  1065. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1066. u32 pmtu, route_mtu_cached;
  1067. pmtu = dst_mtu(dst->child);
  1068. xdst->child_mtu_cached = pmtu;
  1069. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1070. route_mtu_cached = dst_mtu(xdst->route);
  1071. xdst->route_mtu_cached = route_mtu_cached;
  1072. if (pmtu > route_mtu_cached)
  1073. pmtu = route_mtu_cached;
  1074. dst->metrics[RTAX_MTU-1] = pmtu;
  1075. } while ((dst = dst->next));
  1076. }
  1077. EXPORT_SYMBOL(xfrm_init_pmtu);
  1078. /* Check that the bundle accepts the flow and its components are
  1079. * still valid.
  1080. */
  1081. int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family)
  1082. {
  1083. struct dst_entry *dst = &first->u.dst;
  1084. struct xfrm_dst *last;
  1085. u32 mtu;
  1086. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1087. (dst->dev && !netif_running(dst->dev)))
  1088. return 0;
  1089. last = NULL;
  1090. do {
  1091. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1092. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1093. return 0;
  1094. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1095. return 0;
  1096. mtu = dst_mtu(dst->child);
  1097. if (xdst->child_mtu_cached != mtu) {
  1098. last = xdst;
  1099. xdst->child_mtu_cached = mtu;
  1100. }
  1101. if (!dst_check(xdst->route, xdst->route_cookie))
  1102. return 0;
  1103. mtu = dst_mtu(xdst->route);
  1104. if (xdst->route_mtu_cached != mtu) {
  1105. last = xdst;
  1106. xdst->route_mtu_cached = mtu;
  1107. }
  1108. dst = dst->child;
  1109. } while (dst->xfrm);
  1110. if (likely(!last))
  1111. return 1;
  1112. mtu = last->child_mtu_cached;
  1113. for (;;) {
  1114. dst = &last->u.dst;
  1115. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1116. if (mtu > last->route_mtu_cached)
  1117. mtu = last->route_mtu_cached;
  1118. dst->metrics[RTAX_MTU-1] = mtu;
  1119. if (last == first)
  1120. break;
  1121. last = last->u.next;
  1122. last->child_mtu_cached = mtu;
  1123. }
  1124. return 1;
  1125. }
  1126. EXPORT_SYMBOL(xfrm_bundle_ok);
  1127. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1128. {
  1129. int err = 0;
  1130. if (unlikely(afinfo == NULL))
  1131. return -EINVAL;
  1132. if (unlikely(afinfo->family >= NPROTO))
  1133. return -EAFNOSUPPORT;
  1134. write_lock_bh(&xfrm_policy_afinfo_lock);
  1135. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1136. err = -ENOBUFS;
  1137. else {
  1138. struct dst_ops *dst_ops = afinfo->dst_ops;
  1139. if (likely(dst_ops->kmem_cachep == NULL))
  1140. dst_ops->kmem_cachep = xfrm_dst_cache;
  1141. if (likely(dst_ops->check == NULL))
  1142. dst_ops->check = xfrm_dst_check;
  1143. if (likely(dst_ops->negative_advice == NULL))
  1144. dst_ops->negative_advice = xfrm_negative_advice;
  1145. if (likely(dst_ops->link_failure == NULL))
  1146. dst_ops->link_failure = xfrm_link_failure;
  1147. if (likely(afinfo->garbage_collect == NULL))
  1148. afinfo->garbage_collect = __xfrm_garbage_collect;
  1149. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1150. }
  1151. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1152. return err;
  1153. }
  1154. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1155. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1156. {
  1157. int err = 0;
  1158. if (unlikely(afinfo == NULL))
  1159. return -EINVAL;
  1160. if (unlikely(afinfo->family >= NPROTO))
  1161. return -EAFNOSUPPORT;
  1162. write_lock_bh(&xfrm_policy_afinfo_lock);
  1163. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1164. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1165. err = -EINVAL;
  1166. else {
  1167. struct dst_ops *dst_ops = afinfo->dst_ops;
  1168. xfrm_policy_afinfo[afinfo->family] = NULL;
  1169. dst_ops->kmem_cachep = NULL;
  1170. dst_ops->check = NULL;
  1171. dst_ops->negative_advice = NULL;
  1172. dst_ops->link_failure = NULL;
  1173. afinfo->garbage_collect = NULL;
  1174. }
  1175. }
  1176. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1177. return err;
  1178. }
  1179. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1180. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1181. {
  1182. struct xfrm_policy_afinfo *afinfo;
  1183. if (unlikely(family >= NPROTO))
  1184. return NULL;
  1185. read_lock(&xfrm_policy_afinfo_lock);
  1186. afinfo = xfrm_policy_afinfo[family];
  1187. if (unlikely(!afinfo))
  1188. read_unlock(&xfrm_policy_afinfo_lock);
  1189. return afinfo;
  1190. }
  1191. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1192. {
  1193. read_unlock(&xfrm_policy_afinfo_lock);
  1194. }
  1195. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
  1196. {
  1197. struct xfrm_policy_afinfo *afinfo;
  1198. if (unlikely(family >= NPROTO))
  1199. return NULL;
  1200. write_lock_bh(&xfrm_policy_afinfo_lock);
  1201. afinfo = xfrm_policy_afinfo[family];
  1202. if (unlikely(!afinfo))
  1203. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1204. return afinfo;
  1205. }
  1206. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
  1207. {
  1208. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1209. }
  1210. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1211. {
  1212. switch (event) {
  1213. case NETDEV_DOWN:
  1214. xfrm_flush_bundles();
  1215. }
  1216. return NOTIFY_DONE;
  1217. }
  1218. static struct notifier_block xfrm_dev_notifier = {
  1219. xfrm_dev_event,
  1220. NULL,
  1221. 0
  1222. };
  1223. static void __init xfrm_policy_init(void)
  1224. {
  1225. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1226. sizeof(struct xfrm_dst),
  1227. 0, SLAB_HWCACHE_ALIGN,
  1228. NULL, NULL);
  1229. if (!xfrm_dst_cache)
  1230. panic("XFRM: failed to allocate xfrm_dst_cache\n");
  1231. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
  1232. register_netdevice_notifier(&xfrm_dev_notifier);
  1233. }
  1234. void __init xfrm_init(void)
  1235. {
  1236. xfrm_state_init();
  1237. xfrm_policy_init();
  1238. xfrm_input_init();
  1239. }