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 { 75f034b5d4SDavid S. Miller struct hlist_node *entry, *tmp; 76f034b5d4SDavid S. Miller struct xfrm_state *x; 77f034b5d4SDavid S. Miller 78f034b5d4SDavid S. Miller hlist_for_each_entry_safe(x, entry, 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 1611da177e4SLinus Torvalds static DEFINE_RWLOCK(xfrm_state_afinfo_lock); 1621da177e4SLinus Torvalds static struct xfrm_state_afinfo *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); 16953bc6b4dSJamal Hadi Salim void km_state_expired(struct xfrm_state *x, int hard, u32 pid); 1701da177e4SLinus Torvalds 171aa5d62ccSHerbert Xu static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family) 172aa5d62ccSHerbert Xu { 173aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 174aa5d62ccSHerbert Xu if (unlikely(family >= NPROTO)) 175aa5d62ccSHerbert Xu return NULL; 176aa5d62ccSHerbert Xu write_lock_bh(&xfrm_state_afinfo_lock); 177aa5d62ccSHerbert Xu afinfo = xfrm_state_afinfo[family]; 178aa5d62ccSHerbert Xu if (unlikely(!afinfo)) 179aa5d62ccSHerbert Xu write_unlock_bh(&xfrm_state_afinfo_lock); 180aa5d62ccSHerbert Xu return afinfo; 181aa5d62ccSHerbert Xu } 182aa5d62ccSHerbert Xu 183aa5d62ccSHerbert Xu static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo) 1849a429c49SEric Dumazet __releases(xfrm_state_afinfo_lock) 185aa5d62ccSHerbert Xu { 186aa5d62ccSHerbert Xu write_unlock_bh(&xfrm_state_afinfo_lock); 187aa5d62ccSHerbert Xu } 188aa5d62ccSHerbert Xu 189533cb5b0SEric Dumazet int xfrm_register_type(const struct xfrm_type *type, unsigned short family) 190aa5d62ccSHerbert Xu { 191aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family); 192533cb5b0SEric Dumazet const struct xfrm_type **typemap; 193aa5d62ccSHerbert Xu int err = 0; 194aa5d62ccSHerbert Xu 195aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 196aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 197aa5d62ccSHerbert Xu typemap = afinfo->type_map; 198aa5d62ccSHerbert Xu 199aa5d62ccSHerbert Xu if (likely(typemap[type->proto] == NULL)) 200aa5d62ccSHerbert Xu typemap[type->proto] = type; 201aa5d62ccSHerbert Xu else 202aa5d62ccSHerbert Xu err = -EEXIST; 203aa5d62ccSHerbert Xu xfrm_state_unlock_afinfo(afinfo); 204aa5d62ccSHerbert Xu return err; 205aa5d62ccSHerbert Xu } 206aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_type); 207aa5d62ccSHerbert Xu 208533cb5b0SEric Dumazet int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family) 209aa5d62ccSHerbert Xu { 210aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family); 211533cb5b0SEric Dumazet const struct xfrm_type **typemap; 212aa5d62ccSHerbert Xu int err = 0; 213aa5d62ccSHerbert Xu 214aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 215aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 216aa5d62ccSHerbert Xu typemap = afinfo->type_map; 217aa5d62ccSHerbert Xu 218aa5d62ccSHerbert Xu if (unlikely(typemap[type->proto] != type)) 219aa5d62ccSHerbert Xu err = -ENOENT; 220aa5d62ccSHerbert Xu else 221aa5d62ccSHerbert Xu typemap[type->proto] = NULL; 222aa5d62ccSHerbert Xu xfrm_state_unlock_afinfo(afinfo); 223aa5d62ccSHerbert Xu return err; 224aa5d62ccSHerbert Xu } 225aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_type); 226aa5d62ccSHerbert Xu 227533cb5b0SEric Dumazet static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family) 228aa5d62ccSHerbert Xu { 229aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 230533cb5b0SEric Dumazet const struct xfrm_type **typemap; 231533cb5b0SEric Dumazet const struct xfrm_type *type; 232aa5d62ccSHerbert Xu int modload_attempted = 0; 233aa5d62ccSHerbert Xu 234aa5d62ccSHerbert Xu retry: 235aa5d62ccSHerbert Xu afinfo = xfrm_state_get_afinfo(family); 236aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 237aa5d62ccSHerbert Xu return NULL; 238aa5d62ccSHerbert Xu typemap = afinfo->type_map; 239aa5d62ccSHerbert Xu 240aa5d62ccSHerbert Xu type = typemap[proto]; 241aa5d62ccSHerbert Xu if (unlikely(type && !try_module_get(type->owner))) 242aa5d62ccSHerbert Xu type = NULL; 243aa5d62ccSHerbert Xu if (!type && !modload_attempted) { 244aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 245aa5d62ccSHerbert Xu request_module("xfrm-type-%d-%d", family, proto); 246aa5d62ccSHerbert Xu modload_attempted = 1; 247aa5d62ccSHerbert Xu goto retry; 248aa5d62ccSHerbert Xu } 249aa5d62ccSHerbert Xu 250aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 251aa5d62ccSHerbert Xu return type; 252aa5d62ccSHerbert Xu } 253aa5d62ccSHerbert Xu 254533cb5b0SEric Dumazet static void xfrm_put_type(const struct xfrm_type *type) 255aa5d62ccSHerbert Xu { 256aa5d62ccSHerbert Xu module_put(type->owner); 257aa5d62ccSHerbert Xu } 258aa5d62ccSHerbert Xu 259aa5d62ccSHerbert Xu int xfrm_register_mode(struct xfrm_mode *mode, int family) 260aa5d62ccSHerbert Xu { 261aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 262aa5d62ccSHerbert Xu struct xfrm_mode **modemap; 263aa5d62ccSHerbert Xu int err; 264aa5d62ccSHerbert Xu 265aa5d62ccSHerbert Xu if (unlikely(mode->encap >= XFRM_MODE_MAX)) 266aa5d62ccSHerbert Xu return -EINVAL; 267aa5d62ccSHerbert Xu 268aa5d62ccSHerbert Xu afinfo = xfrm_state_lock_afinfo(family); 269aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 270aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 271aa5d62ccSHerbert Xu 272aa5d62ccSHerbert Xu err = -EEXIST; 273aa5d62ccSHerbert Xu modemap = afinfo->mode_map; 27417c2a42aSHerbert Xu if (modemap[mode->encap]) 27517c2a42aSHerbert Xu goto out; 27617c2a42aSHerbert Xu 27717c2a42aSHerbert Xu err = -ENOENT; 27817c2a42aSHerbert Xu if (!try_module_get(afinfo->owner)) 27917c2a42aSHerbert Xu goto out; 28017c2a42aSHerbert Xu 28117c2a42aSHerbert Xu mode->afinfo = afinfo; 282aa5d62ccSHerbert Xu modemap[mode->encap] = mode; 283aa5d62ccSHerbert Xu err = 0; 284aa5d62ccSHerbert Xu 28517c2a42aSHerbert Xu out: 286aa5d62ccSHerbert Xu xfrm_state_unlock_afinfo(afinfo); 287aa5d62ccSHerbert Xu return err; 288aa5d62ccSHerbert Xu } 289aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_mode); 290aa5d62ccSHerbert Xu 291aa5d62ccSHerbert Xu int xfrm_unregister_mode(struct xfrm_mode *mode, int family) 292aa5d62ccSHerbert Xu { 293aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 294aa5d62ccSHerbert Xu struct xfrm_mode **modemap; 295aa5d62ccSHerbert Xu int err; 296aa5d62ccSHerbert Xu 297aa5d62ccSHerbert Xu if (unlikely(mode->encap >= XFRM_MODE_MAX)) 298aa5d62ccSHerbert Xu return -EINVAL; 299aa5d62ccSHerbert Xu 300aa5d62ccSHerbert Xu afinfo = xfrm_state_lock_afinfo(family); 301aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 302aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 303aa5d62ccSHerbert Xu 304aa5d62ccSHerbert Xu err = -ENOENT; 305aa5d62ccSHerbert Xu modemap = afinfo->mode_map; 306aa5d62ccSHerbert Xu if (likely(modemap[mode->encap] == mode)) { 307aa5d62ccSHerbert Xu modemap[mode->encap] = NULL; 30817c2a42aSHerbert Xu module_put(mode->afinfo->owner); 309aa5d62ccSHerbert Xu err = 0; 310aa5d62ccSHerbert Xu } 311aa5d62ccSHerbert Xu 312aa5d62ccSHerbert Xu xfrm_state_unlock_afinfo(afinfo); 313aa5d62ccSHerbert Xu return err; 314aa5d62ccSHerbert Xu } 315aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_mode); 316aa5d62ccSHerbert Xu 317aa5d62ccSHerbert Xu static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family) 318aa5d62ccSHerbert Xu { 319aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 320aa5d62ccSHerbert Xu struct xfrm_mode *mode; 321aa5d62ccSHerbert Xu int modload_attempted = 0; 322aa5d62ccSHerbert Xu 323aa5d62ccSHerbert Xu if (unlikely(encap >= XFRM_MODE_MAX)) 324aa5d62ccSHerbert Xu return NULL; 325aa5d62ccSHerbert Xu 326aa5d62ccSHerbert Xu retry: 327aa5d62ccSHerbert Xu afinfo = xfrm_state_get_afinfo(family); 328aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 329aa5d62ccSHerbert Xu return NULL; 330aa5d62ccSHerbert Xu 331aa5d62ccSHerbert Xu mode = afinfo->mode_map[encap]; 332aa5d62ccSHerbert Xu if (unlikely(mode && !try_module_get(mode->owner))) 333aa5d62ccSHerbert Xu mode = NULL; 334aa5d62ccSHerbert Xu if (!mode && !modload_attempted) { 335aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 336aa5d62ccSHerbert Xu request_module("xfrm-mode-%d-%d", family, encap); 337aa5d62ccSHerbert Xu modload_attempted = 1; 338aa5d62ccSHerbert Xu goto retry; 339aa5d62ccSHerbert Xu } 340aa5d62ccSHerbert Xu 341aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 342aa5d62ccSHerbert Xu return mode; 343aa5d62ccSHerbert Xu } 344aa5d62ccSHerbert Xu 345aa5d62ccSHerbert Xu static void xfrm_put_mode(struct xfrm_mode *mode) 346aa5d62ccSHerbert Xu { 347aa5d62ccSHerbert Xu module_put(mode->owner); 348aa5d62ccSHerbert Xu } 349aa5d62ccSHerbert Xu 3501da177e4SLinus Torvalds static void xfrm_state_gc_destroy(struct xfrm_state *x) 3511da177e4SLinus Torvalds { 3529e0d57fdSYury Polyanskiy tasklet_hrtimer_cancel(&x->mtimer); 353a47f0ce0SDavid S. Miller del_timer_sync(&x->rtimer); 3541da177e4SLinus Torvalds kfree(x->aalg); 3551da177e4SLinus Torvalds kfree(x->ealg); 3561da177e4SLinus Torvalds kfree(x->calg); 3571da177e4SLinus Torvalds kfree(x->encap); 358060f02a3SNoriaki TAKAMIYA kfree(x->coaddr); 359d8647b79SSteffen Klassert kfree(x->replay_esn); 360d8647b79SSteffen Klassert kfree(x->preplay_esn); 36113996378SHerbert Xu if (x->inner_mode) 36213996378SHerbert Xu xfrm_put_mode(x->inner_mode); 363df9dcb45SKazunori MIYAZAWA if (x->inner_mode_iaf) 364df9dcb45SKazunori MIYAZAWA xfrm_put_mode(x->inner_mode_iaf); 36513996378SHerbert Xu if (x->outer_mode) 36613996378SHerbert Xu xfrm_put_mode(x->outer_mode); 3671da177e4SLinus Torvalds if (x->type) { 3681da177e4SLinus Torvalds x->type->destructor(x); 3691da177e4SLinus Torvalds xfrm_put_type(x->type); 3701da177e4SLinus Torvalds } 371df71837dSTrent Jaeger security_xfrm_state_free(x); 3721da177e4SLinus Torvalds kfree(x); 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 375c7837144SAlexey Dobriyan static void xfrm_state_gc_task(struct work_struct *work) 3761da177e4SLinus Torvalds { 377c7837144SAlexey Dobriyan struct net *net = container_of(work, struct net, xfrm.state_gc_work); 37812a169e7SHerbert Xu struct xfrm_state *x; 37912a169e7SHerbert Xu struct hlist_node *entry, *tmp; 38012a169e7SHerbert Xu struct hlist_head gc_list; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_gc_lock); 383c7837144SAlexey Dobriyan hlist_move_list(&net->xfrm.state_gc_list, &gc_list); 3841da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_gc_lock); 3851da177e4SLinus Torvalds 38612a169e7SHerbert Xu hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist) 3871da177e4SLinus Torvalds xfrm_state_gc_destroy(x); 3888f126e37SDavid S. Miller 38950a30657SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds static inline unsigned long make_jiffies(long secs) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) 3951da177e4SLinus Torvalds return MAX_SCHEDULE_TIMEOUT-1; 3961da177e4SLinus Torvalds else 3971da177e4SLinus Torvalds return secs*HZ; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4009e0d57fdSYury Polyanskiy static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me) 4011da177e4SLinus Torvalds { 4029e0d57fdSYury Polyanskiy struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer); 4039e0d57fdSYury Polyanskiy struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer); 40498806f75SAlexey Dobriyan struct net *net = xs_net(x); 4059d729f72SJames Morris unsigned long now = get_seconds(); 4061da177e4SLinus Torvalds long next = LONG_MAX; 4071da177e4SLinus Torvalds int warn = 0; 408161a09e7SJoy Latten int err = 0; 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds spin_lock(&x->lock); 4111da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_DEAD) 4121da177e4SLinus Torvalds goto out; 4131da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_EXPIRED) 4141da177e4SLinus Torvalds goto expired; 4151da177e4SLinus Torvalds if (x->lft.hard_add_expires_seconds) { 4161da177e4SLinus Torvalds long tmo = x->lft.hard_add_expires_seconds + 4171da177e4SLinus Torvalds x->curlft.add_time - now; 4181da177e4SLinus Torvalds if (tmo <= 0) 4191da177e4SLinus Torvalds goto expired; 4201da177e4SLinus Torvalds if (tmo < next) 4211da177e4SLinus Torvalds next = tmo; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds if (x->lft.hard_use_expires_seconds) { 4241da177e4SLinus Torvalds long tmo = x->lft.hard_use_expires_seconds + 4251da177e4SLinus Torvalds (x->curlft.use_time ? : now) - now; 4261da177e4SLinus Torvalds if (tmo <= 0) 4271da177e4SLinus Torvalds goto expired; 4281da177e4SLinus Torvalds if (tmo < next) 4291da177e4SLinus Torvalds next = tmo; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds if (x->km.dying) 4321da177e4SLinus Torvalds goto resched; 4331da177e4SLinus Torvalds if (x->lft.soft_add_expires_seconds) { 4341da177e4SLinus Torvalds long tmo = x->lft.soft_add_expires_seconds + 4351da177e4SLinus Torvalds x->curlft.add_time - now; 4361da177e4SLinus Torvalds if (tmo <= 0) 4371da177e4SLinus Torvalds warn = 1; 4381da177e4SLinus Torvalds else if (tmo < next) 4391da177e4SLinus Torvalds next = tmo; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds if (x->lft.soft_use_expires_seconds) { 4421da177e4SLinus Torvalds long tmo = x->lft.soft_use_expires_seconds + 4431da177e4SLinus Torvalds (x->curlft.use_time ? : now) - now; 4441da177e4SLinus Torvalds if (tmo <= 0) 4451da177e4SLinus Torvalds warn = 1; 4461da177e4SLinus Torvalds else if (tmo < next) 4471da177e4SLinus Torvalds next = tmo; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4504666faabSHerbert Xu x->km.dying = warn; 4511da177e4SLinus Torvalds if (warn) 45253bc6b4dSJamal Hadi Salim km_state_expired(x, 0, 0); 4531da177e4SLinus Torvalds resched: 4549e0d57fdSYury Polyanskiy if (next != LONG_MAX){ 4559e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL); 4569e0d57fdSYury Polyanskiy } 457a47f0ce0SDavid S. Miller 4581da177e4SLinus Torvalds goto out; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds expired: 4611da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) { 4621da177e4SLinus Torvalds x->km.state = XFRM_STATE_EXPIRED; 46398806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 4641da177e4SLinus Torvalds next = 2; 4651da177e4SLinus Torvalds goto resched; 4661da177e4SLinus Torvalds } 467161a09e7SJoy Latten 468161a09e7SJoy Latten err = __xfrm_state_delete(x); 469161a09e7SJoy Latten if (!err && x->id.spi) 47053bc6b4dSJamal Hadi Salim km_state_expired(x, 1, 0); 4711da177e4SLinus Torvalds 472ab5f5e8bSJoy Latten xfrm_audit_state_delete(x, err ? 0 : 1, 4732532386fSEric Paris audit_get_loginuid(current), 4742532386fSEric Paris audit_get_sessionid(current), 0); 475161a09e7SJoy Latten 4761da177e4SLinus Torvalds out: 4771da177e4SLinus Torvalds spin_unlock(&x->lock); 4789e0d57fdSYury Polyanskiy return HRTIMER_NORESTART; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4810ac84752SDavid S. Miller static void xfrm_replay_timer_handler(unsigned long data); 4820ac84752SDavid S. Miller 483673c09beSAlexey Dobriyan struct xfrm_state *xfrm_state_alloc(struct net *net) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds struct xfrm_state *x; 4861da177e4SLinus Torvalds 4870da974f4SPanagiotis Issaris x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC); 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds if (x) { 490673c09beSAlexey Dobriyan write_pnet(&x->xs_net, net); 4911da177e4SLinus Torvalds atomic_set(&x->refcnt, 1); 4921da177e4SLinus Torvalds atomic_set(&x->tunnel_users, 0); 49312a169e7SHerbert Xu INIT_LIST_HEAD(&x->km.all); 4948f126e37SDavid S. Miller INIT_HLIST_NODE(&x->bydst); 4958f126e37SDavid S. Miller INIT_HLIST_NODE(&x->bysrc); 4968f126e37SDavid S. Miller INIT_HLIST_NODE(&x->byspi); 4979e0d57fdSYury Polyanskiy tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS); 498b24b8a24SPavel Emelyanov setup_timer(&x->rtimer, xfrm_replay_timer_handler, 499b24b8a24SPavel Emelyanov (unsigned long)x); 5009d729f72SJames Morris x->curlft.add_time = get_seconds(); 5011da177e4SLinus Torvalds x->lft.soft_byte_limit = XFRM_INF; 5021da177e4SLinus Torvalds x->lft.soft_packet_limit = XFRM_INF; 5031da177e4SLinus Torvalds x->lft.hard_byte_limit = XFRM_INF; 5041da177e4SLinus Torvalds x->lft.hard_packet_limit = XFRM_INF; 505f8cd5488SJamal Hadi Salim x->replay_maxage = 0; 506f8cd5488SJamal Hadi Salim x->replay_maxdiff = 0; 507df9dcb45SKazunori MIYAZAWA x->inner_mode = NULL; 508df9dcb45SKazunori MIYAZAWA x->inner_mode_iaf = NULL; 5091da177e4SLinus Torvalds spin_lock_init(&x->lock); 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds return x; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_alloc); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds void __xfrm_state_destroy(struct xfrm_state *x) 5161da177e4SLinus Torvalds { 51798806f75SAlexey Dobriyan struct net *net = xs_net(x); 51898806f75SAlexey Dobriyan 519547b792cSIlpo Järvinen WARN_ON(x->km.state != XFRM_STATE_DEAD); 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_gc_lock); 52298806f75SAlexey Dobriyan hlist_add_head(&x->gclist, &net->xfrm.state_gc_list); 5231da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_gc_lock); 52498806f75SAlexey Dobriyan schedule_work(&net->xfrm.state_gc_work); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds EXPORT_SYMBOL(__xfrm_state_destroy); 5271da177e4SLinus Torvalds 52853bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x) 5291da177e4SLinus Torvalds { 53098806f75SAlexey Dobriyan struct net *net = xs_net(x); 53126b15dadSJamal Hadi Salim int err = -ESRCH; 53226b15dadSJamal Hadi Salim 5331da177e4SLinus Torvalds if (x->km.state != XFRM_STATE_DEAD) { 5341da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 5351da177e4SLinus Torvalds spin_lock(&xfrm_state_lock); 53612a169e7SHerbert Xu list_del(&x->km.all); 5378f126e37SDavid S. Miller hlist_del(&x->bydst); 5388f126e37SDavid S. Miller hlist_del(&x->bysrc); 539a47f0ce0SDavid S. Miller if (x->id.spi) 5408f126e37SDavid S. Miller hlist_del(&x->byspi); 54198806f75SAlexey Dobriyan net->xfrm.state_num--; 5421da177e4SLinus Torvalds spin_unlock(&xfrm_state_lock); 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* All xfrm_state objects are created by xfrm_state_alloc. 5451da177e4SLinus Torvalds * The xfrm_state_alloc call gives a reference, and that 5461da177e4SLinus Torvalds * is what we are dropping here. 5471da177e4SLinus Torvalds */ 5485dba4797SPatrick McHardy xfrm_state_put(x); 54926b15dadSJamal Hadi Salim err = 0; 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 55226b15dadSJamal Hadi Salim return err; 55326b15dadSJamal Hadi Salim } 55453bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(__xfrm_state_delete); 55526b15dadSJamal Hadi Salim 55626b15dadSJamal Hadi Salim int xfrm_state_delete(struct xfrm_state *x) 5571da177e4SLinus Torvalds { 55826b15dadSJamal Hadi Salim int err; 55926b15dadSJamal Hadi Salim 5601da177e4SLinus Torvalds spin_lock_bh(&x->lock); 56126b15dadSJamal Hadi Salim err = __xfrm_state_delete(x); 5621da177e4SLinus Torvalds spin_unlock_bh(&x->lock); 56326b15dadSJamal Hadi Salim 56426b15dadSJamal Hadi Salim return err; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete); 5671da177e4SLinus Torvalds 5684aa2e62cSJoy Latten #ifdef CONFIG_SECURITY_NETWORK_XFRM 5694aa2e62cSJoy Latten static inline int 5700e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info) 5711da177e4SLinus Torvalds { 5724aa2e62cSJoy Latten int i, err = 0; 5734aa2e62cSJoy Latten 5740e602451SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 5754aa2e62cSJoy Latten struct hlist_node *entry; 5764aa2e62cSJoy Latten struct xfrm_state *x; 5774aa2e62cSJoy Latten 5780e602451SAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) { 5794aa2e62cSJoy Latten if (xfrm_id_proto_match(x->id.proto, proto) && 5804aa2e62cSJoy Latten (err = security_xfrm_state_delete(x)) != 0) { 581ab5f5e8bSJoy Latten xfrm_audit_state_delete(x, 0, 582ab5f5e8bSJoy Latten audit_info->loginuid, 5832532386fSEric Paris audit_info->sessionid, 584ab5f5e8bSJoy Latten audit_info->secid); 5854aa2e62cSJoy Latten return err; 5864aa2e62cSJoy Latten } 5874aa2e62cSJoy Latten } 5884aa2e62cSJoy Latten } 5894aa2e62cSJoy Latten 5904aa2e62cSJoy Latten return err; 5914aa2e62cSJoy Latten } 5924aa2e62cSJoy Latten #else 5934aa2e62cSJoy Latten static inline int 5940e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info) 5954aa2e62cSJoy Latten { 5964aa2e62cSJoy Latten return 0; 5974aa2e62cSJoy Latten } 5984aa2e62cSJoy Latten #endif 5994aa2e62cSJoy Latten 6000e602451SAlexey Dobriyan int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info) 6014aa2e62cSJoy Latten { 6029e64cc95SJamal Hadi Salim int i, err = 0, cnt = 0; 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 6050e602451SAlexey Dobriyan err = xfrm_state_flush_secctx_check(net, proto, audit_info); 6064aa2e62cSJoy Latten if (err) 6074aa2e62cSJoy Latten goto out; 6084aa2e62cSJoy Latten 6099e64cc95SJamal Hadi Salim err = -ESRCH; 6100e602451SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 6118f126e37SDavid S. Miller struct hlist_node *entry; 6128f126e37SDavid S. Miller struct xfrm_state *x; 6131da177e4SLinus Torvalds restart: 6140e602451SAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) { 6151da177e4SLinus Torvalds if (!xfrm_state_kern(x) && 6165794708fSMasahide NAKAMURA xfrm_id_proto_match(x->id.proto, proto)) { 6171da177e4SLinus Torvalds xfrm_state_hold(x); 6181da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 6191da177e4SLinus Torvalds 620161a09e7SJoy Latten err = xfrm_state_delete(x); 621ab5f5e8bSJoy Latten xfrm_audit_state_delete(x, err ? 0 : 1, 622ab5f5e8bSJoy Latten audit_info->loginuid, 6232532386fSEric Paris audit_info->sessionid, 624ab5f5e8bSJoy Latten audit_info->secid); 6251da177e4SLinus Torvalds xfrm_state_put(x); 6269e64cc95SJamal Hadi Salim if (!err) 6279e64cc95SJamal Hadi Salim cnt++; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 6301da177e4SLinus Torvalds goto restart; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds } 6349e64cc95SJamal Hadi Salim if (cnt) 6354aa2e62cSJoy Latten err = 0; 6364aa2e62cSJoy Latten 6374aa2e62cSJoy Latten out: 6381da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 6390e602451SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 6404aa2e62cSJoy Latten return err; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_flush); 6431da177e4SLinus Torvalds 644e071041bSAlexey Dobriyan void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si) 64528d8909bSJamal Hadi Salim { 64628d8909bSJamal Hadi Salim spin_lock_bh(&xfrm_state_lock); 647e071041bSAlexey Dobriyan si->sadcnt = net->xfrm.state_num; 648e071041bSAlexey Dobriyan si->sadhcnt = net->xfrm.state_hmask; 64928d8909bSJamal Hadi Salim si->sadhmcnt = xfrm_state_hashmax; 65028d8909bSJamal Hadi Salim spin_unlock_bh(&xfrm_state_lock); 65128d8909bSJamal Hadi Salim } 65228d8909bSJamal Hadi Salim EXPORT_SYMBOL(xfrm_sad_getinfo); 65328d8909bSJamal Hadi Salim 6541da177e4SLinus Torvalds static int 6551a898592SDavid S. Miller xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl, 65604686013SDavid S. Miller const struct xfrm_tmpl *tmpl, 65733765d06SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 6581da177e4SLinus Torvalds unsigned short family) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 6611da177e4SLinus Torvalds if (!afinfo) 6621da177e4SLinus Torvalds return -1; 6638444cf71SThomas Egerer afinfo->init_tempsel(&x->sel, fl); 6648444cf71SThomas Egerer 6658444cf71SThomas Egerer if (family != tmpl->encap_family) { 6668444cf71SThomas Egerer xfrm_state_put_afinfo(afinfo); 6678444cf71SThomas Egerer afinfo = xfrm_state_get_afinfo(tmpl->encap_family); 6688444cf71SThomas Egerer if (!afinfo) 6698444cf71SThomas Egerer return -1; 6708444cf71SThomas Egerer } 6718444cf71SThomas Egerer afinfo->init_temprop(x, tmpl, daddr, saddr); 6721da177e4SLinus Torvalds xfrm_state_put_afinfo(afinfo); 6731da177e4SLinus Torvalds return 0; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6769aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, 6779aa60088SDavid S. Miller const xfrm_address_t *daddr, 6789aa60088SDavid S. Miller __be32 spi, u8 proto, 6799aa60088SDavid S. Miller unsigned short family) 680edcd5821SDavid S. Miller { 681221df1edSAlexey Dobriyan unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family); 682edcd5821SDavid S. Miller struct xfrm_state *x; 6838f126e37SDavid S. Miller struct hlist_node *entry; 684edcd5821SDavid S. Miller 685221df1edSAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) { 686edcd5821SDavid S. Miller if (x->props.family != family || 687edcd5821SDavid S. Miller x->id.spi != spi || 6881802571bSWei Yongjun x->id.proto != proto || 6891802571bSWei Yongjun xfrm_addr_cmp(&x->id.daddr, daddr, family)) 690edcd5821SDavid S. Miller continue; 691edcd5821SDavid S. Miller 6923d6acfa7SJamal Hadi Salim if ((mark & x->mark.m) != x->mark.v) 6933d6acfa7SJamal Hadi Salim continue; 694edcd5821SDavid S. Miller xfrm_state_hold(x); 695edcd5821SDavid S. Miller return x; 696edcd5821SDavid S. Miller } 697edcd5821SDavid S. Miller 698edcd5821SDavid S. Miller return NULL; 699edcd5821SDavid S. Miller } 700edcd5821SDavid S. Miller 7019aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, 7029aa60088SDavid S. Miller const xfrm_address_t *daddr, 7039aa60088SDavid S. Miller const xfrm_address_t *saddr, 7049aa60088SDavid S. Miller u8 proto, unsigned short family) 705edcd5821SDavid S. Miller { 706221df1edSAlexey Dobriyan unsigned int h = xfrm_src_hash(net, daddr, saddr, family); 707edcd5821SDavid S. Miller struct xfrm_state *x; 7088f126e37SDavid S. Miller struct hlist_node *entry; 709edcd5821SDavid S. Miller 710221df1edSAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) { 711edcd5821SDavid S. Miller if (x->props.family != family || 7121802571bSWei Yongjun x->id.proto != proto || 7131802571bSWei Yongjun xfrm_addr_cmp(&x->id.daddr, daddr, family) || 7141802571bSWei Yongjun xfrm_addr_cmp(&x->props.saddr, saddr, family)) 715edcd5821SDavid S. Miller continue; 716edcd5821SDavid S. Miller 7173d6acfa7SJamal Hadi Salim if ((mark & x->mark.m) != x->mark.v) 7183d6acfa7SJamal Hadi Salim continue; 719edcd5821SDavid S. Miller xfrm_state_hold(x); 720edcd5821SDavid S. Miller return x; 721edcd5821SDavid S. Miller } 722edcd5821SDavid S. Miller 723edcd5821SDavid S. Miller return NULL; 724edcd5821SDavid S. Miller } 725edcd5821SDavid S. Miller 726edcd5821SDavid S. Miller static inline struct xfrm_state * 727edcd5821SDavid S. Miller __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family) 728edcd5821SDavid S. Miller { 729221df1edSAlexey Dobriyan struct net *net = xs_net(x); 730bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 731221df1edSAlexey Dobriyan 732edcd5821SDavid S. Miller if (use_spi) 733bd55775cSJamal Hadi Salim return __xfrm_state_lookup(net, mark, &x->id.daddr, 734bd55775cSJamal Hadi Salim x->id.spi, x->id.proto, family); 735edcd5821SDavid S. Miller else 736bd55775cSJamal Hadi Salim return __xfrm_state_lookup_byaddr(net, mark, 737bd55775cSJamal Hadi Salim &x->id.daddr, 738edcd5821SDavid S. Miller &x->props.saddr, 739edcd5821SDavid S. Miller x->id.proto, family); 740edcd5821SDavid S. Miller } 741edcd5821SDavid S. Miller 74298806f75SAlexey Dobriyan static void xfrm_hash_grow_check(struct net *net, int have_hash_collision) 7432fab22f2SPatrick McHardy { 7442fab22f2SPatrick McHardy if (have_hash_collision && 74598806f75SAlexey Dobriyan (net->xfrm.state_hmask + 1) < xfrm_state_hashmax && 74698806f75SAlexey Dobriyan net->xfrm.state_num > net->xfrm.state_hmask) 74798806f75SAlexey Dobriyan schedule_work(&net->xfrm.state_hash_work); 7482fab22f2SPatrick McHardy } 7492fab22f2SPatrick McHardy 75008ec9af1SDavid S. Miller static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x, 7514a08ab0fSDavid S. Miller const struct flowi *fl, unsigned short family, 75208ec9af1SDavid S. Miller struct xfrm_state **best, int *acq_in_progress, 75308ec9af1SDavid S. Miller int *error) 75408ec9af1SDavid S. Miller { 75508ec9af1SDavid S. Miller /* Resolution logic: 75608ec9af1SDavid S. Miller * 1. There is a valid state with matching selector. Done. 75708ec9af1SDavid S. Miller * 2. Valid state with inappropriate selector. Skip. 75808ec9af1SDavid S. Miller * 75908ec9af1SDavid S. Miller * Entering area of "sysdeps". 76008ec9af1SDavid S. Miller * 76108ec9af1SDavid S. Miller * 3. If state is not valid, selector is temporary, it selects 76208ec9af1SDavid S. Miller * only session which triggered previous resolution. Key 76308ec9af1SDavid S. Miller * manager will do something to install a state with proper 76408ec9af1SDavid S. Miller * selector. 76508ec9af1SDavid S. Miller */ 76608ec9af1SDavid S. Miller if (x->km.state == XFRM_STATE_VALID) { 76708ec9af1SDavid S. Miller if ((x->sel.family && 76808ec9af1SDavid S. Miller !xfrm_selector_match(&x->sel, fl, x->sel.family)) || 76908ec9af1SDavid S. Miller !security_xfrm_state_pol_flow_match(x, pol, fl)) 77008ec9af1SDavid S. Miller return; 77108ec9af1SDavid S. Miller 77208ec9af1SDavid S. Miller if (!*best || 77308ec9af1SDavid S. Miller (*best)->km.dying > x->km.dying || 77408ec9af1SDavid S. Miller ((*best)->km.dying == x->km.dying && 77508ec9af1SDavid S. Miller (*best)->curlft.add_time < x->curlft.add_time)) 77608ec9af1SDavid S. Miller *best = x; 77708ec9af1SDavid S. Miller } else if (x->km.state == XFRM_STATE_ACQ) { 77808ec9af1SDavid S. Miller *acq_in_progress = 1; 77908ec9af1SDavid S. Miller } else if (x->km.state == XFRM_STATE_ERROR || 78008ec9af1SDavid S. Miller x->km.state == XFRM_STATE_EXPIRED) { 78108ec9af1SDavid S. Miller if (xfrm_selector_match(&x->sel, fl, x->sel.family) && 78208ec9af1SDavid S. Miller security_xfrm_state_pol_flow_match(x, pol, fl)) 78308ec9af1SDavid S. Miller *error = -ESRCH; 78408ec9af1SDavid S. Miller } 78508ec9af1SDavid S. Miller } 78608ec9af1SDavid S. Miller 7871da177e4SLinus Torvalds struct xfrm_state * 78833765d06SDavid S. Miller xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, 789b520e9f6SDavid S. Miller const struct flowi *fl, struct xfrm_tmpl *tmpl, 7901da177e4SLinus Torvalds struct xfrm_policy *pol, int *err, 7911da177e4SLinus Torvalds unsigned short family) 7921da177e4SLinus Torvalds { 79308ec9af1SDavid S. Miller static xfrm_address_t saddr_wildcard = { }; 7945447c5e4SAlexey Dobriyan struct net *net = xp_net(pol); 7956a783c90SNicolas Dichtel unsigned int h, h_wildcard; 7968f126e37SDavid S. Miller struct hlist_node *entry; 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); 8085447c5e4SAlexey Dobriyan hlist_for_each_entry(x, entry, 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); 8246a783c90SNicolas Dichtel hlist_for_each_entry(x, entry, 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 struct hlist_node *entry; 905628529b6SJamal Hadi Salim 906628529b6SJamal Hadi Salim spin_lock(&xfrm_state_lock); 9075447c5e4SAlexey Dobriyan h = xfrm_dst_hash(net, daddr, saddr, reqid, family); 9085447c5e4SAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) { 909628529b6SJamal Hadi Salim if (x->props.family == family && 910628529b6SJamal Hadi Salim x->props.reqid == reqid && 9113d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 912628529b6SJamal Hadi Salim !(x->props.flags & XFRM_STATE_WILDRECV) && 913628529b6SJamal Hadi Salim xfrm_state_addr_check(x, daddr, saddr, family) && 914628529b6SJamal Hadi Salim mode == x->props.mode && 915628529b6SJamal Hadi Salim proto == x->id.proto && 916628529b6SJamal Hadi Salim x->km.state == XFRM_STATE_VALID) { 917628529b6SJamal Hadi Salim rx = x; 918628529b6SJamal Hadi Salim break; 919628529b6SJamal Hadi Salim } 920628529b6SJamal Hadi Salim } 921628529b6SJamal Hadi Salim 922628529b6SJamal Hadi Salim if (rx) 923628529b6SJamal Hadi Salim xfrm_state_hold(rx); 924628529b6SJamal Hadi Salim spin_unlock(&xfrm_state_lock); 925628529b6SJamal Hadi Salim 926628529b6SJamal Hadi Salim 927628529b6SJamal Hadi Salim return rx; 928628529b6SJamal Hadi Salim } 929628529b6SJamal Hadi Salim EXPORT_SYMBOL(xfrm_stateonly_find); 930628529b6SJamal Hadi Salim 9311da177e4SLinus Torvalds static void __xfrm_state_insert(struct xfrm_state *x) 9321da177e4SLinus Torvalds { 93398806f75SAlexey Dobriyan struct net *net = xs_net(x); 934a624c108SDavid S. Miller unsigned int h; 9351da177e4SLinus Torvalds 93698806f75SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 9374c563f76STimo Teras 93898806f75SAlexey Dobriyan h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr, 939c1969f29SDavid S. Miller x->props.reqid, x->props.family); 94098806f75SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 9411da177e4SLinus Torvalds 94298806f75SAlexey Dobriyan h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family); 94398806f75SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 9446c44e6b7SMasahide NAKAMURA 9457b4dc360SMasahide NAKAMURA if (x->id.spi) { 94698806f75SAlexey Dobriyan h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, 9476c44e6b7SMasahide NAKAMURA x->props.family); 9481da177e4SLinus Torvalds 94998806f75SAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 9506c44e6b7SMasahide NAKAMURA } 9511da177e4SLinus Torvalds 9529e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); 953a47f0ce0SDavid S. Miller if (x->replay_maxage) 954a47f0ce0SDavid S. Miller mod_timer(&x->rtimer, jiffies + x->replay_maxage); 955f8cd5488SJamal Hadi Salim 95698806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 957f034b5d4SDavid S. Miller 95898806f75SAlexey Dobriyan net->xfrm.state_num++; 959f034b5d4SDavid S. Miller 96098806f75SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 963c7f5ea3aSDavid S. Miller /* xfrm_state_lock is held */ 964c7f5ea3aSDavid S. Miller static void __xfrm_state_bump_genids(struct xfrm_state *xnew) 965c7f5ea3aSDavid S. Miller { 96698806f75SAlexey Dobriyan struct net *net = xs_net(xnew); 967c7f5ea3aSDavid S. Miller unsigned short family = xnew->props.family; 968c7f5ea3aSDavid S. Miller u32 reqid = xnew->props.reqid; 969c7f5ea3aSDavid S. Miller struct xfrm_state *x; 970c7f5ea3aSDavid S. Miller struct hlist_node *entry; 971c7f5ea3aSDavid S. Miller unsigned int h; 9723d6acfa7SJamal Hadi Salim u32 mark = xnew->mark.v & xnew->mark.m; 973c7f5ea3aSDavid S. Miller 97498806f75SAlexey Dobriyan h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family); 97598806f75SAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) { 976c7f5ea3aSDavid S. Miller if (x->props.family == family && 977c7f5ea3aSDavid S. Miller x->props.reqid == reqid && 9783d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 979c1969f29SDavid S. Miller !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) && 980c1969f29SDavid S. Miller !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family)) 98134996cb9SHerbert Xu x->genid++; 982c7f5ea3aSDavid S. Miller } 983c7f5ea3aSDavid S. Miller } 984c7f5ea3aSDavid S. Miller 9851da177e4SLinus Torvalds void xfrm_state_insert(struct xfrm_state *x) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 988c7f5ea3aSDavid S. Miller __xfrm_state_bump_genids(x); 9891da177e4SLinus Torvalds __xfrm_state_insert(x); 9901da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_insert); 9931da177e4SLinus Torvalds 9942770834cSDavid S. Miller /* xfrm_state_lock is held */ 995a70486f0SDavid S. Miller static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m, 996a70486f0SDavid S. Miller unsigned short family, u8 mode, 997a70486f0SDavid S. Miller u32 reqid, u8 proto, 998a70486f0SDavid S. Miller const xfrm_address_t *daddr, 999a70486f0SDavid S. Miller const xfrm_address_t *saddr, int create) 10002770834cSDavid S. Miller { 10015447c5e4SAlexey Dobriyan unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family); 10028f126e37SDavid S. Miller struct hlist_node *entry; 10032770834cSDavid S. Miller struct xfrm_state *x; 10043d6acfa7SJamal Hadi Salim u32 mark = m->v & m->m; 10052770834cSDavid S. Miller 10065447c5e4SAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) { 10072770834cSDavid S. Miller if (x->props.reqid != reqid || 10082770834cSDavid S. Miller x->props.mode != mode || 10092770834cSDavid S. Miller x->props.family != family || 10102770834cSDavid S. Miller x->km.state != XFRM_STATE_ACQ || 101175e252d9SJoy Latten x->id.spi != 0 || 10121802571bSWei Yongjun x->id.proto != proto || 10133d6acfa7SJamal Hadi Salim (mark & x->mark.m) != x->mark.v || 10141802571bSWei Yongjun xfrm_addr_cmp(&x->id.daddr, daddr, family) || 10151802571bSWei Yongjun xfrm_addr_cmp(&x->props.saddr, saddr, family)) 10162770834cSDavid S. Miller continue; 10172770834cSDavid S. Miller 10182770834cSDavid S. Miller xfrm_state_hold(x); 10192770834cSDavid S. Miller return x; 10202770834cSDavid S. Miller } 10212770834cSDavid S. Miller 10222770834cSDavid S. Miller if (!create) 10232770834cSDavid S. Miller return NULL; 10242770834cSDavid S. Miller 10255447c5e4SAlexey Dobriyan x = xfrm_state_alloc(net); 10262770834cSDavid S. Miller if (likely(x)) { 10272770834cSDavid S. Miller switch (family) { 10282770834cSDavid S. Miller case AF_INET: 10292770834cSDavid S. Miller x->sel.daddr.a4 = daddr->a4; 10302770834cSDavid S. Miller x->sel.saddr.a4 = saddr->a4; 10312770834cSDavid S. Miller x->sel.prefixlen_d = 32; 10322770834cSDavid S. Miller x->sel.prefixlen_s = 32; 10332770834cSDavid S. Miller x->props.saddr.a4 = saddr->a4; 10342770834cSDavid S. Miller x->id.daddr.a4 = daddr->a4; 10352770834cSDavid S. Miller break; 10362770834cSDavid S. Miller 10372770834cSDavid S. Miller case AF_INET6: 10384e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr; 10394e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr; 10402770834cSDavid S. Miller x->sel.prefixlen_d = 128; 10412770834cSDavid S. Miller x->sel.prefixlen_s = 128; 10424e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr; 10434e3fd7a0SAlexey Dobriyan *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr; 10442770834cSDavid S. Miller break; 10453ff50b79SStephen Hemminger } 10462770834cSDavid S. Miller 10472770834cSDavid S. Miller x->km.state = XFRM_STATE_ACQ; 10482770834cSDavid S. Miller x->id.proto = proto; 10492770834cSDavid S. Miller x->props.family = family; 10502770834cSDavid S. Miller x->props.mode = mode; 10512770834cSDavid S. Miller x->props.reqid = reqid; 1052bd55775cSJamal Hadi Salim x->mark.v = m->v; 1053bd55775cSJamal Hadi Salim x->mark.m = m->m; 1054b27aeadbSAlexey Dobriyan x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; 10552770834cSDavid S. Miller xfrm_state_hold(x); 10569e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL); 10575447c5e4SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 10585447c5e4SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 10595447c5e4SAlexey Dobriyan h = xfrm_src_hash(net, daddr, saddr, family); 10605447c5e4SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 1061918049f0SDavid S. Miller 10625447c5e4SAlexey Dobriyan net->xfrm.state_num++; 1063918049f0SDavid S. Miller 10645447c5e4SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 10652770834cSDavid S. Miller } 10662770834cSDavid S. Miller 10672770834cSDavid S. Miller return x; 10682770834cSDavid S. Miller } 10692770834cSDavid S. Miller 1070bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq); 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds int xfrm_state_add(struct xfrm_state *x) 10731da177e4SLinus Torvalds { 10745447c5e4SAlexey Dobriyan struct net *net = xs_net(x); 107537b08e34SDavid S. Miller struct xfrm_state *x1, *to_put; 10761da177e4SLinus Torvalds int family; 10771da177e4SLinus Torvalds int err; 1078bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 1079eb2971b6SMasahide NAKAMURA int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds family = x->props.family; 10821da177e4SLinus Torvalds 108337b08e34SDavid S. Miller to_put = NULL; 108437b08e34SDavid S. Miller 10851da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 10861da177e4SLinus Torvalds 1087edcd5821SDavid S. Miller x1 = __xfrm_state_locate(x, use_spi, family); 10881da177e4SLinus Torvalds if (x1) { 108937b08e34SDavid S. Miller to_put = x1; 10901da177e4SLinus Torvalds x1 = NULL; 10911da177e4SLinus Torvalds err = -EEXIST; 10921da177e4SLinus Torvalds goto out; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 1095eb2971b6SMasahide NAKAMURA if (use_spi && x->km.seq) { 1096bd55775cSJamal Hadi Salim x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq); 109775e252d9SJoy Latten if (x1 && ((x1->id.proto != x->id.proto) || 109875e252d9SJoy Latten xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) { 109937b08e34SDavid S. Miller to_put = x1; 11001da177e4SLinus Torvalds x1 = NULL; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 1104eb2971b6SMasahide NAKAMURA if (use_spi && !x1) 1105bd55775cSJamal Hadi Salim x1 = __find_acq_core(net, &x->mark, family, x->props.mode, 1106bd55775cSJamal Hadi Salim x->props.reqid, x->id.proto, 11071da177e4SLinus Torvalds &x->id.daddr, &x->props.saddr, 0); 11081da177e4SLinus Torvalds 1109c7f5ea3aSDavid S. Miller __xfrm_state_bump_genids(x); 11101da177e4SLinus Torvalds __xfrm_state_insert(x); 11111da177e4SLinus Torvalds err = 0; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds out: 11141da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds if (x1) { 11171da177e4SLinus Torvalds xfrm_state_delete(x1); 11181da177e4SLinus Torvalds xfrm_state_put(x1); 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 112137b08e34SDavid S. Miller if (to_put) 112237b08e34SDavid S. Miller xfrm_state_put(to_put); 112337b08e34SDavid S. Miller 11241da177e4SLinus Torvalds return err; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_add); 11271da177e4SLinus Torvalds 112880c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 11296666351dSEric Dumazet static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp) 113080c9abaaSShinta Sugimoto { 113198806f75SAlexey Dobriyan struct net *net = xs_net(orig); 113280c9abaaSShinta Sugimoto int err = -ENOMEM; 113398806f75SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 113480c9abaaSShinta Sugimoto if (!x) 1135553f9118SHerbert Xu goto out; 113680c9abaaSShinta Sugimoto 113780c9abaaSShinta Sugimoto memcpy(&x->id, &orig->id, sizeof(x->id)); 113880c9abaaSShinta Sugimoto memcpy(&x->sel, &orig->sel, sizeof(x->sel)); 113980c9abaaSShinta Sugimoto memcpy(&x->lft, &orig->lft, sizeof(x->lft)); 114080c9abaaSShinta Sugimoto x->props.mode = orig->props.mode; 114180c9abaaSShinta Sugimoto x->props.replay_window = orig->props.replay_window; 114280c9abaaSShinta Sugimoto x->props.reqid = orig->props.reqid; 114380c9abaaSShinta Sugimoto x->props.family = orig->props.family; 114480c9abaaSShinta Sugimoto x->props.saddr = orig->props.saddr; 114580c9abaaSShinta Sugimoto 114680c9abaaSShinta Sugimoto if (orig->aalg) { 11474447bb33SMartin Willi x->aalg = xfrm_algo_auth_clone(orig->aalg); 114880c9abaaSShinta Sugimoto if (!x->aalg) 114980c9abaaSShinta Sugimoto goto error; 115080c9abaaSShinta Sugimoto } 115180c9abaaSShinta Sugimoto x->props.aalgo = orig->props.aalgo; 115280c9abaaSShinta Sugimoto 115380c9abaaSShinta Sugimoto if (orig->ealg) { 115480c9abaaSShinta Sugimoto x->ealg = xfrm_algo_clone(orig->ealg); 115580c9abaaSShinta Sugimoto if (!x->ealg) 115680c9abaaSShinta Sugimoto goto error; 115780c9abaaSShinta Sugimoto } 115880c9abaaSShinta Sugimoto x->props.ealgo = orig->props.ealgo; 115980c9abaaSShinta Sugimoto 116080c9abaaSShinta Sugimoto if (orig->calg) { 116180c9abaaSShinta Sugimoto x->calg = xfrm_algo_clone(orig->calg); 116280c9abaaSShinta Sugimoto if (!x->calg) 116380c9abaaSShinta Sugimoto goto error; 116480c9abaaSShinta Sugimoto } 116580c9abaaSShinta Sugimoto x->props.calgo = orig->props.calgo; 116680c9abaaSShinta Sugimoto 116780c9abaaSShinta Sugimoto if (orig->encap) { 116880c9abaaSShinta Sugimoto x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL); 116980c9abaaSShinta Sugimoto if (!x->encap) 117080c9abaaSShinta Sugimoto goto error; 117180c9abaaSShinta Sugimoto } 117280c9abaaSShinta Sugimoto 117380c9abaaSShinta Sugimoto if (orig->coaddr) { 117480c9abaaSShinta Sugimoto x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr), 117580c9abaaSShinta Sugimoto GFP_KERNEL); 117680c9abaaSShinta Sugimoto if (!x->coaddr) 117780c9abaaSShinta Sugimoto goto error; 117880c9abaaSShinta Sugimoto } 117980c9abaaSShinta Sugimoto 1180af2f464eSSteffen Klassert if (orig->replay_esn) { 1181af2f464eSSteffen Klassert err = xfrm_replay_clone(x, orig); 1182af2f464eSSteffen Klassert if (err) 1183af2f464eSSteffen Klassert goto error; 1184af2f464eSSteffen Klassert } 1185af2f464eSSteffen Klassert 1186bd55775cSJamal Hadi Salim memcpy(&x->mark, &orig->mark, sizeof(x->mark)); 1187bd55775cSJamal Hadi Salim 118880c9abaaSShinta Sugimoto err = xfrm_init_state(x); 118980c9abaaSShinta Sugimoto if (err) 119080c9abaaSShinta Sugimoto goto error; 119180c9abaaSShinta Sugimoto 119280c9abaaSShinta Sugimoto x->props.flags = orig->props.flags; 119380c9abaaSShinta Sugimoto 119480c9abaaSShinta Sugimoto x->curlft.add_time = orig->curlft.add_time; 119580c9abaaSShinta Sugimoto x->km.state = orig->km.state; 119680c9abaaSShinta Sugimoto x->km.seq = orig->km.seq; 119780c9abaaSShinta Sugimoto 119880c9abaaSShinta Sugimoto return x; 119980c9abaaSShinta Sugimoto 120080c9abaaSShinta Sugimoto error: 1201553f9118SHerbert Xu xfrm_state_put(x); 1202553f9118SHerbert Xu out: 120380c9abaaSShinta Sugimoto if (errp) 120480c9abaaSShinta Sugimoto *errp = err; 120580c9abaaSShinta Sugimoto return NULL; 120680c9abaaSShinta Sugimoto } 120780c9abaaSShinta Sugimoto 120880c9abaaSShinta Sugimoto /* xfrm_state_lock is held */ 120980c9abaaSShinta Sugimoto struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m) 121080c9abaaSShinta Sugimoto { 121180c9abaaSShinta Sugimoto unsigned int h; 121280c9abaaSShinta Sugimoto struct xfrm_state *x; 121380c9abaaSShinta Sugimoto struct hlist_node *entry; 121480c9abaaSShinta Sugimoto 121580c9abaaSShinta Sugimoto if (m->reqid) { 121664d0cd00SAlexey Dobriyan h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr, 121780c9abaaSShinta Sugimoto m->reqid, m->old_family); 121873d189dcSAlexey Dobriyan hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) { 121980c9abaaSShinta Sugimoto if (x->props.mode != m->mode || 122080c9abaaSShinta Sugimoto x->id.proto != m->proto) 122180c9abaaSShinta Sugimoto continue; 122280c9abaaSShinta Sugimoto if (m->reqid && x->props.reqid != m->reqid) 122380c9abaaSShinta Sugimoto continue; 122480c9abaaSShinta Sugimoto if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr, 122580c9abaaSShinta Sugimoto m->old_family) || 122680c9abaaSShinta Sugimoto xfrm_addr_cmp(&x->props.saddr, &m->old_saddr, 122780c9abaaSShinta Sugimoto m->old_family)) 122880c9abaaSShinta Sugimoto continue; 122980c9abaaSShinta Sugimoto xfrm_state_hold(x); 123080c9abaaSShinta Sugimoto return x; 123180c9abaaSShinta Sugimoto } 123280c9abaaSShinta Sugimoto } else { 123364d0cd00SAlexey Dobriyan h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr, 123480c9abaaSShinta Sugimoto m->old_family); 1235d320bbb3SAlexey Dobriyan hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) { 123680c9abaaSShinta Sugimoto if (x->props.mode != m->mode || 123780c9abaaSShinta Sugimoto x->id.proto != m->proto) 123880c9abaaSShinta Sugimoto continue; 123980c9abaaSShinta Sugimoto if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr, 124080c9abaaSShinta Sugimoto m->old_family) || 124180c9abaaSShinta Sugimoto xfrm_addr_cmp(&x->props.saddr, &m->old_saddr, 124280c9abaaSShinta Sugimoto m->old_family)) 124380c9abaaSShinta Sugimoto continue; 124480c9abaaSShinta Sugimoto xfrm_state_hold(x); 124580c9abaaSShinta Sugimoto return x; 124680c9abaaSShinta Sugimoto } 124780c9abaaSShinta Sugimoto } 124880c9abaaSShinta Sugimoto 124980c9abaaSShinta Sugimoto return NULL; 125080c9abaaSShinta Sugimoto } 125180c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_migrate_state_find); 125280c9abaaSShinta Sugimoto 125380c9abaaSShinta Sugimoto struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x, 125480c9abaaSShinta Sugimoto struct xfrm_migrate *m) 125580c9abaaSShinta Sugimoto { 125680c9abaaSShinta Sugimoto struct xfrm_state *xc; 125780c9abaaSShinta Sugimoto int err; 125880c9abaaSShinta Sugimoto 125980c9abaaSShinta Sugimoto xc = xfrm_state_clone(x, &err); 126080c9abaaSShinta Sugimoto if (!xc) 126180c9abaaSShinta Sugimoto return NULL; 126280c9abaaSShinta Sugimoto 126380c9abaaSShinta Sugimoto memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr)); 126480c9abaaSShinta Sugimoto memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr)); 126580c9abaaSShinta Sugimoto 126680c9abaaSShinta Sugimoto /* add state */ 126780c9abaaSShinta Sugimoto if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) { 126880c9abaaSShinta Sugimoto /* a care is needed when the destination address of the 126980c9abaaSShinta Sugimoto state is to be updated as it is a part of triplet */ 127080c9abaaSShinta Sugimoto xfrm_state_insert(xc); 127180c9abaaSShinta Sugimoto } else { 127280c9abaaSShinta Sugimoto if ((err = xfrm_state_add(xc)) < 0) 127380c9abaaSShinta Sugimoto goto error; 127480c9abaaSShinta Sugimoto } 127580c9abaaSShinta Sugimoto 127680c9abaaSShinta Sugimoto return xc; 127780c9abaaSShinta Sugimoto error: 127878347c8cSThomas Egerer xfrm_state_put(xc); 127980c9abaaSShinta Sugimoto return NULL; 128080c9abaaSShinta Sugimoto } 128180c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_state_migrate); 128280c9abaaSShinta Sugimoto #endif 128380c9abaaSShinta Sugimoto 12841da177e4SLinus Torvalds int xfrm_state_update(struct xfrm_state *x) 12851da177e4SLinus Torvalds { 128637b08e34SDavid S. Miller struct xfrm_state *x1, *to_put; 12871da177e4SLinus Torvalds int err; 1288eb2971b6SMasahide NAKAMURA int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); 12891da177e4SLinus Torvalds 129037b08e34SDavid S. Miller to_put = NULL; 129137b08e34SDavid S. Miller 12921da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1293edcd5821SDavid S. Miller x1 = __xfrm_state_locate(x, use_spi, x->props.family); 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds err = -ESRCH; 12961da177e4SLinus Torvalds if (!x1) 12971da177e4SLinus Torvalds goto out; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds if (xfrm_state_kern(x1)) { 130037b08e34SDavid S. Miller to_put = x1; 13011da177e4SLinus Torvalds err = -EEXIST; 13021da177e4SLinus Torvalds goto out; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds if (x1->km.state == XFRM_STATE_ACQ) { 13061da177e4SLinus Torvalds __xfrm_state_insert(x); 13071da177e4SLinus Torvalds x = NULL; 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds err = 0; 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds out: 13121da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 13131da177e4SLinus Torvalds 131437b08e34SDavid S. Miller if (to_put) 131537b08e34SDavid S. Miller xfrm_state_put(to_put); 131637b08e34SDavid S. Miller 13171da177e4SLinus Torvalds if (err) 13181da177e4SLinus Torvalds return err; 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds if (!x) { 13211da177e4SLinus Torvalds xfrm_state_delete(x1); 13221da177e4SLinus Torvalds xfrm_state_put(x1); 13231da177e4SLinus Torvalds return 0; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds err = -EINVAL; 13271da177e4SLinus Torvalds spin_lock_bh(&x1->lock); 13281da177e4SLinus Torvalds if (likely(x1->km.state == XFRM_STATE_VALID)) { 13291da177e4SLinus Torvalds if (x->encap && x1->encap) 13301da177e4SLinus Torvalds memcpy(x1->encap, x->encap, sizeof(*x1->encap)); 1331060f02a3SNoriaki TAKAMIYA if (x->coaddr && x1->coaddr) { 1332060f02a3SNoriaki TAKAMIYA memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr)); 1333060f02a3SNoriaki TAKAMIYA } 1334060f02a3SNoriaki TAKAMIYA if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel))) 1335060f02a3SNoriaki TAKAMIYA memcpy(&x1->sel, &x->sel, sizeof(x1->sel)); 13361da177e4SLinus Torvalds memcpy(&x1->lft, &x->lft, sizeof(x1->lft)); 13371da177e4SLinus Torvalds x1->km.dying = 0; 13381da177e4SLinus Torvalds 13399e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); 13401da177e4SLinus Torvalds if (x1->curlft.use_time) 13411da177e4SLinus Torvalds xfrm_state_check_expire(x1); 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds err = 0; 13448fcbc637STushar Gohad x->km.state = XFRM_STATE_DEAD; 13458fcbc637STushar Gohad __xfrm_state_put(x); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds spin_unlock_bh(&x1->lock); 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds xfrm_state_put(x1); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds return err; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_update); 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds int xfrm_state_check_expire(struct xfrm_state *x) 13561da177e4SLinus Torvalds { 13571da177e4SLinus Torvalds if (!x->curlft.use_time) 13589d729f72SJames Morris x->curlft.use_time = get_seconds(); 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds if (x->km.state != XFRM_STATE_VALID) 13611da177e4SLinus Torvalds return -EINVAL; 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds if (x->curlft.bytes >= x->lft.hard_byte_limit || 13641da177e4SLinus Torvalds x->curlft.packets >= x->lft.hard_packet_limit) { 13654666faabSHerbert Xu x->km.state = XFRM_STATE_EXPIRED; 13669e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL); 13671da177e4SLinus Torvalds return -EINVAL; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds if (!x->km.dying && 13711da177e4SLinus Torvalds (x->curlft.bytes >= x->lft.soft_byte_limit || 13724666faabSHerbert Xu x->curlft.packets >= x->lft.soft_packet_limit)) { 13734666faabSHerbert Xu x->km.dying = 1; 137453bc6b4dSJamal Hadi Salim km_state_expired(x, 0, 0); 13754666faabSHerbert Xu } 13761da177e4SLinus Torvalds return 0; 13771da177e4SLinus Torvalds } 13781da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_check_expire); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds struct xfrm_state * 1381a70486f0SDavid S. Miller xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi, 1382bd55775cSJamal Hadi Salim u8 proto, unsigned short family) 13831da177e4SLinus Torvalds { 13841da177e4SLinus Torvalds struct xfrm_state *x; 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1387bd55775cSJamal Hadi Salim x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family); 13881da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 13891da177e4SLinus Torvalds return x; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_lookup); 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds struct xfrm_state * 1394bd55775cSJamal Hadi Salim xfrm_state_lookup_byaddr(struct net *net, u32 mark, 1395a70486f0SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 1396eb2971b6SMasahide NAKAMURA u8 proto, unsigned short family) 1397eb2971b6SMasahide NAKAMURA { 1398eb2971b6SMasahide NAKAMURA struct xfrm_state *x; 1399eb2971b6SMasahide NAKAMURA 1400eb2971b6SMasahide NAKAMURA spin_lock_bh(&xfrm_state_lock); 1401bd55775cSJamal Hadi Salim x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family); 1402eb2971b6SMasahide NAKAMURA spin_unlock_bh(&xfrm_state_lock); 1403eb2971b6SMasahide NAKAMURA return x; 1404eb2971b6SMasahide NAKAMURA } 1405eb2971b6SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_lookup_byaddr); 1406eb2971b6SMasahide NAKAMURA 1407eb2971b6SMasahide NAKAMURA struct xfrm_state * 1408bd55775cSJamal Hadi Salim xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto, 1409a70486f0SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 14101da177e4SLinus Torvalds int create, unsigned short family) 14111da177e4SLinus Torvalds { 14121da177e4SLinus Torvalds struct xfrm_state *x; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1415bd55775cSJamal Hadi Salim x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create); 14161da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 14172770834cSDavid S. Miller 14181da177e4SLinus Torvalds return x; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq); 14211da177e4SLinus Torvalds 142241a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 142341a49cc3SMasahide NAKAMURA int 142441a49cc3SMasahide NAKAMURA xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n, 142541a49cc3SMasahide NAKAMURA unsigned short family) 142641a49cc3SMasahide NAKAMURA { 142741a49cc3SMasahide NAKAMURA int err = 0; 142841a49cc3SMasahide NAKAMURA struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 142941a49cc3SMasahide NAKAMURA if (!afinfo) 143041a49cc3SMasahide NAKAMURA return -EAFNOSUPPORT; 143141a49cc3SMasahide NAKAMURA 143241a49cc3SMasahide NAKAMURA spin_lock_bh(&xfrm_state_lock); 143341a49cc3SMasahide NAKAMURA if (afinfo->tmpl_sort) 143441a49cc3SMasahide NAKAMURA err = afinfo->tmpl_sort(dst, src, n); 143541a49cc3SMasahide NAKAMURA spin_unlock_bh(&xfrm_state_lock); 143641a49cc3SMasahide NAKAMURA xfrm_state_put_afinfo(afinfo); 143741a49cc3SMasahide NAKAMURA return err; 143841a49cc3SMasahide NAKAMURA } 143941a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_tmpl_sort); 144041a49cc3SMasahide NAKAMURA 144141a49cc3SMasahide NAKAMURA int 144241a49cc3SMasahide NAKAMURA xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n, 144341a49cc3SMasahide NAKAMURA unsigned short family) 144441a49cc3SMasahide NAKAMURA { 144541a49cc3SMasahide NAKAMURA int err = 0; 144641a49cc3SMasahide NAKAMURA struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 144741a49cc3SMasahide NAKAMURA if (!afinfo) 144841a49cc3SMasahide NAKAMURA return -EAFNOSUPPORT; 144941a49cc3SMasahide NAKAMURA 145041a49cc3SMasahide NAKAMURA spin_lock_bh(&xfrm_state_lock); 145141a49cc3SMasahide NAKAMURA if (afinfo->state_sort) 145241a49cc3SMasahide NAKAMURA err = afinfo->state_sort(dst, src, n); 145341a49cc3SMasahide NAKAMURA spin_unlock_bh(&xfrm_state_lock); 145441a49cc3SMasahide NAKAMURA xfrm_state_put_afinfo(afinfo); 145541a49cc3SMasahide NAKAMURA return err; 145641a49cc3SMasahide NAKAMURA } 145741a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_sort); 145841a49cc3SMasahide NAKAMURA #endif 145941a49cc3SMasahide NAKAMURA 14601da177e4SLinus Torvalds /* Silly enough, but I'm lazy to build resolution list */ 14611da177e4SLinus Torvalds 1462bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) 14631da177e4SLinus Torvalds { 14641da177e4SLinus Torvalds int i; 14651da177e4SLinus Torvalds 14665447c5e4SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 14678f126e37SDavid S. Miller struct hlist_node *entry; 14688f126e37SDavid S. Miller struct xfrm_state *x; 14698f126e37SDavid S. Miller 14705447c5e4SAlexey Dobriyan hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) { 14718f126e37SDavid S. Miller if (x->km.seq == seq && 14723d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 14738f126e37SDavid S. Miller x->km.state == XFRM_STATE_ACQ) { 14741da177e4SLinus Torvalds xfrm_state_hold(x); 14751da177e4SLinus Torvalds return x; 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds return NULL; 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds 1482bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) 14831da177e4SLinus Torvalds { 14841da177e4SLinus Torvalds struct xfrm_state *x; 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 1487bd55775cSJamal Hadi Salim x = __xfrm_find_acq_byseq(net, mark, seq); 14881da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 14891da177e4SLinus Torvalds return x; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq_byseq); 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds u32 xfrm_get_acqseq(void) 14941da177e4SLinus Torvalds { 14951da177e4SLinus Torvalds u32 res; 14966836b9bdSjamal static atomic_t acqseq; 14971da177e4SLinus Torvalds 14986836b9bdSjamal do { 14996836b9bdSjamal res = atomic_inc_return(&acqseq); 15006836b9bdSjamal } while (!res); 15016836b9bdSjamal 15021da177e4SLinus Torvalds return res; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_get_acqseq); 15051da177e4SLinus Torvalds 1506658b219eSHerbert Xu int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) 15071da177e4SLinus Torvalds { 1508221df1edSAlexey Dobriyan struct net *net = xs_net(x); 1509f034b5d4SDavid S. Miller unsigned int h; 15101da177e4SLinus Torvalds struct xfrm_state *x0; 1511658b219eSHerbert Xu int err = -ENOENT; 1512658b219eSHerbert Xu __be32 minspi = htonl(low); 1513658b219eSHerbert Xu __be32 maxspi = htonl(high); 1514bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 15151da177e4SLinus Torvalds 1516658b219eSHerbert Xu spin_lock_bh(&x->lock); 1517658b219eSHerbert Xu if (x->km.state == XFRM_STATE_DEAD) 1518658b219eSHerbert Xu goto unlock; 1519658b219eSHerbert Xu 1520658b219eSHerbert Xu err = 0; 15211da177e4SLinus Torvalds if (x->id.spi) 1522658b219eSHerbert Xu goto unlock; 1523658b219eSHerbert Xu 1524658b219eSHerbert Xu err = -ENOENT; 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds if (minspi == maxspi) { 1527bd55775cSJamal Hadi Salim x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family); 15281da177e4SLinus Torvalds if (x0) { 15291da177e4SLinus Torvalds xfrm_state_put(x0); 1530658b219eSHerbert Xu goto unlock; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds x->id.spi = minspi; 15331da177e4SLinus Torvalds } else { 15341da177e4SLinus Torvalds u32 spi = 0; 153526977b4eSAl Viro for (h=0; h<high-low+1; h++) { 153626977b4eSAl Viro spi = low + net_random()%(high-low+1); 1537bd55775cSJamal Hadi Salim x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family); 15381da177e4SLinus Torvalds if (x0 == NULL) { 15391da177e4SLinus Torvalds x->id.spi = htonl(spi); 15401da177e4SLinus Torvalds break; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds xfrm_state_put(x0); 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds if (x->id.spi) { 15461da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 154712604d8aSAlexey Dobriyan h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); 154812604d8aSAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 15491da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 1550658b219eSHerbert Xu 1551658b219eSHerbert Xu err = 0; 15521da177e4SLinus Torvalds } 1553658b219eSHerbert Xu 1554658b219eSHerbert Xu unlock: 1555658b219eSHerbert Xu spin_unlock_bh(&x->lock); 1556658b219eSHerbert Xu 1557658b219eSHerbert Xu return err; 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_alloc_spi); 15601da177e4SLinus Torvalds 1561284fa7daSAlexey Dobriyan int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, 15624c563f76STimo Teras int (*func)(struct xfrm_state *, int, void*), 15631da177e4SLinus Torvalds void *data) 15641da177e4SLinus Torvalds { 156512a169e7SHerbert Xu struct xfrm_state *state; 156612a169e7SHerbert Xu struct xfrm_state_walk *x; 15671da177e4SLinus Torvalds int err = 0; 15681da177e4SLinus Torvalds 156912a169e7SHerbert Xu if (walk->seq != 0 && list_empty(&walk->all)) 15704c563f76STimo Teras return 0; 15714c563f76STimo Teras 15721da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_lock); 157312a169e7SHerbert Xu if (list_empty(&walk->all)) 1574284fa7daSAlexey Dobriyan x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all); 157512a169e7SHerbert Xu else 157612a169e7SHerbert Xu x = list_entry(&walk->all, struct xfrm_state_walk, all); 1577284fa7daSAlexey Dobriyan list_for_each_entry_from(x, &net->xfrm.state_all, all) { 157812a169e7SHerbert Xu if (x->state == XFRM_STATE_DEAD) 15794c563f76STimo Teras continue; 158012a169e7SHerbert Xu state = container_of(x, struct xfrm_state, km); 158112a169e7SHerbert Xu if (!xfrm_id_proto_match(state->id.proto, walk->proto)) 158294b9bb54SJamal Hadi Salim continue; 158312a169e7SHerbert Xu err = func(state, walk->seq, data); 15844c563f76STimo Teras if (err) { 158512a169e7SHerbert Xu list_move_tail(&walk->all, &x->all); 158694b9bb54SJamal Hadi Salim goto out; 158794b9bb54SJamal Hadi Salim } 158812a169e7SHerbert Xu walk->seq++; 15894c563f76STimo Teras } 159012a169e7SHerbert Xu if (walk->seq == 0) { 15911da177e4SLinus Torvalds err = -ENOENT; 15921da177e4SLinus Torvalds goto out; 15931da177e4SLinus Torvalds } 159412a169e7SHerbert Xu list_del_init(&walk->all); 15951da177e4SLinus Torvalds out: 15961da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_lock); 15971da177e4SLinus Torvalds return err; 15981da177e4SLinus Torvalds } 15991da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_walk); 16001da177e4SLinus Torvalds 16015c182458SHerbert Xu void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto) 16025c182458SHerbert Xu { 160312a169e7SHerbert Xu INIT_LIST_HEAD(&walk->all); 16045c182458SHerbert Xu walk->proto = proto; 160512a169e7SHerbert Xu walk->state = XFRM_STATE_DEAD; 160612a169e7SHerbert Xu walk->seq = 0; 16075c182458SHerbert Xu } 16085c182458SHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_init); 16095c182458SHerbert Xu 1610abb81c4fSHerbert Xu void xfrm_state_walk_done(struct xfrm_state_walk *walk) 1611abb81c4fSHerbert Xu { 161212a169e7SHerbert Xu if (list_empty(&walk->all)) 16135c182458SHerbert Xu return; 16145c182458SHerbert Xu 161512a169e7SHerbert Xu spin_lock_bh(&xfrm_state_lock); 161612a169e7SHerbert Xu list_del(&walk->all); 16177d0b591cSChuck Ebbert spin_unlock_bh(&xfrm_state_lock); 1618abb81c4fSHerbert Xu } 1619abb81c4fSHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_done); 1620abb81c4fSHerbert Xu 1621f8cd5488SJamal Hadi Salim static void xfrm_replay_timer_handler(unsigned long data) 1622f8cd5488SJamal Hadi Salim { 1623f8cd5488SJamal Hadi Salim struct xfrm_state *x = (struct xfrm_state*)data; 1624f8cd5488SJamal Hadi Salim 1625f8cd5488SJamal Hadi Salim spin_lock(&x->lock); 1626f8cd5488SJamal Hadi Salim 16272717096aSJamal Hadi Salim if (x->km.state == XFRM_STATE_VALID) { 1628a6483b79SAlexey Dobriyan if (xfrm_aevent_is_on(xs_net(x))) 16299fdc4883SSteffen Klassert x->repl->notify(x, XFRM_REPLAY_TIMEOUT); 16302717096aSJamal Hadi Salim else 16312717096aSJamal Hadi Salim x->xflags |= XFRM_TIME_DEFER; 16322717096aSJamal Hadi Salim } 1633f8cd5488SJamal Hadi Salim 1634f8cd5488SJamal Hadi Salim spin_unlock(&x->lock); 1635f8cd5488SJamal Hadi Salim } 1636f8cd5488SJamal Hadi Salim 1637df01812eSDenis Cheng static LIST_HEAD(xfrm_km_list); 16381da177e4SLinus Torvalds static DEFINE_RWLOCK(xfrm_km_lock); 16391da177e4SLinus Torvalds 1640214e005bSDavid S. Miller void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 16411da177e4SLinus Torvalds { 16421da177e4SLinus Torvalds struct xfrm_mgr *km; 16431da177e4SLinus Torvalds 164426b15dadSJamal Hadi Salim read_lock(&xfrm_km_lock); 164526b15dadSJamal Hadi Salim list_for_each_entry(km, &xfrm_km_list, list) 164626b15dadSJamal Hadi Salim if (km->notify_policy) 164726b15dadSJamal Hadi Salim km->notify_policy(xp, dir, c); 164826b15dadSJamal Hadi Salim read_unlock(&xfrm_km_lock); 164926b15dadSJamal Hadi Salim } 165026b15dadSJamal Hadi Salim 1651214e005bSDavid S. Miller void km_state_notify(struct xfrm_state *x, const struct km_event *c) 165226b15dadSJamal Hadi Salim { 165326b15dadSJamal Hadi Salim struct xfrm_mgr *km; 165426b15dadSJamal Hadi Salim read_lock(&xfrm_km_lock); 165526b15dadSJamal Hadi Salim list_for_each_entry(km, &xfrm_km_list, list) 165626b15dadSJamal Hadi Salim if (km->notify) 165726b15dadSJamal Hadi Salim km->notify(x, c); 165826b15dadSJamal Hadi Salim read_unlock(&xfrm_km_lock); 165926b15dadSJamal Hadi Salim } 166026b15dadSJamal Hadi Salim 166126b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_policy_notify); 166226b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_state_notify); 166326b15dadSJamal Hadi Salim 166453bc6b4dSJamal Hadi Salim void km_state_expired(struct xfrm_state *x, int hard, u32 pid) 166526b15dadSJamal Hadi Salim { 166698806f75SAlexey Dobriyan struct net *net = xs_net(x); 166726b15dadSJamal Hadi Salim struct km_event c; 166826b15dadSJamal Hadi Salim 1669bf08867fSHerbert Xu c.data.hard = hard; 167053bc6b4dSJamal Hadi Salim c.pid = pid; 1671f60f6b8fSHerbert Xu c.event = XFRM_MSG_EXPIRE; 167226b15dadSJamal Hadi Salim km_state_notify(x, &c); 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds if (hard) 167598806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 167853bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(km_state_expired); 167926b15dadSJamal Hadi Salim /* 168026b15dadSJamal Hadi Salim * We send to all registered managers regardless of failure 168126b15dadSJamal Hadi Salim * We are happy with one success 168226b15dadSJamal Hadi Salim */ 1683980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol) 16841da177e4SLinus Torvalds { 168526b15dadSJamal Hadi Salim int err = -EINVAL, acqret; 16861da177e4SLinus Torvalds struct xfrm_mgr *km; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds read_lock(&xfrm_km_lock); 16891da177e4SLinus Torvalds list_for_each_entry(km, &xfrm_km_list, list) { 169026b15dadSJamal Hadi Salim acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT); 169126b15dadSJamal Hadi Salim if (!acqret) 169226b15dadSJamal Hadi Salim err = acqret; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds read_unlock(&xfrm_km_lock); 16951da177e4SLinus Torvalds return err; 16961da177e4SLinus Torvalds } 1697980ebd25SJamal Hadi Salim EXPORT_SYMBOL(km_query); 16981da177e4SLinus Torvalds 16995d36b180SAl Viro int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) 17001da177e4SLinus Torvalds { 17011da177e4SLinus Torvalds int err = -EINVAL; 17021da177e4SLinus Torvalds struct xfrm_mgr *km; 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds read_lock(&xfrm_km_lock); 17051da177e4SLinus Torvalds list_for_each_entry(km, &xfrm_km_list, list) { 17061da177e4SLinus Torvalds if (km->new_mapping) 17071da177e4SLinus Torvalds err = km->new_mapping(x, ipaddr, sport); 17081da177e4SLinus Torvalds if (!err) 17091da177e4SLinus Torvalds break; 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds read_unlock(&xfrm_km_lock); 17121da177e4SLinus Torvalds return err; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds EXPORT_SYMBOL(km_new_mapping); 17151da177e4SLinus Torvalds 17166c5c8ca7SJamal Hadi Salim void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid) 17171da177e4SLinus Torvalds { 171898806f75SAlexey Dobriyan struct net *net = xp_net(pol); 171926b15dadSJamal Hadi Salim struct km_event c; 17201da177e4SLinus Torvalds 1721bf08867fSHerbert Xu c.data.hard = hard; 17226c5c8ca7SJamal Hadi Salim c.pid = pid; 1723f60f6b8fSHerbert Xu c.event = XFRM_MSG_POLEXPIRE; 172426b15dadSJamal Hadi Salim km_policy_notify(pol, dir, &c); 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds if (hard) 172798806f75SAlexey Dobriyan wake_up(&net->xfrm.km_waitq); 17281da177e4SLinus Torvalds } 1729a70fcb0bSDavid S. Miller EXPORT_SYMBOL(km_policy_expired); 17301da177e4SLinus Torvalds 17312d60abc2SEric Dumazet #ifdef CONFIG_XFRM_MIGRATE 1732183cad12SDavid S. Miller int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 1733183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 1734183cad12SDavid S. Miller const struct xfrm_kmaddress *k) 173580c9abaaSShinta Sugimoto { 173680c9abaaSShinta Sugimoto int err = -EINVAL; 173780c9abaaSShinta Sugimoto int ret; 173880c9abaaSShinta Sugimoto struct xfrm_mgr *km; 173980c9abaaSShinta Sugimoto 174080c9abaaSShinta Sugimoto read_lock(&xfrm_km_lock); 174180c9abaaSShinta Sugimoto list_for_each_entry(km, &xfrm_km_list, list) { 174280c9abaaSShinta Sugimoto if (km->migrate) { 174313c1d189SArnaud Ebalard ret = km->migrate(sel, dir, type, m, num_migrate, k); 174480c9abaaSShinta Sugimoto if (!ret) 174580c9abaaSShinta Sugimoto err = ret; 174680c9abaaSShinta Sugimoto } 174780c9abaaSShinta Sugimoto } 174880c9abaaSShinta Sugimoto read_unlock(&xfrm_km_lock); 174980c9abaaSShinta Sugimoto return err; 175080c9abaaSShinta Sugimoto } 175180c9abaaSShinta Sugimoto EXPORT_SYMBOL(km_migrate); 17522d60abc2SEric Dumazet #endif 175380c9abaaSShinta Sugimoto 1754db983c11SAlexey Dobriyan int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr) 175597a64b45SMasahide NAKAMURA { 175697a64b45SMasahide NAKAMURA int err = -EINVAL; 175797a64b45SMasahide NAKAMURA int ret; 175897a64b45SMasahide NAKAMURA struct xfrm_mgr *km; 175997a64b45SMasahide NAKAMURA 176097a64b45SMasahide NAKAMURA read_lock(&xfrm_km_lock); 176197a64b45SMasahide NAKAMURA list_for_each_entry(km, &xfrm_km_list, list) { 176297a64b45SMasahide NAKAMURA if (km->report) { 1763db983c11SAlexey Dobriyan ret = km->report(net, proto, sel, addr); 176497a64b45SMasahide NAKAMURA if (!ret) 176597a64b45SMasahide NAKAMURA err = ret; 176697a64b45SMasahide NAKAMURA } 176797a64b45SMasahide NAKAMURA } 176897a64b45SMasahide NAKAMURA read_unlock(&xfrm_km_lock); 176997a64b45SMasahide NAKAMURA return err; 177097a64b45SMasahide NAKAMURA } 177197a64b45SMasahide NAKAMURA EXPORT_SYMBOL(km_report); 177297a64b45SMasahide NAKAMURA 17731da177e4SLinus Torvalds int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) 17741da177e4SLinus Torvalds { 17751da177e4SLinus Torvalds int err; 17761da177e4SLinus Torvalds u8 *data; 17771da177e4SLinus Torvalds struct xfrm_mgr *km; 17781da177e4SLinus Torvalds struct xfrm_policy *pol = NULL; 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds if (optlen <= 0 || optlen > PAGE_SIZE) 17811da177e4SLinus Torvalds return -EMSGSIZE; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds data = kmalloc(optlen, GFP_KERNEL); 17841da177e4SLinus Torvalds if (!data) 17851da177e4SLinus Torvalds return -ENOMEM; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds err = -EFAULT; 17881da177e4SLinus Torvalds if (copy_from_user(data, optval, optlen)) 17891da177e4SLinus Torvalds goto out; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds err = -EINVAL; 17921da177e4SLinus Torvalds read_lock(&xfrm_km_lock); 17931da177e4SLinus Torvalds list_for_each_entry(km, &xfrm_km_list, list) { 1794cb969f07SVenkat Yekkirala pol = km->compile_policy(sk, optname, data, 17951da177e4SLinus Torvalds optlen, &err); 17961da177e4SLinus Torvalds if (err >= 0) 17971da177e4SLinus Torvalds break; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds read_unlock(&xfrm_km_lock); 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds if (err >= 0) { 18021da177e4SLinus Torvalds xfrm_sk_policy_insert(sk, err, pol); 18031da177e4SLinus Torvalds xfrm_pol_put(pol); 18041da177e4SLinus Torvalds err = 0; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds out: 18081da177e4SLinus Torvalds kfree(data); 18091da177e4SLinus Torvalds return err; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_user_policy); 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds int xfrm_register_km(struct xfrm_mgr *km) 18141da177e4SLinus Torvalds { 18151da177e4SLinus Torvalds write_lock_bh(&xfrm_km_lock); 18161da177e4SLinus Torvalds list_add_tail(&km->list, &xfrm_km_list); 18171da177e4SLinus Torvalds write_unlock_bh(&xfrm_km_lock); 18181da177e4SLinus Torvalds return 0; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_register_km); 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds int xfrm_unregister_km(struct xfrm_mgr *km) 18231da177e4SLinus Torvalds { 18241da177e4SLinus Torvalds write_lock_bh(&xfrm_km_lock); 18251da177e4SLinus Torvalds list_del(&km->list); 18261da177e4SLinus Torvalds write_unlock_bh(&xfrm_km_lock); 18271da177e4SLinus Torvalds return 0; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_unregister_km); 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds int err = 0; 18341da177e4SLinus Torvalds if (unlikely(afinfo == NULL)) 18351da177e4SLinus Torvalds return -EINVAL; 18361da177e4SLinus Torvalds if (unlikely(afinfo->family >= NPROTO)) 18371da177e4SLinus Torvalds return -EAFNOSUPPORT; 1838f3111502SIngo Molnar write_lock_bh(&xfrm_state_afinfo_lock); 18391da177e4SLinus Torvalds if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL)) 18401da177e4SLinus Torvalds err = -ENOBUFS; 1841edcd5821SDavid S. Miller else 18421da177e4SLinus Torvalds xfrm_state_afinfo[afinfo->family] = afinfo; 1843f3111502SIngo Molnar write_unlock_bh(&xfrm_state_afinfo_lock); 18441da177e4SLinus Torvalds return err; 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_register_afinfo); 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo) 18491da177e4SLinus Torvalds { 18501da177e4SLinus Torvalds int err = 0; 18511da177e4SLinus Torvalds if (unlikely(afinfo == NULL)) 18521da177e4SLinus Torvalds return -EINVAL; 18531da177e4SLinus Torvalds if (unlikely(afinfo->family >= NPROTO)) 18541da177e4SLinus Torvalds return -EAFNOSUPPORT; 1855f3111502SIngo Molnar write_lock_bh(&xfrm_state_afinfo_lock); 18561da177e4SLinus Torvalds if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) { 18571da177e4SLinus Torvalds if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo)) 18581da177e4SLinus Torvalds err = -EINVAL; 1859edcd5821SDavid S. Miller else 18601da177e4SLinus Torvalds xfrm_state_afinfo[afinfo->family] = NULL; 18611da177e4SLinus Torvalds } 1862f3111502SIngo Molnar write_unlock_bh(&xfrm_state_afinfo_lock); 18631da177e4SLinus Torvalds return err; 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_unregister_afinfo); 18661da177e4SLinus Torvalds 186717c2a42aSHerbert Xu static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family) 18681da177e4SLinus Torvalds { 18691da177e4SLinus Torvalds struct xfrm_state_afinfo *afinfo; 18701da177e4SLinus Torvalds if (unlikely(family >= NPROTO)) 18711da177e4SLinus Torvalds return NULL; 18721da177e4SLinus Torvalds read_lock(&xfrm_state_afinfo_lock); 18731da177e4SLinus Torvalds afinfo = xfrm_state_afinfo[family]; 1874546be240SHerbert Xu if (unlikely(!afinfo)) 18751da177e4SLinus Torvalds read_unlock(&xfrm_state_afinfo_lock); 18761da177e4SLinus Torvalds return afinfo; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 187917c2a42aSHerbert Xu static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo) 18809a429c49SEric Dumazet __releases(xfrm_state_afinfo_lock) 18811da177e4SLinus Torvalds { 1882546be240SHerbert Xu read_unlock(&xfrm_state_afinfo_lock); 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */ 18861da177e4SLinus Torvalds void xfrm_state_delete_tunnel(struct xfrm_state *x) 18871da177e4SLinus Torvalds { 18881da177e4SLinus Torvalds if (x->tunnel) { 18891da177e4SLinus Torvalds struct xfrm_state *t = x->tunnel; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds if (atomic_read(&t->tunnel_users) == 2) 18921da177e4SLinus Torvalds xfrm_state_delete(t); 18931da177e4SLinus Torvalds atomic_dec(&t->tunnel_users); 18941da177e4SLinus Torvalds xfrm_state_put(t); 18951da177e4SLinus Torvalds x->tunnel = NULL; 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete_tunnel); 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds int xfrm_state_mtu(struct xfrm_state *x, int mtu) 19011da177e4SLinus Torvalds { 1902c5c25238SPatrick McHardy int res; 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds spin_lock_bh(&x->lock); 19051da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_VALID && 1906c5c25238SPatrick McHardy x->type && x->type->get_mtu) 1907c5c25238SPatrick McHardy res = x->type->get_mtu(x, mtu); 19081da177e4SLinus Torvalds else 190928121617SPatrick McHardy res = mtu - x->props.header_len; 19101da177e4SLinus Torvalds spin_unlock_bh(&x->lock); 19111da177e4SLinus Torvalds return res; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 1914a454f0ccSWei Yongjun int __xfrm_init_state(struct xfrm_state *x, bool init_replay) 191572cb6962SHerbert Xu { 1916d094cd83SHerbert Xu struct xfrm_state_afinfo *afinfo; 1917df9dcb45SKazunori MIYAZAWA struct xfrm_mode *inner_mode; 1918d094cd83SHerbert Xu int family = x->props.family; 191972cb6962SHerbert Xu int err; 192072cb6962SHerbert Xu 1921d094cd83SHerbert Xu err = -EAFNOSUPPORT; 1922d094cd83SHerbert Xu afinfo = xfrm_state_get_afinfo(family); 1923d094cd83SHerbert Xu if (!afinfo) 1924d094cd83SHerbert Xu goto error; 1925d094cd83SHerbert Xu 1926d094cd83SHerbert Xu err = 0; 1927d094cd83SHerbert Xu if (afinfo->init_flags) 1928d094cd83SHerbert Xu err = afinfo->init_flags(x); 1929d094cd83SHerbert Xu 1930d094cd83SHerbert Xu xfrm_state_put_afinfo(afinfo); 1931d094cd83SHerbert Xu 1932d094cd83SHerbert Xu if (err) 1933d094cd83SHerbert Xu goto error; 1934d094cd83SHerbert Xu 1935d094cd83SHerbert Xu err = -EPROTONOSUPPORT; 1936df9dcb45SKazunori MIYAZAWA 1937df9dcb45SKazunori MIYAZAWA if (x->sel.family != AF_UNSPEC) { 1938df9dcb45SKazunori MIYAZAWA inner_mode = xfrm_get_mode(x->props.mode, x->sel.family); 1939df9dcb45SKazunori MIYAZAWA if (inner_mode == NULL) 194013996378SHerbert Xu goto error; 194113996378SHerbert Xu 1942df9dcb45SKazunori MIYAZAWA if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) && 1943df9dcb45SKazunori MIYAZAWA family != x->sel.family) { 1944df9dcb45SKazunori MIYAZAWA xfrm_put_mode(inner_mode); 194513996378SHerbert Xu goto error; 1946df9dcb45SKazunori MIYAZAWA } 1947df9dcb45SKazunori MIYAZAWA 1948df9dcb45SKazunori MIYAZAWA x->inner_mode = inner_mode; 1949df9dcb45SKazunori MIYAZAWA } else { 1950df9dcb45SKazunori MIYAZAWA struct xfrm_mode *inner_mode_iaf; 1951d81d2285SMartin Willi int iafamily = AF_INET; 1952df9dcb45SKazunori MIYAZAWA 1953d81d2285SMartin Willi inner_mode = xfrm_get_mode(x->props.mode, x->props.family); 1954df9dcb45SKazunori MIYAZAWA if (inner_mode == NULL) 1955df9dcb45SKazunori MIYAZAWA goto error; 1956df9dcb45SKazunori MIYAZAWA 1957df9dcb45SKazunori MIYAZAWA if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) { 1958df9dcb45SKazunori MIYAZAWA xfrm_put_mode(inner_mode); 1959df9dcb45SKazunori MIYAZAWA goto error; 1960df9dcb45SKazunori MIYAZAWA } 1961df9dcb45SKazunori MIYAZAWA x->inner_mode = inner_mode; 1962d81d2285SMartin Willi 1963d81d2285SMartin Willi if (x->props.family == AF_INET) 1964d81d2285SMartin Willi iafamily = AF_INET6; 1965d81d2285SMartin Willi 1966d81d2285SMartin Willi inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily); 1967d81d2285SMartin Willi if (inner_mode_iaf) { 1968d81d2285SMartin Willi if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL) 1969df9dcb45SKazunori MIYAZAWA x->inner_mode_iaf = inner_mode_iaf; 1970d81d2285SMartin Willi else 1971d81d2285SMartin Willi xfrm_put_mode(inner_mode_iaf); 1972df9dcb45SKazunori MIYAZAWA } 1973df9dcb45SKazunori MIYAZAWA } 197413996378SHerbert Xu 1975d094cd83SHerbert Xu x->type = xfrm_get_type(x->id.proto, family); 197672cb6962SHerbert Xu if (x->type == NULL) 197772cb6962SHerbert Xu goto error; 197872cb6962SHerbert Xu 197972cb6962SHerbert Xu err = x->type->init_state(x); 198072cb6962SHerbert Xu if (err) 198172cb6962SHerbert Xu goto error; 198272cb6962SHerbert Xu 198313996378SHerbert Xu x->outer_mode = xfrm_get_mode(x->props.mode, family); 198413996378SHerbert Xu if (x->outer_mode == NULL) 1985b59f45d0SHerbert Xu goto error; 1986b59f45d0SHerbert Xu 1987a454f0ccSWei Yongjun if (init_replay) { 1988a454f0ccSWei Yongjun err = xfrm_init_replay(x); 1989a454f0ccSWei Yongjun if (err) 1990a454f0ccSWei Yongjun goto error; 1991a454f0ccSWei Yongjun } 1992a454f0ccSWei Yongjun 199372cb6962SHerbert Xu x->km.state = XFRM_STATE_VALID; 199472cb6962SHerbert Xu 199572cb6962SHerbert Xu error: 199672cb6962SHerbert Xu return err; 199772cb6962SHerbert Xu } 199872cb6962SHerbert Xu 1999a454f0ccSWei Yongjun EXPORT_SYMBOL(__xfrm_init_state); 2000a454f0ccSWei Yongjun 2001a454f0ccSWei Yongjun int xfrm_init_state(struct xfrm_state *x) 2002a454f0ccSWei Yongjun { 2003a454f0ccSWei Yongjun return __xfrm_init_state(x, true); 2004a454f0ccSWei Yongjun } 2005a454f0ccSWei Yongjun 200672cb6962SHerbert Xu EXPORT_SYMBOL(xfrm_init_state); 20071da177e4SLinus Torvalds 2008d62ddc21SAlexey Dobriyan int __net_init xfrm_state_init(struct net *net) 20091da177e4SLinus Torvalds { 2010f034b5d4SDavid S. Miller unsigned int sz; 20111da177e4SLinus Torvalds 20129d4139c7SAlexey Dobriyan INIT_LIST_HEAD(&net->xfrm.state_all); 20139d4139c7SAlexey Dobriyan 2014f034b5d4SDavid S. Miller sz = sizeof(struct hlist_head) * 8; 2015f034b5d4SDavid S. Miller 201673d189dcSAlexey Dobriyan net->xfrm.state_bydst = xfrm_hash_alloc(sz); 201773d189dcSAlexey Dobriyan if (!net->xfrm.state_bydst) 201873d189dcSAlexey Dobriyan goto out_bydst; 2019d320bbb3SAlexey Dobriyan net->xfrm.state_bysrc = xfrm_hash_alloc(sz); 2020d320bbb3SAlexey Dobriyan if (!net->xfrm.state_bysrc) 2021d320bbb3SAlexey Dobriyan goto out_bysrc; 2022b754a4fdSAlexey Dobriyan net->xfrm.state_byspi = xfrm_hash_alloc(sz); 2023b754a4fdSAlexey Dobriyan if (!net->xfrm.state_byspi) 2024b754a4fdSAlexey Dobriyan goto out_byspi; 2025529983ecSAlexey Dobriyan net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1); 2026f034b5d4SDavid S. Miller 20270bf7c5b0SAlexey Dobriyan net->xfrm.state_num = 0; 202863082733SAlexey Dobriyan INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize); 2029b8a0ae20SAlexey Dobriyan INIT_HLIST_HEAD(&net->xfrm.state_gc_list); 2030c7837144SAlexey Dobriyan INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task); 203150a30657SAlexey Dobriyan init_waitqueue_head(&net->xfrm.km_waitq); 2032d62ddc21SAlexey Dobriyan return 0; 203373d189dcSAlexey Dobriyan 2034b754a4fdSAlexey Dobriyan out_byspi: 2035b754a4fdSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bysrc, sz); 2036d320bbb3SAlexey Dobriyan out_bysrc: 2037d320bbb3SAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bydst, sz); 203873d189dcSAlexey Dobriyan out_bydst: 203973d189dcSAlexey Dobriyan return -ENOMEM; 2040d62ddc21SAlexey Dobriyan } 2041d62ddc21SAlexey Dobriyan 2042d62ddc21SAlexey Dobriyan void xfrm_state_fini(struct net *net) 2043d62ddc21SAlexey Dobriyan { 20447c2776eeSAlexey Dobriyan struct xfrm_audit audit_info; 204573d189dcSAlexey Dobriyan unsigned int sz; 204673d189dcSAlexey Dobriyan 20477c2776eeSAlexey Dobriyan flush_work(&net->xfrm.state_hash_work); 2048e1760bd5SEric W. Biederman audit_info.loginuid = INVALID_UID; 20497c2776eeSAlexey Dobriyan audit_info.sessionid = -1; 20507c2776eeSAlexey Dobriyan audit_info.secid = 0; 20517c2776eeSAlexey Dobriyan xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info); 20527c2776eeSAlexey Dobriyan flush_work(&net->xfrm.state_gc_work); 20537c2776eeSAlexey Dobriyan 20549d4139c7SAlexey Dobriyan WARN_ON(!list_empty(&net->xfrm.state_all)); 205573d189dcSAlexey Dobriyan 2056529983ecSAlexey Dobriyan sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head); 2057b754a4fdSAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_byspi)); 2058b754a4fdSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_byspi, sz); 2059d320bbb3SAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_bysrc)); 2060d320bbb3SAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bysrc, sz); 206173d189dcSAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_bydst)); 206273d189dcSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bydst, sz); 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 2065ab5f5e8bSJoy Latten #ifdef CONFIG_AUDITSYSCALL 2066cf35f43eSIlpo Järvinen static void xfrm_audit_helper_sainfo(struct xfrm_state *x, 2067ab5f5e8bSJoy Latten struct audit_buffer *audit_buf) 2068ab5f5e8bSJoy Latten { 206968277accSPaul Moore struct xfrm_sec_ctx *ctx = x->security; 207068277accSPaul Moore u32 spi = ntohl(x->id.spi); 207168277accSPaul Moore 207268277accSPaul Moore if (ctx) 2073ab5f5e8bSJoy Latten audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", 207468277accSPaul Moore ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); 2075ab5f5e8bSJoy Latten 2076ab5f5e8bSJoy Latten switch(x->props.family) { 2077ab5f5e8bSJoy Latten case AF_INET: 207821454aaaSHarvey Harrison audit_log_format(audit_buf, " src=%pI4 dst=%pI4", 207921454aaaSHarvey Harrison &x->props.saddr.a4, &x->id.daddr.a4); 2080ab5f5e8bSJoy Latten break; 2081ab5f5e8bSJoy Latten case AF_INET6: 20825b095d98SHarvey Harrison audit_log_format(audit_buf, " src=%pI6 dst=%pI6", 2083fdb46ee7SHarvey Harrison x->props.saddr.a6, x->id.daddr.a6); 2084ab5f5e8bSJoy Latten break; 2085ab5f5e8bSJoy Latten } 208668277accSPaul Moore 208768277accSPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); 2088ab5f5e8bSJoy Latten } 2089ab5f5e8bSJoy Latten 2090cf35f43eSIlpo Järvinen static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family, 2091afeb14b4SPaul Moore struct audit_buffer *audit_buf) 2092afeb14b4SPaul Moore { 2093b71d1d42SEric Dumazet const struct iphdr *iph4; 2094b71d1d42SEric Dumazet const struct ipv6hdr *iph6; 2095afeb14b4SPaul Moore 2096afeb14b4SPaul Moore switch (family) { 2097afeb14b4SPaul Moore case AF_INET: 2098afeb14b4SPaul Moore iph4 = ip_hdr(skb); 209921454aaaSHarvey Harrison audit_log_format(audit_buf, " src=%pI4 dst=%pI4", 210021454aaaSHarvey Harrison &iph4->saddr, &iph4->daddr); 2101afeb14b4SPaul Moore break; 2102afeb14b4SPaul Moore case AF_INET6: 2103afeb14b4SPaul Moore iph6 = ipv6_hdr(skb); 2104afeb14b4SPaul Moore audit_log_format(audit_buf, 21055b095d98SHarvey Harrison " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x", 2106fdb46ee7SHarvey Harrison &iph6->saddr,&iph6->daddr, 2107afeb14b4SPaul Moore iph6->flow_lbl[0] & 0x0f, 2108afeb14b4SPaul Moore iph6->flow_lbl[1], 2109afeb14b4SPaul Moore iph6->flow_lbl[2]); 2110afeb14b4SPaul Moore break; 2111afeb14b4SPaul Moore } 2112afeb14b4SPaul Moore } 2113afeb14b4SPaul Moore 211468277accSPaul Moore void xfrm_audit_state_add(struct xfrm_state *x, int result, 2115e1760bd5SEric W. Biederman kuid_t auid, u32 sessionid, u32 secid) 2116ab5f5e8bSJoy Latten { 2117ab5f5e8bSJoy Latten struct audit_buffer *audit_buf; 2118ab5f5e8bSJoy Latten 2119afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SAD-add"); 2120ab5f5e8bSJoy Latten if (audit_buf == NULL) 2121ab5f5e8bSJoy Latten return; 21222532386fSEric Paris xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf); 2123afeb14b4SPaul Moore xfrm_audit_helper_sainfo(x, audit_buf); 2124afeb14b4SPaul Moore audit_log_format(audit_buf, " res=%u", result); 2125ab5f5e8bSJoy Latten audit_log_end(audit_buf); 2126ab5f5e8bSJoy Latten } 2127ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_add); 2128ab5f5e8bSJoy Latten 212968277accSPaul Moore void xfrm_audit_state_delete(struct xfrm_state *x, int result, 2130e1760bd5SEric W. Biederman kuid_t auid, u32 sessionid, u32 secid) 2131ab5f5e8bSJoy Latten { 2132ab5f5e8bSJoy Latten struct audit_buffer *audit_buf; 2133ab5f5e8bSJoy Latten 2134afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SAD-delete"); 2135ab5f5e8bSJoy Latten if (audit_buf == NULL) 2136ab5f5e8bSJoy Latten return; 21372532386fSEric Paris xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf); 2138afeb14b4SPaul Moore xfrm_audit_helper_sainfo(x, audit_buf); 2139afeb14b4SPaul Moore audit_log_format(audit_buf, " res=%u", result); 2140ab5f5e8bSJoy Latten audit_log_end(audit_buf); 2141ab5f5e8bSJoy Latten } 2142ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_delete); 2143afeb14b4SPaul Moore 2144afeb14b4SPaul Moore void xfrm_audit_state_replay_overflow(struct xfrm_state *x, 2145afeb14b4SPaul Moore struct sk_buff *skb) 2146afeb14b4SPaul Moore { 2147afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2148afeb14b4SPaul Moore u32 spi; 2149afeb14b4SPaul Moore 2150afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-replay-overflow"); 2151afeb14b4SPaul Moore if (audit_buf == NULL) 2152afeb14b4SPaul Moore return; 2153afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2154afeb14b4SPaul Moore /* don't record the sequence number because it's inherent in this kind 2155afeb14b4SPaul Moore * of audit message */ 2156afeb14b4SPaul Moore spi = ntohl(x->id.spi); 2157afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); 2158afeb14b4SPaul Moore audit_log_end(audit_buf); 2159afeb14b4SPaul Moore } 2160afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow); 2161afeb14b4SPaul Moore 21629fdc4883SSteffen Klassert void xfrm_audit_state_replay(struct xfrm_state *x, 2163afeb14b4SPaul Moore struct sk_buff *skb, __be32 net_seq) 2164afeb14b4SPaul Moore { 2165afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2166afeb14b4SPaul Moore u32 spi; 2167afeb14b4SPaul Moore 2168afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-replayed-pkt"); 2169afeb14b4SPaul Moore if (audit_buf == NULL) 2170afeb14b4SPaul Moore return; 2171afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2172afeb14b4SPaul Moore spi = ntohl(x->id.spi); 2173afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2174afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2175afeb14b4SPaul Moore audit_log_end(audit_buf); 2176afeb14b4SPaul Moore } 21779fdc4883SSteffen Klassert EXPORT_SYMBOL_GPL(xfrm_audit_state_replay); 2178afeb14b4SPaul Moore 2179afeb14b4SPaul Moore void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family) 2180afeb14b4SPaul Moore { 2181afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2182afeb14b4SPaul Moore 2183afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-notfound"); 2184afeb14b4SPaul Moore if (audit_buf == NULL) 2185afeb14b4SPaul Moore return; 2186afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, family, audit_buf); 2187afeb14b4SPaul Moore audit_log_end(audit_buf); 2188afeb14b4SPaul Moore } 2189afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple); 2190afeb14b4SPaul Moore 2191afeb14b4SPaul Moore void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, 2192afeb14b4SPaul Moore __be32 net_spi, __be32 net_seq) 2193afeb14b4SPaul Moore { 2194afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2195afeb14b4SPaul Moore u32 spi; 2196afeb14b4SPaul Moore 2197afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-notfound"); 2198afeb14b4SPaul Moore if (audit_buf == NULL) 2199afeb14b4SPaul Moore return; 2200afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, family, audit_buf); 2201afeb14b4SPaul Moore spi = ntohl(net_spi); 2202afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2203afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2204afeb14b4SPaul Moore audit_log_end(audit_buf); 2205afeb14b4SPaul Moore } 2206afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound); 2207afeb14b4SPaul Moore 2208afeb14b4SPaul Moore void xfrm_audit_state_icvfail(struct xfrm_state *x, 2209afeb14b4SPaul Moore struct sk_buff *skb, u8 proto) 2210afeb14b4SPaul Moore { 2211afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2212afeb14b4SPaul Moore __be32 net_spi; 2213afeb14b4SPaul Moore __be32 net_seq; 2214afeb14b4SPaul Moore 2215afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-icv-failure"); 2216afeb14b4SPaul Moore if (audit_buf == NULL) 2217afeb14b4SPaul Moore return; 2218afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2219afeb14b4SPaul Moore if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) { 2220afeb14b4SPaul Moore u32 spi = ntohl(net_spi); 2221afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2222afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2223afeb14b4SPaul Moore } 2224afeb14b4SPaul Moore audit_log_end(audit_buf); 2225afeb14b4SPaul Moore } 2226afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail); 2227ab5f5e8bSJoy Latten #endif /* CONFIG_AUDITSYSCALL */ 2228