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