xfrm_state.c 53 KB

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