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