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