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