xref: /freebsd/sys/netinet6/in6.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
1 /*	$FreeBSD$	*/
2 /*	$KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 4. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)in.c	8.2 (Berkeley) 11/15/93
62  */
63 
64 #include "opt_inet.h"
65 #include "opt_inet6.h"
66 
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/time.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 
79 #include <net/if.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
82 #include <net/if_dl.h>
83 
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/if_ether.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
90 
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/mld6_var.h>
95 #include <netinet6/ip6_mroute.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/scope6_var.h>
98 #include <netinet6/in6_pcb.h>
99 
100 #include <net/net_osdep.h>
101 
102 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
103 
104 /*
105  * Definitions of some costant IP6 addresses.
106  */
107 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
108 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
109 const struct in6_addr in6addr_nodelocal_allnodes =
110 	IN6ADDR_NODELOCAL_ALLNODES_INIT;
111 const struct in6_addr in6addr_linklocal_allnodes =
112 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
113 const struct in6_addr in6addr_linklocal_allrouters =
114 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
115 
116 const struct in6_addr in6mask0 = IN6MASK0;
117 const struct in6_addr in6mask32 = IN6MASK32;
118 const struct in6_addr in6mask64 = IN6MASK64;
119 const struct in6_addr in6mask96 = IN6MASK96;
120 const struct in6_addr in6mask128 = IN6MASK128;
121 
122 const struct sockaddr_in6 sa6_any =
123 	{ sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
124 
125 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
126 	struct ifnet *, struct thread *));
127 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
128 	struct sockaddr_in6 *, int));
129 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
130 
131 struct in6_multihead in6_multihead;	/* XXX BSS initialization */
132 int	(*faithprefix_p)(struct in6_addr *);
133 
134 /*
135  * Subroutine for in6_ifaddloop() and in6_ifremloop().
136  * This routine does actual work.
137  */
138 static void
139 in6_ifloop_request(int cmd, struct ifaddr *ifa)
140 {
141 	struct sockaddr_in6 all1_sa;
142 	struct rtentry *nrt = NULL;
143 	int e;
144 
145 	bzero(&all1_sa, sizeof(all1_sa));
146 	all1_sa.sin6_family = AF_INET6;
147 	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
148 	all1_sa.sin6_addr = in6mask128;
149 
150 	/*
151 	 * We specify the address itself as the gateway, and set the
152 	 * RTF_LLINFO flag, so that the corresponding host route would have
153 	 * the flag, and thus applications that assume traditional behavior
154 	 * would be happy.  Note that we assume the caller of the function
155 	 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
156 	 * which changes the outgoing interface to the loopback interface.
157 	 */
158 	e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
159 	    (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
160 	if (e != 0) {
161 		/* XXX need more descriptive message */
162 		log(LOG_ERR, "in6_ifloop_request: "
163 		    "%s operation failed for %s (errno=%d)\n",
164 		    cmd == RTM_ADD ? "ADD" : "DELETE",
165 		    ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
166 		    e);
167 	}
168 
169 	if (nrt) {
170 		RT_LOCK(nrt);
171 		/*
172 		 * Make sure rt_ifa be equal to IFA, the second argument of
173 		 * the function.  We need this because when we refer to
174 		 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
175 		 * points to the address instead of the loopback address.
176 		 */
177 		if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
178 			IFAFREE(nrt->rt_ifa);
179 			IFAREF(ifa);
180 			nrt->rt_ifa = ifa;
181 		}
182 
183 		/*
184 		 * Report the addition/removal of the address to the routing
185 		 * socket.
186 		 *
187 		 * XXX: since we called rtinit for a p2p interface with a
188 		 *      destination, we end up reporting twice in such a case.
189 		 *      Should we rather omit the second report?
190 		 */
191 		rt_newaddrmsg(cmd, ifa, e, nrt);
192 		if (cmd == RTM_DELETE) {
193 			rtfree(nrt);
194 		} else {
195 			/* the cmd must be RTM_ADD here */
196 			RT_REMREF(nrt);
197 			RT_UNLOCK(nrt);
198 		}
199 	}
200 }
201 
202 /*
203  * Add ownaddr as loopback rtentry.  We previously add the route only if
204  * necessary (ex. on a p2p link).  However, since we now manage addresses
205  * separately from prefixes, we should always add the route.  We can't
206  * rely on the cloning mechanism from the corresponding interface route
207  * any more.
208  */
209 static void
210 in6_ifaddloop(struct ifaddr *ifa)
211 {
212 	struct rtentry *rt;
213 	int need_loop;
214 
215 	/* If there is no loopback entry, allocate one. */
216 	rt = rtalloc1(ifa->ifa_addr, 0, 0);
217 	need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
218 	    (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
219 	if (rt)
220 		rtfree(rt);
221 	if (need_loop)
222 		in6_ifloop_request(RTM_ADD, ifa);
223 }
224 
225 /*
226  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
227  * if it exists.
228  */
229 static void
230 in6_ifremloop(struct ifaddr *ifa)
231 {
232 	struct in6_ifaddr *ia;
233 	struct rtentry *rt;
234 	int ia_count = 0;
235 
236 	/*
237 	 * Some of BSD variants do not remove cloned routes
238 	 * from an interface direct route, when removing the direct route
239 	 * (see comments in net/net_osdep.h).  Even for variants that do remove
240 	 * cloned routes, they could fail to remove the cloned routes when
241 	 * we handle multple addresses that share a common prefix.
242 	 * So, we should remove the route corresponding to the deleted address
243 	 * regardless of the result of in6_is_ifloop_auto().
244 	 */
245 
246 	/*
247 	 * Delete the entry only if exact one ifa exists.  More than one ifa
248 	 * can exist if we assign a same single address to multiple
249 	 * (probably p2p) interfaces.
250 	 * XXX: we should avoid such a configuration in IPv6...
251 	 */
252 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
253 		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
254 			ia_count++;
255 			if (ia_count > 1)
256 				break;
257 		}
258 	}
259 
260 	if (ia_count == 1) {
261 		/*
262 		 * Before deleting, check if a corresponding loopbacked host
263 		 * route surely exists.  With this check, we can avoid to
264 		 * delete an interface direct route whose destination is same
265 		 * as the address being removed.  This can happen when removing
266 		 * a subnet-router anycast address on an interface attahced
267 		 * to a shared medium.
268 		 */
269 		rt = rtalloc1(ifa->ifa_addr, 0, 0);
270 		if (rt != NULL) {
271 			if ((rt->rt_flags & RTF_HOST) != 0 &&
272 			    (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
273 				rtfree(rt);
274 				in6_ifloop_request(RTM_DELETE, ifa);
275 			} else
276 				RT_UNLOCK(rt);
277 		}
278 	}
279 }
280 
281 int
282 in6_mask2len(mask, lim0)
283 	struct in6_addr *mask;
284 	u_char *lim0;
285 {
286 	int x = 0, y;
287 	u_char *lim = lim0, *p;
288 
289 	/* ignore the scope_id part */
290 	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
291 		lim = (u_char *)mask + sizeof(*mask);
292 	for (p = (u_char *)mask; p < lim; x++, p++) {
293 		if (*p != 0xff)
294 			break;
295 	}
296 	y = 0;
297 	if (p < lim) {
298 		for (y = 0; y < 8; y++) {
299 			if ((*p & (0x80 >> y)) == 0)
300 				break;
301 		}
302 	}
303 
304 	/*
305 	 * when the limit pointer is given, do a stricter check on the
306 	 * remaining bits.
307 	 */
308 	if (p < lim) {
309 		if (y != 0 && (*p & (0x00ff >> y)) != 0)
310 			return (-1);
311 		for (p = p + 1; p < lim; p++)
312 			if (*p != 0)
313 				return (-1);
314 	}
315 
316 	return x * 8 + y;
317 }
318 
319 #define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
320 #define ia62ifa(ia6)	(&((ia6)->ia_ifa))
321 
322 int
323 in6_control(so, cmd, data, ifp, td)
324 	struct	socket *so;
325 	u_long cmd;
326 	caddr_t	data;
327 	struct ifnet *ifp;
328 	struct thread *td;
329 {
330 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
331 	struct	in6_ifaddr *ia = NULL;
332 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
333 	int privileged;
334 
335 	privileged = 0;
336 	if (td == NULL || !suser(td))
337 		privileged++;
338 
339 	switch (cmd) {
340 	case SIOCGETSGCNT_IN6:
341 	case SIOCGETMIFCNT_IN6:
342 		return (mrt6_ioctl(cmd, data));
343 	}
344 
345 	switch(cmd) {
346 	case SIOCAADDRCTL_POLICY:
347 	case SIOCDADDRCTL_POLICY:
348 		if (!privileged)
349 			return (EPERM);
350 		return (in6_src_ioctl(cmd, data));
351 	}
352 
353 	if (ifp == NULL)
354 		return (EOPNOTSUPP);
355 
356 	switch (cmd) {
357 	case SIOCSNDFLUSH_IN6:
358 	case SIOCSPFXFLUSH_IN6:
359 	case SIOCSRTRFLUSH_IN6:
360 	case SIOCSDEFIFACE_IN6:
361 	case SIOCSIFINFO_FLAGS:
362 		if (!privileged)
363 			return (EPERM);
364 		/* FALLTHROUGH */
365 	case OSIOCGIFINFO_IN6:
366 	case SIOCGIFINFO_IN6:
367 	case SIOCGDRLST_IN6:
368 	case SIOCGPRLST_IN6:
369 	case SIOCGNBRINFO_IN6:
370 	case SIOCGDEFIFACE_IN6:
371 		return (nd6_ioctl(cmd, data, ifp));
372 	}
373 
374 	switch (cmd) {
375 	case SIOCSIFPREFIX_IN6:
376 	case SIOCDIFPREFIX_IN6:
377 	case SIOCAIFPREFIX_IN6:
378 	case SIOCCIFPREFIX_IN6:
379 	case SIOCSGIFPREFIX_IN6:
380 	case SIOCGIFPREFIX_IN6:
381 		log(LOG_NOTICE,
382 		    "prefix ioctls are now invalidated. "
383 		    "please use ifconfig.\n");
384 		return (EOPNOTSUPP);
385 	}
386 
387 	switch (cmd) {
388 	case SIOCSSCOPE6:
389 		if (!privileged)
390 			return (EPERM);
391 		return (scope6_set(ifp,
392 		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
393 	case SIOCGSCOPE6:
394 		return (scope6_get(ifp,
395 		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
396 	case SIOCGSCOPE6DEF:
397 		return (scope6_get_default((struct scope6_id *)
398 		    ifr->ifr_ifru.ifru_scope_id));
399 	}
400 
401 	switch (cmd) {
402 	case SIOCALIFADDR:
403 	case SIOCDLIFADDR:
404 		if (!privileged)
405 			return (EPERM);
406 		/* FALLTHROUGH */
407 	case SIOCGLIFADDR:
408 		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
409 	}
410 
411 	/*
412 	 * Find address for this interface, if it exists.
413 	 */
414 	if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
415 		struct sockaddr_in6 *sa6 =
416 			(struct sockaddr_in6 *)&ifra->ifra_addr;
417 
418 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
419 			if (sa6->sin6_addr.s6_addr16[1] == 0) {
420 				/* link ID is not embedded by the user */
421 				sa6->sin6_addr.s6_addr16[1] =
422 				    htons(ifp->if_index);
423 			} else if (sa6->sin6_addr.s6_addr16[1] !=
424 			    htons(ifp->if_index)) {
425 				return (EINVAL);	/* link ID contradicts */
426 			}
427 			if (sa6->sin6_scope_id) {
428 				if (sa6->sin6_scope_id !=
429 				    (u_int32_t)ifp->if_index)
430 					return (EINVAL);
431 				sa6->sin6_scope_id = 0; /* XXX: good way? */
432 			}
433 		}
434 		ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
435 	}
436 
437 	switch (cmd) {
438 	case SIOCSIFADDR_IN6:
439 	case SIOCSIFDSTADDR_IN6:
440 	case SIOCSIFNETMASK_IN6:
441 		/*
442 		 * Since IPv6 allows a node to assign multiple addresses
443 		 * on a single interface, SIOCSIFxxx ioctls are not suitable
444 		 * and should be unused.
445 		 */
446 		/* we decided to obsolete this command (20000704) */
447 		return (EINVAL);
448 
449 	case SIOCDIFADDR_IN6:
450 		/*
451 		 * for IPv4, we look for existing in_ifaddr here to allow
452 		 * "ifconfig if0 delete" to remove first IPv4 address on the
453 		 * interface.  For IPv6, as the spec allow multiple interface
454 		 * address from the day one, we consider "remove the first one"
455 		 * semantics to be not preferable.
456 		 */
457 		if (ia == NULL)
458 			return (EADDRNOTAVAIL);
459 		/* FALLTHROUGH */
460 	case SIOCAIFADDR_IN6:
461 		/*
462 		 * We always require users to specify a valid IPv6 address for
463 		 * the corresponding operation.
464 		 */
465 		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
466 		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
467 			return (EAFNOSUPPORT);
468 		if (!privileged)
469 			return (EPERM);
470 
471 		break;
472 
473 	case SIOCGIFADDR_IN6:
474 		/* This interface is basically deprecated. use SIOCGIFCONF. */
475 		/* FALLTHROUGH */
476 	case SIOCGIFAFLAG_IN6:
477 	case SIOCGIFNETMASK_IN6:
478 	case SIOCGIFDSTADDR_IN6:
479 	case SIOCGIFALIFETIME_IN6:
480 		/* must think again about its semantics */
481 		if (ia == NULL)
482 			return (EADDRNOTAVAIL);
483 		break;
484 	case SIOCSIFALIFETIME_IN6:
485 	    {
486 		struct in6_addrlifetime *lt;
487 
488 		if (!privileged)
489 			return (EPERM);
490 		if (ia == NULL)
491 			return (EADDRNOTAVAIL);
492 		/* sanity for overflow - beware unsigned */
493 		lt = &ifr->ifr_ifru.ifru_lifetime;
494 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
495 		    lt->ia6t_vltime + time_second < time_second) {
496 			return EINVAL;
497 		}
498 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
499 		    lt->ia6t_pltime + time_second < time_second) {
500 			return EINVAL;
501 		}
502 		break;
503 	    }
504 	}
505 
506 	switch (cmd) {
507 
508 	case SIOCGIFADDR_IN6:
509 		ifr->ifr_addr = ia->ia_addr;
510 		break;
511 
512 	case SIOCGIFDSTADDR_IN6:
513 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
514 			return (EINVAL);
515 		/*
516 		 * XXX: should we check if ifa_dstaddr is NULL and return
517 		 * an error?
518 		 */
519 		ifr->ifr_dstaddr = ia->ia_dstaddr;
520 		break;
521 
522 	case SIOCGIFNETMASK_IN6:
523 		ifr->ifr_addr = ia->ia_prefixmask;
524 		break;
525 
526 	case SIOCGIFAFLAG_IN6:
527 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
528 		break;
529 
530 	case SIOCGIFSTAT_IN6:
531 		if (ifp == NULL)
532 			return EINVAL;
533 		bzero(&ifr->ifr_ifru.ifru_stat,
534 		    sizeof(ifr->ifr_ifru.ifru_stat));
535 		ifr->ifr_ifru.ifru_stat =
536 		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
537 		break;
538 
539 	case SIOCGIFSTAT_ICMP6:
540 		if (ifp == NULL)
541 			return EINVAL;
542 		bzero(&ifr->ifr_ifru.ifru_stat,
543 		    sizeof(ifr->ifr_ifru.ifru_icmp6stat));
544 		ifr->ifr_ifru.ifru_icmp6stat =
545 		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
546 		break;
547 
548 	case SIOCGIFALIFETIME_IN6:
549 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
550 		break;
551 
552 	case SIOCSIFALIFETIME_IN6:
553 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
554 		/* for sanity */
555 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
556 			ia->ia6_lifetime.ia6t_expire =
557 				time_second + ia->ia6_lifetime.ia6t_vltime;
558 		} else
559 			ia->ia6_lifetime.ia6t_expire = 0;
560 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
561 			ia->ia6_lifetime.ia6t_preferred =
562 				time_second + ia->ia6_lifetime.ia6t_pltime;
563 		} else
564 			ia->ia6_lifetime.ia6t_preferred = 0;
565 		break;
566 
567 	case SIOCAIFADDR_IN6:
568 	{
569 		int i, error = 0;
570 		struct nd_prefix pr0, *pr;
571 
572 		/*
573 		 * first, make or update the interface address structure,
574 		 * and link it to the list.
575 		 */
576 		if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
577 			return (error);
578 
579 		/*
580 		 * then, make the prefix on-link on the interface.
581 		 * XXX: we'd rather create the prefix before the address, but
582 		 * we need at least one address to install the corresponding
583 		 * interface route, so we configure the address first.
584 		 */
585 
586 		/*
587 		 * convert mask to prefix length (prefixmask has already
588 		 * been validated in in6_update_ifa().
589 		 */
590 		bzero(&pr0, sizeof(pr0));
591 		pr0.ndpr_ifp = ifp;
592 		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
593 		    NULL);
594 		if (pr0.ndpr_plen == 128) {
595 			break;	/* we don't need to install a host route. */
596 		}
597 		pr0.ndpr_prefix = ifra->ifra_addr;
598 		pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
599 		/* apply the mask for safety. */
600 		for (i = 0; i < 4; i++) {
601 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
602 			    ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
603 		}
604 		/*
605 		 * XXX: since we don't have an API to set prefix (not address)
606 		 * lifetimes, we just use the same lifetimes as addresses.
607 		 * The (temporarily) installed lifetimes can be overridden by
608 		 * later advertised RAs (when accept_rtadv is non 0), which is
609 		 * an intended behavior.
610 		 */
611 		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
612 		pr0.ndpr_raf_auto =
613 		    ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
614 		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
615 		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
616 
617 		/* add the prefix if not yet. */
618 		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
619 			/*
620 			 * nd6_prelist_add will install the corresponding
621 			 * interface route.
622 			 */
623 			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
624 				return (error);
625 			if (pr == NULL) {
626 				log(LOG_ERR, "nd6_prelist_add succeeded but "
627 				    "no prefix\n");
628 				return (EINVAL); /* XXX panic here? */
629 			}
630 		}
631 		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
632 		    == NULL) {
633 		    	/* XXX: this should not happen! */
634 			log(LOG_ERR, "in6_control: addition succeeded, but"
635 			    " no ifaddr\n");
636 		} else {
637 			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
638 			    ia->ia6_ndpr == NULL) { /* new autoconfed addr */
639 				ia->ia6_ndpr = pr;
640 				pr->ndpr_refcnt++;
641 
642 				/*
643 				 * If this is the first autoconf address from
644 				 * the prefix, create a temporary address
645 				 * as well (when specified).
646 				 */
647 				if (ip6_use_tempaddr &&
648 				    pr->ndpr_refcnt == 1) {
649 					int e;
650 					if ((e = in6_tmpifadd(ia, 1)) != 0) {
651 						log(LOG_NOTICE, "in6_control: "
652 						    "failed to create a "
653 						    "temporary address, "
654 						    "errno=%d\n", e);
655 					}
656 				}
657 			}
658 
659 			/*
660 			 * this might affect the status of autoconfigured
661 			 * addresses, that is, this address might make
662 			 * other addresses detached.
663 			 */
664 			pfxlist_onlink_check();
665 		}
666 		if (error == 0 && ia)
667 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
668 		break;
669 	}
670 
671 	case SIOCDIFADDR_IN6:
672 	{
673 		int i = 0;
674 		struct nd_prefix pr0, *pr;
675 
676 		/*
677 		 * If the address being deleted is the only one that owns
678 		 * the corresponding prefix, expire the prefix as well.
679 		 * XXX: theoretically, we don't have to worry about such
680 		 * relationship, since we separate the address management
681 		 * and the prefix management.  We do this, however, to provide
682 		 * as much backward compatibility as possible in terms of
683 		 * the ioctl operation.
684 		 */
685 		bzero(&pr0, sizeof(pr0));
686 		pr0.ndpr_ifp = ifp;
687 		pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
688 					     NULL);
689 		if (pr0.ndpr_plen == 128)
690 			goto purgeaddr;
691 		pr0.ndpr_prefix = ia->ia_addr;
692 		pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
693 		for (i = 0; i < 4; i++) {
694 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
695 				ia->ia_prefixmask.sin6_addr.s6_addr32[i];
696 		}
697 		/*
698 		 * The logic of the following condition is a bit complicated.
699 		 * We expire the prefix when
700 		 * 1. the address obeys autoconfiguration and it is the
701 		 *    only owner of the associated prefix, or
702 		 * 2. the address does not obey autoconf and there is no
703 		 *    other owner of the prefix.
704 		 */
705 		if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
706 		    (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
707 		      pr->ndpr_refcnt == 1) ||
708 		     ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
709 		      pr->ndpr_refcnt == 0))) {
710 			pr->ndpr_expire = 1; /* XXX: just for expiration */
711 		}
712 
713 	  purgeaddr:
714 		in6_purgeaddr(&ia->ia_ifa);
715 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
716 		break;
717 	}
718 
719 	default:
720 		if (ifp == NULL || ifp->if_ioctl == 0)
721 			return (EOPNOTSUPP);
722 		return ((*ifp->if_ioctl)(ifp, cmd, data));
723 	}
724 
725 	return (0);
726 }
727 
728 /*
729  * Update parameters of an IPv6 interface address.
730  * If necessary, a new entry is created and linked into address chains.
731  * This function is separated from in6_control().
732  * XXX: should this be performed under splnet()?
733  */
734 int
735 in6_update_ifa(ifp, ifra, ia)
736 	struct ifnet *ifp;
737 	struct in6_aliasreq *ifra;
738 	struct in6_ifaddr *ia;
739 {
740 	int error = 0, hostIsNew = 0, plen = -1;
741 	struct in6_ifaddr *oia;
742 	struct sockaddr_in6 dst6;
743 	struct in6_addrlifetime *lt;
744 
745 	/* Validate parameters */
746 	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
747 		return (EINVAL);
748 
749 	/*
750 	 * The destination address for a p2p link must have a family
751 	 * of AF_UNSPEC or AF_INET6.
752 	 */
753 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
754 	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
755 	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
756 		return (EAFNOSUPPORT);
757 	/*
758 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
759 	 * does not carry fields other than sin6_len.
760 	 */
761 	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
762 		return (EINVAL);
763 	/*
764 	 * Because the IPv6 address architecture is classless, we require
765 	 * users to specify a (non 0) prefix length (mask) for a new address.
766 	 * We also require the prefix (when specified) mask is valid, and thus
767 	 * reject a non-consecutive mask.
768 	 */
769 	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
770 		return (EINVAL);
771 	if (ifra->ifra_prefixmask.sin6_len != 0) {
772 		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
773 		    (u_char *)&ifra->ifra_prefixmask +
774 		    ifra->ifra_prefixmask.sin6_len);
775 		if (plen <= 0)
776 			return (EINVAL);
777 	} else {
778 		/*
779 		 * In this case, ia must not be NULL.  We just use its prefix
780 		 * length.
781 		 */
782 		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
783 	}
784 	/*
785 	 * If the destination address on a p2p interface is specified,
786 	 * and the address is a scoped one, validate/set the scope
787 	 * zone identifier.
788 	 */
789 	dst6 = ifra->ifra_dstaddr;
790 	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
791 	    (dst6.sin6_family == AF_INET6)) {
792 		u_int32_t zoneid;
793 
794 		if ((error = in6_recoverscope(&dst6,
795 		    &ifra->ifra_dstaddr.sin6_addr, ifp)) != 0)
796 			return (error);
797 		if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &zoneid))
798 			return (EINVAL);
799 		if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
800 			dst6.sin6_scope_id = zoneid;
801 		else if (dst6.sin6_scope_id != zoneid)
802 			return (EINVAL); /* scope ID mismatch. */
803 		if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
804 		    != 0)
805 			return (error);
806 		dst6.sin6_scope_id = 0; /* XXX */
807 	}
808 	/*
809 	 * The destination address can be specified only for a p2p or a
810 	 * loopback interface.  If specified, the corresponding prefix length
811 	 * must be 128.
812 	 */
813 	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
814 		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
815 			/* XXX: noisy message */
816 			nd6log((LOG_INFO, "in6_update_ifa: a destination can "
817 			    "be specified for a p2p or a loopback IF only\n"));
818 			return (EINVAL);
819 		}
820 		if (plen != 128) {
821 			nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
822 			    "be 128 when dstaddr is specified\n"));
823 			return (EINVAL);
824 		}
825 	}
826 	/* lifetime consistency check */
827 	lt = &ifra->ifra_lifetime;
828 	if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
829 	    && lt->ia6t_vltime + time_second < time_second) {
830 		return EINVAL;
831 	}
832 	if (lt->ia6t_vltime == 0) {
833 		/*
834 		 * the following log might be noisy, but this is a typical
835 		 * configuration mistake or a tool's bug.
836 		 */
837 		nd6log((LOG_INFO,
838 		    "in6_update_ifa: valid lifetime is 0 for %s\n",
839 		    ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
840 	}
841 	if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
842 	    && lt->ia6t_pltime + time_second < time_second) {
843 		return EINVAL;
844 	}
845 
846 	/*
847 	 * If this is a new address, allocate a new ifaddr and link it
848 	 * into chains.
849 	 */
850 	if (ia == NULL) {
851 		hostIsNew = 1;
852 		/*
853 		 * When in6_update_ifa() is called in a process of a received
854 		 * RA, it is called under an interrupt context.  So, we should
855 		 * call malloc with M_NOWAIT.
856 		 */
857 		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
858 		    M_NOWAIT);
859 		if (ia == NULL)
860 			return (ENOBUFS);
861 		bzero((caddr_t)ia, sizeof(*ia));
862 		/* Initialize the address and masks */
863 		IFA_LOCK_INIT(&ia->ia_ifa);
864 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
865 		ia->ia_addr.sin6_family = AF_INET6;
866 		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
867 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
868 			/*
869 			 * XXX: some functions expect that ifa_dstaddr is not
870 			 * NULL for p2p interfaces.
871 			 */
872 			ia->ia_ifa.ifa_dstaddr =
873 			    (struct sockaddr *)&ia->ia_dstaddr;
874 		} else {
875 			ia->ia_ifa.ifa_dstaddr = NULL;
876 		}
877 		ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
878 
879 		ia->ia_ifp = ifp;
880 		if ((oia = in6_ifaddr) != NULL) {
881 			for ( ; oia->ia_next; oia = oia->ia_next)
882 				continue;
883 			oia->ia_next = ia;
884 		} else
885 			in6_ifaddr = ia;
886 
887 		ia->ia_ifa.ifa_refcnt = 1;
888 		TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
889 	}
890 
891 	/* set prefix mask */
892 	if (ifra->ifra_prefixmask.sin6_len) {
893 		/*
894 		 * We prohibit changing the prefix length of an existing
895 		 * address, because
896 		 * + such an operation should be rare in IPv6, and
897 		 * + the operation would confuse prefix management.
898 		 */
899 		if (ia->ia_prefixmask.sin6_len &&
900 		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
901 			nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
902 			    " existing (%s) address should not be changed\n",
903 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
904 			error = EINVAL;
905 			goto unlink;
906 		}
907 		ia->ia_prefixmask = ifra->ifra_prefixmask;
908 	}
909 
910 	/*
911 	 * If a new destination address is specified, scrub the old one and
912 	 * install the new destination.  Note that the interface must be
913 	 * p2p or loopback (see the check above.)
914 	 */
915 	if (dst6.sin6_family == AF_INET6 &&
916 	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
917 		int e;
918 
919 		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
920 		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
921 			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
922 			    "a route to the old destination: %s\n",
923 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
924 			/* proceed anyway... */
925 		} else
926 			ia->ia_flags &= ~IFA_ROUTE;
927 		ia->ia_dstaddr = dst6;
928 	}
929 
930 	/* reset the interface and routing table appropriately. */
931 	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
932 		goto unlink;
933 
934 	/*
935 	 * Beyond this point, we should call in6_purgeaddr upon an error,
936 	 * not just go to unlink.
937 	 */
938 
939 	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
940 		struct sockaddr_in6 mltaddr, mltmask;
941 		struct in6_multi *in6m;
942 
943 		if (hostIsNew) {
944 			/* join solicited multicast addr for new host id */
945 			struct in6_addr llsol;
946 
947 			bzero(&llsol, sizeof(struct in6_addr));
948 			llsol.s6_addr16[0] = htons(0xff02);
949 			llsol.s6_addr16[1] = htons(ifp->if_index);
950 			llsol.s6_addr32[1] = 0;
951 			llsol.s6_addr32[2] = htonl(1);
952 			llsol.s6_addr32[3] =
953 				ifra->ifra_addr.sin6_addr.s6_addr32[3];
954 			llsol.s6_addr8[12] = 0xff;
955 			(void)in6_addmulti(&llsol, ifp, &error);
956 			if (error != 0) {
957 				nd6log((LOG_WARNING,
958 				    "in6_update_ifa: addmulti failed for "
959 				    "%s on %s (errno=%d)\n",
960 				    ip6_sprintf(&llsol), if_name(ifp),
961 				    error));
962 				in6_purgeaddr((struct ifaddr *)ia);
963 				return (error);
964 			}
965 		}
966 
967 		bzero(&mltmask, sizeof(mltmask));
968 		mltmask.sin6_len = sizeof(struct sockaddr_in6);
969 		mltmask.sin6_family = AF_INET6;
970 		mltmask.sin6_addr = in6mask32;
971 
972 		/*
973 		 * join link-local all-nodes address
974 		 */
975 		bzero(&mltaddr, sizeof(mltaddr));
976 		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
977 		mltaddr.sin6_family = AF_INET6;
978 		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
979 		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
980 
981 		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
982 		if (in6m == NULL) {
983 			rtrequest(RTM_ADD,
984 				  (struct sockaddr *)&mltaddr,
985 				  (struct sockaddr *)&ia->ia_addr,
986 				  (struct sockaddr *)&mltmask,
987 				  RTF_UP|RTF_CLONING,  /* xxx */
988 				  (struct rtentry **)0);
989 			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
990 			if (error != 0) {
991 				nd6log((LOG_WARNING,
992 				    "in6_update_ifa: addmulti failed for "
993 				    "%s on %s (errno=%d)\n",
994 				    ip6_sprintf(&mltaddr.sin6_addr),
995 				    if_name(ifp), error));
996 			}
997 		}
998 
999 		/*
1000 		 * join node information group address
1001 		 */
1002 #define hostnamelen	strlen(hostname)
1003 		if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1004 		    == 0) {
1005 			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1006 			if (in6m == NULL && ia != NULL) {
1007 				(void)in6_addmulti(&mltaddr.sin6_addr,
1008 				    ifp, &error);
1009 				if (error != 0) {
1010 					nd6log((LOG_WARNING, "in6_update_ifa: "
1011 					    "addmulti failed for "
1012 					    "%s on %s (errno=%d)\n",
1013 					    ip6_sprintf(&mltaddr.sin6_addr),
1014 					    if_name(ifp), error));
1015 				}
1016 			}
1017 		}
1018 #undef hostnamelen
1019 
1020 		/*
1021 		 * join node-local all-nodes address, on loopback.
1022 		 * XXX: since "node-local" is obsoleted by interface-local,
1023 		 *      we have to join the group on every interface with
1024 		 *      some interface-boundary restriction.
1025 		 */
1026 		if (ifp->if_flags & IFF_LOOPBACK) {
1027 			struct in6_ifaddr *ia_loop;
1028 
1029 			struct in6_addr loop6 = in6addr_loopback;
1030 			ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1031 
1032 			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1033 
1034 			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1035 			if (in6m == NULL && ia_loop != NULL) {
1036 				rtrequest(RTM_ADD,
1037 					  (struct sockaddr *)&mltaddr,
1038 					  (struct sockaddr *)&ia_loop->ia_addr,
1039 					  (struct sockaddr *)&mltmask,
1040 					  RTF_UP,
1041 					  (struct rtentry **)0);
1042 				(void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1043 						   &error);
1044 				if (error != 0) {
1045 					nd6log((LOG_WARNING, "in6_update_ifa: "
1046 					    "addmulti failed for %s on %s "
1047 					    "(errno=%d)\n",
1048 					    ip6_sprintf(&mltaddr.sin6_addr),
1049 					    if_name(ifp), error));
1050 				}
1051 			}
1052 		}
1053 	}
1054 
1055 	ia->ia6_flags = ifra->ifra_flags;
1056 	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/*safety*/
1057 	ia->ia6_flags &= ~IN6_IFF_NODAD;	/* Mobile IPv6 */
1058 
1059 	ia->ia6_lifetime = ifra->ifra_lifetime;
1060 	/* for sanity */
1061 	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1062 		ia->ia6_lifetime.ia6t_expire =
1063 			time_second + ia->ia6_lifetime.ia6t_vltime;
1064 	} else
1065 		ia->ia6_lifetime.ia6t_expire = 0;
1066 	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1067 		ia->ia6_lifetime.ia6t_preferred =
1068 			time_second + ia->ia6_lifetime.ia6t_pltime;
1069 	} else
1070 		ia->ia6_lifetime.ia6t_preferred = 0;
1071 
1072 	/*
1073 	 * Perform DAD, if needed.
1074 	 * XXX It may be of use, if we can administratively
1075 	 * disable DAD.
1076 	 */
1077 	if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1078 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
1079 		nd6_dad_start((struct ifaddr *)ia, NULL);
1080 	}
1081 
1082 	return (error);
1083 
1084   unlink:
1085 	/*
1086 	 * XXX: if a change of an existing address failed, keep the entry
1087 	 * anyway.
1088 	 */
1089 	if (hostIsNew)
1090 		in6_unlink_ifa(ia, ifp);
1091 	return (error);
1092 }
1093 
1094 void
1095 in6_purgeaddr(ifa)
1096 	struct ifaddr *ifa;
1097 {
1098 	struct ifnet *ifp = ifa->ifa_ifp;
1099 	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1100 
1101 	/* stop DAD processing */
1102 	nd6_dad_stop(ifa);
1103 
1104 	/*
1105 	 * delete route to the destination of the address being purged.
1106 	 * The interface must be p2p or loopback in this case.
1107 	 */
1108 	if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1109 		int e;
1110 
1111 		if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1112 		    != 0) {
1113 			log(LOG_ERR, "in6_purgeaddr: failed to remove "
1114 			    "a route to the p2p destination: %s on %s, "
1115 			    "errno=%d\n",
1116 			    ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1117 			    e);
1118 			/* proceed anyway... */
1119 		} else
1120 			ia->ia_flags &= ~IFA_ROUTE;
1121 	}
1122 
1123 	/* Remove ownaddr's loopback rtentry, if it exists. */
1124 	in6_ifremloop(&(ia->ia_ifa));
1125 
1126 	if (ifp->if_flags & IFF_MULTICAST) {
1127 		/*
1128 		 * delete solicited multicast addr for deleting host id
1129 		 */
1130 		struct in6_multi *in6m;
1131 		struct in6_addr llsol;
1132 		bzero(&llsol, sizeof(struct in6_addr));
1133 		llsol.s6_addr16[0] = htons(0xff02);
1134 		llsol.s6_addr16[1] = htons(ifp->if_index);
1135 		llsol.s6_addr32[1] = 0;
1136 		llsol.s6_addr32[2] = htonl(1);
1137 		llsol.s6_addr32[3] =
1138 			ia->ia_addr.sin6_addr.s6_addr32[3];
1139 		llsol.s6_addr8[12] = 0xff;
1140 
1141 		IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1142 		if (in6m)
1143 			in6_delmulti(in6m);
1144 	}
1145 
1146 	in6_unlink_ifa(ia, ifp);
1147 }
1148 
1149 static void
1150 in6_unlink_ifa(ia, ifp)
1151 	struct in6_ifaddr *ia;
1152 	struct ifnet *ifp;
1153 {
1154 	int plen, iilen;
1155 	struct in6_ifaddr *oia;
1156 	int	s = splnet();
1157 
1158 	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1159 
1160 	oia = ia;
1161 	if (oia == (ia = in6_ifaddr))
1162 		in6_ifaddr = ia->ia_next;
1163 	else {
1164 		while (ia->ia_next && (ia->ia_next != oia))
1165 			ia = ia->ia_next;
1166 		if (ia->ia_next)
1167 			ia->ia_next = oia->ia_next;
1168 		else {
1169 			/* search failed */
1170 			printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1171 		}
1172 	}
1173 
1174 	if (oia->ia6_ifpr) {	/* check for safety */
1175 		plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1176 		iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1177 		in6_prefix_remove_ifid(iilen, oia);
1178 	}
1179 
1180 	/*
1181 	 * When an autoconfigured address is being removed, release the
1182 	 * reference to the base prefix.  Also, since the release might
1183 	 * affect the status of other (detached) addresses, call
1184 	 * pfxlist_onlink_check().
1185 	 */
1186 	if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1187 		if (oia->ia6_ndpr == NULL) {
1188 			nd6log((LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1189 			    "%p has no prefix\n", oia));
1190 		} else {
1191 			oia->ia6_ndpr->ndpr_refcnt--;
1192 			oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1193 			oia->ia6_ndpr = NULL;
1194 		}
1195 
1196 		pfxlist_onlink_check();
1197 	}
1198 
1199 	/*
1200 	 * release another refcnt for the link from in6_ifaddr.
1201 	 * Note that we should decrement the refcnt at least once for all *BSD.
1202 	 */
1203 	IFAFREE(&oia->ia_ifa);
1204 
1205 	splx(s);
1206 }
1207 
1208 void
1209 in6_purgeif(ifp)
1210 	struct ifnet *ifp;
1211 {
1212 	struct ifaddr *ifa, *nifa;
1213 
1214 	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1215 		nifa = TAILQ_NEXT(ifa, ifa_list);
1216 		if (ifa->ifa_addr->sa_family != AF_INET6)
1217 			continue;
1218 		in6_purgeaddr(ifa);
1219 	}
1220 
1221 	in6_ifdetach(ifp);
1222 }
1223 
1224 /*
1225  * SIOC[GAD]LIFADDR.
1226  *	SIOCGLIFADDR: get first address. (?)
1227  *	SIOCGLIFADDR with IFLR_PREFIX:
1228  *		get first address that matches the specified prefix.
1229  *	SIOCALIFADDR: add the specified address.
1230  *	SIOCALIFADDR with IFLR_PREFIX:
1231  *		add the specified prefix, filling hostid part from
1232  *		the first link-local address.  prefixlen must be <= 64.
1233  *	SIOCDLIFADDR: delete the specified address.
1234  *	SIOCDLIFADDR with IFLR_PREFIX:
1235  *		delete the first address that matches the specified prefix.
1236  * return values:
1237  *	EINVAL on invalid parameters
1238  *	EADDRNOTAVAIL on prefix match failed/specified address not found
1239  *	other values may be returned from in6_ioctl()
1240  *
1241  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1242  * this is to accomodate address naming scheme other than RFC2374,
1243  * in the future.
1244  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1245  * address encoding scheme. (see figure on page 8)
1246  */
1247 static int
1248 in6_lifaddr_ioctl(so, cmd, data, ifp, td)
1249 	struct socket *so;
1250 	u_long cmd;
1251 	caddr_t	data;
1252 	struct ifnet *ifp;
1253 	struct thread *td;
1254 {
1255 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1256 	struct ifaddr *ifa;
1257 	struct sockaddr *sa;
1258 
1259 	/* sanity checks */
1260 	if (!data || !ifp) {
1261 		panic("invalid argument to in6_lifaddr_ioctl");
1262 		/* NOTREACHED */
1263 	}
1264 
1265 	switch (cmd) {
1266 	case SIOCGLIFADDR:
1267 		/* address must be specified on GET with IFLR_PREFIX */
1268 		if ((iflr->flags & IFLR_PREFIX) == 0)
1269 			break;
1270 		/* FALLTHROUGH */
1271 	case SIOCALIFADDR:
1272 	case SIOCDLIFADDR:
1273 		/* address must be specified on ADD and DELETE */
1274 		sa = (struct sockaddr *)&iflr->addr;
1275 		if (sa->sa_family != AF_INET6)
1276 			return EINVAL;
1277 		if (sa->sa_len != sizeof(struct sockaddr_in6))
1278 			return EINVAL;
1279 		/* XXX need improvement */
1280 		sa = (struct sockaddr *)&iflr->dstaddr;
1281 		if (sa->sa_family && sa->sa_family != AF_INET6)
1282 			return EINVAL;
1283 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1284 			return EINVAL;
1285 		break;
1286 	default: /* shouldn't happen */
1287 #if 0
1288 		panic("invalid cmd to in6_lifaddr_ioctl");
1289 		/* NOTREACHED */
1290 #else
1291 		return EOPNOTSUPP;
1292 #endif
1293 	}
1294 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1295 		return EINVAL;
1296 
1297 	switch (cmd) {
1298 	case SIOCALIFADDR:
1299 	    {
1300 		struct in6_aliasreq ifra;
1301 		struct in6_addr *hostid = NULL;
1302 		int prefixlen;
1303 
1304 		if ((iflr->flags & IFLR_PREFIX) != 0) {
1305 			struct sockaddr_in6 *sin6;
1306 
1307 			/*
1308 			 * hostid is to fill in the hostid part of the
1309 			 * address.  hostid points to the first link-local
1310 			 * address attached to the interface.
1311 			 */
1312 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1313 			if (!ifa)
1314 				return EADDRNOTAVAIL;
1315 			hostid = IFA_IN6(ifa);
1316 
1317 		 	/* prefixlen must be <= 64. */
1318 			if (64 < iflr->prefixlen)
1319 				return EINVAL;
1320 			prefixlen = iflr->prefixlen;
1321 
1322 			/* hostid part must be zero. */
1323 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1324 			if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1325 			    sin6->sin6_addr.s6_addr32[3] != 0) {
1326 				return EINVAL;
1327 			}
1328 		} else
1329 			prefixlen = iflr->prefixlen;
1330 
1331 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1332 		bzero(&ifra, sizeof(ifra));
1333 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1334 
1335 		bcopy(&iflr->addr, &ifra.ifra_addr,
1336 		    ((struct sockaddr *)&iflr->addr)->sa_len);
1337 		if (hostid) {
1338 			/* fill in hostid part */
1339 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1340 			    hostid->s6_addr32[2];
1341 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1342 			    hostid->s6_addr32[3];
1343 		}
1344 
1345 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1346 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1347 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1348 			if (hostid) {
1349 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1350 				    hostid->s6_addr32[2];
1351 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1352 				    hostid->s6_addr32[3];
1353 			}
1354 		}
1355 
1356 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1357 		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1358 
1359 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1360 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1361 	    }
1362 	case SIOCGLIFADDR:
1363 	case SIOCDLIFADDR:
1364 	    {
1365 		struct in6_ifaddr *ia;
1366 		struct in6_addr mask, candidate, match;
1367 		struct sockaddr_in6 *sin6;
1368 		int cmp;
1369 
1370 		bzero(&mask, sizeof(mask));
1371 		if (iflr->flags & IFLR_PREFIX) {
1372 			/* lookup a prefix rather than address. */
1373 			in6_prefixlen2mask(&mask, iflr->prefixlen);
1374 
1375 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1376 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
1377 			match.s6_addr32[0] &= mask.s6_addr32[0];
1378 			match.s6_addr32[1] &= mask.s6_addr32[1];
1379 			match.s6_addr32[2] &= mask.s6_addr32[2];
1380 			match.s6_addr32[3] &= mask.s6_addr32[3];
1381 
1382 			/* if you set extra bits, that's wrong */
1383 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1384 				return EINVAL;
1385 
1386 			cmp = 1;
1387 		} else {
1388 			if (cmd == SIOCGLIFADDR) {
1389 				/* on getting an address, take the 1st match */
1390 				cmp = 0;	/* XXX */
1391 			} else {
1392 				/* on deleting an address, do exact match */
1393 				in6_prefixlen2mask(&mask, 128);
1394 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
1395 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
1396 
1397 				cmp = 1;
1398 			}
1399 		}
1400 
1401 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1402 			if (ifa->ifa_addr->sa_family != AF_INET6)
1403 				continue;
1404 			if (!cmp)
1405 				break;
1406 
1407 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1408 			/*
1409 			 * XXX: this is adhoc, but is necessary to allow
1410 			 * a user to specify fe80::/64 (not /10) for a
1411 			 * link-local address.
1412 			 */
1413 			if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1414 				candidate.s6_addr16[1] = 0;
1415 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
1416 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
1417 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
1418 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
1419 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1420 				break;
1421 		}
1422 		if (!ifa)
1423 			return EADDRNOTAVAIL;
1424 		ia = ifa2ia6(ifa);
1425 
1426 		if (cmd == SIOCGLIFADDR) {
1427 			struct sockaddr_in6 *s6;
1428 
1429 			/* fill in the if_laddrreq structure */
1430 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1431 			s6 = (struct sockaddr_in6 *)&iflr->addr;
1432 			if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1433 				s6->sin6_addr.s6_addr16[1] = 0;
1434 				if (in6_addr2zoneid(ifp, &s6->sin6_addr,
1435 				    &s6->sin6_scope_id))
1436 					return (EINVAL);	/* XXX */
1437 			}
1438 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1439 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1440 				    ia->ia_dstaddr.sin6_len);
1441 				s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1442 				if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1443 					s6->sin6_addr.s6_addr16[1] = 0;
1444 					if (in6_addr2zoneid(ifp,
1445 					    &s6->sin6_addr, &s6->sin6_scope_id))
1446 						return (EINVAL); /* EINVAL */
1447 				}
1448 			} else
1449 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1450 
1451 			iflr->prefixlen =
1452 			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1453 
1454 			iflr->flags = ia->ia6_flags;	/* XXX */
1455 
1456 			return 0;
1457 		} else {
1458 			struct in6_aliasreq ifra;
1459 
1460 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1461 			bzero(&ifra, sizeof(ifra));
1462 			bcopy(iflr->iflr_name, ifra.ifra_name,
1463 			    sizeof(ifra.ifra_name));
1464 
1465 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1466 			    ia->ia_addr.sin6_len);
1467 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1468 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1469 				    ia->ia_dstaddr.sin6_len);
1470 			} else {
1471 				bzero(&ifra.ifra_dstaddr,
1472 				    sizeof(ifra.ifra_dstaddr));
1473 			}
1474 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1475 			    ia->ia_prefixmask.sin6_len);
1476 
1477 			ifra.ifra_flags = ia->ia6_flags;
1478 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1479 			    ifp, td);
1480 		}
1481 	    }
1482 	}
1483 
1484 	return EOPNOTSUPP;	/* just for safety */
1485 }
1486 
1487 /*
1488  * Initialize an interface's intetnet6 address
1489  * and routing table entry.
1490  */
1491 static int
1492 in6_ifinit(ifp, ia, sin6, newhost)
1493 	struct ifnet *ifp;
1494 	struct in6_ifaddr *ia;
1495 	struct sockaddr_in6 *sin6;
1496 	int newhost;
1497 {
1498 	int	error = 0, plen, ifacount = 0;
1499 	int	s = splimp();
1500 	struct ifaddr *ifa;
1501 
1502 	/*
1503 	 * Give the interface a chance to initialize
1504 	 * if this is its first address,
1505 	 * and to validate the address if necessary.
1506 	 */
1507 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1508 		if (ifa->ifa_addr == NULL)
1509 			continue;	/* just for safety */
1510 		if (ifa->ifa_addr->sa_family != AF_INET6)
1511 			continue;
1512 		ifacount++;
1513 	}
1514 
1515 	ia->ia_addr = *sin6;
1516 
1517 	if (ifacount <= 1 && ifp->if_ioctl &&
1518 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1519 		splx(s);
1520 		return (error);
1521 	}
1522 	splx(s);
1523 
1524 	ia->ia_ifa.ifa_metric = ifp->if_metric;
1525 
1526 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
1527 
1528 	/*
1529 	 * Special case:
1530 	 * If a new destination address is specified for a point-to-point
1531 	 * interface, install a route to the destination as an interface
1532 	 * direct route.
1533 	 * XXX: the logic below rejects assigning multiple addresses on a p2p
1534 	 * interface that share a same destination.
1535 	 */
1536 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1537 	if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1538 	    ia->ia_dstaddr.sin6_family == AF_INET6) {
1539 		if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1540 				    RTF_UP | RTF_HOST)) != 0)
1541 			return (error);
1542 		ia->ia_flags |= IFA_ROUTE;
1543 	}
1544 	if (plen < 128) {
1545 		/*
1546 		 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1547 		 */
1548 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
1549 	}
1550 
1551 	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1552 	if (newhost) {
1553 		/* set the rtrequest function to create llinfo */
1554 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1555 		in6_ifaddloop(&(ia->ia_ifa));
1556 	}
1557 
1558 	return (error);
1559 }
1560 
1561 /*
1562  * Find an IPv6 interface link-local address specific to an interface.
1563  */
1564 struct in6_ifaddr *
1565 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1566 	struct ifnet *ifp;
1567 	int ignoreflags;
1568 {
1569 	struct ifaddr *ifa;
1570 
1571 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1572 		if (ifa->ifa_addr == NULL)
1573 			continue;	/* just for safety */
1574 		if (ifa->ifa_addr->sa_family != AF_INET6)
1575 			continue;
1576 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1577 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1578 			     ignoreflags) != 0)
1579 				continue;
1580 			break;
1581 		}
1582 	}
1583 
1584 	return ((struct in6_ifaddr *)ifa);
1585 }
1586 
1587 
1588 /*
1589  * find the internet address corresponding to a given interface and address.
1590  */
1591 struct in6_ifaddr *
1592 in6ifa_ifpwithaddr(ifp, addr)
1593 	struct ifnet *ifp;
1594 	struct in6_addr *addr;
1595 {
1596 	struct ifaddr *ifa;
1597 
1598 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1599 		if (ifa->ifa_addr == NULL)
1600 			continue;	/* just for safety */
1601 		if (ifa->ifa_addr->sa_family != AF_INET6)
1602 			continue;
1603 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1604 			break;
1605 	}
1606 
1607 	return ((struct in6_ifaddr *)ifa);
1608 }
1609 
1610 /*
1611  * Convert IP6 address to printable (loggable) representation.
1612  */
1613 static char digits[] = "0123456789abcdef";
1614 static int ip6round = 0;
1615 char *
1616 ip6_sprintf(addr)
1617 	const struct in6_addr *addr;
1618 {
1619 	static char ip6buf[8][48];
1620 	int i;
1621 	char *cp;
1622 	const u_int16_t *a = (const u_int16_t *)addr;
1623 	const u_int8_t *d;
1624 	int dcolon = 0;
1625 
1626 	ip6round = (ip6round + 1) & 7;
1627 	cp = ip6buf[ip6round];
1628 
1629 	for (i = 0; i < 8; i++) {
1630 		if (dcolon == 1) {
1631 			if (*a == 0) {
1632 				if (i == 7)
1633 					*cp++ = ':';
1634 				a++;
1635 				continue;
1636 			} else
1637 				dcolon = 2;
1638 		}
1639 		if (*a == 0) {
1640 			if (dcolon == 0 && *(a + 1) == 0) {
1641 				if (i == 0)
1642 					*cp++ = ':';
1643 				*cp++ = ':';
1644 				dcolon = 1;
1645 			} else {
1646 				*cp++ = '0';
1647 				*cp++ = ':';
1648 			}
1649 			a++;
1650 			continue;
1651 		}
1652 		d = (const u_char *)a;
1653 		*cp++ = digits[*d >> 4];
1654 		*cp++ = digits[*d++ & 0xf];
1655 		*cp++ = digits[*d >> 4];
1656 		*cp++ = digits[*d & 0xf];
1657 		*cp++ = ':';
1658 		a++;
1659 	}
1660 	*--cp = 0;
1661 	return (ip6buf[ip6round]);
1662 }
1663 
1664 int
1665 in6_localaddr(in6)
1666 	struct in6_addr *in6;
1667 {
1668 	struct in6_ifaddr *ia;
1669 
1670 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1671 		return 1;
1672 
1673 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1674 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1675 		    &ia->ia_prefixmask.sin6_addr)) {
1676 			return 1;
1677 		}
1678 	}
1679 
1680 	return (0);
1681 }
1682 
1683 int
1684 in6_is_addr_deprecated(sa6)
1685 	struct sockaddr_in6 *sa6;
1686 {
1687 	struct in6_ifaddr *ia;
1688 
1689 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1690 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1691 				       &sa6->sin6_addr) &&
1692 		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1693 			return (1); /* true */
1694 
1695 		/* XXX: do we still have to go thru the rest of the list? */
1696 	}
1697 
1698 	return (0);		/* false */
1699 }
1700 
1701 /*
1702  * return length of part which dst and src are equal
1703  * hard coding...
1704  */
1705 int
1706 in6_matchlen(src, dst)
1707 struct in6_addr *src, *dst;
1708 {
1709 	int match = 0;
1710 	u_char *s = (u_char *)src, *d = (u_char *)dst;
1711 	u_char *lim = s + 16, r;
1712 
1713 	while (s < lim)
1714 		if ((r = (*d++ ^ *s++)) != 0) {
1715 			while (r < 128) {
1716 				match++;
1717 				r <<= 1;
1718 			}
1719 			break;
1720 		} else
1721 			match += 8;
1722 	return match;
1723 }
1724 
1725 /* XXX: to be scope conscious */
1726 int
1727 in6_are_prefix_equal(p1, p2, len)
1728 	struct in6_addr *p1, *p2;
1729 	int len;
1730 {
1731 	int bytelen, bitlen;
1732 
1733 	/* sanity check */
1734 	if (0 > len || len > 128) {
1735 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1736 		    len);
1737 		return (0);
1738 	}
1739 
1740 	bytelen = len / 8;
1741 	bitlen = len % 8;
1742 
1743 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1744 		return (0);
1745 	if (bitlen != 0 &&
1746 	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
1747 	    p2->s6_addr[bytelen] >> (8 - bitlen))
1748 		return (0);
1749 
1750 	return (1);
1751 }
1752 
1753 void
1754 in6_prefixlen2mask(maskp, len)
1755 	struct in6_addr *maskp;
1756 	int len;
1757 {
1758 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1759 	int bytelen, bitlen, i;
1760 
1761 	/* sanity check */
1762 	if (0 > len || len > 128) {
1763 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1764 		    len);
1765 		return;
1766 	}
1767 
1768 	bzero(maskp, sizeof(*maskp));
1769 	bytelen = len / 8;
1770 	bitlen = len % 8;
1771 	for (i = 0; i < bytelen; i++)
1772 		maskp->s6_addr[i] = 0xff;
1773 	if (bitlen)
1774 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1775 }
1776 
1777 /*
1778  * return the best address out of the same scope. if no address was
1779  * found, return the first valid address from designated IF.
1780  */
1781 struct in6_ifaddr *
1782 in6_ifawithifp(ifp, dst)
1783 	struct ifnet *ifp;
1784 	struct in6_addr *dst;
1785 {
1786 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
1787 	struct ifaddr *ifa;
1788 	struct in6_ifaddr *besta = 0;
1789 	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
1790 
1791 	dep[0] = dep[1] = NULL;
1792 
1793 	/*
1794 	 * We first look for addresses in the same scope.
1795 	 * If there is one, return it.
1796 	 * If two or more, return one which matches the dst longest.
1797 	 * If none, return one of global addresses assigned other ifs.
1798 	 */
1799 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1800 		if (ifa->ifa_addr->sa_family != AF_INET6)
1801 			continue;
1802 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1803 			continue; /* XXX: is there any case to allow anycast? */
1804 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1805 			continue; /* don't use this interface */
1806 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1807 			continue;
1808 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1809 			if (ip6_use_deprecated)
1810 				dep[0] = (struct in6_ifaddr *)ifa;
1811 			continue;
1812 		}
1813 
1814 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1815 			/*
1816 			 * call in6_matchlen() as few as possible
1817 			 */
1818 			if (besta) {
1819 				if (blen == -1)
1820 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1821 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
1822 				if (tlen > blen) {
1823 					blen = tlen;
1824 					besta = (struct in6_ifaddr *)ifa;
1825 				}
1826 			} else
1827 				besta = (struct in6_ifaddr *)ifa;
1828 		}
1829 	}
1830 	if (besta)
1831 		return (besta);
1832 
1833 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1834 		if (ifa->ifa_addr->sa_family != AF_INET6)
1835 			continue;
1836 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1837 			continue; /* XXX: is there any case to allow anycast? */
1838 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1839 			continue; /* don't use this interface */
1840 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1841 			continue;
1842 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1843 			if (ip6_use_deprecated)
1844 				dep[1] = (struct in6_ifaddr *)ifa;
1845 			continue;
1846 		}
1847 
1848 		return (struct in6_ifaddr *)ifa;
1849 	}
1850 
1851 	/* use the last-resort values, that are, deprecated addresses */
1852 	if (dep[0])
1853 		return dep[0];
1854 	if (dep[1])
1855 		return dep[1];
1856 
1857 	return NULL;
1858 }
1859 
1860 /*
1861  * perform DAD when interface becomes IFF_UP.
1862  */
1863 void
1864 in6_if_up(ifp)
1865 	struct ifnet *ifp;
1866 {
1867 	struct ifaddr *ifa;
1868 	struct in6_ifaddr *ia;
1869 	int dad_delay;		/* delay ticks before DAD output */
1870 
1871 	/*
1872 	 * special cases, like 6to4, are handled in in6_ifattach
1873 	 */
1874 	in6_ifattach(ifp, NULL);
1875 
1876 	dad_delay = 0;
1877 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1878 		if (ifa->ifa_addr->sa_family != AF_INET6)
1879 			continue;
1880 		ia = (struct in6_ifaddr *)ifa;
1881 		if (ia->ia6_flags & IN6_IFF_TENTATIVE)
1882 			nd6_dad_start(ifa, &dad_delay);
1883 	}
1884 }
1885 
1886 int
1887 in6if_do_dad(ifp)
1888 	struct ifnet *ifp;
1889 {
1890 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1891 		return (0);
1892 
1893 	switch (ifp->if_type) {
1894 #ifdef IFT_DUMMY
1895 	case IFT_DUMMY:
1896 #endif
1897 	case IFT_FAITH:
1898 		/*
1899 		 * These interfaces do not have the IFF_LOOPBACK flag,
1900 		 * but loop packets back.  We do not have to do DAD on such
1901 		 * interfaces.  We should even omit it, because loop-backed
1902 		 * NS would confuse the DAD procedure.
1903 		 */
1904 		return (0);
1905 	default:
1906 		/*
1907 		 * Our DAD routine requires the interface up and running.
1908 		 * However, some interfaces can be up before the RUNNING
1909 		 * status.  Additionaly, users may try to assign addresses
1910 		 * before the interface becomes up (or running).
1911 		 * We simply skip DAD in such a case as a work around.
1912 		 * XXX: we should rather mark "tentative" on such addresses,
1913 		 * and do DAD after the interface becomes ready.
1914 		 */
1915 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1916 		    (IFF_UP|IFF_RUNNING))
1917 			return (0);
1918 
1919 		return (1);
1920 	}
1921 }
1922 
1923 /*
1924  * Calculate max IPv6 MTU through all the interfaces and store it
1925  * to in6_maxmtu.
1926  */
1927 void
1928 in6_setmaxmtu()
1929 {
1930 	unsigned long maxmtu = 0;
1931 	struct ifnet *ifp;
1932 
1933 	IFNET_RLOCK();
1934 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1935 		/* this function can be called during ifnet initialization */
1936 		if (!ifp->if_afdata[AF_INET6])
1937 			continue;
1938 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
1939 		    IN6_LINKMTU(ifp) > maxmtu)
1940 			maxmtu = IN6_LINKMTU(ifp);
1941 	}
1942 	IFNET_RUNLOCK();
1943 	if (maxmtu)	     /* update only when maxmtu is positive */
1944 		in6_maxmtu = maxmtu;
1945 }
1946 
1947 void *
1948 in6_domifattach(ifp)
1949 	struct ifnet *ifp;
1950 {
1951 	struct in6_ifextra *ext;
1952 
1953 	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
1954 	bzero(ext, sizeof(*ext));
1955 
1956 	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
1957 	    M_IFADDR, M_WAITOK);
1958 	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
1959 
1960 	ext->icmp6_ifstat =
1961 	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
1962 	    M_IFADDR, M_WAITOK);
1963 	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
1964 
1965 	ext->nd_ifinfo = nd6_ifattach(ifp);
1966 	ext->scope6_id = scope6_ifattach(ifp);
1967 	return ext;
1968 }
1969 
1970 void
1971 in6_domifdetach(ifp, aux)
1972 	struct ifnet *ifp;
1973 	void *aux;
1974 {
1975 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
1976 
1977 	scope6_ifdetach(ext->scope6_id);
1978 	nd6_ifdetach(ext->nd_ifinfo);
1979 	free(ext->in6_ifstat, M_IFADDR);
1980 	free(ext->icmp6_ifstat, M_IFADDR);
1981 	free(ext, M_IFADDR);
1982 }
1983 
1984 /*
1985  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
1986  * v4 mapped addr or v4 compat addr
1987  */
1988 void
1989 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
1990 {
1991 	bzero(sin, sizeof(*sin));
1992 	sin->sin_len = sizeof(struct sockaddr_in);
1993 	sin->sin_family = AF_INET;
1994 	sin->sin_port = sin6->sin6_port;
1995 	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
1996 }
1997 
1998 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
1999 void
2000 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2001 {
2002 	bzero(sin6, sizeof(*sin6));
2003 	sin6->sin6_len = sizeof(struct sockaddr_in6);
2004 	sin6->sin6_family = AF_INET6;
2005 	sin6->sin6_port = sin->sin_port;
2006 	sin6->sin6_addr.s6_addr32[0] = 0;
2007 	sin6->sin6_addr.s6_addr32[1] = 0;
2008 	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2009 	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2010 }
2011 
2012 /* Convert sockaddr_in6 into sockaddr_in. */
2013 void
2014 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2015 {
2016 	struct sockaddr_in *sin_p;
2017 	struct sockaddr_in6 sin6;
2018 
2019 	/*
2020 	 * Save original sockaddr_in6 addr and convert it
2021 	 * to sockaddr_in.
2022 	 */
2023 	sin6 = *(struct sockaddr_in6 *)nam;
2024 	sin_p = (struct sockaddr_in *)nam;
2025 	in6_sin6_2_sin(sin_p, &sin6);
2026 }
2027 
2028 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2029 void
2030 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2031 {
2032 	struct sockaddr_in *sin_p;
2033 	struct sockaddr_in6 *sin6_p;
2034 
2035 	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2036 	       M_WAITOK);
2037 	sin_p = (struct sockaddr_in *)*nam;
2038 	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2039 	FREE(*nam, M_SONAME);
2040 	*nam = (struct sockaddr *)sin6_p;
2041 }
2042