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