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