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