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