xfrm_policy.c 79 KB

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