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