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  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1982, 1986, 1991, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  *
   36  * Redistribution and use in source and binary forms, with or without
   37  * modification, are permitted provided that the following conditions
   38  * are met:
   39  * 1. Redistributions of source code must retain the above copyright
   40  *    notice, this list of conditions and the following disclaimer.
   41  * 2. Redistributions in binary form must reproduce the above copyright
   42  *    notice, this list of conditions and the following disclaimer in the
   43  *    documentation and/or other materials provided with the distribution.
   44  * 4. Neither the name of the University nor the names of its contributors
   45  *    may be used to endorse or promote products derived from this software
   46  *    without specific prior written permission.
   47  *
   48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   58  * SUCH DAMAGE.
   59  *
   60  *      @(#)in_pcb.c    8.2 (Berkeley) 1/4/94
   61  */
   62 
   63 #include <sys/cdefs.h>
   64 __FBSDID("$FreeBSD$");
   65 
   66 #include "opt_inet.h"
   67 #include "opt_inet6.h"
   68 #include "opt_ipsec.h"
   69 #include "opt_mac.h"
   70 
   71 #include <sys/param.h>
   72 #include <sys/systm.h>
   73 #include <sys/malloc.h>
   74 #include <sys/mbuf.h>
   75 #include <sys/domain.h>
   76 #include <sys/protosw.h>
   77 #include <sys/socket.h>
   78 #include <sys/socketvar.h>
   79 #include <sys/sockio.h>
   80 #include <sys/errno.h>
   81 #include <sys/time.h>
   82 #include <sys/priv.h>
   83 #include <sys/proc.h>
   84 #include <sys/jail.h>
   85 
   86 #include <vm/uma.h>
   87 
   88 #include <net/if.h>
   89 #include <net/if_types.h>
   90 #include <net/route.h>
   91 
   92 #include <netinet/in.h>
   93 #include <netinet/in_var.h>
   94 #include <netinet/in_systm.h>
   95 #include <netinet/tcp_var.h>
   96 #include <netinet/ip6.h>
   97 #include <netinet/ip_var.h>
   98 #include <netinet6/ip6_var.h>
   99 #include <netinet6/nd6.h>
  100 #include <netinet/in_pcb.h>
  101 #include <netinet6/in6_pcb.h>
  102 #include <netinet6/scope6_var.h>
  103 
  104 #include <security/mac/mac_framework.h>
  105 
  106 struct  in6_addr zeroin6_addr;
  107 
  108 int
  109 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
  110     struct ucred *cred)
  111 {
  112         struct socket *so = inp->inp_socket;
  113         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
  114         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
  115         u_short lport = 0;
  116         int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
  117 
  118         INP_INFO_WLOCK_ASSERT(pcbinfo);
  119         INP_WLOCK_ASSERT(inp);
  120 
  121         if (!in6_ifaddr) /* XXX broken! */
  122                 return (EADDRNOTAVAIL);
  123         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
  124                 return (EINVAL);
  125         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
  126                 wild = INPLOOKUP_WILDCARD;
  127         if (nam == NULL) {
  128                 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
  129                     ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
  130                         return (error);
  131         } else {
  132                 sin6 = (struct sockaddr_in6 *)nam;
  133                 if (nam->sa_len != sizeof(*sin6))
  134                         return (EINVAL);
  135                 /*
  136                  * family check.
  137                  */
  138                 if (nam->sa_family != AF_INET6)
  139                         return (EAFNOSUPPORT);
  140 
  141                 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
  142                         return(error);
  143 
  144                 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
  145                     ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
  146                         return (error);
  147 
  148                 lport = sin6->sin6_port;
  149                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
  150                         /*
  151                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
  152                          * allow compepte duplication of binding if
  153                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
  154                          * and a multicast address is bound on both
  155                          * new and duplicated sockets.
  156                          */
  157                         if (so->so_options & SO_REUSEADDR)
  158                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
  159                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  160                         struct ifaddr *ia = NULL;
  161 
  162                         sin6->sin6_port = 0;            /* yech... */
  163                         if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
  164                                 return (EADDRNOTAVAIL);
  165 
  166                         /*
  167                          * XXX: bind to an anycast address might accidentally
  168                          * cause sending a packet with anycast source address.
  169                          * We should allow to bind to a deprecated address, since
  170                          * the application dares to use it.
  171                          */
  172                         if (ia &&
  173                             ((struct in6_ifaddr *)ia)->ia6_flags &
  174                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
  175                                 return (EADDRNOTAVAIL);
  176                         }
  177                 }
  178                 if (lport) {
  179                         struct inpcb *t;
  180 
  181                         /* GROSS */
  182                         if (ntohs(lport) <= ipport_reservedhigh &&
  183                             ntohs(lport) >= ipport_reservedlow &&
  184                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
  185                             0))
  186                                 return (EACCES);
  187                         if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
  188                             priv_check_cred(inp->inp_cred,
  189                             PRIV_NETINET_REUSEPORT, 0) != 0) {
  190                                 t = in6_pcblookup_local(pcbinfo,
  191                                     &sin6->sin6_addr, lport,
  192                                     INPLOOKUP_WILDCARD, cred);
  193                                 if (t &&
  194                                     ((t->inp_flags & INP_TIMEWAIT) == 0) &&
  195                                     (so->so_type != SOCK_STREAM ||
  196                                      IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
  197                                     (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
  198                                      !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
  199                                      (t->inp_socket->so_options & SO_REUSEPORT)
  200                                       == 0) && (inp->inp_cred->cr_uid !=
  201                                      t->inp_cred->cr_uid))
  202                                         return (EADDRINUSE);
  203                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
  204                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  205                                         struct sockaddr_in sin;
  206 
  207                                         in6_sin6_2_sin(&sin, sin6);
  208                                         t = in_pcblookup_local(pcbinfo,
  209                                             sin.sin_addr, lport,
  210                                             INPLOOKUP_WILDCARD, cred);
  211                                         if (t &&
  212                                             ((t->inp_flags &
  213                                               INP_TIMEWAIT) == 0) &&
  214                                             (so->so_type != SOCK_STREAM ||
  215                                              ntohl(t->inp_faddr.s_addr) ==
  216                                               INADDR_ANY) &&
  217                                             (inp->inp_cred->cr_uid !=
  218                                              t->inp_cred->cr_uid))
  219                                                 return (EADDRINUSE);
  220                                 }
  221                         }
  222                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
  223                             lport, wild, cred);
  224                         if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
  225                             intotw(t)->tw_so_options :
  226                             t->inp_socket->so_options)) == 0)
  227                                 return (EADDRINUSE);
  228                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
  229                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  230                                 struct sockaddr_in sin;
  231 
  232                                 in6_sin6_2_sin(&sin, sin6);
  233                                 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
  234                                     lport, wild, cred);
  235                                 if (t && t->inp_flags & INP_TIMEWAIT) {
  236                                         if ((reuseport &
  237                                             intotw(t)->tw_so_options) == 0 &&
  238                                             (ntohl(t->inp_laddr.s_addr) !=
  239                                              INADDR_ANY || ((inp->inp_vflag &
  240                                              INP_IPV6PROTO) ==
  241                                              (t->inp_vflag & INP_IPV6PROTO))))
  242                                                 return (EADDRINUSE);
  243                                 }
  244                                 else if (t &&
  245                                     (reuseport & t->inp_socket->so_options)
  246                                     == 0 && (ntohl(t->inp_laddr.s_addr) !=
  247                                     INADDR_ANY || INP_SOCKAF(so) ==
  248                                      INP_SOCKAF(t->inp_socket)))
  249                                         return (EADDRINUSE);
  250                         }
  251                 }
  252                 inp->in6p_laddr = sin6->sin6_addr;
  253         }
  254         if (lport == 0) {
  255                 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
  256                         return (error);
  257         } else {
  258                 inp->inp_lport = lport;
  259                 if (in_pcbinshash(inp) != 0) {
  260                         inp->in6p_laddr = in6addr_any;
  261                         inp->inp_lport = 0;
  262                         return (EAGAIN);
  263                 }
  264         }
  265         return (0);
  266 }
  267 
  268 /*
  269  *   Transform old in6_pcbconnect() into an inner subroutine for new
  270  *   in6_pcbconnect(): Do some validity-checking on the remote
  271  *   address (in mbuf 'nam') and then determine local host address
  272  *   (i.e., which interface) to use to access that remote host.
  273  *
  274  *   This preserves definition of in6_pcbconnect(), while supporting a
  275  *   slightly different version for T/TCP.  (This is more than
  276  *   a bit of a kludge, but cleaning up the internal interfaces would
  277  *   have forced minor changes in every protocol).
  278  */
  279 int
  280 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
  281     struct in6_addr **plocal_addr6)
  282 {
  283         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  284         int error = 0;
  285         struct ifnet *ifp = NULL;
  286         int scope_ambiguous = 0;
  287 
  288         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  289         INP_WLOCK_ASSERT(inp);
  290 
  291         if (nam->sa_len != sizeof (*sin6))
  292                 return (EINVAL);
  293         if (sin6->sin6_family != AF_INET6)
  294                 return (EAFNOSUPPORT);
  295         if (sin6->sin6_port == 0)
  296                 return (EADDRNOTAVAIL);
  297 
  298         if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
  299                 scope_ambiguous = 1;
  300         if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
  301                 return(error);
  302 
  303         if (in6_ifaddr) {
  304                 /*
  305                  * If the destination address is UNSPECIFIED addr,
  306                  * use the loopback addr, e.g ::1.
  307                  */
  308                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  309                         sin6->sin6_addr = in6addr_loopback;
  310         }
  311         if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
  312                 return (error);
  313 
  314         /*
  315          * XXX: in6_selectsrc might replace the bound local address
  316          * with the address specified by setsockopt(IPV6_PKTINFO).
  317          * Is it the intended behavior?
  318          */
  319         *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
  320                                       inp, NULL,
  321                                       inp->inp_cred,
  322                                       &ifp, &error);
  323         if (ifp && scope_ambiguous &&
  324             (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
  325                 return(error);
  326         }
  327 
  328         if (*plocal_addr6 == NULL) {
  329                 if (error == 0)
  330                         error = EADDRNOTAVAIL;
  331                 return (error);
  332         }
  333         /*
  334          * Don't do pcblookup call here; return interface in
  335          * plocal_addr6
  336          * and exit to caller, that will do the lookup.
  337          */
  338 
  339         return (0);
  340 }
  341 
  342 /*
  343  * Outer subroutine:
  344  * Connect from a socket to a specified address.
  345  * Both address and port must be specified in argument sin.
  346  * If don't have a local address for this socket yet,
  347  * then pick one.
  348  */
  349 int
  350 in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
  351     struct ucred *cred)
  352 {
  353         struct in6_addr *addr6;
  354         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  355         int error;
  356 
  357         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  358         INP_WLOCK_ASSERT(inp);
  359 
  360         /*
  361          * Call inner routine, to assign local interface address.
  362          * in6_pcbladdr() may automatically fill in sin6_scope_id.
  363          */
  364         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
  365                 return (error);
  366 
  367         if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
  368                                sin6->sin6_port,
  369                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
  370                               ? addr6 : &inp->in6p_laddr,
  371                               inp->inp_lport, 0, NULL) != NULL) {
  372                 return (EADDRINUSE);
  373         }
  374         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  375                 if (inp->inp_lport == 0) {
  376                         error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
  377                         if (error)
  378                                 return (error);
  379                 }
  380                 inp->in6p_laddr = *addr6;
  381         }
  382         inp->in6p_faddr = sin6->sin6_addr;
  383         inp->inp_fport = sin6->sin6_port;
  384         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  385         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
  386         if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
  387                 inp->inp_flow |=
  388                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
  389 
  390         in_pcbrehash(inp);
  391 
  392         return (0);
  393 }
  394 
  395 void
  396 in6_pcbdisconnect(struct inpcb *inp)
  397 {
  398 
  399         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  400         INP_WLOCK_ASSERT(inp);
  401 
  402         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
  403         inp->inp_fport = 0;
  404         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  405         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
  406         in_pcbrehash(inp);
  407 }
  408 
  409 struct sockaddr *
  410 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
  411 {
  412         struct sockaddr_in6 *sin6;
  413 
  414         MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
  415         bzero(sin6, sizeof *sin6);
  416         sin6->sin6_family = AF_INET6;
  417         sin6->sin6_len = sizeof(*sin6);
  418         sin6->sin6_port = port;
  419         sin6->sin6_addr = *addr_p;
  420         (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
  421 
  422         return (struct sockaddr *)sin6;
  423 }
  424 
  425 struct sockaddr *
  426 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
  427 {
  428         struct sockaddr_in sin;
  429         struct sockaddr_in6 *sin6_p;
  430 
  431         bzero(&sin, sizeof sin);
  432         sin.sin_family = AF_INET;
  433         sin.sin_len = sizeof(sin);
  434         sin.sin_port = port;
  435         sin.sin_addr = *addr_p;
  436 
  437         MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
  438                 M_WAITOK);
  439         in6_sin_2_v4mapsin6(&sin, sin6_p);
  440 
  441         return (struct sockaddr *)sin6_p;
  442 }
  443 
  444 int
  445 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
  446 {
  447         register struct inpcb *inp;
  448         struct in6_addr addr;
  449         in_port_t port;
  450 
  451         inp = sotoinpcb(so);
  452         KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
  453 
  454         INP_RLOCK(inp);
  455         port = inp->inp_lport;
  456         addr = inp->in6p_laddr;
  457         INP_RUNLOCK(inp);
  458 
  459         *nam = in6_sockaddr(port, &addr);
  460         return 0;
  461 }
  462 
  463 int
  464 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
  465 {
  466         struct inpcb *inp;
  467         struct in6_addr addr;
  468         in_port_t port;
  469 
  470         inp = sotoinpcb(so);
  471         KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
  472 
  473         INP_RLOCK(inp);
  474         port = inp->inp_fport;
  475         addr = inp->in6p_faddr;
  476         INP_RUNLOCK(inp);
  477 
  478         *nam = in6_sockaddr(port, &addr);
  479         return 0;
  480 }
  481 
  482 int
  483 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
  484 {
  485         struct  inpcb *inp;
  486         int     error;
  487 
  488         inp = sotoinpcb(so);
  489         KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
  490 
  491         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  492                 error = in_getsockaddr(so, nam);
  493                 if (error == 0)
  494                         in6_sin_2_v4mapsin6_in_sock(nam);
  495         } else {
  496                 /* scope issues will be handled in in6_getsockaddr(). */
  497                 error = in6_getsockaddr(so, nam);
  498         }
  499 
  500         return error;
  501 }
  502 
  503 int
  504 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
  505 {
  506         struct  inpcb *inp;
  507         int     error;
  508 
  509         inp = sotoinpcb(so);
  510         KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
  511 
  512         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  513                 error = in_getpeeraddr(so, nam);
  514                 if (error == 0)
  515                         in6_sin_2_v4mapsin6_in_sock(nam);
  516         } else
  517         /* scope issues will be handled in in6_getpeeraddr(). */
  518         error = in6_getpeeraddr(so, nam);
  519 
  520         return error;
  521 }
  522 
  523 /*
  524  * Pass some notification to all connections of a protocol
  525  * associated with address dst.  The local address and/or port numbers
  526  * may be specified to limit the search.  The "usual action" will be
  527  * taken, depending on the ctlinput cmd.  The caller must filter any
  528  * cmds that are uninteresting (e.g., no error in the map).
  529  * Call the protocol specific routine (if any) to report
  530  * any errors for each matching socket.
  531  */
  532 void
  533 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
  534     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
  535     int cmd, void *cmdarg,
  536     struct inpcb *(*notify)(struct inpcb *, int))
  537 {
  538         struct inpcb *inp, *inp_temp;
  539         struct sockaddr_in6 sa6_src, *sa6_dst;
  540         u_short fport = fport_arg, lport = lport_arg;
  541         u_int32_t flowinfo;
  542         int errno;
  543 
  544         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
  545                 return;
  546 
  547         sa6_dst = (struct sockaddr_in6 *)dst;
  548         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
  549                 return;
  550 
  551         /*
  552          * note that src can be NULL when we get notify by local fragmentation.
  553          */
  554         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
  555         flowinfo = sa6_src.sin6_flowinfo;
  556 
  557         /*
  558          * Redirects go to all references to the destination,
  559          * and use in6_rtchange to invalidate the route cache.
  560          * Dead host indications: also use in6_rtchange to invalidate
  561          * the cache, and deliver the error to all the sockets.
  562          * Otherwise, if we have knowledge of the local port and address,
  563          * deliver only to that socket.
  564          */
  565         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
  566                 fport = 0;
  567                 lport = 0;
  568                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
  569 
  570                 if (cmd != PRC_HOSTDEAD)
  571                         notify = in6_rtchange;
  572         }
  573         errno = inet6ctlerrmap[cmd];
  574         INP_INFO_WLOCK(pcbinfo);
  575         LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
  576                 INP_WLOCK(inp);
  577                 if ((inp->inp_vflag & INP_IPV6) == 0) {
  578                         INP_WUNLOCK(inp);
  579                         continue;
  580                 }
  581 
  582                 /*
  583                  * If the error designates a new path MTU for a destination
  584                  * and the application (associated with this socket) wanted to
  585                  * know the value, notify. Note that we notify for all
  586                  * disconnected sockets if the corresponding application
  587                  * wanted. This is because some UDP applications keep sending
  588                  * sockets disconnected.
  589                  * XXX: should we avoid to notify the value to TCP sockets?
  590                  */
  591                 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
  592                     (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
  593                      IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
  594                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
  595                                         (u_int32_t *)cmdarg);
  596                 }
  597 
  598                 /*
  599                  * Detect if we should notify the error. If no source and
  600                  * destination ports are specifed, but non-zero flowinfo and
  601                  * local address match, notify the error. This is the case
  602                  * when the error is delivered with an encrypted buffer
  603                  * by ESP. Otherwise, just compare addresses and ports
  604                  * as usual.
  605                  */
  606                 if (lport == 0 && fport == 0 && flowinfo &&
  607                     inp->inp_socket != NULL &&
  608                     flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
  609                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
  610                         goto do_notify;
  611                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
  612                                              &sa6_dst->sin6_addr) ||
  613                          inp->inp_socket == 0 ||
  614                          (lport && inp->inp_lport != lport) ||
  615                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
  616                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  617                                               &sa6_src.sin6_addr)) ||
  618                          (fport && inp->inp_fport != fport)) {
  619                         INP_WUNLOCK(inp);
  620                         continue;
  621                 }
  622 
  623           do_notify:
  624                 if (notify) {
  625                         if ((*notify)(inp, errno))
  626                                 INP_WUNLOCK(inp);
  627                 } else
  628                         INP_WUNLOCK(inp);
  629         }
  630         INP_INFO_WUNLOCK(pcbinfo);
  631 }
  632 
  633 /*
  634  * Lookup a PCB based on the local address and port.
  635  */
  636 struct inpcb *
  637 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
  638     u_short lport, int wild_okay, struct ucred *cred)
  639 {
  640         register struct inpcb *inp;
  641         int matchwild = 3, wildcard;
  642 
  643         INP_INFO_WLOCK_ASSERT(pcbinfo);
  644 
  645         if (!wild_okay) {
  646                 struct inpcbhead *head;
  647                 /*
  648                  * Look for an unconnected (wildcard foreign addr) PCB that
  649                  * matches the local address and port we're looking for.
  650                  */
  651                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
  652                     0, pcbinfo->ipi_hashmask)];
  653                 LIST_FOREACH(inp, head, inp_hash) {
  654                         /* XXX inp locking */
  655                         if ((inp->inp_vflag & INP_IPV6) == 0)
  656                                 continue;
  657                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
  658                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  659                             inp->inp_lport == lport) {
  660                                 /* Found. */
  661                                 if (cred == NULL ||
  662                                     inp->inp_cred->cr_prison == cred->cr_prison)
  663                                         return (inp);
  664                         }
  665                 }
  666                 /*
  667                  * Not found.
  668                  */
  669                 return (NULL);
  670         } else {
  671                 struct inpcbporthead *porthash;
  672                 struct inpcbport *phd;
  673                 struct inpcb *match = NULL;
  674                 /*
  675                  * Best fit PCB lookup.
  676                  *
  677                  * First see if this local port is in use by looking on the
  678                  * port hash list.
  679                  */
  680                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
  681                     pcbinfo->ipi_porthashmask)];
  682                 LIST_FOREACH(phd, porthash, phd_hash) {
  683                         if (phd->phd_port == lport)
  684                                 break;
  685                 }
  686                 if (phd != NULL) {
  687                         /*
  688                          * Port is in use by one or more PCBs. Look for best
  689                          * fit.
  690                          */
  691                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
  692                                 wildcard = 0;
  693                                 if (cred != NULL &&
  694                                     inp->inp_cred->cr_prison != cred->cr_prison)
  695                                         continue;
  696                                 /* XXX inp locking */
  697                                 if ((inp->inp_vflag & INP_IPV6) == 0)
  698                                         continue;
  699                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
  700                                         wildcard++;
  701                                 if (!IN6_IS_ADDR_UNSPECIFIED(
  702                                         &inp->in6p_laddr)) {
  703                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
  704                                                 wildcard++;
  705                                         else if (!IN6_ARE_ADDR_EQUAL(
  706                                             &inp->in6p_laddr, laddr))
  707                                                 continue;
  708                                 } else {
  709                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
  710                                                 wildcard++;
  711                                 }
  712                                 if (wildcard < matchwild) {
  713                                         match = inp;
  714                                         matchwild = wildcard;
  715                                         if (matchwild == 0)
  716                                                 break;
  717                                 }
  718                         }
  719                 }
  720                 return (match);
  721         }
  722 }
  723 
  724 void
  725 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
  726 {
  727         struct inpcb *in6p;
  728         struct ip6_moptions *im6o;
  729         struct in6_multi_mship *imm, *nimm;
  730 
  731         INP_INFO_RLOCK(pcbinfo);
  732         LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
  733                 INP_WLOCK(in6p);
  734                 im6o = in6p->in6p_moptions;
  735                 if ((in6p->inp_vflag & INP_IPV6) &&
  736                     im6o) {
  737                         /*
  738                          * Unselect the outgoing interface if it is being
  739                          * detached.
  740                          */
  741                         if (im6o->im6o_multicast_ifp == ifp)
  742                                 im6o->im6o_multicast_ifp = NULL;
  743 
  744                         /*
  745                          * Drop multicast group membership if we joined
  746                          * through the interface being detached.
  747                          * XXX controversial - is it really legal for kernel
  748                          * to force this?
  749                          */
  750                         for (imm = im6o->im6o_memberships.lh_first;
  751                              imm != NULL; imm = nimm) {
  752                                 nimm = imm->i6mm_chain.le_next;
  753                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
  754                                         LIST_REMOVE(imm, i6mm_chain);
  755                                         in6_delmulti(imm->i6mm_maddr);
  756                                         free(imm, M_IP6MADDR);
  757                                 }
  758                         }
  759                 }
  760                 INP_WUNLOCK(in6p);
  761         }
  762         INP_INFO_RUNLOCK(pcbinfo);
  763 }
  764 
  765 /*
  766  * Check for alternatives when higher level complains
  767  * about service problems.  For now, invalidate cached
  768  * routing information.  If the route was created dynamically
  769  * (by a redirect), time to try a default gateway again.
  770  */
  771 void
  772 in6_losing(struct inpcb *in6p)
  773 {
  774 
  775         /*
  776          * We don't store route pointers in the routing table anymore
  777          */
  778         return;
  779 }
  780 
  781 /*
  782  * After a routing change, flush old routing
  783  * and allocate a (hopefully) better one.
  784  */
  785 struct inpcb *
  786 in6_rtchange(struct inpcb *inp, int errno)
  787 {
  788         /*
  789          * We don't store route pointers in the routing table anymore
  790          */
  791         return inp;
  792 }
  793 
  794 /*
  795  * Lookup PCB in hash list.
  796  */
  797 struct inpcb *
  798 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
  799     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
  800     struct ifnet *ifp)
  801 {
  802         struct inpcbhead *head;
  803         struct inpcb *inp, *tmpinp;
  804         u_short fport = fport_arg, lport = lport_arg;
  805         int faith;
  806 
  807         INP_INFO_LOCK_ASSERT(pcbinfo);
  808 
  809         if (faithprefix_p != NULL)
  810                 faith = (*faithprefix_p)(laddr);
  811         else
  812                 faith = 0;
  813 
  814         /*
  815          * First look for an exact match.
  816          */
  817         tmpinp = NULL;
  818         head = &pcbinfo->ipi_hashbase[
  819             INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
  820             pcbinfo->ipi_hashmask)];
  821         LIST_FOREACH(inp, head, inp_hash) {
  822                 /* XXX inp locking */
  823                 if ((inp->inp_vflag & INP_IPV6) == 0)
  824                         continue;
  825                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
  826                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  827                     inp->inp_fport == fport &&
  828                     inp->inp_lport == lport) {
  829                         /*
  830                          * XXX We should be able to directly return
  831                          * the inp here, without any checks.
  832                          * Well unless both bound with SO_REUSEPORT?
  833                          */
  834                         if (jailed(inp->inp_cred))
  835                                 return (inp);
  836                         if (tmpinp == NULL)
  837                                 tmpinp = inp;
  838                 }
  839         }
  840         if (tmpinp != NULL)
  841                 return (tmpinp);
  842 
  843         /*
  844          * Then look for a wildcard match, if requested.
  845          */
  846         if (wildcard == INPLOOKUP_WILDCARD) {
  847                 struct inpcb *local_wild = NULL, *local_exact = NULL;
  848                 struct inpcb *jail_wild = NULL;
  849                 int injail;
  850 
  851                 /*
  852                  * Order of socket selection - we always prefer jails.
  853                  *      1. jailed, non-wild.
  854                  *      2. jailed, wild.
  855                  *      3. non-jailed, non-wild.
  856                  *      4. non-jailed, wild.
  857                  */
  858                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
  859                     0, pcbinfo->ipi_hashmask)];
  860                 LIST_FOREACH(inp, head, inp_hash) {
  861                         /* XXX inp locking */
  862                         if ((inp->inp_vflag & INP_IPV6) == 0)
  863                                 continue;
  864 
  865                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
  866                             inp->inp_lport != lport) {
  867                                 continue;
  868                         }
  869 
  870                         /* XXX inp locking */
  871                         if (faith && (inp->inp_flags & INP_FAITH) == 0)
  872                                 continue;
  873 
  874                         injail = jailed(inp->inp_cred);
  875                         if (injail) {
  876                                 if (prison_check_ip6(inp->inp_cred,
  877                                     laddr) != 0)
  878                                         continue;
  879                         } else {
  880                                 if (local_exact != NULL)
  881                                         continue;
  882                         }
  883 
  884                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
  885                                 if (injail)
  886                                         return (inp);
  887                                 else
  888                                         local_exact = inp;
  889                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  890                                 if (injail)
  891                                         jail_wild = inp;
  892                                 else
  893                                         local_wild = inp;
  894                         }
  895                 } /* LIST_FOREACH */
  896 
  897                 if (jail_wild != NULL)
  898                         return (jail_wild);
  899                 if (local_exact != NULL)
  900                         return (local_exact);
  901                 if (local_wild != NULL)
  902                         return (local_wild);
  903         } /* if (wildcard == INPLOOKUP_WILDCARD) */
  904 
  905         /*
  906          * Not found.
  907          */
  908         return (NULL);
  909 }
  910 
  911 void
  912 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
  913 {
  914         struct ip6_hdr *ip;
  915 
  916         ip = mtod(m, struct ip6_hdr *);
  917         bzero(sin6, sizeof(*sin6));
  918         sin6->sin6_len = sizeof(*sin6);
  919         sin6->sin6_family = AF_INET6;
  920         sin6->sin6_addr = ip->ip6_src;
  921 
  922         (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
  923 
  924         return;
  925 }

Cache object: aafcbe59f984398842ab772ca3559da9


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