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