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