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