xfrm_policy.c 80 KB

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