xfrm_policy.c 78 KB

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