The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/tcp_usrreq.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: b83289cba832280a40188f3f2a8cf877


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.