xfrm_policy.c 56 KB

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