xfrm_policy.c 31 KB

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