xfrm_policy.c 76 KB

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