xfrm_policy.c 30 KB

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