xfrm_policy.c 81 KB

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