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