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$");
67 #include "opt_inet6.h"
69 #include <sys/param.h>
70 #include <sys/errno.h>
72 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/syslog.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86 #include <net/if_dl.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <net/if_llatbl.h>
92 #include <netinet/if_ether.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet6/mld6_var.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_ifattach.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet6/in6_pcb.h>
107 * Definitions of some costant IP6 addresses.
109 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
110 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
111 const struct in6_addr in6addr_nodelocal_allnodes =
112 IN6ADDR_NODELOCAL_ALLNODES_INIT;
113 const struct in6_addr in6addr_linklocal_allnodes =
114 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
115 const struct in6_addr in6addr_linklocal_allrouters =
116 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
117 const struct in6_addr in6addr_linklocal_allv2routers =
118 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
120 const struct in6_addr in6mask0 = IN6MASK0;
121 const struct in6_addr in6mask32 = IN6MASK32;
122 const struct in6_addr in6mask64 = IN6MASK64;
123 const struct in6_addr in6mask96 = IN6MASK96;
124 const struct in6_addr in6mask128 = IN6MASK128;
126 const struct sockaddr_in6 sa6_any =
127 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
129 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
130 struct ifnet *, struct thread *));
131 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
132 struct sockaddr_in6 *, int));
133 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
135 int (*faithprefix_p)(struct in6_addr *);
140 in6_mask2len(struct in6_addr *mask, u_char *lim0)
143 u_char *lim = lim0, *p;
145 /* ignore the scope_id part */
146 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
147 lim = (u_char *)mask + sizeof(*mask);
148 for (p = (u_char *)mask; p < lim; x++, p++) {
154 for (y = 0; y < 8; y++) {
155 if ((*p & (0x80 >> y)) == 0)
161 * when the limit pointer is given, do a stricter check on the
165 if (y != 0 && (*p & (0x00ff >> y)) != 0)
167 for (p = p + 1; p < lim; p++)
175 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
176 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
179 in6_control(struct socket *so, u_long cmd, caddr_t data,
180 struct ifnet *ifp, struct thread *td)
182 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
183 struct in6_ifaddr *ia = NULL;
184 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
185 struct sockaddr_in6 *sa6;
189 case SIOCGETSGCNT_IN6:
190 case SIOCGETMIFCNT_IN6:
191 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
195 case SIOCAADDRCTL_POLICY:
196 case SIOCDADDRCTL_POLICY:
198 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
202 return (in6_src_ioctl(cmd, data));
209 case SIOCSNDFLUSH_IN6:
210 case SIOCSPFXFLUSH_IN6:
211 case SIOCSRTRFLUSH_IN6:
212 case SIOCSDEFIFACE_IN6:
213 case SIOCSIFINFO_FLAGS:
214 case SIOCSIFINFO_IN6:
216 error = priv_check(td, PRIV_NETINET_ND6);
221 case OSIOCGIFINFO_IN6:
222 case SIOCGIFINFO_IN6:
225 case SIOCGNBRINFO_IN6:
226 case SIOCGDEFIFACE_IN6:
227 return (nd6_ioctl(cmd, data, ifp));
231 case SIOCSIFPREFIX_IN6:
232 case SIOCDIFPREFIX_IN6:
233 case SIOCAIFPREFIX_IN6:
234 case SIOCCIFPREFIX_IN6:
235 case SIOCSGIFPREFIX_IN6:
236 case SIOCGIFPREFIX_IN6:
238 "prefix ioctls are now invalidated. "
239 "please use ifconfig.\n");
246 error = priv_check(td, PRIV_NETINET_SCOPE6);
250 return (scope6_set(ifp,
251 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
253 return (scope6_get(ifp,
254 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
256 return (scope6_get_default((struct scope6_id *)
257 ifr->ifr_ifru.ifru_scope_id));
263 error = priv_check(td, PRIV_NET_ADDIFADDR);
267 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
271 error = priv_check(td, PRIV_NET_DELIFADDR);
277 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
281 * Find address for this interface, if it exists.
283 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
284 * only, and used the first interface address as the target of other
285 * operations (without checking ifra_addr). This was because netinet
286 * code/API assumed at most 1 interface address per interface.
287 * Since IPv6 allows a node to assign multiple addresses
288 * on a single interface, we almost always look and check the
289 * presence of ifra_addr, and reject invalid ones here.
290 * It also decreases duplicated code among SIOC*_IN6 operations.
293 case SIOCAIFADDR_IN6:
294 case SIOCSIFPHYADDR_IN6:
295 sa6 = &ifra->ifra_addr;
297 case SIOCSIFADDR_IN6:
298 case SIOCGIFADDR_IN6:
299 case SIOCSIFDSTADDR_IN6:
300 case SIOCSIFNETMASK_IN6:
301 case SIOCGIFDSTADDR_IN6:
302 case SIOCGIFNETMASK_IN6:
303 case SIOCDIFADDR_IN6:
304 case SIOCGIFPSRCADDR_IN6:
305 case SIOCGIFPDSTADDR_IN6:
306 case SIOCGIFAFLAG_IN6:
307 case SIOCSNDFLUSH_IN6:
308 case SIOCSPFXFLUSH_IN6:
309 case SIOCSRTRFLUSH_IN6:
310 case SIOCGIFALIFETIME_IN6:
311 case SIOCSIFALIFETIME_IN6:
312 case SIOCGIFSTAT_IN6:
313 case SIOCGIFSTAT_ICMP6:
314 sa6 = &ifr->ifr_addr;
320 if (sa6 && sa6->sin6_family == AF_INET6) {
321 if (sa6->sin6_scope_id != 0)
322 error = sa6_embedscope(sa6, 0);
324 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
327 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
328 &sa6->sin6_addr)) != 0)
330 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
335 case SIOCSIFADDR_IN6:
336 case SIOCSIFDSTADDR_IN6:
337 case SIOCSIFNETMASK_IN6:
339 * Since IPv6 allows a node to assign multiple addresses
340 * on a single interface, SIOCSIFxxx ioctls are deprecated.
342 /* we decided to obsolete this command (20000704) */
346 case SIOCDIFADDR_IN6:
348 * for IPv4, we look for existing in_ifaddr here to allow
349 * "ifconfig if0 delete" to remove the first IPv4 address on
350 * the interface. For IPv6, as the spec allows multiple
351 * interface address from the day one, we consider "remove the
352 * first one" semantics to be not preferable.
355 error = EADDRNOTAVAIL;
359 case SIOCAIFADDR_IN6:
361 * We always require users to specify a valid IPv6 address for
362 * the corresponding operation.
364 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
365 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
366 error = EAFNOSUPPORT;
371 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
372 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
378 case SIOCGIFADDR_IN6:
379 /* This interface is basically deprecated. use SIOCGIFCONF. */
381 case SIOCGIFAFLAG_IN6:
382 case SIOCGIFNETMASK_IN6:
383 case SIOCGIFDSTADDR_IN6:
384 case SIOCGIFALIFETIME_IN6:
385 /* must think again about its semantics */
387 error = EADDRNOTAVAIL;
392 case SIOCSIFALIFETIME_IN6:
394 struct in6_addrlifetime *lt;
397 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
402 error = EADDRNOTAVAIL;
405 /* sanity for overflow - beware unsigned */
406 lt = &ifr->ifr_ifru.ifru_lifetime;
407 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
408 lt->ia6t_vltime + time_second < time_second) {
412 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
413 lt->ia6t_pltime + time_second < time_second) {
422 case SIOCGIFADDR_IN6:
423 ifr->ifr_addr = ia->ia_addr;
424 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
428 case SIOCGIFDSTADDR_IN6:
429 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
434 * XXX: should we check if ifa_dstaddr is NULL and return
437 ifr->ifr_dstaddr = ia->ia_dstaddr;
438 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
442 case SIOCGIFNETMASK_IN6:
443 ifr->ifr_addr = ia->ia_prefixmask;
446 case SIOCGIFAFLAG_IN6:
447 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
450 case SIOCGIFSTAT_IN6:
455 bzero(&ifr->ifr_ifru.ifru_stat,
456 sizeof(ifr->ifr_ifru.ifru_stat));
457 ifr->ifr_ifru.ifru_stat =
458 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
461 case SIOCGIFSTAT_ICMP6:
466 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
467 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
468 ifr->ifr_ifru.ifru_icmp6stat =
469 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
472 case SIOCGIFALIFETIME_IN6:
473 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
474 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
476 struct in6_addrlifetime *retlt =
477 &ifr->ifr_ifru.ifru_lifetime;
480 * XXX: adjust expiration time assuming time_t is
484 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
485 if (ia->ia6_lifetime.ia6t_vltime <
486 maxexpire - ia->ia6_updatetime) {
487 retlt->ia6t_expire = ia->ia6_updatetime +
488 ia->ia6_lifetime.ia6t_vltime;
490 retlt->ia6t_expire = maxexpire;
492 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
494 struct in6_addrlifetime *retlt =
495 &ifr->ifr_ifru.ifru_lifetime;
498 * XXX: adjust expiration time assuming time_t is
502 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
503 if (ia->ia6_lifetime.ia6t_pltime <
504 maxexpire - ia->ia6_updatetime) {
505 retlt->ia6t_preferred = ia->ia6_updatetime +
506 ia->ia6_lifetime.ia6t_pltime;
508 retlt->ia6t_preferred = maxexpire;
512 case SIOCSIFALIFETIME_IN6:
513 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
515 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
516 ia->ia6_lifetime.ia6t_expire =
517 time_second + ia->ia6_lifetime.ia6t_vltime;
519 ia->ia6_lifetime.ia6t_expire = 0;
520 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
521 ia->ia6_lifetime.ia6t_preferred =
522 time_second + ia->ia6_lifetime.ia6t_pltime;
524 ia->ia6_lifetime.ia6t_preferred = 0;
527 case SIOCAIFADDR_IN6:
530 struct nd_prefixctl pr0;
531 struct nd_prefix *pr;
534 * first, make or update the interface address structure,
535 * and link it to the list.
537 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
540 ifa_free(&ia->ia_ifa);
541 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
544 * this can happen when the user specify the 0 valid
551 * then, make the prefix on-link on the interface.
552 * XXX: we'd rather create the prefix before the address, but
553 * we need at least one address to install the corresponding
554 * interface route, so we configure the address first.
558 * convert mask to prefix length (prefixmask has already
559 * been validated in in6_update_ifa().
561 bzero(&pr0, sizeof(pr0));
563 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
565 if (pr0.ndpr_plen == 128) {
566 break; /* we don't need to install a host route. */
568 pr0.ndpr_prefix = ifra->ifra_addr;
569 /* apply the mask for safety. */
570 for (i = 0; i < 4; i++) {
571 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
572 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
575 * XXX: since we don't have an API to set prefix (not address)
576 * lifetimes, we just use the same lifetimes as addresses.
577 * The (temporarily) installed lifetimes can be overridden by
578 * later advertised RAs (when accept_rtadv is non 0), which is
579 * an intended behavior.
581 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
583 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
584 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
585 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
587 /* add the prefix if not yet. */
588 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
590 * nd6_prelist_add will install the corresponding
593 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
596 log(LOG_ERR, "nd6_prelist_add succeeded but "
603 /* relate the address to the prefix */
604 if (ia->ia6_ndpr == NULL) {
609 * If this is the first autoconf address from the
610 * prefix, create a temporary address as well
613 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
614 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
616 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
617 log(LOG_NOTICE, "in6_control: failed "
618 "to create a temporary address, "
625 * this might affect the status of autoconfigured addresses,
626 * that is, this address might make other addresses detached.
628 pfxlist_onlink_check();
629 if (error == 0 && ia)
630 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
634 case SIOCDIFADDR_IN6:
636 struct nd_prefix *pr;
639 * If the address being deleted is the only one that owns
640 * the corresponding prefix, expire the prefix as well.
641 * XXX: theoretically, we don't have to worry about such
642 * relationship, since we separate the address management
643 * and the prefix management. We do this, however, to provide
644 * as much backward compatibility as possible in terms of
645 * the ioctl operation.
646 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
649 in6_purgeaddr(&ia->ia_ifa);
650 if (pr && pr->ndpr_refcnt == 0)
652 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
657 if (ifp == NULL || ifp->if_ioctl == 0) {
661 error = (*ifp->if_ioctl)(ifp, cmd, data);
668 ifa_free(&ia->ia_ifa);
673 * Update parameters of an IPv6 interface address.
674 * If necessary, a new entry is created and linked into address chains.
675 * This function is separated from in6_control().
676 * XXX: should this be performed under splnet()?
679 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
680 struct in6_ifaddr *ia, int flags)
682 int error = 0, hostIsNew = 0, plen = -1;
683 struct sockaddr_in6 dst6;
684 struct in6_addrlifetime *lt;
685 struct in6_multi_mship *imm;
686 struct in6_multi *in6m_sol;
689 char ip6buf[INET6_ADDRSTRLEN];
691 /* Validate parameters */
692 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
696 * The destination address for a p2p link must have a family
697 * of AF_UNSPEC or AF_INET6.
699 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
700 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
701 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
702 return (EAFNOSUPPORT);
704 * validate ifra_prefixmask. don't check sin6_family, netmask
705 * does not carry fields other than sin6_len.
707 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
710 * Because the IPv6 address architecture is classless, we require
711 * users to specify a (non 0) prefix length (mask) for a new address.
712 * We also require the prefix (when specified) mask is valid, and thus
713 * reject a non-consecutive mask.
715 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
717 if (ifra->ifra_prefixmask.sin6_len != 0) {
718 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
719 (u_char *)&ifra->ifra_prefixmask +
720 ifra->ifra_prefixmask.sin6_len);
725 * In this case, ia must not be NULL. We just use its prefix
728 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
731 * If the destination address on a p2p interface is specified,
732 * and the address is a scoped one, validate/set the scope
735 dst6 = ifra->ifra_dstaddr;
736 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
737 (dst6.sin6_family == AF_INET6)) {
738 struct in6_addr in6_tmp;
741 in6_tmp = dst6.sin6_addr;
742 if (in6_setscope(&in6_tmp, ifp, &zoneid))
743 return (EINVAL); /* XXX: should be impossible */
745 if (dst6.sin6_scope_id != 0) {
746 if (dst6.sin6_scope_id != zoneid)
748 } else /* user omit to specify the ID. */
749 dst6.sin6_scope_id = zoneid;
751 /* convert into the internal form */
752 if (sa6_embedscope(&dst6, 0))
753 return (EINVAL); /* XXX: should be impossible */
756 * The destination address can be specified only for a p2p or a
757 * loopback interface. If specified, the corresponding prefix length
760 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
761 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
762 /* XXX: noisy message */
763 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
764 "be specified for a p2p or a loopback IF only\n"));
768 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
769 "be 128 when dstaddr is specified\n"));
773 /* lifetime consistency check */
774 lt = &ifra->ifra_lifetime;
775 if (lt->ia6t_pltime > lt->ia6t_vltime)
777 if (lt->ia6t_vltime == 0) {
779 * the following log might be noisy, but this is a typical
780 * configuration mistake or a tool's bug.
783 "in6_update_ifa: valid lifetime is 0 for %s\n",
784 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
787 return (0); /* there's nothing to do */
791 * If this is a new address, allocate a new ifaddr and link it
797 * When in6_update_ifa() is called in a process of a received
798 * RA, it is called under an interrupt context. So, we should
799 * call malloc with M_NOWAIT.
801 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
805 bzero((caddr_t)ia, sizeof(*ia));
806 ifa_init(&ia->ia_ifa);
807 LIST_INIT(&ia->ia6_memberships);
808 /* Initialize the address and masks, and put time stamp */
809 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
810 ia->ia_addr.sin6_family = AF_INET6;
811 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
812 ia->ia6_createtime = time_second;
813 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
815 * XXX: some functions expect that ifa_dstaddr is not
816 * NULL for p2p interfaces.
818 ia->ia_ifa.ifa_dstaddr =
819 (struct sockaddr *)&ia->ia_dstaddr;
821 ia->ia_ifa.ifa_dstaddr = NULL;
823 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
825 ifa_ref(&ia->ia_ifa); /* if_addrhead */
827 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
830 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
832 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
833 IN6_IFADDR_WUNLOCK();
836 /* update timestamp */
837 ia->ia6_updatetime = time_second;
839 /* set prefix mask */
840 if (ifra->ifra_prefixmask.sin6_len) {
842 * We prohibit changing the prefix length of an existing
844 * + such an operation should be rare in IPv6, and
845 * + the operation would confuse prefix management.
847 if (ia->ia_prefixmask.sin6_len &&
848 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
849 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
850 " existing (%s) address should not be changed\n",
851 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
855 ia->ia_prefixmask = ifra->ifra_prefixmask;
859 * If a new destination address is specified, scrub the old one and
860 * install the new destination. Note that the interface must be
861 * p2p or loopback (see the check above.)
863 if (dst6.sin6_family == AF_INET6 &&
864 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
867 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
868 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
869 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
870 "a route to the old destination: %s\n",
871 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
872 /* proceed anyway... */
874 ia->ia_flags &= ~IFA_ROUTE;
875 ia->ia_dstaddr = dst6;
879 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
880 * to see if the address is deprecated or invalidated, but initialize
881 * these members for applications.
883 ia->ia6_lifetime = ifra->ifra_lifetime;
884 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
885 ia->ia6_lifetime.ia6t_expire =
886 time_second + ia->ia6_lifetime.ia6t_vltime;
888 ia->ia6_lifetime.ia6t_expire = 0;
889 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
890 ia->ia6_lifetime.ia6t_preferred =
891 time_second + ia->ia6_lifetime.ia6t_pltime;
893 ia->ia6_lifetime.ia6t_preferred = 0;
895 /* reset the interface and routing table appropriately. */
896 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
900 * configure address flags.
902 ia->ia6_flags = ifra->ifra_flags;
904 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
905 * userland, make it deprecated.
907 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
908 ia->ia6_lifetime.ia6t_pltime = 0;
909 ia->ia6_lifetime.ia6t_preferred = time_second;
912 * Make the address tentative before joining multicast addresses,
913 * so that corresponding MLD responses would not have a tentative
916 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
917 if (hostIsNew && in6if_do_dad(ifp))
918 ia->ia6_flags |= IN6_IFF_TENTATIVE;
921 * We are done if we have simply modified an existing address.
927 * Beyond this point, we should call in6_purgeaddr upon an error,
928 * not just go to unlink.
931 /* Join necessary multicast groups */
933 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
934 struct sockaddr_in6 mltaddr, mltmask;
935 struct in6_addr llsol;
937 /* join solicited multicast addr for new host id */
938 bzero(&llsol, sizeof(struct in6_addr));
939 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
940 llsol.s6_addr32[1] = 0;
941 llsol.s6_addr32[2] = htonl(1);
942 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
943 llsol.s6_addr8[12] = 0xff;
944 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
945 /* XXX: should not happen */
946 log(LOG_ERR, "in6_update_ifa: "
947 "in6_setscope failed\n");
951 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
953 * We need a random delay for DAD on the address
954 * being configured. It also means delaying
955 * transmission of the corresponding MLD report to
956 * avoid report collision.
957 * [draft-ietf-ipv6-rfc2462bis-02.txt]
959 delay = arc4random() %
960 (MAX_RTR_SOLICITATION_DELAY * hz);
962 imm = in6_joingroup(ifp, &llsol, &error, delay);
965 "in6_update_ifa: addmulti failed for "
966 "%s on %s (errno=%d)\n",
967 ip6_sprintf(ip6buf, &llsol), if_name(ifp),
971 LIST_INSERT_HEAD(&ia->ia6_memberships,
973 in6m_sol = imm->i6mm_maddr;
975 bzero(&mltmask, sizeof(mltmask));
976 mltmask.sin6_len = sizeof(struct sockaddr_in6);
977 mltmask.sin6_family = AF_INET6;
978 mltmask.sin6_addr = in6mask32;
979 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
982 * join link-local all-nodes address
984 bzero(&mltaddr, sizeof(mltaddr));
985 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
986 mltaddr.sin6_family = AF_INET6;
987 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
988 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
990 goto cleanup; /* XXX: should not fail */
993 * XXX: do we really need this automatic routes?
994 * We should probably reconsider this stuff. Most applications
995 * actually do not need the routes, since they usually specify
996 * the outgoing interface.
998 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1000 /* XXX: only works in !SCOPEDROUTING case. */
1001 if (memcmp(&mltaddr.sin6_addr,
1002 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1009 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1010 (struct sockaddr *)&ia->ia_addr,
1011 (struct sockaddr *)&mltmask, RTF_UP,
1012 (struct rtentry **)0);
1019 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1021 nd6log((LOG_WARNING,
1022 "in6_update_ifa: addmulti failed for "
1023 "%s on %s (errno=%d)\n",
1024 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1025 if_name(ifp), error));
1028 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1031 * join node information group address
1034 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1036 * The spec doesn't say anything about delay for this
1037 * group, but the same logic should apply.
1039 delay = arc4random() %
1040 (MAX_RTR_SOLICITATION_DELAY * hz);
1042 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
1043 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1044 delay); /* XXX jinmei */
1046 nd6log((LOG_WARNING, "in6_update_ifa: "
1047 "addmulti failed for %s on %s "
1049 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1050 if_name(ifp), error));
1051 /* XXX not very fatal, go on... */
1053 LIST_INSERT_HEAD(&ia->ia6_memberships,
1059 * join interface-local all-nodes address.
1060 * (ff01::1%ifN, and ff01::%ifN/32)
1062 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1063 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1065 goto cleanup; /* XXX: should not fail */
1066 /* XXX: again, do we really need the route? */
1067 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1069 if (memcmp(&mltaddr.sin6_addr,
1070 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1077 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1078 (struct sockaddr *)&ia->ia_addr,
1079 (struct sockaddr *)&mltmask, RTF_UP,
1080 (struct rtentry **)0);
1086 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1088 nd6log((LOG_WARNING, "in6_update_ifa: "
1089 "addmulti failed for %s on %s "
1091 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1092 if_name(ifp), error));
1095 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1100 * Perform DAD, if needed.
1101 * XXX It may be of use, if we can administratively
1104 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1105 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1107 int mindelay, maxdelay;
1110 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1112 * We need to impose a delay before sending an NS
1113 * for DAD. Check if we also needed a delay for the
1114 * corresponding MLD message. If we did, the delay
1115 * should be larger than the MLD delay (this could be
1116 * relaxed a bit, but this simple logic is at least
1118 * XXX: Break data hiding guidelines and look at
1119 * state for the solicited multicast group.
1122 if (in6m_sol != NULL &&
1123 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1124 mindelay = in6m_sol->in6m_timer;
1126 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1127 if (maxdelay - mindelay == 0)
1131 (arc4random() % (maxdelay - mindelay)) +
1135 nd6_dad_start((struct ifaddr *)ia, delay);
1138 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1139 ifa_free(&ia->ia_ifa);
1144 * XXX: if a change of an existing address failed, keep the entry
1148 in6_unlink_ifa(ia, ifp);
1149 ifa_free(&ia->ia_ifa);
1154 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1155 ifa_free(&ia->ia_ifa);
1156 in6_purgeaddr(&ia->ia_ifa);
1161 in6_purgeaddr(struct ifaddr *ifa)
1163 struct ifnet *ifp = ifa->ifa_ifp;
1164 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1165 struct in6_multi_mship *imm;
1166 struct sockaddr_in6 mltaddr, mltmask;
1168 struct sockaddr_dl gateway;
1169 struct sockaddr_in6 mask, addr;
1172 struct ifaddr *ifa0, *nifa;
1175 * find another IPv6 address as the gateway for the
1176 * link-local and node-local all-nodes multicast
1180 TAILQ_FOREACH_SAFE(ifa0, &ifp->if_addrhead, ifa_link, nifa) {
1181 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1182 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1183 &ia->ia_addr.sin6_addr,
1184 sizeof(struct in6_addr)) == 0)
1191 IF_ADDR_UNLOCK(ifp);
1194 * Remove the loopback route to the interface address.
1195 * The check for the current setting of "nd6_useloopback" is not needed.
1197 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK)) {
1198 struct rt_addrinfo info;
1199 struct sockaddr_dl null_sdl;
1201 bzero(&null_sdl, sizeof(null_sdl));
1202 null_sdl.sdl_len = sizeof(null_sdl);
1203 null_sdl.sdl_family = AF_LINK;
1204 null_sdl.sdl_type = V_loif->if_type;
1205 null_sdl.sdl_index = V_loif->if_index;
1206 bzero(&info, sizeof(info));
1207 info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC;
1208 info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr;
1209 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1210 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1213 log(LOG_INFO, "in6_purgeaddr: deletion failed\n");
1216 /* stop DAD processing */
1219 IF_AFDATA_LOCK(ifp);
1220 lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
1221 (struct sockaddr *)&ia->ia_addr);
1222 IF_AFDATA_UNLOCK(ifp);
1225 * initialize for rtmsg generation
1227 bzero(&gateway, sizeof(gateway));
1228 gateway.sdl_len = sizeof(gateway);
1229 gateway.sdl_family = AF_LINK;
1230 gateway.sdl_nlen = 0;
1231 gateway.sdl_alen = ifp->if_addrlen;
1233 bzero(&rt0, sizeof(rt0));
1234 rt0.rt_gateway = (struct sockaddr *)&gateway;
1235 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1236 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1237 rt_mask(&rt0) = (struct sockaddr *)&mask;
1238 rt_key(&rt0) = (struct sockaddr *)&addr;
1239 rt0.rt_flags = RTF_HOST | RTF_STATIC;
1240 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
1243 * leave from multicast groups we have joined for the interface
1245 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1246 LIST_REMOVE(imm, i6mm_chain);
1247 in6_leavegroup(imm);
1251 * remove the link-local all-nodes address
1253 bzero(&mltmask, sizeof(mltmask));
1254 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1255 mltmask.sin6_family = AF_INET6;
1256 mltmask.sin6_addr = in6mask32;
1258 bzero(&mltaddr, sizeof(mltaddr));
1259 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1260 mltaddr.sin6_family = AF_INET6;
1261 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1263 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1267 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1268 if (rt != NULL && rt->rt_gateway != NULL &&
1269 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1270 &ia->ia_addr.sin6_addr,
1271 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1273 * if no more IPv6 address exists on this interface
1274 * then remove the multicast address route
1277 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1278 sizeof(mltaddr.sin6_addr));
1280 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1281 (struct sockaddr *)&ia->ia_addr,
1282 (struct sockaddr *)&mltmask, RTF_UP,
1283 (struct rtentry **)0);
1285 log(LOG_INFO, "in6_purgeaddr: link-local all-nodes"
1286 "multicast address deletion error\n");
1289 * replace the gateway of the route
1291 struct sockaddr_in6 sa;
1293 bzero(&sa, sizeof(sa));
1294 sa.sin6_len = sizeof(struct sockaddr_in6);
1295 sa.sin6_family = AF_INET6;
1296 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1297 sizeof(sa.sin6_addr));
1298 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1299 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1308 * remove the node-local all-nodes address
1310 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1311 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1315 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1316 if (rt != NULL && rt->rt_gateway != NULL &&
1317 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1318 &ia->ia_addr.sin6_addr,
1319 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1321 * if no more IPv6 address exists on this interface
1322 * then remove the multicast address route
1325 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1326 sizeof(mltaddr.sin6_addr));
1329 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1330 (struct sockaddr *)&ia->ia_addr,
1331 (struct sockaddr *)&mltmask, RTF_UP,
1332 (struct rtentry **)0);
1335 log(LOG_INFO, "in6_purgeaddr: node-local all-nodes"
1336 "multicast address deletion error\n");
1339 * replace the gateway of the route
1341 struct sockaddr_in6 sa;
1343 bzero(&sa, sizeof(sa));
1344 sa.sin6_len = sizeof(struct sockaddr_in6);
1345 sa.sin6_family = AF_INET6;
1346 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1347 sizeof(sa.sin6_addr));
1348 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1349 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1359 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1360 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1362 struct sockaddr *dstaddr;
1365 * use the interface address if configuring an
1366 * interface address with a /128 prefix len
1368 if (ia->ia_dstaddr.sin6_family == AF_INET6)
1369 dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1371 dstaddr = (struct sockaddr *)&ia->ia_addr;
1373 error = rtrequest(RTM_DELETE,
1374 (struct sockaddr *)dstaddr,
1375 (struct sockaddr *)&ia->ia_addr,
1376 (struct sockaddr *)&ia->ia_prefixmask,
1377 ia->ia_flags | RTF_HOST, NULL);
1380 ia->ia_flags &= ~IFA_ROUTE;
1385 in6_unlink_ifa(ia, ifp);
1389 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1394 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1395 IF_ADDR_UNLOCK(ifp);
1396 ifa_free(&ia->ia_ifa); /* if_addrhead */
1399 * Defer the release of what might be the last reference to the
1400 * in6_ifaddr so that it can't be freed before the remainder of the
1404 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1405 IN6_IFADDR_WUNLOCK();
1408 * Release the reference to the base prefix. There should be a
1409 * positive reference.
1411 if (ia->ia6_ndpr == NULL) {
1413 "in6_unlink_ifa: autoconf'ed address "
1414 "%p has no prefix\n", ia));
1416 ia->ia6_ndpr->ndpr_refcnt--;
1417 ia->ia6_ndpr = NULL;
1421 * Also, if the address being removed is autoconf'ed, call
1422 * pfxlist_onlink_check() since the release might affect the status of
1423 * other (detached) addresses.
1425 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1426 pfxlist_onlink_check();
1428 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1433 in6_purgeif(struct ifnet *ifp)
1435 struct ifaddr *ifa, *nifa;
1437 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1438 if (ifa->ifa_addr->sa_family != AF_INET6)
1448 * SIOCGLIFADDR: get first address. (?)
1449 * SIOCGLIFADDR with IFLR_PREFIX:
1450 * get first address that matches the specified prefix.
1451 * SIOCALIFADDR: add the specified address.
1452 * SIOCALIFADDR with IFLR_PREFIX:
1453 * add the specified prefix, filling hostid part from
1454 * the first link-local address. prefixlen must be <= 64.
1455 * SIOCDLIFADDR: delete the specified address.
1456 * SIOCDLIFADDR with IFLR_PREFIX:
1457 * delete the first address that matches the specified prefix.
1459 * EINVAL on invalid parameters
1460 * EADDRNOTAVAIL on prefix match failed/specified address not found
1461 * other values may be returned from in6_ioctl()
1463 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1464 * this is to accomodate address naming scheme other than RFC2374,
1466 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1467 * address encoding scheme. (see figure on page 8)
1470 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1471 struct ifnet *ifp, struct thread *td)
1473 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1475 struct sockaddr *sa;
1478 if (!data || !ifp) {
1479 panic("invalid argument to in6_lifaddr_ioctl");
1485 /* address must be specified on GET with IFLR_PREFIX */
1486 if ((iflr->flags & IFLR_PREFIX) == 0)
1491 /* address must be specified on ADD and DELETE */
1492 sa = (struct sockaddr *)&iflr->addr;
1493 if (sa->sa_family != AF_INET6)
1495 if (sa->sa_len != sizeof(struct sockaddr_in6))
1497 /* XXX need improvement */
1498 sa = (struct sockaddr *)&iflr->dstaddr;
1499 if (sa->sa_family && sa->sa_family != AF_INET6)
1501 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1504 default: /* shouldn't happen */
1506 panic("invalid cmd to in6_lifaddr_ioctl");
1512 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1518 struct in6_aliasreq ifra;
1519 struct in6_addr *hostid = NULL;
1523 if ((iflr->flags & IFLR_PREFIX) != 0) {
1524 struct sockaddr_in6 *sin6;
1527 * hostid is to fill in the hostid part of the
1528 * address. hostid points to the first link-local
1529 * address attached to the interface.
1531 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1533 return EADDRNOTAVAIL;
1534 hostid = IFA_IN6(ifa);
1536 /* prefixlen must be <= 64. */
1537 if (64 < iflr->prefixlen)
1539 prefixlen = iflr->prefixlen;
1541 /* hostid part must be zero. */
1542 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1543 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1544 sin6->sin6_addr.s6_addr32[3] != 0) {
1548 prefixlen = iflr->prefixlen;
1550 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1551 bzero(&ifra, sizeof(ifra));
1552 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1554 bcopy(&iflr->addr, &ifra.ifra_addr,
1555 ((struct sockaddr *)&iflr->addr)->sa_len);
1557 /* fill in hostid part */
1558 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1559 hostid->s6_addr32[2];
1560 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1561 hostid->s6_addr32[3];
1564 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1565 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1566 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1568 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1569 hostid->s6_addr32[2];
1570 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1571 hostid->s6_addr32[3];
1577 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1578 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1580 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1581 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1586 struct in6_ifaddr *ia;
1587 struct in6_addr mask, candidate, match;
1588 struct sockaddr_in6 *sin6;
1591 bzero(&mask, sizeof(mask));
1592 if (iflr->flags & IFLR_PREFIX) {
1593 /* lookup a prefix rather than address. */
1594 in6_prefixlen2mask(&mask, iflr->prefixlen);
1596 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1597 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1598 match.s6_addr32[0] &= mask.s6_addr32[0];
1599 match.s6_addr32[1] &= mask.s6_addr32[1];
1600 match.s6_addr32[2] &= mask.s6_addr32[2];
1601 match.s6_addr32[3] &= mask.s6_addr32[3];
1603 /* if you set extra bits, that's wrong */
1604 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1609 if (cmd == SIOCGLIFADDR) {
1610 /* on getting an address, take the 1st match */
1613 /* on deleting an address, do exact match */
1614 in6_prefixlen2mask(&mask, 128);
1615 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1616 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1623 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1624 if (ifa->ifa_addr->sa_family != AF_INET6)
1630 * XXX: this is adhoc, but is necessary to allow
1631 * a user to specify fe80::/64 (not /10) for a
1632 * link-local address.
1634 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1635 in6_clearscope(&candidate);
1636 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1637 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1638 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1639 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1640 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1643 IF_ADDR_UNLOCK(ifp);
1645 return EADDRNOTAVAIL;
1648 if (cmd == SIOCGLIFADDR) {
1651 /* fill in the if_laddrreq structure */
1652 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1653 error = sa6_recoverscope(
1654 (struct sockaddr_in6 *)&iflr->addr);
1658 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1659 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1660 ia->ia_dstaddr.sin6_len);
1661 error = sa6_recoverscope(
1662 (struct sockaddr_in6 *)&iflr->dstaddr);
1666 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1669 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1671 iflr->flags = ia->ia6_flags; /* XXX */
1675 struct in6_aliasreq ifra;
1677 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1678 bzero(&ifra, sizeof(ifra));
1679 bcopy(iflr->iflr_name, ifra.ifra_name,
1680 sizeof(ifra.ifra_name));
1682 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1683 ia->ia_addr.sin6_len);
1684 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1685 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1686 ia->ia_dstaddr.sin6_len);
1688 bzero(&ifra.ifra_dstaddr,
1689 sizeof(ifra.ifra_dstaddr));
1691 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1692 ia->ia_prefixmask.sin6_len);
1694 ifra.ifra_flags = ia->ia6_flags;
1695 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1701 return EOPNOTSUPP; /* just for safety */
1705 * Initialize an interface's intetnet6 address
1706 * and routing table entry.
1709 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1710 struct sockaddr_in6 *sin6, int newhost)
1712 int error = 0, plen, ifacount = 0;
1717 * Give the interface a chance to initialize
1718 * if this is its first address,
1719 * and to validate the address if necessary.
1722 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1723 if (ifa->ifa_addr->sa_family != AF_INET6)
1727 IF_ADDR_UNLOCK(ifp);
1729 ia->ia_addr = *sin6;
1731 if (ifacount <= 1 && ifp->if_ioctl) {
1732 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1740 ia->ia_ifa.ifa_metric = ifp->if_metric;
1742 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1746 * If a new destination address is specified for a point-to-point
1747 * interface, install a route to the destination as an interface
1749 * XXX: the logic below rejects assigning multiple addresses on a p2p
1750 * interface that share the same destination.
1752 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1753 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128) {
1754 struct sockaddr *dstaddr;
1755 int rtflags = RTF_UP | RTF_HOST;
1758 * use the interface address if configuring an
1759 * interface address with a /128 prefix len
1761 if (ia->ia_dstaddr.sin6_family == AF_INET6)
1762 dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1764 dstaddr = (struct sockaddr *)&ia->ia_addr;
1766 error = rtrequest(RTM_ADD,
1767 (struct sockaddr *)dstaddr,
1768 (struct sockaddr *)&ia->ia_addr,
1769 (struct sockaddr *)&ia->ia_prefixmask,
1770 ia->ia_flags | rtflags, NULL);
1773 ia->ia_flags |= IFA_ROUTE;
1777 * add a loopback route to self
1779 if (V_nd6_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
1780 struct rt_addrinfo info;
1781 struct rtentry *rt = NULL;
1782 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1784 bzero(&info, sizeof(info));
1785 info.rti_ifp = V_loif;
1786 info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC;
1787 info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr;
1788 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1789 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
1791 if (error == 0 && rt != NULL) {
1793 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1794 rt->rt_ifp->if_type;
1795 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1796 rt->rt_ifp->if_index;
1799 } else if (error != 0)
1800 log(LOG_INFO, "in6_ifinit: insertion failed\n");
1803 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1807 struct sockaddr_dl gateway;
1808 struct sockaddr_in6 mask, addr;
1810 IF_AFDATA_LOCK(ifp);
1811 ia->ia_ifa.ifa_rtrequest = NULL;
1814 * we need to report rt_newaddrmsg
1816 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE),
1817 (struct sockaddr *)&ia->ia_addr);
1818 IF_AFDATA_UNLOCK(ifp);
1820 ln->la_expire = 0; /* for IPv6 this means permanent */
1821 ln->ln_state = ND6_LLINFO_REACHABLE;
1823 * initialize for rtmsg generation
1825 bzero(&gateway, sizeof(gateway));
1826 gateway.sdl_len = sizeof(gateway);
1827 gateway.sdl_family = AF_LINK;
1828 gateway.sdl_nlen = 0;
1829 gateway.sdl_alen = 6;
1830 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr));
1835 bzero(&rt, sizeof(rt));
1836 rt.rt_gateway = (struct sockaddr *)&gateway;
1837 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1838 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1839 rt_mask(&rt) = (struct sockaddr *)&mask;
1840 rt_key(&rt) = (struct sockaddr *)&addr;
1841 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
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);
2133 IF_ADDR_UNLOCK(ifp);
2136 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2137 if (ifa->ifa_addr->sa_family != AF_INET6)
2139 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2140 continue; /* XXX: is there any case to allow anycast? */
2141 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2142 continue; /* don't use this interface */
2143 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2145 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2146 if (V_ip6_use_deprecated)
2147 dep[1] = (struct in6_ifaddr *)ifa;
2153 IN6_IFADDR_RUNLOCK();
2154 return (struct in6_ifaddr *)ifa;
2156 IN6_IFADDR_RUNLOCK();
2158 /* use the last-resort values, that are, deprecated addresses */
2168 * perform DAD when interface becomes IFF_UP.
2171 in6_if_up(struct ifnet *ifp)
2174 struct in6_ifaddr *ia;
2177 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2178 if (ifa->ifa_addr->sa_family != AF_INET6)
2180 ia = (struct in6_ifaddr *)ifa;
2181 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2183 * The TENTATIVE flag was likely set by hand
2184 * beforehand, implicitly indicating the need for DAD.
2185 * We may be able to skip the random delay in this
2186 * case, but we impose delays just in case.
2189 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2192 IF_ADDR_UNLOCK(ifp);
2195 * special cases, like 6to4, are handled in in6_ifattach
2197 in6_ifattach(ifp, NULL);
2201 in6if_do_dad(struct ifnet *ifp)
2203 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2206 switch (ifp->if_type) {
2212 * These interfaces do not have the IFF_LOOPBACK flag,
2213 * but loop packets back. We do not have to do DAD on such
2214 * interfaces. We should even omit it, because loop-backed
2215 * NS would confuse the DAD procedure.
2220 * Our DAD routine requires the interface up and running.
2221 * However, some interfaces can be up before the RUNNING
2222 * status. Additionaly, users may try to assign addresses
2223 * before the interface becomes up (or running).
2224 * We simply skip DAD in such a case as a work around.
2225 * XXX: we should rather mark "tentative" on such addresses,
2226 * and do DAD after the interface becomes ready.
2228 if (!((ifp->if_flags & IFF_UP) &&
2229 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2237 * Calculate max IPv6 MTU through all the interfaces and store it
2243 unsigned long maxmtu = 0;
2247 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
2248 ifp = TAILQ_NEXT(ifp, if_list)) {
2249 /* this function can be called during ifnet initialization */
2250 if (!ifp->if_afdata[AF_INET6])
2252 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2253 IN6_LINKMTU(ifp) > maxmtu)
2254 maxmtu = IN6_LINKMTU(ifp);
2257 if (maxmtu) /* update only when maxmtu is positive */
2258 V_in6_maxmtu = maxmtu;
2262 * Provide the length of interface identifiers to be used for the link attached
2263 * to the given interface. The length should be defined in "IPv6 over
2264 * xxx-link" document. Note that address architecture might also define
2265 * the length for a particular set of address prefixes, regardless of the
2266 * link type. As clarified in rfc2462bis, those two definitions should be
2267 * consistent, and those really are as of August 2004.
2270 in6_if2idlen(struct ifnet *ifp)
2272 switch (ifp->if_type) {
2273 case IFT_ETHER: /* RFC2464 */
2274 #ifdef IFT_PROPVIRTUAL
2275 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2278 case IFT_L2VLAN: /* ditto */
2280 #ifdef IFT_IEEE80211
2281 case IFT_IEEE80211: /* ditto */
2284 case IFT_MIP: /* ditto */
2287 case IFT_FDDI: /* RFC2467 */
2289 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2291 case IFT_PPP: /* RFC2472 */
2293 case IFT_ARCNET: /* RFC2497 */
2295 case IFT_FRELAY: /* RFC2590 */
2297 case IFT_IEEE1394: /* RFC3146 */
2300 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2302 return (64); /* XXX: is this really correct? */
2305 * Unknown link type:
2306 * It might be controversial to use the today's common constant
2307 * of 64 for these cases unconditionally. For full compliance,
2308 * we should return an error in this case. On the other hand,
2309 * if we simply miss the standard for the link type or a new
2310 * standard is defined for a new link type, the IFID length
2311 * is very likely to be the common constant. As a compromise,
2312 * we always use the constant, but make an explicit notice
2313 * indicating the "unknown" case.
2315 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2320 #include <sys/sysctl.h>
2322 struct in6_llentry {
2323 struct llentry base;
2324 struct sockaddr_in6 l3_addr6;
2327 static struct llentry *
2328 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2330 struct in6_llentry *lle;
2332 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2333 M_DONTWAIT | M_ZERO);
2334 if (lle == NULL) /* NB: caller generates msg */
2337 callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE);
2338 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2339 lle->base.lle_refcnt = 1;
2340 LLE_LOCK_INIT(&lle->base);
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)
2363 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2364 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2365 struct llentry *lle, *next;
2368 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2369 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2370 if (IN6_ARE_MASKED_ADDR_EQUAL(
2371 &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2374 callout_drain(&lle->la_timer);
2383 in6_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr)
2386 char ip6buf[INET6_ADDRSTRLEN];
2388 KASSERT(l3addr->sa_family == AF_INET6,
2389 ("sin_family %d", l3addr->sa_family));
2391 /* XXX rtalloc1 should take a const param */
2392 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2393 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2396 * Create an ND6 cache for an IPv6 neighbor
2397 * that is not covered by our own prefix.
2399 /* XXX ifaof_ifpforaddr should take a const param */
2400 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2407 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2408 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2417 static struct llentry *
2418 in6_lltable_lookup(struct lltable *llt, u_int flags,
2419 const struct sockaddr *l3addr)
2421 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2422 struct ifnet *ifp = llt->llt_ifp;
2423 struct llentry *lle;
2424 struct llentries *lleh;
2427 IF_AFDATA_LOCK_ASSERT(ifp);
2428 KASSERT(l3addr->sa_family == AF_INET6,
2429 ("sin_family %d", l3addr->sa_family));
2431 hashkey = sin6->sin6_addr.s6_addr32[3];
2432 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2433 LIST_FOREACH(lle, lleh, lle_next) {
2434 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2435 if (lle->la_flags & LLE_DELETED)
2437 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2438 sizeof(struct in6_addr)) == 0)
2443 if (!(flags & LLE_CREATE))
2446 * A route that covers the given address must have
2447 * been installed 1st because we are doing a resolution,
2450 if (!(flags & LLE_IFADDR) &&
2451 in6_lltable_rtcheck(ifp, l3addr) != 0)
2454 lle = in6_lltable_new(l3addr, flags);
2456 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2459 lle->la_flags = flags & ~LLE_CREATE;
2460 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2461 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2462 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2466 lle->lle_head = lleh;
2467 LIST_INSERT_HEAD(lleh, lle, lle_next);
2468 } else if (flags & LLE_DELETE) {
2469 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2471 lle->la_flags = LLE_DELETED;
2474 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2479 if (LLE_IS_VALID(lle)) {
2480 if (flags & LLE_EXCLUSIVE)
2489 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2491 struct ifnet *ifp = llt->llt_ifp;
2492 struct llentry *lle;
2495 struct rt_msghdr rtm;
2496 struct sockaddr_in6 sin6;
2498 * ndp.c assumes that sdl is word aligned
2503 struct sockaddr_dl sdl;
2508 * current IFNET_RLOCK() is mapped to IFNET_WLOCK()
2509 * so it is okay to use this ASSERT, change it when
2510 * IFNET lock is finalized
2512 IFNET_WLOCK_ASSERT();
2515 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2516 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2517 struct sockaddr_dl *sdl;
2519 /* skip deleted or invalid entries */
2520 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2522 /* Skip if jailed and not a valid IP of the prison. */
2523 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2526 * produce a msg made of:
2528 * struct sockaddr_in6 (IPv6)
2529 * struct sockaddr_dl;
2531 bzero(&ndpc, sizeof(ndpc));
2532 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2533 ndpc.rtm.rtm_version = RTM_VERSION;
2534 ndpc.rtm.rtm_type = RTM_GET;
2535 ndpc.rtm.rtm_flags = RTF_UP;
2536 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2537 ndpc.sin6.sin6_family = AF_INET6;
2538 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2539 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2542 if (lle->la_flags & LLE_PUB)
2543 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2546 sdl->sdl_family = AF_LINK;
2547 sdl->sdl_len = sizeof(*sdl);
2548 sdl->sdl_alen = ifp->if_addrlen;
2549 sdl->sdl_index = ifp->if_index;
2550 sdl->sdl_type = ifp->if_type;
2551 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2552 ndpc.rtm.rtm_rmx.rmx_expire =
2553 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2554 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2555 if (lle->la_flags & LLE_STATIC)
2556 ndpc.rtm.rtm_flags |= RTF_STATIC;
2557 ndpc.rtm.rtm_index = ifp->if_index;
2558 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2567 in6_domifattach(struct ifnet *ifp)
2569 struct in6_ifextra *ext;
2571 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2572 bzero(ext, sizeof(*ext));
2574 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2575 M_IFADDR, M_WAITOK);
2576 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2579 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2580 M_IFADDR, M_WAITOK);
2581 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2583 ext->nd_ifinfo = nd6_ifattach(ifp);
2584 ext->scope6_id = scope6_ifattach(ifp);
2585 ext->lltable = lltable_init(ifp, AF_INET6);
2586 if (ext->lltable != NULL) {
2587 ext->lltable->llt_new = in6_lltable_new;
2588 ext->lltable->llt_free = in6_lltable_free;
2589 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2590 ext->lltable->llt_rtcheck = in6_lltable_rtcheck;
2591 ext->lltable->llt_lookup = in6_lltable_lookup;
2592 ext->lltable->llt_dump = in6_lltable_dump;
2595 ext->mld_ifinfo = mld_domifattach(ifp);
2601 in6_domifdetach(struct ifnet *ifp, void *aux)
2603 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2605 mld_domifdetach(ifp);
2606 scope6_ifdetach(ext->scope6_id);
2607 nd6_ifdetach(ext->nd_ifinfo);
2608 lltable_free(ext->lltable);
2609 free(ext->in6_ifstat, M_IFADDR);
2610 free(ext->icmp6_ifstat, M_IFADDR);
2611 free(ext, M_IFADDR);
2615 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2616 * v4 mapped addr or v4 compat addr
2619 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2622 bzero(sin, sizeof(*sin));
2623 sin->sin_len = sizeof(struct sockaddr_in);
2624 sin->sin_family = AF_INET;
2625 sin->sin_port = sin6->sin6_port;
2626 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2629 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2631 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2633 bzero(sin6, sizeof(*sin6));
2634 sin6->sin6_len = sizeof(struct sockaddr_in6);
2635 sin6->sin6_family = AF_INET6;
2636 sin6->sin6_port = sin->sin_port;
2637 sin6->sin6_addr.s6_addr32[0] = 0;
2638 sin6->sin6_addr.s6_addr32[1] = 0;
2639 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2640 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2643 /* Convert sockaddr_in6 into sockaddr_in. */
2645 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2647 struct sockaddr_in *sin_p;
2648 struct sockaddr_in6 sin6;
2651 * Save original sockaddr_in6 addr and convert it
2654 sin6 = *(struct sockaddr_in6 *)nam;
2655 sin_p = (struct sockaddr_in *)nam;
2656 in6_sin6_2_sin(sin_p, &sin6);
2659 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2661 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2663 struct sockaddr_in *sin_p;
2664 struct sockaddr_in6 *sin6_p;
2666 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2668 sin_p = (struct sockaddr_in *)*nam;
2669 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2670 free(*nam, M_SONAME);
2671 *nam = (struct sockaddr *)sin6_p;