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