xfrm_policy.c 71 KB

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