xfrm_policy.c 80 KB

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