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