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