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