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