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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
| 23 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 24 | #include "xfrm_hash.h" |
| 25 | |
David S. Miller | ee857a7 | 2006-03-20 19:18:37 -0800 | [diff] [blame] | 26 | struct sock *xfrm_nl; |
| 27 | EXPORT_SYMBOL(xfrm_nl); |
| 28 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 29 | u32 sysctl_xfrm_aevent_etime = XFRM_AE_ETIME; |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 30 | EXPORT_SYMBOL(sysctl_xfrm_aevent_etime); |
| 31 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 32 | u32 sysctl_xfrm_aevent_rseqth = XFRM_AE_SEQT_SIZE; |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 33 | EXPORT_SYMBOL(sysctl_xfrm_aevent_rseqth); |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | /* Each xfrm_state may be linked to two tables: |
| 36 | |
| 37 | 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl) |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 38 | 2. Hash table by (daddr,family,reqid) to find what SAs exist for given |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | destination/tunnel endpoint. (output) |
| 40 | */ |
| 41 | |
| 42 | static DEFINE_SPINLOCK(xfrm_state_lock); |
| 43 | |
| 44 | /* Hash table to find appropriate SA towards given target (endpoint |
| 45 | * of tunnel or destination of transport mode) allowed by selector. |
| 46 | * |
| 47 | * Main use is finding SA after policy selected tunnel or transport mode. |
| 48 | * Also, it can be used by ah/esp icmp error handler to find offending SA. |
| 49 | */ |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 50 | static struct hlist_head *xfrm_state_bydst __read_mostly; |
| 51 | static struct hlist_head *xfrm_state_bysrc __read_mostly; |
| 52 | static struct hlist_head *xfrm_state_byspi __read_mostly; |
| 53 | static unsigned int xfrm_state_hmask __read_mostly; |
| 54 | static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024; |
| 55 | static unsigned int xfrm_state_num; |
David S. Miller | 9d4a706 | 2006-08-24 03:18:09 -0700 | [diff] [blame] | 56 | static unsigned int xfrm_state_genid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 58 | static inline unsigned int xfrm_dst_hash(xfrm_address_t *daddr, |
| 59 | xfrm_address_t *saddr, |
| 60 | u32 reqid, |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 61 | unsigned short family) |
| 62 | { |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 63 | return __xfrm_dst_hash(daddr, saddr, reqid, family, xfrm_state_hmask); |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 64 | } |
| 65 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 66 | static inline unsigned int xfrm_src_hash(xfrm_address_t *addr, |
| 67 | unsigned short family) |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 68 | { |
| 69 | return __xfrm_src_hash(addr, family, xfrm_state_hmask); |
| 70 | } |
| 71 | |
David S. Miller | 2575b65 | 2006-08-24 03:26:44 -0700 | [diff] [blame] | 72 | static inline unsigned int |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 73 | xfrm_spi_hash(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family) |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 74 | { |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 75 | return __xfrm_spi_hash(daddr, spi, proto, family, xfrm_state_hmask); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 76 | } |
| 77 | |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 78 | static void xfrm_hash_transfer(struct hlist_head *list, |
| 79 | struct hlist_head *ndsttable, |
| 80 | struct hlist_head *nsrctable, |
| 81 | struct hlist_head *nspitable, |
| 82 | unsigned int nhashmask) |
| 83 | { |
| 84 | struct hlist_node *entry, *tmp; |
| 85 | struct xfrm_state *x; |
| 86 | |
| 87 | hlist_for_each_entry_safe(x, entry, tmp, list, bydst) { |
| 88 | unsigned int h; |
| 89 | |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 90 | h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr, |
| 91 | x->props.reqid, x->props.family, |
| 92 | nhashmask); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 93 | hlist_add_head(&x->bydst, ndsttable+h); |
| 94 | |
| 95 | h = __xfrm_src_hash(&x->props.saddr, x->props.family, |
| 96 | nhashmask); |
| 97 | hlist_add_head(&x->bysrc, nsrctable+h); |
| 98 | |
| 99 | h = __xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, |
| 100 | x->props.family, nhashmask); |
| 101 | hlist_add_head(&x->byspi, nspitable+h); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static unsigned long xfrm_hash_new_size(void) |
| 106 | { |
| 107 | return ((xfrm_state_hmask + 1) << 1) * |
| 108 | sizeof(struct hlist_head); |
| 109 | } |
| 110 | |
| 111 | static DEFINE_MUTEX(hash_resize_mutex); |
| 112 | |
| 113 | static void xfrm_hash_resize(void *__unused) |
| 114 | { |
| 115 | struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi; |
| 116 | unsigned long nsize, osize; |
| 117 | unsigned int nhashmask, ohashmask; |
| 118 | int i; |
| 119 | |
| 120 | mutex_lock(&hash_resize_mutex); |
| 121 | |
| 122 | nsize = xfrm_hash_new_size(); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 123 | ndst = xfrm_hash_alloc(nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 124 | if (!ndst) |
| 125 | goto out_unlock; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 126 | nsrc = xfrm_hash_alloc(nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 127 | if (!nsrc) { |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 128 | xfrm_hash_free(ndst, nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 129 | goto out_unlock; |
| 130 | } |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 131 | nspi = xfrm_hash_alloc(nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 132 | if (!nspi) { |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 133 | xfrm_hash_free(ndst, nsize); |
| 134 | xfrm_hash_free(nsrc, nsize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 135 | goto out_unlock; |
| 136 | } |
| 137 | |
| 138 | spin_lock_bh(&xfrm_state_lock); |
| 139 | |
| 140 | nhashmask = (nsize / sizeof(struct hlist_head)) - 1U; |
| 141 | for (i = xfrm_state_hmask; i >= 0; i--) |
| 142 | xfrm_hash_transfer(xfrm_state_bydst+i, ndst, nsrc, nspi, |
| 143 | nhashmask); |
| 144 | |
| 145 | odst = xfrm_state_bydst; |
| 146 | osrc = xfrm_state_bysrc; |
| 147 | ospi = xfrm_state_byspi; |
| 148 | ohashmask = xfrm_state_hmask; |
| 149 | |
| 150 | xfrm_state_bydst = ndst; |
| 151 | xfrm_state_bysrc = nsrc; |
| 152 | xfrm_state_byspi = nspi; |
| 153 | xfrm_state_hmask = nhashmask; |
| 154 | |
| 155 | spin_unlock_bh(&xfrm_state_lock); |
| 156 | |
| 157 | osize = (ohashmask + 1) * sizeof(struct hlist_head); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 158 | xfrm_hash_free(odst, osize); |
| 159 | xfrm_hash_free(osrc, osize); |
| 160 | xfrm_hash_free(ospi, osize); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 161 | |
| 162 | out_unlock: |
| 163 | mutex_unlock(&hash_resize_mutex); |
| 164 | } |
| 165 | |
| 166 | static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL); |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | DECLARE_WAIT_QUEUE_HEAD(km_waitq); |
| 169 | EXPORT_SYMBOL(km_waitq); |
| 170 | |
| 171 | static DEFINE_RWLOCK(xfrm_state_afinfo_lock); |
| 172 | static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO]; |
| 173 | |
| 174 | static struct work_struct xfrm_state_gc_work; |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 175 | static HLIST_HEAD(xfrm_state_gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | static DEFINE_SPINLOCK(xfrm_state_gc_lock); |
| 177 | |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 178 | int __xfrm_state_delete(struct xfrm_state *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family); |
| 181 | static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo); |
| 182 | |
Jamal Hadi Salim | 980ebd2 | 2006-03-20 19:16:40 -0800 | [diff] [blame] | 183 | 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] | 184 | void km_state_expired(struct xfrm_state *x, int hard, u32 pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | static void xfrm_state_gc_destroy(struct xfrm_state *x) |
| 187 | { |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 188 | del_timer_sync(&x->timer); |
| 189 | del_timer_sync(&x->rtimer); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 190 | kfree(x->aalg); |
| 191 | kfree(x->ealg); |
| 192 | kfree(x->calg); |
| 193 | kfree(x->encap); |
Noriaki TAKAMIYA | 060f02a | 2006-08-23 18:18:55 -0700 | [diff] [blame] | 194 | kfree(x->coaddr); |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 195 | if (x->mode) |
| 196 | xfrm_put_mode(x->mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (x->type) { |
| 198 | x->type->destructor(x); |
| 199 | xfrm_put_type(x->type); |
| 200 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 201 | security_xfrm_state_free(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | kfree(x); |
| 203 | } |
| 204 | |
| 205 | static void xfrm_state_gc_task(void *data) |
| 206 | { |
| 207 | struct xfrm_state *x; |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 208 | struct hlist_node *entry, *tmp; |
| 209 | struct hlist_head gc_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | spin_lock_bh(&xfrm_state_gc_lock); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 212 | gc_list.first = xfrm_state_gc_list.first; |
| 213 | INIT_HLIST_HEAD(&xfrm_state_gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | spin_unlock_bh(&xfrm_state_gc_lock); |
| 215 | |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 216 | hlist_for_each_entry_safe(x, entry, tmp, &gc_list, bydst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | xfrm_state_gc_destroy(x); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | wake_up(&km_waitq); |
| 220 | } |
| 221 | |
| 222 | static inline unsigned long make_jiffies(long secs) |
| 223 | { |
| 224 | if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) |
| 225 | return MAX_SCHEDULE_TIMEOUT-1; |
| 226 | else |
| 227 | return secs*HZ; |
| 228 | } |
| 229 | |
| 230 | static void xfrm_timer_handler(unsigned long data) |
| 231 | { |
| 232 | struct xfrm_state *x = (struct xfrm_state*)data; |
| 233 | unsigned long now = (unsigned long)xtime.tv_sec; |
| 234 | long next = LONG_MAX; |
| 235 | int warn = 0; |
| 236 | |
| 237 | spin_lock(&x->lock); |
| 238 | if (x->km.state == XFRM_STATE_DEAD) |
| 239 | goto out; |
| 240 | if (x->km.state == XFRM_STATE_EXPIRED) |
| 241 | goto expired; |
| 242 | if (x->lft.hard_add_expires_seconds) { |
| 243 | long tmo = x->lft.hard_add_expires_seconds + |
| 244 | x->curlft.add_time - now; |
| 245 | if (tmo <= 0) |
| 246 | goto expired; |
| 247 | if (tmo < next) |
| 248 | next = tmo; |
| 249 | } |
| 250 | if (x->lft.hard_use_expires_seconds) { |
| 251 | long tmo = x->lft.hard_use_expires_seconds + |
| 252 | (x->curlft.use_time ? : now) - now; |
| 253 | if (tmo <= 0) |
| 254 | goto expired; |
| 255 | if (tmo < next) |
| 256 | next = tmo; |
| 257 | } |
| 258 | if (x->km.dying) |
| 259 | goto resched; |
| 260 | if (x->lft.soft_add_expires_seconds) { |
| 261 | long tmo = x->lft.soft_add_expires_seconds + |
| 262 | x->curlft.add_time - now; |
| 263 | if (tmo <= 0) |
| 264 | warn = 1; |
| 265 | else if (tmo < next) |
| 266 | next = tmo; |
| 267 | } |
| 268 | if (x->lft.soft_use_expires_seconds) { |
| 269 | long tmo = x->lft.soft_use_expires_seconds + |
| 270 | (x->curlft.use_time ? : now) - now; |
| 271 | if (tmo <= 0) |
| 272 | warn = 1; |
| 273 | else if (tmo < next) |
| 274 | next = tmo; |
| 275 | } |
| 276 | |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 277 | x->km.dying = warn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (warn) |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 279 | km_state_expired(x, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | resched: |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 281 | if (next != LONG_MAX) |
| 282 | mod_timer(&x->timer, jiffies + make_jiffies(next)); |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | goto out; |
| 285 | |
| 286 | expired: |
| 287 | if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) { |
| 288 | x->km.state = XFRM_STATE_EXPIRED; |
| 289 | wake_up(&km_waitq); |
| 290 | next = 2; |
| 291 | goto resched; |
| 292 | } |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 293 | if (!__xfrm_state_delete(x) && x->id.spi) |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 294 | km_state_expired(x, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | out: |
| 297 | spin_unlock(&x->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
David S. Miller | 0ac8475 | 2006-03-20 19:18:23 -0800 | [diff] [blame] | 300 | static void xfrm_replay_timer_handler(unsigned long data); |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | struct xfrm_state *xfrm_state_alloc(void) |
| 303 | { |
| 304 | struct xfrm_state *x; |
| 305 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 306 | x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | if (x) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | atomic_set(&x->refcnt, 1); |
| 310 | atomic_set(&x->tunnel_users, 0); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 311 | INIT_HLIST_NODE(&x->bydst); |
| 312 | INIT_HLIST_NODE(&x->bysrc); |
| 313 | INIT_HLIST_NODE(&x->byspi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | init_timer(&x->timer); |
| 315 | x->timer.function = xfrm_timer_handler; |
| 316 | x->timer.data = (unsigned long)x; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 317 | init_timer(&x->rtimer); |
| 318 | x->rtimer.function = xfrm_replay_timer_handler; |
| 319 | x->rtimer.data = (unsigned long)x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | x->curlft.add_time = (unsigned long)xtime.tv_sec; |
| 321 | x->lft.soft_byte_limit = XFRM_INF; |
| 322 | x->lft.soft_packet_limit = XFRM_INF; |
| 323 | x->lft.hard_byte_limit = XFRM_INF; |
| 324 | x->lft.hard_packet_limit = XFRM_INF; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 325 | x->replay_maxage = 0; |
| 326 | x->replay_maxdiff = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | spin_lock_init(&x->lock); |
| 328 | } |
| 329 | return x; |
| 330 | } |
| 331 | EXPORT_SYMBOL(xfrm_state_alloc); |
| 332 | |
| 333 | void __xfrm_state_destroy(struct xfrm_state *x) |
| 334 | { |
| 335 | BUG_TRAP(x->km.state == XFRM_STATE_DEAD); |
| 336 | |
| 337 | spin_lock_bh(&xfrm_state_gc_lock); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 338 | hlist_add_head(&x->bydst, &xfrm_state_gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | spin_unlock_bh(&xfrm_state_gc_lock); |
| 340 | schedule_work(&xfrm_state_gc_work); |
| 341 | } |
| 342 | EXPORT_SYMBOL(__xfrm_state_destroy); |
| 343 | |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 344 | int __xfrm_state_delete(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 346 | int err = -ESRCH; |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if (x->km.state != XFRM_STATE_DEAD) { |
| 349 | x->km.state = XFRM_STATE_DEAD; |
| 350 | spin_lock(&xfrm_state_lock); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 351 | hlist_del(&x->bydst); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 352 | hlist_del(&x->bysrc); |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 353 | if (x->id.spi) |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 354 | hlist_del(&x->byspi); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 355 | xfrm_state_num--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | spin_unlock(&xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /* All xfrm_state objects are created by xfrm_state_alloc. |
| 359 | * The xfrm_state_alloc call gives a reference, and that |
| 360 | * is what we are dropping here. |
| 361 | */ |
Herbert Xu | 21380b8 | 2006-02-22 14:47:13 -0800 | [diff] [blame] | 362 | __xfrm_state_put(x); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 363 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 365 | |
| 366 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 368 | EXPORT_SYMBOL(__xfrm_state_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 370 | int xfrm_state_delete(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 372 | int err; |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | spin_lock_bh(&x->lock); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 375 | err = __xfrm_state_delete(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | spin_unlock_bh(&x->lock); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 377 | |
| 378 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | EXPORT_SYMBOL(xfrm_state_delete); |
| 381 | |
| 382 | void xfrm_state_flush(u8 proto) |
| 383 | { |
| 384 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | spin_lock_bh(&xfrm_state_lock); |
Masahide NAKAMURA | a9917c0 | 2006-08-31 15:14:32 -0700 | [diff] [blame] | 387 | for (i = 0; i <= xfrm_state_hmask; i++) { |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 388 | struct hlist_node *entry; |
| 389 | struct xfrm_state *x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | restart: |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 391 | hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | if (!xfrm_state_kern(x) && |
Masahide NAKAMURA | 5794708 | 2006-09-22 15:06:24 -0700 | [diff] [blame] | 393 | xfrm_id_proto_match(x->id.proto, proto)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | xfrm_state_hold(x); |
| 395 | spin_unlock_bh(&xfrm_state_lock); |
| 396 | |
| 397 | xfrm_state_delete(x); |
| 398 | xfrm_state_put(x); |
| 399 | |
| 400 | spin_lock_bh(&xfrm_state_lock); |
| 401 | goto restart; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | spin_unlock_bh(&xfrm_state_lock); |
| 406 | wake_up(&km_waitq); |
| 407 | } |
| 408 | EXPORT_SYMBOL(xfrm_state_flush); |
| 409 | |
| 410 | static int |
| 411 | xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl, |
| 412 | struct xfrm_tmpl *tmpl, |
| 413 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 414 | unsigned short family) |
| 415 | { |
| 416 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
| 417 | if (!afinfo) |
| 418 | return -1; |
| 419 | afinfo->init_tempsel(x, fl, tmpl, daddr, saddr); |
| 420 | xfrm_state_put_afinfo(afinfo); |
| 421 | return 0; |
| 422 | } |
| 423 | |
Al Viro | a94cfd1 | 2006-09-27 18:47:24 -0700 | [diff] [blame] | 424 | static struct xfrm_state *__xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family) |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 425 | { |
| 426 | unsigned int h = xfrm_spi_hash(daddr, spi, proto, family); |
| 427 | struct xfrm_state *x; |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 428 | struct hlist_node *entry; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 429 | |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 430 | hlist_for_each_entry(x, entry, xfrm_state_byspi+h, byspi) { |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 431 | if (x->props.family != family || |
| 432 | x->id.spi != spi || |
| 433 | x->id.proto != proto) |
| 434 | continue; |
| 435 | |
| 436 | switch (family) { |
| 437 | case AF_INET: |
| 438 | if (x->id.daddr.a4 != daddr->a4) |
| 439 | continue; |
| 440 | break; |
| 441 | case AF_INET6: |
| 442 | if (!ipv6_addr_equal((struct in6_addr *)daddr, |
| 443 | (struct in6_addr *) |
| 444 | x->id.daddr.a6)) |
| 445 | continue; |
| 446 | break; |
| 447 | }; |
| 448 | |
| 449 | xfrm_state_hold(x); |
| 450 | return x; |
| 451 | } |
| 452 | |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | static struct xfrm_state *__xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family) |
| 457 | { |
| 458 | unsigned int h = xfrm_src_hash(saddr, family); |
| 459 | struct xfrm_state *x; |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 460 | struct hlist_node *entry; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 461 | |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 462 | hlist_for_each_entry(x, entry, xfrm_state_bysrc+h, bysrc) { |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 463 | if (x->props.family != family || |
| 464 | x->id.proto != proto) |
| 465 | continue; |
| 466 | |
| 467 | switch (family) { |
| 468 | case AF_INET: |
| 469 | if (x->id.daddr.a4 != daddr->a4 || |
| 470 | x->props.saddr.a4 != saddr->a4) |
| 471 | continue; |
| 472 | break; |
| 473 | case AF_INET6: |
| 474 | if (!ipv6_addr_equal((struct in6_addr *)daddr, |
| 475 | (struct in6_addr *) |
| 476 | x->id.daddr.a6) || |
| 477 | !ipv6_addr_equal((struct in6_addr *)saddr, |
| 478 | (struct in6_addr *) |
| 479 | x->props.saddr.a6)) |
| 480 | continue; |
| 481 | break; |
| 482 | }; |
| 483 | |
| 484 | xfrm_state_hold(x); |
| 485 | return x; |
| 486 | } |
| 487 | |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | static inline struct xfrm_state * |
| 492 | __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family) |
| 493 | { |
| 494 | if (use_spi) |
| 495 | return __xfrm_state_lookup(&x->id.daddr, x->id.spi, |
| 496 | x->id.proto, family); |
| 497 | else |
| 498 | return __xfrm_state_lookup_byaddr(&x->id.daddr, |
| 499 | &x->props.saddr, |
| 500 | x->id.proto, family); |
| 501 | } |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | struct xfrm_state * |
| 504 | xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 505 | struct flowi *fl, struct xfrm_tmpl *tmpl, |
| 506 | struct xfrm_policy *pol, int *err, |
| 507 | unsigned short family) |
| 508 | { |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 509 | unsigned int h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 510 | struct hlist_node *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | struct xfrm_state *x, *x0; |
| 512 | int acquire_in_progress = 0; |
| 513 | int error = 0; |
| 514 | struct xfrm_state *best = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | spin_lock_bh(&xfrm_state_lock); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 517 | hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | if (x->props.family == family && |
| 519 | x->props.reqid == tmpl->reqid && |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 520 | !(x->props.flags & XFRM_STATE_WILDRECV) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | xfrm_state_addr_check(x, daddr, saddr, family) && |
| 522 | tmpl->mode == x->props.mode && |
| 523 | tmpl->id.proto == x->id.proto && |
| 524 | (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) { |
| 525 | /* Resolution logic: |
| 526 | 1. There is a valid state with matching selector. |
| 527 | Done. |
| 528 | 2. Valid state with inappropriate selector. Skip. |
| 529 | |
| 530 | Entering area of "sysdeps". |
| 531 | |
| 532 | 3. If state is not valid, selector is temporary, |
| 533 | it selects only session which triggered |
| 534 | previous resolution. Key manager will do |
| 535 | something to install a state with proper |
| 536 | selector. |
| 537 | */ |
| 538 | if (x->km.state == XFRM_STATE_VALID) { |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 539 | if (!xfrm_selector_match(&x->sel, fl, family) || |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 540 | !security_xfrm_state_pol_flow_match(x, pol, fl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | continue; |
| 542 | if (!best || |
| 543 | best->km.dying > x->km.dying || |
| 544 | (best->km.dying == x->km.dying && |
| 545 | best->curlft.add_time < x->curlft.add_time)) |
| 546 | best = x; |
| 547 | } else if (x->km.state == XFRM_STATE_ACQ) { |
| 548 | acquire_in_progress = 1; |
| 549 | } else if (x->km.state == XFRM_STATE_ERROR || |
| 550 | x->km.state == XFRM_STATE_EXPIRED) { |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 551 | if (xfrm_selector_match(&x->sel, fl, family) && |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 552 | security_xfrm_state_pol_flow_match(x, pol, fl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | error = -ESRCH; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | x = best; |
| 559 | if (!x && !error && !acquire_in_progress) { |
Patrick McHardy | 5c5d281 | 2005-04-21 20:12:32 -0700 | [diff] [blame] | 560 | if (tmpl->id.spi && |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 561 | (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi, |
| 562 | tmpl->id.proto, family)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | xfrm_state_put(x0); |
| 564 | error = -EEXIST; |
| 565 | goto out; |
| 566 | } |
| 567 | x = xfrm_state_alloc(); |
| 568 | if (x == NULL) { |
| 569 | error = -ENOMEM; |
| 570 | goto out; |
| 571 | } |
| 572 | /* Initialize temporary selector matching only |
| 573 | * to current session. */ |
| 574 | xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family); |
| 575 | |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 576 | error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid); |
| 577 | if (error) { |
| 578 | x->km.state = XFRM_STATE_DEAD; |
| 579 | xfrm_state_put(x); |
| 580 | x = NULL; |
| 581 | goto out; |
| 582 | } |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | if (km_query(x, tmpl, pol) == 0) { |
| 585 | x->km.state = XFRM_STATE_ACQ; |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 586 | hlist_add_head(&x->bydst, xfrm_state_bydst+h); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 587 | h = xfrm_src_hash(saddr, family); |
| 588 | hlist_add_head(&x->bysrc, xfrm_state_bysrc+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | if (x->id.spi) { |
| 590 | h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, family); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 591 | hlist_add_head(&x->byspi, xfrm_state_byspi+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | x->lft.hard_add_expires_seconds = XFRM_ACQ_EXPIRES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | x->timer.expires = jiffies + XFRM_ACQ_EXPIRES*HZ; |
| 595 | add_timer(&x->timer); |
| 596 | } else { |
| 597 | x->km.state = XFRM_STATE_DEAD; |
| 598 | xfrm_state_put(x); |
| 599 | x = NULL; |
| 600 | error = -ESRCH; |
| 601 | } |
| 602 | } |
| 603 | out: |
| 604 | if (x) |
| 605 | xfrm_state_hold(x); |
| 606 | else |
| 607 | *err = acquire_in_progress ? -EAGAIN : error; |
| 608 | spin_unlock_bh(&xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | return x; |
| 610 | } |
| 611 | |
| 612 | static void __xfrm_state_insert(struct xfrm_state *x) |
| 613 | { |
David S. Miller | a624c10 | 2006-08-24 03:24:33 -0700 | [diff] [blame] | 614 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
David S. Miller | 9d4a706 | 2006-08-24 03:18:09 -0700 | [diff] [blame] | 616 | x->genid = ++xfrm_state_genid; |
| 617 | |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 618 | h = xfrm_dst_hash(&x->id.daddr, &x->props.saddr, |
| 619 | x->props.reqid, x->props.family); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 620 | hlist_add_head(&x->bydst, xfrm_state_bydst+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Masahide NAKAMURA | 6c44e6b | 2006-08-23 17:53:57 -0700 | [diff] [blame] | 622 | h = xfrm_src_hash(&x->props.saddr, x->props.family); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 623 | hlist_add_head(&x->bysrc, xfrm_state_bysrc+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Masahide NAKAMURA | 6c44e6b | 2006-08-23 17:53:57 -0700 | [diff] [blame] | 625 | if (xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY)) { |
| 626 | h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, |
| 627 | x->props.family); |
| 628 | |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 629 | hlist_add_head(&x->byspi, xfrm_state_byspi+h); |
Masahide NAKAMURA | 6c44e6b | 2006-08-23 17:53:57 -0700 | [diff] [blame] | 630 | } |
| 631 | |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 632 | mod_timer(&x->timer, jiffies + HZ); |
| 633 | if (x->replay_maxage) |
| 634 | mod_timer(&x->rtimer, jiffies + x->replay_maxage); |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | wake_up(&km_waitq); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 637 | |
| 638 | xfrm_state_num++; |
| 639 | |
| 640 | if (x->bydst.next != NULL && |
| 641 | (xfrm_state_hmask + 1) < xfrm_state_hashmax && |
| 642 | xfrm_state_num > xfrm_state_hmask) |
| 643 | schedule_work(&xfrm_hash_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 646 | /* xfrm_state_lock is held */ |
| 647 | static void __xfrm_state_bump_genids(struct xfrm_state *xnew) |
| 648 | { |
| 649 | unsigned short family = xnew->props.family; |
| 650 | u32 reqid = xnew->props.reqid; |
| 651 | struct xfrm_state *x; |
| 652 | struct hlist_node *entry; |
| 653 | unsigned int h; |
| 654 | |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 655 | h = xfrm_dst_hash(&xnew->id.daddr, &xnew->props.saddr, reqid, family); |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 656 | hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { |
| 657 | if (x->props.family == family && |
| 658 | x->props.reqid == reqid && |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 659 | !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) && |
| 660 | !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family)) |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 661 | x->genid = xfrm_state_genid; |
| 662 | } |
| 663 | } |
| 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | void xfrm_state_insert(struct xfrm_state *x) |
| 666 | { |
| 667 | spin_lock_bh(&xfrm_state_lock); |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 668 | __xfrm_state_bump_genids(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | __xfrm_state_insert(x); |
| 670 | spin_unlock_bh(&xfrm_state_lock); |
| 671 | } |
| 672 | EXPORT_SYMBOL(xfrm_state_insert); |
| 673 | |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 674 | /* xfrm_state_lock is held */ |
| 675 | static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create) |
| 676 | { |
David S. Miller | c1969f2 | 2006-08-24 04:00:03 -0700 | [diff] [blame] | 677 | unsigned int h = xfrm_dst_hash(daddr, saddr, reqid, family); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 678 | struct hlist_node *entry; |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 679 | struct xfrm_state *x; |
| 680 | |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 681 | hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 682 | if (x->props.reqid != reqid || |
| 683 | x->props.mode != mode || |
| 684 | x->props.family != family || |
| 685 | x->km.state != XFRM_STATE_ACQ || |
| 686 | x->id.spi != 0) |
| 687 | continue; |
| 688 | |
| 689 | switch (family) { |
| 690 | case AF_INET: |
| 691 | if (x->id.daddr.a4 != daddr->a4 || |
| 692 | x->props.saddr.a4 != saddr->a4) |
| 693 | continue; |
| 694 | break; |
| 695 | case AF_INET6: |
| 696 | if (!ipv6_addr_equal((struct in6_addr *)x->id.daddr.a6, |
| 697 | (struct in6_addr *)daddr) || |
| 698 | !ipv6_addr_equal((struct in6_addr *) |
| 699 | x->props.saddr.a6, |
| 700 | (struct in6_addr *)saddr)) |
| 701 | continue; |
| 702 | break; |
| 703 | }; |
| 704 | |
| 705 | xfrm_state_hold(x); |
| 706 | return x; |
| 707 | } |
| 708 | |
| 709 | if (!create) |
| 710 | return NULL; |
| 711 | |
| 712 | x = xfrm_state_alloc(); |
| 713 | if (likely(x)) { |
| 714 | switch (family) { |
| 715 | case AF_INET: |
| 716 | x->sel.daddr.a4 = daddr->a4; |
| 717 | x->sel.saddr.a4 = saddr->a4; |
| 718 | x->sel.prefixlen_d = 32; |
| 719 | x->sel.prefixlen_s = 32; |
| 720 | x->props.saddr.a4 = saddr->a4; |
| 721 | x->id.daddr.a4 = daddr->a4; |
| 722 | break; |
| 723 | |
| 724 | case AF_INET6: |
| 725 | ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6, |
| 726 | (struct in6_addr *)daddr); |
| 727 | ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6, |
| 728 | (struct in6_addr *)saddr); |
| 729 | x->sel.prefixlen_d = 128; |
| 730 | x->sel.prefixlen_s = 128; |
| 731 | ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6, |
| 732 | (struct in6_addr *)saddr); |
| 733 | ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6, |
| 734 | (struct in6_addr *)daddr); |
| 735 | break; |
| 736 | }; |
| 737 | |
| 738 | x->km.state = XFRM_STATE_ACQ; |
| 739 | x->id.proto = proto; |
| 740 | x->props.family = family; |
| 741 | x->props.mode = mode; |
| 742 | x->props.reqid = reqid; |
| 743 | x->lft.hard_add_expires_seconds = XFRM_ACQ_EXPIRES; |
| 744 | xfrm_state_hold(x); |
| 745 | x->timer.expires = jiffies + XFRM_ACQ_EXPIRES*HZ; |
| 746 | add_timer(&x->timer); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 747 | hlist_add_head(&x->bydst, xfrm_state_bydst+h); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 748 | h = xfrm_src_hash(saddr, family); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 749 | hlist_add_head(&x->bysrc, xfrm_state_bysrc+h); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 750 | wake_up(&km_waitq); |
| 751 | } |
| 752 | |
| 753 | return x; |
| 754 | } |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq); |
| 757 | |
| 758 | int xfrm_state_add(struct xfrm_state *x) |
| 759 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | struct xfrm_state *x1; |
| 761 | int family; |
| 762 | int err; |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 763 | 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] | 764 | |
| 765 | family = x->props.family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | spin_lock_bh(&xfrm_state_lock); |
| 768 | |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 769 | x1 = __xfrm_state_locate(x, use_spi, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (x1) { |
| 771 | xfrm_state_put(x1); |
| 772 | x1 = NULL; |
| 773 | err = -EEXIST; |
| 774 | goto out; |
| 775 | } |
| 776 | |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 777 | if (use_spi && x->km.seq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | x1 = __xfrm_find_acq_byseq(x->km.seq); |
| 779 | if (x1 && xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family)) { |
| 780 | xfrm_state_put(x1); |
| 781 | x1 = NULL; |
| 782 | } |
| 783 | } |
| 784 | |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 785 | if (use_spi && !x1) |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 786 | x1 = __find_acq_core(family, x->props.mode, x->props.reqid, |
| 787 | x->id.proto, |
| 788 | &x->id.daddr, &x->props.saddr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
David S. Miller | c7f5ea3 | 2006-08-24 03:29:04 -0700 | [diff] [blame] | 790 | __xfrm_state_bump_genids(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | __xfrm_state_insert(x); |
| 792 | err = 0; |
| 793 | |
| 794 | out: |
| 795 | spin_unlock_bh(&xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
| 797 | if (x1) { |
| 798 | xfrm_state_delete(x1); |
| 799 | xfrm_state_put(x1); |
| 800 | } |
| 801 | |
| 802 | return err; |
| 803 | } |
| 804 | EXPORT_SYMBOL(xfrm_state_add); |
| 805 | |
| 806 | int xfrm_state_update(struct xfrm_state *x) |
| 807 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | struct xfrm_state *x1; |
| 809 | int err; |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 810 | 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] | 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | spin_lock_bh(&xfrm_state_lock); |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 813 | x1 = __xfrm_state_locate(x, use_spi, x->props.family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
| 815 | err = -ESRCH; |
| 816 | if (!x1) |
| 817 | goto out; |
| 818 | |
| 819 | if (xfrm_state_kern(x1)) { |
| 820 | xfrm_state_put(x1); |
| 821 | err = -EEXIST; |
| 822 | goto out; |
| 823 | } |
| 824 | |
| 825 | if (x1->km.state == XFRM_STATE_ACQ) { |
| 826 | __xfrm_state_insert(x); |
| 827 | x = NULL; |
| 828 | } |
| 829 | err = 0; |
| 830 | |
| 831 | out: |
| 832 | spin_unlock_bh(&xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
| 834 | if (err) |
| 835 | return err; |
| 836 | |
| 837 | if (!x) { |
| 838 | xfrm_state_delete(x1); |
| 839 | xfrm_state_put(x1); |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | err = -EINVAL; |
| 844 | spin_lock_bh(&x1->lock); |
| 845 | if (likely(x1->km.state == XFRM_STATE_VALID)) { |
| 846 | if (x->encap && x1->encap) |
| 847 | memcpy(x1->encap, x->encap, sizeof(*x1->encap)); |
Noriaki TAKAMIYA | 060f02a | 2006-08-23 18:18:55 -0700 | [diff] [blame] | 848 | if (x->coaddr && x1->coaddr) { |
| 849 | memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr)); |
| 850 | } |
| 851 | if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel))) |
| 852 | memcpy(&x1->sel, &x->sel, sizeof(x1->sel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | memcpy(&x1->lft, &x->lft, sizeof(x1->lft)); |
| 854 | x1->km.dying = 0; |
| 855 | |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 856 | mod_timer(&x1->timer, jiffies + HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | if (x1->curlft.use_time) |
| 858 | xfrm_state_check_expire(x1); |
| 859 | |
| 860 | err = 0; |
| 861 | } |
| 862 | spin_unlock_bh(&x1->lock); |
| 863 | |
| 864 | xfrm_state_put(x1); |
| 865 | |
| 866 | return err; |
| 867 | } |
| 868 | EXPORT_SYMBOL(xfrm_state_update); |
| 869 | |
| 870 | int xfrm_state_check_expire(struct xfrm_state *x) |
| 871 | { |
| 872 | if (!x->curlft.use_time) |
| 873 | x->curlft.use_time = (unsigned long)xtime.tv_sec; |
| 874 | |
| 875 | if (x->km.state != XFRM_STATE_VALID) |
| 876 | return -EINVAL; |
| 877 | |
| 878 | if (x->curlft.bytes >= x->lft.hard_byte_limit || |
| 879 | x->curlft.packets >= x->lft.hard_packet_limit) { |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 880 | x->km.state = XFRM_STATE_EXPIRED; |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 881 | mod_timer(&x->timer, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | return -EINVAL; |
| 883 | } |
| 884 | |
| 885 | if (!x->km.dying && |
| 886 | (x->curlft.bytes >= x->lft.soft_byte_limit || |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 887 | x->curlft.packets >= x->lft.soft_packet_limit)) { |
| 888 | x->km.dying = 1; |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 889 | km_state_expired(x, 0, 0); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return 0; |
| 892 | } |
| 893 | EXPORT_SYMBOL(xfrm_state_check_expire); |
| 894 | |
| 895 | static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb) |
| 896 | { |
| 897 | int nhead = x->props.header_len + LL_RESERVED_SPACE(skb->dst->dev) |
| 898 | - skb_headroom(skb); |
| 899 | |
| 900 | if (nhead > 0) |
| 901 | return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); |
| 902 | |
| 903 | /* Check tail too... */ |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb) |
| 908 | { |
| 909 | int err = xfrm_state_check_expire(x); |
| 910 | if (err < 0) |
| 911 | goto err; |
| 912 | err = xfrm_state_check_space(x, skb); |
| 913 | err: |
| 914 | return err; |
| 915 | } |
| 916 | EXPORT_SYMBOL(xfrm_state_check); |
| 917 | |
| 918 | struct xfrm_state * |
Al Viro | a94cfd1 | 2006-09-27 18:47:24 -0700 | [diff] [blame] | 919 | xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | unsigned short family) |
| 921 | { |
| 922 | struct xfrm_state *x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | spin_lock_bh(&xfrm_state_lock); |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 925 | x = __xfrm_state_lookup(daddr, spi, proto, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | spin_unlock_bh(&xfrm_state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return x; |
| 928 | } |
| 929 | EXPORT_SYMBOL(xfrm_state_lookup); |
| 930 | |
| 931 | struct xfrm_state * |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 932 | xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 933 | u8 proto, unsigned short family) |
| 934 | { |
| 935 | struct xfrm_state *x; |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 936 | |
| 937 | spin_lock_bh(&xfrm_state_lock); |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 938 | x = __xfrm_state_lookup_byaddr(daddr, saddr, proto, family); |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 939 | spin_unlock_bh(&xfrm_state_lock); |
Masahide NAKAMURA | eb2971b | 2006-08-23 17:56:04 -0700 | [diff] [blame] | 940 | return x; |
| 941 | } |
| 942 | EXPORT_SYMBOL(xfrm_state_lookup_byaddr); |
| 943 | |
| 944 | struct xfrm_state * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | xfrm_find_acq(u8 mode, u32 reqid, u8 proto, |
| 946 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
| 947 | int create, unsigned short family) |
| 948 | { |
| 949 | struct xfrm_state *x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
| 951 | spin_lock_bh(&xfrm_state_lock); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 952 | x = __find_acq_core(family, mode, reqid, proto, daddr, saddr, create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | spin_unlock_bh(&xfrm_state_lock); |
David S. Miller | 2770834 | 2006-08-24 00:13:10 -0700 | [diff] [blame] | 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | return x; |
| 956 | } |
| 957 | EXPORT_SYMBOL(xfrm_find_acq); |
| 958 | |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 959 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 960 | int |
| 961 | xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n, |
| 962 | unsigned short family) |
| 963 | { |
| 964 | int err = 0; |
| 965 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
| 966 | if (!afinfo) |
| 967 | return -EAFNOSUPPORT; |
| 968 | |
| 969 | spin_lock_bh(&xfrm_state_lock); |
| 970 | if (afinfo->tmpl_sort) |
| 971 | err = afinfo->tmpl_sort(dst, src, n); |
| 972 | spin_unlock_bh(&xfrm_state_lock); |
| 973 | xfrm_state_put_afinfo(afinfo); |
| 974 | return err; |
| 975 | } |
| 976 | EXPORT_SYMBOL(xfrm_tmpl_sort); |
| 977 | |
| 978 | int |
| 979 | xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n, |
| 980 | unsigned short family) |
| 981 | { |
| 982 | int err = 0; |
| 983 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
| 984 | if (!afinfo) |
| 985 | return -EAFNOSUPPORT; |
| 986 | |
| 987 | spin_lock_bh(&xfrm_state_lock); |
| 988 | if (afinfo->state_sort) |
| 989 | err = afinfo->state_sort(dst, src, n); |
| 990 | spin_unlock_bh(&xfrm_state_lock); |
| 991 | xfrm_state_put_afinfo(afinfo); |
| 992 | return err; |
| 993 | } |
| 994 | EXPORT_SYMBOL(xfrm_state_sort); |
| 995 | #endif |
| 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | /* Silly enough, but I'm lazy to build resolution list */ |
| 998 | |
| 999 | static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq) |
| 1000 | { |
| 1001 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1003 | for (i = 0; i <= xfrm_state_hmask; i++) { |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1004 | struct hlist_node *entry; |
| 1005 | struct xfrm_state *x; |
| 1006 | |
| 1007 | hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) { |
| 1008 | if (x->km.seq == seq && |
| 1009 | x->km.state == XFRM_STATE_ACQ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | xfrm_state_hold(x); |
| 1011 | return x; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | |
| 1018 | struct xfrm_state *xfrm_find_acq_byseq(u32 seq) |
| 1019 | { |
| 1020 | struct xfrm_state *x; |
| 1021 | |
| 1022 | spin_lock_bh(&xfrm_state_lock); |
| 1023 | x = __xfrm_find_acq_byseq(seq); |
| 1024 | spin_unlock_bh(&xfrm_state_lock); |
| 1025 | return x; |
| 1026 | } |
| 1027 | EXPORT_SYMBOL(xfrm_find_acq_byseq); |
| 1028 | |
| 1029 | u32 xfrm_get_acqseq(void) |
| 1030 | { |
| 1031 | u32 res; |
| 1032 | static u32 acqseq; |
| 1033 | static DEFINE_SPINLOCK(acqseq_lock); |
| 1034 | |
| 1035 | spin_lock_bh(&acqseq_lock); |
| 1036 | res = (++acqseq ? : ++acqseq); |
| 1037 | spin_unlock_bh(&acqseq_lock); |
| 1038 | return res; |
| 1039 | } |
| 1040 | EXPORT_SYMBOL(xfrm_get_acqseq); |
| 1041 | |
| 1042 | void |
Al Viro | 26977b4 | 2006-09-27 18:47:05 -0700 | [diff] [blame] | 1043 | xfrm_alloc_spi(struct xfrm_state *x, __be32 minspi, __be32 maxspi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1045 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | struct xfrm_state *x0; |
| 1047 | |
| 1048 | if (x->id.spi) |
| 1049 | return; |
| 1050 | |
| 1051 | if (minspi == maxspi) { |
| 1052 | x0 = xfrm_state_lookup(&x->id.daddr, minspi, x->id.proto, x->props.family); |
| 1053 | if (x0) { |
| 1054 | xfrm_state_put(x0); |
| 1055 | return; |
| 1056 | } |
| 1057 | x->id.spi = minspi; |
| 1058 | } else { |
| 1059 | u32 spi = 0; |
Al Viro | 26977b4 | 2006-09-27 18:47:05 -0700 | [diff] [blame] | 1060 | u32 low = ntohl(minspi); |
| 1061 | u32 high = ntohl(maxspi); |
| 1062 | for (h=0; h<high-low+1; h++) { |
| 1063 | spi = low + net_random()%(high-low+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | x0 = xfrm_state_lookup(&x->id.daddr, htonl(spi), x->id.proto, x->props.family); |
| 1065 | if (x0 == NULL) { |
| 1066 | x->id.spi = htonl(spi); |
| 1067 | break; |
| 1068 | } |
| 1069 | xfrm_state_put(x0); |
| 1070 | } |
| 1071 | } |
| 1072 | if (x->id.spi) { |
| 1073 | spin_lock_bh(&xfrm_state_lock); |
| 1074 | h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, x->props.family); |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1075 | hlist_add_head(&x->byspi, xfrm_state_byspi+h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | spin_unlock_bh(&xfrm_state_lock); |
| 1077 | wake_up(&km_waitq); |
| 1078 | } |
| 1079 | } |
| 1080 | EXPORT_SYMBOL(xfrm_alloc_spi); |
| 1081 | |
| 1082 | int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*), |
| 1083 | void *data) |
| 1084 | { |
| 1085 | int i; |
| 1086 | struct xfrm_state *x; |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1087 | struct hlist_node *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | int count = 0; |
| 1089 | int err = 0; |
| 1090 | |
| 1091 | spin_lock_bh(&xfrm_state_lock); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1092 | for (i = 0; i <= xfrm_state_hmask; i++) { |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1093 | hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) { |
Masahide NAKAMURA | 5794708 | 2006-09-22 15:06:24 -0700 | [diff] [blame] | 1094 | if (xfrm_id_proto_match(x->id.proto, proto)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | count++; |
| 1096 | } |
| 1097 | } |
| 1098 | if (count == 0) { |
| 1099 | err = -ENOENT; |
| 1100 | goto out; |
| 1101 | } |
| 1102 | |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1103 | for (i = 0; i <= xfrm_state_hmask; i++) { |
David S. Miller | 8f126e3 | 2006-08-24 02:45:07 -0700 | [diff] [blame] | 1104 | hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) { |
Masahide NAKAMURA | 5794708 | 2006-09-22 15:06:24 -0700 | [diff] [blame] | 1105 | if (!xfrm_id_proto_match(x->id.proto, proto)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | continue; |
| 1107 | err = func(x, --count, data); |
| 1108 | if (err) |
| 1109 | goto out; |
| 1110 | } |
| 1111 | } |
| 1112 | out: |
| 1113 | spin_unlock_bh(&xfrm_state_lock); |
| 1114 | return err; |
| 1115 | } |
| 1116 | EXPORT_SYMBOL(xfrm_state_walk); |
| 1117 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1118 | |
| 1119 | void xfrm_replay_notify(struct xfrm_state *x, int event) |
| 1120 | { |
| 1121 | struct km_event c; |
| 1122 | /* we send notify messages in case |
| 1123 | * 1. we updated on of the sequence numbers, and the seqno difference |
| 1124 | * is at least x->replay_maxdiff, in this case we also update the |
| 1125 | * timeout of our timer function |
| 1126 | * 2. if x->replay_maxage has elapsed since last update, |
| 1127 | * and there were changes |
| 1128 | * |
| 1129 | * The state structure must be locked! |
| 1130 | */ |
| 1131 | |
| 1132 | switch (event) { |
| 1133 | case XFRM_REPLAY_UPDATE: |
| 1134 | if (x->replay_maxdiff && |
| 1135 | (x->replay.seq - x->preplay.seq < x->replay_maxdiff) && |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 1136 | (x->replay.oseq - x->preplay.oseq < x->replay_maxdiff)) { |
| 1137 | if (x->xflags & XFRM_TIME_DEFER) |
| 1138 | event = XFRM_REPLAY_TIMEOUT; |
| 1139 | else |
| 1140 | return; |
| 1141 | } |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1142 | |
| 1143 | break; |
| 1144 | |
| 1145 | case XFRM_REPLAY_TIMEOUT: |
| 1146 | if ((x->replay.seq == x->preplay.seq) && |
| 1147 | (x->replay.bitmap == x->preplay.bitmap) && |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 1148 | (x->replay.oseq == x->preplay.oseq)) { |
| 1149 | x->xflags |= XFRM_TIME_DEFER; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1150 | return; |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 1151 | } |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1152 | |
| 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | memcpy(&x->preplay, &x->replay, sizeof(struct xfrm_replay_state)); |
| 1157 | c.event = XFRM_MSG_NEWAE; |
| 1158 | c.data.aevent = event; |
| 1159 | km_state_notify(x, &c); |
| 1160 | |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1161 | if (x->replay_maxage && |
David S. Miller | a47f0ce | 2006-08-24 03:54:22 -0700 | [diff] [blame] | 1162 | !mod_timer(&x->rtimer, jiffies + x->replay_maxage)) |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 1163 | x->xflags &= ~XFRM_TIME_DEFER; |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1164 | } |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 1165 | EXPORT_SYMBOL(xfrm_replay_notify); |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1166 | |
| 1167 | static void xfrm_replay_timer_handler(unsigned long data) |
| 1168 | { |
| 1169 | struct xfrm_state *x = (struct xfrm_state*)data; |
| 1170 | |
| 1171 | spin_lock(&x->lock); |
| 1172 | |
Jamal Hadi Salim | 2717096 | 2006-04-14 15:03:05 -0700 | [diff] [blame] | 1173 | if (x->km.state == XFRM_STATE_VALID) { |
| 1174 | if (xfrm_aevent_is_on()) |
| 1175 | xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT); |
| 1176 | else |
| 1177 | x->xflags |= XFRM_TIME_DEFER; |
| 1178 | } |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1179 | |
| 1180 | spin_unlock(&x->lock); |
| 1181 | } |
| 1182 | |
Al Viro | a252cc2 | 2006-09-27 18:48:18 -0700 | [diff] [blame^] | 1183 | int xfrm_replay_check(struct xfrm_state *x, __be32 net_seq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
| 1185 | u32 diff; |
Al Viro | a252cc2 | 2006-09-27 18:48:18 -0700 | [diff] [blame^] | 1186 | u32 seq = ntohl(net_seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | if (unlikely(seq == 0)) |
| 1189 | return -EINVAL; |
| 1190 | |
| 1191 | if (likely(seq > x->replay.seq)) |
| 1192 | return 0; |
| 1193 | |
| 1194 | diff = x->replay.seq - seq; |
| 1195 | if (diff >= x->props.replay_window) { |
| 1196 | x->stats.replay_window++; |
| 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | |
| 1200 | if (x->replay.bitmap & (1U << diff)) { |
| 1201 | x->stats.replay++; |
| 1202 | return -EINVAL; |
| 1203 | } |
| 1204 | return 0; |
| 1205 | } |
| 1206 | EXPORT_SYMBOL(xfrm_replay_check); |
| 1207 | |
| 1208 | void xfrm_replay_advance(struct xfrm_state *x, u32 seq) |
| 1209 | { |
| 1210 | u32 diff; |
| 1211 | |
| 1212 | seq = ntohl(seq); |
| 1213 | |
| 1214 | if (seq > x->replay.seq) { |
| 1215 | diff = seq - x->replay.seq; |
| 1216 | if (diff < x->props.replay_window) |
| 1217 | x->replay.bitmap = ((x->replay.bitmap) << diff) | 1; |
| 1218 | else |
| 1219 | x->replay.bitmap = 1; |
| 1220 | x->replay.seq = seq; |
| 1221 | } else { |
| 1222 | diff = x->replay.seq - seq; |
| 1223 | x->replay.bitmap |= (1U << diff); |
| 1224 | } |
Jamal Hadi Salim | f8cd548 | 2006-03-20 19:15:11 -0800 | [diff] [blame] | 1225 | |
| 1226 | if (xfrm_aevent_is_on()) |
| 1227 | xfrm_replay_notify(x, XFRM_REPLAY_UPDATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
| 1229 | EXPORT_SYMBOL(xfrm_replay_advance); |
| 1230 | |
| 1231 | static struct list_head xfrm_km_list = LIST_HEAD_INIT(xfrm_km_list); |
| 1232 | static DEFINE_RWLOCK(xfrm_km_lock); |
| 1233 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1234 | 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] | 1235 | { |
| 1236 | struct xfrm_mgr *km; |
| 1237 | |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1238 | read_lock(&xfrm_km_lock); |
| 1239 | list_for_each_entry(km, &xfrm_km_list, list) |
| 1240 | if (km->notify_policy) |
| 1241 | km->notify_policy(xp, dir, c); |
| 1242 | read_unlock(&xfrm_km_lock); |
| 1243 | } |
| 1244 | |
| 1245 | void km_state_notify(struct xfrm_state *x, struct km_event *c) |
| 1246 | { |
| 1247 | struct xfrm_mgr *km; |
| 1248 | read_lock(&xfrm_km_lock); |
| 1249 | list_for_each_entry(km, &xfrm_km_list, list) |
| 1250 | if (km->notify) |
| 1251 | km->notify(x, c); |
| 1252 | read_unlock(&xfrm_km_lock); |
| 1253 | } |
| 1254 | |
| 1255 | EXPORT_SYMBOL(km_policy_notify); |
| 1256 | EXPORT_SYMBOL(km_state_notify); |
| 1257 | |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 1258 | 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] | 1259 | { |
| 1260 | struct km_event c; |
| 1261 | |
Herbert Xu | bf08867 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 1262 | c.data.hard = hard; |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 1263 | c.pid = pid; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1264 | c.event = XFRM_MSG_EXPIRE; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1265 | km_state_notify(x, &c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
| 1267 | if (hard) |
| 1268 | wake_up(&km_waitq); |
| 1269 | } |
| 1270 | |
Jamal Hadi Salim | 53bc6b4 | 2006-03-20 19:17:03 -0800 | [diff] [blame] | 1271 | EXPORT_SYMBOL(km_state_expired); |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1272 | /* |
| 1273 | * We send to all registered managers regardless of failure |
| 1274 | * We are happy with one success |
| 1275 | */ |
Jamal Hadi Salim | 980ebd2 | 2006-03-20 19:16:40 -0800 | [diff] [blame] | 1276 | 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] | 1277 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1278 | int err = -EINVAL, acqret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | struct xfrm_mgr *km; |
| 1280 | |
| 1281 | read_lock(&xfrm_km_lock); |
| 1282 | list_for_each_entry(km, &xfrm_km_list, list) { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1283 | acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT); |
| 1284 | if (!acqret) |
| 1285 | err = acqret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
| 1287 | read_unlock(&xfrm_km_lock); |
| 1288 | return err; |
| 1289 | } |
Jamal Hadi Salim | 980ebd2 | 2006-03-20 19:16:40 -0800 | [diff] [blame] | 1290 | EXPORT_SYMBOL(km_query); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
| 1292 | int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport) |
| 1293 | { |
| 1294 | int err = -EINVAL; |
| 1295 | struct xfrm_mgr *km; |
| 1296 | |
| 1297 | read_lock(&xfrm_km_lock); |
| 1298 | list_for_each_entry(km, &xfrm_km_list, list) { |
| 1299 | if (km->new_mapping) |
| 1300 | err = km->new_mapping(x, ipaddr, sport); |
| 1301 | if (!err) |
| 1302 | break; |
| 1303 | } |
| 1304 | read_unlock(&xfrm_km_lock); |
| 1305 | return err; |
| 1306 | } |
| 1307 | EXPORT_SYMBOL(km_new_mapping); |
| 1308 | |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 1309 | 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] | 1310 | { |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1311 | struct km_event c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
Herbert Xu | bf08867 | 2005-06-18 22:44:00 -0700 | [diff] [blame] | 1313 | c.data.hard = hard; |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 1314 | c.pid = pid; |
Herbert Xu | f60f6b8 | 2005-06-18 22:44:37 -0700 | [diff] [blame] | 1315 | c.event = XFRM_MSG_POLEXPIRE; |
Jamal Hadi Salim | 26b15da | 2005-06-18 22:42:13 -0700 | [diff] [blame] | 1316 | km_policy_notify(pol, dir, &c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
| 1318 | if (hard) |
| 1319 | wake_up(&km_waitq); |
| 1320 | } |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 1321 | EXPORT_SYMBOL(km_policy_expired); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
Masahide NAKAMURA | 97a64b4 | 2006-08-23 20:44:06 -0700 | [diff] [blame] | 1323 | int km_report(u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr) |
| 1324 | { |
| 1325 | int err = -EINVAL; |
| 1326 | int ret; |
| 1327 | struct xfrm_mgr *km; |
| 1328 | |
| 1329 | read_lock(&xfrm_km_lock); |
| 1330 | list_for_each_entry(km, &xfrm_km_list, list) { |
| 1331 | if (km->report) { |
| 1332 | ret = km->report(proto, sel, addr); |
| 1333 | if (!ret) |
| 1334 | err = ret; |
| 1335 | } |
| 1336 | } |
| 1337 | read_unlock(&xfrm_km_lock); |
| 1338 | return err; |
| 1339 | } |
| 1340 | EXPORT_SYMBOL(km_report); |
| 1341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) |
| 1343 | { |
| 1344 | int err; |
| 1345 | u8 *data; |
| 1346 | struct xfrm_mgr *km; |
| 1347 | struct xfrm_policy *pol = NULL; |
| 1348 | |
| 1349 | if (optlen <= 0 || optlen > PAGE_SIZE) |
| 1350 | return -EMSGSIZE; |
| 1351 | |
| 1352 | data = kmalloc(optlen, GFP_KERNEL); |
| 1353 | if (!data) |
| 1354 | return -ENOMEM; |
| 1355 | |
| 1356 | err = -EFAULT; |
| 1357 | if (copy_from_user(data, optval, optlen)) |
| 1358 | goto out; |
| 1359 | |
| 1360 | err = -EINVAL; |
| 1361 | read_lock(&xfrm_km_lock); |
| 1362 | list_for_each_entry(km, &xfrm_km_list, list) { |
Venkat Yekkirala | cb969f0 | 2006-07-24 23:32:20 -0700 | [diff] [blame] | 1363 | pol = km->compile_policy(sk, optname, data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | optlen, &err); |
| 1365 | if (err >= 0) |
| 1366 | break; |
| 1367 | } |
| 1368 | read_unlock(&xfrm_km_lock); |
| 1369 | |
| 1370 | if (err >= 0) { |
| 1371 | xfrm_sk_policy_insert(sk, err, pol); |
| 1372 | xfrm_pol_put(pol); |
| 1373 | err = 0; |
| 1374 | } |
| 1375 | |
| 1376 | out: |
| 1377 | kfree(data); |
| 1378 | return err; |
| 1379 | } |
| 1380 | EXPORT_SYMBOL(xfrm_user_policy); |
| 1381 | |
| 1382 | int xfrm_register_km(struct xfrm_mgr *km) |
| 1383 | { |
| 1384 | write_lock_bh(&xfrm_km_lock); |
| 1385 | list_add_tail(&km->list, &xfrm_km_list); |
| 1386 | write_unlock_bh(&xfrm_km_lock); |
| 1387 | return 0; |
| 1388 | } |
| 1389 | EXPORT_SYMBOL(xfrm_register_km); |
| 1390 | |
| 1391 | int xfrm_unregister_km(struct xfrm_mgr *km) |
| 1392 | { |
| 1393 | write_lock_bh(&xfrm_km_lock); |
| 1394 | list_del(&km->list); |
| 1395 | write_unlock_bh(&xfrm_km_lock); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | EXPORT_SYMBOL(xfrm_unregister_km); |
| 1399 | |
| 1400 | int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo) |
| 1401 | { |
| 1402 | int err = 0; |
| 1403 | if (unlikely(afinfo == NULL)) |
| 1404 | return -EINVAL; |
| 1405 | if (unlikely(afinfo->family >= NPROTO)) |
| 1406 | return -EAFNOSUPPORT; |
Ingo Molnar | f311150 | 2006-04-28 15:30:03 -0700 | [diff] [blame] | 1407 | write_lock_bh(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL)) |
| 1409 | err = -ENOBUFS; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 1410 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | xfrm_state_afinfo[afinfo->family] = afinfo; |
Ingo Molnar | f311150 | 2006-04-28 15:30:03 -0700 | [diff] [blame] | 1412 | write_unlock_bh(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | return err; |
| 1414 | } |
| 1415 | EXPORT_SYMBOL(xfrm_state_register_afinfo); |
| 1416 | |
| 1417 | int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo) |
| 1418 | { |
| 1419 | int err = 0; |
| 1420 | if (unlikely(afinfo == NULL)) |
| 1421 | return -EINVAL; |
| 1422 | if (unlikely(afinfo->family >= NPROTO)) |
| 1423 | return -EAFNOSUPPORT; |
Ingo Molnar | f311150 | 2006-04-28 15:30:03 -0700 | [diff] [blame] | 1424 | write_lock_bh(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) { |
| 1426 | if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo)) |
| 1427 | err = -EINVAL; |
David S. Miller | edcd582 | 2006-08-24 00:42:45 -0700 | [diff] [blame] | 1428 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | xfrm_state_afinfo[afinfo->family] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | } |
Ingo Molnar | f311150 | 2006-04-28 15:30:03 -0700 | [diff] [blame] | 1431 | write_unlock_bh(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | return err; |
| 1433 | } |
| 1434 | EXPORT_SYMBOL(xfrm_state_unregister_afinfo); |
| 1435 | |
| 1436 | static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family) |
| 1437 | { |
| 1438 | struct xfrm_state_afinfo *afinfo; |
| 1439 | if (unlikely(family >= NPROTO)) |
| 1440 | return NULL; |
| 1441 | read_lock(&xfrm_state_afinfo_lock); |
| 1442 | afinfo = xfrm_state_afinfo[family]; |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 1443 | if (unlikely(!afinfo)) |
| 1444 | read_unlock(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | return afinfo; |
| 1446 | } |
| 1447 | |
| 1448 | static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo) |
| 1449 | { |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 1450 | read_unlock(&xfrm_state_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */ |
| 1454 | void xfrm_state_delete_tunnel(struct xfrm_state *x) |
| 1455 | { |
| 1456 | if (x->tunnel) { |
| 1457 | struct xfrm_state *t = x->tunnel; |
| 1458 | |
| 1459 | if (atomic_read(&t->tunnel_users) == 2) |
| 1460 | xfrm_state_delete(t); |
| 1461 | atomic_dec(&t->tunnel_users); |
| 1462 | xfrm_state_put(t); |
| 1463 | x->tunnel = NULL; |
| 1464 | } |
| 1465 | } |
| 1466 | EXPORT_SYMBOL(xfrm_state_delete_tunnel); |
| 1467 | |
Herbert Xu | 80b30c1 | 2005-10-15 10:58:30 +1000 | [diff] [blame] | 1468 | /* |
| 1469 | * This function is NOT optimal. For example, with ESP it will give an |
| 1470 | * MTU that's usually two bytes short of being optimal. However, it will |
| 1471 | * usually give an answer that's a multiple of 4 provided the input is |
| 1472 | * also a multiple of 4. |
| 1473 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | int xfrm_state_mtu(struct xfrm_state *x, int mtu) |
| 1475 | { |
| 1476 | int res = mtu; |
| 1477 | |
| 1478 | res -= x->props.header_len; |
| 1479 | |
| 1480 | for (;;) { |
| 1481 | int m = res; |
| 1482 | |
| 1483 | if (m < 68) |
| 1484 | return 68; |
| 1485 | |
| 1486 | spin_lock_bh(&x->lock); |
| 1487 | if (x->km.state == XFRM_STATE_VALID && |
| 1488 | x->type && x->type->get_max_size) |
| 1489 | m = x->type->get_max_size(x, m); |
| 1490 | else |
| 1491 | m += x->props.header_len; |
| 1492 | spin_unlock_bh(&x->lock); |
| 1493 | |
| 1494 | if (m <= mtu) |
| 1495 | break; |
| 1496 | res -= (m - mtu); |
| 1497 | } |
| 1498 | |
| 1499 | return res; |
| 1500 | } |
| 1501 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 1502 | int xfrm_init_state(struct xfrm_state *x) |
| 1503 | { |
Herbert Xu | d094cd8 | 2005-06-20 13:19:41 -0700 | [diff] [blame] | 1504 | struct xfrm_state_afinfo *afinfo; |
| 1505 | int family = x->props.family; |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 1506 | int err; |
| 1507 | |
Herbert Xu | d094cd8 | 2005-06-20 13:19:41 -0700 | [diff] [blame] | 1508 | err = -EAFNOSUPPORT; |
| 1509 | afinfo = xfrm_state_get_afinfo(family); |
| 1510 | if (!afinfo) |
| 1511 | goto error; |
| 1512 | |
| 1513 | err = 0; |
| 1514 | if (afinfo->init_flags) |
| 1515 | err = afinfo->init_flags(x); |
| 1516 | |
| 1517 | xfrm_state_put_afinfo(afinfo); |
| 1518 | |
| 1519 | if (err) |
| 1520 | goto error; |
| 1521 | |
| 1522 | err = -EPROTONOSUPPORT; |
| 1523 | x->type = xfrm_get_type(x->id.proto, family); |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 1524 | if (x->type == NULL) |
| 1525 | goto error; |
| 1526 | |
| 1527 | err = x->type->init_state(x); |
| 1528 | if (err) |
| 1529 | goto error; |
| 1530 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 1531 | x->mode = xfrm_get_mode(x->props.mode, family); |
| 1532 | if (x->mode == NULL) |
| 1533 | goto error; |
| 1534 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 1535 | x->km.state = XFRM_STATE_VALID; |
| 1536 | |
| 1537 | error: |
| 1538 | return err; |
| 1539 | } |
| 1540 | |
| 1541 | EXPORT_SYMBOL(xfrm_init_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
| 1543 | void __init xfrm_state_init(void) |
| 1544 | { |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1545 | unsigned int sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1547 | sz = sizeof(struct hlist_head) * 8; |
| 1548 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 1549 | xfrm_state_bydst = xfrm_hash_alloc(sz); |
| 1550 | xfrm_state_bysrc = xfrm_hash_alloc(sz); |
| 1551 | xfrm_state_byspi = xfrm_hash_alloc(sz); |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 1552 | if (!xfrm_state_bydst || !xfrm_state_bysrc || !xfrm_state_byspi) |
| 1553 | panic("XFRM: Cannot allocate bydst/bysrc/byspi hashes."); |
| 1554 | xfrm_state_hmask = ((sz / sizeof(struct hlist_head)) - 1); |
| 1555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task, NULL); |
| 1557 | } |
| 1558 | |