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 38f034b5d4SDavid S. Miller static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024; 391da177e4SLinus Torvalds 4064d0cd00SAlexey Dobriyan static inline unsigned int xfrm_dst_hash(struct net *net, 412ab38503SDavid S. Miller const xfrm_address_t *daddr, 422ab38503SDavid S. Miller const xfrm_address_t *saddr, 43c1969f29SDavid S. Miller u32 reqid, 44a624c108SDavid S. Miller unsigned short family) 45a624c108SDavid S. Miller { 4664d0cd00SAlexey Dobriyan return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask); 47a624c108SDavid S. Miller } 48a624c108SDavid S. Miller 4964d0cd00SAlexey Dobriyan static inline unsigned int xfrm_src_hash(struct net *net, 502ab38503SDavid S. Miller const xfrm_address_t *daddr, 512ab38503SDavid S. Miller const xfrm_address_t *saddr, 5244e36b42SDavid S. Miller unsigned short family) 53f034b5d4SDavid S. Miller { 5464d0cd00SAlexey Dobriyan return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask); 55f034b5d4SDavid S. Miller } 56f034b5d4SDavid S. Miller 572575b654SDavid S. Miller static inline unsigned int 582ab38503SDavid S. Miller xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr, 592ab38503SDavid S. Miller __be32 spi, u8 proto, unsigned short family) 60f034b5d4SDavid S. Miller { 6164d0cd00SAlexey Dobriyan return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask); 62f034b5d4SDavid S. Miller } 63f034b5d4SDavid S. Miller 64f034b5d4SDavid S. Miller static void xfrm_hash_transfer(struct hlist_head *list, 65f034b5d4SDavid S. Miller struct hlist_head *ndsttable, 66f034b5d4SDavid S. Miller struct hlist_head *nsrctable, 67f034b5d4SDavid S. Miller struct hlist_head *nspitable, 68f034b5d4SDavid S. Miller unsigned int nhashmask) 69f034b5d4SDavid S. Miller { 70b67bfe0dSSasha Levin struct hlist_node *tmp; 71f034b5d4SDavid S. Miller struct xfrm_state *x; 72f034b5d4SDavid S. Miller 73b67bfe0dSSasha Levin hlist_for_each_entry_safe(x, tmp, list, bydst) { 74f034b5d4SDavid S. Miller unsigned int h; 75f034b5d4SDavid S. Miller 76c1969f29SDavid S. Miller h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr, 77c1969f29SDavid S. Miller x->props.reqid, x->props.family, 78c1969f29SDavid S. Miller nhashmask); 79f034b5d4SDavid S. Miller hlist_add_head(&x->bydst, ndsttable+h); 80f034b5d4SDavid S. Miller 81667bbcb6SMasahide NAKAMURA h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr, 82667bbcb6SMasahide NAKAMURA x->props.family, 83f034b5d4SDavid S. Miller nhashmask); 84f034b5d4SDavid S. Miller hlist_add_head(&x->bysrc, nsrctable+h); 85f034b5d4SDavid S. Miller 867b4dc360SMasahide NAKAMURA if (x->id.spi) { 877b4dc360SMasahide NAKAMURA h = __xfrm_spi_hash(&x->id.daddr, x->id.spi, 887b4dc360SMasahide NAKAMURA x->id.proto, x->props.family, 897b4dc360SMasahide NAKAMURA nhashmask); 90f034b5d4SDavid S. Miller hlist_add_head(&x->byspi, nspitable+h); 91f034b5d4SDavid S. Miller } 92f034b5d4SDavid S. Miller } 937b4dc360SMasahide NAKAMURA } 94f034b5d4SDavid S. Miller 9563082733SAlexey Dobriyan static unsigned long xfrm_hash_new_size(unsigned int state_hmask) 96f034b5d4SDavid S. Miller { 9763082733SAlexey Dobriyan return ((state_hmask + 1) << 1) * sizeof(struct hlist_head); 98f034b5d4SDavid S. Miller } 99f034b5d4SDavid S. Miller 10063082733SAlexey Dobriyan static void xfrm_hash_resize(struct work_struct *work) 101f034b5d4SDavid S. Miller { 10263082733SAlexey Dobriyan struct net *net = container_of(work, struct net, xfrm.state_hash_work); 103f034b5d4SDavid S. Miller struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi; 104f034b5d4SDavid S. Miller unsigned long nsize, osize; 105f034b5d4SDavid S. Miller unsigned int nhashmask, ohashmask; 106f034b5d4SDavid S. Miller int i; 107f034b5d4SDavid S. Miller 10863082733SAlexey Dobriyan nsize = xfrm_hash_new_size(net->xfrm.state_hmask); 10944e36b42SDavid S. Miller ndst = xfrm_hash_alloc(nsize); 110f034b5d4SDavid S. Miller if (!ndst) 1110244790cSYing Xue return; 11244e36b42SDavid S. Miller nsrc = xfrm_hash_alloc(nsize); 113f034b5d4SDavid S. Miller if (!nsrc) { 11444e36b42SDavid S. Miller xfrm_hash_free(ndst, nsize); 1150244790cSYing Xue return; 116f034b5d4SDavid S. Miller } 11744e36b42SDavid S. Miller nspi = xfrm_hash_alloc(nsize); 118f034b5d4SDavid S. Miller if (!nspi) { 11944e36b42SDavid S. Miller xfrm_hash_free(ndst, nsize); 12044e36b42SDavid S. Miller xfrm_hash_free(nsrc, nsize); 1210244790cSYing Xue return; 122f034b5d4SDavid S. Miller } 123f034b5d4SDavid S. Miller 124283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 125f034b5d4SDavid S. Miller 126f034b5d4SDavid S. Miller nhashmask = (nsize / sizeof(struct hlist_head)) - 1U; 12763082733SAlexey Dobriyan for (i = net->xfrm.state_hmask; i >= 0; i--) 12863082733SAlexey Dobriyan xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi, 129f034b5d4SDavid S. Miller nhashmask); 130f034b5d4SDavid S. Miller 13163082733SAlexey Dobriyan odst = net->xfrm.state_bydst; 13263082733SAlexey Dobriyan osrc = net->xfrm.state_bysrc; 13363082733SAlexey Dobriyan ospi = net->xfrm.state_byspi; 13463082733SAlexey Dobriyan ohashmask = net->xfrm.state_hmask; 135f034b5d4SDavid S. Miller 13663082733SAlexey Dobriyan net->xfrm.state_bydst = ndst; 13763082733SAlexey Dobriyan net->xfrm.state_bysrc = nsrc; 13863082733SAlexey Dobriyan net->xfrm.state_byspi = nspi; 13963082733SAlexey Dobriyan net->xfrm.state_hmask = nhashmask; 140f034b5d4SDavid S. Miller 141283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 142f034b5d4SDavid S. Miller 143f034b5d4SDavid S. Miller osize = (ohashmask + 1) * sizeof(struct hlist_head); 14444e36b42SDavid S. Miller xfrm_hash_free(odst, osize); 14544e36b42SDavid S. Miller xfrm_hash_free(osrc, osize); 14644e36b42SDavid S. Miller xfrm_hash_free(ospi, osize); 147f034b5d4SDavid S. Miller } 148f034b5d4SDavid S. Miller 14944abdc30SCong Wang static DEFINE_SPINLOCK(xfrm_state_afinfo_lock); 15044abdc30SCong Wang static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO]; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static DEFINE_SPINLOCK(xfrm_state_gc_lock); 1531da177e4SLinus Torvalds 15453bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x); 1551da177e4SLinus Torvalds 156980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol); 1570f24558eSHoria Geanta bool km_is_alive(const struct km_event *c); 15815e47304SEric W. Biederman void km_state_expired(struct xfrm_state *x, int hard, u32 portid); 1591da177e4SLinus Torvalds 1607a9885b9SCong Wang static DEFINE_SPINLOCK(xfrm_type_lock); 161533cb5b0SEric Dumazet int xfrm_register_type(const struct xfrm_type *type, unsigned short family) 162aa5d62ccSHerbert Xu { 1637a9885b9SCong Wang struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 164533cb5b0SEric Dumazet const struct xfrm_type **typemap; 165aa5d62ccSHerbert Xu int err = 0; 166aa5d62ccSHerbert Xu 167aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 168aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 169aa5d62ccSHerbert Xu typemap = afinfo->type_map; 1707a9885b9SCong Wang spin_lock_bh(&xfrm_type_lock); 171aa5d62ccSHerbert Xu 172aa5d62ccSHerbert Xu if (likely(typemap[type->proto] == NULL)) 173aa5d62ccSHerbert Xu typemap[type->proto] = type; 174aa5d62ccSHerbert Xu else 175aa5d62ccSHerbert Xu err = -EEXIST; 1767a9885b9SCong Wang spin_unlock_bh(&xfrm_type_lock); 1777a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 178aa5d62ccSHerbert Xu return err; 179aa5d62ccSHerbert Xu } 180aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_type); 181aa5d62ccSHerbert Xu 182533cb5b0SEric Dumazet int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family) 183aa5d62ccSHerbert Xu { 1847a9885b9SCong Wang struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 185533cb5b0SEric Dumazet const struct xfrm_type **typemap; 186aa5d62ccSHerbert Xu int err = 0; 187aa5d62ccSHerbert Xu 188aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 189aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 190aa5d62ccSHerbert Xu typemap = afinfo->type_map; 1917a9885b9SCong Wang spin_lock_bh(&xfrm_type_lock); 192aa5d62ccSHerbert Xu 193aa5d62ccSHerbert Xu if (unlikely(typemap[type->proto] != type)) 194aa5d62ccSHerbert Xu err = -ENOENT; 195aa5d62ccSHerbert Xu else 196aa5d62ccSHerbert Xu typemap[type->proto] = NULL; 1977a9885b9SCong Wang spin_unlock_bh(&xfrm_type_lock); 1987a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 199aa5d62ccSHerbert Xu return err; 200aa5d62ccSHerbert Xu } 201aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_type); 202aa5d62ccSHerbert Xu 203533cb5b0SEric Dumazet static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family) 204aa5d62ccSHerbert Xu { 205aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 206533cb5b0SEric Dumazet const struct xfrm_type **typemap; 207533cb5b0SEric Dumazet const struct xfrm_type *type; 208aa5d62ccSHerbert Xu int modload_attempted = 0; 209aa5d62ccSHerbert Xu 210aa5d62ccSHerbert Xu retry: 211aa5d62ccSHerbert Xu afinfo = xfrm_state_get_afinfo(family); 212aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 213aa5d62ccSHerbert Xu return NULL; 214aa5d62ccSHerbert Xu typemap = afinfo->type_map; 215aa5d62ccSHerbert Xu 216aa5d62ccSHerbert Xu type = typemap[proto]; 217aa5d62ccSHerbert Xu if (unlikely(type && !try_module_get(type->owner))) 218aa5d62ccSHerbert Xu type = NULL; 219aa5d62ccSHerbert Xu if (!type && !modload_attempted) { 220aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 221aa5d62ccSHerbert Xu request_module("xfrm-type-%d-%d", family, proto); 222aa5d62ccSHerbert Xu modload_attempted = 1; 223aa5d62ccSHerbert Xu goto retry; 224aa5d62ccSHerbert Xu } 225aa5d62ccSHerbert Xu 226aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 227aa5d62ccSHerbert Xu return type; 228aa5d62ccSHerbert Xu } 229aa5d62ccSHerbert Xu 230533cb5b0SEric Dumazet static void xfrm_put_type(const struct xfrm_type *type) 231aa5d62ccSHerbert Xu { 232aa5d62ccSHerbert Xu module_put(type->owner); 233aa5d62ccSHerbert Xu } 234aa5d62ccSHerbert Xu 2357a9885b9SCong Wang static DEFINE_SPINLOCK(xfrm_mode_lock); 236aa5d62ccSHerbert Xu int xfrm_register_mode(struct xfrm_mode *mode, int family) 237aa5d62ccSHerbert Xu { 238aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 239aa5d62ccSHerbert Xu struct xfrm_mode **modemap; 240aa5d62ccSHerbert Xu int err; 241aa5d62ccSHerbert Xu 242aa5d62ccSHerbert Xu if (unlikely(mode->encap >= XFRM_MODE_MAX)) 243aa5d62ccSHerbert Xu return -EINVAL; 244aa5d62ccSHerbert Xu 2457a9885b9SCong Wang afinfo = xfrm_state_get_afinfo(family); 246aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 247aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 248aa5d62ccSHerbert Xu 249aa5d62ccSHerbert Xu err = -EEXIST; 250aa5d62ccSHerbert Xu modemap = afinfo->mode_map; 2517a9885b9SCong Wang spin_lock_bh(&xfrm_mode_lock); 25217c2a42aSHerbert Xu if (modemap[mode->encap]) 25317c2a42aSHerbert Xu goto out; 25417c2a42aSHerbert Xu 25517c2a42aSHerbert Xu err = -ENOENT; 25617c2a42aSHerbert Xu if (!try_module_get(afinfo->owner)) 25717c2a42aSHerbert Xu goto out; 25817c2a42aSHerbert Xu 25917c2a42aSHerbert Xu mode->afinfo = afinfo; 260aa5d62ccSHerbert Xu modemap[mode->encap] = mode; 261aa5d62ccSHerbert Xu err = 0; 262aa5d62ccSHerbert Xu 26317c2a42aSHerbert Xu out: 2647a9885b9SCong Wang spin_unlock_bh(&xfrm_mode_lock); 2657a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 266aa5d62ccSHerbert Xu return err; 267aa5d62ccSHerbert Xu } 268aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_mode); 269aa5d62ccSHerbert Xu 270aa5d62ccSHerbert Xu int xfrm_unregister_mode(struct xfrm_mode *mode, int family) 271aa5d62ccSHerbert Xu { 272aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 273aa5d62ccSHerbert Xu struct xfrm_mode **modemap; 274aa5d62ccSHerbert Xu int err; 275aa5d62ccSHerbert Xu 276aa5d62ccSHerbert Xu if (unlikely(mode->encap >= XFRM_MODE_MAX)) 277aa5d62ccSHerbert Xu return -EINVAL; 278aa5d62ccSHerbert Xu 2797a9885b9SCong Wang afinfo = xfrm_state_get_afinfo(family); 280aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 281aa5d62ccSHerbert Xu return -EAFNOSUPPORT; 282aa5d62ccSHerbert Xu 283aa5d62ccSHerbert Xu err = -ENOENT; 284aa5d62ccSHerbert Xu modemap = afinfo->mode_map; 2857a9885b9SCong Wang spin_lock_bh(&xfrm_mode_lock); 286aa5d62ccSHerbert Xu if (likely(modemap[mode->encap] == mode)) { 287aa5d62ccSHerbert Xu modemap[mode->encap] = NULL; 28817c2a42aSHerbert Xu module_put(mode->afinfo->owner); 289aa5d62ccSHerbert Xu err = 0; 290aa5d62ccSHerbert Xu } 291aa5d62ccSHerbert Xu 2927a9885b9SCong Wang spin_unlock_bh(&xfrm_mode_lock); 2937a9885b9SCong Wang xfrm_state_put_afinfo(afinfo); 294aa5d62ccSHerbert Xu return err; 295aa5d62ccSHerbert Xu } 296aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_mode); 297aa5d62ccSHerbert Xu 298aa5d62ccSHerbert Xu static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family) 299aa5d62ccSHerbert Xu { 300aa5d62ccSHerbert Xu struct xfrm_state_afinfo *afinfo; 301aa5d62ccSHerbert Xu struct xfrm_mode *mode; 302aa5d62ccSHerbert Xu int modload_attempted = 0; 303aa5d62ccSHerbert Xu 304aa5d62ccSHerbert Xu if (unlikely(encap >= XFRM_MODE_MAX)) 305aa5d62ccSHerbert Xu return NULL; 306aa5d62ccSHerbert Xu 307aa5d62ccSHerbert Xu retry: 308aa5d62ccSHerbert Xu afinfo = xfrm_state_get_afinfo(family); 309aa5d62ccSHerbert Xu if (unlikely(afinfo == NULL)) 310aa5d62ccSHerbert Xu return NULL; 311aa5d62ccSHerbert Xu 312aa5d62ccSHerbert Xu mode = afinfo->mode_map[encap]; 313aa5d62ccSHerbert Xu if (unlikely(mode && !try_module_get(mode->owner))) 314aa5d62ccSHerbert Xu mode = NULL; 315aa5d62ccSHerbert Xu if (!mode && !modload_attempted) { 316aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 317aa5d62ccSHerbert Xu request_module("xfrm-mode-%d-%d", family, encap); 318aa5d62ccSHerbert Xu modload_attempted = 1; 319aa5d62ccSHerbert Xu goto retry; 320aa5d62ccSHerbert Xu } 321aa5d62ccSHerbert Xu 322aa5d62ccSHerbert Xu xfrm_state_put_afinfo(afinfo); 323aa5d62ccSHerbert Xu return mode; 324aa5d62ccSHerbert Xu } 325aa5d62ccSHerbert Xu 326aa5d62ccSHerbert Xu static void xfrm_put_mode(struct xfrm_mode *mode) 327aa5d62ccSHerbert Xu { 328aa5d62ccSHerbert Xu module_put(mode->owner); 329aa5d62ccSHerbert Xu } 330aa5d62ccSHerbert Xu 3311da177e4SLinus Torvalds static void xfrm_state_gc_destroy(struct xfrm_state *x) 3321da177e4SLinus Torvalds { 3339e0d57fdSYury Polyanskiy tasklet_hrtimer_cancel(&x->mtimer); 334a47f0ce0SDavid S. Miller del_timer_sync(&x->rtimer); 335b5884793SIlan Tayari kfree(x->aead); 3361da177e4SLinus Torvalds kfree(x->aalg); 3371da177e4SLinus Torvalds kfree(x->ealg); 3381da177e4SLinus Torvalds kfree(x->calg); 3391da177e4SLinus Torvalds kfree(x->encap); 340060f02a3SNoriaki TAKAMIYA kfree(x->coaddr); 341d8647b79SSteffen Klassert kfree(x->replay_esn); 342d8647b79SSteffen Klassert kfree(x->preplay_esn); 34313996378SHerbert Xu if (x->inner_mode) 34413996378SHerbert Xu xfrm_put_mode(x->inner_mode); 345df9dcb45SKazunori MIYAZAWA if (x->inner_mode_iaf) 346df9dcb45SKazunori MIYAZAWA xfrm_put_mode(x->inner_mode_iaf); 34713996378SHerbert Xu if (x->outer_mode) 34813996378SHerbert Xu xfrm_put_mode(x->outer_mode); 3491da177e4SLinus Torvalds if (x->type) { 3501da177e4SLinus Torvalds x->type->destructor(x); 3511da177e4SLinus Torvalds xfrm_put_type(x->type); 3521da177e4SLinus Torvalds } 353df71837dSTrent Jaeger security_xfrm_state_free(x); 3541da177e4SLinus Torvalds kfree(x); 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 357c7837144SAlexey Dobriyan static void xfrm_state_gc_task(struct work_struct *work) 3581da177e4SLinus Torvalds { 359c7837144SAlexey Dobriyan struct net *net = container_of(work, struct net, xfrm.state_gc_work); 36012a169e7SHerbert Xu struct xfrm_state *x; 361b67bfe0dSSasha Levin struct hlist_node *tmp; 36212a169e7SHerbert Xu struct hlist_head gc_list; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_gc_lock); 365c7837144SAlexey Dobriyan hlist_move_list(&net->xfrm.state_gc_list, &gc_list); 3661da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_gc_lock); 3671da177e4SLinus Torvalds 368b67bfe0dSSasha Levin hlist_for_each_entry_safe(x, tmp, &gc_list, gclist) 3691da177e4SLinus Torvalds xfrm_state_gc_destroy(x); 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds static inline unsigned long make_jiffies(long secs) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) 3751da177e4SLinus Torvalds return MAX_SCHEDULE_TIMEOUT-1; 3761da177e4SLinus Torvalds else 3771da177e4SLinus Torvalds return secs*HZ; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 3809e0d57fdSYury Polyanskiy static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me) 3811da177e4SLinus Torvalds { 3829e0d57fdSYury Polyanskiy struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer); 3839e0d57fdSYury Polyanskiy struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer); 3849d729f72SJames Morris unsigned long now = get_seconds(); 3851da177e4SLinus Torvalds long next = LONG_MAX; 3861da177e4SLinus Torvalds int warn = 0; 387161a09e7SJoy Latten int err = 0; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds spin_lock(&x->lock); 3901da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_DEAD) 3911da177e4SLinus Torvalds goto out; 3921da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_EXPIRED) 3931da177e4SLinus Torvalds goto expired; 3941da177e4SLinus Torvalds if (x->lft.hard_add_expires_seconds) { 3951da177e4SLinus Torvalds long tmo = x->lft.hard_add_expires_seconds + 3961da177e4SLinus Torvalds x->curlft.add_time - now; 397e3c0d047SFan Du if (tmo <= 0) { 398e3c0d047SFan Du if (x->xflags & XFRM_SOFT_EXPIRE) { 399e3c0d047SFan Du /* enter hard expire without soft expire first?! 400e3c0d047SFan Du * setting a new date could trigger this. 401e3c0d047SFan Du * workarbound: fix x->curflt.add_time by below: 402e3c0d047SFan Du */ 403e3c0d047SFan Du x->curlft.add_time = now - x->saved_tmo - 1; 404e3c0d047SFan Du tmo = x->lft.hard_add_expires_seconds - x->saved_tmo; 405e3c0d047SFan Du } else 4061da177e4SLinus Torvalds goto expired; 407e3c0d047SFan Du } 4081da177e4SLinus Torvalds if (tmo < next) 4091da177e4SLinus Torvalds next = tmo; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds if (x->lft.hard_use_expires_seconds) { 4121da177e4SLinus Torvalds long tmo = x->lft.hard_use_expires_seconds + 4131da177e4SLinus Torvalds (x->curlft.use_time ? : now) - now; 4141da177e4SLinus Torvalds if (tmo <= 0) 4151da177e4SLinus Torvalds goto expired; 4161da177e4SLinus Torvalds if (tmo < next) 4171da177e4SLinus Torvalds next = tmo; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds if (x->km.dying) 4201da177e4SLinus Torvalds goto resched; 4211da177e4SLinus Torvalds if (x->lft.soft_add_expires_seconds) { 4221da177e4SLinus Torvalds long tmo = x->lft.soft_add_expires_seconds + 4231da177e4SLinus Torvalds x->curlft.add_time - now; 424e3c0d047SFan Du if (tmo <= 0) { 4251da177e4SLinus Torvalds warn = 1; 426e3c0d047SFan Du x->xflags &= ~XFRM_SOFT_EXPIRE; 427e3c0d047SFan Du } else if (tmo < next) { 4281da177e4SLinus Torvalds next = tmo; 429e3c0d047SFan Du x->xflags |= XFRM_SOFT_EXPIRE; 430e3c0d047SFan Du x->saved_tmo = tmo; 431e3c0d047SFan Du } 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds if (x->lft.soft_use_expires_seconds) { 4341da177e4SLinus Torvalds long tmo = x->lft.soft_use_expires_seconds + 4351da177e4SLinus Torvalds (x->curlft.use_time ? : now) - now; 4361da177e4SLinus Torvalds if (tmo <= 0) 4371da177e4SLinus Torvalds warn = 1; 4381da177e4SLinus Torvalds else if (tmo < next) 4391da177e4SLinus Torvalds next = tmo; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 4424666faabSHerbert Xu x->km.dying = warn; 4431da177e4SLinus Torvalds if (warn) 44453bc6b4dSJamal Hadi Salim km_state_expired(x, 0, 0); 4451da177e4SLinus Torvalds resched: 4469e0d57fdSYury Polyanskiy if (next != LONG_MAX) { 4479e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL); 4489e0d57fdSYury Polyanskiy } 449a47f0ce0SDavid S. Miller 4501da177e4SLinus Torvalds goto out; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds expired: 4535b8ef341SSteffen Klassert if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) 4541da177e4SLinus Torvalds x->km.state = XFRM_STATE_EXPIRED; 455161a09e7SJoy Latten 456161a09e7SJoy Latten err = __xfrm_state_delete(x); 4570806ae4cSNicolas Dichtel if (!err) 45853bc6b4dSJamal Hadi Salim km_state_expired(x, 1, 0); 4591da177e4SLinus Torvalds 4602e71029eSTetsuo Handa xfrm_audit_state_delete(x, err ? 0 : 1, true); 461161a09e7SJoy Latten 4621da177e4SLinus Torvalds out: 4631da177e4SLinus Torvalds spin_unlock(&x->lock); 4649e0d57fdSYury Polyanskiy return HRTIMER_NORESTART; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4670ac84752SDavid S. Miller static void xfrm_replay_timer_handler(unsigned long data); 4680ac84752SDavid S. Miller 469673c09beSAlexey Dobriyan struct xfrm_state *xfrm_state_alloc(struct net *net) 4701da177e4SLinus Torvalds { 4711da177e4SLinus Torvalds struct xfrm_state *x; 4721da177e4SLinus Torvalds 4730da974f4SPanagiotis Issaris x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC); 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds if (x) { 476673c09beSAlexey Dobriyan write_pnet(&x->xs_net, net); 4771da177e4SLinus Torvalds atomic_set(&x->refcnt, 1); 4781da177e4SLinus Torvalds atomic_set(&x->tunnel_users, 0); 47912a169e7SHerbert Xu INIT_LIST_HEAD(&x->km.all); 4808f126e37SDavid S. Miller INIT_HLIST_NODE(&x->bydst); 4818f126e37SDavid S. Miller INIT_HLIST_NODE(&x->bysrc); 4828f126e37SDavid S. Miller INIT_HLIST_NODE(&x->byspi); 48399565a6cSFan Du tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, 48499565a6cSFan Du CLOCK_BOOTTIME, HRTIMER_MODE_ABS); 485b24b8a24SPavel Emelyanov setup_timer(&x->rtimer, xfrm_replay_timer_handler, 486b24b8a24SPavel Emelyanov (unsigned long)x); 4879d729f72SJames Morris x->curlft.add_time = get_seconds(); 4881da177e4SLinus Torvalds x->lft.soft_byte_limit = XFRM_INF; 4891da177e4SLinus Torvalds x->lft.soft_packet_limit = XFRM_INF; 4901da177e4SLinus Torvalds x->lft.hard_byte_limit = XFRM_INF; 4911da177e4SLinus Torvalds x->lft.hard_packet_limit = XFRM_INF; 492f8cd5488SJamal Hadi Salim x->replay_maxage = 0; 493f8cd5488SJamal Hadi Salim x->replay_maxdiff = 0; 494df9dcb45SKazunori MIYAZAWA x->inner_mode = NULL; 495df9dcb45SKazunori MIYAZAWA x->inner_mode_iaf = NULL; 4961da177e4SLinus Torvalds spin_lock_init(&x->lock); 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds return x; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_alloc); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds void __xfrm_state_destroy(struct xfrm_state *x) 5031da177e4SLinus Torvalds { 50498806f75SAlexey Dobriyan struct net *net = xs_net(x); 50598806f75SAlexey Dobriyan 506547b792cSIlpo Järvinen WARN_ON(x->km.state != XFRM_STATE_DEAD); 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds spin_lock_bh(&xfrm_state_gc_lock); 50998806f75SAlexey Dobriyan hlist_add_head(&x->gclist, &net->xfrm.state_gc_list); 5101da177e4SLinus Torvalds spin_unlock_bh(&xfrm_state_gc_lock); 51198806f75SAlexey Dobriyan schedule_work(&net->xfrm.state_gc_work); 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds EXPORT_SYMBOL(__xfrm_state_destroy); 5141da177e4SLinus Torvalds 51553bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x) 5161da177e4SLinus Torvalds { 51798806f75SAlexey Dobriyan struct net *net = xs_net(x); 51826b15dadSJamal Hadi Salim int err = -ESRCH; 51926b15dadSJamal Hadi Salim 5201da177e4SLinus Torvalds if (x->km.state != XFRM_STATE_DEAD) { 5211da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 522283bc9f3SFan Du spin_lock(&net->xfrm.xfrm_state_lock); 52312a169e7SHerbert Xu list_del(&x->km.all); 5248f126e37SDavid S. Miller hlist_del(&x->bydst); 5258f126e37SDavid S. Miller hlist_del(&x->bysrc); 526a47f0ce0SDavid S. Miller if (x->id.spi) 5278f126e37SDavid S. Miller hlist_del(&x->byspi); 52898806f75SAlexey Dobriyan net->xfrm.state_num--; 529283bc9f3SFan Du spin_unlock(&net->xfrm.xfrm_state_lock); 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* All xfrm_state objects are created by xfrm_state_alloc. 5321da177e4SLinus Torvalds * The xfrm_state_alloc call gives a reference, and that 5331da177e4SLinus Torvalds * is what we are dropping here. 5341da177e4SLinus Torvalds */ 5355dba4797SPatrick McHardy xfrm_state_put(x); 53626b15dadSJamal Hadi Salim err = 0; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 53926b15dadSJamal Hadi Salim return err; 54026b15dadSJamal Hadi Salim } 54153bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(__xfrm_state_delete); 54226b15dadSJamal Hadi Salim 54326b15dadSJamal Hadi Salim int xfrm_state_delete(struct xfrm_state *x) 5441da177e4SLinus Torvalds { 54526b15dadSJamal Hadi Salim int err; 54626b15dadSJamal Hadi Salim 5471da177e4SLinus Torvalds spin_lock_bh(&x->lock); 54826b15dadSJamal Hadi Salim err = __xfrm_state_delete(x); 5491da177e4SLinus Torvalds spin_unlock_bh(&x->lock); 55026b15dadSJamal Hadi Salim 55126b15dadSJamal Hadi Salim return err; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete); 5541da177e4SLinus Torvalds 5554aa2e62cSJoy Latten #ifdef CONFIG_SECURITY_NETWORK_XFRM 5564aa2e62cSJoy Latten static inline int 5572e71029eSTetsuo Handa xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid) 5581da177e4SLinus Torvalds { 5594aa2e62cSJoy Latten int i, err = 0; 5604aa2e62cSJoy Latten 5610e602451SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 5624aa2e62cSJoy Latten struct xfrm_state *x; 5634aa2e62cSJoy Latten 564b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { 5654aa2e62cSJoy Latten if (xfrm_id_proto_match(x->id.proto, proto) && 5664aa2e62cSJoy Latten (err = security_xfrm_state_delete(x)) != 0) { 5672e71029eSTetsuo Handa xfrm_audit_state_delete(x, 0, task_valid); 5684aa2e62cSJoy Latten return err; 5694aa2e62cSJoy Latten } 5704aa2e62cSJoy Latten } 5714aa2e62cSJoy Latten } 5724aa2e62cSJoy Latten 5734aa2e62cSJoy Latten return err; 5744aa2e62cSJoy Latten } 5754aa2e62cSJoy Latten #else 5764aa2e62cSJoy Latten static inline int 5772e71029eSTetsuo Handa xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid) 5784aa2e62cSJoy Latten { 5794aa2e62cSJoy Latten return 0; 5804aa2e62cSJoy Latten } 5814aa2e62cSJoy Latten #endif 5824aa2e62cSJoy Latten 5832e71029eSTetsuo Handa int xfrm_state_flush(struct net *net, u8 proto, bool task_valid) 5844aa2e62cSJoy Latten { 5859e64cc95SJamal Hadi Salim int i, err = 0, cnt = 0; 5861da177e4SLinus Torvalds 587283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 5882e71029eSTetsuo Handa err = xfrm_state_flush_secctx_check(net, proto, task_valid); 5894aa2e62cSJoy Latten if (err) 5904aa2e62cSJoy Latten goto out; 5914aa2e62cSJoy Latten 5929e64cc95SJamal Hadi Salim err = -ESRCH; 5930e602451SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 5948f126e37SDavid S. Miller struct xfrm_state *x; 5951da177e4SLinus Torvalds restart: 596b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { 5971da177e4SLinus Torvalds if (!xfrm_state_kern(x) && 5985794708fSMasahide NAKAMURA xfrm_id_proto_match(x->id.proto, proto)) { 5991da177e4SLinus Torvalds xfrm_state_hold(x); 600283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 6011da177e4SLinus Torvalds 602161a09e7SJoy Latten err = xfrm_state_delete(x); 603ab5f5e8bSJoy Latten xfrm_audit_state_delete(x, err ? 0 : 1, 6042e71029eSTetsuo Handa task_valid); 6051da177e4SLinus Torvalds xfrm_state_put(x); 6069e64cc95SJamal Hadi Salim if (!err) 6079e64cc95SJamal Hadi Salim cnt++; 6081da177e4SLinus Torvalds 609283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 6101da177e4SLinus Torvalds goto restart; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds } 6149e64cc95SJamal Hadi Salim if (cnt) 6154aa2e62cSJoy Latten err = 0; 6164aa2e62cSJoy Latten 6174aa2e62cSJoy Latten out: 618283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 6194aa2e62cSJoy Latten return err; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_flush); 6221da177e4SLinus Torvalds 623e071041bSAlexey Dobriyan void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si) 62428d8909bSJamal Hadi Salim { 625283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 626e071041bSAlexey Dobriyan si->sadcnt = net->xfrm.state_num; 627e071041bSAlexey Dobriyan si->sadhcnt = net->xfrm.state_hmask; 62828d8909bSJamal Hadi Salim si->sadhmcnt = xfrm_state_hashmax; 629283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 63028d8909bSJamal Hadi Salim } 63128d8909bSJamal Hadi Salim EXPORT_SYMBOL(xfrm_sad_getinfo); 63228d8909bSJamal Hadi Salim 6331da177e4SLinus Torvalds static int 6341a898592SDavid S. Miller xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl, 63504686013SDavid S. Miller const struct xfrm_tmpl *tmpl, 63633765d06SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 6371da177e4SLinus Torvalds unsigned short family) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 6401da177e4SLinus Torvalds if (!afinfo) 6411da177e4SLinus Torvalds return -1; 6428444cf71SThomas Egerer afinfo->init_tempsel(&x->sel, fl); 6438444cf71SThomas Egerer 6448444cf71SThomas Egerer if (family != tmpl->encap_family) { 6458444cf71SThomas Egerer xfrm_state_put_afinfo(afinfo); 6468444cf71SThomas Egerer afinfo = xfrm_state_get_afinfo(tmpl->encap_family); 6478444cf71SThomas Egerer if (!afinfo) 6488444cf71SThomas Egerer return -1; 6498444cf71SThomas Egerer } 6508444cf71SThomas Egerer afinfo->init_temprop(x, tmpl, daddr, saddr); 6511da177e4SLinus Torvalds xfrm_state_put_afinfo(afinfo); 6521da177e4SLinus Torvalds return 0; 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6559aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, 6569aa60088SDavid S. Miller const xfrm_address_t *daddr, 6579aa60088SDavid S. Miller __be32 spi, u8 proto, 6589aa60088SDavid S. Miller unsigned short family) 659edcd5821SDavid S. Miller { 660221df1edSAlexey Dobriyan unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family); 661edcd5821SDavid S. Miller struct xfrm_state *x; 662edcd5821SDavid S. Miller 663b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) { 664edcd5821SDavid S. Miller if (x->props.family != family || 665edcd5821SDavid S. Miller x->id.spi != spi || 6661802571bSWei Yongjun x->id.proto != proto || 66770e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->id.daddr, daddr, family)) 668edcd5821SDavid S. Miller continue; 669edcd5821SDavid S. Miller 6703d6acfa7SJamal Hadi Salim if ((mark & x->mark.m) != x->mark.v) 6713d6acfa7SJamal Hadi Salim continue; 672edcd5821SDavid S. Miller xfrm_state_hold(x); 673edcd5821SDavid S. Miller return x; 674edcd5821SDavid S. Miller } 675edcd5821SDavid S. Miller 676edcd5821SDavid S. Miller return NULL; 677edcd5821SDavid S. Miller } 678edcd5821SDavid S. Miller 6799aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, 6809aa60088SDavid S. Miller const xfrm_address_t *daddr, 6819aa60088SDavid S. Miller const xfrm_address_t *saddr, 6829aa60088SDavid S. Miller u8 proto, unsigned short family) 683edcd5821SDavid S. Miller { 684221df1edSAlexey Dobriyan unsigned int h = xfrm_src_hash(net, daddr, saddr, family); 685edcd5821SDavid S. Miller struct xfrm_state *x; 686edcd5821SDavid S. Miller 687b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) { 688edcd5821SDavid S. Miller if (x->props.family != family || 6891802571bSWei Yongjun x->id.proto != proto || 69070e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->id.daddr, daddr, family) || 69170e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, saddr, family)) 692edcd5821SDavid S. Miller continue; 693edcd5821SDavid S. Miller 6943d6acfa7SJamal Hadi Salim if ((mark & x->mark.m) != x->mark.v) 6953d6acfa7SJamal Hadi Salim continue; 696edcd5821SDavid S. Miller xfrm_state_hold(x); 697edcd5821SDavid S. Miller return x; 698edcd5821SDavid S. Miller } 699edcd5821SDavid S. Miller 700edcd5821SDavid S. Miller return NULL; 701edcd5821SDavid S. Miller } 702edcd5821SDavid S. Miller 703edcd5821SDavid S. Miller static inline struct xfrm_state * 704edcd5821SDavid S. Miller __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family) 705edcd5821SDavid S. Miller { 706221df1edSAlexey Dobriyan struct net *net = xs_net(x); 707bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 708221df1edSAlexey Dobriyan 709edcd5821SDavid S. Miller if (use_spi) 710bd55775cSJamal Hadi Salim return __xfrm_state_lookup(net, mark, &x->id.daddr, 711bd55775cSJamal Hadi Salim x->id.spi, x->id.proto, family); 712edcd5821SDavid S. Miller else 713bd55775cSJamal Hadi Salim return __xfrm_state_lookup_byaddr(net, mark, 714bd55775cSJamal Hadi Salim &x->id.daddr, 715edcd5821SDavid S. Miller &x->props.saddr, 716edcd5821SDavid S. Miller x->id.proto, family); 717edcd5821SDavid S. Miller } 718edcd5821SDavid S. Miller 71998806f75SAlexey Dobriyan static void xfrm_hash_grow_check(struct net *net, int have_hash_collision) 7202fab22f2SPatrick McHardy { 7212fab22f2SPatrick McHardy if (have_hash_collision && 72298806f75SAlexey Dobriyan (net->xfrm.state_hmask + 1) < xfrm_state_hashmax && 72398806f75SAlexey Dobriyan net->xfrm.state_num > net->xfrm.state_hmask) 72498806f75SAlexey Dobriyan schedule_work(&net->xfrm.state_hash_work); 7252fab22f2SPatrick McHardy } 7262fab22f2SPatrick McHardy 72708ec9af1SDavid S. Miller static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x, 7284a08ab0fSDavid S. Miller const struct flowi *fl, unsigned short family, 72908ec9af1SDavid S. Miller struct xfrm_state **best, int *acq_in_progress, 73008ec9af1SDavid S. Miller int *error) 73108ec9af1SDavid S. Miller { 73208ec9af1SDavid S. Miller /* Resolution logic: 73308ec9af1SDavid S. Miller * 1. There is a valid state with matching selector. Done. 73408ec9af1SDavid S. Miller * 2. Valid state with inappropriate selector. Skip. 73508ec9af1SDavid S. Miller * 73608ec9af1SDavid S. Miller * Entering area of "sysdeps". 73708ec9af1SDavid S. Miller * 73808ec9af1SDavid S. Miller * 3. If state is not valid, selector is temporary, it selects 73908ec9af1SDavid S. Miller * only session which triggered previous resolution. Key 74008ec9af1SDavid S. Miller * manager will do something to install a state with proper 74108ec9af1SDavid S. Miller * selector. 74208ec9af1SDavid S. Miller */ 74308ec9af1SDavid S. Miller if (x->km.state == XFRM_STATE_VALID) { 74408ec9af1SDavid S. Miller if ((x->sel.family && 74508ec9af1SDavid S. Miller !xfrm_selector_match(&x->sel, fl, x->sel.family)) || 74608ec9af1SDavid S. Miller !security_xfrm_state_pol_flow_match(x, pol, fl)) 74708ec9af1SDavid S. Miller return; 74808ec9af1SDavid S. Miller 74908ec9af1SDavid S. Miller if (!*best || 75008ec9af1SDavid S. Miller (*best)->km.dying > x->km.dying || 75108ec9af1SDavid S. Miller ((*best)->km.dying == x->km.dying && 75208ec9af1SDavid S. Miller (*best)->curlft.add_time < x->curlft.add_time)) 75308ec9af1SDavid S. Miller *best = x; 75408ec9af1SDavid S. Miller } else if (x->km.state == XFRM_STATE_ACQ) { 75508ec9af1SDavid S. Miller *acq_in_progress = 1; 75608ec9af1SDavid S. Miller } else if (x->km.state == XFRM_STATE_ERROR || 75708ec9af1SDavid S. Miller x->km.state == XFRM_STATE_EXPIRED) { 75808ec9af1SDavid S. Miller if (xfrm_selector_match(&x->sel, fl, x->sel.family) && 75908ec9af1SDavid S. Miller security_xfrm_state_pol_flow_match(x, pol, fl)) 76008ec9af1SDavid S. Miller *error = -ESRCH; 76108ec9af1SDavid S. Miller } 76208ec9af1SDavid S. Miller } 76308ec9af1SDavid S. Miller 7641da177e4SLinus Torvalds struct xfrm_state * 76533765d06SDavid S. Miller xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, 766b520e9f6SDavid S. Miller const struct flowi *fl, struct xfrm_tmpl *tmpl, 7671da177e4SLinus Torvalds struct xfrm_policy *pol, int *err, 7681da177e4SLinus Torvalds unsigned short family) 7691da177e4SLinus Torvalds { 77008ec9af1SDavid S. Miller static xfrm_address_t saddr_wildcard = { }; 7715447c5e4SAlexey Dobriyan struct net *net = xp_net(pol); 7726a783c90SNicolas Dichtel unsigned int h, h_wildcard; 77337b08e34SDavid S. Miller struct xfrm_state *x, *x0, *to_put; 7741da177e4SLinus Torvalds int acquire_in_progress = 0; 7751da177e4SLinus Torvalds int error = 0; 7761da177e4SLinus Torvalds struct xfrm_state *best = NULL; 777bd55775cSJamal Hadi Salim u32 mark = pol->mark.v & pol->mark.m; 7788444cf71SThomas Egerer unsigned short encap_family = tmpl->encap_family; 7790f24558eSHoria Geanta struct km_event c; 7801da177e4SLinus Torvalds 78137b08e34SDavid S. Miller to_put = NULL; 78237b08e34SDavid S. Miller 783283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 7848444cf71SThomas Egerer h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family); 785b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 7868444cf71SThomas Egerer if (x->props.family == encap_family && 7871da177e4SLinus Torvalds x->props.reqid == tmpl->reqid && 7883d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 789fbd9a5b4SMasahide NAKAMURA !(x->props.flags & XFRM_STATE_WILDRECV) && 7908444cf71SThomas Egerer xfrm_state_addr_check(x, daddr, saddr, encap_family) && 7911da177e4SLinus Torvalds tmpl->mode == x->props.mode && 7921da177e4SLinus Torvalds tmpl->id.proto == x->id.proto && 79308ec9af1SDavid S. Miller (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) 7941f673c5fSDavid S. Miller xfrm_state_look_at(pol, x, fl, encap_family, 79508ec9af1SDavid S. Miller &best, &acquire_in_progress, &error); 7961da177e4SLinus Torvalds } 7976f115638SFan Du if (best || acquire_in_progress) 79808ec9af1SDavid S. Miller goto found; 79908ec9af1SDavid S. Miller 8008444cf71SThomas Egerer h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family); 801b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) { 8028444cf71SThomas Egerer if (x->props.family == encap_family && 80308ec9af1SDavid S. Miller x->props.reqid == tmpl->reqid && 8043d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 80508ec9af1SDavid S. Miller !(x->props.flags & XFRM_STATE_WILDRECV) && 806f59bbdfaSFan Du xfrm_addr_equal(&x->id.daddr, daddr, encap_family) && 80708ec9af1SDavid S. Miller tmpl->mode == x->props.mode && 80808ec9af1SDavid S. Miller tmpl->id.proto == x->id.proto && 80908ec9af1SDavid S. Miller (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) 8101f673c5fSDavid S. Miller xfrm_state_look_at(pol, x, fl, encap_family, 81108ec9af1SDavid S. Miller &best, &acquire_in_progress, &error); 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 81408ec9af1SDavid S. Miller found: 8151da177e4SLinus Torvalds x = best; 8161da177e4SLinus Torvalds if (!x && !error && !acquire_in_progress) { 8175c5d281aSPatrick McHardy if (tmpl->id.spi && 818bd55775cSJamal Hadi Salim (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi, 8198444cf71SThomas Egerer tmpl->id.proto, encap_family)) != NULL) { 82037b08e34SDavid S. Miller to_put = x0; 8211da177e4SLinus Torvalds error = -EEXIST; 8221da177e4SLinus Torvalds goto out; 8231da177e4SLinus Torvalds } 8240f24558eSHoria Geanta 8250f24558eSHoria Geanta c.net = net; 8260f24558eSHoria Geanta /* If the KMs have no listeners (yet...), avoid allocating an SA 8270f24558eSHoria Geanta * for each and every packet - garbage collection might not 8280f24558eSHoria Geanta * handle the flood. 8290f24558eSHoria Geanta */ 8300f24558eSHoria Geanta if (!km_is_alive(&c)) { 8310f24558eSHoria Geanta error = -ESRCH; 8320f24558eSHoria Geanta goto out; 8330f24558eSHoria Geanta } 8340f24558eSHoria Geanta 8355447c5e4SAlexey Dobriyan x = xfrm_state_alloc(net); 8361da177e4SLinus Torvalds if (x == NULL) { 8371da177e4SLinus Torvalds error = -ENOMEM; 8381da177e4SLinus Torvalds goto out; 8391da177e4SLinus Torvalds } 8408444cf71SThomas Egerer /* Initialize temporary state matching only 8411da177e4SLinus Torvalds * to current session. */ 8428444cf71SThomas Egerer xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family); 843bd55775cSJamal Hadi Salim memcpy(&x->mark, &pol->mark, sizeof(x->mark)); 8441da177e4SLinus Torvalds 8451d28f42cSDavid S. Miller error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid); 846e0d1caa7SVenkat Yekkirala if (error) { 847e0d1caa7SVenkat Yekkirala x->km.state = XFRM_STATE_DEAD; 84837b08e34SDavid S. Miller to_put = x; 849e0d1caa7SVenkat Yekkirala x = NULL; 850e0d1caa7SVenkat Yekkirala goto out; 851e0d1caa7SVenkat Yekkirala } 852e0d1caa7SVenkat Yekkirala 8531da177e4SLinus Torvalds if (km_query(x, tmpl, pol) == 0) { 8541da177e4SLinus Torvalds x->km.state = XFRM_STATE_ACQ; 8555447c5e4SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 8565447c5e4SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 8578444cf71SThomas Egerer h = xfrm_src_hash(net, daddr, saddr, encap_family); 8585447c5e4SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 8591da177e4SLinus Torvalds if (x->id.spi) { 8608444cf71SThomas Egerer h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family); 8615447c5e4SAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 8621da177e4SLinus Torvalds } 863b27aeadbSAlexey Dobriyan x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; 8649e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL); 8655447c5e4SAlexey Dobriyan net->xfrm.state_num++; 8665447c5e4SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 8671da177e4SLinus Torvalds } else { 8681da177e4SLinus Torvalds x->km.state = XFRM_STATE_DEAD; 86937b08e34SDavid S. Miller to_put = x; 8701da177e4SLinus Torvalds x = NULL; 8711da177e4SLinus Torvalds error = -ESRCH; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds out: 8751da177e4SLinus Torvalds if (x) 8761da177e4SLinus Torvalds xfrm_state_hold(x); 8771da177e4SLinus Torvalds else 8781da177e4SLinus Torvalds *err = acquire_in_progress ? -EAGAIN : error; 879283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 88037b08e34SDavid S. Miller if (to_put) 88137b08e34SDavid S. Miller xfrm_state_put(to_put); 8821da177e4SLinus Torvalds return x; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 885628529b6SJamal Hadi Salim struct xfrm_state * 886bd55775cSJamal Hadi Salim xfrm_stateonly_find(struct net *net, u32 mark, 8875447c5e4SAlexey Dobriyan xfrm_address_t *daddr, xfrm_address_t *saddr, 888628529b6SJamal Hadi Salim unsigned short family, u8 mode, u8 proto, u32 reqid) 889628529b6SJamal Hadi Salim { 8904bda4f25SPavel Emelyanov unsigned int h; 891628529b6SJamal Hadi Salim struct xfrm_state *rx = NULL, *x = NULL; 892628529b6SJamal Hadi Salim 8934ae770bfSFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 8945447c5e4SAlexey Dobriyan h = xfrm_dst_hash(net, daddr, saddr, reqid, family); 895b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 896628529b6SJamal Hadi Salim if (x->props.family == family && 897628529b6SJamal Hadi Salim x->props.reqid == reqid && 8983d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 899628529b6SJamal Hadi Salim !(x->props.flags & XFRM_STATE_WILDRECV) && 900628529b6SJamal Hadi Salim xfrm_state_addr_check(x, daddr, saddr, family) && 901628529b6SJamal Hadi Salim mode == x->props.mode && 902628529b6SJamal Hadi Salim proto == x->id.proto && 903628529b6SJamal Hadi Salim x->km.state == XFRM_STATE_VALID) { 904628529b6SJamal Hadi Salim rx = x; 905628529b6SJamal Hadi Salim break; 906628529b6SJamal Hadi Salim } 907628529b6SJamal Hadi Salim } 908628529b6SJamal Hadi Salim 909628529b6SJamal Hadi Salim if (rx) 910628529b6SJamal Hadi Salim xfrm_state_hold(rx); 9114ae770bfSFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 912628529b6SJamal Hadi Salim 913628529b6SJamal Hadi Salim 914628529b6SJamal Hadi Salim return rx; 915628529b6SJamal Hadi Salim } 916628529b6SJamal Hadi Salim EXPORT_SYMBOL(xfrm_stateonly_find); 917628529b6SJamal Hadi Salim 918c454997eSFan Du struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi, 919c454997eSFan Du unsigned short family) 920c454997eSFan Du { 921c454997eSFan Du struct xfrm_state *x; 922c454997eSFan Du struct xfrm_state_walk *w; 923c454997eSFan Du 924c454997eSFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 925c454997eSFan Du list_for_each_entry(w, &net->xfrm.state_all, all) { 926c454997eSFan Du x = container_of(w, struct xfrm_state, km); 927c454997eSFan Du if (x->props.family != family || 928c454997eSFan Du x->id.spi != spi) 929c454997eSFan Du continue; 930c454997eSFan Du 931c454997eSFan Du xfrm_state_hold(x); 932bdddbf69SLi RongQing spin_unlock_bh(&net->xfrm.xfrm_state_lock); 933c454997eSFan Du return x; 934c454997eSFan Du } 935c454997eSFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 936c454997eSFan Du return NULL; 937c454997eSFan Du } 938c454997eSFan Du EXPORT_SYMBOL(xfrm_state_lookup_byspi); 939c454997eSFan Du 9401da177e4SLinus Torvalds static void __xfrm_state_insert(struct xfrm_state *x) 9411da177e4SLinus Torvalds { 94298806f75SAlexey Dobriyan struct net *net = xs_net(x); 943a624c108SDavid S. Miller unsigned int h; 9441da177e4SLinus Torvalds 94598806f75SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 9464c563f76STimo Teras 94798806f75SAlexey Dobriyan h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr, 948c1969f29SDavid S. Miller x->props.reqid, x->props.family); 94998806f75SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 9501da177e4SLinus Torvalds 95198806f75SAlexey Dobriyan h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family); 95298806f75SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 9536c44e6b7SMasahide NAKAMURA 9547b4dc360SMasahide NAKAMURA if (x->id.spi) { 95598806f75SAlexey Dobriyan h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, 9566c44e6b7SMasahide NAKAMURA x->props.family); 9571da177e4SLinus Torvalds 95898806f75SAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 9596c44e6b7SMasahide NAKAMURA } 9601da177e4SLinus Torvalds 9619e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); 962a47f0ce0SDavid S. Miller if (x->replay_maxage) 963a47f0ce0SDavid S. Miller mod_timer(&x->rtimer, jiffies + x->replay_maxage); 964f8cd5488SJamal Hadi Salim 96598806f75SAlexey Dobriyan net->xfrm.state_num++; 966f034b5d4SDavid S. Miller 96798806f75SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 970283bc9f3SFan Du /* net->xfrm.xfrm_state_lock is held */ 971c7f5ea3aSDavid S. Miller static void __xfrm_state_bump_genids(struct xfrm_state *xnew) 972c7f5ea3aSDavid S. Miller { 97398806f75SAlexey Dobriyan struct net *net = xs_net(xnew); 974c7f5ea3aSDavid S. Miller unsigned short family = xnew->props.family; 975c7f5ea3aSDavid S. Miller u32 reqid = xnew->props.reqid; 976c7f5ea3aSDavid S. Miller struct xfrm_state *x; 977c7f5ea3aSDavid S. Miller unsigned int h; 9783d6acfa7SJamal Hadi Salim u32 mark = xnew->mark.v & xnew->mark.m; 979c7f5ea3aSDavid S. Miller 98098806f75SAlexey Dobriyan h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family); 981b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 982c7f5ea3aSDavid S. Miller if (x->props.family == family && 983c7f5ea3aSDavid S. Miller x->props.reqid == reqid && 9843d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 98570e94e66SYOSHIFUJI Hideaki / 吉藤英明 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) && 98670e94e66SYOSHIFUJI Hideaki / 吉藤英明 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family)) 98734996cb9SHerbert Xu x->genid++; 988c7f5ea3aSDavid S. Miller } 989c7f5ea3aSDavid S. Miller } 990c7f5ea3aSDavid S. Miller 9911da177e4SLinus Torvalds void xfrm_state_insert(struct xfrm_state *x) 9921da177e4SLinus Torvalds { 993283bc9f3SFan Du struct net *net = xs_net(x); 994283bc9f3SFan Du 995283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 996c7f5ea3aSDavid S. Miller __xfrm_state_bump_genids(x); 9971da177e4SLinus Torvalds __xfrm_state_insert(x); 998283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_insert); 10011da177e4SLinus Torvalds 1002283bc9f3SFan Du /* net->xfrm.xfrm_state_lock is held */ 1003e473fcb4SMathias Krause static struct xfrm_state *__find_acq_core(struct net *net, 1004e473fcb4SMathias Krause const struct xfrm_mark *m, 1005a70486f0SDavid S. Miller unsigned short family, u8 mode, 1006a70486f0SDavid S. Miller u32 reqid, u8 proto, 1007a70486f0SDavid S. Miller const xfrm_address_t *daddr, 1008e473fcb4SMathias Krause const xfrm_address_t *saddr, 1009e473fcb4SMathias Krause int create) 10102770834cSDavid S. Miller { 10115447c5e4SAlexey Dobriyan unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family); 10122770834cSDavid S. Miller struct xfrm_state *x; 10133d6acfa7SJamal Hadi Salim u32 mark = m->v & m->m; 10142770834cSDavid S. Miller 1015b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 10162770834cSDavid S. Miller if (x->props.reqid != reqid || 10172770834cSDavid S. Miller x->props.mode != mode || 10182770834cSDavid S. Miller x->props.family != family || 10192770834cSDavid S. Miller x->km.state != XFRM_STATE_ACQ || 102075e252d9SJoy Latten x->id.spi != 0 || 10211802571bSWei Yongjun x->id.proto != proto || 10223d6acfa7SJamal Hadi Salim (mark & x->mark.m) != x->mark.v || 102370e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->id.daddr, daddr, family) || 102470e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, saddr, family)) 10252770834cSDavid S. Miller continue; 10262770834cSDavid S. Miller 10272770834cSDavid S. Miller xfrm_state_hold(x); 10282770834cSDavid S. Miller return x; 10292770834cSDavid S. Miller } 10302770834cSDavid S. Miller 10312770834cSDavid S. Miller if (!create) 10322770834cSDavid S. Miller return NULL; 10332770834cSDavid S. Miller 10345447c5e4SAlexey Dobriyan x = xfrm_state_alloc(net); 10352770834cSDavid S. Miller if (likely(x)) { 10362770834cSDavid S. Miller switch (family) { 10372770834cSDavid S. Miller case AF_INET: 10382770834cSDavid S. Miller x->sel.daddr.a4 = daddr->a4; 10392770834cSDavid S. Miller x->sel.saddr.a4 = saddr->a4; 10402770834cSDavid S. Miller x->sel.prefixlen_d = 32; 10412770834cSDavid S. Miller x->sel.prefixlen_s = 32; 10422770834cSDavid S. Miller x->props.saddr.a4 = saddr->a4; 10432770834cSDavid S. Miller x->id.daddr.a4 = daddr->a4; 10442770834cSDavid S. Miller break; 10452770834cSDavid S. Miller 10462770834cSDavid S. Miller case AF_INET6: 104715e318bdSJiri Benc x->sel.daddr.in6 = daddr->in6; 104815e318bdSJiri Benc x->sel.saddr.in6 = saddr->in6; 10492770834cSDavid S. Miller x->sel.prefixlen_d = 128; 10502770834cSDavid S. Miller x->sel.prefixlen_s = 128; 105115e318bdSJiri Benc x->props.saddr.in6 = saddr->in6; 105215e318bdSJiri Benc x->id.daddr.in6 = daddr->in6; 10532770834cSDavid S. Miller break; 10543ff50b79SStephen Hemminger } 10552770834cSDavid S. Miller 10562770834cSDavid S. Miller x->km.state = XFRM_STATE_ACQ; 10572770834cSDavid S. Miller x->id.proto = proto; 10582770834cSDavid S. Miller x->props.family = family; 10592770834cSDavid S. Miller x->props.mode = mode; 10602770834cSDavid S. Miller x->props.reqid = reqid; 1061bd55775cSJamal Hadi Salim x->mark.v = m->v; 1062bd55775cSJamal Hadi Salim x->mark.m = m->m; 1063b27aeadbSAlexey Dobriyan x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; 10642770834cSDavid S. Miller xfrm_state_hold(x); 10659e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL); 10665447c5e4SAlexey Dobriyan list_add(&x->km.all, &net->xfrm.state_all); 10675447c5e4SAlexey Dobriyan hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); 10685447c5e4SAlexey Dobriyan h = xfrm_src_hash(net, daddr, saddr, family); 10695447c5e4SAlexey Dobriyan hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); 1070918049f0SDavid S. Miller 10715447c5e4SAlexey Dobriyan net->xfrm.state_num++; 1072918049f0SDavid S. Miller 10735447c5e4SAlexey Dobriyan xfrm_hash_grow_check(net, x->bydst.next != NULL); 10742770834cSDavid S. Miller } 10752770834cSDavid S. Miller 10762770834cSDavid S. Miller return x; 10772770834cSDavid S. Miller } 10782770834cSDavid S. Miller 1079bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds int xfrm_state_add(struct xfrm_state *x) 10821da177e4SLinus Torvalds { 10835447c5e4SAlexey Dobriyan struct net *net = xs_net(x); 108437b08e34SDavid S. Miller struct xfrm_state *x1, *to_put; 10851da177e4SLinus Torvalds int family; 10861da177e4SLinus Torvalds int err; 1087bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 1088eb2971b6SMasahide NAKAMURA int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds family = x->props.family; 10911da177e4SLinus Torvalds 109237b08e34SDavid S. Miller to_put = NULL; 109337b08e34SDavid S. Miller 1094283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 10951da177e4SLinus Torvalds 1096edcd5821SDavid S. Miller x1 = __xfrm_state_locate(x, use_spi, family); 10971da177e4SLinus Torvalds if (x1) { 109837b08e34SDavid S. Miller to_put = x1; 10991da177e4SLinus Torvalds x1 = NULL; 11001da177e4SLinus Torvalds err = -EEXIST; 11011da177e4SLinus Torvalds goto out; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 1104eb2971b6SMasahide NAKAMURA if (use_spi && x->km.seq) { 1105bd55775cSJamal Hadi Salim x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq); 110675e252d9SJoy Latten if (x1 && ((x1->id.proto != x->id.proto) || 110770e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) { 110837b08e34SDavid S. Miller to_put = x1; 11091da177e4SLinus Torvalds x1 = NULL; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 1113eb2971b6SMasahide NAKAMURA if (use_spi && !x1) 1114bd55775cSJamal Hadi Salim x1 = __find_acq_core(net, &x->mark, family, x->props.mode, 1115bd55775cSJamal Hadi Salim x->props.reqid, x->id.proto, 11161da177e4SLinus Torvalds &x->id.daddr, &x->props.saddr, 0); 11171da177e4SLinus Torvalds 1118c7f5ea3aSDavid S. Miller __xfrm_state_bump_genids(x); 11191da177e4SLinus Torvalds __xfrm_state_insert(x); 11201da177e4SLinus Torvalds err = 0; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds out: 1123283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds if (x1) { 11261da177e4SLinus Torvalds xfrm_state_delete(x1); 11271da177e4SLinus Torvalds xfrm_state_put(x1); 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 113037b08e34SDavid S. Miller if (to_put) 113137b08e34SDavid S. Miller xfrm_state_put(to_put); 113237b08e34SDavid S. Miller 11331da177e4SLinus Torvalds return err; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_add); 11361da177e4SLinus Torvalds 113780c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE 1138cc9ab60eSSteffen Klassert static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig) 113980c9abaaSShinta Sugimoto { 114098806f75SAlexey Dobriyan struct net *net = xs_net(orig); 114198806f75SAlexey Dobriyan struct xfrm_state *x = xfrm_state_alloc(net); 114280c9abaaSShinta Sugimoto if (!x) 1143553f9118SHerbert Xu goto out; 114480c9abaaSShinta Sugimoto 114580c9abaaSShinta Sugimoto memcpy(&x->id, &orig->id, sizeof(x->id)); 114680c9abaaSShinta Sugimoto memcpy(&x->sel, &orig->sel, sizeof(x->sel)); 114780c9abaaSShinta Sugimoto memcpy(&x->lft, &orig->lft, sizeof(x->lft)); 114880c9abaaSShinta Sugimoto x->props.mode = orig->props.mode; 114980c9abaaSShinta Sugimoto x->props.replay_window = orig->props.replay_window; 115080c9abaaSShinta Sugimoto x->props.reqid = orig->props.reqid; 115180c9abaaSShinta Sugimoto x->props.family = orig->props.family; 115280c9abaaSShinta Sugimoto x->props.saddr = orig->props.saddr; 115380c9abaaSShinta Sugimoto 115480c9abaaSShinta Sugimoto if (orig->aalg) { 11554447bb33SMartin Willi x->aalg = xfrm_algo_auth_clone(orig->aalg); 115680c9abaaSShinta Sugimoto if (!x->aalg) 115780c9abaaSShinta Sugimoto goto error; 115880c9abaaSShinta Sugimoto } 115980c9abaaSShinta Sugimoto x->props.aalgo = orig->props.aalgo; 116080c9abaaSShinta Sugimoto 1161ee5c2317SSteffen Klassert if (orig->aead) { 1162ee5c2317SSteffen Klassert x->aead = xfrm_algo_aead_clone(orig->aead); 1163ee5c2317SSteffen Klassert if (!x->aead) 1164ee5c2317SSteffen Klassert goto error; 1165ee5c2317SSteffen Klassert } 116680c9abaaSShinta Sugimoto if (orig->ealg) { 116780c9abaaSShinta Sugimoto x->ealg = xfrm_algo_clone(orig->ealg); 116880c9abaaSShinta Sugimoto if (!x->ealg) 116980c9abaaSShinta Sugimoto goto error; 117080c9abaaSShinta Sugimoto } 117180c9abaaSShinta Sugimoto x->props.ealgo = orig->props.ealgo; 117280c9abaaSShinta Sugimoto 117380c9abaaSShinta Sugimoto if (orig->calg) { 117480c9abaaSShinta Sugimoto x->calg = xfrm_algo_clone(orig->calg); 117580c9abaaSShinta Sugimoto if (!x->calg) 117680c9abaaSShinta Sugimoto goto error; 117780c9abaaSShinta Sugimoto } 117880c9abaaSShinta Sugimoto x->props.calgo = orig->props.calgo; 117980c9abaaSShinta Sugimoto 118080c9abaaSShinta Sugimoto if (orig->encap) { 118180c9abaaSShinta Sugimoto x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL); 118280c9abaaSShinta Sugimoto if (!x->encap) 118380c9abaaSShinta Sugimoto goto error; 118480c9abaaSShinta Sugimoto } 118580c9abaaSShinta Sugimoto 118680c9abaaSShinta Sugimoto if (orig->coaddr) { 118780c9abaaSShinta Sugimoto x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr), 118880c9abaaSShinta Sugimoto GFP_KERNEL); 118980c9abaaSShinta Sugimoto if (!x->coaddr) 119080c9abaaSShinta Sugimoto goto error; 119180c9abaaSShinta Sugimoto } 119280c9abaaSShinta Sugimoto 1193af2f464eSSteffen Klassert if (orig->replay_esn) { 1194cc9ab60eSSteffen Klassert if (xfrm_replay_clone(x, orig)) 1195af2f464eSSteffen Klassert goto error; 1196af2f464eSSteffen Klassert } 1197af2f464eSSteffen Klassert 1198bd55775cSJamal Hadi Salim memcpy(&x->mark, &orig->mark, sizeof(x->mark)); 1199bd55775cSJamal Hadi Salim 1200cc9ab60eSSteffen Klassert if (xfrm_init_state(x) < 0) 120180c9abaaSShinta Sugimoto goto error; 120280c9abaaSShinta Sugimoto 120380c9abaaSShinta Sugimoto x->props.flags = orig->props.flags; 1204a947b0a9SNicolas Dichtel x->props.extra_flags = orig->props.extra_flags; 120580c9abaaSShinta Sugimoto 1206ee5c2317SSteffen Klassert x->tfcpad = orig->tfcpad; 1207ee5c2317SSteffen Klassert x->replay_maxdiff = orig->replay_maxdiff; 1208ee5c2317SSteffen Klassert x->replay_maxage = orig->replay_maxage; 120980c9abaaSShinta Sugimoto x->curlft.add_time = orig->curlft.add_time; 121080c9abaaSShinta Sugimoto x->km.state = orig->km.state; 121180c9abaaSShinta Sugimoto x->km.seq = orig->km.seq; 121280c9abaaSShinta Sugimoto 121380c9abaaSShinta Sugimoto return x; 121480c9abaaSShinta Sugimoto 121580c9abaaSShinta Sugimoto error: 1216553f9118SHerbert Xu xfrm_state_put(x); 1217553f9118SHerbert Xu out: 121880c9abaaSShinta Sugimoto return NULL; 121980c9abaaSShinta Sugimoto } 122080c9abaaSShinta Sugimoto 1221283bc9f3SFan Du struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net) 122280c9abaaSShinta Sugimoto { 122380c9abaaSShinta Sugimoto unsigned int h; 12248c0cba22SSteffen Klassert struct xfrm_state *x = NULL; 12258c0cba22SSteffen Klassert 12268c0cba22SSteffen Klassert spin_lock_bh(&net->xfrm.xfrm_state_lock); 122780c9abaaSShinta Sugimoto 122880c9abaaSShinta Sugimoto if (m->reqid) { 1229283bc9f3SFan Du h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr, 123080c9abaaSShinta Sugimoto m->reqid, m->old_family); 1231283bc9f3SFan Du hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { 123280c9abaaSShinta Sugimoto if (x->props.mode != m->mode || 123380c9abaaSShinta Sugimoto x->id.proto != m->proto) 123480c9abaaSShinta Sugimoto continue; 123580c9abaaSShinta Sugimoto if (m->reqid && x->props.reqid != m->reqid) 123680c9abaaSShinta Sugimoto continue; 123770e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, 123880c9abaaSShinta Sugimoto m->old_family) || 123970e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, 124080c9abaaSShinta Sugimoto m->old_family)) 124180c9abaaSShinta Sugimoto continue; 124280c9abaaSShinta Sugimoto xfrm_state_hold(x); 12438c0cba22SSteffen Klassert break; 124480c9abaaSShinta Sugimoto } 124580c9abaaSShinta Sugimoto } else { 1246283bc9f3SFan Du h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr, 124780c9abaaSShinta Sugimoto m->old_family); 1248283bc9f3SFan Du hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) { 124980c9abaaSShinta Sugimoto if (x->props.mode != m->mode || 125080c9abaaSShinta Sugimoto x->id.proto != m->proto) 125180c9abaaSShinta Sugimoto continue; 125270e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, 125380c9abaaSShinta Sugimoto m->old_family) || 125470e94e66SYOSHIFUJI Hideaki / 吉藤英明 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, 125580c9abaaSShinta Sugimoto m->old_family)) 125680c9abaaSShinta Sugimoto continue; 125780c9abaaSShinta Sugimoto xfrm_state_hold(x); 12588c0cba22SSteffen Klassert break; 125980c9abaaSShinta Sugimoto } 126080c9abaaSShinta Sugimoto } 126180c9abaaSShinta Sugimoto 12628c0cba22SSteffen Klassert spin_unlock_bh(&net->xfrm.xfrm_state_lock); 12638c0cba22SSteffen Klassert 12648c0cba22SSteffen Klassert return x; 126580c9abaaSShinta Sugimoto } 126680c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_migrate_state_find); 126780c9abaaSShinta Sugimoto 126880c9abaaSShinta Sugimoto struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x, 126980c9abaaSShinta Sugimoto struct xfrm_migrate *m) 127080c9abaaSShinta Sugimoto { 127180c9abaaSShinta Sugimoto struct xfrm_state *xc; 127280c9abaaSShinta Sugimoto 1273cc9ab60eSSteffen Klassert xc = xfrm_state_clone(x); 127480c9abaaSShinta Sugimoto if (!xc) 127580c9abaaSShinta Sugimoto return NULL; 127680c9abaaSShinta Sugimoto 127780c9abaaSShinta Sugimoto memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr)); 127880c9abaaSShinta Sugimoto memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr)); 127980c9abaaSShinta Sugimoto 128080c9abaaSShinta Sugimoto /* add state */ 128170e94e66SYOSHIFUJI Hideaki / 吉藤英明 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) { 128280c9abaaSShinta Sugimoto /* a care is needed when the destination address of the 128380c9abaaSShinta Sugimoto state is to be updated as it is a part of triplet */ 128480c9abaaSShinta Sugimoto xfrm_state_insert(xc); 128580c9abaaSShinta Sugimoto } else { 1286cc9ab60eSSteffen Klassert if (xfrm_state_add(xc) < 0) 128780c9abaaSShinta Sugimoto goto error; 128880c9abaaSShinta Sugimoto } 128980c9abaaSShinta Sugimoto 129080c9abaaSShinta Sugimoto return xc; 129180c9abaaSShinta Sugimoto error: 129278347c8cSThomas Egerer xfrm_state_put(xc); 129380c9abaaSShinta Sugimoto return NULL; 129480c9abaaSShinta Sugimoto } 129580c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_state_migrate); 129680c9abaaSShinta Sugimoto #endif 129780c9abaaSShinta Sugimoto 12981da177e4SLinus Torvalds int xfrm_state_update(struct xfrm_state *x) 12991da177e4SLinus Torvalds { 130037b08e34SDavid S. Miller struct xfrm_state *x1, *to_put; 13011da177e4SLinus Torvalds int err; 1302eb2971b6SMasahide NAKAMURA int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); 1303283bc9f3SFan Du struct net *net = xs_net(x); 13041da177e4SLinus Torvalds 130537b08e34SDavid S. Miller to_put = NULL; 130637b08e34SDavid S. Miller 1307283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 1308edcd5821SDavid S. Miller x1 = __xfrm_state_locate(x, use_spi, x->props.family); 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds err = -ESRCH; 13111da177e4SLinus Torvalds if (!x1) 13121da177e4SLinus Torvalds goto out; 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds if (xfrm_state_kern(x1)) { 131537b08e34SDavid S. Miller to_put = x1; 13161da177e4SLinus Torvalds err = -EEXIST; 13171da177e4SLinus Torvalds goto out; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds if (x1->km.state == XFRM_STATE_ACQ) { 13211da177e4SLinus Torvalds __xfrm_state_insert(x); 13221da177e4SLinus Torvalds x = NULL; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds err = 0; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds out: 1327283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 13281da177e4SLinus Torvalds 132937b08e34SDavid S. Miller if (to_put) 133037b08e34SDavid S. Miller xfrm_state_put(to_put); 133137b08e34SDavid S. Miller 13321da177e4SLinus Torvalds if (err) 13331da177e4SLinus Torvalds return err; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds if (!x) { 13361da177e4SLinus Torvalds xfrm_state_delete(x1); 13371da177e4SLinus Torvalds xfrm_state_put(x1); 13381da177e4SLinus Torvalds return 0; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds err = -EINVAL; 13421da177e4SLinus Torvalds spin_lock_bh(&x1->lock); 13431da177e4SLinus Torvalds if (likely(x1->km.state == XFRM_STATE_VALID)) { 13441da177e4SLinus Torvalds if (x->encap && x1->encap) 13451da177e4SLinus Torvalds memcpy(x1->encap, x->encap, sizeof(*x1->encap)); 1346060f02a3SNoriaki TAKAMIYA if (x->coaddr && x1->coaddr) { 1347060f02a3SNoriaki TAKAMIYA memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr)); 1348060f02a3SNoriaki TAKAMIYA } 1349060f02a3SNoriaki TAKAMIYA if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel))) 1350060f02a3SNoriaki TAKAMIYA memcpy(&x1->sel, &x->sel, sizeof(x1->sel)); 13511da177e4SLinus Torvalds memcpy(&x1->lft, &x->lft, sizeof(x1->lft)); 13521da177e4SLinus Torvalds x1->km.dying = 0; 13531da177e4SLinus Torvalds 13549e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); 13551da177e4SLinus Torvalds if (x1->curlft.use_time) 13561da177e4SLinus Torvalds xfrm_state_check_expire(x1); 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds err = 0; 13598fcbc637STushar Gohad x->km.state = XFRM_STATE_DEAD; 13608fcbc637STushar Gohad __xfrm_state_put(x); 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds spin_unlock_bh(&x1->lock); 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds xfrm_state_put(x1); 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds return err; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_update); 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds int xfrm_state_check_expire(struct xfrm_state *x) 13711da177e4SLinus Torvalds { 13721da177e4SLinus Torvalds if (!x->curlft.use_time) 13739d729f72SJames Morris x->curlft.use_time = get_seconds(); 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds if (x->curlft.bytes >= x->lft.hard_byte_limit || 13761da177e4SLinus Torvalds x->curlft.packets >= x->lft.hard_packet_limit) { 13774666faabSHerbert Xu x->km.state = XFRM_STATE_EXPIRED; 13789e0d57fdSYury Polyanskiy tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL); 13791da177e4SLinus Torvalds return -EINVAL; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds if (!x->km.dying && 13831da177e4SLinus Torvalds (x->curlft.bytes >= x->lft.soft_byte_limit || 13844666faabSHerbert Xu x->curlft.packets >= x->lft.soft_packet_limit)) { 13854666faabSHerbert Xu x->km.dying = 1; 138653bc6b4dSJamal Hadi Salim km_state_expired(x, 0, 0); 13874666faabSHerbert Xu } 13881da177e4SLinus Torvalds return 0; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_check_expire); 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds struct xfrm_state * 1393a70486f0SDavid S. Miller xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi, 1394bd55775cSJamal Hadi Salim u8 proto, unsigned short family) 13951da177e4SLinus Torvalds { 13961da177e4SLinus Torvalds struct xfrm_state *x; 13971da177e4SLinus Torvalds 1398283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 1399bd55775cSJamal Hadi Salim x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family); 1400283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 14011da177e4SLinus Torvalds return x; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_lookup); 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds struct xfrm_state * 1406bd55775cSJamal Hadi Salim xfrm_state_lookup_byaddr(struct net *net, u32 mark, 1407a70486f0SDavid S. Miller const xfrm_address_t *daddr, const xfrm_address_t *saddr, 1408eb2971b6SMasahide NAKAMURA u8 proto, unsigned short family) 1409eb2971b6SMasahide NAKAMURA { 1410eb2971b6SMasahide NAKAMURA struct xfrm_state *x; 1411eb2971b6SMasahide NAKAMURA 1412283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 1413bd55775cSJamal Hadi Salim x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family); 1414283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 1415eb2971b6SMasahide NAKAMURA return x; 1416eb2971b6SMasahide NAKAMURA } 1417eb2971b6SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_lookup_byaddr); 1418eb2971b6SMasahide NAKAMURA 1419eb2971b6SMasahide NAKAMURA struct xfrm_state * 1420e473fcb4SMathias Krause xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid, 1421e473fcb4SMathias Krause u8 proto, const xfrm_address_t *daddr, 1422e473fcb4SMathias Krause const xfrm_address_t *saddr, int create, unsigned short family) 14231da177e4SLinus Torvalds { 14241da177e4SLinus Torvalds struct xfrm_state *x; 14251da177e4SLinus Torvalds 1426283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 1427bd55775cSJamal Hadi Salim x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create); 1428283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 14292770834cSDavid S. Miller 14301da177e4SLinus Torvalds return x; 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq); 14331da177e4SLinus Torvalds 143441a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY 143541a49cc3SMasahide NAKAMURA int 143641a49cc3SMasahide NAKAMURA xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n, 1437283bc9f3SFan Du unsigned short family, struct net *net) 143841a49cc3SMasahide NAKAMURA { 143941a49cc3SMasahide NAKAMURA int err = 0; 144041a49cc3SMasahide NAKAMURA struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 144141a49cc3SMasahide NAKAMURA if (!afinfo) 144241a49cc3SMasahide NAKAMURA return -EAFNOSUPPORT; 144341a49cc3SMasahide NAKAMURA 1444283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/ 144541a49cc3SMasahide NAKAMURA if (afinfo->tmpl_sort) 144641a49cc3SMasahide NAKAMURA err = afinfo->tmpl_sort(dst, src, n); 1447283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 144841a49cc3SMasahide NAKAMURA xfrm_state_put_afinfo(afinfo); 144941a49cc3SMasahide NAKAMURA return err; 145041a49cc3SMasahide NAKAMURA } 145141a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_tmpl_sort); 145241a49cc3SMasahide NAKAMURA 145341a49cc3SMasahide NAKAMURA int 145441a49cc3SMasahide NAKAMURA xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n, 145541a49cc3SMasahide NAKAMURA unsigned short family) 145641a49cc3SMasahide NAKAMURA { 145741a49cc3SMasahide NAKAMURA int err = 0; 145841a49cc3SMasahide NAKAMURA struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); 145935ea790dSSteffen Klassert struct net *net = xs_net(*src); 1460283bc9f3SFan Du 146141a49cc3SMasahide NAKAMURA if (!afinfo) 146241a49cc3SMasahide NAKAMURA return -EAFNOSUPPORT; 146341a49cc3SMasahide NAKAMURA 1464283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 146541a49cc3SMasahide NAKAMURA if (afinfo->state_sort) 146641a49cc3SMasahide NAKAMURA err = afinfo->state_sort(dst, src, n); 1467283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 146841a49cc3SMasahide NAKAMURA xfrm_state_put_afinfo(afinfo); 146941a49cc3SMasahide NAKAMURA return err; 147041a49cc3SMasahide NAKAMURA } 147141a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_sort); 147241a49cc3SMasahide NAKAMURA #endif 147341a49cc3SMasahide NAKAMURA 14741da177e4SLinus Torvalds /* Silly enough, but I'm lazy to build resolution list */ 14751da177e4SLinus Torvalds 1476bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) 14771da177e4SLinus Torvalds { 14781da177e4SLinus Torvalds int i; 14791da177e4SLinus Torvalds 14805447c5e4SAlexey Dobriyan for (i = 0; i <= net->xfrm.state_hmask; i++) { 14818f126e37SDavid S. Miller struct xfrm_state *x; 14828f126e37SDavid S. Miller 1483b67bfe0dSSasha Levin hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { 14848f126e37SDavid S. Miller if (x->km.seq == seq && 14853d6acfa7SJamal Hadi Salim (mark & x->mark.m) == x->mark.v && 14868f126e37SDavid S. Miller x->km.state == XFRM_STATE_ACQ) { 14871da177e4SLinus Torvalds xfrm_state_hold(x); 14881da177e4SLinus Torvalds return x; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds return NULL; 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds 1495bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) 14961da177e4SLinus Torvalds { 14971da177e4SLinus Torvalds struct xfrm_state *x; 14981da177e4SLinus Torvalds 1499283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 1500bd55775cSJamal Hadi Salim x = __xfrm_find_acq_byseq(net, mark, seq); 1501283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 15021da177e4SLinus Torvalds return x; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq_byseq); 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds u32 xfrm_get_acqseq(void) 15071da177e4SLinus Torvalds { 15081da177e4SLinus Torvalds u32 res; 15096836b9bdSjamal static atomic_t acqseq; 15101da177e4SLinus Torvalds 15116836b9bdSjamal do { 15126836b9bdSjamal res = atomic_inc_return(&acqseq); 15136836b9bdSjamal } while (!res); 15146836b9bdSjamal 15151da177e4SLinus Torvalds return res; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_get_acqseq); 15181da177e4SLinus Torvalds 1519776e9dd9SFan Du int verify_spi_info(u8 proto, u32 min, u32 max) 1520776e9dd9SFan Du { 1521776e9dd9SFan Du switch (proto) { 1522776e9dd9SFan Du case IPPROTO_AH: 1523776e9dd9SFan Du case IPPROTO_ESP: 1524776e9dd9SFan Du break; 1525776e9dd9SFan Du 1526776e9dd9SFan Du case IPPROTO_COMP: 1527776e9dd9SFan Du /* IPCOMP spi is 16-bits. */ 1528776e9dd9SFan Du if (max >= 0x10000) 1529776e9dd9SFan Du return -EINVAL; 1530776e9dd9SFan Du break; 1531776e9dd9SFan Du 1532776e9dd9SFan Du default: 1533776e9dd9SFan Du return -EINVAL; 1534776e9dd9SFan Du } 1535776e9dd9SFan Du 1536776e9dd9SFan Du if (min > max) 1537776e9dd9SFan Du return -EINVAL; 1538776e9dd9SFan Du 1539776e9dd9SFan Du return 0; 1540776e9dd9SFan Du } 1541776e9dd9SFan Du EXPORT_SYMBOL(verify_spi_info); 1542776e9dd9SFan Du 1543658b219eSHerbert Xu int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) 15441da177e4SLinus Torvalds { 1545221df1edSAlexey Dobriyan struct net *net = xs_net(x); 1546f034b5d4SDavid S. Miller unsigned int h; 15471da177e4SLinus Torvalds struct xfrm_state *x0; 1548658b219eSHerbert Xu int err = -ENOENT; 1549658b219eSHerbert Xu __be32 minspi = htonl(low); 1550658b219eSHerbert Xu __be32 maxspi = htonl(high); 1551bd55775cSJamal Hadi Salim u32 mark = x->mark.v & x->mark.m; 15521da177e4SLinus Torvalds 1553658b219eSHerbert Xu spin_lock_bh(&x->lock); 1554658b219eSHerbert Xu if (x->km.state == XFRM_STATE_DEAD) 1555658b219eSHerbert Xu goto unlock; 1556658b219eSHerbert Xu 1557658b219eSHerbert Xu err = 0; 15581da177e4SLinus Torvalds if (x->id.spi) 1559658b219eSHerbert Xu goto unlock; 1560658b219eSHerbert Xu 1561658b219eSHerbert Xu err = -ENOENT; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds if (minspi == maxspi) { 1564bd55775cSJamal Hadi Salim x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family); 15651da177e4SLinus Torvalds if (x0) { 15661da177e4SLinus Torvalds xfrm_state_put(x0); 1567658b219eSHerbert Xu goto unlock; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds x->id.spi = minspi; 15701da177e4SLinus Torvalds } else { 15711da177e4SLinus Torvalds u32 spi = 0; 157226977b4eSAl Viro for (h = 0; h < high-low+1; h++) { 157363862b5bSAruna-Hewapathirane spi = low + prandom_u32()%(high-low+1); 1574bd55775cSJamal Hadi Salim x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family); 15751da177e4SLinus Torvalds if (x0 == NULL) { 15761da177e4SLinus Torvalds x->id.spi = htonl(spi); 15771da177e4SLinus Torvalds break; 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds xfrm_state_put(x0); 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds if (x->id.spi) { 1583283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 158412604d8aSAlexey Dobriyan h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); 158512604d8aSAlexey Dobriyan hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); 1586283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 1587658b219eSHerbert Xu 1588658b219eSHerbert Xu err = 0; 15891da177e4SLinus Torvalds } 1590658b219eSHerbert Xu 1591658b219eSHerbert Xu unlock: 1592658b219eSHerbert Xu spin_unlock_bh(&x->lock); 1593658b219eSHerbert Xu 1594658b219eSHerbert Xu return err; 15951da177e4SLinus Torvalds } 15961da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_alloc_spi); 15971da177e4SLinus Torvalds 1598d3623099SNicolas Dichtel static bool __xfrm_state_filter_match(struct xfrm_state *x, 1599870a2df4SNicolas Dichtel struct xfrm_address_filter *filter) 1600d3623099SNicolas Dichtel { 1601d3623099SNicolas Dichtel if (filter) { 1602d3623099SNicolas Dichtel if ((filter->family == AF_INET || 1603d3623099SNicolas Dichtel filter->family == AF_INET6) && 1604d3623099SNicolas Dichtel x->props.family != filter->family) 1605d3623099SNicolas Dichtel return false; 1606d3623099SNicolas Dichtel 1607d3623099SNicolas Dichtel return addr_match(&x->props.saddr, &filter->saddr, 1608d3623099SNicolas Dichtel filter->splen) && 1609d3623099SNicolas Dichtel addr_match(&x->id.daddr, &filter->daddr, 1610d3623099SNicolas Dichtel filter->dplen); 1611d3623099SNicolas Dichtel } 1612d3623099SNicolas Dichtel return true; 1613d3623099SNicolas Dichtel } 1614d3623099SNicolas Dichtel 1615284fa7daSAlexey Dobriyan int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, 16164c563f76STimo Teras int (*func)(struct xfrm_state *, int, void*), 16171da177e4SLinus Torvalds void *data) 16181da177e4SLinus Torvalds { 161912a169e7SHerbert Xu struct xfrm_state *state; 162012a169e7SHerbert Xu struct xfrm_state_walk *x; 16211da177e4SLinus Torvalds int err = 0; 16221da177e4SLinus Torvalds 162312a169e7SHerbert Xu if (walk->seq != 0 && list_empty(&walk->all)) 16244c563f76STimo Teras return 0; 16254c563f76STimo Teras 1626283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 162712a169e7SHerbert Xu if (list_empty(&walk->all)) 1628284fa7daSAlexey Dobriyan x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all); 162912a169e7SHerbert Xu else 163080077702SLi RongQing x = list_first_entry(&walk->all, struct xfrm_state_walk, all); 1631284fa7daSAlexey Dobriyan list_for_each_entry_from(x, &net->xfrm.state_all, all) { 163212a169e7SHerbert Xu if (x->state == XFRM_STATE_DEAD) 16334c563f76STimo Teras continue; 163412a169e7SHerbert Xu state = container_of(x, struct xfrm_state, km); 163512a169e7SHerbert Xu if (!xfrm_id_proto_match(state->id.proto, walk->proto)) 163694b9bb54SJamal Hadi Salim continue; 1637d3623099SNicolas Dichtel if (!__xfrm_state_filter_match(state, walk->filter)) 1638d3623099SNicolas Dichtel continue; 163912a169e7SHerbert Xu err = func(state, walk->seq, data); 16404c563f76STimo Teras if (err) { 164112a169e7SHerbert Xu list_move_tail(&walk->all, &x->all); 164294b9bb54SJamal Hadi Salim goto out; 164394b9bb54SJamal Hadi Salim } 164412a169e7SHerbert Xu walk->seq++; 16454c563f76STimo Teras } 164612a169e7SHerbert Xu if (walk->seq == 0) { 16471da177e4SLinus Torvalds err = -ENOENT; 16481da177e4SLinus Torvalds goto out; 16491da177e4SLinus Torvalds } 165012a169e7SHerbert Xu list_del_init(&walk->all); 16511da177e4SLinus Torvalds out: 1652283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 16531da177e4SLinus Torvalds return err; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_walk); 16561da177e4SLinus Torvalds 1657d3623099SNicolas Dichtel void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto, 1658870a2df4SNicolas Dichtel struct xfrm_address_filter *filter) 16595c182458SHerbert Xu { 166012a169e7SHerbert Xu INIT_LIST_HEAD(&walk->all); 16615c182458SHerbert Xu walk->proto = proto; 166212a169e7SHerbert Xu walk->state = XFRM_STATE_DEAD; 166312a169e7SHerbert Xu walk->seq = 0; 1664d3623099SNicolas Dichtel walk->filter = filter; 16655c182458SHerbert Xu } 16665c182458SHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_init); 16675c182458SHerbert Xu 1668283bc9f3SFan Du void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net) 1669abb81c4fSHerbert Xu { 1670d3623099SNicolas Dichtel kfree(walk->filter); 1671d3623099SNicolas Dichtel 167212a169e7SHerbert Xu if (list_empty(&walk->all)) 16735c182458SHerbert Xu return; 16745c182458SHerbert Xu 1675283bc9f3SFan Du spin_lock_bh(&net->xfrm.xfrm_state_lock); 167612a169e7SHerbert Xu list_del(&walk->all); 1677283bc9f3SFan Du spin_unlock_bh(&net->xfrm.xfrm_state_lock); 1678abb81c4fSHerbert Xu } 1679abb81c4fSHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_done); 1680abb81c4fSHerbert Xu 1681f8cd5488SJamal Hadi Salim static void xfrm_replay_timer_handler(unsigned long data) 1682f8cd5488SJamal Hadi Salim { 1683f8cd5488SJamal Hadi Salim struct xfrm_state *x = (struct xfrm_state *)data; 1684f8cd5488SJamal Hadi Salim 1685f8cd5488SJamal Hadi Salim spin_lock(&x->lock); 1686f8cd5488SJamal Hadi Salim 16872717096aSJamal Hadi Salim if (x->km.state == XFRM_STATE_VALID) { 1688a6483b79SAlexey Dobriyan if (xfrm_aevent_is_on(xs_net(x))) 16899fdc4883SSteffen Klassert x->repl->notify(x, XFRM_REPLAY_TIMEOUT); 16902717096aSJamal Hadi Salim else 16912717096aSJamal Hadi Salim x->xflags |= XFRM_TIME_DEFER; 16922717096aSJamal Hadi Salim } 1693f8cd5488SJamal Hadi Salim 1694f8cd5488SJamal Hadi Salim spin_unlock(&x->lock); 1695f8cd5488SJamal Hadi Salim } 1696f8cd5488SJamal Hadi Salim 1697df01812eSDenis Cheng static LIST_HEAD(xfrm_km_list); 16981da177e4SLinus Torvalds 1699214e005bSDavid S. Miller void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) 17001da177e4SLinus Torvalds { 17011da177e4SLinus Torvalds struct xfrm_mgr *km; 17021da177e4SLinus Torvalds 170385168c00SCong Wang rcu_read_lock(); 170485168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) 170526b15dadSJamal Hadi Salim if (km->notify_policy) 170626b15dadSJamal Hadi Salim km->notify_policy(xp, dir, c); 170785168c00SCong Wang rcu_read_unlock(); 170826b15dadSJamal Hadi Salim } 170926b15dadSJamal Hadi Salim 1710214e005bSDavid S. Miller void km_state_notify(struct xfrm_state *x, const struct km_event *c) 171126b15dadSJamal Hadi Salim { 171226b15dadSJamal Hadi Salim struct xfrm_mgr *km; 171385168c00SCong Wang rcu_read_lock(); 171485168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) 171526b15dadSJamal Hadi Salim if (km->notify) 171626b15dadSJamal Hadi Salim km->notify(x, c); 171785168c00SCong Wang rcu_read_unlock(); 171826b15dadSJamal Hadi Salim } 171926b15dadSJamal Hadi Salim 172026b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_policy_notify); 172126b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_state_notify); 172226b15dadSJamal Hadi Salim 172315e47304SEric W. Biederman void km_state_expired(struct xfrm_state *x, int hard, u32 portid) 172426b15dadSJamal Hadi Salim { 172526b15dadSJamal Hadi Salim struct km_event c; 172626b15dadSJamal Hadi Salim 1727bf08867fSHerbert Xu c.data.hard = hard; 172815e47304SEric W. Biederman c.portid = portid; 1729f60f6b8fSHerbert Xu c.event = XFRM_MSG_EXPIRE; 173026b15dadSJamal Hadi Salim km_state_notify(x, &c); 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 173353bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(km_state_expired); 173426b15dadSJamal Hadi Salim /* 173526b15dadSJamal Hadi Salim * We send to all registered managers regardless of failure 173626b15dadSJamal Hadi Salim * We are happy with one success 173726b15dadSJamal Hadi Salim */ 1738980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol) 17391da177e4SLinus Torvalds { 174026b15dadSJamal Hadi Salim int err = -EINVAL, acqret; 17411da177e4SLinus Torvalds struct xfrm_mgr *km; 17421da177e4SLinus Torvalds 174385168c00SCong Wang rcu_read_lock(); 174485168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 174565e0736bSFan Du acqret = km->acquire(x, t, pol); 174626b15dadSJamal Hadi Salim if (!acqret) 174726b15dadSJamal Hadi Salim err = acqret; 17481da177e4SLinus Torvalds } 174985168c00SCong Wang rcu_read_unlock(); 17501da177e4SLinus Torvalds return err; 17511da177e4SLinus Torvalds } 1752980ebd25SJamal Hadi Salim EXPORT_SYMBOL(km_query); 17531da177e4SLinus Torvalds 17545d36b180SAl Viro int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) 17551da177e4SLinus Torvalds { 17561da177e4SLinus Torvalds int err = -EINVAL; 17571da177e4SLinus Torvalds struct xfrm_mgr *km; 17581da177e4SLinus Torvalds 175985168c00SCong Wang rcu_read_lock(); 176085168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 17611da177e4SLinus Torvalds if (km->new_mapping) 17621da177e4SLinus Torvalds err = km->new_mapping(x, ipaddr, sport); 17631da177e4SLinus Torvalds if (!err) 17641da177e4SLinus Torvalds break; 17651da177e4SLinus Torvalds } 176685168c00SCong Wang rcu_read_unlock(); 17671da177e4SLinus Torvalds return err; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds EXPORT_SYMBOL(km_new_mapping); 17701da177e4SLinus Torvalds 177115e47304SEric W. Biederman void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid) 17721da177e4SLinus Torvalds { 177326b15dadSJamal Hadi Salim struct km_event c; 17741da177e4SLinus Torvalds 1775bf08867fSHerbert Xu c.data.hard = hard; 177615e47304SEric W. Biederman c.portid = portid; 1777f60f6b8fSHerbert Xu c.event = XFRM_MSG_POLEXPIRE; 177826b15dadSJamal Hadi Salim km_policy_notify(pol, dir, &c); 17791da177e4SLinus Torvalds } 1780a70fcb0bSDavid S. Miller EXPORT_SYMBOL(km_policy_expired); 17811da177e4SLinus Torvalds 17822d60abc2SEric Dumazet #ifdef CONFIG_XFRM_MIGRATE 1783183cad12SDavid S. Miller int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, 1784183cad12SDavid S. Miller const struct xfrm_migrate *m, int num_migrate, 1785183cad12SDavid S. Miller const struct xfrm_kmaddress *k) 178680c9abaaSShinta Sugimoto { 178780c9abaaSShinta Sugimoto int err = -EINVAL; 178880c9abaaSShinta Sugimoto int ret; 178980c9abaaSShinta Sugimoto struct xfrm_mgr *km; 179080c9abaaSShinta Sugimoto 179185168c00SCong Wang rcu_read_lock(); 179285168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 179380c9abaaSShinta Sugimoto if (km->migrate) { 179413c1d189SArnaud Ebalard ret = km->migrate(sel, dir, type, m, num_migrate, k); 179580c9abaaSShinta Sugimoto if (!ret) 179680c9abaaSShinta Sugimoto err = ret; 179780c9abaaSShinta Sugimoto } 179880c9abaaSShinta Sugimoto } 179985168c00SCong Wang rcu_read_unlock(); 180080c9abaaSShinta Sugimoto return err; 180180c9abaaSShinta Sugimoto } 180280c9abaaSShinta Sugimoto EXPORT_SYMBOL(km_migrate); 18032d60abc2SEric Dumazet #endif 180480c9abaaSShinta Sugimoto 1805db983c11SAlexey Dobriyan int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr) 180697a64b45SMasahide NAKAMURA { 180797a64b45SMasahide NAKAMURA int err = -EINVAL; 180897a64b45SMasahide NAKAMURA int ret; 180997a64b45SMasahide NAKAMURA struct xfrm_mgr *km; 181097a64b45SMasahide NAKAMURA 181185168c00SCong Wang rcu_read_lock(); 181285168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 181397a64b45SMasahide NAKAMURA if (km->report) { 1814db983c11SAlexey Dobriyan ret = km->report(net, proto, sel, addr); 181597a64b45SMasahide NAKAMURA if (!ret) 181697a64b45SMasahide NAKAMURA err = ret; 181797a64b45SMasahide NAKAMURA } 181897a64b45SMasahide NAKAMURA } 181985168c00SCong Wang rcu_read_unlock(); 182097a64b45SMasahide NAKAMURA return err; 182197a64b45SMasahide NAKAMURA } 182297a64b45SMasahide NAKAMURA EXPORT_SYMBOL(km_report); 182397a64b45SMasahide NAKAMURA 18240f24558eSHoria Geanta bool km_is_alive(const struct km_event *c) 18250f24558eSHoria Geanta { 18260f24558eSHoria Geanta struct xfrm_mgr *km; 18270f24558eSHoria Geanta bool is_alive = false; 18280f24558eSHoria Geanta 18290f24558eSHoria Geanta rcu_read_lock(); 18300f24558eSHoria Geanta list_for_each_entry_rcu(km, &xfrm_km_list, list) { 18310f24558eSHoria Geanta if (km->is_alive && km->is_alive(c)) { 18320f24558eSHoria Geanta is_alive = true; 18330f24558eSHoria Geanta break; 18340f24558eSHoria Geanta } 18350f24558eSHoria Geanta } 18360f24558eSHoria Geanta rcu_read_unlock(); 18370f24558eSHoria Geanta 18380f24558eSHoria Geanta return is_alive; 18390f24558eSHoria Geanta } 18400f24558eSHoria Geanta EXPORT_SYMBOL(km_is_alive); 18410f24558eSHoria Geanta 18421da177e4SLinus Torvalds int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) 18431da177e4SLinus Torvalds { 18441da177e4SLinus Torvalds int err; 18451da177e4SLinus Torvalds u8 *data; 18461da177e4SLinus Torvalds struct xfrm_mgr *km; 18471da177e4SLinus Torvalds struct xfrm_policy *pol = NULL; 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds if (optlen <= 0 || optlen > PAGE_SIZE) 18501da177e4SLinus Torvalds return -EMSGSIZE; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds data = kmalloc(optlen, GFP_KERNEL); 18531da177e4SLinus Torvalds if (!data) 18541da177e4SLinus Torvalds return -ENOMEM; 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds err = -EFAULT; 18571da177e4SLinus Torvalds if (copy_from_user(data, optval, optlen)) 18581da177e4SLinus Torvalds goto out; 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds err = -EINVAL; 186185168c00SCong Wang rcu_read_lock(); 186285168c00SCong Wang list_for_each_entry_rcu(km, &xfrm_km_list, list) { 1863cb969f07SVenkat Yekkirala pol = km->compile_policy(sk, optname, data, 18641da177e4SLinus Torvalds optlen, &err); 18651da177e4SLinus Torvalds if (err >= 0) 18661da177e4SLinus Torvalds break; 18671da177e4SLinus Torvalds } 186885168c00SCong Wang rcu_read_unlock(); 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds if (err >= 0) { 18711da177e4SLinus Torvalds xfrm_sk_policy_insert(sk, err, pol); 18721da177e4SLinus Torvalds xfrm_pol_put(pol); 18731da177e4SLinus Torvalds err = 0; 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds out: 18771da177e4SLinus Torvalds kfree(data); 18781da177e4SLinus Torvalds return err; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_user_policy); 18811da177e4SLinus Torvalds 188285168c00SCong Wang static DEFINE_SPINLOCK(xfrm_km_lock); 188385168c00SCong Wang 18841da177e4SLinus Torvalds int xfrm_register_km(struct xfrm_mgr *km) 18851da177e4SLinus Torvalds { 188685168c00SCong Wang spin_lock_bh(&xfrm_km_lock); 188785168c00SCong Wang list_add_tail_rcu(&km->list, &xfrm_km_list); 188885168c00SCong Wang spin_unlock_bh(&xfrm_km_lock); 18891da177e4SLinus Torvalds return 0; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_register_km); 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds int xfrm_unregister_km(struct xfrm_mgr *km) 18941da177e4SLinus Torvalds { 189585168c00SCong Wang spin_lock_bh(&xfrm_km_lock); 189685168c00SCong Wang list_del_rcu(&km->list); 189785168c00SCong Wang spin_unlock_bh(&xfrm_km_lock); 189885168c00SCong Wang synchronize_rcu(); 18991da177e4SLinus Torvalds return 0; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_unregister_km); 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo) 19041da177e4SLinus Torvalds { 19051da177e4SLinus Torvalds int err = 0; 19061da177e4SLinus Torvalds if (unlikely(afinfo == NULL)) 19071da177e4SLinus Torvalds return -EINVAL; 19081da177e4SLinus Torvalds if (unlikely(afinfo->family >= NPROTO)) 19091da177e4SLinus Torvalds return -EAFNOSUPPORT; 191044abdc30SCong Wang spin_lock_bh(&xfrm_state_afinfo_lock); 19111da177e4SLinus Torvalds if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL)) 1912f31e8d4fSLi RongQing err = -EEXIST; 1913edcd5821SDavid S. Miller else 191444abdc30SCong Wang rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo); 191544abdc30SCong Wang spin_unlock_bh(&xfrm_state_afinfo_lock); 19161da177e4SLinus Torvalds return err; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_register_afinfo); 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo) 19211da177e4SLinus Torvalds { 19221da177e4SLinus Torvalds int err = 0; 19231da177e4SLinus Torvalds if (unlikely(afinfo == NULL)) 19241da177e4SLinus Torvalds return -EINVAL; 19251da177e4SLinus Torvalds if (unlikely(afinfo->family >= NPROTO)) 19261da177e4SLinus Torvalds return -EAFNOSUPPORT; 192744abdc30SCong Wang spin_lock_bh(&xfrm_state_afinfo_lock); 19281da177e4SLinus Torvalds if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) { 19291da177e4SLinus Torvalds if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo)) 19301da177e4SLinus Torvalds err = -EINVAL; 1931edcd5821SDavid S. Miller else 193244abdc30SCong Wang RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL); 19331da177e4SLinus Torvalds } 193444abdc30SCong Wang spin_unlock_bh(&xfrm_state_afinfo_lock); 193544abdc30SCong Wang synchronize_rcu(); 19361da177e4SLinus Torvalds return err; 19371da177e4SLinus Torvalds } 19381da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_unregister_afinfo); 19391da177e4SLinus Torvalds 1940628e341fSHannes Frederic Sowa struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family) 19411da177e4SLinus Torvalds { 19421da177e4SLinus Torvalds struct xfrm_state_afinfo *afinfo; 19431da177e4SLinus Torvalds if (unlikely(family >= NPROTO)) 19441da177e4SLinus Torvalds return NULL; 194544abdc30SCong Wang rcu_read_lock(); 194644abdc30SCong Wang afinfo = rcu_dereference(xfrm_state_afinfo[family]); 1947546be240SHerbert Xu if (unlikely(!afinfo)) 194844abdc30SCong Wang rcu_read_unlock(); 19491da177e4SLinus Torvalds return afinfo; 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds 1952628e341fSHannes Frederic Sowa void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo) 19531da177e4SLinus Torvalds { 195444abdc30SCong Wang rcu_read_unlock(); 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */ 19581da177e4SLinus Torvalds void xfrm_state_delete_tunnel(struct xfrm_state *x) 19591da177e4SLinus Torvalds { 19601da177e4SLinus Torvalds if (x->tunnel) { 19611da177e4SLinus Torvalds struct xfrm_state *t = x->tunnel; 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds if (atomic_read(&t->tunnel_users) == 2) 19641da177e4SLinus Torvalds xfrm_state_delete(t); 19651da177e4SLinus Torvalds atomic_dec(&t->tunnel_users); 19661da177e4SLinus Torvalds xfrm_state_put(t); 19671da177e4SLinus Torvalds x->tunnel = NULL; 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete_tunnel); 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds int xfrm_state_mtu(struct xfrm_state *x, int mtu) 19731da177e4SLinus Torvalds { 1974c5c25238SPatrick McHardy int res; 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds spin_lock_bh(&x->lock); 19771da177e4SLinus Torvalds if (x->km.state == XFRM_STATE_VALID && 1978c5c25238SPatrick McHardy x->type && x->type->get_mtu) 1979c5c25238SPatrick McHardy res = x->type->get_mtu(x, mtu); 19801da177e4SLinus Torvalds else 198128121617SPatrick McHardy res = mtu - x->props.header_len; 19821da177e4SLinus Torvalds spin_unlock_bh(&x->lock); 19831da177e4SLinus Torvalds return res; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 1986a454f0ccSWei Yongjun int __xfrm_init_state(struct xfrm_state *x, bool init_replay) 198772cb6962SHerbert Xu { 1988d094cd83SHerbert Xu struct xfrm_state_afinfo *afinfo; 1989df9dcb45SKazunori MIYAZAWA struct xfrm_mode *inner_mode; 1990d094cd83SHerbert Xu int family = x->props.family; 199172cb6962SHerbert Xu int err; 199272cb6962SHerbert Xu 1993d094cd83SHerbert Xu err = -EAFNOSUPPORT; 1994d094cd83SHerbert Xu afinfo = xfrm_state_get_afinfo(family); 1995d094cd83SHerbert Xu if (!afinfo) 1996d094cd83SHerbert Xu goto error; 1997d094cd83SHerbert Xu 1998d094cd83SHerbert Xu err = 0; 1999d094cd83SHerbert Xu if (afinfo->init_flags) 2000d094cd83SHerbert Xu err = afinfo->init_flags(x); 2001d094cd83SHerbert Xu 2002d094cd83SHerbert Xu xfrm_state_put_afinfo(afinfo); 2003d094cd83SHerbert Xu 2004d094cd83SHerbert Xu if (err) 2005d094cd83SHerbert Xu goto error; 2006d094cd83SHerbert Xu 2007d094cd83SHerbert Xu err = -EPROTONOSUPPORT; 2008df9dcb45SKazunori MIYAZAWA 2009df9dcb45SKazunori MIYAZAWA if (x->sel.family != AF_UNSPEC) { 2010df9dcb45SKazunori MIYAZAWA inner_mode = xfrm_get_mode(x->props.mode, x->sel.family); 2011df9dcb45SKazunori MIYAZAWA if (inner_mode == NULL) 201213996378SHerbert Xu goto error; 201313996378SHerbert Xu 2014df9dcb45SKazunori MIYAZAWA if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) && 2015df9dcb45SKazunori MIYAZAWA family != x->sel.family) { 2016df9dcb45SKazunori MIYAZAWA xfrm_put_mode(inner_mode); 201713996378SHerbert Xu goto error; 2018df9dcb45SKazunori MIYAZAWA } 2019df9dcb45SKazunori MIYAZAWA 2020df9dcb45SKazunori MIYAZAWA x->inner_mode = inner_mode; 2021df9dcb45SKazunori MIYAZAWA } else { 2022df9dcb45SKazunori MIYAZAWA struct xfrm_mode *inner_mode_iaf; 2023d81d2285SMartin Willi int iafamily = AF_INET; 2024df9dcb45SKazunori MIYAZAWA 2025d81d2285SMartin Willi inner_mode = xfrm_get_mode(x->props.mode, x->props.family); 2026df9dcb45SKazunori MIYAZAWA if (inner_mode == NULL) 2027df9dcb45SKazunori MIYAZAWA goto error; 2028df9dcb45SKazunori MIYAZAWA 2029df9dcb45SKazunori MIYAZAWA if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) { 2030df9dcb45SKazunori MIYAZAWA xfrm_put_mode(inner_mode); 2031df9dcb45SKazunori MIYAZAWA goto error; 2032df9dcb45SKazunori MIYAZAWA } 2033df9dcb45SKazunori MIYAZAWA x->inner_mode = inner_mode; 2034d81d2285SMartin Willi 2035d81d2285SMartin Willi if (x->props.family == AF_INET) 2036d81d2285SMartin Willi iafamily = AF_INET6; 2037d81d2285SMartin Willi 2038d81d2285SMartin Willi inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily); 2039d81d2285SMartin Willi if (inner_mode_iaf) { 2040d81d2285SMartin Willi if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL) 2041df9dcb45SKazunori MIYAZAWA x->inner_mode_iaf = inner_mode_iaf; 2042d81d2285SMartin Willi else 2043d81d2285SMartin Willi xfrm_put_mode(inner_mode_iaf); 2044df9dcb45SKazunori MIYAZAWA } 2045df9dcb45SKazunori MIYAZAWA } 204613996378SHerbert Xu 2047d094cd83SHerbert Xu x->type = xfrm_get_type(x->id.proto, family); 204872cb6962SHerbert Xu if (x->type == NULL) 204972cb6962SHerbert Xu goto error; 205072cb6962SHerbert Xu 205172cb6962SHerbert Xu err = x->type->init_state(x); 205272cb6962SHerbert Xu if (err) 205372cb6962SHerbert Xu goto error; 205472cb6962SHerbert Xu 205513996378SHerbert Xu x->outer_mode = xfrm_get_mode(x->props.mode, family); 2056599901c3SJulia Lawall if (x->outer_mode == NULL) { 2057599901c3SJulia Lawall err = -EPROTONOSUPPORT; 2058b59f45d0SHerbert Xu goto error; 2059599901c3SJulia Lawall } 2060b59f45d0SHerbert Xu 2061a454f0ccSWei Yongjun if (init_replay) { 2062a454f0ccSWei Yongjun err = xfrm_init_replay(x); 2063a454f0ccSWei Yongjun if (err) 2064a454f0ccSWei Yongjun goto error; 2065a454f0ccSWei Yongjun } 2066a454f0ccSWei Yongjun 206772cb6962SHerbert Xu x->km.state = XFRM_STATE_VALID; 206872cb6962SHerbert Xu 206972cb6962SHerbert Xu error: 207072cb6962SHerbert Xu return err; 207172cb6962SHerbert Xu } 207272cb6962SHerbert Xu 2073a454f0ccSWei Yongjun EXPORT_SYMBOL(__xfrm_init_state); 2074a454f0ccSWei Yongjun 2075a454f0ccSWei Yongjun int xfrm_init_state(struct xfrm_state *x) 2076a454f0ccSWei Yongjun { 2077a454f0ccSWei Yongjun return __xfrm_init_state(x, true); 2078a454f0ccSWei Yongjun } 2079a454f0ccSWei Yongjun 208072cb6962SHerbert Xu EXPORT_SYMBOL(xfrm_init_state); 20811da177e4SLinus Torvalds 2082d62ddc21SAlexey Dobriyan int __net_init xfrm_state_init(struct net *net) 20831da177e4SLinus Torvalds { 2084f034b5d4SDavid S. Miller unsigned int sz; 20851da177e4SLinus Torvalds 20869d4139c7SAlexey Dobriyan INIT_LIST_HEAD(&net->xfrm.state_all); 20879d4139c7SAlexey Dobriyan 2088f034b5d4SDavid S. Miller sz = sizeof(struct hlist_head) * 8; 2089f034b5d4SDavid S. Miller 209073d189dcSAlexey Dobriyan net->xfrm.state_bydst = xfrm_hash_alloc(sz); 209173d189dcSAlexey Dobriyan if (!net->xfrm.state_bydst) 209273d189dcSAlexey Dobriyan goto out_bydst; 2093d320bbb3SAlexey Dobriyan net->xfrm.state_bysrc = xfrm_hash_alloc(sz); 2094d320bbb3SAlexey Dobriyan if (!net->xfrm.state_bysrc) 2095d320bbb3SAlexey Dobriyan goto out_bysrc; 2096b754a4fdSAlexey Dobriyan net->xfrm.state_byspi = xfrm_hash_alloc(sz); 2097b754a4fdSAlexey Dobriyan if (!net->xfrm.state_byspi) 2098b754a4fdSAlexey Dobriyan goto out_byspi; 2099529983ecSAlexey Dobriyan net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1); 2100f034b5d4SDavid S. Miller 21010bf7c5b0SAlexey Dobriyan net->xfrm.state_num = 0; 210263082733SAlexey Dobriyan INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize); 2103b8a0ae20SAlexey Dobriyan INIT_HLIST_HEAD(&net->xfrm.state_gc_list); 2104c7837144SAlexey Dobriyan INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task); 2105283bc9f3SFan Du spin_lock_init(&net->xfrm.xfrm_state_lock); 2106d62ddc21SAlexey Dobriyan return 0; 210773d189dcSAlexey Dobriyan 2108b754a4fdSAlexey Dobriyan out_byspi: 2109b754a4fdSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bysrc, sz); 2110d320bbb3SAlexey Dobriyan out_bysrc: 2111d320bbb3SAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bydst, sz); 211273d189dcSAlexey Dobriyan out_bydst: 211373d189dcSAlexey Dobriyan return -ENOMEM; 2114d62ddc21SAlexey Dobriyan } 2115d62ddc21SAlexey Dobriyan 2116d62ddc21SAlexey Dobriyan void xfrm_state_fini(struct net *net) 2117d62ddc21SAlexey Dobriyan { 211873d189dcSAlexey Dobriyan unsigned int sz; 211973d189dcSAlexey Dobriyan 21207c2776eeSAlexey Dobriyan flush_work(&net->xfrm.state_hash_work); 21212e71029eSTetsuo Handa xfrm_state_flush(net, IPSEC_PROTO_ANY, false); 21227c2776eeSAlexey Dobriyan flush_work(&net->xfrm.state_gc_work); 21237c2776eeSAlexey Dobriyan 21249d4139c7SAlexey Dobriyan WARN_ON(!list_empty(&net->xfrm.state_all)); 212573d189dcSAlexey Dobriyan 2126529983ecSAlexey Dobriyan sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head); 2127b754a4fdSAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_byspi)); 2128b754a4fdSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_byspi, sz); 2129d320bbb3SAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_bysrc)); 2130d320bbb3SAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bysrc, sz); 213173d189dcSAlexey Dobriyan WARN_ON(!hlist_empty(net->xfrm.state_bydst)); 213273d189dcSAlexey Dobriyan xfrm_hash_free(net->xfrm.state_bydst, sz); 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 2135ab5f5e8bSJoy Latten #ifdef CONFIG_AUDITSYSCALL 2136cf35f43eSIlpo Järvinen static void xfrm_audit_helper_sainfo(struct xfrm_state *x, 2137ab5f5e8bSJoy Latten struct audit_buffer *audit_buf) 2138ab5f5e8bSJoy Latten { 213968277accSPaul Moore struct xfrm_sec_ctx *ctx = x->security; 214068277accSPaul Moore u32 spi = ntohl(x->id.spi); 214168277accSPaul Moore 214268277accSPaul Moore if (ctx) 2143ab5f5e8bSJoy Latten audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", 214468277accSPaul Moore ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); 2145ab5f5e8bSJoy Latten 2146ab5f5e8bSJoy Latten switch (x->props.family) { 2147ab5f5e8bSJoy Latten case AF_INET: 214821454aaaSHarvey Harrison audit_log_format(audit_buf, " src=%pI4 dst=%pI4", 214921454aaaSHarvey Harrison &x->props.saddr.a4, &x->id.daddr.a4); 2150ab5f5e8bSJoy Latten break; 2151ab5f5e8bSJoy Latten case AF_INET6: 21525b095d98SHarvey Harrison audit_log_format(audit_buf, " src=%pI6 dst=%pI6", 2153fdb46ee7SHarvey Harrison x->props.saddr.a6, x->id.daddr.a6); 2154ab5f5e8bSJoy Latten break; 2155ab5f5e8bSJoy Latten } 215668277accSPaul Moore 215768277accSPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); 2158ab5f5e8bSJoy Latten } 2159ab5f5e8bSJoy Latten 2160cf35f43eSIlpo Järvinen static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family, 2161afeb14b4SPaul Moore struct audit_buffer *audit_buf) 2162afeb14b4SPaul Moore { 2163b71d1d42SEric Dumazet const struct iphdr *iph4; 2164b71d1d42SEric Dumazet const struct ipv6hdr *iph6; 2165afeb14b4SPaul Moore 2166afeb14b4SPaul Moore switch (family) { 2167afeb14b4SPaul Moore case AF_INET: 2168afeb14b4SPaul Moore iph4 = ip_hdr(skb); 216921454aaaSHarvey Harrison audit_log_format(audit_buf, " src=%pI4 dst=%pI4", 217021454aaaSHarvey Harrison &iph4->saddr, &iph4->daddr); 2171afeb14b4SPaul Moore break; 2172afeb14b4SPaul Moore case AF_INET6: 2173afeb14b4SPaul Moore iph6 = ipv6_hdr(skb); 2174afeb14b4SPaul Moore audit_log_format(audit_buf, 21755b095d98SHarvey Harrison " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x", 2176fdb46ee7SHarvey Harrison &iph6->saddr, &iph6->daddr, 2177afeb14b4SPaul Moore iph6->flow_lbl[0] & 0x0f, 2178afeb14b4SPaul Moore iph6->flow_lbl[1], 2179afeb14b4SPaul Moore iph6->flow_lbl[2]); 2180afeb14b4SPaul Moore break; 2181afeb14b4SPaul Moore } 2182afeb14b4SPaul Moore } 2183afeb14b4SPaul Moore 21842e71029eSTetsuo Handa void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid) 2185ab5f5e8bSJoy Latten { 2186ab5f5e8bSJoy Latten struct audit_buffer *audit_buf; 2187ab5f5e8bSJoy Latten 2188afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SAD-add"); 2189ab5f5e8bSJoy Latten if (audit_buf == NULL) 2190ab5f5e8bSJoy Latten return; 21912e71029eSTetsuo Handa xfrm_audit_helper_usrinfo(task_valid, audit_buf); 2192afeb14b4SPaul Moore xfrm_audit_helper_sainfo(x, audit_buf); 2193afeb14b4SPaul Moore audit_log_format(audit_buf, " res=%u", result); 2194ab5f5e8bSJoy Latten audit_log_end(audit_buf); 2195ab5f5e8bSJoy Latten } 2196ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_add); 2197ab5f5e8bSJoy Latten 21982e71029eSTetsuo Handa void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid) 2199ab5f5e8bSJoy Latten { 2200ab5f5e8bSJoy Latten struct audit_buffer *audit_buf; 2201ab5f5e8bSJoy Latten 2202afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SAD-delete"); 2203ab5f5e8bSJoy Latten if (audit_buf == NULL) 2204ab5f5e8bSJoy Latten return; 22052e71029eSTetsuo Handa xfrm_audit_helper_usrinfo(task_valid, audit_buf); 2206afeb14b4SPaul Moore xfrm_audit_helper_sainfo(x, audit_buf); 2207afeb14b4SPaul Moore audit_log_format(audit_buf, " res=%u", result); 2208ab5f5e8bSJoy Latten audit_log_end(audit_buf); 2209ab5f5e8bSJoy Latten } 2210ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_delete); 2211afeb14b4SPaul Moore 2212afeb14b4SPaul Moore void xfrm_audit_state_replay_overflow(struct xfrm_state *x, 2213afeb14b4SPaul Moore struct sk_buff *skb) 2214afeb14b4SPaul Moore { 2215afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2216afeb14b4SPaul Moore u32 spi; 2217afeb14b4SPaul Moore 2218afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-replay-overflow"); 2219afeb14b4SPaul Moore if (audit_buf == NULL) 2220afeb14b4SPaul Moore return; 2221afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2222afeb14b4SPaul Moore /* don't record the sequence number because it's inherent in this kind 2223afeb14b4SPaul Moore * of audit message */ 2224afeb14b4SPaul Moore spi = ntohl(x->id.spi); 2225afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); 2226afeb14b4SPaul Moore audit_log_end(audit_buf); 2227afeb14b4SPaul Moore } 2228afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow); 2229afeb14b4SPaul Moore 22309fdc4883SSteffen Klassert void xfrm_audit_state_replay(struct xfrm_state *x, 2231afeb14b4SPaul Moore struct sk_buff *skb, __be32 net_seq) 2232afeb14b4SPaul Moore { 2233afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2234afeb14b4SPaul Moore u32 spi; 2235afeb14b4SPaul Moore 2236afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-replayed-pkt"); 2237afeb14b4SPaul Moore if (audit_buf == NULL) 2238afeb14b4SPaul Moore return; 2239afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2240afeb14b4SPaul Moore spi = ntohl(x->id.spi); 2241afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2242afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2243afeb14b4SPaul Moore audit_log_end(audit_buf); 2244afeb14b4SPaul Moore } 22459fdc4883SSteffen Klassert EXPORT_SYMBOL_GPL(xfrm_audit_state_replay); 2246afeb14b4SPaul Moore 2247afeb14b4SPaul Moore void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family) 2248afeb14b4SPaul Moore { 2249afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2250afeb14b4SPaul Moore 2251afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-notfound"); 2252afeb14b4SPaul Moore if (audit_buf == NULL) 2253afeb14b4SPaul Moore return; 2254afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, family, audit_buf); 2255afeb14b4SPaul Moore audit_log_end(audit_buf); 2256afeb14b4SPaul Moore } 2257afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple); 2258afeb14b4SPaul Moore 2259afeb14b4SPaul Moore void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, 2260afeb14b4SPaul Moore __be32 net_spi, __be32 net_seq) 2261afeb14b4SPaul Moore { 2262afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2263afeb14b4SPaul Moore u32 spi; 2264afeb14b4SPaul Moore 2265afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-notfound"); 2266afeb14b4SPaul Moore if (audit_buf == NULL) 2267afeb14b4SPaul Moore return; 2268afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, family, audit_buf); 2269afeb14b4SPaul Moore spi = ntohl(net_spi); 2270afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2271afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2272afeb14b4SPaul Moore audit_log_end(audit_buf); 2273afeb14b4SPaul Moore } 2274afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound); 2275afeb14b4SPaul Moore 2276afeb14b4SPaul Moore void xfrm_audit_state_icvfail(struct xfrm_state *x, 2277afeb14b4SPaul Moore struct sk_buff *skb, u8 proto) 2278afeb14b4SPaul Moore { 2279afeb14b4SPaul Moore struct audit_buffer *audit_buf; 2280afeb14b4SPaul Moore __be32 net_spi; 2281afeb14b4SPaul Moore __be32 net_seq; 2282afeb14b4SPaul Moore 2283afeb14b4SPaul Moore audit_buf = xfrm_audit_start("SA-icv-failure"); 2284afeb14b4SPaul Moore if (audit_buf == NULL) 2285afeb14b4SPaul Moore return; 2286afeb14b4SPaul Moore xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); 2287afeb14b4SPaul Moore if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) { 2288afeb14b4SPaul Moore u32 spi = ntohl(net_spi); 2289afeb14b4SPaul Moore audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", 2290afeb14b4SPaul Moore spi, spi, ntohl(net_seq)); 2291afeb14b4SPaul Moore } 2292afeb14b4SPaul Moore audit_log_end(audit_buf); 2293afeb14b4SPaul Moore } 2294afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail); 2295ab5f5e8bSJoy Latten #endif /* CONFIG_AUDITSYSCALL */ 2296