xfrm_policy.c 32 KB

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