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_var.h>
85 #include <net/if_types.h>
86 #include <net/route.h>
87 #include <net/if_dl.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <net/if_llatbl.h>
93 #include <netinet/if_ether.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/in_pcb.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet6/mld6_var.h>
102 #include <netinet6/ip6_mroute.h>
103 #include <netinet6/in6_ifattach.h>
104 #include <netinet6/scope6_var.h>
105 #include <netinet6/in6_pcb.h>
108 * Definitions of some costant IP6 addresses.
110 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
111 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
112 const struct in6_addr in6addr_nodelocal_allnodes =
113 IN6ADDR_NODELOCAL_ALLNODES_INIT;
114 const struct in6_addr in6addr_linklocal_allnodes =
115 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
116 const struct in6_addr in6addr_linklocal_allrouters =
117 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
118 const struct in6_addr in6addr_linklocal_allv2routers =
119 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
121 const struct in6_addr in6mask0 = IN6MASK0;
122 const struct in6_addr in6mask32 = IN6MASK32;
123 const struct in6_addr in6mask64 = IN6MASK64;
124 const struct in6_addr in6mask96 = IN6MASK96;
125 const struct in6_addr in6mask128 = IN6MASK128;
127 const struct sockaddr_in6 sa6_any =
128 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
130 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
131 struct ifnet *, struct thread *));
132 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
133 struct sockaddr_in6 *, int));
134 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
136 int (*faithprefix_p)(struct in6_addr *);
141 in6_mask2len(struct in6_addr *mask, u_char *lim0)
144 u_char *lim = lim0, *p;
146 /* ignore the scope_id part */
147 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
148 lim = (u_char *)mask + sizeof(*mask);
149 for (p = (u_char *)mask; p < lim; x++, p++) {
155 for (y = 0; y < 8; y++) {
156 if ((*p & (0x80 >> y)) == 0)
162 * when the limit pointer is given, do a stricter check on the
166 if (y != 0 && (*p & (0x00ff >> y)) != 0)
168 for (p = p + 1; p < lim; p++)
176 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
177 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
180 in6_control(struct socket *so, u_long cmd, caddr_t data,
181 struct ifnet *ifp, struct thread *td)
183 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
184 struct in6_ifaddr *ia = NULL;
185 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
186 struct sockaddr_in6 *sa6;
190 case SIOCGETSGCNT_IN6:
191 case SIOCGETMIFCNT_IN6:
192 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
196 case SIOCAADDRCTL_POLICY:
197 case SIOCDADDRCTL_POLICY:
199 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
203 return (in6_src_ioctl(cmd, data));
210 case SIOCSNDFLUSH_IN6:
211 case SIOCSPFXFLUSH_IN6:
212 case SIOCSRTRFLUSH_IN6:
213 case SIOCSDEFIFACE_IN6:
214 case SIOCSIFINFO_FLAGS:
215 case SIOCSIFINFO_IN6:
217 error = priv_check(td, PRIV_NETINET_ND6);
222 case OSIOCGIFINFO_IN6:
223 case SIOCGIFINFO_IN6:
226 case SIOCGNBRINFO_IN6:
227 case SIOCGDEFIFACE_IN6:
228 return (nd6_ioctl(cmd, data, ifp));
232 case SIOCSIFPREFIX_IN6:
233 case SIOCDIFPREFIX_IN6:
234 case SIOCAIFPREFIX_IN6:
235 case SIOCCIFPREFIX_IN6:
236 case SIOCSGIFPREFIX_IN6:
237 case SIOCGIFPREFIX_IN6:
239 "prefix ioctls are now invalidated. "
240 "please use ifconfig.\n");
247 error = priv_check(td, PRIV_NETINET_SCOPE6);
251 return (scope6_set(ifp,
252 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
254 return (scope6_get(ifp,
255 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
257 return (scope6_get_default((struct scope6_id *)
258 ifr->ifr_ifru.ifru_scope_id));
264 error = priv_check(td, PRIV_NET_ADDIFADDR);
268 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
272 error = priv_check(td, PRIV_NET_DELIFADDR);
278 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
282 * Find address for this interface, if it exists.
284 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
285 * only, and used the first interface address as the target of other
286 * operations (without checking ifra_addr). This was because netinet
287 * code/API assumed at most 1 interface address per interface.
288 * Since IPv6 allows a node to assign multiple addresses
289 * on a single interface, we almost always look and check the
290 * presence of ifra_addr, and reject invalid ones here.
291 * It also decreases duplicated code among SIOC*_IN6 operations.
294 case SIOCAIFADDR_IN6:
295 case SIOCSIFPHYADDR_IN6:
296 sa6 = &ifra->ifra_addr;
298 case SIOCSIFADDR_IN6:
299 case SIOCGIFADDR_IN6:
300 case SIOCSIFDSTADDR_IN6:
301 case SIOCSIFNETMASK_IN6:
302 case SIOCGIFDSTADDR_IN6:
303 case SIOCGIFNETMASK_IN6:
304 case SIOCDIFADDR_IN6:
305 case SIOCGIFPSRCADDR_IN6:
306 case SIOCGIFPDSTADDR_IN6:
307 case SIOCGIFAFLAG_IN6:
308 case SIOCSNDFLUSH_IN6:
309 case SIOCSPFXFLUSH_IN6:
310 case SIOCSRTRFLUSH_IN6:
311 case SIOCGIFALIFETIME_IN6:
312 case SIOCSIFALIFETIME_IN6:
313 case SIOCGIFSTAT_IN6:
314 case SIOCGIFSTAT_ICMP6:
315 sa6 = &ifr->ifr_addr;
321 if (sa6 && sa6->sin6_family == AF_INET6) {
322 if (sa6->sin6_scope_id != 0)
323 error = sa6_embedscope(sa6, 0);
325 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
328 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
329 &sa6->sin6_addr)) != 0)
331 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
336 case SIOCSIFADDR_IN6:
337 case SIOCSIFDSTADDR_IN6:
338 case SIOCSIFNETMASK_IN6:
340 * Since IPv6 allows a node to assign multiple addresses
341 * on a single interface, SIOCSIFxxx ioctls are deprecated.
343 /* we decided to obsolete this command (20000704) */
347 case SIOCDIFADDR_IN6:
349 * for IPv4, we look for existing in_ifaddr here to allow
350 * "ifconfig if0 delete" to remove the first IPv4 address on
351 * the interface. For IPv6, as the spec allows multiple
352 * interface address from the day one, we consider "remove the
353 * first one" semantics to be not preferable.
356 error = EADDRNOTAVAIL;
360 case SIOCAIFADDR_IN6:
362 * We always require users to specify a valid IPv6 address for
363 * the corresponding operation.
365 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
366 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
367 error = EAFNOSUPPORT;
372 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
373 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
379 case SIOCGIFADDR_IN6:
380 /* This interface is basically deprecated. use SIOCGIFCONF. */
382 case SIOCGIFAFLAG_IN6:
383 case SIOCGIFNETMASK_IN6:
384 case SIOCGIFDSTADDR_IN6:
385 case SIOCGIFALIFETIME_IN6:
386 /* must think again about its semantics */
388 error = EADDRNOTAVAIL;
393 case SIOCSIFALIFETIME_IN6:
395 struct in6_addrlifetime *lt;
398 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
403 error = EADDRNOTAVAIL;
406 /* sanity for overflow - beware unsigned */
407 lt = &ifr->ifr_ifru.ifru_lifetime;
408 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
409 lt->ia6t_vltime + time_second < time_second) {
413 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
414 lt->ia6t_pltime + time_second < time_second) {
423 case SIOCGIFADDR_IN6:
424 ifr->ifr_addr = ia->ia_addr;
425 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
429 case SIOCGIFDSTADDR_IN6:
430 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
435 * XXX: should we check if ifa_dstaddr is NULL and return
438 ifr->ifr_dstaddr = ia->ia_dstaddr;
439 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
443 case SIOCGIFNETMASK_IN6:
444 ifr->ifr_addr = ia->ia_prefixmask;
447 case SIOCGIFAFLAG_IN6:
448 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
451 case SIOCGIFSTAT_IN6:
456 bzero(&ifr->ifr_ifru.ifru_stat,
457 sizeof(ifr->ifr_ifru.ifru_stat));
458 ifr->ifr_ifru.ifru_stat =
459 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
462 case SIOCGIFSTAT_ICMP6:
467 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
468 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
469 ifr->ifr_ifru.ifru_icmp6stat =
470 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
473 case SIOCGIFALIFETIME_IN6:
474 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
475 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
477 struct in6_addrlifetime *retlt =
478 &ifr->ifr_ifru.ifru_lifetime;
481 * XXX: adjust expiration time assuming time_t is
485 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
486 if (ia->ia6_lifetime.ia6t_vltime <
487 maxexpire - ia->ia6_updatetime) {
488 retlt->ia6t_expire = ia->ia6_updatetime +
489 ia->ia6_lifetime.ia6t_vltime;
491 retlt->ia6t_expire = maxexpire;
493 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
495 struct in6_addrlifetime *retlt =
496 &ifr->ifr_ifru.ifru_lifetime;
499 * XXX: adjust expiration time assuming time_t is
503 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
504 if (ia->ia6_lifetime.ia6t_pltime <
505 maxexpire - ia->ia6_updatetime) {
506 retlt->ia6t_preferred = ia->ia6_updatetime +
507 ia->ia6_lifetime.ia6t_pltime;
509 retlt->ia6t_preferred = maxexpire;
513 case SIOCSIFALIFETIME_IN6:
514 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
516 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
517 ia->ia6_lifetime.ia6t_expire =
518 time_second + ia->ia6_lifetime.ia6t_vltime;
520 ia->ia6_lifetime.ia6t_expire = 0;
521 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
522 ia->ia6_lifetime.ia6t_preferred =
523 time_second + ia->ia6_lifetime.ia6t_pltime;
525 ia->ia6_lifetime.ia6t_preferred = 0;
528 case SIOCAIFADDR_IN6:
531 struct nd_prefixctl pr0;
532 struct nd_prefix *pr;
535 * first, make or update the interface address structure,
536 * and link it to the list.
538 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
541 ifa_free(&ia->ia_ifa);
542 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
545 * this can happen when the user specify the 0 valid
552 * then, make the prefix on-link on the interface.
553 * XXX: we'd rather create the prefix before the address, but
554 * we need at least one address to install the corresponding
555 * interface route, so we configure the address first.
559 * convert mask to prefix length (prefixmask has already
560 * been validated in in6_update_ifa().
562 bzero(&pr0, sizeof(pr0));
564 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
566 if (pr0.ndpr_plen == 128) {
567 break; /* we don't need to install a host route. */
569 pr0.ndpr_prefix = ifra->ifra_addr;
570 /* apply the mask for safety. */
571 for (i = 0; i < 4; i++) {
572 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
573 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
576 * XXX: since we don't have an API to set prefix (not address)
577 * lifetimes, we just use the same lifetimes as addresses.
578 * The (temporarily) installed lifetimes can be overridden by
579 * later advertised RAs (when accept_rtadv is non 0), which is
580 * an intended behavior.
582 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
584 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
585 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
586 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
588 /* add the prefix if not yet. */
589 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
591 * nd6_prelist_add will install the corresponding
594 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
597 log(LOG_ERR, "nd6_prelist_add succeeded but "
604 /* relate the address to the prefix */
605 if (ia->ia6_ndpr == NULL) {
610 * If this is the first autoconf address from the
611 * prefix, create a temporary address as well
614 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
615 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
617 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
618 log(LOG_NOTICE, "in6_control: failed "
619 "to create a temporary address, "
626 * this might affect the status of autoconfigured addresses,
627 * that is, this address might make other addresses detached.
629 pfxlist_onlink_check();
630 if (error == 0 && ia)
631 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
635 case SIOCDIFADDR_IN6:
637 struct nd_prefix *pr;
640 * If the address being deleted is the only one that owns
641 * the corresponding prefix, expire the prefix as well.
642 * XXX: theoretically, we don't have to worry about such
643 * relationship, since we separate the address management
644 * and the prefix management. We do this, however, to provide
645 * as much backward compatibility as possible in terms of
646 * the ioctl operation.
647 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
650 in6_purgeaddr(&ia->ia_ifa);
651 if (pr && pr->ndpr_refcnt == 0)
653 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
658 if (ifp == NULL || ifp->if_ioctl == 0) {
662 error = (*ifp->if_ioctl)(ifp, cmd, data);
669 ifa_free(&ia->ia_ifa);
674 * Update parameters of an IPv6 interface address.
675 * If necessary, a new entry is created and linked into address chains.
676 * This function is separated from in6_control().
677 * XXX: should this be performed under splnet()?
680 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
681 struct in6_ifaddr *ia, int flags)
683 int error = 0, hostIsNew = 0, plen = -1;
684 struct sockaddr_in6 dst6;
685 struct in6_addrlifetime *lt;
686 struct in6_multi_mship *imm;
687 struct in6_multi *in6m_sol;
690 char ip6buf[INET6_ADDRSTRLEN];
692 /* Validate parameters */
693 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
697 * The destination address for a p2p link must have a family
698 * of AF_UNSPEC or AF_INET6.
700 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
701 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
702 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
703 return (EAFNOSUPPORT);
705 * validate ifra_prefixmask. don't check sin6_family, netmask
706 * does not carry fields other than sin6_len.
708 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
711 * Because the IPv6 address architecture is classless, we require
712 * users to specify a (non 0) prefix length (mask) for a new address.
713 * We also require the prefix (when specified) mask is valid, and thus
714 * reject a non-consecutive mask.
716 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
718 if (ifra->ifra_prefixmask.sin6_len != 0) {
719 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
720 (u_char *)&ifra->ifra_prefixmask +
721 ifra->ifra_prefixmask.sin6_len);
726 * In this case, ia must not be NULL. We just use its prefix
729 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
732 * If the destination address on a p2p interface is specified,
733 * and the address is a scoped one, validate/set the scope
736 dst6 = ifra->ifra_dstaddr;
737 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
738 (dst6.sin6_family == AF_INET6)) {
739 struct in6_addr in6_tmp;
742 in6_tmp = dst6.sin6_addr;
743 if (in6_setscope(&in6_tmp, ifp, &zoneid))
744 return (EINVAL); /* XXX: should be impossible */
746 if (dst6.sin6_scope_id != 0) {
747 if (dst6.sin6_scope_id != zoneid)
749 } else /* user omit to specify the ID. */
750 dst6.sin6_scope_id = zoneid;
752 /* convert into the internal form */
753 if (sa6_embedscope(&dst6, 0))
754 return (EINVAL); /* XXX: should be impossible */
757 * The destination address can be specified only for a p2p or a
758 * loopback interface. If specified, the corresponding prefix length
761 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
762 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
763 /* XXX: noisy message */
764 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
765 "be specified for a p2p or a loopback IF only\n"));
769 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
770 "be 128 when dstaddr is specified\n"));
774 /* lifetime consistency check */
775 lt = &ifra->ifra_lifetime;
776 if (lt->ia6t_pltime > lt->ia6t_vltime)
778 if (lt->ia6t_vltime == 0) {
780 * the following log might be noisy, but this is a typical
781 * configuration mistake or a tool's bug.
784 "in6_update_ifa: valid lifetime is 0 for %s\n",
785 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
788 return (0); /* there's nothing to do */
792 * If this is a new address, allocate a new ifaddr and link it
798 * When in6_update_ifa() is called in a process of a received
799 * RA, it is called under an interrupt context. So, we should
800 * call malloc with M_NOWAIT.
802 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
806 bzero((caddr_t)ia, sizeof(*ia));
807 ifa_init(&ia->ia_ifa);
808 LIST_INIT(&ia->ia6_memberships);
809 /* Initialize the address and masks, and put time stamp */
810 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
811 ia->ia_addr.sin6_family = AF_INET6;
812 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
813 ia->ia6_createtime = time_second;
814 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
816 * XXX: some functions expect that ifa_dstaddr is not
817 * NULL for p2p interfaces.
819 ia->ia_ifa.ifa_dstaddr =
820 (struct sockaddr *)&ia->ia_dstaddr;
822 ia->ia_ifa.ifa_dstaddr = NULL;
824 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
826 ifa_ref(&ia->ia_ifa); /* if_addrhead */
828 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
831 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
833 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
834 IN6_IFADDR_WUNLOCK();
837 /* update timestamp */
838 ia->ia6_updatetime = time_second;
840 /* set prefix mask */
841 if (ifra->ifra_prefixmask.sin6_len) {
843 * We prohibit changing the prefix length of an existing
845 * + such an operation should be rare in IPv6, and
846 * + the operation would confuse prefix management.
848 if (ia->ia_prefixmask.sin6_len &&
849 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
850 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
851 " existing (%s) address should not be changed\n",
852 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
856 ia->ia_prefixmask = ifra->ifra_prefixmask;
860 * If a new destination address is specified, scrub the old one and
861 * install the new destination. Note that the interface must be
862 * p2p or loopback (see the check above.)
864 if (dst6.sin6_family == AF_INET6 &&
865 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
868 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
869 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
870 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
871 "a route to the old destination: %s\n",
872 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
873 /* proceed anyway... */
875 ia->ia_flags &= ~IFA_ROUTE;
876 ia->ia_dstaddr = dst6;
880 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
881 * to see if the address is deprecated or invalidated, but initialize
882 * these members for applications.
884 ia->ia6_lifetime = ifra->ifra_lifetime;
885 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
886 ia->ia6_lifetime.ia6t_expire =
887 time_second + ia->ia6_lifetime.ia6t_vltime;
889 ia->ia6_lifetime.ia6t_expire = 0;
890 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
891 ia->ia6_lifetime.ia6t_preferred =
892 time_second + ia->ia6_lifetime.ia6t_pltime;
894 ia->ia6_lifetime.ia6t_preferred = 0;
896 /* reset the interface and routing table appropriately. */
897 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
901 * configure address flags.
903 ia->ia6_flags = ifra->ifra_flags;
905 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
906 * userland, make it deprecated.
908 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
909 ia->ia6_lifetime.ia6t_pltime = 0;
910 ia->ia6_lifetime.ia6t_preferred = time_second;
913 * Make the address tentative before joining multicast addresses,
914 * so that corresponding MLD responses would not have a tentative
917 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
918 if (hostIsNew && in6if_do_dad(ifp))
919 ia->ia6_flags |= IN6_IFF_TENTATIVE;
922 * We are done if we have simply modified an existing address.
928 * Beyond this point, we should call in6_purgeaddr upon an error,
929 * not just go to unlink.
932 /* Join necessary multicast groups */
934 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
935 struct sockaddr_in6 mltaddr, mltmask;
936 struct in6_addr llsol;
938 /* join solicited multicast addr for new host id */
939 bzero(&llsol, sizeof(struct in6_addr));
940 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
941 llsol.s6_addr32[1] = 0;
942 llsol.s6_addr32[2] = htonl(1);
943 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
944 llsol.s6_addr8[12] = 0xff;
945 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
946 /* XXX: should not happen */
947 log(LOG_ERR, "in6_update_ifa: "
948 "in6_setscope failed\n");
952 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
954 * We need a random delay for DAD on the address
955 * being configured. It also means delaying
956 * transmission of the corresponding MLD report to
957 * avoid report collision.
958 * [draft-ietf-ipv6-rfc2462bis-02.txt]
960 delay = arc4random() %
961 (MAX_RTR_SOLICITATION_DELAY * hz);
963 imm = in6_joingroup(ifp, &llsol, &error, delay);
966 "in6_update_ifa: addmulti failed for "
967 "%s on %s (errno=%d)\n",
968 ip6_sprintf(ip6buf, &llsol), if_name(ifp),
972 LIST_INSERT_HEAD(&ia->ia6_memberships,
974 in6m_sol = imm->i6mm_maddr;
976 bzero(&mltmask, sizeof(mltmask));
977 mltmask.sin6_len = sizeof(struct sockaddr_in6);
978 mltmask.sin6_family = AF_INET6;
979 mltmask.sin6_addr = in6mask32;
980 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
983 * join link-local all-nodes address
985 bzero(&mltaddr, sizeof(mltaddr));
986 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
987 mltaddr.sin6_family = AF_INET6;
988 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
989 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
991 goto cleanup; /* XXX: should not fail */
994 * XXX: do we really need this automatic routes?
995 * We should probably reconsider this stuff. Most applications
996 * actually do not need the routes, since they usually specify
997 * the outgoing interface.
999 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1001 /* XXX: only works in !SCOPEDROUTING case. */
1002 if (memcmp(&mltaddr.sin6_addr,
1003 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1010 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1011 (struct sockaddr *)&ia->ia_addr,
1012 (struct sockaddr *)&mltmask, RTF_UP,
1013 (struct rtentry **)0);
1020 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1022 nd6log((LOG_WARNING,
1023 "in6_update_ifa: addmulti failed for "
1024 "%s on %s (errno=%d)\n",
1025 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1026 if_name(ifp), error));
1029 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1032 * join node information group address
1035 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1037 * The spec doesn't say anything about delay for this
1038 * group, but the same logic should apply.
1040 delay = arc4random() %
1041 (MAX_RTR_SOLICITATION_DELAY * hz);
1043 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
1044 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1045 delay); /* XXX jinmei */
1047 nd6log((LOG_WARNING, "in6_update_ifa: "
1048 "addmulti failed for %s on %s "
1050 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1051 if_name(ifp), error));
1052 /* XXX not very fatal, go on... */
1054 LIST_INSERT_HEAD(&ia->ia6_memberships,
1060 * join interface-local all-nodes address.
1061 * (ff01::1%ifN, and ff01::%ifN/32)
1063 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1064 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1066 goto cleanup; /* XXX: should not fail */
1067 /* XXX: again, do we really need the route? */
1068 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1070 if (memcmp(&mltaddr.sin6_addr,
1071 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1078 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1079 (struct sockaddr *)&ia->ia_addr,
1080 (struct sockaddr *)&mltmask, RTF_UP,
1081 (struct rtentry **)0);
1087 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1089 nd6log((LOG_WARNING, "in6_update_ifa: "
1090 "addmulti failed for %s on %s "
1092 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1093 if_name(ifp), error));
1096 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1101 * Perform DAD, if needed.
1102 * XXX It may be of use, if we can administratively
1105 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1106 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1108 int mindelay, maxdelay;
1111 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1113 * We need to impose a delay before sending an NS
1114 * for DAD. Check if we also needed a delay for the
1115 * corresponding MLD message. If we did, the delay
1116 * should be larger than the MLD delay (this could be
1117 * relaxed a bit, but this simple logic is at least
1119 * XXX: Break data hiding guidelines and look at
1120 * state for the solicited multicast group.
1123 if (in6m_sol != NULL &&
1124 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1125 mindelay = in6m_sol->in6m_timer;
1127 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1128 if (maxdelay - mindelay == 0)
1132 (arc4random() % (maxdelay - mindelay)) +
1136 nd6_dad_start((struct ifaddr *)ia, delay);
1139 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1140 ifa_free(&ia->ia_ifa);
1145 * XXX: if a change of an existing address failed, keep the entry
1149 in6_unlink_ifa(ia, ifp);
1150 ifa_free(&ia->ia_ifa);
1155 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1156 ifa_free(&ia->ia_ifa);
1157 in6_purgeaddr(&ia->ia_ifa);
1162 in6_purgeaddr(struct ifaddr *ifa)
1164 struct ifnet *ifp = ifa->ifa_ifp;
1165 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1166 struct in6_multi_mship *imm;
1167 struct sockaddr_in6 mltaddr, mltmask;
1169 struct sockaddr_dl gateway;
1170 struct sockaddr_in6 mask, addr;
1173 struct ifaddr *ifa0, *nifa;
1176 * find another IPv6 address as the gateway for the
1177 * link-local and node-local all-nodes multicast
1181 TAILQ_FOREACH_SAFE(ifa0, &ifp->if_addrhead, ifa_link, nifa) {
1182 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1183 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1184 &ia->ia_addr.sin6_addr,
1185 sizeof(struct in6_addr)) == 0)
1192 IF_ADDR_UNLOCK(ifp);
1195 * Remove the loopback route to the interface address.
1196 * The check for the current setting of "nd6_useloopback"
1199 error = ifa_del_loopback_route((struct ifaddr *)ia,
1200 (struct sockaddr *)&ia->ia_addr);
1202 /* stop DAD processing */
1205 IF_AFDATA_LOCK(ifp);
1206 lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
1207 (struct sockaddr *)&ia->ia_addr);
1208 IF_AFDATA_UNLOCK(ifp);
1211 * initialize for rtmsg generation
1213 bzero(&gateway, sizeof(gateway));
1214 gateway.sdl_len = sizeof(gateway);
1215 gateway.sdl_family = AF_LINK;
1216 gateway.sdl_nlen = 0;
1217 gateway.sdl_alen = ifp->if_addrlen;
1219 bzero(&rt0, sizeof(rt0));
1220 rt0.rt_gateway = (struct sockaddr *)&gateway;
1221 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1222 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1223 rt_mask(&rt0) = (struct sockaddr *)&mask;
1224 rt_key(&rt0) = (struct sockaddr *)&addr;
1225 rt0.rt_flags = RTF_HOST | RTF_STATIC;
1226 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
1229 * leave from multicast groups we have joined for the interface
1231 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1232 LIST_REMOVE(imm, i6mm_chain);
1233 in6_leavegroup(imm);
1237 * remove the link-local all-nodes address
1239 bzero(&mltmask, sizeof(mltmask));
1240 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1241 mltmask.sin6_family = AF_INET6;
1242 mltmask.sin6_addr = in6mask32;
1244 bzero(&mltaddr, sizeof(mltaddr));
1245 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1246 mltaddr.sin6_family = AF_INET6;
1247 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1249 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1253 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1254 if (rt != NULL && rt->rt_gateway != NULL &&
1255 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1256 &ia->ia_addr.sin6_addr,
1257 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1259 * if no more IPv6 address exists on this interface
1260 * then remove the multicast address route
1263 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1264 sizeof(mltaddr.sin6_addr));
1266 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1267 (struct sockaddr *)&ia->ia_addr,
1268 (struct sockaddr *)&mltmask, RTF_UP,
1269 (struct rtentry **)0);
1271 log(LOG_INFO, "in6_purgeaddr: link-local all-nodes"
1272 "multicast address deletion error\n");
1275 * replace the gateway of the route
1277 struct sockaddr_in6 sa;
1279 bzero(&sa, sizeof(sa));
1280 sa.sin6_len = sizeof(struct sockaddr_in6);
1281 sa.sin6_family = AF_INET6;
1282 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1283 sizeof(sa.sin6_addr));
1284 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1285 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1294 * remove the node-local all-nodes address
1296 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1297 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1301 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1302 if (rt != NULL && rt->rt_gateway != NULL &&
1303 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1304 &ia->ia_addr.sin6_addr,
1305 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1307 * if no more IPv6 address exists on this interface
1308 * then remove the multicast address route
1311 memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1312 sizeof(mltaddr.sin6_addr));
1315 error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1316 (struct sockaddr *)&ia->ia_addr,
1317 (struct sockaddr *)&mltmask, RTF_UP,
1318 (struct rtentry **)0);
1321 log(LOG_INFO, "in6_purgeaddr: node-local all-nodes"
1322 "multicast address deletion error\n");
1325 * replace the gateway of the route
1327 struct sockaddr_in6 sa;
1329 bzero(&sa, sizeof(sa));
1330 sa.sin6_len = sizeof(struct sockaddr_in6);
1331 sa.sin6_family = AF_INET6;
1332 memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1333 sizeof(sa.sin6_addr));
1334 in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1335 memcpy(rt->rt_gateway, &sa, sizeof(sa));
1345 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1346 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1348 struct sockaddr *dstaddr;
1351 * use the interface address if configuring an
1352 * interface address with a /128 prefix len
1354 if (ia->ia_dstaddr.sin6_family == AF_INET6)
1355 dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1357 dstaddr = (struct sockaddr *)&ia->ia_addr;
1359 error = rtrequest(RTM_DELETE,
1360 (struct sockaddr *)dstaddr,
1361 (struct sockaddr *)&ia->ia_addr,
1362 (struct sockaddr *)&ia->ia_prefixmask,
1363 ia->ia_flags | RTF_HOST, NULL);
1366 ia->ia_flags &= ~IFA_ROUTE;
1371 in6_unlink_ifa(ia, ifp);
1375 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1380 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1381 IF_ADDR_UNLOCK(ifp);
1382 ifa_free(&ia->ia_ifa); /* if_addrhead */
1385 * Defer the release of what might be the last reference to the
1386 * in6_ifaddr so that it can't be freed before the remainder of the
1390 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1391 IN6_IFADDR_WUNLOCK();
1394 * Release the reference to the base prefix. There should be a
1395 * positive reference.
1397 if (ia->ia6_ndpr == NULL) {
1399 "in6_unlink_ifa: autoconf'ed address "
1400 "%p has no prefix\n", ia));
1402 ia->ia6_ndpr->ndpr_refcnt--;
1403 ia->ia6_ndpr = NULL;
1407 * Also, if the address being removed is autoconf'ed, call
1408 * pfxlist_onlink_check() since the release might affect the status of
1409 * other (detached) addresses.
1411 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1412 pfxlist_onlink_check();
1414 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1419 in6_purgeif(struct ifnet *ifp)
1421 struct ifaddr *ifa, *nifa;
1423 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1424 if (ifa->ifa_addr->sa_family != AF_INET6)
1434 * SIOCGLIFADDR: get first address. (?)
1435 * SIOCGLIFADDR with IFLR_PREFIX:
1436 * get first address that matches the specified prefix.
1437 * SIOCALIFADDR: add the specified address.
1438 * SIOCALIFADDR with IFLR_PREFIX:
1439 * add the specified prefix, filling hostid part from
1440 * the first link-local address. prefixlen must be <= 64.
1441 * SIOCDLIFADDR: delete the specified address.
1442 * SIOCDLIFADDR with IFLR_PREFIX:
1443 * delete the first address that matches the specified prefix.
1445 * EINVAL on invalid parameters
1446 * EADDRNOTAVAIL on prefix match failed/specified address not found
1447 * other values may be returned from in6_ioctl()
1449 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1450 * this is to accomodate address naming scheme other than RFC2374,
1452 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1453 * address encoding scheme. (see figure on page 8)
1456 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1457 struct ifnet *ifp, struct thread *td)
1459 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1461 struct sockaddr *sa;
1464 if (!data || !ifp) {
1465 panic("invalid argument to in6_lifaddr_ioctl");
1471 /* address must be specified on GET with IFLR_PREFIX */
1472 if ((iflr->flags & IFLR_PREFIX) == 0)
1477 /* address must be specified on ADD and DELETE */
1478 sa = (struct sockaddr *)&iflr->addr;
1479 if (sa->sa_family != AF_INET6)
1481 if (sa->sa_len != sizeof(struct sockaddr_in6))
1483 /* XXX need improvement */
1484 sa = (struct sockaddr *)&iflr->dstaddr;
1485 if (sa->sa_family && sa->sa_family != AF_INET6)
1487 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1490 default: /* shouldn't happen */
1492 panic("invalid cmd to in6_lifaddr_ioctl");
1498 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1504 struct in6_aliasreq ifra;
1505 struct in6_addr *hostid = NULL;
1509 if ((iflr->flags & IFLR_PREFIX) != 0) {
1510 struct sockaddr_in6 *sin6;
1513 * hostid is to fill in the hostid part of the
1514 * address. hostid points to the first link-local
1515 * address attached to the interface.
1517 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1519 return EADDRNOTAVAIL;
1520 hostid = IFA_IN6(ifa);
1522 /* prefixlen must be <= 64. */
1523 if (64 < iflr->prefixlen)
1525 prefixlen = iflr->prefixlen;
1527 /* hostid part must be zero. */
1528 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1529 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1530 sin6->sin6_addr.s6_addr32[3] != 0) {
1534 prefixlen = iflr->prefixlen;
1536 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1537 bzero(&ifra, sizeof(ifra));
1538 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1540 bcopy(&iflr->addr, &ifra.ifra_addr,
1541 ((struct sockaddr *)&iflr->addr)->sa_len);
1543 /* fill in hostid part */
1544 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1545 hostid->s6_addr32[2];
1546 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1547 hostid->s6_addr32[3];
1550 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1551 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1552 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1554 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1555 hostid->s6_addr32[2];
1556 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1557 hostid->s6_addr32[3];
1563 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1564 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1566 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1567 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1572 struct in6_ifaddr *ia;
1573 struct in6_addr mask, candidate, match;
1574 struct sockaddr_in6 *sin6;
1577 bzero(&mask, sizeof(mask));
1578 if (iflr->flags & IFLR_PREFIX) {
1579 /* lookup a prefix rather than address. */
1580 in6_prefixlen2mask(&mask, iflr->prefixlen);
1582 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1583 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1584 match.s6_addr32[0] &= mask.s6_addr32[0];
1585 match.s6_addr32[1] &= mask.s6_addr32[1];
1586 match.s6_addr32[2] &= mask.s6_addr32[2];
1587 match.s6_addr32[3] &= mask.s6_addr32[3];
1589 /* if you set extra bits, that's wrong */
1590 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1595 if (cmd == SIOCGLIFADDR) {
1596 /* on getting an address, take the 1st match */
1599 /* on deleting an address, do exact match */
1600 in6_prefixlen2mask(&mask, 128);
1601 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1602 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1609 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1610 if (ifa->ifa_addr->sa_family != AF_INET6)
1616 * XXX: this is adhoc, but is necessary to allow
1617 * a user to specify fe80::/64 (not /10) for a
1618 * link-local address.
1620 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1621 in6_clearscope(&candidate);
1622 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1623 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1624 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1625 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1626 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1629 IF_ADDR_UNLOCK(ifp);
1631 return EADDRNOTAVAIL;
1634 if (cmd == SIOCGLIFADDR) {
1637 /* fill in the if_laddrreq structure */
1638 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1639 error = sa6_recoverscope(
1640 (struct sockaddr_in6 *)&iflr->addr);
1644 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1645 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1646 ia->ia_dstaddr.sin6_len);
1647 error = sa6_recoverscope(
1648 (struct sockaddr_in6 *)&iflr->dstaddr);
1652 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1655 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1657 iflr->flags = ia->ia6_flags; /* XXX */
1661 struct in6_aliasreq ifra;
1663 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1664 bzero(&ifra, sizeof(ifra));
1665 bcopy(iflr->iflr_name, ifra.ifra_name,
1666 sizeof(ifra.ifra_name));
1668 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1669 ia->ia_addr.sin6_len);
1670 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1671 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1672 ia->ia_dstaddr.sin6_len);
1674 bzero(&ifra.ifra_dstaddr,
1675 sizeof(ifra.ifra_dstaddr));
1677 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1678 ia->ia_prefixmask.sin6_len);
1680 ifra.ifra_flags = ia->ia6_flags;
1681 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1687 return EOPNOTSUPP; /* just for safety */
1691 * Initialize an interface's intetnet6 address
1692 * and routing table entry.
1695 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1696 struct sockaddr_in6 *sin6, int newhost)
1698 int error = 0, plen, ifacount = 0;
1703 * Give the interface a chance to initialize
1704 * if this is its first address,
1705 * and to validate the address if necessary.
1708 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1709 if (ifa->ifa_addr->sa_family != AF_INET6)
1713 IF_ADDR_UNLOCK(ifp);
1715 ia->ia_addr = *sin6;
1717 if (ifacount <= 1 && ifp->if_ioctl) {
1718 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1726 ia->ia_ifa.ifa_metric = ifp->if_metric;
1728 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1732 * If a new destination address is specified for a point-to-point
1733 * interface, install a route to the destination as an interface
1735 * XXX: the logic below rejects assigning multiple addresses on a p2p
1736 * interface that share the same destination.
1738 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1739 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1740 ia->ia_dstaddr.sin6_family == AF_INET6) {
1741 int rtflags = RTF_UP | RTF_HOST;
1743 error = rtrequest(RTM_ADD,
1744 (struct sockaddr *)&ia->ia_dstaddr,
1745 (struct sockaddr *)&ia->ia_addr,
1746 (struct sockaddr *)&ia->ia_prefixmask,
1747 ia->ia_flags | rtflags, NULL);
1750 ia->ia_flags |= IFA_ROUTE;
1754 * add a loopback route to self
1756 if (!(ia->ia_flags & IFA_ROUTE)
1757 && (V_nd6_useloopback
1758 || (ifp->if_flags & IFF_LOOPBACK))) {
1759 error = ifa_add_loopback_route((struct ifaddr *)ia,
1760 (struct sockaddr *)&ia->ia_addr);
1763 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1767 struct sockaddr_dl gateway;
1768 struct sockaddr_in6 mask, addr;
1770 IF_AFDATA_LOCK(ifp);
1771 ia->ia_ifa.ifa_rtrequest = NULL;
1774 * we need to report rt_newaddrmsg
1776 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE),
1777 (struct sockaddr *)&ia->ia_addr);
1778 IF_AFDATA_UNLOCK(ifp);
1780 ln->la_expire = 0; /* for IPv6 this means permanent */
1781 ln->ln_state = ND6_LLINFO_REACHABLE;
1783 * initialize for rtmsg generation
1785 bzero(&gateway, sizeof(gateway));
1786 gateway.sdl_len = sizeof(gateway);
1787 gateway.sdl_family = AF_LINK;
1788 gateway.sdl_nlen = 0;
1789 gateway.sdl_alen = 6;
1790 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr));
1795 bzero(&rt, sizeof(rt));
1796 rt.rt_gateway = (struct sockaddr *)&gateway;
1797 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1798 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1799 rt_mask(&rt) = (struct sockaddr *)&mask;
1800 rt_key(&rt) = (struct sockaddr *)&addr;
1801 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
1802 rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt);
1809 * Find an IPv6 interface link-local address specific to an interface.
1810 * ifaddr is returned referenced.
1813 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1818 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1819 if (ifa->ifa_addr->sa_family != AF_INET6)
1821 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1822 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1829 IF_ADDR_UNLOCK(ifp);
1831 return ((struct in6_ifaddr *)ifa);
1836 * find the internet address corresponding to a given interface and address.
1837 * ifaddr is returned referenced.
1840 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1845 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1846 if (ifa->ifa_addr->sa_family != AF_INET6)
1848 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1853 IF_ADDR_UNLOCK(ifp);
1855 return ((struct in6_ifaddr *)ifa);
1859 * Convert IP6 address to printable (loggable) representation. Caller
1860 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1862 static char digits[] = "0123456789abcdef";
1864 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1868 const u_int16_t *a = (const u_int16_t *)addr;
1870 int dcolon = 0, zero = 0;
1874 for (i = 0; i < 8; i++) {
1885 if (dcolon == 0 && *(a + 1) == 0) {
1897 d = (const u_char *)a;
1898 /* Try to eliminate leading zeros in printout like in :0001. */
1900 *cp = digits[*d >> 4];
1905 *cp = digits[*d++ & 0xf];
1906 if (zero == 0 || (*cp != '0')) {
1910 *cp = digits[*d >> 4];
1911 if (zero == 0 || (*cp != '0')) {
1915 *cp++ = digits[*d & 0xf];
1924 in6_localaddr(struct in6_addr *in6)
1926 struct in6_ifaddr *ia;
1928 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1932 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1933 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1934 &ia->ia_prefixmask.sin6_addr)) {
1935 IN6_IFADDR_RUNLOCK();
1939 IN6_IFADDR_RUNLOCK();
1945 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1947 struct in6_ifaddr *ia;
1950 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1951 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1953 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
1954 IN6_IFADDR_RUNLOCK();
1955 return (1); /* true */
1958 /* XXX: do we still have to go thru the rest of the list? */
1960 IN6_IFADDR_RUNLOCK();
1962 return (0); /* false */
1966 * return length of part which dst and src are equal
1970 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1973 u_char *s = (u_char *)src, *d = (u_char *)dst;
1974 u_char *lim = s + 16, r;
1977 if ((r = (*d++ ^ *s++)) != 0) {
1988 /* XXX: to be scope conscious */
1990 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1992 int bytelen, bitlen;
1995 if (0 > len || len > 128) {
1996 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2004 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2007 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2008 p2->s6_addr[bytelen] >> (8 - bitlen))
2015 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2017 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2018 int bytelen, bitlen, i;
2021 if (0 > len || len > 128) {
2022 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2027 bzero(maskp, sizeof(*maskp));
2030 for (i = 0; i < bytelen; i++)
2031 maskp->s6_addr[i] = 0xff;
2033 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2037 * return the best address out of the same scope. if no address was
2038 * found, return the first valid address from designated IF.
2041 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2043 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2045 struct in6_ifaddr *besta = 0;
2046 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2048 dep[0] = dep[1] = NULL;
2051 * We first look for addresses in the same scope.
2052 * If there is one, return it.
2053 * If two or more, return one which matches the dst longest.
2054 * If none, return one of global addresses assigned other ifs.
2057 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2058 if (ifa->ifa_addr->sa_family != AF_INET6)
2060 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2061 continue; /* XXX: is there any case to allow anycast? */
2062 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2063 continue; /* don't use this interface */
2064 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2066 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2067 if (V_ip6_use_deprecated)
2068 dep[0] = (struct in6_ifaddr *)ifa;
2072 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2074 * call in6_matchlen() as few as possible
2078 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2079 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2082 besta = (struct in6_ifaddr *)ifa;
2085 besta = (struct in6_ifaddr *)ifa;
2089 ifa_ref(&besta->ia_ifa);
2090 IF_ADDR_UNLOCK(ifp);
2093 IF_ADDR_UNLOCK(ifp);
2096 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2097 if (ifa->ifa_addr->sa_family != AF_INET6)
2099 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2100 continue; /* XXX: is there any case to allow anycast? */
2101 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2102 continue; /* don't use this interface */
2103 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2105 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2106 if (V_ip6_use_deprecated)
2107 dep[1] = (struct in6_ifaddr *)ifa;
2113 IN6_IFADDR_RUNLOCK();
2114 return (struct in6_ifaddr *)ifa;
2116 IN6_IFADDR_RUNLOCK();
2118 /* use the last-resort values, that are, deprecated addresses */
2128 * perform DAD when interface becomes IFF_UP.
2131 in6_if_up(struct ifnet *ifp)
2134 struct in6_ifaddr *ia;
2137 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2138 if (ifa->ifa_addr->sa_family != AF_INET6)
2140 ia = (struct in6_ifaddr *)ifa;
2141 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2143 * The TENTATIVE flag was likely set by hand
2144 * beforehand, implicitly indicating the need for DAD.
2145 * We may be able to skip the random delay in this
2146 * case, but we impose delays just in case.
2149 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2152 IF_ADDR_UNLOCK(ifp);
2155 * special cases, like 6to4, are handled in in6_ifattach
2157 in6_ifattach(ifp, NULL);
2161 in6if_do_dad(struct ifnet *ifp)
2163 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2166 switch (ifp->if_type) {
2172 * These interfaces do not have the IFF_LOOPBACK flag,
2173 * but loop packets back. We do not have to do DAD on such
2174 * interfaces. We should even omit it, because loop-backed
2175 * NS would confuse the DAD procedure.
2180 * Our DAD routine requires the interface up and running.
2181 * However, some interfaces can be up before the RUNNING
2182 * status. Additionaly, users may try to assign addresses
2183 * before the interface becomes up (or running).
2184 * We simply skip DAD in such a case as a work around.
2185 * XXX: we should rather mark "tentative" on such addresses,
2186 * and do DAD after the interface becomes ready.
2188 if (!((ifp->if_flags & IFF_UP) &&
2189 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2197 * Calculate max IPv6 MTU through all the interfaces and store it
2203 unsigned long maxmtu = 0;
2206 IFNET_RLOCK_NOSLEEP();
2207 for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
2208 ifp = TAILQ_NEXT(ifp, if_list)) {
2209 /* this function can be called during ifnet initialization */
2210 if (!ifp->if_afdata[AF_INET6])
2212 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2213 IN6_LINKMTU(ifp) > maxmtu)
2214 maxmtu = IN6_LINKMTU(ifp);
2216 IFNET_RUNLOCK_NOSLEEP();
2217 if (maxmtu) /* update only when maxmtu is positive */
2218 V_in6_maxmtu = maxmtu;
2222 * Provide the length of interface identifiers to be used for the link attached
2223 * to the given interface. The length should be defined in "IPv6 over
2224 * xxx-link" document. Note that address architecture might also define
2225 * the length for a particular set of address prefixes, regardless of the
2226 * link type. As clarified in rfc2462bis, those two definitions should be
2227 * consistent, and those really are as of August 2004.
2230 in6_if2idlen(struct ifnet *ifp)
2232 switch (ifp->if_type) {
2233 case IFT_ETHER: /* RFC2464 */
2234 #ifdef IFT_PROPVIRTUAL
2235 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2238 case IFT_L2VLAN: /* ditto */
2240 #ifdef IFT_IEEE80211
2241 case IFT_IEEE80211: /* ditto */
2244 case IFT_MIP: /* ditto */
2247 case IFT_FDDI: /* RFC2467 */
2249 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2251 case IFT_PPP: /* RFC2472 */
2253 case IFT_ARCNET: /* RFC2497 */
2255 case IFT_FRELAY: /* RFC2590 */
2257 case IFT_IEEE1394: /* RFC3146 */
2260 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2262 return (64); /* XXX: is this really correct? */
2265 * Unknown link type:
2266 * It might be controversial to use the today's common constant
2267 * of 64 for these cases unconditionally. For full compliance,
2268 * we should return an error in this case. On the other hand,
2269 * if we simply miss the standard for the link type or a new
2270 * standard is defined for a new link type, the IFID length
2271 * is very likely to be the common constant. As a compromise,
2272 * we always use the constant, but make an explicit notice
2273 * indicating the "unknown" case.
2275 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2280 #include <sys/sysctl.h>
2282 struct in6_llentry {
2283 struct llentry base;
2284 struct sockaddr_in6 l3_addr6;
2287 static struct llentry *
2288 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2290 struct in6_llentry *lle;
2292 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2293 M_DONTWAIT | M_ZERO);
2294 if (lle == NULL) /* NB: caller generates msg */
2297 callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE);
2298 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2299 lle->base.lle_refcnt = 1;
2300 LLE_LOCK_INIT(&lle->base);
2305 * Deletes an address from the address table.
2306 * This function is called by the timer functions
2307 * such as arptimer() and nd6_llinfo_timer(), and
2308 * the caller does the locking.
2311 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2314 LLE_LOCK_DESTROY(lle);
2315 free(lle, M_LLTABLE);
2319 in6_lltable_prefix_free(struct lltable *llt,
2320 const struct sockaddr *prefix,
2321 const struct sockaddr *mask)
2323 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2324 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2325 struct llentry *lle, *next;
2328 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2329 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2330 if (IN6_ARE_MASKED_ADDR_EQUAL(
2331 &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2334 callout_drain(&lle->la_timer);
2343 in6_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr)
2346 char ip6buf[INET6_ADDRSTRLEN];
2348 KASSERT(l3addr->sa_family == AF_INET6,
2349 ("sin_family %d", l3addr->sa_family));
2351 /* XXX rtalloc1 should take a const param */
2352 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2353 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2356 * Create an ND6 cache for an IPv6 neighbor
2357 * that is not covered by our own prefix.
2359 /* XXX ifaof_ifpforaddr should take a const param */
2360 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2367 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2368 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2377 static struct llentry *
2378 in6_lltable_lookup(struct lltable *llt, u_int flags,
2379 const struct sockaddr *l3addr)
2381 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2382 struct ifnet *ifp = llt->llt_ifp;
2383 struct llentry *lle;
2384 struct llentries *lleh;
2387 IF_AFDATA_LOCK_ASSERT(ifp);
2388 KASSERT(l3addr->sa_family == AF_INET6,
2389 ("sin_family %d", l3addr->sa_family));
2391 hashkey = sin6->sin6_addr.s6_addr32[3];
2392 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2393 LIST_FOREACH(lle, lleh, lle_next) {
2394 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2395 if (lle->la_flags & LLE_DELETED)
2397 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2398 sizeof(struct in6_addr)) == 0)
2403 if (!(flags & LLE_CREATE))
2406 * A route that covers the given address must have
2407 * been installed 1st because we are doing a resolution,
2410 if (!(flags & LLE_IFADDR) &&
2411 in6_lltable_rtcheck(ifp, l3addr) != 0)
2414 lle = in6_lltable_new(l3addr, flags);
2416 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2419 lle->la_flags = flags & ~LLE_CREATE;
2420 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2421 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2422 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2426 lle->lle_head = lleh;
2427 LIST_INSERT_HEAD(lleh, lle, lle_next);
2428 } else if (flags & LLE_DELETE) {
2429 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2431 lle->la_flags = LLE_DELETED;
2434 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2439 if (LLE_IS_VALID(lle)) {
2440 if (flags & LLE_EXCLUSIVE)
2449 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2451 struct ifnet *ifp = llt->llt_ifp;
2452 struct llentry *lle;
2455 struct rt_msghdr rtm;
2456 struct sockaddr_in6 sin6;
2458 * ndp.c assumes that sdl is word aligned
2463 struct sockaddr_dl sdl;
2467 if (ifp->if_flags & IFF_LOOPBACK)
2470 LLTABLE_LOCK_ASSERT();
2473 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2474 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2475 struct sockaddr_dl *sdl;
2477 /* skip deleted or invalid entries */
2478 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2480 /* Skip if jailed and not a valid IP of the prison. */
2481 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2484 * produce a msg made of:
2486 * struct sockaddr_in6 (IPv6)
2487 * struct sockaddr_dl;
2489 bzero(&ndpc, sizeof(ndpc));
2490 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2491 ndpc.rtm.rtm_version = RTM_VERSION;
2492 ndpc.rtm.rtm_type = RTM_GET;
2493 ndpc.rtm.rtm_flags = RTF_UP;
2494 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2495 ndpc.sin6.sin6_family = AF_INET6;
2496 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2497 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2500 if (lle->la_flags & LLE_PUB)
2501 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2504 sdl->sdl_family = AF_LINK;
2505 sdl->sdl_len = sizeof(*sdl);
2506 sdl->sdl_alen = ifp->if_addrlen;
2507 sdl->sdl_index = ifp->if_index;
2508 sdl->sdl_type = ifp->if_type;
2509 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2510 ndpc.rtm.rtm_rmx.rmx_expire =
2511 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2512 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2513 if (lle->la_flags & LLE_STATIC)
2514 ndpc.rtm.rtm_flags |= RTF_STATIC;
2515 ndpc.rtm.rtm_index = ifp->if_index;
2516 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2525 in6_domifattach(struct ifnet *ifp)
2527 struct in6_ifextra *ext;
2529 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2530 bzero(ext, sizeof(*ext));
2532 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2533 M_IFADDR, M_WAITOK);
2534 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2537 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2538 M_IFADDR, M_WAITOK);
2539 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2541 ext->nd_ifinfo = nd6_ifattach(ifp);
2542 ext->scope6_id = scope6_ifattach(ifp);
2543 ext->lltable = lltable_init(ifp, AF_INET6);
2544 if (ext->lltable != NULL) {
2545 ext->lltable->llt_new = in6_lltable_new;
2546 ext->lltable->llt_free = in6_lltable_free;
2547 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2548 ext->lltable->llt_rtcheck = in6_lltable_rtcheck;
2549 ext->lltable->llt_lookup = in6_lltable_lookup;
2550 ext->lltable->llt_dump = in6_lltable_dump;
2553 ext->mld_ifinfo = mld_domifattach(ifp);
2559 in6_domifdetach(struct ifnet *ifp, void *aux)
2561 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2563 mld_domifdetach(ifp);
2564 scope6_ifdetach(ext->scope6_id);
2565 nd6_ifdetach(ext->nd_ifinfo);
2566 lltable_free(ext->lltable);
2567 free(ext->in6_ifstat, M_IFADDR);
2568 free(ext->icmp6_ifstat, M_IFADDR);
2569 free(ext, M_IFADDR);
2573 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2574 * v4 mapped addr or v4 compat addr
2577 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2580 bzero(sin, sizeof(*sin));
2581 sin->sin_len = sizeof(struct sockaddr_in);
2582 sin->sin_family = AF_INET;
2583 sin->sin_port = sin6->sin6_port;
2584 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2587 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2589 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2591 bzero(sin6, sizeof(*sin6));
2592 sin6->sin6_len = sizeof(struct sockaddr_in6);
2593 sin6->sin6_family = AF_INET6;
2594 sin6->sin6_port = sin->sin_port;
2595 sin6->sin6_addr.s6_addr32[0] = 0;
2596 sin6->sin6_addr.s6_addr32[1] = 0;
2597 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2598 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2601 /* Convert sockaddr_in6 into sockaddr_in. */
2603 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2605 struct sockaddr_in *sin_p;
2606 struct sockaddr_in6 sin6;
2609 * Save original sockaddr_in6 addr and convert it
2612 sin6 = *(struct sockaddr_in6 *)nam;
2613 sin_p = (struct sockaddr_in *)nam;
2614 in6_sin6_2_sin(sin_p, &sin6);
2617 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2619 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2621 struct sockaddr_in *sin_p;
2622 struct sockaddr_in6 *sin6_p;
2624 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2626 sin_p = (struct sockaddr_in *)*nam;
2627 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2628 free(*nam, M_SONAME);
2629 *nam = (struct sockaddr *)sin6_p;