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/syslog.h> 70 #include <sys/stats.h> 71 72 #ifdef DDB 73 #include <ddb/ddb.h> 74 #endif 75 76 #include <net/if.h> 77 #include <net/if_var.h> 78 #include <net/route.h> 79 #include <net/vnet.h> 80 81 #include <netinet/in.h> 82 #include <netinet/in_kdtrace.h> 83 #include <netinet/in_pcb.h> 84 #include <netinet/in_systm.h> 85 #include <netinet/in_var.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 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 208 so->so_linger = TCP_LINGERTIME; 209 out: 210 TCPDEBUG2(PRU_ATTACH); 211 TCP_PROBE2(debug__user, tp, PRU_ATTACH); 212 return (error); 213 } 214 215 /* 216 * tcp_usr_detach is called when the socket layer loses its final reference 217 * to the socket, be it a file descriptor reference, a reference from TCP, 218 * etc. At this point, there is only one case in which we will keep around 219 * inpcb state: time wait. 220 */ 221 static void 222 tcp_usr_detach(struct socket *so) 223 { 224 struct inpcb *inp; 225 struct tcpcb *tp; 226 227 inp = sotoinpcb(so); 228 KASSERT(inp != NULL, ("%s: inp == NULL", __func__)); 229 INP_WLOCK(inp); 230 KASSERT(so->so_pcb == inp && inp->inp_socket == so, 231 ("%s: socket %p inp %p mismatch", __func__, so, inp)); 232 233 tp = intotcpcb(inp); 234 235 if (inp->inp_flags & INP_TIMEWAIT) { 236 /* 237 * There are two cases to handle: one in which the time wait 238 * state is being discarded (INP_DROPPED), and one in which 239 * this connection will remain in timewait. In the former, 240 * it is time to discard all state (except tcptw, which has 241 * already been discarded by the timewait close code, which 242 * should be further up the call stack somewhere). In the 243 * latter case, we detach from the socket, but leave the pcb 244 * present until timewait ends. 245 * 246 * XXXRW: Would it be cleaner to free the tcptw here? 247 * 248 * Astute question indeed, from twtcp perspective there are 249 * four cases to consider: 250 * 251 * #1 tcp_usr_detach is called at tcptw creation time by 252 * tcp_twstart, then do not discard the newly created tcptw 253 * and leave inpcb present until timewait ends 254 * #2 tcp_usr_detach is called at tcptw creation time by 255 * tcp_twstart, but connection is local and tw will be 256 * discarded immediately 257 * #3 tcp_usr_detach is called at timewait end (or reuse) by 258 * tcp_twclose, then the tcptw has already been discarded 259 * (or reused) and inpcb is freed here 260 * #4 tcp_usr_detach is called() after timewait ends (or reuse) 261 * (e.g. by soclose), then tcptw has already been discarded 262 * (or reused) and inpcb is freed here 263 * 264 * In all three cases the tcptw should not be freed here. 265 */ 266 if (inp->inp_flags & INP_DROPPED) { 267 in_pcbdetach(inp); 268 if (__predict_true(tp == NULL)) { 269 in_pcbfree(inp); 270 } else { 271 /* 272 * This case should not happen as in TIMEWAIT 273 * state the inp should not be destroyed before 274 * its tcptw. If INVARIANTS is defined, panic. 275 */ 276 #ifdef INVARIANTS 277 panic("%s: Panic before an inp double-free: " 278 "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 279 , __func__); 280 #else 281 log(LOG_ERR, "%s: Avoid an inp double-free: " 282 "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 283 , __func__); 284 #endif 285 INP_WUNLOCK(inp); 286 } 287 } else { 288 in_pcbdetach(inp); 289 INP_WUNLOCK(inp); 290 } 291 } else { 292 /* 293 * If the connection is not in timewait, we consider two 294 * two conditions: one in which no further processing is 295 * necessary (dropped || embryonic), and one in which TCP is 296 * not yet done, but no longer requires the socket, so the 297 * pcb will persist for the time being. 298 * 299 * XXXRW: Does the second case still occur? 300 */ 301 if (inp->inp_flags & INP_DROPPED || 302 tp->t_state < TCPS_SYN_SENT) { 303 tcp_discardcb(tp); 304 in_pcbdetach(inp); 305 in_pcbfree(inp); 306 } else { 307 in_pcbdetach(inp); 308 INP_WUNLOCK(inp); 309 } 310 } 311 } 312 313 #ifdef INET 314 /* 315 * Give the socket an address. 316 */ 317 static int 318 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 319 { 320 int error = 0; 321 struct inpcb *inp; 322 struct tcpcb *tp = NULL; 323 struct sockaddr_in *sinp; 324 325 sinp = (struct sockaddr_in *)nam; 326 if (nam->sa_len != sizeof (*sinp)) 327 return (EINVAL); 328 /* 329 * Must check for multicast addresses and disallow binding 330 * to them. 331 */ 332 if (sinp->sin_family == AF_INET && 333 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 334 return (EAFNOSUPPORT); 335 336 TCPDEBUG0; 337 inp = sotoinpcb(so); 338 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 339 INP_WLOCK(inp); 340 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 341 error = EINVAL; 342 goto out; 343 } 344 tp = intotcpcb(inp); 345 TCPDEBUG1(); 346 INP_HASH_WLOCK(&V_tcbinfo); 347 error = in_pcbbind(inp, nam, td->td_ucred); 348 INP_HASH_WUNLOCK(&V_tcbinfo); 349 out: 350 TCPDEBUG2(PRU_BIND); 351 TCP_PROBE2(debug__user, tp, PRU_BIND); 352 INP_WUNLOCK(inp); 353 354 return (error); 355 } 356 #endif /* INET */ 357 358 #ifdef INET6 359 static int 360 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 361 { 362 int error = 0; 363 struct inpcb *inp; 364 struct tcpcb *tp = NULL; 365 struct sockaddr_in6 *sin6; 366 u_char vflagsav; 367 368 sin6 = (struct sockaddr_in6 *)nam; 369 if (nam->sa_len != sizeof (*sin6)) 370 return (EINVAL); 371 /* 372 * Must check for multicast addresses and disallow binding 373 * to them. 374 */ 375 if (sin6->sin6_family == AF_INET6 && 376 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 377 return (EAFNOSUPPORT); 378 379 TCPDEBUG0; 380 inp = sotoinpcb(so); 381 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 382 INP_WLOCK(inp); 383 vflagsav = inp->inp_vflag; 384 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 385 error = EINVAL; 386 goto out; 387 } 388 tp = intotcpcb(inp); 389 TCPDEBUG1(); 390 INP_HASH_WLOCK(&V_tcbinfo); 391 inp->inp_vflag &= ~INP_IPV4; 392 inp->inp_vflag |= INP_IPV6; 393 #ifdef INET 394 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 395 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 396 inp->inp_vflag |= INP_IPV4; 397 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 398 struct sockaddr_in sin; 399 400 in6_sin6_2_sin(&sin, sin6); 401 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 402 error = EAFNOSUPPORT; 403 INP_HASH_WUNLOCK(&V_tcbinfo); 404 goto out; 405 } 406 inp->inp_vflag |= INP_IPV4; 407 inp->inp_vflag &= ~INP_IPV6; 408 error = in_pcbbind(inp, (struct sockaddr *)&sin, 409 td->td_ucred); 410 INP_HASH_WUNLOCK(&V_tcbinfo); 411 goto out; 412 } 413 } 414 #endif 415 error = in6_pcbbind(inp, nam, td->td_ucred); 416 INP_HASH_WUNLOCK(&V_tcbinfo); 417 out: 418 if (error != 0) 419 inp->inp_vflag = vflagsav; 420 TCPDEBUG2(PRU_BIND); 421 TCP_PROBE2(debug__user, tp, PRU_BIND); 422 INP_WUNLOCK(inp); 423 return (error); 424 } 425 #endif /* INET6 */ 426 427 #ifdef INET 428 /* 429 * Prepare to accept connections. 430 */ 431 static int 432 tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 433 { 434 int error = 0; 435 struct inpcb *inp; 436 struct tcpcb *tp = NULL; 437 438 TCPDEBUG0; 439 inp = sotoinpcb(so); 440 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 441 INP_WLOCK(inp); 442 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 443 error = EINVAL; 444 goto out; 445 } 446 tp = intotcpcb(inp); 447 TCPDEBUG1(); 448 SOCK_LOCK(so); 449 error = solisten_proto_check(so); 450 INP_HASH_WLOCK(&V_tcbinfo); 451 if (error == 0 && inp->inp_lport == 0) 452 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 453 INP_HASH_WUNLOCK(&V_tcbinfo); 454 if (error == 0) { 455 tcp_state_change(tp, TCPS_LISTEN); 456 solisten_proto(so, backlog); 457 #ifdef TCP_OFFLOAD 458 if ((so->so_options & SO_NO_OFFLOAD) == 0) 459 tcp_offload_listen_start(tp); 460 #endif 461 } 462 SOCK_UNLOCK(so); 463 464 if (IS_FASTOPEN(tp->t_flags)) 465 tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 466 467 out: 468 TCPDEBUG2(PRU_LISTEN); 469 TCP_PROBE2(debug__user, tp, PRU_LISTEN); 470 INP_WUNLOCK(inp); 471 return (error); 472 } 473 #endif /* INET */ 474 475 #ifdef INET6 476 static int 477 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 478 { 479 int error = 0; 480 struct inpcb *inp; 481 struct tcpcb *tp = NULL; 482 u_char vflagsav; 483 484 TCPDEBUG0; 485 inp = sotoinpcb(so); 486 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 487 INP_WLOCK(inp); 488 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 489 error = EINVAL; 490 goto out; 491 } 492 vflagsav = inp->inp_vflag; 493 tp = intotcpcb(inp); 494 TCPDEBUG1(); 495 SOCK_LOCK(so); 496 error = solisten_proto_check(so); 497 INP_HASH_WLOCK(&V_tcbinfo); 498 if (error == 0 && inp->inp_lport == 0) { 499 inp->inp_vflag &= ~INP_IPV4; 500 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 501 inp->inp_vflag |= INP_IPV4; 502 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 503 } 504 INP_HASH_WUNLOCK(&V_tcbinfo); 505 if (error == 0) { 506 tcp_state_change(tp, TCPS_LISTEN); 507 solisten_proto(so, backlog); 508 #ifdef TCP_OFFLOAD 509 if ((so->so_options & SO_NO_OFFLOAD) == 0) 510 tcp_offload_listen_start(tp); 511 #endif 512 } 513 SOCK_UNLOCK(so); 514 515 if (IS_FASTOPEN(tp->t_flags)) 516 tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 517 518 if (error != 0) 519 inp->inp_vflag = vflagsav; 520 521 out: 522 TCPDEBUG2(PRU_LISTEN); 523 TCP_PROBE2(debug__user, tp, PRU_LISTEN); 524 INP_WUNLOCK(inp); 525 return (error); 526 } 527 #endif /* INET6 */ 528 529 #ifdef INET 530 /* 531 * Initiate connection to peer. 532 * Create a template for use in transmissions on this connection. 533 * Enter SYN_SENT state, and mark socket as connecting. 534 * Start keep-alive timer, and seed output sequence space. 535 * Send initial segment on connection. 536 */ 537 static int 538 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 539 { 540 struct epoch_tracker et; 541 int error = 0; 542 struct inpcb *inp; 543 struct tcpcb *tp = NULL; 544 struct sockaddr_in *sinp; 545 546 sinp = (struct sockaddr_in *)nam; 547 if (nam->sa_len != sizeof (*sinp)) 548 return (EINVAL); 549 /* 550 * Must disallow TCP ``connections'' to multicast addresses. 551 */ 552 if (sinp->sin_family == AF_INET 553 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 554 return (EAFNOSUPPORT); 555 if ((sinp->sin_family == AF_INET) && 556 ((ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) || 557 (sinp->sin_addr.s_addr == INADDR_ANY))) 558 return(EAFNOSUPPORT); 559 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 560 return (error); 561 562 TCPDEBUG0; 563 inp = sotoinpcb(so); 564 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 565 INP_WLOCK(inp); 566 if (inp->inp_flags & INP_TIMEWAIT) { 567 error = EADDRINUSE; 568 goto out; 569 } 570 if (inp->inp_flags & INP_DROPPED) { 571 error = ECONNREFUSED; 572 goto out; 573 } 574 tp = intotcpcb(inp); 575 TCPDEBUG1(); 576 NET_EPOCH_ENTER(et); 577 if ((error = tcp_connect(tp, nam, td)) != 0) 578 goto out_in_epoch; 579 #ifdef TCP_OFFLOAD 580 if (registered_toedevs > 0 && 581 (so->so_options & SO_NO_OFFLOAD) == 0 && 582 (error = tcp_offload_connect(so, nam)) == 0) 583 goto out_in_epoch; 584 #endif 585 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 586 error = tp->t_fb->tfb_tcp_output(tp); 587 out_in_epoch: 588 NET_EPOCH_EXIT(et); 589 out: 590 TCPDEBUG2(PRU_CONNECT); 591 TCP_PROBE2(debug__user, tp, PRU_CONNECT); 592 INP_WUNLOCK(inp); 593 return (error); 594 } 595 #endif /* INET */ 596 597 #ifdef INET6 598 static int 599 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 600 { 601 struct epoch_tracker et; 602 int error = 0; 603 struct inpcb *inp; 604 struct tcpcb *tp = NULL; 605 struct sockaddr_in6 *sin6; 606 u_int8_t incflagsav; 607 u_char vflagsav; 608 609 TCPDEBUG0; 610 611 sin6 = (struct sockaddr_in6 *)nam; 612 if (nam->sa_len != sizeof (*sin6)) 613 return (EINVAL); 614 /* 615 * Must disallow TCP ``connections'' to multicast addresses. 616 */ 617 if (sin6->sin6_family == AF_INET6 618 && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 619 return (EAFNOSUPPORT); 620 621 inp = sotoinpcb(so); 622 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 623 INP_WLOCK(inp); 624 vflagsav = inp->inp_vflag; 625 incflagsav = inp->inp_inc.inc_flags; 626 if (inp->inp_flags & INP_TIMEWAIT) { 627 error = EADDRINUSE; 628 goto out; 629 } 630 if (inp->inp_flags & INP_DROPPED) { 631 error = ECONNREFUSED; 632 goto out; 633 } 634 tp = intotcpcb(inp); 635 TCPDEBUG1(); 636 #ifdef INET 637 /* 638 * XXXRW: Some confusion: V4/V6 flags relate to binding, and 639 * therefore probably require the hash lock, which isn't held here. 640 * Is this a significant problem? 641 */ 642 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 643 struct sockaddr_in sin; 644 645 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 646 error = EINVAL; 647 goto out; 648 } 649 if ((inp->inp_vflag & INP_IPV4) == 0) { 650 error = EAFNOSUPPORT; 651 goto out; 652 } 653 654 in6_sin6_2_sin(&sin, sin6); 655 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 656 error = EAFNOSUPPORT; 657 goto out; 658 } 659 if ((ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) || 660 (sin.sin_addr.s_addr == INADDR_ANY)) { 661 error = EAFNOSUPPORT; 662 goto out; 663 } 664 if ((error = prison_remote_ip4(td->td_ucred, 665 &sin.sin_addr)) != 0) 666 goto out; 667 inp->inp_vflag |= INP_IPV4; 668 inp->inp_vflag &= ~INP_IPV6; 669 NET_EPOCH_ENTER(et); 670 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 671 goto out_in_epoch; 672 #ifdef TCP_OFFLOAD 673 if (registered_toedevs > 0 && 674 (so->so_options & SO_NO_OFFLOAD) == 0 && 675 (error = tcp_offload_connect(so, nam)) == 0) 676 goto out_in_epoch; 677 #endif 678 error = tp->t_fb->tfb_tcp_output(tp); 679 goto out_in_epoch; 680 } else { 681 if ((inp->inp_vflag & INP_IPV6) == 0) { 682 error = EAFNOSUPPORT; 683 goto out; 684 } 685 } 686 #endif 687 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 688 goto out; 689 inp->inp_vflag &= ~INP_IPV4; 690 inp->inp_vflag |= INP_IPV6; 691 inp->inp_inc.inc_flags |= INC_ISIPV6; 692 if ((error = tcp6_connect(tp, nam, td)) != 0) 693 goto out; 694 #ifdef TCP_OFFLOAD 695 if (registered_toedevs > 0 && 696 (so->so_options & SO_NO_OFFLOAD) == 0 && 697 (error = tcp_offload_connect(so, nam)) == 0) 698 goto out; 699 #endif 700 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 701 NET_EPOCH_ENTER(et); 702 error = tp->t_fb->tfb_tcp_output(tp); 703 #ifdef INET 704 out_in_epoch: 705 #endif 706 NET_EPOCH_EXIT(et); 707 out: 708 /* 709 * If the implicit bind in the connect call fails, restore 710 * the flags we modified. 711 */ 712 if (error != 0 && inp->inp_lport == 0) { 713 inp->inp_vflag = vflagsav; 714 inp->inp_inc.inc_flags = incflagsav; 715 } 716 717 TCPDEBUG2(PRU_CONNECT); 718 TCP_PROBE2(debug__user, tp, PRU_CONNECT); 719 INP_WUNLOCK(inp); 720 return (error); 721 } 722 #endif /* INET6 */ 723 724 /* 725 * Initiate disconnect from peer. 726 * If connection never passed embryonic stage, just drop; 727 * else if don't need to let data drain, then can just drop anyways, 728 * else have to begin TCP shutdown process: mark socket disconnecting, 729 * drain unread data, state switch to reflect user close, and 730 * send segment (e.g. FIN) to peer. Socket will be really disconnected 731 * when peer sends FIN and acks ours. 732 * 733 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 734 */ 735 static int 736 tcp_usr_disconnect(struct socket *so) 737 { 738 struct inpcb *inp; 739 struct tcpcb *tp = NULL; 740 struct epoch_tracker et; 741 int error = 0; 742 743 TCPDEBUG0; 744 NET_EPOCH_ENTER(et); 745 inp = sotoinpcb(so); 746 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 747 INP_WLOCK(inp); 748 if (inp->inp_flags & INP_TIMEWAIT) 749 goto out; 750 if (inp->inp_flags & INP_DROPPED) { 751 error = ECONNRESET; 752 goto out; 753 } 754 tp = intotcpcb(inp); 755 TCPDEBUG1(); 756 tcp_disconnect(tp); 757 out: 758 TCPDEBUG2(PRU_DISCONNECT); 759 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 760 INP_WUNLOCK(inp); 761 NET_EPOCH_EXIT(et); 762 return (error); 763 } 764 765 #ifdef INET 766 /* 767 * Accept a connection. Essentially all the work is done at higher levels; 768 * just return the address of the peer, storing through addr. 769 */ 770 static int 771 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 772 { 773 int error = 0; 774 struct inpcb *inp = NULL; 775 struct tcpcb *tp = NULL; 776 struct in_addr addr; 777 in_port_t port = 0; 778 TCPDEBUG0; 779 780 if (so->so_state & SS_ISDISCONNECTED) 781 return (ECONNABORTED); 782 783 inp = sotoinpcb(so); 784 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 785 INP_WLOCK(inp); 786 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 787 error = ECONNABORTED; 788 goto out; 789 } 790 tp = intotcpcb(inp); 791 TCPDEBUG1(); 792 793 /* 794 * We inline in_getpeeraddr and COMMON_END here, so that we can 795 * copy the data of interest and defer the malloc until after we 796 * release the lock. 797 */ 798 port = inp->inp_fport; 799 addr = inp->inp_faddr; 800 801 out: 802 TCPDEBUG2(PRU_ACCEPT); 803 TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 804 INP_WUNLOCK(inp); 805 if (error == 0) 806 *nam = in_sockaddr(port, &addr); 807 return error; 808 } 809 #endif /* INET */ 810 811 #ifdef INET6 812 static int 813 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 814 { 815 struct inpcb *inp = NULL; 816 int error = 0; 817 struct tcpcb *tp = NULL; 818 struct in_addr addr; 819 struct in6_addr addr6; 820 struct epoch_tracker et; 821 in_port_t port = 0; 822 int v4 = 0; 823 TCPDEBUG0; 824 825 if (so->so_state & SS_ISDISCONNECTED) 826 return (ECONNABORTED); 827 828 inp = sotoinpcb(so); 829 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 830 NET_EPOCH_ENTER(et); 831 INP_WLOCK(inp); 832 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 833 error = ECONNABORTED; 834 goto out; 835 } 836 tp = intotcpcb(inp); 837 TCPDEBUG1(); 838 839 /* 840 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 841 * copy the data of interest and defer the malloc until after we 842 * release the lock. 843 */ 844 if (inp->inp_vflag & INP_IPV4) { 845 v4 = 1; 846 port = inp->inp_fport; 847 addr = inp->inp_faddr; 848 } else { 849 port = inp->inp_fport; 850 addr6 = inp->in6p_faddr; 851 } 852 853 out: 854 TCPDEBUG2(PRU_ACCEPT); 855 TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 856 INP_WUNLOCK(inp); 857 NET_EPOCH_EXIT(et); 858 if (error == 0) { 859 if (v4) 860 *nam = in6_v4mapsin6_sockaddr(port, &addr); 861 else 862 *nam = in6_sockaddr(port, &addr6); 863 } 864 return error; 865 } 866 #endif /* INET6 */ 867 868 /* 869 * Mark the connection as being incapable of further output. 870 */ 871 static int 872 tcp_usr_shutdown(struct socket *so) 873 { 874 int error = 0; 875 struct inpcb *inp; 876 struct tcpcb *tp = NULL; 877 struct epoch_tracker et; 878 879 TCPDEBUG0; 880 NET_EPOCH_ENTER(et); 881 inp = sotoinpcb(so); 882 KASSERT(inp != NULL, ("inp == NULL")); 883 INP_WLOCK(inp); 884 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 885 error = ECONNRESET; 886 goto out; 887 } 888 tp = intotcpcb(inp); 889 TCPDEBUG1(); 890 socantsendmore(so); 891 tcp_usrclosed(tp); 892 if (!(inp->inp_flags & INP_DROPPED)) 893 error = tp->t_fb->tfb_tcp_output(tp); 894 895 out: 896 TCPDEBUG2(PRU_SHUTDOWN); 897 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 898 INP_WUNLOCK(inp); 899 NET_EPOCH_EXIT(et); 900 901 return (error); 902 } 903 904 /* 905 * After a receive, possibly send window update to peer. 906 */ 907 static int 908 tcp_usr_rcvd(struct socket *so, int flags) 909 { 910 struct epoch_tracker et; 911 struct inpcb *inp; 912 struct tcpcb *tp = NULL; 913 int error = 0; 914 915 TCPDEBUG0; 916 inp = sotoinpcb(so); 917 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 918 INP_WLOCK(inp); 919 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 920 error = ECONNRESET; 921 goto out; 922 } 923 tp = intotcpcb(inp); 924 TCPDEBUG1(); 925 /* 926 * For passively-created TFO connections, don't attempt a window 927 * update while still in SYN_RECEIVED as this may trigger an early 928 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 929 * application response data, or failing that, when the DELACK timer 930 * expires. 931 */ 932 if (IS_FASTOPEN(tp->t_flags) && 933 (tp->t_state == TCPS_SYN_RECEIVED)) 934 goto out; 935 NET_EPOCH_ENTER(et); 936 #ifdef TCP_OFFLOAD 937 if (tp->t_flags & TF_TOE) 938 tcp_offload_rcvd(tp); 939 else 940 #endif 941 tp->t_fb->tfb_tcp_output(tp); 942 NET_EPOCH_EXIT(et); 943 out: 944 TCPDEBUG2(PRU_RCVD); 945 TCP_PROBE2(debug__user, tp, PRU_RCVD); 946 INP_WUNLOCK(inp); 947 return (error); 948 } 949 950 /* 951 * Do a send by putting data in output queue and updating urgent 952 * marker if URG set. Possibly send more data. Unlike the other 953 * pru_*() routines, the mbuf chains are our responsibility. We 954 * must either enqueue them or free them. The other pru_* routines 955 * generally are caller-frees. 956 */ 957 static int 958 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 959 struct sockaddr *nam, struct mbuf *control, struct thread *td) 960 { 961 struct epoch_tracker et; 962 int error = 0; 963 struct inpcb *inp; 964 struct tcpcb *tp = NULL; 965 #ifdef INET 966 #ifdef INET6 967 struct sockaddr_in sin; 968 #endif 969 struct sockaddr_in *sinp; 970 #endif 971 #ifdef INET6 972 int isipv6; 973 #endif 974 u_int8_t incflagsav; 975 u_char vflagsav; 976 bool restoreflags; 977 TCPDEBUG0; 978 979 /* 980 * We require the pcbinfo "read lock" if we will close the socket 981 * as part of this call. 982 */ 983 NET_EPOCH_ENTER(et); 984 inp = sotoinpcb(so); 985 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 986 INP_WLOCK(inp); 987 vflagsav = inp->inp_vflag; 988 incflagsav = inp->inp_inc.inc_flags; 989 restoreflags = false; 990 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 991 if (control) 992 m_freem(control); 993 /* 994 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 995 * for freeing memory. 996 */ 997 if (m && (flags & PRUS_NOTREADY) == 0) 998 m_freem(m); 999 error = ECONNRESET; 1000 goto out; 1001 } 1002 tp = intotcpcb(inp); 1003 if (flags & PRUS_OOB) { 1004 if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) { 1005 if (control) 1006 m_freem(control); 1007 if (m && (flags & PRUS_NOTREADY) == 0) 1008 m_freem(m); 1009 goto out; 1010 } 1011 } 1012 TCPDEBUG1(); 1013 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 1014 switch (nam->sa_family) { 1015 #ifdef INET 1016 case AF_INET: 1017 sinp = (struct sockaddr_in *)nam; 1018 if (sinp->sin_len != sizeof(struct sockaddr_in)) { 1019 if (m) 1020 m_freem(m); 1021 error = EINVAL; 1022 goto out; 1023 } 1024 if ((inp->inp_vflag & INP_IPV6) != 0) { 1025 if (m) 1026 m_freem(m); 1027 error = EAFNOSUPPORT; 1028 goto out; 1029 } 1030 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 1031 if (m) 1032 m_freem(m); 1033 error = EAFNOSUPPORT; 1034 goto out; 1035 } 1036 if ((ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) || 1037 (sinp->sin_addr.s_addr == INADDR_ANY)) { 1038 if (m) 1039 m_freem(m); 1040 error = EAFNOSUPPORT; 1041 goto out; 1042 } 1043 if ((error = prison_remote_ip4(td->td_ucred, 1044 &sinp->sin_addr))) { 1045 if (m) 1046 m_freem(m); 1047 goto out; 1048 } 1049 #ifdef INET6 1050 isipv6 = 0; 1051 #endif 1052 break; 1053 #endif /* INET */ 1054 #ifdef INET6 1055 case AF_INET6: 1056 { 1057 struct sockaddr_in6 *sin6; 1058 1059 sin6 = (struct sockaddr_in6 *)nam; 1060 if (sin6->sin6_len != sizeof(*sin6)) { 1061 if (m) 1062 m_freem(m); 1063 error = EINVAL; 1064 goto out; 1065 } 1066 if ((inp->inp_vflag & INP_IPV6PROTO) == 0) { 1067 if (m != NULL) 1068 m_freem(m); 1069 error = EAFNOSUPPORT; 1070 goto out; 1071 } 1072 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 1073 if (m) 1074 m_freem(m); 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 if (m) 1083 m_freem(m); 1084 goto out; 1085 } 1086 if ((inp->inp_vflag & INP_IPV4) == 0) { 1087 error = EAFNOSUPPORT; 1088 if (m) 1089 m_freem(m); 1090 goto out; 1091 } 1092 restoreflags = true; 1093 inp->inp_vflag &= ~INP_IPV6; 1094 sinp = &sin; 1095 in6_sin6_2_sin(sinp, sin6); 1096 if (IN_MULTICAST( 1097 ntohl(sinp->sin_addr.s_addr))) { 1098 error = EAFNOSUPPORT; 1099 if (m) 1100 m_freem(m); 1101 goto out; 1102 } 1103 if ((error = prison_remote_ip4(td->td_ucred, 1104 &sinp->sin_addr))) { 1105 if (m) 1106 m_freem(m); 1107 goto out; 1108 } 1109 isipv6 = 0; 1110 #else /* !INET */ 1111 error = EAFNOSUPPORT; 1112 if (m) 1113 m_freem(m); 1114 goto out; 1115 #endif /* INET */ 1116 } else { 1117 if ((inp->inp_vflag & INP_IPV6) == 0) { 1118 if (m) 1119 m_freem(m); 1120 error = EAFNOSUPPORT; 1121 goto out; 1122 } 1123 restoreflags = true; 1124 inp->inp_vflag &= ~INP_IPV4; 1125 inp->inp_inc.inc_flags |= INC_ISIPV6; 1126 if ((error = prison_remote_ip6(td->td_ucred, 1127 &sin6->sin6_addr))) { 1128 if (m) 1129 m_freem(m); 1130 goto out; 1131 } 1132 isipv6 = 1; 1133 } 1134 break; 1135 } 1136 #endif /* INET6 */ 1137 default: 1138 if (m) 1139 m_freem(m); 1140 error = EAFNOSUPPORT; 1141 goto out; 1142 } 1143 } 1144 if (control) { 1145 /* TCP doesn't do control messages (rights, creds, etc) */ 1146 if (control->m_len) { 1147 m_freem(control); 1148 if (m) 1149 m_freem(m); 1150 error = EINVAL; 1151 goto out; 1152 } 1153 m_freem(control); /* empty control, just free it */ 1154 } 1155 if (!(flags & PRUS_OOB)) { 1156 sbappendstream(&so->so_snd, m, flags); 1157 if (nam && tp->t_state < TCPS_SYN_SENT) { 1158 /* 1159 * Do implied connect if not yet connected, 1160 * initialize window to default value, and 1161 * initialize maxseg using peer's cached MSS. 1162 */ 1163 #ifdef INET6 1164 if (isipv6) 1165 error = tcp6_connect(tp, nam, td); 1166 #endif /* INET6 */ 1167 #if defined(INET6) && defined(INET) 1168 else 1169 #endif 1170 #ifdef INET 1171 error = tcp_connect(tp, 1172 (struct sockaddr *)sinp, td); 1173 #endif 1174 /* 1175 * The bind operation in tcp_connect succeeded. We 1176 * no longer want to restore the flags if later 1177 * operations fail. 1178 */ 1179 if (error == 0 || inp->inp_lport != 0) 1180 restoreflags = false; 1181 1182 if (error) 1183 goto out; 1184 if (IS_FASTOPEN(tp->t_flags)) 1185 tcp_fastopen_connect(tp); 1186 else { 1187 tp->snd_wnd = TTCP_CLIENT_SND_WND; 1188 tcp_mss(tp, -1); 1189 } 1190 } 1191 if (flags & PRUS_EOF) { 1192 /* 1193 * Close the send side of the connection after 1194 * the data is sent. 1195 */ 1196 socantsendmore(so); 1197 tcp_usrclosed(tp); 1198 } 1199 if (!(inp->inp_flags & INP_DROPPED) && 1200 !(flags & PRUS_NOTREADY)) { 1201 if (flags & PRUS_MORETOCOME) 1202 tp->t_flags |= TF_MORETOCOME; 1203 error = tp->t_fb->tfb_tcp_output(tp); 1204 if (flags & PRUS_MORETOCOME) 1205 tp->t_flags &= ~TF_MORETOCOME; 1206 } 1207 } else { 1208 /* 1209 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1210 */ 1211 SOCKBUF_LOCK(&so->so_snd); 1212 if (sbspace(&so->so_snd) < -512) { 1213 SOCKBUF_UNLOCK(&so->so_snd); 1214 m_freem(m); 1215 error = ENOBUFS; 1216 goto out; 1217 } 1218 /* 1219 * According to RFC961 (Assigned Protocols), 1220 * the urgent pointer points to the last octet 1221 * of urgent data. We continue, however, 1222 * to consider it to indicate the first octet 1223 * of data past the urgent section. 1224 * Otherwise, snd_up should be one lower. 1225 */ 1226 sbappendstream_locked(&so->so_snd, m, flags); 1227 SOCKBUF_UNLOCK(&so->so_snd); 1228 if (nam && tp->t_state < TCPS_SYN_SENT) { 1229 /* 1230 * Do implied connect if not yet connected, 1231 * initialize window to default value, and 1232 * initialize maxseg using peer's cached MSS. 1233 */ 1234 1235 /* 1236 * Not going to contemplate SYN|URG 1237 */ 1238 if (IS_FASTOPEN(tp->t_flags)) 1239 tp->t_flags &= ~TF_FASTOPEN; 1240 #ifdef INET6 1241 if (isipv6) 1242 error = tcp6_connect(tp, nam, td); 1243 #endif /* INET6 */ 1244 #if defined(INET6) && defined(INET) 1245 else 1246 #endif 1247 #ifdef INET 1248 error = tcp_connect(tp, 1249 (struct sockaddr *)sinp, td); 1250 #endif 1251 /* 1252 * The bind operation in tcp_connect succeeded. We 1253 * no longer want to restore the flags if later 1254 * operations fail. 1255 */ 1256 if (error == 0 || inp->inp_lport != 0) 1257 restoreflags = false; 1258 1259 if (error) 1260 goto out; 1261 tp->snd_wnd = TTCP_CLIENT_SND_WND; 1262 tcp_mss(tp, -1); 1263 } 1264 tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 1265 if (!(flags & PRUS_NOTREADY)) { 1266 tp->t_flags |= TF_FORCEDATA; 1267 error = tp->t_fb->tfb_tcp_output(tp); 1268 tp->t_flags &= ~TF_FORCEDATA; 1269 } 1270 } 1271 TCP_LOG_EVENT(tp, NULL, 1272 &inp->inp_socket->so_rcv, 1273 &inp->inp_socket->so_snd, 1274 TCP_LOG_USERSEND, error, 1275 0, NULL, false); 1276 out: 1277 /* 1278 * If the request was unsuccessful and we changed flags, 1279 * restore the original flags. 1280 */ 1281 if (error != 0 && restoreflags) { 1282 inp->inp_vflag = vflagsav; 1283 inp->inp_inc.inc_flags = incflagsav; 1284 } 1285 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 1286 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1287 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 1288 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1289 INP_WUNLOCK(inp); 1290 NET_EPOCH_EXIT(et); 1291 return (error); 1292 } 1293 1294 static int 1295 tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 1296 { 1297 struct epoch_tracker et; 1298 struct inpcb *inp; 1299 struct tcpcb *tp; 1300 int error; 1301 1302 inp = sotoinpcb(so); 1303 INP_WLOCK(inp); 1304 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1305 INP_WUNLOCK(inp); 1306 mb_free_notready(m, count); 1307 return (ECONNRESET); 1308 } 1309 tp = intotcpcb(inp); 1310 1311 SOCKBUF_LOCK(&so->so_snd); 1312 error = sbready(&so->so_snd, m, count); 1313 SOCKBUF_UNLOCK(&so->so_snd); 1314 if (error == 0) { 1315 NET_EPOCH_ENTER(et); 1316 error = tp->t_fb->tfb_tcp_output(tp); 1317 NET_EPOCH_EXIT(et); 1318 } 1319 INP_WUNLOCK(inp); 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) 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(struct socket *so, struct sockopt *sopt) 1718 { 1719 int error; 1720 struct inpcb *inp; 1721 struct tcpcb *tp; 1722 struct tcp_function_block *blk; 1723 struct tcp_function_set fsn; 1724 1725 error = 0; 1726 inp = sotoinpcb(so); 1727 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1728 if (sopt->sopt_level != IPPROTO_TCP) { 1729 #ifdef INET6 1730 if (inp->inp_vflag & INP_IPV6PROTO) { 1731 error = ip6_ctloutput(so, sopt); 1732 /* 1733 * In case of the IPV6_USE_MIN_MTU socket option, 1734 * the INC_IPV6MINMTU flag to announce a corresponding 1735 * MSS during the initial handshake. 1736 * If the TCP connection is not in the front states, 1737 * just reduce the MSS being used. 1738 * This avoids the sending of TCP segments which will 1739 * be fragmented at the IPv6 layer. 1740 */ 1741 if ((error == 0) && 1742 (sopt->sopt_dir == SOPT_SET) && 1743 (sopt->sopt_level == IPPROTO_IPV6) && 1744 (sopt->sopt_name == IPV6_USE_MIN_MTU)) { 1745 INP_WLOCK(inp); 1746 if ((inp->inp_flags & 1747 (INP_TIMEWAIT | INP_DROPPED))) { 1748 INP_WUNLOCK(inp); 1749 return (ECONNRESET); 1750 } 1751 inp->inp_inc.inc_flags |= INC_IPV6MINMTU; 1752 tp = intotcpcb(inp); 1753 if ((tp->t_state >= TCPS_SYN_SENT) && 1754 (inp->inp_inc.inc_flags & INC_ISIPV6)) { 1755 struct ip6_pktopts *opt; 1756 1757 opt = inp->in6p_outputopts; 1758 if ((opt != NULL) && 1759 (opt->ip6po_minmtu == 1760 IP6PO_MINMTU_ALL)) { 1761 if (tp->t_maxseg > TCP6_MSS) { 1762 tp->t_maxseg = TCP6_MSS; 1763 } 1764 } 1765 } 1766 INP_WUNLOCK(inp); 1767 } 1768 } 1769 #endif /* INET6 */ 1770 #if defined(INET6) && defined(INET) 1771 else 1772 #endif 1773 #ifdef INET 1774 { 1775 error = ip_ctloutput(so, sopt); 1776 } 1777 #endif 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 tp = intotcpcb(inp); 1786 /* 1787 * Protect the TCP option TCP_FUNCTION_BLK so 1788 * that a sub-function can *never* overwrite this. 1789 */ 1790 if ((sopt->sopt_dir == SOPT_SET) && 1791 (sopt->sopt_name == TCP_FUNCTION_BLK)) { 1792 INP_WUNLOCK(inp); 1793 error = sooptcopyin(sopt, &fsn, sizeof fsn, 1794 sizeof fsn); 1795 if (error) 1796 return (error); 1797 INP_WLOCK_RECHECK(inp); 1798 blk = find_and_ref_tcp_functions(&fsn); 1799 if (blk == NULL) { 1800 INP_WUNLOCK(inp); 1801 return (ENOENT); 1802 } 1803 if (tp->t_fb == blk) { 1804 /* You already have this */ 1805 refcount_release(&blk->tfb_refcnt); 1806 INP_WUNLOCK(inp); 1807 return (0); 1808 } 1809 if (tp->t_state != TCPS_CLOSED) { 1810 /* 1811 * The user has advanced the state 1812 * past the initial point, we may not 1813 * be able to switch. 1814 */ 1815 if (blk->tfb_tcp_handoff_ok != NULL) { 1816 /* 1817 * Does the stack provide a 1818 * query mechanism, if so it may 1819 * still be possible? 1820 */ 1821 error = (*blk->tfb_tcp_handoff_ok)(tp); 1822 } else 1823 error = EINVAL; 1824 if (error) { 1825 refcount_release(&blk->tfb_refcnt); 1826 INP_WUNLOCK(inp); 1827 return(error); 1828 } 1829 } 1830 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 1831 refcount_release(&blk->tfb_refcnt); 1832 INP_WUNLOCK(inp); 1833 return (ENOENT); 1834 } 1835 /* 1836 * Release the old refcnt, the 1837 * lookup acquired a ref on the 1838 * new one already. 1839 */ 1840 if (tp->t_fb->tfb_tcp_fb_fini) { 1841 /* 1842 * Tell the stack to cleanup with 0 i.e. 1843 * the tcb is not going away. 1844 */ 1845 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1846 } 1847 #ifdef TCPHPTS 1848 /* Assure that we are not on any hpts */ 1849 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL); 1850 #endif 1851 if (blk->tfb_tcp_fb_init) { 1852 error = (*blk->tfb_tcp_fb_init)(tp); 1853 if (error) { 1854 refcount_release(&blk->tfb_refcnt); 1855 if (tp->t_fb->tfb_tcp_fb_init) { 1856 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 1857 /* Fall back failed, drop the connection */ 1858 INP_WUNLOCK(inp); 1859 soabort(so); 1860 return(error); 1861 } 1862 } 1863 goto err_out; 1864 } 1865 } 1866 refcount_release(&tp->t_fb->tfb_refcnt); 1867 tp->t_fb = blk; 1868 #ifdef TCP_OFFLOAD 1869 if (tp->t_flags & TF_TOE) { 1870 tcp_offload_ctloutput(tp, sopt->sopt_dir, 1871 sopt->sopt_name); 1872 } 1873 #endif 1874 err_out: 1875 INP_WUNLOCK(inp); 1876 return (error); 1877 } else if ((sopt->sopt_dir == SOPT_GET) && 1878 (sopt->sopt_name == TCP_FUNCTION_BLK)) { 1879 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name, 1880 TCP_FUNCTION_NAME_LEN_MAX); 1881 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1882 fsn.pcbcnt = tp->t_fb->tfb_refcnt; 1883 INP_WUNLOCK(inp); 1884 error = sooptcopyout(sopt, &fsn, sizeof fsn); 1885 return (error); 1886 } 1887 /* Pass in the INP locked, called must unlock it */ 1888 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 1889 } 1890 1891 /* 1892 * If this assert becomes untrue, we need to change the size of the buf 1893 * variable in tcp_default_ctloutput(). 1894 */ 1895 #ifdef CTASSERT 1896 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 1897 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 1898 #endif 1899 1900 #ifdef KERN_TLS 1901 static int 1902 copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls) 1903 { 1904 struct tls_enable_v0 tls_v0; 1905 int error; 1906 1907 if (sopt->sopt_valsize == sizeof(tls_v0)) { 1908 error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0), 1909 sizeof(tls_v0)); 1910 if (error) 1911 return (error); 1912 memset(tls, 0, sizeof(*tls)); 1913 tls->cipher_key = tls_v0.cipher_key; 1914 tls->iv = tls_v0.iv; 1915 tls->auth_key = tls_v0.auth_key; 1916 tls->cipher_algorithm = tls_v0.cipher_algorithm; 1917 tls->cipher_key_len = tls_v0.cipher_key_len; 1918 tls->iv_len = tls_v0.iv_len; 1919 tls->auth_algorithm = tls_v0.auth_algorithm; 1920 tls->auth_key_len = tls_v0.auth_key_len; 1921 tls->flags = tls_v0.flags; 1922 tls->tls_vmajor = tls_v0.tls_vmajor; 1923 tls->tls_vminor = tls_v0.tls_vminor; 1924 return (0); 1925 } 1926 1927 return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls))); 1928 } 1929 #endif 1930 1931 int 1932 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 1933 { 1934 int error, opt, optval; 1935 u_int ui; 1936 struct tcp_info ti; 1937 #ifdef KERN_TLS 1938 struct tls_enable tls; 1939 #endif 1940 struct cc_algo *algo; 1941 char *pbuf, buf[TCP_LOG_ID_LEN]; 1942 #ifdef STATS 1943 struct statsblob *sbp; 1944 #endif 1945 size_t len; 1946 1947 /* 1948 * For TCP_CCALGOOPT forward the control to CC module, for both 1949 * SOPT_SET and SOPT_GET. 1950 */ 1951 switch (sopt->sopt_name) { 1952 case TCP_CCALGOOPT: 1953 INP_WUNLOCK(inp); 1954 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 1955 return (EINVAL); 1956 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 1957 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 1958 sopt->sopt_valsize); 1959 if (error) { 1960 free(pbuf, M_TEMP); 1961 return (error); 1962 } 1963 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 1964 if (CC_ALGO(tp)->ctl_output != NULL) 1965 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 1966 else 1967 error = ENOENT; 1968 INP_WUNLOCK(inp); 1969 if (error == 0 && sopt->sopt_dir == SOPT_GET) 1970 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 1971 free(pbuf, M_TEMP); 1972 return (error); 1973 } 1974 1975 switch (sopt->sopt_dir) { 1976 case SOPT_SET: 1977 switch (sopt->sopt_name) { 1978 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1979 case TCP_MD5SIG: 1980 if (!TCPMD5_ENABLED()) { 1981 INP_WUNLOCK(inp); 1982 return (ENOPROTOOPT); 1983 } 1984 error = TCPMD5_PCBCTL(inp, sopt); 1985 if (error) 1986 return (error); 1987 goto unlock_and_done; 1988 #endif /* IPSEC */ 1989 1990 case TCP_NODELAY: 1991 case TCP_NOOPT: 1992 INP_WUNLOCK(inp); 1993 error = sooptcopyin(sopt, &optval, sizeof optval, 1994 sizeof optval); 1995 if (error) 1996 return (error); 1997 1998 INP_WLOCK_RECHECK(inp); 1999 switch (sopt->sopt_name) { 2000 case TCP_NODELAY: 2001 opt = TF_NODELAY; 2002 break; 2003 case TCP_NOOPT: 2004 opt = TF_NOOPT; 2005 break; 2006 default: 2007 opt = 0; /* dead code to fool gcc */ 2008 break; 2009 } 2010 2011 if (optval) 2012 tp->t_flags |= opt; 2013 else 2014 tp->t_flags &= ~opt; 2015 unlock_and_done: 2016 #ifdef TCP_OFFLOAD 2017 if (tp->t_flags & TF_TOE) { 2018 tcp_offload_ctloutput(tp, sopt->sopt_dir, 2019 sopt->sopt_name); 2020 } 2021 #endif 2022 INP_WUNLOCK(inp); 2023 break; 2024 2025 case TCP_NOPUSH: 2026 INP_WUNLOCK(inp); 2027 error = sooptcopyin(sopt, &optval, sizeof optval, 2028 sizeof optval); 2029 if (error) 2030 return (error); 2031 2032 INP_WLOCK_RECHECK(inp); 2033 if (optval) 2034 tp->t_flags |= TF_NOPUSH; 2035 else if (tp->t_flags & TF_NOPUSH) { 2036 tp->t_flags &= ~TF_NOPUSH; 2037 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 2038 struct epoch_tracker et; 2039 2040 NET_EPOCH_ENTER(et); 2041 error = tp->t_fb->tfb_tcp_output(tp); 2042 NET_EPOCH_EXIT(et); 2043 } 2044 } 2045 goto unlock_and_done; 2046 2047 case TCP_MAXSEG: 2048 INP_WUNLOCK(inp); 2049 error = sooptcopyin(sopt, &optval, sizeof optval, 2050 sizeof optval); 2051 if (error) 2052 return (error); 2053 2054 INP_WLOCK_RECHECK(inp); 2055 if (optval > 0 && optval <= tp->t_maxseg && 2056 optval + 40 >= V_tcp_minmss) 2057 tp->t_maxseg = optval; 2058 else 2059 error = EINVAL; 2060 goto unlock_and_done; 2061 2062 case TCP_INFO: 2063 INP_WUNLOCK(inp); 2064 error = EINVAL; 2065 break; 2066 2067 case TCP_STATS: 2068 INP_WUNLOCK(inp); 2069 #ifdef STATS 2070 error = sooptcopyin(sopt, &optval, sizeof optval, 2071 sizeof optval); 2072 if (error) 2073 return (error); 2074 2075 if (optval > 0) 2076 sbp = stats_blob_alloc( 2077 V_tcp_perconn_stats_dflt_tpl, 0); 2078 else 2079 sbp = NULL; 2080 2081 INP_WLOCK_RECHECK(inp); 2082 if ((tp->t_stats != NULL && sbp == NULL) || 2083 (tp->t_stats == NULL && sbp != NULL)) { 2084 struct statsblob *t = tp->t_stats; 2085 tp->t_stats = sbp; 2086 sbp = t; 2087 } 2088 INP_WUNLOCK(inp); 2089 2090 stats_blob_destroy(sbp); 2091 #else 2092 return (EOPNOTSUPP); 2093 #endif /* !STATS */ 2094 break; 2095 2096 case TCP_CONGESTION: 2097 INP_WUNLOCK(inp); 2098 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 2099 if (error) 2100 break; 2101 buf[sopt->sopt_valsize] = '\0'; 2102 INP_WLOCK_RECHECK(inp); 2103 CC_LIST_RLOCK(); 2104 STAILQ_FOREACH(algo, &cc_list, entries) 2105 if (strncmp(buf, algo->name, 2106 TCP_CA_NAME_MAX) == 0) 2107 break; 2108 CC_LIST_RUNLOCK(); 2109 if (algo == NULL) { 2110 INP_WUNLOCK(inp); 2111 error = EINVAL; 2112 break; 2113 } 2114 /* 2115 * We hold a write lock over the tcb so it's safe to 2116 * do these things without ordering concerns. 2117 */ 2118 if (CC_ALGO(tp)->cb_destroy != NULL) 2119 CC_ALGO(tp)->cb_destroy(tp->ccv); 2120 CC_DATA(tp) = NULL; 2121 CC_ALGO(tp) = algo; 2122 /* 2123 * If something goes pear shaped initialising the new 2124 * algo, fall back to newreno (which does not 2125 * require initialisation). 2126 */ 2127 if (algo->cb_init != NULL && 2128 algo->cb_init(tp->ccv) != 0) { 2129 CC_ALGO(tp) = &newreno_cc_algo; 2130 /* 2131 * The only reason init should fail is 2132 * because of malloc. 2133 */ 2134 error = ENOMEM; 2135 } 2136 INP_WUNLOCK(inp); 2137 break; 2138 2139 #ifdef KERN_TLS 2140 case TCP_TXTLS_ENABLE: 2141 INP_WUNLOCK(inp); 2142 error = copyin_tls_enable(sopt, &tls); 2143 if (error) 2144 break; 2145 error = ktls_enable_tx(so, &tls); 2146 break; 2147 case TCP_TXTLS_MODE: 2148 INP_WUNLOCK(inp); 2149 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2150 if (error) 2151 return (error); 2152 2153 INP_WLOCK_RECHECK(inp); 2154 error = ktls_set_tx_mode(so, ui); 2155 INP_WUNLOCK(inp); 2156 break; 2157 case TCP_RXTLS_ENABLE: 2158 INP_WUNLOCK(inp); 2159 error = sooptcopyin(sopt, &tls, sizeof(tls), 2160 sizeof(tls)); 2161 if (error) 2162 break; 2163 error = ktls_enable_rx(so, &tls); 2164 break; 2165 #endif 2166 2167 case TCP_KEEPIDLE: 2168 case TCP_KEEPINTVL: 2169 case TCP_KEEPINIT: 2170 INP_WUNLOCK(inp); 2171 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2172 if (error) 2173 return (error); 2174 2175 if (ui > (UINT_MAX / hz)) { 2176 error = EINVAL; 2177 break; 2178 } 2179 ui *= hz; 2180 2181 INP_WLOCK_RECHECK(inp); 2182 switch (sopt->sopt_name) { 2183 case TCP_KEEPIDLE: 2184 tp->t_keepidle = ui; 2185 /* 2186 * XXX: better check current remaining 2187 * timeout and "merge" it with new value. 2188 */ 2189 if ((tp->t_state > TCPS_LISTEN) && 2190 (tp->t_state <= TCPS_CLOSING)) 2191 tcp_timer_activate(tp, TT_KEEP, 2192 TP_KEEPIDLE(tp)); 2193 break; 2194 case TCP_KEEPINTVL: 2195 tp->t_keepintvl = ui; 2196 if ((tp->t_state == TCPS_FIN_WAIT_2) && 2197 (TP_MAXIDLE(tp) > 0)) 2198 tcp_timer_activate(tp, TT_2MSL, 2199 TP_MAXIDLE(tp)); 2200 break; 2201 case TCP_KEEPINIT: 2202 tp->t_keepinit = ui; 2203 if (tp->t_state == TCPS_SYN_RECEIVED || 2204 tp->t_state == TCPS_SYN_SENT) 2205 tcp_timer_activate(tp, TT_KEEP, 2206 TP_KEEPINIT(tp)); 2207 break; 2208 } 2209 goto unlock_and_done; 2210 2211 case TCP_KEEPCNT: 2212 INP_WUNLOCK(inp); 2213 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 2214 if (error) 2215 return (error); 2216 2217 INP_WLOCK_RECHECK(inp); 2218 tp->t_keepcnt = ui; 2219 if ((tp->t_state == TCPS_FIN_WAIT_2) && 2220 (TP_MAXIDLE(tp) > 0)) 2221 tcp_timer_activate(tp, TT_2MSL, 2222 TP_MAXIDLE(tp)); 2223 goto unlock_and_done; 2224 2225 #ifdef TCPPCAP 2226 case TCP_PCAP_OUT: 2227 case TCP_PCAP_IN: 2228 INP_WUNLOCK(inp); 2229 error = sooptcopyin(sopt, &optval, sizeof optval, 2230 sizeof optval); 2231 if (error) 2232 return (error); 2233 2234 INP_WLOCK_RECHECK(inp); 2235 if (optval >= 0) 2236 tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 2237 &(tp->t_outpkts) : &(tp->t_inpkts), 2238 optval); 2239 else 2240 error = EINVAL; 2241 goto unlock_and_done; 2242 #endif 2243 2244 case TCP_FASTOPEN: { 2245 struct tcp_fastopen tfo_optval; 2246 2247 INP_WUNLOCK(inp); 2248 if (!V_tcp_fastopen_client_enable && 2249 !V_tcp_fastopen_server_enable) 2250 return (EPERM); 2251 2252 error = sooptcopyin(sopt, &tfo_optval, 2253 sizeof(tfo_optval), sizeof(int)); 2254 if (error) 2255 return (error); 2256 2257 INP_WLOCK_RECHECK(inp); 2258 if ((tp->t_state != TCPS_CLOSED) && 2259 (tp->t_state != TCPS_LISTEN)) { 2260 error = EINVAL; 2261 goto unlock_and_done; 2262 } 2263 if (tfo_optval.enable) { 2264 if (tp->t_state == TCPS_LISTEN) { 2265 if (!V_tcp_fastopen_server_enable) { 2266 error = EPERM; 2267 goto unlock_and_done; 2268 } 2269 2270 if (tp->t_tfo_pending == NULL) 2271 tp->t_tfo_pending = 2272 tcp_fastopen_alloc_counter(); 2273 } else { 2274 /* 2275 * If a pre-shared key was provided, 2276 * stash it in the client cookie 2277 * field of the tcpcb for use during 2278 * connect. 2279 */ 2280 if (sopt->sopt_valsize == 2281 sizeof(tfo_optval)) { 2282 memcpy(tp->t_tfo_cookie.client, 2283 tfo_optval.psk, 2284 TCP_FASTOPEN_PSK_LEN); 2285 tp->t_tfo_client_cookie_len = 2286 TCP_FASTOPEN_PSK_LEN; 2287 } 2288 } 2289 tp->t_flags |= TF_FASTOPEN; 2290 } else 2291 tp->t_flags &= ~TF_FASTOPEN; 2292 goto unlock_and_done; 2293 } 2294 2295 #ifdef TCP_BLACKBOX 2296 case TCP_LOG: 2297 INP_WUNLOCK(inp); 2298 error = sooptcopyin(sopt, &optval, sizeof optval, 2299 sizeof optval); 2300 if (error) 2301 return (error); 2302 2303 INP_WLOCK_RECHECK(inp); 2304 error = tcp_log_state_change(tp, optval); 2305 goto unlock_and_done; 2306 2307 case TCP_LOGBUF: 2308 INP_WUNLOCK(inp); 2309 error = EINVAL; 2310 break; 2311 2312 case TCP_LOGID: 2313 INP_WUNLOCK(inp); 2314 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 2315 if (error) 2316 break; 2317 buf[sopt->sopt_valsize] = '\0'; 2318 INP_WLOCK_RECHECK(inp); 2319 error = tcp_log_set_id(tp, buf); 2320 /* tcp_log_set_id() unlocks the INP. */ 2321 break; 2322 2323 case TCP_LOGDUMP: 2324 case TCP_LOGDUMPID: 2325 INP_WUNLOCK(inp); 2326 error = 2327 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 2328 if (error) 2329 break; 2330 buf[sopt->sopt_valsize] = '\0'; 2331 INP_WLOCK_RECHECK(inp); 2332 if (sopt->sopt_name == TCP_LOGDUMP) { 2333 error = tcp_log_dump_tp_logbuf(tp, buf, 2334 M_WAITOK, true); 2335 INP_WUNLOCK(inp); 2336 } else { 2337 tcp_log_dump_tp_bucket_logbufs(tp, buf); 2338 /* 2339 * tcp_log_dump_tp_bucket_logbufs() drops the 2340 * INP lock. 2341 */ 2342 } 2343 break; 2344 #endif 2345 2346 default: 2347 INP_WUNLOCK(inp); 2348 error = ENOPROTOOPT; 2349 break; 2350 } 2351 break; 2352 2353 case SOPT_GET: 2354 tp = intotcpcb(inp); 2355 switch (sopt->sopt_name) { 2356 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 2357 case TCP_MD5SIG: 2358 if (!TCPMD5_ENABLED()) { 2359 INP_WUNLOCK(inp); 2360 return (ENOPROTOOPT); 2361 } 2362 error = TCPMD5_PCBCTL(inp, sopt); 2363 break; 2364 #endif 2365 2366 case TCP_NODELAY: 2367 optval = tp->t_flags & TF_NODELAY; 2368 INP_WUNLOCK(inp); 2369 error = sooptcopyout(sopt, &optval, sizeof optval); 2370 break; 2371 case TCP_MAXSEG: 2372 optval = tp->t_maxseg; 2373 INP_WUNLOCK(inp); 2374 error = sooptcopyout(sopt, &optval, sizeof optval); 2375 break; 2376 case TCP_NOOPT: 2377 optval = tp->t_flags & TF_NOOPT; 2378 INP_WUNLOCK(inp); 2379 error = sooptcopyout(sopt, &optval, sizeof optval); 2380 break; 2381 case TCP_NOPUSH: 2382 optval = tp->t_flags & TF_NOPUSH; 2383 INP_WUNLOCK(inp); 2384 error = sooptcopyout(sopt, &optval, sizeof optval); 2385 break; 2386 case TCP_INFO: 2387 tcp_fill_info(tp, &ti); 2388 INP_WUNLOCK(inp); 2389 error = sooptcopyout(sopt, &ti, sizeof ti); 2390 break; 2391 case TCP_STATS: 2392 { 2393 #ifdef STATS 2394 int nheld; 2395 TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0; 2396 2397 error = 0; 2398 socklen_t outsbsz = sopt->sopt_valsize; 2399 if (tp->t_stats == NULL) 2400 error = ENOENT; 2401 else if (outsbsz >= tp->t_stats->cursz) 2402 outsbsz = tp->t_stats->cursz; 2403 else if (outsbsz >= sizeof(struct statsblob)) 2404 outsbsz = sizeof(struct statsblob); 2405 else 2406 error = EINVAL; 2407 INP_WUNLOCK(inp); 2408 if (error) 2409 break; 2410 2411 sbp = sopt->sopt_val; 2412 nheld = atop(round_page(((vm_offset_t)sbp) + 2413 (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp)); 2414 vm_page_t ma[nheld]; 2415 if (vm_fault_quick_hold_pages( 2416 &curproc->p_vmspace->vm_map, (vm_offset_t)sbp, 2417 outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma, 2418 nheld) < 0) { 2419 error = EFAULT; 2420 break; 2421 } 2422 2423 if ((error = copyin_nofault(&(sbp->flags), &sbflags, 2424 SIZEOF_MEMBER(struct statsblob, flags)))) 2425 goto unhold; 2426 2427 INP_WLOCK_RECHECK(inp); 2428 error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats, 2429 sbflags | SB_CLONE_USRDSTNOFAULT); 2430 INP_WUNLOCK(inp); 2431 sopt->sopt_valsize = outsbsz; 2432 unhold: 2433 vm_page_unhold_pages(ma, nheld); 2434 #else 2435 INP_WUNLOCK(inp); 2436 error = EOPNOTSUPP; 2437 #endif /* !STATS */ 2438 break; 2439 } 2440 case TCP_CONGESTION: 2441 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2442 INP_WUNLOCK(inp); 2443 error = sooptcopyout(sopt, buf, len + 1); 2444 break; 2445 case TCP_KEEPIDLE: 2446 case TCP_KEEPINTVL: 2447 case TCP_KEEPINIT: 2448 case TCP_KEEPCNT: 2449 switch (sopt->sopt_name) { 2450 case TCP_KEEPIDLE: 2451 ui = TP_KEEPIDLE(tp) / hz; 2452 break; 2453 case TCP_KEEPINTVL: 2454 ui = TP_KEEPINTVL(tp) / hz; 2455 break; 2456 case TCP_KEEPINIT: 2457 ui = TP_KEEPINIT(tp) / hz; 2458 break; 2459 case TCP_KEEPCNT: 2460 ui = TP_KEEPCNT(tp); 2461 break; 2462 } 2463 INP_WUNLOCK(inp); 2464 error = sooptcopyout(sopt, &ui, sizeof(ui)); 2465 break; 2466 #ifdef TCPPCAP 2467 case TCP_PCAP_OUT: 2468 case TCP_PCAP_IN: 2469 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 2470 &(tp->t_outpkts) : &(tp->t_inpkts)); 2471 INP_WUNLOCK(inp); 2472 error = sooptcopyout(sopt, &optval, sizeof optval); 2473 break; 2474 #endif 2475 case TCP_FASTOPEN: 2476 optval = tp->t_flags & TF_FASTOPEN; 2477 INP_WUNLOCK(inp); 2478 error = sooptcopyout(sopt, &optval, sizeof optval); 2479 break; 2480 #ifdef TCP_BLACKBOX 2481 case TCP_LOG: 2482 optval = tp->t_logstate; 2483 INP_WUNLOCK(inp); 2484 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2485 break; 2486 case TCP_LOGBUF: 2487 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 2488 error = tcp_log_getlogbuf(sopt, tp); 2489 break; 2490 case TCP_LOGID: 2491 len = tcp_log_get_id(tp, buf); 2492 INP_WUNLOCK(inp); 2493 error = sooptcopyout(sopt, buf, len + 1); 2494 break; 2495 case TCP_LOGDUMP: 2496 case TCP_LOGDUMPID: 2497 INP_WUNLOCK(inp); 2498 error = EINVAL; 2499 break; 2500 #endif 2501 #ifdef KERN_TLS 2502 case TCP_TXTLS_MODE: 2503 optval = ktls_get_tx_mode(so); 2504 INP_WUNLOCK(inp); 2505 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2506 break; 2507 case TCP_RXTLS_MODE: 2508 optval = ktls_get_rx_mode(so); 2509 INP_WUNLOCK(inp); 2510 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2511 break; 2512 #endif 2513 default: 2514 INP_WUNLOCK(inp); 2515 error = ENOPROTOOPT; 2516 break; 2517 } 2518 break; 2519 } 2520 return (error); 2521 } 2522 #undef INP_WLOCK_RECHECK 2523 #undef INP_WLOCK_RECHECK_CLEANUP 2524 2525 /* 2526 * Initiate (or continue) disconnect. 2527 * If embryonic state, just send reset (once). 2528 * If in ``let data drain'' option and linger null, just drop. 2529 * Otherwise (hard), mark socket disconnecting and drop 2530 * current input data; switch states based on user close, and 2531 * send segment to peer (with FIN). 2532 */ 2533 static void 2534 tcp_disconnect(struct tcpcb *tp) 2535 { 2536 struct inpcb *inp = tp->t_inpcb; 2537 struct socket *so = inp->inp_socket; 2538 2539 NET_EPOCH_ASSERT(); 2540 INP_WLOCK_ASSERT(inp); 2541 2542 /* 2543 * Neither tcp_close() nor tcp_drop() should return NULL, as the 2544 * socket is still open. 2545 */ 2546 if (tp->t_state < TCPS_ESTABLISHED && 2547 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2548 tp = tcp_close(tp); 2549 KASSERT(tp != NULL, 2550 ("tcp_disconnect: tcp_close() returned NULL")); 2551 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2552 tp = tcp_drop(tp, 0); 2553 KASSERT(tp != NULL, 2554 ("tcp_disconnect: tcp_drop() returned NULL")); 2555 } else { 2556 soisdisconnecting(so); 2557 sbflush(&so->so_rcv); 2558 tcp_usrclosed(tp); 2559 if (!(inp->inp_flags & INP_DROPPED)) 2560 tp->t_fb->tfb_tcp_output(tp); 2561 } 2562 } 2563 2564 /* 2565 * User issued close, and wish to trail through shutdown states: 2566 * if never received SYN, just forget it. If got a SYN from peer, 2567 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2568 * If already got a FIN from peer, then almost done; go to LAST_ACK 2569 * state. In all other cases, have already sent FIN to peer (e.g. 2570 * after PRU_SHUTDOWN), and just have to play tedious game waiting 2571 * for peer to send FIN or not respond to keep-alives, etc. 2572 * We can let the user exit from the close as soon as the FIN is acked. 2573 */ 2574 static void 2575 tcp_usrclosed(struct tcpcb *tp) 2576 { 2577 2578 NET_EPOCH_ASSERT(); 2579 INP_WLOCK_ASSERT(tp->t_inpcb); 2580 2581 switch (tp->t_state) { 2582 case TCPS_LISTEN: 2583 #ifdef TCP_OFFLOAD 2584 tcp_offload_listen_stop(tp); 2585 #endif 2586 tcp_state_change(tp, TCPS_CLOSED); 2587 /* FALLTHROUGH */ 2588 case TCPS_CLOSED: 2589 tp = tcp_close(tp); 2590 /* 2591 * tcp_close() should never return NULL here as the socket is 2592 * still open. 2593 */ 2594 KASSERT(tp != NULL, 2595 ("tcp_usrclosed: tcp_close() returned NULL")); 2596 break; 2597 2598 case TCPS_SYN_SENT: 2599 case TCPS_SYN_RECEIVED: 2600 tp->t_flags |= TF_NEEDFIN; 2601 break; 2602 2603 case TCPS_ESTABLISHED: 2604 tcp_state_change(tp, TCPS_FIN_WAIT_1); 2605 break; 2606 2607 case TCPS_CLOSE_WAIT: 2608 tcp_state_change(tp, TCPS_LAST_ACK); 2609 break; 2610 } 2611 if (tp->t_state >= TCPS_FIN_WAIT_2) { 2612 soisdisconnected(tp->t_inpcb->inp_socket); 2613 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 2614 if (tp->t_state == TCPS_FIN_WAIT_2) { 2615 int timeout; 2616 2617 timeout = (tcp_fast_finwait2_recycle) ? 2618 tcp_finwait2_timeout : TP_MAXIDLE(tp); 2619 tcp_timer_activate(tp, TT_2MSL, timeout); 2620 } 2621 } 2622 } 2623 2624 #ifdef DDB 2625 static void 2626 db_print_indent(int indent) 2627 { 2628 int i; 2629 2630 for (i = 0; i < indent; i++) 2631 db_printf(" "); 2632 } 2633 2634 static void 2635 db_print_tstate(int t_state) 2636 { 2637 2638 switch (t_state) { 2639 case TCPS_CLOSED: 2640 db_printf("TCPS_CLOSED"); 2641 return; 2642 2643 case TCPS_LISTEN: 2644 db_printf("TCPS_LISTEN"); 2645 return; 2646 2647 case TCPS_SYN_SENT: 2648 db_printf("TCPS_SYN_SENT"); 2649 return; 2650 2651 case TCPS_SYN_RECEIVED: 2652 db_printf("TCPS_SYN_RECEIVED"); 2653 return; 2654 2655 case TCPS_ESTABLISHED: 2656 db_printf("TCPS_ESTABLISHED"); 2657 return; 2658 2659 case TCPS_CLOSE_WAIT: 2660 db_printf("TCPS_CLOSE_WAIT"); 2661 return; 2662 2663 case TCPS_FIN_WAIT_1: 2664 db_printf("TCPS_FIN_WAIT_1"); 2665 return; 2666 2667 case TCPS_CLOSING: 2668 db_printf("TCPS_CLOSING"); 2669 return; 2670 2671 case TCPS_LAST_ACK: 2672 db_printf("TCPS_LAST_ACK"); 2673 return; 2674 2675 case TCPS_FIN_WAIT_2: 2676 db_printf("TCPS_FIN_WAIT_2"); 2677 return; 2678 2679 case TCPS_TIME_WAIT: 2680 db_printf("TCPS_TIME_WAIT"); 2681 return; 2682 2683 default: 2684 db_printf("unknown"); 2685 return; 2686 } 2687 } 2688 2689 static void 2690 db_print_tflags(u_int t_flags) 2691 { 2692 int comma; 2693 2694 comma = 0; 2695 if (t_flags & TF_ACKNOW) { 2696 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2697 comma = 1; 2698 } 2699 if (t_flags & TF_DELACK) { 2700 db_printf("%sTF_DELACK", comma ? ", " : ""); 2701 comma = 1; 2702 } 2703 if (t_flags & TF_NODELAY) { 2704 db_printf("%sTF_NODELAY", comma ? ", " : ""); 2705 comma = 1; 2706 } 2707 if (t_flags & TF_NOOPT) { 2708 db_printf("%sTF_NOOPT", comma ? ", " : ""); 2709 comma = 1; 2710 } 2711 if (t_flags & TF_SENTFIN) { 2712 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2713 comma = 1; 2714 } 2715 if (t_flags & TF_REQ_SCALE) { 2716 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2717 comma = 1; 2718 } 2719 if (t_flags & TF_RCVD_SCALE) { 2720 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2721 comma = 1; 2722 } 2723 if (t_flags & TF_REQ_TSTMP) { 2724 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2725 comma = 1; 2726 } 2727 if (t_flags & TF_RCVD_TSTMP) { 2728 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2729 comma = 1; 2730 } 2731 if (t_flags & TF_SACK_PERMIT) { 2732 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2733 comma = 1; 2734 } 2735 if (t_flags & TF_NEEDSYN) { 2736 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2737 comma = 1; 2738 } 2739 if (t_flags & TF_NEEDFIN) { 2740 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2741 comma = 1; 2742 } 2743 if (t_flags & TF_NOPUSH) { 2744 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2745 comma = 1; 2746 } 2747 if (t_flags & TF_MORETOCOME) { 2748 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2749 comma = 1; 2750 } 2751 if (t_flags & TF_LQ_OVERFLOW) { 2752 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2753 comma = 1; 2754 } 2755 if (t_flags & TF_LASTIDLE) { 2756 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2757 comma = 1; 2758 } 2759 if (t_flags & TF_RXWIN0SENT) { 2760 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2761 comma = 1; 2762 } 2763 if (t_flags & TF_FASTRECOVERY) { 2764 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2765 comma = 1; 2766 } 2767 if (t_flags & TF_CONGRECOVERY) { 2768 db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2769 comma = 1; 2770 } 2771 if (t_flags & TF_WASFRECOVERY) { 2772 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2773 comma = 1; 2774 } 2775 if (t_flags & TF_SIGNATURE) { 2776 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2777 comma = 1; 2778 } 2779 if (t_flags & TF_FORCEDATA) { 2780 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2781 comma = 1; 2782 } 2783 if (t_flags & TF_TSO) { 2784 db_printf("%sTF_TSO", comma ? ", " : ""); 2785 comma = 1; 2786 } 2787 if (t_flags & TF_FASTOPEN) { 2788 db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2789 comma = 1; 2790 } 2791 } 2792 2793 static void 2794 db_print_tflags2(u_int t_flags2) 2795 { 2796 int comma; 2797 2798 comma = 0; 2799 if (t_flags2 & TF2_ECN_PERMIT) { 2800 db_printf("%sTF2_ECN_PERMIT", comma ? ", " : ""); 2801 comma = 1; 2802 } 2803 } 2804 2805 2806 static void 2807 db_print_toobflags(char t_oobflags) 2808 { 2809 int comma; 2810 2811 comma = 0; 2812 if (t_oobflags & TCPOOB_HAVEDATA) { 2813 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2814 comma = 1; 2815 } 2816 if (t_oobflags & TCPOOB_HADDATA) { 2817 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2818 comma = 1; 2819 } 2820 } 2821 2822 static void 2823 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2824 { 2825 2826 db_print_indent(indent); 2827 db_printf("%s at %p\n", name, tp); 2828 2829 indent += 2; 2830 2831 db_print_indent(indent); 2832 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2833 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2834 2835 db_print_indent(indent); 2836 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2837 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2838 2839 db_print_indent(indent); 2840 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2841 &tp->t_timers->tt_delack, tp->t_inpcb); 2842 2843 db_print_indent(indent); 2844 db_printf("t_state: %d (", tp->t_state); 2845 db_print_tstate(tp->t_state); 2846 db_printf(")\n"); 2847 2848 db_print_indent(indent); 2849 db_printf("t_flags: 0x%x (", tp->t_flags); 2850 db_print_tflags(tp->t_flags); 2851 db_printf(")\n"); 2852 2853 db_print_indent(indent); 2854 db_printf("t_flags2: 0x%x (", tp->t_flags2); 2855 db_print_tflags2(tp->t_flags2); 2856 db_printf(")\n"); 2857 2858 db_print_indent(indent); 2859 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2860 tp->snd_una, tp->snd_max, tp->snd_nxt); 2861 2862 db_print_indent(indent); 2863 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2864 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2865 2866 db_print_indent(indent); 2867 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2868 tp->iss, tp->irs, tp->rcv_nxt); 2869 2870 db_print_indent(indent); 2871 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2872 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2873 2874 db_print_indent(indent); 2875 db_printf("snd_wnd: %u snd_cwnd: %u\n", 2876 tp->snd_wnd, tp->snd_cwnd); 2877 2878 db_print_indent(indent); 2879 db_printf("snd_ssthresh: %u snd_recover: " 2880 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2881 2882 db_print_indent(indent); 2883 db_printf("t_rcvtime: %u t_startime: %u\n", 2884 tp->t_rcvtime, tp->t_starttime); 2885 2886 db_print_indent(indent); 2887 db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 2888 tp->t_rtttime, tp->t_rtseq); 2889 2890 db_print_indent(indent); 2891 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 2892 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2893 2894 db_print_indent(indent); 2895 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2896 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2897 tp->t_rttbest); 2898 2899 db_print_indent(indent); 2900 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2901 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2902 2903 db_print_indent(indent); 2904 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2905 db_print_toobflags(tp->t_oobflags); 2906 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2907 2908 db_print_indent(indent); 2909 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2910 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2911 2912 db_print_indent(indent); 2913 db_printf("ts_recent: %u ts_recent_age: %u\n", 2914 tp->ts_recent, tp->ts_recent_age); 2915 2916 db_print_indent(indent); 2917 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2918 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2919 2920 db_print_indent(indent); 2921 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 2922 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2923 tp->snd_recover_prev, tp->t_badrxtwin); 2924 2925 db_print_indent(indent); 2926 db_printf("snd_numholes: %d snd_holes first: %p\n", 2927 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2928 2929 db_print_indent(indent); 2930 db_printf("snd_fack: 0x%08x rcv_numsacks: %d\n", 2931 tp->snd_fack, tp->rcv_numsacks); 2932 2933 /* Skip sackblks, sackhint. */ 2934 2935 db_print_indent(indent); 2936 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2937 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2938 } 2939 2940 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2941 { 2942 struct tcpcb *tp; 2943 2944 if (!have_addr) { 2945 db_printf("usage: show tcpcb <addr>\n"); 2946 return; 2947 } 2948 tp = (struct tcpcb *)addr; 2949 2950 db_print_tcpcb(tp, "tcpcb", 0); 2951 } 2952 #endif 2953