xfrm_policy.c 62 KB

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