xfrm_policy.c 70 KB

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