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