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