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 *);
139 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
140 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
143 in6_ifaddloop(struct ifaddr *ifa)
145 struct sockaddr_dl gateway;
146 struct sockaddr_in6 mask, addr;
148 struct in6_ifaddr *ia;
155 ifa->ifa_rtrequest = nd6_rtrequest;
156 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
157 LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
158 IF_AFDATA_UNLOCK(ifp);
160 ln->la_expire = 0; /* for IPv6 this means permanent */
161 ln->ln_state = ND6_LLINFO_REACHABLE;
163 * initialize for rtmsg generation
165 bzero(&gateway, sizeof(gateway));
166 gateway.sdl_len = sizeof(gateway);
167 gateway.sdl_family = AF_LINK;
168 gateway.sdl_nlen = 0;
169 gateway.sdl_alen = 6;
170 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned,
171 sizeof(ln->ll_addr));
175 bzero(&rt, sizeof(rt));
176 rt.rt_gateway = (struct sockaddr *)&gateway;
177 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
178 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
179 rt_mask(&rt) = (struct sockaddr *)&mask;
180 rt_key(&rt) = (struct sockaddr *)&addr;
181 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
182 rt_newaddrmsg(RTM_ADD, ifa, 0, &rt);
186 in6_ifremloop(struct ifaddr *ifa)
188 struct sockaddr_dl gateway;
189 struct sockaddr_in6 mask, addr;
191 struct in6_ifaddr *ia;
197 lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
198 (struct sockaddr *)&ia->ia_addr);
199 IF_AFDATA_UNLOCK(ifp);
202 * initialize for rtmsg generation
204 bzero(&gateway, sizeof(gateway));
205 gateway.sdl_len = sizeof(gateway);
206 gateway.sdl_family = AF_LINK;
207 gateway.sdl_nlen = 0;
208 gateway.sdl_alen = ifp->if_addrlen;
209 bzero(&rt0, sizeof(rt0));
210 rt0.rt_gateway = (struct sockaddr *)&gateway;
211 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
212 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
213 rt_mask(&rt0) = (struct sockaddr *)&mask;
214 rt_key(&rt0) = (struct sockaddr *)&addr;
215 rt0.rt_flags = RTF_HOST | RTF_STATIC;
216 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
220 in6_mask2len(struct in6_addr *mask, u_char *lim0)
223 u_char *lim = lim0, *p;
225 /* ignore the scope_id part */
226 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
227 lim = (u_char *)mask + sizeof(*mask);
228 for (p = (u_char *)mask; p < lim; x++, p++) {
234 for (y = 0; y < 8; y++) {
235 if ((*p & (0x80 >> y)) == 0)
241 * when the limit pointer is given, do a stricter check on the
245 if (y != 0 && (*p & (0x00ff >> y)) != 0)
247 for (p = p + 1; p < lim; p++)
255 #ifdef COMPAT_FREEBSD32
256 struct in6_ndifreq32 {
257 char ifname[IFNAMSIZ];
260 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
264 in6_control(struct socket *so, u_long cmd, caddr_t data,
265 struct ifnet *ifp, struct thread *td)
267 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
268 struct in6_ifaddr *ia = NULL;
269 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
270 struct sockaddr_in6 *sa6;
274 case SIOCGETSGCNT_IN6:
275 case SIOCGETMIFCNT_IN6:
276 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
280 case SIOCAADDRCTL_POLICY:
281 case SIOCDADDRCTL_POLICY:
283 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
287 return (in6_src_ioctl(cmd, data));
294 case SIOCSNDFLUSH_IN6:
295 case SIOCSPFXFLUSH_IN6:
296 case SIOCSRTRFLUSH_IN6:
297 case SIOCSDEFIFACE_IN6:
298 case SIOCSIFINFO_FLAGS:
299 case SIOCSIFINFO_IN6:
301 error = priv_check(td, PRIV_NETINET_ND6);
306 case OSIOCGIFINFO_IN6:
307 case SIOCGIFINFO_IN6:
310 case SIOCGNBRINFO_IN6:
311 case SIOCGDEFIFACE_IN6:
312 return (nd6_ioctl(cmd, data, ifp));
314 #ifdef COMPAT_FREEBSD32
315 case SIOCGDEFIFACE32_IN6:
317 struct in6_ndifreq ndif;
318 struct in6_ndifreq32 *ndif32;
320 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
324 ndif32 = (struct in6_ndifreq32 *)data;
325 ndif32->ifindex = ndif.ifindex;
332 case SIOCSIFPREFIX_IN6:
333 case SIOCDIFPREFIX_IN6:
334 case SIOCAIFPREFIX_IN6:
335 case SIOCCIFPREFIX_IN6:
336 case SIOCSGIFPREFIX_IN6:
337 case SIOCGIFPREFIX_IN6:
339 "prefix ioctls are now invalidated. "
340 "please use ifconfig.\n");
347 error = priv_check(td, PRIV_NETINET_SCOPE6);
351 return (scope6_set(ifp,
352 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
354 return (scope6_get(ifp,
355 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
357 return (scope6_get_default((struct scope6_id *)
358 ifr->ifr_ifru.ifru_scope_id));
364 error = priv_check(td, PRIV_NET_ADDIFADDR);
368 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
372 error = priv_check(td, PRIV_NET_DELIFADDR);
378 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
382 * Find address for this interface, if it exists.
384 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
385 * only, and used the first interface address as the target of other
386 * operations (without checking ifra_addr). This was because netinet
387 * code/API assumed at most 1 interface address per interface.
388 * Since IPv6 allows a node to assign multiple addresses
389 * on a single interface, we almost always look and check the
390 * presence of ifra_addr, and reject invalid ones here.
391 * It also decreases duplicated code among SIOC*_IN6 operations.
394 case SIOCAIFADDR_IN6:
395 case SIOCSIFPHYADDR_IN6:
396 sa6 = &ifra->ifra_addr;
398 case SIOCSIFADDR_IN6:
399 case SIOCGIFADDR_IN6:
400 case SIOCSIFDSTADDR_IN6:
401 case SIOCSIFNETMASK_IN6:
402 case SIOCGIFDSTADDR_IN6:
403 case SIOCGIFNETMASK_IN6:
404 case SIOCDIFADDR_IN6:
405 case SIOCGIFPSRCADDR_IN6:
406 case SIOCGIFPDSTADDR_IN6:
407 case SIOCGIFAFLAG_IN6:
408 case SIOCSNDFLUSH_IN6:
409 case SIOCSPFXFLUSH_IN6:
410 case SIOCSRTRFLUSH_IN6:
411 case SIOCGIFALIFETIME_IN6:
412 case SIOCSIFALIFETIME_IN6:
413 case SIOCGIFSTAT_IN6:
414 case SIOCGIFSTAT_ICMP6:
415 sa6 = &ifr->ifr_addr;
421 if (sa6 && sa6->sin6_family == AF_INET6) {
422 if (sa6->sin6_scope_id != 0)
423 error = sa6_embedscope(sa6, 0);
425 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
428 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
429 &sa6->sin6_addr)) != 0)
431 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
436 case SIOCSIFADDR_IN6:
437 case SIOCSIFDSTADDR_IN6:
438 case SIOCSIFNETMASK_IN6:
440 * Since IPv6 allows a node to assign multiple addresses
441 * on a single interface, SIOCSIFxxx ioctls are deprecated.
443 /* we decided to obsolete this command (20000704) */
447 case SIOCDIFADDR_IN6:
449 * for IPv4, we look for existing in_ifaddr here to allow
450 * "ifconfig if0 delete" to remove the first IPv4 address on
451 * the interface. For IPv6, as the spec allows multiple
452 * interface address from the day one, we consider "remove the
453 * first one" semantics to be not preferable.
456 error = EADDRNOTAVAIL;
460 case SIOCAIFADDR_IN6:
462 * We always require users to specify a valid IPv6 address for
463 * the corresponding operation.
465 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
466 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
467 error = EAFNOSUPPORT;
472 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
473 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
479 case SIOCGIFADDR_IN6:
480 /* This interface is basically deprecated. use SIOCGIFCONF. */
482 case SIOCGIFAFLAG_IN6:
483 case SIOCGIFNETMASK_IN6:
484 case SIOCGIFDSTADDR_IN6:
485 case SIOCGIFALIFETIME_IN6:
486 /* must think again about its semantics */
488 error = EADDRNOTAVAIL;
493 case SIOCSIFALIFETIME_IN6:
495 struct in6_addrlifetime *lt;
498 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
503 error = EADDRNOTAVAIL;
506 /* sanity for overflow - beware unsigned */
507 lt = &ifr->ifr_ifru.ifru_lifetime;
508 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
509 lt->ia6t_vltime + time_second < time_second) {
513 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
514 lt->ia6t_pltime + time_second < time_second) {
523 case SIOCGIFADDR_IN6:
524 ifr->ifr_addr = ia->ia_addr;
525 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
529 case SIOCGIFDSTADDR_IN6:
530 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
535 * XXX: should we check if ifa_dstaddr is NULL and return
538 ifr->ifr_dstaddr = ia->ia_dstaddr;
539 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
543 case SIOCGIFNETMASK_IN6:
544 ifr->ifr_addr = ia->ia_prefixmask;
547 case SIOCGIFAFLAG_IN6:
548 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
551 case SIOCGIFSTAT_IN6:
556 bzero(&ifr->ifr_ifru.ifru_stat,
557 sizeof(ifr->ifr_ifru.ifru_stat));
558 ifr->ifr_ifru.ifru_stat =
559 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
562 case SIOCGIFSTAT_ICMP6:
567 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
568 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
569 ifr->ifr_ifru.ifru_icmp6stat =
570 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
573 case SIOCGIFALIFETIME_IN6:
574 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
575 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
577 struct in6_addrlifetime *retlt =
578 &ifr->ifr_ifru.ifru_lifetime;
581 * XXX: adjust expiration time assuming time_t is
585 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
586 if (ia->ia6_lifetime.ia6t_vltime <
587 maxexpire - ia->ia6_updatetime) {
588 retlt->ia6t_expire = ia->ia6_updatetime +
589 ia->ia6_lifetime.ia6t_vltime;
591 retlt->ia6t_expire = maxexpire;
593 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
595 struct in6_addrlifetime *retlt =
596 &ifr->ifr_ifru.ifru_lifetime;
599 * XXX: adjust expiration time assuming time_t is
603 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
604 if (ia->ia6_lifetime.ia6t_pltime <
605 maxexpire - ia->ia6_updatetime) {
606 retlt->ia6t_preferred = ia->ia6_updatetime +
607 ia->ia6_lifetime.ia6t_pltime;
609 retlt->ia6t_preferred = maxexpire;
613 case SIOCSIFALIFETIME_IN6:
614 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
616 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
617 ia->ia6_lifetime.ia6t_expire =
618 time_second + ia->ia6_lifetime.ia6t_vltime;
620 ia->ia6_lifetime.ia6t_expire = 0;
621 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
622 ia->ia6_lifetime.ia6t_preferred =
623 time_second + ia->ia6_lifetime.ia6t_pltime;
625 ia->ia6_lifetime.ia6t_preferred = 0;
628 case SIOCAIFADDR_IN6:
631 struct nd_prefixctl pr0;
632 struct nd_prefix *pr;
635 * first, make or update the interface address structure,
636 * and link it to the list.
638 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
641 ifa_free(&ia->ia_ifa);
642 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
645 * this can happen when the user specify the 0 valid
652 * then, make the prefix on-link on the interface.
653 * XXX: we'd rather create the prefix before the address, but
654 * we need at least one address to install the corresponding
655 * interface route, so we configure the address first.
659 * convert mask to prefix length (prefixmask has already
660 * been validated in in6_update_ifa().
662 bzero(&pr0, sizeof(pr0));
664 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
666 if (pr0.ndpr_plen == 128) {
667 break; /* we don't need to install a host route. */
669 pr0.ndpr_prefix = ifra->ifra_addr;
670 /* apply the mask for safety. */
671 for (i = 0; i < 4; i++) {
672 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
673 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
676 * XXX: since we don't have an API to set prefix (not address)
677 * lifetimes, we just use the same lifetimes as addresses.
678 * The (temporarily) installed lifetimes can be overridden by
679 * later advertised RAs (when accept_rtadv is non 0), which is
680 * an intended behavior.
682 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
684 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
685 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
686 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
688 /* add the prefix if not yet. */
689 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
691 * nd6_prelist_add will install the corresponding
694 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
697 log(LOG_ERR, "nd6_prelist_add succeeded but "
704 /* relate the address to the prefix */
705 if (ia->ia6_ndpr == NULL) {
710 * If this is the first autoconf address from the
711 * prefix, create a temporary address as well
714 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
715 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
717 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
718 log(LOG_NOTICE, "in6_control: failed "
719 "to create a temporary address, "
726 * this might affect the status of autoconfigured addresses,
727 * that is, this address might make other addresses detached.
729 pfxlist_onlink_check();
730 if (error == 0 && ia) {
731 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
733 * Try to clear the flag when a new
734 * IPv6 address is added onto an
735 * IFDISABLED interface and it
738 struct in6_ndireq nd;
740 memset(&nd, 0, sizeof(nd));
741 nd.ndi.flags = ND_IFINFO(ifp)->flags;
742 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
743 if (nd6_ioctl(SIOCSIFINFO_FLAGS,
744 (caddr_t)&nd, ifp) < 0)
745 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
746 "SIOCSIFINFO_FLAGS for -ifdisabled "
749 * Ignore failure of clearing the flag
750 * intentionally. The failure means
751 * address duplication was detected.
754 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
759 case SIOCDIFADDR_IN6:
761 struct nd_prefix *pr;
764 * If the address being deleted is the only one that owns
765 * the corresponding prefix, expire the prefix as well.
766 * XXX: theoretically, we don't have to worry about such
767 * relationship, since we separate the address management
768 * and the prefix management. We do this, however, to provide
769 * as much backward compatibility as possible in terms of
770 * the ioctl operation.
771 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
774 in6_purgeaddr(&ia->ia_ifa);
775 if (pr && pr->ndpr_refcnt == 0)
777 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
782 if (ifp == NULL || ifp->if_ioctl == 0) {
786 error = (*ifp->if_ioctl)(ifp, cmd, data);
793 ifa_free(&ia->ia_ifa);
798 * Update parameters of an IPv6 interface address.
799 * If necessary, a new entry is created and linked into address chains.
800 * This function is separated from in6_control().
801 * XXX: should this be performed under splnet()?
804 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
805 struct in6_ifaddr *ia, int flags)
807 int error = 0, hostIsNew = 0, plen = -1;
808 struct sockaddr_in6 dst6;
809 struct in6_addrlifetime *lt;
810 struct in6_multi_mship *imm;
811 struct in6_multi *in6m_sol;
814 char ip6buf[INET6_ADDRSTRLEN];
816 /* Validate parameters */
817 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
821 * The destination address for a p2p link must have a family
822 * of AF_UNSPEC or AF_INET6.
824 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
825 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
826 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
827 return (EAFNOSUPPORT);
829 * validate ifra_prefixmask. don't check sin6_family, netmask
830 * does not carry fields other than sin6_len.
832 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
835 * Because the IPv6 address architecture is classless, we require
836 * users to specify a (non 0) prefix length (mask) for a new address.
837 * We also require the prefix (when specified) mask is valid, and thus
838 * reject a non-consecutive mask.
840 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
842 if (ifra->ifra_prefixmask.sin6_len != 0) {
843 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
844 (u_char *)&ifra->ifra_prefixmask +
845 ifra->ifra_prefixmask.sin6_len);
850 * In this case, ia must not be NULL. We just use its prefix
853 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
856 * If the destination address on a p2p interface is specified,
857 * and the address is a scoped one, validate/set the scope
860 dst6 = ifra->ifra_dstaddr;
861 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
862 (dst6.sin6_family == AF_INET6)) {
863 struct in6_addr in6_tmp;
866 in6_tmp = dst6.sin6_addr;
867 if (in6_setscope(&in6_tmp, ifp, &zoneid))
868 return (EINVAL); /* XXX: should be impossible */
870 if (dst6.sin6_scope_id != 0) {
871 if (dst6.sin6_scope_id != zoneid)
873 } else /* user omit to specify the ID. */
874 dst6.sin6_scope_id = zoneid;
876 /* convert into the internal form */
877 if (sa6_embedscope(&dst6, 0))
878 return (EINVAL); /* XXX: should be impossible */
881 * The destination address can be specified only for a p2p or a
882 * loopback interface. If specified, the corresponding prefix length
885 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
886 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
887 /* XXX: noisy message */
888 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
889 "be specified for a p2p or a loopback IF only\n"));
893 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
894 "be 128 when dstaddr is specified\n"));
898 /* lifetime consistency check */
899 lt = &ifra->ifra_lifetime;
900 if (lt->ia6t_pltime > lt->ia6t_vltime)
902 if (lt->ia6t_vltime == 0) {
904 * the following log might be noisy, but this is a typical
905 * configuration mistake or a tool's bug.
908 "in6_update_ifa: valid lifetime is 0 for %s\n",
909 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
912 return (0); /* there's nothing to do */
916 * If this is a new address, allocate a new ifaddr and link it
922 * When in6_update_ifa() is called in a process of a received
923 * RA, it is called under an interrupt context. So, we should
924 * call malloc with M_NOWAIT.
926 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
930 bzero((caddr_t)ia, sizeof(*ia));
931 ifa_init(&ia->ia_ifa);
932 LIST_INIT(&ia->ia6_memberships);
933 /* Initialize the address and masks, and put time stamp */
934 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
935 ia->ia_addr.sin6_family = AF_INET6;
936 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
937 ia->ia6_createtime = time_second;
938 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
940 * XXX: some functions expect that ifa_dstaddr is not
941 * NULL for p2p interfaces.
943 ia->ia_ifa.ifa_dstaddr =
944 (struct sockaddr *)&ia->ia_dstaddr;
946 ia->ia_ifa.ifa_dstaddr = NULL;
948 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
950 ifa_ref(&ia->ia_ifa); /* if_addrhead */
952 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
955 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
957 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
958 IN6_IFADDR_WUNLOCK();
961 /* update timestamp */
962 ia->ia6_updatetime = time_second;
964 /* set prefix mask */
965 if (ifra->ifra_prefixmask.sin6_len) {
967 * We prohibit changing the prefix length of an existing
969 * + such an operation should be rare in IPv6, and
970 * + the operation would confuse prefix management.
972 if (ia->ia_prefixmask.sin6_len &&
973 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
974 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
975 " existing (%s) address should not be changed\n",
976 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
980 ia->ia_prefixmask = ifra->ifra_prefixmask;
984 * If a new destination address is specified, scrub the old one and
985 * install the new destination. Note that the interface must be
986 * p2p or loopback (see the check above.)
988 if (dst6.sin6_family == AF_INET6 &&
989 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
992 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
993 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
994 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
995 "a route to the old destination: %s\n",
996 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
997 /* proceed anyway... */
999 ia->ia_flags &= ~IFA_ROUTE;
1000 ia->ia_dstaddr = dst6;
1004 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1005 * to see if the address is deprecated or invalidated, but initialize
1006 * these members for applications.
1008 ia->ia6_lifetime = ifra->ifra_lifetime;
1009 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1010 ia->ia6_lifetime.ia6t_expire =
1011 time_second + ia->ia6_lifetime.ia6t_vltime;
1013 ia->ia6_lifetime.ia6t_expire = 0;
1014 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1015 ia->ia6_lifetime.ia6t_preferred =
1016 time_second + ia->ia6_lifetime.ia6t_pltime;
1018 ia->ia6_lifetime.ia6t_preferred = 0;
1020 /* reset the interface and routing table appropriately. */
1021 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1025 * configure address flags.
1027 ia->ia6_flags = ifra->ifra_flags;
1029 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1030 * userland, make it deprecated.
1032 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1033 ia->ia6_lifetime.ia6t_pltime = 0;
1034 ia->ia6_lifetime.ia6t_preferred = time_second;
1037 * Make the address tentative before joining multicast addresses,
1038 * so that corresponding MLD responses would not have a tentative
1041 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1042 if (hostIsNew && in6if_do_dad(ifp))
1043 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1045 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
1046 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1047 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1050 * We are done if we have simply modified an existing address.
1056 * Beyond this point, we should call in6_purgeaddr upon an error,
1057 * not just go to unlink.
1060 /* Join necessary multicast groups */
1062 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1063 struct sockaddr_in6 mltaddr, mltmask;
1064 struct in6_addr llsol;
1066 /* join solicited multicast addr for new host id */
1067 bzero(&llsol, sizeof(struct in6_addr));
1068 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1069 llsol.s6_addr32[1] = 0;
1070 llsol.s6_addr32[2] = htonl(1);
1071 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1072 llsol.s6_addr8[12] = 0xff;
1073 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1074 /* XXX: should not happen */
1075 log(LOG_ERR, "in6_update_ifa: "
1076 "in6_setscope failed\n");
1080 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1082 * We need a random delay for DAD on the address
1083 * being configured. It also means delaying
1084 * transmission of the corresponding MLD report to
1085 * avoid report collision.
1086 * [RFC 4861, Section 6.3.7]
1088 delay = arc4random() %
1089 (MAX_RTR_SOLICITATION_DELAY * hz);
1091 imm = in6_joingroup(ifp, &llsol, &error, delay);
1093 nd6log((LOG_WARNING,
1094 "in6_update_ifa: addmulti failed for "
1095 "%s on %s (errno=%d)\n",
1096 ip6_sprintf(ip6buf, &llsol), if_name(ifp),
1100 LIST_INSERT_HEAD(&ia->ia6_memberships,
1102 in6m_sol = imm->i6mm_maddr;
1104 bzero(&mltmask, sizeof(mltmask));
1105 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1106 mltmask.sin6_family = AF_INET6;
1107 mltmask.sin6_addr = in6mask32;
1108 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1111 * join link-local all-nodes address
1113 bzero(&mltaddr, sizeof(mltaddr));
1114 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1115 mltaddr.sin6_family = AF_INET6;
1116 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1117 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1119 goto cleanup; /* XXX: should not fail */
1122 * XXX: do we really need this automatic routes?
1123 * We should probably reconsider this stuff. Most applications
1124 * actually do not need the routes, since they usually specify
1125 * the outgoing interface.
1127 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1129 /* XXX: only works in !SCOPEDROUTING case. */
1130 if (memcmp(&mltaddr.sin6_addr,
1131 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1138 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1139 (struct sockaddr *)&ia->ia_addr,
1140 (struct sockaddr *)&mltmask, RTF_UP,
1141 (struct rtentry **)0);
1148 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1150 nd6log((LOG_WARNING,
1151 "in6_update_ifa: addmulti failed for "
1152 "%s on %s (errno=%d)\n",
1153 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1154 if_name(ifp), error));
1157 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1160 * join node information group address
1163 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1165 * The spec doesn't say anything about delay for this
1166 * group, but the same logic should apply.
1168 delay = arc4random() %
1169 (MAX_RTR_SOLICITATION_DELAY * hz);
1171 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
1172 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1173 delay); /* XXX jinmei */
1175 nd6log((LOG_WARNING, "in6_update_ifa: "
1176 "addmulti failed for %s on %s "
1178 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1179 if_name(ifp), error));
1180 /* XXX not very fatal, go on... */
1182 LIST_INSERT_HEAD(&ia->ia6_memberships,
1188 * join interface-local all-nodes address.
1189 * (ff01::1%ifN, and ff01::%ifN/32)
1191 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1192 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1194 goto cleanup; /* XXX: should not fail */
1195 /* XXX: again, do we really need the route? */
1196 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1198 if (memcmp(&mltaddr.sin6_addr,
1199 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1206 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1207 (struct sockaddr *)&ia->ia_addr,
1208 (struct sockaddr *)&mltmask, RTF_UP,
1209 (struct rtentry **)0);
1215 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1217 nd6log((LOG_WARNING, "in6_update_ifa: "
1218 "addmulti failed for %s on %s "
1220 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1221 if_name(ifp), error));
1224 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1229 * Perform DAD, if needed.
1230 * XXX It may be of use, if we can administratively
1233 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1234 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1236 int mindelay, maxdelay;
1239 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1241 * We need to impose a delay before sending an NS
1242 * for DAD. Check if we also needed a delay for the
1243 * corresponding MLD message. If we did, the delay
1244 * should be larger than the MLD delay (this could be
1245 * relaxed a bit, but this simple logic is at least
1247 * XXX: Break data hiding guidelines and look at
1248 * state for the solicited multicast group.
1251 if (in6m_sol != NULL &&
1252 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1253 mindelay = in6m_sol->in6m_timer;
1255 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1256 if (maxdelay - mindelay == 0)
1260 (arc4random() % (maxdelay - mindelay)) +
1264 nd6_dad_start((struct ifaddr *)ia, delay);
1267 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1268 ifa_free(&ia->ia_ifa);
1273 * XXX: if a change of an existing address failed, keep the entry
1277 in6_unlink_ifa(ia, ifp);
1278 ifa_free(&ia->ia_ifa);
1283 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1284 ifa_free(&ia->ia_ifa);
1285 in6_purgeaddr(&ia->ia_ifa);
1290 in6_purgeaddr(struct ifaddr *ifa)
1292 struct ifnet *ifp = ifa->ifa_ifp;
1293 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1294 struct in6_multi_mship *imm;
1295 struct sockaddr_in6 mltaddr, mltmask;
1298 struct ifaddr *ifa0;
1301 * find another IPv6 address as the gateway for the
1302 * link-local and node-local all-nodes multicast
1306 TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1307 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1308 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1309 &ia->ia_addr.sin6_addr,
1310 sizeof(struct in6_addr)) == 0)
1317 IF_ADDR_UNLOCK(ifp);
1320 * Remove the loopback route to the interface address.
1321 * The check for the current setting of "nd6_useloopback"
1324 if (ia->ia_flags & IFA_RTSELF) {
1325 error = ifa_del_loopback_route((struct ifaddr *)ia,
1326 (struct sockaddr *)&ia->ia_addr);
1328 ia->ia_flags &= ~IFA_RTSELF;
1331 /* stop DAD processing */
1337 * leave from multicast groups we have joined for the interface
1339 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1340 LIST_REMOVE(imm, i6mm_chain);
1341 in6_leavegroup(imm);
1345 * remove the link-local all-nodes address
1347 bzero(&mltmask, sizeof(mltmask));
1348 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1349 mltmask.sin6_family = AF_INET6;
1350 mltmask.sin6_addr = in6mask32;
1352 bzero(&mltaddr, sizeof(mltaddr));
1353 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1354 mltaddr.sin6_family = AF_INET6;
1355 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1357 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1360 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1361 if (rt != NULL && rt->rt_gateway != NULL &&
1362 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1363 &ia->ia_addr.sin6_addr,
1364 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1366 * if no more IPv6 address exists on this interface
1367 * then remove the multicast address route
1370 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1371 sizeof(mltaddr.sin6_addr));
1373 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1374 (struct sockaddr *)&ia->ia_addr,
1375 (struct sockaddr *)&mltmask, RTF_UP,
1376 (struct rtentry **)0);
1378 log(LOG_INFO, "in6_purgeaddr: link-local all-nodes"
1379 "multicast address deletion error\n");
1382 * replace the gateway of the route
1384 struct sockaddr_in6 sa;
1386 bzero(&sa, sizeof(sa));
1387 sa.sin6_len = sizeof(struct sockaddr_in6);
1388 sa.sin6_family = AF_INET6;
1389 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1390 sizeof(sa.sin6_addr));
1391 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1392 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1401 * remove the node-local all-nodes address
1403 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1404 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1408 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1409 if (rt != NULL && rt->rt_gateway != NULL &&
1410 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1411 &ia->ia_addr.sin6_addr,
1412 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1414 * if no more IPv6 address exists on this interface
1415 * then remove the multicast address route
1418 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1419 sizeof(mltaddr.sin6_addr));
1422 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1423 (struct sockaddr *)&ia->ia_addr,
1424 (struct sockaddr *)&mltmask, RTF_UP,
1425 (struct rtentry **)0);
1428 log(LOG_INFO, "in6_purgeaddr: node-local all-nodes"
1429 "multicast address deletion error\n");
1432 * replace the gateway of the route
1434 struct sockaddr_in6 sa;
1436 bzero(&sa, sizeof(sa));
1437 sa.sin6_len = sizeof(struct sockaddr_in6);
1438 sa.sin6_family = AF_INET6;
1439 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1440 sizeof(sa.sin6_addr));
1441 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1442 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1452 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1453 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1455 struct sockaddr *dstaddr;
1458 * use the interface address if configuring an
1459 * interface address with a /128 prefix len
1461 if (ia->ia_dstaddr.sin6_family == AF_INET6)
1462 dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1464 dstaddr = (struct sockaddr *)&ia->ia_addr;
1466 error = rtrequest(RTM_DELETE,
1467 (struct sockaddr *)dstaddr,
1468 (struct sockaddr *)&ia->ia_addr,
1469 (struct sockaddr *)&ia->ia_prefixmask,
1470 ia->ia_flags | RTF_HOST, NULL);
1473 ia->ia_flags &= ~IFA_ROUTE;
1478 in6_unlink_ifa(ia, ifp);
1482 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1487 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1488 IF_ADDR_UNLOCK(ifp);
1489 ifa_free(&ia->ia_ifa); /* if_addrhead */
1492 * Defer the release of what might be the last reference to the
1493 * in6_ifaddr so that it can't be freed before the remainder of the
1497 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1498 IN6_IFADDR_WUNLOCK();
1501 * Release the reference to the base prefix. There should be a
1502 * positive reference.
1504 if (ia->ia6_ndpr == NULL) {
1506 "in6_unlink_ifa: autoconf'ed address "
1507 "%p has no prefix\n", ia));
1509 ia->ia6_ndpr->ndpr_refcnt--;
1510 ia->ia6_ndpr = NULL;
1514 * Also, if the address being removed is autoconf'ed, call
1515 * pfxlist_onlink_check() since the release might affect the status of
1516 * other (detached) addresses.
1518 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1519 pfxlist_onlink_check();
1521 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1526 in6_purgeif(struct ifnet *ifp)
1528 struct ifaddr *ifa, *nifa;
1530 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1531 if (ifa->ifa_addr->sa_family != AF_INET6)
1541 * SIOCGLIFADDR: get first address. (?)
1542 * SIOCGLIFADDR with IFLR_PREFIX:
1543 * get first address that matches the specified prefix.
1544 * SIOCALIFADDR: add the specified address.
1545 * SIOCALIFADDR with IFLR_PREFIX:
1546 * add the specified prefix, filling hostid part from
1547 * the first link-local address. prefixlen must be <= 64.
1548 * SIOCDLIFADDR: delete the specified address.
1549 * SIOCDLIFADDR with IFLR_PREFIX:
1550 * delete the first address that matches the specified prefix.
1552 * EINVAL on invalid parameters
1553 * EADDRNOTAVAIL on prefix match failed/specified address not found
1554 * other values may be returned from in6_ioctl()
1556 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1557 * this is to accomodate address naming scheme other than RFC2374,
1559 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1560 * address encoding scheme. (see figure on page 8)
1563 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1564 struct ifnet *ifp, struct thread *td)
1566 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1568 struct sockaddr *sa;
1571 if (!data || !ifp) {
1572 panic("invalid argument to in6_lifaddr_ioctl");
1578 /* address must be specified on GET with IFLR_PREFIX */
1579 if ((iflr->flags & IFLR_PREFIX) == 0)
1584 /* address must be specified on ADD and DELETE */
1585 sa = (struct sockaddr *)&iflr->addr;
1586 if (sa->sa_family != AF_INET6)
1588 if (sa->sa_len != sizeof(struct sockaddr_in6))
1590 /* XXX need improvement */
1591 sa = (struct sockaddr *)&iflr->dstaddr;
1592 if (sa->sa_family && sa->sa_family != AF_INET6)
1594 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1597 default: /* shouldn't happen */
1599 panic("invalid cmd to in6_lifaddr_ioctl");
1605 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1611 struct in6_aliasreq ifra;
1612 struct in6_addr *hostid = NULL;
1616 if ((iflr->flags & IFLR_PREFIX) != 0) {
1617 struct sockaddr_in6 *sin6;
1620 * hostid is to fill in the hostid part of the
1621 * address. hostid points to the first link-local
1622 * address attached to the interface.
1624 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1626 return EADDRNOTAVAIL;
1627 hostid = IFA_IN6(ifa);
1629 /* prefixlen must be <= 64. */
1630 if (64 < iflr->prefixlen)
1632 prefixlen = iflr->prefixlen;
1634 /* hostid part must be zero. */
1635 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1636 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1637 sin6->sin6_addr.s6_addr32[3] != 0) {
1641 prefixlen = iflr->prefixlen;
1643 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1644 bzero(&ifra, sizeof(ifra));
1645 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1647 bcopy(&iflr->addr, &ifra.ifra_addr,
1648 ((struct sockaddr *)&iflr->addr)->sa_len);
1650 /* fill in hostid part */
1651 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1652 hostid->s6_addr32[2];
1653 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1654 hostid->s6_addr32[3];
1657 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1658 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1659 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1661 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1662 hostid->s6_addr32[2];
1663 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1664 hostid->s6_addr32[3];
1670 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1671 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1673 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1674 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1679 struct in6_ifaddr *ia;
1680 struct in6_addr mask, candidate, match;
1681 struct sockaddr_in6 *sin6;
1684 bzero(&mask, sizeof(mask));
1685 if (iflr->flags & IFLR_PREFIX) {
1686 /* lookup a prefix rather than address. */
1687 in6_prefixlen2mask(&mask, iflr->prefixlen);
1689 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1690 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1691 match.s6_addr32[0] &= mask.s6_addr32[0];
1692 match.s6_addr32[1] &= mask.s6_addr32[1];
1693 match.s6_addr32[2] &= mask.s6_addr32[2];
1694 match.s6_addr32[3] &= mask.s6_addr32[3];
1696 /* if you set extra bits, that's wrong */
1697 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1702 if (cmd == SIOCGLIFADDR) {
1703 /* on getting an address, take the 1st match */
1706 /* on deleting an address, do exact match */
1707 in6_prefixlen2mask(&mask, 128);
1708 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1709 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1716 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1717 if (ifa->ifa_addr->sa_family != AF_INET6)
1723 * XXX: this is adhoc, but is necessary to allow
1724 * a user to specify fe80::/64 (not /10) for a
1725 * link-local address.
1727 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1728 in6_clearscope(&candidate);
1729 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1730 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1731 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1732 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1733 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1738 IF_ADDR_UNLOCK(ifp);
1740 return EADDRNOTAVAIL;
1743 if (cmd == SIOCGLIFADDR) {
1746 /* fill in the if_laddrreq structure */
1747 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1748 error = sa6_recoverscope(
1749 (struct sockaddr_in6 *)&iflr->addr);
1755 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1756 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1757 ia->ia_dstaddr.sin6_len);
1758 error = sa6_recoverscope(
1759 (struct sockaddr_in6 *)&iflr->dstaddr);
1765 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1768 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1770 iflr->flags = ia->ia6_flags; /* XXX */
1775 struct in6_aliasreq ifra;
1777 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1778 bzero(&ifra, sizeof(ifra));
1779 bcopy(iflr->iflr_name, ifra.ifra_name,
1780 sizeof(ifra.ifra_name));
1782 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1783 ia->ia_addr.sin6_len);
1784 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1785 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1786 ia->ia_dstaddr.sin6_len);
1788 bzero(&ifra.ifra_dstaddr,
1789 sizeof(ifra.ifra_dstaddr));
1791 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1792 ia->ia_prefixmask.sin6_len);
1794 ifra.ifra_flags = ia->ia6_flags;
1796 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1802 return EOPNOTSUPP; /* just for safety */
1806 * Initialize an interface's intetnet6 address
1807 * and routing table entry.
1810 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1811 struct sockaddr_in6 *sin6, int newhost)
1813 int error = 0, plen, ifacount = 0;
1818 * Give the interface a chance to initialize
1819 * if this is its first address,
1820 * and to validate the address if necessary.
1823 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1824 if (ifa->ifa_addr->sa_family != AF_INET6)
1828 IF_ADDR_UNLOCK(ifp);
1830 ia->ia_addr = *sin6;
1832 if (ifacount <= 1 && ifp->if_ioctl) {
1833 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1841 ia->ia_ifa.ifa_metric = ifp->if_metric;
1843 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1847 * If a new destination address is specified for a point-to-point
1848 * interface, install a route to the destination as an interface
1850 * XXX: the logic below rejects assigning multiple addresses on a p2p
1851 * interface that share the same destination.
1853 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1854 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1855 ia->ia_dstaddr.sin6_family == AF_INET6) {
1856 int rtflags = RTF_UP | RTF_HOST;
1858 error = rtrequest(RTM_ADD,
1859 (struct sockaddr *)&ia->ia_dstaddr,
1860 (struct sockaddr *)&ia->ia_addr,
1861 (struct sockaddr *)&ia->ia_prefixmask,
1862 ia->ia_flags | rtflags, NULL);
1865 ia->ia_flags |= IFA_ROUTE;
1867 * Handle the case for ::1 .
1869 if (ifp->if_flags & IFF_LOOPBACK)
1870 ia->ia_flags |= IFA_RTSELF;
1874 * add a loopback route to self
1876 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1877 error = ifa_add_loopback_route((struct ifaddr *)ia,
1878 (struct sockaddr *)&ia->ia_addr);
1880 ia->ia_flags |= IFA_RTSELF;
1883 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1885 in6_ifaddloop(&(ia->ia_ifa));
1891 * Find an IPv6 interface link-local address specific to an interface.
1892 * ifaddr is returned referenced.
1895 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1900 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1901 if (ifa->ifa_addr->sa_family != AF_INET6)
1903 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1904 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1911 IF_ADDR_UNLOCK(ifp);
1913 return ((struct in6_ifaddr *)ifa);
1918 * find the internet address corresponding to a given interface and address.
1919 * ifaddr is returned referenced.
1922 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1927 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1928 if (ifa->ifa_addr->sa_family != AF_INET6)
1930 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1935 IF_ADDR_UNLOCK(ifp);
1937 return ((struct in6_ifaddr *)ifa);
1941 * Convert IP6 address to printable (loggable) representation. Caller
1942 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1944 static char digits[] = "0123456789abcdef";
1946 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1948 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1950 const u_int16_t *a = (const u_int16_t *)addr;
1952 int dcolon = 0, zero = 0;
1956 for (i = 0; i < 8; i++) {
1957 if (*(a + i) == 0) {
1962 else if (maxcnt < cnt) {
1973 for (i = 0; i < 8; i++) {
1984 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1996 d = (const u_char *)a;
1997 /* Try to eliminate leading zeros in printout like in :0001. */
1999 *cp = digits[*d >> 4];
2004 *cp = digits[*d++ & 0xf];
2005 if (zero == 0 || (*cp != '0')) {
2009 *cp = digits[*d >> 4];
2010 if (zero == 0 || (*cp != '0')) {
2014 *cp++ = digits[*d & 0xf];
2023 in6_localaddr(struct in6_addr *in6)
2025 struct in6_ifaddr *ia;
2027 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
2031 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2032 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2033 &ia->ia_prefixmask.sin6_addr)) {
2034 IN6_IFADDR_RUNLOCK();
2038 IN6_IFADDR_RUNLOCK();
2044 * Return 1 if an internet address is for the local host and configured
2045 * on one of its interfaces.
2048 in6_localip(struct in6_addr *in6)
2050 struct in6_ifaddr *ia;
2053 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2054 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
2055 IN6_IFADDR_RUNLOCK();
2059 IN6_IFADDR_RUNLOCK();
2065 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
2067 struct in6_ifaddr *ia;
2070 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2071 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
2073 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
2074 IN6_IFADDR_RUNLOCK();
2075 return (1); /* true */
2078 /* XXX: do we still have to go thru the rest of the list? */
2080 IN6_IFADDR_RUNLOCK();
2082 return (0); /* false */
2086 * return length of part which dst and src are equal
2090 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2093 u_char *s = (u_char *)src, *d = (u_char *)dst;
2094 u_char *lim = s + 16, r;
2097 if ((r = (*d++ ^ *s++)) != 0) {
2108 /* XXX: to be scope conscious */
2110 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2112 int bytelen, bitlen;
2115 if (0 > len || len > 128) {
2116 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2124 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2127 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2128 p2->s6_addr[bytelen] >> (8 - bitlen))
2135 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2137 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2138 int bytelen, bitlen, i;
2141 if (0 > len || len > 128) {
2142 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2147 bzero(maskp, sizeof(*maskp));
2150 for (i = 0; i < bytelen; i++)
2151 maskp->s6_addr[i] = 0xff;
2153 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2157 * return the best address out of the same scope. if no address was
2158 * found, return the first valid address from designated IF.
2161 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2163 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2165 struct in6_ifaddr *besta = 0;
2166 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2168 dep[0] = dep[1] = NULL;
2171 * We first look for addresses in the same scope.
2172 * If there is one, return it.
2173 * If two or more, return one which matches the dst longest.
2174 * If none, return one of global addresses assigned other ifs.
2177 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2178 if (ifa->ifa_addr->sa_family != AF_INET6)
2180 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2181 continue; /* XXX: is there any case to allow anycast? */
2182 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2183 continue; /* don't use this interface */
2184 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2186 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2187 if (V_ip6_use_deprecated)
2188 dep[0] = (struct in6_ifaddr *)ifa;
2192 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2194 * call in6_matchlen() as few as possible
2198 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2199 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2202 besta = (struct in6_ifaddr *)ifa;
2205 besta = (struct in6_ifaddr *)ifa;
2209 ifa_ref(&besta->ia_ifa);
2210 IF_ADDR_UNLOCK(ifp);
2214 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2215 if (ifa->ifa_addr->sa_family != AF_INET6)
2217 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2218 continue; /* XXX: is there any case to allow anycast? */
2219 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2220 continue; /* don't use this interface */
2221 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2223 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2224 if (V_ip6_use_deprecated)
2225 dep[1] = (struct in6_ifaddr *)ifa;
2231 IF_ADDR_UNLOCK(ifp);
2232 return (struct in6_ifaddr *)ifa;
2234 IF_ADDR_UNLOCK(ifp);
2236 /* use the last-resort values, that are, deprecated addresses */
2246 * perform DAD when interface becomes IFF_UP.
2249 in6_if_up(struct ifnet *ifp)
2252 struct in6_ifaddr *ia;
2255 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2256 if (ifa->ifa_addr->sa_family != AF_INET6)
2258 ia = (struct in6_ifaddr *)ifa;
2259 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2261 * The TENTATIVE flag was likely set by hand
2262 * beforehand, implicitly indicating the need for DAD.
2263 * We may be able to skip the random delay in this
2264 * case, but we impose delays just in case.
2267 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2270 IF_ADDR_UNLOCK(ifp);
2273 * special cases, like 6to4, are handled in in6_ifattach
2275 in6_ifattach(ifp, NULL);
2279 in6if_do_dad(struct ifnet *ifp)
2281 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2284 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2287 switch (ifp->if_type) {
2293 * These interfaces do not have the IFF_LOOPBACK flag,
2294 * but loop packets back. We do not have to do DAD on such
2295 * interfaces. We should even omit it, because loop-backed
2296 * NS would confuse the DAD procedure.
2301 * Our DAD routine requires the interface up and running.
2302 * However, some interfaces can be up before the RUNNING
2303 * status. Additionaly, users may try to assign addresses
2304 * before the interface becomes up (or running).
2305 * We simply skip DAD in such a case as a work around.
2306 * XXX: we should rather mark "tentative" on such addresses,
2307 * and do DAD after the interface becomes ready.
2309 if (!((ifp->if_flags & IFF_UP) &&
2310 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2318 * Calculate max IPv6 MTU through all the interfaces and store it
2324 unsigned long maxmtu = 0;
2327 IFNET_RLOCK_NOSLEEP();
2328 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
2329 ifp = TAILQ_NEXT(ifp, if_list)) {
2330 /* this function can be called during ifnet initialization */
2331 if (!ifp->if_afdata[AF_INET6])
2333 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2334 IN6_LINKMTU(ifp) > maxmtu)
2335 maxmtu = IN6_LINKMTU(ifp);
2337 IFNET_RUNLOCK_NOSLEEP();
2338 if (maxmtu) /* update only when maxmtu is positive */
2339 V_in6_maxmtu = maxmtu;
2343 * Provide the length of interface identifiers to be used for the link attached
2344 * to the given interface. The length should be defined in "IPv6 over
2345 * xxx-link" document. Note that address architecture might also define
2346 * the length for a particular set of address prefixes, regardless of the
2347 * link type. As clarified in rfc2462bis, those two definitions should be
2348 * consistent, and those really are as of August 2004.
2351 in6_if2idlen(struct ifnet *ifp)
2353 switch (ifp->if_type) {
2354 case IFT_ETHER: /* RFC2464 */
2355 #ifdef IFT_PROPVIRTUAL
2356 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2359 case IFT_L2VLAN: /* ditto */
2361 #ifdef IFT_IEEE80211
2362 case IFT_IEEE80211: /* ditto */
2365 case IFT_MIP: /* ditto */
2367 case IFT_INFINIBAND:
2369 case IFT_FDDI: /* RFC2467 */
2371 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2373 case IFT_PPP: /* RFC2472 */
2375 case IFT_ARCNET: /* RFC2497 */
2377 case IFT_FRELAY: /* RFC2590 */
2379 case IFT_IEEE1394: /* RFC3146 */
2382 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2384 return (64); /* XXX: is this really correct? */
2387 * Unknown link type:
2388 * It might be controversial to use the today's common constant
2389 * of 64 for these cases unconditionally. For full compliance,
2390 * we should return an error in this case. On the other hand,
2391 * if we simply miss the standard for the link type or a new
2392 * standard is defined for a new link type, the IFID length
2393 * is very likely to be the common constant. As a compromise,
2394 * we always use the constant, but make an explicit notice
2395 * indicating the "unknown" case.
2397 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2402 #include <sys/sysctl.h>
2404 struct in6_llentry {
2405 struct llentry base;
2406 struct sockaddr_in6 l3_addr6;
2409 static struct llentry *
2410 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2412 struct in6_llentry *lle;
2414 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2415 M_DONTWAIT | M_ZERO);
2416 if (lle == NULL) /* NB: caller generates msg */
2419 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2420 lle->base.lle_refcnt = 1;
2421 LLE_LOCK_INIT(&lle->base);
2422 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2423 CALLOUT_RETURNUNLOCKED);
2429 * Deletes an address from the address table.
2430 * This function is called by the timer functions
2431 * such as arptimer() and nd6_llinfo_timer(), and
2432 * the caller does the locking.
2435 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2438 LLE_LOCK_DESTROY(lle);
2439 free(lle, M_LLTABLE);
2443 in6_lltable_prefix_free(struct lltable *llt,
2444 const struct sockaddr *prefix,
2445 const struct sockaddr *mask,
2448 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2449 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2450 struct llentry *lle, *next;
2454 * (flags & LLE_STATIC) means deleting all entries
2455 * including static ND6 entries
2457 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2458 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2459 if (IN6_ARE_MASKED_ADDR_EQUAL(
2460 &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2463 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) {
2466 canceled = callout_drain(&lle->la_timer);
2477 in6_lltable_rtcheck(struct ifnet *ifp,
2479 const struct sockaddr *l3addr)
2482 char ip6buf[INET6_ADDRSTRLEN];
2484 KASSERT(l3addr->sa_family == AF_INET6,
2485 ("sin_family %d", l3addr->sa_family));
2487 /* XXX rtalloc1 should take a const param */
2488 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2489 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2492 * Create an ND6 cache for an IPv6 neighbor
2493 * that is not covered by our own prefix.
2495 /* XXX ifaof_ifpforaddr should take a const param */
2496 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2503 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2504 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2513 static struct llentry *
2514 in6_lltable_lookup(struct lltable *llt, u_int flags,
2515 const struct sockaddr *l3addr)
2517 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2518 struct ifnet *ifp = llt->llt_ifp;
2519 struct llentry *lle;
2520 struct llentries *lleh;
2523 IF_AFDATA_LOCK_ASSERT(ifp);
2524 KASSERT(l3addr->sa_family == AF_INET6,
2525 ("sin_family %d", l3addr->sa_family));
2527 hashkey = sin6->sin6_addr.s6_addr32[3];
2528 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2529 LIST_FOREACH(lle, lleh, lle_next) {
2530 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2531 if (lle->la_flags & LLE_DELETED)
2533 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2534 sizeof(struct in6_addr)) == 0)
2539 if (!(flags & LLE_CREATE))
2542 * A route that covers the given address must have
2543 * been installed 1st because we are doing a resolution,
2546 if (!(flags & LLE_IFADDR) &&
2547 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2550 lle = in6_lltable_new(l3addr, flags);
2552 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2555 lle->la_flags = flags & ~LLE_CREATE;
2556 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2557 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2558 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2562 lle->lle_head = lleh;
2563 LIST_INSERT_HEAD(lleh, lle, lle_next);
2564 } else if (flags & LLE_DELETE) {
2565 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2567 lle->la_flags = LLE_DELETED;
2570 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2575 if (LLE_IS_VALID(lle)) {
2576 if (flags & LLE_EXCLUSIVE)
2585 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2587 struct ifnet *ifp = llt->llt_ifp;
2588 struct llentry *lle;
2591 struct rt_msghdr rtm;
2592 struct sockaddr_in6 sin6;
2594 * ndp.c assumes that sdl is word aligned
2599 struct sockaddr_dl sdl;
2603 if (ifp->if_flags & IFF_LOOPBACK)
2606 LLTABLE_LOCK_ASSERT();
2609 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2610 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2611 struct sockaddr_dl *sdl;
2613 /* skip deleted or invalid entries */
2614 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2616 /* Skip if jailed and not a valid IP of the prison. */
2617 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2620 * produce a msg made of:
2622 * struct sockaddr_in6 (IPv6)
2623 * struct sockaddr_dl;
2625 bzero(&ndpc, sizeof(ndpc));
2626 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2627 ndpc.rtm.rtm_version = RTM_VERSION;
2628 ndpc.rtm.rtm_type = RTM_GET;
2629 ndpc.rtm.rtm_flags = RTF_UP;
2630 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2631 ndpc.sin6.sin6_family = AF_INET6;
2632 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2633 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2636 if (lle->la_flags & LLE_PUB)
2637 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2640 sdl->sdl_family = AF_LINK;
2641 sdl->sdl_len = sizeof(*sdl);
2642 sdl->sdl_alen = ifp->if_addrlen;
2643 sdl->sdl_index = ifp->if_index;
2644 sdl->sdl_type = ifp->if_type;
2645 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2646 ndpc.rtm.rtm_rmx.rmx_expire =
2647 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2648 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2649 if (lle->la_flags & LLE_STATIC)
2650 ndpc.rtm.rtm_flags |= RTF_STATIC;
2651 ndpc.rtm.rtm_index = ifp->if_index;
2652 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2661 in6_domifattach(struct ifnet *ifp)
2663 struct in6_ifextra *ext;
2665 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2666 bzero(ext, sizeof(*ext));
2668 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2669 M_IFADDR, M_WAITOK);
2670 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2673 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2674 M_IFADDR, M_WAITOK);
2675 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2677 ext->nd_ifinfo = nd6_ifattach(ifp);
2678 ext->scope6_id = scope6_ifattach(ifp);
2679 ext->lltable = lltable_init(ifp, AF_INET6);
2680 if (ext->lltable != NULL) {
2681 ext->lltable->llt_free = in6_lltable_free;
2682 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2683 ext->lltable->llt_lookup = in6_lltable_lookup;
2684 ext->lltable->llt_dump = in6_lltable_dump;
2687 ext->mld_ifinfo = mld_domifattach(ifp);
2693 in6_domifdetach(struct ifnet *ifp, void *aux)
2695 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2697 mld_domifdetach(ifp);
2698 scope6_ifdetach(ext->scope6_id);
2699 nd6_ifdetach(ext->nd_ifinfo);
2700 lltable_free(ext->lltable);
2701 free(ext->in6_ifstat, M_IFADDR);
2702 free(ext->icmp6_ifstat, M_IFADDR);
2703 free(ext, M_IFADDR);
2707 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2708 * v4 mapped addr or v4 compat addr
2711 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2714 bzero(sin, sizeof(*sin));
2715 sin->sin_len = sizeof(struct sockaddr_in);
2716 sin->sin_family = AF_INET;
2717 sin->sin_port = sin6->sin6_port;
2718 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2721 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2723 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2725 bzero(sin6, sizeof(*sin6));
2726 sin6->sin6_len = sizeof(struct sockaddr_in6);
2727 sin6->sin6_family = AF_INET6;
2728 sin6->sin6_port = sin->sin_port;
2729 sin6->sin6_addr.s6_addr32[0] = 0;
2730 sin6->sin6_addr.s6_addr32[1] = 0;
2731 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2732 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2735 /* Convert sockaddr_in6 into sockaddr_in. */
2737 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2739 struct sockaddr_in *sin_p;
2740 struct sockaddr_in6 sin6;
2743 * Save original sockaddr_in6 addr and convert it
2746 sin6 = *(struct sockaddr_in6 *)nam;
2747 sin_p = (struct sockaddr_in *)nam;
2748 in6_sin6_2_sin(sin_p, &sin6);
2751 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2753 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2755 struct sockaddr_in *sin_p;
2756 struct sockaddr_in6 *sin6_p;
2758 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2760 sin_p = (struct sockaddr_in *)*nam;
2761 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2762 free(*nam, M_SONAME);
2763 *nam = (struct sockaddr *)sin6_p;