xfrm_policy.c 67 KB

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