in6_ifattach.c revision 252021
1209616Sjfv/*-
2209616Sjfv * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3294958Smarius * All rights reserved.
4209616Sjfv *
5209616Sjfv * Redistribution and use in source and binary forms, with or without
6209616Sjfv * modification, are permitted provided that the following conditions
7209616Sjfv * are met:
8209616Sjfv * 1. Redistributions of source code must retain the above copyright
9209616Sjfv *    notice, this list of conditions and the following disclaimer.
10209616Sjfv * 2. Redistributions in binary form must reproduce the above copyright
11209616Sjfv *    notice, this list of conditions and the following disclaimer in the
12209616Sjfv *    documentation and/or other materials provided with the distribution.
13209616Sjfv * 3. Neither the name of the project nor the names of its contributors
14209616Sjfv *    may be used to endorse or promote products derived from this software
15209616Sjfv *    without specific prior written permission.
16209616Sjfv *
17209616Sjfv * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18209616Sjfv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19209616Sjfv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20209616Sjfv * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21209616Sjfv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22209616Sjfv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23209616Sjfv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24209616Sjfv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25209616Sjfv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26209616Sjfv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27209616Sjfv * SUCH DAMAGE.
28209616Sjfv *
29209616Sjfv *	$KAME: in6_ifattach.c,v 1.118 2001/05/24 07:44:00 itojun Exp $
30209616Sjfv */
31209616Sjfv
32209616Sjfv#include <sys/cdefs.h>
33209616Sjfv__FBSDID("$FreeBSD: stable/9/sys/netinet6/in6_ifattach.c 252021 2013-06-20 07:23:04Z hrs $");
34209616Sjfv
35209616Sjfv#include <sys/param.h>
36209616Sjfv#include <sys/systm.h>
37209616Sjfv#include <sys/malloc.h>
38209616Sjfv#include <sys/socket.h>
39209616Sjfv#include <sys/sockio.h>
40209616Sjfv#include <sys/jail.h>
41269196Sjfv#include <sys/kernel.h>
42269196Sjfv#include <sys/proc.h>
43209616Sjfv#include <sys/syslog.h>
44209616Sjfv#include <sys/md5.h>
45209616Sjfv
46209616Sjfv#include <net/if.h>
47209616Sjfv#include <net/if_dl.h>
48209616Sjfv#include <net/if_types.h>
49209616Sjfv#include <net/route.h>
50209616Sjfv#include <net/vnet.h>
51209616Sjfv
52209616Sjfv#include <netinet/in.h>
53269196Sjfv#include <netinet/in_var.h>
54269196Sjfv#include <netinet/if_ether.h>
55269196Sjfv#include <netinet/in_pcb.h>
56269196Sjfv#include <netinet/ip_var.h>
57209616Sjfv#include <netinet/udp.h>
58209616Sjfv#include <netinet/udp_var.h>
59209616Sjfv
60209616Sjfv#include <netinet/ip6.h>
61209616Sjfv#include <netinet6/ip6_var.h>
62209616Sjfv#include <netinet6/in6_var.h>
63209616Sjfv#include <netinet6/in6_pcb.h>
64209616Sjfv#include <netinet6/in6_ifattach.h>
65209616Sjfv#include <netinet6/ip6_var.h>
66209616Sjfv#include <netinet6/nd6.h>
67209616Sjfv#include <netinet6/mld6_var.h>
68209616Sjfv#include <netinet6/scope6_var.h>
69209616Sjfv
70209616SjfvVNET_DEFINE(unsigned long, in6_maxmtu) = 0;
71209616Sjfv
72209616Sjfv#ifdef IP6_AUTO_LINKLOCAL
73209616SjfvVNET_DEFINE(int, ip6_auto_linklocal) = IP6_AUTO_LINKLOCAL;
74209616Sjfv#else
75209616SjfvVNET_DEFINE(int, ip6_auto_linklocal) = 1;	/* enabled by default */
76209616Sjfv#endif
77209616Sjfv
78209616SjfvVNET_DEFINE(struct callout, in6_tmpaddrtimer_ch);
79209616Sjfv#define	V_in6_tmpaddrtimer_ch		VNET(in6_tmpaddrtimer_ch)
80209616Sjfv
81209616SjfvVNET_DECLARE(struct inpcbinfo, ripcbinfo);
82209616Sjfv#define	V_ripcbinfo			VNET(ripcbinfo)
83209616Sjfv
84209616Sjfvstatic int get_rand_ifid(struct ifnet *, struct in6_addr *);
85209616Sjfvstatic int generate_tmp_ifid(u_int8_t *, const u_int8_t *, u_int8_t *);
86209616Sjfvstatic int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
87209616Sjfvstatic int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
88209616Sjfvstatic int in6_ifattach_loopback(struct ifnet *);
89209616Sjfvstatic void in6_purgemaddrs(struct ifnet *);
90209616Sjfv
91209616Sjfv#define EUI64_GBIT	0x01
92209616Sjfv#define EUI64_UBIT	0x02
93209616Sjfv#define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
94209616Sjfv#define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
95209616Sjfv#define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
96209616Sjfv#define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
97209616Sjfv#define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
98209616Sjfv
99209616Sjfv#define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
100209616Sjfv#define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
101209616Sjfv
102209616Sjfv/*
103209616Sjfv * Generate a last-resort interface identifier, when the machine has no
104209616Sjfv * IEEE802/EUI64 address sources.
105209616Sjfv * The goal here is to get an interface identifier that is
106209616Sjfv * (1) random enough and (2) does not change across reboot.
107209616Sjfv * We currently use MD5(hostname) for it.
108209616Sjfv *
109209616Sjfv * in6 - upper 64bits are preserved
110209616Sjfv */
111209616Sjfvstatic int
112209616Sjfvget_rand_ifid(struct ifnet *ifp, struct in6_addr *in6)
113209616Sjfv{
114209616Sjfv	MD5_CTX ctxt;
115209616Sjfv	struct prison *pr;
116209616Sjfv	u_int8_t digest[16];
117209616Sjfv	int hostnamelen;
118209616Sjfv
119209616Sjfv	pr = curthread->td_ucred->cr_prison;
120209616Sjfv	mtx_lock(&pr->pr_mtx);
121209616Sjfv	hostnamelen = strlen(pr->pr_hostname);
122209616Sjfv#if 0
123209616Sjfv	/* we need at least several letters as seed for ifid */
124209616Sjfv	if (hostnamelen < 3) {
125209616Sjfv		mtx_unlock(&pr->pr_mtx);
126209616Sjfv		return -1;
127209616Sjfv	}
128209616Sjfv#endif
129209616Sjfv
130209616Sjfv	/* generate 8 bytes of pseudo-random value. */
131209616Sjfv	bzero(&ctxt, sizeof(ctxt));
132209616Sjfv	MD5Init(&ctxt);
133209616Sjfv	MD5Update(&ctxt, pr->pr_hostname, hostnamelen);
134209616Sjfv	mtx_unlock(&pr->pr_mtx);
135209616Sjfv	MD5Final(digest, &ctxt);
136209616Sjfv
137209616Sjfv	/* assumes sizeof(digest) > sizeof(ifid) */
138209616Sjfv	bcopy(digest, &in6->s6_addr[8], 8);
139209616Sjfv
140209616Sjfv	/* make sure to set "u" bit to local, and "g" bit to individual. */
141209616Sjfv	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
142209616Sjfv	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
143209616Sjfv
144209616Sjfv	/* convert EUI64 into IPv6 interface identifier */
145209616Sjfv	EUI64_TO_IFID(in6);
146209616Sjfv
147209616Sjfv	return 0;
148209616Sjfv}
149209616Sjfv
150209616Sjfvstatic int
151209616Sjfvgenerate_tmp_ifid(u_int8_t *seed0, const u_int8_t *seed1, u_int8_t *ret)
152209616Sjfv{
153209616Sjfv	MD5_CTX ctxt;
154209616Sjfv	u_int8_t seed[16], digest[16], nullbuf[8];
155209616Sjfv	u_int32_t val32;
156209616Sjfv
157209616Sjfv	/* If there's no history, start with a random seed. */
158209616Sjfv	bzero(nullbuf, sizeof(nullbuf));
159209616Sjfv	if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
160209616Sjfv		int i;
161209616Sjfv
162209616Sjfv		for (i = 0; i < 2; i++) {
163209616Sjfv			val32 = arc4random();
164209616Sjfv			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
165209616Sjfv		}
166209616Sjfv	} else
167209616Sjfv		bcopy(seed0, seed, 8);
168209616Sjfv
169209616Sjfv	/* copy the right-most 64-bits of the given address */
170209616Sjfv	/* XXX assumption on the size of IFID */
171209616Sjfv	bcopy(seed1, &seed[8], 8);
172209616Sjfv
173209616Sjfv	if (0) {		/* for debugging purposes only */
174209616Sjfv		int i;
175209616Sjfv
176209616Sjfv		printf("generate_tmp_ifid: new randomized ID from: ");
177209616Sjfv		for (i = 0; i < 16; i++)
178209616Sjfv			printf("%02x", seed[i]);
179209616Sjfv		printf(" ");
180209616Sjfv	}
181209616Sjfv
182209616Sjfv	/* generate 16 bytes of pseudo-random value. */
183209616Sjfv	bzero(&ctxt, sizeof(ctxt));
184209616Sjfv	MD5Init(&ctxt);
185209616Sjfv	MD5Update(&ctxt, seed, sizeof(seed));
186209616Sjfv	MD5Final(digest, &ctxt);
187209616Sjfv
188209616Sjfv	/*
189209616Sjfv	 * RFC 3041 3.2.1. (3)
190209616Sjfv	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
191209616Sjfv	 * left-most bit is numbered 0) to zero.
192209616Sjfv	 */
193209616Sjfv	bcopy(digest, ret, 8);
194209616Sjfv	ret[0] &= ~EUI64_UBIT;
195209616Sjfv
196209616Sjfv	/*
197209616Sjfv	 * XXX: we'd like to ensure that the generated value is not zero
198209616Sjfv	 * for simplicity.  If the caclculated digest happens to be zero,
199209616Sjfv	 * use a random non-zero value as the last resort.
200209616Sjfv	 */
201209616Sjfv	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
202209616Sjfv		nd6log((LOG_INFO,
203209616Sjfv		    "generate_tmp_ifid: computed MD5 value is zero.\n"));
204209616Sjfv
205209616Sjfv		val32 = arc4random();
206209616Sjfv		val32 = 1 + (val32 % (0xffffffff - 1));
207209616Sjfv	}
208209616Sjfv
209209616Sjfv	/*
210209616Sjfv	 * RFC 3041 3.2.1. (4)
211209616Sjfv	 * Take the rightmost 64-bits of the MD5 digest and save them in
212209616Sjfv	 * stable storage as the history value to be used in the next
213209616Sjfv	 * iteration of the algorithm.
214209616Sjfv	 */
215209616Sjfv	bcopy(&digest[8], seed0, 8);
216209616Sjfv
217209616Sjfv	if (0) {		/* for debugging purposes only */
218209616Sjfv		int i;
219209616Sjfv
220209616Sjfv		printf("to: ");
221209616Sjfv		for (i = 0; i < 16; i++)
222209616Sjfv			printf("%02x", digest[i]);
223209616Sjfv		printf("\n");
224209616Sjfv	}
225209616Sjfv
226209616Sjfv	return 0;
227209616Sjfv}
228209616Sjfv
229209616Sjfv/*
230209616Sjfv * Get interface identifier for the specified interface.
231209616Sjfv * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
232209616Sjfv *
233209616Sjfv * in6 - upper 64bits are preserved
234209616Sjfv */
235209616Sjfvint
236209616Sjfvin6_get_hw_ifid(struct ifnet *ifp, struct in6_addr *in6)
237209616Sjfv{
238209616Sjfv	struct ifaddr *ifa;
239209616Sjfv	struct sockaddr_dl *sdl;
240209616Sjfv	u_int8_t *addr;
241209616Sjfv	size_t addrlen;
242209616Sjfv	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
243209616Sjfv	static u_int8_t allone[8] =
244209616Sjfv		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
245209616Sjfv
246209616Sjfv	IF_ADDR_RLOCK(ifp);
247209616Sjfv	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
248209616Sjfv		if (ifa->ifa_addr->sa_family != AF_LINK)
249209616Sjfv			continue;
250209616Sjfv		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
251209616Sjfv		if (sdl == NULL)
252209616Sjfv			continue;
253209616Sjfv		if (sdl->sdl_alen == 0)
254209616Sjfv			continue;
255209616Sjfv
256209616Sjfv		goto found;
257209616Sjfv	}
258209616Sjfv	IF_ADDR_RUNLOCK(ifp);
259209616Sjfv
260209616Sjfv	return -1;
261209616Sjfv
262209616Sjfvfound:
263209616Sjfv	IF_ADDR_LOCK_ASSERT(ifp);
264209616Sjfv	addr = LLADDR(sdl);
265209616Sjfv	addrlen = sdl->sdl_alen;
266209616Sjfv
267209616Sjfv	/* get EUI64 */
268209616Sjfv	switch (ifp->if_type) {
269209616Sjfv	case IFT_ETHER:
270209616Sjfv	case IFT_L2VLAN:
271209616Sjfv	case IFT_FDDI:
272209616Sjfv	case IFT_ISO88025:
273209616Sjfv	case IFT_ATM:
274209616Sjfv	case IFT_IEEE1394:
275209616Sjfv#ifdef IFT_IEEE80211
276209616Sjfv	case IFT_IEEE80211:
277209616Sjfv#endif
278209616Sjfv		/* IEEE802/EUI64 cases - what others? */
279209616Sjfv		/* IEEE1394 uses 16byte length address starting with EUI64 */
280209616Sjfv		if (addrlen > 8)
281209616Sjfv			addrlen = 8;
282209616Sjfv
283209616Sjfv		/* look at IEEE802/EUI64 only */
284209616Sjfv		if (addrlen != 8 && addrlen != 6) {
285209616Sjfv			IF_ADDR_RUNLOCK(ifp);
286209616Sjfv			return -1;
287209616Sjfv		}
288209616Sjfv
289209616Sjfv		/*
290209616Sjfv		 * check for invalid MAC address - on bsdi, we see it a lot
291209616Sjfv		 * since wildboar configures all-zero MAC on pccard before
292209616Sjfv		 * card insertion.
293209616Sjfv		 */
294209616Sjfv		if (bcmp(addr, allzero, addrlen) == 0) {
295209616Sjfv			IF_ADDR_RUNLOCK(ifp);
296209616Sjfv			return -1;
297209616Sjfv		}
298209616Sjfv		if (bcmp(addr, allone, addrlen) == 0) {
299209616Sjfv			IF_ADDR_RUNLOCK(ifp);
300209616Sjfv			return -1;
301209616Sjfv		}
302209616Sjfv
303209616Sjfv		/* make EUI64 address */
304209616Sjfv		if (addrlen == 8)
305209616Sjfv			bcopy(addr, &in6->s6_addr[8], 8);
306209616Sjfv		else if (addrlen == 6) {
307209616Sjfv			in6->s6_addr[8] = addr[0];
308209616Sjfv			in6->s6_addr[9] = addr[1];
309209616Sjfv			in6->s6_addr[10] = addr[2];
310209616Sjfv			in6->s6_addr[11] = 0xff;
311209616Sjfv			in6->s6_addr[12] = 0xfe;
312209616Sjfv			in6->s6_addr[13] = addr[3];
313209616Sjfv			in6->s6_addr[14] = addr[4];
314209616Sjfv			in6->s6_addr[15] = addr[5];
315209616Sjfv		}
316209616Sjfv		break;
317209616Sjfv
318209616Sjfv	case IFT_ARCNET:
319209616Sjfv		if (addrlen != 1) {
320209616Sjfv			IF_ADDR_RUNLOCK(ifp);
321209616Sjfv			return -1;
322209616Sjfv		}
323209616Sjfv		if (!addr[0]) {
324209616Sjfv			IF_ADDR_RUNLOCK(ifp);
325209616Sjfv			return -1;
326209616Sjfv		}
327209616Sjfv
328209616Sjfv		bzero(&in6->s6_addr[8], 8);
329209616Sjfv		in6->s6_addr[15] = addr[0];
330209616Sjfv
331209616Sjfv		/*
332209616Sjfv		 * due to insufficient bitwidth, we mark it local.
333209616Sjfv		 */
334209616Sjfv		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
335209616Sjfv		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
336209616Sjfv		break;
337209616Sjfv
338209616Sjfv	case IFT_GIF:
339209616Sjfv#ifdef IFT_STF
340209616Sjfv	case IFT_STF:
341209616Sjfv#endif
342209616Sjfv		/*
343209616Sjfv		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
344209616Sjfv		 * however, IPv4 address is not very suitable as unique
345209616Sjfv		 * identifier source (can be renumbered).
346209616Sjfv		 * we don't do this.
347209616Sjfv		 */
348209616Sjfv		IF_ADDR_RUNLOCK(ifp);
349209616Sjfv		return -1;
350209616Sjfv
351209616Sjfv	default:
352209616Sjfv		IF_ADDR_RUNLOCK(ifp);
353209616Sjfv		return -1;
354209616Sjfv	}
355209616Sjfv
356209616Sjfv	/* sanity check: g bit must not indicate "group" */
357209616Sjfv	if (EUI64_GROUP(in6)) {
358209616Sjfv		IF_ADDR_RUNLOCK(ifp);
359209616Sjfv		return -1;
360269196Sjfv	}
361269196Sjfv
362209616Sjfv	/* convert EUI64 into IPv6 interface identifier */
363209616Sjfv	EUI64_TO_IFID(in6);
364209616Sjfv
365209616Sjfv	/*
366209616Sjfv	 * sanity check: ifid must not be all zero, avoid conflict with
367209616Sjfv	 * subnet router anycast
368209616Sjfv	 */
369209616Sjfv	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
370209616Sjfv	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
371209616Sjfv		IF_ADDR_RUNLOCK(ifp);
372209616Sjfv		return -1;
373209616Sjfv	}
374209616Sjfv
375209616Sjfv	IF_ADDR_RUNLOCK(ifp);
376209616Sjfv	return 0;
377209616Sjfv}
378209616Sjfv
379209616Sjfv/*
380209616Sjfv * Get interface identifier for the specified interface.  If it is not
381209616Sjfv * available on ifp0, borrow interface identifier from other information
382269196Sjfv * sources.
383269196Sjfv *
384209616Sjfv * altifp - secondary EUI64 source
385209616Sjfv */
386209616Sjfvstatic int
387209616Sjfvget_ifid(struct ifnet *ifp0, struct ifnet *altifp,
388209616Sjfv    struct in6_addr *in6)
389209616Sjfv{
390209616Sjfv	struct ifnet *ifp;
391209616Sjfv
392209616Sjfv	/* first, try to get it from the interface itself */
393209616Sjfv	if (in6_get_hw_ifid(ifp0, in6) == 0) {
394209616Sjfv		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
395209616Sjfv		    if_name(ifp0)));
396209616Sjfv		goto success;
397209616Sjfv	}
398209616Sjfv
399209616Sjfv	/* try secondary EUI64 source. this basically is for ATM PVC */
400209616Sjfv	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
401209616Sjfv		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
402209616Sjfv		    if_name(ifp0), if_name(altifp)));
403209616Sjfv		goto success;
404269196Sjfv	}
405269196Sjfv
406209616Sjfv	/* next, try to get it from some other hardware interface */
407209616Sjfv	IFNET_RLOCK_NOSLEEP();
408209616Sjfv	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
409209616Sjfv		if (ifp == ifp0)
410209616Sjfv			continue;
411209616Sjfv		if (in6_get_hw_ifid(ifp, in6) != 0)
412269196Sjfv			continue;
413209616Sjfv
414209616Sjfv		/*
415209616Sjfv		 * to borrow ifid from other interface, ifid needs to be
416209616Sjfv		 * globally unique
417209616Sjfv		 */
418209616Sjfv		if (IFID_UNIVERSAL(in6)) {
419209616Sjfv			nd6log((LOG_DEBUG,
420209616Sjfv			    "%s: borrow interface identifier from %s\n",
421209616Sjfv			    if_name(ifp0), if_name(ifp)));
422209616Sjfv			IFNET_RUNLOCK_NOSLEEP();
423209616Sjfv			goto success;
424209616Sjfv		}
425209616Sjfv	}
426209616Sjfv	IFNET_RUNLOCK_NOSLEEP();
427209616Sjfv
428209616Sjfv	/* last resort: get from random number source */
429296055Serj	if (get_rand_ifid(ifp, in6) == 0) {
430209616Sjfv		nd6log((LOG_DEBUG,
431209616Sjfv		    "%s: interface identifier generated by random number\n",
432209616Sjfv		    if_name(ifp0)));
433296055Serj		goto success;
434296055Serj	}
435296055Serj
436209616Sjfv	printf("%s: failed to get interface identifier\n", if_name(ifp0));
437296055Serj	return -1;
438296055Serj
439296055Serjsuccess:
440296055Serj	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
441296055Serj	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
442296055Serj	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
443296055Serj	    in6->s6_addr[14], in6->s6_addr[15]));
444209616Sjfv	return 0;
445209616Sjfv}
446209616Sjfv
447209616Sjfv/*
448209616Sjfv * altifp - secondary EUI64 source
449209616Sjfv */
450209616Sjfvstatic int
451209616Sjfvin6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
452209616Sjfv{
453209616Sjfv	struct in6_ifaddr *ia;
454209616Sjfv	struct in6_aliasreq ifra;
455209616Sjfv	struct nd_prefixctl pr0;
456209616Sjfv	int i, error;
457209616Sjfv
458269196Sjfv	/*
459209616Sjfv	 * configure link-local address.
460209616Sjfv	 */
461209616Sjfv	bzero(&ifra, sizeof(ifra));
462209616Sjfv
463209616Sjfv	/*
464209616Sjfv	 * in6_update_ifa() does not use ifra_name, but we accurately set it
465209616Sjfv	 * for safety.
466209616Sjfv	 */
467209616Sjfv	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
468209616Sjfv
469209616Sjfv	ifra.ifra_addr.sin6_family = AF_INET6;
470209616Sjfv	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
471209616Sjfv	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
472209616Sjfv	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
473209616Sjfv	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
474209616Sjfv		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
475209616Sjfv		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
476209616Sjfv	} else {
477209616Sjfv		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
478209616Sjfv			nd6log((LOG_ERR,
479209616Sjfv			    "%s: no ifid available\n", if_name(ifp)));
480209616Sjfv			return (-1);
481209616Sjfv		}
482209616Sjfv	}
483209616Sjfv	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
484209616Sjfv		return (-1);
485209616Sjfv
486209616Sjfv	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
487209616Sjfv	ifra.ifra_prefixmask.sin6_family = AF_INET6;
488209616Sjfv	ifra.ifra_prefixmask.sin6_addr = in6mask64;
489209616Sjfv	/* link-local addresses should NEVER expire. */
490209616Sjfv	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
491209616Sjfv	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
492209616Sjfv
493209616Sjfv	/*
494209616Sjfv	 * Now call in6_update_ifa() to do a bunch of procedures to configure
495209616Sjfv	 * a link-local address. We can set the 3rd argument to NULL, because
496209616Sjfv	 * we know there's no other link-local address on the interface
497209616Sjfv	 * and therefore we are adding one (instead of updating one).
498209616Sjfv	 */
499269196Sjfv	if ((error = in6_update_ifa(ifp, &ifra, NULL,
500209616Sjfv				    IN6_IFAUPDATE_DADDELAY)) != 0) {
501209616Sjfv		/*
502209616Sjfv		 * XXX: When the interface does not support IPv6, this call
503209616Sjfv		 * would fail in the SIOCSIFADDR ioctl.  I believe the
504209616Sjfv		 * notification is rather confusing in this case, so just
505209616Sjfv		 * suppress it.  (jinmei@kame.net 20010130)
506209616Sjfv		 */
507209616Sjfv		if (error != EAFNOSUPPORT)
508209616Sjfv			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
509209616Sjfv			    "configure a link-local address on %s "
510209616Sjfv			    "(errno=%d)\n",
511209616Sjfv			    if_name(ifp), error));
512209616Sjfv		return (-1);
513209616Sjfv	}
514209616Sjfv
515209616Sjfv	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
516209616Sjfv	KASSERT(ia != NULL, ("%s: ia == NULL, ifp=%p", __func__, ifp));
517209616Sjfv
518209616Sjfv	ifa_free(&ia->ia_ifa);
519209616Sjfv
520209616Sjfv	/*
521209616Sjfv	 * Make the link-local prefix (fe80::%link/64) as on-link.
522209616Sjfv	 * Since we'd like to manage prefixes separately from addresses,
523209616Sjfv	 * we make an ND6 prefix structure for the link-local prefix,
524209616Sjfv	 * and add it to the prefix list as a never-expire prefix.
525209616Sjfv	 * XXX: this change might affect some existing code base...
526209616Sjfv	 */
527209616Sjfv	bzero(&pr0, sizeof(pr0));
528209616Sjfv	pr0.ndpr_ifp = ifp;
529209616Sjfv	/* this should be 64 at this moment. */
530209616Sjfv	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
531209616Sjfv	pr0.ndpr_prefix = ifra.ifra_addr;
532209616Sjfv	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
533209616Sjfv	for (i = 0; i < 4; i++) {
534209616Sjfv		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
535209616Sjfv		    in6mask64.s6_addr32[i];
536209616Sjfv	}
537209616Sjfv	/*
538209616Sjfv	 * Initialize parameters.  The link-local prefix must always be
539209616Sjfv	 * on-link, and its lifetimes never expire.
540209616Sjfv	 */
541209616Sjfv	pr0.ndpr_raf_onlink = 1;
542209616Sjfv	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
543209616Sjfv	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
544209616Sjfv	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
545209616Sjfv	/*
546209616Sjfv	 * Since there is no other link-local addresses, nd6_prefix_lookup()
547209616Sjfv	 * probably returns NULL.  However, we cannot always expect the result.
548209616Sjfv	 * For example, if we first remove the (only) existing link-local
549209616Sjfv	 * address, and then reconfigure another one, the prefix is still
550209616Sjfv	 * valid with referring to the old link-local address.
551209616Sjfv	 */
552209616Sjfv	if (nd6_prefix_lookup(&pr0) == NULL) {
553209616Sjfv		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
554209616Sjfv			return (error);
555209616Sjfv	}
556209616Sjfv
557209616Sjfv	return 0;
558209616Sjfv}
559209616Sjfv
560209616Sjfv/*
561209616Sjfv * ifp - must be IFT_LOOP
562209616Sjfv */
563209616Sjfvstatic int
564209616Sjfvin6_ifattach_loopback(struct ifnet *ifp)
565209616Sjfv{
566209616Sjfv	struct in6_aliasreq ifra;
567209616Sjfv	int error;
568209616Sjfv
569209616Sjfv	bzero(&ifra, sizeof(ifra));
570209616Sjfv
571209616Sjfv	/*
572209616Sjfv	 * in6_update_ifa() does not use ifra_name, but we accurately set it
573209616Sjfv	 * for safety.
574209616Sjfv	 */
575209616Sjfv	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
576209616Sjfv
577209616Sjfv	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
578209616Sjfv	ifra.ifra_prefixmask.sin6_family = AF_INET6;
579209616Sjfv	ifra.ifra_prefixmask.sin6_addr = in6mask128;
580209616Sjfv
581209616Sjfv	/*
582209616Sjfv	 * Always initialize ia_dstaddr (= broadcast address) to loopback
583209616Sjfv	 * address.  Follows IPv4 practice - see in_ifinit().
584209616Sjfv	 */
585209616Sjfv	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
586209616Sjfv	ifra.ifra_dstaddr.sin6_family = AF_INET6;
587209616Sjfv	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
588209616Sjfv
589209616Sjfv	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
590209616Sjfv	ifra.ifra_addr.sin6_family = AF_INET6;
591209616Sjfv	ifra.ifra_addr.sin6_addr = in6addr_loopback;
592209616Sjfv
593209616Sjfv	/* the loopback  address should NEVER expire. */
594209616Sjfv	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
595209616Sjfv	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
596209616Sjfv
597209616Sjfv	/* we don't need to perform DAD on loopback interfaces. */
598209616Sjfv	ifra.ifra_flags |= IN6_IFF_NODAD;
599209616Sjfv
600209616Sjfv	/* skip registration to the prefix list. XXX should be temporary. */
601209616Sjfv	ifra.ifra_flags |= IN6_IFF_NOPFX;
602209616Sjfv
603209616Sjfv	/*
604209616Sjfv	 * We are sure that this is a newly assigned address, so we can set
605209616Sjfv	 * NULL to the 3rd arg.
606209616Sjfv	 */
607209616Sjfv	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
608209616Sjfv		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
609209616Sjfv		    "the loopback address on %s (errno=%d)\n",
610209616Sjfv		    if_name(ifp), error));
611209616Sjfv		return (-1);
612209616Sjfv	}
613209616Sjfv
614209616Sjfv	return 0;
615209616Sjfv}
616209616Sjfv
617209616Sjfv/*
618209616Sjfv * compute NI group address, based on the current hostname setting.
619209616Sjfv * see RFC 4620.
620209616Sjfv *
621209616Sjfv * when ifp == NULL, the caller is responsible for filling scopeid.
622209616Sjfv *
623209616Sjfv * If oldmcprefix == 1, FF02:0:0:0:0:2::/96 is used for NI group address
624209616Sjfv * while it is FF02:0:0:0:0:2:FF00::/104 in RFC 4620.
625209616Sjfv */
626209616Sjfvstatic int
627209616Sjfvin6_nigroup0(struct ifnet *ifp, const char *name, int namelen,
628209616Sjfv    struct in6_addr *in6, int oldmcprefix)
629209616Sjfv{
630209616Sjfv	struct prison *pr;
631209616Sjfv	const char *p;
632209616Sjfv	u_char *q;
633209616Sjfv	MD5_CTX ctxt;
634209616Sjfv	u_int8_t digest[16];
635209616Sjfv	char l;
636209616Sjfv	char n[64];	/* a single label must not exceed 63 chars */
637209616Sjfv
638209616Sjfv	/*
639209616Sjfv	 * If no name is given and namelen is -1,
640209616Sjfv	 * we try to do the hostname lookup ourselves.
641209616Sjfv	 */
642209616Sjfv	if (!name && namelen == -1) {
643209616Sjfv		pr = curthread->td_ucred->cr_prison;
644209616Sjfv		mtx_lock(&pr->pr_mtx);
645209616Sjfv		name = pr->pr_hostname;
646209616Sjfv		namelen = strlen(name);
647209616Sjfv	} else
648296055Serj		pr = NULL;
649209616Sjfv	if (!name || !namelen) {
650209616Sjfv		if (pr != NULL)
651209616Sjfv			mtx_unlock(&pr->pr_mtx);
652296055Serj		return -1;
653296055Serj	}
654296055Serj
655296055Serj	p = name;
656209616Sjfv	while (p && *p && *p != '.' && p - name < namelen)
657296055Serj		p++;
658296055Serj	if (p == name || p - name > sizeof(n) - 1) {
659296055Serj		if (pr != NULL)
660296055Serj			mtx_unlock(&pr->pr_mtx);
661296055Serj		return -1;	/* label too long */
662296055Serj	}
663296055Serj	l = p - name;
664296055Serj	strncpy(n, name, l);
665209616Sjfv	if (pr != NULL)
666209616Sjfv		mtx_unlock(&pr->pr_mtx);
667296055Serj	n[(int)l] = '\0';
668209616Sjfv	for (q = n; *q; q++) {
669209616Sjfv		if ('A' <= *q && *q <= 'Z')
670209616Sjfv			*q = *q - 'A' + 'a';
671209616Sjfv	}
672209616Sjfv
673209616Sjfv	/* generate 16 bytes of pseudo-random value. */
674209616Sjfv	bzero(&ctxt, sizeof(ctxt));
675209616Sjfv	MD5Init(&ctxt);
676209616Sjfv	MD5Update(&ctxt, &l, sizeof(l));
677209616Sjfv	MD5Update(&ctxt, n, l);
678209616Sjfv	MD5Final(digest, &ctxt);
679209616Sjfv
680269196Sjfv	bzero(in6, sizeof(*in6));
681209616Sjfv	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
682209616Sjfv	in6->s6_addr8[11] = 2;
683209616Sjfv	if (oldmcprefix == 0) {
684209616Sjfv		in6->s6_addr8[12] = 0xff;
685209616Sjfv	 	/* Copy the first 24 bits of 128-bit hash into the address. */
686209616Sjfv		bcopy(digest, &in6->s6_addr8[13], 3);
687209616Sjfv	} else {
688209616Sjfv	 	/* Copy the first 32 bits of 128-bit hash into the address. */
689209616Sjfv		bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
690209616Sjfv	}
691209616Sjfv	if (in6_setscope(in6, ifp, NULL))
692209616Sjfv		return (-1); /* XXX: should not fail */
693209616Sjfv
694209616Sjfv	return 0;
695209616Sjfv}
696209616Sjfv
697209616Sjfvint
698209616Sjfvin6_nigroup(struct ifnet *ifp, const char *name, int namelen,
699209616Sjfv    struct in6_addr *in6)
700209616Sjfv{
701209616Sjfv
702209616Sjfv	return (in6_nigroup0(ifp, name, namelen, in6, 0));
703209616Sjfv}
704209616Sjfv
705209616Sjfvint
706209616Sjfvin6_nigroup_oldmcprefix(struct ifnet *ifp, const char *name, int namelen,
707209616Sjfv    struct in6_addr *in6)
708209616Sjfv{
709209616Sjfv
710209616Sjfv	return (in6_nigroup0(ifp, name, namelen, in6, 1));
711209616Sjfv}
712209616Sjfv
713209616Sjfv/*
714209616Sjfv * XXX multiple loopback interface needs more care.  for instance,
715209616Sjfv * nodelocal address needs to be configured onto only one of them.
716209616Sjfv * XXX multiple link-local address case
717209616Sjfv *
718209616Sjfv * altifp - secondary EUI64 source
719209616Sjfv */
720209616Sjfvvoid
721209616Sjfvin6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
722209616Sjfv{
723269196Sjfv	struct in6_ifaddr *ia;
724209616Sjfv	struct in6_addr in6;
725209616Sjfv
726209616Sjfv	/* some of the interfaces are inherently not IPv6 capable */
727209616Sjfv	switch (ifp->if_type) {
728209616Sjfv	case IFT_PFLOG:
729209616Sjfv	case IFT_PFSYNC:
730209616Sjfv	case IFT_CARP:
731209616Sjfv		return;
732209616Sjfv	}
733209616Sjfv
734209616Sjfv	/*
735209616Sjfv	 * quirks based on interface type
736209616Sjfv	 */
737209616Sjfv	switch (ifp->if_type) {
738209616Sjfv#ifdef IFT_STF
739209616Sjfv	case IFT_STF:
740209616Sjfv		/*
741209616Sjfv		 * 6to4 interface is a very special kind of beast.
742209616Sjfv		 * no multicast, no linklocal.  RFC2529 specifies how to make
743209616Sjfv		 * linklocals for 6to4 interface, but there's no use and
744209616Sjfv		 * it is rather harmful to have one.
745209616Sjfv		 */
746209616Sjfv		goto statinit;
747209616Sjfv#endif
748209616Sjfv	default:
749209616Sjfv		break;
750209616Sjfv	}
751209616Sjfv
752209616Sjfv	/*
753209616Sjfv	 * usually, we require multicast capability to the interface
754209616Sjfv	 */
755209616Sjfv	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
756209616Sjfv		nd6log((LOG_INFO, "in6_ifattach: "
757209616Sjfv		    "%s is not multicast capable, IPv6 not enabled\n",
758209616Sjfv		    if_name(ifp)));
759218530Sjfv		return;
760218530Sjfv	}
761218530Sjfv
762269196Sjfv	/*
763209616Sjfv	 * assign loopback address for loopback interface.
764209616Sjfv	 * XXX multiple loopback interface case.
765209616Sjfv	 */
766209616Sjfv	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
767209616Sjfv		struct ifaddr *ifa;
768209616Sjfv
769209616Sjfv		in6 = in6addr_loopback;
770209616Sjfv		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &in6);
771209616Sjfv		if (ifa == NULL) {
772209616Sjfv			if (in6_ifattach_loopback(ifp) != 0)
773209616Sjfv				return;
774209616Sjfv		} else
775209616Sjfv			ifa_free(ifa);
776209616Sjfv	}
777209616Sjfv
778209616Sjfv	/*
779209616Sjfv	 * assign a link-local address, if there's none.
780209616Sjfv	 */
781218530Sjfv	if (ifp->if_type != IFT_BRIDGE &&
782218530Sjfv	    !(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
783209616Sjfv	    ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL) {
784209616Sjfv		int error;
785209616Sjfv
786		ia = in6ifa_ifpforlinklocal(ifp, 0);
787		if (ia == NULL) {
788			error = in6_ifattach_linklocal(ifp, altifp);
789#if 0
790			if (error)
791				log(LOG_NOTICE, "in6_ifattach_linklocal: "
792				    "failed to add a link-local addr to %s\n",
793				    if_name(ifp));
794#endif
795		} else
796			ifa_free(&ia->ia_ifa);
797	}
798
799#ifdef IFT_STF			/* XXX */
800statinit:
801#endif
802
803	/* update dynamically. */
804	if (V_in6_maxmtu < ifp->if_mtu)
805		V_in6_maxmtu = ifp->if_mtu;
806}
807
808/*
809 * NOTE: in6_ifdetach() does not support loopback if at this moment.
810 * We don't need this function in bsdi, because interfaces are never removed
811 * from the ifnet list in bsdi.
812 */
813void
814in6_ifdetach(struct ifnet *ifp)
815{
816	struct in6_ifaddr *ia;
817	struct ifaddr *ifa, *next;
818	struct radix_node_head *rnh;
819	struct rtentry *rt;
820	struct sockaddr_in6 sin6;
821	struct in6_multi_mship *imm;
822
823	/* remove neighbor management table */
824	nd6_purge(ifp);
825
826	/* nuke any of IPv6 addresses we have */
827	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
828		if (ifa->ifa_addr->sa_family != AF_INET6)
829			continue;
830		in6_purgeaddr(ifa);
831	}
832
833	/* undo everything done by in6_ifattach(), just in case */
834	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
835		if (ifa->ifa_addr->sa_family != AF_INET6
836		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
837			continue;
838		}
839
840		ia = (struct in6_ifaddr *)ifa;
841
842		/*
843		 * leave from multicast groups we have joined for the interface
844		 */
845		while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
846			LIST_REMOVE(imm, i6mm_chain);
847			in6_leavegroup(imm);
848		}
849
850		/* Remove link-local from the routing table. */
851		if (ia->ia_flags & IFA_ROUTE)
852			(void)rtinit(&ia->ia_ifa, RTM_DELETE, ia->ia_flags);
853
854		/* remove from the linked list */
855		IF_ADDR_WLOCK(ifp);
856		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
857		IF_ADDR_WUNLOCK(ifp);
858		ifa_free(ifa);				/* if_addrhead */
859
860		IN6_IFADDR_WLOCK();
861		TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
862		IN6_IFADDR_WUNLOCK();
863		ifa_free(ifa);
864	}
865
866	in6_pcbpurgeif0(&V_udbinfo, ifp);
867	in6_pcbpurgeif0(&V_ripcbinfo, ifp);
868	/* leave from all multicast groups joined */
869	in6_purgemaddrs(ifp);
870
871	/*
872	 * remove neighbor management table.  we call it twice just to make
873	 * sure we nuke everything.  maybe we need just one call.
874	 * XXX: since the first call did not release addresses, some prefixes
875	 * might remain.  We should call nd6_purge() again to release the
876	 * prefixes after removing all addresses above.
877	 * (Or can we just delay calling nd6_purge until at this point?)
878	 */
879	nd6_purge(ifp);
880
881	/*
882	 * Remove route to link-local allnodes multicast (ff02::1).
883	 * These only get automatically installed for the default FIB.
884	 */
885	bzero(&sin6, sizeof(sin6));
886	sin6.sin6_len = sizeof(struct sockaddr_in6);
887	sin6.sin6_family = AF_INET6;
888	sin6.sin6_addr = in6addr_linklocal_allnodes;
889	if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
890		/* XXX: should not fail */
891		return;
892	/* XXX grab lock first to avoid LOR */
893	rnh = rt_tables_get_rnh(RT_DEFAULT_FIB, AF_INET6);
894	if (rnh != NULL) {
895		RADIX_NODE_HEAD_LOCK(rnh);
896		rt = in6_rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED,
897		    RT_DEFAULT_FIB);
898		if (rt) {
899			if (rt->rt_ifp == ifp)
900				rtexpunge(rt);
901			RTFREE_LOCKED(rt);
902		}
903		RADIX_NODE_HEAD_UNLOCK(rnh);
904	}
905}
906
907int
908in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
909    const u_int8_t *baseid, int generate)
910{
911	u_int8_t nullbuf[8];
912	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
913
914	bzero(nullbuf, sizeof(nullbuf));
915	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
916		/* we've never created a random ID.  Create a new one. */
917		generate = 1;
918	}
919
920	if (generate) {
921		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
922
923		/* generate_tmp_ifid will update seedn and buf */
924		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
925		    ndi->randomid);
926	}
927	bcopy(ndi->randomid, retbuf, 8);
928
929	return (0);
930}
931
932void
933in6_tmpaddrtimer(void *arg)
934{
935	CURVNET_SET((struct vnet *) arg);
936	struct nd_ifinfo *ndi;
937	u_int8_t nullbuf[8];
938	struct ifnet *ifp;
939
940	callout_reset(&V_in6_tmpaddrtimer_ch,
941	    (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
942	    V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, curvnet);
943
944	bzero(nullbuf, sizeof(nullbuf));
945	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
946		ndi = ND_IFINFO(ifp);
947		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
948			/*
949			 * We've been generating a random ID on this interface.
950			 * Create a new one.
951			 */
952			(void)generate_tmp_ifid(ndi->randomseed0,
953			    ndi->randomseed1, ndi->randomid);
954		}
955	}
956
957	CURVNET_RESTORE();
958}
959
960static void
961in6_purgemaddrs(struct ifnet *ifp)
962{
963	LIST_HEAD(,in6_multi)	 purgeinms;
964	struct in6_multi	*inm, *tinm;
965	struct ifmultiaddr	*ifma;
966
967	LIST_INIT(&purgeinms);
968	IN6_MULTI_LOCK();
969
970	/*
971	 * Extract list of in6_multi associated with the detaching ifp
972	 * which the PF_INET6 layer is about to release.
973	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
974	 * by code further down.
975	 */
976	IF_ADDR_RLOCK(ifp);
977	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
978		if (ifma->ifma_addr->sa_family != AF_INET6 ||
979		    ifma->ifma_protospec == NULL)
980			continue;
981		inm = (struct in6_multi *)ifma->ifma_protospec;
982		LIST_INSERT_HEAD(&purgeinms, inm, in6m_entry);
983	}
984	IF_ADDR_RUNLOCK(ifp);
985
986	LIST_FOREACH_SAFE(inm, &purgeinms, in6m_entry, tinm) {
987		LIST_REMOVE(inm, in6m_entry);
988		in6m_release_locked(inm);
989	}
990	mld_ifdetach(ifp);
991
992	IN6_MULTI_UNLOCK();
993}
994