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