xfrm_policy.c 70 KB

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