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_ifattach.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_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include <sys/param.h>
   36 #include <sys/systm.h>
   37 #include <sys/malloc.h>
   38 #include <sys/socket.h>
   39 #include <sys/sockio.h>
   40 #include <sys/kernel.h>
   41 #include <sys/syslog.h>
   42 #include <sys/md5.h>
   43 
   44 #include <net/if.h>
   45 #include <net/if_dl.h>
   46 #include <net/if_types.h>
   47 #include <net/route.h>
   48 
   49 #include <netinet/in.h>
   50 #include <netinet/in_var.h>
   51 #include <netinet/if_ether.h>
   52 #include <netinet/in_pcb.h>
   53 
   54 #include <netinet/ip6.h>
   55 #include <netinet6/ip6_var.h>
   56 #include <netinet6/in6_var.h>
   57 #include <netinet6/in6_pcb.h>
   58 #include <netinet6/in6_ifattach.h>
   59 #include <netinet6/ip6_var.h>
   60 #include <netinet6/nd6.h>
   61 #include <netinet6/scope6_var.h>
   62 
   63 #include <net/net_osdep.h>
   64 
   65 unsigned long in6_maxmtu = 0;
   66 
   67 #ifdef IP6_AUTO_LINKLOCAL
   68 int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
   69 #else
   70 int ip6_auto_linklocal = 1;     /* enable by default */
   71 #endif
   72 
   73 struct callout in6_tmpaddrtimer_ch;
   74 
   75 extern struct inpcbinfo udbinfo;
   76 extern struct inpcbinfo ripcbinfo;
   77 
   78 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
   79 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
   80 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
   81 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
   82 static int in6_ifattach_loopback(struct ifnet *);
   83 
   84 #define EUI64_GBIT      0x01
   85 #define EUI64_UBIT      0x02
   86 #define EUI64_TO_IFID(in6)      do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
   87 #define EUI64_GROUP(in6)        ((in6)->s6_addr[8] & EUI64_GBIT)
   88 #define EUI64_INDIVIDUAL(in6)   (!EUI64_GROUP(in6))
   89 #define EUI64_LOCAL(in6)        ((in6)->s6_addr[8] & EUI64_UBIT)
   90 #define EUI64_UNIVERSAL(in6)    (!EUI64_LOCAL(in6))
   91 
   92 #define IFID_LOCAL(in6)         (!EUI64_LOCAL(in6))
   93 #define IFID_UNIVERSAL(in6)     (!EUI64_UNIVERSAL(in6))
   94 
   95 /*
   96  * Generate a last-resort interface identifier, when the machine has no
   97  * IEEE802/EUI64 address sources.
   98  * The goal here is to get an interface identifier that is
   99  * (1) random enough and (2) does not change across reboot.
  100  * We currently use MD5(hostname) for it.
  101  */
  102 static int
  103 get_rand_ifid(ifp, in6)
  104         struct ifnet *ifp;
  105         struct in6_addr *in6;   /* upper 64bits are preserved */
  106 {
  107         MD5_CTX ctxt;
  108         u_int8_t digest[16];
  109         int hostnamelen = strlen(hostname);
  110 
  111 #if 0
  112         /* we need at least several letters as seed for ifid */
  113         if (hostnamelen < 3)
  114                 return -1;
  115 #endif
  116 
  117         /* generate 8 bytes of pseudo-random value. */
  118         bzero(&ctxt, sizeof(ctxt));
  119         MD5Init(&ctxt);
  120         MD5Update(&ctxt, hostname, hostnamelen);
  121         MD5Final(digest, &ctxt);
  122 
  123         /* assumes sizeof(digest) > sizeof(ifid) */
  124         bcopy(digest, &in6->s6_addr[8], 8);
  125 
  126         /* make sure to set "u" bit to local, and "g" bit to individual. */
  127         in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
  128         in6->s6_addr[8] |= EUI64_UBIT;  /* u bit to "local" */
  129 
  130         /* convert EUI64 into IPv6 interface identifier */
  131         EUI64_TO_IFID(in6);
  132 
  133         return 0;
  134 }
  135 
  136 static int
  137 generate_tmp_ifid(seed0, seed1, ret)
  138         u_int8_t *seed0, *ret;
  139         const u_int8_t *seed1;
  140 {
  141         MD5_CTX ctxt;
  142         u_int8_t seed[16], digest[16], nullbuf[8];
  143         u_int32_t val32;
  144 
  145         /* If there's no hisotry, start with a random seed. */
  146         bzero(nullbuf, sizeof(nullbuf));
  147         if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
  148                 int i;
  149 
  150                 for (i = 0; i < 2; i++) {
  151                         val32 = arc4random();
  152                         bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
  153                 }
  154         } else
  155                 bcopy(seed0, seed, 8);
  156 
  157         /* copy the right-most 64-bits of the given address */
  158         /* XXX assumption on the size of IFID */
  159         bcopy(seed1, &seed[8], 8);
  160 
  161         if (0) {                /* for debugging purposes only */
  162                 int i;
  163 
  164                 printf("generate_tmp_ifid: new randomized ID from: ");
  165                 for (i = 0; i < 16; i++)
  166                         printf("%02x", seed[i]);
  167                 printf(" ");
  168         }
  169 
  170         /* generate 16 bytes of pseudo-random value. */
  171         bzero(&ctxt, sizeof(ctxt));
  172         MD5Init(&ctxt);
  173         MD5Update(&ctxt, seed, sizeof(seed));
  174         MD5Final(digest, &ctxt);
  175 
  176         /*
  177          * RFC 3041 3.2.1. (3)
  178          * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
  179          * left-most bit is numbered 0) to zero.
  180          */
  181         bcopy(digest, ret, 8);
  182         ret[0] &= ~EUI64_UBIT;
  183 
  184         /*
  185          * XXX: we'd like to ensure that the generated value is not zero
  186          * for simplicity.  If the caclculated digest happens to be zero,
  187          * use a random non-zero value as the last resort.
  188          */
  189         if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
  190                 nd6log((LOG_INFO,
  191                     "generate_tmp_ifid: computed MD5 value is zero.\n"));
  192 
  193                 val32 = arc4random();
  194                 val32 = 1 + (val32 % (0xffffffff - 1));
  195         }
  196 
  197         /*
  198          * RFC 3041 3.2.1. (4)
  199          * Take the rightmost 64-bits of the MD5 digest and save them in
  200          * stable storage as the history value to be used in the next
  201          * iteration of the algorithm.
  202          */
  203         bcopy(&digest[8], seed0, 8);
  204 
  205         if (0) {                /* for debugging purposes only */
  206                 int i;
  207 
  208                 printf("to: ");
  209                 for (i = 0; i < 16; i++)
  210                         printf("%02x", digest[i]);
  211                 printf("\n");
  212         }
  213 
  214         return 0;
  215 }
  216 
  217 /*
  218  * Get interface identifier for the specified interface.
  219  * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
  220  */
  221 int
  222 in6_get_hw_ifid(ifp, in6)
  223         struct ifnet *ifp;
  224         struct in6_addr *in6;   /* upper 64bits are preserved */
  225 {
  226         struct ifaddr *ifa;
  227         struct sockaddr_dl *sdl;
  228         u_int8_t *addr;
  229         size_t addrlen;
  230         static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  231         static u_int8_t allone[8] =
  232                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  233 
  234         for (ifa = ifp->if_addrlist.tqh_first;
  235              ifa;
  236              ifa = ifa->ifa_list.tqe_next) {
  237                 if (ifa->ifa_addr->sa_family != AF_LINK)
  238                         continue;
  239                 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
  240                 if (sdl == NULL)
  241                         continue;
  242                 if (sdl->sdl_alen == 0)
  243                         continue;
  244 
  245                 goto found;
  246         }
  247 
  248         return -1;
  249 
  250 found:
  251         addr = LLADDR(sdl);
  252         addrlen = sdl->sdl_alen;
  253 
  254         /* get EUI64 */
  255         switch (ifp->if_type) {
  256         case IFT_ETHER:
  257         case IFT_FDDI:
  258         case IFT_ISO88025:
  259         case IFT_ATM:
  260         case IFT_IEEE1394:
  261 #ifdef IFT_IEEE80211
  262         case IFT_IEEE80211:
  263 #endif
  264                 /* IEEE802/EUI64 cases - what others? */
  265                 /* IEEE1394 uses 16byte length address starting with EUI64 */
  266                 if (addrlen > 8)
  267                         addrlen = 8;
  268 
  269                 /* look at IEEE802/EUI64 only */
  270                 if (addrlen != 8 && addrlen != 6)
  271                         return -1;
  272 
  273                 /*
  274                  * check for invalid MAC address - on bsdi, we see it a lot
  275                  * since wildboar configures all-zero MAC on pccard before
  276                  * card insertion.
  277                  */
  278                 if (bcmp(addr, allzero, addrlen) == 0)
  279                         return -1;
  280                 if (bcmp(addr, allone, addrlen) == 0)
  281                         return -1;
  282 
  283                 /* make EUI64 address */
  284                 if (addrlen == 8)
  285                         bcopy(addr, &in6->s6_addr[8], 8);
  286                 else if (addrlen == 6) {
  287                         in6->s6_addr[8] = addr[0];
  288                         in6->s6_addr[9] = addr[1];
  289                         in6->s6_addr[10] = addr[2];
  290                         in6->s6_addr[11] = 0xff;
  291                         in6->s6_addr[12] = 0xfe;
  292                         in6->s6_addr[13] = addr[3];
  293                         in6->s6_addr[14] = addr[4];
  294                         in6->s6_addr[15] = addr[5];
  295                 }
  296                 break;
  297 
  298         case IFT_ARCNET:
  299                 if (addrlen != 1)
  300                         return -1;
  301                 if (!addr[0])
  302                         return -1;
  303 
  304                 bzero(&in6->s6_addr[8], 8);
  305                 in6->s6_addr[15] = addr[0];
  306 
  307                 /*
  308                  * due to insufficient bitwidth, we mark it local.
  309                  */
  310                 in6->s6_addr[8] &= ~EUI64_GBIT; /* g bit to "individual" */
  311                 in6->s6_addr[8] |= EUI64_UBIT;  /* u bit to "local" */
  312                 break;
  313 
  314         case IFT_GIF:
  315 #ifdef IFT_STF
  316         case IFT_STF:
  317 #endif
  318                 /*
  319                  * RFC2893 says: "SHOULD use IPv4 address as ifid source".
  320                  * however, IPv4 address is not very suitable as unique
  321                  * identifier source (can be renumbered).
  322                  * we don't do this.
  323                  */
  324                 return -1;
  325 
  326         default:
  327                 return -1;
  328         }
  329 
  330         /* sanity check: g bit must not indicate "group" */
  331         if (EUI64_GROUP(in6))
  332                 return -1;
  333 
  334         /* convert EUI64 into IPv6 interface identifier */
  335         EUI64_TO_IFID(in6);
  336 
  337         /*
  338          * sanity check: ifid must not be all zero, avoid conflict with
  339          * subnet router anycast
  340          */
  341         if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
  342             bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
  343                 return -1;
  344         }
  345 
  346         return 0;
  347 }
  348 
  349 /*
  350  * Get interface identifier for the specified interface.  If it is not
  351  * available on ifp0, borrow interface identifier from other information
  352  * sources.
  353  */
  354 static int
  355 get_ifid(ifp0, altifp, in6)
  356         struct ifnet *ifp0;
  357         struct ifnet *altifp;   /* secondary EUI64 source */
  358         struct in6_addr *in6;
  359 {
  360         struct ifnet *ifp;
  361 
  362         /* first, try to get it from the interface itself */
  363         if (in6_get_hw_ifid(ifp0, in6) == 0) {
  364                 nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
  365                     if_name(ifp0)));
  366                 goto success;
  367         }
  368 
  369         /* try secondary EUI64 source. this basically is for ATM PVC */
  370         if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
  371                 nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
  372                     if_name(ifp0), if_name(altifp)));
  373                 goto success;
  374         }
  375 
  376         /* next, try to get it from some other hardware interface */
  377         IFNET_RLOCK();
  378         for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
  379                 if (ifp == ifp0)
  380                         continue;
  381                 if (in6_get_hw_ifid(ifp, in6) != 0)
  382                         continue;
  383 
  384                 /*
  385                  * to borrow ifid from other interface, ifid needs to be
  386                  * globally unique
  387                  */
  388                 if (IFID_UNIVERSAL(in6)) {
  389                         nd6log((LOG_DEBUG,
  390                             "%s: borrow interface identifier from %s\n",
  391                             if_name(ifp0), if_name(ifp)));
  392                         IFNET_RUNLOCK();
  393                         goto success;
  394                 }
  395         }
  396         IFNET_RUNLOCK();
  397 
  398         /* last resort: get from random number source */
  399         if (get_rand_ifid(ifp, in6) == 0) {
  400                 nd6log((LOG_DEBUG,
  401                     "%s: interface identifier generated by random number\n",
  402                     if_name(ifp0)));
  403                 goto success;
  404         }
  405 
  406         printf("%s: failed to get interface identifier\n", if_name(ifp0));
  407         return -1;
  408 
  409 success:
  410         nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  411             if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
  412             in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
  413             in6->s6_addr[14], in6->s6_addr[15]));
  414         return 0;
  415 }
  416 
  417 static int
  418 in6_ifattach_linklocal(ifp, altifp)
  419         struct ifnet *ifp;
  420         struct ifnet *altifp;   /* secondary EUI64 source */
  421 {
  422         struct in6_ifaddr *ia;
  423         struct in6_aliasreq ifra;
  424         struct nd_prefixctl pr0;
  425         int i, error;
  426 
  427         /*
  428          * configure link-local address.
  429          */
  430         bzero(&ifra, sizeof(ifra));
  431 
  432         /*
  433          * in6_update_ifa() does not use ifra_name, but we accurately set it
  434          * for safety.
  435          */
  436         strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
  437 
  438         ifra.ifra_addr.sin6_family = AF_INET6;
  439         ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
  440         ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
  441         ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
  442         if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
  443                 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
  444                 ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
  445         } else {
  446                 if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
  447                         nd6log((LOG_ERR,
  448                             "%s: no ifid available\n", if_name(ifp)));
  449                         return (-1);
  450                 }
  451         }
  452         if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
  453                 return (-1);
  454 
  455         ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
  456         ifra.ifra_prefixmask.sin6_family = AF_INET6;
  457         ifra.ifra_prefixmask.sin6_addr = in6mask64;
  458         /* link-local addresses should NEVER expire. */
  459         ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  460         ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  461 
  462         /*
  463          * Now call in6_update_ifa() to do a bunch of procedures to configure
  464          * a link-local address. We can set the 3rd argument to NULL, because
  465          * we know there's no other link-local address on the interface
  466          * and therefore we are adding one (instead of updating one).
  467          */
  468         if ((error = in6_update_ifa(ifp, &ifra, NULL,
  469                                     IN6_IFAUPDATE_DADDELAY)) != 0) {
  470                 /*
  471                  * XXX: When the interface does not support IPv6, this call
  472                  * would fail in the SIOCSIFADDR ioctl.  I believe the
  473                  * notification is rather confusing in this case, so just
  474                  * suppress it.  (jinmei@kame.net 20010130)
  475                  */
  476                 if (error != EAFNOSUPPORT)
  477                         nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
  478                             "configure a link-local address on %s "
  479                             "(errno=%d)\n",
  480                             if_name(ifp), error));
  481                 return (-1);
  482         }
  483 
  484         ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
  485 #ifdef DIAGNOSTIC
  486         if (!ia) {
  487                 panic("ia == NULL in in6_ifattach_linklocal");
  488                 /* NOTREACHED */
  489         }
  490 #endif
  491 
  492         /*
  493          * Make the link-local prefix (fe80::%link/64) as on-link.
  494          * Since we'd like to manage prefixes separately from addresses,
  495          * we make an ND6 prefix structure for the link-local prefix,
  496          * and add it to the prefix list as a never-expire prefix.
  497          * XXX: this change might affect some existing code base...
  498          */
  499         bzero(&pr0, sizeof(pr0));
  500         pr0.ndpr_ifp = ifp;
  501         /* this should be 64 at this moment. */
  502         pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
  503         pr0.ndpr_prefix = ifra.ifra_addr;
  504         /* apply the mask for safety. (nd6_prelist_add will apply it again) */
  505         for (i = 0; i < 4; i++) {
  506                 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
  507                     in6mask64.s6_addr32[i];
  508         }
  509         /*
  510          * Initialize parameters.  The link-local prefix must always be
  511          * on-link, and its lifetimes never expire.
  512          */
  513         pr0.ndpr_raf_onlink = 1;
  514         pr0.ndpr_raf_auto = 1;  /* probably meaningless */
  515         pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
  516         pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
  517         /*
  518          * Since there is no other link-local addresses, nd6_prefix_lookup()
  519          * probably returns NULL.  However, we cannot always expect the result.
  520          * For example, if we first remove the (only) existing link-local
  521          * address, and then reconfigure another one, the prefix is still
  522          * valid with referring to the old link-local address.
  523          */
  524         if (nd6_prefix_lookup(&pr0) == NULL) {
  525                 if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
  526                         return (error);
  527         }
  528 
  529         return 0;
  530 }
  531 
  532 static int
  533 in6_ifattach_loopback(ifp)
  534         struct ifnet *ifp;      /* must be IFT_LOOP */
  535 {
  536         struct in6_aliasreq ifra;
  537         int error;
  538 
  539         bzero(&ifra, sizeof(ifra));
  540 
  541         /*
  542          * in6_update_ifa() does not use ifra_name, but we accurately set it
  543          * for safety.
  544          */
  545         strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
  546 
  547         ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
  548         ifra.ifra_prefixmask.sin6_family = AF_INET6;
  549         ifra.ifra_prefixmask.sin6_addr = in6mask128;
  550 
  551         /*
  552          * Always initialize ia_dstaddr (= broadcast address) to loopback
  553          * address.  Follows IPv4 practice - see in_ifinit().
  554          */
  555         ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
  556         ifra.ifra_dstaddr.sin6_family = AF_INET6;
  557         ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
  558 
  559         ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
  560         ifra.ifra_addr.sin6_family = AF_INET6;
  561         ifra.ifra_addr.sin6_addr = in6addr_loopback;
  562 
  563         /* the loopback  address should NEVER expire. */
  564         ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  565         ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  566 
  567         /* we don't need to perform DAD on loopback interfaces. */
  568         ifra.ifra_flags |= IN6_IFF_NODAD;
  569 
  570         /* skip registration to the prefix list. XXX should be temporary. */
  571         ifra.ifra_flags |= IN6_IFF_NOPFX;
  572 
  573         /*
  574          * We are sure that this is a newly assigned address, so we can set
  575          * NULL to the 3rd arg.
  576          */
  577         if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
  578                 nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
  579                     "the loopback address on %s (errno=%d)\n",
  580                     if_name(ifp), error));
  581                 return (-1);
  582         }
  583 
  584         return 0;
  585 }
  586 
  587 /*
  588  * compute NI group address, based on the current hostname setting.
  589  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
  590  *
  591  * when ifp == NULL, the caller is responsible for filling scopeid.
  592  */
  593 int
  594 in6_nigroup(ifp, name, namelen, in6)
  595         struct ifnet *ifp;
  596         const char *name;
  597         int namelen;
  598         struct in6_addr *in6;
  599 {
  600         const char *p;
  601         u_char *q;
  602         MD5_CTX ctxt;
  603         u_int8_t digest[16];
  604         char l;
  605         char n[64];     /* a single label must not exceed 63 chars */
  606 
  607         if (!namelen || !name)
  608                 return -1;
  609 
  610         p = name;
  611         while (p && *p && *p != '.' && p - name < namelen)
  612                 p++;
  613         if (p - name > sizeof(n) - 1)
  614                 return -1;      /* label too long */
  615         l = p - name;
  616         strncpy(n, name, l);
  617         n[(int)l] = '\0';
  618         for (q = n; *q; q++) {
  619                 if ('A' <= *q && *q <= 'Z')
  620                         *q = *q - 'A' + 'a';
  621         }
  622 
  623         /* generate 8 bytes of pseudo-random value. */
  624         bzero(&ctxt, sizeof(ctxt));
  625         MD5Init(&ctxt);
  626         MD5Update(&ctxt, &l, sizeof(l));
  627         MD5Update(&ctxt, n, l);
  628         MD5Final(digest, &ctxt);
  629 
  630         bzero(in6, sizeof(*in6));
  631         in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
  632         in6->s6_addr8[11] = 2;
  633         bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
  634         if (in6_setscope(in6, ifp, NULL))
  635                 return (-1); /* XXX: should not fail */
  636 
  637         return 0;
  638 }
  639 
  640 /*
  641  * XXX multiple loopback interface needs more care.  for instance,
  642  * nodelocal address needs to be configured onto only one of them.
  643  * XXX multiple link-local address case
  644  */
  645 void
  646 in6_ifattach(ifp, altifp)
  647         struct ifnet *ifp;
  648         struct ifnet *altifp;   /* secondary EUI64 source */
  649 {
  650         struct in6_ifaddr *ia;
  651         struct in6_addr in6;
  652 
  653         /* some of the interfaces are inherently not IPv6 capable */
  654         switch (ifp->if_type) {
  655         case IFT_PFLOG:
  656         case IFT_PFSYNC:
  657         case IFT_CARP:
  658                 return;
  659         }
  660 
  661         /*
  662          * quirks based on interface type
  663          */
  664         switch (ifp->if_type) {
  665 #ifdef IFT_STF
  666         case IFT_STF:
  667                 /*
  668                  * 6to4 interface is a very special kind of beast.
  669                  * no multicast, no linklocal.  RFC2529 specifies how to make
  670                  * linklocals for 6to4 interface, but there's no use and
  671                  * it is rather harmful to have one.
  672                  */
  673                 goto statinit;
  674 #endif
  675         default:
  676                 break;
  677         }
  678 
  679         /*
  680          * usually, we require multicast capability to the interface
  681          */
  682         if ((ifp->if_flags & IFF_MULTICAST) == 0) {
  683                 nd6log((LOG_INFO, "in6_ifattach: "
  684                     "%s is not multicast capable, IPv6 not enabled\n",
  685                     if_name(ifp)));
  686                 return;
  687         }
  688 
  689         /*
  690          * assign loopback address for loopback interface.
  691          * XXX multiple loopback interface case.
  692          */
  693         if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
  694                 in6 = in6addr_loopback;
  695                 if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
  696                         if (in6_ifattach_loopback(ifp) != 0)
  697                                 return;
  698                 }
  699         }
  700 
  701         /*
  702          * assign a link-local address, if there's none.
  703          */
  704         if (ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
  705                 ia = in6ifa_ifpforlinklocal(ifp, 0);
  706                 if (ia == NULL) {
  707                         if (in6_ifattach_linklocal(ifp, altifp) == 0) {
  708                                 /* linklocal address assigned */
  709                         } else {
  710                                 /* failed to assign linklocal address. bark? */
  711                         }
  712                 }
  713         }
  714 
  715 #ifdef IFT_STF                  /* XXX */
  716 statinit:
  717 #endif
  718 
  719         /* update dynamically. */
  720         if (in6_maxmtu < ifp->if_mtu)
  721                 in6_maxmtu = ifp->if_mtu;
  722 }
  723 
  724 /*
  725  * NOTE: in6_ifdetach() does not support loopback if at this moment.
  726  * We don't need this function in bsdi, because interfaces are never removed
  727  * from the ifnet list in bsdi.
  728  */
  729 void
  730 in6_ifdetach(ifp)
  731         struct ifnet *ifp;
  732 {
  733         struct in6_ifaddr *ia, *oia;
  734         struct ifaddr *ifa, *next;
  735         struct rtentry *rt;
  736         short rtflags;
  737         struct sockaddr_in6 sin6;
  738         struct in6_multi *in6m, *in6m_next;
  739         struct in6_multi_mship *imm;
  740 
  741         /* remove neighbor management table */
  742         nd6_purge(ifp);
  743 
  744         /* nuke any of IPv6 addresses we have */
  745         for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
  746                 next = ifa->ifa_list.tqe_next;
  747                 if (ifa->ifa_addr->sa_family != AF_INET6)
  748                         continue;
  749                 in6_purgeaddr(ifa);
  750         }
  751 
  752         /* undo everything done by in6_ifattach(), just in case */
  753         for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
  754                 next = ifa->ifa_list.tqe_next;
  755 
  756                 if (ifa->ifa_addr->sa_family != AF_INET6
  757                  || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
  758                         continue;
  759                 }
  760 
  761                 ia = (struct in6_ifaddr *)ifa;
  762 
  763                 /*
  764                  * leave from multicast groups we have joined for the interface
  765                  */
  766                 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
  767                         LIST_REMOVE(imm, i6mm_chain);
  768                         in6_leavegroup(imm);
  769                 }
  770 
  771                 /* remove from the routing table */
  772                 if ((ia->ia_flags & IFA_ROUTE) &&
  773                     (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
  774                         rtflags = rt->rt_flags;
  775                         RTFREE_LOCKED(rt);
  776                         rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
  777                             (struct sockaddr *)&ia->ia_addr,
  778                             (struct sockaddr *)&ia->ia_prefixmask,
  779                             rtflags, (struct rtentry **)0);
  780                 }
  781 
  782                 /* remove from the linked list */
  783                 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
  784                 IFAFREE(&ia->ia_ifa);
  785 
  786                 /* also remove from the IPv6 address chain(itojun&jinmei) */
  787                 oia = ia;
  788                 if (oia == (ia = in6_ifaddr))
  789                         in6_ifaddr = ia->ia_next;
  790                 else {
  791                         while (ia->ia_next && (ia->ia_next != oia))
  792                                 ia = ia->ia_next;
  793                         if (ia->ia_next)
  794                                 ia->ia_next = oia->ia_next;
  795                         else {
  796                                 nd6log((LOG_ERR,
  797                                     "%s: didn't unlink in6ifaddr from list\n",
  798                                     if_name(ifp)));
  799                         }
  800                 }
  801 
  802                 IFAFREE(&oia->ia_ifa);
  803         }
  804 
  805         /* leave from all multicast groups joined */
  806 
  807         if (udbinfo.listhead != NULL)
  808                 in6_pcbpurgeif0(LIST_FIRST(udbinfo.listhead), ifp);
  809         if (ripcbinfo.listhead != NULL)
  810                 in6_pcbpurgeif0(LIST_FIRST(ripcbinfo.listhead), ifp);
  811 
  812         for (in6m = LIST_FIRST(&in6_multihead); in6m; in6m = in6m_next) {
  813                 in6m_next = LIST_NEXT(in6m, in6m_entry);
  814                 if (in6m->in6m_ifp != ifp)
  815                         continue;
  816                 printf("in6_ifdetach: in6m=%p (ref=%d), ifp=%p\n", in6m,
  817                     in6m->in6m_ifma->ifma_refcount, ifp);
  818                 in6_delmulti(in6m);
  819                 in6m = NULL;
  820         }
  821 
  822         /*
  823          * remove neighbor management table.  we call it twice just to make
  824          * sure we nuke everything.  maybe we need just one call.
  825          * XXX: since the first call did not release addresses, some prefixes
  826          * might remain.  We should call nd6_purge() again to release the
  827          * prefixes after removing all addresses above.
  828          * (Or can we just delay calling nd6_purge until at this point?)
  829          */
  830         nd6_purge(ifp);
  831 
  832         /* remove route to link-local allnodes multicast (ff02::1) */
  833         bzero(&sin6, sizeof(sin6));
  834         sin6.sin6_len = sizeof(struct sockaddr_in6);
  835         sin6.sin6_family = AF_INET6;
  836         sin6.sin6_addr = in6addr_linklocal_allnodes;
  837         if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
  838                 /* XXX: should not fail */
  839                 return;
  840         /* XXX grab lock first to avoid LOR */
  841         if (rt_tables[AF_INET6] != NULL) {
  842                 RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET6]);
  843                 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
  844                 if (rt) {
  845                         if (rt->rt_ifp == ifp)
  846                                 rtexpunge(rt);
  847                         RTFREE_LOCKED(rt);
  848                 }
  849                 RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET6]);
  850         }
  851 }
  852 
  853 int
  854 in6_get_tmpifid(ifp, retbuf, baseid, generate)
  855         struct ifnet *ifp;
  856         u_int8_t *retbuf;
  857         const u_int8_t *baseid;
  858         int generate;
  859 {
  860         u_int8_t nullbuf[8];
  861         struct nd_ifinfo *ndi = ND_IFINFO(ifp);
  862 
  863         bzero(nullbuf, sizeof(nullbuf));
  864         if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
  865                 /* we've never created a random ID.  Create a new one. */
  866                 generate = 1;
  867         }
  868 
  869         if (generate) {
  870                 bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
  871 
  872                 /* generate_tmp_ifid will update seedn and buf */
  873                 (void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
  874                     ndi->randomid);
  875         }
  876         bcopy(ndi->randomid, retbuf, 8);
  877 
  878         return (0);
  879 }
  880 
  881 void
  882 in6_tmpaddrtimer(ignored_arg)
  883         void *ignored_arg;
  884 {
  885         struct nd_ifinfo *ndi;
  886         u_int8_t nullbuf[8];
  887         struct ifnet *ifp;
  888         int s = splnet();
  889 
  890         callout_reset(&in6_tmpaddrtimer_ch,
  891             (ip6_temp_preferred_lifetime - ip6_desync_factor -
  892             ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
  893 
  894         bzero(nullbuf, sizeof(nullbuf));
  895         for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
  896                 ndi = ND_IFINFO(ifp);
  897                 if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
  898                         /*
  899                          * We've been generating a random ID on this interface.
  900                          * Create a new one.
  901                          */
  902                         (void)generate_tmp_ifid(ndi->randomseed0,
  903                             ndi->randomseed1, ndi->randomid);
  904                 }
  905         }
  906 
  907         splx(s);
  908 }

Cache object: 9a5683f054b850878402af7b637c7faa


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