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