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 static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock, 15 struct sk_msg *msg, u32 apply_bytes, int flags) 16 { 17 bool apply = apply_bytes; 18 struct scatterlist *sge; 19 u32 size, copied = 0; 20 struct sk_msg *tmp; 21 int i, ret = 0; 22 23 tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL); 24 if (unlikely(!tmp)) 25 return -ENOMEM; 26 27 lock_sock(sk); 28 tmp->sg.start = msg->sg.start; 29 i = msg->sg.start; 30 do { 31 sge = sk_msg_elem(msg, i); 32 size = (apply && apply_bytes < sge->length) ? 33 apply_bytes : sge->length; 34 if (!sk_wmem_schedule(sk, size)) { 35 if (!copied) 36 ret = -ENOMEM; 37 break; 38 } 39 40 sk_mem_charge(sk, size); 41 sk_msg_xfer(tmp, msg, i, size); 42 copied += size; 43 if (sge->length) 44 get_page(sk_msg_page(tmp, i)); 45 sk_msg_iter_var_next(i); 46 tmp->sg.end = i; 47 if (apply) { 48 apply_bytes -= size; 49 if (!apply_bytes) { 50 if (sge->length) 51 sk_msg_iter_var_prev(i); 52 break; 53 } 54 } 55 } while (i != msg->sg.end); 56 57 if (!ret) { 58 msg->sg.start = i; 59 sk_psock_queue_msg(psock, tmp); 60 sk_psock_data_ready(sk, psock); 61 } else { 62 sk_msg_free(sk, tmp); 63 kfree(tmp); 64 } 65 66 release_sock(sk); 67 return ret; 68 } 69 70 static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes, 71 int flags, bool uncharge) 72 { 73 bool apply = apply_bytes; 74 struct scatterlist *sge; 75 struct msghdr msghdr = { .msg_flags = flags | MSG_SPLICE_PAGES, }; 76 struct page *page; 77 int size, ret = 0; 78 u32 off; 79 80 while (1) { 81 struct bio_vec bvec; 82 bool has_tx_ulp; 83 84 sge = sk_msg_elem(msg, msg->sg.start); 85 size = (apply && apply_bytes < sge->length) ? 86 apply_bytes : sge->length; 87 off = sge->offset; 88 page = sg_page(sge); 89 90 tcp_rate_check_app_limited(sk); 91 retry: 92 has_tx_ulp = tls_sw_has_ctx_tx(sk); 93 if (has_tx_ulp) 94 msghdr.msg_flags |= MSG_SENDPAGE_NOPOLICY; 95 96 if (flags & MSG_SENDPAGE_NOTLAST) 97 msghdr.msg_flags |= MSG_MORE; 98 99 bvec_set_page(&bvec, page, size, off); 100 iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size); 101 ret = tcp_sendmsg_locked(sk, &msghdr, size); 102 if (ret <= 0) 103 return ret; 104 105 if (apply) 106 apply_bytes -= ret; 107 msg->sg.size -= ret; 108 sge->offset += ret; 109 sge->length -= ret; 110 if (uncharge) 111 sk_mem_uncharge(sk, ret); 112 if (ret != size) { 113 size -= ret; 114 off += ret; 115 goto retry; 116 } 117 if (!sge->length) { 118 put_page(page); 119 sk_msg_iter_next(msg, start); 120 sg_init_table(sge, 1); 121 if (msg->sg.start == msg->sg.end) 122 break; 123 } 124 if (apply && !apply_bytes) 125 break; 126 } 127 128 return 0; 129 } 130 131 static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg, 132 u32 apply_bytes, int flags, bool uncharge) 133 { 134 int ret; 135 136 lock_sock(sk); 137 ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge); 138 release_sock(sk); 139 return ret; 140 } 141 142 int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress, 143 struct sk_msg *msg, u32 bytes, int flags) 144 { 145 struct sk_psock *psock = sk_psock_get(sk); 146 int ret; 147 148 if (unlikely(!psock)) 149 return -EPIPE; 150 151 ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) : 152 tcp_bpf_push_locked(sk, msg, bytes, flags, false); 153 sk_psock_put(sk, psock); 154 return ret; 155 } 156 EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir); 157 158 #ifdef CONFIG_BPF_SYSCALL 159 static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock, 160 long timeo) 161 { 162 DEFINE_WAIT_FUNC(wait, woken_wake_function); 163 int ret = 0; 164 165 if (sk->sk_shutdown & RCV_SHUTDOWN) 166 return 1; 167 168 if (!timeo) 169 return ret; 170 171 add_wait_queue(sk_sleep(sk), &wait); 172 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 173 ret = sk_wait_event(sk, &timeo, 174 !list_empty(&psock->ingress_msg) || 175 !skb_queue_empty_lockless(&sk->sk_receive_queue), &wait); 176 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 177 remove_wait_queue(sk_sleep(sk), &wait); 178 return ret; 179 } 180 181 static int tcp_bpf_recvmsg_parser(struct sock *sk, 182 struct msghdr *msg, 183 size_t len, 184 int flags, 185 int *addr_len) 186 { 187 struct sk_psock *psock; 188 int copied; 189 190 if (unlikely(flags & MSG_ERRQUEUE)) 191 return inet_recv_error(sk, msg, len, addr_len); 192 193 if (!len) 194 return 0; 195 196 psock = sk_psock_get(sk); 197 if (unlikely(!psock)) 198 return tcp_recvmsg(sk, msg, len, flags, addr_len); 199 200 lock_sock(sk); 201 msg_bytes_ready: 202 copied = sk_msg_recvmsg(sk, psock, msg, len, flags); 203 if (!copied) { 204 long timeo; 205 int data; 206 207 if (sock_flag(sk, SOCK_DONE)) 208 goto out; 209 210 if (sk->sk_err) { 211 copied = sock_error(sk); 212 goto out; 213 } 214 215 if (sk->sk_shutdown & RCV_SHUTDOWN) 216 goto out; 217 218 if (sk->sk_state == TCP_CLOSE) { 219 copied = -ENOTCONN; 220 goto out; 221 } 222 223 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 224 if (!timeo) { 225 copied = -EAGAIN; 226 goto out; 227 } 228 229 if (signal_pending(current)) { 230 copied = sock_intr_errno(timeo); 231 goto out; 232 } 233 234 data = tcp_msg_wait_data(sk, psock, timeo); 235 if (data && !sk_psock_queue_empty(psock)) 236 goto msg_bytes_ready; 237 copied = -EAGAIN; 238 } 239 out: 240 release_sock(sk); 241 sk_psock_put(sk, psock); 242 return copied; 243 } 244 245 static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 246 int flags, int *addr_len) 247 { 248 struct sk_psock *psock; 249 int copied, ret; 250 251 if (unlikely(flags & MSG_ERRQUEUE)) 252 return inet_recv_error(sk, msg, len, addr_len); 253 254 if (!len) 255 return 0; 256 257 psock = sk_psock_get(sk); 258 if (unlikely(!psock)) 259 return tcp_recvmsg(sk, msg, len, flags, addr_len); 260 if (!skb_queue_empty(&sk->sk_receive_queue) && 261 sk_psock_queue_empty(psock)) { 262 sk_psock_put(sk, psock); 263 return tcp_recvmsg(sk, msg, len, flags, addr_len); 264 } 265 lock_sock(sk); 266 msg_bytes_ready: 267 copied = sk_msg_recvmsg(sk, psock, msg, len, flags); 268 if (!copied) { 269 long timeo; 270 int data; 271 272 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 273 data = tcp_msg_wait_data(sk, psock, timeo); 274 if (data) { 275 if (!sk_psock_queue_empty(psock)) 276 goto msg_bytes_ready; 277 release_sock(sk); 278 sk_psock_put(sk, psock); 279 return tcp_recvmsg(sk, msg, len, flags, addr_len); 280 } 281 copied = -EAGAIN; 282 } 283 ret = copied; 284 release_sock(sk); 285 sk_psock_put(sk, psock); 286 return ret; 287 } 288 289 static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock, 290 struct sk_msg *msg, int *copied, int flags) 291 { 292 bool cork = false, enospc = sk_msg_full(msg), redir_ingress; 293 struct sock *sk_redir; 294 u32 tosend, origsize, sent, delta = 0; 295 u32 eval; 296 int ret; 297 298 more_data: 299 if (psock->eval == __SK_NONE) { 300 /* Track delta in msg size to add/subtract it on SK_DROP from 301 * returned to user copied size. This ensures user doesn't 302 * get a positive return code with msg_cut_data and SK_DROP 303 * verdict. 304 */ 305 delta = msg->sg.size; 306 psock->eval = sk_psock_msg_verdict(sk, psock, msg); 307 delta -= msg->sg.size; 308 } 309 310 if (msg->cork_bytes && 311 msg->cork_bytes > msg->sg.size && !enospc) { 312 psock->cork_bytes = msg->cork_bytes - msg->sg.size; 313 if (!psock->cork) { 314 psock->cork = kzalloc(sizeof(*psock->cork), 315 GFP_ATOMIC | __GFP_NOWARN); 316 if (!psock->cork) 317 return -ENOMEM; 318 } 319 memcpy(psock->cork, msg, sizeof(*msg)); 320 return 0; 321 } 322 323 tosend = msg->sg.size; 324 if (psock->apply_bytes && psock->apply_bytes < tosend) 325 tosend = psock->apply_bytes; 326 eval = __SK_NONE; 327 328 switch (psock->eval) { 329 case __SK_PASS: 330 ret = tcp_bpf_push(sk, msg, tosend, flags, true); 331 if (unlikely(ret)) { 332 *copied -= sk_msg_free(sk, msg); 333 break; 334 } 335 sk_msg_apply_bytes(psock, tosend); 336 break; 337 case __SK_REDIRECT: 338 redir_ingress = psock->redir_ingress; 339 sk_redir = psock->sk_redir; 340 sk_msg_apply_bytes(psock, tosend); 341 if (!psock->apply_bytes) { 342 /* Clean up before releasing the sock lock. */ 343 eval = psock->eval; 344 psock->eval = __SK_NONE; 345 psock->sk_redir = NULL; 346 } 347 if (psock->cork) { 348 cork = true; 349 psock->cork = NULL; 350 } 351 sk_msg_return(sk, msg, tosend); 352 release_sock(sk); 353 354 origsize = msg->sg.size; 355 ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress, 356 msg, tosend, flags); 357 sent = origsize - msg->sg.size; 358 359 if (eval == __SK_REDIRECT) 360 sock_put(sk_redir); 361 362 lock_sock(sk); 363 if (unlikely(ret < 0)) { 364 int free = sk_msg_free_nocharge(sk, msg); 365 366 if (!cork) 367 *copied -= free; 368 } 369 if (cork) { 370 sk_msg_free(sk, msg); 371 kfree(msg); 372 msg = NULL; 373 ret = 0; 374 } 375 break; 376 case __SK_DROP: 377 default: 378 sk_msg_free_partial(sk, msg, tosend); 379 sk_msg_apply_bytes(psock, tosend); 380 *copied -= (tosend + delta); 381 return -EACCES; 382 } 383 384 if (likely(!ret)) { 385 if (!psock->apply_bytes) { 386 psock->eval = __SK_NONE; 387 if (psock->sk_redir) { 388 sock_put(psock->sk_redir); 389 psock->sk_redir = NULL; 390 } 391 } 392 if (msg && 393 msg->sg.data[msg->sg.start].page_link && 394 msg->sg.data[msg->sg.start].length) { 395 if (eval == __SK_REDIRECT) 396 sk_mem_charge(sk, tosend - sent); 397 goto more_data; 398 } 399 } 400 return ret; 401 } 402 403 static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) 404 { 405 struct sk_msg tmp, *msg_tx = NULL; 406 int copied = 0, err = 0; 407 struct sk_psock *psock; 408 long timeo; 409 int flags; 410 411 /* Don't let internal sendpage flags through */ 412 flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED); 413 flags |= MSG_NO_SHARED_FRAGS; 414 415 psock = sk_psock_get(sk); 416 if (unlikely(!psock)) 417 return tcp_sendmsg(sk, msg, size); 418 419 lock_sock(sk); 420 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 421 while (msg_data_left(msg)) { 422 bool enospc = false; 423 u32 copy, osize; 424 425 if (sk->sk_err) { 426 err = -sk->sk_err; 427 goto out_err; 428 } 429 430 copy = msg_data_left(msg); 431 if (!sk_stream_memory_free(sk)) 432 goto wait_for_sndbuf; 433 if (psock->cork) { 434 msg_tx = psock->cork; 435 } else { 436 msg_tx = &tmp; 437 sk_msg_init(msg_tx); 438 } 439 440 osize = msg_tx->sg.size; 441 err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1); 442 if (err) { 443 if (err != -ENOSPC) 444 goto wait_for_memory; 445 enospc = true; 446 copy = msg_tx->sg.size - osize; 447 } 448 449 err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx, 450 copy); 451 if (err < 0) { 452 sk_msg_trim(sk, msg_tx, osize); 453 goto out_err; 454 } 455 456 copied += copy; 457 if (psock->cork_bytes) { 458 if (size > psock->cork_bytes) 459 psock->cork_bytes = 0; 460 else 461 psock->cork_bytes -= size; 462 if (psock->cork_bytes && !enospc) 463 goto out_err; 464 /* All cork bytes are accounted, rerun the prog. */ 465 psock->eval = __SK_NONE; 466 psock->cork_bytes = 0; 467 } 468 469 err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags); 470 if (unlikely(err < 0)) 471 goto out_err; 472 continue; 473 wait_for_sndbuf: 474 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 475 wait_for_memory: 476 err = sk_stream_wait_memory(sk, &timeo); 477 if (err) { 478 if (msg_tx && msg_tx != psock->cork) 479 sk_msg_free(sk, msg_tx); 480 goto out_err; 481 } 482 } 483 out_err: 484 if (err < 0) 485 err = sk_stream_error(sk, msg->msg_flags, err); 486 release_sock(sk); 487 sk_psock_put(sk, psock); 488 return copied ? copied : err; 489 } 490 491 static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset, 492 size_t size, int flags) 493 { 494 struct sk_msg tmp, *msg = NULL; 495 int err = 0, copied = 0; 496 struct sk_psock *psock; 497 bool enospc = false; 498 499 psock = sk_psock_get(sk); 500 if (unlikely(!psock)) 501 return tcp_sendpage(sk, page, offset, size, flags); 502 503 lock_sock(sk); 504 if (psock->cork) { 505 msg = psock->cork; 506 } else { 507 msg = &tmp; 508 sk_msg_init(msg); 509 } 510 511 /* Catch case where ring is full and sendpage is stalled. */ 512 if (unlikely(sk_msg_full(msg))) 513 goto out_err; 514 515 sk_msg_page_add(msg, page, size, offset); 516 sk_mem_charge(sk, size); 517 copied = size; 518 if (sk_msg_full(msg)) 519 enospc = true; 520 if (psock->cork_bytes) { 521 if (size > psock->cork_bytes) 522 psock->cork_bytes = 0; 523 else 524 psock->cork_bytes -= size; 525 if (psock->cork_bytes && !enospc) 526 goto out_err; 527 /* All cork bytes are accounted, rerun the prog. */ 528 psock->eval = __SK_NONE; 529 psock->cork_bytes = 0; 530 } 531 532 err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags); 533 out_err: 534 release_sock(sk); 535 sk_psock_put(sk, psock); 536 return copied ? copied : err; 537 } 538 539 enum { 540 TCP_BPF_IPV4, 541 TCP_BPF_IPV6, 542 TCP_BPF_NUM_PROTS, 543 }; 544 545 enum { 546 TCP_BPF_BASE, 547 TCP_BPF_TX, 548 TCP_BPF_RX, 549 TCP_BPF_TXRX, 550 TCP_BPF_NUM_CFGS, 551 }; 552 553 static struct proto *tcpv6_prot_saved __read_mostly; 554 static DEFINE_SPINLOCK(tcpv6_prot_lock); 555 static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS]; 556 557 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS], 558 struct proto *base) 559 { 560 prot[TCP_BPF_BASE] = *base; 561 prot[TCP_BPF_BASE].destroy = sock_map_destroy; 562 prot[TCP_BPF_BASE].close = sock_map_close; 563 prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg; 564 prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable; 565 566 prot[TCP_BPF_TX] = prot[TCP_BPF_BASE]; 567 prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg; 568 prot[TCP_BPF_TX].sendpage = tcp_bpf_sendpage; 569 570 prot[TCP_BPF_RX] = prot[TCP_BPF_BASE]; 571 prot[TCP_BPF_RX].recvmsg = tcp_bpf_recvmsg_parser; 572 573 prot[TCP_BPF_TXRX] = prot[TCP_BPF_TX]; 574 prot[TCP_BPF_TXRX].recvmsg = tcp_bpf_recvmsg_parser; 575 } 576 577 static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops) 578 { 579 if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) { 580 spin_lock_bh(&tcpv6_prot_lock); 581 if (likely(ops != tcpv6_prot_saved)) { 582 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops); 583 smp_store_release(&tcpv6_prot_saved, ops); 584 } 585 spin_unlock_bh(&tcpv6_prot_lock); 586 } 587 } 588 589 static int __init tcp_bpf_v4_build_proto(void) 590 { 591 tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot); 592 return 0; 593 } 594 late_initcall(tcp_bpf_v4_build_proto); 595 596 static int tcp_bpf_assert_proto_ops(struct proto *ops) 597 { 598 /* In order to avoid retpoline, we make assumptions when we call 599 * into ops if e.g. a psock is not present. Make sure they are 600 * indeed valid assumptions. 601 */ 602 return ops->recvmsg == tcp_recvmsg && 603 ops->sendmsg == tcp_sendmsg && 604 ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP; 605 } 606 607 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) 608 { 609 int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4; 610 int config = psock->progs.msg_parser ? TCP_BPF_TX : TCP_BPF_BASE; 611 612 if (psock->progs.stream_verdict || psock->progs.skb_verdict) { 613 config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX; 614 } 615 616 if (restore) { 617 if (inet_csk_has_ulp(sk)) { 618 /* TLS does not have an unhash proto in SW cases, 619 * but we need to ensure we stop using the sock_map 620 * unhash routine because the associated psock is being 621 * removed. So use the original unhash handler. 622 */ 623 WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash); 624 tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space); 625 } else { 626 sk->sk_write_space = psock->saved_write_space; 627 /* Pairs with lockless read in sk_clone_lock() */ 628 sock_replace_proto(sk, psock->sk_proto); 629 } 630 return 0; 631 } 632 633 if (sk->sk_family == AF_INET6) { 634 if (tcp_bpf_assert_proto_ops(psock->sk_proto)) 635 return -EINVAL; 636 637 tcp_bpf_check_v6_needs_rebuild(psock->sk_proto); 638 } 639 640 /* Pairs with lockless read in sk_clone_lock() */ 641 sock_replace_proto(sk, &tcp_bpf_prots[family][config]); 642 return 0; 643 } 644 EXPORT_SYMBOL_GPL(tcp_bpf_update_proto); 645 646 /* If a child got cloned from a listening socket that had tcp_bpf 647 * protocol callbacks installed, we need to restore the callbacks to 648 * the default ones because the child does not inherit the psock state 649 * that tcp_bpf callbacks expect. 650 */ 651 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) 652 { 653 struct proto *prot = newsk->sk_prot; 654 655 if (is_insidevar(prot, tcp_bpf_prots)) 656 newsk->sk_prot = sk->sk_prot_creator; 657 } 658 #endif /* CONFIG_BPF_SYSCALL */ 659