xfrm_policy.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  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/slab.h>
  16. #include <linux/kmod.h>
  17. #include <linux/list.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/notifier.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/netfilter.h>
  23. #include <linux/module.h>
  24. #include <linux/cache.h>
  25. #include <net/xfrm.h>
  26. #include <net/ip.h>
  27. #include "xfrm_hash.h"
  28. int sysctl_xfrm_larval_drop __read_mostly;
  29. DEFINE_MUTEX(xfrm_cfg_mutex);
  30. EXPORT_SYMBOL(xfrm_cfg_mutex);
  31. static DEFINE_RWLOCK(xfrm_policy_lock);
  32. unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
  33. EXPORT_SYMBOL(xfrm_policy_count);
  34. static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
  35. static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
  36. static struct kmem_cache *xfrm_dst_cache __read_mostly;
  37. static struct work_struct xfrm_policy_gc_work;
  38. static HLIST_HEAD(xfrm_policy_gc_list);
  39. static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
  40. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
  41. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
  42. static inline int
  43. __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  44. {
  45. return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
  46. addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
  47. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  48. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  49. (fl->proto == sel->proto || !sel->proto) &&
  50. (fl->oif == sel->ifindex || !sel->ifindex);
  51. }
  52. static inline int
  53. __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  54. {
  55. return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
  56. addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
  57. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  58. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  59. (fl->proto == sel->proto || !sel->proto) &&
  60. (fl->oif == sel->ifindex || !sel->ifindex);
  61. }
  62. int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
  63. unsigned short family)
  64. {
  65. switch (family) {
  66. case AF_INET:
  67. return __xfrm4_selector_match(sel, fl);
  68. case AF_INET6:
  69. return __xfrm6_selector_match(sel, fl);
  70. }
  71. return 0;
  72. }
  73. int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
  74. unsigned short family)
  75. {
  76. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  77. int err = 0;
  78. if (unlikely(afinfo == NULL))
  79. return -EAFNOSUPPORT;
  80. if (likely(afinfo->dst_lookup != NULL))
  81. err = afinfo->dst_lookup(dst, fl);
  82. else
  83. err = -EINVAL;
  84. xfrm_policy_put_afinfo(afinfo);
  85. return err;
  86. }
  87. EXPORT_SYMBOL(xfrm_dst_lookup);
  88. static inline unsigned long make_jiffies(long secs)
  89. {
  90. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  91. return MAX_SCHEDULE_TIMEOUT-1;
  92. else
  93. return secs*HZ;
  94. }
  95. static void xfrm_policy_timer(unsigned long data)
  96. {
  97. struct xfrm_policy *xp = (struct xfrm_policy*)data;
  98. unsigned long now = get_seconds();
  99. long next = LONG_MAX;
  100. int warn = 0;
  101. int dir;
  102. read_lock(&xp->lock);
  103. if (xp->dead)
  104. goto out;
  105. dir = xfrm_policy_id2dir(xp->index);
  106. if (xp->lft.hard_add_expires_seconds) {
  107. long tmo = xp->lft.hard_add_expires_seconds +
  108. xp->curlft.add_time - now;
  109. if (tmo <= 0)
  110. goto expired;
  111. if (tmo < next)
  112. next = tmo;
  113. }
  114. if (xp->lft.hard_use_expires_seconds) {
  115. long tmo = xp->lft.hard_use_expires_seconds +
  116. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  117. if (tmo <= 0)
  118. goto expired;
  119. if (tmo < next)
  120. next = tmo;
  121. }
  122. if (xp->lft.soft_add_expires_seconds) {
  123. long tmo = xp->lft.soft_add_expires_seconds +
  124. xp->curlft.add_time - now;
  125. if (tmo <= 0) {
  126. warn = 1;
  127. tmo = XFRM_KM_TIMEOUT;
  128. }
  129. if (tmo < next)
  130. next = tmo;
  131. }
  132. if (xp->lft.soft_use_expires_seconds) {
  133. long tmo = xp->lft.soft_use_expires_seconds +
  134. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  135. if (tmo <= 0) {
  136. warn = 1;
  137. tmo = XFRM_KM_TIMEOUT;
  138. }
  139. if (tmo < next)
  140. next = tmo;
  141. }
  142. if (warn)
  143. km_policy_expired(xp, dir, 0, 0);
  144. if (next != LONG_MAX &&
  145. !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
  146. xfrm_pol_hold(xp);
  147. out:
  148. read_unlock(&xp->lock);
  149. xfrm_pol_put(xp);
  150. return;
  151. expired:
  152. read_unlock(&xp->lock);
  153. if (!xfrm_policy_delete(xp, dir))
  154. km_policy_expired(xp, dir, 1, 0);
  155. xfrm_pol_put(xp);
  156. }
  157. /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
  158. * SPD calls.
  159. */
  160. struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
  161. {
  162. struct xfrm_policy *policy;
  163. policy = kzalloc(sizeof(struct xfrm_policy), gfp);
  164. if (policy) {
  165. INIT_HLIST_NODE(&policy->bydst);
  166. INIT_HLIST_NODE(&policy->byidx);
  167. rwlock_init(&policy->lock);
  168. atomic_set(&policy->refcnt, 1);
  169. init_timer(&policy->timer);
  170. policy->timer.data = (unsigned long)policy;
  171. policy->timer.function = xfrm_policy_timer;
  172. }
  173. return policy;
  174. }
  175. EXPORT_SYMBOL(xfrm_policy_alloc);
  176. /* Destroy xfrm_policy: descendant resources must be released to this moment. */
  177. void __xfrm_policy_destroy(struct xfrm_policy *policy)
  178. {
  179. BUG_ON(!policy->dead);
  180. BUG_ON(policy->bundles);
  181. if (del_timer(&policy->timer))
  182. BUG();
  183. security_xfrm_policy_free(policy);
  184. kfree(policy);
  185. }
  186. EXPORT_SYMBOL(__xfrm_policy_destroy);
  187. static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
  188. {
  189. struct dst_entry *dst;
  190. while ((dst = policy->bundles) != NULL) {
  191. policy->bundles = dst->next;
  192. dst_free(dst);
  193. }
  194. if (del_timer(&policy->timer))
  195. atomic_dec(&policy->refcnt);
  196. if (atomic_read(&policy->refcnt) > 1)
  197. flow_cache_flush();
  198. xfrm_pol_put(policy);
  199. }
  200. static void xfrm_policy_gc_task(struct work_struct *work)
  201. {
  202. struct xfrm_policy *policy;
  203. struct hlist_node *entry, *tmp;
  204. struct hlist_head gc_list;
  205. spin_lock_bh(&xfrm_policy_gc_lock);
  206. gc_list.first = xfrm_policy_gc_list.first;
  207. INIT_HLIST_HEAD(&xfrm_policy_gc_list);
  208. spin_unlock_bh(&xfrm_policy_gc_lock);
  209. hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
  210. xfrm_policy_gc_kill(policy);
  211. }
  212. /* Rule must be locked. Release descentant resources, announce
  213. * entry dead. The rule must be unlinked from lists to the moment.
  214. */
  215. static void xfrm_policy_kill(struct xfrm_policy *policy)
  216. {
  217. int dead;
  218. write_lock_bh(&policy->lock);
  219. dead = policy->dead;
  220. policy->dead = 1;
  221. write_unlock_bh(&policy->lock);
  222. if (unlikely(dead)) {
  223. WARN_ON(1);
  224. return;
  225. }
  226. spin_lock(&xfrm_policy_gc_lock);
  227. hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
  228. spin_unlock(&xfrm_policy_gc_lock);
  229. schedule_work(&xfrm_policy_gc_work);
  230. }
  231. struct xfrm_policy_hash {
  232. struct hlist_head *table;
  233. unsigned int hmask;
  234. };
  235. static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
  236. static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
  237. static struct hlist_head *xfrm_policy_byidx __read_mostly;
  238. static unsigned int xfrm_idx_hmask __read_mostly;
  239. static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
  240. static inline unsigned int idx_hash(u32 index)
  241. {
  242. return __idx_hash(index, xfrm_idx_hmask);
  243. }
  244. static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
  245. {
  246. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  247. unsigned int hash = __sel_hash(sel, family, hmask);
  248. return (hash == hmask + 1 ?
  249. &xfrm_policy_inexact[dir] :
  250. xfrm_policy_bydst[dir].table + hash);
  251. }
  252. static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
  253. {
  254. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  255. unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
  256. return xfrm_policy_bydst[dir].table + hash;
  257. }
  258. static void xfrm_dst_hash_transfer(struct hlist_head *list,
  259. struct hlist_head *ndsttable,
  260. unsigned int nhashmask)
  261. {
  262. struct hlist_node *entry, *tmp;
  263. struct xfrm_policy *pol;
  264. hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
  265. unsigned int h;
  266. h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
  267. pol->family, nhashmask);
  268. hlist_add_head(&pol->bydst, ndsttable+h);
  269. }
  270. }
  271. static void xfrm_idx_hash_transfer(struct hlist_head *list,
  272. struct hlist_head *nidxtable,
  273. unsigned int nhashmask)
  274. {
  275. struct hlist_node *entry, *tmp;
  276. struct xfrm_policy *pol;
  277. hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
  278. unsigned int h;
  279. h = __idx_hash(pol->index, nhashmask);
  280. hlist_add_head(&pol->byidx, nidxtable+h);
  281. }
  282. }
  283. static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
  284. {
  285. return ((old_hmask + 1) << 1) - 1;
  286. }
  287. static void xfrm_bydst_resize(int dir)
  288. {
  289. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  290. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  291. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  292. struct hlist_head *odst = xfrm_policy_bydst[dir].table;
  293. struct hlist_head *ndst = xfrm_hash_alloc(nsize);
  294. int i;
  295. if (!ndst)
  296. return;
  297. write_lock_bh(&xfrm_policy_lock);
  298. for (i = hmask; i >= 0; i--)
  299. xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
  300. xfrm_policy_bydst[dir].table = ndst;
  301. xfrm_policy_bydst[dir].hmask = nhashmask;
  302. write_unlock_bh(&xfrm_policy_lock);
  303. xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
  304. }
  305. static void xfrm_byidx_resize(int total)
  306. {
  307. unsigned int hmask = xfrm_idx_hmask;
  308. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  309. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  310. struct hlist_head *oidx = xfrm_policy_byidx;
  311. struct hlist_head *nidx = xfrm_hash_alloc(nsize);
  312. int i;
  313. if (!nidx)
  314. return;
  315. write_lock_bh(&xfrm_policy_lock);
  316. for (i = hmask; i >= 0; i--)
  317. xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
  318. xfrm_policy_byidx = nidx;
  319. xfrm_idx_hmask = nhashmask;
  320. write_unlock_bh(&xfrm_policy_lock);
  321. xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
  322. }
  323. static inline int xfrm_bydst_should_resize(int dir, int *total)
  324. {
  325. unsigned int cnt = xfrm_policy_count[dir];
  326. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  327. if (total)
  328. *total += cnt;
  329. if ((hmask + 1) < xfrm_policy_hashmax &&
  330. cnt > hmask)
  331. return 1;
  332. return 0;
  333. }
  334. static inline int xfrm_byidx_should_resize(int total)
  335. {
  336. unsigned int hmask = xfrm_idx_hmask;
  337. if ((hmask + 1) < xfrm_policy_hashmax &&
  338. total > hmask)
  339. return 1;
  340. return 0;
  341. }
  342. void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
  343. {
  344. read_lock_bh(&xfrm_policy_lock);
  345. si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
  346. si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
  347. si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
  348. si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
  349. si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
  350. si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
  351. si->spdhcnt = xfrm_idx_hmask;
  352. si->spdhmcnt = xfrm_policy_hashmax;
  353. read_unlock_bh(&xfrm_policy_lock);
  354. }
  355. EXPORT_SYMBOL(xfrm_spd_getinfo);
  356. static DEFINE_MUTEX(hash_resize_mutex);
  357. static void xfrm_hash_resize(struct work_struct *__unused)
  358. {
  359. int dir, total;
  360. mutex_lock(&hash_resize_mutex);
  361. total = 0;
  362. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  363. if (xfrm_bydst_should_resize(dir, &total))
  364. xfrm_bydst_resize(dir);
  365. }
  366. if (xfrm_byidx_should_resize(total))
  367. xfrm_byidx_resize(total);
  368. mutex_unlock(&hash_resize_mutex);
  369. }
  370. static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
  371. /* Generate new index... KAME seems to generate them ordered by cost
  372. * of an absolute inpredictability of ordering of rules. This will not pass. */
  373. static u32 xfrm_gen_index(u8 type, int dir)
  374. {
  375. static u32 idx_generator;
  376. for (;;) {
  377. struct hlist_node *entry;
  378. struct hlist_head *list;
  379. struct xfrm_policy *p;
  380. u32 idx;
  381. int found;
  382. idx = (idx_generator | dir);
  383. idx_generator += 8;
  384. if (idx == 0)
  385. idx = 8;
  386. list = xfrm_policy_byidx + idx_hash(idx);
  387. found = 0;
  388. hlist_for_each_entry(p, entry, list, byidx) {
  389. if (p->index == idx) {
  390. found = 1;
  391. break;
  392. }
  393. }
  394. if (!found)
  395. return idx;
  396. }
  397. }
  398. static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
  399. {
  400. u32 *p1 = (u32 *) s1;
  401. u32 *p2 = (u32 *) s2;
  402. int len = sizeof(struct xfrm_selector) / sizeof(u32);
  403. int i;
  404. for (i = 0; i < len; i++) {
  405. if (p1[i] != p2[i])
  406. return 1;
  407. }
  408. return 0;
  409. }
  410. int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
  411. {
  412. struct xfrm_policy *pol;
  413. struct xfrm_policy *delpol;
  414. struct hlist_head *chain;
  415. struct hlist_node *entry, *newpos;
  416. struct dst_entry *gc_list;
  417. write_lock_bh(&xfrm_policy_lock);
  418. chain = policy_hash_bysel(&policy->selector, policy->family, dir);
  419. delpol = NULL;
  420. newpos = NULL;
  421. hlist_for_each_entry(pol, entry, chain, bydst) {
  422. if (pol->type == policy->type &&
  423. !selector_cmp(&pol->selector, &policy->selector) &&
  424. xfrm_sec_ctx_match(pol->security, policy->security) &&
  425. !WARN_ON(delpol)) {
  426. if (excl) {
  427. write_unlock_bh(&xfrm_policy_lock);
  428. return -EEXIST;
  429. }
  430. delpol = pol;
  431. if (policy->priority > pol->priority)
  432. continue;
  433. } else if (policy->priority >= pol->priority) {
  434. newpos = &pol->bydst;
  435. continue;
  436. }
  437. if (delpol)
  438. break;
  439. }
  440. if (newpos)
  441. hlist_add_after(newpos, &policy->bydst);
  442. else
  443. hlist_add_head(&policy->bydst, chain);
  444. xfrm_pol_hold(policy);
  445. xfrm_policy_count[dir]++;
  446. atomic_inc(&flow_cache_genid);
  447. if (delpol) {
  448. hlist_del(&delpol->bydst);
  449. hlist_del(&delpol->byidx);
  450. xfrm_policy_count[dir]--;
  451. }
  452. policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
  453. hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
  454. policy->curlft.add_time = get_seconds();
  455. policy->curlft.use_time = 0;
  456. if (!mod_timer(&policy->timer, jiffies + HZ))
  457. xfrm_pol_hold(policy);
  458. write_unlock_bh(&xfrm_policy_lock);
  459. if (delpol)
  460. xfrm_policy_kill(delpol);
  461. else if (xfrm_bydst_should_resize(dir, NULL))
  462. schedule_work(&xfrm_hash_work);
  463. read_lock_bh(&xfrm_policy_lock);
  464. gc_list = NULL;
  465. entry = &policy->bydst;
  466. hlist_for_each_entry_continue(policy, entry, bydst) {
  467. struct dst_entry *dst;
  468. write_lock(&policy->lock);
  469. dst = policy->bundles;
  470. if (dst) {
  471. struct dst_entry *tail = dst;
  472. while (tail->next)
  473. tail = tail->next;
  474. tail->next = gc_list;
  475. gc_list = dst;
  476. policy->bundles = NULL;
  477. }
  478. write_unlock(&policy->lock);
  479. }
  480. read_unlock_bh(&xfrm_policy_lock);
  481. while (gc_list) {
  482. struct dst_entry *dst = gc_list;
  483. gc_list = dst->next;
  484. dst_free(dst);
  485. }
  486. return 0;
  487. }
  488. EXPORT_SYMBOL(xfrm_policy_insert);
  489. struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
  490. struct xfrm_selector *sel,
  491. struct xfrm_sec_ctx *ctx, int delete,
  492. int *err)
  493. {
  494. struct xfrm_policy *pol, *ret;
  495. struct hlist_head *chain;
  496. struct hlist_node *entry;
  497. *err = 0;
  498. write_lock_bh(&xfrm_policy_lock);
  499. chain = policy_hash_bysel(sel, sel->family, dir);
  500. ret = NULL;
  501. hlist_for_each_entry(pol, entry, chain, bydst) {
  502. if (pol->type == type &&
  503. !selector_cmp(sel, &pol->selector) &&
  504. xfrm_sec_ctx_match(ctx, pol->security)) {
  505. xfrm_pol_hold(pol);
  506. if (delete) {
  507. *err = security_xfrm_policy_delete(pol);
  508. if (*err) {
  509. write_unlock_bh(&xfrm_policy_lock);
  510. return pol;
  511. }
  512. hlist_del(&pol->bydst);
  513. hlist_del(&pol->byidx);
  514. xfrm_policy_count[dir]--;
  515. }
  516. ret = pol;
  517. break;
  518. }
  519. }
  520. write_unlock_bh(&xfrm_policy_lock);
  521. if (ret && delete) {
  522. atomic_inc(&flow_cache_genid);
  523. xfrm_policy_kill(ret);
  524. }
  525. return ret;
  526. }
  527. EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
  528. struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
  529. int *err)
  530. {
  531. struct xfrm_policy *pol, *ret;
  532. struct hlist_head *chain;
  533. struct hlist_node *entry;
  534. *err = -ENOENT;
  535. if (xfrm_policy_id2dir(id) != dir)
  536. return NULL;
  537. *err = 0;
  538. write_lock_bh(&xfrm_policy_lock);
  539. chain = xfrm_policy_byidx + idx_hash(id);
  540. ret = NULL;
  541. hlist_for_each_entry(pol, entry, chain, byidx) {
  542. if (pol->type == type && pol->index == id) {
  543. xfrm_pol_hold(pol);
  544. if (delete) {
  545. *err = security_xfrm_policy_delete(pol);
  546. if (*err) {
  547. write_unlock_bh(&xfrm_policy_lock);
  548. return pol;
  549. }
  550. hlist_del(&pol->bydst);
  551. hlist_del(&pol->byidx);
  552. xfrm_policy_count[dir]--;
  553. }
  554. ret = pol;
  555. break;
  556. }
  557. }
  558. write_unlock_bh(&xfrm_policy_lock);
  559. if (ret && delete) {
  560. atomic_inc(&flow_cache_genid);
  561. xfrm_policy_kill(ret);
  562. }
  563. return ret;
  564. }
  565. EXPORT_SYMBOL(xfrm_policy_byid);
  566. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  567. static inline int
  568. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  569. {
  570. int dir, err = 0;
  571. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  572. struct xfrm_policy *pol;
  573. struct hlist_node *entry;
  574. int i;
  575. hlist_for_each_entry(pol, entry,
  576. &xfrm_policy_inexact[dir], bydst) {
  577. if (pol->type != type)
  578. continue;
  579. err = security_xfrm_policy_delete(pol);
  580. if (err) {
  581. xfrm_audit_policy_delete(pol, 0,
  582. audit_info->loginuid,
  583. audit_info->secid);
  584. return err;
  585. }
  586. }
  587. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  588. hlist_for_each_entry(pol, entry,
  589. xfrm_policy_bydst[dir].table + i,
  590. bydst) {
  591. if (pol->type != type)
  592. continue;
  593. err = security_xfrm_policy_delete(pol);
  594. if (err) {
  595. xfrm_audit_policy_delete(pol, 0,
  596. audit_info->loginuid,
  597. audit_info->secid);
  598. return err;
  599. }
  600. }
  601. }
  602. }
  603. return err;
  604. }
  605. #else
  606. static inline int
  607. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  608. {
  609. return 0;
  610. }
  611. #endif
  612. int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
  613. {
  614. int dir, err = 0;
  615. write_lock_bh(&xfrm_policy_lock);
  616. err = xfrm_policy_flush_secctx_check(type, audit_info);
  617. if (err)
  618. goto out;
  619. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  620. struct xfrm_policy *pol;
  621. struct hlist_node *entry;
  622. int i, killed;
  623. killed = 0;
  624. again1:
  625. hlist_for_each_entry(pol, entry,
  626. &xfrm_policy_inexact[dir], bydst) {
  627. if (pol->type != type)
  628. continue;
  629. hlist_del(&pol->bydst);
  630. hlist_del(&pol->byidx);
  631. write_unlock_bh(&xfrm_policy_lock);
  632. xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
  633. audit_info->secid);
  634. xfrm_policy_kill(pol);
  635. killed++;
  636. write_lock_bh(&xfrm_policy_lock);
  637. goto again1;
  638. }
  639. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  640. again2:
  641. hlist_for_each_entry(pol, entry,
  642. xfrm_policy_bydst[dir].table + i,
  643. bydst) {
  644. if (pol->type != type)
  645. continue;
  646. hlist_del(&pol->bydst);
  647. hlist_del(&pol->byidx);
  648. write_unlock_bh(&xfrm_policy_lock);
  649. xfrm_audit_policy_delete(pol, 1,
  650. audit_info->loginuid,
  651. audit_info->secid);
  652. xfrm_policy_kill(pol);
  653. killed++;
  654. write_lock_bh(&xfrm_policy_lock);
  655. goto again2;
  656. }
  657. }
  658. xfrm_policy_count[dir] -= killed;
  659. }
  660. atomic_inc(&flow_cache_genid);
  661. out:
  662. write_unlock_bh(&xfrm_policy_lock);
  663. return err;
  664. }
  665. EXPORT_SYMBOL(xfrm_policy_flush);
  666. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  667. void *data)
  668. {
  669. struct xfrm_policy *pol, *last = NULL;
  670. struct hlist_node *entry;
  671. int dir, last_dir = 0, count, error;
  672. read_lock_bh(&xfrm_policy_lock);
  673. count = 0;
  674. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  675. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  676. int i;
  677. hlist_for_each_entry(pol, entry,
  678. &xfrm_policy_inexact[dir], bydst) {
  679. if (pol->type != type)
  680. continue;
  681. if (last) {
  682. error = func(last, last_dir % XFRM_POLICY_MAX,
  683. count, data);
  684. if (error)
  685. goto out;
  686. }
  687. last = pol;
  688. last_dir = dir;
  689. count++;
  690. }
  691. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  692. hlist_for_each_entry(pol, entry, table + i, bydst) {
  693. if (pol->type != type)
  694. continue;
  695. if (last) {
  696. error = func(last, last_dir % XFRM_POLICY_MAX,
  697. count, data);
  698. if (error)
  699. goto out;
  700. }
  701. last = pol;
  702. last_dir = dir;
  703. count++;
  704. }
  705. }
  706. }
  707. if (count == 0) {
  708. error = -ENOENT;
  709. goto out;
  710. }
  711. error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
  712. out:
  713. read_unlock_bh(&xfrm_policy_lock);
  714. return error;
  715. }
  716. EXPORT_SYMBOL(xfrm_policy_walk);
  717. /*
  718. * Find policy to apply to this flow.
  719. *
  720. * Returns 0 if policy found, else an -errno.
  721. */
  722. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  723. u8 type, u16 family, int dir)
  724. {
  725. struct xfrm_selector *sel = &pol->selector;
  726. int match, ret = -ESRCH;
  727. if (pol->family != family ||
  728. pol->type != type)
  729. return ret;
  730. match = xfrm_selector_match(sel, fl, family);
  731. if (match)
  732. ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
  733. return ret;
  734. }
  735. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  736. u16 family, u8 dir)
  737. {
  738. int err;
  739. struct xfrm_policy *pol, *ret;
  740. xfrm_address_t *daddr, *saddr;
  741. struct hlist_node *entry;
  742. struct hlist_head *chain;
  743. u32 priority = ~0U;
  744. daddr = xfrm_flowi_daddr(fl, family);
  745. saddr = xfrm_flowi_saddr(fl, family);
  746. if (unlikely(!daddr || !saddr))
  747. return NULL;
  748. read_lock_bh(&xfrm_policy_lock);
  749. chain = policy_hash_direct(daddr, saddr, family, dir);
  750. ret = NULL;
  751. hlist_for_each_entry(pol, entry, chain, bydst) {
  752. err = xfrm_policy_match(pol, fl, type, family, dir);
  753. if (err) {
  754. if (err == -ESRCH)
  755. continue;
  756. else {
  757. ret = ERR_PTR(err);
  758. goto fail;
  759. }
  760. } else {
  761. ret = pol;
  762. priority = ret->priority;
  763. break;
  764. }
  765. }
  766. chain = &xfrm_policy_inexact[dir];
  767. hlist_for_each_entry(pol, entry, chain, bydst) {
  768. err = xfrm_policy_match(pol, fl, type, family, dir);
  769. if (err) {
  770. if (err == -ESRCH)
  771. continue;
  772. else {
  773. ret = ERR_PTR(err);
  774. goto fail;
  775. }
  776. } else if (pol->priority < priority) {
  777. ret = pol;
  778. break;
  779. }
  780. }
  781. if (ret)
  782. xfrm_pol_hold(ret);
  783. fail:
  784. read_unlock_bh(&xfrm_policy_lock);
  785. return ret;
  786. }
  787. static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  788. void **objp, atomic_t **obj_refp)
  789. {
  790. struct xfrm_policy *pol;
  791. int err = 0;
  792. #ifdef CONFIG_XFRM_SUB_POLICY
  793. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  794. if (IS_ERR(pol)) {
  795. err = PTR_ERR(pol);
  796. pol = NULL;
  797. }
  798. if (pol || err)
  799. goto end;
  800. #endif
  801. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  802. if (IS_ERR(pol)) {
  803. err = PTR_ERR(pol);
  804. pol = NULL;
  805. }
  806. #ifdef CONFIG_XFRM_SUB_POLICY
  807. end:
  808. #endif
  809. if ((*objp = (void *) pol) != NULL)
  810. *obj_refp = &pol->refcnt;
  811. return err;
  812. }
  813. static inline int policy_to_flow_dir(int dir)
  814. {
  815. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  816. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  817. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  818. return dir;
  819. switch (dir) {
  820. default:
  821. case XFRM_POLICY_IN:
  822. return FLOW_DIR_IN;
  823. case XFRM_POLICY_OUT:
  824. return FLOW_DIR_OUT;
  825. case XFRM_POLICY_FWD:
  826. return FLOW_DIR_FWD;
  827. }
  828. }
  829. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  830. {
  831. struct xfrm_policy *pol;
  832. read_lock_bh(&xfrm_policy_lock);
  833. if ((pol = sk->sk_policy[dir]) != NULL) {
  834. int match = xfrm_selector_match(&pol->selector, fl,
  835. sk->sk_family);
  836. int err = 0;
  837. if (match) {
  838. err = security_xfrm_policy_lookup(pol, fl->secid,
  839. policy_to_flow_dir(dir));
  840. if (!err)
  841. xfrm_pol_hold(pol);
  842. else if (err == -ESRCH)
  843. pol = NULL;
  844. else
  845. pol = ERR_PTR(err);
  846. } else
  847. pol = NULL;
  848. }
  849. read_unlock_bh(&xfrm_policy_lock);
  850. return pol;
  851. }
  852. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  853. {
  854. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  855. pol->family, dir);
  856. hlist_add_head(&pol->bydst, chain);
  857. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  858. xfrm_policy_count[dir]++;
  859. xfrm_pol_hold(pol);
  860. if (xfrm_bydst_should_resize(dir, NULL))
  861. schedule_work(&xfrm_hash_work);
  862. }
  863. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  864. int dir)
  865. {
  866. if (hlist_unhashed(&pol->bydst))
  867. return NULL;
  868. hlist_del(&pol->bydst);
  869. hlist_del(&pol->byidx);
  870. xfrm_policy_count[dir]--;
  871. return pol;
  872. }
  873. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  874. {
  875. write_lock_bh(&xfrm_policy_lock);
  876. pol = __xfrm_policy_unlink(pol, dir);
  877. write_unlock_bh(&xfrm_policy_lock);
  878. if (pol) {
  879. if (dir < XFRM_POLICY_MAX)
  880. atomic_inc(&flow_cache_genid);
  881. xfrm_policy_kill(pol);
  882. return 0;
  883. }
  884. return -ENOENT;
  885. }
  886. EXPORT_SYMBOL(xfrm_policy_delete);
  887. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  888. {
  889. struct xfrm_policy *old_pol;
  890. #ifdef CONFIG_XFRM_SUB_POLICY
  891. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  892. return -EINVAL;
  893. #endif
  894. write_lock_bh(&xfrm_policy_lock);
  895. old_pol = sk->sk_policy[dir];
  896. sk->sk_policy[dir] = pol;
  897. if (pol) {
  898. pol->curlft.add_time = get_seconds();
  899. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  900. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  901. }
  902. if (old_pol)
  903. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  904. write_unlock_bh(&xfrm_policy_lock);
  905. if (old_pol) {
  906. xfrm_policy_kill(old_pol);
  907. }
  908. return 0;
  909. }
  910. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  911. {
  912. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  913. if (newp) {
  914. newp->selector = old->selector;
  915. if (security_xfrm_policy_clone(old, newp)) {
  916. kfree(newp);
  917. return NULL; /* ENOMEM */
  918. }
  919. newp->lft = old->lft;
  920. newp->curlft = old->curlft;
  921. newp->action = old->action;
  922. newp->flags = old->flags;
  923. newp->xfrm_nr = old->xfrm_nr;
  924. newp->index = old->index;
  925. newp->type = old->type;
  926. memcpy(newp->xfrm_vec, old->xfrm_vec,
  927. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  928. write_lock_bh(&xfrm_policy_lock);
  929. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  930. write_unlock_bh(&xfrm_policy_lock);
  931. xfrm_pol_put(newp);
  932. }
  933. return newp;
  934. }
  935. int __xfrm_sk_clone_policy(struct sock *sk)
  936. {
  937. struct xfrm_policy *p0 = sk->sk_policy[0],
  938. *p1 = sk->sk_policy[1];
  939. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  940. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  941. return -ENOMEM;
  942. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  943. return -ENOMEM;
  944. return 0;
  945. }
  946. static int
  947. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  948. unsigned short family)
  949. {
  950. int err;
  951. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  952. if (unlikely(afinfo == NULL))
  953. return -EINVAL;
  954. err = afinfo->get_saddr(local, remote);
  955. xfrm_policy_put_afinfo(afinfo);
  956. return err;
  957. }
  958. /* Resolve list of templates for the flow, given policy. */
  959. static int
  960. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  961. struct xfrm_state **xfrm,
  962. unsigned short family)
  963. {
  964. int nx;
  965. int i, error;
  966. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  967. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  968. xfrm_address_t tmp;
  969. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  970. struct xfrm_state *x;
  971. xfrm_address_t *remote = daddr;
  972. xfrm_address_t *local = saddr;
  973. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  974. if (tmpl->mode == XFRM_MODE_TUNNEL ||
  975. tmpl->mode == XFRM_MODE_BEET) {
  976. remote = &tmpl->id.daddr;
  977. local = &tmpl->saddr;
  978. family = tmpl->encap_family;
  979. if (xfrm_addr_any(local, family)) {
  980. error = xfrm_get_saddr(&tmp, remote, family);
  981. if (error)
  982. goto fail;
  983. local = &tmp;
  984. }
  985. }
  986. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  987. if (x && x->km.state == XFRM_STATE_VALID) {
  988. xfrm[nx++] = x;
  989. daddr = remote;
  990. saddr = local;
  991. continue;
  992. }
  993. if (x) {
  994. error = (x->km.state == XFRM_STATE_ERROR ?
  995. -EINVAL : -EAGAIN);
  996. xfrm_state_put(x);
  997. }
  998. if (!tmpl->optional)
  999. goto fail;
  1000. }
  1001. return nx;
  1002. fail:
  1003. for (nx--; nx>=0; nx--)
  1004. xfrm_state_put(xfrm[nx]);
  1005. return error;
  1006. }
  1007. static int
  1008. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1009. struct xfrm_state **xfrm,
  1010. unsigned short family)
  1011. {
  1012. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1013. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1014. int cnx = 0;
  1015. int error;
  1016. int ret;
  1017. int i;
  1018. for (i = 0; i < npols; i++) {
  1019. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1020. error = -ENOBUFS;
  1021. goto fail;
  1022. }
  1023. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1024. if (ret < 0) {
  1025. error = ret;
  1026. goto fail;
  1027. } else
  1028. cnx += ret;
  1029. }
  1030. /* found states are sorted for outbound processing */
  1031. if (npols > 1)
  1032. xfrm_state_sort(xfrm, tpp, cnx, family);
  1033. return cnx;
  1034. fail:
  1035. for (cnx--; cnx>=0; cnx--)
  1036. xfrm_state_put(tpp[cnx]);
  1037. return error;
  1038. }
  1039. /* Check that the bundle accepts the flow and its components are
  1040. * still valid.
  1041. */
  1042. static struct dst_entry *
  1043. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1044. {
  1045. struct dst_entry *x;
  1046. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1047. if (unlikely(afinfo == NULL))
  1048. return ERR_PTR(-EINVAL);
  1049. x = afinfo->find_bundle(fl, policy);
  1050. xfrm_policy_put_afinfo(afinfo);
  1051. return x;
  1052. }
  1053. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1054. * all the metrics... Shortly, bundle a bundle.
  1055. */
  1056. static int
  1057. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1058. struct flowi *fl, struct dst_entry **dst_p,
  1059. unsigned short family)
  1060. {
  1061. int err;
  1062. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1063. if (unlikely(afinfo == NULL))
  1064. return -EINVAL;
  1065. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1066. xfrm_policy_put_afinfo(afinfo);
  1067. return err;
  1068. }
  1069. static int inline
  1070. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1071. {
  1072. if (!*target) {
  1073. *target = kmalloc(size, GFP_ATOMIC);
  1074. if (!*target)
  1075. return -ENOMEM;
  1076. }
  1077. memcpy(*target, src, size);
  1078. return 0;
  1079. }
  1080. static int inline
  1081. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1082. {
  1083. #ifdef CONFIG_XFRM_SUB_POLICY
  1084. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1085. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1086. sel, sizeof(*sel));
  1087. #else
  1088. return 0;
  1089. #endif
  1090. }
  1091. static int inline
  1092. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1093. {
  1094. #ifdef CONFIG_XFRM_SUB_POLICY
  1095. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1096. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1097. #else
  1098. return 0;
  1099. #endif
  1100. }
  1101. static int stale_bundle(struct dst_entry *dst);
  1102. /* Main function: finds/creates a bundle for given flow.
  1103. *
  1104. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1105. * on interfaces with disabled IPsec.
  1106. */
  1107. int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1108. struct sock *sk, int flags)
  1109. {
  1110. struct xfrm_policy *policy;
  1111. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1112. int npols;
  1113. int pol_dead;
  1114. int xfrm_nr;
  1115. int pi;
  1116. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1117. struct dst_entry *dst, *dst_orig = *dst_p;
  1118. int nx = 0;
  1119. int err;
  1120. u32 genid;
  1121. u16 family;
  1122. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1123. restart:
  1124. genid = atomic_read(&flow_cache_genid);
  1125. policy = NULL;
  1126. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1127. pols[pi] = NULL;
  1128. npols = 0;
  1129. pol_dead = 0;
  1130. xfrm_nr = 0;
  1131. if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
  1132. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1133. if (IS_ERR(policy))
  1134. return PTR_ERR(policy);
  1135. }
  1136. if (!policy) {
  1137. /* To accelerate a bit... */
  1138. if ((dst_orig->flags & DST_NOXFRM) ||
  1139. !xfrm_policy_count[XFRM_POLICY_OUT])
  1140. return 0;
  1141. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1142. dir, xfrm_policy_lookup);
  1143. if (IS_ERR(policy))
  1144. return PTR_ERR(policy);
  1145. }
  1146. if (!policy)
  1147. return 0;
  1148. family = dst_orig->ops->family;
  1149. policy->curlft.use_time = get_seconds();
  1150. pols[0] = policy;
  1151. npols ++;
  1152. xfrm_nr += pols[0]->xfrm_nr;
  1153. switch (policy->action) {
  1154. case XFRM_POLICY_BLOCK:
  1155. /* Prohibit the flow */
  1156. err = -EPERM;
  1157. goto error;
  1158. case XFRM_POLICY_ALLOW:
  1159. #ifndef CONFIG_XFRM_SUB_POLICY
  1160. if (policy->xfrm_nr == 0) {
  1161. /* Flow passes not transformed. */
  1162. xfrm_pol_put(policy);
  1163. return 0;
  1164. }
  1165. #endif
  1166. /* Try to find matching bundle.
  1167. *
  1168. * LATER: help from flow cache. It is optional, this
  1169. * is required only for output policy.
  1170. */
  1171. dst = xfrm_find_bundle(fl, policy, family);
  1172. if (IS_ERR(dst)) {
  1173. err = PTR_ERR(dst);
  1174. goto error;
  1175. }
  1176. if (dst)
  1177. break;
  1178. #ifdef CONFIG_XFRM_SUB_POLICY
  1179. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1180. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1181. fl, family,
  1182. XFRM_POLICY_OUT);
  1183. if (pols[1]) {
  1184. if (IS_ERR(pols[1])) {
  1185. err = PTR_ERR(pols[1]);
  1186. goto error;
  1187. }
  1188. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1189. err = -EPERM;
  1190. goto error;
  1191. }
  1192. npols ++;
  1193. xfrm_nr += pols[1]->xfrm_nr;
  1194. }
  1195. }
  1196. /*
  1197. * Because neither flowi nor bundle information knows about
  1198. * transformation template size. On more than one policy usage
  1199. * we can realize whether all of them is bypass or not after
  1200. * they are searched. See above not-transformed bypass
  1201. * is surrounded by non-sub policy configuration, too.
  1202. */
  1203. if (xfrm_nr == 0) {
  1204. /* Flow passes not transformed. */
  1205. xfrm_pols_put(pols, npols);
  1206. return 0;
  1207. }
  1208. #endif
  1209. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1210. if (unlikely(nx<0)) {
  1211. err = nx;
  1212. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1213. /* EREMOTE tells the caller to generate
  1214. * a one-shot blackhole route.
  1215. */
  1216. xfrm_pol_put(policy);
  1217. return -EREMOTE;
  1218. }
  1219. if (err == -EAGAIN && flags) {
  1220. DECLARE_WAITQUEUE(wait, current);
  1221. add_wait_queue(&km_waitq, &wait);
  1222. set_current_state(TASK_INTERRUPTIBLE);
  1223. schedule();
  1224. set_current_state(TASK_RUNNING);
  1225. remove_wait_queue(&km_waitq, &wait);
  1226. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1227. if (nx == -EAGAIN && signal_pending(current)) {
  1228. err = -ERESTART;
  1229. goto error;
  1230. }
  1231. if (nx == -EAGAIN ||
  1232. genid != atomic_read(&flow_cache_genid)) {
  1233. xfrm_pols_put(pols, npols);
  1234. goto restart;
  1235. }
  1236. err = nx;
  1237. }
  1238. if (err < 0)
  1239. goto error;
  1240. }
  1241. if (nx == 0) {
  1242. /* Flow passes not transformed. */
  1243. xfrm_pols_put(pols, npols);
  1244. return 0;
  1245. }
  1246. dst = dst_orig;
  1247. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1248. if (unlikely(err)) {
  1249. int i;
  1250. for (i=0; i<nx; i++)
  1251. xfrm_state_put(xfrm[i]);
  1252. goto error;
  1253. }
  1254. for (pi = 0; pi < npols; pi++) {
  1255. read_lock_bh(&pols[pi]->lock);
  1256. pol_dead |= pols[pi]->dead;
  1257. read_unlock_bh(&pols[pi]->lock);
  1258. }
  1259. write_lock_bh(&policy->lock);
  1260. if (unlikely(pol_dead || stale_bundle(dst))) {
  1261. /* Wow! While we worked on resolving, this
  1262. * policy has gone. Retry. It is not paranoia,
  1263. * we just cannot enlist new bundle to dead object.
  1264. * We can't enlist stable bundles either.
  1265. */
  1266. write_unlock_bh(&policy->lock);
  1267. if (dst)
  1268. dst_free(dst);
  1269. err = -EHOSTUNREACH;
  1270. goto error;
  1271. }
  1272. if (npols > 1)
  1273. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1274. else
  1275. err = xfrm_dst_update_origin(dst, fl);
  1276. if (unlikely(err)) {
  1277. write_unlock_bh(&policy->lock);
  1278. if (dst)
  1279. dst_free(dst);
  1280. goto error;
  1281. }
  1282. dst->next = policy->bundles;
  1283. policy->bundles = dst;
  1284. dst_hold(dst);
  1285. write_unlock_bh(&policy->lock);
  1286. }
  1287. *dst_p = dst;
  1288. dst_release(dst_orig);
  1289. xfrm_pols_put(pols, npols);
  1290. return 0;
  1291. error:
  1292. dst_release(dst_orig);
  1293. xfrm_pols_put(pols, npols);
  1294. *dst_p = NULL;
  1295. return err;
  1296. }
  1297. EXPORT_SYMBOL(__xfrm_lookup);
  1298. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1299. struct sock *sk, int flags)
  1300. {
  1301. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1302. if (err == -EREMOTE) {
  1303. dst_release(*dst_p);
  1304. *dst_p = NULL;
  1305. err = -EAGAIN;
  1306. }
  1307. return err;
  1308. }
  1309. EXPORT_SYMBOL(xfrm_lookup);
  1310. static inline int
  1311. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1312. {
  1313. struct xfrm_state *x;
  1314. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1315. return 0;
  1316. x = skb->sp->xvec[idx];
  1317. if (!x->type->reject)
  1318. return 0;
  1319. return x->type->reject(x, skb, fl);
  1320. }
  1321. /* When skb is transformed back to its "native" form, we have to
  1322. * check policy restrictions. At the moment we make this in maximally
  1323. * stupid way. Shame on me. :-) Of course, connected sockets must
  1324. * have policy cached at them.
  1325. */
  1326. static inline int
  1327. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1328. unsigned short family)
  1329. {
  1330. if (xfrm_state_kern(x))
  1331. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1332. return x->id.proto == tmpl->id.proto &&
  1333. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1334. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1335. x->props.mode == tmpl->mode &&
  1336. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1337. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1338. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1339. xfrm_state_addr_cmp(tmpl, x, family));
  1340. }
  1341. /*
  1342. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1343. * because of optional transport mode, or next index of the mathced secpath
  1344. * state with the template.
  1345. * -1 is returned when no matching template is found.
  1346. * Otherwise "-2 - errored_index" is returned.
  1347. */
  1348. static inline int
  1349. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1350. unsigned short family)
  1351. {
  1352. int idx = start;
  1353. if (tmpl->optional) {
  1354. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1355. return start;
  1356. } else
  1357. start = -1;
  1358. for (; idx < sp->len; idx++) {
  1359. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1360. return ++idx;
  1361. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1362. if (start == -1)
  1363. start = -2-idx;
  1364. break;
  1365. }
  1366. }
  1367. return start;
  1368. }
  1369. int
  1370. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1371. {
  1372. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1373. int err;
  1374. if (unlikely(afinfo == NULL))
  1375. return -EAFNOSUPPORT;
  1376. afinfo->decode_session(skb, fl);
  1377. err = security_xfrm_decode_session(skb, &fl->secid);
  1378. xfrm_policy_put_afinfo(afinfo);
  1379. return err;
  1380. }
  1381. EXPORT_SYMBOL(xfrm_decode_session);
  1382. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1383. {
  1384. for (; k < sp->len; k++) {
  1385. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1386. *idxp = k;
  1387. return 1;
  1388. }
  1389. }
  1390. return 0;
  1391. }
  1392. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1393. unsigned short family)
  1394. {
  1395. struct xfrm_policy *pol;
  1396. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1397. int npols = 0;
  1398. int xfrm_nr;
  1399. int pi;
  1400. struct flowi fl;
  1401. u8 fl_dir = policy_to_flow_dir(dir);
  1402. int xerr_idx = -1;
  1403. if (xfrm_decode_session(skb, &fl, family) < 0)
  1404. return 0;
  1405. nf_nat_decode_session(skb, &fl, family);
  1406. /* First, check used SA against their selectors. */
  1407. if (skb->sp) {
  1408. int i;
  1409. for (i=skb->sp->len-1; i>=0; i--) {
  1410. struct xfrm_state *x = skb->sp->xvec[i];
  1411. if (!xfrm_selector_match(&x->sel, &fl, family))
  1412. return 0;
  1413. }
  1414. }
  1415. pol = NULL;
  1416. if (sk && sk->sk_policy[dir]) {
  1417. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1418. if (IS_ERR(pol))
  1419. return 0;
  1420. }
  1421. if (!pol)
  1422. pol = flow_cache_lookup(&fl, family, fl_dir,
  1423. xfrm_policy_lookup);
  1424. if (IS_ERR(pol))
  1425. return 0;
  1426. if (!pol) {
  1427. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1428. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1429. return 0;
  1430. }
  1431. return 1;
  1432. }
  1433. pol->curlft.use_time = get_seconds();
  1434. pols[0] = pol;
  1435. npols ++;
  1436. #ifdef CONFIG_XFRM_SUB_POLICY
  1437. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1438. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1439. &fl, family,
  1440. XFRM_POLICY_IN);
  1441. if (pols[1]) {
  1442. if (IS_ERR(pols[1]))
  1443. return 0;
  1444. pols[1]->curlft.use_time = get_seconds();
  1445. npols ++;
  1446. }
  1447. }
  1448. #endif
  1449. if (pol->action == XFRM_POLICY_ALLOW) {
  1450. struct sec_path *sp;
  1451. static struct sec_path dummy;
  1452. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1453. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1454. struct xfrm_tmpl **tpp = tp;
  1455. int ti = 0;
  1456. int i, k;
  1457. if ((sp = skb->sp) == NULL)
  1458. sp = &dummy;
  1459. for (pi = 0; pi < npols; pi++) {
  1460. if (pols[pi] != pol &&
  1461. pols[pi]->action != XFRM_POLICY_ALLOW)
  1462. goto reject;
  1463. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1464. goto reject_error;
  1465. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1466. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1467. }
  1468. xfrm_nr = ti;
  1469. if (npols > 1) {
  1470. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1471. tpp = stp;
  1472. }
  1473. /* For each tunnel xfrm, find the first matching tmpl.
  1474. * For each tmpl before that, find corresponding xfrm.
  1475. * Order is _important_. Later we will implement
  1476. * some barriers, but at the moment barriers
  1477. * are implied between each two transformations.
  1478. */
  1479. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1480. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1481. if (k < 0) {
  1482. if (k < -1)
  1483. /* "-2 - errored_index" returned */
  1484. xerr_idx = -(2+k);
  1485. goto reject;
  1486. }
  1487. }
  1488. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1489. goto reject;
  1490. xfrm_pols_put(pols, npols);
  1491. return 1;
  1492. }
  1493. reject:
  1494. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1495. reject_error:
  1496. xfrm_pols_put(pols, npols);
  1497. return 0;
  1498. }
  1499. EXPORT_SYMBOL(__xfrm_policy_check);
  1500. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1501. {
  1502. struct flowi fl;
  1503. if (xfrm_decode_session(skb, &fl, family) < 0)
  1504. return 0;
  1505. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1506. }
  1507. EXPORT_SYMBOL(__xfrm_route_forward);
  1508. /* Optimize later using cookies and generation ids. */
  1509. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1510. {
  1511. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1512. * to "-1" to force all XFRM destinations to get validated by
  1513. * dst_ops->check on every use. We do this because when a
  1514. * normal route referenced by an XFRM dst is obsoleted we do
  1515. * not go looking around for all parent referencing XFRM dsts
  1516. * so that we can invalidate them. It is just too much work.
  1517. * Instead we make the checks here on every use. For example:
  1518. *
  1519. * XFRM dst A --> IPv4 dst X
  1520. *
  1521. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1522. * in this example). If X is marked obsolete, "A" will not
  1523. * notice. That's what we are validating here via the
  1524. * stale_bundle() check.
  1525. *
  1526. * When a policy's bundle is pruned, we dst_free() the XFRM
  1527. * dst which causes it's ->obsolete field to be set to a
  1528. * positive non-zero integer. If an XFRM dst has been pruned
  1529. * like this, we want to force a new route lookup.
  1530. */
  1531. if (dst->obsolete < 0 && !stale_bundle(dst))
  1532. return dst;
  1533. return NULL;
  1534. }
  1535. static int stale_bundle(struct dst_entry *dst)
  1536. {
  1537. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1538. }
  1539. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1540. {
  1541. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1542. dst->dev = init_net.loopback_dev;
  1543. dev_hold(dst->dev);
  1544. dev_put(dev);
  1545. }
  1546. }
  1547. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1548. static void xfrm_link_failure(struct sk_buff *skb)
  1549. {
  1550. /* Impossible. Such dst must be popped before reaches point of failure. */
  1551. return;
  1552. }
  1553. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1554. {
  1555. if (dst) {
  1556. if (dst->obsolete) {
  1557. dst_release(dst);
  1558. dst = NULL;
  1559. }
  1560. }
  1561. return dst;
  1562. }
  1563. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1564. {
  1565. struct dst_entry *dst, **dstp;
  1566. write_lock(&pol->lock);
  1567. dstp = &pol->bundles;
  1568. while ((dst=*dstp) != NULL) {
  1569. if (func(dst)) {
  1570. *dstp = dst->next;
  1571. dst->next = *gc_list_p;
  1572. *gc_list_p = dst;
  1573. } else {
  1574. dstp = &dst->next;
  1575. }
  1576. }
  1577. write_unlock(&pol->lock);
  1578. }
  1579. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1580. {
  1581. struct dst_entry *gc_list = NULL;
  1582. int dir;
  1583. read_lock_bh(&xfrm_policy_lock);
  1584. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1585. struct xfrm_policy *pol;
  1586. struct hlist_node *entry;
  1587. struct hlist_head *table;
  1588. int i;
  1589. hlist_for_each_entry(pol, entry,
  1590. &xfrm_policy_inexact[dir], bydst)
  1591. prune_one_bundle(pol, func, &gc_list);
  1592. table = xfrm_policy_bydst[dir].table;
  1593. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1594. hlist_for_each_entry(pol, entry, table + i, bydst)
  1595. prune_one_bundle(pol, func, &gc_list);
  1596. }
  1597. }
  1598. read_unlock_bh(&xfrm_policy_lock);
  1599. while (gc_list) {
  1600. struct dst_entry *dst = gc_list;
  1601. gc_list = dst->next;
  1602. dst_free(dst);
  1603. }
  1604. }
  1605. static int unused_bundle(struct dst_entry *dst)
  1606. {
  1607. return !atomic_read(&dst->__refcnt);
  1608. }
  1609. static void __xfrm_garbage_collect(void)
  1610. {
  1611. xfrm_prune_bundles(unused_bundle);
  1612. }
  1613. static int xfrm_flush_bundles(void)
  1614. {
  1615. xfrm_prune_bundles(stale_bundle);
  1616. return 0;
  1617. }
  1618. void xfrm_init_pmtu(struct dst_entry *dst)
  1619. {
  1620. do {
  1621. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1622. u32 pmtu, route_mtu_cached;
  1623. pmtu = dst_mtu(dst->child);
  1624. xdst->child_mtu_cached = pmtu;
  1625. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1626. route_mtu_cached = dst_mtu(xdst->route);
  1627. xdst->route_mtu_cached = route_mtu_cached;
  1628. if (pmtu > route_mtu_cached)
  1629. pmtu = route_mtu_cached;
  1630. dst->metrics[RTAX_MTU-1] = pmtu;
  1631. } while ((dst = dst->next));
  1632. }
  1633. EXPORT_SYMBOL(xfrm_init_pmtu);
  1634. /* Check that the bundle accepts the flow and its components are
  1635. * still valid.
  1636. */
  1637. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1638. struct flowi *fl, int family, int strict)
  1639. {
  1640. struct dst_entry *dst = &first->u.dst;
  1641. struct xfrm_dst *last;
  1642. u32 mtu;
  1643. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1644. (dst->dev && !netif_running(dst->dev)))
  1645. return 0;
  1646. #ifdef CONFIG_XFRM_SUB_POLICY
  1647. if (fl) {
  1648. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1649. return 0;
  1650. if (first->partner &&
  1651. !xfrm_selector_match(first->partner, fl, family))
  1652. return 0;
  1653. }
  1654. #endif
  1655. last = NULL;
  1656. do {
  1657. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1658. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1659. return 0;
  1660. if (fl && pol &&
  1661. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1662. return 0;
  1663. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1664. return 0;
  1665. if (xdst->genid != dst->xfrm->genid)
  1666. return 0;
  1667. if (strict && fl &&
  1668. !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1669. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1670. return 0;
  1671. mtu = dst_mtu(dst->child);
  1672. if (xdst->child_mtu_cached != mtu) {
  1673. last = xdst;
  1674. xdst->child_mtu_cached = mtu;
  1675. }
  1676. if (!dst_check(xdst->route, xdst->route_cookie))
  1677. return 0;
  1678. mtu = dst_mtu(xdst->route);
  1679. if (xdst->route_mtu_cached != mtu) {
  1680. last = xdst;
  1681. xdst->route_mtu_cached = mtu;
  1682. }
  1683. dst = dst->child;
  1684. } while (dst->xfrm);
  1685. if (likely(!last))
  1686. return 1;
  1687. mtu = last->child_mtu_cached;
  1688. for (;;) {
  1689. dst = &last->u.dst;
  1690. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1691. if (mtu > last->route_mtu_cached)
  1692. mtu = last->route_mtu_cached;
  1693. dst->metrics[RTAX_MTU-1] = mtu;
  1694. if (last == first)
  1695. break;
  1696. last = (struct xfrm_dst *)last->u.dst.next;
  1697. last->child_mtu_cached = mtu;
  1698. }
  1699. return 1;
  1700. }
  1701. EXPORT_SYMBOL(xfrm_bundle_ok);
  1702. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1703. {
  1704. int err = 0;
  1705. if (unlikely(afinfo == NULL))
  1706. return -EINVAL;
  1707. if (unlikely(afinfo->family >= NPROTO))
  1708. return -EAFNOSUPPORT;
  1709. write_lock_bh(&xfrm_policy_afinfo_lock);
  1710. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1711. err = -ENOBUFS;
  1712. else {
  1713. struct dst_ops *dst_ops = afinfo->dst_ops;
  1714. if (likely(dst_ops->kmem_cachep == NULL))
  1715. dst_ops->kmem_cachep = xfrm_dst_cache;
  1716. if (likely(dst_ops->check == NULL))
  1717. dst_ops->check = xfrm_dst_check;
  1718. if (likely(dst_ops->negative_advice == NULL))
  1719. dst_ops->negative_advice = xfrm_negative_advice;
  1720. if (likely(dst_ops->link_failure == NULL))
  1721. dst_ops->link_failure = xfrm_link_failure;
  1722. if (likely(afinfo->garbage_collect == NULL))
  1723. afinfo->garbage_collect = __xfrm_garbage_collect;
  1724. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1725. }
  1726. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1727. return err;
  1728. }
  1729. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1730. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1731. {
  1732. int err = 0;
  1733. if (unlikely(afinfo == NULL))
  1734. return -EINVAL;
  1735. if (unlikely(afinfo->family >= NPROTO))
  1736. return -EAFNOSUPPORT;
  1737. write_lock_bh(&xfrm_policy_afinfo_lock);
  1738. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1739. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1740. err = -EINVAL;
  1741. else {
  1742. struct dst_ops *dst_ops = afinfo->dst_ops;
  1743. xfrm_policy_afinfo[afinfo->family] = NULL;
  1744. dst_ops->kmem_cachep = NULL;
  1745. dst_ops->check = NULL;
  1746. dst_ops->negative_advice = NULL;
  1747. dst_ops->link_failure = NULL;
  1748. afinfo->garbage_collect = NULL;
  1749. }
  1750. }
  1751. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1752. return err;
  1753. }
  1754. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1755. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1756. {
  1757. struct xfrm_policy_afinfo *afinfo;
  1758. if (unlikely(family >= NPROTO))
  1759. return NULL;
  1760. read_lock(&xfrm_policy_afinfo_lock);
  1761. afinfo = xfrm_policy_afinfo[family];
  1762. if (unlikely(!afinfo))
  1763. read_unlock(&xfrm_policy_afinfo_lock);
  1764. return afinfo;
  1765. }
  1766. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1767. {
  1768. read_unlock(&xfrm_policy_afinfo_lock);
  1769. }
  1770. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1771. {
  1772. struct net_device *dev = ptr;
  1773. if (dev->nd_net != &init_net)
  1774. return NOTIFY_DONE;
  1775. switch (event) {
  1776. case NETDEV_DOWN:
  1777. xfrm_flush_bundles();
  1778. }
  1779. return NOTIFY_DONE;
  1780. }
  1781. static struct notifier_block xfrm_dev_notifier = {
  1782. xfrm_dev_event,
  1783. NULL,
  1784. 0
  1785. };
  1786. static void __init xfrm_policy_init(void)
  1787. {
  1788. unsigned int hmask, sz;
  1789. int dir;
  1790. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1791. sizeof(struct xfrm_dst),
  1792. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1793. NULL);
  1794. hmask = 8 - 1;
  1795. sz = (hmask+1) * sizeof(struct hlist_head);
  1796. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1797. xfrm_idx_hmask = hmask;
  1798. if (!xfrm_policy_byidx)
  1799. panic("XFRM: failed to allocate byidx hash\n");
  1800. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1801. struct xfrm_policy_hash *htab;
  1802. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1803. htab = &xfrm_policy_bydst[dir];
  1804. htab->table = xfrm_hash_alloc(sz);
  1805. htab->hmask = hmask;
  1806. if (!htab->table)
  1807. panic("XFRM: failed to allocate bydst hash\n");
  1808. }
  1809. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  1810. register_netdevice_notifier(&xfrm_dev_notifier);
  1811. }
  1812. void __init xfrm_init(void)
  1813. {
  1814. xfrm_state_init();
  1815. xfrm_policy_init();
  1816. xfrm_input_init();
  1817. }
  1818. #ifdef CONFIG_AUDITSYSCALL
  1819. static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  1820. struct audit_buffer *audit_buf)
  1821. {
  1822. if (xp->security)
  1823. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1824. xp->security->ctx_alg, xp->security->ctx_doi,
  1825. xp->security->ctx_str);
  1826. switch(xp->selector.family) {
  1827. case AF_INET:
  1828. audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
  1829. NIPQUAD(xp->selector.saddr.a4),
  1830. NIPQUAD(xp->selector.daddr.a4));
  1831. break;
  1832. case AF_INET6:
  1833. {
  1834. struct in6_addr saddr6, daddr6;
  1835. memcpy(&saddr6, xp->selector.saddr.a6,
  1836. sizeof(struct in6_addr));
  1837. memcpy(&daddr6, xp->selector.daddr.a6,
  1838. sizeof(struct in6_addr));
  1839. audit_log_format(audit_buf,
  1840. " src=" NIP6_FMT " dst=" NIP6_FMT,
  1841. NIP6(saddr6), NIP6(daddr6));
  1842. }
  1843. break;
  1844. }
  1845. }
  1846. void
  1847. xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1848. {
  1849. struct audit_buffer *audit_buf;
  1850. extern int audit_enabled;
  1851. if (audit_enabled == 0)
  1852. return;
  1853. audit_buf = xfrm_audit_start(sid, auid);
  1854. if (audit_buf == NULL)
  1855. return;
  1856. audit_log_format(audit_buf, " op=SPD-add res=%u", result);
  1857. xfrm_audit_common_policyinfo(xp, audit_buf);
  1858. audit_log_end(audit_buf);
  1859. }
  1860. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  1861. void
  1862. xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1863. {
  1864. struct audit_buffer *audit_buf;
  1865. extern int audit_enabled;
  1866. if (audit_enabled == 0)
  1867. return;
  1868. audit_buf = xfrm_audit_start(sid, auid);
  1869. if (audit_buf == NULL)
  1870. return;
  1871. audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
  1872. xfrm_audit_common_policyinfo(xp, audit_buf);
  1873. audit_log_end(audit_buf);
  1874. }
  1875. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  1876. #endif
  1877. #ifdef CONFIG_XFRM_MIGRATE
  1878. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  1879. struct xfrm_selector *sel_tgt)
  1880. {
  1881. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  1882. if (sel_tgt->family == sel_cmp->family &&
  1883. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  1884. sel_cmp->family) == 0 &&
  1885. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  1886. sel_cmp->family) == 0 &&
  1887. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  1888. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  1889. return 1;
  1890. }
  1891. } else {
  1892. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  1893. return 1;
  1894. }
  1895. }
  1896. return 0;
  1897. }
  1898. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  1899. u8 dir, u8 type)
  1900. {
  1901. struct xfrm_policy *pol, *ret = NULL;
  1902. struct hlist_node *entry;
  1903. struct hlist_head *chain;
  1904. u32 priority = ~0U;
  1905. read_lock_bh(&xfrm_policy_lock);
  1906. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  1907. hlist_for_each_entry(pol, entry, chain, bydst) {
  1908. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1909. pol->type == type) {
  1910. ret = pol;
  1911. priority = ret->priority;
  1912. break;
  1913. }
  1914. }
  1915. chain = &xfrm_policy_inexact[dir];
  1916. hlist_for_each_entry(pol, entry, chain, bydst) {
  1917. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  1918. pol->type == type &&
  1919. pol->priority < priority) {
  1920. ret = pol;
  1921. break;
  1922. }
  1923. }
  1924. if (ret)
  1925. xfrm_pol_hold(ret);
  1926. read_unlock_bh(&xfrm_policy_lock);
  1927. return ret;
  1928. }
  1929. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  1930. {
  1931. int match = 0;
  1932. if (t->mode == m->mode && t->id.proto == m->proto &&
  1933. (m->reqid == 0 || t->reqid == m->reqid)) {
  1934. switch (t->mode) {
  1935. case XFRM_MODE_TUNNEL:
  1936. case XFRM_MODE_BEET:
  1937. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  1938. m->old_family) == 0 &&
  1939. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  1940. m->old_family) == 0) {
  1941. match = 1;
  1942. }
  1943. break;
  1944. case XFRM_MODE_TRANSPORT:
  1945. /* in case of transport mode, template does not store
  1946. any IP addresses, hence we just compare mode and
  1947. protocol */
  1948. match = 1;
  1949. break;
  1950. default:
  1951. break;
  1952. }
  1953. }
  1954. return match;
  1955. }
  1956. /* update endpoint address(es) of template(s) */
  1957. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  1958. struct xfrm_migrate *m, int num_migrate)
  1959. {
  1960. struct xfrm_migrate *mp;
  1961. struct dst_entry *dst;
  1962. int i, j, n = 0;
  1963. write_lock_bh(&pol->lock);
  1964. if (unlikely(pol->dead)) {
  1965. /* target policy has been deleted */
  1966. write_unlock_bh(&pol->lock);
  1967. return -ENOENT;
  1968. }
  1969. for (i = 0; i < pol->xfrm_nr; i++) {
  1970. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  1971. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  1972. continue;
  1973. n++;
  1974. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
  1975. pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
  1976. continue;
  1977. /* update endpoints */
  1978. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  1979. sizeof(pol->xfrm_vec[i].id.daddr));
  1980. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  1981. sizeof(pol->xfrm_vec[i].saddr));
  1982. pol->xfrm_vec[i].encap_family = mp->new_family;
  1983. /* flush bundles */
  1984. while ((dst = pol->bundles) != NULL) {
  1985. pol->bundles = dst->next;
  1986. dst_free(dst);
  1987. }
  1988. }
  1989. }
  1990. write_unlock_bh(&pol->lock);
  1991. if (!n)
  1992. return -ENODATA;
  1993. return 0;
  1994. }
  1995. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  1996. {
  1997. int i, j;
  1998. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  1999. return -EINVAL;
  2000. for (i = 0; i < num_migrate; i++) {
  2001. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2002. m[i].old_family) == 0) &&
  2003. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2004. m[i].old_family) == 0))
  2005. return -EINVAL;
  2006. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2007. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2008. return -EINVAL;
  2009. /* check if there is any duplicated entry */
  2010. for (j = i + 1; j < num_migrate; j++) {
  2011. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2012. sizeof(m[i].old_daddr)) &&
  2013. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2014. sizeof(m[i].old_saddr)) &&
  2015. m[i].proto == m[j].proto &&
  2016. m[i].mode == m[j].mode &&
  2017. m[i].reqid == m[j].reqid &&
  2018. m[i].old_family == m[j].old_family)
  2019. return -EINVAL;
  2020. }
  2021. }
  2022. return 0;
  2023. }
  2024. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2025. struct xfrm_migrate *m, int num_migrate)
  2026. {
  2027. int i, err, nx_cur = 0, nx_new = 0;
  2028. struct xfrm_policy *pol = NULL;
  2029. struct xfrm_state *x, *xc;
  2030. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2031. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2032. struct xfrm_migrate *mp;
  2033. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2034. goto out;
  2035. /* Stage 1 - find policy */
  2036. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2037. err = -ENOENT;
  2038. goto out;
  2039. }
  2040. /* Stage 2 - find and update state(s) */
  2041. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2042. if ((x = xfrm_migrate_state_find(mp))) {
  2043. x_cur[nx_cur] = x;
  2044. nx_cur++;
  2045. if ((xc = xfrm_state_migrate(x, mp))) {
  2046. x_new[nx_new] = xc;
  2047. nx_new++;
  2048. } else {
  2049. err = -ENODATA;
  2050. goto restore_state;
  2051. }
  2052. }
  2053. }
  2054. /* Stage 3 - update policy */
  2055. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2056. goto restore_state;
  2057. /* Stage 4 - delete old state(s) */
  2058. if (nx_cur) {
  2059. xfrm_states_put(x_cur, nx_cur);
  2060. xfrm_states_delete(x_cur, nx_cur);
  2061. }
  2062. /* Stage 5 - announce */
  2063. km_migrate(sel, dir, type, m, num_migrate);
  2064. xfrm_pol_put(pol);
  2065. return 0;
  2066. out:
  2067. return err;
  2068. restore_state:
  2069. if (pol)
  2070. xfrm_pol_put(pol);
  2071. if (nx_cur)
  2072. xfrm_states_put(x_cur, nx_cur);
  2073. if (nx_new)
  2074. xfrm_states_delete(x_new, nx_new);
  2075. return err;
  2076. }
  2077. EXPORT_SYMBOL(xfrm_migrate);
  2078. #endif