xref: /openbsd-src/sys/netinet6/in6.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: in6.c,v 1.192 2016/09/04 10:32:01 mpi Exp $	*/
2 /*	$KAME: in6.c,v 1.372 2004/06/14 08:14:21 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. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)in.c	8.2 (Berkeley) 11/15/93
62  */
63 
64 #include "carp.h"
65 
66 #include <sys/param.h>
67 #include <sys/ioctl.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/mbuf.h>
74 #include <sys/systm.h>
75 #include <sys/time.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 
79 #include <net/if.h>
80 #include <net/if_dl.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
83 
84 #include <netinet/in.h>
85 #include <netinet/if_ether.h>
86 
87 #include <netinet6/in6_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/nd6.h>
91 #include <netinet6/mld6_var.h>
92 #ifdef MROUTING
93 #include <netinet6/ip6_mroute.h>
94 #endif
95 #include <netinet6/in6_ifattach.h>
96 #if NCARP > 0
97 #include <netinet/ip_carp.h>
98 #endif
99 
100 /* backward compatibility for a while... */
101 #define COMPAT_IN6IFIOCTL
102 
103 /*
104  * Definitions of some constant IP6 addresses.
105  */
106 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
107 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
108 const struct in6_addr in6addr_intfacelocal_allnodes =
109 	IN6ADDR_INTFACELOCAL_ALLNODES_INIT;
110 const struct in6_addr in6addr_linklocal_allnodes =
111 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
112 const struct in6_addr in6addr_linklocal_allrouters =
113 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
114 
115 const struct in6_addr in6mask0 = IN6MASK0;
116 const struct in6_addr in6mask32 = IN6MASK32;
117 const struct in6_addr in6mask64 = IN6MASK64;
118 const struct in6_addr in6mask96 = IN6MASK96;
119 const struct in6_addr in6mask128 = IN6MASK128;
120 
121 int in6_lifaddr_ioctl(u_long, caddr_t, struct ifnet *, int);
122 int in6_ioctl(u_long, caddr_t, struct ifnet *, int);
123 int in6_ifinit(struct ifnet *, struct in6_ifaddr *, int);
124 void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
125 
126 const struct sockaddr_in6 sa6_any = {
127 	sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0
128 };
129 
130 int
131 in6_mask2len(struct in6_addr *mask, u_char *lim0)
132 {
133 	int x = 0, y;
134 	u_char *lim = lim0, *p;
135 
136 	/* ignore the scope_id part */
137 	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
138 		lim = (u_char *)mask + sizeof(*mask);
139 	for (p = (u_char *)mask; p < lim; x++, p++) {
140 		if (*p != 0xff)
141 			break;
142 	}
143 	y = 0;
144 	if (p < lim) {
145 		for (y = 0; y < 8; y++) {
146 			if ((*p & (0x80 >> y)) == 0)
147 				break;
148 		}
149 	}
150 
151 	/*
152 	 * when the limit pointer is given, do a stricter check on the
153 	 * remaining bits.
154 	 */
155 	if (p < lim) {
156 		if (y != 0 && (*p & (0x00ff >> y)) != 0)
157 			return (-1);
158 		for (p = p + 1; p < lim; p++)
159 			if (*p != 0)
160 				return (-1);
161 	}
162 
163 	return x * 8 + y;
164 }
165 
166 int
167 in6_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp)
168 {
169 	int privileged;
170 
171 	privileged = 0;
172 	if ((so->so_state & SS_PRIV) != 0)
173 		privileged++;
174 
175 #ifdef MROUTING
176 	switch (cmd) {
177 	case SIOCGETSGCNT_IN6:
178 	case SIOCGETMIFCNT_IN6:
179 		return (mrt6_ioctl(cmd, data));
180 	}
181 #endif
182 
183 	return (in6_ioctl(cmd, data, ifp, privileged));
184 }
185 
186 int
187 in6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, int privileged)
188 {
189 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
190 	struct	in6_ifaddr *ia6 = NULL;
191 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
192 	struct sockaddr_in6 *sa6;
193 	int s;
194 
195 	if (ifp == NULL)
196 		return (EOPNOTSUPP);
197 
198 	switch (cmd) {
199 	case SIOCSNDFLUSH_IN6:
200 	case SIOCSPFXFLUSH_IN6:
201 	case SIOCSRTRFLUSH_IN6:
202 	case SIOCSIFINFO_FLAGS:
203 		if (!privileged)
204 			return (EPERM);
205 		/* FALLTHROUGH */
206 	case SIOCGIFINFO_IN6:
207 	case SIOCGNBRINFO_IN6:
208 		return (nd6_ioctl(cmd, data, ifp));
209 	}
210 
211 	switch (cmd) {
212 	case SIOCALIFADDR:
213 	case SIOCDLIFADDR:
214 		if (!privileged)
215 			return (EPERM);
216 		/* FALLTHROUGH */
217 	case SIOCGLIFADDR:
218 		return in6_lifaddr_ioctl(cmd, data, ifp, privileged);
219 	}
220 
221 	/*
222 	 * Find address for this interface, if it exists.
223 	 *
224 	 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
225 	 * only, and used the first interface address as the target of other
226 	 * operations (without checking ifra_addr).  This was because netinet
227 	 * code/API assumed at most 1 interface address per interface.
228 	 * Since IPv6 allows a node to assign multiple addresses
229 	 * on a single interface, we almost always look and check the
230 	 * presence of ifra_addr, and reject invalid ones here.
231 	 * It also decreases duplicated code among SIOC*_IN6 operations.
232 	 */
233 	switch (cmd) {
234 	case SIOCAIFADDR_IN6:
235 	case SIOCSIFPHYADDR_IN6:
236 		sa6 = &ifra->ifra_addr;
237 		break;
238 	case SIOCGIFADDR_IN6:
239 	case SIOCGIFDSTADDR_IN6:
240 	case SIOCGIFNETMASK_IN6:
241 	case SIOCDIFADDR_IN6:
242 	case SIOCGIFPSRCADDR_IN6:
243 	case SIOCGIFPDSTADDR_IN6:
244 	case SIOCGIFAFLAG_IN6:
245 	case SIOCSNDFLUSH_IN6:
246 	case SIOCSPFXFLUSH_IN6:
247 	case SIOCSRTRFLUSH_IN6:
248 	case SIOCGIFALIFETIME_IN6:
249 	case SIOCGIFSTAT_IN6:
250 	case SIOCGIFSTAT_ICMP6:
251 		sa6 = &ifr->ifr_addr;
252 		break;
253 	case SIOCSIFADDR:
254 	case SIOCSIFDSTADDR:
255 	case SIOCSIFBRDADDR:
256 	case SIOCSIFNETMASK:
257 		/*
258 		 * Do not pass those ioctl to driver handler since they are not
259 		 * properly setup. Instead just error out.
260 		 */
261 		return (EOPNOTSUPP);
262 	default:
263 		sa6 = NULL;
264 		break;
265 	}
266 	if (sa6 && sa6->sin6_family == AF_INET6) {
267 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
268 			if (sa6->sin6_addr.s6_addr16[1] == 0) {
269 				/* link ID is not embedded by the user */
270 				sa6->sin6_addr.s6_addr16[1] =
271 				    htons(ifp->if_index);
272 			} else if (sa6->sin6_addr.s6_addr16[1] !=
273 			    htons(ifp->if_index)) {
274 				return (EINVAL);	/* link ID contradicts */
275 			}
276 			if (sa6->sin6_scope_id) {
277 				if (sa6->sin6_scope_id !=
278 				    (u_int32_t)ifp->if_index)
279 					return (EINVAL);
280 				sa6->sin6_scope_id = 0; /* XXX: good way? */
281 			}
282 		}
283 		ia6 = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
284 	} else
285 		ia6 = NULL;
286 
287 	switch (cmd) {
288 	case SIOCDIFADDR_IN6:
289 		/*
290 		 * for IPv4, we look for existing in_ifaddr here to allow
291 		 * "ifconfig if0 delete" to remove the first IPv4 address on
292 		 * the interface.  For IPv6, as the spec allows multiple
293 		 * interface address from the day one, we consider "remove the
294 		 * first one" semantics to be not preferable.
295 		 */
296 		if (ia6 == NULL)
297 			return (EADDRNOTAVAIL);
298 		/* FALLTHROUGH */
299 	case SIOCAIFADDR_IN6:
300 		/*
301 		 * We always require users to specify a valid IPv6 address for
302 		 * the corresponding operation.
303 		 */
304 		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
305 		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
306 			return (EAFNOSUPPORT);
307 		if (!privileged)
308 			return (EPERM);
309 
310 		break;
311 
312 	case SIOCGIFADDR_IN6:
313 		/* This interface is basically deprecated. use SIOCGIFCONF. */
314 		/* FALLTHROUGH */
315 	case SIOCGIFAFLAG_IN6:
316 	case SIOCGIFNETMASK_IN6:
317 	case SIOCGIFDSTADDR_IN6:
318 	case SIOCGIFALIFETIME_IN6:
319 		/* must think again about its semantics */
320 		if (ia6 == NULL)
321 			return (EADDRNOTAVAIL);
322 		break;
323 	}
324 
325 	switch (cmd) {
326 
327 	case SIOCGIFADDR_IN6:
328 		ifr->ifr_addr = ia6->ia_addr;
329 		break;
330 
331 	case SIOCGIFDSTADDR_IN6:
332 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
333 			return (EINVAL);
334 		/*
335 		 * XXX: should we check if ifa_dstaddr is NULL and return
336 		 * an error?
337 		 */
338 		ifr->ifr_dstaddr = ia6->ia_dstaddr;
339 		break;
340 
341 	case SIOCGIFNETMASK_IN6:
342 		ifr->ifr_addr = ia6->ia_prefixmask;
343 		break;
344 
345 	case SIOCGIFAFLAG_IN6:
346 		ifr->ifr_ifru.ifru_flags6 = ia6->ia6_flags;
347 		break;
348 
349 	case SIOCGIFSTAT_IN6:
350 	case SIOCGIFSTAT_ICMP6:
351 		return (EOPNOTSUPP);
352 
353 	case SIOCGIFALIFETIME_IN6:
354 		ifr->ifr_ifru.ifru_lifetime = ia6->ia6_lifetime;
355 		if (ia6->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
356 			time_t maxexpire;
357 			struct in6_addrlifetime *retlt =
358 			    &ifr->ifr_ifru.ifru_lifetime;
359 
360 			/*
361 			 * XXX: adjust expiration time assuming time_t is
362 			 * signed.
363 			 */
364 			maxexpire =
365 			    (time_t)~(1ULL << ((sizeof(maxexpire) * 8) - 1));
366 			if (ia6->ia6_lifetime.ia6t_vltime <
367 			    maxexpire - ia6->ia6_updatetime) {
368 				retlt->ia6t_expire = ia6->ia6_updatetime +
369 				    ia6->ia6_lifetime.ia6t_vltime;
370 			} else
371 				retlt->ia6t_expire = maxexpire;
372 		}
373 		if (ia6->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
374 			time_t maxexpire;
375 			struct in6_addrlifetime *retlt =
376 			    &ifr->ifr_ifru.ifru_lifetime;
377 
378 			/*
379 			 * XXX: adjust expiration time assuming time_t is
380 			 * signed.
381 			 */
382 			maxexpire =
383 			    (time_t)~(1ULL << ((sizeof(maxexpire) * 8) - 1));
384 			if (ia6->ia6_lifetime.ia6t_pltime <
385 			    maxexpire - ia6->ia6_updatetime) {
386 				retlt->ia6t_preferred = ia6->ia6_updatetime +
387 				    ia6->ia6_lifetime.ia6t_pltime;
388 			} else
389 				retlt->ia6t_preferred = maxexpire;
390 		}
391 		break;
392 
393 	case SIOCAIFADDR_IN6:
394 	{
395 		int plen, error = 0, newifaddr = 0;
396 
397 		/* reject read-only flags */
398 		if ((ifra->ifra_flags & IN6_IFF_DUPLICATED) != 0 ||
399 		    (ifra->ifra_flags & IN6_IFF_DETACHED) != 0 ||
400 		    (ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0 ||
401 		    (ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0) {
402 			return (EINVAL);
403 		}
404 
405 		if (ia6 == NULL)
406 			newifaddr = 1;
407 
408 		/*
409 		 * Make the address tentative before joining multicast
410 		 * addresses, so that corresponding MLD responses would
411 		 * not have a tentative source address.
412 		 */
413 		if (newifaddr && in6if_do_dad(ifp))
414 			ifra->ifra_flags |= IN6_IFF_TENTATIVE;
415 
416 		/*
417 		 * first, make or update the interface address structure,
418 		 * and link it to the list. try to enable inet6 if there
419 		 * is no link-local yet.
420 		 */
421 		s = splsoftnet();
422 		error = in6_ifattach(ifp);
423 		if (error != 0) {
424 			splx(s);
425 			return (error);
426 		}
427 		error = in6_update_ifa(ifp, ifra, ia6);
428 		splx(s);
429 		if (error != 0)
430 			return (error);
431 
432 		ia6 = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
433 		if (ia6 == NULL) {
434 			/*
435 			 * this can happen when the user specify the 0 valid
436 			 * lifetime.
437 			 */
438 			break;
439 		}
440 
441 		if (!newifaddr) {
442 			dohooks(ifp->if_addrhooks, 0);
443 			break;
444 		}
445 
446 		/* Perform DAD, if needed. */
447 		if (ia6->ia6_flags & IN6_IFF_TENTATIVE)
448 			nd6_dad_start(&ia6->ia_ifa);
449 
450 		plen = in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL);
451 		if ((ifp->if_flags & IFF_LOOPBACK) || plen == 128) {
452 			dohooks(ifp->if_addrhooks, 0);
453 			break;	/* No need to install a connected route. */
454 		}
455 
456 		s = splsoftnet();
457 		error = rt_ifa_add(&ia6->ia_ifa, RTF_CLONING | RTF_CONNECTED,
458 		    ia6->ia_ifa.ifa_addr);
459 		if (error) {
460 			in6_purgeaddr(&ia6->ia_ifa);
461 			splx(s);
462 			return (error);
463 		}
464 		dohooks(ifp->if_addrhooks, 0);
465 		splx(s);
466 		break;
467 	}
468 
469 	case SIOCDIFADDR_IN6:
470 		s = splsoftnet();
471 		in6_purgeaddr(&ia6->ia_ifa);
472 		dohooks(ifp->if_addrhooks, 0);
473 		splx(s);
474 		break;
475 
476 	default:
477 		if (ifp == NULL || ifp->if_ioctl == 0)
478 			return (EOPNOTSUPP);
479 		return ((*ifp->if_ioctl)(ifp, cmd, data));
480 	}
481 
482 	return (0);
483 }
484 
485 /*
486  * Update parameters of an IPv6 interface address.
487  * If necessary, a new entry is created and linked into address chains.
488  * This function is separated from in6_control().
489  */
490 int
491 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
492     struct in6_ifaddr *ia6)
493 {
494 	int error = 0, hostIsNew = 0, plen = -1;
495 	struct sockaddr_in6 dst6;
496 	struct in6_addrlifetime *lt;
497 	struct in6_multi_mship *imm;
498 	struct rtentry *rt;
499 	char addr[INET6_ADDRSTRLEN];
500 
501 	splsoftassert(IPL_SOFTNET);
502 
503 	/* Validate parameters */
504 	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
505 		return (EINVAL);
506 
507 	/*
508 	 * The destination address for a p2p link must have a family
509 	 * of AF_UNSPEC or AF_INET6.
510 	 */
511 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
512 	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
513 	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
514 		return (EAFNOSUPPORT);
515 
516 	/*
517 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
518 	 * does not carry fields other than sin6_len.
519 	 */
520 	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
521 		return (EINVAL);
522 	/*
523 	 * Because the IPv6 address architecture is classless, we require
524 	 * users to specify a (non 0) prefix length (mask) for a new address.
525 	 * We also require the prefix (when specified) mask is valid, and thus
526 	 * reject a non-consecutive mask.
527 	 */
528 	if (ia6 == NULL && ifra->ifra_prefixmask.sin6_len == 0)
529 		return (EINVAL);
530 	if (ifra->ifra_prefixmask.sin6_len != 0) {
531 		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
532 		    (u_char *)&ifra->ifra_prefixmask +
533 		    ifra->ifra_prefixmask.sin6_len);
534 		if (plen <= 0)
535 			return (EINVAL);
536 	} else {
537 		/*
538 		 * In this case, ia6 must not be NULL.  We just use its prefix
539 		 * length.
540 		 */
541 		plen = in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL);
542 	}
543 	/*
544 	 * If the destination address on a p2p interface is specified,
545 	 * and the address is a scoped one, validate/set the scope
546 	 * zone identifier.
547 	 */
548 	dst6 = ifra->ifra_dstaddr;
549 	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
550 	    (dst6.sin6_family == AF_INET6)) {
551 		/* link-local index check: should be a separate function? */
552 		if (IN6_IS_ADDR_LINKLOCAL(&dst6.sin6_addr)) {
553 			if (dst6.sin6_addr.s6_addr16[1] == 0) {
554 				/*
555 				 * interface ID is not embedded by
556 				 * the user
557 				 */
558 				dst6.sin6_addr.s6_addr16[1] =
559 				    htons(ifp->if_index);
560 			} else if (dst6.sin6_addr.s6_addr16[1] !=
561 			    htons(ifp->if_index)) {
562 				return (EINVAL);	/* ifid contradicts */
563 			}
564 		}
565 	}
566 	/*
567 	 * The destination address can be specified only for a p2p or a
568 	 * loopback interface.  If specified, the corresponding prefix length
569 	 * must be 128.
570 	 */
571 	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
572 		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0)
573 			return (EINVAL);
574 		if (plen != 128)
575 			return (EINVAL);
576 	}
577 	/* lifetime consistency check */
578 	lt = &ifra->ifra_lifetime;
579 	if (lt->ia6t_pltime > lt->ia6t_vltime)
580 		return (EINVAL);
581 	if (lt->ia6t_vltime == 0) {
582 		/*
583 		 * the following log might be noisy, but this is a typical
584 		 * configuration mistake or a tool's bug.
585 		 */
586 		nd6log((LOG_INFO,
587 		    "in6_update_ifa: valid lifetime is 0 for %s\n",
588 		    inet_ntop(AF_INET6, &ifra->ifra_addr.sin6_addr,
589 		        addr, sizeof(addr))));
590 
591 		if (ia6 == NULL)
592 			return (0); /* there's nothing to do */
593 	}
594 
595 	/*
596 	 * If this is a new address, allocate a new ifaddr and link it
597 	 * into chains.
598 	 */
599 	if (ia6 == NULL) {
600 		hostIsNew = 1;
601 		ia6 = malloc(sizeof(*ia6), M_IFADDR, M_WAITOK | M_ZERO);
602 		LIST_INIT(&ia6->ia6_memberships);
603 		/* Initialize the address and masks, and put time stamp */
604 		ia6->ia_ifa.ifa_addr = sin6tosa(&ia6->ia_addr);
605 		ia6->ia_addr.sin6_family = AF_INET6;
606 		ia6->ia_addr.sin6_len = sizeof(ia6->ia_addr);
607 		ia6->ia6_createtime = ia6->ia6_updatetime = time_second;
608 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
609 			/*
610 			 * XXX: some functions expect that ifa_dstaddr is not
611 			 * NULL for p2p interfaces.
612 			 */
613 			ia6->ia_ifa.ifa_dstaddr = sin6tosa(&ia6->ia_dstaddr);
614 		} else {
615 			ia6->ia_ifa.ifa_dstaddr = NULL;
616 		}
617 		ia6->ia_ifa.ifa_netmask = sin6tosa(&ia6->ia_prefixmask);
618 
619 		ia6->ia_ifp = ifp;
620 		TAILQ_INSERT_TAIL(&in6_ifaddr, ia6, ia_list);
621 		ia6->ia_addr = ifra->ifra_addr;
622 		ifa_add(ifp, &ia6->ia_ifa);
623 	}
624 
625 	/* set prefix mask */
626 	if (ifra->ifra_prefixmask.sin6_len) {
627 		/*
628 		 * We prohibit changing the prefix length of an existing
629 		 * address, because
630 		 * + such an operation should be rare in IPv6, and
631 		 * + the operation would confuse prefix management.
632 		 */
633 		if (ia6->ia_prefixmask.sin6_len &&
634 		    in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL) != plen) {
635 			error = EINVAL;
636 			goto unlink;
637 		}
638 		ia6->ia_prefixmask = ifra->ifra_prefixmask;
639 	}
640 
641 	/*
642 	 * If a new destination address is specified, scrub the old one and
643 	 * install the new destination.  Note that the interface must be
644 	 * p2p or loopback (see the check above.)
645 	 */
646 	if ((ifp->if_flags & IFF_POINTOPOINT) && dst6.sin6_family == AF_INET6 &&
647 	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia6->ia_dstaddr.sin6_addr)) {
648 		struct ifaddr *ifa = &ia6->ia_ifa;
649 
650 		if ((ia6->ia_flags & IFA_ROUTE) != 0 &&
651 		    rt_ifa_del(ifa, RTF_HOST, ifa->ifa_dstaddr) != 0) {
652 			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
653 			    "a route to the old destination: %s\n",
654 			    inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
655 				addr, sizeof(addr))));
656 			/* proceed anyway... */
657 		} else
658 			ia6->ia_flags &= ~IFA_ROUTE;
659 		ia6->ia_dstaddr = dst6;
660 	}
661 
662 	/*
663 	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
664 	 * to see if the address is deprecated or invalidated, but initialize
665 	 * these members for applications.
666 	 */
667 	ia6->ia6_lifetime = ifra->ifra_lifetime;
668 	if (ia6->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
669 		ia6->ia6_lifetime.ia6t_expire =
670 		    time_second + ia6->ia6_lifetime.ia6t_vltime;
671 	} else
672 		ia6->ia6_lifetime.ia6t_expire = 0;
673 	if (ia6->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
674 		ia6->ia6_lifetime.ia6t_preferred =
675 		    time_second + ia6->ia6_lifetime.ia6t_pltime;
676 	} else
677 		ia6->ia6_lifetime.ia6t_preferred = 0;
678 
679 	/* reset the interface and routing table appropriately. */
680 	if ((error = in6_ifinit(ifp, ia6, hostIsNew)) != 0)
681 		goto unlink;
682 
683 	/*
684 	 * configure address flags.
685 	 */
686 	ia6->ia6_flags = ifra->ifra_flags;
687 
688 	/*
689 	 * We are done if we have simply modified an existing address.
690 	 */
691 	if (!hostIsNew)
692 		return (error);
693 
694 	/*
695 	 * Beyond this point, we should call in6_purgeaddr upon an error,
696 	 * not just go to unlink.
697 	 */
698 
699 	/* join necessary multiast groups */
700 	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
701 		struct sockaddr_in6 mltaddr, mltmask;
702 
703 		/* join solicited multicast addr for new host id */
704 		struct sockaddr_in6 llsol;
705 
706 		bzero(&llsol, sizeof(llsol));
707 		llsol.sin6_family = AF_INET6;
708 		llsol.sin6_len = sizeof(llsol);
709 		llsol.sin6_addr.s6_addr16[0] = htons(0xff02);
710 		llsol.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
711 		llsol.sin6_addr.s6_addr32[1] = 0;
712 		llsol.sin6_addr.s6_addr32[2] = htonl(1);
713 		llsol.sin6_addr.s6_addr32[3] =
714 		    ifra->ifra_addr.sin6_addr.s6_addr32[3];
715 		llsol.sin6_addr.s6_addr8[12] = 0xff;
716 		imm = in6_joingroup(ifp, &llsol.sin6_addr, &error);
717 		if (!imm)
718 			goto cleanup;
719 		LIST_INSERT_HEAD(&ia6->ia6_memberships, imm, i6mm_chain);
720 
721 		bzero(&mltmask, sizeof(mltmask));
722 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
723 		mltmask.sin6_family = AF_INET6;
724 		mltmask.sin6_addr = in6mask32;
725 
726 		/*
727 		 * join link-local all-nodes address
728 		 */
729 		bzero(&mltaddr, sizeof(mltaddr));
730 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
731 		mltaddr.sin6_family = AF_INET6;
732 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
733 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
734 		mltaddr.sin6_scope_id = 0;
735 
736 		/*
737 		 * XXX: do we really need this automatic routes?
738 		 * We should probably reconsider this stuff.  Most applications
739 		 * actually do not need the routes, since they usually specify
740 		 * the outgoing interface.
741 		 */
742 		rt = rtalloc(sin6tosa(&mltaddr), 0, ifp->if_rdomain);
743 		if (rt) {
744 			/*
745 			 * 32bit came from "mltmask"
746 			 */
747 			if (memcmp(&mltaddr.sin6_addr,
748 			    &satosin6(rt_key(rt))->sin6_addr,
749 			    32 / 8)) {
750 				rtfree(rt);
751 				rt = NULL;
752 			}
753 		}
754 		if (!rt) {
755 			struct rt_addrinfo info;
756 
757 			bzero(&info, sizeof(info));
758 			info.rti_info[RTAX_DST] = sin6tosa(&mltaddr);
759 			info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia6->ia_addr);
760 			info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask);
761 			info.rti_info[RTAX_IFA] = sin6tosa(&ia6->ia_addr);
762 			info.rti_flags = RTF_MULTICAST;
763 			error = rtrequest(RTM_ADD, &info, RTP_CONNECTED, NULL,
764 			    ifp->if_rdomain);
765 			if (error)
766 				goto cleanup;
767 		} else {
768 			rtfree(rt);
769 		}
770 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
771 		if (!imm)
772 			goto cleanup;
773 		LIST_INSERT_HEAD(&ia6->ia6_memberships, imm, i6mm_chain);
774 
775 		/*
776 		 * join node information group address
777 		 */
778 		if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) == 0) {
779 			imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
780 			if (!imm) {
781 				/* XXX not very fatal, go on... */
782 			} else {
783 				LIST_INSERT_HEAD(&ia6->ia6_memberships,
784 				    imm, i6mm_chain);
785 			}
786 		}
787 
788 		/*
789 		 * join interface-local all-nodes address.
790 		 * (ff01::1%ifN, and ff01::%ifN/32)
791 		 */
792 		bzero(&mltaddr, sizeof(mltaddr));
793 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
794 		mltaddr.sin6_family = AF_INET6;
795 		mltaddr.sin6_addr = in6addr_intfacelocal_allnodes;
796 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
797 		mltaddr.sin6_scope_id = 0;
798 
799 		/* XXX: again, do we really need the route? */
800 		rt = rtalloc(sin6tosa(&mltaddr), 0, ifp->if_rdomain);
801 		if (rt) {
802 			/* 32bit came from "mltmask" */
803 			if (memcmp(&mltaddr.sin6_addr,
804 			    &satosin6(rt_key(rt))->sin6_addr,
805 			    32 / 8)) {
806 				rtfree(rt);
807 				rt = NULL;
808 			}
809 		}
810 		if (!rt) {
811 			struct rt_addrinfo info;
812 
813 			bzero(&info, sizeof(info));
814 			info.rti_info[RTAX_DST] = sin6tosa(&mltaddr);
815 			info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia6->ia_addr);
816 			info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask);
817 			info.rti_info[RTAX_IFA] = sin6tosa(&ia6->ia_addr);
818 			info.rti_flags = RTF_MULTICAST;
819 			error = rtrequest(RTM_ADD, &info, RTP_CONNECTED, NULL,
820 			    ifp->if_rdomain);
821 			if (error)
822 				goto cleanup;
823 		} else {
824 			rtfree(rt);
825 		}
826 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
827 		if (!imm)
828 			goto cleanup;
829 		LIST_INSERT_HEAD(&ia6->ia6_memberships, imm, i6mm_chain);
830 	}
831 
832 	return (error);
833 
834   unlink:
835 	/*
836 	 * XXX: if a change of an existing address failed, keep the entry
837 	 * anyway.
838 	 */
839 	if (hostIsNew)
840 		in6_unlink_ifa(ia6, ifp);
841 	return (error);
842 
843   cleanup:
844 	in6_purgeaddr(&ia6->ia_ifa);
845 	return error;
846 }
847 
848 void
849 in6_purgeaddr(struct ifaddr *ifa)
850 {
851 	struct ifnet *ifp = ifa->ifa_ifp;
852 	struct in6_ifaddr *ia6 = ifatoia6(ifa);
853 	struct in6_multi_mship *imm;
854 
855 	/* stop DAD processing */
856 	nd6_dad_stop(ifa);
857 
858 	/*
859 	 * delete route to the destination of the address being purged.
860 	 * The interface must be p2p or loopback in this case.
861 	 */
862 	if ((ifp->if_flags & IFF_POINTOPOINT) && (ia6->ia_flags & IFA_ROUTE) &&
863 	    ia6->ia_dstaddr.sin6_len != 0) {
864 		int e;
865 
866 		if ((e = rt_ifa_del(ifa, RTF_HOST, ifa->ifa_dstaddr)) != 0) {
867 			char addr[INET6_ADDRSTRLEN];
868 			log(LOG_ERR, "in6_purgeaddr: failed to remove "
869 			    "a route to the p2p destination: %s on %s, "
870 			    "errno=%d\n",
871 			    inet_ntop(AF_INET6, &ia6->ia_addr.sin6_addr,
872 				addr, sizeof(addr)),
873 			    ifp->if_xname, e);
874 			/* proceed anyway... */
875 		} else
876 			ia6->ia_flags &= ~IFA_ROUTE;
877 	}
878 
879 	/* Remove ownaddr's loopback rtentry, if it exists. */
880 	rt_ifa_dellocal(&(ia6->ia_ifa));
881 
882 	/*
883 	 * leave from multicast groups we have joined for the interface
884 	 */
885 	while (!LIST_EMPTY(&ia6->ia6_memberships)) {
886 		imm = LIST_FIRST(&ia6->ia6_memberships);
887 		LIST_REMOVE(imm, i6mm_chain);
888 		in6_leavegroup(imm);
889 	}
890 
891 	in6_unlink_ifa(ia6, ifp);
892 }
893 
894 void
895 in6_unlink_ifa(struct in6_ifaddr *ia6, struct ifnet *ifp)
896 {
897 	struct ifaddr *ifa = &ia6->ia_ifa;
898 	extern int ifatrash;
899 	int plen;
900 
901 	splsoftassert(IPL_SOFTNET);
902 
903 	TAILQ_REMOVE(&in6_ifaddr, ia6, ia_list);
904 
905 	/* Release the reference to the base prefix. */
906 	if (ia6->ia6_ndpr == NULL) {
907 		plen = in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL);
908 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 && plen != 128) {
909 			rt_ifa_del(ifa, RTF_CLONING | RTF_CONNECTED,
910 			    ifa->ifa_addr);
911 		}
912 	} else {
913 		KASSERT(ia6->ia6_flags & IN6_IFF_AUTOCONF);
914 		ia6->ia6_flags &= ~IN6_IFF_AUTOCONF;
915 		if (--ia6->ia6_ndpr->ndpr_refcnt == 0)
916 			prelist_remove(ia6->ia6_ndpr);
917 		ia6->ia6_ndpr = NULL;
918 	}
919 
920 	rt_ifa_purge(ifa);
921 	ifa_del(ifp, ifa);
922 
923 	ifatrash++;
924 	ia6->ia_ifp = NULL;
925 	ifafree(&ia6->ia_ifa);
926 }
927 
928 /*
929  * SIOC[GAD]LIFADDR.
930  *	SIOCGLIFADDR: get first address. (?)
931  *	SIOCGLIFADDR with IFLR_PREFIX:
932  *		get first address that matches the specified prefix.
933  *	SIOCALIFADDR: add the specified address.
934  *	SIOCALIFADDR with IFLR_PREFIX:
935  *		add the specified prefix, filling hostid part from
936  *		the first link-local address.  prefixlen must be <= 64.
937  *	SIOCDLIFADDR: delete the specified address.
938  *	SIOCDLIFADDR with IFLR_PREFIX:
939  *		delete the first address that matches the specified prefix.
940  * return values:
941  *	EINVAL on invalid parameters
942  *	EADDRNOTAVAIL on prefix match failed/specified address not found
943  *	other values may be returned from in6_ioctl()
944  *
945  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
946  * this is to accommodate address naming scheme other than RFC2374,
947  * in the future.
948  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
949  * address encoding scheme. (see figure on page 8)
950  */
951 int
952 in6_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, int privileged)
953 {
954 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
955 	struct ifaddr *ifa;
956 	struct sockaddr *sa;
957 
958 	/* sanity checks */
959 	if (!data || !ifp) {
960 		panic("invalid argument to in6_lifaddr_ioctl");
961 		/* NOTREACHED */
962 	}
963 
964 	switch (cmd) {
965 	case SIOCGLIFADDR:
966 		/* address must be specified on GET with IFLR_PREFIX */
967 		if ((iflr->flags & IFLR_PREFIX) == 0)
968 			break;
969 		/* FALLTHROUGH */
970 	case SIOCALIFADDR:
971 	case SIOCDLIFADDR:
972 		/* address must be specified on ADD and DELETE */
973 		sa = (struct sockaddr *)&iflr->addr;
974 		if (sa->sa_family != AF_INET6)
975 			return EINVAL;
976 		if (sa->sa_len != sizeof(struct sockaddr_in6))
977 			return EINVAL;
978 		/* XXX need improvement */
979 		sa = (struct sockaddr *)&iflr->dstaddr;
980 		if (sa->sa_family && sa->sa_family != AF_INET6)
981 			return EINVAL;
982 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
983 			return EINVAL;
984 		break;
985 	default: /* shouldn't happen */
986 #if 0
987 		panic("invalid cmd to in6_lifaddr_ioctl");
988 		/* NOTREACHED */
989 #else
990 		return EOPNOTSUPP;
991 #endif
992 	}
993 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
994 		return EINVAL;
995 
996 	switch (cmd) {
997 	case SIOCALIFADDR:
998 	    {
999 		struct in6_aliasreq ifra;
1000 		struct in6_addr *hostid = NULL;
1001 		int prefixlen;
1002 
1003 		if ((iflr->flags & IFLR_PREFIX) != 0) {
1004 			struct sockaddr_in6 *sin6;
1005 
1006 			/*
1007 			 * hostid is to fill in the hostid part of the
1008 			 * address.  hostid points to the first link-local
1009 			 * address attached to the interface.
1010 			 */
1011 			ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa;
1012 			if (!ifa)
1013 				return EADDRNOTAVAIL;
1014 			hostid = IFA_IN6(ifa);
1015 
1016 			/* prefixlen must be <= 64. */
1017 			if (64 < iflr->prefixlen)
1018 				return EINVAL;
1019 			prefixlen = iflr->prefixlen;
1020 
1021 			/* hostid part must be zero. */
1022 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1023 			if (sin6->sin6_addr.s6_addr32[2] != 0
1024 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
1025 				return EINVAL;
1026 			}
1027 		} else
1028 			prefixlen = iflr->prefixlen;
1029 
1030 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1031 		bzero(&ifra, sizeof(ifra));
1032 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1033 
1034 		bcopy(&iflr->addr, &ifra.ifra_addr,
1035 		    ((struct sockaddr *)&iflr->addr)->sa_len);
1036 		if (hostid) {
1037 			/* fill in hostid part */
1038 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1039 			    hostid->s6_addr32[2];
1040 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1041 			    hostid->s6_addr32[3];
1042 		}
1043 
1044 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
1045 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1046 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1047 			if (hostid) {
1048 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1049 				    hostid->s6_addr32[2];
1050 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1051 				    hostid->s6_addr32[3];
1052 			}
1053 		}
1054 
1055 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1056 		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1057 
1058 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1059 		return in6_ioctl(SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp,
1060 		    privileged);
1061 	    }
1062 	case SIOCGLIFADDR:
1063 	case SIOCDLIFADDR:
1064 	    {
1065 		struct in6_ifaddr *ia6;
1066 		struct in6_addr mask, candidate, match;
1067 		struct sockaddr_in6 *sin6;
1068 		int cmp;
1069 
1070 		bzero(&mask, sizeof(mask));
1071 		if (iflr->flags & IFLR_PREFIX) {
1072 			/* lookup a prefix rather than address. */
1073 			in6_prefixlen2mask(&mask, iflr->prefixlen);
1074 
1075 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1076 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
1077 			match.s6_addr32[0] &= mask.s6_addr32[0];
1078 			match.s6_addr32[1] &= mask.s6_addr32[1];
1079 			match.s6_addr32[2] &= mask.s6_addr32[2];
1080 			match.s6_addr32[3] &= mask.s6_addr32[3];
1081 
1082 			/* if you set extra bits, that's wrong */
1083 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1084 				return EINVAL;
1085 
1086 			cmp = 1;
1087 		} else {
1088 			if (cmd == SIOCGLIFADDR) {
1089 				/* on getting an address, take the 1st match */
1090 				cmp = 0;	/* XXX */
1091 			} else {
1092 				/* on deleting an address, do exact match */
1093 				in6_prefixlen2mask(&mask, 128);
1094 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
1095 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
1096 
1097 				cmp = 1;
1098 			}
1099 		}
1100 
1101 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1102 			if (ifa->ifa_addr->sa_family != AF_INET6)
1103 				continue;
1104 			if (!cmp)
1105 				break;
1106 
1107 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1108 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
1109 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
1110 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
1111 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
1112 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1113 				break;
1114 		}
1115 		if (!ifa)
1116 			return EADDRNOTAVAIL;
1117 		ia6 = ifatoia6(ifa);
1118 
1119 		if (cmd == SIOCGLIFADDR) {
1120 			/* fill in the if_laddrreq structure */
1121 			bcopy(&ia6->ia_addr, &iflr->addr, ia6->ia_addr.sin6_len);
1122 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1123 				bcopy(&ia6->ia_dstaddr, &iflr->dstaddr,
1124 				    ia6->ia_dstaddr.sin6_len);
1125 			} else
1126 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1127 
1128 			iflr->prefixlen =
1129 			    in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL);
1130 
1131 			iflr->flags = ia6->ia6_flags;	/*XXX*/
1132 
1133 			return 0;
1134 		} else {
1135 			struct in6_aliasreq ifra;
1136 
1137 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1138 			bzero(&ifra, sizeof(ifra));
1139 			bcopy(iflr->iflr_name, ifra.ifra_name,
1140 			    sizeof(ifra.ifra_name));
1141 
1142 			bcopy(&ia6->ia_addr, &ifra.ifra_addr,
1143 			    ia6->ia_addr.sin6_len);
1144 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1145 				bcopy(&ia6->ia_dstaddr, &ifra.ifra_dstaddr,
1146 				    ia6->ia_dstaddr.sin6_len);
1147 			} else {
1148 				bzero(&ifra.ifra_dstaddr,
1149 				    sizeof(ifra.ifra_dstaddr));
1150 			}
1151 			bcopy(&ia6->ia_prefixmask, &ifra.ifra_dstaddr,
1152 			    ia6->ia_prefixmask.sin6_len);
1153 
1154 			ifra.ifra_flags = ia6->ia6_flags;
1155 			return in6_ioctl(SIOCDIFADDR_IN6, (caddr_t)&ifra, ifp,
1156 			    privileged);
1157 		}
1158 	    }
1159 	}
1160 
1161 	return EOPNOTSUPP;	/* just for safety */
1162 }
1163 
1164 /*
1165  * Initialize an interface's intetnet6 address
1166  * and routing table entry.
1167  */
1168 int
1169 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia6, int newhost)
1170 {
1171 	int	error = 0, plen, ifacount = 0;
1172 	struct ifaddr *ifa;
1173 
1174 	splsoftassert(IPL_SOFTNET);
1175 
1176 	/*
1177 	 * Give the interface a chance to initialize
1178 	 * if this is its first address (or it is a CARP interface)
1179 	 * and to validate the address if necessary.
1180 	 */
1181 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1182 		if (ifa->ifa_addr->sa_family != AF_INET6)
1183 			continue;
1184 		ifacount++;
1185 	}
1186 
1187 	if ((ifacount <= 1 || ifp->if_type == IFT_CARP ||
1188 	    (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))) &&
1189 	    ifp->if_ioctl &&
1190 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia6))) {
1191 		return (error);
1192 	}
1193 
1194 	ia6->ia_ifa.ifa_metric = ifp->if_metric;
1195 
1196 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
1197 
1198 	/*
1199 	 * Special case:
1200 	 * If the destination address is specified for a point-to-point
1201 	 * interface, install a route to the destination as an interface
1202 	 * direct route.
1203 	 */
1204 	plen = in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL); /* XXX */
1205 	if ((ifp->if_flags & IFF_POINTOPOINT) && plen == 128 &&
1206 	    ia6->ia_dstaddr.sin6_family == AF_INET6) {
1207 		ifa = &ia6->ia_ifa;
1208 		error = rt_ifa_add(ifa, RTF_HOST, ifa->ifa_dstaddr);
1209 		if (error != 0)
1210 			return (error);
1211 		ia6->ia_flags |= IFA_ROUTE;
1212 	}
1213 
1214 	if (newhost)
1215 		rt_ifa_addlocal(&(ia6->ia_ifa));
1216 
1217 	return (error);
1218 }
1219 
1220 /*
1221  * Add an address to the list of IP6 multicast addresses for a
1222  * given interface.
1223  */
1224 struct in6_multi *
1225 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1226 {
1227 	struct	in6_ifreq ifr;
1228 	struct	in6_multi *in6m;
1229 	int	s;
1230 
1231 	*errorp = 0;
1232 	/*
1233 	 * See if address already in list.
1234 	 */
1235 	IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1236 	if (in6m != NULL) {
1237 		/*
1238 		 * Found it; just increment the refrence count.
1239 		 */
1240 		in6m->in6m_refcnt++;
1241 	} else {
1242 		if (ifp->if_ioctl == NULL) {
1243 			*errorp = ENXIO; /* XXX: appropriate? */
1244 			return (NULL);
1245 		}
1246 
1247 		/*
1248 		 * New address; allocate a new multicast record
1249 		 * and link it into the interface's multicast list.
1250 		 */
1251 		in6m = malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT | M_ZERO);
1252 		if (in6m == NULL) {
1253 			*errorp = ENOBUFS;
1254 			return (NULL);
1255 		}
1256 
1257 		in6m->in6m_sin.sin6_len = sizeof(struct sockaddr_in6);
1258 		in6m->in6m_sin.sin6_family = AF_INET6;
1259 		in6m->in6m_sin.sin6_addr = *maddr6;
1260 		in6m->in6m_refcnt = 1;
1261 		in6m->in6m_ifidx = ifp->if_index;
1262 		in6m->in6m_ifma.ifma_addr = sin6tosa(&in6m->in6m_sin);
1263 
1264 		/*
1265 		 * Ask the network driver to update its multicast reception
1266 		 * filter appropriately for the new address.
1267 		 */
1268 		memcpy(&ifr.ifr_addr, &in6m->in6m_sin, sizeof(in6m->in6m_sin));
1269 		*errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
1270 		if (*errorp) {
1271 			free(in6m, M_IPMADDR, sizeof(*in6m));
1272 			return (NULL);
1273 		}
1274 
1275 		s = splsoftnet();
1276 		TAILQ_INSERT_HEAD(&ifp->if_maddrlist, &in6m->in6m_ifma,
1277 		    ifma_list);
1278 		splx(s);
1279 
1280 		/*
1281 		 * Let MLD6 know that we have joined a new IP6 multicast
1282 		 * group.
1283 		 */
1284 		mld6_start_listening(in6m);
1285 	}
1286 
1287 	return (in6m);
1288 }
1289 
1290 /*
1291  * Delete a multicast address record.
1292  */
1293 void
1294 in6_delmulti(struct in6_multi *in6m)
1295 {
1296 	struct	in6_ifreq ifr;
1297 	struct	ifnet *ifp;
1298 	int	s;
1299 
1300 	if (--in6m->in6m_refcnt == 0) {
1301 		/*
1302 		 * No remaining claims to this record; let MLD6 know
1303 		 * that we are leaving the multicast group.
1304 		 */
1305 		mld6_stop_listening(in6m);
1306 		ifp = if_get(in6m->in6m_ifidx);
1307 
1308 		/*
1309 		 * Notify the network driver to update its multicast
1310 		 * reception filter.
1311 		 */
1312 		if (ifp != NULL) {
1313 			bzero(&ifr.ifr_addr, sizeof(struct sockaddr_in6));
1314 			ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1315 			ifr.ifr_addr.sin6_family = AF_INET6;
1316 			ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
1317 			(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
1318 
1319 			s = splsoftnet();
1320 			TAILQ_REMOVE(&ifp->if_maddrlist, &in6m->in6m_ifma,
1321 			    ifma_list);
1322 			splx(s);
1323 		}
1324 		if_put(ifp);
1325 
1326 		free(in6m, M_IPMADDR, sizeof(*in6m));
1327 	}
1328 }
1329 
1330 /*
1331  * Return 1 if the multicast group represented by ``maddr6'' has been
1332  * joined by interface ``ifp'', 0 otherwise.
1333  */
1334 int
1335 in6_hasmulti(struct in6_addr *maddr6, struct ifnet *ifp)
1336 {
1337 	struct in6_multi *in6m;
1338 	int joined;
1339 
1340 	IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
1341 	joined = (in6m != NULL);
1342 
1343 	return (joined);
1344 }
1345 
1346 struct in6_multi_mship *
1347 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1348 {
1349 	struct in6_multi_mship *imm;
1350 
1351 	imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1352 	if (!imm) {
1353 		*errorp = ENOBUFS;
1354 		return NULL;
1355 	}
1356 	imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1357 	if (!imm->i6mm_maddr) {
1358 		/* *errorp is alrady set */
1359 		free(imm, M_IPMADDR, sizeof(*imm));
1360 		return NULL;
1361 	}
1362 	return imm;
1363 }
1364 
1365 int
1366 in6_leavegroup(struct in6_multi_mship *imm)
1367 {
1368 
1369 	if (imm->i6mm_maddr)
1370 		in6_delmulti(imm->i6mm_maddr);
1371 	free(imm,  M_IPMADDR, sizeof(*imm));
1372 	return 0;
1373 }
1374 
1375 /*
1376  * Find an IPv6 interface link-local address specific to an interface.
1377  */
1378 struct in6_ifaddr *
1379 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1380 {
1381 	struct ifaddr *ifa;
1382 
1383 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1384 		if (ifa->ifa_addr->sa_family != AF_INET6)
1385 			continue;
1386 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1387 			if ((ifatoia6(ifa)->ia6_flags & ignoreflags) != 0)
1388 				continue;
1389 			break;
1390 		}
1391 	}
1392 
1393 	return (ifatoia6(ifa));
1394 }
1395 
1396 
1397 /*
1398  * find the internet address corresponding to a given interface and address.
1399  */
1400 struct in6_ifaddr *
1401 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1402 {
1403 	struct ifaddr *ifa;
1404 
1405 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1406 		if (ifa->ifa_addr->sa_family != AF_INET6)
1407 			continue;
1408 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1409 			break;
1410 	}
1411 
1412 	return (ifatoia6(ifa));
1413 }
1414 
1415 /*
1416  * Get a scope of the address. Node-local, link-local, site-local or global.
1417  */
1418 int
1419 in6_addrscope(struct in6_addr *addr)
1420 {
1421 	int scope;
1422 
1423 	if (addr->s6_addr8[0] == 0xfe) {
1424 		scope = addr->s6_addr8[1] & 0xc0;
1425 
1426 		switch (scope) {
1427 		case 0x80:
1428 			return __IPV6_ADDR_SCOPE_LINKLOCAL;
1429 			break;
1430 		case 0xc0:
1431 			return __IPV6_ADDR_SCOPE_SITELOCAL;
1432 			break;
1433 		default:
1434 			return __IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
1435 			break;
1436 		}
1437 	}
1438 
1439 
1440 	if (addr->s6_addr8[0] == 0xff) {
1441 		scope = addr->s6_addr8[1] & 0x0f;
1442 
1443 		/*
1444 		 * due to other scope such as reserved,
1445 		 * return scope doesn't work.
1446 		 */
1447 		switch (scope) {
1448 		case __IPV6_ADDR_SCOPE_INTFACELOCAL:
1449 			return __IPV6_ADDR_SCOPE_INTFACELOCAL;
1450 			break;
1451 		case __IPV6_ADDR_SCOPE_LINKLOCAL:
1452 			return __IPV6_ADDR_SCOPE_LINKLOCAL;
1453 			break;
1454 		case __IPV6_ADDR_SCOPE_SITELOCAL:
1455 			return __IPV6_ADDR_SCOPE_SITELOCAL;
1456 			break;
1457 		default:
1458 			return __IPV6_ADDR_SCOPE_GLOBAL;
1459 			break;
1460 		}
1461 	}
1462 
1463 	if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
1464 		if (addr->s6_addr8[15] == 1) /* loopback */
1465 			return __IPV6_ADDR_SCOPE_INTFACELOCAL;
1466 		if (addr->s6_addr8[15] == 0) /* unspecified */
1467 			return __IPV6_ADDR_SCOPE_LINKLOCAL;
1468 	}
1469 
1470 	return __IPV6_ADDR_SCOPE_GLOBAL;
1471 }
1472 
1473 int
1474 in6_addr2scopeid(unsigned int ifidx, struct in6_addr *addr)
1475 {
1476 	int scope = in6_addrscope(addr);
1477 
1478 	switch(scope) {
1479 	case __IPV6_ADDR_SCOPE_INTFACELOCAL:
1480 	case __IPV6_ADDR_SCOPE_LINKLOCAL:
1481 		/* XXX: we do not distinguish between a link and an I/F. */
1482 		return (ifidx);
1483 
1484 	case __IPV6_ADDR_SCOPE_SITELOCAL:
1485 		return (0);	/* XXX: invalid. */
1486 
1487 	default:
1488 		return (0);	/* XXX: treat as global. */
1489 	}
1490 }
1491 
1492 /*
1493  * return length of part which dst and src are equal
1494  * hard coding...
1495  */
1496 int
1497 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1498 {
1499 	int match = 0;
1500 	u_char *s = (u_char *)src, *d = (u_char *)dst;
1501 	u_char *lim = s + 16, r;
1502 
1503 	while (s < lim)
1504 		if ((r = (*d++ ^ *s++)) != 0) {
1505 			while (r < 128) {
1506 				match++;
1507 				r <<= 1;
1508 			}
1509 			break;
1510 		} else
1511 			match += 8;
1512 	return match;
1513 }
1514 
1515 int
1516 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1517 {
1518 	int bytelen, bitlen;
1519 
1520 	/* sanity check */
1521 	if (0 > len || len > 128) {
1522 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1523 		    len);
1524 		return (0);
1525 	}
1526 
1527 	bytelen = len / 8;
1528 	bitlen = len % 8;
1529 
1530 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1531 		return (0);
1532 	/* len == 128 is ok because bitlen == 0 then */
1533 	if (bitlen != 0 &&
1534 	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
1535 	    p2->s6_addr[bytelen] >> (8 - bitlen))
1536 		return (0);
1537 
1538 	return (1);
1539 }
1540 
1541 void
1542 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1543 {
1544 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1545 	int bytelen, bitlen, i;
1546 
1547 	/* sanity check */
1548 	if (0 > len || len > 128) {
1549 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1550 		    len);
1551 		return;
1552 	}
1553 
1554 	bzero(maskp, sizeof(*maskp));
1555 	bytelen = len / 8;
1556 	bitlen = len % 8;
1557 	for (i = 0; i < bytelen; i++)
1558 		maskp->s6_addr[i] = 0xff;
1559 	/* len == 128 is ok because bitlen == 0 then */
1560 	if (bitlen)
1561 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1562 }
1563 
1564 /*
1565  * return the best address out of the same scope
1566  */
1567 struct in6_ifaddr *
1568 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst, u_int rdomain)
1569 {
1570 	int dst_scope =	in6_addrscope(dst), src_scope, best_scope = 0;
1571 	int blen = -1;
1572 	struct ifaddr *ifa;
1573 	struct ifnet *ifp;
1574 	struct in6_ifaddr *ia6_best = NULL;
1575 
1576 	if (oifp == NULL) {
1577 		printf("in6_ifawithscope: output interface is not specified\n");
1578 		return (NULL);
1579 	}
1580 
1581 	/*
1582 	 * We search for all addresses on all interfaces from the beginning.
1583 	 * Comparing an interface with the outgoing interface will be done
1584 	 * only at the final stage of tiebreaking.
1585 	 */
1586 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1587 		if (ifp->if_rdomain != rdomain)
1588 			continue;
1589 #if NCARP > 0
1590 		/*
1591 		 * Never use a carp address of an interface which is not
1592 		 * the master.
1593 		 */
1594 		if (ifp->if_type == IFT_CARP && !carp_iamatch6(ifp))
1595 			continue;
1596 #endif
1597 
1598 		/*
1599 		 * We can never take an address that breaks the scope zone
1600 		 * of the destination.
1601 		 */
1602 		if (in6_addr2scopeid(ifp->if_index, dst) !=
1603 		    in6_addr2scopeid(oifp->if_index, dst))
1604 			continue;
1605 
1606 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1607 			int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1608 
1609 			if (ifa->ifa_addr->sa_family != AF_INET6)
1610 				continue;
1611 
1612 			src_scope = in6_addrscope(IFA_IN6(ifa));
1613 
1614 #ifdef ADDRSELECT_DEBUG		/* should be removed after stabilization */
1615 		{
1616 			char adst[INET6_ADDRSTRLEN], asrc[INET6_ADDRSTRLEN];
1617 			char bestaddr[INET6_ADDRSTRLEN];
1618 
1619 
1620 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
1621 			printf("in6_ifawithscope: dst=%s bestaddr=%s, "
1622 			       "newaddr=%s, scope=%x, dcmp=%d, bcmp=%d, "
1623 			       "matchlen=%d, flgs=%x\n",
1624 			       inet_ntop(AF_INET6, dst, adst, sizeof(adst)),
1625 			       (ia6_best == NULL) ? "none" :
1626 			       inet_ntop(AF_INET6, &ia6_best->ia_addr.sin6_addr,
1627 			           bestaddr, sizeof(bestaddr)),
1628 			       inet_ntop(AF_INET6, IFA_IN6(ifa),
1629 			           asrc, sizeof(asrc)),
1630 			       src_scope, dscopecmp,
1631 			       ia6_best ? IN6_ARE_SCOPE_CMP(src_scope, best_scope) : -1,
1632 			       in6_matchlen(IFA_IN6(ifa), dst),
1633 			       ifatoia6(ifa)->ia6_flags);
1634 		}
1635 #endif
1636 
1637 			/*
1638 			 * Don't use an address before completing DAD
1639 			 * nor a duplicated address.
1640 			 */
1641 			if (ifatoia6(ifa)->ia6_flags &
1642 			    (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED))
1643 				continue;
1644 
1645 			/* XXX: is there any case to allow anycasts? */
1646 			if (ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST)
1647 				continue;
1648 
1649 			if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DETACHED)
1650 				continue;
1651 
1652 			/*
1653 			 * If this is the first address we find,
1654 			 * keep it anyway.
1655 			 */
1656 			if (ia6_best == NULL)
1657 				goto replace;
1658 
1659 			/*
1660 			 * ia6_best is never NULL beyond this line except
1661 			 * within the block labeled "replace".
1662 			 */
1663 
1664 			/*
1665 			 * If ia6_best has a smaller scope than dst and
1666 			 * the current address has a larger one than
1667 			 * (or equal to) dst, always replace ia6_best.
1668 			 * Also, if the current address has a smaller scope
1669 			 * than dst, ignore it unless ia6_best also has a
1670 			 * smaller scope.
1671 			 */
1672 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1673 			    IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1674 				goto replace;
1675 			if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
1676 			    IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
1677 				continue;
1678 
1679 			/*
1680 			 * A deprecated address SHOULD NOT be used in new
1681 			 * communications if an alternate (non-deprecated)
1682 			 * address is available and has sufficient scope.
1683 			 * RFC 2462, Section 5.5.4.
1684 			 */
1685 			if (ifatoia6(ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1686 				/*
1687 				 * Ignore any deprecated addresses if
1688 				 * specified by configuration.
1689 				 */
1690 				if (!ip6_use_deprecated)
1691 					continue;
1692 
1693 				/*
1694 				 * If we have already found a non-deprecated
1695 				 * candidate, just ignore deprecated addresses.
1696 				 */
1697 				if ((ia6_best->ia6_flags & IN6_IFF_DEPRECATED)
1698 				    == 0)
1699 					continue;
1700 			}
1701 
1702 			/*
1703 			 * A non-deprecated address is always preferred
1704 			 * to a deprecated one regardless of scopes and
1705 			 * address matching.
1706 			 */
1707 			if ((ia6_best->ia6_flags & IN6_IFF_DEPRECATED) &&
1708 			    (ifatoia6(ifa)->ia6_flags &
1709 			     IN6_IFF_DEPRECATED) == 0)
1710 				goto replace;
1711 
1712 			/* RFC 3484 5. Rule 5: Prefer outgoing interface */
1713 			if (ia6_best->ia_ifp == oifp && ifp != oifp)
1714 				continue;
1715 			if (ia6_best->ia_ifp != oifp && ifp == oifp)
1716 				goto replace;
1717 
1718 			/*
1719 			 * At this point, we have two cases:
1720 			 * 1. we are looking at a non-deprecated address,
1721 			 *    and ia6_best is also non-deprecated.
1722 			 * 2. we are looking at a deprecated address,
1723 			 *    and ia6_best is also deprecated.
1724 			 * Also, we do not have to consider a case where
1725 			 * the scope of if_best is larger(smaller) than dst and
1726 			 * the scope of the current address is smaller(larger)
1727 			 * than dst. Such a case has already been covered.
1728 			 * Tiebreaking is done according to the following
1729 			 * items:
1730 			 * - the scope comparison between the address and
1731 			 *   dst (dscopecmp)
1732 			 * - the scope comparison between the address and
1733 			 *   ia6_best (bscopecmp)
1734 			 * - if the address match dst longer than ia6_best
1735 			 *   (matchcmp)
1736 			 * - if the address is on the outgoing I/F (outI/F)
1737 			 *
1738 			 * Roughly speaking, the selection policy is
1739 			 * - the most important item is scope. The same scope
1740 			 *   is best. Then search for a larger scope.
1741 			 *   Smaller scopes are the last resort.
1742 			 * - A deprecated address is chosen only when we have
1743 			 *   no address that has an enough scope, but is
1744 			 *   prefered to any addresses of smaller scopes.
1745 			 * - Longest address match against dst is considered
1746 			 *   only for addresses that has the same scope of dst.
1747 			 * - If there is no other reasons to choose one,
1748 			 *   addresses on the outgoing I/F are preferred.
1749 			 *
1750 			 * The precise decision table is as follows:
1751 			 * dscopecmp bscopecmp matchcmp outI/F | replace?
1752 			 *    !equal     equal      N/A    Yes |      Yes (1)
1753 			 *    !equal     equal      N/A     No |       No (2)
1754 			 *    larger    larger      N/A    N/A |       No (3)
1755 			 *    larger   smaller      N/A    N/A |      Yes (4)
1756 			 *   smaller    larger      N/A    N/A |      Yes (5)
1757 			 *   smaller   smaller      N/A    N/A |       No (6)
1758 			 *     equal   smaller      N/A    N/A |      Yes (7)
1759 			 *     equal    larger       (already done)
1760 			 *     equal     equal   larger    N/A |      Yes (8)
1761 			 *     equal     equal  smaller    N/A |       No (9)
1762 			 *     equal     equal    equal    Yes |      Yes (a)
1763 			 *     equal     equal    equal     No |       No (b)
1764 			 */
1765 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
1766 			bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
1767 
1768 			if (dscopecmp && bscopecmp == 0) {
1769 				if (oifp == ifp) /* (1) */
1770 					goto replace;
1771 				continue; /* (2) */
1772 			}
1773 			if (dscopecmp > 0) {
1774 				if (bscopecmp > 0) /* (3) */
1775 					continue;
1776 				goto replace; /* (4) */
1777 			}
1778 			if (dscopecmp < 0) {
1779 				if (bscopecmp > 0) /* (5) */
1780 					goto replace;
1781 				continue; /* (6) */
1782 			}
1783 
1784 			/* now dscopecmp must be 0 */
1785 			if (bscopecmp < 0)
1786 				goto replace; /* (7) */
1787 
1788 			/*
1789 			 * At last both dscopecmp and bscopecmp must be 0.
1790 			 * We need address matching against dst for
1791 			 * tiebreaking.
1792 			 * Privacy addresses are preferred over public
1793 			 * addresses (RFC3484 requires a config knob for
1794 			 * this which we don't provide).
1795 			 */
1796 			if (oifp == ifp) {
1797 				/* Do not replace temporary autoconf addresses
1798 				 * with non-temporary addresses. */
1799 				if ((ia6_best->ia6_flags & IN6_IFF_PRIVACY) &&
1800 			            !(ifatoia6(ifa)->ia6_flags &
1801 				    IN6_IFF_PRIVACY))
1802 					continue;
1803 
1804 				/* Replace non-temporary autoconf addresses
1805 				 * with temporary addresses. */
1806 				if (!(ia6_best->ia6_flags & IN6_IFF_PRIVACY) &&
1807 			            (ifatoia6(ifa)->ia6_flags &
1808 				    IN6_IFF_PRIVACY))
1809 					goto replace;
1810 			}
1811 			tlen = in6_matchlen(IFA_IN6(ifa), dst);
1812 			matchcmp = tlen - blen;
1813 			if (matchcmp > 0) { /* (8) */
1814 #if NCARP > 0
1815 				/*
1816 				 * Don't let carp interfaces win a tie against
1817 				 * the output interface based on matchlen.
1818 				 * We should only use a carp address if no
1819 				 * other interface has a usable address.
1820 				 * Otherwise, when communicating from a carp
1821 				 * master to a carp slave, the slave won't
1822 				 * respond since the carp address is also
1823 				 * configured as a local address on the slave.
1824 				 * Note that carp interfaces in backup state
1825 				 * were already skipped above.
1826 				 */
1827 				if (ifp->if_type == IFT_CARP &&
1828 				    oifp->if_type != IFT_CARP)
1829 					continue;
1830 #endif
1831 				goto replace;
1832 			}
1833 			if (matchcmp < 0) /* (9) */
1834 				continue;
1835 			if (oifp == ifp) /* (a) */
1836 				goto replace;
1837 			continue; /* (b) */
1838 
1839 		  replace:
1840 			ia6_best = ifatoia6(ifa);
1841 			blen = tlen >= 0 ? tlen :
1842 				in6_matchlen(IFA_IN6(ifa), dst);
1843 			best_scope = in6_addrscope(&ia6_best->ia_addr.sin6_addr);
1844 		}
1845 	}
1846 
1847 	/* count statistics for future improvements */
1848 	if (ia6_best == NULL)
1849 		ip6stat.ip6s_sources_none++;
1850 	else {
1851 		if (oifp == ia6_best->ia_ifp)
1852 			ip6stat.ip6s_sources_sameif[best_scope]++;
1853 		else
1854 			ip6stat.ip6s_sources_otherif[best_scope]++;
1855 
1856 		if (best_scope == dst_scope)
1857 			ip6stat.ip6s_sources_samescope[best_scope]++;
1858 		else
1859 			ip6stat.ip6s_sources_otherscope[best_scope]++;
1860 
1861 		if ((ia6_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1862 			ip6stat.ip6s_sources_deprecated[best_scope]++;
1863 	}
1864 
1865 	return (ia6_best);
1866 }
1867 
1868 int
1869 in6if_do_dad(struct ifnet *ifp)
1870 {
1871 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1872 		return (0);
1873 
1874 	switch (ifp->if_type) {
1875 #if NCARP > 0
1876 	case IFT_CARP:
1877 		/*
1878 		 * XXX: DAD does not work currently on carp(4)
1879 		 * so disable it for now.
1880 		 */
1881 		return (0);
1882 #endif
1883 	default:
1884 		/*
1885 		 * Our DAD routine requires the interface up and running.
1886 		 * However, some interfaces can be up before the RUNNING
1887 		 * status.  Additionaly, users may try to assign addresses
1888 		 * before the interface becomes up (or running).
1889 		 * We simply skip DAD in such a case as a work around.
1890 		 * XXX: we should rather mark "tentative" on such addresses,
1891 		 * and do DAD after the interface becomes ready.
1892 		 */
1893 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1894 		    (IFF_UP|IFF_RUNNING))
1895 			return (0);
1896 
1897 		return (1);
1898 	}
1899 }
1900 
1901 void *
1902 in6_domifattach(struct ifnet *ifp)
1903 {
1904 	struct in6_ifextra *ext;
1905 
1906 	ext = malloc(sizeof(*ext), M_IFADDR, M_WAITOK | M_ZERO);
1907 
1908 	ext->nd_ifinfo = nd6_ifattach(ifp);
1909 	ext->nprefixes = 0;
1910 	ext->ndefrouters = 0;
1911 	return ext;
1912 }
1913 
1914 void
1915 in6_domifdetach(struct ifnet *ifp, void *aux)
1916 {
1917 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
1918 
1919 	nd6_ifdetach(ext->nd_ifinfo);
1920 	free(ext, M_IFADDR, sizeof(*ext));
1921 }
1922