1 /* $NetBSD: tcp_usrreq.c,v 1.170 2013/12/02 09:39:54 kefren Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1997, 1998, 2005, 2006 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation 38 * Facility, NASA Ames Research Center. 39 * This code is derived from software contributed to The NetBSD Foundation 40 * by Charles M. Hannum. 41 * This code is derived from software contributed to The NetBSD Foundation 42 * by Rui Paulo. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 63 * POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66 /* 67 * Copyright (c) 1982, 1986, 1988, 1993, 1995 68 * The Regents of the University of California. All rights reserved. 69 * 70 * Redistribution and use in source and binary forms, with or without 71 * modification, are permitted provided that the following conditions 72 * are met: 73 * 1. Redistributions of source code must retain the above copyright 74 * notice, this list of conditions and the following disclaimer. 75 * 2. Redistributions in binary form must reproduce the above copyright 76 * notice, this list of conditions and the following disclaimer in the 77 * documentation and/or other materials provided with the distribution. 78 * 3. Neither the name of the University nor the names of its contributors 79 * may be used to endorse or promote products derived from this software 80 * without specific prior written permission. 81 * 82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 85 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 92 * SUCH DAMAGE. 93 * 94 * @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95 95 */ 96 97 #include <sys/cdefs.h> 98 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.170 2013/12/02 09:39:54 kefren Exp $"); 99 100 #include "opt_inet.h" 101 #include "opt_ipsec.h" 102 #include "opt_tcp_debug.h" 103 #include "opt_mbuftrace.h" 104 105 106 #include <sys/param.h> 107 #include <sys/systm.h> 108 #include <sys/kernel.h> 109 #include <sys/malloc.h> 110 #include <sys/mbuf.h> 111 #include <sys/socket.h> 112 #include <sys/socketvar.h> 113 #include <sys/protosw.h> 114 #include <sys/errno.h> 115 #include <sys/stat.h> 116 #include <sys/proc.h> 117 #include <sys/domain.h> 118 #include <sys/sysctl.h> 119 #include <sys/kauth.h> 120 #include <sys/uidinfo.h> 121 122 #include <net/if.h> 123 #include <net/route.h> 124 125 #include <netinet/in.h> 126 #include <netinet/in_systm.h> 127 #include <netinet/in_var.h> 128 #include <netinet/ip.h> 129 #include <netinet/in_pcb.h> 130 #include <netinet/ip_var.h> 131 #include <netinet/in_offload.h> 132 133 #ifdef INET6 134 #ifndef INET 135 #include <netinet/in.h> 136 #endif 137 #include <netinet/ip6.h> 138 #include <netinet6/in6_pcb.h> 139 #include <netinet6/ip6_var.h> 140 #include <netinet6/scope6_var.h> 141 #endif 142 143 #include <netinet/tcp.h> 144 #include <netinet/tcp_fsm.h> 145 #include <netinet/tcp_seq.h> 146 #include <netinet/tcp_timer.h> 147 #include <netinet/tcp_var.h> 148 #include <netinet/tcp_private.h> 149 #include <netinet/tcp_congctl.h> 150 #include <netinet/tcpip.h> 151 #include <netinet/tcp_debug.h> 152 #include <netinet/tcp_vtw.h> 153 154 #include "opt_tcp_space.h" 155 156 /* 157 * TCP protocol interface to socket abstraction. 158 */ 159 160 /* 161 * Process a TCP user request for TCP tb. If this is a send request 162 * then m is the mbuf chain of send data. If this is a timer expiration 163 * (called from the software clock routine), then timertype tells which timer. 164 */ 165 /*ARGSUSED*/ 166 int 167 tcp_usrreq(struct socket *so, int req, 168 struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l) 169 { 170 struct inpcb *inp; 171 #ifdef INET6 172 struct in6pcb *in6p; 173 #endif 174 struct tcpcb *tp = NULL; 175 int s; 176 int error = 0; 177 #ifdef TCP_DEBUG 178 int ostate = 0; 179 #endif 180 int family; /* family of the socket */ 181 182 family = so->so_proto->pr_domain->dom_family; 183 184 if (req == PRU_CONTROL) { 185 switch (family) { 186 #ifdef INET 187 case PF_INET: 188 return (in_control(so, (long)m, (void *)nam, 189 (struct ifnet *)control, l)); 190 #endif 191 #ifdef INET6 192 case PF_INET6: 193 return (in6_control(so, (long)m, (void *)nam, 194 (struct ifnet *)control, l)); 195 #endif 196 default: 197 return EAFNOSUPPORT; 198 } 199 } 200 201 s = splsoftnet(); 202 203 if (req == PRU_PURGEIF) { 204 mutex_enter(softnet_lock); 205 switch (family) { 206 #ifdef INET 207 case PF_INET: 208 in_pcbpurgeif0(&tcbtable, (struct ifnet *)control); 209 in_purgeif((struct ifnet *)control); 210 in_pcbpurgeif(&tcbtable, (struct ifnet *)control); 211 break; 212 #endif 213 #ifdef INET6 214 case PF_INET6: 215 in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control); 216 in6_purgeif((struct ifnet *)control); 217 in6_pcbpurgeif(&tcbtable, (struct ifnet *)control); 218 break; 219 #endif 220 default: 221 mutex_exit(softnet_lock); 222 splx(s); 223 return (EAFNOSUPPORT); 224 } 225 mutex_exit(softnet_lock); 226 splx(s); 227 return (0); 228 } 229 230 if (req == PRU_ATTACH) 231 sosetlock(so); 232 233 switch (family) { 234 #ifdef INET 235 case PF_INET: 236 inp = sotoinpcb(so); 237 #ifdef INET6 238 in6p = NULL; 239 #endif 240 break; 241 #endif 242 #ifdef INET6 243 case PF_INET6: 244 inp = NULL; 245 in6p = sotoin6pcb(so); 246 break; 247 #endif 248 default: 249 splx(s); 250 return EAFNOSUPPORT; 251 } 252 253 #ifdef DIAGNOSTIC 254 #ifdef INET6 255 if (inp && in6p) 256 panic("tcp_usrreq: both inp and in6p set to non-NULL"); 257 #endif 258 if (req != PRU_SEND && req != PRU_SENDOOB && control) 259 panic("tcp_usrreq: unexpected control mbuf"); 260 #endif 261 /* 262 * When a TCP is attached to a socket, then there will be 263 * a (struct inpcb) pointed at by the socket, and this 264 * structure will point at a subsidary (struct tcpcb). 265 */ 266 if ((inp == 0 267 #ifdef INET6 268 && in6p == 0 269 #endif 270 ) && (req != PRU_ATTACH && req != PRU_SENSE)) 271 { 272 error = EINVAL; 273 goto release; 274 } 275 #ifdef INET 276 if (inp) { 277 tp = intotcpcb(inp); 278 /* WHAT IF TP IS 0? */ 279 #ifdef KPROF 280 tcp_acounts[tp->t_state][req]++; 281 #endif 282 #ifdef TCP_DEBUG 283 ostate = tp->t_state; 284 #endif 285 } 286 #endif 287 #ifdef INET6 288 if (in6p) { 289 tp = in6totcpcb(in6p); 290 /* WHAT IF TP IS 0? */ 291 #ifdef KPROF 292 tcp_acounts[tp->t_state][req]++; 293 #endif 294 #ifdef TCP_DEBUG 295 ostate = tp->t_state; 296 #endif 297 } 298 #endif 299 300 switch (req) { 301 302 /* 303 * TCP attaches to socket via PRU_ATTACH, reserving space, 304 * and an internet control block. 305 */ 306 case PRU_ATTACH: 307 #ifndef INET6 308 if (inp != 0) 309 #else 310 if (inp != 0 || in6p != 0) 311 #endif 312 { 313 error = EISCONN; 314 break; 315 } 316 error = tcp_attach(so); 317 if (error) 318 break; 319 if ((so->so_options & SO_LINGER) && so->so_linger == 0) 320 so->so_linger = TCP_LINGERTIME; 321 tp = sototcpcb(so); 322 break; 323 324 /* 325 * PRU_DETACH detaches the TCP protocol from the socket. 326 */ 327 case PRU_DETACH: 328 tp = tcp_disconnect(tp); 329 break; 330 331 /* 332 * Give the socket an address. 333 */ 334 case PRU_BIND: 335 switch (family) { 336 #ifdef INET 337 case PF_INET: 338 error = in_pcbbind(inp, nam, l); 339 break; 340 #endif 341 #ifdef INET6 342 case PF_INET6: 343 error = in6_pcbbind(in6p, nam, l); 344 if (!error) { 345 /* mapped addr case */ 346 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) 347 tp->t_family = AF_INET; 348 else 349 tp->t_family = AF_INET6; 350 } 351 break; 352 #endif 353 } 354 break; 355 356 /* 357 * Prepare to accept connections. 358 */ 359 case PRU_LISTEN: 360 #ifdef INET 361 if (inp && inp->inp_lport == 0) { 362 error = in_pcbbind(inp, NULL, l); 363 if (error) 364 break; 365 } 366 #endif 367 #ifdef INET6 368 if (in6p && in6p->in6p_lport == 0) { 369 error = in6_pcbbind(in6p, NULL, l); 370 if (error) 371 break; 372 } 373 #endif 374 tp->t_state = TCPS_LISTEN; 375 break; 376 377 /* 378 * Initiate connection to peer. 379 * Create a template for use in transmissions on this connection. 380 * Enter SYN_SENT state, and mark socket as connecting. 381 * Start keep-alive timer, and seed output sequence space. 382 * Send initial segment on connection. 383 */ 384 case PRU_CONNECT: 385 #ifdef INET 386 if (inp) { 387 if (inp->inp_lport == 0) { 388 error = in_pcbbind(inp, NULL, l); 389 if (error) 390 break; 391 } 392 error = in_pcbconnect(inp, nam, l); 393 } 394 #endif 395 #ifdef INET6 396 if (in6p) { 397 if (in6p->in6p_lport == 0) { 398 error = in6_pcbbind(in6p, NULL, l); 399 if (error) 400 break; 401 } 402 error = in6_pcbconnect(in6p, nam, l); 403 if (!error) { 404 /* mapped addr case */ 405 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) 406 tp->t_family = AF_INET; 407 else 408 tp->t_family = AF_INET6; 409 } 410 } 411 #endif 412 if (error) 413 break; 414 tp->t_template = tcp_template(tp); 415 if (tp->t_template == 0) { 416 #ifdef INET 417 if (inp) 418 in_pcbdisconnect(inp); 419 #endif 420 #ifdef INET6 421 if (in6p) 422 in6_pcbdisconnect(in6p); 423 #endif 424 error = ENOBUFS; 425 break; 426 } 427 /* 428 * Compute window scaling to request. 429 * XXX: This should be moved to tcp_output(). 430 */ 431 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 432 (TCP_MAXWIN << tp->request_r_scale) < sb_max) 433 tp->request_r_scale++; 434 soisconnecting(so); 435 TCP_STATINC(TCP_STAT_CONNATTEMPT); 436 tp->t_state = TCPS_SYN_SENT; 437 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit); 438 tp->iss = tcp_new_iss(tp, 0); 439 tcp_sendseqinit(tp); 440 error = tcp_output(tp); 441 break; 442 443 /* 444 * Create a TCP connection between two sockets. 445 */ 446 case PRU_CONNECT2: 447 error = EOPNOTSUPP; 448 break; 449 450 /* 451 * Initiate disconnect from peer. 452 * If connection never passed embryonic stage, just drop; 453 * else if don't need to let data drain, then can just drop anyways, 454 * else have to begin TCP shutdown process: mark socket disconnecting, 455 * drain unread data, state switch to reflect user close, and 456 * send segment (e.g. FIN) to peer. Socket will be really disconnected 457 * when peer sends FIN and acks ours. 458 * 459 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 460 */ 461 case PRU_DISCONNECT: 462 tp = tcp_disconnect(tp); 463 break; 464 465 /* 466 * Accept a connection. Essentially all the work is 467 * done at higher levels; just return the address 468 * of the peer, storing through addr. 469 */ 470 case PRU_ACCEPT: 471 #ifdef INET 472 if (inp) 473 in_setpeeraddr(inp, nam); 474 #endif 475 #ifdef INET6 476 if (in6p) 477 in6_setpeeraddr(in6p, nam); 478 #endif 479 break; 480 481 /* 482 * Mark the connection as being incapable of further output. 483 */ 484 case PRU_SHUTDOWN: 485 socantsendmore(so); 486 tp = tcp_usrclosed(tp); 487 if (tp) 488 error = tcp_output(tp); 489 break; 490 491 /* 492 * After a receive, possibly send window update to peer. 493 */ 494 case PRU_RCVD: 495 /* 496 * soreceive() calls this function when a user receives 497 * ancillary data on a listening socket. We don't call 498 * tcp_output in such a case, since there is no header 499 * template for a listening socket and hence the kernel 500 * will panic. 501 */ 502 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0) 503 (void) tcp_output(tp); 504 break; 505 506 /* 507 * Do a send by putting data in output queue and updating urgent 508 * marker if URG set. Possibly send more data. 509 */ 510 case PRU_SEND: 511 if (control && control->m_len) { 512 m_freem(control); 513 m_freem(m); 514 error = EINVAL; 515 break; 516 } 517 sbappendstream(&so->so_snd, m); 518 error = tcp_output(tp); 519 break; 520 521 /* 522 * Abort the TCP. 523 */ 524 case PRU_ABORT: 525 tp = tcp_drop(tp, ECONNABORTED); 526 break; 527 528 case PRU_SENSE: 529 /* 530 * stat: don't bother with a blocksize. 531 */ 532 splx(s); 533 return (0); 534 535 case PRU_RCVOOB: 536 if (control && control->m_len) { 537 m_freem(control); 538 m_freem(m); 539 error = EINVAL; 540 break; 541 } 542 if ((so->so_oobmark == 0 && 543 (so->so_state & SS_RCVATMARK) == 0) || 544 so->so_options & SO_OOBINLINE || 545 tp->t_oobflags & TCPOOB_HADDATA) { 546 error = EINVAL; 547 break; 548 } 549 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 550 error = EWOULDBLOCK; 551 break; 552 } 553 m->m_len = 1; 554 *mtod(m, char *) = tp->t_iobc; 555 if (((long)nam & MSG_PEEK) == 0) 556 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 557 break; 558 559 case PRU_SENDOOB: 560 if (sbspace(&so->so_snd) < -512) { 561 m_freem(m); 562 error = ENOBUFS; 563 break; 564 } 565 /* 566 * According to RFC961 (Assigned Protocols), 567 * the urgent pointer points to the last octet 568 * of urgent data. We continue, however, 569 * to consider it to indicate the first octet 570 * of data past the urgent section. 571 * Otherwise, snd_up should be one lower. 572 */ 573 sbappendstream(&so->so_snd, m); 574 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 575 tp->t_force = 1; 576 error = tcp_output(tp); 577 tp->t_force = 0; 578 break; 579 580 case PRU_SOCKADDR: 581 #ifdef INET 582 if (inp) 583 in_setsockaddr(inp, nam); 584 #endif 585 #ifdef INET6 586 if (in6p) 587 in6_setsockaddr(in6p, nam); 588 #endif 589 break; 590 591 case PRU_PEERADDR: 592 #ifdef INET 593 if (inp) 594 in_setpeeraddr(inp, nam); 595 #endif 596 #ifdef INET6 597 if (in6p) 598 in6_setpeeraddr(in6p, nam); 599 #endif 600 break; 601 602 default: 603 panic("tcp_usrreq"); 604 } 605 #ifdef TCP_DEBUG 606 if (tp && (so->so_options & SO_DEBUG)) 607 tcp_trace(TA_USER, ostate, tp, NULL, req); 608 #endif 609 610 release: 611 splx(s); 612 return (error); 613 } 614 615 static void 616 change_keepalive(struct socket *so, struct tcpcb *tp) 617 { 618 tp->t_maxidle = tp->t_keepcnt * tp->t_keepintvl; 619 TCP_TIMER_DISARM(tp, TCPT_KEEP); 620 TCP_TIMER_DISARM(tp, TCPT_2MSL); 621 622 if (tp->t_state == TCPS_SYN_RECEIVED || 623 tp->t_state == TCPS_SYN_SENT) { 624 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepinit); 625 } else if (so->so_options & SO_KEEPALIVE && 626 tp->t_state <= TCPS_CLOSE_WAIT) { 627 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepintvl); 628 } else { 629 TCP_TIMER_ARM(tp, TCPT_KEEP, tp->t_keepidle); 630 } 631 632 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0)) 633 TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle); 634 } 635 636 637 int 638 tcp_ctloutput(int op, struct socket *so, struct sockopt *sopt) 639 { 640 int error = 0, s; 641 struct inpcb *inp; 642 #ifdef INET6 643 struct in6pcb *in6p; 644 #endif 645 struct tcpcb *tp; 646 u_int ui; 647 int family; /* family of the socket */ 648 int level, optname, optval; 649 650 level = sopt->sopt_level; 651 optname = sopt->sopt_name; 652 653 family = so->so_proto->pr_domain->dom_family; 654 655 s = splsoftnet(); 656 switch (family) { 657 #ifdef INET 658 case PF_INET: 659 inp = sotoinpcb(so); 660 #ifdef INET6 661 in6p = NULL; 662 #endif 663 break; 664 #endif 665 #ifdef INET6 666 case PF_INET6: 667 inp = NULL; 668 in6p = sotoin6pcb(so); 669 break; 670 #endif 671 default: 672 splx(s); 673 panic("%s: af %d", __func__, family); 674 } 675 #ifndef INET6 676 if (inp == NULL) 677 #else 678 if (inp == NULL && in6p == NULL) 679 #endif 680 { 681 splx(s); 682 return (ECONNRESET); 683 } 684 if (level != IPPROTO_TCP) { 685 switch (family) { 686 #ifdef INET 687 case PF_INET: 688 error = ip_ctloutput(op, so, sopt); 689 break; 690 #endif 691 #ifdef INET6 692 case PF_INET6: 693 error = ip6_ctloutput(op, so, sopt); 694 break; 695 #endif 696 } 697 splx(s); 698 return (error); 699 } 700 if (inp) 701 tp = intotcpcb(inp); 702 #ifdef INET6 703 else if (in6p) 704 tp = in6totcpcb(in6p); 705 #endif 706 else 707 tp = NULL; 708 709 switch (op) { 710 case PRCO_SETOPT: 711 switch (optname) { 712 #ifdef TCP_SIGNATURE 713 case TCP_MD5SIG: 714 error = sockopt_getint(sopt, &optval); 715 if (error) 716 break; 717 if (optval > 0) 718 tp->t_flags |= TF_SIGNATURE; 719 else 720 tp->t_flags &= ~TF_SIGNATURE; 721 break; 722 #endif /* TCP_SIGNATURE */ 723 724 case TCP_NODELAY: 725 error = sockopt_getint(sopt, &optval); 726 if (error) 727 break; 728 if (optval) 729 tp->t_flags |= TF_NODELAY; 730 else 731 tp->t_flags &= ~TF_NODELAY; 732 break; 733 734 case TCP_MAXSEG: 735 error = sockopt_getint(sopt, &optval); 736 if (error) 737 break; 738 if (optval > 0 && optval <= tp->t_peermss) 739 tp->t_peermss = optval; /* limit on send size */ 740 else 741 error = EINVAL; 742 break; 743 #ifdef notyet 744 case TCP_CONGCTL: 745 /* XXX string overflow XXX */ 746 error = tcp_congctl_select(tp, sopt->sopt_data); 747 break; 748 #endif 749 750 case TCP_KEEPIDLE: 751 error = sockopt_get(sopt, &ui, sizeof(ui)); 752 if (error) 753 break; 754 if (ui > 0) { 755 tp->t_keepidle = ui; 756 change_keepalive(so, tp); 757 } else 758 error = EINVAL; 759 break; 760 761 case TCP_KEEPINTVL: 762 error = sockopt_get(sopt, &ui, sizeof(ui)); 763 if (error) 764 break; 765 if (ui > 0) { 766 tp->t_keepintvl = ui; 767 change_keepalive(so, tp); 768 } else 769 error = EINVAL; 770 break; 771 772 case TCP_KEEPCNT: 773 error = sockopt_get(sopt, &ui, sizeof(ui)); 774 if (error) 775 break; 776 if (ui > 0) { 777 tp->t_keepcnt = ui; 778 change_keepalive(so, tp); 779 } else 780 error = EINVAL; 781 break; 782 783 case TCP_KEEPINIT: 784 error = sockopt_get(sopt, &ui, sizeof(ui)); 785 if (error) 786 break; 787 if (ui > 0) { 788 tp->t_keepinit = ui; 789 change_keepalive(so, tp); 790 } else 791 error = EINVAL; 792 break; 793 794 default: 795 error = ENOPROTOOPT; 796 break; 797 } 798 break; 799 800 case PRCO_GETOPT: 801 switch (optname) { 802 #ifdef TCP_SIGNATURE 803 case TCP_MD5SIG: 804 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 805 error = sockopt_set(sopt, &optval, sizeof(optval)); 806 break; 807 #endif 808 case TCP_NODELAY: 809 optval = tp->t_flags & TF_NODELAY; 810 error = sockopt_set(sopt, &optval, sizeof(optval)); 811 break; 812 case TCP_MAXSEG: 813 optval = tp->t_peermss; 814 error = sockopt_set(sopt, &optval, sizeof(optval)); 815 break; 816 #ifdef notyet 817 case TCP_CONGCTL: 818 break; 819 #endif 820 default: 821 error = ENOPROTOOPT; 822 break; 823 } 824 break; 825 } 826 splx(s); 827 return (error); 828 } 829 830 #ifndef TCP_SENDSPACE 831 #define TCP_SENDSPACE 1024*32 832 #endif 833 int tcp_sendspace = TCP_SENDSPACE; 834 #ifndef TCP_RECVSPACE 835 #define TCP_RECVSPACE 1024*32 836 #endif 837 int tcp_recvspace = TCP_RECVSPACE; 838 839 /* 840 * Attach TCP protocol to socket, allocating 841 * internet protocol control block, tcp control block, 842 * bufer space, and entering LISTEN state if to accept connections. 843 */ 844 int 845 tcp_attach(struct socket *so) 846 { 847 struct tcpcb *tp; 848 struct inpcb *inp; 849 #ifdef INET6 850 struct in6pcb *in6p; 851 #endif 852 int error; 853 int family; /* family of the socket */ 854 855 family = so->so_proto->pr_domain->dom_family; 856 857 #ifdef MBUFTRACE 858 so->so_mowner = &tcp_sock_mowner; 859 so->so_rcv.sb_mowner = &tcp_sock_rx_mowner; 860 so->so_snd.sb_mowner = &tcp_sock_tx_mowner; 861 #endif 862 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 863 error = soreserve(so, tcp_sendspace, tcp_recvspace); 864 if (error) 865 return (error); 866 } 867 868 so->so_rcv.sb_flags |= SB_AUTOSIZE; 869 so->so_snd.sb_flags |= SB_AUTOSIZE; 870 871 switch (family) { 872 #ifdef INET 873 case PF_INET: 874 error = in_pcballoc(so, &tcbtable); 875 if (error) 876 return (error); 877 inp = sotoinpcb(so); 878 #ifdef INET6 879 in6p = NULL; 880 #endif 881 break; 882 #endif 883 #ifdef INET6 884 case PF_INET6: 885 error = in6_pcballoc(so, &tcbtable); 886 if (error) 887 return (error); 888 inp = NULL; 889 in6p = sotoin6pcb(so); 890 break; 891 #endif 892 default: 893 return EAFNOSUPPORT; 894 } 895 if (inp) 896 tp = tcp_newtcpcb(family, (void *)inp); 897 #ifdef INET6 898 else if (in6p) 899 tp = tcp_newtcpcb(family, (void *)in6p); 900 #endif 901 else 902 tp = NULL; 903 904 if (tp == 0) { 905 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 906 907 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 908 #ifdef INET 909 if (inp) 910 in_pcbdetach(inp); 911 #endif 912 #ifdef INET6 913 if (in6p) 914 in6_pcbdetach(in6p); 915 #endif 916 so->so_state |= nofd; 917 return (ENOBUFS); 918 } 919 tp->t_state = TCPS_CLOSED; 920 return (0); 921 } 922 923 /* 924 * Initiate (or continue) disconnect. 925 * If embryonic state, just send reset (once). 926 * If in ``let data drain'' option and linger null, just drop. 927 * Otherwise (hard), mark socket disconnecting and drop 928 * current input data; switch states based on user close, and 929 * send segment to peer (with FIN). 930 */ 931 struct tcpcb * 932 tcp_disconnect(struct tcpcb *tp) 933 { 934 struct socket *so; 935 936 if (tp->t_inpcb) 937 so = tp->t_inpcb->inp_socket; 938 #ifdef INET6 939 else if (tp->t_in6pcb) 940 so = tp->t_in6pcb->in6p_socket; 941 #endif 942 else 943 so = NULL; 944 945 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 946 tp = tcp_close(tp); 947 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 948 tp = tcp_drop(tp, 0); 949 else { 950 soisdisconnecting(so); 951 sbflush(&so->so_rcv); 952 tp = tcp_usrclosed(tp); 953 if (tp) 954 (void) tcp_output(tp); 955 } 956 return (tp); 957 } 958 959 /* 960 * User issued close, and wish to trail through shutdown states: 961 * if never received SYN, just forget it. If got a SYN from peer, 962 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 963 * If already got a FIN from peer, then almost done; go to LAST_ACK 964 * state. In all other cases, have already sent FIN to peer (e.g. 965 * after PRU_SHUTDOWN), and just have to play tedious game waiting 966 * for peer to send FIN or not respond to keep-alives, etc. 967 * We can let the user exit from the close as soon as the FIN is acked. 968 */ 969 struct tcpcb * 970 tcp_usrclosed(struct tcpcb *tp) 971 { 972 973 switch (tp->t_state) { 974 975 case TCPS_CLOSED: 976 case TCPS_LISTEN: 977 case TCPS_SYN_SENT: 978 tp->t_state = TCPS_CLOSED; 979 tp = tcp_close(tp); 980 break; 981 982 case TCPS_SYN_RECEIVED: 983 case TCPS_ESTABLISHED: 984 tp->t_state = TCPS_FIN_WAIT_1; 985 break; 986 987 case TCPS_CLOSE_WAIT: 988 tp->t_state = TCPS_LAST_ACK; 989 break; 990 } 991 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 992 struct socket *so; 993 if (tp->t_inpcb) 994 so = tp->t_inpcb->inp_socket; 995 #ifdef INET6 996 else if (tp->t_in6pcb) 997 so = tp->t_in6pcb->in6p_socket; 998 #endif 999 else 1000 so = NULL; 1001 if (so) 1002 soisdisconnected(so); 1003 /* 1004 * If we are in FIN_WAIT_2, we arrived here because the 1005 * application did a shutdown of the send side. Like the 1006 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after 1007 * a full close, we start a timer to make sure sockets are 1008 * not left in FIN_WAIT_2 forever. 1009 */ 1010 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tp->t_maxidle > 0)) 1011 TCP_TIMER_ARM(tp, TCPT_2MSL, tp->t_maxidle); 1012 else if (tp->t_state == TCPS_TIME_WAIT 1013 && ((tp->t_inpcb 1014 && (tcp4_vtw_enable & 1) 1015 && vtw_add(AF_INET, tp)) 1016 || 1017 (tp->t_in6pcb 1018 && (tcp6_vtw_enable & 1) 1019 && vtw_add(AF_INET6, tp)))) { 1020 tp = 0; 1021 } 1022 } 1023 return (tp); 1024 } 1025 1026 /* 1027 * sysctl helper routine for net.inet.ip.mssdflt. it can't be less 1028 * than 32. 1029 */ 1030 static int 1031 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS) 1032 { 1033 int error, mssdflt; 1034 struct sysctlnode node; 1035 1036 mssdflt = tcp_mssdflt; 1037 node = *rnode; 1038 node.sysctl_data = &mssdflt; 1039 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1040 if (error || newp == NULL) 1041 return (error); 1042 1043 if (mssdflt < 32) 1044 return (EINVAL); 1045 tcp_mssdflt = mssdflt; 1046 1047 mutex_enter(softnet_lock); 1048 tcp_tcpcb_template(); 1049 mutex_exit(softnet_lock); 1050 1051 return (0); 1052 } 1053 1054 /* 1055 * sysctl helper for TCP CB template update 1056 */ 1057 static int 1058 sysctl_update_tcpcb_template(SYSCTLFN_ARGS) 1059 { 1060 int t, error; 1061 struct sysctlnode node; 1062 1063 /* follow procedures in sysctl(9) manpage */ 1064 t = *(int *)rnode->sysctl_data; 1065 node = *rnode; 1066 node.sysctl_data = &t; 1067 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1068 if (error || newp == NULL) 1069 return error; 1070 1071 if (t < 0) 1072 return EINVAL; 1073 1074 *(int *)rnode->sysctl_data = t; 1075 1076 mutex_enter(softnet_lock); 1077 tcp_tcpcb_template(); 1078 mutex_exit(softnet_lock); 1079 1080 return 0; 1081 } 1082 1083 /* 1084 * sysctl helper routine for setting port related values under 1085 * net.inet.ip and net.inet6.ip6. does basic range checking and does 1086 * additional checks for each type. this code has placed in 1087 * tcp_input.c since INET and INET6 both use the same tcp code. 1088 * 1089 * this helper is not static so that both inet and inet6 can use it. 1090 */ 1091 int 1092 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS) 1093 { 1094 int error, tmp; 1095 int apmin, apmax; 1096 #ifndef IPNOPRIVPORTS 1097 int lpmin, lpmax; 1098 #endif /* IPNOPRIVPORTS */ 1099 struct sysctlnode node; 1100 1101 if (namelen != 0) 1102 return (EINVAL); 1103 1104 switch (name[-3]) { 1105 #ifdef INET 1106 case PF_INET: 1107 apmin = anonportmin; 1108 apmax = anonportmax; 1109 #ifndef IPNOPRIVPORTS 1110 lpmin = lowportmin; 1111 lpmax = lowportmax; 1112 #endif /* IPNOPRIVPORTS */ 1113 break; 1114 #endif /* INET */ 1115 #ifdef INET6 1116 case PF_INET6: 1117 apmin = ip6_anonportmin; 1118 apmax = ip6_anonportmax; 1119 #ifndef IPNOPRIVPORTS 1120 lpmin = ip6_lowportmin; 1121 lpmax = ip6_lowportmax; 1122 #endif /* IPNOPRIVPORTS */ 1123 break; 1124 #endif /* INET6 */ 1125 default: 1126 return (EINVAL); 1127 } 1128 1129 /* 1130 * insert temporary copy into node, perform lookup on 1131 * temporary, then restore pointer 1132 */ 1133 node = *rnode; 1134 tmp = *(int*)rnode->sysctl_data; 1135 node.sysctl_data = &tmp; 1136 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1137 if (error || newp == NULL) 1138 return (error); 1139 1140 /* 1141 * simple port range check 1142 */ 1143 if (tmp < 0 || tmp > 65535) 1144 return (EINVAL); 1145 1146 /* 1147 * per-node range checks 1148 */ 1149 switch (rnode->sysctl_num) { 1150 case IPCTL_ANONPORTMIN: 1151 case IPV6CTL_ANONPORTMIN: 1152 if (tmp >= apmax) 1153 return (EINVAL); 1154 #ifndef IPNOPRIVPORTS 1155 if (tmp < IPPORT_RESERVED) 1156 return (EINVAL); 1157 #endif /* IPNOPRIVPORTS */ 1158 break; 1159 1160 case IPCTL_ANONPORTMAX: 1161 case IPV6CTL_ANONPORTMAX: 1162 if (apmin >= tmp) 1163 return (EINVAL); 1164 #ifndef IPNOPRIVPORTS 1165 if (tmp < IPPORT_RESERVED) 1166 return (EINVAL); 1167 #endif /* IPNOPRIVPORTS */ 1168 break; 1169 1170 #ifndef IPNOPRIVPORTS 1171 case IPCTL_LOWPORTMIN: 1172 case IPV6CTL_LOWPORTMIN: 1173 if (tmp >= lpmax || 1174 tmp > IPPORT_RESERVEDMAX || 1175 tmp < IPPORT_RESERVEDMIN) 1176 return (EINVAL); 1177 break; 1178 1179 case IPCTL_LOWPORTMAX: 1180 case IPV6CTL_LOWPORTMAX: 1181 if (lpmin >= tmp || 1182 tmp > IPPORT_RESERVEDMAX || 1183 tmp < IPPORT_RESERVEDMIN) 1184 return (EINVAL); 1185 break; 1186 #endif /* IPNOPRIVPORTS */ 1187 1188 default: 1189 return (EINVAL); 1190 } 1191 1192 *(int*)rnode->sysctl_data = tmp; 1193 1194 return (0); 1195 } 1196 1197 static inline int 1198 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp) 1199 { 1200 if (oldp) { 1201 size_t sz; 1202 uid_t uid; 1203 int error; 1204 1205 if (sockp->so_cred == NULL) 1206 return EPERM; 1207 1208 uid = kauth_cred_geteuid(sockp->so_cred); 1209 sz = MIN(sizeof(uid), *oldlenp); 1210 if ((error = copyout(&uid, oldp, sz)) != 0) 1211 return error; 1212 } 1213 *oldlenp = sizeof(uid_t); 1214 return 0; 1215 } 1216 1217 static inline int 1218 inet4_ident_core(struct in_addr raddr, u_int rport, 1219 struct in_addr laddr, u_int lport, 1220 void *oldp, size_t *oldlenp, 1221 struct lwp *l, int dodrop) 1222 { 1223 struct inpcb *inp; 1224 struct socket *sockp; 1225 1226 inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0); 1227 1228 if (inp == NULL || (sockp = inp->inp_socket) == NULL) 1229 return ESRCH; 1230 1231 if (dodrop) { 1232 struct tcpcb *tp; 1233 int error; 1234 1235 if (inp == NULL || (tp = intotcpcb(inp)) == NULL || 1236 (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0) 1237 return ESRCH; 1238 1239 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 1240 KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL); 1241 if (error) 1242 return (error); 1243 1244 (void)tcp_drop(tp, ECONNABORTED); 1245 return 0; 1246 } 1247 else 1248 return copyout_uid(sockp, oldp, oldlenp); 1249 } 1250 1251 #ifdef INET6 1252 static inline int 1253 inet6_ident_core(struct in6_addr *raddr, u_int rport, 1254 struct in6_addr *laddr, u_int lport, 1255 void *oldp, size_t *oldlenp, 1256 struct lwp *l, int dodrop) 1257 { 1258 struct in6pcb *in6p; 1259 struct socket *sockp; 1260 1261 in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0); 1262 1263 if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL) 1264 return ESRCH; 1265 1266 if (dodrop) { 1267 struct tcpcb *tp; 1268 int error; 1269 1270 if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL || 1271 (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0) 1272 return ESRCH; 1273 1274 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 1275 KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL); 1276 if (error) 1277 return (error); 1278 1279 (void)tcp_drop(tp, ECONNABORTED); 1280 return 0; 1281 } 1282 else 1283 return copyout_uid(sockp, oldp, oldlenp); 1284 } 1285 #endif 1286 1287 /* 1288 * sysctl helper routine for the net.inet.tcp.drop and 1289 * net.inet6.tcp6.drop nodes. 1290 */ 1291 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident 1292 1293 /* 1294 * sysctl helper routine for the net.inet.tcp.ident and 1295 * net.inet6.tcp6.ident nodes. contains backwards compat code for the 1296 * old way of looking up the ident information for ipv4 which involves 1297 * stuffing the port/addr pairs into the mib lookup. 1298 */ 1299 static int 1300 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS) 1301 { 1302 #ifdef INET 1303 struct sockaddr_in *si4[2]; 1304 #endif /* INET */ 1305 #ifdef INET6 1306 struct sockaddr_in6 *si6[2]; 1307 #endif /* INET6 */ 1308 struct sockaddr_storage sa[2]; 1309 int error, pf, dodrop; 1310 1311 dodrop = name[-1] == TCPCTL_DROP; 1312 if (dodrop) { 1313 if (oldp != NULL || *oldlenp != 0) 1314 return EINVAL; 1315 if (newp == NULL) 1316 return EPERM; 1317 if (newlen < sizeof(sa)) 1318 return ENOMEM; 1319 } 1320 if (namelen != 4 && namelen != 0) 1321 return EINVAL; 1322 if (name[-2] != IPPROTO_TCP) 1323 return EINVAL; 1324 pf = name[-3]; 1325 1326 /* old style lookup, ipv4 only */ 1327 if (namelen == 4) { 1328 #ifdef INET 1329 struct in_addr laddr, raddr; 1330 u_int lport, rport; 1331 1332 if (pf != PF_INET) 1333 return EPROTONOSUPPORT; 1334 raddr.s_addr = (uint32_t)name[0]; 1335 rport = (u_int)name[1]; 1336 laddr.s_addr = (uint32_t)name[2]; 1337 lport = (u_int)name[3]; 1338 1339 mutex_enter(softnet_lock); 1340 error = inet4_ident_core(raddr, rport, laddr, lport, 1341 oldp, oldlenp, l, dodrop); 1342 mutex_exit(softnet_lock); 1343 return error; 1344 #else /* INET */ 1345 return EINVAL; 1346 #endif /* INET */ 1347 } 1348 1349 if (newp == NULL || newlen != sizeof(sa)) 1350 return EINVAL; 1351 error = copyin(newp, &sa, newlen); 1352 if (error) 1353 return error; 1354 1355 /* 1356 * requested families must match 1357 */ 1358 if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family) 1359 return EINVAL; 1360 1361 switch (pf) { 1362 #ifdef INET6 1363 case PF_INET6: 1364 si6[0] = (struct sockaddr_in6*)&sa[0]; 1365 si6[1] = (struct sockaddr_in6*)&sa[1]; 1366 if (si6[0]->sin6_len != sizeof(*si6[0]) || 1367 si6[1]->sin6_len != sizeof(*si6[1])) 1368 return EINVAL; 1369 1370 if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) && 1371 !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) { 1372 error = sa6_embedscope(si6[0], ip6_use_defzone); 1373 if (error) 1374 return error; 1375 error = sa6_embedscope(si6[1], ip6_use_defzone); 1376 if (error) 1377 return error; 1378 1379 mutex_enter(softnet_lock); 1380 error = inet6_ident_core(&si6[0]->sin6_addr, 1381 si6[0]->sin6_port, &si6[1]->sin6_addr, 1382 si6[1]->sin6_port, oldp, oldlenp, l, dodrop); 1383 mutex_exit(softnet_lock); 1384 return error; 1385 } 1386 1387 if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) != 1388 IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) 1389 return EINVAL; 1390 1391 in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]); 1392 in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]); 1393 /*FALLTHROUGH*/ 1394 #endif /* INET6 */ 1395 #ifdef INET 1396 case PF_INET: 1397 si4[0] = (struct sockaddr_in*)&sa[0]; 1398 si4[1] = (struct sockaddr_in*)&sa[1]; 1399 if (si4[0]->sin_len != sizeof(*si4[0]) || 1400 si4[0]->sin_len != sizeof(*si4[1])) 1401 return EINVAL; 1402 1403 mutex_enter(softnet_lock); 1404 error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port, 1405 si4[1]->sin_addr, si4[1]->sin_port, 1406 oldp, oldlenp, l, dodrop); 1407 mutex_exit(softnet_lock); 1408 return error; 1409 #endif /* INET */ 1410 default: 1411 return EPROTONOSUPPORT; 1412 } 1413 } 1414 1415 /* 1416 * sysctl helper for the inet and inet6 pcblists. handles tcp/udp and 1417 * inet/inet6, as well as raw pcbs for each. specifically not 1418 * declared static so that raw sockets and udp/udp6 can use it as 1419 * well. 1420 */ 1421 int 1422 sysctl_inpcblist(SYSCTLFN_ARGS) 1423 { 1424 #ifdef INET 1425 struct sockaddr_in *in; 1426 const struct inpcb *inp; 1427 #endif 1428 #ifdef INET6 1429 struct sockaddr_in6 *in6; 1430 const struct in6pcb *in6p; 1431 #endif 1432 struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data); 1433 const struct inpcb_hdr *inph; 1434 struct tcpcb *tp; 1435 struct kinfo_pcb pcb; 1436 char *dp; 1437 size_t len, needed, elem_size, out_size; 1438 int error, elem_count, pf, proto, pf2; 1439 1440 if (namelen != 4) 1441 return (EINVAL); 1442 1443 if (oldp != NULL) { 1444 len = *oldlenp; 1445 elem_size = name[2]; 1446 elem_count = name[3]; 1447 if (elem_size != sizeof(pcb)) 1448 return EINVAL; 1449 } else { 1450 len = 0; 1451 elem_count = INT_MAX; 1452 elem_size = sizeof(pcb); 1453 } 1454 error = 0; 1455 dp = oldp; 1456 out_size = elem_size; 1457 needed = 0; 1458 1459 if (namelen == 1 && name[0] == CTL_QUERY) 1460 return (sysctl_query(SYSCTLFN_CALL(rnode))); 1461 1462 if (name - oname != 4) 1463 return (EINVAL); 1464 1465 pf = oname[1]; 1466 proto = oname[2]; 1467 pf2 = (oldp != NULL) ? pf : 0; 1468 1469 mutex_enter(softnet_lock); 1470 1471 TAILQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) { 1472 #ifdef INET 1473 inp = (const struct inpcb *)inph; 1474 #endif 1475 #ifdef INET6 1476 in6p = (const struct in6pcb *)inph; 1477 #endif 1478 1479 if (inph->inph_af != pf) 1480 continue; 1481 1482 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 1483 KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL, 1484 NULL) != 0) 1485 continue; 1486 1487 memset(&pcb, 0, sizeof(pcb)); 1488 1489 pcb.ki_family = pf; 1490 pcb.ki_type = proto; 1491 1492 switch (pf2) { 1493 case 0: 1494 /* just probing for size */ 1495 break; 1496 #ifdef INET 1497 case PF_INET: 1498 pcb.ki_family = inp->inp_socket->so_proto-> 1499 pr_domain->dom_family; 1500 pcb.ki_type = inp->inp_socket->so_proto-> 1501 pr_type; 1502 pcb.ki_protocol = inp->inp_socket->so_proto-> 1503 pr_protocol; 1504 pcb.ki_pflags = inp->inp_flags; 1505 1506 pcb.ki_sostate = inp->inp_socket->so_state; 1507 pcb.ki_prstate = inp->inp_state; 1508 if (proto == IPPROTO_TCP) { 1509 tp = intotcpcb(inp); 1510 pcb.ki_tstate = tp->t_state; 1511 pcb.ki_tflags = tp->t_flags; 1512 } 1513 1514 pcb.ki_pcbaddr = PTRTOUINT64(inp); 1515 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb); 1516 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket); 1517 1518 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc; 1519 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc; 1520 1521 in = satosin(&pcb.ki_src); 1522 in->sin_len = sizeof(*in); 1523 in->sin_family = pf; 1524 in->sin_port = inp->inp_lport; 1525 in->sin_addr = inp->inp_laddr; 1526 if (pcb.ki_prstate >= INP_CONNECTED) { 1527 in = satosin(&pcb.ki_dst); 1528 in->sin_len = sizeof(*in); 1529 in->sin_family = pf; 1530 in->sin_port = inp->inp_fport; 1531 in->sin_addr = inp->inp_faddr; 1532 } 1533 break; 1534 #endif 1535 #ifdef INET6 1536 case PF_INET6: 1537 pcb.ki_family = in6p->in6p_socket->so_proto-> 1538 pr_domain->dom_family; 1539 pcb.ki_type = in6p->in6p_socket->so_proto->pr_type; 1540 pcb.ki_protocol = in6p->in6p_socket->so_proto-> 1541 pr_protocol; 1542 pcb.ki_pflags = in6p->in6p_flags; 1543 1544 pcb.ki_sostate = in6p->in6p_socket->so_state; 1545 pcb.ki_prstate = in6p->in6p_state; 1546 if (proto == IPPROTO_TCP) { 1547 tp = in6totcpcb(in6p); 1548 pcb.ki_tstate = tp->t_state; 1549 pcb.ki_tflags = tp->t_flags; 1550 } 1551 1552 pcb.ki_pcbaddr = PTRTOUINT64(in6p); 1553 pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb); 1554 pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket); 1555 1556 pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc; 1557 pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc; 1558 1559 in6 = satosin6(&pcb.ki_src); 1560 in6->sin6_len = sizeof(*in6); 1561 in6->sin6_family = pf; 1562 in6->sin6_port = in6p->in6p_lport; 1563 in6->sin6_flowinfo = in6p->in6p_flowinfo; 1564 in6->sin6_addr = in6p->in6p_laddr; 1565 in6->sin6_scope_id = 0; /* XXX? */ 1566 1567 if (pcb.ki_prstate >= IN6P_CONNECTED) { 1568 in6 = satosin6(&pcb.ki_dst); 1569 in6->sin6_len = sizeof(*in6); 1570 in6->sin6_family = pf; 1571 in6->sin6_port = in6p->in6p_fport; 1572 in6->sin6_flowinfo = in6p->in6p_flowinfo; 1573 in6->sin6_addr = in6p->in6p_faddr; 1574 in6->sin6_scope_id = 0; /* XXX? */ 1575 } 1576 break; 1577 #endif 1578 } 1579 1580 if (len >= elem_size && elem_count > 0) { 1581 error = copyout(&pcb, dp, out_size); 1582 if (error) { 1583 mutex_exit(softnet_lock); 1584 return (error); 1585 } 1586 dp += elem_size; 1587 len -= elem_size; 1588 } 1589 needed += elem_size; 1590 if (elem_count > 0 && elem_count != INT_MAX) 1591 elem_count--; 1592 } 1593 1594 *oldlenp = needed; 1595 if (oldp == NULL) 1596 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb); 1597 1598 mutex_exit(softnet_lock); 1599 1600 return (error); 1601 } 1602 1603 static int 1604 sysctl_tcp_congctl(SYSCTLFN_ARGS) 1605 { 1606 struct sysctlnode node; 1607 int error; 1608 char newname[TCPCC_MAXLEN]; 1609 1610 strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1); 1611 1612 node = *rnode; 1613 node.sysctl_data = newname; 1614 node.sysctl_size = sizeof(newname); 1615 1616 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1617 1618 if (error || 1619 newp == NULL || 1620 strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0) 1621 return error; 1622 1623 mutex_enter(softnet_lock); 1624 error = tcp_congctl_select(NULL, newname); 1625 mutex_exit(softnet_lock); 1626 1627 return error; 1628 } 1629 1630 static int 1631 sysctl_tcp_init_win(SYSCTLFN_ARGS) 1632 { 1633 int error; 1634 u_int iw; 1635 struct sysctlnode node; 1636 1637 iw = *(u_int *)rnode->sysctl_data; 1638 node = *rnode; 1639 node.sysctl_data = &iw; 1640 node.sysctl_size = sizeof(iw); 1641 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1642 if (error || newp == NULL) 1643 return error; 1644 1645 if (iw >= __arraycount(tcp_init_win_max)) 1646 return EINVAL; 1647 *(u_int *)rnode->sysctl_data = iw; 1648 return 0; 1649 } 1650 1651 static int 1652 sysctl_tcp_keep(SYSCTLFN_ARGS) 1653 { 1654 int error; 1655 u_int tmp; 1656 struct sysctlnode node; 1657 1658 node = *rnode; 1659 tmp = *(u_int *)rnode->sysctl_data; 1660 node.sysctl_data = &tmp; 1661 1662 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1663 if (error || newp == NULL) 1664 return error; 1665 1666 mutex_enter(softnet_lock); 1667 1668 *(u_int *)rnode->sysctl_data = tmp; 1669 tcp_tcpcb_template(); /* update the template */ 1670 1671 mutex_exit(softnet_lock); 1672 return 0; 1673 } 1674 1675 static int 1676 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS) 1677 { 1678 1679 return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS)); 1680 } 1681 1682 /* 1683 * this (second stage) setup routine is a replacement for tcp_sysctl() 1684 * (which is currently used for ipv4 and ipv6) 1685 */ 1686 static void 1687 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname, 1688 const char *tcpname) 1689 { 1690 const struct sysctlnode *sack_node; 1691 const struct sysctlnode *abc_node; 1692 const struct sysctlnode *ecn_node; 1693 const struct sysctlnode *congctl_node; 1694 const struct sysctlnode *mslt_node; 1695 const struct sysctlnode *vtw_node; 1696 #ifdef TCP_DEBUG 1697 extern struct tcp_debug tcp_debug[TCP_NDEBUG]; 1698 extern int tcp_debx; 1699 #endif 1700 1701 sysctl_createv(clog, 0, NULL, NULL, 1702 CTLFLAG_PERMANENT, 1703 CTLTYPE_NODE, "net", NULL, 1704 NULL, 0, NULL, 0, 1705 CTL_NET, CTL_EOL); 1706 sysctl_createv(clog, 0, NULL, NULL, 1707 CTLFLAG_PERMANENT, 1708 CTLTYPE_NODE, pfname, NULL, 1709 NULL, 0, NULL, 0, 1710 CTL_NET, pf, CTL_EOL); 1711 sysctl_createv(clog, 0, NULL, NULL, 1712 CTLFLAG_PERMANENT, 1713 CTLTYPE_NODE, tcpname, 1714 SYSCTL_DESCR("TCP related settings"), 1715 NULL, 0, NULL, 0, 1716 CTL_NET, pf, IPPROTO_TCP, CTL_EOL); 1717 1718 sysctl_createv(clog, 0, NULL, NULL, 1719 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1720 CTLTYPE_INT, "rfc1323", 1721 SYSCTL_DESCR("Enable RFC1323 TCP extensions"), 1722 sysctl_update_tcpcb_template, 0, &tcp_do_rfc1323, 0, 1723 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL); 1724 sysctl_createv(clog, 0, NULL, NULL, 1725 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1726 CTLTYPE_INT, "sendspace", 1727 SYSCTL_DESCR("Default TCP send buffer size"), 1728 NULL, 0, &tcp_sendspace, 0, 1729 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL); 1730 sysctl_createv(clog, 0, NULL, NULL, 1731 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1732 CTLTYPE_INT, "recvspace", 1733 SYSCTL_DESCR("Default TCP receive buffer size"), 1734 NULL, 0, &tcp_recvspace, 0, 1735 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL); 1736 sysctl_createv(clog, 0, NULL, NULL, 1737 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1738 CTLTYPE_INT, "mssdflt", 1739 SYSCTL_DESCR("Default maximum segment size"), 1740 sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0, 1741 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL); 1742 sysctl_createv(clog, 0, NULL, NULL, 1743 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1744 CTLTYPE_INT, "minmss", 1745 SYSCTL_DESCR("Lower limit for TCP maximum segment size"), 1746 NULL, 0, &tcp_minmss, 0, 1747 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1748 sysctl_createv(clog, 0, NULL, NULL, 1749 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1750 CTLTYPE_INT, "msl", 1751 SYSCTL_DESCR("Maximum Segment Life"), 1752 NULL, 0, &tcp_msl, 0, 1753 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL); 1754 sysctl_createv(clog, 0, NULL, NULL, 1755 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1756 CTLTYPE_INT, "syn_cache_limit", 1757 SYSCTL_DESCR("Maximum number of entries in the TCP " 1758 "compressed state engine"), 1759 NULL, 0, &tcp_syn_cache_limit, 0, 1760 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT, 1761 CTL_EOL); 1762 sysctl_createv(clog, 0, NULL, NULL, 1763 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1764 CTLTYPE_INT, "syn_bucket_limit", 1765 SYSCTL_DESCR("Maximum number of entries per hash " 1766 "bucket in the TCP compressed state " 1767 "engine"), 1768 NULL, 0, &tcp_syn_bucket_limit, 0, 1769 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT, 1770 CTL_EOL); 1771 #if 0 /* obsoleted */ 1772 sysctl_createv(clog, 0, NULL, NULL, 1773 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1774 CTLTYPE_INT, "syn_cache_interval", 1775 SYSCTL_DESCR("TCP compressed state engine's timer interval"), 1776 NULL, 0, &tcp_syn_cache_interval, 0, 1777 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER, 1778 CTL_EOL); 1779 #endif 1780 sysctl_createv(clog, 0, NULL, NULL, 1781 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1782 CTLTYPE_INT, "init_win", 1783 SYSCTL_DESCR("Initial TCP congestion window"), 1784 sysctl_tcp_init_win, 0, &tcp_init_win, 0, 1785 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL); 1786 sysctl_createv(clog, 0, NULL, NULL, 1787 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1788 CTLTYPE_INT, "mss_ifmtu", 1789 SYSCTL_DESCR("Use interface MTU for calculating MSS"), 1790 NULL, 0, &tcp_mss_ifmtu, 0, 1791 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL); 1792 sysctl_createv(clog, 0, NULL, &sack_node, 1793 CTLFLAG_PERMANENT, 1794 CTLTYPE_NODE, "sack", 1795 SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"), 1796 NULL, 0, NULL, 0, 1797 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL); 1798 1799 /* Congctl subtree */ 1800 sysctl_createv(clog, 0, NULL, &congctl_node, 1801 CTLFLAG_PERMANENT, 1802 CTLTYPE_NODE, "congctl", 1803 SYSCTL_DESCR("TCP Congestion Control"), 1804 NULL, 0, NULL, 0, 1805 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1806 sysctl_createv(clog, 0, &congctl_node, NULL, 1807 CTLFLAG_PERMANENT, 1808 CTLTYPE_STRING, "available", 1809 SYSCTL_DESCR("Available Congestion Control Mechanisms"), 1810 NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL); 1811 sysctl_createv(clog, 0, &congctl_node, NULL, 1812 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1813 CTLTYPE_STRING, "selected", 1814 SYSCTL_DESCR("Selected Congestion Control Mechanism"), 1815 sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN, 1816 CTL_CREATE, CTL_EOL); 1817 1818 sysctl_createv(clog, 0, NULL, NULL, 1819 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1820 CTLTYPE_INT, "win_scale", 1821 SYSCTL_DESCR("Use RFC1323 window scale options"), 1822 sysctl_update_tcpcb_template, 0, &tcp_do_win_scale, 0, 1823 CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL); 1824 sysctl_createv(clog, 0, NULL, NULL, 1825 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1826 CTLTYPE_INT, "timestamps", 1827 SYSCTL_DESCR("Use RFC1323 time stamp options"), 1828 sysctl_update_tcpcb_template, 0, &tcp_do_timestamps, 0, 1829 CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL); 1830 sysctl_createv(clog, 0, NULL, NULL, 1831 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1832 CTLTYPE_INT, "compat_42", 1833 SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"), 1834 NULL, 0, &tcp_compat_42, 0, 1835 CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL); 1836 sysctl_createv(clog, 0, NULL, NULL, 1837 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1838 CTLTYPE_INT, "cwm", 1839 SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window " 1840 "Monitoring"), 1841 NULL, 0, &tcp_cwm, 0, 1842 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL); 1843 sysctl_createv(clog, 0, NULL, NULL, 1844 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1845 CTLTYPE_INT, "cwm_burstsize", 1846 SYSCTL_DESCR("Congestion Window Monitoring allowed " 1847 "burst count in packets"), 1848 NULL, 0, &tcp_cwm_burstsize, 0, 1849 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE, 1850 CTL_EOL); 1851 sysctl_createv(clog, 0, NULL, NULL, 1852 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1853 CTLTYPE_INT, "ack_on_push", 1854 SYSCTL_DESCR("Immediately return ACK when PSH is " 1855 "received"), 1856 NULL, 0, &tcp_ack_on_push, 0, 1857 CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL); 1858 sysctl_createv(clog, 0, NULL, NULL, 1859 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1860 CTLTYPE_INT, "keepidle", 1861 SYSCTL_DESCR("Allowed connection idle ticks before a " 1862 "keepalive probe is sent"), 1863 sysctl_tcp_keep, 0, &tcp_keepidle, 0, 1864 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL); 1865 sysctl_createv(clog, 0, NULL, NULL, 1866 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1867 CTLTYPE_INT, "keepintvl", 1868 SYSCTL_DESCR("Ticks before next keepalive probe is sent"), 1869 sysctl_tcp_keep, 0, &tcp_keepintvl, 0, 1870 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL); 1871 sysctl_createv(clog, 0, NULL, NULL, 1872 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1873 CTLTYPE_INT, "keepcnt", 1874 SYSCTL_DESCR("Number of keepalive probes to send"), 1875 sysctl_tcp_keep, 0, &tcp_keepcnt, 0, 1876 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL); 1877 sysctl_createv(clog, 0, NULL, NULL, 1878 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 1879 CTLTYPE_INT, "slowhz", 1880 SYSCTL_DESCR("Keepalive ticks per second"), 1881 NULL, PR_SLOWHZ, NULL, 0, 1882 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL); 1883 sysctl_createv(clog, 0, NULL, NULL, 1884 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1885 CTLTYPE_INT, "log_refused", 1886 SYSCTL_DESCR("Log refused TCP connections"), 1887 NULL, 0, &tcp_log_refused, 0, 1888 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL); 1889 #if 0 /* obsoleted */ 1890 sysctl_createv(clog, 0, NULL, NULL, 1891 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1892 CTLTYPE_INT, "rstratelimit", NULL, 1893 NULL, 0, &tcp_rst_ratelim, 0, 1894 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL); 1895 #endif 1896 sysctl_createv(clog, 0, NULL, NULL, 1897 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1898 CTLTYPE_INT, "rstppslimit", 1899 SYSCTL_DESCR("Maximum number of RST packets to send " 1900 "per second"), 1901 NULL, 0, &tcp_rst_ppslim, 0, 1902 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL); 1903 sysctl_createv(clog, 0, NULL, NULL, 1904 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1905 CTLTYPE_INT, "delack_ticks", 1906 SYSCTL_DESCR("Number of ticks to delay sending an ACK"), 1907 NULL, 0, &tcp_delack_ticks, 0, 1908 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL); 1909 sysctl_createv(clog, 0, NULL, NULL, 1910 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1911 CTLTYPE_INT, "init_win_local", 1912 SYSCTL_DESCR("Initial TCP window size (in segments)"), 1913 sysctl_tcp_init_win, 0, &tcp_init_win_local, 0, 1914 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL, 1915 CTL_EOL); 1916 sysctl_createv(clog, 0, NULL, NULL, 1917 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1918 CTLTYPE_STRUCT, "ident", 1919 SYSCTL_DESCR("RFC1413 Identification Protocol lookups"), 1920 sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t), 1921 CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL); 1922 sysctl_createv(clog, 0, NULL, NULL, 1923 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1924 CTLTYPE_INT, "do_loopback_cksum", 1925 SYSCTL_DESCR("Perform TCP checksum on loopback"), 1926 NULL, 0, &tcp_do_loopback_cksum, 0, 1927 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM, 1928 CTL_EOL); 1929 sysctl_createv(clog, 0, NULL, NULL, 1930 CTLFLAG_PERMANENT, 1931 CTLTYPE_STRUCT, "pcblist", 1932 SYSCTL_DESCR("TCP protocol control block list"), 1933 sysctl_inpcblist, 0, &tcbtable, 0, 1934 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, 1935 CTL_EOL); 1936 sysctl_createv(clog, 0, NULL, NULL, 1937 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1938 CTLTYPE_INT, "keepinit", 1939 SYSCTL_DESCR("Ticks before initial tcp connection times out"), 1940 sysctl_tcp_keep, 0, &tcp_keepinit, 0, 1941 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1942 1943 /* TCP socket buffers auto-sizing nodes */ 1944 sysctl_createv(clog, 0, NULL, NULL, 1945 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1946 CTLTYPE_INT, "recvbuf_auto", 1947 SYSCTL_DESCR("Enable automatic receive " 1948 "buffer sizing (experimental)"), 1949 NULL, 0, &tcp_do_autorcvbuf, 0, 1950 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1951 sysctl_createv(clog, 0, NULL, NULL, 1952 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1953 CTLTYPE_INT, "recvbuf_inc", 1954 SYSCTL_DESCR("Incrementor step size of " 1955 "automatic receive buffer"), 1956 NULL, 0, &tcp_autorcvbuf_inc, 0, 1957 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1958 sysctl_createv(clog, 0, NULL, NULL, 1959 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1960 CTLTYPE_INT, "recvbuf_max", 1961 SYSCTL_DESCR("Max size of automatic receive buffer"), 1962 NULL, 0, &tcp_autorcvbuf_max, 0, 1963 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1964 1965 sysctl_createv(clog, 0, NULL, NULL, 1966 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1967 CTLTYPE_INT, "sendbuf_auto", 1968 SYSCTL_DESCR("Enable automatic send " 1969 "buffer sizing (experimental)"), 1970 NULL, 0, &tcp_do_autosndbuf, 0, 1971 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1972 sysctl_createv(clog, 0, NULL, NULL, 1973 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1974 CTLTYPE_INT, "sendbuf_inc", 1975 SYSCTL_DESCR("Incrementor step size of " 1976 "automatic send buffer"), 1977 NULL, 0, &tcp_autosndbuf_inc, 0, 1978 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1979 sysctl_createv(clog, 0, NULL, NULL, 1980 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1981 CTLTYPE_INT, "sendbuf_max", 1982 SYSCTL_DESCR("Max size of automatic send buffer"), 1983 NULL, 0, &tcp_autosndbuf_max, 0, 1984 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1985 1986 /* ECN subtree */ 1987 sysctl_createv(clog, 0, NULL, &ecn_node, 1988 CTLFLAG_PERMANENT, 1989 CTLTYPE_NODE, "ecn", 1990 SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"), 1991 NULL, 0, NULL, 0, 1992 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1993 sysctl_createv(clog, 0, &ecn_node, NULL, 1994 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1995 CTLTYPE_INT, "enable", 1996 SYSCTL_DESCR("Enable TCP Explicit Congestion " 1997 "Notification"), 1998 NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL); 1999 sysctl_createv(clog, 0, &ecn_node, NULL, 2000 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2001 CTLTYPE_INT, "maxretries", 2002 SYSCTL_DESCR("Number of times to retry ECN setup " 2003 "before disabling ECN on the connection"), 2004 NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL); 2005 2006 /* SACK gets it's own little subtree. */ 2007 sysctl_createv(clog, 0, NULL, &sack_node, 2008 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2009 CTLTYPE_INT, "enable", 2010 SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"), 2011 NULL, 0, &tcp_do_sack, 0, 2012 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 2013 sysctl_createv(clog, 0, NULL, &sack_node, 2014 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2015 CTLTYPE_INT, "maxholes", 2016 SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"), 2017 NULL, 0, &tcp_sack_tp_maxholes, 0, 2018 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 2019 sysctl_createv(clog, 0, NULL, &sack_node, 2020 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2021 CTLTYPE_INT, "globalmaxholes", 2022 SYSCTL_DESCR("Global maximum number of TCP SACK holes"), 2023 NULL, 0, &tcp_sack_globalmaxholes, 0, 2024 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 2025 sysctl_createv(clog, 0, NULL, &sack_node, 2026 CTLFLAG_PERMANENT, 2027 CTLTYPE_INT, "globalholes", 2028 SYSCTL_DESCR("Global number of TCP SACK holes"), 2029 NULL, 0, &tcp_sack_globalholes, 0, 2030 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 2031 2032 sysctl_createv(clog, 0, NULL, NULL, 2033 CTLFLAG_PERMANENT, 2034 CTLTYPE_STRUCT, "stats", 2035 SYSCTL_DESCR("TCP statistics"), 2036 sysctl_net_inet_tcp_stats, 0, NULL, 0, 2037 CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS, 2038 CTL_EOL); 2039 sysctl_createv(clog, 0, NULL, NULL, 2040 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2041 CTLTYPE_INT, "local_by_rtt", 2042 SYSCTL_DESCR("Use RTT estimator to decide which hosts " 2043 "are local"), 2044 NULL, 0, &tcp_rttlocal, 0, 2045 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2046 #ifdef TCP_DEBUG 2047 sysctl_createv(clog, 0, NULL, NULL, 2048 CTLFLAG_PERMANENT, 2049 CTLTYPE_STRUCT, "debug", 2050 SYSCTL_DESCR("TCP sockets debug information"), 2051 NULL, 0, &tcp_debug, sizeof(tcp_debug), 2052 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG, 2053 CTL_EOL); 2054 sysctl_createv(clog, 0, NULL, NULL, 2055 CTLFLAG_PERMANENT, 2056 CTLTYPE_INT, "debx", 2057 SYSCTL_DESCR("Number of TCP debug sockets messages"), 2058 NULL, 0, &tcp_debx, sizeof(tcp_debx), 2059 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX, 2060 CTL_EOL); 2061 #endif 2062 sysctl_createv(clog, 0, NULL, NULL, 2063 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2064 CTLTYPE_STRUCT, "drop", 2065 SYSCTL_DESCR("TCP drop connection"), 2066 sysctl_net_inet_tcp_drop, 0, NULL, 0, 2067 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL); 2068 sysctl_createv(clog, 0, NULL, NULL, 2069 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2070 CTLTYPE_INT, "iss_hash", 2071 SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic " 2072 "hash computation"), 2073 NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948), 2074 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, 2075 CTL_EOL); 2076 2077 /* ABC subtree */ 2078 2079 sysctl_createv(clog, 0, NULL, &abc_node, 2080 CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc", 2081 SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"), 2082 NULL, 0, NULL, 0, 2083 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2084 sysctl_createv(clog, 0, &abc_node, NULL, 2085 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2086 CTLTYPE_INT, "enable", 2087 SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"), 2088 NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL); 2089 sysctl_createv(clog, 0, &abc_node, NULL, 2090 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2091 CTLTYPE_INT, "aggressive", 2092 SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"), 2093 NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL); 2094 2095 /* MSL tuning subtree */ 2096 2097 sysctl_createv(clog, 0, NULL, &mslt_node, 2098 CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt", 2099 SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"), 2100 NULL, 0, NULL, 0, 2101 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2102 sysctl_createv(clog, 0, &mslt_node, NULL, 2103 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2104 CTLTYPE_INT, "enable", 2105 SYSCTL_DESCR("Enable TIME_WAIT truncation"), 2106 NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL); 2107 sysctl_createv(clog, 0, &mslt_node, NULL, 2108 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2109 CTLTYPE_INT, "loopback", 2110 SYSCTL_DESCR("MSL value to use for loopback connections"), 2111 NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL); 2112 sysctl_createv(clog, 0, &mslt_node, NULL, 2113 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2114 CTLTYPE_INT, "local", 2115 SYSCTL_DESCR("MSL value to use for local connections"), 2116 NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL); 2117 sysctl_createv(clog, 0, &mslt_node, NULL, 2118 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2119 CTLTYPE_INT, "remote", 2120 SYSCTL_DESCR("MSL value to use for remote connections"), 2121 NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL); 2122 sysctl_createv(clog, 0, &mslt_node, NULL, 2123 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2124 CTLTYPE_INT, "remote_threshold", 2125 SYSCTL_DESCR("RTT estimate value to promote local to remote"), 2126 NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL); 2127 2128 /* vestigial TIME_WAIT tuning subtree */ 2129 2130 sysctl_createv(clog, 0, NULL, &vtw_node, 2131 CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw", 2132 SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"), 2133 NULL, 0, NULL, 0, 2134 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2135 sysctl_createv(clog, 0, &vtw_node, NULL, 2136 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2137 CTLTYPE_INT, "enable", 2138 SYSCTL_DESCR("Enable Vestigial TIME_WAIT"), 2139 sysctl_tcp_vtw_enable, 0, 2140 (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable, 2141 0, CTL_CREATE, CTL_EOL); 2142 sysctl_createv(clog, 0, &vtw_node, NULL, 2143 CTLFLAG_PERMANENT|CTLFLAG_READONLY, 2144 CTLTYPE_INT, "entries", 2145 SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"), 2146 NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL); 2147 } 2148 2149 void 2150 tcp_usrreq_init(void) 2151 { 2152 2153 #ifdef INET 2154 sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp"); 2155 #endif 2156 #ifdef INET6 2157 sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6"); 2158 #endif 2159 } 2160