xfrm_state.c 53 KB

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