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

Cache object: 9d6df5245d56ff740fe85e11411dee91


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