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