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