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