xfrm_policy.c 77 KB

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