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) { 480 error = EADDRINUSE; 481 goto out; 482 } 483 if (inp->inp_flags & INP_DROPPED) { 484 error = ECONNREFUSED; 485 goto out; 486 } 487 tp = intotcpcb(inp); 488 TCPDEBUG1(); 489 if ((error = tcp_connect(tp, nam, td)) != 0) 490 goto out; 491 #ifdef TCP_OFFLOAD 492 if (registered_toedevs > 0 && 493 (so->so_options & SO_NO_OFFLOAD) == 0 && 494 (error = tcp_offload_connect(so, nam)) == 0) 495 goto out; 496 #endif 497 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 498 error = tcp_output(tp); 499 out: 500 TCPDEBUG2(PRU_CONNECT); 501 INP_WUNLOCK(inp); 502 return (error); 503 } 504 #endif /* INET */ 505 506 #ifdef INET6 507 static int 508 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 509 { 510 int error = 0; 511 struct inpcb *inp; 512 struct tcpcb *tp = NULL; 513 struct sockaddr_in6 *sin6p; 514 515 TCPDEBUG0; 516 517 sin6p = (struct sockaddr_in6 *)nam; 518 if (nam->sa_len != sizeof (*sin6p)) 519 return (EINVAL); 520 /* 521 * Must disallow TCP ``connections'' to multicast addresses. 522 */ 523 if (sin6p->sin6_family == AF_INET6 524 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 525 return (EAFNOSUPPORT); 526 527 inp = sotoinpcb(so); 528 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 529 INP_WLOCK(inp); 530 if (inp->inp_flags & INP_TIMEWAIT) { 531 error = EADDRINUSE; 532 goto out; 533 } 534 if (inp->inp_flags & INP_DROPPED) { 535 error = ECONNREFUSED; 536 goto out; 537 } 538 tp = intotcpcb(inp); 539 TCPDEBUG1(); 540 #ifdef INET 541 /* 542 * XXXRW: Some confusion: V4/V6 flags relate to binding, and 543 * therefore probably require the hash lock, which isn't held here. 544 * Is this a significant problem? 545 */ 546 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 547 struct sockaddr_in sin; 548 549 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 550 error = EINVAL; 551 goto out; 552 } 553 554 in6_sin6_2_sin(&sin, sin6p); 555 inp->inp_vflag |= INP_IPV4; 556 inp->inp_vflag &= ~INP_IPV6; 557 if ((error = prison_remote_ip4(td->td_ucred, 558 &sin.sin_addr)) != 0) 559 goto out; 560 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 561 goto out; 562 #ifdef TCP_OFFLOAD 563 if (registered_toedevs > 0 && 564 (so->so_options & SO_NO_OFFLOAD) == 0 && 565 (error = tcp_offload_connect(so, nam)) == 0) 566 goto out; 567 #endif 568 error = tcp_output(tp); 569 goto out; 570 } 571 #endif 572 inp->inp_vflag &= ~INP_IPV4; 573 inp->inp_vflag |= INP_IPV6; 574 inp->inp_inc.inc_flags |= INC_ISIPV6; 575 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 576 goto out; 577 if ((error = tcp6_connect(tp, nam, td)) != 0) 578 goto out; 579 #ifdef TCP_OFFLOAD 580 if (registered_toedevs > 0 && 581 (so->so_options & SO_NO_OFFLOAD) == 0 && 582 (error = tcp_offload_connect(so, nam)) == 0) 583 goto out; 584 #endif 585 tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp)); 586 error = tcp_output(tp); 587 588 out: 589 TCPDEBUG2(PRU_CONNECT); 590 INP_WUNLOCK(inp); 591 return (error); 592 } 593 #endif /* INET6 */ 594 595 /* 596 * Initiate disconnect from peer. 597 * If connection never passed embryonic stage, just drop; 598 * else if don't need to let data drain, then can just drop anyways, 599 * else have to begin TCP shutdown process: mark socket disconnecting, 600 * drain unread data, state switch to reflect user close, and 601 * send segment (e.g. FIN) to peer. Socket will be really disconnected 602 * when peer sends FIN and acks ours. 603 * 604 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 605 */ 606 static int 607 tcp_usr_disconnect(struct socket *so) 608 { 609 struct inpcb *inp; 610 struct tcpcb *tp = NULL; 611 int error = 0; 612 613 TCPDEBUG0; 614 INP_INFO_WLOCK(&V_tcbinfo); 615 inp = sotoinpcb(so); 616 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 617 INP_WLOCK(inp); 618 if (inp->inp_flags & INP_TIMEWAIT) 619 goto out; 620 if (inp->inp_flags & INP_DROPPED) { 621 error = ECONNRESET; 622 goto out; 623 } 624 tp = intotcpcb(inp); 625 TCPDEBUG1(); 626 tcp_disconnect(tp); 627 out: 628 TCPDEBUG2(PRU_DISCONNECT); 629 INP_WUNLOCK(inp); 630 INP_INFO_WUNLOCK(&V_tcbinfo); 631 return (error); 632 } 633 634 #ifdef INET 635 /* 636 * Accept a connection. Essentially all the work is done at higher levels; 637 * just return the address of the peer, storing through addr. 638 */ 639 static int 640 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 641 { 642 int error = 0; 643 struct inpcb *inp = NULL; 644 struct tcpcb *tp = NULL; 645 struct in_addr addr; 646 in_port_t port = 0; 647 TCPDEBUG0; 648 649 if (so->so_state & SS_ISDISCONNECTED) 650 return (ECONNABORTED); 651 652 inp = sotoinpcb(so); 653 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 654 INP_WLOCK(inp); 655 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 656 error = ECONNABORTED; 657 goto out; 658 } 659 tp = intotcpcb(inp); 660 TCPDEBUG1(); 661 662 /* 663 * We inline in_getpeeraddr and COMMON_END here, so that we can 664 * copy the data of interest and defer the malloc until after we 665 * release the lock. 666 */ 667 port = inp->inp_fport; 668 addr = inp->inp_faddr; 669 670 out: 671 TCPDEBUG2(PRU_ACCEPT); 672 INP_WUNLOCK(inp); 673 if (error == 0) 674 *nam = in_sockaddr(port, &addr); 675 return error; 676 } 677 #endif /* INET */ 678 679 #ifdef INET6 680 static int 681 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 682 { 683 struct inpcb *inp = NULL; 684 int error = 0; 685 struct tcpcb *tp = NULL; 686 struct in_addr addr; 687 struct in6_addr addr6; 688 in_port_t port = 0; 689 int v4 = 0; 690 TCPDEBUG0; 691 692 if (so->so_state & SS_ISDISCONNECTED) 693 return (ECONNABORTED); 694 695 inp = sotoinpcb(so); 696 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 697 INP_INFO_RLOCK(&V_tcbinfo); 698 INP_WLOCK(inp); 699 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 700 error = ECONNABORTED; 701 goto out; 702 } 703 tp = intotcpcb(inp); 704 TCPDEBUG1(); 705 706 /* 707 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 708 * copy the data of interest and defer the malloc until after we 709 * release the lock. 710 */ 711 if (inp->inp_vflag & INP_IPV4) { 712 v4 = 1; 713 port = inp->inp_fport; 714 addr = inp->inp_faddr; 715 } else { 716 port = inp->inp_fport; 717 addr6 = inp->in6p_faddr; 718 } 719 720 out: 721 TCPDEBUG2(PRU_ACCEPT); 722 INP_WUNLOCK(inp); 723 INP_INFO_RUNLOCK(&V_tcbinfo); 724 if (error == 0) { 725 if (v4) 726 *nam = in6_v4mapsin6_sockaddr(port, &addr); 727 else 728 *nam = in6_sockaddr(port, &addr6); 729 } 730 return error; 731 } 732 #endif /* INET6 */ 733 734 /* 735 * Mark the connection as being incapable of further output. 736 */ 737 static int 738 tcp_usr_shutdown(struct socket *so) 739 { 740 int error = 0; 741 struct inpcb *inp; 742 struct tcpcb *tp = NULL; 743 744 TCPDEBUG0; 745 INP_INFO_WLOCK(&V_tcbinfo); 746 inp = sotoinpcb(so); 747 KASSERT(inp != NULL, ("inp == NULL")); 748 INP_WLOCK(inp); 749 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 750 error = ECONNRESET; 751 goto out; 752 } 753 tp = intotcpcb(inp); 754 TCPDEBUG1(); 755 socantsendmore(so); 756 tcp_usrclosed(tp); 757 if (!(inp->inp_flags & INP_DROPPED)) 758 error = tcp_output(tp); 759 760 out: 761 TCPDEBUG2(PRU_SHUTDOWN); 762 INP_WUNLOCK(inp); 763 INP_INFO_WUNLOCK(&V_tcbinfo); 764 765 return (error); 766 } 767 768 /* 769 * After a receive, possibly send window update to peer. 770 */ 771 static int 772 tcp_usr_rcvd(struct socket *so, int flags) 773 { 774 struct inpcb *inp; 775 struct tcpcb *tp = NULL; 776 int error = 0; 777 778 TCPDEBUG0; 779 inp = sotoinpcb(so); 780 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 781 INP_WLOCK(inp); 782 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 783 error = ECONNRESET; 784 goto out; 785 } 786 tp = intotcpcb(inp); 787 TCPDEBUG1(); 788 #ifdef TCP_OFFLOAD 789 if (tp->t_flags & TF_TOE) 790 tcp_offload_rcvd(tp); 791 else 792 #endif 793 tcp_output(tp); 794 795 out: 796 TCPDEBUG2(PRU_RCVD); 797 INP_WUNLOCK(inp); 798 return (error); 799 } 800 801 /* 802 * Do a send by putting data in output queue and updating urgent 803 * marker if URG set. Possibly send more data. Unlike the other 804 * pru_*() routines, the mbuf chains are our responsibility. We 805 * must either enqueue them or free them. The other pru_* routines 806 * generally are caller-frees. 807 */ 808 static int 809 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 810 struct sockaddr *nam, struct mbuf *control, struct thread *td) 811 { 812 int error = 0; 813 struct inpcb *inp; 814 struct tcpcb *tp = NULL; 815 #ifdef INET6 816 int isipv6; 817 #endif 818 TCPDEBUG0; 819 820 /* 821 * We require the pcbinfo lock if we will close the socket as part of 822 * this call. 823 */ 824 if (flags & PRUS_EOF) 825 INP_INFO_WLOCK(&V_tcbinfo); 826 inp = sotoinpcb(so); 827 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 828 INP_WLOCK(inp); 829 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 830 if (control) 831 m_freem(control); 832 /* 833 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible 834 * for freeing memory. 835 */ 836 if (m && (flags & PRUS_NOTREADY) == 0) 837 m_freem(m); 838 error = ECONNRESET; 839 goto out; 840 } 841 #ifdef INET6 842 isipv6 = nam && nam->sa_family == AF_INET6; 843 #endif /* INET6 */ 844 tp = intotcpcb(inp); 845 TCPDEBUG1(); 846 if (control) { 847 /* TCP doesn't do control messages (rights, creds, etc) */ 848 if (control->m_len) { 849 m_freem(control); 850 if (m) 851 m_freem(m); 852 error = EINVAL; 853 goto out; 854 } 855 m_freem(control); /* empty control, just free it */ 856 } 857 if (!(flags & PRUS_OOB)) { 858 sbappendstream(&so->so_snd, m, flags); 859 if (nam && tp->t_state < TCPS_SYN_SENT) { 860 /* 861 * Do implied connect if not yet connected, 862 * initialize window to default value, and 863 * initialize maxseg/maxopd using peer's cached 864 * MSS. 865 */ 866 #ifdef INET6 867 if (isipv6) 868 error = tcp6_connect(tp, nam, td); 869 #endif /* INET6 */ 870 #if defined(INET6) && defined(INET) 871 else 872 #endif 873 #ifdef INET 874 error = tcp_connect(tp, nam, td); 875 #endif 876 if (error) 877 goto out; 878 tp->snd_wnd = TTCP_CLIENT_SND_WND; 879 tcp_mss(tp, -1); 880 } 881 if (flags & PRUS_EOF) { 882 /* 883 * Close the send side of the connection after 884 * the data is sent. 885 */ 886 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 887 socantsendmore(so); 888 tcp_usrclosed(tp); 889 } 890 if (!(inp->inp_flags & INP_DROPPED) && 891 !(flags & PRUS_NOTREADY)) { 892 if (flags & PRUS_MORETOCOME) 893 tp->t_flags |= TF_MORETOCOME; 894 error = tcp_output(tp); 895 if (flags & PRUS_MORETOCOME) 896 tp->t_flags &= ~TF_MORETOCOME; 897 } 898 } else { 899 /* 900 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 901 */ 902 SOCKBUF_LOCK(&so->so_snd); 903 if (sbspace(&so->so_snd) < -512) { 904 SOCKBUF_UNLOCK(&so->so_snd); 905 m_freem(m); 906 error = ENOBUFS; 907 goto out; 908 } 909 /* 910 * According to RFC961 (Assigned Protocols), 911 * the urgent pointer points to the last octet 912 * of urgent data. We continue, however, 913 * to consider it to indicate the first octet 914 * of data past the urgent section. 915 * Otherwise, snd_up should be one lower. 916 */ 917 sbappendstream_locked(&so->so_snd, m, flags); 918 SOCKBUF_UNLOCK(&so->so_snd); 919 if (nam && tp->t_state < TCPS_SYN_SENT) { 920 /* 921 * Do implied connect if not yet connected, 922 * initialize window to default value, and 923 * initialize maxseg/maxopd using peer's cached 924 * MSS. 925 */ 926 #ifdef INET6 927 if (isipv6) 928 error = tcp6_connect(tp, nam, td); 929 #endif /* INET6 */ 930 #if defined(INET6) && defined(INET) 931 else 932 #endif 933 #ifdef INET 934 error = tcp_connect(tp, nam, td); 935 #endif 936 if (error) 937 goto out; 938 tp->snd_wnd = TTCP_CLIENT_SND_WND; 939 tcp_mss(tp, -1); 940 } 941 tp->snd_up = tp->snd_una + sbavail(&so->so_snd); 942 if (!(flags & PRUS_NOTREADY)) { 943 tp->t_flags |= TF_FORCEDATA; 944 error = tcp_output(tp); 945 tp->t_flags &= ~TF_FORCEDATA; 946 } 947 } 948 out: 949 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 950 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 951 INP_WUNLOCK(inp); 952 if (flags & PRUS_EOF) 953 INP_INFO_WUNLOCK(&V_tcbinfo); 954 return (error); 955 } 956 957 static int 958 tcp_usr_ready(struct socket *so, struct mbuf *m, int count) 959 { 960 struct inpcb *inp; 961 struct tcpcb *tp; 962 int error; 963 964 inp = sotoinpcb(so); 965 INP_WLOCK(inp); 966 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 967 INP_WUNLOCK(inp); 968 for (int i = 0; i < count; i++) 969 m = m_free(m); 970 return (ECONNRESET); 971 } 972 tp = intotcpcb(inp); 973 974 SOCKBUF_LOCK(&so->so_snd); 975 error = sbready(&so->so_snd, m, count); 976 SOCKBUF_UNLOCK(&so->so_snd); 977 if (error == 0) 978 error = tcp_output(tp); 979 INP_WUNLOCK(inp); 980 981 return (error); 982 } 983 984 /* 985 * Abort the TCP. Drop the connection abruptly. 986 */ 987 static void 988 tcp_usr_abort(struct socket *so) 989 { 990 struct inpcb *inp; 991 struct tcpcb *tp = NULL; 992 TCPDEBUG0; 993 994 inp = sotoinpcb(so); 995 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 996 997 INP_INFO_WLOCK(&V_tcbinfo); 998 INP_WLOCK(inp); 999 KASSERT(inp->inp_socket != NULL, 1000 ("tcp_usr_abort: inp_socket == NULL")); 1001 1002 /* 1003 * If we still have full TCP state, and we're not dropped, drop. 1004 */ 1005 if (!(inp->inp_flags & INP_TIMEWAIT) && 1006 !(inp->inp_flags & INP_DROPPED)) { 1007 tp = intotcpcb(inp); 1008 TCPDEBUG1(); 1009 tcp_drop(tp, ECONNABORTED); 1010 TCPDEBUG2(PRU_ABORT); 1011 } 1012 if (!(inp->inp_flags & INP_DROPPED)) { 1013 SOCK_LOCK(so); 1014 so->so_state |= SS_PROTOREF; 1015 SOCK_UNLOCK(so); 1016 inp->inp_flags |= INP_SOCKREF; 1017 } 1018 INP_WUNLOCK(inp); 1019 INP_INFO_WUNLOCK(&V_tcbinfo); 1020 } 1021 1022 /* 1023 * TCP socket is closed. Start friendly disconnect. 1024 */ 1025 static void 1026 tcp_usr_close(struct socket *so) 1027 { 1028 struct inpcb *inp; 1029 struct tcpcb *tp = NULL; 1030 TCPDEBUG0; 1031 1032 inp = sotoinpcb(so); 1033 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 1034 1035 INP_INFO_WLOCK(&V_tcbinfo); 1036 INP_WLOCK(inp); 1037 KASSERT(inp->inp_socket != NULL, 1038 ("tcp_usr_close: inp_socket == NULL")); 1039 1040 /* 1041 * If we still have full TCP state, and we're not dropped, initiate 1042 * a disconnect. 1043 */ 1044 if (!(inp->inp_flags & INP_TIMEWAIT) && 1045 !(inp->inp_flags & INP_DROPPED)) { 1046 tp = intotcpcb(inp); 1047 TCPDEBUG1(); 1048 tcp_disconnect(tp); 1049 TCPDEBUG2(PRU_CLOSE); 1050 } 1051 if (!(inp->inp_flags & INP_DROPPED)) { 1052 SOCK_LOCK(so); 1053 so->so_state |= SS_PROTOREF; 1054 SOCK_UNLOCK(so); 1055 inp->inp_flags |= INP_SOCKREF; 1056 } 1057 INP_WUNLOCK(inp); 1058 INP_INFO_WUNLOCK(&V_tcbinfo); 1059 } 1060 1061 /* 1062 * Receive out-of-band data. 1063 */ 1064 static int 1065 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 1066 { 1067 int error = 0; 1068 struct inpcb *inp; 1069 struct tcpcb *tp = NULL; 1070 1071 TCPDEBUG0; 1072 inp = sotoinpcb(so); 1073 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 1074 INP_WLOCK(inp); 1075 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1076 error = ECONNRESET; 1077 goto out; 1078 } 1079 tp = intotcpcb(inp); 1080 TCPDEBUG1(); 1081 if ((so->so_oobmark == 0 && 1082 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 1083 so->so_options & SO_OOBINLINE || 1084 tp->t_oobflags & TCPOOB_HADDATA) { 1085 error = EINVAL; 1086 goto out; 1087 } 1088 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 1089 error = EWOULDBLOCK; 1090 goto out; 1091 } 1092 m->m_len = 1; 1093 *mtod(m, caddr_t) = tp->t_iobc; 1094 if ((flags & MSG_PEEK) == 0) 1095 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1096 1097 out: 1098 TCPDEBUG2(PRU_RCVOOB); 1099 INP_WUNLOCK(inp); 1100 return (error); 1101 } 1102 1103 #ifdef INET 1104 struct pr_usrreqs tcp_usrreqs = { 1105 .pru_abort = tcp_usr_abort, 1106 .pru_accept = tcp_usr_accept, 1107 .pru_attach = tcp_usr_attach, 1108 .pru_bind = tcp_usr_bind, 1109 .pru_connect = tcp_usr_connect, 1110 .pru_control = in_control, 1111 .pru_detach = tcp_usr_detach, 1112 .pru_disconnect = tcp_usr_disconnect, 1113 .pru_listen = tcp_usr_listen, 1114 .pru_peeraddr = in_getpeeraddr, 1115 .pru_rcvd = tcp_usr_rcvd, 1116 .pru_rcvoob = tcp_usr_rcvoob, 1117 .pru_send = tcp_usr_send, 1118 .pru_ready = tcp_usr_ready, 1119 .pru_shutdown = tcp_usr_shutdown, 1120 .pru_sockaddr = in_getsockaddr, 1121 .pru_sosetlabel = in_pcbsosetlabel, 1122 .pru_close = tcp_usr_close, 1123 }; 1124 #endif /* INET */ 1125 1126 #ifdef INET6 1127 struct pr_usrreqs tcp6_usrreqs = { 1128 .pru_abort = tcp_usr_abort, 1129 .pru_accept = tcp6_usr_accept, 1130 .pru_attach = tcp_usr_attach, 1131 .pru_bind = tcp6_usr_bind, 1132 .pru_connect = tcp6_usr_connect, 1133 .pru_control = in6_control, 1134 .pru_detach = tcp_usr_detach, 1135 .pru_disconnect = tcp_usr_disconnect, 1136 .pru_listen = tcp6_usr_listen, 1137 .pru_peeraddr = in6_mapped_peeraddr, 1138 .pru_rcvd = tcp_usr_rcvd, 1139 .pru_rcvoob = tcp_usr_rcvoob, 1140 .pru_send = tcp_usr_send, 1141 .pru_ready = tcp_usr_ready, 1142 .pru_shutdown = tcp_usr_shutdown, 1143 .pru_sockaddr = in6_mapped_sockaddr, 1144 .pru_sosetlabel = in_pcbsosetlabel, 1145 .pru_close = tcp_usr_close, 1146 }; 1147 #endif /* INET6 */ 1148 1149 #ifdef INET 1150 /* 1151 * Common subroutine to open a TCP connection to remote host specified 1152 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 1153 * port number if needed. Call in_pcbconnect_setup to do the routing and 1154 * to choose a local host address (interface). If there is an existing 1155 * incarnation of the same connection in TIME-WAIT state and if the remote 1156 * host was sending CC options and if the connection duration was < MSL, then 1157 * truncate the previous TIME-WAIT state and proceed. 1158 * Initialize connection parameters and enter SYN-SENT state. 1159 */ 1160 static int 1161 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1162 { 1163 struct inpcb *inp = tp->t_inpcb, *oinp; 1164 struct socket *so = inp->inp_socket; 1165 struct in_addr laddr; 1166 u_short lport; 1167 int error; 1168 1169 INP_WLOCK_ASSERT(inp); 1170 INP_HASH_WLOCK(&V_tcbinfo); 1171 1172 if (inp->inp_lport == 0) { 1173 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1174 if (error) 1175 goto out; 1176 } 1177 1178 /* 1179 * Cannot simply call in_pcbconnect, because there might be an 1180 * earlier incarnation of this same connection still in 1181 * TIME_WAIT state, creating an ADDRINUSE error. 1182 */ 1183 laddr = inp->inp_laddr; 1184 lport = inp->inp_lport; 1185 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1186 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 1187 if (error && oinp == NULL) 1188 goto out; 1189 if (oinp) { 1190 error = EADDRINUSE; 1191 goto out; 1192 } 1193 inp->inp_laddr = laddr; 1194 in_pcbrehash(inp); 1195 INP_HASH_WUNLOCK(&V_tcbinfo); 1196 1197 /* 1198 * Compute window scaling to request: 1199 * Scale to fit into sweet spot. See tcp_syncache.c. 1200 * XXX: This should move to tcp_output(). 1201 */ 1202 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1203 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1204 tp->request_r_scale++; 1205 1206 soisconnecting(so); 1207 TCPSTAT_INC(tcps_connattempt); 1208 tcp_state_change(tp, TCPS_SYN_SENT); 1209 tp->iss = tcp_new_isn(tp); 1210 tcp_sendseqinit(tp); 1211 1212 return 0; 1213 1214 out: 1215 INP_HASH_WUNLOCK(&V_tcbinfo); 1216 return (error); 1217 } 1218 #endif /* INET */ 1219 1220 #ifdef INET6 1221 static int 1222 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1223 { 1224 struct inpcb *inp = tp->t_inpcb; 1225 int error; 1226 1227 INP_WLOCK_ASSERT(inp); 1228 INP_HASH_WLOCK(&V_tcbinfo); 1229 1230 if (inp->inp_lport == 0) { 1231 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1232 if (error) 1233 goto out; 1234 } 1235 error = in6_pcbconnect(inp, nam, td->td_ucred); 1236 if (error != 0) 1237 goto out; 1238 INP_HASH_WUNLOCK(&V_tcbinfo); 1239 1240 /* Compute window scaling to request. */ 1241 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1242 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1243 tp->request_r_scale++; 1244 1245 soisconnecting(inp->inp_socket); 1246 TCPSTAT_INC(tcps_connattempt); 1247 tcp_state_change(tp, TCPS_SYN_SENT); 1248 tp->iss = tcp_new_isn(tp); 1249 tcp_sendseqinit(tp); 1250 1251 return 0; 1252 1253 out: 1254 INP_HASH_WUNLOCK(&V_tcbinfo); 1255 return error; 1256 } 1257 #endif /* INET6 */ 1258 1259 /* 1260 * Export TCP internal state information via a struct tcp_info, based on the 1261 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1262 * (TCP state machine, etc). We export all information using FreeBSD-native 1263 * constants -- for example, the numeric values for tcpi_state will differ 1264 * from Linux. 1265 */ 1266 static void 1267 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1268 { 1269 1270 INP_WLOCK_ASSERT(tp->t_inpcb); 1271 bzero(ti, sizeof(*ti)); 1272 1273 ti->tcpi_state = tp->t_state; 1274 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1275 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1276 if (tp->t_flags & TF_SACK_PERMIT) 1277 ti->tcpi_options |= TCPI_OPT_SACK; 1278 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1279 ti->tcpi_options |= TCPI_OPT_WSCALE; 1280 ti->tcpi_snd_wscale = tp->snd_scale; 1281 ti->tcpi_rcv_wscale = tp->rcv_scale; 1282 } 1283 1284 ti->tcpi_rto = tp->t_rxtcur * tick; 1285 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 1286 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 1287 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 1288 1289 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1290 ti->tcpi_snd_cwnd = tp->snd_cwnd; 1291 1292 /* 1293 * FreeBSD-specific extension fields for tcp_info. 1294 */ 1295 ti->tcpi_rcv_space = tp->rcv_wnd; 1296 ti->tcpi_rcv_nxt = tp->rcv_nxt; 1297 ti->tcpi_snd_wnd = tp->snd_wnd; 1298 ti->tcpi_snd_bwnd = 0; /* Unused, kept for compat. */ 1299 ti->tcpi_snd_nxt = tp->snd_nxt; 1300 ti->tcpi_snd_mss = tp->t_maxseg; 1301 ti->tcpi_rcv_mss = tp->t_maxseg; 1302 if (tp->t_flags & TF_TOE) 1303 ti->tcpi_options |= TCPI_OPT_TOE; 1304 ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack; 1305 ti->tcpi_rcv_ooopack = tp->t_rcvoopack; 1306 ti->tcpi_snd_zerowin = tp->t_sndzerowin; 1307 } 1308 1309 /* 1310 * tcp_ctloutput() must drop the inpcb lock before performing copyin on 1311 * socket option arguments. When it re-acquires the lock after the copy, it 1312 * has to revalidate that the connection is still valid for the socket 1313 * option. 1314 */ 1315 #define INP_WLOCK_RECHECK(inp) do { \ 1316 INP_WLOCK(inp); \ 1317 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 1318 INP_WUNLOCK(inp); \ 1319 return (ECONNRESET); \ 1320 } \ 1321 tp = intotcpcb(inp); \ 1322 } while(0) 1323 1324 int 1325 tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1326 { 1327 int error, opt, optval; 1328 u_int ui; 1329 struct inpcb *inp; 1330 struct tcpcb *tp; 1331 struct tcp_info ti; 1332 char buf[TCP_CA_NAME_MAX]; 1333 struct cc_algo *algo; 1334 1335 error = 0; 1336 inp = sotoinpcb(so); 1337 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1338 INP_WLOCK(inp); 1339 if (sopt->sopt_level != IPPROTO_TCP) { 1340 #ifdef INET6 1341 if (inp->inp_vflag & INP_IPV6PROTO) { 1342 INP_WUNLOCK(inp); 1343 error = ip6_ctloutput(so, sopt); 1344 } 1345 #endif /* INET6 */ 1346 #if defined(INET6) && defined(INET) 1347 else 1348 #endif 1349 #ifdef INET 1350 { 1351 INP_WUNLOCK(inp); 1352 error = ip_ctloutput(so, sopt); 1353 } 1354 #endif 1355 return (error); 1356 } 1357 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1358 INP_WUNLOCK(inp); 1359 return (ECONNRESET); 1360 } 1361 1362 switch (sopt->sopt_dir) { 1363 case SOPT_SET: 1364 switch (sopt->sopt_name) { 1365 #ifdef TCP_SIGNATURE 1366 case TCP_MD5SIG: 1367 INP_WUNLOCK(inp); 1368 error = sooptcopyin(sopt, &optval, sizeof optval, 1369 sizeof optval); 1370 if (error) 1371 return (error); 1372 1373 INP_WLOCK_RECHECK(inp); 1374 if (optval > 0) 1375 tp->t_flags |= TF_SIGNATURE; 1376 else 1377 tp->t_flags &= ~TF_SIGNATURE; 1378 goto unlock_and_done; 1379 #endif /* TCP_SIGNATURE */ 1380 1381 case TCP_NODELAY: 1382 case TCP_NOOPT: 1383 INP_WUNLOCK(inp); 1384 error = sooptcopyin(sopt, &optval, sizeof optval, 1385 sizeof optval); 1386 if (error) 1387 return (error); 1388 1389 INP_WLOCK_RECHECK(inp); 1390 switch (sopt->sopt_name) { 1391 case TCP_NODELAY: 1392 opt = TF_NODELAY; 1393 break; 1394 case TCP_NOOPT: 1395 opt = TF_NOOPT; 1396 break; 1397 default: 1398 opt = 0; /* dead code to fool gcc */ 1399 break; 1400 } 1401 1402 if (optval) 1403 tp->t_flags |= opt; 1404 else 1405 tp->t_flags &= ~opt; 1406 unlock_and_done: 1407 #ifdef TCP_OFFLOAD 1408 if (tp->t_flags & TF_TOE) { 1409 tcp_offload_ctloutput(tp, sopt->sopt_dir, 1410 sopt->sopt_name); 1411 } 1412 #endif 1413 INP_WUNLOCK(inp); 1414 break; 1415 1416 case TCP_NOPUSH: 1417 INP_WUNLOCK(inp); 1418 error = sooptcopyin(sopt, &optval, sizeof optval, 1419 sizeof optval); 1420 if (error) 1421 return (error); 1422 1423 INP_WLOCK_RECHECK(inp); 1424 if (optval) 1425 tp->t_flags |= TF_NOPUSH; 1426 else if (tp->t_flags & TF_NOPUSH) { 1427 tp->t_flags &= ~TF_NOPUSH; 1428 if (TCPS_HAVEESTABLISHED(tp->t_state)) 1429 error = tcp_output(tp); 1430 } 1431 goto unlock_and_done; 1432 1433 case TCP_MAXSEG: 1434 INP_WUNLOCK(inp); 1435 error = sooptcopyin(sopt, &optval, sizeof optval, 1436 sizeof optval); 1437 if (error) 1438 return (error); 1439 1440 INP_WLOCK_RECHECK(inp); 1441 if (optval > 0 && optval <= tp->t_maxseg && 1442 optval + 40 >= V_tcp_minmss) 1443 tp->t_maxseg = optval; 1444 else 1445 error = EINVAL; 1446 goto unlock_and_done; 1447 1448 case TCP_INFO: 1449 INP_WUNLOCK(inp); 1450 error = EINVAL; 1451 break; 1452 1453 case TCP_CONGESTION: 1454 INP_WUNLOCK(inp); 1455 bzero(buf, sizeof(buf)); 1456 error = sooptcopyin(sopt, &buf, sizeof(buf), 1); 1457 if (error) 1458 break; 1459 INP_WLOCK_RECHECK(inp); 1460 /* 1461 * Return EINVAL if we can't find the requested cc algo. 1462 */ 1463 error = EINVAL; 1464 CC_LIST_RLOCK(); 1465 STAILQ_FOREACH(algo, &cc_list, entries) { 1466 if (strncmp(buf, algo->name, TCP_CA_NAME_MAX) 1467 == 0) { 1468 /* We've found the requested algo. */ 1469 error = 0; 1470 /* 1471 * We hold a write lock over the tcb 1472 * so it's safe to do these things 1473 * without ordering concerns. 1474 */ 1475 if (CC_ALGO(tp)->cb_destroy != NULL) 1476 CC_ALGO(tp)->cb_destroy(tp->ccv); 1477 CC_ALGO(tp) = algo; 1478 /* 1479 * If something goes pear shaped 1480 * initialising the new algo, 1481 * fall back to newreno (which 1482 * does not require initialisation). 1483 */ 1484 if (algo->cb_init != NULL) 1485 if (algo->cb_init(tp->ccv) > 0) { 1486 CC_ALGO(tp) = &newreno_cc_algo; 1487 /* 1488 * The only reason init 1489 * should fail is 1490 * because of malloc. 1491 */ 1492 error = ENOMEM; 1493 } 1494 break; /* Break the STAILQ_FOREACH. */ 1495 } 1496 } 1497 CC_LIST_RUNLOCK(); 1498 goto unlock_and_done; 1499 1500 case TCP_KEEPIDLE: 1501 case TCP_KEEPINTVL: 1502 case TCP_KEEPINIT: 1503 INP_WUNLOCK(inp); 1504 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1505 if (error) 1506 return (error); 1507 1508 if (ui > (UINT_MAX / hz)) { 1509 error = EINVAL; 1510 break; 1511 } 1512 ui *= hz; 1513 1514 INP_WLOCK_RECHECK(inp); 1515 switch (sopt->sopt_name) { 1516 case TCP_KEEPIDLE: 1517 tp->t_keepidle = ui; 1518 /* 1519 * XXX: better check current remaining 1520 * timeout and "merge" it with new value. 1521 */ 1522 if ((tp->t_state > TCPS_LISTEN) && 1523 (tp->t_state <= TCPS_CLOSING)) 1524 tcp_timer_activate(tp, TT_KEEP, 1525 TP_KEEPIDLE(tp)); 1526 break; 1527 case TCP_KEEPINTVL: 1528 tp->t_keepintvl = ui; 1529 if ((tp->t_state == TCPS_FIN_WAIT_2) && 1530 (TP_MAXIDLE(tp) > 0)) 1531 tcp_timer_activate(tp, TT_2MSL, 1532 TP_MAXIDLE(tp)); 1533 break; 1534 case TCP_KEEPINIT: 1535 tp->t_keepinit = ui; 1536 if (tp->t_state == TCPS_SYN_RECEIVED || 1537 tp->t_state == TCPS_SYN_SENT) 1538 tcp_timer_activate(tp, TT_KEEP, 1539 TP_KEEPINIT(tp)); 1540 break; 1541 } 1542 goto unlock_and_done; 1543 1544 case TCP_KEEPCNT: 1545 INP_WUNLOCK(inp); 1546 error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui)); 1547 if (error) 1548 return (error); 1549 1550 INP_WLOCK_RECHECK(inp); 1551 tp->t_keepcnt = ui; 1552 if ((tp->t_state == TCPS_FIN_WAIT_2) && 1553 (TP_MAXIDLE(tp) > 0)) 1554 tcp_timer_activate(tp, TT_2MSL, 1555 TP_MAXIDLE(tp)); 1556 goto unlock_and_done; 1557 1558 default: 1559 INP_WUNLOCK(inp); 1560 error = ENOPROTOOPT; 1561 break; 1562 } 1563 break; 1564 1565 case SOPT_GET: 1566 tp = intotcpcb(inp); 1567 switch (sopt->sopt_name) { 1568 #ifdef TCP_SIGNATURE 1569 case TCP_MD5SIG: 1570 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1571 INP_WUNLOCK(inp); 1572 error = sooptcopyout(sopt, &optval, sizeof optval); 1573 break; 1574 #endif 1575 1576 case TCP_NODELAY: 1577 optval = tp->t_flags & TF_NODELAY; 1578 INP_WUNLOCK(inp); 1579 error = sooptcopyout(sopt, &optval, sizeof optval); 1580 break; 1581 case TCP_MAXSEG: 1582 optval = tp->t_maxseg; 1583 INP_WUNLOCK(inp); 1584 error = sooptcopyout(sopt, &optval, sizeof optval); 1585 break; 1586 case TCP_NOOPT: 1587 optval = tp->t_flags & TF_NOOPT; 1588 INP_WUNLOCK(inp); 1589 error = sooptcopyout(sopt, &optval, sizeof optval); 1590 break; 1591 case TCP_NOPUSH: 1592 optval = tp->t_flags & TF_NOPUSH; 1593 INP_WUNLOCK(inp); 1594 error = sooptcopyout(sopt, &optval, sizeof optval); 1595 break; 1596 case TCP_INFO: 1597 tcp_fill_info(tp, &ti); 1598 INP_WUNLOCK(inp); 1599 error = sooptcopyout(sopt, &ti, sizeof ti); 1600 break; 1601 case TCP_CONGESTION: 1602 bzero(buf, sizeof(buf)); 1603 strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX); 1604 INP_WUNLOCK(inp); 1605 error = sooptcopyout(sopt, buf, TCP_CA_NAME_MAX); 1606 break; 1607 case TCP_KEEPIDLE: 1608 case TCP_KEEPINTVL: 1609 case TCP_KEEPINIT: 1610 case TCP_KEEPCNT: 1611 switch (sopt->sopt_name) { 1612 case TCP_KEEPIDLE: 1613 ui = tp->t_keepidle / hz; 1614 break; 1615 case TCP_KEEPINTVL: 1616 ui = tp->t_keepintvl / hz; 1617 break; 1618 case TCP_KEEPINIT: 1619 ui = tp->t_keepinit / hz; 1620 break; 1621 case TCP_KEEPCNT: 1622 ui = tp->t_keepcnt; 1623 break; 1624 } 1625 INP_WUNLOCK(inp); 1626 error = sooptcopyout(sopt, &ui, sizeof(ui)); 1627 break; 1628 default: 1629 INP_WUNLOCK(inp); 1630 error = ENOPROTOOPT; 1631 break; 1632 } 1633 break; 1634 } 1635 return (error); 1636 } 1637 #undef INP_WLOCK_RECHECK 1638 1639 /* 1640 * Attach TCP protocol to socket, allocating 1641 * internet protocol control block, tcp control block, 1642 * bufer space, and entering LISTEN state if to accept connections. 1643 */ 1644 static int 1645 tcp_attach(struct socket *so) 1646 { 1647 struct tcpcb *tp; 1648 struct inpcb *inp; 1649 int error; 1650 1651 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1652 error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace); 1653 if (error) 1654 return (error); 1655 } 1656 so->so_rcv.sb_flags |= SB_AUTOSIZE; 1657 so->so_snd.sb_flags |= SB_AUTOSIZE; 1658 INP_INFO_WLOCK(&V_tcbinfo); 1659 error = in_pcballoc(so, &V_tcbinfo); 1660 if (error) { 1661 INP_INFO_WUNLOCK(&V_tcbinfo); 1662 return (error); 1663 } 1664 inp = sotoinpcb(so); 1665 #ifdef INET6 1666 if (inp->inp_vflag & INP_IPV6PROTO) { 1667 inp->inp_vflag |= INP_IPV6; 1668 inp->in6p_hops = -1; /* use kernel default */ 1669 } 1670 else 1671 #endif 1672 inp->inp_vflag |= INP_IPV4; 1673 tp = tcp_newtcpcb(inp); 1674 if (tp == NULL) { 1675 in_pcbdetach(inp); 1676 in_pcbfree(inp); 1677 INP_INFO_WUNLOCK(&V_tcbinfo); 1678 return (ENOBUFS); 1679 } 1680 tp->t_state = TCPS_CLOSED; 1681 INP_WUNLOCK(inp); 1682 INP_INFO_WUNLOCK(&V_tcbinfo); 1683 return (0); 1684 } 1685 1686 /* 1687 * Initiate (or continue) disconnect. 1688 * If embryonic state, just send reset (once). 1689 * If in ``let data drain'' option and linger null, just drop. 1690 * Otherwise (hard), mark socket disconnecting and drop 1691 * current input data; switch states based on user close, and 1692 * send segment to peer (with FIN). 1693 */ 1694 static void 1695 tcp_disconnect(struct tcpcb *tp) 1696 { 1697 struct inpcb *inp = tp->t_inpcb; 1698 struct socket *so = inp->inp_socket; 1699 1700 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1701 INP_WLOCK_ASSERT(inp); 1702 1703 /* 1704 * Neither tcp_close() nor tcp_drop() should return NULL, as the 1705 * socket is still open. 1706 */ 1707 if (tp->t_state < TCPS_ESTABLISHED) { 1708 tp = tcp_close(tp); 1709 KASSERT(tp != NULL, 1710 ("tcp_disconnect: tcp_close() returned NULL")); 1711 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1712 tp = tcp_drop(tp, 0); 1713 KASSERT(tp != NULL, 1714 ("tcp_disconnect: tcp_drop() returned NULL")); 1715 } else { 1716 soisdisconnecting(so); 1717 sbflush(&so->so_rcv); 1718 tcp_usrclosed(tp); 1719 if (!(inp->inp_flags & INP_DROPPED)) 1720 tcp_output(tp); 1721 } 1722 } 1723 1724 /* 1725 * User issued close, and wish to trail through shutdown states: 1726 * if never received SYN, just forget it. If got a SYN from peer, 1727 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1728 * If already got a FIN from peer, then almost done; go to LAST_ACK 1729 * state. In all other cases, have already sent FIN to peer (e.g. 1730 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1731 * for peer to send FIN or not respond to keep-alives, etc. 1732 * We can let the user exit from the close as soon as the FIN is acked. 1733 */ 1734 static void 1735 tcp_usrclosed(struct tcpcb *tp) 1736 { 1737 1738 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1739 INP_WLOCK_ASSERT(tp->t_inpcb); 1740 1741 switch (tp->t_state) { 1742 case TCPS_LISTEN: 1743 #ifdef TCP_OFFLOAD 1744 tcp_offload_listen_stop(tp); 1745 #endif 1746 /* FALLTHROUGH */ 1747 case TCPS_CLOSED: 1748 tcp_state_change(tp, TCPS_CLOSED); 1749 tp = tcp_close(tp); 1750 /* 1751 * tcp_close() should never return NULL here as the socket is 1752 * still open. 1753 */ 1754 KASSERT(tp != NULL, 1755 ("tcp_usrclosed: tcp_close() returned NULL")); 1756 break; 1757 1758 case TCPS_SYN_SENT: 1759 case TCPS_SYN_RECEIVED: 1760 tp->t_flags |= TF_NEEDFIN; 1761 break; 1762 1763 case TCPS_ESTABLISHED: 1764 tcp_state_change(tp, TCPS_FIN_WAIT_1); 1765 break; 1766 1767 case TCPS_CLOSE_WAIT: 1768 tcp_state_change(tp, TCPS_LAST_ACK); 1769 break; 1770 } 1771 if (tp->t_state >= TCPS_FIN_WAIT_2) { 1772 soisdisconnected(tp->t_inpcb->inp_socket); 1773 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 1774 if (tp->t_state == TCPS_FIN_WAIT_2) { 1775 int timeout; 1776 1777 timeout = (tcp_fast_finwait2_recycle) ? 1778 tcp_finwait2_timeout : TP_MAXIDLE(tp); 1779 tcp_timer_activate(tp, TT_2MSL, timeout); 1780 } 1781 } 1782 } 1783 1784 #ifdef DDB 1785 static void 1786 db_print_indent(int indent) 1787 { 1788 int i; 1789 1790 for (i = 0; i < indent; i++) 1791 db_printf(" "); 1792 } 1793 1794 static void 1795 db_print_tstate(int t_state) 1796 { 1797 1798 switch (t_state) { 1799 case TCPS_CLOSED: 1800 db_printf("TCPS_CLOSED"); 1801 return; 1802 1803 case TCPS_LISTEN: 1804 db_printf("TCPS_LISTEN"); 1805 return; 1806 1807 case TCPS_SYN_SENT: 1808 db_printf("TCPS_SYN_SENT"); 1809 return; 1810 1811 case TCPS_SYN_RECEIVED: 1812 db_printf("TCPS_SYN_RECEIVED"); 1813 return; 1814 1815 case TCPS_ESTABLISHED: 1816 db_printf("TCPS_ESTABLISHED"); 1817 return; 1818 1819 case TCPS_CLOSE_WAIT: 1820 db_printf("TCPS_CLOSE_WAIT"); 1821 return; 1822 1823 case TCPS_FIN_WAIT_1: 1824 db_printf("TCPS_FIN_WAIT_1"); 1825 return; 1826 1827 case TCPS_CLOSING: 1828 db_printf("TCPS_CLOSING"); 1829 return; 1830 1831 case TCPS_LAST_ACK: 1832 db_printf("TCPS_LAST_ACK"); 1833 return; 1834 1835 case TCPS_FIN_WAIT_2: 1836 db_printf("TCPS_FIN_WAIT_2"); 1837 return; 1838 1839 case TCPS_TIME_WAIT: 1840 db_printf("TCPS_TIME_WAIT"); 1841 return; 1842 1843 default: 1844 db_printf("unknown"); 1845 return; 1846 } 1847 } 1848 1849 static void 1850 db_print_tflags(u_int t_flags) 1851 { 1852 int comma; 1853 1854 comma = 0; 1855 if (t_flags & TF_ACKNOW) { 1856 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1857 comma = 1; 1858 } 1859 if (t_flags & TF_DELACK) { 1860 db_printf("%sTF_DELACK", comma ? ", " : ""); 1861 comma = 1; 1862 } 1863 if (t_flags & TF_NODELAY) { 1864 db_printf("%sTF_NODELAY", comma ? ", " : ""); 1865 comma = 1; 1866 } 1867 if (t_flags & TF_NOOPT) { 1868 db_printf("%sTF_NOOPT", comma ? ", " : ""); 1869 comma = 1; 1870 } 1871 if (t_flags & TF_SENTFIN) { 1872 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1873 comma = 1; 1874 } 1875 if (t_flags & TF_REQ_SCALE) { 1876 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1877 comma = 1; 1878 } 1879 if (t_flags & TF_RCVD_SCALE) { 1880 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1881 comma = 1; 1882 } 1883 if (t_flags & TF_REQ_TSTMP) { 1884 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1885 comma = 1; 1886 } 1887 if (t_flags & TF_RCVD_TSTMP) { 1888 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1889 comma = 1; 1890 } 1891 if (t_flags & TF_SACK_PERMIT) { 1892 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1893 comma = 1; 1894 } 1895 if (t_flags & TF_NEEDSYN) { 1896 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1897 comma = 1; 1898 } 1899 if (t_flags & TF_NEEDFIN) { 1900 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1901 comma = 1; 1902 } 1903 if (t_flags & TF_NOPUSH) { 1904 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1905 comma = 1; 1906 } 1907 if (t_flags & TF_MORETOCOME) { 1908 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1909 comma = 1; 1910 } 1911 if (t_flags & TF_LQ_OVERFLOW) { 1912 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1913 comma = 1; 1914 } 1915 if (t_flags & TF_LASTIDLE) { 1916 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1917 comma = 1; 1918 } 1919 if (t_flags & TF_RXWIN0SENT) { 1920 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1921 comma = 1; 1922 } 1923 if (t_flags & TF_FASTRECOVERY) { 1924 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1925 comma = 1; 1926 } 1927 if (t_flags & TF_CONGRECOVERY) { 1928 db_printf("%sTF_CONGRECOVERY", comma ? ", " : ""); 1929 comma = 1; 1930 } 1931 if (t_flags & TF_WASFRECOVERY) { 1932 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1933 comma = 1; 1934 } 1935 if (t_flags & TF_SIGNATURE) { 1936 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1937 comma = 1; 1938 } 1939 if (t_flags & TF_FORCEDATA) { 1940 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1941 comma = 1; 1942 } 1943 if (t_flags & TF_TSO) { 1944 db_printf("%sTF_TSO", comma ? ", " : ""); 1945 comma = 1; 1946 } 1947 if (t_flags & TF_ECN_PERMIT) { 1948 db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1949 comma = 1; 1950 } 1951 } 1952 1953 static void 1954 db_print_toobflags(char t_oobflags) 1955 { 1956 int comma; 1957 1958 comma = 0; 1959 if (t_oobflags & TCPOOB_HAVEDATA) { 1960 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1961 comma = 1; 1962 } 1963 if (t_oobflags & TCPOOB_HADDATA) { 1964 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1965 comma = 1; 1966 } 1967 } 1968 1969 static void 1970 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1971 { 1972 1973 db_print_indent(indent); 1974 db_printf("%s at %p\n", name, tp); 1975 1976 indent += 2; 1977 1978 db_print_indent(indent); 1979 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 1980 tp->t_segq, tp->t_segqlen, tp->t_dupacks); 1981 1982 db_print_indent(indent); 1983 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 1984 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 1985 1986 db_print_indent(indent); 1987 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 1988 &tp->t_timers->tt_delack, tp->t_inpcb); 1989 1990 db_print_indent(indent); 1991 db_printf("t_state: %d (", tp->t_state); 1992 db_print_tstate(tp->t_state); 1993 db_printf(")\n"); 1994 1995 db_print_indent(indent); 1996 db_printf("t_flags: 0x%x (", tp->t_flags); 1997 db_print_tflags(tp->t_flags); 1998 db_printf(")\n"); 1999 2000 db_print_indent(indent); 2001 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 2002 tp->snd_una, tp->snd_max, tp->snd_nxt); 2003 2004 db_print_indent(indent); 2005 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 2006 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 2007 2008 db_print_indent(indent); 2009 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 2010 tp->iss, tp->irs, tp->rcv_nxt); 2011 2012 db_print_indent(indent); 2013 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 2014 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 2015 2016 db_print_indent(indent); 2017 db_printf("snd_wnd: %lu snd_cwnd: %lu\n", 2018 tp->snd_wnd, tp->snd_cwnd); 2019 2020 db_print_indent(indent); 2021 db_printf("snd_ssthresh: %lu snd_recover: " 2022 "0x%08x\n", tp->snd_ssthresh, tp->snd_recover); 2023 2024 db_print_indent(indent); 2025 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 2026 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 2027 2028 db_print_indent(indent); 2029 db_printf("t_rttime: %u t_rtsq: 0x%08x\n", 2030 tp->t_rtttime, tp->t_rtseq); 2031 2032 db_print_indent(indent); 2033 db_printf("t_rxtcur: %d t_maxseg: %u t_srtt: %d\n", 2034 tp->t_rxtcur, tp->t_maxseg, tp->t_srtt); 2035 2036 db_print_indent(indent); 2037 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 2038 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 2039 tp->t_rttbest); 2040 2041 db_print_indent(indent); 2042 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 2043 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 2044 2045 db_print_indent(indent); 2046 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 2047 db_print_toobflags(tp->t_oobflags); 2048 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 2049 2050 db_print_indent(indent); 2051 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 2052 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 2053 2054 db_print_indent(indent); 2055 db_printf("ts_recent: %u ts_recent_age: %u\n", 2056 tp->ts_recent, tp->ts_recent_age); 2057 2058 db_print_indent(indent); 2059 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 2060 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 2061 2062 db_print_indent(indent); 2063 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 2064 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 2065 tp->snd_recover_prev, tp->t_badrxtwin); 2066 2067 db_print_indent(indent); 2068 db_printf("snd_numholes: %d snd_holes first: %p\n", 2069 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 2070 2071 db_print_indent(indent); 2072 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 2073 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 2074 2075 /* Skip sackblks, sackhint. */ 2076 2077 db_print_indent(indent); 2078 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 2079 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 2080 } 2081 2082 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 2083 { 2084 struct tcpcb *tp; 2085 2086 if (!have_addr) { 2087 db_printf("usage: show tcpcb <addr>\n"); 2088 return; 2089 } 2090 tp = (struct tcpcb *)addr; 2091 2092 db_print_tcpcb(tp, "tcpcb", 0); 2093 } 2094 #endif 2095