xfrm_policy.c 77 KB

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