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