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 #include "opt_tcpdebug.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.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/sysctl.h> 58 #include <sys/mbuf.h> 59 #ifdef INET6 60 #include <sys/domain.h> 61 #endif /* INET6 */ 62 #include <sys/socket.h> 63 #include <sys/socketvar.h> 64 #include <sys/protosw.h> 65 #include <sys/proc.h> 66 #include <sys/jail.h> 67 #include <sys/syslog.h> 68 69 #ifdef DDB 70 #include <ddb/ddb.h> 71 #endif 72 73 #include <net/if.h> 74 #include <net/if_var.h> 75 #include <net/route.h> 76 #include <net/vnet.h> 77 78 #include <netinet/in.h> 79 #include <netinet/in_kdtrace.h> 80 #include <netinet/in_pcb.h> 81 #include <netinet/in_systm.h> 82 #include <netinet/in_var.h> 83 #include <netinet/ip_var.h> 84 #ifdef INET6 85 #include <netinet/ip6.h> 86 #include <netinet6/in6_pcb.h> 87 #include <netinet6/ip6_var.h> 88 #include <netinet6/scope6_var.h> 89 #endif 90 #include <netinet/tcp.h> 91 #include <netinet/tcp_fsm.h> 92 #include <netinet/tcp_seq.h> 93 #include <netinet/tcp_timer.h> 94 #include <netinet/tcp_var.h> 95 #include <netinet/tcp_log_buf.h> 96 #include <netinet/tcpip.h> 97 #include <netinet/cc/cc.h> 98 #include <netinet/tcp_fastopen.h> 99 #include <netinet/tcp_hpts.h> 100 #ifdef TCPPCAP 101 #include <netinet/tcp_pcap.h> 102 #endif 103 #ifdef TCPDEBUG 104 #include <netinet/tcp_debug.h> 105 #endif 106 #ifdef TCP_OFFLOAD 107 #include <netinet/tcp_offload.h> 108 #endif 109 #include <netipsec/ipsec_support.h> 110 111 /* 112 * TCP protocol interface to socket abstraction. 113 */ 114 static int tcp_attach(struct socket *); 115 #ifdef INET 116 static int tcp_connect(struct tcpcb *, struct sockaddr *, 117 struct thread *td); 118 #endif /* INET */ 119 #ifdef INET6 120 static int tcp6_connect(struct tcpcb *, struct sockaddr *, 121 struct thread *td); 122 #endif /* INET6 */ 123 static void tcp_disconnect(struct tcpcb *); 124 static void tcp_usrclosed(struct tcpcb *); 125 static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 126 127 #ifdef TCPDEBUG 128 #define TCPDEBUG0 int ostate = 0 129 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 130 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 131 tcp_trace(TA_USER, ostate, tp, 0, 0, req) 132 #else 133 #define TCPDEBUG0 134 #define TCPDEBUG1() 135 #define TCPDEBUG2(req) 136 #endif 137 138 /* 139 * TCP attaches to socket via pru_attach(), reserving space, 140 * and an internet control block. 141 */ 142 static int 143 tcp_usr_attach(struct socket *so, int proto, struct thread *td) 144 { 145 struct inpcb *inp; 146 struct tcpcb *tp = NULL; 147 int error; 148 TCPDEBUG0; 149 150 inp = sotoinpcb(so); 151 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 152 TCPDEBUG1(); 153 154 error = tcp_attach(so); 155 if (error) 156 goto out; 157 158 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 159 so->so_linger = TCP_LINGERTIME; 160 161 inp = sotoinpcb(so); 162 tp = intotcpcb(inp); 163 out: 164 TCPDEBUG2(PRU_ATTACH); 165 TCP_PROBE2(debug__user, tp, PRU_ATTACH); 166 return error; 167 } 168 169 /* 170 * tcp_detach is called when the socket layer loses its final reference 171 * to the socket, be it a file descriptor reference, a reference from TCP, 172 * etc. At this point, there is only one case in which we will keep around 173 * inpcb state: time wait. 174 * 175 * This function can probably be re-absorbed back into tcp_usr_detach() now 176 * that there is a single detach path. 177 */ 178 static void 179 tcp_detach(struct socket *so, struct inpcb *inp) 180 { 181 struct tcpcb *tp; 182 183 INP_INFO_LOCK_ASSERT(&V_tcbinfo); 184 INP_WLOCK_ASSERT(inp); 185 186 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 187 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 188 189 tp = intotcpcb(inp); 190 191 if (inp->inp_flags & INP_TIMEWAIT) { 192 /* 193 * There are two cases to handle: one in which the time wait 194 * state is being discarded (INP_DROPPED), and one in which 195 * this connection will remain in timewait. In the former, 196 * it is time to discard all state (except tcptw, which has 197 * already been discarded by the timewait close code, which 198 * should be further up the call stack somewhere). In the 199 * latter case, we detach from the socket, but leave the pcb 200 * present until timewait ends. 201 * 202 * XXXRW: Would it be cleaner to free the tcptw here? 203 * 204 * Astute question indeed, from twtcp perspective there are 205 * four cases to consider: 206 * 207 * #1 tcp_detach is called at tcptw creation time by 208 * tcp_twstart, then do not discard the newly created tcptw 209 * and leave inpcb present until timewait ends 210 * #2 tcp_detach is called at tcptw creation time by 211 * tcp_twstart, but connection is local and tw will be 212 * discarded immediately 213 * #3 tcp_detach is called at timewait end (or reuse) by 214 * tcp_twclose, then the tcptw has already been discarded 215 * (or reused) and inpcb is freed here 216 * #4 tcp_detach is called() after timewait ends (or reuse) 217 * (e.g. by soclose), then tcptw has already been discarded 218 * (or reused) and inpcb is freed here 219 * 220 * In all three cases the tcptw should not be freed here. 221 */ 222 if (inp->inp_flags & INP_DROPPED) { 223 in_pcbdetach(inp); 224 if (__predict_true(tp == NULL)) { 225 in_pcbfree(inp); 226 } else { 227 /* 228 * This case should not happen as in TIMEWAIT 229 * state the inp should not be destroyed before 230 * its tcptw. If INVARIANTS is defined, panic. 231 */ 232 #ifdef INVARIANTS 233 panic("%s: Panic before an inp double-free: " 234 "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 235 , __func__); 236 #else 237 log(LOG_ERR, "%s: Avoid an inp double-free: " 238 "INP_TIMEWAIT && INP_DROPPED && tp != NULL" 239 , __func__); 240 #endif 241 INP_WUNLOCK(inp); 242 } 243 } else { 244 in_pcbdetach(inp); 245 INP_WUNLOCK(inp); 246 } 247 } else { 248 /* 249 * If the connection is not in timewait, we consider two 250 * two conditions: one in which no further processing is 251 * necessary (dropped || embryonic), and one in which TCP is 252 * not yet done, but no longer requires the socket, so the 253 * pcb will persist for the time being. 254 * 255 * XXXRW: Does the second case still occur? 256 */ 257 if (inp->inp_flags & INP_DROPPED || 258 tp->t_state < TCPS_SYN_SENT) { 259 tcp_discardcb(tp); 260 in_pcbdetach(inp); 261 in_pcbfree(inp); 262 } else { 263 in_pcbdetach(inp); 264 INP_WUNLOCK(inp); 265 } 266 } 267 } 268 269 /* 270 * pru_detach() detaches the TCP protocol from the socket. 271 * If the protocol state is non-embryonic, then can't 272 * do this directly: have to initiate a pru_disconnect(), 273 * which may finish later; embryonic TCB's can just 274 * be discarded here. 275 */ 276 static void 277 tcp_usr_detach(struct socket *so) 278 { 279 struct inpcb *inp; 280 int rlock = 0; 281 struct epoch_tracker et; 282 283 inp = sotoinpcb(so); 284 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 285 if (!INP_INFO_WLOCKED(&V_tcbinfo)) { 286 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 287 rlock = 1; 288 } 289 INP_WLOCK(inp); 290 KASSERT(inp->inp_socket != NULL, 291 ("tcp_usr_detach: inp_socket == NULL")); 292 tcp_detach(so, inp); 293 if (rlock) 294 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 295 } 296 297 #ifdef INET 298 /* 299 * Give the socket an address. 300 */ 301 static int 302 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 303 { 304 int error = 0; 305 struct inpcb *inp; 306 struct tcpcb *tp = NULL; 307 struct sockaddr_in *sinp; 308 309 sinp = (struct sockaddr_in *)nam; 310 if (nam->sa_len != sizeof (*sinp)) 311 return (EINVAL); 312 /* 313 * Must check for multicast addresses and disallow binding 314 * to them. 315 */ 316 if (sinp->sin_family == AF_INET && 317 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 318 return (EAFNOSUPPORT); 319 320 TCPDEBUG0; 321 inp = sotoinpcb(so); 322 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 323 INP_WLOCK(inp); 324 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 325 error = EINVAL; 326 goto out; 327 } 328 tp = intotcpcb(inp); 329 TCPDEBUG1(); 330 INP_HASH_WLOCK(&V_tcbinfo); 331 error = in_pcbbind(inp, nam, td->td_ucred); 332 INP_HASH_WUNLOCK(&V_tcbinfo); 333 out: 334 TCPDEBUG2(PRU_BIND); 335 TCP_PROBE2(debug__user, tp, PRU_BIND); 336 INP_WUNLOCK(inp); 337 338 return (error); 339 } 340 #endif /* INET */ 341 342 #ifdef INET6 343 static int 344 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 345 { 346 int error = 0; 347 struct inpcb *inp; 348 struct tcpcb *tp = NULL; 349 struct sockaddr_in6 *sin6; 350 351 sin6 = (struct sockaddr_in6 *)nam; 352 if (nam->sa_len != sizeof (*sin6)) 353 return (EINVAL); 354 /* 355 * Must check for multicast addresses and disallow binding 356 * to them. 357 */ 358 if (sin6->sin6_family == AF_INET6 && 359 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 360 return (EAFNOSUPPORT); 361 362 TCPDEBUG0; 363 inp = sotoinpcb(so); 364 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 365 INP_WLOCK(inp); 366 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 367 error = EINVAL; 368 goto out; 369 } 370 tp = intotcpcb(inp); 371 TCPDEBUG1(); 372 INP_HASH_WLOCK(&V_tcbinfo); 373 inp->inp_vflag &= ~INP_IPV4; 374 inp->inp_vflag |= INP_IPV6; 375 #ifdef INET 376 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 377 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 378 inp->inp_vflag |= INP_IPV4; 379 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 380 struct sockaddr_in sin; 381 382 in6_sin6_2_sin(&sin, sin6); 383 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 384 error = EAFNOSUPPORT; 385 INP_HASH_WUNLOCK(&V_tcbinfo); 386 goto out; 387 } 388 inp->inp_vflag |= INP_IPV4; 389 inp->inp_vflag &= ~INP_IPV6; 390 error = in_pcbbind(inp, (struct sockaddr *)&sin, 391 td->td_ucred); 392 INP_HASH_WUNLOCK(&V_tcbinfo); 393 goto out; 394 } 395 } 396 #endif 397 error = in6_pcbbind(inp, nam, td->td_ucred); 398 INP_HASH_WUNLOCK(&V_tcbinfo); 399 out: 400 TCPDEBUG2(PRU_BIND); 401 TCP_PROBE2(debug__user, tp, PRU_BIND); 402 INP_WUNLOCK(inp); 403 return (error); 404 } 405 #endif /* INET6 */ 406 407 #ifdef INET 408 /* 409 * Prepare to accept connections. 410 */ 411 static int 412 tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 413 { 414 int error = 0; 415 struct inpcb *inp; 416 struct tcpcb *tp = NULL; 417 418 TCPDEBUG0; 419 inp = sotoinpcb(so); 420 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 421 INP_WLOCK(inp); 422 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 423 error = EINVAL; 424 goto out; 425 } 426 tp = intotcpcb(inp); 427 TCPDEBUG1(); 428 SOCK_LOCK(so); 429 error = solisten_proto_check(so); 430 INP_HASH_WLOCK(&V_tcbinfo); 431 if (error == 0 && inp->inp_lport == 0) 432 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 433 INP_HASH_WUNLOCK(&V_tcbinfo); 434 if (error == 0) { 435 tcp_state_change(tp, TCPS_LISTEN); 436 solisten_proto(so, backlog); 437 #ifdef TCP_OFFLOAD 438 if ((so->so_options & SO_NO_OFFLOAD) == 0) 439 tcp_offload_listen_start(tp); 440 #endif 441 } 442 SOCK_UNLOCK(so); 443 444 if (IS_FASTOPEN(tp->t_flags)) 445 tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 446 447 out: 448 TCPDEBUG2(PRU_LISTEN); 449 TCP_PROBE2(debug__user, tp, PRU_LISTEN); 450 INP_WUNLOCK(inp); 451 return (error); 452 } 453 #endif /* INET */ 454 455 #ifdef INET6 456 static int 457 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 458 { 459 int error = 0; 460 struct inpcb *inp; 461 struct tcpcb *tp = NULL; 462 463 TCPDEBUG0; 464 inp = sotoinpcb(so); 465 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 466 INP_WLOCK(inp); 467 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 468 error = EINVAL; 469 goto out; 470 } 471 tp = intotcpcb(inp); 472 TCPDEBUG1(); 473 SOCK_LOCK(so); 474 error = solisten_proto_check(so); 475 INP_HASH_WLOCK(&V_tcbinfo); 476 if (error == 0 && inp->inp_lport == 0) { 477 inp->inp_vflag &= ~INP_IPV4; 478 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 479 inp->inp_vflag |= INP_IPV4; 480 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 481 } 482 INP_HASH_WUNLOCK(&V_tcbinfo); 483 if (error == 0) { 484 tcp_state_change(tp, TCPS_LISTEN); 485 solisten_proto(so, backlog); 486 #ifdef TCP_OFFLOAD 487 if ((so->so_options & SO_NO_OFFLOAD) == 0) 488 tcp_offload_listen_start(tp); 489 #endif 490 } 491 SOCK_UNLOCK(so); 492 493 if (IS_FASTOPEN(tp->t_flags)) 494 tp->t_tfo_pending = tcp_fastopen_alloc_counter(); 495 496 out: 497 TCPDEBUG2(PRU_LISTEN); 498 TCP_PROBE2(debug__user, tp, PRU_LISTEN); 499 INP_WUNLOCK(inp); 500 return (error); 501 } 502 #endif /* INET6 */ 503 504 #ifdef INET 505 /* 506 * Initiate connection to peer. 507 * Create a template for use in transmissions on this connection. 508 * Enter SYN_SENT state, and mark socket as connecting. 509 * Start keep-alive timer, and seed output sequence space. 510 * Send initial segment on connection. 511 */ 512 static int 513 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 514 { 515 int error = 0; 516 struct inpcb *inp; 517 struct tcpcb *tp = NULL; 518 struct sockaddr_in *sinp; 519 520 sinp = (struct sockaddr_in *)nam; 521 if (nam->sa_len != sizeof (*sinp)) 522 return (EINVAL); 523 /* 524 * Must disallow TCP ``connections'' to multicast addresses. 525 */ 526 if (sinp->sin_family == AF_INET 527 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 528 return (EAFNOSUPPORT); 529 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 530 return (error); 531 532 TCPDEBUG0; 533 inp = sotoinpcb(so); 534 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 535 INP_WLOCK(inp); 536 if (inp->inp_flags & INP_TIMEWAIT) { 537 error = EADDRINUSE; 538 goto out; 539 } 540 if (inp->inp_flags & INP_DROPPED) { 541 error = ECONNREFUSED; 542 goto out; 543 } 544 tp = intotcpcb(inp); 545 TCPDEBUG1(); 546 if ((error = tcp_connect(tp, nam, td)) != 0) 547 goto out; 548 #ifdef TCP_OFFLOAD 549 if (registered_toedevs > 0 && 550 (so->so_options & SO_NO_OFFLOAD) == 0 && 551 (error = tcp_offload_connect(so, nam)) == 0) 552 goto out; 553 #endif 554 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 555 error = tp->t_fb->tfb_tcp_output(tp); 556 out: 557 TCPDEBUG2(PRU_CONNECT); 558 TCP_PROBE2(debug__user, tp, PRU_CONNECT); 559 INP_WUNLOCK(inp); 560 return (error); 561 } 562 #endif /* INET */ 563 564 #ifdef INET6 565 static int 566 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 567 { 568 int error = 0; 569 struct inpcb *inp; 570 struct tcpcb *tp = NULL; 571 struct sockaddr_in6 *sin6; 572 573 TCPDEBUG0; 574 575 sin6 = (struct sockaddr_in6 *)nam; 576 if (nam->sa_len != sizeof (*sin6)) 577 return (EINVAL); 578 /* 579 * Must disallow TCP ``connections'' to multicast addresses. 580 */ 581 if (sin6->sin6_family == AF_INET6 582 && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 583 return (EAFNOSUPPORT); 584 585 inp = sotoinpcb(so); 586 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 587 INP_WLOCK(inp); 588 if (inp->inp_flags & INP_TIMEWAIT) { 589 error = EADDRINUSE; 590 goto out; 591 } 592 if (inp->inp_flags & INP_DROPPED) { 593 error = ECONNREFUSED; 594 goto out; 595 } 596 tp = intotcpcb(inp); 597 TCPDEBUG1(); 598 #ifdef INET 599 /* 600 * XXXRW: Some confusion: V4/V6 flags relate to binding, and 601 * therefore probably require the hash lock, which isn't held here. 602 * Is this a significant problem? 603 */ 604 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 605 struct sockaddr_in sin; 606 607 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 608 error = EINVAL; 609 goto out; 610 } 611 if ((inp->inp_vflag & INP_IPV4) == 0) { 612 error = EAFNOSUPPORT; 613 goto out; 614 } 615 616 in6_sin6_2_sin(&sin, sin6); 617 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { 618 error = EAFNOSUPPORT; 619 goto out; 620 } 621 inp->inp_vflag |= INP_IPV4; 622 inp->inp_vflag &= ~INP_IPV6; 623 if ((error = prison_remote_ip4(td->td_ucred, 624 &sin.sin_addr)) != 0) 625 goto out; 626 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 627 goto out; 628 #ifdef TCP_OFFLOAD 629 if (registered_toedevs > 0 && 630 (so->so_options & SO_NO_OFFLOAD) == 0 && 631 (error = tcp_offload_connect(so, nam)) == 0) 632 goto out; 633 #endif 634 error = tp->t_fb->tfb_tcp_output(tp); 635 goto out; 636 } else { 637 if ((inp->inp_vflag & INP_IPV6) == 0) { 638 error = EAFNOSUPPORT; 639 goto out; 640 } 641 } 642 #endif 643 inp->inp_vflag &= ~INP_IPV4; 644 inp->inp_vflag |= INP_IPV6; 645 inp->inp_inc.inc_flags |= INC_ISIPV6; 646 if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0) 647 goto out; 648 if ((error = tcp6_connect(tp, nam, td)) != 0) 649 goto out; 650 #ifdef TCP_OFFLOAD 651 if (registered_toedevs > 0 && 652 (so->so_options & SO_NO_OFFLOAD) == 0 && 653 (error = tcp_offload_connect(so, nam)) == 0) 654 goto out; 655 #endif 656 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 657 error = tp->t_fb->tfb_tcp_output(tp); 658 659 out: 660 TCPDEBUG2(PRU_CONNECT); 661 TCP_PROBE2(debug__user, tp, PRU_CONNECT); 662 INP_WUNLOCK(inp); 663 return (error); 664 } 665 #endif /* INET6 */ 666 667 /* 668 * Initiate disconnect from peer. 669 * If connection never passed embryonic stage, just drop; 670 * else if don't need to let data drain, then can just drop anyways, 671 * else have to begin TCP shutdown process: mark socket disconnecting, 672 * drain unread data, state switch to reflect user close, and 673 * send segment (e.g. FIN) to peer. Socket will be really disconnected 674 * when peer sends FIN and acks ours. 675 * 676 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 677 */ 678 static int 679 tcp_usr_disconnect(struct socket *so) 680 { 681 struct inpcb *inp; 682 struct tcpcb *tp = NULL; 683 struct epoch_tracker et; 684 int error = 0; 685 686 TCPDEBUG0; 687 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 688 inp = sotoinpcb(so); 689 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 690 INP_WLOCK(inp); 691 if (inp->inp_flags & INP_TIMEWAIT) 692 goto out; 693 if (inp->inp_flags & INP_DROPPED) { 694 error = ECONNRESET; 695 goto out; 696 } 697 tp = intotcpcb(inp); 698 TCPDEBUG1(); 699 tcp_disconnect(tp); 700 out: 701 TCPDEBUG2(PRU_DISCONNECT); 702 TCP_PROBE2(debug__user, tp, PRU_DISCONNECT); 703 INP_WUNLOCK(inp); 704 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 705 return (error); 706 } 707 708 #ifdef INET 709 /* 710 * Accept a connection. Essentially all the work is done at higher levels; 711 * just return the address of the peer, storing through addr. 712 */ 713 static int 714 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 715 { 716 int error = 0; 717 struct inpcb *inp = NULL; 718 struct tcpcb *tp = NULL; 719 struct in_addr addr; 720 in_port_t port = 0; 721 TCPDEBUG0; 722 723 if (so->so_state & SS_ISDISCONNECTED) 724 return (ECONNABORTED); 725 726 inp = sotoinpcb(so); 727 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 728 INP_WLOCK(inp); 729 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 730 error = ECONNABORTED; 731 goto out; 732 } 733 tp = intotcpcb(inp); 734 TCPDEBUG1(); 735 736 /* 737 * We inline in_getpeeraddr and COMMON_END here, so that we can 738 * copy the data of interest and defer the malloc until after we 739 * release the lock. 740 */ 741 port = inp->inp_fport; 742 addr = inp->inp_faddr; 743 744 out: 745 TCPDEBUG2(PRU_ACCEPT); 746 TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 747 INP_WUNLOCK(inp); 748 if (error == 0) 749 *nam = in_sockaddr(port, &addr); 750 return error; 751 } 752 #endif /* INET */ 753 754 #ifdef INET6 755 static int 756 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 757 { 758 struct inpcb *inp = NULL; 759 int error = 0; 760 struct tcpcb *tp = NULL; 761 struct in_addr addr; 762 struct in6_addr addr6; 763 struct epoch_tracker et; 764 in_port_t port = 0; 765 int v4 = 0; 766 TCPDEBUG0; 767 768 if (so->so_state & SS_ISDISCONNECTED) 769 return (ECONNABORTED); 770 771 inp = sotoinpcb(so); 772 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 773 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 774 INP_WLOCK(inp); 775 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 776 error = ECONNABORTED; 777 goto out; 778 } 779 tp = intotcpcb(inp); 780 TCPDEBUG1(); 781 782 /* 783 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 784 * copy the data of interest and defer the malloc until after we 785 * release the lock. 786 */ 787 if (inp->inp_vflag & INP_IPV4) { 788 v4 = 1; 789 port = inp->inp_fport; 790 addr = inp->inp_faddr; 791 } else { 792 port = inp->inp_fport; 793 addr6 = inp->in6p_faddr; 794 } 795 796 out: 797 TCPDEBUG2(PRU_ACCEPT); 798 TCP_PROBE2(debug__user, tp, PRU_ACCEPT); 799 INP_WUNLOCK(inp); 800 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 801 if (error == 0) { 802 if (v4) 803 *nam = in6_v4mapsin6_sockaddr(port, &addr); 804 else 805 *nam = in6_sockaddr(port, &addr6); 806 } 807 return error; 808 } 809 #endif /* INET6 */ 810 811 /* 812 * Mark the connection as being incapable of further output. 813 */ 814 static int 815 tcp_usr_shutdown(struct socket *so) 816 { 817 int error = 0; 818 struct inpcb *inp; 819 struct tcpcb *tp = NULL; 820 struct epoch_tracker et; 821 822 TCPDEBUG0; 823 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 824 inp = sotoinpcb(so); 825 KASSERT(inp != NULL, ("inp == NULL")); 826 INP_WLOCK(inp); 827 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 828 error = ECONNRESET; 829 goto out; 830 } 831 tp = intotcpcb(inp); 832 TCPDEBUG1(); 833 socantsendmore(so); 834 tcp_usrclosed(tp); 835 if (!(inp->inp_flags & INP_DROPPED)) 836 error = tp->t_fb->tfb_tcp_output(tp); 837 838 out: 839 TCPDEBUG2(PRU_SHUTDOWN); 840 TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN); 841 INP_WUNLOCK(inp); 842 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 843 844 return (error); 845 } 846 847 /* 848 * After a receive, possibly send window update to peer. 849 */ 850 static int 851 tcp_usr_rcvd(struct socket *so, int flags) 852 { 853 struct inpcb *inp; 854 struct tcpcb *tp = NULL; 855 int error = 0; 856 857 TCPDEBUG0; 858 inp = sotoinpcb(so); 859 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 860 INP_WLOCK(inp); 861 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 862 error = ECONNRESET; 863 goto out; 864 } 865 tp = intotcpcb(inp); 866 TCPDEBUG1(); 867 /* 868 * For passively-created TFO connections, don't attempt a window 869 * update while still in SYN_RECEIVED as this may trigger an early 870 * SYN|ACK. It is preferable to have the SYN|ACK be sent along with 871 * application response data, or failing that, when the DELACK timer 872 * expires. 873 */ 874 if (IS_FASTOPEN(tp->t_flags) && 875 (tp->t_state == TCPS_SYN_RECEIVED)) 876 goto out; 877 #ifdef TCP_OFFLOAD 878 if (tp->t_flags & TF_TOE) 879 tcp_offload_rcvd(tp); 880 else 881 #endif 882 tp->t_fb->tfb_tcp_output(tp); 883 884 out: 885 TCPDEBUG2(PRU_RCVD); 886 TCP_PROBE2(debug__user, tp, PRU_RCVD); 887 INP_WUNLOCK(inp); 888 return (error); 889 } 890 891 /* 892 * Do a send by putting data in output queue and updating urgent 893 * marker if URG set. Possibly send more data. Unlike the other 894 * pru_*() routines, the mbuf chains are our responsibility. We 895 * must either enqueue them or free them. The other pru_* routines 896 * generally are caller-frees. 897 */ 898 static int 899 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 900 struct sockaddr *nam, struct mbuf *control, struct thread *td) 901 { 902 int error = 0; 903 struct inpcb *inp; 904 struct tcpcb *tp = NULL; 905 struct epoch_tracker net_et; 906 #ifdef INET 907 #ifdef INET6 908 struct sockaddr_in sin; 909 #endif 910 struct sockaddr_in *sinp; 911 #endif 912 #ifdef INET6 913 int isipv6; 914 #endif 915 TCPDEBUG0; 916 917 /* 918 * We require the pcbinfo lock if we will close the socket as part of 919 * this call. 920 */ 921 if (flags & PRUS_EOF) 922 INP_INFO_RLOCK_ET(&V_tcbinfo, net_et); 923 inp = sotoinpcb(so); 924 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 925 INP_WLOCK(inp); 926 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 927 if (control) 928 m_freem(control); 929 /* 930 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 931 * for freeing memory. 932 */ 933 if (m && (flags & PRUS_NOTREADY) == 0) 934 m_freem(m); 935 error = ECONNRESET; 936 goto out; 937 } 938 tp = intotcpcb(inp); 939 TCPDEBUG1(); 940 if (nam != NULL && tp->t_state < TCPS_SYN_SENT) { 941 switch (nam->sa_family) { 942 #ifdef INET 943 case AF_INET: 944 sinp = (struct sockaddr_in *)nam; 945 if (sinp->sin_len != sizeof(struct sockaddr_in)) { 946 if (m) 947 m_freem(m); 948 error = EINVAL; 949 goto out; 950 } 951 if ((inp->inp_vflag & INP_IPV6) != 0) { 952 if (m) 953 m_freem(m); 954 error = EAFNOSUPPORT; 955 goto out; 956 } 957 if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 958 if (m) 959 m_freem(m); 960 error = EAFNOSUPPORT; 961 goto out; 962 } 963 if ((error = prison_remote_ip4(td->td_ucred, 964 &sinp->sin_addr))) { 965 if (m) 966 m_freem(m); 967 goto out; 968 } 969 #ifdef INET6 970 isipv6 = 0; 971 #endif 972 break; 973 #endif /* INET */ 974 #ifdef INET6 975 case AF_INET6: 976 { 977 struct sockaddr_in6 *sin6; 978 979 sin6 = (struct sockaddr_in6 *)nam; 980 if (sin6->sin6_len != sizeof(*sin6)) { 981 if (m) 982 m_freem(m); 983 error = EINVAL; 984 goto out; 985 } 986 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 987 if (m) 988 m_freem(m); 989 error = EAFNOSUPPORT; 990 goto out; 991 } 992 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 993 #ifdef INET 994 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 995 error = EINVAL; 996 if (m) 997 m_freem(m); 998 goto out; 999 } 1000 if ((inp->inp_vflag & INP_IPV4) == 0) { 1001 error = EAFNOSUPPORT; 1002 if (m) 1003 m_freem(m); 1004 goto out; 1005 } 1006 inp->inp_vflag &= ~INP_IPV6; 1007 sinp = &sin; 1008 in6_sin6_2_sin(sinp, sin6); 1009 if (IN_MULTICAST( 1010 ntohl(sinp->sin_addr.s_addr))) { 1011 error = EAFNOSUPPORT; 1012 if (m) 1013 m_freem(m); 1014 goto out; 1015 } 1016 if ((error = prison_remote_ip4(td->td_ucred, 1017 &sinp->sin_addr))) { 1018 if (m) 1019 m_freem(m); 1020 goto out; 1021 } 1022 isipv6 = 0; 1023 #else /* !INET */ 1024 error = EAFNOSUPPORT; 1025 if (m) 1026 m_freem(m); 1027 goto out; 1028 #endif /* INET */ 1029 } else { 1030 if ((inp->inp_vflag & INP_IPV6) == 0) { 1031 if (m) 1032 m_freem(m); 1033 error = EAFNOSUPPORT; 1034 goto out; 1035 } 1036 inp->inp_vflag &= ~INP_IPV4; 1037 inp->inp_inc.inc_flags |= INC_ISIPV6; 1038 if ((error = prison_remote_ip6(td->td_ucred, 1039 &sin6->sin6_addr))) { 1040 if (m) 1041 m_freem(m); 1042 goto out; 1043 } 1044 isipv6 = 1; 1045 } 1046 break; 1047 } 1048 #endif /* INET6 */ 1049 default: 1050 if (m) 1051 m_freem(m); 1052 error = EAFNOSUPPORT; 1053 goto out; 1054 } 1055 } 1056 if (control) { 1057 /* TCP doesn't do control messages (rights, creds, etc) */ 1058 if (control->m_len) { 1059 m_freem(control); 1060 if (m) 1061 m_freem(m); 1062 error = EINVAL; 1063 goto out; 1064 } 1065 m_freem(control); /* empty control, just free it */ 1066 } 1067 if (!(flags & PRUS_OOB)) { 1068 sbappendstream(&so->so_snd, m, flags); 1069 if (nam && tp->t_state < TCPS_SYN_SENT) { 1070 /* 1071 * Do implied connect if not yet connected, 1072 * initialize window to default value, and 1073 * initialize maxseg using peer's cached MSS. 1074 */ 1075 #ifdef INET6 1076 if (isipv6) 1077 error = tcp6_connect(tp, nam, td); 1078 #endif /* INET6 */ 1079 #if defined(INET6) && defined(INET) 1080 else 1081 #endif 1082 #ifdef INET 1083 error = tcp_connect(tp, 1084 (struct sockaddr *)sinp, td); 1085 #endif 1086 if (error) 1087 goto out; 1088 if (IS_FASTOPEN(tp->t_flags)) 1089 tcp_fastopen_connect(tp); 1090 else { 1091 tp->snd_wnd = TTCP_CLIENT_SND_WND; 1092 tcp_mss(tp, -1); 1093 } 1094 } 1095 if (flags & PRUS_EOF) { 1096 /* 1097 * Close the send side of the connection after 1098 * the data is sent. 1099 */ 1100 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1101 socantsendmore(so); 1102 tcp_usrclosed(tp); 1103 } 1104 if (!(inp->inp_flags & INP_DROPPED) && 1105 !(flags & PRUS_NOTREADY)) { 1106 if (flags & PRUS_MORETOCOME) 1107 tp->t_flags |= TF_MORETOCOME; 1108 error = tp->t_fb->tfb_tcp_output(tp); 1109 if (flags & PRUS_MORETOCOME) 1110 tp->t_flags &= ~TF_MORETOCOME; 1111 } 1112 } else { 1113 /* 1114 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 1115 */ 1116 SOCKBUF_LOCK(&so->so_snd); 1117 if (sbspace(&so->so_snd) < -512) { 1118 SOCKBUF_UNLOCK(&so->so_snd); 1119 m_freem(m); 1120 error = ENOBUFS; 1121 goto out; 1122 } 1123 /* 1124 * According to RFC961 (Assigned Protocols), 1125 * the urgent pointer points to the last octet 1126 * of urgent data. We continue, however, 1127 * to consider it to indicate the first octet 1128 * of data past the urgent section. 1129 * Otherwise, snd_up should be one lower. 1130 */ 1131 sbappendstream_locked(&so->so_snd, m, flags); 1132 SOCKBUF_UNLOCK(&so->so_snd); 1133 if (nam && tp->t_state < TCPS_SYN_SENT) { 1134 /* 1135 * Do implied connect if not yet connected, 1136 * initialize window to default value, and 1137 * initialize maxseg using peer's cached MSS. 1138 */ 1139 1140 /* 1141 * Not going to contemplate SYN|URG 1142 */ 1143 if (IS_FASTOPEN(tp->t_flags)) 1144 tp->t_flags &= ~TF_FASTOPEN; 1145 #ifdef INET6 1146 if (isipv6) 1147 error = tcp6_connect(tp, nam, td); 1148 #endif /* INET6 */ 1149 #if defined(INET6) && defined(INET) 1150 else 1151 #endif 1152 #ifdef INET 1153 error = tcp_connect(tp, 1154 (struct sockaddr *)sinp, td); 1155 #endif 1156 if (error) 1157 goto out; 1158 tp->snd_wnd = TTCP_CLIENT_SND_WND; 1159 tcp_mss(tp, -1); 1160 } 1161 tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 1162 if (!(flags & PRUS_NOTREADY)) { 1163 tp->t_flags |= TF_FORCEDATA; 1164 error = tp->t_fb->tfb_tcp_output(tp); 1165 tp->t_flags &= ~TF_FORCEDATA; 1166 } 1167 } 1168 TCP_LOG_EVENT(tp, NULL, 1169 &inp->inp_socket->so_rcv, 1170 &inp->inp_socket->so_snd, 1171 TCP_LOG_USERSEND, error, 1172 0, NULL, false); 1173 out: 1174 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 1175 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1176 TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB : 1177 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 1178 INP_WUNLOCK(inp); 1179 if (flags & PRUS_EOF) 1180 INP_INFO_RUNLOCK_ET(&V_tcbinfo, net_et); 1181 return (error); 1182 } 1183 1184 static int 1185 tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 1186 { 1187 struct inpcb *inp; 1188 struct tcpcb *tp; 1189 int error; 1190 1191 inp = sotoinpcb(so); 1192 INP_WLOCK(inp); 1193 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1194 INP_WUNLOCK(inp); 1195 mb_free_notready(m, count); 1196 return (ECONNRESET); 1197 } 1198 tp = intotcpcb(inp); 1199 1200 SOCKBUF_LOCK(&so->so_snd); 1201 error = sbready(&so->so_snd, m, count); 1202 SOCKBUF_UNLOCK(&so->so_snd); 1203 if (error == 0) 1204 error = tp->t_fb->tfb_tcp_output(tp); 1205 INP_WUNLOCK(inp); 1206 1207 return (error); 1208 } 1209 1210 /* 1211 * Abort the TCP. Drop the connection abruptly. 1212 */ 1213 static void 1214 tcp_usr_abort(struct socket *so) 1215 { 1216 struct inpcb *inp; 1217 struct tcpcb *tp = NULL; 1218 struct epoch_tracker et; 1219 TCPDEBUG0; 1220 1221 inp = sotoinpcb(so); 1222 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 1223 1224 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 1225 INP_WLOCK(inp); 1226 KASSERT(inp->inp_socket != NULL, 1227 ("tcp_usr_abort: inp_socket == NULL")); 1228 1229 /* 1230 * If we still have full TCP state, and we're not dropped, drop. 1231 */ 1232 if (!(inp->inp_flags & INP_TIMEWAIT) && 1233 !(inp->inp_flags & INP_DROPPED)) { 1234 tp = intotcpcb(inp); 1235 TCPDEBUG1(); 1236 tp = tcp_drop(tp, ECONNABORTED); 1237 if (tp == NULL) 1238 goto dropped; 1239 TCPDEBUG2(PRU_ABORT); 1240 TCP_PROBE2(debug__user, tp, PRU_ABORT); 1241 } 1242 if (!(inp->inp_flags & INP_DROPPED)) { 1243 SOCK_LOCK(so); 1244 so->so_state |= SS_PROTOREF; 1245 SOCK_UNLOCK(so); 1246 inp->inp_flags |= INP_SOCKREF; 1247 } 1248 INP_WUNLOCK(inp); 1249 dropped: 1250 INP_INFO_RUNLOCK_ET(&V_tcbinfo, 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 TCPDEBUG0; 1263 1264 inp = sotoinpcb(so); 1265 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1266 1267 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 1268 INP_WLOCK(inp); 1269 KASSERT(inp->inp_socket != NULL, 1270 ("tcp_usr_close: inp_socket == NULL")); 1271 1272 /* 1273 * If we still have full TCP state, and we're not dropped, initiate 1274 * a disconnect. 1275 */ 1276 if (!(inp->inp_flags & INP_TIMEWAIT) && 1277 !(inp->inp_flags & INP_DROPPED)) { 1278 tp = intotcpcb(inp); 1279 TCPDEBUG1(); 1280 tcp_disconnect(tp); 1281 TCPDEBUG2(PRU_CLOSE); 1282 TCP_PROBE2(debug__user, tp, PRU_CLOSE); 1283 } 1284 if (!(inp->inp_flags & INP_DROPPED)) { 1285 SOCK_LOCK(so); 1286 so->so_state |= SS_PROTOREF; 1287 SOCK_UNLOCK(so); 1288 inp->inp_flags |= INP_SOCKREF; 1289 } 1290 INP_WUNLOCK(inp); 1291 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1292 } 1293 1294 /* 1295 * Receive out-of-band data. 1296 */ 1297 static int 1298 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 1299 { 1300 int error = 0; 1301 struct inpcb *inp; 1302 struct tcpcb *tp = NULL; 1303 1304 TCPDEBUG0; 1305 inp = sotoinpcb(so); 1306 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 1307 INP_WLOCK(inp); 1308 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1309 error = ECONNRESET; 1310 goto out; 1311 } 1312 tp = intotcpcb(inp); 1313 TCPDEBUG1(); 1314 if ((so->so_oobmark == 0 && 1315 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 1316 so->so_options & SO_OOBINLINE || 1317 tp->t_oobflags & TCPOOB_HADDATA) { 1318 error = EINVAL; 1319 goto out; 1320 } 1321 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 1322 error = EWOULDBLOCK; 1323 goto out; 1324 } 1325 m->m_len = 1; 1326 *mtod(m, caddr_t) = tp->t_iobc; 1327 if ((flags & MSG_PEEK) == 0) 1328 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1329 1330 out: 1331 TCPDEBUG2(PRU_RCVOOB); 1332 TCP_PROBE2(debug__user, tp, PRU_RCVOOB); 1333 INP_WUNLOCK(inp); 1334 return (error); 1335 } 1336 1337 #ifdef INET 1338 struct pr_usrreqs tcp_usrreqs = { 1339 .pru_abort = tcp_usr_abort, 1340 .pru_accept = tcp_usr_accept, 1341 .pru_attach = tcp_usr_attach, 1342 .pru_bind = tcp_usr_bind, 1343 .pru_connect = tcp_usr_connect, 1344 .pru_control = in_control, 1345 .pru_detach = tcp_usr_detach, 1346 .pru_disconnect = tcp_usr_disconnect, 1347 .pru_listen = tcp_usr_listen, 1348 .pru_peeraddr = in_getpeeraddr, 1349 .pru_rcvd = tcp_usr_rcvd, 1350 .pru_rcvoob = tcp_usr_rcvoob, 1351 .pru_send = tcp_usr_send, 1352 .pru_ready = tcp_usr_ready, 1353 .pru_shutdown = tcp_usr_shutdown, 1354 .pru_sockaddr = in_getsockaddr, 1355 .pru_sosetlabel = in_pcbsosetlabel, 1356 .pru_close = tcp_usr_close, 1357 }; 1358 #endif /* INET */ 1359 1360 #ifdef INET6 1361 struct pr_usrreqs tcp6_usrreqs = { 1362 .pru_abort = tcp_usr_abort, 1363 .pru_accept = tcp6_usr_accept, 1364 .pru_attach = tcp_usr_attach, 1365 .pru_bind = tcp6_usr_bind, 1366 .pru_connect = tcp6_usr_connect, 1367 .pru_control = in6_control, 1368 .pru_detach = tcp_usr_detach, 1369 .pru_disconnect = tcp_usr_disconnect, 1370 .pru_listen = tcp6_usr_listen, 1371 .pru_peeraddr = in6_mapped_peeraddr, 1372 .pru_rcvd = tcp_usr_rcvd, 1373 .pru_rcvoob = tcp_usr_rcvoob, 1374 .pru_send = tcp_usr_send, 1375 .pru_ready = tcp_usr_ready, 1376 .pru_shutdown = tcp_usr_shutdown, 1377 .pru_sockaddr = in6_mapped_sockaddr, 1378 .pru_sosetlabel = in_pcbsosetlabel, 1379 .pru_close = tcp_usr_close, 1380 }; 1381 #endif /* INET6 */ 1382 1383 #ifdef INET 1384 /* 1385 * Common subroutine to open a TCP connection to remote host specified 1386 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 1387 * port number if needed. Call in_pcbconnect_setup to do the routing and 1388 * to choose a local host address (interface). If there is an existing 1389 * incarnation of the same connection in TIME-WAIT state and if the remote 1390 * host was sending CC options and if the connection duration was < MSL, then 1391 * truncate the previous TIME-WAIT state and proceed. 1392 * Initialize connection parameters and enter SYN-SENT state. 1393 */ 1394 static int 1395 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1396 { 1397 struct inpcb *inp = tp->t_inpcb, *oinp; 1398 struct socket *so = inp->inp_socket; 1399 struct in_addr laddr; 1400 u_short lport; 1401 int error; 1402 1403 INP_WLOCK_ASSERT(inp); 1404 INP_HASH_WLOCK(&V_tcbinfo); 1405 1406 if (inp->inp_lport == 0) { 1407 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1408 if (error) 1409 goto out; 1410 } 1411 1412 /* 1413 * Cannot simply call in_pcbconnect, because there might be an 1414 * earlier incarnation of this same connection still in 1415 * TIME_WAIT state, creating an ADDRINUSE error. 1416 */ 1417 laddr = inp->inp_laddr; 1418 lport = inp->inp_lport; 1419 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1420 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 1421 if (error && oinp == NULL) 1422 goto out; 1423 if (oinp) { 1424 error = EADDRINUSE; 1425 goto out; 1426 } 1427 inp->inp_laddr = laddr; 1428 in_pcbrehash(inp); 1429 INP_HASH_WUNLOCK(&V_tcbinfo); 1430 1431 /* 1432 * Compute window scaling to request: 1433 * Scale to fit into sweet spot. See tcp_syncache.c. 1434 * XXX: This should move to tcp_output(). 1435 */ 1436 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1437 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1438 tp->request_r_scale++; 1439 1440 soisconnecting(so); 1441 TCPSTAT_INC(tcps_connattempt); 1442 tcp_state_change(tp, TCPS_SYN_SENT); 1443 tp->iss = tcp_new_isn(&inp->inp_inc); 1444 if (tp->t_flags & TF_REQ_TSTMP) 1445 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1446 tcp_sendseqinit(tp); 1447 1448 return 0; 1449 1450 out: 1451 INP_HASH_WUNLOCK(&V_tcbinfo); 1452 return (error); 1453 } 1454 #endif /* INET */ 1455 1456 #ifdef INET6 1457 static int 1458 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1459 { 1460 struct inpcb *inp = tp->t_inpcb; 1461 int error; 1462 1463 INP_WLOCK_ASSERT(inp); 1464 INP_HASH_WLOCK(&V_tcbinfo); 1465 1466 if (inp->inp_lport == 0) { 1467 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1468 if (error) 1469 goto out; 1470 } 1471 error = in6_pcbconnect(inp, nam, td->td_ucred); 1472 if (error != 0) 1473 goto out; 1474 INP_HASH_WUNLOCK(&V_tcbinfo); 1475 1476 /* Compute window scaling to request. */ 1477 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1478 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1479 tp->request_r_scale++; 1480 1481 soisconnecting(inp->inp_socket); 1482 TCPSTAT_INC(tcps_connattempt); 1483 tcp_state_change(tp, TCPS_SYN_SENT); 1484 tp->iss = tcp_new_isn(&inp->inp_inc); 1485 if (tp->t_flags & TF_REQ_TSTMP) 1486 tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc); 1487 tcp_sendseqinit(tp); 1488 1489 return 0; 1490 1491 out: 1492 INP_HASH_WUNLOCK(&V_tcbinfo); 1493 return error; 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(tp->t_inpcb); 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_flags & TF_ECN_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 1553 /* 1554 * tcp_ctloutput() must drop the inpcb lock before performing copyin on 1555 * socket option arguments. When it re-acquires the lock after the copy, it 1556 * has to revalidate that the connection is still valid for the socket 1557 * option. 1558 */ 1559 #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do { \ 1560 INP_WLOCK(inp); \ 1561 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 1562 INP_WUNLOCK(inp); \ 1563 cleanup; \ 1564 return (ECONNRESET); \ 1565 } \ 1566 tp = intotcpcb(inp); \ 1567 } while(0) 1568 #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */) 1569 1570 int 1571 tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1572 { 1573 int error; 1574 struct inpcb *inp; 1575 struct tcpcb *tp; 1576 struct tcp_function_block *blk; 1577 struct tcp_function_set fsn; 1578 1579 error = 0; 1580 inp = sotoinpcb(so); 1581 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1582 if (sopt->sopt_level != IPPROTO_TCP) { 1583 #ifdef INET6 1584 if (inp->inp_vflag & INP_IPV6PROTO) { 1585 error = ip6_ctloutput(so, sopt); 1586 /* 1587 * In case of the IPV6_USE_MIN_MTU socket option, 1588 * the INC_IPV6MINMTU flag to announce a corresponding 1589 * MSS during the initial handshake. 1590 * If the TCP connection is not in the front states, 1591 * just reduce the MSS being used. 1592 * This avoids the sending of TCP segments which will 1593 * be fragmented at the IPv6 layer. 1594 */ 1595 if ((error == 0) && 1596 (sopt->sopt_dir == SOPT_SET) && 1597 (sopt->sopt_level == IPPROTO_IPV6) && 1598 (sopt->sopt_name == IPV6_USE_MIN_MTU)) { 1599 INP_WLOCK(inp); 1600 if ((inp->inp_flags & 1601 (INP_TIMEWAIT | INP_DROPPED))) { 1602 INP_WUNLOCK(inp); 1603 return (ECONNRESET); 1604 } 1605 inp->inp_inc.inc_flags |= INC_IPV6MINMTU; 1606 tp = intotcpcb(inp); 1607 if ((tp->t_state >= TCPS_SYN_SENT) && 1608 (inp->inp_inc.inc_flags & INC_ISIPV6)) { 1609 struct ip6_pktopts *opt; 1610 1611 opt = inp->in6p_outputopts; 1612 if ((opt != NULL) && 1613 (opt->ip6po_minmtu == 1614 IP6PO_MINMTU_ALL)) { 1615 if (tp->t_maxseg > TCP6_MSS) { 1616 tp->t_maxseg = TCP6_MSS; 1617 } 1618 } 1619 } 1620 INP_WUNLOCK(inp); 1621 } 1622 } 1623 #endif /* INET6 */ 1624 #if defined(INET6) && defined(INET) 1625 else 1626 #endif 1627 #ifdef INET 1628 { 1629 error = ip_ctloutput(so, sopt); 1630 } 1631 #endif 1632 return (error); 1633 } 1634 INP_WLOCK(inp); 1635 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1636 INP_WUNLOCK(inp); 1637 return (ECONNRESET); 1638 } 1639 tp = intotcpcb(inp); 1640 /* 1641 * Protect the TCP option TCP_FUNCTION_BLK so 1642 * that a sub-function can *never* overwrite this. 1643 */ 1644 if ((sopt->sopt_dir == SOPT_SET) && 1645 (sopt->sopt_name == TCP_FUNCTION_BLK)) { 1646 INP_WUNLOCK(inp); 1647 error = sooptcopyin(sopt, &fsn, sizeof fsn, 1648 sizeof fsn); 1649 if (error) 1650 return (error); 1651 INP_WLOCK_RECHECK(inp); 1652 blk = find_and_ref_tcp_functions(&fsn); 1653 if (blk == NULL) { 1654 INP_WUNLOCK(inp); 1655 return (ENOENT); 1656 } 1657 if (tp->t_fb == blk) { 1658 /* You already have this */ 1659 refcount_release(&blk->tfb_refcnt); 1660 INP_WUNLOCK(inp); 1661 return (0); 1662 } 1663 if (tp->t_state != TCPS_CLOSED) { 1664 /* 1665 * The user has advanced the state 1666 * past the initial point, we may not 1667 * be able to switch. 1668 */ 1669 if (blk->tfb_tcp_handoff_ok != NULL) { 1670 /* 1671 * Does the stack provide a 1672 * query mechanism, if so it may 1673 * still be possible? 1674 */ 1675 error = (*blk->tfb_tcp_handoff_ok)(tp); 1676 } else 1677 error = EINVAL; 1678 if (error) { 1679 refcount_release(&blk->tfb_refcnt); 1680 INP_WUNLOCK(inp); 1681 return(error); 1682 } 1683 } 1684 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 1685 refcount_release(&blk->tfb_refcnt); 1686 INP_WUNLOCK(inp); 1687 return (ENOENT); 1688 } 1689 /* 1690 * Release the old refcnt, the 1691 * lookup acquired a ref on the 1692 * new one already. 1693 */ 1694 if (tp->t_fb->tfb_tcp_fb_fini) { 1695 /* 1696 * Tell the stack to cleanup with 0 i.e. 1697 * the tcb is not going away. 1698 */ 1699 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 1700 } 1701 #ifdef TCPHPTS 1702 /* Assure that we are not on any hpts */ 1703 tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL); 1704 #endif 1705 if (blk->tfb_tcp_fb_init) { 1706 error = (*blk->tfb_tcp_fb_init)(tp); 1707 if (error) { 1708 refcount_release(&blk->tfb_refcnt); 1709 if (tp->t_fb->tfb_tcp_fb_init) { 1710 if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0) { 1711 /* Fall back failed, drop the connection */ 1712 INP_WUNLOCK(inp); 1713 soabort(so); 1714 return(error); 1715 } 1716 } 1717 goto err_out; 1718 } 1719 } 1720 refcount_release(&tp->t_fb->tfb_refcnt); 1721 tp->t_fb = blk; 1722 #ifdef TCP_OFFLOAD 1723 if (tp->t_flags & TF_TOE) { 1724 tcp_offload_ctloutput(tp, sopt->sopt_dir, 1725 sopt->sopt_name); 1726 } 1727 #endif 1728 err_out: 1729 INP_WUNLOCK(inp); 1730 return (error); 1731 } else if ((sopt->sopt_dir == SOPT_GET) && 1732 (sopt->sopt_name == TCP_FUNCTION_BLK)) { 1733 strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name, 1734 TCP_FUNCTION_NAME_LEN_MAX); 1735 fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 1736 fsn.pcbcnt = tp->t_fb->tfb_refcnt; 1737 INP_WUNLOCK(inp); 1738 error = sooptcopyout(sopt, &fsn, sizeof fsn); 1739 return (error); 1740 } 1741 /* Pass in the INP locked, called must unlock it */ 1742 return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp)); 1743 } 1744 1745 /* 1746 * If this assert becomes untrue, we need to change the size of the buf 1747 * variable in tcp_default_ctloutput(). 1748 */ 1749 #ifdef CTASSERT 1750 CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN); 1751 CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN); 1752 #endif 1753 1754 int 1755 tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp) 1756 { 1757 int error, opt, optval; 1758 u_int ui; 1759 struct tcp_info ti; 1760 #ifdef KERN_TLS 1761 struct tls_enable tls; 1762 #endif 1763 struct cc_algo *algo; 1764 char *pbuf, buf[TCP_LOG_ID_LEN]; 1765 size_t len; 1766 1767 /* 1768 * For TCP_CCALGOOPT forward the control to CC module, for both 1769 * SOPT_SET and SOPT_GET. 1770 */ 1771 switch (sopt->sopt_name) { 1772 case TCP_CCALGOOPT: 1773 INP_WUNLOCK(inp); 1774 if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT) 1775 return (EINVAL); 1776 pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO); 1777 error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize, 1778 sopt->sopt_valsize); 1779 if (error) { 1780 free(pbuf, M_TEMP); 1781 return (error); 1782 } 1783 INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP)); 1784 if (CC_ALGO(tp)->ctl_output != NULL) 1785 error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf); 1786 else 1787 error = ENOENT; 1788 INP_WUNLOCK(inp); 1789 if (error == 0 && sopt->sopt_dir == SOPT_GET) 1790 error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize); 1791 free(pbuf, M_TEMP); 1792 return (error); 1793 } 1794 1795 switch (sopt->sopt_dir) { 1796 case SOPT_SET: 1797 switch (sopt->sopt_name) { 1798 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1799 case TCP_MD5SIG: 1800 if (!TCPMD5_ENABLED()) { 1801 INP_WUNLOCK(inp); 1802 return (ENOPROTOOPT); 1803 } 1804 error = TCPMD5_PCBCTL(inp, sopt); 1805 if (error) 1806 return (error); 1807 goto unlock_and_done; 1808 #endif /* IPSEC */ 1809 1810 case TCP_NODELAY: 1811 case TCP_NOOPT: 1812 INP_WUNLOCK(inp); 1813 error = sooptcopyin(sopt, &optval, sizeof optval, 1814 sizeof optval); 1815 if (error) 1816 return (error); 1817 1818 INP_WLOCK_RECHECK(inp); 1819 switch (sopt->sopt_name) { 1820 case TCP_NODELAY: 1821 opt = TF_NODELAY; 1822 break; 1823 case TCP_NOOPT: 1824 opt = TF_NOOPT; 1825 break; 1826 default: 1827 opt = 0; /* dead code to fool gcc */ 1828 break; 1829 } 1830 1831 if (optval) 1832 tp->t_flags |= opt; 1833 else 1834 tp->t_flags &= ~opt; 1835 unlock_and_done: 1836 #ifdef TCP_OFFLOAD 1837 if (tp->t_flags & TF_TOE) { 1838 tcp_offload_ctloutput(tp, sopt->sopt_dir, 1839 sopt->sopt_name); 1840 } 1841 #endif 1842 INP_WUNLOCK(inp); 1843 break; 1844 1845 case TCP_NOPUSH: 1846 INP_WUNLOCK(inp); 1847 error = sooptcopyin(sopt, &optval, sizeof optval, 1848 sizeof optval); 1849 if (error) 1850 return (error); 1851 1852 INP_WLOCK_RECHECK(inp); 1853 if (optval) 1854 tp->t_flags |= TF_NOPUSH; 1855 else if (tp->t_flags & TF_NOPUSH) { 1856 tp->t_flags &= ~TF_NOPUSH; 1857 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1858 error = tp->t_fb->tfb_tcp_output(tp); 1859 } 1860 goto unlock_and_done; 1861 1862 case TCP_MAXSEG: 1863 INP_WUNLOCK(inp); 1864 error = sooptcopyin(sopt, &optval, sizeof optval, 1865 sizeof optval); 1866 if (error) 1867 return (error); 1868 1869 INP_WLOCK_RECHECK(inp); 1870 if (optval > 0 && optval <= tp->t_maxseg && 1871 optval + 40 >= V_tcp_minmss) 1872 tp->t_maxseg = optval; 1873 else 1874 error = EINVAL; 1875 goto unlock_and_done; 1876 1877 case TCP_INFO: 1878 INP_WUNLOCK(inp); 1879 error = EINVAL; 1880 break; 1881 1882 case TCP_CONGESTION: 1883 INP_WUNLOCK(inp); 1884 error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1); 1885 if (error) 1886 break; 1887 buf[sopt->sopt_valsize] = '\0'; 1888 INP_WLOCK_RECHECK(inp); 1889 CC_LIST_RLOCK(); 1890 STAILQ_FOREACH(algo, &cc_list, entries) 1891 if (strncmp(buf, algo->name, 1892 TCP_CA_NAME_MAX) == 0) 1893 break; 1894 CC_LIST_RUNLOCK(); 1895 if (algo == NULL) { 1896 INP_WUNLOCK(inp); 1897 error = EINVAL; 1898 break; 1899 } 1900 /* 1901 * We hold a write lock over the tcb so it's safe to 1902 * do these things without ordering concerns. 1903 */ 1904 if (CC_ALGO(tp)->cb_destroy != NULL) 1905 CC_ALGO(tp)->cb_destroy(tp->ccv); 1906 CC_DATA(tp) = NULL; 1907 CC_ALGO(tp) = algo; 1908 /* 1909 * If something goes pear shaped initialising the new 1910 * algo, fall back to newreno (which does not 1911 * require initialisation). 1912 */ 1913 if (algo->cb_init != NULL && 1914 algo->cb_init(tp->ccv) != 0) { 1915 CC_ALGO(tp) = &newreno_cc_algo; 1916 /* 1917 * The only reason init should fail is 1918 * because of malloc. 1919 */ 1920 error = ENOMEM; 1921 } 1922 INP_WUNLOCK(inp); 1923 break; 1924 1925 #ifdef KERN_TLS 1926 case TCP_TXTLS_ENABLE: 1927 INP_WUNLOCK(inp); 1928 error = sooptcopyin(sopt, &tls, sizeof(tls), 1929 sizeof(tls)); 1930 if (error) 1931 break; 1932 error = ktls_enable_tx(so, &tls); 1933 break; 1934 case TCP_TXTLS_MODE: 1935 INP_WUNLOCK(inp); 1936 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1937 if (error) 1938 return (error); 1939 if (ui != TCP_TLS_MODE_SW && ui != TCP_TLS_MODE_IFNET) 1940 return (EINVAL); 1941 1942 INP_WLOCK_RECHECK(inp); 1943 error = ktls_set_tx_mode(so, ui); 1944 INP_WUNLOCK(inp); 1945 break; 1946 #endif 1947 1948 case TCP_KEEPIDLE: 1949 case TCP_KEEPINTVL: 1950 case TCP_KEEPINIT: 1951 INP_WUNLOCK(inp); 1952 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1953 if (error) 1954 return (error); 1955 1956 if (ui > (UINT_MAX / hz)) { 1957 error = EINVAL; 1958 break; 1959 } 1960 ui *= hz; 1961 1962 INP_WLOCK_RECHECK(inp); 1963 switch (sopt->sopt_name) { 1964 case TCP_KEEPIDLE: 1965 tp->t_keepidle = ui; 1966 /* 1967 * XXX: better check current remaining 1968 * timeout and "merge" it with new value. 1969 */ 1970 if ((tp->t_state > TCPS_LISTEN) && 1971 (tp->t_state <= TCPS_CLOSING)) 1972 tcp_timer_activate(tp, TT_KEEP, 1973 TP_KEEPIDLE(tp)); 1974 break; 1975 case TCP_KEEPINTVL: 1976 tp->t_keepintvl = ui; 1977 if ((tp->t_state == TCPS_FIN_WAIT_2) && 1978 (TP_MAXIDLE(tp) > 0)) 1979 tcp_timer_activate(tp, TT_2MSL, 1980 TP_MAXIDLE(tp)); 1981 break; 1982 case TCP_KEEPINIT: 1983 tp->t_keepinit = ui; 1984 if (tp->t_state == TCPS_SYN_RECEIVED || 1985 tp->t_state == TCPS_SYN_SENT) 1986 tcp_timer_activate(tp, TT_KEEP, 1987 TP_KEEPINIT(tp)); 1988 break; 1989 } 1990 goto unlock_and_done; 1991 1992 case TCP_KEEPCNT: 1993 INP_WUNLOCK(inp); 1994 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1995 if (error) 1996 return (error); 1997 1998 INP_WLOCK_RECHECK(inp); 1999 tp->t_keepcnt = ui; 2000 if ((tp->t_state == TCPS_FIN_WAIT_2) && 2001 (TP_MAXIDLE(tp) > 0)) 2002 tcp_timer_activate(tp, TT_2MSL, 2003 TP_MAXIDLE(tp)); 2004 goto unlock_and_done; 2005 2006 #ifdef TCPPCAP 2007 case TCP_PCAP_OUT: 2008 case TCP_PCAP_IN: 2009 INP_WUNLOCK(inp); 2010 error = sooptcopyin(sopt, &optval, sizeof optval, 2011 sizeof optval); 2012 if (error) 2013 return (error); 2014 2015 INP_WLOCK_RECHECK(inp); 2016 if (optval >= 0) 2017 tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 2018 &(tp->t_outpkts) : &(tp->t_inpkts), 2019 optval); 2020 else 2021 error = EINVAL; 2022 goto unlock_and_done; 2023 #endif 2024 2025 case TCP_FASTOPEN: { 2026 struct tcp_fastopen tfo_optval; 2027 2028 INP_WUNLOCK(inp); 2029 if (!V_tcp_fastopen_client_enable && 2030 !V_tcp_fastopen_server_enable) 2031 return (EPERM); 2032 2033 error = sooptcopyin(sopt, &tfo_optval, 2034 sizeof(tfo_optval), sizeof(int)); 2035 if (error) 2036 return (error); 2037 2038 INP_WLOCK_RECHECK(inp); 2039 if (tfo_optval.enable) { 2040 if (tp->t_state == TCPS_LISTEN) { 2041 if (!V_tcp_fastopen_server_enable) { 2042 error = EPERM; 2043 goto unlock_and_done; 2044 } 2045 2046 tp->t_flags |= TF_FASTOPEN; 2047 if (tp->t_tfo_pending == NULL) 2048 tp->t_tfo_pending = 2049 tcp_fastopen_alloc_counter(); 2050 } else { 2051 /* 2052 * If a pre-shared key was provided, 2053 * stash it in the client cookie 2054 * field of the tcpcb for use during 2055 * connect. 2056 */ 2057 if (sopt->sopt_valsize == 2058 sizeof(tfo_optval)) { 2059 memcpy(tp->t_tfo_cookie.client, 2060 tfo_optval.psk, 2061 TCP_FASTOPEN_PSK_LEN); 2062 tp->t_tfo_client_cookie_len = 2063 TCP_FASTOPEN_PSK_LEN; 2064 } 2065 tp->t_flags |= TF_FASTOPEN; 2066 } 2067 } else 2068 tp->t_flags &= ~TF_FASTOPEN; 2069 goto unlock_and_done; 2070 } 2071 2072 #ifdef TCP_BLACKBOX 2073 case TCP_LOG: 2074 INP_WUNLOCK(inp); 2075 error = sooptcopyin(sopt, &optval, sizeof optval, 2076 sizeof optval); 2077 if (error) 2078 return (error); 2079 2080 INP_WLOCK_RECHECK(inp); 2081 error = tcp_log_state_change(tp, optval); 2082 goto unlock_and_done; 2083 2084 case TCP_LOGBUF: 2085 INP_WUNLOCK(inp); 2086 error = EINVAL; 2087 break; 2088 2089 case TCP_LOGID: 2090 INP_WUNLOCK(inp); 2091 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 2092 if (error) 2093 break; 2094 buf[sopt->sopt_valsize] = '\0'; 2095 INP_WLOCK_RECHECK(inp); 2096 error = tcp_log_set_id(tp, buf); 2097 /* tcp_log_set_id() unlocks the INP. */ 2098 break; 2099 2100 case TCP_LOGDUMP: 2101 case TCP_LOGDUMPID: 2102 INP_WUNLOCK(inp); 2103 error = 2104 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 2105 if (error) 2106 break; 2107 buf[sopt->sopt_valsize] = '\0'; 2108 INP_WLOCK_RECHECK(inp); 2109 if (sopt->sopt_name == TCP_LOGDUMP) { 2110 error = tcp_log_dump_tp_logbuf(tp, buf, 2111 M_WAITOK, true); 2112 INP_WUNLOCK(inp); 2113 } else { 2114 tcp_log_dump_tp_bucket_logbufs(tp, buf); 2115 /* 2116 * tcp_log_dump_tp_bucket_logbufs() drops the 2117 * INP lock. 2118 */ 2119 } 2120 break; 2121 #endif 2122 2123 default: 2124 INP_WUNLOCK(inp); 2125 error = ENOPROTOOPT; 2126 break; 2127 } 2128 break; 2129 2130 case SOPT_GET: 2131 tp = intotcpcb(inp); 2132 switch (sopt->sopt_name) { 2133 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 2134 case TCP_MD5SIG: 2135 if (!TCPMD5_ENABLED()) { 2136 INP_WUNLOCK(inp); 2137 return (ENOPROTOOPT); 2138 } 2139 error = TCPMD5_PCBCTL(inp, sopt); 2140 break; 2141 #endif 2142 2143 case TCP_NODELAY: 2144 optval = tp->t_flags & TF_NODELAY; 2145 INP_WUNLOCK(inp); 2146 error = sooptcopyout(sopt, &optval, sizeof optval); 2147 break; 2148 case TCP_MAXSEG: 2149 optval = tp->t_maxseg; 2150 INP_WUNLOCK(inp); 2151 error = sooptcopyout(sopt, &optval, sizeof optval); 2152 break; 2153 case TCP_NOOPT: 2154 optval = tp->t_flags & TF_NOOPT; 2155 INP_WUNLOCK(inp); 2156 error = sooptcopyout(sopt, &optval, sizeof optval); 2157 break; 2158 case TCP_NOPUSH: 2159 optval = tp->t_flags & TF_NOPUSH; 2160 INP_WUNLOCK(inp); 2161 error = sooptcopyout(sopt, &optval, sizeof optval); 2162 break; 2163 case TCP_INFO: 2164 tcp_fill_info(tp, &ti); 2165 INP_WUNLOCK(inp); 2166 error = sooptcopyout(sopt, &ti, sizeof ti); 2167 break; 2168 case TCP_CONGESTION: 2169 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2170 INP_WUNLOCK(inp); 2171 error = sooptcopyout(sopt, buf, len + 1); 2172 break; 2173 case TCP_KEEPIDLE: 2174 case TCP_KEEPINTVL: 2175 case TCP_KEEPINIT: 2176 case TCP_KEEPCNT: 2177 switch (sopt->sopt_name) { 2178 case TCP_KEEPIDLE: 2179 ui = TP_KEEPIDLE(tp) / hz; 2180 break; 2181 case TCP_KEEPINTVL: 2182 ui = TP_KEEPINTVL(tp) / hz; 2183 break; 2184 case TCP_KEEPINIT: 2185 ui = TP_KEEPINIT(tp) / hz; 2186 break; 2187 case TCP_KEEPCNT: 2188 ui = TP_KEEPCNT(tp); 2189 break; 2190 } 2191 INP_WUNLOCK(inp); 2192 error = sooptcopyout(sopt, &ui, sizeof(ui)); 2193 break; 2194 #ifdef TCPPCAP 2195 case TCP_PCAP_OUT: 2196 case TCP_PCAP_IN: 2197 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 2198 &(tp->t_outpkts) : &(tp->t_inpkts)); 2199 INP_WUNLOCK(inp); 2200 error = sooptcopyout(sopt, &optval, sizeof optval); 2201 break; 2202 #endif 2203 case TCP_FASTOPEN: 2204 optval = tp->t_flags & TF_FASTOPEN; 2205 INP_WUNLOCK(inp); 2206 error = sooptcopyout(sopt, &optval, sizeof optval); 2207 break; 2208 #ifdef TCP_BLACKBOX 2209 case TCP_LOG: 2210 optval = tp->t_logstate; 2211 INP_WUNLOCK(inp); 2212 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2213 break; 2214 case TCP_LOGBUF: 2215 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 2216 error = tcp_log_getlogbuf(sopt, tp); 2217 break; 2218 case TCP_LOGID: 2219 len = tcp_log_get_id(tp, buf); 2220 INP_WUNLOCK(inp); 2221 error = sooptcopyout(sopt, buf, len + 1); 2222 break; 2223 case TCP_LOGDUMP: 2224 case TCP_LOGDUMPID: 2225 INP_WUNLOCK(inp); 2226 error = EINVAL; 2227 break; 2228 #endif 2229 #ifdef KERN_TLS 2230 case TCP_TXTLS_MODE: 2231 optval = ktls_get_tx_mode(so); 2232 INP_WUNLOCK(inp); 2233 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2234 break; 2235 #endif 2236 default: 2237 INP_WUNLOCK(inp); 2238 error = ENOPROTOOPT; 2239 break; 2240 } 2241 break; 2242 } 2243 return (error); 2244 } 2245 #undef INP_WLOCK_RECHECK 2246 #undef INP_WLOCK_RECHECK_CLEANUP 2247 2248 /* 2249 * Attach TCP protocol to socket, allocating 2250 * internet protocol control block, tcp control block, 2251 * bufer space, and entering LISTEN state if to accept connections. 2252 */ 2253 static int 2254 tcp_attach(struct socket *so) 2255 { 2256 struct tcpcb *tp; 2257 struct inpcb *inp; 2258 struct epoch_tracker et; 2259 int error; 2260 2261 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2262 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 2263 if (error) 2264 return (error); 2265 } 2266 so->so_rcv.sb_flags |= SB_AUTOSIZE; 2267 so->so_snd.sb_flags |= SB_AUTOSIZE; 2268 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 2269 error = in_pcballoc(so, &V_tcbinfo); 2270 if (error) { 2271 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2272 return (error); 2273 } 2274 inp = sotoinpcb(so); 2275 #ifdef INET6 2276 if (inp->inp_vflag & INP_IPV6PROTO) { 2277 inp->inp_vflag |= INP_IPV6; 2278 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 2279 inp->inp_vflag |= INP_IPV4; 2280 inp->in6p_hops = -1; /* use kernel default */ 2281 } 2282 else 2283 #endif 2284 inp->inp_vflag |= INP_IPV4; 2285 tp = tcp_newtcpcb(inp); 2286 if (tp == NULL) { 2287 in_pcbdetach(inp); 2288 in_pcbfree(inp); 2289 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2290 return (ENOBUFS); 2291 } 2292 tp->t_state = TCPS_CLOSED; 2293 INP_WUNLOCK(inp); 2294 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2295 TCPSTATES_INC(TCPS_CLOSED); 2296 return (0); 2297 } 2298 2299 /* 2300 * Initiate (or continue) disconnect. 2301 * If embryonic state, just send reset (once). 2302 * If in ``let data drain'' option and linger null, just drop. 2303 * Otherwise (hard), mark socket disconnecting and drop 2304 * current input data; switch states based on user close, and 2305 * send segment to peer (with FIN). 2306 */ 2307 static void 2308 tcp_disconnect(struct tcpcb *tp) 2309 { 2310 struct inpcb *inp = tp->t_inpcb; 2311 struct socket *so = inp->inp_socket; 2312 2313 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2314 INP_WLOCK_ASSERT(inp); 2315 2316 /* 2317 * Neither tcp_close() nor tcp_drop() should return NULL, as the 2318 * socket is still open. 2319 */ 2320 if (tp->t_state < TCPS_ESTABLISHED && 2321 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2322 tp = tcp_close(tp); 2323 KASSERT(tp != NULL, 2324 ("tcp_disconnect: tcp_close() returned NULL")); 2325 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2326 tp = tcp_drop(tp, 0); 2327 KASSERT(tp != NULL, 2328 ("tcp_disconnect: tcp_drop() returned NULL")); 2329 } else { 2330 soisdisconnecting(so); 2331 sbflush(&so->so_rcv); 2332 tcp_usrclosed(tp); 2333 if (!(inp->inp_flags & INP_DROPPED)) 2334 tp->t_fb->tfb_tcp_output(tp); 2335 } 2336 } 2337 2338 /* 2339 * User issued close, and wish to trail through shutdown states: 2340 * if never received SYN, just forget it. If got a SYN from peer, 2341 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2342 * If already got a FIN from peer, then almost done; go to LAST_ACK 2343 * state. In all other cases, have already sent FIN to peer (e.g. 2344 * after PRU_SHUTDOWN), and just have to play tedious game waiting 2345 * for peer to send FIN or not respond to keep-alives, etc. 2346 * We can let the user exit from the close as soon as the FIN is acked. 2347 */ 2348 static void 2349 tcp_usrclosed(struct tcpcb *tp) 2350 { 2351 2352 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2353 INP_WLOCK_ASSERT(tp->t_inpcb); 2354 2355 switch (tp->t_state) { 2356 case TCPS_LISTEN: 2357 #ifdef TCP_OFFLOAD 2358 tcp_offload_listen_stop(tp); 2359 #endif 2360 tcp_state_change(tp, TCPS_CLOSED); 2361 /* FALLTHROUGH */ 2362 case TCPS_CLOSED: 2363 tp = tcp_close(tp); 2364 /* 2365 * tcp_close() should never return NULL here as the socket is 2366 * still open. 2367 */ 2368 KASSERT(tp != NULL, 2369 ("tcp_usrclosed: tcp_close() returned NULL")); 2370 break; 2371 2372 case TCPS_SYN_SENT: 2373 case TCPS_SYN_RECEIVED: 2374 tp->t_flags |= TF_NEEDFIN; 2375 break; 2376 2377 case TCPS_ESTABLISHED: 2378 tcp_state_change(tp, TCPS_FIN_WAIT_1); 2379 break; 2380 2381 case TCPS_CLOSE_WAIT: 2382 tcp_state_change(tp, TCPS_LAST_ACK); 2383 break; 2384 } 2385 if (tp->t_state >= TCPS_FIN_WAIT_2) { 2386 soisdisconnected(tp->t_inpcb->inp_socket); 2387 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 2388 if (tp->t_state == TCPS_FIN_WAIT_2) { 2389 int timeout; 2390 2391 timeout = (tcp_fast_finwait2_recycle) ? 2392 tcp_finwait2_timeout : TP_MAXIDLE(tp); 2393 tcp_timer_activate(tp, TT_2MSL, timeout); 2394 } 2395 } 2396 } 2397 2398 #ifdef DDB 2399 static void 2400 db_print_indent(int indent) 2401 { 2402 int i; 2403 2404 for (i = 0; i < indent; i++) 2405 db_printf(" "); 2406 } 2407 2408 static void 2409 db_print_tstate(int t_state) 2410 { 2411 2412 switch (t_state) { 2413 case TCPS_CLOSED: 2414 db_printf("TCPS_CLOSED"); 2415 return; 2416 2417 case TCPS_LISTEN: 2418 db_printf("TCPS_LISTEN"); 2419 return; 2420 2421 case TCPS_SYN_SENT: 2422 db_printf("TCPS_SYN_SENT"); 2423 return; 2424 2425 case TCPS_SYN_RECEIVED: 2426 db_printf("TCPS_SYN_RECEIVED"); 2427 return; 2428 2429 case TCPS_ESTABLISHED: 2430 db_printf("TCPS_ESTABLISHED"); 2431 return; 2432 2433 case TCPS_CLOSE_WAIT: 2434 db_printf("TCPS_CLOSE_WAIT"); 2435 return; 2436 2437 case TCPS_FIN_WAIT_1: 2438 db_printf("TCPS_FIN_WAIT_1"); 2439 return; 2440 2441 case TCPS_CLOSING: 2442 db_printf("TCPS_CLOSING"); 2443 return; 2444 2445 case TCPS_LAST_ACK: 2446 db_printf("TCPS_LAST_ACK"); 2447 return; 2448 2449 case TCPS_FIN_WAIT_2: 2450 db_printf("TCPS_FIN_WAIT_2"); 2451 return; 2452 2453 case TCPS_TIME_WAIT: 2454 db_printf("TCPS_TIME_WAIT"); 2455 return; 2456 2457 default: 2458 db_printf("unknown"); 2459 return; 2460 } 2461 } 2462 2463 static void 2464 db_print_tflags(u_int t_flags) 2465 { 2466 int comma; 2467 2468 comma = 0; 2469 if (t_flags & TF_ACKNOW) { 2470 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2471 comma = 1; 2472 } 2473 if (t_flags & TF_DELACK) { 2474 db_printf("%sTF_DELACK", comma ? ", " : ""); 2475 comma = 1; 2476 } 2477 if (t_flags & TF_NODELAY) { 2478 db_printf("%sTF_NODELAY", comma ? ", " : ""); 2479 comma = 1; 2480 } 2481 if (t_flags & TF_NOOPT) { 2482 db_printf("%sTF_NOOPT", comma ? ", " : ""); 2483 comma = 1; 2484 } 2485 if (t_flags & TF_SENTFIN) { 2486 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2487 comma = 1; 2488 } 2489 if (t_flags & TF_REQ_SCALE) { 2490 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2491 comma = 1; 2492 } 2493 if (t_flags & TF_RCVD_SCALE) { 2494 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2495 comma = 1; 2496 } 2497 if (t_flags & TF_REQ_TSTMP) { 2498 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2499 comma = 1; 2500 } 2501 if (t_flags & TF_RCVD_TSTMP) { 2502 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2503 comma = 1; 2504 } 2505 if (t_flags & TF_SACK_PERMIT) { 2506 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2507 comma = 1; 2508 } 2509 if (t_flags & TF_NEEDSYN) { 2510 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2511 comma = 1; 2512 } 2513 if (t_flags & TF_NEEDFIN) { 2514 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2515 comma = 1; 2516 } 2517 if (t_flags & TF_NOPUSH) { 2518 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2519 comma = 1; 2520 } 2521 if (t_flags & TF_MORETOCOME) { 2522 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2523 comma = 1; 2524 } 2525 if (t_flags & TF_LQ_OVERFLOW) { 2526 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2527 comma = 1; 2528 } 2529 if (t_flags & TF_LASTIDLE) { 2530 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2531 comma = 1; 2532 } 2533 if (t_flags & TF_RXWIN0SENT) { 2534 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2535 comma = 1; 2536 } 2537 if (t_flags & TF_FASTRECOVERY) { 2538 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2539 comma = 1; 2540 } 2541 if (t_flags & TF_CONGRECOVERY) { 2542 db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2543 comma = 1; 2544 } 2545 if (t_flags & TF_WASFRECOVERY) { 2546 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2547 comma = 1; 2548 } 2549 if (t_flags & TF_SIGNATURE) { 2550 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2551 comma = 1; 2552 } 2553 if (t_flags & TF_FORCEDATA) { 2554 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2555 comma = 1; 2556 } 2557 if (t_flags & TF_TSO) { 2558 db_printf("%sTF_TSO", comma ? ", " : ""); 2559 comma = 1; 2560 } 2561 if (t_flags & TF_ECN_PERMIT) { 2562 db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 2563 comma = 1; 2564 } 2565 if (t_flags & TF_FASTOPEN) { 2566 db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2567 comma = 1; 2568 } 2569 } 2570 2571 static void 2572 db_print_toobflags(char t_oobflags) 2573 { 2574 int comma; 2575 2576 comma = 0; 2577 if (t_oobflags & TCPOOB_HAVEDATA) { 2578 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2579 comma = 1; 2580 } 2581 if (t_oobflags & TCPOOB_HADDATA) { 2582 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2583 comma = 1; 2584 } 2585 } 2586 2587 static void 2588 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2589 { 2590 2591 db_print_indent(indent); 2592 db_printf("%s at %p\n", name, tp); 2593 2594 indent += 2; 2595 2596 db_print_indent(indent); 2597 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2598 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2599 2600 db_print_indent(indent); 2601 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2602 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2603 2604 db_print_indent(indent); 2605 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2606 &tp->t_timers->tt_delack, tp->t_inpcb); 2607 2608 db_print_indent(indent); 2609 db_printf("t_state: %d (", tp->t_state); 2610 db_print_tstate(tp->t_state); 2611 db_printf(")\n"); 2612 2613 db_print_indent(indent); 2614 db_printf("t_flags: 0x%x (", tp->t_flags); 2615 db_print_tflags(tp->t_flags); 2616 db_printf(")\n"); 2617 2618 db_print_indent(indent); 2619 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2620 tp->snd_una, tp->snd_max, tp->snd_nxt); 2621 2622 db_print_indent(indent); 2623 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2624 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2625 2626 db_print_indent(indent); 2627 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2628 tp->iss, tp->irs, tp->rcv_nxt); 2629 2630 db_print_indent(indent); 2631 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2632 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2633 2634 db_print_indent(indent); 2635 db_printf("snd_wnd: %u snd_cwnd: %u\n", 2636 tp->snd_wnd, tp->snd_cwnd); 2637 2638 db_print_indent(indent); 2639 db_printf("snd_ssthresh: %u snd_recover: " 2640 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2641 2642 db_print_indent(indent); 2643 db_printf("t_rcvtime: %u t_startime: %u\n", 2644 tp->t_rcvtime, tp->t_starttime); 2645 2646 db_print_indent(indent); 2647 db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 2648 tp->t_rtttime, tp->t_rtseq); 2649 2650 db_print_indent(indent); 2651 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 2652 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2653 2654 db_print_indent(indent); 2655 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2656 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2657 tp->t_rttbest); 2658 2659 db_print_indent(indent); 2660 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2661 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2662 2663 db_print_indent(indent); 2664 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2665 db_print_toobflags(tp->t_oobflags); 2666 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2667 2668 db_print_indent(indent); 2669 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2670 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2671 2672 db_print_indent(indent); 2673 db_printf("ts_recent: %u ts_recent_age: %u\n", 2674 tp->ts_recent, tp->ts_recent_age); 2675 2676 db_print_indent(indent); 2677 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2678 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2679 2680 db_print_indent(indent); 2681 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 2682 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2683 tp->snd_recover_prev, tp->t_badrxtwin); 2684 2685 db_print_indent(indent); 2686 db_printf("snd_numholes: %d snd_holes first: %p\n", 2687 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2688 2689 db_print_indent(indent); 2690 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2691 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2692 2693 /* Skip sackblks, sackhint. */ 2694 2695 db_print_indent(indent); 2696 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2697 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2698 } 2699 2700 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2701 { 2702 struct tcpcb *tp; 2703 2704 if (!have_addr) { 2705 db_printf("usage: show tcpcb <addr>\n"); 2706 return; 2707 } 2708 tp = (struct tcpcb *)addr; 2709 2710 db_print_tcpcb(tp, "tcpcb", 0); 2711 } 2712 #endif 2713