xfrm_policy.c 77 KB

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