xref: /freebsd/sys/netinet6/in6.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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->sa_family != AF_INET6)
1700 			continue;
1701 		ifacount++;
1702 	}
1703 
1704 	ia->ia_addr = *sin6;
1705 
1706 	if (ifacount <= 1 && ifp->if_ioctl) {
1707 		IFF_LOCKGIANT(ifp);
1708 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1709 		IFF_UNLOCKGIANT(ifp);
1710 		if (error) {
1711 			splx(s);
1712 			return (error);
1713 		}
1714 	}
1715 	splx(s);
1716 
1717 	ia->ia_ifa.ifa_metric = ifp->if_metric;
1718 
1719 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
1720 
1721 	if (newhost && nd6_need_cache(ifp) != 0) {
1722 		/* set the rtrequest function to create llinfo */
1723 		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1724 	}
1725 
1726 	/*
1727 	 * Special case:
1728 	 * If a new destination address is specified for a point-to-point
1729 	 * interface, install a route to the destination as an interface
1730 	 * direct route.  In addition, if the link is expected to have neighbor
1731 	 * cache entries, specify RTF_LLINFO so that a cache entry for the
1732 	 * destination address will be created.
1733 	 * created
1734 	 * XXX: the logic below rejects assigning multiple addresses on a p2p
1735 	 * interface that share the same destination.
1736 	 */
1737 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1738 	if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1739 	    ia->ia_dstaddr.sin6_family == AF_INET6) {
1740 		int rtflags = RTF_UP | RTF_HOST;
1741 		struct rtentry *rt = NULL, **rtp = NULL;
1742 
1743 		if (nd6_need_cache(ifp) != 0) {
1744 			rtflags |= RTF_LLINFO;
1745 			rtp = &rt;
1746 		}
1747 
1748 		error = rtrequest(RTM_ADD, (struct sockaddr *)&ia->ia_dstaddr,
1749 		    (struct sockaddr *)&ia->ia_addr,
1750 		    (struct sockaddr *)&ia->ia_prefixmask,
1751 		    ia->ia_flags | rtflags, rtp);
1752 		if (error != 0)
1753 			return (error);
1754 		if (rt != NULL) {
1755 			struct llinfo_nd6 *ln;
1756 
1757 			RT_LOCK(rt);
1758 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1759 			if (ln != NULL) {
1760 				/*
1761 				 * Set the state to STALE because we don't
1762 				 * have to perform address resolution on this
1763 				 * link.
1764 				 */
1765 				ln->ln_state = ND6_LLINFO_STALE;
1766 			}
1767 			RT_REMREF(rt);
1768 			RT_UNLOCK(rt);
1769 		}
1770 		ia->ia_flags |= IFA_ROUTE;
1771 	}
1772 	if (plen < 128) {
1773 		/*
1774 		 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1775 		 */
1776 		ia->ia_ifa.ifa_flags |= RTF_CLONING;
1777 	}
1778 
1779 	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1780 	if (newhost)
1781 		in6_ifaddloop(&(ia->ia_ifa));
1782 
1783 	return (error);
1784 }
1785 
1786 struct in6_multi_mship *
1787 in6_joingroup(ifp, addr, errorp, delay)
1788 	struct ifnet *ifp;
1789 	struct in6_addr *addr;
1790 	int *errorp;
1791 	int delay;
1792 {
1793 	struct in6_multi_mship *imm;
1794 
1795 	imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1796 	if (!imm) {
1797 		*errorp = ENOBUFS;
1798 		return NULL;
1799 	}
1800 	imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay);
1801 	if (!imm->i6mm_maddr) {
1802 		/* *errorp is alrady set */
1803 		free(imm, M_IP6MADDR);
1804 		return NULL;
1805 	}
1806 	return imm;
1807 }
1808 
1809 int
1810 in6_leavegroup(imm)
1811 	struct in6_multi_mship *imm;
1812 {
1813 
1814 	if (imm->i6mm_maddr)
1815 		in6_delmulti(imm->i6mm_maddr);
1816 	free(imm,  M_IP6MADDR);
1817 	return 0;
1818 }
1819 
1820 /*
1821  * Find an IPv6 interface link-local address specific to an interface.
1822  */
1823 struct in6_ifaddr *
1824 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1825 	struct ifnet *ifp;
1826 	int ignoreflags;
1827 {
1828 	struct ifaddr *ifa;
1829 
1830 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1831 		if (ifa->ifa_addr->sa_family != AF_INET6)
1832 			continue;
1833 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1834 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1835 			     ignoreflags) != 0)
1836 				continue;
1837 			break;
1838 		}
1839 	}
1840 
1841 	return ((struct in6_ifaddr *)ifa);
1842 }
1843 
1844 
1845 /*
1846  * find the internet address corresponding to a given interface and address.
1847  */
1848 struct in6_ifaddr *
1849 in6ifa_ifpwithaddr(ifp, addr)
1850 	struct ifnet *ifp;
1851 	struct in6_addr *addr;
1852 {
1853 	struct ifaddr *ifa;
1854 
1855 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1856 		if (ifa->ifa_addr->sa_family != AF_INET6)
1857 			continue;
1858 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1859 			break;
1860 	}
1861 
1862 	return ((struct in6_ifaddr *)ifa);
1863 }
1864 
1865 /*
1866  * Convert IP6 address to printable (loggable) representation.
1867  */
1868 static char digits[] = "0123456789abcdef";
1869 static int ip6round = 0;
1870 char *
1871 ip6_sprintf(addr)
1872 	const struct in6_addr *addr;
1873 {
1874 	static char ip6buf[8][48];
1875 	int i;
1876 	char *cp;
1877 	const u_int16_t *a = (const u_int16_t *)addr;
1878 	const u_int8_t *d;
1879 	int dcolon = 0;
1880 
1881 	ip6round = (ip6round + 1) & 7;
1882 	cp = ip6buf[ip6round];
1883 
1884 	for (i = 0; i < 8; i++) {
1885 		if (dcolon == 1) {
1886 			if (*a == 0) {
1887 				if (i == 7)
1888 					*cp++ = ':';
1889 				a++;
1890 				continue;
1891 			} else
1892 				dcolon = 2;
1893 		}
1894 		if (*a == 0) {
1895 			if (dcolon == 0 && *(a + 1) == 0) {
1896 				if (i == 0)
1897 					*cp++ = ':';
1898 				*cp++ = ':';
1899 				dcolon = 1;
1900 			} else {
1901 				*cp++ = '0';
1902 				*cp++ = ':';
1903 			}
1904 			a++;
1905 			continue;
1906 		}
1907 		d = (const u_char *)a;
1908 		*cp++ = digits[*d >> 4];
1909 		*cp++ = digits[*d++ & 0xf];
1910 		*cp++ = digits[*d >> 4];
1911 		*cp++ = digits[*d & 0xf];
1912 		*cp++ = ':';
1913 		a++;
1914 	}
1915 	*--cp = 0;
1916 	return (ip6buf[ip6round]);
1917 }
1918 
1919 int
1920 in6_localaddr(in6)
1921 	struct in6_addr *in6;
1922 {
1923 	struct in6_ifaddr *ia;
1924 
1925 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1926 		return 1;
1927 
1928 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1929 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1930 		    &ia->ia_prefixmask.sin6_addr)) {
1931 			return 1;
1932 		}
1933 	}
1934 
1935 	return (0);
1936 }
1937 
1938 int
1939 in6_is_addr_deprecated(sa6)
1940 	struct sockaddr_in6 *sa6;
1941 {
1942 	struct in6_ifaddr *ia;
1943 
1944 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1945 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1946 				       &sa6->sin6_addr) &&
1947 		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1948 			return (1); /* true */
1949 
1950 		/* XXX: do we still have to go thru the rest of the list? */
1951 	}
1952 
1953 	return (0);		/* false */
1954 }
1955 
1956 /*
1957  * return length of part which dst and src are equal
1958  * hard coding...
1959  */
1960 int
1961 in6_matchlen(src, dst)
1962 struct in6_addr *src, *dst;
1963 {
1964 	int match = 0;
1965 	u_char *s = (u_char *)src, *d = (u_char *)dst;
1966 	u_char *lim = s + 16, r;
1967 
1968 	while (s < lim)
1969 		if ((r = (*d++ ^ *s++)) != 0) {
1970 			while (r < 128) {
1971 				match++;
1972 				r <<= 1;
1973 			}
1974 			break;
1975 		} else
1976 			match += 8;
1977 	return match;
1978 }
1979 
1980 /* XXX: to be scope conscious */
1981 int
1982 in6_are_prefix_equal(p1, p2, len)
1983 	struct in6_addr *p1, *p2;
1984 	int len;
1985 {
1986 	int bytelen, bitlen;
1987 
1988 	/* sanity check */
1989 	if (0 > len || len > 128) {
1990 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1991 		    len);
1992 		return (0);
1993 	}
1994 
1995 	bytelen = len / 8;
1996 	bitlen = len % 8;
1997 
1998 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1999 		return (0);
2000 	if (bitlen != 0 &&
2001 	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
2002 	    p2->s6_addr[bytelen] >> (8 - bitlen))
2003 		return (0);
2004 
2005 	return (1);
2006 }
2007 
2008 void
2009 in6_prefixlen2mask(maskp, len)
2010 	struct in6_addr *maskp;
2011 	int len;
2012 {
2013 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2014 	int bytelen, bitlen, i;
2015 
2016 	/* sanity check */
2017 	if (0 > len || len > 128) {
2018 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2019 		    len);
2020 		return;
2021 	}
2022 
2023 	bzero(maskp, sizeof(*maskp));
2024 	bytelen = len / 8;
2025 	bitlen = len % 8;
2026 	for (i = 0; i < bytelen; i++)
2027 		maskp->s6_addr[i] = 0xff;
2028 	if (bitlen)
2029 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2030 }
2031 
2032 /*
2033  * return the best address out of the same scope. if no address was
2034  * found, return the first valid address from designated IF.
2035  */
2036 struct in6_ifaddr *
2037 in6_ifawithifp(ifp, dst)
2038 	struct ifnet *ifp;
2039 	struct in6_addr *dst;
2040 {
2041 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
2042 	struct ifaddr *ifa;
2043 	struct in6_ifaddr *besta = 0;
2044 	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
2045 
2046 	dep[0] = dep[1] = NULL;
2047 
2048 	/*
2049 	 * We first look for addresses in the same scope.
2050 	 * If there is one, return it.
2051 	 * If two or more, return one which matches the dst longest.
2052 	 * If none, return one of global addresses assigned other ifs.
2053 	 */
2054 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2055 		if (ifa->ifa_addr->sa_family != AF_INET6)
2056 			continue;
2057 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2058 			continue; /* XXX: is there any case to allow anycast? */
2059 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2060 			continue; /* don't use this interface */
2061 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2062 			continue;
2063 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2064 			if (ip6_use_deprecated)
2065 				dep[0] = (struct in6_ifaddr *)ifa;
2066 			continue;
2067 		}
2068 
2069 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2070 			/*
2071 			 * call in6_matchlen() as few as possible
2072 			 */
2073 			if (besta) {
2074 				if (blen == -1)
2075 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2076 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2077 				if (tlen > blen) {
2078 					blen = tlen;
2079 					besta = (struct in6_ifaddr *)ifa;
2080 				}
2081 			} else
2082 				besta = (struct in6_ifaddr *)ifa;
2083 		}
2084 	}
2085 	if (besta)
2086 		return (besta);
2087 
2088 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2089 		if (ifa->ifa_addr->sa_family != AF_INET6)
2090 			continue;
2091 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2092 			continue; /* XXX: is there any case to allow anycast? */
2093 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2094 			continue; /* don't use this interface */
2095 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2096 			continue;
2097 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2098 			if (ip6_use_deprecated)
2099 				dep[1] = (struct in6_ifaddr *)ifa;
2100 			continue;
2101 		}
2102 
2103 		return (struct in6_ifaddr *)ifa;
2104 	}
2105 
2106 	/* use the last-resort values, that are, deprecated addresses */
2107 	if (dep[0])
2108 		return dep[0];
2109 	if (dep[1])
2110 		return dep[1];
2111 
2112 	return NULL;
2113 }
2114 
2115 /*
2116  * perform DAD when interface becomes IFF_UP.
2117  */
2118 void
2119 in6_if_up(ifp)
2120 	struct ifnet *ifp;
2121 {
2122 	struct ifaddr *ifa;
2123 	struct in6_ifaddr *ia;
2124 
2125 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2126 		if (ifa->ifa_addr->sa_family != AF_INET6)
2127 			continue;
2128 		ia = (struct in6_ifaddr *)ifa;
2129 		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2130 			/*
2131 			 * The TENTATIVE flag was likely set by hand
2132 			 * beforehand, implicitly indicating the need for DAD.
2133 			 * We may be able to skip the random delay in this
2134 			 * case, but we impose delays just in case.
2135 			 */
2136 			nd6_dad_start(ifa,
2137 			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2138 		}
2139 	}
2140 
2141 	/*
2142 	 * special cases, like 6to4, are handled in in6_ifattach
2143 	 */
2144 	in6_ifattach(ifp, NULL);
2145 }
2146 
2147 int
2148 in6if_do_dad(ifp)
2149 	struct ifnet *ifp;
2150 {
2151 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2152 		return (0);
2153 
2154 	switch (ifp->if_type) {
2155 #ifdef IFT_DUMMY
2156 	case IFT_DUMMY:
2157 #endif
2158 	case IFT_FAITH:
2159 		/*
2160 		 * These interfaces do not have the IFF_LOOPBACK flag,
2161 		 * but loop packets back.  We do not have to do DAD on such
2162 		 * interfaces.  We should even omit it, because loop-backed
2163 		 * NS would confuse the DAD procedure.
2164 		 */
2165 		return (0);
2166 	default:
2167 		/*
2168 		 * Our DAD routine requires the interface up and running.
2169 		 * However, some interfaces can be up before the RUNNING
2170 		 * status.  Additionaly, users may try to assign addresses
2171 		 * before the interface becomes up (or running).
2172 		 * We simply skip DAD in such a case as a work around.
2173 		 * XXX: we should rather mark "tentative" on such addresses,
2174 		 * and do DAD after the interface becomes ready.
2175 		 */
2176 		if (!((ifp->if_flags & IFF_UP) &&
2177 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2178 			return (0);
2179 
2180 		return (1);
2181 	}
2182 }
2183 
2184 /*
2185  * Calculate max IPv6 MTU through all the interfaces and store it
2186  * to in6_maxmtu.
2187  */
2188 void
2189 in6_setmaxmtu()
2190 {
2191 	unsigned long maxmtu = 0;
2192 	struct ifnet *ifp;
2193 
2194 	IFNET_RLOCK();
2195 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2196 		/* this function can be called during ifnet initialization */
2197 		if (!ifp->if_afdata[AF_INET6])
2198 			continue;
2199 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2200 		    IN6_LINKMTU(ifp) > maxmtu)
2201 			maxmtu = IN6_LINKMTU(ifp);
2202 	}
2203 	IFNET_RUNLOCK();
2204 	if (maxmtu)	     /* update only when maxmtu is positive */
2205 		in6_maxmtu = maxmtu;
2206 }
2207 
2208 /*
2209  * Provide the length of interface identifiers to be used for the link attached
2210  * to the given interface.  The length should be defined in "IPv6 over
2211  * xxx-link" document.  Note that address architecture might also define
2212  * the length for a particular set of address prefixes, regardless of the
2213  * link type.  As clarified in rfc2462bis, those two definitions should be
2214  * consistent, and those really are as of August 2004.
2215  */
2216 int
2217 in6_if2idlen(ifp)
2218 	struct ifnet *ifp;
2219 {
2220 	switch (ifp->if_type) {
2221 	case IFT_ETHER:		/* RFC2464 */
2222 #ifdef IFT_PROPVIRTUAL
2223 	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
2224 #endif
2225 #ifdef IFT_L2VLAN
2226 	case IFT_L2VLAN:	/* ditto */
2227 #endif
2228 #ifdef IFT_IEEE80211
2229 	case IFT_IEEE80211:	/* ditto */
2230 #endif
2231 #ifdef IFT_MIP
2232 	case IFT_MIP:	/* ditto */
2233 #endif
2234 		return (64);
2235 	case IFT_FDDI:		/* RFC2467 */
2236 		return (64);
2237 	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
2238 		return (64);
2239 	case IFT_PPP:		/* RFC2472 */
2240 		return (64);
2241 	case IFT_ARCNET:	/* RFC2497 */
2242 		return (64);
2243 	case IFT_FRELAY:	/* RFC2590 */
2244 		return (64);
2245 	case IFT_IEEE1394:	/* RFC3146 */
2246 		return (64);
2247 	case IFT_GIF:
2248 		return (64);	/* draft-ietf-v6ops-mech-v2-07 */
2249 	case IFT_LOOP:
2250 		return (64);	/* XXX: is this really correct? */
2251 	default:
2252 		/*
2253 		 * Unknown link type:
2254 		 * It might be controversial to use the today's common constant
2255 		 * of 64 for these cases unconditionally.  For full compliance,
2256 		 * we should return an error in this case.  On the other hand,
2257 		 * if we simply miss the standard for the link type or a new
2258 		 * standard is defined for a new link type, the IFID length
2259 		 * is very likely to be the common constant.  As a compromise,
2260 		 * we always use the constant, but make an explicit notice
2261 		 * indicating the "unknown" case.
2262 		 */
2263 		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2264 		return (64);
2265 	}
2266 }
2267 
2268 void *
2269 in6_domifattach(ifp)
2270 	struct ifnet *ifp;
2271 {
2272 	struct in6_ifextra *ext;
2273 
2274 	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2275 	bzero(ext, sizeof(*ext));
2276 
2277 	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2278 	    M_IFADDR, M_WAITOK);
2279 	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2280 
2281 	ext->icmp6_ifstat =
2282 	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2283 	    M_IFADDR, M_WAITOK);
2284 	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2285 
2286 	ext->nd_ifinfo = nd6_ifattach(ifp);
2287 	ext->scope6_id = scope6_ifattach(ifp);
2288 	return ext;
2289 }
2290 
2291 void
2292 in6_domifdetach(ifp, aux)
2293 	struct ifnet *ifp;
2294 	void *aux;
2295 {
2296 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2297 
2298 	scope6_ifdetach(ext->scope6_id);
2299 	nd6_ifdetach(ext->nd_ifinfo);
2300 	free(ext->in6_ifstat, M_IFADDR);
2301 	free(ext->icmp6_ifstat, M_IFADDR);
2302 	free(ext, M_IFADDR);
2303 }
2304 
2305 /*
2306  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
2307  * v4 mapped addr or v4 compat addr
2308  */
2309 void
2310 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2311 {
2312 	bzero(sin, sizeof(*sin));
2313 	sin->sin_len = sizeof(struct sockaddr_in);
2314 	sin->sin_family = AF_INET;
2315 	sin->sin_port = sin6->sin6_port;
2316 	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2317 }
2318 
2319 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2320 void
2321 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2322 {
2323 	bzero(sin6, sizeof(*sin6));
2324 	sin6->sin6_len = sizeof(struct sockaddr_in6);
2325 	sin6->sin6_family = AF_INET6;
2326 	sin6->sin6_port = sin->sin_port;
2327 	sin6->sin6_addr.s6_addr32[0] = 0;
2328 	sin6->sin6_addr.s6_addr32[1] = 0;
2329 	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2330 	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2331 }
2332 
2333 /* Convert sockaddr_in6 into sockaddr_in. */
2334 void
2335 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2336 {
2337 	struct sockaddr_in *sin_p;
2338 	struct sockaddr_in6 sin6;
2339 
2340 	/*
2341 	 * Save original sockaddr_in6 addr and convert it
2342 	 * to sockaddr_in.
2343 	 */
2344 	sin6 = *(struct sockaddr_in6 *)nam;
2345 	sin_p = (struct sockaddr_in *)nam;
2346 	in6_sin6_2_sin(sin_p, &sin6);
2347 }
2348 
2349 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2350 void
2351 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2352 {
2353 	struct sockaddr_in *sin_p;
2354 	struct sockaddr_in6 *sin6_p;
2355 
2356 	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2357 	       M_WAITOK);
2358 	sin_p = (struct sockaddr_in *)*nam;
2359 	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2360 	FREE(*nam, M_SONAME);
2361 	*nam = (struct sockaddr *)sin6_p;
2362 }
2363