1 /* $NetBSD: tcp_usrreq.c,v 1.125 2006/10/13 15:39:19 elad 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 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the NetBSD 55 * Foundation, Inc. and its contributors. 56 * 4. Neither the name of The NetBSD Foundation nor the names of its 57 * contributors may be used to endorse or promote products derived 58 * from this software without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 61 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 62 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 64 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 65 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 66 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 67 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 68 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 70 * POSSIBILITY OF SUCH DAMAGE. 71 */ 72 73 /* 74 * Copyright (c) 1982, 1986, 1988, 1993, 1995 75 * The Regents of the University of California. All rights reserved. 76 * 77 * Redistribution and use in source and binary forms, with or without 78 * modification, are permitted provided that the following conditions 79 * are met: 80 * 1. Redistributions of source code must retain the above copyright 81 * notice, this list of conditions and the following disclaimer. 82 * 2. Redistributions in binary form must reproduce the above copyright 83 * notice, this list of conditions and the following disclaimer in the 84 * documentation and/or other materials provided with the distribution. 85 * 3. Neither the name of the University nor the names of its contributors 86 * may be used to endorse or promote products derived from this software 87 * without specific prior written permission. 88 * 89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 99 * SUCH DAMAGE. 100 * 101 * @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95 102 */ 103 104 #include <sys/cdefs.h> 105 __KERNEL_RCSID(0, "$NetBSD: tcp_usrreq.c,v 1.125 2006/10/13 15:39:19 elad Exp $"); 106 107 #include "opt_inet.h" 108 #include "opt_ipsec.h" 109 #include "opt_tcp_debug.h" 110 #include "opt_mbuftrace.h" 111 112 #include <sys/param.h> 113 #include <sys/systm.h> 114 #include <sys/kernel.h> 115 #include <sys/malloc.h> 116 #include <sys/mbuf.h> 117 #include <sys/socket.h> 118 #include <sys/socketvar.h> 119 #include <sys/protosw.h> 120 #include <sys/errno.h> 121 #include <sys/stat.h> 122 #include <sys/proc.h> 123 #include <sys/domain.h> 124 #include <sys/sysctl.h> 125 #include <sys/kauth.h> 126 127 #include <net/if.h> 128 #include <net/route.h> 129 130 #include <netinet/in.h> 131 #include <netinet/in_systm.h> 132 #include <netinet/in_var.h> 133 #include <netinet/ip.h> 134 #include <netinet/in_pcb.h> 135 #include <netinet/ip_var.h> 136 #include <netinet/in_offload.h> 137 138 #ifdef INET6 139 #ifndef INET 140 #include <netinet/in.h> 141 #endif 142 #include <netinet/ip6.h> 143 #include <netinet6/in6_pcb.h> 144 #include <netinet6/ip6_var.h> 145 #endif 146 147 #include <netinet/tcp.h> 148 #include <netinet/tcp_fsm.h> 149 #include <netinet/tcp_seq.h> 150 #include <netinet/tcp_timer.h> 151 #include <netinet/tcp_var.h> 152 #include <netinet/tcp_congctl.h> 153 #include <netinet/tcpip.h> 154 #include <netinet/tcp_debug.h> 155 156 #include "opt_tcp_space.h" 157 158 #ifdef IPSEC 159 #include <netinet6/ipsec.h> 160 #endif /*IPSEC*/ 161 162 /* 163 * TCP protocol interface to socket abstraction. 164 */ 165 166 /* 167 * Process a TCP user request for TCP tb. If this is a send request 168 * then m is the mbuf chain of send data. If this is a timer expiration 169 * (called from the software clock routine), then timertype tells which timer. 170 */ 171 /*ARGSUSED*/ 172 int 173 tcp_usrreq(struct socket *so, int req, 174 struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l) 175 { 176 struct inpcb *inp; 177 #ifdef INET6 178 struct in6pcb *in6p; 179 #endif 180 struct tcpcb *tp = NULL; 181 int s; 182 int error = 0; 183 #ifdef TCP_DEBUG 184 int ostate = 0; 185 #endif 186 int family; /* family of the socket */ 187 188 family = so->so_proto->pr_domain->dom_family; 189 190 if (req == PRU_CONTROL) { 191 switch (family) { 192 #ifdef INET 193 case PF_INET: 194 return (in_control(so, (long)m, (caddr_t)nam, 195 (struct ifnet *)control, l)); 196 #endif 197 #ifdef INET6 198 case PF_INET6: 199 return (in6_control(so, (long)m, (caddr_t)nam, 200 (struct ifnet *)control, l)); 201 #endif 202 default: 203 return EAFNOSUPPORT; 204 } 205 } 206 207 s = splsoftnet(); 208 209 if (req == PRU_PURGEIF) { 210 switch (family) { 211 #ifdef INET 212 case PF_INET: 213 in_pcbpurgeif0(&tcbtable, (struct ifnet *)control); 214 in_purgeif((struct ifnet *)control); 215 in_pcbpurgeif(&tcbtable, (struct ifnet *)control); 216 break; 217 #endif 218 #ifdef INET6 219 case PF_INET6: 220 in6_pcbpurgeif0(&tcbtable, (struct ifnet *)control); 221 in6_purgeif((struct ifnet *)control); 222 in6_pcbpurgeif(&tcbtable, (struct ifnet *)control); 223 break; 224 #endif 225 default: 226 splx(s); 227 return (EAFNOSUPPORT); 228 } 229 splx(s); 230 return (0); 231 } 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 #ifndef INET6 267 if (inp == 0 && req != PRU_ATTACH) 268 #else 269 if ((inp == 0 && in6p == 0) && req != PRU_ATTACH) 270 #endif 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, (struct mbuf *)0, 363 (struct lwp *)0); 364 if (error) 365 break; 366 } 367 #endif 368 #ifdef INET6 369 if (in6p && in6p->in6p_lport == 0) { 370 error = in6_pcbbind(in6p, (struct mbuf *)0, 371 (struct lwp *)0); 372 if (error) 373 break; 374 } 375 #endif 376 tp->t_state = TCPS_LISTEN; 377 break; 378 379 /* 380 * Initiate connection to peer. 381 * Create a template for use in transmissions on this connection. 382 * Enter SYN_SENT state, and mark socket as connecting. 383 * Start keep-alive timer, and seed output sequence space. 384 * Send initial segment on connection. 385 */ 386 case PRU_CONNECT: 387 #ifdef INET 388 if (inp) { 389 if (inp->inp_lport == 0) { 390 error = in_pcbbind(inp, (struct mbuf *)0, 391 (struct lwp *)0); 392 if (error) 393 break; 394 } 395 error = in_pcbconnect(inp, nam, l); 396 } 397 #endif 398 #ifdef INET6 399 if (in6p) { 400 if (in6p->in6p_lport == 0) { 401 error = in6_pcbbind(in6p, (struct mbuf *)0, 402 (struct lwp *)0); 403 if (error) 404 break; 405 } 406 error = in6_pcbconnect(in6p, nam, l); 407 if (!error) { 408 /* mapped addr case */ 409 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) 410 tp->t_family = AF_INET; 411 else 412 tp->t_family = AF_INET6; 413 } 414 } 415 #endif 416 if (error) 417 break; 418 tp->t_template = tcp_template(tp); 419 if (tp->t_template == 0) { 420 #ifdef INET 421 if (inp) 422 in_pcbdisconnect(inp); 423 #endif 424 #ifdef INET6 425 if (in6p) 426 in6_pcbdisconnect(in6p); 427 #endif 428 error = ENOBUFS; 429 break; 430 } 431 /* Compute window scaling to request. */ 432 while (tp->request_r_scale < TCP_MAX_WINSHIFT && 433 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat) 434 tp->request_r_scale++; 435 soisconnecting(so); 436 tcpstat.tcps_connattempt++; 437 tp->t_state = TCPS_SYN_SENT; 438 TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT); 439 tp->iss = tcp_new_iss(tp, 0); 440 tcp_sendseqinit(tp); 441 error = tcp_output(tp); 442 break; 443 444 /* 445 * Create a TCP connection between two sockets. 446 */ 447 case PRU_CONNECT2: 448 error = EOPNOTSUPP; 449 break; 450 451 /* 452 * Initiate disconnect from peer. 453 * If connection never passed embryonic stage, just drop; 454 * else if don't need to let data drain, then can just drop anyways, 455 * else have to begin TCP shutdown process: mark socket disconnecting, 456 * drain unread data, state switch to reflect user close, and 457 * send segment (e.g. FIN) to peer. Socket will be really disconnected 458 * when peer sends FIN and acks ours. 459 * 460 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB. 461 */ 462 case PRU_DISCONNECT: 463 tp = tcp_disconnect(tp); 464 break; 465 466 /* 467 * Accept a connection. Essentially all the work is 468 * done at higher levels; just return the address 469 * of the peer, storing through addr. 470 */ 471 case PRU_ACCEPT: 472 #ifdef INET 473 if (inp) 474 in_setpeeraddr(inp, nam); 475 #endif 476 #ifdef INET6 477 if (in6p) 478 in6_setpeeraddr(in6p, nam); 479 #endif 480 break; 481 482 /* 483 * Mark the connection as being incapable of further output. 484 */ 485 case PRU_SHUTDOWN: 486 socantsendmore(so); 487 tp = tcp_usrclosed(tp); 488 if (tp) 489 error = tcp_output(tp); 490 break; 491 492 /* 493 * After a receive, possibly send window update to peer. 494 */ 495 case PRU_RCVD: 496 /* 497 * soreceive() calls this function when a user receives 498 * ancillary data on a listening socket. We don't call 499 * tcp_output in such a case, since there is no header 500 * template for a listening socket and hence the kernel 501 * will panic. 502 */ 503 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) != 0) 504 (void) tcp_output(tp); 505 break; 506 507 /* 508 * Do a send by putting data in output queue and updating urgent 509 * marker if URG set. Possibly send more data. 510 */ 511 case PRU_SEND: 512 if (control && control->m_len) { 513 m_freem(control); 514 m_freem(m); 515 error = EINVAL; 516 break; 517 } 518 sbappendstream(&so->so_snd, m); 519 error = tcp_output(tp); 520 break; 521 522 /* 523 * Abort the TCP. 524 */ 525 case PRU_ABORT: 526 tp = tcp_drop(tp, ECONNABORTED); 527 break; 528 529 case PRU_SENSE: 530 /* 531 * stat: don't bother with a blocksize. 532 */ 533 splx(s); 534 return (0); 535 536 case PRU_RCVOOB: 537 if (control && control->m_len) { 538 m_freem(control); 539 m_freem(m); 540 error = EINVAL; 541 break; 542 } 543 if ((so->so_oobmark == 0 && 544 (so->so_state & SS_RCVATMARK) == 0) || 545 so->so_options & SO_OOBINLINE || 546 tp->t_oobflags & TCPOOB_HADDATA) { 547 error = EINVAL; 548 break; 549 } 550 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) { 551 error = EWOULDBLOCK; 552 break; 553 } 554 m->m_len = 1; 555 *mtod(m, caddr_t) = tp->t_iobc; 556 if (((long)nam & MSG_PEEK) == 0) 557 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA); 558 break; 559 560 case PRU_SENDOOB: 561 if (sbspace(&so->so_snd) < -512) { 562 m_freem(m); 563 error = ENOBUFS; 564 break; 565 } 566 /* 567 * According to RFC961 (Assigned Protocols), 568 * the urgent pointer points to the last octet 569 * of urgent data. We continue, however, 570 * to consider it to indicate the first octet 571 * of data past the urgent section. 572 * Otherwise, snd_up should be one lower. 573 */ 574 sbappendstream(&so->so_snd, m); 575 tp->snd_up = tp->snd_una + so->so_snd.sb_cc; 576 tp->t_force = 1; 577 error = tcp_output(tp); 578 tp->t_force = 0; 579 break; 580 581 case PRU_SOCKADDR: 582 #ifdef INET 583 if (inp) 584 in_setsockaddr(inp, nam); 585 #endif 586 #ifdef INET6 587 if (in6p) 588 in6_setsockaddr(in6p, nam); 589 #endif 590 break; 591 592 case PRU_PEERADDR: 593 #ifdef INET 594 if (inp) 595 in_setpeeraddr(inp, nam); 596 #endif 597 #ifdef INET6 598 if (in6p) 599 in6_setpeeraddr(in6p, nam); 600 #endif 601 break; 602 603 default: 604 panic("tcp_usrreq"); 605 } 606 #ifdef TCP_DEBUG 607 if (tp && (so->so_options & SO_DEBUG)) 608 tcp_trace(TA_USER, ostate, tp, NULL, req); 609 #endif 610 611 release: 612 splx(s); 613 return (error); 614 } 615 616 int 617 tcp_ctloutput(int op, struct socket *so, int level, int optname, 618 struct mbuf **mp) 619 { 620 int error = 0, s; 621 struct inpcb *inp; 622 #ifdef INET6 623 struct in6pcb *in6p; 624 #endif 625 struct tcpcb *tp; 626 struct mbuf *m; 627 int i; 628 int family; /* family of the socket */ 629 630 family = so->so_proto->pr_domain->dom_family; 631 632 s = splsoftnet(); 633 switch (family) { 634 #ifdef INET 635 case PF_INET: 636 inp = sotoinpcb(so); 637 #ifdef INET6 638 in6p = NULL; 639 #endif 640 break; 641 #endif 642 #ifdef INET6 643 case PF_INET6: 644 inp = NULL; 645 in6p = sotoin6pcb(so); 646 break; 647 #endif 648 default: 649 splx(s); 650 return EAFNOSUPPORT; 651 } 652 #ifndef INET6 653 if (inp == NULL) 654 #else 655 if (inp == NULL && in6p == NULL) 656 #endif 657 { 658 splx(s); 659 if (op == PRCO_SETOPT && *mp) 660 (void) m_free(*mp); 661 return (ECONNRESET); 662 } 663 if (level != IPPROTO_TCP) { 664 switch (family) { 665 #ifdef INET 666 case PF_INET: 667 error = ip_ctloutput(op, so, level, optname, mp); 668 break; 669 #endif 670 #ifdef INET6 671 case PF_INET6: 672 error = ip6_ctloutput(op, so, level, optname, mp); 673 break; 674 #endif 675 } 676 splx(s); 677 return (error); 678 } 679 if (inp) 680 tp = intotcpcb(inp); 681 #ifdef INET6 682 else if (in6p) 683 tp = in6totcpcb(in6p); 684 #endif 685 else 686 tp = NULL; 687 688 switch (op) { 689 690 case PRCO_SETOPT: 691 m = *mp; 692 switch (optname) { 693 694 #ifdef TCP_SIGNATURE 695 case TCP_MD5SIG: 696 if (m == NULL || m->m_len < sizeof (int)) 697 error = EINVAL; 698 if (error) 699 break; 700 if (*mtod(m, int *) > 0) 701 tp->t_flags |= TF_SIGNATURE; 702 else 703 tp->t_flags &= ~TF_SIGNATURE; 704 break; 705 #endif /* TCP_SIGNATURE */ 706 707 case TCP_NODELAY: 708 if (m == NULL || m->m_len < sizeof (int)) 709 error = EINVAL; 710 else if (*mtod(m, int *)) 711 tp->t_flags |= TF_NODELAY; 712 else 713 tp->t_flags &= ~TF_NODELAY; 714 break; 715 716 case TCP_MAXSEG: 717 if (m && (i = *mtod(m, int *)) > 0 && 718 i <= tp->t_peermss) 719 tp->t_peermss = i; /* limit on send size */ 720 else 721 error = EINVAL; 722 break; 723 #ifdef notyet 724 case TCP_CONGCTL: 725 if (m == NULL) 726 error = EINVAL; 727 error = tcp_congctl_select(tp, mtod(m, char *)); 728 #endif 729 break; 730 731 default: 732 error = ENOPROTOOPT; 733 break; 734 } 735 if (m) 736 (void) m_free(m); 737 break; 738 739 case PRCO_GETOPT: 740 *mp = m = m_get(M_WAIT, MT_SOOPTS); 741 m->m_len = sizeof(int); 742 MCLAIM(m, so->so_mowner); 743 744 switch (optname) { 745 #ifdef TCP_SIGNATURE 746 case TCP_MD5SIG: 747 *mtod(m, int *) = (tp->t_flags & TF_SIGNATURE) ? 1 : 0; 748 break; 749 #endif 750 case TCP_NODELAY: 751 *mtod(m, int *) = tp->t_flags & TF_NODELAY; 752 break; 753 case TCP_MAXSEG: 754 *mtod(m, int *) = tp->t_peermss; 755 break; 756 #ifdef notyet 757 case TCP_CONGCTL: 758 break; 759 #endif 760 default: 761 error = ENOPROTOOPT; 762 break; 763 } 764 break; 765 } 766 splx(s); 767 return (error); 768 } 769 770 #ifndef TCP_SENDSPACE 771 #define TCP_SENDSPACE 1024*32 772 #endif 773 int tcp_sendspace = TCP_SENDSPACE; 774 #ifndef TCP_RECVSPACE 775 #define TCP_RECVSPACE 1024*32 776 #endif 777 int tcp_recvspace = TCP_RECVSPACE; 778 779 /* 780 * Attach TCP protocol to socket, allocating 781 * internet protocol control block, tcp control block, 782 * bufer space, and entering LISTEN state if to accept connections. 783 */ 784 int 785 tcp_attach(struct socket *so) 786 { 787 struct tcpcb *tp; 788 struct inpcb *inp; 789 #ifdef INET6 790 struct in6pcb *in6p; 791 #endif 792 int error; 793 int family; /* family of the socket */ 794 795 family = so->so_proto->pr_domain->dom_family; 796 797 #ifdef MBUFTRACE 798 so->so_mowner = &tcp_mowner; 799 so->so_rcv.sb_mowner = &tcp_rx_mowner; 800 so->so_snd.sb_mowner = &tcp_tx_mowner; 801 #endif 802 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 803 error = soreserve(so, tcp_sendspace, tcp_recvspace); 804 if (error) 805 return (error); 806 } 807 switch (family) { 808 #ifdef INET 809 case PF_INET: 810 error = in_pcballoc(so, &tcbtable); 811 if (error) 812 return (error); 813 inp = sotoinpcb(so); 814 #ifdef INET6 815 in6p = NULL; 816 #endif 817 break; 818 #endif 819 #ifdef INET6 820 case PF_INET6: 821 error = in6_pcballoc(so, &tcbtable); 822 if (error) 823 return (error); 824 inp = NULL; 825 in6p = sotoin6pcb(so); 826 break; 827 #endif 828 default: 829 return EAFNOSUPPORT; 830 } 831 if (inp) 832 tp = tcp_newtcpcb(family, (void *)inp); 833 #ifdef INET6 834 else if (in6p) 835 tp = tcp_newtcpcb(family, (void *)in6p); 836 #endif 837 else 838 tp = NULL; 839 840 if (tp == 0) { 841 int nofd = so->so_state & SS_NOFDREF; /* XXX */ 842 843 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */ 844 #ifdef INET 845 if (inp) 846 in_pcbdetach(inp); 847 #endif 848 #ifdef INET6 849 if (in6p) 850 in6_pcbdetach(in6p); 851 #endif 852 so->so_state |= nofd; 853 return (ENOBUFS); 854 } 855 tp->t_state = TCPS_CLOSED; 856 return (0); 857 } 858 859 /* 860 * Initiate (or continue) disconnect. 861 * If embryonic state, just send reset (once). 862 * If in ``let data drain'' option and linger null, just drop. 863 * Otherwise (hard), mark socket disconnecting and drop 864 * current input data; switch states based on user close, and 865 * send segment to peer (with FIN). 866 */ 867 struct tcpcb * 868 tcp_disconnect(struct tcpcb *tp) 869 { 870 struct socket *so; 871 872 if (tp->t_inpcb) 873 so = tp->t_inpcb->inp_socket; 874 #ifdef INET6 875 else if (tp->t_in6pcb) 876 so = tp->t_in6pcb->in6p_socket; 877 #endif 878 else 879 so = NULL; 880 881 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0) 882 tp = tcp_close(tp); 883 else if ((so->so_options & SO_LINGER) && so->so_linger == 0) 884 tp = tcp_drop(tp, 0); 885 else { 886 soisdisconnecting(so); 887 sbflush(&so->so_rcv); 888 tp = tcp_usrclosed(tp); 889 if (tp) 890 (void) tcp_output(tp); 891 } 892 return (tp); 893 } 894 895 /* 896 * User issued close, and wish to trail through shutdown states: 897 * if never received SYN, just forget it. If got a SYN from peer, 898 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN. 899 * If already got a FIN from peer, then almost done; go to LAST_ACK 900 * state. In all other cases, have already sent FIN to peer (e.g. 901 * after PRU_SHUTDOWN), and just have to play tedious game waiting 902 * for peer to send FIN or not respond to keep-alives, etc. 903 * We can let the user exit from the close as soon as the FIN is acked. 904 */ 905 struct tcpcb * 906 tcp_usrclosed(struct tcpcb *tp) 907 { 908 909 switch (tp->t_state) { 910 911 case TCPS_CLOSED: 912 case TCPS_LISTEN: 913 case TCPS_SYN_SENT: 914 tp->t_state = TCPS_CLOSED; 915 tp = tcp_close(tp); 916 break; 917 918 case TCPS_SYN_RECEIVED: 919 case TCPS_ESTABLISHED: 920 tp->t_state = TCPS_FIN_WAIT_1; 921 break; 922 923 case TCPS_CLOSE_WAIT: 924 tp->t_state = TCPS_LAST_ACK; 925 break; 926 } 927 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) { 928 struct socket *so; 929 if (tp->t_inpcb) 930 so = tp->t_inpcb->inp_socket; 931 #ifdef INET6 932 else if (tp->t_in6pcb) 933 so = tp->t_in6pcb->in6p_socket; 934 #endif 935 else 936 so = NULL; 937 if (so) 938 soisdisconnected(so); 939 /* 940 * If we are in FIN_WAIT_2, we arrived here because the 941 * application did a shutdown of the send side. Like the 942 * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after 943 * a full close, we start a timer to make sure sockets are 944 * not left in FIN_WAIT_2 forever. 945 */ 946 if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0)) 947 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle); 948 } 949 return (tp); 950 } 951 952 /* 953 * sysctl helper routine for net.inet.ip.mssdflt. it can't be less 954 * than 32. 955 */ 956 static int 957 sysctl_net_inet_tcp_mssdflt(SYSCTLFN_ARGS) 958 { 959 int error, mssdflt; 960 struct sysctlnode node; 961 962 mssdflt = tcp_mssdflt; 963 node = *rnode; 964 node.sysctl_data = &mssdflt; 965 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 966 if (error || newp == NULL) 967 return (error); 968 969 if (mssdflt < 32) 970 return (EINVAL); 971 tcp_mssdflt = mssdflt; 972 973 return (0); 974 } 975 976 /* 977 * sysctl helper routine for setting port related values under 978 * net.inet.ip and net.inet6.ip6. does basic range checking and does 979 * additional checks for each type. this code has placed in 980 * tcp_input.c since INET and INET6 both use the same tcp code. 981 * 982 * this helper is not static so that both inet and inet6 can use it. 983 */ 984 int 985 sysctl_net_inet_ip_ports(SYSCTLFN_ARGS) 986 { 987 int error, tmp; 988 int apmin, apmax; 989 #ifndef IPNOPRIVPORTS 990 int lpmin, lpmax; 991 #endif /* IPNOPRIVPORTS */ 992 struct sysctlnode node; 993 994 if (namelen != 0) 995 return (EINVAL); 996 997 switch (name[-3]) { 998 #ifdef INET 999 case PF_INET: 1000 apmin = anonportmin; 1001 apmax = anonportmax; 1002 #ifndef IPNOPRIVPORTS 1003 lpmin = lowportmin; 1004 lpmax = lowportmax; 1005 #endif /* IPNOPRIVPORTS */ 1006 break; 1007 #endif /* INET */ 1008 #ifdef INET6 1009 case PF_INET6: 1010 apmin = ip6_anonportmin; 1011 apmax = ip6_anonportmax; 1012 #ifndef IPNOPRIVPORTS 1013 lpmin = ip6_lowportmin; 1014 lpmax = ip6_lowportmax; 1015 #endif /* IPNOPRIVPORTS */ 1016 break; 1017 #endif /* INET6 */ 1018 default: 1019 return (EINVAL); 1020 } 1021 1022 /* 1023 * insert temporary copy into node, perform lookup on 1024 * temporary, then restore pointer 1025 */ 1026 node = *rnode; 1027 tmp = *(int*)rnode->sysctl_data; 1028 node.sysctl_data = &tmp; 1029 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1030 if (error || newp == NULL) 1031 return (error); 1032 1033 /* 1034 * simple port range check 1035 */ 1036 if (tmp < 0 || tmp > 65535) 1037 return (EINVAL); 1038 1039 /* 1040 * per-node range checks 1041 */ 1042 switch (rnode->sysctl_num) { 1043 case IPCTL_ANONPORTMIN: 1044 if (tmp >= apmax) 1045 return (EINVAL); 1046 #ifndef IPNOPRIVPORTS 1047 if (tmp < IPPORT_RESERVED) 1048 return (EINVAL); 1049 #endif /* IPNOPRIVPORTS */ 1050 break; 1051 1052 case IPCTL_ANONPORTMAX: 1053 if (apmin >= tmp) 1054 return (EINVAL); 1055 #ifndef IPNOPRIVPORTS 1056 if (tmp < IPPORT_RESERVED) 1057 return (EINVAL); 1058 #endif /* IPNOPRIVPORTS */ 1059 break; 1060 1061 #ifndef IPNOPRIVPORTS 1062 case IPCTL_LOWPORTMIN: 1063 if (tmp >= lpmax || 1064 tmp > IPPORT_RESERVEDMAX || 1065 tmp < IPPORT_RESERVEDMIN) 1066 return (EINVAL); 1067 break; 1068 1069 case IPCTL_LOWPORTMAX: 1070 if (lpmin >= tmp || 1071 tmp > IPPORT_RESERVEDMAX || 1072 tmp < IPPORT_RESERVEDMIN) 1073 return (EINVAL); 1074 break; 1075 #endif /* IPNOPRIVPORTS */ 1076 1077 default: 1078 return (EINVAL); 1079 } 1080 1081 *(int*)rnode->sysctl_data = tmp; 1082 1083 return (0); 1084 } 1085 1086 /* 1087 * sysctl helper routine for the net.inet.tcp.ident and 1088 * net.inet6.tcp6.ident nodes. contains backwards compat code for the 1089 * old way of looking up the ident information for ipv4 which involves 1090 * stuffing the port/addr pairs into the mib lookup. 1091 */ 1092 static int 1093 sysctl_net_inet_tcp_ident(SYSCTLFN_ARGS) 1094 { 1095 #ifdef INET 1096 struct inpcb *inb; 1097 struct sockaddr_in *si4[2]; 1098 #endif /* INET */ 1099 #ifdef INET6 1100 struct in6pcb *in6b; 1101 struct sockaddr_in6 *si6[2]; 1102 #endif /* INET6 */ 1103 struct sockaddr_storage sa[2]; 1104 struct socket *sockp; 1105 size_t sz; 1106 uid_t uid; 1107 int error, pf; 1108 1109 if (namelen != 4 && namelen != 0) 1110 return (EINVAL); 1111 if (name[-2] != IPPROTO_TCP) 1112 return (EINVAL); 1113 pf = name[-3]; 1114 1115 /* old style lookup, ipv4 only */ 1116 if (namelen == 4) { 1117 #ifdef INET 1118 struct in_addr laddr, raddr; 1119 u_int lport, rport; 1120 1121 if (pf != PF_INET) 1122 return (EPROTONOSUPPORT); 1123 raddr.s_addr = (uint32_t)name[0]; 1124 rport = (u_int)name[1]; 1125 laddr.s_addr = (uint32_t)name[2]; 1126 lport = (u_int)name[3]; 1127 inb = in_pcblookup_connect(&tcbtable, raddr, rport, 1128 laddr, lport); 1129 if (inb == NULL || (sockp = inb->inp_socket) == NULL) 1130 return (ESRCH); 1131 uid = sockp->so_uidinfo->ui_uid; 1132 if (oldp) { 1133 sz = MIN(sizeof(uid), *oldlenp); 1134 error = copyout(&uid, oldp, sz); 1135 if (error) 1136 return (error); 1137 } 1138 *oldlenp = sizeof(uid); 1139 return (0); 1140 #else /* INET */ 1141 return (EINVAL); 1142 #endif /* INET */ 1143 } 1144 1145 if (newp == NULL || newlen != sizeof(sa)) 1146 return (EINVAL); 1147 error = copyin(newp, &sa, newlen); 1148 if (error) 1149 return (error); 1150 1151 /* 1152 * requested families must match 1153 */ 1154 if (pf != sa[0].ss_family || sa[0].ss_family != sa[1].ss_family) 1155 return (EINVAL); 1156 1157 switch (pf) { 1158 #ifdef INET 1159 case PF_INET: 1160 si4[0] = (struct sockaddr_in*)&sa[0]; 1161 si4[1] = (struct sockaddr_in*)&sa[1]; 1162 if (si4[0]->sin_len != sizeof(*si4[0]) || 1163 si4[0]->sin_len != si4[1]->sin_len) 1164 return (EINVAL); 1165 inb = in_pcblookup_connect(&tcbtable, 1166 si4[0]->sin_addr, si4[0]->sin_port, 1167 si4[1]->sin_addr, si4[1]->sin_port); 1168 if (inb == NULL || (sockp = inb->inp_socket) == NULL) 1169 return (ESRCH); 1170 break; 1171 #endif /* INET */ 1172 #ifdef INET6 1173 case PF_INET6: 1174 si6[0] = (struct sockaddr_in6*)&sa[0]; 1175 si6[1] = (struct sockaddr_in6*)&sa[1]; 1176 if (si6[0]->sin6_len != sizeof(*si6[0]) || 1177 si6[0]->sin6_len != si6[1]->sin6_len) 1178 return (EINVAL); 1179 in6b = in6_pcblookup_connect(&tcbtable, 1180 &si6[0]->sin6_addr, si6[0]->sin6_port, 1181 &si6[1]->sin6_addr, si6[1]->sin6_port, 0); 1182 if (in6b == NULL || (sockp = in6b->in6p_socket) == NULL) 1183 return (ESRCH); 1184 break; 1185 #endif /* INET6 */ 1186 default: 1187 return (EPROTONOSUPPORT); 1188 } 1189 *oldlenp = sizeof(uid); 1190 1191 uid = sockp->so_uidinfo->ui_uid; 1192 if (oldp) { 1193 sz = MIN(sizeof(uid), *oldlenp); 1194 error = copyout(&uid, oldp, sz); 1195 if (error) 1196 return (error); 1197 } 1198 *oldlenp = sizeof(uid); 1199 1200 return (0); 1201 } 1202 1203 /* 1204 * sysctl helper for the inet and inet6 pcblists. handles tcp/udp and 1205 * inet/inet6, as well as raw pcbs for each. specifically not 1206 * declared static so that raw sockets and udp/udp6 can use it as 1207 * well. 1208 */ 1209 int 1210 sysctl_inpcblist(SYSCTLFN_ARGS) 1211 { 1212 #ifdef INET 1213 struct sockaddr_in *in; 1214 const struct inpcb *inp; 1215 #endif 1216 #ifdef INET6 1217 struct sockaddr_in6 *in6; 1218 const struct in6pcb *in6p; 1219 #endif 1220 /* 1221 * sysctl_data is const, but CIRCLEQ_FOREACH can't use a const 1222 * struct inpcbtable pointer, so we have to discard const. :-/ 1223 */ 1224 struct inpcbtable *pcbtbl = __UNCONST(rnode->sysctl_data); 1225 const struct inpcb_hdr *inph; 1226 struct tcpcb *tp; 1227 struct kinfo_pcb pcb; 1228 char *dp; 1229 u_int op, arg; 1230 size_t len, needed, elem_size, out_size; 1231 int error, elem_count, pf, proto, pf2; 1232 1233 if (namelen != 4) 1234 return (EINVAL); 1235 1236 if (oldp != NULL) { 1237 len = *oldlenp; 1238 elem_size = name[2]; 1239 elem_count = name[3]; 1240 if (elem_size != sizeof(pcb)) 1241 return EINVAL; 1242 } else { 1243 len = 0; 1244 elem_count = INT_MAX; 1245 elem_size = sizeof(pcb); 1246 } 1247 error = 0; 1248 dp = oldp; 1249 op = name[0]; 1250 arg = name[1]; 1251 out_size = elem_size; 1252 needed = 0; 1253 1254 if (namelen == 1 && name[0] == CTL_QUERY) 1255 return (sysctl_query(SYSCTLFN_CALL(rnode))); 1256 1257 if (name - oname != 4) 1258 return (EINVAL); 1259 1260 pf = oname[1]; 1261 proto = oname[2]; 1262 pf2 = (oldp != NULL) ? pf : 0; 1263 1264 CIRCLEQ_FOREACH(inph, &pcbtbl->inpt_queue, inph_queue) { 1265 #ifdef INET 1266 inp = (const struct inpcb *)inph; 1267 #endif 1268 #ifdef INET6 1269 in6p = (const struct in6pcb *)inph; 1270 #endif 1271 1272 if (inph->inph_af != pf) 1273 continue; 1274 1275 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 1276 KAUTH_REQ_NETWORK_SOCKET_CANSEE, inph->inph_socket, NULL, 1277 NULL) != 0) 1278 continue; 1279 1280 memset(&pcb, 0, sizeof(pcb)); 1281 1282 pcb.ki_family = pf; 1283 pcb.ki_type = proto; 1284 1285 switch (pf2) { 1286 case 0: 1287 /* just probing for size */ 1288 break; 1289 #ifdef INET 1290 case PF_INET: 1291 pcb.ki_family = inp->inp_socket->so_proto-> 1292 pr_domain->dom_family; 1293 pcb.ki_type = inp->inp_socket->so_proto-> 1294 pr_type; 1295 pcb.ki_protocol = inp->inp_socket->so_proto-> 1296 pr_protocol; 1297 pcb.ki_pflags = inp->inp_flags; 1298 1299 pcb.ki_sostate = inp->inp_socket->so_state; 1300 pcb.ki_prstate = inp->inp_state; 1301 if (proto == IPPROTO_TCP) { 1302 tp = intotcpcb(inp); 1303 pcb.ki_tstate = tp->t_state; 1304 pcb.ki_tflags = tp->t_flags; 1305 } 1306 1307 pcb.ki_pcbaddr = PTRTOUINT64(inp); 1308 pcb.ki_ppcbaddr = PTRTOUINT64(inp->inp_ppcb); 1309 pcb.ki_sockaddr = PTRTOUINT64(inp->inp_socket); 1310 1311 pcb.ki_rcvq = inp->inp_socket->so_rcv.sb_cc; 1312 pcb.ki_sndq = inp->inp_socket->so_snd.sb_cc; 1313 1314 in = satosin(&pcb.ki_src); 1315 in->sin_len = sizeof(*in); 1316 in->sin_family = pf; 1317 in->sin_port = inp->inp_lport; 1318 in->sin_addr = inp->inp_laddr; 1319 if (pcb.ki_prstate >= INP_CONNECTED) { 1320 in = satosin(&pcb.ki_dst); 1321 in->sin_len = sizeof(*in); 1322 in->sin_family = pf; 1323 in->sin_port = inp->inp_fport; 1324 in->sin_addr = inp->inp_faddr; 1325 } 1326 break; 1327 #endif 1328 #ifdef INET6 1329 case PF_INET6: 1330 pcb.ki_family = in6p->in6p_socket->so_proto-> 1331 pr_domain->dom_family; 1332 pcb.ki_type = in6p->in6p_socket->so_proto->pr_type; 1333 pcb.ki_protocol = in6p->in6p_socket->so_proto-> 1334 pr_protocol; 1335 pcb.ki_pflags = in6p->in6p_flags; 1336 1337 pcb.ki_sostate = in6p->in6p_socket->so_state; 1338 pcb.ki_prstate = in6p->in6p_state; 1339 if (proto == IPPROTO_TCP) { 1340 tp = in6totcpcb(in6p); 1341 pcb.ki_tstate = tp->t_state; 1342 pcb.ki_tflags = tp->t_flags; 1343 } 1344 1345 pcb.ki_pcbaddr = PTRTOUINT64(in6p); 1346 pcb.ki_ppcbaddr = PTRTOUINT64(in6p->in6p_ppcb); 1347 pcb.ki_sockaddr = PTRTOUINT64(in6p->in6p_socket); 1348 1349 pcb.ki_rcvq = in6p->in6p_socket->so_rcv.sb_cc; 1350 pcb.ki_sndq = in6p->in6p_socket->so_snd.sb_cc; 1351 1352 in6 = satosin6(&pcb.ki_src); 1353 in6->sin6_len = sizeof(*in6); 1354 in6->sin6_family = pf; 1355 in6->sin6_port = in6p->in6p_lport; 1356 in6->sin6_flowinfo = in6p->in6p_flowinfo; 1357 in6->sin6_addr = in6p->in6p_laddr; 1358 in6->sin6_scope_id = 0; /* XXX? */ 1359 1360 if (pcb.ki_prstate >= IN6P_CONNECTED) { 1361 in6 = satosin6(&pcb.ki_dst); 1362 in6->sin6_len = sizeof(*in6); 1363 in6->sin6_family = pf; 1364 in6->sin6_port = in6p->in6p_fport; 1365 in6->sin6_flowinfo = in6p->in6p_flowinfo; 1366 in6->sin6_addr = in6p->in6p_faddr; 1367 in6->sin6_scope_id = 0; /* XXX? */ 1368 } 1369 break; 1370 #endif 1371 } 1372 1373 if (len >= elem_size && elem_count > 0) { 1374 error = copyout(&pcb, dp, out_size); 1375 if (error) 1376 return (error); 1377 dp += elem_size; 1378 len -= elem_size; 1379 } 1380 if (elem_count > 0) { 1381 needed += elem_size; 1382 if (elem_count != INT_MAX) 1383 elem_count--; 1384 } 1385 } 1386 1387 *oldlenp = needed; 1388 if (oldp == NULL) 1389 *oldlenp += PCB_SLOP * sizeof(struct kinfo_pcb); 1390 1391 return (error); 1392 } 1393 1394 static int 1395 sysctl_tcp_congctl(SYSCTLFN_ARGS) 1396 { 1397 struct sysctlnode node; 1398 int error, r; 1399 char newname[TCPCC_MAXLEN]; 1400 1401 strlcpy(newname, tcp_congctl_global_name, sizeof(newname) - 1); 1402 1403 node = *rnode; 1404 node.sysctl_data = newname; 1405 node.sysctl_size = sizeof(newname); 1406 1407 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1408 1409 if (error || 1410 newp == NULL || 1411 strncmp(newname, tcp_congctl_global_name, sizeof(newname)) == 0) 1412 return error; 1413 1414 if ((r = tcp_congctl_select(NULL, newname))) 1415 return r; 1416 1417 return error; 1418 } 1419 1420 /* 1421 * this (second stage) setup routine is a replacement for tcp_sysctl() 1422 * (which is currently used for ipv4 and ipv6) 1423 */ 1424 static void 1425 sysctl_net_inet_tcp_setup2(struct sysctllog **clog, int pf, const char *pfname, 1426 const char *tcpname) 1427 { 1428 int ecn_node, congctl_node; 1429 const struct sysctlnode *sack_node, *node; 1430 #ifdef TCP_DEBUG 1431 extern struct tcp_debug tcp_debug[TCP_NDEBUG]; 1432 extern int tcp_debx; 1433 #endif 1434 1435 sysctl_createv(clog, 0, NULL, NULL, 1436 CTLFLAG_PERMANENT, 1437 CTLTYPE_NODE, "net", NULL, 1438 NULL, 0, NULL, 0, 1439 CTL_NET, CTL_EOL); 1440 sysctl_createv(clog, 0, NULL, NULL, 1441 CTLFLAG_PERMANENT, 1442 CTLTYPE_NODE, pfname, NULL, 1443 NULL, 0, NULL, 0, 1444 CTL_NET, pf, CTL_EOL); 1445 sysctl_createv(clog, 0, NULL, NULL, 1446 CTLFLAG_PERMANENT, 1447 CTLTYPE_NODE, tcpname, 1448 SYSCTL_DESCR("TCP related settings"), 1449 NULL, 0, NULL, 0, 1450 CTL_NET, pf, IPPROTO_TCP, CTL_EOL); 1451 1452 sysctl_createv(clog, 0, NULL, NULL, 1453 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1454 CTLTYPE_INT, "rfc1323", 1455 SYSCTL_DESCR("Enable RFC1323 TCP extensions"), 1456 NULL, 0, &tcp_do_rfc1323, 0, 1457 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RFC1323, CTL_EOL); 1458 sysctl_createv(clog, 0, NULL, NULL, 1459 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1460 CTLTYPE_INT, "sendspace", 1461 SYSCTL_DESCR("Default TCP send buffer size"), 1462 NULL, 0, &tcp_sendspace, 0, 1463 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SENDSPACE, CTL_EOL); 1464 sysctl_createv(clog, 0, NULL, NULL, 1465 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1466 CTLTYPE_INT, "recvspace", 1467 SYSCTL_DESCR("Default TCP receive buffer size"), 1468 NULL, 0, &tcp_recvspace, 0, 1469 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RECVSPACE, CTL_EOL); 1470 sysctl_createv(clog, 0, NULL, NULL, 1471 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1472 CTLTYPE_INT, "mssdflt", 1473 SYSCTL_DESCR("Default maximum segment size"), 1474 sysctl_net_inet_tcp_mssdflt, 0, &tcp_mssdflt, 0, 1475 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSSDFLT, CTL_EOL); 1476 sysctl_createv(clog, 0, NULL, NULL, 1477 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1478 CTLTYPE_INT, "syn_cache_limit", 1479 SYSCTL_DESCR("Maximum number of entries in the TCP " 1480 "compressed state engine"), 1481 NULL, 0, &tcp_syn_cache_limit, 0, 1482 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_LIMIT, 1483 CTL_EOL); 1484 sysctl_createv(clog, 0, NULL, NULL, 1485 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1486 CTLTYPE_INT, "syn_bucket_limit", 1487 SYSCTL_DESCR("Maximum number of entries per hash " 1488 "bucket in the TCP compressed state " 1489 "engine"), 1490 NULL, 0, &tcp_syn_bucket_limit, 0, 1491 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_BUCKET_LIMIT, 1492 CTL_EOL); 1493 #if 0 /* obsoleted */ 1494 sysctl_createv(clog, 0, NULL, NULL, 1495 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1496 CTLTYPE_INT, "syn_cache_interval", 1497 SYSCTL_DESCR("TCP compressed state engine's timer interval"), 1498 NULL, 0, &tcp_syn_cache_interval, 0, 1499 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SYN_CACHE_INTER, 1500 CTL_EOL); 1501 #endif 1502 sysctl_createv(clog, 0, NULL, NULL, 1503 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1504 CTLTYPE_INT, "init_win", 1505 SYSCTL_DESCR("Initial TCP congestion window"), 1506 NULL, 0, &tcp_init_win, 0, 1507 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN, CTL_EOL); 1508 sysctl_createv(clog, 0, NULL, NULL, 1509 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1510 CTLTYPE_INT, "mss_ifmtu", 1511 SYSCTL_DESCR("Use interface MTU for calculating MSS"), 1512 NULL, 0, &tcp_mss_ifmtu, 0, 1513 CTL_NET, pf, IPPROTO_TCP, TCPCTL_MSS_IFMTU, CTL_EOL); 1514 sysctl_createv(clog, 0, NULL, &sack_node, 1515 CTLFLAG_PERMANENT, 1516 CTLTYPE_NODE, "sack", 1517 SYSCTL_DESCR("RFC2018 Selective ACKnowledgement tunables"), 1518 NULL, 0, NULL, 0, 1519 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_EOL); 1520 sysctl_createv(clog, 0, NULL, &node, 1521 CTLFLAG_PERMANENT, 1522 CTLTYPE_NODE, "ecn", 1523 SYSCTL_DESCR("RFC3168 Explicit Congestion Notification"), 1524 NULL, 0, NULL, 0, 1525 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1526 ecn_node = node->sysctl_num; 1527 sysctl_createv(clog, 0, NULL, &node, 1528 CTLFLAG_PERMANENT, 1529 CTLTYPE_NODE, "congctl", 1530 SYSCTL_DESCR("TCP Congestion Control"), 1531 NULL, 0, NULL, 0, 1532 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, CTL_EOL); 1533 congctl_node = node->sysctl_num; 1534 sysctl_createv(clog, 0, NULL, NULL, 1535 CTLFLAG_PERMANENT, 1536 CTLTYPE_STRING, "available", 1537 SYSCTL_DESCR("Available Congestion Control Mechanisms"), 1538 NULL, 0, &tcp_congctl_avail, 0, 1539 CTL_NET, pf, IPPROTO_TCP, congctl_node, 1540 CTL_CREATE, CTL_EOL); 1541 sysctl_createv(clog, 0, NULL, NULL, 1542 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1543 CTLTYPE_STRING, "selected", 1544 SYSCTL_DESCR("Selected Congestion Control Mechanism"), 1545 sysctl_tcp_congctl, 0, NULL, TCPCC_MAXLEN, 1546 CTL_NET, pf, IPPROTO_TCP, congctl_node, 1547 CTL_CREATE, CTL_EOL); 1548 1549 sysctl_createv(clog, 0, NULL, NULL, 1550 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1551 CTLTYPE_INT, "win_scale", 1552 SYSCTL_DESCR("Use RFC1323 window scale options"), 1553 NULL, 0, &tcp_do_win_scale, 0, 1554 CTL_NET, pf, IPPROTO_TCP, TCPCTL_WSCALE, CTL_EOL); 1555 sysctl_createv(clog, 0, NULL, NULL, 1556 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1557 CTLTYPE_INT, "timestamps", 1558 SYSCTL_DESCR("Use RFC1323 time stamp options"), 1559 NULL, 0, &tcp_do_timestamps, 0, 1560 CTL_NET, pf, IPPROTO_TCP, TCPCTL_TSTAMP, CTL_EOL); 1561 sysctl_createv(clog, 0, NULL, NULL, 1562 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1563 CTLTYPE_INT, "compat_42", 1564 SYSCTL_DESCR("Enable workarounds for 4.2BSD TCP bugs"), 1565 NULL, 0, &tcp_compat_42, 0, 1566 CTL_NET, pf, IPPROTO_TCP, TCPCTL_COMPAT_42, CTL_EOL); 1567 sysctl_createv(clog, 0, NULL, NULL, 1568 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1569 CTLTYPE_INT, "cwm", 1570 SYSCTL_DESCR("Hughes/Touch/Heidemann Congestion Window " 1571 "Monitoring"), 1572 NULL, 0, &tcp_cwm, 0, 1573 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM, CTL_EOL); 1574 sysctl_createv(clog, 0, NULL, NULL, 1575 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1576 CTLTYPE_INT, "cwm_burstsize", 1577 SYSCTL_DESCR("Congestion Window Monitoring allowed " 1578 "burst count in packets"), 1579 NULL, 0, &tcp_cwm_burstsize, 0, 1580 CTL_NET, pf, IPPROTO_TCP, TCPCTL_CWM_BURSTSIZE, 1581 CTL_EOL); 1582 sysctl_createv(clog, 0, NULL, NULL, 1583 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1584 CTLTYPE_INT, "ack_on_push", 1585 SYSCTL_DESCR("Immediately return ACK when PSH is " 1586 "received"), 1587 NULL, 0, &tcp_ack_on_push, 0, 1588 CTL_NET, pf, IPPROTO_TCP, TCPCTL_ACK_ON_PUSH, CTL_EOL); 1589 sysctl_createv(clog, 0, NULL, NULL, 1590 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1591 CTLTYPE_INT, "keepidle", 1592 SYSCTL_DESCR("Allowed connection idle ticks before a " 1593 "keepalive probe is sent"), 1594 NULL, 0, &tcp_keepidle, 0, 1595 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPIDLE, CTL_EOL); 1596 sysctl_createv(clog, 0, NULL, NULL, 1597 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1598 CTLTYPE_INT, "keepintvl", 1599 SYSCTL_DESCR("Ticks before next keepalive probe is sent"), 1600 NULL, 0, &tcp_keepintvl, 0, 1601 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPINTVL, CTL_EOL); 1602 sysctl_createv(clog, 0, NULL, NULL, 1603 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1604 CTLTYPE_INT, "keepcnt", 1605 SYSCTL_DESCR("Number of keepalive probes to send"), 1606 NULL, 0, &tcp_keepcnt, 0, 1607 CTL_NET, pf, IPPROTO_TCP, TCPCTL_KEEPCNT, CTL_EOL); 1608 sysctl_createv(clog, 0, NULL, NULL, 1609 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 1610 CTLTYPE_INT, "slowhz", 1611 SYSCTL_DESCR("Keepalive ticks per second"), 1612 NULL, PR_SLOWHZ, NULL, 0, 1613 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SLOWHZ, CTL_EOL); 1614 sysctl_createv(clog, 0, NULL, NULL, 1615 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1616 CTLTYPE_INT, "log_refused", 1617 SYSCTL_DESCR("Log refused TCP connections"), 1618 NULL, 0, &tcp_log_refused, 0, 1619 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOG_REFUSED, CTL_EOL); 1620 #if 0 /* obsoleted */ 1621 sysctl_createv(clog, 0, NULL, NULL, 1622 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1623 CTLTYPE_INT, "rstratelimit", NULL, 1624 NULL, 0, &tcp_rst_ratelim, 0, 1625 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTRATELIMIT, CTL_EOL); 1626 #endif 1627 sysctl_createv(clog, 0, NULL, NULL, 1628 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1629 CTLTYPE_INT, "rstppslimit", 1630 SYSCTL_DESCR("Maximum number of RST packets to send " 1631 "per second"), 1632 NULL, 0, &tcp_rst_ppslim, 0, 1633 CTL_NET, pf, IPPROTO_TCP, TCPCTL_RSTPPSLIMIT, CTL_EOL); 1634 sysctl_createv(clog, 0, NULL, NULL, 1635 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1636 CTLTYPE_INT, "delack_ticks", 1637 SYSCTL_DESCR("Number of ticks to delay sending an ACK"), 1638 NULL, 0, &tcp_delack_ticks, 0, 1639 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DELACK_TICKS, CTL_EOL); 1640 sysctl_createv(clog, 0, NULL, NULL, 1641 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1642 CTLTYPE_INT, "init_win_local", 1643 SYSCTL_DESCR("Initial TCP window size (in segments)"), 1644 NULL, 0, &tcp_init_win_local, 0, 1645 CTL_NET, pf, IPPROTO_TCP, TCPCTL_INIT_WIN_LOCAL, 1646 CTL_EOL); 1647 sysctl_createv(clog, 0, NULL, NULL, 1648 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1649 CTLTYPE_STRUCT, "ident", 1650 SYSCTL_DESCR("RFC1413 Identification Protocol lookups"), 1651 sysctl_net_inet_tcp_ident, 0, NULL, sizeof(uid_t), 1652 CTL_NET, pf, IPPROTO_TCP, TCPCTL_IDENT, CTL_EOL); 1653 sysctl_createv(clog, 0, NULL, NULL, 1654 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1655 CTLTYPE_INT, "do_loopback_cksum", 1656 SYSCTL_DESCR("Perform TCP checksum on loopback"), 1657 NULL, 0, &tcp_do_loopback_cksum, 0, 1658 CTL_NET, pf, IPPROTO_TCP, TCPCTL_LOOPBACKCKSUM, 1659 CTL_EOL); 1660 sysctl_createv(clog, 0, NULL, NULL, 1661 CTLFLAG_PERMANENT, 1662 CTLTYPE_STRUCT, "pcblist", 1663 SYSCTL_DESCR("TCP protocol control block list"), 1664 sysctl_inpcblist, 0, &tcbtable, 0, 1665 CTL_NET, pf, IPPROTO_TCP, CTL_CREATE, 1666 CTL_EOL); 1667 1668 sysctl_createv(clog, 0, NULL, NULL, 1669 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1670 CTLTYPE_INT, "enable", 1671 SYSCTL_DESCR("Enable TCP Explicit Congestion " 1672 "Notification"), 1673 NULL, 0, &tcp_do_ecn, 0, 1674 CTL_NET, pf, IPPROTO_TCP, ecn_node, 1675 CTL_CREATE, CTL_EOL); 1676 sysctl_createv(clog, 0, NULL, NULL, 1677 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1678 CTLTYPE_INT, "maxretries", 1679 SYSCTL_DESCR("Number of times to retry ECN setup " 1680 "before disabling ECN on the connection"), 1681 NULL, 0, &tcp_ecn_maxretries, 0, 1682 CTL_NET, pf, IPPROTO_TCP, ecn_node, 1683 CTL_CREATE, CTL_EOL); 1684 1685 /* SACK gets it's own little subtree. */ 1686 sysctl_createv(clog, 0, NULL, &sack_node, 1687 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1688 CTLTYPE_INT, "enable", 1689 SYSCTL_DESCR("Enable RFC2018 Selective ACKnowledgement"), 1690 NULL, 0, &tcp_do_sack, 0, 1691 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 1692 sysctl_createv(clog, 0, NULL, &sack_node, 1693 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1694 CTLTYPE_INT, "maxholes", 1695 SYSCTL_DESCR("Maximum number of TCP SACK holes allowed per connection"), 1696 NULL, 0, &tcp_sack_tp_maxholes, 0, 1697 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 1698 sysctl_createv(clog, 0, NULL, &sack_node, 1699 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1700 CTLTYPE_INT, "globalmaxholes", 1701 SYSCTL_DESCR("Global maximum number of TCP SACK holes"), 1702 NULL, 0, &tcp_sack_globalmaxholes, 0, 1703 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 1704 sysctl_createv(clog, 0, NULL, &sack_node, 1705 CTLFLAG_PERMANENT, 1706 CTLTYPE_INT, "globalholes", 1707 SYSCTL_DESCR("Global number of TCP SACK holes"), 1708 NULL, 0, &tcp_sack_globalholes, 0, 1709 CTL_NET, pf, IPPROTO_TCP, TCPCTL_SACK, CTL_CREATE, CTL_EOL); 1710 1711 sysctl_createv(clog, 0, NULL, NULL, 1712 CTLFLAG_PERMANENT, 1713 CTLTYPE_STRUCT, "stats", 1714 SYSCTL_DESCR("TCP statistics"), 1715 NULL, 0, &tcpstat, sizeof(tcpstat), 1716 CTL_NET, pf, IPPROTO_TCP, TCPCTL_STATS, 1717 CTL_EOL); 1718 #ifdef TCP_DEBUG 1719 sysctl_createv(clog, 0, NULL, NULL, 1720 CTLFLAG_PERMANENT, 1721 CTLTYPE_STRUCT, "debug", 1722 SYSCTL_DESCR("TCP sockets debug information"), 1723 NULL, 0, &tcp_debug, sizeof(tcp_debug), 1724 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBUG, 1725 CTL_EOL); 1726 sysctl_createv(clog, 0, NULL, NULL, 1727 CTLFLAG_PERMANENT, 1728 CTLTYPE_INT, "debx", 1729 SYSCTL_DESCR("Number of TCP debug sockets messages"), 1730 NULL, 0, &tcp_debx, sizeof(tcp_debx), 1731 CTL_NET, pf, IPPROTO_TCP, TCPCTL_DEBX, 1732 CTL_EOL); 1733 #endif 1734 1735 } 1736 1737 /* 1738 * Sysctl for tcp variables. 1739 */ 1740 #ifdef INET 1741 SYSCTL_SETUP(sysctl_net_inet_tcp_setup, "sysctl net.inet.tcp subtree setup") 1742 { 1743 1744 sysctl_net_inet_tcp_setup2(clog, PF_INET, "inet", "tcp"); 1745 } 1746 #endif /* INET */ 1747 1748 #ifdef INET6 1749 SYSCTL_SETUP(sysctl_net_inet6_tcp6_setup, "sysctl net.inet6.tcp6 subtree setup") 1750 { 1751 1752 sysctl_net_inet_tcp_setup2(clog, PF_INET6, "inet6", "tcp6"); 1753 } 1754 #endif /* INET6 */ 1755