tcp_usrreq.c revision 25201
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 * $Id: tcp_usrreq.c,v 1.30 1997/02/21 16:30:31 wollman Exp $ 35 */ 36 37#include <sys/param.h> 38#include <sys/queue.h> 39#include <sys/systm.h> 40#include <sys/kernel.h> 41#include <sys/sysctl.h> 42#include <sys/malloc.h> 43#include <sys/mbuf.h> 44#include <sys/socket.h> 45#include <sys/socketvar.h> 46#include <sys/protosw.h> 47#include <sys/errno.h> 48#include <sys/stat.h> 49 50#include <net/if.h> 51#include <net/route.h> 52 53#include <netinet/in.h> 54#include <netinet/in_systm.h> 55#include <netinet/ip.h> 56#include <netinet/in_pcb.h> 57#include <netinet/in_var.h> 58#include <netinet/ip_var.h> 59#include <netinet/tcp.h> 60#include <netinet/tcp_fsm.h> 61#include <netinet/tcp_seq.h> 62#include <netinet/tcp_timer.h> 63#include <netinet/tcp_var.h> 64#include <netinet/tcpip.h> 65#ifdef TCPDEBUG 66#include <netinet/tcp_debug.h> 67#endif 68 69/* 70 * TCP protocol interface to socket abstraction. 71 */ 72extern char *tcpstates[]; /* XXX ??? */ 73 74static int tcp_attach __P((struct socket *, struct proc *)); 75static int tcp_connect __P((struct tcpcb *, struct mbuf *, 76 struct proc *)); 77static struct tcpcb * 78 tcp_disconnect __P((struct tcpcb *)); 79static struct tcpcb * 80 tcp_usrclosed __P((struct tcpcb *)); 81 82#ifdef TCPDEBUG 83#define TCPDEBUG0 int ostate 84#define TCPDEBUG1() ostate = tp ? tp->t_state : 0 85#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \ 86 tcp_trace(TA_USER, ostate, tp, 0, req) 87#else 88#define TCPDEBUG0 89#define TCPDEBUG1() 90#define TCPDEBUG2(req) 91#endif 92 93/* 94 * TCP attaches to socket via pru_attach(), reserving space, 95 * and an internet control block. 96 */ 97static int 98tcp_usr_attach(struct socket *so, int proto, struct proc *p) 99{ 100 int s = splnet(); 101 int error; 102 struct inpcb *inp = sotoinpcb(so); 103 struct tcpcb *tp = 0; 104 TCPDEBUG0; 105 106 TCPDEBUG1(); 107 if (inp) { 108 error = EISCONN; 109 goto out; 110 } 111 112 error = tcp_attach(so, p); 113 if (error) 114 goto out; 115 116 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 117 so->so_linger = TCP_LINGERTIME * hz; 118 tp = sototcpcb(so); 119out: 120 TCPDEBUG2(PRU_ATTACH); 121 splx(s); 122 return error; 123} 124 125/* 126 * pru_detach() detaches the TCP protocol from the socket. 127 * If the protocol state is non-embryonic, then can't 128 * do this directly: have to initiate a pru_disconnect(), 129 * which may finish later; embryonic TCB's can just 130 * be discarded here. 131 */ 132static int 133tcp_usr_detach(struct socket *so) 134{ 135 int s = splnet(); 136 int error = 0; 137 struct inpcb *inp = sotoinpcb(so); 138 struct tcpcb *tp; 139 TCPDEBUG0; 140 141 if (inp == 0) { 142 splx(s); 143 return EINVAL; /* XXX */ 144 } 145 tp = intotcpcb(inp); 146 TCPDEBUG1(); 147 if (tp->t_state > TCPS_LISTEN) 148 tp = tcp_disconnect(tp); 149 else 150 tp = tcp_close(tp); 151 152 TCPDEBUG2(PRU_DETACH); 153 splx(s); 154 return error; 155} 156 157#define COMMON_START() TCPDEBUG0; \ 158 do { \ 159 if (inp == 0) { \ 160 splx(s); \ 161 return EINVAL; \ 162 } \ 163 tp = intotcpcb(inp); \ 164 TCPDEBUG1(); \ 165 } while(0) 166 167#define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out 168 169 170/* 171 * Give the socket an address. 172 */ 173static int 174tcp_usr_bind(struct socket *so, struct mbuf *nam, struct proc *p) 175{ 176 int s = splnet(); 177 int error = 0; 178 struct inpcb *inp = sotoinpcb(so); 179 struct tcpcb *tp; 180 struct sockaddr_in *sinp; 181 182 COMMON_START(); 183 184 /* 185 * Must check for multicast addresses and disallow binding 186 * to them. 187 */ 188 sinp = mtod(nam, struct sockaddr_in *); 189 if (sinp->sin_family == AF_INET && 190 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 191 error = EAFNOSUPPORT; 192 goto out; 193 } 194 error = in_pcbbind(inp, nam, p); 195 if (error) 196 goto out; 197 COMMON_END(PRU_BIND); 198 199} 200 201/* 202 * Prepare to accept connections. 203 */ 204static int 205tcp_usr_listen(struct socket *so, struct proc *p) 206{ 207 int s = splnet(); 208 int error = 0; 209 struct inpcb *inp = sotoinpcb(so); 210 struct tcpcb *tp; 211 212 COMMON_START(); 213 if (inp->inp_lport == 0) 214 error = in_pcbbind(inp, (struct mbuf *)0, p); 215 if (error == 0) 216 tp->t_state = TCPS_LISTEN; 217 COMMON_END(PRU_LISTEN); 218} 219 220/* 221 * Initiate connection to peer. 222 * Create a template for use in transmissions on this connection. 223 * Enter SYN_SENT state, and mark socket as connecting. 224 * Start keep-alive timer, and seed output sequence space. 225 * Send initial segment on connection. 226 */ 227static int 228tcp_usr_connect(struct socket *so, struct mbuf *nam, struct proc *p) 229{ 230 int s = splnet(); 231 int error = 0; 232 struct inpcb *inp = sotoinpcb(so); 233 struct tcpcb *tp; 234 struct sockaddr_in *sinp; 235 236 COMMON_START(); 237 238 /* 239 * Must disallow TCP ``connections'' to multicast addresses. 240 */ 241 sinp = mtod(nam, struct sockaddr_in *); 242 if (sinp->sin_family == AF_INET 243 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) { 244 error = EAFNOSUPPORT; 245 goto out; 246 } 247 248 if ((error = tcp_connect(tp, nam, p)) != 0) 249 goto out; 250 error = tcp_output(tp); 251 COMMON_END(PRU_CONNECT); 252} 253 254/* 255 * Initiate disconnect from peer. 256 * If connection never passed embryonic stage, just drop; 257 * else if don't need to let data drain, then can just drop anyways, 258 * else have to begin TCP shutdown process: mark socket disconnecting, 259 * drain unread data, state switch to reflect user close, and 260 * send segment (e.g. FIN) to peer. Socket will be really disconnected 261 * when peer sends FIN and acks ours. 262 * 263 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 264 */ 265static int 266tcp_usr_disconnect(struct socket *so) 267{ 268 int s = splnet(); 269 int error = 0; 270 struct inpcb *inp = sotoinpcb(so); 271 struct tcpcb *tp; 272 273 COMMON_START(); 274 tp = tcp_disconnect(tp); 275 COMMON_END(PRU_DISCONNECT); 276} 277 278/* 279 * Accept a connection. Essentially all the work is 280 * done at higher levels; just return the address 281 * of the peer, storing through addr. 282 */ 283static int 284tcp_usr_accept(struct socket *so, struct mbuf *nam) 285{ 286 int s = splnet(); 287 int error = 0; 288 struct inpcb *inp = sotoinpcb(so); 289 struct tcpcb *tp; 290 291 COMMON_START(); 292 in_setpeeraddr(so, nam); 293 COMMON_END(PRU_ACCEPT); 294} 295 296/* 297 * Mark the connection as being incapable of further output. 298 */ 299static int 300tcp_usr_shutdown(struct socket *so) 301{ 302 int s = splnet(); 303 int error = 0; 304 struct inpcb *inp = sotoinpcb(so); 305 struct tcpcb *tp; 306 307 COMMON_START(); 308 socantsendmore(so); 309 tp = tcp_usrclosed(tp); 310 if (tp) 311 error = tcp_output(tp); 312 COMMON_END(PRU_SHUTDOWN); 313} 314 315/* 316 * After a receive, possibly send window update to peer. 317 */ 318static int 319tcp_usr_rcvd(struct socket *so, int flags) 320{ 321 int s = splnet(); 322 int error = 0; 323 struct inpcb *inp = sotoinpcb(so); 324 struct tcpcb *tp; 325 326 COMMON_START(); 327 tcp_output(tp); 328 COMMON_END(PRU_RCVD); 329} 330 331/* 332 * Do a send by putting data in output queue and updating urgent 333 * marker if URG set. Possibly send more data. 334 */ 335static int 336tcp_usr_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam, 337 struct mbuf *control, struct proc *p) 338{ 339 int s = splnet(); 340 int error = 0; 341 struct inpcb *inp = sotoinpcb(so); 342 struct tcpcb *tp; 343 344 COMMON_START(); 345 if (control && control->m_len) { 346 m_freem(control); /* XXX shouldn't caller do this??? */ 347 if (m) 348 m_freem(m); 349 return EINVAL; 350 } 351 352 if(!(flags & PRUS_OOB)) { 353 sbappend(&so->so_snd, m); 354 if (nam && tp->t_state < TCPS_SYN_SENT) { 355 /* 356 * Do implied connect if not yet connected, 357 * initialize window to default value, and 358 * initialize maxseg/maxopd using peer's cached 359 * MSS. 360 */ 361 error = tcp_connect(tp, nam, p); 362 if (error) 363 goto out; 364 tp->snd_wnd = TTCP_CLIENT_SND_WND; 365 tcp_mss(tp, -1); 366 } 367 368 if (flags & PRUS_EOF) { 369 /* 370 * Close the send side of the connection after 371 * the data is sent. 372 */ 373 socantsendmore(so); 374 tp = tcp_usrclosed(tp); 375 } 376 if (tp != NULL) 377 error = tcp_output(tp); 378 } else { 379 if (sbspace(&so->so_snd) < -512) { 380 m_freem(m); 381 error = ENOBUFS; 382 goto out; 383 } 384 /* 385 * According to RFC961 (Assigned Protocols), 386 * the urgent pointer points to the last octet 387 * of urgent data. We continue, however, 388 * to consider it to indicate the first octet 389 * of data past the urgent section. 390 * Otherwise, snd_up should be one lower. 391 */ 392 sbappend(&so->so_snd, m); 393 if (nam && tp->t_state < TCPS_SYN_SENT) { 394 /* 395 * Do implied connect if not yet connected, 396 * initialize window to default value, and 397 * initialize maxseg/maxopd using peer's cached 398 * MSS. 399 */ 400 error = tcp_connect(tp, nam, p); 401 if (error) 402 goto out; 403 tp->snd_wnd = TTCP_CLIENT_SND_WND; 404 tcp_mss(tp, -1); 405 } 406 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 407 tp->t_force = 1; 408 error = tcp_output(tp); 409 tp->t_force = 0; 410 } 411 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 412 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND)); 413} 414 415/* 416 * Abort the TCP. 417 */ 418static int 419tcp_usr_abort(struct socket *so) 420{ 421 int s = splnet(); 422 int error = 0; 423 struct inpcb *inp = sotoinpcb(so); 424 struct tcpcb *tp; 425 426 COMMON_START(); 427 tp = tcp_drop(tp, ECONNABORTED); 428 COMMON_END(PRU_ABORT); 429} 430 431/* 432 * Receive out-of-band data. 433 */ 434static int 435tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags) 436{ 437 int s = splnet(); 438 int error = 0; 439 struct inpcb *inp = sotoinpcb(so); 440 struct tcpcb *tp; 441 442 COMMON_START(); 443 if ((so->so_oobmark == 0 && 444 (so->so_state & SS_RCVATMARK) == 0) || 445 so->so_options & SO_OOBINLINE || 446 tp->t_oobflags & TCPOOB_HADDATA) { 447 error = EINVAL; 448 goto out; 449 } 450 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 451 error = EWOULDBLOCK; 452 goto out; 453 } 454 m->m_len = 1; 455 *mtod(m, caddr_t) = tp->t_iobc; 456 if ((flags & MSG_PEEK) == 0) 457 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 458 COMMON_END(PRU_RCVOOB); 459} 460 461/* xxx - should be const */ 462struct pr_usrreqs tcp_usrreqs = { 463 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind, 464 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach, 465 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd, 466 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown, 467 in_setsockaddr, sosend, soreceive, soselect 468}; 469 470/* 471 * Common subroutine to open a TCP connection to remote host specified 472 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local 473 * port number if needed. Call in_pcbladdr to do the routing and to choose 474 * a local host address (interface). If there is an existing incarnation 475 * of the same connection in TIME-WAIT state and if the remote host was 476 * sending CC options and if the connection duration was < MSL, then 477 * truncate the previous TIME-WAIT state and proceed. 478 * Initialize connection parameters and enter SYN-SENT state. 479 */ 480static int 481tcp_connect(tp, nam, p) 482 register struct tcpcb *tp; 483 struct mbuf *nam; 484 struct proc *p; 485{ 486 struct inpcb *inp = tp->t_inpcb, *oinp; 487 struct socket *so = inp->inp_socket; 488 struct tcpcb *otp; 489 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *); 490 struct sockaddr_in *ifaddr; 491 int error; 492 struct rmxp_tao *taop; 493 struct rmxp_tao tao_noncached; 494 495 if (inp->inp_lport == 0) { 496 error = in_pcbbind(inp, (struct mbuf *)0, p); 497 if (error) 498 return error; 499 } 500 501 /* 502 * Cannot simply call in_pcbconnect, because there might be an 503 * earlier incarnation of this same connection still in 504 * TIME_WAIT state, creating an ADDRINUSE error. 505 */ 506 error = in_pcbladdr(inp, nam, &ifaddr); 507 if (error) 508 return error; 509 oinp = in_pcblookuphash(inp->inp_pcbinfo, 510 sin->sin_addr, sin->sin_port, 511 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr 512 : ifaddr->sin_addr, 513 inp->inp_lport, 0); 514 if (oinp) { 515 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL && 516 otp->t_state == TCPS_TIME_WAIT && 517 otp->t_duration < TCPTV_MSL && 518 (otp->t_flags & TF_RCVD_CC)) 519 otp = tcp_close(otp); 520 else 521 return EADDRINUSE; 522 } 523 if (inp->inp_laddr.s_addr == INADDR_ANY) 524 inp->inp_laddr = ifaddr->sin_addr; 525 inp->inp_faddr = sin->sin_addr; 526 inp->inp_fport = sin->sin_port; 527 in_pcbrehash(inp); 528 529 tp->t_template = tcp_template(tp); 530 if (tp->t_template == 0) { 531 in_pcbdisconnect(inp); 532 return ENOBUFS; 533 } 534 535 /* Compute window scaling to request. */ 536 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 537 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 538 tp->request_r_scale++; 539 540 soisconnecting(so); 541 tcpstat.tcps_connattempt++; 542 tp->t_state = TCPS_SYN_SENT; 543 tp->t_timer[TCPT_KEEP] = tcp_keepinit; 544 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 545 tcp_sendseqinit(tp); 546 547 /* 548 * Generate a CC value for this connection and 549 * check whether CC or CCnew should be used. 550 */ 551 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { 552 taop = &tao_noncached; 553 bzero(taop, sizeof(*taop)); 554 } 555 556 tp->cc_send = CC_INC(tcp_ccgen); 557 if (taop->tao_ccsent != 0 && 558 CC_GEQ(tp->cc_send, taop->tao_ccsent)) { 559 taop->tao_ccsent = tp->cc_send; 560 } else { 561 taop->tao_ccsent = 0; 562 tp->t_flags |= TF_SENDCCNEW; 563 } 564 565 return 0; 566} 567 568int 569tcp_ctloutput(op, so, level, optname, mp, p) 570 int op; 571 struct socket *so; 572 int level, optname; 573 struct mbuf **mp; 574 struct proc *p; 575{ 576 int error = 0, s; 577 struct inpcb *inp; 578 register struct tcpcb *tp; 579 register struct mbuf *m; 580 register int i; 581 582 s = splnet(); 583 inp = sotoinpcb(so); 584 if (inp == NULL) { 585 splx(s); 586 if (op == PRCO_SETOPT && *mp) 587 (void) m_free(*mp); 588 return (ECONNRESET); 589 } 590 if (level != IPPROTO_TCP) { 591 error = ip_ctloutput(op, so, level, optname, mp, p); 592 splx(s); 593 return (error); 594 } 595 tp = intotcpcb(inp); 596 597 switch (op) { 598 599 case PRCO_SETOPT: 600 m = *mp; 601 switch (optname) { 602 603 case TCP_NODELAY: 604 if (m == NULL || m->m_len < sizeof (int)) 605 error = EINVAL; 606 else if (*mtod(m, int *)) 607 tp->t_flags |= TF_NODELAY; 608 else 609 tp->t_flags &= ~TF_NODELAY; 610 break; 611 612 case TCP_MAXSEG: 613 if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg) 614 tp->t_maxseg = i; 615 else 616 error = EINVAL; 617 break; 618 619 case TCP_NOOPT: 620 if (m == NULL || m->m_len < sizeof (int)) 621 error = EINVAL; 622 else if (*mtod(m, int *)) 623 tp->t_flags |= TF_NOOPT; 624 else 625 tp->t_flags &= ~TF_NOOPT; 626 break; 627 628 case TCP_NOPUSH: 629 if (m == NULL || m->m_len < sizeof (int)) 630 error = EINVAL; 631 else if (*mtod(m, int *)) 632 tp->t_flags |= TF_NOPUSH; 633 else 634 tp->t_flags &= ~TF_NOPUSH; 635 break; 636 637 default: 638 error = ENOPROTOOPT; 639 break; 640 } 641 if (m) 642 (void) m_free(m); 643 break; 644 645 case PRCO_GETOPT: 646 *mp = m = m_get(M_WAIT, MT_SOOPTS); 647 m->m_len = sizeof(int); 648 649 switch (optname) { 650 case TCP_NODELAY: 651 *mtod(m, int *) = tp->t_flags & TF_NODELAY; 652 break; 653 case TCP_MAXSEG: 654 *mtod(m, int *) = tp->t_maxseg; 655 break; 656 case TCP_NOOPT: 657 *mtod(m, int *) = tp->t_flags & TF_NOOPT; 658 break; 659 case TCP_NOPUSH: 660 *mtod(m, int *) = tp->t_flags & TF_NOPUSH; 661 break; 662 default: 663 error = ENOPROTOOPT; 664 break; 665 } 666 break; 667 } 668 splx(s); 669 return (error); 670} 671 672/* 673 * tcp_sendspace and tcp_recvspace are the default send and receive window 674 * sizes, respectively. These are obsolescent (this information should 675 * be set by the route). 676 */ 677u_long tcp_sendspace = 1024*16; 678SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, 679 CTLFLAG_RW, &tcp_sendspace , 0, ""); 680u_long tcp_recvspace = 1024*16; 681SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, 682 CTLFLAG_RW, &tcp_recvspace , 0, ""); 683 684/* 685 * Attach TCP protocol to socket, allocating 686 * internet protocol control block, tcp control block, 687 * bufer space, and entering LISTEN state if to accept connections. 688 */ 689static int 690tcp_attach(so, p) 691 struct socket *so; 692 struct proc *p; 693{ 694 register struct tcpcb *tp; 695 struct inpcb *inp; 696 int error; 697 698 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 699 error = soreserve(so, tcp_sendspace, tcp_recvspace); 700 if (error) 701 return (error); 702 } 703 error = in_pcballoc(so, &tcbinfo, p); 704 if (error) 705 return (error); 706 inp = sotoinpcb(so); 707 tp = tcp_newtcpcb(inp); 708 if (tp == 0) { 709 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 710 711 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 712 in_pcbdetach(inp); 713 so->so_state |= nofd; 714 return (ENOBUFS); 715 } 716 tp->t_state = TCPS_CLOSED; 717 return (0); 718} 719 720/* 721 * Initiate (or continue) disconnect. 722 * If embryonic state, just send reset (once). 723 * If in ``let data drain'' option and linger null, just drop. 724 * Otherwise (hard), mark socket disconnecting and drop 725 * current input data; switch states based on user close, and 726 * send segment to peer (with FIN). 727 */ 728static struct tcpcb * 729tcp_disconnect(tp) 730 register struct tcpcb *tp; 731{ 732 struct socket *so = tp->t_inpcb->inp_socket; 733 734 if (tp->t_state < TCPS_ESTABLISHED) 735 tp = tcp_close(tp); 736 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 737 tp = tcp_drop(tp, 0); 738 else { 739 soisdisconnecting(so); 740 sbflush(&so->so_rcv); 741 tp = tcp_usrclosed(tp); 742 if (tp) 743 (void) tcp_output(tp); 744 } 745 return (tp); 746} 747 748/* 749 * User issued close, and wish to trail through shutdown states: 750 * if never received SYN, just forget it. If got a SYN from peer, 751 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 752 * If already got a FIN from peer, then almost done; go to LAST_ACK 753 * state. In all other cases, have already sent FIN to peer (e.g. 754 * after PRU_SHUTDOWN), and just have to play tedious game waiting 755 * for peer to send FIN or not respond to keep-alives, etc. 756 * We can let the user exit from the close as soon as the FIN is acked. 757 */ 758static struct tcpcb * 759tcp_usrclosed(tp) 760 register struct tcpcb *tp; 761{ 762 763 switch (tp->t_state) { 764 765 case TCPS_CLOSED: 766 case TCPS_LISTEN: 767 tp->t_state = TCPS_CLOSED; 768 tp = tcp_close(tp); 769 break; 770 771 case TCPS_SYN_SENT: 772 case TCPS_SYN_RECEIVED: 773 tp->t_flags |= TF_NEEDFIN; 774 break; 775 776 case TCPS_ESTABLISHED: 777 tp->t_state = TCPS_FIN_WAIT_1; 778 break; 779 780 case TCPS_CLOSE_WAIT: 781 tp->t_state = TCPS_LAST_ACK; 782 break; 783 } 784 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 785 soisdisconnected(tp->t_inpcb->inp_socket); 786 /* To prevent the connection hanging in FIN_WAIT_2 forever. */ 787 if (tp->t_state == TCPS_FIN_WAIT_2) 788 tp->t_timer[TCPT_2MSL] = tcp_maxidle; 789 } 790 return (tp); 791} 792 793