1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1993 5 * The Regents of the University of California. 6 * Copyright (c) 2006-2007 Robert N. M. Watson 7 * Copyright (c) 2010-2011 Juniper Networks, Inc. 8 * All rights reserved. 9 * 10 * Portions of this software were developed by Robert N. M. Watson under 11 * contract to Juniper Networks, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/cdefs.h> 39 #include "opt_ddb.h" 40 #include "opt_inet.h" 41 #include "opt_inet6.h" 42 #include "opt_ipsec.h" 43 #include "opt_kern_tls.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/arb.h> 48 #include <sys/limits.h> 49 #include <sys/malloc.h> 50 #include <sys/refcount.h> 51 #include <sys/kernel.h> 52 #include <sys/ktls.h> 53 #include <sys/qmath.h> 54 #include <sys/sysctl.h> 55 #include <sys/mbuf.h> 56 #ifdef INET6 57 #include <sys/domain.h> 58 #endif /* INET6 */ 59 #include <sys/socket.h> 60 #include <sys/socketvar.h> 61 #include <sys/protosw.h> 62 #include <sys/proc.h> 63 #include <sys/jail.h> 64 #include <sys/stats.h> 65 66 #ifdef DDB 67 #include <ddb/ddb.h> 68 #endif 69 70 #include <net/if.h> 71 #include <net/if_var.h> 72 #include <net/route.h> 73 #include <net/vnet.h> 74 75 #include <netinet/in.h> 76 #include <netinet/in_kdtrace.h> 77 #include <netinet/in_pcb.h> 78 #include <netinet/in_systm.h> 79 #include <netinet/in_var.h> 80 #include <netinet/ip.h> 81 #include <netinet/ip_var.h> 82 #ifdef INET6 83 #include <netinet/ip6.h> 84 #include <netinet6/in6_pcb.h> 85 #include <netinet6/ip6_var.h> 86 #include <netinet6/scope6_var.h> 87 #endif 88 #include <netinet/tcp.h> 89 #include <netinet/tcp_fsm.h> 90 #include <netinet/tcp_seq.h> 91 #include <netinet/tcp_timer.h> 92 #include <netinet/tcp_var.h> 93 #include <netinet/tcp_log_buf.h> 94 #include <netinet/tcpip.h> 95 #include <netinet/cc/cc.h> 96 #include <netinet/tcp_fastopen.h> 97 #include <netinet/tcp_hpts.h> 98 #ifdef TCPPCAP 99 #include <netinet/tcp_pcap.h> 100 #endif 101 #ifdef TCP_OFFLOAD 102 #include <netinet/tcp_offload.h> 103 #endif 104 #include <netipsec/ipsec_support.h> 105 106 #include <vm/vm.h> 107 #include <vm/vm_param.h> 108 #include <vm/pmap.h> 109 #include <vm/vm_extern.h> 110 #include <vm/vm_map.h> 111 #include <vm/vm_page.h> 112 113 /* 114 * TCP protocol interface to socket abstraction. 115 */ 116 #ifdef INET 117 static int tcp_connect(struct tcpcb *, struct sockaddr_in *, 118 struct thread *td); 119 #endif /* INET */ 120 #ifdef INET6 121 static int tcp6_connect(struct tcpcb *, struct sockaddr_in6 *, 122 struct thread *td); 123 #endif /* INET6 */ 124 static void tcp_disconnect(struct tcpcb *); 125 static void tcp_usrclosed(struct tcpcb *); 126 static void tcp_fill_info(const struct tcpcb *, struct tcp_info *); 127 128 static int tcp_pru_options_support(struct tcpcb *tp, int flags); 129 130 static void 131 tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error) 132 { 133 struct tcp_log_buffer *lgb; 134 135 KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL")); 136 INP_WLOCK_ASSERT(tptoinpcb(tp)); 137 if (tcp_bblogging_on(tp)) { 138 lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error, 139 0, NULL, false, NULL, NULL, 0, NULL); 140 } else { 141 lgb = NULL; 142 } 143 if (lgb != NULL) { 144 if (error >= 0) { 145 lgb->tlb_errno = (uint32_t)error; 146 } 147 lgb->tlb_flex1 = pru; 148 } 149 } 150 151 /* 152 * TCP attaches to socket via pru_attach(), reserving space, 153 * and an internet control block. 154 */ 155 static int 156 tcp_usr_attach(struct socket *so, int proto, struct thread *td) 157 { 158 struct inpcb *inp; 159 struct tcpcb *tp = NULL; 160 int error; 161 162 inp = sotoinpcb(so); 163 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 164 165 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 166 if (error) 167 goto out; 168 169 so->so_rcv.sb_flags |= SB_AUTOSIZE; 170 so->so_snd.sb_flags |= SB_AUTOSIZE; 171 error = in_pcballoc(so, &V_tcbinfo); 172 if (error) 173 goto out; 174 inp = sotoinpcb(so); 175 tp = tcp_newtcpcb(inp); 176 if (tp == NULL) { 177 error = ENOBUFS; 178 in_pcbdetach(inp); 179 in_pcbfree(inp); 180 goto out; 181 } 182 tp->t_state = TCPS_CLOSED; 183 tcp_bblog_pru(tp, PRU_ATTACH, error); 184 INP_WUNLOCK(inp); 185 TCPSTATES_INC(TCPS_CLOSED); 186 out: 187 TCP_PROBE2(debug__user, tp, PRU_ATTACH); 188 return (error); 189 } 190 191 /* 192 * tcp_usr_detach is called when the socket layer loses its final reference 193 * to the socket, be it a file descriptor reference, a reference from TCP, 194 * etc. At this point, there is only one case in which we will keep around 195 * inpcb state: time wait. 196 */ 197 static void 198 tcp_usr_detach(struct socket *so) 199 { 200 struct inpcb *inp; 201 struct tcpcb *tp; 202 203 inp = sotoinpcb(so); 204 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 205 INP_WLOCK(inp); 206 KASSERT(so->so_pcb == inp && inp->inp_socket == so, 207 ("%s: socket %p inp %p mismatch", __func__, so, inp)); 208 209 tp = intotcpcb(inp); 210 211 KASSERT(inp->inp_flags & INP_DROPPED || 212 tp->t_state < TCPS_SYN_SENT, 213 ("%s: inp %p not dropped or embryonic", __func__, inp)); 214 215 tcp_discardcb(tp); 216 in_pcbdetach(inp); 217 in_pcbfree(inp); 218 } 219 220 #ifdef INET 221 /* 222 * Give the socket an address. 223 */ 224 static int 225 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 226 { 227 int error = 0; 228 struct inpcb *inp; 229 struct tcpcb *tp; 230 struct sockaddr_in *sinp; 231 232 inp = sotoinpcb(so); 233 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 234 INP_WLOCK(inp); 235 if (inp->inp_flags & INP_DROPPED) { 236 INP_WUNLOCK(inp); 237 return (EINVAL); 238 } 239 tp = intotcpcb(inp); 240 241 sinp = (struct sockaddr_in *)nam; 242 if (nam->sa_family != AF_INET) { 243 /* 244 * Preserve compatibility with old programs. 245 */ 246 if (nam->sa_family != AF_UNSPEC || 247 nam->sa_len < offsetof(struct sockaddr_in, sin_zero) || 248 sinp->sin_addr.s_addr != INADDR_ANY) { 249 error = EAFNOSUPPORT; 250 goto out; 251 } 252 nam->sa_family = AF_INET; 253 } 254 if (nam->sa_len != sizeof(*sinp)) { 255 error = EINVAL; 256 goto out; 257 } 258 /* 259 * Must check for multicast addresses and disallow binding 260 * to them. 261 */ 262 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 263 error = EAFNOSUPPORT; 264 goto out; 265 } 266 INP_HASH_WLOCK(&V_tcbinfo); 267 error = in_pcbbind(inp, sinp, td->td_ucred); 268 INP_HASH_WUNLOCK(&V_tcbinfo); 269 out: 270 tcp_bblog_pru(tp, PRU_BIND, error); 271 TCP_PROBE2(debug__user, tp, PRU_BIND); 272 INP_WUNLOCK(inp); 273 274 return (error); 275 } 276 #endif /* INET */ 277 278 #ifdef INET6 279 static int 280 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 281 { 282 int error = 0; 283 struct inpcb *inp; 284 struct tcpcb *tp; 285 struct sockaddr_in6 *sin6; 286 u_char vflagsav; 287 288 inp = sotoinpcb(so); 289 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 290 INP_WLOCK(inp); 291 if (inp->inp_flags & INP_DROPPED) { 292 INP_WUNLOCK(inp); 293 return (EINVAL); 294 } 295 tp = intotcpcb(inp); 296 297 vflagsav = inp->inp_vflag; 298 299 sin6 = (struct sockaddr_in6 *)nam; 300 if (nam->sa_family != AF_INET6) { 301 error = EAFNOSUPPORT; 302 goto out; 303 } 304 if (nam->sa_len != sizeof(*sin6)) { 305 error = EINVAL; 306 goto out; 307 } 308 /* 309 * Must check for multicast addresses and disallow binding 310 * to them. 311 */ 312 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 313 error = EAFNOSUPPORT; 314 goto out; 315 } 316 317 INP_HASH_WLOCK(&V_tcbinfo); 318 inp->inp_vflag &= ~INP_IPV4; 319 inp->inp_vflag |= INP_IPV6; 320 #ifdef INET 321 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 322 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 323 inp->inp_vflag |= INP_IPV4; 324 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 325 struct sockaddr_in sin; 326 327 in6_sin6_2_sin(&sin, sin6); 328 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 329 error = EAFNOSUPPORT; 330 INP_HASH_WUNLOCK(&V_tcbinfo); 331 goto out; 332 } 333 inp->inp_vflag |= INP_IPV4; 334 inp->inp_vflag &= ~INP_IPV6; 335 error = in_pcbbind(inp, &sin, td->td_ucred); 336 INP_HASH_WUNLOCK(&V_tcbinfo); 337 goto out; 338 } 339 } 340 #endif 341 error = in6_pcbbind(inp, sin6, td->td_ucred); 342 INP_HASH_WUNLOCK(&V_tcbinfo); 343 out: 344 if (error != 0) 345 inp->inp_vflag = vflagsav; 346 tcp_bblog_pru(tp, PRU_BIND, error); 347 TCP_PROBE2(debug__user, tp, PRU_BIND); 348 INP_WUNLOCK(inp); 349 return (error); 350 } 351 #endif /* INET6 */ 352 353 #ifdef INET 354 /* 355 * Prepare to accept connections. 356 */ 357 static int 358 tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 359 { 360 int error = 0; 361 struct inpcb *inp; 362 struct tcpcb *tp; 363 364 inp = sotoinpcb(so); 365 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 366 INP_WLOCK(inp); 367 if (inp->inp_flags & INP_DROPPED) { 368 INP_WUNLOCK(inp); 369 return (EINVAL); 370 } 371 tp = intotcpcb(inp); 372 373 SOCK_LOCK(so); 374 error = solisten_proto_check(so); 375 if (error != 0) { 376 SOCK_UNLOCK(so); 377 goto out; 378 } 379 if (inp->inp_lport == 0) { 380 INP_HASH_WLOCK(&V_tcbinfo); 381 error = in_pcbbind(inp, NULL, td->td_ucred); 382 INP_HASH_WUNLOCK(&V_tcbinfo); 383 } 384 if (error == 0) { 385 tcp_state_change(tp, TCPS_LISTEN); 386 solisten_proto(so, backlog); 387 #ifdef TCP_OFFLOAD 388 if ((so->so_options & SO_NO_OFFLOAD) == 0) 389 tcp_offload_listen_start(tp); 390 #endif 391 } else { 392 solisten_proto_abort(so); 393 } 394 SOCK_UNLOCK(so); 395 396 if (IS_FASTOPEN(tp->t_flags)) 397 tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 398 399 out: 400 tcp_bblog_pru(tp, PRU_LISTEN, error); 401 TCP_PROBE2(debug__user, tp, PRU_LISTEN); 402 INP_WUNLOCK(inp); 403 return (error); 404 } 405 #endif /* INET */ 406 407 #ifdef INET6 408 static int 409 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 410 { 411 int error = 0; 412 struct inpcb *inp; 413 struct tcpcb *tp; 414 u_char vflagsav; 415 416 inp = sotoinpcb(so); 417 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 418 INP_WLOCK(inp); 419 if (inp->inp_flags & INP_DROPPED) { 420 INP_WUNLOCK(inp); 421 return (EINVAL); 422 } 423 tp = intotcpcb(inp); 424 425 vflagsav = inp->inp_vflag; 426 427 SOCK_LOCK(so); 428 error = solisten_proto_check(so); 429 if (error != 0) { 430 SOCK_UNLOCK(so); 431 goto out; 432 } 433 INP_HASH_WLOCK(&V_tcbinfo); 434 if (inp->inp_lport == 0) { 435 inp->inp_vflag &= ~INP_IPV4; 436 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 437 inp->inp_vflag |= INP_IPV4; 438 error = in6_pcbbind(inp, NULL, td->td_ucred); 439 } 440 INP_HASH_WUNLOCK(&V_tcbinfo); 441 if (error == 0) { 442 tcp_state_change(tp, TCPS_LISTEN); 443 solisten_proto(so, backlog); 444 #ifdef TCP_OFFLOAD 445 if ((so->so_options & SO_NO_OFFLOAD) == 0) 446 tcp_offload_listen_start(tp); 447 #endif 448 } else { 449 solisten_proto_abort(so); 450 } 451 SOCK_UNLOCK(so); 452 453 if (IS_FASTOPEN(tp->t_flags)) 454 tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 455 456 if (error != 0) 457 inp->inp_vflag = vflagsav; 458 459 out: 460 tcp_bblog_pru(tp, PRU_LISTEN, error); 461 TCP_PROBE2(debug__user, tp, PRU_LISTEN); 462 INP_WUNLOCK(inp); 463 return (error); 464 } 465 #endif /* INET6 */ 466 467 #ifdef INET 468 /* 469 * Initiate connection to peer. 470 * Create a template for use in transmissions on this connection. 471 * Enter SYN_SENT state, and mark socket as connecting. 472 * Start keep-alive timer, and seed output sequence space. 473 * Send initial segment on connection. 474 */ 475 static int 476 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 477 { 478 struct epoch_tracker et; 479 int error = 0; 480 struct inpcb *inp; 481 struct tcpcb *tp; 482 struct sockaddr_in *sinp; 483 484 inp = sotoinpcb(so); 485 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 486 INP_WLOCK(inp); 487 if (inp->inp_flags & INP_DROPPED) { 488 INP_WUNLOCK(inp); 489 return (ECONNREFUSED); 490 } 491 tp = intotcpcb(inp); 492 493 sinp = (struct sockaddr_in *)nam; 494 if (nam->sa_family != AF_INET) { 495 error = EAFNOSUPPORT; 496 goto out; 497 } 498 if (nam->sa_len != sizeof (*sinp)) { 499 error = EINVAL; 500 goto out; 501 } 502 /* 503 * Must disallow TCP ``connections'' to multicast addresses. 504 */ 505 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 506 error = EAFNOSUPPORT; 507 goto out; 508 } 509 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 510 error = EACCES; 511 goto out; 512 } 513 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 514 goto out; 515 if (SOLISTENING(so)) { 516 error = EOPNOTSUPP; 517 goto out; 518 } 519 NET_EPOCH_ENTER(et); 520 if ((error = tcp_connect(tp, sinp, td)) != 0) 521 goto out_in_epoch; 522 #ifdef TCP_OFFLOAD 523 if (registered_toedevs > 0 && 524 (so->so_options & SO_NO_OFFLOAD) == 0 && 525 (error = tcp_offload_connect(so, nam)) == 0) 526 goto out_in_epoch; 527 #endif 528 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 529 error = tcp_output(tp); 530 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()" 531 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error)); 532 out_in_epoch: 533 NET_EPOCH_EXIT(et); 534 out: 535 tcp_bblog_pru(tp, PRU_CONNECT, error); 536 TCP_PROBE2(debug__user, tp, PRU_CONNECT); 537 INP_WUNLOCK(inp); 538 return (error); 539 } 540 #endif /* INET */ 541 542 #ifdef INET6 543 static int 544 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 545 { 546 struct epoch_tracker et; 547 int error = 0; 548 struct inpcb *inp; 549 struct tcpcb *tp; 550 struct sockaddr_in6 *sin6; 551 u_int8_t incflagsav; 552 u_char vflagsav; 553 554 inp = sotoinpcb(so); 555 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 556 INP_WLOCK(inp); 557 if (inp->inp_flags & INP_DROPPED) { 558 INP_WUNLOCK(inp); 559 return (ECONNREFUSED); 560 } 561 tp = intotcpcb(inp); 562 563 vflagsav = inp->inp_vflag; 564 incflagsav = inp->inp_inc.inc_flags; 565 566 sin6 = (struct sockaddr_in6 *)nam; 567 if (nam->sa_family != AF_INET6) { 568 error = EAFNOSUPPORT; 569 goto out; 570 } 571 if (nam->sa_len != sizeof (*sin6)) { 572 error = EINVAL; 573 goto out; 574 } 575 /* 576 * Must disallow TCP ``connections'' to multicast addresses. 577 */ 578 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 579 error = EAFNOSUPPORT; 580 goto out; 581 } 582 if (SOLISTENING(so)) { 583 error = EINVAL; 584 goto out; 585 } 586 #ifdef INET 587 /* 588 * XXXRW: Some confusion: V4/V6 flags relate to binding, and 589 * therefore probably require the hash lock, which isn't held here. 590 * Is this a significant problem? 591 */ 592 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 593 struct sockaddr_in sin; 594 595 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 596 error = EINVAL; 597 goto out; 598 } 599 if ((inp->inp_vflag & INP_IPV4) == 0) { 600 error = EAFNOSUPPORT; 601 goto out; 602 } 603 604 in6_sin6_2_sin(&sin, sin6); 605 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 606 error = EAFNOSUPPORT; 607 goto out; 608 } 609 if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) { 610 error = EACCES; 611 goto out; 612 } 613 if ((error = prison_remote_ip4(td->td_ucred, 614 &sin.sin_addr)) != 0) 615 goto out; 616 inp->inp_vflag |= INP_IPV4; 617 inp->inp_vflag &= ~INP_IPV6; 618 NET_EPOCH_ENTER(et); 619 if ((error = tcp_connect(tp, &sin, td)) != 0) 620 goto out_in_epoch; 621 #ifdef TCP_OFFLOAD 622 if (registered_toedevs > 0 && 623 (so->so_options & SO_NO_OFFLOAD) == 0 && 624 (error = tcp_offload_connect(so, nam)) == 0) 625 goto out_in_epoch; 626 #endif 627 error = tcp_output(tp); 628 goto out_in_epoch; 629 } else { 630 if ((inp->inp_vflag & INP_IPV6) == 0) { 631 error = EAFNOSUPPORT; 632 goto out; 633 } 634 } 635 #endif 636 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 637 goto out; 638 inp->inp_vflag &= ~INP_IPV4; 639 inp->inp_vflag |= INP_IPV6; 640 inp->inp_inc.inc_flags |= INC_ISIPV6; 641 NET_EPOCH_ENTER(et); 642 if ((error = tcp6_connect(tp, sin6, td)) != 0) 643 goto out_in_epoch; 644 #ifdef TCP_OFFLOAD 645 if (registered_toedevs > 0 && 646 (so->so_options & SO_NO_OFFLOAD) == 0 && 647 (error = tcp_offload_connect(so, nam)) == 0) 648 goto out_in_epoch; 649 #endif 650 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 651 error = tcp_output(tp); 652 out_in_epoch: 653 NET_EPOCH_EXIT(et); 654 out: 655 KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()" 656 ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error)); 657 /* 658 * If the implicit bind in the connect call fails, restore 659 * the flags we modified. 660 */ 661 if (error != 0 && inp->inp_lport == 0) { 662 inp->inp_vflag = vflagsav; 663 inp->inp_inc.inc_flags = incflagsav; 664 } 665 666 tcp_bblog_pru(tp, PRU_CONNECT, error); 667 TCP_PROBE2(debug__user, tp, PRU_CONNECT); 668 INP_WUNLOCK(inp); 669 return (error); 670 } 671 #endif /* INET6 */ 672 673 /* 674 * Initiate disconnect from peer. 675 * If connection never passed embryonic stage, just drop; 676 * else if don't need to let data drain, then can just drop anyways, 677 * else have to begin TCP shutdown process: mark socket disconnecting, 678 * drain unread data, state switch to reflect user close, and 679 * send segment (e.g. FIN) to peer. Socket will be really disconnected 680 * when peer sends FIN and acks ours. 681 * 682 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 683 */ 684 static int 685 tcp_usr_disconnect(struct socket *so) 686 { 687 struct inpcb *inp; 688 struct tcpcb *tp = NULL; 689 struct epoch_tracker et; 690 int error = 0; 691 692 NET_EPOCH_ENTER(et); 693 inp = sotoinpcb(so); 694 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 695 INP_WLOCK(inp); 696 if (inp->inp_flags & INP_DROPPED) { 697 INP_WUNLOCK(inp); 698 NET_EPOCH_EXIT(et); 699 return (ECONNRESET); 700 } 701 tp = intotcpcb(inp); 702 703 if (tp->t_state == TCPS_TIME_WAIT) 704 goto out; 705 tcp_disconnect(tp); 706 out: 707 tcp_bblog_pru(tp, PRU_DISCONNECT, error); 708 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 709 INP_WUNLOCK(inp); 710 NET_EPOCH_EXIT(et); 711 return (error); 712 } 713 714 #ifdef INET 715 /* 716 * Accept a connection. Essentially all the work is done at higher levels; 717 * just return the address of the peer, storing through addr. 718 */ 719 static int 720 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 721 { 722 int error = 0; 723 struct inpcb *inp; 724 struct tcpcb *tp; 725 struct in_addr addr; 726 in_port_t port = 0; 727 728 inp = sotoinpcb(so); 729 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 730 INP_WLOCK(inp); 731 if (inp->inp_flags & INP_DROPPED) { 732 INP_WUNLOCK(inp); 733 return (ECONNABORTED); 734 } 735 tp = intotcpcb(inp); 736 737 if (so->so_state & SS_ISDISCONNECTED) { 738 error = ECONNABORTED; 739 goto out; 740 } 741 /* 742 * We inline in_getpeeraddr and COMMON_END here, so that we can 743 * copy the data of interest and defer the malloc until after we 744 * release the lock. 745 */ 746 port = inp->inp_fport; 747 addr = inp->inp_faddr; 748 749 out: 750 tcp_bblog_pru(tp, PRU_ACCEPT, error); 751 TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 752 INP_WUNLOCK(inp); 753 if (error == 0) 754 *nam = in_sockaddr(port, &addr); 755 return error; 756 } 757 #endif /* INET */ 758 759 #ifdef INET6 760 static int 761 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 762 { 763 struct inpcb *inp; 764 int error = 0; 765 struct tcpcb *tp; 766 struct in_addr addr; 767 struct in6_addr addr6; 768 in_port_t port = 0; 769 int v4 = 0; 770 771 inp = sotoinpcb(so); 772 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 773 INP_WLOCK(inp); 774 if (inp->inp_flags & INP_DROPPED) { 775 INP_WUNLOCK(inp); 776 return (ECONNABORTED); 777 } 778 tp = intotcpcb(inp); 779 780 if (so->so_state & SS_ISDISCONNECTED) { 781 error = ECONNABORTED; 782 goto out; 783 } 784 /* 785 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 786 * copy the data of interest and defer the malloc until after we 787 * release the lock. 788 */ 789 if (inp->inp_vflag & INP_IPV4) { 790 v4 = 1; 791 port = inp->inp_fport; 792 addr = inp->inp_faddr; 793 } else { 794 port = inp->inp_fport; 795 addr6 = inp->in6p_faddr; 796 } 797 798 out: 799 tcp_bblog_pru(tp, PRU_ACCEPT, error); 800 TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 801 INP_WUNLOCK(inp); 802 if (error == 0) { 803 if (v4) 804 *nam = in6_v4mapsin6_sockaddr(port, &addr); 805 else 806 *nam = in6_sockaddr(port, &addr6); 807 } 808 return error; 809 } 810 #endif /* INET6 */ 811 812 /* 813 * Mark the connection as being incapable of further output. 814 */ 815 static int 816 tcp_usr_shutdown(struct socket *so) 817 { 818 int error = 0; 819 struct inpcb *inp; 820 struct tcpcb *tp; 821 struct epoch_tracker et; 822 823 inp = sotoinpcb(so); 824 KASSERT(inp != NULL, ("inp == NULL")); 825 INP_WLOCK(inp); 826 if (inp->inp_flags & INP_DROPPED) { 827 INP_WUNLOCK(inp); 828 return (ECONNRESET); 829 } 830 tp = intotcpcb(inp); 831 832 NET_EPOCH_ENTER(et); 833 socantsendmore(so); 834 tcp_usrclosed(tp); 835 if (!(inp->inp_flags & INP_DROPPED)) 836 error = tcp_output_nodrop(tp); 837 tcp_bblog_pru(tp, PRU_SHUTDOWN, error); 838 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 839 error = tcp_unlock_or_drop(tp, error); 840 NET_EPOCH_EXIT(et); 841 842 return (error); 843 } 844 845 /* 846 * After a receive, possibly send window update to peer. 847 */ 848 static int 849 tcp_usr_rcvd(struct socket *so, int flags) 850 { 851 struct epoch_tracker et; 852 struct inpcb *inp; 853 struct tcpcb *tp; 854 int outrv = 0, error = 0; 855 856 inp = sotoinpcb(so); 857 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 858 INP_WLOCK(inp); 859 if (inp->inp_flags & INP_DROPPED) { 860 INP_WUNLOCK(inp); 861 return (ECONNRESET); 862 } 863 tp = intotcpcb(inp); 864 865 NET_EPOCH_ENTER(et); 866 /* 867 * For passively-created TFO connections, don't attempt a window 868 * update while still in SYN_RECEIVED as this may trigger an early 869 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 870 * application response data, or failing that, when the DELACK timer 871 * expires. 872 */ 873 if (IS_FASTOPEN(tp->t_flags) && 874 (tp->t_state == TCPS_SYN_RECEIVED)) 875 goto out; 876 #ifdef TCP_OFFLOAD 877 if (tp->t_flags & TF_TOE) 878 tcp_offload_rcvd(tp); 879 else 880 #endif 881 outrv = tcp_output_nodrop(tp); 882 out: 883 tcp_bblog_pru(tp, PRU_RCVD, error); 884 TCP_PROBE2(debug__user, tp, PRU_RCVD); 885 (void) tcp_unlock_or_drop(tp, outrv); 886 NET_EPOCH_EXIT(et); 887 return (error); 888 } 889 890 /* 891 * Do a send by putting data in output queue and updating urgent 892 * marker if URG set. Possibly send more data. Unlike the other 893 * pru_*() routines, the mbuf chains are our responsibility. We 894 * must either enqueue them or free them. The other pru_* routines 895 * generally are caller-frees. 896 */ 897 static int 898 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 899 struct sockaddr *nam, struct mbuf *control, struct thread *td) 900 { 901 struct epoch_tracker et; 902 int error = 0; 903 struct inpcb *inp; 904 struct tcpcb *tp; 905 #ifdef INET 906 #ifdef INET6 907 struct sockaddr_in sin; 908 #endif 909 struct sockaddr_in *sinp; 910 #endif 911 #ifdef INET6 912 struct sockaddr_in6 *sin6; 913 int isipv6; 914 #endif 915 u_int8_t incflagsav; 916 u_char vflagsav; 917 bool restoreflags; 918 919 inp = sotoinpcb(so); 920 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 921 INP_WLOCK(inp); 922 if (inp->inp_flags & INP_DROPPED) { 923 if (m != NULL && (flags & PRUS_NOTREADY) == 0) 924 m_freem(m); 925 INP_WUNLOCK(inp); 926 return (ECONNRESET); 927 } 928 tp = intotcpcb(inp); 929 930 vflagsav = inp->inp_vflag; 931 incflagsav = inp->inp_inc.inc_flags; 932 restoreflags = false; 933 934 NET_EPOCH_ENTER(et); 935 if (control != NULL) { 936 /* TCP doesn't do control messages (rights, creds, etc) */ 937 if (control->m_len > 0) { 938 m_freem(control); 939 error = EINVAL; 940 goto out; 941 } 942 m_freem(control); /* empty control, just free it */ 943 } 944 945 if ((flags & PRUS_OOB) != 0 && 946 (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) 947 goto out; 948 949 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 950 if (tp->t_state == TCPS_LISTEN) { 951 error = EINVAL; 952 goto out; 953 } 954 switch (nam->sa_family) { 955 #ifdef INET 956 case AF_INET: 957 sinp = (struct sockaddr_in *)nam; 958 if (sinp->sin_len != sizeof(struct sockaddr_in)) { 959 error = EINVAL; 960 goto out; 961 } 962 if ((inp->inp_vflag & INP_IPV6) != 0) { 963 error = EAFNOSUPPORT; 964 goto out; 965 } 966 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 967 error = EAFNOSUPPORT; 968 goto out; 969 } 970 if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) { 971 error = EACCES; 972 goto out; 973 } 974 if ((error = prison_remote_ip4(td->td_ucred, 975 &sinp->sin_addr))) 976 goto out; 977 #ifdef INET6 978 isipv6 = 0; 979 #endif 980 break; 981 #endif /* INET */ 982 #ifdef INET6 983 case AF_INET6: 984 sin6 = (struct sockaddr_in6 *)nam; 985 if (sin6->sin6_len != sizeof(*sin6)) { 986 error = EINVAL; 987 goto out; 988 } 989 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 990 error = EAFNOSUPPORT; 991 goto out; 992 } 993 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 994 error = EAFNOSUPPORT; 995 goto out; 996 } 997 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 998 #ifdef INET 999 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 1000 error = EINVAL; 1001 goto out; 1002 } 1003 if ((inp->inp_vflag & INP_IPV4) == 0) { 1004 error = EAFNOSUPPORT; 1005 goto out; 1006 } 1007 restoreflags = true; 1008 inp->inp_vflag &= ~INP_IPV6; 1009 sinp = &sin; 1010 in6_sin6_2_sin(sinp, sin6); 1011 if (IN_MULTICAST( 1012 ntohl(sinp->sin_addr.s_addr))) { 1013 error = EAFNOSUPPORT; 1014 goto out; 1015 } 1016 if ((error = prison_remote_ip4(td->td_ucred, 1017 &sinp->sin_addr))) 1018 goto out; 1019 isipv6 = 0; 1020 #else /* !INET */ 1021 error = EAFNOSUPPORT; 1022 goto out; 1023 #endif /* INET */ 1024 } else { 1025 if ((inp->inp_vflag & INP_IPV6) == 0) { 1026 error = EAFNOSUPPORT; 1027 goto out; 1028 } 1029 restoreflags = true; 1030 inp->inp_vflag &= ~INP_IPV4; 1031 inp->inp_inc.inc_flags |= INC_ISIPV6; 1032 if ((error = prison_remote_ip6(td->td_ucred, 1033 &sin6->sin6_addr))) 1034 goto out; 1035 isipv6 = 1; 1036 } 1037 break; 1038 #endif /* INET6 */ 1039 default: 1040 error = EAFNOSUPPORT; 1041 goto out; 1042 } 1043 } 1044 if (!(flags & PRUS_OOB)) { 1045 if (tp->t_acktime == 0) 1046 tp->t_acktime = ticks; 1047 sbappendstream(&so->so_snd, m, flags); 1048 m = NULL; 1049 if (nam && tp->t_state < TCPS_SYN_SENT) { 1050 KASSERT(tp->t_state == TCPS_CLOSED, 1051 ("%s: tp %p is listening", __func__, tp)); 1052 1053 /* 1054 * Do implied connect if not yet connected, 1055 * initialize window to default value, and 1056 * initialize maxseg using peer's cached MSS. 1057 */ 1058 #ifdef INET6 1059 if (isipv6) 1060 error = tcp6_connect(tp, sin6, td); 1061 #endif /* INET6 */ 1062 #if defined(INET6) && defined(INET) 1063 else 1064 #endif 1065 #ifdef INET 1066 error = tcp_connect(tp, sinp, td); 1067 #endif 1068 /* 1069 * The bind operation in tcp_connect succeeded. We 1070 * no longer want to restore the flags if later 1071 * operations fail. 1072 */ 1073 if (error == 0 || inp->inp_lport != 0) 1074 restoreflags = false; 1075 1076 if (error) { 1077 /* m is freed if PRUS_NOTREADY is unset. */ 1078 sbflush(&so->so_snd); 1079 goto out; 1080 } 1081 if (IS_FASTOPEN(tp->t_flags)) 1082 tcp_fastopen_connect(tp); 1083 else { 1084 tp->snd_wnd = TTCP_CLIENT_SND_WND; 1085 tcp_mss(tp, -1); 1086 } 1087 } 1088 if (flags & PRUS_EOF) { 1089 /* 1090 * Close the send side of the connection after 1091 * the data is sent. 1092 */ 1093 socantsendmore(so); 1094 tcp_usrclosed(tp); 1095 } 1096 if (TCPS_HAVEESTABLISHED(tp->t_state) && 1097 ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) && 1098 (tp->t_fbyte_out == 0) && 1099 (so->so_snd.sb_ccc > 0)) { 1100 tp->t_fbyte_out = ticks; 1101 if (tp->t_fbyte_out == 0) 1102 tp->t_fbyte_out = 1; 1103 if (tp->t_fbyte_out && tp->t_fbyte_in) 1104 tp->t_flags2 |= TF2_FBYTES_COMPLETE; 1105 } 1106 if (!(inp->inp_flags & INP_DROPPED) && 1107 !(flags & PRUS_NOTREADY)) { 1108 if (flags & PRUS_MORETOCOME) 1109 tp->t_flags |= TF_MORETOCOME; 1110 error = tcp_output_nodrop(tp); 1111 if (flags & PRUS_MORETOCOME) 1112 tp->t_flags &= ~TF_MORETOCOME; 1113 } 1114 } else { 1115 /* 1116 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1117 */ 1118 SOCKBUF_LOCK(&so->so_snd); 1119 if (sbspace(&so->so_snd) < -512) { 1120 SOCKBUF_UNLOCK(&so->so_snd); 1121 error = ENOBUFS; 1122 goto out; 1123 } 1124 /* 1125 * According to RFC961 (Assigned Protocols), 1126 * the urgent pointer points to the last octet 1127 * of urgent data. We continue, however, 1128 * to consider it to indicate the first octet 1129 * of data past the urgent section. 1130 * Otherwise, snd_up should be one lower. 1131 */ 1132 if (tp->t_acktime == 0) 1133 tp->t_acktime = ticks; 1134 sbappendstream_locked(&so->so_snd, m, flags); 1135 SOCKBUF_UNLOCK(&so->so_snd); 1136 m = NULL; 1137 if (nam && tp->t_state < TCPS_SYN_SENT) { 1138 /* 1139 * Do implied connect if not yet connected, 1140 * initialize window to default value, and 1141 * initialize maxseg using peer's cached MSS. 1142 */ 1143 1144 /* 1145 * Not going to contemplate SYN|URG 1146 */ 1147 if (IS_FASTOPEN(tp->t_flags)) 1148 tp->t_flags &= ~TF_FASTOPEN; 1149 #ifdef INET6 1150 if (isipv6) 1151 error = tcp6_connect(tp, sin6, td); 1152 #endif /* INET6 */ 1153 #if defined(INET6) && defined(INET) 1154 else 1155 #endif 1156 #ifdef INET 1157 error = tcp_connect(tp, sinp, td); 1158 #endif 1159 /* 1160 * The bind operation in tcp_connect succeeded. We 1161 * no longer want to restore the flags if later 1162 * operations fail. 1163 */ 1164 if (error == 0 || inp->inp_lport != 0) 1165 restoreflags = false; 1166 1167 if (error != 0) { 1168 /* m is freed if PRUS_NOTREADY is unset. */ 1169 sbflush(&so->so_snd); 1170 goto out; 1171 } 1172 tp->snd_wnd = TTCP_CLIENT_SND_WND; 1173 tcp_mss(tp, -1); 1174 } 1175 tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 1176 if ((flags & PRUS_NOTREADY) == 0) { 1177 tp->t_flags |= TF_FORCEDATA; 1178 error = tcp_output_nodrop(tp); 1179 tp->t_flags &= ~TF_FORCEDATA; 1180 } 1181 } 1182 TCP_LOG_EVENT(tp, NULL, 1183 &inp->inp_socket->so_rcv, 1184 &inp->inp_socket->so_snd, 1185 TCP_LOG_USERSEND, error, 1186 0, NULL, false); 1187 1188 out: 1189 /* 1190 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is 1191 * responsible for freeing memory. 1192 */ 1193 if (m != NULL && (flags & PRUS_NOTREADY) == 0) 1194 m_freem(m); 1195 1196 /* 1197 * If the request was unsuccessful and we changed flags, 1198 * restore the original flags. 1199 */ 1200 if (error != 0 && restoreflags) { 1201 inp->inp_vflag = vflagsav; 1202 inp->inp_inc.inc_flags = incflagsav; 1203 } 1204 tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 1205 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error); 1206 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 1207 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1208 error = tcp_unlock_or_drop(tp, error); 1209 NET_EPOCH_EXIT(et); 1210 return (error); 1211 } 1212 1213 static int 1214 tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 1215 { 1216 struct epoch_tracker et; 1217 struct inpcb *inp; 1218 struct tcpcb *tp; 1219 int error; 1220 1221 inp = sotoinpcb(so); 1222 INP_WLOCK(inp); 1223 if (inp->inp_flags & INP_DROPPED) { 1224 INP_WUNLOCK(inp); 1225 mb_free_notready(m, count); 1226 return (ECONNRESET); 1227 } 1228 tp = intotcpcb(inp); 1229 1230 SOCKBUF_LOCK(&so->so_snd); 1231 error = sbready(&so->so_snd, m, count); 1232 SOCKBUF_UNLOCK(&so->so_snd); 1233 if (error) { 1234 INP_WUNLOCK(inp); 1235 return (error); 1236 } 1237 NET_EPOCH_ENTER(et); 1238 error = tcp_output_unlock(tp); 1239 NET_EPOCH_EXIT(et); 1240 1241 return (error); 1242 } 1243 1244 /* 1245 * Abort the TCP. Drop the connection abruptly. 1246 */ 1247 static void 1248 tcp_usr_abort(struct socket *so) 1249 { 1250 struct inpcb *inp; 1251 struct tcpcb *tp; 1252 struct epoch_tracker et; 1253 1254 inp = sotoinpcb(so); 1255 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1256 1257 NET_EPOCH_ENTER(et); 1258 INP_WLOCK(inp); 1259 KASSERT(inp->inp_socket != NULL, 1260 ("tcp_usr_abort: inp_socket == NULL")); 1261 1262 /* 1263 * If we still have full TCP state, and we're not dropped, drop. 1264 */ 1265 if (!(inp->inp_flags & INP_DROPPED)) { 1266 tp = intotcpcb(inp); 1267 tp = tcp_drop(tp, ECONNABORTED); 1268 if (tp == NULL) 1269 goto dropped; 1270 tcp_bblog_pru(tp, PRU_ABORT, 0); 1271 TCP_PROBE2(debug__user, tp, PRU_ABORT); 1272 } 1273 if (!(inp->inp_flags & INP_DROPPED)) { 1274 soref(so); 1275 inp->inp_flags |= INP_SOCKREF; 1276 } 1277 INP_WUNLOCK(inp); 1278 dropped: 1279 NET_EPOCH_EXIT(et); 1280 } 1281 1282 /* 1283 * TCP socket is closed. Start friendly disconnect. 1284 */ 1285 static void 1286 tcp_usr_close(struct socket *so) 1287 { 1288 struct inpcb *inp; 1289 struct tcpcb *tp; 1290 struct epoch_tracker et; 1291 1292 inp = sotoinpcb(so); 1293 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1294 1295 NET_EPOCH_ENTER(et); 1296 INP_WLOCK(inp); 1297 KASSERT(inp->inp_socket != NULL, 1298 ("tcp_usr_close: inp_socket == NULL")); 1299 1300 /* 1301 * If we are still connected and we're not dropped, initiate 1302 * a disconnect. 1303 */ 1304 if (!(inp->inp_flags & INP_DROPPED)) { 1305 tp = intotcpcb(inp); 1306 if (tp->t_state != TCPS_TIME_WAIT) { 1307 tp->t_flags |= TF_CLOSED; 1308 tcp_disconnect(tp); 1309 tcp_bblog_pru(tp, PRU_CLOSE, 0); 1310 TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1311 } 1312 } 1313 if (!(inp->inp_flags & INP_DROPPED)) { 1314 soref(so); 1315 inp->inp_flags |= INP_SOCKREF; 1316 } 1317 INP_WUNLOCK(inp); 1318 NET_EPOCH_EXIT(et); 1319 } 1320 1321 static int 1322 tcp_pru_options_support(struct tcpcb *tp, int flags) 1323 { 1324 /* 1325 * If the specific TCP stack has a pru_options 1326 * specified then it does not always support 1327 * all the PRU_XX options and we must ask it. 1328 * If the function is not specified then all 1329 * of the PRU_XX options are supported. 1330 */ 1331 int ret = 0; 1332 1333 if (tp->t_fb->tfb_pru_options) { 1334 ret = (*tp->t_fb->tfb_pru_options)(tp, flags); 1335 } 1336 return (ret); 1337 } 1338 1339 /* 1340 * Receive out-of-band data. 1341 */ 1342 static int 1343 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 1344 { 1345 int error = 0; 1346 struct inpcb *inp; 1347 struct tcpcb *tp; 1348 1349 inp = sotoinpcb(so); 1350 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 1351 INP_WLOCK(inp); 1352 if (inp->inp_flags & INP_DROPPED) { 1353 INP_WUNLOCK(inp); 1354 return (ECONNRESET); 1355 } 1356 tp = intotcpcb(inp); 1357 1358 error = tcp_pru_options_support(tp, PRUS_OOB); 1359 if (error) { 1360 goto out; 1361 } 1362 if ((so->so_oobmark == 0 && 1363 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 1364 so->so_options & SO_OOBINLINE || 1365 tp->t_oobflags & TCPOOB_HADDATA) { 1366 error = EINVAL; 1367 goto out; 1368 } 1369 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 1370 error = EWOULDBLOCK; 1371 goto out; 1372 } 1373 m->m_len = 1; 1374 *mtod(m, caddr_t) = tp->t_iobc; 1375 if ((flags & MSG_PEEK) == 0) 1376 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1377 1378 out: 1379 tcp_bblog_pru(tp, PRU_RCVOOB, error); 1380 TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 1381 INP_WUNLOCK(inp); 1382 return (error); 1383 } 1384 1385 #ifdef INET 1386 struct protosw tcp_protosw = { 1387 .pr_type = SOCK_STREAM, 1388 .pr_protocol = IPPROTO_TCP, 1389 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD | 1390 PR_CAPATTACH, 1391 .pr_ctloutput = tcp_ctloutput, 1392 .pr_abort = tcp_usr_abort, 1393 .pr_accept = tcp_usr_accept, 1394 .pr_attach = tcp_usr_attach, 1395 .pr_bind = tcp_usr_bind, 1396 .pr_connect = tcp_usr_connect, 1397 .pr_control = in_control, 1398 .pr_detach = tcp_usr_detach, 1399 .pr_disconnect = tcp_usr_disconnect, 1400 .pr_listen = tcp_usr_listen, 1401 .pr_peeraddr = in_getpeeraddr, 1402 .pr_rcvd = tcp_usr_rcvd, 1403 .pr_rcvoob = tcp_usr_rcvoob, 1404 .pr_send = tcp_usr_send, 1405 .pr_ready = tcp_usr_ready, 1406 .pr_shutdown = tcp_usr_shutdown, 1407 .pr_sockaddr = in_getsockaddr, 1408 .pr_sosetlabel = in_pcbsosetlabel, 1409 .pr_close = tcp_usr_close, 1410 }; 1411 #endif /* INET */ 1412 1413 #ifdef INET6 1414 struct protosw tcp6_protosw = { 1415 .pr_type = SOCK_STREAM, 1416 .pr_protocol = IPPROTO_TCP, 1417 .pr_flags = PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD | 1418 PR_CAPATTACH, 1419 .pr_ctloutput = tcp_ctloutput, 1420 .pr_abort = tcp_usr_abort, 1421 .pr_accept = tcp6_usr_accept, 1422 .pr_attach = tcp_usr_attach, 1423 .pr_bind = tcp6_usr_bind, 1424 .pr_connect = tcp6_usr_connect, 1425 .pr_control = in6_control, 1426 .pr_detach = tcp_usr_detach, 1427 .pr_disconnect = tcp_usr_disconnect, 1428 .pr_listen = tcp6_usr_listen, 1429 .pr_peeraddr = in6_mapped_peeraddr, 1430 .pr_rcvd = tcp_usr_rcvd, 1431 .pr_rcvoob = tcp_usr_rcvoob, 1432 .pr_send = tcp_usr_send, 1433 .pr_ready = tcp_usr_ready, 1434 .pr_shutdown = tcp_usr_shutdown, 1435 .pr_sockaddr = in6_mapped_sockaddr, 1436 .pr_sosetlabel = in_pcbsosetlabel, 1437 .pr_close = tcp_usr_close, 1438 }; 1439 #endif /* INET6 */ 1440 1441 #ifdef INET 1442 /* 1443 * Common subroutine to open a TCP connection to remote host specified 1444 * by struct sockaddr_in. Call in_pcbconnect() to choose local host address 1445 * and assign a local port number and install the inpcb into the hash. 1446 * Initialize connection parameters and enter SYN-SENT state. 1447 */ 1448 static int 1449 tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td) 1450 { 1451 struct inpcb *inp = tptoinpcb(tp); 1452 struct socket *so = tptosocket(tp); 1453 int error; 1454 1455 NET_EPOCH_ASSERT(); 1456 INP_WLOCK_ASSERT(inp); 1457 1458 if (__predict_false((so->so_state & 1459 (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING | 1460 SS_ISDISCONNECTED)) != 0)) 1461 return (EISCONN); 1462 1463 INP_HASH_WLOCK(&V_tcbinfo); 1464 error = in_pcbconnect(inp, sin, td->td_ucred, true); 1465 INP_HASH_WUNLOCK(&V_tcbinfo); 1466 if (error != 0) 1467 return (error); 1468 1469 /* 1470 * Compute window scaling to request: 1471 * Scale to fit into sweet spot. See tcp_syncache.c. 1472 * XXX: This should move to tcp_output(). 1473 */ 1474 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1475 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1476 tp->request_r_scale++; 1477 1478 soisconnecting(so); 1479 TCPSTAT_INC(tcps_connattempt); 1480 tcp_state_change(tp, TCPS_SYN_SENT); 1481 tp->iss = tcp_new_isn(&inp->inp_inc); 1482 if (tp->t_flags & TF_REQ_TSTMP) 1483 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1484 tcp_sendseqinit(tp); 1485 1486 return (0); 1487 } 1488 #endif /* INET */ 1489 1490 #ifdef INET6 1491 static int 1492 tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td) 1493 { 1494 struct inpcb *inp = tptoinpcb(tp); 1495 struct socket *so = tptosocket(tp); 1496 int error; 1497 1498 NET_EPOCH_ASSERT(); 1499 INP_WLOCK_ASSERT(inp); 1500 1501 if (__predict_false((so->so_state & 1502 (SS_ISCONNECTING | SS_ISCONNECTED)) != 0)) 1503 return (EISCONN); 1504 1505 INP_HASH_WLOCK(&V_tcbinfo); 1506 error = in6_pcbconnect(inp, sin6, td->td_ucred, true); 1507 INP_HASH_WUNLOCK(&V_tcbinfo); 1508 if (error != 0) 1509 return (error); 1510 1511 /* Compute window scaling to request. */ 1512 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1513 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1514 tp->request_r_scale++; 1515 1516 soisconnecting(so); 1517 TCPSTAT_INC(tcps_connattempt); 1518 tcp_state_change(tp, TCPS_SYN_SENT); 1519 tp->iss = tcp_new_isn(&inp->inp_inc); 1520 if (tp->t_flags & TF_REQ_TSTMP) 1521 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1522 tcp_sendseqinit(tp); 1523 1524 return (0); 1525 } 1526 #endif /* INET6 */ 1527 1528 /* 1529 * Export TCP internal state information via a struct tcp_info, based on the 1530 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1531 * (TCP state machine, etc). We export all information using FreeBSD-native 1532 * constants -- for example, the numeric values for tcpi_state will differ 1533 * from Linux. 1534 */ 1535 void 1536 tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti) 1537 { 1538 1539 INP_LOCK_ASSERT(tptoinpcb(tp)); 1540 bzero(ti, sizeof(*ti)); 1541 1542 ti->tcpi_state = tp->t_state; 1543 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1544 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1545 if (tp->t_flags & TF_SACK_PERMIT) 1546 ti->tcpi_options |= TCPI_OPT_SACK; 1547 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1548 ti->tcpi_options |= TCPI_OPT_WSCALE; 1549 ti->tcpi_snd_wscale = tp->snd_scale; 1550 ti->tcpi_rcv_wscale = tp->rcv_scale; 1551 } 1552 switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) { 1553 case TF2_ECN_PERMIT: 1554 ti->tcpi_options |= TCPI_OPT_ECN; 1555 break; 1556 case TF2_ACE_PERMIT: 1557 /* FALLTHROUGH */ 1558 case TF2_ECN_PERMIT | TF2_ACE_PERMIT: 1559 ti->tcpi_options |= TCPI_OPT_ACE; 1560 break; 1561 default: 1562 break; 1563 } 1564 if (IS_FASTOPEN(tp->t_flags)) 1565 ti->tcpi_options |= TCPI_OPT_TFO; 1566 1567 ti->tcpi_rto = tp->t_rxtcur * tick; 1568 ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick; 1569 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 1570 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 1571 1572 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1573 ti->tcpi_snd_cwnd = tp->snd_cwnd; 1574 1575 /* 1576 * FreeBSD-specific extension fields for tcp_info. 1577 */ 1578 ti->tcpi_rcv_space = tp->rcv_wnd; 1579 ti->tcpi_rcv_nxt = tp->rcv_nxt; 1580 ti->tcpi_snd_wnd = tp->snd_wnd; 1581 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1582 ti->tcpi_snd_nxt = tp->snd_nxt; 1583 ti->tcpi_snd_mss = tp->t_maxseg; 1584 ti->tcpi_rcv_mss = tp->t_maxseg; 1585 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1586 ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1587 ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1588 ti->tcpi_snd_una = tp->snd_una; 1589 ti->tcpi_snd_max = tp->snd_max; 1590 ti->tcpi_rcv_numsacks = tp->rcv_numsacks; 1591 ti->tcpi_rcv_adv = tp->rcv_adv; 1592 ti->tcpi_dupacks = tp->t_dupacks; 1593 #ifdef TCP_OFFLOAD 1594 if (tp->t_flags & TF_TOE) { 1595 ti->tcpi_options |= TCPI_OPT_TOE; 1596 tcp_offload_tcp_info(tp, ti); 1597 } 1598 #endif 1599 /* 1600 * AccECN related counters. 1601 */ 1602 if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) == 1603 (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) 1604 /* 1605 * Internal counter starts at 5 for AccECN 1606 * but 0 for RFC3168 ECN. 1607 */ 1608 ti->tcpi_delivered_ce = tp->t_scep - 5; 1609 else 1610 ti->tcpi_delivered_ce = tp->t_scep; 1611 ti->tcpi_received_ce = tp->t_rcep; 1612 } 1613 1614 /* 1615 * tcp_ctloutput() must drop the inpcb lock before performing copyin on 1616 * socket option arguments. When it re-acquires the lock after the copy, it 1617 * has to revalidate that the connection is still valid for the socket 1618 * option. 1619 */ 1620 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 1621 INP_WLOCK(inp); \ 1622 if (inp->inp_flags & INP_DROPPED) { \ 1623 INP_WUNLOCK(inp); \ 1624 cleanup; \ 1625 return (ECONNRESET); \ 1626 } \ 1627 tp = intotcpcb(inp); \ 1628 } while(0) 1629 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 1630 1631 int 1632 tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt) 1633 { 1634 struct socket *so = inp->inp_socket; 1635 struct tcpcb *tp = intotcpcb(inp); 1636 int error = 0; 1637 1638 MPASS(sopt->sopt_dir == SOPT_SET); 1639 INP_WLOCK_ASSERT(inp); 1640 KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1641 ("inp_flags == %x", inp->inp_flags)); 1642 KASSERT(so != NULL, ("inp_socket == NULL")); 1643 1644 if (sopt->sopt_level != IPPROTO_TCP) { 1645 INP_WUNLOCK(inp); 1646 #ifdef INET6 1647 if (inp->inp_vflag & INP_IPV6PROTO) 1648 error = ip6_ctloutput(so, sopt); 1649 #endif 1650 #if defined(INET6) && defined(INET) 1651 else 1652 #endif 1653 #ifdef INET 1654 error = ip_ctloutput(so, sopt); 1655 #endif 1656 /* 1657 * When an IP-level socket option affects TCP, pass control 1658 * down to stack tfb_tcp_ctloutput, otherwise return what 1659 * IP level returned. 1660 */ 1661 switch (sopt->sopt_level) { 1662 #ifdef INET6 1663 case IPPROTO_IPV6: 1664 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) 1665 return (error); 1666 switch (sopt->sopt_name) { 1667 case IPV6_TCLASS: 1668 /* Notify tcp stacks that care (e.g. RACK). */ 1669 break; 1670 case IPV6_USE_MIN_MTU: 1671 /* Update t_maxseg accordingly. */ 1672 break; 1673 default: 1674 return (error); 1675 } 1676 break; 1677 #endif 1678 #ifdef INET 1679 case IPPROTO_IP: 1680 switch (sopt->sopt_name) { 1681 case IP_TOS: 1682 inp->inp_ip_tos &= ~IPTOS_ECN_MASK; 1683 break; 1684 case IP_TTL: 1685 /* Notify tcp stacks that care (e.g. RACK). */ 1686 break; 1687 default: 1688 return (error); 1689 } 1690 break; 1691 #endif 1692 default: 1693 return (error); 1694 } 1695 INP_WLOCK(inp); 1696 if (inp->inp_flags & INP_DROPPED) { 1697 INP_WUNLOCK(inp); 1698 return (ECONNRESET); 1699 } 1700 } else if (sopt->sopt_name == TCP_FUNCTION_BLK) { 1701 /* 1702 * Protect the TCP option TCP_FUNCTION_BLK so 1703 * that a sub-function can *never* overwrite this. 1704 */ 1705 struct tcp_function_set fsn; 1706 struct tcp_function_block *blk; 1707 void *ptr = NULL; 1708 1709 INP_WUNLOCK(inp); 1710 error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn); 1711 if (error) 1712 return (error); 1713 1714 INP_WLOCK(inp); 1715 tp = intotcpcb(inp); 1716 1717 blk = find_and_ref_tcp_functions(&fsn); 1718 if (blk == NULL) { 1719 INP_WUNLOCK(inp); 1720 return (ENOENT); 1721 } 1722 if (tp->t_fb == blk) { 1723 /* You already have this */ 1724 refcount_release(&blk->tfb_refcnt); 1725 INP_WUNLOCK(inp); 1726 return (0); 1727 } 1728 if (tp->t_state != TCPS_CLOSED) { 1729 /* 1730 * The user has advanced the state 1731 * past the initial point, we may not 1732 * be able to switch. 1733 */ 1734 if (blk->tfb_tcp_handoff_ok != NULL) { 1735 /* 1736 * Does the stack provide a 1737 * query mechanism, if so it may 1738 * still be possible? 1739 */ 1740 error = (*blk->tfb_tcp_handoff_ok)(tp); 1741 } else 1742 error = EINVAL; 1743 if (error) { 1744 refcount_release(&blk->tfb_refcnt); 1745 INP_WUNLOCK(inp); 1746 return(error); 1747 } 1748 } 1749 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 1750 refcount_release(&blk->tfb_refcnt); 1751 INP_WUNLOCK(inp); 1752 return (ENOENT); 1753 } 1754 /* 1755 * Ensure the new stack takes ownership with a 1756 * clean slate on peak rate threshold. 1757 */ 1758 #ifdef TCPHPTS 1759 /* Assure that we are not on any hpts */ 1760 tcp_hpts_remove(tp); 1761 #endif 1762 if (blk->tfb_tcp_fb_init) { 1763 error = (*blk->tfb_tcp_fb_init)(tp, &ptr); 1764 if (error) { 1765 /* 1766 * Release the ref count the lookup 1767 * acquired. 1768 */ 1769 refcount_release(&blk->tfb_refcnt); 1770 /* 1771 * Now there is a chance that the 1772 * init() function mucked with some 1773 * things before it failed, such as 1774 * hpts or inp_flags2 or timer granularity. 1775 * It should not of, but lets give the old 1776 * stack a chance to reset to a known good state. 1777 */ 1778 if (tp->t_fb->tfb_switch_failed) { 1779 (*tp->t_fb->tfb_switch_failed)(tp); 1780 } 1781 goto err_out; 1782 } 1783 } 1784 if (tp->t_fb->tfb_tcp_fb_fini) { 1785 struct epoch_tracker et; 1786 /* 1787 * Tell the stack to cleanup with 0 i.e. 1788 * the tcb is not going away. 1789 */ 1790 NET_EPOCH_ENTER(et); 1791 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1792 NET_EPOCH_EXIT(et); 1793 } 1794 /* 1795 * Release the old refcnt, the 1796 * lookup acquired a ref on the 1797 * new one already. 1798 */ 1799 refcount_release(&tp->t_fb->tfb_refcnt); 1800 /* 1801 * Set in the new stack. 1802 */ 1803 tp->t_fb = blk; 1804 tp->t_fb_ptr = ptr; 1805 #ifdef TCP_OFFLOAD 1806 if (tp->t_flags & TF_TOE) { 1807 tcp_offload_ctloutput(tp, sopt->sopt_dir, 1808 sopt->sopt_name); 1809 } 1810 #endif 1811 err_out: 1812 INP_WUNLOCK(inp); 1813 return (error); 1814 1815 } 1816 1817 /* Pass in the INP locked, callee must unlock it. */ 1818 return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt)); 1819 } 1820 1821 static int 1822 tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt) 1823 { 1824 struct socket *so = inp->inp_socket; 1825 struct tcpcb *tp = intotcpcb(inp); 1826 int error = 0; 1827 1828 MPASS(sopt->sopt_dir == SOPT_GET); 1829 INP_WLOCK_ASSERT(inp); 1830 KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1831 ("inp_flags == %x", inp->inp_flags)); 1832 KASSERT(so != NULL, ("inp_socket == NULL")); 1833 1834 if (sopt->sopt_level != IPPROTO_TCP) { 1835 INP_WUNLOCK(inp); 1836 #ifdef INET6 1837 if (inp->inp_vflag & INP_IPV6PROTO) 1838 error = ip6_ctloutput(so, sopt); 1839 #endif /* INET6 */ 1840 #if defined(INET6) && defined(INET) 1841 else 1842 #endif 1843 #ifdef INET 1844 error = ip_ctloutput(so, sopt); 1845 #endif 1846 return (error); 1847 } 1848 if (((sopt->sopt_name == TCP_FUNCTION_BLK) || 1849 (sopt->sopt_name == TCP_FUNCTION_ALIAS))) { 1850 struct tcp_function_set fsn; 1851 1852 if (sopt->sopt_name == TCP_FUNCTION_ALIAS) { 1853 memset(&fsn, 0, sizeof(fsn)); 1854 find_tcp_function_alias(tp->t_fb, &fsn); 1855 } else { 1856 strncpy(fsn.function_set_name, 1857 tp->t_fb->tfb_tcp_block_name, 1858 TCP_FUNCTION_NAME_LEN_MAX); 1859 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1860 } 1861 fsn.pcbcnt = tp->t_fb->tfb_refcnt; 1862 INP_WUNLOCK(inp); 1863 error = sooptcopyout(sopt, &fsn, sizeof fsn); 1864 return (error); 1865 } 1866 1867 /* Pass in the INP locked, callee must unlock it. */ 1868 return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt)); 1869 } 1870 1871 int 1872 tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1873 { 1874 struct inpcb *inp; 1875 1876 inp = sotoinpcb(so); 1877 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1878 1879 INP_WLOCK(inp); 1880 if (inp->inp_flags & INP_DROPPED) { 1881 INP_WUNLOCK(inp); 1882 return (ECONNRESET); 1883 } 1884 if (sopt->sopt_dir == SOPT_SET) 1885 return (tcp_ctloutput_set(inp, sopt)); 1886 else if (sopt->sopt_dir == SOPT_GET) 1887 return (tcp_ctloutput_get(inp, sopt)); 1888 else 1889 panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir); 1890 } 1891 1892 /* 1893 * If this assert becomes untrue, we need to change the size of the buf 1894 * variable in tcp_default_ctloutput(). 1895 */ 1896 #ifdef CTASSERT 1897 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 1898 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 1899 #endif 1900 1901 #ifdef KERN_TLS 1902 static int 1903 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1904 { 1905 struct tls_enable_v0 tls_v0; 1906 int error; 1907 1908 if (sopt->sopt_valsize == sizeof(tls_v0)) { 1909 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1910 sizeof(tls_v0)); 1911 if (error) 1912 return (error); 1913 memset(tls, 0, sizeof(*tls)); 1914 tls->cipher_key = tls_v0.cipher_key; 1915 tls->iv = tls_v0.iv; 1916 tls->auth_key = tls_v0.auth_key; 1917 tls->cipher_algorithm = tls_v0.cipher_algorithm; 1918 tls->cipher_key_len = tls_v0.cipher_key_len; 1919 tls->iv_len = tls_v0.iv_len; 1920 tls->auth_algorithm = tls_v0.auth_algorithm; 1921 tls->auth_key_len = tls_v0.auth_key_len; 1922 tls->flags = tls_v0.flags; 1923 tls->tls_vmajor = tls_v0.tls_vmajor; 1924 tls->tls_vminor = tls_v0.tls_vminor; 1925 return (0); 1926 } 1927 1928 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1929 } 1930 #endif 1931 1932 extern struct cc_algo newreno_cc_algo; 1933 1934 static int 1935 tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt) 1936 { 1937 struct cc_algo *algo; 1938 void *ptr = NULL; 1939 struct tcpcb *tp; 1940 struct cc_var cc_mem; 1941 char buf[TCP_CA_NAME_MAX]; 1942 size_t mem_sz; 1943 int error; 1944 1945 INP_WUNLOCK(inp); 1946 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1947 if (error) 1948 return(error); 1949 buf[sopt->sopt_valsize] = '\0'; 1950 CC_LIST_RLOCK(); 1951 STAILQ_FOREACH(algo, &cc_list, entries) { 1952 if (strncmp(buf, algo->name, 1953 TCP_CA_NAME_MAX) == 0) { 1954 if (algo->flags & CC_MODULE_BEING_REMOVED) { 1955 /* We can't "see" modules being unloaded */ 1956 continue; 1957 } 1958 break; 1959 } 1960 } 1961 if (algo == NULL) { 1962 CC_LIST_RUNLOCK(); 1963 return(ESRCH); 1964 } 1965 /* 1966 * With a reference the algorithm cannot be removed 1967 * so we hold a reference through the change process. 1968 */ 1969 cc_refer(algo); 1970 CC_LIST_RUNLOCK(); 1971 if (algo->cb_init != NULL) { 1972 /* We can now pre-get the memory for the CC */ 1973 mem_sz = (*algo->cc_data_sz)(); 1974 if (mem_sz == 0) { 1975 goto no_mem_needed; 1976 } 1977 ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK); 1978 } else { 1979 no_mem_needed: 1980 mem_sz = 0; 1981 ptr = NULL; 1982 } 1983 /* 1984 * Make sure its all clean and zero and also get 1985 * back the inplock. 1986 */ 1987 memset(&cc_mem, 0, sizeof(cc_mem)); 1988 INP_WLOCK(inp); 1989 if (inp->inp_flags & INP_DROPPED) { 1990 INP_WUNLOCK(inp); 1991 if (ptr) 1992 free(ptr, M_CC_MEM); 1993 /* Release our temp reference */ 1994 CC_LIST_RLOCK(); 1995 cc_release(algo); 1996 CC_LIST_RUNLOCK(); 1997 return (ECONNRESET); 1998 } 1999 tp = intotcpcb(inp); 2000 if (ptr != NULL) 2001 memset(ptr, 0, mem_sz); 2002 cc_mem.ccvc.tcp = tp; 2003 /* 2004 * We once again hold a write lock over the tcb so it's 2005 * safe to do these things without ordering concerns. 2006 * Note here we init into stack memory. 2007 */ 2008 if (algo->cb_init != NULL) 2009 error = algo->cb_init(&cc_mem, ptr); 2010 else 2011 error = 0; 2012 /* 2013 * The CC algorithms, when given their memory 2014 * should not fail we could in theory have a 2015 * KASSERT here. 2016 */ 2017 if (error == 0) { 2018 /* 2019 * Touchdown, lets go ahead and move the 2020 * connection to the new CC module by 2021 * copying in the cc_mem after we call 2022 * the old ones cleanup (if any). 2023 */ 2024 if (CC_ALGO(tp)->cb_destroy != NULL) 2025 CC_ALGO(tp)->cb_destroy(&tp->t_ccv); 2026 /* Detach the old CC from the tcpcb */ 2027 cc_detach(tp); 2028 /* Copy in our temp memory that was inited */ 2029 memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var)); 2030 /* Now attach the new, which takes a reference */ 2031 cc_attach(tp, algo); 2032 /* Ok now are we where we have gotten past any conn_init? */ 2033 if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) { 2034 /* Yep run the connection init for the new CC */ 2035 CC_ALGO(tp)->conn_init(&tp->t_ccv); 2036 } 2037 } else if (ptr) 2038 free(ptr, M_CC_MEM); 2039 INP_WUNLOCK(inp); 2040 /* Now lets release our temp reference */ 2041 CC_LIST_RLOCK(); 2042 cc_release(algo); 2043 CC_LIST_RUNLOCK(); 2044 return (error); 2045 } 2046 2047 int 2048 tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt) 2049 { 2050 struct inpcb *inp = tptoinpcb(tp); 2051 int error, opt, optval; 2052 u_int ui; 2053 struct tcp_info ti; 2054 #ifdef KERN_TLS 2055 struct tls_enable tls; 2056 struct socket *so = inp->inp_socket; 2057 #endif 2058 char *pbuf, buf[TCP_LOG_ID_LEN]; 2059 #ifdef STATS 2060 struct statsblob *sbp; 2061 #endif 2062 size_t len; 2063 2064 INP_WLOCK_ASSERT(inp); 2065 KASSERT((inp->inp_flags & INP_DROPPED) == 0, 2066 ("inp_flags == %x", inp->inp_flags)); 2067 KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL")); 2068 2069 switch (sopt->sopt_level) { 2070 #ifdef INET6 2071 case IPPROTO_IPV6: 2072 MPASS(inp->inp_vflag & INP_IPV6PROTO); 2073 switch (sopt->sopt_name) { 2074 case IPV6_USE_MIN_MTU: 2075 tcp6_use_min_mtu(tp); 2076 /* FALLTHROUGH */ 2077 } 2078 INP_WUNLOCK(inp); 2079 return (0); 2080 #endif 2081 #ifdef INET 2082 case IPPROTO_IP: 2083 INP_WUNLOCK(inp); 2084 return (0); 2085 #endif 2086 } 2087 2088 /* 2089 * For TCP_CCALGOOPT forward the control to CC module, for both 2090 * SOPT_SET and SOPT_GET. 2091 */ 2092 switch (sopt->sopt_name) { 2093 case TCP_CCALGOOPT: 2094 INP_WUNLOCK(inp); 2095 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 2096 return (EINVAL); 2097 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 2098 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 2099 sopt->sopt_valsize); 2100 if (error) { 2101 free(pbuf, M_TEMP); 2102 return (error); 2103 } 2104 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 2105 if (CC_ALGO(tp)->ctl_output != NULL) 2106 error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf); 2107 else 2108 error = ENOENT; 2109 INP_WUNLOCK(inp); 2110 if (error == 0 && sopt->sopt_dir == SOPT_GET) 2111 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 2112 free(pbuf, M_TEMP); 2113 return (error); 2114 } 2115 2116 switch (sopt->sopt_dir) { 2117 case SOPT_SET: 2118 switch (sopt->sopt_name) { 2119 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 2120 case TCP_MD5SIG: 2121 INP_WUNLOCK(inp); 2122 if (!TCPMD5_ENABLED()) 2123 return (ENOPROTOOPT); 2124 error = TCPMD5_PCBCTL(inp, sopt); 2125 if (error) 2126 return (error); 2127 INP_WLOCK_RECHECK(inp); 2128 goto unlock_and_done; 2129 #endif /* IPSEC */ 2130 2131 case TCP_NODELAY: 2132 case TCP_NOOPT: 2133 case TCP_LRD: 2134 INP_WUNLOCK(inp); 2135 error = sooptcopyin(sopt, &optval, sizeof optval, 2136 sizeof optval); 2137 if (error) 2138 return (error); 2139 2140 INP_WLOCK_RECHECK(inp); 2141 switch (sopt->sopt_name) { 2142 case TCP_NODELAY: 2143 opt = TF_NODELAY; 2144 break; 2145 case TCP_NOOPT: 2146 opt = TF_NOOPT; 2147 break; 2148 case TCP_LRD: 2149 opt = TF_LRD; 2150 break; 2151 default: 2152 opt = 0; /* dead code to fool gcc */ 2153 break; 2154 } 2155 2156 if (optval) 2157 tp->t_flags |= opt; 2158 else 2159 tp->t_flags &= ~opt; 2160 unlock_and_done: 2161 #ifdef TCP_OFFLOAD 2162 if (tp->t_flags & TF_TOE) { 2163 tcp_offload_ctloutput(tp, sopt->sopt_dir, 2164 sopt->sopt_name); 2165 } 2166 #endif 2167 INP_WUNLOCK(inp); 2168 break; 2169 2170 case TCP_NOPUSH: 2171 INP_WUNLOCK(inp); 2172 error = sooptcopyin(sopt, &optval, sizeof optval, 2173 sizeof optval); 2174 if (error) 2175 return (error); 2176 2177 INP_WLOCK_RECHECK(inp); 2178 if (optval) 2179 tp->t_flags |= TF_NOPUSH; 2180 else if (tp->t_flags & TF_NOPUSH) { 2181 tp->t_flags &= ~TF_NOPUSH; 2182 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2183 struct epoch_tracker et; 2184 2185 NET_EPOCH_ENTER(et); 2186 error = tcp_output_nodrop(tp); 2187 NET_EPOCH_EXIT(et); 2188 } 2189 } 2190 goto unlock_and_done; 2191 2192 case TCP_REMOTE_UDP_ENCAPS_PORT: 2193 INP_WUNLOCK(inp); 2194 error = sooptcopyin(sopt, &optval, sizeof optval, 2195 sizeof optval); 2196 if (error) 2197 return (error); 2198 if ((optval < TCP_TUNNELING_PORT_MIN) || 2199 (optval > TCP_TUNNELING_PORT_MAX)) { 2200 /* Its got to be in range */ 2201 return (EINVAL); 2202 } 2203 if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) { 2204 /* You have to have enabled a UDP tunneling port first */ 2205 return (EINVAL); 2206 } 2207 INP_WLOCK_RECHECK(inp); 2208 if (tp->t_state != TCPS_CLOSED) { 2209 /* You can't change after you are connected */ 2210 error = EINVAL; 2211 } else { 2212 /* Ok we are all good set the port */ 2213 tp->t_port = htons(optval); 2214 } 2215 goto unlock_and_done; 2216 2217 case TCP_MAXSEG: 2218 INP_WUNLOCK(inp); 2219 error = sooptcopyin(sopt, &optval, sizeof optval, 2220 sizeof optval); 2221 if (error) 2222 return (error); 2223 2224 INP_WLOCK_RECHECK(inp); 2225 if (optval > 0 && optval <= tp->t_maxseg && 2226 optval + 40 >= V_tcp_minmss) 2227 tp->t_maxseg = optval; 2228 else 2229 error = EINVAL; 2230 goto unlock_and_done; 2231 2232 case TCP_INFO: 2233 INP_WUNLOCK(inp); 2234 error = EINVAL; 2235 break; 2236 2237 case TCP_STATS: 2238 INP_WUNLOCK(inp); 2239 #ifdef STATS 2240 error = sooptcopyin(sopt, &optval, sizeof optval, 2241 sizeof optval); 2242 if (error) 2243 return (error); 2244 2245 if (optval > 0) 2246 sbp = stats_blob_alloc( 2247 V_tcp_perconn_stats_dflt_tpl, 0); 2248 else 2249 sbp = NULL; 2250 2251 INP_WLOCK_RECHECK(inp); 2252 if ((tp->t_stats != NULL && sbp == NULL) || 2253 (tp->t_stats == NULL && sbp != NULL)) { 2254 struct statsblob *t = tp->t_stats; 2255 tp->t_stats = sbp; 2256 sbp = t; 2257 } 2258 INP_WUNLOCK(inp); 2259 2260 stats_blob_destroy(sbp); 2261 #else 2262 return (EOPNOTSUPP); 2263 #endif /* !STATS */ 2264 break; 2265 2266 case TCP_CONGESTION: 2267 error = tcp_set_cc_mod(inp, sopt); 2268 break; 2269 2270 case TCP_REUSPORT_LB_NUMA: 2271 INP_WUNLOCK(inp); 2272 error = sooptcopyin(sopt, &optval, sizeof(optval), 2273 sizeof(optval)); 2274 INP_WLOCK_RECHECK(inp); 2275 if (!error) 2276 error = in_pcblbgroup_numa(inp, optval); 2277 INP_WUNLOCK(inp); 2278 break; 2279 2280 #ifdef KERN_TLS 2281 case TCP_TXTLS_ENABLE: 2282 INP_WUNLOCK(inp); 2283 error = copyin_tls_enable(sopt, &tls); 2284 if (error) 2285 break; 2286 error = ktls_enable_tx(so, &tls); 2287 break; 2288 case TCP_TXTLS_MODE: 2289 INP_WUNLOCK(inp); 2290 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2291 if (error) 2292 return (error); 2293 2294 INP_WLOCK_RECHECK(inp); 2295 error = ktls_set_tx_mode(so, ui); 2296 INP_WUNLOCK(inp); 2297 break; 2298 case TCP_RXTLS_ENABLE: 2299 INP_WUNLOCK(inp); 2300 error = sooptcopyin(sopt, &tls, sizeof(tls), 2301 sizeof(tls)); 2302 if (error) 2303 break; 2304 error = ktls_enable_rx(so, &tls); 2305 break; 2306 #endif 2307 case TCP_MAXUNACKTIME: 2308 case TCP_KEEPIDLE: 2309 case TCP_KEEPINTVL: 2310 case TCP_KEEPINIT: 2311 INP_WUNLOCK(inp); 2312 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2313 if (error) 2314 return (error); 2315 2316 if (ui > (UINT_MAX / hz)) { 2317 error = EINVAL; 2318 break; 2319 } 2320 ui *= hz; 2321 2322 INP_WLOCK_RECHECK(inp); 2323 switch (sopt->sopt_name) { 2324 case TCP_MAXUNACKTIME: 2325 tp->t_maxunacktime = ui; 2326 break; 2327 2328 case TCP_KEEPIDLE: 2329 tp->t_keepidle = ui; 2330 /* 2331 * XXX: better check current remaining 2332 * timeout and "merge" it with new value. 2333 */ 2334 if ((tp->t_state > TCPS_LISTEN) && 2335 (tp->t_state <= TCPS_CLOSING)) 2336 tcp_timer_activate(tp, TT_KEEP, 2337 TP_KEEPIDLE(tp)); 2338 break; 2339 case TCP_KEEPINTVL: 2340 tp->t_keepintvl = ui; 2341 if ((tp->t_state == TCPS_FIN_WAIT_2) && 2342 (TP_MAXIDLE(tp) > 0)) 2343 tcp_timer_activate(tp, TT_2MSL, 2344 TP_MAXIDLE(tp)); 2345 break; 2346 case TCP_KEEPINIT: 2347 tp->t_keepinit = ui; 2348 if (tp->t_state == TCPS_SYN_RECEIVED || 2349 tp->t_state == TCPS_SYN_SENT) 2350 tcp_timer_activate(tp, TT_KEEP, 2351 TP_KEEPINIT(tp)); 2352 break; 2353 } 2354 goto unlock_and_done; 2355 2356 case TCP_KEEPCNT: 2357 INP_WUNLOCK(inp); 2358 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2359 if (error) 2360 return (error); 2361 2362 INP_WLOCK_RECHECK(inp); 2363 tp->t_keepcnt = ui; 2364 if ((tp->t_state == TCPS_FIN_WAIT_2) && 2365 (TP_MAXIDLE(tp) > 0)) 2366 tcp_timer_activate(tp, TT_2MSL, 2367 TP_MAXIDLE(tp)); 2368 goto unlock_and_done; 2369 2370 #ifdef TCPPCAP 2371 case TCP_PCAP_OUT: 2372 case TCP_PCAP_IN: 2373 INP_WUNLOCK(inp); 2374 error = sooptcopyin(sopt, &optval, sizeof optval, 2375 sizeof optval); 2376 if (error) 2377 return (error); 2378 2379 INP_WLOCK_RECHECK(inp); 2380 if (optval >= 0) 2381 tcp_pcap_set_sock_max( 2382 (sopt->sopt_name == TCP_PCAP_OUT) ? 2383 &(tp->t_outpkts) : &(tp->t_inpkts), 2384 optval); 2385 else 2386 error = EINVAL; 2387 goto unlock_and_done; 2388 #endif 2389 2390 case TCP_FASTOPEN: { 2391 struct tcp_fastopen tfo_optval; 2392 2393 INP_WUNLOCK(inp); 2394 if (!V_tcp_fastopen_client_enable && 2395 !V_tcp_fastopen_server_enable) 2396 return (EPERM); 2397 2398 error = sooptcopyin(sopt, &tfo_optval, 2399 sizeof(tfo_optval), sizeof(int)); 2400 if (error) 2401 return (error); 2402 2403 INP_WLOCK_RECHECK(inp); 2404 if ((tp->t_state != TCPS_CLOSED) && 2405 (tp->t_state != TCPS_LISTEN)) { 2406 error = EINVAL; 2407 goto unlock_and_done; 2408 } 2409 if (tfo_optval.enable) { 2410 if (tp->t_state == TCPS_LISTEN) { 2411 if (!V_tcp_fastopen_server_enable) { 2412 error = EPERM; 2413 goto unlock_and_done; 2414 } 2415 2416 if (tp->t_tfo_pending == NULL) 2417 tp->t_tfo_pending = 2418 tcp_fastopen_alloc_counter(); 2419 } else { 2420 /* 2421 * If a pre-shared key was provided, 2422 * stash it in the client cookie 2423 * field of the tcpcb for use during 2424 * connect. 2425 */ 2426 if (sopt->sopt_valsize == 2427 sizeof(tfo_optval)) { 2428 memcpy(tp->t_tfo_cookie.client, 2429 tfo_optval.psk, 2430 TCP_FASTOPEN_PSK_LEN); 2431 tp->t_tfo_client_cookie_len = 2432 TCP_FASTOPEN_PSK_LEN; 2433 } 2434 } 2435 tp->t_flags |= TF_FASTOPEN; 2436 } else 2437 tp->t_flags &= ~TF_FASTOPEN; 2438 goto unlock_and_done; 2439 } 2440 2441 #ifdef TCP_BLACKBOX 2442 case TCP_LOG: 2443 INP_WUNLOCK(inp); 2444 error = sooptcopyin(sopt, &optval, sizeof optval, 2445 sizeof optval); 2446 if (error) 2447 return (error); 2448 2449 INP_WLOCK_RECHECK(inp); 2450 error = tcp_log_state_change(tp, optval); 2451 goto unlock_and_done; 2452 2453 case TCP_LOGBUF: 2454 INP_WUNLOCK(inp); 2455 error = EINVAL; 2456 break; 2457 2458 case TCP_LOGID: 2459 INP_WUNLOCK(inp); 2460 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 2461 if (error) 2462 break; 2463 buf[sopt->sopt_valsize] = '\0'; 2464 INP_WLOCK_RECHECK(inp); 2465 error = tcp_log_set_id(tp, buf); 2466 /* tcp_log_set_id() unlocks the INP. */ 2467 break; 2468 2469 case TCP_LOGDUMP: 2470 case TCP_LOGDUMPID: 2471 INP_WUNLOCK(inp); 2472 error = 2473 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 2474 if (error) 2475 break; 2476 buf[sopt->sopt_valsize] = '\0'; 2477 INP_WLOCK_RECHECK(inp); 2478 if (sopt->sopt_name == TCP_LOGDUMP) { 2479 error = tcp_log_dump_tp_logbuf(tp, buf, 2480 M_WAITOK, true); 2481 INP_WUNLOCK(inp); 2482 } else { 2483 tcp_log_dump_tp_bucket_logbufs(tp, buf); 2484 /* 2485 * tcp_log_dump_tp_bucket_logbufs() drops the 2486 * INP lock. 2487 */ 2488 } 2489 break; 2490 #endif 2491 2492 default: 2493 INP_WUNLOCK(inp); 2494 error = ENOPROTOOPT; 2495 break; 2496 } 2497 break; 2498 2499 case SOPT_GET: 2500 tp = intotcpcb(inp); 2501 switch (sopt->sopt_name) { 2502 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 2503 case TCP_MD5SIG: 2504 INP_WUNLOCK(inp); 2505 if (!TCPMD5_ENABLED()) 2506 return (ENOPROTOOPT); 2507 error = TCPMD5_PCBCTL(inp, sopt); 2508 break; 2509 #endif 2510 2511 case TCP_NODELAY: 2512 optval = tp->t_flags & TF_NODELAY; 2513 INP_WUNLOCK(inp); 2514 error = sooptcopyout(sopt, &optval, sizeof optval); 2515 break; 2516 case TCP_MAXSEG: 2517 optval = tp->t_maxseg; 2518 INP_WUNLOCK(inp); 2519 error = sooptcopyout(sopt, &optval, sizeof optval); 2520 break; 2521 case TCP_REMOTE_UDP_ENCAPS_PORT: 2522 optval = ntohs(tp->t_port); 2523 INP_WUNLOCK(inp); 2524 error = sooptcopyout(sopt, &optval, sizeof optval); 2525 break; 2526 case TCP_NOOPT: 2527 optval = tp->t_flags & TF_NOOPT; 2528 INP_WUNLOCK(inp); 2529 error = sooptcopyout(sopt, &optval, sizeof optval); 2530 break; 2531 case TCP_NOPUSH: 2532 optval = tp->t_flags & TF_NOPUSH; 2533 INP_WUNLOCK(inp); 2534 error = sooptcopyout(sopt, &optval, sizeof optval); 2535 break; 2536 case TCP_INFO: 2537 tcp_fill_info(tp, &ti); 2538 INP_WUNLOCK(inp); 2539 error = sooptcopyout(sopt, &ti, sizeof ti); 2540 break; 2541 case TCP_STATS: 2542 { 2543 #ifdef STATS 2544 int nheld; 2545 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2546 2547 error = 0; 2548 socklen_t outsbsz = sopt->sopt_valsize; 2549 if (tp->t_stats == NULL) 2550 error = ENOENT; 2551 else if (outsbsz >= tp->t_stats->cursz) 2552 outsbsz = tp->t_stats->cursz; 2553 else if (outsbsz >= sizeof(struct statsblob)) 2554 outsbsz = sizeof(struct statsblob); 2555 else 2556 error = EINVAL; 2557 INP_WUNLOCK(inp); 2558 if (error) 2559 break; 2560 2561 sbp = sopt->sopt_val; 2562 nheld = atop(round_page(((vm_offset_t)sbp) + 2563 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2564 vm_page_t ma[nheld]; 2565 if (vm_fault_quick_hold_pages( 2566 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2567 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2568 nheld) < 0) { 2569 error = EFAULT; 2570 break; 2571 } 2572 2573 if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2574 SIZEOF_MEMBER(struct statsblob, flags)))) 2575 goto unhold; 2576 2577 INP_WLOCK_RECHECK(inp); 2578 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2579 sbflags | SB_CLONE_USRDSTNOFAULT); 2580 INP_WUNLOCK(inp); 2581 sopt->sopt_valsize = outsbsz; 2582 unhold: 2583 vm_page_unhold_pages(ma, nheld); 2584 #else 2585 INP_WUNLOCK(inp); 2586 error = EOPNOTSUPP; 2587 #endif /* !STATS */ 2588 break; 2589 } 2590 case TCP_CONGESTION: 2591 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2592 INP_WUNLOCK(inp); 2593 error = sooptcopyout(sopt, buf, len + 1); 2594 break; 2595 case TCP_MAXUNACKTIME: 2596 case TCP_KEEPIDLE: 2597 case TCP_KEEPINTVL: 2598 case TCP_KEEPINIT: 2599 case TCP_KEEPCNT: 2600 switch (sopt->sopt_name) { 2601 case TCP_MAXUNACKTIME: 2602 ui = TP_MAXUNACKTIME(tp) / hz; 2603 break; 2604 case TCP_KEEPIDLE: 2605 ui = TP_KEEPIDLE(tp) / hz; 2606 break; 2607 case TCP_KEEPINTVL: 2608 ui = TP_KEEPINTVL(tp) / hz; 2609 break; 2610 case TCP_KEEPINIT: 2611 ui = TP_KEEPINIT(tp) / hz; 2612 break; 2613 case TCP_KEEPCNT: 2614 ui = TP_KEEPCNT(tp); 2615 break; 2616 } 2617 INP_WUNLOCK(inp); 2618 error = sooptcopyout(sopt, &ui, sizeof(ui)); 2619 break; 2620 #ifdef TCPPCAP 2621 case TCP_PCAP_OUT: 2622 case TCP_PCAP_IN: 2623 optval = tcp_pcap_get_sock_max( 2624 (sopt->sopt_name == TCP_PCAP_OUT) ? 2625 &(tp->t_outpkts) : &(tp->t_inpkts)); 2626 INP_WUNLOCK(inp); 2627 error = sooptcopyout(sopt, &optval, sizeof optval); 2628 break; 2629 #endif 2630 case TCP_FASTOPEN: 2631 optval = tp->t_flags & TF_FASTOPEN; 2632 INP_WUNLOCK(inp); 2633 error = sooptcopyout(sopt, &optval, sizeof optval); 2634 break; 2635 #ifdef TCP_BLACKBOX 2636 case TCP_LOG: 2637 optval = tcp_get_bblog_state(tp); 2638 INP_WUNLOCK(inp); 2639 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2640 break; 2641 case TCP_LOGBUF: 2642 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 2643 error = tcp_log_getlogbuf(sopt, tp); 2644 break; 2645 case TCP_LOGID: 2646 len = tcp_log_get_id(tp, buf); 2647 INP_WUNLOCK(inp); 2648 error = sooptcopyout(sopt, buf, len + 1); 2649 break; 2650 case TCP_LOGDUMP: 2651 case TCP_LOGDUMPID: 2652 INP_WUNLOCK(inp); 2653 error = EINVAL; 2654 break; 2655 #endif 2656 #ifdef KERN_TLS 2657 case TCP_TXTLS_MODE: 2658 error = ktls_get_tx_mode(so, &optval); 2659 INP_WUNLOCK(inp); 2660 if (error == 0) 2661 error = sooptcopyout(sopt, &optval, 2662 sizeof(optval)); 2663 break; 2664 case TCP_RXTLS_MODE: 2665 error = ktls_get_rx_mode(so, &optval); 2666 INP_WUNLOCK(inp); 2667 if (error == 0) 2668 error = sooptcopyout(sopt, &optval, 2669 sizeof(optval)); 2670 break; 2671 #endif 2672 case TCP_LRD: 2673 optval = tp->t_flags & TF_LRD; 2674 INP_WUNLOCK(inp); 2675 error = sooptcopyout(sopt, &optval, sizeof optval); 2676 break; 2677 default: 2678 INP_WUNLOCK(inp); 2679 error = ENOPROTOOPT; 2680 break; 2681 } 2682 break; 2683 } 2684 return (error); 2685 } 2686 #undef INP_WLOCK_RECHECK 2687 #undef INP_WLOCK_RECHECK_CLEANUP 2688 2689 /* 2690 * Initiate (or continue) disconnect. 2691 * If embryonic state, just send reset (once). 2692 * If in ``let data drain'' option and linger null, just drop. 2693 * Otherwise (hard), mark socket disconnecting and drop 2694 * current input data; switch states based on user close, and 2695 * send segment to peer (with FIN). 2696 */ 2697 static void 2698 tcp_disconnect(struct tcpcb *tp) 2699 { 2700 struct inpcb *inp = tptoinpcb(tp); 2701 struct socket *so = tptosocket(tp); 2702 2703 NET_EPOCH_ASSERT(); 2704 INP_WLOCK_ASSERT(inp); 2705 2706 /* 2707 * Neither tcp_close() nor tcp_drop() should return NULL, as the 2708 * socket is still open. 2709 */ 2710 if (tp->t_state < TCPS_ESTABLISHED && 2711 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2712 tp = tcp_close(tp); 2713 KASSERT(tp != NULL, 2714 ("tcp_disconnect: tcp_close() returned NULL")); 2715 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2716 tp = tcp_drop(tp, 0); 2717 KASSERT(tp != NULL, 2718 ("tcp_disconnect: tcp_drop() returned NULL")); 2719 } else { 2720 soisdisconnecting(so); 2721 sbflush(&so->so_rcv); 2722 tcp_usrclosed(tp); 2723 if (!(inp->inp_flags & INP_DROPPED)) 2724 /* Ignore stack's drop request, we already at it. */ 2725 (void)tcp_output_nodrop(tp); 2726 } 2727 } 2728 2729 /* 2730 * User issued close, and wish to trail through shutdown states: 2731 * if never received SYN, just forget it. If got a SYN from peer, 2732 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2733 * If already got a FIN from peer, then almost done; go to LAST_ACK 2734 * state. In all other cases, have already sent FIN to peer (e.g. 2735 * after PRU_SHUTDOWN), and just have to play tedious game waiting 2736 * for peer to send FIN or not respond to keep-alives, etc. 2737 * We can let the user exit from the close as soon as the FIN is acked. 2738 */ 2739 static void 2740 tcp_usrclosed(struct tcpcb *tp) 2741 { 2742 2743 NET_EPOCH_ASSERT(); 2744 INP_WLOCK_ASSERT(tptoinpcb(tp)); 2745 2746 switch (tp->t_state) { 2747 case TCPS_LISTEN: 2748 #ifdef TCP_OFFLOAD 2749 tcp_offload_listen_stop(tp); 2750 #endif 2751 tcp_state_change(tp, TCPS_CLOSED); 2752 /* FALLTHROUGH */ 2753 case TCPS_CLOSED: 2754 tp = tcp_close(tp); 2755 /* 2756 * tcp_close() should never return NULL here as the socket is 2757 * still open. 2758 */ 2759 KASSERT(tp != NULL, 2760 ("tcp_usrclosed: tcp_close() returned NULL")); 2761 break; 2762 2763 case TCPS_SYN_SENT: 2764 case TCPS_SYN_RECEIVED: 2765 tp->t_flags |= TF_NEEDFIN; 2766 break; 2767 2768 case TCPS_ESTABLISHED: 2769 tcp_state_change(tp, TCPS_FIN_WAIT_1); 2770 break; 2771 2772 case TCPS_CLOSE_WAIT: 2773 tcp_state_change(tp, TCPS_LAST_ACK); 2774 break; 2775 } 2776 if (tp->t_acktime == 0) 2777 tp->t_acktime = ticks; 2778 if (tp->t_state >= TCPS_FIN_WAIT_2) { 2779 soisdisconnected(tptosocket(tp)); 2780 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 2781 if (tp->t_state == TCPS_FIN_WAIT_2) { 2782 int timeout; 2783 2784 timeout = (tcp_fast_finwait2_recycle) ? 2785 tcp_finwait2_timeout : TP_MAXIDLE(tp); 2786 tcp_timer_activate(tp, TT_2MSL, timeout); 2787 } 2788 } 2789 } 2790 2791 #ifdef DDB 2792 static void 2793 db_print_indent(int indent) 2794 { 2795 int i; 2796 2797 for (i = 0; i < indent; i++) 2798 db_printf(" "); 2799 } 2800 2801 static void 2802 db_print_tstate(int t_state) 2803 { 2804 2805 switch (t_state) { 2806 case TCPS_CLOSED: 2807 db_printf("TCPS_CLOSED"); 2808 return; 2809 2810 case TCPS_LISTEN: 2811 db_printf("TCPS_LISTEN"); 2812 return; 2813 2814 case TCPS_SYN_SENT: 2815 db_printf("TCPS_SYN_SENT"); 2816 return; 2817 2818 case TCPS_SYN_RECEIVED: 2819 db_printf("TCPS_SYN_RECEIVED"); 2820 return; 2821 2822 case TCPS_ESTABLISHED: 2823 db_printf("TCPS_ESTABLISHED"); 2824 return; 2825 2826 case TCPS_CLOSE_WAIT: 2827 db_printf("TCPS_CLOSE_WAIT"); 2828 return; 2829 2830 case TCPS_FIN_WAIT_1: 2831 db_printf("TCPS_FIN_WAIT_1"); 2832 return; 2833 2834 case TCPS_CLOSING: 2835 db_printf("TCPS_CLOSING"); 2836 return; 2837 2838 case TCPS_LAST_ACK: 2839 db_printf("TCPS_LAST_ACK"); 2840 return; 2841 2842 case TCPS_FIN_WAIT_2: 2843 db_printf("TCPS_FIN_WAIT_2"); 2844 return; 2845 2846 case TCPS_TIME_WAIT: 2847 db_printf("TCPS_TIME_WAIT"); 2848 return; 2849 2850 default: 2851 db_printf("unknown"); 2852 return; 2853 } 2854 } 2855 2856 static void 2857 db_print_tflags(u_int t_flags) 2858 { 2859 int comma; 2860 2861 comma = 0; 2862 if (t_flags & TF_ACKNOW) { 2863 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2864 comma = 1; 2865 } 2866 if (t_flags & TF_DELACK) { 2867 db_printf("%sTF_DELACK", comma ? ", " : ""); 2868 comma = 1; 2869 } 2870 if (t_flags & TF_NODELAY) { 2871 db_printf("%sTF_NODELAY", comma ? ", " : ""); 2872 comma = 1; 2873 } 2874 if (t_flags & TF_NOOPT) { 2875 db_printf("%sTF_NOOPT", comma ? ", " : ""); 2876 comma = 1; 2877 } 2878 if (t_flags & TF_SENTFIN) { 2879 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2880 comma = 1; 2881 } 2882 if (t_flags & TF_REQ_SCALE) { 2883 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2884 comma = 1; 2885 } 2886 if (t_flags & TF_RCVD_SCALE) { 2887 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2888 comma = 1; 2889 } 2890 if (t_flags & TF_REQ_TSTMP) { 2891 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2892 comma = 1; 2893 } 2894 if (t_flags & TF_RCVD_TSTMP) { 2895 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2896 comma = 1; 2897 } 2898 if (t_flags & TF_SACK_PERMIT) { 2899 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2900 comma = 1; 2901 } 2902 if (t_flags & TF_NEEDSYN) { 2903 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2904 comma = 1; 2905 } 2906 if (t_flags & TF_NEEDFIN) { 2907 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2908 comma = 1; 2909 } 2910 if (t_flags & TF_NOPUSH) { 2911 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2912 comma = 1; 2913 } 2914 if (t_flags & TF_PREVVALID) { 2915 db_printf("%sTF_PREVVALID", comma ? ", " : ""); 2916 comma = 1; 2917 } 2918 if (t_flags & TF_MORETOCOME) { 2919 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2920 comma = 1; 2921 } 2922 if (t_flags & TF_SONOTCONN) { 2923 db_printf("%sTF_SONOTCONN", comma ? ", " : ""); 2924 comma = 1; 2925 } 2926 if (t_flags & TF_LASTIDLE) { 2927 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2928 comma = 1; 2929 } 2930 if (t_flags & TF_RXWIN0SENT) { 2931 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2932 comma = 1; 2933 } 2934 if (t_flags & TF_FASTRECOVERY) { 2935 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2936 comma = 1; 2937 } 2938 if (t_flags & TF_CONGRECOVERY) { 2939 db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2940 comma = 1; 2941 } 2942 if (t_flags & TF_WASFRECOVERY) { 2943 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2944 comma = 1; 2945 } 2946 if (t_flags & TF_WASCRECOVERY) { 2947 db_printf("%sTF_WASCRECOVERY", comma ? ", " : ""); 2948 comma = 1; 2949 } 2950 if (t_flags & TF_SIGNATURE) { 2951 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2952 comma = 1; 2953 } 2954 if (t_flags & TF_FORCEDATA) { 2955 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2956 comma = 1; 2957 } 2958 if (t_flags & TF_TSO) { 2959 db_printf("%sTF_TSO", comma ? ", " : ""); 2960 comma = 1; 2961 } 2962 if (t_flags & TF_FASTOPEN) { 2963 db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2964 comma = 1; 2965 } 2966 } 2967 2968 static void 2969 db_print_tflags2(u_int t_flags2) 2970 { 2971 int comma; 2972 2973 comma = 0; 2974 if (t_flags2 & TF2_PLPMTU_BLACKHOLE) { 2975 db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : ""); 2976 comma = 1; 2977 } 2978 if (t_flags2 & TF2_PLPMTU_PMTUD) { 2979 db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : ""); 2980 comma = 1; 2981 } 2982 if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) { 2983 db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : ""); 2984 comma = 1; 2985 } 2986 if (t_flags2 & TF2_LOG_AUTO) { 2987 db_printf("%sTF2_LOG_AUTO", comma ? ", " : ""); 2988 comma = 1; 2989 } 2990 if (t_flags2 & TF2_DROP_AF_DATA) { 2991 db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : ""); 2992 comma = 1; 2993 } 2994 if (t_flags2 & TF2_ECN_PERMIT) { 2995 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 2996 comma = 1; 2997 } 2998 if (t_flags2 & TF2_ECN_SND_CWR) { 2999 db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : ""); 3000 comma = 1; 3001 } 3002 if (t_flags2 & TF2_ECN_SND_ECE) { 3003 db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : ""); 3004 comma = 1; 3005 } 3006 if (t_flags2 & TF2_ACE_PERMIT) { 3007 db_printf("%sTF2_ACE_PERMIT", comma ? ", " : ""); 3008 comma = 1; 3009 } 3010 if (t_flags2 & TF2_FBYTES_COMPLETE) { 3011 db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : ""); 3012 comma = 1; 3013 } 3014 } 3015 3016 static void 3017 db_print_toobflags(char t_oobflags) 3018 { 3019 int comma; 3020 3021 comma = 0; 3022 if (t_oobflags & TCPOOB_HAVEDATA) { 3023 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 3024 comma = 1; 3025 } 3026 if (t_oobflags & TCPOOB_HADDATA) { 3027 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 3028 comma = 1; 3029 } 3030 } 3031 3032 static void 3033 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 3034 { 3035 3036 db_print_indent(indent); 3037 db_printf("%s at %p\n", name, tp); 3038 3039 indent += 2; 3040 3041 db_print_indent(indent); 3042 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 3043 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 3044 3045 db_print_indent(indent); 3046 db_printf("t_callout: %p t_timers: %p\n", 3047 &tp->t_callout, &tp->t_timers); 3048 3049 db_print_indent(indent); 3050 db_printf("t_state: %d (", tp->t_state); 3051 db_print_tstate(tp->t_state); 3052 db_printf(")\n"); 3053 3054 db_print_indent(indent); 3055 db_printf("t_flags: 0x%x (", tp->t_flags); 3056 db_print_tflags(tp->t_flags); 3057 db_printf(")\n"); 3058 3059 db_print_indent(indent); 3060 db_printf("t_flags2: 0x%x (", tp->t_flags2); 3061 db_print_tflags2(tp->t_flags2); 3062 db_printf(")\n"); 3063 3064 db_print_indent(indent); 3065 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: 0x%08x\n", 3066 tp->snd_una, tp->snd_max, tp->snd_nxt); 3067 3068 db_print_indent(indent); 3069 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 3070 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 3071 3072 db_print_indent(indent); 3073 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 3074 tp->iss, tp->irs, tp->rcv_nxt); 3075 3076 db_print_indent(indent); 3077 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 3078 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 3079 3080 db_print_indent(indent); 3081 db_printf("snd_wnd: %u snd_cwnd: %u\n", 3082 tp->snd_wnd, tp->snd_cwnd); 3083 3084 db_print_indent(indent); 3085 db_printf("snd_ssthresh: %u snd_recover: " 3086 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 3087 3088 db_print_indent(indent); 3089 db_printf("t_rcvtime: %u t_startime: %u\n", 3090 tp->t_rcvtime, tp->t_starttime); 3091 3092 db_print_indent(indent); 3093 db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 3094 tp->t_rtttime, tp->t_rtseq); 3095 3096 db_print_indent(indent); 3097 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 3098 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 3099 3100 db_print_indent(indent); 3101 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u\n", 3102 tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin); 3103 3104 db_print_indent(indent); 3105 db_printf("t_rttupdated: %u max_sndwnd: %u t_softerror: %d\n", 3106 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 3107 3108 db_print_indent(indent); 3109 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 3110 db_print_toobflags(tp->t_oobflags); 3111 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 3112 3113 db_print_indent(indent); 3114 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 3115 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 3116 3117 db_print_indent(indent); 3118 db_printf("ts_recent: %u ts_recent_age: %u\n", 3119 tp->ts_recent, tp->ts_recent_age); 3120 3121 db_print_indent(indent); 3122 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 3123 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 3124 3125 db_print_indent(indent); 3126 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 3127 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 3128 tp->snd_recover_prev, tp->t_badrxtwin); 3129 3130 db_print_indent(indent); 3131 db_printf("snd_numholes: %d snd_holes first: %p\n", 3132 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 3133 3134 db_print_indent(indent); 3135 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 3136 tp->snd_fack, tp->rcv_numsacks); 3137 3138 /* Skip sackblks, sackhint. */ 3139 3140 db_print_indent(indent); 3141 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 3142 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 3143 } 3144 3145 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 3146 { 3147 struct tcpcb *tp; 3148 3149 if (!have_addr) { 3150 db_printf("usage: show tcpcb <addr>\n"); 3151 return; 3152 } 3153 tp = (struct tcpcb *)addr; 3154 3155 db_print_tcpcb(tp, "tcpcb", 0); 3156 } 3157 #endif 3158