in6_ifattach.c revision 121770
1/*	$FreeBSD: head/sys/netinet6/in6_ifattach.c 121770 2003-10-30 23:02:51Z sam $	*/
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
63unsigned long in6_maxmtu = 0;
64
65#ifdef IP6_AUTO_LINKLOCAL
66int ip6_auto_linklocal = IP6_AUTO_LINKLOCAL;
67#else
68int ip6_auto_linklocal = 1;	/* enable by default */
69#endif
70
71struct callout in6_tmpaddrtimer_ch;
72
73extern struct inpcbinfo udbinfo;
74extern struct inpcbinfo ripcbinfo;
75
76static int get_rand_ifid __P((struct ifnet *, struct in6_addr *));
77static int generate_tmp_ifid __P((u_int8_t *, const u_int8_t *, u_int8_t *));
78static int get_hw_ifid __P((struct ifnet *, struct in6_addr *));
79static int get_ifid __P((struct ifnet *, struct ifnet *, struct in6_addr *));
80static int in6_ifattach_linklocal __P((struct ifnet *, struct ifnet *));
81static int in6_ifattach_loopback __P((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 */
101static int
102get_rand_ifid(ifp, in6)
103	struct ifnet *ifp;
104	struct in6_addr *in6;	/* upper 64bits are preserved */
105{
106	MD5_CTX ctxt;
107	u_int8_t digest[16];
108	int hostnamelen	= strlen(hostname);
109
110#if 0
111	/* we need at least several letters as seed for ifid */
112	if (hostnamelen < 3)
113		return -1;
114#endif
115
116	/* generate 8 bytes of pseudo-random value. */
117	bzero(&ctxt, sizeof(ctxt));
118	MD5Init(&ctxt);
119	MD5Update(&ctxt, hostname, hostnamelen);
120	MD5Final(digest, &ctxt);
121
122	/* assumes sizeof(digest) > sizeof(ifid) */
123	bcopy(digest, &in6->s6_addr[8], 8);
124
125	/* make sure to set "u" bit to local, and "g" bit to individual. */
126	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
127	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
128
129	/* convert EUI64 into IPv6 interface identifier */
130	EUI64_TO_IFID(in6);
131
132	return 0;
133}
134
135static int
136generate_tmp_ifid(seed0, seed1, ret)
137	u_int8_t *seed0, *ret;
138	const u_int8_t *seed1;
139{
140	MD5_CTX ctxt;
141	u_int8_t seed[16], digest[16], nullbuf[8];
142	u_int32_t val32;
143	struct timeval tv;
144
145	/* If there's no hisotry, start with a random seed. */
146	bzero(nullbuf, sizeof(nullbuf));
147	if (bcmp(nullbuf, seed0, sizeof(nullbuf)) == 0) {
148		int i;
149
150		for (i = 0; i < 2; i++) {
151			microtime(&tv);
152			val32 = random() ^ tv.tv_usec;
153			bcopy(&val32, seed + sizeof(val32) * i, sizeof(val32));
154		}
155	} else
156		bcopy(seed0, seed, 8);
157
158	/* copy the right-most 64-bits of the given address */
159	/* XXX assumption on the size of IFID */
160	bcopy(seed1, &seed[8], 8);
161
162	if (0) {		/* for debugging purposes only */
163		int i;
164
165		printf("generate_tmp_ifid: new randomized ID from: ");
166		for (i = 0; i < 16; i++)
167			printf("%02x", seed[i]);
168		printf(" ");
169	}
170
171	/* generate 16 bytes of pseudo-random value. */
172	bzero(&ctxt, sizeof(ctxt));
173	MD5Init(&ctxt);
174	MD5Update(&ctxt, seed, sizeof(seed));
175	MD5Final(digest, &ctxt);
176
177	/*
178	 * RFC 3041 3.2.1. (3)
179	 * Take the left-most 64-bits of the MD5 digest and set bit 6 (the
180	 * left-most bit is numbered 0) to zero.
181	 */
182	bcopy(digest, ret, 8);
183	ret[0] &= ~EUI64_UBIT;
184
185	/*
186	 * XXX: we'd like to ensure that the generated value is not zero
187	 * for simplicity.  If the caclculated digest happens to be zero,
188	 * use a random non-zero value as the last resort.
189	 */
190	if (bcmp(nullbuf, ret, sizeof(nullbuf)) == 0) {
191		log(LOG_INFO,
192		    "generate_tmp_ifid: computed MD5 value is zero.\n");
193
194		microtime(&tv);
195		val32 = random() ^ tv.tv_usec;
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 */
223static int
224get_hw_ifid(ifp, in6)
225	struct ifnet *ifp;
226	struct in6_addr *in6;	/* upper 64bits are preserved */
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 */
356static int
357get_ifid(ifp0, altifp, in6)
358	struct ifnet *ifp0;
359	struct ifnet *altifp;	/* secondary EUI64 source */
360	struct in6_addr *in6;
361{
362	struct ifnet *ifp;
363
364	/* first, try to get it from the interface itself */
365	if (get_hw_ifid(ifp0, in6) == 0) {
366		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
367		    if_name(ifp0)));
368		goto success;
369	}
370
371	/* try secondary EUI64 source. this basically is for ATM PVC */
372	if (altifp && get_hw_ifid(altifp, in6) == 0) {
373		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
374		    if_name(ifp0), if_name(altifp)));
375		goto success;
376	}
377
378	/* next, try to get it from some other hardware interface */
379	IFNET_RLOCK();
380	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
381		if (ifp == ifp0)
382			continue;
383		if (get_hw_ifid(ifp, in6) != 0)
384			continue;
385
386		/*
387		 * to borrow ifid from other interface, ifid needs to be
388		 * globally unique
389		 */
390		if (IFID_UNIVERSAL(in6)) {
391			nd6log((LOG_DEBUG,
392			    "%s: borrow interface identifier from %s\n",
393			    if_name(ifp0), if_name(ifp)));
394			IFNET_RUNLOCK();
395			goto success;
396		}
397	}
398	IFNET_RUNLOCK();
399
400	/* last resort: get from random number source */
401	if (get_rand_ifid(ifp, in6) == 0) {
402		nd6log((LOG_DEBUG,
403		    "%s: interface identifier generated by random number\n",
404		    if_name(ifp0)));
405		goto success;
406	}
407
408	printf("%s: failed to get interface identifier\n", if_name(ifp0));
409	return -1;
410
411success:
412	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
413	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
414	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
415	    in6->s6_addr[14], in6->s6_addr[15]));
416	return 0;
417}
418
419static int
420in6_ifattach_linklocal(ifp, altifp)
421	struct ifnet *ifp;
422	struct ifnet *altifp;	/* secondary EUI64 source */
423{
424	struct in6_ifaddr *ia;
425	struct in6_aliasreq ifra;
426	struct nd_prefix pr0;
427	int i, error;
428
429	/*
430	 * configure link-local address.
431	 */
432	bzero(&ifra, sizeof(ifra));
433
434	/*
435	 * in6_update_ifa() does not use ifra_name, but we accurately set it
436	 * for safety.
437	 */
438	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
439
440	ifra.ifra_addr.sin6_family = AF_INET6;
441	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
442	ifra.ifra_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
443	ifra.ifra_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index); /* XXX */
444	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
445	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
446		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
447		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
448	} else {
449		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
450			nd6log((LOG_ERR,
451			    "%s: no ifid available\n", if_name(ifp)));
452			return (-1);
453		}
454	}
455
456	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
457	ifra.ifra_prefixmask.sin6_family = AF_INET6;
458	ifra.ifra_prefixmask.sin6_addr = in6mask64;
459	/* link-local addresses should NEVER expire. */
460	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
461	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
462
463	/*
464	 * Do not let in6_update_ifa() do DAD, since we need a random delay
465	 * before sending an NS at the first time the interface becomes up.
466	 * Instead, in6_if_up() will start DAD with a proper random delay.
467	 */
468	ifra.ifra_flags |= IN6_IFF_NODAD;
469
470	/*
471	 * Now call in6_update_ifa() to do a bunch of procedures to configure
472	 * a link-local address. We can set NULL to the 3rd argument, because
473	 * we know there's no other link-local address on the interface
474	 * and therefore we are adding one (instead of updating one).
475	 */
476	if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
477		/*
478		 * XXX: When the interface does not support IPv6, this call
479		 * would fail in the SIOCSIFADDR ioctl.  I believe the
480		 * notification is rather confusing in this case, so just
481		 * suppress it.  (jinmei@kame.net 20010130)
482		 */
483		if (error != EAFNOSUPPORT)
484			log(LOG_NOTICE, "in6_ifattach_linklocal: failed to "
485			    "configure a link-local address on %s "
486			    "(errno=%d)\n",
487			    if_name(ifp), error);
488		return (-1);
489	}
490
491	/*
492	 * Adjust ia6_flags so that in6_if_up will perform DAD.
493	 * XXX: Some P2P interfaces seem not to send packets just after
494	 * becoming up, so we skip p2p interfaces for safety.
495	 */
496	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
497#ifdef DIAGNOSTIC
498	if (!ia) {
499		panic("ia == NULL in in6_ifattach_linklocal");
500		/* NOTREACHED */
501	}
502#endif
503	if (in6if_do_dad(ifp) && (ifp->if_flags & IFF_POINTOPOINT) == 0) {
504		ia->ia6_flags &= ~IN6_IFF_NODAD;
505		ia->ia6_flags |= IN6_IFF_TENTATIVE;
506	}
507
508	/*
509	 * Make the link-local prefix (fe80::%link/64) as on-link.
510	 * Since we'd like to manage prefixes separately from addresses,
511	 * we make an ND6 prefix structure for the link-local prefix,
512	 * and add it to the prefix list as a never-expire prefix.
513	 * XXX: this change might affect some existing code base...
514	 */
515	bzero(&pr0, sizeof(pr0));
516	pr0.ndpr_ifp = ifp;
517	/* this should be 64 at this moment. */
518	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
519	pr0.ndpr_mask = ifra.ifra_prefixmask.sin6_addr;
520	pr0.ndpr_prefix = ifra.ifra_addr;
521	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
522	for (i = 0; i < 4; i++) {
523		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
524		    in6mask64.s6_addr32[i];
525	}
526	/*
527	 * Initialize parameters.  The link-local prefix must always be
528	 * on-link, and its lifetimes never expire.
529	 */
530	pr0.ndpr_raf_onlink = 1;
531	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
532	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
533	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
534	/*
535	 * Since there is no other link-local addresses, nd6_prefix_lookup()
536	 * probably returns NULL.  However, we cannot always expect the result.
537	 * For example, if we first remove the (only) existing link-local
538	 * address, and then reconfigure another one, the prefix is still
539	 * valid with referring to the old link-local address.
540	 */
541	if (nd6_prefix_lookup(&pr0) == NULL) {
542		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
543			return (error);
544	}
545
546	return 0;
547}
548
549static int
550in6_ifattach_loopback(ifp)
551	struct ifnet *ifp;	/* must be IFT_LOOP */
552{
553	struct in6_aliasreq ifra;
554	int error;
555
556	bzero(&ifra, sizeof(ifra));
557
558	/*
559	 * in6_update_ifa() does not use ifra_name, but we accurately set it
560	 * for safety.
561	 */
562	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
563
564	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
565	ifra.ifra_prefixmask.sin6_family = AF_INET6;
566	ifra.ifra_prefixmask.sin6_addr = in6mask128;
567
568	/*
569	 * Always initialize ia_dstaddr (= broadcast address) to loopback
570	 * address.  Follows IPv4 practice - see in_ifinit().
571	 */
572	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
573	ifra.ifra_dstaddr.sin6_family = AF_INET6;
574	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
575
576	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
577	ifra.ifra_addr.sin6_family = AF_INET6;
578	ifra.ifra_addr.sin6_addr = in6addr_loopback;
579
580	/* the loopback  address should NEVER expire. */
581	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
582	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
583
584	/* we don't need to perform DAD on loopback interfaces. */
585	ifra.ifra_flags |= IN6_IFF_NODAD;
586
587	/* skip registration to the prefix list. XXX should be temporary. */
588	ifra.ifra_flags |= IN6_IFF_NOPFX;
589
590	/*
591	 * We are sure that this is a newly assigned address, so we can set
592	 * NULL to the 3rd arg.
593	 */
594	if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) {
595		log(LOG_ERR, "in6_ifattach_loopback: failed to configure "
596		    "the loopback address on %s (errno=%d)\n",
597		    if_name(ifp), error);
598		return (-1);
599	}
600
601	return 0;
602}
603
604/*
605 * compute NI group address, based on the current hostname setting.
606 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
607 *
608 * when ifp == NULL, the caller is responsible for filling scopeid.
609 */
610int
611in6_nigroup(ifp, name, namelen, in6)
612	struct ifnet *ifp;
613	const char *name;
614	int namelen;
615	struct in6_addr *in6;
616{
617	const char *p;
618	u_char *q;
619	MD5_CTX ctxt;
620	u_int8_t digest[16];
621	char l;
622	char n[64];	/* a single label must not exceed 63 chars */
623
624	if (!namelen || !name)
625		return -1;
626
627	p = name;
628	while (p && *p && *p != '.' && p - name < namelen)
629		p++;
630	if (p - name > sizeof(n) - 1)
631		return -1;	/* label too long */
632	l = p - name;
633	strncpy(n, name, l);
634	n[(int)l] = '\0';
635	for (q = n; *q; q++) {
636		if ('A' <= *q && *q <= 'Z')
637			*q = *q - 'A' + 'a';
638	}
639
640	/* generate 8 bytes of pseudo-random value. */
641	bzero(&ctxt, sizeof(ctxt));
642	MD5Init(&ctxt);
643	MD5Update(&ctxt, &l, sizeof(l));
644	MD5Update(&ctxt, n, l);
645	MD5Final(digest, &ctxt);
646
647	bzero(in6, sizeof(*in6));
648	in6->s6_addr16[0] = htons(0xff02);
649	if (ifp)
650		in6->s6_addr16[1] = htons(ifp->if_index);
651	in6->s6_addr8[11] = 2;
652	bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
653
654	return 0;
655}
656
657void
658in6_nigroup_attach(name, namelen)
659	const char *name;
660	int namelen;
661{
662	struct ifnet *ifp;
663	struct sockaddr_in6 mltaddr;
664	struct in6_multi *in6m;
665	int error;
666
667	bzero(&mltaddr, sizeof(mltaddr));
668	mltaddr.sin6_family = AF_INET6;
669	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
670	if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
671		return;
672
673	IFNET_RLOCK();
674	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
675	{
676		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
677		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
678		if (!in6m) {
679			if (!in6_addmulti(&mltaddr.sin6_addr, ifp, &error)) {
680				nd6log((LOG_ERR, "%s: failed to join %s "
681				    "(errno=%d)\n", if_name(ifp),
682				    ip6_sprintf(&mltaddr.sin6_addr),
683				    error));
684			}
685		}
686	}
687	IFNET_RUNLOCK();
688}
689
690void
691in6_nigroup_detach(name, namelen)
692	const char *name;
693	int namelen;
694{
695	struct ifnet *ifp;
696	struct sockaddr_in6 mltaddr;
697	struct in6_multi *in6m;
698
699	bzero(&mltaddr, sizeof(mltaddr));
700	mltaddr.sin6_family = AF_INET6;
701	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
702	if (in6_nigroup(NULL, name, namelen, &mltaddr.sin6_addr) != 0)
703		return;
704
705	IFNET_RLOCK();
706	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
707	{
708		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
709		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
710		if (in6m)
711			in6_delmulti(in6m);
712	}
713	IFNET_RUNLOCK();
714}
715
716/*
717 * XXX multiple loopback interface needs more care.  for instance,
718 * nodelocal address needs to be configured onto only one of them.
719 * XXX multiple link-local address case
720 */
721void
722in6_ifattach(ifp, altifp)
723	struct ifnet *ifp;
724	struct ifnet *altifp;	/* secondary EUI64 source */
725{
726	struct in6_ifaddr *ia;
727	struct in6_addr in6;
728
729	/* some of the interfaces are inherently not IPv6 capable */
730	switch (ifp->if_type) {
731#ifdef IFT_BRIDGE	/* OpenBSD 2.8, NetBSD 1.6 */
732	case IFT_BRIDGE:
733		return;
734#endif
735	}
736
737	/*
738	 * quirks based on interface type
739	 */
740	switch (ifp->if_type) {
741#ifdef IFT_STF
742	case IFT_STF:
743		/*
744		 * 6to4 interface is a very special kind of beast.
745		 * no multicast, no linklocal.  RFC2529 specifies how to make
746		 * linklocals for 6to4 interface, but there's no use and
747		 * it is rather harmful to have one.
748		 */
749		goto statinit;
750#endif
751	default:
752		break;
753	}
754
755	/*
756	 * usually, we require multicast capability to the interface
757	 */
758	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
759		log(LOG_INFO, "in6_ifattach: "
760		    "%s is not multicast capable, IPv6 not enabled\n",
761		    if_name(ifp));
762		return;
763	}
764
765	/*
766	 * assign loopback address for loopback interface.
767	 * XXX multiple loopback interface case.
768	 */
769	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
770		in6 = in6addr_loopback;
771		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
772			if (in6_ifattach_loopback(ifp) != 0)
773				return;
774		}
775	}
776
777	/*
778	 * assign a link-local address, if there's none.
779	 */
780	if (ip6_auto_linklocal) {
781		ia = in6ifa_ifpforlinklocal(ifp, 0);
782		if (ia == NULL) {
783			if (in6_ifattach_linklocal(ifp, altifp) == 0) {
784				/* linklocal address assigned */
785			} else {
786				/* failed to assign linklocal address. bark? */
787			}
788		}
789	}
790
791#ifdef IFT_STF			/* XXX */
792statinit:
793#endif
794
795	/* update dynamically. */
796	if (in6_maxmtu < ifp->if_mtu)
797		in6_maxmtu = ifp->if_mtu;
798}
799
800/*
801 * NOTE: in6_ifdetach() does not support loopback if at this moment.
802 * We don't need this function in bsdi, because interfaces are never removed
803 * from the ifnet list in bsdi.
804 */
805void
806in6_ifdetach(ifp)
807	struct ifnet *ifp;
808{
809	struct in6_ifaddr *ia, *oia;
810	struct ifaddr *ifa, *next;
811	struct rtentry *rt;
812	short rtflags;
813	struct sockaddr_in6 sin6;
814	struct in6_multi *in6m;
815	struct in6_multi *in6m_next;
816
817	/* nuke prefix list.  this may try to remove some of ifaddrs as well */
818	in6_purgeprefix(ifp);
819
820	/* remove neighbor management table */
821	nd6_purge(ifp);
822
823	/* nuke any of IPv6 addresses we have */
824	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
825		next = ifa->ifa_list.tqe_next;
826		if (ifa->ifa_addr->sa_family != AF_INET6)
827			continue;
828		in6_purgeaddr(ifa);
829	}
830
831	/* undo everything done by in6_ifattach(), just in case */
832	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next) {
833		next = ifa->ifa_list.tqe_next;
834
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		/* remove from the routing table */
843		if ((ia->ia_flags & IFA_ROUTE) &&
844		    (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
845			rtflags = rt->rt_flags;
846			rtfree(rt);
847			rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
848			    (struct sockaddr *)&ia->ia_addr,
849			    (struct sockaddr *)&ia->ia_prefixmask,
850			    rtflags, (struct rtentry **)0);
851		}
852
853		/* remove from the linked list */
854		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
855		IFAFREE(&ia->ia_ifa);
856
857		/* also remove from the IPv6 address chain(itojun&jinmei) */
858		oia = ia;
859		if (oia == (ia = in6_ifaddr))
860			in6_ifaddr = ia->ia_next;
861		else {
862			while (ia->ia_next && (ia->ia_next != oia))
863				ia = ia->ia_next;
864			if (ia->ia_next)
865				ia->ia_next = oia->ia_next;
866			else {
867				nd6log((LOG_ERR,
868				    "%s: didn't unlink in6ifaddr from list\n",
869				    if_name(ifp)));
870			}
871		}
872
873		IFAFREE(&oia->ia_ifa);
874	}
875
876	/* leave from all multicast groups joined */
877
878	if (udbinfo.listhead != NULL)
879		in6_pcbpurgeif0(LIST_FIRST(udbinfo.listhead), ifp);
880	if (ripcbinfo.listhead != NULL)
881		in6_pcbpurgeif0(LIST_FIRST(ripcbinfo.listhead), ifp);
882
883	for (in6m = LIST_FIRST(&in6_multihead); in6m; in6m = in6m_next) {
884		in6m_next = LIST_NEXT(in6m, in6m_entry);
885		if (in6m->in6m_ifp != ifp)
886			continue;
887		in6_delmulti(in6m);
888		in6m = NULL;
889	}
890
891	/*
892	 * remove neighbor management table.  we call it twice just to make
893	 * sure we nuke everything.  maybe we need just one call.
894	 * XXX: since the first call did not release addresses, some prefixes
895	 * might remain.  We should call nd6_purge() again to release the
896	 * prefixes after removing all addresses above.
897	 * (Or can we just delay calling nd6_purge until at this point?)
898	 */
899	nd6_purge(ifp);
900
901	/* remove route to link-local allnodes multicast (ff02::1) */
902	bzero(&sin6, sizeof(sin6));
903	sin6.sin6_len = sizeof(struct sockaddr_in6);
904	sin6.sin6_family = AF_INET6;
905	sin6.sin6_addr = in6addr_linklocal_allnodes;
906	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
907	/* XXX grab lock first to avoid LOR */
908	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET6]);
909	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
910	if (rt) {
911		if (rt->rt_ifp == ifp)
912			rtexpunge(rt);
913		RTFREE_LOCKED(rt);
914	}
915	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET6]);
916}
917
918void
919in6_get_tmpifid(ifp, retbuf, baseid, generate)
920	struct ifnet *ifp;
921	u_int8_t *retbuf;
922	const u_int8_t *baseid;
923	int generate;
924{
925	u_int8_t nullbuf[8];
926	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
927
928	bzero(nullbuf, sizeof(nullbuf));
929	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
930		/* we've never created a random ID.  Create a new one. */
931		generate = 1;
932	}
933
934	if (generate) {
935		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
936
937		/* generate_tmp_ifid will update seedn and buf */
938		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
939		    ndi->randomid);
940	}
941	bcopy(ndi->randomid, retbuf, 8);
942}
943
944void
945in6_tmpaddrtimer(ignored_arg)
946	void *ignored_arg;
947{
948	struct nd_ifinfo *ndi;
949	u_int8_t nullbuf[8];
950	struct ifnet *ifp;
951	int s = splnet();
952
953	callout_reset(&in6_tmpaddrtimer_ch,
954	    (ip6_temp_preferred_lifetime - ip6_desync_factor -
955	    ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
956
957	bzero(nullbuf, sizeof(nullbuf));
958	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
959		ndi = ND_IFINFO(ifp);
960		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
961			/*
962			 * We've been generating a random ID on this interface.
963			 * Create a new one.
964			 */
965			(void)generate_tmp_ifid(ndi->randomseed0,
966			    ndi->randomseed1, ndi->randomid);
967		}
968	}
969
970	splx(s);
971}
972