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