xfrm_policy.c 76 KB

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