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