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