xfrm_policy.c 77 KB

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