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/in_pcb.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 /*-
    2  * Copyright (c) 1982, 1986, 1991, 1993, 1995
    3  *      The Regents of the University of California.
    4  * Copyright (c) 2007-2009 Robert N. M. Watson
    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  * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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  *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/7.4/sys/netinet/in_pcb.c 202924 2010-01-24 14:05:56Z bz $");
   36 
   37 #include "opt_ddb.h"
   38 #include "opt_ipsec.h"
   39 #include "opt_inet6.h"
   40 #include "opt_mac.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/malloc.h>
   45 #include <sys/mbuf.h>
   46 #include <sys/domain.h>
   47 #include <sys/protosw.h>
   48 #include <sys/socket.h>
   49 #include <sys/socketvar.h>
   50 #include <sys/priv.h>
   51 #include <sys/proc.h>
   52 #include <sys/jail.h>
   53 #include <sys/kernel.h>
   54 #include <sys/sysctl.h>
   55 
   56 #ifdef DDB
   57 #include <ddb/ddb.h>
   58 #endif
   59 
   60 #include <vm/uma.h>
   61 
   62 #include <net/if.h>
   63 #include <net/if_types.h>
   64 #include <net/route.h>
   65 
   66 #include <netinet/in.h>
   67 #include <netinet/in_pcb.h>
   68 #include <netinet/in_var.h>
   69 #include <netinet/ip_var.h>
   70 #include <netinet/tcp_var.h>
   71 #include <netinet/udp.h>
   72 #include <netinet/udp_var.h>
   73 #ifdef INET6
   74 #include <netinet/ip6.h>
   75 #include <netinet6/ip6_var.h>
   76 #endif /* INET6 */
   77 
   78 
   79 #ifdef IPSEC
   80 #include <netipsec/ipsec.h>
   81 #include <netipsec/key.h>
   82 #endif /* IPSEC */
   83 
   84 #include <security/mac/mac_framework.h>
   85 
   86 /*
   87  * These configure the range of local port addresses assigned to
   88  * "unspecified" outgoing connections/packets/whatever.
   89  */
   90 int     ipport_lowfirstauto  = IPPORT_RESERVED - 1;     /* 1023 */
   91 int     ipport_lowlastauto = IPPORT_RESERVEDSTART;      /* 600 */
   92 int     ipport_firstauto = IPPORT_HIFIRSTAUTO;          /* 49152 */
   93 int     ipport_lastauto  = IPPORT_HILASTAUTO;           /* 65535 */
   94 int     ipport_hifirstauto = IPPORT_HIFIRSTAUTO;        /* 49152 */
   95 int     ipport_hilastauto  = IPPORT_HILASTAUTO;         /* 65535 */
   96 
   97 /*
   98  * Reserved ports accessible only to root. There are significant
   99  * security considerations that must be accounted for when changing these,
  100  * but the security benefits can be great. Please be careful.
  101  */
  102 int     ipport_reservedhigh = IPPORT_RESERVED - 1;      /* 1023 */
  103 int     ipport_reservedlow = 0;
  104 
  105 /* Variables dealing with random ephemeral port allocation. */
  106 int     ipport_randomized = 1;  /* user controlled via sysctl */
  107 int     ipport_randomcps = 10;  /* user controlled via sysctl */
  108 int     ipport_randomtime = 45; /* user controlled via sysctl */
  109 int     ipport_stoprandom = 0;  /* toggled by ipport_tick */
  110 int     ipport_tcpallocs;
  111 int     ipport_tcplastcount;
  112 
  113 #define RANGECHK(var, min, max) \
  114         if ((var) < (min)) { (var) = (min); } \
  115         else if ((var) > (max)) { (var) = (max); }
  116 
  117 static int
  118 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
  119 {
  120         int error;
  121 
  122         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
  123         if (error == 0) {
  124                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
  125                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
  126                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
  127                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
  128                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
  129                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
  130         }
  131         return (error);
  132 }
  133 
  134 #undef RANGECHK
  135 
  136 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
  137 
  138 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
  139            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
  140 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
  141            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
  142 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
  143            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
  144 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
  145            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
  146 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
  147            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
  148 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
  149            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
  150 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
  151            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
  152 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
  153            CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
  154 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
  155            &ipport_randomized, 0, "Enable random port allocation");
  156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
  157            &ipport_randomcps, 0, "Maximum number of random port "
  158            "allocations before switching to a sequental one");
  159 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
  160            &ipport_randomtime, 0, "Minimum time to keep sequental port "
  161            "allocation before switching to a random one");
  162 
  163 /*
  164  * in_pcb.c: manage the Protocol Control Blocks.
  165  *
  166  * NOTE: It is assumed that most of these functions will be called with
  167  * the pcbinfo lock held, and often, the inpcb lock held, as these utility
  168  * functions often modify hash chains or addresses in pcbs.
  169  */
  170 
  171 /*
  172  * Allocate a PCB and associate it with the socket.
  173  * On success return with the PCB locked.
  174  */
  175 int
  176 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
  177 {
  178         struct inpcb *inp;
  179         int error;
  180 
  181         INP_INFO_WLOCK_ASSERT(pcbinfo);
  182         error = 0;
  183         inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
  184         if (inp == NULL)
  185                 return (ENOBUFS);
  186         bzero(inp, inp_zero_size);
  187         inp->inp_pcbinfo = pcbinfo;
  188         inp->inp_socket = so;
  189         inp->inp_cred = crhold(so->so_cred);
  190         inp->inp_inc.inc_fibnum = so->so_fibnum;
  191 #ifdef MAC
  192         error = mac_init_inpcb(inp, M_NOWAIT);
  193         if (error != 0)
  194                 goto out;
  195         SOCK_LOCK(so);
  196         mac_create_inpcb_from_socket(so, inp);
  197         SOCK_UNLOCK(so);
  198 #endif
  199 #ifdef IPSEC
  200         error = ipsec_init_policy(so, &inp->inp_sp);
  201         if (error != 0) {
  202 #ifdef MAC
  203                 mac_destroy_inpcb(inp);
  204 #endif
  205                 goto out;
  206         }
  207 #endif /*IPSEC*/
  208 #ifdef INET6
  209         if (INP_SOCKAF(so) == AF_INET6) {
  210                 inp->inp_vflag |= INP_IPV6PROTO;
  211                 if (ip6_v6only)
  212                         inp->inp_flags |= IN6P_IPV6_V6ONLY;
  213         }
  214 #endif
  215         LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
  216         pcbinfo->ipi_count++;
  217         so->so_pcb = (caddr_t)inp;
  218 #ifdef INET6
  219         if (ip6_auto_flowlabel)
  220                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
  221 #endif
  222         INP_WLOCK(inp);
  223         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
  224 #if defined(IPSEC) || defined(MAC)
  225 out:
  226         if (error != 0) {
  227                 crfree(inp->inp_cred);
  228                 uma_zfree(pcbinfo->ipi_zone, inp);
  229         }
  230 #endif
  231         return (error);
  232 }
  233 
  234 int
  235 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
  236 {
  237         int anonport, error;
  238 
  239         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  240         INP_WLOCK_ASSERT(inp);
  241 
  242         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
  243                 return (EINVAL);
  244         anonport = inp->inp_lport == 0 && (nam == NULL ||
  245             ((struct sockaddr_in *)nam)->sin_port == 0);
  246         error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
  247             &inp->inp_lport, cred);
  248         if (error)
  249                 return (error);
  250         if (in_pcbinshash(inp) != 0) {
  251                 inp->inp_laddr.s_addr = INADDR_ANY;
  252                 inp->inp_lport = 0;
  253                 return (EAGAIN);
  254         }
  255         if (anonport)
  256                 inp->inp_flags |= INP_ANONPORT;
  257         return (0);
  258 }
  259 
  260 /*
  261  * Set up a bind operation on a PCB, performing port allocation
  262  * as required, but do not actually modify the PCB. Callers can
  263  * either complete the bind by setting inp_laddr/inp_lport and
  264  * calling in_pcbinshash(), or they can just use the resulting
  265  * port and address to authorise the sending of a once-off packet.
  266  *
  267  * On error, the values of *laddrp and *lportp are not changed.
  268  */
  269 int
  270 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
  271     u_short *lportp, struct ucred *cred)
  272 {
  273         struct socket *so = inp->inp_socket;
  274         unsigned short *lastport;
  275         struct sockaddr_in *sin;
  276         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
  277         struct in_addr laddr;
  278         u_short lport = 0;
  279         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
  280         int error;
  281         int dorandom;
  282 
  283         /*
  284          * Because no actual state changes occur here, a global write lock on
  285          * the pcbinfo isn't required.
  286          */
  287         INP_INFO_LOCK_ASSERT(pcbinfo);
  288         INP_LOCK_ASSERT(inp);
  289 
  290         if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
  291                 return (EADDRNOTAVAIL);
  292         laddr.s_addr = *laddrp;
  293         if (nam != NULL && laddr.s_addr != INADDR_ANY)
  294                 return (EINVAL);
  295         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
  296                 wild = INPLOOKUP_WILDCARD;
  297         if (nam == NULL) {
  298                 if ((error = prison_local_ip4(cred, &laddr)) != 0)
  299                         return (error);
  300         } else {
  301                 sin = (struct sockaddr_in *)nam;
  302                 if (nam->sa_len != sizeof (*sin))
  303                         return (EINVAL);
  304 #ifdef notdef
  305                 /*
  306                  * We should check the family, but old programs
  307                  * incorrectly fail to initialize it.
  308                  */
  309                 if (sin->sin_family != AF_INET)
  310                         return (EAFNOSUPPORT);
  311 #endif
  312                 error = prison_local_ip4(cred, &sin->sin_addr);
  313                 if (error)
  314                         return (error);
  315                 if (sin->sin_port != *lportp) {
  316                         /* Don't allow the port to change. */
  317                         if (*lportp != 0)
  318                                 return (EINVAL);
  319                         lport = sin->sin_port;
  320                 }
  321                 /* NB: lport is left as 0 if the port isn't being changed. */
  322                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
  323                         /*
  324                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
  325                          * allow complete duplication of binding if
  326                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
  327                          * and a multicast address is bound on both
  328                          * new and duplicated sockets.
  329                          */
  330                         if (so->so_options & SO_REUSEADDR)
  331                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
  332                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
  333                         sin->sin_port = 0;              /* yech... */
  334                         bzero(&sin->sin_zero, sizeof(sin->sin_zero));
  335                         if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
  336                                 return (EADDRNOTAVAIL);
  337                 }
  338                 laddr = sin->sin_addr;
  339                 if (lport) {
  340                         struct inpcb *t;
  341                         struct tcptw *tw;
  342 
  343                         /* GROSS */
  344                         if (ntohs(lport) <= ipport_reservedhigh &&
  345                             ntohs(lport) >= ipport_reservedlow &&
  346                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
  347                             0))
  348                                 return (EACCES);
  349                         if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
  350                             priv_check_cred(inp->inp_cred,
  351                             PRIV_NETINET_REUSEPORT, 0) != 0) {
  352                                 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
  353                                     lport, INPLOOKUP_WILDCARD, cred);
  354         /*
  355          * XXX
  356          * This entire block sorely needs a rewrite.
  357          */
  358                                 if (t &&
  359                                     ((t->inp_flags & INP_TIMEWAIT) == 0) &&
  360                                     (so->so_type != SOCK_STREAM ||
  361                                      ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
  362                                     (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
  363                                      ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
  364                                      (t->inp_socket->so_options &
  365                                          SO_REUSEPORT) == 0) &&
  366                                     (inp->inp_cred->cr_uid !=
  367                                      t->inp_cred->cr_uid))
  368                                         return (EADDRINUSE);
  369                         }
  370                         t = in_pcblookup_local(pcbinfo, sin->sin_addr,
  371                             lport, wild, cred);
  372                         if (t && (t->inp_flags & INP_TIMEWAIT)) {
  373                                 /*
  374                                  * XXXRW: If an incpb has had its timewait
  375                                  * state recycled, we treat the address as
  376                                  * being in use (for now).  This is better
  377                                  * than a panic, but not desirable.
  378                                  */
  379                                 tw = intotw(inp);
  380                                 if (tw == NULL ||
  381                                     (reuseport & tw->tw_so_options) == 0)
  382                                         return (EADDRINUSE);
  383                         } else if (t &&
  384                             (reuseport & t->inp_socket->so_options) == 0) {
  385 #ifdef INET6
  386                                 if (ntohl(sin->sin_addr.s_addr) !=
  387                                     INADDR_ANY ||
  388                                     ntohl(t->inp_laddr.s_addr) !=
  389                                     INADDR_ANY ||
  390                                     INP_SOCKAF(so) ==
  391                                     INP_SOCKAF(t->inp_socket))
  392 #endif
  393                                 return (EADDRINUSE);
  394                         }
  395                 }
  396         }
  397         if (*lportp != 0)
  398                 lport = *lportp;
  399         if (lport == 0) {
  400                 u_short first, last;
  401                 int count;
  402 
  403                 if (inp->inp_flags & INP_HIGHPORT) {
  404                         first = ipport_hifirstauto;     /* sysctl */
  405                         last  = ipport_hilastauto;
  406                         lastport = &pcbinfo->ipi_lasthi;
  407                 } else if (inp->inp_flags & INP_LOWPORT) {
  408                         error = priv_check_cred(cred,
  409                             PRIV_NETINET_RESERVEDPORT, 0);
  410                         if (error)
  411                                 return error;
  412                         first = ipport_lowfirstauto;    /* 1023 */
  413                         last  = ipport_lowlastauto;     /* 600 */
  414                         lastport = &pcbinfo->ipi_lastlow;
  415                 } else {
  416                         first = ipport_firstauto;       /* sysctl */
  417                         last  = ipport_lastauto;
  418                         lastport = &pcbinfo->ipi_lastport;
  419                 }
  420                 /*
  421                  * For UDP, use random port allocation as long as the user
  422                  * allows it.  For TCP (and as of yet unknown) connections,
  423                  * use random port allocation only if the user allows it AND
  424                  * ipport_tick() allows it.
  425                  */
  426                 if (ipport_randomized &&
  427                         (!ipport_stoprandom || pcbinfo == &udbinfo))
  428                         dorandom = 1;
  429                 else
  430                         dorandom = 0;
  431                 /*
  432                  * It makes no sense to do random port allocation if
  433                  * we have the only port available.
  434                  */
  435                 if (first == last)
  436                         dorandom = 0;
  437                 /* Make sure to not include UDP packets in the count. */
  438                 if (pcbinfo != &udbinfo)
  439                         ipport_tcpallocs++;
  440                 /*
  441                  * Instead of having two loops further down counting up or down
  442                  * make sure that first is always <= last and go with only one
  443                  * code path implementing all logic.
  444                  *
  445                  * We split the two cases (up and down) so that the direction
  446                  * is not being tested on each round of the loop.
  447                  */
  448                 if (first > last) {
  449                         /*
  450                          * counting down
  451                          */
  452                         if (dorandom)
  453                                 *lastport = first -
  454                                             (arc4random() % (first - last));
  455                         count = first - last;
  456 
  457                         do {
  458                                 if (count-- < 0)        /* completely used? */
  459                                         return (EADDRNOTAVAIL);
  460                                 --*lastport;
  461                                 if (*lastport > first || *lastport < last)
  462                                         *lastport = first;
  463                                 lport = htons(*lastport);
  464                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
  465                             wild, cred));
  466                 } else {
  467                         /*
  468                          * counting up
  469                          */
  470                         if (dorandom)
  471                                 *lastport = first +
  472                                             (arc4random() % (last - first));
  473                         count = last - first;
  474 
  475                         do {
  476                                 if (count-- < 0)        /* completely used? */
  477                                         return (EADDRNOTAVAIL);
  478                                 ++*lastport;
  479                                 if (*lastport < first || *lastport > last)
  480                                         *lastport = first;
  481                                 lport = htons(*lastport);
  482                         } while (in_pcblookup_local(pcbinfo, laddr, lport,
  483                             wild, cred));
  484                 }
  485         }
  486         *laddrp = laddr.s_addr;
  487         *lportp = lport;
  488         return (0);
  489 }
  490 
  491 /*
  492  * Connect from a socket to a specified address.
  493  * Both address and port must be specified in argument sin.
  494  * If don't have a local address for this socket yet,
  495  * then pick one.
  496  */
  497 int
  498 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
  499 {
  500         u_short lport, fport;
  501         in_addr_t laddr, faddr;
  502         int anonport, error;
  503 
  504         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  505         INP_WLOCK_ASSERT(inp);
  506 
  507         lport = inp->inp_lport;
  508         laddr = inp->inp_laddr.s_addr;
  509         anonport = (lport == 0);
  510         error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
  511             NULL, cred);
  512         if (error)
  513                 return (error);
  514 
  515         /* Do the initial binding of the local address if required. */
  516         if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
  517                 inp->inp_lport = lport;
  518                 inp->inp_laddr.s_addr = laddr;
  519                 if (in_pcbinshash(inp) != 0) {
  520                         inp->inp_laddr.s_addr = INADDR_ANY;
  521                         inp->inp_lport = 0;
  522                         return (EAGAIN);
  523                 }
  524         }
  525 
  526         /* Commit the remaining changes. */
  527         inp->inp_lport = lport;
  528         inp->inp_laddr.s_addr = laddr;
  529         inp->inp_faddr.s_addr = faddr;
  530         inp->inp_fport = fport;
  531         in_pcbrehash(inp);
  532 
  533         if (anonport)
  534                 inp->inp_flags |= INP_ANONPORT;
  535         return (0);
  536 }
  537 
  538 /*
  539  * Do proper source address selection on an unbound socket in case
  540  * of connect. Take jails into account as well.
  541  */
  542 static int
  543 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
  544     struct ucred *cred)
  545 {
  546         struct in_ifaddr *ia;
  547         struct ifaddr *ifa;
  548         struct sockaddr *sa;
  549         struct sockaddr_in *sin;
  550         struct route sro;
  551         int error;
  552 
  553         KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
  554 
  555         /*
  556          * Bypass source address selection and use the primary jail IP
  557          * if requested.
  558          */
  559         if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
  560                 return (0);
  561 
  562         error = 0;
  563         ia = NULL;
  564         bzero(&sro, sizeof(sro));
  565 
  566         sin = (struct sockaddr_in *)&sro.ro_dst;
  567         sin->sin_family = AF_INET;
  568         sin->sin_len = sizeof(struct sockaddr_in);
  569         sin->sin_addr.s_addr = faddr->s_addr;
  570 
  571         /*
  572          * If route is known our src addr is taken from the i/f,
  573          * else punt.
  574          *
  575          * Find out route to destination.
  576          */
  577         if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
  578                 in_rtalloc_ign(&sro, RTF_CLONING, inp->inp_inc.inc_fibnum);
  579 
  580         /*
  581          * If we found a route, use the address corresponding to
  582          * the outgoing interface.
  583          * 
  584          * Otherwise assume faddr is reachable on a directly connected
  585          * network and try to find a corresponding interface to take
  586          * the source address from.
  587          */
  588         if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
  589                 struct ifnet *ifp;
  590 
  591                 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
  592                 if (ia == NULL)
  593                         ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin));
  594                 if (ia == NULL) {
  595                         error = ENETUNREACH;
  596                         goto done;
  597                 }
  598 
  599                 if (cred == NULL || !jailed(cred)) {
  600                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
  601                         goto done;
  602                 }
  603 
  604                 ifp = ia->ia_ifp;
  605                 ia = NULL;
  606                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  607 
  608                         sa = ifa->ifa_addr;
  609                         if (sa->sa_family != AF_INET)
  610                                 continue;
  611                         sin = (struct sockaddr_in *)sa;
  612                         if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
  613                                 ia = (struct in_ifaddr *)ifa;
  614                                 break;
  615                         }
  616                 }
  617                 if (ia != NULL) {
  618                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
  619                         goto done;
  620                 }
  621 
  622                 /* 3. As a last resort return the 'default' jail address. */
  623                 error = prison_get_ip4(cred, laddr);
  624                 goto done;
  625         }
  626 
  627         /*
  628          * If the outgoing interface on the route found is not
  629          * a loopback interface, use the address from that interface.
  630          * In case of jails do those three steps:
  631          * 1. check if the interface address belongs to the jail. If so use it.
  632          * 2. check if we have any address on the outgoing interface
  633          *    belonging to this jail. If so use it.
  634          * 3. as a last resort return the 'default' jail address.
  635          */
  636         if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
  637 
  638                 /* If not jailed, use the default returned. */
  639                 if (cred == NULL || !jailed(cred)) {
  640                         ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
  641                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
  642                         goto done;
  643                 }
  644 
  645                 /* Jailed. */
  646                 /* 1. Check if the iface address belongs to the jail. */
  647                 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
  648                 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
  649                         ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
  650                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
  651                         goto done;
  652                 }
  653 
  654                 /*
  655                  * 2. Check if we have any address on the outgoing interface
  656                  *    belonging to this jail.
  657                  */
  658                 TAILQ_FOREACH(ifa, &sro.ro_rt->rt_ifp->if_addrhead, ifa_link) {
  659 
  660                         sa = ifa->ifa_addr;
  661                         if (sa->sa_family != AF_INET)
  662                                 continue;
  663                         sin = (struct sockaddr_in *)sa;
  664                         if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
  665                                 ia = (struct in_ifaddr *)ifa;
  666                                 break;
  667                         }
  668                 }
  669                 if (ia != NULL) {
  670                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
  671                         goto done;
  672                 }
  673 
  674                 /* 3. As a last resort return the 'default' jail address. */
  675                 error = prison_get_ip4(cred, laddr);
  676                 goto done;
  677         }
  678 
  679         /*
  680          * The outgoing interface is marked with 'loopback net', so a route
  681          * to ourselves is here.
  682          * Try to find the interface of the destination address and then
  683          * take the address from there. That interface is not necessarily
  684          * a loopback interface.
  685          * In case of jails, check that it is an address of the jail
  686          * and if we cannot find, fall back to the 'default' jail address.
  687          */
  688         if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
  689                 struct sockaddr_in sain;
  690 
  691                 bzero(&sain, sizeof(struct sockaddr_in));
  692                 sain.sin_family = AF_INET;
  693                 sain.sin_len = sizeof(struct sockaddr_in);
  694                 sain.sin_addr.s_addr = faddr->s_addr;
  695 
  696                 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
  697                 if (ia == NULL)
  698                         ia = ifatoia(ifa_ifwithnet(sintosa(&sain)));
  699 
  700                 if (cred == NULL || !jailed(cred)) {
  701 #if __FreeBSD_version < 800000
  702                         if (ia == NULL)
  703                                 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
  704 #endif
  705                         if (ia == NULL) {
  706                                 error = ENETUNREACH;
  707                                 goto done;
  708                         }
  709                         laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
  710                         goto done;
  711                 }
  712 
  713                 /* Jailed. */
  714                 if (ia != NULL) {
  715                         struct ifnet *ifp;
  716 
  717                         ifp = ia->ia_ifp;
  718                         ia = NULL;
  719                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  720 
  721                                 sa = ifa->ifa_addr;
  722                                 if (sa->sa_family != AF_INET)
  723                                         continue;
  724                                 sin = (struct sockaddr_in *)sa;
  725                                 if (prison_check_ip4(cred,
  726                                     &sin->sin_addr) == 0) {
  727                                         ia = (struct in_ifaddr *)ifa;
  728                                         break;
  729                                 }
  730                         }
  731                         if (ia != NULL) {
  732                                 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
  733                                 goto done;
  734                         }
  735                 }
  736 
  737                 /* 3. As a last resort return the 'default' jail address. */
  738                 error = prison_get_ip4(cred, laddr);
  739                 goto done;
  740         }
  741 
  742 done:
  743         if (sro.ro_rt != NULL)
  744                 RTFREE(sro.ro_rt);
  745         return (error);
  746 }
  747 
  748 /*
  749  * Set up for a connect from a socket to the specified address.
  750  * On entry, *laddrp and *lportp should contain the current local
  751  * address and port for the PCB; these are updated to the values
  752  * that should be placed in inp_laddr and inp_lport to complete
  753  * the connect.
  754  *
  755  * On success, *faddrp and *fportp will be set to the remote address
  756  * and port. These are not updated in the error case.
  757  *
  758  * If the operation fails because the connection already exists,
  759  * *oinpp will be set to the PCB of that connection so that the
  760  * caller can decide to override it. In all other cases, *oinpp
  761  * is set to NULL.
  762  */
  763 int
  764 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
  765     in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
  766     struct inpcb **oinpp, struct ucred *cred)
  767 {
  768         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
  769         struct in_ifaddr *ia;
  770         struct inpcb *oinp;
  771         struct in_addr laddr, faddr;
  772         u_short lport, fport;
  773         int error;
  774 
  775         /*
  776          * Because a global state change doesn't actually occur here, a read
  777          * lock is sufficient.
  778          */
  779         INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
  780         INP_LOCK_ASSERT(inp);
  781 
  782         if (oinpp != NULL)
  783                 *oinpp = NULL;
  784         if (nam->sa_len != sizeof (*sin))
  785                 return (EINVAL);
  786         if (sin->sin_family != AF_INET)
  787                 return (EAFNOSUPPORT);
  788         if (sin->sin_port == 0)
  789                 return (EADDRNOTAVAIL);
  790         laddr.s_addr = *laddrp;
  791         lport = *lportp;
  792         faddr = sin->sin_addr;
  793         fport = sin->sin_port;
  794 
  795         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
  796                 /*
  797                  * If the destination address is INADDR_ANY,
  798                  * use the primary local address.
  799                  * If the supplied address is INADDR_BROADCAST,
  800                  * and the primary interface supports broadcast,
  801                  * choose the broadcast address for that interface.
  802                  */
  803                 if (faddr.s_addr == INADDR_ANY) {
  804                         faddr =
  805                             IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
  806                         if (cred != NULL &&
  807                             (error = prison_get_ip4(cred, &faddr)) != 0)
  808                                 return (error);
  809                 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
  810                     (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
  811                     IFF_BROADCAST))
  812                         faddr = satosin(&TAILQ_FIRST(
  813                             &in_ifaddrhead)->ia_broadaddr)->sin_addr;
  814         }
  815         if (laddr.s_addr == INADDR_ANY) {
  816                 error = in_pcbladdr(inp, &faddr, &laddr, cred);
  817                 if (error)
  818                         return (error);
  819 
  820                 /*
  821                  * If the destination address is multicast and an outgoing
  822                  * interface has been set as a multicast option, use the
  823                  * address of that interface as our source address.
  824                  */
  825                 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
  826                     inp->inp_moptions != NULL) {
  827                         struct ip_moptions *imo;
  828                         struct ifnet *ifp;
  829 
  830                         imo = inp->inp_moptions;
  831                         if (imo->imo_multicast_ifp != NULL) {
  832                                 ifp = imo->imo_multicast_ifp;
  833                                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
  834                                         if (ia->ia_ifp == ifp)
  835                                                 break;
  836                                 if (ia == NULL)
  837                                         return (EADDRNOTAVAIL);
  838                                 laddr = ia->ia_addr.sin_addr;
  839                         }
  840                 }
  841         }
  842 
  843         oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
  844             0, NULL);
  845         if (oinp != NULL) {
  846                 if (oinpp != NULL)
  847                         *oinpp = oinp;
  848                 return (EADDRINUSE);
  849         }
  850         if (lport == 0) {
  851                 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
  852                     cred);
  853                 if (error)
  854                         return (error);
  855         }
  856         *laddrp = laddr.s_addr;
  857         *lportp = lport;
  858         *faddrp = faddr.s_addr;
  859         *fportp = fport;
  860         return (0);
  861 }
  862 
  863 void
  864 in_pcbdisconnect(struct inpcb *inp)
  865 {
  866 
  867         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  868         INP_WLOCK_ASSERT(inp);
  869 
  870         inp->inp_faddr.s_addr = INADDR_ANY;
  871         inp->inp_fport = 0;
  872         in_pcbrehash(inp);
  873 }
  874 
  875 /*
  876  * Historically, in_pcbdetach() included the functionality now found in
  877  * in_pcbfree() and in_pcbdrop().  They are now broken out to reflect the
  878  * more complex life cycle of TCP.
  879  *
  880  * in_pcbdetach() is responsibe for disconnecting the socket from an inpcb.
  881  * For most protocols, this will be invoked immediately prior to calling
  882  * in_pcbfree().  However, for TCP the inpcb may significantly outlive the
  883  * socket, in which case in_pcbfree() may be deferred.
  884  */
  885 void
  886 in_pcbdetach(struct inpcb *inp)
  887 {
  888 
  889         KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
  890 
  891         inp->inp_socket->so_pcb = NULL;
  892         inp->inp_socket = NULL;
  893 }
  894 
  895 /*
  896  * in_pcbfree() is responsible for freeing an already-detached inpcb, as well
  897  * as removing it from any global inpcb lists it might be on.
  898  */
  899 void
  900 in_pcbfree(struct inpcb *inp)
  901 {
  902         struct inpcbinfo *ipi = inp->inp_pcbinfo;
  903 
  904         KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
  905 
  906         INP_INFO_WLOCK_ASSERT(ipi);
  907         INP_WLOCK_ASSERT(inp);
  908 
  909 #ifdef IPSEC
  910         if (inp->inp_sp != NULL)
  911                 ipsec_delete_pcbpolicy(inp);
  912 #endif /* IPSEC */
  913         inp->inp_gencnt = ++ipi->ipi_gencnt;
  914         in_pcbremlists(inp);
  915 #ifdef INET6
  916         if (inp->inp_vflag & INP_IPV6PROTO) {
  917                 ip6_freepcbopts(inp->in6p_outputopts);
  918                 ip6_freemoptions(inp->in6p_moptions);
  919         }
  920 #endif
  921         if (inp->inp_options)
  922                 (void)m_free(inp->inp_options);
  923         if (inp->inp_moptions != NULL)
  924                 inp_freemoptions(inp->inp_moptions);
  925         inp->inp_vflag = 0;
  926         crfree(inp->inp_cred);
  927 
  928 #ifdef MAC
  929         mac_destroy_inpcb(inp);
  930 #endif
  931         INP_WUNLOCK(inp);
  932         uma_zfree(ipi->ipi_zone, inp);
  933 }
  934 
  935 /*
  936  * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
  937  * port reservation, and preventing it from being returned by inpcb lookups.
  938  *
  939  * It is used by TCP to mark an inpcb as unused and avoid future packet
  940  * delivery or event notification when a socket remains open but TCP has
  941  * closed.  This might occur as a result of a shutdown()-initiated TCP close
  942  * or a RST on the wire, and allows the port binding to be reused while still
  943  * maintaining the invariant that so_pcb always points to a valid inpcb until
  944  * in_pcbdetach().
  945  *
  946  * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
  947  * lists, but can lead to confusing netstat output, as open sockets with
  948  * closed TCP connections will no longer appear to have their bound port
  949  * number.  An explicit flag would be better, as it would allow us to leave
  950  * the port number intact after the connection is dropped.
  951  *
  952  * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
  953  * in_pcbnotifyall() and in_pcbpurgeif0()?
  954  */
  955 void
  956 in_pcbdrop(struct inpcb *inp)
  957 {
  958 
  959         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  960         INP_WLOCK_ASSERT(inp);
  961 
  962         inp->inp_flags |= INP_DROPPED;
  963         if (inp->inp_flags & INP_INHASHLIST) {
  964                 struct inpcbport *phd = inp->inp_phd;
  965 
  966                 LIST_REMOVE(inp, inp_hash);
  967                 LIST_REMOVE(inp, inp_portlist);
  968                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
  969                         LIST_REMOVE(phd, phd_hash);
  970                         free(phd, M_PCB);
  971                 }
  972                 inp->inp_flags &= ~INP_INHASHLIST;
  973         }
  974 }
  975 
  976 /*
  977  * Common routines to return the socket addresses associated with inpcbs.
  978  */
  979 struct sockaddr *
  980 in_sockaddr(in_port_t port, struct in_addr *addr_p)
  981 {
  982         struct sockaddr_in *sin;
  983 
  984         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
  985                 M_WAITOK | M_ZERO);
  986         sin->sin_family = AF_INET;
  987         sin->sin_len = sizeof(*sin);
  988         sin->sin_addr = *addr_p;
  989         sin->sin_port = port;
  990 
  991         return (struct sockaddr *)sin;
  992 }
  993 
  994 int
  995 in_getsockaddr(struct socket *so, struct sockaddr **nam)
  996 {
  997         struct inpcb *inp;
  998         struct in_addr addr;
  999         in_port_t port;
 1000 
 1001         inp = sotoinpcb(so);
 1002         KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
 1003 
 1004         INP_RLOCK(inp);
 1005         port = inp->inp_lport;
 1006         addr = inp->inp_laddr;
 1007         INP_RUNLOCK(inp);
 1008 
 1009         *nam = in_sockaddr(port, &addr);
 1010         return 0;
 1011 }
 1012 
 1013 int
 1014 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
 1015 {
 1016         struct inpcb *inp;
 1017         struct in_addr addr;
 1018         in_port_t port;
 1019 
 1020         inp = sotoinpcb(so);
 1021         KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
 1022 
 1023         INP_RLOCK(inp);
 1024         port = inp->inp_fport;
 1025         addr = inp->inp_faddr;
 1026         INP_RUNLOCK(inp);
 1027 
 1028         *nam = in_sockaddr(port, &addr);
 1029         return 0;
 1030 }
 1031 
 1032 void
 1033 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
 1034     struct inpcb *(*notify)(struct inpcb *, int))
 1035 {
 1036         struct inpcb *inp, *inp_temp;
 1037 
 1038         INP_INFO_WLOCK(pcbinfo);
 1039         LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
 1040                 INP_WLOCK(inp);
 1041 #ifdef INET6
 1042                 if ((inp->inp_vflag & INP_IPV4) == 0) {
 1043                         INP_WUNLOCK(inp);
 1044                         continue;
 1045                 }
 1046 #endif
 1047                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
 1048                     inp->inp_socket == NULL) {
 1049                         INP_WUNLOCK(inp);
 1050                         continue;
 1051                 }
 1052                 if ((*notify)(inp, errno))
 1053                         INP_WUNLOCK(inp);
 1054         }
 1055         INP_INFO_WUNLOCK(pcbinfo);
 1056 }
 1057 
 1058 void
 1059 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
 1060 {
 1061         struct inpcb *inp;
 1062         struct ip_moptions *imo;
 1063         int i, gap;
 1064 
 1065         INP_INFO_RLOCK(pcbinfo);
 1066         LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
 1067                 INP_WLOCK(inp);
 1068                 imo = inp->inp_moptions;
 1069                 if ((inp->inp_vflag & INP_IPV4) &&
 1070                     imo != NULL) {
 1071                         /*
 1072                          * Unselect the outgoing interface if it is being
 1073                          * detached.
 1074                          */
 1075                         if (imo->imo_multicast_ifp == ifp)
 1076                                 imo->imo_multicast_ifp = NULL;
 1077 
 1078                         /*
 1079                          * Drop multicast group membership if we joined
 1080                          * through the interface being detached.
 1081                          */
 1082                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
 1083                             i++) {
 1084                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
 1085                                         in_delmulti(imo->imo_membership[i]);
 1086                                         gap++;
 1087                                 } else if (gap != 0)
 1088                                         imo->imo_membership[i - gap] =
 1089                                             imo->imo_membership[i];
 1090                         }
 1091                         imo->imo_num_memberships -= gap;
 1092                 }
 1093                 INP_WUNLOCK(inp);
 1094         }
 1095         INP_INFO_RUNLOCK(pcbinfo);
 1096 }
 1097 
 1098 /*
 1099  * Lookup a PCB based on the local address and port.
 1100  */
 1101 #define INP_LOOKUP_MAPPED_PCB_COST      3
 1102 struct inpcb *
 1103 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
 1104     u_short lport, int wild_okay, struct ucred *cred)
 1105 {
 1106         struct inpcb *inp;
 1107 #ifdef INET6
 1108         int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
 1109 #else
 1110         int matchwild = 3;
 1111 #endif
 1112         int wildcard;
 1113 
 1114         INP_INFO_LOCK_ASSERT(pcbinfo);
 1115 
 1116         if (!wild_okay) {
 1117                 struct inpcbhead *head;
 1118                 /*
 1119                  * Look for an unconnected (wildcard foreign addr) PCB that
 1120                  * matches the local address and port we're looking for.
 1121                  */
 1122                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
 1123                     0, pcbinfo->ipi_hashmask)];
 1124                 LIST_FOREACH(inp, head, inp_hash) {
 1125 #ifdef INET6
 1126                         /* XXX inp locking */
 1127                         if ((inp->inp_vflag & INP_IPV4) == 0)
 1128                                 continue;
 1129 #endif
 1130                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
 1131                             inp->inp_laddr.s_addr == laddr.s_addr &&
 1132                             inp->inp_lport == lport) {
 1133                                 /*
 1134                                  * Found?
 1135                                  */
 1136                                 if (cred == NULL ||
 1137                                     inp->inp_cred->cr_prison == cred->cr_prison)
 1138                                         return (inp);
 1139                         }
 1140                 }
 1141                 /*
 1142                  * Not found.
 1143                  */
 1144                 return (NULL);
 1145         } else {
 1146                 struct inpcbporthead *porthash;
 1147                 struct inpcbport *phd;
 1148                 struct inpcb *match = NULL;
 1149                 /*
 1150                  * Best fit PCB lookup.
 1151                  *
 1152                  * First see if this local port is in use by looking on the
 1153                  * port hash list.
 1154                  */
 1155                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
 1156                     pcbinfo->ipi_porthashmask)];
 1157                 LIST_FOREACH(phd, porthash, phd_hash) {
 1158                         if (phd->phd_port == lport)
 1159                                 break;
 1160                 }
 1161                 if (phd != NULL) {
 1162                         /*
 1163                          * Port is in use by one or more PCBs. Look for best
 1164                          * fit.
 1165                          */
 1166                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
 1167                                 wildcard = 0;
 1168                                 if (cred != NULL &&
 1169                                     inp->inp_cred->cr_prison != cred->cr_prison)
 1170                                         continue;
 1171 #ifdef INET6
 1172                                 /* XXX inp locking */
 1173                                 if ((inp->inp_vflag & INP_IPV4) == 0)
 1174                                         continue;
 1175                                 /*
 1176                                  * We never select the PCB that has
 1177                                  * INP_IPV6 flag and is bound to :: if
 1178                                  * we have another PCB which is bound
 1179                                  * to 0.0.0.0.  If a PCB has the
 1180                                  * INP_IPV6 flag, then we set its cost
 1181                                  * higher than IPv4 only PCBs.
 1182                                  *
 1183                                  * Note that the case only happens
 1184                                  * when a socket is bound to ::, under
 1185                                  * the condition that the use of the
 1186                                  * mapped address is allowed.
 1187                                  */
 1188                                 if ((inp->inp_vflag & INP_IPV6) != 0)
 1189                                         wildcard += INP_LOOKUP_MAPPED_PCB_COST;
 1190 #endif
 1191                                 if (inp->inp_faddr.s_addr != INADDR_ANY)
 1192                                         wildcard++;
 1193                                 if (inp->inp_laddr.s_addr != INADDR_ANY) {
 1194                                         if (laddr.s_addr == INADDR_ANY)
 1195                                                 wildcard++;
 1196                                         else if (inp->inp_laddr.s_addr != laddr.s_addr)
 1197                                                 continue;
 1198                                 } else {
 1199                                         if (laddr.s_addr != INADDR_ANY)
 1200                                                 wildcard++;
 1201                                 }
 1202                                 if (wildcard < matchwild) {
 1203                                         match = inp;
 1204                                         matchwild = wildcard;
 1205                                         if (matchwild == 0)
 1206                                                 break;
 1207                                 }
 1208                         }
 1209                 }
 1210                 return (match);
 1211         }
 1212 }
 1213 #undef INP_LOOKUP_MAPPED_PCB_COST
 1214 
 1215 /*
 1216  * Lookup PCB in hash list.
 1217  */
 1218 struct inpcb *
 1219 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
 1220     u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
 1221     struct ifnet *ifp)
 1222 {
 1223         struct inpcbhead *head;
 1224         struct inpcb *inp, *tmpinp;
 1225         u_short fport = fport_arg, lport = lport_arg;
 1226 
 1227         INP_INFO_LOCK_ASSERT(pcbinfo);
 1228 
 1229         /*
 1230          * First look for an exact match.
 1231          */
 1232         tmpinp = NULL;
 1233         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
 1234             pcbinfo->ipi_hashmask)];
 1235         LIST_FOREACH(inp, head, inp_hash) {
 1236 #ifdef INET6
 1237                 /* XXX inp locking */
 1238                 if ((inp->inp_vflag & INP_IPV4) == 0)
 1239                         continue;
 1240 #endif
 1241                 if (inp->inp_faddr.s_addr == faddr.s_addr &&
 1242                     inp->inp_laddr.s_addr == laddr.s_addr &&
 1243                     inp->inp_fport == fport &&
 1244                     inp->inp_lport == lport) {
 1245                         /*
 1246                          * XXX We should be able to directly return
 1247                          * the inp here, without any checks.
 1248                          * Well unless both bound with SO_REUSEPORT?
 1249                          */
 1250                         if (jailed(inp->inp_cred))
 1251                                 return (inp);
 1252                         if (tmpinp == NULL)
 1253                                 tmpinp = inp;
 1254                 }
 1255         }
 1256         if (tmpinp != NULL)
 1257                 return (tmpinp);
 1258 
 1259         /*
 1260          * Then look for a wildcard match, if requested.
 1261          */
 1262         if (wildcard == INPLOOKUP_WILDCARD) {
 1263                 struct inpcb *local_wild = NULL, *local_exact = NULL;
 1264 #ifdef INET6
 1265                 struct inpcb *local_wild_mapped = NULL;
 1266 #endif
 1267                 struct inpcb *jail_wild = NULL;
 1268                 int injail;
 1269 
 1270                 /*
 1271                  * Order of socket selection - we always prefer jails.
 1272                  *      1. jailed, non-wild.
 1273                  *      2. jailed, wild.
 1274                  *      3. non-jailed, non-wild.
 1275                  *      4. non-jailed, wild.
 1276                  */
 1277 
 1278                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
 1279                     0, pcbinfo->ipi_hashmask)];
 1280                 LIST_FOREACH(inp, head, inp_hash) {
 1281 #ifdef INET6
 1282                         /* XXX inp locking */
 1283                         if ((inp->inp_vflag & INP_IPV4) == 0)
 1284                                 continue;
 1285 #endif
 1286                         if (inp->inp_faddr.s_addr != INADDR_ANY ||
 1287                             inp->inp_lport != lport)
 1288                                 continue;
 1289 
 1290                         /* XXX inp locking */
 1291                         if (ifp && ifp->if_type == IFT_FAITH &&
 1292                             (inp->inp_flags & INP_FAITH) == 0)
 1293                                 continue;
 1294 
 1295                         injail = jailed(inp->inp_cred);
 1296                         if (injail) {
 1297                                 if (prison_check_ip4(inp->inp_cred,
 1298                                     &laddr) != 0)
 1299                                         continue;
 1300                         } else {
 1301                                 if (local_exact != NULL)
 1302                                         continue;
 1303                         }
 1304 
 1305                         if (inp->inp_laddr.s_addr == laddr.s_addr) {
 1306                                 if (injail)
 1307                                         return (inp);
 1308                                 else
 1309                                         local_exact = inp;
 1310                         } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
 1311 #ifdef INET6
 1312                                 /* XXX inp locking, NULL check */
 1313                                 if (inp->inp_vflag & INP_IPV6PROTO)
 1314                                         local_wild_mapped = inp;
 1315                                 else
 1316 #endif /* INET6 */
 1317                                         if (injail)
 1318                                                 jail_wild = inp;
 1319                                         else
 1320                                                 local_wild = inp;
 1321                         }
 1322                 } /* LIST_FOREACH */
 1323                 if (jail_wild != NULL)
 1324                         return (jail_wild);
 1325                 if (local_exact != NULL)
 1326                         return (local_exact);
 1327                 if (local_wild != NULL)
 1328                         return (local_wild);
 1329 #ifdef INET6
 1330                 if (local_wild_mapped != NULL)
 1331                         return (local_wild_mapped);
 1332 #endif /* defined(INET6) */
 1333         } /* if (wildcard == INPLOOKUP_WILDCARD) */
 1334 
 1335         return (NULL);
 1336 }
 1337 
 1338 /*
 1339  * Insert PCB onto various hash lists.
 1340  */
 1341 int
 1342 in_pcbinshash(struct inpcb *inp)
 1343 {
 1344         struct inpcbhead *pcbhash;
 1345         struct inpcbporthead *pcbporthash;
 1346         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
 1347         struct inpcbport *phd;
 1348         u_int32_t hashkey_faddr;
 1349 
 1350         INP_INFO_WLOCK_ASSERT(pcbinfo);
 1351         INP_WLOCK_ASSERT(inp);
 1352         KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
 1353             ("in_pcbinshash: INP_INHASHLIST"));
 1354 
 1355 #ifdef INET6
 1356         if (inp->inp_vflag & INP_IPV6)
 1357                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
 1358         else
 1359 #endif /* INET6 */
 1360         hashkey_faddr = inp->inp_faddr.s_addr;
 1361 
 1362         pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
 1363                  inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
 1364 
 1365         pcbporthash = &pcbinfo->ipi_porthashbase[
 1366             INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
 1367 
 1368         /*
 1369          * Go through port list and look for a head for this lport.
 1370          */
 1371         LIST_FOREACH(phd, pcbporthash, phd_hash) {
 1372                 if (phd->phd_port == inp->inp_lport)
 1373                         break;
 1374         }
 1375         /*
 1376          * If none exists, malloc one and tack it on.
 1377          */
 1378         if (phd == NULL) {
 1379                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
 1380                 if (phd == NULL) {
 1381                         return (ENOBUFS); /* XXX */
 1382                 }
 1383                 phd->phd_port = inp->inp_lport;
 1384                 LIST_INIT(&phd->phd_pcblist);
 1385                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
 1386         }
 1387         inp->inp_phd = phd;
 1388         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
 1389         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
 1390         inp->inp_flags |= INP_INHASHLIST;
 1391         return (0);
 1392 }
 1393 
 1394 /*
 1395  * Move PCB to the proper hash bucket when { faddr, fport } have  been
 1396  * changed. NOTE: This does not handle the case of the lport changing (the
 1397  * hashed port list would have to be updated as well), so the lport must
 1398  * not change after in_pcbinshash() has been called.
 1399  */
 1400 void
 1401 in_pcbrehash(struct inpcb *inp)
 1402 {
 1403         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
 1404         struct inpcbhead *head;
 1405         u_int32_t hashkey_faddr;
 1406 
 1407         INP_INFO_WLOCK_ASSERT(pcbinfo);
 1408         INP_WLOCK_ASSERT(inp);
 1409         KASSERT(inp->inp_flags & INP_INHASHLIST,
 1410             ("in_pcbrehash: !INP_INHASHLIST"));
 1411 
 1412 #ifdef INET6
 1413         if (inp->inp_vflag & INP_IPV6)
 1414                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
 1415         else
 1416 #endif /* INET6 */
 1417         hashkey_faddr = inp->inp_faddr.s_addr;
 1418 
 1419         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
 1420                 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
 1421 
 1422         LIST_REMOVE(inp, inp_hash);
 1423         LIST_INSERT_HEAD(head, inp, inp_hash);
 1424 }
 1425 
 1426 /*
 1427  * Remove PCB from various lists.
 1428  */
 1429 void
 1430 in_pcbremlists(struct inpcb *inp)
 1431 {
 1432         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
 1433 
 1434         INP_INFO_WLOCK_ASSERT(pcbinfo);
 1435         INP_WLOCK_ASSERT(inp);
 1436 
 1437         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
 1438         if (inp->inp_flags & INP_INHASHLIST) {
 1439                 struct inpcbport *phd = inp->inp_phd;
 1440 
 1441                 LIST_REMOVE(inp, inp_hash);
 1442                 LIST_REMOVE(inp, inp_portlist);
 1443                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
 1444                         LIST_REMOVE(phd, phd_hash);
 1445                         free(phd, M_PCB);
 1446                 }
 1447                 inp->inp_flags &= ~INP_INHASHLIST;
 1448         }
 1449         LIST_REMOVE(inp, inp_list);
 1450         pcbinfo->ipi_count--;
 1451 }
 1452 
 1453 /*
 1454  * A set label operation has occurred at the socket layer, propagate the
 1455  * label change into the in_pcb for the socket.
 1456  */
 1457 void
 1458 in_pcbsosetlabel(struct socket *so)
 1459 {
 1460 #ifdef MAC
 1461         struct inpcb *inp;
 1462 
 1463         inp = sotoinpcb(so);
 1464         KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
 1465 
 1466         INP_WLOCK(inp);
 1467         SOCK_LOCK(so);
 1468         mac_inpcb_sosetlabel(so, inp);
 1469         SOCK_UNLOCK(so);
 1470         INP_WUNLOCK(inp);
 1471 #endif
 1472 }
 1473 
 1474 /*
 1475  * ipport_tick runs once per second, determining if random port allocation
 1476  * should be continued.  If more than ipport_randomcps ports have been
 1477  * allocated in the last second, then we return to sequential port
 1478  * allocation. We return to random allocation only once we drop below
 1479  * ipport_randomcps for at least ipport_randomtime seconds.
 1480  */
 1481 void
 1482 ipport_tick(void *xtp)
 1483 {
 1484 
 1485         if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
 1486                 if (ipport_stoprandom > 0)
 1487                         ipport_stoprandom--;
 1488         } else
 1489                 ipport_stoprandom = ipport_randomtime;
 1490         ipport_tcplastcount = ipport_tcpallocs;
 1491         callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
 1492 }
 1493 
 1494 void
 1495 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
 1496 {
 1497         struct inpcb *inp;
 1498 
 1499         INP_INFO_RLOCK(&tcbinfo);
 1500         LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) {
 1501                 INP_WLOCK(inp);
 1502                 func(inp, arg);
 1503                 INP_WUNLOCK(inp);
 1504         }
 1505         INP_INFO_RUNLOCK(&tcbinfo);
 1506 }
 1507 
 1508 struct socket *
 1509 inp_inpcbtosocket(struct inpcb *inp)
 1510 {
 1511 
 1512         INP_WLOCK_ASSERT(inp);
 1513         return (inp->inp_socket);
 1514 }
 1515 
 1516 struct tcpcb *
 1517 inp_inpcbtotcpcb(struct inpcb *inp)
 1518 {
 1519 
 1520         INP_WLOCK_ASSERT(inp);
 1521         return ((struct tcpcb *)inp->inp_ppcb);
 1522 }
 1523 
 1524 int
 1525 inp_ip_tos_get(const struct inpcb *inp)
 1526 {
 1527 
 1528         return (inp->inp_ip_tos);
 1529 }
 1530 
 1531 void
 1532 inp_ip_tos_set(struct inpcb *inp, int val)
 1533 {
 1534 
 1535         inp->inp_ip_tos = val;
 1536 }
 1537 
 1538 void
 1539 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
 1540     uint32_t *faddr, uint16_t *fp)
 1541 {
 1542 
 1543         INP_LOCK_ASSERT(inp);
 1544         *laddr = inp->inp_laddr.s_addr;
 1545         *faddr = inp->inp_faddr.s_addr;
 1546         *lp = inp->inp_lport;
 1547         *fp = inp->inp_fport;
 1548 }
 1549 
 1550 struct inpcb *
 1551 so_sotoinpcb(struct socket *so)
 1552 {
 1553 
 1554         return (sotoinpcb(so));
 1555 }
 1556 
 1557 struct tcpcb *
 1558 so_sototcpcb(struct socket *so)
 1559 {
 1560 
 1561         return (sototcpcb(so));
 1562 }
 1563 
 1564 void
 1565 inp_wlock(struct inpcb *inp)
 1566 {
 1567 
 1568         INP_WLOCK(inp);
 1569 }
 1570 
 1571 void
 1572 inp_wunlock(struct inpcb *inp)
 1573 {
 1574 
 1575         INP_WUNLOCK(inp);
 1576 }
 1577 
 1578 void
 1579 inp_rlock(struct inpcb *inp)
 1580 {
 1581 
 1582         INP_RLOCK(inp);
 1583 }
 1584 
 1585 void
 1586 inp_runlock(struct inpcb *inp)
 1587 {
 1588 
 1589         INP_RUNLOCK(inp);
 1590 }
 1591 
 1592 #ifdef INVARIANTS
 1593 void
 1594 inp_wlock_assert(struct inpcb *inp)
 1595 {
 1596 
 1597         INP_WLOCK_ASSERT(inp);
 1598 }
 1599 
 1600 void
 1601 inp_rlock_assert(struct inpcb *inp)
 1602 {
 1603 
 1604         INP_RLOCK_ASSERT(inp);
 1605 }
 1606 
 1607 void
 1608 inp_lock_assert(struct inpcb *inp)
 1609 {
 1610 
 1611         INP_LOCK_ASSERT(inp);
 1612 }
 1613 
 1614 void
 1615 inp_unlock_assert(struct inpcb *inp)
 1616 {
 1617 
 1618         INP_UNLOCK_ASSERT(inp);
 1619 }
 1620 
 1621 #endif
 1622 
 1623 #ifdef DDB
 1624 static void
 1625 db_print_indent(int indent)
 1626 {
 1627         int i;
 1628 
 1629         for (i = 0; i < indent; i++)
 1630                 db_printf(" ");
 1631 }
 1632 
 1633 static void
 1634 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
 1635 {
 1636         char faddr_str[48], laddr_str[48];
 1637 
 1638         db_print_indent(indent);
 1639         db_printf("%s at %p\n", name, inc);
 1640 
 1641         indent += 2;
 1642 
 1643 #ifdef INET6
 1644         if (inc->inc_flags & INC_ISIPV6) {
 1645                 /* IPv6. */
 1646                 ip6_sprintf(laddr_str, &inc->inc6_laddr);
 1647                 ip6_sprintf(faddr_str, &inc->inc6_faddr);
 1648         } else {
 1649 #endif
 1650                 /* IPv4. */
 1651                 inet_ntoa_r(inc->inc_laddr, laddr_str);
 1652                 inet_ntoa_r(inc->inc_faddr, faddr_str);
 1653 #ifdef INET6
 1654         }
 1655 #endif
 1656         db_print_indent(indent);
 1657         db_printf("inc_laddr %s   inc_lport %u\n", laddr_str,
 1658             ntohs(inc->inc_lport));
 1659         db_print_indent(indent);
 1660         db_printf("inc_faddr %s   inc_fport %u\n", faddr_str,
 1661             ntohs(inc->inc_fport));
 1662 }
 1663 
 1664 static void
 1665 db_print_inpflags(int inp_flags)
 1666 {
 1667         int comma;
 1668 
 1669         comma = 0;
 1670         if (inp_flags & INP_RECVOPTS) {
 1671                 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
 1672                 comma = 1;
 1673         }
 1674         if (inp_flags & INP_RECVRETOPTS) {
 1675                 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
 1676                 comma = 1;
 1677         }
 1678         if (inp_flags & INP_RECVDSTADDR) {
 1679                 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
 1680                 comma = 1;
 1681         }
 1682         if (inp_flags & INP_HDRINCL) {
 1683                 db_printf("%sINP_HDRINCL", comma ? ", " : "");
 1684                 comma = 1;
 1685         }
 1686         if (inp_flags & INP_HIGHPORT) {
 1687                 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
 1688                 comma = 1;
 1689         }
 1690         if (inp_flags & INP_LOWPORT) {
 1691                 db_printf("%sINP_LOWPORT", comma ? ", " : "");
 1692                 comma = 1;
 1693         }
 1694         if (inp_flags & INP_ANONPORT) {
 1695                 db_printf("%sINP_ANONPORT", comma ? ", " : "");
 1696                 comma = 1;
 1697         }
 1698         if (inp_flags & INP_RECVIF) {
 1699                 db_printf("%sINP_RECVIF", comma ? ", " : "");
 1700                 comma = 1;
 1701         }
 1702         if (inp_flags & INP_MTUDISC) {
 1703                 db_printf("%sINP_MTUDISC", comma ? ", " : "");
 1704                 comma = 1;
 1705         }
 1706         if (inp_flags & INP_FAITH) {
 1707                 db_printf("%sINP_FAITH", comma ? ", " : "");
 1708                 comma = 1;
 1709         }
 1710         if (inp_flags & INP_RECVTTL) {
 1711                 db_printf("%sINP_RECVTTL", comma ? ", " : "");
 1712                 comma = 1;
 1713         }
 1714         if (inp_flags & INP_DONTFRAG) {
 1715                 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
 1716                 comma = 1;
 1717         }
 1718         if (inp_flags & IN6P_IPV6_V6ONLY) {
 1719                 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
 1720                 comma = 1;
 1721         }
 1722         if (inp_flags & IN6P_PKTINFO) {
 1723                 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
 1724                 comma = 1;
 1725         }
 1726         if (inp_flags & IN6P_HOPLIMIT) {
 1727                 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
 1728                 comma = 1;
 1729         }
 1730         if (inp_flags & IN6P_HOPOPTS) {
 1731                 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
 1732                 comma = 1;
 1733         }
 1734         if (inp_flags & IN6P_DSTOPTS) {
 1735                 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
 1736                 comma = 1;
 1737         }
 1738         if (inp_flags & IN6P_RTHDR) {
 1739                 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
 1740                 comma = 1;
 1741         }
 1742         if (inp_flags & IN6P_RTHDRDSTOPTS) {
 1743                 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
 1744                 comma = 1;
 1745         }
 1746         if (inp_flags & IN6P_TCLASS) {
 1747                 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
 1748                 comma = 1;
 1749         }
 1750         if (inp_flags & IN6P_AUTOFLOWLABEL) {
 1751                 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
 1752                 comma = 1;
 1753         }
 1754         if (inp_flags & INP_TIMEWAIT) {
 1755                 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
 1756                 comma  = 1;
 1757         }
 1758         if (inp_flags & INP_ONESBCAST) {
 1759                 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
 1760                 comma  = 1;
 1761         }
 1762         if (inp_flags & INP_DROPPED) {
 1763                 db_printf("%sINP_DROPPED", comma ? ", " : "");
 1764                 comma  = 1;
 1765         }
 1766         if (inp_flags & INP_SOCKREF) {
 1767                 db_printf("%sINP_SOCKREF", comma ? ", " : "");
 1768                 comma  = 1;
 1769         }
 1770         if (inp_flags & IN6P_RFC2292) {
 1771                 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
 1772                 comma = 1;
 1773         }
 1774         if (inp_flags & IN6P_MTU) {
 1775                 db_printf("IN6P_MTU%s", comma ? ", " : "");
 1776                 comma = 1;
 1777         }
 1778 }
 1779 
 1780 static void
 1781 db_print_inpvflag(u_char inp_vflag)
 1782 {
 1783         int comma;
 1784 
 1785         comma = 0;
 1786         if (inp_vflag & INP_IPV4) {
 1787                 db_printf("%sINP_IPV4", comma ? ", " : "");
 1788                 comma  = 1;
 1789         }
 1790         if (inp_vflag & INP_IPV6) {
 1791                 db_printf("%sINP_IPV6", comma ? ", " : "");
 1792                 comma  = 1;
 1793         }
 1794         if (inp_vflag & INP_IPV6PROTO) {
 1795                 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
 1796                 comma  = 1;
 1797         }
 1798 }
 1799 
 1800 void
 1801 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
 1802 {
 1803 
 1804         db_print_indent(indent);
 1805         db_printf("%s at %p\n", name, inp);
 1806 
 1807         indent += 2;
 1808 
 1809         db_print_indent(indent);
 1810         db_printf("inp_flow: 0x%x\n", inp->inp_flow);
 1811 
 1812         db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
 1813 
 1814         db_print_indent(indent);
 1815         db_printf("inp_ppcb: %p   inp_pcbinfo: %p   inp_socket: %p\n",
 1816             inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
 1817 
 1818         db_print_indent(indent);
 1819         db_printf("inp_label: %p   inp_flags: 0x%x (",
 1820            inp->inp_label, inp->inp_flags);
 1821         db_print_inpflags(inp->inp_flags);
 1822         db_printf(")\n");
 1823 
 1824         db_print_indent(indent);
 1825         db_printf("inp_sp: %p   inp_vflag: 0x%x (", inp->inp_sp,
 1826             inp->inp_vflag);
 1827         db_print_inpvflag(inp->inp_vflag);
 1828         db_printf(")\n");
 1829 
 1830         db_print_indent(indent);
 1831         db_printf("inp_ip_ttl: %d   inp_ip_p: %d   inp_ip_minttl: %d\n",
 1832             inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
 1833 
 1834         db_print_indent(indent);
 1835 #ifdef INET6
 1836         if (inp->inp_vflag & INP_IPV6) {
 1837                 db_printf("in6p_options: %p   in6p_outputopts: %p   "
 1838                     "in6p_moptions: %p\n", inp->in6p_options,
 1839                     inp->in6p_outputopts, inp->in6p_moptions);
 1840                 db_printf("in6p_icmp6filt: %p   in6p_cksum %d   "
 1841                     "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
 1842                     inp->in6p_hops);
 1843         } else
 1844 #endif
 1845         {
 1846                 db_printf("inp_ip_tos: %d   inp_ip_options: %p   "
 1847                     "inp_ip_moptions: %p\n", inp->inp_ip_tos,
 1848                     inp->inp_options, inp->inp_moptions);
 1849         }
 1850 
 1851         db_print_indent(indent);
 1852         db_printf("inp_phd: %p   inp_gencnt: %ju\n", inp->inp_phd,
 1853             (uintmax_t)inp->inp_gencnt);
 1854 }
 1855 
 1856 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
 1857 {
 1858         struct inpcb *inp;
 1859 
 1860         if (!have_addr) {
 1861                 db_printf("usage: show inpcb <addr>\n");
 1862                 return;
 1863         }
 1864         inp = (struct inpcb *)addr;
 1865 
 1866         db_print_inpcb(inp, "inpcb", 0);
 1867 }
 1868 #endif

Cache object: 205c3055abd0b1532ce677487ce1ef95


[ 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.