11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * xfrm_state.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Changes: 51da177e4SLinus Torvalds * Mitsuru KANDA @USAGI 61da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI 71da177e4SLinus Torvalds * Kunihiro Ishiguro <kunihiro@ipinfusion.com> 81da177e4SLinus Torvalds * IPv6 support 91da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 101da177e4SLinus Torvalds * Split up af-specific functions 111da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com> 121da177e4SLinus Torvalds * Add UDP Encapsulation 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/workqueue.h> 171da177e4SLinus Torvalds #include <net/xfrm.h> 181da177e4SLinus Torvalds #include <linux/pfkeyv2.h> 191da177e4SLinus Torvalds #include <linux/ipsec.h> 201da177e4SLinus Torvalds #include <linux/module.h> 21f034b5d4SDavid S. Miller #include <linux/cache.h> 2268277accSPaul Moore #include <linux/audit.h> 23b5890d8bSJesper Juhl #include <asm/uaccess.h> 249e0d57fdSYury Polyanskiy #include <linux/ktime.h> 255a0e3ad6STejun Heo #include <linux/slab.h> 269e0d57fdSYury Polyanskiy #include <linux/interrupt.h> 279e0d57fdSYury Polyanskiy #include <linux/kernel.h> 281da177e4SLinus Torvalds 2944e36b42SDavid S. Miller #include "xfrm_hash.h" 3044e36b42SDavid S. Miller 311da177e4SLinus Torvalds /* Each xfrm_state may be linked to two tables: 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl) 34a624c108SDavid S. Miller 2. Hash table by (daddr,family,reqid) to find what SAs exist for given 351da177e4SLinus Torvalds destination/tunnel endpoint. (output) 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds static DEFINE_SPINLOCK(xfrm_state_lock); 391da177e4SLinus Torvalds 40f034b5d4SDavid S. Miller static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024; 411da177e4SLinus Torvalds 4217c2a42aSHerbert Xu static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family); 4317c2a42aSHerbert Xu static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo); 4417c2a42aSHerbert Xu 4564d0cd00SAlexey Dobriyan static inline unsigned int xfrm_dst_hash(struct net *net, 462ab38503SDavid S. Miller const xfrm_address_t *daddr, 472ab38503SDavid S. Miller const xfrm_address_t *saddr, 48c1969f29SDavid S. Miller u32 reqid, 49a624c108SDavid S. Miller unsigned short family) 50a624c108SDavid S. Miller { 5164d0cd00SAlexey Dobriyan return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask); 52a624c108SDavid S. Miller } 53a624c108SDavid S. Miller 5464d0cd00SAlexey Dobriyan static inline unsigned int xfrm_src_hash(struct net *net, 552ab38503SDavid S. Miller const xfrm_address_t *daddr, 562ab38503SDavid S. Miller const xfrm_address_t *saddr, 5744e36b42SDavid S. Miller unsigned short family) 58f034b5d4SDavid S. Miller { 5964d0cd00SAlexey Dobriyan return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask); 60f034b5d4SDavid S. Miller } 61f034b5d4SDavid S. Miller 622575b654SDavid S. Miller static inline unsigned int 632ab38503SDavid S. Miller xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr, 642ab38503SDavid S. Miller __be32 spi, u8 proto, unsigned short family) 65f034b5d4SDavid S. Miller { 6664d0cd00SAlexey Dobriyan return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask); 67f034b5d4SDavid S. Miller } 68f034b5d4SDavid S. Miller 69f034b5d4SDavid S. Miller static void xfrm_hash_transfer(struct hlist_head *list, 70f034b5d4SDavid S. Miller struct hlist_head *ndsttable, 71f034b5d4SDavid S. Miller struct hlist_head *nsrctable, 72f034b5d4SDavid S. Miller struct hlist_head *nspitable, 73f034b5d4SDavid S. Miller unsigned int nhashmask) 74f034b5d4SDavid S. Miller { 75b67bfe0dSSasha Levin struct hlist_node *tmp; 76f034b5d4SDavid S. Miller struct xfrm_state *x; 77f034b5d4SDavid S. Miller 78b67bfe0dSSasha Levin hlist_for_each_entry_safe(x, tmp, list, bydst) { 79f034b5d4SDavid S. Miller unsigned int h; 80f034b5d4SDavid S. Miller 81c1969f29SDavid S. Miller h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr, 82c1969f29SDavid S. Miller x->props.reqid, x->props.family, 83c1969f29SDavid S. Miller nhashmask); 84f034b5d4SDavid S. Miller hlist_add_head(&x->bydst, ndsttable+h); 85f034b5d4SDavid S. Miller 86667bbcb6SMasahide NAKAMURA h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr, 87667bbcb6SMasahide NAKAMURA x->props.family, 88f034b5d4SDavid S. Miller nhashmask); 89f034b5d4SDavid S. Miller hlist_add_head(&x->bysrc, nsrctable+h); 90f034b5d4SDavid S. Miller 917b4dc360SMasahide NAKAMURA if (x->id.spi) { 927b4dc360SMasahide NAKAMURA h = __xfrm_spi_hash(&x->id.daddr, x->id.spi, 937b4dc360SMasahide NAKAMURA x->id.proto, x->props.family, 947b4dc360SMasahide NAKAMURA nhashmask); 95f034b5d4SDavid S. Miller hlist_add_head(&x->byspi, nspitable+h); 96f034b5d4SDavid S. Miller } 97f034b5d4SDavid S. Miller } 987b4dc360SMasahide NAKAMURA } 99f034b5d4SDavid S. Miller 10063082733SAlexey Dobriyan static unsigned long xfrm_hash_new_size(unsigned int state_hmask) 101f034b5d4SDavid S. Miller { 10263082733SAlexey Dobriyan return ((state_hmask + 1) << 1) * sizeof(struct hlist_head); 103f034b5d4SDavid S. Miller } 104f034b5d4SDavid S. Miller 105f034b5d4SDavid S. Miller static DEFINE_MUTEX(hash_resize_mutex); 106f034b5d4SDavid S. Miller 10763082733SAlexey Dobriyan static void xfrm_hash_resize(struct work_struct *work) 108f034b5d4SDavid S. Miller { 10963082733SAlexey Dobriyan struct net *net = container_of(work, struct net, xfrm.state_hash_work); 110f034b5d4SDavid S. Miller struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi; 111f034b5d4SDavid S. Miller unsigned long nsize, osize; 112f034b5d4SDavid S. Miller unsigned int nhashmask, ohashmask; 113f034b5d4SDavid S. Miller int i; 114f034b5d4SDavid S. Miller 115f034b5d4SDavid S. Miller mutex_lock(&hash_resize_mutex); 116f034b5d4SDavid S. Miller 11763082733SAlexey Dobriyan nsize = xfrm_hash_new_size(net->xfrm.state_hmask); 11844e36b42SDavid S. Miller ndst = xfrm_hash_alloc(nsize); 119f034b5d4SDavid S. Miller if (!ndst) 120f034b5d4SDavid S. Miller goto out_unlock; 12144e36b42SDavid S. Miller nsrc = xfrm_hash_alloc(nsize); 122f034b5d4SDavid S. Miller if (!nsrc) { 12344e36b42SDavid S. Miller xfrm_hash_free(ndst, nsize); 124f034b5d4SDavid S. Miller goto out_unlock; 125f034b5d4SDavid S. Miller } 12644e36b42SDavid S. Miller nspi = xfrm_hash_alloc(nsize); 127f034b5d4SDavid S. Miller if (!nspi) { 12844e36b42SDavid S. Miller xfrm_hash_free(ndst, nsize); 12944e36b42SDavid S. Miller xfrm_hash_free(nsrc, nsize); 130f034b5d4SDavid S. Miller goto out_unlock; 131f034b5d4SDavid S. Miller } 132f034b5d4SDavid S. Miller 133f034b5d4SDavid S. Miller spin_lock_bh(&xfrm_state_lock); 134f034b5d4SDavid S. Miller 135f034b5d4SDavid S. Miller nhashmask = (nsize / sizeof(struct hlist_head)) - 1U; 13663082733SAlexey Dobriyan for (i = net->xfrm.state_hmask; i >= 0; i--) 13763082733SAlexey Dobriyan xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi, 138f034b5d4SDavid S. Miller nhashmask); 139f034b5d4SDavid S. Miller 14063082733SAlexey Dobriyan odst = net->xfrm.state_bydst; 14163082733SAlexey Dobriyan osrc = net->xfrm.state_bysrc; 14263082733SAlexey Dobriyan ospi = net->xfrm.state_byspi; 14363082733SAlexey Dobriyan ohashmask = net->xfrm.state_hmask; 144f034b5d4SDavid S. Miller 14563082733SAlexey Dobriyan net->xfrm.state_bydst = ndst; 14663082733SAlexey Dobriyan net->xfrm.state_bysrc = nsrc; 14763082733SAlexey Dobriyan net->xfrm.state_byspi = nspi; 14863082733SAlexey Dobriyan net->xfrm.state_hmask = nhashmask; 149f034b5d4SDavid S. Miller 150f034b5d4SDavid S. Miller spin_unlock_bh(&xfrm_state_lock); 151f034b5d4SDavid S. Miller 152f034b5d4SDavid S. Miller osize = (ohashmask + 1) * sizeof(struct hlist_head); 15344e36b42SDavid S. Miller xfrm_hash_free(odst, osize); 15444e36b42SDavid S. Miller xfrm_hash_free(osrc, osize); 15544e36b42SDavid S. Miller xfrm_hash_free(ospi, osize); 156f034b5d4SDavid S. Miller 157f034b5d4SDavid S. Miller out_unlock: 158f034b5d4SDavid S. Miller mutex_unlock(&hash_resize_mutex); 159f034b5d4SDavid S. Miller } 160f034b5d4SDavid S. Miller 16144abdc30SCong Wang static DEFINE_SPINLOCK(xfrm_state_afinfo_lock); 16244abdc30SCong Wang static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO]; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds static DEFINE_SPINLOCK(xfrm_state_gc_lock); 1651da177e4SLinus Torvalds 16653bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x); 1671da177e4SLinus Torvalds 168980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol); 16915e47304SEric W. Biederman void km_state_expired(struct xfrm_state *x, int hard, u32 portid); 1701da177e4SLinus Torvalds 1717a9885b9SCong Wang static DEFINE_SPINLOCK(xfrm_type_lock); 172533cb5b0SEric Dumazet int xfrm_register_type(const struct xfrm_type *type, unsigned short family) 173aa5d62ccSHerbert Xu { 1747a9885b9SCong Wang struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 175533cb5b0SEric Dumazet const struct xfrm_type **typemap; 176aa5d62ccSHerbert Xu int err = 0; 177aa5d62ccSHerbert Xu 178aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 179aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 180aa5d62ccSHerbert Xu typemap = afinfo->type_map; 1817a9885b9SCong Wang spin_lock_bh(&xfrm_type_lock); 182aa5d62ccSHerbert Xu 183aa5d62ccSHerbert Xu if (likely(typemap[type->proto] == NULL)) 184aa5d62ccSHerbert Xu typemap[type->proto] = type; 185aa5d62ccSHerbert Xu else 186aa5d62ccSHerbert Xu err = -EEXIST; 1877a9885b9SCong Wang spin_unlock_bh(&xfrm_type_lock); 1887a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 189aa5d62ccSHerbert Xu return err; 190aa5d62ccSHerbert Xu } 191aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_type); 192aa5d62ccSHerbert Xu 193533cb5b0SEric Dumazet int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family) 194aa5d62ccSHerbert Xu { 1957a9885b9SCong Wang struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 196533cb5b0SEric Dumazet const struct xfrm_type **typemap; 197aa5d62ccSHerbert Xu int err = 0; 198aa5d62ccSHerbert Xu 199aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 200aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 201aa5d62ccSHerbert Xu typemap = afinfo->type_map; 2027a9885b9SCong Wang spin_lock_bh(&xfrm_type_lock); 203aa5d62ccSHerbert Xu 204aa5d62ccSHerbert Xu if (unlikely(typemap[type->proto] != type)) 205aa5d62ccSHerbert Xu err = -ENOENT; 206aa5d62ccSHerbert Xu else 207aa5d62ccSHerbert Xu typemap[type->proto] = NULL; 2087a9885b9SCong Wang spin_unlock_bh(&xfrm_type_lock); 2097a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 210aa5d62ccSHerbert Xu return err; 211aa5d62ccSHerbert Xu } 212aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_type); 213aa5d62ccSHerbert Xu 214533cb5b0SEric Dumazet static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family) 215aa5d62ccSHerbert Xu { 216aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 217533cb5b0SEric Dumazet const struct xfrm_type **typemap; 218533cb5b0SEric Dumazet const struct xfrm_type *type; 219aa5d62ccSHerbert Xu int modload_attempted = 0; 220aa5d62ccSHerbert Xu 221aa5d62ccSHerbert Xu retry: 222aa5d62ccSHerbert Xu afinfo = xfrm_state_get_afinfo(family); 223aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 224aa5d62ccSHerbert Xu return NULL; 225aa5d62ccSHerbert Xu typemap = afinfo->type_map; 226aa5d62ccSHerbert Xu 227aa5d62ccSHerbert Xu type = typemap[proto]; 228aa5d62ccSHerbert Xu if (unlikely(type && !try_module_get(type->owner))) 229aa5d62ccSHerbert Xu type = NULL; 230aa5d62ccSHerbert Xu if (!type && !modload_attempted) { 231aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 232aa5d62ccSHerbert Xu request_module("xfrm-type-%d-%d", family, proto); 233aa5d62ccSHerbert Xu modload_attempted = 1; 234aa5d62ccSHerbert Xu goto retry; 235aa5d62ccSHerbert Xu } 236aa5d62ccSHerbert Xu 237aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 238aa5d62ccSHerbert Xu return type; 239aa5d62ccSHerbert Xu } 240aa5d62ccSHerbert Xu 241533cb5b0SEric Dumazet static void xfrm_put_type(const struct xfrm_type *type) 242aa5d62ccSHerbert Xu { 243aa5d62ccSHerbert Xu module_put(type->owner); 244aa5d62ccSHerbert Xu } 245aa5d62ccSHerbert Xu 2467a9885b9SCong Wang static DEFINE_SPINLOCK(xfrm_mode_lock); 247aa5d62ccSHerbert Xu int xfrm_register_mode(struct xfrm_mode *mode, int family) 248aa5d62ccSHerbert Xu { 249aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 250aa5d62ccSHerbert Xu struct xfrm_mode **modemap; 251aa5d62ccSHerbert Xu int err; 252aa5d62ccSHerbert Xu 253aa5d62ccSHerbert Xu if (unlikely(mode->encap >= XFRM_MODE_MAX)) 254aa5d62ccSHerbert Xu return -EINVAL; 255aa5d62ccSHerbert Xu 2567a9885b9SCong Wang afinfo = xfrm_state_get_afinfo(family); 257aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 258aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 259aa5d62ccSHerbert Xu 260aa5d62ccSHerbert Xu err = -EEXIST; 261aa5d62ccSHerbert Xu modemap = afinfo->mode_map; 2627a9885b9SCong Wang spin_lock_bh(&xfrm_mode_lock); 26317c2a42aSHerbert Xu if (modemap[mode->encap]) 26417c2a42aSHerbert Xu goto out; 26517c2a42aSHerbert Xu 26617c2a42aSHerbert Xu err = -ENOENT; 26717c2a42aSHerbert Xu if (!try_module_get(afinfo->owner)) 26817c2a42aSHerbert Xu goto out; 26917c2a42aSHerbert Xu 27017c2a42aSHerbert Xu mode->afinfo = afinfo; 271aa5d62ccSHerbert Xu modemap[mode->encap] = mode; 272aa5d62ccSHerbert Xu err = 0; 273aa5d62ccSHerbert Xu 27417c2a42aSHerbert Xu out: 2757a9885b9SCong Wang spin_unlock_bh(&xfrm_mode_lock); 2767a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 277aa5d62ccSHerbert Xu return err; 278aa5d62ccSHerbert Xu } 279aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_mode); 280aa5d62ccSHerbert Xu 281aa5d62ccSHerbert Xu int xfrm_unregister_mode(struct xfrm_mode *mode, int family) 282aa5d62ccSHerbert Xu { 283aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 284aa5d62ccSHerbert Xu struct xfrm_mode **modemap; 285aa5d62ccSHerbert Xu int err; 286aa5d62ccSHerbert Xu 287aa5d62ccSHerbert Xu if (unlikely(mode->encap >= XFRM_MODE_MAX)) 288aa5d62ccSHerbert Xu return -EINVAL; 289aa5d62ccSHerbert Xu 2907a9885b9SCong Wang afinfo = xfrm_state_get_afinfo(family); 291aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 292aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 293aa5d62ccSHerbert Xu 294aa5d62ccSHerbert Xu err = -ENOENT; 295aa5d62ccSHerbert Xu modemap = afinfo->mode_map; 2967a9885b9SCong Wang spin_lock_bh(&xfrm_mode_lock); 297aa5d62ccSHerbert Xu if (likely(modemap[mode->encap] == mode)) { 298aa5d62ccSHerbert Xu modemap[mode->encap] = NULL; 29917c2a42aSHerbert Xu module_put(mode->afinfo->owner); 300aa5d62ccSHerbert Xu err = 0; 301aa5d62ccSHerbert Xu } 302aa5d62ccSHerbert Xu 3037a9885b9SCong Wang spin_unlock_bh(&xfrm_mode_lock); 3047a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 305aa5d62ccSHerbert Xu return err; 306aa5d62ccSHerbert Xu } 307aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_mode); 308aa5d62ccSHerbert Xu 309aa5d62ccSHerbert Xu static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family) 310aa5d62ccSHerbert Xu { 311aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 312aa5d62ccSHerbert Xu struct xfrm_mode *mode; 313aa5d62ccSHerbert Xu int modload_attempted = 0; 314aa5d62ccSHerbert Xu 315aa5d62ccSHerbert Xu if (unlikely(encap >= XFRM_MODE_MAX)) 316aa5d62ccSHerbert Xu return NULL; 317aa5d62ccSHerbert Xu 318aa5d62ccSHerbert Xu retry: 319aa5d62ccSHerbert Xu afinfo = xfrm_state_get_afinfo(family); 320aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 321aa5d62ccSHerbert Xu return NULL; 322aa5d62ccSHerbert Xu 323aa5d62ccSHerbert Xu mode = afinfo->mode_map[encap]; 324aa5d62ccSHerbert Xu if (unlikely(mode && !try_module_get(mode->owner))) 325aa5d62ccSHerbert Xu mode = NULL; 326aa5d62ccSHerbert Xu if (!mode && !modload_attempted) { 327aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 328aa5d62ccSHerbert Xu request_module("xfrm-mode-%d-%d", family, encap); 329aa5d62ccSHerbert Xu modload_attempted = 1; 330aa5d62ccSHerbert Xu goto retry; 331aa5d62ccSHerbert Xu } 332aa5d62ccSHerbert Xu 333aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 334aa5d62ccSHerbert Xu return mode; 335aa5d62ccSHerbert Xu } 336aa5d62ccSHerbert Xu 337aa5d62ccSHerbert Xu static void xfrm_put_mode(struct xfrm_mode *mode) 338aa5d62ccSHerbert Xu { 339aa5d62ccSHerbert Xu module_put(mode->owner); 340aa5d62ccSHerbert Xu } 341aa5d62ccSHerbert Xu 3421da177e4SLinus Torvalds static void xfrm_state_gc_destroy(struct xfrm_state *x) 3431da177e4SLinus Torvalds { 3449e0d57fdSYury Polyanskiy tasklet_hrtimer_cancel(&x->mtimer); 345a47f0ce0SDavid S. Miller del_timer_sync(&x->rtimer); 3461da177e4SLinus Torvalds kfree(x->aalg); 3471da177e4SLinus Torvalds kfree(x->ealg); 3481da177e4SLinus Torvalds kfree(x->calg); 3491da177e4SLinus Torvalds kfree(x->encap); 350060f02a3SNoriaki TAKAMIYA kfree(x->coaddr); 351d8647b79SSteffen Klassert kfree(x->replay_esn); 352d8647b79SSteffen Klassert kfree(x->preplay_esn); 35313996378SHerbert Xu if (x->inner_mode) 35413996378SHerbert Xu xfrm_put_mode(x->inner_mode); 355df9dcb45SKazunori MIYAZAWA if (x->inner_mode_iaf) 356df9dcb45SKazunori MIYAZAWA xfrm_put_mode(x->inner_mode_iaf); 35713996378SHerbert Xu if (x->outer_mode) 35813996378SHerbert Xu xfrm_put_mode(x->outer_mode); 3591da177e4SLinus Torvalds if (x->type) { 3601da177e4SLinus Torvalds x->type->destructor(x); 3611da177e4SLinus Torvalds xfrm_put_type(x->type); 3621da177e4SLinus Torvalds } 363df71837dSTrent Jaeger security_xfrm_state_free(x); 3641da177e4SLinus Torvalds kfree(x); 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 367c7837144SAlexey Dobriyan static void xfrm_state_gc_task(struct work_struct *work) 3681da177e4SLinus Torvalds { 369c7837144SAlexey Dobriyan struct net *net = container_of(work, struct net, xfrm.state_gc_work); 37012a169e7SHerbert Xu struct xfrm_state *x; 371b67bfe0dSSasha Levin struct hlist_node *tmp; 37212a169e7SHerbert Xu struct hlist_head gc_list; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_gc_lock); 375c7837144SAlexey Dobriyan hlist_move_list(&net->xfrm.state_gc_list, &gc_list); 3761da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_gc_lock); 3771da177e4SLinus Torvalds 378b67bfe0dSSasha Levin hlist_for_each_entry_safe(x, tmp, &gc_list, gclist) 3791da177e4SLinus Torvalds xfrm_state_gc_destroy(x); 3808f126e37SDavid S. Miller 38150a30657SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds static inline unsigned long make_jiffies(long secs) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) 3871da177e4SLinus Torvalds return MAX_SCHEDULE_TIMEOUT-1; 3881da177e4SLinus Torvalds else 3891da177e4SLinus Torvalds return secs*HZ; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3929e0d57fdSYury Polyanskiy static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me) 3931da177e4SLinus Torvalds { 3949e0d57fdSYury Polyanskiy struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer); 3959e0d57fdSYury Polyanskiy struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer); 39698806f75SAlexey Dobriyan struct net *net = xs_net(x); 3979d729f72SJames Morris unsigned long now = get_seconds(); 3981da177e4SLinus Torvalds long next = LONG_MAX; 3991da177e4SLinus Torvalds int warn = 0; 400161a09e7SJoy Latten int err = 0; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds spin_lock(&x->lock); 4031da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_DEAD) 4041da177e4SLinus Torvalds goto out; 4051da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_EXPIRED) 4061da177e4SLinus Torvalds goto expired; 4071da177e4SLinus Torvalds if (x->lft.hard_add_expires_seconds) { 4081da177e4SLinus Torvalds long tmo = x->lft.hard_add_expires_seconds + 4091da177e4SLinus Torvalds x->curlft.add_time - now; 410e3c0d047SFan Du if (tmo <= 0) { 411e3c0d047SFan Du if (x->xflags & XFRM_SOFT_EXPIRE) { 412e3c0d047SFan Du /* enter hard expire without soft expire first?! 413e3c0d047SFan Du * setting a new date could trigger this. 414e3c0d047SFan Du * workarbound: fix x->curflt.add_time by below: 415e3c0d047SFan Du */ 416e3c0d047SFan Du x->curlft.add_time = now - x->saved_tmo - 1; 417e3c0d047SFan Du tmo = x->lft.hard_add_expires_seconds - x->saved_tmo; 418e3c0d047SFan Du } else 4191da177e4SLinus Torvalds goto expired; 420e3c0d047SFan Du } 4211da177e4SLinus Torvalds if (tmo < next) 4221da177e4SLinus Torvalds next = tmo; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds if (x->lft.hard_use_expires_seconds) { 4251da177e4SLinus Torvalds long tmo = x->lft.hard_use_expires_seconds + 4261da177e4SLinus Torvalds (x->curlft.use_time ? : now) - now; 4271da177e4SLinus Torvalds if (tmo <= 0) 4281da177e4SLinus Torvalds goto expired; 4291da177e4SLinus Torvalds if (tmo < next) 4301da177e4SLinus Torvalds next = tmo; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds if (x->km.dying) 4331da177e4SLinus Torvalds goto resched; 4341da177e4SLinus Torvalds if (x->lft.soft_add_expires_seconds) { 4351da177e4SLinus Torvalds long tmo = x->lft.soft_add_expires_seconds + 4361da177e4SLinus Torvalds x->curlft.add_time - now; 437e3c0d047SFan Du if (tmo <= 0) { 4381da177e4SLinus Torvalds warn = 1; 439e3c0d047SFan Du x->xflags &= ~XFRM_SOFT_EXPIRE; 440e3c0d047SFan Du } else if (tmo < next) { 4411da177e4SLinus Torvalds next = tmo; 442e3c0d047SFan Du x->xflags |= XFRM_SOFT_EXPIRE; 443e3c0d047SFan Du x->saved_tmo = tmo; 444e3c0d047SFan Du } 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds if (x->lft.soft_use_expires_seconds) { 4471da177e4SLinus Torvalds long tmo = x->lft.soft_use_expires_seconds + 4481da177e4SLinus Torvalds (x->curlft.use_time ? : now) - now; 4491da177e4SLinus Torvalds if (tmo <= 0) 4501da177e4SLinus Torvalds warn = 1; 4511da177e4SLinus Torvalds else if (tmo < next) 4521da177e4SLinus Torvalds next = tmo; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4554666faabSHerbert Xu x->km.dying = warn; 4561da177e4SLinus Torvalds if (warn) 45753bc6b4dSJamal Hadi Salim km_state_expired(x, 0, 0); 4581da177e4SLinus Torvalds resched: 4599e0d57fdSYury Polyanskiy if (next != LONG_MAX){ 4609e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL); 4619e0d57fdSYury Polyanskiy } 462a47f0ce0SDavid S. Miller 4631da177e4SLinus Torvalds goto out; 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds expired: 4661da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) { 4671da177e4SLinus Torvalds x->km.state = XFRM_STATE_EXPIRED; 46898806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 4691da177e4SLinus Torvalds next = 2; 4701da177e4SLinus Torvalds goto resched; 4711da177e4SLinus Torvalds } 472161a09e7SJoy Latten 473161a09e7SJoy Latten err = __xfrm_state_delete(x); 474161a09e7SJoy Latten if (!err && x->id.spi) 47553bc6b4dSJamal Hadi Salim km_state_expired(x, 1, 0); 4761da177e4SLinus Torvalds 477ab5f5e8bSJoy Latten xfrm_audit_state_delete(x, err ? 0 : 1, 4782532386fSEric Paris audit_get_loginuid(current), 4792532386fSEric Paris audit_get_sessionid(current), 0); 480161a09e7SJoy Latten 4811da177e4SLinus Torvalds out: 4821da177e4SLinus Torvalds spin_unlock(&x->lock); 4839e0d57fdSYury Polyanskiy return HRTIMER_NORESTART; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 4860ac84752SDavid S. Miller static void xfrm_replay_timer_handler(unsigned long data); 4870ac84752SDavid S. Miller 488673c09beSAlexey Dobriyan struct xfrm_state *xfrm_state_alloc(struct net *net) 4891da177e4SLinus Torvalds { 4901da177e4SLinus Torvalds struct xfrm_state *x; 4911da177e4SLinus Torvalds 4920da974f4SPanagiotis Issaris x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (x) { 495673c09beSAlexey Dobriyan write_pnet(&x->xs_net, net); 4961da177e4SLinus Torvalds atomic_set(&x->refcnt, 1); 4971da177e4SLinus Torvalds atomic_set(&x->tunnel_users, 0); 49812a169e7SHerbert Xu INIT_LIST_HEAD(&x->km.all); 4998f126e37SDavid S. Miller INIT_HLIST_NODE(&x->bydst); 5008f126e37SDavid S. Miller INIT_HLIST_NODE(&x->bysrc); 5018f126e37SDavid S. Miller INIT_HLIST_NODE(&x->byspi); 5029e0d57fdSYury Polyanskiy tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS); 503b24b8a24SPavel Emelyanov setup_timer(&x->rtimer, xfrm_replay_timer_handler, 504b24b8a24SPavel Emelyanov (unsigned long)x); 5059d729f72SJames Morris x->curlft.add_time = get_seconds(); 5061da177e4SLinus Torvalds x->lft.soft_byte_limit = XFRM_INF; 5071da177e4SLinus Torvalds x->lft.soft_packet_limit = XFRM_INF; 5081da177e4SLinus Torvalds x->lft.hard_byte_limit = XFRM_INF; 5091da177e4SLinus Torvalds x->lft.hard_packet_limit = XFRM_INF; 510f8cd5488SJamal Hadi Salim x->replay_maxage = 0; 511f8cd5488SJamal Hadi Salim x->replay_maxdiff = 0; 512df9dcb45SKazunori MIYAZAWA x->inner_mode = NULL; 513df9dcb45SKazunori MIYAZAWA x->inner_mode_iaf = NULL; 5141da177e4SLinus Torvalds spin_lock_init(&x->lock); 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds return x; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_alloc); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds void __xfrm_state_destroy(struct xfrm_state *x) 5211da177e4SLinus Torvalds { 52298806f75SAlexey Dobriyan struct net *net = xs_net(x); 52398806f75SAlexey Dobriyan 524547b792cSIlpo Järvinen WARN_ON(x->km.state != XFRM_STATE_DEAD); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_gc_lock); 52798806f75SAlexey Dobriyan hlist_add_head(&x->gclist, &net->xfrm.state_gc_list); 5281da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_gc_lock); 52998806f75SAlexey Dobriyan schedule_work(&net->xfrm.state_gc_work); 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds EXPORT_SYMBOL(__xfrm_state_destroy); 5321da177e4SLinus Torvalds 53353bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x) 5341da177e4SLinus Torvalds { 53598806f75SAlexey Dobriyan struct net *net = xs_net(x); 53626b15dadSJamal Hadi Salim int err = -ESRCH; 53726b15dadSJamal Hadi Salim 5381da177e4SLinus Torvalds if (x->km.state != XFRM_STATE_DEAD) { 5391da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 5401da177e4SLinus Torvalds spin_lock(&xfrm_state_lock); 54112a169e7SHerbert Xu list_del(&x->km.all); 5428f126e37SDavid S. Miller hlist_del(&x->bydst); 5438f126e37SDavid S. Miller hlist_del(&x->bysrc); 544a47f0ce0SDavid S. Miller if (x->id.spi) 5458f126e37SDavid S. Miller hlist_del(&x->byspi); 54698806f75SAlexey Dobriyan net->xfrm.state_num--; 5471da177e4SLinus Torvalds spin_unlock(&xfrm_state_lock); 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* All xfrm_state objects are created by xfrm_state_alloc. 5501da177e4SLinus Torvalds * The xfrm_state_alloc call gives a reference, and that 5511da177e4SLinus Torvalds * is what we are dropping here. 5521da177e4SLinus Torvalds */ 5535dba4797SPatrick McHardy xfrm_state_put(x); 55426b15dadSJamal Hadi Salim err = 0; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 55726b15dadSJamal Hadi Salim return err; 55826b15dadSJamal Hadi Salim } 55953bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(__xfrm_state_delete); 56026b15dadSJamal Hadi Salim 56126b15dadSJamal Hadi Salim int xfrm_state_delete(struct xfrm_state *x) 5621da177e4SLinus Torvalds { 56326b15dadSJamal Hadi Salim int err; 56426b15dadSJamal Hadi Salim 5651da177e4SLinus Torvalds spin_lock_bh(&x->lock); 56626b15dadSJamal Hadi Salim err = __xfrm_state_delete(x); 5671da177e4SLinus Torvalds spin_unlock_bh(&x->lock); 56826b15dadSJamal Hadi Salim 56926b15dadSJamal Hadi Salim return err; 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete); 5721da177e4SLinus Torvalds 5734aa2e62cSJoy Latten #ifdef CONFIG_SECURITY_NETWORK_XFRM 5744aa2e62cSJoy Latten static inline int 5750e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info) 5761da177e4SLinus Torvalds { 5774aa2e62cSJoy Latten int i, err = 0; 5784aa2e62cSJoy Latten 5790e602451SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 5804aa2e62cSJoy Latten struct xfrm_state *x; 5814aa2e62cSJoy Latten 582b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { 5834aa2e62cSJoy Latten if (xfrm_id_proto_match(x->id.proto, proto) && 5844aa2e62cSJoy Latten (err = security_xfrm_state_delete(x)) != 0) { 585ab5f5e8bSJoy Latten xfrm_audit_state_delete(x, 0, 586ab5f5e8bSJoy Latten audit_info->loginuid, 5872532386fSEric Paris audit_info->sessionid, 588ab5f5e8bSJoy Latten audit_info->secid); 5894aa2e62cSJoy Latten return err; 5904aa2e62cSJoy Latten } 5914aa2e62cSJoy Latten } 5924aa2e62cSJoy Latten } 5934aa2e62cSJoy Latten 5944aa2e62cSJoy Latten return err; 5954aa2e62cSJoy Latten } 5964aa2e62cSJoy Latten #else 5974aa2e62cSJoy Latten static inline int 5980e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info) 5994aa2e62cSJoy Latten { 6004aa2e62cSJoy Latten return 0; 6014aa2e62cSJoy Latten } 6024aa2e62cSJoy Latten #endif 6034aa2e62cSJoy Latten 6040e602451SAlexey Dobriyan int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info) 6054aa2e62cSJoy Latten { 6069e64cc95SJamal Hadi Salim int i, err = 0, cnt = 0; 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 6090e602451SAlexey Dobriyan err = xfrm_state_flush_secctx_check(net, proto, audit_info); 6104aa2e62cSJoy Latten if (err) 6114aa2e62cSJoy Latten goto out; 6124aa2e62cSJoy Latten 6139e64cc95SJamal Hadi Salim err = -ESRCH; 6140e602451SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 6158f126e37SDavid S. Miller struct xfrm_state *x; 6161da177e4SLinus Torvalds restart: 617b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { 6181da177e4SLinus Torvalds if (!xfrm_state_kern(x) && 6195794708fSMasahide NAKAMURA xfrm_id_proto_match(x->id.proto, proto)) { 6201da177e4SLinus Torvalds xfrm_state_hold(x); 6211da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 6221da177e4SLinus Torvalds 623161a09e7SJoy Latten err = xfrm_state_delete(x); 624ab5f5e8bSJoy Latten xfrm_audit_state_delete(x, err ? 0 : 1, 625ab5f5e8bSJoy Latten audit_info->loginuid, 6262532386fSEric Paris audit_info->sessionid, 627ab5f5e8bSJoy Latten audit_info->secid); 6281da177e4SLinus Torvalds xfrm_state_put(x); 6299e64cc95SJamal Hadi Salim if (!err) 6309e64cc95SJamal Hadi Salim cnt++; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 6331da177e4SLinus Torvalds goto restart; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds } 6379e64cc95SJamal Hadi Salim if (cnt) 6384aa2e62cSJoy Latten err = 0; 6394aa2e62cSJoy Latten 6404aa2e62cSJoy Latten out: 6411da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 6420e602451SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 6434aa2e62cSJoy Latten return err; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_flush); 6461da177e4SLinus Torvalds 647e071041bSAlexey Dobriyan void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si) 64828d8909bSJamal Hadi Salim { 64928d8909bSJamal Hadi Salim spin_lock_bh(&xfrm_state_lock); 650e071041bSAlexey Dobriyan si->sadcnt = net->xfrm.state_num; 651e071041bSAlexey Dobriyan si->sadhcnt = net->xfrm.state_hmask; 65228d8909bSJamal Hadi Salim si->sadhmcnt = xfrm_state_hashmax; 65328d8909bSJamal Hadi Salim spin_unlock_bh(&xfrm_state_lock); 65428d8909bSJamal Hadi Salim } 65528d8909bSJamal Hadi Salim EXPORT_SYMBOL(xfrm_sad_getinfo); 65628d8909bSJamal Hadi Salim 6571da177e4SLinus Torvalds static int 6581a898592SDavid S. Miller xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl, 65904686013SDavid S. Miller const struct xfrm_tmpl *tmpl, 66033765d06SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 6611da177e4SLinus Torvalds unsigned short family) 6621da177e4SLinus Torvalds { 6631da177e4SLinus Torvalds struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 6641da177e4SLinus Torvalds if (!afinfo) 6651da177e4SLinus Torvalds return -1; 6668444cf71SThomas Egerer afinfo->init_tempsel(&x->sel, fl); 6678444cf71SThomas Egerer 6688444cf71SThomas Egerer if (family != tmpl->encap_family) { 6698444cf71SThomas Egerer xfrm_state_put_afinfo(afinfo); 6708444cf71SThomas Egerer afinfo = xfrm_state_get_afinfo(tmpl->encap_family); 6718444cf71SThomas Egerer if (!afinfo) 6728444cf71SThomas Egerer return -1; 6738444cf71SThomas Egerer } 6748444cf71SThomas Egerer afinfo->init_temprop(x, tmpl, daddr, saddr); 6751da177e4SLinus Torvalds xfrm_state_put_afinfo(afinfo); 6761da177e4SLinus Torvalds return 0; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 6799aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, 6809aa60088SDavid S. Miller const xfrm_address_t *daddr, 6819aa60088SDavid S. Miller __be32 spi, u8 proto, 6829aa60088SDavid S. Miller unsigned short family) 683edcd5821SDavid S. Miller { 684221df1edSAlexey Dobriyan unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family); 685edcd5821SDavid S. Miller struct xfrm_state *x; 686edcd5821SDavid S. Miller 687b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) { 688edcd5821SDavid S. Miller if (x->props.family != family || 689edcd5821SDavid S. Miller x->id.spi != spi || 6901802571bSWei Yongjun x->id.proto != proto || 69170e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->id.daddr, daddr, family)) 692edcd5821SDavid S. Miller continue; 693edcd5821SDavid S. Miller 6943d6acfa7SJamal Hadi Salim if ((mark & x->mark.m) != x->mark.v) 6953d6acfa7SJamal Hadi Salim continue; 696edcd5821SDavid S. Miller xfrm_state_hold(x); 697edcd5821SDavid S. Miller return x; 698edcd5821SDavid S. Miller } 699edcd5821SDavid S. Miller 700edcd5821SDavid S. Miller return NULL; 701edcd5821SDavid S. Miller } 702edcd5821SDavid S. Miller 7039aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, 7049aa60088SDavid S. Miller const xfrm_address_t *daddr, 7059aa60088SDavid S. Miller const xfrm_address_t *saddr, 7069aa60088SDavid S. Miller u8 proto, unsigned short family) 707edcd5821SDavid S. Miller { 708221df1edSAlexey Dobriyan unsigned int h = xfrm_src_hash(net, daddr, saddr, family); 709edcd5821SDavid S. Miller struct xfrm_state *x; 710edcd5821SDavid S. Miller 711b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) { 712edcd5821SDavid S. Miller if (x->props.family != family || 7131802571bSWei Yongjun x->id.proto != proto || 71470e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->id.daddr, daddr, family) || 71570e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, saddr, family)) 716edcd5821SDavid S. Miller continue; 717edcd5821SDavid S. Miller 7183d6acfa7SJamal Hadi Salim if ((mark & x->mark.m) != x->mark.v) 7193d6acfa7SJamal Hadi Salim continue; 720edcd5821SDavid S. Miller xfrm_state_hold(x); 721edcd5821SDavid S. Miller return x; 722edcd5821SDavid S. Miller } 723edcd5821SDavid S. Miller 724edcd5821SDavid S. Miller return NULL; 725edcd5821SDavid S. Miller } 726edcd5821SDavid S. Miller 727edcd5821SDavid S. Miller static inline struct xfrm_state * 728edcd5821SDavid S. Miller __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family) 729edcd5821SDavid S. Miller { 730221df1edSAlexey Dobriyan struct net *net = xs_net(x); 731bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 732221df1edSAlexey Dobriyan 733edcd5821SDavid S. Miller if (use_spi) 734bd55775cSJamal Hadi Salim return __xfrm_state_lookup(net, mark, &x->id.daddr, 735bd55775cSJamal Hadi Salim x->id.spi, x->id.proto, family); 736edcd5821SDavid S. Miller else 737bd55775cSJamal Hadi Salim return __xfrm_state_lookup_byaddr(net, mark, 738bd55775cSJamal Hadi Salim &x->id.daddr, 739edcd5821SDavid S. Miller &x->props.saddr, 740edcd5821SDavid S. Miller x->id.proto, family); 741edcd5821SDavid S. Miller } 742edcd5821SDavid S. Miller 74398806f75SAlexey Dobriyan static void xfrm_hash_grow_check(struct net *net, int have_hash_collision) 7442fab22f2SPatrick McHardy { 7452fab22f2SPatrick McHardy if (have_hash_collision && 74698806f75SAlexey Dobriyan (net->xfrm.state_hmask + 1) < xfrm_state_hashmax && 74798806f75SAlexey Dobriyan net->xfrm.state_num > net->xfrm.state_hmask) 74898806f75SAlexey Dobriyan schedule_work(&net->xfrm.state_hash_work); 7492fab22f2SPatrick McHardy } 7502fab22f2SPatrick McHardy 75108ec9af1SDavid S. Miller static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x, 7524a08ab0fSDavid S. Miller const struct flowi *fl, unsigned short family, 75308ec9af1SDavid S. Miller struct xfrm_state **best, int *acq_in_progress, 75408ec9af1SDavid S. Miller int *error) 75508ec9af1SDavid S. Miller { 75608ec9af1SDavid S. Miller /* Resolution logic: 75708ec9af1SDavid S. Miller * 1. There is a valid state with matching selector. Done. 75808ec9af1SDavid S. Miller * 2. Valid state with inappropriate selector. Skip. 75908ec9af1SDavid S. Miller * 76008ec9af1SDavid S. Miller * Entering area of "sysdeps". 76108ec9af1SDavid S. Miller * 76208ec9af1SDavid S. Miller * 3. If state is not valid, selector is temporary, it selects 76308ec9af1SDavid S. Miller * only session which triggered previous resolution. Key 76408ec9af1SDavid S. Miller * manager will do something to install a state with proper 76508ec9af1SDavid S. Miller * selector. 76608ec9af1SDavid S. Miller */ 76708ec9af1SDavid S. Miller if (x->km.state == XFRM_STATE_VALID) { 76808ec9af1SDavid S. Miller if ((x->sel.family && 76908ec9af1SDavid S. Miller !xfrm_selector_match(&x->sel, fl, x->sel.family)) || 77008ec9af1SDavid S. Miller !security_xfrm_state_pol_flow_match(x, pol, fl)) 77108ec9af1SDavid S. Miller return; 77208ec9af1SDavid S. Miller 77308ec9af1SDavid S. Miller if (!*best || 77408ec9af1SDavid S. Miller (*best)->km.dying > x->km.dying || 77508ec9af1SDavid S. Miller ((*best)->km.dying == x->km.dying && 77608ec9af1SDavid S. Miller (*best)->curlft.add_time < x->curlft.add_time)) 77708ec9af1SDavid S. Miller *best = x; 77808ec9af1SDavid S. Miller } else if (x->km.state == XFRM_STATE_ACQ) { 77908ec9af1SDavid S. Miller *acq_in_progress = 1; 78008ec9af1SDavid S. Miller } else if (x->km.state == XFRM_STATE_ERROR || 78108ec9af1SDavid S. Miller x->km.state == XFRM_STATE_EXPIRED) { 78208ec9af1SDavid S. Miller if (xfrm_selector_match(&x->sel, fl, x->sel.family) && 78308ec9af1SDavid S. Miller security_xfrm_state_pol_flow_match(x, pol, fl)) 78408ec9af1SDavid S. Miller *error = -ESRCH; 78508ec9af1SDavid S. Miller } 78608ec9af1SDavid S. Miller } 78708ec9af1SDavid S. Miller 7881da177e4SLinus Torvalds struct xfrm_state * 78933765d06SDavid S. Miller xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, 790b520e9f6SDavid S. Miller const struct flowi *fl, struct xfrm_tmpl *tmpl, 7911da177e4SLinus Torvalds struct xfrm_policy *pol, int *err, 7921da177e4SLinus Torvalds unsigned short family) 7931da177e4SLinus Torvalds { 79408ec9af1SDavid S. Miller static xfrm_address_t saddr_wildcard = { }; 7955447c5e4SAlexey Dobriyan struct net *net = xp_net(pol); 7966a783c90SNicolas Dichtel unsigned int h, h_wildcard; 79737b08e34SDavid S. Miller struct xfrm_state *x, *x0, *to_put; 7981da177e4SLinus Torvalds int acquire_in_progress = 0; 7991da177e4SLinus Torvalds int error = 0; 8001da177e4SLinus Torvalds struct xfrm_state *best = NULL; 801bd55775cSJamal Hadi Salim u32 mark = pol->mark.v & pol->mark.m; 8028444cf71SThomas Egerer unsigned short encap_family = tmpl->encap_family; 8031da177e4SLinus Torvalds 80437b08e34SDavid S. Miller to_put = NULL; 80537b08e34SDavid S. Miller 8061da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 8078444cf71SThomas Egerer h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family); 808b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 8098444cf71SThomas Egerer if (x->props.family == encap_family && 8101da177e4SLinus Torvalds x->props.reqid == tmpl->reqid && 8113d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 812fbd9a5b4SMasahide NAKAMURA !(x->props.flags & XFRM_STATE_WILDRECV) && 8138444cf71SThomas Egerer xfrm_state_addr_check(x, daddr, saddr, encap_family) && 8141da177e4SLinus Torvalds tmpl->mode == x->props.mode && 8151da177e4SLinus Torvalds tmpl->id.proto == x->id.proto && 81608ec9af1SDavid S. Miller (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) 8171f673c5fSDavid S. Miller xfrm_state_look_at(pol, x, fl, encap_family, 81808ec9af1SDavid S. Miller &best, &acquire_in_progress, &error); 8191da177e4SLinus Torvalds } 82008ec9af1SDavid S. Miller if (best) 82108ec9af1SDavid S. Miller goto found; 82208ec9af1SDavid S. Miller 8238444cf71SThomas Egerer h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family); 824b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) { 8258444cf71SThomas Egerer if (x->props.family == encap_family && 82608ec9af1SDavid S. Miller x->props.reqid == tmpl->reqid && 8273d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 82808ec9af1SDavid S. Miller !(x->props.flags & XFRM_STATE_WILDRECV) && 8298444cf71SThomas Egerer xfrm_state_addr_check(x, daddr, saddr, encap_family) && 83008ec9af1SDavid S. Miller tmpl->mode == x->props.mode && 83108ec9af1SDavid S. Miller tmpl->id.proto == x->id.proto && 83208ec9af1SDavid S. Miller (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) 8331f673c5fSDavid S. Miller xfrm_state_look_at(pol, x, fl, encap_family, 83408ec9af1SDavid S. Miller &best, &acquire_in_progress, &error); 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 83708ec9af1SDavid S. Miller found: 8381da177e4SLinus Torvalds x = best; 8391da177e4SLinus Torvalds if (!x && !error && !acquire_in_progress) { 8405c5d281aSPatrick McHardy if (tmpl->id.spi && 841bd55775cSJamal Hadi Salim (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi, 8428444cf71SThomas Egerer tmpl->id.proto, encap_family)) != NULL) { 84337b08e34SDavid S. Miller to_put = x0; 8441da177e4SLinus Torvalds error = -EEXIST; 8451da177e4SLinus Torvalds goto out; 8461da177e4SLinus Torvalds } 8475447c5e4SAlexey Dobriyan x = xfrm_state_alloc(net); 8481da177e4SLinus Torvalds if (x == NULL) { 8491da177e4SLinus Torvalds error = -ENOMEM; 8501da177e4SLinus Torvalds goto out; 8511da177e4SLinus Torvalds } 8528444cf71SThomas Egerer /* Initialize temporary state matching only 8531da177e4SLinus Torvalds * to current session. */ 8548444cf71SThomas Egerer xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family); 855bd55775cSJamal Hadi Salim memcpy(&x->mark, &pol->mark, sizeof(x->mark)); 8561da177e4SLinus Torvalds 8571d28f42cSDavid S. Miller error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid); 858e0d1caa7SVenkat Yekkirala if (error) { 859e0d1caa7SVenkat Yekkirala x->km.state = XFRM_STATE_DEAD; 86037b08e34SDavid S. Miller to_put = x; 861e0d1caa7SVenkat Yekkirala x = NULL; 862e0d1caa7SVenkat Yekkirala goto out; 863e0d1caa7SVenkat Yekkirala } 864e0d1caa7SVenkat Yekkirala 8651da177e4SLinus Torvalds if (km_query(x, tmpl, pol) == 0) { 8661da177e4SLinus Torvalds x->km.state = XFRM_STATE_ACQ; 8675447c5e4SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 8685447c5e4SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 8698444cf71SThomas Egerer h = xfrm_src_hash(net, daddr, saddr, encap_family); 8705447c5e4SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 8711da177e4SLinus Torvalds if (x->id.spi) { 8728444cf71SThomas Egerer h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family); 8735447c5e4SAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 8741da177e4SLinus Torvalds } 875b27aeadbSAlexey Dobriyan x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; 8769e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL); 8775447c5e4SAlexey Dobriyan net->xfrm.state_num++; 8785447c5e4SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 8791da177e4SLinus Torvalds } else { 8801da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 88137b08e34SDavid S. Miller to_put = x; 8821da177e4SLinus Torvalds x = NULL; 8831da177e4SLinus Torvalds error = -ESRCH; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds out: 8871da177e4SLinus Torvalds if (x) 8881da177e4SLinus Torvalds xfrm_state_hold(x); 8891da177e4SLinus Torvalds else 8901da177e4SLinus Torvalds *err = acquire_in_progress ? -EAGAIN : error; 8911da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 89237b08e34SDavid S. Miller if (to_put) 89337b08e34SDavid S. Miller xfrm_state_put(to_put); 8941da177e4SLinus Torvalds return x; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds 897628529b6SJamal Hadi Salim struct xfrm_state * 898bd55775cSJamal Hadi Salim xfrm_stateonly_find(struct net *net, u32 mark, 8995447c5e4SAlexey Dobriyan xfrm_address_t *daddr, xfrm_address_t *saddr, 900628529b6SJamal Hadi Salim unsigned short family, u8 mode, u8 proto, u32 reqid) 901628529b6SJamal Hadi Salim { 9024bda4f25SPavel Emelyanov unsigned int h; 903628529b6SJamal Hadi Salim struct xfrm_state *rx = NULL, *x = NULL; 904628529b6SJamal Hadi Salim 905628529b6SJamal Hadi Salim spin_lock(&xfrm_state_lock); 9065447c5e4SAlexey Dobriyan h = xfrm_dst_hash(net, daddr, saddr, reqid, family); 907b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 908628529b6SJamal Hadi Salim if (x->props.family == family && 909628529b6SJamal Hadi Salim x->props.reqid == reqid && 9103d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 911628529b6SJamal Hadi Salim !(x->props.flags & XFRM_STATE_WILDRECV) && 912628529b6SJamal Hadi Salim xfrm_state_addr_check(x, daddr, saddr, family) && 913628529b6SJamal Hadi Salim mode == x->props.mode && 914628529b6SJamal Hadi Salim proto == x->id.proto && 915628529b6SJamal Hadi Salim x->km.state == XFRM_STATE_VALID) { 916628529b6SJamal Hadi Salim rx = x; 917628529b6SJamal Hadi Salim break; 918628529b6SJamal Hadi Salim } 919628529b6SJamal Hadi Salim } 920628529b6SJamal Hadi Salim 921628529b6SJamal Hadi Salim if (rx) 922628529b6SJamal Hadi Salim xfrm_state_hold(rx); 923628529b6SJamal Hadi Salim spin_unlock(&xfrm_state_lock); 924628529b6SJamal Hadi Salim 925628529b6SJamal Hadi Salim 926628529b6SJamal Hadi Salim return rx; 927628529b6SJamal Hadi Salim } 928628529b6SJamal Hadi Salim EXPORT_SYMBOL(xfrm_stateonly_find); 929628529b6SJamal Hadi Salim 9301da177e4SLinus Torvalds static void __xfrm_state_insert(struct xfrm_state *x) 9311da177e4SLinus Torvalds { 93298806f75SAlexey Dobriyan struct net *net = xs_net(x); 933a624c108SDavid S. Miller unsigned int h; 9341da177e4SLinus Torvalds 93598806f75SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 9364c563f76STimo Teras 93798806f75SAlexey Dobriyan h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr, 938c1969f29SDavid S. Miller x->props.reqid, x->props.family); 93998806f75SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 9401da177e4SLinus Torvalds 94198806f75SAlexey Dobriyan h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family); 94298806f75SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 9436c44e6b7SMasahide NAKAMURA 9447b4dc360SMasahide NAKAMURA if (x->id.spi) { 94598806f75SAlexey Dobriyan h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, 9466c44e6b7SMasahide NAKAMURA x->props.family); 9471da177e4SLinus Torvalds 94898806f75SAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 9496c44e6b7SMasahide NAKAMURA } 9501da177e4SLinus Torvalds 9519e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); 952a47f0ce0SDavid S. Miller if (x->replay_maxage) 953a47f0ce0SDavid S. Miller mod_timer(&x->rtimer, jiffies + x->replay_maxage); 954f8cd5488SJamal Hadi Salim 95598806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 956f034b5d4SDavid S. Miller 95798806f75SAlexey Dobriyan net->xfrm.state_num++; 958f034b5d4SDavid S. Miller 95998806f75SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 962c7f5ea3aSDavid S. Miller /* xfrm_state_lock is held */ 963c7f5ea3aSDavid S. Miller static void __xfrm_state_bump_genids(struct xfrm_state *xnew) 964c7f5ea3aSDavid S. Miller { 96598806f75SAlexey Dobriyan struct net *net = xs_net(xnew); 966c7f5ea3aSDavid S. Miller unsigned short family = xnew->props.family; 967c7f5ea3aSDavid S. Miller u32 reqid = xnew->props.reqid; 968c7f5ea3aSDavid S. Miller struct xfrm_state *x; 969c7f5ea3aSDavid S. Miller unsigned int h; 9703d6acfa7SJamal Hadi Salim u32 mark = xnew->mark.v & xnew->mark.m; 971c7f5ea3aSDavid S. Miller 97298806f75SAlexey Dobriyan h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family); 973b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 974c7f5ea3aSDavid S. Miller if (x->props.family == family && 975c7f5ea3aSDavid S. Miller x->props.reqid == reqid && 9763d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 97770e94e66SYOSHIFUJI Hideaki / 吉藤英明 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) && 97870e94e66SYOSHIFUJI Hideaki / 吉藤英明 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family)) 97934996cb9SHerbert Xu x->genid++; 980c7f5ea3aSDavid S. Miller } 981c7f5ea3aSDavid S. Miller } 982c7f5ea3aSDavid S. Miller 9831da177e4SLinus Torvalds void xfrm_state_insert(struct xfrm_state *x) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 986c7f5ea3aSDavid S. Miller __xfrm_state_bump_genids(x); 9871da177e4SLinus Torvalds __xfrm_state_insert(x); 9881da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_insert); 9911da177e4SLinus Torvalds 9922770834cSDavid S. Miller /* xfrm_state_lock is held */ 993a70486f0SDavid S. Miller static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m, 994a70486f0SDavid S. Miller unsigned short family, u8 mode, 995a70486f0SDavid S. Miller u32 reqid, u8 proto, 996a70486f0SDavid S. Miller const xfrm_address_t *daddr, 997a70486f0SDavid S. Miller const xfrm_address_t *saddr, int create) 9982770834cSDavid S. Miller { 9995447c5e4SAlexey Dobriyan unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family); 10002770834cSDavid S. Miller struct xfrm_state *x; 10013d6acfa7SJamal Hadi Salim u32 mark = m->v & m->m; 10022770834cSDavid S. Miller 1003b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 10042770834cSDavid S. Miller if (x->props.reqid != reqid || 10052770834cSDavid S. Miller x->props.mode != mode || 10062770834cSDavid S. Miller x->props.family != family || 10072770834cSDavid S. Miller x->km.state != XFRM_STATE_ACQ || 100875e252d9SJoy Latten x->id.spi != 0 || 10091802571bSWei Yongjun x->id.proto != proto || 10103d6acfa7SJamal Hadi Salim (mark & x->mark.m) != x->mark.v || 101170e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->id.daddr, daddr, family) || 101270e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, saddr, family)) 10132770834cSDavid S. Miller continue; 10142770834cSDavid S. Miller 10152770834cSDavid S. Miller xfrm_state_hold(x); 10162770834cSDavid S. Miller return x; 10172770834cSDavid S. Miller } 10182770834cSDavid S. Miller 10192770834cSDavid S. Miller if (!create) 10202770834cSDavid S. Miller return NULL; 10212770834cSDavid S. Miller 10225447c5e4SAlexey Dobriyan x = xfrm_state_alloc(net); 10232770834cSDavid S. Miller if (likely(x)) { 10242770834cSDavid S. Miller switch (family) { 10252770834cSDavid S. Miller case AF_INET: 10262770834cSDavid S. Miller x->sel.daddr.a4 = daddr->a4; 10272770834cSDavid S. Miller x->sel.saddr.a4 = saddr->a4; 10282770834cSDavid S. Miller x->sel.prefixlen_d = 32; 10292770834cSDavid S. Miller x->sel.prefixlen_s = 32; 10302770834cSDavid S. Miller x->props.saddr.a4 = saddr->a4; 10312770834cSDavid S. Miller x->id.daddr.a4 = daddr->a4; 10322770834cSDavid S. Miller break; 10332770834cSDavid S. Miller 10342770834cSDavid S. Miller case AF_INET6: 10354e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr; 10364e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr; 10372770834cSDavid S. Miller x->sel.prefixlen_d = 128; 10382770834cSDavid S. Miller x->sel.prefixlen_s = 128; 10394e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr; 10404e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr; 10412770834cSDavid S. Miller break; 10423ff50b79SStephen Hemminger } 10432770834cSDavid S. Miller 10442770834cSDavid S. Miller x->km.state = XFRM_STATE_ACQ; 10452770834cSDavid S. Miller x->id.proto = proto; 10462770834cSDavid S. Miller x->props.family = family; 10472770834cSDavid S. Miller x->props.mode = mode; 10482770834cSDavid S. Miller x->props.reqid = reqid; 1049bd55775cSJamal Hadi Salim x->mark.v = m->v; 1050bd55775cSJamal Hadi Salim x->mark.m = m->m; 1051b27aeadbSAlexey Dobriyan x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; 10522770834cSDavid S. Miller xfrm_state_hold(x); 10539e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL); 10545447c5e4SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 10555447c5e4SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 10565447c5e4SAlexey Dobriyan h = xfrm_src_hash(net, daddr, saddr, family); 10575447c5e4SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 1058918049f0SDavid S. Miller 10595447c5e4SAlexey Dobriyan net->xfrm.state_num++; 1060918049f0SDavid S. Miller 10615447c5e4SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 10622770834cSDavid S. Miller } 10632770834cSDavid S. Miller 10642770834cSDavid S. Miller return x; 10652770834cSDavid S. Miller } 10662770834cSDavid S. Miller 1067bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq); 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds int xfrm_state_add(struct xfrm_state *x) 10701da177e4SLinus Torvalds { 10715447c5e4SAlexey Dobriyan struct net *net = xs_net(x); 107237b08e34SDavid S. Miller struct xfrm_state *x1, *to_put; 10731da177e4SLinus Torvalds int family; 10741da177e4SLinus Torvalds int err; 1075bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 1076eb2971b6SMasahide NAKAMURA int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds family = x->props.family; 10791da177e4SLinus Torvalds 108037b08e34SDavid S. Miller to_put = NULL; 108137b08e34SDavid S. Miller 10821da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 10831da177e4SLinus Torvalds 1084edcd5821SDavid S. Miller x1 = __xfrm_state_locate(x, use_spi, family); 10851da177e4SLinus Torvalds if (x1) { 108637b08e34SDavid S. Miller to_put = x1; 10871da177e4SLinus Torvalds x1 = NULL; 10881da177e4SLinus Torvalds err = -EEXIST; 10891da177e4SLinus Torvalds goto out; 10901da177e4SLinus Torvalds } 10911da177e4SLinus Torvalds 1092eb2971b6SMasahide NAKAMURA if (use_spi && x->km.seq) { 1093bd55775cSJamal Hadi Salim x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq); 109475e252d9SJoy Latten if (x1 && ((x1->id.proto != x->id.proto) || 109570e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) { 109637b08e34SDavid S. Miller to_put = x1; 10971da177e4SLinus Torvalds x1 = NULL; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds 1101eb2971b6SMasahide NAKAMURA if (use_spi && !x1) 1102bd55775cSJamal Hadi Salim x1 = __find_acq_core(net, &x->mark, family, x->props.mode, 1103bd55775cSJamal Hadi Salim x->props.reqid, x->id.proto, 11041da177e4SLinus Torvalds &x->id.daddr, &x->props.saddr, 0); 11051da177e4SLinus Torvalds 1106c7f5ea3aSDavid S. Miller __xfrm_state_bump_genids(x); 11071da177e4SLinus Torvalds __xfrm_state_insert(x); 11081da177e4SLinus Torvalds err = 0; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds out: 11111da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds if (x1) { 11141da177e4SLinus Torvalds xfrm_state_delete(x1); 11151da177e4SLinus Torvalds xfrm_state_put(x1); 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 111837b08e34SDavid S. Miller if (to_put) 111937b08e34SDavid S. Miller xfrm_state_put(to_put); 112037b08e34SDavid S. Miller 11211da177e4SLinus Torvalds return err; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_add); 11241da177e4SLinus Torvalds 112580c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 11266666351dSEric Dumazet static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp) 112780c9abaaSShinta Sugimoto { 112898806f75SAlexey Dobriyan struct net *net = xs_net(orig); 112980c9abaaSShinta Sugimoto int err = -ENOMEM; 113098806f75SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 113180c9abaaSShinta Sugimoto if (!x) 1132553f9118SHerbert Xu goto out; 113380c9abaaSShinta Sugimoto 113480c9abaaSShinta Sugimoto memcpy(&x->id, &orig->id, sizeof(x->id)); 113580c9abaaSShinta Sugimoto memcpy(&x->sel, &orig->sel, sizeof(x->sel)); 113680c9abaaSShinta Sugimoto memcpy(&x->lft, &orig->lft, sizeof(x->lft)); 113780c9abaaSShinta Sugimoto x->props.mode = orig->props.mode; 113880c9abaaSShinta Sugimoto x->props.replay_window = orig->props.replay_window; 113980c9abaaSShinta Sugimoto x->props.reqid = orig->props.reqid; 114080c9abaaSShinta Sugimoto x->props.family = orig->props.family; 114180c9abaaSShinta Sugimoto x->props.saddr = orig->props.saddr; 114280c9abaaSShinta Sugimoto 114380c9abaaSShinta Sugimoto if (orig->aalg) { 11444447bb33SMartin Willi x->aalg = xfrm_algo_auth_clone(orig->aalg); 114580c9abaaSShinta Sugimoto if (!x->aalg) 114680c9abaaSShinta Sugimoto goto error; 114780c9abaaSShinta Sugimoto } 114880c9abaaSShinta Sugimoto x->props.aalgo = orig->props.aalgo; 114980c9abaaSShinta Sugimoto 115080c9abaaSShinta Sugimoto if (orig->ealg) { 115180c9abaaSShinta Sugimoto x->ealg = xfrm_algo_clone(orig->ealg); 115280c9abaaSShinta Sugimoto if (!x->ealg) 115380c9abaaSShinta Sugimoto goto error; 115480c9abaaSShinta Sugimoto } 115580c9abaaSShinta Sugimoto x->props.ealgo = orig->props.ealgo; 115680c9abaaSShinta Sugimoto 115780c9abaaSShinta Sugimoto if (orig->calg) { 115880c9abaaSShinta Sugimoto x->calg = xfrm_algo_clone(orig->calg); 115980c9abaaSShinta Sugimoto if (!x->calg) 116080c9abaaSShinta Sugimoto goto error; 116180c9abaaSShinta Sugimoto } 116280c9abaaSShinta Sugimoto x->props.calgo = orig->props.calgo; 116380c9abaaSShinta Sugimoto 116480c9abaaSShinta Sugimoto if (orig->encap) { 116580c9abaaSShinta Sugimoto x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL); 116680c9abaaSShinta Sugimoto if (!x->encap) 116780c9abaaSShinta Sugimoto goto error; 116880c9abaaSShinta Sugimoto } 116980c9abaaSShinta Sugimoto 117080c9abaaSShinta Sugimoto if (orig->coaddr) { 117180c9abaaSShinta Sugimoto x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr), 117280c9abaaSShinta Sugimoto GFP_KERNEL); 117380c9abaaSShinta Sugimoto if (!x->coaddr) 117480c9abaaSShinta Sugimoto goto error; 117580c9abaaSShinta Sugimoto } 117680c9abaaSShinta Sugimoto 1177af2f464eSSteffen Klassert if (orig->replay_esn) { 1178af2f464eSSteffen Klassert err = xfrm_replay_clone(x, orig); 1179af2f464eSSteffen Klassert if (err) 1180af2f464eSSteffen Klassert goto error; 1181af2f464eSSteffen Klassert } 1182af2f464eSSteffen Klassert 1183bd55775cSJamal Hadi Salim memcpy(&x->mark, &orig->mark, sizeof(x->mark)); 1184bd55775cSJamal Hadi Salim 118580c9abaaSShinta Sugimoto err = xfrm_init_state(x); 118680c9abaaSShinta Sugimoto if (err) 118780c9abaaSShinta Sugimoto goto error; 118880c9abaaSShinta Sugimoto 118980c9abaaSShinta Sugimoto x->props.flags = orig->props.flags; 1190a947b0a9SNicolas Dichtel x->props.extra_flags = orig->props.extra_flags; 119180c9abaaSShinta Sugimoto 119280c9abaaSShinta Sugimoto x->curlft.add_time = orig->curlft.add_time; 119380c9abaaSShinta Sugimoto x->km.state = orig->km.state; 119480c9abaaSShinta Sugimoto x->km.seq = orig->km.seq; 119580c9abaaSShinta Sugimoto 119680c9abaaSShinta Sugimoto return x; 119780c9abaaSShinta Sugimoto 119880c9abaaSShinta Sugimoto error: 1199553f9118SHerbert Xu xfrm_state_put(x); 1200553f9118SHerbert Xu out: 120180c9abaaSShinta Sugimoto if (errp) 120280c9abaaSShinta Sugimoto *errp = err; 120380c9abaaSShinta Sugimoto return NULL; 120480c9abaaSShinta Sugimoto } 120580c9abaaSShinta Sugimoto 120680c9abaaSShinta Sugimoto /* xfrm_state_lock is held */ 120780c9abaaSShinta Sugimoto struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m) 120880c9abaaSShinta Sugimoto { 120980c9abaaSShinta Sugimoto unsigned int h; 121080c9abaaSShinta Sugimoto struct xfrm_state *x; 121180c9abaaSShinta Sugimoto 121280c9abaaSShinta Sugimoto if (m->reqid) { 121364d0cd00SAlexey Dobriyan h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr, 121480c9abaaSShinta Sugimoto m->reqid, m->old_family); 1215b67bfe0dSSasha Levin hlist_for_each_entry(x, init_net.xfrm.state_bydst+h, bydst) { 121680c9abaaSShinta Sugimoto if (x->props.mode != m->mode || 121780c9abaaSShinta Sugimoto x->id.proto != m->proto) 121880c9abaaSShinta Sugimoto continue; 121980c9abaaSShinta Sugimoto if (m->reqid && x->props.reqid != m->reqid) 122080c9abaaSShinta Sugimoto continue; 122170e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, 122280c9abaaSShinta Sugimoto m->old_family) || 122370e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, 122480c9abaaSShinta Sugimoto m->old_family)) 122580c9abaaSShinta Sugimoto continue; 122680c9abaaSShinta Sugimoto xfrm_state_hold(x); 122780c9abaaSShinta Sugimoto return x; 122880c9abaaSShinta Sugimoto } 122980c9abaaSShinta Sugimoto } else { 123064d0cd00SAlexey Dobriyan h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr, 123180c9abaaSShinta Sugimoto m->old_family); 1232b67bfe0dSSasha Levin hlist_for_each_entry(x, init_net.xfrm.state_bysrc+h, bysrc) { 123380c9abaaSShinta Sugimoto if (x->props.mode != m->mode || 123480c9abaaSShinta Sugimoto x->id.proto != m->proto) 123580c9abaaSShinta Sugimoto continue; 123670e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, 123780c9abaaSShinta Sugimoto m->old_family) || 123870e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, 123980c9abaaSShinta Sugimoto m->old_family)) 124080c9abaaSShinta Sugimoto continue; 124180c9abaaSShinta Sugimoto xfrm_state_hold(x); 124280c9abaaSShinta Sugimoto return x; 124380c9abaaSShinta Sugimoto } 124480c9abaaSShinta Sugimoto } 124580c9abaaSShinta Sugimoto 124680c9abaaSShinta Sugimoto return NULL; 124780c9abaaSShinta Sugimoto } 124880c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_migrate_state_find); 124980c9abaaSShinta Sugimoto 125080c9abaaSShinta Sugimoto struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x, 125180c9abaaSShinta Sugimoto struct xfrm_migrate *m) 125280c9abaaSShinta Sugimoto { 125380c9abaaSShinta Sugimoto struct xfrm_state *xc; 125480c9abaaSShinta Sugimoto int err; 125580c9abaaSShinta Sugimoto 125680c9abaaSShinta Sugimoto xc = xfrm_state_clone(x, &err); 125780c9abaaSShinta Sugimoto if (!xc) 125880c9abaaSShinta Sugimoto return NULL; 125980c9abaaSShinta Sugimoto 126080c9abaaSShinta Sugimoto memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr)); 126180c9abaaSShinta Sugimoto memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr)); 126280c9abaaSShinta Sugimoto 126380c9abaaSShinta Sugimoto /* add state */ 126470e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) { 126580c9abaaSShinta Sugimoto /* a care is needed when the destination address of the 126680c9abaaSShinta Sugimoto state is to be updated as it is a part of triplet */ 126780c9abaaSShinta Sugimoto xfrm_state_insert(xc); 126880c9abaaSShinta Sugimoto } else { 126980c9abaaSShinta Sugimoto if ((err = xfrm_state_add(xc)) < 0) 127080c9abaaSShinta Sugimoto goto error; 127180c9abaaSShinta Sugimoto } 127280c9abaaSShinta Sugimoto 127380c9abaaSShinta Sugimoto return xc; 127480c9abaaSShinta Sugimoto error: 127578347c8cSThomas Egerer xfrm_state_put(xc); 127680c9abaaSShinta Sugimoto return NULL; 127780c9abaaSShinta Sugimoto } 127880c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_state_migrate); 127980c9abaaSShinta Sugimoto #endif 128080c9abaaSShinta Sugimoto 12811da177e4SLinus Torvalds int xfrm_state_update(struct xfrm_state *x) 12821da177e4SLinus Torvalds { 128337b08e34SDavid S. Miller struct xfrm_state *x1, *to_put; 12841da177e4SLinus Torvalds int err; 1285eb2971b6SMasahide NAKAMURA int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); 12861da177e4SLinus Torvalds 128737b08e34SDavid S. Miller to_put = NULL; 128837b08e34SDavid S. Miller 12891da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1290edcd5821SDavid S. Miller x1 = __xfrm_state_locate(x, use_spi, x->props.family); 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds err = -ESRCH; 12931da177e4SLinus Torvalds if (!x1) 12941da177e4SLinus Torvalds goto out; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds if (xfrm_state_kern(x1)) { 129737b08e34SDavid S. Miller to_put = x1; 12981da177e4SLinus Torvalds err = -EEXIST; 12991da177e4SLinus Torvalds goto out; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds if (x1->km.state == XFRM_STATE_ACQ) { 13031da177e4SLinus Torvalds __xfrm_state_insert(x); 13041da177e4SLinus Torvalds x = NULL; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds err = 0; 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds out: 13091da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 13101da177e4SLinus Torvalds 131137b08e34SDavid S. Miller if (to_put) 131237b08e34SDavid S. Miller xfrm_state_put(to_put); 131337b08e34SDavid S. Miller 13141da177e4SLinus Torvalds if (err) 13151da177e4SLinus Torvalds return err; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds if (!x) { 13181da177e4SLinus Torvalds xfrm_state_delete(x1); 13191da177e4SLinus Torvalds xfrm_state_put(x1); 13201da177e4SLinus Torvalds return 0; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds err = -EINVAL; 13241da177e4SLinus Torvalds spin_lock_bh(&x1->lock); 13251da177e4SLinus Torvalds if (likely(x1->km.state == XFRM_STATE_VALID)) { 13261da177e4SLinus Torvalds if (x->encap && x1->encap) 13271da177e4SLinus Torvalds memcpy(x1->encap, x->encap, sizeof(*x1->encap)); 1328060f02a3SNoriaki TAKAMIYA if (x->coaddr && x1->coaddr) { 1329060f02a3SNoriaki TAKAMIYA memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr)); 1330060f02a3SNoriaki TAKAMIYA } 1331060f02a3SNoriaki TAKAMIYA if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel))) 1332060f02a3SNoriaki TAKAMIYA memcpy(&x1->sel, &x->sel, sizeof(x1->sel)); 13331da177e4SLinus Torvalds memcpy(&x1->lft, &x->lft, sizeof(x1->lft)); 13341da177e4SLinus Torvalds x1->km.dying = 0; 13351da177e4SLinus Torvalds 13369e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); 13371da177e4SLinus Torvalds if (x1->curlft.use_time) 13381da177e4SLinus Torvalds xfrm_state_check_expire(x1); 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds err = 0; 13418fcbc637STushar Gohad x->km.state = XFRM_STATE_DEAD; 13428fcbc637STushar Gohad __xfrm_state_put(x); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds spin_unlock_bh(&x1->lock); 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds xfrm_state_put(x1); 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds return err; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_update); 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds int xfrm_state_check_expire(struct xfrm_state *x) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds if (!x->curlft.use_time) 13559d729f72SJames Morris x->curlft.use_time = get_seconds(); 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds if (x->curlft.bytes >= x->lft.hard_byte_limit || 13581da177e4SLinus Torvalds x->curlft.packets >= x->lft.hard_packet_limit) { 13594666faabSHerbert Xu x->km.state = XFRM_STATE_EXPIRED; 13609e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL); 13611da177e4SLinus Torvalds return -EINVAL; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds if (!x->km.dying && 13651da177e4SLinus Torvalds (x->curlft.bytes >= x->lft.soft_byte_limit || 13664666faabSHerbert Xu x->curlft.packets >= x->lft.soft_packet_limit)) { 13674666faabSHerbert Xu x->km.dying = 1; 136853bc6b4dSJamal Hadi Salim km_state_expired(x, 0, 0); 13694666faabSHerbert Xu } 13701da177e4SLinus Torvalds return 0; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_check_expire); 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds struct xfrm_state * 1375a70486f0SDavid S. Miller xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi, 1376bd55775cSJamal Hadi Salim u8 proto, unsigned short family) 13771da177e4SLinus Torvalds { 13781da177e4SLinus Torvalds struct xfrm_state *x; 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1381bd55775cSJamal Hadi Salim x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family); 13821da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 13831da177e4SLinus Torvalds return x; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_lookup); 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds struct xfrm_state * 1388bd55775cSJamal Hadi Salim xfrm_state_lookup_byaddr(struct net *net, u32 mark, 1389a70486f0SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 1390eb2971b6SMasahide NAKAMURA u8 proto, unsigned short family) 1391eb2971b6SMasahide NAKAMURA { 1392eb2971b6SMasahide NAKAMURA struct xfrm_state *x; 1393eb2971b6SMasahide NAKAMURA 1394eb2971b6SMasahide NAKAMURA spin_lock_bh(&xfrm_state_lock); 1395bd55775cSJamal Hadi Salim x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family); 1396eb2971b6SMasahide NAKAMURA spin_unlock_bh(&xfrm_state_lock); 1397eb2971b6SMasahide NAKAMURA return x; 1398eb2971b6SMasahide NAKAMURA } 1399eb2971b6SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_lookup_byaddr); 1400eb2971b6SMasahide NAKAMURA 1401eb2971b6SMasahide NAKAMURA struct xfrm_state * 1402bd55775cSJamal Hadi Salim xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto, 1403a70486f0SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 14041da177e4SLinus Torvalds int create, unsigned short family) 14051da177e4SLinus Torvalds { 14061da177e4SLinus Torvalds struct xfrm_state *x; 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1409bd55775cSJamal Hadi Salim x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create); 14101da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 14112770834cSDavid S. Miller 14121da177e4SLinus Torvalds return x; 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq); 14151da177e4SLinus Torvalds 141641a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 141741a49cc3SMasahide NAKAMURA int 141841a49cc3SMasahide NAKAMURA xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n, 141941a49cc3SMasahide NAKAMURA unsigned short family) 142041a49cc3SMasahide NAKAMURA { 142141a49cc3SMasahide NAKAMURA int err = 0; 142241a49cc3SMasahide NAKAMURA struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 142341a49cc3SMasahide NAKAMURA if (!afinfo) 142441a49cc3SMasahide NAKAMURA return -EAFNOSUPPORT; 142541a49cc3SMasahide NAKAMURA 142641a49cc3SMasahide NAKAMURA spin_lock_bh(&xfrm_state_lock); 142741a49cc3SMasahide NAKAMURA if (afinfo->tmpl_sort) 142841a49cc3SMasahide NAKAMURA err = afinfo->tmpl_sort(dst, src, n); 142941a49cc3SMasahide NAKAMURA spin_unlock_bh(&xfrm_state_lock); 143041a49cc3SMasahide NAKAMURA xfrm_state_put_afinfo(afinfo); 143141a49cc3SMasahide NAKAMURA return err; 143241a49cc3SMasahide NAKAMURA } 143341a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_tmpl_sort); 143441a49cc3SMasahide NAKAMURA 143541a49cc3SMasahide NAKAMURA int 143641a49cc3SMasahide NAKAMURA xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n, 143741a49cc3SMasahide NAKAMURA unsigned short family) 143841a49cc3SMasahide NAKAMURA { 143941a49cc3SMasahide NAKAMURA int err = 0; 144041a49cc3SMasahide NAKAMURA struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 144141a49cc3SMasahide NAKAMURA if (!afinfo) 144241a49cc3SMasahide NAKAMURA return -EAFNOSUPPORT; 144341a49cc3SMasahide NAKAMURA 144441a49cc3SMasahide NAKAMURA spin_lock_bh(&xfrm_state_lock); 144541a49cc3SMasahide NAKAMURA if (afinfo->state_sort) 144641a49cc3SMasahide NAKAMURA err = afinfo->state_sort(dst, src, n); 144741a49cc3SMasahide NAKAMURA spin_unlock_bh(&xfrm_state_lock); 144841a49cc3SMasahide NAKAMURA xfrm_state_put_afinfo(afinfo); 144941a49cc3SMasahide NAKAMURA return err; 145041a49cc3SMasahide NAKAMURA } 145141a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_sort); 145241a49cc3SMasahide NAKAMURA #endif 145341a49cc3SMasahide NAKAMURA 14541da177e4SLinus Torvalds /* Silly enough, but I'm lazy to build resolution list */ 14551da177e4SLinus Torvalds 1456bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) 14571da177e4SLinus Torvalds { 14581da177e4SLinus Torvalds int i; 14591da177e4SLinus Torvalds 14605447c5e4SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 14618f126e37SDavid S. Miller struct xfrm_state *x; 14628f126e37SDavid S. Miller 1463b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { 14648f126e37SDavid S. Miller if (x->km.seq == seq && 14653d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 14668f126e37SDavid S. Miller x->km.state == XFRM_STATE_ACQ) { 14671da177e4SLinus Torvalds xfrm_state_hold(x); 14681da177e4SLinus Torvalds return x; 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds return NULL; 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 1475bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) 14761da177e4SLinus Torvalds { 14771da177e4SLinus Torvalds struct xfrm_state *x; 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1480bd55775cSJamal Hadi Salim x = __xfrm_find_acq_byseq(net, mark, seq); 14811da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 14821da177e4SLinus Torvalds return x; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq_byseq); 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds u32 xfrm_get_acqseq(void) 14871da177e4SLinus Torvalds { 14881da177e4SLinus Torvalds u32 res; 14896836b9bdSjamal static atomic_t acqseq; 14901da177e4SLinus Torvalds 14916836b9bdSjamal do { 14926836b9bdSjamal res = atomic_inc_return(&acqseq); 14936836b9bdSjamal } while (!res); 14946836b9bdSjamal 14951da177e4SLinus Torvalds return res; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_get_acqseq); 14981da177e4SLinus Torvalds 1499658b219eSHerbert Xu int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) 15001da177e4SLinus Torvalds { 1501221df1edSAlexey Dobriyan struct net *net = xs_net(x); 1502f034b5d4SDavid S. Miller unsigned int h; 15031da177e4SLinus Torvalds struct xfrm_state *x0; 1504658b219eSHerbert Xu int err = -ENOENT; 1505658b219eSHerbert Xu __be32 minspi = htonl(low); 1506658b219eSHerbert Xu __be32 maxspi = htonl(high); 1507bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 15081da177e4SLinus Torvalds 1509658b219eSHerbert Xu spin_lock_bh(&x->lock); 1510658b219eSHerbert Xu if (x->km.state == XFRM_STATE_DEAD) 1511658b219eSHerbert Xu goto unlock; 1512658b219eSHerbert Xu 1513658b219eSHerbert Xu err = 0; 15141da177e4SLinus Torvalds if (x->id.spi) 1515658b219eSHerbert Xu goto unlock; 1516658b219eSHerbert Xu 1517658b219eSHerbert Xu err = -ENOENT; 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds if (minspi == maxspi) { 1520bd55775cSJamal Hadi Salim x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family); 15211da177e4SLinus Torvalds if (x0) { 15221da177e4SLinus Torvalds xfrm_state_put(x0); 1523658b219eSHerbert Xu goto unlock; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds x->id.spi = minspi; 15261da177e4SLinus Torvalds } else { 15271da177e4SLinus Torvalds u32 spi = 0; 152826977b4eSAl Viro for (h=0; h<high-low+1; h++) { 152926977b4eSAl Viro spi = low + net_random()%(high-low+1); 1530bd55775cSJamal Hadi Salim x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family); 15311da177e4SLinus Torvalds if (x0 == NULL) { 15321da177e4SLinus Torvalds x->id.spi = htonl(spi); 15331da177e4SLinus Torvalds break; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds xfrm_state_put(x0); 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds if (x->id.spi) { 15391da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 154012604d8aSAlexey Dobriyan h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); 154112604d8aSAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 15421da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 1543658b219eSHerbert Xu 1544658b219eSHerbert Xu err = 0; 15451da177e4SLinus Torvalds } 1546658b219eSHerbert Xu 1547658b219eSHerbert Xu unlock: 1548658b219eSHerbert Xu spin_unlock_bh(&x->lock); 1549658b219eSHerbert Xu 1550658b219eSHerbert Xu return err; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_alloc_spi); 15531da177e4SLinus Torvalds 1554284fa7daSAlexey Dobriyan int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, 15554c563f76STimo Teras int (*func)(struct xfrm_state *, int, void*), 15561da177e4SLinus Torvalds void *data) 15571da177e4SLinus Torvalds { 155812a169e7SHerbert Xu struct xfrm_state *state; 155912a169e7SHerbert Xu struct xfrm_state_walk *x; 15601da177e4SLinus Torvalds int err = 0; 15611da177e4SLinus Torvalds 156212a169e7SHerbert Xu if (walk->seq != 0 && list_empty(&walk->all)) 15634c563f76STimo Teras return 0; 15644c563f76STimo Teras 15651da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 156612a169e7SHerbert Xu if (list_empty(&walk->all)) 1567284fa7daSAlexey Dobriyan x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all); 156812a169e7SHerbert Xu else 156912a169e7SHerbert Xu x = list_entry(&walk->all, struct xfrm_state_walk, all); 1570284fa7daSAlexey Dobriyan list_for_each_entry_from(x, &net->xfrm.state_all, all) { 157112a169e7SHerbert Xu if (x->state == XFRM_STATE_DEAD) 15724c563f76STimo Teras continue; 157312a169e7SHerbert Xu state = container_of(x, struct xfrm_state, km); 157412a169e7SHerbert Xu if (!xfrm_id_proto_match(state->id.proto, walk->proto)) 157594b9bb54SJamal Hadi Salim continue; 157612a169e7SHerbert Xu err = func(state, walk->seq, data); 15774c563f76STimo Teras if (err) { 157812a169e7SHerbert Xu list_move_tail(&walk->all, &x->all); 157994b9bb54SJamal Hadi Salim goto out; 158094b9bb54SJamal Hadi Salim } 158112a169e7SHerbert Xu walk->seq++; 15824c563f76STimo Teras } 158312a169e7SHerbert Xu if (walk->seq == 0) { 15841da177e4SLinus Torvalds err = -ENOENT; 15851da177e4SLinus Torvalds goto out; 15861da177e4SLinus Torvalds } 158712a169e7SHerbert Xu list_del_init(&walk->all); 15881da177e4SLinus Torvalds out: 15891da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 15901da177e4SLinus Torvalds return err; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_walk); 15931da177e4SLinus Torvalds 15945c182458SHerbert Xu void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto) 15955c182458SHerbert Xu { 159612a169e7SHerbert Xu INIT_LIST_HEAD(&walk->all); 15975c182458SHerbert Xu walk->proto = proto; 159812a169e7SHerbert Xu walk->state = XFRM_STATE_DEAD; 159912a169e7SHerbert Xu walk->seq = 0; 16005c182458SHerbert Xu } 16015c182458SHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_init); 16025c182458SHerbert Xu 1603abb81c4fSHerbert Xu void xfrm_state_walk_done(struct xfrm_state_walk *walk) 1604abb81c4fSHerbert Xu { 160512a169e7SHerbert Xu if (list_empty(&walk->all)) 16065c182458SHerbert Xu return; 16075c182458SHerbert Xu 160812a169e7SHerbert Xu spin_lock_bh(&xfrm_state_lock); 160912a169e7SHerbert Xu list_del(&walk->all); 16107d0b591cSChuck Ebbert spin_unlock_bh(&xfrm_state_lock); 1611abb81c4fSHerbert Xu } 1612abb81c4fSHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_done); 1613abb81c4fSHerbert Xu 1614f8cd5488SJamal Hadi Salim static void xfrm_replay_timer_handler(unsigned long data) 1615f8cd5488SJamal Hadi Salim { 1616f8cd5488SJamal Hadi Salim struct xfrm_state *x = (struct xfrm_state*)data; 1617f8cd5488SJamal Hadi Salim 1618f8cd5488SJamal Hadi Salim spin_lock(&x->lock); 1619f8cd5488SJamal Hadi Salim 16202717096aSJamal Hadi Salim if (x->km.state == XFRM_STATE_VALID) { 1621a6483b79SAlexey Dobriyan if (xfrm_aevent_is_on(xs_net(x))) 16229fdc4883SSteffen Klassert x->repl->notify(x, XFRM_REPLAY_TIMEOUT); 16232717096aSJamal Hadi Salim else 16242717096aSJamal Hadi Salim x->xflags |= XFRM_TIME_DEFER; 16252717096aSJamal Hadi Salim } 1626f8cd5488SJamal Hadi Salim 1627f8cd5488SJamal Hadi Salim spin_unlock(&x->lock); 1628f8cd5488SJamal Hadi Salim } 1629f8cd5488SJamal Hadi Salim 1630df01812eSDenis Cheng static LIST_HEAD(xfrm_km_list); 16311da177e4SLinus Torvalds 1632214e005bSDavid S. Miller void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 16331da177e4SLinus Torvalds { 16341da177e4SLinus Torvalds struct xfrm_mgr *km; 16351da177e4SLinus Torvalds 163685168c00SCong Wang rcu_read_lock(); 163785168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) 163826b15dadSJamal Hadi Salim if (km->notify_policy) 163926b15dadSJamal Hadi Salim km->notify_policy(xp, dir, c); 164085168c00SCong Wang rcu_read_unlock(); 164126b15dadSJamal Hadi Salim } 164226b15dadSJamal Hadi Salim 1643214e005bSDavid S. Miller void km_state_notify(struct xfrm_state *x, const struct km_event *c) 164426b15dadSJamal Hadi Salim { 164526b15dadSJamal Hadi Salim struct xfrm_mgr *km; 164685168c00SCong Wang rcu_read_lock(); 164785168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) 164826b15dadSJamal Hadi Salim if (km->notify) 164926b15dadSJamal Hadi Salim km->notify(x, c); 165085168c00SCong Wang rcu_read_unlock(); 165126b15dadSJamal Hadi Salim } 165226b15dadSJamal Hadi Salim 165326b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_policy_notify); 165426b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_state_notify); 165526b15dadSJamal Hadi Salim 165615e47304SEric W. Biederman void km_state_expired(struct xfrm_state *x, int hard, u32 portid) 165726b15dadSJamal Hadi Salim { 165898806f75SAlexey Dobriyan struct net *net = xs_net(x); 165926b15dadSJamal Hadi Salim struct km_event c; 166026b15dadSJamal Hadi Salim 1661bf08867fSHerbert Xu c.data.hard = hard; 166215e47304SEric W. Biederman c.portid = portid; 1663f60f6b8fSHerbert Xu c.event = XFRM_MSG_EXPIRE; 166426b15dadSJamal Hadi Salim km_state_notify(x, &c); 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds if (hard) 166798806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 167053bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(km_state_expired); 167126b15dadSJamal Hadi Salim /* 167226b15dadSJamal Hadi Salim * We send to all registered managers regardless of failure 167326b15dadSJamal Hadi Salim * We are happy with one success 167426b15dadSJamal Hadi Salim */ 1675980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol) 16761da177e4SLinus Torvalds { 167726b15dadSJamal Hadi Salim int err = -EINVAL, acqret; 16781da177e4SLinus Torvalds struct xfrm_mgr *km; 16791da177e4SLinus Torvalds 168085168c00SCong Wang rcu_read_lock(); 168185168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 168265e0736bSFan Du acqret = km->acquire(x, t, pol); 168326b15dadSJamal Hadi Salim if (!acqret) 168426b15dadSJamal Hadi Salim err = acqret; 16851da177e4SLinus Torvalds } 168685168c00SCong Wang rcu_read_unlock(); 16871da177e4SLinus Torvalds return err; 16881da177e4SLinus Torvalds } 1689980ebd25SJamal Hadi Salim EXPORT_SYMBOL(km_query); 16901da177e4SLinus Torvalds 16915d36b180SAl Viro int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) 16921da177e4SLinus Torvalds { 16931da177e4SLinus Torvalds int err = -EINVAL; 16941da177e4SLinus Torvalds struct xfrm_mgr *km; 16951da177e4SLinus Torvalds 169685168c00SCong Wang rcu_read_lock(); 169785168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 16981da177e4SLinus Torvalds if (km->new_mapping) 16991da177e4SLinus Torvalds err = km->new_mapping(x, ipaddr, sport); 17001da177e4SLinus Torvalds if (!err) 17011da177e4SLinus Torvalds break; 17021da177e4SLinus Torvalds } 170385168c00SCong Wang rcu_read_unlock(); 17041da177e4SLinus Torvalds return err; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds EXPORT_SYMBOL(km_new_mapping); 17071da177e4SLinus Torvalds 170815e47304SEric W. Biederman void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid) 17091da177e4SLinus Torvalds { 171098806f75SAlexey Dobriyan struct net *net = xp_net(pol); 171126b15dadSJamal Hadi Salim struct km_event c; 17121da177e4SLinus Torvalds 1713bf08867fSHerbert Xu c.data.hard = hard; 171415e47304SEric W. Biederman c.portid = portid; 1715f60f6b8fSHerbert Xu c.event = XFRM_MSG_POLEXPIRE; 171626b15dadSJamal Hadi Salim km_policy_notify(pol, dir, &c); 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds if (hard) 171998806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 17201da177e4SLinus Torvalds } 1721a70fcb0bSDavid S. Miller EXPORT_SYMBOL(km_policy_expired); 17221da177e4SLinus Torvalds 17232d60abc2SEric Dumazet #ifdef CONFIG_XFRM_MIGRATE 1724183cad12SDavid S. Miller int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 1725183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 1726183cad12SDavid S. Miller const struct xfrm_kmaddress *k) 172780c9abaaSShinta Sugimoto { 172880c9abaaSShinta Sugimoto int err = -EINVAL; 172980c9abaaSShinta Sugimoto int ret; 173080c9abaaSShinta Sugimoto struct xfrm_mgr *km; 173180c9abaaSShinta Sugimoto 173285168c00SCong Wang rcu_read_lock(); 173385168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 173480c9abaaSShinta Sugimoto if (km->migrate) { 173513c1d189SArnaud Ebalard ret = km->migrate(sel, dir, type, m, num_migrate, k); 173680c9abaaSShinta Sugimoto if (!ret) 173780c9abaaSShinta Sugimoto err = ret; 173880c9abaaSShinta Sugimoto } 173980c9abaaSShinta Sugimoto } 174085168c00SCong Wang rcu_read_unlock(); 174180c9abaaSShinta Sugimoto return err; 174280c9abaaSShinta Sugimoto } 174380c9abaaSShinta Sugimoto EXPORT_SYMBOL(km_migrate); 17442d60abc2SEric Dumazet #endif 174580c9abaaSShinta Sugimoto 1746db983c11SAlexey Dobriyan int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr) 174797a64b45SMasahide NAKAMURA { 174897a64b45SMasahide NAKAMURA int err = -EINVAL; 174997a64b45SMasahide NAKAMURA int ret; 175097a64b45SMasahide NAKAMURA struct xfrm_mgr *km; 175197a64b45SMasahide NAKAMURA 175285168c00SCong Wang rcu_read_lock(); 175385168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 175497a64b45SMasahide NAKAMURA if (km->report) { 1755db983c11SAlexey Dobriyan ret = km->report(net, proto, sel, addr); 175697a64b45SMasahide NAKAMURA if (!ret) 175797a64b45SMasahide NAKAMURA err = ret; 175897a64b45SMasahide NAKAMURA } 175997a64b45SMasahide NAKAMURA } 176085168c00SCong Wang rcu_read_unlock(); 176197a64b45SMasahide NAKAMURA return err; 176297a64b45SMasahide NAKAMURA } 176397a64b45SMasahide NAKAMURA EXPORT_SYMBOL(km_report); 176497a64b45SMasahide NAKAMURA 17651da177e4SLinus Torvalds int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds int err; 17681da177e4SLinus Torvalds u8 *data; 17691da177e4SLinus Torvalds struct xfrm_mgr *km; 17701da177e4SLinus Torvalds struct xfrm_policy *pol = NULL; 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds if (optlen <= 0 || optlen > PAGE_SIZE) 17731da177e4SLinus Torvalds return -EMSGSIZE; 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds data = kmalloc(optlen, GFP_KERNEL); 17761da177e4SLinus Torvalds if (!data) 17771da177e4SLinus Torvalds return -ENOMEM; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds err = -EFAULT; 17801da177e4SLinus Torvalds if (copy_from_user(data, optval, optlen)) 17811da177e4SLinus Torvalds goto out; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds err = -EINVAL; 178485168c00SCong Wang rcu_read_lock(); 178585168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 1786cb969f07SVenkat Yekkirala pol = km->compile_policy(sk, optname, data, 17871da177e4SLinus Torvalds optlen, &err); 17881da177e4SLinus Torvalds if (err >= 0) 17891da177e4SLinus Torvalds break; 17901da177e4SLinus Torvalds } 179185168c00SCong Wang rcu_read_unlock(); 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds if (err >= 0) { 17941da177e4SLinus Torvalds xfrm_sk_policy_insert(sk, err, pol); 17951da177e4SLinus Torvalds xfrm_pol_put(pol); 17961da177e4SLinus Torvalds err = 0; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds out: 18001da177e4SLinus Torvalds kfree(data); 18011da177e4SLinus Torvalds return err; 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_user_policy); 18041da177e4SLinus Torvalds 180585168c00SCong Wang static DEFINE_SPINLOCK(xfrm_km_lock); 180685168c00SCong Wang 18071da177e4SLinus Torvalds int xfrm_register_km(struct xfrm_mgr *km) 18081da177e4SLinus Torvalds { 180985168c00SCong Wang spin_lock_bh(&xfrm_km_lock); 181085168c00SCong Wang list_add_tail_rcu(&km->list, &xfrm_km_list); 181185168c00SCong Wang spin_unlock_bh(&xfrm_km_lock); 18121da177e4SLinus Torvalds return 0; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_register_km); 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds int xfrm_unregister_km(struct xfrm_mgr *km) 18171da177e4SLinus Torvalds { 181885168c00SCong Wang spin_lock_bh(&xfrm_km_lock); 181985168c00SCong Wang list_del_rcu(&km->list); 182085168c00SCong Wang spin_unlock_bh(&xfrm_km_lock); 182185168c00SCong Wang synchronize_rcu(); 18221da177e4SLinus Torvalds return 0; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_unregister_km); 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo) 18271da177e4SLinus Torvalds { 18281da177e4SLinus Torvalds int err = 0; 18291da177e4SLinus Torvalds if (unlikely(afinfo == NULL)) 18301da177e4SLinus Torvalds return -EINVAL; 18311da177e4SLinus Torvalds if (unlikely(afinfo->family >= NPROTO)) 18321da177e4SLinus Torvalds return -EAFNOSUPPORT; 183344abdc30SCong Wang spin_lock_bh(&xfrm_state_afinfo_lock); 18341da177e4SLinus Torvalds if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL)) 18351da177e4SLinus Torvalds err = -ENOBUFS; 1836edcd5821SDavid S. Miller else 183744abdc30SCong Wang rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo); 183844abdc30SCong Wang spin_unlock_bh(&xfrm_state_afinfo_lock); 18391da177e4SLinus Torvalds return err; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_register_afinfo); 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo) 18441da177e4SLinus Torvalds { 18451da177e4SLinus Torvalds int err = 0; 18461da177e4SLinus Torvalds if (unlikely(afinfo == NULL)) 18471da177e4SLinus Torvalds return -EINVAL; 18481da177e4SLinus Torvalds if (unlikely(afinfo->family >= NPROTO)) 18491da177e4SLinus Torvalds return -EAFNOSUPPORT; 185044abdc30SCong Wang spin_lock_bh(&xfrm_state_afinfo_lock); 18511da177e4SLinus Torvalds if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) { 18521da177e4SLinus Torvalds if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo)) 18531da177e4SLinus Torvalds err = -EINVAL; 1854edcd5821SDavid S. Miller else 185544abdc30SCong Wang RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL); 18561da177e4SLinus Torvalds } 185744abdc30SCong Wang spin_unlock_bh(&xfrm_state_afinfo_lock); 185844abdc30SCong Wang synchronize_rcu(); 18591da177e4SLinus Torvalds return err; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_unregister_afinfo); 18621da177e4SLinus Torvalds 186317c2a42aSHerbert Xu static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family) 18641da177e4SLinus Torvalds { 18651da177e4SLinus Torvalds struct xfrm_state_afinfo *afinfo; 18661da177e4SLinus Torvalds if (unlikely(family >= NPROTO)) 18671da177e4SLinus Torvalds return NULL; 186844abdc30SCong Wang rcu_read_lock(); 186944abdc30SCong Wang afinfo = rcu_dereference(xfrm_state_afinfo[family]); 1870546be240SHerbert Xu if (unlikely(!afinfo)) 187144abdc30SCong Wang rcu_read_unlock(); 18721da177e4SLinus Torvalds return afinfo; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 187517c2a42aSHerbert Xu static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo) 18761da177e4SLinus Torvalds { 187744abdc30SCong Wang rcu_read_unlock(); 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */ 18811da177e4SLinus Torvalds void xfrm_state_delete_tunnel(struct xfrm_state *x) 18821da177e4SLinus Torvalds { 18831da177e4SLinus Torvalds if (x->tunnel) { 18841da177e4SLinus Torvalds struct xfrm_state *t = x->tunnel; 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds if (atomic_read(&t->tunnel_users) == 2) 18871da177e4SLinus Torvalds xfrm_state_delete(t); 18881da177e4SLinus Torvalds atomic_dec(&t->tunnel_users); 18891da177e4SLinus Torvalds xfrm_state_put(t); 18901da177e4SLinus Torvalds x->tunnel = NULL; 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete_tunnel); 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds int xfrm_state_mtu(struct xfrm_state *x, int mtu) 18961da177e4SLinus Torvalds { 1897c5c25238SPatrick McHardy int res; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds spin_lock_bh(&x->lock); 19001da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_VALID && 1901c5c25238SPatrick McHardy x->type && x->type->get_mtu) 1902c5c25238SPatrick McHardy res = x->type->get_mtu(x, mtu); 19031da177e4SLinus Torvalds else 190428121617SPatrick McHardy res = mtu - x->props.header_len; 19051da177e4SLinus Torvalds spin_unlock_bh(&x->lock); 19061da177e4SLinus Torvalds return res; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 1909a454f0ccSWei Yongjun int __xfrm_init_state(struct xfrm_state *x, bool init_replay) 191072cb6962SHerbert Xu { 1911d094cd83SHerbert Xu struct xfrm_state_afinfo *afinfo; 1912df9dcb45SKazunori MIYAZAWA struct xfrm_mode *inner_mode; 1913d094cd83SHerbert Xu int family = x->props.family; 191472cb6962SHerbert Xu int err; 191572cb6962SHerbert Xu 1916d094cd83SHerbert Xu err = -EAFNOSUPPORT; 1917d094cd83SHerbert Xu afinfo = xfrm_state_get_afinfo(family); 1918d094cd83SHerbert Xu if (!afinfo) 1919d094cd83SHerbert Xu goto error; 1920d094cd83SHerbert Xu 1921d094cd83SHerbert Xu err = 0; 1922d094cd83SHerbert Xu if (afinfo->init_flags) 1923d094cd83SHerbert Xu err = afinfo->init_flags(x); 1924d094cd83SHerbert Xu 1925d094cd83SHerbert Xu xfrm_state_put_afinfo(afinfo); 1926d094cd83SHerbert Xu 1927d094cd83SHerbert Xu if (err) 1928d094cd83SHerbert Xu goto error; 1929d094cd83SHerbert Xu 1930d094cd83SHerbert Xu err = -EPROTONOSUPPORT; 1931df9dcb45SKazunori MIYAZAWA 1932df9dcb45SKazunori MIYAZAWA if (x->sel.family != AF_UNSPEC) { 1933df9dcb45SKazunori MIYAZAWA inner_mode = xfrm_get_mode(x->props.mode, x->sel.family); 1934df9dcb45SKazunori MIYAZAWA if (inner_mode == NULL) 193513996378SHerbert Xu goto error; 193613996378SHerbert Xu 1937df9dcb45SKazunori MIYAZAWA if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) && 1938df9dcb45SKazunori MIYAZAWA family != x->sel.family) { 1939df9dcb45SKazunori MIYAZAWA xfrm_put_mode(inner_mode); 194013996378SHerbert Xu goto error; 1941df9dcb45SKazunori MIYAZAWA } 1942df9dcb45SKazunori MIYAZAWA 1943df9dcb45SKazunori MIYAZAWA x->inner_mode = inner_mode; 1944df9dcb45SKazunori MIYAZAWA } else { 1945df9dcb45SKazunori MIYAZAWA struct xfrm_mode *inner_mode_iaf; 1946d81d2285SMartin Willi int iafamily = AF_INET; 1947df9dcb45SKazunori MIYAZAWA 1948d81d2285SMartin Willi inner_mode = xfrm_get_mode(x->props.mode, x->props.family); 1949df9dcb45SKazunori MIYAZAWA if (inner_mode == NULL) 1950df9dcb45SKazunori MIYAZAWA goto error; 1951df9dcb45SKazunori MIYAZAWA 1952df9dcb45SKazunori MIYAZAWA if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) { 1953df9dcb45SKazunori MIYAZAWA xfrm_put_mode(inner_mode); 1954df9dcb45SKazunori MIYAZAWA goto error; 1955df9dcb45SKazunori MIYAZAWA } 1956df9dcb45SKazunori MIYAZAWA x->inner_mode = inner_mode; 1957d81d2285SMartin Willi 1958d81d2285SMartin Willi if (x->props.family == AF_INET) 1959d81d2285SMartin Willi iafamily = AF_INET6; 1960d81d2285SMartin Willi 1961d81d2285SMartin Willi inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily); 1962d81d2285SMartin Willi if (inner_mode_iaf) { 1963d81d2285SMartin Willi if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL) 1964df9dcb45SKazunori MIYAZAWA x->inner_mode_iaf = inner_mode_iaf; 1965d81d2285SMartin Willi else 1966d81d2285SMartin Willi xfrm_put_mode(inner_mode_iaf); 1967df9dcb45SKazunori MIYAZAWA } 1968df9dcb45SKazunori MIYAZAWA } 196913996378SHerbert Xu 1970d094cd83SHerbert Xu x->type = xfrm_get_type(x->id.proto, family); 197172cb6962SHerbert Xu if (x->type == NULL) 197272cb6962SHerbert Xu goto error; 197372cb6962SHerbert Xu 197472cb6962SHerbert Xu err = x->type->init_state(x); 197572cb6962SHerbert Xu if (err) 197672cb6962SHerbert Xu goto error; 197772cb6962SHerbert Xu 197813996378SHerbert Xu x->outer_mode = xfrm_get_mode(x->props.mode, family); 1979599901c3SJulia Lawall if (x->outer_mode == NULL) { 1980599901c3SJulia Lawall err = -EPROTONOSUPPORT; 1981b59f45d0SHerbert Xu goto error; 1982599901c3SJulia Lawall } 1983b59f45d0SHerbert Xu 1984a454f0ccSWei Yongjun if (init_replay) { 1985a454f0ccSWei Yongjun err = xfrm_init_replay(x); 1986a454f0ccSWei Yongjun if (err) 1987a454f0ccSWei Yongjun goto error; 1988a454f0ccSWei Yongjun } 1989a454f0ccSWei Yongjun 199072cb6962SHerbert Xu x->km.state = XFRM_STATE_VALID; 199172cb6962SHerbert Xu 199272cb6962SHerbert Xu error: 199372cb6962SHerbert Xu return err; 199472cb6962SHerbert Xu } 199572cb6962SHerbert Xu 1996a454f0ccSWei Yongjun EXPORT_SYMBOL(__xfrm_init_state); 1997a454f0ccSWei Yongjun 1998a454f0ccSWei Yongjun int xfrm_init_state(struct xfrm_state *x) 1999a454f0ccSWei Yongjun { 2000a454f0ccSWei Yongjun return __xfrm_init_state(x, true); 2001a454f0ccSWei Yongjun } 2002a454f0ccSWei Yongjun 200372cb6962SHerbert Xu EXPORT_SYMBOL(xfrm_init_state); 20041da177e4SLinus Torvalds 2005d62ddc21SAlexey Dobriyan int __net_init xfrm_state_init(struct net *net) 20061da177e4SLinus Torvalds { 2007f034b5d4SDavid S. Miller unsigned int sz; 20081da177e4SLinus Torvalds 20099d4139c7SAlexey Dobriyan INIT_LIST_HEAD(&net->xfrm.state_all); 20109d4139c7SAlexey Dobriyan 2011f034b5d4SDavid S. Miller sz = sizeof(struct hlist_head) * 8; 2012f034b5d4SDavid S. Miller 201373d189dcSAlexey Dobriyan net->xfrm.state_bydst = xfrm_hash_alloc(sz); 201473d189dcSAlexey Dobriyan if (!net->xfrm.state_bydst) 201573d189dcSAlexey Dobriyan goto out_bydst; 2016d320bbb3SAlexey Dobriyan net->xfrm.state_bysrc = xfrm_hash_alloc(sz); 2017d320bbb3SAlexey Dobriyan if (!net->xfrm.state_bysrc) 2018d320bbb3SAlexey Dobriyan goto out_bysrc; 2019b754a4fdSAlexey Dobriyan net->xfrm.state_byspi = xfrm_hash_alloc(sz); 2020b754a4fdSAlexey Dobriyan if (!net->xfrm.state_byspi) 2021b754a4fdSAlexey Dobriyan goto out_byspi; 2022529983ecSAlexey Dobriyan net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1); 2023f034b5d4SDavid S. Miller 20240bf7c5b0SAlexey Dobriyan net->xfrm.state_num = 0; 202563082733SAlexey Dobriyan INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize); 2026b8a0ae20SAlexey Dobriyan INIT_HLIST_HEAD(&net->xfrm.state_gc_list); 2027c7837144SAlexey Dobriyan INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task); 202850a30657SAlexey Dobriyan init_waitqueue_head(&net->xfrm.km_waitq); 2029d62ddc21SAlexey Dobriyan return 0; 203073d189dcSAlexey Dobriyan 2031b754a4fdSAlexey Dobriyan out_byspi: 2032b754a4fdSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bysrc, sz); 2033d320bbb3SAlexey Dobriyan out_bysrc: 2034d320bbb3SAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bydst, sz); 203573d189dcSAlexey Dobriyan out_bydst: 203673d189dcSAlexey Dobriyan return -ENOMEM; 2037d62ddc21SAlexey Dobriyan } 2038d62ddc21SAlexey Dobriyan 2039d62ddc21SAlexey Dobriyan void xfrm_state_fini(struct net *net) 2040d62ddc21SAlexey Dobriyan { 20417c2776eeSAlexey Dobriyan struct xfrm_audit audit_info; 204273d189dcSAlexey Dobriyan unsigned int sz; 204373d189dcSAlexey Dobriyan 20447c2776eeSAlexey Dobriyan flush_work(&net->xfrm.state_hash_work); 2045e1760bd5SEric W. Biederman audit_info.loginuid = INVALID_UID; 20467c2776eeSAlexey Dobriyan audit_info.sessionid = -1; 20477c2776eeSAlexey Dobriyan audit_info.secid = 0; 20487c2776eeSAlexey Dobriyan xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info); 20497c2776eeSAlexey Dobriyan flush_work(&net->xfrm.state_gc_work); 20507c2776eeSAlexey Dobriyan 20519d4139c7SAlexey Dobriyan WARN_ON(!list_empty(&net->xfrm.state_all)); 205273d189dcSAlexey Dobriyan 2053529983ecSAlexey Dobriyan sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head); 2054b754a4fdSAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_byspi)); 2055b754a4fdSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_byspi, sz); 2056d320bbb3SAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_bysrc)); 2057d320bbb3SAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bysrc, sz); 205873d189dcSAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_bydst)); 205973d189dcSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bydst, sz); 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 2062ab5f5e8bSJoy Latten #ifdef CONFIG_AUDITSYSCALL 2063cf35f43eSIlpo Järvinen static void xfrm_audit_helper_sainfo(struct xfrm_state *x, 2064ab5f5e8bSJoy Latten struct audit_buffer *audit_buf) 2065ab5f5e8bSJoy Latten { 206668277accSPaul Moore struct xfrm_sec_ctx *ctx = x->security; 206768277accSPaul Moore u32 spi = ntohl(x->id.spi); 206868277accSPaul Moore 206968277accSPaul Moore if (ctx) 2070ab5f5e8bSJoy Latten audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", 207168277accSPaul Moore ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); 2072ab5f5e8bSJoy Latten 2073ab5f5e8bSJoy Latten switch(x->props.family) { 2074ab5f5e8bSJoy Latten case AF_INET: 207521454aaaSHarvey Harrison audit_log_format(audit_buf, " src=%pI4 dst=%pI4", 207621454aaaSHarvey Harrison &x->props.saddr.a4, &x->id.daddr.a4); 2077ab5f5e8bSJoy Latten break; 2078ab5f5e8bSJoy Latten case AF_INET6: 20795b095d98SHarvey Harrison audit_log_format(audit_buf, " src=%pI6 dst=%pI6", 2080fdb46ee7SHarvey Harrison x->props.saddr.a6, x->id.daddr.a6); 2081ab5f5e8bSJoy Latten break; 2082ab5f5e8bSJoy Latten } 208368277accSPaul Moore 208468277accSPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); 2085ab5f5e8bSJoy Latten } 2086ab5f5e8bSJoy Latten 2087cf35f43eSIlpo Järvinen static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family, 2088afeb14b4SPaul Moore struct audit_buffer *audit_buf) 2089afeb14b4SPaul Moore { 2090b71d1d42SEric Dumazet const struct iphdr *iph4; 2091b71d1d42SEric Dumazet const struct ipv6hdr *iph6; 2092afeb14b4SPaul Moore 2093afeb14b4SPaul Moore switch (family) { 2094afeb14b4SPaul Moore case AF_INET: 2095afeb14b4SPaul Moore iph4 = ip_hdr(skb); 209621454aaaSHarvey Harrison audit_log_format(audit_buf, " src=%pI4 dst=%pI4", 209721454aaaSHarvey Harrison &iph4->saddr, &iph4->daddr); 2098afeb14b4SPaul Moore break; 2099afeb14b4SPaul Moore case AF_INET6: 2100afeb14b4SPaul Moore iph6 = ipv6_hdr(skb); 2101afeb14b4SPaul Moore audit_log_format(audit_buf, 21025b095d98SHarvey Harrison " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x", 2103fdb46ee7SHarvey Harrison &iph6->saddr,&iph6->daddr, 2104afeb14b4SPaul Moore iph6->flow_lbl[0] & 0x0f, 2105afeb14b4SPaul Moore iph6->flow_lbl[1], 2106afeb14b4SPaul Moore iph6->flow_lbl[2]); 2107afeb14b4SPaul Moore break; 2108afeb14b4SPaul Moore } 2109afeb14b4SPaul Moore } 2110afeb14b4SPaul Moore 211168277accSPaul Moore void xfrm_audit_state_add(struct xfrm_state *x, int result, 2112e1760bd5SEric W. Biederman kuid_t auid, u32 sessionid, u32 secid) 2113ab5f5e8bSJoy Latten { 2114ab5f5e8bSJoy Latten struct audit_buffer *audit_buf; 2115ab5f5e8bSJoy Latten 2116afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SAD-add"); 2117ab5f5e8bSJoy Latten if (audit_buf == NULL) 2118ab5f5e8bSJoy Latten return; 21192532386fSEric Paris xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf); 2120afeb14b4SPaul Moore xfrm_audit_helper_sainfo(x, audit_buf); 2121afeb14b4SPaul Moore audit_log_format(audit_buf, " res=%u", result); 2122ab5f5e8bSJoy Latten audit_log_end(audit_buf); 2123ab5f5e8bSJoy Latten } 2124ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_add); 2125ab5f5e8bSJoy Latten 212668277accSPaul Moore void xfrm_audit_state_delete(struct xfrm_state *x, int result, 2127e1760bd5SEric W. Biederman kuid_t auid, u32 sessionid, u32 secid) 2128ab5f5e8bSJoy Latten { 2129ab5f5e8bSJoy Latten struct audit_buffer *audit_buf; 2130ab5f5e8bSJoy Latten 2131afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SAD-delete"); 2132ab5f5e8bSJoy Latten if (audit_buf == NULL) 2133ab5f5e8bSJoy Latten return; 21342532386fSEric Paris xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf); 2135afeb14b4SPaul Moore xfrm_audit_helper_sainfo(x, audit_buf); 2136afeb14b4SPaul Moore audit_log_format(audit_buf, " res=%u", result); 2137ab5f5e8bSJoy Latten audit_log_end(audit_buf); 2138ab5f5e8bSJoy Latten } 2139ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_delete); 2140afeb14b4SPaul Moore 2141afeb14b4SPaul Moore void xfrm_audit_state_replay_overflow(struct xfrm_state *x, 2142afeb14b4SPaul Moore struct sk_buff *skb) 2143afeb14b4SPaul Moore { 2144afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2145afeb14b4SPaul Moore u32 spi; 2146afeb14b4SPaul Moore 2147afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-replay-overflow"); 2148afeb14b4SPaul Moore if (audit_buf == NULL) 2149afeb14b4SPaul Moore return; 2150afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2151afeb14b4SPaul Moore /* don't record the sequence number because it's inherent in this kind 2152afeb14b4SPaul Moore * of audit message */ 2153afeb14b4SPaul Moore spi = ntohl(x->id.spi); 2154afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); 2155afeb14b4SPaul Moore audit_log_end(audit_buf); 2156afeb14b4SPaul Moore } 2157afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow); 2158afeb14b4SPaul Moore 21599fdc4883SSteffen Klassert void xfrm_audit_state_replay(struct xfrm_state *x, 2160afeb14b4SPaul Moore struct sk_buff *skb, __be32 net_seq) 2161afeb14b4SPaul Moore { 2162afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2163afeb14b4SPaul Moore u32 spi; 2164afeb14b4SPaul Moore 2165afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-replayed-pkt"); 2166afeb14b4SPaul Moore if (audit_buf == NULL) 2167afeb14b4SPaul Moore return; 2168afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2169afeb14b4SPaul Moore spi = ntohl(x->id.spi); 2170afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2171afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2172afeb14b4SPaul Moore audit_log_end(audit_buf); 2173afeb14b4SPaul Moore } 21749fdc4883SSteffen Klassert EXPORT_SYMBOL_GPL(xfrm_audit_state_replay); 2175afeb14b4SPaul Moore 2176afeb14b4SPaul Moore void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family) 2177afeb14b4SPaul Moore { 2178afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2179afeb14b4SPaul Moore 2180afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-notfound"); 2181afeb14b4SPaul Moore if (audit_buf == NULL) 2182afeb14b4SPaul Moore return; 2183afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, family, audit_buf); 2184afeb14b4SPaul Moore audit_log_end(audit_buf); 2185afeb14b4SPaul Moore } 2186afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple); 2187afeb14b4SPaul Moore 2188afeb14b4SPaul Moore void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, 2189afeb14b4SPaul Moore __be32 net_spi, __be32 net_seq) 2190afeb14b4SPaul Moore { 2191afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2192afeb14b4SPaul Moore u32 spi; 2193afeb14b4SPaul Moore 2194afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-notfound"); 2195afeb14b4SPaul Moore if (audit_buf == NULL) 2196afeb14b4SPaul Moore return; 2197afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, family, audit_buf); 2198afeb14b4SPaul Moore spi = ntohl(net_spi); 2199afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2200afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2201afeb14b4SPaul Moore audit_log_end(audit_buf); 2202afeb14b4SPaul Moore } 2203afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound); 2204afeb14b4SPaul Moore 2205afeb14b4SPaul Moore void xfrm_audit_state_icvfail(struct xfrm_state *x, 2206afeb14b4SPaul Moore struct sk_buff *skb, u8 proto) 2207afeb14b4SPaul Moore { 2208afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2209afeb14b4SPaul Moore __be32 net_spi; 2210afeb14b4SPaul Moore __be32 net_seq; 2211afeb14b4SPaul Moore 2212afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-icv-failure"); 2213afeb14b4SPaul Moore if (audit_buf == NULL) 2214afeb14b4SPaul Moore return; 2215afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2216afeb14b4SPaul Moore if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) { 2217afeb14b4SPaul Moore u32 spi = ntohl(net_spi); 2218afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2219afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2220afeb14b4SPaul Moore } 2221afeb14b4SPaul Moore audit_log_end(audit_buf); 2222afeb14b4SPaul Moore } 2223afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail); 2224ab5f5e8bSJoy Latten #endif /* CONFIG_AUDITSYSCALL */ 2225