xfrm_state.c 54 KB

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