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

Cache object: 5dfbe6653dddee5eb18c4f572d29271e


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