xfrm_policy.c 29 KB

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