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