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