xfrm_state.c 54 KB

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