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