xfrm_state.c 55 KB

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