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