1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. 4 * Copyright (c) 2006-2007 Robert N. M. Watson 5 * Copyright (c) 2010-2011 Juniper Networks, Inc. 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Robert N. M. Watson under 9 * contract to Juniper Networks, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_ddb.h" 42 #include "opt_inet.h" 43 #include "opt_inet6.h" 44 #include "opt_tcpdebug.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/limits.h> 49 #include <sys/malloc.h> 50 #include <sys/kernel.h> 51 #include <sys/sysctl.h> 52 #include <sys/mbuf.h> 53 #ifdef INET6 54 #include <sys/domain.h> 55 #endif /* INET6 */ 56 #include <sys/socket.h> 57 #include <sys/socketvar.h> 58 #include <sys/protosw.h> 59 #include <sys/proc.h> 60 #include <sys/jail.h> 61 62 #ifdef DDB 63 #include <ddb/ddb.h> 64 #endif 65 66 #include <net/if.h> 67 #include <net/if_var.h> 68 #include <net/route.h> 69 #include <net/vnet.h> 70 71 #include <netinet/cc.h> 72 #include <netinet/in.h> 73 #include <netinet/in_pcb.h> 74 #include <netinet/in_systm.h> 75 #include <netinet/in_var.h> 76 #include <netinet/ip_var.h> 77 #ifdef INET6 78 #include <netinet/ip6.h> 79 #include <netinet6/in6_pcb.h> 80 #include <netinet6/ip6_var.h> 81 #include <netinet6/scope6_var.h> 82 #endif 83 #include <netinet/tcp_fsm.h> 84 #include <netinet/tcp_seq.h> 85 #include <netinet/tcp_timer.h> 86 #include <netinet/tcp_var.h> 87 #include <netinet/tcpip.h> 88 #ifdef TCPDEBUG 89 #include <netinet/tcp_debug.h> 90 #endif 91 #ifdef TCP_OFFLOAD 92 #include <netinet/tcp_offload.h> 93 #endif 94 95 /* 96 * TCP protocol interface to socket abstraction. 97 */ 98 static int tcp_attach(struct socket *); 99 #ifdef INET 100 static int tcp_connect(struct tcpcb *, struct sockaddr *, 101 struct thread *td); 102 #endif /* INET */ 103 #ifdef INET6 104 static int tcp6_connect(struct tcpcb *, struct sockaddr *, 105 struct thread *td); 106 #endif /* INET6 */ 107 static void tcp_disconnect(struct tcpcb *); 108 static void tcp_usrclosed(struct tcpcb *); 109 static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 110 111 #ifdef TCPDEBUG 112 #define TCPDEBUG0 int ostate = 0 113 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 114 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 115 tcp_trace(TA_USER, ostate, tp, 0, 0, req) 116 #else 117 #define TCPDEBUG0 118 #define TCPDEBUG1() 119 #define TCPDEBUG2(req) 120 #endif 121 122 /* 123 * TCP attaches to socket via pru_attach(), reserving space, 124 * and an internet control block. 125 */ 126 static int 127 tcp_usr_attach(struct socket *so, int proto, struct thread *td) 128 { 129 struct inpcb *inp; 130 struct tcpcb *tp = NULL; 131 int error; 132 TCPDEBUG0; 133 134 inp = sotoinpcb(so); 135 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 136 TCPDEBUG1(); 137 138 error = tcp_attach(so); 139 if (error) 140 goto out; 141 142 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 143 so->so_linger = TCP_LINGERTIME; 144 145 inp = sotoinpcb(so); 146 tp = intotcpcb(inp); 147 out: 148 TCPDEBUG2(PRU_ATTACH); 149 return error; 150 } 151 152 /* 153 * tcp_detach is called when the socket layer loses its final reference 154 * to the socket, be it a file descriptor reference, a reference from TCP, 155 * etc. At this point, there is only one case in which we will keep around 156 * inpcb state: time wait. 157 * 158 * This function can probably be re-absorbed back into tcp_usr_detach() now 159 * that there is a single detach path. 160 */ 161 static void 162 tcp_detach(struct socket *so, struct inpcb *inp) 163 { 164 struct tcpcb *tp; 165 166 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 167 INP_WLOCK_ASSERT(inp); 168 169 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 170 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 171 172 tp = intotcpcb(inp); 173 174 if (inp->inp_flags & INP_TIMEWAIT) { 175 /* 176 * There are two cases to handle: one in which the time wait 177 * state is being discarded (INP_DROPPED), and one in which 178 * this connection will remain in timewait. In the former, 179 * it is time to discard all state (except tcptw, which has 180 * already been discarded by the timewait close code, which 181 * should be further up the call stack somewhere). In the 182 * latter case, we detach from the socket, but leave the pcb 183 * present until timewait ends. 184 * 185 * XXXRW: Would it be cleaner to free the tcptw here? 186 * 187 * Astute question indeed, from twtcp perspective there are 188 * three cases to consider: 189 * 190 * #1 tcp_detach is called at tcptw creation time by 191 * tcp_twstart, then do not discard the newly created tcptw 192 * and leave inpcb present until timewait ends 193 * #2 tcp_detach is called at timewait end (or reuse) by 194 * tcp_twclose, then the tcptw has already been discarded 195 * and inpcb is freed here 196 * #3 tcp_detach is called() after timewait ends (or reuse) 197 * (e.g. by soclose), then tcptw has already been discarded 198 * and inpcb is freed here 199 * 200 * In all three cases the tcptw should not be freed here. 201 */ 202 if (inp->inp_flags & INP_DROPPED) { 203 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 204 "INP_DROPPED && tp != NULL")); 205 in_pcbdetach(inp); 206 in_pcbfree(inp); 207 } else { 208 in_pcbdetach(inp); 209 INP_WUNLOCK(inp); 210 } 211 } else { 212 /* 213 * If the connection is not in timewait, we consider two 214 * two conditions: one in which no further processing is 215 * necessary (dropped || embryonic), and one in which TCP is 216 * not yet done, but no longer requires the socket, so the 217 * pcb will persist for the time being. 218 * 219 * XXXRW: Does the second case still occur? 220 */ 221 if (inp->inp_flags & INP_DROPPED || 222 tp->t_state < TCPS_SYN_SENT) { 223 tcp_discardcb(tp); 224 in_pcbdetach(inp); 225 in_pcbfree(inp); 226 } else { 227 in_pcbdetach(inp); 228 INP_WUNLOCK(inp); 229 } 230 } 231 } 232 233 /* 234 * pru_detach() detaches the TCP protocol from the socket. 235 * If the protocol state is non-embryonic, then can't 236 * do this directly: have to initiate a pru_disconnect(), 237 * which may finish later; embryonic TCB's can just 238 * be discarded here. 239 */ 240 static void 241 tcp_usr_detach(struct socket *so) 242 { 243 struct inpcb *inp; 244 245 inp = sotoinpcb(so); 246 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 247 INP_INFO_WLOCK(&V_tcbinfo); 248 INP_WLOCK(inp); 249 KASSERT(inp->inp_socket != NULL, 250 ("tcp_usr_detach: inp_socket == NULL")); 251 tcp_detach(so, inp); 252 INP_INFO_WUNLOCK(&V_tcbinfo); 253 } 254 255 #ifdef INET 256 /* 257 * Give the socket an address. 258 */ 259 static int 260 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 261 { 262 int error = 0; 263 struct inpcb *inp; 264 struct tcpcb *tp = NULL; 265 struct sockaddr_in *sinp; 266 267 sinp = (struct sockaddr_in *)nam; 268 if (nam->sa_len != sizeof (*sinp)) 269 return (EINVAL); 270 /* 271 * Must check for multicast addresses and disallow binding 272 * to them. 273 */ 274 if (sinp->sin_family == AF_INET && 275 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 276 return (EAFNOSUPPORT); 277 278 TCPDEBUG0; 279 inp = sotoinpcb(so); 280 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 281 INP_WLOCK(inp); 282 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 283 error = EINVAL; 284 goto out; 285 } 286 tp = intotcpcb(inp); 287 TCPDEBUG1(); 288 INP_HASH_WLOCK(&V_tcbinfo); 289 error = in_pcbbind(inp, nam, td->td_ucred); 290 INP_HASH_WUNLOCK(&V_tcbinfo); 291 out: 292 TCPDEBUG2(PRU_BIND); 293 INP_WUNLOCK(inp); 294 295 return (error); 296 } 297 #endif /* INET */ 298 299 #ifdef INET6 300 static int 301 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 302 { 303 int error = 0; 304 struct inpcb *inp; 305 struct tcpcb *tp = NULL; 306 struct sockaddr_in6 *sin6p; 307 308 sin6p = (struct sockaddr_in6 *)nam; 309 if (nam->sa_len != sizeof (*sin6p)) 310 return (EINVAL); 311 /* 312 * Must check for multicast addresses and disallow binding 313 * to them. 314 */ 315 if (sin6p->sin6_family == AF_INET6 && 316 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 317 return (EAFNOSUPPORT); 318 319 TCPDEBUG0; 320 inp = sotoinpcb(so); 321 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 322 INP_WLOCK(inp); 323 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 324 error = EINVAL; 325 goto out; 326 } 327 tp = intotcpcb(inp); 328 TCPDEBUG1(); 329 INP_HASH_WLOCK(&V_tcbinfo); 330 inp->inp_vflag &= ~INP_IPV4; 331 inp->inp_vflag |= INP_IPV6; 332 #ifdef INET 333 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 334 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 335 inp->inp_vflag |= INP_IPV4; 336 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 337 struct sockaddr_in sin; 338 339 in6_sin6_2_sin(&sin, sin6p); 340 inp->inp_vflag |= INP_IPV4; 341 inp->inp_vflag &= ~INP_IPV6; 342 error = in_pcbbind(inp, (struct sockaddr *)&sin, 343 td->td_ucred); 344 INP_HASH_WUNLOCK(&V_tcbinfo); 345 goto out; 346 } 347 } 348 #endif 349 error = in6_pcbbind(inp, nam, td->td_ucred); 350 INP_HASH_WUNLOCK(&V_tcbinfo); 351 out: 352 TCPDEBUG2(PRU_BIND); 353 INP_WUNLOCK(inp); 354 return (error); 355 } 356 #endif /* INET6 */ 357 358 #ifdef INET 359 /* 360 * Prepare to accept connections. 361 */ 362 static int 363 tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 364 { 365 int error = 0; 366 struct inpcb *inp; 367 struct tcpcb *tp = NULL; 368 369 TCPDEBUG0; 370 inp = sotoinpcb(so); 371 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 372 INP_WLOCK(inp); 373 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 374 error = EINVAL; 375 goto out; 376 } 377 tp = intotcpcb(inp); 378 TCPDEBUG1(); 379 SOCK_LOCK(so); 380 error = solisten_proto_check(so); 381 INP_HASH_WLOCK(&V_tcbinfo); 382 if (error == 0 && inp->inp_lport == 0) 383 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 384 INP_HASH_WUNLOCK(&V_tcbinfo); 385 if (error == 0) { 386 tcp_state_change(tp, TCPS_LISTEN); 387 solisten_proto(so, backlog); 388 #ifdef TCP_OFFLOAD 389 if ((so->so_options & SO_NO_OFFLOAD) == 0) 390 tcp_offload_listen_start(tp); 391 #endif 392 } 393 SOCK_UNLOCK(so); 394 395 out: 396 TCPDEBUG2(PRU_LISTEN); 397 INP_WUNLOCK(inp); 398 return (error); 399 } 400 #endif /* INET */ 401 402 #ifdef INET6 403 static int 404 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 405 { 406 int error = 0; 407 struct inpcb *inp; 408 struct tcpcb *tp = NULL; 409 410 TCPDEBUG0; 411 inp = sotoinpcb(so); 412 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 413 INP_WLOCK(inp); 414 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 415 error = EINVAL; 416 goto out; 417 } 418 tp = intotcpcb(inp); 419 TCPDEBUG1(); 420 SOCK_LOCK(so); 421 error = solisten_proto_check(so); 422 INP_HASH_WLOCK(&V_tcbinfo); 423 if (error == 0 && inp->inp_lport == 0) { 424 inp->inp_vflag &= ~INP_IPV4; 425 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 426 inp->inp_vflag |= INP_IPV4; 427 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 428 } 429 INP_HASH_WUNLOCK(&V_tcbinfo); 430 if (error == 0) { 431 tcp_state_change(tp, TCPS_LISTEN); 432 solisten_proto(so, backlog); 433 #ifdef TCP_OFFLOAD 434 if ((so->so_options & SO_NO_OFFLOAD) == 0) 435 tcp_offload_listen_start(tp); 436 #endif 437 } 438 SOCK_UNLOCK(so); 439 440 out: 441 TCPDEBUG2(PRU_LISTEN); 442 INP_WUNLOCK(inp); 443 return (error); 444 } 445 #endif /* INET6 */ 446 447 #ifdef INET 448 /* 449 * Initiate connection to peer. 450 * Create a template for use in transmissions on this connection. 451 * Enter SYN_SENT state, and mark socket as connecting. 452 * Start keep-alive timer, and seed output sequence space. 453 * Send initial segment on connection. 454 */ 455 static int 456 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 457 { 458 int error = 0; 459 struct inpcb *inp; 460 struct tcpcb *tp = NULL; 461 struct sockaddr_in *sinp; 462 463 sinp = (struct sockaddr_in *)nam; 464 if (nam->sa_len != sizeof (*sinp)) 465 return (EINVAL); 466 /* 467 * Must disallow TCP ``connections'' to multicast addresses. 468 */ 469 if (sinp->sin_family == AF_INET 470 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 471 return (EAFNOSUPPORT); 472 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 473 return (error); 474 475 TCPDEBUG0; 476 inp = sotoinpcb(so); 477 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 478 INP_WLOCK(inp); 479 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 480 error = EINVAL; 481 goto out; 482 } 483 tp = intotcpcb(inp); 484 TCPDEBUG1(); 485 if ((error = tcp_connect(tp, nam, td)) != 0) 486 goto out; 487 #ifdef TCP_OFFLOAD 488 if (registered_toedevs > 0 && 489 (so->so_options & SO_NO_OFFLOAD) == 0 && 490 (error = tcp_offload_connect(so, nam)) == 0) 491 goto out; 492 #endif 493 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 494 error = tcp_output(tp); 495 out: 496 TCPDEBUG2(PRU_CONNECT); 497 INP_WUNLOCK(inp); 498 return (error); 499 } 500 #endif /* INET */ 501 502 #ifdef INET6 503 static int 504 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 505 { 506 int error = 0; 507 struct inpcb *inp; 508 struct tcpcb *tp = NULL; 509 struct sockaddr_in6 *sin6p; 510 511 TCPDEBUG0; 512 513 sin6p = (struct sockaddr_in6 *)nam; 514 if (nam->sa_len != sizeof (*sin6p)) 515 return (EINVAL); 516 /* 517 * Must disallow TCP ``connections'' to multicast addresses. 518 */ 519 if (sin6p->sin6_family == AF_INET6 520 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 521 return (EAFNOSUPPORT); 522 523 inp = sotoinpcb(so); 524 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 525 INP_WLOCK(inp); 526 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 527 error = EINVAL; 528 goto out; 529 } 530 tp = intotcpcb(inp); 531 TCPDEBUG1(); 532 #ifdef INET 533 /* 534 * XXXRW: Some confusion: V4/V6 flags relate to binding, and 535 * therefore probably require the hash lock, which isn't held here. 536 * Is this a significant problem? 537 */ 538 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 539 struct sockaddr_in sin; 540 541 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 542 error = EINVAL; 543 goto out; 544 } 545 546 in6_sin6_2_sin(&sin, sin6p); 547 inp->inp_vflag |= INP_IPV4; 548 inp->inp_vflag &= ~INP_IPV6; 549 if ((error = prison_remote_ip4(td->td_ucred, 550 &sin.sin_addr)) != 0) 551 goto out; 552 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 553 goto out; 554 #ifdef TCP_OFFLOAD 555 if (registered_toedevs > 0 && 556 (so->so_options & SO_NO_OFFLOAD) == 0 && 557 (error = tcp_offload_connect(so, nam)) == 0) 558 goto out; 559 #endif 560 error = tcp_output(tp); 561 goto out; 562 } 563 #endif 564 inp->inp_vflag &= ~INP_IPV4; 565 inp->inp_vflag |= INP_IPV6; 566 inp->inp_inc.inc_flags |= INC_ISIPV6; 567 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 568 goto out; 569 if ((error = tcp6_connect(tp, nam, td)) != 0) 570 goto out; 571 #ifdef TCP_OFFLOAD 572 if (registered_toedevs > 0 && 573 (so->so_options & SO_NO_OFFLOAD) == 0 && 574 (error = tcp_offload_connect(so, nam)) == 0) 575 goto out; 576 #endif 577 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 578 error = tcp_output(tp); 579 580 out: 581 TCPDEBUG2(PRU_CONNECT); 582 INP_WUNLOCK(inp); 583 return (error); 584 } 585 #endif /* INET6 */ 586 587 /* 588 * Initiate disconnect from peer. 589 * If connection never passed embryonic stage, just drop; 590 * else if don't need to let data drain, then can just drop anyways, 591 * else have to begin TCP shutdown process: mark socket disconnecting, 592 * drain unread data, state switch to reflect user close, and 593 * send segment (e.g. FIN) to peer. Socket will be really disconnected 594 * when peer sends FIN and acks ours. 595 * 596 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 597 */ 598 static int 599 tcp_usr_disconnect(struct socket *so) 600 { 601 struct inpcb *inp; 602 struct tcpcb *tp = NULL; 603 int error = 0; 604 605 TCPDEBUG0; 606 INP_INFO_WLOCK(&V_tcbinfo); 607 inp = sotoinpcb(so); 608 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 609 INP_WLOCK(inp); 610 if (inp->inp_flags & INP_TIMEWAIT) 611 goto out; 612 if (inp->inp_flags & INP_DROPPED) { 613 error = ECONNRESET; 614 goto out; 615 } 616 tp = intotcpcb(inp); 617 TCPDEBUG1(); 618 tcp_disconnect(tp); 619 out: 620 TCPDEBUG2(PRU_DISCONNECT); 621 INP_WUNLOCK(inp); 622 INP_INFO_WUNLOCK(&V_tcbinfo); 623 return (error); 624 } 625 626 #ifdef INET 627 /* 628 * Accept a connection. Essentially all the work is done at higher levels; 629 * just return the address of the peer, storing through addr. 630 */ 631 static int 632 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 633 { 634 int error = 0; 635 struct inpcb *inp = NULL; 636 struct tcpcb *tp = NULL; 637 struct in_addr addr; 638 in_port_t port = 0; 639 TCPDEBUG0; 640 641 if (so->so_state & SS_ISDISCONNECTED) 642 return (ECONNABORTED); 643 644 inp = sotoinpcb(so); 645 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 646 INP_WLOCK(inp); 647 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 648 error = ECONNABORTED; 649 goto out; 650 } 651 tp = intotcpcb(inp); 652 TCPDEBUG1(); 653 654 /* 655 * We inline in_getpeeraddr and COMMON_END here, so that we can 656 * copy the data of interest and defer the malloc until after we 657 * release the lock. 658 */ 659 port = inp->inp_fport; 660 addr = inp->inp_faddr; 661 662 out: 663 TCPDEBUG2(PRU_ACCEPT); 664 INP_WUNLOCK(inp); 665 if (error == 0) 666 *nam = in_sockaddr(port, &addr); 667 return error; 668 } 669 #endif /* INET */ 670 671 #ifdef INET6 672 static int 673 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 674 { 675 struct inpcb *inp = NULL; 676 int error = 0; 677 struct tcpcb *tp = NULL; 678 struct in_addr addr; 679 struct in6_addr addr6; 680 in_port_t port = 0; 681 int v4 = 0; 682 TCPDEBUG0; 683 684 if (so->so_state & SS_ISDISCONNECTED) 685 return (ECONNABORTED); 686 687 inp = sotoinpcb(so); 688 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 689 INP_INFO_RLOCK(&V_tcbinfo); 690 INP_WLOCK(inp); 691 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 692 error = ECONNABORTED; 693 goto out; 694 } 695 tp = intotcpcb(inp); 696 TCPDEBUG1(); 697 698 /* 699 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 700 * copy the data of interest and defer the malloc until after we 701 * release the lock. 702 */ 703 if (inp->inp_vflag & INP_IPV4) { 704 v4 = 1; 705 port = inp->inp_fport; 706 addr = inp->inp_faddr; 707 } else { 708 port = inp->inp_fport; 709 addr6 = inp->in6p_faddr; 710 } 711 712 out: 713 TCPDEBUG2(PRU_ACCEPT); 714 INP_WUNLOCK(inp); 715 INP_INFO_RUNLOCK(&V_tcbinfo); 716 if (error == 0) { 717 if (v4) 718 *nam = in6_v4mapsin6_sockaddr(port, &addr); 719 else 720 *nam = in6_sockaddr(port, &addr6); 721 } 722 return error; 723 } 724 #endif /* INET6 */ 725 726 /* 727 * Mark the connection as being incapable of further output. 728 */ 729 static int 730 tcp_usr_shutdown(struct socket *so) 731 { 732 int error = 0; 733 struct inpcb *inp; 734 struct tcpcb *tp = NULL; 735 736 TCPDEBUG0; 737 INP_INFO_WLOCK(&V_tcbinfo); 738 inp = sotoinpcb(so); 739 KASSERT(inp != NULL, ("inp == NULL")); 740 INP_WLOCK(inp); 741 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 742 error = ECONNRESET; 743 goto out; 744 } 745 tp = intotcpcb(inp); 746 TCPDEBUG1(); 747 socantsendmore(so); 748 tcp_usrclosed(tp); 749 if (!(inp->inp_flags & INP_DROPPED)) 750 error = tcp_output(tp); 751 752 out: 753 TCPDEBUG2(PRU_SHUTDOWN); 754 INP_WUNLOCK(inp); 755 INP_INFO_WUNLOCK(&V_tcbinfo); 756 757 return (error); 758 } 759 760 /* 761 * After a receive, possibly send window update to peer. 762 */ 763 static int 764 tcp_usr_rcvd(struct socket *so, int flags) 765 { 766 struct inpcb *inp; 767 struct tcpcb *tp = NULL; 768 int error = 0; 769 770 TCPDEBUG0; 771 inp = sotoinpcb(so); 772 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 773 INP_WLOCK(inp); 774 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 775 error = ECONNRESET; 776 goto out; 777 } 778 tp = intotcpcb(inp); 779 TCPDEBUG1(); 780 #ifdef TCP_OFFLOAD 781 if (tp->t_flags & TF_TOE) 782 tcp_offload_rcvd(tp); 783 else 784 #endif 785 tcp_output(tp); 786 787 out: 788 TCPDEBUG2(PRU_RCVD); 789 INP_WUNLOCK(inp); 790 return (error); 791 } 792 793 /* 794 * Do a send by putting data in output queue and updating urgent 795 * marker if URG set. Possibly send more data. Unlike the other 796 * pru_*() routines, the mbuf chains are our responsibility. We 797 * must either enqueue them or free them. The other pru_* routines 798 * generally are caller-frees. 799 */ 800 static int 801 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 802 struct sockaddr *nam, struct mbuf *control, struct thread *td) 803 { 804 int error = 0; 805 struct inpcb *inp; 806 struct tcpcb *tp = NULL; 807 #ifdef INET6 808 int isipv6; 809 #endif 810 TCPDEBUG0; 811 812 /* 813 * We require the pcbinfo lock if we will close the socket as part of 814 * this call. 815 */ 816 if (flags & PRUS_EOF) 817 INP_INFO_WLOCK(&V_tcbinfo); 818 inp = sotoinpcb(so); 819 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 820 INP_WLOCK(inp); 821 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 822 if (control) 823 m_freem(control); 824 if (m) 825 m_freem(m); 826 error = ECONNRESET; 827 goto out; 828 } 829 #ifdef INET6 830 isipv6 = nam && nam->sa_family == AF_INET6; 831 #endif /* INET6 */ 832 tp = intotcpcb(inp); 833 TCPDEBUG1(); 834 if (control) { 835 /* TCP doesn't do control messages (rights, creds, etc) */ 836 if (control->m_len) { 837 m_freem(control); 838 if (m) 839 m_freem(m); 840 error = EINVAL; 841 goto out; 842 } 843 m_freem(control); /* empty control, just free it */ 844 } 845 if (!(flags & PRUS_OOB)) { 846 sbappendstream(&so->so_snd, m); 847 if (nam && tp->t_state < TCPS_SYN_SENT) { 848 /* 849 * Do implied connect if not yet connected, 850 * initialize window to default value, and 851 * initialize maxseg/maxopd using peer's cached 852 * MSS. 853 */ 854 #ifdef INET6 855 if (isipv6) 856 error = tcp6_connect(tp, nam, td); 857 #endif /* INET6 */ 858 #if defined(INET6) && defined(INET) 859 else 860 #endif 861 #ifdef INET 862 error = tcp_connect(tp, nam, td); 863 #endif 864 if (error) 865 goto out; 866 tp->snd_wnd = TTCP_CLIENT_SND_WND; 867 tcp_mss(tp, -1); 868 } 869 if (flags & PRUS_EOF) { 870 /* 871 * Close the send side of the connection after 872 * the data is sent. 873 */ 874 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 875 socantsendmore(so); 876 tcp_usrclosed(tp); 877 } 878 if (!(inp->inp_flags & INP_DROPPED)) { 879 if (flags & PRUS_MORETOCOME) 880 tp->t_flags |= TF_MORETOCOME; 881 error = tcp_output(tp); 882 if (flags & PRUS_MORETOCOME) 883 tp->t_flags &= ~TF_MORETOCOME; 884 } 885 } else { 886 /* 887 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 888 */ 889 SOCKBUF_LOCK(&so->so_snd); 890 if (sbspace(&so->so_snd) < -512) { 891 SOCKBUF_UNLOCK(&so->so_snd); 892 m_freem(m); 893 error = ENOBUFS; 894 goto out; 895 } 896 /* 897 * According to RFC961 (Assigned Protocols), 898 * the urgent pointer points to the last octet 899 * of urgent data. We continue, however, 900 * to consider it to indicate the first octet 901 * of data past the urgent section. 902 * Otherwise, snd_up should be one lower. 903 */ 904 sbappendstream_locked(&so->so_snd, m); 905 SOCKBUF_UNLOCK(&so->so_snd); 906 if (nam && tp->t_state < TCPS_SYN_SENT) { 907 /* 908 * Do implied connect if not yet connected, 909 * initialize window to default value, and 910 * initialize maxseg/maxopd using peer's cached 911 * MSS. 912 */ 913 #ifdef INET6 914 if (isipv6) 915 error = tcp6_connect(tp, nam, td); 916 #endif /* INET6 */ 917 #if defined(INET6) && defined(INET) 918 else 919 #endif 920 #ifdef INET 921 error = tcp_connect(tp, nam, td); 922 #endif 923 if (error) 924 goto out; 925 tp->snd_wnd = TTCP_CLIENT_SND_WND; 926 tcp_mss(tp, -1); 927 } 928 tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 929 tp->t_flags |= TF_FORCEDATA; 930 error = tcp_output(tp); 931 tp->t_flags &= ~TF_FORCEDATA; 932 } 933 out: 934 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 935 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 936 INP_WUNLOCK(inp); 937 if (flags & PRUS_EOF) 938 INP_INFO_WUNLOCK(&V_tcbinfo); 939 return (error); 940 } 941 942 /* 943 * Abort the TCP. Drop the connection abruptly. 944 */ 945 static void 946 tcp_usr_abort(struct socket *so) 947 { 948 struct inpcb *inp; 949 struct tcpcb *tp = NULL; 950 TCPDEBUG0; 951 952 inp = sotoinpcb(so); 953 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 954 955 INP_INFO_WLOCK(&V_tcbinfo); 956 INP_WLOCK(inp); 957 KASSERT(inp->inp_socket != NULL, 958 ("tcp_usr_abort: inp_socket == NULL")); 959 960 /* 961 * If we still have full TCP state, and we're not dropped, drop. 962 */ 963 if (!(inp->inp_flags & INP_TIMEWAIT) && 964 !(inp->inp_flags & INP_DROPPED)) { 965 tp = intotcpcb(inp); 966 TCPDEBUG1(); 967 tcp_drop(tp, ECONNABORTED); 968 TCPDEBUG2(PRU_ABORT); 969 } 970 if (!(inp->inp_flags & INP_DROPPED)) { 971 SOCK_LOCK(so); 972 so->so_state |= SS_PROTOREF; 973 SOCK_UNLOCK(so); 974 inp->inp_flags |= INP_SOCKREF; 975 } 976 INP_WUNLOCK(inp); 977 INP_INFO_WUNLOCK(&V_tcbinfo); 978 } 979 980 /* 981 * TCP socket is closed. Start friendly disconnect. 982 */ 983 static void 984 tcp_usr_close(struct socket *so) 985 { 986 struct inpcb *inp; 987 struct tcpcb *tp = NULL; 988 TCPDEBUG0; 989 990 inp = sotoinpcb(so); 991 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 992 993 INP_INFO_WLOCK(&V_tcbinfo); 994 INP_WLOCK(inp); 995 KASSERT(inp->inp_socket != NULL, 996 ("tcp_usr_close: inp_socket == NULL")); 997 998 /* 999 * If we still have full TCP state, and we're not dropped, initiate 1000 * a disconnect. 1001 */ 1002 if (!(inp->inp_flags & INP_TIMEWAIT) && 1003 !(inp->inp_flags & INP_DROPPED)) { 1004 tp = intotcpcb(inp); 1005 TCPDEBUG1(); 1006 tcp_disconnect(tp); 1007 TCPDEBUG2(PRU_CLOSE); 1008 } 1009 if (!(inp->inp_flags & INP_DROPPED)) { 1010 SOCK_LOCK(so); 1011 so->so_state |= SS_PROTOREF; 1012 SOCK_UNLOCK(so); 1013 inp->inp_flags |= INP_SOCKREF; 1014 } 1015 INP_WUNLOCK(inp); 1016 INP_INFO_WUNLOCK(&V_tcbinfo); 1017 } 1018 1019 /* 1020 * Receive out-of-band data. 1021 */ 1022 static int 1023 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 1024 { 1025 int error = 0; 1026 struct inpcb *inp; 1027 struct tcpcb *tp = NULL; 1028 1029 TCPDEBUG0; 1030 inp = sotoinpcb(so); 1031 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 1032 INP_WLOCK(inp); 1033 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1034 error = ECONNRESET; 1035 goto out; 1036 } 1037 tp = intotcpcb(inp); 1038 TCPDEBUG1(); 1039 if ((so->so_oobmark == 0 && 1040 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 1041 so->so_options & SO_OOBINLINE || 1042 tp->t_oobflags & TCPOOB_HADDATA) { 1043 error = EINVAL; 1044 goto out; 1045 } 1046 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 1047 error = EWOULDBLOCK; 1048 goto out; 1049 } 1050 m->m_len = 1; 1051 *mtod(m, caddr_t) = tp->t_iobc; 1052 if ((flags & MSG_PEEK) == 0) 1053 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1054 1055 out: 1056 TCPDEBUG2(PRU_RCVOOB); 1057 INP_WUNLOCK(inp); 1058 return (error); 1059 } 1060 1061 #ifdef INET 1062 struct pr_usrreqs tcp_usrreqs = { 1063 .pru_abort = tcp_usr_abort, 1064 .pru_accept = tcp_usr_accept, 1065 .pru_attach = tcp_usr_attach, 1066 .pru_bind = tcp_usr_bind, 1067 .pru_connect = tcp_usr_connect, 1068 .pru_control = in_control, 1069 .pru_detach = tcp_usr_detach, 1070 .pru_disconnect = tcp_usr_disconnect, 1071 .pru_listen = tcp_usr_listen, 1072 .pru_peeraddr = in_getpeeraddr, 1073 .pru_rcvd = tcp_usr_rcvd, 1074 .pru_rcvoob = tcp_usr_rcvoob, 1075 .pru_send = tcp_usr_send, 1076 .pru_shutdown = tcp_usr_shutdown, 1077 .pru_sockaddr = in_getsockaddr, 1078 .pru_sosetlabel = in_pcbsosetlabel, 1079 .pru_close = tcp_usr_close, 1080 }; 1081 #endif /* INET */ 1082 1083 #ifdef INET6 1084 struct pr_usrreqs tcp6_usrreqs = { 1085 .pru_abort = tcp_usr_abort, 1086 .pru_accept = tcp6_usr_accept, 1087 .pru_attach = tcp_usr_attach, 1088 .pru_bind = tcp6_usr_bind, 1089 .pru_connect = tcp6_usr_connect, 1090 .pru_control = in6_control, 1091 .pru_detach = tcp_usr_detach, 1092 .pru_disconnect = tcp_usr_disconnect, 1093 .pru_listen = tcp6_usr_listen, 1094 .pru_peeraddr = in6_mapped_peeraddr, 1095 .pru_rcvd = tcp_usr_rcvd, 1096 .pru_rcvoob = tcp_usr_rcvoob, 1097 .pru_send = tcp_usr_send, 1098 .pru_shutdown = tcp_usr_shutdown, 1099 .pru_sockaddr = in6_mapped_sockaddr, 1100 .pru_sosetlabel = in_pcbsosetlabel, 1101 .pru_close = tcp_usr_close, 1102 }; 1103 #endif /* INET6 */ 1104 1105 #ifdef INET 1106 /* 1107 * Common subroutine to open a TCP connection to remote host specified 1108 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 1109 * port number if needed. Call in_pcbconnect_setup to do the routing and 1110 * to choose a local host address (interface). If there is an existing 1111 * incarnation of the same connection in TIME-WAIT state and if the remote 1112 * host was sending CC options and if the connection duration was < MSL, then 1113 * truncate the previous TIME-WAIT state and proceed. 1114 * Initialize connection parameters and enter SYN-SENT state. 1115 */ 1116 static int 1117 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1118 { 1119 struct inpcb *inp = tp->t_inpcb, *oinp; 1120 struct socket *so = inp->inp_socket; 1121 struct in_addr laddr; 1122 u_short lport; 1123 int error; 1124 1125 INP_WLOCK_ASSERT(inp); 1126 INP_HASH_WLOCK(&V_tcbinfo); 1127 1128 if (inp->inp_lport == 0) { 1129 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1130 if (error) 1131 goto out; 1132 } 1133 1134 /* 1135 * Cannot simply call in_pcbconnect, because there might be an 1136 * earlier incarnation of this same connection still in 1137 * TIME_WAIT state, creating an ADDRINUSE error. 1138 */ 1139 laddr = inp->inp_laddr; 1140 lport = inp->inp_lport; 1141 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1142 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 1143 if (error && oinp == NULL) 1144 goto out; 1145 if (oinp) { 1146 error = EADDRINUSE; 1147 goto out; 1148 } 1149 inp->inp_laddr = laddr; 1150 in_pcbrehash(inp); 1151 INP_HASH_WUNLOCK(&V_tcbinfo); 1152 1153 /* 1154 * Compute window scaling to request: 1155 * Scale to fit into sweet spot. See tcp_syncache.c. 1156 * XXX: This should move to tcp_output(). 1157 */ 1158 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1159 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1160 tp->request_r_scale++; 1161 1162 soisconnecting(so); 1163 TCPSTAT_INC(tcps_connattempt); 1164 tcp_state_change(tp, TCPS_SYN_SENT); 1165 tp->iss = tcp_new_isn(tp); 1166 tcp_sendseqinit(tp); 1167 1168 return 0; 1169 1170 out: 1171 INP_HASH_WUNLOCK(&V_tcbinfo); 1172 return (error); 1173 } 1174 #endif /* INET */ 1175 1176 #ifdef INET6 1177 static int 1178 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1179 { 1180 struct inpcb *inp = tp->t_inpcb; 1181 int error; 1182 1183 INP_WLOCK_ASSERT(inp); 1184 INP_HASH_WLOCK(&V_tcbinfo); 1185 1186 if (inp->inp_lport == 0) { 1187 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1188 if (error) 1189 goto out; 1190 } 1191 error = in6_pcbconnect(inp, nam, td->td_ucred); 1192 if (error != 0) 1193 goto out; 1194 INP_HASH_WUNLOCK(&V_tcbinfo); 1195 1196 /* Compute window scaling to request. */ 1197 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1198 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1199 tp->request_r_scale++; 1200 1201 soisconnecting(inp->inp_socket); 1202 TCPSTAT_INC(tcps_connattempt); 1203 tcp_state_change(tp, TCPS_SYN_SENT); 1204 tp->iss = tcp_new_isn(tp); 1205 tcp_sendseqinit(tp); 1206 1207 return 0; 1208 1209 out: 1210 INP_HASH_WUNLOCK(&V_tcbinfo); 1211 return error; 1212 } 1213 #endif /* INET6 */ 1214 1215 /* 1216 * Export TCP internal state information via a struct tcp_info, based on the 1217 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1218 * (TCP state machine, etc). We export all information using FreeBSD-native 1219 * constants -- for example, the numeric values for tcpi_state will differ 1220 * from Linux. 1221 */ 1222 static void 1223 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1224 { 1225 1226 INP_WLOCK_ASSERT(tp->t_inpcb); 1227 bzero(ti, sizeof(*ti)); 1228 1229 ti->tcpi_state = tp->t_state; 1230 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1231 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1232 if (tp->t_flags & TF_SACK_PERMIT) 1233 ti->tcpi_options |= TCPI_OPT_SACK; 1234 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1235 ti->tcpi_options |= TCPI_OPT_WSCALE; 1236 ti->tcpi_snd_wscale = tp->snd_scale; 1237 ti->tcpi_rcv_wscale = tp->rcv_scale; 1238 } 1239 1240 ti->tcpi_rto = tp->t_rxtcur * tick; 1241 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 1242 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 1243 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 1244 1245 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1246 ti->tcpi_snd_cwnd = tp->snd_cwnd; 1247 1248 /* 1249 * FreeBSD-specific extension fields for tcp_info. 1250 */ 1251 ti->tcpi_rcv_space = tp->rcv_wnd; 1252 ti->tcpi_rcv_nxt = tp->rcv_nxt; 1253 ti->tcpi_snd_wnd = tp->snd_wnd; 1254 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1255 ti->tcpi_snd_nxt = tp->snd_nxt; 1256 ti->tcpi_snd_mss = tp->t_maxseg; 1257 ti->tcpi_rcv_mss = tp->t_maxseg; 1258 if (tp->t_flags & TF_TOE) 1259 ti->tcpi_options |= TCPI_OPT_TOE; 1260 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1261 ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1262 ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1263 } 1264 1265 /* 1266 * tcp_ctloutput() must drop the inpcb lock before performing copyin on 1267 * socket option arguments. When it re-acquires the lock after the copy, it 1268 * has to revalidate that the connection is still valid for the socket 1269 * option. 1270 */ 1271 #define INP_WLOCK_RECHECK(inp) do { \ 1272 INP_WLOCK(inp); \ 1273 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 1274 INP_WUNLOCK(inp); \ 1275 return (ECONNRESET); \ 1276 } \ 1277 tp = intotcpcb(inp); \ 1278 } while(0) 1279 1280 int 1281 tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1282 { 1283 int error, opt, optval; 1284 u_int ui; 1285 struct inpcb *inp; 1286 struct tcpcb *tp; 1287 struct tcp_info ti; 1288 char buf[TCP_CA_NAME_MAX]; 1289 struct cc_algo *algo; 1290 1291 error = 0; 1292 inp = sotoinpcb(so); 1293 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1294 INP_WLOCK(inp); 1295 if (sopt->sopt_level != IPPROTO_TCP) { 1296 #ifdef INET6 1297 if (inp->inp_vflag & INP_IPV6PROTO) { 1298 INP_WUNLOCK(inp); 1299 error = ip6_ctloutput(so, sopt); 1300 } 1301 #endif /* INET6 */ 1302 #if defined(INET6) && defined(INET) 1303 else 1304 #endif 1305 #ifdef INET 1306 { 1307 INP_WUNLOCK(inp); 1308 error = ip_ctloutput(so, sopt); 1309 } 1310 #endif 1311 return (error); 1312 } 1313 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1314 INP_WUNLOCK(inp); 1315 return (ECONNRESET); 1316 } 1317 1318 switch (sopt->sopt_dir) { 1319 case SOPT_SET: 1320 switch (sopt->sopt_name) { 1321 #ifdef TCP_SIGNATURE 1322 case TCP_MD5SIG: 1323 INP_WUNLOCK(inp); 1324 error = sooptcopyin(sopt, &optval, sizeof optval, 1325 sizeof optval); 1326 if (error) 1327 return (error); 1328 1329 INP_WLOCK_RECHECK(inp); 1330 if (optval > 0) 1331 tp->t_flags |= TF_SIGNATURE; 1332 else 1333 tp->t_flags &= ~TF_SIGNATURE; 1334 goto unlock_and_done; 1335 #endif /* TCP_SIGNATURE */ 1336 1337 case TCP_NODELAY: 1338 case TCP_NOOPT: 1339 INP_WUNLOCK(inp); 1340 error = sooptcopyin(sopt, &optval, sizeof optval, 1341 sizeof optval); 1342 if (error) 1343 return (error); 1344 1345 INP_WLOCK_RECHECK(inp); 1346 switch (sopt->sopt_name) { 1347 case TCP_NODELAY: 1348 opt = TF_NODELAY; 1349 break; 1350 case TCP_NOOPT: 1351 opt = TF_NOOPT; 1352 break; 1353 default: 1354 opt = 0; /* dead code to fool gcc */ 1355 break; 1356 } 1357 1358 if (optval) 1359 tp->t_flags |= opt; 1360 else 1361 tp->t_flags &= ~opt; 1362 unlock_and_done: 1363 #ifdef TCP_OFFLOAD 1364 if (tp->t_flags & TF_TOE) { 1365 tcp_offload_ctloutput(tp, sopt->sopt_dir, 1366 sopt->sopt_name); 1367 } 1368 #endif 1369 INP_WUNLOCK(inp); 1370 break; 1371 1372 case TCP_NOPUSH: 1373 INP_WUNLOCK(inp); 1374 error = sooptcopyin(sopt, &optval, sizeof optval, 1375 sizeof optval); 1376 if (error) 1377 return (error); 1378 1379 INP_WLOCK_RECHECK(inp); 1380 if (optval) 1381 tp->t_flags |= TF_NOPUSH; 1382 else if (tp->t_flags & TF_NOPUSH) { 1383 tp->t_flags &= ~TF_NOPUSH; 1384 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1385 error = tcp_output(tp); 1386 } 1387 goto unlock_and_done; 1388 1389 case TCP_MAXSEG: 1390 INP_WUNLOCK(inp); 1391 error = sooptcopyin(sopt, &optval, sizeof optval, 1392 sizeof optval); 1393 if (error) 1394 return (error); 1395 1396 INP_WLOCK_RECHECK(inp); 1397 if (optval > 0 && optval <= tp->t_maxseg && 1398 optval + 40 >= V_tcp_minmss) 1399 tp->t_maxseg = optval; 1400 else 1401 error = EINVAL; 1402 goto unlock_and_done; 1403 1404 case TCP_INFO: 1405 INP_WUNLOCK(inp); 1406 error = EINVAL; 1407 break; 1408 1409 case TCP_CONGESTION: 1410 INP_WUNLOCK(inp); 1411 bzero(buf, sizeof(buf)); 1412 error = sooptcopyin(sopt, &buf, sizeof(buf), 1); 1413 if (error) 1414 break; 1415 INP_WLOCK_RECHECK(inp); 1416 /* 1417 * Return EINVAL if we can't find the requested cc algo. 1418 */ 1419 error = EINVAL; 1420 CC_LIST_RLOCK(); 1421 STAILQ_FOREACH(algo, &cc_list, entries) { 1422 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX) 1423 == 0) { 1424 /* We've found the requested algo. */ 1425 error = 0; 1426 /* 1427 * We hold a write lock over the tcb 1428 * so it's safe to do these things 1429 * without ordering concerns. 1430 */ 1431 if (CC_ALGO(tp)->cb_destroy != NULL) 1432 CC_ALGO(tp)->cb_destroy(tp->ccv); 1433 CC_ALGO(tp) = algo; 1434 /* 1435 * If something goes pear shaped 1436 * initialising the new algo, 1437 * fall back to newreno (which 1438 * does not require initialisation). 1439 */ 1440 if (algo->cb_init != NULL) 1441 if (algo->cb_init(tp->ccv) > 0) { 1442 CC_ALGO(tp) = &newreno_cc_algo; 1443 /* 1444 * The only reason init 1445 * should fail is 1446 * because of malloc. 1447 */ 1448 error = ENOMEM; 1449 } 1450 break; /* Break the STAILQ_FOREACH. */ 1451 } 1452 } 1453 CC_LIST_RUNLOCK(); 1454 goto unlock_and_done; 1455 1456 case TCP_KEEPIDLE: 1457 case TCP_KEEPINTVL: 1458 case TCP_KEEPINIT: 1459 INP_WUNLOCK(inp); 1460 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1461 if (error) 1462 return (error); 1463 1464 if (ui > (UINT_MAX / hz)) { 1465 error = EINVAL; 1466 break; 1467 } 1468 ui *= hz; 1469 1470 INP_WLOCK_RECHECK(inp); 1471 switch (sopt->sopt_name) { 1472 case TCP_KEEPIDLE: 1473 tp->t_keepidle = ui; 1474 /* 1475 * XXX: better check current remaining 1476 * timeout and "merge" it with new value. 1477 */ 1478 if ((tp->t_state > TCPS_LISTEN) && 1479 (tp->t_state <= TCPS_CLOSING)) 1480 tcp_timer_activate(tp, TT_KEEP, 1481 TP_KEEPIDLE(tp)); 1482 break; 1483 case TCP_KEEPINTVL: 1484 tp->t_keepintvl = ui; 1485 if ((tp->t_state == TCPS_FIN_WAIT_2) && 1486 (TP_MAXIDLE(tp) > 0)) 1487 tcp_timer_activate(tp, TT_2MSL, 1488 TP_MAXIDLE(tp)); 1489 break; 1490 case TCP_KEEPINIT: 1491 tp->t_keepinit = ui; 1492 if (tp->t_state == TCPS_SYN_RECEIVED || 1493 tp->t_state == TCPS_SYN_SENT) 1494 tcp_timer_activate(tp, TT_KEEP, 1495 TP_KEEPINIT(tp)); 1496 break; 1497 } 1498 goto unlock_and_done; 1499 1500 case TCP_KEEPCNT: 1501 INP_WUNLOCK(inp); 1502 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1503 if (error) 1504 return (error); 1505 1506 INP_WLOCK_RECHECK(inp); 1507 tp->t_keepcnt = ui; 1508 if ((tp->t_state == TCPS_FIN_WAIT_2) && 1509 (TP_MAXIDLE(tp) > 0)) 1510 tcp_timer_activate(tp, TT_2MSL, 1511 TP_MAXIDLE(tp)); 1512 goto unlock_and_done; 1513 1514 default: 1515 INP_WUNLOCK(inp); 1516 error = ENOPROTOOPT; 1517 break; 1518 } 1519 break; 1520 1521 case SOPT_GET: 1522 tp = intotcpcb(inp); 1523 switch (sopt->sopt_name) { 1524 #ifdef TCP_SIGNATURE 1525 case TCP_MD5SIG: 1526 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1527 INP_WUNLOCK(inp); 1528 error = sooptcopyout(sopt, &optval, sizeof optval); 1529 break; 1530 #endif 1531 1532 case TCP_NODELAY: 1533 optval = tp->t_flags & TF_NODELAY; 1534 INP_WUNLOCK(inp); 1535 error = sooptcopyout(sopt, &optval, sizeof optval); 1536 break; 1537 case TCP_MAXSEG: 1538 optval = tp->t_maxseg; 1539 INP_WUNLOCK(inp); 1540 error = sooptcopyout(sopt, &optval, sizeof optval); 1541 break; 1542 case TCP_NOOPT: 1543 optval = tp->t_flags & TF_NOOPT; 1544 INP_WUNLOCK(inp); 1545 error = sooptcopyout(sopt, &optval, sizeof optval); 1546 break; 1547 case TCP_NOPUSH: 1548 optval = tp->t_flags & TF_NOPUSH; 1549 INP_WUNLOCK(inp); 1550 error = sooptcopyout(sopt, &optval, sizeof optval); 1551 break; 1552 case TCP_INFO: 1553 tcp_fill_info(tp, &ti); 1554 INP_WUNLOCK(inp); 1555 error = sooptcopyout(sopt, &ti, sizeof ti); 1556 break; 1557 case TCP_CONGESTION: 1558 bzero(buf, sizeof(buf)); 1559 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 1560 INP_WUNLOCK(inp); 1561 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX); 1562 break; 1563 case TCP_KEEPIDLE: 1564 case TCP_KEEPINTVL: 1565 case TCP_KEEPINIT: 1566 case TCP_KEEPCNT: 1567 switch (sopt->sopt_name) { 1568 case TCP_KEEPIDLE: 1569 ui = tp->t_keepidle / hz; 1570 break; 1571 case TCP_KEEPINTVL: 1572 ui = tp->t_keepintvl / hz; 1573 break; 1574 case TCP_KEEPINIT: 1575 ui = tp->t_keepinit / hz; 1576 break; 1577 case TCP_KEEPCNT: 1578 ui = tp->t_keepcnt; 1579 break; 1580 } 1581 INP_WUNLOCK(inp); 1582 error = sooptcopyout(sopt, &ui, sizeof(ui)); 1583 break; 1584 default: 1585 INP_WUNLOCK(inp); 1586 error = ENOPROTOOPT; 1587 break; 1588 } 1589 break; 1590 } 1591 return (error); 1592 } 1593 #undef INP_WLOCK_RECHECK 1594 1595 /* 1596 * Attach TCP protocol to socket, allocating 1597 * internet protocol control block, tcp control block, 1598 * bufer space, and entering LISTEN state if to accept connections. 1599 */ 1600 static int 1601 tcp_attach(struct socket *so) 1602 { 1603 struct tcpcb *tp; 1604 struct inpcb *inp; 1605 int error; 1606 1607 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1608 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1609 if (error) 1610 return (error); 1611 } 1612 so->so_rcv.sb_flags |= SB_AUTOSIZE; 1613 so->so_snd.sb_flags |= SB_AUTOSIZE; 1614 INP_INFO_WLOCK(&V_tcbinfo); 1615 error = in_pcballoc(so, &V_tcbinfo); 1616 if (error) { 1617 INP_INFO_WUNLOCK(&V_tcbinfo); 1618 return (error); 1619 } 1620 inp = sotoinpcb(so); 1621 #ifdef INET6 1622 if (inp->inp_vflag & INP_IPV6PROTO) { 1623 inp->inp_vflag |= INP_IPV6; 1624 inp->in6p_hops = -1; /* use kernel default */ 1625 } 1626 else 1627 #endif 1628 inp->inp_vflag |= INP_IPV4; 1629 tp = tcp_newtcpcb(inp); 1630 if (tp == NULL) { 1631 in_pcbdetach(inp); 1632 in_pcbfree(inp); 1633 INP_INFO_WUNLOCK(&V_tcbinfo); 1634 return (ENOBUFS); 1635 } 1636 tp->t_state = TCPS_CLOSED; 1637 INP_WUNLOCK(inp); 1638 INP_INFO_WUNLOCK(&V_tcbinfo); 1639 return (0); 1640 } 1641 1642 /* 1643 * Initiate (or continue) disconnect. 1644 * If embryonic state, just send reset (once). 1645 * If in ``let data drain'' option and linger null, just drop. 1646 * Otherwise (hard), mark socket disconnecting and drop 1647 * current input data; switch states based on user close, and 1648 * send segment to peer (with FIN). 1649 */ 1650 static void 1651 tcp_disconnect(struct tcpcb *tp) 1652 { 1653 struct inpcb *inp = tp->t_inpcb; 1654 struct socket *so = inp->inp_socket; 1655 1656 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1657 INP_WLOCK_ASSERT(inp); 1658 1659 /* 1660 * Neither tcp_close() nor tcp_drop() should return NULL, as the 1661 * socket is still open. 1662 */ 1663 if (tp->t_state < TCPS_ESTABLISHED) { 1664 tp = tcp_close(tp); 1665 KASSERT(tp != NULL, 1666 ("tcp_disconnect: tcp_close() returned NULL")); 1667 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1668 tp = tcp_drop(tp, 0); 1669 KASSERT(tp != NULL, 1670 ("tcp_disconnect: tcp_drop() returned NULL")); 1671 } else { 1672 soisdisconnecting(so); 1673 sbflush(&so->so_rcv); 1674 tcp_usrclosed(tp); 1675 if (!(inp->inp_flags & INP_DROPPED)) 1676 tcp_output(tp); 1677 } 1678 } 1679 1680 /* 1681 * User issued close, and wish to trail through shutdown states: 1682 * if never received SYN, just forget it. If got a SYN from peer, 1683 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1684 * If already got a FIN from peer, then almost done; go to LAST_ACK 1685 * state. In all other cases, have already sent FIN to peer (e.g. 1686 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1687 * for peer to send FIN or not respond to keep-alives, etc. 1688 * We can let the user exit from the close as soon as the FIN is acked. 1689 */ 1690 static void 1691 tcp_usrclosed(struct tcpcb *tp) 1692 { 1693 1694 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1695 INP_WLOCK_ASSERT(tp->t_inpcb); 1696 1697 switch (tp->t_state) { 1698 case TCPS_LISTEN: 1699 #ifdef TCP_OFFLOAD 1700 tcp_offload_listen_stop(tp); 1701 #endif 1702 /* FALLTHROUGH */ 1703 case TCPS_CLOSED: 1704 tcp_state_change(tp, TCPS_CLOSED); 1705 tp = tcp_close(tp); 1706 /* 1707 * tcp_close() should never return NULL here as the socket is 1708 * still open. 1709 */ 1710 KASSERT(tp != NULL, 1711 ("tcp_usrclosed: tcp_close() returned NULL")); 1712 break; 1713 1714 case TCPS_SYN_SENT: 1715 case TCPS_SYN_RECEIVED: 1716 tp->t_flags |= TF_NEEDFIN; 1717 break; 1718 1719 case TCPS_ESTABLISHED: 1720 tcp_state_change(tp, TCPS_FIN_WAIT_1); 1721 break; 1722 1723 case TCPS_CLOSE_WAIT: 1724 tcp_state_change(tp, TCPS_LAST_ACK); 1725 break; 1726 } 1727 if (tp->t_state >= TCPS_FIN_WAIT_2) { 1728 soisdisconnected(tp->t_inpcb->inp_socket); 1729 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 1730 if (tp->t_state == TCPS_FIN_WAIT_2) { 1731 int timeout; 1732 1733 timeout = (tcp_fast_finwait2_recycle) ? 1734 tcp_finwait2_timeout : TP_MAXIDLE(tp); 1735 tcp_timer_activate(tp, TT_2MSL, timeout); 1736 } 1737 } 1738 } 1739 1740 #ifdef DDB 1741 static void 1742 db_print_indent(int indent) 1743 { 1744 int i; 1745 1746 for (i = 0; i < indent; i++) 1747 db_printf(" "); 1748 } 1749 1750 static void 1751 db_print_tstate(int t_state) 1752 { 1753 1754 switch (t_state) { 1755 case TCPS_CLOSED: 1756 db_printf("TCPS_CLOSED"); 1757 return; 1758 1759 case TCPS_LISTEN: 1760 db_printf("TCPS_LISTEN"); 1761 return; 1762 1763 case TCPS_SYN_SENT: 1764 db_printf("TCPS_SYN_SENT"); 1765 return; 1766 1767 case TCPS_SYN_RECEIVED: 1768 db_printf("TCPS_SYN_RECEIVED"); 1769 return; 1770 1771 case TCPS_ESTABLISHED: 1772 db_printf("TCPS_ESTABLISHED"); 1773 return; 1774 1775 case TCPS_CLOSE_WAIT: 1776 db_printf("TCPS_CLOSE_WAIT"); 1777 return; 1778 1779 case TCPS_FIN_WAIT_1: 1780 db_printf("TCPS_FIN_WAIT_1"); 1781 return; 1782 1783 case TCPS_CLOSING: 1784 db_printf("TCPS_CLOSING"); 1785 return; 1786 1787 case TCPS_LAST_ACK: 1788 db_printf("TCPS_LAST_ACK"); 1789 return; 1790 1791 case TCPS_FIN_WAIT_2: 1792 db_printf("TCPS_FIN_WAIT_2"); 1793 return; 1794 1795 case TCPS_TIME_WAIT: 1796 db_printf("TCPS_TIME_WAIT"); 1797 return; 1798 1799 default: 1800 db_printf("unknown"); 1801 return; 1802 } 1803 } 1804 1805 static void 1806 db_print_tflags(u_int t_flags) 1807 { 1808 int comma; 1809 1810 comma = 0; 1811 if (t_flags & TF_ACKNOW) { 1812 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1813 comma = 1; 1814 } 1815 if (t_flags & TF_DELACK) { 1816 db_printf("%sTF_DELACK", comma ? ", " : ""); 1817 comma = 1; 1818 } 1819 if (t_flags & TF_NODELAY) { 1820 db_printf("%sTF_NODELAY", comma ? ", " : ""); 1821 comma = 1; 1822 } 1823 if (t_flags & TF_NOOPT) { 1824 db_printf("%sTF_NOOPT", comma ? ", " : ""); 1825 comma = 1; 1826 } 1827 if (t_flags & TF_SENTFIN) { 1828 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1829 comma = 1; 1830 } 1831 if (t_flags & TF_REQ_SCALE) { 1832 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1833 comma = 1; 1834 } 1835 if (t_flags & TF_RCVD_SCALE) { 1836 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1837 comma = 1; 1838 } 1839 if (t_flags & TF_REQ_TSTMP) { 1840 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1841 comma = 1; 1842 } 1843 if (t_flags & TF_RCVD_TSTMP) { 1844 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1845 comma = 1; 1846 } 1847 if (t_flags & TF_SACK_PERMIT) { 1848 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1849 comma = 1; 1850 } 1851 if (t_flags & TF_NEEDSYN) { 1852 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1853 comma = 1; 1854 } 1855 if (t_flags & TF_NEEDFIN) { 1856 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1857 comma = 1; 1858 } 1859 if (t_flags & TF_NOPUSH) { 1860 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1861 comma = 1; 1862 } 1863 if (t_flags & TF_MORETOCOME) { 1864 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1865 comma = 1; 1866 } 1867 if (t_flags & TF_LQ_OVERFLOW) { 1868 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1869 comma = 1; 1870 } 1871 if (t_flags & TF_LASTIDLE) { 1872 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1873 comma = 1; 1874 } 1875 if (t_flags & TF_RXWIN0SENT) { 1876 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1877 comma = 1; 1878 } 1879 if (t_flags & TF_FASTRECOVERY) { 1880 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1881 comma = 1; 1882 } 1883 if (t_flags & TF_CONGRECOVERY) { 1884 db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 1885 comma = 1; 1886 } 1887 if (t_flags & TF_WASFRECOVERY) { 1888 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1889 comma = 1; 1890 } 1891 if (t_flags & TF_SIGNATURE) { 1892 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1893 comma = 1; 1894 } 1895 if (t_flags & TF_FORCEDATA) { 1896 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1897 comma = 1; 1898 } 1899 if (t_flags & TF_TSO) { 1900 db_printf("%sTF_TSO", comma ? ", " : ""); 1901 comma = 1; 1902 } 1903 if (t_flags & TF_ECN_PERMIT) { 1904 db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1905 comma = 1; 1906 } 1907 } 1908 1909 static void 1910 db_print_toobflags(char t_oobflags) 1911 { 1912 int comma; 1913 1914 comma = 0; 1915 if (t_oobflags & TCPOOB_HAVEDATA) { 1916 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1917 comma = 1; 1918 } 1919 if (t_oobflags & TCPOOB_HADDATA) { 1920 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1921 comma = 1; 1922 } 1923 } 1924 1925 static void 1926 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1927 { 1928 1929 db_print_indent(indent); 1930 db_printf("%s at %p\n", name, tp); 1931 1932 indent += 2; 1933 1934 db_print_indent(indent); 1935 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 1936 tp->t_segq, tp->t_segqlen, tp->t_dupacks); 1937 1938 db_print_indent(indent); 1939 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 1940 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 1941 1942 db_print_indent(indent); 1943 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 1944 &tp->t_timers->tt_delack, tp->t_inpcb); 1945 1946 db_print_indent(indent); 1947 db_printf("t_state: %d (", tp->t_state); 1948 db_print_tstate(tp->t_state); 1949 db_printf(")\n"); 1950 1951 db_print_indent(indent); 1952 db_printf("t_flags: 0x%x (", tp->t_flags); 1953 db_print_tflags(tp->t_flags); 1954 db_printf(")\n"); 1955 1956 db_print_indent(indent); 1957 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 1958 tp->snd_una, tp->snd_max, tp->snd_nxt); 1959 1960 db_print_indent(indent); 1961 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 1962 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 1963 1964 db_print_indent(indent); 1965 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 1966 tp->iss, tp->irs, tp->rcv_nxt); 1967 1968 db_print_indent(indent); 1969 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 1970 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 1971 1972 db_print_indent(indent); 1973 db_printf("snd_wnd: %lu snd_cwnd: %lu\n", 1974 tp->snd_wnd, tp->snd_cwnd); 1975 1976 db_print_indent(indent); 1977 db_printf("snd_ssthresh: %lu snd_recover: " 1978 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 1979 1980 db_print_indent(indent); 1981 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 1982 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 1983 1984 db_print_indent(indent); 1985 db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 1986 tp->t_rtttime, tp->t_rtseq); 1987 1988 db_print_indent(indent); 1989 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 1990 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 1991 1992 db_print_indent(indent); 1993 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 1994 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 1995 tp->t_rttbest); 1996 1997 db_print_indent(indent); 1998 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 1999 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2000 2001 db_print_indent(indent); 2002 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2003 db_print_toobflags(tp->t_oobflags); 2004 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2005 2006 db_print_indent(indent); 2007 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2008 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2009 2010 db_print_indent(indent); 2011 db_printf("ts_recent: %u ts_recent_age: %u\n", 2012 tp->ts_recent, tp->ts_recent_age); 2013 2014 db_print_indent(indent); 2015 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2016 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2017 2018 db_print_indent(indent); 2019 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 2020 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2021 tp->snd_recover_prev, tp->t_badrxtwin); 2022 2023 db_print_indent(indent); 2024 db_printf("snd_numholes: %d snd_holes first: %p\n", 2025 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2026 2027 db_print_indent(indent); 2028 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2029 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2030 2031 /* Skip sackblks, sackhint. */ 2032 2033 db_print_indent(indent); 2034 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2035 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2036 } 2037 2038 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2039 { 2040 struct tcpcb *tp; 2041 2042 if (!have_addr) { 2043 db_printf("usage: show tcpcb <addr>\n"); 2044 return; 2045 } 2046 tp = (struct tcpcb *)addr; 2047 2048 db_print_tcpcb(tp, "tcpcb", 0); 2049 } 2050 #endif 2051