in6_ifattach.c revision 207369
189837Skris/*-
289837Skris * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3280304Sjkim * All rights reserved.
4280304Sjkim *
5280304Sjkim * Redistribution and use in source and binary forms, with or without
689837Skris * modification, are permitted provided that the following conditions
789837Skris * are met:
868651Skris * 1. Redistributions of source code must retain the above copyright
968651Skris *    notice, this list of conditions and the following disclaimer.
1068651Skris * 2. Redistributions in binary form must reproduce the above copyright
1168651Skris *    notice, this list of conditions and the following disclaimer in the
1268651Skris *    documentation and/or other materials provided with the distribution.
1368651Skris * 3. Neither the name of the project nor the names of its contributors
14280304Sjkim *    may be used to endorse or promote products derived from this software
1568651Skris *    without specific prior written permission.
1668651Skris *
1768651Skris * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1868651Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1968651Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2068651Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21280304Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2268651Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2368651Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2468651Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2568651Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2668651Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2768651Skris * SUCH DAMAGE.
28280304Sjkim *
2968651Skris *	$KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
3068651Skris */
3168651Skris
3268651Skris#include <sys/cdefs.h>
3368651Skris__FBSDID("$FreeBSD: head/sys/netinet6/in6_ifattach.c 207369 2010-04-29 11:52:42Z bz $");
3468651Skris
3568651Skris#include <sys/param.h>
3668651Skris#include <sys/systm.h>
3768651Skris#include <sys/malloc.h>
3868651Skris#include <sys/socket.h>
3968651Skris#include <sys/sockio.h>
4068651Skris#include <sys/jail.h>
4168651Skris#include <sys/kernel.h>
4268651Skris#include <sys/proc.h>
43280304Sjkim#include <sys/syslog.h>
4468651Skris#include <sys/md5.h>
4568651Skris
46280304Sjkim#include <net/if.h>
4768651Skris#include <net/if_dl.h>
4868651Skris#include <net/if_types.h>
4968651Skris#include <net/route.h>
5068651Skris#include <net/vnet.h>
5168651Skris
5268651Skris#include <netinet/in.h>
5368651Skris#include <netinet/in_var.h>
5468651Skris#include <netinet/if_ether.h>
5568651Skris#include <netinet/in_pcb.h>
5668651Skris#include <netinet/ip_var.h>
5768651Skris#include <netinet/udp.h>
58280304Sjkim#include <netinet/udp_var.h>
5968651Skris
6068651Skris#include <netinet/ip6.h>
6168651Skris#include <netinet6/ip6_var.h>
6268651Skris#include <netinet6/in6_var.h>
6368651Skris#include <netinet6/in6_pcb.h>
6468651Skris#include <netinet6/in6_ifattach.h>
65280304Sjkim#include <netinet6/ip6_var.h>
66280304Sjkim#include <netinet6/nd6.h>
67280304Sjkim#include <netinet6/mld6_var.h>
68280304Sjkim#include <netinet6/scope6_var.h>
6968651Skris
70280304SjkimVNET_DEFINE(unsigned long, in6_maxmtu) = 0;
71280304Sjkim
72280304Sjkim#ifdef IP6_AUTO_LINKLOCAL
73280304SjkimVNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
74160814Ssimon#else
75280304SjkimVNET_DEFINE(int, ip6_auto_linklocal) = 1;	/* enabled by default */
76280304Sjkim#endif
77109998Smarkm
78280304SjkimVNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
79280304Sjkim#define	V_in6_tmpaddrtimer_ch		VNET(in6_tmpaddrtimer_ch)
80280304Sjkim
81280304SjkimVNET_DECLARE(struct inpcbinfo, ripcbinfo);
8268651Skris#define	V_ripcbinfo			VNET(ripcbinfo)
83280304Sjkim
84280304Sjkimstatic int get_rand_ifid(struct ifnet *, struct in6_addr *);
85280304Sjkimstatic int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
86280304Sjkimstatic int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
87160814Ssimonstatic int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
88280304Sjkimstatic int in6_ifattach_loopback(struct ifnet *);
89280304Sjkimstatic void in6_purgemaddrs(struct ifnet *);
9089837Skris
91280304Sjkim#define EUI64_GBIT	0x01
92280304Sjkim#define EUI64_UBIT	0x02
93280304Sjkim#define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
94280304Sjkim#define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
9568651Skris#define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
96280304Sjkim#define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
97280304Sjkim#define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
98280304Sjkim
99160814Ssimon#define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
100280304Sjkim#define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
101280304Sjkim
102280304Sjkim/*
103280304Sjkim * Generate a last-resort interface identifier, when the machine has no
104194206Ssimon * IEEE802/EUI64 address sources.
105280304Sjkim * The goal here is to get an interface identifier that is
106280304Sjkim * (1) random enough and (2) does not change across reboot.
107280304Sjkim * We currently use MD5(hostname) for it.
108280304Sjkim *
109194206Ssimon * in6 - upper 64bits are preserved
110280304Sjkim */
111280304Sjkimstatic int
112280304Sjkimget_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
113160814Ssimon{
114280304Sjkim	MD5_CTX ctxt;
115280304Sjkim	struct prison *pr;
116280304Sjkim	u_int8_t digest[16];
117280304Sjkim	int hostnamelen;
118160814Ssimon
119280304Sjkim	pr = curthread->td_ucred->cr_prison;
120280304Sjkim	mtx_lock(&pr->pr_mtx);
121280304Sjkim	hostnamelen = strlen(pr->pr_hostname);
122160814Ssimon#if 0
123280304Sjkim	/* we need at least several letters as seed for ifid */
124280304Sjkim	if (hostnamelen < 3) {
125280304Sjkim		mtx_unlock(&pr->pr_mtx);
126160814Ssimon		return -1;
127280304Sjkim	}
128280304Sjkim#endif
129280304Sjkim
130280304Sjkim	/* generate 8 bytes of pseudo-random value. */
13189837Skris	bzero(&ctxt, sizeof(ctxt));
132280304Sjkim	MD5Init(&ctxt);
133280304Sjkim	MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
134280304Sjkim	mtx_unlock(&pr->pr_mtx);
13589837Skris	MD5Final(digest, &ctxt);
136280304Sjkim
137280304Sjkim	/* assumes sizeof(digest) > sizeof(ifid) */
138280304Sjkim	bcopy(digest, &in6->s6_addr[8], 8);
139280304Sjkim
14068651Skris	/* make sure to set "u" bit to local, and "g" bit to individual. */
141280304Sjkim	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
142280304Sjkim	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
143280304Sjkim
144280304Sjkim	/* convert EUI64 into IPv6 interface identifier */
14568651Skris	EUI64_TO_IFID(in6);
146280304Sjkim
147280304Sjkim	return 0;
148280304Sjkim}
149280304Sjkim
15068651Skrisstatic int
151280304Sjkimgenerate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
152280304Sjkim{
153280304Sjkim	MD5_CTX ctxt;
154280304Sjkim	u_int8_t seed[16], digest[16], nullbuf[8];
15568651Skris	u_int32_t val32;
156280304Sjkim
157280304Sjkim	/* If there's no history, start with a random seed. */
158280304Sjkim	bzero(nullbuf, sizeof(nullbuf));
159280304Sjkim	if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
16068651Skris		int i;
161280304Sjkim
162280304Sjkim		for (i = 0; i < 2; i++) {
163280304Sjkim			val32 = arc4random();
164280304Sjkim			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
165109998Smarkm		}
166280304Sjkim	} else
167109998Smarkm		bcopy(seed0, seed, 8);
168280304Sjkim
169280304Sjkim	/* copy the right-most 64-bits of the given address */
170280304Sjkim	/* XXX assumption on the size of IFID */
171109998Smarkm	bcopy(seed1, &seed[8], 8);
172280304Sjkim
173280304Sjkim	if (0) {		/* for debugging purposes only */
174280304Sjkim		int i;
175109998Smarkm
176280304Sjkim		printf("generate_tmp_ifid: new randomized ID from: ");
177280304Sjkim		for (i = 0; i < 16; i++)
178280304Sjkim			printf("%02x", seed[i]);
179160814Ssimon		printf(" ");
180280304Sjkim	}
181280304Sjkim
182280304Sjkim	/* generate 16 bytes of pseudo-random value. */
183160814Ssimon	bzero(&ctxt, sizeof(ctxt));
184280304Sjkim	MD5Init(&ctxt);
185280304Sjkim	MD5Update(&ctxt, seed, sizeof(seed));
186280304Sjkim	MD5Final(digest, &ctxt);
187160814Ssimon
188280304Sjkim	/*
189280304Sjkim	 * RFC 3041 3.2.1. (3)
190280304Sjkim	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
191160814Ssimon	 * left-most bit is numbered 0) to zero.
192280304Sjkim	 */
193109998Smarkm	bcopy(digest, ret, 8);
194280304Sjkim	ret[0] &= ~EUI64_UBIT;
195280304Sjkim
196280304Sjkim	/*
197109998Smarkm	 * XXX: we'd like to ensure that the generated value is not zero
198280304Sjkim	 * for simplicity.  If the caclculated digest happens to be zero,
199109998Smarkm	 * use a random non-zero value as the last resort.
200280304Sjkim	 */
201109998Smarkm	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
202280304Sjkim		nd6log((LOG_INFO,
203280304Sjkim		    "generate_tmp_ifid: computed MD5 value is zero.\n"));
204280304Sjkim
205160814Ssimon		val32 = arc4random();
206280304Sjkim		val32 = 1 + (val32 % (0xffffffff - 1));
207280304Sjkim	}
208280304Sjkim
209160814Ssimon	/*
210280304Sjkim	 * RFC 3041 3.2.1. (4)
211280304Sjkim	 * Take the rightmost 64-bits of the MD5 digest and save them in
212280304Sjkim	 * stable storage as the history value to be used in the next
213160814Ssimon	 * iteration of the algorithm.
214280304Sjkim	 */
215280304Sjkim	bcopy(&digest[8], seed0, 8);
216280304Sjkim
217160814Ssimon	if (0) {		/* for debugging purposes only */
218280304Sjkim		int i;
219280304Sjkim
220280304Sjkim		printf("to: ");
221160814Ssimon		for (i = 0; i < 16; i++)
222280304Sjkim			printf("%02x", digest[i]);
223280304Sjkim		printf("\n");
224280304Sjkim	}
225160814Ssimon
226280304Sjkim	return 0;
227280304Sjkim}
228280304Sjkim
229160814Ssimon/*
230280304Sjkim * Get interface identifier for the specified interface.
231280304Sjkim * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
232280304Sjkim *
233160814Ssimon * in6 - upper 64bits are preserved
234280304Sjkim */
235280304Sjkimint
236280304Sjkimin6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
237160814Ssimon{
238280304Sjkim	struct ifaddr *ifa;
239280304Sjkim	struct sockaddr_dl *sdl;
240280304Sjkim	u_int8_t *addr;
241160814Ssimon	size_t addrlen;
242280304Sjkim	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
243280304Sjkim	static u_int8_t allone[8] =
244280304Sjkim		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
245160814Ssimon
246280304Sjkim	IF_ADDR_LOCK(ifp);
247280304Sjkim	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
248280304Sjkim		if (ifa->ifa_addr->sa_family != AF_LINK)
249160814Ssimon			continue;
250280304Sjkim		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
251280304Sjkim		if (sdl == NULL)
252280304Sjkim			continue;
253160814Ssimon		if (sdl->sdl_alen == 0)
254280304Sjkim			continue;
255280304Sjkim
256280304Sjkim		goto found;
257160814Ssimon	}
258280304Sjkim	IF_ADDR_UNLOCK(ifp);
259280304Sjkim
260280304Sjkim	return -1;
261160814Ssimon
262280304Sjkimfound:
263280304Sjkim	IF_ADDR_LOCK_ASSERT(ifp);
264280304Sjkim	addr = LLADDR(sdl);
265160814Ssimon	addrlen = sdl->sdl_alen;
266280304Sjkim
267280304Sjkim	/* get EUI64 */
268280304Sjkim	switch (ifp->if_type) {
269160814Ssimon	case IFT_ETHER:
270280304Sjkim	case IFT_FDDI:
271280304Sjkim	case IFT_ISO88025:
272280304Sjkim	case IFT_ATM:
273160814Ssimon	case IFT_IEEE1394:
274280304Sjkim#ifdef IFT_IEEE80211
275280304Sjkim	case IFT_IEEE80211:
276280304Sjkim#endif
277160814Ssimon		/* IEEE802/EUI64 cases - what others? */
278280304Sjkim		/* IEEE1394 uses 16byte length address starting with EUI64 */
279280304Sjkim		if (addrlen > 8)
280280304Sjkim			addrlen = 8;
281160814Ssimon
282280304Sjkim		/* look at IEEE802/EUI64 only */
283109998Smarkm		if (addrlen != 8 && addrlen != 6) {
284280304Sjkim			IF_ADDR_UNLOCK(ifp);
285280304Sjkim			return -1;
286280304Sjkim		}
287109998Smarkm
288280304Sjkim		/*
289280304Sjkim		 * check for invalid MAC address - on bsdi, we see it a lot
290280304Sjkim		 * since wildboar configures all-zero MAC on pccard before
291109998Smarkm		 * card insertion.
292280304Sjkim		 */
293280304Sjkim		if (bcmp(addr, allzero, addrlen) == 0) {
294280304Sjkim			IF_ADDR_UNLOCK(ifp);
295109998Smarkm			return -1;
296280304Sjkim		}
297280304Sjkim		if (bcmp(addr, allone, addrlen) == 0) {
298280304Sjkim			IF_ADDR_UNLOCK(ifp);
299109998Smarkm			return -1;
300280304Sjkim		}
301280304Sjkim
302280304Sjkim		/* make EUI64 address */
303109998Smarkm		if (addrlen == 8)
304280304Sjkim			bcopy(addr, &in6->s6_addr[8], 8);
305280304Sjkim		else if (addrlen == 6) {
306280304Sjkim			in6->s6_addr[8] = addr[0];
307109998Smarkm			in6->s6_addr[9] = addr[1];
308280304Sjkim			in6->s6_addr[10] = addr[2];
309280304Sjkim			in6->s6_addr[11] = 0xff;
310280304Sjkim			in6->s6_addr[12] = 0xfe;
311109998Smarkm			in6->s6_addr[13] = addr[3];
312280304Sjkim			in6->s6_addr[14] = addr[4];
313109998Smarkm			in6->s6_addr[15] = addr[5];
314280304Sjkim		}
315280304Sjkim		break;
316280304Sjkim
317109998Smarkm	case IFT_ARCNET:
318280304Sjkim		if (addrlen != 1) {
319280304Sjkim			IF_ADDR_UNLOCK(ifp);
320280304Sjkim			return -1;
321194206Ssimon		}
322280304Sjkim		if (!addr[0]) {
323280304Sjkim			IF_ADDR_UNLOCK(ifp);
324280304Sjkim			return -1;
325194206Ssimon		}
326280304Sjkim
327280304Sjkim		bzero(&in6->s6_addr[8], 8);
328280304Sjkim		in6->s6_addr[15] = addr[0];
329194206Ssimon
330280304Sjkim		/*
331280304Sjkim		 * due to insufficient bitwidth, we mark it local.
332280304Sjkim		 */
333194206Ssimon		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
334280304Sjkim		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
335280304Sjkim		break;
336280304Sjkim
337194206Ssimon	case IFT_GIF:
338280304Sjkim#ifdef IFT_STF
339280304Sjkim	case IFT_STF:
340280304Sjkim#endif
341194206Ssimon		/*
342280304Sjkim		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
343160814Ssimon		 * however, IPv4 address is not very suitable as unique
344280304Sjkim		 * identifier source (can be renumbered).
345280304Sjkim		 * we don't do this.
346280304Sjkim		 */
347160814Ssimon		IF_ADDR_UNLOCK(ifp);
348280304Sjkim		return -1;
349280304Sjkim
350280304Sjkim	default:
351160814Ssimon		IF_ADDR_UNLOCK(ifp);
352280304Sjkim		return -1;
353280304Sjkim	}
354280304Sjkim
355160814Ssimon	/* sanity check: g bit must not indicate "group" */
356280304Sjkim	if (EUI64_GROUP(in6)) {
357280304Sjkim		IF_ADDR_UNLOCK(ifp);
358280304Sjkim		return -1;
359160814Ssimon	}
360280304Sjkim
361280304Sjkim	/* convert EUI64 into IPv6 interface identifier */
362280304Sjkim	EUI64_TO_IFID(in6);
363160814Ssimon
364280304Sjkim	/*
365280304Sjkim	 * sanity check: ifid must not be all zero, avoid conflict with
366280304Sjkim	 * subnet router anycast
367160814Ssimon	 */
368280304Sjkim	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
369280304Sjkim	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
370280304Sjkim		IF_ADDR_UNLOCK(ifp);
371160814Ssimon		return -1;
372280304Sjkim	}
373280304Sjkim
374280304Sjkim	IF_ADDR_UNLOCK(ifp);
375160814Ssimon	return 0;
376280304Sjkim}
377280304Sjkim
378280304Sjkim/*
379160814Ssimon * Get interface identifier for the specified interface.  If it is not
380280304Sjkim * available on ifp0, borrow interface identifier from other information
381280304Sjkim * sources.
382280304Sjkim *
383160814Ssimon * altifp - secondary EUI64 source
384280304Sjkim */
385280304Sjkimstatic int
386280304Sjkimget_ifid(struct ifnet *ifp0, struct ifnet *altifp,
387160814Ssimon    struct in6_addr *in6)
388280304Sjkim{
389280304Sjkim	struct ifnet *ifp;
390280304Sjkim
391160814Ssimon	/* first, try to get it from the interface itself */
392280304Sjkim	if (in6_get_hw_ifid(ifp0, in6) == 0) {
393280304Sjkim		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
394280304Sjkim		    if_name(ifp0)));
395160814Ssimon		goto success;
396280304Sjkim	}
397280304Sjkim
398280304Sjkim	/* try secondary EUI64 source. this basically is for ATM PVC */
399160814Ssimon	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
400280304Sjkim		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
401280304Sjkim		    if_name(ifp0), if_name(altifp)));
402280304Sjkim		goto success;
403160814Ssimon	}
404280304Sjkim
405280304Sjkim	/* next, try to get it from some other hardware interface */
406280304Sjkim	IFNET_RLOCK_NOSLEEP();
407160814Ssimon	for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
408280304Sjkim		if (ifp == ifp0)
409280304Sjkim			continue;
410280304Sjkim		if (in6_get_hw_ifid(ifp, in6) != 0)
411160814Ssimon			continue;
412280304Sjkim
413280304Sjkim		/*
414280304Sjkim		 * to borrow ifid from other interface, ifid needs to be
415160814Ssimon		 * globally unique
416280304Sjkim		 */
417280304Sjkim		if (IFID_UNIVERSAL(in6)) {
418280304Sjkim			nd6log((LOG_DEBUG,
419160814Ssimon			    "%s: borrow interface identifier from %s\n",
420280304Sjkim			    if_name(ifp0), if_name(ifp)));
421280304Sjkim			IFNET_RUNLOCK_NOSLEEP();
422280304Sjkim			goto success;
423160814Ssimon		}
424280304Sjkim	}
425280304Sjkim	IFNET_RUNLOCK_NOSLEEP();
426280304Sjkim
427160814Ssimon	/* last resort: get from random number source */
428280304Sjkim	if (get_rand_ifid(ifp, in6) == 0) {
429280304Sjkim		nd6log((LOG_DEBUG,
430280304Sjkim		    "%s: interface identifier generated by random number\n",
431160814Ssimon		    if_name(ifp0)));
432280304Sjkim		goto success;
433280304Sjkim	}
434280304Sjkim
435160814Ssimon	printf("%s: failed to get interface identifier\n", if_name(ifp0));
436280304Sjkim	return -1;
437280304Sjkim
438280304Sjkimsuccess:
439160814Ssimon	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
440280304Sjkim	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
441280304Sjkim	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
442280304Sjkim	    in6->s6_addr[14], in6->s6_addr[15]));
443160814Ssimon	return 0;
444280304Sjkim}
445280304Sjkim
446280304Sjkim/*
447160814Ssimon * altifp - secondary EUI64 source
448280304Sjkim */
449280304Sjkimstatic int
450280304Sjkimin6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
451160814Ssimon{
452280304Sjkim	struct in6_ifaddr *ia;
453280304Sjkim	struct in6_aliasreq ifra;
454280304Sjkim	struct nd_prefixctl pr0;
455160814Ssimon	int i, error;
456280304Sjkim
457280304Sjkim	/*
458280304Sjkim	 * configure link-local address.
459160814Ssimon	 */
460280304Sjkim	bzero(&ifra, sizeof(ifra));
461280304Sjkim
462280304Sjkim	/*
463160814Ssimon	 * in6_update_ifa() does not use ifra_name, but we accurately set it
464280304Sjkim	 * for safety.
465280304Sjkim	 */
466280304Sjkim	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
467160814Ssimon
468280304Sjkim	ifra.ifra_addr.sin6_family = AF_INET6;
469160814Ssimon	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
470280304Sjkim	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
471280304Sjkim	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
472280304Sjkim	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
473160814Ssimon		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
474280304Sjkim		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
475280304Sjkim	} else {
476280304Sjkim		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
477160814Ssimon			nd6log((LOG_ERR,
478280304Sjkim			    "%s: no ifid available\n", if_name(ifp)));
479280304Sjkim			return (-1);
480280304Sjkim		}
481160814Ssimon	}
482280304Sjkim	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
483280304Sjkim		return (-1);
484280304Sjkim
485160814Ssimon	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
486280304Sjkim	ifra.ifra_prefixmask.sin6_family = AF_INET6;
487280304Sjkim	ifra.ifra_prefixmask.sin6_addr = in6mask64;
488280304Sjkim	/* link-local addresses should NEVER expire. */
489160814Ssimon	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
490280304Sjkim	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
491280304Sjkim
492280304Sjkim	/*
493160814Ssimon	 * Now call in6_update_ifa() to do a bunch of procedures to configure
494280304Sjkim	 * a link-local address. We can set the 3rd argument to NULL, because
495280304Sjkim	 * we know there's no other link-local address on the interface
496280304Sjkim	 * and therefore we are adding one (instead of updating one).
497160814Ssimon	 */
498280304Sjkim	if ((error = in6_update_ifa(ifp, &ifra, NULL,
499280304Sjkim				    IN6_IFAUPDATE_DADDELAY)) != 0) {
500280304Sjkim		/*
501160814Ssimon		 * XXX: When the interface does not support IPv6, this call
502280304Sjkim		 * would fail in the SIOCSIFADDR ioctl.  I believe the
503280304Sjkim		 * notification is rather confusing in this case, so just
504280304Sjkim		 * suppress it.  (jinmei@kame.net 20010130)
505160814Ssimon		 */
506280304Sjkim		if (error != EAFNOSUPPORT)
507280304Sjkim			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
508280304Sjkim			    "configure a link-local address on %s "
509160814Ssimon			    "(errno=%d)\n",
510280304Sjkim			    if_name(ifp), error));
511280304Sjkim		return (-1);
512280304Sjkim	}
513160814Ssimon
514280304Sjkim	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
515280304Sjkim#ifdef DIAGNOSTIC
516280304Sjkim	if (!ia) {
517280304Sjkim		panic("ia == NULL in in6_ifattach_linklocal");
51868651Skris		/* NOTREACHED */
519280304Sjkim	}
520280304Sjkim#endif
521280304Sjkim	ifa_free(&ia->ia_ifa);
52268651Skris
523280304Sjkim	/*
524280304Sjkim	 * Make the link-local prefix (fe80::%link/64) as on-link.
525280304Sjkim	 * Since we'd like to manage prefixes separately from addresses,
52668651Skris	 * we make an ND6 prefix structure for the link-local prefix,
527280304Sjkim	 * and add it to the prefix list as a never-expire prefix.
528280304Sjkim	 * XXX: this change might affect some existing code base...
529280304Sjkim	 */
53068651Skris	bzero(&pr0, sizeof(pr0));
531280304Sjkim	pr0.ndpr_ifp = ifp;
532280304Sjkim	/* this should be 64 at this moment. */
533280304Sjkim	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
53468651Skris	pr0.ndpr_prefix = ifra.ifra_addr;
535280304Sjkim	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
536280304Sjkim	for (i = 0; i < 4; i++) {
537280304Sjkim		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
538280304Sjkim		    in6mask64.s6_addr32[i];
539194206Ssimon	}
540280304Sjkim	/*
541280304Sjkim	 * Initialize parameters.  The link-local prefix must always be
542280304Sjkim	 * on-link, and its lifetimes never expire.
543280304Sjkim	 */
544194206Ssimon	pr0.ndpr_raf_onlink = 1;
545280304Sjkim	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
546280304Sjkim	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
547280304Sjkim	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
548280304Sjkim	/*
54968651Skris	 * Since there is no other link-local addresses, nd6_prefix_lookup()
550280304Sjkim	 * probably returns NULL.  However, we cannot always expect the result.
551280304Sjkim	 * For example, if we first remove the (only) existing link-local
552280304Sjkim	 * address, and then reconfigure another one, the prefix is still
553280304Sjkim	 * valid with referring to the old link-local address.
55468651Skris	 */
555280304Sjkim	if (nd6_prefix_lookup(&pr0) == NULL) {
556280304Sjkim		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
557280304Sjkim			return (error);
55868651Skris	}
559280304Sjkim
560280304Sjkim	return 0;
561280304Sjkim}
56268651Skris
563280304Sjkim/*
564280304Sjkim * ifp - must be IFT_LOOP
565280304Sjkim */
566280304Sjkimstatic int
56768651Skrisin6_ifattach_loopback(struct ifnet *ifp)
568280304Sjkim{
569280304Sjkim	struct in6_aliasreq ifra;
570280304Sjkim	int error;
571280304Sjkim
57289837Skris	bzero(&ifra, sizeof(ifra));
573280304Sjkim
574280304Sjkim	/*
575280304Sjkim	 * in6_update_ifa() does not use ifra_name, but we accurately set it
576280304Sjkim	 * for safety.
57768651Skris	 */
578280304Sjkim	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
579280304Sjkim
580280304Sjkim	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
581280304Sjkim	ifra.ifra_prefixmask.sin6_family = AF_INET6;
58268651Skris	ifra.ifra_prefixmask.sin6_addr = in6mask128;
583280304Sjkim
584280304Sjkim	/*
585280304Sjkim	 * Always initialize ia_dstaddr (= broadcast address) to loopback
586280304Sjkim	 * address.  Follows IPv4 practice - see in_ifinit().
587238405Sjkim	 */
588280304Sjkim	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
589280304Sjkim	ifra.ifra_dstaddr.sin6_family = AF_INET6;
590280304Sjkim	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
591280304Sjkim
592238405Sjkim	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
593280304Sjkim	ifra.ifra_addr.sin6_family = AF_INET6;
594280304Sjkim	ifra.ifra_addr.sin6_addr = in6addr_loopback;
595280304Sjkim
596280304Sjkim	/* the loopback  address should NEVER expire. */
597238405Sjkim	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
598280304Sjkim	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
599280304Sjkim
600280304Sjkim	/* we don't need to perform DAD on loopback interfaces. */
601280304Sjkim	ifra.ifra_flags |= IN6_IFF_NODAD;
602160814Ssimon
603280304Sjkim	/* skip registration to the prefix list. XXX should be temporary. */
604280304Sjkim	ifra.ifra_flags |= IN6_IFF_NOPFX;
605280304Sjkim
606280304Sjkim	/*
607160814Ssimon	 * We are sure that this is a newly assigned address, so we can set
608280304Sjkim	 * NULL to the 3rd arg.
609280304Sjkim	 */
610280304Sjkim	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
611280304Sjkim		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
612160814Ssimon		    "the loopback address on %s (errno=%d)\n",
613280304Sjkim		    if_name(ifp), error));
614280304Sjkim		return (-1);
615280304Sjkim	}
616280304Sjkim
617160814Ssimon	return 0;
618280304Sjkim}
619280304Sjkim
620280304Sjkim/*
62168651Skris * compute NI group address, based on the current hostname setting.
622280304Sjkim * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
623280304Sjkim *
624280304Sjkim * when ifp == NULL, the caller is responsible for filling scopeid.
62568651Skris */
626280304Sjkimint
627280304Sjkimin6_nigroup(struct ifnet *ifp, const char *name, int namelen,
628280304Sjkim    struct in6_addr *in6)
62968651Skris{
630280304Sjkim	struct prison *pr;
631280304Sjkim	const char *p;
632280304Sjkim	u_char *q;
633280304Sjkim	MD5_CTX ctxt;
63468651Skris	u_int8_t digest[16];
635280304Sjkim	char l;
636280304Sjkim	char n[64];	/* a single label must not exceed 63 chars */
637280304Sjkim
638280304Sjkim	/*
63968651Skris	 * If no name is given and namelen is -1,
640280304Sjkim	 * we try to do the hostname lookup ourselves.
641280304Sjkim	 */
642280304Sjkim	if (!name && namelen == -1) {
643280304Sjkim		pr = curthread->td_ucred->cr_prison;
64468651Skris		mtx_lock(&pr->pr_mtx);
645280304Sjkim		name = pr->pr_hostname;
646280304Sjkim		namelen = strlen(name);
647280304Sjkim	} else
648280304Sjkim		pr = NULL;
64968651Skris	if (!name || !namelen) {
650280304Sjkim		if (pr != NULL)
651280304Sjkim			mtx_unlock(&pr->pr_mtx);
652280304Sjkim		return -1;
653280304Sjkim	}
65468651Skris
655280304Sjkim	p = name;
656280304Sjkim	while (p && *p && *p != '.' && p - name < namelen)
657280304Sjkim		p++;
658280304Sjkim	if (p == name || p - name > sizeof(n) - 1) {
65968651Skris		if (pr != NULL)
660280304Sjkim			mtx_unlock(&pr->pr_mtx);
661280304Sjkim		return -1;	/* label too long */
662280304Sjkim	}
66368651Skris	l = p - name;
664280304Sjkim	strncpy(n, name, l);
665280304Sjkim	if (pr != NULL)
666280304Sjkim		mtx_unlock(&pr->pr_mtx);
66768651Skris	n[(int)l] = '\0';
668280304Sjkim	for (q = n; *q; q++) {
669280304Sjkim		if ('A' <= *q && *q <= 'Z')
670280304Sjkim			*q = *q - 'A' + 'a';
67168651Skris	}
672280304Sjkim
673280304Sjkim	/* generate 8 bytes of pseudo-random value. */
674280304Sjkim	bzero(&ctxt, sizeof(ctxt));
67568651Skris	MD5Init(&ctxt);
676280304Sjkim	MD5Update(&ctxt, &l, sizeof(l));
677280304Sjkim	MD5Update(&ctxt, n, l);
678280304Sjkim	MD5Final(digest, &ctxt);
67968651Skris
680280304Sjkim	bzero(in6, sizeof(*in6));
681280304Sjkim	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
682280304Sjkim	in6->s6_addr8[11] = 2;
68368651Skris	bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
684280304Sjkim	if (in6_setscope(in6, ifp, NULL))
685280304Sjkim		return (-1); /* XXX: should not fail */
686280304Sjkim
68768651Skris	return 0;
688280304Sjkim}
689280304Sjkim
690280304Sjkim/*
69168651Skris * XXX multiple loopback interface needs more care.  for instance,
692280304Sjkim * nodelocal address needs to be configured onto only one of them.
693280304Sjkim * XXX multiple link-local address case
694280304Sjkim *
69568651Skris * altifp - secondary EUI64 source
696280304Sjkim */
697280304Sjkimvoid
698280304Sjkimin6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
69968651Skris{
700280304Sjkim	struct in6_ifaddr *ia;
701280304Sjkim	struct in6_addr in6;
702280304Sjkim
70368651Skris	/* some of the interfaces are inherently not IPv6 capable */
704280304Sjkim	switch (ifp->if_type) {
705280304Sjkim	case IFT_PFLOG:
706280304Sjkim	case IFT_PFSYNC:
70768651Skris	case IFT_CARP:
708280304Sjkim		return;
709280304Sjkim	}
710280304Sjkim
71168651Skris	/*
712280304Sjkim	 * quirks based on interface type
713280304Sjkim	 */
714280304Sjkim	switch (ifp->if_type) {
71568651Skris#ifdef IFT_STF
716280304Sjkim	case IFT_STF:
717280304Sjkim		/*
718280304Sjkim		 * 6to4 interface is a very special kind of beast.
71968651Skris		 * no multicast, no linklocal.  RFC2529 specifies how to make
720280304Sjkim		 * linklocals for 6to4 interface, but there's no use and
721280304Sjkim		 * it is rather harmful to have one.
722280304Sjkim		 */
72368651Skris		goto statinit;
724280304Sjkim#endif
725280304Sjkim	default:
726280304Sjkim		break;
72768651Skris	}
728280304Sjkim
729280304Sjkim	/*
730280304Sjkim	 * usually, we require multicast capability to the interface
73168651Skris	 */
732280304Sjkim	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
733280304Sjkim		nd6log((LOG_INFO, "in6_ifattach: "
734280304Sjkim		    "%s is not multicast capable, IPv6 not enabled\n",
73568651Skris		    if_name(ifp)));
736280304Sjkim		return;
737280304Sjkim	}
738280304Sjkim
73968651Skris	/*
740280304Sjkim	 * assign loopback address for loopback interface.
741280304Sjkim	 * XXX multiple loopback interface case.
742280304Sjkim	 */
743280304Sjkim	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
74468651Skris		struct ifaddr *ifa;
745280304Sjkim
746280304Sjkim		in6 = in6addr_loopback;
747280304Sjkim		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
748280304Sjkim		if (ifa == NULL) {
74968651Skris			if (in6_ifattach_loopback(ifp) != 0)
750280304Sjkim				return;
751280304Sjkim		} else
752280304Sjkim			ifa_free(ifa);
753280304Sjkim	}
75468651Skris
755280304Sjkim	/*
756280304Sjkim	 * assign a link-local address, if there's none.
757280304Sjkim	 */
75868651Skris	if (ifp->if_type != IFT_BRIDGE &&
759280304Sjkim	    !(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
760280304Sjkim	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
761280304Sjkim		int error;
76268651Skris
763280304Sjkim		ia = in6ifa_ifpforlinklocal(ifp, 0);
764280304Sjkim		if (ia == NULL) {
765280304Sjkim			error = in6_ifattach_linklocal(ifp, altifp);
76668651Skris#if 0
767280304Sjkim			if (error)
768280304Sjkim				log(LOG_NOTICE, "in6_ifattach_linklocal: "
769280304Sjkim				    "failed to add a link-local addr to %s\n",
77068651Skris				    if_name(ifp));
771280304Sjkim#endif
772280304Sjkim		} else
773280304Sjkim			ifa_free(&ia->ia_ifa);
77468651Skris	}
775280304Sjkim
776280304Sjkim#ifdef IFT_STF			/* XXX */
777280304Sjkimstatinit:
77868651Skris#endif
779280304Sjkim
780280304Sjkim	/* update dynamically. */
781280304Sjkim	if (V_in6_maxmtu < ifp->if_mtu)
78268651Skris		V_in6_maxmtu = ifp->if_mtu;
783280304Sjkim}
784280304Sjkim
785280304Sjkim/*
78668651Skris * NOTE: in6_ifdetach() does not support loopback if at this moment.
787280304Sjkim * We don't need this function in bsdi, because interfaces are never removed
788280304Sjkim * from the ifnet list in bsdi.
789280304Sjkim */
79068651Skrisvoid
791280304Sjkimin6_ifdetach(struct ifnet *ifp)
792280304Sjkim{
793280304Sjkim	struct in6_ifaddr *ia;
79468651Skris	struct ifaddr *ifa, *next;
795280304Sjkim	struct radix_node_head *rnh;
796280304Sjkim	struct rtentry *rt;
797280304Sjkim	short rtflags;
79868651Skris	struct sockaddr_in6 sin6;
799280304Sjkim	struct in6_multi_mship *imm;
800280304Sjkim
801280304Sjkim	/* remove neighbor management table */
80268651Skris	nd6_purge(ifp);
803280304Sjkim
804280304Sjkim	/* nuke any of IPv6 addresses we have */
805280304Sjkim	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
80668651Skris		if (ifa->ifa_addr->sa_family != AF_INET6)
807280304Sjkim			continue;
808280304Sjkim		in6_purgeaddr(ifa);
809280304Sjkim	}
81068651Skris
811280304Sjkim	/* undo everything done by in6_ifattach(), just in case */
812280304Sjkim	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
813280304Sjkim		if (ifa->ifa_addr->sa_family != AF_INET6
81468651Skris		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
815280304Sjkim			continue;
816280304Sjkim		}
817280304Sjkim
81868651Skris		ia = (struct in6_ifaddr *)ifa;
819280304Sjkim
820280304Sjkim		/*
821280304Sjkim		 * leave from multicast groups we have joined for the interface
82268651Skris		 */
823280304Sjkim		while ((imm = ia->ia6_memberships.lh_first) != NULL) {
824280304Sjkim			LIST_REMOVE(imm, i6mm_chain);
825280304Sjkim			in6_leavegroup(imm);
82668651Skris		}
827280304Sjkim
828280304Sjkim		/* remove from the routing table */
829280304Sjkim		if ((ia->ia_flags & IFA_ROUTE) &&
83068651Skris		    (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
831280304Sjkim			rtflags = rt->rt_flags;
832280304Sjkim			RTFREE_LOCKED(rt);
833280304Sjkim			rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
83468651Skris			    (struct sockaddr *)&ia->ia_addr,
835280304Sjkim			    (struct sockaddr *)&ia->ia_prefixmask,
836280304Sjkim			    rtflags, (struct rtentry **)0);
837280304Sjkim		}
83868651Skris
839280304Sjkim		/* remove from the linked list */
840280304Sjkim		IF_ADDR_LOCK(ifp);
841280304Sjkim		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
84268651Skris		IF_ADDR_UNLOCK(ifp);
843280304Sjkim		ifa_free(ifa);				/* if_addrhead */
844280304Sjkim
845280304Sjkim		IN6_IFADDR_WLOCK();
84668651Skris		TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
847280304Sjkim		IN6_IFADDR_WUNLOCK();
848280304Sjkim		ifa_free(ifa);
849280304Sjkim	}
850194206Ssimon
851280304Sjkim	in6_pcbpurgeif0(&V_udbinfo, ifp);
852280304Sjkim	in6_pcbpurgeif0(&V_ripcbinfo, ifp);
853280304Sjkim	/* leave from all multicast groups joined */
854194206Ssimon	in6_purgemaddrs(ifp);
855280304Sjkim
856280304Sjkim	/*
857280304Sjkim	 * remove neighbor management table.  we call it twice just to make
85868651Skris	 * sure we nuke everything.  maybe we need just one call.
859280304Sjkim	 * XXX: since the first call did not release addresses, some prefixes
860280304Sjkim	 * might remain.  We should call nd6_purge() again to release the
861280304Sjkim	 * prefixes after removing all addresses above.
86268651Skris	 * (Or can we just delay calling nd6_purge until at this point?)
863280304Sjkim	 */
864280304Sjkim	nd6_purge(ifp);
865280304Sjkim
86668651Skris	/* remove route to link-local allnodes multicast (ff02::1) */
867280304Sjkim	bzero(&sin6, sizeof(sin6));
868280304Sjkim	sin6.sin6_len = sizeof(struct sockaddr_in6);
869280304Sjkim	sin6.sin6_family = AF_INET6;
87068651Skris	sin6.sin6_addr = in6addr_linklocal_allnodes;
871280304Sjkim	if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
872280304Sjkim		/* XXX: should not fail */
873280304Sjkim		return;
87468651Skris	/* XXX grab lock first to avoid LOR */
875280304Sjkim	rnh = rt_tables_get_rnh(0, AF_INET6);
876280304Sjkim	if (rnh != NULL) {
877280304Sjkim		RADIX_NODE_HEAD_LOCK(rnh);
87868651Skris		rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED);
879280304Sjkim		if (rt) {
880280304Sjkim			if (rt->rt_ifp == ifp)
881280304Sjkim				rtexpunge(rt);
88268651Skris			RTFREE_LOCKED(rt);
883280304Sjkim		}
884280304Sjkim		RADIX_NODE_HEAD_UNLOCK(rnh);
885280304Sjkim	}
88668651Skris}
887280304Sjkim
888280304Sjkimint
889280304Sjkimin6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
89068651Skris    const u_int8_t *baseid, int generate)
891280304Sjkim{
892280304Sjkim	u_int8_t nullbuf[8];
893280304Sjkim	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
89468651Skris
895280304Sjkim	bzero(nullbuf, sizeof(nullbuf));
896280304Sjkim	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
897280304Sjkim		/* we've never created a random ID.  Create a new one. */
89868651Skris		generate = 1;
899280304Sjkim	}
900280304Sjkim
901280304Sjkim	if (generate) {
90268651Skris		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
903280304Sjkim
904280304Sjkim		/* generate_tmp_ifid will update seedn and buf */
905280304Sjkim		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
90668651Skris		    ndi->randomid);
907280304Sjkim	}
908280304Sjkim	bcopy(ndi->randomid, retbuf, 8);
909280304Sjkim
91068651Skris	return (0);
911280304Sjkim}
912280304Sjkim
913280304Sjkimvoid
91468651Skrisin6_tmpaddrtimer(void *arg)
915280304Sjkim{
916280304Sjkim	CURVNET_SET((struct vnet *) arg);
917280304Sjkim	struct nd_ifinfo *ndi;
91868651Skris	u_int8_t nullbuf[8];
919280304Sjkim	struct ifnet *ifp;
920280304Sjkim
921280304Sjkim	callout_reset(&V_in6_tmpaddrtimer_ch,
92268651Skris	    (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
923280304Sjkim	    V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
924280304Sjkim
925280304Sjkim	bzero(nullbuf, sizeof(nullbuf));
92668651Skris	for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
927280304Sjkim	    ifp = TAILQ_NEXT(ifp, if_list)) {
928280304Sjkim		ndi = ND_IFINFO(ifp);
929280304Sjkim		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
93068651Skris			/*
931280304Sjkim			 * We've been generating a random ID on this interface.
932280304Sjkim			 * Create a new one.
933280304Sjkim			 */
93468651Skris			(void)generate_tmp_ifid(ndi->randomseed0,
935280304Sjkim			    ndi->randomseed1, ndi->randomid);
936280304Sjkim		}
937280304Sjkim	}
93868651Skris
939280304Sjkim	CURVNET_RESTORE();
940280304Sjkim}
941280304Sjkim
94268651Skrisstatic void
943280304Sjkimin6_purgemaddrs(struct ifnet *ifp)
944280304Sjkim{
945280304Sjkim	LIST_HEAD(,in6_multi)	 purgeinms;
94668651Skris	struct in6_multi	*inm, *tinm;
947280304Sjkim	struct ifmultiaddr	*ifma;
948280304Sjkim
949280304Sjkim	LIST_INIT(&purgeinms);
95068651Skris	IN6_MULTI_LOCK();
951280304Sjkim
952280304Sjkim	/*
953280304Sjkim	 * Extract list of in6_multi associated with the detaching ifp
95468651Skris	 * which the PF_INET6 layer is about to release.
955280304Sjkim	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
956280304Sjkim	 * by code further down.
957280304Sjkim	 */
95868651Skris	IF_ADDR_LOCK(ifp);
959280304Sjkim	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
960280304Sjkim		if (ifma->ifma_addr->sa_family != AF_INET6 ||
961280304Sjkim		    ifma->ifma_protospec == NULL)
96268651Skris			continue;
963280304Sjkim		inm = (struct in6_multi *)ifma->ifma_protospec;
964280304Sjkim		LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
965280304Sjkim	}
96668651Skris	IF_ADDR_UNLOCK(ifp);
967280304Sjkim
968280304Sjkim	LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
969280304Sjkim		LIST_REMOVE(inm, in6m_entry);
97068651Skris		in6m_release_locked(inm);
971280304Sjkim	}
972280304Sjkim	mld_ifdetach(ifp);
973280304Sjkim
97468651Skris	IN6_MULTI_UNLOCK();
975280304Sjkim}
976280304Sjkim