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