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