xfrm_policy.c 76 KB

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