xfrm_state.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. /*
  2. * xfrm_state.c
  3. *
  4. * Changes:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  8. * IPv6 support
  9. * YOSHIFUJI Hideaki @USAGI
  10. * Split up af-specific functions
  11. * Derek Atkins <derek@ihtfp.com>
  12. * Add UDP Encapsulation
  13. *
  14. */
  15. #include <linux/workqueue.h>
  16. #include <net/xfrm.h>
  17. #include <linux/pfkeyv2.h>
  18. #include <linux/ipsec.h>
  19. #include <linux/module.h>
  20. #include <linux/cache.h>
  21. #include <linux/audit.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/ktime.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kernel.h>
  27. #include "xfrm_hash.h"
  28. /* Each xfrm_state may be linked to two tables:
  29. 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
  30. 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
  31. destination/tunnel endpoint. (output)
  32. */
  33. static DEFINE_SPINLOCK(xfrm_state_lock);
  34. static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
  35. static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
  36. static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
  37. static inline unsigned int xfrm_dst_hash(struct net *net,
  38. const xfrm_address_t *daddr,
  39. const xfrm_address_t *saddr,
  40. u32 reqid,
  41. unsigned short family)
  42. {
  43. return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
  44. }
  45. static inline unsigned int xfrm_src_hash(struct net *net,
  46. const xfrm_address_t *daddr,
  47. const xfrm_address_t *saddr,
  48. unsigned short family)
  49. {
  50. return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
  51. }
  52. static inline unsigned int
  53. xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
  54. __be32 spi, u8 proto, unsigned short family)
  55. {
  56. return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
  57. }
  58. static void xfrm_hash_transfer(struct hlist_head *list,
  59. struct hlist_head *ndsttable,
  60. struct hlist_head *nsrctable,
  61. struct hlist_head *nspitable,
  62. unsigned int nhashmask)
  63. {
  64. struct hlist_node *tmp;
  65. struct xfrm_state *x;
  66. hlist_for_each_entry_safe(x, tmp, list, bydst) {
  67. unsigned int h;
  68. h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
  69. x->props.reqid, x->props.family,
  70. nhashmask);
  71. hlist_add_head(&x->bydst, ndsttable+h);
  72. h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
  73. x->props.family,
  74. nhashmask);
  75. hlist_add_head(&x->bysrc, nsrctable+h);
  76. if (x->id.spi) {
  77. h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
  78. x->id.proto, x->props.family,
  79. nhashmask);
  80. hlist_add_head(&x->byspi, nspitable+h);
  81. }
  82. }
  83. }
  84. static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
  85. {
  86. return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
  87. }
  88. static DEFINE_MUTEX(hash_resize_mutex);
  89. static void xfrm_hash_resize(struct work_struct *work)
  90. {
  91. struct net *net = container_of(work, struct net, xfrm.state_hash_work);
  92. struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
  93. unsigned long nsize, osize;
  94. unsigned int nhashmask, ohashmask;
  95. int i;
  96. mutex_lock(&hash_resize_mutex);
  97. nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
  98. ndst = xfrm_hash_alloc(nsize);
  99. if (!ndst)
  100. goto out_unlock;
  101. nsrc = xfrm_hash_alloc(nsize);
  102. if (!nsrc) {
  103. xfrm_hash_free(ndst, nsize);
  104. goto out_unlock;
  105. }
  106. nspi = xfrm_hash_alloc(nsize);
  107. if (!nspi) {
  108. xfrm_hash_free(ndst, nsize);
  109. xfrm_hash_free(nsrc, nsize);
  110. goto out_unlock;
  111. }
  112. spin_lock_bh(&xfrm_state_lock);
  113. nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
  114. for (i = net->xfrm.state_hmask; i >= 0; i--)
  115. xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
  116. nhashmask);
  117. odst = net->xfrm.state_bydst;
  118. osrc = net->xfrm.state_bysrc;
  119. ospi = net->xfrm.state_byspi;
  120. ohashmask = net->xfrm.state_hmask;
  121. net->xfrm.state_bydst = ndst;
  122. net->xfrm.state_bysrc = nsrc;
  123. net->xfrm.state_byspi = nspi;
  124. net->xfrm.state_hmask = nhashmask;
  125. spin_unlock_bh(&xfrm_state_lock);
  126. osize = (ohashmask + 1) * sizeof(struct hlist_head);
  127. xfrm_hash_free(odst, osize);
  128. xfrm_hash_free(osrc, osize);
  129. xfrm_hash_free(ospi, osize);
  130. out_unlock:
  131. mutex_unlock(&hash_resize_mutex);
  132. }
  133. static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
  134. static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
  135. static DEFINE_SPINLOCK(xfrm_state_gc_lock);
  136. int __xfrm_state_delete(struct xfrm_state *x);
  137. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
  138. void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
  139. static DEFINE_SPINLOCK(xfrm_type_lock);
  140. int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
  141. {
  142. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  143. const struct xfrm_type **typemap;
  144. int err = 0;
  145. if (unlikely(afinfo == NULL))
  146. return -EAFNOSUPPORT;
  147. typemap = afinfo->type_map;
  148. spin_lock_bh(&xfrm_type_lock);
  149. if (likely(typemap[type->proto] == NULL))
  150. typemap[type->proto] = type;
  151. else
  152. err = -EEXIST;
  153. spin_unlock_bh(&xfrm_type_lock);
  154. xfrm_state_put_afinfo(afinfo);
  155. return err;
  156. }
  157. EXPORT_SYMBOL(xfrm_register_type);
  158. int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
  159. {
  160. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  161. const struct xfrm_type **typemap;
  162. int err = 0;
  163. if (unlikely(afinfo == NULL))
  164. return -EAFNOSUPPORT;
  165. typemap = afinfo->type_map;
  166. spin_lock_bh(&xfrm_type_lock);
  167. if (unlikely(typemap[type->proto] != type))
  168. err = -ENOENT;
  169. else
  170. typemap[type->proto] = NULL;
  171. spin_unlock_bh(&xfrm_type_lock);
  172. xfrm_state_put_afinfo(afinfo);
  173. return err;
  174. }
  175. EXPORT_SYMBOL(xfrm_unregister_type);
  176. static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
  177. {
  178. struct xfrm_state_afinfo *afinfo;
  179. const struct xfrm_type **typemap;
  180. const struct xfrm_type *type;
  181. int modload_attempted = 0;
  182. retry:
  183. afinfo = xfrm_state_get_afinfo(family);
  184. if (unlikely(afinfo == NULL))
  185. return NULL;
  186. typemap = afinfo->type_map;
  187. type = typemap[proto];
  188. if (unlikely(type && !try_module_get(type->owner)))
  189. type = NULL;
  190. if (!type && !modload_attempted) {
  191. xfrm_state_put_afinfo(afinfo);
  192. request_module("xfrm-type-%d-%d", family, proto);
  193. modload_attempted = 1;
  194. goto retry;
  195. }
  196. xfrm_state_put_afinfo(afinfo);
  197. return type;
  198. }
  199. static void xfrm_put_type(const struct xfrm_type *type)
  200. {
  201. module_put(type->owner);
  202. }
  203. static DEFINE_SPINLOCK(xfrm_mode_lock);
  204. int xfrm_register_mode(struct xfrm_mode *mode, int family)
  205. {
  206. struct xfrm_state_afinfo *afinfo;
  207. struct xfrm_mode **modemap;
  208. int err;
  209. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  210. return -EINVAL;
  211. afinfo = xfrm_state_get_afinfo(family);
  212. if (unlikely(afinfo == NULL))
  213. return -EAFNOSUPPORT;
  214. err = -EEXIST;
  215. modemap = afinfo->mode_map;
  216. spin_lock_bh(&xfrm_mode_lock);
  217. if (modemap[mode->encap])
  218. goto out;
  219. err = -ENOENT;
  220. if (!try_module_get(afinfo->owner))
  221. goto out;
  222. mode->afinfo = afinfo;
  223. modemap[mode->encap] = mode;
  224. err = 0;
  225. out:
  226. spin_unlock_bh(&xfrm_mode_lock);
  227. xfrm_state_put_afinfo(afinfo);
  228. return err;
  229. }
  230. EXPORT_SYMBOL(xfrm_register_mode);
  231. int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
  232. {
  233. struct xfrm_state_afinfo *afinfo;
  234. struct xfrm_mode **modemap;
  235. int err;
  236. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  237. return -EINVAL;
  238. afinfo = xfrm_state_get_afinfo(family);
  239. if (unlikely(afinfo == NULL))
  240. return -EAFNOSUPPORT;
  241. err = -ENOENT;
  242. modemap = afinfo->mode_map;
  243. spin_lock_bh(&xfrm_mode_lock);
  244. if (likely(modemap[mode->encap] == mode)) {
  245. modemap[mode->encap] = NULL;
  246. module_put(mode->afinfo->owner);
  247. err = 0;
  248. }
  249. spin_unlock_bh(&xfrm_mode_lock);
  250. xfrm_state_put_afinfo(afinfo);
  251. return err;
  252. }
  253. EXPORT_SYMBOL(xfrm_unregister_mode);
  254. static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
  255. {
  256. struct xfrm_state_afinfo *afinfo;
  257. struct xfrm_mode *mode;
  258. int modload_attempted = 0;
  259. if (unlikely(encap >= XFRM_MODE_MAX))
  260. return NULL;
  261. retry:
  262. afinfo = xfrm_state_get_afinfo(family);
  263. if (unlikely(afinfo == NULL))
  264. return NULL;
  265. mode = afinfo->mode_map[encap];
  266. if (unlikely(mode && !try_module_get(mode->owner)))
  267. mode = NULL;
  268. if (!mode && !modload_attempted) {
  269. xfrm_state_put_afinfo(afinfo);
  270. request_module("xfrm-mode-%d-%d", family, encap);
  271. modload_attempted = 1;
  272. goto retry;
  273. }
  274. xfrm_state_put_afinfo(afinfo);
  275. return mode;
  276. }
  277. static void xfrm_put_mode(struct xfrm_mode *mode)
  278. {
  279. module_put(mode->owner);
  280. }
  281. static void xfrm_state_gc_destroy(struct xfrm_state *x)
  282. {
  283. tasklet_hrtimer_cancel(&x->mtimer);
  284. del_timer_sync(&x->rtimer);
  285. kfree(x->aalg);
  286. kfree(x->ealg);
  287. kfree(x->calg);
  288. kfree(x->encap);
  289. kfree(x->coaddr);
  290. kfree(x->replay_esn);
  291. kfree(x->preplay_esn);
  292. if (x->inner_mode)
  293. xfrm_put_mode(x->inner_mode);
  294. if (x->inner_mode_iaf)
  295. xfrm_put_mode(x->inner_mode_iaf);
  296. if (x->outer_mode)
  297. xfrm_put_mode(x->outer_mode);
  298. if (x->type) {
  299. x->type->destructor(x);
  300. xfrm_put_type(x->type);
  301. }
  302. security_xfrm_state_free(x);
  303. kfree(x);
  304. }
  305. static void xfrm_state_gc_task(struct work_struct *work)
  306. {
  307. struct net *net = container_of(work, struct net, xfrm.state_gc_work);
  308. struct xfrm_state *x;
  309. struct hlist_node *tmp;
  310. struct hlist_head gc_list;
  311. spin_lock_bh(&xfrm_state_gc_lock);
  312. hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
  313. spin_unlock_bh(&xfrm_state_gc_lock);
  314. hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
  315. xfrm_state_gc_destroy(x);
  316. wake_up(&net->xfrm.km_waitq);
  317. }
  318. static inline unsigned long make_jiffies(long secs)
  319. {
  320. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  321. return MAX_SCHEDULE_TIMEOUT-1;
  322. else
  323. return secs*HZ;
  324. }
  325. static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
  326. {
  327. struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
  328. struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
  329. struct net *net = xs_net(x);
  330. unsigned long now = get_seconds();
  331. long next = LONG_MAX;
  332. int warn = 0;
  333. int err = 0;
  334. spin_lock(&x->lock);
  335. if (x->km.state == XFRM_STATE_DEAD)
  336. goto out;
  337. if (x->km.state == XFRM_STATE_EXPIRED)
  338. goto expired;
  339. if (x->lft.hard_add_expires_seconds) {
  340. long tmo = x->lft.hard_add_expires_seconds +
  341. x->curlft.add_time - now;
  342. if (tmo <= 0) {
  343. if (x->xflags & XFRM_SOFT_EXPIRE) {
  344. /* enter hard expire without soft expire first?!
  345. * setting a new date could trigger this.
  346. * workarbound: fix x->curflt.add_time by below:
  347. */
  348. x->curlft.add_time = now - x->saved_tmo - 1;
  349. tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
  350. } else
  351. goto expired;
  352. }
  353. if (tmo < next)
  354. next = tmo;
  355. }
  356. if (x->lft.hard_use_expires_seconds) {
  357. long tmo = x->lft.hard_use_expires_seconds +
  358. (x->curlft.use_time ? : now) - now;
  359. if (tmo <= 0)
  360. goto expired;
  361. if (tmo < next)
  362. next = tmo;
  363. }
  364. if (x->km.dying)
  365. goto resched;
  366. if (x->lft.soft_add_expires_seconds) {
  367. long tmo = x->lft.soft_add_expires_seconds +
  368. x->curlft.add_time - now;
  369. if (tmo <= 0) {
  370. warn = 1;
  371. x->xflags &= ~XFRM_SOFT_EXPIRE;
  372. } else if (tmo < next) {
  373. next = tmo;
  374. x->xflags |= XFRM_SOFT_EXPIRE;
  375. x->saved_tmo = tmo;
  376. }
  377. }
  378. if (x->lft.soft_use_expires_seconds) {
  379. long tmo = x->lft.soft_use_expires_seconds +
  380. (x->curlft.use_time ? : now) - now;
  381. if (tmo <= 0)
  382. warn = 1;
  383. else if (tmo < next)
  384. next = tmo;
  385. }
  386. x->km.dying = warn;
  387. if (warn)
  388. km_state_expired(x, 0, 0);
  389. resched:
  390. if (next != LONG_MAX){
  391. tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
  392. }
  393. goto out;
  394. expired:
  395. if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
  396. x->km.state = XFRM_STATE_EXPIRED;
  397. wake_up(&net->xfrm.km_waitq);
  398. next = 2;
  399. goto resched;
  400. }
  401. err = __xfrm_state_delete(x);
  402. if (!err && x->id.spi)
  403. km_state_expired(x, 1, 0);
  404. xfrm_audit_state_delete(x, err ? 0 : 1,
  405. audit_get_loginuid(current),
  406. audit_get_sessionid(current), 0);
  407. out:
  408. spin_unlock(&x->lock);
  409. return HRTIMER_NORESTART;
  410. }
  411. static void xfrm_replay_timer_handler(unsigned long data);
  412. struct xfrm_state *xfrm_state_alloc(struct net *net)
  413. {
  414. struct xfrm_state *x;
  415. x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
  416. if (x) {
  417. write_pnet(&x->xs_net, net);
  418. atomic_set(&x->refcnt, 1);
  419. atomic_set(&x->tunnel_users, 0);
  420. INIT_LIST_HEAD(&x->km.all);
  421. INIT_HLIST_NODE(&x->bydst);
  422. INIT_HLIST_NODE(&x->bysrc);
  423. INIT_HLIST_NODE(&x->byspi);
  424. tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  425. setup_timer(&x->rtimer, xfrm_replay_timer_handler,
  426. (unsigned long)x);
  427. x->curlft.add_time = get_seconds();
  428. x->lft.soft_byte_limit = XFRM_INF;
  429. x->lft.soft_packet_limit = XFRM_INF;
  430. x->lft.hard_byte_limit = XFRM_INF;
  431. x->lft.hard_packet_limit = XFRM_INF;
  432. x->replay_maxage = 0;
  433. x->replay_maxdiff = 0;
  434. x->inner_mode = NULL;
  435. x->inner_mode_iaf = NULL;
  436. spin_lock_init(&x->lock);
  437. }
  438. return x;
  439. }
  440. EXPORT_SYMBOL(xfrm_state_alloc);
  441. void __xfrm_state_destroy(struct xfrm_state *x)
  442. {
  443. struct net *net = xs_net(x);
  444. WARN_ON(x->km.state != XFRM_STATE_DEAD);
  445. spin_lock_bh(&xfrm_state_gc_lock);
  446. hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
  447. spin_unlock_bh(&xfrm_state_gc_lock);
  448. schedule_work(&net->xfrm.state_gc_work);
  449. }
  450. EXPORT_SYMBOL(__xfrm_state_destroy);
  451. int __xfrm_state_delete(struct xfrm_state *x)
  452. {
  453. struct net *net = xs_net(x);
  454. int err = -ESRCH;
  455. if (x->km.state != XFRM_STATE_DEAD) {
  456. x->km.state = XFRM_STATE_DEAD;
  457. spin_lock(&xfrm_state_lock);
  458. list_del(&x->km.all);
  459. hlist_del(&x->bydst);
  460. hlist_del(&x->bysrc);
  461. if (x->id.spi)
  462. hlist_del(&x->byspi);
  463. net->xfrm.state_num--;
  464. spin_unlock(&xfrm_state_lock);
  465. /* All xfrm_state objects are created by xfrm_state_alloc.
  466. * The xfrm_state_alloc call gives a reference, and that
  467. * is what we are dropping here.
  468. */
  469. xfrm_state_put(x);
  470. err = 0;
  471. }
  472. return err;
  473. }
  474. EXPORT_SYMBOL(__xfrm_state_delete);
  475. int xfrm_state_delete(struct xfrm_state *x)
  476. {
  477. int err;
  478. spin_lock_bh(&x->lock);
  479. err = __xfrm_state_delete(x);
  480. spin_unlock_bh(&x->lock);
  481. return err;
  482. }
  483. EXPORT_SYMBOL(xfrm_state_delete);
  484. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  485. static inline int
  486. xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
  487. {
  488. int i, err = 0;
  489. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  490. struct xfrm_state *x;
  491. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  492. if (xfrm_id_proto_match(x->id.proto, proto) &&
  493. (err = security_xfrm_state_delete(x)) != 0) {
  494. xfrm_audit_state_delete(x, 0,
  495. audit_info->loginuid,
  496. audit_info->sessionid,
  497. audit_info->secid);
  498. return err;
  499. }
  500. }
  501. }
  502. return err;
  503. }
  504. #else
  505. static inline int
  506. xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
  507. {
  508. return 0;
  509. }
  510. #endif
  511. int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
  512. {
  513. int i, err = 0, cnt = 0;
  514. spin_lock_bh(&xfrm_state_lock);
  515. err = xfrm_state_flush_secctx_check(net, proto, audit_info);
  516. if (err)
  517. goto out;
  518. err = -ESRCH;
  519. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  520. struct xfrm_state *x;
  521. restart:
  522. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  523. if (!xfrm_state_kern(x) &&
  524. xfrm_id_proto_match(x->id.proto, proto)) {
  525. xfrm_state_hold(x);
  526. spin_unlock_bh(&xfrm_state_lock);
  527. err = xfrm_state_delete(x);
  528. xfrm_audit_state_delete(x, err ? 0 : 1,
  529. audit_info->loginuid,
  530. audit_info->sessionid,
  531. audit_info->secid);
  532. xfrm_state_put(x);
  533. if (!err)
  534. cnt++;
  535. spin_lock_bh(&xfrm_state_lock);
  536. goto restart;
  537. }
  538. }
  539. }
  540. if (cnt)
  541. err = 0;
  542. out:
  543. spin_unlock_bh(&xfrm_state_lock);
  544. wake_up(&net->xfrm.km_waitq);
  545. return err;
  546. }
  547. EXPORT_SYMBOL(xfrm_state_flush);
  548. void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
  549. {
  550. spin_lock_bh(&xfrm_state_lock);
  551. si->sadcnt = net->xfrm.state_num;
  552. si->sadhcnt = net->xfrm.state_hmask;
  553. si->sadhmcnt = xfrm_state_hashmax;
  554. spin_unlock_bh(&xfrm_state_lock);
  555. }
  556. EXPORT_SYMBOL(xfrm_sad_getinfo);
  557. static int
  558. xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
  559. const struct xfrm_tmpl *tmpl,
  560. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  561. unsigned short family)
  562. {
  563. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  564. if (!afinfo)
  565. return -1;
  566. afinfo->init_tempsel(&x->sel, fl);
  567. if (family != tmpl->encap_family) {
  568. xfrm_state_put_afinfo(afinfo);
  569. afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
  570. if (!afinfo)
  571. return -1;
  572. }
  573. afinfo->init_temprop(x, tmpl, daddr, saddr);
  574. xfrm_state_put_afinfo(afinfo);
  575. return 0;
  576. }
  577. static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
  578. const xfrm_address_t *daddr,
  579. __be32 spi, u8 proto,
  580. unsigned short family)
  581. {
  582. unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
  583. struct xfrm_state *x;
  584. hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
  585. if (x->props.family != family ||
  586. x->id.spi != spi ||
  587. x->id.proto != proto ||
  588. !xfrm_addr_equal(&x->id.daddr, daddr, family))
  589. continue;
  590. if ((mark & x->mark.m) != x->mark.v)
  591. continue;
  592. xfrm_state_hold(x);
  593. return x;
  594. }
  595. return NULL;
  596. }
  597. static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
  598. const xfrm_address_t *daddr,
  599. const xfrm_address_t *saddr,
  600. u8 proto, unsigned short family)
  601. {
  602. unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
  603. struct xfrm_state *x;
  604. hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
  605. if (x->props.family != family ||
  606. x->id.proto != proto ||
  607. !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
  608. !xfrm_addr_equal(&x->props.saddr, saddr, family))
  609. continue;
  610. if ((mark & x->mark.m) != x->mark.v)
  611. continue;
  612. xfrm_state_hold(x);
  613. return x;
  614. }
  615. return NULL;
  616. }
  617. static inline struct xfrm_state *
  618. __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
  619. {
  620. struct net *net = xs_net(x);
  621. u32 mark = x->mark.v & x->mark.m;
  622. if (use_spi)
  623. return __xfrm_state_lookup(net, mark, &x->id.daddr,
  624. x->id.spi, x->id.proto, family);
  625. else
  626. return __xfrm_state_lookup_byaddr(net, mark,
  627. &x->id.daddr,
  628. &x->props.saddr,
  629. x->id.proto, family);
  630. }
  631. static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
  632. {
  633. if (have_hash_collision &&
  634. (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
  635. net->xfrm.state_num > net->xfrm.state_hmask)
  636. schedule_work(&net->xfrm.state_hash_work);
  637. }
  638. static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
  639. const struct flowi *fl, unsigned short family,
  640. struct xfrm_state **best, int *acq_in_progress,
  641. int *error)
  642. {
  643. /* Resolution logic:
  644. * 1. There is a valid state with matching selector. Done.
  645. * 2. Valid state with inappropriate selector. Skip.
  646. *
  647. * Entering area of "sysdeps".
  648. *
  649. * 3. If state is not valid, selector is temporary, it selects
  650. * only session which triggered previous resolution. Key
  651. * manager will do something to install a state with proper
  652. * selector.
  653. */
  654. if (x->km.state == XFRM_STATE_VALID) {
  655. if ((x->sel.family &&
  656. !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
  657. !security_xfrm_state_pol_flow_match(x, pol, fl))
  658. return;
  659. if (!*best ||
  660. (*best)->km.dying > x->km.dying ||
  661. ((*best)->km.dying == x->km.dying &&
  662. (*best)->curlft.add_time < x->curlft.add_time))
  663. *best = x;
  664. } else if (x->km.state == XFRM_STATE_ACQ) {
  665. *acq_in_progress = 1;
  666. } else if (x->km.state == XFRM_STATE_ERROR ||
  667. x->km.state == XFRM_STATE_EXPIRED) {
  668. if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
  669. security_xfrm_state_pol_flow_match(x, pol, fl))
  670. *error = -ESRCH;
  671. }
  672. }
  673. struct xfrm_state *
  674. xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  675. const struct flowi *fl, struct xfrm_tmpl *tmpl,
  676. struct xfrm_policy *pol, int *err,
  677. unsigned short family)
  678. {
  679. static xfrm_address_t saddr_wildcard = { };
  680. struct net *net = xp_net(pol);
  681. unsigned int h, h_wildcard;
  682. struct xfrm_state *x, *x0, *to_put;
  683. int acquire_in_progress = 0;
  684. int error = 0;
  685. struct xfrm_state *best = NULL;
  686. u32 mark = pol->mark.v & pol->mark.m;
  687. unsigned short encap_family = tmpl->encap_family;
  688. to_put = NULL;
  689. spin_lock_bh(&xfrm_state_lock);
  690. h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
  691. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  692. if (x->props.family == encap_family &&
  693. x->props.reqid == tmpl->reqid &&
  694. (mark & x->mark.m) == x->mark.v &&
  695. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  696. xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
  697. tmpl->mode == x->props.mode &&
  698. tmpl->id.proto == x->id.proto &&
  699. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  700. xfrm_state_look_at(pol, x, fl, encap_family,
  701. &best, &acquire_in_progress, &error);
  702. }
  703. if (best)
  704. goto found;
  705. h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
  706. hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
  707. if (x->props.family == encap_family &&
  708. x->props.reqid == tmpl->reqid &&
  709. (mark & x->mark.m) == x->mark.v &&
  710. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  711. xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
  712. tmpl->mode == x->props.mode &&
  713. tmpl->id.proto == x->id.proto &&
  714. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  715. xfrm_state_look_at(pol, x, fl, encap_family,
  716. &best, &acquire_in_progress, &error);
  717. }
  718. found:
  719. x = best;
  720. if (!x && !error && !acquire_in_progress) {
  721. if (tmpl->id.spi &&
  722. (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
  723. tmpl->id.proto, encap_family)) != NULL) {
  724. to_put = x0;
  725. error = -EEXIST;
  726. goto out;
  727. }
  728. x = xfrm_state_alloc(net);
  729. if (x == NULL) {
  730. error = -ENOMEM;
  731. goto out;
  732. }
  733. /* Initialize temporary state matching only
  734. * to current session. */
  735. xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
  736. memcpy(&x->mark, &pol->mark, sizeof(x->mark));
  737. error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
  738. if (error) {
  739. x->km.state = XFRM_STATE_DEAD;
  740. to_put = x;
  741. x = NULL;
  742. goto out;
  743. }
  744. if (km_query(x, tmpl, pol) == 0) {
  745. x->km.state = XFRM_STATE_ACQ;
  746. list_add(&x->km.all, &net->xfrm.state_all);
  747. hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
  748. h = xfrm_src_hash(net, daddr, saddr, encap_family);
  749. hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
  750. if (x->id.spi) {
  751. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
  752. hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
  753. }
  754. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  755. tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
  756. net->xfrm.state_num++;
  757. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  758. } else {
  759. x->km.state = XFRM_STATE_DEAD;
  760. to_put = x;
  761. x = NULL;
  762. error = -ESRCH;
  763. }
  764. }
  765. out:
  766. if (x)
  767. xfrm_state_hold(x);
  768. else
  769. *err = acquire_in_progress ? -EAGAIN : error;
  770. spin_unlock_bh(&xfrm_state_lock);
  771. if (to_put)
  772. xfrm_state_put(to_put);
  773. return x;
  774. }
  775. struct xfrm_state *
  776. xfrm_stateonly_find(struct net *net, u32 mark,
  777. xfrm_address_t *daddr, xfrm_address_t *saddr,
  778. unsigned short family, u8 mode, u8 proto, u32 reqid)
  779. {
  780. unsigned int h;
  781. struct xfrm_state *rx = NULL, *x = NULL;
  782. spin_lock(&xfrm_state_lock);
  783. h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  784. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  785. if (x->props.family == family &&
  786. x->props.reqid == reqid &&
  787. (mark & x->mark.m) == x->mark.v &&
  788. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  789. xfrm_state_addr_check(x, daddr, saddr, family) &&
  790. mode == x->props.mode &&
  791. proto == x->id.proto &&
  792. x->km.state == XFRM_STATE_VALID) {
  793. rx = x;
  794. break;
  795. }
  796. }
  797. if (rx)
  798. xfrm_state_hold(rx);
  799. spin_unlock(&xfrm_state_lock);
  800. return rx;
  801. }
  802. EXPORT_SYMBOL(xfrm_stateonly_find);
  803. static void __xfrm_state_insert(struct xfrm_state *x)
  804. {
  805. struct net *net = xs_net(x);
  806. unsigned int h;
  807. list_add(&x->km.all, &net->xfrm.state_all);
  808. h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
  809. x->props.reqid, x->props.family);
  810. hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
  811. h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
  812. hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
  813. if (x->id.spi) {
  814. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
  815. x->props.family);
  816. hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
  817. }
  818. tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
  819. if (x->replay_maxage)
  820. mod_timer(&x->rtimer, jiffies + x->replay_maxage);
  821. wake_up(&net->xfrm.km_waitq);
  822. net->xfrm.state_num++;
  823. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  824. }
  825. /* xfrm_state_lock is held */
  826. static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
  827. {
  828. struct net *net = xs_net(xnew);
  829. unsigned short family = xnew->props.family;
  830. u32 reqid = xnew->props.reqid;
  831. struct xfrm_state *x;
  832. unsigned int h;
  833. u32 mark = xnew->mark.v & xnew->mark.m;
  834. h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
  835. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  836. if (x->props.family == family &&
  837. x->props.reqid == reqid &&
  838. (mark & x->mark.m) == x->mark.v &&
  839. xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
  840. xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
  841. x->genid++;
  842. }
  843. }
  844. void xfrm_state_insert(struct xfrm_state *x)
  845. {
  846. spin_lock_bh(&xfrm_state_lock);
  847. __xfrm_state_bump_genids(x);
  848. __xfrm_state_insert(x);
  849. spin_unlock_bh(&xfrm_state_lock);
  850. }
  851. EXPORT_SYMBOL(xfrm_state_insert);
  852. /* xfrm_state_lock is held */
  853. static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
  854. unsigned short family, u8 mode,
  855. u32 reqid, u8 proto,
  856. const xfrm_address_t *daddr,
  857. const xfrm_address_t *saddr, int create)
  858. {
  859. unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  860. struct xfrm_state *x;
  861. u32 mark = m->v & m->m;
  862. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  863. if (x->props.reqid != reqid ||
  864. x->props.mode != mode ||
  865. x->props.family != family ||
  866. x->km.state != XFRM_STATE_ACQ ||
  867. x->id.spi != 0 ||
  868. x->id.proto != proto ||
  869. (mark & x->mark.m) != x->mark.v ||
  870. !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
  871. !xfrm_addr_equal(&x->props.saddr, saddr, family))
  872. continue;
  873. xfrm_state_hold(x);
  874. return x;
  875. }
  876. if (!create)
  877. return NULL;
  878. x = xfrm_state_alloc(net);
  879. if (likely(x)) {
  880. switch (family) {
  881. case AF_INET:
  882. x->sel.daddr.a4 = daddr->a4;
  883. x->sel.saddr.a4 = saddr->a4;
  884. x->sel.prefixlen_d = 32;
  885. x->sel.prefixlen_s = 32;
  886. x->props.saddr.a4 = saddr->a4;
  887. x->id.daddr.a4 = daddr->a4;
  888. break;
  889. case AF_INET6:
  890. *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
  891. *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
  892. x->sel.prefixlen_d = 128;
  893. x->sel.prefixlen_s = 128;
  894. *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
  895. *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
  896. break;
  897. }
  898. x->km.state = XFRM_STATE_ACQ;
  899. x->id.proto = proto;
  900. x->props.family = family;
  901. x->props.mode = mode;
  902. x->props.reqid = reqid;
  903. x->mark.v = m->v;
  904. x->mark.m = m->m;
  905. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  906. xfrm_state_hold(x);
  907. tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
  908. list_add(&x->km.all, &net->xfrm.state_all);
  909. hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
  910. h = xfrm_src_hash(net, daddr, saddr, family);
  911. hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
  912. net->xfrm.state_num++;
  913. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  914. }
  915. return x;
  916. }
  917. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
  918. int xfrm_state_add(struct xfrm_state *x)
  919. {
  920. struct net *net = xs_net(x);
  921. struct xfrm_state *x1, *to_put;
  922. int family;
  923. int err;
  924. u32 mark = x->mark.v & x->mark.m;
  925. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  926. family = x->props.family;
  927. to_put = NULL;
  928. spin_lock_bh(&xfrm_state_lock);
  929. x1 = __xfrm_state_locate(x, use_spi, family);
  930. if (x1) {
  931. to_put = x1;
  932. x1 = NULL;
  933. err = -EEXIST;
  934. goto out;
  935. }
  936. if (use_spi && x->km.seq) {
  937. x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
  938. if (x1 && ((x1->id.proto != x->id.proto) ||
  939. !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
  940. to_put = x1;
  941. x1 = NULL;
  942. }
  943. }
  944. if (use_spi && !x1)
  945. x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
  946. x->props.reqid, x->id.proto,
  947. &x->id.daddr, &x->props.saddr, 0);
  948. __xfrm_state_bump_genids(x);
  949. __xfrm_state_insert(x);
  950. err = 0;
  951. out:
  952. spin_unlock_bh(&xfrm_state_lock);
  953. if (x1) {
  954. xfrm_state_delete(x1);
  955. xfrm_state_put(x1);
  956. }
  957. if (to_put)
  958. xfrm_state_put(to_put);
  959. return err;
  960. }
  961. EXPORT_SYMBOL(xfrm_state_add);
  962. #ifdef CONFIG_XFRM_MIGRATE
  963. static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
  964. {
  965. struct net *net = xs_net(orig);
  966. int err = -ENOMEM;
  967. struct xfrm_state *x = xfrm_state_alloc(net);
  968. if (!x)
  969. goto out;
  970. memcpy(&x->id, &orig->id, sizeof(x->id));
  971. memcpy(&x->sel, &orig->sel, sizeof(x->sel));
  972. memcpy(&x->lft, &orig->lft, sizeof(x->lft));
  973. x->props.mode = orig->props.mode;
  974. x->props.replay_window = orig->props.replay_window;
  975. x->props.reqid = orig->props.reqid;
  976. x->props.family = orig->props.family;
  977. x->props.saddr = orig->props.saddr;
  978. if (orig->aalg) {
  979. x->aalg = xfrm_algo_auth_clone(orig->aalg);
  980. if (!x->aalg)
  981. goto error;
  982. }
  983. x->props.aalgo = orig->props.aalgo;
  984. if (orig->ealg) {
  985. x->ealg = xfrm_algo_clone(orig->ealg);
  986. if (!x->ealg)
  987. goto error;
  988. }
  989. x->props.ealgo = orig->props.ealgo;
  990. if (orig->calg) {
  991. x->calg = xfrm_algo_clone(orig->calg);
  992. if (!x->calg)
  993. goto error;
  994. }
  995. x->props.calgo = orig->props.calgo;
  996. if (orig->encap) {
  997. x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
  998. if (!x->encap)
  999. goto error;
  1000. }
  1001. if (orig->coaddr) {
  1002. x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
  1003. GFP_KERNEL);
  1004. if (!x->coaddr)
  1005. goto error;
  1006. }
  1007. if (orig->replay_esn) {
  1008. err = xfrm_replay_clone(x, orig);
  1009. if (err)
  1010. goto error;
  1011. }
  1012. memcpy(&x->mark, &orig->mark, sizeof(x->mark));
  1013. err = xfrm_init_state(x);
  1014. if (err)
  1015. goto error;
  1016. x->props.flags = orig->props.flags;
  1017. x->curlft.add_time = orig->curlft.add_time;
  1018. x->km.state = orig->km.state;
  1019. x->km.seq = orig->km.seq;
  1020. return x;
  1021. error:
  1022. xfrm_state_put(x);
  1023. out:
  1024. if (errp)
  1025. *errp = err;
  1026. return NULL;
  1027. }
  1028. /* xfrm_state_lock is held */
  1029. struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
  1030. {
  1031. unsigned int h;
  1032. struct xfrm_state *x;
  1033. if (m->reqid) {
  1034. h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
  1035. m->reqid, m->old_family);
  1036. hlist_for_each_entry(x, init_net.xfrm.state_bydst+h, bydst) {
  1037. if (x->props.mode != m->mode ||
  1038. x->id.proto != m->proto)
  1039. continue;
  1040. if (m->reqid && x->props.reqid != m->reqid)
  1041. continue;
  1042. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1043. m->old_family) ||
  1044. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1045. m->old_family))
  1046. continue;
  1047. xfrm_state_hold(x);
  1048. return x;
  1049. }
  1050. } else {
  1051. h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
  1052. m->old_family);
  1053. hlist_for_each_entry(x, init_net.xfrm.state_bysrc+h, bysrc) {
  1054. if (x->props.mode != m->mode ||
  1055. x->id.proto != m->proto)
  1056. continue;
  1057. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1058. m->old_family) ||
  1059. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1060. m->old_family))
  1061. continue;
  1062. xfrm_state_hold(x);
  1063. return x;
  1064. }
  1065. }
  1066. return NULL;
  1067. }
  1068. EXPORT_SYMBOL(xfrm_migrate_state_find);
  1069. struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
  1070. struct xfrm_migrate *m)
  1071. {
  1072. struct xfrm_state *xc;
  1073. int err;
  1074. xc = xfrm_state_clone(x, &err);
  1075. if (!xc)
  1076. return NULL;
  1077. memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
  1078. memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
  1079. /* add state */
  1080. if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
  1081. /* a care is needed when the destination address of the
  1082. state is to be updated as it is a part of triplet */
  1083. xfrm_state_insert(xc);
  1084. } else {
  1085. if ((err = xfrm_state_add(xc)) < 0)
  1086. goto error;
  1087. }
  1088. return xc;
  1089. error:
  1090. xfrm_state_put(xc);
  1091. return NULL;
  1092. }
  1093. EXPORT_SYMBOL(xfrm_state_migrate);
  1094. #endif
  1095. int xfrm_state_update(struct xfrm_state *x)
  1096. {
  1097. struct xfrm_state *x1, *to_put;
  1098. int err;
  1099. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  1100. to_put = NULL;
  1101. spin_lock_bh(&xfrm_state_lock);
  1102. x1 = __xfrm_state_locate(x, use_spi, x->props.family);
  1103. err = -ESRCH;
  1104. if (!x1)
  1105. goto out;
  1106. if (xfrm_state_kern(x1)) {
  1107. to_put = x1;
  1108. err = -EEXIST;
  1109. goto out;
  1110. }
  1111. if (x1->km.state == XFRM_STATE_ACQ) {
  1112. __xfrm_state_insert(x);
  1113. x = NULL;
  1114. }
  1115. err = 0;
  1116. out:
  1117. spin_unlock_bh(&xfrm_state_lock);
  1118. if (to_put)
  1119. xfrm_state_put(to_put);
  1120. if (err)
  1121. return err;
  1122. if (!x) {
  1123. xfrm_state_delete(x1);
  1124. xfrm_state_put(x1);
  1125. return 0;
  1126. }
  1127. err = -EINVAL;
  1128. spin_lock_bh(&x1->lock);
  1129. if (likely(x1->km.state == XFRM_STATE_VALID)) {
  1130. if (x->encap && x1->encap)
  1131. memcpy(x1->encap, x->encap, sizeof(*x1->encap));
  1132. if (x->coaddr && x1->coaddr) {
  1133. memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
  1134. }
  1135. if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
  1136. memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
  1137. memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
  1138. x1->km.dying = 0;
  1139. tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
  1140. if (x1->curlft.use_time)
  1141. xfrm_state_check_expire(x1);
  1142. err = 0;
  1143. x->km.state = XFRM_STATE_DEAD;
  1144. __xfrm_state_put(x);
  1145. }
  1146. spin_unlock_bh(&x1->lock);
  1147. xfrm_state_put(x1);
  1148. return err;
  1149. }
  1150. EXPORT_SYMBOL(xfrm_state_update);
  1151. int xfrm_state_check_expire(struct xfrm_state *x)
  1152. {
  1153. if (!x->curlft.use_time)
  1154. x->curlft.use_time = get_seconds();
  1155. if (x->curlft.bytes >= x->lft.hard_byte_limit ||
  1156. x->curlft.packets >= x->lft.hard_packet_limit) {
  1157. x->km.state = XFRM_STATE_EXPIRED;
  1158. tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
  1159. return -EINVAL;
  1160. }
  1161. if (!x->km.dying &&
  1162. (x->curlft.bytes >= x->lft.soft_byte_limit ||
  1163. x->curlft.packets >= x->lft.soft_packet_limit)) {
  1164. x->km.dying = 1;
  1165. km_state_expired(x, 0, 0);
  1166. }
  1167. return 0;
  1168. }
  1169. EXPORT_SYMBOL(xfrm_state_check_expire);
  1170. struct xfrm_state *
  1171. xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
  1172. u8 proto, unsigned short family)
  1173. {
  1174. struct xfrm_state *x;
  1175. spin_lock_bh(&xfrm_state_lock);
  1176. x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
  1177. spin_unlock_bh(&xfrm_state_lock);
  1178. return x;
  1179. }
  1180. EXPORT_SYMBOL(xfrm_state_lookup);
  1181. struct xfrm_state *
  1182. xfrm_state_lookup_byaddr(struct net *net, u32 mark,
  1183. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  1184. u8 proto, unsigned short family)
  1185. {
  1186. struct xfrm_state *x;
  1187. spin_lock_bh(&xfrm_state_lock);
  1188. x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
  1189. spin_unlock_bh(&xfrm_state_lock);
  1190. return x;
  1191. }
  1192. EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
  1193. struct xfrm_state *
  1194. xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
  1195. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  1196. int create, unsigned short family)
  1197. {
  1198. struct xfrm_state *x;
  1199. spin_lock_bh(&xfrm_state_lock);
  1200. x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
  1201. spin_unlock_bh(&xfrm_state_lock);
  1202. return x;
  1203. }
  1204. EXPORT_SYMBOL(xfrm_find_acq);
  1205. #ifdef CONFIG_XFRM_SUB_POLICY
  1206. int
  1207. xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
  1208. unsigned short family)
  1209. {
  1210. int err = 0;
  1211. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  1212. if (!afinfo)
  1213. return -EAFNOSUPPORT;
  1214. spin_lock_bh(&xfrm_state_lock);
  1215. if (afinfo->tmpl_sort)
  1216. err = afinfo->tmpl_sort(dst, src, n);
  1217. spin_unlock_bh(&xfrm_state_lock);
  1218. xfrm_state_put_afinfo(afinfo);
  1219. return err;
  1220. }
  1221. EXPORT_SYMBOL(xfrm_tmpl_sort);
  1222. int
  1223. xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
  1224. unsigned short family)
  1225. {
  1226. int err = 0;
  1227. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  1228. if (!afinfo)
  1229. return -EAFNOSUPPORT;
  1230. spin_lock_bh(&xfrm_state_lock);
  1231. if (afinfo->state_sort)
  1232. err = afinfo->state_sort(dst, src, n);
  1233. spin_unlock_bh(&xfrm_state_lock);
  1234. xfrm_state_put_afinfo(afinfo);
  1235. return err;
  1236. }
  1237. EXPORT_SYMBOL(xfrm_state_sort);
  1238. #endif
  1239. /* Silly enough, but I'm lazy to build resolution list */
  1240. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
  1241. {
  1242. int i;
  1243. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  1244. struct xfrm_state *x;
  1245. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  1246. if (x->km.seq == seq &&
  1247. (mark & x->mark.m) == x->mark.v &&
  1248. x->km.state == XFRM_STATE_ACQ) {
  1249. xfrm_state_hold(x);
  1250. return x;
  1251. }
  1252. }
  1253. }
  1254. return NULL;
  1255. }
  1256. struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
  1257. {
  1258. struct xfrm_state *x;
  1259. spin_lock_bh(&xfrm_state_lock);
  1260. x = __xfrm_find_acq_byseq(net, mark, seq);
  1261. spin_unlock_bh(&xfrm_state_lock);
  1262. return x;
  1263. }
  1264. EXPORT_SYMBOL(xfrm_find_acq_byseq);
  1265. u32 xfrm_get_acqseq(void)
  1266. {
  1267. u32 res;
  1268. static atomic_t acqseq;
  1269. do {
  1270. res = atomic_inc_return(&acqseq);
  1271. } while (!res);
  1272. return res;
  1273. }
  1274. EXPORT_SYMBOL(xfrm_get_acqseq);
  1275. int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
  1276. {
  1277. struct net *net = xs_net(x);
  1278. unsigned int h;
  1279. struct xfrm_state *x0;
  1280. int err = -ENOENT;
  1281. __be32 minspi = htonl(low);
  1282. __be32 maxspi = htonl(high);
  1283. u32 mark = x->mark.v & x->mark.m;
  1284. spin_lock_bh(&x->lock);
  1285. if (x->km.state == XFRM_STATE_DEAD)
  1286. goto unlock;
  1287. err = 0;
  1288. if (x->id.spi)
  1289. goto unlock;
  1290. err = -ENOENT;
  1291. if (minspi == maxspi) {
  1292. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
  1293. if (x0) {
  1294. xfrm_state_put(x0);
  1295. goto unlock;
  1296. }
  1297. x->id.spi = minspi;
  1298. } else {
  1299. u32 spi = 0;
  1300. for (h=0; h<high-low+1; h++) {
  1301. spi = low + net_random()%(high-low+1);
  1302. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
  1303. if (x0 == NULL) {
  1304. x->id.spi = htonl(spi);
  1305. break;
  1306. }
  1307. xfrm_state_put(x0);
  1308. }
  1309. }
  1310. if (x->id.spi) {
  1311. spin_lock_bh(&xfrm_state_lock);
  1312. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
  1313. hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
  1314. spin_unlock_bh(&xfrm_state_lock);
  1315. err = 0;
  1316. }
  1317. unlock:
  1318. spin_unlock_bh(&x->lock);
  1319. return err;
  1320. }
  1321. EXPORT_SYMBOL(xfrm_alloc_spi);
  1322. int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
  1323. int (*func)(struct xfrm_state *, int, void*),
  1324. void *data)
  1325. {
  1326. struct xfrm_state *state;
  1327. struct xfrm_state_walk *x;
  1328. int err = 0;
  1329. if (walk->seq != 0 && list_empty(&walk->all))
  1330. return 0;
  1331. spin_lock_bh(&xfrm_state_lock);
  1332. if (list_empty(&walk->all))
  1333. x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
  1334. else
  1335. x = list_entry(&walk->all, struct xfrm_state_walk, all);
  1336. list_for_each_entry_from(x, &net->xfrm.state_all, all) {
  1337. if (x->state == XFRM_STATE_DEAD)
  1338. continue;
  1339. state = container_of(x, struct xfrm_state, km);
  1340. if (!xfrm_id_proto_match(state->id.proto, walk->proto))
  1341. continue;
  1342. err = func(state, walk->seq, data);
  1343. if (err) {
  1344. list_move_tail(&walk->all, &x->all);
  1345. goto out;
  1346. }
  1347. walk->seq++;
  1348. }
  1349. if (walk->seq == 0) {
  1350. err = -ENOENT;
  1351. goto out;
  1352. }
  1353. list_del_init(&walk->all);
  1354. out:
  1355. spin_unlock_bh(&xfrm_state_lock);
  1356. return err;
  1357. }
  1358. EXPORT_SYMBOL(xfrm_state_walk);
  1359. void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
  1360. {
  1361. INIT_LIST_HEAD(&walk->all);
  1362. walk->proto = proto;
  1363. walk->state = XFRM_STATE_DEAD;
  1364. walk->seq = 0;
  1365. }
  1366. EXPORT_SYMBOL(xfrm_state_walk_init);
  1367. void xfrm_state_walk_done(struct xfrm_state_walk *walk)
  1368. {
  1369. if (list_empty(&walk->all))
  1370. return;
  1371. spin_lock_bh(&xfrm_state_lock);
  1372. list_del(&walk->all);
  1373. spin_unlock_bh(&xfrm_state_lock);
  1374. }
  1375. EXPORT_SYMBOL(xfrm_state_walk_done);
  1376. static void xfrm_replay_timer_handler(unsigned long data)
  1377. {
  1378. struct xfrm_state *x = (struct xfrm_state*)data;
  1379. spin_lock(&x->lock);
  1380. if (x->km.state == XFRM_STATE_VALID) {
  1381. if (xfrm_aevent_is_on(xs_net(x)))
  1382. x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
  1383. else
  1384. x->xflags |= XFRM_TIME_DEFER;
  1385. }
  1386. spin_unlock(&x->lock);
  1387. }
  1388. static LIST_HEAD(xfrm_km_list);
  1389. void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  1390. {
  1391. struct xfrm_mgr *km;
  1392. rcu_read_lock();
  1393. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  1394. if (km->notify_policy)
  1395. km->notify_policy(xp, dir, c);
  1396. rcu_read_unlock();
  1397. }
  1398. void km_state_notify(struct xfrm_state *x, const struct km_event *c)
  1399. {
  1400. struct xfrm_mgr *km;
  1401. rcu_read_lock();
  1402. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  1403. if (km->notify)
  1404. km->notify(x, c);
  1405. rcu_read_unlock();
  1406. }
  1407. EXPORT_SYMBOL(km_policy_notify);
  1408. EXPORT_SYMBOL(km_state_notify);
  1409. void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
  1410. {
  1411. struct net *net = xs_net(x);
  1412. struct km_event c;
  1413. c.data.hard = hard;
  1414. c.portid = portid;
  1415. c.event = XFRM_MSG_EXPIRE;
  1416. km_state_notify(x, &c);
  1417. if (hard)
  1418. wake_up(&net->xfrm.km_waitq);
  1419. }
  1420. EXPORT_SYMBOL(km_state_expired);
  1421. /*
  1422. * We send to all registered managers regardless of failure
  1423. * We are happy with one success
  1424. */
  1425. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
  1426. {
  1427. int err = -EINVAL, acqret;
  1428. struct xfrm_mgr *km;
  1429. rcu_read_lock();
  1430. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1431. acqret = km->acquire(x, t, pol);
  1432. if (!acqret)
  1433. err = acqret;
  1434. }
  1435. rcu_read_unlock();
  1436. return err;
  1437. }
  1438. EXPORT_SYMBOL(km_query);
  1439. int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
  1440. {
  1441. int err = -EINVAL;
  1442. struct xfrm_mgr *km;
  1443. rcu_read_lock();
  1444. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1445. if (km->new_mapping)
  1446. err = km->new_mapping(x, ipaddr, sport);
  1447. if (!err)
  1448. break;
  1449. }
  1450. rcu_read_unlock();
  1451. return err;
  1452. }
  1453. EXPORT_SYMBOL(km_new_mapping);
  1454. void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
  1455. {
  1456. struct net *net = xp_net(pol);
  1457. struct km_event c;
  1458. c.data.hard = hard;
  1459. c.portid = portid;
  1460. c.event = XFRM_MSG_POLEXPIRE;
  1461. km_policy_notify(pol, dir, &c);
  1462. if (hard)
  1463. wake_up(&net->xfrm.km_waitq);
  1464. }
  1465. EXPORT_SYMBOL(km_policy_expired);
  1466. #ifdef CONFIG_XFRM_MIGRATE
  1467. int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1468. const struct xfrm_migrate *m, int num_migrate,
  1469. const struct xfrm_kmaddress *k)
  1470. {
  1471. int err = -EINVAL;
  1472. int ret;
  1473. struct xfrm_mgr *km;
  1474. rcu_read_lock();
  1475. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1476. if (km->migrate) {
  1477. ret = km->migrate(sel, dir, type, m, num_migrate, k);
  1478. if (!ret)
  1479. err = ret;
  1480. }
  1481. }
  1482. rcu_read_unlock();
  1483. return err;
  1484. }
  1485. EXPORT_SYMBOL(km_migrate);
  1486. #endif
  1487. int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
  1488. {
  1489. int err = -EINVAL;
  1490. int ret;
  1491. struct xfrm_mgr *km;
  1492. rcu_read_lock();
  1493. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1494. if (km->report) {
  1495. ret = km->report(net, proto, sel, addr);
  1496. if (!ret)
  1497. err = ret;
  1498. }
  1499. }
  1500. rcu_read_unlock();
  1501. return err;
  1502. }
  1503. EXPORT_SYMBOL(km_report);
  1504. int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
  1505. {
  1506. int err;
  1507. u8 *data;
  1508. struct xfrm_mgr *km;
  1509. struct xfrm_policy *pol = NULL;
  1510. if (optlen <= 0 || optlen > PAGE_SIZE)
  1511. return -EMSGSIZE;
  1512. data = kmalloc(optlen, GFP_KERNEL);
  1513. if (!data)
  1514. return -ENOMEM;
  1515. err = -EFAULT;
  1516. if (copy_from_user(data, optval, optlen))
  1517. goto out;
  1518. err = -EINVAL;
  1519. rcu_read_lock();
  1520. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1521. pol = km->compile_policy(sk, optname, data,
  1522. optlen, &err);
  1523. if (err >= 0)
  1524. break;
  1525. }
  1526. rcu_read_unlock();
  1527. if (err >= 0) {
  1528. xfrm_sk_policy_insert(sk, err, pol);
  1529. xfrm_pol_put(pol);
  1530. err = 0;
  1531. }
  1532. out:
  1533. kfree(data);
  1534. return err;
  1535. }
  1536. EXPORT_SYMBOL(xfrm_user_policy);
  1537. static DEFINE_SPINLOCK(xfrm_km_lock);
  1538. int xfrm_register_km(struct xfrm_mgr *km)
  1539. {
  1540. spin_lock_bh(&xfrm_km_lock);
  1541. list_add_tail_rcu(&km->list, &xfrm_km_list);
  1542. spin_unlock_bh(&xfrm_km_lock);
  1543. return 0;
  1544. }
  1545. EXPORT_SYMBOL(xfrm_register_km);
  1546. int xfrm_unregister_km(struct xfrm_mgr *km)
  1547. {
  1548. spin_lock_bh(&xfrm_km_lock);
  1549. list_del_rcu(&km->list);
  1550. spin_unlock_bh(&xfrm_km_lock);
  1551. synchronize_rcu();
  1552. return 0;
  1553. }
  1554. EXPORT_SYMBOL(xfrm_unregister_km);
  1555. int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
  1556. {
  1557. int err = 0;
  1558. if (unlikely(afinfo == NULL))
  1559. return -EINVAL;
  1560. if (unlikely(afinfo->family >= NPROTO))
  1561. return -EAFNOSUPPORT;
  1562. spin_lock_bh(&xfrm_state_afinfo_lock);
  1563. if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
  1564. err = -ENOBUFS;
  1565. else
  1566. rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
  1567. spin_unlock_bh(&xfrm_state_afinfo_lock);
  1568. return err;
  1569. }
  1570. EXPORT_SYMBOL(xfrm_state_register_afinfo);
  1571. int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
  1572. {
  1573. int err = 0;
  1574. if (unlikely(afinfo == NULL))
  1575. return -EINVAL;
  1576. if (unlikely(afinfo->family >= NPROTO))
  1577. return -EAFNOSUPPORT;
  1578. spin_lock_bh(&xfrm_state_afinfo_lock);
  1579. if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
  1580. if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
  1581. err = -EINVAL;
  1582. else
  1583. RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
  1584. }
  1585. spin_unlock_bh(&xfrm_state_afinfo_lock);
  1586. synchronize_rcu();
  1587. return err;
  1588. }
  1589. EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
  1590. static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
  1591. {
  1592. struct xfrm_state_afinfo *afinfo;
  1593. if (unlikely(family >= NPROTO))
  1594. return NULL;
  1595. rcu_read_lock();
  1596. afinfo = rcu_dereference(xfrm_state_afinfo[family]);
  1597. if (unlikely(!afinfo))
  1598. rcu_read_unlock();
  1599. return afinfo;
  1600. }
  1601. static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
  1602. {
  1603. rcu_read_unlock();
  1604. }
  1605. /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
  1606. void xfrm_state_delete_tunnel(struct xfrm_state *x)
  1607. {
  1608. if (x->tunnel) {
  1609. struct xfrm_state *t = x->tunnel;
  1610. if (atomic_read(&t->tunnel_users) == 2)
  1611. xfrm_state_delete(t);
  1612. atomic_dec(&t->tunnel_users);
  1613. xfrm_state_put(t);
  1614. x->tunnel = NULL;
  1615. }
  1616. }
  1617. EXPORT_SYMBOL(xfrm_state_delete_tunnel);
  1618. int xfrm_state_mtu(struct xfrm_state *x, int mtu)
  1619. {
  1620. int res;
  1621. spin_lock_bh(&x->lock);
  1622. if (x->km.state == XFRM_STATE_VALID &&
  1623. x->type && x->type->get_mtu)
  1624. res = x->type->get_mtu(x, mtu);
  1625. else
  1626. res = mtu - x->props.header_len;
  1627. spin_unlock_bh(&x->lock);
  1628. return res;
  1629. }
  1630. int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
  1631. {
  1632. struct xfrm_state_afinfo *afinfo;
  1633. struct xfrm_mode *inner_mode;
  1634. int family = x->props.family;
  1635. int err;
  1636. err = -EAFNOSUPPORT;
  1637. afinfo = xfrm_state_get_afinfo(family);
  1638. if (!afinfo)
  1639. goto error;
  1640. err = 0;
  1641. if (afinfo->init_flags)
  1642. err = afinfo->init_flags(x);
  1643. xfrm_state_put_afinfo(afinfo);
  1644. if (err)
  1645. goto error;
  1646. err = -EPROTONOSUPPORT;
  1647. if (x->sel.family != AF_UNSPEC) {
  1648. inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
  1649. if (inner_mode == NULL)
  1650. goto error;
  1651. if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1652. family != x->sel.family) {
  1653. xfrm_put_mode(inner_mode);
  1654. goto error;
  1655. }
  1656. x->inner_mode = inner_mode;
  1657. } else {
  1658. struct xfrm_mode *inner_mode_iaf;
  1659. int iafamily = AF_INET;
  1660. inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
  1661. if (inner_mode == NULL)
  1662. goto error;
  1663. if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
  1664. xfrm_put_mode(inner_mode);
  1665. goto error;
  1666. }
  1667. x->inner_mode = inner_mode;
  1668. if (x->props.family == AF_INET)
  1669. iafamily = AF_INET6;
  1670. inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
  1671. if (inner_mode_iaf) {
  1672. if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
  1673. x->inner_mode_iaf = inner_mode_iaf;
  1674. else
  1675. xfrm_put_mode(inner_mode_iaf);
  1676. }
  1677. }
  1678. x->type = xfrm_get_type(x->id.proto, family);
  1679. if (x->type == NULL)
  1680. goto error;
  1681. err = x->type->init_state(x);
  1682. if (err)
  1683. goto error;
  1684. x->outer_mode = xfrm_get_mode(x->props.mode, family);
  1685. if (x->outer_mode == NULL) {
  1686. err = -EPROTONOSUPPORT;
  1687. goto error;
  1688. }
  1689. if (init_replay) {
  1690. err = xfrm_init_replay(x);
  1691. if (err)
  1692. goto error;
  1693. }
  1694. x->km.state = XFRM_STATE_VALID;
  1695. error:
  1696. return err;
  1697. }
  1698. EXPORT_SYMBOL(__xfrm_init_state);
  1699. int xfrm_init_state(struct xfrm_state *x)
  1700. {
  1701. return __xfrm_init_state(x, true);
  1702. }
  1703. EXPORT_SYMBOL(xfrm_init_state);
  1704. int __net_init xfrm_state_init(struct net *net)
  1705. {
  1706. unsigned int sz;
  1707. INIT_LIST_HEAD(&net->xfrm.state_all);
  1708. sz = sizeof(struct hlist_head) * 8;
  1709. net->xfrm.state_bydst = xfrm_hash_alloc(sz);
  1710. if (!net->xfrm.state_bydst)
  1711. goto out_bydst;
  1712. net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
  1713. if (!net->xfrm.state_bysrc)
  1714. goto out_bysrc;
  1715. net->xfrm.state_byspi = xfrm_hash_alloc(sz);
  1716. if (!net->xfrm.state_byspi)
  1717. goto out_byspi;
  1718. net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
  1719. net->xfrm.state_num = 0;
  1720. INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
  1721. INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
  1722. INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
  1723. init_waitqueue_head(&net->xfrm.km_waitq);
  1724. return 0;
  1725. out_byspi:
  1726. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  1727. out_bysrc:
  1728. xfrm_hash_free(net->xfrm.state_bydst, sz);
  1729. out_bydst:
  1730. return -ENOMEM;
  1731. }
  1732. void xfrm_state_fini(struct net *net)
  1733. {
  1734. struct xfrm_audit audit_info;
  1735. unsigned int sz;
  1736. flush_work(&net->xfrm.state_hash_work);
  1737. audit_info.loginuid = INVALID_UID;
  1738. audit_info.sessionid = -1;
  1739. audit_info.secid = 0;
  1740. xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
  1741. flush_work(&net->xfrm.state_gc_work);
  1742. WARN_ON(!list_empty(&net->xfrm.state_all));
  1743. sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
  1744. WARN_ON(!hlist_empty(net->xfrm.state_byspi));
  1745. xfrm_hash_free(net->xfrm.state_byspi, sz);
  1746. WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
  1747. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  1748. WARN_ON(!hlist_empty(net->xfrm.state_bydst));
  1749. xfrm_hash_free(net->xfrm.state_bydst, sz);
  1750. }
  1751. #ifdef CONFIG_AUDITSYSCALL
  1752. static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
  1753. struct audit_buffer *audit_buf)
  1754. {
  1755. struct xfrm_sec_ctx *ctx = x->security;
  1756. u32 spi = ntohl(x->id.spi);
  1757. if (ctx)
  1758. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1759. ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
  1760. switch(x->props.family) {
  1761. case AF_INET:
  1762. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  1763. &x->props.saddr.a4, &x->id.daddr.a4);
  1764. break;
  1765. case AF_INET6:
  1766. audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
  1767. x->props.saddr.a6, x->id.daddr.a6);
  1768. break;
  1769. }
  1770. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  1771. }
  1772. static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
  1773. struct audit_buffer *audit_buf)
  1774. {
  1775. const struct iphdr *iph4;
  1776. const struct ipv6hdr *iph6;
  1777. switch (family) {
  1778. case AF_INET:
  1779. iph4 = ip_hdr(skb);
  1780. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  1781. &iph4->saddr, &iph4->daddr);
  1782. break;
  1783. case AF_INET6:
  1784. iph6 = ipv6_hdr(skb);
  1785. audit_log_format(audit_buf,
  1786. " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
  1787. &iph6->saddr,&iph6->daddr,
  1788. iph6->flow_lbl[0] & 0x0f,
  1789. iph6->flow_lbl[1],
  1790. iph6->flow_lbl[2]);
  1791. break;
  1792. }
  1793. }
  1794. void xfrm_audit_state_add(struct xfrm_state *x, int result,
  1795. kuid_t auid, u32 sessionid, u32 secid)
  1796. {
  1797. struct audit_buffer *audit_buf;
  1798. audit_buf = xfrm_audit_start("SAD-add");
  1799. if (audit_buf == NULL)
  1800. return;
  1801. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  1802. xfrm_audit_helper_sainfo(x, audit_buf);
  1803. audit_log_format(audit_buf, " res=%u", result);
  1804. audit_log_end(audit_buf);
  1805. }
  1806. EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
  1807. void xfrm_audit_state_delete(struct xfrm_state *x, int result,
  1808. kuid_t auid, u32 sessionid, u32 secid)
  1809. {
  1810. struct audit_buffer *audit_buf;
  1811. audit_buf = xfrm_audit_start("SAD-delete");
  1812. if (audit_buf == NULL)
  1813. return;
  1814. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  1815. xfrm_audit_helper_sainfo(x, audit_buf);
  1816. audit_log_format(audit_buf, " res=%u", result);
  1817. audit_log_end(audit_buf);
  1818. }
  1819. EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
  1820. void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
  1821. struct sk_buff *skb)
  1822. {
  1823. struct audit_buffer *audit_buf;
  1824. u32 spi;
  1825. audit_buf = xfrm_audit_start("SA-replay-overflow");
  1826. if (audit_buf == NULL)
  1827. return;
  1828. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1829. /* don't record the sequence number because it's inherent in this kind
  1830. * of audit message */
  1831. spi = ntohl(x->id.spi);
  1832. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  1833. audit_log_end(audit_buf);
  1834. }
  1835. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
  1836. void xfrm_audit_state_replay(struct xfrm_state *x,
  1837. struct sk_buff *skb, __be32 net_seq)
  1838. {
  1839. struct audit_buffer *audit_buf;
  1840. u32 spi;
  1841. audit_buf = xfrm_audit_start("SA-replayed-pkt");
  1842. if (audit_buf == NULL)
  1843. return;
  1844. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1845. spi = ntohl(x->id.spi);
  1846. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1847. spi, spi, ntohl(net_seq));
  1848. audit_log_end(audit_buf);
  1849. }
  1850. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
  1851. void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
  1852. {
  1853. struct audit_buffer *audit_buf;
  1854. audit_buf = xfrm_audit_start("SA-notfound");
  1855. if (audit_buf == NULL)
  1856. return;
  1857. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  1858. audit_log_end(audit_buf);
  1859. }
  1860. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
  1861. void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
  1862. __be32 net_spi, __be32 net_seq)
  1863. {
  1864. struct audit_buffer *audit_buf;
  1865. u32 spi;
  1866. audit_buf = xfrm_audit_start("SA-notfound");
  1867. if (audit_buf == NULL)
  1868. return;
  1869. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  1870. spi = ntohl(net_spi);
  1871. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1872. spi, spi, ntohl(net_seq));
  1873. audit_log_end(audit_buf);
  1874. }
  1875. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
  1876. void xfrm_audit_state_icvfail(struct xfrm_state *x,
  1877. struct sk_buff *skb, u8 proto)
  1878. {
  1879. struct audit_buffer *audit_buf;
  1880. __be32 net_spi;
  1881. __be32 net_seq;
  1882. audit_buf = xfrm_audit_start("SA-icv-failure");
  1883. if (audit_buf == NULL)
  1884. return;
  1885. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1886. if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
  1887. u32 spi = ntohl(net_spi);
  1888. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1889. spi, spi, ntohl(net_seq));
  1890. }
  1891. audit_log_end(audit_buf);
  1892. }
  1893. EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
  1894. #endif /* CONFIG_AUDITSYSCALL */