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