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