xfrm_policy.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  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 <linux/cache.h>
  25. #include <net/xfrm.h>
  26. #include <net/ip.h>
  27. #include "xfrm_hash.h"
  28. DEFINE_MUTEX(xfrm_cfg_mutex);
  29. EXPORT_SYMBOL(xfrm_cfg_mutex);
  30. static DEFINE_RWLOCK(xfrm_policy_lock);
  31. unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
  32. EXPORT_SYMBOL(xfrm_policy_count);
  33. static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
  34. static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
  35. static kmem_cache_t *xfrm_dst_cache __read_mostly;
  36. static struct work_struct xfrm_policy_gc_work;
  37. static HLIST_HEAD(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 = kzalloc(sizeof(struct xfrm_policy), gfp);
  261. if (policy) {
  262. INIT_HLIST_NODE(&policy->bydst);
  263. INIT_HLIST_NODE(&policy->byidx);
  264. rwlock_init(&policy->lock);
  265. atomic_set(&policy->refcnt, 1);
  266. init_timer(&policy->timer);
  267. policy->timer.data = (unsigned long)policy;
  268. policy->timer.function = xfrm_policy_timer;
  269. }
  270. return policy;
  271. }
  272. EXPORT_SYMBOL(xfrm_policy_alloc);
  273. /* Destroy xfrm_policy: descendant resources must be released to this moment. */
  274. void __xfrm_policy_destroy(struct xfrm_policy *policy)
  275. {
  276. BUG_ON(!policy->dead);
  277. BUG_ON(policy->bundles);
  278. if (del_timer(&policy->timer))
  279. BUG();
  280. security_xfrm_policy_free(policy);
  281. kfree(policy);
  282. }
  283. EXPORT_SYMBOL(__xfrm_policy_destroy);
  284. static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
  285. {
  286. struct dst_entry *dst;
  287. while ((dst = policy->bundles) != NULL) {
  288. policy->bundles = dst->next;
  289. dst_free(dst);
  290. }
  291. if (del_timer(&policy->timer))
  292. atomic_dec(&policy->refcnt);
  293. if (atomic_read(&policy->refcnt) > 1)
  294. flow_cache_flush();
  295. xfrm_pol_put(policy);
  296. }
  297. static void xfrm_policy_gc_task(void *data)
  298. {
  299. struct xfrm_policy *policy;
  300. struct hlist_node *entry, *tmp;
  301. struct hlist_head gc_list;
  302. spin_lock_bh(&xfrm_policy_gc_lock);
  303. gc_list.first = xfrm_policy_gc_list.first;
  304. INIT_HLIST_HEAD(&xfrm_policy_gc_list);
  305. spin_unlock_bh(&xfrm_policy_gc_lock);
  306. hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
  307. xfrm_policy_gc_kill(policy);
  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. hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
  325. spin_unlock(&xfrm_policy_gc_lock);
  326. schedule_work(&xfrm_policy_gc_work);
  327. }
  328. struct xfrm_policy_hash {
  329. struct hlist_head *table;
  330. unsigned int hmask;
  331. };
  332. static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
  333. static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
  334. static struct hlist_head *xfrm_policy_byidx __read_mostly;
  335. static unsigned int xfrm_idx_hmask __read_mostly;
  336. static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
  337. static inline unsigned int idx_hash(u32 index)
  338. {
  339. return __idx_hash(index, xfrm_idx_hmask);
  340. }
  341. static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
  342. {
  343. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  344. unsigned int hash = __sel_hash(sel, family, hmask);
  345. return (hash == hmask + 1 ?
  346. &xfrm_policy_inexact[dir] :
  347. xfrm_policy_bydst[dir].table + hash);
  348. }
  349. static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
  350. {
  351. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  352. unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
  353. return xfrm_policy_bydst[dir].table + hash;
  354. }
  355. static void xfrm_dst_hash_transfer(struct hlist_head *list,
  356. struct hlist_head *ndsttable,
  357. unsigned int nhashmask)
  358. {
  359. struct hlist_node *entry, *tmp;
  360. struct xfrm_policy *pol;
  361. hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
  362. unsigned int h;
  363. h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
  364. pol->family, nhashmask);
  365. hlist_add_head(&pol->bydst, ndsttable+h);
  366. }
  367. }
  368. static void xfrm_idx_hash_transfer(struct hlist_head *list,
  369. struct hlist_head *nidxtable,
  370. unsigned int nhashmask)
  371. {
  372. struct hlist_node *entry, *tmp;
  373. struct xfrm_policy *pol;
  374. hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
  375. unsigned int h;
  376. h = __idx_hash(pol->index, nhashmask);
  377. hlist_add_head(&pol->byidx, nidxtable+h);
  378. }
  379. }
  380. static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
  381. {
  382. return ((old_hmask + 1) << 1) - 1;
  383. }
  384. static void xfrm_bydst_resize(int dir)
  385. {
  386. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  387. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  388. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  389. struct hlist_head *odst = xfrm_policy_bydst[dir].table;
  390. struct hlist_head *ndst = xfrm_hash_alloc(nsize);
  391. int i;
  392. if (!ndst)
  393. return;
  394. write_lock_bh(&xfrm_policy_lock);
  395. for (i = hmask; i >= 0; i--)
  396. xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
  397. xfrm_policy_bydst[dir].table = ndst;
  398. xfrm_policy_bydst[dir].hmask = nhashmask;
  399. write_unlock_bh(&xfrm_policy_lock);
  400. xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
  401. }
  402. static void xfrm_byidx_resize(int total)
  403. {
  404. unsigned int hmask = xfrm_idx_hmask;
  405. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  406. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  407. struct hlist_head *oidx = xfrm_policy_byidx;
  408. struct hlist_head *nidx = xfrm_hash_alloc(nsize);
  409. int i;
  410. if (!nidx)
  411. return;
  412. write_lock_bh(&xfrm_policy_lock);
  413. for (i = hmask; i >= 0; i--)
  414. xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
  415. xfrm_policy_byidx = nidx;
  416. xfrm_idx_hmask = nhashmask;
  417. write_unlock_bh(&xfrm_policy_lock);
  418. xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
  419. }
  420. static inline int xfrm_bydst_should_resize(int dir, int *total)
  421. {
  422. unsigned int cnt = xfrm_policy_count[dir];
  423. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  424. if (total)
  425. *total += cnt;
  426. if ((hmask + 1) < xfrm_policy_hashmax &&
  427. cnt > hmask)
  428. return 1;
  429. return 0;
  430. }
  431. static inline int xfrm_byidx_should_resize(int total)
  432. {
  433. unsigned int hmask = xfrm_idx_hmask;
  434. if ((hmask + 1) < xfrm_policy_hashmax &&
  435. total > hmask)
  436. return 1;
  437. return 0;
  438. }
  439. static DEFINE_MUTEX(hash_resize_mutex);
  440. static void xfrm_hash_resize(void *__unused)
  441. {
  442. int dir, total;
  443. mutex_lock(&hash_resize_mutex);
  444. total = 0;
  445. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  446. if (xfrm_bydst_should_resize(dir, &total))
  447. xfrm_bydst_resize(dir);
  448. }
  449. if (xfrm_byidx_should_resize(total))
  450. xfrm_byidx_resize(total);
  451. mutex_unlock(&hash_resize_mutex);
  452. }
  453. static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL);
  454. /* Generate new index... KAME seems to generate them ordered by cost
  455. * of an absolute inpredictability of ordering of rules. This will not pass. */
  456. static u32 xfrm_gen_index(u8 type, int dir)
  457. {
  458. static u32 idx_generator;
  459. for (;;) {
  460. struct hlist_node *entry;
  461. struct hlist_head *list;
  462. struct xfrm_policy *p;
  463. u32 idx;
  464. int found;
  465. idx = (idx_generator | dir);
  466. idx_generator += 8;
  467. if (idx == 0)
  468. idx = 8;
  469. list = xfrm_policy_byidx + idx_hash(idx);
  470. found = 0;
  471. hlist_for_each_entry(p, entry, list, byidx) {
  472. if (p->index == idx) {
  473. found = 1;
  474. break;
  475. }
  476. }
  477. if (!found)
  478. return idx;
  479. }
  480. }
  481. static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
  482. {
  483. u32 *p1 = (u32 *) s1;
  484. u32 *p2 = (u32 *) s2;
  485. int len = sizeof(struct xfrm_selector) / sizeof(u32);
  486. int i;
  487. for (i = 0; i < len; i++) {
  488. if (p1[i] != p2[i])
  489. return 1;
  490. }
  491. return 0;
  492. }
  493. int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
  494. {
  495. struct xfrm_policy *pol;
  496. struct xfrm_policy *delpol;
  497. struct hlist_head *chain;
  498. struct hlist_node *entry, *newpos, *last;
  499. struct dst_entry *gc_list;
  500. write_lock_bh(&xfrm_policy_lock);
  501. chain = policy_hash_bysel(&policy->selector, policy->family, dir);
  502. delpol = NULL;
  503. newpos = NULL;
  504. last = NULL;
  505. hlist_for_each_entry(pol, entry, chain, bydst) {
  506. if (!delpol &&
  507. pol->type == policy->type &&
  508. !selector_cmp(&pol->selector, &policy->selector) &&
  509. xfrm_sec_ctx_match(pol->security, policy->security)) {
  510. if (excl) {
  511. write_unlock_bh(&xfrm_policy_lock);
  512. return -EEXIST;
  513. }
  514. delpol = pol;
  515. if (policy->priority > pol->priority)
  516. continue;
  517. } else if (policy->priority >= pol->priority) {
  518. last = &pol->bydst;
  519. continue;
  520. }
  521. if (!newpos)
  522. newpos = &pol->bydst;
  523. if (delpol)
  524. break;
  525. last = &pol->bydst;
  526. }
  527. if (!newpos)
  528. newpos = last;
  529. if (newpos)
  530. hlist_add_after(newpos, &policy->bydst);
  531. else
  532. hlist_add_head(&policy->bydst, chain);
  533. xfrm_pol_hold(policy);
  534. xfrm_policy_count[dir]++;
  535. atomic_inc(&flow_cache_genid);
  536. if (delpol) {
  537. hlist_del(&delpol->bydst);
  538. hlist_del(&delpol->byidx);
  539. xfrm_policy_count[dir]--;
  540. }
  541. policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
  542. hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
  543. policy->curlft.add_time = (unsigned long)xtime.tv_sec;
  544. policy->curlft.use_time = 0;
  545. if (!mod_timer(&policy->timer, jiffies + HZ))
  546. xfrm_pol_hold(policy);
  547. write_unlock_bh(&xfrm_policy_lock);
  548. if (delpol)
  549. xfrm_policy_kill(delpol);
  550. else if (xfrm_bydst_should_resize(dir, NULL))
  551. schedule_work(&xfrm_hash_work);
  552. read_lock_bh(&xfrm_policy_lock);
  553. gc_list = NULL;
  554. entry = &policy->bydst;
  555. hlist_for_each_entry_continue(policy, entry, bydst) {
  556. struct dst_entry *dst;
  557. write_lock(&policy->lock);
  558. dst = policy->bundles;
  559. if (dst) {
  560. struct dst_entry *tail = dst;
  561. while (tail->next)
  562. tail = tail->next;
  563. tail->next = gc_list;
  564. gc_list = dst;
  565. policy->bundles = NULL;
  566. }
  567. write_unlock(&policy->lock);
  568. }
  569. read_unlock_bh(&xfrm_policy_lock);
  570. while (gc_list) {
  571. struct dst_entry *dst = gc_list;
  572. gc_list = dst->next;
  573. dst_free(dst);
  574. }
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(xfrm_policy_insert);
  578. struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
  579. struct xfrm_selector *sel,
  580. struct xfrm_sec_ctx *ctx, int delete)
  581. {
  582. struct xfrm_policy *pol, *ret;
  583. struct hlist_head *chain;
  584. struct hlist_node *entry;
  585. write_lock_bh(&xfrm_policy_lock);
  586. chain = policy_hash_bysel(sel, sel->family, dir);
  587. ret = NULL;
  588. hlist_for_each_entry(pol, entry, chain, bydst) {
  589. if (pol->type == type &&
  590. !selector_cmp(sel, &pol->selector) &&
  591. xfrm_sec_ctx_match(ctx, pol->security)) {
  592. xfrm_pol_hold(pol);
  593. if (delete) {
  594. hlist_del(&pol->bydst);
  595. hlist_del(&pol->byidx);
  596. xfrm_policy_count[dir]--;
  597. }
  598. ret = pol;
  599. break;
  600. }
  601. }
  602. write_unlock_bh(&xfrm_policy_lock);
  603. if (ret && delete) {
  604. atomic_inc(&flow_cache_genid);
  605. xfrm_policy_kill(ret);
  606. }
  607. return ret;
  608. }
  609. EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
  610. struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete)
  611. {
  612. struct xfrm_policy *pol, *ret;
  613. struct hlist_head *chain;
  614. struct hlist_node *entry;
  615. write_lock_bh(&xfrm_policy_lock);
  616. chain = xfrm_policy_byidx + idx_hash(id);
  617. ret = NULL;
  618. hlist_for_each_entry(pol, entry, chain, byidx) {
  619. if (pol->type == type && pol->index == id) {
  620. xfrm_pol_hold(pol);
  621. if (delete) {
  622. hlist_del(&pol->bydst);
  623. hlist_del(&pol->byidx);
  624. xfrm_policy_count[dir]--;
  625. }
  626. ret = pol;
  627. break;
  628. }
  629. }
  630. write_unlock_bh(&xfrm_policy_lock);
  631. if (ret && delete) {
  632. atomic_inc(&flow_cache_genid);
  633. xfrm_policy_kill(ret);
  634. }
  635. return ret;
  636. }
  637. EXPORT_SYMBOL(xfrm_policy_byid);
  638. void xfrm_policy_flush(u8 type)
  639. {
  640. int dir;
  641. write_lock_bh(&xfrm_policy_lock);
  642. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  643. struct xfrm_policy *pol;
  644. struct hlist_node *entry;
  645. int i;
  646. again1:
  647. hlist_for_each_entry(pol, entry,
  648. &xfrm_policy_inexact[dir], bydst) {
  649. if (pol->type != type)
  650. continue;
  651. hlist_del(&pol->bydst);
  652. hlist_del(&pol->byidx);
  653. write_unlock_bh(&xfrm_policy_lock);
  654. xfrm_policy_kill(pol);
  655. write_lock_bh(&xfrm_policy_lock);
  656. goto again1;
  657. }
  658. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  659. again2:
  660. hlist_for_each_entry(pol, entry,
  661. xfrm_policy_bydst[dir].table + i,
  662. bydst) {
  663. if (pol->type != type)
  664. continue;
  665. hlist_del(&pol->bydst);
  666. hlist_del(&pol->byidx);
  667. write_unlock_bh(&xfrm_policy_lock);
  668. xfrm_policy_kill(pol);
  669. write_lock_bh(&xfrm_policy_lock);
  670. goto again2;
  671. }
  672. }
  673. xfrm_policy_count[dir] = 0;
  674. }
  675. atomic_inc(&flow_cache_genid);
  676. write_unlock_bh(&xfrm_policy_lock);
  677. }
  678. EXPORT_SYMBOL(xfrm_policy_flush);
  679. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  680. void *data)
  681. {
  682. struct xfrm_policy *pol;
  683. struct hlist_node *entry;
  684. int dir, count, error;
  685. read_lock_bh(&xfrm_policy_lock);
  686. count = 0;
  687. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  688. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  689. int i;
  690. hlist_for_each_entry(pol, entry,
  691. &xfrm_policy_inexact[dir], bydst) {
  692. if (pol->type == type)
  693. count++;
  694. }
  695. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  696. hlist_for_each_entry(pol, entry, table + i, bydst) {
  697. if (pol->type == type)
  698. count++;
  699. }
  700. }
  701. }
  702. if (count == 0) {
  703. error = -ENOENT;
  704. goto out;
  705. }
  706. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  707. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  708. int i;
  709. hlist_for_each_entry(pol, entry,
  710. &xfrm_policy_inexact[dir], bydst) {
  711. if (pol->type != type)
  712. continue;
  713. error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
  714. if (error)
  715. goto out;
  716. }
  717. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  718. hlist_for_each_entry(pol, entry, table + i, bydst) {
  719. if (pol->type != type)
  720. continue;
  721. error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
  722. if (error)
  723. goto out;
  724. }
  725. }
  726. }
  727. error = 0;
  728. out:
  729. read_unlock_bh(&xfrm_policy_lock);
  730. return error;
  731. }
  732. EXPORT_SYMBOL(xfrm_policy_walk);
  733. /* Find policy to apply to this flow. */
  734. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  735. u8 type, u16 family, int dir)
  736. {
  737. struct xfrm_selector *sel = &pol->selector;
  738. int match;
  739. if (pol->family != family ||
  740. pol->type != type)
  741. return 0;
  742. match = xfrm_selector_match(sel, fl, family);
  743. if (match) {
  744. if (!security_xfrm_policy_lookup(pol, fl->secid, dir))
  745. return 1;
  746. }
  747. return 0;
  748. }
  749. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  750. u16 family, u8 dir)
  751. {
  752. struct xfrm_policy *pol, *ret;
  753. xfrm_address_t *daddr, *saddr;
  754. struct hlist_node *entry;
  755. struct hlist_head *chain;
  756. u32 priority = ~0U;
  757. daddr = xfrm_flowi_daddr(fl, family);
  758. saddr = xfrm_flowi_saddr(fl, family);
  759. if (unlikely(!daddr || !saddr))
  760. return NULL;
  761. read_lock_bh(&xfrm_policy_lock);
  762. chain = policy_hash_direct(daddr, saddr, family, dir);
  763. ret = NULL;
  764. hlist_for_each_entry(pol, entry, chain, bydst) {
  765. if (xfrm_policy_match(pol, fl, type, family, dir)) {
  766. ret = pol;
  767. priority = ret->priority;
  768. break;
  769. }
  770. }
  771. chain = &xfrm_policy_inexact[dir];
  772. hlist_for_each_entry(pol, entry, chain, bydst) {
  773. if (xfrm_policy_match(pol, fl, type, family, dir) &&
  774. pol->priority < priority) {
  775. ret = pol;
  776. break;
  777. }
  778. }
  779. if (ret)
  780. xfrm_pol_hold(ret);
  781. read_unlock_bh(&xfrm_policy_lock);
  782. return ret;
  783. }
  784. static void xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  785. void **objp, atomic_t **obj_refp)
  786. {
  787. struct xfrm_policy *pol;
  788. #ifdef CONFIG_XFRM_SUB_POLICY
  789. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  790. if (pol)
  791. goto end;
  792. #endif
  793. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  794. #ifdef CONFIG_XFRM_SUB_POLICY
  795. end:
  796. #endif
  797. if ((*objp = (void *) pol) != NULL)
  798. *obj_refp = &pol->refcnt;
  799. }
  800. static inline int policy_to_flow_dir(int dir)
  801. {
  802. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  803. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  804. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  805. return dir;
  806. switch (dir) {
  807. default:
  808. case XFRM_POLICY_IN:
  809. return FLOW_DIR_IN;
  810. case XFRM_POLICY_OUT:
  811. return FLOW_DIR_OUT;
  812. case XFRM_POLICY_FWD:
  813. return FLOW_DIR_FWD;
  814. };
  815. }
  816. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  817. {
  818. struct xfrm_policy *pol;
  819. read_lock_bh(&xfrm_policy_lock);
  820. if ((pol = sk->sk_policy[dir]) != NULL) {
  821. int match = xfrm_selector_match(&pol->selector, fl,
  822. sk->sk_family);
  823. int err = 0;
  824. if (match)
  825. err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir));
  826. if (match && !err)
  827. xfrm_pol_hold(pol);
  828. else
  829. pol = NULL;
  830. }
  831. read_unlock_bh(&xfrm_policy_lock);
  832. return pol;
  833. }
  834. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  835. {
  836. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  837. pol->family, dir);
  838. hlist_add_head(&pol->bydst, chain);
  839. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  840. xfrm_policy_count[dir]++;
  841. xfrm_pol_hold(pol);
  842. if (xfrm_bydst_should_resize(dir, NULL))
  843. schedule_work(&xfrm_hash_work);
  844. }
  845. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  846. int dir)
  847. {
  848. if (hlist_unhashed(&pol->bydst))
  849. return NULL;
  850. hlist_del(&pol->bydst);
  851. hlist_del(&pol->byidx);
  852. xfrm_policy_count[dir]--;
  853. return pol;
  854. }
  855. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  856. {
  857. write_lock_bh(&xfrm_policy_lock);
  858. pol = __xfrm_policy_unlink(pol, dir);
  859. write_unlock_bh(&xfrm_policy_lock);
  860. if (pol) {
  861. if (dir < XFRM_POLICY_MAX)
  862. atomic_inc(&flow_cache_genid);
  863. xfrm_policy_kill(pol);
  864. return 0;
  865. }
  866. return -ENOENT;
  867. }
  868. EXPORT_SYMBOL(xfrm_policy_delete);
  869. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  870. {
  871. struct xfrm_policy *old_pol;
  872. #ifdef CONFIG_XFRM_SUB_POLICY
  873. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  874. return -EINVAL;
  875. #endif
  876. write_lock_bh(&xfrm_policy_lock);
  877. old_pol = sk->sk_policy[dir];
  878. sk->sk_policy[dir] = pol;
  879. if (pol) {
  880. pol->curlft.add_time = (unsigned long)xtime.tv_sec;
  881. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  882. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  883. }
  884. if (old_pol)
  885. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  886. write_unlock_bh(&xfrm_policy_lock);
  887. if (old_pol) {
  888. xfrm_policy_kill(old_pol);
  889. }
  890. return 0;
  891. }
  892. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  893. {
  894. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  895. if (newp) {
  896. newp->selector = old->selector;
  897. if (security_xfrm_policy_clone(old, newp)) {
  898. kfree(newp);
  899. return NULL; /* ENOMEM */
  900. }
  901. newp->lft = old->lft;
  902. newp->curlft = old->curlft;
  903. newp->action = old->action;
  904. newp->flags = old->flags;
  905. newp->xfrm_nr = old->xfrm_nr;
  906. newp->index = old->index;
  907. newp->type = old->type;
  908. memcpy(newp->xfrm_vec, old->xfrm_vec,
  909. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  910. write_lock_bh(&xfrm_policy_lock);
  911. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  912. write_unlock_bh(&xfrm_policy_lock);
  913. xfrm_pol_put(newp);
  914. }
  915. return newp;
  916. }
  917. int __xfrm_sk_clone_policy(struct sock *sk)
  918. {
  919. struct xfrm_policy *p0 = sk->sk_policy[0],
  920. *p1 = sk->sk_policy[1];
  921. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  922. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  923. return -ENOMEM;
  924. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  925. return -ENOMEM;
  926. return 0;
  927. }
  928. static int
  929. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  930. unsigned short family)
  931. {
  932. int err;
  933. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  934. if (unlikely(afinfo == NULL))
  935. return -EINVAL;
  936. err = afinfo->get_saddr(local, remote);
  937. xfrm_policy_put_afinfo(afinfo);
  938. return err;
  939. }
  940. /* Resolve list of templates for the flow, given policy. */
  941. static int
  942. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  943. struct xfrm_state **xfrm,
  944. unsigned short family)
  945. {
  946. int nx;
  947. int i, error;
  948. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  949. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  950. xfrm_address_t tmp;
  951. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  952. struct xfrm_state *x;
  953. xfrm_address_t *remote = daddr;
  954. xfrm_address_t *local = saddr;
  955. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  956. if (tmpl->mode == XFRM_MODE_TUNNEL) {
  957. remote = &tmpl->id.daddr;
  958. local = &tmpl->saddr;
  959. if (xfrm_addr_any(local, family)) {
  960. error = xfrm_get_saddr(&tmp, remote, family);
  961. if (error)
  962. goto fail;
  963. local = &tmp;
  964. }
  965. }
  966. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  967. if (x && x->km.state == XFRM_STATE_VALID) {
  968. xfrm[nx++] = x;
  969. daddr = remote;
  970. saddr = local;
  971. continue;
  972. }
  973. if (x) {
  974. error = (x->km.state == XFRM_STATE_ERROR ?
  975. -EINVAL : -EAGAIN);
  976. xfrm_state_put(x);
  977. }
  978. if (!tmpl->optional)
  979. goto fail;
  980. }
  981. return nx;
  982. fail:
  983. for (nx--; nx>=0; nx--)
  984. xfrm_state_put(xfrm[nx]);
  985. return error;
  986. }
  987. static int
  988. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  989. struct xfrm_state **xfrm,
  990. unsigned short family)
  991. {
  992. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  993. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  994. int cnx = 0;
  995. int error;
  996. int ret;
  997. int i;
  998. for (i = 0; i < npols; i++) {
  999. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1000. error = -ENOBUFS;
  1001. goto fail;
  1002. }
  1003. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1004. if (ret < 0) {
  1005. error = ret;
  1006. goto fail;
  1007. } else
  1008. cnx += ret;
  1009. }
  1010. /* found states are sorted for outbound processing */
  1011. if (npols > 1)
  1012. xfrm_state_sort(xfrm, tpp, cnx, family);
  1013. return cnx;
  1014. fail:
  1015. for (cnx--; cnx>=0; cnx--)
  1016. xfrm_state_put(tpp[cnx]);
  1017. return error;
  1018. }
  1019. /* Check that the bundle accepts the flow and its components are
  1020. * still valid.
  1021. */
  1022. static struct dst_entry *
  1023. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1024. {
  1025. struct dst_entry *x;
  1026. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1027. if (unlikely(afinfo == NULL))
  1028. return ERR_PTR(-EINVAL);
  1029. x = afinfo->find_bundle(fl, policy);
  1030. xfrm_policy_put_afinfo(afinfo);
  1031. return x;
  1032. }
  1033. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1034. * all the metrics... Shortly, bundle a bundle.
  1035. */
  1036. static int
  1037. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1038. struct flowi *fl, struct dst_entry **dst_p,
  1039. unsigned short family)
  1040. {
  1041. int err;
  1042. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1043. if (unlikely(afinfo == NULL))
  1044. return -EINVAL;
  1045. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1046. xfrm_policy_put_afinfo(afinfo);
  1047. return err;
  1048. }
  1049. static int stale_bundle(struct dst_entry *dst);
  1050. /* Main function: finds/creates a bundle for given flow.
  1051. *
  1052. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1053. * on interfaces with disabled IPsec.
  1054. */
  1055. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1056. struct sock *sk, int flags)
  1057. {
  1058. struct xfrm_policy *policy;
  1059. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1060. int npols;
  1061. int pol_dead;
  1062. int xfrm_nr;
  1063. int pi;
  1064. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1065. struct dst_entry *dst, *dst_orig = *dst_p;
  1066. int nx = 0;
  1067. int err;
  1068. u32 genid;
  1069. u16 family;
  1070. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1071. restart:
  1072. genid = atomic_read(&flow_cache_genid);
  1073. policy = NULL;
  1074. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1075. pols[pi] = NULL;
  1076. npols = 0;
  1077. pol_dead = 0;
  1078. xfrm_nr = 0;
  1079. if (sk && sk->sk_policy[1])
  1080. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1081. if (!policy) {
  1082. /* To accelerate a bit... */
  1083. if ((dst_orig->flags & DST_NOXFRM) ||
  1084. !xfrm_policy_count[XFRM_POLICY_OUT])
  1085. return 0;
  1086. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1087. dir, xfrm_policy_lookup);
  1088. }
  1089. if (!policy)
  1090. return 0;
  1091. family = dst_orig->ops->family;
  1092. policy->curlft.use_time = (unsigned long)xtime.tv_sec;
  1093. pols[0] = policy;
  1094. npols ++;
  1095. xfrm_nr += pols[0]->xfrm_nr;
  1096. switch (policy->action) {
  1097. case XFRM_POLICY_BLOCK:
  1098. /* Prohibit the flow */
  1099. err = -EPERM;
  1100. goto error;
  1101. case XFRM_POLICY_ALLOW:
  1102. #ifndef CONFIG_XFRM_SUB_POLICY
  1103. if (policy->xfrm_nr == 0) {
  1104. /* Flow passes not transformed. */
  1105. xfrm_pol_put(policy);
  1106. return 0;
  1107. }
  1108. #endif
  1109. /* Try to find matching bundle.
  1110. *
  1111. * LATER: help from flow cache. It is optional, this
  1112. * is required only for output policy.
  1113. */
  1114. dst = xfrm_find_bundle(fl, policy, family);
  1115. if (IS_ERR(dst)) {
  1116. err = PTR_ERR(dst);
  1117. goto error;
  1118. }
  1119. if (dst)
  1120. break;
  1121. #ifdef CONFIG_XFRM_SUB_POLICY
  1122. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1123. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1124. fl, family,
  1125. XFRM_POLICY_OUT);
  1126. if (pols[1]) {
  1127. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1128. err = -EPERM;
  1129. goto error;
  1130. }
  1131. npols ++;
  1132. xfrm_nr += pols[1]->xfrm_nr;
  1133. }
  1134. }
  1135. /*
  1136. * Because neither flowi nor bundle information knows about
  1137. * transformation template size. On more than one policy usage
  1138. * we can realize whether all of them is bypass or not after
  1139. * they are searched. See above not-transformed bypass
  1140. * is surrounded by non-sub policy configuration, too.
  1141. */
  1142. if (xfrm_nr == 0) {
  1143. /* Flow passes not transformed. */
  1144. xfrm_pols_put(pols, npols);
  1145. return 0;
  1146. }
  1147. #endif
  1148. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1149. if (unlikely(nx<0)) {
  1150. err = nx;
  1151. if (err == -EAGAIN && flags) {
  1152. DECLARE_WAITQUEUE(wait, current);
  1153. add_wait_queue(&km_waitq, &wait);
  1154. set_current_state(TASK_INTERRUPTIBLE);
  1155. schedule();
  1156. set_current_state(TASK_RUNNING);
  1157. remove_wait_queue(&km_waitq, &wait);
  1158. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1159. if (nx == -EAGAIN && signal_pending(current)) {
  1160. err = -ERESTART;
  1161. goto error;
  1162. }
  1163. if (nx == -EAGAIN ||
  1164. genid != atomic_read(&flow_cache_genid)) {
  1165. xfrm_pols_put(pols, npols);
  1166. goto restart;
  1167. }
  1168. err = nx;
  1169. }
  1170. if (err < 0)
  1171. goto error;
  1172. }
  1173. if (nx == 0) {
  1174. /* Flow passes not transformed. */
  1175. xfrm_pols_put(pols, npols);
  1176. return 0;
  1177. }
  1178. dst = dst_orig;
  1179. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1180. if (unlikely(err)) {
  1181. int i;
  1182. for (i=0; i<nx; i++)
  1183. xfrm_state_put(xfrm[i]);
  1184. goto error;
  1185. }
  1186. for (pi = 0; pi < npols; pi++) {
  1187. read_lock_bh(&pols[pi]->lock);
  1188. pol_dead |= pols[pi]->dead;
  1189. read_unlock_bh(&pols[pi]->lock);
  1190. }
  1191. write_lock_bh(&policy->lock);
  1192. if (unlikely(pol_dead || stale_bundle(dst))) {
  1193. /* Wow! While we worked on resolving, this
  1194. * policy has gone. Retry. It is not paranoia,
  1195. * we just cannot enlist new bundle to dead object.
  1196. * We can't enlist stable bundles either.
  1197. */
  1198. write_unlock_bh(&policy->lock);
  1199. if (dst)
  1200. dst_free(dst);
  1201. err = -EHOSTUNREACH;
  1202. goto error;
  1203. }
  1204. dst->next = policy->bundles;
  1205. policy->bundles = dst;
  1206. dst_hold(dst);
  1207. write_unlock_bh(&policy->lock);
  1208. }
  1209. *dst_p = dst;
  1210. dst_release(dst_orig);
  1211. xfrm_pols_put(pols, npols);
  1212. return 0;
  1213. error:
  1214. dst_release(dst_orig);
  1215. xfrm_pols_put(pols, npols);
  1216. *dst_p = NULL;
  1217. return err;
  1218. }
  1219. EXPORT_SYMBOL(xfrm_lookup);
  1220. static inline int
  1221. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1222. {
  1223. struct xfrm_state *x;
  1224. int err;
  1225. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1226. return 0;
  1227. x = skb->sp->xvec[idx];
  1228. if (!x->type->reject)
  1229. return 0;
  1230. xfrm_state_hold(x);
  1231. err = x->type->reject(x, skb, fl);
  1232. xfrm_state_put(x);
  1233. return err;
  1234. }
  1235. /* When skb is transformed back to its "native" form, we have to
  1236. * check policy restrictions. At the moment we make this in maximally
  1237. * stupid way. Shame on me. :-) Of course, connected sockets must
  1238. * have policy cached at them.
  1239. */
  1240. static inline int
  1241. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1242. unsigned short family)
  1243. {
  1244. if (xfrm_state_kern(x))
  1245. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
  1246. return x->id.proto == tmpl->id.proto &&
  1247. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1248. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1249. x->props.mode == tmpl->mode &&
  1250. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1251. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1252. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1253. xfrm_state_addr_cmp(tmpl, x, family));
  1254. }
  1255. /*
  1256. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1257. * because of optional transport mode, or next index of the mathced secpath
  1258. * state with the template.
  1259. * -1 is returned when no matching template is found.
  1260. * Otherwise "-2 - errored_index" is returned.
  1261. */
  1262. static inline int
  1263. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1264. unsigned short family)
  1265. {
  1266. int idx = start;
  1267. if (tmpl->optional) {
  1268. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1269. return start;
  1270. } else
  1271. start = -1;
  1272. for (; idx < sp->len; idx++) {
  1273. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1274. return ++idx;
  1275. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1276. if (start == -1)
  1277. start = -2-idx;
  1278. break;
  1279. }
  1280. }
  1281. return start;
  1282. }
  1283. int
  1284. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1285. {
  1286. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1287. int err;
  1288. if (unlikely(afinfo == NULL))
  1289. return -EAFNOSUPPORT;
  1290. afinfo->decode_session(skb, fl);
  1291. err = security_xfrm_decode_session(skb, &fl->secid);
  1292. xfrm_policy_put_afinfo(afinfo);
  1293. return err;
  1294. }
  1295. EXPORT_SYMBOL(xfrm_decode_session);
  1296. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1297. {
  1298. for (; k < sp->len; k++) {
  1299. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1300. *idxp = k;
  1301. return 1;
  1302. }
  1303. }
  1304. return 0;
  1305. }
  1306. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1307. unsigned short family)
  1308. {
  1309. struct xfrm_policy *pol;
  1310. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1311. int npols = 0;
  1312. int xfrm_nr;
  1313. int pi;
  1314. struct flowi fl;
  1315. u8 fl_dir = policy_to_flow_dir(dir);
  1316. int xerr_idx = -1;
  1317. if (xfrm_decode_session(skb, &fl, family) < 0)
  1318. return 0;
  1319. nf_nat_decode_session(skb, &fl, family);
  1320. /* First, check used SA against their selectors. */
  1321. if (skb->sp) {
  1322. int i;
  1323. for (i=skb->sp->len-1; i>=0; i--) {
  1324. struct xfrm_state *x = skb->sp->xvec[i];
  1325. if (!xfrm_selector_match(&x->sel, &fl, family))
  1326. return 0;
  1327. }
  1328. }
  1329. pol = NULL;
  1330. if (sk && sk->sk_policy[dir])
  1331. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1332. if (!pol)
  1333. pol = flow_cache_lookup(&fl, family, fl_dir,
  1334. xfrm_policy_lookup);
  1335. if (!pol) {
  1336. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1337. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1338. return 0;
  1339. }
  1340. return 1;
  1341. }
  1342. pol->curlft.use_time = (unsigned long)xtime.tv_sec;
  1343. pols[0] = pol;
  1344. npols ++;
  1345. #ifdef CONFIG_XFRM_SUB_POLICY
  1346. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1347. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1348. &fl, family,
  1349. XFRM_POLICY_IN);
  1350. if (pols[1]) {
  1351. pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
  1352. npols ++;
  1353. }
  1354. }
  1355. #endif
  1356. if (pol->action == XFRM_POLICY_ALLOW) {
  1357. struct sec_path *sp;
  1358. static struct sec_path dummy;
  1359. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1360. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1361. struct xfrm_tmpl **tpp = tp;
  1362. int ti = 0;
  1363. int i, k;
  1364. if ((sp = skb->sp) == NULL)
  1365. sp = &dummy;
  1366. for (pi = 0; pi < npols; pi++) {
  1367. if (pols[pi] != pol &&
  1368. pols[pi]->action != XFRM_POLICY_ALLOW)
  1369. goto reject;
  1370. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1371. goto reject_error;
  1372. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1373. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1374. }
  1375. xfrm_nr = ti;
  1376. if (npols > 1) {
  1377. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1378. tpp = stp;
  1379. }
  1380. /* For each tunnel xfrm, find the first matching tmpl.
  1381. * For each tmpl before that, find corresponding xfrm.
  1382. * Order is _important_. Later we will implement
  1383. * some barriers, but at the moment barriers
  1384. * are implied between each two transformations.
  1385. */
  1386. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1387. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1388. if (k < 0) {
  1389. if (k < -1)
  1390. /* "-2 - errored_index" returned */
  1391. xerr_idx = -(2+k);
  1392. goto reject;
  1393. }
  1394. }
  1395. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1396. goto reject;
  1397. xfrm_pols_put(pols, npols);
  1398. return 1;
  1399. }
  1400. reject:
  1401. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1402. reject_error:
  1403. xfrm_pols_put(pols, npols);
  1404. return 0;
  1405. }
  1406. EXPORT_SYMBOL(__xfrm_policy_check);
  1407. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1408. {
  1409. struct flowi fl;
  1410. if (xfrm_decode_session(skb, &fl, family) < 0)
  1411. return 0;
  1412. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1413. }
  1414. EXPORT_SYMBOL(__xfrm_route_forward);
  1415. /* Optimize later using cookies and generation ids. */
  1416. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1417. {
  1418. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1419. * to "-1" to force all XFRM destinations to get validated by
  1420. * dst_ops->check on every use. We do this because when a
  1421. * normal route referenced by an XFRM dst is obsoleted we do
  1422. * not go looking around for all parent referencing XFRM dsts
  1423. * so that we can invalidate them. It is just too much work.
  1424. * Instead we make the checks here on every use. For example:
  1425. *
  1426. * XFRM dst A --> IPv4 dst X
  1427. *
  1428. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1429. * in this example). If X is marked obsolete, "A" will not
  1430. * notice. That's what we are validating here via the
  1431. * stale_bundle() check.
  1432. *
  1433. * When a policy's bundle is pruned, we dst_free() the XFRM
  1434. * dst which causes it's ->obsolete field to be set to a
  1435. * positive non-zero integer. If an XFRM dst has been pruned
  1436. * like this, we want to force a new route lookup.
  1437. */
  1438. if (dst->obsolete < 0 && !stale_bundle(dst))
  1439. return dst;
  1440. return NULL;
  1441. }
  1442. static int stale_bundle(struct dst_entry *dst)
  1443. {
  1444. return !xfrm_bundle_ok((struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1445. }
  1446. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1447. {
  1448. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1449. dst->dev = &loopback_dev;
  1450. dev_hold(&loopback_dev);
  1451. dev_put(dev);
  1452. }
  1453. }
  1454. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1455. static void xfrm_link_failure(struct sk_buff *skb)
  1456. {
  1457. /* Impossible. Such dst must be popped before reaches point of failure. */
  1458. return;
  1459. }
  1460. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1461. {
  1462. if (dst) {
  1463. if (dst->obsolete) {
  1464. dst_release(dst);
  1465. dst = NULL;
  1466. }
  1467. }
  1468. return dst;
  1469. }
  1470. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1471. {
  1472. struct dst_entry *dst, **dstp;
  1473. write_lock(&pol->lock);
  1474. dstp = &pol->bundles;
  1475. while ((dst=*dstp) != NULL) {
  1476. if (func(dst)) {
  1477. *dstp = dst->next;
  1478. dst->next = *gc_list_p;
  1479. *gc_list_p = dst;
  1480. } else {
  1481. dstp = &dst->next;
  1482. }
  1483. }
  1484. write_unlock(&pol->lock);
  1485. }
  1486. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1487. {
  1488. struct dst_entry *gc_list = NULL;
  1489. int dir;
  1490. read_lock_bh(&xfrm_policy_lock);
  1491. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1492. struct xfrm_policy *pol;
  1493. struct hlist_node *entry;
  1494. struct hlist_head *table;
  1495. int i;
  1496. hlist_for_each_entry(pol, entry,
  1497. &xfrm_policy_inexact[dir], bydst)
  1498. prune_one_bundle(pol, func, &gc_list);
  1499. table = xfrm_policy_bydst[dir].table;
  1500. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1501. hlist_for_each_entry(pol, entry, table + i, bydst)
  1502. prune_one_bundle(pol, func, &gc_list);
  1503. }
  1504. }
  1505. read_unlock_bh(&xfrm_policy_lock);
  1506. while (gc_list) {
  1507. struct dst_entry *dst = gc_list;
  1508. gc_list = dst->next;
  1509. dst_free(dst);
  1510. }
  1511. }
  1512. static int unused_bundle(struct dst_entry *dst)
  1513. {
  1514. return !atomic_read(&dst->__refcnt);
  1515. }
  1516. static void __xfrm_garbage_collect(void)
  1517. {
  1518. xfrm_prune_bundles(unused_bundle);
  1519. }
  1520. static int xfrm_flush_bundles(void)
  1521. {
  1522. xfrm_prune_bundles(stale_bundle);
  1523. return 0;
  1524. }
  1525. void xfrm_init_pmtu(struct dst_entry *dst)
  1526. {
  1527. do {
  1528. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1529. u32 pmtu, route_mtu_cached;
  1530. pmtu = dst_mtu(dst->child);
  1531. xdst->child_mtu_cached = pmtu;
  1532. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1533. route_mtu_cached = dst_mtu(xdst->route);
  1534. xdst->route_mtu_cached = route_mtu_cached;
  1535. if (pmtu > route_mtu_cached)
  1536. pmtu = route_mtu_cached;
  1537. dst->metrics[RTAX_MTU-1] = pmtu;
  1538. } while ((dst = dst->next));
  1539. }
  1540. EXPORT_SYMBOL(xfrm_init_pmtu);
  1541. /* Check that the bundle accepts the flow and its components are
  1542. * still valid.
  1543. */
  1544. int xfrm_bundle_ok(struct xfrm_dst *first, struct flowi *fl, int family, int strict)
  1545. {
  1546. struct dst_entry *dst = &first->u.dst;
  1547. struct xfrm_dst *last;
  1548. u32 mtu;
  1549. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1550. (dst->dev && !netif_running(dst->dev)))
  1551. return 0;
  1552. last = NULL;
  1553. do {
  1554. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1555. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1556. return 0;
  1557. if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm))
  1558. return 0;
  1559. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1560. return 0;
  1561. if (xdst->genid != dst->xfrm->genid)
  1562. return 0;
  1563. if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
  1564. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1565. return 0;
  1566. mtu = dst_mtu(dst->child);
  1567. if (xdst->child_mtu_cached != mtu) {
  1568. last = xdst;
  1569. xdst->child_mtu_cached = mtu;
  1570. }
  1571. if (!dst_check(xdst->route, xdst->route_cookie))
  1572. return 0;
  1573. mtu = dst_mtu(xdst->route);
  1574. if (xdst->route_mtu_cached != mtu) {
  1575. last = xdst;
  1576. xdst->route_mtu_cached = mtu;
  1577. }
  1578. dst = dst->child;
  1579. } while (dst->xfrm);
  1580. if (likely(!last))
  1581. return 1;
  1582. mtu = last->child_mtu_cached;
  1583. for (;;) {
  1584. dst = &last->u.dst;
  1585. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1586. if (mtu > last->route_mtu_cached)
  1587. mtu = last->route_mtu_cached;
  1588. dst->metrics[RTAX_MTU-1] = mtu;
  1589. if (last == first)
  1590. break;
  1591. last = last->u.next;
  1592. last->child_mtu_cached = mtu;
  1593. }
  1594. return 1;
  1595. }
  1596. EXPORT_SYMBOL(xfrm_bundle_ok);
  1597. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1598. {
  1599. int err = 0;
  1600. if (unlikely(afinfo == NULL))
  1601. return -EINVAL;
  1602. if (unlikely(afinfo->family >= NPROTO))
  1603. return -EAFNOSUPPORT;
  1604. write_lock_bh(&xfrm_policy_afinfo_lock);
  1605. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1606. err = -ENOBUFS;
  1607. else {
  1608. struct dst_ops *dst_ops = afinfo->dst_ops;
  1609. if (likely(dst_ops->kmem_cachep == NULL))
  1610. dst_ops->kmem_cachep = xfrm_dst_cache;
  1611. if (likely(dst_ops->check == NULL))
  1612. dst_ops->check = xfrm_dst_check;
  1613. if (likely(dst_ops->negative_advice == NULL))
  1614. dst_ops->negative_advice = xfrm_negative_advice;
  1615. if (likely(dst_ops->link_failure == NULL))
  1616. dst_ops->link_failure = xfrm_link_failure;
  1617. if (likely(afinfo->garbage_collect == NULL))
  1618. afinfo->garbage_collect = __xfrm_garbage_collect;
  1619. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1620. }
  1621. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1622. return err;
  1623. }
  1624. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1625. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1626. {
  1627. int err = 0;
  1628. if (unlikely(afinfo == NULL))
  1629. return -EINVAL;
  1630. if (unlikely(afinfo->family >= NPROTO))
  1631. return -EAFNOSUPPORT;
  1632. write_lock_bh(&xfrm_policy_afinfo_lock);
  1633. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1634. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1635. err = -EINVAL;
  1636. else {
  1637. struct dst_ops *dst_ops = afinfo->dst_ops;
  1638. xfrm_policy_afinfo[afinfo->family] = NULL;
  1639. dst_ops->kmem_cachep = NULL;
  1640. dst_ops->check = NULL;
  1641. dst_ops->negative_advice = NULL;
  1642. dst_ops->link_failure = NULL;
  1643. afinfo->garbage_collect = NULL;
  1644. }
  1645. }
  1646. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1647. return err;
  1648. }
  1649. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1650. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1651. {
  1652. struct xfrm_policy_afinfo *afinfo;
  1653. if (unlikely(family >= NPROTO))
  1654. return NULL;
  1655. read_lock(&xfrm_policy_afinfo_lock);
  1656. afinfo = xfrm_policy_afinfo[family];
  1657. if (unlikely(!afinfo))
  1658. read_unlock(&xfrm_policy_afinfo_lock);
  1659. return afinfo;
  1660. }
  1661. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1662. {
  1663. read_unlock(&xfrm_policy_afinfo_lock);
  1664. }
  1665. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
  1666. {
  1667. struct xfrm_policy_afinfo *afinfo;
  1668. if (unlikely(family >= NPROTO))
  1669. return NULL;
  1670. write_lock_bh(&xfrm_policy_afinfo_lock);
  1671. afinfo = xfrm_policy_afinfo[family];
  1672. if (unlikely(!afinfo))
  1673. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1674. return afinfo;
  1675. }
  1676. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
  1677. {
  1678. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1679. }
  1680. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1681. {
  1682. switch (event) {
  1683. case NETDEV_DOWN:
  1684. xfrm_flush_bundles();
  1685. }
  1686. return NOTIFY_DONE;
  1687. }
  1688. static struct notifier_block xfrm_dev_notifier = {
  1689. xfrm_dev_event,
  1690. NULL,
  1691. 0
  1692. };
  1693. static void __init xfrm_policy_init(void)
  1694. {
  1695. unsigned int hmask, sz;
  1696. int dir;
  1697. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1698. sizeof(struct xfrm_dst),
  1699. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1700. NULL, NULL);
  1701. hmask = 8 - 1;
  1702. sz = (hmask+1) * sizeof(struct hlist_head);
  1703. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1704. xfrm_idx_hmask = hmask;
  1705. if (!xfrm_policy_byidx)
  1706. panic("XFRM: failed to allocate byidx hash\n");
  1707. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1708. struct xfrm_policy_hash *htab;
  1709. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1710. htab = &xfrm_policy_bydst[dir];
  1711. htab->table = xfrm_hash_alloc(sz);
  1712. htab->hmask = hmask;
  1713. if (!htab->table)
  1714. panic("XFRM: failed to allocate bydst hash\n");
  1715. }
  1716. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
  1717. register_netdevice_notifier(&xfrm_dev_notifier);
  1718. }
  1719. void __init xfrm_init(void)
  1720. {
  1721. xfrm_state_init();
  1722. xfrm_policy_init();
  1723. xfrm_input_init();
  1724. }