xfrm_policy.c 71 KB

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