xfrm_policy.c 64 KB

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