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