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