xfrm_state.c 56 KB

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