in6_ifattach.c revision 191340
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 191340 2009-04-20 22:45:21Z rwatson $");
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	IF_ADDR_LOCK(ifp);
237	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
238		if (ifa->ifa_addr->sa_family != AF_LINK)
239			continue;
240		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
241		if (sdl == NULL)
242			continue;
243		if (sdl->sdl_alen == 0)
244			continue;
245
246		goto found;
247	}
248	IF_ADDR_UNLOCK(ifp);
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			IF_ADDR_UNLOCK(ifp);
274			return -1;
275		}
276
277		/*
278		 * check for invalid MAC address - on bsdi, we see it a lot
279		 * since wildboar configures all-zero MAC on pccard before
280		 * card insertion.
281		 */
282		if (bcmp(addr, allzero, addrlen) == 0) {
283			IF_ADDR_UNLOCK(ifp);
284			return -1;
285		}
286		if (bcmp(addr, allone, addrlen) == 0) {
287			IF_ADDR_UNLOCK(ifp);
288			return -1;
289		}
290
291		/* make EUI64 address */
292		if (addrlen == 8)
293			bcopy(addr, &in6->s6_addr[8], 8);
294		else if (addrlen == 6) {
295			in6->s6_addr[8] = addr[0];
296			in6->s6_addr[9] = addr[1];
297			in6->s6_addr[10] = addr[2];
298			in6->s6_addr[11] = 0xff;
299			in6->s6_addr[12] = 0xfe;
300			in6->s6_addr[13] = addr[3];
301			in6->s6_addr[14] = addr[4];
302			in6->s6_addr[15] = addr[5];
303		}
304		break;
305
306	case IFT_ARCNET:
307		if (addrlen != 1) {
308			IF_ADDR_UNLOCK(ifp);
309			return -1;
310		}
311		if (!addr[0]) {
312			IF_ADDR_UNLOCK(ifp);
313			return -1;
314		}
315
316		bzero(&in6->s6_addr[8], 8);
317		in6->s6_addr[15] = addr[0];
318
319		/*
320		 * due to insufficient bitwidth, we mark it local.
321		 */
322		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
323		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
324		break;
325
326	case IFT_GIF:
327#ifdef IFT_STF
328	case IFT_STF:
329#endif
330		/*
331		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
332		 * however, IPv4 address is not very suitable as unique
333		 * identifier source (can be renumbered).
334		 * we don't do this.
335		 */
336		IF_ADDR_UNLOCK(ifp);
337		return -1;
338
339	default:
340		IF_ADDR_UNLOCK(ifp);
341		return -1;
342	}
343
344	/* sanity check: g bit must not indicate "group" */
345	if (EUI64_GROUP(in6)) {
346		IF_ADDR_UNLOCK(ifp);
347		return -1;
348	}
349
350	/* convert EUI64 into IPv6 interface identifier */
351	EUI64_TO_IFID(in6);
352
353	/*
354	 * sanity check: ifid must not be all zero, avoid conflict with
355	 * subnet router anycast
356	 */
357	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
358	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
359		IF_ADDR_UNLOCK(ifp);
360		return -1;
361	}
362
363	IF_ADDR_UNLOCK(ifp);
364	return 0;
365}
366
367/*
368 * Get interface identifier for the specified interface.  If it is not
369 * available on ifp0, borrow interface identifier from other information
370 * sources.
371 *
372 * altifp - secondary EUI64 source
373 */
374static int
375get_ifid(struct ifnet *ifp0, struct ifnet *altifp,
376    struct in6_addr *in6)
377{
378	INIT_VNET_NET(ifp0->if_vnet);
379	INIT_VNET_INET6(ifp0->if_vnet);
380	struct ifnet *ifp;
381
382	/* first, try to get it from the interface itself */
383	if (in6_get_hw_ifid(ifp0, in6) == 0) {
384		nd6log((LOG_DEBUG, "%s: got interface identifier from itself\n",
385		    if_name(ifp0)));
386		goto success;
387	}
388
389	/* try secondary EUI64 source. this basically is for ATM PVC */
390	if (altifp && in6_get_hw_ifid(altifp, in6) == 0) {
391		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
392		    if_name(ifp0), if_name(altifp)));
393		goto success;
394	}
395
396	/* next, try to get it from some other hardware interface */
397	IFNET_RLOCK();
398	for (ifp = V_ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
399		if (ifp == ifp0)
400			continue;
401		if (in6_get_hw_ifid(ifp, in6) != 0)
402			continue;
403
404		/*
405		 * to borrow ifid from other interface, ifid needs to be
406		 * globally unique
407		 */
408		if (IFID_UNIVERSAL(in6)) {
409			nd6log((LOG_DEBUG,
410			    "%s: borrow interface identifier from %s\n",
411			    if_name(ifp0), if_name(ifp)));
412			IFNET_RUNLOCK();
413			goto success;
414		}
415	}
416	IFNET_RUNLOCK();
417
418	/* last resort: get from random number source */
419	if (get_rand_ifid(ifp, in6) == 0) {
420		nd6log((LOG_DEBUG,
421		    "%s: interface identifier generated by random number\n",
422		    if_name(ifp0)));
423		goto success;
424	}
425
426	printf("%s: failed to get interface identifier\n", if_name(ifp0));
427	return -1;
428
429success:
430	nd6log((LOG_INFO, "%s: ifid: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
431	    if_name(ifp0), in6->s6_addr[8], in6->s6_addr[9], in6->s6_addr[10],
432	    in6->s6_addr[11], in6->s6_addr[12], in6->s6_addr[13],
433	    in6->s6_addr[14], in6->s6_addr[15]));
434	return 0;
435}
436
437/*
438 * altifp - secondary EUI64 source
439 */
440static int
441in6_ifattach_linklocal(struct ifnet *ifp, struct ifnet *altifp)
442{
443	INIT_VNET_INET6(curvnet);
444	struct in6_ifaddr *ia;
445	struct in6_aliasreq ifra;
446	struct nd_prefixctl pr0;
447	int i, error;
448
449	/*
450	 * configure link-local address.
451	 */
452	bzero(&ifra, sizeof(ifra));
453
454	/*
455	 * in6_update_ifa() does not use ifra_name, but we accurately set it
456	 * for safety.
457	 */
458	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
459
460	ifra.ifra_addr.sin6_family = AF_INET6;
461	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
462	ifra.ifra_addr.sin6_addr.s6_addr32[0] = htonl(0xfe800000);
463	ifra.ifra_addr.sin6_addr.s6_addr32[1] = 0;
464	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
465		ifra.ifra_addr.sin6_addr.s6_addr32[2] = 0;
466		ifra.ifra_addr.sin6_addr.s6_addr32[3] = htonl(1);
467	} else {
468		if (get_ifid(ifp, altifp, &ifra.ifra_addr.sin6_addr) != 0) {
469			nd6log((LOG_ERR,
470			    "%s: no ifid available\n", if_name(ifp)));
471			return (-1);
472		}
473	}
474	if (in6_setscope(&ifra.ifra_addr.sin6_addr, ifp, NULL))
475		return (-1);
476
477	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
478	ifra.ifra_prefixmask.sin6_family = AF_INET6;
479	ifra.ifra_prefixmask.sin6_addr = in6mask64;
480	/* link-local addresses should NEVER expire. */
481	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
482	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
483
484	/*
485	 * Now call in6_update_ifa() to do a bunch of procedures to configure
486	 * a link-local address. We can set the 3rd argument to NULL, because
487	 * we know there's no other link-local address on the interface
488	 * and therefore we are adding one (instead of updating one).
489	 */
490	if ((error = in6_update_ifa(ifp, &ifra, NULL,
491				    IN6_IFAUPDATE_DADDELAY)) != 0) {
492		/*
493		 * XXX: When the interface does not support IPv6, this call
494		 * would fail in the SIOCSIFADDR ioctl.  I believe the
495		 * notification is rather confusing in this case, so just
496		 * suppress it.  (jinmei@kame.net 20010130)
497		 */
498		if (error != EAFNOSUPPORT)
499			nd6log((LOG_NOTICE, "in6_ifattach_linklocal: failed to "
500			    "configure a link-local address on %s "
501			    "(errno=%d)\n",
502			    if_name(ifp), error));
503		return (-1);
504	}
505
506	ia = in6ifa_ifpforlinklocal(ifp, 0); /* ia must not be NULL */
507#ifdef DIAGNOSTIC
508	if (!ia) {
509		panic("ia == NULL in in6_ifattach_linklocal");
510		/* NOTREACHED */
511	}
512#endif
513
514	/*
515	 * Make the link-local prefix (fe80::%link/64) as on-link.
516	 * Since we'd like to manage prefixes separately from addresses,
517	 * we make an ND6 prefix structure for the link-local prefix,
518	 * and add it to the prefix list as a never-expire prefix.
519	 * XXX: this change might affect some existing code base...
520	 */
521	bzero(&pr0, sizeof(pr0));
522	pr0.ndpr_ifp = ifp;
523	/* this should be 64 at this moment. */
524	pr0.ndpr_plen = in6_mask2len(&ifra.ifra_prefixmask.sin6_addr, NULL);
525	pr0.ndpr_prefix = ifra.ifra_addr;
526	/* apply the mask for safety. (nd6_prelist_add will apply it again) */
527	for (i = 0; i < 4; i++) {
528		pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
529		    in6mask64.s6_addr32[i];
530	}
531	/*
532	 * Initialize parameters.  The link-local prefix must always be
533	 * on-link, and its lifetimes never expire.
534	 */
535	pr0.ndpr_raf_onlink = 1;
536	pr0.ndpr_raf_auto = 1;	/* probably meaningless */
537	pr0.ndpr_vltime = ND6_INFINITE_LIFETIME;
538	pr0.ndpr_pltime = ND6_INFINITE_LIFETIME;
539	/*
540	 * Since there is no other link-local addresses, nd6_prefix_lookup()
541	 * probably returns NULL.  However, we cannot always expect the result.
542	 * For example, if we first remove the (only) existing link-local
543	 * address, and then reconfigure another one, the prefix is still
544	 * valid with referring to the old link-local address.
545	 */
546	if (nd6_prefix_lookup(&pr0) == NULL) {
547		if ((error = nd6_prelist_add(&pr0, NULL, NULL)) != 0)
548			return (error);
549	}
550
551	return 0;
552}
553
554/*
555 * ifp - must be IFT_LOOP
556 */
557static int
558in6_ifattach_loopback(struct ifnet *ifp)
559{
560	INIT_VNET_INET6(curvnet);
561	struct in6_aliasreq ifra;
562	int error;
563
564	bzero(&ifra, sizeof(ifra));
565
566	/*
567	 * in6_update_ifa() does not use ifra_name, but we accurately set it
568	 * for safety.
569	 */
570	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
571
572	ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
573	ifra.ifra_prefixmask.sin6_family = AF_INET6;
574	ifra.ifra_prefixmask.sin6_addr = in6mask128;
575
576	/*
577	 * Always initialize ia_dstaddr (= broadcast address) to loopback
578	 * address.  Follows IPv4 practice - see in_ifinit().
579	 */
580	ifra.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
581	ifra.ifra_dstaddr.sin6_family = AF_INET6;
582	ifra.ifra_dstaddr.sin6_addr = in6addr_loopback;
583
584	ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
585	ifra.ifra_addr.sin6_family = AF_INET6;
586	ifra.ifra_addr.sin6_addr = in6addr_loopback;
587
588	/* the loopback  address should NEVER expire. */
589	ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
590	ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
591
592	/* we don't need to perform DAD on loopback interfaces. */
593	ifra.ifra_flags |= IN6_IFF_NODAD;
594
595	/* skip registration to the prefix list. XXX should be temporary. */
596	ifra.ifra_flags |= IN6_IFF_NOPFX;
597
598	/*
599	 * We are sure that this is a newly assigned address, so we can set
600	 * NULL to the 3rd arg.
601	 */
602	if ((error = in6_update_ifa(ifp, &ifra, NULL, 0)) != 0) {
603		nd6log((LOG_ERR, "in6_ifattach_loopback: failed to configure "
604		    "the loopback address on %s (errno=%d)\n",
605		    if_name(ifp), error));
606		return (-1);
607	}
608
609	return 0;
610}
611
612/*
613 * compute NI group address, based on the current hostname setting.
614 * see draft-ietf-ipngwg-icmp-name-lookup-* (04 and later).
615 *
616 * when ifp == NULL, the caller is responsible for filling scopeid.
617 */
618int
619in6_nigroup(struct ifnet *ifp, const char *name, int namelen,
620    struct in6_addr *in6)
621{
622	const char *p;
623	u_char *q;
624	MD5_CTX ctxt;
625	u_int8_t digest[16];
626	char l;
627	char n[64];	/* a single label must not exceed 63 chars */
628
629	if (!namelen || !name)
630		return -1;
631
632	p = name;
633	while (p && *p && *p != '.' && p - name < namelen)
634		p++;
635	if (p - name > sizeof(n) - 1)
636		return -1;	/* label too long */
637	l = p - name;
638	strncpy(n, name, l);
639	n[(int)l] = '\0';
640	for (q = n; *q; q++) {
641		if ('A' <= *q && *q <= 'Z')
642			*q = *q - 'A' + 'a';
643	}
644
645	/* generate 8 bytes of pseudo-random value. */
646	bzero(&ctxt, sizeof(ctxt));
647	MD5Init(&ctxt);
648	MD5Update(&ctxt, &l, sizeof(l));
649	MD5Update(&ctxt, n, l);
650	MD5Final(digest, &ctxt);
651
652	bzero(in6, sizeof(*in6));
653	in6->s6_addr16[0] = IPV6_ADDR_INT16_MLL;
654	in6->s6_addr8[11] = 2;
655	bcopy(digest, &in6->s6_addr32[3], sizeof(in6->s6_addr32[3]));
656	if (in6_setscope(in6, ifp, NULL))
657		return (-1); /* XXX: should not fail */
658
659	return 0;
660}
661
662/*
663 * XXX multiple loopback interface needs more care.  for instance,
664 * nodelocal address needs to be configured onto only one of them.
665 * XXX multiple link-local address case
666 *
667 * altifp - secondary EUI64 source
668 */
669void
670in6_ifattach(struct ifnet *ifp, struct ifnet *altifp)
671{
672	INIT_VNET_INET6(ifp->if_vnet);
673	struct in6_ifaddr *ia;
674	struct in6_addr in6;
675
676	/* some of the interfaces are inherently not IPv6 capable */
677	switch (ifp->if_type) {
678	case IFT_PFLOG:
679	case IFT_PFSYNC:
680	case IFT_CARP:
681		return;
682	}
683
684	/*
685	 * quirks based on interface type
686	 */
687	switch (ifp->if_type) {
688#ifdef IFT_STF
689	case IFT_STF:
690		/*
691		 * 6to4 interface is a very special kind of beast.
692		 * no multicast, no linklocal.  RFC2529 specifies how to make
693		 * linklocals for 6to4 interface, but there's no use and
694		 * it is rather harmful to have one.
695		 */
696		goto statinit;
697#endif
698	default:
699		break;
700	}
701
702	/*
703	 * usually, we require multicast capability to the interface
704	 */
705	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
706		nd6log((LOG_INFO, "in6_ifattach: "
707		    "%s is not multicast capable, IPv6 not enabled\n",
708		    if_name(ifp)));
709		return;
710	}
711
712	/*
713	 * assign loopback address for loopback interface.
714	 * XXX multiple loopback interface case.
715	 */
716	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
717		in6 = in6addr_loopback;
718		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
719			if (in6_ifattach_loopback(ifp) != 0)
720				return;
721		}
722	}
723
724	/*
725	 * assign a link-local address, if there's none.
726	 */
727	if (V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) {
728		ia = in6ifa_ifpforlinklocal(ifp, 0);
729		if (ia == NULL) {
730			if (in6_ifattach_linklocal(ifp, altifp) == 0) {
731				/* linklocal address assigned */
732			} else {
733				/* failed to assign linklocal address. bark? */
734			}
735		}
736	}
737
738#ifdef IFT_STF			/* XXX */
739statinit:
740#endif
741
742	/* update dynamically. */
743	if (V_in6_maxmtu < ifp->if_mtu)
744		V_in6_maxmtu = ifp->if_mtu;
745}
746
747/*
748 * NOTE: in6_ifdetach() does not support loopback if at this moment.
749 * We don't need this function in bsdi, because interfaces are never removed
750 * from the ifnet list in bsdi.
751 */
752void
753in6_ifdetach(struct ifnet *ifp)
754{
755	INIT_VNET_NET(ifp->if_vnet);
756	INIT_VNET_INET(ifp->if_vnet);
757	INIT_VNET_INET6(ifp->if_vnet);
758	struct in6_ifaddr *ia, *oia;
759	struct ifaddr *ifa, *next;
760	struct rtentry *rt;
761	short rtflags;
762	struct sockaddr_in6 sin6;
763	struct in6_multi_mship *imm;
764
765	/* remove neighbor management table */
766	nd6_purge(ifp);
767
768	/* nuke any of IPv6 addresses we have */
769	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
770		if (ifa->ifa_addr->sa_family != AF_INET6)
771			continue;
772		in6_purgeaddr(ifa);
773	}
774
775	/* undo everything done by in6_ifattach(), just in case */
776	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
777		if (ifa->ifa_addr->sa_family != AF_INET6
778		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
779			continue;
780		}
781
782		ia = (struct in6_ifaddr *)ifa;
783
784		/*
785		 * leave from multicast groups we have joined for the interface
786		 */
787		while ((imm = ia->ia6_memberships.lh_first) != NULL) {
788			LIST_REMOVE(imm, i6mm_chain);
789			in6_leavegroup(imm);
790		}
791
792		/* remove from the routing table */
793		if ((ia->ia_flags & IFA_ROUTE) &&
794		    (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0, 0UL))) {
795			rtflags = rt->rt_flags;
796			RTFREE_LOCKED(rt);
797			rtrequest(RTM_DELETE, (struct sockaddr *)&ia->ia_addr,
798			    (struct sockaddr *)&ia->ia_addr,
799			    (struct sockaddr *)&ia->ia_prefixmask,
800			    rtflags, (struct rtentry **)0);
801		}
802
803		/* remove from the linked list */
804		IF_ADDR_LOCK(ifp);
805		TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
806		IF_ADDR_UNLOCK(ifp);
807		IFAFREE(&ia->ia_ifa);
808
809		/* also remove from the IPv6 address chain(itojun&jinmei) */
810		oia = ia;
811		if (oia == (ia = V_in6_ifaddr))
812			V_in6_ifaddr = ia->ia_next;
813		else {
814			while (ia->ia_next && (ia->ia_next != oia))
815				ia = ia->ia_next;
816			if (ia->ia_next)
817				ia->ia_next = oia->ia_next;
818			else {
819				nd6log((LOG_ERR,
820				    "%s: didn't unlink in6ifaddr from list\n",
821				    if_name(ifp)));
822			}
823		}
824
825		IFAFREE(&oia->ia_ifa);
826	}
827
828	in6_pcbpurgeif0(&V_udbinfo, ifp);
829	in6_pcbpurgeif0(&V_ripcbinfo, ifp);
830	/* leave from all multicast groups joined */
831	in6_purgemaddrs(ifp);
832
833	/*
834	 * remove neighbor management table.  we call it twice just to make
835	 * sure we nuke everything.  maybe we need just one call.
836	 * XXX: since the first call did not release addresses, some prefixes
837	 * might remain.  We should call nd6_purge() again to release the
838	 * prefixes after removing all addresses above.
839	 * (Or can we just delay calling nd6_purge until at this point?)
840	 */
841	nd6_purge(ifp);
842
843	/* remove route to link-local allnodes multicast (ff02::1) */
844	bzero(&sin6, sizeof(sin6));
845	sin6.sin6_len = sizeof(struct sockaddr_in6);
846	sin6.sin6_family = AF_INET6;
847	sin6.sin6_addr = in6addr_linklocal_allnodes;
848	if (in6_setscope(&sin6.sin6_addr, ifp, NULL))
849		/* XXX: should not fail */
850		return;
851	/* XXX grab lock first to avoid LOR */
852	if (V_rt_tables[0][AF_INET6] != NULL) {
853		RADIX_NODE_HEAD_LOCK(V_rt_tables[0][AF_INET6]);
854		rt = rtalloc1((struct sockaddr *)&sin6, 0, RTF_RNH_LOCKED);
855		if (rt) {
856			if (rt->rt_ifp == ifp)
857				rtexpunge(rt);
858			RTFREE_LOCKED(rt);
859		}
860		RADIX_NODE_HEAD_UNLOCK(V_rt_tables[0][AF_INET6]);
861	}
862}
863
864int
865in6_get_tmpifid(struct ifnet *ifp, u_int8_t *retbuf,
866    const u_int8_t *baseid, int generate)
867{
868	u_int8_t nullbuf[8];
869	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
870
871	bzero(nullbuf, sizeof(nullbuf));
872	if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) == 0) {
873		/* we've never created a random ID.  Create a new one. */
874		generate = 1;
875	}
876
877	if (generate) {
878		bcopy(baseid, ndi->randomseed1, sizeof(ndi->randomseed1));
879
880		/* generate_tmp_ifid will update seedn and buf */
881		(void)generate_tmp_ifid(ndi->randomseed0, ndi->randomseed1,
882		    ndi->randomid);
883	}
884	bcopy(ndi->randomid, retbuf, 8);
885
886	return (0);
887}
888
889void
890in6_tmpaddrtimer(void *ignored_arg)
891{
892	INIT_VNET_NET(curvnet);
893	INIT_VNET_INET6(curvnet);
894	struct nd_ifinfo *ndi;
895	u_int8_t nullbuf[8];
896	struct ifnet *ifp;
897
898	callout_reset(&V_in6_tmpaddrtimer_ch,
899	    (V_ip6_temp_preferred_lifetime - V_ip6_desync_factor -
900	    V_ip6_temp_regen_advance) * hz, in6_tmpaddrtimer, NULL);
901
902	bzero(nullbuf, sizeof(nullbuf));
903	for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
904	    ifp = TAILQ_NEXT(ifp, if_list)) {
905		ndi = ND_IFINFO(ifp);
906		if (bcmp(ndi->randomid, nullbuf, sizeof(nullbuf)) != 0) {
907			/*
908			 * We've been generating a random ID on this interface.
909			 * Create a new one.
910			 */
911			(void)generate_tmp_ifid(ndi->randomseed0,
912			    ndi->randomseed1, ndi->randomid);
913		}
914	}
915
916}
917
918static void
919in6_purgemaddrs(struct ifnet *ifp)
920{
921	struct in6_multi *in6m;
922	struct in6_multi *oin6m;
923
924	LIST_FOREACH_SAFE(in6m, &in6_multihead, in6m_entry, oin6m) {
925		if (in6m->in6m_ifp == ifp)
926			in6_delmulti(in6m);
927	}
928}
929