xfrm_policy.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  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/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/kmod.h>
  18. #include <linux/list.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/notifier.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/netfilter.h>
  24. #include <linux/module.h>
  25. #include <linux/cache.h>
  26. #include <net/xfrm.h>
  27. #include <net/ip.h>
  28. #include "xfrm_hash.h"
  29. int sysctl_xfrm_larval_drop __read_mostly;
  30. DEFINE_MUTEX(xfrm_cfg_mutex);
  31. EXPORT_SYMBOL(xfrm_cfg_mutex);
  32. static DEFINE_RWLOCK(xfrm_policy_lock);
  33. unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
  34. EXPORT_SYMBOL(xfrm_policy_count);
  35. static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
  36. static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
  37. static struct kmem_cache *xfrm_dst_cache __read_mostly;
  38. static struct work_struct xfrm_policy_gc_work;
  39. static HLIST_HEAD(xfrm_policy_gc_list);
  40. static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
  41. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
  42. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
  43. static inline int
  44. __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  45. {
  46. return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
  47. addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
  48. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  49. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  50. (fl->proto == sel->proto || !sel->proto) &&
  51. (fl->oif == sel->ifindex || !sel->ifindex);
  52. }
  53. static inline int
  54. __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  55. {
  56. return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
  57. addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
  58. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  59. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  60. (fl->proto == sel->proto || !sel->proto) &&
  61. (fl->oif == sel->ifindex || !sel->ifindex);
  62. }
  63. int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
  64. unsigned short family)
  65. {
  66. switch (family) {
  67. case AF_INET:
  68. return __xfrm4_selector_match(sel, fl);
  69. case AF_INET6:
  70. return __xfrm6_selector_match(sel, fl);
  71. }
  72. return 0;
  73. }
  74. struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos)
  75. {
  76. xfrm_address_t *saddr = &x->props.saddr;
  77. xfrm_address_t *daddr = &x->id.daddr;
  78. struct xfrm_policy_afinfo *afinfo;
  79. struct dst_entry *dst;
  80. if (x->type->flags & XFRM_TYPE_LOCAL_COADDR)
  81. saddr = x->coaddr;
  82. if (x->type->flags & XFRM_TYPE_REMOTE_COADDR)
  83. daddr = x->coaddr;
  84. afinfo = xfrm_policy_get_afinfo(x->props.family);
  85. if (unlikely(afinfo == NULL))
  86. return ERR_PTR(-EAFNOSUPPORT);
  87. dst = afinfo->dst_lookup(tos, saddr, daddr);
  88. xfrm_policy_put_afinfo(afinfo);
  89. return dst;
  90. }
  91. EXPORT_SYMBOL(xfrm_dst_lookup);
  92. static inline unsigned long make_jiffies(long secs)
  93. {
  94. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  95. return MAX_SCHEDULE_TIMEOUT-1;
  96. else
  97. return secs*HZ;
  98. }
  99. static void xfrm_policy_timer(unsigned long data)
  100. {
  101. struct xfrm_policy *xp = (struct xfrm_policy*)data;
  102. unsigned long now = get_seconds();
  103. long next = LONG_MAX;
  104. int warn = 0;
  105. int dir;
  106. read_lock(&xp->lock);
  107. if (xp->dead)
  108. goto out;
  109. dir = xfrm_policy_id2dir(xp->index);
  110. if (xp->lft.hard_add_expires_seconds) {
  111. long tmo = xp->lft.hard_add_expires_seconds +
  112. xp->curlft.add_time - now;
  113. if (tmo <= 0)
  114. goto expired;
  115. if (tmo < next)
  116. next = tmo;
  117. }
  118. if (xp->lft.hard_use_expires_seconds) {
  119. long tmo = xp->lft.hard_use_expires_seconds +
  120. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  121. if (tmo <= 0)
  122. goto expired;
  123. if (tmo < next)
  124. next = tmo;
  125. }
  126. if (xp->lft.soft_add_expires_seconds) {
  127. long tmo = xp->lft.soft_add_expires_seconds +
  128. xp->curlft.add_time - now;
  129. if (tmo <= 0) {
  130. warn = 1;
  131. tmo = XFRM_KM_TIMEOUT;
  132. }
  133. if (tmo < next)
  134. next = tmo;
  135. }
  136. if (xp->lft.soft_use_expires_seconds) {
  137. long tmo = xp->lft.soft_use_expires_seconds +
  138. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  139. if (tmo <= 0) {
  140. warn = 1;
  141. tmo = XFRM_KM_TIMEOUT;
  142. }
  143. if (tmo < next)
  144. next = tmo;
  145. }
  146. if (warn)
  147. km_policy_expired(xp, dir, 0, 0);
  148. if (next != LONG_MAX &&
  149. !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
  150. xfrm_pol_hold(xp);
  151. out:
  152. read_unlock(&xp->lock);
  153. xfrm_pol_put(xp);
  154. return;
  155. expired:
  156. read_unlock(&xp->lock);
  157. if (!xfrm_policy_delete(xp, dir))
  158. km_policy_expired(xp, dir, 1, 0);
  159. xfrm_pol_put(xp);
  160. }
  161. /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
  162. * SPD calls.
  163. */
  164. struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
  165. {
  166. struct xfrm_policy *policy;
  167. policy = kzalloc(sizeof(struct xfrm_policy), gfp);
  168. if (policy) {
  169. INIT_HLIST_NODE(&policy->bydst);
  170. INIT_HLIST_NODE(&policy->byidx);
  171. rwlock_init(&policy->lock);
  172. atomic_set(&policy->refcnt, 1);
  173. setup_timer(&policy->timer, xfrm_policy_timer,
  174. (unsigned long)policy);
  175. }
  176. return policy;
  177. }
  178. EXPORT_SYMBOL(xfrm_policy_alloc);
  179. /* Destroy xfrm_policy: descendant resources must be released to this moment. */
  180. void __xfrm_policy_destroy(struct xfrm_policy *policy)
  181. {
  182. BUG_ON(!policy->dead);
  183. BUG_ON(policy->bundles);
  184. if (del_timer(&policy->timer))
  185. BUG();
  186. security_xfrm_policy_free(policy);
  187. kfree(policy);
  188. }
  189. EXPORT_SYMBOL(__xfrm_policy_destroy);
  190. static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
  191. {
  192. struct dst_entry *dst;
  193. while ((dst = policy->bundles) != NULL) {
  194. policy->bundles = dst->next;
  195. dst_free(dst);
  196. }
  197. if (del_timer(&policy->timer))
  198. atomic_dec(&policy->refcnt);
  199. if (atomic_read(&policy->refcnt) > 1)
  200. flow_cache_flush();
  201. xfrm_pol_put(policy);
  202. }
  203. static void xfrm_policy_gc_task(struct work_struct *work)
  204. {
  205. struct xfrm_policy *policy;
  206. struct hlist_node *entry, *tmp;
  207. struct hlist_head gc_list;
  208. spin_lock_bh(&xfrm_policy_gc_lock);
  209. gc_list.first = xfrm_policy_gc_list.first;
  210. INIT_HLIST_HEAD(&xfrm_policy_gc_list);
  211. spin_unlock_bh(&xfrm_policy_gc_lock);
  212. hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
  213. xfrm_policy_gc_kill(policy);
  214. }
  215. /* Rule must be locked. Release descentant resources, announce
  216. * entry dead. The rule must be unlinked from lists to the moment.
  217. */
  218. static void xfrm_policy_kill(struct xfrm_policy *policy)
  219. {
  220. int dead;
  221. write_lock_bh(&policy->lock);
  222. dead = policy->dead;
  223. policy->dead = 1;
  224. write_unlock_bh(&policy->lock);
  225. if (unlikely(dead)) {
  226. WARN_ON(1);
  227. return;
  228. }
  229. spin_lock(&xfrm_policy_gc_lock);
  230. hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
  231. spin_unlock(&xfrm_policy_gc_lock);
  232. schedule_work(&xfrm_policy_gc_work);
  233. }
  234. struct xfrm_policy_hash {
  235. struct hlist_head *table;
  236. unsigned int hmask;
  237. };
  238. static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
  239. static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
  240. static struct hlist_head *xfrm_policy_byidx __read_mostly;
  241. static unsigned int xfrm_idx_hmask __read_mostly;
  242. static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
  243. static inline unsigned int idx_hash(u32 index)
  244. {
  245. return __idx_hash(index, xfrm_idx_hmask);
  246. }
  247. static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
  248. {
  249. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  250. unsigned int hash = __sel_hash(sel, family, hmask);
  251. return (hash == hmask + 1 ?
  252. &xfrm_policy_inexact[dir] :
  253. xfrm_policy_bydst[dir].table + hash);
  254. }
  255. static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
  256. {
  257. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  258. unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
  259. return xfrm_policy_bydst[dir].table + hash;
  260. }
  261. static void xfrm_dst_hash_transfer(struct hlist_head *list,
  262. struct hlist_head *ndsttable,
  263. unsigned int nhashmask)
  264. {
  265. struct hlist_node *entry, *tmp;
  266. struct xfrm_policy *pol;
  267. hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
  268. unsigned int h;
  269. h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
  270. pol->family, nhashmask);
  271. hlist_add_head(&pol->bydst, ndsttable+h);
  272. }
  273. }
  274. static void xfrm_idx_hash_transfer(struct hlist_head *list,
  275. struct hlist_head *nidxtable,
  276. unsigned int nhashmask)
  277. {
  278. struct hlist_node *entry, *tmp;
  279. struct xfrm_policy *pol;
  280. hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
  281. unsigned int h;
  282. h = __idx_hash(pol->index, nhashmask);
  283. hlist_add_head(&pol->byidx, nidxtable+h);
  284. }
  285. }
  286. static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
  287. {
  288. return ((old_hmask + 1) << 1) - 1;
  289. }
  290. static void xfrm_bydst_resize(int dir)
  291. {
  292. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  293. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  294. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  295. struct hlist_head *odst = xfrm_policy_bydst[dir].table;
  296. struct hlist_head *ndst = xfrm_hash_alloc(nsize);
  297. int i;
  298. if (!ndst)
  299. return;
  300. write_lock_bh(&xfrm_policy_lock);
  301. for (i = hmask; i >= 0; i--)
  302. xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
  303. xfrm_policy_bydst[dir].table = ndst;
  304. xfrm_policy_bydst[dir].hmask = nhashmask;
  305. write_unlock_bh(&xfrm_policy_lock);
  306. xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
  307. }
  308. static void xfrm_byidx_resize(int total)
  309. {
  310. unsigned int hmask = xfrm_idx_hmask;
  311. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  312. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  313. struct hlist_head *oidx = xfrm_policy_byidx;
  314. struct hlist_head *nidx = xfrm_hash_alloc(nsize);
  315. int i;
  316. if (!nidx)
  317. return;
  318. write_lock_bh(&xfrm_policy_lock);
  319. for (i = hmask; i >= 0; i--)
  320. xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
  321. xfrm_policy_byidx = nidx;
  322. xfrm_idx_hmask = nhashmask;
  323. write_unlock_bh(&xfrm_policy_lock);
  324. xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
  325. }
  326. static inline int xfrm_bydst_should_resize(int dir, int *total)
  327. {
  328. unsigned int cnt = xfrm_policy_count[dir];
  329. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  330. if (total)
  331. *total += cnt;
  332. if ((hmask + 1) < xfrm_policy_hashmax &&
  333. cnt > hmask)
  334. return 1;
  335. return 0;
  336. }
  337. static inline int xfrm_byidx_should_resize(int total)
  338. {
  339. unsigned int hmask = xfrm_idx_hmask;
  340. if ((hmask + 1) < xfrm_policy_hashmax &&
  341. total > hmask)
  342. return 1;
  343. return 0;
  344. }
  345. void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
  346. {
  347. read_lock_bh(&xfrm_policy_lock);
  348. si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
  349. si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
  350. si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
  351. si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
  352. si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
  353. si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
  354. si->spdhcnt = xfrm_idx_hmask;
  355. si->spdhmcnt = xfrm_policy_hashmax;
  356. read_unlock_bh(&xfrm_policy_lock);
  357. }
  358. EXPORT_SYMBOL(xfrm_spd_getinfo);
  359. static DEFINE_MUTEX(hash_resize_mutex);
  360. static void xfrm_hash_resize(struct work_struct *__unused)
  361. {
  362. int dir, total;
  363. mutex_lock(&hash_resize_mutex);
  364. total = 0;
  365. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  366. if (xfrm_bydst_should_resize(dir, &total))
  367. xfrm_bydst_resize(dir);
  368. }
  369. if (xfrm_byidx_should_resize(total))
  370. xfrm_byidx_resize(total);
  371. mutex_unlock(&hash_resize_mutex);
  372. }
  373. static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
  374. /* Generate new index... KAME seems to generate them ordered by cost
  375. * of an absolute inpredictability of ordering of rules. This will not pass. */
  376. static u32 xfrm_gen_index(u8 type, int dir)
  377. {
  378. static u32 idx_generator;
  379. for (;;) {
  380. struct hlist_node *entry;
  381. struct hlist_head *list;
  382. struct xfrm_policy *p;
  383. u32 idx;
  384. int found;
  385. idx = (idx_generator | dir);
  386. idx_generator += 8;
  387. if (idx == 0)
  388. idx = 8;
  389. list = xfrm_policy_byidx + idx_hash(idx);
  390. found = 0;
  391. hlist_for_each_entry(p, entry, list, byidx) {
  392. if (p->index == idx) {
  393. found = 1;
  394. break;
  395. }
  396. }
  397. if (!found)
  398. return idx;
  399. }
  400. }
  401. static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
  402. {
  403. u32 *p1 = (u32 *) s1;
  404. u32 *p2 = (u32 *) s2;
  405. int len = sizeof(struct xfrm_selector) / sizeof(u32);
  406. int i;
  407. for (i = 0; i < len; i++) {
  408. if (p1[i] != p2[i])
  409. return 1;
  410. }
  411. return 0;
  412. }
  413. int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
  414. {
  415. struct xfrm_policy *pol;
  416. struct xfrm_policy *delpol;
  417. struct hlist_head *chain;
  418. struct hlist_node *entry, *newpos;
  419. struct dst_entry *gc_list;
  420. write_lock_bh(&xfrm_policy_lock);
  421. chain = policy_hash_bysel(&policy->selector, policy->family, dir);
  422. delpol = NULL;
  423. newpos = NULL;
  424. hlist_for_each_entry(pol, entry, chain, bydst) {
  425. if (pol->type == policy->type &&
  426. !selector_cmp(&pol->selector, &policy->selector) &&
  427. xfrm_sec_ctx_match(pol->security, policy->security) &&
  428. !WARN_ON(delpol)) {
  429. if (excl) {
  430. write_unlock_bh(&xfrm_policy_lock);
  431. return -EEXIST;
  432. }
  433. delpol = pol;
  434. if (policy->priority > pol->priority)
  435. continue;
  436. } else if (policy->priority >= pol->priority) {
  437. newpos = &pol->bydst;
  438. continue;
  439. }
  440. if (delpol)
  441. break;
  442. }
  443. if (newpos)
  444. hlist_add_after(newpos, &policy->bydst);
  445. else
  446. hlist_add_head(&policy->bydst, chain);
  447. xfrm_pol_hold(policy);
  448. xfrm_policy_count[dir]++;
  449. atomic_inc(&flow_cache_genid);
  450. if (delpol) {
  451. hlist_del(&delpol->bydst);
  452. hlist_del(&delpol->byidx);
  453. xfrm_policy_count[dir]--;
  454. }
  455. policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
  456. hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
  457. policy->curlft.add_time = get_seconds();
  458. policy->curlft.use_time = 0;
  459. if (!mod_timer(&policy->timer, jiffies + HZ))
  460. xfrm_pol_hold(policy);
  461. write_unlock_bh(&xfrm_policy_lock);
  462. if (delpol)
  463. xfrm_policy_kill(delpol);
  464. else if (xfrm_bydst_should_resize(dir, NULL))
  465. schedule_work(&xfrm_hash_work);
  466. read_lock_bh(&xfrm_policy_lock);
  467. gc_list = NULL;
  468. entry = &policy->bydst;
  469. hlist_for_each_entry_continue(policy, entry, bydst) {
  470. struct dst_entry *dst;
  471. write_lock(&policy->lock);
  472. dst = policy->bundles;
  473. if (dst) {
  474. struct dst_entry *tail = dst;
  475. while (tail->next)
  476. tail = tail->next;
  477. tail->next = gc_list;
  478. gc_list = dst;
  479. policy->bundles = NULL;
  480. }
  481. write_unlock(&policy->lock);
  482. }
  483. read_unlock_bh(&xfrm_policy_lock);
  484. while (gc_list) {
  485. struct dst_entry *dst = gc_list;
  486. gc_list = dst->next;
  487. dst_free(dst);
  488. }
  489. return 0;
  490. }
  491. EXPORT_SYMBOL(xfrm_policy_insert);
  492. struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
  493. struct xfrm_selector *sel,
  494. struct xfrm_sec_ctx *ctx, int delete,
  495. int *err)
  496. {
  497. struct xfrm_policy *pol, *ret;
  498. struct hlist_head *chain;
  499. struct hlist_node *entry;
  500. *err = 0;
  501. write_lock_bh(&xfrm_policy_lock);
  502. chain = policy_hash_bysel(sel, sel->family, dir);
  503. ret = NULL;
  504. hlist_for_each_entry(pol, entry, chain, bydst) {
  505. if (pol->type == type &&
  506. !selector_cmp(sel, &pol->selector) &&
  507. xfrm_sec_ctx_match(ctx, pol->security)) {
  508. xfrm_pol_hold(pol);
  509. if (delete) {
  510. *err = security_xfrm_policy_delete(pol);
  511. if (*err) {
  512. write_unlock_bh(&xfrm_policy_lock);
  513. return pol;
  514. }
  515. hlist_del(&pol->bydst);
  516. hlist_del(&pol->byidx);
  517. xfrm_policy_count[dir]--;
  518. }
  519. ret = pol;
  520. break;
  521. }
  522. }
  523. write_unlock_bh(&xfrm_policy_lock);
  524. if (ret && delete) {
  525. atomic_inc(&flow_cache_genid);
  526. xfrm_policy_kill(ret);
  527. }
  528. return ret;
  529. }
  530. EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
  531. struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
  532. int *err)
  533. {
  534. struct xfrm_policy *pol, *ret;
  535. struct hlist_head *chain;
  536. struct hlist_node *entry;
  537. *err = -ENOENT;
  538. if (xfrm_policy_id2dir(id) != dir)
  539. return NULL;
  540. *err = 0;
  541. write_lock_bh(&xfrm_policy_lock);
  542. chain = xfrm_policy_byidx + idx_hash(id);
  543. ret = NULL;
  544. hlist_for_each_entry(pol, entry, chain, byidx) {
  545. if (pol->type == type && pol->index == id) {
  546. xfrm_pol_hold(pol);
  547. if (delete) {
  548. *err = security_xfrm_policy_delete(pol);
  549. if (*err) {
  550. write_unlock_bh(&xfrm_policy_lock);
  551. return pol;
  552. }
  553. hlist_del(&pol->bydst);
  554. hlist_del(&pol->byidx);
  555. xfrm_policy_count[dir]--;
  556. }
  557. ret = pol;
  558. break;
  559. }
  560. }
  561. write_unlock_bh(&xfrm_policy_lock);
  562. if (ret && delete) {
  563. atomic_inc(&flow_cache_genid);
  564. xfrm_policy_kill(ret);
  565. }
  566. return ret;
  567. }
  568. EXPORT_SYMBOL(xfrm_policy_byid);
  569. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  570. static inline int
  571. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  572. {
  573. int dir, err = 0;
  574. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  575. struct xfrm_policy *pol;
  576. struct hlist_node *entry;
  577. int i;
  578. hlist_for_each_entry(pol, entry,
  579. &xfrm_policy_inexact[dir], bydst) {
  580. if (pol->type != type)
  581. continue;
  582. err = security_xfrm_policy_delete(pol);
  583. if (err) {
  584. xfrm_audit_policy_delete(pol, 0,
  585. audit_info->loginuid,
  586. audit_info->secid);
  587. return err;
  588. }
  589. }
  590. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  591. hlist_for_each_entry(pol, entry,
  592. xfrm_policy_bydst[dir].table + i,
  593. bydst) {
  594. if (pol->type != type)
  595. continue;
  596. err = security_xfrm_policy_delete(pol);
  597. if (err) {
  598. xfrm_audit_policy_delete(pol, 0,
  599. audit_info->loginuid,
  600. audit_info->secid);
  601. return err;
  602. }
  603. }
  604. }
  605. }
  606. return err;
  607. }
  608. #else
  609. static inline int
  610. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  611. {
  612. return 0;
  613. }
  614. #endif
  615. int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
  616. {
  617. int dir, err = 0;
  618. write_lock_bh(&xfrm_policy_lock);
  619. err = xfrm_policy_flush_secctx_check(type, audit_info);
  620. if (err)
  621. goto out;
  622. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  623. struct xfrm_policy *pol;
  624. struct hlist_node *entry;
  625. int i, killed;
  626. killed = 0;
  627. again1:
  628. hlist_for_each_entry(pol, entry,
  629. &xfrm_policy_inexact[dir], bydst) {
  630. if (pol->type != type)
  631. continue;
  632. hlist_del(&pol->bydst);
  633. hlist_del(&pol->byidx);
  634. write_unlock_bh(&xfrm_policy_lock);
  635. xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
  636. audit_info->secid);
  637. xfrm_policy_kill(pol);
  638. killed++;
  639. write_lock_bh(&xfrm_policy_lock);
  640. goto again1;
  641. }
  642. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  643. again2:
  644. hlist_for_each_entry(pol, entry,
  645. xfrm_policy_bydst[dir].table + i,
  646. bydst) {
  647. if (pol->type != type)
  648. continue;
  649. hlist_del(&pol->bydst);
  650. hlist_del(&pol->byidx);
  651. write_unlock_bh(&xfrm_policy_lock);
  652. xfrm_audit_policy_delete(pol, 1,
  653. audit_info->loginuid,
  654. audit_info->secid);
  655. xfrm_policy_kill(pol);
  656. killed++;
  657. write_lock_bh(&xfrm_policy_lock);
  658. goto again2;
  659. }
  660. }
  661. xfrm_policy_count[dir] -= killed;
  662. }
  663. atomic_inc(&flow_cache_genid);
  664. out:
  665. write_unlock_bh(&xfrm_policy_lock);
  666. return err;
  667. }
  668. EXPORT_SYMBOL(xfrm_policy_flush);
  669. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  670. void *data)
  671. {
  672. struct xfrm_policy *pol, *last = NULL;
  673. struct hlist_node *entry;
  674. int dir, last_dir = 0, count, error;
  675. read_lock_bh(&xfrm_policy_lock);
  676. count = 0;
  677. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  678. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  679. int i;
  680. hlist_for_each_entry(pol, entry,
  681. &xfrm_policy_inexact[dir], bydst) {
  682. if (pol->type != type)
  683. continue;
  684. if (last) {
  685. error = func(last, last_dir % XFRM_POLICY_MAX,
  686. count, data);
  687. if (error)
  688. goto out;
  689. }
  690. last = pol;
  691. last_dir = dir;
  692. count++;
  693. }
  694. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  695. hlist_for_each_entry(pol, entry, table + i, bydst) {
  696. if (pol->type != type)
  697. continue;
  698. if (last) {
  699. error = func(last, last_dir % XFRM_POLICY_MAX,
  700. count, data);
  701. if (error)
  702. goto out;
  703. }
  704. last = pol;
  705. last_dir = dir;
  706. count++;
  707. }
  708. }
  709. }
  710. if (count == 0) {
  711. error = -ENOENT;
  712. goto out;
  713. }
  714. error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
  715. out:
  716. read_unlock_bh(&xfrm_policy_lock);
  717. return error;
  718. }
  719. EXPORT_SYMBOL(xfrm_policy_walk);
  720. /*
  721. * Find policy to apply to this flow.
  722. *
  723. * Returns 0 if policy found, else an -errno.
  724. */
  725. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  726. u8 type, u16 family, int dir)
  727. {
  728. struct xfrm_selector *sel = &pol->selector;
  729. int match, ret = -ESRCH;
  730. if (pol->family != family ||
  731. pol->type != type)
  732. return ret;
  733. match = xfrm_selector_match(sel, fl, family);
  734. if (match)
  735. ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
  736. return ret;
  737. }
  738. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  739. u16 family, u8 dir)
  740. {
  741. int err;
  742. struct xfrm_policy *pol, *ret;
  743. xfrm_address_t *daddr, *saddr;
  744. struct hlist_node *entry;
  745. struct hlist_head *chain;
  746. u32 priority = ~0U;
  747. daddr = xfrm_flowi_daddr(fl, family);
  748. saddr = xfrm_flowi_saddr(fl, family);
  749. if (unlikely(!daddr || !saddr))
  750. return NULL;
  751. read_lock_bh(&xfrm_policy_lock);
  752. chain = policy_hash_direct(daddr, saddr, family, dir);
  753. ret = NULL;
  754. hlist_for_each_entry(pol, entry, chain, bydst) {
  755. err = xfrm_policy_match(pol, fl, type, family, dir);
  756. if (err) {
  757. if (err == -ESRCH)
  758. continue;
  759. else {
  760. ret = ERR_PTR(err);
  761. goto fail;
  762. }
  763. } else {
  764. ret = pol;
  765. priority = ret->priority;
  766. break;
  767. }
  768. }
  769. chain = &xfrm_policy_inexact[dir];
  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 if (pol->priority < priority) {
  780. ret = pol;
  781. break;
  782. }
  783. }
  784. if (ret)
  785. xfrm_pol_hold(ret);
  786. fail:
  787. read_unlock_bh(&xfrm_policy_lock);
  788. return ret;
  789. }
  790. static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  791. void **objp, atomic_t **obj_refp)
  792. {
  793. struct xfrm_policy *pol;
  794. int err = 0;
  795. #ifdef CONFIG_XFRM_SUB_POLICY
  796. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  797. if (IS_ERR(pol)) {
  798. err = PTR_ERR(pol);
  799. pol = NULL;
  800. }
  801. if (pol || err)
  802. goto end;
  803. #endif
  804. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  805. if (IS_ERR(pol)) {
  806. err = PTR_ERR(pol);
  807. pol = NULL;
  808. }
  809. #ifdef CONFIG_XFRM_SUB_POLICY
  810. end:
  811. #endif
  812. if ((*objp = (void *) pol) != NULL)
  813. *obj_refp = &pol->refcnt;
  814. return err;
  815. }
  816. static inline int policy_to_flow_dir(int dir)
  817. {
  818. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  819. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  820. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  821. return dir;
  822. switch (dir) {
  823. default:
  824. case XFRM_POLICY_IN:
  825. return FLOW_DIR_IN;
  826. case XFRM_POLICY_OUT:
  827. return FLOW_DIR_OUT;
  828. case XFRM_POLICY_FWD:
  829. return FLOW_DIR_FWD;
  830. }
  831. }
  832. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  833. {
  834. struct xfrm_policy *pol;
  835. read_lock_bh(&xfrm_policy_lock);
  836. if ((pol = sk->sk_policy[dir]) != NULL) {
  837. int match = xfrm_selector_match(&pol->selector, fl,
  838. sk->sk_family);
  839. int err = 0;
  840. if (match) {
  841. err = security_xfrm_policy_lookup(pol, fl->secid,
  842. policy_to_flow_dir(dir));
  843. if (!err)
  844. xfrm_pol_hold(pol);
  845. else if (err == -ESRCH)
  846. pol = NULL;
  847. else
  848. pol = ERR_PTR(err);
  849. } else
  850. pol = NULL;
  851. }
  852. read_unlock_bh(&xfrm_policy_lock);
  853. return pol;
  854. }
  855. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  856. {
  857. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  858. pol->family, dir);
  859. hlist_add_head(&pol->bydst, chain);
  860. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  861. xfrm_policy_count[dir]++;
  862. xfrm_pol_hold(pol);
  863. if (xfrm_bydst_should_resize(dir, NULL))
  864. schedule_work(&xfrm_hash_work);
  865. }
  866. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  867. int dir)
  868. {
  869. if (hlist_unhashed(&pol->bydst))
  870. return NULL;
  871. hlist_del(&pol->bydst);
  872. hlist_del(&pol->byidx);
  873. xfrm_policy_count[dir]--;
  874. return pol;
  875. }
  876. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  877. {
  878. write_lock_bh(&xfrm_policy_lock);
  879. pol = __xfrm_policy_unlink(pol, dir);
  880. write_unlock_bh(&xfrm_policy_lock);
  881. if (pol) {
  882. if (dir < XFRM_POLICY_MAX)
  883. atomic_inc(&flow_cache_genid);
  884. xfrm_policy_kill(pol);
  885. return 0;
  886. }
  887. return -ENOENT;
  888. }
  889. EXPORT_SYMBOL(xfrm_policy_delete);
  890. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  891. {
  892. struct xfrm_policy *old_pol;
  893. #ifdef CONFIG_XFRM_SUB_POLICY
  894. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  895. return -EINVAL;
  896. #endif
  897. write_lock_bh(&xfrm_policy_lock);
  898. old_pol = sk->sk_policy[dir];
  899. sk->sk_policy[dir] = pol;
  900. if (pol) {
  901. pol->curlft.add_time = get_seconds();
  902. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  903. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  904. }
  905. if (old_pol)
  906. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  907. write_unlock_bh(&xfrm_policy_lock);
  908. if (old_pol) {
  909. xfrm_policy_kill(old_pol);
  910. }
  911. return 0;
  912. }
  913. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  914. {
  915. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  916. if (newp) {
  917. newp->selector = old->selector;
  918. if (security_xfrm_policy_clone(old, newp)) {
  919. kfree(newp);
  920. return NULL; /* ENOMEM */
  921. }
  922. newp->lft = old->lft;
  923. newp->curlft = old->curlft;
  924. newp->action = old->action;
  925. newp->flags = old->flags;
  926. newp->xfrm_nr = old->xfrm_nr;
  927. newp->index = old->index;
  928. newp->type = old->type;
  929. memcpy(newp->xfrm_vec, old->xfrm_vec,
  930. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  931. write_lock_bh(&xfrm_policy_lock);
  932. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  933. write_unlock_bh(&xfrm_policy_lock);
  934. xfrm_pol_put(newp);
  935. }
  936. return newp;
  937. }
  938. int __xfrm_sk_clone_policy(struct sock *sk)
  939. {
  940. struct xfrm_policy *p0 = sk->sk_policy[0],
  941. *p1 = sk->sk_policy[1];
  942. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  943. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  944. return -ENOMEM;
  945. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  946. return -ENOMEM;
  947. return 0;
  948. }
  949. static int
  950. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  951. unsigned short family)
  952. {
  953. int err;
  954. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  955. if (unlikely(afinfo == NULL))
  956. return -EINVAL;
  957. err = afinfo->get_saddr(local, remote);
  958. xfrm_policy_put_afinfo(afinfo);
  959. return err;
  960. }
  961. /* Resolve list of templates for the flow, given policy. */
  962. static int
  963. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  964. struct xfrm_state **xfrm,
  965. unsigned short family)
  966. {
  967. int nx;
  968. int i, error;
  969. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  970. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  971. xfrm_address_t tmp;
  972. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  973. struct xfrm_state *x;
  974. xfrm_address_t *remote = daddr;
  975. xfrm_address_t *local = saddr;
  976. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  977. if (tmpl->mode == XFRM_MODE_TUNNEL ||
  978. tmpl->mode == XFRM_MODE_BEET) {
  979. remote = &tmpl->id.daddr;
  980. local = &tmpl->saddr;
  981. family = tmpl->encap_family;
  982. if (xfrm_addr_any(local, family)) {
  983. error = xfrm_get_saddr(&tmp, remote, family);
  984. if (error)
  985. goto fail;
  986. local = &tmp;
  987. }
  988. }
  989. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  990. if (x && x->km.state == XFRM_STATE_VALID) {
  991. xfrm[nx++] = x;
  992. daddr = remote;
  993. saddr = local;
  994. continue;
  995. }
  996. if (x) {
  997. error = (x->km.state == XFRM_STATE_ERROR ?
  998. -EINVAL : -EAGAIN);
  999. xfrm_state_put(x);
  1000. }
  1001. if (!tmpl->optional)
  1002. goto fail;
  1003. }
  1004. return nx;
  1005. fail:
  1006. for (nx--; nx>=0; nx--)
  1007. xfrm_state_put(xfrm[nx]);
  1008. return error;
  1009. }
  1010. static int
  1011. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1012. struct xfrm_state **xfrm,
  1013. unsigned short family)
  1014. {
  1015. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1016. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1017. int cnx = 0;
  1018. int error;
  1019. int ret;
  1020. int i;
  1021. for (i = 0; i < npols; i++) {
  1022. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1023. error = -ENOBUFS;
  1024. goto fail;
  1025. }
  1026. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1027. if (ret < 0) {
  1028. error = ret;
  1029. goto fail;
  1030. } else
  1031. cnx += ret;
  1032. }
  1033. /* found states are sorted for outbound processing */
  1034. if (npols > 1)
  1035. xfrm_state_sort(xfrm, tpp, cnx, family);
  1036. return cnx;
  1037. fail:
  1038. for (cnx--; cnx>=0; cnx--)
  1039. xfrm_state_put(tpp[cnx]);
  1040. return error;
  1041. }
  1042. /* Check that the bundle accepts the flow and its components are
  1043. * still valid.
  1044. */
  1045. static struct dst_entry *
  1046. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1047. {
  1048. struct dst_entry *x;
  1049. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1050. if (unlikely(afinfo == NULL))
  1051. return ERR_PTR(-EINVAL);
  1052. x = afinfo->find_bundle(fl, policy);
  1053. xfrm_policy_put_afinfo(afinfo);
  1054. return x;
  1055. }
  1056. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1057. * all the metrics... Shortly, bundle a bundle.
  1058. */
  1059. static int
  1060. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1061. struct flowi *fl, struct dst_entry **dst_p,
  1062. unsigned short family)
  1063. {
  1064. int err;
  1065. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1066. if (unlikely(afinfo == NULL))
  1067. return -EINVAL;
  1068. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1069. xfrm_policy_put_afinfo(afinfo);
  1070. return err;
  1071. }
  1072. static int inline
  1073. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1074. {
  1075. if (!*target) {
  1076. *target = kmalloc(size, GFP_ATOMIC);
  1077. if (!*target)
  1078. return -ENOMEM;
  1079. }
  1080. memcpy(*target, src, size);
  1081. return 0;
  1082. }
  1083. static int inline
  1084. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1085. {
  1086. #ifdef CONFIG_XFRM_SUB_POLICY
  1087. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1088. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1089. sel, sizeof(*sel));
  1090. #else
  1091. return 0;
  1092. #endif
  1093. }
  1094. static int inline
  1095. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1096. {
  1097. #ifdef CONFIG_XFRM_SUB_POLICY
  1098. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1099. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1100. #else
  1101. return 0;
  1102. #endif
  1103. }
  1104. static int stale_bundle(struct dst_entry *dst);
  1105. /* Main function: finds/creates a bundle for given flow.
  1106. *
  1107. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1108. * on interfaces with disabled IPsec.
  1109. */
  1110. int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1111. struct sock *sk, int flags)
  1112. {
  1113. struct xfrm_policy *policy;
  1114. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1115. int npols;
  1116. int pol_dead;
  1117. int xfrm_nr;
  1118. int pi;
  1119. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1120. struct dst_entry *dst, *dst_orig = *dst_p;
  1121. int nx = 0;
  1122. int err;
  1123. u32 genid;
  1124. u16 family;
  1125. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1126. restart:
  1127. genid = atomic_read(&flow_cache_genid);
  1128. policy = NULL;
  1129. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1130. pols[pi] = NULL;
  1131. npols = 0;
  1132. pol_dead = 0;
  1133. xfrm_nr = 0;
  1134. if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
  1135. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1136. err = PTR_ERR(policy);
  1137. if (IS_ERR(policy))
  1138. goto dropdst;
  1139. }
  1140. if (!policy) {
  1141. /* To accelerate a bit... */
  1142. if ((dst_orig->flags & DST_NOXFRM) ||
  1143. !xfrm_policy_count[XFRM_POLICY_OUT])
  1144. return 0;
  1145. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1146. dir, xfrm_policy_lookup);
  1147. err = PTR_ERR(policy);
  1148. if (IS_ERR(policy))
  1149. goto dropdst;
  1150. }
  1151. if (!policy)
  1152. return 0;
  1153. family = dst_orig->ops->family;
  1154. policy->curlft.use_time = get_seconds();
  1155. pols[0] = policy;
  1156. npols ++;
  1157. xfrm_nr += pols[0]->xfrm_nr;
  1158. switch (policy->action) {
  1159. default:
  1160. case XFRM_POLICY_BLOCK:
  1161. /* Prohibit the flow */
  1162. err = -EPERM;
  1163. goto error;
  1164. case XFRM_POLICY_ALLOW:
  1165. #ifndef CONFIG_XFRM_SUB_POLICY
  1166. if (policy->xfrm_nr == 0) {
  1167. /* Flow passes not transformed. */
  1168. xfrm_pol_put(policy);
  1169. return 0;
  1170. }
  1171. #endif
  1172. /* Try to find matching bundle.
  1173. *
  1174. * LATER: help from flow cache. It is optional, this
  1175. * is required only for output policy.
  1176. */
  1177. dst = xfrm_find_bundle(fl, policy, family);
  1178. if (IS_ERR(dst)) {
  1179. err = PTR_ERR(dst);
  1180. goto error;
  1181. }
  1182. if (dst)
  1183. break;
  1184. #ifdef CONFIG_XFRM_SUB_POLICY
  1185. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1186. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1187. fl, family,
  1188. XFRM_POLICY_OUT);
  1189. if (pols[1]) {
  1190. if (IS_ERR(pols[1])) {
  1191. err = PTR_ERR(pols[1]);
  1192. goto error;
  1193. }
  1194. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1195. err = -EPERM;
  1196. goto error;
  1197. }
  1198. npols ++;
  1199. xfrm_nr += pols[1]->xfrm_nr;
  1200. }
  1201. }
  1202. /*
  1203. * Because neither flowi nor bundle information knows about
  1204. * transformation template size. On more than one policy usage
  1205. * we can realize whether all of them is bypass or not after
  1206. * they are searched. See above not-transformed bypass
  1207. * is surrounded by non-sub policy configuration, too.
  1208. */
  1209. if (xfrm_nr == 0) {
  1210. /* Flow passes not transformed. */
  1211. xfrm_pols_put(pols, npols);
  1212. return 0;
  1213. }
  1214. #endif
  1215. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1216. if (unlikely(nx<0)) {
  1217. err = nx;
  1218. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1219. /* EREMOTE tells the caller to generate
  1220. * a one-shot blackhole route.
  1221. */
  1222. xfrm_pol_put(policy);
  1223. return -EREMOTE;
  1224. }
  1225. if (err == -EAGAIN && flags) {
  1226. DECLARE_WAITQUEUE(wait, current);
  1227. add_wait_queue(&km_waitq, &wait);
  1228. set_current_state(TASK_INTERRUPTIBLE);
  1229. schedule();
  1230. set_current_state(TASK_RUNNING);
  1231. remove_wait_queue(&km_waitq, &wait);
  1232. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1233. if (nx == -EAGAIN && signal_pending(current)) {
  1234. err = -ERESTART;
  1235. goto error;
  1236. }
  1237. if (nx == -EAGAIN ||
  1238. genid != atomic_read(&flow_cache_genid)) {
  1239. xfrm_pols_put(pols, npols);
  1240. goto restart;
  1241. }
  1242. err = nx;
  1243. }
  1244. if (err < 0)
  1245. goto error;
  1246. }
  1247. if (nx == 0) {
  1248. /* Flow passes not transformed. */
  1249. xfrm_pols_put(pols, npols);
  1250. return 0;
  1251. }
  1252. dst = dst_orig;
  1253. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1254. if (unlikely(err)) {
  1255. int i;
  1256. for (i=0; i<nx; i++)
  1257. xfrm_state_put(xfrm[i]);
  1258. goto error;
  1259. }
  1260. for (pi = 0; pi < npols; pi++) {
  1261. read_lock_bh(&pols[pi]->lock);
  1262. pol_dead |= pols[pi]->dead;
  1263. read_unlock_bh(&pols[pi]->lock);
  1264. }
  1265. write_lock_bh(&policy->lock);
  1266. if (unlikely(pol_dead || stale_bundle(dst))) {
  1267. /* Wow! While we worked on resolving, this
  1268. * policy has gone. Retry. It is not paranoia,
  1269. * we just cannot enlist new bundle to dead object.
  1270. * We can't enlist stable bundles either.
  1271. */
  1272. write_unlock_bh(&policy->lock);
  1273. if (dst)
  1274. dst_free(dst);
  1275. err = -EHOSTUNREACH;
  1276. goto error;
  1277. }
  1278. if (npols > 1)
  1279. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1280. else
  1281. err = xfrm_dst_update_origin(dst, fl);
  1282. if (unlikely(err)) {
  1283. write_unlock_bh(&policy->lock);
  1284. if (dst)
  1285. dst_free(dst);
  1286. goto error;
  1287. }
  1288. dst->next = policy->bundles;
  1289. policy->bundles = dst;
  1290. dst_hold(dst);
  1291. write_unlock_bh(&policy->lock);
  1292. }
  1293. *dst_p = dst;
  1294. dst_release(dst_orig);
  1295. xfrm_pols_put(pols, npols);
  1296. return 0;
  1297. error:
  1298. xfrm_pols_put(pols, npols);
  1299. dropdst:
  1300. dst_release(dst_orig);
  1301. *dst_p = NULL;
  1302. return err;
  1303. }
  1304. EXPORT_SYMBOL(__xfrm_lookup);
  1305. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1306. struct sock *sk, int flags)
  1307. {
  1308. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1309. if (err == -EREMOTE) {
  1310. dst_release(*dst_p);
  1311. *dst_p = NULL;
  1312. err = -EAGAIN;
  1313. }
  1314. return err;
  1315. }
  1316. EXPORT_SYMBOL(xfrm_lookup);
  1317. static inline int
  1318. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1319. {
  1320. struct xfrm_state *x;
  1321. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1322. return 0;
  1323. x = skb->sp->xvec[idx];
  1324. if (!x->type->reject)
  1325. return 0;
  1326. return x->type->reject(x, skb, fl);
  1327. }
  1328. /* When skb is transformed back to its "native" form, we have to
  1329. * check policy restrictions. At the moment we make this in maximally
  1330. * stupid way. Shame on me. :-) Of course, connected sockets must
  1331. * have policy cached at them.
  1332. */
  1333. static inline int
  1334. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1335. unsigned short family)
  1336. {
  1337. if (xfrm_state_kern(x))
  1338. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1339. return x->id.proto == tmpl->id.proto &&
  1340. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1341. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1342. x->props.mode == tmpl->mode &&
  1343. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1344. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1345. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1346. xfrm_state_addr_cmp(tmpl, x, family));
  1347. }
  1348. /*
  1349. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1350. * because of optional transport mode, or next index of the mathced secpath
  1351. * state with the template.
  1352. * -1 is returned when no matching template is found.
  1353. * Otherwise "-2 - errored_index" is returned.
  1354. */
  1355. static inline int
  1356. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1357. unsigned short family)
  1358. {
  1359. int idx = start;
  1360. if (tmpl->optional) {
  1361. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1362. return start;
  1363. } else
  1364. start = -1;
  1365. for (; idx < sp->len; idx++) {
  1366. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1367. return ++idx;
  1368. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1369. if (start == -1)
  1370. start = -2-idx;
  1371. break;
  1372. }
  1373. }
  1374. return start;
  1375. }
  1376. int
  1377. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1378. {
  1379. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1380. int err;
  1381. if (unlikely(afinfo == NULL))
  1382. return -EAFNOSUPPORT;
  1383. afinfo->decode_session(skb, fl);
  1384. err = security_xfrm_decode_session(skb, &fl->secid);
  1385. xfrm_policy_put_afinfo(afinfo);
  1386. return err;
  1387. }
  1388. EXPORT_SYMBOL(xfrm_decode_session);
  1389. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1390. {
  1391. for (; k < sp->len; k++) {
  1392. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1393. *idxp = k;
  1394. return 1;
  1395. }
  1396. }
  1397. return 0;
  1398. }
  1399. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1400. unsigned short family)
  1401. {
  1402. struct xfrm_policy *pol;
  1403. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1404. int npols = 0;
  1405. int xfrm_nr;
  1406. int pi;
  1407. struct flowi fl;
  1408. u8 fl_dir = policy_to_flow_dir(dir);
  1409. int xerr_idx = -1;
  1410. if (xfrm_decode_session(skb, &fl, family) < 0)
  1411. return 0;
  1412. nf_nat_decode_session(skb, &fl, family);
  1413. /* First, check used SA against their selectors. */
  1414. if (skb->sp) {
  1415. int i;
  1416. for (i=skb->sp->len-1; i>=0; i--) {
  1417. struct xfrm_state *x = skb->sp->xvec[i];
  1418. if (!xfrm_selector_match(&x->sel, &fl, family))
  1419. return 0;
  1420. }
  1421. }
  1422. pol = NULL;
  1423. if (sk && sk->sk_policy[dir]) {
  1424. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1425. if (IS_ERR(pol))
  1426. return 0;
  1427. }
  1428. if (!pol)
  1429. pol = flow_cache_lookup(&fl, family, fl_dir,
  1430. xfrm_policy_lookup);
  1431. if (IS_ERR(pol))
  1432. return 0;
  1433. if (!pol) {
  1434. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1435. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1436. return 0;
  1437. }
  1438. return 1;
  1439. }
  1440. pol->curlft.use_time = get_seconds();
  1441. pols[0] = pol;
  1442. npols ++;
  1443. #ifdef CONFIG_XFRM_SUB_POLICY
  1444. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1445. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1446. &fl, family,
  1447. XFRM_POLICY_IN);
  1448. if (pols[1]) {
  1449. if (IS_ERR(pols[1]))
  1450. return 0;
  1451. pols[1]->curlft.use_time = get_seconds();
  1452. npols ++;
  1453. }
  1454. }
  1455. #endif
  1456. if (pol->action == XFRM_POLICY_ALLOW) {
  1457. struct sec_path *sp;
  1458. static struct sec_path dummy;
  1459. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1460. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1461. struct xfrm_tmpl **tpp = tp;
  1462. int ti = 0;
  1463. int i, k;
  1464. if ((sp = skb->sp) == NULL)
  1465. sp = &dummy;
  1466. for (pi = 0; pi < npols; pi++) {
  1467. if (pols[pi] != pol &&
  1468. pols[pi]->action != XFRM_POLICY_ALLOW)
  1469. goto reject;
  1470. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1471. goto reject_error;
  1472. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1473. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1474. }
  1475. xfrm_nr = ti;
  1476. if (npols > 1) {
  1477. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1478. tpp = stp;
  1479. }
  1480. /* For each tunnel xfrm, find the first matching tmpl.
  1481. * For each tmpl before that, find corresponding xfrm.
  1482. * Order is _important_. Later we will implement
  1483. * some barriers, but at the moment barriers
  1484. * are implied between each two transformations.
  1485. */
  1486. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1487. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1488. if (k < 0) {
  1489. if (k < -1)
  1490. /* "-2 - errored_index" returned */
  1491. xerr_idx = -(2+k);
  1492. goto reject;
  1493. }
  1494. }
  1495. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1496. goto reject;
  1497. xfrm_pols_put(pols, npols);
  1498. return 1;
  1499. }
  1500. reject:
  1501. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1502. reject_error:
  1503. xfrm_pols_put(pols, npols);
  1504. return 0;
  1505. }
  1506. EXPORT_SYMBOL(__xfrm_policy_check);
  1507. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1508. {
  1509. struct flowi fl;
  1510. if (xfrm_decode_session(skb, &fl, family) < 0)
  1511. return 0;
  1512. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1513. }
  1514. EXPORT_SYMBOL(__xfrm_route_forward);
  1515. /* Optimize later using cookies and generation ids. */
  1516. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1517. {
  1518. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1519. * to "-1" to force all XFRM destinations to get validated by
  1520. * dst_ops->check on every use. We do this because when a
  1521. * normal route referenced by an XFRM dst is obsoleted we do
  1522. * not go looking around for all parent referencing XFRM dsts
  1523. * so that we can invalidate them. It is just too much work.
  1524. * Instead we make the checks here on every use. For example:
  1525. *
  1526. * XFRM dst A --> IPv4 dst X
  1527. *
  1528. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1529. * in this example). If X is marked obsolete, "A" will not
  1530. * notice. That's what we are validating here via the
  1531. * stale_bundle() check.
  1532. *
  1533. * When a policy's bundle is pruned, we dst_free() the XFRM
  1534. * dst which causes it's ->obsolete field to be set to a
  1535. * positive non-zero integer. If an XFRM dst has been pruned
  1536. * like this, we want to force a new route lookup.
  1537. */
  1538. if (dst->obsolete < 0 && !stale_bundle(dst))
  1539. return dst;
  1540. return NULL;
  1541. }
  1542. static int stale_bundle(struct dst_entry *dst)
  1543. {
  1544. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1545. }
  1546. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1547. {
  1548. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1549. dst->dev = init_net.loopback_dev;
  1550. dev_hold(dst->dev);
  1551. dev_put(dev);
  1552. }
  1553. }
  1554. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1555. static void xfrm_link_failure(struct sk_buff *skb)
  1556. {
  1557. /* Impossible. Such dst must be popped before reaches point of failure. */
  1558. return;
  1559. }
  1560. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1561. {
  1562. if (dst) {
  1563. if (dst->obsolete) {
  1564. dst_release(dst);
  1565. dst = NULL;
  1566. }
  1567. }
  1568. return dst;
  1569. }
  1570. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1571. {
  1572. struct dst_entry *dst, **dstp;
  1573. write_lock(&pol->lock);
  1574. dstp = &pol->bundles;
  1575. while ((dst=*dstp) != NULL) {
  1576. if (func(dst)) {
  1577. *dstp = dst->next;
  1578. dst->next = *gc_list_p;
  1579. *gc_list_p = dst;
  1580. } else {
  1581. dstp = &dst->next;
  1582. }
  1583. }
  1584. write_unlock(&pol->lock);
  1585. }
  1586. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1587. {
  1588. struct dst_entry *gc_list = NULL;
  1589. int dir;
  1590. read_lock_bh(&xfrm_policy_lock);
  1591. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1592. struct xfrm_policy *pol;
  1593. struct hlist_node *entry;
  1594. struct hlist_head *table;
  1595. int i;
  1596. hlist_for_each_entry(pol, entry,
  1597. &xfrm_policy_inexact[dir], bydst)
  1598. prune_one_bundle(pol, func, &gc_list);
  1599. table = xfrm_policy_bydst[dir].table;
  1600. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1601. hlist_for_each_entry(pol, entry, table + i, bydst)
  1602. prune_one_bundle(pol, func, &gc_list);
  1603. }
  1604. }
  1605. read_unlock_bh(&xfrm_policy_lock);
  1606. while (gc_list) {
  1607. struct dst_entry *dst = gc_list;
  1608. gc_list = dst->next;
  1609. dst_free(dst);
  1610. }
  1611. }
  1612. static int unused_bundle(struct dst_entry *dst)
  1613. {
  1614. return !atomic_read(&dst->__refcnt);
  1615. }
  1616. static void __xfrm_garbage_collect(void)
  1617. {
  1618. xfrm_prune_bundles(unused_bundle);
  1619. }
  1620. static int xfrm_flush_bundles(void)
  1621. {
  1622. xfrm_prune_bundles(stale_bundle);
  1623. return 0;
  1624. }
  1625. void xfrm_init_pmtu(struct dst_entry *dst)
  1626. {
  1627. do {
  1628. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1629. u32 pmtu, route_mtu_cached;
  1630. pmtu = dst_mtu(dst->child);
  1631. xdst->child_mtu_cached = pmtu;
  1632. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1633. route_mtu_cached = dst_mtu(xdst->route);
  1634. xdst->route_mtu_cached = route_mtu_cached;
  1635. if (pmtu > route_mtu_cached)
  1636. pmtu = route_mtu_cached;
  1637. dst->metrics[RTAX_MTU-1] = pmtu;
  1638. } while ((dst = dst->next));
  1639. }
  1640. EXPORT_SYMBOL(xfrm_init_pmtu);
  1641. /* Check that the bundle accepts the flow and its components are
  1642. * still valid.
  1643. */
  1644. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1645. struct flowi *fl, int family, int strict)
  1646. {
  1647. struct dst_entry *dst = &first->u.dst;
  1648. struct xfrm_dst *last;
  1649. u32 mtu;
  1650. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1651. (dst->dev && !netif_running(dst->dev)))
  1652. return 0;
  1653. #ifdef CONFIG_XFRM_SUB_POLICY
  1654. if (fl) {
  1655. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1656. return 0;
  1657. if (first->partner &&
  1658. !xfrm_selector_match(first->partner, fl, family))
  1659. return 0;
  1660. }
  1661. #endif
  1662. last = NULL;
  1663. do {
  1664. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1665. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1666. return 0;
  1667. if (fl && pol &&
  1668. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1669. return 0;
  1670. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1671. return 0;
  1672. if (xdst->genid != dst->xfrm->genid)
  1673. return 0;
  1674. if (strict && fl &&
  1675. !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1676. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1677. return 0;
  1678. mtu = dst_mtu(dst->child);
  1679. if (xdst->child_mtu_cached != mtu) {
  1680. last = xdst;
  1681. xdst->child_mtu_cached = mtu;
  1682. }
  1683. if (!dst_check(xdst->route, xdst->route_cookie))
  1684. return 0;
  1685. mtu = dst_mtu(xdst->route);
  1686. if (xdst->route_mtu_cached != mtu) {
  1687. last = xdst;
  1688. xdst->route_mtu_cached = mtu;
  1689. }
  1690. dst = dst->child;
  1691. } while (dst->xfrm);
  1692. if (likely(!last))
  1693. return 1;
  1694. mtu = last->child_mtu_cached;
  1695. for (;;) {
  1696. dst = &last->u.dst;
  1697. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1698. if (mtu > last->route_mtu_cached)
  1699. mtu = last->route_mtu_cached;
  1700. dst->metrics[RTAX_MTU-1] = mtu;
  1701. if (last == first)
  1702. break;
  1703. last = (struct xfrm_dst *)last->u.dst.next;
  1704. last->child_mtu_cached = mtu;
  1705. }
  1706. return 1;
  1707. }
  1708. EXPORT_SYMBOL(xfrm_bundle_ok);
  1709. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1710. {
  1711. int err = 0;
  1712. if (unlikely(afinfo == NULL))
  1713. return -EINVAL;
  1714. if (unlikely(afinfo->family >= NPROTO))
  1715. return -EAFNOSUPPORT;
  1716. write_lock_bh(&xfrm_policy_afinfo_lock);
  1717. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1718. err = -ENOBUFS;
  1719. else {
  1720. struct dst_ops *dst_ops = afinfo->dst_ops;
  1721. if (likely(dst_ops->kmem_cachep == NULL))
  1722. dst_ops->kmem_cachep = xfrm_dst_cache;
  1723. if (likely(dst_ops->check == NULL))
  1724. dst_ops->check = xfrm_dst_check;
  1725. if (likely(dst_ops->negative_advice == NULL))
  1726. dst_ops->negative_advice = xfrm_negative_advice;
  1727. if (likely(dst_ops->link_failure == NULL))
  1728. dst_ops->link_failure = xfrm_link_failure;
  1729. if (likely(afinfo->garbage_collect == NULL))
  1730. afinfo->garbage_collect = __xfrm_garbage_collect;
  1731. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1732. }
  1733. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1734. return err;
  1735. }
  1736. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1737. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1738. {
  1739. int err = 0;
  1740. if (unlikely(afinfo == NULL))
  1741. return -EINVAL;
  1742. if (unlikely(afinfo->family >= NPROTO))
  1743. return -EAFNOSUPPORT;
  1744. write_lock_bh(&xfrm_policy_afinfo_lock);
  1745. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1746. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1747. err = -EINVAL;
  1748. else {
  1749. struct dst_ops *dst_ops = afinfo->dst_ops;
  1750. xfrm_policy_afinfo[afinfo->family] = NULL;
  1751. dst_ops->kmem_cachep = NULL;
  1752. dst_ops->check = NULL;
  1753. dst_ops->negative_advice = NULL;
  1754. dst_ops->link_failure = NULL;
  1755. afinfo->garbage_collect = NULL;
  1756. }
  1757. }
  1758. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1759. return err;
  1760. }
  1761. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1762. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1763. {
  1764. struct xfrm_policy_afinfo *afinfo;
  1765. if (unlikely(family >= NPROTO))
  1766. return NULL;
  1767. read_lock(&xfrm_policy_afinfo_lock);
  1768. afinfo = xfrm_policy_afinfo[family];
  1769. if (unlikely(!afinfo))
  1770. read_unlock(&xfrm_policy_afinfo_lock);
  1771. return afinfo;
  1772. }
  1773. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1774. {
  1775. read_unlock(&xfrm_policy_afinfo_lock);
  1776. }
  1777. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1778. {
  1779. struct net_device *dev = ptr;
  1780. if (dev->nd_net != &init_net)
  1781. return NOTIFY_DONE;
  1782. switch (event) {
  1783. case NETDEV_DOWN:
  1784. xfrm_flush_bundles();
  1785. }
  1786. return NOTIFY_DONE;
  1787. }
  1788. static struct notifier_block xfrm_dev_notifier = {
  1789. xfrm_dev_event,
  1790. NULL,
  1791. 0
  1792. };
  1793. static void __init xfrm_policy_init(void)
  1794. {
  1795. unsigned int hmask, sz;
  1796. int dir;
  1797. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1798. sizeof(struct xfrm_dst),
  1799. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1800. NULL);
  1801. hmask = 8 - 1;
  1802. sz = (hmask+1) * sizeof(struct hlist_head);
  1803. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1804. xfrm_idx_hmask = hmask;
  1805. if (!xfrm_policy_byidx)
  1806. panic("XFRM: failed to allocate byidx hash\n");
  1807. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1808. struct xfrm_policy_hash *htab;
  1809. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1810. htab = &xfrm_policy_bydst[dir];
  1811. htab->table = xfrm_hash_alloc(sz);
  1812. htab->hmask = hmask;
  1813. if (!htab->table)
  1814. panic("XFRM: failed to allocate bydst hash\n");
  1815. }
  1816. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  1817. register_netdevice_notifier(&xfrm_dev_notifier);
  1818. }
  1819. void __init xfrm_init(void)
  1820. {
  1821. xfrm_state_init();
  1822. xfrm_policy_init();
  1823. xfrm_input_init();
  1824. }
  1825. #ifdef CONFIG_AUDITSYSCALL
  1826. static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  1827. struct audit_buffer *audit_buf)
  1828. {
  1829. if (xp->security)
  1830. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1831. xp->security->ctx_alg, xp->security->ctx_doi,
  1832. xp->security->ctx_str);
  1833. switch(xp->selector.family) {
  1834. case AF_INET:
  1835. audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
  1836. NIPQUAD(xp->selector.saddr.a4),
  1837. NIPQUAD(xp->selector.daddr.a4));
  1838. break;
  1839. case AF_INET6:
  1840. {
  1841. struct in6_addr saddr6, daddr6;
  1842. memcpy(&saddr6, xp->selector.saddr.a6,
  1843. sizeof(struct in6_addr));
  1844. memcpy(&daddr6, xp->selector.daddr.a6,
  1845. sizeof(struct in6_addr));
  1846. audit_log_format(audit_buf,
  1847. " src=" NIP6_FMT " dst=" NIP6_FMT,
  1848. NIP6(saddr6), NIP6(daddr6));
  1849. }
  1850. break;
  1851. }
  1852. }
  1853. void
  1854. xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1855. {
  1856. struct audit_buffer *audit_buf;
  1857. extern int audit_enabled;
  1858. if (audit_enabled == 0)
  1859. return;
  1860. audit_buf = xfrm_audit_start(auid, sid);
  1861. if (audit_buf == NULL)
  1862. return;
  1863. audit_log_format(audit_buf, " op=SPD-add res=%u", result);
  1864. xfrm_audit_common_policyinfo(xp, audit_buf);
  1865. audit_log_end(audit_buf);
  1866. }
  1867. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  1868. void
  1869. xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1870. {
  1871. struct audit_buffer *audit_buf;
  1872. extern int audit_enabled;
  1873. if (audit_enabled == 0)
  1874. return;
  1875. audit_buf = xfrm_audit_start(auid, sid);
  1876. if (audit_buf == NULL)
  1877. return;
  1878. audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
  1879. xfrm_audit_common_policyinfo(xp, audit_buf);
  1880. audit_log_end(audit_buf);
  1881. }
  1882. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  1883. #endif
  1884. #ifdef CONFIG_XFRM_MIGRATE
  1885. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  1886. struct xfrm_selector *sel_tgt)
  1887. {
  1888. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  1889. if (sel_tgt->family == sel_cmp->family &&
  1890. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  1891. sel_cmp->family) == 0 &&
  1892. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  1893. sel_cmp->family) == 0 &&
  1894. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  1895. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  1896. return 1;
  1897. }
  1898. } else {
  1899. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  1900. return 1;
  1901. }
  1902. }
  1903. return 0;
  1904. }
  1905. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  1906. u8 dir, u8 type)
  1907. {
  1908. struct xfrm_policy *pol, *ret = NULL;
  1909. struct hlist_node *entry;
  1910. struct hlist_head *chain;
  1911. u32 priority = ~0U;
  1912. read_lock_bh(&xfrm_policy_lock);
  1913. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  1914. hlist_for_each_entry(pol, entry, chain, bydst) {
  1915. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1916. pol->type == type) {
  1917. ret = pol;
  1918. priority = ret->priority;
  1919. break;
  1920. }
  1921. }
  1922. chain = &xfrm_policy_inexact[dir];
  1923. hlist_for_each_entry(pol, entry, chain, bydst) {
  1924. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1925. pol->type == type &&
  1926. pol->priority < priority) {
  1927. ret = pol;
  1928. break;
  1929. }
  1930. }
  1931. if (ret)
  1932. xfrm_pol_hold(ret);
  1933. read_unlock_bh(&xfrm_policy_lock);
  1934. return ret;
  1935. }
  1936. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  1937. {
  1938. int match = 0;
  1939. if (t->mode == m->mode && t->id.proto == m->proto &&
  1940. (m->reqid == 0 || t->reqid == m->reqid)) {
  1941. switch (t->mode) {
  1942. case XFRM_MODE_TUNNEL:
  1943. case XFRM_MODE_BEET:
  1944. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  1945. m->old_family) == 0 &&
  1946. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  1947. m->old_family) == 0) {
  1948. match = 1;
  1949. }
  1950. break;
  1951. case XFRM_MODE_TRANSPORT:
  1952. /* in case of transport mode, template does not store
  1953. any IP addresses, hence we just compare mode and
  1954. protocol */
  1955. match = 1;
  1956. break;
  1957. default:
  1958. break;
  1959. }
  1960. }
  1961. return match;
  1962. }
  1963. /* update endpoint address(es) of template(s) */
  1964. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  1965. struct xfrm_migrate *m, int num_migrate)
  1966. {
  1967. struct xfrm_migrate *mp;
  1968. struct dst_entry *dst;
  1969. int i, j, n = 0;
  1970. write_lock_bh(&pol->lock);
  1971. if (unlikely(pol->dead)) {
  1972. /* target policy has been deleted */
  1973. write_unlock_bh(&pol->lock);
  1974. return -ENOENT;
  1975. }
  1976. for (i = 0; i < pol->xfrm_nr; i++) {
  1977. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  1978. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  1979. continue;
  1980. n++;
  1981. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
  1982. pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
  1983. continue;
  1984. /* update endpoints */
  1985. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  1986. sizeof(pol->xfrm_vec[i].id.daddr));
  1987. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  1988. sizeof(pol->xfrm_vec[i].saddr));
  1989. pol->xfrm_vec[i].encap_family = mp->new_family;
  1990. /* flush bundles */
  1991. while ((dst = pol->bundles) != NULL) {
  1992. pol->bundles = dst->next;
  1993. dst_free(dst);
  1994. }
  1995. }
  1996. }
  1997. write_unlock_bh(&pol->lock);
  1998. if (!n)
  1999. return -ENODATA;
  2000. return 0;
  2001. }
  2002. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  2003. {
  2004. int i, j;
  2005. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2006. return -EINVAL;
  2007. for (i = 0; i < num_migrate; i++) {
  2008. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2009. m[i].old_family) == 0) &&
  2010. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2011. m[i].old_family) == 0))
  2012. return -EINVAL;
  2013. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2014. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2015. return -EINVAL;
  2016. /* check if there is any duplicated entry */
  2017. for (j = i + 1; j < num_migrate; j++) {
  2018. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2019. sizeof(m[i].old_daddr)) &&
  2020. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2021. sizeof(m[i].old_saddr)) &&
  2022. m[i].proto == m[j].proto &&
  2023. m[i].mode == m[j].mode &&
  2024. m[i].reqid == m[j].reqid &&
  2025. m[i].old_family == m[j].old_family)
  2026. return -EINVAL;
  2027. }
  2028. }
  2029. return 0;
  2030. }
  2031. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2032. struct xfrm_migrate *m, int num_migrate)
  2033. {
  2034. int i, err, nx_cur = 0, nx_new = 0;
  2035. struct xfrm_policy *pol = NULL;
  2036. struct xfrm_state *x, *xc;
  2037. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2038. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2039. struct xfrm_migrate *mp;
  2040. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2041. goto out;
  2042. /* Stage 1 - find policy */
  2043. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2044. err = -ENOENT;
  2045. goto out;
  2046. }
  2047. /* Stage 2 - find and update state(s) */
  2048. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2049. if ((x = xfrm_migrate_state_find(mp))) {
  2050. x_cur[nx_cur] = x;
  2051. nx_cur++;
  2052. if ((xc = xfrm_state_migrate(x, mp))) {
  2053. x_new[nx_new] = xc;
  2054. nx_new++;
  2055. } else {
  2056. err = -ENODATA;
  2057. goto restore_state;
  2058. }
  2059. }
  2060. }
  2061. /* Stage 3 - update policy */
  2062. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2063. goto restore_state;
  2064. /* Stage 4 - delete old state(s) */
  2065. if (nx_cur) {
  2066. xfrm_states_put(x_cur, nx_cur);
  2067. xfrm_states_delete(x_cur, nx_cur);
  2068. }
  2069. /* Stage 5 - announce */
  2070. km_migrate(sel, dir, type, m, num_migrate);
  2071. xfrm_pol_put(pol);
  2072. return 0;
  2073. out:
  2074. return err;
  2075. restore_state:
  2076. if (pol)
  2077. xfrm_pol_put(pol);
  2078. if (nx_cur)
  2079. xfrm_states_put(x_cur, nx_cur);
  2080. if (nx_new)
  2081. xfrm_states_delete(x_new, nx_new);
  2082. return err;
  2083. }
  2084. EXPORT_SYMBOL(xfrm_migrate);
  2085. #endif