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/scope6.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) 2000 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: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/10.0/sys/netinet6/scope6.c 245244 2013-01-10 00:10:24Z ae $");
   34 
   35 #include <sys/param.h>
   36 #include <sys/malloc.h>
   37 #include <sys/mbuf.h>
   38 #include <sys/socket.h>
   39 #include <sys/systm.h>
   40 #include <sys/queue.h>
   41 #include <sys/sysctl.h>
   42 #include <sys/syslog.h>
   43 
   44 #include <net/if.h>
   45 #include <net/vnet.h>
   46 
   47 #include <netinet/in.h>
   48 
   49 #include <netinet/ip6.h>
   50 #include <netinet6/in6_var.h>
   51 #include <netinet6/ip6_var.h>
   52 #include <netinet6/scope6_var.h>
   53 
   54 #ifdef ENABLE_DEFAULT_SCOPE
   55 VNET_DEFINE(int, ip6_use_defzone) = 1;
   56 #else
   57 VNET_DEFINE(int, ip6_use_defzone) = 0;
   58 #endif
   59 VNET_DEFINE(int, deembed_scopeid) = 1;
   60 SYSCTL_DECL(_net_inet6_ip6);
   61 SYSCTL_VNET_INT(_net_inet6_ip6, OID_AUTO, deembed_scopeid, CTLFLAG_RW,
   62     &VNET_NAME(deembed_scopeid), 0,
   63     "Extract embedded zone ID and set it to sin6_scope_id in sockaddr_in6.");
   64 
   65 /*
   66  * The scope6_lock protects the global sid default stored in
   67  * sid_default below.
   68  */
   69 static struct mtx scope6_lock;
   70 #define SCOPE6_LOCK_INIT()      mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
   71 #define SCOPE6_LOCK()           mtx_lock(&scope6_lock)
   72 #define SCOPE6_UNLOCK()         mtx_unlock(&scope6_lock)
   73 #define SCOPE6_LOCK_ASSERT()    mtx_assert(&scope6_lock, MA_OWNED)
   74 
   75 static VNET_DEFINE(struct scope6_id, sid_default);
   76 #define V_sid_default                   VNET(sid_default)
   77 
   78 #define SID(ifp) \
   79         (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
   80 
   81 void
   82 scope6_init(void)
   83 {
   84 
   85         bzero(&V_sid_default, sizeof(V_sid_default));
   86 
   87         if (!IS_DEFAULT_VNET(curvnet))
   88                 return;
   89 
   90         SCOPE6_LOCK_INIT();
   91 }
   92 
   93 struct scope6_id *
   94 scope6_ifattach(struct ifnet *ifp)
   95 {
   96         struct scope6_id *sid;
   97 
   98         sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
   99         bzero(sid, sizeof(*sid));
  100 
  101         /*
  102          * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
  103          * Should we rather hardcode here?
  104          */
  105         sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
  106         sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
  107 #ifdef MULTI_SCOPE
  108         /* by default, we don't care about scope boundary for these scopes. */
  109         sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
  110         sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
  111 #endif
  112 
  113         return sid;
  114 }
  115 
  116 void
  117 scope6_ifdetach(struct scope6_id *sid)
  118 {
  119 
  120         free(sid, M_IFADDR);
  121 }
  122 
  123 int
  124 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
  125 {
  126         int i;
  127         int error = 0;
  128         struct scope6_id *sid = NULL;
  129 
  130         IF_AFDATA_WLOCK(ifp);
  131         sid = SID(ifp);
  132 
  133         if (!sid) {     /* paranoid? */
  134                 IF_AFDATA_WUNLOCK(ifp);
  135                 return (EINVAL);
  136         }
  137 
  138         /*
  139          * XXX: We need more consistency checks of the relationship among
  140          * scopes (e.g. an organization should be larger than a site).
  141          */
  142 
  143         /*
  144          * TODO(XXX): after setting, we should reflect the changes to
  145          * interface addresses, routing table entries, PCB entries...
  146          */
  147 
  148         for (i = 0; i < 16; i++) {
  149                 if (idlist->s6id_list[i] &&
  150                     idlist->s6id_list[i] != sid->s6id_list[i]) {
  151                         /*
  152                          * An interface zone ID must be the corresponding
  153                          * interface index by definition.
  154                          */
  155                         if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
  156                             idlist->s6id_list[i] != ifp->if_index) {
  157                                 IF_AFDATA_WUNLOCK(ifp);
  158                                 return (EINVAL);
  159                         }
  160 
  161                         if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
  162                             idlist->s6id_list[i] > V_if_index) {
  163                                 /*
  164                                  * XXX: theoretically, there should be no
  165                                  * relationship between link IDs and interface
  166                                  * IDs, but we check the consistency for
  167                                  * safety in later use.
  168                                  */
  169                                 IF_AFDATA_WUNLOCK(ifp);
  170                                 return (EINVAL);
  171                         }
  172 
  173                         /*
  174                          * XXX: we must need lots of work in this case,
  175                          * but we simply set the new value in this initial
  176                          * implementation.
  177                          */
  178                         sid->s6id_list[i] = idlist->s6id_list[i];
  179                 }
  180         }
  181         IF_AFDATA_WUNLOCK(ifp);
  182 
  183         return (error);
  184 }
  185 
  186 int
  187 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
  188 {
  189         struct scope6_id *sid;
  190 
  191         /* We only need to lock the interface's afdata for SID() to work. */
  192         IF_AFDATA_RLOCK(ifp);
  193         sid = SID(ifp);
  194         if (sid == NULL) {      /* paranoid? */
  195                 IF_AFDATA_RUNLOCK(ifp);
  196                 return (EINVAL);
  197         }
  198 
  199         *idlist = *sid;
  200 
  201         IF_AFDATA_RUNLOCK(ifp);
  202         return (0);
  203 }
  204 
  205 
  206 /*
  207  * Get a scope of the address. Node-local, link-local, site-local or global.
  208  */
  209 int
  210 in6_addrscope(struct in6_addr *addr)
  211 {
  212         int scope;
  213 
  214         if (addr->s6_addr[0] == 0xfe) {
  215                 scope = addr->s6_addr[1] & 0xc0;
  216 
  217                 switch (scope) {
  218                 case 0x80:
  219                         return IPV6_ADDR_SCOPE_LINKLOCAL;
  220                         break;
  221                 case 0xc0:
  222                         return IPV6_ADDR_SCOPE_SITELOCAL;
  223                         break;
  224                 default:
  225                         return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
  226                         break;
  227                 }
  228         }
  229 
  230 
  231         if (addr->s6_addr[0] == 0xff) {
  232                 scope = addr->s6_addr[1] & 0x0f;
  233 
  234                 /*
  235                  * due to other scope such as reserved,
  236                  * return scope doesn't work.
  237                  */
  238                 switch (scope) {
  239                 case IPV6_ADDR_SCOPE_INTFACELOCAL:
  240                         return IPV6_ADDR_SCOPE_INTFACELOCAL;
  241                         break;
  242                 case IPV6_ADDR_SCOPE_LINKLOCAL:
  243                         return IPV6_ADDR_SCOPE_LINKLOCAL;
  244                         break;
  245                 case IPV6_ADDR_SCOPE_SITELOCAL:
  246                         return IPV6_ADDR_SCOPE_SITELOCAL;
  247                         break;
  248                 default:
  249                         return IPV6_ADDR_SCOPE_GLOBAL;
  250                         break;
  251                 }
  252         }
  253 
  254         /*
  255          * Regard loopback and unspecified addresses as global, since
  256          * they have no ambiguity.
  257          */
  258         if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
  259                 if (addr->s6_addr[15] == 1) /* loopback */
  260                         return IPV6_ADDR_SCOPE_LINKLOCAL;
  261                 if (addr->s6_addr[15] == 0) /* unspecified */
  262                         return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
  263         }
  264 
  265         return IPV6_ADDR_SCOPE_GLOBAL;
  266 }
  267 
  268 /*
  269  * ifp - note that this might be NULL
  270  */
  271 
  272 void
  273 scope6_setdefault(struct ifnet *ifp)
  274 {
  275 
  276         /*
  277          * Currently, this function just sets the default "interfaces"
  278          * and "links" according to the given interface.
  279          * We might eventually have to separate the notion of "link" from
  280          * "interface" and provide a user interface to set the default.
  281          */
  282         SCOPE6_LOCK();
  283         if (ifp) {
  284                 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
  285                         ifp->if_index;
  286                 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
  287                         ifp->if_index;
  288         } else {
  289                 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
  290                 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
  291         }
  292         SCOPE6_UNLOCK();
  293 }
  294 
  295 int
  296 scope6_get_default(struct scope6_id *idlist)
  297 {
  298 
  299         SCOPE6_LOCK();
  300         *idlist = V_sid_default;
  301         SCOPE6_UNLOCK();
  302 
  303         return (0);
  304 }
  305 
  306 u_int32_t
  307 scope6_addr2default(struct in6_addr *addr)
  308 {
  309         u_int32_t id;
  310 
  311         /*
  312          * special case: The loopback address should be considered as
  313          * link-local, but there's no ambiguity in the syntax.
  314          */
  315         if (IN6_IS_ADDR_LOOPBACK(addr))
  316                 return (0);
  317 
  318         /*
  319          * XXX: 32-bit read is atomic on all our platforms, is it OK
  320          * not to lock here?
  321          */
  322         SCOPE6_LOCK();
  323         id = V_sid_default.s6id_list[in6_addrscope(addr)];
  324         SCOPE6_UNLOCK();
  325         return (id);
  326 }
  327 
  328 /*
  329  * Validate the specified scope zone ID in the sin6_scope_id field.  If the ID
  330  * is unspecified (=0), needs to be specified, and the default zone ID can be
  331  * used, the default value will be used.
  332  * This routine then generates the kernel-internal form: if the address scope
  333  * of is interface-local or link-local, embed the interface index in the
  334  * address.
  335  */
  336 int
  337 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
  338 {
  339         u_int32_t zoneid;
  340 
  341         if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
  342                 zoneid = scope6_addr2default(&sin6->sin6_addr);
  343 
  344         if (zoneid != 0 &&
  345             (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
  346             IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
  347                 /*
  348                  * At this moment, we only check interface-local and
  349                  * link-local scope IDs, and use interface indices as the
  350                  * zone IDs assuming a one-to-one mapping between interfaces
  351                  * and links.
  352                  */
  353                 if (V_if_index < zoneid || ifnet_byindex(zoneid) == NULL)
  354                         return (ENXIO);
  355 
  356                 /* XXX assignment to 16bit from 32bit variable */
  357                 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
  358                 sin6->sin6_scope_id = 0;
  359         }
  360 
  361         return 0;
  362 }
  363 
  364 /*
  365  * generate standard sockaddr_in6 from embedded form.
  366  */
  367 int
  368 sa6_recoverscope(struct sockaddr_in6 *sin6)
  369 {
  370         char ip6buf[INET6_ADDRSTRLEN];
  371         u_int32_t zoneid;
  372 
  373         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
  374             IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
  375                 /*
  376                  * KAME assumption: link id == interface id
  377                  */
  378                 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
  379                 if (zoneid) {
  380                         /* sanity check */
  381                         if (V_if_index < zoneid)
  382                                 return (ENXIO);
  383 #if 0
  384                         /* XXX: Disabled due to possible deadlock. */
  385                         if (!ifnet_byindex(zoneid))
  386                                 return (ENXIO);
  387 #endif
  388                         if (sin6->sin6_scope_id != 0 &&
  389                             zoneid != sin6->sin6_scope_id) {
  390                                 log(LOG_NOTICE,
  391                                     "%s: embedded scope mismatch: %s%%%d. "
  392                                     "sin6_scope_id was overridden.", __func__,
  393                                     ip6_sprintf(ip6buf, &sin6->sin6_addr),
  394                                     sin6->sin6_scope_id);
  395                         }
  396                         sin6->sin6_addr.s6_addr16[1] = 0;
  397                         sin6->sin6_scope_id = zoneid;
  398                 }
  399         }
  400 
  401         return 0;
  402 }
  403 
  404 /*
  405  * Determine the appropriate scope zone ID for in6 and ifp.  If ret_id is
  406  * non NULL, it is set to the zone ID.  If the zone ID needs to be embedded
  407  * in the in6_addr structure, in6 will be modified.
  408  *
  409  * ret_id - unnecessary?
  410  */
  411 int
  412 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
  413 {
  414         int scope;
  415         u_int32_t zoneid = 0;
  416         struct scope6_id *sid;
  417 
  418         /*
  419          * special case: the loopback address can only belong to a loopback
  420          * interface.
  421          */
  422         if (IN6_IS_ADDR_LOOPBACK(in6)) {
  423                 if (!(ifp->if_flags & IFF_LOOPBACK))
  424                         return (EINVAL);
  425         } else {
  426                 scope = in6_addrscope(in6);
  427                 if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
  428                     scope == IPV6_ADDR_SCOPE_LINKLOCAL) {
  429                         /*
  430                          * Currently we use interface indeces as the
  431                          * zone IDs for interface-local and link-local
  432                          * scopes.
  433                          */
  434                         zoneid = ifp->if_index;
  435                         in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
  436                 } else if (scope != IPV6_ADDR_SCOPE_GLOBAL) {
  437                         IF_AFDATA_RLOCK(ifp);
  438                         sid = SID(ifp);
  439                         zoneid = sid->s6id_list[scope];
  440                         IF_AFDATA_RUNLOCK(ifp);
  441                 }
  442         }
  443 
  444         if (ret_id != NULL)
  445                 *ret_id = zoneid;
  446 
  447         return (0);
  448 }
  449 
  450 /*
  451  * Just clear the embedded scope identifier.  Return 0 if the original address
  452  * is intact; return non 0 if the address is modified.
  453  */
  454 int
  455 in6_clearscope(struct in6_addr *in6)
  456 {
  457         int modified = 0;
  458 
  459         if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
  460                 if (in6->s6_addr16[1] != 0)
  461                         modified = 1;
  462                 in6->s6_addr16[1] = 0;
  463         }
  464 
  465         return (modified);
  466 }
  467 
  468 /*
  469  * Return the scope identifier or zero.
  470  */
  471 uint16_t
  472 in6_getscope(struct in6_addr *in6)
  473 {
  474 
  475         if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
  476                 return (in6->s6_addr16[1]);
  477 
  478         return (0);
  479 }

Cache object: 45b4b69b3acc41e5996b9426666e25e4


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