xfrm_policy.c 32 KB

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