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