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>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/nd6.h>
102 #include <netinet6/mld6_var.h>
103 #include <netinet6/ip6_mroute.h>
104 #include <netinet6/in6_ifattach.h>
105 #include <netinet6/scope6_var.h>
106 #include <netinet6/in6_pcb.h>
109 * Definitions of some costant IP6 addresses.
111 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
112 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
113 const struct in6_addr in6addr_nodelocal_allnodes =
114 IN6ADDR_NODELOCAL_ALLNODES_INIT;
115 const struct in6_addr in6addr_linklocal_allnodes =
116 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
117 const struct in6_addr in6addr_linklocal_allrouters =
118 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
119 const struct in6_addr in6addr_linklocal_allv2routers =
120 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
122 const struct in6_addr in6mask0 = IN6MASK0;
123 const struct in6_addr in6mask32 = IN6MASK32;
124 const struct in6_addr in6mask64 = IN6MASK64;
125 const struct in6_addr in6mask96 = IN6MASK96;
126 const struct in6_addr in6mask128 = IN6MASK128;
128 const struct sockaddr_in6 sa6_any =
129 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
131 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
132 struct ifnet *, struct thread *));
133 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
134 struct sockaddr_in6 *, int));
135 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
137 int (*faithprefix_p)(struct in6_addr *);
142 in6_mask2len(struct in6_addr *mask, u_char *lim0)
145 u_char *lim = lim0, *p;
147 /* ignore the scope_id part */
148 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
149 lim = (u_char *)mask + sizeof(*mask);
150 for (p = (u_char *)mask; p < lim; x++, p++) {
156 for (y = 0; y < 8; y++) {
157 if ((*p & (0x80 >> y)) == 0)
163 * when the limit pointer is given, do a stricter check on the
167 if (y != 0 && (*p & (0x00ff >> y)) != 0)
169 for (p = p + 1; p < lim; p++)
177 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
178 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
180 #ifdef COMPAT_FREEBSD32
181 struct in6_ndifreq32 {
182 char ifname[IFNAMSIZ];
185 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
189 in6_control(struct socket *so, u_long cmd, caddr_t data,
190 struct ifnet *ifp, struct thread *td)
192 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
193 struct in6_ifaddr *ia = NULL;
194 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
195 struct sockaddr_in6 *sa6;
199 case SIOCGETSGCNT_IN6:
200 case SIOCGETMIFCNT_IN6:
202 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
203 * We cannot see how that would be needed, so do not adjust the
204 * KPI blindly; more likely should clean up the IPv4 variant.
206 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
210 case SIOCAADDRCTL_POLICY:
211 case SIOCDADDRCTL_POLICY:
213 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
217 return (in6_src_ioctl(cmd, data));
224 case SIOCSNDFLUSH_IN6:
225 case SIOCSPFXFLUSH_IN6:
226 case SIOCSRTRFLUSH_IN6:
227 case SIOCSDEFIFACE_IN6:
228 case SIOCSIFINFO_FLAGS:
229 case SIOCSIFINFO_IN6:
231 error = priv_check(td, PRIV_NETINET_ND6);
236 case OSIOCGIFINFO_IN6:
237 case SIOCGIFINFO_IN6:
240 case SIOCGNBRINFO_IN6:
241 case SIOCGDEFIFACE_IN6:
242 return (nd6_ioctl(cmd, data, ifp));
244 #ifdef COMPAT_FREEBSD32
245 case SIOCGDEFIFACE32_IN6:
247 struct in6_ndifreq ndif;
248 struct in6_ndifreq32 *ndif32;
250 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
254 ndif32 = (struct in6_ndifreq32 *)data;
255 ndif32->ifindex = ndif.ifindex;
262 case SIOCSIFPREFIX_IN6:
263 case SIOCDIFPREFIX_IN6:
264 case SIOCAIFPREFIX_IN6:
265 case SIOCCIFPREFIX_IN6:
266 case SIOCSGIFPREFIX_IN6:
267 case SIOCGIFPREFIX_IN6:
269 "prefix ioctls are now invalidated. "
270 "please use ifconfig.\n");
277 error = priv_check(td, PRIV_NETINET_SCOPE6);
281 return (scope6_set(ifp,
282 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
284 return (scope6_get(ifp,
285 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
287 return (scope6_get_default((struct scope6_id *)
288 ifr->ifr_ifru.ifru_scope_id));
294 error = priv_check(td, PRIV_NET_ADDIFADDR);
298 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
302 error = priv_check(td, PRIV_NET_DELIFADDR);
308 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
312 * Find address for this interface, if it exists.
314 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
315 * only, and used the first interface address as the target of other
316 * operations (without checking ifra_addr). This was because netinet
317 * code/API assumed at most 1 interface address per interface.
318 * Since IPv6 allows a node to assign multiple addresses
319 * on a single interface, we almost always look and check the
320 * presence of ifra_addr, and reject invalid ones here.
321 * It also decreases duplicated code among SIOC*_IN6 operations.
324 case SIOCAIFADDR_IN6:
325 case SIOCSIFPHYADDR_IN6:
326 sa6 = &ifra->ifra_addr;
328 case SIOCSIFADDR_IN6:
329 case SIOCGIFADDR_IN6:
330 case SIOCSIFDSTADDR_IN6:
331 case SIOCSIFNETMASK_IN6:
332 case SIOCGIFDSTADDR_IN6:
333 case SIOCGIFNETMASK_IN6:
334 case SIOCDIFADDR_IN6:
335 case SIOCGIFPSRCADDR_IN6:
336 case SIOCGIFPDSTADDR_IN6:
337 case SIOCGIFAFLAG_IN6:
338 case SIOCSNDFLUSH_IN6:
339 case SIOCSPFXFLUSH_IN6:
340 case SIOCSRTRFLUSH_IN6:
341 case SIOCGIFALIFETIME_IN6:
342 case SIOCSIFALIFETIME_IN6:
343 case SIOCGIFSTAT_IN6:
344 case SIOCGIFSTAT_ICMP6:
345 sa6 = &ifr->ifr_addr;
351 if (sa6 && sa6->sin6_family == AF_INET6) {
352 if (sa6->sin6_scope_id != 0)
353 error = sa6_embedscope(sa6, 0);
355 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
358 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
359 &sa6->sin6_addr)) != 0)
361 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
366 case SIOCSIFADDR_IN6:
367 case SIOCSIFDSTADDR_IN6:
368 case SIOCSIFNETMASK_IN6:
370 * Since IPv6 allows a node to assign multiple addresses
371 * on a single interface, SIOCSIFxxx ioctls are deprecated.
373 /* we decided to obsolete this command (20000704) */
377 case SIOCDIFADDR_IN6:
379 * for IPv4, we look for existing in_ifaddr here to allow
380 * "ifconfig if0 delete" to remove the first IPv4 address on
381 * the interface. For IPv6, as the spec allows multiple
382 * interface address from the day one, we consider "remove the
383 * first one" semantics to be not preferable.
386 error = EADDRNOTAVAIL;
390 case SIOCAIFADDR_IN6:
392 * We always require users to specify a valid IPv6 address for
393 * the corresponding operation.
395 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
396 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
397 error = EAFNOSUPPORT;
402 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
403 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
409 case SIOCGIFADDR_IN6:
410 /* This interface is basically deprecated. use SIOCGIFCONF. */
412 case SIOCGIFAFLAG_IN6:
413 case SIOCGIFNETMASK_IN6:
414 case SIOCGIFDSTADDR_IN6:
415 case SIOCGIFALIFETIME_IN6:
416 /* must think again about its semantics */
418 error = EADDRNOTAVAIL;
423 case SIOCSIFALIFETIME_IN6:
425 struct in6_addrlifetime *lt;
428 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
433 error = EADDRNOTAVAIL;
436 /* sanity for overflow - beware unsigned */
437 lt = &ifr->ifr_ifru.ifru_lifetime;
438 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
439 lt->ia6t_vltime + time_second < time_second) {
443 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
444 lt->ia6t_pltime + time_second < time_second) {
453 case SIOCGIFADDR_IN6:
454 ifr->ifr_addr = ia->ia_addr;
455 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
459 case SIOCGIFDSTADDR_IN6:
460 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
465 * XXX: should we check if ifa_dstaddr is NULL and return
468 ifr->ifr_dstaddr = ia->ia_dstaddr;
469 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
473 case SIOCGIFNETMASK_IN6:
474 ifr->ifr_addr = ia->ia_prefixmask;
477 case SIOCGIFAFLAG_IN6:
478 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
481 case SIOCGIFSTAT_IN6:
486 bzero(&ifr->ifr_ifru.ifru_stat,
487 sizeof(ifr->ifr_ifru.ifru_stat));
488 ifr->ifr_ifru.ifru_stat =
489 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
492 case SIOCGIFSTAT_ICMP6:
497 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
498 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
499 ifr->ifr_ifru.ifru_icmp6stat =
500 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
503 case SIOCGIFALIFETIME_IN6:
504 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
505 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
507 struct in6_addrlifetime *retlt =
508 &ifr->ifr_ifru.ifru_lifetime;
511 * XXX: adjust expiration time assuming time_t is
515 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
516 if (ia->ia6_lifetime.ia6t_vltime <
517 maxexpire - ia->ia6_updatetime) {
518 retlt->ia6t_expire = ia->ia6_updatetime +
519 ia->ia6_lifetime.ia6t_vltime;
521 retlt->ia6t_expire = maxexpire;
523 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
525 struct in6_addrlifetime *retlt =
526 &ifr->ifr_ifru.ifru_lifetime;
529 * XXX: adjust expiration time assuming time_t is
533 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
534 if (ia->ia6_lifetime.ia6t_pltime <
535 maxexpire - ia->ia6_updatetime) {
536 retlt->ia6t_preferred = ia->ia6_updatetime +
537 ia->ia6_lifetime.ia6t_pltime;
539 retlt->ia6t_preferred = maxexpire;
543 case SIOCSIFALIFETIME_IN6:
544 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
546 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
547 ia->ia6_lifetime.ia6t_expire =
548 time_second + ia->ia6_lifetime.ia6t_vltime;
550 ia->ia6_lifetime.ia6t_expire = 0;
551 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
552 ia->ia6_lifetime.ia6t_preferred =
553 time_second + ia->ia6_lifetime.ia6t_pltime;
555 ia->ia6_lifetime.ia6t_preferred = 0;
558 case SIOCAIFADDR_IN6:
561 struct nd_prefixctl pr0;
562 struct nd_prefix *pr;
565 * first, make or update the interface address structure,
566 * and link it to the list.
568 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
571 ifa_free(&ia->ia_ifa);
572 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
575 * this can happen when the user specify the 0 valid
582 * then, make the prefix on-link on the interface.
583 * XXX: we'd rather create the prefix before the address, but
584 * we need at least one address to install the corresponding
585 * interface route, so we configure the address first.
589 * convert mask to prefix length (prefixmask has already
590 * been validated in in6_update_ifa().
592 bzero(&pr0, sizeof(pr0));
594 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
596 if (pr0.ndpr_plen == 128) {
597 break; /* we don't need to install a host route. */
599 pr0.ndpr_prefix = ifra->ifra_addr;
600 /* apply the mask for safety. */
601 for (i = 0; i < 4; i++) {
602 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
603 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
606 * XXX: since we don't have an API to set prefix (not address)
607 * lifetimes, we just use the same lifetimes as addresses.
608 * The (temporarily) installed lifetimes can be overridden by
609 * later advertised RAs (when accept_rtadv is non 0), which is
610 * an intended behavior.
612 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
614 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
615 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
616 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
618 /* add the prefix if not yet. */
619 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
621 * nd6_prelist_add will install the corresponding
624 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
627 log(LOG_ERR, "nd6_prelist_add succeeded but "
634 /* relate the address to the prefix */
635 if (ia->ia6_ndpr == NULL) {
640 * If this is the first autoconf address from the
641 * prefix, create a temporary address as well
644 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
645 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
647 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
648 log(LOG_NOTICE, "in6_control: failed "
649 "to create a temporary address, "
656 * this might affect the status of autoconfigured addresses,
657 * that is, this address might make other addresses detached.
659 pfxlist_onlink_check();
660 if (error == 0 && ia)
661 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
665 case SIOCDIFADDR_IN6:
667 struct nd_prefix *pr;
670 * If the address being deleted is the only one that owns
671 * the corresponding prefix, expire the prefix as well.
672 * XXX: theoretically, we don't have to worry about such
673 * relationship, since we separate the address management
674 * and the prefix management. We do this, however, to provide
675 * as much backward compatibility as possible in terms of
676 * the ioctl operation.
677 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
680 in6_purgeaddr(&ia->ia_ifa);
681 if (pr && pr->ndpr_refcnt == 0)
683 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
688 if (ifp == NULL || ifp->if_ioctl == 0) {
692 error = (*ifp->if_ioctl)(ifp, cmd, data);
699 ifa_free(&ia->ia_ifa);
704 * Join necessary multicast groups. Factored out from in6_update_ifa().
705 * This entire work should only be done once, for the default FIB.
708 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
709 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
711 char ip6buf[INET6_ADDRSTRLEN];
712 struct sockaddr_in6 mltaddr, mltmask;
713 struct in6_addr llsol;
714 struct in6_multi_mship *imm;
718 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
720 /* Join solicited multicast addr for new host id. */
721 bzero(&llsol, sizeof(struct in6_addr));
722 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
723 llsol.s6_addr32[1] = 0;
724 llsol.s6_addr32[2] = htonl(1);
725 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
726 llsol.s6_addr8[12] = 0xff;
727 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
728 /* XXX: should not happen */
729 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
733 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
735 * We need a random delay for DAD on the address being
736 * configured. It also means delaying transmission of the
737 * corresponding MLD report to avoid report collision.
738 * [RFC 4861, Section 6.3.7]
740 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
742 imm = in6_joingroup(ifp, &llsol, &error, delay);
744 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
745 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &llsol),
746 if_name(ifp), error));
749 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
750 *in6m_sol = imm->i6mm_maddr;
752 bzero(&mltmask, sizeof(mltmask));
753 mltmask.sin6_len = sizeof(struct sockaddr_in6);
754 mltmask.sin6_family = AF_INET6;
755 mltmask.sin6_addr = in6mask32;
756 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
759 * Join link-local all-nodes address.
761 bzero(&mltaddr, sizeof(mltaddr));
762 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
763 mltaddr.sin6_family = AF_INET6;
764 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
765 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
766 goto cleanup; /* XXX: should not fail */
769 * XXX: do we really need this automatic routes? We should probably
770 * reconsider this stuff. Most applications actually do not need the
771 * routes, since they usually specify the outgoing interface.
773 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
775 /* XXX: only works in !SCOPEDROUTING case. */
776 if (memcmp(&mltaddr.sin6_addr,
777 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
784 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
785 (struct sockaddr *)&ia->ia_addr,
786 (struct sockaddr *)&mltmask, RTF_UP,
787 (struct rtentry **)0, RT_DEFAULT_FIB);
793 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
795 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
796 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
797 &mltaddr.sin6_addr), if_name(ifp), error));
800 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
803 * Join node information group address.
806 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
808 * The spec does not say anything about delay for this group,
809 * but the same logic should apply.
811 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
813 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
815 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
817 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
818 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
819 &mltaddr.sin6_addr), if_name(ifp), error));
820 /* XXX not very fatal, go on... */
822 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
826 * Join interface-local all-nodes address.
827 * (ff01::1%ifN, and ff01::%ifN/32)
829 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
830 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
831 goto cleanup; /* XXX: should not fail */
832 /* XXX: again, do we really need the route? */
833 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
835 if (memcmp(&mltaddr.sin6_addr,
836 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
843 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
844 (struct sockaddr *)&ia->ia_addr,
845 (struct sockaddr *)&mltmask, RTF_UP,
846 (struct rtentry **)0, RT_DEFAULT_FIB);
852 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
854 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
855 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
856 &mltaddr.sin6_addr), if_name(ifp), error));
859 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
867 * Update parameters of an IPv6 interface address.
868 * If necessary, a new entry is created and linked into address chains.
869 * This function is separated from in6_control().
870 * XXX: should this be performed under splnet()?
873 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
874 struct in6_ifaddr *ia, int flags)
876 int error = 0, hostIsNew = 0, plen = -1;
877 struct sockaddr_in6 dst6;
878 struct in6_addrlifetime *lt;
879 struct in6_multi *in6m_sol;
881 char ip6buf[INET6_ADDRSTRLEN];
883 /* Validate parameters */
884 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
888 * The destination address for a p2p link must have a family
889 * of AF_UNSPEC or AF_INET6.
891 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
892 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
893 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
894 return (EAFNOSUPPORT);
896 * validate ifra_prefixmask. don't check sin6_family, netmask
897 * does not carry fields other than sin6_len.
899 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
902 * Because the IPv6 address architecture is classless, we require
903 * users to specify a (non 0) prefix length (mask) for a new address.
904 * We also require the prefix (when specified) mask is valid, and thus
905 * reject a non-consecutive mask.
907 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
909 if (ifra->ifra_prefixmask.sin6_len != 0) {
910 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
911 (u_char *)&ifra->ifra_prefixmask +
912 ifra->ifra_prefixmask.sin6_len);
917 * In this case, ia must not be NULL. We just use its prefix
920 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
923 * If the destination address on a p2p interface is specified,
924 * and the address is a scoped one, validate/set the scope
927 dst6 = ifra->ifra_dstaddr;
928 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
929 (dst6.sin6_family == AF_INET6)) {
930 struct in6_addr in6_tmp;
933 in6_tmp = dst6.sin6_addr;
934 if (in6_setscope(&in6_tmp, ifp, &zoneid))
935 return (EINVAL); /* XXX: should be impossible */
937 if (dst6.sin6_scope_id != 0) {
938 if (dst6.sin6_scope_id != zoneid)
940 } else /* user omit to specify the ID. */
941 dst6.sin6_scope_id = zoneid;
943 /* convert into the internal form */
944 if (sa6_embedscope(&dst6, 0))
945 return (EINVAL); /* XXX: should be impossible */
948 * The destination address can be specified only for a p2p or a
949 * loopback interface. If specified, the corresponding prefix length
952 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
953 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
954 /* XXX: noisy message */
955 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
956 "be specified for a p2p or a loopback IF only\n"));
960 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
961 "be 128 when dstaddr is specified\n"));
965 /* lifetime consistency check */
966 lt = &ifra->ifra_lifetime;
967 if (lt->ia6t_pltime > lt->ia6t_vltime)
969 if (lt->ia6t_vltime == 0) {
971 * the following log might be noisy, but this is a typical
972 * configuration mistake or a tool's bug.
975 "in6_update_ifa: valid lifetime is 0 for %s\n",
976 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
979 return (0); /* there's nothing to do */
983 * If this is a new address, allocate a new ifaddr and link it
989 * When in6_update_ifa() is called in a process of a received
990 * RA, it is called under an interrupt context. So, we should
991 * call malloc with M_NOWAIT.
993 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
997 bzero((caddr_t)ia, sizeof(*ia));
998 ifa_init(&ia->ia_ifa);
999 LIST_INIT(&ia->ia6_memberships);
1000 /* Initialize the address and masks, and put time stamp */
1001 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1002 ia->ia_addr.sin6_family = AF_INET6;
1003 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1004 ia->ia6_createtime = time_second;
1005 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1007 * XXX: some functions expect that ifa_dstaddr is not
1008 * NULL for p2p interfaces.
1010 ia->ia_ifa.ifa_dstaddr =
1011 (struct sockaddr *)&ia->ia_dstaddr;
1013 ia->ia_ifa.ifa_dstaddr = NULL;
1015 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1017 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1019 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1020 IF_ADDR_UNLOCK(ifp);
1022 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1024 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1025 IN6_IFADDR_WUNLOCK();
1028 /* update timestamp */
1029 ia->ia6_updatetime = time_second;
1031 /* set prefix mask */
1032 if (ifra->ifra_prefixmask.sin6_len) {
1034 * We prohibit changing the prefix length of an existing
1036 * + such an operation should be rare in IPv6, and
1037 * + the operation would confuse prefix management.
1039 if (ia->ia_prefixmask.sin6_len &&
1040 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1041 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
1042 " existing (%s) address should not be changed\n",
1043 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1047 ia->ia_prefixmask = ifra->ifra_prefixmask;
1051 * If a new destination address is specified, scrub the old one and
1052 * install the new destination. Note that the interface must be
1053 * p2p or loopback (see the check above.)
1055 if (dst6.sin6_family == AF_INET6 &&
1056 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1059 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1060 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1061 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1062 "a route to the old destination: %s\n",
1063 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1064 /* proceed anyway... */
1066 ia->ia_flags &= ~IFA_ROUTE;
1067 ia->ia_dstaddr = dst6;
1071 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1072 * to see if the address is deprecated or invalidated, but initialize
1073 * these members for applications.
1075 ia->ia6_lifetime = ifra->ifra_lifetime;
1076 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1077 ia->ia6_lifetime.ia6t_expire =
1078 time_second + ia->ia6_lifetime.ia6t_vltime;
1080 ia->ia6_lifetime.ia6t_expire = 0;
1081 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1082 ia->ia6_lifetime.ia6t_preferred =
1083 time_second + ia->ia6_lifetime.ia6t_pltime;
1085 ia->ia6_lifetime.ia6t_preferred = 0;
1087 /* reset the interface and routing table appropriately. */
1088 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1092 * configure address flags.
1094 ia->ia6_flags = ifra->ifra_flags;
1096 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1097 * userland, make it deprecated.
1099 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1100 ia->ia6_lifetime.ia6t_pltime = 0;
1101 ia->ia6_lifetime.ia6t_preferred = time_second;
1104 * Make the address tentative before joining multicast addresses,
1105 * so that corresponding MLD responses would not have a tentative
1108 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1109 if (hostIsNew && in6if_do_dad(ifp))
1110 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1113 * We are done if we have simply modified an existing address.
1119 * Beyond this point, we should call in6_purgeaddr upon an error,
1120 * not just go to unlink.
1123 /* Join necessary multicast groups. */
1125 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1126 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1132 * Perform DAD, if needed.
1133 * XXX It may be of use, if we can administratively disable DAD.
1135 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1136 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1138 int mindelay, maxdelay;
1141 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1143 * We need to impose a delay before sending an NS
1144 * for DAD. Check if we also needed a delay for the
1145 * corresponding MLD message. If we did, the delay
1146 * should be larger than the MLD delay (this could be
1147 * relaxed a bit, but this simple logic is at least
1149 * XXX: Break data hiding guidelines and look at
1150 * state for the solicited multicast group.
1153 if (in6m_sol != NULL &&
1154 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1155 mindelay = in6m_sol->in6m_timer;
1157 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1158 if (maxdelay - mindelay == 0)
1162 (arc4random() % (maxdelay - mindelay)) +
1166 nd6_dad_start((struct ifaddr *)ia, delay);
1169 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1170 ifa_free(&ia->ia_ifa);
1175 * XXX: if a change of an existing address failed, keep the entry
1179 in6_unlink_ifa(ia, ifp);
1180 ifa_free(&ia->ia_ifa);
1185 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1186 ifa_free(&ia->ia_ifa);
1187 in6_purgeaddr(&ia->ia_ifa);
1192 * Leave multicast groups. Factored out from in6_purgeaddr().
1193 * This entire work should only be done once, for the default FIB.
1196 in6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0)
1198 struct sockaddr_in6 mltaddr, mltmask;
1199 struct in6_multi_mship *imm;
1204 * Leave from multicast groups we have joined for the interface.
1206 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1207 LIST_REMOVE(imm, i6mm_chain);
1208 in6_leavegroup(imm);
1212 * Remove the link-local all-nodes address.
1214 bzero(&mltmask, sizeof(mltmask));
1215 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1216 mltmask.sin6_family = AF_INET6;
1217 mltmask.sin6_addr = in6mask32;
1219 bzero(&mltaddr, sizeof(mltaddr));
1220 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1221 mltaddr.sin6_family = AF_INET6;
1222 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1224 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1227 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1228 if (rt != NULL && rt->rt_gateway != NULL &&
1229 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1230 &ia->ia_addr.sin6_addr,
1231 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1233 * If no more IPv6 address exists on this interface then
1234 * remove the multicast address route.
1237 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1238 sizeof(mltaddr.sin6_addr));
1240 error = in6_rtrequest(RTM_DELETE,
1241 (struct sockaddr *)&mltaddr,
1242 (struct sockaddr *)&ia->ia_addr,
1243 (struct sockaddr *)&mltmask, RTF_UP,
1244 (struct rtentry **)0, RT_DEFAULT_FIB);
1246 log(LOG_INFO, "%s: link-local all-nodes "
1247 "multicast address deletion error\n",
1251 * Replace the gateway of the route.
1253 struct sockaddr_in6 sa;
1255 bzero(&sa, sizeof(sa));
1256 sa.sin6_len = sizeof(struct sockaddr_in6);
1257 sa.sin6_family = AF_INET6;
1258 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1259 sizeof(sa.sin6_addr));
1260 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1261 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1270 * Remove the node-local all-nodes address.
1272 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1273 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1276 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1277 if (rt != NULL && rt->rt_gateway != NULL &&
1278 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1279 &ia->ia_addr.sin6_addr,
1280 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1282 * If no more IPv6 address exists on this interface then
1283 * remove the multicast address route.
1286 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1287 sizeof(mltaddr.sin6_addr));
1290 error = in6_rtrequest(RTM_DELETE,
1291 (struct sockaddr *)&mltaddr,
1292 (struct sockaddr *)&ia->ia_addr,
1293 (struct sockaddr *)&mltmask, RTF_UP,
1294 (struct rtentry **)0, RT_DEFAULT_FIB);
1296 log(LOG_INFO, "%s: node-local all-nodes"
1297 "multicast address deletion error\n",
1301 * Replace the gateway of the route.
1303 struct sockaddr_in6 sa;
1305 bzero(&sa, sizeof(sa));
1306 sa.sin6_len = sizeof(struct sockaddr_in6);
1307 sa.sin6_family = AF_INET6;
1308 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1309 sizeof(sa.sin6_addr));
1310 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1311 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1323 in6_purgeaddr(struct ifaddr *ifa)
1325 struct ifnet *ifp = ifa->ifa_ifp;
1326 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1327 struct sockaddr_dl gateway;
1328 struct sockaddr_in6 mask, addr;
1331 struct ifaddr *ifa0;
1334 * find another IPv6 address as the gateway for the
1335 * link-local and node-local all-nodes multicast
1339 TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1340 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1341 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1342 &ia->ia_addr.sin6_addr,
1343 sizeof(struct in6_addr)) == 0)
1350 IF_ADDR_UNLOCK(ifp);
1353 * Remove the loopback route to the interface address.
1354 * The check for the current setting of "nd6_useloopback"
1357 if (ia->ia_flags & IFA_RTSELF) {
1358 error = ifa_del_loopback_route((struct ifaddr *)ia,
1359 (struct sockaddr *)&ia->ia_addr);
1361 ia->ia_flags &= ~IFA_RTSELF;
1364 /* stop DAD processing */
1367 /* Remove local address entry from lltable. */
1368 IF_AFDATA_LOCK(ifp);
1369 lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
1370 (struct sockaddr *)&ia->ia_addr);
1371 IF_AFDATA_UNLOCK(ifp);
1374 * initialize for rtmsg generation
1376 bzero(&gateway, sizeof(gateway));
1377 gateway.sdl_len = sizeof(gateway);
1378 gateway.sdl_family = AF_LINK;
1379 gateway.sdl_nlen = 0;
1380 gateway.sdl_alen = ifp->if_addrlen;
1382 bzero(&rt0, sizeof(rt0));
1383 rt0.rt_gateway = (struct sockaddr *)&gateway;
1384 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1385 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1386 rt_mask(&rt0) = (struct sockaddr *)&mask;
1387 rt_key(&rt0) = (struct sockaddr *)&addr;
1388 rt0.rt_flags = RTF_HOST | RTF_STATIC;
1389 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
1391 /* Leave multicast groups. */
1392 error = in6_purgeaddr_mc(ifp, ia, ifa0);
1397 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1398 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1399 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1400 (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0);
1402 log(LOG_INFO, "%s: err=%d, destination address delete "
1403 "failed\n", __func__, error);
1404 ia->ia_flags &= ~IFA_ROUTE;
1407 in6_unlink_ifa(ia, ifp);
1411 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1416 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1417 IF_ADDR_UNLOCK(ifp);
1418 ifa_free(&ia->ia_ifa); /* if_addrhead */
1421 * Defer the release of what might be the last reference to the
1422 * in6_ifaddr so that it can't be freed before the remainder of the
1426 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1427 IN6_IFADDR_WUNLOCK();
1430 * Release the reference to the base prefix. There should be a
1431 * positive reference.
1433 if (ia->ia6_ndpr == NULL) {
1435 "in6_unlink_ifa: autoconf'ed address "
1436 "%p has no prefix\n", ia));
1438 ia->ia6_ndpr->ndpr_refcnt--;
1439 ia->ia6_ndpr = NULL;
1443 * Also, if the address being removed is autoconf'ed, call
1444 * pfxlist_onlink_check() since the release might affect the status of
1445 * other (detached) addresses.
1447 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1448 pfxlist_onlink_check();
1450 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1455 in6_purgeif(struct ifnet *ifp)
1457 struct ifaddr *ifa, *nifa;
1459 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1460 if (ifa->ifa_addr->sa_family != AF_INET6)
1470 * SIOCGLIFADDR: get first address. (?)
1471 * SIOCGLIFADDR with IFLR_PREFIX:
1472 * get first address that matches the specified prefix.
1473 * SIOCALIFADDR: add the specified address.
1474 * SIOCALIFADDR with IFLR_PREFIX:
1475 * add the specified prefix, filling hostid part from
1476 * the first link-local address. prefixlen must be <= 64.
1477 * SIOCDLIFADDR: delete the specified address.
1478 * SIOCDLIFADDR with IFLR_PREFIX:
1479 * delete the first address that matches the specified prefix.
1481 * EINVAL on invalid parameters
1482 * EADDRNOTAVAIL on prefix match failed/specified address not found
1483 * other values may be returned from in6_ioctl()
1485 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1486 * this is to accomodate address naming scheme other than RFC2374,
1488 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1489 * address encoding scheme. (see figure on page 8)
1492 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1493 struct ifnet *ifp, struct thread *td)
1495 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1497 struct sockaddr *sa;
1500 if (!data || !ifp) {
1501 panic("invalid argument to in6_lifaddr_ioctl");
1507 /* address must be specified on GET with IFLR_PREFIX */
1508 if ((iflr->flags & IFLR_PREFIX) == 0)
1513 /* address must be specified on ADD and DELETE */
1514 sa = (struct sockaddr *)&iflr->addr;
1515 if (sa->sa_family != AF_INET6)
1517 if (sa->sa_len != sizeof(struct sockaddr_in6))
1519 /* XXX need improvement */
1520 sa = (struct sockaddr *)&iflr->dstaddr;
1521 if (sa->sa_family && sa->sa_family != AF_INET6)
1523 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1526 default: /* shouldn't happen */
1528 panic("invalid cmd to in6_lifaddr_ioctl");
1534 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1540 struct in6_aliasreq ifra;
1541 struct in6_addr *hostid = NULL;
1545 if ((iflr->flags & IFLR_PREFIX) != 0) {
1546 struct sockaddr_in6 *sin6;
1549 * hostid is to fill in the hostid part of the
1550 * address. hostid points to the first link-local
1551 * address attached to the interface.
1553 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1555 return EADDRNOTAVAIL;
1556 hostid = IFA_IN6(ifa);
1558 /* prefixlen must be <= 64. */
1559 if (64 < iflr->prefixlen)
1561 prefixlen = iflr->prefixlen;
1563 /* hostid part must be zero. */
1564 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1565 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1566 sin6->sin6_addr.s6_addr32[3] != 0) {
1570 prefixlen = iflr->prefixlen;
1572 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1573 bzero(&ifra, sizeof(ifra));
1574 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1576 bcopy(&iflr->addr, &ifra.ifra_addr,
1577 ((struct sockaddr *)&iflr->addr)->sa_len);
1579 /* fill in hostid part */
1580 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1581 hostid->s6_addr32[2];
1582 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1583 hostid->s6_addr32[3];
1586 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1587 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1588 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1590 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1591 hostid->s6_addr32[2];
1592 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1593 hostid->s6_addr32[3];
1599 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1600 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1602 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1603 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1608 struct in6_ifaddr *ia;
1609 struct in6_addr mask, candidate, match;
1610 struct sockaddr_in6 *sin6;
1613 bzero(&mask, sizeof(mask));
1614 if (iflr->flags & IFLR_PREFIX) {
1615 /* lookup a prefix rather than address. */
1616 in6_prefixlen2mask(&mask, iflr->prefixlen);
1618 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1619 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1620 match.s6_addr32[0] &= mask.s6_addr32[0];
1621 match.s6_addr32[1] &= mask.s6_addr32[1];
1622 match.s6_addr32[2] &= mask.s6_addr32[2];
1623 match.s6_addr32[3] &= mask.s6_addr32[3];
1625 /* if you set extra bits, that's wrong */
1626 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1631 if (cmd == SIOCGLIFADDR) {
1632 /* on getting an address, take the 1st match */
1635 /* on deleting an address, do exact match */
1636 in6_prefixlen2mask(&mask, 128);
1637 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1638 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1645 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1646 if (ifa->ifa_addr->sa_family != AF_INET6)
1652 * XXX: this is adhoc, but is necessary to allow
1653 * a user to specify fe80::/64 (not /10) for a
1654 * link-local address.
1656 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1657 in6_clearscope(&candidate);
1658 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1659 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1660 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1661 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1662 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1667 IF_ADDR_UNLOCK(ifp);
1669 return EADDRNOTAVAIL;
1672 if (cmd == SIOCGLIFADDR) {
1675 /* fill in the if_laddrreq structure */
1676 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1677 error = sa6_recoverscope(
1678 (struct sockaddr_in6 *)&iflr->addr);
1684 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1685 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1686 ia->ia_dstaddr.sin6_len);
1687 error = sa6_recoverscope(
1688 (struct sockaddr_in6 *)&iflr->dstaddr);
1694 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1697 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1699 iflr->flags = ia->ia6_flags; /* XXX */
1704 struct in6_aliasreq ifra;
1706 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1707 bzero(&ifra, sizeof(ifra));
1708 bcopy(iflr->iflr_name, ifra.ifra_name,
1709 sizeof(ifra.ifra_name));
1711 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1712 ia->ia_addr.sin6_len);
1713 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1714 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1715 ia->ia_dstaddr.sin6_len);
1717 bzero(&ifra.ifra_dstaddr,
1718 sizeof(ifra.ifra_dstaddr));
1720 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1721 ia->ia_prefixmask.sin6_len);
1723 ifra.ifra_flags = ia->ia6_flags;
1725 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1731 return EOPNOTSUPP; /* just for safety */
1735 * Initialize an interface's IPv6 address and routing table entry.
1738 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1739 struct sockaddr_in6 *sin6, int newhost)
1741 int error = 0, plen, ifacount = 0;
1746 * Give the interface a chance to initialize
1747 * if this is its first address,
1748 * and to validate the address if necessary.
1751 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1752 if (ifa->ifa_addr->sa_family != AF_INET6)
1756 IF_ADDR_UNLOCK(ifp);
1758 ia->ia_addr = *sin6;
1760 if (ifacount <= 1 && ifp->if_ioctl) {
1761 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1769 ia->ia_ifa.ifa_metric = ifp->if_metric;
1771 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1775 * If a new destination address is specified for a point-to-point
1776 * interface, install a route to the destination as an interface
1778 * XXX: the logic below rejects assigning multiple addresses on a p2p
1779 * interface that share the same destination.
1781 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1782 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1783 ia->ia_dstaddr.sin6_family == AF_INET6) {
1784 int rtflags = RTF_UP | RTF_HOST;
1785 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1788 ia->ia_flags |= IFA_ROUTE;
1790 * Handle the case for ::1 .
1792 if (ifp->if_flags & IFF_LOOPBACK)
1793 ia->ia_flags |= IFA_RTSELF;
1797 * add a loopback route to self
1799 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1800 error = ifa_add_loopback_route((struct ifaddr *)ia,
1801 (struct sockaddr *)&ia->ia_addr);
1803 ia->ia_flags |= IFA_RTSELF;
1806 /* Add local address to lltable, if necessary (ex. on p2p link). */
1810 struct sockaddr_dl gateway;
1811 struct sockaddr_in6 mask, addr;
1813 IF_AFDATA_LOCK(ifp);
1814 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1815 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE),
1816 (struct sockaddr *)&ia->ia_addr);
1817 IF_AFDATA_UNLOCK(ifp);
1819 ln->la_expire = 0; /* for IPv6 this means permanent */
1820 ln->ln_state = ND6_LLINFO_REACHABLE;
1822 * initialize for rtmsg generation
1824 bzero(&gateway, sizeof(gateway));
1825 gateway.sdl_len = sizeof(gateway);
1826 gateway.sdl_family = AF_LINK;
1827 gateway.sdl_nlen = 0;
1828 gateway.sdl_alen = 6;
1829 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr));
1834 bzero(&rt, sizeof(rt));
1835 rt.rt_gateway = (struct sockaddr *)&gateway;
1836 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1837 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1838 rt_mask(&rt) = (struct sockaddr *)&mask;
1839 rt_key(&rt) = (struct sockaddr *)&addr;
1840 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
1841 /* Announce arrival of local address to all FIBs. */
1842 rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt);
1849 * Find an IPv6 interface link-local address specific to an interface.
1850 * ifaddr is returned referenced.
1853 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1858 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1859 if (ifa->ifa_addr->sa_family != AF_INET6)
1861 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1862 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1869 IF_ADDR_UNLOCK(ifp);
1871 return ((struct in6_ifaddr *)ifa);
1876 * find the internet address corresponding to a given interface and address.
1877 * ifaddr is returned referenced.
1880 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1885 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1886 if (ifa->ifa_addr->sa_family != AF_INET6)
1888 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1893 IF_ADDR_UNLOCK(ifp);
1895 return ((struct in6_ifaddr *)ifa);
1899 * Convert IP6 address to printable (loggable) representation. Caller
1900 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1902 static char digits[] = "0123456789abcdef";
1904 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1908 const u_int16_t *a = (const u_int16_t *)addr;
1910 int dcolon = 0, zero = 0;
1914 for (i = 0; i < 8; i++) {
1925 if (dcolon == 0 && *(a + 1) == 0) {
1937 d = (const u_char *)a;
1938 /* Try to eliminate leading zeros in printout like in :0001. */
1940 *cp = digits[*d >> 4];
1945 *cp = digits[*d++ & 0xf];
1946 if (zero == 0 || (*cp != '0')) {
1950 *cp = digits[*d >> 4];
1951 if (zero == 0 || (*cp != '0')) {
1955 *cp++ = digits[*d & 0xf];
1964 in6_localaddr(struct in6_addr *in6)
1966 struct in6_ifaddr *ia;
1968 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1972 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1973 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1974 &ia->ia_prefixmask.sin6_addr)) {
1975 IN6_IFADDR_RUNLOCK();
1979 IN6_IFADDR_RUNLOCK();
1985 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1987 struct in6_ifaddr *ia;
1990 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1991 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1993 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1994 IN6_IFADDR_RUNLOCK();
1995 return (1); /* true */
1998 /* XXX: do we still have to go thru the rest of the list? */
2000 IN6_IFADDR_RUNLOCK();
2002 return (0); /* false */
2006 * return length of part which dst and src are equal
2010 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2013 u_char *s = (u_char *)src, *d = (u_char *)dst;
2014 u_char *lim = s + 16, r;
2017 if ((r = (*d++ ^ *s++)) != 0) {
2028 /* XXX: to be scope conscious */
2030 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2032 int bytelen, bitlen;
2035 if (0 > len || len > 128) {
2036 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2044 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2047 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2048 p2->s6_addr[bytelen] >> (8 - bitlen))
2055 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2057 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2058 int bytelen, bitlen, i;
2061 if (0 > len || len > 128) {
2062 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2067 bzero(maskp, sizeof(*maskp));
2070 for (i = 0; i < bytelen; i++)
2071 maskp->s6_addr[i] = 0xff;
2073 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2077 * return the best address out of the same scope. if no address was
2078 * found, return the first valid address from designated IF.
2081 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2083 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2085 struct in6_ifaddr *besta = 0;
2086 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2088 dep[0] = dep[1] = NULL;
2091 * We first look for addresses in the same scope.
2092 * If there is one, return it.
2093 * If two or more, return one which matches the dst longest.
2094 * If none, return one of global addresses assigned other ifs.
2097 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2098 if (ifa->ifa_addr->sa_family != AF_INET6)
2100 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2101 continue; /* XXX: is there any case to allow anycast? */
2102 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2103 continue; /* don't use this interface */
2104 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2106 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2107 if (V_ip6_use_deprecated)
2108 dep[0] = (struct in6_ifaddr *)ifa;
2112 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2114 * call in6_matchlen() as few as possible
2118 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2119 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2122 besta = (struct in6_ifaddr *)ifa;
2125 besta = (struct in6_ifaddr *)ifa;
2129 ifa_ref(&besta->ia_ifa);
2130 IF_ADDR_UNLOCK(ifp);
2134 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2135 if (ifa->ifa_addr->sa_family != AF_INET6)
2137 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2138 continue; /* XXX: is there any case to allow anycast? */
2139 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2140 continue; /* don't use this interface */
2141 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2143 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2144 if (V_ip6_use_deprecated)
2145 dep[1] = (struct in6_ifaddr *)ifa;
2151 IF_ADDR_UNLOCK(ifp);
2152 return (struct in6_ifaddr *)ifa;
2154 IF_ADDR_UNLOCK(ifp);
2156 /* use the last-resort values, that are, deprecated addresses */
2166 * perform DAD when interface becomes IFF_UP.
2169 in6_if_up(struct ifnet *ifp)
2172 struct in6_ifaddr *ia;
2175 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2176 if (ifa->ifa_addr->sa_family != AF_INET6)
2178 ia = (struct in6_ifaddr *)ifa;
2179 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2181 * The TENTATIVE flag was likely set by hand
2182 * beforehand, implicitly indicating the need for DAD.
2183 * We may be able to skip the random delay in this
2184 * case, but we impose delays just in case.
2187 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2190 IF_ADDR_UNLOCK(ifp);
2193 * special cases, like 6to4, are handled in in6_ifattach
2195 in6_ifattach(ifp, NULL);
2199 in6if_do_dad(struct ifnet *ifp)
2201 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2204 switch (ifp->if_type) {
2210 * These interfaces do not have the IFF_LOOPBACK flag,
2211 * but loop packets back. We do not have to do DAD on such
2212 * interfaces. We should even omit it, because loop-backed
2213 * NS would confuse the DAD procedure.
2218 * Our DAD routine requires the interface up and running.
2219 * However, some interfaces can be up before the RUNNING
2220 * status. Additionaly, users may try to assign addresses
2221 * before the interface becomes up (or running).
2222 * We simply skip DAD in such a case as a work around.
2223 * XXX: we should rather mark "tentative" on such addresses,
2224 * and do DAD after the interface becomes ready.
2226 if (!((ifp->if_flags & IFF_UP) &&
2227 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2235 * Calculate max IPv6 MTU through all the interfaces and store it
2241 unsigned long maxmtu = 0;
2244 IFNET_RLOCK_NOSLEEP();
2245 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
2246 ifp = TAILQ_NEXT(ifp, if_list)) {
2247 /* this function can be called during ifnet initialization */
2248 if (!ifp->if_afdata[AF_INET6])
2250 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2251 IN6_LINKMTU(ifp) > maxmtu)
2252 maxmtu = IN6_LINKMTU(ifp);
2254 IFNET_RUNLOCK_NOSLEEP();
2255 if (maxmtu) /* update only when maxmtu is positive */
2256 V_in6_maxmtu = maxmtu;
2260 * Provide the length of interface identifiers to be used for the link attached
2261 * to the given interface. The length should be defined in "IPv6 over
2262 * xxx-link" document. Note that address architecture might also define
2263 * the length for a particular set of address prefixes, regardless of the
2264 * link type. As clarified in rfc2462bis, those two definitions should be
2265 * consistent, and those really are as of August 2004.
2268 in6_if2idlen(struct ifnet *ifp)
2270 switch (ifp->if_type) {
2271 case IFT_ETHER: /* RFC2464 */
2272 #ifdef IFT_PROPVIRTUAL
2273 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2276 case IFT_L2VLAN: /* ditto */
2278 #ifdef IFT_IEEE80211
2279 case IFT_IEEE80211: /* ditto */
2282 case IFT_MIP: /* ditto */
2285 case IFT_FDDI: /* RFC2467 */
2287 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2289 case IFT_PPP: /* RFC2472 */
2291 case IFT_ARCNET: /* RFC2497 */
2293 case IFT_FRELAY: /* RFC2590 */
2295 case IFT_IEEE1394: /* RFC3146 */
2298 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2300 return (64); /* XXX: is this really correct? */
2303 * Unknown link type:
2304 * It might be controversial to use the today's common constant
2305 * of 64 for these cases unconditionally. For full compliance,
2306 * we should return an error in this case. On the other hand,
2307 * if we simply miss the standard for the link type or a new
2308 * standard is defined for a new link type, the IFID length
2309 * is very likely to be the common constant. As a compromise,
2310 * we always use the constant, but make an explicit notice
2311 * indicating the "unknown" case.
2313 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2318 #include <sys/sysctl.h>
2320 struct in6_llentry {
2321 struct llentry base;
2322 struct sockaddr_in6 l3_addr6;
2325 static struct llentry *
2326 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2328 struct in6_llentry *lle;
2330 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2331 M_DONTWAIT | M_ZERO);
2332 if (lle == NULL) /* NB: caller generates msg */
2335 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2336 lle->base.lle_refcnt = 1;
2337 LLE_LOCK_INIT(&lle->base);
2338 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2339 CALLOUT_RETURNUNLOCKED);
2345 * Deletes an address from the address table.
2346 * This function is called by the timer functions
2347 * such as arptimer() and nd6_llinfo_timer(), and
2348 * the caller does the locking.
2351 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2354 LLE_LOCK_DESTROY(lle);
2355 free(lle, M_LLTABLE);
2359 in6_lltable_prefix_free(struct lltable *llt,
2360 const struct sockaddr *prefix,
2361 const struct sockaddr *mask,
2364 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2365 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2366 struct llentry *lle, *next;
2370 * (flags & LLE_STATIC) means deleting all entries
2371 * including static ND6 entries
2373 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2374 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2375 if (IN6_ARE_MASKED_ADDR_EQUAL(
2376 &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2379 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) {
2382 canceled = callout_drain(&lle->la_timer);
2393 in6_lltable_rtcheck(struct ifnet *ifp,
2395 const struct sockaddr *l3addr)
2398 char ip6buf[INET6_ADDRSTRLEN];
2400 KASSERT(l3addr->sa_family == AF_INET6,
2401 ("sin_family %d", l3addr->sa_family));
2403 /* Our local addresses are always only installed on the default FIB. */
2404 /* XXX rtalloc1 should take a const param */
2405 rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0,
2407 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2410 * Create an ND6 cache for an IPv6 neighbor
2411 * that is not covered by our own prefix.
2413 /* XXX ifaof_ifpforaddr should take a const param */
2414 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2421 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2422 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2431 static struct llentry *
2432 in6_lltable_lookup(struct lltable *llt, u_int flags,
2433 const struct sockaddr *l3addr)
2435 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2436 struct ifnet *ifp = llt->llt_ifp;
2437 struct llentry *lle;
2438 struct llentries *lleh;
2441 IF_AFDATA_LOCK_ASSERT(ifp);
2442 KASSERT(l3addr->sa_family == AF_INET6,
2443 ("sin_family %d", l3addr->sa_family));
2445 hashkey = sin6->sin6_addr.s6_addr32[3];
2446 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2447 LIST_FOREACH(lle, lleh, lle_next) {
2448 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2449 if (lle->la_flags & LLE_DELETED)
2451 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2452 sizeof(struct in6_addr)) == 0)
2457 if (!(flags & LLE_CREATE))
2460 * A route that covers the given address must have
2461 * been installed 1st because we are doing a resolution,
2464 if (!(flags & LLE_IFADDR) &&
2465 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2468 lle = in6_lltable_new(l3addr, flags);
2470 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2473 lle->la_flags = flags & ~LLE_CREATE;
2474 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2475 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2476 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2480 lle->lle_head = lleh;
2481 LIST_INSERT_HEAD(lleh, lle, lle_next);
2482 } else if (flags & LLE_DELETE) {
2483 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2485 lle->la_flags = LLE_DELETED;
2488 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2493 if (LLE_IS_VALID(lle)) {
2494 if (flags & LLE_EXCLUSIVE)
2503 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2505 struct ifnet *ifp = llt->llt_ifp;
2506 struct llentry *lle;
2509 struct rt_msghdr rtm;
2510 struct sockaddr_in6 sin6;
2512 * ndp.c assumes that sdl is word aligned
2517 struct sockaddr_dl sdl;
2521 if (ifp->if_flags & IFF_LOOPBACK)
2524 LLTABLE_LOCK_ASSERT();
2527 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2528 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2529 struct sockaddr_dl *sdl;
2531 /* skip deleted or invalid entries */
2532 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2534 /* Skip if jailed and not a valid IP of the prison. */
2535 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2538 * produce a msg made of:
2540 * struct sockaddr_in6 (IPv6)
2541 * struct sockaddr_dl;
2543 bzero(&ndpc, sizeof(ndpc));
2544 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2545 ndpc.rtm.rtm_version = RTM_VERSION;
2546 ndpc.rtm.rtm_type = RTM_GET;
2547 ndpc.rtm.rtm_flags = RTF_UP;
2548 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2549 ndpc.sin6.sin6_family = AF_INET6;
2550 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2551 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2554 if (lle->la_flags & LLE_PUB)
2555 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2558 sdl->sdl_family = AF_LINK;
2559 sdl->sdl_len = sizeof(*sdl);
2560 sdl->sdl_alen = ifp->if_addrlen;
2561 sdl->sdl_index = ifp->if_index;
2562 sdl->sdl_type = ifp->if_type;
2563 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2564 ndpc.rtm.rtm_rmx.rmx_expire =
2565 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2566 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2567 if (lle->la_flags & LLE_STATIC)
2568 ndpc.rtm.rtm_flags |= RTF_STATIC;
2569 ndpc.rtm.rtm_index = ifp->if_index;
2570 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2579 in6_domifattach(struct ifnet *ifp)
2581 struct in6_ifextra *ext;
2583 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2584 bzero(ext, sizeof(*ext));
2586 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2587 M_IFADDR, M_WAITOK);
2588 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2591 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2592 M_IFADDR, M_WAITOK);
2593 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2595 ext->nd_ifinfo = nd6_ifattach(ifp);
2596 ext->scope6_id = scope6_ifattach(ifp);
2597 ext->lltable = lltable_init(ifp, AF_INET6);
2598 if (ext->lltable != NULL) {
2599 ext->lltable->llt_free = in6_lltable_free;
2600 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2601 ext->lltable->llt_lookup = in6_lltable_lookup;
2602 ext->lltable->llt_dump = in6_lltable_dump;
2605 ext->mld_ifinfo = mld_domifattach(ifp);
2611 in6_domifdetach(struct ifnet *ifp, void *aux)
2613 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2615 mld_domifdetach(ifp);
2616 scope6_ifdetach(ext->scope6_id);
2617 nd6_ifdetach(ext->nd_ifinfo);
2618 lltable_free(ext->lltable);
2619 free(ext->in6_ifstat, M_IFADDR);
2620 free(ext->icmp6_ifstat, M_IFADDR);
2621 free(ext, M_IFADDR);
2625 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2626 * v4 mapped addr or v4 compat addr
2629 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2632 bzero(sin, sizeof(*sin));
2633 sin->sin_len = sizeof(struct sockaddr_in);
2634 sin->sin_family = AF_INET;
2635 sin->sin_port = sin6->sin6_port;
2636 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2639 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2641 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2643 bzero(sin6, sizeof(*sin6));
2644 sin6->sin6_len = sizeof(struct sockaddr_in6);
2645 sin6->sin6_family = AF_INET6;
2646 sin6->sin6_port = sin->sin_port;
2647 sin6->sin6_addr.s6_addr32[0] = 0;
2648 sin6->sin6_addr.s6_addr32[1] = 0;
2649 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2650 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2653 /* Convert sockaddr_in6 into sockaddr_in. */
2655 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2657 struct sockaddr_in *sin_p;
2658 struct sockaddr_in6 sin6;
2661 * Save original sockaddr_in6 addr and convert it
2664 sin6 = *(struct sockaddr_in6 *)nam;
2665 sin_p = (struct sockaddr_in *)nam;
2666 in6_sin6_2_sin(sin_p, &sin6);
2669 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2671 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2673 struct sockaddr_in *sin_p;
2674 struct sockaddr_in6 *sin6_p;
2676 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2678 sin_p = (struct sockaddr_in *)*nam;
2679 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2680 free(*nam, M_SONAME);
2681 *nam = (struct sockaddr *)sin6_p;