xref: /freebsd/sys/netinet6/in6.c (revision 6e660824a82f590542932de52f128db584029893)
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_compat.h"
67 #include "opt_inet.h"
68 #include "opt_inet6.h"
69 
70 #include <sys/param.h>
71 #include <sys/errno.h>
72 #include <sys/jail.h>
73 #include <sys/malloc.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/systm.h>
78 #include <sys/priv.h>
79 #include <sys/proc.h>
80 #include <sys/time.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
83 
84 #include <net/if.h>
85 #include <net/if_var.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
89 #include <net/vnet.h>
90 
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <net/if_llatbl.h>
94 #include <netinet/if_ether.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/ip_carp.h>
99 
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/mld6_var.h>
104 #include <netinet6/ip6_mroute.h>
105 #include <netinet6/in6_ifattach.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/in6_pcb.h>
108 
109 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
110 #define V_icmp6_nodeinfo_oldmcprefix	VNET(icmp6_nodeinfo_oldmcprefix)
111 
112 /*
113  * Definitions of some costant IP6 addresses.
114  */
115 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
116 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
117 const struct in6_addr in6addr_nodelocal_allnodes =
118 	IN6ADDR_NODELOCAL_ALLNODES_INIT;
119 const struct in6_addr in6addr_linklocal_allnodes =
120 	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
121 const struct in6_addr in6addr_linklocal_allrouters =
122 	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
123 const struct in6_addr in6addr_linklocal_allv2routers =
124 	IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
125 
126 const struct in6_addr in6mask0 = IN6MASK0;
127 const struct in6_addr in6mask32 = IN6MASK32;
128 const struct in6_addr in6mask64 = IN6MASK64;
129 const struct in6_addr in6mask96 = IN6MASK96;
130 const struct in6_addr in6mask128 = IN6MASK128;
131 
132 const struct sockaddr_in6 sa6_any =
133 	{ sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
134 
135 static int in6_lifaddr_ioctl(struct socket *, u_long, caddr_t,
136 	struct ifnet *, struct thread *);
137 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
138 	struct sockaddr_in6 *, int);
139 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
140 
141 int	(*faithprefix_p)(struct in6_addr *);
142 
143 #define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
144 #define ia62ifa(ia6)	(&((ia6)->ia_ifa))
145 
146 void
147 in6_ifaddloop(struct ifaddr *ifa)
148 {
149 	struct sockaddr_dl gateway;
150 	struct sockaddr_in6 mask, addr;
151 	struct rtentry rt;
152 	struct in6_ifaddr *ia;
153 	struct ifnet *ifp;
154 	struct llentry *ln;
155 
156 	ia = ifa2ia6(ifa);
157 	ifp = ifa->ifa_ifp;
158 	IF_AFDATA_LOCK(ifp);
159 	ifa->ifa_rtrequest = nd6_rtrequest;
160 	ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
161 	    LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
162 	IF_AFDATA_UNLOCK(ifp);
163 	if (ln != NULL) {
164 		ln->la_expire = 0;  /* for IPv6 this means permanent */
165 		ln->ln_state = ND6_LLINFO_REACHABLE;
166 		/*
167 		 * initialize for rtmsg generation
168 		 */
169 		bzero(&gateway, sizeof(gateway));
170 		gateway.sdl_len = sizeof(gateway);
171 		gateway.sdl_family = AF_LINK;
172 		gateway.sdl_nlen = 0;
173 		gateway.sdl_alen = 6;
174 		memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned,
175 		    sizeof(ln->ll_addr));
176 		LLE_WUNLOCK(ln);
177 	}
178 
179 	bzero(&rt, sizeof(rt));
180 	rt.rt_gateway = (struct sockaddr *)&gateway;
181 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
182 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
183 	rt_mask(&rt) = (struct sockaddr *)&mask;
184 	rt_key(&rt) = (struct sockaddr *)&addr;
185 	rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
186 	/* Announce arrival of local address to all FIBs. */
187 	rt_newaddrmsg(RTM_ADD, ifa, 0, &rt);
188 }
189 
190 void
191 in6_ifremloop(struct ifaddr *ifa)
192 {
193 	struct sockaddr_dl gateway;
194 	struct sockaddr_in6 mask, addr;
195 	struct rtentry rt0;
196 	struct in6_ifaddr *ia;
197 	struct ifnet *ifp;
198 
199 	ia = ifa2ia6(ifa);
200 	ifp = ifa->ifa_ifp;
201 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
202 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
203 	lltable_prefix_free(AF_INET6, (struct sockaddr *)&addr,
204 	            (struct sockaddr *)&mask, LLE_STATIC);
205 
206 	/*
207 	 * initialize for rtmsg generation
208 	 */
209 	bzero(&gateway, sizeof(gateway));
210 	gateway.sdl_len = sizeof(gateway);
211 	gateway.sdl_family = AF_LINK;
212 	gateway.sdl_nlen = 0;
213 	gateway.sdl_alen = ifp->if_addrlen;
214 	bzero(&rt0, sizeof(rt0));
215 	rt0.rt_gateway = (struct sockaddr *)&gateway;
216 	rt_mask(&rt0) = (struct sockaddr *)&mask;
217 	rt_key(&rt0) = (struct sockaddr *)&addr;
218 	rt0.rt_flags = RTF_HOST | RTF_STATIC;
219 	/* Announce removal of local address to all FIBs. */
220 	rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
221 }
222 
223 int
224 in6_mask2len(struct in6_addr *mask, u_char *lim0)
225 {
226 	int x = 0, y;
227 	u_char *lim = lim0, *p;
228 
229 	/* ignore the scope_id part */
230 	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
231 		lim = (u_char *)mask + sizeof(*mask);
232 	for (p = (u_char *)mask; p < lim; x++, p++) {
233 		if (*p != 0xff)
234 			break;
235 	}
236 	y = 0;
237 	if (p < lim) {
238 		for (y = 0; y < 8; y++) {
239 			if ((*p & (0x80 >> y)) == 0)
240 				break;
241 		}
242 	}
243 
244 	/*
245 	 * when the limit pointer is given, do a stricter check on the
246 	 * remaining bits.
247 	 */
248 	if (p < lim) {
249 		if (y != 0 && (*p & (0x00ff >> y)) != 0)
250 			return (-1);
251 		for (p = p + 1; p < lim; p++)
252 			if (*p != 0)
253 				return (-1);
254 	}
255 
256 	return x * 8 + y;
257 }
258 
259 #ifdef COMPAT_FREEBSD32
260 struct in6_ndifreq32 {
261 	char ifname[IFNAMSIZ];
262 	uint32_t ifindex;
263 };
264 #define	SIOCGDEFIFACE32_IN6	_IOWR('i', 86, struct in6_ndifreq32)
265 #endif
266 
267 int
268 in6_control(struct socket *so, u_long cmd, caddr_t data,
269     struct ifnet *ifp, struct thread *td)
270 {
271 	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
272 	struct	in6_ifaddr *ia = NULL;
273 	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
274 	struct sockaddr_in6 *sa6;
275 	int carp_attached = 0;
276 	int error;
277 	u_long ocmd = cmd;
278 
279 	/*
280 	 * Compat to make pre-10.x ifconfig(8) operable.
281 	 */
282 	if (cmd == OSIOCAIFADDR_IN6)
283 		cmd = SIOCAIFADDR_IN6;
284 
285 	switch (cmd) {
286 	case SIOCGETSGCNT_IN6:
287 	case SIOCGETMIFCNT_IN6:
288 		/*
289 		 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
290 		 * We cannot see how that would be needed, so do not adjust the
291 		 * KPI blindly; more likely should clean up the IPv4 variant.
292 		 */
293 		return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
294 	}
295 
296 	switch(cmd) {
297 	case SIOCAADDRCTL_POLICY:
298 	case SIOCDADDRCTL_POLICY:
299 		if (td != NULL) {
300 			error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
301 			if (error)
302 				return (error);
303 		}
304 		return (in6_src_ioctl(cmd, data));
305 	}
306 
307 	if (ifp == NULL)
308 		return (EOPNOTSUPP);
309 
310 	switch (cmd) {
311 	case SIOCSNDFLUSH_IN6:
312 	case SIOCSPFXFLUSH_IN6:
313 	case SIOCSRTRFLUSH_IN6:
314 	case SIOCSDEFIFACE_IN6:
315 	case SIOCSIFINFO_FLAGS:
316 	case SIOCSIFINFO_IN6:
317 		if (td != NULL) {
318 			error = priv_check(td, PRIV_NETINET_ND6);
319 			if (error)
320 				return (error);
321 		}
322 		/* FALLTHROUGH */
323 	case OSIOCGIFINFO_IN6:
324 	case SIOCGIFINFO_IN6:
325 	case SIOCGDRLST_IN6:
326 	case SIOCGPRLST_IN6:
327 	case SIOCGNBRINFO_IN6:
328 	case SIOCGDEFIFACE_IN6:
329 		return (nd6_ioctl(cmd, data, ifp));
330 
331 #ifdef COMPAT_FREEBSD32
332 	case SIOCGDEFIFACE32_IN6:
333 		{
334 			struct in6_ndifreq ndif;
335 			struct in6_ndifreq32 *ndif32;
336 
337 			error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
338 			    ifp);
339 			if (error)
340 				return (error);
341 			ndif32 = (struct in6_ndifreq32 *)data;
342 			ndif32->ifindex = ndif.ifindex;
343 			return (0);
344 		}
345 #endif
346 	}
347 
348 	switch (cmd) {
349 	case SIOCSIFPREFIX_IN6:
350 	case SIOCDIFPREFIX_IN6:
351 	case SIOCAIFPREFIX_IN6:
352 	case SIOCCIFPREFIX_IN6:
353 	case SIOCSGIFPREFIX_IN6:
354 	case SIOCGIFPREFIX_IN6:
355 		log(LOG_NOTICE,
356 		    "prefix ioctls are now invalidated. "
357 		    "please use ifconfig.\n");
358 		return (EOPNOTSUPP);
359 	}
360 
361 	switch (cmd) {
362 	case SIOCSSCOPE6:
363 		if (td != NULL) {
364 			error = priv_check(td, PRIV_NETINET_SCOPE6);
365 			if (error)
366 				return (error);
367 		}
368 		return (scope6_set(ifp,
369 		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
370 	case SIOCGSCOPE6:
371 		return (scope6_get(ifp,
372 		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
373 	case SIOCGSCOPE6DEF:
374 		return (scope6_get_default((struct scope6_id *)
375 		    ifr->ifr_ifru.ifru_scope_id));
376 	}
377 
378 	switch (cmd) {
379 	case SIOCALIFADDR:
380 		if (td != NULL) {
381 			error = priv_check(td, PRIV_NET_ADDIFADDR);
382 			if (error)
383 				return (error);
384 		}
385 		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
386 
387 	case SIOCDLIFADDR:
388 		if (td != NULL) {
389 			error = priv_check(td, PRIV_NET_DELIFADDR);
390 			if (error)
391 				return (error);
392 		}
393 		/* FALLTHROUGH */
394 	case SIOCGLIFADDR:
395 		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
396 	}
397 
398 	/*
399 	 * Find address for this interface, if it exists.
400 	 *
401 	 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
402 	 * only, and used the first interface address as the target of other
403 	 * operations (without checking ifra_addr).  This was because netinet
404 	 * code/API assumed at most 1 interface address per interface.
405 	 * Since IPv6 allows a node to assign multiple addresses
406 	 * on a single interface, we almost always look and check the
407 	 * presence of ifra_addr, and reject invalid ones here.
408 	 * It also decreases duplicated code among SIOC*_IN6 operations.
409 	 */
410 	switch (cmd) {
411 	case SIOCAIFADDR_IN6:
412 	case SIOCSIFPHYADDR_IN6:
413 		sa6 = &ifra->ifra_addr;
414 		break;
415 	case SIOCSIFADDR_IN6:
416 	case SIOCGIFADDR_IN6:
417 	case SIOCSIFDSTADDR_IN6:
418 	case SIOCSIFNETMASK_IN6:
419 	case SIOCGIFDSTADDR_IN6:
420 	case SIOCGIFNETMASK_IN6:
421 	case SIOCDIFADDR_IN6:
422 	case SIOCGIFPSRCADDR_IN6:
423 	case SIOCGIFPDSTADDR_IN6:
424 	case SIOCGIFAFLAG_IN6:
425 	case SIOCSNDFLUSH_IN6:
426 	case SIOCSPFXFLUSH_IN6:
427 	case SIOCSRTRFLUSH_IN6:
428 	case SIOCGIFALIFETIME_IN6:
429 	case SIOCSIFALIFETIME_IN6:
430 	case SIOCGIFSTAT_IN6:
431 	case SIOCGIFSTAT_ICMP6:
432 		sa6 = &ifr->ifr_addr;
433 		break;
434 	default:
435 		sa6 = NULL;
436 		break;
437 	}
438 	if (sa6 && sa6->sin6_family == AF_INET6) {
439 		if (sa6->sin6_scope_id != 0)
440 			error = sa6_embedscope(sa6, 0);
441 		else
442 			error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
443 		if (error != 0)
444 			return (error);
445 		if (td != NULL && (error = prison_check_ip6(td->td_ucred,
446 		    &sa6->sin6_addr)) != 0)
447 			return (error);
448 		ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
449 	} else
450 		ia = NULL;
451 
452 	switch (cmd) {
453 	case SIOCSIFADDR_IN6:
454 	case SIOCSIFDSTADDR_IN6:
455 	case SIOCSIFNETMASK_IN6:
456 		/*
457 		 * Since IPv6 allows a node to assign multiple addresses
458 		 * on a single interface, SIOCSIFxxx ioctls are deprecated.
459 		 */
460 		/* we decided to obsolete this command (20000704) */
461 		error = EINVAL;
462 		goto out;
463 
464 	case SIOCDIFADDR_IN6:
465 		/*
466 		 * for IPv4, we look for existing in_ifaddr here to allow
467 		 * "ifconfig if0 delete" to remove the first IPv4 address on
468 		 * the interface.  For IPv6, as the spec allows multiple
469 		 * interface address from the day one, we consider "remove the
470 		 * first one" semantics to be not preferable.
471 		 */
472 		if (ia == NULL) {
473 			error = EADDRNOTAVAIL;
474 			goto out;
475 		}
476 		/* FALLTHROUGH */
477 	case SIOCAIFADDR_IN6:
478 		/*
479 		 * We always require users to specify a valid IPv6 address for
480 		 * the corresponding operation.
481 		 */
482 		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
483 		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
484 			error = EAFNOSUPPORT;
485 			goto out;
486 		}
487 
488 		if (td != NULL) {
489 			error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
490 			    PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
491 			if (error)
492 				goto out;
493 		}
494 		break;
495 
496 	case SIOCGIFADDR_IN6:
497 		/* This interface is basically deprecated. use SIOCGIFCONF. */
498 		/* FALLTHROUGH */
499 	case SIOCGIFAFLAG_IN6:
500 	case SIOCGIFNETMASK_IN6:
501 	case SIOCGIFDSTADDR_IN6:
502 	case SIOCGIFALIFETIME_IN6:
503 		/* must think again about its semantics */
504 		if (ia == NULL) {
505 			error = EADDRNOTAVAIL;
506 			goto out;
507 		}
508 		break;
509 
510 	case SIOCSIFALIFETIME_IN6:
511 	    {
512 		struct in6_addrlifetime *lt;
513 
514 		if (td != NULL) {
515 			error = priv_check(td, PRIV_NETINET_ALIFETIME6);
516 			if (error)
517 				goto out;
518 		}
519 		if (ia == NULL) {
520 			error = EADDRNOTAVAIL;
521 			goto out;
522 		}
523 		/* sanity for overflow - beware unsigned */
524 		lt = &ifr->ifr_ifru.ifru_lifetime;
525 		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
526 		    lt->ia6t_vltime + time_second < time_second) {
527 			error = EINVAL;
528 			goto out;
529 		}
530 		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
531 		    lt->ia6t_pltime + time_second < time_second) {
532 			error = EINVAL;
533 			goto out;
534 		}
535 		break;
536 	    }
537 	}
538 
539 	switch (cmd) {
540 	case SIOCGIFADDR_IN6:
541 		ifr->ifr_addr = ia->ia_addr;
542 		if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
543 			goto out;
544 		break;
545 
546 	case SIOCGIFDSTADDR_IN6:
547 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
548 			error = EINVAL;
549 			goto out;
550 		}
551 		/*
552 		 * XXX: should we check if ifa_dstaddr is NULL and return
553 		 * an error?
554 		 */
555 		ifr->ifr_dstaddr = ia->ia_dstaddr;
556 		if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
557 			goto out;
558 		break;
559 
560 	case SIOCGIFNETMASK_IN6:
561 		ifr->ifr_addr = ia->ia_prefixmask;
562 		break;
563 
564 	case SIOCGIFAFLAG_IN6:
565 		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
566 		break;
567 
568 	case SIOCGIFSTAT_IN6:
569 		if (ifp == NULL) {
570 			error = EINVAL;
571 			goto out;
572 		}
573 		COUNTER_ARRAY_COPY(((struct in6_ifextra *)
574 		    ifp->if_afdata[AF_INET6])->in6_ifstat,
575 		    &ifr->ifr_ifru.ifru_stat,
576 		    sizeof(struct in6_ifstat) / sizeof(uint64_t));
577 		break;
578 
579 	case SIOCGIFSTAT_ICMP6:
580 		if (ifp == NULL) {
581 			error = EINVAL;
582 			goto out;
583 		}
584 		COUNTER_ARRAY_COPY(((struct in6_ifextra *)
585 		    ifp->if_afdata[AF_INET6])->icmp6_ifstat,
586 		    &ifr->ifr_ifru.ifru_icmp6stat,
587 		    sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
588 		break;
589 
590 	case SIOCGIFALIFETIME_IN6:
591 		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
592 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
593 			time_t maxexpire;
594 			struct in6_addrlifetime *retlt =
595 			    &ifr->ifr_ifru.ifru_lifetime;
596 
597 			/*
598 			 * XXX: adjust expiration time assuming time_t is
599 			 * signed.
600 			 */
601 			maxexpire = (-1) &
602 			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
603 			if (ia->ia6_lifetime.ia6t_vltime <
604 			    maxexpire - ia->ia6_updatetime) {
605 				retlt->ia6t_expire = ia->ia6_updatetime +
606 				    ia->ia6_lifetime.ia6t_vltime;
607 			} else
608 				retlt->ia6t_expire = maxexpire;
609 		}
610 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
611 			time_t maxexpire;
612 			struct in6_addrlifetime *retlt =
613 			    &ifr->ifr_ifru.ifru_lifetime;
614 
615 			/*
616 			 * XXX: adjust expiration time assuming time_t is
617 			 * signed.
618 			 */
619 			maxexpire = (-1) &
620 			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
621 			if (ia->ia6_lifetime.ia6t_pltime <
622 			    maxexpire - ia->ia6_updatetime) {
623 				retlt->ia6t_preferred = ia->ia6_updatetime +
624 				    ia->ia6_lifetime.ia6t_pltime;
625 			} else
626 				retlt->ia6t_preferred = maxexpire;
627 		}
628 		break;
629 
630 	case SIOCSIFALIFETIME_IN6:
631 		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
632 		/* for sanity */
633 		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
634 			ia->ia6_lifetime.ia6t_expire =
635 				time_second + ia->ia6_lifetime.ia6t_vltime;
636 		} else
637 			ia->ia6_lifetime.ia6t_expire = 0;
638 		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
639 			ia->ia6_lifetime.ia6t_preferred =
640 				time_second + ia->ia6_lifetime.ia6t_pltime;
641 		} else
642 			ia->ia6_lifetime.ia6t_preferred = 0;
643 		break;
644 
645 	case SIOCAIFADDR_IN6:
646 	{
647 		int i;
648 		struct nd_prefixctl pr0;
649 		struct nd_prefix *pr;
650 
651 		/*
652 		 * first, make or update the interface address structure,
653 		 * and link it to the list.
654 		 */
655 		if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
656 			goto out;
657 		if (ia != NULL)
658 			ifa_free(&ia->ia_ifa);
659 		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
660 		    == NULL) {
661 			/*
662 			 * this can happen when the user specify the 0 valid
663 			 * lifetime.
664 			 */
665 			break;
666 		}
667 
668 		if (cmd == ocmd && ifra->ifra_vhid > 0) {
669 			if (carp_attach_p != NULL)
670 				error = (*carp_attach_p)(&ia->ia_ifa,
671 				    ifra->ifra_vhid);
672 			else
673 				error = EPROTONOSUPPORT;
674 			if (error)
675 				goto out;
676 			else
677 				carp_attached = 1;
678 		}
679 
680 		/*
681 		 * then, make the prefix on-link on the interface.
682 		 * XXX: we'd rather create the prefix before the address, but
683 		 * we need at least one address to install the corresponding
684 		 * interface route, so we configure the address first.
685 		 */
686 
687 		/*
688 		 * convert mask to prefix length (prefixmask has already
689 		 * been validated in in6_update_ifa().
690 		 */
691 		bzero(&pr0, sizeof(pr0));
692 		pr0.ndpr_ifp = ifp;
693 		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
694 		    NULL);
695 		if (pr0.ndpr_plen == 128) {
696 			break;	/* we don't need to install a host route. */
697 		}
698 		pr0.ndpr_prefix = ifra->ifra_addr;
699 		/* apply the mask for safety. */
700 		for (i = 0; i < 4; i++) {
701 			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
702 			    ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
703 		}
704 		/*
705 		 * XXX: since we don't have an API to set prefix (not address)
706 		 * lifetimes, we just use the same lifetimes as addresses.
707 		 * The (temporarily) installed lifetimes can be overridden by
708 		 * later advertised RAs (when accept_rtadv is non 0), which is
709 		 * an intended behavior.
710 		 */
711 		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
712 		pr0.ndpr_raf_auto =
713 		    ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
714 		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
715 		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
716 
717 		/* add the prefix if not yet. */
718 		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
719 			/*
720 			 * nd6_prelist_add will install the corresponding
721 			 * interface route.
722 			 */
723 			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
724 				if (carp_attached)
725 					(*carp_detach_p)(&ia->ia_ifa);
726 				goto out;
727 			}
728 			if (pr == NULL) {
729 				if (carp_attached)
730 					(*carp_detach_p)(&ia->ia_ifa);
731 				log(LOG_ERR, "nd6_prelist_add succeeded but "
732 				    "no prefix\n");
733 				error = EINVAL;
734 				goto out;
735 			}
736 		}
737 
738 		/* relate the address to the prefix */
739 		if (ia->ia6_ndpr == NULL) {
740 			ia->ia6_ndpr = pr;
741 			pr->ndpr_refcnt++;
742 
743 			/*
744 			 * If this is the first autoconf address from the
745 			 * prefix, create a temporary address as well
746 			 * (when required).
747 			 */
748 			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
749 			    V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
750 				int e;
751 				if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
752 					log(LOG_NOTICE, "in6_control: failed "
753 					    "to create a temporary address, "
754 					    "errno=%d\n", e);
755 				}
756 			}
757 		}
758 
759 		/*
760 		 * this might affect the status of autoconfigured addresses,
761 		 * that is, this address might make other addresses detached.
762 		 */
763 		pfxlist_onlink_check();
764 		if (error == 0 && ia) {
765 			if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
766 				/*
767 				 * Try to clear the flag when a new
768 				 * IPv6 address is added onto an
769 				 * IFDISABLED interface and it
770 				 * succeeds.
771 				 */
772 				struct in6_ndireq nd;
773 
774 				memset(&nd, 0, sizeof(nd));
775 				nd.ndi.flags = ND_IFINFO(ifp)->flags;
776 				nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
777 				if (nd6_ioctl(SIOCSIFINFO_FLAGS,
778 				    (caddr_t)&nd, ifp) < 0)
779 					log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
780 					    "SIOCSIFINFO_FLAGS for -ifdisabled "
781 					    "failed.");
782 				/*
783 				 * Ignore failure of clearing the flag
784 				 * intentionally.  The failure means
785 				 * address duplication was detected.
786 				 */
787 			}
788 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
789 		}
790 		break;
791 	}
792 
793 	case SIOCDIFADDR_IN6:
794 	{
795 		struct nd_prefix *pr;
796 
797 		/*
798 		 * If the address being deleted is the only one that owns
799 		 * the corresponding prefix, expire the prefix as well.
800 		 * XXX: theoretically, we don't have to worry about such
801 		 * relationship, since we separate the address management
802 		 * and the prefix management.  We do this, however, to provide
803 		 * as much backward compatibility as possible in terms of
804 		 * the ioctl operation.
805 		 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
806 		 */
807 		pr = ia->ia6_ndpr;
808 		in6_purgeaddr(&ia->ia_ifa);
809 		if (pr && pr->ndpr_refcnt == 0)
810 			prelist_remove(pr);
811 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
812 		break;
813 	}
814 
815 	default:
816 		if (ifp == NULL || ifp->if_ioctl == 0) {
817 			error = EOPNOTSUPP;
818 			goto out;
819 		}
820 		error = (*ifp->if_ioctl)(ifp, cmd, data);
821 		goto out;
822 	}
823 
824 	error = 0;
825 out:
826 	if (ia != NULL)
827 		ifa_free(&ia->ia_ifa);
828 	return (error);
829 }
830 
831 
832 /*
833  * Join necessary multicast groups.  Factored out from in6_update_ifa().
834  * This entire work should only be done once, for the default FIB.
835  */
836 static int
837 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
838     struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
839 {
840 	char ip6buf[INET6_ADDRSTRLEN];
841 	struct sockaddr_in6 mltaddr, mltmask;
842 	struct in6_addr llsol;
843 	struct in6_multi_mship *imm;
844 	struct rtentry *rt;
845 	int delay, error;
846 
847 	KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
848 
849 	/* Join solicited multicast addr for new host id. */
850 	bzero(&llsol, sizeof(struct in6_addr));
851 	llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
852 	llsol.s6_addr32[1] = 0;
853 	llsol.s6_addr32[2] = htonl(1);
854 	llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
855 	llsol.s6_addr8[12] = 0xff;
856 	if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
857 		/* XXX: should not happen */
858 		log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
859 		goto cleanup;
860 	}
861 	delay = 0;
862 	if ((flags & IN6_IFAUPDATE_DADDELAY)) {
863 		/*
864 		 * We need a random delay for DAD on the address being
865 		 * configured.  It also means delaying transmission of the
866 		 * corresponding MLD report to avoid report collision.
867 		 * [RFC 4861, Section 6.3.7]
868 		 */
869 		delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
870 	}
871 	imm = in6_joingroup(ifp, &llsol, &error, delay);
872 	if (imm == NULL) {
873 		nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
874 		    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &llsol),
875 		    if_name(ifp), error));
876 		goto cleanup;
877 	}
878 	LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
879 	*in6m_sol = imm->i6mm_maddr;
880 
881 	bzero(&mltmask, sizeof(mltmask));
882 	mltmask.sin6_len = sizeof(struct sockaddr_in6);
883 	mltmask.sin6_family = AF_INET6;
884 	mltmask.sin6_addr = in6mask32;
885 #define	MLTMASK_LEN  4	/* mltmask's masklen (=32bit=4octet) */
886 
887 	/*
888 	 * Join link-local all-nodes address.
889 	 */
890 	bzero(&mltaddr, sizeof(mltaddr));
891 	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
892 	mltaddr.sin6_family = AF_INET6;
893 	mltaddr.sin6_addr = in6addr_linklocal_allnodes;
894 	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
895 		goto cleanup; /* XXX: should not fail */
896 
897 	/*
898 	 * XXX: do we really need this automatic routes?  We should probably
899 	 * reconsider this stuff.  Most applications actually do not need the
900 	 * routes, since they usually specify the outgoing interface.
901 	 */
902 	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
903 	if (rt != NULL) {
904 		/* XXX: only works in !SCOPEDROUTING case. */
905 		if (memcmp(&mltaddr.sin6_addr,
906 		    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
907 		    MLTMASK_LEN)) {
908 			RTFREE_LOCKED(rt);
909 			rt = NULL;
910 		}
911 	}
912 	if (rt == NULL) {
913 		error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
914 		    (struct sockaddr *)&ia->ia_addr,
915 		    (struct sockaddr *)&mltmask, RTF_UP,
916 		    (struct rtentry **)0, RT_DEFAULT_FIB);
917 		if (error)
918 			goto cleanup;
919 	} else
920 		RTFREE_LOCKED(rt);
921 
922 	imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
923 	if (imm == NULL) {
924 		nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
925 		    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
926 		    &mltaddr.sin6_addr), if_name(ifp), error));
927 		goto cleanup;
928 	}
929 	LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
930 
931 	/*
932 	 * Join node information group address.
933 	 */
934 	delay = 0;
935 	if ((flags & IN6_IFAUPDATE_DADDELAY)) {
936 		/*
937 		 * The spec does not say anything about delay for this group,
938 		 * but the same logic should apply.
939 		 */
940 		delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
941 	}
942 	if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
943 		/* XXX jinmei */
944 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
945 		if (imm == NULL)
946 			nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
947 			    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
948 			    &mltaddr.sin6_addr), if_name(ifp), error));
949 			/* XXX not very fatal, go on... */
950 		else
951 			LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
952 	}
953 	if (V_icmp6_nodeinfo_oldmcprefix &&
954 	     in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
955 		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
956 		if (imm == NULL)
957 			nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
958 			    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
959 			    &mltaddr.sin6_addr), if_name(ifp), error));
960 			/* XXX not very fatal, go on... */
961 		else
962 			LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
963 	}
964 
965 	/*
966 	 * Join interface-local all-nodes address.
967 	 * (ff01::1%ifN, and ff01::%ifN/32)
968 	 */
969 	mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
970 	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
971 		goto cleanup; /* XXX: should not fail */
972 	/* XXX: again, do we really need the route? */
973 	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
974 	if (rt != NULL) {
975 		if (memcmp(&mltaddr.sin6_addr,
976 		    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
977 		    MLTMASK_LEN)) {
978 			RTFREE_LOCKED(rt);
979 			rt = NULL;
980 		}
981 	}
982 	if (rt == NULL) {
983 		error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
984 		    (struct sockaddr *)&ia->ia_addr,
985 		    (struct sockaddr *)&mltmask, RTF_UP,
986 		    (struct rtentry **)0, RT_DEFAULT_FIB);
987 		if (error)
988 			goto cleanup;
989 	} else
990 		RTFREE_LOCKED(rt);
991 
992 	imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
993 	if (imm == NULL) {
994 		nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
995 		    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
996 		    &mltaddr.sin6_addr), if_name(ifp), error));
997 		goto cleanup;
998 	}
999 	LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1000 #undef	MLTMASK_LEN
1001 
1002 cleanup:
1003 	return (error);
1004 }
1005 
1006 /*
1007  * Update parameters of an IPv6 interface address.
1008  * If necessary, a new entry is created and linked into address chains.
1009  * This function is separated from in6_control().
1010  */
1011 int
1012 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1013     struct in6_ifaddr *ia, int flags)
1014 {
1015 	int error = 0, hostIsNew = 0, plen = -1;
1016 	struct sockaddr_in6 dst6;
1017 	struct in6_addrlifetime *lt;
1018 	struct in6_multi *in6m_sol;
1019 	int delay;
1020 	char ip6buf[INET6_ADDRSTRLEN];
1021 
1022 	/* Validate parameters */
1023 	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
1024 		return (EINVAL);
1025 
1026 	/*
1027 	 * The destination address for a p2p link must have a family
1028 	 * of AF_UNSPEC or AF_INET6.
1029 	 */
1030 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1031 	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
1032 	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
1033 		return (EAFNOSUPPORT);
1034 	/*
1035 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
1036 	 * does not carry fields other than sin6_len.
1037 	 */
1038 	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
1039 		return (EINVAL);
1040 	/*
1041 	 * Because the IPv6 address architecture is classless, we require
1042 	 * users to specify a (non 0) prefix length (mask) for a new address.
1043 	 * We also require the prefix (when specified) mask is valid, and thus
1044 	 * reject a non-consecutive mask.
1045 	 */
1046 	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
1047 		return (EINVAL);
1048 	if (ifra->ifra_prefixmask.sin6_len != 0) {
1049 		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
1050 		    (u_char *)&ifra->ifra_prefixmask +
1051 		    ifra->ifra_prefixmask.sin6_len);
1052 		if (plen <= 0)
1053 			return (EINVAL);
1054 	} else {
1055 		/*
1056 		 * In this case, ia must not be NULL.  We just use its prefix
1057 		 * length.
1058 		 */
1059 		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1060 	}
1061 	/*
1062 	 * If the destination address on a p2p interface is specified,
1063 	 * and the address is a scoped one, validate/set the scope
1064 	 * zone identifier.
1065 	 */
1066 	dst6 = ifra->ifra_dstaddr;
1067 	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
1068 	    (dst6.sin6_family == AF_INET6)) {
1069 		struct in6_addr in6_tmp;
1070 		u_int32_t zoneid;
1071 
1072 		in6_tmp = dst6.sin6_addr;
1073 		if (in6_setscope(&in6_tmp, ifp, &zoneid))
1074 			return (EINVAL); /* XXX: should be impossible */
1075 
1076 		if (dst6.sin6_scope_id != 0) {
1077 			if (dst6.sin6_scope_id != zoneid)
1078 				return (EINVAL);
1079 		} else		/* user omit to specify the ID. */
1080 			dst6.sin6_scope_id = zoneid;
1081 
1082 		/* convert into the internal form */
1083 		if (sa6_embedscope(&dst6, 0))
1084 			return (EINVAL); /* XXX: should be impossible */
1085 	}
1086 	/*
1087 	 * The destination address can be specified only for a p2p or a
1088 	 * loopback interface.  If specified, the corresponding prefix length
1089 	 * must be 128.
1090 	 */
1091 	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
1092 		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1093 			/* XXX: noisy message */
1094 			nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1095 			    "be specified for a p2p or a loopback IF only\n"));
1096 			return (EINVAL);
1097 		}
1098 		if (plen != 128) {
1099 			nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1100 			    "be 128 when dstaddr is specified\n"));
1101 			return (EINVAL);
1102 		}
1103 	}
1104 	/* lifetime consistency check */
1105 	lt = &ifra->ifra_lifetime;
1106 	if (lt->ia6t_pltime > lt->ia6t_vltime)
1107 		return (EINVAL);
1108 	if (lt->ia6t_vltime == 0) {
1109 		/*
1110 		 * the following log might be noisy, but this is a typical
1111 		 * configuration mistake or a tool's bug.
1112 		 */
1113 		nd6log((LOG_INFO,
1114 		    "in6_update_ifa: valid lifetime is 0 for %s\n",
1115 		    ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1116 
1117 		if (ia == NULL)
1118 			return (0); /* there's nothing to do */
1119 	}
1120 
1121 	/*
1122 	 * If this is a new address, allocate a new ifaddr and link it
1123 	 * into chains.
1124 	 */
1125 	if (ia == NULL) {
1126 		hostIsNew = 1;
1127 		/*
1128 		 * When in6_update_ifa() is called in a process of a received
1129 		 * RA, it is called under an interrupt context.  So, we should
1130 		 * call malloc with M_NOWAIT.
1131 		 */
1132 		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
1133 		    M_NOWAIT);
1134 		if (ia == NULL)
1135 			return (ENOBUFS);
1136 		bzero((caddr_t)ia, sizeof(*ia));
1137 		ifa_init(&ia->ia_ifa);
1138 		LIST_INIT(&ia->ia6_memberships);
1139 		/* Initialize the address and masks, and put time stamp */
1140 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1141 		ia->ia_addr.sin6_family = AF_INET6;
1142 		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1143 		ia->ia6_createtime = time_second;
1144 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1145 			/*
1146 			 * XXX: some functions expect that ifa_dstaddr is not
1147 			 * NULL for p2p interfaces.
1148 			 */
1149 			ia->ia_ifa.ifa_dstaddr =
1150 			    (struct sockaddr *)&ia->ia_dstaddr;
1151 		} else {
1152 			ia->ia_ifa.ifa_dstaddr = NULL;
1153 		}
1154 		ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1155 		ia->ia_ifp = ifp;
1156 		ifa_ref(&ia->ia_ifa);			/* if_addrhead */
1157 		IF_ADDR_WLOCK(ifp);
1158 		TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1159 		IF_ADDR_WUNLOCK(ifp);
1160 
1161 		ifa_ref(&ia->ia_ifa);			/* in6_ifaddrhead */
1162 		IN6_IFADDR_WLOCK();
1163 		TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1164 		LIST_INSERT_HEAD(IN6ADDR_HASH(&ifra->ifra_addr.sin6_addr),
1165 		    ia, ia6_hash);
1166 		IN6_IFADDR_WUNLOCK();
1167 	}
1168 
1169 	/* update timestamp */
1170 	ia->ia6_updatetime = time_second;
1171 
1172 	/* set prefix mask */
1173 	if (ifra->ifra_prefixmask.sin6_len) {
1174 		/*
1175 		 * We prohibit changing the prefix length of an existing
1176 		 * address, because
1177 		 * + such an operation should be rare in IPv6, and
1178 		 * + the operation would confuse prefix management.
1179 		 */
1180 		if (ia->ia_prefixmask.sin6_len &&
1181 		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1182 			nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
1183 			    " existing (%s) address should not be changed\n",
1184 			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1185 			error = EINVAL;
1186 			goto unlink;
1187 		}
1188 		ia->ia_prefixmask = ifra->ifra_prefixmask;
1189 		ia->ia_prefixmask.sin6_family = AF_INET6;
1190 	}
1191 
1192 	/*
1193 	 * If a new destination address is specified, scrub the old one and
1194 	 * install the new destination.  Note that the interface must be
1195 	 * p2p or loopback (see the check above.)
1196 	 */
1197 	if (dst6.sin6_family == AF_INET6 &&
1198 	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1199 		int e;
1200 
1201 		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1202 		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1203 			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1204 			    "a route to the old destination: %s\n",
1205 			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1206 			/* proceed anyway... */
1207 		} else
1208 			ia->ia_flags &= ~IFA_ROUTE;
1209 		ia->ia_dstaddr = dst6;
1210 	}
1211 
1212 	/*
1213 	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
1214 	 * to see if the address is deprecated or invalidated, but initialize
1215 	 * these members for applications.
1216 	 */
1217 	ia->ia6_lifetime = ifra->ifra_lifetime;
1218 	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1219 		ia->ia6_lifetime.ia6t_expire =
1220 		    time_second + ia->ia6_lifetime.ia6t_vltime;
1221 	} else
1222 		ia->ia6_lifetime.ia6t_expire = 0;
1223 	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1224 		ia->ia6_lifetime.ia6t_preferred =
1225 		    time_second + ia->ia6_lifetime.ia6t_pltime;
1226 	} else
1227 		ia->ia6_lifetime.ia6t_preferred = 0;
1228 
1229 	/* reset the interface and routing table appropriately. */
1230 	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1231 		goto unlink;
1232 
1233 	/*
1234 	 * configure address flags.
1235 	 */
1236 	ia->ia6_flags = ifra->ifra_flags;
1237 	/*
1238 	 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1239 	 * userland, make it deprecated.
1240 	 */
1241 	if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1242 		ia->ia6_lifetime.ia6t_pltime = 0;
1243 		ia->ia6_lifetime.ia6t_preferred = time_second;
1244 	}
1245 	/*
1246 	 * Make the address tentative before joining multicast addresses,
1247 	 * so that corresponding MLD responses would not have a tentative
1248 	 * source address.
1249 	 */
1250 	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/* safety */
1251 	if (hostIsNew && in6if_do_dad(ifp))
1252 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
1253 
1254 	/* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
1255 	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1256 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
1257 
1258 	/*
1259 	 * We are done if we have simply modified an existing address.
1260 	 */
1261 	if (!hostIsNew)
1262 		return (error);
1263 
1264 	/*
1265 	 * Beyond this point, we should call in6_purgeaddr upon an error,
1266 	 * not just go to unlink.
1267 	 */
1268 
1269 	/* Join necessary multicast groups. */
1270 	in6m_sol = NULL;
1271 	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1272 		error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1273 		if (error)
1274 			goto cleanup;
1275 	}
1276 
1277 	/*
1278 	 * Perform DAD, if needed.
1279 	 * XXX It may be of use, if we can administratively disable DAD.
1280 	 */
1281 	if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1282 	    (ia->ia6_flags & IN6_IFF_TENTATIVE))
1283 	{
1284 		int mindelay, maxdelay;
1285 
1286 		delay = 0;
1287 		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1288 			/*
1289 			 * We need to impose a delay before sending an NS
1290 			 * for DAD.  Check if we also needed a delay for the
1291 			 * corresponding MLD message.  If we did, the delay
1292 			 * should be larger than the MLD delay (this could be
1293 			 * relaxed a bit, but this simple logic is at least
1294 			 * safe).
1295 			 * XXX: Break data hiding guidelines and look at
1296 			 * state for the solicited multicast group.
1297 			 */
1298 			mindelay = 0;
1299 			if (in6m_sol != NULL &&
1300 			    in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1301 				mindelay = in6m_sol->in6m_timer;
1302 			}
1303 			maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1304 			if (maxdelay - mindelay == 0)
1305 				delay = 0;
1306 			else {
1307 				delay =
1308 				    (arc4random() % (maxdelay - mindelay)) +
1309 				    mindelay;
1310 			}
1311 		}
1312 		nd6_dad_start((struct ifaddr *)ia, delay);
1313 	}
1314 
1315 	KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1316 	ifa_free(&ia->ia_ifa);
1317 	return (error);
1318 
1319   unlink:
1320 	/*
1321 	 * XXX: if a change of an existing address failed, keep the entry
1322 	 * anyway.
1323 	 */
1324 	if (hostIsNew) {
1325 		in6_unlink_ifa(ia, ifp);
1326 		ifa_free(&ia->ia_ifa);
1327 	}
1328 	return (error);
1329 
1330   cleanup:
1331 	KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1332 	ifa_free(&ia->ia_ifa);
1333 	in6_purgeaddr(&ia->ia_ifa);
1334 	return error;
1335 }
1336 
1337 /*
1338  * Leave multicast groups.  Factored out from in6_purgeaddr().
1339  * This entire work should only be done once, for the default FIB.
1340  */
1341 static int
1342 in6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0)
1343 {
1344 	struct sockaddr_in6 mltaddr, mltmask;
1345 	struct in6_multi_mship *imm;
1346 	struct rtentry *rt;
1347 	struct sockaddr_in6 sin6;
1348 	int error;
1349 
1350 	/*
1351 	 * Leave from multicast groups we have joined for the interface.
1352 	 */
1353 	while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1354 		LIST_REMOVE(imm, i6mm_chain);
1355 		in6_leavegroup(imm);
1356 	}
1357 
1358 	/*
1359 	 * Remove the link-local all-nodes address.
1360 	 */
1361 	bzero(&mltmask, sizeof(mltmask));
1362 	mltmask.sin6_len = sizeof(struct sockaddr_in6);
1363 	mltmask.sin6_family = AF_INET6;
1364 	mltmask.sin6_addr = in6mask32;
1365 
1366 	bzero(&mltaddr, sizeof(mltaddr));
1367 	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1368 	mltaddr.sin6_family = AF_INET6;
1369 	mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1370 
1371 	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1372 		return (error);
1373 
1374 	/*
1375 	 * As for the mltaddr above, proactively prepare the sin6 to avoid
1376 	 * rtentry un- and re-locking.
1377 	 */
1378 	if (ifa0 != NULL) {
1379 		bzero(&sin6, sizeof(sin6));
1380 		sin6.sin6_len = sizeof(sin6);
1381 		sin6.sin6_family = AF_INET6;
1382 		memcpy(&sin6.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1383 		    sizeof(sin6.sin6_addr));
1384 		error = in6_setscope(&sin6.sin6_addr, ifa0->ifa_ifp, NULL);
1385 		if (error != 0)
1386 			return (error);
1387 	}
1388 
1389 	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1390 	if (rt != NULL && rt->rt_gateway != NULL &&
1391 	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1392 		    &ia->ia_addr.sin6_addr,
1393 		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1394 		/*
1395 		 * If no more IPv6 address exists on this interface then
1396 		 * remove the multicast address route.
1397 		 */
1398 		if (ifa0 == NULL) {
1399 			memcpy(&mltaddr.sin6_addr,
1400 			    &satosin6(rt_key(rt))->sin6_addr,
1401 			    sizeof(mltaddr.sin6_addr));
1402 			RTFREE_LOCKED(rt);
1403 			error = in6_rtrequest(RTM_DELETE,
1404 			    (struct sockaddr *)&mltaddr,
1405 			    (struct sockaddr *)&ia->ia_addr,
1406 			    (struct sockaddr *)&mltmask, RTF_UP,
1407 			    (struct rtentry **)0, RT_DEFAULT_FIB);
1408 			if (error)
1409 				log(LOG_INFO, "%s: link-local all-nodes "
1410 				    "multicast address deletion error\n",
1411 				    __func__);
1412 		} else {
1413 			/*
1414 			 * Replace the gateway of the route.
1415 			 */
1416 			memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1417 			RTFREE_LOCKED(rt);
1418 		}
1419 	} else {
1420 		if (rt != NULL)
1421 			RTFREE_LOCKED(rt);
1422 	}
1423 
1424 	/*
1425 	 * Remove the node-local all-nodes address.
1426 	 */
1427 	mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1428 	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1429 		return (error);
1430 
1431 	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1432 	if (rt != NULL && rt->rt_gateway != NULL &&
1433 	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1434 		    &ia->ia_addr.sin6_addr,
1435 		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1436 		/*
1437 		 * If no more IPv6 address exists on this interface then
1438 		 * remove the multicast address route.
1439 		 */
1440 		if (ifa0 == NULL) {
1441 			memcpy(&mltaddr.sin6_addr,
1442 			    &satosin6(rt_key(rt))->sin6_addr,
1443 			    sizeof(mltaddr.sin6_addr));
1444 
1445 			RTFREE_LOCKED(rt);
1446 			error = in6_rtrequest(RTM_DELETE,
1447 			    (struct sockaddr *)&mltaddr,
1448 			    (struct sockaddr *)&ia->ia_addr,
1449 			    (struct sockaddr *)&mltmask, RTF_UP,
1450 			    (struct rtentry **)0, RT_DEFAULT_FIB);
1451 			if (error)
1452 				log(LOG_INFO, "%s: node-local all-nodes"
1453 				    "multicast address deletion error\n",
1454 				    __func__);
1455 		} else {
1456 			/*
1457 			 * Replace the gateway of the route.
1458 			 */
1459 			memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1460 			RTFREE_LOCKED(rt);
1461 		}
1462 	} else {
1463 		if (rt != NULL)
1464 			RTFREE_LOCKED(rt);
1465 	}
1466 
1467 	return (0);
1468 }
1469 
1470 void
1471 in6_purgeaddr(struct ifaddr *ifa)
1472 {
1473 	struct ifnet *ifp = ifa->ifa_ifp;
1474 	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1475 	int plen, error;
1476 	struct ifaddr *ifa0;
1477 
1478 	if (ifa->ifa_carp)
1479 		(*carp_detach_p)(ifa);
1480 
1481 	/*
1482 	 * find another IPv6 address as the gateway for the
1483 	 * link-local and node-local all-nodes multicast
1484 	 * address routes
1485 	 */
1486 	IF_ADDR_RLOCK(ifp);
1487 	TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1488 		if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1489 		    memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1490 		    &ia->ia_addr.sin6_addr, sizeof(struct in6_addr)) == 0)
1491 			continue;
1492 		else
1493 			break;
1494 	}
1495 	if (ifa0 != NULL)
1496 		ifa_ref(ifa0);
1497 	IF_ADDR_RUNLOCK(ifp);
1498 
1499 	/*
1500 	 * Remove the loopback route to the interface address.
1501 	 * The check for the current setting of "nd6_useloopback"
1502 	 * is not needed.
1503 	 */
1504 	if (ia->ia_flags & IFA_RTSELF) {
1505 		error = ifa_del_loopback_route((struct ifaddr *)ia,
1506 		    (struct sockaddr *)&ia->ia_addr);
1507 		if (error == 0)
1508 			ia->ia_flags &= ~IFA_RTSELF;
1509 	}
1510 
1511 	/* stop DAD processing */
1512 	nd6_dad_stop(ifa);
1513 
1514 	/* Remove local address entry from lltable. */
1515 	in6_ifremloop(ifa);
1516 
1517 	/* Leave multicast groups. */
1518 	error = in6_purgeaddr_mc(ifp, ia, ifa0);
1519 
1520 	if (ifa0 != NULL)
1521 		ifa_free(ifa0);
1522 
1523 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1524 	if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1525 		error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1526 		    (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0);
1527 		if (error != 0)
1528 			log(LOG_INFO, "%s: err=%d, destination address delete "
1529 			    "failed\n", __func__, error);
1530 		ia->ia_flags &= ~IFA_ROUTE;
1531 	}
1532 
1533 	in6_unlink_ifa(ia, ifp);
1534 }
1535 
1536 static void
1537 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1538 {
1539 
1540 	IF_ADDR_WLOCK(ifp);
1541 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1542 	IF_ADDR_WUNLOCK(ifp);
1543 	ifa_free(&ia->ia_ifa);			/* if_addrhead */
1544 
1545 	/*
1546 	 * Defer the release of what might be the last reference to the
1547 	 * in6_ifaddr so that it can't be freed before the remainder of the
1548 	 * cleanup.
1549 	 */
1550 	IN6_IFADDR_WLOCK();
1551 	TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1552 	LIST_REMOVE(ia, ia6_hash);
1553 	IN6_IFADDR_WUNLOCK();
1554 
1555 	/*
1556 	 * Release the reference to the base prefix.  There should be a
1557 	 * positive reference.
1558 	 */
1559 	if (ia->ia6_ndpr == NULL) {
1560 		nd6log((LOG_NOTICE,
1561 		    "in6_unlink_ifa: autoconf'ed address "
1562 		    "%p has no prefix\n", ia));
1563 	} else {
1564 		ia->ia6_ndpr->ndpr_refcnt--;
1565 		ia->ia6_ndpr = NULL;
1566 	}
1567 
1568 	/*
1569 	 * Also, if the address being removed is autoconf'ed, call
1570 	 * pfxlist_onlink_check() since the release might affect the status of
1571 	 * other (detached) addresses.
1572 	 */
1573 	if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1574 		pfxlist_onlink_check();
1575 	}
1576 	ifa_free(&ia->ia_ifa);			/* in6_ifaddrhead */
1577 }
1578 
1579 void
1580 in6_purgeif(struct ifnet *ifp)
1581 {
1582 	struct ifaddr *ifa, *nifa;
1583 
1584 	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1585 		if (ifa->ifa_addr->sa_family != AF_INET6)
1586 			continue;
1587 		in6_purgeaddr(ifa);
1588 	}
1589 
1590 	in6_ifdetach(ifp);
1591 }
1592 
1593 /*
1594  * SIOC[GAD]LIFADDR.
1595  *	SIOCGLIFADDR: get first address. (?)
1596  *	SIOCGLIFADDR with IFLR_PREFIX:
1597  *		get first address that matches the specified prefix.
1598  *	SIOCALIFADDR: add the specified address.
1599  *	SIOCALIFADDR with IFLR_PREFIX:
1600  *		add the specified prefix, filling hostid part from
1601  *		the first link-local address.  prefixlen must be <= 64.
1602  *	SIOCDLIFADDR: delete the specified address.
1603  *	SIOCDLIFADDR with IFLR_PREFIX:
1604  *		delete the first address that matches the specified prefix.
1605  * return values:
1606  *	EINVAL on invalid parameters
1607  *	EADDRNOTAVAIL on prefix match failed/specified address not found
1608  *	other values may be returned from in6_ioctl()
1609  *
1610  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1611  * this is to accomodate address naming scheme other than RFC2374,
1612  * in the future.
1613  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1614  * address encoding scheme. (see figure on page 8)
1615  */
1616 static int
1617 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1618     struct ifnet *ifp, struct thread *td)
1619 {
1620 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1621 	struct ifaddr *ifa;
1622 	struct sockaddr *sa;
1623 
1624 	/* sanity checks */
1625 	if (!data || !ifp) {
1626 		panic("invalid argument to in6_lifaddr_ioctl");
1627 		/* NOTREACHED */
1628 	}
1629 
1630 	switch (cmd) {
1631 	case SIOCGLIFADDR:
1632 		/* address must be specified on GET with IFLR_PREFIX */
1633 		if ((iflr->flags & IFLR_PREFIX) == 0)
1634 			break;
1635 		/* FALLTHROUGH */
1636 	case SIOCALIFADDR:
1637 	case SIOCDLIFADDR:
1638 		/* address must be specified on ADD and DELETE */
1639 		sa = (struct sockaddr *)&iflr->addr;
1640 		if (sa->sa_family != AF_INET6)
1641 			return EINVAL;
1642 		if (sa->sa_len != sizeof(struct sockaddr_in6))
1643 			return EINVAL;
1644 		/* XXX need improvement */
1645 		sa = (struct sockaddr *)&iflr->dstaddr;
1646 		if (sa->sa_family && sa->sa_family != AF_INET6)
1647 			return EINVAL;
1648 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1649 			return EINVAL;
1650 		break;
1651 	default: /* shouldn't happen */
1652 #if 0
1653 		panic("invalid cmd to in6_lifaddr_ioctl");
1654 		/* NOTREACHED */
1655 #else
1656 		return EOPNOTSUPP;
1657 #endif
1658 	}
1659 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1660 		return EINVAL;
1661 
1662 	switch (cmd) {
1663 	case SIOCALIFADDR:
1664 	    {
1665 		struct in6_aliasreq ifra;
1666 		struct in6_addr *hostid = NULL;
1667 		int prefixlen;
1668 
1669 		ifa = NULL;
1670 		if ((iflr->flags & IFLR_PREFIX) != 0) {
1671 			struct sockaddr_in6 *sin6;
1672 
1673 			/*
1674 			 * hostid is to fill in the hostid part of the
1675 			 * address.  hostid points to the first link-local
1676 			 * address attached to the interface.
1677 			 */
1678 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1679 			if (!ifa)
1680 				return EADDRNOTAVAIL;
1681 			hostid = IFA_IN6(ifa);
1682 
1683 			/* prefixlen must be <= 64. */
1684 			if (64 < iflr->prefixlen) {
1685 				if (ifa != NULL)
1686 					ifa_free(ifa);
1687 				return EINVAL;
1688 			}
1689 			prefixlen = iflr->prefixlen;
1690 
1691 			/* hostid part must be zero. */
1692 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1693 			if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1694 			    sin6->sin6_addr.s6_addr32[3] != 0) {
1695 				if (ifa != NULL)
1696 					ifa_free(ifa);
1697 				return EINVAL;
1698 			}
1699 		} else
1700 			prefixlen = iflr->prefixlen;
1701 
1702 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1703 		bzero(&ifra, sizeof(ifra));
1704 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1705 
1706 		bcopy(&iflr->addr, &ifra.ifra_addr,
1707 		    ((struct sockaddr *)&iflr->addr)->sa_len);
1708 		if (hostid) {
1709 			/* fill in hostid part */
1710 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1711 			    hostid->s6_addr32[2];
1712 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1713 			    hostid->s6_addr32[3];
1714 		}
1715 
1716 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1717 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1718 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1719 			if (hostid) {
1720 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1721 				    hostid->s6_addr32[2];
1722 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1723 				    hostid->s6_addr32[3];
1724 			}
1725 		}
1726 		if (ifa != NULL)
1727 			ifa_free(ifa);
1728 
1729 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1730 		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1731 
1732 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1733 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1734 	    }
1735 	case SIOCGLIFADDR:
1736 	case SIOCDLIFADDR:
1737 	    {
1738 		struct in6_ifaddr *ia;
1739 		struct in6_addr mask, candidate, match;
1740 		struct sockaddr_in6 *sin6;
1741 		int cmp;
1742 
1743 		bzero(&mask, sizeof(mask));
1744 		if (iflr->flags & IFLR_PREFIX) {
1745 			/* lookup a prefix rather than address. */
1746 			in6_prefixlen2mask(&mask, iflr->prefixlen);
1747 
1748 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1749 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
1750 			match.s6_addr32[0] &= mask.s6_addr32[0];
1751 			match.s6_addr32[1] &= mask.s6_addr32[1];
1752 			match.s6_addr32[2] &= mask.s6_addr32[2];
1753 			match.s6_addr32[3] &= mask.s6_addr32[3];
1754 
1755 			/* if you set extra bits, that's wrong */
1756 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1757 				return EINVAL;
1758 
1759 			cmp = 1;
1760 		} else {
1761 			if (cmd == SIOCGLIFADDR) {
1762 				/* on getting an address, take the 1st match */
1763 				cmp = 0;	/* XXX */
1764 			} else {
1765 				/* on deleting an address, do exact match */
1766 				in6_prefixlen2mask(&mask, 128);
1767 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
1768 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
1769 
1770 				cmp = 1;
1771 			}
1772 		}
1773 
1774 		IF_ADDR_RLOCK(ifp);
1775 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1776 			if (ifa->ifa_addr->sa_family != AF_INET6)
1777 				continue;
1778 			if (!cmp)
1779 				break;
1780 
1781 			/*
1782 			 * XXX: this is adhoc, but is necessary to allow
1783 			 * a user to specify fe80::/64 (not /10) for a
1784 			 * link-local address.
1785 			 */
1786 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1787 			in6_clearscope(&candidate);
1788 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
1789 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
1790 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
1791 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
1792 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1793 				break;
1794 		}
1795 		if (ifa != NULL)
1796 			ifa_ref(ifa);
1797 		IF_ADDR_RUNLOCK(ifp);
1798 		if (!ifa)
1799 			return EADDRNOTAVAIL;
1800 		ia = ifa2ia6(ifa);
1801 
1802 		if (cmd == SIOCGLIFADDR) {
1803 			int error;
1804 
1805 			/* fill in the if_laddrreq structure */
1806 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1807 			error = sa6_recoverscope(
1808 			    (struct sockaddr_in6 *)&iflr->addr);
1809 			if (error != 0) {
1810 				ifa_free(ifa);
1811 				return (error);
1812 			}
1813 
1814 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1815 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1816 				    ia->ia_dstaddr.sin6_len);
1817 				error = sa6_recoverscope(
1818 				    (struct sockaddr_in6 *)&iflr->dstaddr);
1819 				if (error != 0) {
1820 					ifa_free(ifa);
1821 					return (error);
1822 				}
1823 			} else
1824 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1825 
1826 			iflr->prefixlen =
1827 			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1828 
1829 			iflr->flags = ia->ia6_flags;	/* XXX */
1830 			ifa_free(ifa);
1831 
1832 			return 0;
1833 		} else {
1834 			struct in6_aliasreq ifra;
1835 
1836 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1837 			bzero(&ifra, sizeof(ifra));
1838 			bcopy(iflr->iflr_name, ifra.ifra_name,
1839 			    sizeof(ifra.ifra_name));
1840 
1841 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1842 			    ia->ia_addr.sin6_len);
1843 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1844 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1845 				    ia->ia_dstaddr.sin6_len);
1846 			} else {
1847 				bzero(&ifra.ifra_dstaddr,
1848 				    sizeof(ifra.ifra_dstaddr));
1849 			}
1850 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1851 			    ia->ia_prefixmask.sin6_len);
1852 
1853 			ifra.ifra_flags = ia->ia6_flags;
1854 			ifa_free(ifa);
1855 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1856 			    ifp, td);
1857 		}
1858 	    }
1859 	}
1860 
1861 	return EOPNOTSUPP;	/* just for safety */
1862 }
1863 
1864 /*
1865  * Initialize an interface's IPv6 address and routing table entry.
1866  */
1867 static int
1868 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1869     struct sockaddr_in6 *sin6, int newhost)
1870 {
1871 	int	error = 0, plen, ifacount = 0;
1872 	struct ifaddr *ifa;
1873 
1874 	/*
1875 	 * Give the interface a chance to initialize
1876 	 * if this is its first address,
1877 	 * and to validate the address if necessary.
1878 	 */
1879 	IF_ADDR_RLOCK(ifp);
1880 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1881 		if (ifa->ifa_addr->sa_family != AF_INET6)
1882 			continue;
1883 		ifacount++;
1884 	}
1885 	IF_ADDR_RUNLOCK(ifp);
1886 
1887 	ia->ia_addr = *sin6;
1888 
1889 	if (ifacount <= 1 && ifp->if_ioctl) {
1890 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1891 		if (error)
1892 			return (error);
1893 	}
1894 
1895 	ia->ia_ifa.ifa_metric = ifp->if_metric;
1896 
1897 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
1898 
1899 	/*
1900 	 * Special case:
1901 	 * If a new destination address is specified for a point-to-point
1902 	 * interface, install a route to the destination as an interface
1903 	 * direct route.
1904 	 * XXX: the logic below rejects assigning multiple addresses on a p2p
1905 	 * interface that share the same destination.
1906 	 */
1907 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1908 	if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1909 	    ia->ia_dstaddr.sin6_family == AF_INET6) {
1910 		int rtflags = RTF_UP | RTF_HOST;
1911 		error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1912 		if (error)
1913 			return (error);
1914 		ia->ia_flags |= IFA_ROUTE;
1915 		/*
1916 		 * Handle the case for ::1 .
1917 		 */
1918 		if (ifp->if_flags & IFF_LOOPBACK)
1919 			ia->ia_flags |= IFA_RTSELF;
1920 	}
1921 
1922 	/*
1923 	 * add a loopback route to self
1924 	 */
1925 	if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1926 		error = ifa_add_loopback_route((struct ifaddr *)ia,
1927 		    (struct sockaddr *)&ia->ia_addr);
1928 		if (error == 0)
1929 			ia->ia_flags |= IFA_RTSELF;
1930 	}
1931 
1932 	/* Add local address to lltable, if necessary (ex. on p2p link). */
1933 	if (newhost)
1934 		in6_ifaddloop(&(ia->ia_ifa));
1935 
1936 	return (error);
1937 }
1938 
1939 /*
1940  * Find an IPv6 interface link-local address specific to an interface.
1941  * ifaddr is returned referenced.
1942  */
1943 struct in6_ifaddr *
1944 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1945 {
1946 	struct ifaddr *ifa;
1947 
1948 	IF_ADDR_RLOCK(ifp);
1949 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1950 		if (ifa->ifa_addr->sa_family != AF_INET6)
1951 			continue;
1952 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1953 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1954 			    ignoreflags) != 0)
1955 				continue;
1956 			ifa_ref(ifa);
1957 			break;
1958 		}
1959 	}
1960 	IF_ADDR_RUNLOCK(ifp);
1961 
1962 	return ((struct in6_ifaddr *)ifa);
1963 }
1964 
1965 
1966 /*
1967  * find the internet address corresponding to a given interface and address.
1968  * ifaddr is returned referenced.
1969  */
1970 struct in6_ifaddr *
1971 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1972 {
1973 	struct ifaddr *ifa;
1974 
1975 	IF_ADDR_RLOCK(ifp);
1976 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1977 		if (ifa->ifa_addr->sa_family != AF_INET6)
1978 			continue;
1979 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1980 			ifa_ref(ifa);
1981 			break;
1982 		}
1983 	}
1984 	IF_ADDR_RUNLOCK(ifp);
1985 
1986 	return ((struct in6_ifaddr *)ifa);
1987 }
1988 
1989 /*
1990  * Find a link-local scoped address on ifp and return it if any.
1991  */
1992 struct in6_ifaddr *
1993 in6ifa_llaonifp(struct ifnet *ifp)
1994 {
1995 	struct sockaddr_in6 *sin6;
1996 	struct ifaddr *ifa;
1997 
1998 	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1999 		return (NULL);
2000 	if_addr_rlock(ifp);
2001 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2002 		if (ifa->ifa_addr->sa_family != AF_INET6)
2003 			continue;
2004 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2005 		if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
2006 		    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
2007 		    IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
2008 			break;
2009 	}
2010 	if_addr_runlock(ifp);
2011 
2012 	return ((struct in6_ifaddr *)ifa);
2013 }
2014 
2015 /*
2016  * Convert IP6 address to printable (loggable) representation. Caller
2017  * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
2018  */
2019 static char digits[] = "0123456789abcdef";
2020 char *
2021 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
2022 {
2023 	int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
2024 	char *cp;
2025 	const u_int16_t *a = (const u_int16_t *)addr;
2026 	const u_int8_t *d;
2027 	int dcolon = 0, zero = 0;
2028 
2029 	cp = ip6buf;
2030 
2031 	for (i = 0; i < 8; i++) {
2032 		if (*(a + i) == 0) {
2033 			cnt++;
2034 			if (cnt == 1)
2035 				idx = i;
2036 		}
2037 		else if (maxcnt < cnt) {
2038 			maxcnt = cnt;
2039 			index = idx;
2040 			cnt = 0;
2041 		}
2042 	}
2043 	if (maxcnt < cnt) {
2044 		maxcnt = cnt;
2045 		index = idx;
2046 	}
2047 
2048 	for (i = 0; i < 8; i++) {
2049 		if (dcolon == 1) {
2050 			if (*a == 0) {
2051 				if (i == 7)
2052 					*cp++ = ':';
2053 				a++;
2054 				continue;
2055 			} else
2056 				dcolon = 2;
2057 		}
2058 		if (*a == 0) {
2059 			if (dcolon == 0 && *(a + 1) == 0 && i == index) {
2060 				if (i == 0)
2061 					*cp++ = ':';
2062 				*cp++ = ':';
2063 				dcolon = 1;
2064 			} else {
2065 				*cp++ = '0';
2066 				*cp++ = ':';
2067 			}
2068 			a++;
2069 			continue;
2070 		}
2071 		d = (const u_char *)a;
2072 		/* Try to eliminate leading zeros in printout like in :0001. */
2073 		zero = 1;
2074 		*cp = digits[*d >> 4];
2075 		if (*cp != '0') {
2076 			zero = 0;
2077 			cp++;
2078 		}
2079 		*cp = digits[*d++ & 0xf];
2080 		if (zero == 0 || (*cp != '0')) {
2081 			zero = 0;
2082 			cp++;
2083 		}
2084 		*cp = digits[*d >> 4];
2085 		if (zero == 0 || (*cp != '0')) {
2086 			zero = 0;
2087 			cp++;
2088 		}
2089 		*cp++ = digits[*d & 0xf];
2090 		*cp++ = ':';
2091 		a++;
2092 	}
2093 	*--cp = '\0';
2094 	return (ip6buf);
2095 }
2096 
2097 int
2098 in6_localaddr(struct in6_addr *in6)
2099 {
2100 	struct in6_ifaddr *ia;
2101 
2102 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
2103 		return 1;
2104 
2105 	IN6_IFADDR_RLOCK();
2106 	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2107 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2108 		    &ia->ia_prefixmask.sin6_addr)) {
2109 			IN6_IFADDR_RUNLOCK();
2110 			return 1;
2111 		}
2112 	}
2113 	IN6_IFADDR_RUNLOCK();
2114 
2115 	return (0);
2116 }
2117 
2118 /*
2119  * Return 1 if an internet address is for the local host and configured
2120  * on one of its interfaces.
2121  */
2122 int
2123 in6_localip(struct in6_addr *in6)
2124 {
2125 	struct in6_ifaddr *ia;
2126 
2127 	IN6_IFADDR_RLOCK();
2128 	LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
2129 		if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
2130 			IN6_IFADDR_RUNLOCK();
2131 			return (1);
2132 		}
2133 	}
2134 	IN6_IFADDR_RUNLOCK();
2135 	return (0);
2136 }
2137 
2138 int
2139 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
2140 {
2141 	struct in6_ifaddr *ia;
2142 
2143 	IN6_IFADDR_RLOCK();
2144 	LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
2145 		if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
2146 			if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
2147 				IN6_IFADDR_RUNLOCK();
2148 				return (1); /* true */
2149 			}
2150 			break;
2151 		}
2152 	}
2153 	IN6_IFADDR_RUNLOCK();
2154 
2155 	return (0);		/* false */
2156 }
2157 
2158 /*
2159  * return length of part which dst and src are equal
2160  * hard coding...
2161  */
2162 int
2163 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2164 {
2165 	int match = 0;
2166 	u_char *s = (u_char *)src, *d = (u_char *)dst;
2167 	u_char *lim = s + 16, r;
2168 
2169 	while (s < lim)
2170 		if ((r = (*d++ ^ *s++)) != 0) {
2171 			while (r < 128) {
2172 				match++;
2173 				r <<= 1;
2174 			}
2175 			break;
2176 		} else
2177 			match += 8;
2178 	return match;
2179 }
2180 
2181 /* XXX: to be scope conscious */
2182 int
2183 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2184 {
2185 	int bytelen, bitlen;
2186 
2187 	/* sanity check */
2188 	if (0 > len || len > 128) {
2189 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2190 		    len);
2191 		return (0);
2192 	}
2193 
2194 	bytelen = len / 8;
2195 	bitlen = len % 8;
2196 
2197 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2198 		return (0);
2199 	if (bitlen != 0 &&
2200 	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
2201 	    p2->s6_addr[bytelen] >> (8 - bitlen))
2202 		return (0);
2203 
2204 	return (1);
2205 }
2206 
2207 void
2208 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2209 {
2210 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2211 	int bytelen, bitlen, i;
2212 
2213 	/* sanity check */
2214 	if (0 > len || len > 128) {
2215 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2216 		    len);
2217 		return;
2218 	}
2219 
2220 	bzero(maskp, sizeof(*maskp));
2221 	bytelen = len / 8;
2222 	bitlen = len % 8;
2223 	for (i = 0; i < bytelen; i++)
2224 		maskp->s6_addr[i] = 0xff;
2225 	if (bitlen)
2226 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2227 }
2228 
2229 /*
2230  * return the best address out of the same scope. if no address was
2231  * found, return the first valid address from designated IF.
2232  */
2233 struct in6_ifaddr *
2234 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2235 {
2236 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
2237 	struct ifaddr *ifa;
2238 	struct in6_ifaddr *besta = 0;
2239 	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
2240 
2241 	dep[0] = dep[1] = NULL;
2242 
2243 	/*
2244 	 * We first look for addresses in the same scope.
2245 	 * If there is one, return it.
2246 	 * If two or more, return one which matches the dst longest.
2247 	 * If none, return one of global addresses assigned other ifs.
2248 	 */
2249 	IF_ADDR_RLOCK(ifp);
2250 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2251 		if (ifa->ifa_addr->sa_family != AF_INET6)
2252 			continue;
2253 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2254 			continue; /* XXX: is there any case to allow anycast? */
2255 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2256 			continue; /* don't use this interface */
2257 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2258 			continue;
2259 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2260 			if (V_ip6_use_deprecated)
2261 				dep[0] = (struct in6_ifaddr *)ifa;
2262 			continue;
2263 		}
2264 
2265 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2266 			/*
2267 			 * call in6_matchlen() as few as possible
2268 			 */
2269 			if (besta) {
2270 				if (blen == -1)
2271 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2272 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2273 				if (tlen > blen) {
2274 					blen = tlen;
2275 					besta = (struct in6_ifaddr *)ifa;
2276 				}
2277 			} else
2278 				besta = (struct in6_ifaddr *)ifa;
2279 		}
2280 	}
2281 	if (besta) {
2282 		ifa_ref(&besta->ia_ifa);
2283 		IF_ADDR_RUNLOCK(ifp);
2284 		return (besta);
2285 	}
2286 
2287 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2288 		if (ifa->ifa_addr->sa_family != AF_INET6)
2289 			continue;
2290 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2291 			continue; /* XXX: is there any case to allow anycast? */
2292 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2293 			continue; /* don't use this interface */
2294 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2295 			continue;
2296 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2297 			if (V_ip6_use_deprecated)
2298 				dep[1] = (struct in6_ifaddr *)ifa;
2299 			continue;
2300 		}
2301 
2302 		if (ifa != NULL)
2303 			ifa_ref(ifa);
2304 		IF_ADDR_RUNLOCK(ifp);
2305 		return (struct in6_ifaddr *)ifa;
2306 	}
2307 
2308 	/* use the last-resort values, that are, deprecated addresses */
2309 	if (dep[0]) {
2310 		ifa_ref((struct ifaddr *)dep[0]);
2311 		IF_ADDR_RUNLOCK(ifp);
2312 		return dep[0];
2313 	}
2314 	if (dep[1]) {
2315 		ifa_ref((struct ifaddr *)dep[1]);
2316 		IF_ADDR_RUNLOCK(ifp);
2317 		return dep[1];
2318 	}
2319 
2320 	IF_ADDR_RUNLOCK(ifp);
2321 	return NULL;
2322 }
2323 
2324 /*
2325  * perform DAD when interface becomes IFF_UP.
2326  */
2327 void
2328 in6_if_up(struct ifnet *ifp)
2329 {
2330 	struct ifaddr *ifa;
2331 	struct in6_ifaddr *ia;
2332 
2333 	IF_ADDR_RLOCK(ifp);
2334 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2335 		if (ifa->ifa_addr->sa_family != AF_INET6)
2336 			continue;
2337 		ia = (struct in6_ifaddr *)ifa;
2338 		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2339 			/*
2340 			 * The TENTATIVE flag was likely set by hand
2341 			 * beforehand, implicitly indicating the need for DAD.
2342 			 * We may be able to skip the random delay in this
2343 			 * case, but we impose delays just in case.
2344 			 */
2345 			nd6_dad_start(ifa,
2346 			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2347 		}
2348 	}
2349 	IF_ADDR_RUNLOCK(ifp);
2350 
2351 	/*
2352 	 * special cases, like 6to4, are handled in in6_ifattach
2353 	 */
2354 	in6_ifattach(ifp, NULL);
2355 }
2356 
2357 int
2358 in6if_do_dad(struct ifnet *ifp)
2359 {
2360 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2361 		return (0);
2362 
2363 	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2364 		return (0);
2365 
2366 	switch (ifp->if_type) {
2367 #ifdef IFT_DUMMY
2368 	case IFT_DUMMY:
2369 #endif
2370 	case IFT_FAITH:
2371 		/*
2372 		 * These interfaces do not have the IFF_LOOPBACK flag,
2373 		 * but loop packets back.  We do not have to do DAD on such
2374 		 * interfaces.  We should even omit it, because loop-backed
2375 		 * NS would confuse the DAD procedure.
2376 		 */
2377 		return (0);
2378 	default:
2379 		/*
2380 		 * Our DAD routine requires the interface up and running.
2381 		 * However, some interfaces can be up before the RUNNING
2382 		 * status.  Additionaly, users may try to assign addresses
2383 		 * before the interface becomes up (or running).
2384 		 * We simply skip DAD in such a case as a work around.
2385 		 * XXX: we should rather mark "tentative" on such addresses,
2386 		 * and do DAD after the interface becomes ready.
2387 		 */
2388 		if (!((ifp->if_flags & IFF_UP) &&
2389 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2390 			return (0);
2391 
2392 		return (1);
2393 	}
2394 }
2395 
2396 /*
2397  * Calculate max IPv6 MTU through all the interfaces and store it
2398  * to in6_maxmtu.
2399  */
2400 void
2401 in6_setmaxmtu(void)
2402 {
2403 	unsigned long maxmtu = 0;
2404 	struct ifnet *ifp;
2405 
2406 	IFNET_RLOCK_NOSLEEP();
2407 	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
2408 		/* this function can be called during ifnet initialization */
2409 		if (!ifp->if_afdata[AF_INET6])
2410 			continue;
2411 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2412 		    IN6_LINKMTU(ifp) > maxmtu)
2413 			maxmtu = IN6_LINKMTU(ifp);
2414 	}
2415 	IFNET_RUNLOCK_NOSLEEP();
2416 	if (maxmtu)	/* update only when maxmtu is positive */
2417 		V_in6_maxmtu = maxmtu;
2418 }
2419 
2420 /*
2421  * Provide the length of interface identifiers to be used for the link attached
2422  * to the given interface.  The length should be defined in "IPv6 over
2423  * xxx-link" document.  Note that address architecture might also define
2424  * the length for a particular set of address prefixes, regardless of the
2425  * link type.  As clarified in rfc2462bis, those two definitions should be
2426  * consistent, and those really are as of August 2004.
2427  */
2428 int
2429 in6_if2idlen(struct ifnet *ifp)
2430 {
2431 	switch (ifp->if_type) {
2432 	case IFT_ETHER:		/* RFC2464 */
2433 #ifdef IFT_PROPVIRTUAL
2434 	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
2435 #endif
2436 #ifdef IFT_L2VLAN
2437 	case IFT_L2VLAN:	/* ditto */
2438 #endif
2439 #ifdef IFT_IEEE80211
2440 	case IFT_IEEE80211:	/* ditto */
2441 #endif
2442 #ifdef IFT_MIP
2443 	case IFT_MIP:	/* ditto */
2444 #endif
2445 	case IFT_INFINIBAND:
2446 		return (64);
2447 	case IFT_FDDI:		/* RFC2467 */
2448 		return (64);
2449 	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
2450 		return (64);
2451 	case IFT_PPP:		/* RFC2472 */
2452 		return (64);
2453 	case IFT_ARCNET:	/* RFC2497 */
2454 		return (64);
2455 	case IFT_FRELAY:	/* RFC2590 */
2456 		return (64);
2457 	case IFT_IEEE1394:	/* RFC3146 */
2458 		return (64);
2459 	case IFT_GIF:
2460 		return (64);	/* draft-ietf-v6ops-mech-v2-07 */
2461 	case IFT_LOOP:
2462 		return (64);	/* XXX: is this really correct? */
2463 	default:
2464 		/*
2465 		 * Unknown link type:
2466 		 * It might be controversial to use the today's common constant
2467 		 * of 64 for these cases unconditionally.  For full compliance,
2468 		 * we should return an error in this case.  On the other hand,
2469 		 * if we simply miss the standard for the link type or a new
2470 		 * standard is defined for a new link type, the IFID length
2471 		 * is very likely to be the common constant.  As a compromise,
2472 		 * we always use the constant, but make an explicit notice
2473 		 * indicating the "unknown" case.
2474 		 */
2475 		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2476 		return (64);
2477 	}
2478 }
2479 
2480 #include <sys/sysctl.h>
2481 
2482 struct in6_llentry {
2483 	struct llentry		base;
2484 	struct sockaddr_in6	l3_addr6;
2485 };
2486 
2487 /*
2488  * Deletes an address from the address table.
2489  * This function is called by the timer functions
2490  * such as arptimer() and nd6_llinfo_timer(), and
2491  * the caller does the locking.
2492  */
2493 static void
2494 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2495 {
2496 	LLE_WUNLOCK(lle);
2497 	LLE_LOCK_DESTROY(lle);
2498 	free(lle, M_LLTABLE);
2499 }
2500 
2501 static struct llentry *
2502 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2503 {
2504 	struct in6_llentry *lle;
2505 
2506 	lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2507 	if (lle == NULL)		/* NB: caller generates msg */
2508 		return NULL;
2509 
2510 	lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2511 	lle->base.lle_refcnt = 1;
2512 	lle->base.lle_free = in6_lltable_free;
2513 	LLE_LOCK_INIT(&lle->base);
2514 	callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2515 	    CALLOUT_RETURNUNLOCKED);
2516 
2517 	return (&lle->base);
2518 }
2519 
2520 static void
2521 in6_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
2522     const struct sockaddr *mask, u_int flags)
2523 {
2524 	const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2525 	const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2526 	struct llentry *lle, *next;
2527 	int i;
2528 
2529 	/*
2530 	 * (flags & LLE_STATIC) means deleting all entries
2531 	 * including static ND6 entries.
2532 	 */
2533 	IF_AFDATA_WLOCK(llt->llt_ifp);
2534 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2535 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2536 			if (IN6_ARE_MASKED_ADDR_EQUAL(
2537 			    &satosin6(L3_ADDR(lle))->sin6_addr,
2538 			    &pfx->sin6_addr, &msk->sin6_addr) &&
2539 			    ((flags & LLE_STATIC) ||
2540 			    !(lle->la_flags & LLE_STATIC))) {
2541 				LLE_WLOCK(lle);
2542 				if (callout_stop(&lle->la_timer))
2543 					LLE_REMREF(lle);
2544 				llentry_free(lle);
2545 			}
2546 		}
2547 	}
2548 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
2549 }
2550 
2551 static int
2552 in6_lltable_rtcheck(struct ifnet *ifp,
2553 		    u_int flags,
2554 		    const struct sockaddr *l3addr)
2555 {
2556 	struct rtentry *rt;
2557 	char ip6buf[INET6_ADDRSTRLEN];
2558 
2559 	KASSERT(l3addr->sa_family == AF_INET6,
2560 	    ("sin_family %d", l3addr->sa_family));
2561 
2562 	/* Our local addresses are always only installed on the default FIB. */
2563 	/* XXX rtalloc1 should take a const param */
2564 	rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0,
2565 	    RT_DEFAULT_FIB);
2566 	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2567 		struct ifaddr *ifa;
2568 		/*
2569 		 * Create an ND6 cache for an IPv6 neighbor
2570 		 * that is not covered by our own prefix.
2571 		 */
2572 		/* XXX ifaof_ifpforaddr should take a const param */
2573 		ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2574 		if (ifa != NULL) {
2575 			ifa_free(ifa);
2576 			if (rt != NULL)
2577 				RTFREE_LOCKED(rt);
2578 			return 0;
2579 		}
2580 		log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2581 		    ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2582 		if (rt != NULL)
2583 			RTFREE_LOCKED(rt);
2584 		return EINVAL;
2585 	}
2586 	RTFREE_LOCKED(rt);
2587 	return 0;
2588 }
2589 
2590 static struct llentry *
2591 in6_lltable_lookup(struct lltable *llt, u_int flags,
2592 	const struct sockaddr *l3addr)
2593 {
2594 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2595 	struct ifnet *ifp = llt->llt_ifp;
2596 	struct llentry *lle;
2597 	struct llentries *lleh;
2598 	u_int hashkey;
2599 
2600 	IF_AFDATA_LOCK_ASSERT(ifp);
2601 	KASSERT(l3addr->sa_family == AF_INET6,
2602 	    ("sin_family %d", l3addr->sa_family));
2603 
2604 	hashkey = sin6->sin6_addr.s6_addr32[3];
2605 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2606 	LIST_FOREACH(lle, lleh, lle_next) {
2607 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2608 		if (lle->la_flags & LLE_DELETED)
2609 			continue;
2610 		if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2611 		    sizeof(struct in6_addr)) == 0)
2612 			break;
2613 	}
2614 
2615 	if (lle == NULL) {
2616 		if (!(flags & LLE_CREATE))
2617 			return (NULL);
2618 		/*
2619 		 * A route that covers the given address must have
2620 		 * been installed 1st because we are doing a resolution,
2621 		 * verify this.
2622 		 */
2623 		if (!(flags & LLE_IFADDR) &&
2624 		    in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2625 			return NULL;
2626 
2627 		lle = in6_lltable_new(l3addr, flags);
2628 		if (lle == NULL) {
2629 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2630 			return NULL;
2631 		}
2632 		lle->la_flags = flags & ~LLE_CREATE;
2633 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2634 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2635 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
2636 		}
2637 
2638 		lle->lle_tbl  = llt;
2639 		lle->lle_head = lleh;
2640 		lle->la_flags |= LLE_LINKED;
2641 		LIST_INSERT_HEAD(lleh, lle, lle_next);
2642 	} else if (flags & LLE_DELETE) {
2643 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2644 			LLE_WLOCK(lle);
2645 			lle->la_flags |= LLE_DELETED;
2646 #ifdef DIAGNOSTIC
2647 			log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2648 #endif
2649 			if ((lle->la_flags &
2650 			    (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
2651 				llentry_free(lle);
2652 			else
2653 				LLE_WUNLOCK(lle);
2654 		}
2655 		lle = (void *)-1;
2656 	}
2657 	if (LLE_IS_VALID(lle)) {
2658 		if (flags & LLE_EXCLUSIVE)
2659 			LLE_WLOCK(lle);
2660 		else
2661 			LLE_RLOCK(lle);
2662 	}
2663 	return (lle);
2664 }
2665 
2666 static int
2667 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2668 {
2669 	struct ifnet *ifp = llt->llt_ifp;
2670 	struct llentry *lle;
2671 	/* XXX stack use */
2672 	struct {
2673 		struct rt_msghdr	rtm;
2674 		struct sockaddr_in6	sin6;
2675 		/*
2676 		 * ndp.c assumes that sdl is word aligned
2677 		 */
2678 #ifdef __LP64__
2679 		uint32_t		pad;
2680 #endif
2681 		struct sockaddr_dl	sdl;
2682 	} ndpc;
2683 	int i, error;
2684 
2685 	if (ifp->if_flags & IFF_LOOPBACK)
2686 		return 0;
2687 
2688 	LLTABLE_LOCK_ASSERT();
2689 
2690 	error = 0;
2691 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2692 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2693 			struct sockaddr_dl *sdl;
2694 
2695 			/* skip deleted or invalid entries */
2696 			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2697 				continue;
2698 			/* Skip if jailed and not a valid IP of the prison. */
2699 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2700 				continue;
2701 			/*
2702 			 * produce a msg made of:
2703 			 *  struct rt_msghdr;
2704 			 *  struct sockaddr_in6 (IPv6)
2705 			 *  struct sockaddr_dl;
2706 			 */
2707 			bzero(&ndpc, sizeof(ndpc));
2708 			ndpc.rtm.rtm_msglen = sizeof(ndpc);
2709 			ndpc.rtm.rtm_version = RTM_VERSION;
2710 			ndpc.rtm.rtm_type = RTM_GET;
2711 			ndpc.rtm.rtm_flags = RTF_UP;
2712 			ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2713 			ndpc.sin6.sin6_family = AF_INET6;
2714 			ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2715 			bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2716 			if (V_deembed_scopeid)
2717 				sa6_recoverscope(&ndpc.sin6);
2718 
2719 			/* publish */
2720 			if (lle->la_flags & LLE_PUB)
2721 				ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2722 
2723 			sdl = &ndpc.sdl;
2724 			sdl->sdl_family = AF_LINK;
2725 			sdl->sdl_len = sizeof(*sdl);
2726 			sdl->sdl_alen = ifp->if_addrlen;
2727 			sdl->sdl_index = ifp->if_index;
2728 			sdl->sdl_type = ifp->if_type;
2729 			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2730 			ndpc.rtm.rtm_rmx.rmx_expire =
2731 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2732 			ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2733 			if (lle->la_flags & LLE_STATIC)
2734 				ndpc.rtm.rtm_flags |= RTF_STATIC;
2735 			ndpc.rtm.rtm_index = ifp->if_index;
2736 			error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2737 			if (error)
2738 				break;
2739 		}
2740 	}
2741 	return error;
2742 }
2743 
2744 void *
2745 in6_domifattach(struct ifnet *ifp)
2746 {
2747 	struct in6_ifextra *ext;
2748 
2749 	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2750 	bzero(ext, sizeof(*ext));
2751 
2752 	ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2753 	    sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2754 	COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2755 	    sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2756 
2757 	ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2758 	    sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2759 	    M_WAITOK);
2760 	COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2761 	    sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2762 
2763 	ext->nd_ifinfo = nd6_ifattach(ifp);
2764 	ext->scope6_id = scope6_ifattach(ifp);
2765 	ext->lltable = lltable_init(ifp, AF_INET6);
2766 	if (ext->lltable != NULL) {
2767 		ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2768 		ext->lltable->llt_lookup = in6_lltable_lookup;
2769 		ext->lltable->llt_dump = in6_lltable_dump;
2770 	}
2771 
2772 	ext->mld_ifinfo = mld_domifattach(ifp);
2773 
2774 	return ext;
2775 }
2776 
2777 void
2778 in6_domifdetach(struct ifnet *ifp, void *aux)
2779 {
2780 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2781 
2782 	mld_domifdetach(ifp);
2783 	scope6_ifdetach(ext->scope6_id);
2784 	nd6_ifdetach(ext->nd_ifinfo);
2785 	lltable_free(ext->lltable);
2786 	COUNTER_ARRAY_FREE(ext->in6_ifstat,
2787 	    sizeof(struct in6_ifstat) / sizeof(uint64_t));
2788 	free(ext->in6_ifstat, M_IFADDR);
2789 	COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2790 	    sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2791 	free(ext->icmp6_ifstat, M_IFADDR);
2792 	free(ext, M_IFADDR);
2793 }
2794 
2795 /*
2796  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
2797  * v4 mapped addr or v4 compat addr
2798  */
2799 void
2800 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2801 {
2802 
2803 	bzero(sin, sizeof(*sin));
2804 	sin->sin_len = sizeof(struct sockaddr_in);
2805 	sin->sin_family = AF_INET;
2806 	sin->sin_port = sin6->sin6_port;
2807 	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2808 }
2809 
2810 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2811 void
2812 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2813 {
2814 	bzero(sin6, sizeof(*sin6));
2815 	sin6->sin6_len = sizeof(struct sockaddr_in6);
2816 	sin6->sin6_family = AF_INET6;
2817 	sin6->sin6_port = sin->sin_port;
2818 	sin6->sin6_addr.s6_addr32[0] = 0;
2819 	sin6->sin6_addr.s6_addr32[1] = 0;
2820 	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2821 	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2822 }
2823 
2824 /* Convert sockaddr_in6 into sockaddr_in. */
2825 void
2826 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2827 {
2828 	struct sockaddr_in *sin_p;
2829 	struct sockaddr_in6 sin6;
2830 
2831 	/*
2832 	 * Save original sockaddr_in6 addr and convert it
2833 	 * to sockaddr_in.
2834 	 */
2835 	sin6 = *(struct sockaddr_in6 *)nam;
2836 	sin_p = (struct sockaddr_in *)nam;
2837 	in6_sin6_2_sin(sin_p, &sin6);
2838 }
2839 
2840 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2841 void
2842 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2843 {
2844 	struct sockaddr_in *sin_p;
2845 	struct sockaddr_in6 *sin6_p;
2846 
2847 	sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2848 	sin_p = (struct sockaddr_in *)*nam;
2849 	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2850 	free(*nam, M_SONAME);
2851 	*nam = (struct sockaddr *)sin6_p;
2852 }
2853