xfrm_policy.c 33 KB

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