2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)in.c 8.2 (Berkeley) 11/15/93
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include "opt_compat.h"
68 #include "opt_inet6.h"
70 #include <sys/param.h>
71 #include <sys/eventhandler.h>
72 #include <sys/errno.h>
74 #include <sys/malloc.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/systm.h>
82 #include <sys/kernel.h>
84 #include <sys/rmlock.h>
85 #include <sys/syslog.h>
88 #include <net/if_var.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <net/if_dl.h>
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <net/if_llatbl.h>
97 #include <netinet/if_ether.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet/ip_carp.h>
103 #include <netinet/ip6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet6/nd6.h>
106 #include <netinet6/mld6_var.h>
107 #include <netinet6/ip6_mroute.h>
108 #include <netinet6/in6_ifattach.h>
109 #include <netinet6/scope6_var.h>
110 #include <netinet6/in6_fib.h>
111 #include <netinet6/in6_pcb.h>
113 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
114 #define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix)
117 * Definitions of some costant IP6 addresses.
119 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
120 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
121 const struct in6_addr in6addr_nodelocal_allnodes =
122 IN6ADDR_NODELOCAL_ALLNODES_INIT;
123 const struct in6_addr in6addr_linklocal_allnodes =
124 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
125 const struct in6_addr in6addr_linklocal_allrouters =
126 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
127 const struct in6_addr in6addr_linklocal_allv2routers =
128 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
130 const struct in6_addr in6mask0 = IN6MASK0;
131 const struct in6_addr in6mask32 = IN6MASK32;
132 const struct in6_addr in6mask64 = IN6MASK64;
133 const struct in6_addr in6mask96 = IN6MASK96;
134 const struct in6_addr in6mask128 = IN6MASK128;
136 const struct sockaddr_in6 sa6_any =
137 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
139 static int in6_notify_ifa(struct ifnet *, struct in6_ifaddr *,
140 struct in6_aliasreq *, int);
141 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
143 static int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *,
144 struct in6_ifaddr *, int);
145 static struct in6_ifaddr *in6_alloc_ifa(struct ifnet *,
146 struct in6_aliasreq *, int flags);
147 static int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *,
148 struct in6_ifaddr *, int, int);
149 static int in6_broadcast_ifa(struct ifnet *, struct in6_aliasreq *,
150 struct in6_ifaddr *, int);
152 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
153 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
157 in6_newaddrmsg(struct in6_ifaddr *ia, int cmd)
159 struct sockaddr_dl gateway;
160 struct sockaddr_in6 mask, addr;
165 * initialize for rtmsg generation
167 bzero(&gateway, sizeof(gateway));
168 gateway.sdl_len = sizeof(gateway);
169 gateway.sdl_family = AF_LINK;
171 bzero(&rt, sizeof(rt));
172 rt.rt_gateway = (struct sockaddr *)&gateway;
173 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
174 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
175 rt_mask(&rt) = (struct sockaddr *)&mask;
176 rt_key(&rt) = (struct sockaddr *)&addr;
177 rt.rt_flags = RTF_HOST | RTF_STATIC;
179 rt.rt_flags |= RTF_UP;
180 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS : ia62ifa(ia)->ifa_ifp->if_fib;
181 /* Announce arrival of local address to this FIB. */
182 rt_newaddrmsg_fib(cmd, &ia->ia_ifa, 0, &rt, fibnum);
186 in6_mask2len(struct in6_addr *mask, u_char *lim0)
189 u_char *lim = lim0, *p;
191 /* ignore the scope_id part */
192 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
193 lim = (u_char *)mask + sizeof(*mask);
194 for (p = (u_char *)mask; p < lim; x++, p++) {
200 for (y = 0; y < 8; y++) {
201 if ((*p & (0x80 >> y)) == 0)
207 * when the limit pointer is given, do a stricter check on the
211 if (y != 0 && (*p & (0x00ff >> y)) != 0)
213 for (p = p + 1; p < lim; p++)
221 #ifdef COMPAT_FREEBSD32
222 struct in6_ndifreq32 {
223 char ifname[IFNAMSIZ];
226 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
230 in6_control(struct socket *so, u_long cmd, caddr_t data,
231 struct ifnet *ifp, struct thread *td)
233 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
234 struct in6_ifaddr *ia = NULL;
235 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
236 struct sockaddr_in6 *sa6;
237 int carp_attached = 0;
242 * Compat to make pre-10.x ifconfig(8) operable.
244 if (cmd == OSIOCAIFADDR_IN6)
245 cmd = SIOCAIFADDR_IN6;
248 case SIOCGETSGCNT_IN6:
249 case SIOCGETMIFCNT_IN6:
251 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
252 * We cannot see how that would be needed, so do not adjust the
253 * KPI blindly; more likely should clean up the IPv4 variant.
255 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
259 case SIOCAADDRCTL_POLICY:
260 case SIOCDADDRCTL_POLICY:
262 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
266 return (in6_src_ioctl(cmd, data));
273 case SIOCSNDFLUSH_IN6:
274 case SIOCSPFXFLUSH_IN6:
275 case SIOCSRTRFLUSH_IN6:
276 case SIOCSDEFIFACE_IN6:
277 case SIOCSIFINFO_FLAGS:
278 case SIOCSIFINFO_IN6:
280 error = priv_check(td, PRIV_NETINET_ND6);
285 case OSIOCGIFINFO_IN6:
286 case SIOCGIFINFO_IN6:
287 case SIOCGNBRINFO_IN6:
288 case SIOCGDEFIFACE_IN6:
289 return (nd6_ioctl(cmd, data, ifp));
291 #ifdef COMPAT_FREEBSD32
292 case SIOCGDEFIFACE32_IN6:
294 struct in6_ndifreq ndif;
295 struct in6_ndifreq32 *ndif32;
297 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
301 ndif32 = (struct in6_ndifreq32 *)data;
302 ndif32->ifindex = ndif.ifindex;
309 case SIOCSIFPREFIX_IN6:
310 case SIOCDIFPREFIX_IN6:
311 case SIOCAIFPREFIX_IN6:
312 case SIOCCIFPREFIX_IN6:
313 case SIOCSGIFPREFIX_IN6:
314 case SIOCGIFPREFIX_IN6:
316 "prefix ioctls are now invalidated. "
317 "please use ifconfig.\n");
324 error = priv_check(td, PRIV_NETINET_SCOPE6);
331 return (scope6_ioctl(cmd, data, ifp));
335 * Find address for this interface, if it exists.
337 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
338 * only, and used the first interface address as the target of other
339 * operations (without checking ifra_addr). This was because netinet
340 * code/API assumed at most 1 interface address per interface.
341 * Since IPv6 allows a node to assign multiple addresses
342 * on a single interface, we almost always look and check the
343 * presence of ifra_addr, and reject invalid ones here.
344 * It also decreases duplicated code among SIOC*_IN6 operations.
347 case SIOCAIFADDR_IN6:
348 case SIOCSIFPHYADDR_IN6:
349 sa6 = &ifra->ifra_addr;
351 case SIOCSIFADDR_IN6:
352 case SIOCGIFADDR_IN6:
353 case SIOCSIFDSTADDR_IN6:
354 case SIOCSIFNETMASK_IN6:
355 case SIOCGIFDSTADDR_IN6:
356 case SIOCGIFNETMASK_IN6:
357 case SIOCDIFADDR_IN6:
358 case SIOCGIFPSRCADDR_IN6:
359 case SIOCGIFPDSTADDR_IN6:
360 case SIOCGIFAFLAG_IN6:
361 case SIOCSNDFLUSH_IN6:
362 case SIOCSPFXFLUSH_IN6:
363 case SIOCSRTRFLUSH_IN6:
364 case SIOCGIFALIFETIME_IN6:
365 case SIOCGIFSTAT_IN6:
366 case SIOCGIFSTAT_ICMP6:
367 sa6 = &ifr->ifr_addr;
374 * Although we should pass any non-INET6 ioctl requests
375 * down to driver, we filter some legacy INET requests.
376 * Drivers trust SIOCSIFADDR et al to come from an already
377 * privileged layer, and do not perform any credentials
378 * checks or input validation.
385 if (sa6 && sa6->sin6_family == AF_INET6) {
386 if (sa6->sin6_scope_id != 0)
387 error = sa6_embedscope(sa6, 0);
389 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
392 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
393 &sa6->sin6_addr)) != 0)
395 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
400 case SIOCSIFADDR_IN6:
401 case SIOCSIFDSTADDR_IN6:
402 case SIOCSIFNETMASK_IN6:
404 * Since IPv6 allows a node to assign multiple addresses
405 * on a single interface, SIOCSIFxxx ioctls are deprecated.
407 /* we decided to obsolete this command (20000704) */
411 case SIOCDIFADDR_IN6:
413 * for IPv4, we look for existing in_ifaddr here to allow
414 * "ifconfig if0 delete" to remove the first IPv4 address on
415 * the interface. For IPv6, as the spec allows multiple
416 * interface address from the day one, we consider "remove the
417 * first one" semantics to be not preferable.
420 error = EADDRNOTAVAIL;
424 case SIOCAIFADDR_IN6:
426 * We always require users to specify a valid IPv6 address for
427 * the corresponding operation.
429 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
430 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
431 error = EAFNOSUPPORT;
436 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
437 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
442 case SIOCGIFSTAT_IN6:
443 case SIOCGIFSTAT_ICMP6:
444 if (ifp->if_afdata[AF_INET6] == NULL) {
445 error = EPFNOSUPPORT;
450 case SIOCGIFADDR_IN6:
451 /* This interface is basically deprecated. use SIOCGIFCONF. */
453 case SIOCGIFAFLAG_IN6:
454 case SIOCGIFNETMASK_IN6:
455 case SIOCGIFDSTADDR_IN6:
456 case SIOCGIFALIFETIME_IN6:
457 /* must think again about its semantics */
459 error = EADDRNOTAVAIL;
466 case SIOCGIFADDR_IN6:
467 ifr->ifr_addr = ia->ia_addr;
468 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
472 case SIOCGIFDSTADDR_IN6:
473 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
478 * XXX: should we check if ifa_dstaddr is NULL and return
481 ifr->ifr_dstaddr = ia->ia_dstaddr;
482 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
486 case SIOCGIFNETMASK_IN6:
487 ifr->ifr_addr = ia->ia_prefixmask;
490 case SIOCGIFAFLAG_IN6:
491 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
494 case SIOCGIFSTAT_IN6:
495 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
496 ifp->if_afdata[AF_INET6])->in6_ifstat,
497 &ifr->ifr_ifru.ifru_stat,
498 sizeof(struct in6_ifstat) / sizeof(uint64_t));
501 case SIOCGIFSTAT_ICMP6:
502 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
503 ifp->if_afdata[AF_INET6])->icmp6_ifstat,
504 &ifr->ifr_ifru.ifru_icmp6stat,
505 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
508 case SIOCGIFALIFETIME_IN6:
509 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
510 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
512 struct in6_addrlifetime *retlt =
513 &ifr->ifr_ifru.ifru_lifetime;
516 * XXX: adjust expiration time assuming time_t is
520 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
521 if (ia->ia6_lifetime.ia6t_vltime <
522 maxexpire - ia->ia6_updatetime) {
523 retlt->ia6t_expire = ia->ia6_updatetime +
524 ia->ia6_lifetime.ia6t_vltime;
526 retlt->ia6t_expire = maxexpire;
528 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
530 struct in6_addrlifetime *retlt =
531 &ifr->ifr_ifru.ifru_lifetime;
534 * XXX: adjust expiration time assuming time_t is
538 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
539 if (ia->ia6_lifetime.ia6t_pltime <
540 maxexpire - ia->ia6_updatetime) {
541 retlt->ia6t_preferred = ia->ia6_updatetime +
542 ia->ia6_lifetime.ia6t_pltime;
544 retlt->ia6t_preferred = maxexpire;
548 case SIOCAIFADDR_IN6:
550 struct nd_prefixctl pr0;
551 struct nd_prefix *pr;
554 * first, make or update the interface address structure,
555 * and link it to the list.
557 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
560 if (ia->ia_ifa.ifa_carp)
561 (*carp_detach_p)(&ia->ia_ifa, true);
562 ifa_free(&ia->ia_ifa);
564 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
567 * this can happen when the user specify the 0 valid
573 if (cmd == ocmd && ifra->ifra_vhid > 0) {
574 if (carp_attach_p != NULL)
575 error = (*carp_attach_p)(&ia->ia_ifa,
578 error = EPROTONOSUPPORT;
586 * then, make the prefix on-link on the interface.
587 * XXX: we'd rather create the prefix before the address, but
588 * we need at least one address to install the corresponding
589 * interface route, so we configure the address first.
593 * convert mask to prefix length (prefixmask has already
594 * been validated in in6_update_ifa().
596 bzero(&pr0, sizeof(pr0));
598 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
600 if (pr0.ndpr_plen == 128) {
601 /* we don't need to install a host route. */
604 pr0.ndpr_prefix = ifra->ifra_addr;
605 /* apply the mask for safety. */
606 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr,
607 &ifra->ifra_prefixmask.sin6_addr);
610 * XXX: since we don't have an API to set prefix (not address)
611 * lifetimes, we just use the same lifetimes as addresses.
612 * The (temporarily) installed lifetimes can be overridden by
613 * later advertised RAs (when accept_rtadv is non 0), which is
614 * an intended behavior.
616 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
618 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
619 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
620 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
622 /* add the prefix if not yet. */
623 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
625 * nd6_prelist_add will install the corresponding
628 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
630 (*carp_detach_p)(&ia->ia_ifa, false);
635 /* relate the address to the prefix */
636 if (ia->ia6_ndpr == NULL) {
641 * If this is the first autoconf address from the
642 * prefix, create a temporary address as well
645 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
646 V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) {
648 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
649 log(LOG_NOTICE, "in6_control: failed "
650 "to create a temporary address, "
658 * this might affect the status of autoconfigured addresses,
659 * that is, this address might make other addresses detached.
661 pfxlist_onlink_check();
665 * Try to clear the flag when a new IPv6 address is added
666 * onto an IFDISABLED interface and it succeeds.
668 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
669 struct in6_ndireq nd;
671 memset(&nd, 0, sizeof(nd));
672 nd.ndi.flags = ND_IFINFO(ifp)->flags;
673 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
674 if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0)
675 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
676 "SIOCSIFINFO_FLAGS for -ifdisabled "
679 * Ignore failure of clearing the flag intentionally.
680 * The failure means address duplication was detected.
683 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
687 case SIOCDIFADDR_IN6:
689 struct nd_prefix *pr;
692 * If the address being deleted is the only one that owns
693 * the corresponding prefix, expire the prefix as well.
694 * XXX: theoretically, we don't have to worry about such
695 * relationship, since we separate the address management
696 * and the prefix management. We do this, however, to provide
697 * as much backward compatibility as possible in terms of
698 * the ioctl operation.
699 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
702 in6_purgeaddr(&ia->ia_ifa);
703 if (pr != NULL && pr->ndpr_addrcnt == 0) {
705 nd6_prefix_unlink(pr, NULL);
709 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
714 if (ifp->if_ioctl == NULL) {
718 error = (*ifp->if_ioctl)(ifp, cmd, data);
725 ifa_free(&ia->ia_ifa);
731 * Join necessary multicast groups. Factored out from in6_update_ifa().
732 * This entire work should only be done once, for the default FIB.
735 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
736 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
738 char ip6buf[INET6_ADDRSTRLEN];
739 struct in6_addr mltaddr;
740 struct in6_multi_mship *imm;
743 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
745 /* Join solicited multicast addr for new host id. */
746 bzero(&mltaddr, sizeof(struct in6_addr));
747 mltaddr.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
748 mltaddr.s6_addr32[2] = htonl(1);
749 mltaddr.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
750 mltaddr.s6_addr8[12] = 0xff;
751 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) {
752 /* XXX: should not happen */
753 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
757 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
759 * We need a random delay for DAD on the address being
760 * configured. It also means delaying transmission of the
761 * corresponding MLD report to avoid report collision.
762 * [RFC 4861, Section 6.3.7]
764 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
766 imm = in6_joingroup(ifp, &mltaddr, &error, delay);
768 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
769 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
770 if_name(ifp), error));
773 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
774 *in6m_sol = imm->i6mm_maddr;
777 * Join link-local all-nodes address.
779 mltaddr = in6addr_linklocal_allnodes;
780 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
781 goto cleanup; /* XXX: should not fail */
783 imm = in6_joingroup(ifp, &mltaddr, &error, 0);
785 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
786 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
787 if_name(ifp), error));
790 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
793 * Join node information group address.
796 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
798 * The spec does not say anything about delay for this group,
799 * but the same logic should apply.
801 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
803 if (in6_nigroup(ifp, NULL, -1, &mltaddr) == 0) {
805 imm = in6_joingroup(ifp, &mltaddr, &error, delay);
808 "%s: in6_joingroup failed for %s on %s "
809 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
810 &mltaddr), if_name(ifp), error));
811 /* XXX not very fatal, go on... */
813 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
815 if (V_icmp6_nodeinfo_oldmcprefix &&
816 in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr) == 0) {
817 imm = in6_joingroup(ifp, &mltaddr, &error, delay);
820 "%s: in6_joingroup failed for %s on %s "
821 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
822 &mltaddr), if_name(ifp), error));
823 /* XXX not very fatal, go on... */
825 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
829 * Join interface-local all-nodes address.
830 * (ff01::1%ifN, and ff01::%ifN/32)
832 mltaddr = in6addr_nodelocal_allnodes;
833 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
834 goto cleanup; /* XXX: should not fail */
836 imm = in6_joingroup(ifp, &mltaddr, &error, 0);
838 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
839 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
840 &mltaddr), if_name(ifp), error));
843 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
850 * Update parameters of an IPv6 interface address.
851 * If necessary, a new entry is created and linked into address chains.
852 * This function is separated from in6_control().
855 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
856 struct in6_ifaddr *ia, int flags)
858 int error, hostIsNew = 0;
860 if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0)
865 if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL)
869 error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags);
871 if (hostIsNew != 0) {
872 in6_unlink_ifa(ia, ifp);
873 ifa_free(&ia->ia_ifa);
879 error = in6_broadcast_ifa(ifp, ifra, ia, flags);
885 * Fill in basic IPv6 address request info.
888 in6_prepare_ifra(struct in6_aliasreq *ifra, const struct in6_addr *addr,
889 const struct in6_addr *mask)
892 memset(ifra, 0, sizeof(struct in6_aliasreq));
894 ifra->ifra_addr.sin6_family = AF_INET6;
895 ifra->ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
897 ifra->ifra_addr.sin6_addr = *addr;
899 ifra->ifra_prefixmask.sin6_family = AF_INET6;
900 ifra->ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
902 ifra->ifra_prefixmask.sin6_addr = *mask;
906 in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra,
907 struct in6_ifaddr *ia, int flags)
910 struct sockaddr_in6 dst6;
911 struct in6_addrlifetime *lt;
912 char ip6buf[INET6_ADDRSTRLEN];
914 /* Validate parameters */
915 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
919 * The destination address for a p2p link must have a family
920 * of AF_UNSPEC or AF_INET6.
922 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
923 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
924 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
925 return (EAFNOSUPPORT);
930 if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) ||
931 ifra->ifra_addr.sin6_family != AF_INET6)
935 * validate ifra_prefixmask. don't check sin6_family, netmask
936 * does not carry fields other than sin6_len.
938 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
941 * Because the IPv6 address architecture is classless, we require
942 * users to specify a (non 0) prefix length (mask) for a new address.
943 * We also require the prefix (when specified) mask is valid, and thus
944 * reject a non-consecutive mask.
946 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
948 if (ifra->ifra_prefixmask.sin6_len != 0) {
949 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
950 (u_char *)&ifra->ifra_prefixmask +
951 ifra->ifra_prefixmask.sin6_len);
956 * In this case, ia must not be NULL. We just use its prefix
959 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
962 * If the destination address on a p2p interface is specified,
963 * and the address is a scoped one, validate/set the scope
966 dst6 = ifra->ifra_dstaddr;
967 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
968 (dst6.sin6_family == AF_INET6)) {
969 struct in6_addr in6_tmp;
972 in6_tmp = dst6.sin6_addr;
973 if (in6_setscope(&in6_tmp, ifp, &zoneid))
974 return (EINVAL); /* XXX: should be impossible */
976 if (dst6.sin6_scope_id != 0) {
977 if (dst6.sin6_scope_id != zoneid)
979 } else /* user omit to specify the ID. */
980 dst6.sin6_scope_id = zoneid;
982 /* convert into the internal form */
983 if (sa6_embedscope(&dst6, 0))
984 return (EINVAL); /* XXX: should be impossible */
986 /* Modify original ifra_dstaddr to reflect changes */
987 ifra->ifra_dstaddr = dst6;
990 * The destination address can be specified only for a p2p or a
991 * loopback interface. If specified, the corresponding prefix length
994 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
995 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
996 /* XXX: noisy message */
997 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
998 "be specified for a p2p or a loopback IF only\n"));
1002 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1003 "be 128 when dstaddr is specified\n"));
1007 /* lifetime consistency check */
1008 lt = &ifra->ifra_lifetime;
1009 if (lt->ia6t_pltime > lt->ia6t_vltime)
1011 if (lt->ia6t_vltime == 0) {
1013 * the following log might be noisy, but this is a typical
1014 * configuration mistake or a tool's bug.
1017 "in6_update_ifa: valid lifetime is 0 for %s\n",
1018 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1021 return (0); /* there's nothing to do */
1024 /* Check prefix mask */
1025 if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) {
1027 * We prohibit changing the prefix length of an existing
1029 * + such an operation should be rare in IPv6, and
1030 * + the operation would confuse prefix management.
1032 if (ia->ia_prefixmask.sin6_len != 0 &&
1033 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1034 nd6log((LOG_INFO, "in6_validate_ifa: the prefix length "
1035 "of an existing %s address should not be changed\n",
1036 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1047 * Allocate a new ifaddr and link it into chains.
1049 static struct in6_ifaddr *
1050 in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
1052 struct in6_ifaddr *ia;
1055 * When in6_alloc_ifa() is called in a process of a received
1056 * RA, it is called under an interrupt context. So, we should
1057 * call malloc with M_NOWAIT.
1059 ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT);
1062 LIST_INIT(&ia->ia6_memberships);
1063 /* Initialize the address and masks, and put time stamp */
1064 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1065 ia->ia_addr.sin6_family = AF_INET6;
1066 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1067 /* XXX: Can we assign ,sin6_addr and skip the rest? */
1068 ia->ia_addr = ifra->ifra_addr;
1069 ia->ia6_createtime = time_uptime;
1070 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1072 * Some functions expect that ifa_dstaddr is not
1073 * NULL for p2p interfaces.
1075 ia->ia_ifa.ifa_dstaddr =
1076 (struct sockaddr *)&ia->ia_dstaddr;
1078 ia->ia_ifa.ifa_dstaddr = NULL;
1081 /* set prefix mask if any */
1082 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1083 if (ifra->ifra_prefixmask.sin6_len != 0) {
1084 ia->ia_prefixmask.sin6_family = AF_INET6;
1085 ia->ia_prefixmask.sin6_len = ifra->ifra_prefixmask.sin6_len;
1086 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr;
1090 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1092 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1093 IF_ADDR_WUNLOCK(ifp);
1095 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1097 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1098 LIST_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash);
1099 IN6_IFADDR_WUNLOCK();
1105 * Update/configure interface address parameters:
1107 * 1) Update lifetime
1108 * 2) Update interface metric ad flags
1109 * 3) Notify other subsystems
1112 in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra,
1113 struct in6_ifaddr *ia, int hostIsNew, int flags)
1117 /* update timestamp */
1118 ia->ia6_updatetime = time_uptime;
1121 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1122 * to see if the address is deprecated or invalidated, but initialize
1123 * these members for applications.
1125 ia->ia6_lifetime = ifra->ifra_lifetime;
1126 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1127 ia->ia6_lifetime.ia6t_expire =
1128 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1130 ia->ia6_lifetime.ia6t_expire = 0;
1131 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1132 ia->ia6_lifetime.ia6t_preferred =
1133 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1135 ia->ia6_lifetime.ia6t_preferred = 0;
1138 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1139 * userland, make it deprecated.
1141 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1142 ia->ia6_lifetime.ia6t_pltime = 0;
1143 ia->ia6_lifetime.ia6t_preferred = time_uptime;
1147 * configure address flags.
1149 ia->ia6_flags = ifra->ifra_flags;
1152 * Make the address tentative before joining multicast addresses,
1153 * so that corresponding MLD responses would not have a tentative
1156 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1159 * DAD should be performed for an new address or addresses on
1160 * an interface with ND6_IFF_IFDISABLED.
1162 if (in6if_do_dad(ifp) &&
1163 (hostIsNew || (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)))
1164 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1166 /* notify other subsystems */
1167 error = in6_notify_ifa(ifp, ia, ifra, hostIsNew);
1173 * Do link-level ifa job:
1174 * 1) Add lle entry for added address
1175 * 2) Notifies routing socket users about new address
1176 * 3) join appropriate multicast group
1177 * 4) start DAD if enabled
1180 in6_broadcast_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1181 struct in6_ifaddr *ia, int flags)
1183 struct in6_multi *in6m_sol;
1186 /* Add local address to lltable, if necessary (ex. on p2p link). */
1187 if ((error = nd6_add_ifa_lle(ia)) != 0) {
1188 in6_purgeaddr(&ia->ia_ifa);
1189 ifa_free(&ia->ia_ifa);
1193 /* Join necessary multicast groups. */
1195 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1196 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1198 in6_purgeaddr(&ia->ia_ifa);
1199 ifa_free(&ia->ia_ifa);
1204 /* Perform DAD, if the address is TENTATIVE. */
1205 if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1206 int delay, mindelay, maxdelay;
1209 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1211 * We need to impose a delay before sending an NS
1212 * for DAD. Check if we also needed a delay for the
1213 * corresponding MLD message. If we did, the delay
1214 * should be larger than the MLD delay (this could be
1215 * relaxed a bit, but this simple logic is at least
1217 * XXX: Break data hiding guidelines and look at
1218 * state for the solicited multicast group.
1221 if (in6m_sol != NULL &&
1222 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1223 mindelay = in6m_sol->in6m_timer;
1225 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1226 if (maxdelay - mindelay == 0)
1230 (arc4random() % (maxdelay - mindelay)) +
1234 nd6_dad_start((struct ifaddr *)ia, delay);
1237 in6_newaddrmsg(ia, RTM_ADD);
1238 ifa_free(&ia->ia_ifa);
1243 in6_purgeaddr(struct ifaddr *ifa)
1245 struct ifnet *ifp = ifa->ifa_ifp;
1246 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1247 struct in6_multi_mship *imm;
1251 (*carp_detach_p)(ifa, false);
1254 * Remove the loopback route to the interface address.
1255 * The check for the current setting of "nd6_useloopback"
1258 if (ia->ia_flags & IFA_RTSELF) {
1259 error = ifa_del_loopback_route((struct ifaddr *)ia,
1260 (struct sockaddr *)&ia->ia_addr);
1262 ia->ia_flags &= ~IFA_RTSELF;
1265 /* stop DAD processing */
1268 /* Leave multicast groups. */
1269 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1270 LIST_REMOVE(imm, i6mm_chain);
1271 in6_leavegroup(imm);
1273 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1274 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1275 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1276 (ia->ia_dstaddr.sin6_family == AF_INET6 ? RTF_HOST : 0));
1278 log(LOG_INFO, "%s: err=%d, destination address delete "
1279 "failed\n", __func__, error);
1280 ia->ia_flags &= ~IFA_ROUTE;
1283 in6_newaddrmsg(ia, RTM_DELETE);
1284 in6_unlink_ifa(ia, ifp);
1288 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1290 char ip6buf[INET6_ADDRSTRLEN];
1294 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1295 IF_ADDR_WUNLOCK(ifp);
1296 ifa_free(&ia->ia_ifa); /* if_addrhead */
1299 * Defer the release of what might be the last reference to the
1300 * in6_ifaddr so that it can't be freed before the remainder of the
1304 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1305 LIST_REMOVE(ia, ia6_hash);
1306 IN6_IFADDR_WUNLOCK();
1309 * Release the reference to the base prefix. There should be a
1310 * positive reference.
1313 if (ia->ia6_ndpr == NULL) {
1315 "in6_unlink_ifa: autoconf'ed address "
1316 "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
1318 ia->ia6_ndpr->ndpr_addrcnt--;
1319 /* Do not delete lles within prefix if refcont != 0 */
1320 if (ia->ia6_ndpr->ndpr_addrcnt == 0)
1322 ia->ia6_ndpr = NULL;
1325 nd6_rem_ifa_lle(ia, remove_lle);
1328 * Also, if the address being removed is autoconf'ed, call
1329 * pfxlist_onlink_check() since the release might affect the status of
1330 * other (detached) addresses.
1332 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1333 pfxlist_onlink_check();
1335 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1339 * Notifies other subsystems about address change/arrival:
1340 * 1) Notifies device handler on the first IPv6 address assignment
1341 * 2) Handle routing table changes for P2P links and route
1342 * 3) Handle routing table changes for address host route
1345 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia,
1346 struct in6_aliasreq *ifra, int hostIsNew)
1348 int error = 0, plen, ifacount = 0;
1350 struct sockaddr_in6 *pdst;
1351 char ip6buf[INET6_ADDRSTRLEN];
1354 * Give the interface a chance to initialize
1355 * if this is its first address,
1357 if (hostIsNew != 0) {
1359 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1360 if (ifa->ifa_addr->sa_family != AF_INET6)
1364 IF_ADDR_RUNLOCK(ifp);
1367 if (ifacount <= 1 && ifp->if_ioctl) {
1368 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1374 * If a new destination address is specified, scrub the old one and
1375 * install the new destination. Note that the interface must be
1378 pdst = &ifra->ifra_dstaddr;
1379 if (pdst->sin6_family == AF_INET6 &&
1380 !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1381 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1382 (rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST) != 0)) {
1383 nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
1384 "remove a route to the old destination: %s\n",
1385 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1386 /* proceed anyway... */
1388 ia->ia_flags &= ~IFA_ROUTE;
1389 ia->ia_dstaddr = *pdst;
1393 * If a new destination address is specified for a point-to-point
1394 * interface, install a route to the destination as an interface
1396 * XXX: the logic below rejects assigning multiple addresses on a p2p
1397 * interface that share the same destination.
1399 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1400 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1401 ia->ia_dstaddr.sin6_family == AF_INET6) {
1402 int rtflags = RTF_UP | RTF_HOST;
1404 * Handle the case for ::1 .
1406 if (ifp->if_flags & IFF_LOOPBACK)
1407 ia->ia_flags |= IFA_RTSELF;
1408 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1411 ia->ia_flags |= IFA_ROUTE;
1415 * add a loopback route to self if not exists
1417 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1418 error = ifa_add_loopback_route((struct ifaddr *)ia,
1419 (struct sockaddr *)&ia->ia_addr);
1421 ia->ia_flags |= IFA_RTSELF;
1428 * Find an IPv6 interface link-local address specific to an interface.
1429 * ifaddr is returned referenced.
1432 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1437 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1438 if (ifa->ifa_addr->sa_family != AF_INET6)
1440 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1441 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1448 IF_ADDR_RUNLOCK(ifp);
1450 return ((struct in6_ifaddr *)ifa);
1455 * find the internet address corresponding to a given address.
1456 * ifaddr is returned referenced.
1459 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid)
1461 struct rm_priotracker in6_ifa_tracker;
1462 struct in6_ifaddr *ia;
1464 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1465 LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
1466 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
1468 zoneid != ia->ia_addr.sin6_scope_id)
1470 ifa_ref(&ia->ia_ifa);
1474 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1479 * find the internet address corresponding to a given interface and address.
1480 * ifaddr is returned referenced.
1483 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr)
1488 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1489 if (ifa->ifa_addr->sa_family != AF_INET6)
1491 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1496 IF_ADDR_RUNLOCK(ifp);
1498 return ((struct in6_ifaddr *)ifa);
1502 * Find a link-local scoped address on ifp and return it if any.
1505 in6ifa_llaonifp(struct ifnet *ifp)
1507 struct sockaddr_in6 *sin6;
1510 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1513 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1514 if (ifa->ifa_addr->sa_family != AF_INET6)
1516 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1517 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1518 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1519 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1522 IF_ADDR_RUNLOCK(ifp);
1524 return ((struct in6_ifaddr *)ifa);
1528 * Convert IP6 address to printable (loggable) representation. Caller
1529 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1531 static char digits[] = "0123456789abcdef";
1533 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1535 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1537 const u_int16_t *a = (const u_int16_t *)addr;
1539 int dcolon = 0, zero = 0;
1543 for (i = 0; i < 8; i++) {
1544 if (*(a + i) == 0) {
1549 else if (maxcnt < cnt) {
1560 for (i = 0; i < 8; i++) {
1571 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1583 d = (const u_char *)a;
1584 /* Try to eliminate leading zeros in printout like in :0001. */
1586 *cp = digits[*d >> 4];
1591 *cp = digits[*d++ & 0xf];
1592 if (zero == 0 || (*cp != '0')) {
1596 *cp = digits[*d >> 4];
1597 if (zero == 0 || (*cp != '0')) {
1601 *cp++ = digits[*d & 0xf];
1610 in6_localaddr(struct in6_addr *in6)
1612 struct rm_priotracker in6_ifa_tracker;
1613 struct in6_ifaddr *ia;
1615 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1618 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1619 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1620 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1621 &ia->ia_prefixmask.sin6_addr)) {
1622 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1626 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1632 * Return 1 if an internet address is for the local host and configured
1633 * on one of its interfaces.
1636 in6_localip(struct in6_addr *in6)
1638 struct rm_priotracker in6_ifa_tracker;
1639 struct in6_ifaddr *ia;
1641 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1642 LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1643 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
1644 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1648 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1653 * Return 1 if an internet address is configured on an interface.
1656 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
1658 struct in6_addr in6;
1660 struct in6_ifaddr *ia6;
1663 if (in6_clearscope(&in6))
1665 in6_setscope(&in6, ifp, NULL);
1668 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1669 if (ifa->ifa_addr->sa_family != AF_INET6)
1671 ia6 = (struct in6_ifaddr *)ifa;
1672 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6)) {
1673 IF_ADDR_RUNLOCK(ifp);
1677 IF_ADDR_RUNLOCK(ifp);
1683 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1685 struct rm_priotracker in6_ifa_tracker;
1686 struct in6_ifaddr *ia;
1688 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1689 LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
1690 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
1691 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
1692 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1693 return (1); /* true */
1698 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1700 return (0); /* false */
1704 * return length of part which dst and src are equal
1708 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1711 u_char *s = (u_char *)src, *d = (u_char *)dst;
1712 u_char *lim = s + 16, r;
1715 if ((r = (*d++ ^ *s++)) != 0) {
1726 /* XXX: to be scope conscious */
1728 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1730 int bytelen, bitlen;
1733 if (0 > len || len > 128) {
1734 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1742 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1745 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1746 p2->s6_addr[bytelen] >> (8 - bitlen))
1753 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1755 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1756 int bytelen, bitlen, i;
1759 if (0 > len || len > 128) {
1760 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1765 bzero(maskp, sizeof(*maskp));
1768 for (i = 0; i < bytelen; i++)
1769 maskp->s6_addr[i] = 0xff;
1771 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1775 * return the best address out of the same scope. if no address was
1776 * found, return the first valid address from designated IF.
1779 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1781 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1783 struct in6_ifaddr *besta = NULL;
1784 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1786 dep[0] = dep[1] = NULL;
1789 * We first look for addresses in the same scope.
1790 * If there is one, return it.
1791 * If two or more, return one which matches the dst longest.
1792 * If none, return one of global addresses assigned other ifs.
1795 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1796 if (ifa->ifa_addr->sa_family != AF_INET6)
1798 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1799 continue; /* XXX: is there any case to allow anycast? */
1800 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1801 continue; /* don't use this interface */
1802 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1804 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1805 if (V_ip6_use_deprecated)
1806 dep[0] = (struct in6_ifaddr *)ifa;
1810 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1812 * call in6_matchlen() as few as possible
1816 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1817 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1820 besta = (struct in6_ifaddr *)ifa;
1823 besta = (struct in6_ifaddr *)ifa;
1827 ifa_ref(&besta->ia_ifa);
1828 IF_ADDR_RUNLOCK(ifp);
1832 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1833 if (ifa->ifa_addr->sa_family != AF_INET6)
1835 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1836 continue; /* XXX: is there any case to allow anycast? */
1837 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1838 continue; /* don't use this interface */
1839 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1841 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1842 if (V_ip6_use_deprecated)
1843 dep[1] = (struct in6_ifaddr *)ifa;
1849 IF_ADDR_RUNLOCK(ifp);
1850 return (struct in6_ifaddr *)ifa;
1853 /* use the last-resort values, that are, deprecated addresses */
1855 ifa_ref((struct ifaddr *)dep[0]);
1856 IF_ADDR_RUNLOCK(ifp);
1860 ifa_ref((struct ifaddr *)dep[1]);
1861 IF_ADDR_RUNLOCK(ifp);
1865 IF_ADDR_RUNLOCK(ifp);
1870 * perform DAD when interface becomes IFF_UP.
1873 in6_if_up(struct ifnet *ifp)
1876 struct in6_ifaddr *ia;
1879 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1880 if (ifa->ifa_addr->sa_family != AF_INET6)
1882 ia = (struct in6_ifaddr *)ifa;
1883 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
1885 * The TENTATIVE flag was likely set by hand
1886 * beforehand, implicitly indicating the need for DAD.
1887 * We may be able to skip the random delay in this
1888 * case, but we impose delays just in case.
1891 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
1894 IF_ADDR_RUNLOCK(ifp);
1897 * special cases, like 6to4, are handled in in6_ifattach
1899 in6_ifattach(ifp, NULL);
1903 in6if_do_dad(struct ifnet *ifp)
1905 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1908 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) ||
1909 (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD))
1913 * Our DAD routine requires the interface up and running.
1914 * However, some interfaces can be up before the RUNNING
1915 * status. Additionally, users may try to assign addresses
1916 * before the interface becomes up (or running).
1917 * This function returns EAGAIN in that case.
1918 * The caller should mark "tentative" on the address instead of
1919 * performing DAD immediately.
1921 if (!((ifp->if_flags & IFF_UP) &&
1922 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
1929 * Calculate max IPv6 MTU through all the interfaces and store it
1935 unsigned long maxmtu = 0;
1938 IFNET_RLOCK_NOSLEEP();
1939 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1940 /* this function can be called during ifnet initialization */
1941 if (!ifp->if_afdata[AF_INET6])
1943 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
1944 IN6_LINKMTU(ifp) > maxmtu)
1945 maxmtu = IN6_LINKMTU(ifp);
1947 IFNET_RUNLOCK_NOSLEEP();
1948 if (maxmtu) /* update only when maxmtu is positive */
1949 V_in6_maxmtu = maxmtu;
1953 * Provide the length of interface identifiers to be used for the link attached
1954 * to the given interface. The length should be defined in "IPv6 over
1955 * xxx-link" document. Note that address architecture might also define
1956 * the length for a particular set of address prefixes, regardless of the
1957 * link type. As clarified in rfc2462bis, those two definitions should be
1958 * consistent, and those really are as of August 2004.
1961 in6_if2idlen(struct ifnet *ifp)
1963 switch (ifp->if_type) {
1964 case IFT_ETHER: /* RFC2464 */
1965 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
1966 case IFT_L2VLAN: /* ditto */
1967 case IFT_BRIDGE: /* bridge(4) only does Ethernet-like links */
1968 case IFT_INFINIBAND:
1970 case IFT_FDDI: /* RFC2467 */
1972 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
1974 case IFT_PPP: /* RFC2472 */
1976 case IFT_ARCNET: /* RFC2497 */
1978 case IFT_FRELAY: /* RFC2590 */
1980 case IFT_IEEE1394: /* RFC3146 */
1983 return (64); /* draft-ietf-v6ops-mech-v2-07 */
1985 return (64); /* XXX: is this really correct? */
1988 * Unknown link type:
1989 * It might be controversial to use the today's common constant
1990 * of 64 for these cases unconditionally. For full compliance,
1991 * we should return an error in this case. On the other hand,
1992 * if we simply miss the standard for the link type or a new
1993 * standard is defined for a new link type, the IFID length
1994 * is very likely to be the common constant. As a compromise,
1995 * we always use the constant, but make an explicit notice
1996 * indicating the "unknown" case.
1998 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2003 #include <sys/sysctl.h>
2005 struct in6_llentry {
2006 struct llentry base;
2009 #define IN6_LLTBL_DEFAULT_HSIZE 32
2010 #define IN6_LLTBL_HASH(k, h) \
2011 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
2014 * Do actual deallocation of @lle.
2017 in6_lltable_destroy_lle_unlocked(struct llentry *lle)
2020 LLE_LOCK_DESTROY(lle);
2021 LLE_REQ_DESTROY(lle);
2022 free(lle, M_LLTABLE);
2026 * Called by LLE_FREE_LOCKED when number of references
2030 in6_lltable_destroy_lle(struct llentry *lle)
2034 in6_lltable_destroy_lle_unlocked(lle);
2037 static struct llentry *
2038 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
2040 struct in6_llentry *lle;
2042 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2043 if (lle == NULL) /* NB: caller generates msg */
2046 lle->base.r_l3addr.addr6 = *addr6;
2047 lle->base.lle_refcnt = 1;
2048 lle->base.lle_free = in6_lltable_destroy_lle;
2049 LLE_LOCK_INIT(&lle->base);
2050 LLE_REQ_INIT(&lle->base);
2051 callout_init(&lle->base.lle_timer, 1);
2053 return (&lle->base);
2057 in6_lltable_match_prefix(const struct sockaddr *saddr,
2058 const struct sockaddr *smask, u_int flags, struct llentry *lle)
2060 const struct in6_addr *addr, *mask, *lle_addr;
2062 addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr;
2063 mask = &((const struct sockaddr_in6 *)smask)->sin6_addr;
2064 lle_addr = &lle->r_l3addr.addr6;
2066 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
2069 if (lle->la_flags & LLE_IFADDR) {
2072 * Delete LLE_IFADDR records IFF address & flag matches.
2073 * Note that addr is the interface address within prefix
2076 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) &&
2077 (flags & LLE_STATIC) != 0)
2082 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2083 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
2090 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2094 LLE_WLOCK_ASSERT(lle);
2095 KASSERT(llt != NULL, ("lltable is NULL"));
2097 /* Unlink entry from table */
2098 if ((lle->la_flags & LLE_LINKED) != 0) {
2101 IF_AFDATA_WLOCK_ASSERT(ifp);
2102 lltable_unlink_entry(llt, lle);
2105 if (callout_stop(&lle->lle_timer) > 0)
2112 in6_lltable_rtcheck(struct ifnet *ifp,
2114 const struct sockaddr *l3addr)
2116 const struct sockaddr_in6 *sin6;
2117 struct nhop6_basic nh6;
2118 struct in6_addr dst;
2121 char ip6buf[INET6_ADDRSTRLEN];
2124 KASSERT(l3addr->sa_family == AF_INET6,
2125 ("sin_family %d", l3addr->sa_family));
2127 sin6 = (const struct sockaddr_in6 *)l3addr;
2128 in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
2129 fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib;
2130 error = fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6);
2131 if (error != 0 || (nh6.nh_flags & NHF_GATEWAY) || nh6.nh_ifp != ifp) {
2134 * Create an ND6 cache for an IPv6 neighbor
2135 * that is not covered by our own prefix.
2137 ifa = ifaof_ifpforaddr(l3addr, ifp);
2142 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2143 ip6_sprintf(ip6buf, &sin6->sin6_addr));
2149 static inline uint32_t
2150 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
2153 return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize));
2157 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
2160 return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
2164 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2166 struct sockaddr_in6 *sin6;
2168 sin6 = (struct sockaddr_in6 *)sa;
2169 bzero(sin6, sizeof(*sin6));
2170 sin6->sin6_family = AF_INET6;
2171 sin6->sin6_len = sizeof(*sin6);
2172 sin6->sin6_addr = lle->r_l3addr.addr6;
2175 static inline struct llentry *
2176 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
2178 struct llentry *lle;
2179 struct llentries *lleh;
2182 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
2183 lleh = &llt->lle_head[hashidx];
2184 LIST_FOREACH(lle, lleh, lle_next) {
2185 if (lle->la_flags & LLE_DELETED)
2187 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
2195 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2198 lle->la_flags |= LLE_DELETED;
2199 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2201 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2206 static struct llentry *
2207 in6_lltable_alloc(struct lltable *llt, u_int flags,
2208 const struct sockaddr *l3addr)
2210 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2211 struct ifnet *ifp = llt->llt_ifp;
2212 struct llentry *lle;
2213 char linkhdr[LLE_MAX_LINKHDR];
2217 KASSERT(l3addr->sa_family == AF_INET6,
2218 ("sin_family %d", l3addr->sa_family));
2221 * A route that covers the given address must have
2222 * been installed 1st because we are doing a resolution,
2225 if (!(flags & LLE_IFADDR) &&
2226 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2229 lle = in6_lltable_new(&sin6->sin6_addr, flags);
2231 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2234 lle->la_flags = flags;
2235 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2236 linkhdrsize = LLE_MAX_LINKHDR;
2237 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp),
2238 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
2239 in6_lltable_destroy_lle_unlocked(lle);
2242 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
2244 lle->la_flags |= LLE_STATIC;
2247 if ((lle->la_flags & LLE_STATIC) != 0)
2248 lle->ln_state = ND6_LLINFO_REACHABLE;
2253 static struct llentry *
2254 in6_lltable_lookup(struct lltable *llt, u_int flags,
2255 const struct sockaddr *l3addr)
2257 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2258 struct llentry *lle;
2260 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2261 KASSERT(l3addr->sa_family == AF_INET6,
2262 ("sin_family %d", l3addr->sa_family));
2264 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2269 KASSERT((flags & (LLE_UNLOCKED|LLE_EXCLUSIVE)) !=
2270 (LLE_UNLOCKED|LLE_EXCLUSIVE),("wrong lle request flags: 0x%X",
2273 if (flags & LLE_UNLOCKED)
2276 if (flags & LLE_EXCLUSIVE)
2284 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2285 struct sysctl_req *wr)
2287 struct ifnet *ifp = llt->llt_ifp;
2290 struct rt_msghdr rtm;
2291 struct sockaddr_in6 sin6;
2293 * ndp.c assumes that sdl is word aligned
2298 struct sockaddr_dl sdl;
2300 struct sockaddr_dl *sdl;
2303 bzero(&ndpc, sizeof(ndpc));
2304 /* skip deleted entries */
2305 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
2307 /* Skip if jailed and not a valid IP of the prison. */
2308 lltable_fill_sa_entry(lle,
2309 (struct sockaddr *)&ndpc.sin6);
2310 if (prison_if(wr->td->td_ucred,
2311 (struct sockaddr *)&ndpc.sin6) != 0)
2314 * produce a msg made of:
2316 * struct sockaddr_in6 (IPv6)
2317 * struct sockaddr_dl;
2319 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2320 ndpc.rtm.rtm_version = RTM_VERSION;
2321 ndpc.rtm.rtm_type = RTM_GET;
2322 ndpc.rtm.rtm_flags = RTF_UP;
2323 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2324 if (V_deembed_scopeid)
2325 sa6_recoverscope(&ndpc.sin6);
2328 if (lle->la_flags & LLE_PUB)
2329 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2332 sdl->sdl_family = AF_LINK;
2333 sdl->sdl_len = sizeof(*sdl);
2334 sdl->sdl_index = ifp->if_index;
2335 sdl->sdl_type = ifp->if_type;
2336 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2337 sdl->sdl_alen = ifp->if_addrlen;
2338 bcopy(lle->ll_addr, LLADDR(sdl),
2342 bzero(LLADDR(sdl), ifp->if_addrlen);
2344 if (lle->la_expire != 0)
2345 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire +
2346 lle->lle_remtime / hz +
2347 time_second - time_uptime;
2348 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2349 if (lle->la_flags & LLE_STATIC)
2350 ndpc.rtm.rtm_flags |= RTF_STATIC;
2351 if (lle->la_flags & LLE_IFADDR)
2352 ndpc.rtm.rtm_flags |= RTF_PINNED;
2353 if (lle->ln_router != 0)
2354 ndpc.rtm.rtm_flags |= RTF_GATEWAY;
2355 ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked;
2356 /* Store state in rmx_weight value */
2357 ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state;
2358 ndpc.rtm.rtm_index = ifp->if_index;
2359 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2364 static struct lltable *
2365 in6_lltattach(struct ifnet *ifp)
2367 struct lltable *llt;
2369 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
2370 llt->llt_af = AF_INET6;
2373 llt->llt_lookup = in6_lltable_lookup;
2374 llt->llt_alloc_entry = in6_lltable_alloc;
2375 llt->llt_delete_entry = in6_lltable_delete_entry;
2376 llt->llt_dump_entry = in6_lltable_dump_entry;
2377 llt->llt_hash = in6_lltable_hash;
2378 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
2379 llt->llt_free_entry = in6_lltable_free_entry;
2380 llt->llt_match_prefix = in6_lltable_match_prefix;
2387 in6_domifattach(struct ifnet *ifp)
2389 struct in6_ifextra *ext;
2391 /* There are not IPv6-capable interfaces. */
2392 switch (ifp->if_type) {
2398 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2399 bzero(ext, sizeof(*ext));
2401 ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2402 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2403 COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2404 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2406 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2407 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2409 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2410 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2412 ext->nd_ifinfo = nd6_ifattach(ifp);
2413 ext->scope6_id = scope6_ifattach(ifp);
2414 ext->lltable = in6_lltattach(ifp);
2416 ext->mld_ifinfo = mld_domifattach(ifp);
2422 in6_domifmtu(struct ifnet *ifp)
2424 if (ifp->if_afdata[AF_INET6] == NULL)
2427 return (IN6_LINKMTU(ifp));
2431 in6_domifdetach(struct ifnet *ifp, void *aux)
2433 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2435 mld_domifdetach(ifp);
2436 scope6_ifdetach(ext->scope6_id);
2437 nd6_ifdetach(ifp, ext->nd_ifinfo);
2438 lltable_free(ext->lltable);
2439 COUNTER_ARRAY_FREE(ext->in6_ifstat,
2440 sizeof(struct in6_ifstat) / sizeof(uint64_t));
2441 free(ext->in6_ifstat, M_IFADDR);
2442 COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2443 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2444 free(ext->icmp6_ifstat, M_IFADDR);
2445 free(ext, M_IFADDR);
2449 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2450 * v4 mapped addr or v4 compat addr
2453 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2456 bzero(sin, sizeof(*sin));
2457 sin->sin_len = sizeof(struct sockaddr_in);
2458 sin->sin_family = AF_INET;
2459 sin->sin_port = sin6->sin6_port;
2460 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2463 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2465 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2467 bzero(sin6, sizeof(*sin6));
2468 sin6->sin6_len = sizeof(struct sockaddr_in6);
2469 sin6->sin6_family = AF_INET6;
2470 sin6->sin6_port = sin->sin_port;
2471 sin6->sin6_addr.s6_addr32[0] = 0;
2472 sin6->sin6_addr.s6_addr32[1] = 0;
2473 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2474 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2477 /* Convert sockaddr_in6 into sockaddr_in. */
2479 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2481 struct sockaddr_in *sin_p;
2482 struct sockaddr_in6 sin6;
2485 * Save original sockaddr_in6 addr and convert it
2488 sin6 = *(struct sockaddr_in6 *)nam;
2489 sin_p = (struct sockaddr_in *)nam;
2490 in6_sin6_2_sin(sin_p, &sin6);
2493 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2495 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2497 struct sockaddr_in *sin_p;
2498 struct sockaddr_in6 *sin6_p;
2500 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2501 sin_p = (struct sockaddr_in *)*nam;
2502 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2503 free(*nam, M_SONAME);
2504 *nam = (struct sockaddr *)sin6_p;