1 /* $OpenBSD: tcp_usrreq.c,v 1.153 2017/08/15 17:47:15 bluhm 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 <net/if.h> 84 #include <net/if_var.h> 85 #include <net/route.h> 86 87 #include <netinet/in.h> 88 #include <netinet/in_var.h> 89 #include <netinet/ip.h> 90 #include <netinet/in_pcb.h> 91 #include <netinet/ip_var.h> 92 #include <netinet/tcp.h> 93 #include <netinet/tcp_fsm.h> 94 #include <netinet/tcp_seq.h> 95 #include <netinet/tcp_timer.h> 96 #include <netinet/tcp_var.h> 97 #include <netinet/tcp_debug.h> 98 99 #ifdef INET6 100 #include <netinet6/in6_var.h> 101 #endif 102 103 #ifndef TCP_SENDSPACE 104 #define TCP_SENDSPACE 1024*16 105 #endif 106 u_int tcp_sendspace = TCP_SENDSPACE; 107 #ifndef TCP_RECVSPACE 108 #define TCP_RECVSPACE 1024*16 109 #endif 110 u_int tcp_recvspace = TCP_RECVSPACE; 111 u_int tcp_autorcvbuf_inc = 16 * 1024; 112 113 int *tcpctl_vars[TCPCTL_MAXID] = TCPCTL_VARS; 114 115 struct inpcbtable tcbtable; 116 117 int tcp_ident(void *, size_t *, void *, size_t, int); 118 119 /* 120 * Process a TCP user request for TCP tb. If this is a send request 121 * then m is the mbuf chain of send data. If this is a timer expiration 122 * (called from the software clock routine), then timertype tells which timer. 123 */ 124 /*ARGSUSED*/ 125 int 126 tcp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 127 struct mbuf *control, struct proc *p) 128 { 129 struct inpcb *inp; 130 struct tcpcb *tp = NULL; 131 int error = 0; 132 short ostate; 133 134 NET_ASSERT_LOCKED(); 135 136 if (req == PRU_CONTROL) { 137 #ifdef INET6 138 if (sotopf(so) == PF_INET6) 139 return in6_control(so, (u_long)m, (caddr_t)nam, 140 (struct ifnet *)control); 141 else 142 #endif /* INET6 */ 143 return (in_control(so, (u_long)m, (caddr_t)nam, 144 (struct ifnet *)control)); 145 } 146 if (control && control->m_len) { 147 m_freem(control); 148 m_freem(m); 149 return (EINVAL); 150 } 151 152 inp = sotoinpcb(so); 153 /* 154 * When a TCP is attached to a socket, then there will be 155 * a (struct inpcb) pointed at by the socket, and this 156 * structure will point at a subsidiary (struct tcpcb). 157 */ 158 if (inp == NULL) { 159 error = so->so_error; 160 if (error == 0) 161 error = EINVAL; 162 /* 163 * The following corrects an mbuf leak under rare 164 * circumstances 165 */ 166 if (req == PRU_SEND || req == PRU_SENDOOB) 167 m_freem(m); 168 return (error); 169 } 170 if (inp) { 171 tp = intotcpcb(inp); 172 /* tp might get 0 when using socket splicing */ 173 if (tp == NULL) { 174 return (0); 175 } 176 #ifdef KPROF 177 tcp_acounts[tp->t_state][req]++; 178 #endif 179 ostate = tp->t_state; 180 } else 181 ostate = 0; 182 switch (req) { 183 184 /* 185 * PRU_DETACH detaches the TCP protocol from the socket. 186 * If the protocol state is non-embryonic, then can't 187 * do this directly: have to initiate a PRU_DISCONNECT, 188 * which may finish later; embryonic TCB's can just 189 * be discarded here. 190 */ 191 case PRU_DETACH: 192 tp = tcp_disconnect(tp); 193 break; 194 195 /* 196 * Give the socket an address. 197 */ 198 case PRU_BIND: 199 error = in_pcbbind(inp, nam, p); 200 break; 201 202 /* 203 * Prepare to accept connections. 204 */ 205 case PRU_LISTEN: 206 if (inp->inp_lport == 0) 207 error = in_pcbbind(inp, NULL, p); 208 /* If the in_pcbbind() above is called, the tp->pf 209 should still be whatever it was before. */ 210 if (error == 0) 211 tp->t_state = TCPS_LISTEN; 212 break; 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 case PRU_CONNECT: 222 #ifdef INET6 223 if (inp->inp_flags & INP_IPV6) { 224 struct sockaddr_in6 *sin6; 225 226 if ((error = in6_nam2sin6(nam, &sin6))) 227 break; 228 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || 229 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { 230 error = EINVAL; 231 break; 232 } 233 error = in6_pcbconnect(inp, nam); 234 } else 235 #endif /* INET6 */ 236 { 237 struct sockaddr_in *sin; 238 239 if ((error = in_nam2sin(nam, &sin))) 240 break; 241 if ((sin->sin_addr.s_addr == INADDR_ANY) || 242 (sin->sin_addr.s_addr == INADDR_BROADCAST) || 243 IN_MULTICAST(sin->sin_addr.s_addr) || 244 in_broadcast(sin->sin_addr, inp->inp_rtableid)) { 245 error = EINVAL; 246 break; 247 } 248 error = in_pcbconnect(inp, nam); 249 } 250 if (error) 251 break; 252 253 tp->t_template = tcp_template(tp); 254 if (tp->t_template == 0) { 255 in_pcbdisconnect(inp); 256 error = ENOBUFS; 257 break; 258 } 259 260 so->so_state |= SS_CONNECTOUT; 261 262 /* Compute window scaling to request. */ 263 tcp_rscale(tp, sb_max); 264 265 soisconnecting(so); 266 tcpstat_inc(tcps_connattempt); 267 tp->t_state = TCPS_SYN_SENT; 268 TCP_TIMER_ARM(tp, TCPT_KEEP, tcptv_keep_init); 269 tcp_set_iss_tsm(tp); 270 tcp_sendseqinit(tp); 271 #if defined(TCP_SACK) 272 tp->snd_last = tp->snd_una; 273 #endif 274 #if defined(TCP_SACK) && defined(TCP_FACK) 275 tp->snd_fack = tp->snd_una; 276 tp->retran_data = 0; 277 tp->snd_awnd = 0; 278 #endif 279 error = tcp_output(tp); 280 break; 281 282 /* 283 * Create a TCP connection between two sockets. 284 */ 285 case PRU_CONNECT2: 286 error = EOPNOTSUPP; 287 break; 288 289 /* 290 * Initiate disconnect from peer. 291 * If connection never passed embryonic stage, just drop; 292 * else if don't need to let data drain, then can just drop anyways, 293 * else have to begin TCP shutdown process: mark socket disconnecting, 294 * drain unread data, state switch to reflect user close, and 295 * send segment (e.g. FIN) to peer. Socket will be really disconnected 296 * when peer sends FIN and acks ours. 297 * 298 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 299 */ 300 case PRU_DISCONNECT: 301 tp = tcp_disconnect(tp); 302 break; 303 304 /* 305 * Accept a connection. Essentially all the work is 306 * done at higher levels; just return the address 307 * of the peer, storing through addr. 308 */ 309 case PRU_ACCEPT: 310 #ifdef INET6 311 if (inp->inp_flags & INP_IPV6) 312 in6_setpeeraddr(inp, nam); 313 else 314 #endif 315 in_setpeeraddr(inp, nam); 316 break; 317 318 /* 319 * Mark the connection as being incapable of further output. 320 */ 321 case PRU_SHUTDOWN: 322 if (so->so_state & SS_CANTSENDMORE) 323 break; 324 socantsendmore(so); 325 tp = tcp_usrclosed(tp); 326 if (tp) 327 error = tcp_output(tp); 328 break; 329 330 /* 331 * After a receive, possibly send window update to peer. 332 */ 333 case PRU_RCVD: 334 /* 335 * soreceive() calls this function when a user receives 336 * ancillary data on a listening socket. We don't call 337 * tcp_output in such a case, since there is no header 338 * template for a listening socket and hence the kernel 339 * will panic. 340 */ 341 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0) 342 (void) tcp_output(tp); 343 break; 344 345 /* 346 * Do a send by putting data in output queue and updating urgent 347 * marker if URG set. Possibly send more data. 348 */ 349 case PRU_SEND: 350 sbappendstream(so, &so->so_snd, m); 351 error = tcp_output(tp); 352 break; 353 354 /* 355 * Abort the TCP. 356 */ 357 case PRU_ABORT: 358 tp = tcp_drop(tp, ECONNABORTED); 359 break; 360 361 case PRU_SENSE: 362 ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 363 return (0); 364 365 case PRU_RCVOOB: 366 if ((so->so_oobmark == 0 && 367 (so->so_state & SS_RCVATMARK) == 0) || 368 so->so_options & SO_OOBINLINE || 369 tp->t_oobflags & TCPOOB_HADDATA) { 370 error = EINVAL; 371 break; 372 } 373 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 374 error = EWOULDBLOCK; 375 break; 376 } 377 m->m_len = 1; 378 *mtod(m, caddr_t) = tp->t_iobc; 379 if (((long)nam & MSG_PEEK) == 0) 380 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 381 break; 382 383 case PRU_SENDOOB: 384 if (sbspace(so, &so->so_snd) < -512) { 385 m_freem(m); 386 error = ENOBUFS; 387 break; 388 } 389 /* 390 * According to RFC961 (Assigned Protocols), 391 * the urgent pointer points to the last octet 392 * of urgent data. We continue, however, 393 * to consider it to indicate the first octet 394 * of data past the urgent section. 395 * Otherwise, snd_up should be one lower. 396 */ 397 sbappendstream(so, &so->so_snd, m); 398 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 399 tp->t_force = 1; 400 error = tcp_output(tp); 401 tp->t_force = 0; 402 break; 403 404 case PRU_SOCKADDR: 405 #ifdef INET6 406 if (inp->inp_flags & INP_IPV6) 407 in6_setsockaddr(inp, nam); 408 else 409 #endif 410 in_setsockaddr(inp, nam); 411 break; 412 413 case PRU_PEERADDR: 414 #ifdef INET6 415 if (inp->inp_flags & INP_IPV6) 416 in6_setpeeraddr(inp, nam); 417 else 418 #endif 419 in_setpeeraddr(inp, nam); 420 break; 421 422 default: 423 panic("tcp_usrreq"); 424 } 425 if (tp && (so->so_options & SO_DEBUG)) 426 tcp_trace(TA_USER, ostate, tp, (caddr_t)0, req, 0); 427 return (error); 428 } 429 430 int 431 tcp_ctloutput(int op, struct socket *so, int level, int optname, 432 struct mbuf *m) 433 { 434 int error = 0; 435 struct inpcb *inp; 436 struct tcpcb *tp; 437 int i; 438 439 inp = sotoinpcb(so); 440 if (inp == NULL) { 441 if (op == PRCO_SETOPT) 442 (void) m_free(m); 443 return (ECONNRESET); 444 } 445 if (level != IPPROTO_TCP) { 446 switch (so->so_proto->pr_domain->dom_family) { 447 #ifdef INET6 448 case PF_INET6: 449 error = ip6_ctloutput(op, so, level, optname, m); 450 break; 451 #endif /* INET6 */ 452 case PF_INET: 453 error = ip_ctloutput(op, so, level, optname, m); 454 break; 455 default: 456 error = EAFNOSUPPORT; /*?*/ 457 break; 458 } 459 return (error); 460 } 461 tp = intotcpcb(inp); 462 463 switch (op) { 464 465 case PRCO_SETOPT: 466 switch (optname) { 467 468 case TCP_NODELAY: 469 if (m == NULL || m->m_len < sizeof (int)) 470 error = EINVAL; 471 else if (*mtod(m, int *)) 472 tp->t_flags |= TF_NODELAY; 473 else 474 tp->t_flags &= ~TF_NODELAY; 475 break; 476 477 case TCP_NOPUSH: 478 if (m == NULL || m->m_len < sizeof (int)) 479 error = EINVAL; 480 else if (*mtod(m, int *)) 481 tp->t_flags |= TF_NOPUSH; 482 else if (tp->t_flags & TF_NOPUSH) { 483 tp->t_flags &= ~TF_NOPUSH; 484 if (TCPS_HAVEESTABLISHED(tp->t_state)) 485 error = tcp_output(tp); 486 } 487 break; 488 489 case TCP_MAXSEG: 490 if (m == NULL || m->m_len < sizeof (int)) { 491 error = EINVAL; 492 break; 493 } 494 495 i = *mtod(m, int *); 496 if (i > 0 && i <= tp->t_maxseg) 497 tp->t_maxseg = i; 498 else 499 error = EINVAL; 500 break; 501 502 #ifdef TCP_SACK 503 case TCP_SACK_ENABLE: 504 if (m == NULL || m->m_len < sizeof (int)) { 505 error = EINVAL; 506 break; 507 } 508 509 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 510 error = EPERM; 511 break; 512 } 513 514 if (tp->t_flags & TF_SIGNATURE) { 515 error = EPERM; 516 break; 517 } 518 519 if (*mtod(m, int *)) 520 tp->sack_enable = 1; 521 else 522 tp->sack_enable = 0; 523 break; 524 #endif 525 #ifdef TCP_SIGNATURE 526 case TCP_MD5SIG: 527 if (m == NULL || m->m_len < sizeof (int)) { 528 error = EINVAL; 529 break; 530 } 531 532 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 533 error = EPERM; 534 break; 535 } 536 537 if (*mtod(m, int *)) { 538 tp->t_flags |= TF_SIGNATURE; 539 #ifdef TCP_SACK 540 tp->sack_enable = 0; 541 #endif /* TCP_SACK */ 542 } else 543 tp->t_flags &= ~TF_SIGNATURE; 544 break; 545 #endif /* TCP_SIGNATURE */ 546 default: 547 error = ENOPROTOOPT; 548 break; 549 } 550 m_free(m); 551 break; 552 553 case PRCO_GETOPT: 554 m->m_len = sizeof(int); 555 556 switch (optname) { 557 case TCP_NODELAY: 558 *mtod(m, int *) = tp->t_flags & TF_NODELAY; 559 break; 560 case TCP_NOPUSH: 561 *mtod(m, int *) = tp->t_flags & TF_NOPUSH; 562 break; 563 case TCP_MAXSEG: 564 *mtod(m, int *) = tp->t_maxseg; 565 break; 566 #ifdef TCP_SACK 567 case TCP_SACK_ENABLE: 568 *mtod(m, int *) = tp->sack_enable; 569 break; 570 #endif 571 #ifdef TCP_SIGNATURE 572 case TCP_MD5SIG: 573 *mtod(m, int *) = tp->t_flags & TF_SIGNATURE; 574 break; 575 #endif 576 default: 577 error = ENOPROTOOPT; 578 break; 579 } 580 break; 581 } 582 return (error); 583 } 584 585 /* 586 * Attach TCP protocol to socket, allocating 587 * internet protocol control block, tcp control block, 588 * bufer space, and entering LISTEN state if to accept connections. 589 */ 590 int 591 tcp_attach(struct socket *so, int proto) 592 { 593 struct tcpcb *tp; 594 struct inpcb *inp; 595 int error; 596 597 if (so->so_pcb) 598 return EISCONN; 599 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0 || 600 sbcheckreserve(so->so_snd.sb_wat, tcp_sendspace) || 601 sbcheckreserve(so->so_rcv.sb_wat, tcp_recvspace)) { 602 error = soreserve(so, tcp_sendspace, tcp_recvspace); 603 if (error) 604 return (error); 605 } 606 607 error = in_pcballoc(so, &tcbtable); 608 if (error) 609 return (error); 610 inp = sotoinpcb(so); 611 tp = tcp_newtcpcb(inp); 612 if (tp == NULL) { 613 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 614 615 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 616 in_pcbdetach(inp); 617 so->so_state |= nofd; 618 return (ENOBUFS); 619 } 620 tp->t_state = TCPS_CLOSED; 621 #ifdef INET6 622 /* we disallow IPv4 mapped address completely. */ 623 if (inp->inp_flags & INP_IPV6) 624 tp->pf = PF_INET6; 625 else 626 tp->pf = PF_INET; 627 #else 628 tp->pf = PF_INET; 629 #endif 630 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 631 so->so_linger = TCP_LINGERTIME; 632 633 if (tp && (so->so_options & SO_DEBUG)) 634 tcp_trace(TA_USER, 0, tp, (caddr_t)0, 0 /* XXX */, 0); 635 return (0); 636 } 637 638 /* 639 * Initiate (or continue) disconnect. 640 * If embryonic state, just send reset (once). 641 * If in ``let data drain'' option and linger null, just drop. 642 * Otherwise (hard), mark socket disconnecting and drop 643 * current input data; switch states based on user close, and 644 * send segment to peer (with FIN). 645 */ 646 struct tcpcb * 647 tcp_disconnect(struct tcpcb *tp) 648 { 649 struct socket *so = tp->t_inpcb->inp_socket; 650 651 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 652 tp = tcp_close(tp); 653 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 654 tp = tcp_drop(tp, 0); 655 else { 656 soisdisconnecting(so); 657 sbflush(so, &so->so_rcv); 658 tp = tcp_usrclosed(tp); 659 if (tp) 660 (void) tcp_output(tp); 661 } 662 return (tp); 663 } 664 665 /* 666 * User issued close, and wish to trail through shutdown states: 667 * if never received SYN, just forget it. If got a SYN from peer, 668 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 669 * If already got a FIN from peer, then almost done; go to LAST_ACK 670 * state. In all other cases, have already sent FIN to peer (e.g. 671 * after PRU_SHUTDOWN), and just have to play tedious game waiting 672 * for peer to send FIN or not respond to keep-alives, etc. 673 * We can let the user exit from the close as soon as the FIN is acked. 674 */ 675 struct tcpcb * 676 tcp_usrclosed(struct tcpcb *tp) 677 { 678 679 switch (tp->t_state) { 680 681 case TCPS_CLOSED: 682 case TCPS_LISTEN: 683 case TCPS_SYN_SENT: 684 tp->t_state = TCPS_CLOSED; 685 tp = tcp_close(tp); 686 break; 687 688 case TCPS_SYN_RECEIVED: 689 case TCPS_ESTABLISHED: 690 tp->t_state = TCPS_FIN_WAIT_1; 691 break; 692 693 case TCPS_CLOSE_WAIT: 694 tp->t_state = TCPS_LAST_ACK; 695 break; 696 } 697 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 698 soisdisconnected(tp->t_inpcb->inp_socket); 699 /* 700 * If we are in FIN_WAIT_2, we arrived here because the 701 * application did a shutdown of the send side. Like the 702 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after 703 * a full close, we start a timer to make sure sockets are 704 * not left in FIN_WAIT_2 forever. 705 */ 706 if (tp->t_state == TCPS_FIN_WAIT_2) 707 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle); 708 } 709 return (tp); 710 } 711 712 /* 713 * Look up a socket for ident or tcpdrop, ... 714 */ 715 int 716 tcp_ident(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int dodrop) 717 { 718 int error = 0; 719 struct tcp_ident_mapping tir; 720 struct inpcb *inp; 721 struct tcpcb *tp = NULL; 722 struct sockaddr_in *fin, *lin; 723 #ifdef INET6 724 struct sockaddr_in6 *fin6, *lin6; 725 struct in6_addr f6, l6; 726 #endif 727 728 NET_ASSERT_LOCKED(); 729 730 if (dodrop) { 731 if (oldp != NULL || *oldlenp != 0) 732 return (EINVAL); 733 if (newp == NULL) 734 return (EPERM); 735 if (newlen < sizeof(tir)) 736 return (ENOMEM); 737 if ((error = copyin(newp, &tir, sizeof (tir))) != 0 ) 738 return (error); 739 } else { 740 if (oldp == NULL) 741 return (EINVAL); 742 if (*oldlenp < sizeof(tir)) 743 return (ENOMEM); 744 if (newp != NULL || newlen != 0) 745 return (EINVAL); 746 if ((error = copyin(oldp, &tir, sizeof (tir))) != 0 ) 747 return (error); 748 } 749 switch (tir.faddr.ss_family) { 750 #ifdef INET6 751 case AF_INET6: 752 fin6 = (struct sockaddr_in6 *)&tir.faddr; 753 error = in6_embedscope(&f6, fin6, NULL); 754 if (error) 755 return EINVAL; /*?*/ 756 lin6 = (struct sockaddr_in6 *)&tir.laddr; 757 error = in6_embedscope(&l6, lin6, NULL); 758 if (error) 759 return EINVAL; /*?*/ 760 break; 761 #endif 762 case AF_INET: 763 fin = (struct sockaddr_in *)&tir.faddr; 764 lin = (struct sockaddr_in *)&tir.laddr; 765 break; 766 default: 767 return (EINVAL); 768 } 769 770 switch (tir.faddr.ss_family) { 771 #ifdef INET6 772 case AF_INET6: 773 inp = in6_pcbhashlookup(&tcbtable, &f6, 774 fin6->sin6_port, &l6, lin6->sin6_port, tir.rdomain); 775 break; 776 #endif 777 case AF_INET: 778 inp = in_pcbhashlookup(&tcbtable, fin->sin_addr, 779 fin->sin_port, lin->sin_addr, lin->sin_port, tir.rdomain); 780 break; 781 default: 782 unhandled_af(tir.faddr.ss_family); 783 } 784 785 if (dodrop) { 786 if (inp && (tp = intotcpcb(inp)) && 787 ((inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) 788 tp = tcp_drop(tp, ECONNABORTED); 789 else 790 error = ESRCH; 791 return (error); 792 } 793 794 if (inp == NULL) { 795 tcpstat_inc(tcps_pcbhashmiss); 796 switch (tir.faddr.ss_family) { 797 #ifdef INET6 798 case AF_INET6: 799 inp = in6_pcblookup_listen(&tcbtable, 800 &l6, lin6->sin6_port, 0, NULL, tir.rdomain); 801 break; 802 #endif 803 case AF_INET: 804 inp = in_pcblookup_listen(&tcbtable, 805 lin->sin_addr, lin->sin_port, 0, NULL, tir.rdomain); 806 break; 807 } 808 } 809 810 if (inp != NULL && (inp->inp_socket->so_state & SS_CONNECTOUT)) { 811 tir.ruid = inp->inp_socket->so_ruid; 812 tir.euid = inp->inp_socket->so_euid; 813 } else { 814 tir.ruid = -1; 815 tir.euid = -1; 816 } 817 818 *oldlenp = sizeof (tir); 819 error = copyout((void *)&tir, oldp, sizeof (tir)); 820 return (error); 821 } 822 823 int 824 tcp_sysctl_tcpstat(void *oldp, size_t *oldlenp, void *newp) 825 { 826 uint64_t counters[tcps_ncounters]; 827 struct tcpstat tcpstat; 828 struct syn_cache_set *set; 829 int i = 0; 830 831 #define ASSIGN(field) do { tcpstat.field = counters[i++]; } while (0) 832 833 memset(&tcpstat, 0, sizeof tcpstat); 834 counters_read(tcpcounters, counters, nitems(counters)); 835 ASSIGN(tcps_connattempt); 836 ASSIGN(tcps_accepts); 837 ASSIGN(tcps_connects); 838 ASSIGN(tcps_drops); 839 ASSIGN(tcps_conndrops); 840 ASSIGN(tcps_closed); 841 ASSIGN(tcps_segstimed); 842 ASSIGN(tcps_rttupdated); 843 ASSIGN(tcps_delack); 844 ASSIGN(tcps_timeoutdrop); 845 ASSIGN(tcps_rexmttimeo); 846 ASSIGN(tcps_persisttimeo); 847 ASSIGN(tcps_persistdrop); 848 ASSIGN(tcps_keeptimeo); 849 ASSIGN(tcps_keepprobe); 850 ASSIGN(tcps_keepdrops); 851 ASSIGN(tcps_sndtotal); 852 ASSIGN(tcps_sndpack); 853 ASSIGN(tcps_sndbyte); 854 ASSIGN(tcps_sndrexmitpack); 855 ASSIGN(tcps_sndrexmitbyte); 856 ASSIGN(tcps_sndrexmitfast); 857 ASSIGN(tcps_sndacks); 858 ASSIGN(tcps_sndprobe); 859 ASSIGN(tcps_sndurg); 860 ASSIGN(tcps_sndwinup); 861 ASSIGN(tcps_sndctrl); 862 ASSIGN(tcps_rcvtotal); 863 ASSIGN(tcps_rcvpack); 864 ASSIGN(tcps_rcvbyte); 865 ASSIGN(tcps_rcvbadsum); 866 ASSIGN(tcps_rcvbadoff); 867 ASSIGN(tcps_rcvmemdrop); 868 ASSIGN(tcps_rcvnosec); 869 ASSIGN(tcps_rcvshort); 870 ASSIGN(tcps_rcvduppack); 871 ASSIGN(tcps_rcvdupbyte); 872 ASSIGN(tcps_rcvpartduppack); 873 ASSIGN(tcps_rcvpartdupbyte); 874 ASSIGN(tcps_rcvoopack); 875 ASSIGN(tcps_rcvoobyte); 876 ASSIGN(tcps_rcvpackafterwin); 877 ASSIGN(tcps_rcvbyteafterwin); 878 ASSIGN(tcps_rcvafterclose); 879 ASSIGN(tcps_rcvwinprobe); 880 ASSIGN(tcps_rcvdupack); 881 ASSIGN(tcps_rcvacktoomuch); 882 ASSIGN(tcps_rcvacktooold); 883 ASSIGN(tcps_rcvackpack); 884 ASSIGN(tcps_rcvackbyte); 885 ASSIGN(tcps_rcvwinupd); 886 ASSIGN(tcps_pawsdrop); 887 ASSIGN(tcps_predack); 888 ASSIGN(tcps_preddat); 889 ASSIGN(tcps_pcbhashmiss); 890 ASSIGN(tcps_noport); 891 ASSIGN(tcps_badsyn); 892 ASSIGN(tcps_dropsyn); 893 ASSIGN(tcps_rcvbadsig); 894 ASSIGN(tcps_rcvgoodsig); 895 ASSIGN(tcps_inswcsum); 896 ASSIGN(tcps_outswcsum); 897 ASSIGN(tcps_ecn_accepts); 898 ASSIGN(tcps_ecn_rcvece); 899 ASSIGN(tcps_ecn_rcvcwr); 900 ASSIGN(tcps_ecn_rcvce); 901 ASSIGN(tcps_ecn_sndect); 902 ASSIGN(tcps_ecn_sndece); 903 ASSIGN(tcps_ecn_sndcwr); 904 ASSIGN(tcps_cwr_ecn); 905 ASSIGN(tcps_cwr_frecovery); 906 ASSIGN(tcps_cwr_timeout); 907 ASSIGN(tcps_sc_added); 908 ASSIGN(tcps_sc_completed); 909 ASSIGN(tcps_sc_timed_out); 910 ASSIGN(tcps_sc_overflowed); 911 ASSIGN(tcps_sc_reset); 912 ASSIGN(tcps_sc_unreach); 913 ASSIGN(tcps_sc_bucketoverflow); 914 ASSIGN(tcps_sc_aborted); 915 ASSIGN(tcps_sc_dupesyn); 916 ASSIGN(tcps_sc_dropped); 917 ASSIGN(tcps_sc_collisions); 918 ASSIGN(tcps_sc_retransmitted); 919 ASSIGN(tcps_sc_seedrandom); 920 ASSIGN(tcps_sc_hash_size); 921 ASSIGN(tcps_sc_entry_count); 922 ASSIGN(tcps_sc_entry_limit); 923 ASSIGN(tcps_sc_bucket_maxlen); 924 ASSIGN(tcps_sc_bucket_limit); 925 ASSIGN(tcps_sc_uses_left); 926 ASSIGN(tcps_conndrained); 927 ASSIGN(tcps_sack_recovery_episode); 928 ASSIGN(tcps_sack_rexmits); 929 ASSIGN(tcps_sack_rexmit_bytes); 930 ASSIGN(tcps_sack_rcv_opts); 931 ASSIGN(tcps_sack_snd_opts); 932 933 #undef ASSIGN 934 935 set = &tcp_syn_cache[tcp_syn_cache_active]; 936 tcpstat.tcps_sc_hash_size = set->scs_size; 937 tcpstat.tcps_sc_entry_count = set->scs_count; 938 tcpstat.tcps_sc_entry_limit = tcp_syn_cache_limit; 939 tcpstat.tcps_sc_bucket_maxlen = 0; 940 for (i = 0; i < set->scs_size; i++) { 941 if (tcpstat.tcps_sc_bucket_maxlen < 942 set->scs_buckethead[i].sch_length) 943 tcpstat.tcps_sc_bucket_maxlen = 944 set->scs_buckethead[i].sch_length; 945 } 946 tcpstat.tcps_sc_bucket_limit = tcp_syn_bucket_limit; 947 tcpstat.tcps_sc_uses_left = set->scs_use; 948 949 return (sysctl_rdstruct(oldp, oldlenp, newp, 950 &tcpstat, sizeof(tcpstat))); 951 } 952 953 /* 954 * Sysctl for tcp variables. 955 */ 956 int 957 tcp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 958 size_t newlen) 959 { 960 int error, nval; 961 962 NET_ASSERT_LOCKED(); 963 964 /* All sysctl names at this level are terminal. */ 965 if (namelen != 1) 966 return (ENOTDIR); 967 968 switch (name[0]) { 969 #ifdef TCP_SACK 970 case TCPCTL_SACK: 971 return (sysctl_int(oldp, oldlenp, newp, newlen, 972 &tcp_do_sack)); 973 #endif 974 case TCPCTL_SLOWHZ: 975 return (sysctl_rdint(oldp, oldlenp, newp, PR_SLOWHZ)); 976 977 case TCPCTL_BADDYNAMIC: 978 return (sysctl_struct(oldp, oldlenp, newp, newlen, 979 baddynamicports.tcp, sizeof(baddynamicports.tcp))); 980 981 case TCPCTL_ROOTONLY: 982 if (newp && securelevel > 0) 983 return (EPERM); 984 return (sysctl_struct(oldp, oldlenp, newp, newlen, 985 rootonlyports.tcp, sizeof(rootonlyports.tcp))); 986 987 case TCPCTL_IDENT: 988 return (tcp_ident(oldp, oldlenp, newp, newlen, 0)); 989 990 case TCPCTL_DROP: 991 return (tcp_ident(oldp, oldlenp, newp, newlen, 1)); 992 993 case TCPCTL_ALWAYS_KEEPALIVE: 994 return (sysctl_int(oldp, oldlenp, newp, newlen, 995 &tcp_always_keepalive)); 996 997 #ifdef TCP_ECN 998 case TCPCTL_ECN: 999 return (sysctl_int(oldp, oldlenp, newp, newlen, 1000 &tcp_do_ecn)); 1001 #endif 1002 case TCPCTL_REASS_LIMIT: 1003 nval = tcp_reass_limit; 1004 error = sysctl_int(oldp, oldlenp, newp, newlen, &nval); 1005 if (error) 1006 return (error); 1007 if (nval != tcp_reass_limit) { 1008 error = pool_sethardlimit(&tcpqe_pool, nval, NULL, 0); 1009 if (error) 1010 return (error); 1011 tcp_reass_limit = nval; 1012 } 1013 return (0); 1014 #ifdef TCP_SACK 1015 case TCPCTL_SACKHOLE_LIMIT: 1016 nval = tcp_sackhole_limit; 1017 error = sysctl_int(oldp, oldlenp, newp, newlen, &nval); 1018 if (error) 1019 return (error); 1020 if (nval != tcp_sackhole_limit) { 1021 error = pool_sethardlimit(&sackhl_pool, nval, NULL, 0); 1022 if (error) 1023 return (error); 1024 tcp_sackhole_limit = nval; 1025 } 1026 return (0); 1027 #endif 1028 1029 case TCPCTL_STATS: 1030 return (tcp_sysctl_tcpstat(oldp, oldlenp, newp)); 1031 1032 case TCPCTL_SYN_USE_LIMIT: 1033 error = sysctl_int(oldp, oldlenp, newp, newlen, 1034 &tcp_syn_use_limit); 1035 if (error) 1036 return (error); 1037 if (newp != NULL) { 1038 /* 1039 * Global tcp_syn_use_limit is used when reseeding a 1040 * new cache. Also update the value in active cache. 1041 */ 1042 if (tcp_syn_cache[0].scs_use > tcp_syn_use_limit) 1043 tcp_syn_cache[0].scs_use = tcp_syn_use_limit; 1044 if (tcp_syn_cache[1].scs_use > tcp_syn_use_limit) 1045 tcp_syn_cache[1].scs_use = tcp_syn_use_limit; 1046 } 1047 return (0); 1048 1049 case TCPCTL_SYN_HASH_SIZE: 1050 nval = tcp_syn_hash_size; 1051 error = sysctl_int(oldp, oldlenp, newp, newlen, &nval); 1052 if (error) 1053 return (error); 1054 if (nval != tcp_syn_hash_size) { 1055 if (nval < 1 || nval > 100000) 1056 return (EINVAL); 1057 /* 1058 * If global hash size has been changed, switch sets as 1059 * soon as possible. Then the actual hash array will 1060 * be reallocated. 1061 */ 1062 if (tcp_syn_cache[0].scs_size != nval) 1063 tcp_syn_cache[0].scs_use = 0; 1064 if (tcp_syn_cache[1].scs_size != nval) 1065 tcp_syn_cache[1].scs_use = 0; 1066 tcp_syn_hash_size = nval; 1067 } 1068 return (0); 1069 1070 default: 1071 if (name[0] < TCPCTL_MAXID) 1072 return (sysctl_int_arr(tcpctl_vars, name, namelen, 1073 oldp, oldlenp, newp, newlen)); 1074 return (ENOPROTOOPT); 1075 } 1076 /* NOTREACHED */ 1077 } 1078 1079 /* 1080 * Scale the send buffer so that inflight data is not accounted against 1081 * the limit. The buffer will scale with the congestion window, if the 1082 * the receiver stops acking data the window will shrink and therefor 1083 * the buffer size will shrink as well. 1084 * In low memory situation try to shrink the buffer to the initial size 1085 * disabling the send buffer scaling as long as the situation persists. 1086 */ 1087 void 1088 tcp_update_sndspace(struct tcpcb *tp) 1089 { 1090 struct socket *so = tp->t_inpcb->inp_socket; 1091 u_long nmax = so->so_snd.sb_hiwat; 1092 1093 if (sbchecklowmem()) { 1094 /* low on memory try to get rid of some */ 1095 if (tcp_sendspace < nmax) 1096 nmax = tcp_sendspace; 1097 } else if (so->so_snd.sb_wat != tcp_sendspace) 1098 /* user requested buffer size, auto-scaling disabled */ 1099 nmax = so->so_snd.sb_wat; 1100 else 1101 /* automatic buffer scaling */ 1102 nmax = MIN(sb_max, so->so_snd.sb_wat + tp->snd_max - 1103 tp->snd_una); 1104 1105 /* a writable socket must be preserved because of poll(2) semantics */ 1106 if (sbspace(so, &so->so_snd) >= so->so_snd.sb_lowat) { 1107 if (nmax < so->so_snd.sb_cc + so->so_snd.sb_lowat) 1108 nmax = so->so_snd.sb_cc + so->so_snd.sb_lowat; 1109 if (nmax * 2 < so->so_snd.sb_mbcnt + so->so_snd.sb_lowat) 1110 nmax = (so->so_snd.sb_mbcnt+so->so_snd.sb_lowat+1) / 2; 1111 } 1112 1113 /* round to MSS boundary */ 1114 nmax = roundup(nmax, tp->t_maxseg); 1115 1116 if (nmax != so->so_snd.sb_hiwat) 1117 sbreserve(so, &so->so_snd, nmax); 1118 } 1119 1120 /* 1121 * Scale the recv buffer by looking at how much data was transferred in 1122 * on approximated RTT. If more than a big part of the recv buffer was 1123 * transferred during that time we increase the buffer by a constant. 1124 * In low memory situation try to shrink the buffer to the initial size. 1125 */ 1126 void 1127 tcp_update_rcvspace(struct tcpcb *tp) 1128 { 1129 struct socket *so = tp->t_inpcb->inp_socket; 1130 u_long nmax = so->so_rcv.sb_hiwat; 1131 1132 if (sbchecklowmem()) { 1133 /* low on memory try to get rid of some */ 1134 if (tcp_recvspace < nmax) 1135 nmax = tcp_recvspace; 1136 } else if (so->so_rcv.sb_wat != tcp_recvspace) 1137 /* user requested buffer size, auto-scaling disabled */ 1138 nmax = so->so_rcv.sb_wat; 1139 else { 1140 /* automatic buffer scaling */ 1141 if (tp->rfbuf_cnt > so->so_rcv.sb_hiwat / 8 * 7) 1142 nmax = MIN(sb_max, so->so_rcv.sb_hiwat + 1143 tcp_autorcvbuf_inc); 1144 } 1145 1146 /* a readable socket must be preserved because of poll(2) semantics */ 1147 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat && 1148 nmax < so->so_snd.sb_lowat) 1149 nmax = so->so_snd.sb_lowat; 1150 1151 if (nmax == so->so_rcv.sb_hiwat) 1152 return; 1153 1154 /* round to MSS boundary */ 1155 nmax = roundup(nmax, tp->t_maxseg); 1156 sbreserve(so, &so->so_rcv, nmax); 1157 } 1158