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