xref: /freebsd/sys/netinet6/in6.c (revision dcc3a33188bceb5b6e819efdb9c5f72d059084b6)
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"
1196 	 * is not needed.
1197 	 */
1198 	{
1199 		struct rt_addrinfo info;
1200 		struct sockaddr_dl null_sdl;
1201 
1202 		bzero(&null_sdl, sizeof(null_sdl));
1203 		null_sdl.sdl_len = sizeof(null_sdl);
1204 		null_sdl.sdl_family = AF_LINK;
1205 		null_sdl.sdl_type = ia->ia_ifp->if_type;
1206 		null_sdl.sdl_index = ia->ia_ifp->if_index;
1207 		bzero(&info, sizeof(info));
1208 		info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC;
1209 		info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr;
1210 		info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1211 		error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1212 
1213 		if (error != 0)
1214 			log(LOG_INFO, "in6_purgeaddr: deletion failed\n");
1215 	}
1216 
1217 	/* stop DAD processing */
1218 	nd6_dad_stop(ifa);
1219 
1220 	IF_AFDATA_LOCK(ifp);
1221 	lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
1222 	    (struct sockaddr *)&ia->ia_addr);
1223 	IF_AFDATA_UNLOCK(ifp);
1224 
1225 	/*
1226 	 * initialize for rtmsg generation
1227 	 */
1228 	bzero(&gateway, sizeof(gateway));
1229 	gateway.sdl_len = sizeof(gateway);
1230 	gateway.sdl_family = AF_LINK;
1231 	gateway.sdl_nlen = 0;
1232 	gateway.sdl_alen = ifp->if_addrlen;
1233 	/* */
1234 	bzero(&rt0, sizeof(rt0));
1235 	rt0.rt_gateway = (struct sockaddr *)&gateway;
1236 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1237 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1238 	rt_mask(&rt0) = (struct sockaddr *)&mask;
1239 	rt_key(&rt0) = (struct sockaddr *)&addr;
1240 	rt0.rt_flags = RTF_HOST | RTF_STATIC;
1241 	rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
1242 
1243 	/*
1244 	 * leave from multicast groups we have joined for the interface
1245 	 */
1246 	while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1247 		LIST_REMOVE(imm, i6mm_chain);
1248 		in6_leavegroup(imm);
1249 	}
1250 
1251 	/*
1252 	 * remove the link-local all-nodes address
1253 	 */
1254 	bzero(&mltmask, sizeof(mltmask));
1255 	mltmask.sin6_len = sizeof(struct sockaddr_in6);
1256 	mltmask.sin6_family = AF_INET6;
1257 	mltmask.sin6_addr = in6mask32;
1258 
1259 	bzero(&mltaddr, sizeof(mltaddr));
1260 	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1261 	mltaddr.sin6_family = AF_INET6;
1262 	mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1263 
1264 	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1265 	    0)
1266 		goto cleanup;
1267 
1268 	rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1269 	if (rt != NULL && rt->rt_gateway != NULL &&
1270 	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1271 		    &ia->ia_addr.sin6_addr,
1272 		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1273 		/*
1274 		 * if no more IPv6 address exists on this interface
1275 		 * then remove the multicast address route
1276 		 */
1277 		if (ifa0 == NULL) {
1278 			memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1279 			       sizeof(mltaddr.sin6_addr));
1280 			RTFREE_LOCKED(rt);
1281 			error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1282 					  (struct sockaddr *)&ia->ia_addr,
1283 					  (struct sockaddr *)&mltmask, RTF_UP,
1284 					  (struct rtentry **)0);
1285 			if (error)
1286 				log(LOG_INFO, "in6_purgeaddr: link-local all-nodes"
1287 				    "multicast address deletion error\n");
1288 		} else {
1289 			/*
1290 			 * replace the gateway of the route
1291 			 */
1292 			struct sockaddr_in6 sa;
1293 
1294 			bzero(&sa, sizeof(sa));
1295 			sa.sin6_len = sizeof(struct sockaddr_in6);
1296 			sa.sin6_family = AF_INET6;
1297 			memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1298 			       sizeof(sa.sin6_addr));
1299 			in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1300 			memcpy(rt->rt_gateway, &sa, sizeof(sa));
1301 			RTFREE_LOCKED(rt);
1302 		}
1303 	} else {
1304 		if (rt != NULL)
1305 			RTFREE_LOCKED(rt);
1306 	}
1307 
1308 	/*
1309 	 * remove the node-local all-nodes address
1310 	 */
1311 	mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1312 	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1313 	    0)
1314 		goto cleanup;
1315 
1316 	rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1317 	if (rt != NULL && rt->rt_gateway != NULL &&
1318 	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1319 		    &ia->ia_addr.sin6_addr,
1320 		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1321 		/*
1322 		 * if no more IPv6 address exists on this interface
1323 		 * then remove the multicast address route
1324 		 */
1325 		if (ifa0 == NULL) {
1326 			memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1327 			       sizeof(mltaddr.sin6_addr));
1328 
1329 			RTFREE_LOCKED(rt);
1330 			error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1331 					  (struct sockaddr *)&ia->ia_addr,
1332 					  (struct sockaddr *)&mltmask, RTF_UP,
1333 					  (struct rtentry **)0);
1334 
1335 			if (error)
1336 				log(LOG_INFO, "in6_purgeaddr: node-local all-nodes"
1337 				    "multicast address deletion error\n");
1338 		} else {
1339 			/*
1340 			 * replace the gateway of the route
1341 			 */
1342 			struct sockaddr_in6 sa;
1343 
1344 			bzero(&sa, sizeof(sa));
1345 			sa.sin6_len = sizeof(struct sockaddr_in6);
1346 			sa.sin6_family = AF_INET6;
1347 			memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1348 			       sizeof(sa.sin6_addr));
1349 			in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1350 			memcpy(rt->rt_gateway, &sa, sizeof(sa));
1351 			RTFREE_LOCKED(rt);
1352 		}
1353 	} else {
1354 		if (rt != NULL)
1355 			RTFREE_LOCKED(rt);
1356 	}
1357 
1358 cleanup:
1359 
1360 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1361 	if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1362 		int error;
1363 		struct sockaddr *dstaddr;
1364 
1365 		/*
1366 		 * use the interface address if configuring an
1367 		 * interface address with a /128 prefix len
1368 		 */
1369 		if (ia->ia_dstaddr.sin6_family == AF_INET6)
1370 			dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1371 		else
1372 			dstaddr = (struct sockaddr *)&ia->ia_addr;
1373 
1374 		error = rtrequest(RTM_DELETE,
1375 		    (struct sockaddr *)dstaddr,
1376 		    (struct sockaddr *)&ia->ia_addr,
1377 		    (struct sockaddr *)&ia->ia_prefixmask,
1378 		    ia->ia_flags | RTF_HOST, NULL);
1379 		if (error != 0)
1380 			return;
1381 		ia->ia_flags &= ~IFA_ROUTE;
1382 	}
1383 	if (ifa0 != NULL)
1384 		ifa_free(ifa0);
1385 
1386 	in6_unlink_ifa(ia, ifp);
1387 }
1388 
1389 static void
1390 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1391 {
1392 	int	s = splnet();
1393 
1394 	IF_ADDR_LOCK(ifp);
1395 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1396 	IF_ADDR_UNLOCK(ifp);
1397 	ifa_free(&ia->ia_ifa);			/* if_addrhead */
1398 
1399 	/*
1400 	 * Defer the release of what might be the last reference to the
1401 	 * in6_ifaddr so that it can't be freed before the remainder of the
1402 	 * cleanup.
1403 	 */
1404 	IN6_IFADDR_WLOCK();
1405 	TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1406 	IN6_IFADDR_WUNLOCK();
1407 
1408 	/*
1409 	 * Release the reference to the base prefix.  There should be a
1410 	 * positive reference.
1411 	 */
1412 	if (ia->ia6_ndpr == NULL) {
1413 		nd6log((LOG_NOTICE,
1414 		    "in6_unlink_ifa: autoconf'ed address "
1415 		    "%p has no prefix\n", ia));
1416 	} else {
1417 		ia->ia6_ndpr->ndpr_refcnt--;
1418 		ia->ia6_ndpr = NULL;
1419 	}
1420 
1421 	/*
1422 	 * Also, if the address being removed is autoconf'ed, call
1423 	 * pfxlist_onlink_check() since the release might affect the status of
1424 	 * other (detached) addresses.
1425 	 */
1426 	if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1427 		pfxlist_onlink_check();
1428 	}
1429 	ifa_free(&ia->ia_ifa);			/* in6_ifaddrhead */
1430 	splx(s);
1431 }
1432 
1433 void
1434 in6_purgeif(struct ifnet *ifp)
1435 {
1436 	struct ifaddr *ifa, *nifa;
1437 
1438 	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1439 		if (ifa->ifa_addr->sa_family != AF_INET6)
1440 			continue;
1441 		in6_purgeaddr(ifa);
1442 	}
1443 
1444 	in6_ifdetach(ifp);
1445 }
1446 
1447 /*
1448  * SIOC[GAD]LIFADDR.
1449  *	SIOCGLIFADDR: get first address. (?)
1450  *	SIOCGLIFADDR with IFLR_PREFIX:
1451  *		get first address that matches the specified prefix.
1452  *	SIOCALIFADDR: add the specified address.
1453  *	SIOCALIFADDR with IFLR_PREFIX:
1454  *		add the specified prefix, filling hostid part from
1455  *		the first link-local address.  prefixlen must be <= 64.
1456  *	SIOCDLIFADDR: delete the specified address.
1457  *	SIOCDLIFADDR with IFLR_PREFIX:
1458  *		delete the first address that matches the specified prefix.
1459  * return values:
1460  *	EINVAL on invalid parameters
1461  *	EADDRNOTAVAIL on prefix match failed/specified address not found
1462  *	other values may be returned from in6_ioctl()
1463  *
1464  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1465  * this is to accomodate address naming scheme other than RFC2374,
1466  * in the future.
1467  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1468  * address encoding scheme. (see figure on page 8)
1469  */
1470 static int
1471 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1472     struct ifnet *ifp, struct thread *td)
1473 {
1474 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1475 	struct ifaddr *ifa;
1476 	struct sockaddr *sa;
1477 
1478 	/* sanity checks */
1479 	if (!data || !ifp) {
1480 		panic("invalid argument to in6_lifaddr_ioctl");
1481 		/* NOTREACHED */
1482 	}
1483 
1484 	switch (cmd) {
1485 	case SIOCGLIFADDR:
1486 		/* address must be specified on GET with IFLR_PREFIX */
1487 		if ((iflr->flags & IFLR_PREFIX) == 0)
1488 			break;
1489 		/* FALLTHROUGH */
1490 	case SIOCALIFADDR:
1491 	case SIOCDLIFADDR:
1492 		/* address must be specified on ADD and DELETE */
1493 		sa = (struct sockaddr *)&iflr->addr;
1494 		if (sa->sa_family != AF_INET6)
1495 			return EINVAL;
1496 		if (sa->sa_len != sizeof(struct sockaddr_in6))
1497 			return EINVAL;
1498 		/* XXX need improvement */
1499 		sa = (struct sockaddr *)&iflr->dstaddr;
1500 		if (sa->sa_family && sa->sa_family != AF_INET6)
1501 			return EINVAL;
1502 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1503 			return EINVAL;
1504 		break;
1505 	default: /* shouldn't happen */
1506 #if 0
1507 		panic("invalid cmd to in6_lifaddr_ioctl");
1508 		/* NOTREACHED */
1509 #else
1510 		return EOPNOTSUPP;
1511 #endif
1512 	}
1513 	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1514 		return EINVAL;
1515 
1516 	switch (cmd) {
1517 	case SIOCALIFADDR:
1518 	    {
1519 		struct in6_aliasreq ifra;
1520 		struct in6_addr *hostid = NULL;
1521 		int prefixlen;
1522 
1523 		ifa = NULL;
1524 		if ((iflr->flags & IFLR_PREFIX) != 0) {
1525 			struct sockaddr_in6 *sin6;
1526 
1527 			/*
1528 			 * hostid is to fill in the hostid part of the
1529 			 * address.  hostid points to the first link-local
1530 			 * address attached to the interface.
1531 			 */
1532 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1533 			if (!ifa)
1534 				return EADDRNOTAVAIL;
1535 			hostid = IFA_IN6(ifa);
1536 
1537 			/* prefixlen must be <= 64. */
1538 			if (64 < iflr->prefixlen)
1539 				return EINVAL;
1540 			prefixlen = iflr->prefixlen;
1541 
1542 			/* hostid part must be zero. */
1543 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1544 			if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1545 			    sin6->sin6_addr.s6_addr32[3] != 0) {
1546 				return EINVAL;
1547 			}
1548 		} else
1549 			prefixlen = iflr->prefixlen;
1550 
1551 		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1552 		bzero(&ifra, sizeof(ifra));
1553 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1554 
1555 		bcopy(&iflr->addr, &ifra.ifra_addr,
1556 		    ((struct sockaddr *)&iflr->addr)->sa_len);
1557 		if (hostid) {
1558 			/* fill in hostid part */
1559 			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1560 			    hostid->s6_addr32[2];
1561 			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1562 			    hostid->s6_addr32[3];
1563 		}
1564 
1565 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1566 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1567 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1568 			if (hostid) {
1569 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1570 				    hostid->s6_addr32[2];
1571 				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1572 				    hostid->s6_addr32[3];
1573 			}
1574 		}
1575 		if (ifa != NULL)
1576 			ifa_free(ifa);
1577 
1578 		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1579 		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1580 
1581 		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1582 		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1583 	    }
1584 	case SIOCGLIFADDR:
1585 	case SIOCDLIFADDR:
1586 	    {
1587 		struct in6_ifaddr *ia;
1588 		struct in6_addr mask, candidate, match;
1589 		struct sockaddr_in6 *sin6;
1590 		int cmp;
1591 
1592 		bzero(&mask, sizeof(mask));
1593 		if (iflr->flags & IFLR_PREFIX) {
1594 			/* lookup a prefix rather than address. */
1595 			in6_prefixlen2mask(&mask, iflr->prefixlen);
1596 
1597 			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1598 			bcopy(&sin6->sin6_addr, &match, sizeof(match));
1599 			match.s6_addr32[0] &= mask.s6_addr32[0];
1600 			match.s6_addr32[1] &= mask.s6_addr32[1];
1601 			match.s6_addr32[2] &= mask.s6_addr32[2];
1602 			match.s6_addr32[3] &= mask.s6_addr32[3];
1603 
1604 			/* if you set extra bits, that's wrong */
1605 			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1606 				return EINVAL;
1607 
1608 			cmp = 1;
1609 		} else {
1610 			if (cmd == SIOCGLIFADDR) {
1611 				/* on getting an address, take the 1st match */
1612 				cmp = 0;	/* XXX */
1613 			} else {
1614 				/* on deleting an address, do exact match */
1615 				in6_prefixlen2mask(&mask, 128);
1616 				sin6 = (struct sockaddr_in6 *)&iflr->addr;
1617 				bcopy(&sin6->sin6_addr, &match, sizeof(match));
1618 
1619 				cmp = 1;
1620 			}
1621 		}
1622 
1623 		IF_ADDR_LOCK(ifp);
1624 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1625 			if (ifa->ifa_addr->sa_family != AF_INET6)
1626 				continue;
1627 			if (!cmp)
1628 				break;
1629 
1630 			/*
1631 			 * XXX: this is adhoc, but is necessary to allow
1632 			 * a user to specify fe80::/64 (not /10) for a
1633 			 * link-local address.
1634 			 */
1635 			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1636 			in6_clearscope(&candidate);
1637 			candidate.s6_addr32[0] &= mask.s6_addr32[0];
1638 			candidate.s6_addr32[1] &= mask.s6_addr32[1];
1639 			candidate.s6_addr32[2] &= mask.s6_addr32[2];
1640 			candidate.s6_addr32[3] &= mask.s6_addr32[3];
1641 			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1642 				break;
1643 		}
1644 		IF_ADDR_UNLOCK(ifp);
1645 		if (!ifa)
1646 			return EADDRNOTAVAIL;
1647 		ia = ifa2ia6(ifa);
1648 
1649 		if (cmd == SIOCGLIFADDR) {
1650 			int error;
1651 
1652 			/* fill in the if_laddrreq structure */
1653 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1654 			error = sa6_recoverscope(
1655 			    (struct sockaddr_in6 *)&iflr->addr);
1656 			if (error != 0)
1657 				return (error);
1658 
1659 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1660 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1661 				    ia->ia_dstaddr.sin6_len);
1662 				error = sa6_recoverscope(
1663 				    (struct sockaddr_in6 *)&iflr->dstaddr);
1664 				if (error != 0)
1665 					return (error);
1666 			} else
1667 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1668 
1669 			iflr->prefixlen =
1670 			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1671 
1672 			iflr->flags = ia->ia6_flags;	/* XXX */
1673 
1674 			return 0;
1675 		} else {
1676 			struct in6_aliasreq ifra;
1677 
1678 			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1679 			bzero(&ifra, sizeof(ifra));
1680 			bcopy(iflr->iflr_name, ifra.ifra_name,
1681 			    sizeof(ifra.ifra_name));
1682 
1683 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1684 			    ia->ia_addr.sin6_len);
1685 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1686 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1687 				    ia->ia_dstaddr.sin6_len);
1688 			} else {
1689 				bzero(&ifra.ifra_dstaddr,
1690 				    sizeof(ifra.ifra_dstaddr));
1691 			}
1692 			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1693 			    ia->ia_prefixmask.sin6_len);
1694 
1695 			ifra.ifra_flags = ia->ia6_flags;
1696 			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1697 			    ifp, td);
1698 		}
1699 	    }
1700 	}
1701 
1702 	return EOPNOTSUPP;	/* just for safety */
1703 }
1704 
1705 /*
1706  * Initialize an interface's intetnet6 address
1707  * and routing table entry.
1708  */
1709 static int
1710 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1711     struct sockaddr_in6 *sin6, int newhost)
1712 {
1713 	int	error = 0, plen, ifacount = 0;
1714 	int	s = splimp();
1715 	struct ifaddr *ifa;
1716 
1717 	/*
1718 	 * Give the interface a chance to initialize
1719 	 * if this is its first address,
1720 	 * and to validate the address if necessary.
1721 	 */
1722 	IF_ADDR_LOCK(ifp);
1723 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1724 		if (ifa->ifa_addr->sa_family != AF_INET6)
1725 			continue;
1726 		ifacount++;
1727 	}
1728 	IF_ADDR_UNLOCK(ifp);
1729 
1730 	ia->ia_addr = *sin6;
1731 
1732 	if (ifacount <= 1 && ifp->if_ioctl) {
1733 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1734 		if (error) {
1735 			splx(s);
1736 			return (error);
1737 		}
1738 	}
1739 	splx(s);
1740 
1741 	ia->ia_ifa.ifa_metric = ifp->if_metric;
1742 
1743 	/* we could do in(6)_socktrim here, but just omit it at this moment. */
1744 
1745 	/*
1746 	 * Special case:
1747 	 * If a new destination address is specified for a point-to-point
1748 	 * interface, install a route to the destination as an interface
1749 	 * direct route.
1750 	 * XXX: the logic below rejects assigning multiple addresses on a p2p
1751 	 * interface that share the same destination.
1752 	 */
1753 	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1754 	if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1755 	    ia->ia_dstaddr.sin6_family == AF_INET6) {
1756 		int rtflags = RTF_UP | RTF_HOST;
1757 
1758 		error = rtrequest(RTM_ADD,
1759 		    (struct sockaddr *)&ia->ia_dstaddr,
1760 		    (struct sockaddr *)&ia->ia_addr,
1761 		    (struct sockaddr *)&ia->ia_prefixmask,
1762 		    ia->ia_flags | rtflags, NULL);
1763 		if (error != 0)
1764 			return (error);
1765 		ia->ia_flags |= IFA_ROUTE;
1766 	}
1767 
1768 	/*
1769 	 * add a loopback route to self
1770 	 */
1771 	if (!(ia->ia_flags & IFA_ROUTE)
1772 	    && (V_nd6_useloopback
1773 		|| (ifp->if_flags & IFF_LOOPBACK))) {
1774 		struct rt_addrinfo info;
1775 		struct rtentry *rt = NULL;
1776 		static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1777 
1778 		bzero(&info, sizeof(info));
1779 		info.rti_ifp = V_loif;
1780 		info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC;
1781 		info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr;
1782 		info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1783 		error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1784 
1785 		if (error == 0 && rt != NULL) {
1786 			RT_LOCK(rt);
1787 			((struct sockaddr_dl *)rt->rt_gateway)->sdl_type  =
1788 				ifp->if_type;
1789 			((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1790 				ifp->if_index;
1791 			RT_REMREF(rt);
1792 			RT_UNLOCK(rt);
1793 		} else if (error != 0)
1794 			log(LOG_INFO, "in6_ifinit: error = %d, insertion failed\n", error);
1795 	}
1796 
1797 	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1798 	if (newhost) {
1799 		struct llentry *ln;
1800 		struct rtentry rt;
1801 		struct sockaddr_dl gateway;
1802 		struct sockaddr_in6 mask, addr;
1803 
1804 		IF_AFDATA_LOCK(ifp);
1805 		ia->ia_ifa.ifa_rtrequest = NULL;
1806 
1807 		/* XXX QL
1808 		 * we need to report rt_newaddrmsg
1809 		 */
1810 		ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE),
1811 		    (struct sockaddr *)&ia->ia_addr);
1812 		IF_AFDATA_UNLOCK(ifp);
1813 		if (ln != NULL) {
1814 			ln->la_expire = 0;  /* for IPv6 this means permanent */
1815 			ln->ln_state = ND6_LLINFO_REACHABLE;
1816 			/*
1817 			 * initialize for rtmsg generation
1818 			 */
1819 			bzero(&gateway, sizeof(gateway));
1820 			gateway.sdl_len = sizeof(gateway);
1821 			gateway.sdl_family = AF_LINK;
1822 			gateway.sdl_nlen = 0;
1823 			gateway.sdl_alen = 6;
1824 			memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr));
1825 			/* */
1826 			LLE_WUNLOCK(ln);
1827 		}
1828 
1829 		bzero(&rt, sizeof(rt));
1830 		rt.rt_gateway = (struct sockaddr *)&gateway;
1831 		memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1832 		memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1833 		rt_mask(&rt) = (struct sockaddr *)&mask;
1834 		rt_key(&rt) = (struct sockaddr *)&addr;
1835 		rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
1836 		rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt);
1837 	}
1838 
1839 	return (error);
1840 }
1841 
1842 /*
1843  * Find an IPv6 interface link-local address specific to an interface.
1844  * ifaddr is returned referenced.
1845  */
1846 struct in6_ifaddr *
1847 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1848 {
1849 	struct ifaddr *ifa;
1850 
1851 	IF_ADDR_LOCK(ifp);
1852 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1853 		if (ifa->ifa_addr->sa_family != AF_INET6)
1854 			continue;
1855 		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1856 			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1857 			     ignoreflags) != 0)
1858 				continue;
1859 			ifa_ref(ifa);
1860 			break;
1861 		}
1862 	}
1863 	IF_ADDR_UNLOCK(ifp);
1864 
1865 	return ((struct in6_ifaddr *)ifa);
1866 }
1867 
1868 
1869 /*
1870  * find the internet address corresponding to a given interface and address.
1871  * ifaddr is returned referenced.
1872  */
1873 struct in6_ifaddr *
1874 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1875 {
1876 	struct ifaddr *ifa;
1877 
1878 	IF_ADDR_LOCK(ifp);
1879 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1880 		if (ifa->ifa_addr->sa_family != AF_INET6)
1881 			continue;
1882 		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1883 			ifa_ref(ifa);
1884 			break;
1885 		}
1886 	}
1887 	IF_ADDR_UNLOCK(ifp);
1888 
1889 	return ((struct in6_ifaddr *)ifa);
1890 }
1891 
1892 /*
1893  * Convert IP6 address to printable (loggable) representation. Caller
1894  * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1895  */
1896 static char digits[] = "0123456789abcdef";
1897 char *
1898 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1899 {
1900 	int i;
1901 	char *cp;
1902 	const u_int16_t *a = (const u_int16_t *)addr;
1903 	const u_int8_t *d;
1904 	int dcolon = 0, zero = 0;
1905 
1906 	cp = ip6buf;
1907 
1908 	for (i = 0; i < 8; i++) {
1909 		if (dcolon == 1) {
1910 			if (*a == 0) {
1911 				if (i == 7)
1912 					*cp++ = ':';
1913 				a++;
1914 				continue;
1915 			} else
1916 				dcolon = 2;
1917 		}
1918 		if (*a == 0) {
1919 			if (dcolon == 0 && *(a + 1) == 0) {
1920 				if (i == 0)
1921 					*cp++ = ':';
1922 				*cp++ = ':';
1923 				dcolon = 1;
1924 			} else {
1925 				*cp++ = '0';
1926 				*cp++ = ':';
1927 			}
1928 			a++;
1929 			continue;
1930 		}
1931 		d = (const u_char *)a;
1932 		/* Try to eliminate leading zeros in printout like in :0001. */
1933 		zero = 1;
1934 		*cp = digits[*d >> 4];
1935 		if (*cp != '0') {
1936 			zero = 0;
1937 			cp++;
1938 		}
1939 		*cp = digits[*d++ & 0xf];
1940 		if (zero == 0 || (*cp != '0')) {
1941 			zero = 0;
1942 			cp++;
1943 		}
1944 		*cp = digits[*d >> 4];
1945 		if (zero == 0 || (*cp != '0')) {
1946 			zero = 0;
1947 			cp++;
1948 		}
1949 		*cp++ = digits[*d & 0xf];
1950 		*cp++ = ':';
1951 		a++;
1952 	}
1953 	*--cp = '\0';
1954 	return (ip6buf);
1955 }
1956 
1957 int
1958 in6_localaddr(struct in6_addr *in6)
1959 {
1960 	struct in6_ifaddr *ia;
1961 
1962 	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1963 		return 1;
1964 
1965 	IN6_IFADDR_RLOCK();
1966 	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1967 		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1968 		    &ia->ia_prefixmask.sin6_addr)) {
1969 			IN6_IFADDR_RUNLOCK();
1970 			return 1;
1971 		}
1972 	}
1973 	IN6_IFADDR_RUNLOCK();
1974 
1975 	return (0);
1976 }
1977 
1978 int
1979 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1980 {
1981 	struct in6_ifaddr *ia;
1982 
1983 	IN6_IFADDR_RLOCK();
1984 	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1985 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1986 				       &sa6->sin6_addr) &&
1987 		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1988 			IN6_IFADDR_RUNLOCK();
1989 			return (1); /* true */
1990 		}
1991 
1992 		/* XXX: do we still have to go thru the rest of the list? */
1993 	}
1994 	IN6_IFADDR_RUNLOCK();
1995 
1996 	return (0);		/* false */
1997 }
1998 
1999 /*
2000  * return length of part which dst and src are equal
2001  * hard coding...
2002  */
2003 int
2004 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2005 {
2006 	int match = 0;
2007 	u_char *s = (u_char *)src, *d = (u_char *)dst;
2008 	u_char *lim = s + 16, r;
2009 
2010 	while (s < lim)
2011 		if ((r = (*d++ ^ *s++)) != 0) {
2012 			while (r < 128) {
2013 				match++;
2014 				r <<= 1;
2015 			}
2016 			break;
2017 		} else
2018 			match += 8;
2019 	return match;
2020 }
2021 
2022 /* XXX: to be scope conscious */
2023 int
2024 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2025 {
2026 	int bytelen, bitlen;
2027 
2028 	/* sanity check */
2029 	if (0 > len || len > 128) {
2030 		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2031 		    len);
2032 		return (0);
2033 	}
2034 
2035 	bytelen = len / 8;
2036 	bitlen = len % 8;
2037 
2038 	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2039 		return (0);
2040 	if (bitlen != 0 &&
2041 	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
2042 	    p2->s6_addr[bytelen] >> (8 - bitlen))
2043 		return (0);
2044 
2045 	return (1);
2046 }
2047 
2048 void
2049 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2050 {
2051 	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2052 	int bytelen, bitlen, i;
2053 
2054 	/* sanity check */
2055 	if (0 > len || len > 128) {
2056 		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2057 		    len);
2058 		return;
2059 	}
2060 
2061 	bzero(maskp, sizeof(*maskp));
2062 	bytelen = len / 8;
2063 	bitlen = len % 8;
2064 	for (i = 0; i < bytelen; i++)
2065 		maskp->s6_addr[i] = 0xff;
2066 	if (bitlen)
2067 		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2068 }
2069 
2070 /*
2071  * return the best address out of the same scope. if no address was
2072  * found, return the first valid address from designated IF.
2073  */
2074 struct in6_ifaddr *
2075 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2076 {
2077 	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
2078 	struct ifaddr *ifa;
2079 	struct in6_ifaddr *besta = 0;
2080 	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
2081 
2082 	dep[0] = dep[1] = NULL;
2083 
2084 	/*
2085 	 * We first look for addresses in the same scope.
2086 	 * If there is one, return it.
2087 	 * If two or more, return one which matches the dst longest.
2088 	 * If none, return one of global addresses assigned other ifs.
2089 	 */
2090 	IF_ADDR_LOCK(ifp);
2091 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2092 		if (ifa->ifa_addr->sa_family != AF_INET6)
2093 			continue;
2094 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2095 			continue; /* XXX: is there any case to allow anycast? */
2096 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2097 			continue; /* don't use this interface */
2098 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2099 			continue;
2100 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2101 			if (V_ip6_use_deprecated)
2102 				dep[0] = (struct in6_ifaddr *)ifa;
2103 			continue;
2104 		}
2105 
2106 		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2107 			/*
2108 			 * call in6_matchlen() as few as possible
2109 			 */
2110 			if (besta) {
2111 				if (blen == -1)
2112 					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2113 				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2114 				if (tlen > blen) {
2115 					blen = tlen;
2116 					besta = (struct in6_ifaddr *)ifa;
2117 				}
2118 			} else
2119 				besta = (struct in6_ifaddr *)ifa;
2120 		}
2121 	}
2122 	if (besta) {
2123 		ifa_ref(&besta->ia_ifa);
2124 		IF_ADDR_UNLOCK(ifp);
2125 		return (besta);
2126 	}
2127 	IF_ADDR_UNLOCK(ifp);
2128 
2129 	IN6_IFADDR_RLOCK();
2130 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2131 		if (ifa->ifa_addr->sa_family != AF_INET6)
2132 			continue;
2133 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2134 			continue; /* XXX: is there any case to allow anycast? */
2135 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2136 			continue; /* don't use this interface */
2137 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2138 			continue;
2139 		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2140 			if (V_ip6_use_deprecated)
2141 				dep[1] = (struct in6_ifaddr *)ifa;
2142 			continue;
2143 		}
2144 
2145 		if (ifa != NULL)
2146 			ifa_ref(ifa);
2147 		IN6_IFADDR_RUNLOCK();
2148 		return (struct in6_ifaddr *)ifa;
2149 	}
2150 	IN6_IFADDR_RUNLOCK();
2151 
2152 	/* use the last-resort values, that are, deprecated addresses */
2153 	if (dep[0])
2154 		return dep[0];
2155 	if (dep[1])
2156 		return dep[1];
2157 
2158 	return NULL;
2159 }
2160 
2161 /*
2162  * perform DAD when interface becomes IFF_UP.
2163  */
2164 void
2165 in6_if_up(struct ifnet *ifp)
2166 {
2167 	struct ifaddr *ifa;
2168 	struct in6_ifaddr *ia;
2169 
2170 	IF_ADDR_LOCK(ifp);
2171 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2172 		if (ifa->ifa_addr->sa_family != AF_INET6)
2173 			continue;
2174 		ia = (struct in6_ifaddr *)ifa;
2175 		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2176 			/*
2177 			 * The TENTATIVE flag was likely set by hand
2178 			 * beforehand, implicitly indicating the need for DAD.
2179 			 * We may be able to skip the random delay in this
2180 			 * case, but we impose delays just in case.
2181 			 */
2182 			nd6_dad_start(ifa,
2183 			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2184 		}
2185 	}
2186 	IF_ADDR_UNLOCK(ifp);
2187 
2188 	/*
2189 	 * special cases, like 6to4, are handled in in6_ifattach
2190 	 */
2191 	in6_ifattach(ifp, NULL);
2192 }
2193 
2194 int
2195 in6if_do_dad(struct ifnet *ifp)
2196 {
2197 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2198 		return (0);
2199 
2200 	switch (ifp->if_type) {
2201 #ifdef IFT_DUMMY
2202 	case IFT_DUMMY:
2203 #endif
2204 	case IFT_FAITH:
2205 		/*
2206 		 * These interfaces do not have the IFF_LOOPBACK flag,
2207 		 * but loop packets back.  We do not have to do DAD on such
2208 		 * interfaces.  We should even omit it, because loop-backed
2209 		 * NS would confuse the DAD procedure.
2210 		 */
2211 		return (0);
2212 	default:
2213 		/*
2214 		 * Our DAD routine requires the interface up and running.
2215 		 * However, some interfaces can be up before the RUNNING
2216 		 * status.  Additionaly, users may try to assign addresses
2217 		 * before the interface becomes up (or running).
2218 		 * We simply skip DAD in such a case as a work around.
2219 		 * XXX: we should rather mark "tentative" on such addresses,
2220 		 * and do DAD after the interface becomes ready.
2221 		 */
2222 		if (!((ifp->if_flags & IFF_UP) &&
2223 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2224 			return (0);
2225 
2226 		return (1);
2227 	}
2228 }
2229 
2230 /*
2231  * Calculate max IPv6 MTU through all the interfaces and store it
2232  * to in6_maxmtu.
2233  */
2234 void
2235 in6_setmaxmtu(void)
2236 {
2237 	unsigned long maxmtu = 0;
2238 	struct ifnet *ifp;
2239 
2240 	IFNET_RLOCK_NOSLEEP();
2241 	for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
2242 	    ifp = TAILQ_NEXT(ifp, if_list)) {
2243 		/* this function can be called during ifnet initialization */
2244 		if (!ifp->if_afdata[AF_INET6])
2245 			continue;
2246 		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2247 		    IN6_LINKMTU(ifp) > maxmtu)
2248 			maxmtu = IN6_LINKMTU(ifp);
2249 	}
2250 	IFNET_RUNLOCK_NOSLEEP();
2251 	if (maxmtu)	     /* update only when maxmtu is positive */
2252 		V_in6_maxmtu = maxmtu;
2253 }
2254 
2255 /*
2256  * Provide the length of interface identifiers to be used for the link attached
2257  * to the given interface.  The length should be defined in "IPv6 over
2258  * xxx-link" document.  Note that address architecture might also define
2259  * the length for a particular set of address prefixes, regardless of the
2260  * link type.  As clarified in rfc2462bis, those two definitions should be
2261  * consistent, and those really are as of August 2004.
2262  */
2263 int
2264 in6_if2idlen(struct ifnet *ifp)
2265 {
2266 	switch (ifp->if_type) {
2267 	case IFT_ETHER:		/* RFC2464 */
2268 #ifdef IFT_PROPVIRTUAL
2269 	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
2270 #endif
2271 #ifdef IFT_L2VLAN
2272 	case IFT_L2VLAN:	/* ditto */
2273 #endif
2274 #ifdef IFT_IEEE80211
2275 	case IFT_IEEE80211:	/* ditto */
2276 #endif
2277 #ifdef IFT_MIP
2278 	case IFT_MIP:	/* ditto */
2279 #endif
2280 		return (64);
2281 	case IFT_FDDI:		/* RFC2467 */
2282 		return (64);
2283 	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
2284 		return (64);
2285 	case IFT_PPP:		/* RFC2472 */
2286 		return (64);
2287 	case IFT_ARCNET:	/* RFC2497 */
2288 		return (64);
2289 	case IFT_FRELAY:	/* RFC2590 */
2290 		return (64);
2291 	case IFT_IEEE1394:	/* RFC3146 */
2292 		return (64);
2293 	case IFT_GIF:
2294 		return (64);	/* draft-ietf-v6ops-mech-v2-07 */
2295 	case IFT_LOOP:
2296 		return (64);	/* XXX: is this really correct? */
2297 	default:
2298 		/*
2299 		 * Unknown link type:
2300 		 * It might be controversial to use the today's common constant
2301 		 * of 64 for these cases unconditionally.  For full compliance,
2302 		 * we should return an error in this case.  On the other hand,
2303 		 * if we simply miss the standard for the link type or a new
2304 		 * standard is defined for a new link type, the IFID length
2305 		 * is very likely to be the common constant.  As a compromise,
2306 		 * we always use the constant, but make an explicit notice
2307 		 * indicating the "unknown" case.
2308 		 */
2309 		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2310 		return (64);
2311 	}
2312 }
2313 
2314 #include <sys/sysctl.h>
2315 
2316 struct in6_llentry {
2317 	struct llentry		base;
2318 	struct sockaddr_in6	l3_addr6;
2319 };
2320 
2321 static struct llentry *
2322 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2323 {
2324 	struct in6_llentry *lle;
2325 
2326 	lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2327 	    M_DONTWAIT | M_ZERO);
2328 	if (lle == NULL)		/* NB: caller generates msg */
2329 		return NULL;
2330 
2331 	callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE);
2332 	lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2333 	lle->base.lle_refcnt = 1;
2334 	LLE_LOCK_INIT(&lle->base);
2335 	return &lle->base;
2336 }
2337 
2338 /*
2339  * Deletes an address from the address table.
2340  * This function is called by the timer functions
2341  * such as arptimer() and nd6_llinfo_timer(), and
2342  * the caller does the locking.
2343  */
2344 static void
2345 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2346 {
2347 	LLE_WUNLOCK(lle);
2348 	LLE_LOCK_DESTROY(lle);
2349 	free(lle, M_LLTABLE);
2350 }
2351 
2352 static void
2353 in6_lltable_prefix_free(struct lltable *llt,
2354 			const struct sockaddr *prefix,
2355 			const struct sockaddr *mask)
2356 {
2357 	const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2358 	const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2359 	struct llentry *lle, *next;
2360 	register int i;
2361 
2362 	for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2363 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2364 			if (IN6_ARE_MASKED_ADDR_EQUAL(
2365 				    &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2366 				    &pfx->sin6_addr,
2367 				    &msk->sin6_addr)) {
2368 				callout_drain(&lle->la_timer);
2369 				LLE_WLOCK(lle);
2370 				llentry_free(lle);
2371 			}
2372 		}
2373 	}
2374 }
2375 
2376 static int
2377 in6_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr)
2378 {
2379 	struct rtentry *rt;
2380 	char ip6buf[INET6_ADDRSTRLEN];
2381 
2382 	KASSERT(l3addr->sa_family == AF_INET6,
2383 	    ("sin_family %d", l3addr->sa_family));
2384 
2385 	/* XXX rtalloc1 should take a const param */
2386 	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2387 	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2388 		struct ifaddr *ifa;
2389 		/*
2390 		 * Create an ND6 cache for an IPv6 neighbor
2391 		 * that is not covered by our own prefix.
2392 		 */
2393 		/* XXX ifaof_ifpforaddr should take a const param */
2394 		ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2395 		if (ifa != NULL) {
2396 			ifa_free(ifa);
2397 			if (rt != NULL)
2398 				RTFREE_LOCKED(rt);
2399 			return 0;
2400 		}
2401 		log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2402 		    ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2403 		if (rt != NULL)
2404 			RTFREE_LOCKED(rt);
2405 		return EINVAL;
2406 	}
2407 	RTFREE_LOCKED(rt);
2408 	return 0;
2409 }
2410 
2411 static struct llentry *
2412 in6_lltable_lookup(struct lltable *llt, u_int flags,
2413 	const struct sockaddr *l3addr)
2414 {
2415 	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2416 	struct ifnet *ifp = llt->llt_ifp;
2417 	struct llentry *lle;
2418 	struct llentries *lleh;
2419 	u_int hashkey;
2420 
2421 	IF_AFDATA_LOCK_ASSERT(ifp);
2422 	KASSERT(l3addr->sa_family == AF_INET6,
2423 	    ("sin_family %d", l3addr->sa_family));
2424 
2425 	hashkey = sin6->sin6_addr.s6_addr32[3];
2426 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2427 	LIST_FOREACH(lle, lleh, lle_next) {
2428 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2429 		if (lle->la_flags & LLE_DELETED)
2430 			continue;
2431 		if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2432 			 sizeof(struct in6_addr)) == 0)
2433 			break;
2434 	}
2435 
2436 	if (lle == NULL) {
2437 		if (!(flags & LLE_CREATE))
2438 			return (NULL);
2439 		/*
2440 		 * A route that covers the given address must have
2441 		 * been installed 1st because we are doing a resolution,
2442 		 * verify this.
2443 		 */
2444 		if (!(flags & LLE_IFADDR) &&
2445 		    in6_lltable_rtcheck(ifp, l3addr) != 0)
2446 			return NULL;
2447 
2448 		lle = in6_lltable_new(l3addr, flags);
2449 		if (lle == NULL) {
2450 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2451 			return NULL;
2452 		}
2453 		lle->la_flags = flags & ~LLE_CREATE;
2454 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2455 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2456 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
2457 		}
2458 
2459 		lle->lle_tbl  = llt;
2460 		lle->lle_head = lleh;
2461 		LIST_INSERT_HEAD(lleh, lle, lle_next);
2462 	} else if (flags & LLE_DELETE) {
2463 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2464 			LLE_WLOCK(lle);
2465 			lle->la_flags = LLE_DELETED;
2466 			LLE_WUNLOCK(lle);
2467 #ifdef DIAGNOSTICS
2468 			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
2469 #endif
2470 		}
2471 		lle = (void *)-1;
2472 	}
2473 	if (LLE_IS_VALID(lle)) {
2474 		if (flags & LLE_EXCLUSIVE)
2475 			LLE_WLOCK(lle);
2476 		else
2477 			LLE_RLOCK(lle);
2478 	}
2479 	return (lle);
2480 }
2481 
2482 static int
2483 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2484 {
2485 	struct ifnet *ifp = llt->llt_ifp;
2486 	struct llentry *lle;
2487 	/* XXX stack use */
2488 	struct {
2489 		struct rt_msghdr	rtm;
2490 		struct sockaddr_in6	sin6;
2491 		/*
2492 		 * ndp.c assumes that sdl is word aligned
2493 		 */
2494 #ifdef __LP64__
2495 		uint32_t		pad;
2496 #endif
2497 		struct sockaddr_dl	sdl;
2498 	} ndpc;
2499 	int i, error;
2500 
2501 	if (ifp->if_flags & IFF_LOOPBACK)
2502 		return 0;
2503 
2504 	LLTABLE_LOCK_ASSERT();
2505 
2506 	error = 0;
2507 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2508 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2509 			struct sockaddr_dl *sdl;
2510 
2511 			/* skip deleted or invalid entries */
2512 			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2513 				continue;
2514 			/* Skip if jailed and not a valid IP of the prison. */
2515 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2516 				continue;
2517 			/*
2518 			 * produce a msg made of:
2519 			 *  struct rt_msghdr;
2520 			 *  struct sockaddr_in6 (IPv6)
2521 			 *  struct sockaddr_dl;
2522 			 */
2523 			bzero(&ndpc, sizeof(ndpc));
2524 			ndpc.rtm.rtm_msglen = sizeof(ndpc);
2525 			ndpc.rtm.rtm_version = RTM_VERSION;
2526 			ndpc.rtm.rtm_type = RTM_GET;
2527 			ndpc.rtm.rtm_flags = RTF_UP;
2528 			ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2529 			ndpc.sin6.sin6_family = AF_INET6;
2530 			ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2531 			bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2532 
2533 			/* publish */
2534 			if (lle->la_flags & LLE_PUB)
2535 				ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2536 
2537 			sdl = &ndpc.sdl;
2538 			sdl->sdl_family = AF_LINK;
2539 			sdl->sdl_len = sizeof(*sdl);
2540 			sdl->sdl_alen = ifp->if_addrlen;
2541 			sdl->sdl_index = ifp->if_index;
2542 			sdl->sdl_type = ifp->if_type;
2543 			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2544 			ndpc.rtm.rtm_rmx.rmx_expire =
2545 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2546 			ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2547 			if (lle->la_flags & LLE_STATIC)
2548 				ndpc.rtm.rtm_flags |= RTF_STATIC;
2549 			ndpc.rtm.rtm_index = ifp->if_index;
2550 			error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2551 			if (error)
2552 				break;
2553 		}
2554 	}
2555 	return error;
2556 }
2557 
2558 void *
2559 in6_domifattach(struct ifnet *ifp)
2560 {
2561 	struct in6_ifextra *ext;
2562 
2563 	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2564 	bzero(ext, sizeof(*ext));
2565 
2566 	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2567 	    M_IFADDR, M_WAITOK);
2568 	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2569 
2570 	ext->icmp6_ifstat =
2571 	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2572 	    M_IFADDR, M_WAITOK);
2573 	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2574 
2575 	ext->nd_ifinfo = nd6_ifattach(ifp);
2576 	ext->scope6_id = scope6_ifattach(ifp);
2577 	ext->lltable = lltable_init(ifp, AF_INET6);
2578 	if (ext->lltable != NULL) {
2579 		ext->lltable->llt_new = in6_lltable_new;
2580 		ext->lltable->llt_free = in6_lltable_free;
2581 		ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2582 		ext->lltable->llt_rtcheck = in6_lltable_rtcheck;
2583 		ext->lltable->llt_lookup = in6_lltable_lookup;
2584 		ext->lltable->llt_dump = in6_lltable_dump;
2585 	}
2586 
2587 	ext->mld_ifinfo = mld_domifattach(ifp);
2588 
2589 	return ext;
2590 }
2591 
2592 void
2593 in6_domifdetach(struct ifnet *ifp, void *aux)
2594 {
2595 	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2596 
2597 	mld_domifdetach(ifp);
2598 	scope6_ifdetach(ext->scope6_id);
2599 	nd6_ifdetach(ext->nd_ifinfo);
2600 	lltable_free(ext->lltable);
2601 	free(ext->in6_ifstat, M_IFADDR);
2602 	free(ext->icmp6_ifstat, M_IFADDR);
2603 	free(ext, M_IFADDR);
2604 }
2605 
2606 /*
2607  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
2608  * v4 mapped addr or v4 compat addr
2609  */
2610 void
2611 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2612 {
2613 
2614 	bzero(sin, sizeof(*sin));
2615 	sin->sin_len = sizeof(struct sockaddr_in);
2616 	sin->sin_family = AF_INET;
2617 	sin->sin_port = sin6->sin6_port;
2618 	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2619 }
2620 
2621 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2622 void
2623 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2624 {
2625 	bzero(sin6, sizeof(*sin6));
2626 	sin6->sin6_len = sizeof(struct sockaddr_in6);
2627 	sin6->sin6_family = AF_INET6;
2628 	sin6->sin6_port = sin->sin_port;
2629 	sin6->sin6_addr.s6_addr32[0] = 0;
2630 	sin6->sin6_addr.s6_addr32[1] = 0;
2631 	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2632 	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2633 }
2634 
2635 /* Convert sockaddr_in6 into sockaddr_in. */
2636 void
2637 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2638 {
2639 	struct sockaddr_in *sin_p;
2640 	struct sockaddr_in6 sin6;
2641 
2642 	/*
2643 	 * Save original sockaddr_in6 addr and convert it
2644 	 * to sockaddr_in.
2645 	 */
2646 	sin6 = *(struct sockaddr_in6 *)nam;
2647 	sin_p = (struct sockaddr_in *)nam;
2648 	in6_sin6_2_sin(sin_p, &sin6);
2649 }
2650 
2651 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2652 void
2653 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2654 {
2655 	struct sockaddr_in *sin_p;
2656 	struct sockaddr_in6 *sin6_p;
2657 
2658 	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2659 	       M_WAITOK);
2660 	sin_p = (struct sockaddr_in *)*nam;
2661 	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2662 	free(*nam, M_SONAME);
2663 	*nam = (struct sockaddr *)sin6_p;
2664 }
2665