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