xfrm_policy.c 52 KB

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