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/netinet6/in6_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) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * Copyright (c) 2010-2011 Juniper Networks, Inc.
    4  * All rights reserved.
    5  *
    6  * Portions of this software were developed by Robert N. M. Watson under
    7  * contract to Juniper Networks, Inc.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. Neither the name of the project nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
   34  */
   35 
   36 /*-
   37  * Copyright (c) 1982, 1986, 1991, 1993
   38  *      The Regents of the University of California.  All rights reserved.
   39  *
   40  * Redistribution and use in source and binary forms, with or without
   41  * modification, are permitted provided that the following conditions
   42  * are met:
   43  * 1. Redistributions of source code must retain the above copyright
   44  *    notice, this list of conditions and the following disclaimer.
   45  * 2. Redistributions in binary form must reproduce the above copyright
   46  *    notice, this list of conditions and the following disclaimer in the
   47  *    documentation and/or other materials provided with the distribution.
   48  * 4. Neither the name of the University nor the names of its contributors
   49  *    may be used to endorse or promote products derived from this software
   50  *    without specific prior written permission.
   51  *
   52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   62  * SUCH DAMAGE.
   63  *
   64  *      @(#)in_pcb.c    8.2 (Berkeley) 1/4/94
   65  */
   66 
   67 #include <sys/cdefs.h>
   68 __FBSDID("$FreeBSD$");
   69 
   70 #include "opt_inet.h"
   71 #include "opt_inet6.h"
   72 #include "opt_ipsec.h"
   73 #include "opt_pcbgroup.h"
   74 #include "opt_rss.h"
   75 
   76 #include <sys/param.h>
   77 #include <sys/systm.h>
   78 #include <sys/malloc.h>
   79 #include <sys/mbuf.h>
   80 #include <sys/domain.h>
   81 #include <sys/protosw.h>
   82 #include <sys/socket.h>
   83 #include <sys/socketvar.h>
   84 #include <sys/sockio.h>
   85 #include <sys/errno.h>
   86 #include <sys/time.h>
   87 #include <sys/priv.h>
   88 #include <sys/proc.h>
   89 #include <sys/jail.h>
   90 
   91 #include <vm/uma.h>
   92 
   93 #include <net/if.h>
   94 #include <net/if_var.h>
   95 #include <net/if_llatbl.h>
   96 #include <net/if_types.h>
   97 #include <net/route.h>
   98 
   99 #include <netinet/in.h>
  100 #include <netinet/in_var.h>
  101 #include <netinet/in_systm.h>
  102 #include <netinet/tcp_var.h>
  103 #include <netinet/ip6.h>
  104 #include <netinet/ip_var.h>
  105 
  106 #include <netinet6/ip6_var.h>
  107 #include <netinet6/nd6.h>
  108 #include <netinet/in_pcb.h>
  109 #include <netinet6/in6_pcb.h>
  110 #include <netinet6/scope6_var.h>
  111 
  112 static struct inpcb *in6_pcblookup_hash_locked(struct inpcbinfo *,
  113     struct in6_addr *, u_int, struct in6_addr *, u_int, int, struct ifnet *);
  114 
  115 int
  116 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
  117     struct ucred *cred)
  118 {
  119         struct socket *so = inp->inp_socket;
  120         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
  121         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
  122         u_short lport = 0;
  123         int error, lookupflags = 0;
  124         int reuseport = (so->so_options & SO_REUSEPORT);
  125 
  126         INP_WLOCK_ASSERT(inp);
  127         INP_HASH_WLOCK_ASSERT(pcbinfo);
  128 
  129         if (TAILQ_EMPTY(&V_in6_ifaddrhead))     /* XXX broken! */
  130                 return (EADDRNOTAVAIL);
  131         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
  132                 return (EINVAL);
  133         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
  134                 lookupflags = INPLOOKUP_WILDCARD;
  135         if (nam == NULL) {
  136                 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
  137                     ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
  138                         return (error);
  139         } else {
  140                 sin6 = (struct sockaddr_in6 *)nam;
  141                 if (nam->sa_len != sizeof(*sin6))
  142                         return (EINVAL);
  143                 /*
  144                  * family check.
  145                  */
  146                 if (nam->sa_family != AF_INET6)
  147                         return (EAFNOSUPPORT);
  148 
  149                 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
  150                         return(error);
  151 
  152                 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
  153                     ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
  154                         return (error);
  155 
  156                 lport = sin6->sin6_port;
  157                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
  158                         /*
  159                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
  160                          * allow compepte duplication of binding if
  161                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
  162                          * and a multicast address is bound on both
  163                          * new and duplicated sockets.
  164                          */
  165                         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
  166                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
  167                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  168                         struct ifaddr *ifa;
  169 
  170                         sin6->sin6_port = 0;            /* yech... */
  171                         if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
  172                             NULL &&
  173                             (inp->inp_flags & INP_BINDANY) == 0) {
  174                                 return (EADDRNOTAVAIL);
  175                         }
  176 
  177                         /*
  178                          * XXX: bind to an anycast address might accidentally
  179                          * cause sending a packet with anycast source address.
  180                          * We should allow to bind to a deprecated address, since
  181                          * the application dares to use it.
  182                          */
  183                         if (ifa != NULL &&
  184                             ((struct in6_ifaddr *)ifa)->ia6_flags &
  185                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
  186                                 ifa_free(ifa);
  187                                 return (EADDRNOTAVAIL);
  188                         }
  189                         if (ifa != NULL)
  190                                 ifa_free(ifa);
  191                 }
  192                 if (lport) {
  193                         struct inpcb *t;
  194                         struct tcptw *tw;
  195 
  196                         /* GROSS */
  197                         if (ntohs(lport) <= V_ipport_reservedhigh &&
  198                             ntohs(lport) >= V_ipport_reservedlow &&
  199                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
  200                             0))
  201                                 return (EACCES);
  202                         if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
  203                             priv_check_cred(inp->inp_cred,
  204                             PRIV_NETINET_REUSEPORT, 0) != 0) {
  205                                 t = in6_pcblookup_local(pcbinfo,
  206                                     &sin6->sin6_addr, lport,
  207                                     INPLOOKUP_WILDCARD, cred);
  208                                 if (t &&
  209                                     ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
  210                                     ((t->inp_flags & INP_TIMEWAIT) == 0) &&
  211                                     (so->so_type != SOCK_STREAM ||
  212                                      IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
  213                                     (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
  214                                      !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
  215                                      (t->inp_flags2 & INP_REUSEPORT) == 0) &&
  216                                     (inp->inp_cred->cr_uid !=
  217                                      t->inp_cred->cr_uid))
  218                                         return (EADDRINUSE);
  219 
  220                                 /*
  221                                  * If the socket is a BINDMULTI socket, then
  222                                  * the credentials need to match and the
  223                                  * original socket also has to have been bound
  224                                  * with BINDMULTI.
  225                                  */
  226                                 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
  227                                         return (EADDRINUSE);
  228 
  229 #ifdef INET
  230                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
  231                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  232                                         struct sockaddr_in sin;
  233 
  234                                         in6_sin6_2_sin(&sin, sin6);
  235                                         t = in_pcblookup_local(pcbinfo,
  236                                             sin.sin_addr, lport,
  237                                             INPLOOKUP_WILDCARD, cred);
  238                                         if (t &&
  239                                             ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
  240                                             ((t->inp_flags &
  241                                               INP_TIMEWAIT) == 0) &&
  242                                             (so->so_type != SOCK_STREAM ||
  243                                              ntohl(t->inp_faddr.s_addr) ==
  244                                               INADDR_ANY) &&
  245                                             (inp->inp_cred->cr_uid !=
  246                                              t->inp_cred->cr_uid))
  247                                                 return (EADDRINUSE);
  248 
  249                                         if (t && (! in_pcbbind_check_bindmulti(inp, t)))
  250                                                 return (EADDRINUSE);
  251                                 }
  252 #endif
  253                         }
  254                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
  255                             lport, lookupflags, cred);
  256                         if (t && (t->inp_flags & INP_TIMEWAIT)) {
  257                                 /*
  258                                  * XXXRW: If an incpb has had its timewait
  259                                  * state recycled, we treat the address as
  260                                  * being in use (for now).  This is better
  261                                  * than a panic, but not desirable.
  262                                  */
  263                                 tw = intotw(t);
  264                                 if (tw == NULL ||
  265                                     (reuseport & tw->tw_so_options) == 0)
  266                                         return (EADDRINUSE);
  267                         } else if (t && (reuseport & inp_so_options(t)) == 0) {
  268                                 return (EADDRINUSE);
  269                         }
  270 #ifdef INET
  271                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
  272                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  273                                 struct sockaddr_in sin;
  274 
  275                                 in6_sin6_2_sin(&sin, sin6);
  276                                 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
  277                                     lport, lookupflags, cred);
  278                                 if (t && t->inp_flags & INP_TIMEWAIT) {
  279                                         tw = intotw(t);
  280                                         if (tw == NULL)
  281                                                 return (EADDRINUSE);
  282                                         if ((reuseport & tw->tw_so_options) == 0
  283                                             && (ntohl(t->inp_laddr.s_addr) !=
  284                                              INADDR_ANY || ((inp->inp_vflag &
  285                                              INP_IPV6PROTO) ==
  286                                              (t->inp_vflag & INP_IPV6PROTO))))
  287                                                 return (EADDRINUSE);
  288                                 } else if (t &&
  289                                     (reuseport & inp_so_options(t)) == 0 &&
  290                                     (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
  291                                     (t->inp_vflag & INP_IPV6PROTO) != 0))
  292                                         return (EADDRINUSE);
  293                         }
  294 #endif
  295                 }
  296                 inp->in6p_laddr = sin6->sin6_addr;
  297         }
  298         if (lport == 0) {
  299                 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
  300                         /* Undo an address bind that may have occurred. */
  301                         inp->in6p_laddr = in6addr_any;
  302                         return (error);
  303                 }
  304         } else {
  305                 inp->inp_lport = lport;
  306                 if (in_pcbinshash(inp) != 0) {
  307                         inp->in6p_laddr = in6addr_any;
  308                         inp->inp_lport = 0;
  309                         return (EAGAIN);
  310                 }
  311         }
  312         return (0);
  313 }
  314 
  315 /*
  316  *   Transform old in6_pcbconnect() into an inner subroutine for new
  317  *   in6_pcbconnect(): Do some validity-checking on the remote
  318  *   address (in mbuf 'nam') and then determine local host address
  319  *   (i.e., which interface) to use to access that remote host.
  320  *
  321  *   This preserves definition of in6_pcbconnect(), while supporting a
  322  *   slightly different version for T/TCP.  (This is more than
  323  *   a bit of a kludge, but cleaning up the internal interfaces would
  324  *   have forced minor changes in every protocol).
  325  */
  326 static int
  327 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
  328     struct in6_addr *plocal_addr6)
  329 {
  330         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  331         int error = 0;
  332         int scope_ambiguous = 0;
  333         struct in6_addr in6a;
  334 
  335         INP_WLOCK_ASSERT(inp);
  336         INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);        /* XXXRW: why? */
  337 
  338         if (nam->sa_len != sizeof (*sin6))
  339                 return (EINVAL);
  340         if (sin6->sin6_family != AF_INET6)
  341                 return (EAFNOSUPPORT);
  342         if (sin6->sin6_port == 0)
  343                 return (EADDRNOTAVAIL);
  344 
  345         if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
  346                 scope_ambiguous = 1;
  347         if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
  348                 return(error);
  349 
  350         if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
  351                 /*
  352                  * If the destination address is UNSPECIFIED addr,
  353                  * use the loopback addr, e.g ::1.
  354                  */
  355                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  356                         sin6->sin6_addr = in6addr_loopback;
  357         }
  358         if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
  359                 return (error);
  360 
  361         error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
  362             inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
  363         if (error)
  364                 return (error);
  365 
  366         /*
  367          * Do not update this earlier, in case we return with an error.
  368          *
  369          * XXX: this in6_selectsrc_socket result might replace the bound local
  370          * address with the address specified by setsockopt(IPV6_PKTINFO).
  371          * Is it the intended behavior?
  372          */
  373         *plocal_addr6 = in6a;
  374 
  375         /*
  376          * Don't do pcblookup call here; return interface in
  377          * plocal_addr6
  378          * and exit to caller, that will do the lookup.
  379          */
  380 
  381         return (0);
  382 }
  383 
  384 /*
  385  * Outer subroutine:
  386  * Connect from a socket to a specified address.
  387  * Both address and port must be specified in argument sin.
  388  * If don't have a local address for this socket yet,
  389  * then pick one.
  390  */
  391 int
  392 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
  393     struct ucred *cred, struct mbuf *m)
  394 {
  395         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
  396         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  397         struct in6_addr addr6;
  398         int error;
  399 
  400         INP_WLOCK_ASSERT(inp);
  401         INP_HASH_WLOCK_ASSERT(pcbinfo);
  402 
  403         /*
  404          * Call inner routine, to assign local interface address.
  405          * in6_pcbladdr() may automatically fill in sin6_scope_id.
  406          */
  407         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
  408                 return (error);
  409 
  410         if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
  411                                sin6->sin6_port,
  412                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
  413                               ? &addr6 : &inp->in6p_laddr,
  414                               inp->inp_lport, 0, NULL) != NULL) {
  415                 return (EADDRINUSE);
  416         }
  417         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  418                 if (inp->inp_lport == 0) {
  419                         error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
  420                         if (error)
  421                                 return (error);
  422                 }
  423                 inp->in6p_laddr = addr6;
  424         }
  425         inp->in6p_faddr = sin6->sin6_addr;
  426         inp->inp_fport = sin6->sin6_port;
  427         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  428         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
  429         if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
  430                 inp->inp_flow |=
  431                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
  432 
  433         in_pcbrehash_mbuf(inp, m);
  434 
  435         return (0);
  436 }
  437 
  438 int
  439 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
  440 {
  441 
  442         return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
  443 }
  444 
  445 void
  446 in6_pcbdisconnect(struct inpcb *inp)
  447 {
  448 
  449         INP_WLOCK_ASSERT(inp);
  450         INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
  451 
  452         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
  453         inp->inp_fport = 0;
  454         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  455         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
  456         in_pcbrehash(inp);
  457 }
  458 
  459 struct sockaddr *
  460 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
  461 {
  462         struct sockaddr_in6 *sin6;
  463 
  464         sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
  465         bzero(sin6, sizeof *sin6);
  466         sin6->sin6_family = AF_INET6;
  467         sin6->sin6_len = sizeof(*sin6);
  468         sin6->sin6_port = port;
  469         sin6->sin6_addr = *addr_p;
  470         (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
  471 
  472         return (struct sockaddr *)sin6;
  473 }
  474 
  475 struct sockaddr *
  476 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
  477 {
  478         struct sockaddr_in sin;
  479         struct sockaddr_in6 *sin6_p;
  480 
  481         bzero(&sin, sizeof sin);
  482         sin.sin_family = AF_INET;
  483         sin.sin_len = sizeof(sin);
  484         sin.sin_port = port;
  485         sin.sin_addr = *addr_p;
  486 
  487         sin6_p = malloc(sizeof *sin6_p, M_SONAME,
  488                 M_WAITOK);
  489         in6_sin_2_v4mapsin6(&sin, sin6_p);
  490 
  491         return (struct sockaddr *)sin6_p;
  492 }
  493 
  494 int
  495 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
  496 {
  497         struct inpcb *inp;
  498         struct in6_addr addr;
  499         in_port_t port;
  500 
  501         inp = sotoinpcb(so);
  502         KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
  503 
  504         INP_RLOCK(inp);
  505         port = inp->inp_lport;
  506         addr = inp->in6p_laddr;
  507         INP_RUNLOCK(inp);
  508 
  509         *nam = in6_sockaddr(port, &addr);
  510         return 0;
  511 }
  512 
  513 int
  514 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
  515 {
  516         struct inpcb *inp;
  517         struct in6_addr addr;
  518         in_port_t port;
  519 
  520         inp = sotoinpcb(so);
  521         KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
  522 
  523         INP_RLOCK(inp);
  524         port = inp->inp_fport;
  525         addr = inp->in6p_faddr;
  526         INP_RUNLOCK(inp);
  527 
  528         *nam = in6_sockaddr(port, &addr);
  529         return 0;
  530 }
  531 
  532 int
  533 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
  534 {
  535         struct  inpcb *inp;
  536         int     error;
  537 
  538         inp = sotoinpcb(so);
  539         KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
  540 
  541 #ifdef INET
  542         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  543                 error = in_getsockaddr(so, nam);
  544                 if (error == 0)
  545                         in6_sin_2_v4mapsin6_in_sock(nam);
  546         } else
  547 #endif
  548         {
  549                 /* scope issues will be handled in in6_getsockaddr(). */
  550                 error = in6_getsockaddr(so, nam);
  551         }
  552 
  553         return error;
  554 }
  555 
  556 int
  557 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
  558 {
  559         struct  inpcb *inp;
  560         int     error;
  561 
  562         inp = sotoinpcb(so);
  563         KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
  564 
  565 #ifdef INET
  566         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  567                 error = in_getpeeraddr(so, nam);
  568                 if (error == 0)
  569                         in6_sin_2_v4mapsin6_in_sock(nam);
  570         } else
  571 #endif
  572         /* scope issues will be handled in in6_getpeeraddr(). */
  573         error = in6_getpeeraddr(so, nam);
  574 
  575         return error;
  576 }
  577 
  578 /*
  579  * Pass some notification to all connections of a protocol
  580  * associated with address dst.  The local address and/or port numbers
  581  * may be specified to limit the search.  The "usual action" will be
  582  * taken, depending on the ctlinput cmd.  The caller must filter any
  583  * cmds that are uninteresting (e.g., no error in the map).
  584  * Call the protocol specific routine (if any) to report
  585  * any errors for each matching socket.
  586  */
  587 void
  588 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
  589     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
  590     int cmd, void *cmdarg,
  591     struct inpcb *(*notify)(struct inpcb *, int))
  592 {
  593         struct inpcb *inp, *inp_temp;
  594         struct sockaddr_in6 sa6_src, *sa6_dst;
  595         u_short fport = fport_arg, lport = lport_arg;
  596         u_int32_t flowinfo;
  597         int errno;
  598 
  599         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
  600                 return;
  601 
  602         sa6_dst = (struct sockaddr_in6 *)dst;
  603         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
  604                 return;
  605 
  606         /*
  607          * note that src can be NULL when we get notify by local fragmentation.
  608          */
  609         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
  610         flowinfo = sa6_src.sin6_flowinfo;
  611 
  612         /*
  613          * Redirects go to all references to the destination,
  614          * and use in6_rtchange to invalidate the route cache.
  615          * Dead host indications: also use in6_rtchange to invalidate
  616          * the cache, and deliver the error to all the sockets.
  617          * Otherwise, if we have knowledge of the local port and address,
  618          * deliver only to that socket.
  619          */
  620         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
  621                 fport = 0;
  622                 lport = 0;
  623                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
  624 
  625                 if (cmd != PRC_HOSTDEAD)
  626                         notify = in6_rtchange;
  627         }
  628         errno = inet6ctlerrmap[cmd];
  629         INP_INFO_WLOCK(pcbinfo);
  630         LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
  631                 INP_WLOCK(inp);
  632                 if ((inp->inp_vflag & INP_IPV6) == 0) {
  633                         INP_WUNLOCK(inp);
  634                         continue;
  635                 }
  636 
  637                 /*
  638                  * If the error designates a new path MTU for a destination
  639                  * and the application (associated with this socket) wanted to
  640                  * know the value, notify.
  641                  * XXX: should we avoid to notify the value to TCP sockets?
  642                  */
  643                 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
  644                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
  645                                         *(u_int32_t *)cmdarg);
  646 
  647                 /*
  648                  * Detect if we should notify the error. If no source and
  649                  * destination ports are specifed, but non-zero flowinfo and
  650                  * local address match, notify the error. This is the case
  651                  * when the error is delivered with an encrypted buffer
  652                  * by ESP. Otherwise, just compare addresses and ports
  653                  * as usual.
  654                  */
  655                 if (lport == 0 && fport == 0 && flowinfo &&
  656                     inp->inp_socket != NULL &&
  657                     flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
  658                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
  659                         goto do_notify;
  660                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
  661                                              &sa6_dst->sin6_addr) ||
  662                          inp->inp_socket == 0 ||
  663                          (lport && inp->inp_lport != lport) ||
  664                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
  665                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  666                                               &sa6_src.sin6_addr)) ||
  667                          (fport && inp->inp_fport != fport)) {
  668                         INP_WUNLOCK(inp);
  669                         continue;
  670                 }
  671 
  672           do_notify:
  673                 if (notify) {
  674                         if ((*notify)(inp, errno))
  675                                 INP_WUNLOCK(inp);
  676                 } else
  677                         INP_WUNLOCK(inp);
  678         }
  679         INP_INFO_WUNLOCK(pcbinfo);
  680 }
  681 
  682 /*
  683  * Lookup a PCB based on the local address and port.  Caller must hold the
  684  * hash lock.  No inpcb locks or references are acquired.
  685  */
  686 struct inpcb *
  687 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
  688     u_short lport, int lookupflags, struct ucred *cred)
  689 {
  690         struct inpcb *inp;
  691         int matchwild = 3, wildcard;
  692 
  693         KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
  694             ("%s: invalid lookup flags %d", __func__, lookupflags));
  695 
  696         INP_HASH_WLOCK_ASSERT(pcbinfo);
  697 
  698         if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
  699                 struct inpcbhead *head;
  700                 /*
  701                  * Look for an unconnected (wildcard foreign addr) PCB that
  702                  * matches the local address and port we're looking for.
  703                  */
  704                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
  705                     INP6_PCBHASHKEY(&in6addr_any), lport, 0,
  706                     pcbinfo->ipi_hashmask)];
  707                 LIST_FOREACH(inp, head, inp_hash) {
  708                         /* XXX inp locking */
  709                         if ((inp->inp_vflag & INP_IPV6) == 0)
  710                                 continue;
  711                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
  712                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  713                             inp->inp_lport == lport) {
  714                                 /* Found. */
  715                                 if (cred == NULL ||
  716                                     prison_equal_ip6(cred->cr_prison,
  717                                         inp->inp_cred->cr_prison))
  718                                         return (inp);
  719                         }
  720                 }
  721                 /*
  722                  * Not found.
  723                  */
  724                 return (NULL);
  725         } else {
  726                 struct inpcbporthead *porthash;
  727                 struct inpcbport *phd;
  728                 struct inpcb *match = NULL;
  729                 /*
  730                  * Best fit PCB lookup.
  731                  *
  732                  * First see if this local port is in use by looking on the
  733                  * port hash list.
  734                  */
  735                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
  736                     pcbinfo->ipi_porthashmask)];
  737                 LIST_FOREACH(phd, porthash, phd_hash) {
  738                         if (phd->phd_port == lport)
  739                                 break;
  740                 }
  741                 if (phd != NULL) {
  742                         /*
  743                          * Port is in use by one or more PCBs. Look for best
  744                          * fit.
  745                          */
  746                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
  747                                 wildcard = 0;
  748                                 if (cred != NULL &&
  749                                     !prison_equal_ip6(cred->cr_prison,
  750                                         inp->inp_cred->cr_prison))
  751                                         continue;
  752                                 /* XXX inp locking */
  753                                 if ((inp->inp_vflag & INP_IPV6) == 0)
  754                                         continue;
  755                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
  756                                         wildcard++;
  757                                 if (!IN6_IS_ADDR_UNSPECIFIED(
  758                                         &inp->in6p_laddr)) {
  759                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
  760                                                 wildcard++;
  761                                         else if (!IN6_ARE_ADDR_EQUAL(
  762                                             &inp->in6p_laddr, laddr))
  763                                                 continue;
  764                                 } else {
  765                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
  766                                                 wildcard++;
  767                                 }
  768                                 if (wildcard < matchwild) {
  769                                         match = inp;
  770                                         matchwild = wildcard;
  771                                         if (matchwild == 0)
  772                                                 break;
  773                                 }
  774                         }
  775                 }
  776                 return (match);
  777         }
  778 }
  779 
  780 void
  781 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
  782 {
  783         struct inpcb *in6p;
  784         struct ip6_moptions *im6o;
  785         int i, gap;
  786 
  787         INP_INFO_WLOCK(pcbinfo);
  788         LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
  789                 INP_WLOCK(in6p);
  790                 im6o = in6p->in6p_moptions;
  791                 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
  792                         /*
  793                          * Unselect the outgoing ifp for multicast if it
  794                          * is being detached.
  795                          */
  796                         if (im6o->im6o_multicast_ifp == ifp)
  797                                 im6o->im6o_multicast_ifp = NULL;
  798                         /*
  799                          * Drop multicast group membership if we joined
  800                          * through the interface being detached.
  801                          */
  802                         gap = 0;
  803                         for (i = 0; i < im6o->im6o_num_memberships; i++) {
  804                                 if (im6o->im6o_membership[i]->in6m_ifp ==
  805                                     ifp) {
  806                                         in6_mc_leave(im6o->im6o_membership[i],
  807                                             NULL);
  808                                         gap++;
  809                                 } else if (gap != 0) {
  810                                         im6o->im6o_membership[i - gap] =
  811                                             im6o->im6o_membership[i];
  812                                 }
  813                         }
  814                         im6o->im6o_num_memberships -= gap;
  815                 }
  816                 INP_WUNLOCK(in6p);
  817         }
  818         INP_INFO_WUNLOCK(pcbinfo);
  819 }
  820 
  821 /*
  822  * Check for alternatives when higher level complains
  823  * about service problems.  For now, invalidate cached
  824  * routing information.  If the route was created dynamically
  825  * (by a redirect), time to try a default gateway again.
  826  */
  827 void
  828 in6_losing(struct inpcb *in6p)
  829 {
  830 
  831         if (in6p->inp_route6.ro_rt) {
  832                 RTFREE(in6p->inp_route6.ro_rt);
  833                 in6p->inp_route6.ro_rt = (struct rtentry *)NULL;
  834         }
  835         if (in6p->inp_route.ro_lle)
  836                 LLE_FREE(in6p->inp_route.ro_lle);       /* zeros ro_lle */
  837         return;
  838 }
  839 
  840 /*
  841  * After a routing change, flush old routing
  842  * and allocate a (hopefully) better one.
  843  */
  844 struct inpcb *
  845 in6_rtchange(struct inpcb *inp, int errno)
  846 {
  847 
  848         if (inp->inp_route6.ro_rt) {
  849                 RTFREE(inp->inp_route6.ro_rt);
  850                 inp->inp_route6.ro_rt = (struct rtentry *)NULL;
  851         }
  852         if (inp->inp_route.ro_lle)
  853                 LLE_FREE(inp->inp_route.ro_lle);        /* zeros ro_lle */
  854         return inp;
  855 }
  856 
  857 #ifdef PCBGROUP
  858 /*
  859  * Lookup PCB in hash list, using pcbgroup tables.
  860  */
  861 static struct inpcb *
  862 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
  863     struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
  864     u_int lport_arg, int lookupflags, struct ifnet *ifp)
  865 {
  866         struct inpcbhead *head;
  867         struct inpcb *inp, *tmpinp;
  868         u_short fport = fport_arg, lport = lport_arg;
  869         bool locked;
  870 
  871         /*
  872          * First look for an exact match.
  873          */
  874         tmpinp = NULL;
  875         INP_GROUP_LOCK(pcbgroup);
  876         head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
  877             INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
  878         LIST_FOREACH(inp, head, inp_pcbgrouphash) {
  879                 /* XXX inp locking */
  880                 if ((inp->inp_vflag & INP_IPV6) == 0)
  881                         continue;
  882                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
  883                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  884                     inp->inp_fport == fport &&
  885                     inp->inp_lport == lport) {
  886                         /*
  887                          * XXX We should be able to directly return
  888                          * the inp here, without any checks.
  889                          * Well unless both bound with SO_REUSEPORT?
  890                          */
  891                         if (prison_flag(inp->inp_cred, PR_IP6))
  892                                 goto found;
  893                         if (tmpinp == NULL)
  894                                 tmpinp = inp;
  895                 }
  896         }
  897         if (tmpinp != NULL) {
  898                 inp = tmpinp;
  899                 goto found;
  900         }
  901 
  902         /*
  903          * Then look for a wildcard match in the pcbgroup.
  904          */
  905         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
  906                 struct inpcb *local_wild = NULL, *local_exact = NULL;
  907                 struct inpcb *jail_wild = NULL;
  908                 int injail;
  909 
  910                 /*
  911                  * Order of socket selection - we always prefer jails.
  912                  *      1. jailed, non-wild.
  913                  *      2. jailed, wild.
  914                  *      3. non-jailed, non-wild.
  915                  *      4. non-jailed, wild.
  916                  */
  917                 head = &pcbgroup->ipg_hashbase[
  918                     INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
  919                 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
  920                         /* XXX inp locking */
  921                         if ((inp->inp_vflag & INP_IPV6) == 0)
  922                                 continue;
  923 
  924                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
  925                             inp->inp_lport != lport) {
  926                                 continue;
  927                         }
  928 
  929                         injail = prison_flag(inp->inp_cred, PR_IP6);
  930                         if (injail) {
  931                                 if (prison_check_ip6(inp->inp_cred,
  932                                     laddr) != 0)
  933                                         continue;
  934                         } else {
  935                                 if (local_exact != NULL)
  936                                         continue;
  937                         }
  938 
  939                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
  940                                 if (injail)
  941                                         goto found;
  942                                 else
  943                                         local_exact = inp;
  944                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  945                                 if (injail)
  946                                         jail_wild = inp;
  947                                 else
  948                                         local_wild = inp;
  949                         }
  950                 } /* LIST_FOREACH */
  951 
  952                 inp = jail_wild;
  953                 if (inp == NULL)
  954                         inp = jail_wild;
  955                 if (inp == NULL)
  956                         inp = local_exact;
  957                 if (inp == NULL)
  958                         inp = local_wild;
  959                 if (inp != NULL)
  960                         goto found;
  961         }
  962 
  963         /*
  964          * Then look for a wildcard match, if requested.
  965          */
  966         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
  967                 struct inpcb *local_wild = NULL, *local_exact = NULL;
  968                 struct inpcb *jail_wild = NULL;
  969                 int injail;
  970 
  971                 /*
  972                  * Order of socket selection - we always prefer jails.
  973                  *      1. jailed, non-wild.
  974                  *      2. jailed, wild.
  975                  *      3. non-jailed, non-wild.
  976                  *      4. non-jailed, wild.
  977                  */
  978                 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
  979                     INP6_PCBHASHKEY(&in6addr_any), lport, 0,
  980                     pcbinfo->ipi_wildmask)];
  981                 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
  982                         /* XXX inp locking */
  983                         if ((inp->inp_vflag & INP_IPV6) == 0)
  984                                 continue;
  985 
  986                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
  987                             inp->inp_lport != lport) {
  988                                 continue;
  989                         }
  990 
  991                         injail = prison_flag(inp->inp_cred, PR_IP6);
  992                         if (injail) {
  993                                 if (prison_check_ip6(inp->inp_cred,
  994                                     laddr) != 0)
  995                                         continue;
  996                         } else {
  997                                 if (local_exact != NULL)
  998                                         continue;
  999                         }
 1000 
 1001                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
 1002                                 if (injail)
 1003                                         goto found;
 1004                                 else
 1005                                         local_exact = inp;
 1006                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
 1007                                 if (injail)
 1008                                         jail_wild = inp;
 1009                                 else
 1010                                         local_wild = inp;
 1011                         }
 1012                 } /* LIST_FOREACH */
 1013 
 1014                 inp = jail_wild;
 1015                 if (inp == NULL)
 1016                         inp = jail_wild;
 1017                 if (inp == NULL)
 1018                         inp = local_exact;
 1019                 if (inp == NULL)
 1020                         inp = local_wild;
 1021                 if (inp != NULL)
 1022                         goto found;
 1023         } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
 1024         INP_GROUP_UNLOCK(pcbgroup);
 1025         return (NULL);
 1026 
 1027 found:
 1028         if (lookupflags & INPLOOKUP_WLOCKPCB)
 1029                 locked = INP_TRY_WLOCK(inp);
 1030         else if (lookupflags & INPLOOKUP_RLOCKPCB)
 1031                 locked = INP_TRY_RLOCK(inp);
 1032         else
 1033                 panic("%s: locking buf", __func__);
 1034         if (!locked)
 1035                 in_pcbref(inp);
 1036         INP_GROUP_UNLOCK(pcbgroup);
 1037         if (!locked) {
 1038                 if (lookupflags & INPLOOKUP_WLOCKPCB) {
 1039                         INP_WLOCK(inp);
 1040                         if (in_pcbrele_wlocked(inp))
 1041                                 return (NULL);
 1042                 } else {
 1043                         INP_RLOCK(inp);
 1044                         if (in_pcbrele_rlocked(inp))
 1045                                 return (NULL);
 1046                 }
 1047         }
 1048 #ifdef INVARIANTS
 1049         if (lookupflags & INPLOOKUP_WLOCKPCB)
 1050                 INP_WLOCK_ASSERT(inp);
 1051         else
 1052                 INP_RLOCK_ASSERT(inp);
 1053 #endif
 1054         return (inp);
 1055 }
 1056 #endif /* PCBGROUP */
 1057 
 1058 /*
 1059  * Lookup PCB in hash list.
 1060  */
 1061 static struct inpcb *
 1062 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
 1063     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
 1064     int lookupflags, struct ifnet *ifp)
 1065 {
 1066         struct inpcbhead *head;
 1067         struct inpcb *inp, *tmpinp;
 1068         u_short fport = fport_arg, lport = lport_arg;
 1069 
 1070         KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
 1071             ("%s: invalid lookup flags %d", __func__, lookupflags));
 1072 
 1073         INP_HASH_LOCK_ASSERT(pcbinfo);
 1074 
 1075         /*
 1076          * First look for an exact match.
 1077          */
 1078         tmpinp = NULL;
 1079         head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
 1080             INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
 1081         LIST_FOREACH(inp, head, inp_hash) {
 1082                 /* XXX inp locking */
 1083                 if ((inp->inp_vflag & INP_IPV6) == 0)
 1084                         continue;
 1085                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
 1086                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
 1087                     inp->inp_fport == fport &&
 1088                     inp->inp_lport == lport) {
 1089                         /*
 1090                          * XXX We should be able to directly return
 1091                          * the inp here, without any checks.
 1092                          * Well unless both bound with SO_REUSEPORT?
 1093                          */
 1094                         if (prison_flag(inp->inp_cred, PR_IP6))
 1095                                 return (inp);
 1096                         if (tmpinp == NULL)
 1097                                 tmpinp = inp;
 1098                 }
 1099         }
 1100         if (tmpinp != NULL)
 1101                 return (tmpinp);
 1102 
 1103         /*
 1104          * Then look for a wildcard match, if requested.
 1105          */
 1106         if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
 1107                 struct inpcb *local_wild = NULL, *local_exact = NULL;
 1108                 struct inpcb *jail_wild = NULL;
 1109                 int injail;
 1110 
 1111                 /*
 1112                  * Order of socket selection - we always prefer jails.
 1113                  *      1. jailed, non-wild.
 1114                  *      2. jailed, wild.
 1115                  *      3. non-jailed, non-wild.
 1116                  *      4. non-jailed, wild.
 1117                  */
 1118                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
 1119                     INP6_PCBHASHKEY(&in6addr_any), lport, 0,
 1120                     pcbinfo->ipi_hashmask)];
 1121                 LIST_FOREACH(inp, head, inp_hash) {
 1122                         /* XXX inp locking */
 1123                         if ((inp->inp_vflag & INP_IPV6) == 0)
 1124                                 continue;
 1125 
 1126                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
 1127                             inp->inp_lport != lport) {
 1128                                 continue;
 1129                         }
 1130 
 1131                         injail = prison_flag(inp->inp_cred, PR_IP6);
 1132                         if (injail) {
 1133                                 if (prison_check_ip6(inp->inp_cred,
 1134                                     laddr) != 0)
 1135                                         continue;
 1136                         } else {
 1137                                 if (local_exact != NULL)
 1138                                         continue;
 1139                         }
 1140 
 1141                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
 1142                                 if (injail)
 1143                                         return (inp);
 1144                                 else
 1145                                         local_exact = inp;
 1146                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
 1147                                 if (injail)
 1148                                         jail_wild = inp;
 1149                                 else
 1150                                         local_wild = inp;
 1151                         }
 1152                 } /* LIST_FOREACH */
 1153 
 1154                 if (jail_wild != NULL)
 1155                         return (jail_wild);
 1156                 if (local_exact != NULL)
 1157                         return (local_exact);
 1158                 if (local_wild != NULL)
 1159                         return (local_wild);
 1160         } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
 1161 
 1162         /*
 1163          * Not found.
 1164          */
 1165         return (NULL);
 1166 }
 1167 
 1168 /*
 1169  * Lookup PCB in hash list, using pcbinfo tables.  This variation locks the
 1170  * hash list lock, and will return the inpcb locked (i.e., requires
 1171  * INPLOOKUP_LOCKPCB).
 1172  */
 1173 static struct inpcb *
 1174 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
 1175     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
 1176     struct ifnet *ifp)
 1177 {
 1178         struct inpcb *inp;
 1179         bool locked;
 1180 
 1181         INP_HASH_RLOCK(pcbinfo);
 1182         inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
 1183             (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
 1184         if (inp != NULL) {
 1185                 if (lookupflags & INPLOOKUP_WLOCKPCB)
 1186                         locked = INP_TRY_WLOCK(inp);
 1187                 else if (lookupflags & INPLOOKUP_RLOCKPCB)
 1188                         locked = INP_TRY_RLOCK(inp);
 1189                 else
 1190                         panic("%s: locking bug", __func__);
 1191                 if (!locked)
 1192                         in_pcbref(inp);
 1193                 INP_HASH_RUNLOCK(pcbinfo);
 1194                 if (!locked) {
 1195                         if (lookupflags & INPLOOKUP_WLOCKPCB) {
 1196                                 INP_WLOCK(inp);
 1197                                 if (in_pcbrele_wlocked(inp))
 1198                                         return (NULL);
 1199                         } else {
 1200                                 INP_RLOCK(inp);
 1201                                 if (in_pcbrele_rlocked(inp))
 1202                                         return (NULL);
 1203                         }
 1204                 }
 1205 #ifdef INVARIANTS
 1206                 if (lookupflags & INPLOOKUP_WLOCKPCB)
 1207                         INP_WLOCK_ASSERT(inp);
 1208                 else
 1209                         INP_RLOCK_ASSERT(inp);
 1210 #endif
 1211         } else
 1212                 INP_HASH_RUNLOCK(pcbinfo);
 1213         return (inp);
 1214 }
 1215 
 1216 /*
 1217  * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
 1218  * from which a pre-calculated hash value may be extracted.
 1219  *
 1220  * Possibly more of this logic should be in in6_pcbgroup.c.
 1221  */
 1222 struct inpcb *
 1223 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
 1224     struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
 1225 {
 1226 #if defined(PCBGROUP) && !defined(RSS)
 1227         struct inpcbgroup *pcbgroup;
 1228 #endif
 1229 
 1230         KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
 1231             ("%s: invalid lookup flags %d", __func__, lookupflags));
 1232         KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
 1233             ("%s: LOCKPCB not set", __func__));
 1234 
 1235         /*
 1236          * When not using RSS, use connection groups in preference to the
 1237          * reservation table when looking up 4-tuples.  When using RSS, just
 1238          * use the reservation table, due to the cost of the Toeplitz hash
 1239          * in software.
 1240          *
 1241          * XXXRW: This policy belongs in the pcbgroup code, as in principle
 1242          * we could be doing RSS with a non-Toeplitz hash that is affordable
 1243          * in software.
 1244          */
 1245 #if defined(PCBGROUP) && !defined(RSS)
 1246         if (in_pcbgroup_enabled(pcbinfo)) {
 1247                 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
 1248                     fport);
 1249                 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
 1250                     laddr, lport, lookupflags, ifp));
 1251         }
 1252 #endif
 1253         return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
 1254             lookupflags, ifp));
 1255 }
 1256 
 1257 struct inpcb *
 1258 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
 1259     u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
 1260     struct ifnet *ifp, struct mbuf *m)
 1261 {
 1262 #ifdef PCBGROUP
 1263         struct inpcbgroup *pcbgroup;
 1264 #endif
 1265 
 1266         KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
 1267             ("%s: invalid lookup flags %d", __func__, lookupflags));
 1268         KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
 1269             ("%s: LOCKPCB not set", __func__));
 1270 
 1271 #ifdef PCBGROUP
 1272         /*
 1273          * If we can use a hardware-generated hash to look up the connection
 1274          * group, use that connection group to find the inpcb.  Otherwise
 1275          * fall back on a software hash -- or the reservation table if we're
 1276          * using RSS.
 1277          *
 1278          * XXXRW: As above, that policy belongs in the pcbgroup code.
 1279          */
 1280         if (in_pcbgroup_enabled(pcbinfo) &&
 1281             M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
 1282                 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
 1283                     m->m_pkthdr.flowid);
 1284                 if (pcbgroup != NULL)
 1285                         return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
 1286                             fport, laddr, lport, lookupflags, ifp));
 1287 #ifndef RSS
 1288                 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
 1289                     fport);
 1290                 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
 1291                     laddr, lport, lookupflags, ifp));
 1292 #endif
 1293         }
 1294 #endif
 1295         return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
 1296             lookupflags, ifp));
 1297 }
 1298 
 1299 void
 1300 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
 1301 {
 1302         struct ip6_hdr *ip;
 1303 
 1304         ip = mtod(m, struct ip6_hdr *);
 1305         bzero(sin6, sizeof(*sin6));
 1306         sin6->sin6_len = sizeof(*sin6);
 1307         sin6->sin6_family = AF_INET6;
 1308         sin6->sin6_addr = ip->ip6_src;
 1309 
 1310         (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
 1311 
 1312         return;
 1313 }

Cache object: b9476cca271e6bd485ff2645e97e4e71


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