xfrm_policy.c 48 KB

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