xfrm_policy.c 71 KB

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