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

Cache object: 8ed0ada358edf644f5a098a69cc811d7


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