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$ 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 */ 85 extern char *tcpstates[]; /* XXX ??? */ 86 87 static int tcp_attach(struct socket *); 88 static int tcp_connect(struct tcpcb *, struct sockaddr *, 89 struct thread *td); 90 #ifdef INET6 91 static int tcp6_connect(struct tcpcb *, struct sockaddr *, 92 struct thread *td); 93 #endif /* INET6 */ 94 static void tcp_disconnect(struct tcpcb *); 95 static void tcp_usrclosed(struct tcpcb *); 96 static 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 */ 113 static int 114 tcp_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); 134 out: 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 */ 148 static void 149 tcp_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 */ 242 static void 243 tcp_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 */ 266 static int 267 tcp_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); 297 out: 298 TCPDEBUG2(PRU_BIND); 299 INP_UNLOCK(inp); 300 INP_INFO_WUNLOCK(&tcbinfo); 301 302 return (error); 303 } 304 305 #ifdef INET6 306 static int 307 tcp6_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); 353 out: 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 */ 364 static int 365 tcp_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 392 out: 393 TCPDEBUG2(PRU_LISTEN); 394 INP_UNLOCK(inp); 395 INP_INFO_WUNLOCK(&tcbinfo); 396 return (error); 397 } 398 399 #ifdef INET6 400 static int 401 tcp6_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 432 out: 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 */ 447 static int 448 tcp_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); 481 out: 482 TCPDEBUG2(PRU_CONNECT); 483 INP_UNLOCK(inp); 484 INP_INFO_WUNLOCK(&tcbinfo); 485 return (error); 486 } 487 488 #ifdef INET6 489 static int 490 tcp6_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 542 out: 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 */ 561 static int 562 tcp_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 = ECONNRESET; 575 goto out; 576 } 577 tp = intotcpcb(inp); 578 TCPDEBUG1(); 579 tcp_disconnect(tp); 580 out: 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 */ 592 static int 593 tcp_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 623 out: 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 632 static int 633 tcp6_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 = ECONNABORTED; 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 671 out: 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 */ 689 static int 690 tcp_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 */ 699 static int 700 tcp_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 */ 708 static int 709 tcp_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 = ECONNRESET; 722 goto out; 723 } 724 tp = intotcpcb(inp); 725 TCPDEBUG1(); 726 socantsendmore(so); 727 tcp_usrclosed(tp); 728 error = tcp_output(tp); 729 730 out: 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 */ 741 static int 742 tcp_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 = ECONNRESET; 754 goto out; 755 } 756 tp = intotcpcb(inp); 757 TCPDEBUG1(); 758 tcp_output(tp); 759 760 out: 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 */ 773 static int 774 tcp_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 if (control) 804 m_freem(control); 805 if (m) 806 m_freem(m); 807 error = ECONNRESET; 808 goto out; 809 } 810 #ifdef INET6 811 isipv6 = nam && nam->sa_family == AF_INET6; 812 #endif /* INET6 */ 813 tp = intotcpcb(inp); 814 TCPDEBUG1(); 815 if (control) { 816 /* TCP doesn't do control messages (rights, creds, etc) */ 817 if (control->m_len) { 818 m_freem(control); 819 if (m) 820 m_freem(m); 821 error = EINVAL; 822 goto out; 823 } 824 m_freem(control); /* empty control, just free it */ 825 } 826 if (!(flags & PRUS_OOB)) { 827 sbappendstream(&so->so_snd, m); 828 if (nam && tp->t_state < TCPS_SYN_SENT) { 829 /* 830 * Do implied connect if not yet connected, 831 * initialize window to default value, and 832 * initialize maxseg/maxopd using peer's cached 833 * MSS. 834 */ 835 INP_INFO_WLOCK_ASSERT(&tcbinfo); 836 #ifdef INET6 837 if (isipv6) 838 error = tcp6_connect(tp, nam, td); 839 else 840 #endif /* INET6 */ 841 error = tcp_connect(tp, nam, td); 842 if (error) 843 goto out; 844 tp->snd_wnd = TTCP_CLIENT_SND_WND; 845 tcp_mss(tp, -1); 846 } 847 if (flags & PRUS_EOF) { 848 /* 849 * Close the send side of the connection after 850 * the data is sent. 851 */ 852 INP_INFO_WLOCK_ASSERT(&tcbinfo); 853 socantsendmore(so); 854 tcp_usrclosed(tp); 855 } 856 if (headlocked) { 857 INP_INFO_WUNLOCK(&tcbinfo); 858 headlocked = 0; 859 } 860 if (tp != NULL) { 861 if (flags & PRUS_MORETOCOME) 862 tp->t_flags |= TF_MORETOCOME; 863 error = tcp_output(tp); 864 if (flags & PRUS_MORETOCOME) 865 tp->t_flags &= ~TF_MORETOCOME; 866 } 867 } else { 868 /* 869 * XXXRW: PRUS_EOF not implemented with PRUS_OOB? 870 */ 871 SOCKBUF_LOCK(&so->so_snd); 872 if (sbspace(&so->so_snd) < -512) { 873 SOCKBUF_UNLOCK(&so->so_snd); 874 m_freem(m); 875 error = ENOBUFS; 876 goto out; 877 } 878 /* 879 * According to RFC961 (Assigned Protocols), 880 * the urgent pointer points to the last octet 881 * of urgent data. We continue, however, 882 * to consider it to indicate the first octet 883 * of data past the urgent section. 884 * Otherwise, snd_up should be one lower. 885 */ 886 sbappendstream_locked(&so->so_snd, m); 887 SOCKBUF_UNLOCK(&so->so_snd); 888 if (nam && tp->t_state < TCPS_SYN_SENT) { 889 /* 890 * Do implied connect if not yet connected, 891 * initialize window to default value, and 892 * initialize maxseg/maxopd using peer's cached 893 * MSS. 894 */ 895 INP_INFO_WLOCK_ASSERT(&tcbinfo); 896 #ifdef INET6 897 if (isipv6) 898 error = tcp6_connect(tp, nam, td); 899 else 900 #endif /* INET6 */ 901 error = tcp_connect(tp, nam, td); 902 if (error) 903 goto out; 904 tp->snd_wnd = TTCP_CLIENT_SND_WND; 905 tcp_mss(tp, -1); 906 INP_INFO_WUNLOCK(&tcbinfo); 907 headlocked = 0; 908 } else if (nam) { 909 INP_INFO_WUNLOCK(&tcbinfo); 910 headlocked = 0; 911 } 912 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 913 tp->t_flags |= TF_FORCEDATA; 914 error = tcp_output(tp); 915 tp->t_flags &= ~TF_FORCEDATA; 916 } 917 out: 918 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB : 919 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 920 INP_UNLOCK(inp); 921 if (headlocked) 922 INP_INFO_WUNLOCK(&tcbinfo); 923 return (error); 924 } 925 926 /* 927 * Abort the TCP. Drop the connection abruptly. 928 */ 929 static void 930 tcp_usr_abort(struct socket *so) 931 { 932 struct inpcb *inp; 933 struct tcpcb *tp = NULL; 934 TCPDEBUG0; 935 936 inp = sotoinpcb(so); 937 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL")); 938 939 INP_INFO_WLOCK(&tcbinfo); 940 INP_LOCK(inp); 941 KASSERT(inp->inp_socket != NULL, 942 ("tcp_usr_abort: inp_socket == NULL")); 943 944 /* 945 * If we still have full TCP state, and we're not dropped, drop. 946 */ 947 if (!(inp->inp_vflag & INP_TIMEWAIT) && 948 !(inp->inp_vflag & INP_DROPPED)) { 949 tp = intotcpcb(inp); 950 TCPDEBUG1(); 951 tcp_drop(tp, ECONNABORTED); 952 TCPDEBUG2(PRU_ABORT); 953 } 954 if (!(inp->inp_vflag & INP_DROPPED)) { 955 SOCK_LOCK(so); 956 so->so_state |= SS_PROTOREF; 957 SOCK_UNLOCK(so); 958 inp->inp_vflag |= INP_SOCKREF; 959 } 960 INP_UNLOCK(inp); 961 INP_INFO_WUNLOCK(&tcbinfo); 962 } 963 964 /* 965 * TCP socket is closed. Start friendly disconnect. 966 */ 967 static void 968 tcp_usr_close(struct socket *so) 969 { 970 struct inpcb *inp; 971 struct tcpcb *tp = NULL; 972 TCPDEBUG0; 973 974 inp = sotoinpcb(so); 975 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL")); 976 977 INP_INFO_WLOCK(&tcbinfo); 978 INP_LOCK(inp); 979 KASSERT(inp->inp_socket != NULL, 980 ("tcp_usr_close: inp_socket == NULL")); 981 982 /* 983 * If we still have full TCP state, and we're not dropped, initiate 984 * a disconnect. 985 */ 986 if (!(inp->inp_vflag & INP_TIMEWAIT) && 987 !(inp->inp_vflag & INP_DROPPED)) { 988 tp = intotcpcb(inp); 989 TCPDEBUG1(); 990 tcp_disconnect(tp); 991 TCPDEBUG2(PRU_CLOSE); 992 } 993 if (!(inp->inp_vflag & INP_DROPPED)) { 994 SOCK_LOCK(so); 995 so->so_state |= SS_PROTOREF; 996 SOCK_UNLOCK(so); 997 inp->inp_vflag |= INP_SOCKREF; 998 } 999 INP_UNLOCK(inp); 1000 INP_INFO_WUNLOCK(&tcbinfo); 1001 } 1002 1003 /* 1004 * Receive out-of-band data. 1005 */ 1006 static int 1007 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 1008 { 1009 int error = 0; 1010 struct inpcb *inp; 1011 struct tcpcb *tp = NULL; 1012 1013 TCPDEBUG0; 1014 inp = sotoinpcb(so); 1015 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL")); 1016 INP_LOCK(inp); 1017 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 1018 error = ECONNRESET; 1019 goto out; 1020 } 1021 tp = intotcpcb(inp); 1022 TCPDEBUG1(); 1023 if ((so->so_oobmark == 0 && 1024 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 1025 so->so_options & SO_OOBINLINE || 1026 tp->t_oobflags & TCPOOB_HADDATA) { 1027 error = EINVAL; 1028 goto out; 1029 } 1030 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 1031 error = EWOULDBLOCK; 1032 goto out; 1033 } 1034 m->m_len = 1; 1035 *mtod(m, caddr_t) = tp->t_iobc; 1036 if ((flags & MSG_PEEK) == 0) 1037 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 1038 1039 out: 1040 TCPDEBUG2(PRU_RCVOOB); 1041 INP_UNLOCK(inp); 1042 return (error); 1043 } 1044 1045 struct pr_usrreqs tcp_usrreqs = { 1046 .pru_abort = tcp_usr_abort, 1047 .pru_accept = tcp_usr_accept, 1048 .pru_attach = tcp_usr_attach, 1049 .pru_bind = tcp_usr_bind, 1050 .pru_connect = tcp_usr_connect, 1051 .pru_control = in_control, 1052 .pru_detach = tcp_usr_detach, 1053 .pru_disconnect = tcp_usr_disconnect, 1054 .pru_listen = tcp_usr_listen, 1055 .pru_peeraddr = tcp_peeraddr, 1056 .pru_rcvd = tcp_usr_rcvd, 1057 .pru_rcvoob = tcp_usr_rcvoob, 1058 .pru_send = tcp_usr_send, 1059 .pru_shutdown = tcp_usr_shutdown, 1060 .pru_sockaddr = tcp_sockaddr, 1061 .pru_sosetlabel = in_pcbsosetlabel, 1062 .pru_close = tcp_usr_close, 1063 }; 1064 1065 #ifdef INET6 1066 struct pr_usrreqs tcp6_usrreqs = { 1067 .pru_abort = tcp_usr_abort, 1068 .pru_accept = tcp6_usr_accept, 1069 .pru_attach = tcp_usr_attach, 1070 .pru_bind = tcp6_usr_bind, 1071 .pru_connect = tcp6_usr_connect, 1072 .pru_control = in6_control, 1073 .pru_detach = tcp_usr_detach, 1074 .pru_disconnect = tcp_usr_disconnect, 1075 .pru_listen = tcp6_usr_listen, 1076 .pru_peeraddr = in6_mapped_peeraddr, 1077 .pru_rcvd = tcp_usr_rcvd, 1078 .pru_rcvoob = tcp_usr_rcvoob, 1079 .pru_send = tcp_usr_send, 1080 .pru_shutdown = tcp_usr_shutdown, 1081 .pru_sockaddr = in6_mapped_sockaddr, 1082 .pru_sosetlabel = in_pcbsosetlabel, 1083 .pru_close = tcp_usr_close, 1084 }; 1085 #endif /* INET6 */ 1086 1087 /* 1088 * Common subroutine to open a TCP connection to remote host specified 1089 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 1090 * port number if needed. Call in_pcbconnect_setup to do the routing and 1091 * to choose a local host address (interface). If there is an existing 1092 * incarnation of the same connection in TIME-WAIT state and if the remote 1093 * host was sending CC options and if the connection duration was < MSL, then 1094 * truncate the previous TIME-WAIT state and proceed. 1095 * Initialize connection parameters and enter SYN-SENT state. 1096 */ 1097 static int 1098 tcp_connect(tp, nam, td) 1099 register struct tcpcb *tp; 1100 struct sockaddr *nam; 1101 struct thread *td; 1102 { 1103 struct inpcb *inp = tp->t_inpcb, *oinp; 1104 struct socket *so = inp->inp_socket; 1105 struct in_addr laddr; 1106 u_short lport; 1107 int error; 1108 1109 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1110 INP_LOCK_ASSERT(inp); 1111 1112 if (inp->inp_lport == 0) { 1113 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1114 if (error) 1115 return error; 1116 } 1117 1118 /* 1119 * Cannot simply call in_pcbconnect, because there might be an 1120 * earlier incarnation of this same connection still in 1121 * TIME_WAIT state, creating an ADDRINUSE error. 1122 */ 1123 laddr = inp->inp_laddr; 1124 lport = inp->inp_lport; 1125 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 1126 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 1127 if (error && oinp == NULL) 1128 return error; 1129 if (oinp) 1130 return EADDRINUSE; 1131 inp->inp_laddr = laddr; 1132 in_pcbrehash(inp); 1133 1134 /* Compute window scaling to request. */ 1135 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1136 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1137 tp->request_r_scale++; 1138 1139 soisconnecting(so); 1140 tcpstat.tcps_connattempt++; 1141 tp->t_state = TCPS_SYN_SENT; 1142 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1143 tp->iss = tcp_new_isn(tp); 1144 tp->t_bw_rtseq = tp->iss; 1145 tcp_sendseqinit(tp); 1146 1147 return 0; 1148 } 1149 1150 #ifdef INET6 1151 static int 1152 tcp6_connect(tp, nam, td) 1153 register struct tcpcb *tp; 1154 struct sockaddr *nam; 1155 struct thread *td; 1156 { 1157 struct inpcb *inp = tp->t_inpcb, *oinp; 1158 struct socket *so = inp->inp_socket; 1159 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 1160 struct in6_addr *addr6; 1161 int error; 1162 1163 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1164 INP_LOCK_ASSERT(inp); 1165 1166 if (inp->inp_lport == 0) { 1167 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 1168 if (error) 1169 return error; 1170 } 1171 1172 /* 1173 * Cannot simply call in_pcbconnect, because there might be an 1174 * earlier incarnation of this same connection still in 1175 * TIME_WAIT state, creating an ADDRINUSE error. 1176 * in6_pcbladdr() also handles scope zone IDs. 1177 */ 1178 error = in6_pcbladdr(inp, nam, &addr6); 1179 if (error) 1180 return error; 1181 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 1182 &sin6->sin6_addr, sin6->sin6_port, 1183 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 1184 ? addr6 1185 : &inp->in6p_laddr, 1186 inp->inp_lport, 0, NULL); 1187 if (oinp) 1188 return EADDRINUSE; 1189 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 1190 inp->in6p_laddr = *addr6; 1191 inp->in6p_faddr = sin6->sin6_addr; 1192 inp->inp_fport = sin6->sin6_port; 1193 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 1194 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 1195 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 1196 inp->in6p_flowinfo |= 1197 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 1198 in_pcbrehash(inp); 1199 1200 /* Compute window scaling to request. */ 1201 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 1202 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 1203 tp->request_r_scale++; 1204 1205 soisconnecting(so); 1206 tcpstat.tcps_connattempt++; 1207 tp->t_state = TCPS_SYN_SENT; 1208 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 1209 tp->iss = tcp_new_isn(tp); 1210 tp->t_bw_rtseq = tp->iss; 1211 tcp_sendseqinit(tp); 1212 1213 return 0; 1214 } 1215 #endif /* INET6 */ 1216 1217 /* 1218 * Export TCP internal state information via a struct tcp_info, based on the 1219 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently 1220 * (TCP state machine, etc). We export all information using FreeBSD-native 1221 * constants -- for example, the numeric values for tcpi_state will differ 1222 * from Linux. 1223 */ 1224 static void 1225 tcp_fill_info(tp, ti) 1226 struct tcpcb *tp; 1227 struct tcp_info *ti; 1228 { 1229 1230 INP_LOCK_ASSERT(tp->t_inpcb); 1231 bzero(ti, sizeof(*ti)); 1232 1233 ti->tcpi_state = tp->t_state; 1234 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP)) 1235 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS; 1236 if (tp->sack_enable) 1237 ti->tcpi_options |= TCPI_OPT_SACK; 1238 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) { 1239 ti->tcpi_options |= TCPI_OPT_WSCALE; 1240 ti->tcpi_snd_wscale = tp->snd_scale; 1241 ti->tcpi_rcv_wscale = tp->rcv_scale; 1242 } 1243 ti->tcpi_snd_ssthresh = tp->snd_ssthresh; 1244 ti->tcpi_snd_cwnd = tp->snd_cwnd; 1245 1246 /* 1247 * FreeBSD-specific extension fields for tcp_info. 1248 */ 1249 ti->tcpi_rcv_space = tp->rcv_wnd; 1250 ti->tcpi_snd_wnd = tp->snd_wnd; 1251 ti->tcpi_snd_bwnd = tp->snd_bwnd; 1252 } 1253 1254 /* 1255 * The new sockopt interface makes it possible for us to block in the 1256 * copyin/out step (if we take a page fault). Taking a page fault at 1257 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 1258 * use TSM, there probably isn't any need for this function to run at 1259 * splnet() any more. This needs more examination.) 1260 * 1261 * XXXRW: The locking here is wrong; we may take a page fault while holding 1262 * the inpcb lock. 1263 */ 1264 int 1265 tcp_ctloutput(so, sopt) 1266 struct socket *so; 1267 struct sockopt *sopt; 1268 { 1269 int error, opt, optval; 1270 struct inpcb *inp; 1271 struct tcpcb *tp; 1272 struct tcp_info ti; 1273 1274 error = 0; 1275 inp = sotoinpcb(so); 1276 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL")); 1277 INP_LOCK(inp); 1278 if (sopt->sopt_level != IPPROTO_TCP) { 1279 INP_UNLOCK(inp); 1280 #ifdef INET6 1281 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1282 error = ip6_ctloutput(so, sopt); 1283 else 1284 #endif /* INET6 */ 1285 error = ip_ctloutput(so, sopt); 1286 return (error); 1287 } 1288 if (inp->inp_vflag & (INP_TIMEWAIT | INP_DROPPED)) { 1289 error = ECONNRESET; 1290 goto out; 1291 } 1292 tp = intotcpcb(inp); 1293 1294 switch (sopt->sopt_dir) { 1295 case SOPT_SET: 1296 switch (sopt->sopt_name) { 1297 #ifdef TCP_SIGNATURE 1298 case TCP_MD5SIG: 1299 error = sooptcopyin(sopt, &optval, sizeof optval, 1300 sizeof optval); 1301 if (error) 1302 break; 1303 1304 if (optval > 0) 1305 tp->t_flags |= TF_SIGNATURE; 1306 else 1307 tp->t_flags &= ~TF_SIGNATURE; 1308 break; 1309 #endif /* TCP_SIGNATURE */ 1310 case TCP_NODELAY: 1311 case TCP_NOOPT: 1312 error = sooptcopyin(sopt, &optval, sizeof optval, 1313 sizeof optval); 1314 if (error) 1315 break; 1316 1317 switch (sopt->sopt_name) { 1318 case TCP_NODELAY: 1319 opt = TF_NODELAY; 1320 break; 1321 case TCP_NOOPT: 1322 opt = TF_NOOPT; 1323 break; 1324 default: 1325 opt = 0; /* dead code to fool gcc */ 1326 break; 1327 } 1328 1329 if (optval) 1330 tp->t_flags |= opt; 1331 else 1332 tp->t_flags &= ~opt; 1333 break; 1334 1335 case TCP_NOPUSH: 1336 error = sooptcopyin(sopt, &optval, sizeof optval, 1337 sizeof optval); 1338 if (error) 1339 break; 1340 1341 if (optval) 1342 tp->t_flags |= TF_NOPUSH; 1343 else { 1344 tp->t_flags &= ~TF_NOPUSH; 1345 error = tcp_output(tp); 1346 } 1347 break; 1348 1349 case TCP_MAXSEG: 1350 error = sooptcopyin(sopt, &optval, sizeof optval, 1351 sizeof optval); 1352 if (error) 1353 break; 1354 1355 if (optval > 0 && optval <= tp->t_maxseg && 1356 optval + 40 >= tcp_minmss) 1357 tp->t_maxseg = optval; 1358 else 1359 error = EINVAL; 1360 break; 1361 1362 case TCP_INFO: 1363 error = EINVAL; 1364 break; 1365 1366 default: 1367 error = ENOPROTOOPT; 1368 break; 1369 } 1370 break; 1371 1372 case SOPT_GET: 1373 switch (sopt->sopt_name) { 1374 #ifdef TCP_SIGNATURE 1375 case TCP_MD5SIG: 1376 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1377 error = sooptcopyout(sopt, &optval, sizeof optval); 1378 break; 1379 #endif 1380 case TCP_NODELAY: 1381 optval = tp->t_flags & TF_NODELAY; 1382 error = sooptcopyout(sopt, &optval, sizeof optval); 1383 break; 1384 case TCP_MAXSEG: 1385 optval = tp->t_maxseg; 1386 error = sooptcopyout(sopt, &optval, sizeof optval); 1387 break; 1388 case TCP_NOOPT: 1389 optval = tp->t_flags & TF_NOOPT; 1390 error = sooptcopyout(sopt, &optval, sizeof optval); 1391 break; 1392 case TCP_NOPUSH: 1393 optval = tp->t_flags & TF_NOPUSH; 1394 error = sooptcopyout(sopt, &optval, sizeof optval); 1395 break; 1396 case TCP_INFO: 1397 tcp_fill_info(tp, &ti); 1398 error = sooptcopyout(sopt, &ti, sizeof ti); 1399 break; 1400 default: 1401 error = ENOPROTOOPT; 1402 break; 1403 } 1404 break; 1405 } 1406 out: 1407 INP_UNLOCK(inp); 1408 return (error); 1409 } 1410 1411 /* 1412 * tcp_sendspace and tcp_recvspace are the default send and receive window 1413 * sizes, respectively. These are obsolescent (this information should 1414 * be set by the route). 1415 */ 1416 u_long tcp_sendspace = 1024*32; 1417 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1418 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1419 u_long tcp_recvspace = 1024*64; 1420 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1421 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1422 1423 /* 1424 * Attach TCP protocol to socket, allocating 1425 * internet protocol control block, tcp control block, 1426 * bufer space, and entering LISTEN state if to accept connections. 1427 */ 1428 static int 1429 tcp_attach(so) 1430 struct socket *so; 1431 { 1432 register struct tcpcb *tp; 1433 struct inpcb *inp; 1434 int error; 1435 #ifdef INET6 1436 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1437 #endif 1438 1439 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1440 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1441 if (error) 1442 return (error); 1443 } 1444 INP_INFO_WLOCK(&tcbinfo); 1445 error = in_pcballoc(so, &tcbinfo); 1446 if (error) { 1447 INP_INFO_WUNLOCK(&tcbinfo); 1448 return (error); 1449 } 1450 inp = sotoinpcb(so); 1451 #ifdef INET6 1452 if (isipv6) { 1453 inp->inp_vflag |= INP_IPV6; 1454 inp->in6p_hops = -1; /* use kernel default */ 1455 } 1456 else 1457 #endif 1458 inp->inp_vflag |= INP_IPV4; 1459 tp = tcp_newtcpcb(inp); 1460 if (tp == NULL) { 1461 #ifdef INET6 1462 if (isipv6) { 1463 in6_pcbdetach(inp); 1464 in6_pcbfree(inp); 1465 } else { 1466 #endif 1467 in_pcbdetach(inp); 1468 in_pcbfree(inp); 1469 #ifdef INET6 1470 } 1471 #endif 1472 INP_INFO_WUNLOCK(&tcbinfo); 1473 return (ENOBUFS); 1474 } 1475 tp->t_state = TCPS_CLOSED; 1476 INP_UNLOCK(inp); 1477 INP_INFO_WUNLOCK(&tcbinfo); 1478 return (0); 1479 } 1480 1481 /* 1482 * Initiate (or continue) disconnect. 1483 * If embryonic state, just send reset (once). 1484 * If in ``let data drain'' option and linger null, just drop. 1485 * Otherwise (hard), mark socket disconnecting and drop 1486 * current input data; switch states based on user close, and 1487 * send segment to peer (with FIN). 1488 */ 1489 static void 1490 tcp_disconnect(tp) 1491 register struct tcpcb *tp; 1492 { 1493 struct inpcb *inp = tp->t_inpcb; 1494 struct socket *so = inp->inp_socket; 1495 1496 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1497 INP_LOCK_ASSERT(inp); 1498 1499 /* 1500 * Neither tcp_close() nor tcp_drop() should return NULL, as the 1501 * socket is still open. 1502 */ 1503 if (tp->t_state < TCPS_ESTABLISHED) { 1504 tp = tcp_close(tp); 1505 KASSERT(tp != NULL, 1506 ("tcp_disconnect: tcp_close() returned NULL")); 1507 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) { 1508 tp = tcp_drop(tp, 0); 1509 KASSERT(tp != NULL, 1510 ("tcp_disconnect: tcp_drop() returned NULL")); 1511 } else { 1512 soisdisconnecting(so); 1513 sbflush(&so->so_rcv); 1514 tcp_usrclosed(tp); 1515 if (!(inp->inp_vflag & INP_DROPPED)) 1516 tcp_output(tp); 1517 } 1518 } 1519 1520 /* 1521 * User issued close, and wish to trail through shutdown states: 1522 * if never received SYN, just forget it. If got a SYN from peer, 1523 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1524 * If already got a FIN from peer, then almost done; go to LAST_ACK 1525 * state. In all other cases, have already sent FIN to peer (e.g. 1526 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1527 * for peer to send FIN or not respond to keep-alives, etc. 1528 * We can let the user exit from the close as soon as the FIN is acked. 1529 */ 1530 static void 1531 tcp_usrclosed(tp) 1532 register struct tcpcb *tp; 1533 { 1534 1535 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1536 INP_LOCK_ASSERT(tp->t_inpcb); 1537 1538 switch (tp->t_state) { 1539 1540 case TCPS_CLOSED: 1541 case TCPS_LISTEN: 1542 tp->t_state = TCPS_CLOSED; 1543 tp = tcp_close(tp); 1544 /* 1545 * tcp_close() should never return NULL here as the socket is 1546 * still open. 1547 */ 1548 KASSERT(tp != NULL, 1549 ("tcp_usrclosed: tcp_close() returned NULL")); 1550 break; 1551 1552 case TCPS_SYN_SENT: 1553 case TCPS_SYN_RECEIVED: 1554 tp->t_flags |= TF_NEEDFIN; 1555 break; 1556 1557 case TCPS_ESTABLISHED: 1558 tp->t_state = TCPS_FIN_WAIT_1; 1559 break; 1560 1561 case TCPS_CLOSE_WAIT: 1562 tp->t_state = TCPS_LAST_ACK; 1563 break; 1564 } 1565 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1566 soisdisconnected(tp->t_inpcb->inp_socket); 1567 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1568 if (tp->t_state == TCPS_FIN_WAIT_2) 1569 callout_reset(tp->tt_2msl, tcp_maxidle, 1570 tcp_timer_2msl, tp); 1571 } 1572 } 1573