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