xfrm_policy.c 77 KB

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