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