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