xref: /netbsd-src/sys/netinet6/in6.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: in6.c,v 1.49 2001/11/13 00:56:58 lukem Exp $	*/
2 /*	$KAME: in6.c,v 1.198 2001/07/18 09:12:38 itojun 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 /*
34  * Copyright (c) 1982, 1986, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)in.c	8.2 (Berkeley) 11/15/93
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.49 2001/11/13 00:56:58 lukem Exp $");
70 
71 #include "opt_inet.h"
72 
73 #include <sys/param.h>
74 #include <sys/ioctl.h>
75 #include <sys/errno.h>
76 #include <sys/malloc.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sockio.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82 #include <sys/time.h>
83 #include <sys/kernel.h>
84 #include <sys/syslog.h>
85 
86 #include <net/if.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/if_dl.h>
90 
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <net/if_ether.h>
94 
95 #include <netinet/ip6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/nd6.h>
98 #include <netinet6/mld6_var.h>
99 #include <netinet6/ip6_mroute.h>
100 #include <netinet6/in6_ifattach.h>
101 
102 #include <net/net_osdep.h>
103 
104 /* enable backward compatibility code for obsoleted ioctls */
105 #define COMPAT_IN6IFIOCTL
106 
107 /*
108  * Definitions of some costant IP6 addresses.
109  */
110 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
111 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
112 const struct in6_addr in6addr_nodelocal_allnodes =
113 	IN6ADDR_NODELOCAL_ALLNODES_INIT;
114 const struct in6_addr in6addr_linklocal_allnodes =
115 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
116 const struct in6_addr in6addr_linklocal_allrouters =
117 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
118 
119 const struct in6_addr in6mask0 = IN6MASK0;
120 const struct in6_addr in6mask32 = IN6MASK32;
121 const struct in6_addr in6mask64 = IN6MASK64;
122 const struct in6_addr in6mask96 = IN6MASK96;
123 const struct in6_addr in6mask128 = IN6MASK128;
124 
125 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
126 				     0, 0, IN6ADDR_ANY_INIT, 0};
127 
128 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
129 	struct ifnet *, struct proc *));
130 
131 /*
132  * This structure is used to keep track of in6_multi chains which belong to
133  * deleted interface addresses.
134  */
135 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
136 
137 struct multi6_kludge {
138 	LIST_ENTRY(multi6_kludge) mk_entry;
139 	struct ifnet *mk_ifp;
140 	struct in6_multihead mk_head;
141 };
142 
143 /*
144  * Check if the loopback entry will be automatically generated.
145  *   if 0 returned, will not be automatically generated.
146  *   if 1 returned, will be automatically generated.
147  */
148 static int
149 in6_is_ifloop_auto(struct ifaddr *ifa)
150 {
151 #define SIN6(s) ((struct sockaddr_in6 *)s)
152 	/*
153 	 * If RTF_CLONING is unset, or (IFF_LOOPBACK | IFF_POINTOPOINT),
154 	 * or netmask is all0 or all1, then cloning will not happen,
155 	 * then we can't rely on its loopback entry generation.
156 	 */
157 	if ((ifa->ifa_flags & RTF_CLONING) == 0 ||
158 	    (ifa->ifa_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) ||
159 	    (SIN6(ifa->ifa_netmask)->sin6_len == sizeof(struct sockaddr_in6)
160 	     &&
161 	     IN6_ARE_ADDR_EQUAL(&SIN6(ifa->ifa_netmask)->sin6_addr,
162 				&in6mask128)) ||
163 	    ((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_len == 0)
164 		return 0;
165 	else
166 		return 1;
167 #undef SIN6
168 }
169 
170 /*
171  * Subroutine for in6_ifaddloop() and in6_ifremloop().
172  * This routine does actual work.
173  */
174 static void
175 in6_ifloop_request(int cmd, struct ifaddr *ifa)
176 {
177 	struct sockaddr_in6 lo_sa;
178 	struct sockaddr_in6 all1_sa;
179 	struct rtentry *nrt = NULL, **nrtp = NULL;
180 
181 	bzero(&lo_sa, sizeof(lo_sa));
182 	bzero(&all1_sa, sizeof(all1_sa));
183 	lo_sa.sin6_family = AF_INET6;
184 	lo_sa.sin6_len = sizeof(struct sockaddr_in6);
185 	all1_sa = lo_sa;
186 	lo_sa.sin6_addr = in6addr_loopback;
187 	all1_sa.sin6_addr = in6mask128;
188 
189 	/*
190 	 * So we add or remove static loopback entry, here.
191 	 * This request for deletion could fail, e.g. when we remove
192 	 * an address right after adding it.
193 	 */
194 	if (cmd == RTM_ADD)
195 		nrtp = &nrt;
196 	rtrequest(cmd, ifa->ifa_addr,
197 		  (struct sockaddr *)&lo_sa,
198 		  (struct sockaddr *)&all1_sa,
199 		  RTF_UP|RTF_HOST, nrtp);
200 
201 	/*
202 	 * Make sure rt_ifa be equal to IFA, the second argument of the
203 	 * function.
204 	 * We need this because when we refer to rt_ifa->ia6_flags in
205 	 * ip6_input, we assume that the rt_ifa points to the address instead
206 	 * of the loopback address.
207 	 */
208 	if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
209 		IFAFREE(nrt->rt_ifa);
210 		IFAREF(ifa);
211 		nrt->rt_ifa = ifa;
212 	}
213 	if (nrt)
214 		nrt->rt_refcnt--;
215 }
216 
217 /*
218  * Add ownaddr as loopback rtentry, if necessary(ex. on p2p link).
219  * Because, KAME needs loopback rtentry for ownaddr check in
220  * ip6_input().
221  */
222 static void
223 in6_ifaddloop(struct ifaddr *ifa)
224 {
225 	if (!in6_is_ifloop_auto(ifa)) {
226 		struct rtentry *rt;
227 
228 		/* If there is no loopback entry, allocate one. */
229 		rt = rtalloc1(ifa->ifa_addr, 0);
230 		if (rt == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
231 			in6_ifloop_request(RTM_ADD, ifa);
232 		if (rt)
233 			rt->rt_refcnt--;
234 	}
235 }
236 
237 /*
238  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
239  * if it exists.
240  */
241 static void
242 in6_ifremloop(struct ifaddr *ifa)
243 {
244 	if (!in6_is_ifloop_auto(ifa)) {
245 		struct in6_ifaddr *ia;
246 		int ia_count = 0;
247 
248 		/* If only one ifa for the loopback entry, delete it. */
249 		for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
250 			if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa),
251 					       &ia->ia_addr.sin6_addr)) {
252 				ia_count++;
253 				if (ia_count > 1)
254 					break;
255 			}
256 		}
257 		if (ia_count == 1)
258 			in6_ifloop_request(RTM_DELETE, ifa);
259 	}
260 }
261 
262 int
263 in6_ifindex2scopeid(idx)
264 	int idx;
265 {
266 	struct ifnet *ifp;
267 	struct ifaddr *ifa;
268 	struct sockaddr_in6 *sin6;
269 
270 	if (idx < 0 || if_index < idx)
271 		return -1;
272 	ifp = ifindex2ifnet[idx];
273 	if (!ifp)
274 		return -1;
275 
276 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
277 	{
278 		if (ifa->ifa_addr->sa_family != AF_INET6)
279 			continue;
280 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
281 		if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
282 			return sin6->sin6_scope_id & 0xffff;
283 	}
284 
285 	return -1;
286 }
287 
288 int
289 in6_mask2len(mask)
290 	struct in6_addr *mask;
291 {
292 	int x, y;
293 
294 	for (x = 0; x < sizeof(*mask); x++) {
295 		if (mask->s6_addr8[x] != 0xff)
296 			break;
297 	}
298 	y = 0;
299 	if (x < sizeof(*mask)) {
300 		for (y = 0; y < 8; y++) {
301 			if ((mask->s6_addr8[x] & (0x80 >> y)) == 0)
302 				break;
303 		}
304 	}
305 	return x * 8 + y;
306 }
307 
308 void
309 in6_len2mask(mask, len)
310 	struct in6_addr *mask;
311 	int len;
312 {
313 	int i;
314 
315 	bzero(mask, sizeof(*mask));
316 	for (i = 0; i < len / 8; i++)
317 		mask->s6_addr8[i] = 0xff;
318 	if (len % 8)
319 		mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
320 }
321 
322 #define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
323 #define ia62ifa(ia6)	(&((ia6)->ia_ifa))
324 
325 int
326 in6_control(so, cmd, data, ifp, p)
327 	struct	socket *so;
328 	u_long cmd;
329 	caddr_t	data;
330 	struct ifnet *ifp;
331 	struct proc *p;
332 {
333 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
334 	struct	in6_ifaddr *ia, *oia;
335 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
336 	struct	sockaddr_in6 oldaddr;
337 #ifdef COMPAT_IN6IFIOCTL
338 	struct	sockaddr_in6 net;
339 #endif
340 	int error = 0, hostIsNew, prefixIsNew;
341 	int newifaddr;
342 	time_t time_second = (time_t)time.tv_sec;
343 	int privileged;
344 
345 	privileged = 0;
346 	if (p && !suser(p->p_ucred, &p->p_acflag))
347 		privileged++;
348 
349 	switch (cmd) {
350 	case SIOCGETSGCNT_IN6:
351 	case SIOCGETMIFCNT_IN6:
352 		return (mrt6_ioctl(cmd, data));
353 	}
354 
355 	if (ifp == NULL)
356 		return(EOPNOTSUPP);
357 
358 	switch (cmd) {
359 	case SIOCSNDFLUSH_IN6:
360 	case SIOCSPFXFLUSH_IN6:
361 	case SIOCSRTRFLUSH_IN6:
362 	case SIOCSDEFIFACE_IN6:
363 	case SIOCSIFINFO_FLAGS:
364 		if (!privileged)
365 			return(EPERM);
366 		/*fall through*/
367 	case SIOCGIFINFO_IN6:
368 	case SIOCGDRLST_IN6:
369 	case SIOCGPRLST_IN6:
370 	case SIOCGNBRINFO_IN6:
371 	case SIOCGDEFIFACE_IN6:
372 		return(nd6_ioctl(cmd, data, ifp));
373 	}
374 
375 	switch (cmd) {
376 	case SIOCSIFPREFIX_IN6:
377 	case SIOCDIFPREFIX_IN6:
378 	case SIOCAIFPREFIX_IN6:
379 	case SIOCCIFPREFIX_IN6:
380 	case SIOCSGIFPREFIX_IN6:
381 		if (!privileged)
382 			return(EPERM);
383 		/*fall through*/
384 	case SIOCGIFPREFIX_IN6:
385 		return(in6_prefix_ioctl(so, cmd, data, ifp));
386 	}
387 
388 	switch (cmd) {
389 	case SIOCALIFADDR:
390 	case SIOCDLIFADDR:
391 		if (!privileged)
392 			return(EPERM);
393 		/*fall through*/
394 	case SIOCGLIFADDR:
395 		return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
396 	}
397 
398 	/*
399 	 * Find address for this interface, if it exists.
400 	 */
401 	if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
402 		struct sockaddr_in6 *sa6 =
403 			(struct sockaddr_in6 *)&ifra->ifra_addr;
404 
405 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
406 			if (sa6->sin6_addr.s6_addr16[1] == 0) {
407 				/* interface ID is not embedded by the user */
408 				sa6->sin6_addr.s6_addr16[1] =
409 					htons(ifp->if_index);
410 			} else if (sa6->sin6_addr.s6_addr16[1] !=
411 				    htons(ifp->if_index)) {
412 				return(EINVAL);	/* ifid contradicts */
413 			}
414 			if (sa6->sin6_scope_id) {
415 				if (sa6->sin6_scope_id !=
416 				    (u_int32_t)ifp->if_index)
417 					return(EINVAL);
418 				sa6->sin6_scope_id = 0; /* XXX: good way? */
419 			}
420 		}
421 		ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
422 	} else
423 		ia = NULL;
424 
425 	switch (cmd) {
426 
427 	case SIOCDIFADDR_IN6:
428 		/*
429 		 * for IPv4, we look for existing in_ifaddr here to allow
430 		 * "ifconfig if0 delete" to remove first IPv4 address on the
431 		 * interface.  For IPv6, as the spec allow multiple interface
432 		 * address from the day one, we consider "remove the first one"
433 		 * semantics to be not preferable.
434 		 */
435 		if (ia == NULL)
436 			return(EADDRNOTAVAIL);
437 		/* FALLTHROUGH */
438 	case SIOCAIFADDR_IN6:
439 	case SIOCSIFADDR_IN6:
440 #ifdef COMPAT_IN6IFIOCTL
441 	case SIOCSIFDSTADDR_IN6:
442 	case SIOCSIFNETMASK_IN6:
443 		/*
444 		 * Since IPv6 allows a node to assign multiple addresses
445 		 * on a single interface, SIOCSIFxxx ioctls are not suitable
446 		 * and should be unused.
447 		 */
448 #endif
449 		if (ifra->ifra_addr.sin6_family != AF_INET6)
450 			return(EAFNOSUPPORT);
451 		if (!privileged)
452 			return(EPERM);
453 		if (ia == NULL) {
454 			ia = (struct in6_ifaddr *)
455 				malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
456 			if (ia == NULL)
457 				return (ENOBUFS);
458 			bzero((caddr_t)ia, sizeof(*ia));
459 			/* Initialize the address and masks */
460 			ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
461 			ia->ia_addr.sin6_family = AF_INET6;
462 			ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
463 			if (ifp->if_flags & IFF_POINTOPOINT) {
464 				ia->ia_ifa.ifa_dstaddr
465 					= (struct sockaddr *)&ia->ia_dstaddr;
466 				ia->ia_dstaddr.sin6_family = AF_INET6;
467 				ia->ia_dstaddr.sin6_len = sizeof(ia->ia_dstaddr);
468 			} else {
469 				ia->ia_ifa.ifa_dstaddr = NULL;
470 				bzero(&ia->ia_dstaddr, sizeof(ia->ia_dstaddr));
471 			}
472 			ia->ia_ifa.ifa_netmask
473 				= (struct sockaddr *)&ia->ia_prefixmask;
474 
475 			ia->ia_ifp = ifp;
476 			if ((oia = in6_ifaddr) != NULL) {
477 				for ( ; oia->ia_next; oia = oia->ia_next)
478 					continue;
479 				oia->ia_next = ia;
480 			} else
481 				in6_ifaddr = ia;
482 			IFAREF(&ia->ia_ifa);
483 
484 			TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
485 			    ifa_list);
486 			IFAREF(&ia->ia_ifa);
487 
488 			newifaddr = 1;
489 		} else
490 			newifaddr = 0;
491 
492 		if (cmd == SIOCAIFADDR_IN6) {
493 			/* sanity for overflow - beware unsigned */
494 			struct in6_addrlifetime *lt;
495 			lt = &ifra->ifra_lifetime;
496 			if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
497 			 && lt->ia6t_vltime + time_second < time_second) {
498 				return EINVAL;
499 			}
500 			if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
501 			 && lt->ia6t_pltime + time_second < time_second) {
502 				return EINVAL;
503 			}
504 		}
505 		break;
506 
507 	case SIOCGIFADDR_IN6:
508 		/* This interface is basically deprecated. use SIOCGIFCONF. */
509 		/* fall through */
510 	case SIOCGIFAFLAG_IN6:
511 	case SIOCGIFNETMASK_IN6:
512 	case SIOCGIFDSTADDR_IN6:
513 	case SIOCGIFALIFETIME_IN6:
514 		/* must think again about its semantics */
515 		if (ia == NULL)
516 			return(EADDRNOTAVAIL);
517 		break;
518 	case SIOCSIFALIFETIME_IN6:
519 	    {
520 		struct in6_addrlifetime *lt;
521 
522 		if (!privileged)
523 			return(EPERM);
524 		if (ia == NULL)
525 			return(EADDRNOTAVAIL);
526 		/* sanity for overflow - beware unsigned */
527 		lt = &ifr->ifr_ifru.ifru_lifetime;
528 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
529 		 && lt->ia6t_vltime + time_second < time_second) {
530 			return EINVAL;
531 		}
532 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
533 		 && lt->ia6t_pltime + time_second < time_second) {
534 			return EINVAL;
535 		}
536 		break;
537 	    }
538 	}
539 
540 	switch (cmd) {
541 
542 	case SIOCGIFADDR_IN6:
543 		ifr->ifr_addr = ia->ia_addr;
544 		break;
545 
546 	case SIOCGIFDSTADDR_IN6:
547 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
548 			return(EINVAL);
549 		/*
550 		 * XXX: should we check if ifa_dstaddr is NULL and return
551 		 * an error?
552 		 */
553 		ifr->ifr_dstaddr = ia->ia_dstaddr;
554 		break;
555 
556 	case SIOCGIFNETMASK_IN6:
557 		ifr->ifr_addr = ia->ia_prefixmask;
558 		break;
559 
560 	case SIOCGIFAFLAG_IN6:
561 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
562 		break;
563 
564 	case SIOCGIFSTAT_IN6:
565 		if (ifp == NULL)
566 			return EINVAL;
567 		if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
568 		 || in6_ifstat[ifp->if_index] == NULL) {
569 			/* return EAFNOSUPPORT? */
570 			bzero(&ifr->ifr_ifru.ifru_stat,
571 				sizeof(ifr->ifr_ifru.ifru_stat));
572 		} else
573 			ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
574 		break;
575 
576 	case SIOCGIFSTAT_ICMP6:
577 		if (ifp == NULL)
578 			return EINVAL;
579 		if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
580 		    icmp6_ifstat[ifp->if_index] == NULL) {
581 			/* return EAFNOSUPPORT? */
582 			bzero(&ifr->ifr_ifru.ifru_stat,
583 				sizeof(ifr->ifr_ifru.ifru_icmp6stat));
584 		} else
585 			ifr->ifr_ifru.ifru_icmp6stat =
586 				*icmp6_ifstat[ifp->if_index];
587 		break;
588 
589 #ifdef COMPAT_IN6IFIOCTL		/* should be unused */
590 	case SIOCSIFDSTADDR_IN6:
591 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
592 			return(EINVAL);
593 		oldaddr = ia->ia_dstaddr;
594 		ia->ia_dstaddr = ifr->ifr_dstaddr;
595 
596 		/* link-local index check */
597 		if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
598 			if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
599 				/* interface ID is not embedded by the user */
600 				ia->ia_dstaddr.sin6_addr.s6_addr16[1]
601 					= htons(ifp->if_index);
602 			} else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
603 				    htons(ifp->if_index)) {
604 				ia->ia_dstaddr = oldaddr;
605 				return(EINVAL);	/* ifid contradicts */
606 			}
607 		}
608 
609 		if (ifp->if_ioctl && (error = (ifp->if_ioctl)
610 				      (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
611 			ia->ia_dstaddr = oldaddr;
612 			return(error);
613 		}
614 		if (ia->ia_flags & IFA_ROUTE) {
615 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
616 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
617 			ia->ia_ifa.ifa_dstaddr =
618 				(struct sockaddr *)&ia->ia_dstaddr;
619 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
620 		}
621 		break;
622 
623 #endif
624 	case SIOCGIFALIFETIME_IN6:
625 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
626 		break;
627 
628 	case SIOCSIFALIFETIME_IN6:
629 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
630 		/* for sanity */
631 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
632 			ia->ia6_lifetime.ia6t_expire =
633 				time_second + ia->ia6_lifetime.ia6t_vltime;
634 		} else
635 			ia->ia6_lifetime.ia6t_expire = 0;
636 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
637 			ia->ia6_lifetime.ia6t_preferred =
638 				time_second + ia->ia6_lifetime.ia6t_pltime;
639 		} else
640 			ia->ia6_lifetime.ia6t_preferred = 0;
641 		break;
642 
643 	case SIOCSIFADDR_IN6:
644 		error = in6_ifinit(ifp, ia, &ifr->ifr_addr, 1);
645 #if 0
646 		/*
647 		 * the code chokes if we are to assign multiple addresses with
648 		 * the same address prefix (rtinit() will return EEXIST, which
649 		 * is not fatal actually).  we will get memory leak if we
650 		 * don't do it.
651 		 * -> we may want to hide EEXIST from rtinit().
652 		 */
653   undo:
654 		if (error && newifaddr) {
655 			TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
656 			IFAFREE(&ia->ia_ifa);
657 
658 			oia = ia;
659 			if (oia == (ia = in6_ifaddr))
660 				in6_ifaddr = ia->ia_next;
661 			else {
662 				while (ia->ia_next && (ia->ia_next != oia))
663 					ia = ia->ia_next;
664 				if (ia->ia_next)
665 					ia->ia_next = oia->ia_next;
666 				else {
667 					printf("Didn't unlink in6_ifaddr "
668 					    "from list\n");
669 				}
670 			}
671 			IFAFREE(&oia->ia_ifa);
672 		}
673 #endif
674 		return error;
675 
676 #ifdef COMPAT_IN6IFIOCTL		/* XXX should be unused */
677 	case SIOCSIFNETMASK_IN6:
678 		ia->ia_prefixmask = ifr->ifr_addr;
679 		bzero(&net, sizeof(net));
680 		net.sin6_len = sizeof(struct sockaddr_in6);
681 		net.sin6_family = AF_INET6;
682 		net.sin6_port = htons(0);
683 		net.sin6_flowinfo = htonl(0);
684 		net.sin6_addr.s6_addr32[0]
685 			= ia->ia_addr.sin6_addr.s6_addr32[0] &
686 				ia->ia_prefixmask.sin6_addr.s6_addr32[0];
687 		net.sin6_addr.s6_addr32[1]
688 			= ia->ia_addr.sin6_addr.s6_addr32[1] &
689 				ia->ia_prefixmask.sin6_addr.s6_addr32[1];
690 		net.sin6_addr.s6_addr32[2]
691 			= ia->ia_addr.sin6_addr.s6_addr32[2] &
692 				ia->ia_prefixmask.sin6_addr.s6_addr32[2];
693 		net.sin6_addr.s6_addr32[3]
694 			= ia->ia_addr.sin6_addr.s6_addr32[3] &
695 				ia->ia_prefixmask.sin6_addr.s6_addr32[3];
696 		ia->ia_net = net;
697 		break;
698 #endif
699 
700 	case SIOCAIFADDR_IN6:
701 		prefixIsNew = 0;
702 		hostIsNew = 1;
703 
704 		if (ifra->ifra_addr.sin6_len == 0) {
705 			ifra->ifra_addr = ia->ia_addr;
706 			hostIsNew = 0;
707 		} else if (IN6_ARE_ADDR_EQUAL(&ifra->ifra_addr.sin6_addr,
708 					      &ia->ia_addr.sin6_addr))
709 			hostIsNew = 0;
710 
711 		/* Validate address families: */
712 		/*
713 		 * The destination address for a p2p link must have a family
714 		 * of AF_UNSPEC or AF_INET6.
715 		 */
716 		if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
717 		    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
718 		    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
719 			return(EAFNOSUPPORT);
720 		/*
721 		 * The prefixmask must have a family of AF_UNSPEC or AF_INET6.
722 		 */
723 		if (ifra->ifra_prefixmask.sin6_family != AF_INET6 &&
724 		    ifra->ifra_prefixmask.sin6_family != AF_UNSPEC)
725 			return(EAFNOSUPPORT);
726 
727 		if (ifra->ifra_prefixmask.sin6_len) {
728 			in6_ifscrub(ifp, ia);
729 			ia->ia_prefixmask = ifra->ifra_prefixmask;
730 			prefixIsNew = 1;
731 		}
732 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
733 		    (ifra->ifra_dstaddr.sin6_family == AF_INET6)) {
734 			in6_ifscrub(ifp, ia);
735 			oldaddr = ia->ia_dstaddr;
736 			ia->ia_dstaddr = ifra->ifra_dstaddr;
737 			/* link-local index check: should be a separate function? */
738 			if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_dstaddr.sin6_addr)) {
739 				if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] == 0) {
740 					/*
741 					 * interface ID is not embedded by
742 					 * the user
743 					 */
744 					ia->ia_dstaddr.sin6_addr.s6_addr16[1]
745 						= htons(ifp->if_index);
746 				} else if (ia->ia_dstaddr.sin6_addr.s6_addr16[1] !=
747 					    htons(ifp->if_index)) {
748 					ia->ia_dstaddr = oldaddr;
749 					return(EINVAL);	/* ifid contradicts */
750 				}
751 			}
752 			prefixIsNew = 1; /* We lie; but effect's the same */
753 		}
754 		if (hostIsNew || prefixIsNew) {
755 			error = in6_ifinit(ifp, ia, &ifra->ifra_addr, 0);
756 #if 0
757 			if (error)
758 				goto undo;
759 #endif
760 		}
761 		if (hostIsNew && (ifp->if_flags & IFF_MULTICAST)) {
762 			int error_local = 0;
763 
764 			/*
765 			 * join solicited multicast addr for new host id
766 			 */
767 			struct in6_addr llsol;
768 			bzero(&llsol, sizeof(struct in6_addr));
769 			llsol.s6_addr16[0] = htons(0xff02);
770 			llsol.s6_addr16[1] = htons(ifp->if_index);
771 			llsol.s6_addr32[1] = 0;
772 			llsol.s6_addr32[2] = htonl(1);
773 			llsol.s6_addr32[3] =
774 				ifra->ifra_addr.sin6_addr.s6_addr32[3];
775 			llsol.s6_addr8[12] = 0xff;
776 			(void)in6_addmulti(&llsol, ifp, &error_local);
777 			if (error == 0)
778 				error = error_local;
779 		}
780 
781 		ia->ia6_flags = ifra->ifra_flags;
782 		ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/*safety*/
783 
784 		ia->ia6_lifetime = ifra->ifra_lifetime;
785 		/* for sanity */
786 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
787 			ia->ia6_lifetime.ia6t_expire =
788 				time_second + ia->ia6_lifetime.ia6t_vltime;
789 		} else
790 			ia->ia6_lifetime.ia6t_expire = 0;
791 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
792 			ia->ia6_lifetime.ia6t_preferred =
793 				time_second + ia->ia6_lifetime.ia6t_pltime;
794 		} else
795 			ia->ia6_lifetime.ia6t_preferred = 0;
796 
797 		/*
798 		 * make sure to initialize ND6 information.  this is to
799 		 * workaround issues with interfaces with IPv6 addresses,
800 		 * which have never brought # up.  we are assuming that it is
801 		 * safe to nd6_ifattach multiple times.
802 		 */
803 		nd6_ifattach(ifp);
804 
805 		/*
806 		 * Perform DAD, if needed.
807 		 * XXX It may be of use, if we can administratively
808 		 * disable DAD.
809 		 */
810 		switch (ifp->if_type) {
811 		case IFT_ARCNET:
812 		case IFT_ETHER:
813 		case IFT_FDDI:
814 		case IFT_IEEE1394:
815 #if 0
816 		case IFT_ATM:
817 		case IFT_SLIP:
818 		case IFT_PPP:
819 #endif
820 			ia->ia6_flags |= IN6_IFF_TENTATIVE;
821 			nd6_dad_start(&ia->ia_ifa, NULL);
822 			break;
823 		case IFT_FAITH:
824 		case IFT_GIF:
825 		case IFT_LOOP:
826 		default:
827 			break;
828 		}
829 
830 		if (hostIsNew) {
831 			int iilen;
832 			int error_local = 0;
833 
834 			iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) -
835 				in6_mask2len(&ia->ia_prefixmask.sin6_addr);
836 			error_local = in6_prefix_add_ifid(iilen, ia);
837 			if (error == 0)
838 				error = error_local;
839 		}
840 
841 		return(error);
842 
843 	case SIOCDIFADDR_IN6:
844 		in6_purgeaddr(&ia->ia_ifa, ifp);
845 		break;
846 
847 	default:
848 		if (ifp == NULL || ifp->if_ioctl == 0)
849 			return(EOPNOTSUPP);
850 		return((*ifp->if_ioctl)(ifp, cmd, data));
851 	}
852 	return(0);
853 }
854 
855 void
856 in6_purgeaddr(ifa, ifp)
857 	struct ifaddr *ifa;
858 	struct ifnet *ifp;
859 {
860 	struct in6_ifaddr *oia, *ia = (void *) ifa;
861 
862 	/* stop DAD processing */
863 	nd6_dad_stop(ifa);
864 
865 	in6_ifscrub(ifp, ia);
866 
867 	if (ifp->if_flags & IFF_MULTICAST) {
868 		/*
869 		 * delete solicited multicast addr for deleting host id
870 		 */
871 		struct in6_multi *in6m;
872 		struct in6_addr llsol;
873 		bzero(&llsol, sizeof(struct in6_addr));
874 		llsol.s6_addr16[0] = htons(0xff02);
875 		llsol.s6_addr16[1] = htons(ifp->if_index);
876 		llsol.s6_addr32[1] = 0;
877 		llsol.s6_addr32[2] = htonl(1);
878 		llsol.s6_addr32[3] =
879 			ia->ia_addr.sin6_addr.s6_addr32[3];
880 		llsol.s6_addr8[12] = 0xff;
881 
882 		IN6_LOOKUP_MULTI(llsol, ifp, in6m);
883 		if (in6m)
884 			in6_delmulti(in6m);
885 	}
886 
887 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
888 	IFAFREE(&ia->ia_ifa);
889 
890 	oia = ia;
891 	if (oia == (ia = in6_ifaddr))
892 		in6_ifaddr = ia->ia_next;
893 	else {
894 		while (ia->ia_next && (ia->ia_next != oia))
895 			ia = ia->ia_next;
896 		if (ia->ia_next)
897 			ia->ia_next = oia->ia_next;
898 		else
899 			printf("Didn't unlink in6_ifaddr from list\n");
900 	}
901 	{
902 		int iilen;
903 
904 		iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) -
905 			in6_mask2len(&oia->ia_prefixmask.sin6_addr);
906 		in6_prefix_remove_ifid(iilen, oia);
907 	}
908 	if (oia->ia6_multiaddrs.lh_first != NULL) {
909 		/*
910 		 * XXX thorpej@netbsd.org -- if the interface is going
911 		 * XXX away, don't save the multicast entries, delete them!
912 		 */
913 		if (oia->ia_ifa.ifa_ifp->if_output == if_nulloutput) {
914 			struct in6_multi *in6m;
915 
916 			while ((in6m =
917 			    LIST_FIRST(&oia->ia6_multiaddrs)) != NULL)
918 				in6_delmulti(in6m);
919 		} else
920 			in6_savemkludge(oia);
921 	}
922 
923 	IFAFREE(&oia->ia_ifa);
924 }
925 
926 void
927 in6_purgeif(ifp)
928 	struct ifnet *ifp;
929 {
930 	struct ifaddr *ifa, *nifa;
931 
932 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
933 		nifa = TAILQ_NEXT(ifa, ifa_list);
934 		if (ifa->ifa_addr->sa_family != AF_INET6)
935 			continue;
936 		in6_purgeaddr(ifa, ifp);
937 	}
938 
939 	in6_ifdetach(ifp);
940 }
941 
942 /*
943  * SIOC[GAD]LIFADDR.
944  *	SIOCGLIFADDR: get first address. (?)
945  *	SIOCGLIFADDR with IFLR_PREFIX:
946  *		get first address that matches the specified prefix.
947  *	SIOCALIFADDR: add the specified address.
948  *	SIOCALIFADDR with IFLR_PREFIX:
949  *		add the specified prefix, filling hostid part from
950  *		the first link-local address.  prefixlen must be <= 64.
951  *	SIOCDLIFADDR: delete the specified address.
952  *	SIOCDLIFADDR with IFLR_PREFIX:
953  *		delete the first address that matches the specified prefix.
954  * return values:
955  *	EINVAL on invalid parameters
956  *	EADDRNOTAVAIL on prefix match failed/specified address not found
957  *	other values may be returned from in6_ioctl()
958  *
959  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
960  * this is to accomodate address naming scheme other than RFC2374,
961  * in the future.
962  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
963  * address encoding scheme. (see figure on page 8)
964  */
965 static int
966 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
967 	struct socket *so;
968 	u_long cmd;
969 	caddr_t	data;
970 	struct ifnet *ifp;
971 	struct proc *p;
972 {
973 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
974 	struct ifaddr *ifa;
975 	struct sockaddr *sa;
976 
977 	/* sanity checks */
978 	if (!data || !ifp) {
979 		panic("invalid argument to in6_lifaddr_ioctl");
980 		/*NOTRECHED*/
981 	}
982 
983 	switch (cmd) {
984 	case SIOCGLIFADDR:
985 		/* address must be specified on GET with IFLR_PREFIX */
986 		if ((iflr->flags & IFLR_PREFIX) == 0)
987 			break;
988 		/*FALLTHROUGH*/
989 	case SIOCALIFADDR:
990 	case SIOCDLIFADDR:
991 		/* address must be specified on ADD and DELETE */
992 		sa = (struct sockaddr *)&iflr->addr;
993 		if (sa->sa_family != AF_INET6)
994 			return EINVAL;
995 		if (sa->sa_len != sizeof(struct sockaddr_in6))
996 			return EINVAL;
997 		/* XXX need improvement */
998 		sa = (struct sockaddr *)&iflr->dstaddr;
999 		if (sa->sa_family && sa->sa_family != AF_INET6)
1000 			return EINVAL;
1001 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1002 			return EINVAL;
1003 		break;
1004 	default: /*shouldn't happen*/
1005 #if 0
1006 		panic("invalid cmd to in6_lifaddr_ioctl");
1007 		/*NOTREACHED*/
1008 #else
1009 		return EOPNOTSUPP;
1010 #endif
1011 	}
1012 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1013 		return EINVAL;
1014 
1015 	switch (cmd) {
1016 	case SIOCALIFADDR:
1017 	    {
1018 		struct in6_aliasreq ifra;
1019 		struct in6_addr *hostid = NULL;
1020 		int prefixlen;
1021 
1022 		if ((iflr->flags & IFLR_PREFIX) != 0) {
1023 			struct sockaddr_in6 *sin6;
1024 
1025 			/*
1026 			 * hostid is to fill in the hostid part of the
1027 			 * address.  hostid points to the first link-local
1028 			 * address attached to the interface.
1029 			 */
1030 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1031 			if (!ifa)
1032 				return EADDRNOTAVAIL;
1033 			hostid = IFA_IN6(ifa);
1034 
1035 		 	/* prefixlen must be <= 64. */
1036 			if (64 < iflr->prefixlen)
1037 				return EINVAL;
1038 			prefixlen = iflr->prefixlen;
1039 
1040 			/* hostid part must be zero. */
1041 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1042 			if (sin6->sin6_addr.s6_addr32[2] != 0
1043 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
1044 				return EINVAL;
1045 			}
1046 		} else
1047 			prefixlen = iflr->prefixlen;
1048 
1049 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1050 		bzero(&ifra, sizeof(ifra));
1051 		bcopy(iflr->iflr_name, ifra.ifra_name,
1052 			sizeof(ifra.ifra_name));
1053 
1054 		bcopy(&iflr->addr, &ifra.ifra_addr,
1055 			((struct sockaddr *)&iflr->addr)->sa_len);
1056 		if (hostid) {
1057 			/* fill in hostid part */
1058 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1059 				hostid->s6_addr32[2];
1060 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1061 				hostid->s6_addr32[3];
1062 		}
1063 
1064 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
1065 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1066 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
1067 			if (hostid) {
1068 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1069 					hostid->s6_addr32[2];
1070 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1071 					hostid->s6_addr32[3];
1072 			}
1073 		}
1074 
1075 		ifra.ifra_prefixmask.sin6_family = AF_INET6;
1076 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1077 		in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1078 
1079 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1080 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1081 	    }
1082 	case SIOCGLIFADDR:
1083 	case SIOCDLIFADDR:
1084 	    {
1085 		struct in6_ifaddr *ia;
1086 		struct in6_addr mask, candidate, match;
1087 		struct sockaddr_in6 *sin6;
1088 		int cmp;
1089 
1090 		bzero(&mask, sizeof(mask));
1091 		if (iflr->flags & IFLR_PREFIX) {
1092 			/* lookup a prefix rather than address. */
1093 			in6_len2mask(&mask, iflr->prefixlen);
1094 
1095 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1096 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
1097 			match.s6_addr32[0] &= mask.s6_addr32[0];
1098 			match.s6_addr32[1] &= mask.s6_addr32[1];
1099 			match.s6_addr32[2] &= mask.s6_addr32[2];
1100 			match.s6_addr32[3] &= mask.s6_addr32[3];
1101 
1102 			/* if you set extra bits, that's wrong */
1103 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1104 				return EINVAL;
1105 
1106 			cmp = 1;
1107 		} else {
1108 			if (cmd == SIOCGLIFADDR) {
1109 				/* on getting an address, take the 1st match */
1110 				cmp = 0;	/*XXX*/
1111 			} else {
1112 				/* on deleting an address, do exact match */
1113 				in6_len2mask(&mask, 128);
1114 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
1115 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
1116 
1117 				cmp = 1;
1118 			}
1119 		}
1120 
1121 		for (ifa = ifp->if_addrlist.tqh_first;
1122 		     ifa;
1123 		     ifa = ifa->ifa_list.tqe_next)
1124 		{
1125 			if (ifa->ifa_addr->sa_family != AF_INET6)
1126 				continue;
1127 			if (!cmp)
1128 				break;
1129 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1130 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
1131 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
1132 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
1133 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
1134 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1135 				break;
1136 		}
1137 		if (!ifa)
1138 			return EADDRNOTAVAIL;
1139 		ia = ifa2ia6(ifa);
1140 
1141 		if (cmd == SIOCGLIFADDR) {
1142 			/* fill in the if_laddrreq structure */
1143 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1144 
1145 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1146 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1147 					ia->ia_dstaddr.sin6_len);
1148 			} else
1149 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1150 
1151 			iflr->prefixlen =
1152 				in6_mask2len(&ia->ia_prefixmask.sin6_addr);
1153 
1154 			iflr->flags = ia->ia6_flags;	/*XXX*/
1155 
1156 			return 0;
1157 		} else {
1158 			struct in6_aliasreq ifra;
1159 
1160 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1161 			bzero(&ifra, sizeof(ifra));
1162 			bcopy(iflr->iflr_name, ifra.ifra_name,
1163 				sizeof(ifra.ifra_name));
1164 
1165 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1166 				ia->ia_addr.sin6_len);
1167 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1168 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1169 					ia->ia_dstaddr.sin6_len);
1170 			} else {
1171 				bzero(&ifra.ifra_dstaddr,
1172 				    sizeof(ifra.ifra_dstaddr));
1173 			}
1174 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1175 				ia->ia_prefixmask.sin6_len);
1176 
1177 			ifra.ifra_flags = ia->ia6_flags;
1178 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1179 				ifp, p);
1180 		}
1181 	    }
1182 	}
1183 
1184 	return EOPNOTSUPP;	/*just for safety*/
1185 }
1186 
1187 /*
1188  * Delete any existing route for an interface.
1189  */
1190 void
1191 in6_ifscrub(ifp, ia)
1192 	struct ifnet *ifp;
1193 	struct in6_ifaddr *ia;
1194 {
1195 	if ((ia->ia_flags & IFA_ROUTE) == 0)
1196 		return;
1197 
1198 	/*
1199 	 * We should check the existence of dstaddr, because link-local
1200 	 * addresses can be configured without particular destinations
1201 	 * even on point-to-point or loopback interfaces.
1202 	 * In this case, kernel would panic in rtinit()...
1203 	 */
1204 	if (ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT) &&
1205 	    (ia->ia_ifa.ifa_dstaddr != NULL))
1206 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
1207 	else
1208 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
1209 	ia->ia_flags &= ~IFA_ROUTE;
1210 
1211 	/* Remove ownaddr's loopback rtentry, if it exists. */
1212 	in6_ifremloop(&(ia->ia_ifa));
1213 }
1214 
1215 /*
1216  * Initialize an interface's intetnet6 address
1217  * and routing table entry.
1218  */
1219 int
1220 in6_ifinit(ifp, ia, sin6, scrub)
1221 	struct ifnet *ifp;
1222 	struct in6_ifaddr *ia;
1223 	struct sockaddr_in6 *sin6;
1224 	int scrub;
1225 {
1226 	struct	sockaddr_in6 oldaddr;
1227 	int	error, flags = RTF_UP;
1228 	int	s = splnet();
1229 
1230 	oldaddr = ia->ia_addr;
1231 	ia->ia_addr = *sin6;
1232 	/*
1233 	 * Give the interface a chance to initialize
1234 	 * if this is its first address,
1235 	 * and to validate the address if necessary.
1236 	 */
1237 	if (ifp->if_ioctl &&
1238 	   (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1239 		splx(s);
1240 		ia->ia_addr = oldaddr;
1241 		return(error);
1242 	}
1243 
1244 	switch (ifp->if_type) {
1245 	case IFT_ARCNET:
1246 	case IFT_ETHER:
1247 	case IFT_FDDI:
1248 	case IFT_IEEE1394:
1249 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1250 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
1251 		break;
1252 	case IFT_PPP:
1253 		ia->ia_ifa.ifa_rtrequest = nd6_p2p_rtrequest;
1254 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
1255 		break;
1256 	}
1257 
1258 	splx(s);
1259 	if (scrub) {
1260 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1261 		in6_ifscrub(ifp, ia);
1262 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1263 	}
1264 	/* xxx
1265 	 * in_socktrim
1266 	 */
1267 	/*
1268 	 * Add route for the network.
1269 	 */
1270 	ia->ia_ifa.ifa_metric = ifp->if_metric;
1271 	if (ifp->if_flags & IFF_LOOPBACK) {
1272 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1273 		flags |= RTF_HOST;
1274 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
1275 		if (ia->ia_dstaddr.sin6_family != AF_INET6)
1276 			return(0);
1277 		flags |= RTF_HOST;
1278 	}
1279 	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
1280 		ia->ia_flags |= IFA_ROUTE;
1281 	/* XXX check if the subnet route points to the same interface */
1282 	if (error == EEXIST)
1283 		error = 0;
1284 
1285 	/* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1286 	in6_ifaddloop(&(ia->ia_ifa));
1287 
1288 	if (ifp->if_flags & IFF_MULTICAST)
1289 		in6_restoremkludge(ia, ifp);
1290 
1291 	return(error);
1292 }
1293 
1294 /*
1295  * Multicast address kludge:
1296  * If there were any multicast addresses attached to this interface address,
1297  * either move them to another address on this interface, or save them until
1298  * such time as this interface is reconfigured for IPv6.
1299  */
1300 void
1301 in6_savemkludge(oia)
1302 	struct in6_ifaddr *oia;
1303 {
1304 	struct in6_ifaddr *ia;
1305 	struct in6_multi *in6m, *next;
1306 
1307 	IFP_TO_IA6(oia->ia_ifp, ia);
1308 	if (ia) {	/* there is another address */
1309 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1310 			next = in6m->in6m_entry.le_next;
1311 			IFAFREE(&in6m->in6m_ia->ia_ifa);
1312 			IFAREF(&ia->ia_ifa);
1313 			in6m->in6m_ia = ia;
1314 			LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1315 		}
1316 	} else {	/* last address on this if deleted, save */
1317 		struct multi6_kludge *mk;
1318 
1319 		for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1320 			if (mk->mk_ifp == oia->ia_ifp)
1321 				break;
1322 		}
1323 		if (mk == NULL) /* this should not happen! */
1324 			panic("in6_savemkludge: no kludge space");
1325 
1326 		for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next){
1327 			next = in6m->in6m_entry.le_next;
1328 			IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1329 			in6m->in6m_ia = NULL;
1330 			LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
1331 		}
1332 	}
1333 }
1334 
1335 /*
1336  * Continuation of multicast address hack:
1337  * If there was a multicast group list previously saved for this interface,
1338  * then we re-attach it to the first address configured on the i/f.
1339  */
1340 void
1341 in6_restoremkludge(ia, ifp)
1342 	struct in6_ifaddr *ia;
1343 	struct ifnet *ifp;
1344 {
1345 	struct multi6_kludge *mk;
1346 
1347 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1348 		if (mk->mk_ifp == ifp) {
1349 			struct in6_multi *in6m, *next;
1350 
1351 			for (in6m = mk->mk_head.lh_first; in6m; in6m = next) {
1352 				next = in6m->in6m_entry.le_next;
1353 				in6m->in6m_ia = ia;
1354 				IFAREF(&ia->ia_ifa);
1355 				LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
1356 						 in6m, in6m_entry);
1357 			}
1358 			LIST_INIT(&mk->mk_head);
1359 			break;
1360 		}
1361 	}
1362 }
1363 
1364 /*
1365  * Allocate space for the kludge at interface initialization time.
1366  * Formerly, we dynamically allocated the space in in6_savemkludge() with
1367  * malloc(M_WAITOK).  However, it was wrong since the function could be called
1368  * under an interrupt context (software timer on address lifetime expiration).
1369  * Also, we cannot just give up allocating the strucutre, since the group
1370  * membership structure is very complex and we need to keep it anyway.
1371  * Of course, this function MUST NOT be called under an interrupt context.
1372  * Specifically, it is expected to be called only from in6_ifattach(), though
1373  * it is a global function.
1374  */
1375 void
1376 in6_createmkludge(ifp)
1377 	struct ifnet *ifp;
1378 {
1379 	struct multi6_kludge *mk;
1380 
1381 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1382 		/* If we've already had one, do not allocate. */
1383 		if (mk->mk_ifp == ifp)
1384 			return;
1385 	}
1386 
1387 	mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
1388 
1389 	bzero(mk, sizeof(*mk));
1390 	LIST_INIT(&mk->mk_head);
1391 	mk->mk_ifp = ifp;
1392 	LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
1393 }
1394 
1395 void
1396 in6_purgemkludge(ifp)
1397 	struct ifnet *ifp;
1398 {
1399 	struct multi6_kludge *mk;
1400 	struct in6_multi *in6m;
1401 
1402 	for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
1403 		if (mk->mk_ifp != ifp)
1404 			continue;
1405 
1406 		/* leave from all multicast groups joined */
1407 		while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL)
1408 			in6_delmulti(in6m);
1409 		LIST_REMOVE(mk, mk_entry);
1410 		free(mk, M_IPMADDR);
1411 		break;
1412 	}
1413 }
1414 
1415 /*
1416  * Add an address to the list of IP6 multicast addresses for a
1417  * given interface.
1418  */
1419 struct	in6_multi *
1420 in6_addmulti(maddr6, ifp, errorp)
1421 	struct in6_addr *maddr6;
1422 	struct ifnet *ifp;
1423 	int *errorp;
1424 {
1425 	struct	in6_ifaddr *ia;
1426 	struct	in6_ifreq ifr;
1427 	struct	in6_multi *in6m;
1428 	int	s = splsoftnet();
1429 
1430 	*errorp = 0;
1431 	/*
1432 	 * See if address already in list.
1433 	 */
1434 	IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1435 	if (in6m != NULL) {
1436 		/*
1437 		 * Found it; just increment the refrence count.
1438 		 */
1439 		in6m->in6m_refcount++;
1440 	} else {
1441 		/*
1442 		 * New address; allocate a new multicast record
1443 		 * and link it into the interface's multicast list.
1444 		 */
1445 		in6m = (struct in6_multi *)
1446 			malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1447 		if (in6m == NULL) {
1448 			splx(s);
1449 			*errorp = ENOBUFS;
1450 			return(NULL);
1451 		}
1452 		in6m->in6m_addr = *maddr6;
1453 		in6m->in6m_ifp = ifp;
1454 		in6m->in6m_refcount = 1;
1455 		IFP_TO_IA6(ifp, ia);
1456 		if (ia == NULL) {
1457 			free(in6m, M_IPMADDR);
1458 			splx(s);
1459 			*errorp = EADDRNOTAVAIL; /* appropriate? */
1460 			return(NULL);
1461 		}
1462 		in6m->in6m_ia = ia;
1463 		IFAREF(&ia->ia_ifa); /* gain a reference */
1464 		LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
1465 
1466 		/*
1467 		 * Ask the network driver to update its multicast reception
1468 		 * filter appropriately for the new address.
1469 		 */
1470 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1471 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1472 		ifr.ifr_addr.sin6_family = AF_INET6;
1473 		ifr.ifr_addr.sin6_addr = *maddr6;
1474 		if (ifp->if_ioctl == NULL)
1475 			*errorp = ENXIO; /* XXX: appropriate? */
1476 		else
1477 			*errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
1478 						    (caddr_t)&ifr);
1479 		if (*errorp) {
1480 			LIST_REMOVE(in6m, in6m_entry);
1481 			free(in6m, M_IPMADDR);
1482 			IFAFREE(&ia->ia_ifa);
1483 			splx(s);
1484 			return(NULL);
1485 		}
1486 		/*
1487 		 * Let MLD6 know that we have joined a new IP6 multicast
1488 		 * group.
1489 		 */
1490 		mld6_start_listening(in6m);
1491 	}
1492 	splx(s);
1493 	return(in6m);
1494 }
1495 
1496 /*
1497  * Delete a multicast address record.
1498  */
1499 void
1500 in6_delmulti(in6m)
1501 	struct in6_multi *in6m;
1502 {
1503 	struct	in6_ifreq ifr;
1504 	int	s = splsoftnet();
1505 
1506 	if (--in6m->in6m_refcount == 0) {
1507 		/*
1508 		 * No remaining claims to this record; let MLD6 know
1509 		 * that we are leaving the multicast group.
1510 		 */
1511 		mld6_stop_listening(in6m);
1512 
1513 		/*
1514 		 * Unlink from list.
1515 		 */
1516 		LIST_REMOVE(in6m, in6m_entry);
1517 		if (in6m->in6m_ia) {
1518 			IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1519 		}
1520 
1521 		/*
1522 		 * Notify the network driver to update its multicast
1523 		 * reception filter.
1524 		 */
1525 		bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1526 		ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1527 		ifr.ifr_addr.sin6_family = AF_INET6;
1528 		ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1529 		(*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
1530 					    SIOCDELMULTI, (caddr_t)&ifr);
1531 		free(in6m, M_IPMADDR);
1532 	}
1533 	splx(s);
1534 }
1535 
1536 /*
1537  * Find an IPv6 interface link-local address specific to an interface.
1538  */
1539 struct in6_ifaddr *
1540 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1541 	struct ifnet *ifp;
1542 	int ignoreflags;
1543 {
1544 	struct ifaddr *ifa;
1545 
1546 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1547 	{
1548 		if (ifa->ifa_addr == NULL)
1549 			continue;	/* just for safety */
1550 		if (ifa->ifa_addr->sa_family != AF_INET6)
1551 			continue;
1552 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1553 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1554 			     ignoreflags) != 0)
1555 				continue;
1556 			break;
1557 		}
1558 	}
1559 
1560 	return((struct in6_ifaddr *)ifa);
1561 }
1562 
1563 
1564 /*
1565  * find the internet address corresponding to a given interface and address.
1566  */
1567 struct in6_ifaddr *
1568 in6ifa_ifpwithaddr(ifp, addr)
1569 	struct ifnet *ifp;
1570 	struct in6_addr *addr;
1571 {
1572 	struct ifaddr *ifa;
1573 
1574 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
1575 	{
1576 		if (ifa->ifa_addr == NULL)
1577 			continue;	/* just for safety */
1578 		if (ifa->ifa_addr->sa_family != AF_INET6)
1579 			continue;
1580 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1581 			break;
1582 	}
1583 
1584 	return((struct in6_ifaddr *)ifa);
1585 }
1586 
1587 /*
1588  * Convert IP6 address to printable (loggable) representation.
1589  */
1590 static char digits[] = "0123456789abcdef";
1591 static int ip6round = 0;
1592 char *
1593 ip6_sprintf(addr)
1594 	const struct in6_addr *addr;
1595 {
1596 	static char ip6buf[8][48];
1597 	int i;
1598 	char *cp;
1599 	u_short *a = (u_short *)addr;
1600 	u_char *d;
1601 	int dcolon = 0;
1602 
1603 	ip6round = (ip6round + 1) & 7;
1604 	cp = ip6buf[ip6round];
1605 
1606 	for (i = 0; i < 8; i++) {
1607 		if (dcolon == 1) {
1608 			if (*a == 0) {
1609 				if (i == 7)
1610 					*cp++ = ':';
1611 				a++;
1612 				continue;
1613 			} else
1614 				dcolon = 2;
1615 		}
1616 		if (*a == 0) {
1617 			if (dcolon == 0 && *(a + 1) == 0) {
1618 				if (i == 0)
1619 					*cp++ = ':';
1620 				*cp++ = ':';
1621 				dcolon = 1;
1622 			} else {
1623 				*cp++ = '0';
1624 				*cp++ = ':';
1625 			}
1626 			a++;
1627 			continue;
1628 		}
1629 		d = (u_char *)a;
1630 		*cp++ = digits[*d >> 4];
1631 		*cp++ = digits[*d++ & 0xf];
1632 		*cp++ = digits[*d >> 4];
1633 		*cp++ = digits[*d & 0xf];
1634 		*cp++ = ':';
1635 		a++;
1636 	}
1637 	*--cp = 0;
1638 	return(ip6buf[ip6round]);
1639 }
1640 
1641 int
1642 in6_localaddr(in6)
1643 	struct in6_addr *in6;
1644 {
1645 	struct in6_ifaddr *ia;
1646 
1647 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1648 		return 1;
1649 
1650 	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1651 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1652 					      &ia->ia_prefixmask.sin6_addr))
1653 			return 1;
1654 
1655 	return (0);
1656 }
1657 
1658 /*
1659  * Get a scope of the address. Node-local, link-local, site-local or global.
1660  */
1661 int
1662 in6_addrscope (addr)
1663 struct in6_addr *addr;
1664 {
1665 	int scope;
1666 
1667 	if (addr->s6_addr8[0] == 0xfe) {
1668 		scope = addr->s6_addr8[1] & 0xc0;
1669 
1670 		switch (scope) {
1671 		case 0x80:
1672 			return IPV6_ADDR_SCOPE_LINKLOCAL;
1673 			break;
1674 		case 0xc0:
1675 			return IPV6_ADDR_SCOPE_SITELOCAL;
1676 			break;
1677 		default:
1678 			return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1679 			break;
1680 		}
1681 	}
1682 
1683 
1684 	if (addr->s6_addr8[0] == 0xff) {
1685 		scope = addr->s6_addr8[1] & 0x0f;
1686 
1687 		/*
1688 		 * due to other scope such as reserved,
1689 		 * return scope doesn't work.
1690 		 */
1691 		switch (scope) {
1692 		case IPV6_ADDR_SCOPE_NODELOCAL:
1693 			return IPV6_ADDR_SCOPE_NODELOCAL;
1694 			break;
1695 		case IPV6_ADDR_SCOPE_LINKLOCAL:
1696 			return IPV6_ADDR_SCOPE_LINKLOCAL;
1697 			break;
1698 		case IPV6_ADDR_SCOPE_SITELOCAL:
1699 			return IPV6_ADDR_SCOPE_SITELOCAL;
1700 			break;
1701 		default:
1702 			return IPV6_ADDR_SCOPE_GLOBAL;
1703 			break;
1704 		}
1705 	}
1706 
1707 	if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
1708 		if (addr->s6_addr8[15] == 1) /* loopback */
1709 			return IPV6_ADDR_SCOPE_NODELOCAL;
1710 		if (addr->s6_addr8[15] == 0) /* unspecified */
1711 			return IPV6_ADDR_SCOPE_LINKLOCAL;
1712 	}
1713 
1714 	return IPV6_ADDR_SCOPE_GLOBAL;
1715 }
1716 
1717 int
1718 in6_addr2scopeid(ifp, addr)
1719 	struct ifnet *ifp;	/* must not be NULL */
1720 	struct in6_addr *addr;	/* must not be NULL */
1721 {
1722 	int scope = in6_addrscope(addr);
1723 
1724 	switch(scope) {
1725 	case IPV6_ADDR_SCOPE_NODELOCAL:
1726 		return(-1);	/* XXX: is this an appropriate value? */
1727 
1728 	case IPV6_ADDR_SCOPE_LINKLOCAL:
1729 		/* XXX: we do not distinguish between a link and an I/F. */
1730 		return(ifp->if_index);
1731 
1732 	case IPV6_ADDR_SCOPE_SITELOCAL:
1733 		return(0);	/* XXX: invalid. */
1734 
1735 	default:
1736 		return(0);	/* XXX: treat as global. */
1737 	}
1738 }
1739 
1740 /*
1741  * return length of part which dst and src are equal
1742  * hard coding...
1743  */
1744 int
1745 in6_matchlen(src, dst)
1746 struct in6_addr *src, *dst;
1747 {
1748 	int match = 0;
1749 	u_char *s = (u_char *)src, *d = (u_char *)dst;
1750 	u_char *lim = s + 16, r;
1751 
1752 	while (s < lim)
1753 		if ((r = (*d++ ^ *s++)) != 0) {
1754 			while (r < 128) {
1755 				match++;
1756 				r <<= 1;
1757 			}
1758 			break;
1759 		} else
1760 			match += 8;
1761 	return match;
1762 }
1763 
1764 int
1765 in6_are_prefix_equal(p1, p2, len)
1766 	struct in6_addr *p1, *p2;
1767 	int len;
1768 {
1769 	int bytelen, bitlen;
1770 
1771 	/* sanity check */
1772 	if (0 > len || len > 128) {
1773 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1774 		    len);
1775 		return(0);
1776 	}
1777 
1778 	bytelen = len / 8;
1779 	bitlen = len % 8;
1780 
1781 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1782 		return(0);
1783 	if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1784 	    p2->s6_addr[bytelen] >> (8 - bitlen))
1785 		return(0);
1786 
1787 	return(1);
1788 }
1789 
1790 void
1791 in6_prefixlen2mask(maskp, len)
1792 	struct in6_addr *maskp;
1793 	int len;
1794 {
1795 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1796 	int bytelen, bitlen, i;
1797 
1798 	/* sanity check */
1799 	if (0 > len || len > 128) {
1800 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1801 		    len);
1802 		return;
1803 	}
1804 
1805 	bzero(maskp, sizeof(*maskp));
1806 	bytelen = len / 8;
1807 	bitlen = len % 8;
1808 	for (i = 0; i < bytelen; i++)
1809 		maskp->s6_addr[i] = 0xff;
1810 	if (bitlen)
1811 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1812 }
1813 
1814 /*
1815  * return the best address out of the same scope
1816  */
1817 struct in6_ifaddr *
1818 in6_ifawithscope(oifp, dst)
1819 	struct ifnet *oifp;
1820 	struct in6_addr *dst;
1821 {
1822 	int dst_scope =	in6_addrscope(dst), src_scope, best_scope = 0;
1823 	int blen = -1;
1824 	struct ifaddr *ifa;
1825 	struct ifnet *ifp;
1826 	struct in6_ifaddr *ifa_best = NULL;
1827 
1828 	if (oifp == NULL) {
1829 		printf("in6_ifawithscope: output interface is not specified\n");
1830 		return(NULL);
1831 	}
1832 
1833 	/*
1834 	 * We search for all addresses on all interfaces from the beginning.
1835 	 * Comparing an interface with the outgoing interface will be done
1836 	 * only at the final stage of tiebreaking.
1837 	 */
1838 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1839 	{
1840 		/*
1841 		 * We can never take an address that breaks the scope zone
1842 		 * of the destination.
1843 		 */
1844 		if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1845 			continue;
1846 
1847 		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1848 		     ifa = ifa->ifa_list.tqe_next)
1849 		{
1850 			int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1851 
1852 			if (ifa->ifa_addr->sa_family != AF_INET6)
1853 				continue;
1854 
1855 			src_scope = in6_addrscope(IFA_IN6(ifa));
1856 
1857 #ifdef ADDRSELECT_DEBUG		/* should be removed after stabilization */
1858 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
1859 			printf("in6_ifawithscope: dst=%s bestaddr=%s, "
1860 			       "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, "
1861 			       "matchlen=%d, flgs=%x\n",
1862 			       ip6_sprintf(dst),
1863 			       ifa_best ? ip6_sprintf(&ifa_best->ia_addr.sin6_addr) : "none",
1864 			       ip6_sprintf(IFA_IN6(ifa)), src_scope,
1865 			       dscopecmp,
1866 			       ifa_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1,
1867 			       in6_matchlen(IFA_IN6(ifa), dst),
1868 			       ((struct in6_ifaddr *)ifa)->ia6_flags);
1869 #endif
1870 
1871 			/*
1872 			 * Don't use an address before completing DAD
1873 			 * nor a duplicated address.
1874 			 */
1875 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1876 			    IN6_IFF_NOTREADY)
1877 				continue;
1878 
1879 			/* XXX: is there any case to allow anycasts? */
1880 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1881 			    IN6_IFF_ANYCAST)
1882 				continue;
1883 
1884 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1885 			    IN6_IFF_DETACHED)
1886 				continue;
1887 
1888 			/*
1889 			 * If this is the first address we find,
1890 			 * keep it anyway.
1891 			 */
1892 			if (ifa_best == NULL)
1893 				goto replace;
1894 
1895 			/*
1896 			 * ifa_best is never NULL beyond this line except
1897 			 * within the block labeled "replace".
1898 			 */
1899 
1900 			/*
1901 			 * If ifa_best has a smaller scope than dst and
1902 			 * the current address has a larger one than
1903 			 * (or equal to) dst, always replace ifa_best.
1904 			 * Also, if the current address has a smaller scope
1905 			 * than dst, ignore it unless ifa_best also has a
1906 			 * smaller scope.
1907 			 */
1908 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1909 			    IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1910 				goto replace;
1911 			if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
1912 			    IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
1913 				continue;
1914 
1915 			/*
1916 			 * A deprecated address SHOULD NOT be used in new
1917 			 * communications if an alternate (non-deprecated)
1918 			 * address is available and has sufficient scope.
1919 			 * RFC 2462, Section 5.5.4.
1920 			 */
1921 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1922 			    IN6_IFF_DEPRECATED) {
1923 				/*
1924 				 * Ignore any deprecated addresses if
1925 				 * specified by configuration.
1926 				 */
1927 				if (!ip6_use_deprecated)
1928 					continue;
1929 
1930 				/*
1931 				 * If we have already found a non-deprecated
1932 				 * candidate, just ignore deprecated addresses.
1933 				 */
1934 				if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
1935 				    == 0)
1936 					continue;
1937 			}
1938 
1939 			/*
1940 			 * A non-deprecated address is always preferred
1941 			 * to a deprecated one regardless of scopes and
1942 			 * address matching.
1943 			 */
1944 			if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
1945 			    (((struct in6_ifaddr *)ifa)->ia6_flags &
1946 			     IN6_IFF_DEPRECATED) == 0)
1947 				goto replace;
1948 
1949 			/*
1950 			 * At this point, we have two cases:
1951 			 * 1. we are looking at a non-deprecated address,
1952 			 *    and ifa_best is also non-deprecated.
1953 			 * 2. we are looking at a deprecated address,
1954 			 *    and ifa_best is also deprecated.
1955 			 * Also, we do not have to consider a case where
1956 			 * the scope of if_best is larger(smaller) than dst and
1957 			 * the scope of the current address is smaller(larger)
1958 			 * than dst. Such a case has already been covered.
1959 			 * Tiebreaking is done according to the following
1960 			 * items:
1961 			 * - the scope comparison between the address and
1962 			 *   dst (dscopecmp)
1963 			 * - the scope comparison between the address and
1964 			 *   ifa_best (bscopecmp)
1965 			 * - if the address match dst longer than ifa_best
1966 			 *   (matchcmp)
1967 			 * - if the address is on the outgoing I/F (outI/F)
1968 			 *
1969 			 * Roughly speaking, the selection policy is
1970 			 * - the most important item is scope. The same scope
1971 			 *   is best. Then search for a larger scope.
1972 			 *   Smaller scopes are the last resort.
1973 			 * - A deprecated address is chosen only when we have
1974 			 *   no address that has an enough scope, but is
1975 			 *   prefered to any addresses of smaller scopes.
1976 			 * - Longest address match against dst is considered
1977 			 *   only for addresses that has the same scope of dst.
1978 			 * - If there is no other reasons to choose one,
1979 			 *   addresses on the outgoing I/F are preferred.
1980 			 *
1981 			 * The precise decision table is as follows:
1982 			 * dscopecmp bscopecmp matchcmp outI/F | replace?
1983 			 *    !equal     equal      N/A    Yes |      Yes (1)
1984 			 *    !equal     equal      N/A     No |       No (2)
1985 			 *    larger    larger      N/A    N/A |       No (3)
1986 			 *    larger   smaller      N/A    N/A |      Yes (4)
1987 			 *   smaller    larger      N/A    N/A |      Yes (5)
1988 			 *   smaller   smaller      N/A    N/A |       No (6)
1989 			 *     equal   smaller      N/A    N/A |      Yes (7)
1990 			 *     equal    larger       (already done)
1991 			 *     equal     equal   larger    N/A |      Yes (8)
1992 			 *     equal     equal  smaller    N/A |       No (9)
1993 			 *     equal     equal    equal    Yes |      Yes (a)
1994 			 *     eaual     eqaul    equal     No |       No (b)
1995 			 */
1996 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
1997 			bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
1998 
1999 			if (dscopecmp && bscopecmp == 0) {
2000 				if (oifp == ifp) /* (1) */
2001 					goto replace;
2002 				continue; /* (2) */
2003 			}
2004 			if (dscopecmp > 0) {
2005 				if (bscopecmp > 0) /* (3) */
2006 					continue;
2007 				goto replace; /* (4) */
2008 			}
2009 			if (dscopecmp < 0) {
2010 				if (bscopecmp > 0) /* (5) */
2011 					goto replace;
2012 				continue; /* (6) */
2013 			}
2014 
2015 			/* now dscopecmp must be 0 */
2016 			if (bscopecmp < 0)
2017 				goto replace; /* (7) */
2018 
2019 			/*
2020 			 * At last both dscopecmp and bscopecmp must be 0.
2021 			 * We need address matching against dst for
2022 			 * tiebreaking.
2023 			 */
2024 			tlen = in6_matchlen(IFA_IN6(ifa), dst);
2025 			matchcmp = tlen - blen;
2026 			if (matchcmp > 0) /* (8) */
2027 				goto replace;
2028 			if (matchcmp < 0) /* (9) */
2029 				continue;
2030 			if (oifp == ifp) /* (a) */
2031 				goto replace;
2032 			continue; /* (b) */
2033 
2034 		  replace:
2035 			ifa_best = (struct in6_ifaddr *)ifa;
2036 			blen = tlen >= 0 ? tlen :
2037 				in6_matchlen(IFA_IN6(ifa), dst);
2038 			best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2039 		}
2040 	}
2041 
2042 	/* count statistics for future improvements */
2043 	if (ifa_best == NULL)
2044 		ip6stat.ip6s_sources_none++;
2045 	else {
2046 		if (oifp == ifa_best->ia_ifp)
2047 			ip6stat.ip6s_sources_sameif[best_scope]++;
2048 		else
2049 			ip6stat.ip6s_sources_otherif[best_scope]++;
2050 
2051 		if (best_scope == dst_scope)
2052 			ip6stat.ip6s_sources_samescope[best_scope]++;
2053 		else
2054 			ip6stat.ip6s_sources_otherscope[best_scope]++;
2055 
2056 		if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2057 			ip6stat.ip6s_sources_deprecated[best_scope]++;
2058 	}
2059 
2060 	return(ifa_best);
2061 }
2062 
2063 /*
2064  * return the best address out of the same scope. if no address was
2065  * found, return the first valid address from designated IF.
2066  */
2067 struct in6_ifaddr *
2068 in6_ifawithifp(ifp, dst)
2069 	struct ifnet *ifp;
2070 	struct in6_addr *dst;
2071 {
2072 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
2073 	struct ifaddr *ifa;
2074 	struct in6_ifaddr *besta = 0;
2075 	struct in6_ifaddr *dep[2];	/*last-resort: deprecated*/
2076 
2077 	dep[0] = dep[1] = NULL;
2078 
2079 	/*
2080 	 * We first look for addresses in the same scope.
2081 	 * If there is one, return it.
2082 	 * If two or more, return one which matches the dst longest.
2083 	 * If none, return one of global addresses assigned other ifs.
2084 	 */
2085 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2086 	{
2087 		if (ifa->ifa_addr->sa_family != AF_INET6)
2088 			continue;
2089 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2090 			continue; /* XXX: is there any case to allow anycast? */
2091 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2092 			continue; /* don't use this interface */
2093 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2094 			continue;
2095 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2096 			if (ip6_use_deprecated)
2097 				dep[0] = (struct in6_ifaddr *)ifa;
2098 			continue;
2099 		}
2100 
2101 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2102 			/*
2103 			 * call in6_matchlen() as few as possible
2104 			 */
2105 			if (besta) {
2106 				if (blen == -1)
2107 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2108 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2109 				if (tlen > blen) {
2110 					blen = tlen;
2111 					besta = (struct in6_ifaddr *)ifa;
2112 				}
2113 			} else
2114 				besta = (struct in6_ifaddr *)ifa;
2115 		}
2116 	}
2117 	if (besta)
2118 		return(besta);
2119 
2120 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2121 	{
2122 		if (ifa->ifa_addr->sa_family != AF_INET6)
2123 			continue;
2124 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2125 			continue; /* XXX: is there any case to allow anycast? */
2126 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2127 			continue; /* don't use this interface */
2128 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2129 			continue;
2130 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2131 			if (ip6_use_deprecated)
2132 				dep[1] = (struct in6_ifaddr *)ifa;
2133 			continue;
2134 		}
2135 
2136 		return (struct in6_ifaddr *)ifa;
2137 	}
2138 
2139 	/* use the last-resort values, that are, deprecated addresses */
2140 	if (dep[0])
2141 		return dep[0];
2142 	if (dep[1])
2143 		return dep[1];
2144 
2145 	return NULL;
2146 }
2147 
2148 /*
2149  * perform DAD when interface becomes IFF_UP.
2150  */
2151 void
2152 in6_if_up(ifp)
2153 	struct ifnet *ifp;
2154 {
2155 	struct ifaddr *ifa;
2156 	struct in6_ifaddr *ia;
2157 	int dad_delay;		/* delay ticks before DAD output */
2158 
2159 	/*
2160 	 * special cases, like 6to4, are handled in in6_ifattach
2161 	 */
2162 	in6_ifattach(ifp, NULL);
2163 
2164 	dad_delay = 0;
2165 	for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
2166 	{
2167 		if (ifa->ifa_addr->sa_family != AF_INET6)
2168 			continue;
2169 		ia = (struct in6_ifaddr *)ifa;
2170 		if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2171 			nd6_dad_start(ifa, &dad_delay);
2172 	}
2173 }
2174 
2175 /*
2176  * Calculate max IPv6 MTU through all the interfaces and store it
2177  * to in6_maxmtu.
2178  */
2179 void
2180 in6_setmaxmtu()
2181 {
2182 	unsigned long maxmtu = 0;
2183 	struct ifnet *ifp;
2184 
2185 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2186 	{
2187 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2188 		    nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2189 			maxmtu =  nd_ifinfo[ifp->if_index].linkmtu;
2190 	}
2191 	if (maxmtu)	/* update only when maxmtu is positive */
2192 		in6_maxmtu = maxmtu;
2193 }
2194