xfrm_policy.c 76 KB

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