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