in6_ifattach.c revision 1.25
1/*	$OpenBSD: in6_ifattach.c,v 1.25 2002/05/29 07:54:59 itojun Exp $	*/
2/*	$KAME: in6_ifattach.c,v 1.124 2001/07/18 08:32:51 jinmei 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/md5k.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
51#include <netinet/ip6.h>
52#include <netinet6/ip6_var.h>
53#include <netinet6/in6_ifattach.h>
54#include <netinet6/ip6_var.h>
55#include <netinet6/nd6.h>
56
57unsigned long in6_maxmtu = 0;
58
59static int get_rand_ifid(struct ifnet *, struct in6_addr *);
60static int get_hw_ifid(struct ifnet *, struct in6_addr *);
61static int get_ifid(struct ifnet *, struct ifnet *, struct in6_addr *);
62static int in6_ifattach_addaddr(struct ifnet *, struct in6_ifaddr *);
63static int in6_ifattach_linklocal(struct ifnet *, struct ifnet *);
64static int in6_ifattach_loopback(struct ifnet *);
65
66#define EUI64_GBIT	0x01
67#define EUI64_UBIT	0x02
68#define EUI64_TO_IFID(in6)	do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
69#define EUI64_GROUP(in6)	((in6)->s6_addr[8] & EUI64_GBIT)
70#define EUI64_INDIVIDUAL(in6)	(!EUI64_GROUP(in6))
71#define EUI64_LOCAL(in6)	((in6)->s6_addr[8] & EUI64_UBIT)
72#define EUI64_UNIVERSAL(in6)	(!EUI64_LOCAL(in6))
73
74#define IFID_LOCAL(in6)		(!EUI64_LOCAL(in6))
75#define IFID_UNIVERSAL(in6)	(!EUI64_UNIVERSAL(in6))
76
77/*
78 * Generate a last-resort interface identifier, when the machine has no
79 * IEEE802/EUI64 address sources.
80 * The goal here is to get an interface identifier that is
81 * (1) random enough and (2) does not change across reboot.
82 * We currently use MD5(hostname) for it.
83 */
84static int
85get_rand_ifid(ifp, in6)
86	struct ifnet *ifp;
87	struct in6_addr *in6;	/* upper 64bits are preserved */
88{
89	MD5_CTX ctxt;
90	u_int8_t digest[16];
91
92#if 0
93	/* we need at least several letters as seed for ifid */
94	if (hostnamelen < 3)
95		return -1;
96#endif
97
98	/* generate 8 bytes of pseudo-random value. */
99	bzero(&ctxt, sizeof(ctxt));
100	MD5Init(&ctxt);
101	MD5Update(&ctxt, hostname, hostnamelen);
102	MD5Final(digest, &ctxt);
103
104	/* assumes sizeof(digest) > sizeof(ifid) */
105	bcopy(digest, &in6->s6_addr[8], 8);
106
107	/* make sure to set "u" bit to local, and "g" bit to individual. */
108	in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
109	in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
110
111	/* convert EUI64 into IPv6 interface identifier */
112	EUI64_TO_IFID(in6);
113
114	return 0;
115}
116
117/*
118 * Get interface identifier for the specified interface.
119 * XXX assumes single sockaddr_dl (AF_LINK address) per an interface
120 */
121static int
122get_hw_ifid(ifp, in6)
123	struct ifnet *ifp;
124	struct in6_addr *in6;	/* upper 64bits are preserved */
125{
126	struct ifaddr *ifa;
127	struct sockaddr_dl *sdl;
128	u_int8_t *addr;
129	size_t addrlen;
130	static u_int8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
131	static u_int8_t allone[8] =
132		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
133
134	for (ifa = ifp->if_addrlist.tqh_first;
135	     ifa;
136	     ifa = ifa->ifa_list.tqe_next)
137	{
138		if (ifa->ifa_addr->sa_family != AF_LINK)
139			continue;
140		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
141		if (sdl == NULL)
142			continue;
143		if (sdl->sdl_alen == 0)
144			continue;
145
146		goto found;
147	}
148
149	return -1;
150
151found:
152	addr = LLADDR(sdl);
153	addrlen = sdl->sdl_alen;
154
155	/* get EUI64 */
156	switch (ifp->if_type) {
157	case IFT_ETHER:
158	case IFT_FDDI:
159	case IFT_ATM:
160		/* IEEE802/EUI64 cases - what others? */
161
162		/* look at IEEE802/EUI64 only */
163		if (addrlen != 8 && addrlen != 6)
164			return -1;
165
166		/*
167		 * check for invalid MAC address - on bsdi, we see it a lot
168		 * since wildboar configures all-zero MAC on pccard before
169		 * card insertion.
170		 */
171		if (bcmp(addr, allzero, addrlen) == 0)
172			return -1;
173		if (bcmp(addr, allone, addrlen) == 0)
174			return -1;
175
176		/* make EUI64 address */
177		if (addrlen == 8)
178			bcopy(addr, &in6->s6_addr[8], 8);
179		else if (addrlen == 6) {
180			in6->s6_addr[8] = addr[0];
181			in6->s6_addr[9] = addr[1];
182			in6->s6_addr[10] = addr[2];
183			in6->s6_addr[11] = 0xff;
184			in6->s6_addr[12] = 0xfe;
185			in6->s6_addr[13] = addr[3];
186			in6->s6_addr[14] = addr[4];
187			in6->s6_addr[15] = addr[5];
188		}
189		break;
190
191	case IFT_ARCNET:
192		if (addrlen != 1)
193			return -1;
194		if (!addr[0])
195			return -1;
196
197		bzero(&in6->s6_addr[8], 8);
198		in6->s6_addr[15] = addr[0];
199
200		/*
201		 * due to insufficient bitwidth, we mark it local.
202		 */
203		in6->s6_addr[8] &= ~EUI64_GBIT;	/* g bit to "individual" */
204		in6->s6_addr[8] |= EUI64_UBIT;	/* u bit to "local" */
205		break;
206
207	case IFT_GIF:
208#ifdef IFT_STF
209	case IFT_STF:
210#endif
211		/*
212		 * RFC2893 says: "SHOULD use IPv4 address as ifid source".
213		 * however, IPv4 address is not very suitable as unique
214		 * identifier source (can be renumbered).
215		 * we don't do this.
216		 */
217		return -1;
218
219	default:
220		return -1;
221	}
222
223	/* sanity check: g bit must not indicate "group" */
224	if (EUI64_GROUP(in6))
225		return -1;
226
227	/* convert EUI64 into IPv6 interface identifier */
228	EUI64_TO_IFID(in6);
229
230	/*
231	 * sanity check: ifid must not be all zero, avoid conflict with
232	 * subnet router anycast
233	 */
234	if ((in6->s6_addr[8] & ~(EUI64_GBIT | EUI64_UBIT)) == 0x00 &&
235	    bcmp(&in6->s6_addr[9], allzero, 7) == 0) {
236		return -1;
237	}
238
239	return 0;
240}
241
242/*
243 * Get interface identifier for the specified interface.  If it is not
244 * available on ifp0, borrow interface identifier from other information
245 * sources.
246 */
247static int
248get_ifid(ifp0, altifp, in6)
249	struct ifnet *ifp0;
250	struct ifnet *altifp;	/* secondary EUI64 source */
251	struct in6_addr *in6;
252{
253	struct ifnet *ifp;
254
255	/* first, try to get it from the interface itself */
256	if (get_hw_ifid(ifp0, in6) == 0) {
257		nd6log((LOG_DEBUG,
258		    "%s: got interface identifier from itself\n",
259		    ifp0->if_xname));
260		goto success;
261	}
262
263	/* try secondary EUI64 source. this basically is for ATM PVC */
264	if (altifp && get_hw_ifid(altifp, in6) == 0) {
265		nd6log((LOG_DEBUG, "%s: got interface identifier from %s\n",
266		    ifp0->if_xname, altifp->if_xname));
267		goto success;
268	}
269
270	/* next, try to get it from some other hardware interface */
271	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
272	{
273		if (ifp == ifp0)
274			continue;
275		if (get_hw_ifid(ifp, in6) != 0)
276			continue;
277
278		/*
279		 * to borrow ifid from other interface, ifid needs to be
280		 * globally unique
281		 */
282		if (IFID_UNIVERSAL(in6)) {
283			nd6log((LOG_DEBUG,
284			    "%s: borrow interface identifier from %s\n",
285			    ifp0->if_xname, ifp->if_xname));
286			goto success;
287		}
288	}
289
290	/* last resort: get from random number source */
291	if (get_rand_ifid(ifp, in6) == 0) {
292		nd6log((LOG_DEBUG,
293		    "%s: interface identifier generated by random number\n",
294		    ifp0->if_xname));
295		goto success;
296	}
297
298	printf("%s: failed to get interface identifier\n", ifp0->if_xname);
299	return -1;
300
301success:
302	nd6log((LOG_INFO, "%s: ifid: "
303		"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
304		ifp0->if_xname,
305		in6->s6_addr[8], in6->s6_addr[9],
306		in6->s6_addr[10], in6->s6_addr[11],
307		in6->s6_addr[12], in6->s6_addr[13],
308		in6->s6_addr[14], in6->s6_addr[15]));
309	return 0;
310}
311
312/*
313 * configure IPv6 interface address.  XXX code duplicated with in.c
314 */
315static int
316in6_ifattach_addaddr(ifp, ia)
317	struct ifnet *ifp;
318	struct in6_ifaddr *ia;
319{
320	struct in6_ifaddr *oia;
321	struct ifaddr *ifa;
322	int error;
323	int rtflag;
324	struct in6_addr llsol;
325
326	/*
327	 * initialize if_addrlist, if we are the very first one
328	 */
329	ifa = TAILQ_FIRST(&ifp->if_addrlist);
330	if (ifa == NULL) {
331		TAILQ_INIT(&ifp->if_addrlist);
332	}
333
334	/*
335	 * link the interface address to global list
336	 */
337	TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
338	ia->ia_ifa.ifa_refcnt++;
339
340	/*
341	 * Also link into the IPv6 address chain beginning with in6_ifaddr.
342	 * kazu opposed it, but itojun & jinmei wanted.
343	 */
344	if ((oia = in6_ifaddr) != NULL) {
345		for (; oia->ia_next; oia = oia->ia_next)
346			continue;
347		oia->ia_next = ia;
348	} else
349		in6_ifaddr = ia;
350	ia->ia_ifa.ifa_refcnt++;
351
352	/*
353	 * give the interface a chance to initialize, in case this
354	 * is the first address to be added.
355	 */
356	if (ifp->if_ioctl != NULL) {
357		int s;
358		s = splimp();
359		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
360		splx(s);
361	} else
362		error = 0;
363	if (error) {
364		switch (error) {
365		case EAFNOSUPPORT:
366			printf("%s: IPv6 not supported\n", ifp->if_xname);
367			break;
368		default:
369			printf("%s: SIOCSIFADDR error %d\n", ifp->if_xname,
370			    error);
371			break;
372		}
373
374		/* undo changes */
375		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
376		IFAFREE(&ia->ia_ifa);
377		if (oia)
378			oia->ia_next = ia->ia_next;
379		else
380			in6_ifaddr = ia->ia_next;
381		IFAFREE(&ia->ia_ifa);
382		return -1;
383	}
384
385	/* configure link-layer address resolution */
386	rtflag = 0;
387	if (IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr, &in6mask128))
388		rtflag = RTF_HOST;
389	else {
390		switch (ifp->if_type) {
391		case IFT_LOOP:
392#ifdef IFT_STF
393		case IFT_STF:
394#endif
395			rtflag = 0;
396			break;
397		default:
398			ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
399			ia->ia_ifa.ifa_flags |= RTF_CLONING;
400			rtflag = RTF_CLONING;
401			break;
402		}
403	}
404
405	/* add route to the interface. */
406	rtrequest(RTM_ADD,
407		  (struct sockaddr *)&ia->ia_addr,
408		  (struct sockaddr *)&ia->ia_addr,
409		  (struct sockaddr *)&ia->ia_prefixmask,
410		  RTF_UP | rtflag,
411		  (struct rtentry **)0);
412	ia->ia_flags |= IFA_ROUTE;
413
414	if ((rtflag & RTF_CLONING) != 0 &&
415	    (ifp->if_flags & IFF_MULTICAST) != 0) {
416		/* Restore saved multicast addresses (if any). */
417		in6_restoremkludge(ia, ifp);
418
419		/*
420		 * join solicited multicast address
421		 */
422		bzero(&llsol, sizeof(llsol));
423		llsol.s6_addr16[0] = htons(0xff02);
424		llsol.s6_addr16[1] = htons(ifp->if_index);
425		llsol.s6_addr32[1] = 0;
426		llsol.s6_addr32[2] = htonl(1);
427		llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
428		llsol.s6_addr8[12] = 0xff;
429		(void)in6_addmulti(&llsol, ifp, &error);
430
431		/* XXX should we run DAD on other interface types? */
432		if (in6if_do_dad(ifp)) {
433			/* mark the address TENTATIVE, if needed. */
434			ia->ia6_flags |= IN6_IFF_TENTATIVE;
435			/* nd6_dad_start() will be called in in6_if_up */
436		}
437	}
438
439	return 0;
440}
441
442static int
443in6_ifattach_linklocal(ifp, altifp)
444	struct ifnet *ifp;
445	struct ifnet *altifp;	/*secondary EUI64 source*/
446{
447	struct in6_ifaddr *ia;
448
449	/*
450	 * configure link-local address
451	 */
452	ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
453	bzero((caddr_t)ia, sizeof(*ia));
454	ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
455	if (ifp->if_flags & IFF_POINTOPOINT)
456		ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
457	else
458		ia->ia_ifa.ifa_dstaddr = NULL;
459	ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
460	ia->ia_ifp = ifp;
461
462	bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
463	ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
464	ia->ia_prefixmask.sin6_family = AF_INET6;
465	ia->ia_prefixmask.sin6_addr = in6mask64;
466
467	/* just in case */
468	bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
469	ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
470	ia->ia_dstaddr.sin6_family = AF_INET6;
471
472	bzero(&ia->ia_addr, sizeof(ia->ia_addr));
473	ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
474	ia->ia_addr.sin6_family = AF_INET6;
475	ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
476	ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
477	ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
478	if (ifp->if_flags & IFF_LOOPBACK) {
479		ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
480		ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
481	} else {
482		if (get_ifid(ifp, altifp, &ia->ia_addr.sin6_addr) != 0) {
483			nd6log((LOG_ERR,
484			    "%s: no ifid available\n", ifp->if_xname));
485			free(ia, M_IFADDR);
486			return -1;
487		}
488	}
489
490	ia->ia_ifa.ifa_metric = ifp->if_metric;
491
492	if (in6_ifattach_addaddr(ifp, ia) != 0) {
493		/* ia will be freed on failure */
494		return -1;
495	}
496
497	return 0;
498}
499
500static int
501in6_ifattach_loopback(ifp)
502	struct ifnet *ifp;	/* must be IFT_LOOP */
503{
504	struct in6_ifaddr *ia;
505
506	/*
507	 * configure link-local address
508	 */
509	ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
510	bzero((caddr_t)ia, sizeof(*ia));
511	ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
512	ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
513	ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
514	ia->ia_ifp = ifp;
515
516	bzero(&ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
517	ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
518	ia->ia_prefixmask.sin6_family = AF_INET6;
519	ia->ia_prefixmask.sin6_addr = in6mask128;
520
521	/*
522	 * Always initialize ia_dstaddr (= broadcast address) to loopback
523	 * address, to make getifaddr happier.
524	 *
525	 * For BSDI, it is mandatory.  The BSDI version of
526	 * ifa_ifwithroute() rejects to add a route to the loopback
527	 * interface.  Even for other systems, loopback looks somewhat
528	 * special.
529	 */
530	bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
531	ia->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
532	ia->ia_dstaddr.sin6_family = AF_INET6;
533	ia->ia_dstaddr.sin6_addr = in6addr_loopback;
534
535	bzero(&ia->ia_addr, sizeof(ia->ia_addr));
536	ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
537	ia->ia_addr.sin6_family = AF_INET6;
538	ia->ia_addr.sin6_addr = in6addr_loopback;
539
540	ia->ia_ifa.ifa_metric = ifp->if_metric;
541
542	if (in6_ifattach_addaddr(ifp, ia) != 0) {
543		/* ia will be freed on failure */
544		return -1;
545	}
546
547	return 0;
548}
549
550/*
551 * XXX multiple loopback interface needs more care.  for instance,
552 * nodelocal address needs to be configured onto only one of them.
553 * XXX multiple link-local address case
554 */
555void
556in6_ifattach(ifp, altifp)
557	struct ifnet *ifp;
558	struct ifnet *altifp;	/* secondary EUI64 source */
559{
560	struct sockaddr_in6 mltaddr;
561	struct sockaddr_in6 mltmask;
562	struct sockaddr_in6 gate;
563	struct sockaddr_in6 mask;
564	struct in6_ifaddr *ia;
565	struct in6_addr in6;
566
567	/* some of the interfaces are inherently not IPv6 capable */
568	switch (ifp->if_type) {
569	case IFT_BRIDGE:
570	case IFT_ENC:
571	case IFT_PFLOG:
572		return;
573	case IFT_PROPVIRTUAL:
574		if (strncmp("bridge", ifp->if_xname, sizeof("bridge")) == 0 &&
575		    '0' <= ifp->if_xname[sizeof("bridge")] &&
576		    ifp->if_xname[sizeof("bridge")] <= '9')
577			return;
578		break;
579	}
580
581	/*
582	 * if link mtu is too small, don't try to configure IPv6.
583	 * remember there could be some link-layer that has special
584	 * fragmentation logic.
585	 */
586	if (ifp->if_mtu < IPV6_MMTU)
587		return;
588
589	/* create a multicast kludge storage (if we have not had one) */
590	in6_createmkludge(ifp);
591
592	/*
593	 * quirks based on interface type
594	 */
595	switch (ifp->if_type) {
596#ifdef IFT_STF
597	case IFT_STF:
598		/*
599		 * 6to4 interface is a very special kind of beast.
600		 * no multicast, no linklocal.  RFC2529 specifies how to make
601		 * linklocals for 6to4 interface, but there's no use and
602		 * it is rather harmful to have one.
603		 */
604		return;
605#endif
606	default:
607		break;
608	}
609
610	/*
611	 * usually, we require multicast capability to the interface
612	 */
613	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
614		log(LOG_INFO, "in6_ifattach: "
615		    "%s is not multicast capable, IPv6 not enabled\n",
616		    ifp->if_xname);
617		return;
618	}
619
620	/*
621	 * assign link-local address, if there's none
622	 */
623	ia = in6ifa_ifpforlinklocal(ifp, 0);
624	if (ia == NULL) {
625		if (in6_ifattach_linklocal(ifp, altifp) != 0)
626			return;
627		ia = in6ifa_ifpforlinklocal(ifp, 0);
628
629		if (ia == NULL) {
630			printf("%s: failed to add link-local address\n",
631			    ifp->if_xname);
632
633			/* we can't initialize multicasts without link-local */
634			return;
635		}
636	}
637
638	if (ifp->if_flags & IFF_POINTOPOINT) {
639		/*
640		 * route local address to loopback
641		 */
642		bzero(&gate, sizeof(gate));
643		gate.sin6_len = sizeof(struct sockaddr_in6);
644		gate.sin6_family = AF_INET6;
645		gate.sin6_addr = in6addr_loopback;
646		bzero(&mask, sizeof(mask));
647		mask.sin6_len = sizeof(struct sockaddr_in6);
648		mask.sin6_family = AF_INET6;
649		mask.sin6_addr = in6mask64;
650		rtrequest(RTM_ADD,
651			  (struct sockaddr *)&ia->ia_addr,
652			  (struct sockaddr *)&gate,
653			  (struct sockaddr *)&mask,
654			  RTF_UP|RTF_HOST,
655			  (struct rtentry **)0);
656	}
657
658	/*
659	 * assign loopback address for loopback interface
660	 * XXX multiple loopback interface case
661	 */
662	in6 = in6addr_loopback;
663	if (ifp->if_flags & IFF_LOOPBACK) {
664		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
665			if (in6_ifattach_loopback(ifp) != 0)
666				return;
667		}
668	}
669
670#ifdef DIAGNOSTIC
671	if (!ia) {
672		panic("ia == NULL in in6_ifattach");
673		/*NOTREACHED*/
674	}
675#endif
676
677	/*
678	 * join multicast
679	 */
680	if (ifp->if_flags & IFF_MULTICAST) {
681		int error;	/* not used */
682		struct in6_multi *in6m;
683
684		/* Restore saved multicast addresses(if any). */
685		in6_restoremkludge(ia, ifp);
686
687		bzero(&mltmask, sizeof(mltmask));
688		mltmask.sin6_len = sizeof(struct sockaddr_in6);
689		mltmask.sin6_family = AF_INET6;
690		mltmask.sin6_addr = in6mask32;
691
692		/*
693		 * join link-local all-nodes address
694		 */
695		bzero(&mltaddr, sizeof(mltaddr));
696		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
697		mltaddr.sin6_family = AF_INET6;
698		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
699		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
700
701		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
702		if (in6m == NULL) {
703			rtrequest(RTM_ADD,
704				  (struct sockaddr *)&mltaddr,
705				  (struct sockaddr *)&ia->ia_addr,
706				  (struct sockaddr *)&mltmask,
707				  RTF_UP|RTF_CLONING,  /* xxx */
708				  (struct rtentry **)0);
709			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
710		}
711
712		if (ifp->if_flags & IFF_LOOPBACK) {
713			in6 = in6addr_loopback;
714			ia = in6ifa_ifpwithaddr(ifp, &in6);
715			/*
716			 * join node-local all-nodes address, on loopback
717			 */
718			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
719
720			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
721			if (in6m == NULL && ia != NULL) {
722				rtrequest(RTM_ADD,
723					  (struct sockaddr *)&mltaddr,
724					  (struct sockaddr *)&ia->ia_addr,
725					  (struct sockaddr *)&mltmask,
726					  RTF_UP,
727					  (struct rtentry **)0);
728				(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
729			}
730		}
731	}
732}
733
734/*
735 * NOTE: in6_ifdetach() does not support loopback if at this moment.
736 */
737void
738in6_ifdetach(ifp)
739	struct ifnet *ifp;
740{
741	struct in6_ifaddr *ia, *oia;
742	struct ifaddr *ifa, *next;
743	struct rtentry *rt;
744	short rtflags;
745	struct sockaddr_in6 sin6;
746	struct in6_multi *in6m;
747
748	/* nuke prefix list.  this may try to remove some of ifaddrs as well */
749	in6_purgeprefix(ifp);
750
751	/* remove neighbor management table */
752	nd6_purge(ifp);
753
754	/* nuke any of IPv6 addresses we have */
755	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
756	{
757		next = ifa->ifa_list.tqe_next;
758		if (ifa->ifa_addr->sa_family != AF_INET6)
759			continue;
760		in6_purgeaddr(ifa, ifp);
761	}
762
763	/* undo everything done by in6_ifattach(), just in case */
764	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
765	{
766		if (ifa->ifa_addr->sa_family != AF_INET6
767		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
768			continue;
769		}
770
771		ia = (struct in6_ifaddr *)ifa;
772
773		/* leave from all multicast groups joined */
774		while ((in6m = LIST_FIRST(&ia->ia6_multiaddrs)) != NULL)
775			in6_delmulti(in6m);
776
777		/* remove from the routing table */
778		if ((ia->ia_flags & IFA_ROUTE)
779		 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
780			rtflags = rt->rt_flags;
781			rtfree(rt);
782			rtrequest(RTM_DELETE,
783				(struct sockaddr *)&ia->ia_addr,
784				(struct sockaddr *)&ia->ia_addr,
785				(struct sockaddr *)&ia->ia_prefixmask,
786				rtflags, (struct rtentry **)0);
787		}
788
789		/* remove from the linked list */
790		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
791		IFAFREE(&ia->ia_ifa);
792
793		/* also remove from the IPv6 address chain(itojun&jinmei) */
794		oia = ia;
795		if (oia == (ia = in6_ifaddr))
796			in6_ifaddr = ia->ia_next;
797		else {
798			while (ia->ia_next && (ia->ia_next != oia))
799				ia = ia->ia_next;
800			if (ia->ia_next)
801				ia->ia_next = oia->ia_next;
802			else {
803				nd6log((LOG_ERR,
804				    "%s: didn't unlink in6ifaddr from "
805				    "list\n", ifp->if_xname));
806			}
807		}
808
809		IFAFREE(&oia->ia_ifa);
810	}
811
812	/* cleanup multicast address kludge table, if there is any */
813	in6_purgemkludge(ifp);
814
815	/*
816	 * remove neighbor management table.  we call it twice just to make
817	 * sure we nuke everything.  maybe we need just one call.
818	 * XXX: since the first call did not release addresses, some prefixes
819	 * might remain.  We should call nd6_purge() again to release the
820	 * prefixes after removing all addresses above.
821	 * (Or can we just delay calling nd6_purge until at this point?)
822	 */
823	nd6_purge(ifp);
824
825	/* remove route to link-local allnodes multicast (ff02::1) */
826	bzero(&sin6, sizeof(sin6));
827	sin6.sin6_len = sizeof(struct sockaddr_in6);
828	sin6.sin6_family = AF_INET6;
829	sin6.sin6_addr = in6addr_linklocal_allnodes;
830	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
831	rt = rtalloc1((struct sockaddr *)&sin6, 0);
832	if (rt && rt->rt_ifp == ifp) {
833		rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
834			rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
835		rtfree(rt);
836	}
837}
838