xfrm_state.c 56 KB

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