xfrm_policy.c 31 KB

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