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