xfrm_policy.c 30 KB

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