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