Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm_state.c |
| 3 | * |
| 4 | * Changes: |
| 5 | * Mitsuru KANDA @USAGI |
| 6 | * Kazunori MIYAZAWA @USAGI |
| 7 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 8 | * IPv6 support |
| 9 | * YOSHIFUJI Hideaki @USAGI |
| 10 | * Split up af-specific functions |
| 11 | * Derek Atkins <derek@ihtfp.com> |
| 12 | * Add UDP Encapsulation |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/workqueue.h> |
| 17 | #include <net/xfrm.h> |
| 18 | #include <linux/pfkeyv2.h> |
| 19 | #include <linux/ipsec.h> |
| 20 | #include <linux/module.h> |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 21 | #include <linux/cache.h> |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 22 | #include <linux/audit.h> |
Jesper Juhl | b5890d8 | 2007-08-10 15:20:21 -0700 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 24 | #include <linux/ktime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 29 | #include "xfrm_hash.h" |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* Each xfrm_state may be linked to two tables: |
| 32 | |
| 33 | 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl) |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 34 | 2. Hash table by (daddr,family,reqid) to find what SAs exist for given |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | destination/tunnel endpoint. (output) |
| 36 | */ |
| 37 | |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 38 | static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Alexey Dobriyan | 64d0cd0 | 2008-11-25 17:29:21 -0800 | [diff] [blame] | 40 | static inline unsigned int xfrm_dst_hash(struct net *net, |
David S. Miller | 2ab3850 | 2011-02-24 01:47:16 -0500 | [diff] [blame] | 41 | const xfrm_address_t *daddr, |
| 42 | const xfrm_address_t *saddr, |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 43 | u32 reqid, |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 44 | unsigned short family) |
| 45 | { |
Alexey Dobriyan | 64d0cd0 | 2008-11-25 17:29:21 -0800 | [diff] [blame] | 46 | return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask); |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Alexey Dobriyan | 64d0cd0 | 2008-11-25 17:29:21 -0800 | [diff] [blame] | 49 | static inline unsigned int xfrm_src_hash(struct net *net, |
David S. Miller | 2ab3850 | 2011-02-24 01:47:16 -0500 | [diff] [blame] | 50 | const xfrm_address_t *daddr, |
| 51 | const xfrm_address_t *saddr, |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 52 | unsigned short family) |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 53 | { |
Alexey Dobriyan | 64d0cd0 | 2008-11-25 17:29:21 -0800 | [diff] [blame] | 54 | return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 55 | } |
| 56 | |
David S. Miller | 2575b65 | 2006-08-24 03:26:44 -0700 | [diff] [blame] | 57 | static inline unsigned int |
David S. Miller | 2ab3850 | 2011-02-24 01:47:16 -0500 | [diff] [blame] | 58 | xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr, |
| 59 | __be32 spi, u8 proto, unsigned short family) |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 60 | { |
Alexey Dobriyan | 64d0cd0 | 2008-11-25 17:29:21 -0800 | [diff] [blame] | 61 | return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 62 | } |
| 63 | |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 64 | static void xfrm_hash_transfer(struct hlist_head *list, |
| 65 | struct hlist_head *ndsttable, |
| 66 | struct hlist_head *nsrctable, |
| 67 | struct hlist_head *nspitable, |
| 68 | unsigned int nhashmask) |
| 69 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 70 | struct hlist_node *tmp; |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 71 | struct xfrm_state *x; |
| 72 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 73 | hlist_for_each_entry_safe(x, tmp, list, bydst) { |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 74 | unsigned int h; |
| 75 | |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 76 | h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr, |
| 77 | x->props.reqid, x->props.family, |
| 78 | nhashmask); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 79 | hlist_add_head(&x->bydst, ndsttable+h); |
| 80 | |
Masahide NAKAMURA | 667bbcb | 2006-10-03 15:56:09 -0700 | [diff] [blame] | 81 | h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr, |
| 82 | x->props.family, |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 83 | nhashmask); |
| 84 | hlist_add_head(&x->bysrc, nsrctable+h); |
| 85 | |
Masahide NAKAMURA | 7b4dc360 | 2006-09-27 22:21:52 -0700 | [diff] [blame] | 86 | if (x->id.spi) { |
| 87 | h = __xfrm_spi_hash(&x->id.daddr, x->id.spi, |
| 88 | x->id.proto, x->props.family, |
| 89 | nhashmask); |
| 90 | hlist_add_head(&x->byspi, nspitable+h); |
| 91 | } |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 95 | static unsigned long xfrm_hash_new_size(unsigned int state_hmask) |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 96 | { |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 97 | return ((state_hmask + 1) << 1) * sizeof(struct hlist_head); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static DEFINE_MUTEX(hash_resize_mutex); |
| 101 | |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 102 | static void xfrm_hash_resize(struct work_struct *work) |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 103 | { |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 104 | struct net *net = container_of(work, struct net, xfrm.state_hash_work); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 105 | struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi; |
| 106 | unsigned long nsize, osize; |
| 107 | unsigned int nhashmask, ohashmask; |
| 108 | int i; |
| 109 | |
| 110 | mutex_lock(&hash_resize_mutex); |
| 111 | |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 112 | nsize = xfrm_hash_new_size(net->xfrm.state_hmask); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 113 | ndst = xfrm_hash_alloc(nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 114 | if (!ndst) |
| 115 | goto out_unlock; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 116 | nsrc = xfrm_hash_alloc(nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 117 | if (!nsrc) { |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 118 | xfrm_hash_free(ndst, nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 119 | goto out_unlock; |
| 120 | } |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 121 | nspi = xfrm_hash_alloc(nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 122 | if (!nspi) { |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 123 | xfrm_hash_free(ndst, nsize); |
| 124 | xfrm_hash_free(nsrc, nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 125 | goto out_unlock; |
| 126 | } |
| 127 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 128 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 129 | |
| 130 | nhashmask = (nsize / sizeof(struct hlist_head)) - 1U; |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 131 | for (i = net->xfrm.state_hmask; i >= 0; i--) |
| 132 | xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi, |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 133 | nhashmask); |
| 134 | |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 135 | odst = net->xfrm.state_bydst; |
| 136 | osrc = net->xfrm.state_bysrc; |
| 137 | ospi = net->xfrm.state_byspi; |
| 138 | ohashmask = net->xfrm.state_hmask; |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 139 | |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 140 | net->xfrm.state_bydst = ndst; |
| 141 | net->xfrm.state_bysrc = nsrc; |
| 142 | net->xfrm.state_byspi = nspi; |
| 143 | net->xfrm.state_hmask = nhashmask; |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 144 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 145 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 146 | |
| 147 | osize = (ohashmask + 1) * sizeof(struct hlist_head); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 148 | xfrm_hash_free(odst, osize); |
| 149 | xfrm_hash_free(osrc, osize); |
| 150 | xfrm_hash_free(ospi, osize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 151 | |
| 152 | out_unlock: |
| 153 | mutex_unlock(&hash_resize_mutex); |
| 154 | } |
| 155 | |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 156 | static DEFINE_SPINLOCK(xfrm_state_afinfo_lock); |
| 157 | static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | static DEFINE_SPINLOCK(xfrm_state_gc_lock); |
| 160 | |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 161 | int __xfrm_state_delete(struct xfrm_state *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Jamal Hadi Salim | 980ebd2 | 2006-03-20 19:16:40 -0800 | [diff] [blame] | 163 | int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol); |
Horia Geanta | 0f24558 | 2014-02-12 16:20:06 +0200 | [diff] [blame] | 164 | bool km_is_alive(const struct km_event *c); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 165 | void km_state_expired(struct xfrm_state *x, int hard, u32 portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 167 | static DEFINE_SPINLOCK(xfrm_type_lock); |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 168 | int xfrm_register_type(const struct xfrm_type *type, unsigned short family) |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 169 | { |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 170 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 171 | const struct xfrm_type **typemap; |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 172 | int err = 0; |
| 173 | |
| 174 | if (unlikely(afinfo == NULL)) |
| 175 | return -EAFNOSUPPORT; |
| 176 | typemap = afinfo->type_map; |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 177 | spin_lock_bh(&xfrm_type_lock); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 178 | |
| 179 | if (likely(typemap[type->proto] == NULL)) |
| 180 | typemap[type->proto] = type; |
| 181 | else |
| 182 | err = -EEXIST; |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 183 | spin_unlock_bh(&xfrm_type_lock); |
| 184 | xfrm_state_put_afinfo(afinfo); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 185 | return err; |
| 186 | } |
| 187 | EXPORT_SYMBOL(xfrm_register_type); |
| 188 | |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 189 | int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family) |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 190 | { |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 191 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 192 | const struct xfrm_type **typemap; |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 193 | int err = 0; |
| 194 | |
| 195 | if (unlikely(afinfo == NULL)) |
| 196 | return -EAFNOSUPPORT; |
| 197 | typemap = afinfo->type_map; |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 198 | spin_lock_bh(&xfrm_type_lock); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 199 | |
| 200 | if (unlikely(typemap[type->proto] != type)) |
| 201 | err = -ENOENT; |
| 202 | else |
| 203 | typemap[type->proto] = NULL; |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 204 | spin_unlock_bh(&xfrm_type_lock); |
| 205 | xfrm_state_put_afinfo(afinfo); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 206 | return err; |
| 207 | } |
| 208 | EXPORT_SYMBOL(xfrm_unregister_type); |
| 209 | |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 210 | static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family) |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 211 | { |
| 212 | struct xfrm_state_afinfo *afinfo; |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 213 | const struct xfrm_type **typemap; |
| 214 | const struct xfrm_type *type; |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 215 | int modload_attempted = 0; |
| 216 | |
| 217 | retry: |
| 218 | afinfo = xfrm_state_get_afinfo(family); |
| 219 | if (unlikely(afinfo == NULL)) |
| 220 | return NULL; |
| 221 | typemap = afinfo->type_map; |
| 222 | |
| 223 | type = typemap[proto]; |
| 224 | if (unlikely(type && !try_module_get(type->owner))) |
| 225 | type = NULL; |
| 226 | if (!type && !modload_attempted) { |
| 227 | xfrm_state_put_afinfo(afinfo); |
| 228 | request_module("xfrm-type-%d-%d", family, proto); |
| 229 | modload_attempted = 1; |
| 230 | goto retry; |
| 231 | } |
| 232 | |
| 233 | xfrm_state_put_afinfo(afinfo); |
| 234 | return type; |
| 235 | } |
| 236 | |
Eric Dumazet | 533cb5b | 2008-01-30 19:11:50 -0800 | [diff] [blame] | 237 | static void xfrm_put_type(const struct xfrm_type *type) |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 238 | { |
| 239 | module_put(type->owner); |
| 240 | } |
| 241 | |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 242 | static DEFINE_SPINLOCK(xfrm_mode_lock); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 243 | int xfrm_register_mode(struct xfrm_mode *mode, int family) |
| 244 | { |
| 245 | struct xfrm_state_afinfo *afinfo; |
| 246 | struct xfrm_mode **modemap; |
| 247 | int err; |
| 248 | |
| 249 | if (unlikely(mode->encap >= XFRM_MODE_MAX)) |
| 250 | return -EINVAL; |
| 251 | |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 252 | afinfo = xfrm_state_get_afinfo(family); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 253 | if (unlikely(afinfo == NULL)) |
| 254 | return -EAFNOSUPPORT; |
| 255 | |
| 256 | err = -EEXIST; |
| 257 | modemap = afinfo->mode_map; |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 258 | spin_lock_bh(&xfrm_mode_lock); |
Herbert Xu | 17c2a42 | 2007-10-17 21:33:12 -0700 | [diff] [blame] | 259 | if (modemap[mode->encap]) |
| 260 | goto out; |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 261 | |
Herbert Xu | 17c2a42 | 2007-10-17 21:33:12 -0700 | [diff] [blame] | 262 | err = -ENOENT; |
| 263 | if (!try_module_get(afinfo->owner)) |
| 264 | goto out; |
| 265 | |
| 266 | mode->afinfo = afinfo; |
| 267 | modemap[mode->encap] = mode; |
| 268 | err = 0; |
| 269 | |
| 270 | out: |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 271 | spin_unlock_bh(&xfrm_mode_lock); |
| 272 | xfrm_state_put_afinfo(afinfo); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 273 | return err; |
| 274 | } |
| 275 | EXPORT_SYMBOL(xfrm_register_mode); |
| 276 | |
| 277 | int xfrm_unregister_mode(struct xfrm_mode *mode, int family) |
| 278 | { |
| 279 | struct xfrm_state_afinfo *afinfo; |
| 280 | struct xfrm_mode **modemap; |
| 281 | int err; |
| 282 | |
| 283 | if (unlikely(mode->encap >= XFRM_MODE_MAX)) |
| 284 | return -EINVAL; |
| 285 | |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 286 | afinfo = xfrm_state_get_afinfo(family); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 287 | if (unlikely(afinfo == NULL)) |
| 288 | return -EAFNOSUPPORT; |
| 289 | |
| 290 | err = -ENOENT; |
| 291 | modemap = afinfo->mode_map; |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 292 | spin_lock_bh(&xfrm_mode_lock); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 293 | if (likely(modemap[mode->encap] == mode)) { |
| 294 | modemap[mode->encap] = NULL; |
Herbert Xu | 17c2a42 | 2007-10-17 21:33:12 -0700 | [diff] [blame] | 295 | module_put(mode->afinfo->owner); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 296 | err = 0; |
| 297 | } |
| 298 | |
Cong Wang | 7a9885b | 2013-01-17 16:34:11 +0800 | [diff] [blame] | 299 | spin_unlock_bh(&xfrm_mode_lock); |
| 300 | xfrm_state_put_afinfo(afinfo); |
Herbert Xu | aa5d62c | 2007-10-17 21:31:12 -0700 | [diff] [blame] | 301 | return err; |
| 302 | } |
| 303 | EXPORT_SYMBOL(xfrm_unregister_mode); |
| 304 | |
| 305 | static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family) |
| 306 | { |
| 307 | struct xfrm_state_afinfo *afinfo; |
| 308 | struct xfrm_mode *mode; |
| 309 | int modload_attempted = 0; |
| 310 | |
| 311 | if (unlikely(encap >= XFRM_MODE_MAX)) |
| 312 | return NULL; |
| 313 | |
| 314 | retry: |
| 315 | afinfo = xfrm_state_get_afinfo(family); |
| 316 | if (unlikely(afinfo == NULL)) |
| 317 | return NULL; |
| 318 | |
| 319 | mode = afinfo->mode_map[encap]; |
| 320 | if (unlikely(mode && !try_module_get(mode->owner))) |
| 321 | mode = NULL; |
| 322 | if (!mode && !modload_attempted) { |
| 323 | xfrm_state_put_afinfo(afinfo); |
| 324 | request_module("xfrm-mode-%d-%d", family, encap); |
| 325 | modload_attempted = 1; |
| 326 | goto retry; |
| 327 | } |
| 328 | |
| 329 | xfrm_state_put_afinfo(afinfo); |
| 330 | return mode; |
| 331 | } |
| 332 | |
| 333 | static void xfrm_put_mode(struct xfrm_mode *mode) |
| 334 | { |
| 335 | module_put(mode->owner); |
| 336 | } |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | static void xfrm_state_gc_destroy(struct xfrm_state *x) |
| 339 | { |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 340 | tasklet_hrtimer_cancel(&x->mtimer); |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 341 | del_timer_sync(&x->rtimer); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 342 | kfree(x->aalg); |
| 343 | kfree(x->ealg); |
| 344 | kfree(x->calg); |
| 345 | kfree(x->encap); |
Noriaki TAKAMIYA | 060f02a | 2006-08-23 18:18:55 -0700 | [diff] [blame] | 346 | kfree(x->coaddr); |
Steffen Klassert | d8647b7 | 2011-03-08 00:10:27 +0000 | [diff] [blame] | 347 | kfree(x->replay_esn); |
| 348 | kfree(x->preplay_esn); |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 349 | if (x->inner_mode) |
| 350 | xfrm_put_mode(x->inner_mode); |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 351 | if (x->inner_mode_iaf) |
| 352 | xfrm_put_mode(x->inner_mode_iaf); |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 353 | if (x->outer_mode) |
| 354 | xfrm_put_mode(x->outer_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (x->type) { |
| 356 | x->type->destructor(x); |
| 357 | xfrm_put_type(x->type); |
| 358 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 359 | security_xfrm_state_free(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | kfree(x); |
| 361 | } |
| 362 | |
Alexey Dobriyan | c783714 | 2008-11-25 17:20:36 -0800 | [diff] [blame] | 363 | static void xfrm_state_gc_task(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
Alexey Dobriyan | c783714 | 2008-11-25 17:20:36 -0800 | [diff] [blame] | 365 | struct net *net = container_of(work, struct net, xfrm.state_gc_work); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 366 | struct xfrm_state *x; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 367 | struct hlist_node *tmp; |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 368 | struct hlist_head gc_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | spin_lock_bh(&xfrm_state_gc_lock); |
Alexey Dobriyan | c783714 | 2008-11-25 17:20:36 -0800 | [diff] [blame] | 371 | hlist_move_list(&net->xfrm.state_gc_list, &gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | spin_unlock_bh(&xfrm_state_gc_lock); |
| 373 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 374 | hlist_for_each_entry_safe(x, tmp, &gc_list, gclist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | xfrm_state_gc_destroy(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static inline unsigned long make_jiffies(long secs) |
| 379 | { |
| 380 | if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) |
| 381 | return MAX_SCHEDULE_TIMEOUT-1; |
| 382 | else |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 383 | return secs*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 386 | static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 388 | struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer); |
| 389 | struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer); |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 390 | unsigned long now = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | long next = LONG_MAX; |
| 392 | int warn = 0; |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 393 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | spin_lock(&x->lock); |
| 396 | if (x->km.state == XFRM_STATE_DEAD) |
| 397 | goto out; |
| 398 | if (x->km.state == XFRM_STATE_EXPIRED) |
| 399 | goto expired; |
| 400 | if (x->lft.hard_add_expires_seconds) { |
| 401 | long tmo = x->lft.hard_add_expires_seconds + |
| 402 | x->curlft.add_time - now; |
Fan Du | e3c0d04 | 2012-07-30 21:43:54 +0000 | [diff] [blame] | 403 | if (tmo <= 0) { |
| 404 | if (x->xflags & XFRM_SOFT_EXPIRE) { |
| 405 | /* enter hard expire without soft expire first?! |
| 406 | * setting a new date could trigger this. |
| 407 | * workarbound: fix x->curflt.add_time by below: |
| 408 | */ |
| 409 | x->curlft.add_time = now - x->saved_tmo - 1; |
| 410 | tmo = x->lft.hard_add_expires_seconds - x->saved_tmo; |
| 411 | } else |
| 412 | goto expired; |
| 413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (tmo < next) |
| 415 | next = tmo; |
| 416 | } |
| 417 | if (x->lft.hard_use_expires_seconds) { |
| 418 | long tmo = x->lft.hard_use_expires_seconds + |
| 419 | (x->curlft.use_time ? : now) - now; |
| 420 | if (tmo <= 0) |
| 421 | goto expired; |
| 422 | if (tmo < next) |
| 423 | next = tmo; |
| 424 | } |
| 425 | if (x->km.dying) |
| 426 | goto resched; |
| 427 | if (x->lft.soft_add_expires_seconds) { |
| 428 | long tmo = x->lft.soft_add_expires_seconds + |
| 429 | x->curlft.add_time - now; |
Fan Du | e3c0d04 | 2012-07-30 21:43:54 +0000 | [diff] [blame] | 430 | if (tmo <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | warn = 1; |
Fan Du | e3c0d04 | 2012-07-30 21:43:54 +0000 | [diff] [blame] | 432 | x->xflags &= ~XFRM_SOFT_EXPIRE; |
| 433 | } else if (tmo < next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | next = tmo; |
Fan Du | e3c0d04 | 2012-07-30 21:43:54 +0000 | [diff] [blame] | 435 | x->xflags |= XFRM_SOFT_EXPIRE; |
| 436 | x->saved_tmo = tmo; |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | if (x->lft.soft_use_expires_seconds) { |
| 440 | long tmo = x->lft.soft_use_expires_seconds + |
| 441 | (x->curlft.use_time ? : now) - now; |
| 442 | if (tmo <= 0) |
| 443 | warn = 1; |
| 444 | else if (tmo < next) |
| 445 | next = tmo; |
| 446 | } |
| 447 | |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 448 | x->km.dying = warn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | if (warn) |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 450 | km_state_expired(x, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | resched: |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 452 | if (next != LONG_MAX) { |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 453 | tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL); |
| 454 | } |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | goto out; |
| 457 | |
| 458 | expired: |
Steffen Klassert | 5b8ef34 | 2013-08-27 13:43:30 +0200 | [diff] [blame] | 459 | if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | x->km.state = XFRM_STATE_EXPIRED; |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 461 | |
| 462 | err = __xfrm_state_delete(x); |
Nicolas Dichtel | 0806ae4 | 2013-08-23 15:46:08 +0200 | [diff] [blame] | 463 | if (!err) |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 464 | km_state_expired(x, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 466 | xfrm_audit_state_delete(x, err ? 0 : 1, true); |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | out: |
| 469 | spin_unlock(&x->lock); |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 470 | return HRTIMER_NORESTART; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
David S. Miller | 0ac8475 | 2006-03-20 19:18:23 -0800 | [diff] [blame] | 473 | static void xfrm_replay_timer_handler(unsigned long data); |
| 474 | |
Alexey Dobriyan | 673c09b | 2008-11-25 17:15:16 -0800 | [diff] [blame] | 475 | struct xfrm_state *xfrm_state_alloc(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | struct xfrm_state *x; |
| 478 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 479 | x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | if (x) { |
Alexey Dobriyan | 673c09b | 2008-11-25 17:15:16 -0800 | [diff] [blame] | 482 | write_pnet(&x->xs_net, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | atomic_set(&x->refcnt, 1); |
| 484 | atomic_set(&x->tunnel_users, 0); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 485 | INIT_LIST_HEAD(&x->km.all); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 486 | INIT_HLIST_NODE(&x->bydst); |
| 487 | INIT_HLIST_NODE(&x->bysrc); |
| 488 | INIT_HLIST_NODE(&x->byspi); |
Fan Du | 99565a6c | 2013-08-15 15:49:06 +0800 | [diff] [blame] | 489 | tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, |
| 490 | CLOCK_BOOTTIME, HRTIMER_MODE_ABS); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 491 | setup_timer(&x->rtimer, xfrm_replay_timer_handler, |
| 492 | (unsigned long)x); |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 493 | x->curlft.add_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | x->lft.soft_byte_limit = XFRM_INF; |
| 495 | x->lft.soft_packet_limit = XFRM_INF; |
| 496 | x->lft.hard_byte_limit = XFRM_INF; |
| 497 | x->lft.hard_packet_limit = XFRM_INF; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 498 | x->replay_maxage = 0; |
| 499 | x->replay_maxdiff = 0; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 500 | x->inner_mode = NULL; |
| 501 | x->inner_mode_iaf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | spin_lock_init(&x->lock); |
| 503 | } |
| 504 | return x; |
| 505 | } |
| 506 | EXPORT_SYMBOL(xfrm_state_alloc); |
| 507 | |
| 508 | void __xfrm_state_destroy(struct xfrm_state *x) |
| 509 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 510 | struct net *net = xs_net(x); |
| 511 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 512 | WARN_ON(x->km.state != XFRM_STATE_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | spin_lock_bh(&xfrm_state_gc_lock); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 515 | hlist_add_head(&x->gclist, &net->xfrm.state_gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | spin_unlock_bh(&xfrm_state_gc_lock); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 517 | schedule_work(&net->xfrm.state_gc_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | EXPORT_SYMBOL(__xfrm_state_destroy); |
| 520 | |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 521 | int __xfrm_state_delete(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 523 | struct net *net = xs_net(x); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 524 | int err = -ESRCH; |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (x->km.state != XFRM_STATE_DEAD) { |
| 527 | x->km.state = XFRM_STATE_DEAD; |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 528 | spin_lock(&net->xfrm.xfrm_state_lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 529 | list_del(&x->km.all); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 530 | hlist_del(&x->bydst); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 531 | hlist_del(&x->bysrc); |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 532 | if (x->id.spi) |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 533 | hlist_del(&x->byspi); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 534 | net->xfrm.state_num--; |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 535 | spin_unlock(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | /* All xfrm_state objects are created by xfrm_state_alloc. |
| 538 | * The xfrm_state_alloc call gives a reference, and that |
| 539 | * is what we are dropping here. |
| 540 | */ |
Patrick McHardy | 5dba479 | 2007-11-27 11:10:07 +0800 | [diff] [blame] | 541 | xfrm_state_put(x); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 542 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 544 | |
| 545 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 547 | EXPORT_SYMBOL(__xfrm_state_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 549 | int xfrm_state_delete(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 551 | int err; |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | spin_lock_bh(&x->lock); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 554 | err = __xfrm_state_delete(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | spin_unlock_bh(&x->lock); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 556 | |
| 557 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | EXPORT_SYMBOL(xfrm_state_delete); |
| 560 | |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 561 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 562 | static inline int |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 563 | xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 565 | int i, err = 0; |
| 566 | |
Alexey Dobriyan | 0e60245 | 2008-11-25 17:30:18 -0800 | [diff] [blame] | 567 | for (i = 0; i <= net->xfrm.state_hmask; i++) { |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 568 | struct xfrm_state *x; |
| 569 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 570 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 571 | if (xfrm_id_proto_match(x->id.proto, proto) && |
| 572 | (err = security_xfrm_state_delete(x)) != 0) { |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 573 | xfrm_audit_state_delete(x, 0, task_valid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 574 | return err; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return err; |
| 580 | } |
| 581 | #else |
| 582 | static inline int |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 583 | xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid) |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 584 | { |
| 585 | return 0; |
| 586 | } |
| 587 | #endif |
| 588 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 589 | int xfrm_state_flush(struct net *net, u8 proto, bool task_valid) |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 590 | { |
Jamal Hadi Salim | 9e64cc9 | 2010-02-19 02:00:41 +0000 | [diff] [blame] | 591 | int i, err = 0, cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 593 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 594 | err = xfrm_state_flush_secctx_check(net, proto, task_valid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 595 | if (err) |
| 596 | goto out; |
| 597 | |
Jamal Hadi Salim | 9e64cc9 | 2010-02-19 02:00:41 +0000 | [diff] [blame] | 598 | err = -ESRCH; |
Alexey Dobriyan | 0e60245 | 2008-11-25 17:30:18 -0800 | [diff] [blame] | 599 | for (i = 0; i <= net->xfrm.state_hmask; i++) { |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 600 | struct xfrm_state *x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | restart: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 602 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (!xfrm_state_kern(x) && |
Masahide NAKAMURA | 5794708 | 2006-09-22 15:06:24 -0700 | [diff] [blame] | 604 | xfrm_id_proto_match(x->id.proto, proto)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | xfrm_state_hold(x); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 606 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 608 | err = xfrm_state_delete(x); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 609 | xfrm_audit_state_delete(x, err ? 0 : 1, |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 610 | task_valid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | xfrm_state_put(x); |
Jamal Hadi Salim | 9e64cc9 | 2010-02-19 02:00:41 +0000 | [diff] [blame] | 612 | if (!err) |
| 613 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 615 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | goto restart; |
| 617 | } |
| 618 | } |
| 619 | } |
Jamal Hadi Salim | 9e64cc9 | 2010-02-19 02:00:41 +0000 | [diff] [blame] | 620 | if (cnt) |
| 621 | err = 0; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 622 | |
| 623 | out: |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 624 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 625 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | EXPORT_SYMBOL(xfrm_state_flush); |
| 628 | |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 629 | void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si) |
Jamal Hadi Salim | 28d8909 | 2007-04-26 00:10:29 -0700 | [diff] [blame] | 630 | { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 631 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 632 | si->sadcnt = net->xfrm.state_num; |
| 633 | si->sadhcnt = net->xfrm.state_hmask; |
Jamal Hadi Salim | 28d8909 | 2007-04-26 00:10:29 -0700 | [diff] [blame] | 634 | si->sadhmcnt = xfrm_state_hashmax; |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 635 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Jamal Hadi Salim | 28d8909 | 2007-04-26 00:10:29 -0700 | [diff] [blame] | 636 | } |
| 637 | EXPORT_SYMBOL(xfrm_sad_getinfo); |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | static int |
David S. Miller | 1a89859 | 2011-02-22 18:22:34 -0800 | [diff] [blame] | 640 | xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl, |
David S. Miller | 0468601 | 2011-02-24 01:50:12 -0500 | [diff] [blame] | 641 | const struct xfrm_tmpl *tmpl, |
David S. Miller | 33765d0 | 2011-02-24 01:55:45 -0500 | [diff] [blame] | 642 | const xfrm_address_t *daddr, const xfrm_address_t *saddr, |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 643 | unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
| 645 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
| 646 | if (!afinfo) |
| 647 | return -1; |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 648 | afinfo->init_tempsel(&x->sel, fl); |
| 649 | |
| 650 | if (family != tmpl->encap_family) { |
| 651 | xfrm_state_put_afinfo(afinfo); |
| 652 | afinfo = xfrm_state_get_afinfo(tmpl->encap_family); |
| 653 | if (!afinfo) |
| 654 | return -1; |
| 655 | } |
| 656 | afinfo->init_temprop(x, tmpl, daddr, saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | xfrm_state_put_afinfo(afinfo); |
| 658 | return 0; |
| 659 | } |
| 660 | |
David S. Miller | 9aa6008 | 2011-02-24 01:51:36 -0500 | [diff] [blame] | 661 | static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, |
| 662 | const xfrm_address_t *daddr, |
| 663 | __be32 spi, u8 proto, |
| 664 | unsigned short family) |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 665 | { |
Alexey Dobriyan | 221df1e | 2008-11-25 17:30:50 -0800 | [diff] [blame] | 666 | unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family); |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 667 | struct xfrm_state *x; |
| 668 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 669 | hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) { |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 670 | if (x->props.family != family || |
| 671 | x->id.spi != spi || |
Wei Yongjun | 1802571 | 2009-06-28 18:42:53 +0000 | [diff] [blame] | 672 | x->id.proto != proto || |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 673 | !xfrm_addr_equal(&x->id.daddr, daddr, family)) |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 674 | continue; |
| 675 | |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 676 | if ((mark & x->mark.m) != x->mark.v) |
| 677 | continue; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 678 | xfrm_state_hold(x); |
| 679 | return x; |
| 680 | } |
| 681 | |
| 682 | return NULL; |
| 683 | } |
| 684 | |
David S. Miller | 9aa6008 | 2011-02-24 01:51:36 -0500 | [diff] [blame] | 685 | static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, |
| 686 | const xfrm_address_t *daddr, |
| 687 | const xfrm_address_t *saddr, |
| 688 | u8 proto, unsigned short family) |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 689 | { |
Alexey Dobriyan | 221df1e | 2008-11-25 17:30:50 -0800 | [diff] [blame] | 690 | unsigned int h = xfrm_src_hash(net, daddr, saddr, family); |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 691 | struct xfrm_state *x; |
| 692 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 693 | hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) { |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 694 | if (x->props.family != family || |
Wei Yongjun | 1802571 | 2009-06-28 18:42:53 +0000 | [diff] [blame] | 695 | x->id.proto != proto || |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 696 | !xfrm_addr_equal(&x->id.daddr, daddr, family) || |
| 697 | !xfrm_addr_equal(&x->props.saddr, saddr, family)) |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 698 | continue; |
| 699 | |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 700 | if ((mark & x->mark.m) != x->mark.v) |
| 701 | continue; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 702 | xfrm_state_hold(x); |
| 703 | return x; |
| 704 | } |
| 705 | |
| 706 | return NULL; |
| 707 | } |
| 708 | |
| 709 | static inline struct xfrm_state * |
| 710 | __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family) |
| 711 | { |
Alexey Dobriyan | 221df1e | 2008-11-25 17:30:50 -0800 | [diff] [blame] | 712 | struct net *net = xs_net(x); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 713 | u32 mark = x->mark.v & x->mark.m; |
Alexey Dobriyan | 221df1e | 2008-11-25 17:30:50 -0800 | [diff] [blame] | 714 | |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 715 | if (use_spi) |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 716 | return __xfrm_state_lookup(net, mark, &x->id.daddr, |
| 717 | x->id.spi, x->id.proto, family); |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 718 | else |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 719 | return __xfrm_state_lookup_byaddr(net, mark, |
| 720 | &x->id.daddr, |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 721 | &x->props.saddr, |
| 722 | x->id.proto, family); |
| 723 | } |
| 724 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 725 | static void xfrm_hash_grow_check(struct net *net, int have_hash_collision) |
Patrick McHardy | 2fab22f | 2006-10-24 15:34:00 -0700 | [diff] [blame] | 726 | { |
| 727 | if (have_hash_collision && |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 728 | (net->xfrm.state_hmask + 1) < xfrm_state_hashmax && |
| 729 | net->xfrm.state_num > net->xfrm.state_hmask) |
| 730 | schedule_work(&net->xfrm.state_hash_work); |
Patrick McHardy | 2fab22f | 2006-10-24 15:34:00 -0700 | [diff] [blame] | 731 | } |
| 732 | |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 733 | static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x, |
David S. Miller | 4a08ab0 | 2011-02-22 18:21:31 -0800 | [diff] [blame] | 734 | const struct flowi *fl, unsigned short family, |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 735 | struct xfrm_state **best, int *acq_in_progress, |
| 736 | int *error) |
| 737 | { |
| 738 | /* Resolution logic: |
| 739 | * 1. There is a valid state with matching selector. Done. |
| 740 | * 2. Valid state with inappropriate selector. Skip. |
| 741 | * |
| 742 | * Entering area of "sysdeps". |
| 743 | * |
| 744 | * 3. If state is not valid, selector is temporary, it selects |
| 745 | * only session which triggered previous resolution. Key |
| 746 | * manager will do something to install a state with proper |
| 747 | * selector. |
| 748 | */ |
| 749 | if (x->km.state == XFRM_STATE_VALID) { |
| 750 | if ((x->sel.family && |
| 751 | !xfrm_selector_match(&x->sel, fl, x->sel.family)) || |
| 752 | !security_xfrm_state_pol_flow_match(x, pol, fl)) |
| 753 | return; |
| 754 | |
| 755 | if (!*best || |
| 756 | (*best)->km.dying > x->km.dying || |
| 757 | ((*best)->km.dying == x->km.dying && |
| 758 | (*best)->curlft.add_time < x->curlft.add_time)) |
| 759 | *best = x; |
| 760 | } else if (x->km.state == XFRM_STATE_ACQ) { |
| 761 | *acq_in_progress = 1; |
| 762 | } else if (x->km.state == XFRM_STATE_ERROR || |
| 763 | x->km.state == XFRM_STATE_EXPIRED) { |
| 764 | if (xfrm_selector_match(&x->sel, fl, x->sel.family) && |
| 765 | security_xfrm_state_pol_flow_match(x, pol, fl)) |
| 766 | *error = -ESRCH; |
| 767 | } |
| 768 | } |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | struct xfrm_state * |
David S. Miller | 33765d0 | 2011-02-24 01:55:45 -0500 | [diff] [blame] | 771 | xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, |
David S. Miller | b520e9f | 2011-02-22 18:24:19 -0800 | [diff] [blame] | 772 | const struct flowi *fl, struct xfrm_tmpl *tmpl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | struct xfrm_policy *pol, int *err, |
| 774 | unsigned short family) |
| 775 | { |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 776 | static xfrm_address_t saddr_wildcard = { }; |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 777 | struct net *net = xp_net(pol); |
Nicolas Dichtel | 6a783c9 | 2009-04-27 02:58:59 -0700 | [diff] [blame] | 778 | unsigned int h, h_wildcard; |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 779 | struct xfrm_state *x, *x0, *to_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | int acquire_in_progress = 0; |
| 781 | int error = 0; |
| 782 | struct xfrm_state *best = NULL; |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 783 | u32 mark = pol->mark.v & pol->mark.m; |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 784 | unsigned short encap_family = tmpl->encap_family; |
Horia Geanta | 0f24558 | 2014-02-12 16:20:06 +0200 | [diff] [blame] | 785 | struct km_event c; |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 786 | |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 787 | to_put = NULL; |
| 788 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 789 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 790 | h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 791 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 792 | if (x->props.family == encap_family && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | x->props.reqid == tmpl->reqid && |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 794 | (mark & x->mark.m) == x->mark.v && |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 795 | !(x->props.flags & XFRM_STATE_WILDRECV) && |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 796 | xfrm_state_addr_check(x, daddr, saddr, encap_family) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | tmpl->mode == x->props.mode && |
| 798 | tmpl->id.proto == x->id.proto && |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 799 | (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) |
David S. Miller | 1f673c5 | 2011-02-24 01:53:13 -0500 | [diff] [blame] | 800 | xfrm_state_look_at(pol, x, fl, encap_family, |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 801 | &best, &acquire_in_progress, &error); |
| 802 | } |
Fan Du | 6f11563 | 2013-09-23 17:18:25 +0800 | [diff] [blame] | 803 | if (best || acquire_in_progress) |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 804 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 806 | h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 807 | hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) { |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 808 | if (x->props.family == encap_family && |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 809 | x->props.reqid == tmpl->reqid && |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 810 | (mark & x->mark.m) == x->mark.v && |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 811 | !(x->props.flags & XFRM_STATE_WILDRECV) && |
Fan Du | f59bbdf | 2013-09-27 16:32:50 +0800 | [diff] [blame] | 812 | xfrm_addr_equal(&x->id.daddr, daddr, encap_family) && |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 813 | tmpl->mode == x->props.mode && |
| 814 | tmpl->id.proto == x->id.proto && |
| 815 | (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) |
David S. Miller | 1f673c5 | 2011-02-24 01:53:13 -0500 | [diff] [blame] | 816 | xfrm_state_look_at(pol, x, fl, encap_family, |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 817 | &best, &acquire_in_progress, &error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
David S. Miller | 08ec9af | 2009-03-13 14:22:40 -0700 | [diff] [blame] | 820 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | x = best; |
| 822 | if (!x && !error && !acquire_in_progress) { |
Patrick McHardy | 5c5d281 | 2005-04-21 20:12:32 -0700 | [diff] [blame] | 823 | if (tmpl->id.spi && |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 824 | (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi, |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 825 | tmpl->id.proto, encap_family)) != NULL) { |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 826 | to_put = x0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | error = -EEXIST; |
| 828 | goto out; |
| 829 | } |
Horia Geanta | 0f24558 | 2014-02-12 16:20:06 +0200 | [diff] [blame] | 830 | |
| 831 | c.net = net; |
| 832 | /* If the KMs have no listeners (yet...), avoid allocating an SA |
| 833 | * for each and every packet - garbage collection might not |
| 834 | * handle the flood. |
| 835 | */ |
| 836 | if (!km_is_alive(&c)) { |
| 837 | error = -ESRCH; |
| 838 | goto out; |
| 839 | } |
| 840 | |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 841 | x = xfrm_state_alloc(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | if (x == NULL) { |
| 843 | error = -ENOMEM; |
| 844 | goto out; |
| 845 | } |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 846 | /* Initialize temporary state matching only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | * to current session. */ |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 848 | xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 849 | memcpy(&x->mark, &pol->mark, sizeof(x->mark)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 851 | error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid); |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 852 | if (error) { |
| 853 | x->km.state = XFRM_STATE_DEAD; |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 854 | to_put = x; |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 855 | x = NULL; |
| 856 | goto out; |
| 857 | } |
| 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (km_query(x, tmpl, pol) == 0) { |
| 860 | x->km.state = XFRM_STATE_ACQ; |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 861 | list_add(&x->km.all, &net->xfrm.state_all); |
| 862 | hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 863 | h = xfrm_src_hash(net, daddr, saddr, encap_family); |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 864 | hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | if (x->id.spi) { |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 866 | h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family); |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 867 | hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 869 | x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 870 | tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL); |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 871 | net->xfrm.state_num++; |
| 872 | xfrm_hash_grow_check(net, x->bydst.next != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } else { |
| 874 | x->km.state = XFRM_STATE_DEAD; |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 875 | to_put = x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | x = NULL; |
| 877 | error = -ESRCH; |
| 878 | } |
| 879 | } |
| 880 | out: |
| 881 | if (x) |
| 882 | xfrm_state_hold(x); |
| 883 | else |
| 884 | *err = acquire_in_progress ? -EAGAIN : error; |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 885 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 886 | if (to_put) |
| 887 | xfrm_state_put(to_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | return x; |
| 889 | } |
| 890 | |
Jamal Hadi Salim | 628529b | 2007-07-02 22:41:14 -0700 | [diff] [blame] | 891 | struct xfrm_state * |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 892 | xfrm_stateonly_find(struct net *net, u32 mark, |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 893 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
Jamal Hadi Salim | 628529b | 2007-07-02 22:41:14 -0700 | [diff] [blame] | 894 | unsigned short family, u8 mode, u8 proto, u32 reqid) |
| 895 | { |
Pavel Emelyanov | 4bda4f2 | 2007-12-14 11:38:04 -0800 | [diff] [blame] | 896 | unsigned int h; |
Jamal Hadi Salim | 628529b | 2007-07-02 22:41:14 -0700 | [diff] [blame] | 897 | struct xfrm_state *rx = NULL, *x = NULL; |
Jamal Hadi Salim | 628529b | 2007-07-02 22:41:14 -0700 | [diff] [blame] | 898 | |
Fan Du | 4ae770b | 2014-01-03 11:18:29 +0800 | [diff] [blame] | 899 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 900 | h = xfrm_dst_hash(net, daddr, saddr, reqid, family); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 901 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
Jamal Hadi Salim | 628529b | 2007-07-02 22:41:14 -0700 | [diff] [blame] | 902 | if (x->props.family == family && |
| 903 | x->props.reqid == reqid && |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 904 | (mark & x->mark.m) == x->mark.v && |
Jamal Hadi Salim | 628529b | 2007-07-02 22:41:14 -0700 | [diff] [blame] | 905 | !(x->props.flags & XFRM_STATE_WILDRECV) && |
| 906 | xfrm_state_addr_check(x, daddr, saddr, family) && |
| 907 | mode == x->props.mode && |
| 908 | proto == x->id.proto && |
| 909 | x->km.state == XFRM_STATE_VALID) { |
| 910 | rx = x; |
| 911 | break; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (rx) |
| 916 | xfrm_state_hold(rx); |
Fan Du | 4ae770b | 2014-01-03 11:18:29 +0800 | [diff] [blame] | 917 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Jamal Hadi Salim | 628529b | 2007-07-02 22:41:14 -0700 | [diff] [blame] | 918 | |
| 919 | |
| 920 | return rx; |
| 921 | } |
| 922 | EXPORT_SYMBOL(xfrm_stateonly_find); |
| 923 | |
Fan Du | c454997 | 2014-01-03 11:18:32 +0800 | [diff] [blame] | 924 | struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi, |
| 925 | unsigned short family) |
| 926 | { |
| 927 | struct xfrm_state *x; |
| 928 | struct xfrm_state_walk *w; |
| 929 | |
| 930 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
| 931 | list_for_each_entry(w, &net->xfrm.state_all, all) { |
| 932 | x = container_of(w, struct xfrm_state, km); |
| 933 | if (x->props.family != family || |
| 934 | x->id.spi != spi) |
| 935 | continue; |
| 936 | |
| 937 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
| 938 | xfrm_state_hold(x); |
| 939 | return x; |
| 940 | } |
| 941 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
| 942 | return NULL; |
| 943 | } |
| 944 | EXPORT_SYMBOL(xfrm_state_lookup_byspi); |
| 945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | static void __xfrm_state_insert(struct xfrm_state *x) |
| 947 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 948 | struct net *net = xs_net(x); |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 949 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 951 | list_add(&x->km.all, &net->xfrm.state_all); |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 952 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 953 | h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr, |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 954 | x->props.reqid, x->props.family); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 955 | hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 957 | h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family); |
| 958 | hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Masahide NAKAMURA | 7b4dc360 | 2006-09-27 22:21:52 -0700 | [diff] [blame] | 960 | if (x->id.spi) { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 961 | h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, |
Masahide NAKAMURA | 6c44e6b | 2006-08-23 17:53:57 -0700 | [diff] [blame] | 962 | x->props.family); |
| 963 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 964 | hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); |
Masahide NAKAMURA | 6c44e6b | 2006-08-23 17:53:57 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 967 | tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 968 | if (x->replay_maxage) |
| 969 | mod_timer(&x->rtimer, jiffies + x->replay_maxage); |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 970 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 971 | net->xfrm.state_num++; |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 972 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 973 | xfrm_hash_grow_check(net, x->bydst.next != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 976 | /* net->xfrm.xfrm_state_lock is held */ |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 977 | static void __xfrm_state_bump_genids(struct xfrm_state *xnew) |
| 978 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 979 | struct net *net = xs_net(xnew); |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 980 | unsigned short family = xnew->props.family; |
| 981 | u32 reqid = xnew->props.reqid; |
| 982 | struct xfrm_state *x; |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 983 | unsigned int h; |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 984 | u32 mark = xnew->mark.v & xnew->mark.m; |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 985 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 986 | h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 987 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 988 | if (x->props.family == family && |
| 989 | x->props.reqid == reqid && |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 990 | (mark & x->mark.m) == x->mark.v && |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 991 | xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) && |
| 992 | xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family)) |
Herbert Xu | 34996cb | 2010-03-31 01:19:49 +0000 | [diff] [blame] | 993 | x->genid++; |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | void xfrm_state_insert(struct xfrm_state *x) |
| 998 | { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 999 | struct net *net = xs_net(x); |
| 1000 | |
| 1001 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 1002 | __xfrm_state_bump_genids(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | __xfrm_state_insert(x); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1004 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | } |
| 1006 | EXPORT_SYMBOL(xfrm_state_insert); |
| 1007 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1008 | /* net->xfrm.xfrm_state_lock is held */ |
Mathias Krause | e473fcb | 2013-06-26 23:56:58 +0200 | [diff] [blame] | 1009 | static struct xfrm_state *__find_acq_core(struct net *net, |
| 1010 | const struct xfrm_mark *m, |
David S. Miller | a70486f | 2011-02-27 23:17:24 -0800 | [diff] [blame] | 1011 | unsigned short family, u8 mode, |
| 1012 | u32 reqid, u8 proto, |
| 1013 | const xfrm_address_t *daddr, |
Mathias Krause | e473fcb | 2013-06-26 23:56:58 +0200 | [diff] [blame] | 1014 | const xfrm_address_t *saddr, |
| 1015 | int create) |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1016 | { |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 1017 | unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1018 | struct xfrm_state *x; |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 1019 | u32 mark = m->v & m->m; |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1020 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1021 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1022 | if (x->props.reqid != reqid || |
| 1023 | x->props.mode != mode || |
| 1024 | x->props.family != family || |
| 1025 | x->km.state != XFRM_STATE_ACQ || |
Joy Latten | 75e252d | 2007-03-12 17:14:07 -0700 | [diff] [blame] | 1026 | x->id.spi != 0 || |
Wei Yongjun | 1802571 | 2009-06-28 18:42:53 +0000 | [diff] [blame] | 1027 | x->id.proto != proto || |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 1028 | (mark & x->mark.m) != x->mark.v || |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 1029 | !xfrm_addr_equal(&x->id.daddr, daddr, family) || |
| 1030 | !xfrm_addr_equal(&x->props.saddr, saddr, family)) |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1031 | continue; |
| 1032 | |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1033 | xfrm_state_hold(x); |
| 1034 | return x; |
| 1035 | } |
| 1036 | |
| 1037 | if (!create) |
| 1038 | return NULL; |
| 1039 | |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 1040 | x = xfrm_state_alloc(net); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1041 | if (likely(x)) { |
| 1042 | switch (family) { |
| 1043 | case AF_INET: |
| 1044 | x->sel.daddr.a4 = daddr->a4; |
| 1045 | x->sel.saddr.a4 = saddr->a4; |
| 1046 | x->sel.prefixlen_d = 32; |
| 1047 | x->sel.prefixlen_s = 32; |
| 1048 | x->props.saddr.a4 = saddr->a4; |
| 1049 | x->id.daddr.a4 = daddr->a4; |
| 1050 | break; |
| 1051 | |
| 1052 | case AF_INET6: |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1053 | *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr; |
| 1054 | *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr; |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1055 | x->sel.prefixlen_d = 128; |
| 1056 | x->sel.prefixlen_s = 128; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1057 | *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr; |
| 1058 | *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr; |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1059 | break; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1060 | } |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1061 | |
| 1062 | x->km.state = XFRM_STATE_ACQ; |
| 1063 | x->id.proto = proto; |
| 1064 | x->props.family = family; |
| 1065 | x->props.mode = mode; |
| 1066 | x->props.reqid = reqid; |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1067 | x->mark.v = m->v; |
| 1068 | x->mark.m = m->m; |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 1069 | x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1070 | xfrm_state_hold(x); |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 1071 | tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL); |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 1072 | list_add(&x->km.all, &net->xfrm.state_all); |
| 1073 | hlist_add_head(&x->bydst, net->xfrm.state_bydst+h); |
| 1074 | h = xfrm_src_hash(net, daddr, saddr, family); |
| 1075 | hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h); |
David S. Miller | 918049f | 2006-10-12 22:03:24 -0700 | [diff] [blame] | 1076 | |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 1077 | net->xfrm.state_num++; |
David S. Miller | 918049f | 2006-10-12 22:03:24 -0700 | [diff] [blame] | 1078 | |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 1079 | xfrm_hash_grow_check(net, x->bydst.next != NULL); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | return x; |
| 1083 | } |
| 1084 | |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1085 | static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
| 1087 | int xfrm_state_add(struct xfrm_state *x) |
| 1088 | { |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 1089 | struct net *net = xs_net(x); |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1090 | struct xfrm_state *x1, *to_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | int family; |
| 1092 | int err; |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1093 | u32 mark = x->mark.v & x->mark.m; |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 1094 | int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
| 1096 | family = x->props.family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1098 | to_put = NULL; |
| 1099 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1100 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 1102 | x1 = __xfrm_state_locate(x, use_spi, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | if (x1) { |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1104 | to_put = x1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | x1 = NULL; |
| 1106 | err = -EEXIST; |
| 1107 | goto out; |
| 1108 | } |
| 1109 | |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 1110 | if (use_spi && x->km.seq) { |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1111 | x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq); |
Joy Latten | 75e252d | 2007-03-12 17:14:07 -0700 | [diff] [blame] | 1112 | if (x1 && ((x1->id.proto != x->id.proto) || |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 1113 | !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) { |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1114 | to_put = x1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | x1 = NULL; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 1119 | if (use_spi && !x1) |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1120 | x1 = __find_acq_core(net, &x->mark, family, x->props.mode, |
| 1121 | x->props.reqid, x->id.proto, |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1122 | &x->id.daddr, &x->props.saddr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 1124 | __xfrm_state_bump_genids(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | __xfrm_state_insert(x); |
| 1126 | err = 0; |
| 1127 | |
| 1128 | out: |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1129 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (x1) { |
| 1132 | xfrm_state_delete(x1); |
| 1133 | xfrm_state_put(x1); |
| 1134 | } |
| 1135 | |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1136 | if (to_put) |
| 1137 | xfrm_state_put(to_put); |
| 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | return err; |
| 1140 | } |
| 1141 | EXPORT_SYMBOL(xfrm_state_add); |
| 1142 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1143 | #ifdef CONFIG_XFRM_MIGRATE |
Steffen Klassert | cc9ab60 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1144 | static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1145 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1146 | struct net *net = xs_net(orig); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1147 | struct xfrm_state *x = xfrm_state_alloc(net); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1148 | if (!x) |
Herbert Xu | 553f911 | 2010-02-15 20:00:51 +0000 | [diff] [blame] | 1149 | goto out; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1150 | |
| 1151 | memcpy(&x->id, &orig->id, sizeof(x->id)); |
| 1152 | memcpy(&x->sel, &orig->sel, sizeof(x->sel)); |
| 1153 | memcpy(&x->lft, &orig->lft, sizeof(x->lft)); |
| 1154 | x->props.mode = orig->props.mode; |
| 1155 | x->props.replay_window = orig->props.replay_window; |
| 1156 | x->props.reqid = orig->props.reqid; |
| 1157 | x->props.family = orig->props.family; |
| 1158 | x->props.saddr = orig->props.saddr; |
| 1159 | |
| 1160 | if (orig->aalg) { |
Martin Willi | 4447bb3 | 2009-11-25 00:29:52 +0000 | [diff] [blame] | 1161 | x->aalg = xfrm_algo_auth_clone(orig->aalg); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1162 | if (!x->aalg) |
| 1163 | goto error; |
| 1164 | } |
| 1165 | x->props.aalgo = orig->props.aalgo; |
| 1166 | |
Steffen Klassert | ee5c231 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1167 | if (orig->aead) { |
| 1168 | x->aead = xfrm_algo_aead_clone(orig->aead); |
| 1169 | if (!x->aead) |
| 1170 | goto error; |
| 1171 | } |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1172 | if (orig->ealg) { |
| 1173 | x->ealg = xfrm_algo_clone(orig->ealg); |
| 1174 | if (!x->ealg) |
| 1175 | goto error; |
| 1176 | } |
| 1177 | x->props.ealgo = orig->props.ealgo; |
| 1178 | |
| 1179 | if (orig->calg) { |
| 1180 | x->calg = xfrm_algo_clone(orig->calg); |
| 1181 | if (!x->calg) |
| 1182 | goto error; |
| 1183 | } |
| 1184 | x->props.calgo = orig->props.calgo; |
| 1185 | |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 1186 | if (orig->encap) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1187 | x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL); |
| 1188 | if (!x->encap) |
| 1189 | goto error; |
| 1190 | } |
| 1191 | |
| 1192 | if (orig->coaddr) { |
| 1193 | x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr), |
| 1194 | GFP_KERNEL); |
| 1195 | if (!x->coaddr) |
| 1196 | goto error; |
| 1197 | } |
| 1198 | |
Steffen Klassert | af2f464 | 2011-03-28 19:46:39 +0000 | [diff] [blame] | 1199 | if (orig->replay_esn) { |
Steffen Klassert | cc9ab60 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1200 | if (xfrm_replay_clone(x, orig)) |
Steffen Klassert | af2f464 | 2011-03-28 19:46:39 +0000 | [diff] [blame] | 1201 | goto error; |
| 1202 | } |
| 1203 | |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1204 | memcpy(&x->mark, &orig->mark, sizeof(x->mark)); |
| 1205 | |
Steffen Klassert | cc9ab60 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1206 | if (xfrm_init_state(x) < 0) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1207 | goto error; |
| 1208 | |
| 1209 | x->props.flags = orig->props.flags; |
Nicolas Dichtel | a947b0a | 2013-02-22 10:54:54 +0100 | [diff] [blame] | 1210 | x->props.extra_flags = orig->props.extra_flags; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1211 | |
Steffen Klassert | ee5c231 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1212 | x->tfcpad = orig->tfcpad; |
| 1213 | x->replay_maxdiff = orig->replay_maxdiff; |
| 1214 | x->replay_maxage = orig->replay_maxage; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1215 | x->curlft.add_time = orig->curlft.add_time; |
| 1216 | x->km.state = orig->km.state; |
| 1217 | x->km.seq = orig->km.seq; |
| 1218 | |
| 1219 | return x; |
| 1220 | |
| 1221 | error: |
Herbert Xu | 553f911 | 2010-02-15 20:00:51 +0000 | [diff] [blame] | 1222 | xfrm_state_put(x); |
| 1223 | out: |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1224 | return NULL; |
| 1225 | } |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1226 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1227 | struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1228 | { |
| 1229 | unsigned int h; |
Steffen Klassert | 8c0cba2 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1230 | struct xfrm_state *x = NULL; |
| 1231 | |
| 1232 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1233 | |
| 1234 | if (m->reqid) { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1235 | h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr, |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1236 | m->reqid, m->old_family); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1237 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1238 | if (x->props.mode != m->mode || |
| 1239 | x->id.proto != m->proto) |
| 1240 | continue; |
| 1241 | if (m->reqid && x->props.reqid != m->reqid) |
| 1242 | continue; |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 1243 | if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, |
| 1244 | m->old_family) || |
| 1245 | !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, |
| 1246 | m->old_family)) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1247 | continue; |
| 1248 | xfrm_state_hold(x); |
Steffen Klassert | 8c0cba2 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1249 | break; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1250 | } |
| 1251 | } else { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1252 | h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr, |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1253 | m->old_family); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1254 | hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1255 | if (x->props.mode != m->mode || |
| 1256 | x->id.proto != m->proto) |
| 1257 | continue; |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 1258 | if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, |
| 1259 | m->old_family) || |
| 1260 | !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, |
| 1261 | m->old_family)) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1262 | continue; |
| 1263 | xfrm_state_hold(x); |
Steffen Klassert | 8c0cba2 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1264 | break; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | |
Steffen Klassert | 8c0cba2 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1268 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
| 1269 | |
| 1270 | return x; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1271 | } |
| 1272 | EXPORT_SYMBOL(xfrm_migrate_state_find); |
| 1273 | |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 1274 | struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x, |
| 1275 | struct xfrm_migrate *m) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1276 | { |
| 1277 | struct xfrm_state *xc; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1278 | |
Steffen Klassert | cc9ab60 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1279 | xc = xfrm_state_clone(x); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1280 | if (!xc) |
| 1281 | return NULL; |
| 1282 | |
| 1283 | memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr)); |
| 1284 | memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr)); |
| 1285 | |
| 1286 | /* add state */ |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 1287 | if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1288 | /* a care is needed when the destination address of the |
| 1289 | state is to be updated as it is a part of triplet */ |
| 1290 | xfrm_state_insert(xc); |
| 1291 | } else { |
Steffen Klassert | cc9ab60 | 2014-02-19 13:33:24 +0100 | [diff] [blame] | 1292 | if (xfrm_state_add(xc) < 0) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1293 | goto error; |
| 1294 | } |
| 1295 | |
| 1296 | return xc; |
| 1297 | error: |
Thomas Egerer | 78347c8 | 2010-12-06 23:28:56 +0000 | [diff] [blame] | 1298 | xfrm_state_put(xc); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1299 | return NULL; |
| 1300 | } |
| 1301 | EXPORT_SYMBOL(xfrm_state_migrate); |
| 1302 | #endif |
| 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | int xfrm_state_update(struct xfrm_state *x) |
| 1305 | { |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1306 | struct xfrm_state *x1, *to_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | int err; |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 1308 | int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1309 | struct net *net = xs_net(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1311 | to_put = NULL; |
| 1312 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1313 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 1314 | x1 = __xfrm_state_locate(x, use_spi, x->props.family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
| 1316 | err = -ESRCH; |
| 1317 | if (!x1) |
| 1318 | goto out; |
| 1319 | |
| 1320 | if (xfrm_state_kern(x1)) { |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1321 | to_put = x1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | err = -EEXIST; |
| 1323 | goto out; |
| 1324 | } |
| 1325 | |
| 1326 | if (x1->km.state == XFRM_STATE_ACQ) { |
| 1327 | __xfrm_state_insert(x); |
| 1328 | x = NULL; |
| 1329 | } |
| 1330 | err = 0; |
| 1331 | |
| 1332 | out: |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1333 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
David S. Miller | 37b08e3 | 2008-09-02 20:14:15 -0700 | [diff] [blame] | 1335 | if (to_put) |
| 1336 | xfrm_state_put(to_put); |
| 1337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | if (err) |
| 1339 | return err; |
| 1340 | |
| 1341 | if (!x) { |
| 1342 | xfrm_state_delete(x1); |
| 1343 | xfrm_state_put(x1); |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | err = -EINVAL; |
| 1348 | spin_lock_bh(&x1->lock); |
| 1349 | if (likely(x1->km.state == XFRM_STATE_VALID)) { |
| 1350 | if (x->encap && x1->encap) |
| 1351 | memcpy(x1->encap, x->encap, sizeof(*x1->encap)); |
Noriaki TAKAMIYA | 060f02a | 2006-08-23 18:18:55 -0700 | [diff] [blame] | 1352 | if (x->coaddr && x1->coaddr) { |
| 1353 | memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr)); |
| 1354 | } |
| 1355 | if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel))) |
| 1356 | memcpy(&x1->sel, &x->sel, sizeof(x1->sel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | memcpy(&x1->lft, &x->lft, sizeof(x1->lft)); |
| 1358 | x1->km.dying = 0; |
| 1359 | |
Yury Polyanskiy | 9e0d57f | 2009-11-08 20:58:41 -0800 | [diff] [blame] | 1360 | tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | if (x1->curlft.use_time) |
| 1362 | xfrm_state_check_expire(x1); |
| 1363 | |
| 1364 | err = 0; |
Tushar Gohad | 8fcbc63 | 2011-07-07 15:38:52 +0000 | [diff] [blame] | 1365 | x->km.state = XFRM_STATE_DEAD; |
| 1366 | __xfrm_state_put(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | } |
| 1368 | spin_unlock_bh(&x1->lock); |
| 1369 | |
| 1370 | xfrm_state_put(x1); |
| 1371 | |
| 1372 | return err; |
| 1373 | } |
| 1374 | EXPORT_SYMBOL(xfrm_state_update); |
| 1375 | |
| 1376 | int xfrm_state_check_expire(struct xfrm_state *x) |
| 1377 | { |
| 1378 | if (!x->curlft.use_time) |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 1379 | x->curlft.use_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | if (x->curlft.bytes >= x->lft.hard_byte_limit || |
| 1382 | x->curlft.packets >= x->lft.hard_packet_limit) { |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1383 | x->km.state = XFRM_STATE_EXPIRED; |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 1384 | tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | return -EINVAL; |
| 1386 | } |
| 1387 | |
| 1388 | if (!x->km.dying && |
| 1389 | (x->curlft.bytes >= x->lft.soft_byte_limit || |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1390 | x->curlft.packets >= x->lft.soft_packet_limit)) { |
| 1391 | x->km.dying = 1; |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 1392 | km_state_expired(x, 0, 0); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | return 0; |
| 1395 | } |
| 1396 | EXPORT_SYMBOL(xfrm_state_check_expire); |
| 1397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | struct xfrm_state * |
David S. Miller | a70486f | 2011-02-27 23:17:24 -0800 | [diff] [blame] | 1399 | xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi, |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1400 | u8 proto, unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | { |
| 1402 | struct xfrm_state *x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1404 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1405 | x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1406 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | return x; |
| 1408 | } |
| 1409 | EXPORT_SYMBOL(xfrm_state_lookup); |
| 1410 | |
| 1411 | struct xfrm_state * |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1412 | xfrm_state_lookup_byaddr(struct net *net, u32 mark, |
David S. Miller | a70486f | 2011-02-27 23:17:24 -0800 | [diff] [blame] | 1413 | const xfrm_address_t *daddr, const xfrm_address_t *saddr, |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 1414 | u8 proto, unsigned short family) |
| 1415 | { |
| 1416 | struct xfrm_state *x; |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 1417 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1418 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1419 | x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1420 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 1421 | return x; |
| 1422 | } |
| 1423 | EXPORT_SYMBOL(xfrm_state_lookup_byaddr); |
| 1424 | |
| 1425 | struct xfrm_state * |
Mathias Krause | e473fcb | 2013-06-26 23:56:58 +0200 | [diff] [blame] | 1426 | xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid, |
| 1427 | u8 proto, const xfrm_address_t *daddr, |
| 1428 | const xfrm_address_t *saddr, int create, unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | { |
| 1430 | struct xfrm_state *x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1432 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1433 | x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1434 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | return x; |
| 1437 | } |
| 1438 | EXPORT_SYMBOL(xfrm_find_acq); |
| 1439 | |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1440 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1441 | int |
| 1442 | xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n, |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1443 | unsigned short family, struct net *net) |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1444 | { |
| 1445 | int err = 0; |
| 1446 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
| 1447 | if (!afinfo) |
| 1448 | return -EAFNOSUPPORT; |
| 1449 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1450 | spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/ |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1451 | if (afinfo->tmpl_sort) |
| 1452 | err = afinfo->tmpl_sort(dst, src, n); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1453 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1454 | xfrm_state_put_afinfo(afinfo); |
| 1455 | return err; |
| 1456 | } |
| 1457 | EXPORT_SYMBOL(xfrm_tmpl_sort); |
| 1458 | |
| 1459 | int |
| 1460 | xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n, |
| 1461 | unsigned short family) |
| 1462 | { |
| 1463 | int err = 0; |
| 1464 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
Steffen Klassert | 35ea790d | 2014-02-19 13:33:23 +0100 | [diff] [blame] | 1465 | struct net *net = xs_net(*src); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1466 | |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1467 | if (!afinfo) |
| 1468 | return -EAFNOSUPPORT; |
| 1469 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1470 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1471 | if (afinfo->state_sort) |
| 1472 | err = afinfo->state_sort(dst, src, n); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1473 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1474 | xfrm_state_put_afinfo(afinfo); |
| 1475 | return err; |
| 1476 | } |
| 1477 | EXPORT_SYMBOL(xfrm_state_sort); |
| 1478 | #endif |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | /* Silly enough, but I'm lazy to build resolution list */ |
| 1481 | |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1482 | static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | { |
| 1484 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
Alexey Dobriyan | 5447c5e | 2008-11-25 17:31:51 -0800 | [diff] [blame] | 1486 | for (i = 0; i <= net->xfrm.state_hmask; i++) { |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1487 | struct xfrm_state *x; |
| 1488 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1489 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1490 | if (x->km.seq == seq && |
Jamal Hadi Salim | 3d6acfa | 2010-02-22 11:32:56 +0000 | [diff] [blame] | 1491 | (mark & x->mark.m) == x->mark.v && |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1492 | x->km.state == XFRM_STATE_ACQ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | xfrm_state_hold(x); |
| 1494 | return x; |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1501 | struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | { |
| 1503 | struct xfrm_state *x; |
| 1504 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1505 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1506 | x = __xfrm_find_acq_byseq(net, mark, seq); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1507 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | return x; |
| 1509 | } |
| 1510 | EXPORT_SYMBOL(xfrm_find_acq_byseq); |
| 1511 | |
| 1512 | u32 xfrm_get_acqseq(void) |
| 1513 | { |
| 1514 | u32 res; |
jamal | 6836b9b | 2010-02-16 02:01:22 +0000 | [diff] [blame] | 1515 | static atomic_t acqseq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | |
jamal | 6836b9b | 2010-02-16 02:01:22 +0000 | [diff] [blame] | 1517 | do { |
| 1518 | res = atomic_inc_return(&acqseq); |
| 1519 | } while (!res); |
| 1520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | return res; |
| 1522 | } |
| 1523 | EXPORT_SYMBOL(xfrm_get_acqseq); |
| 1524 | |
Fan Du | 776e9dd | 2013-12-16 18:47:49 +0800 | [diff] [blame] | 1525 | int verify_spi_info(u8 proto, u32 min, u32 max) |
| 1526 | { |
| 1527 | switch (proto) { |
| 1528 | case IPPROTO_AH: |
| 1529 | case IPPROTO_ESP: |
| 1530 | break; |
| 1531 | |
| 1532 | case IPPROTO_COMP: |
| 1533 | /* IPCOMP spi is 16-bits. */ |
| 1534 | if (max >= 0x10000) |
| 1535 | return -EINVAL; |
| 1536 | break; |
| 1537 | |
| 1538 | default: |
| 1539 | return -EINVAL; |
| 1540 | } |
| 1541 | |
| 1542 | if (min > max) |
| 1543 | return -EINVAL; |
| 1544 | |
| 1545 | return 0; |
| 1546 | } |
| 1547 | EXPORT_SYMBOL(verify_spi_info); |
| 1548 | |
Herbert Xu | 658b219 | 2007-10-09 13:29:52 -0700 | [diff] [blame] | 1549 | int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { |
Alexey Dobriyan | 221df1e | 2008-11-25 17:30:50 -0800 | [diff] [blame] | 1551 | struct net *net = xs_net(x); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1552 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | struct xfrm_state *x0; |
Herbert Xu | 658b219 | 2007-10-09 13:29:52 -0700 | [diff] [blame] | 1554 | int err = -ENOENT; |
| 1555 | __be32 minspi = htonl(low); |
| 1556 | __be32 maxspi = htonl(high); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1557 | u32 mark = x->mark.v & x->mark.m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Herbert Xu | 658b219 | 2007-10-09 13:29:52 -0700 | [diff] [blame] | 1559 | spin_lock_bh(&x->lock); |
| 1560 | if (x->km.state == XFRM_STATE_DEAD) |
| 1561 | goto unlock; |
| 1562 | |
| 1563 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | if (x->id.spi) |
Herbert Xu | 658b219 | 2007-10-09 13:29:52 -0700 | [diff] [blame] | 1565 | goto unlock; |
| 1566 | |
| 1567 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
| 1569 | if (minspi == maxspi) { |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1570 | x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (x0) { |
| 1572 | xfrm_state_put(x0); |
Herbert Xu | 658b219 | 2007-10-09 13:29:52 -0700 | [diff] [blame] | 1573 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
| 1575 | x->id.spi = minspi; |
| 1576 | } else { |
| 1577 | u32 spi = 0; |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 1578 | for (h = 0; h < high-low+1; h++) { |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 1579 | spi = low + prandom_u32()%(high-low+1); |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 1580 | x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | if (x0 == NULL) { |
| 1582 | x->id.spi = htonl(spi); |
| 1583 | break; |
| 1584 | } |
| 1585 | xfrm_state_put(x0); |
| 1586 | } |
| 1587 | } |
| 1588 | if (x->id.spi) { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1589 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Alexey Dobriyan | 12604d8 | 2008-11-25 17:31:18 -0800 | [diff] [blame] | 1590 | h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); |
| 1591 | hlist_add_head(&x->byspi, net->xfrm.state_byspi+h); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1592 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Herbert Xu | 658b219 | 2007-10-09 13:29:52 -0700 | [diff] [blame] | 1593 | |
| 1594 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | } |
Herbert Xu | 658b219 | 2007-10-09 13:29:52 -0700 | [diff] [blame] | 1596 | |
| 1597 | unlock: |
| 1598 | spin_unlock_bh(&x->lock); |
| 1599 | |
| 1600 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | } |
| 1602 | EXPORT_SYMBOL(xfrm_alloc_spi); |
| 1603 | |
Nicolas Dichtel | d362309 | 2014-02-14 15:30:36 +0100 | [diff] [blame] | 1604 | static bool __xfrm_state_filter_match(struct xfrm_state *x, |
Nicolas Dichtel | 870a2df | 2014-03-06 18:24:29 +0100 | [diff] [blame] | 1605 | struct xfrm_address_filter *filter) |
Nicolas Dichtel | d362309 | 2014-02-14 15:30:36 +0100 | [diff] [blame] | 1606 | { |
| 1607 | if (filter) { |
| 1608 | if ((filter->family == AF_INET || |
| 1609 | filter->family == AF_INET6) && |
| 1610 | x->props.family != filter->family) |
| 1611 | return false; |
| 1612 | |
| 1613 | return addr_match(&x->props.saddr, &filter->saddr, |
| 1614 | filter->splen) && |
| 1615 | addr_match(&x->id.daddr, &filter->daddr, |
| 1616 | filter->dplen); |
| 1617 | } |
| 1618 | return true; |
| 1619 | } |
| 1620 | |
Alexey Dobriyan | 284fa7d | 2008-11-25 17:32:14 -0800 | [diff] [blame] | 1621 | int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1622 | int (*func)(struct xfrm_state *, int, void*), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | void *data) |
| 1624 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1625 | struct xfrm_state *state; |
| 1626 | struct xfrm_state_walk *x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | int err = 0; |
| 1628 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1629 | if (walk->seq != 0 && list_empty(&walk->all)) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1630 | return 0; |
| 1631 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1632 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1633 | if (list_empty(&walk->all)) |
Alexey Dobriyan | 284fa7d | 2008-11-25 17:32:14 -0800 | [diff] [blame] | 1634 | x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1635 | else |
| 1636 | x = list_entry(&walk->all, struct xfrm_state_walk, all); |
Alexey Dobriyan | 284fa7d | 2008-11-25 17:32:14 -0800 | [diff] [blame] | 1637 | list_for_each_entry_from(x, &net->xfrm.state_all, all) { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1638 | if (x->state == XFRM_STATE_DEAD) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1639 | continue; |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1640 | state = container_of(x, struct xfrm_state, km); |
| 1641 | if (!xfrm_id_proto_match(state->id.proto, walk->proto)) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1642 | continue; |
Nicolas Dichtel | d362309 | 2014-02-14 15:30:36 +0100 | [diff] [blame] | 1643 | if (!__xfrm_state_filter_match(state, walk->filter)) |
| 1644 | continue; |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1645 | err = func(state, walk->seq, data); |
| 1646 | if (err) { |
| 1647 | list_move_tail(&walk->all, &x->all); |
| 1648 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1650 | walk->seq++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1652 | if (walk->seq == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | err = -ENOENT; |
| 1654 | goto out; |
| 1655 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1656 | list_del_init(&walk->all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | out: |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1658 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | return err; |
| 1660 | } |
| 1661 | EXPORT_SYMBOL(xfrm_state_walk); |
| 1662 | |
Nicolas Dichtel | d362309 | 2014-02-14 15:30:36 +0100 | [diff] [blame] | 1663 | void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto, |
Nicolas Dichtel | 870a2df | 2014-03-06 18:24:29 +0100 | [diff] [blame] | 1664 | struct xfrm_address_filter *filter) |
Herbert Xu | 5c18245 | 2008-09-22 19:48:19 -0700 | [diff] [blame] | 1665 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1666 | INIT_LIST_HEAD(&walk->all); |
Herbert Xu | 5c18245 | 2008-09-22 19:48:19 -0700 | [diff] [blame] | 1667 | walk->proto = proto; |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1668 | walk->state = XFRM_STATE_DEAD; |
| 1669 | walk->seq = 0; |
Nicolas Dichtel | d362309 | 2014-02-14 15:30:36 +0100 | [diff] [blame] | 1670 | walk->filter = filter; |
Herbert Xu | 5c18245 | 2008-09-22 19:48:19 -0700 | [diff] [blame] | 1671 | } |
| 1672 | EXPORT_SYMBOL(xfrm_state_walk_init); |
| 1673 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1674 | void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net) |
Herbert Xu | abb81c4 | 2008-09-09 19:58:29 -0700 | [diff] [blame] | 1675 | { |
Nicolas Dichtel | d362309 | 2014-02-14 15:30:36 +0100 | [diff] [blame] | 1676 | kfree(walk->filter); |
| 1677 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1678 | if (list_empty(&walk->all)) |
Herbert Xu | 5c18245 | 2008-09-22 19:48:19 -0700 | [diff] [blame] | 1679 | return; |
Herbert Xu | 5c18245 | 2008-09-22 19:48:19 -0700 | [diff] [blame] | 1680 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1681 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1682 | list_del(&walk->all); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1683 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
Herbert Xu | abb81c4 | 2008-09-09 19:58:29 -0700 | [diff] [blame] | 1684 | } |
| 1685 | EXPORT_SYMBOL(xfrm_state_walk_done); |
| 1686 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1687 | static void xfrm_replay_timer_handler(unsigned long data) |
| 1688 | { |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 1689 | struct xfrm_state *x = (struct xfrm_state *)data; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1690 | |
| 1691 | spin_lock(&x->lock); |
| 1692 | |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 1693 | if (x->km.state == XFRM_STATE_VALID) { |
Alexey Dobriyan | a6483b7 | 2008-11-25 17:38:20 -0800 | [diff] [blame] | 1694 | if (xfrm_aevent_is_on(xs_net(x))) |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 1695 | x->repl->notify(x, XFRM_REPLAY_TIMEOUT); |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 1696 | else |
| 1697 | x->xflags |= XFRM_TIME_DEFER; |
| 1698 | } |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1699 | |
| 1700 | spin_unlock(&x->lock); |
| 1701 | } |
| 1702 | |
Denis Cheng | df01812 | 2007-12-07 00:51:11 -0800 | [diff] [blame] | 1703 | static LIST_HEAD(xfrm_km_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | |
David S. Miller | 214e005 | 2011-02-24 00:02:38 -0500 | [diff] [blame] | 1705 | void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | { |
| 1707 | struct xfrm_mgr *km; |
| 1708 | |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1709 | rcu_read_lock(); |
| 1710 | list_for_each_entry_rcu(km, &xfrm_km_list, list) |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1711 | if (km->notify_policy) |
| 1712 | km->notify_policy(xp, dir, c); |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1713 | rcu_read_unlock(); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1714 | } |
| 1715 | |
David S. Miller | 214e005 | 2011-02-24 00:02:38 -0500 | [diff] [blame] | 1716 | void km_state_notify(struct xfrm_state *x, const struct km_event *c) |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1717 | { |
| 1718 | struct xfrm_mgr *km; |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1719 | rcu_read_lock(); |
| 1720 | list_for_each_entry_rcu(km, &xfrm_km_list, list) |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1721 | if (km->notify) |
| 1722 | km->notify(x, c); |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1723 | rcu_read_unlock(); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | EXPORT_SYMBOL(km_policy_notify); |
| 1727 | EXPORT_SYMBOL(km_state_notify); |
| 1728 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1729 | void km_state_expired(struct xfrm_state *x, int hard, u32 portid) |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1730 | { |
| 1731 | struct km_event c; |
| 1732 | |
Herbert Xu | bf08867 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 1733 | c.data.hard = hard; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1734 | c.portid = portid; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1735 | c.event = XFRM_MSG_EXPIRE; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1736 | km_state_notify(x, &c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 1739 | EXPORT_SYMBOL(km_state_expired); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1740 | /* |
| 1741 | * We send to all registered managers regardless of failure |
| 1742 | * We are happy with one success |
| 1743 | */ |
Jamal Hadi Salim | 980ebd2 | 2006-03-20 19:16:40 -0800 | [diff] [blame] | 1744 | int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1746 | int err = -EINVAL, acqret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | struct xfrm_mgr *km; |
| 1748 | |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1749 | rcu_read_lock(); |
| 1750 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { |
Fan Du | 65e0736 | 2012-08-15 10:13:47 +0800 | [diff] [blame] | 1751 | acqret = km->acquire(x, t, pol); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1752 | if (!acqret) |
| 1753 | err = acqret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | } |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1755 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | return err; |
| 1757 | } |
Jamal Hadi Salim | 980ebd2 | 2006-03-20 19:16:40 -0800 | [diff] [blame] | 1758 | EXPORT_SYMBOL(km_query); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | |
Al Viro | 5d36b18 | 2006-11-08 00:24:06 -0800 | [diff] [blame] | 1760 | int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | { |
| 1762 | int err = -EINVAL; |
| 1763 | struct xfrm_mgr *km; |
| 1764 | |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1765 | rcu_read_lock(); |
| 1766 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | if (km->new_mapping) |
| 1768 | err = km->new_mapping(x, ipaddr, sport); |
| 1769 | if (!err) |
| 1770 | break; |
| 1771 | } |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1772 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | return err; |
| 1774 | } |
| 1775 | EXPORT_SYMBOL(km_new_mapping); |
| 1776 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1777 | void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1779 | struct km_event c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
Herbert Xu | bf08867 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 1781 | c.data.hard = hard; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1782 | c.portid = portid; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1783 | c.event = XFRM_MSG_POLEXPIRE; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1784 | km_policy_notify(pol, dir, &c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | } |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 1786 | EXPORT_SYMBOL(km_policy_expired); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
Eric Dumazet | 2d60abc | 2008-01-03 20:43:21 -0800 | [diff] [blame] | 1788 | #ifdef CONFIG_XFRM_MIGRATE |
David S. Miller | 183cad1 | 2011-02-24 00:28:01 -0500 | [diff] [blame] | 1789 | int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
| 1790 | const struct xfrm_migrate *m, int num_migrate, |
| 1791 | const struct xfrm_kmaddress *k) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1792 | { |
| 1793 | int err = -EINVAL; |
| 1794 | int ret; |
| 1795 | struct xfrm_mgr *km; |
| 1796 | |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1797 | rcu_read_lock(); |
| 1798 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1799 | if (km->migrate) { |
Arnaud Ebalard | 13c1d18 | 2008-10-05 13:33:42 -0700 | [diff] [blame] | 1800 | ret = km->migrate(sel, dir, type, m, num_migrate, k); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1801 | if (!ret) |
| 1802 | err = ret; |
| 1803 | } |
| 1804 | } |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1805 | rcu_read_unlock(); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1806 | return err; |
| 1807 | } |
| 1808 | EXPORT_SYMBOL(km_migrate); |
Eric Dumazet | 2d60abc | 2008-01-03 20:43:21 -0800 | [diff] [blame] | 1809 | #endif |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 1810 | |
Alexey Dobriyan | db983c1 | 2008-11-25 17:51:01 -0800 | [diff] [blame] | 1811 | int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr) |
Masahide NAKAMURA | 97a64b4 | 2006-08-23 20:44:06 -0700 | [diff] [blame] | 1812 | { |
| 1813 | int err = -EINVAL; |
| 1814 | int ret; |
| 1815 | struct xfrm_mgr *km; |
| 1816 | |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1817 | rcu_read_lock(); |
| 1818 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { |
Masahide NAKAMURA | 97a64b4 | 2006-08-23 20:44:06 -0700 | [diff] [blame] | 1819 | if (km->report) { |
Alexey Dobriyan | db983c1 | 2008-11-25 17:51:01 -0800 | [diff] [blame] | 1820 | ret = km->report(net, proto, sel, addr); |
Masahide NAKAMURA | 97a64b4 | 2006-08-23 20:44:06 -0700 | [diff] [blame] | 1821 | if (!ret) |
| 1822 | err = ret; |
| 1823 | } |
| 1824 | } |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1825 | rcu_read_unlock(); |
Masahide NAKAMURA | 97a64b4 | 2006-08-23 20:44:06 -0700 | [diff] [blame] | 1826 | return err; |
| 1827 | } |
| 1828 | EXPORT_SYMBOL(km_report); |
| 1829 | |
Horia Geanta | 0f24558 | 2014-02-12 16:20:06 +0200 | [diff] [blame] | 1830 | bool km_is_alive(const struct km_event *c) |
| 1831 | { |
| 1832 | struct xfrm_mgr *km; |
| 1833 | bool is_alive = false; |
| 1834 | |
| 1835 | rcu_read_lock(); |
| 1836 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { |
| 1837 | if (km->is_alive && km->is_alive(c)) { |
| 1838 | is_alive = true; |
| 1839 | break; |
| 1840 | } |
| 1841 | } |
| 1842 | rcu_read_unlock(); |
| 1843 | |
| 1844 | return is_alive; |
| 1845 | } |
| 1846 | EXPORT_SYMBOL(km_is_alive); |
| 1847 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) |
| 1849 | { |
| 1850 | int err; |
| 1851 | u8 *data; |
| 1852 | struct xfrm_mgr *km; |
| 1853 | struct xfrm_policy *pol = NULL; |
| 1854 | |
| 1855 | if (optlen <= 0 || optlen > PAGE_SIZE) |
| 1856 | return -EMSGSIZE; |
| 1857 | |
| 1858 | data = kmalloc(optlen, GFP_KERNEL); |
| 1859 | if (!data) |
| 1860 | return -ENOMEM; |
| 1861 | |
| 1862 | err = -EFAULT; |
| 1863 | if (copy_from_user(data, optval, optlen)) |
| 1864 | goto out; |
| 1865 | |
| 1866 | err = -EINVAL; |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1867 | rcu_read_lock(); |
| 1868 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { |
Venkat Yekkirala | cb969f0 | 2006-07-24 23:32:20 -0700 | [diff] [blame] | 1869 | pol = km->compile_policy(sk, optname, data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | optlen, &err); |
| 1871 | if (err >= 0) |
| 1872 | break; |
| 1873 | } |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1874 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
| 1876 | if (err >= 0) { |
| 1877 | xfrm_sk_policy_insert(sk, err, pol); |
| 1878 | xfrm_pol_put(pol); |
| 1879 | err = 0; |
| 1880 | } |
| 1881 | |
| 1882 | out: |
| 1883 | kfree(data); |
| 1884 | return err; |
| 1885 | } |
| 1886 | EXPORT_SYMBOL(xfrm_user_policy); |
| 1887 | |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1888 | static DEFINE_SPINLOCK(xfrm_km_lock); |
| 1889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | int xfrm_register_km(struct xfrm_mgr *km) |
| 1891 | { |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1892 | spin_lock_bh(&xfrm_km_lock); |
| 1893 | list_add_tail_rcu(&km->list, &xfrm_km_list); |
| 1894 | spin_unlock_bh(&xfrm_km_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | return 0; |
| 1896 | } |
| 1897 | EXPORT_SYMBOL(xfrm_register_km); |
| 1898 | |
| 1899 | int xfrm_unregister_km(struct xfrm_mgr *km) |
| 1900 | { |
Cong Wang | 85168c0 | 2013-01-16 16:05:06 +0800 | [diff] [blame] | 1901 | spin_lock_bh(&xfrm_km_lock); |
| 1902 | list_del_rcu(&km->list); |
| 1903 | spin_unlock_bh(&xfrm_km_lock); |
| 1904 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | return 0; |
| 1906 | } |
| 1907 | EXPORT_SYMBOL(xfrm_unregister_km); |
| 1908 | |
| 1909 | int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo) |
| 1910 | { |
| 1911 | int err = 0; |
| 1912 | if (unlikely(afinfo == NULL)) |
| 1913 | return -EINVAL; |
| 1914 | if (unlikely(afinfo->family >= NPROTO)) |
| 1915 | return -EAFNOSUPPORT; |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1916 | spin_lock_bh(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL)) |
| 1918 | err = -ENOBUFS; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 1919 | else |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1920 | rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo); |
| 1921 | spin_unlock_bh(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | return err; |
| 1923 | } |
| 1924 | EXPORT_SYMBOL(xfrm_state_register_afinfo); |
| 1925 | |
| 1926 | int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo) |
| 1927 | { |
| 1928 | int err = 0; |
| 1929 | if (unlikely(afinfo == NULL)) |
| 1930 | return -EINVAL; |
| 1931 | if (unlikely(afinfo->family >= NPROTO)) |
| 1932 | return -EAFNOSUPPORT; |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1933 | spin_lock_bh(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) { |
| 1935 | if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo)) |
| 1936 | err = -EINVAL; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 1937 | else |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1938 | RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | } |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1940 | spin_unlock_bh(&xfrm_state_afinfo_lock); |
| 1941 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | return err; |
| 1943 | } |
| 1944 | EXPORT_SYMBOL(xfrm_state_unregister_afinfo); |
| 1945 | |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 1946 | struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | { |
| 1948 | struct xfrm_state_afinfo *afinfo; |
| 1949 | if (unlikely(family >= NPROTO)) |
| 1950 | return NULL; |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1951 | rcu_read_lock(); |
| 1952 | afinfo = rcu_dereference(xfrm_state_afinfo[family]); |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 1953 | if (unlikely(!afinfo)) |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1954 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | return afinfo; |
| 1956 | } |
| 1957 | |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 1958 | void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | { |
Cong Wang | 44abdc3 | 2013-01-16 16:05:05 +0800 | [diff] [blame] | 1960 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */ |
| 1964 | void xfrm_state_delete_tunnel(struct xfrm_state *x) |
| 1965 | { |
| 1966 | if (x->tunnel) { |
| 1967 | struct xfrm_state *t = x->tunnel; |
| 1968 | |
| 1969 | if (atomic_read(&t->tunnel_users) == 2) |
| 1970 | xfrm_state_delete(t); |
| 1971 | atomic_dec(&t->tunnel_users); |
| 1972 | xfrm_state_put(t); |
| 1973 | x->tunnel = NULL; |
| 1974 | } |
| 1975 | } |
| 1976 | EXPORT_SYMBOL(xfrm_state_delete_tunnel); |
| 1977 | |
| 1978 | int xfrm_state_mtu(struct xfrm_state *x, int mtu) |
| 1979 | { |
Patrick McHardy | c5c2523 | 2007-04-09 11:47:18 -0700 | [diff] [blame] | 1980 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
Patrick McHardy | c5c2523 | 2007-04-09 11:47:18 -0700 | [diff] [blame] | 1982 | spin_lock_bh(&x->lock); |
| 1983 | if (x->km.state == XFRM_STATE_VALID && |
| 1984 | x->type && x->type->get_mtu) |
| 1985 | res = x->type->get_mtu(x, mtu); |
| 1986 | else |
Patrick McHardy | 2812161 | 2007-06-18 22:30:15 -0700 | [diff] [blame] | 1987 | res = mtu - x->props.header_len; |
Patrick McHardy | c5c2523 | 2007-04-09 11:47:18 -0700 | [diff] [blame] | 1988 | spin_unlock_bh(&x->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | return res; |
| 1990 | } |
| 1991 | |
Wei Yongjun | a454f0c | 2011-03-21 18:08:28 -0700 | [diff] [blame] | 1992 | int __xfrm_init_state(struct xfrm_state *x, bool init_replay) |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 1993 | { |
Herbert Xu | d094cd8 | 2005-06-20 13:19:41 -0700 | [diff] [blame] | 1994 | struct xfrm_state_afinfo *afinfo; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 1995 | struct xfrm_mode *inner_mode; |
Herbert Xu | d094cd8 | 2005-06-20 13:19:41 -0700 | [diff] [blame] | 1996 | int family = x->props.family; |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 1997 | int err; |
| 1998 | |
Herbert Xu | d094cd8 | 2005-06-20 13:19:41 -0700 | [diff] [blame] | 1999 | err = -EAFNOSUPPORT; |
| 2000 | afinfo = xfrm_state_get_afinfo(family); |
| 2001 | if (!afinfo) |
| 2002 | goto error; |
| 2003 | |
| 2004 | err = 0; |
| 2005 | if (afinfo->init_flags) |
| 2006 | err = afinfo->init_flags(x); |
| 2007 | |
| 2008 | xfrm_state_put_afinfo(afinfo); |
| 2009 | |
| 2010 | if (err) |
| 2011 | goto error; |
| 2012 | |
| 2013 | err = -EPROTONOSUPPORT; |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 2014 | |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 2015 | if (x->sel.family != AF_UNSPEC) { |
| 2016 | inner_mode = xfrm_get_mode(x->props.mode, x->sel.family); |
| 2017 | if (inner_mode == NULL) |
| 2018 | goto error; |
| 2019 | |
| 2020 | if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) && |
| 2021 | family != x->sel.family) { |
| 2022 | xfrm_put_mode(inner_mode); |
| 2023 | goto error; |
| 2024 | } |
| 2025 | |
| 2026 | x->inner_mode = inner_mode; |
| 2027 | } else { |
| 2028 | struct xfrm_mode *inner_mode_iaf; |
Martin Willi | d81d228 | 2008-12-03 15:38:07 -0800 | [diff] [blame] | 2029 | int iafamily = AF_INET; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 2030 | |
Martin Willi | d81d228 | 2008-12-03 15:38:07 -0800 | [diff] [blame] | 2031 | inner_mode = xfrm_get_mode(x->props.mode, x->props.family); |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 2032 | if (inner_mode == NULL) |
| 2033 | goto error; |
| 2034 | |
| 2035 | if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) { |
| 2036 | xfrm_put_mode(inner_mode); |
| 2037 | goto error; |
| 2038 | } |
Martin Willi | d81d228 | 2008-12-03 15:38:07 -0800 | [diff] [blame] | 2039 | x->inner_mode = inner_mode; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 2040 | |
Martin Willi | d81d228 | 2008-12-03 15:38:07 -0800 | [diff] [blame] | 2041 | if (x->props.family == AF_INET) |
| 2042 | iafamily = AF_INET6; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 2043 | |
Martin Willi | d81d228 | 2008-12-03 15:38:07 -0800 | [diff] [blame] | 2044 | inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily); |
| 2045 | if (inner_mode_iaf) { |
| 2046 | if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL) |
| 2047 | x->inner_mode_iaf = inner_mode_iaf; |
| 2048 | else |
| 2049 | xfrm_put_mode(inner_mode_iaf); |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 2050 | } |
| 2051 | } |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 2052 | |
Herbert Xu | d094cd8 | 2005-06-20 13:19:41 -0700 | [diff] [blame] | 2053 | x->type = xfrm_get_type(x->id.proto, family); |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 2054 | if (x->type == NULL) |
| 2055 | goto error; |
| 2056 | |
| 2057 | err = x->type->init_state(x); |
| 2058 | if (err) |
| 2059 | goto error; |
| 2060 | |
Herbert Xu | 1399637 | 2007-10-17 21:35:51 -0700 | [diff] [blame] | 2061 | x->outer_mode = xfrm_get_mode(x->props.mode, family); |
Julia Lawall | 599901c | 2012-08-29 06:49:15 +0000 | [diff] [blame] | 2062 | if (x->outer_mode == NULL) { |
| 2063 | err = -EPROTONOSUPPORT; |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 2064 | goto error; |
Julia Lawall | 599901c | 2012-08-29 06:49:15 +0000 | [diff] [blame] | 2065 | } |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 2066 | |
Wei Yongjun | a454f0c | 2011-03-21 18:08:28 -0700 | [diff] [blame] | 2067 | if (init_replay) { |
| 2068 | err = xfrm_init_replay(x); |
| 2069 | if (err) |
| 2070 | goto error; |
| 2071 | } |
| 2072 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 2073 | x->km.state = XFRM_STATE_VALID; |
| 2074 | |
| 2075 | error: |
| 2076 | return err; |
| 2077 | } |
| 2078 | |
Wei Yongjun | a454f0c | 2011-03-21 18:08:28 -0700 | [diff] [blame] | 2079 | EXPORT_SYMBOL(__xfrm_init_state); |
| 2080 | |
| 2081 | int xfrm_init_state(struct xfrm_state *x) |
| 2082 | { |
| 2083 | return __xfrm_init_state(x, true); |
| 2084 | } |
| 2085 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 2086 | EXPORT_SYMBOL(xfrm_init_state); |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 2087 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2088 | int __net_init xfrm_state_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 2090 | unsigned int sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | |
Alexey Dobriyan | 9d4139c | 2008-11-25 17:16:11 -0800 | [diff] [blame] | 2092 | INIT_LIST_HEAD(&net->xfrm.state_all); |
| 2093 | |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 2094 | sz = sizeof(struct hlist_head) * 8; |
| 2095 | |
Alexey Dobriyan | 73d189d | 2008-11-25 17:16:58 -0800 | [diff] [blame] | 2096 | net->xfrm.state_bydst = xfrm_hash_alloc(sz); |
| 2097 | if (!net->xfrm.state_bydst) |
| 2098 | goto out_bydst; |
Alexey Dobriyan | d320bbb | 2008-11-25 17:17:24 -0800 | [diff] [blame] | 2099 | net->xfrm.state_bysrc = xfrm_hash_alloc(sz); |
| 2100 | if (!net->xfrm.state_bysrc) |
| 2101 | goto out_bysrc; |
Alexey Dobriyan | b754a4f | 2008-11-25 17:17:47 -0800 | [diff] [blame] | 2102 | net->xfrm.state_byspi = xfrm_hash_alloc(sz); |
| 2103 | if (!net->xfrm.state_byspi) |
| 2104 | goto out_byspi; |
Alexey Dobriyan | 529983e | 2008-11-25 17:18:12 -0800 | [diff] [blame] | 2105 | net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 2106 | |
Alexey Dobriyan | 0bf7c5b | 2008-11-25 17:18:39 -0800 | [diff] [blame] | 2107 | net->xfrm.state_num = 0; |
Alexey Dobriyan | 6308273 | 2008-11-25 17:19:07 -0800 | [diff] [blame] | 2108 | INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize); |
Alexey Dobriyan | b8a0ae2 | 2008-11-25 17:20:11 -0800 | [diff] [blame] | 2109 | INIT_HLIST_HEAD(&net->xfrm.state_gc_list); |
Alexey Dobriyan | c783714 | 2008-11-25 17:20:36 -0800 | [diff] [blame] | 2110 | INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 2111 | spin_lock_init(&net->xfrm.xfrm_state_lock); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2112 | return 0; |
Alexey Dobriyan | 73d189d | 2008-11-25 17:16:58 -0800 | [diff] [blame] | 2113 | |
Alexey Dobriyan | b754a4f | 2008-11-25 17:17:47 -0800 | [diff] [blame] | 2114 | out_byspi: |
| 2115 | xfrm_hash_free(net->xfrm.state_bysrc, sz); |
Alexey Dobriyan | d320bbb | 2008-11-25 17:17:24 -0800 | [diff] [blame] | 2116 | out_bysrc: |
| 2117 | xfrm_hash_free(net->xfrm.state_bydst, sz); |
Alexey Dobriyan | 73d189d | 2008-11-25 17:16:58 -0800 | [diff] [blame] | 2118 | out_bydst: |
| 2119 | return -ENOMEM; |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | void xfrm_state_fini(struct net *net) |
| 2123 | { |
Alexey Dobriyan | 73d189d | 2008-11-25 17:16:58 -0800 | [diff] [blame] | 2124 | unsigned int sz; |
| 2125 | |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 2126 | flush_work(&net->xfrm.state_hash_work); |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 2127 | xfrm_state_flush(net, IPSEC_PROTO_ANY, false); |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 2128 | flush_work(&net->xfrm.state_gc_work); |
| 2129 | |
Alexey Dobriyan | 9d4139c | 2008-11-25 17:16:11 -0800 | [diff] [blame] | 2130 | WARN_ON(!list_empty(&net->xfrm.state_all)); |
Alexey Dobriyan | 73d189d | 2008-11-25 17:16:58 -0800 | [diff] [blame] | 2131 | |
Alexey Dobriyan | 529983e | 2008-11-25 17:18:12 -0800 | [diff] [blame] | 2132 | sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | b754a4f | 2008-11-25 17:17:47 -0800 | [diff] [blame] | 2133 | WARN_ON(!hlist_empty(net->xfrm.state_byspi)); |
| 2134 | xfrm_hash_free(net->xfrm.state_byspi, sz); |
Alexey Dobriyan | d320bbb | 2008-11-25 17:17:24 -0800 | [diff] [blame] | 2135 | WARN_ON(!hlist_empty(net->xfrm.state_bysrc)); |
| 2136 | xfrm_hash_free(net->xfrm.state_bysrc, sz); |
Alexey Dobriyan | 73d189d | 2008-11-25 17:16:58 -0800 | [diff] [blame] | 2137 | WARN_ON(!hlist_empty(net->xfrm.state_bydst)); |
| 2138 | xfrm_hash_free(net->xfrm.state_bydst, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2141 | #ifdef CONFIG_AUDITSYSCALL |
Ilpo Järvinen | cf35f43 | 2008-01-05 23:13:20 -0800 | [diff] [blame] | 2142 | static void xfrm_audit_helper_sainfo(struct xfrm_state *x, |
| 2143 | struct audit_buffer *audit_buf) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2144 | { |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 2145 | struct xfrm_sec_ctx *ctx = x->security; |
| 2146 | u32 spi = ntohl(x->id.spi); |
| 2147 | |
| 2148 | if (ctx) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2149 | audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 2150 | ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2151 | |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 2152 | switch (x->props.family) { |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2153 | case AF_INET: |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 2154 | audit_log_format(audit_buf, " src=%pI4 dst=%pI4", |
| 2155 | &x->props.saddr.a4, &x->id.daddr.a4); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2156 | break; |
| 2157 | case AF_INET6: |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 2158 | audit_log_format(audit_buf, " src=%pI6 dst=%pI6", |
Harvey Harrison | fdb46ee | 2008-10-28 16:10:17 -0700 | [diff] [blame] | 2159 | x->props.saddr.a6, x->id.daddr.a6); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2160 | break; |
| 2161 | } |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 2162 | |
| 2163 | audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
Ilpo Järvinen | cf35f43 | 2008-01-05 23:13:20 -0800 | [diff] [blame] | 2166 | static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family, |
| 2167 | struct audit_buffer *audit_buf) |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2168 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2169 | const struct iphdr *iph4; |
| 2170 | const struct ipv6hdr *iph6; |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2171 | |
| 2172 | switch (family) { |
| 2173 | case AF_INET: |
| 2174 | iph4 = ip_hdr(skb); |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 2175 | audit_log_format(audit_buf, " src=%pI4 dst=%pI4", |
| 2176 | &iph4->saddr, &iph4->daddr); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2177 | break; |
| 2178 | case AF_INET6: |
| 2179 | iph6 = ipv6_hdr(skb); |
| 2180 | audit_log_format(audit_buf, |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 2181 | " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x", |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 2182 | &iph6->saddr, &iph6->daddr, |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2183 | iph6->flow_lbl[0] & 0x0f, |
| 2184 | iph6->flow_lbl[1], |
| 2185 | iph6->flow_lbl[2]); |
| 2186 | break; |
| 2187 | } |
| 2188 | } |
| 2189 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 2190 | void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2191 | { |
| 2192 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2193 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2194 | audit_buf = xfrm_audit_start("SAD-add"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2195 | if (audit_buf == NULL) |
| 2196 | return; |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 2197 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2198 | xfrm_audit_helper_sainfo(x, audit_buf); |
| 2199 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2200 | audit_log_end(audit_buf); |
| 2201 | } |
| 2202 | EXPORT_SYMBOL_GPL(xfrm_audit_state_add); |
| 2203 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 2204 | void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2205 | { |
| 2206 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2207 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2208 | audit_buf = xfrm_audit_start("SAD-delete"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2209 | if (audit_buf == NULL) |
| 2210 | return; |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 2211 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2212 | xfrm_audit_helper_sainfo(x, audit_buf); |
| 2213 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2214 | audit_log_end(audit_buf); |
| 2215 | } |
| 2216 | EXPORT_SYMBOL_GPL(xfrm_audit_state_delete); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2217 | |
| 2218 | void xfrm_audit_state_replay_overflow(struct xfrm_state *x, |
| 2219 | struct sk_buff *skb) |
| 2220 | { |
| 2221 | struct audit_buffer *audit_buf; |
| 2222 | u32 spi; |
| 2223 | |
| 2224 | audit_buf = xfrm_audit_start("SA-replay-overflow"); |
| 2225 | if (audit_buf == NULL) |
| 2226 | return; |
| 2227 | xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); |
| 2228 | /* don't record the sequence number because it's inherent in this kind |
| 2229 | * of audit message */ |
| 2230 | spi = ntohl(x->id.spi); |
| 2231 | audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); |
| 2232 | audit_log_end(audit_buf); |
| 2233 | } |
| 2234 | EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow); |
| 2235 | |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 2236 | void xfrm_audit_state_replay(struct xfrm_state *x, |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2237 | struct sk_buff *skb, __be32 net_seq) |
| 2238 | { |
| 2239 | struct audit_buffer *audit_buf; |
| 2240 | u32 spi; |
| 2241 | |
| 2242 | audit_buf = xfrm_audit_start("SA-replayed-pkt"); |
| 2243 | if (audit_buf == NULL) |
| 2244 | return; |
| 2245 | xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); |
| 2246 | spi = ntohl(x->id.spi); |
| 2247 | audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", |
| 2248 | spi, spi, ntohl(net_seq)); |
| 2249 | audit_log_end(audit_buf); |
| 2250 | } |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 2251 | EXPORT_SYMBOL_GPL(xfrm_audit_state_replay); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 2252 | |
| 2253 | void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family) |
| 2254 | { |
| 2255 | struct audit_buffer *audit_buf; |
| 2256 | |
| 2257 | audit_buf = xfrm_audit_start("SA-notfound"); |
| 2258 | if (audit_buf == NULL) |
| 2259 | return; |
| 2260 | xfrm_audit_helper_pktinfo(skb, family, audit_buf); |
| 2261 | audit_log_end(audit_buf); |
| 2262 | } |
| 2263 | EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple); |
| 2264 | |
| 2265 | void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, |
| 2266 | __be32 net_spi, __be32 net_seq) |
| 2267 | { |
| 2268 | struct audit_buffer *audit_buf; |
| 2269 | u32 spi; |
| 2270 | |
| 2271 | audit_buf = xfrm_audit_start("SA-notfound"); |
| 2272 | if (audit_buf == NULL) |
| 2273 | return; |
| 2274 | xfrm_audit_helper_pktinfo(skb, family, audit_buf); |
| 2275 | spi = ntohl(net_spi); |
| 2276 | audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", |
| 2277 | spi, spi, ntohl(net_seq)); |
| 2278 | audit_log_end(audit_buf); |
| 2279 | } |
| 2280 | EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound); |
| 2281 | |
| 2282 | void xfrm_audit_state_icvfail(struct xfrm_state *x, |
| 2283 | struct sk_buff *skb, u8 proto) |
| 2284 | { |
| 2285 | struct audit_buffer *audit_buf; |
| 2286 | __be32 net_spi; |
| 2287 | __be32 net_seq; |
| 2288 | |
| 2289 | audit_buf = xfrm_audit_start("SA-icv-failure"); |
| 2290 | if (audit_buf == NULL) |
| 2291 | return; |
| 2292 | xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); |
| 2293 | if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) { |
| 2294 | u32 spi = ntohl(net_spi); |
| 2295 | audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", |
| 2296 | spi, spi, ntohl(net_seq)); |
| 2297 | } |
| 2298 | audit_log_end(audit_buf); |
| 2299 | } |
| 2300 | EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2301 | #endif /* CONFIG_AUDITSYSCALL */ |