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