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