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