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