2 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 #include "opt_inet6.h"
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
82 #include <net/if_dl.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/if_ether.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/mld6_var.h>
95 #include <netinet6/ip6_mroute.h>
96 #include <netinet6/in6_ifattach.h>
97 #include <netinet6/scope6_var.h>
98 #include <netinet6/in6_pcb.h>
100 #include <net/net_osdep.h>
102 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address");
105 * Definitions of some costant IP6 addresses.
107 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
108 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
109 const struct in6_addr in6addr_nodelocal_allnodes =
110 IN6ADDR_NODELOCAL_ALLNODES_INIT;
111 const struct in6_addr in6addr_linklocal_allnodes =
112 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
113 const struct in6_addr in6addr_linklocal_allrouters =
114 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
116 const struct in6_addr in6mask0 = IN6MASK0;
117 const struct in6_addr in6mask32 = IN6MASK32;
118 const struct in6_addr in6mask64 = IN6MASK64;
119 const struct in6_addr in6mask96 = IN6MASK96;
120 const struct in6_addr in6mask128 = IN6MASK128;
122 const struct sockaddr_in6 sa6_any =
123 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
125 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
126 struct ifnet *, struct thread *));
127 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
128 struct sockaddr_in6 *, int));
129 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
131 struct in6_multihead in6_multihead; /* XXX BSS initialization */
132 int (*faithprefix_p)(struct in6_addr *);
135 * Subroutine for in6_ifaddloop() and in6_ifremloop().
136 * This routine does actual work.
139 in6_ifloop_request(int cmd, struct ifaddr *ifa)
141 struct sockaddr_in6 all1_sa;
142 struct rtentry *nrt = NULL;
145 bzero(&all1_sa, sizeof(all1_sa));
146 all1_sa.sin6_family = AF_INET6;
147 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
148 all1_sa.sin6_addr = in6mask128;
151 * We specify the address itself as the gateway, and set the
152 * RTF_LLINFO flag, so that the corresponding host route would have
153 * the flag, and thus applications that assume traditional behavior
154 * would be happy. Note that we assume the caller of the function
155 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
156 * which changes the outgoing interface to the loopback interface.
158 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
159 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
161 /* XXX need more descriptive message */
162 log(LOG_ERR, "in6_ifloop_request: "
163 "%s operation failed for %s (errno=%d)\n",
164 cmd == RTM_ADD ? "ADD" : "DELETE",
165 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
170 * Report the addition/removal of the address to the routing socket.
171 * XXX: since we called rtinit for a p2p interface with a destination,
172 * we end up reporting twice in such a case. Should we rather
173 * omit the second report?
178 * Make sure rt_ifa be equal to IFA, the second argument of
179 * the function. We need this because when we refer to
180 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
181 * points to the address instead of the loopback address.
183 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
184 IFAFREE(nrt->rt_ifa);
189 rt_newaddrmsg(cmd, ifa, e, nrt);
190 if (cmd == RTM_DELETE) {
193 /* the cmd must be RTM_ADD here */
201 * Add ownaddr as loopback rtentry. We previously add the route only if
202 * necessary (ex. on a p2p link). However, since we now manage addresses
203 * separately from prefixes, we should always add the route. We can't
204 * rely on the cloning mechanism from the corresponding interface route
208 in6_ifaddloop(struct ifaddr *ifa)
213 /* If there is no loopback entry, allocate one. */
214 rt = rtalloc1(ifa->ifa_addr, 0, 0);
215 need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
216 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
220 in6_ifloop_request(RTM_ADD, ifa);
224 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
228 in6_ifremloop(struct ifaddr *ifa)
230 struct in6_ifaddr *ia;
235 * Some of BSD variants do not remove cloned routes
236 * from an interface direct route, when removing the direct route
237 * (see comments in net/net_osdep.h). Even for variants that do remove
238 * cloned routes, they could fail to remove the cloned routes when
239 * we handle multple addresses that share a common prefix.
240 * So, we should remove the route corresponding to the deleted address.
244 * Delete the entry only if exact one ifa exists. More than one ifa
245 * can exist if we assign a same single address to multiple
246 * (probably p2p) interfaces.
247 * XXX: we should avoid such a configuration in IPv6...
249 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
250 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
259 * Before deleting, check if a corresponding loopbacked host
260 * route surely exists. With this check, we can avoid to
261 * delete an interface direct route whose destination is same
262 * as the address being removed. This can happen when removing
263 * a subnet-router anycast address on an interface attahced
264 * to a shared medium.
266 rt = rtalloc1(ifa->ifa_addr, 0, 0);
268 if ((rt->rt_flags & RTF_HOST) != 0 &&
269 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
271 in6_ifloop_request(RTM_DELETE, ifa);
279 in6_mask2len(mask, lim0)
280 struct in6_addr *mask;
284 u_char *lim = lim0, *p;
286 /* ignore the scope_id part */
287 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
288 lim = (u_char *)mask + sizeof(*mask);
289 for (p = (u_char *)mask; p < lim; x++, p++) {
295 for (y = 0; y < 8; y++) {
296 if ((*p & (0x80 >> y)) == 0)
302 * when the limit pointer is given, do a stricter check on the
306 if (y != 0 && (*p & (0x00ff >> y)) != 0)
308 for (p = p + 1; p < lim; p++)
316 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
317 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
320 in6_control(so, cmd, data, ifp, td)
327 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
328 struct in6_ifaddr *ia = NULL;
329 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
330 int error, privileged;
331 struct sockaddr_in6 *sa6;
334 if (td == NULL || !suser(td))
338 case SIOCGETSGCNT_IN6:
339 case SIOCGETMIFCNT_IN6:
340 return (mrt6_ioctl(cmd, data));
344 case SIOCAADDRCTL_POLICY:
345 case SIOCDADDRCTL_POLICY:
348 return (in6_src_ioctl(cmd, data));
355 case SIOCSNDFLUSH_IN6:
356 case SIOCSPFXFLUSH_IN6:
357 case SIOCSRTRFLUSH_IN6:
358 case SIOCSDEFIFACE_IN6:
359 case SIOCSIFINFO_FLAGS:
363 case OSIOCGIFINFO_IN6:
364 case SIOCGIFINFO_IN6:
365 case SIOCSIFINFO_IN6:
368 case SIOCGNBRINFO_IN6:
369 case SIOCGDEFIFACE_IN6:
370 return (nd6_ioctl(cmd, data, ifp));
374 case SIOCSIFPREFIX_IN6:
375 case SIOCDIFPREFIX_IN6:
376 case SIOCAIFPREFIX_IN6:
377 case SIOCCIFPREFIX_IN6:
378 case SIOCSGIFPREFIX_IN6:
379 case SIOCGIFPREFIX_IN6:
381 "prefix ioctls are now invalidated. "
382 "please use ifconfig.\n");
390 return (scope6_set(ifp,
391 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
393 return (scope6_get(ifp,
394 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
396 return (scope6_get_default((struct scope6_id *)
397 ifr->ifr_ifru.ifru_scope_id));
407 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
411 * Find address for this interface, if it exists.
413 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
414 * only, and used the first interface address as the target of other
415 * operations (without checking ifra_addr). This was because netinet
416 * code/API assumed at most 1 interface address per interface.
417 * Since IPv6 allows a node to assign multiple addresses
418 * on a single interface, we almost always look and check the
419 * presence of ifra_addr, and reject invalid ones here.
420 * It also decreases duplicated code among SIOC*_IN6 operations.
423 case SIOCAIFADDR_IN6:
424 case SIOCSIFPHYADDR_IN6:
425 sa6 = &ifra->ifra_addr;
427 case SIOCSIFADDR_IN6:
428 case SIOCGIFADDR_IN6:
429 case SIOCSIFDSTADDR_IN6:
430 case SIOCSIFNETMASK_IN6:
431 case SIOCGIFDSTADDR_IN6:
432 case SIOCGIFNETMASK_IN6:
433 case SIOCDIFADDR_IN6:
434 case SIOCGIFPSRCADDR_IN6:
435 case SIOCGIFPDSTADDR_IN6:
436 case SIOCGIFAFLAG_IN6:
437 case SIOCSNDFLUSH_IN6:
438 case SIOCSPFXFLUSH_IN6:
439 case SIOCSRTRFLUSH_IN6:
440 case SIOCGIFALIFETIME_IN6:
441 case SIOCSIFALIFETIME_IN6:
442 case SIOCGIFSTAT_IN6:
443 case SIOCGIFSTAT_ICMP6:
444 sa6 = &ifr->ifr_addr;
450 if (sa6 && sa6->sin6_family == AF_INET6) {
453 if (sa6->sin6_scope_id != 0)
454 error = sa6_embedscope(sa6, 0);
456 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
459 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
464 case SIOCSIFADDR_IN6:
465 case SIOCSIFDSTADDR_IN6:
466 case SIOCSIFNETMASK_IN6:
468 * Since IPv6 allows a node to assign multiple addresses
469 * on a single interface, SIOCSIFxxx ioctls are deprecated.
471 /* we decided to obsolete this command (20000704) */
474 case SIOCDIFADDR_IN6:
476 * for IPv4, we look for existing in_ifaddr here to allow
477 * "ifconfig if0 delete" to remove the first IPv4 address on
478 * the interface. For IPv6, as the spec allows multiple
479 * interface address from the day one, we consider "remove the
480 * first one" semantics to be not preferable.
483 return (EADDRNOTAVAIL);
485 case SIOCAIFADDR_IN6:
487 * We always require users to specify a valid IPv6 address for
488 * the corresponding operation.
490 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
491 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
492 return (EAFNOSUPPORT);
498 case SIOCGIFADDR_IN6:
499 /* This interface is basically deprecated. use SIOCGIFCONF. */
501 case SIOCGIFAFLAG_IN6:
502 case SIOCGIFNETMASK_IN6:
503 case SIOCGIFDSTADDR_IN6:
504 case SIOCGIFALIFETIME_IN6:
505 /* must think again about its semantics */
507 return (EADDRNOTAVAIL);
509 case SIOCSIFALIFETIME_IN6:
511 struct in6_addrlifetime *lt;
516 return (EADDRNOTAVAIL);
517 /* sanity for overflow - beware unsigned */
518 lt = &ifr->ifr_ifru.ifru_lifetime;
519 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
520 lt->ia6t_vltime + time_second < time_second) {
523 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
524 lt->ia6t_pltime + time_second < time_second) {
533 case SIOCGIFADDR_IN6:
534 ifr->ifr_addr = ia->ia_addr;
535 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
539 case SIOCGIFDSTADDR_IN6:
540 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
543 * XXX: should we check if ifa_dstaddr is NULL and return
546 ifr->ifr_dstaddr = ia->ia_dstaddr;
547 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
551 case SIOCGIFNETMASK_IN6:
552 ifr->ifr_addr = ia->ia_prefixmask;
555 case SIOCGIFAFLAG_IN6:
556 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
559 case SIOCGIFSTAT_IN6:
562 bzero(&ifr->ifr_ifru.ifru_stat,
563 sizeof(ifr->ifr_ifru.ifru_stat));
564 ifr->ifr_ifru.ifru_stat =
565 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
568 case SIOCGIFSTAT_ICMP6:
571 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
572 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
573 ifr->ifr_ifru.ifru_icmp6stat =
574 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
577 case SIOCGIFALIFETIME_IN6:
578 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
579 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
581 struct in6_addrlifetime *retlt =
582 &ifr->ifr_ifru.ifru_lifetime;
585 * XXX: adjust expiration time assuming time_t is
589 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
590 if (ia->ia6_lifetime.ia6t_vltime <
591 maxexpire - ia->ia6_updatetime) {
592 retlt->ia6t_expire = ia->ia6_updatetime +
593 ia->ia6_lifetime.ia6t_vltime;
595 retlt->ia6t_expire = maxexpire;
597 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
599 struct in6_addrlifetime *retlt =
600 &ifr->ifr_ifru.ifru_lifetime;
603 * XXX: adjust expiration time assuming time_t is
607 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
608 if (ia->ia6_lifetime.ia6t_pltime <
609 maxexpire - ia->ia6_updatetime) {
610 retlt->ia6t_preferred = ia->ia6_updatetime +
611 ia->ia6_lifetime.ia6t_pltime;
613 retlt->ia6t_preferred = maxexpire;
617 case SIOCSIFALIFETIME_IN6:
618 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
620 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
621 ia->ia6_lifetime.ia6t_expire =
622 time_second + ia->ia6_lifetime.ia6t_vltime;
624 ia->ia6_lifetime.ia6t_expire = 0;
625 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
626 ia->ia6_lifetime.ia6t_preferred =
627 time_second + ia->ia6_lifetime.ia6t_pltime;
629 ia->ia6_lifetime.ia6t_preferred = 0;
632 case SIOCAIFADDR_IN6:
635 struct nd_prefixctl pr0;
636 struct nd_prefix *pr;
639 * first, make or update the interface address structure,
640 * and link it to the list.
642 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
644 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
647 * this can happen when the user specify the 0 valid
654 * then, make the prefix on-link on the interface.
655 * XXX: we'd rather create the prefix before the address, but
656 * we need at least one address to install the corresponding
657 * interface route, so we configure the address first.
661 * convert mask to prefix length (prefixmask has already
662 * been validated in in6_update_ifa().
664 bzero(&pr0, sizeof(pr0));
666 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
668 if (pr0.ndpr_plen == 128) {
669 break; /* we don't need to install a host route. */
671 pr0.ndpr_prefix = ifra->ifra_addr;
672 /* apply the mask for safety. */
673 for (i = 0; i < 4; i++) {
674 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
675 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
678 * XXX: since we don't have an API to set prefix (not address)
679 * lifetimes, we just use the same lifetimes as addresses.
680 * The (temporarily) installed lifetimes can be overridden by
681 * later advertised RAs (when accept_rtadv is non 0), which is
682 * an intended behavior.
684 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
686 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
687 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
688 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
690 /* add the prefix if not yet. */
691 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
693 * nd6_prelist_add will install the corresponding
696 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
699 log(LOG_ERR, "nd6_prelist_add succeeded but "
701 return (EINVAL); /* XXX panic here? */
705 /* relate the address to the prefix */
706 if (ia->ia6_ndpr == NULL) {
711 * If this is the first autoconf address from the
712 * prefix, create a temporary address as well
715 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
716 ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
718 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
719 log(LOG_NOTICE, "in6_control: failed "
720 "to create a temporary address, "
727 * this might affect the status of autoconfigured addresses,
728 * that is, this address might make other addresses detached.
730 pfxlist_onlink_check();
731 if (error == 0 && ia)
732 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
736 case SIOCDIFADDR_IN6:
738 struct nd_prefix *pr;
741 * If the address being deleted is the only one that owns
742 * the corresponding prefix, expire the prefix as well.
743 * XXX: theoretically, we don't have to worry about such
744 * relationship, since we separate the address management
745 * and the prefix management. We do this, however, to provide
746 * as much backward compatibility as possible in terms of
747 * the ioctl operation.
748 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
751 in6_purgeaddr(&ia->ia_ifa);
752 if (pr && pr->ndpr_refcnt == 0)
754 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
759 if (ifp == NULL || ifp->if_ioctl == 0)
761 return ((*ifp->if_ioctl)(ifp, cmd, data));
768 * Update parameters of an IPv6 interface address.
769 * If necessary, a new entry is created and linked into address chains.
770 * This function is separated from in6_control().
771 * XXX: should this be performed under splnet()?
774 in6_update_ifa(ifp, ifra, ia, flags)
776 struct in6_aliasreq *ifra;
777 struct in6_ifaddr *ia;
780 int error = 0, hostIsNew = 0, plen = -1;
781 struct in6_ifaddr *oia;
782 struct sockaddr_in6 dst6;
783 struct in6_addrlifetime *lt;
784 struct in6_multi_mship *imm;
785 struct in6_multi *in6m_sol;
789 /* Validate parameters */
790 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
794 * The destination address for a p2p link must have a family
795 * of AF_UNSPEC or AF_INET6.
797 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
798 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
799 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
800 return (EAFNOSUPPORT);
802 * validate ifra_prefixmask. don't check sin6_family, netmask
803 * does not carry fields other than sin6_len.
805 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
808 * Because the IPv6 address architecture is classless, we require
809 * users to specify a (non 0) prefix length (mask) for a new address.
810 * We also require the prefix (when specified) mask is valid, and thus
811 * reject a non-consecutive mask.
813 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
815 if (ifra->ifra_prefixmask.sin6_len != 0) {
816 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
817 (u_char *)&ifra->ifra_prefixmask +
818 ifra->ifra_prefixmask.sin6_len);
823 * In this case, ia must not be NULL. We just use its prefix
826 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
829 * If the destination address on a p2p interface is specified,
830 * and the address is a scoped one, validate/set the scope
833 dst6 = ifra->ifra_dstaddr;
834 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
835 (dst6.sin6_family == AF_INET6)) {
836 struct in6_addr in6_tmp;
839 in6_tmp = dst6.sin6_addr;
840 if (in6_setscope(&in6_tmp, ifp, &zoneid))
841 return (EINVAL); /* XXX: should be impossible */
843 if (dst6.sin6_scope_id != 0) {
844 if (dst6.sin6_scope_id != zoneid)
846 } else /* user omit to specify the ID. */
847 dst6.sin6_scope_id = zoneid;
849 /* convert into the internal form */
850 if (sa6_embedscope(&dst6, 0))
851 return (EINVAL); /* XXX: should be impossible */
854 * The destination address can be specified only for a p2p or a
855 * loopback interface. If specified, the corresponding prefix length
858 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
859 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
860 /* XXX: noisy message */
861 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
862 "be specified for a p2p or a loopback IF only\n"));
866 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
867 "be 128 when dstaddr is specified\n"));
871 /* lifetime consistency check */
872 lt = &ifra->ifra_lifetime;
873 if (lt->ia6t_pltime > lt->ia6t_vltime)
875 if (lt->ia6t_vltime == 0) {
877 * the following log might be noisy, but this is a typical
878 * configuration mistake or a tool's bug.
881 "in6_update_ifa: valid lifetime is 0 for %s\n",
882 ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
885 return (0); /* there's nothing to do */
889 * If this is a new address, allocate a new ifaddr and link it
895 * When in6_update_ifa() is called in a process of a received
896 * RA, it is called under an interrupt context. So, we should
897 * call malloc with M_NOWAIT.
899 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
903 bzero((caddr_t)ia, sizeof(*ia));
904 /* Initialize the address and masks, and put time stamp */
905 IFA_LOCK_INIT(&ia->ia_ifa);
906 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
907 ia->ia_addr.sin6_family = AF_INET6;
908 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
909 ia->ia6_createtime = time_second;
910 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
912 * XXX: some functions expect that ifa_dstaddr is not
913 * NULL for p2p interfaces.
915 ia->ia_ifa.ifa_dstaddr =
916 (struct sockaddr *)&ia->ia_dstaddr;
918 ia->ia_ifa.ifa_dstaddr = NULL;
920 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
923 if ((oia = in6_ifaddr) != NULL) {
924 for ( ; oia->ia_next; oia = oia->ia_next)
930 ia->ia_ifa.ifa_refcnt = 1;
931 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
934 /* update timestamp */
935 ia->ia6_updatetime = time_second;
937 /* set prefix mask */
938 if (ifra->ifra_prefixmask.sin6_len) {
940 * We prohibit changing the prefix length of an existing
942 * + such an operation should be rare in IPv6, and
943 * + the operation would confuse prefix management.
945 if (ia->ia_prefixmask.sin6_len &&
946 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
947 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
948 " existing (%s) address should not be changed\n",
949 ip6_sprintf(&ia->ia_addr.sin6_addr)));
953 ia->ia_prefixmask = ifra->ifra_prefixmask;
957 * If a new destination address is specified, scrub the old one and
958 * install the new destination. Note that the interface must be
959 * p2p or loopback (see the check above.)
961 if (dst6.sin6_family == AF_INET6 &&
962 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
965 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
966 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
967 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
968 "a route to the old destination: %s\n",
969 ip6_sprintf(&ia->ia_addr.sin6_addr)));
970 /* proceed anyway... */
972 ia->ia_flags &= ~IFA_ROUTE;
973 ia->ia_dstaddr = dst6;
977 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
978 * to see if the address is deprecated or invalidated, but initialize
979 * these members for applications.
981 ia->ia6_lifetime = ifra->ifra_lifetime;
982 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
983 ia->ia6_lifetime.ia6t_expire =
984 time_second + ia->ia6_lifetime.ia6t_vltime;
986 ia->ia6_lifetime.ia6t_expire = 0;
987 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
988 ia->ia6_lifetime.ia6t_preferred =
989 time_second + ia->ia6_lifetime.ia6t_pltime;
991 ia->ia6_lifetime.ia6t_preferred = 0;
993 /* reset the interface and routing table appropriately. */
994 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
998 * configure address flags.
1000 ia->ia6_flags = ifra->ifra_flags;
1002 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1003 * userland, make it deprecated.
1005 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1006 ia->ia6_lifetime.ia6t_pltime = 0;
1007 ia->ia6_lifetime.ia6t_preferred = time_second;
1010 * Make the address tentative before joining multicast addresses,
1011 * so that corresponding MLD responses would not have a tentative
1014 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1015 if (hostIsNew && in6if_do_dad(ifp))
1016 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1019 * We are done if we have simply modified an existing address.
1025 * Beyond this point, we should call in6_purgeaddr upon an error,
1026 * not just go to unlink.
1029 /* Join necessary multicast groups */
1031 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1032 struct sockaddr_in6 mltaddr, mltmask;
1033 struct in6_addr llsol;
1035 /* join solicited multicast addr for new host id */
1036 bzero(&llsol, sizeof(struct in6_addr));
1037 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1038 llsol.s6_addr32[1] = 0;
1039 llsol.s6_addr32[2] = htonl(1);
1040 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1041 llsol.s6_addr8[12] = 0xff;
1042 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1043 /* XXX: should not happen */
1044 log(LOG_ERR, "in6_update_ifa: "
1045 "in6_setscope failed\n");
1049 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1051 * We need a random delay for DAD on the address
1052 * being configured. It also means delaying
1053 * transmission of the corresponding MLD report to
1054 * avoid report collision.
1055 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1057 delay = arc4random() %
1058 (MAX_RTR_SOLICITATION_DELAY * hz);
1060 imm = in6_joingroup(ifp, &llsol, &error, delay);
1062 nd6log((LOG_WARNING,
1063 "in6_update_ifa: addmulti failed for "
1064 "%s on %s (errno=%d)\n",
1065 ip6_sprintf(&llsol), if_name(ifp),
1067 in6_purgeaddr((struct ifaddr *)ia);
1070 in6m_sol = imm->i6mm_maddr;
1072 bzero(&mltmask, sizeof(mltmask));
1073 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1074 mltmask.sin6_family = AF_INET6;
1075 mltmask.sin6_addr = in6mask32;
1076 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1079 * join link-local all-nodes address
1081 bzero(&mltaddr, sizeof(mltaddr));
1082 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1083 mltaddr.sin6_family = AF_INET6;
1084 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1085 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1087 goto cleanup; /* XXX: should not fail */
1090 * XXX: do we really need this automatic routes?
1091 * We should probably reconsider this stuff. Most applications
1092 * actually do not need the routes, since they usually specify
1093 * the outgoing interface.
1095 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1097 if (memcmp(&mltaddr.sin6_addr,
1098 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1105 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1106 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1107 (struct sockaddr *)&ia->ia_addr,
1108 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1109 (struct rtentry **)0);
1116 * XXX: do we really need this automatic routes?
1117 * We should probably reconsider this stuff. Most applications
1118 * actually do not need the routes, since they usually specify
1119 * the outgoing interface.
1121 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1123 /* XXX: only works in !SCOPEDROUTING case. */
1124 if (memcmp(&mltaddr.sin6_addr,
1125 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1132 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1133 (struct sockaddr *)&ia->ia_addr,
1134 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1135 (struct rtentry **)0);
1142 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1144 nd6log((LOG_WARNING,
1145 "in6_update_ifa: addmulti failed for "
1146 "%s on %s (errno=%d)\n",
1147 ip6_sprintf(&mltaddr.sin6_addr),
1148 if_name(ifp), error));
1153 * join node information group address
1155 #define hostnamelen strlen(hostname)
1157 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1159 * The spec doesn't say anything about delay for this
1160 * group, but the same logic should apply.
1162 delay = arc4random() %
1163 (MAX_RTR_SOLICITATION_DELAY * hz);
1165 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1167 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1168 delay); /* XXX jinmei */
1170 nd6log((LOG_WARNING, "in6_update_ifa: "
1171 "addmulti failed for %s on %s "
1173 ip6_sprintf(&mltaddr.sin6_addr),
1174 if_name(ifp), error));
1175 /* XXX not very fatal, go on... */
1181 * join interface-local all-nodes address.
1182 * (ff01::1%ifN, and ff01::%ifN/32)
1184 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1185 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1187 goto cleanup; /* XXX: should not fail */
1188 /* XXX: again, do we really need the route? */
1189 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1191 if (memcmp(&mltaddr.sin6_addr,
1192 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1199 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1200 (struct sockaddr *)&ia->ia_addr,
1201 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1202 (struct rtentry **)0);
1208 /* XXX: again, do we really need the route? */
1209 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1211 if (memcmp(&mltaddr.sin6_addr,
1212 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1219 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1220 (struct sockaddr *)&ia->ia_addr,
1221 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1222 (struct rtentry **)0);
1229 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1231 nd6log((LOG_WARNING, "in6_update_ifa: "
1232 "addmulti failed for %s on %s "
1234 ip6_sprintf(&mltaddr.sin6_addr),
1235 if_name(ifp), error));
1242 * Perform DAD, if needed.
1243 * XXX It may be of use, if we can administratively
1246 if (hostIsNew && in6if_do_dad(ifp) &&
1247 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1248 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1250 int mindelay, maxdelay;
1253 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1255 * We need to impose a delay before sending an NS
1256 * for DAD. Check if we also needed a delay for the
1257 * corresponding MLD message. If we did, the delay
1258 * should be larger than the MLD delay (this could be
1259 * relaxed a bit, but this simple logic is at least
1263 if (in6m_sol != NULL &&
1264 in6m_sol->in6m_state == MLD_REPORTPENDING) {
1265 mindelay = in6m_sol->in6m_timer;
1267 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1268 if (maxdelay - mindelay == 0)
1272 (arc4random() % (maxdelay - mindelay)) +
1276 nd6_dad_start((struct ifaddr *)ia, delay);
1283 * XXX: if a change of an existing address failed, keep the entry
1287 in6_unlink_ifa(ia, ifp);
1291 in6_purgeaddr(&ia->ia_ifa);
1299 struct ifnet *ifp = ifa->ifa_ifp;
1300 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1302 /* stop DAD processing */
1306 * delete route to the destination of the address being purged.
1307 * The interface must be p2p or loopback in this case.
1309 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1312 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1314 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1315 "a route to the p2p destination: %s on %s, "
1317 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1319 /* proceed anyway... */
1321 ia->ia_flags &= ~IFA_ROUTE;
1324 /* Remove ownaddr's loopback rtentry, if it exists. */
1325 in6_ifremloop(&(ia->ia_ifa));
1327 if (ifp->if_flags & IFF_MULTICAST) {
1329 * delete solicited multicast addr for deleting host id
1331 struct in6_multi *in6m;
1332 struct in6_addr llsol;
1333 bzero(&llsol, sizeof(struct in6_addr));
1334 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1335 llsol.s6_addr32[1] = 0;
1336 llsol.s6_addr32[2] = htonl(1);
1337 llsol.s6_addr32[3] =
1338 ia->ia_addr.sin6_addr.s6_addr32[3];
1339 llsol.s6_addr8[12] = 0xff;
1340 (void)in6_setscope(&llsol, ifp, NULL); /* XXX proceed anyway */
1342 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1347 in6_unlink_ifa(ia, ifp);
1351 in6_unlink_ifa(ia, ifp)
1352 struct in6_ifaddr *ia;
1355 struct in6_ifaddr *oia;
1358 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1361 if (oia == (ia = in6_ifaddr))
1362 in6_ifaddr = ia->ia_next;
1364 while (ia->ia_next && (ia->ia_next != oia))
1367 ia->ia_next = oia->ia_next;
1370 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1375 * Release the reference to the base prefix. There should be a
1376 * positive reference.
1378 if (oia->ia6_ndpr == NULL) {
1380 "in6_unlink_ifa: autoconf'ed address "
1381 "%p has no prefix\n", oia));
1383 oia->ia6_ndpr->ndpr_refcnt--;
1384 oia->ia6_ndpr = NULL;
1388 * Also, if the address being removed is autoconf'ed, call
1389 * pfxlist_onlink_check() since the release might affect the status of
1390 * other (detached) addresses.
1392 if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) {
1393 pfxlist_onlink_check();
1397 * release another refcnt for the link from in6_ifaddr.
1398 * Note that we should decrement the refcnt at least once for all *BSD.
1400 IFAFREE(&oia->ia_ifa);
1409 struct ifaddr *ifa, *nifa;
1411 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1412 nifa = TAILQ_NEXT(ifa, ifa_list);
1413 if (ifa->ifa_addr->sa_family != AF_INET6)
1423 * SIOCGLIFADDR: get first address. (?)
1424 * SIOCGLIFADDR with IFLR_PREFIX:
1425 * get first address that matches the specified prefix.
1426 * SIOCALIFADDR: add the specified address.
1427 * SIOCALIFADDR with IFLR_PREFIX:
1428 * add the specified prefix, filling hostid part from
1429 * the first link-local address. prefixlen must be <= 64.
1430 * SIOCDLIFADDR: delete the specified address.
1431 * SIOCDLIFADDR with IFLR_PREFIX:
1432 * delete the first address that matches the specified prefix.
1434 * EINVAL on invalid parameters
1435 * EADDRNOTAVAIL on prefix match failed/specified address not found
1436 * other values may be returned from in6_ioctl()
1438 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1439 * this is to accomodate address naming scheme other than RFC2374,
1441 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1442 * address encoding scheme. (see figure on page 8)
1445 in6_lifaddr_ioctl(so, cmd, data, ifp, td)
1452 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1454 struct sockaddr *sa;
1457 if (!data || !ifp) {
1458 panic("invalid argument to in6_lifaddr_ioctl");
1464 /* address must be specified on GET with IFLR_PREFIX */
1465 if ((iflr->flags & IFLR_PREFIX) == 0)
1470 /* address must be specified on ADD and DELETE */
1471 sa = (struct sockaddr *)&iflr->addr;
1472 if (sa->sa_family != AF_INET6)
1474 if (sa->sa_len != sizeof(struct sockaddr_in6))
1476 /* XXX need improvement */
1477 sa = (struct sockaddr *)&iflr->dstaddr;
1478 if (sa->sa_family && sa->sa_family != AF_INET6)
1480 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1483 default: /* shouldn't happen */
1485 panic("invalid cmd to in6_lifaddr_ioctl");
1491 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1497 struct in6_aliasreq ifra;
1498 struct in6_addr *hostid = NULL;
1501 if ((iflr->flags & IFLR_PREFIX) != 0) {
1502 struct sockaddr_in6 *sin6;
1505 * hostid is to fill in the hostid part of the
1506 * address. hostid points to the first link-local
1507 * address attached to the interface.
1509 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1511 return EADDRNOTAVAIL;
1512 hostid = IFA_IN6(ifa);
1514 /* prefixlen must be <= 64. */
1515 if (64 < iflr->prefixlen)
1517 prefixlen = iflr->prefixlen;
1519 /* hostid part must be zero. */
1520 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1521 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1522 sin6->sin6_addr.s6_addr32[3] != 0) {
1526 prefixlen = iflr->prefixlen;
1528 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1529 bzero(&ifra, sizeof(ifra));
1530 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1532 bcopy(&iflr->addr, &ifra.ifra_addr,
1533 ((struct sockaddr *)&iflr->addr)->sa_len);
1535 /* fill in hostid part */
1536 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1537 hostid->s6_addr32[2];
1538 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1539 hostid->s6_addr32[3];
1542 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1543 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1544 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1546 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1547 hostid->s6_addr32[2];
1548 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1549 hostid->s6_addr32[3];
1553 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1554 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1556 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1557 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1562 struct in6_ifaddr *ia;
1563 struct in6_addr mask, candidate, match;
1564 struct sockaddr_in6 *sin6;
1567 bzero(&mask, sizeof(mask));
1568 if (iflr->flags & IFLR_PREFIX) {
1569 /* lookup a prefix rather than address. */
1570 in6_prefixlen2mask(&mask, iflr->prefixlen);
1572 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1573 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1574 match.s6_addr32[0] &= mask.s6_addr32[0];
1575 match.s6_addr32[1] &= mask.s6_addr32[1];
1576 match.s6_addr32[2] &= mask.s6_addr32[2];
1577 match.s6_addr32[3] &= mask.s6_addr32[3];
1579 /* if you set extra bits, that's wrong */
1580 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1585 if (cmd == SIOCGLIFADDR) {
1586 /* on getting an address, take the 1st match */
1589 /* on deleting an address, do exact match */
1590 in6_prefixlen2mask(&mask, 128);
1591 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1592 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1598 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1599 if (ifa->ifa_addr->sa_family != AF_INET6)
1605 * XXX: this is adhoc, but is necessary to allow
1606 * a user to specify fe80::/64 (not /10) for a
1607 * link-local address.
1609 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1610 in6_clearscope(&candidate);
1611 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1612 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1613 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1614 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1615 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1619 return EADDRNOTAVAIL;
1622 if (cmd == SIOCGLIFADDR) {
1625 /* fill in the if_laddrreq structure */
1626 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1627 error = sa6_recoverscope(
1628 (struct sockaddr_in6 *)&iflr->addr);
1632 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1633 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1634 ia->ia_dstaddr.sin6_len);
1635 error = sa6_recoverscope(
1636 (struct sockaddr_in6 *)&iflr->dstaddr);
1640 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1643 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1645 iflr->flags = ia->ia6_flags; /* XXX */
1649 struct in6_aliasreq ifra;
1651 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1652 bzero(&ifra, sizeof(ifra));
1653 bcopy(iflr->iflr_name, ifra.ifra_name,
1654 sizeof(ifra.ifra_name));
1656 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1657 ia->ia_addr.sin6_len);
1658 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1659 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1660 ia->ia_dstaddr.sin6_len);
1662 bzero(&ifra.ifra_dstaddr,
1663 sizeof(ifra.ifra_dstaddr));
1665 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1666 ia->ia_prefixmask.sin6_len);
1668 ifra.ifra_flags = ia->ia6_flags;
1669 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1675 return EOPNOTSUPP; /* just for safety */
1679 * Initialize an interface's intetnet6 address
1680 * and routing table entry.
1683 in6_ifinit(ifp, ia, sin6, newhost)
1685 struct in6_ifaddr *ia;
1686 struct sockaddr_in6 *sin6;
1689 int error = 0, plen, ifacount = 0;
1694 * Give the interface a chance to initialize
1695 * if this is its first address,
1696 * and to validate the address if necessary.
1698 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1699 if (ifa->ifa_addr->sa_family != AF_INET6)
1704 ia->ia_addr = *sin6;
1706 if (ifacount <= 1 && ifp->if_ioctl) {
1708 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1709 IFF_UNLOCKGIANT(ifp);
1717 ia->ia_ifa.ifa_metric = ifp->if_metric;
1719 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1721 if (newhost && nd6_need_cache(ifp) != 0) {
1722 /* set the rtrequest function to create llinfo */
1723 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1728 * If a new destination address is specified for a point-to-point
1729 * interface, install a route to the destination as an interface
1730 * direct route. In addition, if the link is expected to have neighbor
1731 * cache entries, specify RTF_LLINFO so that a cache entry for the
1732 * destination address will be created.
1734 * XXX: the logic below rejects assigning multiple addresses on a p2p
1735 * interface that share the same destination.
1737 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1738 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1739 ia->ia_dstaddr.sin6_family == AF_INET6) {
1740 int rtflags = RTF_UP | RTF_HOST;
1741 struct rtentry *rt = NULL, **rtp = NULL;
1743 if (nd6_need_cache(ifp) != 0) {
1744 rtflags |= RTF_LLINFO;
1748 error = rtrequest(RTM_ADD, (struct sockaddr *)&ia->ia_dstaddr,
1749 (struct sockaddr *)&ia->ia_addr,
1750 (struct sockaddr *)&ia->ia_prefixmask,
1751 ia->ia_flags | rtflags, rtp);
1755 struct llinfo_nd6 *ln;
1758 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1761 * Set the state to STALE because we don't
1762 * have to perform address resolution on this
1765 ln->ln_state = ND6_LLINFO_STALE;
1770 ia->ia_flags |= IFA_ROUTE;
1774 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1776 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1779 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1781 in6_ifaddloop(&(ia->ia_ifa));
1786 struct in6_multi_mship *
1787 in6_joingroup(ifp, addr, errorp, delay)
1789 struct in6_addr *addr;
1793 struct in6_multi_mship *imm;
1795 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1800 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay);
1801 if (!imm->i6mm_maddr) {
1802 /* *errorp is alrady set */
1803 free(imm, M_IP6MADDR);
1811 struct in6_multi_mship *imm;
1814 if (imm->i6mm_maddr)
1815 in6_delmulti(imm->i6mm_maddr);
1816 free(imm, M_IP6MADDR);
1821 * Find an IPv6 interface link-local address specific to an interface.
1824 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1830 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1831 if (ifa->ifa_addr->sa_family != AF_INET6)
1833 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1834 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1841 return ((struct in6_ifaddr *)ifa);
1846 * find the internet address corresponding to a given interface and address.
1849 in6ifa_ifpwithaddr(ifp, addr)
1851 struct in6_addr *addr;
1855 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1856 if (ifa->ifa_addr->sa_family != AF_INET6)
1858 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1862 return ((struct in6_ifaddr *)ifa);
1866 * Convert IP6 address to printable (loggable) representation.
1868 static char digits[] = "0123456789abcdef";
1869 static int ip6round = 0;
1872 const struct in6_addr *addr;
1874 static char ip6buf[8][48];
1877 const u_int16_t *a = (const u_int16_t *)addr;
1881 ip6round = (ip6round + 1) & 7;
1882 cp = ip6buf[ip6round];
1884 for (i = 0; i < 8; i++) {
1895 if (dcolon == 0 && *(a + 1) == 0) {
1907 d = (const u_char *)a;
1908 *cp++ = digits[*d >> 4];
1909 *cp++ = digits[*d++ & 0xf];
1910 *cp++ = digits[*d >> 4];
1911 *cp++ = digits[*d & 0xf];
1916 return (ip6buf[ip6round]);
1921 struct in6_addr *in6;
1923 struct in6_ifaddr *ia;
1925 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1928 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1929 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1930 &ia->ia_prefixmask.sin6_addr)) {
1939 in6_is_addr_deprecated(sa6)
1940 struct sockaddr_in6 *sa6;
1942 struct in6_ifaddr *ia;
1944 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1945 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1947 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1948 return (1); /* true */
1950 /* XXX: do we still have to go thru the rest of the list? */
1953 return (0); /* false */
1957 * return length of part which dst and src are equal
1961 in6_matchlen(src, dst)
1962 struct in6_addr *src, *dst;
1965 u_char *s = (u_char *)src, *d = (u_char *)dst;
1966 u_char *lim = s + 16, r;
1969 if ((r = (*d++ ^ *s++)) != 0) {
1980 /* XXX: to be scope conscious */
1982 in6_are_prefix_equal(p1, p2, len)
1983 struct in6_addr *p1, *p2;
1986 int bytelen, bitlen;
1989 if (0 > len || len > 128) {
1990 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1998 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2001 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2002 p2->s6_addr[bytelen] >> (8 - bitlen))
2009 in6_prefixlen2mask(maskp, len)
2010 struct in6_addr *maskp;
2013 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2014 int bytelen, bitlen, i;
2017 if (0 > len || len > 128) {
2018 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2023 bzero(maskp, sizeof(*maskp));
2026 for (i = 0; i < bytelen; i++)
2027 maskp->s6_addr[i] = 0xff;
2029 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2033 * return the best address out of the same scope. if no address was
2034 * found, return the first valid address from designated IF.
2037 in6_ifawithifp(ifp, dst)
2039 struct in6_addr *dst;
2041 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2043 struct in6_ifaddr *besta = 0;
2044 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2046 dep[0] = dep[1] = NULL;
2049 * We first look for addresses in the same scope.
2050 * If there is one, return it.
2051 * If two or more, return one which matches the dst longest.
2052 * If none, return one of global addresses assigned other ifs.
2054 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2055 if (ifa->ifa_addr->sa_family != AF_INET6)
2057 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2058 continue; /* XXX: is there any case to allow anycast? */
2059 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2060 continue; /* don't use this interface */
2061 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2063 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2064 if (ip6_use_deprecated)
2065 dep[0] = (struct in6_ifaddr *)ifa;
2069 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2071 * call in6_matchlen() as few as possible
2075 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2076 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2079 besta = (struct in6_ifaddr *)ifa;
2082 besta = (struct in6_ifaddr *)ifa;
2088 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2089 if (ifa->ifa_addr->sa_family != AF_INET6)
2091 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2092 continue; /* XXX: is there any case to allow anycast? */
2093 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2094 continue; /* don't use this interface */
2095 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2097 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2098 if (ip6_use_deprecated)
2099 dep[1] = (struct in6_ifaddr *)ifa;
2103 return (struct in6_ifaddr *)ifa;
2106 /* use the last-resort values, that are, deprecated addresses */
2116 * perform DAD when interface becomes IFF_UP.
2123 struct in6_ifaddr *ia;
2125 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2126 if (ifa->ifa_addr->sa_family != AF_INET6)
2128 ia = (struct in6_ifaddr *)ifa;
2129 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2131 * The TENTATIVE flag was likely set by hand
2132 * beforehand, implicitly indicating the need for DAD.
2133 * We may be able to skip the random delay in this
2134 * case, but we impose delays just in case.
2137 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2142 * special cases, like 6to4, are handled in in6_ifattach
2144 in6_ifattach(ifp, NULL);
2151 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2154 switch (ifp->if_type) {
2160 * These interfaces do not have the IFF_LOOPBACK flag,
2161 * but loop packets back. We do not have to do DAD on such
2162 * interfaces. We should even omit it, because loop-backed
2163 * NS would confuse the DAD procedure.
2168 * Our DAD routine requires the interface up and running.
2169 * However, some interfaces can be up before the RUNNING
2170 * status. Additionaly, users may try to assign addresses
2171 * before the interface becomes up (or running).
2172 * We simply skip DAD in such a case as a work around.
2173 * XXX: we should rather mark "tentative" on such addresses,
2174 * and do DAD after the interface becomes ready.
2176 if (!((ifp->if_flags & IFF_UP) &&
2177 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2185 * Calculate max IPv6 MTU through all the interfaces and store it
2191 unsigned long maxmtu = 0;
2195 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2196 /* this function can be called during ifnet initialization */
2197 if (!ifp->if_afdata[AF_INET6])
2199 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2200 IN6_LINKMTU(ifp) > maxmtu)
2201 maxmtu = IN6_LINKMTU(ifp);
2204 if (maxmtu) /* update only when maxmtu is positive */
2205 in6_maxmtu = maxmtu;
2209 * Provide the length of interface identifiers to be used for the link attached
2210 * to the given interface. The length should be defined in "IPv6 over
2211 * xxx-link" document. Note that address architecture might also define
2212 * the length for a particular set of address prefixes, regardless of the
2213 * link type. As clarified in rfc2462bis, those two definitions should be
2214 * consistent, and those really are as of August 2004.
2220 switch (ifp->if_type) {
2221 case IFT_ETHER: /* RFC2464 */
2222 #ifdef IFT_PROPVIRTUAL
2223 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2226 case IFT_L2VLAN: /* ditto */
2228 #ifdef IFT_IEEE80211
2229 case IFT_IEEE80211: /* ditto */
2232 case IFT_MIP: /* ditto */
2235 case IFT_FDDI: /* RFC2467 */
2237 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2239 case IFT_PPP: /* RFC2472 */
2241 case IFT_ARCNET: /* RFC2497 */
2243 case IFT_FRELAY: /* RFC2590 */
2245 case IFT_IEEE1394: /* RFC3146 */
2248 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2250 return (64); /* XXX: is this really correct? */
2253 * Unknown link type:
2254 * It might be controversial to use the today's common constant
2255 * of 64 for these cases unconditionally. For full compliance,
2256 * we should return an error in this case. On the other hand,
2257 * if we simply miss the standard for the link type or a new
2258 * standard is defined for a new link type, the IFID length
2259 * is very likely to be the common constant. As a compromise,
2260 * we always use the constant, but make an explicit notice
2261 * indicating the "unknown" case.
2263 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2269 in6_domifattach(ifp)
2272 struct in6_ifextra *ext;
2274 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2275 bzero(ext, sizeof(*ext));
2277 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2278 M_IFADDR, M_WAITOK);
2279 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2282 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2283 M_IFADDR, M_WAITOK);
2284 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2286 ext->nd_ifinfo = nd6_ifattach(ifp);
2287 ext->scope6_id = scope6_ifattach(ifp);
2292 in6_domifdetach(ifp, aux)
2296 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2298 scope6_ifdetach(ext->scope6_id);
2299 nd6_ifdetach(ext->nd_ifinfo);
2300 free(ext->in6_ifstat, M_IFADDR);
2301 free(ext->icmp6_ifstat, M_IFADDR);
2302 free(ext, M_IFADDR);
2306 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2307 * v4 mapped addr or v4 compat addr
2310 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2312 bzero(sin, sizeof(*sin));
2313 sin->sin_len = sizeof(struct sockaddr_in);
2314 sin->sin_family = AF_INET;
2315 sin->sin_port = sin6->sin6_port;
2316 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2319 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2321 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2323 bzero(sin6, sizeof(*sin6));
2324 sin6->sin6_len = sizeof(struct sockaddr_in6);
2325 sin6->sin6_family = AF_INET6;
2326 sin6->sin6_port = sin->sin_port;
2327 sin6->sin6_addr.s6_addr32[0] = 0;
2328 sin6->sin6_addr.s6_addr32[1] = 0;
2329 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2330 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2333 /* Convert sockaddr_in6 into sockaddr_in. */
2335 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2337 struct sockaddr_in *sin_p;
2338 struct sockaddr_in6 sin6;
2341 * Save original sockaddr_in6 addr and convert it
2344 sin6 = *(struct sockaddr_in6 *)nam;
2345 sin_p = (struct sockaddr_in *)nam;
2346 in6_sin6_2_sin(sin_p, &sin6);
2349 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2351 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2353 struct sockaddr_in *sin_p;
2354 struct sockaddr_in6 *sin6_p;
2356 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2358 sin_p = (struct sockaddr_in *)*nam;
2359 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2360 FREE(*nam, M_SONAME);
2361 *nam = (struct sockaddr *)sin6_p;