xref: /freebsd/sys/netinet6/in6_ifattach.c (revision c27f7d6b9cf6d4ab01cb3d0972726c14e0aca146)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	$KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
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/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/proc.h>
43 #include <sys/rmlock.h>
44 #include <sys/syslog.h>
45 #include <sys/md5.h>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/if_private.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
53 #include <net/vnet.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/udp.h>
61 #include <netinet/udp_var.h>
62 
63 #include <netinet/ip6.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/in6_var.h>
66 #include <netinet6/in6_pcb.h>
67 #include <netinet6/in6_ifattach.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/nd6.h>
70 #include <netinet6/mld6_var.h>
71 #include <netinet6/scope6_var.h>
72 
73 #ifdef IP6_AUTO_LINKLOCAL
74 VNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
75 #else
76 VNET_DEFINE(int, ip6_auto_linklocal) = 1;	/* enabled by default */
77 #endif
78 
79 VNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
80 #define	V_in6_tmpaddrtimer_ch		VNET(in6_tmpaddrtimer_ch)
81 
82 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
83 #define	V_ripcbinfo			VNET(ripcbinfo)
84 
85 static int get_rand_ifid(struct ifnet *, struct in6_addr *);
86 static int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
87 static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
88 static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
89 static int in6_ifattach_loopback(struct ifnet *);
90 static void in6_purgemaddrs(struct ifnet *);
91 
92 #define EUI64_GBIT	0x01
93 #define EUI64_UBIT	0x02
94 #define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
95 #define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
96 #define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
97 #define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
98 #define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
99 
100 #define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
101 #define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
102 
103 /*
104  * Generate a last-resort interface identifier, when the machine has no
105  * IEEE802/EUI64 address sources.
106  * The goal here is to get an interface identifier that is
107  * (1) random enough and (2) does not change across reboot.
108  * We currently use MD5(hostname) for it.
109  *
110  * in6 - upper 64bits are preserved
111  */
112 static int
113 get_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
114 {
115 	MD5_CTX ctxt;
116 	struct prison *pr;
117 	u_int8_t digest[16];
118 	int hostnamelen;
119 
120 	pr = curthread->td_ucred->cr_prison;
121 	mtx_lock(&pr->pr_mtx);
122 	hostnamelen = strlen(pr->pr_hostname);
123 #if 0
124 	/* we need at least several letters as seed for ifid */
125 	if (hostnamelen < 3) {
126 		mtx_unlock(&pr->pr_mtx);
127 		return -1;
128 	}
129 #endif
130 
131 	/* generate 8 bytes of pseudo-random value. */
132 	bzero(&ctxt, sizeof(ctxt));
133 	MD5Init(&ctxt);
134 	MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
135 	mtx_unlock(&pr->pr_mtx);
136 	MD5Final(digest, &ctxt);
137 
138 	/* assumes sizeof(digest) > sizeof(ifid) */
139 	bcopy(digest, &in6->s6_addr[8], 8);
140 
141 	/* make sure to set "u" bit to local, and "g" bit to individual. */
142 	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
143 	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
144 
145 	/* convert EUI64 into IPv6 interface identifier */
146 	EUI64_TO_IFID(in6);
147 
148 	return 0;
149 }
150 
151 static int
152 generate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
153 {
154 	MD5_CTX ctxt;
155 	u_int8_t seed[16], digest[16], nullbuf[8];
156 	u_int32_t val32;
157 
158 	/* If there's no history, start with a random seed. */
159 	bzero(nullbuf, sizeof(nullbuf));
160 	if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
161 		int i;
162 
163 		for (i = 0; i < 2; i++) {
164 			val32 = arc4random();
165 			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
166 		}
167 	} else
168 		bcopy(seed0, seed, 8);
169 
170 	/* copy the right-most 64-bits of the given address */
171 	/* XXX assumption on the size of IFID */
172 	bcopy(seed1, &seed[8], 8);
173 
174 	if (0) {		/* for debugging purposes only */
175 		int i;
176 
177 		printf("generate_tmp_ifid: new randomized ID from: ");
178 		for (i = 0; i < 16; i++)
179 			printf("%02x", seed[i]);
180 		printf(" ");
181 	}
182 
183 	/* generate 16 bytes of pseudo-random value. */
184 	bzero(&ctxt, sizeof(ctxt));
185 	MD5Init(&ctxt);
186 	MD5Update(&ctxt, seed, sizeof(seed));
187 	MD5Final(digest, &ctxt);
188 
189 	/*
190 	 * RFC 3041 3.2.1. (3)
191 	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
192 	 * left-most bit is numbered 0) to zero.
193 	 */
194 	bcopy(digest, ret, 8);
195 	ret[0] &= ~EUI64_UBIT;
196 
197 	/*
198 	 * XXX: we'd like to ensure that the generated value is not zero
199 	 * for simplicity.  If the caclculated digest happens to be zero,
200 	 * use a random non-zero value as the last resort.
201 	 */
202 	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
203 		nd6log((LOG_INFO,
204 		    "generate_tmp_ifid: computed MD5 value is zero.\n"));
205 
206 		val32 = arc4random();
207 		val32 = 1 + (val32 % (0xffffffff - 1));
208 	}
209 
210 	/*
211 	 * RFC 3041 3.2.1. (4)
212 	 * Take the rightmost 64-bits of the MD5 digest and save them in
213 	 * stable storage as the history value to be used in the next
214 	 * iteration of the algorithm.
215 	 */
216 	bcopy(&digest[8], seed0, 8);
217 
218 	if (0) {		/* for debugging purposes only */
219 		int i;
220 
221 		printf("to: ");
222 		for (i = 0; i < 16; i++)
223 			printf("%02x", digest[i]);
224 		printf("\n");
225 	}
226 
227 	return 0;
228 }
229 
230 /*
231  * Get interface identifier for the specified interface.
232  * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
233  *
234  * in6 - upper 64bits are preserved
235  */
236 int
237 in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
238 {
239 	struct ifaddr *ifa;
240 	struct sockaddr_dl *sdl;
241 	u_int8_t *addr;
242 	size_t addrlen;
243 	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
244 	static u_int8_t allone[8] =
245 		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
246 
247 	NET_EPOCH_ASSERT();
248 
249 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
250 		if (ifa->ifa_addr->sa_family != AF_LINK)
251 			continue;
252 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
253 		if (sdl == NULL)
254 			continue;
255 		if (sdl->sdl_alen == 0)
256 			continue;
257 
258 		goto found;
259 	}
260 
261 	return -1;
262 
263 found:
264 	addr = LLADDR(sdl);
265 	addrlen = sdl->sdl_alen;
266 
267 	/* get EUI64 */
268 	switch (ifp->if_type) {
269 	case IFT_BRIDGE:
270 	case IFT_ETHER:
271 	case IFT_L2VLAN:
272 	case IFT_ATM:
273 	case IFT_IEEE1394:
274 		/* IEEE802/EUI64 cases - what others? */
275 		/* IEEE1394 uses 16byte length address starting with EUI64 */
276 		if (addrlen > 8)
277 			addrlen = 8;
278 
279 		/* look at IEEE802/EUI64 only */
280 		if (addrlen != 8 && addrlen != 6)
281 			return -1;
282 
283 		/*
284 		 * check for invalid MAC address - on bsdi, we see it a lot
285 		 * since wildboar configures all-zero MAC on pccard before
286 		 * card insertion.
287 		 */
288 		if (bcmp(addr, allzero, addrlen) == 0)
289 			return -1;
290 		if (bcmp(addr, allone, addrlen) == 0)
291 			return -1;
292 
293 		/* make EUI64 address */
294 		if (addrlen == 8)
295 			bcopy(addr, &in6->s6_addr[8], 8);
296 		else if (addrlen == 6) {
297 			in6->s6_addr[8] = addr[0];
298 			in6->s6_addr[9] = addr[1];
299 			in6->s6_addr[10] = addr[2];
300 			in6->s6_addr[11] = 0xff;
301 			in6->s6_addr[12] = 0xfe;
302 			in6->s6_addr[13] = addr[3];
303 			in6->s6_addr[14] = addr[4];
304 			in6->s6_addr[15] = addr[5];
305 		}
306 		break;
307 
308 	case IFT_GIF:
309 	case IFT_STF:
310 		/*
311 		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
312 		 * however, IPv4 address is not very suitable as unique
313 		 * identifier source (can be renumbered).
314 		 * we don't do this.
315 		 */
316 		return -1;
317 
318 	case IFT_INFINIBAND:
319 		if (addrlen != 20)
320 			return -1;
321 		bcopy(addr + 12, &in6->s6_addr[8], 8);
322 		break;
323 
324 	default:
325 		return -1;
326 	}
327 
328 	/* sanity check: g bit must not indicate "group" */
329 	if (EUI64_GROUP(in6))
330 		return -1;
331 
332 	/* convert EUI64 into IPv6 interface identifier */
333 	EUI64_TO_IFID(in6);
334 
335 	/*
336 	 * sanity check: ifid must not be all zero, avoid conflict with
337 	 * subnet router anycast
338 	 */
339 	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
340 	    bcmp(&in6->s6_addr[9], allzero, 7) == 0)
341 		return -1;
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  * altifp - secondary EUI64 source
352  */
353 static int
354 get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
355     struct in6_addr *in6)
356 {
357 	struct ifnet *ifp;
358 
359 	NET_EPOCH_ASSERT();
360 
361 	/* first, try to get it from the interface itself */
362 	if (in6_get_hw_ifid(ifp0, in6) == 0) {
363 		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
364 		    if_name(ifp0)));
365 		goto success;
366 	}
367 
368 	/* try secondary EUI64 source. this basically is for ATM PVC */
369 	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
370 		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
371 		    if_name(ifp0), if_name(altifp)));
372 		goto success;
373 	}
374 
375 	/* next, try to get it from some other hardware interface */
376 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
377 		if (ifp == ifp0)
378 			continue;
379 		if (in6_get_hw_ifid(ifp, in6) != 0)
380 			continue;
381 
382 		/*
383 		 * to borrow ifid from other interface, ifid needs to be
384 		 * globally unique
385 		 */
386 		if (IFID_UNIVERSAL(in6)) {
387 			nd6log((LOG_DEBUG,
388 			    "%s: borrow interface identifier from %s\n",
389 			    if_name(ifp0), if_name(ifp)));
390 			goto success;
391 		}
392 	}
393 
394 	/* last resort: get from random number source */
395 	if (get_rand_ifid(ifp, in6) == 0) {
396 		nd6log((LOG_DEBUG,
397 		    "%s: interface identifier generated by random number\n",
398 		    if_name(ifp0)));
399 		goto success;
400 	}
401 
402 	printf("%s: failed to get interface identifier\n", if_name(ifp0));
403 	return -1;
404 
405 success:
406 	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
407 	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
408 	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
409 	    in6->s6_addr[14], in6->s6_addr[15]));
410 	return 0;
411 }
412 
413 /*
414  * altifp - secondary EUI64 source
415  */
416 static int
417 in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
418 {
419 	struct in6_ifaddr *ia;
420 	struct in6_aliasreq ifra;
421 	struct nd_prefixctl pr0;
422 	struct epoch_tracker et;
423 	struct nd_prefix *pr;
424 	int error;
425 
426 	/*
427 	 * configure link-local address.
428 	 */
429 	in6_prepare_ifra(&ifra, NULL, &in6mask64);
430 
431 	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
432 	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
433 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
434 		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
435 		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
436 	} else {
437 		NET_EPOCH_ENTER(et);
438 		error = get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr);
439 		NET_EPOCH_EXIT(et);
440 		if (error != 0) {
441 			nd6log((LOG_ERR,
442 			    "%s: no ifid available\n", if_name(ifp)));
443 			return (-1);
444 		}
445 	}
446 	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
447 		return (-1);
448 
449 	/* link-local addresses should NEVER expire. */
450 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
451 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
452 
453 	/*
454 	 * Now call in6_update_ifa() to do a bunch of procedures to configure
455 	 * a link-local address. We can set the 3rd argument to NULL, because
456 	 * we know there's no other link-local address on the interface
457 	 * and therefore we are adding one (instead of updating one).
458 	 */
459 	if ((error = in6_update_ifa(ifp, &ifra, NULL,
460 				    IN6_IFAUPDATE_DADDELAY)) != 0) {
461 		/*
462 		 * XXX: When the interface does not support IPv6, this call
463 		 * would fail in the SIOCSIFADDR ioctl.  I believe the
464 		 * notification is rather confusing in this case, so just
465 		 * suppress it.  (jinmei@kame.net 20010130)
466 		 */
467 		if (error != EAFNOSUPPORT)
468 			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
469 			    "configure a link-local address on %s "
470 			    "(errno=%d)\n",
471 			    if_name(ifp), error));
472 		return (-1);
473 	}
474 
475 	NET_EPOCH_ENTER(et);
476 	ia = in6ifa_ifpforlinklocal(ifp, 0);
477 	NET_EPOCH_EXIT(et);
478 	if (ia == NULL) {
479 		/*
480 		 * Another thread removed the address that we just added.
481 		 * This should be rare, but it happens.
482 		 */
483 		nd6log((LOG_NOTICE, "%s: %s: new link-local address "
484 			"disappeared\n", __func__, if_name(ifp)));
485 		return (-1);
486 	}
487 	ifa_free(&ia->ia_ifa);
488 
489 	/*
490 	 * Make the link-local prefix (fe80::%link/64) as on-link.
491 	 * Since we'd like to manage prefixes separately from addresses,
492 	 * we make an ND6 prefix structure for the link-local prefix,
493 	 * and add it to the prefix list as a never-expire prefix.
494 	 * XXX: this change might affect some existing code base...
495 	 */
496 	bzero(&pr0, sizeof(pr0));
497 	pr0.ndpr_ifp = ifp;
498 	/* this should be 64 at this moment. */
499 	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
500 	pr0.ndpr_prefix = ifra.ifra_addr;
501 	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
502 	IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, &in6mask64);
503 	/*
504 	 * Initialize parameters.  The link-local prefix must always be
505 	 * on-link, and its lifetimes never expire.
506 	 */
507 	pr0.ndpr_raf_onlink = 1;
508 	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
509 	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
510 	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
511 	/*
512 	 * Since there is no other link-local addresses, nd6_prefix_lookup()
513 	 * probably returns NULL.  However, we cannot always expect the result.
514 	 * For example, if we first remove the (only) existing link-local
515 	 * address, and then reconfigure another one, the prefix is still
516 	 * valid with referring to the old link-local address.
517 	 */
518 	if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
519 		if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
520 			return (error);
521 		/* Reference prefix */
522 		ia->ia6_ndpr = pr;
523 		pr->ndpr_addrcnt++;
524 	} else
525 		nd6_prefix_rele(pr);
526 
527 	return 0;
528 }
529 
530 /*
531  * ifp - must be IFT_LOOP
532  */
533 static int
534 in6_ifattach_loopback(struct ifnet *ifp)
535 {
536 	struct in6_aliasreq ifra;
537 	int error;
538 
539 	in6_prepare_ifra(&ifra, &in6addr_loopback, &in6mask128);
540 
541 	/*
542 	 * Always initialize ia_dstaddr (= broadcast address) to loopback
543 	 * address.  Follows IPv4 practice - see in_ifinit().
544 	 */
545 	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
546 	ifra.ifra_dstaddr.sin6_family = AF_INET6;
547 	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
548 
549 	/* the loopback  address should NEVER expire. */
550 	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
551 	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
552 
553 	/*
554 	 * We are sure that this is a newly assigned address, so we can set
555 	 * NULL to the 3rd arg.
556 	 */
557 	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
558 		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
559 		    "the loopback address on %s (errno=%d)\n",
560 		    if_name(ifp), error));
561 		return (-1);
562 	}
563 
564 	return 0;
565 }
566 
567 /*
568  * compute NI group address, based on the current hostname setting.
569  * see RFC 4620.
570  *
571  * when ifp == NULL, the caller is responsible for filling scopeid.
572  *
573  * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
574  * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
575  */
576 static int
577 in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
578     struct in6_addr *in6, int oldmcprefix)
579 {
580 	struct prison *pr;
581 	const char *p;
582 	u_char *q;
583 	MD5_CTX ctxt;
584 	u_int8_t digest[16];
585 	char l;
586 	char n[64];	/* a single label must not exceed 63 chars */
587 
588 	/*
589 	 * If no name is given and namelen is -1,
590 	 * we try to do the hostname lookup ourselves.
591 	 */
592 	if (!name && namelen == -1) {
593 		pr = curthread->td_ucred->cr_prison;
594 		mtx_lock(&pr->pr_mtx);
595 		name = pr->pr_hostname;
596 		namelen = strlen(name);
597 	} else
598 		pr = NULL;
599 	if (!name || !namelen) {
600 		if (pr != NULL)
601 			mtx_unlock(&pr->pr_mtx);
602 		return -1;
603 	}
604 
605 	p = name;
606 	while (p && *p && *p != '.' && p - name < namelen)
607 		p++;
608 	if (p == name || p - name > sizeof(n) - 1) {
609 		if (pr != NULL)
610 			mtx_unlock(&pr->pr_mtx);
611 		return -1;	/* label too long */
612 	}
613 	l = p - name;
614 	strncpy(n, name, l);
615 	if (pr != NULL)
616 		mtx_unlock(&pr->pr_mtx);
617 	n[(int)l] = '\0';
618 	for (q = n; *q; q++) {
619 		if ('A' <= *q && *q <= 'Z')
620 			*q = *q - 'A' + 'a';
621 	}
622 
623 	/* generate 16 bytes of pseudo-random value. */
624 	bzero(&ctxt, sizeof(ctxt));
625 	MD5Init(&ctxt);
626 	MD5Update(&ctxt, &l, sizeof(l));
627 	MD5Update(&ctxt, n, l);
628 	MD5Final(digest, &ctxt);
629 
630 	bzero(in6, sizeof(*in6));
631 	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
632 	in6->s6_addr8[11] = 2;
633 	if (oldmcprefix == 0) {
634 		in6->s6_addr8[12] = 0xff;
635 	 	/* Copy the first 24 bits of 128-bit hash into the address. */
636 		bcopy(digest, &in6->s6_addr8[13], 3);
637 	} else {
638 	 	/* Copy the first 32 bits of 128-bit hash into the address. */
639 		bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
640 	}
641 	if (in6_setscope(in6, ifp, NULL))
642 		return (-1); /* XXX: should not fail */
643 
644 	return 0;
645 }
646 
647 int
648 in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
649     struct in6_addr *in6)
650 {
651 
652 	return (in6_nigroup0(ifp, name, namelen, in6, 0));
653 }
654 
655 int
656 in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
657     struct in6_addr *in6)
658 {
659 
660 	return (in6_nigroup0(ifp, name, namelen, in6, 1));
661 }
662 
663 /*
664  * XXX multiple loopback interface needs more care.  for instance,
665  * nodelocal address needs to be configured onto only one of them.
666  * XXX multiple link-local address case
667  *
668  * altifp - secondary EUI64 source
669  */
670 void
671 in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
672 {
673 	struct in6_ifaddr *ia;
674 
675 	if (ifp->if_afdata[AF_INET6] == NULL)
676 		return;
677 	/*
678 	 * quirks based on interface type
679 	 */
680 	switch (ifp->if_type) {
681 	case IFT_STF:
682 		/*
683 		 * 6to4 interface is a very special kind of beast.
684 		 * no multicast, no linklocal.  RFC2529 specifies how to make
685 		 * linklocals for 6to4 interface, but there's no use and
686 		 * it is rather harmful to have one.
687 		 */
688 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
689 		ND_IFINFO(ifp)->flags |= ND6_IFF_NO_DAD;
690 		break;
691 	default:
692 		break;
693 	}
694 
695 	/*
696 	 * usually, we require multicast capability to the interface
697 	 */
698 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
699 		nd6log((LOG_INFO, "in6_ifattach: "
700 		    "%s is not multicast capable, IPv6 not enabled\n",
701 		    if_name(ifp)));
702 		return;
703 	}
704 
705 	/*
706 	 * assign loopback address for loopback interface.
707 	 */
708 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
709 		/*
710 		 * check that loopback address doesn't exist yet.
711 		 */
712 		ia = in6ifa_ifwithaddr(&in6addr_loopback, 0, false);
713 		if (ia == NULL)
714 			in6_ifattach_loopback(ifp);
715 	}
716 
717 	/*
718 	 * assign a link-local address, if there's none.
719 	 */
720 	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
721 	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
722 		struct epoch_tracker et;
723 
724 		NET_EPOCH_ENTER(et);
725 		ia = in6ifa_ifpforlinklocal(ifp, 0);
726 		NET_EPOCH_EXIT(et);
727 		if (ia == NULL)
728 			in6_ifattach_linklocal(ifp, altifp);
729 		else
730 			ifa_free(&ia->ia_ifa);
731 	}
732 }
733 
734 /*
735  * NOTE: in6_ifdetach() does not support loopback if at this moment.
736  *
737  * When shutting down a VNET we clean up layers top-down.  In that case
738  * upper layer protocols (ulp) are cleaned up already and locks are destroyed
739  * and we must not call into these cleanup functions anymore, thus purgeulp
740  * is set to 0 in that case by in6_ifdetach_destroy().
741  * The normal case of destroying a (cloned) interface still needs to cleanup
742  * everything related to the interface and will have purgeulp set to 1.
743  */
744 static void
745 _in6_ifdetach(struct ifnet *ifp, int purgeulp)
746 {
747 	struct ifaddr *ifa, *next;
748 
749 	if (ifp->if_afdata[AF_INET6] == NULL)
750 		return;
751 
752 	/*
753 	 * nuke any of IPv6 addresses we have
754 	 */
755 	CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
756 		if (ifa->ifa_addr->sa_family != AF_INET6)
757 			continue;
758 		in6_purgeaddr(ifa);
759 	}
760 	if (purgeulp) {
761 		IN6_MULTI_LOCK();
762 		in6_pcbpurgeif0(&V_udbinfo, ifp);
763 		in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
764 		in6_pcbpurgeif0(&V_ripcbinfo, ifp);
765 		IN6_MULTI_UNLOCK();
766 	}
767 	/* leave from all multicast groups joined */
768 	in6_purgemaddrs(ifp);
769 
770 	/*
771 	 * Remove neighbor management table.
772 	 * Enabling the nd6_purge will panic on vmove for interfaces on VNET
773 	 * teardown as the IPv6 layer is cleaned up already and the locks
774 	 * are destroyed.
775 	 */
776 	if (purgeulp)
777 		nd6_purge(ifp);
778 }
779 
780 void
781 in6_ifdetach(struct ifnet *ifp)
782 {
783 
784 	_in6_ifdetach(ifp, 1);
785 }
786 
787 void
788 in6_ifdetach_destroy(struct ifnet *ifp)
789 {
790 
791 	_in6_ifdetach(ifp, 0);
792 }
793 
794 int
795 in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
796     const u_int8_t *baseid, int generate)
797 {
798 	u_int8_t nullbuf[8];
799 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
800 
801 	bzero(nullbuf, sizeof(nullbuf));
802 	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
803 		/* we've never created a random ID.  Create a new one. */
804 		generate = 1;
805 	}
806 
807 	if (generate) {
808 		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
809 
810 		/* generate_tmp_ifid will update seedn and buf */
811 		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
812 		    ndi->randomid);
813 	}
814 	bcopy(ndi->randomid, retbuf, 8);
815 
816 	return (0);
817 }
818 
819 void
820 in6_tmpaddrtimer(void *arg)
821 {
822 	CURVNET_SET((struct vnet *) arg);
823 	struct epoch_tracker et;
824 	struct nd_ifinfo *ndi;
825 	u_int8_t nullbuf[8];
826 	struct ifnet *ifp;
827 
828 	callout_reset(&V_in6_tmpaddrtimer_ch,
829 	    (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
830 	    V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
831 
832 	bzero(nullbuf, sizeof(nullbuf));
833 	NET_EPOCH_ENTER(et);
834 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
835 		if (ifp->if_afdata[AF_INET6] == NULL)
836 			continue;
837 		ndi = ND_IFINFO(ifp);
838 		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
839 			/*
840 			 * We've been generating a random ID on this interface.
841 			 * Create a new one.
842 			 */
843 			(void)generate_tmp_ifid(ndi->randomseed0,
844 			    ndi->randomseed1, ndi->randomid);
845 		}
846 	}
847 	NET_EPOCH_EXIT(et);
848 	CURVNET_RESTORE();
849 }
850 
851 static void
852 in6_purgemaddrs(struct ifnet *ifp)
853 {
854 	struct in6_multi_head inmh;
855 
856 	SLIST_INIT(&inmh);
857 	IN6_MULTI_LOCK();
858 	IN6_MULTI_LIST_LOCK();
859 	mld_ifdetach(ifp, &inmh);
860 	IN6_MULTI_LIST_UNLOCK();
861 	IN6_MULTI_UNLOCK();
862 	in6m_release_list_deferred(&inmh);
863 
864 	/*
865 	 * Make sure all multicast deletions invoking if_ioctl() are
866 	 * completed before returning. Else we risk accessing a freed
867 	 * ifnet structure pointer.
868 	 */
869 	in6m_release_wait(NULL);
870 }
871 
872 void
873 in6_ifattach_destroy(void)
874 {
875 
876 	callout_drain(&V_in6_tmpaddrtimer_ch);
877 }
878 
879 static void
880 in6_ifattach_init(void *dummy)
881 {
882 
883 	/* Timer for regeneranation of temporary addresses randomize ID. */
884 	callout_init(&V_in6_tmpaddrtimer_ch, 1);
885 	callout_reset(&V_in6_tmpaddrtimer_ch,
886 	    (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
887 	    V_ip6_temp_regen_advance) * hz,
888 	    in6_tmpaddrtimer, curvnet);
889 }
890 
891 /*
892  * Cheat.
893  * This must be after route_init(), which is now SI_ORDER_THIRD.
894  */
895 SYSINIT(in6_ifattach_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
896     in6_ifattach_init, NULL);
897