xfrm_policy.c 77 KB

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