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