xref: /freebsd/sys/netinet6/in6.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*	$FreeBSD$	*/
2 /*	$KAME: in6.c,v 1.187 2001/05/24 07:43:59 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 #include "opt_inet6.h"
70 
71 #include <sys/param.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 <net/if_dl.h>
87 
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/if_ether.h>
91 #ifndef SCOPEDROUTING
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #endif
96 
97 #include <netinet6/nd6.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/mld6_var.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_ifattach.h>
103 #include <netinet6/scope6_var.h>
104 #ifndef SCOPEDROUTING
105 #include <netinet6/in6_pcb.h>
106 #endif
107 
108 #include <net/net_osdep.h>
109 
110 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
111 
112 /*
113  * Definitions of some costant IP6 addresses.
114  */
115 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
116 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
117 const struct in6_addr in6addr_nodelocal_allnodes =
118 	IN6ADDR_NODELOCAL_ALLNODES_INIT;
119 const struct in6_addr in6addr_linklocal_allnodes =
120 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
121 const struct in6_addr in6addr_linklocal_allrouters =
122 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
123 
124 const struct in6_addr in6mask0 = IN6MASK0;
125 const struct in6_addr in6mask32 = IN6MASK32;
126 const struct in6_addr in6mask64 = IN6MASK64;
127 const struct in6_addr in6mask96 = IN6MASK96;
128 const struct in6_addr in6mask128 = IN6MASK128;
129 
130 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
131 				     0, 0, IN6ADDR_ANY_INIT, 0};
132 
133 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
134 	struct ifnet *, struct proc *));
135 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
136 			   struct sockaddr_in6 *, int));
137 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
138 
139 struct in6_multihead in6_multihead;	/* XXX BSS initialization */
140 
141 /*
142  * Subroutine for in6_ifaddloop() and in6_ifremloop().
143  * This routine does actual work.
144  */
145 static void
146 in6_ifloop_request(int cmd, struct ifaddr *ifa)
147 {
148 	struct sockaddr_in6 all1_sa;
149 	struct rtentry *nrt = NULL;
150 	int e;
151 
152 	bzero(&all1_sa, sizeof(all1_sa));
153 	all1_sa.sin6_family = AF_INET6;
154 	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
155 	all1_sa.sin6_addr = in6mask128;
156 
157 	/*
158 	 * We specify the address itself as the gateway, and set the
159 	 * RTF_LLINFO flag, so that the corresponding host route would have
160 	 * the flag, and thus applications that assume traditional behavior
161 	 * would be happy.  Note that we assume the caller of the function
162 	 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
163 	 * which changes the outgoing interface to the loopback interface.
164 	 */
165 	e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
166 		      (struct sockaddr *)&all1_sa,
167 		      RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
168 	if (e != 0) {
169 		log(LOG_ERR, "in6_ifloop_request: "
170 		    "%s operation failed for %s (errno=%d)\n",
171 		    cmd == RTM_ADD ? "ADD" : "DELETE",
172 		    ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
173 		    e);
174 	}
175 
176 	/*
177 	 * Make sure rt_ifa be equal to IFA, the second argument of the
178 	 * function.
179 	 * We need this because when we refer to rt_ifa->ia6_flags in
180 	 * ip6_input, we assume that the rt_ifa points to the address instead
181 	 * of the loopback address.
182 	 */
183 	if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
184 		IFAFREE(nrt->rt_ifa);
185 		IFAREF(ifa);
186 		nrt->rt_ifa = ifa;
187 	}
188 
189 	/*
190 	 * Report the addition/removal of the address to the routing socket.
191 	 * XXX: since we called rtinit for a p2p interface with a destination,
192 	 *      we end up reporting twice in such a case.  Should we rather
193 	 *      omit the second report?
194 	 */
195 	if (nrt) {
196 		rt_newaddrmsg(cmd, ifa, e, nrt);
197 		if (cmd == RTM_DELETE) {
198 			if (nrt->rt_refcnt <= 0) {
199 				/* XXX: we should free the entry ourselves. */
200 				nrt->rt_refcnt++;
201 				rtfree(nrt);
202 			}
203 		} else {
204 			/* the cmd must be RTM_ADD here */
205 			nrt->rt_refcnt--;
206 		}
207 	}
208 }
209 
210 /*
211  * Add ownaddr as loopback rtentry.  We previously add the route only if
212  * necessary (ex. on a p2p link).  However, since we now manage addresses
213  * separately from prefixes, we should always add the route.  We can't
214  * rely on the cloning mechanism from the corresponding interface route
215  * any more.
216  */
217 static void
218 in6_ifaddloop(struct ifaddr *ifa)
219 {
220 	struct rtentry *rt;
221 
222 	/* If there is no loopback entry, allocate one. */
223 	rt = rtalloc1(ifa->ifa_addr, 0, 0);
224 	if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
225 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
226 		in6_ifloop_request(RTM_ADD, ifa);
227 	if (rt)
228 		rt->rt_refcnt--;
229 }
230 
231 /*
232  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
233  * if it exists.
234  */
235 static void
236 in6_ifremloop(struct ifaddr *ifa)
237 {
238 	struct in6_ifaddr *ia;
239 	struct rtentry *rt;
240 	int ia_count = 0;
241 
242 	/*
243 	 * Some of BSD variants do not remove cloned routes
244 	 * from an interface direct route, when removing the direct route
245 	 * (see comments in net/net_osdep.h).  Even for variants that do remove
246 	 * cloned routes, they could fail to remove the cloned routes when
247 	 * we handle multple addresses that share a common prefix.
248 	 * So, we should remove the route corresponding to the deleted address
249 	 * regardless of the result of in6_is_ifloop_auto().
250 	 */
251 
252 	/*
253 	 * Delete the entry only if exact one ifa exists. More than one ifa
254 	 * can exist if we assign a same single address to multiple
255 	 * (probably p2p) interfaces.
256 	 * XXX: we should avoid such a configuration in IPv6...
257 	 */
258 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
259 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
260 			ia_count++;
261 			if (ia_count > 1)
262 				break;
263 		}
264 	}
265 
266 	if (ia_count == 1) {
267 		/*
268 		 * Before deleting, check if a corresponding loopbacked host
269 		 * route surely exists. With this check, we can avoid to
270 		 * delete an interface direct route whose destination is same
271 		 * as the address being removed. This can happen when remofing
272 		 * a subnet-router anycast address on an interface attahced
273 		 * to a shared medium.
274 		 */
275 		rt = rtalloc1(ifa->ifa_addr, 0, 0);
276 		if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
277 		    (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
278 			rt->rt_refcnt--;
279 			in6_ifloop_request(RTM_DELETE, ifa);
280 		}
281 	}
282 }
283 
284 int
285 in6_ifindex2scopeid(idx)
286 	int idx;
287 {
288 	struct ifnet *ifp;
289 	struct ifaddr *ifa;
290 	struct sockaddr_in6 *sin6;
291 
292 	if (idx < 0 || if_index < idx)
293 		return -1;
294 	ifp = ifindex2ifnet[idx];
295 
296 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
297 	{
298 		if (ifa->ifa_addr->sa_family != AF_INET6)
299 			continue;
300 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
301 		if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
302 			return sin6->sin6_scope_id & 0xffff;
303 	}
304 
305 	return -1;
306 }
307 
308 int
309 in6_mask2len(mask, lim0)
310 	struct in6_addr *mask;
311 	u_char *lim0;
312 {
313 	int x = 0, y;
314 	u_char *lim = lim0, *p;
315 
316 	if (lim0 == NULL ||
317 	    lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
318 		lim = (u_char *)mask + sizeof(*mask);
319 	for (p = (u_char *)mask; p < lim; x++, p++) {
320 		if (*p != 0xff)
321 			break;
322 	}
323 	y = 0;
324 	if (p < lim) {
325 		for (y = 0; y < 8; y++) {
326 			if ((*p & (0x80 >> y)) == 0)
327 				break;
328 		}
329 	}
330 
331 	/*
332 	 * when the limit pointer is given, do a stricter check on the
333 	 * remaining bits.
334 	 */
335 	if (p < lim) {
336 		if (y != 0 && (*p & (0x00ff >> y)) != 0)
337 			return(-1);
338 		for (p = p + 1; p < lim; p++)
339 			if (*p != 0)
340 				return(-1);
341 	}
342 
343 	return x * 8 + y;
344 }
345 
346 void
347 in6_len2mask(mask, len)
348 	struct in6_addr *mask;
349 	int len;
350 {
351 	int i;
352 
353 	bzero(mask, sizeof(*mask));
354 	for (i = 0; i < len / 8; i++)
355 		mask->s6_addr8[i] = 0xff;
356 	if (len % 8)
357 		mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
358 }
359 
360 #define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
361 #define ia62ifa(ia6)	(&((ia6)->ia_ifa))
362 
363 int
364 in6_control(so, cmd, data, ifp, p)
365 	struct	socket *so;
366 	u_long cmd;
367 	caddr_t	data;
368 	struct ifnet *ifp;
369 	struct proc *p;
370 {
371 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
372 	struct	in6_ifaddr *ia = NULL;
373 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
374 	int privileged;
375 
376 	privileged = 0;
377 	if (p == NULL || !suser(p))
378 		privileged++;
379 
380 	switch (cmd) {
381 	case SIOCGETSGCNT_IN6:
382 	case SIOCGETMIFCNT_IN6:
383 		return (mrt6_ioctl(cmd, data));
384 	}
385 
386 	if (ifp == NULL)
387 		return(EOPNOTSUPP);
388 
389 	switch (cmd) {
390 	case SIOCSNDFLUSH_IN6:
391 	case SIOCSPFXFLUSH_IN6:
392 	case SIOCSRTRFLUSH_IN6:
393 	case SIOCSDEFIFACE_IN6:
394 	case SIOCSIFINFO_FLAGS:
395 		if (!privileged)
396 			return(EPERM);
397 		/*fall through*/
398 	case OSIOCGIFINFO_IN6:
399 	case SIOCGIFINFO_IN6:
400 	case SIOCGDRLST_IN6:
401 	case SIOCGPRLST_IN6:
402 	case SIOCGNBRINFO_IN6:
403 	case SIOCGDEFIFACE_IN6:
404 		return(nd6_ioctl(cmd, data, ifp));
405 	}
406 
407 	switch (cmd) {
408 	case SIOCSIFPREFIX_IN6:
409 	case SIOCDIFPREFIX_IN6:
410 	case SIOCAIFPREFIX_IN6:
411 	case SIOCCIFPREFIX_IN6:
412 	case SIOCSGIFPREFIX_IN6:
413 	case SIOCGIFPREFIX_IN6:
414 		log(LOG_NOTICE,
415 		    "prefix ioctls are now invalidated. "
416 		    "please use ifconfig.\n");
417 		return(EOPNOTSUPP);
418 	}
419 
420 	switch(cmd) {
421 	case SIOCSSCOPE6:
422 		if (!privileged)
423 			return(EPERM);
424 		return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
425 		break;
426 	case SIOCGSCOPE6:
427 		return(scope6_get(ifp, ifr->ifr_ifru.ifru_scope_id));
428 		break;
429 	case SIOCGSCOPE6DEF:
430 		return(scope6_get_default(ifr->ifr_ifru.ifru_scope_id));
431 		break;
432 	}
433 
434 	switch (cmd) {
435 	case SIOCALIFADDR:
436 	case SIOCDLIFADDR:
437 		if (!privileged)
438 			return(EPERM);
439 		/*fall through*/
440 	case SIOCGLIFADDR:
441 		return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
442 	}
443 
444 	/*
445 	 * Find address for this interface, if it exists.
446 	 */
447 	if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
448 		struct sockaddr_in6 *sa6 =
449 			(struct sockaddr_in6 *)&ifra->ifra_addr;
450 
451 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
452 			if (sa6->sin6_addr.s6_addr16[1] == 0) {
453 				/* link ID is not embedded by the user */
454 				sa6->sin6_addr.s6_addr16[1] =
455 					htons(ifp->if_index);
456 			} else if (sa6->sin6_addr.s6_addr16[1] !=
457 				    htons(ifp->if_index)) {
458 				return(EINVAL);	/* link ID contradicts */
459 			}
460 			if (sa6->sin6_scope_id) {
461 				if (sa6->sin6_scope_id !=
462 				    (u_int32_t)ifp->if_index)
463 					return(EINVAL);
464 				sa6->sin6_scope_id = 0; /* XXX: good way? */
465 			}
466 		}
467 		ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
468 	}
469 
470 	switch (cmd) {
471 	case SIOCSIFADDR_IN6:
472 	case SIOCSIFDSTADDR_IN6:
473 	case SIOCSIFNETMASK_IN6:
474 		/*
475 		 * Since IPv6 allows a node to assign multiple addresses
476 		 * on a single interface, SIOCSIFxxx ioctls are not suitable
477 		 * and should be unused.
478 		 */
479 		/* we decided to obsolete this command (20000704) */
480 		return(EINVAL);
481 
482 	case SIOCDIFADDR_IN6:
483 		/*
484 		 * for IPv4, we look for existing in_ifaddr here to allow
485 		 * "ifconfig if0 delete" to remove first IPv4 address on the
486 		 * interface.  For IPv6, as the spec allow multiple interface
487 		 * address from the day one, we consider "remove the first one"
488 		 * semantics to be not preferable.
489 		 */
490 		if (ia == NULL)
491 			return(EADDRNOTAVAIL);
492 		/* FALLTHROUGH */
493 	case SIOCAIFADDR_IN6:
494 		/*
495 		 * We always require users to specify a valid IPv6 address for
496 		 * the corresponding operation.
497 		 */
498 		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
499 		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
500 			return(EAFNOSUPPORT);
501 		if (!privileged)
502 			return(EPERM);
503 
504 		break;
505 
506 	case SIOCGIFADDR_IN6:
507 		/* This interface is basically deprecated. use SIOCGIFCONF. */
508 		/* fall through */
509 	case SIOCGIFAFLAG_IN6:
510 	case SIOCGIFNETMASK_IN6:
511 	case SIOCGIFDSTADDR_IN6:
512 	case SIOCGIFALIFETIME_IN6:
513 		/* must think again about its semantics */
514 		if (ia == NULL)
515 			return(EADDRNOTAVAIL);
516 		break;
517 	case SIOCSIFALIFETIME_IN6:
518 	    {
519 		struct in6_addrlifetime *lt;
520 
521 		if (!privileged)
522 			return(EPERM);
523 		if (ia == NULL)
524 			return(EADDRNOTAVAIL);
525 		/* sanity for overflow - beware unsigned */
526 		lt = &ifr->ifr_ifru.ifru_lifetime;
527 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
528 		 && lt->ia6t_vltime + time_second < time_second) {
529 			return EINVAL;
530 		}
531 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
532 		 && lt->ia6t_pltime + time_second < time_second) {
533 			return EINVAL;
534 		}
535 		break;
536 	    }
537 	}
538 
539 	switch (cmd) {
540 
541 	case SIOCGIFADDR_IN6:
542 		ifr->ifr_addr = ia->ia_addr;
543 		break;
544 
545 	case SIOCGIFDSTADDR_IN6:
546 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
547 			return(EINVAL);
548 		/*
549 		 * XXX: should we check if ifa_dstaddr is NULL and return
550 		 * an error?
551 		 */
552 		ifr->ifr_dstaddr = ia->ia_dstaddr;
553 		break;
554 
555 	case SIOCGIFNETMASK_IN6:
556 		ifr->ifr_addr = ia->ia_prefixmask;
557 		break;
558 
559 	case SIOCGIFAFLAG_IN6:
560 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
561 		break;
562 
563 	case SIOCGIFSTAT_IN6:
564 		if (ifp == NULL)
565 			return EINVAL;
566 		if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
567 		 || in6_ifstat[ifp->if_index] == NULL) {
568 			/* return EAFNOSUPPORT? */
569 			bzero(&ifr->ifr_ifru.ifru_stat,
570 				sizeof(ifr->ifr_ifru.ifru_stat));
571 		} else
572 			ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
573 		break;
574 
575 	case SIOCGIFSTAT_ICMP6:
576 		if (ifp == NULL)
577 			return EINVAL;
578 		if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
579 		    icmp6_ifstat[ifp->if_index] == NULL) {
580 			/* return EAFNOSUPPORT? */
581 			bzero(&ifr->ifr_ifru.ifru_stat,
582 				sizeof(ifr->ifr_ifru.ifru_icmp6stat));
583 		} else
584 			ifr->ifr_ifru.ifru_icmp6stat =
585 				*icmp6_ifstat[ifp->if_index];
586 		break;
587 
588 	case SIOCGIFALIFETIME_IN6:
589 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
590 		break;
591 
592 	case SIOCSIFALIFETIME_IN6:
593 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
594 		/* for sanity */
595 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
596 			ia->ia6_lifetime.ia6t_expire =
597 				time_second + ia->ia6_lifetime.ia6t_vltime;
598 		} else
599 			ia->ia6_lifetime.ia6t_expire = 0;
600 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
601 			ia->ia6_lifetime.ia6t_preferred =
602 				time_second + ia->ia6_lifetime.ia6t_pltime;
603 		} else
604 			ia->ia6_lifetime.ia6t_preferred = 0;
605 		break;
606 
607 	case SIOCAIFADDR_IN6:
608 	{
609 		int i, error = 0;
610 		struct nd_prefix pr0, *pr;
611 
612 		/*
613 		 * first, make or update the interface address structure,
614 		 * and link it to the list.
615 		 */
616 		if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
617 			return(error);
618 
619 		/*
620 		 * then, make the prefix on-link on the interface.
621 		 * XXX: we'd rather create the prefix before the address, but
622 		 * we need at least one address to install the corresponding
623 		 * interface route, so we configure the address first.
624 		 */
625 
626 		/*
627 		 * convert mask to prefix length (prefixmask has already
628 		 * been validated in in6_update_ifa().
629 		 */
630 		bzero(&pr0, sizeof(pr0));
631 		pr0.ndpr_ifp = ifp;
632 		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
633 					     NULL);
634 		if (pr0.ndpr_plen == 128)
635 			break;	/* we don't need to install a host route. */
636 		pr0.ndpr_prefix = ifra->ifra_addr;
637 		pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
638 		/* apply the mask for safety. */
639 		for (i = 0; i < 4; i++) {
640 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
641 				ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
642 		}
643 		/*
644 		 * XXX: since we don't have enough APIs, we just set inifinity
645 		 * to lifetimes.  They can be overridden by later advertised
646 		 * RAs (when accept_rtadv is non 0), but we'd rather intend
647 		 * such a behavior.
648 		 */
649 		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
650 		pr0.ndpr_raf_auto =
651 			((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
652 		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
653 		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
654 
655 		/* add the prefix if there's one. */
656 		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
657 			/*
658 			 * nd6_prelist_add will install the corresponding
659 			 * interface route.
660 			 */
661 			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
662 				return(error);
663 			if (pr == NULL) {
664 				log(LOG_ERR, "nd6_prelist_add succedded but "
665 				    "no prefix\n");
666 				return(EINVAL); /* XXX panic here? */
667 			}
668 		}
669 		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
670 		    == NULL) {
671 		    	/* XXX: this should not happen! */
672 			log(LOG_ERR, "in6_control: addition succeeded, but"
673 			    " no ifaddr\n");
674 		} else {
675 			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
676 			    ia->ia6_ndpr == NULL) { /* new autoconfed addr */
677 				ia->ia6_ndpr = pr;
678 				pr->ndpr_refcnt++;
679 
680 				/*
681 				 * If this is the first autoconf address from
682 				 * the prefix, create a temporary address
683 				 * as well (when specified).
684 				 */
685 				if (ip6_use_tempaddr &&
686 				    pr->ndpr_refcnt == 1) {
687 					int e;
688 					if ((e = in6_tmpifadd(ia, 1)) != 0) {
689 						log(LOG_NOTICE, "in6_control: "
690 						    "failed to create a "
691 						    "temporary address, "
692 						    "errno=%d\n",
693 						    e);
694 					}
695 				}
696 			}
697 
698 			/*
699 			 * this might affect the status of autoconfigured
700 			 * addresses, that is, this address might make
701 			 * other addresses detached.
702 			 */
703 			pfxlist_onlink_check();
704 		}
705 		break;
706 	}
707 
708 	case SIOCDIFADDR_IN6:
709 	{
710 		int i = 0;
711 		struct nd_prefix pr0, *pr;
712 
713 		/*
714 		 * If the address being deleted is the only one that owns
715 		 * the corresponding prefix, expire the prefix as well.
716 		 * XXX: theoretically, we don't have to warry about such
717 		 * relationship, since we separate the address management
718 		 * and the prefix management.  We do this, however, to provide
719 		 * as much backward compatibility as possible in terms of
720 		 * the ioctl operation.
721 		 */
722 		bzero(&pr0, sizeof(pr0));
723 		pr0.ndpr_ifp = ifp;
724 		pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
725 					     NULL);
726 		if (pr0.ndpr_plen == 128)
727 			goto purgeaddr;
728 		pr0.ndpr_prefix = ia->ia_addr;
729 		pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
730 		for (i = 0; i < 4; i++) {
731 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
732 				ia->ia_prefixmask.sin6_addr.s6_addr32[i];
733 		}
734 		/*
735 		 * The logic of the following condition is a bit complicated.
736 		 * We expire the prefix when
737 		 * 1. the address obeys autoconfiguration and it is the
738 		 *    only owner of the associated prefix, or
739 		 * 2. the address does not obey autoconf and there is no
740 		 *    other owner of the prefix.
741 		 */
742 		if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
743 		    (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
744 		      pr->ndpr_refcnt == 1) ||
745 		     ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
746 		      pr->ndpr_refcnt == 0))) {
747 			pr->ndpr_expire = 1; /* XXX: just for expiration */
748 		}
749 
750 	  purgeaddr:
751 		in6_purgeaddr(&ia->ia_ifa);
752 		break;
753 	}
754 
755 	default:
756 		if (ifp == NULL || ifp->if_ioctl == 0)
757 			return(EOPNOTSUPP);
758 		return((*ifp->if_ioctl)(ifp, cmd, data));
759 	}
760 
761 	return(0);
762 }
763 
764 /*
765  * Update parameters of an IPv6 interface address.
766  * If necessary, a new entry is created and linked into address chains.
767  * This function is separated from in6_control().
768  * XXX: should this be performed under splnet()?
769  */
770 int
771 in6_update_ifa(ifp, ifra, ia)
772 	struct ifnet *ifp;
773 	struct in6_aliasreq *ifra;
774 	struct in6_ifaddr *ia;
775 {
776 	int error = 0, hostIsNew = 0, plen = -1;
777 	struct in6_ifaddr *oia;
778 	struct sockaddr_in6 dst6;
779 	struct in6_addrlifetime *lt;
780 
781 	/* Validate parameters */
782 	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
783 		return(EINVAL);
784 
785 	/*
786 	 * The destination address for a p2p link must have a family
787 	 * of AF_UNSPEC or AF_INET6.
788 	 */
789 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
790 	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
791 	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
792 		return(EAFNOSUPPORT);
793 	/*
794 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
795 	 * does not carry fields other than sin6_len.
796 	 */
797 	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
798 		return(EINVAL);
799 	/*
800 	 * Because the IPv6 address architecture is classless, we require
801 	 * users to specify a (non 0) prefix length (mask) for a new address.
802 	 * We also require the prefix (when specified) mask is valid, and thus
803 	 * reject a non-consecutive mask.
804 	 */
805 	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
806 		return(EINVAL);
807 	if (ifra->ifra_prefixmask.sin6_len != 0) {
808 		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
809 				    (u_char *)&ifra->ifra_prefixmask +
810 				    ifra->ifra_prefixmask.sin6_len);
811 		if (plen <= 0)
812 			return(EINVAL);
813 	}
814 	else {
815 		/*
816 		 * In this case, ia must not be NULL. We just use its prefix
817 		 * length.
818 		 */
819 		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
820 	}
821 	/*
822 	 * If the destination address on a p2p interface is specified,
823 	 * and the address is a scoped one, validate/set the scope
824 	 * zone identifier.
825 	 */
826 	dst6 = ifra->ifra_dstaddr;
827 	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
828 	    (dst6.sin6_family == AF_INET6)) {
829 		int scopeid;
830 
831 #ifndef SCOPEDROUTING
832 		if ((error = in6_recoverscope(&dst6,
833 					      &ifra->ifra_dstaddr.sin6_addr,
834 					      ifp)) != 0)
835 			return(error);
836 #endif
837 		scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
838 		if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
839 			dst6.sin6_scope_id = scopeid;
840 		else if (dst6.sin6_scope_id != scopeid)
841 			return(EINVAL); /* scope ID mismatch. */
842 #ifndef SCOPEDROUTING
843 		if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
844 		    != 0)
845 			return(error);
846 		dst6.sin6_scope_id = 0; /* XXX */
847 #endif
848 	}
849 	/*
850 	 * The destination address can be specified only for a p2p or a
851 	 * loopback interface.  If specified, the corresponding prefix length
852 	 * must be 128.
853 	 */
854 	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
855 		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
856 			/* XXX: noisy message */
857 			log(LOG_INFO, "in6_update_ifa: a destination can be "
858 			    "specified for a p2p or a loopback IF only\n");
859 			return(EINVAL);
860 		}
861 		if (plen != 128) {
862 			/*
863 			 * The following message seems noisy, but we dare to
864 			 * add it for diagnosis.
865 			 */
866 			log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
867 			    "when dstaddr is specified\n");
868 			return(EINVAL);
869 		}
870 	}
871 	/* lifetime consistency check */
872 	lt = &ifra->ifra_lifetime;
873 	if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
874 	    && lt->ia6t_vltime + time_second < time_second) {
875 		return EINVAL;
876 	}
877 	if (lt->ia6t_vltime == 0) {
878 		/*
879 		 * the following log might be noisy, but this is a typical
880 		 * configuration mistake or a tool's bug.
881 		 */
882 		log(LOG_INFO,
883 		    "in6_update_ifa: valid lifetime is 0 for %s\n",
884 		    ip6_sprintf(&ifra->ifra_addr.sin6_addr));
885 	}
886 	if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
887 	    && lt->ia6t_pltime + time_second < time_second) {
888 		return EINVAL;
889 	}
890 
891 	/*
892 	 * If this is a new address, allocate a new ifaddr and link it
893 	 * into chains.
894 	 */
895 	if (ia == NULL) {
896 		hostIsNew = 1;
897 		ia = (struct in6_ifaddr *)
898 			malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
899 		if (ia == NULL)
900 			return (ENOBUFS);
901 		bzero((caddr_t)ia, sizeof(*ia));
902 		/* Initialize the address and masks */
903 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
904 		ia->ia_addr.sin6_family = AF_INET6;
905 		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
906 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
907 			/*
908 			 * XXX: some functions expect that ifa_dstaddr is not
909 			 * NULL for p2p interfaces.
910 			 */
911 			ia->ia_ifa.ifa_dstaddr
912 				= (struct sockaddr *)&ia->ia_dstaddr;
913 		} else {
914 			ia->ia_ifa.ifa_dstaddr = NULL;
915 		}
916 		ia->ia_ifa.ifa_netmask
917 			= (struct sockaddr *)&ia->ia_prefixmask;
918 
919 		ia->ia_ifp = ifp;
920 		if ((oia = in6_ifaddr) != NULL) {
921 			for ( ; oia->ia_next; oia = oia->ia_next)
922 				continue;
923 			oia->ia_next = ia;
924 		} else
925 			in6_ifaddr = ia;
926 
927 		TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
928 				  ifa_list);
929 	}
930 
931 	/* set prefix mask */
932 	if (ifra->ifra_prefixmask.sin6_len) {
933 		/*
934 		 * We prohibit changing the prefix length of an existing
935 		 * address, because
936 		 * + such an operation should be rare in IPv6, and
937 		 * + the operation would confuse prefix management.
938 		 */
939 		if (ia->ia_prefixmask.sin6_len &&
940 		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
941 			log(LOG_INFO, "in6_update_ifa: the prefix length of an"
942 			    " existing (%s) address should not be changed\n",
943 			    ip6_sprintf(&ia->ia_addr.sin6_addr));
944 			error = EINVAL;
945 			goto unlink;
946 		}
947 		ia->ia_prefixmask = ifra->ifra_prefixmask;
948 	}
949 
950 	/*
951 	 * If a new destination address is specified, scrub the old one and
952 	 * install the new destination.  Note that the interface must be
953 	 * p2p or loopback (see the check above.)
954 	 */
955 	if (dst6.sin6_family == AF_INET6 &&
956 	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
957 				&ia->ia_dstaddr.sin6_addr)) {
958 		int e;
959 
960 		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
961 		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
962 		    != 0) {
963 			log(LOG_ERR, "in6_update_ifa: failed to remove "
964 			    "a route to the old destination: %s\n",
965 			    ip6_sprintf(&ia->ia_addr.sin6_addr));
966 			/* proceed anyway... */
967 		}
968 		else
969 			ia->ia_flags &= ~IFA_ROUTE;
970 		ia->ia_dstaddr = dst6;
971 	}
972 
973 	/* reset the interface and routing table appropriately. */
974 	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
975 		goto unlink;
976 
977 	/*
978 	 * Beyond this point, we should call in6_purgeaddr upon an error,
979 	 * not just go to unlink.
980 	 */
981 
982 #if 0				/* disable this mechanism for now */
983 	/* update prefix list */
984 	if (hostIsNew &&
985 	    (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
986 		int iilen;
987 
988 		iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
989 		if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
990 			in6_purgeaddr((struct ifaddr *)ia);
991 			return(error);
992 		}
993 	}
994 #endif
995 
996 	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
997 		struct sockaddr_in6 mltaddr, mltmask;
998 		struct in6_multi *in6m;
999 
1000 		if (hostIsNew) {
1001 			/*
1002 			 * join solicited multicast addr for new host id
1003 			 */
1004 			struct in6_addr llsol;
1005 			bzero(&llsol, sizeof(struct in6_addr));
1006 			llsol.s6_addr16[0] = htons(0xff02);
1007 			llsol.s6_addr16[1] = htons(ifp->if_index);
1008 			llsol.s6_addr32[1] = 0;
1009 			llsol.s6_addr32[2] = htonl(1);
1010 			llsol.s6_addr32[3] =
1011 				ifra->ifra_addr.sin6_addr.s6_addr32[3];
1012 			llsol.s6_addr8[12] = 0xff;
1013 			(void)in6_addmulti(&llsol, ifp, &error);
1014 			if (error != 0) {
1015 				log(LOG_WARNING,
1016 				    "in6_update_ifa: addmulti failed for "
1017 				    "%s on %s (errno=%d)\n",
1018 				    ip6_sprintf(&llsol), if_name(ifp),
1019 				    error);
1020 				in6_purgeaddr((struct ifaddr *)ia);
1021 				return(error);
1022 			}
1023 		}
1024 
1025 		bzero(&mltmask, sizeof(mltmask));
1026 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
1027 		mltmask.sin6_family = AF_INET6;
1028 		mltmask.sin6_addr = in6mask32;
1029 
1030 		/*
1031 		 * join link-local all-nodes address
1032 		 */
1033 		bzero(&mltaddr, sizeof(mltaddr));
1034 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1035 		mltaddr.sin6_family = AF_INET6;
1036 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1037 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1038 
1039 		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1040 		if (in6m == NULL) {
1041 			rtrequest(RTM_ADD,
1042 				  (struct sockaddr *)&mltaddr,
1043 				  (struct sockaddr *)&ia->ia_addr,
1044 				  (struct sockaddr *)&mltmask,
1045 				  RTF_UP|RTF_CLONING,  /* xxx */
1046 				  (struct rtentry **)0);
1047 			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1048 			if (error != 0) {
1049 				log(LOG_WARNING,
1050 				    "in6_update_ifa: addmulti failed for "
1051 				    "%s on %s (errno=%d)\n",
1052 				    ip6_sprintf(&mltaddr.sin6_addr),
1053 				    if_name(ifp), error);
1054 			}
1055 		}
1056 
1057 		/*
1058 		 * join node information group address
1059 		 */
1060 #define hostnamelen	strlen(hostname)
1061 		if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1062 		    == 0) {
1063 			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1064 			if (in6m == NULL && ia != NULL) {
1065 				(void)in6_addmulti(&mltaddr.sin6_addr,
1066 				    ifp, &error);
1067 				if (error != 0) {
1068 					log(LOG_WARNING, "in6_update_ifa: "
1069 					    "addmulti failed for "
1070 					    "%s on %s (errno=%d)\n",
1071 					    ip6_sprintf(&mltaddr.sin6_addr),
1072 					    if_name(ifp), error);
1073 				}
1074 			}
1075 		}
1076 #undef hostnamelen
1077 
1078 		/*
1079 		 * join node-local all-nodes address, on loopback.
1080 		 * XXX: since "node-local" is obsoleted by interface-local,
1081 		 *      we have to join the group on every interface with
1082 		 *      some interface-boundary restriction.
1083 		 */
1084 		if (ifp->if_flags & IFF_LOOPBACK) {
1085 			struct in6_addr loop6 = in6addr_loopback;
1086 			ia = in6ifa_ifpwithaddr(ifp, &loop6);
1087 
1088 			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1089 
1090 			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1091 			if (in6m == NULL && ia != NULL) {
1092 				rtrequest(RTM_ADD,
1093 					  (struct sockaddr *)&mltaddr,
1094 					  (struct sockaddr *)&ia->ia_addr,
1095 					  (struct sockaddr *)&mltmask,
1096 					  RTF_UP,
1097 					  (struct rtentry **)0);
1098 				(void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1099 						   &error);
1100 				if (error != 0) {
1101 					log(LOG_WARNING, "in6_update_ifa: "
1102 					    "addmulti failed for %s on %s "
1103 					    "(errno=%d)\n",
1104 					    ip6_sprintf(&mltaddr.sin6_addr),
1105 					    if_name(ifp), error);
1106 				}
1107 			}
1108 		}
1109 	}
1110 
1111 	ia->ia6_flags = ifra->ifra_flags;
1112 	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/*safety*/
1113 	ia->ia6_flags &= ~IN6_IFF_NODAD;	/* Mobile IPv6 */
1114 
1115 	ia->ia6_lifetime = ifra->ifra_lifetime;
1116 	/* for sanity */
1117 	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1118 		ia->ia6_lifetime.ia6t_expire =
1119 			time_second + ia->ia6_lifetime.ia6t_vltime;
1120 	} else
1121 		ia->ia6_lifetime.ia6t_expire = 0;
1122 	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1123 		ia->ia6_lifetime.ia6t_preferred =
1124 			time_second + ia->ia6_lifetime.ia6t_pltime;
1125 	} else
1126 		ia->ia6_lifetime.ia6t_preferred = 0;
1127 
1128 	/*
1129 	 * make sure to initialize ND6 information.  this is to workaround
1130 	 * issues with interfaces with IPv6 addresses, which have never brought
1131 	 * up.  We are assuming that it is safe to nd6_ifattach multiple times.
1132 	 */
1133 	nd6_ifattach(ifp);
1134 
1135 	/*
1136 	 * Perform DAD, if needed.
1137 	 * XXX It may be of use, if we can administratively
1138 	 * disable DAD.
1139 	 */
1140 	if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1141 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
1142 		nd6_dad_start((struct ifaddr *)ia, NULL);
1143 	}
1144 
1145 	return(error);
1146 
1147   unlink:
1148 	/*
1149 	 * XXX: if a change of an existing address failed, keep the entry
1150 	 * anyway.
1151 	 */
1152 	if (hostIsNew)
1153 		in6_unlink_ifa(ia, ifp);
1154 	return(error);
1155 }
1156 
1157 void
1158 in6_purgeaddr(ifa)
1159 	struct ifaddr *ifa;
1160 {
1161 	struct ifnet *ifp = ifa->ifa_ifp;
1162 	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1163 
1164 	/* stop DAD processing */
1165 	nd6_dad_stop(ifa);
1166 
1167 	/*
1168 	 * delete route to the destination of the address being purged.
1169 	 * The interface must be p2p or loopback in this case.
1170 	 */
1171 	if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1172 		int e;
1173 
1174 		if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1175 		    != 0) {
1176 			log(LOG_ERR, "in6_purgeaddr: failed to remove "
1177 			    "a route to the p2p destination: %s on %s, "
1178 			    "errno=%d\n",
1179 			    ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1180 			    e);
1181 			/* proceed anyway... */
1182 		}
1183 		else
1184 			ia->ia_flags &= ~IFA_ROUTE;
1185 	}
1186 
1187 	/* Remove ownaddr's loopback rtentry, if it exists. */
1188 	in6_ifremloop(&(ia->ia_ifa));
1189 
1190 	if (ifp->if_flags & IFF_MULTICAST) {
1191 		/*
1192 		 * delete solicited multicast addr for deleting host id
1193 		 */
1194 		struct in6_multi *in6m;
1195 		struct in6_addr llsol;
1196 		bzero(&llsol, sizeof(struct in6_addr));
1197 		llsol.s6_addr16[0] = htons(0xff02);
1198 		llsol.s6_addr16[1] = htons(ifp->if_index);
1199 		llsol.s6_addr32[1] = 0;
1200 		llsol.s6_addr32[2] = htonl(1);
1201 		llsol.s6_addr32[3] =
1202 			ia->ia_addr.sin6_addr.s6_addr32[3];
1203 		llsol.s6_addr8[12] = 0xff;
1204 
1205 		IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1206 		if (in6m)
1207 			in6_delmulti(in6m);
1208 	}
1209 
1210 	in6_unlink_ifa(ia, ifp);
1211 }
1212 
1213 static void
1214 in6_unlink_ifa(ia, ifp)
1215 	struct in6_ifaddr *ia;
1216 	struct ifnet *ifp;
1217 {
1218 	int plen, iilen;
1219 	struct in6_ifaddr *oia;
1220 	int	s = splnet();
1221 
1222 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1223 
1224 	oia = ia;
1225 	if (oia == (ia = in6_ifaddr))
1226 		in6_ifaddr = ia->ia_next;
1227 	else {
1228 		while (ia->ia_next && (ia->ia_next != oia))
1229 			ia = ia->ia_next;
1230 		if (ia->ia_next)
1231 			ia->ia_next = oia->ia_next;
1232 		else {
1233 			/* search failed */
1234 			printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1235 		}
1236 	}
1237 
1238 	if (oia->ia6_ifpr) {	/* check for safety */
1239 		plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1240 		iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1241 		in6_prefix_remove_ifid(iilen, oia);
1242 	}
1243 
1244 	/*
1245 	 * When an autoconfigured address is being removed, release the
1246 	 * reference to the base prefix.  Also, since the release might
1247 	 * affect the status of other (detached) addresses, call
1248 	 * pfxlist_onlink_check().
1249 	 */
1250 	if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1251 		if (oia->ia6_ndpr == NULL) {
1252 			log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1253 			    "%p has no prefix\n", oia);
1254 		} else {
1255 			oia->ia6_ndpr->ndpr_refcnt--;
1256 			oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1257 			oia->ia6_ndpr = NULL;
1258 		}
1259 
1260 		pfxlist_onlink_check();
1261 	}
1262 
1263 	/*
1264 	 * release another refcnt for the link from in6_ifaddr.
1265 	 * Note that we should decrement the refcnt at least once for all *BSD.
1266 	 */
1267 	IFAFREE(&oia->ia_ifa);
1268 
1269 	splx(s);
1270 }
1271 
1272 void
1273 in6_purgeif(ifp)
1274 	struct ifnet *ifp;
1275 {
1276 	struct ifaddr *ifa, *nifa;
1277 
1278 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1279 	{
1280 		nifa = TAILQ_NEXT(ifa, ifa_list);
1281 		if (ifa->ifa_addr->sa_family != AF_INET6)
1282 			continue;
1283 		in6_purgeaddr(ifa);
1284 	}
1285 
1286 	in6_ifdetach(ifp);
1287 }
1288 
1289 /*
1290  * SIOC[GAD]LIFADDR.
1291  *	SIOCGLIFADDR: get first address. (?)
1292  *	SIOCGLIFADDR with IFLR_PREFIX:
1293  *		get first address that matches the specified prefix.
1294  *	SIOCALIFADDR: add the specified address.
1295  *	SIOCALIFADDR with IFLR_PREFIX:
1296  *		add the specified prefix, filling hostid part from
1297  *		the first link-local address.  prefixlen must be <= 64.
1298  *	SIOCDLIFADDR: delete the specified address.
1299  *	SIOCDLIFADDR with IFLR_PREFIX:
1300  *		delete the first address that matches the specified prefix.
1301  * return values:
1302  *	EINVAL on invalid parameters
1303  *	EADDRNOTAVAIL on prefix match failed/specified address not found
1304  *	other values may be returned from in6_ioctl()
1305  *
1306  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1307  * this is to accomodate address naming scheme other than RFC2374,
1308  * in the future.
1309  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1310  * address encoding scheme. (see figure on page 8)
1311  */
1312 static int
1313 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
1314 	struct socket *so;
1315 	u_long cmd;
1316 	caddr_t	data;
1317 	struct ifnet *ifp;
1318 	struct proc *p;
1319 {
1320 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1321 	struct ifaddr *ifa;
1322 	struct sockaddr *sa;
1323 
1324 	/* sanity checks */
1325 	if (!data || !ifp) {
1326 		panic("invalid argument to in6_lifaddr_ioctl");
1327 		/*NOTRECHED*/
1328 	}
1329 
1330 	switch (cmd) {
1331 	case SIOCGLIFADDR:
1332 		/* address must be specified on GET with IFLR_PREFIX */
1333 		if ((iflr->flags & IFLR_PREFIX) == 0)
1334 			break;
1335 		/*FALLTHROUGH*/
1336 	case SIOCALIFADDR:
1337 	case SIOCDLIFADDR:
1338 		/* address must be specified on ADD and DELETE */
1339 		sa = (struct sockaddr *)&iflr->addr;
1340 		if (sa->sa_family != AF_INET6)
1341 			return EINVAL;
1342 		if (sa->sa_len != sizeof(struct sockaddr_in6))
1343 			return EINVAL;
1344 		/* XXX need improvement */
1345 		sa = (struct sockaddr *)&iflr->dstaddr;
1346 		if (sa->sa_family && sa->sa_family != AF_INET6)
1347 			return EINVAL;
1348 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1349 			return EINVAL;
1350 		break;
1351 	default: /*shouldn't happen*/
1352 #if 0
1353 		panic("invalid cmd to in6_lifaddr_ioctl");
1354 		/*NOTREACHED*/
1355 #else
1356 		return EOPNOTSUPP;
1357 #endif
1358 	}
1359 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1360 		return EINVAL;
1361 
1362 	switch (cmd) {
1363 	case SIOCALIFADDR:
1364 	    {
1365 		struct in6_aliasreq ifra;
1366 		struct in6_addr *hostid = NULL;
1367 		int prefixlen;
1368 
1369 		if ((iflr->flags & IFLR_PREFIX) != 0) {
1370 			struct sockaddr_in6 *sin6;
1371 
1372 			/*
1373 			 * hostid is to fill in the hostid part of the
1374 			 * address.  hostid points to the first link-local
1375 			 * address attached to the interface.
1376 			 */
1377 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1378 			if (!ifa)
1379 				return EADDRNOTAVAIL;
1380 			hostid = IFA_IN6(ifa);
1381 
1382 		 	/* prefixlen must be <= 64. */
1383 			if (64 < iflr->prefixlen)
1384 				return EINVAL;
1385 			prefixlen = iflr->prefixlen;
1386 
1387 			/* hostid part must be zero. */
1388 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1389 			if (sin6->sin6_addr.s6_addr32[2] != 0
1390 			 || sin6->sin6_addr.s6_addr32[3] != 0) {
1391 				return EINVAL;
1392 			}
1393 		} else
1394 			prefixlen = iflr->prefixlen;
1395 
1396 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1397 		bzero(&ifra, sizeof(ifra));
1398 		bcopy(iflr->iflr_name, ifra.ifra_name,
1399 			sizeof(ifra.ifra_name));
1400 
1401 		bcopy(&iflr->addr, &ifra.ifra_addr,
1402 			((struct sockaddr *)&iflr->addr)->sa_len);
1403 		if (hostid) {
1404 			/* fill in hostid part */
1405 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1406 				hostid->s6_addr32[2];
1407 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1408 				hostid->s6_addr32[3];
1409 		}
1410 
1411 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
1412 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1413 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
1414 			if (hostid) {
1415 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1416 					hostid->s6_addr32[2];
1417 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1418 					hostid->s6_addr32[3];
1419 			}
1420 		}
1421 
1422 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1423 		in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1424 
1425 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1426 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1427 	    }
1428 	case SIOCGLIFADDR:
1429 	case SIOCDLIFADDR:
1430 	    {
1431 		struct in6_ifaddr *ia;
1432 		struct in6_addr mask, candidate, match;
1433 		struct sockaddr_in6 *sin6;
1434 		int cmp;
1435 
1436 		bzero(&mask, sizeof(mask));
1437 		if (iflr->flags & IFLR_PREFIX) {
1438 			/* lookup a prefix rather than address. */
1439 			in6_len2mask(&mask, iflr->prefixlen);
1440 
1441 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1442 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
1443 			match.s6_addr32[0] &= mask.s6_addr32[0];
1444 			match.s6_addr32[1] &= mask.s6_addr32[1];
1445 			match.s6_addr32[2] &= mask.s6_addr32[2];
1446 			match.s6_addr32[3] &= mask.s6_addr32[3];
1447 
1448 			/* if you set extra bits, that's wrong */
1449 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1450 				return EINVAL;
1451 
1452 			cmp = 1;
1453 		} else {
1454 			if (cmd == SIOCGLIFADDR) {
1455 				/* on getting an address, take the 1st match */
1456 				cmp = 0;	/*XXX*/
1457 			} else {
1458 				/* on deleting an address, do exact match */
1459 				in6_len2mask(&mask, 128);
1460 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
1461 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
1462 
1463 				cmp = 1;
1464 			}
1465 		}
1466 
1467 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1468 		{
1469 			if (ifa->ifa_addr->sa_family != AF_INET6)
1470 				continue;
1471 			if (!cmp)
1472 				break;
1473 
1474 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1475 #ifndef SCOPEDROUTING
1476 			/*
1477 			 * XXX: this is adhoc, but is necessary to allow
1478 			 * a user to specify fe80::/64 (not /10) for a
1479 			 * link-local address.
1480 			 */
1481 			if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1482 				candidate.s6_addr16[1] = 0;
1483 #endif
1484 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
1485 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
1486 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
1487 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
1488 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1489 				break;
1490 		}
1491 		if (!ifa)
1492 			return EADDRNOTAVAIL;
1493 		ia = ifa2ia6(ifa);
1494 
1495 		if (cmd == SIOCGLIFADDR) {
1496 #ifndef SCOPEDROUTING
1497 			struct sockaddr_in6 *s6;
1498 #endif
1499 
1500 			/* fill in the if_laddrreq structure */
1501 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1502 #ifndef SCOPEDROUTING		/* XXX see above */
1503 			s6 = (struct sockaddr_in6 *)&iflr->addr;
1504 			if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1505 				s6->sin6_addr.s6_addr16[1] = 0;
1506 				s6->sin6_scope_id =
1507 					in6_addr2scopeid(ifp, &s6->sin6_addr);
1508 			}
1509 #endif
1510 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1511 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1512 					ia->ia_dstaddr.sin6_len);
1513 #ifndef SCOPEDROUTING		/* XXX see above */
1514 				s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1515 				if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1516 					s6->sin6_addr.s6_addr16[1] = 0;
1517 					s6->sin6_scope_id =
1518 						in6_addr2scopeid(ifp,
1519 								 &s6->sin6_addr);
1520 				}
1521 #endif
1522 			} else
1523 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1524 
1525 			iflr->prefixlen =
1526 				in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1527 					     NULL);
1528 
1529 			iflr->flags = ia->ia6_flags;	/*XXX*/
1530 
1531 			return 0;
1532 		} else {
1533 			struct in6_aliasreq ifra;
1534 
1535 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1536 			bzero(&ifra, sizeof(ifra));
1537 			bcopy(iflr->iflr_name, ifra.ifra_name,
1538 				sizeof(ifra.ifra_name));
1539 
1540 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1541 				ia->ia_addr.sin6_len);
1542 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1543 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1544 					ia->ia_dstaddr.sin6_len);
1545 			} else {
1546 				bzero(&ifra.ifra_dstaddr,
1547 				    sizeof(ifra.ifra_dstaddr));
1548 			}
1549 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1550 				ia->ia_prefixmask.sin6_len);
1551 
1552 			ifra.ifra_flags = ia->ia6_flags;
1553 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1554 				ifp, p);
1555 		}
1556 	    }
1557 	}
1558 
1559 	return EOPNOTSUPP;	/*just for safety*/
1560 }
1561 
1562 /*
1563  * Initialize an interface's intetnet6 address
1564  * and routing table entry.
1565  */
1566 static int
1567 in6_ifinit(ifp, ia, sin6, newhost)
1568 	struct ifnet *ifp;
1569 	struct in6_ifaddr *ia;
1570 	struct sockaddr_in6 *sin6;
1571 	int newhost;
1572 {
1573 	int	error = 0, plen, ifacount = 0;
1574 	int	s = splimp();
1575 	struct ifaddr *ifa;
1576 
1577 	/*
1578 	 * Give the interface a chance to initialize
1579 	 * if this is its first address,
1580 	 * and to validate the address if necessary.
1581 	 */
1582 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1583 	{
1584 		if (ifa->ifa_addr == NULL)
1585 			continue;	/* just for safety */
1586 		if (ifa->ifa_addr->sa_family != AF_INET6)
1587 			continue;
1588 		ifacount++;
1589 	}
1590 
1591 	ia->ia_addr = *sin6;
1592 
1593 	if (ifacount <= 1 && ifp->if_ioctl &&
1594 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1595 		splx(s);
1596 		return(error);
1597 	}
1598 	splx(s);
1599 
1600 	ia->ia_ifa.ifa_metric = ifp->if_metric;
1601 
1602 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
1603 
1604 	/*
1605 	 * Special case:
1606 	 * If the destination address is specified for a point-to-point
1607 	 * interface, install a route to the destination as an interface
1608 	 * direct route.
1609 	 */
1610 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1611 	if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1612 		if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1613 				    RTF_UP | RTF_HOST)) != 0)
1614 			return(error);
1615 		ia->ia_flags |= IFA_ROUTE;
1616 	}
1617 	if (plen < 128) {
1618 		/*
1619 		 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1620 		 */
1621 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
1622 	}
1623 
1624 	/* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1625 	if (newhost) {
1626 		/* set the rtrequest function to create llinfo */
1627 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1628 		in6_ifaddloop(&(ia->ia_ifa));
1629 	}
1630 
1631 	return(error);
1632 }
1633 
1634 /*
1635  * Add an address to the list of IP6 multicast addresses for a
1636  * given interface.
1637  */
1638 struct	in6_multi *
1639 in6_addmulti(maddr6, ifp, errorp)
1640 	struct in6_addr *maddr6;
1641 	struct ifnet *ifp;
1642 	int *errorp;
1643 {
1644 	struct	in6_multi *in6m;
1645 	struct sockaddr_in6 sin6;
1646 	struct ifmultiaddr *ifma;
1647 	int	s = splnet();
1648 
1649 	*errorp = 0;
1650 
1651 	/*
1652 	 * Call generic routine to add membership or increment
1653 	 * refcount.  It wants addresses in the form of a sockaddr,
1654 	 * so we build one here (being careful to zero the unused bytes).
1655 	 */
1656 	bzero(&sin6, sizeof sin6);
1657 	sin6.sin6_family = AF_INET6;
1658 	sin6.sin6_len = sizeof sin6;
1659 	sin6.sin6_addr = *maddr6;
1660 	*errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1661 	if (*errorp) {
1662 		splx(s);
1663 		return 0;
1664 	}
1665 
1666 	/*
1667 	 * If ifma->ifma_protospec is null, then if_addmulti() created
1668 	 * a new record.  Otherwise, we are done.
1669 	 */
1670 	if (ifma->ifma_protospec != 0)
1671 		return ifma->ifma_protospec;
1672 
1673 	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
1674 	   at interrupt time?  If so, need to fix if_addmulti. XXX */
1675 	in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1676 	if (in6m == NULL) {
1677 		splx(s);
1678 		return (NULL);
1679 	}
1680 
1681 	bzero(in6m, sizeof *in6m);
1682 	in6m->in6m_addr = *maddr6;
1683 	in6m->in6m_ifp = ifp;
1684 	in6m->in6m_ifma = ifma;
1685 	ifma->ifma_protospec = in6m;
1686 	LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1687 
1688 	/*
1689 	 * Let MLD6 know that we have joined a new IP6 multicast
1690 	 * group.
1691 	 */
1692 	mld6_start_listening(in6m);
1693 	splx(s);
1694 	return(in6m);
1695 }
1696 
1697 /*
1698  * Delete a multicast address record.
1699  */
1700 void
1701 in6_delmulti(in6m)
1702 	struct in6_multi *in6m;
1703 {
1704 	struct ifmultiaddr *ifma = in6m->in6m_ifma;
1705 	int	s = splnet();
1706 
1707 	if (ifma->ifma_refcount == 1) {
1708 		/*
1709 		 * No remaining claims to this record; let MLD6 know
1710 		 * that we are leaving the multicast group.
1711 		 */
1712 		mld6_stop_listening(in6m);
1713 		ifma->ifma_protospec = 0;
1714 		LIST_REMOVE(in6m, in6m_entry);
1715 		free(in6m, M_IPMADDR);
1716 	}
1717 	/* XXX - should be separate API for when we have an ifma? */
1718 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1719 	splx(s);
1720 }
1721 
1722 /*
1723  * Find an IPv6 interface link-local address specific to an interface.
1724  */
1725 struct in6_ifaddr *
1726 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1727 	struct ifnet *ifp;
1728 	int ignoreflags;
1729 {
1730 	struct ifaddr *ifa;
1731 
1732 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1733 	{
1734 		if (ifa->ifa_addr == NULL)
1735 			continue;	/* just for safety */
1736 		if (ifa->ifa_addr->sa_family != AF_INET6)
1737 			continue;
1738 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1739 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1740 			     ignoreflags) != 0)
1741 				continue;
1742 			break;
1743 		}
1744 	}
1745 
1746 	return((struct in6_ifaddr *)ifa);
1747 }
1748 
1749 
1750 /*
1751  * find the internet address corresponding to a given interface and address.
1752  */
1753 struct in6_ifaddr *
1754 in6ifa_ifpwithaddr(ifp, addr)
1755 	struct ifnet *ifp;
1756 	struct in6_addr *addr;
1757 {
1758 	struct ifaddr *ifa;
1759 
1760 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1761 	{
1762 		if (ifa->ifa_addr == NULL)
1763 			continue;	/* just for safety */
1764 		if (ifa->ifa_addr->sa_family != AF_INET6)
1765 			continue;
1766 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1767 			break;
1768 	}
1769 
1770 	return((struct in6_ifaddr *)ifa);
1771 }
1772 
1773 /*
1774  * Convert IP6 address to printable (loggable) representation.
1775  */
1776 static char digits[] = "0123456789abcdef";
1777 static int ip6round = 0;
1778 char *
1779 ip6_sprintf(addr)
1780 	const struct in6_addr *addr;
1781 {
1782 	static char ip6buf[8][48];
1783 	int i;
1784 	char *cp;
1785 	u_short *a = (u_short *)addr;
1786 	u_char *d;
1787 	int dcolon = 0;
1788 
1789 	ip6round = (ip6round + 1) & 7;
1790 	cp = ip6buf[ip6round];
1791 
1792 	for (i = 0; i < 8; i++) {
1793 		if (dcolon == 1) {
1794 			if (*a == 0) {
1795 				if (i == 7)
1796 					*cp++ = ':';
1797 				a++;
1798 				continue;
1799 			} else
1800 				dcolon = 2;
1801 		}
1802 		if (*a == 0) {
1803 			if (dcolon == 0 && *(a + 1) == 0) {
1804 				if (i == 0)
1805 					*cp++ = ':';
1806 				*cp++ = ':';
1807 				dcolon = 1;
1808 			} else {
1809 				*cp++ = '0';
1810 				*cp++ = ':';
1811 			}
1812 			a++;
1813 			continue;
1814 		}
1815 		d = (u_char *)a;
1816 		*cp++ = digits[*d >> 4];
1817 		*cp++ = digits[*d++ & 0xf];
1818 		*cp++ = digits[*d >> 4];
1819 		*cp++ = digits[*d & 0xf];
1820 		*cp++ = ':';
1821 		a++;
1822 	}
1823 	*--cp = 0;
1824 	return(ip6buf[ip6round]);
1825 }
1826 
1827 int
1828 in6_localaddr(in6)
1829 	struct in6_addr *in6;
1830 {
1831 	struct in6_ifaddr *ia;
1832 
1833 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1834 		return 1;
1835 
1836 	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1837 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1838 					      &ia->ia_prefixmask.sin6_addr))
1839 			return 1;
1840 
1841 	return (0);
1842 }
1843 
1844 int
1845 in6_is_addr_deprecated(sa6)
1846 	struct sockaddr_in6 *sa6;
1847 {
1848 	struct in6_ifaddr *ia;
1849 
1850 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1851 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1852 				       &sa6->sin6_addr) &&
1853 #ifdef SCOPEDROUTING
1854 		    ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1855 #endif
1856 		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1857 			return(1); /* true */
1858 
1859 		/* XXX: do we still have to go thru the rest of the list? */
1860 	}
1861 
1862 	return(0);		/* false */
1863 }
1864 
1865 /*
1866  * return length of part which dst and src are equal
1867  * hard coding...
1868  */
1869 int
1870 in6_matchlen(src, dst)
1871 struct in6_addr *src, *dst;
1872 {
1873 	int match = 0;
1874 	u_char *s = (u_char *)src, *d = (u_char *)dst;
1875 	u_char *lim = s + 16, r;
1876 
1877 	while (s < lim)
1878 		if ((r = (*d++ ^ *s++)) != 0) {
1879 			while (r < 128) {
1880 				match++;
1881 				r <<= 1;
1882 			}
1883 			break;
1884 		} else
1885 			match += 8;
1886 	return match;
1887 }
1888 
1889 /* XXX: to be scope conscious */
1890 int
1891 in6_are_prefix_equal(p1, p2, len)
1892 	struct in6_addr *p1, *p2;
1893 	int len;
1894 {
1895 	int bytelen, bitlen;
1896 
1897 	/* sanity check */
1898 	if (0 > len || len > 128) {
1899 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1900 		    len);
1901 		return(0);
1902 	}
1903 
1904 	bytelen = len / 8;
1905 	bitlen = len % 8;
1906 
1907 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1908 		return(0);
1909 	if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1910 	    p2->s6_addr[bytelen] >> (8 - bitlen))
1911 		return(0);
1912 
1913 	return(1);
1914 }
1915 
1916 void
1917 in6_prefixlen2mask(maskp, len)
1918 	struct in6_addr *maskp;
1919 	int len;
1920 {
1921 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1922 	int bytelen, bitlen, i;
1923 
1924 	/* sanity check */
1925 	if (0 > len || len > 128) {
1926 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1927 		    len);
1928 		return;
1929 	}
1930 
1931 	bzero(maskp, sizeof(*maskp));
1932 	bytelen = len / 8;
1933 	bitlen = len % 8;
1934 	for (i = 0; i < bytelen; i++)
1935 		maskp->s6_addr[i] = 0xff;
1936 	if (bitlen)
1937 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1938 }
1939 
1940 /*
1941  * return the best address out of the same scope
1942  */
1943 struct in6_ifaddr *
1944 in6_ifawithscope(oifp, dst)
1945 	struct ifnet *oifp;
1946 	struct in6_addr *dst;
1947 {
1948 	int dst_scope =	in6_addrscope(dst), src_scope, best_scope = 0;
1949 	int blen = -1;
1950 	struct ifaddr *ifa;
1951 	struct ifnet *ifp;
1952 	struct in6_ifaddr *ifa_best = NULL;
1953 
1954 	if (oifp == NULL) {
1955 #if 0
1956 		printf("in6_ifawithscope: output interface is not specified\n");
1957 #endif
1958 		return(NULL);
1959 	}
1960 
1961 	/*
1962 	 * We search for all addresses on all interfaces from the beginning.
1963 	 * Comparing an interface with the outgoing interface will be done
1964 	 * only at the final stage of tiebreaking.
1965 	 */
1966 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1967 	{
1968 		/*
1969 		 * We can never take an address that breaks the scope zone
1970 		 * of the destination.
1971 		 */
1972 		if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1973 			continue;
1974 
1975 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1976 		{
1977 			int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1978 
1979 			if (ifa->ifa_addr->sa_family != AF_INET6)
1980 				continue;
1981 
1982 			src_scope = in6_addrscope(IFA_IN6(ifa));
1983 
1984 			/*
1985 			 * Don't use an address before completing DAD
1986 			 * nor a duplicated address.
1987 			 */
1988 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1989 			    IN6_IFF_NOTREADY)
1990 				continue;
1991 
1992 			/* XXX: is there any case to allow anycasts? */
1993 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1994 			    IN6_IFF_ANYCAST)
1995 				continue;
1996 
1997 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1998 			    IN6_IFF_DETACHED)
1999 				continue;
2000 
2001 			/*
2002 			 * If this is the first address we find,
2003 			 * keep it anyway.
2004 			 */
2005 			if (ifa_best == NULL)
2006 				goto replace;
2007 
2008 			/*
2009 			 * ifa_best is never NULL beyond this line except
2010 			 * within the block labeled "replace".
2011 			 */
2012 
2013 			/*
2014 			 * If ifa_best has a smaller scope than dst and
2015 			 * the current address has a larger one than
2016 			 * (or equal to) dst, always replace ifa_best.
2017 			 * Also, if the current address has a smaller scope
2018 			 * than dst, ignore it unless ifa_best also has a
2019 			 * smaller scope.
2020 			 * Consequently, after the two if-clause below,
2021 			 * the followings must be satisfied:
2022 			 * (scope(src) < scope(dst) &&
2023 			 *  scope(best) < scope(dst))
2024 			 *  OR
2025 			 * (scope(best) >= scope(dst) &&
2026 			 *  scope(src) >= scope(dst))
2027 			 */
2028 			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2029 			    IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2030 				goto replace; /* (A) */
2031 			if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2032 			    IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2033 				continue; /* (B) */
2034 
2035 			/*
2036 			 * A deprecated address SHOULD NOT be used in new
2037 			 * communications if an alternate (non-deprecated)
2038 			 * address is available and has sufficient scope.
2039 			 * RFC 2462, Section 5.5.4.
2040 			 */
2041 			if (((struct in6_ifaddr *)ifa)->ia6_flags &
2042 			    IN6_IFF_DEPRECATED) {
2043 				/*
2044 				 * Ignore any deprecated addresses if
2045 				 * specified by configuration.
2046 				 */
2047 				if (!ip6_use_deprecated)
2048 					continue;
2049 
2050 				/*
2051 				 * If we have already found a non-deprecated
2052 				 * candidate, just ignore deprecated addresses.
2053 				 */
2054 				if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2055 				    == 0)
2056 					continue;
2057 			}
2058 
2059 			/*
2060 			 * A non-deprecated address is always preferred
2061 			 * to a deprecated one regardless of scopes and
2062 			 * address matching (Note invariants ensured by the
2063 			 * conditions (A) and (B) above.)
2064 			 */
2065 			if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2066 			    (((struct in6_ifaddr *)ifa)->ia6_flags &
2067 			     IN6_IFF_DEPRECATED) == 0)
2068 				goto replace;
2069 
2070 			/*
2071 			 * When we use temporary addresses described in
2072 			 * RFC 3041, we prefer temporary addresses to
2073 			 * public autoconf addresses.  Again, note the
2074 			 * invariants from (A) and (B).  Also note that we
2075 			 * don't have any preference between static addresses
2076 			 * and autoconf addresses (despite of whether or not
2077 			 * the latter is temporary or public.)
2078 			 */
2079 			if (ip6_use_tempaddr) {
2080 				struct in6_ifaddr *ifat;
2081 
2082 				ifat = (struct in6_ifaddr *)ifa;
2083 				if ((ifa_best->ia6_flags &
2084 				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2085 				     == IN6_IFF_AUTOCONF &&
2086 				    (ifat->ia6_flags &
2087 				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2088 				     == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2089 					goto replace;
2090 				}
2091 				if ((ifa_best->ia6_flags &
2092 				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2093 				    == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2094 				    (ifat->ia6_flags &
2095 				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2096 				     == IN6_IFF_AUTOCONF) {
2097 					continue;
2098 				}
2099 			}
2100 
2101 			/*
2102 			 * At this point, we have two cases:
2103 			 * 1. we are looking at a non-deprecated address,
2104 			 *    and ifa_best is also non-deprecated.
2105 			 * 2. we are looking at a deprecated address,
2106 			 *    and ifa_best is also deprecated.
2107 			 * Also, we do not have to consider a case where
2108 			 * the scope of if_best is larger(smaller) than dst and
2109 			 * the scope of the current address is smaller(larger)
2110 			 * than dst. Such a case has already been covered.
2111 			 * Tiebreaking is done according to the following
2112 			 * items:
2113 			 * - the scope comparison between the address and
2114 			 *   dst (dscopecmp)
2115 			 * - the scope comparison between the address and
2116 			 *   ifa_best (bscopecmp)
2117 			 * - if the address match dst longer than ifa_best
2118 			 *   (matchcmp)
2119 			 * - if the address is on the outgoing I/F (outI/F)
2120 			 *
2121 			 * Roughly speaking, the selection policy is
2122 			 * - the most important item is scope. The same scope
2123 			 *   is best. Then search for a larger scope.
2124 			 *   Smaller scopes are the last resort.
2125 			 * - A deprecated address is chosen only when we have
2126 			 *   no address that has an enough scope, but is
2127 			 *   prefered to any addresses of smaller scopes
2128 			 *   (this must be already done above.)
2129 			 * - addresses on the outgoing I/F are preferred to
2130 			 *   ones on other interfaces if none of above
2131 			 *   tiebreaks.  In the table below, the column "bI"
2132 			 *   means if the best_ifa is on the outgoing
2133 			 *   interface, and the column "sI" means if the ifa
2134 			 *   is on the outgoing interface.
2135 			 * - If there is no other reasons to choose one,
2136 			 *   longest address match against dst is considered.
2137 			 *
2138 			 * The precise decision table is as follows:
2139 			 * dscopecmp bscopecmp    match  bI oI | replace?
2140 			 *       N/A     equal      N/A   Y  N |   No (1)
2141 			 *       N/A     equal      N/A   N  Y |  Yes (2)
2142 			 *       N/A     equal   larger    N/A |  Yes (3)
2143 			 *       N/A     equal  !larger    N/A |   No (4)
2144 			 *    larger    larger      N/A    N/A |   No (5)
2145 			 *    larger   smaller      N/A    N/A |  Yes (6)
2146 			 *   smaller    larger      N/A    N/A |  Yes (7)
2147 			 *   smaller   smaller      N/A    N/A |   No (8)
2148 			 *     equal   smaller      N/A    N/A |  Yes (9)
2149 			 *     equal    larger       (already done at A above)
2150 			 */
2151 			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2152 			bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2153 
2154 			if (bscopecmp == 0) {
2155 				struct ifnet *bifp = ifa_best->ia_ifp;
2156 
2157 				if (bifp == oifp && ifp != oifp) /* (1) */
2158 					continue;
2159 				if (bifp != oifp && ifp == oifp) /* (2) */
2160 					goto replace;
2161 
2162 				/*
2163 				 * Both bifp and ifp are on the outgoing
2164 				 * interface, or both two are on a different
2165 				 * interface from the outgoing I/F.
2166 				 * now we need address matching against dst
2167 				 * for tiebreaking.
2168 				 */
2169 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2170 				matchcmp = tlen - blen;
2171 				if (matchcmp > 0) /* (3) */
2172 					goto replace;
2173 				continue; /* (4) */
2174 			}
2175 			if (dscopecmp > 0) {
2176 				if (bscopecmp > 0) /* (5) */
2177 					continue;
2178 				goto replace; /* (6) */
2179 			}
2180 			if (dscopecmp < 0) {
2181 				if (bscopecmp > 0) /* (7) */
2182 					goto replace;
2183 				continue; /* (8) */
2184 			}
2185 
2186 			/* now dscopecmp must be 0 */
2187 			if (bscopecmp < 0)
2188 				goto replace; /* (9) */
2189 
2190 		  replace:
2191 			ifa_best = (struct in6_ifaddr *)ifa;
2192 			blen = tlen >= 0 ? tlen :
2193 				in6_matchlen(IFA_IN6(ifa), dst);
2194 			best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2195 		}
2196 	}
2197 
2198 	/* count statistics for future improvements */
2199 	if (ifa_best == NULL)
2200 		ip6stat.ip6s_sources_none++;
2201 	else {
2202 		if (oifp == ifa_best->ia_ifp)
2203 			ip6stat.ip6s_sources_sameif[best_scope]++;
2204 		else
2205 			ip6stat.ip6s_sources_otherif[best_scope]++;
2206 
2207 		if (best_scope == dst_scope)
2208 			ip6stat.ip6s_sources_samescope[best_scope]++;
2209 		else
2210 			ip6stat.ip6s_sources_otherscope[best_scope]++;
2211 
2212 		if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2213 			ip6stat.ip6s_sources_deprecated[best_scope]++;
2214 	}
2215 
2216 	return(ifa_best);
2217 }
2218 
2219 /*
2220  * return the best address out of the same scope. if no address was
2221  * found, return the first valid address from designated IF.
2222  */
2223 struct in6_ifaddr *
2224 in6_ifawithifp(ifp, dst)
2225 	struct ifnet *ifp;
2226 	struct in6_addr *dst;
2227 {
2228 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
2229 	struct ifaddr *ifa;
2230 	struct in6_ifaddr *besta = 0;
2231 	struct in6_ifaddr *dep[2];	/*last-resort: deprecated*/
2232 
2233 	dep[0] = dep[1] = NULL;
2234 
2235 	/*
2236 	 * We first look for addresses in the same scope.
2237 	 * If there is one, return it.
2238 	 * If two or more, return one which matches the dst longest.
2239 	 * If none, return one of global addresses assigned other ifs.
2240 	 */
2241 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2242 	{
2243 		if (ifa->ifa_addr->sa_family != AF_INET6)
2244 			continue;
2245 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2246 			continue; /* XXX: is there any case to allow anycast? */
2247 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2248 			continue; /* don't use this interface */
2249 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2250 			continue;
2251 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2252 			if (ip6_use_deprecated)
2253 				dep[0] = (struct in6_ifaddr *)ifa;
2254 			continue;
2255 		}
2256 
2257 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2258 			/*
2259 			 * call in6_matchlen() as few as possible
2260 			 */
2261 			if (besta) {
2262 				if (blen == -1)
2263 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2264 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2265 				if (tlen > blen) {
2266 					blen = tlen;
2267 					besta = (struct in6_ifaddr *)ifa;
2268 				}
2269 			} else
2270 				besta = (struct in6_ifaddr *)ifa;
2271 		}
2272 	}
2273 	if (besta)
2274 		return(besta);
2275 
2276 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2277 	{
2278 		if (ifa->ifa_addr->sa_family != AF_INET6)
2279 			continue;
2280 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2281 			continue; /* XXX: is there any case to allow anycast? */
2282 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2283 			continue; /* don't use this interface */
2284 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2285 			continue;
2286 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2287 			if (ip6_use_deprecated)
2288 				dep[1] = (struct in6_ifaddr *)ifa;
2289 			continue;
2290 		}
2291 
2292 		return (struct in6_ifaddr *)ifa;
2293 	}
2294 
2295 	/* use the last-resort values, that are, deprecated addresses */
2296 	if (dep[0])
2297 		return dep[0];
2298 	if (dep[1])
2299 		return dep[1];
2300 
2301 	return NULL;
2302 }
2303 
2304 /*
2305  * perform DAD when interface becomes IFF_UP.
2306  */
2307 void
2308 in6_if_up(ifp)
2309 	struct ifnet *ifp;
2310 {
2311 	struct ifaddr *ifa;
2312 	struct in6_ifaddr *ia;
2313 	int dad_delay;		/* delay ticks before DAD output */
2314 
2315 	/*
2316 	 * special cases, like 6to4, are handled in in6_ifattach
2317 	 */
2318 	in6_ifattach(ifp, NULL);
2319 
2320 	dad_delay = 0;
2321 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2322 	{
2323 		if (ifa->ifa_addr->sa_family != AF_INET6)
2324 			continue;
2325 		ia = (struct in6_ifaddr *)ifa;
2326 		if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2327 			nd6_dad_start(ifa, &dad_delay);
2328 	}
2329 }
2330 
2331 int
2332 in6if_do_dad(ifp)
2333 	struct ifnet *ifp;
2334 {
2335 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2336 		return(0);
2337 
2338 	switch (ifp->if_type) {
2339 #ifdef IFT_DUMMY
2340 	case IFT_DUMMY:
2341 #endif
2342 	case IFT_FAITH:
2343 		/*
2344 		 * These interfaces do not have the IFF_LOOPBACK flag,
2345 		 * but loop packets back.  We do not have to do DAD on such
2346 		 * interfaces.  We should even omit it, because loop-backed
2347 		 * NS would confuse the DAD procedure.
2348 		 */
2349 		return(0);
2350 	default:
2351 		/*
2352 		 * Our DAD routine requires the interface up and running.
2353 		 * However, some interfaces can be up before the RUNNING
2354 		 * status.  Additionaly, users may try to assign addresses
2355 		 * before the interface becomes up (or running).
2356 		 * We simply skip DAD in such a case as a work around.
2357 		 * XXX: we should rather mark "tentative" on such addresses,
2358 		 * and do DAD after the interface becomes ready.
2359 		 */
2360 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2361 		    (IFF_UP|IFF_RUNNING))
2362 			return(0);
2363 
2364 		return(1);
2365 	}
2366 }
2367 
2368 /*
2369  * Calculate max IPv6 MTU through all the interfaces and store it
2370  * to in6_maxmtu.
2371  */
2372 void
2373 in6_setmaxmtu()
2374 {
2375 	unsigned long maxmtu = 0;
2376 	struct ifnet *ifp;
2377 
2378 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2379 	{
2380 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2381 		    nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2382 			maxmtu =  nd_ifinfo[ifp->if_index].linkmtu;
2383 	}
2384 	if (maxmtu)	/* update only when maxmtu is positive */
2385 		in6_maxmtu = maxmtu;
2386 }
2387 
2388 /*
2389  * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2390  * v4 mapped addr or v4 compat addr
2391  */
2392 void
2393 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2394 {
2395 	bzero(sin, sizeof(*sin));
2396 	sin->sin_len = sizeof(struct sockaddr_in);
2397 	sin->sin_family = AF_INET;
2398 	sin->sin_port = sin6->sin6_port;
2399 	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2400 }
2401 
2402 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2403 void
2404 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2405 {
2406 	bzero(sin6, sizeof(*sin6));
2407 	sin6->sin6_len = sizeof(struct sockaddr_in6);
2408 	sin6->sin6_family = AF_INET6;
2409 	sin6->sin6_port = sin->sin_port;
2410 	sin6->sin6_addr.s6_addr32[0] = 0;
2411 	sin6->sin6_addr.s6_addr32[1] = 0;
2412 	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2413 	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2414 }
2415 
2416 /* Convert sockaddr_in6 into sockaddr_in. */
2417 void
2418 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2419 {
2420 	struct sockaddr_in *sin_p;
2421 	struct sockaddr_in6 sin6;
2422 
2423 	/*
2424 	 * Save original sockaddr_in6 addr and convert it
2425 	 * to sockaddr_in.
2426 	 */
2427 	sin6 = *(struct sockaddr_in6 *)nam;
2428 	sin_p = (struct sockaddr_in *)nam;
2429 	in6_sin6_2_sin(sin_p, &sin6);
2430 }
2431 
2432 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2433 void
2434 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2435 {
2436 	struct sockaddr_in *sin_p;
2437 	struct sockaddr_in6 *sin6_p;
2438 
2439 	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2440 	       M_WAITOK);
2441 	sin_p = (struct sockaddr_in *)*nam;
2442 	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2443 	FREE(*nam, M_SONAME);
2444 	*nam = (struct sockaddr *)sin6_p;
2445 }
2446