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