1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */ 3 4 #include <linux/bpf.h> 5 #include <linux/btf_ids.h> 6 #include <linux/filter.h> 7 #include <linux/errno.h> 8 #include <linux/file.h> 9 #include <linux/net.h> 10 #include <linux/workqueue.h> 11 #include <linux/skmsg.h> 12 #include <linux/list.h> 13 #include <linux/jhash.h> 14 #include <linux/sock_diag.h> 15 #include <net/udp.h> 16 17 struct bpf_stab { 18 struct bpf_map map; 19 struct sock **sks; 20 struct sk_psock_progs progs; 21 spinlock_t lock; 22 }; 23 24 #define SOCK_CREATE_FLAG_MASK \ 25 (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY) 26 27 /* This mutex is used to 28 * - protect race between prog/link attach/detach and link prog update, and 29 * - protect race between releasing and accessing map in bpf_link. 30 * A single global mutex lock is used since it is expected contention is low. 31 */ 32 static DEFINE_MUTEX(sockmap_mutex); 33 34 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, 35 struct bpf_prog *old, struct bpf_link *link, 36 u32 which); 37 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map); 38 39 static struct bpf_map *sock_map_alloc(union bpf_attr *attr) 40 { 41 struct bpf_stab *stab; 42 43 if (attr->max_entries == 0 || 44 attr->key_size != 4 || 45 (attr->value_size != sizeof(u32) && 46 attr->value_size != sizeof(u64)) || 47 attr->map_flags & ~SOCK_CREATE_FLAG_MASK) 48 return ERR_PTR(-EINVAL); 49 50 stab = bpf_map_area_alloc(sizeof(*stab), NUMA_NO_NODE); 51 if (!stab) 52 return ERR_PTR(-ENOMEM); 53 54 bpf_map_init_from_attr(&stab->map, attr); 55 spin_lock_init(&stab->lock); 56 57 stab->sks = bpf_map_area_alloc((u64) stab->map.max_entries * 58 sizeof(struct sock *), 59 stab->map.numa_node); 60 if (!stab->sks) { 61 bpf_map_area_free(stab); 62 return ERR_PTR(-ENOMEM); 63 } 64 65 return &stab->map; 66 } 67 68 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog) 69 { 70 u32 ufd = attr->target_fd; 71 struct bpf_map *map; 72 struct fd f; 73 int ret; 74 75 if (attr->attach_flags || attr->replace_bpf_fd) 76 return -EINVAL; 77 78 f = fdget(ufd); 79 map = __bpf_map_get(f); 80 if (IS_ERR(map)) 81 return PTR_ERR(map); 82 mutex_lock(&sockmap_mutex); 83 ret = sock_map_prog_update(map, prog, NULL, NULL, attr->attach_type); 84 mutex_unlock(&sockmap_mutex); 85 fdput(f); 86 return ret; 87 } 88 89 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype) 90 { 91 u32 ufd = attr->target_fd; 92 struct bpf_prog *prog; 93 struct bpf_map *map; 94 struct fd f; 95 int ret; 96 97 if (attr->attach_flags || attr->replace_bpf_fd) 98 return -EINVAL; 99 100 f = fdget(ufd); 101 map = __bpf_map_get(f); 102 if (IS_ERR(map)) 103 return PTR_ERR(map); 104 105 prog = bpf_prog_get(attr->attach_bpf_fd); 106 if (IS_ERR(prog)) { 107 ret = PTR_ERR(prog); 108 goto put_map; 109 } 110 111 if (prog->type != ptype) { 112 ret = -EINVAL; 113 goto put_prog; 114 } 115 116 mutex_lock(&sockmap_mutex); 117 ret = sock_map_prog_update(map, NULL, prog, NULL, attr->attach_type); 118 mutex_unlock(&sockmap_mutex); 119 put_prog: 120 bpf_prog_put(prog); 121 put_map: 122 fdput(f); 123 return ret; 124 } 125 126 static void sock_map_sk_acquire(struct sock *sk) 127 __acquires(&sk->sk_lock.slock) 128 { 129 lock_sock(sk); 130 rcu_read_lock(); 131 } 132 133 static void sock_map_sk_release(struct sock *sk) 134 __releases(&sk->sk_lock.slock) 135 { 136 rcu_read_unlock(); 137 release_sock(sk); 138 } 139 140 static void sock_map_add_link(struct sk_psock *psock, 141 struct sk_psock_link *link, 142 struct bpf_map *map, void *link_raw) 143 { 144 link->link_raw = link_raw; 145 link->map = map; 146 spin_lock_bh(&psock->link_lock); 147 list_add_tail(&link->list, &psock->link); 148 spin_unlock_bh(&psock->link_lock); 149 } 150 151 static void sock_map_del_link(struct sock *sk, 152 struct sk_psock *psock, void *link_raw) 153 { 154 bool strp_stop = false, verdict_stop = false; 155 struct sk_psock_link *link, *tmp; 156 157 spin_lock_bh(&psock->link_lock); 158 list_for_each_entry_safe(link, tmp, &psock->link, list) { 159 if (link->link_raw == link_raw) { 160 struct bpf_map *map = link->map; 161 struct sk_psock_progs *progs = sock_map_progs(map); 162 163 if (psock->saved_data_ready && progs->stream_parser) 164 strp_stop = true; 165 if (psock->saved_data_ready && progs->stream_verdict) 166 verdict_stop = true; 167 if (psock->saved_data_ready && progs->skb_verdict) 168 verdict_stop = true; 169 list_del(&link->list); 170 sk_psock_free_link(link); 171 } 172 } 173 spin_unlock_bh(&psock->link_lock); 174 if (strp_stop || verdict_stop) { 175 write_lock_bh(&sk->sk_callback_lock); 176 if (strp_stop) 177 sk_psock_stop_strp(sk, psock); 178 if (verdict_stop) 179 sk_psock_stop_verdict(sk, psock); 180 181 if (psock->psock_update_sk_prot) 182 psock->psock_update_sk_prot(sk, psock, false); 183 write_unlock_bh(&sk->sk_callback_lock); 184 } 185 } 186 187 static void sock_map_unref(struct sock *sk, void *link_raw) 188 { 189 struct sk_psock *psock = sk_psock(sk); 190 191 if (likely(psock)) { 192 sock_map_del_link(sk, psock, link_raw); 193 sk_psock_put(sk, psock); 194 } 195 } 196 197 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock) 198 { 199 if (!sk->sk_prot->psock_update_sk_prot) 200 return -EINVAL; 201 psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot; 202 return sk->sk_prot->psock_update_sk_prot(sk, psock, false); 203 } 204 205 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk) 206 { 207 struct sk_psock *psock; 208 209 rcu_read_lock(); 210 psock = sk_psock(sk); 211 if (psock) { 212 if (sk->sk_prot->close != sock_map_close) { 213 psock = ERR_PTR(-EBUSY); 214 goto out; 215 } 216 217 if (!refcount_inc_not_zero(&psock->refcnt)) 218 psock = ERR_PTR(-EBUSY); 219 } 220 out: 221 rcu_read_unlock(); 222 return psock; 223 } 224 225 static int sock_map_link(struct bpf_map *map, struct sock *sk) 226 { 227 struct sk_psock_progs *progs = sock_map_progs(map); 228 struct bpf_prog *stream_verdict = NULL; 229 struct bpf_prog *stream_parser = NULL; 230 struct bpf_prog *skb_verdict = NULL; 231 struct bpf_prog *msg_parser = NULL; 232 struct sk_psock *psock; 233 int ret; 234 235 stream_verdict = READ_ONCE(progs->stream_verdict); 236 if (stream_verdict) { 237 stream_verdict = bpf_prog_inc_not_zero(stream_verdict); 238 if (IS_ERR(stream_verdict)) 239 return PTR_ERR(stream_verdict); 240 } 241 242 stream_parser = READ_ONCE(progs->stream_parser); 243 if (stream_parser) { 244 stream_parser = bpf_prog_inc_not_zero(stream_parser); 245 if (IS_ERR(stream_parser)) { 246 ret = PTR_ERR(stream_parser); 247 goto out_put_stream_verdict; 248 } 249 } 250 251 msg_parser = READ_ONCE(progs->msg_parser); 252 if (msg_parser) { 253 msg_parser = bpf_prog_inc_not_zero(msg_parser); 254 if (IS_ERR(msg_parser)) { 255 ret = PTR_ERR(msg_parser); 256 goto out_put_stream_parser; 257 } 258 } 259 260 skb_verdict = READ_ONCE(progs->skb_verdict); 261 if (skb_verdict) { 262 skb_verdict = bpf_prog_inc_not_zero(skb_verdict); 263 if (IS_ERR(skb_verdict)) { 264 ret = PTR_ERR(skb_verdict); 265 goto out_put_msg_parser; 266 } 267 } 268 269 psock = sock_map_psock_get_checked(sk); 270 if (IS_ERR(psock)) { 271 ret = PTR_ERR(psock); 272 goto out_progs; 273 } 274 275 if (psock) { 276 if ((msg_parser && READ_ONCE(psock->progs.msg_parser)) || 277 (stream_parser && READ_ONCE(psock->progs.stream_parser)) || 278 (skb_verdict && READ_ONCE(psock->progs.skb_verdict)) || 279 (skb_verdict && READ_ONCE(psock->progs.stream_verdict)) || 280 (stream_verdict && READ_ONCE(psock->progs.skb_verdict)) || 281 (stream_verdict && READ_ONCE(psock->progs.stream_verdict))) { 282 sk_psock_put(sk, psock); 283 ret = -EBUSY; 284 goto out_progs; 285 } 286 } else { 287 psock = sk_psock_init(sk, map->numa_node); 288 if (IS_ERR(psock)) { 289 ret = PTR_ERR(psock); 290 goto out_progs; 291 } 292 } 293 294 if (msg_parser) 295 psock_set_prog(&psock->progs.msg_parser, msg_parser); 296 if (stream_parser) 297 psock_set_prog(&psock->progs.stream_parser, stream_parser); 298 if (stream_verdict) 299 psock_set_prog(&psock->progs.stream_verdict, stream_verdict); 300 if (skb_verdict) 301 psock_set_prog(&psock->progs.skb_verdict, skb_verdict); 302 303 /* msg_* and stream_* programs references tracked in psock after this 304 * point. Reference dec and cleanup will occur through psock destructor 305 */ 306 ret = sock_map_init_proto(sk, psock); 307 if (ret < 0) { 308 sk_psock_put(sk, psock); 309 goto out; 310 } 311 312 write_lock_bh(&sk->sk_callback_lock); 313 if (stream_parser && stream_verdict && !psock->saved_data_ready) { 314 ret = sk_psock_init_strp(sk, psock); 315 if (ret) { 316 write_unlock_bh(&sk->sk_callback_lock); 317 sk_psock_put(sk, psock); 318 goto out; 319 } 320 sk_psock_start_strp(sk, psock); 321 } else if (!stream_parser && stream_verdict && !psock->saved_data_ready) { 322 sk_psock_start_verdict(sk,psock); 323 } else if (!stream_verdict && skb_verdict && !psock->saved_data_ready) { 324 sk_psock_start_verdict(sk, psock); 325 } 326 write_unlock_bh(&sk->sk_callback_lock); 327 return 0; 328 out_progs: 329 if (skb_verdict) 330 bpf_prog_put(skb_verdict); 331 out_put_msg_parser: 332 if (msg_parser) 333 bpf_prog_put(msg_parser); 334 out_put_stream_parser: 335 if (stream_parser) 336 bpf_prog_put(stream_parser); 337 out_put_stream_verdict: 338 if (stream_verdict) 339 bpf_prog_put(stream_verdict); 340 out: 341 return ret; 342 } 343 344 static void sock_map_free(struct bpf_map *map) 345 { 346 struct bpf_stab *stab = container_of(map, struct bpf_stab, map); 347 int i; 348 349 /* After the sync no updates or deletes will be in-flight so it 350 * is safe to walk map and remove entries without risking a race 351 * in EEXIST update case. 352 */ 353 synchronize_rcu(); 354 for (i = 0; i < stab->map.max_entries; i++) { 355 struct sock **psk = &stab->sks[i]; 356 struct sock *sk; 357 358 sk = xchg(psk, NULL); 359 if (sk) { 360 sock_hold(sk); 361 lock_sock(sk); 362 rcu_read_lock(); 363 sock_map_unref(sk, psk); 364 rcu_read_unlock(); 365 release_sock(sk); 366 sock_put(sk); 367 } 368 } 369 370 /* wait for psock readers accessing its map link */ 371 synchronize_rcu(); 372 373 bpf_map_area_free(stab->sks); 374 bpf_map_area_free(stab); 375 } 376 377 static void sock_map_release_progs(struct bpf_map *map) 378 { 379 psock_progs_drop(&container_of(map, struct bpf_stab, map)->progs); 380 } 381 382 static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key) 383 { 384 struct bpf_stab *stab = container_of(map, struct bpf_stab, map); 385 386 WARN_ON_ONCE(!rcu_read_lock_held()); 387 388 if (unlikely(key >= map->max_entries)) 389 return NULL; 390 return READ_ONCE(stab->sks[key]); 391 } 392 393 static void *sock_map_lookup(struct bpf_map *map, void *key) 394 { 395 struct sock *sk; 396 397 sk = __sock_map_lookup_elem(map, *(u32 *)key); 398 if (!sk) 399 return NULL; 400 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt)) 401 return NULL; 402 return sk; 403 } 404 405 static void *sock_map_lookup_sys(struct bpf_map *map, void *key) 406 { 407 struct sock *sk; 408 409 if (map->value_size != sizeof(u64)) 410 return ERR_PTR(-ENOSPC); 411 412 sk = __sock_map_lookup_elem(map, *(u32 *)key); 413 if (!sk) 414 return ERR_PTR(-ENOENT); 415 416 __sock_gen_cookie(sk); 417 return &sk->sk_cookie; 418 } 419 420 static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test, 421 struct sock **psk) 422 { 423 struct sock *sk; 424 int err = 0; 425 426 if (irqs_disabled()) 427 return -EOPNOTSUPP; /* locks here are hardirq-unsafe */ 428 429 spin_lock_bh(&stab->lock); 430 sk = *psk; 431 if (!sk_test || sk_test == sk) 432 sk = xchg(psk, NULL); 433 434 if (likely(sk)) 435 sock_map_unref(sk, psk); 436 else 437 err = -EINVAL; 438 439 spin_unlock_bh(&stab->lock); 440 return err; 441 } 442 443 static void sock_map_delete_from_link(struct bpf_map *map, struct sock *sk, 444 void *link_raw) 445 { 446 struct bpf_stab *stab = container_of(map, struct bpf_stab, map); 447 448 __sock_map_delete(stab, sk, link_raw); 449 } 450 451 static long sock_map_delete_elem(struct bpf_map *map, void *key) 452 { 453 struct bpf_stab *stab = container_of(map, struct bpf_stab, map); 454 u32 i = *(u32 *)key; 455 struct sock **psk; 456 457 if (unlikely(i >= map->max_entries)) 458 return -EINVAL; 459 460 psk = &stab->sks[i]; 461 return __sock_map_delete(stab, NULL, psk); 462 } 463 464 static int sock_map_get_next_key(struct bpf_map *map, void *key, void *next) 465 { 466 struct bpf_stab *stab = container_of(map, struct bpf_stab, map); 467 u32 i = key ? *(u32 *)key : U32_MAX; 468 u32 *key_next = next; 469 470 if (i == stab->map.max_entries - 1) 471 return -ENOENT; 472 if (i >= stab->map.max_entries) 473 *key_next = 0; 474 else 475 *key_next = i + 1; 476 return 0; 477 } 478 479 static int sock_map_update_common(struct bpf_map *map, u32 idx, 480 struct sock *sk, u64 flags) 481 { 482 struct bpf_stab *stab = container_of(map, struct bpf_stab, map); 483 struct sk_psock_link *link; 484 struct sk_psock *psock; 485 struct sock *osk; 486 int ret; 487 488 WARN_ON_ONCE(!rcu_read_lock_held()); 489 if (unlikely(flags > BPF_EXIST)) 490 return -EINVAL; 491 if (unlikely(idx >= map->max_entries)) 492 return -E2BIG; 493 494 link = sk_psock_init_link(); 495 if (!link) 496 return -ENOMEM; 497 498 ret = sock_map_link(map, sk); 499 if (ret < 0) 500 goto out_free; 501 502 psock = sk_psock(sk); 503 WARN_ON_ONCE(!psock); 504 505 spin_lock_bh(&stab->lock); 506 osk = stab->sks[idx]; 507 if (osk && flags == BPF_NOEXIST) { 508 ret = -EEXIST; 509 goto out_unlock; 510 } else if (!osk && flags == BPF_EXIST) { 511 ret = -ENOENT; 512 goto out_unlock; 513 } 514 515 sock_map_add_link(psock, link, map, &stab->sks[idx]); 516 stab->sks[idx] = sk; 517 if (osk) 518 sock_map_unref(osk, &stab->sks[idx]); 519 spin_unlock_bh(&stab->lock); 520 return 0; 521 out_unlock: 522 spin_unlock_bh(&stab->lock); 523 if (psock) 524 sk_psock_put(sk, psock); 525 out_free: 526 sk_psock_free_link(link); 527 return ret; 528 } 529 530 static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops) 531 { 532 return ops->op == BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB || 533 ops->op == BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB || 534 ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB; 535 } 536 537 static bool sock_map_redirect_allowed(const struct sock *sk) 538 { 539 if (sk_is_tcp(sk)) 540 return sk->sk_state != TCP_LISTEN; 541 else 542 return sk->sk_state == TCP_ESTABLISHED; 543 } 544 545 static bool sock_map_sk_is_suitable(const struct sock *sk) 546 { 547 return !!sk->sk_prot->psock_update_sk_prot; 548 } 549 550 static bool sock_map_sk_state_allowed(const struct sock *sk) 551 { 552 if (sk_is_tcp(sk)) 553 return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN); 554 if (sk_is_stream_unix(sk)) 555 return (1 << sk->sk_state) & TCPF_ESTABLISHED; 556 return true; 557 } 558 559 static int sock_hash_update_common(struct bpf_map *map, void *key, 560 struct sock *sk, u64 flags); 561 562 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, 563 u64 flags) 564 { 565 struct socket *sock; 566 struct sock *sk; 567 int ret; 568 u64 ufd; 569 570 if (map->value_size == sizeof(u64)) 571 ufd = *(u64 *)value; 572 else 573 ufd = *(u32 *)value; 574 if (ufd > S32_MAX) 575 return -EINVAL; 576 577 sock = sockfd_lookup(ufd, &ret); 578 if (!sock) 579 return ret; 580 sk = sock->sk; 581 if (!sk) { 582 ret = -EINVAL; 583 goto out; 584 } 585 if (!sock_map_sk_is_suitable(sk)) { 586 ret = -EOPNOTSUPP; 587 goto out; 588 } 589 590 sock_map_sk_acquire(sk); 591 if (!sock_map_sk_state_allowed(sk)) 592 ret = -EOPNOTSUPP; 593 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP) 594 ret = sock_map_update_common(map, *(u32 *)key, sk, flags); 595 else 596 ret = sock_hash_update_common(map, key, sk, flags); 597 sock_map_sk_release(sk); 598 out: 599 sockfd_put(sock); 600 return ret; 601 } 602 603 static long sock_map_update_elem(struct bpf_map *map, void *key, 604 void *value, u64 flags) 605 { 606 struct sock *sk = (struct sock *)value; 607 int ret; 608 609 if (unlikely(!sk || !sk_fullsock(sk))) 610 return -EINVAL; 611 612 if (!sock_map_sk_is_suitable(sk)) 613 return -EOPNOTSUPP; 614 615 local_bh_disable(); 616 bh_lock_sock(sk); 617 if (!sock_map_sk_state_allowed(sk)) 618 ret = -EOPNOTSUPP; 619 else if (map->map_type == BPF_MAP_TYPE_SOCKMAP) 620 ret = sock_map_update_common(map, *(u32 *)key, sk, flags); 621 else 622 ret = sock_hash_update_common(map, key, sk, flags); 623 bh_unlock_sock(sk); 624 local_bh_enable(); 625 return ret; 626 } 627 628 BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, sops, 629 struct bpf_map *, map, void *, key, u64, flags) 630 { 631 WARN_ON_ONCE(!rcu_read_lock_held()); 632 633 if (likely(sock_map_sk_is_suitable(sops->sk) && 634 sock_map_op_okay(sops))) 635 return sock_map_update_common(map, *(u32 *)key, sops->sk, 636 flags); 637 return -EOPNOTSUPP; 638 } 639 640 const struct bpf_func_proto bpf_sock_map_update_proto = { 641 .func = bpf_sock_map_update, 642 .gpl_only = false, 643 .pkt_access = true, 644 .ret_type = RET_INTEGER, 645 .arg1_type = ARG_PTR_TO_CTX, 646 .arg2_type = ARG_CONST_MAP_PTR, 647 .arg3_type = ARG_PTR_TO_MAP_KEY, 648 .arg4_type = ARG_ANYTHING, 649 }; 650 651 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb, 652 struct bpf_map *, map, u32, key, u64, flags) 653 { 654 struct sock *sk; 655 656 if (unlikely(flags & ~(BPF_F_INGRESS))) 657 return SK_DROP; 658 659 sk = __sock_map_lookup_elem(map, key); 660 if (unlikely(!sk || !sock_map_redirect_allowed(sk))) 661 return SK_DROP; 662 663 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS); 664 return SK_PASS; 665 } 666 667 const struct bpf_func_proto bpf_sk_redirect_map_proto = { 668 .func = bpf_sk_redirect_map, 669 .gpl_only = false, 670 .ret_type = RET_INTEGER, 671 .arg1_type = ARG_PTR_TO_CTX, 672 .arg2_type = ARG_CONST_MAP_PTR, 673 .arg3_type = ARG_ANYTHING, 674 .arg4_type = ARG_ANYTHING, 675 }; 676 677 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg, 678 struct bpf_map *, map, u32, key, u64, flags) 679 { 680 struct sock *sk; 681 682 if (unlikely(flags & ~(BPF_F_INGRESS))) 683 return SK_DROP; 684 685 sk = __sock_map_lookup_elem(map, key); 686 if (unlikely(!sk || !sock_map_redirect_allowed(sk))) 687 return SK_DROP; 688 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk)) 689 return SK_DROP; 690 691 msg->flags = flags; 692 msg->sk_redir = sk; 693 return SK_PASS; 694 } 695 696 const struct bpf_func_proto bpf_msg_redirect_map_proto = { 697 .func = bpf_msg_redirect_map, 698 .gpl_only = false, 699 .ret_type = RET_INTEGER, 700 .arg1_type = ARG_PTR_TO_CTX, 701 .arg2_type = ARG_CONST_MAP_PTR, 702 .arg3_type = ARG_ANYTHING, 703 .arg4_type = ARG_ANYTHING, 704 }; 705 706 struct sock_map_seq_info { 707 struct bpf_map *map; 708 struct sock *sk; 709 u32 index; 710 }; 711 712 struct bpf_iter__sockmap { 713 __bpf_md_ptr(struct bpf_iter_meta *, meta); 714 __bpf_md_ptr(struct bpf_map *, map); 715 __bpf_md_ptr(void *, key); 716 __bpf_md_ptr(struct sock *, sk); 717 }; 718 719 DEFINE_BPF_ITER_FUNC(sockmap, struct bpf_iter_meta *meta, 720 struct bpf_map *map, void *key, 721 struct sock *sk) 722 723 static void *sock_map_seq_lookup_elem(struct sock_map_seq_info *info) 724 { 725 if (unlikely(info->index >= info->map->max_entries)) 726 return NULL; 727 728 info->sk = __sock_map_lookup_elem(info->map, info->index); 729 730 /* can't return sk directly, since that might be NULL */ 731 return info; 732 } 733 734 static void *sock_map_seq_start(struct seq_file *seq, loff_t *pos) 735 __acquires(rcu) 736 { 737 struct sock_map_seq_info *info = seq->private; 738 739 if (*pos == 0) 740 ++*pos; 741 742 /* pairs with sock_map_seq_stop */ 743 rcu_read_lock(); 744 return sock_map_seq_lookup_elem(info); 745 } 746 747 static void *sock_map_seq_next(struct seq_file *seq, void *v, loff_t *pos) 748 __must_hold(rcu) 749 { 750 struct sock_map_seq_info *info = seq->private; 751 752 ++*pos; 753 ++info->index; 754 755 return sock_map_seq_lookup_elem(info); 756 } 757 758 static int sock_map_seq_show(struct seq_file *seq, void *v) 759 __must_hold(rcu) 760 { 761 struct sock_map_seq_info *info = seq->private; 762 struct bpf_iter__sockmap ctx = {}; 763 struct bpf_iter_meta meta; 764 struct bpf_prog *prog; 765 766 meta.seq = seq; 767 prog = bpf_iter_get_info(&meta, !v); 768 if (!prog) 769 return 0; 770 771 ctx.meta = &meta; 772 ctx.map = info->map; 773 if (v) { 774 ctx.key = &info->index; 775 ctx.sk = info->sk; 776 } 777 778 return bpf_iter_run_prog(prog, &ctx); 779 } 780 781 static void sock_map_seq_stop(struct seq_file *seq, void *v) 782 __releases(rcu) 783 { 784 if (!v) 785 (void)sock_map_seq_show(seq, NULL); 786 787 /* pairs with sock_map_seq_start */ 788 rcu_read_unlock(); 789 } 790 791 static const struct seq_operations sock_map_seq_ops = { 792 .start = sock_map_seq_start, 793 .next = sock_map_seq_next, 794 .stop = sock_map_seq_stop, 795 .show = sock_map_seq_show, 796 }; 797 798 static int sock_map_init_seq_private(void *priv_data, 799 struct bpf_iter_aux_info *aux) 800 { 801 struct sock_map_seq_info *info = priv_data; 802 803 bpf_map_inc_with_uref(aux->map); 804 info->map = aux->map; 805 return 0; 806 } 807 808 static void sock_map_fini_seq_private(void *priv_data) 809 { 810 struct sock_map_seq_info *info = priv_data; 811 812 bpf_map_put_with_uref(info->map); 813 } 814 815 static u64 sock_map_mem_usage(const struct bpf_map *map) 816 { 817 u64 usage = sizeof(struct bpf_stab); 818 819 usage += (u64)map->max_entries * sizeof(struct sock *); 820 return usage; 821 } 822 823 static const struct bpf_iter_seq_info sock_map_iter_seq_info = { 824 .seq_ops = &sock_map_seq_ops, 825 .init_seq_private = sock_map_init_seq_private, 826 .fini_seq_private = sock_map_fini_seq_private, 827 .seq_priv_size = sizeof(struct sock_map_seq_info), 828 }; 829 830 BTF_ID_LIST_SINGLE(sock_map_btf_ids, struct, bpf_stab) 831 const struct bpf_map_ops sock_map_ops = { 832 .map_meta_equal = bpf_map_meta_equal, 833 .map_alloc = sock_map_alloc, 834 .map_free = sock_map_free, 835 .map_get_next_key = sock_map_get_next_key, 836 .map_lookup_elem_sys_only = sock_map_lookup_sys, 837 .map_update_elem = sock_map_update_elem, 838 .map_delete_elem = sock_map_delete_elem, 839 .map_lookup_elem = sock_map_lookup, 840 .map_release_uref = sock_map_release_progs, 841 .map_check_btf = map_check_no_btf, 842 .map_mem_usage = sock_map_mem_usage, 843 .map_btf_id = &sock_map_btf_ids[0], 844 .iter_seq_info = &sock_map_iter_seq_info, 845 }; 846 847 struct bpf_shtab_elem { 848 struct rcu_head rcu; 849 u32 hash; 850 struct sock *sk; 851 struct hlist_node node; 852 u8 key[]; 853 }; 854 855 struct bpf_shtab_bucket { 856 struct hlist_head head; 857 spinlock_t lock; 858 }; 859 860 struct bpf_shtab { 861 struct bpf_map map; 862 struct bpf_shtab_bucket *buckets; 863 u32 buckets_num; 864 u32 elem_size; 865 struct sk_psock_progs progs; 866 atomic_t count; 867 }; 868 869 static inline u32 sock_hash_bucket_hash(const void *key, u32 len) 870 { 871 return jhash(key, len, 0); 872 } 873 874 static struct bpf_shtab_bucket *sock_hash_select_bucket(struct bpf_shtab *htab, 875 u32 hash) 876 { 877 return &htab->buckets[hash & (htab->buckets_num - 1)]; 878 } 879 880 static struct bpf_shtab_elem * 881 sock_hash_lookup_elem_raw(struct hlist_head *head, u32 hash, void *key, 882 u32 key_size) 883 { 884 struct bpf_shtab_elem *elem; 885 886 hlist_for_each_entry_rcu(elem, head, node) { 887 if (elem->hash == hash && 888 !memcmp(&elem->key, key, key_size)) 889 return elem; 890 } 891 892 return NULL; 893 } 894 895 static struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key) 896 { 897 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); 898 u32 key_size = map->key_size, hash; 899 struct bpf_shtab_bucket *bucket; 900 struct bpf_shtab_elem *elem; 901 902 WARN_ON_ONCE(!rcu_read_lock_held()); 903 904 hash = sock_hash_bucket_hash(key, key_size); 905 bucket = sock_hash_select_bucket(htab, hash); 906 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size); 907 908 return elem ? elem->sk : NULL; 909 } 910 911 static void sock_hash_free_elem(struct bpf_shtab *htab, 912 struct bpf_shtab_elem *elem) 913 { 914 atomic_dec(&htab->count); 915 kfree_rcu(elem, rcu); 916 } 917 918 static void sock_hash_delete_from_link(struct bpf_map *map, struct sock *sk, 919 void *link_raw) 920 { 921 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); 922 struct bpf_shtab_elem *elem_probe, *elem = link_raw; 923 struct bpf_shtab_bucket *bucket; 924 925 WARN_ON_ONCE(!rcu_read_lock_held()); 926 bucket = sock_hash_select_bucket(htab, elem->hash); 927 928 /* elem may be deleted in parallel from the map, but access here 929 * is okay since it's going away only after RCU grace period. 930 * However, we need to check whether it's still present. 931 */ 932 spin_lock_bh(&bucket->lock); 933 elem_probe = sock_hash_lookup_elem_raw(&bucket->head, elem->hash, 934 elem->key, map->key_size); 935 if (elem_probe && elem_probe == elem) { 936 hlist_del_rcu(&elem->node); 937 sock_map_unref(elem->sk, elem); 938 sock_hash_free_elem(htab, elem); 939 } 940 spin_unlock_bh(&bucket->lock); 941 } 942 943 static long sock_hash_delete_elem(struct bpf_map *map, void *key) 944 { 945 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); 946 u32 hash, key_size = map->key_size; 947 struct bpf_shtab_bucket *bucket; 948 struct bpf_shtab_elem *elem; 949 int ret = -ENOENT; 950 951 if (irqs_disabled()) 952 return -EOPNOTSUPP; /* locks here are hardirq-unsafe */ 953 954 hash = sock_hash_bucket_hash(key, key_size); 955 bucket = sock_hash_select_bucket(htab, hash); 956 957 spin_lock_bh(&bucket->lock); 958 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size); 959 if (elem) { 960 hlist_del_rcu(&elem->node); 961 sock_map_unref(elem->sk, elem); 962 sock_hash_free_elem(htab, elem); 963 ret = 0; 964 } 965 spin_unlock_bh(&bucket->lock); 966 return ret; 967 } 968 969 static struct bpf_shtab_elem *sock_hash_alloc_elem(struct bpf_shtab *htab, 970 void *key, u32 key_size, 971 u32 hash, struct sock *sk, 972 struct bpf_shtab_elem *old) 973 { 974 struct bpf_shtab_elem *new; 975 976 if (atomic_inc_return(&htab->count) > htab->map.max_entries) { 977 if (!old) { 978 atomic_dec(&htab->count); 979 return ERR_PTR(-E2BIG); 980 } 981 } 982 983 new = bpf_map_kmalloc_node(&htab->map, htab->elem_size, 984 GFP_ATOMIC | __GFP_NOWARN, 985 htab->map.numa_node); 986 if (!new) { 987 atomic_dec(&htab->count); 988 return ERR_PTR(-ENOMEM); 989 } 990 memcpy(new->key, key, key_size); 991 new->sk = sk; 992 new->hash = hash; 993 return new; 994 } 995 996 static int sock_hash_update_common(struct bpf_map *map, void *key, 997 struct sock *sk, u64 flags) 998 { 999 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); 1000 u32 key_size = map->key_size, hash; 1001 struct bpf_shtab_elem *elem, *elem_new; 1002 struct bpf_shtab_bucket *bucket; 1003 struct sk_psock_link *link; 1004 struct sk_psock *psock; 1005 int ret; 1006 1007 WARN_ON_ONCE(!rcu_read_lock_held()); 1008 if (unlikely(flags > BPF_EXIST)) 1009 return -EINVAL; 1010 1011 link = sk_psock_init_link(); 1012 if (!link) 1013 return -ENOMEM; 1014 1015 ret = sock_map_link(map, sk); 1016 if (ret < 0) 1017 goto out_free; 1018 1019 psock = sk_psock(sk); 1020 WARN_ON_ONCE(!psock); 1021 1022 hash = sock_hash_bucket_hash(key, key_size); 1023 bucket = sock_hash_select_bucket(htab, hash); 1024 1025 spin_lock_bh(&bucket->lock); 1026 elem = sock_hash_lookup_elem_raw(&bucket->head, hash, key, key_size); 1027 if (elem && flags == BPF_NOEXIST) { 1028 ret = -EEXIST; 1029 goto out_unlock; 1030 } else if (!elem && flags == BPF_EXIST) { 1031 ret = -ENOENT; 1032 goto out_unlock; 1033 } 1034 1035 elem_new = sock_hash_alloc_elem(htab, key, key_size, hash, sk, elem); 1036 if (IS_ERR(elem_new)) { 1037 ret = PTR_ERR(elem_new); 1038 goto out_unlock; 1039 } 1040 1041 sock_map_add_link(psock, link, map, elem_new); 1042 /* Add new element to the head of the list, so that 1043 * concurrent search will find it before old elem. 1044 */ 1045 hlist_add_head_rcu(&elem_new->node, &bucket->head); 1046 if (elem) { 1047 hlist_del_rcu(&elem->node); 1048 sock_map_unref(elem->sk, elem); 1049 sock_hash_free_elem(htab, elem); 1050 } 1051 spin_unlock_bh(&bucket->lock); 1052 return 0; 1053 out_unlock: 1054 spin_unlock_bh(&bucket->lock); 1055 sk_psock_put(sk, psock); 1056 out_free: 1057 sk_psock_free_link(link); 1058 return ret; 1059 } 1060 1061 static int sock_hash_get_next_key(struct bpf_map *map, void *key, 1062 void *key_next) 1063 { 1064 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); 1065 struct bpf_shtab_elem *elem, *elem_next; 1066 u32 hash, key_size = map->key_size; 1067 struct hlist_head *head; 1068 int i = 0; 1069 1070 if (!key) 1071 goto find_first_elem; 1072 hash = sock_hash_bucket_hash(key, key_size); 1073 head = &sock_hash_select_bucket(htab, hash)->head; 1074 elem = sock_hash_lookup_elem_raw(head, hash, key, key_size); 1075 if (!elem) 1076 goto find_first_elem; 1077 1078 elem_next = hlist_entry_safe(rcu_dereference(hlist_next_rcu(&elem->node)), 1079 struct bpf_shtab_elem, node); 1080 if (elem_next) { 1081 memcpy(key_next, elem_next->key, key_size); 1082 return 0; 1083 } 1084 1085 i = hash & (htab->buckets_num - 1); 1086 i++; 1087 find_first_elem: 1088 for (; i < htab->buckets_num; i++) { 1089 head = &sock_hash_select_bucket(htab, i)->head; 1090 elem_next = hlist_entry_safe(rcu_dereference(hlist_first_rcu(head)), 1091 struct bpf_shtab_elem, node); 1092 if (elem_next) { 1093 memcpy(key_next, elem_next->key, key_size); 1094 return 0; 1095 } 1096 } 1097 1098 return -ENOENT; 1099 } 1100 1101 static struct bpf_map *sock_hash_alloc(union bpf_attr *attr) 1102 { 1103 struct bpf_shtab *htab; 1104 int i, err; 1105 1106 if (attr->max_entries == 0 || 1107 attr->key_size == 0 || 1108 (attr->value_size != sizeof(u32) && 1109 attr->value_size != sizeof(u64)) || 1110 attr->map_flags & ~SOCK_CREATE_FLAG_MASK) 1111 return ERR_PTR(-EINVAL); 1112 if (attr->key_size > MAX_BPF_STACK) 1113 return ERR_PTR(-E2BIG); 1114 1115 htab = bpf_map_area_alloc(sizeof(*htab), NUMA_NO_NODE); 1116 if (!htab) 1117 return ERR_PTR(-ENOMEM); 1118 1119 bpf_map_init_from_attr(&htab->map, attr); 1120 1121 htab->buckets_num = roundup_pow_of_two(htab->map.max_entries); 1122 htab->elem_size = sizeof(struct bpf_shtab_elem) + 1123 round_up(htab->map.key_size, 8); 1124 if (htab->buckets_num == 0 || 1125 htab->buckets_num > U32_MAX / sizeof(struct bpf_shtab_bucket)) { 1126 err = -EINVAL; 1127 goto free_htab; 1128 } 1129 1130 htab->buckets = bpf_map_area_alloc(htab->buckets_num * 1131 sizeof(struct bpf_shtab_bucket), 1132 htab->map.numa_node); 1133 if (!htab->buckets) { 1134 err = -ENOMEM; 1135 goto free_htab; 1136 } 1137 1138 for (i = 0; i < htab->buckets_num; i++) { 1139 INIT_HLIST_HEAD(&htab->buckets[i].head); 1140 spin_lock_init(&htab->buckets[i].lock); 1141 } 1142 1143 return &htab->map; 1144 free_htab: 1145 bpf_map_area_free(htab); 1146 return ERR_PTR(err); 1147 } 1148 1149 static void sock_hash_free(struct bpf_map *map) 1150 { 1151 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); 1152 struct bpf_shtab_bucket *bucket; 1153 struct hlist_head unlink_list; 1154 struct bpf_shtab_elem *elem; 1155 struct hlist_node *node; 1156 int i; 1157 1158 /* After the sync no updates or deletes will be in-flight so it 1159 * is safe to walk map and remove entries without risking a race 1160 * in EEXIST update case. 1161 */ 1162 synchronize_rcu(); 1163 for (i = 0; i < htab->buckets_num; i++) { 1164 bucket = sock_hash_select_bucket(htab, i); 1165 1166 /* We are racing with sock_hash_delete_from_link to 1167 * enter the spin-lock critical section. Every socket on 1168 * the list is still linked to sockhash. Since link 1169 * exists, psock exists and holds a ref to socket. That 1170 * lets us to grab a socket ref too. 1171 */ 1172 spin_lock_bh(&bucket->lock); 1173 hlist_for_each_entry(elem, &bucket->head, node) 1174 sock_hold(elem->sk); 1175 hlist_move_list(&bucket->head, &unlink_list); 1176 spin_unlock_bh(&bucket->lock); 1177 1178 /* Process removed entries out of atomic context to 1179 * block for socket lock before deleting the psock's 1180 * link to sockhash. 1181 */ 1182 hlist_for_each_entry_safe(elem, node, &unlink_list, node) { 1183 hlist_del(&elem->node); 1184 lock_sock(elem->sk); 1185 rcu_read_lock(); 1186 sock_map_unref(elem->sk, elem); 1187 rcu_read_unlock(); 1188 release_sock(elem->sk); 1189 sock_put(elem->sk); 1190 sock_hash_free_elem(htab, elem); 1191 } 1192 } 1193 1194 /* wait for psock readers accessing its map link */ 1195 synchronize_rcu(); 1196 1197 bpf_map_area_free(htab->buckets); 1198 bpf_map_area_free(htab); 1199 } 1200 1201 static void *sock_hash_lookup_sys(struct bpf_map *map, void *key) 1202 { 1203 struct sock *sk; 1204 1205 if (map->value_size != sizeof(u64)) 1206 return ERR_PTR(-ENOSPC); 1207 1208 sk = __sock_hash_lookup_elem(map, key); 1209 if (!sk) 1210 return ERR_PTR(-ENOENT); 1211 1212 __sock_gen_cookie(sk); 1213 return &sk->sk_cookie; 1214 } 1215 1216 static void *sock_hash_lookup(struct bpf_map *map, void *key) 1217 { 1218 struct sock *sk; 1219 1220 sk = __sock_hash_lookup_elem(map, key); 1221 if (!sk) 1222 return NULL; 1223 if (sk_is_refcounted(sk) && !refcount_inc_not_zero(&sk->sk_refcnt)) 1224 return NULL; 1225 return sk; 1226 } 1227 1228 static void sock_hash_release_progs(struct bpf_map *map) 1229 { 1230 psock_progs_drop(&container_of(map, struct bpf_shtab, map)->progs); 1231 } 1232 1233 BPF_CALL_4(bpf_sock_hash_update, struct bpf_sock_ops_kern *, sops, 1234 struct bpf_map *, map, void *, key, u64, flags) 1235 { 1236 WARN_ON_ONCE(!rcu_read_lock_held()); 1237 1238 if (likely(sock_map_sk_is_suitable(sops->sk) && 1239 sock_map_op_okay(sops))) 1240 return sock_hash_update_common(map, key, sops->sk, flags); 1241 return -EOPNOTSUPP; 1242 } 1243 1244 const struct bpf_func_proto bpf_sock_hash_update_proto = { 1245 .func = bpf_sock_hash_update, 1246 .gpl_only = false, 1247 .pkt_access = true, 1248 .ret_type = RET_INTEGER, 1249 .arg1_type = ARG_PTR_TO_CTX, 1250 .arg2_type = ARG_CONST_MAP_PTR, 1251 .arg3_type = ARG_PTR_TO_MAP_KEY, 1252 .arg4_type = ARG_ANYTHING, 1253 }; 1254 1255 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb, 1256 struct bpf_map *, map, void *, key, u64, flags) 1257 { 1258 struct sock *sk; 1259 1260 if (unlikely(flags & ~(BPF_F_INGRESS))) 1261 return SK_DROP; 1262 1263 sk = __sock_hash_lookup_elem(map, key); 1264 if (unlikely(!sk || !sock_map_redirect_allowed(sk))) 1265 return SK_DROP; 1266 1267 skb_bpf_set_redir(skb, sk, flags & BPF_F_INGRESS); 1268 return SK_PASS; 1269 } 1270 1271 const struct bpf_func_proto bpf_sk_redirect_hash_proto = { 1272 .func = bpf_sk_redirect_hash, 1273 .gpl_only = false, 1274 .ret_type = RET_INTEGER, 1275 .arg1_type = ARG_PTR_TO_CTX, 1276 .arg2_type = ARG_CONST_MAP_PTR, 1277 .arg3_type = ARG_PTR_TO_MAP_KEY, 1278 .arg4_type = ARG_ANYTHING, 1279 }; 1280 1281 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg, 1282 struct bpf_map *, map, void *, key, u64, flags) 1283 { 1284 struct sock *sk; 1285 1286 if (unlikely(flags & ~(BPF_F_INGRESS))) 1287 return SK_DROP; 1288 1289 sk = __sock_hash_lookup_elem(map, key); 1290 if (unlikely(!sk || !sock_map_redirect_allowed(sk))) 1291 return SK_DROP; 1292 if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk)) 1293 return SK_DROP; 1294 1295 msg->flags = flags; 1296 msg->sk_redir = sk; 1297 return SK_PASS; 1298 } 1299 1300 const struct bpf_func_proto bpf_msg_redirect_hash_proto = { 1301 .func = bpf_msg_redirect_hash, 1302 .gpl_only = false, 1303 .ret_type = RET_INTEGER, 1304 .arg1_type = ARG_PTR_TO_CTX, 1305 .arg2_type = ARG_CONST_MAP_PTR, 1306 .arg3_type = ARG_PTR_TO_MAP_KEY, 1307 .arg4_type = ARG_ANYTHING, 1308 }; 1309 1310 struct sock_hash_seq_info { 1311 struct bpf_map *map; 1312 struct bpf_shtab *htab; 1313 u32 bucket_id; 1314 }; 1315 1316 static void *sock_hash_seq_find_next(struct sock_hash_seq_info *info, 1317 struct bpf_shtab_elem *prev_elem) 1318 { 1319 const struct bpf_shtab *htab = info->htab; 1320 struct bpf_shtab_bucket *bucket; 1321 struct bpf_shtab_elem *elem; 1322 struct hlist_node *node; 1323 1324 /* try to find next elem in the same bucket */ 1325 if (prev_elem) { 1326 node = rcu_dereference(hlist_next_rcu(&prev_elem->node)); 1327 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node); 1328 if (elem) 1329 return elem; 1330 1331 /* no more elements, continue in the next bucket */ 1332 info->bucket_id++; 1333 } 1334 1335 for (; info->bucket_id < htab->buckets_num; info->bucket_id++) { 1336 bucket = &htab->buckets[info->bucket_id]; 1337 node = rcu_dereference(hlist_first_rcu(&bucket->head)); 1338 elem = hlist_entry_safe(node, struct bpf_shtab_elem, node); 1339 if (elem) 1340 return elem; 1341 } 1342 1343 return NULL; 1344 } 1345 1346 static void *sock_hash_seq_start(struct seq_file *seq, loff_t *pos) 1347 __acquires(rcu) 1348 { 1349 struct sock_hash_seq_info *info = seq->private; 1350 1351 if (*pos == 0) 1352 ++*pos; 1353 1354 /* pairs with sock_hash_seq_stop */ 1355 rcu_read_lock(); 1356 return sock_hash_seq_find_next(info, NULL); 1357 } 1358 1359 static void *sock_hash_seq_next(struct seq_file *seq, void *v, loff_t *pos) 1360 __must_hold(rcu) 1361 { 1362 struct sock_hash_seq_info *info = seq->private; 1363 1364 ++*pos; 1365 return sock_hash_seq_find_next(info, v); 1366 } 1367 1368 static int sock_hash_seq_show(struct seq_file *seq, void *v) 1369 __must_hold(rcu) 1370 { 1371 struct sock_hash_seq_info *info = seq->private; 1372 struct bpf_iter__sockmap ctx = {}; 1373 struct bpf_shtab_elem *elem = v; 1374 struct bpf_iter_meta meta; 1375 struct bpf_prog *prog; 1376 1377 meta.seq = seq; 1378 prog = bpf_iter_get_info(&meta, !elem); 1379 if (!prog) 1380 return 0; 1381 1382 ctx.meta = &meta; 1383 ctx.map = info->map; 1384 if (elem) { 1385 ctx.key = elem->key; 1386 ctx.sk = elem->sk; 1387 } 1388 1389 return bpf_iter_run_prog(prog, &ctx); 1390 } 1391 1392 static void sock_hash_seq_stop(struct seq_file *seq, void *v) 1393 __releases(rcu) 1394 { 1395 if (!v) 1396 (void)sock_hash_seq_show(seq, NULL); 1397 1398 /* pairs with sock_hash_seq_start */ 1399 rcu_read_unlock(); 1400 } 1401 1402 static const struct seq_operations sock_hash_seq_ops = { 1403 .start = sock_hash_seq_start, 1404 .next = sock_hash_seq_next, 1405 .stop = sock_hash_seq_stop, 1406 .show = sock_hash_seq_show, 1407 }; 1408 1409 static int sock_hash_init_seq_private(void *priv_data, 1410 struct bpf_iter_aux_info *aux) 1411 { 1412 struct sock_hash_seq_info *info = priv_data; 1413 1414 bpf_map_inc_with_uref(aux->map); 1415 info->map = aux->map; 1416 info->htab = container_of(aux->map, struct bpf_shtab, map); 1417 return 0; 1418 } 1419 1420 static void sock_hash_fini_seq_private(void *priv_data) 1421 { 1422 struct sock_hash_seq_info *info = priv_data; 1423 1424 bpf_map_put_with_uref(info->map); 1425 } 1426 1427 static u64 sock_hash_mem_usage(const struct bpf_map *map) 1428 { 1429 struct bpf_shtab *htab = container_of(map, struct bpf_shtab, map); 1430 u64 usage = sizeof(*htab); 1431 1432 usage += htab->buckets_num * sizeof(struct bpf_shtab_bucket); 1433 usage += atomic_read(&htab->count) * (u64)htab->elem_size; 1434 return usage; 1435 } 1436 1437 static const struct bpf_iter_seq_info sock_hash_iter_seq_info = { 1438 .seq_ops = &sock_hash_seq_ops, 1439 .init_seq_private = sock_hash_init_seq_private, 1440 .fini_seq_private = sock_hash_fini_seq_private, 1441 .seq_priv_size = sizeof(struct sock_hash_seq_info), 1442 }; 1443 1444 BTF_ID_LIST_SINGLE(sock_hash_map_btf_ids, struct, bpf_shtab) 1445 const struct bpf_map_ops sock_hash_ops = { 1446 .map_meta_equal = bpf_map_meta_equal, 1447 .map_alloc = sock_hash_alloc, 1448 .map_free = sock_hash_free, 1449 .map_get_next_key = sock_hash_get_next_key, 1450 .map_update_elem = sock_map_update_elem, 1451 .map_delete_elem = sock_hash_delete_elem, 1452 .map_lookup_elem = sock_hash_lookup, 1453 .map_lookup_elem_sys_only = sock_hash_lookup_sys, 1454 .map_release_uref = sock_hash_release_progs, 1455 .map_check_btf = map_check_no_btf, 1456 .map_mem_usage = sock_hash_mem_usage, 1457 .map_btf_id = &sock_hash_map_btf_ids[0], 1458 .iter_seq_info = &sock_hash_iter_seq_info, 1459 }; 1460 1461 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map) 1462 { 1463 switch (map->map_type) { 1464 case BPF_MAP_TYPE_SOCKMAP: 1465 return &container_of(map, struct bpf_stab, map)->progs; 1466 case BPF_MAP_TYPE_SOCKHASH: 1467 return &container_of(map, struct bpf_shtab, map)->progs; 1468 default: 1469 break; 1470 } 1471 1472 return NULL; 1473 } 1474 1475 static int sock_map_prog_link_lookup(struct bpf_map *map, struct bpf_prog ***pprog, 1476 struct bpf_link ***plink, u32 which) 1477 { 1478 struct sk_psock_progs *progs = sock_map_progs(map); 1479 struct bpf_prog **cur_pprog; 1480 struct bpf_link **cur_plink; 1481 1482 if (!progs) 1483 return -EOPNOTSUPP; 1484 1485 switch (which) { 1486 case BPF_SK_MSG_VERDICT: 1487 cur_pprog = &progs->msg_parser; 1488 cur_plink = &progs->msg_parser_link; 1489 break; 1490 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER) 1491 case BPF_SK_SKB_STREAM_PARSER: 1492 cur_pprog = &progs->stream_parser; 1493 cur_plink = &progs->stream_parser_link; 1494 break; 1495 #endif 1496 case BPF_SK_SKB_STREAM_VERDICT: 1497 if (progs->skb_verdict) 1498 return -EBUSY; 1499 cur_pprog = &progs->stream_verdict; 1500 cur_plink = &progs->stream_verdict_link; 1501 break; 1502 case BPF_SK_SKB_VERDICT: 1503 if (progs->stream_verdict) 1504 return -EBUSY; 1505 cur_pprog = &progs->skb_verdict; 1506 cur_plink = &progs->skb_verdict_link; 1507 break; 1508 default: 1509 return -EOPNOTSUPP; 1510 } 1511 1512 *pprog = cur_pprog; 1513 if (plink) 1514 *plink = cur_plink; 1515 return 0; 1516 } 1517 1518 /* Handle the following four cases: 1519 * prog_attach: prog != NULL, old == NULL, link == NULL 1520 * prog_detach: prog == NULL, old != NULL, link == NULL 1521 * link_attach: prog != NULL, old == NULL, link != NULL 1522 * link_detach: prog == NULL, old != NULL, link != NULL 1523 */ 1524 static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, 1525 struct bpf_prog *old, struct bpf_link *link, 1526 u32 which) 1527 { 1528 struct bpf_prog **pprog; 1529 struct bpf_link **plink; 1530 int ret; 1531 1532 ret = sock_map_prog_link_lookup(map, &pprog, &plink, which); 1533 if (ret) 1534 return ret; 1535 1536 /* for prog_attach/prog_detach/link_attach, return error if a bpf_link 1537 * exists for that prog. 1538 */ 1539 if ((!link || prog) && *plink) 1540 return -EBUSY; 1541 1542 if (old) { 1543 ret = psock_replace_prog(pprog, prog, old); 1544 if (!ret) 1545 *plink = NULL; 1546 } else { 1547 psock_set_prog(pprog, prog); 1548 if (link) 1549 *plink = link; 1550 } 1551 1552 return ret; 1553 } 1554 1555 int sock_map_bpf_prog_query(const union bpf_attr *attr, 1556 union bpf_attr __user *uattr) 1557 { 1558 __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids); 1559 u32 prog_cnt = 0, flags = 0, ufd = attr->target_fd; 1560 struct bpf_prog **pprog; 1561 struct bpf_prog *prog; 1562 struct bpf_map *map; 1563 struct fd f; 1564 u32 id = 0; 1565 int ret; 1566 1567 if (attr->query.query_flags) 1568 return -EINVAL; 1569 1570 f = fdget(ufd); 1571 map = __bpf_map_get(f); 1572 if (IS_ERR(map)) 1573 return PTR_ERR(map); 1574 1575 rcu_read_lock(); 1576 1577 ret = sock_map_prog_link_lookup(map, &pprog, NULL, attr->query.attach_type); 1578 if (ret) 1579 goto end; 1580 1581 prog = *pprog; 1582 prog_cnt = !prog ? 0 : 1; 1583 1584 if (!attr->query.prog_cnt || !prog_ids || !prog_cnt) 1585 goto end; 1586 1587 /* we do not hold the refcnt, the bpf prog may be released 1588 * asynchronously and the id would be set to 0. 1589 */ 1590 id = data_race(prog->aux->id); 1591 if (id == 0) 1592 prog_cnt = 0; 1593 1594 end: 1595 rcu_read_unlock(); 1596 1597 if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)) || 1598 (id != 0 && copy_to_user(prog_ids, &id, sizeof(u32))) || 1599 copy_to_user(&uattr->query.prog_cnt, &prog_cnt, sizeof(prog_cnt))) 1600 ret = -EFAULT; 1601 1602 fdput(f); 1603 return ret; 1604 } 1605 1606 static void sock_map_unlink(struct sock *sk, struct sk_psock_link *link) 1607 { 1608 switch (link->map->map_type) { 1609 case BPF_MAP_TYPE_SOCKMAP: 1610 return sock_map_delete_from_link(link->map, sk, 1611 link->link_raw); 1612 case BPF_MAP_TYPE_SOCKHASH: 1613 return sock_hash_delete_from_link(link->map, sk, 1614 link->link_raw); 1615 default: 1616 break; 1617 } 1618 } 1619 1620 static void sock_map_remove_links(struct sock *sk, struct sk_psock *psock) 1621 { 1622 struct sk_psock_link *link; 1623 1624 while ((link = sk_psock_link_pop(psock))) { 1625 sock_map_unlink(sk, link); 1626 sk_psock_free_link(link); 1627 } 1628 } 1629 1630 void sock_map_unhash(struct sock *sk) 1631 { 1632 void (*saved_unhash)(struct sock *sk); 1633 struct sk_psock *psock; 1634 1635 rcu_read_lock(); 1636 psock = sk_psock(sk); 1637 if (unlikely(!psock)) { 1638 rcu_read_unlock(); 1639 saved_unhash = READ_ONCE(sk->sk_prot)->unhash; 1640 } else { 1641 saved_unhash = psock->saved_unhash; 1642 sock_map_remove_links(sk, psock); 1643 rcu_read_unlock(); 1644 } 1645 if (WARN_ON_ONCE(saved_unhash == sock_map_unhash)) 1646 return; 1647 if (saved_unhash) 1648 saved_unhash(sk); 1649 } 1650 EXPORT_SYMBOL_GPL(sock_map_unhash); 1651 1652 void sock_map_destroy(struct sock *sk) 1653 { 1654 void (*saved_destroy)(struct sock *sk); 1655 struct sk_psock *psock; 1656 1657 rcu_read_lock(); 1658 psock = sk_psock_get(sk); 1659 if (unlikely(!psock)) { 1660 rcu_read_unlock(); 1661 saved_destroy = READ_ONCE(sk->sk_prot)->destroy; 1662 } else { 1663 saved_destroy = psock->saved_destroy; 1664 sock_map_remove_links(sk, psock); 1665 rcu_read_unlock(); 1666 sk_psock_stop(psock); 1667 sk_psock_put(sk, psock); 1668 } 1669 if (WARN_ON_ONCE(saved_destroy == sock_map_destroy)) 1670 return; 1671 if (saved_destroy) 1672 saved_destroy(sk); 1673 } 1674 EXPORT_SYMBOL_GPL(sock_map_destroy); 1675 1676 void sock_map_close(struct sock *sk, long timeout) 1677 { 1678 void (*saved_close)(struct sock *sk, long timeout); 1679 struct sk_psock *psock; 1680 1681 lock_sock(sk); 1682 rcu_read_lock(); 1683 psock = sk_psock_get(sk); 1684 if (unlikely(!psock)) { 1685 rcu_read_unlock(); 1686 release_sock(sk); 1687 saved_close = READ_ONCE(sk->sk_prot)->close; 1688 } else { 1689 saved_close = psock->saved_close; 1690 sock_map_remove_links(sk, psock); 1691 rcu_read_unlock(); 1692 sk_psock_stop(psock); 1693 release_sock(sk); 1694 cancel_delayed_work_sync(&psock->work); 1695 sk_psock_put(sk, psock); 1696 } 1697 1698 /* Make sure we do not recurse. This is a bug. 1699 * Leak the socket instead of crashing on a stack overflow. 1700 */ 1701 if (WARN_ON_ONCE(saved_close == sock_map_close)) 1702 return; 1703 saved_close(sk, timeout); 1704 } 1705 EXPORT_SYMBOL_GPL(sock_map_close); 1706 1707 struct sockmap_link { 1708 struct bpf_link link; 1709 struct bpf_map *map; 1710 enum bpf_attach_type attach_type; 1711 }; 1712 1713 static void sock_map_link_release(struct bpf_link *link) 1714 { 1715 struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link); 1716 1717 mutex_lock(&sockmap_mutex); 1718 if (!sockmap_link->map) 1719 goto out; 1720 1721 WARN_ON_ONCE(sock_map_prog_update(sockmap_link->map, NULL, link->prog, link, 1722 sockmap_link->attach_type)); 1723 1724 bpf_map_put_with_uref(sockmap_link->map); 1725 sockmap_link->map = NULL; 1726 out: 1727 mutex_unlock(&sockmap_mutex); 1728 } 1729 1730 static int sock_map_link_detach(struct bpf_link *link) 1731 { 1732 sock_map_link_release(link); 1733 return 0; 1734 } 1735 1736 static void sock_map_link_dealloc(struct bpf_link *link) 1737 { 1738 kfree(link); 1739 } 1740 1741 /* Handle the following two cases: 1742 * case 1: link != NULL, prog != NULL, old != NULL 1743 * case 2: link != NULL, prog != NULL, old == NULL 1744 */ 1745 static int sock_map_link_update_prog(struct bpf_link *link, 1746 struct bpf_prog *prog, 1747 struct bpf_prog *old) 1748 { 1749 const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link); 1750 struct bpf_prog **pprog, *old_link_prog; 1751 struct bpf_link **plink; 1752 int ret = 0; 1753 1754 mutex_lock(&sockmap_mutex); 1755 1756 /* If old prog is not NULL, ensure old prog is the same as link->prog. */ 1757 if (old && link->prog != old) { 1758 ret = -EPERM; 1759 goto out; 1760 } 1761 /* Ensure link->prog has the same type/attach_type as the new prog. */ 1762 if (link->prog->type != prog->type || 1763 link->prog->expected_attach_type != prog->expected_attach_type) { 1764 ret = -EINVAL; 1765 goto out; 1766 } 1767 1768 ret = sock_map_prog_link_lookup(sockmap_link->map, &pprog, &plink, 1769 sockmap_link->attach_type); 1770 if (ret) 1771 goto out; 1772 1773 /* return error if the stored bpf_link does not match the incoming bpf_link. */ 1774 if (link != *plink) { 1775 ret = -EBUSY; 1776 goto out; 1777 } 1778 1779 if (old) { 1780 ret = psock_replace_prog(pprog, prog, old); 1781 if (ret) 1782 goto out; 1783 } else { 1784 psock_set_prog(pprog, prog); 1785 } 1786 1787 bpf_prog_inc(prog); 1788 old_link_prog = xchg(&link->prog, prog); 1789 bpf_prog_put(old_link_prog); 1790 1791 out: 1792 mutex_unlock(&sockmap_mutex); 1793 return ret; 1794 } 1795 1796 static u32 sock_map_link_get_map_id(const struct sockmap_link *sockmap_link) 1797 { 1798 u32 map_id = 0; 1799 1800 mutex_lock(&sockmap_mutex); 1801 if (sockmap_link->map) 1802 map_id = sockmap_link->map->id; 1803 mutex_unlock(&sockmap_mutex); 1804 return map_id; 1805 } 1806 1807 static int sock_map_link_fill_info(const struct bpf_link *link, 1808 struct bpf_link_info *info) 1809 { 1810 const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link); 1811 u32 map_id = sock_map_link_get_map_id(sockmap_link); 1812 1813 info->sockmap.map_id = map_id; 1814 info->sockmap.attach_type = sockmap_link->attach_type; 1815 return 0; 1816 } 1817 1818 static void sock_map_link_show_fdinfo(const struct bpf_link *link, 1819 struct seq_file *seq) 1820 { 1821 const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link); 1822 u32 map_id = sock_map_link_get_map_id(sockmap_link); 1823 1824 seq_printf(seq, "map_id:\t%u\n", map_id); 1825 seq_printf(seq, "attach_type:\t%u\n", sockmap_link->attach_type); 1826 } 1827 1828 static const struct bpf_link_ops sock_map_link_ops = { 1829 .release = sock_map_link_release, 1830 .dealloc = sock_map_link_dealloc, 1831 .detach = sock_map_link_detach, 1832 .update_prog = sock_map_link_update_prog, 1833 .fill_link_info = sock_map_link_fill_info, 1834 .show_fdinfo = sock_map_link_show_fdinfo, 1835 }; 1836 1837 int sock_map_link_create(const union bpf_attr *attr, struct bpf_prog *prog) 1838 { 1839 struct bpf_link_primer link_primer; 1840 struct sockmap_link *sockmap_link; 1841 enum bpf_attach_type attach_type; 1842 struct bpf_map *map; 1843 int ret; 1844 1845 if (attr->link_create.flags) 1846 return -EINVAL; 1847 1848 map = bpf_map_get_with_uref(attr->link_create.target_fd); 1849 if (IS_ERR(map)) 1850 return PTR_ERR(map); 1851 if (map->map_type != BPF_MAP_TYPE_SOCKMAP && map->map_type != BPF_MAP_TYPE_SOCKHASH) { 1852 ret = -EINVAL; 1853 goto out; 1854 } 1855 1856 sockmap_link = kzalloc(sizeof(*sockmap_link), GFP_USER); 1857 if (!sockmap_link) { 1858 ret = -ENOMEM; 1859 goto out; 1860 } 1861 1862 attach_type = attr->link_create.attach_type; 1863 bpf_link_init(&sockmap_link->link, BPF_LINK_TYPE_SOCKMAP, &sock_map_link_ops, prog); 1864 sockmap_link->map = map; 1865 sockmap_link->attach_type = attach_type; 1866 1867 ret = bpf_link_prime(&sockmap_link->link, &link_primer); 1868 if (ret) { 1869 kfree(sockmap_link); 1870 goto out; 1871 } 1872 1873 mutex_lock(&sockmap_mutex); 1874 ret = sock_map_prog_update(map, prog, NULL, &sockmap_link->link, attach_type); 1875 mutex_unlock(&sockmap_mutex); 1876 if (ret) { 1877 bpf_link_cleanup(&link_primer); 1878 goto out; 1879 } 1880 1881 /* Increase refcnt for the prog since when old prog is replaced with 1882 * psock_replace_prog() and psock_set_prog() its refcnt will be decreased. 1883 * 1884 * Actually, we do not need to increase refcnt for the prog since bpf_link 1885 * will hold a reference. But in order to have less complexity w.r.t. 1886 * replacing/setting prog, let us increase the refcnt to make things simpler. 1887 */ 1888 bpf_prog_inc(prog); 1889 1890 return bpf_link_settle(&link_primer); 1891 1892 out: 1893 bpf_map_put_with_uref(map); 1894 return ret; 1895 } 1896 1897 static int sock_map_iter_attach_target(struct bpf_prog *prog, 1898 union bpf_iter_link_info *linfo, 1899 struct bpf_iter_aux_info *aux) 1900 { 1901 struct bpf_map *map; 1902 int err = -EINVAL; 1903 1904 if (!linfo->map.map_fd) 1905 return -EBADF; 1906 1907 map = bpf_map_get_with_uref(linfo->map.map_fd); 1908 if (IS_ERR(map)) 1909 return PTR_ERR(map); 1910 1911 if (map->map_type != BPF_MAP_TYPE_SOCKMAP && 1912 map->map_type != BPF_MAP_TYPE_SOCKHASH) 1913 goto put_map; 1914 1915 if (prog->aux->max_rdonly_access > map->key_size) { 1916 err = -EACCES; 1917 goto put_map; 1918 } 1919 1920 aux->map = map; 1921 return 0; 1922 1923 put_map: 1924 bpf_map_put_with_uref(map); 1925 return err; 1926 } 1927 1928 static void sock_map_iter_detach_target(struct bpf_iter_aux_info *aux) 1929 { 1930 bpf_map_put_with_uref(aux->map); 1931 } 1932 1933 static struct bpf_iter_reg sock_map_iter_reg = { 1934 .target = "sockmap", 1935 .attach_target = sock_map_iter_attach_target, 1936 .detach_target = sock_map_iter_detach_target, 1937 .show_fdinfo = bpf_iter_map_show_fdinfo, 1938 .fill_link_info = bpf_iter_map_fill_link_info, 1939 .ctx_arg_info_size = 2, 1940 .ctx_arg_info = { 1941 { offsetof(struct bpf_iter__sockmap, key), 1942 PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY }, 1943 { offsetof(struct bpf_iter__sockmap, sk), 1944 PTR_TO_BTF_ID_OR_NULL }, 1945 }, 1946 }; 1947 1948 static int __init bpf_sockmap_iter_init(void) 1949 { 1950 sock_map_iter_reg.ctx_arg_info[1].btf_id = 1951 btf_sock_ids[BTF_SOCK_TYPE_SOCK]; 1952 return bpf_iter_reg_target(&sock_map_iter_reg); 1953 } 1954 late_initcall(bpf_sockmap_iter_init); 1955