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