xfrm_policy.c 31 KB

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