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