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