xref: /freebsd/sys/netinet6/in6_ifattach.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*	$FreeBSD$	*/
2 /*	$KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/kernel.h>
39 #include <sys/syslog.h>
40 #include <sys/md5.h>
41 
42 #include <net/if.h>
43 #include <net/if_dl.h>
44 #include <net/if_types.h>
45 #include <net/route.h>
46 
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <netinet/if_ether.h>
50 
51 #include <netinet/ip6.h>
52 #include <netinet6/ip6_var.h>
53 #include <netinet6/in6_var.h>
54 #include <netinet6/in6_ifattach.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet6/nd6.h>
57 #include <netinet6/scope6_var.h>
58 
59 #include <net/net_osdep.h>
60 
61 struct in6_ifstat **in6_ifstat = NULL;
62 struct icmp6_ifstat **icmp6_ifstat = NULL;
63 size_t in6_ifstatmax = 0;
64 size_t icmp6_ifstatmax = 0;
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 static int get_rand_ifid __P((struct ifnet *, struct in6_addr *));
76 static int generate_tmp_ifid __P((u_int8_t *, const u_int8_t *, u_int8_t *));
77 static int get_hw_ifid __P((struct ifnet *, struct in6_addr *));
78 static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *));
79 static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *));
80 static int in6_ifattach_loopback __P((struct ifnet *));
81 
82 #define EUI64_GBIT	0x01
83 #define EUI64_UBIT	0x02
84 #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
85 #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
86 #define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
87 #define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
88 #define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
89 
90 #define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
91 #define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
92 
93 /*
94  * Generate a last-resort interface identifier, when the machine has no
95  * IEEE802/EUI64 address sources.
96  * The goal here is to get an interface identifier that is
97  * (1) random enough and (2) does not change across reboot.
98  * We currently use MD5(hostname) for it.
99  */
100 static int
101 get_rand_ifid(ifp, in6)
102 	struct ifnet *ifp;
103 	struct in6_addr *in6;	/*upper 64bits are preserved */
104 {
105 	MD5_CTX ctxt;
106 	u_int8_t digest[16];
107 	int hostnamelen	= strlen(hostname);
108 
109 #if 0
110 	/* we need at least several letters as seed for ifid */
111 	if (hostnamelen < 3)
112 		return -1;
113 #endif
114 
115 	/* generate 8 bytes of pseudo-random value. */
116 	bzero(&ctxt, sizeof(ctxt));
117 	MD5Init(&ctxt);
118 	MD5Update(&ctxt, hostname, hostnamelen);
119 	MD5Final(digest, &ctxt);
120 
121 	/* assumes sizeof(digest) > sizeof(ifid) */
122 	bcopy(digest, &in6->s6_addr[8], 8);
123 
124 	/* make sure to set "u" bit to local, and "g" bit to individual. */
125 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
126 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
127 
128 	/* convert EUI64 into IPv6 interface identifier */
129 	EUI64_TO_IFID(in6);
130 
131 	return 0;
132 }
133 
134 static int
135 generate_tmp_ifid(seed0, seed1, ret)
136 	u_int8_t *seed0, *ret;
137 	const u_int8_t *seed1;
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 = random() ^ tv.tv_usec;
152 			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
153 		}
154 	} else
155 		bcopy(seed0, seed, 8);
156 
157 	/* copy the right-most 64-bits of the given address */
158 	/* XXX assumption on the size of IFID */
159 	bcopy(seed1, &seed[8], 8);
160 
161 	if (0) {		/* for debugging purposes only */
162 		int i;
163 
164 		printf("generate_tmp_ifid: new randomized ID from: ");
165 		for (i = 0; i < 16; i++)
166 			printf("%02x", seed[i]);
167 		printf(" ");
168 	}
169 
170 	/* generate 16 bytes of pseudo-random value. */
171 	bzero(&ctxt, sizeof(ctxt));
172 	MD5Init(&ctxt);
173 	MD5Update(&ctxt, seed, sizeof(seed));
174 	MD5Final(digest, &ctxt);
175 
176 	/*
177 	 * RFC 3041 3.2.1. (3)
178 	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
179 	 * left-most bit is numbered 0) to zero.
180 	 */
181 	bcopy(digest, ret, 8);
182 	ret[0] &= ~EUI64_UBIT;
183 
184 	/*
185 	 * XXX: we'd like to ensure that the generated value is not zero
186 	 * for simplicity.  If the caclculated digest happens to be zero,
187 	 * use a random non-zero value as the last resort.
188 	 */
189 	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
190 		log(LOG_INFO,
191 		    "generate_tmp_ifid: computed MD5 value is zero.\n");
192 
193 		microtime(&tv);
194 		val32 = random() ^ tv.tv_usec;
195 		val32 = 1 + (val32 % (0xffffffff - 1));
196 	}
197 
198 	/*
199 	 * RFC 3041 3.2.1. (4)
200 	 * Take the rightmost 64-bits of the MD5 digest and save them in
201 	 * stable storage as the history value to be used in the next
202 	 * iteration of the algorithm.
203 	 */
204 	bcopy(&digest[8], seed0, 8);
205 
206 	if (0) {		/* for debugging purposes only */
207 		int i;
208 
209 		printf("to: ");
210 		for (i = 0; i < 16; i++)
211 			printf("%02x", digest[i]);
212 		printf("\n");
213 	}
214 
215 	return 0;
216 }
217 
218 /*
219  * Get interface identifier for the specified interface.
220  * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
221  */
222 static int
223 get_hw_ifid(ifp, in6)
224 	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 	for (ifa = ifp->if_addrlist.tqh_first;
236 	     ifa;
237 	     ifa = ifa->ifa_list.tqe_next)
238 	{
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(ifp0, altifp, in6)
357 	struct ifnet *ifp0;
358 	struct ifnet *altifp;	/*secondary EUI64 source*/
359 	struct in6_addr *in6;
360 {
361 	struct ifnet *ifp;
362 
363 	/* first, try to get it from the interface itself */
364 	if (get_hw_ifid(ifp0, in6) == 0) {
365 		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
366 		    if_name(ifp0)));
367 		goto success;
368 	}
369 
370 	/* try secondary EUI64 source. this basically is for ATM PVC */
371 	if (altifp && get_hw_ifid(altifp, in6) == 0) {
372 		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
373 		    if_name(ifp0), if_name(altifp)));
374 		goto success;
375 	}
376 
377 	/* next, try to get it from some other hardware interface */
378 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
379 	{
380 		if (ifp == ifp0)
381 			continue;
382 		if (get_hw_ifid(ifp, in6) != 0)
383 			continue;
384 
385 		/*
386 		 * to borrow ifid from other interface, ifid needs to be
387 		 * globally unique
388 		 */
389 		if (IFID_UNIVERSAL(in6)) {
390 			nd6log((LOG_DEBUG,
391 			    "%s: borrow interface identifier from %s\n",
392 			    if_name(ifp0), if_name(ifp)));
393 			goto success;
394 		}
395 	}
396 
397 	/* last resort: get from random number source */
398 	if (get_rand_ifid(ifp, in6) == 0) {
399 		nd6log((LOG_DEBUG,
400 		    "%s: interface identifier generated by random number\n",
401 		    if_name(ifp0)));
402 		goto success;
403 	}
404 
405 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
406 	return -1;
407 
408 success:
409 	nd6log((LOG_INFO, "%s: ifid: "
410 		"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
411 		if_name(ifp0),
412 		in6->s6_addr[8], in6->s6_addr[9],
413 		in6->s6_addr[10], in6->s6_addr[11],
414 		in6->s6_addr[12], in6->s6_addr[13],
415 		in6->s6_addr[14], in6->s6_addr[15]));
416 	return 0;
417 }
418 
419 static int
420 in6_ifattach_linklocal(ifp, altifp)
421 	struct ifnet *ifp;
422 	struct ifnet *altifp;	/* secondary EUI64 source */
423 {
424 	struct in6_ifaddr *ia;
425 	struct in6_aliasreq ifra;
426 	struct nd_prefix pr0;
427 	int i, error;
428 
429 	/*
430 	 * configure link-local address.
431 	 */
432 	bzero(&ifra, sizeof(ifra));
433 
434 	/*
435 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
436 	 * for safety.
437 	 */
438 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
439 
440 	ifra.ifra_addr.sin6_family = AF_INET6;
441 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
442 	ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
443 #ifdef SCOPEDROUTING
444 	ifra.ifra_addr.sin6_addr.s6_addr16[1] = 0
445 #else
446 	ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); /* XXX */
447 #endif
448 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
449 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
450 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
451 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
452 	} else {
453 		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
454 			nd6log((LOG_ERR,
455 			    "%s: no ifid available\n", if_name(ifp)));
456 			return -1;
457 		}
458 	}
459 #ifdef SCOPEDROUTING
460 	ifra.ifra_addr.sin6_scope_id =
461 		in6_addr2scopeid(ifp,  &ifra.ifra_addr.sin6_addr);
462 #endif
463 
464 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
465 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
466 	ifra.ifra_prefixmask.sin6_addr = in6mask64;
467 #ifdef SCOPEDROUTING
468 	/* take into accound the sin6_scope_id field for routing */
469 	ifra.ifra_prefixmask.sin6_scope_id = 0xffffffff;
470 #endif
471 	/* link-local addresses should NEVER expire. */
472 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
473 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
474 
475 	/*
476 	 * Do not let in6_update_ifa() do DAD, since we need a random delay
477 	 * before sending an NS at the first time the inteface becomes up.
478 	 * Instead, in6_if_up() will start DAD with a proper random delay.
479 	 */
480 	ifra.ifra_flags |= IN6_IFF_NODAD;
481 
482 	/*
483 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
484 	 * a link-local address. We can set NULL to the 3rd argument, because
485 	 * we know there's no other link-local address on the interface.
486 	 */
487 	if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
488 		/*
489 		 * XXX: When the interface does not support IPv6, this call
490 		 * would fail in the SIOCSIFADDR ioctl.  I believe the
491 		 * notification is rather confusing in this case, so just
492 		 * supress it.  (jinmei@kame.net 20010130)
493 		 */
494 		if (error != EAFNOSUPPORT)
495 			log(LOG_NOTICE, "in6_ifattach_linklocal: failed to "
496 			    "configure a link-local address on %s "
497 			    "(errno=%d)\n",
498 			    if_name(ifp), error);
499 		return(-1);
500 	}
501 
502 	/*
503 	 * Adjust ia6_flags so that in6_if_up will perform DAD.
504 	 * XXX: Some P2P interfaces seem not to send packets just after
505 	 * becoming up, so we skip p2p interfaces for safety.
506 	 */
507 	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
508 #ifdef DIAGNOSTIC
509 	if (!ia) {
510 		panic("ia == NULL in in6_ifattach_linklocal");
511 		/*NOTREACHED*/
512 	}
513 #endif
514 	if (in6if_do_dad(ifp) && (ifp->if_flags & IFF_POINTOPOINT) == 0) {
515 		ia->ia6_flags &= ~IN6_IFF_NODAD;
516 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
517 	}
518 
519 	/*
520 	 * Make the link-local prefix (fe80::/64%link) as on-link.
521 	 * Since we'd like to manage prefixes separately from addresses,
522 	 * we make an ND6 prefix structure for the link-local prefix,
523 	 * and add it to the prefix list as a never-expire prefix.
524 	 * XXX: this change might affect some existing code base...
525 	 */
526 	bzero(&pr0, sizeof(pr0));
527 	pr0.ndpr_ifp = ifp;
528 	/* this should be 64 at this moment. */
529 	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
530 	pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr;
531 	pr0.ndpr_prefix = ifra.ifra_addr;
532 	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
533 	for (i = 0; i < 4; i++) {
534 		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
535 			in6mask64.s6_addr32[i];
536 	}
537 	/*
538 	 * Initialize parameters.  The link-local prefix must always be
539 	 * on-link, and its lifetimes never expire.
540 	 */
541 	pr0.ndpr_raf_onlink = 1;
542 	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
543 	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
544 	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
545 	/*
546 	 * Since there is no other link-local addresses, nd6_prefix_lookup()
547 	 * probably returns NULL.  However, we cannot always expect the result.
548 	 * For example, if we first remove the (only) existing link-local
549 	 * address, and then reconfigure another one, the prefix is still
550 	 * valid with referring to the old link-local address.
551 	 */
552 	if (nd6_prefix_lookup(&pr0) == NULL) {
553 		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
554 			return(error);
555 	}
556 
557 	return 0;
558 }
559 
560 static int
561 in6_ifattach_loopback(ifp)
562 	struct ifnet *ifp;	/* must be IFT_LOOP */
563 {
564 	struct in6_aliasreq ifra;
565 	int error;
566 
567 	bzero(&ifra, sizeof(ifra));
568 
569 	/*
570 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
571 	 * for safety.
572 	 */
573 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
574 
575 	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
576 	ifra.ifra_prefixmask.sin6_family = AF_INET6;
577 	ifra.ifra_prefixmask.sin6_addr = in6mask128;
578 
579 	/*
580 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
581 	 * address.  Follows IPv4 practice - see in_ifinit().
582 	 */
583 	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
584 	ifra.ifra_dstaddr.sin6_family = AF_INET6;
585 	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
586 
587 	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
588 	ifra.ifra_addr.sin6_family = AF_INET6;
589 	ifra.ifra_addr.sin6_addr = in6addr_loopback;
590 
591 	/* the loopback  address should NEVER expire. */
592 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
593 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
594 
595 	/* we don't need to perfrom DAD on loopback interfaces. */
596 	ifra.ifra_flags |= IN6_IFF_NODAD;
597 
598 	/* skip registration to the prefix list. XXX should be temporary. */
599 	ifra.ifra_flags |= IN6_IFF_NOPFX;
600 
601 	/*
602 	 * We can set NULL to the 3rd arg. See comments in
603 	 * in6_ifattach_linklocal().
604 	 */
605 	if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
606 		log(LOG_ERR, "in6_ifattach_loopback: failed to configure "
607 		    "the loopback address on %s (errno=%d)\n",
608 		    if_name(ifp), error);
609 		return(-1);
610 	}
611 
612 	return 0;
613 }
614 
615 /*
616  * compute NI group address, based on the current hostname setting.
617  * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
618  *
619  * when ifp == NULL, the caller is responsible for filling scopeid.
620  */
621 int
622 in6_nigroup(ifp, name, namelen, in6)
623 	struct ifnet *ifp;
624 	const char *name;
625 	int namelen;
626 	struct in6_addr *in6;
627 {
628 	const char *p;
629 	u_char *q;
630 	MD5_CTX ctxt;
631 	u_int8_t digest[16];
632 	char l;
633 	char n[64];	/* a single label must not exceed 63 chars */
634 
635 	if (!namelen || !name)
636 		return -1;
637 
638 	p = name;
639 	while (p && *p && *p != '.' && p - name < namelen)
640 		p++;
641 	if (p - name > sizeof(n) - 1)
642 		return -1;	/*label too long*/
643 	l = p - name;
644 	strncpy(n, name, l);
645 	n[(int)l] = '\0';
646 	for (q = n; *q; q++) {
647 		if ('A' <= *q && *q <= 'Z')
648 			*q = *q - 'A' + 'a';
649 	}
650 
651 	/* generate 8 bytes of pseudo-random value. */
652 	bzero(&ctxt, sizeof(ctxt));
653 	MD5Init(&ctxt);
654 	MD5Update(&ctxt, &l, sizeof(l));
655 	MD5Update(&ctxt, n, l);
656 	MD5Final(digest, &ctxt);
657 
658 	bzero(in6, sizeof(*in6));
659 	in6->s6_addr16[0] = htons(0xff02);
660 	if (ifp)
661 		in6->s6_addr16[1] = htons(ifp->if_index);
662 	in6->s6_addr8[11] = 2;
663 	bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
664 
665 	return 0;
666 }
667 
668 void
669 in6_nigroup_attach(name, namelen)
670 	const char *name;
671 	int namelen;
672 {
673 	struct ifnet *ifp;
674 	struct sockaddr_in6 mltaddr;
675 	struct in6_multi *in6m;
676 	int error;
677 
678 	bzero(&mltaddr, sizeof(mltaddr));
679 	mltaddr.sin6_family = AF_INET6;
680 	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
681 	if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
682 		return;
683 
684 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
685 	{
686 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
687 		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
688 		if (!in6m) {
689 			if (!in6_addmulti(&mltaddr.sin6_addr, ifp, &error)) {
690 				nd6log((LOG_ERR, "%s: failed to join %s "
691 				    "(errno=%d)\n", if_name(ifp),
692 				    ip6_sprintf(&mltaddr.sin6_addr),
693 				    error));
694 			}
695 		}
696 	}
697 }
698 
699 void
700 in6_nigroup_detach(name, namelen)
701 	const char *name;
702 	int namelen;
703 {
704 	struct ifnet *ifp;
705 	struct sockaddr_in6 mltaddr;
706 	struct in6_multi *in6m;
707 
708 	bzero(&mltaddr, sizeof(mltaddr));
709 	mltaddr.sin6_family = AF_INET6;
710 	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
711 	if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
712 		return;
713 
714 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
715 	{
716 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
717 		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
718 		if (in6m)
719 			in6_delmulti(in6m);
720 	}
721 }
722 
723 /*
724  * XXX multiple loopback interface needs more care.  for instance,
725  * nodelocal address needs to be configured onto only one of them.
726  * XXX multiple link-local address case
727  */
728 void
729 in6_ifattach(ifp, altifp)
730 	struct ifnet *ifp;
731 	struct ifnet *altifp;	/* secondary EUI64 source */
732 {
733 	static size_t if_indexlim = 8;
734 	struct in6_ifaddr *ia;
735 	struct in6_addr in6;
736 
737 	/* some of the interfaces are inherently not IPv6 capable */
738 	switch (ifp->if_type) {
739 #ifdef IFT_BRIDGE	/*OpenBSD 2.8*/
740 	case IFT_BRIDGE:
741 		return;
742 #endif
743 	}
744 
745 	/*
746 	 * We have some arrays that should be indexed by if_index.
747 	 * since if_index will grow dynamically, they should grow too.
748 	 *	struct in6_ifstat **in6_ifstat
749 	 *	struct icmp6_ifstat **icmp6_ifstat
750 	 */
751 	if (in6_ifstat == NULL || icmp6_ifstat == NULL ||
752 	    if_index >= if_indexlim) {
753 		size_t n;
754 		caddr_t q;
755 		size_t olim;
756 
757 		olim = if_indexlim;
758 		while (if_index >= if_indexlim)
759 			if_indexlim <<= 1;
760 
761 		/* grow in6_ifstat */
762 		n = if_indexlim * sizeof(struct in6_ifstat *);
763 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
764 		bzero(q, n);
765 		if (in6_ifstat) {
766 			bcopy((caddr_t)in6_ifstat, q,
767 				olim * sizeof(struct in6_ifstat *));
768 			free((caddr_t)in6_ifstat, M_IFADDR);
769 		}
770 		in6_ifstat = (struct in6_ifstat **)q;
771 		in6_ifstatmax = if_indexlim;
772 
773 		/* grow icmp6_ifstat */
774 		n = if_indexlim * sizeof(struct icmp6_ifstat *);
775 		q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
776 		bzero(q, n);
777 		if (icmp6_ifstat) {
778 			bcopy((caddr_t)icmp6_ifstat, q,
779 				olim * sizeof(struct icmp6_ifstat *));
780 			free((caddr_t)icmp6_ifstat, M_IFADDR);
781 		}
782 		icmp6_ifstat = (struct icmp6_ifstat **)q;
783 		icmp6_ifstatmax = if_indexlim;
784 	}
785 
786 	/* initialize scope identifiers */
787 	scope6_ifattach(ifp);
788 
789 	/*
790 	 * quirks based on interface type
791 	 */
792 	switch (ifp->if_type) {
793 #ifdef IFT_STF
794 	case IFT_STF:
795 		/*
796 		 * 6to4 interface is a very speical kind of beast.
797 		 * no multicast, no linklocal (based on 03 draft).
798 		 */
799 		goto statinit;
800 #endif
801 	default:
802 		break;
803 	}
804 
805 	/*
806 	 * usually, we require multicast capability to the interface
807 	 */
808 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
809 		log(LOG_INFO, "in6_ifattach: "
810 		    "%s is not multicast capable, IPv6 not enabled\n",
811 		    if_name(ifp));
812 		return;
813 	}
814 
815 	/*
816 	 * assign loopback address for loopback interface.
817 	 * XXX multiple loopback interface case.
818 	 */
819 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
820 		in6 = in6addr_loopback;
821 		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
822 			if (in6_ifattach_loopback(ifp) != 0)
823 				return;
824 		}
825 	}
826 
827 	/*
828 	 * assign a link-local address, if there's none.
829 	 */
830 	if (ip6_auto_linklocal) {
831 		ia = in6ifa_ifpforlinklocal(ifp, 0);
832 		if (ia == NULL) {
833 			if (in6_ifattach_linklocal(ifp, altifp) == 0) {
834 				/* linklocal address assigned */
835 			} else {
836 				/* failed to assign linklocal address. bark? */
837 			}
838 		}
839 	}
840 
841 #ifdef IFT_STF			/* XXX */
842 statinit:
843 #endif
844 
845 	/* update dynamically. */
846 	if (in6_maxmtu < ifp->if_mtu)
847 		in6_maxmtu = ifp->if_mtu;
848 
849 	if (in6_ifstat[ifp->if_index] == NULL) {
850 		in6_ifstat[ifp->if_index] = (struct in6_ifstat *)
851 			malloc(sizeof(struct in6_ifstat), M_IFADDR, M_WAITOK);
852 		bzero(in6_ifstat[ifp->if_index], sizeof(struct in6_ifstat));
853 	}
854 	if (icmp6_ifstat[ifp->if_index] == NULL) {
855 		icmp6_ifstat[ifp->if_index] = (struct icmp6_ifstat *)
856 			malloc(sizeof(struct icmp6_ifstat), M_IFADDR, M_WAITOK);
857 		bzero(icmp6_ifstat[ifp->if_index], sizeof(struct icmp6_ifstat));
858 	}
859 
860 	/* initialize NDP variables */
861 	nd6_ifattach(ifp);
862 }
863 
864 /*
865  * NOTE: in6_ifdetach() does not support loopback if at this moment.
866  * We don't need this function in bsdi, because interfaces are never removed
867  * from the ifnet list in bsdi.
868  */
869 void
870 in6_ifdetach(ifp)
871 	struct ifnet *ifp;
872 {
873 	struct in6_ifaddr *ia, *oia;
874 	struct ifaddr *ifa, *next;
875 	struct rtentry *rt;
876 	short rtflags;
877 	struct sockaddr_in6 sin6;
878 	struct in6_multi *in6m;
879 	struct in6_multi *in6m_next;
880 
881 	/* nuke prefix list.  this may try to remove some of ifaddrs as well */
882 	in6_purgeprefix(ifp);
883 
884 	/* remove neighbor management table */
885 	nd6_purge(ifp);
886 
887 	/* nuke any of IPv6 addresses we have */
888 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
889 	{
890 		next = ifa->ifa_list.tqe_next;
891 		if (ifa->ifa_addr->sa_family != AF_INET6)
892 			continue;
893 		in6_purgeaddr(ifa);
894 	}
895 
896 	/* undo everything done by in6_ifattach(), just in case */
897 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
898 	{
899 		next = ifa->ifa_list.tqe_next;
900 
901 
902 		if (ifa->ifa_addr->sa_family != AF_INET6
903 		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
904 			continue;
905 		}
906 
907 		ia = (struct in6_ifaddr *)ifa;
908 
909 		/* remove from the routing table */
910 		if ((ia->ia_flags & IFA_ROUTE)
911 		 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
912 			rtflags = rt->rt_flags;
913 			rtfree(rt);
914 			rtrequest(RTM_DELETE,
915 				(struct sockaddr *)&ia->ia_addr,
916 				(struct sockaddr *)&ia->ia_addr,
917 				(struct sockaddr *)&ia->ia_prefixmask,
918 				rtflags, (struct rtentry **)0);
919 		}
920 
921 		/* remove from the linked list */
922 		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
923 		IFAFREE(&ia->ia_ifa);
924 
925 		/* also remove from the IPv6 address chain(itojun&jinmei) */
926 		oia = ia;
927 		if (oia == (ia = in6_ifaddr))
928 			in6_ifaddr = ia->ia_next;
929 		else {
930 			while (ia->ia_next && (ia->ia_next != oia))
931 				ia = ia->ia_next;
932 			if (ia->ia_next)
933 				ia->ia_next = oia->ia_next;
934 			else {
935 				nd6log((LOG_ERR,
936 				    "%s: didn't unlink in6ifaddr from "
937 				    "list\n", if_name(ifp)));
938 			}
939 		}
940 
941 		IFAFREE(&oia->ia_ifa);
942 	}
943 
944 	/* leave from all multicast groups joined */
945 	for (in6m = LIST_FIRST(&in6_multihead); in6m; in6m = in6m_next) {
946 		in6m_next = LIST_NEXT(in6m, in6m_entry);
947 		if (in6m->in6m_ifp != ifp)
948 			continue;
949 		in6_delmulti(in6m);
950 		in6m = NULL;
951 	}
952 
953 	/*
954 	 * remove neighbor management table.  we call it twice just to make
955 	 * sure we nuke everything.  maybe we need just one call.
956 	 * XXX: since the first call did not release addresses, some prefixes
957 	 * might remain.  We should call nd6_purge() again to release the
958 	 * prefixes after removing all addresses above.
959 	 * (Or can we just delay calling nd6_purge until at this point?)
960 	 */
961 	nd6_purge(ifp);
962 
963 	/* remove route to link-local allnodes multicast (ff02::1) */
964 	bzero(&sin6, sizeof(sin6));
965 	sin6.sin6_len = sizeof(struct sockaddr_in6);
966 	sin6.sin6_family = AF_INET6;
967 	sin6.sin6_addr = in6addr_linklocal_allnodes;
968 	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
969 	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
970 	if (rt && rt->rt_ifp == ifp) {
971 		rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
972 			rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
973 		rtfree(rt);
974 	}
975 }
976 
977 void
978 in6_get_tmpifid(ifp, retbuf, baseid, generate)
979 	struct ifnet *ifp;
980 	u_int8_t *retbuf;
981 	const u_int8_t *baseid;
982 	int generate;
983 {
984 	u_int8_t nullbuf[8];
985 	struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
986 
987 	bzero(nullbuf, sizeof(nullbuf));
988 	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
989 		/* we've never created a random ID.  Create a new one. */
990 		generate = 1;
991 	}
992 
993 	if (generate) {
994 		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
995 
996 		/* generate_tmp_ifid will update seedn and buf */
997 		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
998 					ndi->randomid);
999 	}
1000 	bcopy(ndi->randomid, retbuf, 8);
1001 }
1002 
1003 void
1004 in6_tmpaddrtimer(ignored_arg)
1005 	void *ignored_arg;
1006 {
1007 	int i;
1008 	struct nd_ifinfo *ndi;
1009 	u_int8_t nullbuf[8];
1010 	int s = splnet();
1011 
1012 	callout_reset(&in6_tmpaddrtimer_ch,
1013 		      (ip6_temp_preferred_lifetime - ip6_desync_factor -
1014 		       ip6_temp_regen_advance) * hz,
1015 		      in6_tmpaddrtimer, NULL);
1016 
1017 	bzero(nullbuf, sizeof(nullbuf));
1018 	for (i = 1; i < if_index + 1; i++) {
1019 		ndi = &nd_ifinfo[i];
1020 		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
1021 			/*
1022 			 * We've been generating a random ID on this interface.
1023 			 * Create a new one.
1024 			 */
1025 			(void)generate_tmp_ifid(ndi->randomseed0,
1026 						ndi->randomseed1,
1027 						ndi->randomid);
1028 		}
1029 	}
1030 
1031 	splx(s);
1032 }
1033