xfrm_state.c 55 KB

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