in6_ifattach.c revision 1.24
1/*	$OpenBSD: in6_ifattach.c,v 1.24 2002/05/29 02:59:12 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	/* create a multicast kludge storage (if we have not had one) */
582	in6_createmkludge(ifp);
583
584	/*
585	 * quirks based on interface type
586	 */
587	switch (ifp->if_type) {
588#ifdef IFT_STF
589	case IFT_STF:
590		/*
591		 * 6to4 interface is a very special kind of beast.
592		 * no multicast, no linklocal.  RFC2529 specifies how to make
593		 * linklocals for 6to4 interface, but there's no use and
594		 * it is rather harmful to have one.
595		 */
596		goto statinit;
597#endif
598	default:
599		break;
600	}
601
602	/*
603	 * usually, we require multicast capability to the interface
604	 */
605	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
606		log(LOG_INFO, "in6_ifattach: "
607		    "%s is not multicast capable, IPv6 not enabled\n",
608		    ifp->if_xname);
609		return;
610	}
611
612	/*
613	 * assign link-local address, if there's none
614	 */
615	ia = in6ifa_ifpforlinklocal(ifp, 0);
616	if (ia == NULL) {
617		if (in6_ifattach_linklocal(ifp, altifp) != 0)
618			return;
619		ia = in6ifa_ifpforlinklocal(ifp, 0);
620
621		if (ia == NULL) {
622			printf("%s: failed to add link-local address\n",
623			    ifp->if_xname);
624
625			/* we can't initialize multicasts without link-local */
626			goto statinit;
627		}
628	}
629
630	if (ifp->if_flags & IFF_POINTOPOINT) {
631		/*
632		 * route local address to loopback
633		 */
634		bzero(&gate, sizeof(gate));
635		gate.sin6_len = sizeof(struct sockaddr_in6);
636		gate.sin6_family = AF_INET6;
637		gate.sin6_addr = in6addr_loopback;
638		bzero(&mask, sizeof(mask));
639		mask.sin6_len = sizeof(struct sockaddr_in6);
640		mask.sin6_family = AF_INET6;
641		mask.sin6_addr = in6mask64;
642		rtrequest(RTM_ADD,
643			  (struct sockaddr *)&ia->ia_addr,
644			  (struct sockaddr *)&gate,
645			  (struct sockaddr *)&mask,
646			  RTF_UP|RTF_HOST,
647			  (struct rtentry **)0);
648	}
649
650	/*
651	 * assign loopback address for loopback interface
652	 * XXX multiple loopback interface case
653	 */
654	in6 = in6addr_loopback;
655	if (ifp->if_flags & IFF_LOOPBACK) {
656		if (in6ifa_ifpwithaddr(ifp, &in6) == NULL) {
657			if (in6_ifattach_loopback(ifp) != 0)
658				return;
659		}
660	}
661
662#ifdef DIAGNOSTIC
663	if (!ia) {
664		panic("ia == NULL in in6_ifattach");
665		/*NOTREACHED*/
666	}
667#endif
668
669	/*
670	 * join multicast
671	 */
672	if (ifp->if_flags & IFF_MULTICAST) {
673		int error;	/* not used */
674		struct in6_multi *in6m;
675
676		/* Restore saved multicast addresses(if any). */
677		in6_restoremkludge(ia, ifp);
678
679		bzero(&mltmask, sizeof(mltmask));
680		mltmask.sin6_len = sizeof(struct sockaddr_in6);
681		mltmask.sin6_family = AF_INET6;
682		mltmask.sin6_addr = in6mask32;
683
684		/*
685		 * join link-local all-nodes address
686		 */
687		bzero(&mltaddr, sizeof(mltaddr));
688		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
689		mltaddr.sin6_family = AF_INET6;
690		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
691		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
692
693		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
694		if (in6m == NULL) {
695			rtrequest(RTM_ADD,
696				  (struct sockaddr *)&mltaddr,
697				  (struct sockaddr *)&ia->ia_addr,
698				  (struct sockaddr *)&mltmask,
699				  RTF_UP|RTF_CLONING,  /* xxx */
700				  (struct rtentry **)0);
701			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
702		}
703
704		if (ifp->if_flags & IFF_LOOPBACK) {
705			in6 = in6addr_loopback;
706			ia = in6ifa_ifpwithaddr(ifp, &in6);
707			/*
708			 * join node-local all-nodes address, on loopback
709			 */
710			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
711
712			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
713			if (in6m == NULL && ia != NULL) {
714				rtrequest(RTM_ADD,
715					  (struct sockaddr *)&mltaddr,
716					  (struct sockaddr *)&ia->ia_addr,
717					  (struct sockaddr *)&mltmask,
718					  RTF_UP,
719					  (struct rtentry **)0);
720				(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
721			}
722		}
723	}
724
725statinit:;
726
727	/* update dynamically. */
728	if (in6_maxmtu < ifp->if_mtu)
729		in6_maxmtu = ifp->if_mtu;
730
731	/* initialize NDP variables */
732	nd6_ifattach(ifp);
733}
734
735/*
736 * NOTE: in6_ifdetach() does not support loopback if at this moment.
737 */
738void
739in6_ifdetach(ifp)
740	struct ifnet *ifp;
741{
742	struct in6_ifaddr *ia, *oia;
743	struct ifaddr *ifa, *next;
744	struct rtentry *rt;
745	short rtflags;
746	struct sockaddr_in6 sin6;
747	struct in6_multi *in6m;
748
749	/* nuke prefix list.  this may try to remove some of ifaddrs as well */
750	in6_purgeprefix(ifp);
751
752	/* remove neighbor management table */
753	nd6_purge(ifp);
754
755	/* nuke any of IPv6 addresses we have */
756	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
757	{
758		next = ifa->ifa_list.tqe_next;
759		if (ifa->ifa_addr->sa_family != AF_INET6)
760			continue;
761		in6_purgeaddr(ifa, ifp);
762	}
763
764	/* undo everything done by in6_ifattach(), just in case */
765	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = next)
766	{
767		if (ifa->ifa_addr->sa_family != AF_INET6
768		 || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
769			continue;
770		}
771
772		ia = (struct in6_ifaddr *)ifa;
773
774		/* leave from all multicast groups joined */
775		while ((in6m = LIST_FIRST(&ia->ia6_multiaddrs)) != NULL)
776			in6_delmulti(in6m);
777
778		/* remove from the routing table */
779		if ((ia->ia_flags & IFA_ROUTE)
780		 && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
781			rtflags = rt->rt_flags;
782			rtfree(rt);
783			rtrequest(RTM_DELETE,
784				(struct sockaddr *)&ia->ia_addr,
785				(struct sockaddr *)&ia->ia_addr,
786				(struct sockaddr *)&ia->ia_prefixmask,
787				rtflags, (struct rtentry **)0);
788		}
789
790		/* remove from the linked list */
791		TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
792		IFAFREE(&ia->ia_ifa);
793
794		/* also remove from the IPv6 address chain(itojun&jinmei) */
795		oia = ia;
796		if (oia == (ia = in6_ifaddr))
797			in6_ifaddr = ia->ia_next;
798		else {
799			while (ia->ia_next && (ia->ia_next != oia))
800				ia = ia->ia_next;
801			if (ia->ia_next)
802				ia->ia_next = oia->ia_next;
803			else {
804				nd6log((LOG_ERR,
805				    "%s: didn't unlink in6ifaddr from "
806				    "list\n", ifp->if_xname));
807			}
808		}
809
810		IFAFREE(&oia->ia_ifa);
811	}
812
813	/* cleanup multicast address kludge table, if there is any */
814	in6_purgemkludge(ifp);
815
816	/*
817	 * remove neighbor management table.  we call it twice just to make
818	 * sure we nuke everything.  maybe we need just one call.
819	 * XXX: since the first call did not release addresses, some prefixes
820	 * might remain.  We should call nd6_purge() again to release the
821	 * prefixes after removing all addresses above.
822	 * (Or can we just delay calling nd6_purge until at this point?)
823	 */
824	nd6_purge(ifp);
825
826	/* remove route to link-local allnodes multicast (ff02::1) */
827	bzero(&sin6, sizeof(sin6));
828	sin6.sin6_len = sizeof(struct sockaddr_in6);
829	sin6.sin6_family = AF_INET6;
830	sin6.sin6_addr = in6addr_linklocal_allnodes;
831	sin6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
832	rt = rtalloc1((struct sockaddr *)&sin6, 0);
833	if (rt && rt->rt_ifp == ifp) {
834		rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
835			rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
836		rtfree(rt);
837	}
838}
839