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>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
83 #include <net/if_dl.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/if_ether.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/in_pcb.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/mld6_var.h>
96 #include <netinet6/ip6_mroute.h>
97 #include <netinet6/in6_ifattach.h>
98 #include <netinet6/scope6_var.h>
99 #include <netinet6/in6_pcb.h>
101 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address");
104 * Definitions of some costant IP6 addresses.
106 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
107 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
108 const struct in6_addr in6addr_nodelocal_allnodes =
109 IN6ADDR_NODELOCAL_ALLNODES_INIT;
110 const struct in6_addr in6addr_linklocal_allnodes =
111 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
112 const struct in6_addr in6addr_linklocal_allrouters =
113 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
115 const struct in6_addr in6mask0 = IN6MASK0;
116 const struct in6_addr in6mask32 = IN6MASK32;
117 const struct in6_addr in6mask64 = IN6MASK64;
118 const struct in6_addr in6mask96 = IN6MASK96;
119 const struct in6_addr in6mask128 = IN6MASK128;
121 const struct sockaddr_in6 sa6_any =
122 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
124 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
125 struct ifnet *, struct thread *));
126 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
127 struct sockaddr_in6 *, int));
128 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
130 struct in6_multihead in6_multihead; /* XXX BSS initialization */
131 int (*faithprefix_p)(struct in6_addr *);
134 * Subroutine for in6_ifaddloop() and in6_ifremloop().
135 * This routine does actual work.
138 in6_ifloop_request(int cmd, struct ifaddr *ifa)
140 struct sockaddr_in6 all1_sa;
141 struct rtentry *nrt = NULL;
143 char ip6buf[INET6_ADDRSTRLEN];
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 */
163 log(LOG_ERR, "in6_ifloop_request: "
164 "%s operation failed for %s (errno=%d)\n",
165 cmd == RTM_ADD ? "ADD" : "DELETE",
167 &((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr), e);
171 * Report the addition/removal of the address to the routing socket.
172 * XXX: since we called rtinit for a p2p interface with a destination,
173 * we end up reporting twice in such a case. Should we rather
174 * omit the second report?
179 * Make sure rt_ifa be equal to IFA, the second argument of
180 * the function. We need this because when we refer to
181 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
182 * points to the address instead of the loopback address.
184 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
185 IFAFREE(nrt->rt_ifa);
190 rt_newaddrmsg(cmd, ifa, e, nrt);
191 if (cmd == RTM_DELETE)
194 /* the cmd must be RTM_ADD here */
202 * Add ownaddr as loopback rtentry. We previously add the route only if
203 * necessary (ex. on a p2p link). However, since we now manage addresses
204 * separately from prefixes, we should always add the route. We can't
205 * rely on the cloning mechanism from the corresponding interface route
209 in6_ifaddloop(struct ifaddr *ifa)
214 /* If there is no loopback entry, allocate one. */
215 rt = rtalloc1(ifa->ifa_addr, 0, 0);
216 need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
217 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
221 in6_ifloop_request(RTM_ADD, ifa);
225 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
229 in6_ifremloop(struct ifaddr *ifa)
231 struct in6_ifaddr *ia;
236 * Some of BSD variants do not remove cloned routes
237 * from an interface direct route, when removing the direct route
238 * (see comments in net/net_osdep.h). Even for variants that do remove
239 * cloned routes, they could fail to remove the cloned routes when
240 * we handle multple addresses that share a common prefix.
241 * So, we should remove the route corresponding to the deleted address.
245 * Delete the entry only if exact one ifa exists. More than one ifa
246 * can exist if we assign a same single address to multiple
247 * (probably p2p) interfaces.
248 * XXX: we should avoid such a configuration in IPv6...
250 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
251 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
260 * Before deleting, check if a corresponding loopbacked host
261 * route surely exists. With this check, we can avoid to
262 * delete an interface direct route whose destination is same
263 * as the address being removed. This can happen when removing
264 * a subnet-router anycast address on an interface attahced
265 * to a shared medium.
267 rt = rtalloc1(ifa->ifa_addr, 0, 0);
269 if ((rt->rt_flags & RTF_HOST) != 0 &&
270 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
272 in6_ifloop_request(RTM_DELETE, ifa);
280 in6_mask2len(struct in6_addr *mask, u_char *lim0)
283 u_char *lim = lim0, *p;
285 /* ignore the scope_id part */
286 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
287 lim = (u_char *)mask + sizeof(*mask);
288 for (p = (u_char *)mask; p < lim; x++, p++) {
294 for (y = 0; y < 8; y++) {
295 if ((*p & (0x80 >> y)) == 0)
301 * when the limit pointer is given, do a stricter check on the
305 if (y != 0 && (*p & (0x00ff >> y)) != 0)
307 for (p = p + 1; p < lim; p++)
315 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
316 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
319 in6_control(struct socket *so, u_long cmd, caddr_t data,
320 struct ifnet *ifp, struct thread *td)
322 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
323 struct in6_ifaddr *ia = NULL;
324 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
325 struct sockaddr_in6 *sa6;
329 case SIOCGETSGCNT_IN6:
330 case SIOCGETMIFCNT_IN6:
331 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
335 case SIOCAADDRCTL_POLICY:
336 case SIOCDADDRCTL_POLICY:
338 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
342 return (in6_src_ioctl(cmd, data));
349 case SIOCSNDFLUSH_IN6:
350 case SIOCSPFXFLUSH_IN6:
351 case SIOCSRTRFLUSH_IN6:
352 case SIOCSDEFIFACE_IN6:
353 case SIOCSIFINFO_FLAGS:
355 error = priv_check(td, PRIV_NETINET_ND6);
360 case OSIOCGIFINFO_IN6:
361 case SIOCGIFINFO_IN6:
362 case SIOCSIFINFO_IN6:
365 case SIOCGNBRINFO_IN6:
366 case SIOCGDEFIFACE_IN6:
367 return (nd6_ioctl(cmd, data, ifp));
371 case SIOCSIFPREFIX_IN6:
372 case SIOCDIFPREFIX_IN6:
373 case SIOCAIFPREFIX_IN6:
374 case SIOCCIFPREFIX_IN6:
375 case SIOCSGIFPREFIX_IN6:
376 case SIOCGIFPREFIX_IN6:
378 "prefix ioctls are now invalidated. "
379 "please use ifconfig.\n");
386 error = priv_check(td, PRIV_NETINET_SCOPE6);
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));
404 * XXXRW: Is this checked at another layer? What priv to use
414 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
418 * Find address for this interface, if it exists.
420 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
421 * only, and used the first interface address as the target of other
422 * operations (without checking ifra_addr). This was because netinet
423 * code/API assumed at most 1 interface address per interface.
424 * Since IPv6 allows a node to assign multiple addresses
425 * on a single interface, we almost always look and check the
426 * presence of ifra_addr, and reject invalid ones here.
427 * It also decreases duplicated code among SIOC*_IN6 operations.
430 case SIOCAIFADDR_IN6:
431 case SIOCSIFPHYADDR_IN6:
432 sa6 = &ifra->ifra_addr;
434 case SIOCSIFADDR_IN6:
435 case SIOCGIFADDR_IN6:
436 case SIOCSIFDSTADDR_IN6:
437 case SIOCSIFNETMASK_IN6:
438 case SIOCGIFDSTADDR_IN6:
439 case SIOCGIFNETMASK_IN6:
440 case SIOCDIFADDR_IN6:
441 case SIOCGIFPSRCADDR_IN6:
442 case SIOCGIFPDSTADDR_IN6:
443 case SIOCGIFAFLAG_IN6:
444 case SIOCSNDFLUSH_IN6:
445 case SIOCSPFXFLUSH_IN6:
446 case SIOCSRTRFLUSH_IN6:
447 case SIOCGIFALIFETIME_IN6:
448 case SIOCSIFALIFETIME_IN6:
449 case SIOCGIFSTAT_IN6:
450 case SIOCGIFSTAT_ICMP6:
451 sa6 = &ifr->ifr_addr;
457 if (sa6 && sa6->sin6_family == AF_INET6) {
460 if (sa6->sin6_scope_id != 0)
461 error = sa6_embedscope(sa6, 0);
463 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
466 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
471 case SIOCSIFADDR_IN6:
472 case SIOCSIFDSTADDR_IN6:
473 case SIOCSIFNETMASK_IN6:
475 * Since IPv6 allows a node to assign multiple addresses
476 * on a single interface, SIOCSIFxxx ioctls are deprecated.
478 /* we decided to obsolete this command (20000704) */
481 case SIOCDIFADDR_IN6:
483 * for IPv4, we look for existing in_ifaddr here to allow
484 * "ifconfig if0 delete" to remove the first IPv4 address on
485 * the interface. For IPv6, as the spec allows multiple
486 * interface address from the day one, we consider "remove the
487 * first one" semantics to be not preferable.
490 return (EADDRNOTAVAIL);
492 case SIOCAIFADDR_IN6:
494 * We always require users to specify a valid IPv6 address for
495 * the corresponding operation.
497 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
498 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
499 return (EAFNOSUPPORT);
502 * XXXRW: Is this checked at another layer? What priv to use
513 case SIOCGIFADDR_IN6:
514 /* This interface is basically deprecated. use SIOCGIFCONF. */
516 case SIOCGIFAFLAG_IN6:
517 case SIOCGIFNETMASK_IN6:
518 case SIOCGIFDSTADDR_IN6:
519 case SIOCGIFALIFETIME_IN6:
520 /* must think again about its semantics */
522 return (EADDRNOTAVAIL);
524 case SIOCSIFALIFETIME_IN6:
526 struct in6_addrlifetime *lt;
529 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
534 return (EADDRNOTAVAIL);
535 /* sanity for overflow - beware unsigned */
536 lt = &ifr->ifr_ifru.ifru_lifetime;
537 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
538 lt->ia6t_vltime + time_second < time_second) {
541 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
542 lt->ia6t_pltime + time_second < time_second) {
551 case SIOCGIFADDR_IN6:
552 ifr->ifr_addr = ia->ia_addr;
553 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
557 case SIOCGIFDSTADDR_IN6:
558 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
561 * XXX: should we check if ifa_dstaddr is NULL and return
564 ifr->ifr_dstaddr = ia->ia_dstaddr;
565 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
569 case SIOCGIFNETMASK_IN6:
570 ifr->ifr_addr = ia->ia_prefixmask;
573 case SIOCGIFAFLAG_IN6:
574 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
577 case SIOCGIFSTAT_IN6:
580 bzero(&ifr->ifr_ifru.ifru_stat,
581 sizeof(ifr->ifr_ifru.ifru_stat));
582 ifr->ifr_ifru.ifru_stat =
583 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
586 case SIOCGIFSTAT_ICMP6:
589 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
590 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
591 ifr->ifr_ifru.ifru_icmp6stat =
592 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
595 case SIOCGIFALIFETIME_IN6:
596 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
597 if (ia->ia6_lifetime.ia6t_vltime != 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_vltime <
609 maxexpire - ia->ia6_updatetime) {
610 retlt->ia6t_expire = ia->ia6_updatetime +
611 ia->ia6_lifetime.ia6t_vltime;
613 retlt->ia6t_expire = maxexpire;
615 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
617 struct in6_addrlifetime *retlt =
618 &ifr->ifr_ifru.ifru_lifetime;
621 * XXX: adjust expiration time assuming time_t is
625 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
626 if (ia->ia6_lifetime.ia6t_pltime <
627 maxexpire - ia->ia6_updatetime) {
628 retlt->ia6t_preferred = ia->ia6_updatetime +
629 ia->ia6_lifetime.ia6t_pltime;
631 retlt->ia6t_preferred = maxexpire;
635 case SIOCSIFALIFETIME_IN6:
636 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
638 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
639 ia->ia6_lifetime.ia6t_expire =
640 time_second + ia->ia6_lifetime.ia6t_vltime;
642 ia->ia6_lifetime.ia6t_expire = 0;
643 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
644 ia->ia6_lifetime.ia6t_preferred =
645 time_second + ia->ia6_lifetime.ia6t_pltime;
647 ia->ia6_lifetime.ia6t_preferred = 0;
650 case SIOCAIFADDR_IN6:
653 struct nd_prefixctl pr0;
654 struct nd_prefix *pr;
657 * first, make or update the interface address structure,
658 * and link it to the list.
660 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
662 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
665 * this can happen when the user specify the 0 valid
672 * then, make the prefix on-link on the interface.
673 * XXX: we'd rather create the prefix before the address, but
674 * we need at least one address to install the corresponding
675 * interface route, so we configure the address first.
679 * convert mask to prefix length (prefixmask has already
680 * been validated in in6_update_ifa().
682 bzero(&pr0, sizeof(pr0));
684 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
686 if (pr0.ndpr_plen == 128) {
687 break; /* we don't need to install a host route. */
689 pr0.ndpr_prefix = ifra->ifra_addr;
690 /* apply the mask for safety. */
691 for (i = 0; i < 4; i++) {
692 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
693 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
696 * XXX: since we don't have an API to set prefix (not address)
697 * lifetimes, we just use the same lifetimes as addresses.
698 * The (temporarily) installed lifetimes can be overridden by
699 * later advertised RAs (when accept_rtadv is non 0), which is
700 * an intended behavior.
702 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
704 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
705 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
706 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
708 /* add the prefix if not yet. */
709 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
711 * nd6_prelist_add will install the corresponding
714 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
717 log(LOG_ERR, "nd6_prelist_add succeeded but "
719 return (EINVAL); /* XXX panic here? */
723 /* relate the address to the prefix */
724 if (ia->ia6_ndpr == NULL) {
729 * If this is the first autoconf address from the
730 * prefix, create a temporary address as well
733 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
734 ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
736 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
737 log(LOG_NOTICE, "in6_control: failed "
738 "to create a temporary address, "
745 * this might affect the status of autoconfigured addresses,
746 * that is, this address might make other addresses detached.
748 pfxlist_onlink_check();
749 if (error == 0 && ia)
750 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
754 case SIOCDIFADDR_IN6:
756 struct nd_prefix *pr;
759 * If the address being deleted is the only one that owns
760 * the corresponding prefix, expire the prefix as well.
761 * XXX: theoretically, we don't have to worry about such
762 * relationship, since we separate the address management
763 * and the prefix management. We do this, however, to provide
764 * as much backward compatibility as possible in terms of
765 * the ioctl operation.
766 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
769 in6_purgeaddr(&ia->ia_ifa);
770 if (pr && pr->ndpr_refcnt == 0)
772 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
777 if (ifp == NULL || ifp->if_ioctl == 0)
779 return ((*ifp->if_ioctl)(ifp, cmd, data));
786 * Update parameters of an IPv6 interface address.
787 * If necessary, a new entry is created and linked into address chains.
788 * This function is separated from in6_control().
789 * XXX: should this be performed under splnet()?
792 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
793 struct in6_ifaddr *ia, int flags)
795 int error = 0, hostIsNew = 0, plen = -1;
796 struct in6_ifaddr *oia;
797 struct sockaddr_in6 dst6;
798 struct in6_addrlifetime *lt;
799 struct in6_multi_mship *imm;
800 struct in6_multi *in6m_sol;
803 char ip6buf[INET6_ADDRSTRLEN];
805 /* Validate parameters */
806 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
810 * The destination address for a p2p link must have a family
811 * of AF_UNSPEC or AF_INET6.
813 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
814 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
815 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
816 return (EAFNOSUPPORT);
818 * validate ifra_prefixmask. don't check sin6_family, netmask
819 * does not carry fields other than sin6_len.
821 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
824 * Because the IPv6 address architecture is classless, we require
825 * users to specify a (non 0) prefix length (mask) for a new address.
826 * We also require the prefix (when specified) mask is valid, and thus
827 * reject a non-consecutive mask.
829 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
831 if (ifra->ifra_prefixmask.sin6_len != 0) {
832 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
833 (u_char *)&ifra->ifra_prefixmask +
834 ifra->ifra_prefixmask.sin6_len);
839 * In this case, ia must not be NULL. We just use its prefix
842 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
845 * If the destination address on a p2p interface is specified,
846 * and the address is a scoped one, validate/set the scope
849 dst6 = ifra->ifra_dstaddr;
850 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
851 (dst6.sin6_family == AF_INET6)) {
852 struct in6_addr in6_tmp;
855 in6_tmp = dst6.sin6_addr;
856 if (in6_setscope(&in6_tmp, ifp, &zoneid))
857 return (EINVAL); /* XXX: should be impossible */
859 if (dst6.sin6_scope_id != 0) {
860 if (dst6.sin6_scope_id != zoneid)
862 } else /* user omit to specify the ID. */
863 dst6.sin6_scope_id = zoneid;
865 /* convert into the internal form */
866 if (sa6_embedscope(&dst6, 0))
867 return (EINVAL); /* XXX: should be impossible */
870 * The destination address can be specified only for a p2p or a
871 * loopback interface. If specified, the corresponding prefix length
874 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
875 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
876 /* XXX: noisy message */
877 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
878 "be specified for a p2p or a loopback IF only\n"));
882 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
883 "be 128 when dstaddr is specified\n"));
887 /* lifetime consistency check */
888 lt = &ifra->ifra_lifetime;
889 if (lt->ia6t_pltime > lt->ia6t_vltime)
891 if (lt->ia6t_vltime == 0) {
893 * the following log might be noisy, but this is a typical
894 * configuration mistake or a tool's bug.
897 "in6_update_ifa: valid lifetime is 0 for %s\n",
898 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
901 return (0); /* there's nothing to do */
905 * If this is a new address, allocate a new ifaddr and link it
911 * When in6_update_ifa() is called in a process of a received
912 * RA, it is called under an interrupt context. So, we should
913 * call malloc with M_NOWAIT.
915 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
919 bzero((caddr_t)ia, sizeof(*ia));
920 LIST_INIT(&ia->ia6_memberships);
921 /* Initialize the address and masks, and put time stamp */
922 IFA_LOCK_INIT(&ia->ia_ifa);
923 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
924 ia->ia_addr.sin6_family = AF_INET6;
925 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
926 ia->ia6_createtime = time_second;
927 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
929 * XXX: some functions expect that ifa_dstaddr is not
930 * NULL for p2p interfaces.
932 ia->ia_ifa.ifa_dstaddr =
933 (struct sockaddr *)&ia->ia_dstaddr;
935 ia->ia_ifa.ifa_dstaddr = NULL;
937 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
940 if ((oia = in6_ifaddr) != NULL) {
941 for ( ; oia->ia_next; oia = oia->ia_next)
947 ia->ia_ifa.ifa_refcnt = 1;
948 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
951 /* update timestamp */
952 ia->ia6_updatetime = time_second;
954 /* set prefix mask */
955 if (ifra->ifra_prefixmask.sin6_len) {
957 * We prohibit changing the prefix length of an existing
959 * + such an operation should be rare in IPv6, and
960 * + the operation would confuse prefix management.
962 if (ia->ia_prefixmask.sin6_len &&
963 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
964 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
965 " existing (%s) address should not be changed\n",
966 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
970 ia->ia_prefixmask = ifra->ifra_prefixmask;
974 * If a new destination address is specified, scrub the old one and
975 * install the new destination. Note that the interface must be
976 * p2p or loopback (see the check above.)
978 if (dst6.sin6_family == AF_INET6 &&
979 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
982 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
983 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
984 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
985 "a route to the old destination: %s\n",
986 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
987 /* proceed anyway... */
989 ia->ia_flags &= ~IFA_ROUTE;
990 ia->ia_dstaddr = dst6;
994 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
995 * to see if the address is deprecated or invalidated, but initialize
996 * these members for applications.
998 ia->ia6_lifetime = ifra->ifra_lifetime;
999 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1000 ia->ia6_lifetime.ia6t_expire =
1001 time_second + ia->ia6_lifetime.ia6t_vltime;
1003 ia->ia6_lifetime.ia6t_expire = 0;
1004 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1005 ia->ia6_lifetime.ia6t_preferred =
1006 time_second + ia->ia6_lifetime.ia6t_pltime;
1008 ia->ia6_lifetime.ia6t_preferred = 0;
1010 /* reset the interface and routing table appropriately. */
1011 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1015 * configure address flags.
1017 ia->ia6_flags = ifra->ifra_flags;
1019 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1020 * userland, make it deprecated.
1022 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1023 ia->ia6_lifetime.ia6t_pltime = 0;
1024 ia->ia6_lifetime.ia6t_preferred = time_second;
1027 * Make the address tentative before joining multicast addresses,
1028 * so that corresponding MLD responses would not have a tentative
1031 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1032 if (hostIsNew && in6if_do_dad(ifp))
1033 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1036 * We are done if we have simply modified an existing address.
1042 * Beyond this point, we should call in6_purgeaddr upon an error,
1043 * not just go to unlink.
1046 /* Join necessary multicast groups */
1048 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1049 struct sockaddr_in6 mltaddr, mltmask;
1050 struct in6_addr llsol;
1052 /* join solicited multicast addr for new host id */
1053 bzero(&llsol, sizeof(struct in6_addr));
1054 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1055 llsol.s6_addr32[1] = 0;
1056 llsol.s6_addr32[2] = htonl(1);
1057 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1058 llsol.s6_addr8[12] = 0xff;
1059 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1060 /* XXX: should not happen */
1061 log(LOG_ERR, "in6_update_ifa: "
1062 "in6_setscope failed\n");
1066 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1068 * We need a random delay for DAD on the address
1069 * being configured. It also means delaying
1070 * transmission of the corresponding MLD report to
1071 * avoid report collision.
1072 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1074 delay = arc4random() %
1075 (MAX_RTR_SOLICITATION_DELAY * hz);
1077 imm = in6_joingroup(ifp, &llsol, &error, delay);
1079 nd6log((LOG_WARNING,
1080 "in6_update_ifa: addmulti failed for "
1081 "%s on %s (errno=%d)\n",
1082 ip6_sprintf(ip6buf, &llsol), if_name(ifp),
1084 in6_purgeaddr((struct ifaddr *)ia);
1087 LIST_INSERT_HEAD(&ia->ia6_memberships,
1089 in6m_sol = imm->i6mm_maddr;
1091 bzero(&mltmask, sizeof(mltmask));
1092 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1093 mltmask.sin6_family = AF_INET6;
1094 mltmask.sin6_addr = in6mask32;
1095 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1098 * join link-local all-nodes address
1100 bzero(&mltaddr, sizeof(mltaddr));
1101 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1102 mltaddr.sin6_family = AF_INET6;
1103 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1104 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1106 goto cleanup; /* XXX: should not fail */
1109 * XXX: do we really need this automatic routes?
1110 * We should probably reconsider this stuff. Most applications
1111 * actually do not need the routes, since they usually specify
1112 * the outgoing interface.
1114 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1116 if (memcmp(&mltaddr.sin6_addr,
1117 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1124 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1125 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1126 (struct sockaddr *)&ia->ia_addr,
1127 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1128 (struct rtentry **)0);
1135 * XXX: do we really need this automatic routes?
1136 * We should probably reconsider this stuff. Most applications
1137 * actually do not need the routes, since they usually specify
1138 * the outgoing interface.
1140 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1142 /* XXX: only works in !SCOPEDROUTING case. */
1143 if (memcmp(&mltaddr.sin6_addr,
1144 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1151 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1152 (struct sockaddr *)&ia->ia_addr,
1153 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1154 (struct rtentry **)0);
1161 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1163 nd6log((LOG_WARNING,
1164 "in6_update_ifa: addmulti failed for "
1165 "%s on %s (errno=%d)\n",
1166 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1167 if_name(ifp), error));
1170 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1173 * join node information group address
1175 #define hostnamelen strlen(hostname)
1177 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1179 * The spec doesn't say anything about delay for this
1180 * group, but the same logic should apply.
1182 delay = arc4random() %
1183 (MAX_RTR_SOLICITATION_DELAY * hz);
1185 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1187 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1188 delay); /* XXX jinmei */
1190 nd6log((LOG_WARNING, "in6_update_ifa: "
1191 "addmulti failed for %s on %s "
1193 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1194 if_name(ifp), error));
1195 /* XXX not very fatal, go on... */
1197 LIST_INSERT_HEAD(&ia->ia6_memberships,
1204 * join interface-local all-nodes address.
1205 * (ff01::1%ifN, and ff01::%ifN/32)
1207 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1208 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1210 goto cleanup; /* XXX: should not fail */
1211 /* XXX: again, do we really need the route? */
1212 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1214 if (memcmp(&mltaddr.sin6_addr,
1215 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1222 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1223 (struct sockaddr *)&ia->ia_addr,
1224 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1225 (struct rtentry **)0);
1231 /* XXX: again, do we really need the route? */
1232 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1234 if (memcmp(&mltaddr.sin6_addr,
1235 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1242 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1243 (struct sockaddr *)&ia->ia_addr,
1244 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1245 (struct rtentry **)0);
1252 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1254 nd6log((LOG_WARNING, "in6_update_ifa: "
1255 "addmulti failed for %s on %s "
1257 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1258 if_name(ifp), error));
1261 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1266 * Perform DAD, if needed.
1267 * XXX It may be of use, if we can administratively
1270 if (hostIsNew && in6if_do_dad(ifp) &&
1271 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1272 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1274 int mindelay, maxdelay;
1277 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1279 * We need to impose a delay before sending an NS
1280 * for DAD. Check if we also needed a delay for the
1281 * corresponding MLD message. If we did, the delay
1282 * should be larger than the MLD delay (this could be
1283 * relaxed a bit, but this simple logic is at least
1287 if (in6m_sol != NULL &&
1288 in6m_sol->in6m_state == MLD_REPORTPENDING) {
1289 mindelay = in6m_sol->in6m_timer;
1291 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1292 if (maxdelay - mindelay == 0)
1296 (arc4random() % (maxdelay - mindelay)) +
1300 nd6_dad_start((struct ifaddr *)ia, delay);
1307 * XXX: if a change of an existing address failed, keep the entry
1311 in6_unlink_ifa(ia, ifp);
1315 in6_purgeaddr(&ia->ia_ifa);
1320 in6_purgeaddr(struct ifaddr *ifa)
1322 struct ifnet *ifp = ifa->ifa_ifp;
1323 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1324 char ip6buf[INET6_ADDRSTRLEN];
1325 struct in6_multi_mship *imm;
1327 /* stop DAD processing */
1331 * delete route to the destination of the address being purged.
1332 * The interface must be p2p or loopback in this case.
1334 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1337 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1339 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1340 "a route to the p2p destination: %s on %s, "
1342 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1344 /* proceed anyway... */
1346 ia->ia_flags &= ~IFA_ROUTE;
1349 /* Remove ownaddr's loopback rtentry, if it exists. */
1350 in6_ifremloop(&(ia->ia_ifa));
1353 * leave from multicast groups we have joined for the interface
1355 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1356 LIST_REMOVE(imm, i6mm_chain);
1357 in6_leavegroup(imm);
1360 in6_unlink_ifa(ia, ifp);
1364 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1366 struct in6_ifaddr *oia;
1369 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1372 if (oia == (ia = in6_ifaddr))
1373 in6_ifaddr = ia->ia_next;
1375 while (ia->ia_next && (ia->ia_next != oia))
1378 ia->ia_next = oia->ia_next;
1381 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1386 * Release the reference to the base prefix. There should be a
1387 * positive reference.
1389 if (oia->ia6_ndpr == NULL) {
1391 "in6_unlink_ifa: autoconf'ed address "
1392 "%p has no prefix\n", oia));
1394 oia->ia6_ndpr->ndpr_refcnt--;
1395 oia->ia6_ndpr = NULL;
1399 * Also, if the address being removed is autoconf'ed, call
1400 * pfxlist_onlink_check() since the release might affect the status of
1401 * other (detached) addresses.
1403 if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) {
1404 pfxlist_onlink_check();
1408 * release another refcnt for the link from in6_ifaddr.
1409 * Note that we should decrement the refcnt at least once for all *BSD.
1411 IFAFREE(&oia->ia_ifa);
1417 in6_purgeif(struct ifnet *ifp)
1419 struct ifaddr *ifa, *nifa;
1421 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1422 nifa = TAILQ_NEXT(ifa, ifa_list);
1423 if (ifa->ifa_addr->sa_family != AF_INET6)
1433 * SIOCGLIFADDR: get first address. (?)
1434 * SIOCGLIFADDR with IFLR_PREFIX:
1435 * get first address that matches the specified prefix.
1436 * SIOCALIFADDR: add the specified address.
1437 * SIOCALIFADDR with IFLR_PREFIX:
1438 * add the specified prefix, filling hostid part from
1439 * the first link-local address. prefixlen must be <= 64.
1440 * SIOCDLIFADDR: delete the specified address.
1441 * SIOCDLIFADDR with IFLR_PREFIX:
1442 * delete the first address that matches the specified prefix.
1444 * EINVAL on invalid parameters
1445 * EADDRNOTAVAIL on prefix match failed/specified address not found
1446 * other values may be returned from in6_ioctl()
1448 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1449 * this is to accomodate address naming scheme other than RFC2374,
1451 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1452 * address encoding scheme. (see figure on page 8)
1455 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1456 struct ifnet *ifp, struct thread *td)
1458 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1460 struct sockaddr *sa;
1463 if (!data || !ifp) {
1464 panic("invalid argument to in6_lifaddr_ioctl");
1470 /* address must be specified on GET with IFLR_PREFIX */
1471 if ((iflr->flags & IFLR_PREFIX) == 0)
1476 /* address must be specified on ADD and DELETE */
1477 sa = (struct sockaddr *)&iflr->addr;
1478 if (sa->sa_family != AF_INET6)
1480 if (sa->sa_len != sizeof(struct sockaddr_in6))
1482 /* XXX need improvement */
1483 sa = (struct sockaddr *)&iflr->dstaddr;
1484 if (sa->sa_family && sa->sa_family != AF_INET6)
1486 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1489 default: /* shouldn't happen */
1491 panic("invalid cmd to in6_lifaddr_ioctl");
1497 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1503 struct in6_aliasreq ifra;
1504 struct in6_addr *hostid = NULL;
1507 if ((iflr->flags & IFLR_PREFIX) != 0) {
1508 struct sockaddr_in6 *sin6;
1511 * hostid is to fill in the hostid part of the
1512 * address. hostid points to the first link-local
1513 * address attached to the interface.
1515 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1517 return EADDRNOTAVAIL;
1518 hostid = IFA_IN6(ifa);
1520 /* prefixlen must be <= 64. */
1521 if (64 < iflr->prefixlen)
1523 prefixlen = iflr->prefixlen;
1525 /* hostid part must be zero. */
1526 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1527 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1528 sin6->sin6_addr.s6_addr32[3] != 0) {
1532 prefixlen = iflr->prefixlen;
1534 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1535 bzero(&ifra, sizeof(ifra));
1536 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1538 bcopy(&iflr->addr, &ifra.ifra_addr,
1539 ((struct sockaddr *)&iflr->addr)->sa_len);
1541 /* fill in hostid part */
1542 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1543 hostid->s6_addr32[2];
1544 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1545 hostid->s6_addr32[3];
1548 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1549 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1550 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1552 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1553 hostid->s6_addr32[2];
1554 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1555 hostid->s6_addr32[3];
1559 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1560 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1562 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1563 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1568 struct in6_ifaddr *ia;
1569 struct in6_addr mask, candidate, match;
1570 struct sockaddr_in6 *sin6;
1573 bzero(&mask, sizeof(mask));
1574 if (iflr->flags & IFLR_PREFIX) {
1575 /* lookup a prefix rather than address. */
1576 in6_prefixlen2mask(&mask, iflr->prefixlen);
1578 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1579 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1580 match.s6_addr32[0] &= mask.s6_addr32[0];
1581 match.s6_addr32[1] &= mask.s6_addr32[1];
1582 match.s6_addr32[2] &= mask.s6_addr32[2];
1583 match.s6_addr32[3] &= mask.s6_addr32[3];
1585 /* if you set extra bits, that's wrong */
1586 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1591 if (cmd == SIOCGLIFADDR) {
1592 /* on getting an address, take the 1st match */
1595 /* on deleting an address, do exact match */
1596 in6_prefixlen2mask(&mask, 128);
1597 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1598 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1604 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1605 if (ifa->ifa_addr->sa_family != AF_INET6)
1611 * XXX: this is adhoc, but is necessary to allow
1612 * a user to specify fe80::/64 (not /10) for a
1613 * link-local address.
1615 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1616 in6_clearscope(&candidate);
1617 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1618 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1619 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1620 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1621 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1625 return EADDRNOTAVAIL;
1628 if (cmd == SIOCGLIFADDR) {
1631 /* fill in the if_laddrreq structure */
1632 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1633 error = sa6_recoverscope(
1634 (struct sockaddr_in6 *)&iflr->addr);
1638 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1639 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1640 ia->ia_dstaddr.sin6_len);
1641 error = sa6_recoverscope(
1642 (struct sockaddr_in6 *)&iflr->dstaddr);
1646 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1649 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1651 iflr->flags = ia->ia6_flags; /* XXX */
1655 struct in6_aliasreq ifra;
1657 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1658 bzero(&ifra, sizeof(ifra));
1659 bcopy(iflr->iflr_name, ifra.ifra_name,
1660 sizeof(ifra.ifra_name));
1662 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1663 ia->ia_addr.sin6_len);
1664 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1665 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1666 ia->ia_dstaddr.sin6_len);
1668 bzero(&ifra.ifra_dstaddr,
1669 sizeof(ifra.ifra_dstaddr));
1671 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1672 ia->ia_prefixmask.sin6_len);
1674 ifra.ifra_flags = ia->ia6_flags;
1675 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1681 return EOPNOTSUPP; /* just for safety */
1685 * Initialize an interface's intetnet6 address
1686 * and routing table entry.
1689 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1690 struct sockaddr_in6 *sin6, int newhost)
1692 int error = 0, plen, ifacount = 0;
1697 * Give the interface a chance to initialize
1698 * if this is its first address,
1699 * and to validate the address if necessary.
1701 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1702 if (ifa->ifa_addr->sa_family != AF_INET6)
1707 ia->ia_addr = *sin6;
1709 if (ifacount <= 1 && ifp->if_ioctl) {
1711 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1712 IFF_UNLOCKGIANT(ifp);
1720 ia->ia_ifa.ifa_metric = ifp->if_metric;
1722 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1726 * set the rtrequest function to create llinfo. It also
1727 * adjust outgoing interface of the route for the local
1728 * address when called via in6_ifaddloop() below.
1730 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1735 * If a new destination address is specified for a point-to-point
1736 * interface, install a route to the destination as an interface
1737 * direct route. In addition, if the link is expected to have neighbor
1738 * cache entries, specify RTF_LLINFO so that a cache entry for the
1739 * destination address will be created.
1741 * XXX: the logic below rejects assigning multiple addresses on a p2p
1742 * interface that share the same destination.
1744 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1745 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1746 ia->ia_dstaddr.sin6_family == AF_INET6) {
1747 int rtflags = RTF_UP | RTF_HOST;
1748 struct rtentry *rt = NULL, **rtp = NULL;
1750 if (nd6_need_cache(ifp) != 0) {
1751 rtflags |= RTF_LLINFO;
1755 error = rtrequest(RTM_ADD, (struct sockaddr *)&ia->ia_dstaddr,
1756 (struct sockaddr *)&ia->ia_addr,
1757 (struct sockaddr *)&ia->ia_prefixmask,
1758 ia->ia_flags | rtflags, rtp);
1762 struct llinfo_nd6 *ln;
1765 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1768 * Set the state to STALE because we don't
1769 * have to perform address resolution on this
1772 ln->ln_state = ND6_LLINFO_STALE;
1777 ia->ia_flags |= IFA_ROUTE;
1781 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1783 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1786 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1788 in6_ifaddloop(&(ia->ia_ifa));
1793 struct in6_multi_mship *
1794 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
1795 int *errorp, int delay)
1797 struct in6_multi_mship *imm;
1799 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1804 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay);
1805 if (!imm->i6mm_maddr) {
1806 /* *errorp is alrady set */
1807 free(imm, M_IP6MADDR);
1814 in6_leavegroup(struct in6_multi_mship *imm)
1817 if (imm->i6mm_maddr)
1818 in6_delmulti(imm->i6mm_maddr);
1819 free(imm, M_IP6MADDR);
1824 * Find an IPv6 interface link-local address specific to an interface.
1827 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1831 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1832 if (ifa->ifa_addr->sa_family != AF_INET6)
1834 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1835 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1842 return ((struct in6_ifaddr *)ifa);
1847 * find the internet address corresponding to a given interface and address.
1850 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1854 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1855 if (ifa->ifa_addr->sa_family != AF_INET6)
1857 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1861 return ((struct in6_ifaddr *)ifa);
1865 * Convert IP6 address to printable (loggable) representation. Caller
1866 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1868 static char digits[] = "0123456789abcdef";
1870 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1874 const u_int16_t *a = (const u_int16_t *)addr;
1876 int dcolon = 0, zero = 0;
1880 for (i = 0; i < 8; i++) {
1891 if (dcolon == 0 && *(a + 1) == 0) {
1903 d = (const u_char *)a;
1904 /* Try to eliminate leading zeros in printout like in :0001. */
1906 *cp = digits[*d >> 4];
1911 *cp = digits[*d++ & 0xf];
1912 if (zero == 0 || (*cp != '0')) {
1916 *cp = digits[*d >> 4];
1917 if (zero == 0 || (*cp != '0')) {
1921 *cp++ = digits[*d & 0xf];
1930 in6_localaddr(struct in6_addr *in6)
1932 struct in6_ifaddr *ia;
1934 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1937 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1938 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1939 &ia->ia_prefixmask.sin6_addr)) {
1948 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1950 struct in6_ifaddr *ia;
1952 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1953 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1955 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1956 return (1); /* true */
1958 /* XXX: do we still have to go thru the rest of the list? */
1961 return (0); /* false */
1965 * return length of part which dst and src are equal
1969 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1972 u_char *s = (u_char *)src, *d = (u_char *)dst;
1973 u_char *lim = s + 16, r;
1976 if ((r = (*d++ ^ *s++)) != 0) {
1987 /* XXX: to be scope conscious */
1989 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1991 int bytelen, bitlen;
1994 if (0 > len || len > 128) {
1995 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2003 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2006 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2007 p2->s6_addr[bytelen] >> (8 - bitlen))
2014 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2016 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2017 int bytelen, bitlen, i;
2020 if (0 > len || len > 128) {
2021 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2026 bzero(maskp, sizeof(*maskp));
2029 for (i = 0; i < bytelen; i++)
2030 maskp->s6_addr[i] = 0xff;
2032 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2036 * return the best address out of the same scope. if no address was
2037 * found, return the first valid address from designated IF.
2040 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2042 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2044 struct in6_ifaddr *besta = 0;
2045 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2047 dep[0] = dep[1] = NULL;
2050 * We first look for addresses in the same scope.
2051 * If there is one, return it.
2052 * If two or more, return one which matches the dst longest.
2053 * If none, return one of global addresses assigned other ifs.
2055 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2056 if (ifa->ifa_addr->sa_family != AF_INET6)
2058 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2059 continue; /* XXX: is there any case to allow anycast? */
2060 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2061 continue; /* don't use this interface */
2062 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2064 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2065 if (ip6_use_deprecated)
2066 dep[0] = (struct in6_ifaddr *)ifa;
2070 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2072 * call in6_matchlen() as few as possible
2076 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2077 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2080 besta = (struct in6_ifaddr *)ifa;
2083 besta = (struct in6_ifaddr *)ifa;
2089 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2090 if (ifa->ifa_addr->sa_family != AF_INET6)
2092 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2093 continue; /* XXX: is there any case to allow anycast? */
2094 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2095 continue; /* don't use this interface */
2096 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2098 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2099 if (ip6_use_deprecated)
2100 dep[1] = (struct in6_ifaddr *)ifa;
2104 return (struct in6_ifaddr *)ifa;
2107 /* use the last-resort values, that are, deprecated addresses */
2117 * perform DAD when interface becomes IFF_UP.
2120 in6_if_up(struct ifnet *ifp)
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);
2148 in6if_do_dad(struct ifnet *ifp)
2150 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2153 switch (ifp->if_type) {
2159 * These interfaces do not have the IFF_LOOPBACK flag,
2160 * but loop packets back. We do not have to do DAD on such
2161 * interfaces. We should even omit it, because loop-backed
2162 * NS would confuse the DAD procedure.
2167 * Our DAD routine requires the interface up and running.
2168 * However, some interfaces can be up before the RUNNING
2169 * status. Additionaly, users may try to assign addresses
2170 * before the interface becomes up (or running).
2171 * We simply skip DAD in such a case as a work around.
2172 * XXX: we should rather mark "tentative" on such addresses,
2173 * and do DAD after the interface becomes ready.
2175 if (!((ifp->if_flags & IFF_UP) &&
2176 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2184 * Calculate max IPv6 MTU through all the interfaces and store it
2190 unsigned long maxmtu = 0;
2194 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2195 /* this function can be called during ifnet initialization */
2196 if (!ifp->if_afdata[AF_INET6])
2198 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2199 IN6_LINKMTU(ifp) > maxmtu)
2200 maxmtu = IN6_LINKMTU(ifp);
2203 if (maxmtu) /* update only when maxmtu is positive */
2204 in6_maxmtu = maxmtu;
2208 * Provide the length of interface identifiers to be used for the link attached
2209 * to the given interface. The length should be defined in "IPv6 over
2210 * xxx-link" document. Note that address architecture might also define
2211 * the length for a particular set of address prefixes, regardless of the
2212 * link type. As clarified in rfc2462bis, those two definitions should be
2213 * consistent, and those really are as of August 2004.
2216 in6_if2idlen(struct ifnet *ifp)
2218 switch (ifp->if_type) {
2219 case IFT_ETHER: /* RFC2464 */
2220 #ifdef IFT_PROPVIRTUAL
2221 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2224 case IFT_L2VLAN: /* ditto */
2226 #ifdef IFT_IEEE80211
2227 case IFT_IEEE80211: /* ditto */
2230 case IFT_MIP: /* ditto */
2233 case IFT_FDDI: /* RFC2467 */
2235 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2237 case IFT_PPP: /* RFC2472 */
2239 case IFT_ARCNET: /* RFC2497 */
2241 case IFT_FRELAY: /* RFC2590 */
2243 case IFT_IEEE1394: /* RFC3146 */
2246 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2248 return (64); /* XXX: is this really correct? */
2251 * Unknown link type:
2252 * It might be controversial to use the today's common constant
2253 * of 64 for these cases unconditionally. For full compliance,
2254 * we should return an error in this case. On the other hand,
2255 * if we simply miss the standard for the link type or a new
2256 * standard is defined for a new link type, the IFID length
2257 * is very likely to be the common constant. As a compromise,
2258 * we always use the constant, but make an explicit notice
2259 * indicating the "unknown" case.
2261 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2267 in6_domifattach(struct ifnet *ifp)
2269 struct in6_ifextra *ext;
2271 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2272 bzero(ext, sizeof(*ext));
2274 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2275 M_IFADDR, M_WAITOK);
2276 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2279 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2280 M_IFADDR, M_WAITOK);
2281 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2283 ext->nd_ifinfo = nd6_ifattach(ifp);
2284 ext->scope6_id = scope6_ifattach(ifp);
2289 in6_domifdetach(struct ifnet *ifp, void *aux)
2291 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2293 scope6_ifdetach(ext->scope6_id);
2294 nd6_ifdetach(ext->nd_ifinfo);
2295 free(ext->in6_ifstat, M_IFADDR);
2296 free(ext->icmp6_ifstat, M_IFADDR);
2297 free(ext, M_IFADDR);
2301 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2302 * v4 mapped addr or v4 compat addr
2305 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2308 bzero(sin, sizeof(*sin));
2309 sin->sin_len = sizeof(struct sockaddr_in);
2310 sin->sin_family = AF_INET;
2311 sin->sin_port = sin6->sin6_port;
2312 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2315 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2317 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2319 bzero(sin6, sizeof(*sin6));
2320 sin6->sin6_len = sizeof(struct sockaddr_in6);
2321 sin6->sin6_family = AF_INET6;
2322 sin6->sin6_port = sin->sin_port;
2323 sin6->sin6_addr.s6_addr32[0] = 0;
2324 sin6->sin6_addr.s6_addr32[1] = 0;
2325 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2326 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2329 /* Convert sockaddr_in6 into sockaddr_in. */
2331 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2333 struct sockaddr_in *sin_p;
2334 struct sockaddr_in6 sin6;
2337 * Save original sockaddr_in6 addr and convert it
2340 sin6 = *(struct sockaddr_in6 *)nam;
2341 sin_p = (struct sockaddr_in *)nam;
2342 in6_sin6_2_sin(sin_p, &sin6);
2345 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2347 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2349 struct sockaddr_in *sin_p;
2350 struct sockaddr_in6 *sin6_p;
2352 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2354 sin_p = (struct sockaddr_in *)*nam;
2355 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2356 FREE(*nam, M_SONAME);
2357 *nam = (struct sockaddr *)sin6_p;