xfrm_policy.c 80 KB

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