xfrm_policy.c 68 KB

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