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