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