xfrm_policy.c 70 KB

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