2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
62 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 #include "opt_inet6.h"
71 #include <sys/param.h>
72 #include <sys/eventhandler.h>
73 #include <sys/errno.h>
75 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/sockio.h>
79 #include <sys/systm.h>
82 #include <sys/protosw.h>
84 #include <sys/kernel.h>
86 #include <sys/rmlock.h>
87 #include <sys/syslog.h>
90 #include <net/if_var.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/if_dl.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <net/if_llatbl.h>
99 #include <netinet/if_ether.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/ip_carp.h>
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>
116 * struct in6_ifreq and struct ifreq must be type punnable for common members
117 * of ifr_ifru to allow accessors to be shared.
119 _Static_assert(offsetof(struct in6_ifreq, ifr_ifru) ==
120 offsetof(struct ifreq, ifr_ifru),
121 "struct in6_ifreq and struct ifreq are not type punnable");
123 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
124 #define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix)
127 * Definitions of some costant IP6 addresses.
129 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
130 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
131 const struct in6_addr in6addr_nodelocal_allnodes =
132 IN6ADDR_NODELOCAL_ALLNODES_INIT;
133 const struct in6_addr in6addr_linklocal_allnodes =
134 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
135 const struct in6_addr in6addr_linklocal_allrouters =
136 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
137 const struct in6_addr in6addr_linklocal_allv2routers =
138 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
140 const struct in6_addr in6mask0 = IN6MASK0;
141 const struct in6_addr in6mask32 = IN6MASK32;
142 const struct in6_addr in6mask64 = IN6MASK64;
143 const struct in6_addr in6mask96 = IN6MASK96;
144 const struct in6_addr in6mask128 = IN6MASK128;
146 const struct sockaddr_in6 sa6_any =
147 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
149 static int in6_notify_ifa(struct ifnet *, struct in6_ifaddr *,
150 struct in6_aliasreq *, int);
151 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
153 static int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *,
154 struct in6_ifaddr *, int);
155 static struct in6_ifaddr *in6_alloc_ifa(struct ifnet *,
156 struct in6_aliasreq *, int flags);
157 static int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *,
158 struct in6_ifaddr *, int, int);
159 static int in6_broadcast_ifa(struct ifnet *, struct in6_aliasreq *,
160 struct in6_ifaddr *, int);
162 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
163 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
167 in6_newaddrmsg(struct in6_ifaddr *ia, int cmd)
169 struct sockaddr_dl gateway;
170 struct sockaddr_in6 mask, addr;
175 * initialize for rtmsg generation
177 bzero(&gateway, sizeof(gateway));
178 gateway.sdl_len = sizeof(gateway);
179 gateway.sdl_family = AF_LINK;
181 bzero(&rt, sizeof(rt));
182 rt.rt_gateway = (struct sockaddr *)&gateway;
183 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
184 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
185 rt_mask(&rt) = (struct sockaddr *)&mask;
186 rt_key(&rt) = (struct sockaddr *)&addr;
187 rt.rt_flags = RTF_HOST | RTF_STATIC;
189 rt.rt_flags |= RTF_UP;
190 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS : ia62ifa(ia)->ifa_ifp->if_fib;
191 /* Announce arrival of local address to this FIB. */
192 rt_newaddrmsg_fib(cmd, &ia->ia_ifa, 0, &rt, fibnum);
196 in6_mask2len(struct in6_addr *mask, u_char *lim0)
199 u_char *lim = lim0, *p;
201 /* ignore the scope_id part */
202 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
203 lim = (u_char *)mask + sizeof(*mask);
204 for (p = (u_char *)mask; p < lim; x++, p++) {
210 for (y = 0; y < 8; y++) {
211 if ((*p & (0x80 >> y)) == 0)
217 * when the limit pointer is given, do a stricter check on the
221 if (y != 0 && (*p & (0x00ff >> y)) != 0)
223 for (p = p + 1; p < lim; p++)
231 #ifdef COMPAT_FREEBSD32
232 struct in6_ndifreq32 {
233 char ifname[IFNAMSIZ];
236 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
240 in6_control(struct socket *so, u_long cmd, caddr_t data,
241 struct ifnet *ifp, struct thread *td)
243 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
244 struct in6_ifaddr *ia = NULL;
245 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
246 struct sockaddr_in6 *sa6;
247 int carp_attached = 0;
252 * Compat to make pre-10.x ifconfig(8) operable.
254 if (cmd == OSIOCAIFADDR_IN6)
255 cmd = SIOCAIFADDR_IN6;
258 case SIOCGETSGCNT_IN6:
259 case SIOCGETMIFCNT_IN6:
261 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
262 * We cannot see how that would be needed, so do not adjust the
263 * KPI blindly; more likely should clean up the IPv4 variant.
265 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
269 case SIOCAADDRCTL_POLICY:
270 case SIOCDADDRCTL_POLICY:
272 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
276 return (in6_src_ioctl(cmd, data));
283 case SIOCSNDFLUSH_IN6:
284 case SIOCSPFXFLUSH_IN6:
285 case SIOCSRTRFLUSH_IN6:
286 case SIOCSDEFIFACE_IN6:
287 case SIOCSIFINFO_FLAGS:
288 case SIOCSIFINFO_IN6:
290 error = priv_check(td, PRIV_NETINET_ND6);
295 case OSIOCGIFINFO_IN6:
296 case SIOCGIFINFO_IN6:
297 case SIOCGNBRINFO_IN6:
298 case SIOCGDEFIFACE_IN6:
299 return (nd6_ioctl(cmd, data, ifp));
301 #ifdef COMPAT_FREEBSD32
302 case SIOCGDEFIFACE32_IN6:
304 struct in6_ndifreq ndif;
305 struct in6_ndifreq32 *ndif32;
307 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
311 ndif32 = (struct in6_ndifreq32 *)data;
312 ndif32->ifindex = ndif.ifindex;
319 case SIOCSIFPREFIX_IN6:
320 case SIOCDIFPREFIX_IN6:
321 case SIOCAIFPREFIX_IN6:
322 case SIOCCIFPREFIX_IN6:
323 case SIOCSGIFPREFIX_IN6:
324 case SIOCGIFPREFIX_IN6:
326 "prefix ioctls are now invalidated. "
327 "please use ifconfig.\n");
334 error = priv_check(td, PRIV_NETINET_SCOPE6);
341 return (scope6_ioctl(cmd, data, ifp));
345 * Find address for this interface, if it exists.
347 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
348 * only, and used the first interface address as the target of other
349 * operations (without checking ifra_addr). This was because netinet
350 * code/API assumed at most 1 interface address per interface.
351 * Since IPv6 allows a node to assign multiple addresses
352 * on a single interface, we almost always look and check the
353 * presence of ifra_addr, and reject invalid ones here.
354 * It also decreases duplicated code among SIOC*_IN6 operations.
357 case SIOCAIFADDR_IN6:
358 case SIOCSIFPHYADDR_IN6:
359 sa6 = &ifra->ifra_addr;
361 case SIOCSIFADDR_IN6:
362 case SIOCGIFADDR_IN6:
363 case SIOCSIFDSTADDR_IN6:
364 case SIOCSIFNETMASK_IN6:
365 case SIOCGIFDSTADDR_IN6:
366 case SIOCGIFNETMASK_IN6:
367 case SIOCDIFADDR_IN6:
368 case SIOCGIFPSRCADDR_IN6:
369 case SIOCGIFPDSTADDR_IN6:
370 case SIOCGIFAFLAG_IN6:
371 case SIOCSNDFLUSH_IN6:
372 case SIOCSPFXFLUSH_IN6:
373 case SIOCSRTRFLUSH_IN6:
374 case SIOCGIFALIFETIME_IN6:
375 case SIOCGIFSTAT_IN6:
376 case SIOCGIFSTAT_ICMP6:
377 sa6 = &ifr->ifr_addr;
384 * Although we should pass any non-INET6 ioctl requests
385 * down to driver, we filter some legacy INET requests.
386 * Drivers trust SIOCSIFADDR et al to come from an already
387 * privileged layer, and do not perform any credentials
388 * checks or input validation.
395 if (sa6 && sa6->sin6_family == AF_INET6) {
396 if (sa6->sin6_scope_id != 0)
397 error = sa6_embedscope(sa6, 0);
399 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
402 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
403 &sa6->sin6_addr)) != 0)
405 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
410 case SIOCSIFADDR_IN6:
411 case SIOCSIFDSTADDR_IN6:
412 case SIOCSIFNETMASK_IN6:
414 * Since IPv6 allows a node to assign multiple addresses
415 * on a single interface, SIOCSIFxxx ioctls are deprecated.
417 /* we decided to obsolete this command (20000704) */
421 case SIOCDIFADDR_IN6:
423 * for IPv4, we look for existing in_ifaddr here to allow
424 * "ifconfig if0 delete" to remove the first IPv4 address on
425 * the interface. For IPv6, as the spec allows multiple
426 * interface address from the day one, we consider "remove the
427 * first one" semantics to be not preferable.
430 error = EADDRNOTAVAIL;
434 case SIOCAIFADDR_IN6:
436 * We always require users to specify a valid IPv6 address for
437 * the corresponding operation.
439 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
440 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
441 error = EAFNOSUPPORT;
446 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
447 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
452 case SIOCGIFSTAT_IN6:
453 case SIOCGIFSTAT_ICMP6:
454 if (ifp->if_afdata[AF_INET6] == NULL) {
455 error = EPFNOSUPPORT;
460 case SIOCGIFADDR_IN6:
461 /* This interface is basically deprecated. use SIOCGIFCONF. */
463 case SIOCGIFAFLAG_IN6:
464 case SIOCGIFNETMASK_IN6:
465 case SIOCGIFDSTADDR_IN6:
466 case SIOCGIFALIFETIME_IN6:
467 /* must think again about its semantics */
469 error = EADDRNOTAVAIL;
476 case SIOCGIFADDR_IN6:
477 ifr->ifr_addr = ia->ia_addr;
478 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
482 case SIOCGIFDSTADDR_IN6:
483 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
487 ifr->ifr_dstaddr = ia->ia_dstaddr;
488 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
492 case SIOCGIFNETMASK_IN6:
493 ifr->ifr_addr = ia->ia_prefixmask;
496 case SIOCGIFAFLAG_IN6:
497 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
500 case SIOCGIFSTAT_IN6:
501 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
502 ifp->if_afdata[AF_INET6])->in6_ifstat,
503 &ifr->ifr_ifru.ifru_stat,
504 sizeof(struct in6_ifstat) / sizeof(uint64_t));
507 case SIOCGIFSTAT_ICMP6:
508 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
509 ifp->if_afdata[AF_INET6])->icmp6_ifstat,
510 &ifr->ifr_ifru.ifru_icmp6stat,
511 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
514 case SIOCGIFALIFETIME_IN6:
515 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
516 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
518 struct in6_addrlifetime *retlt =
519 &ifr->ifr_ifru.ifru_lifetime;
522 * XXX: adjust expiration time assuming time_t is
526 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
527 if (ia->ia6_lifetime.ia6t_vltime <
528 maxexpire - ia->ia6_updatetime) {
529 retlt->ia6t_expire = ia->ia6_updatetime +
530 ia->ia6_lifetime.ia6t_vltime;
532 retlt->ia6t_expire = maxexpire;
534 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
536 struct in6_addrlifetime *retlt =
537 &ifr->ifr_ifru.ifru_lifetime;
540 * XXX: adjust expiration time assuming time_t is
544 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
545 if (ia->ia6_lifetime.ia6t_pltime <
546 maxexpire - ia->ia6_updatetime) {
547 retlt->ia6t_preferred = ia->ia6_updatetime +
548 ia->ia6_lifetime.ia6t_pltime;
550 retlt->ia6t_preferred = maxexpire;
554 case SIOCAIFADDR_IN6:
556 struct nd_prefixctl pr0;
557 struct nd_prefix *pr;
560 * first, make or update the interface address structure,
561 * and link it to the list.
563 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
566 if (ia->ia_ifa.ifa_carp)
567 (*carp_detach_p)(&ia->ia_ifa, true);
568 ifa_free(&ia->ia_ifa);
570 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
573 * this can happen when the user specify the 0 valid
579 if (cmd == ocmd && ifra->ifra_vhid > 0) {
580 if (carp_attach_p != NULL)
581 error = (*carp_attach_p)(&ia->ia_ifa,
584 error = EPROTONOSUPPORT;
592 * then, make the prefix on-link on the interface.
593 * XXX: we'd rather create the prefix before the address, but
594 * we need at least one address to install the corresponding
595 * interface route, so we configure the address first.
599 * convert mask to prefix length (prefixmask has already
600 * been validated in in6_update_ifa().
602 bzero(&pr0, sizeof(pr0));
604 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
606 if (pr0.ndpr_plen == 128) {
607 /* we don't need to install a host route. */
610 pr0.ndpr_prefix = ifra->ifra_addr;
611 /* apply the mask for safety. */
612 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr,
613 &ifra->ifra_prefixmask.sin6_addr);
616 * XXX: since we don't have an API to set prefix (not address)
617 * lifetimes, we just use the same lifetimes as addresses.
618 * The (temporarily) installed lifetimes can be overridden by
619 * later advertised RAs (when accept_rtadv is non 0), which is
620 * an intended behavior.
622 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
624 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
625 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
626 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
628 /* add the prefix if not yet. */
629 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
631 * nd6_prelist_add will install the corresponding
634 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
636 (*carp_detach_p)(&ia->ia_ifa, false);
641 /* relate the address to the prefix */
642 if (ia->ia6_ndpr == NULL) {
647 * If this is the first autoconf address from the
648 * prefix, create a temporary address as well
651 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
652 V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) {
654 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
655 log(LOG_NOTICE, "in6_control: failed "
656 "to create a temporary address, "
664 * this might affect the status of autoconfigured addresses,
665 * that is, this address might make other addresses detached.
667 pfxlist_onlink_check();
671 * Try to clear the flag when a new IPv6 address is added
672 * onto an IFDISABLED interface and it succeeds.
674 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
675 struct in6_ndireq nd;
677 memset(&nd, 0, sizeof(nd));
678 nd.ndi.flags = ND_IFINFO(ifp)->flags;
679 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
680 if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0)
681 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
682 "SIOCSIFINFO_FLAGS for -ifdisabled "
685 * Ignore failure of clearing the flag intentionally.
686 * The failure means address duplication was detected.
692 case SIOCDIFADDR_IN6:
694 struct nd_prefix *pr;
697 * If the address being deleted is the only one that owns
698 * the corresponding prefix, expire the prefix as well.
699 * XXX: theoretically, we don't have to worry about such
700 * relationship, since we separate the address management
701 * and the prefix management. We do this, however, to provide
702 * as much backward compatibility as possible in terms of
703 * the ioctl operation.
704 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
707 in6_purgeaddr(&ia->ia_ifa);
708 if (pr != NULL && pr->ndpr_addrcnt == 0) {
710 nd6_prefix_unlink(pr, NULL);
714 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
719 if (ifp->if_ioctl == NULL) {
723 error = (*ifp->if_ioctl)(ifp, cmd, data);
730 ifa_free(&ia->ia_ifa);
735 static struct in6_multi_mship *
736 in6_joingroup_legacy(struct ifnet *ifp, const struct in6_addr *mcaddr,
737 int *errorp, int delay)
739 struct in6_multi_mship *imm;
742 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
748 delay = (delay * PR_FASTHZ) / hz;
750 error = in6_joingroup(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay);
753 free(imm, M_IP6MADDR);
760 * Join necessary multicast groups. Factored out from in6_update_ifa().
761 * This entire work should only be done once, for the default FIB.
764 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
765 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
767 char ip6buf[INET6_ADDRSTRLEN];
768 struct in6_addr mltaddr;
769 struct in6_multi_mship *imm;
772 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
774 /* Join solicited multicast addr for new host id. */
775 bzero(&mltaddr, sizeof(struct in6_addr));
776 mltaddr.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
777 mltaddr.s6_addr32[2] = htonl(1);
778 mltaddr.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
779 mltaddr.s6_addr8[12] = 0xff;
780 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) {
781 /* XXX: should not happen */
782 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
786 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
788 * We need a random delay for DAD on the address being
789 * configured. It also means delaying transmission of the
790 * corresponding MLD report to avoid report collision.
791 * [RFC 4861, Section 6.3.7]
793 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
795 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay);
797 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
798 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
799 if_name(ifp), error));
802 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
803 *in6m_sol = imm->i6mm_maddr;
806 * Join link-local all-nodes address.
808 mltaddr = in6addr_linklocal_allnodes;
809 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
810 goto cleanup; /* XXX: should not fail */
812 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0);
814 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
815 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
816 if_name(ifp), error));
819 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
822 * Join node information group address.
825 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
827 * The spec does not say anything about delay for this group,
828 * but the same logic should apply.
830 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
832 if (in6_nigroup(ifp, NULL, -1, &mltaddr) == 0) {
834 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay);
837 "%s: in6_joingroup failed for %s on %s "
838 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
839 &mltaddr), if_name(ifp), error));
840 /* XXX not very fatal, go on... */
842 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
844 if (V_icmp6_nodeinfo_oldmcprefix &&
845 in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr) == 0) {
846 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay);
849 "%s: in6_joingroup failed for %s on %s "
850 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
851 &mltaddr), if_name(ifp), error));
852 /* XXX not very fatal, go on... */
854 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
858 * Join interface-local all-nodes address.
859 * (ff01::1%ifN, and ff01::%ifN/32)
861 mltaddr = in6addr_nodelocal_allnodes;
862 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
863 goto cleanup; /* XXX: should not fail */
865 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0);
867 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
868 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
869 &mltaddr), if_name(ifp), error));
872 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
879 * Update parameters of an IPv6 interface address.
880 * If necessary, a new entry is created and linked into address chains.
881 * This function is separated from in6_control().
884 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
885 struct in6_ifaddr *ia, int flags)
887 int error, hostIsNew = 0;
889 if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0)
894 if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL)
898 error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags);
900 if (hostIsNew != 0) {
901 in6_unlink_ifa(ia, ifp);
902 ifa_free(&ia->ia_ifa);
908 error = in6_broadcast_ifa(ifp, ifra, ia, flags);
914 * Fill in basic IPv6 address request info.
917 in6_prepare_ifra(struct in6_aliasreq *ifra, const struct in6_addr *addr,
918 const struct in6_addr *mask)
921 memset(ifra, 0, sizeof(struct in6_aliasreq));
923 ifra->ifra_addr.sin6_family = AF_INET6;
924 ifra->ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
926 ifra->ifra_addr.sin6_addr = *addr;
928 ifra->ifra_prefixmask.sin6_family = AF_INET6;
929 ifra->ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
931 ifra->ifra_prefixmask.sin6_addr = *mask;
935 in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra,
936 struct in6_ifaddr *ia, int flags)
939 struct sockaddr_in6 dst6;
940 struct in6_addrlifetime *lt;
941 char ip6buf[INET6_ADDRSTRLEN];
943 /* Validate parameters */
944 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
948 * The destination address for a p2p link must have a family
949 * of AF_UNSPEC or AF_INET6.
951 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
952 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
953 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
954 return (EAFNOSUPPORT);
959 if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) ||
960 ifra->ifra_addr.sin6_family != AF_INET6)
964 * validate ifra_prefixmask. don't check sin6_family, netmask
965 * does not carry fields other than sin6_len.
967 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
970 * Because the IPv6 address architecture is classless, we require
971 * users to specify a (non 0) prefix length (mask) for a new address.
972 * We also require the prefix (when specified) mask is valid, and thus
973 * reject a non-consecutive mask.
975 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
977 if (ifra->ifra_prefixmask.sin6_len != 0) {
978 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
979 (u_char *)&ifra->ifra_prefixmask +
980 ifra->ifra_prefixmask.sin6_len);
985 * In this case, ia must not be NULL. We just use its prefix
988 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
991 * If the destination address on a p2p interface is specified,
992 * and the address is a scoped one, validate/set the scope
995 dst6 = ifra->ifra_dstaddr;
996 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
997 (dst6.sin6_family == AF_INET6)) {
998 struct in6_addr in6_tmp;
1001 in6_tmp = dst6.sin6_addr;
1002 if (in6_setscope(&in6_tmp, ifp, &zoneid))
1003 return (EINVAL); /* XXX: should be impossible */
1005 if (dst6.sin6_scope_id != 0) {
1006 if (dst6.sin6_scope_id != zoneid)
1008 } else /* user omit to specify the ID. */
1009 dst6.sin6_scope_id = zoneid;
1011 /* convert into the internal form */
1012 if (sa6_embedscope(&dst6, 0))
1013 return (EINVAL); /* XXX: should be impossible */
1015 /* Modify original ifra_dstaddr to reflect changes */
1016 ifra->ifra_dstaddr = dst6;
1019 * The destination address can be specified only for a p2p or a
1020 * loopback interface. If specified, the corresponding prefix length
1023 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
1024 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1025 /* XXX: noisy message */
1026 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1027 "be specified for a p2p or a loopback IF only\n"));
1031 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1032 "be 128 when dstaddr is specified\n"));
1036 /* lifetime consistency check */
1037 lt = &ifra->ifra_lifetime;
1038 if (lt->ia6t_pltime > lt->ia6t_vltime)
1040 if (lt->ia6t_vltime == 0) {
1042 * the following log might be noisy, but this is a typical
1043 * configuration mistake or a tool's bug.
1046 "in6_update_ifa: valid lifetime is 0 for %s\n",
1047 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1050 return (0); /* there's nothing to do */
1053 /* Check prefix mask */
1054 if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) {
1056 * We prohibit changing the prefix length of an existing
1058 * + such an operation should be rare in IPv6, and
1059 * + the operation would confuse prefix management.
1061 if (ia->ia_prefixmask.sin6_len != 0 &&
1062 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1063 nd6log((LOG_INFO, "in6_validate_ifa: the prefix length "
1064 "of an existing %s address should not be changed\n",
1065 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1076 * Allocate a new ifaddr and link it into chains.
1078 static struct in6_ifaddr *
1079 in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
1081 struct in6_ifaddr *ia;
1084 * When in6_alloc_ifa() is called in a process of a received
1085 * RA, it is called under an interrupt context. So, we should
1086 * call malloc with M_NOWAIT.
1088 ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT);
1091 LIST_INIT(&ia->ia6_memberships);
1092 /* Initialize the address and masks, and put time stamp */
1093 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1094 ia->ia_addr.sin6_family = AF_INET6;
1095 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1096 /* XXX: Can we assign ,sin6_addr and skip the rest? */
1097 ia->ia_addr = ifra->ifra_addr;
1098 ia->ia6_createtime = time_uptime;
1099 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1101 * Some functions expect that ifa_dstaddr is not
1102 * NULL for p2p interfaces.
1104 ia->ia_ifa.ifa_dstaddr =
1105 (struct sockaddr *)&ia->ia_dstaddr;
1107 ia->ia_ifa.ifa_dstaddr = NULL;
1110 /* set prefix mask if any */
1111 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1112 if (ifra->ifra_prefixmask.sin6_len != 0) {
1113 ia->ia_prefixmask.sin6_family = AF_INET6;
1114 ia->ia_prefixmask.sin6_len = ifra->ifra_prefixmask.sin6_len;
1115 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr;
1119 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1121 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1122 IF_ADDR_WUNLOCK(ifp);
1124 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1126 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1127 LIST_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash);
1128 IN6_IFADDR_WUNLOCK();
1134 * Update/configure interface address parameters:
1136 * 1) Update lifetime
1137 * 2) Update interface metric ad flags
1138 * 3) Notify other subsystems
1141 in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra,
1142 struct in6_ifaddr *ia, int hostIsNew, int flags)
1146 /* update timestamp */
1147 ia->ia6_updatetime = time_uptime;
1150 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1151 * to see if the address is deprecated or invalidated, but initialize
1152 * these members for applications.
1154 ia->ia6_lifetime = ifra->ifra_lifetime;
1155 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1156 ia->ia6_lifetime.ia6t_expire =
1157 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1159 ia->ia6_lifetime.ia6t_expire = 0;
1160 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1161 ia->ia6_lifetime.ia6t_preferred =
1162 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1164 ia->ia6_lifetime.ia6t_preferred = 0;
1167 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1168 * userland, make it deprecated.
1170 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1171 ia->ia6_lifetime.ia6t_pltime = 0;
1172 ia->ia6_lifetime.ia6t_preferred = time_uptime;
1176 * configure address flags.
1178 ia->ia6_flags = ifra->ifra_flags;
1181 * Make the address tentative before joining multicast addresses,
1182 * so that corresponding MLD responses would not have a tentative
1185 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1188 * DAD should be performed for an new address or addresses on
1189 * an interface with ND6_IFF_IFDISABLED.
1191 if (in6if_do_dad(ifp) &&
1192 (hostIsNew || (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)))
1193 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1195 /* notify other subsystems */
1196 error = in6_notify_ifa(ifp, ia, ifra, hostIsNew);
1202 * Do link-level ifa job:
1203 * 1) Add lle entry for added address
1204 * 2) Notifies routing socket users about new address
1205 * 3) join appropriate multicast group
1206 * 4) start DAD if enabled
1209 in6_broadcast_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1210 struct in6_ifaddr *ia, int flags)
1212 struct in6_multi *in6m_sol;
1215 /* Add local address to lltable, if necessary (ex. on p2p link). */
1216 if ((error = nd6_add_ifa_lle(ia)) != 0) {
1217 in6_purgeaddr(&ia->ia_ifa);
1218 ifa_free(&ia->ia_ifa);
1222 /* Join necessary multicast groups. */
1224 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1225 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1227 in6_purgeaddr(&ia->ia_ifa);
1228 ifa_free(&ia->ia_ifa);
1233 /* Perform DAD, if the address is TENTATIVE. */
1234 if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1235 int delay, mindelay, maxdelay;
1238 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1240 * We need to impose a delay before sending an NS
1241 * for DAD. Check if we also needed a delay for the
1242 * corresponding MLD message. If we did, the delay
1243 * should be larger than the MLD delay (this could be
1244 * relaxed a bit, but this simple logic is at least
1246 * XXX: Break data hiding guidelines and look at
1247 * state for the solicited multicast group.
1250 if (in6m_sol != NULL &&
1251 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1252 mindelay = in6m_sol->in6m_timer;
1254 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1255 if (maxdelay - mindelay == 0)
1259 (arc4random() % (maxdelay - mindelay)) +
1263 nd6_dad_start((struct ifaddr *)ia, delay);
1266 in6_newaddrmsg(ia, RTM_ADD);
1267 ifa_free(&ia->ia_ifa);
1272 in6_purgeaddr(struct ifaddr *ifa)
1274 struct ifnet *ifp = ifa->ifa_ifp;
1275 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1276 struct in6_multi_mship *imm;
1280 (*carp_detach_p)(ifa, false);
1283 * Remove the loopback route to the interface address.
1284 * The check for the current setting of "nd6_useloopback"
1287 if (ia->ia_flags & IFA_RTSELF) {
1288 error = ifa_del_loopback_route((struct ifaddr *)ia,
1289 (struct sockaddr *)&ia->ia_addr);
1291 ia->ia_flags &= ~IFA_RTSELF;
1294 /* stop DAD processing */
1297 /* Leave multicast groups. */
1298 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1299 LIST_REMOVE(imm, i6mm_chain);
1300 if (imm->i6mm_maddr != NULL)
1301 in6_leavegroup(imm->i6mm_maddr, NULL);
1302 free(imm, M_IP6MADDR);
1304 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1305 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1306 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1307 (ia->ia_dstaddr.sin6_family == AF_INET6 ? RTF_HOST : 0));
1309 log(LOG_INFO, "%s: err=%d, destination address delete "
1310 "failed\n", __func__, error);
1311 ia->ia_flags &= ~IFA_ROUTE;
1314 in6_newaddrmsg(ia, RTM_DELETE);
1315 in6_unlink_ifa(ia, ifp);
1319 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1321 char ip6buf[INET6_ADDRSTRLEN];
1325 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1326 IF_ADDR_WUNLOCK(ifp);
1327 ifa_free(&ia->ia_ifa); /* if_addrhead */
1330 * Defer the release of what might be the last reference to the
1331 * in6_ifaddr so that it can't be freed before the remainder of the
1335 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1336 LIST_REMOVE(ia, ia6_hash);
1337 IN6_IFADDR_WUNLOCK();
1340 * Release the reference to the base prefix. There should be a
1341 * positive reference.
1344 if (ia->ia6_ndpr == NULL) {
1346 "in6_unlink_ifa: autoconf'ed address "
1347 "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
1349 ia->ia6_ndpr->ndpr_addrcnt--;
1350 /* Do not delete lles within prefix if refcont != 0 */
1351 if (ia->ia6_ndpr->ndpr_addrcnt == 0)
1353 ia->ia6_ndpr = NULL;
1356 nd6_rem_ifa_lle(ia, remove_lle);
1359 * Also, if the address being removed is autoconf'ed, call
1360 * pfxlist_onlink_check() since the release might affect the status of
1361 * other (detached) addresses.
1363 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1364 pfxlist_onlink_check();
1366 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1370 * Notifies other subsystems about address change/arrival:
1371 * 1) Notifies device handler on the first IPv6 address assignment
1372 * 2) Handle routing table changes for P2P links and route
1373 * 3) Handle routing table changes for address host route
1376 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia,
1377 struct in6_aliasreq *ifra, int hostIsNew)
1379 int error = 0, plen, ifacount = 0;
1381 struct sockaddr_in6 *pdst;
1382 char ip6buf[INET6_ADDRSTRLEN];
1385 * Give the interface a chance to initialize
1386 * if this is its first address,
1388 if (hostIsNew != 0) {
1390 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1391 if (ifa->ifa_addr->sa_family != AF_INET6)
1395 IF_ADDR_RUNLOCK(ifp);
1398 if (ifacount <= 1 && ifp->if_ioctl) {
1399 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1405 * If a new destination address is specified, scrub the old one and
1406 * install the new destination. Note that the interface must be
1409 pdst = &ifra->ifra_dstaddr;
1410 if (pdst->sin6_family == AF_INET6 &&
1411 !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1412 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1413 (rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST) != 0)) {
1414 nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
1415 "remove a route to the old destination: %s\n",
1416 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1417 /* proceed anyway... */
1419 ia->ia_flags &= ~IFA_ROUTE;
1420 ia->ia_dstaddr = *pdst;
1424 * If a new destination address is specified for a point-to-point
1425 * interface, install a route to the destination as an interface
1427 * XXX: the logic below rejects assigning multiple addresses on a p2p
1428 * interface that share the same destination.
1430 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1431 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1432 ia->ia_dstaddr.sin6_family == AF_INET6) {
1433 int rtflags = RTF_UP | RTF_HOST;
1435 * Handle the case for ::1 .
1437 if (ifp->if_flags & IFF_LOOPBACK)
1438 ia->ia_flags |= IFA_RTSELF;
1439 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1442 ia->ia_flags |= IFA_ROUTE;
1446 * add a loopback route to self if not exists
1448 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1449 error = ifa_add_loopback_route((struct ifaddr *)ia,
1450 (struct sockaddr *)&ia->ia_addr);
1452 ia->ia_flags |= IFA_RTSELF;
1455 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1456 "Invoking IPv6 network device address event may sleep");
1458 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
1464 * Find an IPv6 interface link-local address specific to an interface.
1465 * ifaddr is returned referenced.
1468 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1473 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1474 if (ifa->ifa_addr->sa_family != AF_INET6)
1476 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1477 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1484 IF_ADDR_RUNLOCK(ifp);
1486 return ((struct in6_ifaddr *)ifa);
1491 * find the interface address corresponding to a given IPv6 address.
1492 * ifaddr is returned referenced.
1495 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid)
1497 struct rm_priotracker in6_ifa_tracker;
1498 struct in6_ifaddr *ia;
1500 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1501 LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
1502 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
1504 zoneid != ia->ia_addr.sin6_scope_id)
1506 ifa_ref(&ia->ia_ifa);
1510 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1515 * find the internet address corresponding to a given interface and address.
1516 * ifaddr is returned referenced.
1519 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr)
1524 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1525 if (ifa->ifa_addr->sa_family != AF_INET6)
1527 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1532 IF_ADDR_RUNLOCK(ifp);
1534 return ((struct in6_ifaddr *)ifa);
1538 * Find a link-local scoped address on ifp and return it if any.
1541 in6ifa_llaonifp(struct ifnet *ifp)
1543 struct sockaddr_in6 *sin6;
1546 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1549 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1550 if (ifa->ifa_addr->sa_family != AF_INET6)
1552 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1553 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1554 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1555 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1558 IF_ADDR_RUNLOCK(ifp);
1560 return ((struct in6_ifaddr *)ifa);
1564 * Convert IP6 address to printable (loggable) representation. Caller
1565 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1567 static char digits[] = "0123456789abcdef";
1569 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1571 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1573 const u_int16_t *a = (const u_int16_t *)addr;
1575 int dcolon = 0, zero = 0;
1579 for (i = 0; i < 8; i++) {
1580 if (*(a + i) == 0) {
1585 else if (maxcnt < cnt) {
1596 for (i = 0; i < 8; i++) {
1607 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1619 d = (const u_char *)a;
1620 /* Try to eliminate leading zeros in printout like in :0001. */
1622 *cp = digits[*d >> 4];
1627 *cp = digits[*d++ & 0xf];
1628 if (zero == 0 || (*cp != '0')) {
1632 *cp = digits[*d >> 4];
1633 if (zero == 0 || (*cp != '0')) {
1637 *cp++ = digits[*d & 0xf];
1646 in6_localaddr(struct in6_addr *in6)
1648 struct rm_priotracker in6_ifa_tracker;
1649 struct in6_ifaddr *ia;
1651 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1654 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1655 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1656 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1657 &ia->ia_prefixmask.sin6_addr)) {
1658 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1662 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1668 * Return 1 if an internet address is for the local host and configured
1669 * on one of its interfaces.
1672 in6_localip(struct in6_addr *in6)
1674 struct rm_priotracker in6_ifa_tracker;
1675 struct in6_ifaddr *ia;
1677 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1678 LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1679 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
1680 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1684 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1689 * Return 1 if an internet address is configured on an interface.
1692 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
1694 struct in6_addr in6;
1696 struct in6_ifaddr *ia6;
1699 if (in6_clearscope(&in6))
1701 in6_setscope(&in6, ifp, NULL);
1704 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1705 if (ifa->ifa_addr->sa_family != AF_INET6)
1707 ia6 = (struct in6_ifaddr *)ifa;
1708 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6)) {
1709 IF_ADDR_RUNLOCK(ifp);
1713 IF_ADDR_RUNLOCK(ifp);
1719 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1721 struct rm_priotracker in6_ifa_tracker;
1722 struct in6_ifaddr *ia;
1724 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1725 LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
1726 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
1727 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
1728 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1729 return (1); /* true */
1734 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1736 return (0); /* false */
1740 * return length of part which dst and src are equal
1744 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1747 u_char *s = (u_char *)src, *d = (u_char *)dst;
1748 u_char *lim = s + 16, r;
1751 if ((r = (*d++ ^ *s++)) != 0) {
1762 /* XXX: to be scope conscious */
1764 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1766 int bytelen, bitlen;
1769 if (0 > len || len > 128) {
1770 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1778 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1781 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1782 p2->s6_addr[bytelen] >> (8 - bitlen))
1789 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1791 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1792 int bytelen, bitlen, i;
1795 if (0 > len || len > 128) {
1796 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1801 bzero(maskp, sizeof(*maskp));
1804 for (i = 0; i < bytelen; i++)
1805 maskp->s6_addr[i] = 0xff;
1807 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1811 * return the best address out of the same scope. if no address was
1812 * found, return the first valid address from designated IF.
1815 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1817 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1819 struct in6_ifaddr *besta = NULL;
1820 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1822 dep[0] = dep[1] = NULL;
1825 * We first look for addresses in the same scope.
1826 * If there is one, return it.
1827 * If two or more, return one which matches the dst longest.
1828 * If none, return one of global addresses assigned other ifs.
1831 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1832 if (ifa->ifa_addr->sa_family != AF_INET6)
1834 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1835 continue; /* XXX: is there any case to allow anycast? */
1836 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1837 continue; /* don't use this interface */
1838 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1840 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1841 if (V_ip6_use_deprecated)
1842 dep[0] = (struct in6_ifaddr *)ifa;
1846 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1848 * call in6_matchlen() as few as possible
1852 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1853 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1856 besta = (struct in6_ifaddr *)ifa;
1859 besta = (struct in6_ifaddr *)ifa;
1863 ifa_ref(&besta->ia_ifa);
1864 IF_ADDR_RUNLOCK(ifp);
1868 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1869 if (ifa->ifa_addr->sa_family != AF_INET6)
1871 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1872 continue; /* XXX: is there any case to allow anycast? */
1873 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1874 continue; /* don't use this interface */
1875 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1877 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1878 if (V_ip6_use_deprecated)
1879 dep[1] = (struct in6_ifaddr *)ifa;
1885 IF_ADDR_RUNLOCK(ifp);
1886 return (struct in6_ifaddr *)ifa;
1889 /* use the last-resort values, that are, deprecated addresses */
1891 ifa_ref((struct ifaddr *)dep[0]);
1892 IF_ADDR_RUNLOCK(ifp);
1896 ifa_ref((struct ifaddr *)dep[1]);
1897 IF_ADDR_RUNLOCK(ifp);
1901 IF_ADDR_RUNLOCK(ifp);
1906 * perform DAD when interface becomes IFF_UP.
1909 in6_if_up(struct ifnet *ifp)
1912 struct in6_ifaddr *ia;
1915 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1916 if (ifa->ifa_addr->sa_family != AF_INET6)
1918 ia = (struct in6_ifaddr *)ifa;
1919 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
1921 * The TENTATIVE flag was likely set by hand
1922 * beforehand, implicitly indicating the need for DAD.
1923 * We may be able to skip the random delay in this
1924 * case, but we impose delays just in case.
1927 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
1930 IF_ADDR_RUNLOCK(ifp);
1933 * special cases, like 6to4, are handled in in6_ifattach
1935 in6_ifattach(ifp, NULL);
1939 in6if_do_dad(struct ifnet *ifp)
1941 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1944 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) ||
1945 (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD))
1949 * Our DAD routine requires the interface up and running.
1950 * However, some interfaces can be up before the RUNNING
1951 * status. Additionally, users may try to assign addresses
1952 * before the interface becomes up (or running).
1953 * This function returns EAGAIN in that case.
1954 * The caller should mark "tentative" on the address instead of
1955 * performing DAD immediately.
1957 if (!((ifp->if_flags & IFF_UP) &&
1958 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
1965 * Calculate max IPv6 MTU through all the interfaces and store it
1971 unsigned long maxmtu = 0;
1974 IFNET_RLOCK_NOSLEEP();
1975 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1976 /* this function can be called during ifnet initialization */
1977 if (!ifp->if_afdata[AF_INET6])
1979 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
1980 IN6_LINKMTU(ifp) > maxmtu)
1981 maxmtu = IN6_LINKMTU(ifp);
1983 IFNET_RUNLOCK_NOSLEEP();
1984 if (maxmtu) /* update only when maxmtu is positive */
1985 V_in6_maxmtu = maxmtu;
1989 * Provide the length of interface identifiers to be used for the link attached
1990 * to the given interface. The length should be defined in "IPv6 over
1991 * xxx-link" document. Note that address architecture might also define
1992 * the length for a particular set of address prefixes, regardless of the
1993 * link type. As clarified in rfc2462bis, those two definitions should be
1994 * consistent, and those really are as of August 2004.
1997 in6_if2idlen(struct ifnet *ifp)
1999 switch (ifp->if_type) {
2000 case IFT_ETHER: /* RFC2464 */
2001 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2002 case IFT_L2VLAN: /* ditto */
2003 case IFT_BRIDGE: /* bridge(4) only does Ethernet-like links */
2004 case IFT_INFINIBAND:
2006 case IFT_PPP: /* RFC2472 */
2008 case IFT_FRELAY: /* RFC2590 */
2010 case IFT_IEEE1394: /* RFC3146 */
2013 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2015 return (64); /* XXX: is this really correct? */
2018 * Unknown link type:
2019 * It might be controversial to use the today's common constant
2020 * of 64 for these cases unconditionally. For full compliance,
2021 * we should return an error in this case. On the other hand,
2022 * if we simply miss the standard for the link type or a new
2023 * standard is defined for a new link type, the IFID length
2024 * is very likely to be the common constant. As a compromise,
2025 * we always use the constant, but make an explicit notice
2026 * indicating the "unknown" case.
2028 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2033 #include <sys/sysctl.h>
2035 struct in6_llentry {
2036 struct llentry base;
2039 #define IN6_LLTBL_DEFAULT_HSIZE 32
2040 #define IN6_LLTBL_HASH(k, h) \
2041 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
2044 * Do actual deallocation of @lle.
2047 in6_lltable_destroy_lle_unlocked(struct llentry *lle)
2050 LLE_LOCK_DESTROY(lle);
2051 LLE_REQ_DESTROY(lle);
2052 free(lle, M_LLTABLE);
2056 * Called by LLE_FREE_LOCKED when number of references
2060 in6_lltable_destroy_lle(struct llentry *lle)
2064 in6_lltable_destroy_lle_unlocked(lle);
2067 static struct llentry *
2068 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
2070 struct in6_llentry *lle;
2072 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2073 if (lle == NULL) /* NB: caller generates msg */
2076 lle->base.r_l3addr.addr6 = *addr6;
2077 lle->base.lle_refcnt = 1;
2078 lle->base.lle_free = in6_lltable_destroy_lle;
2079 LLE_LOCK_INIT(&lle->base);
2080 LLE_REQ_INIT(&lle->base);
2081 callout_init(&lle->base.lle_timer, 1);
2083 return (&lle->base);
2087 in6_lltable_match_prefix(const struct sockaddr *saddr,
2088 const struct sockaddr *smask, u_int flags, struct llentry *lle)
2090 const struct in6_addr *addr, *mask, *lle_addr;
2092 addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr;
2093 mask = &((const struct sockaddr_in6 *)smask)->sin6_addr;
2094 lle_addr = &lle->r_l3addr.addr6;
2096 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
2099 if (lle->la_flags & LLE_IFADDR) {
2102 * Delete LLE_IFADDR records IFF address & flag matches.
2103 * Note that addr is the interface address within prefix
2106 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) &&
2107 (flags & LLE_STATIC) != 0)
2112 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2113 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
2120 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2124 LLE_WLOCK_ASSERT(lle);
2125 KASSERT(llt != NULL, ("lltable is NULL"));
2127 /* Unlink entry from table */
2128 if ((lle->la_flags & LLE_LINKED) != 0) {
2131 IF_AFDATA_WLOCK_ASSERT(ifp);
2132 lltable_unlink_entry(llt, lle);
2135 if (callout_stop(&lle->lle_timer) > 0)
2142 in6_lltable_rtcheck(struct ifnet *ifp,
2144 const struct sockaddr *l3addr)
2146 const struct sockaddr_in6 *sin6;
2147 struct nhop6_basic nh6;
2148 struct in6_addr dst;
2151 char ip6buf[INET6_ADDRSTRLEN];
2154 KASSERT(l3addr->sa_family == AF_INET6,
2155 ("sin_family %d", l3addr->sa_family));
2157 sin6 = (const struct sockaddr_in6 *)l3addr;
2158 in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
2159 fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib;
2160 error = fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6);
2161 if (error != 0 || (nh6.nh_flags & NHF_GATEWAY) || nh6.nh_ifp != ifp) {
2164 * Create an ND6 cache for an IPv6 neighbor
2165 * that is not covered by our own prefix.
2167 ifa = ifaof_ifpforaddr(l3addr, ifp);
2172 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2173 ip6_sprintf(ip6buf, &sin6->sin6_addr));
2180 * Called by the datapath to indicate that the entry was used.
2183 in6_lltable_mark_used(struct llentry *lle)
2187 lle->r_skip_req = 0;
2190 * Set the hit time so the callback function
2191 * can determine the remaining time before
2192 * transiting to the DELAY state.
2194 lle->lle_hittime = time_uptime;
2195 LLE_REQ_UNLOCK(lle);
2198 static inline uint32_t
2199 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
2202 return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize));
2206 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
2209 return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
2213 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2215 struct sockaddr_in6 *sin6;
2217 sin6 = (struct sockaddr_in6 *)sa;
2218 bzero(sin6, sizeof(*sin6));
2219 sin6->sin6_family = AF_INET6;
2220 sin6->sin6_len = sizeof(*sin6);
2221 sin6->sin6_addr = lle->r_l3addr.addr6;
2224 static inline struct llentry *
2225 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
2227 struct llentry *lle;
2228 struct llentries *lleh;
2231 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
2232 lleh = &llt->lle_head[hashidx];
2233 LIST_FOREACH(lle, lleh, lle_next) {
2234 if (lle->la_flags & LLE_DELETED)
2236 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
2244 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2247 lle->la_flags |= LLE_DELETED;
2248 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2250 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2255 static struct llentry *
2256 in6_lltable_alloc(struct lltable *llt, u_int flags,
2257 const struct sockaddr *l3addr)
2259 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2260 struct ifnet *ifp = llt->llt_ifp;
2261 struct llentry *lle;
2262 char linkhdr[LLE_MAX_LINKHDR];
2266 KASSERT(l3addr->sa_family == AF_INET6,
2267 ("sin_family %d", l3addr->sa_family));
2270 * A route that covers the given address must have
2271 * been installed 1st because we are doing a resolution,
2274 if (!(flags & LLE_IFADDR) &&
2275 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2278 lle = in6_lltable_new(&sin6->sin6_addr, flags);
2280 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2283 lle->la_flags = flags;
2284 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2285 linkhdrsize = LLE_MAX_LINKHDR;
2286 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp),
2287 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
2288 in6_lltable_destroy_lle_unlocked(lle);
2291 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
2293 lle->la_flags |= LLE_STATIC;
2296 if ((lle->la_flags & LLE_STATIC) != 0)
2297 lle->ln_state = ND6_LLINFO_REACHABLE;
2302 static struct llentry *
2303 in6_lltable_lookup(struct lltable *llt, u_int flags,
2304 const struct sockaddr *l3addr)
2306 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2307 struct llentry *lle;
2309 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2310 KASSERT(l3addr->sa_family == AF_INET6,
2311 ("sin_family %d", l3addr->sa_family));
2313 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2318 KASSERT((flags & (LLE_UNLOCKED|LLE_EXCLUSIVE)) !=
2319 (LLE_UNLOCKED|LLE_EXCLUSIVE),("wrong lle request flags: 0x%X",
2322 if (flags & LLE_UNLOCKED)
2325 if (flags & LLE_EXCLUSIVE)
2333 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2334 struct sysctl_req *wr)
2336 struct ifnet *ifp = llt->llt_ifp;
2339 struct rt_msghdr rtm;
2340 struct sockaddr_in6 sin6;
2342 * ndp.c assumes that sdl is word aligned
2347 struct sockaddr_dl sdl;
2349 struct sockaddr_dl *sdl;
2352 bzero(&ndpc, sizeof(ndpc));
2353 /* skip deleted entries */
2354 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
2356 /* Skip if jailed and not a valid IP of the prison. */
2357 lltable_fill_sa_entry(lle, (struct sockaddr *)&ndpc.sin6);
2358 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&ndpc.sin6) != 0)
2361 * produce a msg made of:
2363 * struct sockaddr_in6 (IPv6)
2364 * struct sockaddr_dl;
2366 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2367 ndpc.rtm.rtm_version = RTM_VERSION;
2368 ndpc.rtm.rtm_type = RTM_GET;
2369 ndpc.rtm.rtm_flags = RTF_UP;
2370 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2371 if (V_deembed_scopeid)
2372 sa6_recoverscope(&ndpc.sin6);
2375 if (lle->la_flags & LLE_PUB)
2376 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2379 sdl->sdl_family = AF_LINK;
2380 sdl->sdl_len = sizeof(*sdl);
2381 sdl->sdl_index = ifp->if_index;
2382 sdl->sdl_type = ifp->if_type;
2383 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2384 sdl->sdl_alen = ifp->if_addrlen;
2385 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2388 bzero(LLADDR(sdl), ifp->if_addrlen);
2390 if (lle->la_expire != 0)
2391 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire +
2392 lle->lle_remtime / hz + time_second - time_uptime;
2393 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2394 if (lle->la_flags & LLE_STATIC)
2395 ndpc.rtm.rtm_flags |= RTF_STATIC;
2396 if (lle->la_flags & LLE_IFADDR)
2397 ndpc.rtm.rtm_flags |= RTF_PINNED;
2398 if (lle->ln_router != 0)
2399 ndpc.rtm.rtm_flags |= RTF_GATEWAY;
2400 ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked;
2401 /* Store state in rmx_weight value */
2402 ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state;
2403 ndpc.rtm.rtm_index = ifp->if_index;
2404 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2409 static struct lltable *
2410 in6_lltattach(struct ifnet *ifp)
2412 struct lltable *llt;
2414 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
2415 llt->llt_af = AF_INET6;
2418 llt->llt_lookup = in6_lltable_lookup;
2419 llt->llt_alloc_entry = in6_lltable_alloc;
2420 llt->llt_delete_entry = in6_lltable_delete_entry;
2421 llt->llt_dump_entry = in6_lltable_dump_entry;
2422 llt->llt_hash = in6_lltable_hash;
2423 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
2424 llt->llt_free_entry = in6_lltable_free_entry;
2425 llt->llt_match_prefix = in6_lltable_match_prefix;
2426 llt->llt_mark_used = in6_lltable_mark_used;
2433 in6_domifattach(struct ifnet *ifp)
2435 struct in6_ifextra *ext;
2437 /* There are not IPv6-capable interfaces. */
2438 switch (ifp->if_type) {
2444 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2445 bzero(ext, sizeof(*ext));
2447 ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2448 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2449 COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2450 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2452 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2453 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2455 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2456 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2458 ext->nd_ifinfo = nd6_ifattach(ifp);
2459 ext->scope6_id = scope6_ifattach(ifp);
2460 ext->lltable = in6_lltattach(ifp);
2462 ext->mld_ifinfo = mld_domifattach(ifp);
2468 in6_domifmtu(struct ifnet *ifp)
2470 if (ifp->if_afdata[AF_INET6] == NULL)
2473 return (IN6_LINKMTU(ifp));
2477 in6_domifdetach(struct ifnet *ifp, void *aux)
2479 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2481 mld_domifdetach(ifp);
2482 scope6_ifdetach(ext->scope6_id);
2483 nd6_ifdetach(ifp, ext->nd_ifinfo);
2484 lltable_free(ext->lltable);
2485 COUNTER_ARRAY_FREE(ext->in6_ifstat,
2486 sizeof(struct in6_ifstat) / sizeof(uint64_t));
2487 free(ext->in6_ifstat, M_IFADDR);
2488 COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2489 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2490 free(ext->icmp6_ifstat, M_IFADDR);
2491 free(ext, M_IFADDR);
2495 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2496 * v4 mapped addr or v4 compat addr
2499 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2502 bzero(sin, sizeof(*sin));
2503 sin->sin_len = sizeof(struct sockaddr_in);
2504 sin->sin_family = AF_INET;
2505 sin->sin_port = sin6->sin6_port;
2506 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2509 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2511 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2513 bzero(sin6, sizeof(*sin6));
2514 sin6->sin6_len = sizeof(struct sockaddr_in6);
2515 sin6->sin6_family = AF_INET6;
2516 sin6->sin6_port = sin->sin_port;
2517 sin6->sin6_addr.s6_addr32[0] = 0;
2518 sin6->sin6_addr.s6_addr32[1] = 0;
2519 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2520 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2523 /* Convert sockaddr_in6 into sockaddr_in. */
2525 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2527 struct sockaddr_in *sin_p;
2528 struct sockaddr_in6 sin6;
2531 * Save original sockaddr_in6 addr and convert it
2534 sin6 = *(struct sockaddr_in6 *)nam;
2535 sin_p = (struct sockaddr_in *)nam;
2536 in6_sin6_2_sin(sin_p, &sin6);
2539 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2541 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2543 struct sockaddr_in *sin_p;
2544 struct sockaddr_in6 *sin6_p;
2546 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2547 sin_p = (struct sockaddr_in *)*nam;
2548 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2549 free(*nam, M_SONAME);
2550 *nam = (struct sockaddr *)sin6_p;