1 /* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94 30 * $FreeBSD$ 31 */ 32 33 #include "opt_ipsec.h" 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_random_ip_id.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 #endif 71 #include <netinet/tcp.h> 72 #include <netinet/tcp_fsm.h> 73 #include <netinet/tcp_seq.h> 74 #include <netinet/tcp_timer.h> 75 #include <netinet/tcp_var.h> 76 #include <netinet/tcpip.h> 77 #ifdef TCPDEBUG 78 #include <netinet/tcp_debug.h> 79 #endif 80 81 #ifdef IPSEC 82 #include <netinet6/ipsec.h> 83 #endif /*IPSEC*/ 84 85 /* 86 * TCP protocol interface to socket abstraction. 87 */ 88 extern char *tcpstates[]; /* XXX ??? */ 89 90 static int tcp_attach(struct socket *); 91 static int tcp_connect(struct tcpcb *, struct sockaddr *, 92 struct thread *td); 93 #ifdef INET6 94 static int tcp6_connect(struct tcpcb *, struct sockaddr *, 95 struct thread *td); 96 #endif /* INET6 */ 97 static struct tcpcb * 98 tcp_disconnect(struct tcpcb *); 99 static struct tcpcb * 100 tcp_usrclosed(struct tcpcb *); 101 102 #ifdef TCPDEBUG 103 #define TCPDEBUG0 int ostate = 0 104 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0 105 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 106 tcp_trace(TA_USER, ostate, tp, 0, 0, req) 107 #else 108 #define TCPDEBUG0 109 #define TCPDEBUG1() 110 #define TCPDEBUG2(req) 111 #endif 112 113 /* 114 * TCP attaches to socket via pru_attach(), reserving space, 115 * and an internet control block. 116 */ 117 static int 118 tcp_usr_attach(struct socket *so, int proto, struct thread *td) 119 { 120 int error; 121 struct inpcb *inp; 122 struct tcpcb *tp = 0; 123 TCPDEBUG0; 124 125 INP_INFO_WLOCK(&tcbinfo); 126 TCPDEBUG1(); 127 inp = sotoinpcb(so); 128 if (inp) { 129 error = EISCONN; 130 goto out; 131 } 132 133 error = tcp_attach(so); 134 if (error) 135 goto out; 136 137 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 138 so->so_linger = TCP_LINGERTIME; 139 140 inp = sotoinpcb(so); 141 tp = intotcpcb(inp); 142 out: 143 TCPDEBUG2(PRU_ATTACH); 144 INP_INFO_WUNLOCK(&tcbinfo); 145 return error; 146 } 147 148 /* 149 * pru_detach() detaches the TCP protocol from the socket. 150 * If the protocol state is non-embryonic, then can't 151 * do this directly: have to initiate a pru_disconnect(), 152 * which may finish later; embryonic TCB's can just 153 * be discarded here. 154 */ 155 static int 156 tcp_usr_detach(struct socket *so) 157 { 158 int error = 0; 159 struct inpcb *inp; 160 struct tcpcb *tp; 161 TCPDEBUG0; 162 163 INP_INFO_WLOCK(&tcbinfo); 164 inp = sotoinpcb(so); 165 if (inp == NULL) { 166 INP_INFO_WUNLOCK(&tcbinfo); 167 return error; 168 } 169 INP_LOCK(inp); 170 tp = intotcpcb(inp); 171 TCPDEBUG1(); 172 tp = tcp_disconnect(tp); 173 174 TCPDEBUG2(PRU_DETACH); 175 if (tp) 176 INP_UNLOCK(inp); 177 INP_INFO_WUNLOCK(&tcbinfo); 178 return error; 179 } 180 181 #define INI_NOLOCK 0 182 #define INI_READ 1 183 #define INI_WRITE 2 184 185 #define COMMON_START() \ 186 TCPDEBUG0; \ 187 do { \ 188 if (inirw == INI_READ) \ 189 INP_INFO_RLOCK(&tcbinfo); \ 190 else if (inirw == INI_WRITE) \ 191 INP_INFO_WLOCK(&tcbinfo); \ 192 inp = sotoinpcb(so); \ 193 if (inp == 0) { \ 194 if (inirw == INI_READ) \ 195 INP_INFO_RUNLOCK(&tcbinfo); \ 196 else if (inirw == INI_WRITE) \ 197 INP_INFO_WUNLOCK(&tcbinfo); \ 198 return EINVAL; \ 199 } \ 200 INP_LOCK(inp); \ 201 if (inirw == INI_READ) \ 202 INP_INFO_RUNLOCK(&tcbinfo); \ 203 tp = intotcpcb(inp); \ 204 TCPDEBUG1(); \ 205 } while(0) 206 207 #define COMMON_END(req) \ 208 out: TCPDEBUG2(req); \ 209 do { \ 210 if (tp) \ 211 INP_UNLOCK(inp); \ 212 if (inirw == INI_WRITE) \ 213 INP_INFO_WUNLOCK(&tcbinfo); \ 214 return error; \ 215 goto out; \ 216 } while(0) 217 218 /* 219 * Give the socket an address. 220 */ 221 static int 222 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 223 { 224 int error = 0; 225 struct inpcb *inp; 226 struct tcpcb *tp; 227 struct sockaddr_in *sinp; 228 const int inirw = INI_WRITE; 229 230 sinp = (struct sockaddr_in *)nam; 231 if (nam->sa_len != sizeof (*sinp)) 232 return (EINVAL); 233 /* 234 * Must check for multicast addresses and disallow binding 235 * to them. 236 */ 237 if (sinp->sin_family == AF_INET && 238 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 239 return (EAFNOSUPPORT); 240 241 COMMON_START(); 242 error = in_pcbbind(inp, nam, td->td_ucred); 243 if (error) 244 goto out; 245 COMMON_END(PRU_BIND); 246 } 247 248 #ifdef INET6 249 static int 250 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 251 { 252 int error = 0; 253 struct inpcb *inp; 254 struct tcpcb *tp; 255 struct sockaddr_in6 *sin6p; 256 const int inirw = INI_WRITE; 257 258 sin6p = (struct sockaddr_in6 *)nam; 259 if (nam->sa_len != sizeof (*sin6p)) 260 return (EINVAL); 261 /* 262 * Must check for multicast addresses and disallow binding 263 * to them. 264 */ 265 if (sin6p->sin6_family == AF_INET6 && 266 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 267 return (EAFNOSUPPORT); 268 269 COMMON_START(); 270 inp->inp_vflag &= ~INP_IPV4; 271 inp->inp_vflag |= INP_IPV6; 272 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 273 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) 274 inp->inp_vflag |= INP_IPV4; 275 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 276 struct sockaddr_in sin; 277 278 in6_sin6_2_sin(&sin, sin6p); 279 inp->inp_vflag |= INP_IPV4; 280 inp->inp_vflag &= ~INP_IPV6; 281 error = in_pcbbind(inp, (struct sockaddr *)&sin, 282 td->td_ucred); 283 goto out; 284 } 285 } 286 error = in6_pcbbind(inp, nam, td->td_ucred); 287 if (error) 288 goto out; 289 COMMON_END(PRU_BIND); 290 } 291 #endif /* INET6 */ 292 293 /* 294 * Prepare to accept connections. 295 */ 296 static int 297 tcp_usr_listen(struct socket *so, struct thread *td) 298 { 299 int error = 0; 300 struct inpcb *inp; 301 struct tcpcb *tp; 302 const int inirw = INI_WRITE; 303 304 COMMON_START(); 305 if (inp->inp_lport == 0) 306 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 307 if (error == 0) 308 tp->t_state = TCPS_LISTEN; 309 COMMON_END(PRU_LISTEN); 310 } 311 312 #ifdef INET6 313 static int 314 tcp6_usr_listen(struct socket *so, struct thread *td) 315 { 316 int error = 0; 317 struct inpcb *inp; 318 struct tcpcb *tp; 319 const int inirw = INI_WRITE; 320 321 COMMON_START(); 322 if (inp->inp_lport == 0) { 323 inp->inp_vflag &= ~INP_IPV4; 324 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 325 inp->inp_vflag |= INP_IPV4; 326 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 327 } 328 if (error == 0) 329 tp->t_state = TCPS_LISTEN; 330 COMMON_END(PRU_LISTEN); 331 } 332 #endif /* INET6 */ 333 334 /* 335 * Initiate connection to peer. 336 * Create a template for use in transmissions on this connection. 337 * Enter SYN_SENT state, and mark socket as connecting. 338 * Start keep-alive timer, and seed output sequence space. 339 * Send initial segment on connection. 340 */ 341 static int 342 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 343 { 344 int error = 0; 345 struct inpcb *inp; 346 struct tcpcb *tp; 347 struct sockaddr_in *sinp; 348 const int inirw = INI_WRITE; 349 350 sinp = (struct sockaddr_in *)nam; 351 if (nam->sa_len != sizeof (*sinp)) 352 return (EINVAL); 353 /* 354 * Must disallow TCP ``connections'' to multicast addresses. 355 */ 356 if (sinp->sin_family == AF_INET 357 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) 358 return (EAFNOSUPPORT); 359 if (td && jailed(td->td_ucred)) 360 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr); 361 362 COMMON_START(); 363 if ((error = tcp_connect(tp, nam, td)) != 0) 364 goto out; 365 error = tcp_output(tp); 366 COMMON_END(PRU_CONNECT); 367 } 368 369 #ifdef INET6 370 static int 371 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 372 { 373 int error = 0; 374 struct inpcb *inp; 375 struct tcpcb *tp; 376 struct sockaddr_in6 *sin6p; 377 const int inirw = INI_WRITE; 378 379 sin6p = (struct sockaddr_in6 *)nam; 380 if (nam->sa_len != sizeof (*sin6p)) 381 return (EINVAL); 382 /* 383 * Must disallow TCP ``connections'' to multicast addresses. 384 */ 385 if (sin6p->sin6_family == AF_INET6 386 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) 387 return (EAFNOSUPPORT); 388 389 COMMON_START(); 390 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) { 391 struct sockaddr_in sin; 392 393 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) { 394 error = EINVAL; 395 goto out; 396 } 397 398 in6_sin6_2_sin(&sin, sin6p); 399 inp->inp_vflag |= INP_IPV4; 400 inp->inp_vflag &= ~INP_IPV6; 401 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0) 402 goto out; 403 error = tcp_output(tp); 404 goto out; 405 } 406 inp->inp_vflag &= ~INP_IPV4; 407 inp->inp_vflag |= INP_IPV6; 408 inp->inp_inc.inc_isipv6 = 1; 409 if ((error = tcp6_connect(tp, nam, td)) != 0) 410 goto out; 411 error = tcp_output(tp); 412 COMMON_END(PRU_CONNECT); 413 } 414 #endif /* INET6 */ 415 416 /* 417 * Initiate disconnect from peer. 418 * If connection never passed embryonic stage, just drop; 419 * else if don't need to let data drain, then can just drop anyways, 420 * else have to begin TCP shutdown process: mark socket disconnecting, 421 * drain unread data, state switch to reflect user close, and 422 * send segment (e.g. FIN) to peer. Socket will be really disconnected 423 * when peer sends FIN and acks ours. 424 * 425 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 426 */ 427 static int 428 tcp_usr_disconnect(struct socket *so) 429 { 430 int error = 0; 431 struct inpcb *inp; 432 struct tcpcb *tp; 433 const int inirw = INI_WRITE; 434 435 COMMON_START(); 436 tp = tcp_disconnect(tp); 437 COMMON_END(PRU_DISCONNECT); 438 } 439 440 /* 441 * Accept a connection. Essentially all the work is 442 * done at higher levels; just return the address 443 * of the peer, storing through addr. 444 */ 445 static int 446 tcp_usr_accept(struct socket *so, struct sockaddr **nam) 447 { 448 int error = 0; 449 struct inpcb *inp = NULL; 450 struct tcpcb *tp = NULL; 451 struct in_addr addr; 452 in_port_t port = 0; 453 TCPDEBUG0; 454 455 if (so->so_state & SS_ISDISCONNECTED) { 456 error = ECONNABORTED; 457 goto out; 458 } 459 460 INP_INFO_RLOCK(&tcbinfo); 461 inp = sotoinpcb(so); 462 if (!inp) { 463 INP_INFO_RUNLOCK(&tcbinfo); 464 return (EINVAL); 465 } 466 INP_LOCK(inp); 467 INP_INFO_RUNLOCK(&tcbinfo); 468 tp = intotcpcb(inp); 469 TCPDEBUG1(); 470 471 /* 472 * We inline in_setpeeraddr and COMMON_END here, so that we can 473 * copy the data of interest and defer the malloc until after we 474 * release the lock. 475 */ 476 port = inp->inp_fport; 477 addr = inp->inp_faddr; 478 479 out: TCPDEBUG2(PRU_ACCEPT); 480 if (tp) 481 INP_UNLOCK(inp); 482 if (error == 0) 483 *nam = in_sockaddr(port, &addr); 484 return error; 485 } 486 487 #ifdef INET6 488 static int 489 tcp6_usr_accept(struct socket *so, struct sockaddr **nam) 490 { 491 struct inpcb *inp = NULL; 492 int error = 0; 493 struct tcpcb *tp = NULL; 494 struct in_addr addr; 495 struct in6_addr addr6; 496 in_port_t port = 0; 497 int v4 = 0; 498 TCPDEBUG0; 499 500 if (so->so_state & SS_ISDISCONNECTED) { 501 error = ECONNABORTED; 502 goto out; 503 } 504 505 INP_INFO_RLOCK(&tcbinfo); 506 inp = sotoinpcb(so); 507 if (inp == 0) { 508 INP_INFO_RUNLOCK(&tcbinfo); 509 return (EINVAL); 510 } 511 INP_LOCK(inp); 512 INP_INFO_RUNLOCK(&tcbinfo); 513 tp = intotcpcb(inp); 514 TCPDEBUG1(); 515 /* 516 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can 517 * copy the data of interest and defer the malloc until after we 518 * release the lock. 519 */ 520 if (inp->inp_vflag & INP_IPV4) { 521 v4 = 1; 522 port = inp->inp_fport; 523 addr = inp->inp_faddr; 524 } else { 525 port = inp->inp_fport; 526 addr6 = inp->in6p_faddr; 527 } 528 529 out: TCPDEBUG2(PRU_ACCEPT); 530 if (tp) 531 INP_UNLOCK(inp); 532 if (error == 0) { 533 if (v4) 534 *nam = in6_v4mapsin6_sockaddr(port, &addr); 535 else 536 *nam = in6_sockaddr(port, &addr6); 537 } 538 return error; 539 } 540 #endif /* INET6 */ 541 542 /* 543 * This is the wrapper function for in_setsockaddr. We just pass down 544 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking 545 * here because in_setsockaddr will call malloc and can block. 546 */ 547 static int 548 tcp_sockaddr(struct socket *so, struct sockaddr **nam) 549 { 550 return (in_setsockaddr(so, nam, &tcbinfo)); 551 } 552 553 /* 554 * This is the wrapper function for in_setpeeraddr. We just pass down 555 * the pcbinfo for in_setpeeraddr to lock. 556 */ 557 static int 558 tcp_peeraddr(struct socket *so, struct sockaddr **nam) 559 { 560 return (in_setpeeraddr(so, nam, &tcbinfo)); 561 } 562 563 /* 564 * Mark the connection as being incapable of further output. 565 */ 566 static int 567 tcp_usr_shutdown(struct socket *so) 568 { 569 int error = 0; 570 struct inpcb *inp; 571 struct tcpcb *tp; 572 const int inirw = INI_WRITE; 573 574 COMMON_START(); 575 socantsendmore(so); 576 tp = tcp_usrclosed(tp); 577 if (tp) 578 error = tcp_output(tp); 579 COMMON_END(PRU_SHUTDOWN); 580 } 581 582 /* 583 * After a receive, possibly send window update to peer. 584 */ 585 static int 586 tcp_usr_rcvd(struct socket *so, int flags) 587 { 588 int error = 0; 589 struct inpcb *inp; 590 struct tcpcb *tp; 591 const int inirw = INI_READ; 592 593 COMMON_START(); 594 tcp_output(tp); 595 COMMON_END(PRU_RCVD); 596 } 597 598 /* 599 * Do a send by putting data in output queue and updating urgent 600 * marker if URG set. Possibly send more data. Unlike the other 601 * pru_*() routines, the mbuf chains are our responsibility. We 602 * must either enqueue them or free them. The other pru_* routines 603 * generally are caller-frees. 604 */ 605 static int 606 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 607 struct sockaddr *nam, struct mbuf *control, struct thread *td) 608 { 609 int error = 0; 610 struct inpcb *inp; 611 struct tcpcb *tp; 612 const int inirw = INI_WRITE; 613 #ifdef INET6 614 int isipv6; 615 #endif 616 TCPDEBUG0; 617 618 /* 619 * Need write lock here because this function might call 620 * tcp_connect or tcp_usrclosed. 621 * We really want to have to this function upgrade from read lock 622 * to write lock. XXX 623 */ 624 INP_INFO_WLOCK(&tcbinfo); 625 inp = sotoinpcb(so); 626 if (inp == NULL) { 627 /* 628 * OOPS! we lost a race, the TCP session got reset after 629 * we checked SBS_CANTSENDMORE, eg: while doing uiomove or a 630 * network interrupt in the non-splnet() section of sosend(). 631 */ 632 if (m) 633 m_freem(m); 634 if (control) 635 m_freem(control); 636 error = ECONNRESET; /* XXX EPIPE? */ 637 tp = NULL; 638 TCPDEBUG1(); 639 goto out; 640 } 641 INP_LOCK(inp); 642 #ifdef INET6 643 isipv6 = nam && nam->sa_family == AF_INET6; 644 #endif /* INET6 */ 645 tp = intotcpcb(inp); 646 TCPDEBUG1(); 647 if (control) { 648 /* TCP doesn't do control messages (rights, creds, etc) */ 649 if (control->m_len) { 650 m_freem(control); 651 if (m) 652 m_freem(m); 653 error = EINVAL; 654 goto out; 655 } 656 m_freem(control); /* empty control, just free it */ 657 } 658 if (!(flags & PRUS_OOB)) { 659 sbappendstream(&so->so_snd, m); 660 if (nam && tp->t_state < TCPS_SYN_SENT) { 661 /* 662 * Do implied connect if not yet connected, 663 * initialize window to default value, and 664 * initialize maxseg/maxopd using peer's cached 665 * MSS. 666 */ 667 #ifdef INET6 668 if (isipv6) 669 error = tcp6_connect(tp, nam, td); 670 else 671 #endif /* INET6 */ 672 error = tcp_connect(tp, nam, td); 673 if (error) 674 goto out; 675 tp->snd_wnd = TTCP_CLIENT_SND_WND; 676 tcp_mss(tp, -1); 677 } 678 679 if (flags & PRUS_EOF) { 680 /* 681 * Close the send side of the connection after 682 * the data is sent. 683 */ 684 socantsendmore(so); 685 tp = tcp_usrclosed(tp); 686 } 687 if (tp != NULL) { 688 if (flags & PRUS_MORETOCOME) 689 tp->t_flags |= TF_MORETOCOME; 690 error = tcp_output(tp); 691 if (flags & PRUS_MORETOCOME) 692 tp->t_flags &= ~TF_MORETOCOME; 693 } 694 } else { 695 if (sbspace(&so->so_snd) < -512) { 696 m_freem(m); 697 error = ENOBUFS; 698 goto out; 699 } 700 /* 701 * According to RFC961 (Assigned Protocols), 702 * the urgent pointer points to the last octet 703 * of urgent data. We continue, however, 704 * to consider it to indicate the first octet 705 * of data past the urgent section. 706 * Otherwise, snd_up should be one lower. 707 */ 708 sbappendstream(&so->so_snd, m); 709 if (nam && tp->t_state < TCPS_SYN_SENT) { 710 /* 711 * Do implied connect if not yet connected, 712 * initialize window to default value, and 713 * initialize maxseg/maxopd using peer's cached 714 * MSS. 715 */ 716 #ifdef INET6 717 if (isipv6) 718 error = tcp6_connect(tp, nam, td); 719 else 720 #endif /* INET6 */ 721 error = tcp_connect(tp, nam, td); 722 if (error) 723 goto out; 724 tp->snd_wnd = TTCP_CLIENT_SND_WND; 725 tcp_mss(tp, -1); 726 } 727 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 728 tp->t_force = 1; 729 error = tcp_output(tp); 730 tp->t_force = 0; 731 } 732 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 733 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 734 } 735 736 /* 737 * Abort the TCP. 738 */ 739 static int 740 tcp_usr_abort(struct socket *so) 741 { 742 int error = 0; 743 struct inpcb *inp; 744 struct tcpcb *tp; 745 const int inirw = INI_WRITE; 746 747 COMMON_START(); 748 tp = tcp_drop(tp, ECONNABORTED); 749 COMMON_END(PRU_ABORT); 750 } 751 752 /* 753 * Receive out-of-band data. 754 */ 755 static int 756 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 757 { 758 int error = 0; 759 struct inpcb *inp; 760 struct tcpcb *tp; 761 const int inirw = INI_READ; 762 763 COMMON_START(); 764 if ((so->so_oobmark == 0 && 765 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) || 766 so->so_options & SO_OOBINLINE || 767 tp->t_oobflags & TCPOOB_HADDATA) { 768 error = EINVAL; 769 goto out; 770 } 771 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 772 error = EWOULDBLOCK; 773 goto out; 774 } 775 m->m_len = 1; 776 *mtod(m, caddr_t) = tp->t_iobc; 777 if ((flags & MSG_PEEK) == 0) 778 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 779 COMMON_END(PRU_RCVOOB); 780 } 781 782 /* xxx - should be const */ 783 struct pr_usrreqs tcp_usrreqs = { 784 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 785 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, 786 tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd, 787 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 788 tcp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 789 }; 790 791 #ifdef INET6 792 struct pr_usrreqs tcp6_usrreqs = { 793 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind, 794 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach, 795 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd, 796 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 797 in6_mapped_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 798 }; 799 #endif /* INET6 */ 800 801 /* 802 * Common subroutine to open a TCP connection to remote host specified 803 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 804 * port number if needed. Call in_pcbconnect_setup to do the routing and 805 * to choose a local host address (interface). If there is an existing 806 * incarnation of the same connection in TIME-WAIT state and if the remote 807 * host was sending CC options and if the connection duration was < MSL, then 808 * truncate the previous TIME-WAIT state and proceed. 809 * Initialize connection parameters and enter SYN-SENT state. 810 */ 811 static int 812 tcp_connect(tp, nam, td) 813 register struct tcpcb *tp; 814 struct sockaddr *nam; 815 struct thread *td; 816 { 817 struct inpcb *inp = tp->t_inpcb, *oinp; 818 struct socket *so = inp->inp_socket; 819 struct tcptw *otw; 820 struct rmxp_tao tao; 821 struct in_addr laddr; 822 u_short lport; 823 int error; 824 825 bzero(&tao, sizeof(tao)); 826 827 if (inp->inp_lport == 0) { 828 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 829 if (error) 830 return error; 831 } 832 833 /* 834 * Cannot simply call in_pcbconnect, because there might be an 835 * earlier incarnation of this same connection still in 836 * TIME_WAIT state, creating an ADDRINUSE error. 837 */ 838 laddr = inp->inp_laddr; 839 lport = inp->inp_lport; 840 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport, 841 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred); 842 if (error && oinp == NULL) 843 return error; 844 if (oinp) { 845 if (oinp != inp && 846 (oinp->inp_vflag & INP_TIMEWAIT) && 847 (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl && 848 otw->cc_recv != 0) { 849 inp->inp_faddr = oinp->inp_faddr; 850 inp->inp_fport = oinp->inp_fport; 851 (void) tcp_twclose(otw, 0); 852 } else 853 return EADDRINUSE; 854 } 855 inp->inp_laddr = laddr; 856 in_pcbrehash(inp); 857 858 /* Compute window scaling to request. */ 859 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 860 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 861 tp->request_r_scale++; 862 863 soisconnecting(so); 864 tcpstat.tcps_connattempt++; 865 tp->t_state = TCPS_SYN_SENT; 866 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 867 tp->iss = tcp_new_isn(tp); 868 tp->t_bw_rtseq = tp->iss; 869 tcp_sendseqinit(tp); 870 871 /* 872 * Generate a CC value for this connection and 873 * check whether CC or CCnew should be used. 874 */ 875 if (tcp_do_rfc1644) 876 tcp_hc_gettao(&inp->inp_inc, &tao); 877 878 tp->cc_send = CC_INC(tcp_ccgen); 879 if (tao.tao_ccsent != 0 && 880 CC_GEQ(tp->cc_send, tao.tao_ccsent)) { 881 tao.tao_ccsent = tp->cc_send; 882 } else { 883 tao.tao_ccsent = 0; 884 tp->t_flags |= TF_SENDCCNEW; 885 } 886 887 if (tcp_do_rfc1644) 888 tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, 889 tao.tao_ccsent, 0); 890 891 return 0; 892 } 893 894 #ifdef INET6 895 static int 896 tcp6_connect(tp, nam, td) 897 register struct tcpcb *tp; 898 struct sockaddr *nam; 899 struct thread *td; 900 { 901 struct inpcb *inp = tp->t_inpcb, *oinp; 902 struct socket *so = inp->inp_socket; 903 struct tcptw *otw; 904 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; 905 struct in6_addr *addr6; 906 struct rmxp_tao tao; 907 int error; 908 909 bzero(&tao, sizeof(tao)); 910 911 if (inp->inp_lport == 0) { 912 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred); 913 if (error) 914 return error; 915 } 916 917 /* 918 * Cannot simply call in_pcbconnect, because there might be an 919 * earlier incarnation of this same connection still in 920 * TIME_WAIT state, creating an ADDRINUSE error. 921 */ 922 error = in6_pcbladdr(inp, nam, &addr6); 923 if (error) 924 return error; 925 oinp = in6_pcblookup_hash(inp->inp_pcbinfo, 926 &sin6->sin6_addr, sin6->sin6_port, 927 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) 928 ? addr6 929 : &inp->in6p_laddr, 930 inp->inp_lport, 0, NULL); 931 if (oinp) { 932 if (oinp != inp && 933 (oinp->inp_vflag & INP_TIMEWAIT) && 934 (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl && 935 otw->cc_recv != 0) { 936 inp->inp_faddr = oinp->inp_faddr; 937 inp->inp_fport = oinp->inp_fport; 938 (void) tcp_twclose(otw, 0); 939 } else 940 return EADDRINUSE; 941 } 942 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) 943 inp->in6p_laddr = *addr6; 944 inp->in6p_faddr = sin6->sin6_addr; 945 inp->inp_fport = sin6->sin6_port; 946 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */ 947 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 948 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL) 949 inp->in6p_flowinfo |= 950 #ifdef RANDOM_IP_ID 951 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK); 952 #else 953 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 954 #endif 955 in_pcbrehash(inp); 956 957 /* Compute window scaling to request. */ 958 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 959 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 960 tp->request_r_scale++; 961 962 soisconnecting(so); 963 tcpstat.tcps_connattempt++; 964 tp->t_state = TCPS_SYN_SENT; 965 callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp); 966 tp->iss = tcp_new_isn(tp); 967 tp->t_bw_rtseq = tp->iss; 968 tcp_sendseqinit(tp); 969 970 /* 971 * Generate a CC value for this connection and 972 * check whether CC or CCnew should be used. 973 */ 974 if (tcp_do_rfc1644) 975 tcp_hc_gettao(&inp->inp_inc, &tao); 976 977 tp->cc_send = CC_INC(tcp_ccgen); 978 if (tao.tao_ccsent != 0 && 979 CC_GEQ(tp->cc_send, tao.tao_ccsent)) { 980 tao.tao_ccsent = tp->cc_send; 981 } else { 982 tao.tao_ccsent = 0; 983 tp->t_flags |= TF_SENDCCNEW; 984 } 985 if (tcp_do_rfc1644) 986 tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, 987 tao.tao_ccsent, 0); 988 989 return 0; 990 } 991 #endif /* INET6 */ 992 993 /* 994 * The new sockopt interface makes it possible for us to block in the 995 * copyin/out step (if we take a page fault). Taking a page fault at 996 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now 997 * use TSM, there probably isn't any need for this function to run at 998 * splnet() any more. This needs more examination.) 999 */ 1000 int 1001 tcp_ctloutput(so, sopt) 1002 struct socket *so; 1003 struct sockopt *sopt; 1004 { 1005 int error, opt, optval; 1006 struct inpcb *inp; 1007 struct tcpcb *tp; 1008 1009 error = 0; 1010 INP_INFO_RLOCK(&tcbinfo); 1011 inp = sotoinpcb(so); 1012 if (inp == NULL) { 1013 INP_INFO_RUNLOCK(&tcbinfo); 1014 return (ECONNRESET); 1015 } 1016 INP_LOCK(inp); 1017 INP_INFO_RUNLOCK(&tcbinfo); 1018 if (sopt->sopt_level != IPPROTO_TCP) { 1019 INP_UNLOCK(inp); 1020 #ifdef INET6 1021 if (INP_CHECK_SOCKAF(so, AF_INET6)) 1022 error = ip6_ctloutput(so, sopt); 1023 else 1024 #endif /* INET6 */ 1025 error = ip_ctloutput(so, sopt); 1026 return (error); 1027 } 1028 tp = intotcpcb(inp); 1029 1030 switch (sopt->sopt_dir) { 1031 case SOPT_SET: 1032 switch (sopt->sopt_name) { 1033 #ifdef TCP_SIGNATURE 1034 case TCP_MD5SIG: 1035 error = sooptcopyin(sopt, &optval, sizeof optval, 1036 sizeof optval); 1037 if (error) 1038 break; 1039 1040 if (optval > 0) 1041 tp->t_flags |= TF_SIGNATURE; 1042 else 1043 tp->t_flags &= ~TF_SIGNATURE; 1044 break; 1045 #endif /* TCP_SIGNATURE */ 1046 case TCP_NODELAY: 1047 case TCP_NOOPT: 1048 error = sooptcopyin(sopt, &optval, sizeof optval, 1049 sizeof optval); 1050 if (error) 1051 break; 1052 1053 switch (sopt->sopt_name) { 1054 case TCP_NODELAY: 1055 opt = TF_NODELAY; 1056 break; 1057 case TCP_NOOPT: 1058 opt = TF_NOOPT; 1059 break; 1060 default: 1061 opt = 0; /* dead code to fool gcc */ 1062 break; 1063 } 1064 1065 if (optval) 1066 tp->t_flags |= opt; 1067 else 1068 tp->t_flags &= ~opt; 1069 break; 1070 1071 case TCP_NOPUSH: 1072 error = sooptcopyin(sopt, &optval, sizeof optval, 1073 sizeof optval); 1074 if (error) 1075 break; 1076 1077 if (optval) 1078 tp->t_flags |= TF_NOPUSH; 1079 else { 1080 tp->t_flags &= ~TF_NOPUSH; 1081 error = tcp_output(tp); 1082 } 1083 break; 1084 1085 case TCP_MAXSEG: 1086 error = sooptcopyin(sopt, &optval, sizeof optval, 1087 sizeof optval); 1088 if (error) 1089 break; 1090 1091 if (optval > 0 && optval <= tp->t_maxseg && 1092 optval + 40 >= tcp_minmss) 1093 tp->t_maxseg = optval; 1094 else 1095 error = EINVAL; 1096 break; 1097 1098 default: 1099 error = ENOPROTOOPT; 1100 break; 1101 } 1102 break; 1103 1104 case SOPT_GET: 1105 switch (sopt->sopt_name) { 1106 #ifdef TCP_SIGNATURE 1107 case TCP_MD5SIG: 1108 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 1109 break; 1110 #endif 1111 case TCP_NODELAY: 1112 optval = tp->t_flags & TF_NODELAY; 1113 break; 1114 case TCP_MAXSEG: 1115 optval = tp->t_maxseg; 1116 break; 1117 case TCP_NOOPT: 1118 optval = tp->t_flags & TF_NOOPT; 1119 break; 1120 case TCP_NOPUSH: 1121 optval = tp->t_flags & TF_NOPUSH; 1122 break; 1123 default: 1124 error = ENOPROTOOPT; 1125 break; 1126 } 1127 if (error == 0) 1128 error = sooptcopyout(sopt, &optval, sizeof optval); 1129 break; 1130 } 1131 INP_UNLOCK(inp); 1132 return (error); 1133 } 1134 1135 /* 1136 * tcp_sendspace and tcp_recvspace are the default send and receive window 1137 * sizes, respectively. These are obsolescent (this information should 1138 * be set by the route). 1139 */ 1140 u_long tcp_sendspace = 1024*32; 1141 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 1142 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size"); 1143 u_long tcp_recvspace = 1024*64; 1144 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1145 &tcp_recvspace , 0, "Maximum incoming TCP datagram size"); 1146 1147 /* 1148 * Attach TCP protocol to socket, allocating 1149 * internet protocol control block, tcp control block, 1150 * bufer space, and entering LISTEN state if to accept connections. 1151 */ 1152 static int 1153 tcp_attach(so) 1154 struct socket *so; 1155 { 1156 register struct tcpcb *tp; 1157 struct inpcb *inp; 1158 int error; 1159 #ifdef INET6 1160 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0; 1161 #endif 1162 1163 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1164 error = soreserve(so, tcp_sendspace, tcp_recvspace); 1165 if (error) 1166 return (error); 1167 } 1168 error = in_pcballoc(so, &tcbinfo, "tcpinp"); 1169 if (error) 1170 return (error); 1171 inp = sotoinpcb(so); 1172 #ifdef INET6 1173 if (isipv6) { 1174 inp->inp_vflag |= INP_IPV6; 1175 inp->in6p_hops = -1; /* use kernel default */ 1176 } 1177 else 1178 #endif 1179 inp->inp_vflag |= INP_IPV4; 1180 tp = tcp_newtcpcb(inp); 1181 if (tp == 0) { 1182 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 1183 1184 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 1185 #ifdef INET6 1186 if (isipv6) 1187 in6_pcbdetach(inp); 1188 else 1189 #endif 1190 in_pcbdetach(inp); 1191 so->so_state |= nofd; 1192 return (ENOBUFS); 1193 } 1194 tp->t_state = TCPS_CLOSED; 1195 return (0); 1196 } 1197 1198 /* 1199 * Initiate (or continue) disconnect. 1200 * If embryonic state, just send reset (once). 1201 * If in ``let data drain'' option and linger null, just drop. 1202 * Otherwise (hard), mark socket disconnecting and drop 1203 * current input data; switch states based on user close, and 1204 * send segment to peer (with FIN). 1205 */ 1206 static struct tcpcb * 1207 tcp_disconnect(tp) 1208 register struct tcpcb *tp; 1209 { 1210 struct socket *so = tp->t_inpcb->inp_socket; 1211 1212 if (tp->t_state < TCPS_ESTABLISHED) 1213 tp = tcp_close(tp); 1214 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 1215 tp = tcp_drop(tp, 0); 1216 else { 1217 soisdisconnecting(so); 1218 sbflush(&so->so_rcv); 1219 tp = tcp_usrclosed(tp); 1220 if (tp) 1221 (void) tcp_output(tp); 1222 } 1223 return (tp); 1224 } 1225 1226 /* 1227 * User issued close, and wish to trail through shutdown states: 1228 * if never received SYN, just forget it. If got a SYN from peer, 1229 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 1230 * If already got a FIN from peer, then almost done; go to LAST_ACK 1231 * state. In all other cases, have already sent FIN to peer (e.g. 1232 * after PRU_SHUTDOWN), and just have to play tedious game waiting 1233 * for peer to send FIN or not respond to keep-alives, etc. 1234 * We can let the user exit from the close as soon as the FIN is acked. 1235 */ 1236 static struct tcpcb * 1237 tcp_usrclosed(tp) 1238 register struct tcpcb *tp; 1239 { 1240 1241 switch (tp->t_state) { 1242 1243 case TCPS_CLOSED: 1244 case TCPS_LISTEN: 1245 tp->t_state = TCPS_CLOSED; 1246 tp = tcp_close(tp); 1247 break; 1248 1249 case TCPS_SYN_SENT: 1250 case TCPS_SYN_RECEIVED: 1251 tp->t_flags |= TF_NEEDFIN; 1252 break; 1253 1254 case TCPS_ESTABLISHED: 1255 tp->t_state = TCPS_FIN_WAIT_1; 1256 break; 1257 1258 case TCPS_CLOSE_WAIT: 1259 tp->t_state = TCPS_LAST_ACK; 1260 break; 1261 } 1262 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 1263 soisdisconnected(tp->t_inpcb->inp_socket); 1264 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 1265 if (tp->t_state == TCPS_FIN_WAIT_2) 1266 callout_reset(tp->tt_2msl, tcp_maxidle, 1267 tcp_timer_2msl, tp); 1268 } 1269 return (tp); 1270 } 1271 1272