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