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