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