xfrm_policy.c 80 KB

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