xfrm_state.c 54 KB

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