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 1940 INP_WLOCK_RECHECK(inp); 1941 error = ktls_set_tx_mode(so, ui); 1942 INP_WUNLOCK(inp); 1943 break; 1944 #endif 1945 1946 case TCP_KEEPIDLE: 1947 case TCP_KEEPINTVL: 1948 case TCP_KEEPINIT: 1949 INP_WUNLOCK(inp); 1950 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1951 if (error) 1952 return (error); 1953 1954 if (ui > (UINT_MAX / hz)) { 1955 error = EINVAL; 1956 break; 1957 } 1958 ui *= hz; 1959 1960 INP_WLOCK_RECHECK(inp); 1961 switch (sopt->sopt_name) { 1962 case TCP_KEEPIDLE: 1963 tp->t_keepidle = ui; 1964 /* 1965 * XXX: better check current remaining 1966 * timeout and "merge" it with new value. 1967 */ 1968 if ((tp->t_state > TCPS_LISTEN) && 1969 (tp->t_state <= TCPS_CLOSING)) 1970 tcp_timer_activate(tp, TT_KEEP, 1971 TP_KEEPIDLE(tp)); 1972 break; 1973 case TCP_KEEPINTVL: 1974 tp->t_keepintvl = ui; 1975 if ((tp->t_state == TCPS_FIN_WAIT_2) && 1976 (TP_MAXIDLE(tp) > 0)) 1977 tcp_timer_activate(tp, TT_2MSL, 1978 TP_MAXIDLE(tp)); 1979 break; 1980 case TCP_KEEPINIT: 1981 tp->t_keepinit = ui; 1982 if (tp->t_state == TCPS_SYN_RECEIVED || 1983 tp->t_state == TCPS_SYN_SENT) 1984 tcp_timer_activate(tp, TT_KEEP, 1985 TP_KEEPINIT(tp)); 1986 break; 1987 } 1988 goto unlock_and_done; 1989 1990 case TCP_KEEPCNT: 1991 INP_WUNLOCK(inp); 1992 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1993 if (error) 1994 return (error); 1995 1996 INP_WLOCK_RECHECK(inp); 1997 tp->t_keepcnt = ui; 1998 if ((tp->t_state == TCPS_FIN_WAIT_2) && 1999 (TP_MAXIDLE(tp) > 0)) 2000 tcp_timer_activate(tp, TT_2MSL, 2001 TP_MAXIDLE(tp)); 2002 goto unlock_and_done; 2003 2004 #ifdef TCPPCAP 2005 case TCP_PCAP_OUT: 2006 case TCP_PCAP_IN: 2007 INP_WUNLOCK(inp); 2008 error = sooptcopyin(sopt, &optval, sizeof optval, 2009 sizeof optval); 2010 if (error) 2011 return (error); 2012 2013 INP_WLOCK_RECHECK(inp); 2014 if (optval >= 0) 2015 tcp_pcap_set_sock_max(TCP_PCAP_OUT ? 2016 &(tp->t_outpkts) : &(tp->t_inpkts), 2017 optval); 2018 else 2019 error = EINVAL; 2020 goto unlock_and_done; 2021 #endif 2022 2023 case TCP_FASTOPEN: { 2024 struct tcp_fastopen tfo_optval; 2025 2026 INP_WUNLOCK(inp); 2027 if (!V_tcp_fastopen_client_enable && 2028 !V_tcp_fastopen_server_enable) 2029 return (EPERM); 2030 2031 error = sooptcopyin(sopt, &tfo_optval, 2032 sizeof(tfo_optval), sizeof(int)); 2033 if (error) 2034 return (error); 2035 2036 INP_WLOCK_RECHECK(inp); 2037 if (tfo_optval.enable) { 2038 if (tp->t_state == TCPS_LISTEN) { 2039 if (!V_tcp_fastopen_server_enable) { 2040 error = EPERM; 2041 goto unlock_and_done; 2042 } 2043 2044 tp->t_flags |= TF_FASTOPEN; 2045 if (tp->t_tfo_pending == NULL) 2046 tp->t_tfo_pending = 2047 tcp_fastopen_alloc_counter(); 2048 } else { 2049 /* 2050 * If a pre-shared key was provided, 2051 * stash it in the client cookie 2052 * field of the tcpcb for use during 2053 * connect. 2054 */ 2055 if (sopt->sopt_valsize == 2056 sizeof(tfo_optval)) { 2057 memcpy(tp->t_tfo_cookie.client, 2058 tfo_optval.psk, 2059 TCP_FASTOPEN_PSK_LEN); 2060 tp->t_tfo_client_cookie_len = 2061 TCP_FASTOPEN_PSK_LEN; 2062 } 2063 tp->t_flags |= TF_FASTOPEN; 2064 } 2065 } else 2066 tp->t_flags &= ~TF_FASTOPEN; 2067 goto unlock_and_done; 2068 } 2069 2070 #ifdef TCP_BLACKBOX 2071 case TCP_LOG: 2072 INP_WUNLOCK(inp); 2073 error = sooptcopyin(sopt, &optval, sizeof optval, 2074 sizeof optval); 2075 if (error) 2076 return (error); 2077 2078 INP_WLOCK_RECHECK(inp); 2079 error = tcp_log_state_change(tp, optval); 2080 goto unlock_and_done; 2081 2082 case TCP_LOGBUF: 2083 INP_WUNLOCK(inp); 2084 error = EINVAL; 2085 break; 2086 2087 case TCP_LOGID: 2088 INP_WUNLOCK(inp); 2089 error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0); 2090 if (error) 2091 break; 2092 buf[sopt->sopt_valsize] = '\0'; 2093 INP_WLOCK_RECHECK(inp); 2094 error = tcp_log_set_id(tp, buf); 2095 /* tcp_log_set_id() unlocks the INP. */ 2096 break; 2097 2098 case TCP_LOGDUMP: 2099 case TCP_LOGDUMPID: 2100 INP_WUNLOCK(inp); 2101 error = 2102 sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0); 2103 if (error) 2104 break; 2105 buf[sopt->sopt_valsize] = '\0'; 2106 INP_WLOCK_RECHECK(inp); 2107 if (sopt->sopt_name == TCP_LOGDUMP) { 2108 error = tcp_log_dump_tp_logbuf(tp, buf, 2109 M_WAITOK, true); 2110 INP_WUNLOCK(inp); 2111 } else { 2112 tcp_log_dump_tp_bucket_logbufs(tp, buf); 2113 /* 2114 * tcp_log_dump_tp_bucket_logbufs() drops the 2115 * INP lock. 2116 */ 2117 } 2118 break; 2119 #endif 2120 2121 default: 2122 INP_WUNLOCK(inp); 2123 error = ENOPROTOOPT; 2124 break; 2125 } 2126 break; 2127 2128 case SOPT_GET: 2129 tp = intotcpcb(inp); 2130 switch (sopt->sopt_name) { 2131 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 2132 case TCP_MD5SIG: 2133 if (!TCPMD5_ENABLED()) { 2134 INP_WUNLOCK(inp); 2135 return (ENOPROTOOPT); 2136 } 2137 error = TCPMD5_PCBCTL(inp, sopt); 2138 break; 2139 #endif 2140 2141 case TCP_NODELAY: 2142 optval = tp->t_flags & TF_NODELAY; 2143 INP_WUNLOCK(inp); 2144 error = sooptcopyout(sopt, &optval, sizeof optval); 2145 break; 2146 case TCP_MAXSEG: 2147 optval = tp->t_maxseg; 2148 INP_WUNLOCK(inp); 2149 error = sooptcopyout(sopt, &optval, sizeof optval); 2150 break; 2151 case TCP_NOOPT: 2152 optval = tp->t_flags & TF_NOOPT; 2153 INP_WUNLOCK(inp); 2154 error = sooptcopyout(sopt, &optval, sizeof optval); 2155 break; 2156 case TCP_NOPUSH: 2157 optval = tp->t_flags & TF_NOPUSH; 2158 INP_WUNLOCK(inp); 2159 error = sooptcopyout(sopt, &optval, sizeof optval); 2160 break; 2161 case TCP_INFO: 2162 tcp_fill_info(tp, &ti); 2163 INP_WUNLOCK(inp); 2164 error = sooptcopyout(sopt, &ti, sizeof ti); 2165 break; 2166 case TCP_CONGESTION: 2167 len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 2168 INP_WUNLOCK(inp); 2169 error = sooptcopyout(sopt, buf, len + 1); 2170 break; 2171 case TCP_KEEPIDLE: 2172 case TCP_KEEPINTVL: 2173 case TCP_KEEPINIT: 2174 case TCP_KEEPCNT: 2175 switch (sopt->sopt_name) { 2176 case TCP_KEEPIDLE: 2177 ui = TP_KEEPIDLE(tp) / hz; 2178 break; 2179 case TCP_KEEPINTVL: 2180 ui = TP_KEEPINTVL(tp) / hz; 2181 break; 2182 case TCP_KEEPINIT: 2183 ui = TP_KEEPINIT(tp) / hz; 2184 break; 2185 case TCP_KEEPCNT: 2186 ui = TP_KEEPCNT(tp); 2187 break; 2188 } 2189 INP_WUNLOCK(inp); 2190 error = sooptcopyout(sopt, &ui, sizeof(ui)); 2191 break; 2192 #ifdef TCPPCAP 2193 case TCP_PCAP_OUT: 2194 case TCP_PCAP_IN: 2195 optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ? 2196 &(tp->t_outpkts) : &(tp->t_inpkts)); 2197 INP_WUNLOCK(inp); 2198 error = sooptcopyout(sopt, &optval, sizeof optval); 2199 break; 2200 #endif 2201 case TCP_FASTOPEN: 2202 optval = tp->t_flags & TF_FASTOPEN; 2203 INP_WUNLOCK(inp); 2204 error = sooptcopyout(sopt, &optval, sizeof optval); 2205 break; 2206 #ifdef TCP_BLACKBOX 2207 case TCP_LOG: 2208 optval = tp->t_logstate; 2209 INP_WUNLOCK(inp); 2210 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2211 break; 2212 case TCP_LOGBUF: 2213 /* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */ 2214 error = tcp_log_getlogbuf(sopt, tp); 2215 break; 2216 case TCP_LOGID: 2217 len = tcp_log_get_id(tp, buf); 2218 INP_WUNLOCK(inp); 2219 error = sooptcopyout(sopt, buf, len + 1); 2220 break; 2221 case TCP_LOGDUMP: 2222 case TCP_LOGDUMPID: 2223 INP_WUNLOCK(inp); 2224 error = EINVAL; 2225 break; 2226 #endif 2227 #ifdef KERN_TLS 2228 case TCP_TXTLS_MODE: 2229 optval = ktls_get_tx_mode(so); 2230 INP_WUNLOCK(inp); 2231 error = sooptcopyout(sopt, &optval, sizeof(optval)); 2232 break; 2233 #endif 2234 default: 2235 INP_WUNLOCK(inp); 2236 error = ENOPROTOOPT; 2237 break; 2238 } 2239 break; 2240 } 2241 return (error); 2242 } 2243 #undef INP_WLOCK_RECHECK 2244 #undef INP_WLOCK_RECHECK_CLEANUP 2245 2246 /* 2247 * Attach TCP protocol to socket, allocating 2248 * internet protocol control block, tcp control block, 2249 * bufer space, and entering LISTEN state if to accept connections. 2250 */ 2251 static int 2252 tcp_attach(struct socket *so) 2253 { 2254 struct tcpcb *tp; 2255 struct inpcb *inp; 2256 struct epoch_tracker et; 2257 int error; 2258 2259 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 2260 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 2261 if (error) 2262 return (error); 2263 } 2264 so->so_rcv.sb_flags |= SB_AUTOSIZE; 2265 so->so_snd.sb_flags |= SB_AUTOSIZE; 2266 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 2267 error = in_pcballoc(so, &V_tcbinfo); 2268 if (error) { 2269 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2270 return (error); 2271 } 2272 inp = sotoinpcb(so); 2273 #ifdef INET6 2274 if (inp->inp_vflag & INP_IPV6PROTO) { 2275 inp->inp_vflag |= INP_IPV6; 2276 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 2277 inp->inp_vflag |= INP_IPV4; 2278 inp->in6p_hops = -1; /* use kernel default */ 2279 } 2280 else 2281 #endif 2282 inp->inp_vflag |= INP_IPV4; 2283 tp = tcp_newtcpcb(inp); 2284 if (tp == NULL) { 2285 in_pcbdetach(inp); 2286 in_pcbfree(inp); 2287 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2288 return (ENOBUFS); 2289 } 2290 tp->t_state = TCPS_CLOSED; 2291 INP_WUNLOCK(inp); 2292 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 2293 TCPSTATES_INC(TCPS_CLOSED); 2294 return (0); 2295 } 2296 2297 /* 2298 * Initiate (or continue) disconnect. 2299 * If embryonic state, just send reset (once). 2300 * If in ``let data drain'' option and linger null, just drop. 2301 * Otherwise (hard), mark socket disconnecting and drop 2302 * current input data; switch states based on user close, and 2303 * send segment to peer (with FIN). 2304 */ 2305 static void 2306 tcp_disconnect(struct tcpcb *tp) 2307 { 2308 struct inpcb *inp = tp->t_inpcb; 2309 struct socket *so = inp->inp_socket; 2310 2311 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2312 INP_WLOCK_ASSERT(inp); 2313 2314 /* 2315 * Neither tcp_close() nor tcp_drop() should return NULL, as the 2316 * socket is still open. 2317 */ 2318 if (tp->t_state < TCPS_ESTABLISHED && 2319 !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) { 2320 tp = tcp_close(tp); 2321 KASSERT(tp != NULL, 2322 ("tcp_disconnect: tcp_close() returned NULL")); 2323 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 2324 tp = tcp_drop(tp, 0); 2325 KASSERT(tp != NULL, 2326 ("tcp_disconnect: tcp_drop() returned NULL")); 2327 } else { 2328 soisdisconnecting(so); 2329 sbflush(&so->so_rcv); 2330 tcp_usrclosed(tp); 2331 if (!(inp->inp_flags & INP_DROPPED)) 2332 tp->t_fb->tfb_tcp_output(tp); 2333 } 2334 } 2335 2336 /* 2337 * User issued close, and wish to trail through shutdown states: 2338 * if never received SYN, just forget it. If got a SYN from peer, 2339 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 2340 * If already got a FIN from peer, then almost done; go to LAST_ACK 2341 * state. In all other cases, have already sent FIN to peer (e.g. 2342 * after PRU_SHUTDOWN), and just have to play tedious game waiting 2343 * for peer to send FIN or not respond to keep-alives, etc. 2344 * We can let the user exit from the close as soon as the FIN is acked. 2345 */ 2346 static void 2347 tcp_usrclosed(struct tcpcb *tp) 2348 { 2349 2350 INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 2351 INP_WLOCK_ASSERT(tp->t_inpcb); 2352 2353 switch (tp->t_state) { 2354 case TCPS_LISTEN: 2355 #ifdef TCP_OFFLOAD 2356 tcp_offload_listen_stop(tp); 2357 #endif 2358 tcp_state_change(tp, TCPS_CLOSED); 2359 /* FALLTHROUGH */ 2360 case TCPS_CLOSED: 2361 tp = tcp_close(tp); 2362 /* 2363 * tcp_close() should never return NULL here as the socket is 2364 * still open. 2365 */ 2366 KASSERT(tp != NULL, 2367 ("tcp_usrclosed: tcp_close() returned NULL")); 2368 break; 2369 2370 case TCPS_SYN_SENT: 2371 case TCPS_SYN_RECEIVED: 2372 tp->t_flags |= TF_NEEDFIN; 2373 break; 2374 2375 case TCPS_ESTABLISHED: 2376 tcp_state_change(tp, TCPS_FIN_WAIT_1); 2377 break; 2378 2379 case TCPS_CLOSE_WAIT: 2380 tcp_state_change(tp, TCPS_LAST_ACK); 2381 break; 2382 } 2383 if (tp->t_state >= TCPS_FIN_WAIT_2) { 2384 soisdisconnected(tp->t_inpcb->inp_socket); 2385 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 2386 if (tp->t_state == TCPS_FIN_WAIT_2) { 2387 int timeout; 2388 2389 timeout = (tcp_fast_finwait2_recycle) ? 2390 tcp_finwait2_timeout : TP_MAXIDLE(tp); 2391 tcp_timer_activate(tp, TT_2MSL, timeout); 2392 } 2393 } 2394 } 2395 2396 #ifdef DDB 2397 static void 2398 db_print_indent(int indent) 2399 { 2400 int i; 2401 2402 for (i = 0; i < indent; i++) 2403 db_printf(" "); 2404 } 2405 2406 static void 2407 db_print_tstate(int t_state) 2408 { 2409 2410 switch (t_state) { 2411 case TCPS_CLOSED: 2412 db_printf("TCPS_CLOSED"); 2413 return; 2414 2415 case TCPS_LISTEN: 2416 db_printf("TCPS_LISTEN"); 2417 return; 2418 2419 case TCPS_SYN_SENT: 2420 db_printf("TCPS_SYN_SENT"); 2421 return; 2422 2423 case TCPS_SYN_RECEIVED: 2424 db_printf("TCPS_SYN_RECEIVED"); 2425 return; 2426 2427 case TCPS_ESTABLISHED: 2428 db_printf("TCPS_ESTABLISHED"); 2429 return; 2430 2431 case TCPS_CLOSE_WAIT: 2432 db_printf("TCPS_CLOSE_WAIT"); 2433 return; 2434 2435 case TCPS_FIN_WAIT_1: 2436 db_printf("TCPS_FIN_WAIT_1"); 2437 return; 2438 2439 case TCPS_CLOSING: 2440 db_printf("TCPS_CLOSING"); 2441 return; 2442 2443 case TCPS_LAST_ACK: 2444 db_printf("TCPS_LAST_ACK"); 2445 return; 2446 2447 case TCPS_FIN_WAIT_2: 2448 db_printf("TCPS_FIN_WAIT_2"); 2449 return; 2450 2451 case TCPS_TIME_WAIT: 2452 db_printf("TCPS_TIME_WAIT"); 2453 return; 2454 2455 default: 2456 db_printf("unknown"); 2457 return; 2458 } 2459 } 2460 2461 static void 2462 db_print_tflags(u_int t_flags) 2463 { 2464 int comma; 2465 2466 comma = 0; 2467 if (t_flags & TF_ACKNOW) { 2468 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 2469 comma = 1; 2470 } 2471 if (t_flags & TF_DELACK) { 2472 db_printf("%sTF_DELACK", comma ? ", " : ""); 2473 comma = 1; 2474 } 2475 if (t_flags & TF_NODELAY) { 2476 db_printf("%sTF_NODELAY", comma ? ", " : ""); 2477 comma = 1; 2478 } 2479 if (t_flags & TF_NOOPT) { 2480 db_printf("%sTF_NOOPT", comma ? ", " : ""); 2481 comma = 1; 2482 } 2483 if (t_flags & TF_SENTFIN) { 2484 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 2485 comma = 1; 2486 } 2487 if (t_flags & TF_REQ_SCALE) { 2488 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 2489 comma = 1; 2490 } 2491 if (t_flags & TF_RCVD_SCALE) { 2492 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 2493 comma = 1; 2494 } 2495 if (t_flags & TF_REQ_TSTMP) { 2496 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 2497 comma = 1; 2498 } 2499 if (t_flags & TF_RCVD_TSTMP) { 2500 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 2501 comma = 1; 2502 } 2503 if (t_flags & TF_SACK_PERMIT) { 2504 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 2505 comma = 1; 2506 } 2507 if (t_flags & TF_NEEDSYN) { 2508 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 2509 comma = 1; 2510 } 2511 if (t_flags & TF_NEEDFIN) { 2512 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 2513 comma = 1; 2514 } 2515 if (t_flags & TF_NOPUSH) { 2516 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 2517 comma = 1; 2518 } 2519 if (t_flags & TF_MORETOCOME) { 2520 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 2521 comma = 1; 2522 } 2523 if (t_flags & TF_LQ_OVERFLOW) { 2524 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 2525 comma = 1; 2526 } 2527 if (t_flags & TF_LASTIDLE) { 2528 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 2529 comma = 1; 2530 } 2531 if (t_flags & TF_RXWIN0SENT) { 2532 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 2533 comma = 1; 2534 } 2535 if (t_flags & TF_FASTRECOVERY) { 2536 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 2537 comma = 1; 2538 } 2539 if (t_flags & TF_CONGRECOVERY) { 2540 db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 2541 comma = 1; 2542 } 2543 if (t_flags & TF_WASFRECOVERY) { 2544 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 2545 comma = 1; 2546 } 2547 if (t_flags & TF_SIGNATURE) { 2548 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 2549 comma = 1; 2550 } 2551 if (t_flags & TF_FORCEDATA) { 2552 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 2553 comma = 1; 2554 } 2555 if (t_flags & TF_TSO) { 2556 db_printf("%sTF_TSO", comma ? ", " : ""); 2557 comma = 1; 2558 } 2559 if (t_flags & TF_ECN_PERMIT) { 2560 db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 2561 comma = 1; 2562 } 2563 if (t_flags & TF_FASTOPEN) { 2564 db_printf("%sTF_FASTOPEN", comma ? ", " : ""); 2565 comma = 1; 2566 } 2567 } 2568 2569 static void 2570 db_print_toobflags(char t_oobflags) 2571 { 2572 int comma; 2573 2574 comma = 0; 2575 if (t_oobflags & TCPOOB_HAVEDATA) { 2576 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 2577 comma = 1; 2578 } 2579 if (t_oobflags & TCPOOB_HADDATA) { 2580 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 2581 comma = 1; 2582 } 2583 } 2584 2585 static void 2586 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 2587 { 2588 2589 db_print_indent(indent); 2590 db_printf("%s at %p\n", name, tp); 2591 2592 indent += 2; 2593 2594 db_print_indent(indent); 2595 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 2596 TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 2597 2598 db_print_indent(indent); 2599 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 2600 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 2601 2602 db_print_indent(indent); 2603 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 2604 &tp->t_timers->tt_delack, tp->t_inpcb); 2605 2606 db_print_indent(indent); 2607 db_printf("t_state: %d (", tp->t_state); 2608 db_print_tstate(tp->t_state); 2609 db_printf(")\n"); 2610 2611 db_print_indent(indent); 2612 db_printf("t_flags: 0x%x (", tp->t_flags); 2613 db_print_tflags(tp->t_flags); 2614 db_printf(")\n"); 2615 2616 db_print_indent(indent); 2617 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2618 tp->snd_una, tp->snd_max, tp->snd_nxt); 2619 2620 db_print_indent(indent); 2621 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2622 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2623 2624 db_print_indent(indent); 2625 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2626 tp->iss, tp->irs, tp->rcv_nxt); 2627 2628 db_print_indent(indent); 2629 db_printf("rcv_adv: 0x%08x rcv_wnd: %u rcv_up: 0x%08x\n", 2630 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2631 2632 db_print_indent(indent); 2633 db_printf("snd_wnd: %u snd_cwnd: %u\n", 2634 tp->snd_wnd, tp->snd_cwnd); 2635 2636 db_print_indent(indent); 2637 db_printf("snd_ssthresh: %u snd_recover: " 2638 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2639 2640 db_print_indent(indent); 2641 db_printf("t_rcvtime: %u t_startime: %u\n", 2642 tp->t_rcvtime, tp->t_starttime); 2643 2644 db_print_indent(indent); 2645 db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 2646 tp->t_rtttime, tp->t_rtseq); 2647 2648 db_print_indent(indent); 2649 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 2650 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2651 2652 db_print_indent(indent); 2653 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2654 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2655 tp->t_rttbest); 2656 2657 db_print_indent(indent); 2658 db_printf("t_rttupdated: %lu max_sndwnd: %u t_softerror: %d\n", 2659 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2660 2661 db_print_indent(indent); 2662 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2663 db_print_toobflags(tp->t_oobflags); 2664 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2665 2666 db_print_indent(indent); 2667 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2668 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2669 2670 db_print_indent(indent); 2671 db_printf("ts_recent: %u ts_recent_age: %u\n", 2672 tp->ts_recent, tp->ts_recent_age); 2673 2674 db_print_indent(indent); 2675 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2676 "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2677 2678 db_print_indent(indent); 2679 db_printf("snd_ssthresh_prev: %u snd_recover_prev: 0x%08x " 2680 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2681 tp->snd_recover_prev, tp->t_badrxtwin); 2682 2683 db_print_indent(indent); 2684 db_printf("snd_numholes: %d snd_holes first: %p\n", 2685 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2686 2687 db_print_indent(indent); 2688 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2689 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2690 2691 /* Skip sackblks, sackhint. */ 2692 2693 db_print_indent(indent); 2694 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2695 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2696 } 2697 2698 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2699 { 2700 struct tcpcb *tp; 2701 2702 if (!have_addr) { 2703 db_printf("usage: show tcpcb <addr>\n"); 2704 return; 2705 } 2706 tp = (struct tcpcb *)addr; 2707 2708 db_print_tcpcb(tp, "tcpcb", 0); 2709 } 2710 #endif 2711