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