xfrm_policy.c 77 KB

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