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