xfrm_policy.c 76 KB

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