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