xfrm_policy.c 76 KB

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