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