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