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 /* Check if this interface is a bridge member */
1239 if (ifp->if_bridge && bridge_member_ifaddrs_p &&
1240 !bridge_member_ifaddrs_p()) {
1241 error = EINVAL;
1242 goto out;
1243 }
1244
1245 /*
1246 * first, make or update the interface address structure,
1247 * and link it to the list.
1248 */
1249 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
1250 goto out;
1251 if (ia != NULL) {
1252 if (ia->ia_ifa.ifa_carp)
1253 (*carp_detach_p)(&ia->ia_ifa, true);
1254 ifa_free(&ia->ia_ifa);
1255 }
1256 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) == NULL) {
1257 /*
1258 * this can happen when the user specify the 0 valid
1259 * lifetime.
1260 */
1261 return (0);
1262 }
1263
1264 if (ifra->ifra_vhid > 0) {
1265 if (carp_attach_p != NULL)
1266 error = (*carp_attach_p)(&ia->ia_ifa,
1267 ifra->ifra_vhid);
1268 else
1269 error = EPROTONOSUPPORT;
1270 if (error)
1271 goto out;
1272 else
1273 carp_attached = 1;
1274 }
1275
1276 /*
1277 * then, make the prefix on-link on the interface.
1278 * XXX: we'd rather create the prefix before the address, but
1279 * we need at least one address to install the corresponding
1280 * interface route, so we configure the address first.
1281 */
1282
1283 /*
1284 * convert mask to prefix length (prefixmask has already
1285 * been validated in in6_update_ifa().
1286 */
1287 bzero(&pr0, sizeof(pr0));
1288 pr0.ndpr_ifp = ifp;
1289 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
1290 NULL);
1291 if (pr0.ndpr_plen == 128) {
1292 /* we don't need to install a host route. */
1293 goto aifaddr_out;
1294 }
1295 pr0.ndpr_prefix = ifra->ifra_addr;
1296 /* apply the mask for safety. */
1297 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr,
1298 &ifra->ifra_prefixmask.sin6_addr);
1299
1300 /*
1301 * XXX: since we don't have an API to set prefix (not address)
1302 * lifetimes, we just use the same lifetimes as addresses.
1303 * The (temporarily) installed lifetimes can be overridden by
1304 * later advertised RAs (when accept_rtadv is non 0), which is
1305 * an intended behavior.
1306 */
1307 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
1308 pr0.ndpr_raf_auto =
1309 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
1310 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
1311 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
1312
1313 /* add the prefix if not yet. */
1314 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
1315 /*
1316 * nd6_prelist_add will install the corresponding
1317 * interface route.
1318 */
1319 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
1320 if (carp_attached)
1321 (*carp_detach_p)(&ia->ia_ifa, false);
1322 goto out;
1323 }
1324 }
1325
1326 /* relate the address to the prefix */
1327 if (ia->ia6_ndpr == NULL) {
1328 ia->ia6_ndpr = pr;
1329 pr->ndpr_addrcnt++;
1330
1331 /*
1332 * If this is the first autoconf address from the
1333 * prefix, create a temporary address as well
1334 * (when required).
1335 */
1336 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
1337 V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) {
1338 int e;
1339 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
1340 log(LOG_NOTICE, "in6_control: failed "
1341 "to create a temporary address, "
1342 "errno=%d\n", e);
1343 }
1344 }
1345 }
1346 nd6_prefix_rele(pr);
1347
1348 /*
1349 * this might affect the status of autoconfigured addresses,
1350 * that is, this address might make other addresses detached.
1351 */
1352 pfxlist_onlink_check();
1353
1354 aifaddr_out:
1355 /*
1356 * Try to clear the flag when a new IPv6 address is added
1357 * onto an IFDISABLED interface and it succeeds.
1358 */
1359 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
1360 struct in6_ndireq nd;
1361
1362 memset(&nd, 0, sizeof(nd));
1363 nd.ndi.flags = ND_IFINFO(ifp)->flags;
1364 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
1365 if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0)
1366 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
1367 "SIOCSIFINFO_FLAGS for -ifdisabled "
1368 "failed.");
1369 /*
1370 * Ignore failure of clearing the flag intentionally.
1371 * The failure means address duplication was detected.
1372 */
1373 }
1374 error = 0;
1375
1376 out:
1377 if (ia != NULL)
1378 ifa_free(&ia->ia_ifa);
1379 return (error);
1380 }
1381
1382 void
in6_purgeaddr(struct ifaddr * ifa)1383 in6_purgeaddr(struct ifaddr *ifa)
1384 {
1385 struct ifnet *ifp = ifa->ifa_ifp;
1386 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1387 struct in6_multi_mship *imm;
1388 int error;
1389
1390 if (ifa->ifa_carp)
1391 (*carp_detach_p)(ifa, false);
1392
1393 /*
1394 * Remove the loopback route to the interface address.
1395 * The check for the current setting of "nd6_useloopback"
1396 * is not needed.
1397 */
1398 if (ia->ia_flags & IFA_RTSELF) {
1399 error = ifa_del_loopback_route((struct ifaddr *)ia,
1400 (struct sockaddr *)&ia->ia_addr);
1401 if (error == 0)
1402 ia->ia_flags &= ~IFA_RTSELF;
1403 }
1404
1405 /* stop DAD processing */
1406 nd6_dad_stop(ifa);
1407
1408 /* Leave multicast groups. */
1409 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1410 LIST_REMOVE(imm, i6mm_chain);
1411 if (imm->i6mm_maddr != NULL)
1412 in6_leavegroup(imm->i6mm_maddr, NULL);
1413 free(imm, M_IP6MADDR);
1414 }
1415 /* Check if we need to remove p2p route */
1416 if ((ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) {
1417 error = in6_handle_dstaddr_rtrequest(RTM_DELETE, ia);
1418 if (error != 0)
1419 log(LOG_INFO, "%s: err=%d, destination address delete "
1420 "failed\n", __func__, error);
1421 ia->ia_flags &= ~IFA_ROUTE;
1422 }
1423
1424 in6_newaddrmsg(ia, RTM_DELETE);
1425 in6_unlink_ifa(ia, ifp);
1426 }
1427
1428 /*
1429 * Removes @ia from the corresponding interfaces and unlinks corresponding
1430 * prefix if no addresses are using it anymore.
1431 */
1432 void
in6_purgeifaddr(struct in6_ifaddr * ia)1433 in6_purgeifaddr(struct in6_ifaddr *ia)
1434 {
1435 struct nd_prefix *pr;
1436
1437 /*
1438 * If the address being deleted is the only one that owns
1439 * the corresponding prefix, expire the prefix as well.
1440 * XXX: theoretically, we don't have to worry about such
1441 * relationship, since we separate the address management
1442 * and the prefix management. We do this, however, to provide
1443 * as much backward compatibility as possible in terms of
1444 * the ioctl operation.
1445 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1446 */
1447 pr = ia->ia6_ndpr;
1448 in6_purgeaddr(&ia->ia_ifa);
1449 if (pr != NULL && pr->ndpr_addrcnt == 0) {
1450 ND6_WLOCK();
1451 nd6_prefix_unlink(pr, NULL);
1452 ND6_WUNLOCK();
1453 nd6_prefix_del(pr);
1454 }
1455 }
1456
1457
1458 static void
in6_unlink_ifa(struct in6_ifaddr * ia,struct ifnet * ifp)1459 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1460 {
1461 char ip6buf[INET6_ADDRSTRLEN];
1462 int remove_lle;
1463
1464 IF_ADDR_WLOCK(ifp);
1465 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
1466 IF_ADDR_WUNLOCK(ifp);
1467 ifa_free(&ia->ia_ifa); /* if_addrhead */
1468
1469 /*
1470 * Defer the release of what might be the last reference to the
1471 * in6_ifaddr so that it can't be freed before the remainder of the
1472 * cleanup.
1473 */
1474 IN6_IFADDR_WLOCK();
1475 CK_STAILQ_REMOVE(&V_in6_ifaddrhead, ia, in6_ifaddr, ia_link);
1476 CK_LIST_REMOVE(ia, ia6_hash);
1477 IN6_IFADDR_WUNLOCK();
1478
1479 /*
1480 * Release the reference to the base prefix. There should be a
1481 * positive reference.
1482 */
1483 remove_lle = 0;
1484 if (ia->ia6_ndpr == NULL) {
1485 nd6log((LOG_NOTICE,
1486 "in6_unlink_ifa: autoconf'ed address "
1487 "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
1488 } else {
1489 ia->ia6_ndpr->ndpr_addrcnt--;
1490 /* Do not delete lles within prefix if refcont != 0 */
1491 if (ia->ia6_ndpr->ndpr_addrcnt == 0)
1492 remove_lle = 1;
1493 ia->ia6_ndpr = NULL;
1494 }
1495
1496 nd6_rem_ifa_lle(ia, remove_lle);
1497
1498 /*
1499 * Also, if the address being removed is autoconf'ed, call
1500 * pfxlist_onlink_check() since the release might affect the status of
1501 * other (detached) addresses.
1502 */
1503 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1504 pfxlist_onlink_check();
1505 }
1506 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1507 }
1508
1509 /*
1510 * Notifies other subsystems about address change/arrival:
1511 * 1) Notifies device handler on the first IPv6 address assignment
1512 * 2) Handle routing table changes for P2P links and route
1513 * 3) Handle routing table changes for address host route
1514 */
1515 static int
in6_notify_ifa(struct ifnet * ifp,struct in6_ifaddr * ia,struct in6_aliasreq * ifra,int hostIsNew)1516 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia,
1517 struct in6_aliasreq *ifra, int hostIsNew)
1518 {
1519 int error = 0, ifacount = 0;
1520 struct ifaddr *ifa;
1521 struct sockaddr_in6 *pdst;
1522 char ip6buf[INET6_ADDRSTRLEN];
1523
1524 /*
1525 * Give the interface a chance to initialize
1526 * if this is its first address,
1527 */
1528 if (hostIsNew != 0) {
1529 struct epoch_tracker et;
1530
1531 NET_EPOCH_ENTER(et);
1532 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1533 if (ifa->ifa_addr->sa_family != AF_INET6)
1534 continue;
1535 ifacount++;
1536 }
1537 NET_EPOCH_EXIT(et);
1538 }
1539
1540 if (ifacount <= 1 && ifp->if_ioctl) {
1541 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1542 if (error)
1543 goto done;
1544 }
1545
1546 /*
1547 * If a new destination address is specified, scrub the old one and
1548 * install the new destination. Note that the interface must be
1549 * p2p or loopback.
1550 */
1551 pdst = &ifra->ifra_dstaddr;
1552 if (pdst->sin6_family == AF_INET6 &&
1553 !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1554 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1555 (in6_handle_dstaddr_rtrequest(RTM_DELETE, ia) != 0)) {
1556 nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
1557 "remove a route to the old destination: %s\n",
1558 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1559 /* proceed anyway... */
1560 } else
1561 ia->ia_flags &= ~IFA_ROUTE;
1562 ia->ia_dstaddr = *pdst;
1563 }
1564
1565 /*
1566 * If a new destination address is specified for a point-to-point
1567 * interface, install a route to the destination as an interface
1568 * direct route.
1569 * XXX: the logic below rejects assigning multiple addresses on a p2p
1570 * interface that share the same destination.
1571 */
1572 if (!(ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) {
1573 error = in6_handle_dstaddr_rtrequest(RTM_ADD, ia);
1574 if (error)
1575 goto done;
1576 ia->ia_flags |= IFA_ROUTE;
1577 }
1578
1579 /*
1580 * add a loopback route to self if not exists
1581 */
1582 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1583 error = ifa_add_loopback_route((struct ifaddr *)ia,
1584 (struct sockaddr *)&ia->ia_addr);
1585 if (error == 0)
1586 ia->ia_flags |= IFA_RTSELF;
1587 }
1588 done:
1589 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1590 "Invoking IPv6 network device address event may sleep");
1591
1592 ifa_ref(&ia->ia_ifa);
1593 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
1594 IFADDR_EVENT_ADD);
1595 ifa_free(&ia->ia_ifa);
1596
1597 return (error);
1598 }
1599
1600 /*
1601 * Find an IPv6 interface link-local address specific to an interface.
1602 * ifaddr is returned referenced.
1603 */
1604 struct in6_ifaddr *
in6ifa_ifpforlinklocal(struct ifnet * ifp,int ignoreflags)1605 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1606 {
1607 struct ifaddr *ifa;
1608
1609 NET_EPOCH_ASSERT();
1610
1611 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1612 if (ifa->ifa_addr->sa_family != AF_INET6)
1613 continue;
1614 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1615 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1616 ignoreflags) != 0)
1617 continue;
1618 ifa_ref(ifa);
1619 break;
1620 }
1621 }
1622
1623 return ((struct in6_ifaddr *)ifa);
1624 }
1625
1626 /*
1627 * find the interface address corresponding to a given IPv6 address.
1628 * ifaddr is returned referenced if @referenced flag is set.
1629 */
1630 struct in6_ifaddr *
in6ifa_ifwithaddr(const struct in6_addr * addr,uint32_t zoneid,bool referenced)1631 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid, bool referenced)
1632 {
1633 struct rm_priotracker in6_ifa_tracker;
1634 struct in6_ifaddr *ia;
1635
1636 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1637 CK_LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
1638 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
1639 if (zoneid != 0 &&
1640 zoneid != ia->ia_addr.sin6_scope_id)
1641 continue;
1642 if (referenced)
1643 ifa_ref(&ia->ia_ifa);
1644 break;
1645 }
1646 }
1647 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1648 return (ia);
1649 }
1650
1651 /*
1652 * find the internet address corresponding to a given interface and address.
1653 * ifaddr is returned referenced.
1654 */
1655 struct in6_ifaddr *
in6ifa_ifpwithaddr(struct ifnet * ifp,const struct in6_addr * addr)1656 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr)
1657 {
1658 struct epoch_tracker et;
1659 struct ifaddr *ifa;
1660
1661 NET_EPOCH_ENTER(et);
1662 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1663 if (ifa->ifa_addr->sa_family != AF_INET6)
1664 continue;
1665 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1666 ifa_ref(ifa);
1667 break;
1668 }
1669 }
1670 NET_EPOCH_EXIT(et);
1671
1672 return ((struct in6_ifaddr *)ifa);
1673 }
1674
1675 /*
1676 * Find a link-local scoped address on ifp and return it if any.
1677 */
1678 struct in6_ifaddr *
in6ifa_llaonifp(struct ifnet * ifp)1679 in6ifa_llaonifp(struct ifnet *ifp)
1680 {
1681 struct epoch_tracker et;
1682 struct sockaddr_in6 *sin6;
1683 struct ifaddr *ifa;
1684
1685 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1686 return (NULL);
1687 NET_EPOCH_ENTER(et);
1688 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1689 if (ifa->ifa_addr->sa_family != AF_INET6)
1690 continue;
1691 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1692 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1693 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1694 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1695 break;
1696 }
1697 NET_EPOCH_EXIT(et);
1698
1699 return ((struct in6_ifaddr *)ifa);
1700 }
1701
1702 /*
1703 * Convert IP6 address to printable (loggable) representation. Caller
1704 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1705 */
1706 static char digits[] = "0123456789abcdef";
1707 char *
ip6_sprintf(char * ip6buf,const struct in6_addr * addr)1708 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1709 {
1710 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1711 char *cp;
1712 const u_int16_t *a = (const u_int16_t *)addr;
1713 const u_int8_t *d;
1714 int dcolon = 0, zero = 0;
1715
1716 cp = ip6buf;
1717
1718 for (i = 0; i < 8; i++) {
1719 if (*(a + i) == 0) {
1720 cnt++;
1721 if (cnt == 1)
1722 idx = i;
1723 }
1724 else if (maxcnt < cnt) {
1725 maxcnt = cnt;
1726 index = idx;
1727 cnt = 0;
1728 }
1729 }
1730 if (maxcnt < cnt) {
1731 maxcnt = cnt;
1732 index = idx;
1733 }
1734
1735 for (i = 0; i < 8; i++) {
1736 if (dcolon == 1) {
1737 if (*a == 0) {
1738 if (i == 7)
1739 *cp++ = ':';
1740 a++;
1741 continue;
1742 } else
1743 dcolon = 2;
1744 }
1745 if (*a == 0) {
1746 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1747 if (i == 0)
1748 *cp++ = ':';
1749 *cp++ = ':';
1750 dcolon = 1;
1751 } else {
1752 *cp++ = '0';
1753 *cp++ = ':';
1754 }
1755 a++;
1756 continue;
1757 }
1758 d = (const u_char *)a;
1759 /* Try to eliminate leading zeros in printout like in :0001. */
1760 zero = 1;
1761 *cp = digits[*d >> 4];
1762 if (*cp != '0') {
1763 zero = 0;
1764 cp++;
1765 }
1766 *cp = digits[*d++ & 0xf];
1767 if (zero == 0 || (*cp != '0')) {
1768 zero = 0;
1769 cp++;
1770 }
1771 *cp = digits[*d >> 4];
1772 if (zero == 0 || (*cp != '0')) {
1773 zero = 0;
1774 cp++;
1775 }
1776 *cp++ = digits[*d & 0xf];
1777 *cp++ = ':';
1778 a++;
1779 }
1780 *--cp = '\0';
1781 return (ip6buf);
1782 }
1783
1784 int
in6_localaddr(struct in6_addr * in6)1785 in6_localaddr(struct in6_addr *in6)
1786 {
1787 struct rm_priotracker in6_ifa_tracker;
1788 struct in6_ifaddr *ia;
1789
1790 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1791 return 1;
1792
1793 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1794 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1795 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1796 &ia->ia_prefixmask.sin6_addr)) {
1797 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1798 return 1;
1799 }
1800 }
1801 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1802
1803 return (0);
1804 }
1805
1806 /*
1807 * Return 1 if an internet address is for the local host and configured
1808 * on one of its interfaces.
1809 */
1810 int
in6_localip(struct in6_addr * in6)1811 in6_localip(struct in6_addr *in6)
1812 {
1813 struct rm_priotracker in6_ifa_tracker;
1814 struct in6_ifaddr *ia;
1815
1816 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1817 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1818 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
1819 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1820 return (1);
1821 }
1822 }
1823 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1824 return (0);
1825 }
1826
1827 /*
1828 * Like in6_localip(), but FIB-aware and carp(4)-aware.
1829 */
1830 bool
in6_localip_fib(struct in6_addr * in6,uint16_t fib)1831 in6_localip_fib(struct in6_addr *in6, uint16_t fib)
1832 {
1833 struct rm_priotracker in6_ifa_tracker;
1834 struct in6_ifaddr *ia;
1835
1836 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1837 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1838 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr) &&
1839 (ia->ia_ifa.ifa_carp == NULL ||
1840 carp_master_p(&ia->ia_ifa)) &&
1841 ia->ia_ifa.ifa_ifp->if_fib == fib) {
1842 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1843 return (true);
1844 }
1845 }
1846 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1847 return (false);
1848 }
1849
1850 /*
1851 * Return 1 if an internet address is configured on an interface.
1852 */
1853 int
in6_ifhasaddr(struct ifnet * ifp,struct in6_addr * addr)1854 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
1855 {
1856 struct in6_addr in6;
1857 struct ifaddr *ifa;
1858 struct in6_ifaddr *ia6;
1859
1860 NET_EPOCH_ASSERT();
1861
1862 in6 = *addr;
1863 if (in6_clearscope(&in6))
1864 return (0);
1865 in6_setscope(&in6, ifp, NULL);
1866
1867 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1868 if (ifa->ifa_addr->sa_family != AF_INET6)
1869 continue;
1870 ia6 = (struct in6_ifaddr *)ifa;
1871 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6))
1872 return (1);
1873 }
1874
1875 return (0);
1876 }
1877
1878 int
in6_is_addr_deprecated(struct sockaddr_in6 * sa6)1879 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1880 {
1881 struct rm_priotracker in6_ifa_tracker;
1882 struct in6_ifaddr *ia;
1883
1884 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1885 CK_LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
1886 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
1887 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
1888 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1889 return (1); /* true */
1890 }
1891 break;
1892 }
1893 }
1894 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1895
1896 return (0); /* false */
1897 }
1898
1899 /*
1900 * return length of part which dst and src are equal
1901 * hard coding...
1902 */
1903 int
in6_matchlen(struct in6_addr * src,struct in6_addr * dst)1904 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1905 {
1906 int match = 0;
1907 u_char *s = (u_char *)src, *d = (u_char *)dst;
1908 u_char *lim = s + 16, r;
1909
1910 while (s < lim)
1911 if ((r = (*d++ ^ *s++)) != 0) {
1912 while (r < 128) {
1913 match++;
1914 r <<= 1;
1915 }
1916 break;
1917 } else
1918 match += 8;
1919 return match;
1920 }
1921
1922 /* XXX: to be scope conscious */
1923 int
in6_are_prefix_equal(struct in6_addr * p1,struct in6_addr * p2,int len)1924 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1925 {
1926 int bytelen, bitlen;
1927
1928 /* sanity check */
1929 if (0 > len || len > 128) {
1930 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1931 len);
1932 return (0);
1933 }
1934
1935 bytelen = len / 8;
1936 bitlen = len % 8;
1937
1938 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1939 return (0);
1940 if (bitlen != 0 &&
1941 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1942 p2->s6_addr[bytelen] >> (8 - bitlen))
1943 return (0);
1944
1945 return (1);
1946 }
1947
1948 void
in6_prefixlen2mask(struct in6_addr * maskp,int len)1949 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1950 {
1951 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1952 int bytelen, bitlen, i;
1953
1954 /* sanity check */
1955 if (0 > len || len > 128) {
1956 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1957 len);
1958 return;
1959 }
1960
1961 bzero(maskp, sizeof(*maskp));
1962 bytelen = len / 8;
1963 bitlen = len % 8;
1964 for (i = 0; i < bytelen; i++)
1965 maskp->s6_addr[i] = 0xff;
1966 if (bitlen)
1967 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1968 }
1969
1970 /*
1971 * return the best address out of the same scope. if no address was
1972 * found, return the first valid address from designated IF.
1973 */
1974 struct in6_ifaddr *
in6_ifawithifp(struct ifnet * ifp,struct in6_addr * dst)1975 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1976 {
1977 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1978 struct ifaddr *ifa;
1979 struct in6_ifaddr *besta = NULL;
1980 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1981
1982 NET_EPOCH_ASSERT();
1983
1984 dep[0] = dep[1] = NULL;
1985
1986 /*
1987 * We first look for addresses in the same scope.
1988 * If there is one, return it.
1989 * If two or more, return one which matches the dst longest.
1990 * If none, return one of global addresses assigned other ifs.
1991 */
1992 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1993 if (ifa->ifa_addr->sa_family != AF_INET6)
1994 continue;
1995 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1996 continue; /* XXX: is there any case to allow anycast? */
1997 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1998 continue; /* don't use this interface */
1999 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2000 continue;
2001 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2002 if (V_ip6_use_deprecated)
2003 dep[0] = (struct in6_ifaddr *)ifa;
2004 continue;
2005 }
2006
2007 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2008 /*
2009 * call in6_matchlen() as few as possible
2010 */
2011 if (besta) {
2012 if (blen == -1)
2013 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2014 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2015 if (tlen > blen) {
2016 blen = tlen;
2017 besta = (struct in6_ifaddr *)ifa;
2018 }
2019 } else
2020 besta = (struct in6_ifaddr *)ifa;
2021 }
2022 }
2023 if (besta)
2024 return (besta);
2025
2026 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2027 if (ifa->ifa_addr->sa_family != AF_INET6)
2028 continue;
2029 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2030 continue; /* XXX: is there any case to allow anycast? */
2031 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2032 continue; /* don't use this interface */
2033 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2034 continue;
2035 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2036 if (V_ip6_use_deprecated)
2037 dep[1] = (struct in6_ifaddr *)ifa;
2038 continue;
2039 }
2040
2041 return (struct in6_ifaddr *)ifa;
2042 }
2043
2044 /* use the last-resort values, that are, deprecated addresses */
2045 if (dep[0])
2046 return dep[0];
2047 if (dep[1])
2048 return dep[1];
2049
2050 return NULL;
2051 }
2052
2053 /*
2054 * perform DAD when interface becomes IFF_UP.
2055 */
2056 void
in6_if_up(struct ifnet * ifp)2057 in6_if_up(struct ifnet *ifp)
2058 {
2059 struct epoch_tracker et;
2060 struct ifaddr *ifa;
2061 struct in6_ifaddr *ia;
2062
2063 NET_EPOCH_ENTER(et);
2064 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2065 if (ifa->ifa_addr->sa_family != AF_INET6)
2066 continue;
2067 ia = (struct in6_ifaddr *)ifa;
2068 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2069 /*
2070 * The TENTATIVE flag was likely set by hand
2071 * beforehand, implicitly indicating the need for DAD.
2072 * We may be able to skip the random delay in this
2073 * case, but we impose delays just in case.
2074 */
2075 nd6_dad_start(ifa,
2076 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2077 }
2078 }
2079 NET_EPOCH_EXIT(et);
2080
2081 /*
2082 * special cases, like 6to4, are handled in in6_ifattach
2083 */
2084 in6_ifattach(ifp, NULL);
2085 }
2086
2087 static void
in6_ifevent(void * arg __unused,struct ifnet * ifp,int event)2088 in6_ifevent(void *arg __unused, struct ifnet *ifp, int event)
2089 {
2090 if (event == IFNET_EVENT_UP)
2091 in6_if_up(ifp);
2092 }
2093
2094 static void
in6_init(void * arg __unused)2095 in6_init(void *arg __unused)
2096 {
2097 EVENTHANDLER_REGISTER(ifnet_event, in6_ifevent, NULL, EVENTHANDLER_PRI_ANY);
2098 }
2099 SYSINIT(in6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, in6_init, NULL);
2100
2101 int
in6if_do_dad(struct ifnet * ifp)2102 in6if_do_dad(struct ifnet *ifp)
2103 {
2104
2105 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2106 return (0);
2107 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2108 return (0);
2109 if ((ND_IFINFO(ifp)->flags &
2110 (ND6_IFF_IFDISABLED | ND6_IFF_NO_DAD)) != 0)
2111 return (0);
2112 return (1);
2113 }
2114
2115 /*
2116 * Provide the length of interface identifiers to be used for the link attached
2117 * to the given interface. The length should be defined in "IPv6 over
2118 * xxx-link" document. Note that address architecture might also define
2119 * the length for a particular set of address prefixes, regardless of the
2120 * link type. As clarified in rfc2462bis, those two definitions should be
2121 * consistent, and those really are as of August 2004.
2122 */
2123 int
in6_if2idlen(struct ifnet * ifp)2124 in6_if2idlen(struct ifnet *ifp)
2125 {
2126 switch (ifp->if_type) {
2127 case IFT_ETHER: /* RFC2464 */
2128 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2129 case IFT_L2VLAN: /* ditto */
2130 case IFT_BRIDGE: /* bridge(4) only does Ethernet-like links */
2131 case IFT_INFINIBAND:
2132 return (64);
2133 case IFT_PPP: /* RFC2472 */
2134 return (64);
2135 case IFT_FRELAY: /* RFC2590 */
2136 return (64);
2137 case IFT_IEEE1394: /* RFC3146 */
2138 return (64);
2139 case IFT_GIF:
2140 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2141 case IFT_LOOP:
2142 return (64); /* XXX: is this really correct? */
2143 default:
2144 /*
2145 * Unknown link type:
2146 * It might be controversial to use the today's common constant
2147 * of 64 for these cases unconditionally. For full compliance,
2148 * we should return an error in this case. On the other hand,
2149 * if we simply miss the standard for the link type or a new
2150 * standard is defined for a new link type, the IFID length
2151 * is very likely to be the common constant. As a compromise,
2152 * we always use the constant, but make an explicit notice
2153 * indicating the "unknown" case.
2154 */
2155 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2156 return (64);
2157 }
2158 }
2159
2160 struct in6_llentry {
2161 struct llentry base;
2162 };
2163
2164 #define IN6_LLTBL_DEFAULT_HSIZE 32
2165 #define IN6_LLTBL_HASH(k, h) \
2166 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
2167
2168 /*
2169 * Do actual deallocation of @lle.
2170 */
2171 static void
in6_lltable_destroy_lle_unlocked(epoch_context_t ctx)2172 in6_lltable_destroy_lle_unlocked(epoch_context_t ctx)
2173 {
2174 struct llentry *lle;
2175
2176 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
2177 LLE_LOCK_DESTROY(lle);
2178 LLE_REQ_DESTROY(lle);
2179 free(lle, M_LLTABLE);
2180 }
2181
2182 /*
2183 * Called by LLE_FREE_LOCKED when number of references
2184 * drops to zero.
2185 */
2186 static void
in6_lltable_destroy_lle(struct llentry * lle)2187 in6_lltable_destroy_lle(struct llentry *lle)
2188 {
2189
2190 LLE_WUNLOCK(lle);
2191 NET_EPOCH_CALL(in6_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
2192 }
2193
2194 static struct llentry *
in6_lltable_new(const struct in6_addr * addr6,u_int flags)2195 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
2196 {
2197 struct in6_llentry *lle;
2198
2199 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2200 if (lle == NULL) /* NB: caller generates msg */
2201 return NULL;
2202
2203 lle->base.r_l3addr.addr6 = *addr6;
2204 lle->base.lle_refcnt = 1;
2205 lle->base.lle_free = in6_lltable_destroy_lle;
2206 LLE_LOCK_INIT(&lle->base);
2207 LLE_REQ_INIT(&lle->base);
2208 callout_init(&lle->base.lle_timer, 1);
2209
2210 return (&lle->base);
2211 }
2212
2213 static int
in6_lltable_match_prefix(const struct sockaddr * saddr,const struct sockaddr * smask,u_int flags,struct llentry * lle)2214 in6_lltable_match_prefix(const struct sockaddr *saddr,
2215 const struct sockaddr *smask, u_int flags, struct llentry *lle)
2216 {
2217 const struct in6_addr *addr, *mask, *lle_addr;
2218
2219 addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr;
2220 mask = &((const struct sockaddr_in6 *)smask)->sin6_addr;
2221 lle_addr = &lle->r_l3addr.addr6;
2222
2223 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
2224 return (0);
2225
2226 if (lle->la_flags & LLE_IFADDR) {
2227 /*
2228 * Delete LLE_IFADDR records IFF address & flag matches.
2229 * Note that addr is the interface address within prefix
2230 * being matched.
2231 */
2232 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) &&
2233 (flags & LLE_STATIC) != 0)
2234 return (1);
2235 return (0);
2236 }
2237
2238 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2239 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
2240 return (1);
2241
2242 return (0);
2243 }
2244
2245 static void
in6_lltable_free_entry(struct lltable * llt,struct llentry * lle)2246 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2247 {
2248 struct ifnet *ifp __diagused;
2249
2250 LLE_WLOCK_ASSERT(lle);
2251 KASSERT(llt != NULL, ("lltable is NULL"));
2252
2253 /* Unlink entry from table */
2254 if ((lle->la_flags & LLE_LINKED) != 0) {
2255 ifp = llt->llt_ifp;
2256 IF_AFDATA_WLOCK_ASSERT(ifp);
2257 lltable_unlink_entry(llt, lle);
2258 }
2259
2260 llentry_free(lle);
2261 }
2262
2263 static int
in6_lltable_rtcheck(struct ifnet * ifp,u_int flags,const struct sockaddr * l3addr)2264 in6_lltable_rtcheck(struct ifnet *ifp,
2265 u_int flags,
2266 const struct sockaddr *l3addr)
2267 {
2268 const struct sockaddr_in6 *sin6;
2269 struct nhop_object *nh;
2270 struct in6_addr dst;
2271 uint32_t scopeid;
2272 char ip6buf[INET6_ADDRSTRLEN];
2273 int fibnum;
2274
2275 NET_EPOCH_ASSERT();
2276 KASSERT(l3addr->sa_family == AF_INET6,
2277 ("sin_family %d", l3addr->sa_family));
2278
2279 sin6 = (const struct sockaddr_in6 *)l3addr;
2280 in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
2281 fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib;
2282 nh = fib6_lookup(fibnum, &dst, scopeid, NHR_NONE, 0);
2283 if (nh && ((nh->nh_flags & NHF_GATEWAY) || nh->nh_ifp != ifp)) {
2284 struct ifaddr *ifa;
2285 /*
2286 * Create an ND6 cache for an IPv6 neighbor
2287 * that is not covered by our own prefix.
2288 */
2289 ifa = ifaof_ifpforaddr(l3addr, ifp);
2290 if (ifa != NULL) {
2291 return 0;
2292 }
2293 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2294 ip6_sprintf(ip6buf, &sin6->sin6_addr));
2295 return EINVAL;
2296 }
2297 return 0;
2298 }
2299
2300 static inline uint32_t
in6_lltable_hash_dst(const struct in6_addr * dst,uint32_t hsize)2301 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
2302 {
2303
2304 return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize));
2305 }
2306
2307 static uint32_t
in6_lltable_hash(const struct llentry * lle,uint32_t hsize)2308 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
2309 {
2310
2311 return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
2312 }
2313
2314 static void
in6_lltable_fill_sa_entry(const struct llentry * lle,struct sockaddr * sa)2315 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2316 {
2317 struct sockaddr_in6 *sin6;
2318
2319 sin6 = (struct sockaddr_in6 *)sa;
2320 bzero(sin6, sizeof(*sin6));
2321 sin6->sin6_family = AF_INET6;
2322 sin6->sin6_len = sizeof(*sin6);
2323 sin6->sin6_addr = lle->r_l3addr.addr6;
2324 }
2325
2326 static inline struct llentry *
in6_lltable_find_dst(struct lltable * llt,const struct in6_addr * dst)2327 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
2328 {
2329 struct llentry *lle;
2330 struct llentries *lleh;
2331 u_int hashidx;
2332
2333 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
2334 lleh = &llt->lle_head[hashidx];
2335 CK_LIST_FOREACH(lle, lleh, lle_next) {
2336 if (lle->la_flags & LLE_DELETED)
2337 continue;
2338 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
2339 break;
2340 }
2341
2342 return (lle);
2343 }
2344
2345 static void
in6_lltable_delete_entry(struct lltable * llt,struct llentry * lle)2346 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2347 {
2348
2349 lle->la_flags |= LLE_DELETED;
2350
2351 /* Leave the solicited multicast group. */
2352 if ((lle->la_flags & LLE_PUB) != 0)
2353 in6_leave_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6);
2354 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2355 #ifdef DIAGNOSTIC
2356 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2357 #endif
2358 llentry_free(lle);
2359 }
2360
2361 static struct llentry *
in6_lltable_alloc(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)2362 in6_lltable_alloc(struct lltable *llt, u_int flags,
2363 const struct sockaddr *l3addr)
2364 {
2365 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2366 struct ifnet *ifp = llt->llt_ifp;
2367 struct llentry *lle;
2368 char linkhdr[LLE_MAX_LINKHDR];
2369 size_t linkhdrsize;
2370 int lladdr_off;
2371
2372 KASSERT(l3addr->sa_family == AF_INET6,
2373 ("sin_family %d", l3addr->sa_family));
2374
2375 /*
2376 * A route that covers the given address must have
2377 * been installed 1st because we are doing a resolution,
2378 * verify this.
2379 */
2380 if (!(flags & LLE_IFADDR) &&
2381 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2382 return (NULL);
2383
2384 lle = in6_lltable_new(&sin6->sin6_addr, flags);
2385 if (lle == NULL) {
2386 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2387 return (NULL);
2388 }
2389 lle->la_flags = flags;
2390 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2391 linkhdrsize = LLE_MAX_LINKHDR;
2392 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp),
2393 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
2394 in6_lltable_free_entry(llt, lle);
2395 return (NULL);
2396 }
2397 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
2398 lladdr_off);
2399 lle->la_flags |= LLE_STATIC;
2400 }
2401
2402 if ((lle->la_flags & LLE_STATIC) != 0)
2403 lle->ln_state = ND6_LLINFO_REACHABLE;
2404
2405 return (lle);
2406 }
2407
2408 static struct llentry *
in6_lltable_lookup(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)2409 in6_lltable_lookup(struct lltable *llt, u_int flags,
2410 const struct sockaddr *l3addr)
2411 {
2412 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2413 int family = flags >> 16;
2414 struct llentry *lle;
2415
2416 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2417 KASSERT(l3addr->sa_family == AF_INET6,
2418 ("sin_family %d", l3addr->sa_family));
2419 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
2420 (LLE_UNLOCKED | LLE_EXCLUSIVE),
2421 ("wrong lle request flags: %#x", flags));
2422
2423 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2424
2425 if (__predict_false(family != AF_INET6))
2426 lle = llentry_lookup_family(lle, family);
2427
2428 if (lle == NULL)
2429 return (NULL);
2430
2431 if (flags & LLE_UNLOCKED)
2432 return (lle);
2433
2434 if (flags & LLE_EXCLUSIVE)
2435 LLE_WLOCK(lle);
2436 else
2437 LLE_RLOCK(lle);
2438
2439 /*
2440 * If the afdata lock is not held, the LLE may have been unlinked while
2441 * we were blocked on the LLE lock. Check for this case.
2442 */
2443 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
2444 if (flags & LLE_EXCLUSIVE)
2445 LLE_WUNLOCK(lle);
2446 else
2447 LLE_RUNLOCK(lle);
2448 return (NULL);
2449 }
2450 return (lle);
2451 }
2452
2453 static int
in6_lltable_dump_entry(struct lltable * llt,struct llentry * lle,struct sysctl_req * wr)2454 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2455 struct sysctl_req *wr)
2456 {
2457 struct ifnet *ifp = llt->llt_ifp;
2458 /* XXX stack use */
2459 struct {
2460 struct rt_msghdr rtm;
2461 struct sockaddr_in6 sin6;
2462 /*
2463 * ndp.c assumes that sdl is word aligned
2464 */
2465 #ifdef __LP64__
2466 uint32_t pad;
2467 #endif
2468 struct sockaddr_dl sdl;
2469 } ndpc;
2470 struct sockaddr_dl *sdl;
2471 int error;
2472
2473 bzero(&ndpc, sizeof(ndpc));
2474 /* skip deleted entries */
2475 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
2476 return (0);
2477 /* Skip if jailed and not a valid IP of the prison. */
2478 lltable_fill_sa_entry(lle, (struct sockaddr *)&ndpc.sin6);
2479 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&ndpc.sin6) != 0)
2480 return (0);
2481 /*
2482 * produce a msg made of:
2483 * struct rt_msghdr;
2484 * struct sockaddr_in6 (IPv6)
2485 * struct sockaddr_dl;
2486 */
2487 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2488 ndpc.rtm.rtm_version = RTM_VERSION;
2489 ndpc.rtm.rtm_type = RTM_GET;
2490 ndpc.rtm.rtm_flags = RTF_UP;
2491 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2492 sa6_recoverscope(&ndpc.sin6);
2493
2494 /* publish */
2495 if (lle->la_flags & LLE_PUB)
2496 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2497
2498 sdl = &ndpc.sdl;
2499 sdl->sdl_family = AF_LINK;
2500 sdl->sdl_len = sizeof(*sdl);
2501 sdl->sdl_index = ifp->if_index;
2502 sdl->sdl_type = ifp->if_type;
2503 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2504 sdl->sdl_alen = ifp->if_addrlen;
2505 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2506 } else {
2507 sdl->sdl_alen = 0;
2508 bzero(LLADDR(sdl), ifp->if_addrlen);
2509 }
2510 if (lle->la_expire != 0)
2511 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire +
2512 lle->lle_remtime / hz + time_second - time_uptime;
2513 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2514 if (lle->la_flags & LLE_STATIC)
2515 ndpc.rtm.rtm_flags |= RTF_STATIC;
2516 if (lle->la_flags & LLE_IFADDR)
2517 ndpc.rtm.rtm_flags |= RTF_PINNED;
2518 if (lle->ln_router != 0)
2519 ndpc.rtm.rtm_flags |= RTF_GATEWAY;
2520 ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked;
2521 /* Store state in rmx_weight value */
2522 ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state;
2523 ndpc.rtm.rtm_index = ifp->if_index;
2524 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2525
2526 return (error);
2527 }
2528
2529 static void
in6_lltable_post_resolved(struct lltable * llt,struct llentry * lle)2530 in6_lltable_post_resolved(struct lltable *llt, struct llentry *lle)
2531 {
2532 /* Join the solicited multicast group for dst. */
2533 if ((lle->la_flags & LLE_PUB) == LLE_PUB)
2534 in6_join_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6);
2535 }
2536
2537 static struct lltable *
in6_lltattach(struct ifnet * ifp)2538 in6_lltattach(struct ifnet *ifp)
2539 {
2540 struct lltable *llt;
2541
2542 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
2543 llt->llt_af = AF_INET6;
2544 llt->llt_ifp = ifp;
2545
2546 llt->llt_lookup = in6_lltable_lookup;
2547 llt->llt_alloc_entry = in6_lltable_alloc;
2548 llt->llt_delete_entry = in6_lltable_delete_entry;
2549 llt->llt_dump_entry = in6_lltable_dump_entry;
2550 llt->llt_hash = in6_lltable_hash;
2551 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
2552 llt->llt_free_entry = in6_lltable_free_entry;
2553 llt->llt_match_prefix = in6_lltable_match_prefix;
2554 llt->llt_mark_used = llentry_mark_used;
2555 llt->llt_post_resolved = in6_lltable_post_resolved;
2556 lltable_link(llt);
2557
2558 return (llt);
2559 }
2560
2561 struct lltable *
in6_lltable_get(struct ifnet * ifp)2562 in6_lltable_get(struct ifnet *ifp)
2563 {
2564 struct lltable *llt = NULL;
2565
2566 void *afdata_ptr = ifp->if_afdata[AF_INET6];
2567 if (afdata_ptr != NULL)
2568 llt = ((struct in6_ifextra *)afdata_ptr)->lltable;
2569 return (llt);
2570 }
2571
2572 void *
in6_domifattach(struct ifnet * ifp)2573 in6_domifattach(struct ifnet *ifp)
2574 {
2575 struct in6_ifextra *ext;
2576
2577 /* There are not IPv6-capable interfaces. */
2578 switch (ifp->if_type) {
2579 case IFT_PFLOG:
2580 case IFT_PFSYNC:
2581 case IFT_USB:
2582 return (NULL);
2583 }
2584 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2585 bzero(ext, sizeof(*ext));
2586
2587 ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2588 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2589 COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2590 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2591
2592 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2593 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2594 M_WAITOK);
2595 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2596 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2597
2598 ext->nd_ifinfo = nd6_ifattach(ifp);
2599 ext->scope6_id = scope6_ifattach(ifp);
2600 ext->lltable = in6_lltattach(ifp);
2601
2602 ext->mld_ifinfo = mld_domifattach(ifp);
2603
2604 return ext;
2605 }
2606
2607 int
in6_domifmtu(struct ifnet * ifp)2608 in6_domifmtu(struct ifnet *ifp)
2609 {
2610 if (ifp->if_afdata[AF_INET6] == NULL)
2611 return ifp->if_mtu;
2612
2613 return (IN6_LINKMTU(ifp));
2614 }
2615
2616 void
in6_domifdetach(struct ifnet * ifp,void * aux)2617 in6_domifdetach(struct ifnet *ifp, void *aux)
2618 {
2619 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2620
2621 mld_domifdetach(ifp);
2622 scope6_ifdetach(ext->scope6_id);
2623 nd6_ifdetach(ifp, ext->nd_ifinfo);
2624 lltable_free(ext->lltable);
2625 COUNTER_ARRAY_FREE(ext->in6_ifstat,
2626 sizeof(struct in6_ifstat) / sizeof(uint64_t));
2627 free(ext->in6_ifstat, M_IFADDR);
2628 COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2629 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2630 free(ext->icmp6_ifstat, M_IFADDR);
2631 free(ext, M_IFADDR);
2632 }
2633
2634 /*
2635 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2636 * v4 mapped addr or v4 compat addr
2637 */
2638 void
in6_sin6_2_sin(struct sockaddr_in * sin,const struct sockaddr_in6 * sin6)2639 in6_sin6_2_sin(struct sockaddr_in *sin, const struct sockaddr_in6 *sin6)
2640 {
2641
2642 bzero(sin, sizeof(*sin));
2643 sin->sin_len = sizeof(struct sockaddr_in);
2644 sin->sin_family = AF_INET;
2645 sin->sin_port = sin6->sin6_port;
2646 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2647 }
2648
2649 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2650 void
in6_sin_2_v4mapsin6(const struct sockaddr_in * sin,struct sockaddr_in6 * sin6)2651 in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2652 {
2653 bzero(sin6, sizeof(*sin6));
2654 sin6->sin6_len = sizeof(struct sockaddr_in6);
2655 sin6->sin6_family = AF_INET6;
2656 sin6->sin6_port = sin->sin_port;
2657 sin6->sin6_addr.s6_addr32[0] = 0;
2658 sin6->sin6_addr.s6_addr32[1] = 0;
2659 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2660 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2661 }
2662
2663 /* Convert sockaddr_in6 into sockaddr_in. */
2664 void
in6_sin6_2_sin_in_sock(struct sockaddr * nam)2665 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2666 {
2667 struct sockaddr_in *sin_p;
2668 struct sockaddr_in6 sin6;
2669
2670 /*
2671 * Save original sockaddr_in6 addr and convert it
2672 * to sockaddr_in.
2673 */
2674 sin6 = *(struct sockaddr_in6 *)nam;
2675 sin_p = (struct sockaddr_in *)nam;
2676 in6_sin6_2_sin(sin_p, &sin6);
2677 }
2678
2679 /*
2680 * Join/leave the solicited multicast groups for proxy NDP entries.
2681 */
2682 static void
in6_join_proxy_ndp_mc(struct ifnet * ifp,const struct in6_addr * dst)2683 in6_join_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst)
2684 {
2685 struct in6_multi *inm;
2686 struct in6_addr mltaddr;
2687 char ip6buf[INET6_ADDRSTRLEN];
2688 int error;
2689
2690 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0)
2691 return; /* error logged in in6_solicited_node_maddr. */
2692
2693 error = in6_joingroup(ifp, &mltaddr, NULL, &inm, 0);
2694 if (error != 0) {
2695 nd6log((LOG_WARNING,
2696 "%s: in6_joingroup failed for %s on %s (errno=%d)\n",
2697 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp),
2698 error));
2699 }
2700 }
2701
2702 static void
in6_leave_proxy_ndp_mc(struct ifnet * ifp,const struct in6_addr * dst)2703 in6_leave_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst)
2704 {
2705 struct epoch_tracker et;
2706 struct in6_multi *inm;
2707 struct in6_addr mltaddr;
2708 char ip6buf[INET6_ADDRSTRLEN];
2709
2710 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0)
2711 return; /* error logged in in6_solicited_node_maddr. */
2712
2713 NET_EPOCH_ENTER(et);
2714 inm = in6m_lookup(ifp, &mltaddr);
2715 NET_EPOCH_EXIT(et);
2716 if (inm != NULL)
2717 in6_leavegroup(inm, NULL);
2718 else
2719 nd6log((LOG_WARNING, "%s: in6m_lookup failed for %s on %s\n",
2720 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp)));
2721 }
2722
2723 static bool
in6_lle_match_pub(struct lltable * llt,struct llentry * lle,void * farg)2724 in6_lle_match_pub(struct lltable *llt, struct llentry *lle, void *farg)
2725 {
2726 return ((lle->la_flags & LLE_PUB) != 0);
2727 }
2728
2729 void
in6_purge_proxy_ndp(struct ifnet * ifp)2730 in6_purge_proxy_ndp(struct ifnet *ifp)
2731 {
2732 struct lltable *llt;
2733 bool need_purge;
2734
2735 if (ifp->if_afdata[AF_INET6] == NULL)
2736 return;
2737
2738 llt = LLTABLE6(ifp);
2739 IF_AFDATA_WLOCK(ifp);
2740 need_purge = ((llt->llt_flags & LLT_ADDEDPROXY) != 0);
2741 IF_AFDATA_WUNLOCK(ifp);
2742
2743 /*
2744 * Ever added proxy ndp entries, leave solicited node multicast
2745 * before deleting the llentry.
2746 */
2747 if (need_purge)
2748 lltable_delete_conditional(llt, in6_lle_match_pub, NULL);
2749 }
2750