in6_ifattach.c revision 287731
1139823Simp/*-
2103026Ssobomax * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3284066Sae * All rights reserved.
4103026Ssobomax *
5103026Ssobomax * Redistribution and use in source and binary forms, with or without
6103026Ssobomax * modification, are permitted provided that the following conditions
7103026Ssobomax * are met:
8103026Ssobomax * 1. Redistributions of source code must retain the above copyright
9148613Sbz *    notice, this list of conditions and the following disclaimer.
10148613Sbz * 2. Redistributions in binary form must reproduce the above copyright
11103026Ssobomax *    notice, this list of conditions and the following disclaimer in the
12103026Ssobomax *    documentation and/or other materials provided with the distribution.
13103026Ssobomax * 3. Neither the name of the project nor the names of its contributors
14103026Ssobomax *    may be used to endorse or promote products derived from this software
15103026Ssobomax *    without specific prior written permission.
16103026Ssobomax *
17103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18103026Ssobomax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19103026Ssobomax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20103026Ssobomax * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21103026Ssobomax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22103026Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23103026Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24103026Ssobomax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25103026Ssobomax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26103026Ssobomax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27103026Ssobomax * SUCH DAMAGE.
28103026Ssobomax *
29103026Ssobomax *	$KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
30103026Ssobomax */
31284066Sae
32284066Sae#include <sys/cdefs.h>
33103026Ssobomax__FBSDID("$FreeBSD: stable/10/sys/netinet6/in6_ifattach.c 287731 2015-09-13 01:39:37Z hrs $");
34103026Ssobomax
35172467Ssilby#include <sys/param.h>
36172467Ssilby#include <sys/systm.h>
37172467Ssilby#include <sys/malloc.h>
38103026Ssobomax#include <sys/socket.h>
39148613Sbz#include <sys/sockio.h>
40103026Ssobomax#include <sys/jail.h>
41103026Ssobomax#include <sys/kernel.h>
42103026Ssobomax#include <sys/proc.h>
43103026Ssobomax#include <sys/syslog.h>
44103026Ssobomax#include <sys/md5.h>
45103026Ssobomax
46103026Ssobomax#include <net/if.h>
47103026Ssobomax#include <net/if_dl.h>
48103026Ssobomax#include <net/if_types.h>
49103026Ssobomax#include <net/route.h>
50284066Sae#include <net/vnet.h>
51284066Sae
52284066Sae#include <netinet/in.h>
53103026Ssobomax#include <netinet/in_var.h>
54103026Ssobomax#include <netinet/if_ether.h>
55284066Sae#include <netinet/in_pcb.h>
56284066Sae#include <netinet/ip_var.h>
57103026Ssobomax#include <netinet/udp.h>
58103026Ssobomax#include <netinet/udp_var.h>
59103026Ssobomax
60103026Ssobomax#include <netinet/ip6.h>
61103026Ssobomax#include <netinet6/ip6_var.h>
62103026Ssobomax#include <netinet6/in6_var.h>
63284066Sae#include <netinet6/in6_pcb.h>
64103026Ssobomax#include <netinet6/in6_ifattach.h>
65103026Ssobomax#include <netinet6/ip6_var.h>
66103026Ssobomax#include <netinet6/nd6.h>
67284066Sae#include <netinet6/mld6_var.h>
68284066Sae#include <netinet6/scope6_var.h>
69103026Ssobomax
70103026SsobomaxVNET_DEFINE(unsigned long, in6_maxmtu) = 0;
71103026Ssobomax
72103026Ssobomax#ifdef IP6_AUTO_LINKLOCAL
73103026SsobomaxVNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
74103026Ssobomax#else
75284066SaeVNET_DEFINE(int, ip6_auto_linklocal) = 1;	/* enabled by default */
76284066Sae#endif
77284066Sae
78284066SaeVNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
79284066Sae#define	V_in6_tmpaddrtimer_ch		VNET(in6_tmpaddrtimer_ch)
80284066Sae
81284066SaeVNET_DECLARE(struct inpcbinfo, ripcbinfo);
82284066Sae#define	V_ripcbinfo			VNET(ripcbinfo)
83284066Sae
84284066Saestatic int get_rand_ifid(struct ifnet *, struct in6_addr *);
85284066Saestatic int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
86284066Saestatic int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
87284066Saestatic int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
88103026Ssobomaxstatic int in6_ifattach_loopback(struct ifnet *);
89284066Saestatic void in6_purgemaddrs(struct ifnet *);
90284066Sae
91284066Sae#define EUI64_GBIT	0x01
92284066Sae#define EUI64_UBIT	0x02
93284066Sae#define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
94284066Sae#define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
95284066Sae#define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
96284066Sae#define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
97103026Ssobomax#define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
98158645Sglebius
99103026Ssobomax#define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
100103026Ssobomax#define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
101284066Sae
102103026Ssobomax/*
103103026Ssobomax * Generate a last-resort interface identifier, when the machine has no
104284066Sae * IEEE802/EUI64 address sources.
105284066Sae * The goal here is to get an interface identifier that is
106103026Ssobomax * (1) random enough and (2) does not change across reboot.
107284066Sae * We currently use MD5(hostname) for it.
108103026Ssobomax *
109284066Sae * in6 - upper 64bits are preserved
110103026Ssobomax */
111284066Saestatic int
112284066Saeget_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
113284066Sae{
114103026Ssobomax	MD5_CTX ctxt;
115284066Sae	struct prison *pr;
116284066Sae	u_int8_t digest[16];
117284066Sae	int hostnamelen;
118284066Sae
119284066Sae	pr = curthread->td_ucred->cr_prison;
120284066Sae	mtx_lock(&pr->pr_mtx);
121284066Sae	hostnamelen = strlen(pr->pr_hostname);
122123992Ssobomax#if 0
123284066Sae	/* we need at least several letters as seed for ifid */
124284066Sae	if (hostnamelen < 3) {
125284066Sae		mtx_unlock(&pr->pr_mtx);
126103026Ssobomax		return -1;
127284066Sae	}
128284066Sae#endif
129103026Ssobomax
130284066Sae	/* generate 8 bytes of pseudo-random value. */
131284066Sae	bzero(&ctxt, sizeof(ctxt));
132284066Sae	MD5Init(&ctxt);
133284066Sae	MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
134103026Ssobomax	mtx_unlock(&pr->pr_mtx);
135284066Sae	MD5Final(digest, &ctxt);
136284066Sae
137284066Sae	/* assumes sizeof(digest) > sizeof(ifid) */
138284066Sae	bcopy(digest, &in6->s6_addr[8], 8);
139284066Sae
140103026Ssobomax	/* make sure to set "u" bit to local, and "g" bit to individual. */
141103026Ssobomax	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
142284066Sae	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
143284066Sae
144103026Ssobomax	/* convert EUI64 into IPv6 interface identifier */
145284066Sae	EUI64_TO_IFID(in6);
146103026Ssobomax
147284066Sae	return 0;
148284066Sae}
149284066Sae
150284066Saestatic int
151284066Saegenerate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
152284066Sae{
153284066Sae	MD5_CTX ctxt;
154284066Sae	u_int8_t seed[16], digest[16], nullbuf[8];
155284066Sae	u_int32_t val32;
156284066Sae
157284066Sae	/* If there's no history, start with a random seed. */
158284066Sae	bzero(nullbuf, sizeof(nullbuf));
159284066Sae	if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
160284066Sae		int i;
161284066Sae
162284066Sae		for (i = 0; i < 2; i++) {
163284066Sae			val32 = arc4random();
164284066Sae			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
165284066Sae		}
166103026Ssobomax	} else
167284066Sae		bcopy(seed0, seed, 8);
168284066Sae
169284066Sae	/* copy the right-most 64-bits of the given address */
170103026Ssobomax	/* XXX assumption on the size of IFID */
171103026Ssobomax	bcopy(seed1, &seed[8], 8);
172284066Sae
173284066Sae	if (0) {		/* for debugging purposes only */
174103026Ssobomax		int i;
175103026Ssobomax
176284066Sae		printf("generate_tmp_ifid: new randomized ID from: ");
177284066Sae		for (i = 0; i < 16; i++)
178284066Sae			printf("%02x", seed[i]);
179284066Sae		printf(" ");
180284066Sae	}
181284066Sae
182103026Ssobomax	/* generate 16 bytes of pseudo-random value. */
183	bzero(&ctxt, sizeof(ctxt));
184	MD5Init(&ctxt);
185	MD5Update(&ctxt, seed, sizeof(seed));
186	MD5Final(digest, &ctxt);
187
188	/*
189	 * RFC 3041 3.2.1. (3)
190	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
191	 * left-most bit is numbered 0) to zero.
192	 */
193	bcopy(digest, ret, 8);
194	ret[0] &= ~EUI64_UBIT;
195
196	/*
197	 * XXX: we'd like to ensure that the generated value is not zero
198	 * for simplicity.  If the caclculated digest happens to be zero,
199	 * use a random non-zero value as the last resort.
200	 */
201	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
202		nd6log((LOG_INFO,
203		    "generate_tmp_ifid: computed MD5 value is zero.\n"));
204
205		val32 = arc4random();
206		val32 = 1 + (val32 % (0xffffffff - 1));
207	}
208
209	/*
210	 * RFC 3041 3.2.1. (4)
211	 * Take the rightmost 64-bits of the MD5 digest and save them in
212	 * stable storage as the history value to be used in the next
213	 * iteration of the algorithm.
214	 */
215	bcopy(&digest[8], seed0, 8);
216
217	if (0) {		/* for debugging purposes only */
218		int i;
219
220		printf("to: ");
221		for (i = 0; i < 16; i++)
222			printf("%02x", digest[i]);
223		printf("\n");
224	}
225
226	return 0;
227}
228
229/*
230 * Get interface identifier for the specified interface.
231 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
232 *
233 * in6 - upper 64bits are preserved
234 */
235int
236in6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
237{
238	struct ifaddr *ifa;
239	struct sockaddr_dl *sdl;
240	u_int8_t *addr;
241	size_t addrlen;
242	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
243	static u_int8_t allone[8] =
244		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
245
246	IF_ADDR_RLOCK(ifp);
247	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
248		if (ifa->ifa_addr->sa_family != AF_LINK)
249			continue;
250		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
251		if (sdl == NULL)
252			continue;
253		if (sdl->sdl_alen == 0)
254			continue;
255
256		goto found;
257	}
258	IF_ADDR_RUNLOCK(ifp);
259
260	return -1;
261
262found:
263	IF_ADDR_LOCK_ASSERT(ifp);
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_FDDI:
273	case IFT_ISO88025:
274	case IFT_ATM:
275	case IFT_IEEE1394:
276#ifdef IFT_IEEE80211
277	case IFT_IEEE80211:
278#endif
279		/* IEEE802/EUI64 cases - what others? */
280		/* IEEE1394 uses 16byte length address starting with EUI64 */
281		if (addrlen > 8)
282			addrlen = 8;
283
284		/* look at IEEE802/EUI64 only */
285		if (addrlen != 8 && addrlen != 6) {
286			IF_ADDR_RUNLOCK(ifp);
287			return -1;
288		}
289
290		/*
291		 * check for invalid MAC address - on bsdi, we see it a lot
292		 * since wildboar configures all-zero MAC on pccard before
293		 * card insertion.
294		 */
295		if (bcmp(addr, allzero, addrlen) == 0) {
296			IF_ADDR_RUNLOCK(ifp);
297			return -1;
298		}
299		if (bcmp(addr, allone, addrlen) == 0) {
300			IF_ADDR_RUNLOCK(ifp);
301			return -1;
302		}
303
304		/* make EUI64 address */
305		if (addrlen == 8)
306			bcopy(addr, &in6->s6_addr[8], 8);
307		else if (addrlen == 6) {
308			in6->s6_addr[8] = addr[0];
309			in6->s6_addr[9] = addr[1];
310			in6->s6_addr[10] = addr[2];
311			in6->s6_addr[11] = 0xff;
312			in6->s6_addr[12] = 0xfe;
313			in6->s6_addr[13] = addr[3];
314			in6->s6_addr[14] = addr[4];
315			in6->s6_addr[15] = addr[5];
316		}
317		break;
318
319	case IFT_ARCNET:
320		if (addrlen != 1) {
321			IF_ADDR_RUNLOCK(ifp);
322			return -1;
323		}
324		if (!addr[0]) {
325			IF_ADDR_RUNLOCK(ifp);
326			return -1;
327		}
328
329		bzero(&in6->s6_addr[8], 8);
330		in6->s6_addr[15] = addr[0];
331
332		/*
333		 * due to insufficient bitwidth, we mark it local.
334		 */
335		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
336		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
337		break;
338
339	case IFT_GIF:
340#ifdef IFT_STF
341	case IFT_STF:
342#endif
343		/*
344		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
345		 * however, IPv4 address is not very suitable as unique
346		 * identifier source (can be renumbered).
347		 * we don't do this.
348		 */
349		IF_ADDR_RUNLOCK(ifp);
350		return -1;
351
352	default:
353		IF_ADDR_RUNLOCK(ifp);
354		return -1;
355	}
356
357	/* sanity check: g bit must not indicate "group" */
358	if (EUI64_GROUP(in6)) {
359		IF_ADDR_RUNLOCK(ifp);
360		return -1;
361	}
362
363	/* convert EUI64 into IPv6 interface identifier */
364	EUI64_TO_IFID(in6);
365
366	/*
367	 * sanity check: ifid must not be all zero, avoid conflict with
368	 * subnet router anycast
369	 */
370	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
371	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
372		IF_ADDR_RUNLOCK(ifp);
373		return -1;
374	}
375
376	IF_ADDR_RUNLOCK(ifp);
377	return 0;
378}
379
380/*
381 * Get interface identifier for the specified interface.  If it is not
382 * available on ifp0, borrow interface identifier from other information
383 * sources.
384 *
385 * altifp - secondary EUI64 source
386 */
387static int
388get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
389    struct in6_addr *in6)
390{
391	struct ifnet *ifp;
392
393	/* first, try to get it from the interface itself */
394	if (in6_get_hw_ifid(ifp0, in6) == 0) {
395		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
396		    if_name(ifp0)));
397		goto success;
398	}
399
400	/* try secondary EUI64 source. this basically is for ATM PVC */
401	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
402		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
403		    if_name(ifp0), if_name(altifp)));
404		goto success;
405	}
406
407	/* next, try to get it from some other hardware interface */
408	IFNET_RLOCK_NOSLEEP();
409	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
410		if (ifp == ifp0)
411			continue;
412		if (in6_get_hw_ifid(ifp, in6) != 0)
413			continue;
414
415		/*
416		 * to borrow ifid from other interface, ifid needs to be
417		 * globally unique
418		 */
419		if (IFID_UNIVERSAL(in6)) {
420			nd6log((LOG_DEBUG,
421			    "%s: borrow interface identifier from %s\n",
422			    if_name(ifp0), if_name(ifp)));
423			IFNET_RUNLOCK_NOSLEEP();
424			goto success;
425		}
426	}
427	IFNET_RUNLOCK_NOSLEEP();
428
429	/* last resort: get from random number source */
430	if (get_rand_ifid(ifp, in6) == 0) {
431		nd6log((LOG_DEBUG,
432		    "%s: interface identifier generated by random number\n",
433		    if_name(ifp0)));
434		goto success;
435	}
436
437	printf("%s: failed to get interface identifier\n", if_name(ifp0));
438	return -1;
439
440success:
441	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
442	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
443	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
444	    in6->s6_addr[14], in6->s6_addr[15]));
445	return 0;
446}
447
448/*
449 * altifp - secondary EUI64 source
450 */
451static int
452in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
453{
454	struct in6_ifaddr *ia;
455	struct in6_aliasreq ifra;
456	struct nd_prefixctl pr0;
457	int i, error;
458
459	/*
460	 * configure link-local address.
461	 */
462	bzero(&ifra, sizeof(ifra));
463
464	/*
465	 * in6_update_ifa() does not use ifra_name, but we accurately set it
466	 * for safety.
467	 */
468	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
469
470	ifra.ifra_addr.sin6_family = AF_INET6;
471	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
472	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
473	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
474	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
475		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
476		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
477	} else {
478		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
479			nd6log((LOG_ERR,
480			    "%s: no ifid available\n", if_name(ifp)));
481			return (-1);
482		}
483	}
484	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
485		return (-1);
486
487	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
488	ifra.ifra_prefixmask.sin6_family = AF_INET6;
489	ifra.ifra_prefixmask.sin6_addr = in6mask64;
490	/* link-local addresses should NEVER expire. */
491	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
492	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
493
494	/*
495	 * Now call in6_update_ifa() to do a bunch of procedures to configure
496	 * a link-local address. We can set the 3rd argument to NULL, because
497	 * we know there's no other link-local address on the interface
498	 * and therefore we are adding one (instead of updating one).
499	 */
500	if ((error = in6_update_ifa(ifp, &ifra, NULL,
501				    IN6_IFAUPDATE_DADDELAY)) != 0) {
502		/*
503		 * XXX: When the interface does not support IPv6, this call
504		 * would fail in the SIOCSIFADDR ioctl.  I believe the
505		 * notification is rather confusing in this case, so just
506		 * suppress it.  (jinmei@kame.net 20010130)
507		 */
508		if (error != EAFNOSUPPORT)
509			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
510			    "configure a link-local address on %s "
511			    "(errno=%d)\n",
512			    if_name(ifp), error));
513		return (-1);
514	}
515
516	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
517	KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
518
519	ifa_free(&ia->ia_ifa);
520
521	/*
522	 * Make the link-local prefix (fe80::%link/64) as on-link.
523	 * Since we'd like to manage prefixes separately from addresses,
524	 * we make an ND6 prefix structure for the link-local prefix,
525	 * and add it to the prefix list as a never-expire prefix.
526	 * XXX: this change might affect some existing code base...
527	 */
528	bzero(&pr0, sizeof(pr0));
529	pr0.ndpr_ifp = ifp;
530	/* this should be 64 at this moment. */
531	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
532	pr0.ndpr_prefix = ifra.ifra_addr;
533	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
534	for (i = 0; i < 4; i++) {
535		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
536		    in6mask64.s6_addr32[i];
537	}
538	/*
539	 * Initialize parameters.  The link-local prefix must always be
540	 * on-link, and its lifetimes never expire.
541	 */
542	pr0.ndpr_raf_onlink = 1;
543	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
544	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
545	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
546	/*
547	 * Since there is no other link-local addresses, nd6_prefix_lookup()
548	 * probably returns NULL.  However, we cannot always expect the result.
549	 * For example, if we first remove the (only) existing link-local
550	 * address, and then reconfigure another one, the prefix is still
551	 * valid with referring to the old link-local address.
552	 */
553	if (nd6_prefix_lookup(&pr0) == NULL) {
554		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
555			return (error);
556	}
557
558	return 0;
559}
560
561/*
562 * ifp - must be IFT_LOOP
563 */
564static int
565in6_ifattach_loopback(struct ifnet *ifp)
566{
567	struct in6_aliasreq ifra;
568	int error;
569
570	bzero(&ifra, sizeof(ifra));
571
572	/*
573	 * in6_update_ifa() does not use ifra_name, but we accurately set it
574	 * for safety.
575	 */
576	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
577
578	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
579	ifra.ifra_prefixmask.sin6_family = AF_INET6;
580	ifra.ifra_prefixmask.sin6_addr = in6mask128;
581
582	/*
583	 * Always initialize ia_dstaddr (= broadcast address) to loopback
584	 * address.  Follows IPv4 practice - see in_ifinit().
585	 */
586	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
587	ifra.ifra_dstaddr.sin6_family = AF_INET6;
588	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
589
590	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
591	ifra.ifra_addr.sin6_family = AF_INET6;
592	ifra.ifra_addr.sin6_addr = in6addr_loopback;
593
594	/* the loopback  address should NEVER expire. */
595	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
596	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
597
598	/* we don't need to perform DAD on loopback interfaces. */
599	ifra.ifra_flags |= IN6_IFF_NODAD;
600
601	/*
602	 * We are sure that this is a newly assigned address, so we can set
603	 * NULL to the 3rd arg.
604	 */
605	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
606		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
607		    "the loopback address on %s (errno=%d)\n",
608		    if_name(ifp), error));
609		return (-1);
610	}
611
612	return 0;
613}
614
615/*
616 * compute NI group address, based on the current hostname setting.
617 * see RFC 4620.
618 *
619 * when ifp == NULL, the caller is responsible for filling scopeid.
620 *
621 * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
622 * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
623 */
624static int
625in6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
626    struct in6_addr *in6, int oldmcprefix)
627{
628	struct prison *pr;
629	const char *p;
630	u_char *q;
631	MD5_CTX ctxt;
632	u_int8_t digest[16];
633	char l;
634	char n[64];	/* a single label must not exceed 63 chars */
635
636	/*
637	 * If no name is given and namelen is -1,
638	 * we try to do the hostname lookup ourselves.
639	 */
640	if (!name && namelen == -1) {
641		pr = curthread->td_ucred->cr_prison;
642		mtx_lock(&pr->pr_mtx);
643		name = pr->pr_hostname;
644		namelen = strlen(name);
645	} else
646		pr = NULL;
647	if (!name || !namelen) {
648		if (pr != NULL)
649			mtx_unlock(&pr->pr_mtx);
650		return -1;
651	}
652
653	p = name;
654	while (p && *p && *p != '.' && p - name < namelen)
655		p++;
656	if (p == name || p - name > sizeof(n) - 1) {
657		if (pr != NULL)
658			mtx_unlock(&pr->pr_mtx);
659		return -1;	/* label too long */
660	}
661	l = p - name;
662	strncpy(n, name, l);
663	if (pr != NULL)
664		mtx_unlock(&pr->pr_mtx);
665	n[(int)l] = '\0';
666	for (q = n; *q; q++) {
667		if ('A' <= *q && *q <= 'Z')
668			*q = *q - 'A' + 'a';
669	}
670
671	/* generate 16 bytes of pseudo-random value. */
672	bzero(&ctxt, sizeof(ctxt));
673	MD5Init(&ctxt);
674	MD5Update(&ctxt, &l, sizeof(l));
675	MD5Update(&ctxt, n, l);
676	MD5Final(digest, &ctxt);
677
678	bzero(in6, sizeof(*in6));
679	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
680	in6->s6_addr8[11] = 2;
681	if (oldmcprefix == 0) {
682		in6->s6_addr8[12] = 0xff;
683	 	/* Copy the first 24 bits of 128-bit hash into the address. */
684		bcopy(digest, &in6->s6_addr8[13], 3);
685	} else {
686	 	/* Copy the first 32 bits of 128-bit hash into the address. */
687		bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
688	}
689	if (in6_setscope(in6, ifp, NULL))
690		return (-1); /* XXX: should not fail */
691
692	return 0;
693}
694
695int
696in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
697    struct in6_addr *in6)
698{
699
700	return (in6_nigroup0(ifp, name, namelen, in6, 0));
701}
702
703int
704in6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
705    struct in6_addr *in6)
706{
707
708	return (in6_nigroup0(ifp, name, namelen, in6, 1));
709}
710
711/*
712 * XXX multiple loopback interface needs more care.  for instance,
713 * nodelocal address needs to be configured onto only one of them.
714 * XXX multiple link-local address case
715 *
716 * altifp - secondary EUI64 source
717 */
718void
719in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
720{
721	struct in6_ifaddr *ia;
722	struct in6_addr in6;
723
724	if (ifp->if_afdata[AF_INET6] == NULL)
725		return;
726	/*
727	 * quirks based on interface type
728	 */
729	switch (ifp->if_type) {
730	case IFT_STF:
731		/*
732		 * 6to4 interface is a very special kind of beast.
733		 * no multicast, no linklocal.  RFC2529 specifies how to make
734		 * linklocals for 6to4 interface, but there's no use and
735		 * it is rather harmful to have one.
736		 */
737		ND_IFINFO(ifp)->flags &= ~ND6_IFF_AUTO_LINKLOCAL;
738		break;
739	default:
740		break;
741	}
742
743	/*
744	 * usually, we require multicast capability to the interface
745	 */
746	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
747		nd6log((LOG_INFO, "in6_ifattach: "
748		    "%s is not multicast capable, IPv6 not enabled\n",
749		    if_name(ifp)));
750		return;
751	}
752
753	/*
754	 * assign loopback address for loopback interface.
755	 * XXX multiple loopback interface case.
756	 */
757	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
758		struct ifaddr *ifa;
759
760		in6 = in6addr_loopback;
761		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
762		if (ifa == NULL) {
763			if (in6_ifattach_loopback(ifp) != 0)
764				return;
765		} else
766			ifa_free(ifa);
767	}
768
769	/*
770	 * assign a link-local address, if there's none.
771	 */
772	if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
773	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
774		int error;
775
776		ia = in6ifa_ifpforlinklocal(ifp, 0);
777		if (ia == NULL) {
778			error = in6_ifattach_linklocal(ifp, altifp);
779#if 0
780			if (error)
781				log(LOG_NOTICE, "in6_ifattach_linklocal: "
782				    "failed to add a link-local addr to %s\n",
783				    if_name(ifp));
784#endif
785		} else
786			ifa_free(&ia->ia_ifa);
787	}
788
789	/* update dynamically. */
790	if (V_in6_maxmtu < ifp->if_mtu)
791		V_in6_maxmtu = ifp->if_mtu;
792}
793
794/*
795 * NOTE: in6_ifdetach() does not support loopback if at this moment.
796 * We don't need this function in bsdi, because interfaces are never removed
797 * from the ifnet list in bsdi.
798 */
799void
800in6_ifdetach(struct ifnet *ifp)
801{
802	struct in6_ifaddr *ia;
803	struct ifaddr *ifa, *next;
804	struct radix_node_head *rnh;
805	struct rtentry *rt;
806	struct sockaddr_in6 sin6;
807	struct in6_multi_mship *imm;
808
809	if (ifp->if_afdata[AF_INET6] == NULL)
810		return;
811
812	/* remove neighbor management table */
813	nd6_purge(ifp);
814
815	/* nuke any of IPv6 addresses we have */
816	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
817		if (ifa->ifa_addr->sa_family != AF_INET6)
818			continue;
819		in6_purgeaddr(ifa);
820	}
821
822	/* undo everything done by in6_ifattach(), just in case */
823	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
824		if (ifa->ifa_addr->sa_family != AF_INET6
825		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
826			continue;
827		}
828
829		ia = (struct in6_ifaddr *)ifa;
830
831		/*
832		 * leave from multicast groups we have joined for the interface
833		 */
834		while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
835			LIST_REMOVE(imm, i6mm_chain);
836			in6_leavegroup(imm);
837		}
838
839		/* Remove link-local from the routing table. */
840		if (ia->ia_flags & IFA_ROUTE)
841			(void)rtinit(&ia->ia_ifa, RTM_DELETE, ia->ia_flags);
842
843		/* remove from the linked list */
844		IF_ADDR_WLOCK(ifp);
845		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
846		IF_ADDR_WUNLOCK(ifp);
847		ifa_free(ifa);				/* if_addrhead */
848
849		IN6_IFADDR_WLOCK();
850		TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
851		IN6_IFADDR_WUNLOCK();
852		ifa_free(ifa);
853	}
854
855	in6_pcbpurgeif0(&V_udbinfo, ifp);
856	in6_pcbpurgeif0(&V_ulitecbinfo, ifp);
857	in6_pcbpurgeif0(&V_ripcbinfo, ifp);
858	/* leave from all multicast groups joined */
859	in6_purgemaddrs(ifp);
860
861	/*
862	 * remove neighbor management table.  we call it twice just to make
863	 * sure we nuke everything.  maybe we need just one call.
864	 * XXX: since the first call did not release addresses, some prefixes
865	 * might remain.  We should call nd6_purge() again to release the
866	 * prefixes after removing all addresses above.
867	 * (Or can we just delay calling nd6_purge until at this point?)
868	 */
869	nd6_purge(ifp);
870
871	/*
872	 * Remove route to link-local allnodes multicast (ff02::1).
873	 * These only get automatically installed for the default FIB.
874	 */
875	bzero(&sin6, sizeof(sin6));
876	sin6.sin6_len = sizeof(struct sockaddr_in6);
877	sin6.sin6_family = AF_INET6;
878	sin6.sin6_addr = in6addr_linklocal_allnodes;
879	if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
880		/* XXX: should not fail */
881		return;
882	/* XXX grab lock first to avoid LOR */
883	rnh = rt_tables_get_rnh(RT_DEFAULT_FIB, AF_INET6);
884	if (rnh != NULL) {
885		RADIX_NODE_HEAD_LOCK(rnh);
886		rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED,
887		    RT_DEFAULT_FIB);
888		if (rt) {
889			if (rt->rt_ifp == ifp)
890				rtexpunge(rt);
891			RTFREE_LOCKED(rt);
892		}
893		RADIX_NODE_HEAD_UNLOCK(rnh);
894	}
895}
896
897int
898in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
899    const u_int8_t *baseid, int generate)
900{
901	u_int8_t nullbuf[8];
902	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
903
904	bzero(nullbuf, sizeof(nullbuf));
905	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
906		/* we've never created a random ID.  Create a new one. */
907		generate = 1;
908	}
909
910	if (generate) {
911		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
912
913		/* generate_tmp_ifid will update seedn and buf */
914		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
915		    ndi->randomid);
916	}
917	bcopy(ndi->randomid, retbuf, 8);
918
919	return (0);
920}
921
922void
923in6_tmpaddrtimer(void *arg)
924{
925	CURVNET_SET((struct vnet *) arg);
926	struct nd_ifinfo *ndi;
927	u_int8_t nullbuf[8];
928	struct ifnet *ifp;
929
930	callout_reset(&V_in6_tmpaddrtimer_ch,
931	    (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
932	    V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
933
934	bzero(nullbuf, sizeof(nullbuf));
935	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
936		if (ifp->if_afdata[AF_INET6] == NULL)
937			continue;
938		ndi = ND_IFINFO(ifp);
939		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
940			/*
941			 * We've been generating a random ID on this interface.
942			 * Create a new one.
943			 */
944			(void)generate_tmp_ifid(ndi->randomseed0,
945			    ndi->randomseed1, ndi->randomid);
946		}
947	}
948
949	CURVNET_RESTORE();
950}
951
952static void
953in6_purgemaddrs(struct ifnet *ifp)
954{
955	LIST_HEAD(,in6_multi)	 purgeinms;
956	struct in6_multi	*inm, *tinm;
957	struct ifmultiaddr	*ifma;
958
959	LIST_INIT(&purgeinms);
960	IN6_MULTI_LOCK();
961
962	/*
963	 * Extract list of in6_multi associated with the detaching ifp
964	 * which the PF_INET6 layer is about to release.
965	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
966	 * by code further down.
967	 */
968	IF_ADDR_RLOCK(ifp);
969	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
970		if (ifma->ifma_addr->sa_family != AF_INET6 ||
971		    ifma->ifma_protospec == NULL)
972			continue;
973		inm = (struct in6_multi *)ifma->ifma_protospec;
974		LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
975	}
976	IF_ADDR_RUNLOCK(ifp);
977
978	LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
979		LIST_REMOVE(inm, in6m_entry);
980		in6m_release_locked(inm);
981	}
982	mld_ifdetach(ifp);
983
984	IN6_MULTI_UNLOCK();
985}
986