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/errno.h>
73 #include <sys/malloc.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
85 #include <net/if_var.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <net/if_llatbl.h>
94 #include <netinet/if_ether.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/ip_carp.h>
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/mld6_var.h>
104 #include <netinet6/ip6_mroute.h>
105 #include <netinet6/in6_ifattach.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/in6_pcb.h>
110 * Definitions of some costant IP6 addresses.
112 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
113 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
114 const struct in6_addr in6addr_nodelocal_allnodes =
115 IN6ADDR_NODELOCAL_ALLNODES_INIT;
116 const struct in6_addr in6addr_linklocal_allnodes =
117 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
118 const struct in6_addr in6addr_linklocal_allrouters =
119 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
120 const struct in6_addr in6addr_linklocal_allv2routers =
121 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
123 const struct in6_addr in6mask0 = IN6MASK0;
124 const struct in6_addr in6mask32 = IN6MASK32;
125 const struct in6_addr in6mask64 = IN6MASK64;
126 const struct in6_addr in6mask96 = IN6MASK96;
127 const struct in6_addr in6mask128 = IN6MASK128;
129 const struct sockaddr_in6 sa6_any =
130 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
132 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
133 struct ifnet *, struct thread *));
134 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
135 struct sockaddr_in6 *, int));
136 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
138 int (*faithprefix_p)(struct in6_addr *);
140 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
141 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
144 in6_ifaddloop(struct ifaddr *ifa)
146 struct sockaddr_dl gateway;
147 struct sockaddr_in6 mask, addr;
149 struct in6_ifaddr *ia;
156 ifa->ifa_rtrequest = nd6_rtrequest;
157 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
158 LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
159 IF_AFDATA_UNLOCK(ifp);
161 ln->la_expire = 0; /* for IPv6 this means permanent */
162 ln->ln_state = ND6_LLINFO_REACHABLE;
164 * initialize for rtmsg generation
166 bzero(&gateway, sizeof(gateway));
167 gateway.sdl_len = sizeof(gateway);
168 gateway.sdl_family = AF_LINK;
169 gateway.sdl_nlen = 0;
170 gateway.sdl_alen = 6;
171 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned,
172 sizeof(ln->ll_addr));
176 bzero(&rt, sizeof(rt));
177 rt.rt_gateway = (struct sockaddr *)&gateway;
178 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
179 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
180 rt_mask(&rt) = (struct sockaddr *)&mask;
181 rt_key(&rt) = (struct sockaddr *)&addr;
182 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
183 /* Announce arrival of local address to all FIBs. */
184 rt_newaddrmsg(RTM_ADD, ifa, 0, &rt);
188 in6_ifremloop(struct ifaddr *ifa)
190 struct sockaddr_dl gateway;
191 struct sockaddr_in6 mask, addr;
193 struct in6_ifaddr *ia;
199 lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
200 (struct sockaddr *)&ia->ia_addr);
201 IF_AFDATA_UNLOCK(ifp);
204 * initialize for rtmsg generation
206 bzero(&gateway, sizeof(gateway));
207 gateway.sdl_len = sizeof(gateway);
208 gateway.sdl_family = AF_LINK;
209 gateway.sdl_nlen = 0;
210 gateway.sdl_alen = ifp->if_addrlen;
211 bzero(&rt0, sizeof(rt0));
212 rt0.rt_gateway = (struct sockaddr *)&gateway;
213 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
214 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
215 rt_mask(&rt0) = (struct sockaddr *)&mask;
216 rt_key(&rt0) = (struct sockaddr *)&addr;
217 rt0.rt_flags = RTF_HOST | RTF_STATIC;
218 /* Announce removal of local address to all FIBs. */
219 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
223 in6_mask2len(struct in6_addr *mask, u_char *lim0)
226 u_char *lim = lim0, *p;
228 /* ignore the scope_id part */
229 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
230 lim = (u_char *)mask + sizeof(*mask);
231 for (p = (u_char *)mask; p < lim; x++, p++) {
237 for (y = 0; y < 8; y++) {
238 if ((*p & (0x80 >> y)) == 0)
244 * when the limit pointer is given, do a stricter check on the
248 if (y != 0 && (*p & (0x00ff >> y)) != 0)
250 for (p = p + 1; p < lim; p++)
258 #ifdef COMPAT_FREEBSD32
259 struct in6_ndifreq32 {
260 char ifname[IFNAMSIZ];
263 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
267 in6_control(struct socket *so, u_long cmd, caddr_t data,
268 struct ifnet *ifp, struct thread *td)
270 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
271 struct in6_ifaddr *ia = NULL;
272 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
273 struct sockaddr_in6 *sa6;
274 int carp_attached = 0;
279 * Compat to make pre-10.x ifconfig(8) operable.
281 if (cmd == OSIOCAIFADDR_IN6)
282 cmd = SIOCAIFADDR_IN6;
285 case SIOCGETSGCNT_IN6:
286 case SIOCGETMIFCNT_IN6:
288 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
289 * We cannot see how that would be needed, so do not adjust the
290 * KPI blindly; more likely should clean up the IPv4 variant.
292 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
296 case SIOCAADDRCTL_POLICY:
297 case SIOCDADDRCTL_POLICY:
299 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
303 return (in6_src_ioctl(cmd, data));
310 case SIOCSNDFLUSH_IN6:
311 case SIOCSPFXFLUSH_IN6:
312 case SIOCSRTRFLUSH_IN6:
313 case SIOCSDEFIFACE_IN6:
314 case SIOCSIFINFO_FLAGS:
315 case SIOCSIFINFO_IN6:
317 error = priv_check(td, PRIV_NETINET_ND6);
322 case OSIOCGIFINFO_IN6:
323 case SIOCGIFINFO_IN6:
326 case SIOCGNBRINFO_IN6:
327 case SIOCGDEFIFACE_IN6:
328 return (nd6_ioctl(cmd, data, ifp));
330 #ifdef COMPAT_FREEBSD32
331 case SIOCGDEFIFACE32_IN6:
333 struct in6_ndifreq ndif;
334 struct in6_ndifreq32 *ndif32;
336 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
340 ndif32 = (struct in6_ndifreq32 *)data;
341 ndif32->ifindex = ndif.ifindex;
348 case SIOCSIFPREFIX_IN6:
349 case SIOCDIFPREFIX_IN6:
350 case SIOCAIFPREFIX_IN6:
351 case SIOCCIFPREFIX_IN6:
352 case SIOCSGIFPREFIX_IN6:
353 case SIOCGIFPREFIX_IN6:
355 "prefix ioctls are now invalidated. "
356 "please use ifconfig.\n");
363 error = priv_check(td, PRIV_NETINET_SCOPE6);
367 return (scope6_set(ifp,
368 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
370 return (scope6_get(ifp,
371 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
373 return (scope6_get_default((struct scope6_id *)
374 ifr->ifr_ifru.ifru_scope_id));
380 error = priv_check(td, PRIV_NET_ADDIFADDR);
384 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
388 error = priv_check(td, PRIV_NET_DELIFADDR);
394 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
398 * Find address for this interface, if it exists.
400 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
401 * only, and used the first interface address as the target of other
402 * operations (without checking ifra_addr). This was because netinet
403 * code/API assumed at most 1 interface address per interface.
404 * Since IPv6 allows a node to assign multiple addresses
405 * on a single interface, we almost always look and check the
406 * presence of ifra_addr, and reject invalid ones here.
407 * It also decreases duplicated code among SIOC*_IN6 operations.
410 case SIOCAIFADDR_IN6:
411 case SIOCSIFPHYADDR_IN6:
412 sa6 = &ifra->ifra_addr;
414 case SIOCSIFADDR_IN6:
415 case SIOCGIFADDR_IN6:
416 case SIOCSIFDSTADDR_IN6:
417 case SIOCSIFNETMASK_IN6:
418 case SIOCGIFDSTADDR_IN6:
419 case SIOCGIFNETMASK_IN6:
420 case SIOCDIFADDR_IN6:
421 case SIOCGIFPSRCADDR_IN6:
422 case SIOCGIFPDSTADDR_IN6:
423 case SIOCGIFAFLAG_IN6:
424 case SIOCSNDFLUSH_IN6:
425 case SIOCSPFXFLUSH_IN6:
426 case SIOCSRTRFLUSH_IN6:
427 case SIOCGIFALIFETIME_IN6:
428 case SIOCSIFALIFETIME_IN6:
429 case SIOCGIFSTAT_IN6:
430 case SIOCGIFSTAT_ICMP6:
431 sa6 = &ifr->ifr_addr;
437 if (sa6 && sa6->sin6_family == AF_INET6) {
438 if (sa6->sin6_scope_id != 0)
439 error = sa6_embedscope(sa6, 0);
441 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
444 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
445 &sa6->sin6_addr)) != 0)
447 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
452 case SIOCSIFADDR_IN6:
453 case SIOCSIFDSTADDR_IN6:
454 case SIOCSIFNETMASK_IN6:
456 * Since IPv6 allows a node to assign multiple addresses
457 * on a single interface, SIOCSIFxxx ioctls are deprecated.
459 /* we decided to obsolete this command (20000704) */
463 case SIOCDIFADDR_IN6:
465 * for IPv4, we look for existing in_ifaddr here to allow
466 * "ifconfig if0 delete" to remove the first IPv4 address on
467 * the interface. For IPv6, as the spec allows multiple
468 * interface address from the day one, we consider "remove the
469 * first one" semantics to be not preferable.
472 error = EADDRNOTAVAIL;
476 case SIOCAIFADDR_IN6:
478 * We always require users to specify a valid IPv6 address for
479 * the corresponding operation.
481 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
482 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
483 error = EAFNOSUPPORT;
488 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
489 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
495 case SIOCGIFADDR_IN6:
496 /* This interface is basically deprecated. use SIOCGIFCONF. */
498 case SIOCGIFAFLAG_IN6:
499 case SIOCGIFNETMASK_IN6:
500 case SIOCGIFDSTADDR_IN6:
501 case SIOCGIFALIFETIME_IN6:
502 /* must think again about its semantics */
504 error = EADDRNOTAVAIL;
509 case SIOCSIFALIFETIME_IN6:
511 struct in6_addrlifetime *lt;
514 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
519 error = EADDRNOTAVAIL;
522 /* sanity for overflow - beware unsigned */
523 lt = &ifr->ifr_ifru.ifru_lifetime;
524 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
525 lt->ia6t_vltime + time_second < time_second) {
529 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
530 lt->ia6t_pltime + time_second < time_second) {
539 case SIOCGIFADDR_IN6:
540 ifr->ifr_addr = ia->ia_addr;
541 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
545 case SIOCGIFDSTADDR_IN6:
546 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
551 * XXX: should we check if ifa_dstaddr is NULL and return
554 ifr->ifr_dstaddr = ia->ia_dstaddr;
555 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
559 case SIOCGIFNETMASK_IN6:
560 ifr->ifr_addr = ia->ia_prefixmask;
563 case SIOCGIFAFLAG_IN6:
564 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
567 case SIOCGIFSTAT_IN6:
572 bzero(&ifr->ifr_ifru.ifru_stat,
573 sizeof(ifr->ifr_ifru.ifru_stat));
574 ifr->ifr_ifru.ifru_stat =
575 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
578 case SIOCGIFSTAT_ICMP6:
583 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
584 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
585 ifr->ifr_ifru.ifru_icmp6stat =
586 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
589 case SIOCGIFALIFETIME_IN6:
590 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
591 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
593 struct in6_addrlifetime *retlt =
594 &ifr->ifr_ifru.ifru_lifetime;
597 * XXX: adjust expiration time assuming time_t is
601 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
602 if (ia->ia6_lifetime.ia6t_vltime <
603 maxexpire - ia->ia6_updatetime) {
604 retlt->ia6t_expire = ia->ia6_updatetime +
605 ia->ia6_lifetime.ia6t_vltime;
607 retlt->ia6t_expire = maxexpire;
609 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
611 struct in6_addrlifetime *retlt =
612 &ifr->ifr_ifru.ifru_lifetime;
615 * XXX: adjust expiration time assuming time_t is
619 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
620 if (ia->ia6_lifetime.ia6t_pltime <
621 maxexpire - ia->ia6_updatetime) {
622 retlt->ia6t_preferred = ia->ia6_updatetime +
623 ia->ia6_lifetime.ia6t_pltime;
625 retlt->ia6t_preferred = maxexpire;
629 case SIOCSIFALIFETIME_IN6:
630 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
632 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
633 ia->ia6_lifetime.ia6t_expire =
634 time_second + ia->ia6_lifetime.ia6t_vltime;
636 ia->ia6_lifetime.ia6t_expire = 0;
637 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
638 ia->ia6_lifetime.ia6t_preferred =
639 time_second + ia->ia6_lifetime.ia6t_pltime;
641 ia->ia6_lifetime.ia6t_preferred = 0;
644 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 for (i = 0; i < 4; i++) {
700 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
701 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
704 * XXX: since we don't have an API to set prefix (not address)
705 * lifetimes, we just use the same lifetimes as addresses.
706 * The (temporarily) installed lifetimes can be overridden by
707 * later advertised RAs (when accept_rtadv is non 0), which is
708 * an intended behavior.
710 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
712 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
713 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
714 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
716 /* add the prefix if not yet. */
717 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
719 * nd6_prelist_add will install the corresponding
722 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
724 (*carp_detach_p)(&ia->ia_ifa);
729 (*carp_detach_p)(&ia->ia_ifa);
730 log(LOG_ERR, "nd6_prelist_add succeeded but "
737 /* relate the address to the prefix */
738 if (ia->ia6_ndpr == NULL) {
743 * If this is the first autoconf address from the
744 * prefix, create a temporary address as well
747 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
748 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
750 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
751 log(LOG_NOTICE, "in6_control: failed "
752 "to create a temporary address, "
759 * this might affect the status of autoconfigured addresses,
760 * that is, this address might make other addresses detached.
762 pfxlist_onlink_check();
763 if (error == 0 && ia) {
764 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
766 * Try to clear the flag when a new
767 * IPv6 address is added onto an
768 * IFDISABLED interface and it
771 struct in6_ndireq nd;
773 memset(&nd, 0, sizeof(nd));
774 nd.ndi.flags = ND_IFINFO(ifp)->flags;
775 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
776 if (nd6_ioctl(SIOCSIFINFO_FLAGS,
777 (caddr_t)&nd, ifp) < 0)
778 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
779 "SIOCSIFINFO_FLAGS for -ifdisabled "
782 * Ignore failure of clearing the flag
783 * intentionally. The failure means
784 * address duplication was detected.
787 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
792 case SIOCDIFADDR_IN6:
794 struct nd_prefix *pr;
797 * If the address being deleted is the only one that owns
798 * the corresponding prefix, expire the prefix as well.
799 * XXX: theoretically, we don't have to worry about such
800 * relationship, since we separate the address management
801 * and the prefix management. We do this, however, to provide
802 * as much backward compatibility as possible in terms of
803 * the ioctl operation.
804 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
807 in6_purgeaddr(&ia->ia_ifa);
808 if (pr && pr->ndpr_refcnt == 0)
810 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
815 if (ifp == NULL || ifp->if_ioctl == 0) {
819 error = (*ifp->if_ioctl)(ifp, cmd, data);
826 ifa_free(&ia->ia_ifa);
832 * Join necessary multicast groups. Factored out from in6_update_ifa().
833 * This entire work should only be done once, for the default FIB.
836 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
837 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
839 char ip6buf[INET6_ADDRSTRLEN];
840 struct sockaddr_in6 mltaddr, mltmask;
841 struct in6_addr llsol;
842 struct in6_multi_mship *imm;
846 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
848 /* Join solicited multicast addr for new host id. */
849 bzero(&llsol, sizeof(struct in6_addr));
850 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
851 llsol.s6_addr32[1] = 0;
852 llsol.s6_addr32[2] = htonl(1);
853 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
854 llsol.s6_addr8[12] = 0xff;
855 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
856 /* XXX: should not happen */
857 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
861 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
863 * We need a random delay for DAD on the address being
864 * configured. It also means delaying transmission of the
865 * corresponding MLD report to avoid report collision.
866 * [RFC 4861, Section 6.3.7]
868 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
870 imm = in6_joingroup(ifp, &llsol, &error, delay);
872 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
873 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &llsol),
874 if_name(ifp), error));
877 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
878 *in6m_sol = imm->i6mm_maddr;
880 bzero(&mltmask, sizeof(mltmask));
881 mltmask.sin6_len = sizeof(struct sockaddr_in6);
882 mltmask.sin6_family = AF_INET6;
883 mltmask.sin6_addr = in6mask32;
884 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
887 * Join link-local all-nodes address.
889 bzero(&mltaddr, sizeof(mltaddr));
890 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
891 mltaddr.sin6_family = AF_INET6;
892 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
893 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
894 goto cleanup; /* XXX: should not fail */
897 * XXX: do we really need this automatic routes? We should probably
898 * reconsider this stuff. Most applications actually do not need the
899 * routes, since they usually specify the outgoing interface.
901 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
903 /* XXX: only works in !SCOPEDROUTING case. */
904 if (memcmp(&mltaddr.sin6_addr,
905 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
912 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
913 (struct sockaddr *)&ia->ia_addr,
914 (struct sockaddr *)&mltmask, RTF_UP,
915 (struct rtentry **)0, RT_DEFAULT_FIB);
921 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
923 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
924 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
925 &mltaddr.sin6_addr), if_name(ifp), error));
928 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
931 * Join node information group address.
934 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
936 * The spec does not say anything about delay for this group,
937 * but the same logic should apply.
939 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
941 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
943 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
945 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
946 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
947 &mltaddr.sin6_addr), if_name(ifp), error));
948 /* XXX not very fatal, go on... */
950 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
954 * Join interface-local all-nodes address.
955 * (ff01::1%ifN, and ff01::%ifN/32)
957 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
958 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
959 goto cleanup; /* XXX: should not fail */
960 /* XXX: again, do we really need the route? */
961 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
963 if (memcmp(&mltaddr.sin6_addr,
964 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
971 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
972 (struct sockaddr *)&ia->ia_addr,
973 (struct sockaddr *)&mltmask, RTF_UP,
974 (struct rtentry **)0, RT_DEFAULT_FIB);
980 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
982 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
983 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
984 &mltaddr.sin6_addr), if_name(ifp), error));
987 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
995 * Update parameters of an IPv6 interface address.
996 * If necessary, a new entry is created and linked into address chains.
997 * This function is separated from in6_control().
998 * XXX: should this be performed under splnet()?
1001 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1002 struct in6_ifaddr *ia, int flags)
1004 int error = 0, hostIsNew = 0, plen = -1;
1005 struct sockaddr_in6 dst6;
1006 struct in6_addrlifetime *lt;
1007 struct in6_multi *in6m_sol;
1009 char ip6buf[INET6_ADDRSTRLEN];
1011 /* Validate parameters */
1012 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
1016 * The destination address for a p2p link must have a family
1017 * of AF_UNSPEC or AF_INET6.
1019 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1020 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
1021 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
1022 return (EAFNOSUPPORT);
1024 * validate ifra_prefixmask. don't check sin6_family, netmask
1025 * does not carry fields other than sin6_len.
1027 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
1030 * Because the IPv6 address architecture is classless, we require
1031 * users to specify a (non 0) prefix length (mask) for a new address.
1032 * We also require the prefix (when specified) mask is valid, and thus
1033 * reject a non-consecutive mask.
1035 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
1037 if (ifra->ifra_prefixmask.sin6_len != 0) {
1038 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
1039 (u_char *)&ifra->ifra_prefixmask +
1040 ifra->ifra_prefixmask.sin6_len);
1045 * In this case, ia must not be NULL. We just use its prefix
1048 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1051 * If the destination address on a p2p interface is specified,
1052 * and the address is a scoped one, validate/set the scope
1055 dst6 = ifra->ifra_dstaddr;
1056 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
1057 (dst6.sin6_family == AF_INET6)) {
1058 struct in6_addr in6_tmp;
1061 in6_tmp = dst6.sin6_addr;
1062 if (in6_setscope(&in6_tmp, ifp, &zoneid))
1063 return (EINVAL); /* XXX: should be impossible */
1065 if (dst6.sin6_scope_id != 0) {
1066 if (dst6.sin6_scope_id != zoneid)
1068 } else /* user omit to specify the ID. */
1069 dst6.sin6_scope_id = zoneid;
1071 /* convert into the internal form */
1072 if (sa6_embedscope(&dst6, 0))
1073 return (EINVAL); /* XXX: should be impossible */
1076 * The destination address can be specified only for a p2p or a
1077 * loopback interface. If specified, the corresponding prefix length
1080 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
1081 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1082 /* XXX: noisy message */
1083 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1084 "be specified for a p2p or a loopback IF only\n"));
1088 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1089 "be 128 when dstaddr is specified\n"));
1093 /* lifetime consistency check */
1094 lt = &ifra->ifra_lifetime;
1095 if (lt->ia6t_pltime > lt->ia6t_vltime)
1097 if (lt->ia6t_vltime == 0) {
1099 * the following log might be noisy, but this is a typical
1100 * configuration mistake or a tool's bug.
1103 "in6_update_ifa: valid lifetime is 0 for %s\n",
1104 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1107 return (0); /* there's nothing to do */
1111 * If this is a new address, allocate a new ifaddr and link it
1117 * When in6_update_ifa() is called in a process of a received
1118 * RA, it is called under an interrupt context. So, we should
1119 * call malloc with M_NOWAIT.
1121 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
1125 bzero((caddr_t)ia, sizeof(*ia));
1126 ifa_init(&ia->ia_ifa);
1127 LIST_INIT(&ia->ia6_memberships);
1128 /* Initialize the address and masks, and put time stamp */
1129 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1130 ia->ia_addr.sin6_family = AF_INET6;
1131 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1132 ia->ia6_createtime = time_second;
1133 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1135 * XXX: some functions expect that ifa_dstaddr is not
1136 * NULL for p2p interfaces.
1138 ia->ia_ifa.ifa_dstaddr =
1139 (struct sockaddr *)&ia->ia_dstaddr;
1141 ia->ia_ifa.ifa_dstaddr = NULL;
1143 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1145 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1147 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1148 IF_ADDR_WUNLOCK(ifp);
1150 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1152 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1153 IN6_IFADDR_WUNLOCK();
1156 /* update timestamp */
1157 ia->ia6_updatetime = time_second;
1159 /* set prefix mask */
1160 if (ifra->ifra_prefixmask.sin6_len) {
1162 * We prohibit changing the prefix length of an existing
1164 * + such an operation should be rare in IPv6, and
1165 * + the operation would confuse prefix management.
1167 if (ia->ia_prefixmask.sin6_len &&
1168 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1169 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
1170 " existing (%s) address should not be changed\n",
1171 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1175 ia->ia_prefixmask = ifra->ifra_prefixmask;
1179 * If a new destination address is specified, scrub the old one and
1180 * install the new destination. Note that the interface must be
1181 * p2p or loopback (see the check above.)
1183 if (dst6.sin6_family == AF_INET6 &&
1184 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1187 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1188 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1189 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1190 "a route to the old destination: %s\n",
1191 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1192 /* proceed anyway... */
1194 ia->ia_flags &= ~IFA_ROUTE;
1195 ia->ia_dstaddr = dst6;
1199 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1200 * to see if the address is deprecated or invalidated, but initialize
1201 * these members for applications.
1203 ia->ia6_lifetime = ifra->ifra_lifetime;
1204 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1205 ia->ia6_lifetime.ia6t_expire =
1206 time_second + ia->ia6_lifetime.ia6t_vltime;
1208 ia->ia6_lifetime.ia6t_expire = 0;
1209 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1210 ia->ia6_lifetime.ia6t_preferred =
1211 time_second + ia->ia6_lifetime.ia6t_pltime;
1213 ia->ia6_lifetime.ia6t_preferred = 0;
1215 /* reset the interface and routing table appropriately. */
1216 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1220 * configure address flags.
1222 ia->ia6_flags = ifra->ifra_flags;
1224 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1225 * userland, make it deprecated.
1227 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1228 ia->ia6_lifetime.ia6t_pltime = 0;
1229 ia->ia6_lifetime.ia6t_preferred = time_second;
1232 * Make the address tentative before joining multicast addresses,
1233 * so that corresponding MLD responses would not have a tentative
1236 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1237 if (hostIsNew && in6if_do_dad(ifp))
1238 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1240 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
1241 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1242 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1245 * We are done if we have simply modified an existing address.
1251 * Beyond this point, we should call in6_purgeaddr upon an error,
1252 * not just go to unlink.
1255 /* Join necessary multicast groups. */
1257 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1258 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1264 * Perform DAD, if needed.
1265 * XXX It may be of use, if we can administratively disable DAD.
1267 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1268 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1270 int mindelay, maxdelay;
1273 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1275 * We need to impose a delay before sending an NS
1276 * for DAD. Check if we also needed a delay for the
1277 * corresponding MLD message. If we did, the delay
1278 * should be larger than the MLD delay (this could be
1279 * relaxed a bit, but this simple logic is at least
1281 * XXX: Break data hiding guidelines and look at
1282 * state for the solicited multicast group.
1285 if (in6m_sol != NULL &&
1286 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1287 mindelay = in6m_sol->in6m_timer;
1289 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1290 if (maxdelay - mindelay == 0)
1294 (arc4random() % (maxdelay - mindelay)) +
1298 nd6_dad_start((struct ifaddr *)ia, delay);
1301 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1302 ifa_free(&ia->ia_ifa);
1307 * XXX: if a change of an existing address failed, keep the entry
1311 in6_unlink_ifa(ia, ifp);
1312 ifa_free(&ia->ia_ifa);
1317 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1318 ifa_free(&ia->ia_ifa);
1319 in6_purgeaddr(&ia->ia_ifa);
1324 * Leave multicast groups. Factored out from in6_purgeaddr().
1325 * This entire work should only be done once, for the default FIB.
1328 in6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0)
1330 struct sockaddr_in6 mltaddr, mltmask;
1331 struct in6_multi_mship *imm;
1336 * Leave from multicast groups we have joined for the interface.
1338 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1339 LIST_REMOVE(imm, i6mm_chain);
1340 in6_leavegroup(imm);
1344 * Remove the link-local all-nodes address.
1346 bzero(&mltmask, sizeof(mltmask));
1347 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1348 mltmask.sin6_family = AF_INET6;
1349 mltmask.sin6_addr = in6mask32;
1351 bzero(&mltaddr, sizeof(mltaddr));
1352 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1353 mltaddr.sin6_family = AF_INET6;
1354 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1356 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1359 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1360 if (rt != NULL && rt->rt_gateway != NULL &&
1361 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1362 &ia->ia_addr.sin6_addr,
1363 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1365 * If no more IPv6 address exists on this interface then
1366 * remove the multicast address route.
1369 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1370 sizeof(mltaddr.sin6_addr));
1372 error = in6_rtrequest(RTM_DELETE,
1373 (struct sockaddr *)&mltaddr,
1374 (struct sockaddr *)&ia->ia_addr,
1375 (struct sockaddr *)&mltmask, RTF_UP,
1376 (struct rtentry **)0, RT_DEFAULT_FIB);
1378 log(LOG_INFO, "%s: link-local all-nodes "
1379 "multicast address deletion error\n",
1383 * Replace the gateway of the route.
1385 struct sockaddr_in6 sa;
1387 bzero(&sa, sizeof(sa));
1388 sa.sin6_len = sizeof(struct sockaddr_in6);
1389 sa.sin6_family = AF_INET6;
1390 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1391 sizeof(sa.sin6_addr));
1392 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1393 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1402 * Remove the node-local all-nodes address.
1404 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1405 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1408 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1409 if (rt != NULL && rt->rt_gateway != NULL &&
1410 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1411 &ia->ia_addr.sin6_addr,
1412 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1414 * If no more IPv6 address exists on this interface then
1415 * remove the multicast address route.
1418 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1419 sizeof(mltaddr.sin6_addr));
1422 error = in6_rtrequest(RTM_DELETE,
1423 (struct sockaddr *)&mltaddr,
1424 (struct sockaddr *)&ia->ia_addr,
1425 (struct sockaddr *)&mltmask, RTF_UP,
1426 (struct rtentry **)0, RT_DEFAULT_FIB);
1428 log(LOG_INFO, "%s: node-local all-nodes"
1429 "multicast address deletion error\n",
1433 * Replace the gateway of the route.
1435 struct sockaddr_in6 sa;
1437 bzero(&sa, sizeof(sa));
1438 sa.sin6_len = sizeof(struct sockaddr_in6);
1439 sa.sin6_family = AF_INET6;
1440 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1441 sizeof(sa.sin6_addr));
1442 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1443 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1455 in6_purgeaddr(struct ifaddr *ifa)
1457 struct ifnet *ifp = ifa->ifa_ifp;
1458 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1460 struct ifaddr *ifa0;
1463 (*carp_detach_p)(ifa);
1466 * find another IPv6 address as the gateway for the
1467 * link-local and node-local all-nodes multicast
1471 TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1472 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1473 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1474 &ia->ia_addr.sin6_addr,
1475 sizeof(struct in6_addr)) == 0)
1482 IF_ADDR_RUNLOCK(ifp);
1485 * Remove the loopback route to the interface address.
1486 * The check for the current setting of "nd6_useloopback"
1489 if (ia->ia_flags & IFA_RTSELF) {
1490 error = ifa_del_loopback_route((struct ifaddr *)ia,
1491 (struct sockaddr *)&ia->ia_addr);
1493 ia->ia_flags &= ~IFA_RTSELF;
1496 /* stop DAD processing */
1499 /* Remove local address entry from lltable. */
1502 /* Leave multicast groups. */
1503 error = in6_purgeaddr_mc(ifp, ia, ifa0);
1508 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1509 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1510 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1511 (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0);
1513 log(LOG_INFO, "%s: err=%d, destination address delete "
1514 "failed\n", __func__, error);
1515 ia->ia_flags &= ~IFA_ROUTE;
1518 in6_unlink_ifa(ia, ifp);
1522 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1527 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1528 IF_ADDR_WUNLOCK(ifp);
1529 ifa_free(&ia->ia_ifa); /* if_addrhead */
1532 * Defer the release of what might be the last reference to the
1533 * in6_ifaddr so that it can't be freed before the remainder of the
1537 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1538 IN6_IFADDR_WUNLOCK();
1541 * Release the reference to the base prefix. There should be a
1542 * positive reference.
1544 if (ia->ia6_ndpr == NULL) {
1546 "in6_unlink_ifa: autoconf'ed address "
1547 "%p has no prefix\n", ia));
1549 ia->ia6_ndpr->ndpr_refcnt--;
1550 ia->ia6_ndpr = NULL;
1554 * Also, if the address being removed is autoconf'ed, call
1555 * pfxlist_onlink_check() since the release might affect the status of
1556 * other (detached) addresses.
1558 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1559 pfxlist_onlink_check();
1561 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1566 in6_purgeif(struct ifnet *ifp)
1568 struct ifaddr *ifa, *nifa;
1570 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1571 if (ifa->ifa_addr->sa_family != AF_INET6)
1581 * SIOCGLIFADDR: get first address. (?)
1582 * SIOCGLIFADDR with IFLR_PREFIX:
1583 * get first address that matches the specified prefix.
1584 * SIOCALIFADDR: add the specified address.
1585 * SIOCALIFADDR with IFLR_PREFIX:
1586 * add the specified prefix, filling hostid part from
1587 * the first link-local address. prefixlen must be <= 64.
1588 * SIOCDLIFADDR: delete the specified address.
1589 * SIOCDLIFADDR with IFLR_PREFIX:
1590 * delete the first address that matches the specified prefix.
1592 * EINVAL on invalid parameters
1593 * EADDRNOTAVAIL on prefix match failed/specified address not found
1594 * other values may be returned from in6_ioctl()
1596 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1597 * this is to accomodate address naming scheme other than RFC2374,
1599 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1600 * address encoding scheme. (see figure on page 8)
1603 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1604 struct ifnet *ifp, struct thread *td)
1606 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1608 struct sockaddr *sa;
1611 if (!data || !ifp) {
1612 panic("invalid argument to in6_lifaddr_ioctl");
1618 /* address must be specified on GET with IFLR_PREFIX */
1619 if ((iflr->flags & IFLR_PREFIX) == 0)
1624 /* address must be specified on ADD and DELETE */
1625 sa = (struct sockaddr *)&iflr->addr;
1626 if (sa->sa_family != AF_INET6)
1628 if (sa->sa_len != sizeof(struct sockaddr_in6))
1630 /* XXX need improvement */
1631 sa = (struct sockaddr *)&iflr->dstaddr;
1632 if (sa->sa_family && sa->sa_family != AF_INET6)
1634 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1637 default: /* shouldn't happen */
1639 panic("invalid cmd to in6_lifaddr_ioctl");
1645 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1651 struct in6_aliasreq ifra;
1652 struct in6_addr *hostid = NULL;
1656 if ((iflr->flags & IFLR_PREFIX) != 0) {
1657 struct sockaddr_in6 *sin6;
1660 * hostid is to fill in the hostid part of the
1661 * address. hostid points to the first link-local
1662 * address attached to the interface.
1664 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1666 return EADDRNOTAVAIL;
1667 hostid = IFA_IN6(ifa);
1669 /* prefixlen must be <= 64. */
1670 if (64 < iflr->prefixlen)
1672 prefixlen = iflr->prefixlen;
1674 /* hostid part must be zero. */
1675 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1676 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1677 sin6->sin6_addr.s6_addr32[3] != 0) {
1681 prefixlen = iflr->prefixlen;
1683 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1684 bzero(&ifra, sizeof(ifra));
1685 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1687 bcopy(&iflr->addr, &ifra.ifra_addr,
1688 ((struct sockaddr *)&iflr->addr)->sa_len);
1690 /* fill in hostid part */
1691 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1692 hostid->s6_addr32[2];
1693 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1694 hostid->s6_addr32[3];
1697 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1698 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1699 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1701 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1702 hostid->s6_addr32[2];
1703 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1704 hostid->s6_addr32[3];
1710 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1711 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1713 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1714 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1719 struct in6_ifaddr *ia;
1720 struct in6_addr mask, candidate, match;
1721 struct sockaddr_in6 *sin6;
1724 bzero(&mask, sizeof(mask));
1725 if (iflr->flags & IFLR_PREFIX) {
1726 /* lookup a prefix rather than address. */
1727 in6_prefixlen2mask(&mask, iflr->prefixlen);
1729 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1730 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1731 match.s6_addr32[0] &= mask.s6_addr32[0];
1732 match.s6_addr32[1] &= mask.s6_addr32[1];
1733 match.s6_addr32[2] &= mask.s6_addr32[2];
1734 match.s6_addr32[3] &= mask.s6_addr32[3];
1736 /* if you set extra bits, that's wrong */
1737 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1742 if (cmd == SIOCGLIFADDR) {
1743 /* on getting an address, take the 1st match */
1746 /* on deleting an address, do exact match */
1747 in6_prefixlen2mask(&mask, 128);
1748 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1749 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1756 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1757 if (ifa->ifa_addr->sa_family != AF_INET6)
1763 * XXX: this is adhoc, but is necessary to allow
1764 * a user to specify fe80::/64 (not /10) for a
1765 * link-local address.
1767 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1768 in6_clearscope(&candidate);
1769 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1770 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1771 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1772 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1773 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1778 IF_ADDR_RUNLOCK(ifp);
1780 return EADDRNOTAVAIL;
1783 if (cmd == SIOCGLIFADDR) {
1786 /* fill in the if_laddrreq structure */
1787 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1788 error = sa6_recoverscope(
1789 (struct sockaddr_in6 *)&iflr->addr);
1795 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1796 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1797 ia->ia_dstaddr.sin6_len);
1798 error = sa6_recoverscope(
1799 (struct sockaddr_in6 *)&iflr->dstaddr);
1805 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1808 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1810 iflr->flags = ia->ia6_flags; /* XXX */
1815 struct in6_aliasreq ifra;
1817 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1818 bzero(&ifra, sizeof(ifra));
1819 bcopy(iflr->iflr_name, ifra.ifra_name,
1820 sizeof(ifra.ifra_name));
1822 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1823 ia->ia_addr.sin6_len);
1824 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1825 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1826 ia->ia_dstaddr.sin6_len);
1828 bzero(&ifra.ifra_dstaddr,
1829 sizeof(ifra.ifra_dstaddr));
1831 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1832 ia->ia_prefixmask.sin6_len);
1834 ifra.ifra_flags = ia->ia6_flags;
1836 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1842 return EOPNOTSUPP; /* just for safety */
1846 * Initialize an interface's IPv6 address and routing table entry.
1849 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1850 struct sockaddr_in6 *sin6, int newhost)
1852 int error = 0, plen, ifacount = 0;
1857 * Give the interface a chance to initialize
1858 * if this is its first address,
1859 * and to validate the address if necessary.
1862 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1863 if (ifa->ifa_addr->sa_family != AF_INET6)
1867 IF_ADDR_RUNLOCK(ifp);
1869 ia->ia_addr = *sin6;
1871 if (ifacount <= 1 && ifp->if_ioctl) {
1872 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1880 ia->ia_ifa.ifa_metric = ifp->if_metric;
1882 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1886 * If a new destination address is specified for a point-to-point
1887 * interface, install a route to the destination as an interface
1889 * XXX: the logic below rejects assigning multiple addresses on a p2p
1890 * interface that share the same destination.
1892 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1893 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1894 ia->ia_dstaddr.sin6_family == AF_INET6) {
1895 int rtflags = RTF_UP | RTF_HOST;
1896 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1899 ia->ia_flags |= IFA_ROUTE;
1901 * Handle the case for ::1 .
1903 if (ifp->if_flags & IFF_LOOPBACK)
1904 ia->ia_flags |= IFA_RTSELF;
1908 * add a loopback route to self
1910 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1911 error = ifa_add_loopback_route((struct ifaddr *)ia,
1912 (struct sockaddr *)&ia->ia_addr);
1914 ia->ia_flags |= IFA_RTSELF;
1917 /* Add local address to lltable, if necessary (ex. on p2p link). */
1919 in6_ifaddloop(&(ia->ia_ifa));
1925 * Find an IPv6 interface link-local address specific to an interface.
1926 * ifaddr is returned referenced.
1929 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1934 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1935 if (ifa->ifa_addr->sa_family != AF_INET6)
1937 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1938 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1945 IF_ADDR_RUNLOCK(ifp);
1947 return ((struct in6_ifaddr *)ifa);
1952 * find the internet address corresponding to a given interface and address.
1953 * ifaddr is returned referenced.
1956 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1961 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1962 if (ifa->ifa_addr->sa_family != AF_INET6)
1964 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1969 IF_ADDR_RUNLOCK(ifp);
1971 return ((struct in6_ifaddr *)ifa);
1975 * Convert IP6 address to printable (loggable) representation. Caller
1976 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1978 static char digits[] = "0123456789abcdef";
1980 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1982 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1984 const u_int16_t *a = (const u_int16_t *)addr;
1986 int dcolon = 0, zero = 0;
1990 for (i = 0; i < 8; i++) {
1991 if (*(a + i) == 0) {
1996 else if (maxcnt < cnt) {
2007 for (i = 0; i < 8; i++) {
2018 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
2030 d = (const u_char *)a;
2031 /* Try to eliminate leading zeros in printout like in :0001. */
2033 *cp = digits[*d >> 4];
2038 *cp = digits[*d++ & 0xf];
2039 if (zero == 0 || (*cp != '0')) {
2043 *cp = digits[*d >> 4];
2044 if (zero == 0 || (*cp != '0')) {
2048 *cp++ = digits[*d & 0xf];
2057 in6_localaddr(struct in6_addr *in6)
2059 struct in6_ifaddr *ia;
2061 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
2065 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2066 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2067 &ia->ia_prefixmask.sin6_addr)) {
2068 IN6_IFADDR_RUNLOCK();
2072 IN6_IFADDR_RUNLOCK();
2078 * Return 1 if an internet address is for the local host and configured
2079 * on one of its interfaces.
2082 in6_localip(struct in6_addr *in6)
2084 struct in6_ifaddr *ia;
2087 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2088 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
2089 IN6_IFADDR_RUNLOCK();
2093 IN6_IFADDR_RUNLOCK();
2099 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
2101 struct in6_ifaddr *ia;
2104 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2105 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
2107 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
2108 IN6_IFADDR_RUNLOCK();
2109 return (1); /* true */
2112 /* XXX: do we still have to go thru the rest of the list? */
2114 IN6_IFADDR_RUNLOCK();
2116 return (0); /* false */
2120 * return length of part which dst and src are equal
2124 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2127 u_char *s = (u_char *)src, *d = (u_char *)dst;
2128 u_char *lim = s + 16, r;
2131 if ((r = (*d++ ^ *s++)) != 0) {
2142 /* XXX: to be scope conscious */
2144 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2146 int bytelen, bitlen;
2149 if (0 > len || len > 128) {
2150 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2158 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2161 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2162 p2->s6_addr[bytelen] >> (8 - bitlen))
2169 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2171 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2172 int bytelen, bitlen, i;
2175 if (0 > len || len > 128) {
2176 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2181 bzero(maskp, sizeof(*maskp));
2184 for (i = 0; i < bytelen; i++)
2185 maskp->s6_addr[i] = 0xff;
2187 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2191 * return the best address out of the same scope. if no address was
2192 * found, return the first valid address from designated IF.
2195 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2197 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2199 struct in6_ifaddr *besta = 0;
2200 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2202 dep[0] = dep[1] = NULL;
2205 * We first look for addresses in the same scope.
2206 * If there is one, return it.
2207 * If two or more, return one which matches the dst longest.
2208 * If none, return one of global addresses assigned other ifs.
2211 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2212 if (ifa->ifa_addr->sa_family != AF_INET6)
2214 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2215 continue; /* XXX: is there any case to allow anycast? */
2216 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2217 continue; /* don't use this interface */
2218 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2220 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2221 if (V_ip6_use_deprecated)
2222 dep[0] = (struct in6_ifaddr *)ifa;
2226 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2228 * call in6_matchlen() as few as possible
2232 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2233 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2236 besta = (struct in6_ifaddr *)ifa;
2239 besta = (struct in6_ifaddr *)ifa;
2243 ifa_ref(&besta->ia_ifa);
2244 IF_ADDR_RUNLOCK(ifp);
2248 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2249 if (ifa->ifa_addr->sa_family != AF_INET6)
2251 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2252 continue; /* XXX: is there any case to allow anycast? */
2253 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2254 continue; /* don't use this interface */
2255 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2257 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2258 if (V_ip6_use_deprecated)
2259 dep[1] = (struct in6_ifaddr *)ifa;
2265 IF_ADDR_RUNLOCK(ifp);
2266 return (struct in6_ifaddr *)ifa;
2268 IF_ADDR_RUNLOCK(ifp);
2270 /* use the last-resort values, that are, deprecated addresses */
2280 * perform DAD when interface becomes IFF_UP.
2283 in6_if_up(struct ifnet *ifp)
2286 struct in6_ifaddr *ia;
2289 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2290 if (ifa->ifa_addr->sa_family != AF_INET6)
2292 ia = (struct in6_ifaddr *)ifa;
2293 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2295 * The TENTATIVE flag was likely set by hand
2296 * beforehand, implicitly indicating the need for DAD.
2297 * We may be able to skip the random delay in this
2298 * case, but we impose delays just in case.
2301 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2304 IF_ADDR_RUNLOCK(ifp);
2307 * special cases, like 6to4, are handled in in6_ifattach
2309 in6_ifattach(ifp, NULL);
2313 in6if_do_dad(struct ifnet *ifp)
2315 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2318 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2321 switch (ifp->if_type) {
2327 * These interfaces do not have the IFF_LOOPBACK flag,
2328 * but loop packets back. We do not have to do DAD on such
2329 * interfaces. We should even omit it, because loop-backed
2330 * NS would confuse the DAD procedure.
2335 * Our DAD routine requires the interface up and running.
2336 * However, some interfaces can be up before the RUNNING
2337 * status. Additionaly, users may try to assign addresses
2338 * before the interface becomes up (or running).
2339 * We simply skip DAD in such a case as a work around.
2340 * XXX: we should rather mark "tentative" on such addresses,
2341 * and do DAD after the interface becomes ready.
2343 if (!((ifp->if_flags & IFF_UP) &&
2344 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2352 * Calculate max IPv6 MTU through all the interfaces and store it
2358 unsigned long maxmtu = 0;
2361 IFNET_RLOCK_NOSLEEP();
2362 TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
2363 /* this function can be called during ifnet initialization */
2364 if (!ifp->if_afdata[AF_INET6])
2366 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2367 IN6_LINKMTU(ifp) > maxmtu)
2368 maxmtu = IN6_LINKMTU(ifp);
2370 IFNET_RUNLOCK_NOSLEEP();
2371 if (maxmtu) /* update only when maxmtu is positive */
2372 V_in6_maxmtu = maxmtu;
2376 * Provide the length of interface identifiers to be used for the link attached
2377 * to the given interface. The length should be defined in "IPv6 over
2378 * xxx-link" document. Note that address architecture might also define
2379 * the length for a particular set of address prefixes, regardless of the
2380 * link type. As clarified in rfc2462bis, those two definitions should be
2381 * consistent, and those really are as of August 2004.
2384 in6_if2idlen(struct ifnet *ifp)
2386 switch (ifp->if_type) {
2387 case IFT_ETHER: /* RFC2464 */
2388 #ifdef IFT_PROPVIRTUAL
2389 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2392 case IFT_L2VLAN: /* ditto */
2394 #ifdef IFT_IEEE80211
2395 case IFT_IEEE80211: /* ditto */
2398 case IFT_MIP: /* ditto */
2400 case IFT_INFINIBAND:
2402 case IFT_FDDI: /* RFC2467 */
2404 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2406 case IFT_PPP: /* RFC2472 */
2408 case IFT_ARCNET: /* RFC2497 */
2410 case IFT_FRELAY: /* RFC2590 */
2412 case IFT_IEEE1394: /* RFC3146 */
2415 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2417 return (64); /* XXX: is this really correct? */
2420 * Unknown link type:
2421 * It might be controversial to use the today's common constant
2422 * of 64 for these cases unconditionally. For full compliance,
2423 * we should return an error in this case. On the other hand,
2424 * if we simply miss the standard for the link type or a new
2425 * standard is defined for a new link type, the IFID length
2426 * is very likely to be the common constant. As a compromise,
2427 * we always use the constant, but make an explicit notice
2428 * indicating the "unknown" case.
2430 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2435 #include <sys/sysctl.h>
2437 struct in6_llentry {
2438 struct llentry base;
2439 struct sockaddr_in6 l3_addr6;
2443 * Deletes an address from the address table.
2444 * This function is called by the timer functions
2445 * such as arptimer() and nd6_llinfo_timer(), and
2446 * the caller does the locking.
2449 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2452 LLE_LOCK_DESTROY(lle);
2453 free(lle, M_LLTABLE);
2456 static struct llentry *
2457 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2459 struct in6_llentry *lle;
2461 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2462 M_DONTWAIT | M_ZERO);
2463 if (lle == NULL) /* NB: caller generates msg */
2466 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2467 lle->base.lle_refcnt = 1;
2468 lle->base.lle_free = in6_lltable_free;
2469 LLE_LOCK_INIT(&lle->base);
2470 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2471 CALLOUT_RETURNUNLOCKED);
2477 in6_lltable_prefix_free(struct lltable *llt,
2478 const struct sockaddr *prefix,
2479 const struct sockaddr *mask,
2482 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2483 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2484 struct llentry *lle, *next;
2488 * (flags & LLE_STATIC) means deleting all entries
2489 * including static ND6 entries
2491 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2492 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2493 if (IN6_ARE_MASKED_ADDR_EQUAL(
2494 &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2497 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) {
2500 canceled = callout_drain(&lle->la_timer);
2511 in6_lltable_rtcheck(struct ifnet *ifp,
2513 const struct sockaddr *l3addr)
2516 char ip6buf[INET6_ADDRSTRLEN];
2518 KASSERT(l3addr->sa_family == AF_INET6,
2519 ("sin_family %d", l3addr->sa_family));
2521 /* Our local addresses are always only installed on the default FIB. */
2522 /* XXX rtalloc1 should take a const param */
2523 rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0,
2525 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2528 * Create an ND6 cache for an IPv6 neighbor
2529 * that is not covered by our own prefix.
2531 /* XXX ifaof_ifpforaddr should take a const param */
2532 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2539 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2540 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2549 static struct llentry *
2550 in6_lltable_lookup(struct lltable *llt, u_int flags,
2551 const struct sockaddr *l3addr)
2553 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2554 struct ifnet *ifp = llt->llt_ifp;
2555 struct llentry *lle;
2556 struct llentries *lleh;
2559 IF_AFDATA_LOCK_ASSERT(ifp);
2560 KASSERT(l3addr->sa_family == AF_INET6,
2561 ("sin_family %d", l3addr->sa_family));
2563 hashkey = sin6->sin6_addr.s6_addr32[3];
2564 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2565 LIST_FOREACH(lle, lleh, lle_next) {
2566 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2567 if (lle->la_flags & LLE_DELETED)
2569 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2570 sizeof(struct in6_addr)) == 0)
2575 if (!(flags & LLE_CREATE))
2578 * A route that covers the given address must have
2579 * been installed 1st because we are doing a resolution,
2582 if (!(flags & LLE_IFADDR) &&
2583 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2586 lle = in6_lltable_new(l3addr, flags);
2588 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2591 lle->la_flags = flags & ~LLE_CREATE;
2592 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2593 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2594 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2598 lle->lle_head = lleh;
2599 LIST_INSERT_HEAD(lleh, lle, lle_next);
2600 } else if (flags & LLE_DELETE) {
2601 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2603 lle->la_flags = LLE_DELETED;
2606 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2611 if (LLE_IS_VALID(lle)) {
2612 if (flags & LLE_EXCLUSIVE)
2621 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2623 struct ifnet *ifp = llt->llt_ifp;
2624 struct llentry *lle;
2627 struct rt_msghdr rtm;
2628 struct sockaddr_in6 sin6;
2630 * ndp.c assumes that sdl is word aligned
2635 struct sockaddr_dl sdl;
2639 if (ifp->if_flags & IFF_LOOPBACK)
2642 LLTABLE_LOCK_ASSERT();
2645 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2646 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2647 struct sockaddr_dl *sdl;
2649 /* skip deleted or invalid entries */
2650 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2652 /* Skip if jailed and not a valid IP of the prison. */
2653 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2656 * produce a msg made of:
2658 * struct sockaddr_in6 (IPv6)
2659 * struct sockaddr_dl;
2661 bzero(&ndpc, sizeof(ndpc));
2662 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2663 ndpc.rtm.rtm_version = RTM_VERSION;
2664 ndpc.rtm.rtm_type = RTM_GET;
2665 ndpc.rtm.rtm_flags = RTF_UP;
2666 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2667 ndpc.sin6.sin6_family = AF_INET6;
2668 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2669 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2672 if (lle->la_flags & LLE_PUB)
2673 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2676 sdl->sdl_family = AF_LINK;
2677 sdl->sdl_len = sizeof(*sdl);
2678 sdl->sdl_alen = ifp->if_addrlen;
2679 sdl->sdl_index = ifp->if_index;
2680 sdl->sdl_type = ifp->if_type;
2681 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2682 ndpc.rtm.rtm_rmx.rmx_expire =
2683 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2684 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2685 if (lle->la_flags & LLE_STATIC)
2686 ndpc.rtm.rtm_flags |= RTF_STATIC;
2687 ndpc.rtm.rtm_index = ifp->if_index;
2688 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2697 in6_domifattach(struct ifnet *ifp)
2699 struct in6_ifextra *ext;
2701 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2702 bzero(ext, sizeof(*ext));
2704 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2705 M_IFADDR, M_WAITOK);
2706 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2709 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2710 M_IFADDR, M_WAITOK);
2711 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2713 ext->nd_ifinfo = nd6_ifattach(ifp);
2714 ext->scope6_id = scope6_ifattach(ifp);
2715 ext->lltable = lltable_init(ifp, AF_INET6);
2716 if (ext->lltable != NULL) {
2717 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2718 ext->lltable->llt_lookup = in6_lltable_lookup;
2719 ext->lltable->llt_dump = in6_lltable_dump;
2722 ext->mld_ifinfo = mld_domifattach(ifp);
2728 in6_domifdetach(struct ifnet *ifp, void *aux)
2730 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2732 mld_domifdetach(ifp);
2733 scope6_ifdetach(ext->scope6_id);
2734 nd6_ifdetach(ext->nd_ifinfo);
2735 lltable_free(ext->lltable);
2736 free(ext->in6_ifstat, M_IFADDR);
2737 free(ext->icmp6_ifstat, M_IFADDR);
2738 free(ext, M_IFADDR);
2742 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2743 * v4 mapped addr or v4 compat addr
2746 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2749 bzero(sin, sizeof(*sin));
2750 sin->sin_len = sizeof(struct sockaddr_in);
2751 sin->sin_family = AF_INET;
2752 sin->sin_port = sin6->sin6_port;
2753 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2756 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2758 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2760 bzero(sin6, sizeof(*sin6));
2761 sin6->sin6_len = sizeof(struct sockaddr_in6);
2762 sin6->sin6_family = AF_INET6;
2763 sin6->sin6_port = sin->sin_port;
2764 sin6->sin6_addr.s6_addr32[0] = 0;
2765 sin6->sin6_addr.s6_addr32[1] = 0;
2766 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2767 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2770 /* Convert sockaddr_in6 into sockaddr_in. */
2772 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2774 struct sockaddr_in *sin_p;
2775 struct sockaddr_in6 sin6;
2778 * Save original sockaddr_in6 addr and convert it
2781 sin6 = *(struct sockaddr_in6 *)nam;
2782 sin_p = (struct sockaddr_in *)nam;
2783 in6_sin6_2_sin(sin_p, &sin6);
2786 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2788 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2790 struct sockaddr_in *sin_p;
2791 struct sockaddr_in6 *sin6_p;
2793 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2795 sin_p = (struct sockaddr_in *)*nam;
2796 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2797 free(*nam, M_SONAME);
2798 *nam = (struct sockaddr *)sin6_p;