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