1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */ 3 4 #include <linux/skmsg.h> 5 #include <linux/filter.h> 6 #include <linux/bpf.h> 7 #include <linux/init.h> 8 #include <linux/wait.h> 9 #include <linux/util_macros.h> 10 11 #include <net/inet_common.h> 12 #include <net/tls.h> 13 #include <asm/ioctls.h> 14 15 void tcp_eat_skb(struct sock *sk, struct sk_buff *skb) 16 { 17 struct tcp_sock *tcp; 18 int copied; 19 20 if (!skb || !skb->len || !sk_is_tcp(sk)) 21 return; 22 23 if (skb_bpf_strparser(skb)) 24 return; 25 26 tcp = tcp_sk(sk); 27 copied = tcp->copied_seq + skb->len; 28 WRITE_ONCE(tcp->copied_seq, copied); 29 tcp_rcv_space_adjust(sk); 30 __tcp_cleanup_rbuf(sk, skb->len); 31 } 32 33 static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock, 34 struct sk_msg *msg, u32 apply_bytes) 35 { 36 bool apply = apply_bytes; 37 struct scatterlist *sge; 38 u32 size, copied = 0; 39 struct sk_msg *tmp; 40 int i, ret = 0; 41 42 tmp = kzalloc_obj(*tmp, __GFP_NOWARN | GFP_KERNEL); 43 if (unlikely(!tmp)) 44 return -ENOMEM; 45 46 lock_sock(sk); 47 tmp->sg.start = msg->sg.start; 48 i = msg->sg.start; 49 do { 50 sge = sk_msg_elem(msg, i); 51 size = (apply && apply_bytes < sge->length) ? 52 apply_bytes : sge->length; 53 if (!__sk_rmem_schedule(sk, size, false)) { 54 if (!copied) 55 ret = -ENOMEM; 56 break; 57 } 58 59 sk_mem_charge(sk, size); 60 atomic_add(size, &sk->sk_rmem_alloc); 61 sk_msg_xfer(tmp, msg, i, size); 62 copied += size; 63 if (sge->length) 64 get_page(sk_msg_page(tmp, i)); 65 sk_msg_iter_var_next(i); 66 tmp->sg.end = i; 67 if (apply) { 68 apply_bytes -= size; 69 if (!apply_bytes) { 70 if (sge->length) 71 sk_msg_iter_var_prev(i); 72 break; 73 } 74 } 75 } while (i != msg->sg.end); 76 77 if (!ret) { 78 msg->sg.start = i; 79 if (!sk_psock_queue_msg(psock, tmp)) 80 atomic_sub(copied, &sk->sk_rmem_alloc); 81 sk_psock_data_ready(sk, psock); 82 } else { 83 sk_msg_free(sk, tmp); 84 kfree(tmp); 85 } 86 87 release_sock(sk); 88 return ret; 89 } 90 91 static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes, 92 int flags, bool uncharge) 93 { 94 struct msghdr msghdr = {}; 95 bool apply = apply_bytes; 96 struct scatterlist *sge; 97 struct page *page; 98 int size, ret = 0; 99 u32 off; 100 101 while (1) { 102 struct bio_vec bvec; 103 bool has_tx_ulp; 104 105 sge = sk_msg_elem(msg, msg->sg.start); 106 size = (apply && apply_bytes < sge->length) ? 107 apply_bytes : sge->length; 108 off = sge->offset; 109 page = sg_page(sge); 110 111 tcp_rate_check_app_limited(sk); 112 retry: 113 msghdr.msg_flags = flags | MSG_SPLICE_PAGES; 114 has_tx_ulp = tls_sw_has_ctx_tx(sk); 115 if (has_tx_ulp) 116 msghdr.msg_flags |= MSG_SENDPAGE_NOPOLICY; 117 118 if (size < sge->length && msg->sg.start != msg->sg.end) 119 msghdr.msg_flags |= MSG_MORE; 120 121 bvec_set_page(&bvec, page, size, off); 122 iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size); 123 ret = tcp_sendmsg_locked(sk, &msghdr, size); 124 if (ret <= 0) 125 return ret; 126 127 if (apply) 128 apply_bytes -= ret; 129 msg->sg.size -= ret; 130 sge->offset += ret; 131 sge->length -= ret; 132 if (uncharge) 133 sk_mem_uncharge(sk, ret); 134 if (ret != size) { 135 size -= ret; 136 off += ret; 137 goto retry; 138 } 139 if (!sge->length) { 140 put_page(page); 141 sk_msg_iter_next(msg, start); 142 sg_init_table(sge, 1); 143 if (msg->sg.start == msg->sg.end) 144 break; 145 } 146 if (apply && !apply_bytes) 147 break; 148 } 149 150 return 0; 151 } 152 153 static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg, 154 u32 apply_bytes, int flags, bool uncharge) 155 { 156 int ret; 157 158 lock_sock(sk); 159 ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge); 160 release_sock(sk); 161 return ret; 162 } 163 164 int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress, 165 struct sk_msg *msg, u32 bytes, int flags) 166 { 167 struct sk_psock *psock = sk_psock_get(sk); 168 int ret; 169 170 if (unlikely(!psock)) 171 return -EPIPE; 172 173 ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes) : 174 tcp_bpf_push_locked(sk, msg, bytes, flags, false); 175 sk_psock_put(sk, psock); 176 return ret; 177 } 178 EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir); 179 180 #ifdef CONFIG_BPF_SYSCALL 181 static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock, 182 long timeo) 183 { 184 DEFINE_WAIT_FUNC(wait, woken_wake_function); 185 int ret = 0; 186 187 if (sk->sk_shutdown & RCV_SHUTDOWN) 188 return 1; 189 190 if (!timeo) 191 return ret; 192 193 add_wait_queue(sk_sleep(sk), &wait); 194 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 195 ret = sk_wait_event(sk, &timeo, 196 !list_empty(&psock->ingress_msg) || 197 !skb_queue_empty_lockless(&sk->sk_receive_queue), &wait); 198 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 199 remove_wait_queue(sk_sleep(sk), &wait); 200 return ret; 201 } 202 203 static bool is_next_msg_fin(struct sk_psock *psock) 204 { 205 struct scatterlist *sge; 206 struct sk_msg *msg_rx; 207 int i; 208 209 msg_rx = sk_psock_peek_msg(psock); 210 i = msg_rx->sg.start; 211 sge = sk_msg_elem(msg_rx, i); 212 if (!sge->length) { 213 struct sk_buff *skb = msg_rx->skb; 214 215 if (skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 216 return true; 217 } 218 return false; 219 } 220 221 static int tcp_bpf_recvmsg_parser(struct sock *sk, 222 struct msghdr *msg, 223 size_t len, 224 int flags) 225 { 226 int peek = flags & MSG_PEEK; 227 struct sk_psock *psock; 228 struct tcp_sock *tcp; 229 int copied_from_self = 0; 230 int copied = 0; 231 u32 seq; 232 233 if (unlikely(flags & MSG_ERRQUEUE)) 234 return inet_recv_error(sk, msg, len); 235 236 if (!len) 237 return 0; 238 239 psock = sk_psock_get(sk); 240 if (unlikely(!psock)) 241 return tcp_recvmsg(sk, msg, len, flags); 242 243 lock_sock(sk); 244 tcp = tcp_sk(sk); 245 seq = tcp->copied_seq; 246 /* We may have received data on the sk_receive_queue pre-accept and 247 * then we can not use read_skb in this context because we haven't 248 * assigned a sk_socket yet so have no link to the ops. The work-around 249 * is to check the sk_receive_queue and in these cases read skbs off 250 * queue again. The read_skb hook is not running at this point because 251 * of lock_sock so we avoid having multiple runners in read_skb. 252 */ 253 if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) { 254 tcp_data_ready(sk); 255 /* This handles the ENOMEM errors if we both receive data 256 * pre accept and are already under memory pressure. At least 257 * let user know to retry. 258 */ 259 if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) { 260 copied = -EAGAIN; 261 goto out; 262 } 263 } 264 265 msg_bytes_ready: 266 copied = __sk_msg_recvmsg(sk, psock, msg, len, flags, &copied_from_self); 267 /* The typical case for EFAULT is the socket was gracefully 268 * shutdown with a FIN pkt. So check here the other case is 269 * some error on copy_page_to_iter which would be unexpected. 270 * On fin return correct return code to zero. 271 */ 272 if (copied == -EFAULT) { 273 bool is_fin = is_next_msg_fin(psock); 274 275 if (is_fin) { 276 copied = 0; 277 seq++; 278 goto out; 279 } 280 } 281 seq += copied_from_self; 282 if (!copied) { 283 long timeo; 284 int data; 285 286 if (sock_flag(sk, SOCK_DONE)) 287 goto out; 288 289 copied = sock_error(sk); 290 if (copied) 291 goto out; 292 293 if (sk->sk_shutdown & RCV_SHUTDOWN) 294 goto out; 295 296 if (sk->sk_state == TCP_CLOSE) { 297 copied = -ENOTCONN; 298 goto out; 299 } 300 301 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 302 if (!timeo) { 303 copied = -EAGAIN; 304 goto out; 305 } 306 307 if (signal_pending(current)) { 308 copied = sock_intr_errno(timeo); 309 goto out; 310 } 311 312 data = tcp_msg_wait_data(sk, psock, timeo); 313 if (data < 0) { 314 copied = data; 315 goto unlock; 316 } 317 if (data && !sk_psock_queue_empty(psock)) 318 goto msg_bytes_ready; 319 copied = -EAGAIN; 320 } 321 out: 322 if (!peek) 323 WRITE_ONCE(tcp->copied_seq, seq); 324 tcp_rcv_space_adjust(sk); 325 if (copied > 0) 326 __tcp_cleanup_rbuf(sk, copied); 327 328 unlock: 329 release_sock(sk); 330 sk_psock_put(sk, psock); 331 return copied; 332 } 333 334 static int tcp_bpf_ioctl(struct sock *sk, int cmd, int *karg) 335 { 336 struct sk_psock *psock; 337 bool slow; 338 339 if (cmd != SIOCINQ) 340 return tcp_ioctl(sk, cmd, karg); 341 342 /* works similar as tcp_ioctl */ 343 if (sk->sk_state == TCP_LISTEN) 344 return -EINVAL; 345 346 slow = lock_sock_fast(sk); 347 psock = sk_psock_get(sk); 348 if (unlikely(!psock)) { 349 unlock_sock_fast(sk, slow); 350 return tcp_ioctl(sk, cmd, karg); 351 } 352 *karg = sk_psock_get_msg_len_nolock(psock); 353 /* Without a verdict program, ingress data is never diverted to 354 * ingress_msg: it stays in sk_receive_queue and is read through 355 * the fallback to tcp_recvmsg(), so account for it like 356 * tcp_ioctl() does. 357 */ 358 if (!READ_ONCE(psock->progs.stream_verdict) && 359 !READ_ONCE(psock->progs.skb_verdict)) 360 *karg += tcp_inq(sk); 361 sk_psock_put(sk, psock); 362 unlock_sock_fast(sk, slow); 363 364 return 0; 365 } 366 367 static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 368 int flags) 369 { 370 struct sk_psock *psock; 371 int copied, ret; 372 373 if (unlikely(flags & MSG_ERRQUEUE)) 374 return inet_recv_error(sk, msg, len); 375 376 if (!len) 377 return 0; 378 379 psock = sk_psock_get(sk); 380 if (unlikely(!psock)) 381 return tcp_recvmsg(sk, msg, len, flags); 382 if (!skb_queue_empty(&sk->sk_receive_queue) && 383 sk_psock_queue_empty(psock)) { 384 sk_psock_put(sk, psock); 385 return tcp_recvmsg(sk, msg, len, flags); 386 } 387 lock_sock(sk); 388 msg_bytes_ready: 389 copied = sk_msg_recvmsg(sk, psock, msg, len, flags); 390 if (!copied) { 391 long timeo; 392 int data; 393 394 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 395 data = tcp_msg_wait_data(sk, psock, timeo); 396 if (data < 0) { 397 ret = data; 398 goto unlock; 399 } 400 if (data) { 401 if (!sk_psock_queue_empty(psock)) 402 goto msg_bytes_ready; 403 release_sock(sk); 404 sk_psock_put(sk, psock); 405 return tcp_recvmsg(sk, msg, len, flags); 406 } 407 copied = -EAGAIN; 408 } 409 ret = copied; 410 411 unlock: 412 release_sock(sk); 413 sk_psock_put(sk, psock); 414 return ret; 415 } 416 417 static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock, 418 struct sk_msg *msg, int *copied, int flags) 419 { 420 bool cork = false, enospc = sk_msg_full(msg), redir_ingress; 421 struct sock *sk_redir; 422 u32 tosend, origsize, sent, delta = 0; 423 u32 eval; 424 int ret; 425 426 more_data: 427 if (psock->eval == __SK_NONE) { 428 /* Track delta in msg size to add/subtract it on SK_DROP from 429 * returned to user copied size. This ensures user doesn't 430 * get a positive return code with msg_cut_data and SK_DROP 431 * verdict. 432 */ 433 delta = msg->sg.size; 434 psock->eval = sk_psock_msg_verdict(sk, psock, msg); 435 delta -= msg->sg.size; 436 } 437 438 if (msg->cork_bytes && 439 msg->cork_bytes > msg->sg.size && !enospc) { 440 psock->cork_bytes = msg->cork_bytes - msg->sg.size; 441 if (!psock->cork) { 442 psock->cork = kzalloc_obj(*psock->cork, 443 GFP_ATOMIC | __GFP_NOWARN); 444 if (!psock->cork) { 445 sk_msg_free(sk, msg); 446 *copied = 0; 447 return -ENOMEM; 448 } 449 } 450 memcpy(psock->cork, msg, sizeof(*msg)); 451 return 0; 452 } 453 454 tosend = msg->sg.size; 455 if (psock->apply_bytes && psock->apply_bytes < tosend) 456 tosend = psock->apply_bytes; 457 eval = __SK_NONE; 458 459 switch (psock->eval) { 460 case __SK_PASS: 461 ret = tcp_bpf_push(sk, msg, tosend, flags, true); 462 if (unlikely(ret)) { 463 *copied -= sk_msg_free(sk, msg); 464 break; 465 } 466 sk_msg_apply_bytes(psock, tosend); 467 break; 468 case __SK_REDIRECT: 469 redir_ingress = psock->redir_ingress; 470 sk_redir = psock->sk_redir; 471 sock_hold(sk_redir); 472 sk_msg_apply_bytes(psock, tosend); 473 if (!psock->apply_bytes) { 474 /* Clean up before releasing the sock lock. */ 475 eval = psock->eval; 476 psock->eval = __SK_NONE; 477 psock->sk_redir = NULL; 478 } 479 if (psock->cork) { 480 cork = true; 481 psock->cork = NULL; 482 } 483 release_sock(sk); 484 485 origsize = msg->sg.size; 486 ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress, 487 msg, tosend, flags); 488 sent = origsize - msg->sg.size; 489 490 if (eval == __SK_REDIRECT) 491 sock_put(sk_redir); 492 sock_put(sk_redir); 493 494 lock_sock(sk); 495 sk_mem_uncharge(sk, sent); 496 if (unlikely(ret < 0)) { 497 int free = sk_msg_free(sk, msg); 498 499 if (!cork) 500 *copied -= free; 501 } 502 if (cork) { 503 sk_msg_free(sk, msg); 504 kfree(msg); 505 msg = NULL; 506 ret = 0; 507 } 508 break; 509 case __SK_DROP: 510 default: 511 sk_msg_free(sk, msg); 512 sk_msg_apply_bytes(psock, tosend); 513 *copied -= (tosend + delta); 514 return -EACCES; 515 } 516 517 if (likely(!ret)) { 518 if (!psock->apply_bytes) { 519 psock->eval = __SK_NONE; 520 if (psock->sk_redir) { 521 sock_put(psock->sk_redir); 522 psock->sk_redir = NULL; 523 } 524 } 525 if (msg && 526 msg->sg.data[msg->sg.start].page_link && 527 msg->sg.data[msg->sg.start].length) 528 goto more_data; 529 } 530 return ret; 531 } 532 533 static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) 534 { 535 struct sk_msg tmp, *msg_tx = NULL; 536 int copied = 0, err = 0, ret = 0; 537 struct sk_psock *psock; 538 long timeo; 539 int flags; 540 541 /* Don't let internal flags through */ 542 flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED); 543 flags |= MSG_NO_SHARED_FRAGS; 544 545 psock = sk_psock_get(sk); 546 if (unlikely(!psock)) 547 return tcp_sendmsg(sk, msg, size); 548 549 lock_sock(sk); 550 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 551 while (msg_data_left(msg)) { 552 bool enospc = false; 553 u32 copy, osize; 554 555 err = -READ_ONCE(sk->sk_err); 556 if (err) 557 goto out_err; 558 559 copy = msg_data_left(msg); 560 if (!sk_stream_memory_free(sk)) 561 goto wait_for_sndbuf; 562 if (psock->cork) { 563 msg_tx = psock->cork; 564 } else { 565 msg_tx = &tmp; 566 sk_msg_init(msg_tx); 567 } 568 569 osize = msg_tx->sg.size; 570 err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1); 571 if (err) { 572 if (err != -ENOSPC) 573 goto wait_for_memory; 574 enospc = true; 575 copy = msg_tx->sg.size - osize; 576 } 577 578 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx, 579 copy); 580 if (ret < 0) { 581 sk_msg_trim(sk, msg_tx, osize); 582 err = ret; 583 goto out_err; 584 } 585 586 copied += ret; 587 if (psock->cork_bytes) { 588 if (size > psock->cork_bytes) 589 psock->cork_bytes = 0; 590 else 591 psock->cork_bytes -= size; 592 if (psock->cork_bytes && !enospc) 593 goto out_err; 594 /* All cork bytes are accounted, rerun the prog. */ 595 psock->eval = __SK_NONE; 596 psock->cork_bytes = 0; 597 } 598 599 err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags); 600 if (unlikely(err < 0)) 601 goto out_err; 602 continue; 603 wait_for_sndbuf: 604 sk_set_nospace(sk); 605 wait_for_memory: 606 err = sk_stream_wait_memory(sk, &timeo); 607 if (err) { 608 if (msg_tx == &tmp) 609 sk_msg_free(sk, msg_tx); 610 goto out_err; 611 } 612 } 613 out_err: 614 if (err < 0) 615 err = sk_stream_error(sk, msg->msg_flags, err); 616 release_sock(sk); 617 sk_psock_put(sk, psock); 618 return copied > 0 ? copied : err; 619 } 620 621 enum { 622 TCP_BPF_IPV4, 623 TCP_BPF_IPV6, 624 TCP_BPF_NUM_PROTS, 625 }; 626 627 enum { 628 TCP_BPF_BASE, 629 TCP_BPF_TX, 630 TCP_BPF_RX, 631 TCP_BPF_TXRX, 632 TCP_BPF_NUM_CFGS, 633 }; 634 635 static struct proto *tcpv6_prot_saved __read_mostly; 636 static DEFINE_SPINLOCK(tcpv6_prot_lock); 637 static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS]; 638 639 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS], 640 struct proto *base) 641 { 642 prot[TCP_BPF_BASE] = *base; 643 prot[TCP_BPF_BASE].destroy = sock_map_destroy; 644 prot[TCP_BPF_BASE].close = sock_map_close; 645 prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg; 646 prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable; 647 prot[TCP_BPF_BASE].ioctl = tcp_bpf_ioctl; 648 649 prot[TCP_BPF_TX] = prot[TCP_BPF_BASE]; 650 prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg; 651 652 prot[TCP_BPF_RX] = prot[TCP_BPF_BASE]; 653 prot[TCP_BPF_RX].recvmsg = tcp_bpf_recvmsg_parser; 654 655 prot[TCP_BPF_TXRX] = prot[TCP_BPF_TX]; 656 prot[TCP_BPF_TXRX].recvmsg = tcp_bpf_recvmsg_parser; 657 } 658 659 static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops) 660 { 661 if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) { 662 spin_lock_bh(&tcpv6_prot_lock); 663 if (likely(ops != tcpv6_prot_saved)) { 664 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops); 665 smp_store_release(&tcpv6_prot_saved, ops); 666 } 667 spin_unlock_bh(&tcpv6_prot_lock); 668 } 669 } 670 671 static int __init tcp_bpf_v4_build_proto(void) 672 { 673 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot); 674 return 0; 675 } 676 late_initcall(tcp_bpf_v4_build_proto); 677 678 static int tcp_bpf_assert_proto_ops(struct proto *ops) 679 { 680 /* In order to avoid retpoline, we make assumptions when we call 681 * into ops if e.g. a psock is not present. Make sure they are 682 * indeed valid assumptions. 683 */ 684 return ops->recvmsg == tcp_recvmsg && 685 ops->sendmsg == tcp_sendmsg ? 0 : -ENOTSUPP; 686 } 687 688 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER) 689 int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc, 690 sk_read_actor_t recv_actor) 691 { 692 struct sock *sk = strp->sk; 693 struct sk_psock *psock; 694 struct tcp_sock *tp; 695 int copied = 0; 696 697 tp = tcp_sk(sk); 698 rcu_read_lock(); 699 psock = sk_psock(sk); 700 if (WARN_ON_ONCE(!psock)) { 701 desc->error = -EINVAL; 702 goto out; 703 } 704 705 psock->ingress_bytes = 0; 706 copied = tcp_read_sock_noack(sk, desc, recv_actor, true, 707 &psock->copied_seq); 708 if (copied < 0) 709 goto out; 710 /* recv_actor may redirect skb to another socket (SK_REDIRECT) or 711 * just put skb into ingress queue of current socket (SK_PASS). 712 * For SK_REDIRECT, we need to ack the frame immediately but for 713 * SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser(). 714 */ 715 tp->copied_seq = psock->copied_seq - psock->ingress_bytes; 716 tcp_rcv_space_adjust(sk); 717 __tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes); 718 out: 719 rcu_read_unlock(); 720 return copied; 721 } 722 #endif /* CONFIG_BPF_STREAM_PARSER */ 723 724 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) 725 { 726 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4; 727 int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE; 728 729 if (psock->progs.stream_verdict || psock->progs.skb_verdict) { 730 config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX; 731 } 732 733 if (restore) { 734 if (inet_csk_has_ulp(sk)) { 735 /* TLS does not have an unhash proto in SW cases, 736 * but we need to ensure we stop using the sock_map 737 * unhash routine because the associated psock is being 738 * removed. So use the original unhash handler. 739 */ 740 WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash); 741 tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space); 742 } else { 743 WRITE_ONCE(sk->sk_write_space, psock->saved_write_space); 744 /* Pairs with lockless read in sk_clone_lock() */ 745 sock_replace_proto(sk, psock->sk_proto); 746 } 747 return 0; 748 } 749 750 if (sk->sk_family == AF_INET6) { 751 if (tcp_bpf_assert_proto_ops(psock->sk_proto)) 752 return -EINVAL; 753 754 tcp_bpf_check_v6_needs_rebuild(psock->sk_proto); 755 } 756 757 /* Pairs with lockless read in sk_clone_lock() */ 758 sock_replace_proto(sk, &tcp_bpf_prots[family][config]); 759 return 0; 760 } 761 EXPORT_SYMBOL_GPL(tcp_bpf_update_proto); 762 763 /* If a child got cloned from a listening socket that had tcp_bpf 764 * protocol callbacks installed, we need to restore the callbacks to 765 * the default ones because the child does not inherit the psock state 766 * that tcp_bpf callbacks expect. 767 */ 768 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) 769 { 770 struct proto *prot = newsk->sk_prot; 771 772 if (is_insidevar(prot, tcp_bpf_prots)) 773 newsk->sk_prot = sk->sk_prot_creator; 774 } 775 #endif /* CONFIG_BPF_SYSCALL */ 776