xfrm_policy.c 78 KB

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