xfrm_state.c 61 KB

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