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