xfrm_policy.c 77 KB

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