xfrm_policy.c 57 KB

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