tcp_usrreq.c revision 159218
1/*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. 4 * Copyright (c) 2006 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 159218 2006-06-04 09:31:34Z rwatson $ 33 */ 34 35#include "opt_inet.h" 36#include "opt_inet6.h" 37#include "opt_tcpdebug.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/malloc.h> 42#include <sys/kernel.h> 43#include <sys/sysctl.h> 44#include <sys/mbuf.h> 45#ifdef INET6 46#include <sys/domain.h> 47#endif /* INET6 */ 48#include <sys/socket.h> 49#include <sys/socketvar.h> 50#include <sys/protosw.h> 51#include <sys/proc.h> 52#include <sys/jail.h> 53 54#include <net/if.h> 55#include <net/route.h> 56 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#ifdef INET6 60#include <netinet/ip6.h> 61#endif 62#include <netinet/in_pcb.h> 63#ifdef INET6 64#include <netinet6/in6_pcb.h> 65#endif 66#include <netinet/in_var.h> 67#include <netinet/ip_var.h> 68#ifdef INET6 69#include <netinet6/ip6_var.h> 70#include <netinet6/scope6_var.h> 71#endif 72#include <netinet/tcp.h> 73#include <netinet/tcp_fsm.h> 74#include <netinet/tcp_seq.h> 75#include <netinet/tcp_timer.h> 76#include <netinet/tcp_var.h> 77#include <netinet/tcpip.h> 78#ifdef TCPDEBUG 79#include <netinet/tcp_debug.h> 80#endif 81 82/* 83 * TCP protocol interface to socket abstraction. 84 */ 85extern char *tcpstates[]; /* XXX ??? */ 86 87static int tcp_attach(struct socket *); 88static int tcp_connect(struct tcpcb *, struct sockaddr *, 89 struct thread *td); 90#ifdef INET6 91static int tcp6_connect(struct tcpcb *, struct sockaddr *, 92 struct thread *td); 93#endif /* INET6 */ 94static void tcp_disconnect(struct tcpcb *); 95static void tcp_usrclosed(struct tcpcb *); 96static void tcp_fill_info(struct tcpcb *, struct tcp_info *); 97 98#ifdef TCPDEBUG 99#define TCPDEBUG0 int ostate = 0 100#define TCPDEBUG1() ostate = tp ? tp->t_state : 0 101#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 102 tcp_trace(TA_USER, ostate, tp, 0, 0, req) 103#else 104#define TCPDEBUG0 105#define TCPDEBUG1() 106#define TCPDEBUG2(req) 107#endif 108 109/* 110 * TCP attaches to socket via pru_attach(), reserving space, 111 * and an internet control block. 112 */ 113static int 114tcp_usr_attach(struct socket *so, int proto, struct thread *td) 115{ 116 struct inpcb *inp; 117 struct tcpcb *tp = NULL; 118 int error; 119 TCPDEBUG0; 120 121 inp = sotoinpcb(so); 122 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL")); 123 TCPDEBUG1(); 124 125 error = tcp_attach(so); 126 if (error) 127 goto out; 128 129 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 130 so->so_linger = TCP_LINGERTIME; 131 132 inp = sotoinpcb(so); 133 tp = intotcpcb(inp); 134out: 135 TCPDEBUG2(PRU_ATTACH); 136 return error; 137} 138 139/* 140 * tcp_detach() releases any protocol state that can be reasonably released 141 * when a socket shutdown is requested, and is a shared code path for 142 * tcp_usr_detach() and tcp_usr_abort(), the two socket close entry points. 143 * 144 * Accepts pcbinfo, inpcb locked, will unlock the inpcb (if needed) on 145 * return. 146 */ 147static void 148tcp_detach(struct socket *so, struct inpcb *inp) 149{ 150 struct tcpcb *tp; 151#ifdef INET6 152 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 153#endif 154 155 INP_INFO_WLOCK_ASSERT(&tcbinfo); 156 INP_LOCK_ASSERT(inp); 157 158 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp")); 159 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so")); 160 161 if (inp->inp_vflag & INP_TIMEWAIT) { 162 if (inp->inp_vflag & INP_DROPPED) { 163 /* 164 * Connection was in time wait and has been dropped; 165 * the calling path is either via tcp_twclose(), or 166 * as a result of an eventual soclose() after 167 * tcp_twclose() has been called. In either case, 168 * tcp_twclose() has detached the tcptw from the 169 * inpcb, so we just detach and free the inpcb. 170 * 171 * XXXRW: Would it be cleaner to free the tcptw 172 * here? 173 */ 174#ifdef INET6 175 if (isipv6) { 176 in6_pcbdetach(inp); 177 in6_pcbfree(inp); 178 } else { 179#endif 180 in_pcbdetach(inp); 181 in_pcbfree(inp); 182#ifdef INET6 183 } 184#endif 185 } else { 186 /* 187 * Connection is in time wait and has not yet been 188 * dropped; allow the socket to be discarded, but 189 * need to keep inpcb until end of time wait. 190 */ 191#ifdef INET6 192 if (isipv6) 193 in6_pcbdetach(inp); 194 else 195#endif 196 in_pcbdetach(inp); 197 INP_UNLOCK(inp); 198 } 199 } else { 200 /* 201 * If not in timewait, there are two possible paths. First, 202 * the TCP connection is either embryonic or done, in which 203 * case we tear down all state. Second, it may still be 204 * active, in which case we acquire a reference to the socket 205 * and will free it later when TCP is done. 206 */ 207 tp = intotcpcb(inp); 208 if (inp->inp_vflag & INP_DROPPED || 209 tp->t_state < TCPS_SYN_SENT) { 210 tcp_discardcb(tp); 211#ifdef INET6 212 if (isipv6) { 213 in_pcbdetach(inp); 214 in_pcbfree(inp); 215 } else { 216#endif 217 in_pcbdetach(inp); 218 in_pcbfree(inp); 219#ifdef INET6 220 } 221#endif 222 } else { 223 SOCK_LOCK(so); 224 so->so_state |= SS_PROTOREF; 225 SOCK_UNLOCK(so); 226 inp->inp_vflag |= INP_SOCKREF; 227 INP_UNLOCK(inp); 228 } 229 } 230} 231 232/* 233 * pru_detach() detaches the TCP protocol from the socket. 234 * If the protocol state is non-embryonic, then can't 235 * do this directly: have to initiate a pru_disconnect(), 236 * which may finish later; embryonic TCB's can just 237 * be discarded here. 238 */ 239static void 240tcp_usr_detach(struct socket *so) 241{ 242 struct inpcb *inp; 243 struct tcpcb *tp; 244 TCPDEBUG0; 245 246 inp = sotoinpcb(so); 247 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL")); 248 INP_INFO_WLOCK(&tcbinfo); 249 INP_LOCK(inp); 250 KASSERT(inp->inp_socket != NULL, 251 ("tcp_usr_detach: inp_socket == NULL")); 252 TCPDEBUG1(); 253 254 /* 255 * First, if we still have full TCP state, and we're not dropped, 256 * initiate a disconnect. 257 */ 258 if (!(inp->inp_vflag & INP_TIMEWAIT) && 259 !(inp->inp_vflag & INP_DROPPED)) { 260 tp = intotcpcb(inp); 261 tcp_disconnect(tp); 262 } 263 tcp_detach(so, inp); 264 tp = NULL; 265 TCPDEBUG2(PRU_DETACH); 266 INP_INFO_WUNLOCK(&tcbinfo); 267} 268 269/* 270 * Give the socket an address. 271 */ 272static int 273tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 274{ 275 int error = 0; 276 struct inpcb *inp; 277 struct tcpcb *tp = NULL; 278 struct sockaddr_in *sinp; 279 280 sinp = (struct sockaddr_in *)nam; 281 if (nam->sa_len != sizeof (*sinp)) 282 return (EINVAL); 283 /* 284 * Must check for multicast addresses and disallow binding 285 * to them. 286 */ 287 if (sinp->sin_family == AF_INET && 288 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 289 return (EAFNOSUPPORT); 290 291 TCPDEBUG0; 292 INP_INFO_WLOCK(&tcbinfo); 293 inp = sotoinpcb(so); 294 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL")); 295 INP_LOCK(inp); 296 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 297 error = EINVAL; 298 goto out; 299 } 300 tp = intotcpcb(inp); 301 TCPDEBUG1(); 302 error = in_pcbbind(inp, nam, td->td_ucred); 303out: 304 TCPDEBUG2(PRU_BIND); 305 INP_UNLOCK(inp); 306 INP_INFO_WUNLOCK(&tcbinfo); 307 308 return (error); 309} 310 311#ifdef INET6 312static int 313tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 314{ 315 int error = 0; 316 struct inpcb *inp; 317 struct tcpcb *tp = NULL; 318 struct sockaddr_in6 *sin6p; 319 320 sin6p = (struct sockaddr_in6 *)nam; 321 if (nam->sa_len != sizeof (*sin6p)) 322 return (EINVAL); 323 /* 324 * Must check for multicast addresses and disallow binding 325 * to them. 326 */ 327 if (sin6p->sin6_family == AF_INET6 && 328 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 329 return (EAFNOSUPPORT); 330 331 TCPDEBUG0; 332 INP_INFO_WLOCK(&tcbinfo); 333 inp = sotoinpcb(so); 334 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL")); 335 INP_LOCK(inp); 336 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 337 error = EINVAL; 338 goto out; 339 } 340 tp = intotcpcb(inp); 341 TCPDEBUG1(); 342 inp->inp_vflag &= ~INP_IPV4; 343 inp->inp_vflag |= INP_IPV6; 344 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 345 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 346 inp->inp_vflag |= INP_IPV4; 347 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 348 struct sockaddr_in sin; 349 350 in6_sin6_2_sin(&sin, sin6p); 351 inp->inp_vflag |= INP_IPV4; 352 inp->inp_vflag &= ~INP_IPV6; 353 error = in_pcbbind(inp, (struct sockaddr *)&sin, 354 td->td_ucred); 355 goto out; 356 } 357 } 358 error = in6_pcbbind(inp, nam, td->td_ucred); 359out: 360 TCPDEBUG2(PRU_BIND); 361 INP_UNLOCK(inp); 362 INP_INFO_WUNLOCK(&tcbinfo); 363 return (error); 364} 365#endif /* INET6 */ 366 367/* 368 * Prepare to accept connections. 369 */ 370static int 371tcp_usr_listen(struct socket *so, int backlog, struct thread *td) 372{ 373 int error = 0; 374 struct inpcb *inp; 375 struct tcpcb *tp = NULL; 376 377 TCPDEBUG0; 378 INP_INFO_WLOCK(&tcbinfo); 379 inp = sotoinpcb(so); 380 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL")); 381 INP_LOCK(inp); 382 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 383 error = EINVAL; 384 goto out; 385 } 386 tp = intotcpcb(inp); 387 TCPDEBUG1(); 388 SOCK_LOCK(so); 389 error = solisten_proto_check(so); 390 if (error == 0 && inp->inp_lport == 0) 391 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 392 if (error == 0) { 393 tp->t_state = TCPS_LISTEN; 394 solisten_proto(so, backlog); 395 } 396 SOCK_UNLOCK(so); 397 398out: 399 TCPDEBUG2(PRU_LISTEN); 400 INP_UNLOCK(inp); 401 INP_INFO_WUNLOCK(&tcbinfo); 402 return (error); 403} 404 405#ifdef INET6 406static int 407tcp6_usr_listen(struct socket *so, int backlog, struct thread *td) 408{ 409 int error = 0; 410 struct inpcb *inp; 411 struct tcpcb *tp = NULL; 412 413 TCPDEBUG0; 414 INP_INFO_WLOCK(&tcbinfo); 415 inp = sotoinpcb(so); 416 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL")); 417 INP_LOCK(inp); 418 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 419 error = EINVAL; 420 goto out; 421 } 422 tp = intotcpcb(inp); 423 TCPDEBUG1(); 424 SOCK_LOCK(so); 425 error = solisten_proto_check(so); 426 if (error == 0 && inp->inp_lport == 0) { 427 inp->inp_vflag &= ~INP_IPV4; 428 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 429 inp->inp_vflag |= INP_IPV4; 430 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 431 } 432 if (error == 0) { 433 tp->t_state = TCPS_LISTEN; 434 solisten_proto(so, backlog); 435 } 436 SOCK_UNLOCK(so); 437 438out: 439 TCPDEBUG2(PRU_LISTEN); 440 INP_UNLOCK(inp); 441 INP_INFO_WUNLOCK(&tcbinfo); 442 return (error); 443} 444#endif /* INET6 */ 445 446/* 447 * Initiate connection to peer. 448 * Create a template for use in transmissions on this connection. 449 * Enter SYN_SENT state, and mark socket as connecting. 450 * Start keep-alive timer, and seed output sequence space. 451 * Send initial segment on connection. 452 */ 453static int 454tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 455{ 456 int error = 0; 457 struct inpcb *inp; 458 struct tcpcb *tp = NULL; 459 struct sockaddr_in *sinp; 460 461 sinp = (struct sockaddr_in *)nam; 462 if (nam->sa_len != sizeof (*sinp)) 463 return (EINVAL); 464 /* 465 * Must disallow TCP ``connections'' to multicast addresses. 466 */ 467 if (sinp->sin_family == AF_INET 468 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 469 return (EAFNOSUPPORT); 470 if (jailed(td->td_ucred)) 471 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 472 473 TCPDEBUG0; 474 INP_INFO_WLOCK(&tcbinfo); 475 inp = sotoinpcb(so); 476 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL")); 477 INP_LOCK(inp); 478 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 479 error = EINVAL; 480 goto out; 481 } 482 tp = intotcpcb(inp); 483 TCPDEBUG1(); 484 if ((error = tcp_connect(tp, nam, td)) != 0) 485 goto out; 486 error = tcp_output(tp); 487out: 488 TCPDEBUG2(PRU_CONNECT); 489 INP_UNLOCK(inp); 490 INP_INFO_WUNLOCK(&tcbinfo); 491 return (error); 492} 493 494#ifdef INET6 495static int 496tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 497{ 498 int error = 0; 499 struct inpcb *inp; 500 struct tcpcb *tp = NULL; 501 struct sockaddr_in6 *sin6p; 502 503 TCPDEBUG0; 504 505 sin6p = (struct sockaddr_in6 *)nam; 506 if (nam->sa_len != sizeof (*sin6p)) 507 return (EINVAL); 508 /* 509 * Must disallow TCP ``connections'' to multicast addresses. 510 */ 511 if (sin6p->sin6_family == AF_INET6 512 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 513 return (EAFNOSUPPORT); 514 515 INP_INFO_WLOCK(&tcbinfo); 516 inp = sotoinpcb(so); 517 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL")); 518 INP_LOCK(inp); 519 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 520 error = EINVAL; 521 goto out; 522 } 523 tp = intotcpcb(inp); 524 TCPDEBUG1(); 525 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 526 struct sockaddr_in sin; 527 528 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 529 error = EINVAL; 530 goto out; 531 } 532 533 in6_sin6_2_sin(&sin, sin6p); 534 inp->inp_vflag |= INP_IPV4; 535 inp->inp_vflag &= ~INP_IPV6; 536 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 537 goto out; 538 error = tcp_output(tp); 539 goto out; 540 } 541 inp->inp_vflag &= ~INP_IPV4; 542 inp->inp_vflag |= INP_IPV6; 543 inp->inp_inc.inc_isipv6 = 1; 544 if ((error = tcp6_connect(tp, nam, td)) != 0) 545 goto out; 546 error = tcp_output(tp); 547 548out: 549 TCPDEBUG2(PRU_CONNECT); 550 INP_UNLOCK(inp); 551 INP_INFO_WUNLOCK(&tcbinfo); 552 return (error); 553} 554#endif /* INET6 */ 555 556/* 557 * Initiate disconnect from peer. 558 * If connection never passed embryonic stage, just drop; 559 * else if don't need to let data drain, then can just drop anyways, 560 * else have to begin TCP shutdown process: mark socket disconnecting, 561 * drain unread data, state switch to reflect user close, and 562 * send segment (e.g. FIN) to peer. Socket will be really disconnected 563 * when peer sends FIN and acks ours. 564 * 565 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 566 */ 567static int 568tcp_usr_disconnect(struct socket *so) 569{ 570 struct inpcb *inp; 571 struct tcpcb *tp = NULL; 572 int error = 0; 573 574 TCPDEBUG0; 575 INP_INFO_WLOCK(&tcbinfo); 576 inp = sotoinpcb(so); 577 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL")); 578 INP_LOCK(inp); 579 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 580 error = EINVAL; 581 goto out; 582 } 583 tp = intotcpcb(inp); 584 TCPDEBUG1(); 585 tcp_disconnect(tp); 586out: 587 TCPDEBUG2(PRU_DISCONNECT); 588 INP_UNLOCK(inp); 589 INP_INFO_WUNLOCK(&tcbinfo); 590 return (error); 591} 592 593/* 594 * Accept a connection. Essentially all the work is 595 * done at higher levels; just return the address 596 * of the peer, storing through addr. 597 */ 598static int 599tcp_usr_accept(struct socket *so, struct sockaddr **nam) 600{ 601 int error = 0; 602 struct inpcb *inp = NULL; 603 struct tcpcb *tp = NULL; 604 struct in_addr addr; 605 in_port_t port = 0; 606 TCPDEBUG0; 607 608 if (so->so_state & SS_ISDISCONNECTED) 609 return (ECONNABORTED); 610 611 inp = sotoinpcb(so); 612 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL")); 613 INP_LOCK(inp); 614 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 615 error = ECONNABORTED; 616 goto out; 617 } 618 tp = intotcpcb(inp); 619 TCPDEBUG1(); 620 621 /* 622 * We inline in_setpeeraddr and COMMON_END here, so that we can 623 * copy the data of interest and defer the malloc until after we 624 * release the lock. 625 */ 626 port = inp->inp_fport; 627 addr = inp->inp_faddr; 628 629out: 630 TCPDEBUG2(PRU_ACCEPT); 631 INP_UNLOCK(inp); 632 if (error == 0) 633 *nam = in_sockaddr(port, &addr); 634 return error; 635} 636 637#ifdef INET6 638static int 639tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 640{ 641 struct inpcb *inp = NULL; 642 int error = 0; 643 struct tcpcb *tp = NULL; 644 struct in_addr addr; 645 struct in6_addr addr6; 646 in_port_t port = 0; 647 int v4 = 0; 648 TCPDEBUG0; 649 650 if (so->so_state & SS_ISDISCONNECTED) { 651 error = ECONNABORTED; 652 goto out; 653 } 654 655 inp = sotoinpcb(so); 656 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL")); 657 INP_LOCK(inp); 658 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 659 error = EINVAL; 660 goto out; 661 } 662 tp = intotcpcb(inp); 663 TCPDEBUG1(); 664 665 /* 666 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 667 * copy the data of interest and defer the malloc until after we 668 * release the lock. 669 */ 670 if (inp->inp_vflag & INP_IPV4) { 671 v4 = 1; 672 port = inp->inp_fport; 673 addr = inp->inp_faddr; 674 } else { 675 port = inp->inp_fport; 676 addr6 = inp->in6p_faddr; 677 } 678 679out: 680 TCPDEBUG2(PRU_ACCEPT); 681 INP_UNLOCK(inp); 682 if (error == 0) { 683 if (v4) 684 *nam = in6_v4mapsin6_sockaddr(port, &addr); 685 else 686 *nam = in6_sockaddr(port, &addr6); 687 } 688 return error; 689} 690#endif /* INET6 */ 691 692/* 693 * This is the wrapper function for in_setsockaddr. We just pass down 694 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 695 * here because in_setsockaddr will call malloc and can block. 696 */ 697static int 698tcp_sockaddr(struct socket *so, struct sockaddr **nam) 699{ 700 return (in_setsockaddr(so, nam, &tcbinfo)); 701} 702 703/* 704 * This is the wrapper function for in_setpeeraddr. We just pass down 705 * the pcbinfo for in_setpeeraddr to lock. 706 */ 707static int 708tcp_peeraddr(struct socket *so, struct sockaddr **nam) 709{ 710 return (in_setpeeraddr(so, nam, &tcbinfo)); 711} 712 713/* 714 * Mark the connection as being incapable of further output. 715 */ 716static int 717tcp_usr_shutdown(struct socket *so) 718{ 719 int error = 0; 720 struct inpcb *inp; 721 struct tcpcb *tp = NULL; 722 723 TCPDEBUG0; 724 INP_INFO_WLOCK(&tcbinfo); 725 inp = sotoinpcb(so); 726 KASSERT(inp != NULL, ("inp == NULL")); 727 INP_LOCK(inp); 728 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 729 error = EINVAL; 730 goto out; 731 } 732 tp = intotcpcb(inp); 733 TCPDEBUG1(); 734 socantsendmore(so); 735 tcp_usrclosed(tp); 736 error = tcp_output(tp); 737 738out: 739 TCPDEBUG2(PRU_SHUTDOWN); 740 INP_UNLOCK(inp); 741 INP_INFO_WUNLOCK(&tcbinfo); 742 743 return (error); 744} 745 746/* 747 * After a receive, possibly send window update to peer. 748 */ 749static int 750tcp_usr_rcvd(struct socket *so, int flags) 751{ 752 struct inpcb *inp; 753 struct tcpcb *tp = NULL; 754 int error = 0; 755 756 TCPDEBUG0; 757 inp = sotoinpcb(so); 758 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL")); 759 INP_LOCK(inp); 760 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 761 error = EINVAL; 762 goto out; 763 } 764 tp = intotcpcb(inp); 765 TCPDEBUG1(); 766 tcp_output(tp); 767 768out: 769 TCPDEBUG2(PRU_RCVD); 770 INP_UNLOCK(inp); 771 return (error); 772} 773 774/* 775 * Do a send by putting data in output queue and updating urgent 776 * marker if URG set. Possibly send more data. Unlike the other 777 * pru_*() routines, the mbuf chains are our responsibility. We 778 * must either enqueue them or free them. The other pru_* routines 779 * generally are caller-frees. 780 */ 781static int 782tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 783 struct sockaddr *nam, struct mbuf *control, struct thread *td) 784{ 785 int error = 0; 786 struct inpcb *inp; 787 struct tcpcb *tp = NULL; 788 int headlocked = 0; 789#ifdef INET6 790 int isipv6; 791#endif 792 TCPDEBUG0; 793 794 /* 795 * We require the pcbinfo lock in two cases: 796 * 797 * (1) An implied connect is taking place, which can result in 798 * binding IPs and ports and hence modification of the pcb hash 799 * chains. 800 * 801 * (2) PRUS_EOF is set, resulting in explicit close on the send. 802 */ 803 if ((nam != NULL) || (flags & PRUS_EOF)) { 804 INP_INFO_WLOCK(&tcbinfo); 805 headlocked = 1; 806 } 807 inp = sotoinpcb(so); 808 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL")); 809 INP_LOCK(inp); 810 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 811 error = EINVAL; 812 goto out; 813 } 814#ifdef INET6 815 isipv6 = nam && nam->sa_family == AF_INET6; 816#endif /* INET6 */ 817 tp = intotcpcb(inp); 818 TCPDEBUG1(); 819 if (control) { 820 /* TCP doesn't do control messages (rights, creds, etc) */ 821 if (control->m_len) { 822 m_freem(control); 823 if (m) 824 m_freem(m); 825 error = EINVAL; 826 goto out; 827 } 828 m_freem(control); /* empty control, just free it */ 829 } 830 if (!(flags & PRUS_OOB)) { 831 sbappendstream(&so->so_snd, m); 832 if (nam && tp->t_state < TCPS_SYN_SENT) { 833 /* 834 * Do implied connect if not yet connected, 835 * initialize window to default value, and 836 * initialize maxseg/maxopd using peer's cached 837 * MSS. 838 */ 839 INP_INFO_WLOCK_ASSERT(&tcbinfo); 840#ifdef INET6 841 if (isipv6) 842 error = tcp6_connect(tp, nam, td); 843 else 844#endif /* INET6 */ 845 error = tcp_connect(tp, nam, td); 846 if (error) 847 goto out; 848 tp->snd_wnd = TTCP_CLIENT_SND_WND; 849 tcp_mss(tp, -1); 850 } 851 if (flags & PRUS_EOF) { 852 /* 853 * Close the send side of the connection after 854 * the data is sent. 855 */ 856 INP_INFO_WLOCK_ASSERT(&tcbinfo); 857 socantsendmore(so); 858 tcp_usrclosed(tp); 859 } 860 if (headlocked) { 861 INP_INFO_WUNLOCK(&tcbinfo); 862 headlocked = 0; 863 } 864 if (tp != NULL) { 865 if (flags & PRUS_MORETOCOME) 866 tp->t_flags |= TF_MORETOCOME; 867 error = tcp_output(tp); 868 if (flags & PRUS_MORETOCOME) 869 tp->t_flags &= ~TF_MORETOCOME; 870 } 871 } else { 872 /* 873 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 874 */ 875 SOCKBUF_LOCK(&so->so_snd); 876 if (sbspace(&so->so_snd) < -512) { 877 SOCKBUF_UNLOCK(&so->so_snd); 878 m_freem(m); 879 error = ENOBUFS; 880 goto out; 881 } 882 /* 883 * According to RFC961 (Assigned Protocols), 884 * the urgent pointer points to the last octet 885 * of urgent data. We continue, however, 886 * to consider it to indicate the first octet 887 * of data past the urgent section. 888 * Otherwise, snd_up should be one lower. 889 */ 890 sbappendstream_locked(&so->so_snd, m); 891 SOCKBUF_UNLOCK(&so->so_snd); 892 if (nam && tp->t_state < TCPS_SYN_SENT) { 893 /* 894 * Do implied connect if not yet connected, 895 * initialize window to default value, and 896 * initialize maxseg/maxopd using peer's cached 897 * MSS. 898 */ 899 INP_INFO_WLOCK_ASSERT(&tcbinfo); 900#ifdef INET6 901 if (isipv6) 902 error = tcp6_connect(tp, nam, td); 903 else 904#endif /* INET6 */ 905 error = tcp_connect(tp, nam, td); 906 if (error) 907 goto out; 908 tp->snd_wnd = TTCP_CLIENT_SND_WND; 909 tcp_mss(tp, -1); 910 INP_INFO_WUNLOCK(&tcbinfo); 911 headlocked = 0; 912 } else if (nam) { 913 INP_INFO_WUNLOCK(&tcbinfo); 914 headlocked = 0; 915 } 916 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 917 tp->t_flags |= TF_FORCEDATA; 918 error = tcp_output(tp); 919 tp->t_flags &= ~TF_FORCEDATA; 920 } 921out: 922 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 923 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 924 INP_UNLOCK(inp); 925 if (headlocked) 926 INP_INFO_WUNLOCK(&tcbinfo); 927 return (error); 928} 929 930/* 931 * Abort the TCP. 932 * 933 * First, drop the connection. Then collect state if possible. 934 */ 935static void 936tcp_usr_abort(struct socket *so) 937{ 938 struct inpcb *inp; 939 struct tcpcb *tp; 940 TCPDEBUG0; 941 942 inp = sotoinpcb(so); 943 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 944 945 INP_INFO_WLOCK(&tcbinfo); 946 INP_LOCK(inp); 947 KASSERT(inp->inp_socket != NULL, 948 ("tcp_usr_abort: inp_socket == NULL")); 949 TCPDEBUG1(); 950 951 /* 952 * First, if we still have full TCP state, and we're not dropped, 953 * drop. 954 */ 955 if (!(inp->inp_vflag & INP_TIMEWAIT) && 956 !(inp->inp_vflag & INP_DROPPED)) { 957 tp = intotcpcb(inp); 958 tcp_drop(tp, ECONNABORTED); 959 } 960 tcp_detach(so, inp); 961 tp = NULL; 962 TCPDEBUG2(PRU_DETACH); 963 INP_INFO_WUNLOCK(&tcbinfo); 964} 965 966/* 967 * Receive out-of-band data. 968 */ 969static int 970tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 971{ 972 int error = 0; 973 struct inpcb *inp; 974 struct tcpcb *tp = NULL; 975 976 TCPDEBUG0; 977 inp = sotoinpcb(so); 978 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 979 INP_LOCK(inp); 980 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 981 error = EINVAL; 982 goto out; 983 } 984 tp = intotcpcb(inp); 985 TCPDEBUG1(); 986 if ((so->so_oobmark == 0 && 987 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 988 so->so_options & SO_OOBINLINE || 989 tp->t_oobflags & TCPOOB_HADDATA) { 990 error = EINVAL; 991 goto out; 992 } 993 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 994 error = EWOULDBLOCK; 995 goto out; 996 } 997 m->m_len = 1; 998 *mtod(m, caddr_t) = tp->t_iobc; 999 if ((flags & MSG_PEEK) == 0) 1000 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1001 1002out: 1003 TCPDEBUG2(PRU_RCVOOB); 1004 INP_UNLOCK(inp); 1005 return (error); 1006} 1007 1008struct pr_usrreqs tcp_usrreqs = { 1009 .pru_abort = tcp_usr_abort, 1010 .pru_accept = tcp_usr_accept, 1011 .pru_attach = tcp_usr_attach, 1012 .pru_bind = tcp_usr_bind, 1013 .pru_connect = tcp_usr_connect, 1014 .pru_control = in_control, 1015 .pru_detach = tcp_usr_detach, 1016 .pru_disconnect = tcp_usr_disconnect, 1017 .pru_listen = tcp_usr_listen, 1018 .pru_peeraddr = tcp_peeraddr, 1019 .pru_rcvd = tcp_usr_rcvd, 1020 .pru_rcvoob = tcp_usr_rcvoob, 1021 .pru_send = tcp_usr_send, 1022 .pru_shutdown = tcp_usr_shutdown, 1023 .pru_sockaddr = tcp_sockaddr, 1024 .pru_sosetlabel = in_pcbsosetlabel 1025}; 1026 1027#ifdef INET6 1028struct pr_usrreqs tcp6_usrreqs = { 1029 .pru_abort = tcp_usr_abort, 1030 .pru_accept = tcp6_usr_accept, 1031 .pru_attach = tcp_usr_attach, 1032 .pru_bind = tcp6_usr_bind, 1033 .pru_connect = tcp6_usr_connect, 1034 .pru_control = in6_control, 1035 .pru_detach = tcp_usr_detach, 1036 .pru_disconnect = tcp_usr_disconnect, 1037 .pru_listen = tcp6_usr_listen, 1038 .pru_peeraddr = in6_mapped_peeraddr, 1039 .pru_rcvd = tcp_usr_rcvd, 1040 .pru_rcvoob = tcp_usr_rcvoob, 1041 .pru_send = tcp_usr_send, 1042 .pru_shutdown = tcp_usr_shutdown, 1043 .pru_sockaddr = in6_mapped_sockaddr, 1044 .pru_sosetlabel = in_pcbsosetlabel 1045}; 1046#endif /* INET6 */ 1047 1048/* 1049 * Common subroutine to open a TCP connection to remote host specified 1050 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 1051 * port number if needed. Call in_pcbconnect_setup to do the routing and 1052 * to choose a local host address (interface). If there is an existing 1053 * incarnation of the same connection in TIME-WAIT state and if the remote 1054 * host was sending CC options and if the connection duration was < MSL, then 1055 * truncate the previous TIME-WAIT state and proceed. 1056 * Initialize connection parameters and enter SYN-SENT state. 1057 */ 1058static int 1059tcp_connect(tp, nam, td) 1060 register struct tcpcb *tp; 1061 struct sockaddr *nam; 1062 struct thread *td; 1063{ 1064 struct inpcb *inp = tp->t_inpcb, *oinp; 1065 struct socket *so = inp->inp_socket; 1066 struct in_addr laddr; 1067 u_short lport; 1068 int error; 1069 1070 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1071 INP_LOCK_ASSERT(inp); 1072 1073 if (inp->inp_lport == 0) { 1074 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1075 if (error) 1076 return error; 1077 } 1078 1079 /* 1080 * Cannot simply call in_pcbconnect, because there might be an 1081 * earlier incarnation of this same connection still in 1082 * TIME_WAIT state, creating an ADDRINUSE error. 1083 */ 1084 laddr = inp->inp_laddr; 1085 lport = inp->inp_lport; 1086 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1087 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 1088 if (error && oinp == NULL) 1089 return error; 1090 if (oinp) 1091 return EADDRINUSE; 1092 inp->inp_laddr = laddr; 1093 in_pcbrehash(inp); 1094 1095 /* Compute window scaling to request. */ 1096 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1097 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1098 tp->request_r_scale++; 1099 1100 soisconnecting(so); 1101 tcpstat.tcps_connattempt++; 1102 tp->t_state = TCPS_SYN_SENT; 1103 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1104 tp->iss = tcp_new_isn(tp); 1105 tp->t_bw_rtseq = tp->iss; 1106 tcp_sendseqinit(tp); 1107 1108 return 0; 1109} 1110 1111#ifdef INET6 1112static int 1113tcp6_connect(tp, nam, td) 1114 register struct tcpcb *tp; 1115 struct sockaddr *nam; 1116 struct thread *td; 1117{ 1118 struct inpcb *inp = tp->t_inpcb, *oinp; 1119 struct socket *so = inp->inp_socket; 1120 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1121 struct in6_addr *addr6; 1122 int error; 1123 1124 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1125 INP_LOCK_ASSERT(inp); 1126 1127 if (inp->inp_lport == 0) { 1128 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1129 if (error) 1130 return error; 1131 } 1132 1133 /* 1134 * Cannot simply call in_pcbconnect, because there might be an 1135 * earlier incarnation of this same connection still in 1136 * TIME_WAIT state, creating an ADDRINUSE error. 1137 * in6_pcbladdr() also handles scope zone IDs. 1138 */ 1139 error = in6_pcbladdr(inp, nam, &addr6); 1140 if (error) 1141 return error; 1142 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1143 &sin6->sin6_addr, sin6->sin6_port, 1144 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 1145 ? addr6 1146 : &inp->in6p_laddr, 1147 inp->inp_lport, 0, NULL); 1148 if (oinp) 1149 return EADDRINUSE; 1150 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1151 inp->in6p_laddr = *addr6; 1152 inp->in6p_faddr = sin6->sin6_addr; 1153 inp->inp_fport = sin6->sin6_port; 1154 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 1155 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 1156 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 1157 inp->in6p_flowinfo |= 1158 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1159 in_pcbrehash(inp); 1160 1161 /* Compute window scaling to request. */ 1162 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1163 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1164 tp->request_r_scale++; 1165 1166 soisconnecting(so); 1167 tcpstat.tcps_connattempt++; 1168 tp->t_state = TCPS_SYN_SENT; 1169 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1170 tp->iss = tcp_new_isn(tp); 1171 tp->t_bw_rtseq = tp->iss; 1172 tcp_sendseqinit(tp); 1173 1174 return 0; 1175} 1176#endif /* INET6 */ 1177 1178/* 1179 * Export TCP internal state information via a struct tcp_info, based on the 1180 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1181 * (TCP state machine, etc). We export all information using FreeBSD-native 1182 * constants -- for example, the numeric values for tcpi_state will differ 1183 * from Linux. 1184 */ 1185static void 1186tcp_fill_info(tp, ti) 1187 struct tcpcb *tp; 1188 struct tcp_info *ti; 1189{ 1190 1191 INP_LOCK_ASSERT(tp->t_inpcb); 1192 bzero(ti, sizeof(*ti)); 1193 1194 ti->tcpi_state = tp->t_state; 1195 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1196 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1197 if (tp->sack_enable) 1198 ti->tcpi_options |= TCPI_OPT_SACK; 1199 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1200 ti->tcpi_options |= TCPI_OPT_WSCALE; 1201 ti->tcpi_snd_wscale = tp->snd_scale; 1202 ti->tcpi_rcv_wscale = tp->rcv_scale; 1203 } 1204 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1205 ti->tcpi_snd_cwnd = tp->snd_cwnd; 1206 1207 /* 1208 * FreeBSD-specific extension fields for tcp_info. 1209 */ 1210 ti->tcpi_rcv_space = tp->rcv_wnd; 1211 ti->tcpi_snd_wnd = tp->snd_wnd; 1212 ti->tcpi_snd_bwnd = tp->snd_bwnd; 1213} 1214 1215/* 1216 * The new sockopt interface makes it possible for us to block in the 1217 * copyin/out step (if we take a page fault). Taking a page fault at 1218 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 1219 * use TSM, there probably isn't any need for this function to run at 1220 * splnet() any more. This needs more examination.) 1221 * 1222 * XXXRW: The locking here is wrong; we may take a page fault while holding 1223 * the inpcb lock. 1224 */ 1225int 1226tcp_ctloutput(so, sopt) 1227 struct socket *so; 1228 struct sockopt *sopt; 1229{ 1230 int error, opt, optval; 1231 struct inpcb *inp; 1232 struct tcpcb *tp; 1233 struct tcp_info ti; 1234 1235 error = 0; 1236 inp = sotoinpcb(so); 1237 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1238 INP_LOCK(inp); 1239 if (sopt->sopt_level != IPPROTO_TCP) { 1240 INP_UNLOCK(inp); 1241#ifdef INET6 1242 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1243 error = ip6_ctloutput(so, sopt); 1244 else 1245#endif /* INET6 */ 1246 error = ip_ctloutput(so, sopt); 1247 return (error); 1248 } 1249 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 1250 error = ECONNRESET; 1251 goto out; 1252 } 1253 tp = intotcpcb(inp); 1254 1255 switch (sopt->sopt_dir) { 1256 case SOPT_SET: 1257 switch (sopt->sopt_name) { 1258#ifdef TCP_SIGNATURE 1259 case TCP_MD5SIG: 1260 error = sooptcopyin(sopt, &optval, sizeof optval, 1261 sizeof optval); 1262 if (error) 1263 break; 1264 1265 if (optval > 0) 1266 tp->t_flags |= TF_SIGNATURE; 1267 else 1268 tp->t_flags &= ~TF_SIGNATURE; 1269 break; 1270#endif /* TCP_SIGNATURE */ 1271 case TCP_NODELAY: 1272 case TCP_NOOPT: 1273 error = sooptcopyin(sopt, &optval, sizeof optval, 1274 sizeof optval); 1275 if (error) 1276 break; 1277 1278 switch (sopt->sopt_name) { 1279 case TCP_NODELAY: 1280 opt = TF_NODELAY; 1281 break; 1282 case TCP_NOOPT: 1283 opt = TF_NOOPT; 1284 break; 1285 default: 1286 opt = 0; /* dead code to fool gcc */ 1287 break; 1288 } 1289 1290 if (optval) 1291 tp->t_flags |= opt; 1292 else 1293 tp->t_flags &= ~opt; 1294 break; 1295 1296 case TCP_NOPUSH: 1297 error = sooptcopyin(sopt, &optval, sizeof optval, 1298 sizeof optval); 1299 if (error) 1300 break; 1301 1302 if (optval) 1303 tp->t_flags |= TF_NOPUSH; 1304 else { 1305 tp->t_flags &= ~TF_NOPUSH; 1306 error = tcp_output(tp); 1307 } 1308 break; 1309 1310 case TCP_MAXSEG: 1311 error = sooptcopyin(sopt, &optval, sizeof optval, 1312 sizeof optval); 1313 if (error) 1314 break; 1315 1316 if (optval > 0 && optval <= tp->t_maxseg && 1317 optval + 40 >= tcp_minmss) 1318 tp->t_maxseg = optval; 1319 else 1320 error = EINVAL; 1321 break; 1322 1323 case TCP_INFO: 1324 error = EINVAL; 1325 break; 1326 1327 default: 1328 error = ENOPROTOOPT; 1329 break; 1330 } 1331 break; 1332 1333 case SOPT_GET: 1334 switch (sopt->sopt_name) { 1335#ifdef TCP_SIGNATURE 1336 case TCP_MD5SIG: 1337 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1338 error = sooptcopyout(sopt, &optval, sizeof optval); 1339 break; 1340#endif 1341 case TCP_NODELAY: 1342 optval = tp->t_flags & TF_NODELAY; 1343 error = sooptcopyout(sopt, &optval, sizeof optval); 1344 break; 1345 case TCP_MAXSEG: 1346 optval = tp->t_maxseg; 1347 error = sooptcopyout(sopt, &optval, sizeof optval); 1348 break; 1349 case TCP_NOOPT: 1350 optval = tp->t_flags & TF_NOOPT; 1351 error = sooptcopyout(sopt, &optval, sizeof optval); 1352 break; 1353 case TCP_NOPUSH: 1354 optval = tp->t_flags & TF_NOPUSH; 1355 error = sooptcopyout(sopt, &optval, sizeof optval); 1356 break; 1357 case TCP_INFO: 1358 tcp_fill_info(tp, &ti); 1359 error = sooptcopyout(sopt, &ti, sizeof ti); 1360 break; 1361 default: 1362 error = ENOPROTOOPT; 1363 break; 1364 } 1365 break; 1366 } 1367out: 1368 INP_UNLOCK(inp); 1369 return (error); 1370} 1371 1372/* 1373 * tcp_sendspace and tcp_recvspace are the default send and receive window 1374 * sizes, respectively. These are obsolescent (this information should 1375 * be set by the route). 1376 */ 1377u_long tcp_sendspace = 1024*32; 1378SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1379 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1380u_long tcp_recvspace = 1024*64; 1381SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1382 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1383 1384/* 1385 * Attach TCP protocol to socket, allocating 1386 * internet protocol control block, tcp control block, 1387 * bufer space, and entering LISTEN state if to accept connections. 1388 */ 1389static int 1390tcp_attach(so) 1391 struct socket *so; 1392{ 1393 register struct tcpcb *tp; 1394 struct inpcb *inp; 1395 int error; 1396#ifdef INET6 1397 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1398#endif 1399 1400 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1401 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1402 if (error) 1403 return (error); 1404 } 1405 INP_INFO_WLOCK(&tcbinfo); 1406 error = in_pcballoc(so, &tcbinfo, "tcpinp"); 1407 if (error) { 1408 INP_INFO_WUNLOCK(&tcbinfo); 1409 return (error); 1410 } 1411 inp = sotoinpcb(so); 1412 INP_LOCK(inp); 1413#ifdef INET6 1414 if (isipv6) { 1415 inp->inp_vflag |= INP_IPV6; 1416 inp->in6p_hops = -1; /* use kernel default */ 1417 } 1418 else 1419#endif 1420 inp->inp_vflag |= INP_IPV4; 1421 tp = tcp_newtcpcb(inp); 1422 if (tp == NULL) { 1423#ifdef INET6 1424 if (isipv6) { 1425 in6_pcbdetach(inp); 1426 in6_pcbfree(inp); 1427 } else { 1428#endif 1429 in_pcbdetach(inp); 1430 in_pcbfree(inp); 1431#ifdef INET6 1432 } 1433#endif 1434 INP_INFO_WUNLOCK(&tcbinfo); 1435 return (ENOBUFS); 1436 } 1437 tp->t_state = TCPS_CLOSED; 1438 INP_UNLOCK(inp); 1439 INP_INFO_WUNLOCK(&tcbinfo); 1440 return (0); 1441} 1442 1443/* 1444 * Initiate (or continue) disconnect. 1445 * If embryonic state, just send reset (once). 1446 * If in ``let data drain'' option and linger null, just drop. 1447 * Otherwise (hard), mark socket disconnecting and drop 1448 * current input data; switch states based on user close, and 1449 * send segment to peer (with FIN). 1450 */ 1451static void 1452tcp_disconnect(tp) 1453 register struct tcpcb *tp; 1454{ 1455 struct inpcb *inp = tp->t_inpcb; 1456 struct socket *so = inp->inp_socket; 1457 1458 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1459 INP_LOCK_ASSERT(inp); 1460 1461 /* 1462 * Neither tcp_close() nor tcp_drop() should return NULL, as the 1463 * socket is still open. 1464 */ 1465 if (tp->t_state < TCPS_ESTABLISHED) { 1466 tp = tcp_close(tp); 1467 KASSERT(tp != NULL, 1468 ("tcp_disconnect: tcp_close() returned NULL")); 1469 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1470 tp = tcp_drop(tp, 0); 1471 KASSERT(tp != NULL, 1472 ("tcp_disconnect: tcp_drop() returned NULL")); 1473 } else { 1474 soisdisconnecting(so); 1475 sbflush(&so->so_rcv); 1476 tcp_usrclosed(tp); 1477 if (!(inp->inp_vflag & INP_DROPPED)) 1478 tcp_output(tp); 1479 } 1480} 1481 1482/* 1483 * User issued close, and wish to trail through shutdown states: 1484 * if never received SYN, just forget it. If got a SYN from peer, 1485 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1486 * If already got a FIN from peer, then almost done; go to LAST_ACK 1487 * state. In all other cases, have already sent FIN to peer (e.g. 1488 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1489 * for peer to send FIN or not respond to keep-alives, etc. 1490 * We can let the user exit from the close as soon as the FIN is acked. 1491 */ 1492static void 1493tcp_usrclosed(tp) 1494 register struct tcpcb *tp; 1495{ 1496 1497 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1498 INP_LOCK_ASSERT(tp->t_inpcb); 1499 1500 switch (tp->t_state) { 1501 1502 case TCPS_CLOSED: 1503 case TCPS_LISTEN: 1504 tp->t_state = TCPS_CLOSED; 1505 tp = tcp_close(tp); 1506 /* 1507 * tcp_close() should never return NULL here as the socket is 1508 * still open. 1509 */ 1510 KASSERT(tp != NULL, 1511 ("tcp_usrclosed: tcp_close() returned NULL")); 1512 break; 1513 1514 case TCPS_SYN_SENT: 1515 case TCPS_SYN_RECEIVED: 1516 tp->t_flags |= TF_NEEDFIN; 1517 break; 1518 1519 case TCPS_ESTABLISHED: 1520 tp->t_state = TCPS_FIN_WAIT_1; 1521 break; 1522 1523 case TCPS_CLOSE_WAIT: 1524 tp->t_state = TCPS_LAST_ACK; 1525 break; 1526 } 1527 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1528 soisdisconnected(tp->t_inpcb->inp_socket); 1529 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1530 if (tp->t_state == TCPS_FIN_WAIT_2) 1531 callout_reset(tp->tt_2msl, tcp_maxidle, 1532 tcp_timer_2msl, tp); 1533 } 1534} 1535