xfrm_policy.c 76 KB

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