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 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_ddb.h" 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 #include "opt_tcpdebug.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/malloc.h> 45 #include <sys/kernel.h> 46 #include <sys/sysctl.h> 47 #include <sys/mbuf.h> 48 #ifdef INET6 49 #include <sys/domain.h> 50 #endif /* INET6 */ 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/protosw.h> 54 #include <sys/proc.h> 55 #include <sys/jail.h> 56 57 #ifdef DDB 58 #include <ddb/ddb.h> 59 #endif 60 61 #include <net/if.h> 62 #include <net/route.h> 63 #include <net/vnet.h> 64 65 #include <netinet/in.h> 66 #include <netinet/in_systm.h> 67 #ifdef INET6 68 #include <netinet/ip6.h> 69 #endif 70 #include <netinet/in_pcb.h> 71 #ifdef INET6 72 #include <netinet6/in6_pcb.h> 73 #endif 74 #include <netinet/in_var.h> 75 #include <netinet/ip_var.h> 76 #ifdef INET6 77 #include <netinet6/ip6_var.h> 78 #include <netinet6/scope6_var.h> 79 #endif 80 #include <netinet/tcp.h> 81 #include <netinet/tcp_fsm.h> 82 #include <netinet/tcp_seq.h> 83 #include <netinet/tcp_timer.h> 84 #include <netinet/tcp_var.h> 85 #include <netinet/tcpip.h> 86 #ifdef TCPDEBUG 87 #include <netinet/tcp_debug.h> 88 #endif 89 #include <netinet/tcp_offload.h> 90 91 /* 92 * TCP protocol interface to socket abstraction. 93 */ 94 static int tcp_attach(struct socket *); 95 static int tcp_connect(struct tcpcb *, struct sockaddr *, 96 struct thread *td); 97 #ifdef INET6 98 static int tcp6_connect(struct tcpcb *, struct sockaddr *, 99 struct thread *td); 100 #endif /* INET6 */ 101 static void tcp_disconnect(struct tcpcb *); 102 static void tcp_usrclosed(struct tcpcb *); 103 static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 104 105 #ifdef TCPDEBUG 106 #define TCPDEBUG0 int ostate = 0 107 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 108 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 109 tcp_trace(TA_USER, ostate, tp, 0, 0, req) 110 #else 111 #define TCPDEBUG0 112 #define TCPDEBUG1() 113 #define TCPDEBUG2(req) 114 #endif 115 116 /* 117 * TCP attaches to socket via pru_attach(), reserving space, 118 * and an internet control block. 119 */ 120 static int 121 tcp_usr_attach(struct socket *so, int proto, struct thread *td) 122 { 123 struct inpcb *inp; 124 struct tcpcb *tp = NULL; 125 int error; 126 TCPDEBUG0; 127 128 inp = sotoinpcb(so); 129 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 130 TCPDEBUG1(); 131 132 error = tcp_attach(so); 133 if (error) 134 goto out; 135 136 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 137 so->so_linger = TCP_LINGERTIME; 138 139 inp = sotoinpcb(so); 140 tp = intotcpcb(inp); 141 out: 142 TCPDEBUG2(PRU_ATTACH); 143 return error; 144 } 145 146 /* 147 * tcp_detach is called when the socket layer loses its final reference 148 * to the socket, be it a file descriptor reference, a reference from TCP, 149 * etc. At this point, there is only one case in which we will keep around 150 * inpcb state: time wait. 151 * 152 * This function can probably be re-absorbed back into tcp_usr_detach() now 153 * that there is a single detach path. 154 */ 155 static void 156 tcp_detach(struct socket *so, struct inpcb *inp) 157 { 158 struct tcpcb *tp; 159 160 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 161 INP_WLOCK_ASSERT(inp); 162 163 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 164 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 165 166 tp = intotcpcb(inp); 167 168 if (inp->inp_flags & INP_TIMEWAIT) { 169 /* 170 * There are two cases to handle: one in which the time wait 171 * state is being discarded (INP_DROPPED), and one in which 172 * this connection will remain in timewait. In the former, 173 * it is time to discard all state (except tcptw, which has 174 * already been discarded by the timewait close code, which 175 * should be further up the call stack somewhere). In the 176 * latter case, we detach from the socket, but leave the pcb 177 * present until timewait ends. 178 * 179 * XXXRW: Would it be cleaner to free the tcptw here? 180 */ 181 if (inp->inp_flags & INP_DROPPED) { 182 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && " 183 "INP_DROPPED && tp != NULL")); 184 in_pcbdetach(inp); 185 in_pcbfree(inp); 186 } else { 187 in_pcbdetach(inp); 188 INP_WUNLOCK(inp); 189 } 190 } else { 191 /* 192 * If the connection is not in timewait, we consider two 193 * two conditions: one in which no further processing is 194 * necessary (dropped || embryonic), and one in which TCP is 195 * not yet done, but no longer requires the socket, so the 196 * pcb will persist for the time being. 197 * 198 * XXXRW: Does the second case still occur? 199 */ 200 if (inp->inp_flags & INP_DROPPED || 201 tp->t_state < TCPS_SYN_SENT) { 202 tcp_discardcb(tp); 203 in_pcbdetach(inp); 204 in_pcbfree(inp); 205 } else 206 in_pcbdetach(inp); 207 } 208 } 209 210 /* 211 * pru_detach() detaches the TCP protocol from the socket. 212 * If the protocol state is non-embryonic, then can't 213 * do this directly: have to initiate a pru_disconnect(), 214 * which may finish later; embryonic TCB's can just 215 * be discarded here. 216 */ 217 static void 218 tcp_usr_detach(struct socket *so) 219 { 220 struct inpcb *inp; 221 222 inp = sotoinpcb(so); 223 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 224 INP_INFO_WLOCK(&V_tcbinfo); 225 INP_WLOCK(inp); 226 KASSERT(inp->inp_socket != NULL, 227 ("tcp_usr_detach: inp_socket == NULL")); 228 tcp_detach(so, inp); 229 INP_INFO_WUNLOCK(&V_tcbinfo); 230 } 231 232 /* 233 * Give the socket an address. 234 */ 235 static int 236 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 237 { 238 int error = 0; 239 struct inpcb *inp; 240 struct tcpcb *tp = NULL; 241 struct sockaddr_in *sinp; 242 243 sinp = (struct sockaddr_in *)nam; 244 if (nam->sa_len != sizeof (*sinp)) 245 return (EINVAL); 246 /* 247 * Must check for multicast addresses and disallow binding 248 * to them. 249 */ 250 if (sinp->sin_family == AF_INET && 251 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 252 return (EAFNOSUPPORT); 253 254 TCPDEBUG0; 255 INP_INFO_WLOCK(&V_tcbinfo); 256 inp = sotoinpcb(so); 257 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 258 INP_WLOCK(inp); 259 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 260 error = EINVAL; 261 goto out; 262 } 263 tp = intotcpcb(inp); 264 TCPDEBUG1(); 265 error = in_pcbbind(inp, nam, td->td_ucred); 266 out: 267 TCPDEBUG2(PRU_BIND); 268 INP_WUNLOCK(inp); 269 INP_INFO_WUNLOCK(&V_tcbinfo); 270 271 return (error); 272 } 273 274 #ifdef INET6 275 static int 276 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 277 { 278 int error = 0; 279 struct inpcb *inp; 280 struct tcpcb *tp = NULL; 281 struct sockaddr_in6 *sin6p; 282 283 sin6p = (struct sockaddr_in6 *)nam; 284 if (nam->sa_len != sizeof (*sin6p)) 285 return (EINVAL); 286 /* 287 * Must check for multicast addresses and disallow binding 288 * to them. 289 */ 290 if (sin6p->sin6_family == AF_INET6 && 291 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 292 return (EAFNOSUPPORT); 293 294 TCPDEBUG0; 295 INP_INFO_WLOCK(&V_tcbinfo); 296 inp = sotoinpcb(so); 297 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 298 INP_WLOCK(inp); 299 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 300 error = EINVAL; 301 goto out; 302 } 303 tp = intotcpcb(inp); 304 TCPDEBUG1(); 305 inp->inp_vflag &= ~INP_IPV4; 306 inp->inp_vflag |= INP_IPV6; 307 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 308 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 309 inp->inp_vflag |= INP_IPV4; 310 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 311 struct sockaddr_in sin; 312 313 in6_sin6_2_sin(&sin, sin6p); 314 inp->inp_vflag |= INP_IPV4; 315 inp->inp_vflag &= ~INP_IPV6; 316 error = in_pcbbind(inp, (struct sockaddr *)&sin, 317 td->td_ucred); 318 goto out; 319 } 320 } 321 error = in6_pcbbind(inp, nam, td->td_ucred); 322 out: 323 TCPDEBUG2(PRU_BIND); 324 INP_WUNLOCK(inp); 325 INP_INFO_WUNLOCK(&V_tcbinfo); 326 return (error); 327 } 328 #endif /* INET6 */ 329 330 /* 331 * Prepare to accept connections. 332 */ 333 static int 334 tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 335 { 336 int error = 0; 337 struct inpcb *inp; 338 struct tcpcb *tp = NULL; 339 340 TCPDEBUG0; 341 INP_INFO_WLOCK(&V_tcbinfo); 342 inp = sotoinpcb(so); 343 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 344 INP_WLOCK(inp); 345 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 346 error = EINVAL; 347 goto out; 348 } 349 tp = intotcpcb(inp); 350 TCPDEBUG1(); 351 SOCK_LOCK(so); 352 error = solisten_proto_check(so); 353 if (error == 0 && inp->inp_lport == 0) 354 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 355 if (error == 0) { 356 tp->t_state = TCPS_LISTEN; 357 solisten_proto(so, backlog); 358 tcp_offload_listen_open(tp); 359 } 360 SOCK_UNLOCK(so); 361 362 out: 363 TCPDEBUG2(PRU_LISTEN); 364 INP_WUNLOCK(inp); 365 INP_INFO_WUNLOCK(&V_tcbinfo); 366 return (error); 367 } 368 369 #ifdef INET6 370 static int 371 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 372 { 373 int error = 0; 374 struct inpcb *inp; 375 struct tcpcb *tp = NULL; 376 377 TCPDEBUG0; 378 INP_INFO_WLOCK(&V_tcbinfo); 379 inp = sotoinpcb(so); 380 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 381 INP_WLOCK(inp); 382 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 383 error = EINVAL; 384 goto out; 385 } 386 tp = intotcpcb(inp); 387 TCPDEBUG1(); 388 SOCK_LOCK(so); 389 error = solisten_proto_check(so); 390 if (error == 0 && inp->inp_lport == 0) { 391 inp->inp_vflag &= ~INP_IPV4; 392 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 393 inp->inp_vflag |= INP_IPV4; 394 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 395 } 396 if (error == 0) { 397 tp->t_state = TCPS_LISTEN; 398 solisten_proto(so, backlog); 399 } 400 SOCK_UNLOCK(so); 401 402 out: 403 TCPDEBUG2(PRU_LISTEN); 404 INP_WUNLOCK(inp); 405 INP_INFO_WUNLOCK(&V_tcbinfo); 406 return (error); 407 } 408 #endif /* INET6 */ 409 410 /* 411 * Initiate connection to peer. 412 * Create a template for use in transmissions on this connection. 413 * Enter SYN_SENT state, and mark socket as connecting. 414 * Start keep-alive timer, and seed output sequence space. 415 * Send initial segment on connection. 416 */ 417 static int 418 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 419 { 420 int error = 0; 421 struct inpcb *inp; 422 struct tcpcb *tp = NULL; 423 struct sockaddr_in *sinp; 424 425 sinp = (struct sockaddr_in *)nam; 426 if (nam->sa_len != sizeof (*sinp)) 427 return (EINVAL); 428 /* 429 * Must disallow TCP ``connections'' to multicast addresses. 430 */ 431 if (sinp->sin_family == AF_INET 432 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 433 return (EAFNOSUPPORT); 434 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0) 435 return (error); 436 437 TCPDEBUG0; 438 INP_INFO_WLOCK(&V_tcbinfo); 439 inp = sotoinpcb(so); 440 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 441 INP_WLOCK(inp); 442 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 443 error = EINVAL; 444 goto out; 445 } 446 tp = intotcpcb(inp); 447 TCPDEBUG1(); 448 if ((error = tcp_connect(tp, nam, td)) != 0) 449 goto out; 450 error = tcp_output_connect(so, nam); 451 out: 452 TCPDEBUG2(PRU_CONNECT); 453 INP_WUNLOCK(inp); 454 INP_INFO_WUNLOCK(&V_tcbinfo); 455 return (error); 456 } 457 458 #ifdef INET6 459 static int 460 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 461 { 462 int error = 0; 463 struct inpcb *inp; 464 struct tcpcb *tp = NULL; 465 struct sockaddr_in6 *sin6p; 466 467 TCPDEBUG0; 468 469 sin6p = (struct sockaddr_in6 *)nam; 470 if (nam->sa_len != sizeof (*sin6p)) 471 return (EINVAL); 472 /* 473 * Must disallow TCP ``connections'' to multicast addresses. 474 */ 475 if (sin6p->sin6_family == AF_INET6 476 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 477 return (EAFNOSUPPORT); 478 479 INP_INFO_WLOCK(&V_tcbinfo); 480 inp = sotoinpcb(so); 481 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 482 INP_WLOCK(inp); 483 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 484 error = EINVAL; 485 goto out; 486 } 487 tp = intotcpcb(inp); 488 TCPDEBUG1(); 489 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 490 struct sockaddr_in sin; 491 492 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 493 error = EINVAL; 494 goto out; 495 } 496 497 in6_sin6_2_sin(&sin, sin6p); 498 inp->inp_vflag |= INP_IPV4; 499 inp->inp_vflag &= ~INP_IPV6; 500 if ((error = prison_remote_ip4(td->td_ucred, 501 &sin.sin_addr)) != 0) 502 goto out; 503 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 504 goto out; 505 error = tcp_output_connect(so, nam); 506 goto out; 507 } 508 inp->inp_vflag &= ~INP_IPV4; 509 inp->inp_vflag |= INP_IPV6; 510 inp->inp_inc.inc_flags |= INC_ISIPV6; 511 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0) 512 goto out; 513 if ((error = tcp6_connect(tp, nam, td)) != 0) 514 goto out; 515 error = tcp_output_connect(so, nam); 516 517 out: 518 TCPDEBUG2(PRU_CONNECT); 519 INP_WUNLOCK(inp); 520 INP_INFO_WUNLOCK(&V_tcbinfo); 521 return (error); 522 } 523 #endif /* INET6 */ 524 525 /* 526 * Initiate disconnect from peer. 527 * If connection never passed embryonic stage, just drop; 528 * else if don't need to let data drain, then can just drop anyways, 529 * else have to begin TCP shutdown process: mark socket disconnecting, 530 * drain unread data, state switch to reflect user close, and 531 * send segment (e.g. FIN) to peer. Socket will be really disconnected 532 * when peer sends FIN and acks ours. 533 * 534 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 535 */ 536 static int 537 tcp_usr_disconnect(struct socket *so) 538 { 539 struct inpcb *inp; 540 struct tcpcb *tp = NULL; 541 int error = 0; 542 543 TCPDEBUG0; 544 INP_INFO_WLOCK(&V_tcbinfo); 545 inp = sotoinpcb(so); 546 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 547 INP_WLOCK(inp); 548 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 549 error = ECONNRESET; 550 goto out; 551 } 552 tp = intotcpcb(inp); 553 TCPDEBUG1(); 554 tcp_disconnect(tp); 555 out: 556 TCPDEBUG2(PRU_DISCONNECT); 557 INP_WUNLOCK(inp); 558 INP_INFO_WUNLOCK(&V_tcbinfo); 559 return (error); 560 } 561 562 /* 563 * Accept a connection. Essentially all the work is 564 * done at higher levels; just return the address 565 * of the peer, storing through addr. 566 */ 567 static int 568 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 569 { 570 int error = 0; 571 struct inpcb *inp = NULL; 572 struct tcpcb *tp = NULL; 573 struct in_addr addr; 574 in_port_t port = 0; 575 TCPDEBUG0; 576 577 if (so->so_state & SS_ISDISCONNECTED) 578 return (ECONNABORTED); 579 580 inp = sotoinpcb(so); 581 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 582 INP_INFO_RLOCK(&V_tcbinfo); 583 INP_WLOCK(inp); 584 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 585 error = ECONNABORTED; 586 goto out; 587 } 588 tp = intotcpcb(inp); 589 TCPDEBUG1(); 590 591 /* 592 * We inline in_getpeeraddr and COMMON_END here, so that we can 593 * copy the data of interest and defer the malloc until after we 594 * release the lock. 595 */ 596 port = inp->inp_fport; 597 addr = inp->inp_faddr; 598 599 out: 600 TCPDEBUG2(PRU_ACCEPT); 601 INP_WUNLOCK(inp); 602 INP_INFO_RUNLOCK(&V_tcbinfo); 603 if (error == 0) 604 *nam = in_sockaddr(port, &addr); 605 return error; 606 } 607 608 #ifdef INET6 609 static int 610 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 611 { 612 struct inpcb *inp = NULL; 613 int error = 0; 614 struct tcpcb *tp = NULL; 615 struct in_addr addr; 616 struct in6_addr addr6; 617 in_port_t port = 0; 618 int v4 = 0; 619 TCPDEBUG0; 620 621 if (so->so_state & SS_ISDISCONNECTED) 622 return (ECONNABORTED); 623 624 inp = sotoinpcb(so); 625 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 626 INP_WLOCK(inp); 627 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 628 error = ECONNABORTED; 629 goto out; 630 } 631 tp = intotcpcb(inp); 632 TCPDEBUG1(); 633 634 /* 635 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 636 * copy the data of interest and defer the malloc until after we 637 * release the lock. 638 */ 639 if (inp->inp_vflag & INP_IPV4) { 640 v4 = 1; 641 port = inp->inp_fport; 642 addr = inp->inp_faddr; 643 } else { 644 port = inp->inp_fport; 645 addr6 = inp->in6p_faddr; 646 } 647 648 out: 649 TCPDEBUG2(PRU_ACCEPT); 650 INP_WUNLOCK(inp); 651 if (error == 0) { 652 if (v4) 653 *nam = in6_v4mapsin6_sockaddr(port, &addr); 654 else 655 *nam = in6_sockaddr(port, &addr6); 656 } 657 return error; 658 } 659 #endif /* INET6 */ 660 661 /* 662 * Mark the connection as being incapable of further output. 663 */ 664 static int 665 tcp_usr_shutdown(struct socket *so) 666 { 667 int error = 0; 668 struct inpcb *inp; 669 struct tcpcb *tp = NULL; 670 671 TCPDEBUG0; 672 INP_INFO_WLOCK(&V_tcbinfo); 673 inp = sotoinpcb(so); 674 KASSERT(inp != NULL, ("inp == NULL")); 675 INP_WLOCK(inp); 676 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 677 error = ECONNRESET; 678 goto out; 679 } 680 tp = intotcpcb(inp); 681 TCPDEBUG1(); 682 socantsendmore(so); 683 tcp_usrclosed(tp); 684 if (!(inp->inp_flags & INP_DROPPED)) 685 error = tcp_output_disconnect(tp); 686 687 out: 688 TCPDEBUG2(PRU_SHUTDOWN); 689 INP_WUNLOCK(inp); 690 INP_INFO_WUNLOCK(&V_tcbinfo); 691 692 return (error); 693 } 694 695 /* 696 * After a receive, possibly send window update to peer. 697 */ 698 static int 699 tcp_usr_rcvd(struct socket *so, int flags) 700 { 701 struct inpcb *inp; 702 struct tcpcb *tp = NULL; 703 int error = 0; 704 705 TCPDEBUG0; 706 inp = sotoinpcb(so); 707 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 708 INP_WLOCK(inp); 709 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 710 error = ECONNRESET; 711 goto out; 712 } 713 tp = intotcpcb(inp); 714 TCPDEBUG1(); 715 tcp_output_rcvd(tp); 716 717 out: 718 TCPDEBUG2(PRU_RCVD); 719 INP_WUNLOCK(inp); 720 return (error); 721 } 722 723 /* 724 * Do a send by putting data in output queue and updating urgent 725 * marker if URG set. Possibly send more data. Unlike the other 726 * pru_*() routines, the mbuf chains are our responsibility. We 727 * must either enqueue them or free them. The other pru_* routines 728 * generally are caller-frees. 729 */ 730 static int 731 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 732 struct sockaddr *nam, struct mbuf *control, struct thread *td) 733 { 734 int error = 0; 735 struct inpcb *inp; 736 struct tcpcb *tp = NULL; 737 int headlocked = 0; 738 #ifdef INET6 739 int isipv6; 740 #endif 741 TCPDEBUG0; 742 743 /* 744 * We require the pcbinfo lock in two cases: 745 * 746 * (1) An implied connect is taking place, which can result in 747 * binding IPs and ports and hence modification of the pcb hash 748 * chains. 749 * 750 * (2) PRUS_EOF is set, resulting in explicit close on the send. 751 */ 752 if ((nam != NULL) || (flags & PRUS_EOF)) { 753 INP_INFO_WLOCK(&V_tcbinfo); 754 headlocked = 1; 755 } 756 inp = sotoinpcb(so); 757 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 758 INP_WLOCK(inp); 759 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 760 if (control) 761 m_freem(control); 762 if (m) 763 m_freem(m); 764 error = ECONNRESET; 765 goto out; 766 } 767 #ifdef INET6 768 isipv6 = nam && nam->sa_family == AF_INET6; 769 #endif /* INET6 */ 770 tp = intotcpcb(inp); 771 TCPDEBUG1(); 772 if (control) { 773 /* TCP doesn't do control messages (rights, creds, etc) */ 774 if (control->m_len) { 775 m_freem(control); 776 if (m) 777 m_freem(m); 778 error = EINVAL; 779 goto out; 780 } 781 m_freem(control); /* empty control, just free it */ 782 } 783 if (!(flags & PRUS_OOB)) { 784 sbappendstream(&so->so_snd, m); 785 if (nam && tp->t_state < TCPS_SYN_SENT) { 786 /* 787 * Do implied connect if not yet connected, 788 * initialize window to default value, and 789 * initialize maxseg/maxopd using peer's cached 790 * MSS. 791 */ 792 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 793 #ifdef INET6 794 if (isipv6) 795 error = tcp6_connect(tp, nam, td); 796 else 797 #endif /* INET6 */ 798 error = tcp_connect(tp, nam, td); 799 if (error) 800 goto out; 801 tp->snd_wnd = TTCP_CLIENT_SND_WND; 802 tcp_mss(tp, -1); 803 } 804 if (flags & PRUS_EOF) { 805 /* 806 * Close the send side of the connection after 807 * the data is sent. 808 */ 809 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 810 socantsendmore(so); 811 tcp_usrclosed(tp); 812 } 813 if (headlocked) { 814 INP_INFO_WUNLOCK(&V_tcbinfo); 815 headlocked = 0; 816 } 817 if (!(inp->inp_flags & INP_DROPPED)) { 818 if (flags & PRUS_MORETOCOME) 819 tp->t_flags |= TF_MORETOCOME; 820 error = tcp_output_send(tp); 821 if (flags & PRUS_MORETOCOME) 822 tp->t_flags &= ~TF_MORETOCOME; 823 } 824 } else { 825 /* 826 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 827 */ 828 SOCKBUF_LOCK(&so->so_snd); 829 if (sbspace(&so->so_snd) < -512) { 830 SOCKBUF_UNLOCK(&so->so_snd); 831 m_freem(m); 832 error = ENOBUFS; 833 goto out; 834 } 835 /* 836 * According to RFC961 (Assigned Protocols), 837 * the urgent pointer points to the last octet 838 * of urgent data. We continue, however, 839 * to consider it to indicate the first octet 840 * of data past the urgent section. 841 * Otherwise, snd_up should be one lower. 842 */ 843 sbappendstream_locked(&so->so_snd, m); 844 SOCKBUF_UNLOCK(&so->so_snd); 845 if (nam && tp->t_state < TCPS_SYN_SENT) { 846 /* 847 * Do implied connect if not yet connected, 848 * initialize window to default value, and 849 * initialize maxseg/maxopd using peer's cached 850 * MSS. 851 */ 852 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 853 #ifdef INET6 854 if (isipv6) 855 error = tcp6_connect(tp, nam, td); 856 else 857 #endif /* INET6 */ 858 error = tcp_connect(tp, nam, td); 859 if (error) 860 goto out; 861 tp->snd_wnd = TTCP_CLIENT_SND_WND; 862 tcp_mss(tp, -1); 863 INP_INFO_WUNLOCK(&V_tcbinfo); 864 headlocked = 0; 865 } else if (nam) { 866 INP_INFO_WUNLOCK(&V_tcbinfo); 867 headlocked = 0; 868 } 869 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 870 tp->t_flags |= TF_FORCEDATA; 871 error = tcp_output_send(tp); 872 tp->t_flags &= ~TF_FORCEDATA; 873 } 874 out: 875 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 876 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 877 INP_WUNLOCK(inp); 878 if (headlocked) 879 INP_INFO_WUNLOCK(&V_tcbinfo); 880 return (error); 881 } 882 883 /* 884 * Abort the TCP. Drop the connection abruptly. 885 */ 886 static void 887 tcp_usr_abort(struct socket *so) 888 { 889 struct inpcb *inp; 890 struct tcpcb *tp = NULL; 891 TCPDEBUG0; 892 893 inp = sotoinpcb(so); 894 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 895 896 INP_INFO_WLOCK(&V_tcbinfo); 897 INP_WLOCK(inp); 898 KASSERT(inp->inp_socket != NULL, 899 ("tcp_usr_abort: inp_socket == NULL")); 900 901 /* 902 * If we still have full TCP state, and we're not dropped, drop. 903 */ 904 if (!(inp->inp_flags & INP_TIMEWAIT) && 905 !(inp->inp_flags & INP_DROPPED)) { 906 tp = intotcpcb(inp); 907 TCPDEBUG1(); 908 tcp_drop(tp, ECONNABORTED); 909 TCPDEBUG2(PRU_ABORT); 910 } 911 if (!(inp->inp_flags & INP_DROPPED)) { 912 SOCK_LOCK(so); 913 so->so_state |= SS_PROTOREF; 914 SOCK_UNLOCK(so); 915 inp->inp_flags |= INP_SOCKREF; 916 } 917 INP_WUNLOCK(inp); 918 INP_INFO_WUNLOCK(&V_tcbinfo); 919 } 920 921 /* 922 * TCP socket is closed. Start friendly disconnect. 923 */ 924 static void 925 tcp_usr_close(struct socket *so) 926 { 927 struct inpcb *inp; 928 struct tcpcb *tp = NULL; 929 TCPDEBUG0; 930 931 inp = sotoinpcb(so); 932 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 933 934 INP_INFO_WLOCK(&V_tcbinfo); 935 INP_WLOCK(inp); 936 KASSERT(inp->inp_socket != NULL, 937 ("tcp_usr_close: inp_socket == NULL")); 938 939 /* 940 * If we still have full TCP state, and we're not dropped, initiate 941 * a disconnect. 942 */ 943 if (!(inp->inp_flags & INP_TIMEWAIT) && 944 !(inp->inp_flags & INP_DROPPED)) { 945 tp = intotcpcb(inp); 946 TCPDEBUG1(); 947 tcp_disconnect(tp); 948 TCPDEBUG2(PRU_CLOSE); 949 } 950 if (!(inp->inp_flags & INP_DROPPED)) { 951 SOCK_LOCK(so); 952 so->so_state |= SS_PROTOREF; 953 SOCK_UNLOCK(so); 954 inp->inp_flags |= INP_SOCKREF; 955 } 956 INP_WUNLOCK(inp); 957 INP_INFO_WUNLOCK(&V_tcbinfo); 958 } 959 960 /* 961 * Receive out-of-band data. 962 */ 963 static int 964 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 965 { 966 int error = 0; 967 struct inpcb *inp; 968 struct tcpcb *tp = NULL; 969 970 TCPDEBUG0; 971 inp = sotoinpcb(so); 972 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 973 INP_WLOCK(inp); 974 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 975 error = ECONNRESET; 976 goto out; 977 } 978 tp = intotcpcb(inp); 979 TCPDEBUG1(); 980 if ((so->so_oobmark == 0 && 981 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 982 so->so_options & SO_OOBINLINE || 983 tp->t_oobflags & TCPOOB_HADDATA) { 984 error = EINVAL; 985 goto out; 986 } 987 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 988 error = EWOULDBLOCK; 989 goto out; 990 } 991 m->m_len = 1; 992 *mtod(m, caddr_t) = tp->t_iobc; 993 if ((flags & MSG_PEEK) == 0) 994 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 995 996 out: 997 TCPDEBUG2(PRU_RCVOOB); 998 INP_WUNLOCK(inp); 999 return (error); 1000 } 1001 1002 struct pr_usrreqs tcp_usrreqs = { 1003 .pru_abort = tcp_usr_abort, 1004 .pru_accept = tcp_usr_accept, 1005 .pru_attach = tcp_usr_attach, 1006 .pru_bind = tcp_usr_bind, 1007 .pru_connect = tcp_usr_connect, 1008 .pru_control = in_control, 1009 .pru_detach = tcp_usr_detach, 1010 .pru_disconnect = tcp_usr_disconnect, 1011 .pru_listen = tcp_usr_listen, 1012 .pru_peeraddr = in_getpeeraddr, 1013 .pru_rcvd = tcp_usr_rcvd, 1014 .pru_rcvoob = tcp_usr_rcvoob, 1015 .pru_send = tcp_usr_send, 1016 .pru_shutdown = tcp_usr_shutdown, 1017 .pru_sockaddr = in_getsockaddr, 1018 .pru_sosetlabel = in_pcbsosetlabel, 1019 .pru_close = tcp_usr_close, 1020 }; 1021 1022 #ifdef INET6 1023 struct pr_usrreqs tcp6_usrreqs = { 1024 .pru_abort = tcp_usr_abort, 1025 .pru_accept = tcp6_usr_accept, 1026 .pru_attach = tcp_usr_attach, 1027 .pru_bind = tcp6_usr_bind, 1028 .pru_connect = tcp6_usr_connect, 1029 .pru_control = in6_control, 1030 .pru_detach = tcp_usr_detach, 1031 .pru_disconnect = tcp_usr_disconnect, 1032 .pru_listen = tcp6_usr_listen, 1033 .pru_peeraddr = in6_mapped_peeraddr, 1034 .pru_rcvd = tcp_usr_rcvd, 1035 .pru_rcvoob = tcp_usr_rcvoob, 1036 .pru_send = tcp_usr_send, 1037 .pru_shutdown = tcp_usr_shutdown, 1038 .pru_sockaddr = in6_mapped_sockaddr, 1039 .pru_sosetlabel = in_pcbsosetlabel, 1040 .pru_close = tcp_usr_close, 1041 }; 1042 #endif /* INET6 */ 1043 1044 /* 1045 * Common subroutine to open a TCP connection to remote host specified 1046 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 1047 * port number if needed. Call in_pcbconnect_setup to do the routing and 1048 * to choose a local host address (interface). If there is an existing 1049 * incarnation of the same connection in TIME-WAIT state and if the remote 1050 * host was sending CC options and if the connection duration was < MSL, then 1051 * truncate the previous TIME-WAIT state and proceed. 1052 * Initialize connection parameters and enter SYN-SENT state. 1053 */ 1054 static int 1055 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1056 { 1057 struct inpcb *inp = tp->t_inpcb, *oinp; 1058 struct socket *so = inp->inp_socket; 1059 struct in_addr laddr; 1060 u_short lport; 1061 int error; 1062 1063 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1064 INP_WLOCK_ASSERT(inp); 1065 1066 if (inp->inp_lport == 0) { 1067 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1068 if (error) 1069 return error; 1070 } 1071 1072 /* 1073 * Cannot simply call in_pcbconnect, because there might be an 1074 * earlier incarnation of this same connection still in 1075 * TIME_WAIT state, creating an ADDRINUSE error. 1076 */ 1077 laddr = inp->inp_laddr; 1078 lport = inp->inp_lport; 1079 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1080 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 1081 if (error && oinp == NULL) 1082 return error; 1083 if (oinp) 1084 return EADDRINUSE; 1085 inp->inp_laddr = laddr; 1086 in_pcbrehash(inp); 1087 1088 /* 1089 * Compute window scaling to request: 1090 * Scale to fit into sweet spot. See tcp_syncache.c. 1091 * XXX: This should move to tcp_output(). 1092 */ 1093 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1094 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1095 tp->request_r_scale++; 1096 1097 soisconnecting(so); 1098 TCPSTAT_INC(tcps_connattempt); 1099 tp->t_state = TCPS_SYN_SENT; 1100 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1101 tp->iss = tcp_new_isn(tp); 1102 tp->t_bw_rtseq = tp->iss; 1103 tcp_sendseqinit(tp); 1104 1105 return 0; 1106 } 1107 1108 #ifdef INET6 1109 static int 1110 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) 1111 { 1112 struct inpcb *inp = tp->t_inpcb, *oinp; 1113 struct socket *so = inp->inp_socket; 1114 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1115 struct in6_addr addr6; 1116 int error; 1117 1118 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1119 INP_WLOCK_ASSERT(inp); 1120 1121 if (inp->inp_lport == 0) { 1122 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1123 if (error) 1124 return error; 1125 } 1126 1127 /* 1128 * Cannot simply call in_pcbconnect, because there might be an 1129 * earlier incarnation of this same connection still in 1130 * TIME_WAIT state, creating an ADDRINUSE error. 1131 * in6_pcbladdr() also handles scope zone IDs. 1132 */ 1133 error = in6_pcbladdr(inp, nam, &addr6); 1134 if (error) 1135 return error; 1136 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1137 &sin6->sin6_addr, sin6->sin6_port, 1138 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 1139 ? &addr6 1140 : &inp->in6p_laddr, 1141 inp->inp_lport, 0, NULL); 1142 if (oinp) 1143 return EADDRINUSE; 1144 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1145 inp->in6p_laddr = addr6; 1146 inp->in6p_faddr = sin6->sin6_addr; 1147 inp->inp_fport = sin6->sin6_port; 1148 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 1149 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; 1150 if (inp->inp_flags & IN6P_AUTOFLOWLABEL) 1151 inp->inp_flow |= 1152 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1153 in_pcbrehash(inp); 1154 1155 /* Compute window scaling to request. */ 1156 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1157 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 1158 tp->request_r_scale++; 1159 1160 soisconnecting(so); 1161 TCPSTAT_INC(tcps_connattempt); 1162 tp->t_state = TCPS_SYN_SENT; 1163 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit); 1164 tp->iss = tcp_new_isn(tp); 1165 tp->t_bw_rtseq = tp->iss; 1166 tcp_sendseqinit(tp); 1167 1168 return 0; 1169 } 1170 #endif /* INET6 */ 1171 1172 /* 1173 * Export TCP internal state information via a struct tcp_info, based on the 1174 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1175 * (TCP state machine, etc). We export all information using FreeBSD-native 1176 * constants -- for example, the numeric values for tcpi_state will differ 1177 * from Linux. 1178 */ 1179 static void 1180 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti) 1181 { 1182 1183 INP_WLOCK_ASSERT(tp->t_inpcb); 1184 bzero(ti, sizeof(*ti)); 1185 1186 ti->tcpi_state = tp->t_state; 1187 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1188 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1189 if (tp->t_flags & TF_SACK_PERMIT) 1190 ti->tcpi_options |= TCPI_OPT_SACK; 1191 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1192 ti->tcpi_options |= TCPI_OPT_WSCALE; 1193 ti->tcpi_snd_wscale = tp->snd_scale; 1194 ti->tcpi_rcv_wscale = tp->rcv_scale; 1195 } 1196 1197 ti->tcpi_rto = tp->t_rxtcur * tick; 1198 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick; 1199 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT; 1200 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT; 1201 1202 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1203 ti->tcpi_snd_cwnd = tp->snd_cwnd; 1204 1205 /* 1206 * FreeBSD-specific extension fields for tcp_info. 1207 */ 1208 ti->tcpi_rcv_space = tp->rcv_wnd; 1209 ti->tcpi_rcv_nxt = tp->rcv_nxt; 1210 ti->tcpi_snd_wnd = tp->snd_wnd; 1211 ti->tcpi_snd_bwnd = tp->snd_bwnd; 1212 ti->tcpi_snd_nxt = tp->snd_nxt; 1213 ti->tcpi_snd_mss = tp->t_maxseg; 1214 ti->tcpi_rcv_mss = tp->t_maxseg; 1215 if (tp->t_flags & TF_TOE) 1216 ti->tcpi_options |= TCPI_OPT_TOE; 1217 } 1218 1219 /* 1220 * tcp_ctloutput() must drop the inpcb lock before performing copyin on 1221 * socket option arguments. When it re-acquires the lock after the copy, it 1222 * has to revalidate that the connection is still valid for the socket 1223 * option. 1224 */ 1225 #define INP_WLOCK_RECHECK(inp) do { \ 1226 INP_WLOCK(inp); \ 1227 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \ 1228 INP_WUNLOCK(inp); \ 1229 return (ECONNRESET); \ 1230 } \ 1231 tp = intotcpcb(inp); \ 1232 } while(0) 1233 1234 int 1235 tcp_ctloutput(struct socket *so, struct sockopt *sopt) 1236 { 1237 int error, opt, optval; 1238 struct inpcb *inp; 1239 struct tcpcb *tp; 1240 struct tcp_info ti; 1241 1242 error = 0; 1243 inp = sotoinpcb(so); 1244 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1245 INP_WLOCK(inp); 1246 if (sopt->sopt_level != IPPROTO_TCP) { 1247 #ifdef INET6 1248 if (inp->inp_vflag & INP_IPV6PROTO) { 1249 INP_WUNLOCK(inp); 1250 error = ip6_ctloutput(so, sopt); 1251 } else { 1252 #endif /* INET6 */ 1253 INP_WUNLOCK(inp); 1254 error = ip_ctloutput(so, sopt); 1255 #ifdef INET6 1256 } 1257 #endif 1258 return (error); 1259 } 1260 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 1261 INP_WUNLOCK(inp); 1262 return (ECONNRESET); 1263 } 1264 1265 switch (sopt->sopt_dir) { 1266 case SOPT_SET: 1267 switch (sopt->sopt_name) { 1268 #ifdef TCP_SIGNATURE 1269 case TCP_MD5SIG: 1270 INP_WUNLOCK(inp); 1271 error = sooptcopyin(sopt, &optval, sizeof optval, 1272 sizeof optval); 1273 if (error) 1274 return (error); 1275 1276 INP_WLOCK_RECHECK(inp); 1277 if (optval > 0) 1278 tp->t_flags |= TF_SIGNATURE; 1279 else 1280 tp->t_flags &= ~TF_SIGNATURE; 1281 INP_WUNLOCK(inp); 1282 break; 1283 #endif /* TCP_SIGNATURE */ 1284 case TCP_NODELAY: 1285 case TCP_NOOPT: 1286 INP_WUNLOCK(inp); 1287 error = sooptcopyin(sopt, &optval, sizeof optval, 1288 sizeof optval); 1289 if (error) 1290 return (error); 1291 1292 INP_WLOCK_RECHECK(inp); 1293 switch (sopt->sopt_name) { 1294 case TCP_NODELAY: 1295 opt = TF_NODELAY; 1296 break; 1297 case TCP_NOOPT: 1298 opt = TF_NOOPT; 1299 break; 1300 default: 1301 opt = 0; /* dead code to fool gcc */ 1302 break; 1303 } 1304 1305 if (optval) 1306 tp->t_flags |= opt; 1307 else 1308 tp->t_flags &= ~opt; 1309 INP_WUNLOCK(inp); 1310 break; 1311 1312 case TCP_NOPUSH: 1313 INP_WUNLOCK(inp); 1314 error = sooptcopyin(sopt, &optval, sizeof optval, 1315 sizeof optval); 1316 if (error) 1317 return (error); 1318 1319 INP_WLOCK_RECHECK(inp); 1320 if (optval) 1321 tp->t_flags |= TF_NOPUSH; 1322 else { 1323 tp->t_flags &= ~TF_NOPUSH; 1324 error = tcp_output(tp); 1325 } 1326 INP_WUNLOCK(inp); 1327 break; 1328 1329 case TCP_MAXSEG: 1330 INP_WUNLOCK(inp); 1331 error = sooptcopyin(sopt, &optval, sizeof optval, 1332 sizeof optval); 1333 if (error) 1334 return (error); 1335 1336 INP_WLOCK_RECHECK(inp); 1337 if (optval > 0 && optval <= tp->t_maxseg && 1338 optval + 40 >= V_tcp_minmss) 1339 tp->t_maxseg = optval; 1340 else 1341 error = EINVAL; 1342 INP_WUNLOCK(inp); 1343 break; 1344 1345 case TCP_INFO: 1346 INP_WUNLOCK(inp); 1347 error = EINVAL; 1348 break; 1349 1350 default: 1351 INP_WUNLOCK(inp); 1352 error = ENOPROTOOPT; 1353 break; 1354 } 1355 break; 1356 1357 case SOPT_GET: 1358 tp = intotcpcb(inp); 1359 switch (sopt->sopt_name) { 1360 #ifdef TCP_SIGNATURE 1361 case TCP_MD5SIG: 1362 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1363 INP_WUNLOCK(inp); 1364 error = sooptcopyout(sopt, &optval, sizeof optval); 1365 break; 1366 #endif 1367 1368 case TCP_NODELAY: 1369 optval = tp->t_flags & TF_NODELAY; 1370 INP_WUNLOCK(inp); 1371 error = sooptcopyout(sopt, &optval, sizeof optval); 1372 break; 1373 case TCP_MAXSEG: 1374 optval = tp->t_maxseg; 1375 INP_WUNLOCK(inp); 1376 error = sooptcopyout(sopt, &optval, sizeof optval); 1377 break; 1378 case TCP_NOOPT: 1379 optval = tp->t_flags & TF_NOOPT; 1380 INP_WUNLOCK(inp); 1381 error = sooptcopyout(sopt, &optval, sizeof optval); 1382 break; 1383 case TCP_NOPUSH: 1384 optval = tp->t_flags & TF_NOPUSH; 1385 INP_WUNLOCK(inp); 1386 error = sooptcopyout(sopt, &optval, sizeof optval); 1387 break; 1388 case TCP_INFO: 1389 tcp_fill_info(tp, &ti); 1390 INP_WUNLOCK(inp); 1391 error = sooptcopyout(sopt, &ti, sizeof ti); 1392 break; 1393 default: 1394 INP_WUNLOCK(inp); 1395 error = ENOPROTOOPT; 1396 break; 1397 } 1398 break; 1399 } 1400 return (error); 1401 } 1402 #undef INP_WLOCK_RECHECK 1403 1404 /* 1405 * tcp_sendspace and tcp_recvspace are the default send and receive window 1406 * sizes, respectively. These are obsolescent (this information should 1407 * be set by the route). 1408 */ 1409 u_long tcp_sendspace = 1024*32; 1410 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1411 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1412 u_long tcp_recvspace = 1024*64; 1413 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1414 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1415 1416 /* 1417 * Attach TCP protocol to socket, allocating 1418 * internet protocol control block, tcp control block, 1419 * bufer space, and entering LISTEN state if to accept connections. 1420 */ 1421 static int 1422 tcp_attach(struct socket *so) 1423 { 1424 struct tcpcb *tp; 1425 struct inpcb *inp; 1426 int error; 1427 1428 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1429 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1430 if (error) 1431 return (error); 1432 } 1433 so->so_rcv.sb_flags |= SB_AUTOSIZE; 1434 so->so_snd.sb_flags |= SB_AUTOSIZE; 1435 INP_INFO_WLOCK(&V_tcbinfo); 1436 error = in_pcballoc(so, &V_tcbinfo); 1437 if (error) { 1438 INP_INFO_WUNLOCK(&V_tcbinfo); 1439 return (error); 1440 } 1441 inp = sotoinpcb(so); 1442 #ifdef INET6 1443 if (inp->inp_vflag & INP_IPV6PROTO) { 1444 inp->inp_vflag |= INP_IPV6; 1445 inp->in6p_hops = -1; /* use kernel default */ 1446 } 1447 else 1448 #endif 1449 inp->inp_vflag |= INP_IPV4; 1450 tp = tcp_newtcpcb(inp); 1451 if (tp == NULL) { 1452 in_pcbdetach(inp); 1453 in_pcbfree(inp); 1454 INP_INFO_WUNLOCK(&V_tcbinfo); 1455 return (ENOBUFS); 1456 } 1457 tp->t_state = TCPS_CLOSED; 1458 INP_WUNLOCK(inp); 1459 INP_INFO_WUNLOCK(&V_tcbinfo); 1460 return (0); 1461 } 1462 1463 /* 1464 * Initiate (or continue) disconnect. 1465 * If embryonic state, just send reset (once). 1466 * If in ``let data drain'' option and linger null, just drop. 1467 * Otherwise (hard), mark socket disconnecting and drop 1468 * current input data; switch states based on user close, and 1469 * send segment to peer (with FIN). 1470 */ 1471 static void 1472 tcp_disconnect(struct tcpcb *tp) 1473 { 1474 struct inpcb *inp = tp->t_inpcb; 1475 struct socket *so = inp->inp_socket; 1476 1477 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1478 INP_WLOCK_ASSERT(inp); 1479 1480 /* 1481 * Neither tcp_close() nor tcp_drop() should return NULL, as the 1482 * socket is still open. 1483 */ 1484 if (tp->t_state < TCPS_ESTABLISHED) { 1485 tp = tcp_close(tp); 1486 KASSERT(tp != NULL, 1487 ("tcp_disconnect: tcp_close() returned NULL")); 1488 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1489 tp = tcp_drop(tp, 0); 1490 KASSERT(tp != NULL, 1491 ("tcp_disconnect: tcp_drop() returned NULL")); 1492 } else { 1493 soisdisconnecting(so); 1494 sbflush(&so->so_rcv); 1495 tcp_usrclosed(tp); 1496 if (!(inp->inp_flags & INP_DROPPED)) 1497 tcp_output_disconnect(tp); 1498 } 1499 } 1500 1501 /* 1502 * User issued close, and wish to trail through shutdown states: 1503 * if never received SYN, just forget it. If got a SYN from peer, 1504 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1505 * If already got a FIN from peer, then almost done; go to LAST_ACK 1506 * state. In all other cases, have already sent FIN to peer (e.g. 1507 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1508 * for peer to send FIN or not respond to keep-alives, etc. 1509 * We can let the user exit from the close as soon as the FIN is acked. 1510 */ 1511 static void 1512 tcp_usrclosed(struct tcpcb *tp) 1513 { 1514 1515 INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1516 INP_WLOCK_ASSERT(tp->t_inpcb); 1517 1518 switch (tp->t_state) { 1519 case TCPS_LISTEN: 1520 tcp_offload_listen_close(tp); 1521 /* FALLTHROUGH */ 1522 case TCPS_CLOSED: 1523 tp->t_state = TCPS_CLOSED; 1524 tp = tcp_close(tp); 1525 /* 1526 * tcp_close() should never return NULL here as the socket is 1527 * still open. 1528 */ 1529 KASSERT(tp != NULL, 1530 ("tcp_usrclosed: tcp_close() returned NULL")); 1531 break; 1532 1533 case TCPS_SYN_SENT: 1534 case TCPS_SYN_RECEIVED: 1535 tp->t_flags |= TF_NEEDFIN; 1536 break; 1537 1538 case TCPS_ESTABLISHED: 1539 tp->t_state = TCPS_FIN_WAIT_1; 1540 break; 1541 1542 case TCPS_CLOSE_WAIT: 1543 tp->t_state = TCPS_LAST_ACK; 1544 break; 1545 } 1546 if (tp->t_state >= TCPS_FIN_WAIT_2) { 1547 soisdisconnected(tp->t_inpcb->inp_socket); 1548 /* Prevent the connection hanging in FIN_WAIT_2 forever. */ 1549 if (tp->t_state == TCPS_FIN_WAIT_2) { 1550 int timeout; 1551 1552 timeout = (tcp_fast_finwait2_recycle) ? 1553 tcp_finwait2_timeout : tcp_maxidle; 1554 tcp_timer_activate(tp, TT_2MSL, timeout); 1555 } 1556 } 1557 } 1558 1559 #ifdef DDB 1560 static void 1561 db_print_indent(int indent) 1562 { 1563 int i; 1564 1565 for (i = 0; i < indent; i++) 1566 db_printf(" "); 1567 } 1568 1569 static void 1570 db_print_tstate(int t_state) 1571 { 1572 1573 switch (t_state) { 1574 case TCPS_CLOSED: 1575 db_printf("TCPS_CLOSED"); 1576 return; 1577 1578 case TCPS_LISTEN: 1579 db_printf("TCPS_LISTEN"); 1580 return; 1581 1582 case TCPS_SYN_SENT: 1583 db_printf("TCPS_SYN_SENT"); 1584 return; 1585 1586 case TCPS_SYN_RECEIVED: 1587 db_printf("TCPS_SYN_RECEIVED"); 1588 return; 1589 1590 case TCPS_ESTABLISHED: 1591 db_printf("TCPS_ESTABLISHED"); 1592 return; 1593 1594 case TCPS_CLOSE_WAIT: 1595 db_printf("TCPS_CLOSE_WAIT"); 1596 return; 1597 1598 case TCPS_FIN_WAIT_1: 1599 db_printf("TCPS_FIN_WAIT_1"); 1600 return; 1601 1602 case TCPS_CLOSING: 1603 db_printf("TCPS_CLOSING"); 1604 return; 1605 1606 case TCPS_LAST_ACK: 1607 db_printf("TCPS_LAST_ACK"); 1608 return; 1609 1610 case TCPS_FIN_WAIT_2: 1611 db_printf("TCPS_FIN_WAIT_2"); 1612 return; 1613 1614 case TCPS_TIME_WAIT: 1615 db_printf("TCPS_TIME_WAIT"); 1616 return; 1617 1618 default: 1619 db_printf("unknown"); 1620 return; 1621 } 1622 } 1623 1624 static void 1625 db_print_tflags(u_int t_flags) 1626 { 1627 int comma; 1628 1629 comma = 0; 1630 if (t_flags & TF_ACKNOW) { 1631 db_printf("%sTF_ACKNOW", comma ? ", " : ""); 1632 comma = 1; 1633 } 1634 if (t_flags & TF_DELACK) { 1635 db_printf("%sTF_DELACK", comma ? ", " : ""); 1636 comma = 1; 1637 } 1638 if (t_flags & TF_NODELAY) { 1639 db_printf("%sTF_NODELAY", comma ? ", " : ""); 1640 comma = 1; 1641 } 1642 if (t_flags & TF_NOOPT) { 1643 db_printf("%sTF_NOOPT", comma ? ", " : ""); 1644 comma = 1; 1645 } 1646 if (t_flags & TF_SENTFIN) { 1647 db_printf("%sTF_SENTFIN", comma ? ", " : ""); 1648 comma = 1; 1649 } 1650 if (t_flags & TF_REQ_SCALE) { 1651 db_printf("%sTF_REQ_SCALE", comma ? ", " : ""); 1652 comma = 1; 1653 } 1654 if (t_flags & TF_RCVD_SCALE) { 1655 db_printf("%sTF_RECVD_SCALE", comma ? ", " : ""); 1656 comma = 1; 1657 } 1658 if (t_flags & TF_REQ_TSTMP) { 1659 db_printf("%sTF_REQ_TSTMP", comma ? ", " : ""); 1660 comma = 1; 1661 } 1662 if (t_flags & TF_RCVD_TSTMP) { 1663 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : ""); 1664 comma = 1; 1665 } 1666 if (t_flags & TF_SACK_PERMIT) { 1667 db_printf("%sTF_SACK_PERMIT", comma ? ", " : ""); 1668 comma = 1; 1669 } 1670 if (t_flags & TF_NEEDSYN) { 1671 db_printf("%sTF_NEEDSYN", comma ? ", " : ""); 1672 comma = 1; 1673 } 1674 if (t_flags & TF_NEEDFIN) { 1675 db_printf("%sTF_NEEDFIN", comma ? ", " : ""); 1676 comma = 1; 1677 } 1678 if (t_flags & TF_NOPUSH) { 1679 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1680 comma = 1; 1681 } 1682 if (t_flags & TF_NOPUSH) { 1683 db_printf("%sTF_NOPUSH", comma ? ", " : ""); 1684 comma = 1; 1685 } 1686 if (t_flags & TF_MORETOCOME) { 1687 db_printf("%sTF_MORETOCOME", comma ? ", " : ""); 1688 comma = 1; 1689 } 1690 if (t_flags & TF_LQ_OVERFLOW) { 1691 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : ""); 1692 comma = 1; 1693 } 1694 if (t_flags & TF_LASTIDLE) { 1695 db_printf("%sTF_LASTIDLE", comma ? ", " : ""); 1696 comma = 1; 1697 } 1698 if (t_flags & TF_RXWIN0SENT) { 1699 db_printf("%sTF_RXWIN0SENT", comma ? ", " : ""); 1700 comma = 1; 1701 } 1702 if (t_flags & TF_FASTRECOVERY) { 1703 db_printf("%sTF_FASTRECOVERY", comma ? ", " : ""); 1704 comma = 1; 1705 } 1706 if (t_flags & TF_WASFRECOVERY) { 1707 db_printf("%sTF_WASFRECOVERY", comma ? ", " : ""); 1708 comma = 1; 1709 } 1710 if (t_flags & TF_SIGNATURE) { 1711 db_printf("%sTF_SIGNATURE", comma ? ", " : ""); 1712 comma = 1; 1713 } 1714 if (t_flags & TF_FORCEDATA) { 1715 db_printf("%sTF_FORCEDATA", comma ? ", " : ""); 1716 comma = 1; 1717 } 1718 if (t_flags & TF_TSO) { 1719 db_printf("%sTF_TSO", comma ? ", " : ""); 1720 comma = 1; 1721 } 1722 if (t_flags & TF_ECN_PERMIT) { 1723 db_printf("%sTF_ECN_PERMIT", comma ? ", " : ""); 1724 comma = 1; 1725 } 1726 } 1727 1728 static void 1729 db_print_toobflags(char t_oobflags) 1730 { 1731 int comma; 1732 1733 comma = 0; 1734 if (t_oobflags & TCPOOB_HAVEDATA) { 1735 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : ""); 1736 comma = 1; 1737 } 1738 if (t_oobflags & TCPOOB_HADDATA) { 1739 db_printf("%sTCPOOB_HADDATA", comma ? ", " : ""); 1740 comma = 1; 1741 } 1742 } 1743 1744 static void 1745 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent) 1746 { 1747 1748 db_print_indent(indent); 1749 db_printf("%s at %p\n", name, tp); 1750 1751 indent += 2; 1752 1753 db_print_indent(indent); 1754 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n", 1755 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks); 1756 1757 db_print_indent(indent); 1758 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n", 1759 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep); 1760 1761 db_print_indent(indent); 1762 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl, 1763 &tp->t_timers->tt_delack, tp->t_inpcb); 1764 1765 db_print_indent(indent); 1766 db_printf("t_state: %d (", tp->t_state); 1767 db_print_tstate(tp->t_state); 1768 db_printf(")\n"); 1769 1770 db_print_indent(indent); 1771 db_printf("t_flags: 0x%x (", tp->t_flags); 1772 db_print_tflags(tp->t_flags); 1773 db_printf(")\n"); 1774 1775 db_print_indent(indent); 1776 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n", 1777 tp->snd_una, tp->snd_max, tp->snd_nxt); 1778 1779 db_print_indent(indent); 1780 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n", 1781 tp->snd_up, tp->snd_wl1, tp->snd_wl2); 1782 1783 db_print_indent(indent); 1784 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n", 1785 tp->iss, tp->irs, tp->rcv_nxt); 1786 1787 db_print_indent(indent); 1788 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n", 1789 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up); 1790 1791 db_print_indent(indent); 1792 db_printf("snd_wnd: %lu snd_cwnd: %lu snd_bwnd: %lu\n", 1793 tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd); 1794 1795 db_print_indent(indent); 1796 db_printf("snd_ssthresh: %lu snd_bandwidth: %lu snd_recover: " 1797 "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth, 1798 tp->snd_recover); 1799 1800 db_print_indent(indent); 1801 db_printf("t_maxopd: %u t_rcvtime: %u t_startime: %u\n", 1802 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime); 1803 1804 db_print_indent(indent); 1805 db_printf("t_rttime: %u t_rtsq: 0x%08x t_bw_rtttime: %u\n", 1806 tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime); 1807 1808 db_print_indent(indent); 1809 db_printf("t_bw_rtseq: 0x%08x t_rxtcur: %d t_maxseg: %u " 1810 "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg, 1811 tp->t_srtt); 1812 1813 db_print_indent(indent); 1814 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u " 1815 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin, 1816 tp->t_rttbest); 1817 1818 db_print_indent(indent); 1819 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n", 1820 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror); 1821 1822 db_print_indent(indent); 1823 db_printf("t_oobflags: 0x%x (", tp->t_oobflags); 1824 db_print_toobflags(tp->t_oobflags); 1825 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc); 1826 1827 db_print_indent(indent); 1828 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n", 1829 tp->snd_scale, tp->rcv_scale, tp->request_r_scale); 1830 1831 db_print_indent(indent); 1832 db_printf("ts_recent: %u ts_recent_age: %u\n", 1833 tp->ts_recent, tp->ts_recent_age); 1834 1835 db_print_indent(indent); 1836 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: " 1837 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev); 1838 1839 db_print_indent(indent); 1840 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x " 1841 "t_badrxtwin: %u\n", tp->snd_ssthresh_prev, 1842 tp->snd_recover_prev, tp->t_badrxtwin); 1843 1844 db_print_indent(indent); 1845 db_printf("snd_numholes: %d snd_holes first: %p\n", 1846 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes)); 1847 1848 db_print_indent(indent); 1849 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: " 1850 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata); 1851 1852 /* Skip sackblks, sackhint. */ 1853 1854 db_print_indent(indent); 1855 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n", 1856 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt); 1857 } 1858 1859 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb) 1860 { 1861 struct tcpcb *tp; 1862 1863 if (!have_addr) { 1864 db_printf("usage: show tcpcb <addr>\n"); 1865 return; 1866 } 1867 tp = (struct tcpcb *)addr; 1868 1869 db_print_tcpcb(tp, "tcpcb", 0); 1870 } 1871 #endif 1872