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 * 4. 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>
83 #include <sys/syslog.h>
86 #include <net/if_var.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/if_dl.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <net/if_llatbl.h>
95 #include <netinet/if_ether.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/ip_carp.h>
101 #include <netinet/ip6.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet6/nd6.h>
104 #include <netinet6/mld6_var.h>
105 #include <netinet6/ip6_mroute.h>
106 #include <netinet6/in6_ifattach.h>
107 #include <netinet6/scope6_var.h>
108 #include <netinet6/in6_pcb.h>
110 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
111 #define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix)
114 * Definitions of some costant IP6 addresses.
116 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
117 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
118 const struct in6_addr in6addr_nodelocal_allnodes =
119 IN6ADDR_NODELOCAL_ALLNODES_INIT;
120 const struct in6_addr in6addr_linklocal_allnodes =
121 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
122 const struct in6_addr in6addr_linklocal_allrouters =
123 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
124 const struct in6_addr in6addr_linklocal_allv2routers =
125 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
127 const struct in6_addr in6mask0 = IN6MASK0;
128 const struct in6_addr in6mask32 = IN6MASK32;
129 const struct in6_addr in6mask64 = IN6MASK64;
130 const struct in6_addr in6mask96 = IN6MASK96;
131 const struct in6_addr in6mask128 = IN6MASK128;
133 const struct sockaddr_in6 sa6_any =
134 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
136 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
137 struct sockaddr_in6 *, int);
138 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
140 int (*faithprefix_p)(struct in6_addr *);
142 static int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *,
143 struct in6_ifaddr *, int);
144 static struct in6_ifaddr *in6_alloc_ifa(struct ifnet *,
145 struct in6_aliasreq *, int flags);
146 static int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *,
147 struct in6_ifaddr *, int, int);
148 static int in6_setup_ifa(struct ifnet *, struct in6_aliasreq *,
149 struct in6_ifaddr *, int);
151 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
152 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
155 in6_ifaddloop(struct ifaddr *ifa)
157 struct sockaddr_dl gateway;
158 struct sockaddr_in6 mask, addr;
160 struct in6_ifaddr *ia;
167 ifa->ifa_rtrequest = nd6_rtrequest;
168 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
169 LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
170 IF_AFDATA_UNLOCK(ifp);
172 ln->la_expire = 0; /* for IPv6 this means permanent */
173 ln->ln_state = ND6_LLINFO_REACHABLE;
175 * initialize for rtmsg generation
177 bzero(&gateway, sizeof(gateway));
178 gateway.sdl_len = sizeof(gateway);
179 gateway.sdl_family = AF_LINK;
180 gateway.sdl_nlen = 0;
181 gateway.sdl_alen = 6;
182 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned,
183 sizeof(ln->ll_addr));
187 bzero(&rt, sizeof(rt));
188 rt.rt_gateway = (struct sockaddr *)&gateway;
189 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
190 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
191 rt_mask(&rt) = (struct sockaddr *)&mask;
192 rt_key(&rt) = (struct sockaddr *)&addr;
193 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
194 /* Announce arrival of local address to all FIBs. */
195 rt_newaddrmsg(RTM_ADD, ifa, 0, &rt);
199 in6_ifremloop(struct ifaddr *ifa)
201 struct sockaddr_dl gateway;
202 struct sockaddr_in6 mask, addr;
204 struct in6_ifaddr *ia;
209 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
210 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
211 lltable_prefix_free(AF_INET6, (struct sockaddr *)&addr,
212 (struct sockaddr *)&mask, LLE_STATIC);
215 * initialize for rtmsg generation
217 bzero(&gateway, sizeof(gateway));
218 gateway.sdl_len = sizeof(gateway);
219 gateway.sdl_family = AF_LINK;
220 gateway.sdl_nlen = 0;
221 gateway.sdl_alen = ifp->if_addrlen;
222 bzero(&rt0, sizeof(rt0));
223 rt0.rt_gateway = (struct sockaddr *)&gateway;
224 rt_mask(&rt0) = (struct sockaddr *)&mask;
225 rt_key(&rt0) = (struct sockaddr *)&addr;
226 rt0.rt_flags = RTF_HOST | RTF_STATIC;
227 /* Announce removal of local address to all FIBs. */
228 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
232 in6_mask2len(struct in6_addr *mask, u_char *lim0)
235 u_char *lim = lim0, *p;
237 /* ignore the scope_id part */
238 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
239 lim = (u_char *)mask + sizeof(*mask);
240 for (p = (u_char *)mask; p < lim; x++, p++) {
246 for (y = 0; y < 8; y++) {
247 if ((*p & (0x80 >> y)) == 0)
253 * when the limit pointer is given, do a stricter check on the
257 if (y != 0 && (*p & (0x00ff >> y)) != 0)
259 for (p = p + 1; p < lim; p++)
267 #ifdef COMPAT_FREEBSD32
268 struct in6_ndifreq32 {
269 char ifname[IFNAMSIZ];
272 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
276 in6_control(struct socket *so, u_long cmd, caddr_t data,
277 struct ifnet *ifp, struct thread *td)
279 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
280 struct in6_ifaddr *ia = NULL;
281 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
282 struct sockaddr_in6 *sa6;
283 int carp_attached = 0;
288 * Compat to make pre-10.x ifconfig(8) operable.
290 if (cmd == OSIOCAIFADDR_IN6)
291 cmd = SIOCAIFADDR_IN6;
294 case SIOCGETSGCNT_IN6:
295 case SIOCGETMIFCNT_IN6:
297 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
298 * We cannot see how that would be needed, so do not adjust the
299 * KPI blindly; more likely should clean up the IPv4 variant.
301 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
305 case SIOCAADDRCTL_POLICY:
306 case SIOCDADDRCTL_POLICY:
308 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
312 return (in6_src_ioctl(cmd, data));
319 case SIOCSNDFLUSH_IN6:
320 case SIOCSPFXFLUSH_IN6:
321 case SIOCSRTRFLUSH_IN6:
322 case SIOCSDEFIFACE_IN6:
323 case SIOCSIFINFO_FLAGS:
324 case SIOCSIFINFO_IN6:
326 error = priv_check(td, PRIV_NETINET_ND6);
331 case OSIOCGIFINFO_IN6:
332 case SIOCGIFINFO_IN6:
335 case SIOCGNBRINFO_IN6:
336 case SIOCGDEFIFACE_IN6:
337 return (nd6_ioctl(cmd, data, ifp));
339 #ifdef COMPAT_FREEBSD32
340 case SIOCGDEFIFACE32_IN6:
342 struct in6_ndifreq ndif;
343 struct in6_ndifreq32 *ndif32;
345 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
349 ndif32 = (struct in6_ndifreq32 *)data;
350 ndif32->ifindex = ndif.ifindex;
357 case SIOCSIFPREFIX_IN6:
358 case SIOCDIFPREFIX_IN6:
359 case SIOCAIFPREFIX_IN6:
360 case SIOCCIFPREFIX_IN6:
361 case SIOCSGIFPREFIX_IN6:
362 case SIOCGIFPREFIX_IN6:
364 "prefix ioctls are now invalidated. "
365 "please use ifconfig.\n");
372 error = priv_check(td, PRIV_NETINET_SCOPE6);
376 return (scope6_set(ifp,
377 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
379 return (scope6_get(ifp,
380 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
382 return (scope6_get_default((struct scope6_id *)
383 ifr->ifr_ifru.ifru_scope_id));
387 * Find address for this interface, if it exists.
389 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
390 * only, and used the first interface address as the target of other
391 * operations (without checking ifra_addr). This was because netinet
392 * code/API assumed at most 1 interface address per interface.
393 * Since IPv6 allows a node to assign multiple addresses
394 * on a single interface, we almost always look and check the
395 * presence of ifra_addr, and reject invalid ones here.
396 * It also decreases duplicated code among SIOC*_IN6 operations.
399 case SIOCAIFADDR_IN6:
400 case SIOCSIFPHYADDR_IN6:
401 sa6 = &ifra->ifra_addr;
403 case SIOCSIFADDR_IN6:
404 case SIOCGIFADDR_IN6:
405 case SIOCSIFDSTADDR_IN6:
406 case SIOCSIFNETMASK_IN6:
407 case SIOCGIFDSTADDR_IN6:
408 case SIOCGIFNETMASK_IN6:
409 case SIOCDIFADDR_IN6:
410 case SIOCGIFPSRCADDR_IN6:
411 case SIOCGIFPDSTADDR_IN6:
412 case SIOCGIFAFLAG_IN6:
413 case SIOCSNDFLUSH_IN6:
414 case SIOCSPFXFLUSH_IN6:
415 case SIOCSRTRFLUSH_IN6:
416 case SIOCGIFALIFETIME_IN6:
417 case SIOCSIFALIFETIME_IN6:
418 case SIOCGIFSTAT_IN6:
419 case SIOCGIFSTAT_ICMP6:
420 sa6 = &ifr->ifr_addr;
427 * Although we should pass any non-INET6 ioctl requests
428 * down to driver, we filter some legacy INET requests.
429 * Drivers trust SIOCSIFADDR et al to come from an already
430 * privileged layer, and do not perform any credentials
431 * checks or input validation.
438 if (sa6 && sa6->sin6_family == AF_INET6) {
439 if (sa6->sin6_scope_id != 0)
440 error = sa6_embedscope(sa6, 0);
442 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
445 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
446 &sa6->sin6_addr)) != 0)
448 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
453 case SIOCSIFADDR_IN6:
454 case SIOCSIFDSTADDR_IN6:
455 case SIOCSIFNETMASK_IN6:
457 * Since IPv6 allows a node to assign multiple addresses
458 * on a single interface, SIOCSIFxxx ioctls are deprecated.
460 /* we decided to obsolete this command (20000704) */
464 case SIOCDIFADDR_IN6:
466 * for IPv4, we look for existing in_ifaddr here to allow
467 * "ifconfig if0 delete" to remove the first IPv4 address on
468 * the interface. For IPv6, as the spec allows multiple
469 * interface address from the day one, we consider "remove the
470 * first one" semantics to be not preferable.
473 error = EADDRNOTAVAIL;
477 case SIOCAIFADDR_IN6:
479 * We always require users to specify a valid IPv6 address for
480 * the corresponding operation.
482 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
483 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
484 error = EAFNOSUPPORT;
489 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
490 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
496 case SIOCGIFADDR_IN6:
497 /* This interface is basically deprecated. use SIOCGIFCONF. */
499 case SIOCGIFAFLAG_IN6:
500 case SIOCGIFNETMASK_IN6:
501 case SIOCGIFDSTADDR_IN6:
502 case SIOCGIFALIFETIME_IN6:
503 /* must think again about its semantics */
505 error = EADDRNOTAVAIL;
510 case SIOCSIFALIFETIME_IN6:
512 struct in6_addrlifetime *lt;
515 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
520 error = EADDRNOTAVAIL;
523 /* sanity for overflow - beware unsigned */
524 lt = &ifr->ifr_ifru.ifru_lifetime;
525 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
526 lt->ia6t_vltime + time_uptime < time_uptime) {
530 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
531 lt->ia6t_pltime + time_uptime < time_uptime) {
540 case SIOCGIFADDR_IN6:
541 ifr->ifr_addr = ia->ia_addr;
542 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
546 case SIOCGIFDSTADDR_IN6:
547 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
552 * XXX: should we check if ifa_dstaddr is NULL and return
555 ifr->ifr_dstaddr = ia->ia_dstaddr;
556 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
560 case SIOCGIFNETMASK_IN6:
561 ifr->ifr_addr = ia->ia_prefixmask;
564 case SIOCGIFAFLAG_IN6:
565 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
568 case SIOCGIFSTAT_IN6:
573 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
574 ifp->if_afdata[AF_INET6])->in6_ifstat,
575 &ifr->ifr_ifru.ifru_stat,
576 sizeof(struct in6_ifstat) / sizeof(uint64_t));
579 case SIOCGIFSTAT_ICMP6:
584 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
585 ifp->if_afdata[AF_INET6])->icmp6_ifstat,
586 &ifr->ifr_ifru.ifru_icmp6stat,
587 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
590 case SIOCGIFALIFETIME_IN6:
591 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
592 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
594 struct in6_addrlifetime *retlt =
595 &ifr->ifr_ifru.ifru_lifetime;
598 * XXX: adjust expiration time assuming time_t is
602 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
603 if (ia->ia6_lifetime.ia6t_vltime <
604 maxexpire - ia->ia6_updatetime) {
605 retlt->ia6t_expire = ia->ia6_updatetime +
606 ia->ia6_lifetime.ia6t_vltime;
608 retlt->ia6t_expire = maxexpire;
610 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
612 struct in6_addrlifetime *retlt =
613 &ifr->ifr_ifru.ifru_lifetime;
616 * XXX: adjust expiration time assuming time_t is
620 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
621 if (ia->ia6_lifetime.ia6t_pltime <
622 maxexpire - ia->ia6_updatetime) {
623 retlt->ia6t_preferred = ia->ia6_updatetime +
624 ia->ia6_lifetime.ia6t_pltime;
626 retlt->ia6t_preferred = maxexpire;
630 case SIOCSIFALIFETIME_IN6:
631 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
633 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
634 ia->ia6_lifetime.ia6t_expire =
635 time_uptime + ia->ia6_lifetime.ia6t_vltime;
637 ia->ia6_lifetime.ia6t_expire = 0;
638 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
639 ia->ia6_lifetime.ia6t_preferred =
640 time_uptime + ia->ia6_lifetime.ia6t_pltime;
642 ia->ia6_lifetime.ia6t_preferred = 0;
645 case SIOCAIFADDR_IN6:
647 struct nd_prefixctl pr0;
648 struct nd_prefix *pr;
651 * first, make or update the interface address structure,
652 * and link it to the list.
654 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
657 ifa_free(&ia->ia_ifa);
658 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
661 * this can happen when the user specify the 0 valid
667 if (cmd == ocmd && ifra->ifra_vhid > 0) {
668 if (carp_attach_p != NULL)
669 error = (*carp_attach_p)(&ia->ia_ifa,
672 error = EPROTONOSUPPORT;
680 * then, make the prefix on-link on the interface.
681 * XXX: we'd rather create the prefix before the address, but
682 * we need at least one address to install the corresponding
683 * interface route, so we configure the address first.
687 * convert mask to prefix length (prefixmask has already
688 * been validated in in6_update_ifa().
690 bzero(&pr0, sizeof(pr0));
692 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
694 if (pr0.ndpr_plen == 128) {
695 break; /* we don't need to install a host route. */
697 pr0.ndpr_prefix = ifra->ifra_addr;
698 /* apply the mask for safety. */
699 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr,
700 &ifra->ifra_prefixmask.sin6_addr);
703 * XXX: since we don't have an API to set prefix (not address)
704 * lifetimes, we just use the same lifetimes as addresses.
705 * The (temporarily) installed lifetimes can be overridden by
706 * later advertised RAs (when accept_rtadv is non 0), which is
707 * an intended behavior.
709 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
711 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
712 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
713 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
715 /* add the prefix if not yet. */
716 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
718 * nd6_prelist_add will install the corresponding
721 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
723 (*carp_detach_p)(&ia->ia_ifa);
728 (*carp_detach_p)(&ia->ia_ifa);
729 log(LOG_ERR, "nd6_prelist_add succeeded but "
736 /* relate the address to the prefix */
737 if (ia->ia6_ndpr == NULL) {
742 * If this is the first autoconf address from the
743 * prefix, create a temporary address as well
746 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
747 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
749 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
750 log(LOG_NOTICE, "in6_control: failed "
751 "to create a temporary address, "
758 * this might affect the status of autoconfigured addresses,
759 * that is, this address might make other addresses detached.
761 pfxlist_onlink_check();
762 if (error == 0 && ia) {
763 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
765 * Try to clear the flag when a new
766 * IPv6 address is added onto an
767 * IFDISABLED interface and it
770 struct in6_ndireq nd;
772 memset(&nd, 0, sizeof(nd));
773 nd.ndi.flags = ND_IFINFO(ifp)->flags;
774 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
775 if (nd6_ioctl(SIOCSIFINFO_FLAGS,
776 (caddr_t)&nd, ifp) < 0)
777 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
778 "SIOCSIFINFO_FLAGS for -ifdisabled "
781 * Ignore failure of clearing the flag
782 * intentionally. The failure means
783 * address duplication was detected.
786 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
791 case SIOCDIFADDR_IN6:
793 struct nd_prefix *pr;
796 * If the address being deleted is the only one that owns
797 * the corresponding prefix, expire the prefix as well.
798 * XXX: theoretically, we don't have to worry about such
799 * relationship, since we separate the address management
800 * and the prefix management. We do this, however, to provide
801 * as much backward compatibility as possible in terms of
802 * the ioctl operation.
803 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
806 in6_purgeaddr(&ia->ia_ifa);
807 if (pr && pr->ndpr_refcnt == 0)
809 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
814 if (ifp == NULL || ifp->if_ioctl == 0) {
818 error = (*ifp->if_ioctl)(ifp, cmd, data);
825 ifa_free(&ia->ia_ifa);
831 * Join necessary multicast groups. Factored out from in6_update_ifa().
832 * This entire work should only be done once, for the default FIB.
835 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
836 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
838 char ip6buf[INET6_ADDRSTRLEN];
839 struct sockaddr_in6 mltaddr, mltmask;
840 struct in6_addr llsol;
841 struct in6_multi_mship *imm;
845 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
847 /* Join solicited multicast addr for new host id. */
848 bzero(&llsol, sizeof(struct in6_addr));
849 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
850 llsol.s6_addr32[1] = 0;
851 llsol.s6_addr32[2] = htonl(1);
852 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
853 llsol.s6_addr8[12] = 0xff;
854 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
855 /* XXX: should not happen */
856 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
860 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
862 * We need a random delay for DAD on the address being
863 * configured. It also means delaying transmission of the
864 * corresponding MLD report to avoid report collision.
865 * [RFC 4861, Section 6.3.7]
867 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
869 imm = in6_joingroup(ifp, &llsol, &error, delay);
871 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
872 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &llsol),
873 if_name(ifp), error));
876 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
877 *in6m_sol = imm->i6mm_maddr;
879 bzero(&mltmask, sizeof(mltmask));
880 mltmask.sin6_len = sizeof(struct sockaddr_in6);
881 mltmask.sin6_family = AF_INET6;
882 mltmask.sin6_addr = in6mask32;
883 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
886 * Join link-local all-nodes address.
888 bzero(&mltaddr, sizeof(mltaddr));
889 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
890 mltaddr.sin6_family = AF_INET6;
891 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
892 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
893 goto cleanup; /* XXX: should not fail */
896 * XXX: do we really need this automatic routes? We should probably
897 * reconsider this stuff. Most applications actually do not need the
898 * routes, since they usually specify the outgoing interface.
900 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
902 /* XXX: only works in !SCOPEDROUTING case. */
903 if (memcmp(&mltaddr.sin6_addr,
904 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
911 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
912 (struct sockaddr *)&ia->ia_addr,
913 (struct sockaddr *)&mltmask, RTF_UP,
914 (struct rtentry **)0, RT_DEFAULT_FIB);
920 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
922 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
923 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
924 &mltaddr.sin6_addr), if_name(ifp), error));
927 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
930 * Join node information group address.
933 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
935 * The spec does not say anything about delay for this group,
936 * but the same logic should apply.
938 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
940 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
942 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
944 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
945 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
946 &mltaddr.sin6_addr), if_name(ifp), error));
947 /* XXX not very fatal, go on... */
949 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
951 if (V_icmp6_nodeinfo_oldmcprefix &&
952 in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
953 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
955 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
956 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
957 &mltaddr.sin6_addr), if_name(ifp), error));
958 /* XXX not very fatal, go on... */
960 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
964 * Join interface-local all-nodes address.
965 * (ff01::1%ifN, and ff01::%ifN/32)
967 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
968 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
969 goto cleanup; /* XXX: should not fail */
970 /* XXX: again, do we really need the route? */
971 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
973 if (memcmp(&mltaddr.sin6_addr,
974 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
981 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
982 (struct sockaddr *)&ia->ia_addr,
983 (struct sockaddr *)&mltmask, RTF_UP,
984 (struct rtentry **)0, RT_DEFAULT_FIB);
990 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
992 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
993 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
994 &mltaddr.sin6_addr), if_name(ifp), error));
997 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1005 * Update parameters of an IPv6 interface address.
1006 * If necessary, a new entry is created and linked into address chains.
1007 * This function is separated from in6_control().
1010 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1011 struct in6_ifaddr *ia, int flags)
1013 int error, hostIsNew = 0;
1015 if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0)
1020 if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL)
1024 error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags);
1026 if (hostIsNew != 0) {
1027 in6_unlink_ifa(ia, ifp);
1028 ifa_free(&ia->ia_ifa);
1034 error = in6_setup_ifa(ifp, ifra, ia, flags);
1040 in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra,
1041 struct in6_ifaddr *ia, int flags)
1044 struct sockaddr_in6 dst6;
1045 struct in6_addrlifetime *lt;
1046 char ip6buf[INET6_ADDRSTRLEN];
1048 /* Validate parameters */
1049 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
1053 * The destination address for a p2p link must have a family
1054 * of AF_UNSPEC or AF_INET6.
1056 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1057 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
1058 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
1059 return (EAFNOSUPPORT);
1061 * validate ifra_prefixmask. don't check sin6_family, netmask
1062 * does not carry fields other than sin6_len.
1064 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
1067 * Because the IPv6 address architecture is classless, we require
1068 * users to specify a (non 0) prefix length (mask) for a new address.
1069 * We also require the prefix (when specified) mask is valid, and thus
1070 * reject a non-consecutive mask.
1072 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
1074 if (ifra->ifra_prefixmask.sin6_len != 0) {
1075 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
1076 (u_char *)&ifra->ifra_prefixmask +
1077 ifra->ifra_prefixmask.sin6_len);
1082 * In this case, ia must not be NULL. We just use its prefix
1085 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1088 * If the destination address on a p2p interface is specified,
1089 * and the address is a scoped one, validate/set the scope
1092 dst6 = ifra->ifra_dstaddr;
1093 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
1094 (dst6.sin6_family == AF_INET6)) {
1095 struct in6_addr in6_tmp;
1098 in6_tmp = dst6.sin6_addr;
1099 if (in6_setscope(&in6_tmp, ifp, &zoneid))
1100 return (EINVAL); /* XXX: should be impossible */
1102 if (dst6.sin6_scope_id != 0) {
1103 if (dst6.sin6_scope_id != zoneid)
1105 } else /* user omit to specify the ID. */
1106 dst6.sin6_scope_id = zoneid;
1108 /* convert into the internal form */
1109 if (sa6_embedscope(&dst6, 0))
1110 return (EINVAL); /* XXX: should be impossible */
1112 /* Modify original ifra_dstaddr to reflect changes */
1113 ifra->ifra_dstaddr = dst6;
1116 * The destination address can be specified only for a p2p or a
1117 * loopback interface. If specified, the corresponding prefix length
1120 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
1121 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1122 /* XXX: noisy message */
1123 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1124 "be specified for a p2p or a loopback IF only\n"));
1128 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1129 "be 128 when dstaddr is specified\n"));
1133 /* lifetime consistency check */
1134 lt = &ifra->ifra_lifetime;
1135 if (lt->ia6t_pltime > lt->ia6t_vltime)
1137 if (lt->ia6t_vltime == 0) {
1139 * the following log might be noisy, but this is a typical
1140 * configuration mistake or a tool's bug.
1143 "in6_update_ifa: valid lifetime is 0 for %s\n",
1144 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1147 return (0); /* there's nothing to do */
1150 /* Check prefix mask */
1151 if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) {
1153 * We prohibit changing the prefix length of an existing
1155 * + such an operation should be rare in IPv6, and
1156 * + the operation would confuse prefix management.
1158 if (ia->ia_prefixmask.sin6_len != 0 &&
1159 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1160 nd6log((LOG_INFO, "in6_validate_ifa: the prefix length "
1161 "of an existing %s address should not be changed\n",
1162 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1172 * If this is a new address, allocate a new ifaddr and link it
1175 static struct in6_ifaddr *
1176 in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
1178 struct in6_ifaddr *ia;
1181 * When in6_update_ifa() is called in a process of a received
1182 * RA, it is called under an interrupt context. So, we should
1183 * call malloc with M_NOWAIT.
1185 ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT);
1188 LIST_INIT(&ia->ia6_memberships);
1189 /* Initialize the address and masks, and put time stamp */
1190 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1191 ia->ia_addr.sin6_family = AF_INET6;
1192 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1193 ia->ia6_createtime = time_uptime;
1194 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1196 * XXX: some functions expect that ifa_dstaddr is not
1197 * NULL for p2p interfaces.
1199 ia->ia_ifa.ifa_dstaddr =
1200 (struct sockaddr *)&ia->ia_dstaddr;
1202 ia->ia_ifa.ifa_dstaddr = NULL;
1204 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1206 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1208 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1209 IF_ADDR_WUNLOCK(ifp);
1211 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1213 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1214 LIST_INSERT_HEAD(IN6ADDR_HASH(&ifra->ifra_addr.sin6_addr),
1216 IN6_IFADDR_WUNLOCK();
1222 in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra,
1223 struct in6_ifaddr *ia, int hostIsNew, int flags)
1225 struct sockaddr_in6 *pdst;
1227 char ip6buf[INET6_ADDRSTRLEN];
1229 /* update timestamp */
1230 ia->ia6_updatetime = time_uptime;
1232 /* set prefix mask */
1233 if (ifra->ifra_prefixmask.sin6_len != 0) {
1234 ia->ia_prefixmask = ifra->ifra_prefixmask;
1235 ia->ia_prefixmask.sin6_family = AF_INET6;
1239 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1240 * to see if the address is deprecated or invalidated, but initialize
1241 * these members for applications.
1243 ia->ia6_lifetime = ifra->ifra_lifetime;
1244 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1245 ia->ia6_lifetime.ia6t_expire =
1246 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1248 ia->ia6_lifetime.ia6t_expire = 0;
1249 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1250 ia->ia6_lifetime.ia6t_preferred =
1251 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1253 ia->ia6_lifetime.ia6t_preferred = 0;
1256 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1257 * userland, make it deprecated.
1259 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1260 ia->ia6_lifetime.ia6t_pltime = 0;
1261 ia->ia6_lifetime.ia6t_preferred = time_uptime;
1265 * configure address flags.
1267 ia->ia6_flags = ifra->ifra_flags;
1270 * Make the address tentative before joining multicast addresses,
1271 * so that corresponding MLD responses would not have a tentative
1274 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1275 if (hostIsNew && in6if_do_dad(ifp))
1276 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1278 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
1279 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1280 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1283 * If a new destination address is specified, scrub the old one and
1284 * install the new destination. Note that the interface must be
1285 * p2p or loopback (see the check above.)
1287 pdst = &ifra->ifra_dstaddr;
1288 if (pdst->sin6_family == AF_INET6 &&
1289 !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1290 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1291 (rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST) != 0)) {
1292 nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
1293 "remove a route to the old destination: %s\n",
1294 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1295 /* proceed anyway... */
1297 ia->ia_flags &= ~IFA_ROUTE;
1298 ia->ia_dstaddr = *pdst;
1301 /* reset the interface and routing table appropriately. */
1302 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) {
1304 * XXX: if a change of an existing address failed, keep the entry
1313 in6_setup_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1314 struct in6_ifaddr *ia, int flags)
1316 struct in6_multi *in6m_sol;
1319 /* Add local address to lltable, if necessary (ex. on p2p link). */
1320 in6_ifaddloop(&(ia->ia_ifa));
1323 * Beyond this point, we should call in6_purgeaddr upon an error,
1324 * not just go to unlink.
1327 /* Join necessary multicast groups. */
1329 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1330 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1332 ifa_free(&ia->ia_ifa);
1333 in6_purgeaddr(&ia->ia_ifa);
1339 * Perform DAD, if needed.
1340 * XXX It may be of use, if we can administratively disable DAD.
1342 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1343 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1345 int delay, mindelay, maxdelay;
1348 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1350 * We need to impose a delay before sending an NS
1351 * for DAD. Check if we also needed a delay for the
1352 * corresponding MLD message. If we did, the delay
1353 * should be larger than the MLD delay (this could be
1354 * relaxed a bit, but this simple logic is at least
1356 * XXX: Break data hiding guidelines and look at
1357 * state for the solicited multicast group.
1360 if (in6m_sol != NULL &&
1361 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1362 mindelay = in6m_sol->in6m_timer;
1364 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1365 if (maxdelay - mindelay == 0)
1369 (arc4random() % (maxdelay - mindelay)) +
1373 nd6_dad_start((struct ifaddr *)ia, delay);
1376 ifa_free(&ia->ia_ifa);
1381 * Leave multicast groups. Factored out from in6_purgeaddr().
1382 * This entire work should only be done once, for the default FIB.
1385 in6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0)
1387 struct sockaddr_in6 mltaddr, mltmask;
1388 struct in6_multi_mship *imm;
1390 struct sockaddr_in6 sin6;
1394 * Leave from multicast groups we have joined for the interface.
1396 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1397 LIST_REMOVE(imm, i6mm_chain);
1398 in6_leavegroup(imm);
1402 * Remove the link-local all-nodes address.
1404 bzero(&mltmask, sizeof(mltmask));
1405 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1406 mltmask.sin6_family = AF_INET6;
1407 mltmask.sin6_addr = in6mask32;
1409 bzero(&mltaddr, sizeof(mltaddr));
1410 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1411 mltaddr.sin6_family = AF_INET6;
1412 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1414 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1418 * As for the mltaddr above, proactively prepare the sin6 to avoid
1419 * rtentry un- and re-locking.
1422 bzero(&sin6, sizeof(sin6));
1423 sin6.sin6_len = sizeof(sin6);
1424 sin6.sin6_family = AF_INET6;
1425 memcpy(&sin6.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1426 sizeof(sin6.sin6_addr));
1427 error = in6_setscope(&sin6.sin6_addr, ifa0->ifa_ifp, NULL);
1432 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1433 if (rt != NULL && rt->rt_gateway != NULL &&
1434 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1435 &ia->ia_addr.sin6_addr,
1436 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1438 * If no more IPv6 address exists on this interface then
1439 * remove the multicast address route.
1442 memcpy(&mltaddr.sin6_addr,
1443 &satosin6(rt_key(rt))->sin6_addr,
1444 sizeof(mltaddr.sin6_addr));
1446 error = in6_rtrequest(RTM_DELETE,
1447 (struct sockaddr *)&mltaddr,
1448 (struct sockaddr *)&ia->ia_addr,
1449 (struct sockaddr *)&mltmask, RTF_UP,
1450 (struct rtentry **)0, RT_DEFAULT_FIB);
1452 log(LOG_INFO, "%s: link-local all-nodes "
1453 "multicast address deletion error\n",
1457 * Replace the gateway of the route.
1459 memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1468 * Remove the node-local all-nodes address.
1470 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1471 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1474 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1475 if (rt != NULL && rt->rt_gateway != NULL &&
1476 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1477 &ia->ia_addr.sin6_addr,
1478 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1480 * If no more IPv6 address exists on this interface then
1481 * remove the multicast address route.
1484 memcpy(&mltaddr.sin6_addr,
1485 &satosin6(rt_key(rt))->sin6_addr,
1486 sizeof(mltaddr.sin6_addr));
1489 error = in6_rtrequest(RTM_DELETE,
1490 (struct sockaddr *)&mltaddr,
1491 (struct sockaddr *)&ia->ia_addr,
1492 (struct sockaddr *)&mltmask, RTF_UP,
1493 (struct rtentry **)0, RT_DEFAULT_FIB);
1495 log(LOG_INFO, "%s: node-local all-nodes"
1496 "multicast address deletion error\n",
1500 * Replace the gateway of the route.
1502 memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1514 in6_purgeaddr(struct ifaddr *ifa)
1516 struct ifnet *ifp = ifa->ifa_ifp;
1517 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1519 struct ifaddr *ifa0;
1522 (*carp_detach_p)(ifa);
1525 * find another IPv6 address as the gateway for the
1526 * link-local and node-local all-nodes multicast
1530 TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1531 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1532 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1533 &ia->ia_addr.sin6_addr, sizeof(struct in6_addr)) == 0)
1540 IF_ADDR_RUNLOCK(ifp);
1543 * Remove the loopback route to the interface address.
1544 * The check for the current setting of "nd6_useloopback"
1547 if (ia->ia_flags & IFA_RTSELF) {
1548 error = ifa_del_loopback_route((struct ifaddr *)ia,
1549 (struct sockaddr *)&ia->ia_addr);
1551 ia->ia_flags &= ~IFA_RTSELF;
1554 /* stop DAD processing */
1557 /* Remove local address entry from lltable. */
1560 /* Leave multicast groups. */
1561 error = in6_purgeaddr_mc(ifp, ia, ifa0);
1566 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1567 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1568 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1569 (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0);
1571 log(LOG_INFO, "%s: err=%d, destination address delete "
1572 "failed\n", __func__, error);
1573 ia->ia_flags &= ~IFA_ROUTE;
1576 in6_unlink_ifa(ia, ifp);
1580 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1584 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1585 IF_ADDR_WUNLOCK(ifp);
1586 ifa_free(&ia->ia_ifa); /* if_addrhead */
1589 * Defer the release of what might be the last reference to the
1590 * in6_ifaddr so that it can't be freed before the remainder of the
1594 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1595 LIST_REMOVE(ia, ia6_hash);
1596 IN6_IFADDR_WUNLOCK();
1599 * Release the reference to the base prefix. There should be a
1600 * positive reference.
1602 if (ia->ia6_ndpr == NULL) {
1604 "in6_unlink_ifa: autoconf'ed address "
1605 "%p has no prefix\n", ia));
1607 ia->ia6_ndpr->ndpr_refcnt--;
1608 ia->ia6_ndpr = NULL;
1612 * Also, if the address being removed is autoconf'ed, call
1613 * pfxlist_onlink_check() since the release might affect the status of
1614 * other (detached) addresses.
1616 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1617 pfxlist_onlink_check();
1619 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1623 in6_purgeif(struct ifnet *ifp)
1625 struct ifaddr *ifa, *nifa;
1627 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1628 if (ifa->ifa_addr->sa_family != AF_INET6)
1637 * Initialize an interface's IPv6 address and routing table entry.
1640 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1641 struct sockaddr_in6 *sin6, int newhost)
1643 int error = 0, plen, ifacount = 0;
1647 * Give the interface a chance to initialize
1648 * if this is its first address,
1649 * and to validate the address if necessary.
1652 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1653 if (ifa->ifa_addr->sa_family != AF_INET6)
1657 IF_ADDR_RUNLOCK(ifp);
1659 ia->ia_addr = *sin6;
1661 if (ifacount <= 1 && ifp->if_ioctl) {
1662 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1667 ia->ia_ifa.ifa_metric = ifp->if_metric;
1669 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1673 * If a new destination address is specified for a point-to-point
1674 * interface, install a route to the destination as an interface
1676 * XXX: the logic below rejects assigning multiple addresses on a p2p
1677 * interface that share the same destination.
1679 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1680 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1681 ia->ia_dstaddr.sin6_family == AF_INET6) {
1682 int rtflags = RTF_UP | RTF_HOST;
1683 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1686 ia->ia_flags |= IFA_ROUTE;
1688 * Handle the case for ::1 .
1690 if (ifp->if_flags & IFF_LOOPBACK)
1691 ia->ia_flags |= IFA_RTSELF;
1695 * add a loopback route to self
1697 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1698 error = ifa_add_loopback_route((struct ifaddr *)ia,
1699 (struct sockaddr *)&ia->ia_addr);
1701 ia->ia_flags |= IFA_RTSELF;
1708 * Find an IPv6 interface link-local address specific to an interface.
1709 * ifaddr is returned referenced.
1712 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1717 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1718 if (ifa->ifa_addr->sa_family != AF_INET6)
1720 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1721 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1728 IF_ADDR_RUNLOCK(ifp);
1730 return ((struct in6_ifaddr *)ifa);
1735 * find the internet address corresponding to a given interface and address.
1736 * ifaddr is returned referenced.
1739 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1744 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1745 if (ifa->ifa_addr->sa_family != AF_INET6)
1747 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1752 IF_ADDR_RUNLOCK(ifp);
1754 return ((struct in6_ifaddr *)ifa);
1758 * Find a link-local scoped address on ifp and return it if any.
1761 in6ifa_llaonifp(struct ifnet *ifp)
1763 struct sockaddr_in6 *sin6;
1766 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1769 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1770 if (ifa->ifa_addr->sa_family != AF_INET6)
1772 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1773 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1774 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1775 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1778 if_addr_runlock(ifp);
1780 return ((struct in6_ifaddr *)ifa);
1784 * Convert IP6 address to printable (loggable) representation. Caller
1785 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1787 static char digits[] = "0123456789abcdef";
1789 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1791 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1793 const u_int16_t *a = (const u_int16_t *)addr;
1795 int dcolon = 0, zero = 0;
1799 for (i = 0; i < 8; i++) {
1800 if (*(a + i) == 0) {
1805 else if (maxcnt < cnt) {
1816 for (i = 0; i < 8; i++) {
1827 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1839 d = (const u_char *)a;
1840 /* Try to eliminate leading zeros in printout like in :0001. */
1842 *cp = digits[*d >> 4];
1847 *cp = digits[*d++ & 0xf];
1848 if (zero == 0 || (*cp != '0')) {
1852 *cp = digits[*d >> 4];
1853 if (zero == 0 || (*cp != '0')) {
1857 *cp++ = digits[*d & 0xf];
1866 in6_localaddr(struct in6_addr *in6)
1868 struct in6_ifaddr *ia;
1870 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1874 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1875 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1876 &ia->ia_prefixmask.sin6_addr)) {
1877 IN6_IFADDR_RUNLOCK();
1881 IN6_IFADDR_RUNLOCK();
1887 * Return 1 if an internet address is for the local host and configured
1888 * on one of its interfaces.
1891 in6_localip(struct in6_addr *in6)
1893 struct in6_ifaddr *ia;
1896 LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1897 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
1898 IN6_IFADDR_RUNLOCK();
1902 IN6_IFADDR_RUNLOCK();
1907 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1909 struct in6_ifaddr *ia;
1912 LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
1913 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
1914 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
1915 IN6_IFADDR_RUNLOCK();
1916 return (1); /* true */
1921 IN6_IFADDR_RUNLOCK();
1923 return (0); /* false */
1927 * return length of part which dst and src are equal
1931 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1934 u_char *s = (u_char *)src, *d = (u_char *)dst;
1935 u_char *lim = s + 16, r;
1938 if ((r = (*d++ ^ *s++)) != 0) {
1949 /* XXX: to be scope conscious */
1951 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1953 int bytelen, bitlen;
1956 if (0 > len || len > 128) {
1957 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1965 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1968 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1969 p2->s6_addr[bytelen] >> (8 - bitlen))
1976 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1978 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1979 int bytelen, bitlen, i;
1982 if (0 > len || len > 128) {
1983 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1988 bzero(maskp, sizeof(*maskp));
1991 for (i = 0; i < bytelen; i++)
1992 maskp->s6_addr[i] = 0xff;
1994 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1998 * return the best address out of the same scope. if no address was
1999 * found, return the first valid address from designated IF.
2002 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2004 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2006 struct in6_ifaddr *besta = 0;
2007 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2009 dep[0] = dep[1] = NULL;
2012 * We first look for addresses in the same scope.
2013 * If there is one, return it.
2014 * If two or more, return one which matches the dst longest.
2015 * If none, return one of global addresses assigned other ifs.
2018 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2019 if (ifa->ifa_addr->sa_family != AF_INET6)
2021 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2022 continue; /* XXX: is there any case to allow anycast? */
2023 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2024 continue; /* don't use this interface */
2025 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2027 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2028 if (V_ip6_use_deprecated)
2029 dep[0] = (struct in6_ifaddr *)ifa;
2033 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2035 * call in6_matchlen() as few as possible
2039 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2040 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2043 besta = (struct in6_ifaddr *)ifa;
2046 besta = (struct in6_ifaddr *)ifa;
2050 ifa_ref(&besta->ia_ifa);
2051 IF_ADDR_RUNLOCK(ifp);
2055 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2056 if (ifa->ifa_addr->sa_family != AF_INET6)
2058 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2059 continue; /* XXX: is there any case to allow anycast? */
2060 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2061 continue; /* don't use this interface */
2062 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2064 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2065 if (V_ip6_use_deprecated)
2066 dep[1] = (struct in6_ifaddr *)ifa;
2072 IF_ADDR_RUNLOCK(ifp);
2073 return (struct in6_ifaddr *)ifa;
2076 /* use the last-resort values, that are, deprecated addresses */
2078 ifa_ref((struct ifaddr *)dep[0]);
2079 IF_ADDR_RUNLOCK(ifp);
2083 ifa_ref((struct ifaddr *)dep[1]);
2084 IF_ADDR_RUNLOCK(ifp);
2088 IF_ADDR_RUNLOCK(ifp);
2093 * perform DAD when interface becomes IFF_UP.
2096 in6_if_up(struct ifnet *ifp)
2099 struct in6_ifaddr *ia;
2102 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2103 if (ifa->ifa_addr->sa_family != AF_INET6)
2105 ia = (struct in6_ifaddr *)ifa;
2106 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2108 * The TENTATIVE flag was likely set by hand
2109 * beforehand, implicitly indicating the need for DAD.
2110 * We may be able to skip the random delay in this
2111 * case, but we impose delays just in case.
2114 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2117 IF_ADDR_RUNLOCK(ifp);
2120 * special cases, like 6to4, are handled in in6_ifattach
2122 in6_ifattach(ifp, NULL);
2126 in6if_do_dad(struct ifnet *ifp)
2128 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2131 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2134 switch (ifp->if_type) {
2140 * These interfaces do not have the IFF_LOOPBACK flag,
2141 * but loop packets back. We do not have to do DAD on such
2142 * interfaces. We should even omit it, because loop-backed
2143 * NS would confuse the DAD procedure.
2148 * Our DAD routine requires the interface up and running.
2149 * However, some interfaces can be up before the RUNNING
2150 * status. Additionaly, users may try to assign addresses
2151 * before the interface becomes up (or running).
2152 * We simply skip DAD in such a case as a work around.
2153 * XXX: we should rather mark "tentative" on such addresses,
2154 * and do DAD after the interface becomes ready.
2156 if (!((ifp->if_flags & IFF_UP) &&
2157 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2165 * Calculate max IPv6 MTU through all the interfaces and store it
2171 unsigned long maxmtu = 0;
2174 IFNET_RLOCK_NOSLEEP();
2175 TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
2176 /* this function can be called during ifnet initialization */
2177 if (!ifp->if_afdata[AF_INET6])
2179 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2180 IN6_LINKMTU(ifp) > maxmtu)
2181 maxmtu = IN6_LINKMTU(ifp);
2183 IFNET_RUNLOCK_NOSLEEP();
2184 if (maxmtu) /* update only when maxmtu is positive */
2185 V_in6_maxmtu = maxmtu;
2189 * Provide the length of interface identifiers to be used for the link attached
2190 * to the given interface. The length should be defined in "IPv6 over
2191 * xxx-link" document. Note that address architecture might also define
2192 * the length for a particular set of address prefixes, regardless of the
2193 * link type. As clarified in rfc2462bis, those two definitions should be
2194 * consistent, and those really are as of August 2004.
2197 in6_if2idlen(struct ifnet *ifp)
2199 switch (ifp->if_type) {
2200 case IFT_ETHER: /* RFC2464 */
2201 #ifdef IFT_PROPVIRTUAL
2202 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2205 case IFT_L2VLAN: /* ditto */
2207 #ifdef IFT_IEEE80211
2208 case IFT_IEEE80211: /* ditto */
2211 case IFT_MIP: /* ditto */
2213 case IFT_INFINIBAND:
2215 case IFT_FDDI: /* RFC2467 */
2217 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2219 case IFT_PPP: /* RFC2472 */
2221 case IFT_ARCNET: /* RFC2497 */
2223 case IFT_FRELAY: /* RFC2590 */
2225 case IFT_IEEE1394: /* RFC3146 */
2228 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2230 return (64); /* XXX: is this really correct? */
2233 * Unknown link type:
2234 * It might be controversial to use the today's common constant
2235 * of 64 for these cases unconditionally. For full compliance,
2236 * we should return an error in this case. On the other hand,
2237 * if we simply miss the standard for the link type or a new
2238 * standard is defined for a new link type, the IFID length
2239 * is very likely to be the common constant. As a compromise,
2240 * we always use the constant, but make an explicit notice
2241 * indicating the "unknown" case.
2243 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2248 #include <sys/sysctl.h>
2250 struct in6_llentry {
2251 struct llentry base;
2252 struct sockaddr_in6 l3_addr6;
2256 * Deletes an address from the address table.
2257 * This function is called by the timer functions
2258 * such as arptimer() and nd6_llinfo_timer(), and
2259 * the caller does the locking.
2262 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2265 LLE_LOCK_DESTROY(lle);
2266 free(lle, M_LLTABLE);
2269 static struct llentry *
2270 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2272 struct in6_llentry *lle;
2274 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2275 if (lle == NULL) /* NB: caller generates msg */
2278 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2279 lle->base.lle_refcnt = 1;
2280 lle->base.lle_free = in6_lltable_free;
2281 LLE_LOCK_INIT(&lle->base);
2282 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2283 CALLOUT_RETURNUNLOCKED);
2285 return (&lle->base);
2289 in6_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
2290 const struct sockaddr *mask, u_int flags)
2292 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2293 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2294 struct llentry *lle, *next;
2298 * (flags & LLE_STATIC) means deleting all entries
2299 * including static ND6 entries.
2301 IF_AFDATA_WLOCK(llt->llt_ifp);
2302 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2303 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2304 if (IN6_ARE_MASKED_ADDR_EQUAL(
2305 &satosin6(L3_ADDR(lle))->sin6_addr,
2306 &pfx->sin6_addr, &msk->sin6_addr) &&
2307 ((flags & LLE_STATIC) ||
2308 !(lle->la_flags & LLE_STATIC))) {
2310 if (callout_stop(&lle->la_timer))
2316 IF_AFDATA_WUNLOCK(llt->llt_ifp);
2320 in6_lltable_rtcheck(struct ifnet *ifp,
2322 const struct sockaddr *l3addr)
2325 char ip6buf[INET6_ADDRSTRLEN];
2327 KASSERT(l3addr->sa_family == AF_INET6,
2328 ("sin_family %d", l3addr->sa_family));
2330 /* Our local addresses are always only installed on the default FIB. */
2331 /* XXX rtalloc1 should take a const param */
2332 rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0,
2334 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2337 * Create an ND6 cache for an IPv6 neighbor
2338 * that is not covered by our own prefix.
2340 /* XXX ifaof_ifpforaddr should take a const param */
2341 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2348 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2349 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2358 static struct llentry *
2359 in6_lltable_lookup(struct lltable *llt, u_int flags,
2360 const struct sockaddr *l3addr)
2362 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2363 struct ifnet *ifp = llt->llt_ifp;
2364 struct llentry *lle;
2365 struct llentries *lleh;
2368 IF_AFDATA_LOCK_ASSERT(ifp);
2369 KASSERT(l3addr->sa_family == AF_INET6,
2370 ("sin_family %d", l3addr->sa_family));
2372 hashkey = sin6->sin6_addr.s6_addr32[3];
2373 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2374 LIST_FOREACH(lle, lleh, lle_next) {
2375 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2376 if (lle->la_flags & LLE_DELETED)
2378 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2379 sizeof(struct in6_addr)) == 0)
2384 if (!(flags & LLE_CREATE))
2386 IF_AFDATA_WLOCK_ASSERT(ifp);
2388 * A route that covers the given address must have
2389 * been installed 1st because we are doing a resolution,
2392 if (!(flags & LLE_IFADDR) &&
2393 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2396 lle = in6_lltable_new(l3addr, flags);
2398 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2401 lle->la_flags = flags & ~LLE_CREATE;
2402 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2403 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2404 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2408 lle->lle_head = lleh;
2409 lle->la_flags |= LLE_LINKED;
2410 LIST_INSERT_HEAD(lleh, lle, lle_next);
2411 } else if (flags & LLE_DELETE) {
2412 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2414 lle->la_flags |= LLE_DELETED;
2416 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2418 if ((lle->la_flags &
2419 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
2426 if (LLE_IS_VALID(lle)) {
2427 if (flags & LLE_EXCLUSIVE)
2436 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2438 struct ifnet *ifp = llt->llt_ifp;
2439 struct llentry *lle;
2442 struct rt_msghdr rtm;
2443 struct sockaddr_in6 sin6;
2445 * ndp.c assumes that sdl is word aligned
2450 struct sockaddr_dl sdl;
2454 if (ifp->if_flags & IFF_LOOPBACK)
2457 LLTABLE_LOCK_ASSERT();
2460 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2461 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2462 struct sockaddr_dl *sdl;
2464 /* skip deleted or invalid entries */
2465 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2467 /* Skip if jailed and not a valid IP of the prison. */
2468 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2471 * produce a msg made of:
2473 * struct sockaddr_in6 (IPv6)
2474 * struct sockaddr_dl;
2476 bzero(&ndpc, sizeof(ndpc));
2477 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2478 ndpc.rtm.rtm_version = RTM_VERSION;
2479 ndpc.rtm.rtm_type = RTM_GET;
2480 ndpc.rtm.rtm_flags = RTF_UP;
2481 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2482 ndpc.sin6.sin6_family = AF_INET6;
2483 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2484 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2485 if (V_deembed_scopeid)
2486 sa6_recoverscope(&ndpc.sin6);
2489 if (lle->la_flags & LLE_PUB)
2490 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2493 sdl->sdl_family = AF_LINK;
2494 sdl->sdl_len = sizeof(*sdl);
2495 sdl->sdl_alen = ifp->if_addrlen;
2496 sdl->sdl_index = ifp->if_index;
2497 sdl->sdl_type = ifp->if_type;
2498 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2499 ndpc.rtm.rtm_rmx.rmx_expire =
2500 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2501 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2502 if (lle->la_flags & LLE_STATIC)
2503 ndpc.rtm.rtm_flags |= RTF_STATIC;
2504 ndpc.rtm.rtm_index = ifp->if_index;
2505 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2514 in6_domifattach(struct ifnet *ifp)
2516 struct in6_ifextra *ext;
2518 /* There are not IPv6-capable interfaces. */
2519 switch (ifp->if_type) {
2525 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2526 bzero(ext, sizeof(*ext));
2528 ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2529 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2530 COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2531 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2533 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2534 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2536 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2537 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2539 ext->nd_ifinfo = nd6_ifattach(ifp);
2540 ext->scope6_id = scope6_ifattach(ifp);
2541 ext->lltable = lltable_init(ifp, AF_INET6);
2542 if (ext->lltable != NULL) {
2543 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2544 ext->lltable->llt_lookup = in6_lltable_lookup;
2545 ext->lltable->llt_dump = in6_lltable_dump;
2548 ext->mld_ifinfo = mld_domifattach(ifp);
2554 in6_domifdetach(struct ifnet *ifp, void *aux)
2556 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2558 mld_domifdetach(ifp);
2559 scope6_ifdetach(ext->scope6_id);
2560 nd6_ifdetach(ext->nd_ifinfo);
2561 lltable_free(ext->lltable);
2562 COUNTER_ARRAY_FREE(ext->in6_ifstat,
2563 sizeof(struct in6_ifstat) / sizeof(uint64_t));
2564 free(ext->in6_ifstat, M_IFADDR);
2565 COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2566 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2567 free(ext->icmp6_ifstat, M_IFADDR);
2568 free(ext, M_IFADDR);
2572 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2573 * v4 mapped addr or v4 compat addr
2576 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2579 bzero(sin, sizeof(*sin));
2580 sin->sin_len = sizeof(struct sockaddr_in);
2581 sin->sin_family = AF_INET;
2582 sin->sin_port = sin6->sin6_port;
2583 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2586 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2588 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2590 bzero(sin6, sizeof(*sin6));
2591 sin6->sin6_len = sizeof(struct sockaddr_in6);
2592 sin6->sin6_family = AF_INET6;
2593 sin6->sin6_port = sin->sin_port;
2594 sin6->sin6_addr.s6_addr32[0] = 0;
2595 sin6->sin6_addr.s6_addr32[1] = 0;
2596 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2597 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2600 /* Convert sockaddr_in6 into sockaddr_in. */
2602 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2604 struct sockaddr_in *sin_p;
2605 struct sockaddr_in6 sin6;
2608 * Save original sockaddr_in6 addr and convert it
2611 sin6 = *(struct sockaddr_in6 *)nam;
2612 sin_p = (struct sockaddr_in *)nam;
2613 in6_sin6_2_sin(sin_p, &sin6);
2616 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2618 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2620 struct sockaddr_in *sin_p;
2621 struct sockaddr_in6 *sin6_p;
2623 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2624 sin_p = (struct sockaddr_in *)*nam;
2625 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2626 free(*nam, M_SONAME);
2627 *nam = (struct sockaddr *)sin6_p;