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