xfrm_policy.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  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, killed;
  646. killed = 0;
  647. again1:
  648. hlist_for_each_entry(pol, entry,
  649. &xfrm_policy_inexact[dir], bydst) {
  650. if (pol->type != type)
  651. continue;
  652. hlist_del(&pol->bydst);
  653. hlist_del(&pol->byidx);
  654. write_unlock_bh(&xfrm_policy_lock);
  655. xfrm_policy_kill(pol);
  656. killed++;
  657. write_lock_bh(&xfrm_policy_lock);
  658. goto again1;
  659. }
  660. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  661. again2:
  662. hlist_for_each_entry(pol, entry,
  663. xfrm_policy_bydst[dir].table + i,
  664. bydst) {
  665. if (pol->type != type)
  666. continue;
  667. hlist_del(&pol->bydst);
  668. hlist_del(&pol->byidx);
  669. write_unlock_bh(&xfrm_policy_lock);
  670. xfrm_policy_kill(pol);
  671. killed++;
  672. write_lock_bh(&xfrm_policy_lock);
  673. goto again2;
  674. }
  675. }
  676. xfrm_policy_count[dir] -= killed;
  677. }
  678. atomic_inc(&flow_cache_genid);
  679. write_unlock_bh(&xfrm_policy_lock);
  680. }
  681. EXPORT_SYMBOL(xfrm_policy_flush);
  682. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  683. void *data)
  684. {
  685. struct xfrm_policy *pol;
  686. struct hlist_node *entry;
  687. int dir, count, error;
  688. read_lock_bh(&xfrm_policy_lock);
  689. count = 0;
  690. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  691. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  692. int i;
  693. hlist_for_each_entry(pol, entry,
  694. &xfrm_policy_inexact[dir], bydst) {
  695. if (pol->type == type)
  696. count++;
  697. }
  698. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  699. hlist_for_each_entry(pol, entry, table + i, bydst) {
  700. if (pol->type == type)
  701. count++;
  702. }
  703. }
  704. }
  705. if (count == 0) {
  706. error = -ENOENT;
  707. goto out;
  708. }
  709. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  710. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  711. int i;
  712. hlist_for_each_entry(pol, entry,
  713. &xfrm_policy_inexact[dir], bydst) {
  714. if (pol->type != type)
  715. continue;
  716. error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
  717. if (error)
  718. goto out;
  719. }
  720. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  721. hlist_for_each_entry(pol, entry, table + i, bydst) {
  722. if (pol->type != type)
  723. continue;
  724. error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
  725. if (error)
  726. goto out;
  727. }
  728. }
  729. }
  730. error = 0;
  731. out:
  732. read_unlock_bh(&xfrm_policy_lock);
  733. return error;
  734. }
  735. EXPORT_SYMBOL(xfrm_policy_walk);
  736. /*
  737. * Find policy to apply to this flow.
  738. *
  739. * Returns 0 if policy found, else an -errno.
  740. */
  741. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  742. u8 type, u16 family, int dir)
  743. {
  744. struct xfrm_selector *sel = &pol->selector;
  745. int match, ret = -ESRCH;
  746. if (pol->family != family ||
  747. pol->type != type)
  748. return ret;
  749. match = xfrm_selector_match(sel, fl, family);
  750. if (match)
  751. ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
  752. return ret;
  753. }
  754. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  755. u16 family, u8 dir)
  756. {
  757. int err;
  758. struct xfrm_policy *pol, *ret;
  759. xfrm_address_t *daddr, *saddr;
  760. struct hlist_node *entry;
  761. struct hlist_head *chain;
  762. u32 priority = ~0U;
  763. daddr = xfrm_flowi_daddr(fl, family);
  764. saddr = xfrm_flowi_saddr(fl, family);
  765. if (unlikely(!daddr || !saddr))
  766. return NULL;
  767. read_lock_bh(&xfrm_policy_lock);
  768. chain = policy_hash_direct(daddr, saddr, family, dir);
  769. ret = NULL;
  770. hlist_for_each_entry(pol, entry, chain, bydst) {
  771. err = xfrm_policy_match(pol, fl, type, family, dir);
  772. if (err) {
  773. if (err == -ESRCH)
  774. continue;
  775. else {
  776. ret = ERR_PTR(err);
  777. goto fail;
  778. }
  779. } else {
  780. ret = pol;
  781. priority = ret->priority;
  782. break;
  783. }
  784. }
  785. chain = &xfrm_policy_inexact[dir];
  786. hlist_for_each_entry(pol, entry, chain, bydst) {
  787. err = xfrm_policy_match(pol, fl, type, family, dir);
  788. if (err) {
  789. if (err == -ESRCH)
  790. continue;
  791. else {
  792. ret = ERR_PTR(err);
  793. goto fail;
  794. }
  795. } else if (pol->priority < priority) {
  796. ret = pol;
  797. break;
  798. }
  799. }
  800. if (ret)
  801. xfrm_pol_hold(ret);
  802. fail:
  803. read_unlock_bh(&xfrm_policy_lock);
  804. return ret;
  805. }
  806. static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  807. void **objp, atomic_t **obj_refp)
  808. {
  809. struct xfrm_policy *pol;
  810. int err = 0;
  811. #ifdef CONFIG_XFRM_SUB_POLICY
  812. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  813. if (IS_ERR(pol)) {
  814. err = PTR_ERR(pol);
  815. pol = NULL;
  816. }
  817. if (pol || err)
  818. goto end;
  819. #endif
  820. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  821. if (IS_ERR(pol)) {
  822. err = PTR_ERR(pol);
  823. pol = NULL;
  824. }
  825. #ifdef CONFIG_XFRM_SUB_POLICY
  826. end:
  827. #endif
  828. if ((*objp = (void *) pol) != NULL)
  829. *obj_refp = &pol->refcnt;
  830. return err;
  831. }
  832. static inline int policy_to_flow_dir(int dir)
  833. {
  834. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  835. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  836. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  837. return dir;
  838. switch (dir) {
  839. default:
  840. case XFRM_POLICY_IN:
  841. return FLOW_DIR_IN;
  842. case XFRM_POLICY_OUT:
  843. return FLOW_DIR_OUT;
  844. case XFRM_POLICY_FWD:
  845. return FLOW_DIR_FWD;
  846. };
  847. }
  848. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  849. {
  850. struct xfrm_policy *pol;
  851. read_lock_bh(&xfrm_policy_lock);
  852. if ((pol = sk->sk_policy[dir]) != NULL) {
  853. int match = xfrm_selector_match(&pol->selector, fl,
  854. sk->sk_family);
  855. int err = 0;
  856. if (match) {
  857. err = security_xfrm_policy_lookup(pol, fl->secid,
  858. policy_to_flow_dir(dir));
  859. if (!err)
  860. xfrm_pol_hold(pol);
  861. else if (err == -ESRCH)
  862. pol = NULL;
  863. else
  864. pol = ERR_PTR(err);
  865. } else
  866. pol = NULL;
  867. }
  868. read_unlock_bh(&xfrm_policy_lock);
  869. return pol;
  870. }
  871. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  872. {
  873. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  874. pol->family, dir);
  875. hlist_add_head(&pol->bydst, chain);
  876. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  877. xfrm_policy_count[dir]++;
  878. xfrm_pol_hold(pol);
  879. if (xfrm_bydst_should_resize(dir, NULL))
  880. schedule_work(&xfrm_hash_work);
  881. }
  882. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  883. int dir)
  884. {
  885. if (hlist_unhashed(&pol->bydst))
  886. return NULL;
  887. hlist_del(&pol->bydst);
  888. hlist_del(&pol->byidx);
  889. xfrm_policy_count[dir]--;
  890. return pol;
  891. }
  892. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  893. {
  894. write_lock_bh(&xfrm_policy_lock);
  895. pol = __xfrm_policy_unlink(pol, dir);
  896. write_unlock_bh(&xfrm_policy_lock);
  897. if (pol) {
  898. if (dir < XFRM_POLICY_MAX)
  899. atomic_inc(&flow_cache_genid);
  900. xfrm_policy_kill(pol);
  901. return 0;
  902. }
  903. return -ENOENT;
  904. }
  905. EXPORT_SYMBOL(xfrm_policy_delete);
  906. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  907. {
  908. struct xfrm_policy *old_pol;
  909. #ifdef CONFIG_XFRM_SUB_POLICY
  910. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  911. return -EINVAL;
  912. #endif
  913. write_lock_bh(&xfrm_policy_lock);
  914. old_pol = sk->sk_policy[dir];
  915. sk->sk_policy[dir] = pol;
  916. if (pol) {
  917. pol->curlft.add_time = (unsigned long)xtime.tv_sec;
  918. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  919. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  920. }
  921. if (old_pol)
  922. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  923. write_unlock_bh(&xfrm_policy_lock);
  924. if (old_pol) {
  925. xfrm_policy_kill(old_pol);
  926. }
  927. return 0;
  928. }
  929. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  930. {
  931. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  932. if (newp) {
  933. newp->selector = old->selector;
  934. if (security_xfrm_policy_clone(old, newp)) {
  935. kfree(newp);
  936. return NULL; /* ENOMEM */
  937. }
  938. newp->lft = old->lft;
  939. newp->curlft = old->curlft;
  940. newp->action = old->action;
  941. newp->flags = old->flags;
  942. newp->xfrm_nr = old->xfrm_nr;
  943. newp->index = old->index;
  944. newp->type = old->type;
  945. memcpy(newp->xfrm_vec, old->xfrm_vec,
  946. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  947. write_lock_bh(&xfrm_policy_lock);
  948. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  949. write_unlock_bh(&xfrm_policy_lock);
  950. xfrm_pol_put(newp);
  951. }
  952. return newp;
  953. }
  954. int __xfrm_sk_clone_policy(struct sock *sk)
  955. {
  956. struct xfrm_policy *p0 = sk->sk_policy[0],
  957. *p1 = sk->sk_policy[1];
  958. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  959. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  960. return -ENOMEM;
  961. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  962. return -ENOMEM;
  963. return 0;
  964. }
  965. static int
  966. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  967. unsigned short family)
  968. {
  969. int err;
  970. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  971. if (unlikely(afinfo == NULL))
  972. return -EINVAL;
  973. err = afinfo->get_saddr(local, remote);
  974. xfrm_policy_put_afinfo(afinfo);
  975. return err;
  976. }
  977. /* Resolve list of templates for the flow, given policy. */
  978. static int
  979. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  980. struct xfrm_state **xfrm,
  981. unsigned short family)
  982. {
  983. int nx;
  984. int i, error;
  985. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  986. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  987. xfrm_address_t tmp;
  988. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  989. struct xfrm_state *x;
  990. xfrm_address_t *remote = daddr;
  991. xfrm_address_t *local = saddr;
  992. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  993. if (tmpl->mode == XFRM_MODE_TUNNEL) {
  994. remote = &tmpl->id.daddr;
  995. local = &tmpl->saddr;
  996. if (xfrm_addr_any(local, family)) {
  997. error = xfrm_get_saddr(&tmp, remote, family);
  998. if (error)
  999. goto fail;
  1000. local = &tmp;
  1001. }
  1002. }
  1003. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  1004. if (x && x->km.state == XFRM_STATE_VALID) {
  1005. xfrm[nx++] = x;
  1006. daddr = remote;
  1007. saddr = local;
  1008. continue;
  1009. }
  1010. if (x) {
  1011. error = (x->km.state == XFRM_STATE_ERROR ?
  1012. -EINVAL : -EAGAIN);
  1013. xfrm_state_put(x);
  1014. }
  1015. if (!tmpl->optional)
  1016. goto fail;
  1017. }
  1018. return nx;
  1019. fail:
  1020. for (nx--; nx>=0; nx--)
  1021. xfrm_state_put(xfrm[nx]);
  1022. return error;
  1023. }
  1024. static int
  1025. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1026. struct xfrm_state **xfrm,
  1027. unsigned short family)
  1028. {
  1029. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1030. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1031. int cnx = 0;
  1032. int error;
  1033. int ret;
  1034. int i;
  1035. for (i = 0; i < npols; i++) {
  1036. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1037. error = -ENOBUFS;
  1038. goto fail;
  1039. }
  1040. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1041. if (ret < 0) {
  1042. error = ret;
  1043. goto fail;
  1044. } else
  1045. cnx += ret;
  1046. }
  1047. /* found states are sorted for outbound processing */
  1048. if (npols > 1)
  1049. xfrm_state_sort(xfrm, tpp, cnx, family);
  1050. return cnx;
  1051. fail:
  1052. for (cnx--; cnx>=0; cnx--)
  1053. xfrm_state_put(tpp[cnx]);
  1054. return error;
  1055. }
  1056. /* Check that the bundle accepts the flow and its components are
  1057. * still valid.
  1058. */
  1059. static struct dst_entry *
  1060. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1061. {
  1062. struct dst_entry *x;
  1063. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1064. if (unlikely(afinfo == NULL))
  1065. return ERR_PTR(-EINVAL);
  1066. x = afinfo->find_bundle(fl, policy);
  1067. xfrm_policy_put_afinfo(afinfo);
  1068. return x;
  1069. }
  1070. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1071. * all the metrics... Shortly, bundle a bundle.
  1072. */
  1073. static int
  1074. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1075. struct flowi *fl, struct dst_entry **dst_p,
  1076. unsigned short family)
  1077. {
  1078. int err;
  1079. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1080. if (unlikely(afinfo == NULL))
  1081. return -EINVAL;
  1082. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1083. xfrm_policy_put_afinfo(afinfo);
  1084. return err;
  1085. }
  1086. static int stale_bundle(struct dst_entry *dst);
  1087. /* Main function: finds/creates a bundle for given flow.
  1088. *
  1089. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1090. * on interfaces with disabled IPsec.
  1091. */
  1092. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1093. struct sock *sk, int flags)
  1094. {
  1095. struct xfrm_policy *policy;
  1096. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1097. int npols;
  1098. int pol_dead;
  1099. int xfrm_nr;
  1100. int pi;
  1101. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1102. struct dst_entry *dst, *dst_orig = *dst_p;
  1103. int nx = 0;
  1104. int err;
  1105. u32 genid;
  1106. u16 family;
  1107. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1108. restart:
  1109. genid = atomic_read(&flow_cache_genid);
  1110. policy = NULL;
  1111. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1112. pols[pi] = NULL;
  1113. npols = 0;
  1114. pol_dead = 0;
  1115. xfrm_nr = 0;
  1116. if (sk && sk->sk_policy[1]) {
  1117. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1118. if (IS_ERR(policy))
  1119. return PTR_ERR(policy);
  1120. }
  1121. if (!policy) {
  1122. /* To accelerate a bit... */
  1123. if ((dst_orig->flags & DST_NOXFRM) ||
  1124. !xfrm_policy_count[XFRM_POLICY_OUT])
  1125. return 0;
  1126. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1127. dir, xfrm_policy_lookup);
  1128. if (IS_ERR(policy))
  1129. return PTR_ERR(policy);
  1130. }
  1131. if (!policy)
  1132. return 0;
  1133. family = dst_orig->ops->family;
  1134. policy->curlft.use_time = (unsigned long)xtime.tv_sec;
  1135. pols[0] = policy;
  1136. npols ++;
  1137. xfrm_nr += pols[0]->xfrm_nr;
  1138. switch (policy->action) {
  1139. case XFRM_POLICY_BLOCK:
  1140. /* Prohibit the flow */
  1141. err = -EPERM;
  1142. goto error;
  1143. case XFRM_POLICY_ALLOW:
  1144. #ifndef CONFIG_XFRM_SUB_POLICY
  1145. if (policy->xfrm_nr == 0) {
  1146. /* Flow passes not transformed. */
  1147. xfrm_pol_put(policy);
  1148. return 0;
  1149. }
  1150. #endif
  1151. /* Try to find matching bundle.
  1152. *
  1153. * LATER: help from flow cache. It is optional, this
  1154. * is required only for output policy.
  1155. */
  1156. dst = xfrm_find_bundle(fl, policy, family);
  1157. if (IS_ERR(dst)) {
  1158. err = PTR_ERR(dst);
  1159. goto error;
  1160. }
  1161. if (dst)
  1162. break;
  1163. #ifdef CONFIG_XFRM_SUB_POLICY
  1164. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1165. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1166. fl, family,
  1167. XFRM_POLICY_OUT);
  1168. if (pols[1]) {
  1169. if (IS_ERR(pols[1])) {
  1170. err = PTR_ERR(pols[1]);
  1171. goto error;
  1172. }
  1173. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1174. err = -EPERM;
  1175. goto error;
  1176. }
  1177. npols ++;
  1178. xfrm_nr += pols[1]->xfrm_nr;
  1179. }
  1180. }
  1181. /*
  1182. * Because neither flowi nor bundle information knows about
  1183. * transformation template size. On more than one policy usage
  1184. * we can realize whether all of them is bypass or not after
  1185. * they are searched. See above not-transformed bypass
  1186. * is surrounded by non-sub policy configuration, too.
  1187. */
  1188. if (xfrm_nr == 0) {
  1189. /* Flow passes not transformed. */
  1190. xfrm_pols_put(pols, npols);
  1191. return 0;
  1192. }
  1193. #endif
  1194. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1195. if (unlikely(nx<0)) {
  1196. err = nx;
  1197. if (err == -EAGAIN && flags) {
  1198. DECLARE_WAITQUEUE(wait, current);
  1199. add_wait_queue(&km_waitq, &wait);
  1200. set_current_state(TASK_INTERRUPTIBLE);
  1201. schedule();
  1202. set_current_state(TASK_RUNNING);
  1203. remove_wait_queue(&km_waitq, &wait);
  1204. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1205. if (nx == -EAGAIN && signal_pending(current)) {
  1206. err = -ERESTART;
  1207. goto error;
  1208. }
  1209. if (nx == -EAGAIN ||
  1210. genid != atomic_read(&flow_cache_genid)) {
  1211. xfrm_pols_put(pols, npols);
  1212. goto restart;
  1213. }
  1214. err = nx;
  1215. }
  1216. if (err < 0)
  1217. goto error;
  1218. }
  1219. if (nx == 0) {
  1220. /* Flow passes not transformed. */
  1221. xfrm_pols_put(pols, npols);
  1222. return 0;
  1223. }
  1224. dst = dst_orig;
  1225. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1226. if (unlikely(err)) {
  1227. int i;
  1228. for (i=0; i<nx; i++)
  1229. xfrm_state_put(xfrm[i]);
  1230. goto error;
  1231. }
  1232. for (pi = 0; pi < npols; pi++) {
  1233. read_lock_bh(&pols[pi]->lock);
  1234. pol_dead |= pols[pi]->dead;
  1235. read_unlock_bh(&pols[pi]->lock);
  1236. }
  1237. write_lock_bh(&policy->lock);
  1238. if (unlikely(pol_dead || stale_bundle(dst))) {
  1239. /* Wow! While we worked on resolving, this
  1240. * policy has gone. Retry. It is not paranoia,
  1241. * we just cannot enlist new bundle to dead object.
  1242. * We can't enlist stable bundles either.
  1243. */
  1244. write_unlock_bh(&policy->lock);
  1245. if (dst)
  1246. dst_free(dst);
  1247. err = -EHOSTUNREACH;
  1248. goto error;
  1249. }
  1250. dst->next = policy->bundles;
  1251. policy->bundles = dst;
  1252. dst_hold(dst);
  1253. write_unlock_bh(&policy->lock);
  1254. }
  1255. *dst_p = dst;
  1256. dst_release(dst_orig);
  1257. xfrm_pols_put(pols, npols);
  1258. return 0;
  1259. error:
  1260. dst_release(dst_orig);
  1261. xfrm_pols_put(pols, npols);
  1262. *dst_p = NULL;
  1263. return err;
  1264. }
  1265. EXPORT_SYMBOL(xfrm_lookup);
  1266. static inline int
  1267. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1268. {
  1269. struct xfrm_state *x;
  1270. int err;
  1271. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1272. return 0;
  1273. x = skb->sp->xvec[idx];
  1274. if (!x->type->reject)
  1275. return 0;
  1276. xfrm_state_hold(x);
  1277. err = x->type->reject(x, skb, fl);
  1278. xfrm_state_put(x);
  1279. return err;
  1280. }
  1281. /* When skb is transformed back to its "native" form, we have to
  1282. * check policy restrictions. At the moment we make this in maximally
  1283. * stupid way. Shame on me. :-) Of course, connected sockets must
  1284. * have policy cached at them.
  1285. */
  1286. static inline int
  1287. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1288. unsigned short family)
  1289. {
  1290. if (xfrm_state_kern(x))
  1291. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family);
  1292. return x->id.proto == tmpl->id.proto &&
  1293. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1294. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1295. x->props.mode == tmpl->mode &&
  1296. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1297. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1298. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1299. xfrm_state_addr_cmp(tmpl, x, family));
  1300. }
  1301. /*
  1302. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1303. * because of optional transport mode, or next index of the mathced secpath
  1304. * state with the template.
  1305. * -1 is returned when no matching template is found.
  1306. * Otherwise "-2 - errored_index" is returned.
  1307. */
  1308. static inline int
  1309. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1310. unsigned short family)
  1311. {
  1312. int idx = start;
  1313. if (tmpl->optional) {
  1314. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1315. return start;
  1316. } else
  1317. start = -1;
  1318. for (; idx < sp->len; idx++) {
  1319. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1320. return ++idx;
  1321. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1322. if (start == -1)
  1323. start = -2-idx;
  1324. break;
  1325. }
  1326. }
  1327. return start;
  1328. }
  1329. int
  1330. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1331. {
  1332. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1333. int err;
  1334. if (unlikely(afinfo == NULL))
  1335. return -EAFNOSUPPORT;
  1336. afinfo->decode_session(skb, fl);
  1337. err = security_xfrm_decode_session(skb, &fl->secid);
  1338. xfrm_policy_put_afinfo(afinfo);
  1339. return err;
  1340. }
  1341. EXPORT_SYMBOL(xfrm_decode_session);
  1342. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1343. {
  1344. for (; k < sp->len; k++) {
  1345. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1346. *idxp = k;
  1347. return 1;
  1348. }
  1349. }
  1350. return 0;
  1351. }
  1352. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1353. unsigned short family)
  1354. {
  1355. struct xfrm_policy *pol;
  1356. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1357. int npols = 0;
  1358. int xfrm_nr;
  1359. int pi;
  1360. struct flowi fl;
  1361. u8 fl_dir = policy_to_flow_dir(dir);
  1362. int xerr_idx = -1;
  1363. if (xfrm_decode_session(skb, &fl, family) < 0)
  1364. return 0;
  1365. nf_nat_decode_session(skb, &fl, family);
  1366. /* First, check used SA against their selectors. */
  1367. if (skb->sp) {
  1368. int i;
  1369. for (i=skb->sp->len-1; i>=0; i--) {
  1370. struct xfrm_state *x = skb->sp->xvec[i];
  1371. if (!xfrm_selector_match(&x->sel, &fl, family))
  1372. return 0;
  1373. }
  1374. }
  1375. pol = NULL;
  1376. if (sk && sk->sk_policy[dir]) {
  1377. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1378. if (IS_ERR(pol))
  1379. return 0;
  1380. }
  1381. if (!pol)
  1382. pol = flow_cache_lookup(&fl, family, fl_dir,
  1383. xfrm_policy_lookup);
  1384. if (IS_ERR(pol))
  1385. return 0;
  1386. if (!pol) {
  1387. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1388. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1389. return 0;
  1390. }
  1391. return 1;
  1392. }
  1393. pol->curlft.use_time = (unsigned long)xtime.tv_sec;
  1394. pols[0] = pol;
  1395. npols ++;
  1396. #ifdef CONFIG_XFRM_SUB_POLICY
  1397. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1398. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1399. &fl, family,
  1400. XFRM_POLICY_IN);
  1401. if (pols[1]) {
  1402. if (IS_ERR(pols[1]))
  1403. return 0;
  1404. pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec;
  1405. npols ++;
  1406. }
  1407. }
  1408. #endif
  1409. if (pol->action == XFRM_POLICY_ALLOW) {
  1410. struct sec_path *sp;
  1411. static struct sec_path dummy;
  1412. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1413. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1414. struct xfrm_tmpl **tpp = tp;
  1415. int ti = 0;
  1416. int i, k;
  1417. if ((sp = skb->sp) == NULL)
  1418. sp = &dummy;
  1419. for (pi = 0; pi < npols; pi++) {
  1420. if (pols[pi] != pol &&
  1421. pols[pi]->action != XFRM_POLICY_ALLOW)
  1422. goto reject;
  1423. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1424. goto reject_error;
  1425. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1426. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1427. }
  1428. xfrm_nr = ti;
  1429. if (npols > 1) {
  1430. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1431. tpp = stp;
  1432. }
  1433. /* For each tunnel xfrm, find the first matching tmpl.
  1434. * For each tmpl before that, find corresponding xfrm.
  1435. * Order is _important_. Later we will implement
  1436. * some barriers, but at the moment barriers
  1437. * are implied between each two transformations.
  1438. */
  1439. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1440. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1441. if (k < 0) {
  1442. if (k < -1)
  1443. /* "-2 - errored_index" returned */
  1444. xerr_idx = -(2+k);
  1445. goto reject;
  1446. }
  1447. }
  1448. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1449. goto reject;
  1450. xfrm_pols_put(pols, npols);
  1451. return 1;
  1452. }
  1453. reject:
  1454. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1455. reject_error:
  1456. xfrm_pols_put(pols, npols);
  1457. return 0;
  1458. }
  1459. EXPORT_SYMBOL(__xfrm_policy_check);
  1460. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1461. {
  1462. struct flowi fl;
  1463. if (xfrm_decode_session(skb, &fl, family) < 0)
  1464. return 0;
  1465. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1466. }
  1467. EXPORT_SYMBOL(__xfrm_route_forward);
  1468. /* Optimize later using cookies and generation ids. */
  1469. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1470. {
  1471. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1472. * to "-1" to force all XFRM destinations to get validated by
  1473. * dst_ops->check on every use. We do this because when a
  1474. * normal route referenced by an XFRM dst is obsoleted we do
  1475. * not go looking around for all parent referencing XFRM dsts
  1476. * so that we can invalidate them. It is just too much work.
  1477. * Instead we make the checks here on every use. For example:
  1478. *
  1479. * XFRM dst A --> IPv4 dst X
  1480. *
  1481. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1482. * in this example). If X is marked obsolete, "A" will not
  1483. * notice. That's what we are validating here via the
  1484. * stale_bundle() check.
  1485. *
  1486. * When a policy's bundle is pruned, we dst_free() the XFRM
  1487. * dst which causes it's ->obsolete field to be set to a
  1488. * positive non-zero integer. If an XFRM dst has been pruned
  1489. * like this, we want to force a new route lookup.
  1490. */
  1491. if (dst->obsolete < 0 && !stale_bundle(dst))
  1492. return dst;
  1493. return NULL;
  1494. }
  1495. static int stale_bundle(struct dst_entry *dst)
  1496. {
  1497. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1498. }
  1499. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1500. {
  1501. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1502. dst->dev = &loopback_dev;
  1503. dev_hold(&loopback_dev);
  1504. dev_put(dev);
  1505. }
  1506. }
  1507. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1508. static void xfrm_link_failure(struct sk_buff *skb)
  1509. {
  1510. /* Impossible. Such dst must be popped before reaches point of failure. */
  1511. return;
  1512. }
  1513. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1514. {
  1515. if (dst) {
  1516. if (dst->obsolete) {
  1517. dst_release(dst);
  1518. dst = NULL;
  1519. }
  1520. }
  1521. return dst;
  1522. }
  1523. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1524. {
  1525. struct dst_entry *dst, **dstp;
  1526. write_lock(&pol->lock);
  1527. dstp = &pol->bundles;
  1528. while ((dst=*dstp) != NULL) {
  1529. if (func(dst)) {
  1530. *dstp = dst->next;
  1531. dst->next = *gc_list_p;
  1532. *gc_list_p = dst;
  1533. } else {
  1534. dstp = &dst->next;
  1535. }
  1536. }
  1537. write_unlock(&pol->lock);
  1538. }
  1539. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1540. {
  1541. struct dst_entry *gc_list = NULL;
  1542. int dir;
  1543. read_lock_bh(&xfrm_policy_lock);
  1544. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1545. struct xfrm_policy *pol;
  1546. struct hlist_node *entry;
  1547. struct hlist_head *table;
  1548. int i;
  1549. hlist_for_each_entry(pol, entry,
  1550. &xfrm_policy_inexact[dir], bydst)
  1551. prune_one_bundle(pol, func, &gc_list);
  1552. table = xfrm_policy_bydst[dir].table;
  1553. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1554. hlist_for_each_entry(pol, entry, table + i, bydst)
  1555. prune_one_bundle(pol, func, &gc_list);
  1556. }
  1557. }
  1558. read_unlock_bh(&xfrm_policy_lock);
  1559. while (gc_list) {
  1560. struct dst_entry *dst = gc_list;
  1561. gc_list = dst->next;
  1562. dst_free(dst);
  1563. }
  1564. }
  1565. static int unused_bundle(struct dst_entry *dst)
  1566. {
  1567. return !atomic_read(&dst->__refcnt);
  1568. }
  1569. static void __xfrm_garbage_collect(void)
  1570. {
  1571. xfrm_prune_bundles(unused_bundle);
  1572. }
  1573. static int xfrm_flush_bundles(void)
  1574. {
  1575. xfrm_prune_bundles(stale_bundle);
  1576. return 0;
  1577. }
  1578. void xfrm_init_pmtu(struct dst_entry *dst)
  1579. {
  1580. do {
  1581. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1582. u32 pmtu, route_mtu_cached;
  1583. pmtu = dst_mtu(dst->child);
  1584. xdst->child_mtu_cached = pmtu;
  1585. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1586. route_mtu_cached = dst_mtu(xdst->route);
  1587. xdst->route_mtu_cached = route_mtu_cached;
  1588. if (pmtu > route_mtu_cached)
  1589. pmtu = route_mtu_cached;
  1590. dst->metrics[RTAX_MTU-1] = pmtu;
  1591. } while ((dst = dst->next));
  1592. }
  1593. EXPORT_SYMBOL(xfrm_init_pmtu);
  1594. /* Check that the bundle accepts the flow and its components are
  1595. * still valid.
  1596. */
  1597. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1598. struct flowi *fl, int family, int strict)
  1599. {
  1600. struct dst_entry *dst = &first->u.dst;
  1601. struct xfrm_dst *last;
  1602. u32 mtu;
  1603. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1604. (dst->dev && !netif_running(dst->dev)))
  1605. return 0;
  1606. last = NULL;
  1607. do {
  1608. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1609. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1610. return 0;
  1611. if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm, pol))
  1612. return 0;
  1613. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1614. return 0;
  1615. if (xdst->genid != dst->xfrm->genid)
  1616. return 0;
  1617. if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
  1618. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1619. return 0;
  1620. mtu = dst_mtu(dst->child);
  1621. if (xdst->child_mtu_cached != mtu) {
  1622. last = xdst;
  1623. xdst->child_mtu_cached = mtu;
  1624. }
  1625. if (!dst_check(xdst->route, xdst->route_cookie))
  1626. return 0;
  1627. mtu = dst_mtu(xdst->route);
  1628. if (xdst->route_mtu_cached != mtu) {
  1629. last = xdst;
  1630. xdst->route_mtu_cached = mtu;
  1631. }
  1632. dst = dst->child;
  1633. } while (dst->xfrm);
  1634. if (likely(!last))
  1635. return 1;
  1636. mtu = last->child_mtu_cached;
  1637. for (;;) {
  1638. dst = &last->u.dst;
  1639. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1640. if (mtu > last->route_mtu_cached)
  1641. mtu = last->route_mtu_cached;
  1642. dst->metrics[RTAX_MTU-1] = mtu;
  1643. if (last == first)
  1644. break;
  1645. last = last->u.next;
  1646. last->child_mtu_cached = mtu;
  1647. }
  1648. return 1;
  1649. }
  1650. EXPORT_SYMBOL(xfrm_bundle_ok);
  1651. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1652. {
  1653. int err = 0;
  1654. if (unlikely(afinfo == NULL))
  1655. return -EINVAL;
  1656. if (unlikely(afinfo->family >= NPROTO))
  1657. return -EAFNOSUPPORT;
  1658. write_lock_bh(&xfrm_policy_afinfo_lock);
  1659. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1660. err = -ENOBUFS;
  1661. else {
  1662. struct dst_ops *dst_ops = afinfo->dst_ops;
  1663. if (likely(dst_ops->kmem_cachep == NULL))
  1664. dst_ops->kmem_cachep = xfrm_dst_cache;
  1665. if (likely(dst_ops->check == NULL))
  1666. dst_ops->check = xfrm_dst_check;
  1667. if (likely(dst_ops->negative_advice == NULL))
  1668. dst_ops->negative_advice = xfrm_negative_advice;
  1669. if (likely(dst_ops->link_failure == NULL))
  1670. dst_ops->link_failure = xfrm_link_failure;
  1671. if (likely(afinfo->garbage_collect == NULL))
  1672. afinfo->garbage_collect = __xfrm_garbage_collect;
  1673. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1674. }
  1675. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1676. return err;
  1677. }
  1678. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1679. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1680. {
  1681. int err = 0;
  1682. if (unlikely(afinfo == NULL))
  1683. return -EINVAL;
  1684. if (unlikely(afinfo->family >= NPROTO))
  1685. return -EAFNOSUPPORT;
  1686. write_lock_bh(&xfrm_policy_afinfo_lock);
  1687. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1688. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1689. err = -EINVAL;
  1690. else {
  1691. struct dst_ops *dst_ops = afinfo->dst_ops;
  1692. xfrm_policy_afinfo[afinfo->family] = NULL;
  1693. dst_ops->kmem_cachep = NULL;
  1694. dst_ops->check = NULL;
  1695. dst_ops->negative_advice = NULL;
  1696. dst_ops->link_failure = NULL;
  1697. afinfo->garbage_collect = NULL;
  1698. }
  1699. }
  1700. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1701. return err;
  1702. }
  1703. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1704. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1705. {
  1706. struct xfrm_policy_afinfo *afinfo;
  1707. if (unlikely(family >= NPROTO))
  1708. return NULL;
  1709. read_lock(&xfrm_policy_afinfo_lock);
  1710. afinfo = xfrm_policy_afinfo[family];
  1711. if (unlikely(!afinfo))
  1712. read_unlock(&xfrm_policy_afinfo_lock);
  1713. return afinfo;
  1714. }
  1715. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1716. {
  1717. read_unlock(&xfrm_policy_afinfo_lock);
  1718. }
  1719. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
  1720. {
  1721. struct xfrm_policy_afinfo *afinfo;
  1722. if (unlikely(family >= NPROTO))
  1723. return NULL;
  1724. write_lock_bh(&xfrm_policy_afinfo_lock);
  1725. afinfo = xfrm_policy_afinfo[family];
  1726. if (unlikely(!afinfo))
  1727. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1728. return afinfo;
  1729. }
  1730. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
  1731. {
  1732. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1733. }
  1734. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1735. {
  1736. switch (event) {
  1737. case NETDEV_DOWN:
  1738. xfrm_flush_bundles();
  1739. }
  1740. return NOTIFY_DONE;
  1741. }
  1742. static struct notifier_block xfrm_dev_notifier = {
  1743. xfrm_dev_event,
  1744. NULL,
  1745. 0
  1746. };
  1747. static void __init xfrm_policy_init(void)
  1748. {
  1749. unsigned int hmask, sz;
  1750. int dir;
  1751. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1752. sizeof(struct xfrm_dst),
  1753. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1754. NULL, NULL);
  1755. hmask = 8 - 1;
  1756. sz = (hmask+1) * sizeof(struct hlist_head);
  1757. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1758. xfrm_idx_hmask = hmask;
  1759. if (!xfrm_policy_byidx)
  1760. panic("XFRM: failed to allocate byidx hash\n");
  1761. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1762. struct xfrm_policy_hash *htab;
  1763. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1764. htab = &xfrm_policy_bydst[dir];
  1765. htab->table = xfrm_hash_alloc(sz);
  1766. htab->hmask = hmask;
  1767. if (!htab->table)
  1768. panic("XFRM: failed to allocate bydst hash\n");
  1769. }
  1770. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL);
  1771. register_netdevice_notifier(&xfrm_dev_notifier);
  1772. }
  1773. void __init xfrm_init(void)
  1774. {
  1775. xfrm_state_init();
  1776. xfrm_policy_init();
  1777. xfrm_input_init();
  1778. }