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