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                         /* Undo an address bind that may have occurred. */
  257                         inp->in6p_laddr = in6addr_any;
  258                         return (error);
  259                 }
  260         } else {
  261                 inp->inp_lport = lport;
  262                 if (in_pcbinshash(inp) != 0) {
  263                         inp->in6p_laddr = in6addr_any;
  264                         inp->inp_lport = 0;
  265                         return (EAGAIN);
  266                 }
  267         }
  268         return (0);
  269 }
  270 
  271 /*
  272  *   Transform old in6_pcbconnect() into an inner subroutine for new
  273  *   in6_pcbconnect(): Do some validity-checking on the remote
  274  *   address (in mbuf 'nam') and then determine local host address
  275  *   (i.e., which interface) to use to access that remote host.
  276  *
  277  *   This preserves definition of in6_pcbconnect(), while supporting a
  278  *   slightly different version for T/TCP.  (This is more than
  279  *   a bit of a kludge, but cleaning up the internal interfaces would
  280  *   have forced minor changes in every protocol).
  281  */
  282 int
  283 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
  284     struct in6_addr *plocal_addr6)
  285 {
  286         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  287         int error = 0;
  288         struct ifnet *ifp = NULL;
  289         int scope_ambiguous = 0;
  290         struct in6_addr in6a;
  291 
  292         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  293         INP_WLOCK_ASSERT(inp);
  294 
  295         if (nam->sa_len != sizeof (*sin6))
  296                 return (EINVAL);
  297         if (sin6->sin6_family != AF_INET6)
  298                 return (EAFNOSUPPORT);
  299         if (sin6->sin6_port == 0)
  300                 return (EADDRNOTAVAIL);
  301 
  302         if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
  303                 scope_ambiguous = 1;
  304         if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
  305                 return(error);
  306 
  307         if (in6_ifaddr) {
  308                 /*
  309                  * If the destination address is UNSPECIFIED addr,
  310                  * use the loopback addr, e.g ::1.
  311                  */
  312                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  313                         sin6->sin6_addr = in6addr_loopback;
  314         }
  315         if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
  316                 return (error);
  317 
  318         error = in6_selectsrc(sin6, inp->in6p_outputopts,
  319             inp, NULL, inp->inp_cred, &ifp, &in6a);
  320         if (error)
  321                 return (error);
  322 
  323         if (ifp && scope_ambiguous &&
  324             (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
  325                 return(error);
  326         }
  327 
  328         /*
  329          * Do not update this earlier, in case we return with an error.
  330          *
  331          * XXX: this in6_selectsrc result might replace the bound local
  332          * address with the address specified by setsockopt(IPV6_PKTINFO).
  333          * Is it the intended behavior?
  334          */
  335         *plocal_addr6 = in6a;
  336 
  337         /*
  338          * Don't do pcblookup call here; return interface in
  339          * plocal_addr6
  340          * and exit to caller, that will do the lookup.
  341          */
  342 
  343         return (0);
  344 }
  345 
  346 /*
  347  * Outer subroutine:
  348  * Connect from a socket to a specified address.
  349  * Both address and port must be specified in argument sin.
  350  * If don't have a local address for this socket yet,
  351  * then pick one.
  352  */
  353 int
  354 in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
  355     struct ucred *cred)
  356 {
  357         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  358         struct in6_addr addr6;
  359         int error;
  360 
  361         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  362         INP_WLOCK_ASSERT(inp);
  363 
  364         /*
  365          * Call inner routine, to assign local interface address.
  366          * in6_pcbladdr() may automatically fill in sin6_scope_id.
  367          */
  368         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
  369                 return (error);
  370 
  371         if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
  372                                sin6->sin6_port,
  373                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
  374                               ? &addr6 : &inp->in6p_laddr,
  375                               inp->inp_lport, 0, NULL) != NULL) {
  376                 return (EADDRINUSE);
  377         }
  378         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  379                 if (inp->inp_lport == 0) {
  380                         error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
  381                         if (error)
  382                                 return (error);
  383                 }
  384                 inp->in6p_laddr = addr6;
  385         }
  386         inp->in6p_faddr = sin6->sin6_addr;
  387         inp->inp_fport = sin6->sin6_port;
  388         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  389         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
  390         if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
  391                 inp->inp_flow |=
  392                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
  393 
  394         in_pcbrehash(inp);
  395 
  396         return (0);
  397 }
  398 
  399 void
  400 in6_pcbdisconnect(struct inpcb *inp)
  401 {
  402 
  403         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  404         INP_WLOCK_ASSERT(inp);
  405 
  406         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
  407         inp->inp_fport = 0;
  408         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  409         inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
  410         in_pcbrehash(inp);
  411 }
  412 
  413 struct sockaddr *
  414 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
  415 {
  416         struct sockaddr_in6 *sin6;
  417 
  418         MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
  419         bzero(sin6, sizeof *sin6);
  420         sin6->sin6_family = AF_INET6;
  421         sin6->sin6_len = sizeof(*sin6);
  422         sin6->sin6_port = port;
  423         sin6->sin6_addr = *addr_p;
  424         (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
  425 
  426         return (struct sockaddr *)sin6;
  427 }
  428 
  429 struct sockaddr *
  430 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
  431 {
  432         struct sockaddr_in sin;
  433         struct sockaddr_in6 *sin6_p;
  434 
  435         bzero(&sin, sizeof sin);
  436         sin.sin_family = AF_INET;
  437         sin.sin_len = sizeof(sin);
  438         sin.sin_port = port;
  439         sin.sin_addr = *addr_p;
  440 
  441         MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
  442                 M_WAITOK);
  443         in6_sin_2_v4mapsin6(&sin, sin6_p);
  444 
  445         return (struct sockaddr *)sin6_p;
  446 }
  447 
  448 int
  449 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
  450 {
  451         register struct inpcb *inp;
  452         struct in6_addr addr;
  453         in_port_t port;
  454 
  455         inp = sotoinpcb(so);
  456         KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
  457 
  458         INP_RLOCK(inp);
  459         port = inp->inp_lport;
  460         addr = inp->in6p_laddr;
  461         INP_RUNLOCK(inp);
  462 
  463         *nam = in6_sockaddr(port, &addr);
  464         return 0;
  465 }
  466 
  467 int
  468 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
  469 {
  470         struct inpcb *inp;
  471         struct in6_addr addr;
  472         in_port_t port;
  473 
  474         inp = sotoinpcb(so);
  475         KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
  476 
  477         INP_RLOCK(inp);
  478         port = inp->inp_fport;
  479         addr = inp->in6p_faddr;
  480         INP_RUNLOCK(inp);
  481 
  482         *nam = in6_sockaddr(port, &addr);
  483         return 0;
  484 }
  485 
  486 int
  487 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
  488 {
  489         struct  inpcb *inp;
  490         int     error;
  491 
  492         inp = sotoinpcb(so);
  493         KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
  494 
  495         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  496                 error = in_getsockaddr(so, nam);
  497                 if (error == 0)
  498                         in6_sin_2_v4mapsin6_in_sock(nam);
  499         } else {
  500                 /* scope issues will be handled in in6_getsockaddr(). */
  501                 error = in6_getsockaddr(so, nam);
  502         }
  503 
  504         return error;
  505 }
  506 
  507 int
  508 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
  509 {
  510         struct  inpcb *inp;
  511         int     error;
  512 
  513         inp = sotoinpcb(so);
  514         KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
  515 
  516         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  517                 error = in_getpeeraddr(so, nam);
  518                 if (error == 0)
  519                         in6_sin_2_v4mapsin6_in_sock(nam);
  520         } else
  521         /* scope issues will be handled in in6_getpeeraddr(). */
  522         error = in6_getpeeraddr(so, nam);
  523 
  524         return error;
  525 }
  526 
  527 /*
  528  * Pass some notification to all connections of a protocol
  529  * associated with address dst.  The local address and/or port numbers
  530  * may be specified to limit the search.  The "usual action" will be
  531  * taken, depending on the ctlinput cmd.  The caller must filter any
  532  * cmds that are uninteresting (e.g., no error in the map).
  533  * Call the protocol specific routine (if any) to report
  534  * any errors for each matching socket.
  535  */
  536 void
  537 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
  538     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
  539     int cmd, void *cmdarg,
  540     struct inpcb *(*notify)(struct inpcb *, int))
  541 {
  542         struct inpcb *inp, *inp_temp;
  543         struct sockaddr_in6 sa6_src, *sa6_dst;
  544         u_short fport = fport_arg, lport = lport_arg;
  545         u_int32_t flowinfo;
  546         int errno;
  547 
  548         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
  549                 return;
  550 
  551         sa6_dst = (struct sockaddr_in6 *)dst;
  552         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
  553                 return;
  554 
  555         /*
  556          * note that src can be NULL when we get notify by local fragmentation.
  557          */
  558         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
  559         flowinfo = sa6_src.sin6_flowinfo;
  560 
  561         /*
  562          * Redirects go to all references to the destination,
  563          * and use in6_rtchange to invalidate the route cache.
  564          * Dead host indications: also use in6_rtchange to invalidate
  565          * the cache, and deliver the error to all the sockets.
  566          * Otherwise, if we have knowledge of the local port and address,
  567          * deliver only to that socket.
  568          */
  569         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
  570                 fport = 0;
  571                 lport = 0;
  572                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
  573 
  574                 if (cmd != PRC_HOSTDEAD)
  575                         notify = in6_rtchange;
  576         }
  577         errno = inet6ctlerrmap[cmd];
  578         INP_INFO_WLOCK(pcbinfo);
  579         LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
  580                 INP_WLOCK(inp);
  581                 if ((inp->inp_vflag & INP_IPV6) == 0) {
  582                         INP_WUNLOCK(inp);
  583                         continue;
  584                 }
  585 
  586                 /*
  587                  * If the error designates a new path MTU for a destination
  588                  * and the application (associated with this socket) wanted to
  589                  * know the value, notify. Note that we notify for all
  590                  * disconnected sockets if the corresponding application
  591                  * wanted. This is because some UDP applications keep sending
  592                  * sockets disconnected.
  593                  * XXX: should we avoid to notify the value to TCP sockets?
  594                  */
  595                 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
  596                     (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
  597                      IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
  598                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
  599                                         (u_int32_t *)cmdarg);
  600                 }
  601 
  602                 /*
  603                  * Detect if we should notify the error. If no source and
  604                  * destination ports are specifed, but non-zero flowinfo and
  605                  * local address match, notify the error. This is the case
  606                  * when the error is delivered with an encrypted buffer
  607                  * by ESP. Otherwise, just compare addresses and ports
  608                  * as usual.
  609                  */
  610                 if (lport == 0 && fport == 0 && flowinfo &&
  611                     inp->inp_socket != NULL &&
  612                     flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
  613                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
  614                         goto do_notify;
  615                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
  616                                              &sa6_dst->sin6_addr) ||
  617                          inp->inp_socket == 0 ||
  618                          (lport && inp->inp_lport != lport) ||
  619                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
  620                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  621                                               &sa6_src.sin6_addr)) ||
  622                          (fport && inp->inp_fport != fport)) {
  623                         INP_WUNLOCK(inp);
  624                         continue;
  625                 }
  626 
  627           do_notify:
  628                 if (notify) {
  629                         if ((*notify)(inp, errno))
  630                                 INP_WUNLOCK(inp);
  631                 } else
  632                         INP_WUNLOCK(inp);
  633         }
  634         INP_INFO_WUNLOCK(pcbinfo);
  635 }
  636 
  637 /*
  638  * Lookup a PCB based on the local address and port.
  639  */
  640 struct inpcb *
  641 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
  642     u_short lport, int wild_okay, struct ucred *cred)
  643 {
  644         register struct inpcb *inp;
  645         int matchwild = 3, wildcard;
  646 
  647         INP_INFO_WLOCK_ASSERT(pcbinfo);
  648 
  649         if (!wild_okay) {
  650                 struct inpcbhead *head;
  651                 /*
  652                  * Look for an unconnected (wildcard foreign addr) PCB that
  653                  * matches the local address and port we're looking for.
  654                  */
  655                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
  656                     0, pcbinfo->ipi_hashmask)];
  657                 LIST_FOREACH(inp, head, inp_hash) {
  658                         /* XXX inp locking */
  659                         if ((inp->inp_vflag & INP_IPV6) == 0)
  660                                 continue;
  661                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
  662                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  663                             inp->inp_lport == lport) {
  664                                 /* Found. */
  665                                 if (cred == NULL ||
  666                                     inp->inp_cred->cr_prison == cred->cr_prison)
  667                                         return (inp);
  668                         }
  669                 }
  670                 /*
  671                  * Not found.
  672                  */
  673                 return (NULL);
  674         } else {
  675                 struct inpcbporthead *porthash;
  676                 struct inpcbport *phd;
  677                 struct inpcb *match = NULL;
  678                 /*
  679                  * Best fit PCB lookup.
  680                  *
  681                  * First see if this local port is in use by looking on the
  682                  * port hash list.
  683                  */
  684                 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
  685                     pcbinfo->ipi_porthashmask)];
  686                 LIST_FOREACH(phd, porthash, phd_hash) {
  687                         if (phd->phd_port == lport)
  688                                 break;
  689                 }
  690                 if (phd != NULL) {
  691                         /*
  692                          * Port is in use by one or more PCBs. Look for best
  693                          * fit.
  694                          */
  695                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
  696                                 wildcard = 0;
  697                                 if (cred != NULL &&
  698                                     inp->inp_cred->cr_prison != cred->cr_prison)
  699                                         continue;
  700                                 /* XXX inp locking */
  701                                 if ((inp->inp_vflag & INP_IPV6) == 0)
  702                                         continue;
  703                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
  704                                         wildcard++;
  705                                 if (!IN6_IS_ADDR_UNSPECIFIED(
  706                                         &inp->in6p_laddr)) {
  707                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
  708                                                 wildcard++;
  709                                         else if (!IN6_ARE_ADDR_EQUAL(
  710                                             &inp->in6p_laddr, laddr))
  711                                                 continue;
  712                                 } else {
  713                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
  714                                                 wildcard++;
  715                                 }
  716                                 if (wildcard < matchwild) {
  717                                         match = inp;
  718                                         matchwild = wildcard;
  719                                         if (matchwild == 0)
  720                                                 break;
  721                                 }
  722                         }
  723                 }
  724                 return (match);
  725         }
  726 }
  727 
  728 void
  729 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
  730 {
  731         struct inpcb *in6p;
  732         struct ip6_moptions *im6o;
  733         struct in6_multi_mship *imm, *nimm;
  734 
  735         INP_INFO_RLOCK(pcbinfo);
  736         LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
  737                 INP_WLOCK(in6p);
  738                 im6o = in6p->in6p_moptions;
  739                 if ((in6p->inp_vflag & INP_IPV6) &&
  740                     im6o) {
  741                         /*
  742                          * Unselect the outgoing interface if it is being
  743                          * detached.
  744                          */
  745                         if (im6o->im6o_multicast_ifp == ifp)
  746                                 im6o->im6o_multicast_ifp = NULL;
  747 
  748                         /*
  749                          * Drop multicast group membership if we joined
  750                          * through the interface being detached.
  751                          * XXX controversial - is it really legal for kernel
  752                          * to force this?
  753                          */
  754                         for (imm = im6o->im6o_memberships.lh_first;
  755                              imm != NULL; imm = nimm) {
  756                                 nimm = imm->i6mm_chain.le_next;
  757                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
  758                                         LIST_REMOVE(imm, i6mm_chain);
  759                                         in6_delmulti(imm->i6mm_maddr);
  760                                         free(imm, M_IP6MADDR);
  761                                 }
  762                         }
  763                 }
  764                 INP_WUNLOCK(in6p);
  765         }
  766         INP_INFO_RUNLOCK(pcbinfo);
  767 }
  768 
  769 /*
  770  * Check for alternatives when higher level complains
  771  * about service problems.  For now, invalidate cached
  772  * routing information.  If the route was created dynamically
  773  * (by a redirect), time to try a default gateway again.
  774  */
  775 void
  776 in6_losing(struct inpcb *in6p)
  777 {
  778 
  779         /*
  780          * We don't store route pointers in the routing table anymore
  781          */
  782         return;
  783 }
  784 
  785 /*
  786  * After a routing change, flush old routing
  787  * and allocate a (hopefully) better one.
  788  */
  789 struct inpcb *
  790 in6_rtchange(struct inpcb *inp, int errno)
  791 {
  792         /*
  793          * We don't store route pointers in the routing table anymore
  794          */
  795         return inp;
  796 }
  797 
  798 /*
  799  * Lookup PCB in hash list.
  800  */
  801 struct inpcb *
  802 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
  803     u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
  804     struct ifnet *ifp)
  805 {
  806         struct inpcbhead *head;
  807         struct inpcb *inp, *tmpinp;
  808         u_short fport = fport_arg, lport = lport_arg;
  809         int faith;
  810 
  811         INP_INFO_LOCK_ASSERT(pcbinfo);
  812 
  813         if (faithprefix_p != NULL)
  814                 faith = (*faithprefix_p)(laddr);
  815         else
  816                 faith = 0;
  817 
  818         /*
  819          * First look for an exact match.
  820          */
  821         tmpinp = NULL;
  822         head = &pcbinfo->ipi_hashbase[
  823             INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
  824             pcbinfo->ipi_hashmask)];
  825         LIST_FOREACH(inp, head, inp_hash) {
  826                 /* XXX inp locking */
  827                 if ((inp->inp_vflag & INP_IPV6) == 0)
  828                         continue;
  829                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
  830                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  831                     inp->inp_fport == fport &&
  832                     inp->inp_lport == lport) {
  833                         /*
  834                          * XXX We should be able to directly return
  835                          * the inp here, without any checks.
  836                          * Well unless both bound with SO_REUSEPORT?
  837                          */
  838                         if (jailed(inp->inp_cred))
  839                                 return (inp);
  840                         if (tmpinp == NULL)
  841                                 tmpinp = inp;
  842                 }
  843         }
  844         if (tmpinp != NULL)
  845                 return (tmpinp);
  846 
  847         /*
  848          * Then look for a wildcard match, if requested.
  849          */
  850         if (wildcard == INPLOOKUP_WILDCARD) {
  851                 struct inpcb *local_wild = NULL, *local_exact = NULL;
  852                 struct inpcb *jail_wild = NULL;
  853                 int injail;
  854 
  855                 /*
  856                  * Order of socket selection - we always prefer jails.
  857                  *      1. jailed, non-wild.
  858                  *      2. jailed, wild.
  859                  *      3. non-jailed, non-wild.
  860                  *      4. non-jailed, wild.
  861                  */
  862                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
  863                     0, pcbinfo->ipi_hashmask)];
  864                 LIST_FOREACH(inp, head, inp_hash) {
  865                         /* XXX inp locking */
  866                         if ((inp->inp_vflag & INP_IPV6) == 0)
  867                                 continue;
  868 
  869                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
  870                             inp->inp_lport != lport) {
  871                                 continue;
  872                         }
  873 
  874                         /* XXX inp locking */
  875                         if (faith && (inp->inp_flags & INP_FAITH) == 0)
  876                                 continue;
  877 
  878                         injail = jailed(inp->inp_cred);
  879                         if (injail) {
  880                                 if (prison_check_ip6(inp->inp_cred,
  881                                     laddr) != 0)
  882                                         continue;
  883                         } else {
  884                                 if (local_exact != NULL)
  885                                         continue;
  886                         }
  887 
  888                         if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
  889                                 if (injail)
  890                                         return (inp);
  891                                 else
  892                                         local_exact = inp;
  893                         } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  894                                 if (injail)
  895                                         jail_wild = inp;
  896                                 else
  897                                         local_wild = inp;
  898                         }
  899                 } /* LIST_FOREACH */
  900 
  901                 if (jail_wild != NULL)
  902                         return (jail_wild);
  903                 if (local_exact != NULL)
  904                         return (local_exact);
  905                 if (local_wild != NULL)
  906                         return (local_wild);
  907         } /* if (wildcard == INPLOOKUP_WILDCARD) */
  908 
  909         /*
  910          * Not found.
  911          */
  912         return (NULL);
  913 }
  914 
  915 void
  916 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
  917 {
  918         struct ip6_hdr *ip;
  919 
  920         ip = mtod(m, struct ip6_hdr *);
  921         bzero(sin6, sizeof(*sin6));
  922         sin6->sin6_len = sizeof(*sin6);
  923         sin6->sin6_family = AF_INET6;
  924         sin6->sin6_addr = ip->ip6_src;
  925 
  926         (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
  927 
  928         return;
  929 }

Cache object: 53186dfa76e894ae4da46faaf0346886


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