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