1 /* $NetBSD: tcp_usrreq.c,v 1.168 2013/10/04 16:20:35 christos 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.168 2013/10/04 16:20:35 christos 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 return (0); 1048 } 1049 1050 /* 1051 * sysctl helper routine for setting port related values under 1052 * net.inet.ip and net.inet6.ip6. does basic range checking and does 1053 * additional checks for each type. this code has placed in 1054 * tcp_input.c since INET and INET6 both use the same tcp code. 1055 * 1056 * this helper is not static so that both inet and inet6 can use it. 1057 */ 1058 int 1059 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS) 1060 { 1061 int error, tmp; 1062 int apmin, apmax; 1063 #ifndef IPNOPRIVPORTS 1064 int lpmin, lpmax; 1065 #endif /* IPNOPRIVPORTS */ 1066 struct sysctlnode node; 1067 1068 if (namelen != 0) 1069 return (EINVAL); 1070 1071 switch (name[-3]) { 1072 #ifdef INET 1073 case PF_INET: 1074 apmin = anonportmin; 1075 apmax = anonportmax; 1076 #ifndef IPNOPRIVPORTS 1077 lpmin = lowportmin; 1078 lpmax = lowportmax; 1079 #endif /* IPNOPRIVPORTS */ 1080 break; 1081 #endif /* INET */ 1082 #ifdef INET6 1083 case PF_INET6: 1084 apmin = ip6_anonportmin; 1085 apmax = ip6_anonportmax; 1086 #ifndef IPNOPRIVPORTS 1087 lpmin = ip6_lowportmin; 1088 lpmax = ip6_lowportmax; 1089 #endif /* IPNOPRIVPORTS */ 1090 break; 1091 #endif /* INET6 */ 1092 default: 1093 return (EINVAL); 1094 } 1095 1096 /* 1097 * insert temporary copy into node, perform lookup on 1098 * temporary, then restore pointer 1099 */ 1100 node = *rnode; 1101 tmp = *(int*)rnode->sysctl_data; 1102 node.sysctl_data = &tmp; 1103 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1104 if (error || newp == NULL) 1105 return (error); 1106 1107 /* 1108 * simple port range check 1109 */ 1110 if (tmp < 0 || tmp > 65535) 1111 return (EINVAL); 1112 1113 /* 1114 * per-node range checks 1115 */ 1116 switch (rnode->sysctl_num) { 1117 case IPCTL_ANONPORTMIN: 1118 case IPV6CTL_ANONPORTMIN: 1119 if (tmp >= apmax) 1120 return (EINVAL); 1121 #ifndef IPNOPRIVPORTS 1122 if (tmp < IPPORT_RESERVED) 1123 return (EINVAL); 1124 #endif /* IPNOPRIVPORTS */ 1125 break; 1126 1127 case IPCTL_ANONPORTMAX: 1128 case IPV6CTL_ANONPORTMAX: 1129 if (apmin >= tmp) 1130 return (EINVAL); 1131 #ifndef IPNOPRIVPORTS 1132 if (tmp < IPPORT_RESERVED) 1133 return (EINVAL); 1134 #endif /* IPNOPRIVPORTS */ 1135 break; 1136 1137 #ifndef IPNOPRIVPORTS 1138 case IPCTL_LOWPORTMIN: 1139 case IPV6CTL_LOWPORTMIN: 1140 if (tmp >= lpmax || 1141 tmp > IPPORT_RESERVEDMAX || 1142 tmp < IPPORT_RESERVEDMIN) 1143 return (EINVAL); 1144 break; 1145 1146 case IPCTL_LOWPORTMAX: 1147 case IPV6CTL_LOWPORTMAX: 1148 if (lpmin >= tmp || 1149 tmp > IPPORT_RESERVEDMAX || 1150 tmp < IPPORT_RESERVEDMIN) 1151 return (EINVAL); 1152 break; 1153 #endif /* IPNOPRIVPORTS */ 1154 1155 default: 1156 return (EINVAL); 1157 } 1158 1159 *(int*)rnode->sysctl_data = tmp; 1160 1161 return (0); 1162 } 1163 1164 static inline int 1165 copyout_uid(struct socket *sockp, void *oldp, size_t *oldlenp) 1166 { 1167 if (oldp) { 1168 size_t sz; 1169 uid_t uid; 1170 int error; 1171 1172 if (sockp->so_cred == NULL) 1173 return EPERM; 1174 1175 uid = kauth_cred_geteuid(sockp->so_cred); 1176 sz = MIN(sizeof(uid), *oldlenp); 1177 if ((error = copyout(&uid, oldp, sz)) != 0) 1178 return error; 1179 } 1180 *oldlenp = sizeof(uid_t); 1181 return 0; 1182 } 1183 1184 static inline int 1185 inet4_ident_core(struct in_addr raddr, u_int rport, 1186 struct in_addr laddr, u_int lport, 1187 void *oldp, size_t *oldlenp, 1188 struct lwp *l, int dodrop) 1189 { 1190 struct inpcb *inp; 1191 struct socket *sockp; 1192 1193 inp = in_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0); 1194 1195 if (inp == NULL || (sockp = inp->inp_socket) == NULL) 1196 return ESRCH; 1197 1198 if (dodrop) { 1199 struct tcpcb *tp; 1200 int error; 1201 1202 if (inp == NULL || (tp = intotcpcb(inp)) == NULL || 1203 (inp->inp_socket->so_options & SO_ACCEPTCONN) != 0) 1204 return ESRCH; 1205 1206 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 1207 KAUTH_REQ_NETWORK_SOCKET_DROP, inp->inp_socket, tp, NULL); 1208 if (error) 1209 return (error); 1210 1211 (void)tcp_drop(tp, ECONNABORTED); 1212 return 0; 1213 } 1214 else 1215 return copyout_uid(sockp, oldp, oldlenp); 1216 } 1217 1218 #ifdef INET6 1219 static inline int 1220 inet6_ident_core(struct in6_addr *raddr, u_int rport, 1221 struct in6_addr *laddr, u_int lport, 1222 void *oldp, size_t *oldlenp, 1223 struct lwp *l, int dodrop) 1224 { 1225 struct in6pcb *in6p; 1226 struct socket *sockp; 1227 1228 in6p = in6_pcblookup_connect(&tcbtable, raddr, rport, laddr, lport, 0, 0); 1229 1230 if (in6p == NULL || (sockp = in6p->in6p_socket) == NULL) 1231 return ESRCH; 1232 1233 if (dodrop) { 1234 struct tcpcb *tp; 1235 int error; 1236 1237 if (in6p == NULL || (tp = in6totcpcb(in6p)) == NULL || 1238 (in6p->in6p_socket->so_options & SO_ACCEPTCONN) != 0) 1239 return ESRCH; 1240 1241 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 1242 KAUTH_REQ_NETWORK_SOCKET_DROP, in6p->in6p_socket, tp, NULL); 1243 if (error) 1244 return (error); 1245 1246 (void)tcp_drop(tp, ECONNABORTED); 1247 return 0; 1248 } 1249 else 1250 return copyout_uid(sockp, oldp, oldlenp); 1251 } 1252 #endif 1253 1254 /* 1255 * sysctl helper routine for the net.inet.tcp.drop and 1256 * net.inet6.tcp6.drop nodes. 1257 */ 1258 #define sysctl_net_inet_tcp_drop sysctl_net_inet_tcp_ident 1259 1260 /* 1261 * sysctl helper routine for the net.inet.tcp.ident and 1262 * net.inet6.tcp6.ident nodes. contains backwards compat code for the 1263 * old way of looking up the ident information for ipv4 which involves 1264 * stuffing the port/addr pairs into the mib lookup. 1265 */ 1266 static int 1267 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS) 1268 { 1269 #ifdef INET 1270 struct sockaddr_in *si4[2]; 1271 #endif /* INET */ 1272 #ifdef INET6 1273 struct sockaddr_in6 *si6[2]; 1274 #endif /* INET6 */ 1275 struct sockaddr_storage sa[2]; 1276 int error, pf, dodrop; 1277 1278 dodrop = name[-1] == TCPCTL_DROP; 1279 if (dodrop) { 1280 if (oldp != NULL || *oldlenp != 0) 1281 return EINVAL; 1282 if (newp == NULL) 1283 return EPERM; 1284 if (newlen < sizeof(sa)) 1285 return ENOMEM; 1286 } 1287 if (namelen != 4 && namelen != 0) 1288 return EINVAL; 1289 if (name[-2] != IPPROTO_TCP) 1290 return EINVAL; 1291 pf = name[-3]; 1292 1293 /* old style lookup, ipv4 only */ 1294 if (namelen == 4) { 1295 #ifdef INET 1296 struct in_addr laddr, raddr; 1297 u_int lport, rport; 1298 1299 if (pf != PF_INET) 1300 return EPROTONOSUPPORT; 1301 raddr.s_addr = (uint32_t)name[0]; 1302 rport = (u_int)name[1]; 1303 laddr.s_addr = (uint32_t)name[2]; 1304 lport = (u_int)name[3]; 1305 1306 mutex_enter(softnet_lock); 1307 error = inet4_ident_core(raddr, rport, laddr, lport, 1308 oldp, oldlenp, l, dodrop); 1309 mutex_exit(softnet_lock); 1310 return error; 1311 #else /* INET */ 1312 return EINVAL; 1313 #endif /* INET */ 1314 } 1315 1316 if (newp == NULL || newlen != sizeof(sa)) 1317 return EINVAL; 1318 error = copyin(newp, &sa, newlen); 1319 if (error) 1320 return error; 1321 1322 /* 1323 * requested families must match 1324 */ 1325 if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family) 1326 return EINVAL; 1327 1328 switch (pf) { 1329 #ifdef INET6 1330 case PF_INET6: 1331 si6[0] = (struct sockaddr_in6*)&sa[0]; 1332 si6[1] = (struct sockaddr_in6*)&sa[1]; 1333 if (si6[0]->sin6_len != sizeof(*si6[0]) || 1334 si6[1]->sin6_len != sizeof(*si6[1])) 1335 return EINVAL; 1336 1337 if (!IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) && 1338 !IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) { 1339 error = sa6_embedscope(si6[0], ip6_use_defzone); 1340 if (error) 1341 return error; 1342 error = sa6_embedscope(si6[1], ip6_use_defzone); 1343 if (error) 1344 return error; 1345 1346 mutex_enter(softnet_lock); 1347 error = inet6_ident_core(&si6[0]->sin6_addr, 1348 si6[0]->sin6_port, &si6[1]->sin6_addr, 1349 si6[1]->sin6_port, oldp, oldlenp, l, dodrop); 1350 mutex_exit(softnet_lock); 1351 return error; 1352 } 1353 1354 if (IN6_IS_ADDR_V4MAPPED(&si6[0]->sin6_addr) != 1355 IN6_IS_ADDR_V4MAPPED(&si6[1]->sin6_addr)) 1356 return EINVAL; 1357 1358 in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[0]); 1359 in6_sin6_2_sin_in_sock((struct sockaddr *)&sa[1]); 1360 /*FALLTHROUGH*/ 1361 #endif /* INET6 */ 1362 #ifdef INET 1363 case PF_INET: 1364 si4[0] = (struct sockaddr_in*)&sa[0]; 1365 si4[1] = (struct sockaddr_in*)&sa[1]; 1366 if (si4[0]->sin_len != sizeof(*si4[0]) || 1367 si4[0]->sin_len != sizeof(*si4[1])) 1368 return EINVAL; 1369 1370 mutex_enter(softnet_lock); 1371 error = inet4_ident_core(si4[0]->sin_addr, si4[0]->sin_port, 1372 si4[1]->sin_addr, si4[1]->sin_port, 1373 oldp, oldlenp, l, dodrop); 1374 mutex_exit(softnet_lock); 1375 return error; 1376 #endif /* INET */ 1377 default: 1378 return EPROTONOSUPPORT; 1379 } 1380 } 1381 1382 /* 1383 * sysctl helper for the inet and inet6 pcblists. handles tcp/udp and 1384 * inet/inet6, as well as raw pcbs for each. specifically not 1385 * declared static so that raw sockets and udp/udp6 can use it as 1386 * well. 1387 */ 1388 int 1389 sysctl_inpcblist(SYSCTLFN_ARGS) 1390 { 1391 #ifdef INET 1392 struct sockaddr_in *in; 1393 const struct inpcb *inp; 1394 #endif 1395 #ifdef INET6 1396 struct sockaddr_in6 *in6; 1397 const struct in6pcb *in6p; 1398 #endif 1399 /* 1400 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const 1401 * struct inpcbtable pointer, so we have to discard const. :-/ 1402 */ 1403 struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data); 1404 const struct inpcb_hdr *inph; 1405 struct tcpcb *tp; 1406 struct kinfo_pcb pcb; 1407 char *dp; 1408 size_t len, needed, elem_size, out_size; 1409 int error, elem_count, pf, proto, pf2; 1410 1411 if (namelen != 4) 1412 return (EINVAL); 1413 1414 if (oldp != NULL) { 1415 len = *oldlenp; 1416 elem_size = name[2]; 1417 elem_count = name[3]; 1418 if (elem_size != sizeof(pcb)) 1419 return EINVAL; 1420 } else { 1421 len = 0; 1422 elem_count = INT_MAX; 1423 elem_size = sizeof(pcb); 1424 } 1425 error = 0; 1426 dp = oldp; 1427 out_size = elem_size; 1428 needed = 0; 1429 1430 if (namelen == 1 && name[0] == CTL_QUERY) 1431 return (sysctl_query(SYSCTLFN_CALL(rnode))); 1432 1433 if (name - oname != 4) 1434 return (EINVAL); 1435 1436 pf = oname[1]; 1437 proto = oname[2]; 1438 pf2 = (oldp != NULL) ? pf : 0; 1439 1440 mutex_enter(softnet_lock); 1441 1442 CIRCLEQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) { 1443 #ifdef INET 1444 inp = (const struct inpcb *)inph; 1445 #endif 1446 #ifdef INET6 1447 in6p = (const struct in6pcb *)inph; 1448 #endif 1449 1450 if (inph->inph_af != pf) 1451 continue; 1452 1453 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 1454 KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL, 1455 NULL) != 0) 1456 continue; 1457 1458 memset(&pcb, 0, sizeof(pcb)); 1459 1460 pcb.ki_family = pf; 1461 pcb.ki_type = proto; 1462 1463 switch (pf2) { 1464 case 0: 1465 /* just probing for size */ 1466 break; 1467 #ifdef INET 1468 case PF_INET: 1469 pcb.ki_family = inp->inp_socket->so_proto-> 1470 pr_domain->dom_family; 1471 pcb.ki_type = inp->inp_socket->so_proto-> 1472 pr_type; 1473 pcb.ki_protocol = inp->inp_socket->so_proto-> 1474 pr_protocol; 1475 pcb.ki_pflags = inp->inp_flags; 1476 1477 pcb.ki_sostate = inp->inp_socket->so_state; 1478 pcb.ki_prstate = inp->inp_state; 1479 if (proto == IPPROTO_TCP) { 1480 tp = intotcpcb(inp); 1481 pcb.ki_tstate = tp->t_state; 1482 pcb.ki_tflags = tp->t_flags; 1483 } 1484 1485 pcb.ki_pcbaddr = PTRTOUINT64(inp); 1486 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb); 1487 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket); 1488 1489 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc; 1490 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc; 1491 1492 in = satosin(&pcb.ki_src); 1493 in->sin_len = sizeof(*in); 1494 in->sin_family = pf; 1495 in->sin_port = inp->inp_lport; 1496 in->sin_addr = inp->inp_laddr; 1497 if (pcb.ki_prstate >= INP_CONNECTED) { 1498 in = satosin(&pcb.ki_dst); 1499 in->sin_len = sizeof(*in); 1500 in->sin_family = pf; 1501 in->sin_port = inp->inp_fport; 1502 in->sin_addr = inp->inp_faddr; 1503 } 1504 break; 1505 #endif 1506 #ifdef INET6 1507 case PF_INET6: 1508 pcb.ki_family = in6p->in6p_socket->so_proto-> 1509 pr_domain->dom_family; 1510 pcb.ki_type = in6p->in6p_socket->so_proto->pr_type; 1511 pcb.ki_protocol = in6p->in6p_socket->so_proto-> 1512 pr_protocol; 1513 pcb.ki_pflags = in6p->in6p_flags; 1514 1515 pcb.ki_sostate = in6p->in6p_socket->so_state; 1516 pcb.ki_prstate = in6p->in6p_state; 1517 if (proto == IPPROTO_TCP) { 1518 tp = in6totcpcb(in6p); 1519 pcb.ki_tstate = tp->t_state; 1520 pcb.ki_tflags = tp->t_flags; 1521 } 1522 1523 pcb.ki_pcbaddr = PTRTOUINT64(in6p); 1524 pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb); 1525 pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket); 1526 1527 pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc; 1528 pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc; 1529 1530 in6 = satosin6(&pcb.ki_src); 1531 in6->sin6_len = sizeof(*in6); 1532 in6->sin6_family = pf; 1533 in6->sin6_port = in6p->in6p_lport; 1534 in6->sin6_flowinfo = in6p->in6p_flowinfo; 1535 in6->sin6_addr = in6p->in6p_laddr; 1536 in6->sin6_scope_id = 0; /* XXX? */ 1537 1538 if (pcb.ki_prstate >= IN6P_CONNECTED) { 1539 in6 = satosin6(&pcb.ki_dst); 1540 in6->sin6_len = sizeof(*in6); 1541 in6->sin6_family = pf; 1542 in6->sin6_port = in6p->in6p_fport; 1543 in6->sin6_flowinfo = in6p->in6p_flowinfo; 1544 in6->sin6_addr = in6p->in6p_faddr; 1545 in6->sin6_scope_id = 0; /* XXX? */ 1546 } 1547 break; 1548 #endif 1549 } 1550 1551 if (len >= elem_size && elem_count > 0) { 1552 error = copyout(&pcb, dp, out_size); 1553 if (error) { 1554 mutex_exit(softnet_lock); 1555 return (error); 1556 } 1557 dp += elem_size; 1558 len -= elem_size; 1559 } 1560 needed += elem_size; 1561 if (elem_count > 0 && elem_count != INT_MAX) 1562 elem_count--; 1563 } 1564 1565 *oldlenp = needed; 1566 if (oldp == NULL) 1567 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb); 1568 1569 mutex_exit(softnet_lock); 1570 1571 return (error); 1572 } 1573 1574 static int 1575 sysctl_tcp_congctl(SYSCTLFN_ARGS) 1576 { 1577 struct sysctlnode node; 1578 int error; 1579 char newname[TCPCC_MAXLEN]; 1580 1581 strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1); 1582 1583 node = *rnode; 1584 node.sysctl_data = newname; 1585 node.sysctl_size = sizeof(newname); 1586 1587 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1588 1589 if (error || 1590 newp == NULL || 1591 strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0) 1592 return error; 1593 1594 mutex_enter(softnet_lock); 1595 error = tcp_congctl_select(NULL, newname); 1596 mutex_exit(softnet_lock); 1597 1598 return error; 1599 } 1600 1601 static int 1602 sysctl_tcp_init_win(SYSCTLFN_ARGS) 1603 { 1604 int error; 1605 u_int iw; 1606 struct sysctlnode node; 1607 1608 iw = *(u_int *)rnode->sysctl_data; 1609 node = *rnode; 1610 node.sysctl_data = &iw; 1611 node.sysctl_size = sizeof(iw); 1612 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1613 if (error || newp == NULL) 1614 return error; 1615 1616 if (iw >= __arraycount(tcp_init_win_max)) 1617 return EINVAL; 1618 *(u_int *)rnode->sysctl_data = iw; 1619 return 0; 1620 } 1621 1622 static int 1623 sysctl_tcp_keep(SYSCTLFN_ARGS) 1624 { 1625 int error; 1626 u_int tmp; 1627 struct sysctlnode node; 1628 1629 node = *rnode; 1630 tmp = *(u_int *)rnode->sysctl_data; 1631 node.sysctl_data = &tmp; 1632 1633 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1634 if (error || newp == NULL) 1635 return error; 1636 1637 mutex_enter(softnet_lock); 1638 1639 *(u_int *)rnode->sysctl_data = tmp; 1640 tcp_tcpcb_template(); /* update the template */ 1641 1642 mutex_exit(softnet_lock); 1643 return 0; 1644 } 1645 1646 static int 1647 sysctl_net_inet_tcp_stats(SYSCTLFN_ARGS) 1648 { 1649 1650 return (NETSTAT_SYSCTL(tcpstat_percpu, TCP_NSTATS)); 1651 } 1652 1653 /* 1654 * this (second stage) setup routine is a replacement for tcp_sysctl() 1655 * (which is currently used for ipv4 and ipv6) 1656 */ 1657 static void 1658 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname, 1659 const char *tcpname) 1660 { 1661 const struct sysctlnode *sack_node; 1662 const struct sysctlnode *abc_node; 1663 const struct sysctlnode *ecn_node; 1664 const struct sysctlnode *congctl_node; 1665 const struct sysctlnode *mslt_node; 1666 const struct sysctlnode *vtw_node; 1667 #ifdef TCP_DEBUG 1668 extern struct tcp_debug tcp_debug[TCP_NDEBUG]; 1669 extern int tcp_debx; 1670 #endif 1671 1672 sysctl_createv(clog, 0, NULL, NULL, 1673 CTLFLAG_PERMANENT, 1674 CTLTYPE_NODE, "net", NULL, 1675 NULL, 0, NULL, 0, 1676 CTL_NET, CTL_EOL); 1677 sysctl_createv(clog, 0, NULL, NULL, 1678 CTLFLAG_PERMANENT, 1679 CTLTYPE_NODE, pfname, NULL, 1680 NULL, 0, NULL, 0, 1681 CTL_NET, pf, CTL_EOL); 1682 sysctl_createv(clog, 0, NULL, NULL, 1683 CTLFLAG_PERMANENT, 1684 CTLTYPE_NODE, tcpname, 1685 SYSCTL_DESCR("TCP related settings"), 1686 NULL, 0, NULL, 0, 1687 CTL_NET, pf, IPPROTO_TCP, CTL_EOL); 1688 1689 sysctl_createv(clog, 0, NULL, NULL, 1690 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1691 CTLTYPE_INT, "rfc1323", 1692 SYSCTL_DESCR("Enable RFC1323 TCP extensions"), 1693 NULL, 0, &tcp_do_rfc1323, 0, 1694 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL); 1695 sysctl_createv(clog, 0, NULL, NULL, 1696 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1697 CTLTYPE_INT, "sendspace", 1698 SYSCTL_DESCR("Default TCP send buffer size"), 1699 NULL, 0, &tcp_sendspace, 0, 1700 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL); 1701 sysctl_createv(clog, 0, NULL, NULL, 1702 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1703 CTLTYPE_INT, "recvspace", 1704 SYSCTL_DESCR("Default TCP receive buffer size"), 1705 NULL, 0, &tcp_recvspace, 0, 1706 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL); 1707 sysctl_createv(clog, 0, NULL, NULL, 1708 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1709 CTLTYPE_INT, "mssdflt", 1710 SYSCTL_DESCR("Default maximum segment size"), 1711 sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0, 1712 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL); 1713 sysctl_createv(clog, 0, NULL, NULL, 1714 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1715 CTLTYPE_INT, "minmss", 1716 SYSCTL_DESCR("Lower limit for TCP maximum segment size"), 1717 NULL, 0, &tcp_minmss, 0, 1718 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1719 sysctl_createv(clog, 0, NULL, NULL, 1720 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1721 CTLTYPE_INT, "msl", 1722 SYSCTL_DESCR("Maximum Segment Life"), 1723 NULL, 0, &tcp_msl, 0, 1724 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSL, CTL_EOL); 1725 sysctl_createv(clog, 0, NULL, NULL, 1726 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1727 CTLTYPE_INT, "syn_cache_limit", 1728 SYSCTL_DESCR("Maximum number of entries in the TCP " 1729 "compressed state engine"), 1730 NULL, 0, &tcp_syn_cache_limit, 0, 1731 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT, 1732 CTL_EOL); 1733 sysctl_createv(clog, 0, NULL, NULL, 1734 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1735 CTLTYPE_INT, "syn_bucket_limit", 1736 SYSCTL_DESCR("Maximum number of entries per hash " 1737 "bucket in the TCP compressed state " 1738 "engine"), 1739 NULL, 0, &tcp_syn_bucket_limit, 0, 1740 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT, 1741 CTL_EOL); 1742 #if 0 /* obsoleted */ 1743 sysctl_createv(clog, 0, NULL, NULL, 1744 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1745 CTLTYPE_INT, "syn_cache_interval", 1746 SYSCTL_DESCR("TCP compressed state engine's timer interval"), 1747 NULL, 0, &tcp_syn_cache_interval, 0, 1748 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER, 1749 CTL_EOL); 1750 #endif 1751 sysctl_createv(clog, 0, NULL, NULL, 1752 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1753 CTLTYPE_INT, "init_win", 1754 SYSCTL_DESCR("Initial TCP congestion window"), 1755 sysctl_tcp_init_win, 0, &tcp_init_win, 0, 1756 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL); 1757 sysctl_createv(clog, 0, NULL, NULL, 1758 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1759 CTLTYPE_INT, "mss_ifmtu", 1760 SYSCTL_DESCR("Use interface MTU for calculating MSS"), 1761 NULL, 0, &tcp_mss_ifmtu, 0, 1762 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL); 1763 sysctl_createv(clog, 0, NULL, &sack_node, 1764 CTLFLAG_PERMANENT, 1765 CTLTYPE_NODE, "sack", 1766 SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"), 1767 NULL, 0, NULL, 0, 1768 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL); 1769 1770 /* Congctl subtree */ 1771 sysctl_createv(clog, 0, NULL, &congctl_node, 1772 CTLFLAG_PERMANENT, 1773 CTLTYPE_NODE, "congctl", 1774 SYSCTL_DESCR("TCP Congestion Control"), 1775 NULL, 0, NULL, 0, 1776 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1777 sysctl_createv(clog, 0, &congctl_node, NULL, 1778 CTLFLAG_PERMANENT, 1779 CTLTYPE_STRING, "available", 1780 SYSCTL_DESCR("Available Congestion Control Mechanisms"), 1781 NULL, 0, tcp_congctl_avail, 0, CTL_CREATE, CTL_EOL); 1782 sysctl_createv(clog, 0, &congctl_node, NULL, 1783 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1784 CTLTYPE_STRING, "selected", 1785 SYSCTL_DESCR("Selected Congestion Control Mechanism"), 1786 sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN, 1787 CTL_CREATE, CTL_EOL); 1788 1789 sysctl_createv(clog, 0, NULL, NULL, 1790 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1791 CTLTYPE_INT, "win_scale", 1792 SYSCTL_DESCR("Use RFC1323 window scale options"), 1793 NULL, 0, &tcp_do_win_scale, 0, 1794 CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL); 1795 sysctl_createv(clog, 0, NULL, NULL, 1796 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1797 CTLTYPE_INT, "timestamps", 1798 SYSCTL_DESCR("Use RFC1323 time stamp options"), 1799 NULL, 0, &tcp_do_timestamps, 0, 1800 CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL); 1801 sysctl_createv(clog, 0, NULL, NULL, 1802 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1803 CTLTYPE_INT, "compat_42", 1804 SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"), 1805 NULL, 0, &tcp_compat_42, 0, 1806 CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL); 1807 sysctl_createv(clog, 0, NULL, NULL, 1808 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1809 CTLTYPE_INT, "cwm", 1810 SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window " 1811 "Monitoring"), 1812 NULL, 0, &tcp_cwm, 0, 1813 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL); 1814 sysctl_createv(clog, 0, NULL, NULL, 1815 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1816 CTLTYPE_INT, "cwm_burstsize", 1817 SYSCTL_DESCR("Congestion Window Monitoring allowed " 1818 "burst count in packets"), 1819 NULL, 0, &tcp_cwm_burstsize, 0, 1820 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE, 1821 CTL_EOL); 1822 sysctl_createv(clog, 0, NULL, NULL, 1823 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1824 CTLTYPE_INT, "ack_on_push", 1825 SYSCTL_DESCR("Immediately return ACK when PSH is " 1826 "received"), 1827 NULL, 0, &tcp_ack_on_push, 0, 1828 CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL); 1829 sysctl_createv(clog, 0, NULL, NULL, 1830 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1831 CTLTYPE_INT, "keepidle", 1832 SYSCTL_DESCR("Allowed connection idle ticks before a " 1833 "keepalive probe is sent"), 1834 sysctl_tcp_keep, 0, &tcp_keepidle, 0, 1835 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL); 1836 sysctl_createv(clog, 0, NULL, NULL, 1837 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1838 CTLTYPE_INT, "keepintvl", 1839 SYSCTL_DESCR("Ticks before next keepalive probe is sent"), 1840 sysctl_tcp_keep, 0, &tcp_keepintvl, 0, 1841 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL); 1842 sysctl_createv(clog, 0, NULL, NULL, 1843 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1844 CTLTYPE_INT, "keepcnt", 1845 SYSCTL_DESCR("Number of keepalive probes to send"), 1846 sysctl_tcp_keep, 0, &tcp_keepcnt, 0, 1847 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL); 1848 sysctl_createv(clog, 0, NULL, NULL, 1849 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 1850 CTLTYPE_INT, "slowhz", 1851 SYSCTL_DESCR("Keepalive ticks per second"), 1852 NULL, PR_SLOWHZ, NULL, 0, 1853 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL); 1854 sysctl_createv(clog, 0, NULL, NULL, 1855 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1856 CTLTYPE_INT, "log_refused", 1857 SYSCTL_DESCR("Log refused TCP connections"), 1858 NULL, 0, &tcp_log_refused, 0, 1859 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL); 1860 #if 0 /* obsoleted */ 1861 sysctl_createv(clog, 0, NULL, NULL, 1862 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1863 CTLTYPE_INT, "rstratelimit", NULL, 1864 NULL, 0, &tcp_rst_ratelim, 0, 1865 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL); 1866 #endif 1867 sysctl_createv(clog, 0, NULL, NULL, 1868 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1869 CTLTYPE_INT, "rstppslimit", 1870 SYSCTL_DESCR("Maximum number of RST packets to send " 1871 "per second"), 1872 NULL, 0, &tcp_rst_ppslim, 0, 1873 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL); 1874 sysctl_createv(clog, 0, NULL, NULL, 1875 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1876 CTLTYPE_INT, "delack_ticks", 1877 SYSCTL_DESCR("Number of ticks to delay sending an ACK"), 1878 NULL, 0, &tcp_delack_ticks, 0, 1879 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL); 1880 sysctl_createv(clog, 0, NULL, NULL, 1881 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1882 CTLTYPE_INT, "init_win_local", 1883 SYSCTL_DESCR("Initial TCP window size (in segments)"), 1884 sysctl_tcp_init_win, 0, &tcp_init_win_local, 0, 1885 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL, 1886 CTL_EOL); 1887 sysctl_createv(clog, 0, NULL, NULL, 1888 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1889 CTLTYPE_STRUCT, "ident", 1890 SYSCTL_DESCR("RFC1413 Identification Protocol lookups"), 1891 sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t), 1892 CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL); 1893 sysctl_createv(clog, 0, NULL, NULL, 1894 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1895 CTLTYPE_INT, "do_loopback_cksum", 1896 SYSCTL_DESCR("Perform TCP checksum on loopback"), 1897 NULL, 0, &tcp_do_loopback_cksum, 0, 1898 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM, 1899 CTL_EOL); 1900 sysctl_createv(clog, 0, NULL, NULL, 1901 CTLFLAG_PERMANENT, 1902 CTLTYPE_STRUCT, "pcblist", 1903 SYSCTL_DESCR("TCP protocol control block list"), 1904 sysctl_inpcblist, 0, &tcbtable, 0, 1905 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, 1906 CTL_EOL); 1907 sysctl_createv(clog, 0, NULL, NULL, 1908 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1909 CTLTYPE_INT, "keepinit", 1910 SYSCTL_DESCR("Ticks before initial tcp connection times out"), 1911 sysctl_tcp_keep, 0, &tcp_keepinit, 0, 1912 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1913 1914 /* TCP socket buffers auto-sizing nodes */ 1915 sysctl_createv(clog, 0, NULL, NULL, 1916 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1917 CTLTYPE_INT, "recvbuf_auto", 1918 SYSCTL_DESCR("Enable automatic receive " 1919 "buffer sizing (experimental)"), 1920 NULL, 0, &tcp_do_autorcvbuf, 0, 1921 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1922 sysctl_createv(clog, 0, NULL, NULL, 1923 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1924 CTLTYPE_INT, "recvbuf_inc", 1925 SYSCTL_DESCR("Incrementor step size of " 1926 "automatic receive buffer"), 1927 NULL, 0, &tcp_autorcvbuf_inc, 0, 1928 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1929 sysctl_createv(clog, 0, NULL, NULL, 1930 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1931 CTLTYPE_INT, "recvbuf_max", 1932 SYSCTL_DESCR("Max size of automatic receive buffer"), 1933 NULL, 0, &tcp_autorcvbuf_max, 0, 1934 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1935 1936 sysctl_createv(clog, 0, NULL, NULL, 1937 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1938 CTLTYPE_INT, "sendbuf_auto", 1939 SYSCTL_DESCR("Enable automatic send " 1940 "buffer sizing (experimental)"), 1941 NULL, 0, &tcp_do_autosndbuf, 0, 1942 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1943 sysctl_createv(clog, 0, NULL, NULL, 1944 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1945 CTLTYPE_INT, "sendbuf_inc", 1946 SYSCTL_DESCR("Incrementor step size of " 1947 "automatic send buffer"), 1948 NULL, 0, &tcp_autosndbuf_inc, 0, 1949 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1950 sysctl_createv(clog, 0, NULL, NULL, 1951 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1952 CTLTYPE_INT, "sendbuf_max", 1953 SYSCTL_DESCR("Max size of automatic send buffer"), 1954 NULL, 0, &tcp_autosndbuf_max, 0, 1955 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1956 1957 /* ECN subtree */ 1958 sysctl_createv(clog, 0, NULL, &ecn_node, 1959 CTLFLAG_PERMANENT, 1960 CTLTYPE_NODE, "ecn", 1961 SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"), 1962 NULL, 0, NULL, 0, 1963 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1964 sysctl_createv(clog, 0, &ecn_node, NULL, 1965 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1966 CTLTYPE_INT, "enable", 1967 SYSCTL_DESCR("Enable TCP Explicit Congestion " 1968 "Notification"), 1969 NULL, 0, &tcp_do_ecn, 0, CTL_CREATE, CTL_EOL); 1970 sysctl_createv(clog, 0, &ecn_node, NULL, 1971 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1972 CTLTYPE_INT, "maxretries", 1973 SYSCTL_DESCR("Number of times to retry ECN setup " 1974 "before disabling ECN on the connection"), 1975 NULL, 0, &tcp_ecn_maxretries, 0, CTL_CREATE, CTL_EOL); 1976 1977 /* SACK gets it's own little subtree. */ 1978 sysctl_createv(clog, 0, NULL, &sack_node, 1979 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1980 CTLTYPE_INT, "enable", 1981 SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"), 1982 NULL, 0, &tcp_do_sack, 0, 1983 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 1984 sysctl_createv(clog, 0, NULL, &sack_node, 1985 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1986 CTLTYPE_INT, "maxholes", 1987 SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"), 1988 NULL, 0, &tcp_sack_tp_maxholes, 0, 1989 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 1990 sysctl_createv(clog, 0, NULL, &sack_node, 1991 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1992 CTLTYPE_INT, "globalmaxholes", 1993 SYSCTL_DESCR("Global maximum number of TCP SACK holes"), 1994 NULL, 0, &tcp_sack_globalmaxholes, 0, 1995 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 1996 sysctl_createv(clog, 0, NULL, &sack_node, 1997 CTLFLAG_PERMANENT, 1998 CTLTYPE_INT, "globalholes", 1999 SYSCTL_DESCR("Global number of TCP SACK holes"), 2000 NULL, 0, &tcp_sack_globalholes, 0, 2001 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 2002 2003 sysctl_createv(clog, 0, NULL, NULL, 2004 CTLFLAG_PERMANENT, 2005 CTLTYPE_STRUCT, "stats", 2006 SYSCTL_DESCR("TCP statistics"), 2007 sysctl_net_inet_tcp_stats, 0, NULL, 0, 2008 CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS, 2009 CTL_EOL); 2010 sysctl_createv(clog, 0, NULL, NULL, 2011 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2012 CTLTYPE_INT, "local_by_rtt", 2013 SYSCTL_DESCR("Use RTT estimator to decide which hosts " 2014 "are local"), 2015 NULL, 0, &tcp_rttlocal, 0, 2016 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2017 #ifdef TCP_DEBUG 2018 sysctl_createv(clog, 0, NULL, NULL, 2019 CTLFLAG_PERMANENT, 2020 CTLTYPE_STRUCT, "debug", 2021 SYSCTL_DESCR("TCP sockets debug information"), 2022 NULL, 0, &tcp_debug, sizeof(tcp_debug), 2023 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG, 2024 CTL_EOL); 2025 sysctl_createv(clog, 0, NULL, NULL, 2026 CTLFLAG_PERMANENT, 2027 CTLTYPE_INT, "debx", 2028 SYSCTL_DESCR("Number of TCP debug sockets messages"), 2029 NULL, 0, &tcp_debx, sizeof(tcp_debx), 2030 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX, 2031 CTL_EOL); 2032 #endif 2033 sysctl_createv(clog, 0, NULL, NULL, 2034 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2035 CTLTYPE_STRUCT, "drop", 2036 SYSCTL_DESCR("TCP drop connection"), 2037 sysctl_net_inet_tcp_drop, 0, NULL, 0, 2038 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DROP, CTL_EOL); 2039 sysctl_createv(clog, 0, NULL, NULL, 2040 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2041 CTLTYPE_INT, "iss_hash", 2042 SYSCTL_DESCR("Enable RFC 1948 ISS by cryptographic " 2043 "hash computation"), 2044 NULL, 0, &tcp_do_rfc1948, sizeof(tcp_do_rfc1948), 2045 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, 2046 CTL_EOL); 2047 2048 /* ABC subtree */ 2049 2050 sysctl_createv(clog, 0, NULL, &abc_node, 2051 CTLFLAG_PERMANENT, CTLTYPE_NODE, "abc", 2052 SYSCTL_DESCR("RFC3465 Appropriate Byte Counting (ABC)"), 2053 NULL, 0, NULL, 0, 2054 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2055 sysctl_createv(clog, 0, &abc_node, NULL, 2056 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2057 CTLTYPE_INT, "enable", 2058 SYSCTL_DESCR("Enable RFC3465 Appropriate Byte Counting"), 2059 NULL, 0, &tcp_do_abc, 0, CTL_CREATE, CTL_EOL); 2060 sysctl_createv(clog, 0, &abc_node, NULL, 2061 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2062 CTLTYPE_INT, "aggressive", 2063 SYSCTL_DESCR("1: L=2*SMSS 0: L=1*SMSS"), 2064 NULL, 0, &tcp_abc_aggressive, 0, CTL_CREATE, CTL_EOL); 2065 2066 /* MSL tuning subtree */ 2067 2068 sysctl_createv(clog, 0, NULL, &mslt_node, 2069 CTLFLAG_PERMANENT, CTLTYPE_NODE, "mslt", 2070 SYSCTL_DESCR("MSL Tuning for TIME_WAIT truncation"), 2071 NULL, 0, NULL, 0, 2072 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2073 sysctl_createv(clog, 0, &mslt_node, NULL, 2074 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2075 CTLTYPE_INT, "enable", 2076 SYSCTL_DESCR("Enable TIME_WAIT truncation"), 2077 NULL, 0, &tcp_msl_enable, 0, CTL_CREATE, CTL_EOL); 2078 sysctl_createv(clog, 0, &mslt_node, NULL, 2079 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2080 CTLTYPE_INT, "loopback", 2081 SYSCTL_DESCR("MSL value to use for loopback connections"), 2082 NULL, 0, &tcp_msl_loop, 0, CTL_CREATE, CTL_EOL); 2083 sysctl_createv(clog, 0, &mslt_node, NULL, 2084 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2085 CTLTYPE_INT, "local", 2086 SYSCTL_DESCR("MSL value to use for local connections"), 2087 NULL, 0, &tcp_msl_local, 0, CTL_CREATE, CTL_EOL); 2088 sysctl_createv(clog, 0, &mslt_node, NULL, 2089 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2090 CTLTYPE_INT, "remote", 2091 SYSCTL_DESCR("MSL value to use for remote connections"), 2092 NULL, 0, &tcp_msl_remote, 0, CTL_CREATE, CTL_EOL); 2093 sysctl_createv(clog, 0, &mslt_node, NULL, 2094 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2095 CTLTYPE_INT, "remote_threshold", 2096 SYSCTL_DESCR("RTT estimate value to promote local to remote"), 2097 NULL, 0, &tcp_msl_remote_threshold, 0, CTL_CREATE, CTL_EOL); 2098 2099 /* vestigial TIME_WAIT tuning subtree */ 2100 2101 sysctl_createv(clog, 0, NULL, &vtw_node, 2102 CTLFLAG_PERMANENT, CTLTYPE_NODE, "vtw", 2103 SYSCTL_DESCR("Tuning for Vestigial TIME_WAIT"), 2104 NULL, 0, NULL, 0, 2105 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 2106 sysctl_createv(clog, 0, &vtw_node, NULL, 2107 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2108 CTLTYPE_INT, "enable", 2109 SYSCTL_DESCR("Enable Vestigial TIME_WAIT"), 2110 sysctl_tcp_vtw_enable, 0, 2111 (pf == AF_INET) ? &tcp4_vtw_enable : &tcp6_vtw_enable, 2112 0, CTL_CREATE, CTL_EOL); 2113 sysctl_createv(clog, 0, &vtw_node, NULL, 2114 CTLFLAG_PERMANENT|CTLFLAG_READONLY, 2115 CTLTYPE_INT, "entries", 2116 SYSCTL_DESCR("Maximum number of vestigial TIME_WAIT entries"), 2117 NULL, 0, &tcp_vtw_entries, 0, CTL_CREATE, CTL_EOL); 2118 } 2119 2120 void 2121 tcp_usrreq_init(void) 2122 { 2123 2124 #ifdef INET 2125 sysctl_net_inet_tcp_setup2(NULL, PF_INET, "inet", "tcp"); 2126 #endif 2127 #ifdef INET6 2128 sysctl_net_inet_tcp_setup2(NULL, PF_INET6, "inet6", "tcp6"); 2129 #endif 2130 } 2131