1 /* $OpenBSD: tcp_usrreq.c,v 1.99 2008/05/24 19:48:32 thib Exp $ */ 2 /* $NetBSD: tcp_usrreq.c,v 1.20 1996/02/13 23:44:16 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mbuf.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/protosw.h> 77 #include <sys/stat.h> 78 #include <sys/sysctl.h> 79 #include <sys/domain.h> 80 #include <sys/kernel.h> 81 #include <sys/pool.h> 82 83 #include <dev/rndvar.h> 84 85 #include <net/if.h> 86 #include <net/route.h> 87 88 #include <netinet/in.h> 89 #include <netinet/in_systm.h> 90 #include <netinet/in_var.h> 91 #include <netinet/ip.h> 92 #include <netinet/in_pcb.h> 93 #include <netinet/ip_var.h> 94 #include <netinet/tcp.h> 95 #include <netinet/tcp_fsm.h> 96 #include <netinet/tcp_seq.h> 97 #include <netinet/tcp_timer.h> 98 #include <netinet/tcp_var.h> 99 #include <netinet/tcpip.h> 100 #include <netinet/tcp_debug.h> 101 102 /* 103 * TCP protocol interface to socket abstraction. 104 */ 105 extern char *tcpstates[]; 106 extern int tcptv_keep_init; 107 108 extern int tcp_rst_ppslim; 109 110 /* from in_pcb.c */ 111 extern struct baddynamicports baddynamicports; 112 113 #ifndef TCP_SENDSPACE 114 #define TCP_SENDSPACE 1024*16 115 #endif 116 u_int tcp_sendspace = TCP_SENDSPACE; 117 #ifndef TCP_RECVSPACE 118 #define TCP_RECVSPACE 1024*16 119 #endif 120 u_int tcp_recvspace = TCP_RECVSPACE; 121 122 int *tcpctl_vars[TCPCTL_MAXID] = TCPCTL_VARS; 123 124 struct inpcbtable tcbtable; 125 126 int tcp_ident(void *, size_t *, void *, size_t, int); 127 128 /* 129 * Process a TCP user request for TCP tb. If this is a send request 130 * then m is the mbuf chain of send data. If this is a timer expiration 131 * (called from the software clock routine), then timertype tells which timer. 132 */ 133 /*ARGSUSED*/ 134 int 135 tcp_usrreq(so, req, m, nam, control, p) 136 struct socket *so; 137 int req; 138 struct mbuf *m, *nam, *control; 139 struct proc *p; 140 { 141 struct sockaddr_in *sin; 142 struct inpcb *inp; 143 struct tcpcb *tp = NULL; 144 int s; 145 int error = 0; 146 short ostate; 147 148 if (req == PRU_CONTROL) { 149 #ifdef INET6 150 if (sotopf(so) == PF_INET6) 151 return in6_control(so, (u_long)m, (caddr_t)nam, 152 (struct ifnet *)control, 0); 153 else 154 #endif /* INET6 */ 155 return (in_control(so, (u_long)m, (caddr_t)nam, 156 (struct ifnet *)control)); 157 } 158 if (control && control->m_len) { 159 m_freem(control); 160 if (m) 161 m_freem(m); 162 return (EINVAL); 163 } 164 165 s = splsoftnet(); 166 inp = sotoinpcb(so); 167 /* 168 * When a TCP is attached to a socket, then there will be 169 * a (struct inpcb) pointed at by the socket, and this 170 * structure will point at a subsidiary (struct tcpcb). 171 */ 172 if (inp == 0 && req != PRU_ATTACH) { 173 error = so->so_error; 174 if (error == 0) 175 error = EINVAL; 176 splx(s); 177 /* 178 * The following corrects an mbuf leak under rare 179 * circumstances 180 */ 181 if (m && (req == PRU_SEND || req == PRU_SENDOOB)) 182 m_freem(m); 183 return (error); 184 } 185 if (inp) { 186 tp = intotcpcb(inp); 187 /* WHAT IF TP IS 0? */ 188 #ifdef KPROF 189 tcp_acounts[tp->t_state][req]++; 190 #endif 191 ostate = tp->t_state; 192 } else 193 ostate = 0; 194 switch (req) { 195 196 /* 197 * TCP attaches to socket via PRU_ATTACH, reserving space, 198 * and an internet control block. 199 */ 200 case PRU_ATTACH: 201 if (inp) { 202 error = EISCONN; 203 break; 204 } 205 error = tcp_attach(so); 206 if (error) 207 break; 208 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 209 so->so_linger = TCP_LINGERTIME; 210 tp = sototcpcb(so); 211 break; 212 213 /* 214 * PRU_DETACH detaches the TCP protocol from the socket. 215 * If the protocol state is non-embryonic, then can't 216 * do this directly: have to initiate a PRU_DISCONNECT, 217 * which may finish later; embryonic TCB's can just 218 * be discarded here. 219 */ 220 case PRU_DETACH: 221 tp = tcp_disconnect(tp); 222 break; 223 224 /* 225 * Give the socket an address. 226 */ 227 case PRU_BIND: 228 #ifdef INET6 229 if (inp->inp_flags & INP_IPV6) 230 error = in6_pcbbind(inp, nam, p); 231 else 232 #endif 233 error = in_pcbbind(inp, nam, p); 234 if (error) 235 break; 236 break; 237 238 /* 239 * Prepare to accept connections. 240 */ 241 case PRU_LISTEN: 242 if (inp->inp_lport == 0) { 243 #ifdef INET6 244 if (inp->inp_flags & INP_IPV6) 245 error = in6_pcbbind(inp, NULL, p); 246 else 247 #endif 248 error = in_pcbbind(inp, NULL, p); 249 } 250 /* If the in_pcbbind() above is called, the tp->pf 251 should still be whatever it was before. */ 252 if (error == 0) 253 tp->t_state = TCPS_LISTEN; 254 break; 255 256 /* 257 * Initiate connection to peer. 258 * Create a template for use in transmissions on this connection. 259 * Enter SYN_SENT state, and mark socket as connecting. 260 * Start keep-alive timer, and seed output sequence space. 261 * Send initial segment on connection. 262 */ 263 case PRU_CONNECT: 264 sin = mtod(nam, struct sockaddr_in *); 265 266 #ifdef INET6 267 if (sin->sin_family == AF_INET6) { 268 struct in6_addr *in6_addr = &mtod(nam, 269 struct sockaddr_in6 *)->sin6_addr; 270 271 if (IN6_IS_ADDR_UNSPECIFIED(in6_addr) || 272 IN6_IS_ADDR_MULTICAST(in6_addr) || 273 (IN6_IS_ADDR_V4MAPPED(in6_addr) && 274 ((in6_addr->s6_addr32[3] == INADDR_ANY) || 275 IN_MULTICAST(in6_addr->s6_addr32[3]) || 276 in_broadcast(sin->sin_addr, NULL)))) { 277 error = EINVAL; 278 break; 279 } 280 281 if (inp->inp_lport == 0) { 282 error = in6_pcbbind(inp, NULL, p); 283 if (error) 284 break; 285 } 286 error = in6_pcbconnect(inp, nam); 287 } else if (sin->sin_family == AF_INET) 288 #endif /* INET6 */ 289 { 290 if ((sin->sin_addr.s_addr == INADDR_ANY) || 291 IN_MULTICAST(sin->sin_addr.s_addr) || 292 in_broadcast(sin->sin_addr, NULL)) { 293 error = EINVAL; 294 break; 295 } 296 297 if (inp->inp_lport == 0) { 298 error = in_pcbbind(inp, NULL, p); 299 if (error) 300 break; 301 } 302 error = in_pcbconnect(inp, nam); 303 } 304 305 if (error) 306 break; 307 308 tp->t_template = tcp_template(tp); 309 if (tp->t_template == 0) { 310 in_pcbdisconnect(inp); 311 error = ENOBUFS; 312 break; 313 } 314 315 so->so_state |= SS_CONNECTOUT; 316 317 /* Compute window scaling to request. */ 318 tcp_rscale(tp, so->so_rcv.sb_hiwat); 319 320 soisconnecting(so); 321 tcpstat.tcps_connattempt++; 322 tp->t_state = TCPS_SYN_SENT; 323 TCP_TIMER_ARM(tp, TCPT_KEEP, tcptv_keep_init); 324 tcp_set_iss_tsm(tp); 325 tcp_sendseqinit(tp); 326 #if defined(TCP_SACK) 327 tp->snd_last = tp->snd_una; 328 #endif 329 #if defined(TCP_SACK) && defined(TCP_FACK) 330 tp->snd_fack = tp->snd_una; 331 tp->retran_data = 0; 332 tp->snd_awnd = 0; 333 #endif 334 error = tcp_output(tp); 335 break; 336 337 /* 338 * Create a TCP connection between two sockets. 339 */ 340 case PRU_CONNECT2: 341 error = EOPNOTSUPP; 342 break; 343 344 /* 345 * Initiate disconnect from peer. 346 * If connection never passed embryonic stage, just drop; 347 * else if don't need to let data drain, then can just drop anyways, 348 * else have to begin TCP shutdown process: mark socket disconnecting, 349 * drain unread data, state switch to reflect user close, and 350 * send segment (e.g. FIN) to peer. Socket will be really disconnected 351 * when peer sends FIN and acks ours. 352 * 353 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 354 */ 355 case PRU_DISCONNECT: 356 tp = tcp_disconnect(tp); 357 break; 358 359 /* 360 * Accept a connection. Essentially all the work is 361 * done at higher levels; just return the address 362 * of the peer, storing through addr. 363 */ 364 case PRU_ACCEPT: 365 #ifdef INET6 366 if (inp->inp_flags & INP_IPV6) 367 in6_setpeeraddr(inp, nam); 368 else 369 #endif 370 in_setpeeraddr(inp, nam); 371 break; 372 373 /* 374 * Mark the connection as being incapable of further output. 375 */ 376 case PRU_SHUTDOWN: 377 if (so->so_state & SS_CANTSENDMORE) 378 break; 379 socantsendmore(so); 380 tp = tcp_usrclosed(tp); 381 if (tp) 382 error = tcp_output(tp); 383 break; 384 385 /* 386 * After a receive, possibly send window update to peer. 387 */ 388 case PRU_RCVD: 389 /* 390 * soreceive() calls this function when a user receives 391 * ancillary data on a listening socket. We don't call 392 * tcp_output in such a case, since there is no header 393 * template for a listening socket and hence the kernel 394 * will panic. 395 */ 396 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0) 397 (void) tcp_output(tp); 398 break; 399 400 /* 401 * Do a send by putting data in output queue and updating urgent 402 * marker if URG set. Possibly send more data. 403 */ 404 case PRU_SEND: 405 sbappendstream(&so->so_snd, m); 406 error = tcp_output(tp); 407 break; 408 409 /* 410 * Abort the TCP. 411 */ 412 case PRU_ABORT: 413 tp = tcp_drop(tp, ECONNABORTED); 414 break; 415 416 case PRU_SENSE: 417 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 418 splx(s); 419 return (0); 420 421 case PRU_RCVOOB: 422 if ((so->so_oobmark == 0 && 423 (so->so_state & SS_RCVATMARK) == 0) || 424 so->so_options & SO_OOBINLINE || 425 tp->t_oobflags & TCPOOB_HADDATA) { 426 error = EINVAL; 427 break; 428 } 429 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 430 error = EWOULDBLOCK; 431 break; 432 } 433 m->m_len = 1; 434 *mtod(m, caddr_t) = tp->t_iobc; 435 if (((long)nam & MSG_PEEK) == 0) 436 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 437 break; 438 439 case PRU_SENDOOB: 440 if (sbspace(&so->so_snd) < -512) { 441 m_freem(m); 442 error = ENOBUFS; 443 break; 444 } 445 /* 446 * According to RFC961 (Assigned Protocols), 447 * the urgent pointer points to the last octet 448 * of urgent data. We continue, however, 449 * to consider it to indicate the first octet 450 * of data past the urgent section. 451 * Otherwise, snd_up should be one lower. 452 */ 453 sbappendstream(&so->so_snd, m); 454 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 455 tp->t_force = 1; 456 error = tcp_output(tp); 457 tp->t_force = 0; 458 break; 459 460 case PRU_SOCKADDR: 461 #ifdef INET6 462 if (inp->inp_flags & INP_IPV6) 463 in6_setsockaddr(inp, nam); 464 else 465 #endif 466 in_setsockaddr(inp, nam); 467 break; 468 469 case PRU_PEERADDR: 470 #ifdef INET6 471 if (inp->inp_flags & INP_IPV6) 472 in6_setpeeraddr(inp, nam); 473 else 474 #endif 475 in_setpeeraddr(inp, nam); 476 break; 477 478 default: 479 panic("tcp_usrreq"); 480 } 481 if (tp && (so->so_options & SO_DEBUG)) 482 tcp_trace(TA_USER, ostate, tp, (caddr_t)0, req, 0); 483 splx(s); 484 return (error); 485 } 486 487 int 488 tcp_ctloutput(op, so, level, optname, mp) 489 int op; 490 struct socket *so; 491 int level, optname; 492 struct mbuf **mp; 493 { 494 int error = 0, s; 495 struct inpcb *inp; 496 struct tcpcb *tp; 497 struct mbuf *m; 498 int i; 499 500 s = splsoftnet(); 501 inp = sotoinpcb(so); 502 if (inp == NULL) { 503 splx(s); 504 if (op == PRCO_SETOPT && *mp) 505 (void) m_free(*mp); 506 return (ECONNRESET); 507 } 508 #ifdef INET6 509 tp = intotcpcb(inp); 510 #endif /* INET6 */ 511 if (level != IPPROTO_TCP) { 512 switch (so->so_proto->pr_domain->dom_family) { 513 #ifdef INET6 514 case PF_INET6: 515 error = ip6_ctloutput(op, so, level, optname, mp); 516 break; 517 #endif /* INET6 */ 518 case PF_INET: 519 error = ip_ctloutput(op, so, level, optname, mp); 520 break; 521 default: 522 error = EAFNOSUPPORT; /*?*/ 523 break; 524 } 525 splx(s); 526 return (error); 527 } 528 #ifndef INET6 529 tp = intotcpcb(inp); 530 #endif /* !INET6 */ 531 532 switch (op) { 533 534 case PRCO_SETOPT: 535 m = *mp; 536 switch (optname) { 537 538 case TCP_NODELAY: 539 if (m == NULL || m->m_len < sizeof (int)) 540 error = EINVAL; 541 else if (*mtod(m, int *)) 542 tp->t_flags |= TF_NODELAY; 543 else 544 tp->t_flags &= ~TF_NODELAY; 545 break; 546 547 case TCP_MAXSEG: 548 if (m == NULL || m->m_len < sizeof (int)) { 549 error = EINVAL; 550 break; 551 } 552 553 i = *mtod(m, int *); 554 if (i > 0 && i <= tp->t_maxseg) 555 tp->t_maxseg = i; 556 else 557 error = EINVAL; 558 break; 559 560 #ifdef TCP_SACK 561 case TCP_SACK_ENABLE: 562 if (m == NULL || m->m_len < sizeof (int)) { 563 error = EINVAL; 564 break; 565 } 566 567 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 568 error = EPERM; 569 break; 570 } 571 572 if (tp->t_flags & TF_SIGNATURE) { 573 error = EPERM; 574 break; 575 } 576 577 if (*mtod(m, int *)) 578 tp->sack_enable = 1; 579 else 580 tp->sack_enable = 0; 581 break; 582 #endif 583 #ifdef TCP_SIGNATURE 584 case TCP_MD5SIG: 585 if (m == NULL || m->m_len < sizeof (int)) { 586 error = EINVAL; 587 break; 588 } 589 590 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 591 error = EPERM; 592 break; 593 } 594 595 if (*mtod(m, int *)) { 596 tp->t_flags |= TF_SIGNATURE; 597 #ifdef TCP_SACK 598 tp->sack_enable = 0; 599 #endif /* TCP_SACK */ 600 } else 601 tp->t_flags &= ~TF_SIGNATURE; 602 break; 603 #endif /* TCP_SIGNATURE */ 604 default: 605 error = ENOPROTOOPT; 606 break; 607 } 608 if (m) 609 (void) m_free(m); 610 break; 611 612 case PRCO_GETOPT: 613 *mp = m = m_get(M_WAIT, MT_SOOPTS); 614 m->m_len = sizeof(int); 615 616 switch (optname) { 617 case TCP_NODELAY: 618 *mtod(m, int *) = tp->t_flags & TF_NODELAY; 619 break; 620 case TCP_MAXSEG: 621 *mtod(m, int *) = tp->t_maxseg; 622 break; 623 #ifdef TCP_SACK 624 case TCP_SACK_ENABLE: 625 *mtod(m, int *) = tp->sack_enable; 626 break; 627 #endif 628 #ifdef TCP_SIGNATURE 629 case TCP_MD5SIG: 630 *mtod(m, int *) = tp->t_flags & TF_SIGNATURE; 631 break; 632 #endif 633 default: 634 error = ENOPROTOOPT; 635 break; 636 } 637 break; 638 } 639 splx(s); 640 return (error); 641 } 642 643 /* 644 * Attach TCP protocol to socket, allocating 645 * internet protocol control block, tcp control block, 646 * bufer space, and entering LISTEN state if to accept connections. 647 */ 648 int 649 tcp_attach(so) 650 struct socket *so; 651 { 652 struct tcpcb *tp; 653 struct inpcb *inp; 654 int error; 655 656 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 657 error = soreserve(so, tcp_sendspace, tcp_recvspace); 658 if (error) 659 return (error); 660 } 661 error = in_pcballoc(so, &tcbtable); 662 if (error) 663 return (error); 664 inp = sotoinpcb(so); 665 tp = tcp_newtcpcb(inp); 666 if (tp == NULL) { 667 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 668 669 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 670 in_pcbdetach(inp); 671 so->so_state |= nofd; 672 return (ENOBUFS); 673 } 674 tp->t_state = TCPS_CLOSED; 675 #ifdef INET6 676 /* we disallow IPv4 mapped address completely. */ 677 if (inp->inp_flags & INP_IPV6) 678 tp->pf = PF_INET6; 679 else 680 tp->pf = PF_INET; 681 #else 682 tp->pf = PF_INET; 683 #endif 684 return (0); 685 } 686 687 /* 688 * Initiate (or continue) disconnect. 689 * If embryonic state, just send reset (once). 690 * If in ``let data drain'' option and linger null, just drop. 691 * Otherwise (hard), mark socket disconnecting and drop 692 * current input data; switch states based on user close, and 693 * send segment to peer (with FIN). 694 */ 695 struct tcpcb * 696 tcp_disconnect(tp) 697 struct tcpcb *tp; 698 { 699 struct socket *so = tp->t_inpcb->inp_socket; 700 701 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 702 tp = tcp_close(tp); 703 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 704 tp = tcp_drop(tp, 0); 705 else { 706 soisdisconnecting(so); 707 sbflush(&so->so_rcv); 708 tp = tcp_usrclosed(tp); 709 if (tp) 710 (void) tcp_output(tp); 711 } 712 return (tp); 713 } 714 715 /* 716 * User issued close, and wish to trail through shutdown states: 717 * if never received SYN, just forget it. If got a SYN from peer, 718 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 719 * If already got a FIN from peer, then almost done; go to LAST_ACK 720 * state. In all other cases, have already sent FIN to peer (e.g. 721 * after PRU_SHUTDOWN), and just have to play tedious game waiting 722 * for peer to send FIN or not respond to keep-alives, etc. 723 * We can let the user exit from the close as soon as the FIN is acked. 724 */ 725 struct tcpcb * 726 tcp_usrclosed(tp) 727 struct tcpcb *tp; 728 { 729 730 switch (tp->t_state) { 731 732 case TCPS_CLOSED: 733 case TCPS_LISTEN: 734 case TCPS_SYN_SENT: 735 tp->t_state = TCPS_CLOSED; 736 tp = tcp_close(tp); 737 break; 738 739 case TCPS_SYN_RECEIVED: 740 case TCPS_ESTABLISHED: 741 tp->t_state = TCPS_FIN_WAIT_1; 742 break; 743 744 case TCPS_CLOSE_WAIT: 745 tp->t_state = TCPS_LAST_ACK; 746 break; 747 } 748 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 749 soisdisconnected(tp->t_inpcb->inp_socket); 750 /* 751 * If we are in FIN_WAIT_2, we arrived here because the 752 * application did a shutdown of the send side. Like the 753 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after 754 * a full close, we start a timer to make sure sockets are 755 * not left in FIN_WAIT_2 forever. 756 */ 757 if (tp->t_state == TCPS_FIN_WAIT_2) 758 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle); 759 } 760 return (tp); 761 } 762 763 /* 764 * Look up a socket for ident or tcpdrop, ... 765 */ 766 int 767 tcp_ident(oldp, oldlenp, newp, newlen, dodrop) 768 void *oldp; 769 size_t *oldlenp; 770 void *newp; 771 size_t newlen; 772 int dodrop; 773 { 774 int error = 0, s; 775 struct tcp_ident_mapping tir; 776 struct inpcb *inp; 777 struct tcpcb *tp = NULL; 778 struct sockaddr_in *fin, *lin; 779 #ifdef INET6 780 struct sockaddr_in6 *fin6, *lin6; 781 struct in6_addr f6, l6; 782 #endif 783 if (dodrop) { 784 if (oldp != NULL || *oldlenp != 0) 785 return (EINVAL); 786 if (newp == NULL) 787 return (EPERM); 788 if (newlen < sizeof(tir)) 789 return (ENOMEM); 790 if ((error = copyin(newp, &tir, sizeof (tir))) != 0 ) 791 return (error); 792 } else { 793 if (oldp == NULL) 794 return (EINVAL); 795 if (*oldlenp < sizeof(tir)) 796 return (ENOMEM); 797 if (newp != NULL || newlen != 0) 798 return (EINVAL); 799 if ((error = copyin(oldp, &tir, sizeof (tir))) != 0 ) 800 return (error); 801 } 802 switch (tir.faddr.ss_family) { 803 #ifdef INET6 804 case AF_INET6: 805 fin6 = (struct sockaddr_in6 *)&tir.faddr; 806 error = in6_embedscope(&f6, fin6, NULL, NULL); 807 if (error) 808 return EINVAL; /*?*/ 809 lin6 = (struct sockaddr_in6 *)&tir.laddr; 810 error = in6_embedscope(&l6, lin6, NULL, NULL); 811 if (error) 812 return EINVAL; /*?*/ 813 break; 814 #endif 815 case AF_INET: 816 fin = (struct sockaddr_in *)&tir.faddr; 817 lin = (struct sockaddr_in *)&tir.laddr; 818 break; 819 default: 820 return (EINVAL); 821 } 822 823 s = splsoftnet(); 824 switch (tir.faddr.ss_family) { 825 #ifdef INET6 826 case AF_INET6: 827 inp = in6_pcbhashlookup(&tcbtable, &f6, 828 fin6->sin6_port, &l6, lin6->sin6_port); 829 break; 830 #endif 831 case AF_INET: 832 inp = in_pcbhashlookup(&tcbtable, fin->sin_addr, 833 fin->sin_port, lin->sin_addr, lin->sin_port); 834 break; 835 } 836 837 if (dodrop) { 838 if (inp && (tp = intotcpcb(inp)) && 839 ((inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) 840 tp = tcp_drop(tp, ECONNABORTED); 841 else 842 error = ESRCH; 843 splx(s); 844 return (error); 845 } 846 847 if (inp == NULL) { 848 ++tcpstat.tcps_pcbhashmiss; 849 switch (tir.faddr.ss_family) { 850 #ifdef INET6 851 case AF_INET6: 852 inp = in6_pcblookup_listen(&tcbtable, 853 &l6, lin6->sin6_port, 0, NULL); 854 break; 855 #endif 856 case AF_INET: 857 inp = in_pcblookup_listen(&tcbtable, 858 lin->sin_addr, lin->sin_port, 0, NULL); 859 break; 860 } 861 } 862 863 if (inp != NULL && (inp->inp_socket->so_state & SS_CONNECTOUT)) { 864 tir.ruid = inp->inp_socket->so_ruid; 865 tir.euid = inp->inp_socket->so_euid; 866 } else { 867 tir.ruid = -1; 868 tir.euid = -1; 869 } 870 splx(s); 871 872 *oldlenp = sizeof (tir); 873 error = copyout((void *)&tir, oldp, sizeof (tir)); 874 return (error); 875 } 876 877 /* 878 * Sysctl for tcp variables. 879 */ 880 int 881 tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 882 int *name; 883 u_int namelen; 884 void *oldp; 885 size_t *oldlenp; 886 void *newp; 887 size_t newlen; 888 { 889 int error, nval; 890 891 /* All sysctl names at this level are terminal. */ 892 if (namelen != 1) 893 return (ENOTDIR); 894 895 switch (name[0]) { 896 #ifdef TCP_SACK 897 case TCPCTL_SACK: 898 return (sysctl_int(oldp, oldlenp, newp, newlen, 899 &tcp_do_sack)); 900 #endif 901 case TCPCTL_SLOWHZ: 902 return (sysctl_rdint(oldp, oldlenp, newp, PR_SLOWHZ)); 903 904 case TCPCTL_BADDYNAMIC: 905 return (sysctl_struct(oldp, oldlenp, newp, newlen, 906 baddynamicports.tcp, sizeof(baddynamicports.tcp))); 907 908 case TCPCTL_IDENT: 909 return (tcp_ident(oldp, oldlenp, newp, newlen, 0)); 910 911 case TCPCTL_DROP: 912 return (tcp_ident(oldp, oldlenp, newp, newlen, 1)); 913 914 #ifdef TCP_ECN 915 case TCPCTL_ECN: 916 return (sysctl_int(oldp, oldlenp, newp, newlen, 917 &tcp_do_ecn)); 918 #endif 919 case TCPCTL_REASS_LIMIT: 920 nval = tcp_reass_limit; 921 error = sysctl_int(oldp, oldlenp, newp, newlen, &nval); 922 if (error) 923 return (error); 924 if (nval != tcp_reass_limit) { 925 error = pool_sethardlimit(&tcpqe_pool, nval, NULL, 0); 926 if (error) 927 return (error); 928 tcp_reass_limit = nval; 929 } 930 return (0); 931 #ifdef TCP_SACK 932 case TCPCTL_SACKHOLE_LIMIT: 933 nval = tcp_sackhole_limit; 934 error = sysctl_int(oldp, oldlenp, newp, newlen, &nval); 935 if (error) 936 return (error); 937 if (nval != tcp_sackhole_limit) { 938 error = pool_sethardlimit(&sackhl_pool, nval, NULL, 0); 939 if (error) 940 return (error); 941 tcp_sackhole_limit = nval; 942 } 943 return (0); 944 #endif 945 946 case TCPCTL_STATS: 947 if (newp != NULL) 948 return (EPERM); 949 return (sysctl_struct(oldp, oldlenp, newp, newlen, 950 &tcpstat, sizeof(tcpstat))); 951 952 default: 953 if (name[0] < TCPCTL_MAXID) 954 return (sysctl_int_arr(tcpctl_vars, name, namelen, 955 oldp, oldlenp, newp, newlen)); 956 return (ENOPROTOOPT); 957 } 958 /* NOTREACHED */ 959 } 960