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