xfrm_policy.c 77 KB

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