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