xfrm_policy.c 80 KB

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