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