2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)in.c 8.2 (Berkeley) 11/15/93
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
69 #include <sys/param.h>
70 #include <sys/errno.h>
72 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/syslog.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86 #include <net/if_dl.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/if_ether.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/nd6.h>
98 #include <netinet6/mld6_var.h>
99 #include <netinet6/ip6_mroute.h>
100 #include <netinet6/in6_ifattach.h>
101 #include <netinet6/scope6_var.h>
102 #include <netinet6/in6_pcb.h>
104 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address");
107 * Definitions of some costant IP6 addresses.
109 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
110 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
111 const struct in6_addr in6addr_nodelocal_allnodes =
112 IN6ADDR_NODELOCAL_ALLNODES_INIT;
113 const struct in6_addr in6addr_linklocal_allnodes =
114 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
115 const struct in6_addr in6addr_linklocal_allrouters =
116 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
118 const struct in6_addr in6mask0 = IN6MASK0;
119 const struct in6_addr in6mask32 = IN6MASK32;
120 const struct in6_addr in6mask64 = IN6MASK64;
121 const struct in6_addr in6mask96 = IN6MASK96;
122 const struct in6_addr in6mask128 = IN6MASK128;
124 const struct sockaddr_in6 sa6_any =
125 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
127 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
128 struct ifnet *, struct thread *));
129 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
130 struct sockaddr_in6 *, int));
131 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
133 struct in6_multihead in6_multihead; /* XXX BSS initialization */
134 int (*faithprefix_p)(struct in6_addr *);
137 * Subroutine for in6_ifaddloop() and in6_ifremloop().
138 * This routine does actual work.
141 in6_ifloop_request(int cmd, struct ifaddr *ifa)
143 struct sockaddr_in6 all1_sa;
144 struct rtentry *nrt = NULL;
146 char ip6buf[INET6_ADDRSTRLEN];
148 bzero(&all1_sa, sizeof(all1_sa));
149 all1_sa.sin6_family = AF_INET6;
150 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
151 all1_sa.sin6_addr = in6mask128;
154 * We specify the address itself as the gateway, and set the
155 * RTF_LLINFO flag, so that the corresponding host route would have
156 * the flag, and thus applications that assume traditional behavior
157 * would be happy. Note that we assume the caller of the function
158 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
159 * which changes the outgoing interface to the loopback interface.
161 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
162 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
164 /* XXX need more descriptive message */
166 log(LOG_ERR, "in6_ifloop_request: "
167 "%s operation failed for %s (errno=%d)\n",
168 cmd == RTM_ADD ? "ADD" : "DELETE",
170 &((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr), e);
174 * Report the addition/removal of the address to the routing socket.
175 * XXX: since we called rtinit for a p2p interface with a destination,
176 * we end up reporting twice in such a case. Should we rather
177 * omit the second report?
182 * Make sure rt_ifa be equal to IFA, the second argument of
183 * the function. We need this because when we refer to
184 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
185 * points to the address instead of the loopback address.
187 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
188 IFAFREE(nrt->rt_ifa);
193 rt_newaddrmsg(cmd, ifa, e, nrt);
194 if (cmd == RTM_DELETE)
197 /* the cmd must be RTM_ADD here */
205 * Add ownaddr as loopback rtentry. We previously add the route only if
206 * necessary (ex. on a p2p link). However, since we now manage addresses
207 * separately from prefixes, we should always add the route. We can't
208 * rely on the cloning mechanism from the corresponding interface route
212 in6_ifaddloop(struct ifaddr *ifa)
217 /* If there is no loopback entry, allocate one. */
218 rt = rtalloc1(ifa->ifa_addr, 0, 0);
219 need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
220 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
224 in6_ifloop_request(RTM_ADD, ifa);
228 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
232 in6_ifremloop(struct ifaddr *ifa)
234 struct in6_ifaddr *ia;
239 * Some of BSD variants do not remove cloned routes
240 * from an interface direct route, when removing the direct route
241 * (see comments in net/net_osdep.h). Even for variants that do remove
242 * cloned routes, they could fail to remove the cloned routes when
243 * we handle multple addresses that share a common prefix.
244 * So, we should remove the route corresponding to the deleted address.
248 * Delete the entry only if exact one ifa exists. More than one ifa
249 * can exist if we assign a same single address to multiple
250 * (probably p2p) interfaces.
251 * XXX: we should avoid such a configuration in IPv6...
253 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
254 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
263 * Before deleting, check if a corresponding loopbacked host
264 * route surely exists. With this check, we can avoid to
265 * delete an interface direct route whose destination is same
266 * as the address being removed. This can happen when removing
267 * a subnet-router anycast address on an interface attahced
268 * to a shared medium.
270 rt = rtalloc1(ifa->ifa_addr, 0, 0);
272 if ((rt->rt_flags & RTF_HOST) != 0 &&
273 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
275 in6_ifloop_request(RTM_DELETE, ifa);
283 in6_mask2len(struct in6_addr *mask, u_char *lim0)
286 u_char *lim = lim0, *p;
288 /* ignore the scope_id part */
289 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
290 lim = (u_char *)mask + sizeof(*mask);
291 for (p = (u_char *)mask; p < lim; x++, p++) {
297 for (y = 0; y < 8; y++) {
298 if ((*p & (0x80 >> y)) == 0)
304 * when the limit pointer is given, do a stricter check on the
308 if (y != 0 && (*p & (0x00ff >> y)) != 0)
310 for (p = p + 1; p < lim; p++)
318 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
319 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
322 in6_control(struct socket *so, u_long cmd, caddr_t data,
323 struct ifnet *ifp, struct thread *td)
325 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
326 struct in6_ifaddr *ia = NULL;
327 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
328 struct sockaddr_in6 *sa6;
332 case SIOCGETSGCNT_IN6:
333 case SIOCGETMIFCNT_IN6:
334 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
338 case SIOCAADDRCTL_POLICY:
339 case SIOCDADDRCTL_POLICY:
341 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
345 return (in6_src_ioctl(cmd, data));
352 case SIOCSNDFLUSH_IN6:
353 case SIOCSPFXFLUSH_IN6:
354 case SIOCSRTRFLUSH_IN6:
355 case SIOCSDEFIFACE_IN6:
356 case SIOCSIFINFO_FLAGS:
357 case SIOCSIFINFO_IN6:
359 error = priv_check(td, PRIV_NETINET_ND6);
364 case OSIOCGIFINFO_IN6:
365 case SIOCGIFINFO_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");
389 error = priv_check(td, PRIV_NETINET_SCOPE6);
393 return (scope6_set(ifp,
394 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
396 return (scope6_get(ifp,
397 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
399 return (scope6_get_default((struct scope6_id *)
400 ifr->ifr_ifru.ifru_scope_id));
406 error = priv_check(td, PRIV_NET_ADDIFADDR);
410 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
414 error = priv_check(td, PRIV_NET_DELIFADDR);
420 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
424 * Find address for this interface, if it exists.
426 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
427 * only, and used the first interface address as the target of other
428 * operations (without checking ifra_addr). This was because netinet
429 * code/API assumed at most 1 interface address per interface.
430 * Since IPv6 allows a node to assign multiple addresses
431 * on a single interface, we almost always look and check the
432 * presence of ifra_addr, and reject invalid ones here.
433 * It also decreases duplicated code among SIOC*_IN6 operations.
436 case SIOCAIFADDR_IN6:
437 case SIOCSIFPHYADDR_IN6:
438 sa6 = &ifra->ifra_addr;
440 case SIOCSIFADDR_IN6:
441 case SIOCGIFADDR_IN6:
442 case SIOCSIFDSTADDR_IN6:
443 case SIOCSIFNETMASK_IN6:
444 case SIOCGIFDSTADDR_IN6:
445 case SIOCGIFNETMASK_IN6:
446 case SIOCDIFADDR_IN6:
447 case SIOCGIFPSRCADDR_IN6:
448 case SIOCGIFPDSTADDR_IN6:
449 case SIOCGIFAFLAG_IN6:
450 case SIOCSNDFLUSH_IN6:
451 case SIOCSPFXFLUSH_IN6:
452 case SIOCSRTRFLUSH_IN6:
453 case SIOCGIFALIFETIME_IN6:
454 case SIOCSIFALIFETIME_IN6:
455 case SIOCGIFSTAT_IN6:
456 case SIOCGIFSTAT_ICMP6:
457 sa6 = &ifr->ifr_addr;
463 if (sa6 && sa6->sin6_family == AF_INET6) {
466 if (sa6->sin6_scope_id != 0)
467 error = sa6_embedscope(sa6, 0);
469 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
472 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
473 &sa6->sin6_addr)) != 0)
475 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
480 case SIOCSIFADDR_IN6:
481 case SIOCSIFDSTADDR_IN6:
482 case SIOCSIFNETMASK_IN6:
484 * Since IPv6 allows a node to assign multiple addresses
485 * on a single interface, SIOCSIFxxx ioctls are deprecated.
487 /* we decided to obsolete this command (20000704) */
490 case SIOCDIFADDR_IN6:
492 * for IPv4, we look for existing in_ifaddr here to allow
493 * "ifconfig if0 delete" to remove the first IPv4 address on
494 * the interface. For IPv6, as the spec allows multiple
495 * interface address from the day one, we consider "remove the
496 * first one" semantics to be not preferable.
499 return (EADDRNOTAVAIL);
501 case SIOCAIFADDR_IN6:
503 * We always require users to specify a valid IPv6 address for
504 * the corresponding operation.
506 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
507 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
508 return (EAFNOSUPPORT);
511 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
512 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
519 case SIOCGIFADDR_IN6:
520 /* This interface is basically deprecated. use SIOCGIFCONF. */
522 case SIOCGIFAFLAG_IN6:
523 case SIOCGIFNETMASK_IN6:
524 case SIOCGIFDSTADDR_IN6:
525 case SIOCGIFALIFETIME_IN6:
526 /* must think again about its semantics */
528 return (EADDRNOTAVAIL);
530 case SIOCSIFALIFETIME_IN6:
532 struct in6_addrlifetime *lt;
535 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
540 return (EADDRNOTAVAIL);
541 /* sanity for overflow - beware unsigned */
542 lt = &ifr->ifr_ifru.ifru_lifetime;
543 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
544 lt->ia6t_vltime + time_second < time_second) {
547 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
548 lt->ia6t_pltime + time_second < time_second) {
557 case SIOCGIFADDR_IN6:
558 ifr->ifr_addr = ia->ia_addr;
559 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
563 case SIOCGIFDSTADDR_IN6:
564 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
567 * XXX: should we check if ifa_dstaddr is NULL and return
570 ifr->ifr_dstaddr = ia->ia_dstaddr;
571 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
575 case SIOCGIFNETMASK_IN6:
576 ifr->ifr_addr = ia->ia_prefixmask;
579 case SIOCGIFAFLAG_IN6:
580 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
583 case SIOCGIFSTAT_IN6:
586 bzero(&ifr->ifr_ifru.ifru_stat,
587 sizeof(ifr->ifr_ifru.ifru_stat));
588 ifr->ifr_ifru.ifru_stat =
589 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
592 case SIOCGIFSTAT_ICMP6:
595 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
596 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
597 ifr->ifr_ifru.ifru_icmp6stat =
598 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
601 case SIOCGIFALIFETIME_IN6:
602 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
603 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
605 struct in6_addrlifetime *retlt =
606 &ifr->ifr_ifru.ifru_lifetime;
609 * XXX: adjust expiration time assuming time_t is
613 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
614 if (ia->ia6_lifetime.ia6t_vltime <
615 maxexpire - ia->ia6_updatetime) {
616 retlt->ia6t_expire = ia->ia6_updatetime +
617 ia->ia6_lifetime.ia6t_vltime;
619 retlt->ia6t_expire = maxexpire;
621 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
623 struct in6_addrlifetime *retlt =
624 &ifr->ifr_ifru.ifru_lifetime;
627 * XXX: adjust expiration time assuming time_t is
631 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
632 if (ia->ia6_lifetime.ia6t_pltime <
633 maxexpire - ia->ia6_updatetime) {
634 retlt->ia6t_preferred = ia->ia6_updatetime +
635 ia->ia6_lifetime.ia6t_pltime;
637 retlt->ia6t_preferred = maxexpire;
641 case SIOCSIFALIFETIME_IN6:
642 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
644 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
645 ia->ia6_lifetime.ia6t_expire =
646 time_second + ia->ia6_lifetime.ia6t_vltime;
648 ia->ia6_lifetime.ia6t_expire = 0;
649 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
650 ia->ia6_lifetime.ia6t_preferred =
651 time_second + ia->ia6_lifetime.ia6t_pltime;
653 ia->ia6_lifetime.ia6t_preferred = 0;
656 case SIOCAIFADDR_IN6:
659 struct nd_prefixctl pr0;
660 struct nd_prefix *pr;
663 * first, make or update the interface address structure,
664 * and link it to the list.
666 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
668 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
671 * this can happen when the user specify the 0 valid
678 * then, make the prefix on-link on the interface.
679 * XXX: we'd rather create the prefix before the address, but
680 * we need at least one address to install the corresponding
681 * interface route, so we configure the address first.
685 * convert mask to prefix length (prefixmask has already
686 * been validated in in6_update_ifa().
688 bzero(&pr0, sizeof(pr0));
690 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
692 if (pr0.ndpr_plen == 128) {
693 break; /* we don't need to install a host route. */
695 pr0.ndpr_prefix = ifra->ifra_addr;
696 /* apply the mask for safety. */
697 for (i = 0; i < 4; i++) {
698 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
699 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
702 * XXX: since we don't have an API to set prefix (not address)
703 * lifetimes, we just use the same lifetimes as addresses.
704 * The (temporarily) installed lifetimes can be overridden by
705 * later advertised RAs (when accept_rtadv is non 0), which is
706 * an intended behavior.
708 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
710 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
711 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
712 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
714 /* add the prefix if not yet. */
715 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
717 * nd6_prelist_add will install the corresponding
720 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
723 log(LOG_ERR, "nd6_prelist_add succeeded but "
725 return (EINVAL); /* XXX panic here? */
729 /* relate the address to the prefix */
730 if (ia->ia6_ndpr == NULL) {
735 * If this is the first autoconf address from the
736 * prefix, create a temporary address as well
739 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
740 ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
742 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
743 log(LOG_NOTICE, "in6_control: failed "
744 "to create a temporary address, "
751 * this might affect the status of autoconfigured addresses,
752 * that is, this address might make other addresses detached.
754 pfxlist_onlink_check();
755 if (error == 0 && ia)
756 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
760 case SIOCDIFADDR_IN6:
762 struct nd_prefix *pr;
765 * If the address being deleted is the only one that owns
766 * the corresponding prefix, expire the prefix as well.
767 * XXX: theoretically, we don't have to worry about such
768 * relationship, since we separate the address management
769 * and the prefix management. We do this, however, to provide
770 * as much backward compatibility as possible in terms of
771 * the ioctl operation.
772 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
775 in6_purgeaddr(&ia->ia_ifa);
776 if (pr && pr->ndpr_refcnt == 0)
778 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
783 if (ifp == NULL || ifp->if_ioctl == 0)
785 return ((*ifp->if_ioctl)(ifp, cmd, data));
792 * Update parameters of an IPv6 interface address.
793 * If necessary, a new entry is created and linked into address chains.
794 * This function is separated from in6_control().
795 * XXX: should this be performed under splnet()?
798 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
799 struct in6_ifaddr *ia, int flags)
801 int error = 0, hostIsNew = 0, plen = -1;
802 struct in6_ifaddr *oia;
803 struct sockaddr_in6 dst6;
804 struct in6_addrlifetime *lt;
805 struct in6_multi_mship *imm;
806 struct in6_multi *in6m_sol;
809 char ip6buf[INET6_ADDRSTRLEN];
811 /* Validate parameters */
812 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
816 * The destination address for a p2p link must have a family
817 * of AF_UNSPEC or AF_INET6.
819 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
820 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
821 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
822 return (EAFNOSUPPORT);
824 * validate ifra_prefixmask. don't check sin6_family, netmask
825 * does not carry fields other than sin6_len.
827 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
830 * Because the IPv6 address architecture is classless, we require
831 * users to specify a (non 0) prefix length (mask) for a new address.
832 * We also require the prefix (when specified) mask is valid, and thus
833 * reject a non-consecutive mask.
835 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
837 if (ifra->ifra_prefixmask.sin6_len != 0) {
838 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
839 (u_char *)&ifra->ifra_prefixmask +
840 ifra->ifra_prefixmask.sin6_len);
845 * In this case, ia must not be NULL. We just use its prefix
848 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
851 * If the destination address on a p2p interface is specified,
852 * and the address is a scoped one, validate/set the scope
855 dst6 = ifra->ifra_dstaddr;
856 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
857 (dst6.sin6_family == AF_INET6)) {
858 struct in6_addr in6_tmp;
861 in6_tmp = dst6.sin6_addr;
862 if (in6_setscope(&in6_tmp, ifp, &zoneid))
863 return (EINVAL); /* XXX: should be impossible */
865 if (dst6.sin6_scope_id != 0) {
866 if (dst6.sin6_scope_id != zoneid)
868 } else /* user omit to specify the ID. */
869 dst6.sin6_scope_id = zoneid;
871 /* convert into the internal form */
872 if (sa6_embedscope(&dst6, 0))
873 return (EINVAL); /* XXX: should be impossible */
876 * The destination address can be specified only for a p2p or a
877 * loopback interface. If specified, the corresponding prefix length
880 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
881 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
882 /* XXX: noisy message */
883 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
884 "be specified for a p2p or a loopback IF only\n"));
888 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
889 "be 128 when dstaddr is specified\n"));
893 /* lifetime consistency check */
894 lt = &ifra->ifra_lifetime;
895 if (lt->ia6t_pltime > lt->ia6t_vltime)
897 if (lt->ia6t_vltime == 0) {
899 * the following log might be noisy, but this is a typical
900 * configuration mistake or a tool's bug.
903 "in6_update_ifa: valid lifetime is 0 for %s\n",
904 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
907 return (0); /* there's nothing to do */
911 * If this is a new address, allocate a new ifaddr and link it
917 * When in6_update_ifa() is called in a process of a received
918 * RA, it is called under an interrupt context. So, we should
919 * call malloc with M_NOWAIT.
921 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
925 bzero((caddr_t)ia, sizeof(*ia));
926 LIST_INIT(&ia->ia6_memberships);
927 /* Initialize the address and masks, and put time stamp */
928 IFA_LOCK_INIT(&ia->ia_ifa);
929 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
930 ia->ia_addr.sin6_family = AF_INET6;
931 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
932 ia->ia6_createtime = time_second;
933 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
935 * XXX: some functions expect that ifa_dstaddr is not
936 * NULL for p2p interfaces.
938 ia->ia_ifa.ifa_dstaddr =
939 (struct sockaddr *)&ia->ia_dstaddr;
941 ia->ia_ifa.ifa_dstaddr = NULL;
943 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
946 if ((oia = in6_ifaddr) != NULL) {
947 for ( ; oia->ia_next; oia = oia->ia_next)
953 ia->ia_ifa.ifa_refcnt = 1;
954 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
957 /* update timestamp */
958 ia->ia6_updatetime = time_second;
960 /* set prefix mask */
961 if (ifra->ifra_prefixmask.sin6_len) {
963 * We prohibit changing the prefix length of an existing
965 * + such an operation should be rare in IPv6, and
966 * + the operation would confuse prefix management.
968 if (ia->ia_prefixmask.sin6_len &&
969 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
970 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
971 " existing (%s) address should not be changed\n",
972 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
976 ia->ia_prefixmask = ifra->ifra_prefixmask;
980 * If a new destination address is specified, scrub the old one and
981 * install the new destination. Note that the interface must be
982 * p2p or loopback (see the check above.)
984 if (dst6.sin6_family == AF_INET6 &&
985 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
988 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
989 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
990 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
991 "a route to the old destination: %s\n",
992 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
993 /* proceed anyway... */
995 ia->ia_flags &= ~IFA_ROUTE;
996 ia->ia_dstaddr = dst6;
1000 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1001 * to see if the address is deprecated or invalidated, but initialize
1002 * these members for applications.
1004 ia->ia6_lifetime = ifra->ifra_lifetime;
1005 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1006 ia->ia6_lifetime.ia6t_expire =
1007 time_second + ia->ia6_lifetime.ia6t_vltime;
1009 ia->ia6_lifetime.ia6t_expire = 0;
1010 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1011 ia->ia6_lifetime.ia6t_preferred =
1012 time_second + ia->ia6_lifetime.ia6t_pltime;
1014 ia->ia6_lifetime.ia6t_preferred = 0;
1016 /* reset the interface and routing table appropriately. */
1017 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1021 * configure address flags.
1023 ia->ia6_flags = ifra->ifra_flags;
1025 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1026 * userland, make it deprecated.
1028 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1029 ia->ia6_lifetime.ia6t_pltime = 0;
1030 ia->ia6_lifetime.ia6t_preferred = time_second;
1033 * Make the address tentative before joining multicast addresses,
1034 * so that corresponding MLD responses would not have a tentative
1037 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1038 if (hostIsNew && in6if_do_dad(ifp))
1039 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1042 * We are done if we have simply modified an existing address.
1048 * Beyond this point, we should call in6_purgeaddr upon an error,
1049 * not just go to unlink.
1052 /* Join necessary multicast groups */
1054 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1055 struct sockaddr_in6 mltaddr, mltmask;
1056 struct in6_addr llsol;
1058 /* join solicited multicast addr for new host id */
1059 bzero(&llsol, sizeof(struct in6_addr));
1060 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1061 llsol.s6_addr32[1] = 0;
1062 llsol.s6_addr32[2] = htonl(1);
1063 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1064 llsol.s6_addr8[12] = 0xff;
1065 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1066 /* XXX: should not happen */
1067 log(LOG_ERR, "in6_update_ifa: "
1068 "in6_setscope failed\n");
1072 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1074 * We need a random delay for DAD on the address
1075 * being configured. It also means delaying
1076 * transmission of the corresponding MLD report to
1077 * avoid report collision.
1078 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1080 delay = arc4random() %
1081 (MAX_RTR_SOLICITATION_DELAY * hz);
1083 imm = in6_joingroup(ifp, &llsol, &error, delay);
1085 nd6log((LOG_WARNING,
1086 "in6_update_ifa: addmulti failed for "
1087 "%s on %s (errno=%d)\n",
1088 ip6_sprintf(ip6buf, &llsol), if_name(ifp),
1090 in6_purgeaddr((struct ifaddr *)ia);
1093 LIST_INSERT_HEAD(&ia->ia6_memberships,
1095 in6m_sol = imm->i6mm_maddr;
1097 bzero(&mltmask, sizeof(mltmask));
1098 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1099 mltmask.sin6_family = AF_INET6;
1100 mltmask.sin6_addr = in6mask32;
1101 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1104 * join link-local all-nodes address
1106 bzero(&mltaddr, sizeof(mltaddr));
1107 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1108 mltaddr.sin6_family = AF_INET6;
1109 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1110 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1112 goto cleanup; /* XXX: should not fail */
1115 * XXX: do we really need this automatic routes?
1116 * We should probably reconsider this stuff. Most applications
1117 * actually do not need the routes, since they usually specify
1118 * the outgoing interface.
1120 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1122 if (memcmp(&mltaddr.sin6_addr,
1123 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1130 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1131 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1132 (struct sockaddr *)&ia->ia_addr,
1133 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1134 (struct rtentry **)0);
1141 * XXX: do we really need this automatic routes?
1142 * We should probably reconsider this stuff. Most applications
1143 * actually do not need the routes, since they usually specify
1144 * the outgoing interface.
1146 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1148 /* XXX: only works in !SCOPEDROUTING case. */
1149 if (memcmp(&mltaddr.sin6_addr,
1150 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1157 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1158 (struct sockaddr *)&ia->ia_addr,
1159 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1160 (struct rtentry **)0);
1167 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1169 nd6log((LOG_WARNING,
1170 "in6_update_ifa: addmulti failed for "
1171 "%s on %s (errno=%d)\n",
1172 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1173 if_name(ifp), error));
1176 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1179 * join node information group address
1181 #define hostnamelen strlen(hostname)
1183 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1185 * The spec doesn't say anything about delay for this
1186 * group, but the same logic should apply.
1188 delay = arc4random() %
1189 (MAX_RTR_SOLICITATION_DELAY * hz);
1191 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1193 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1194 delay); /* XXX jinmei */
1196 nd6log((LOG_WARNING, "in6_update_ifa: "
1197 "addmulti failed for %s on %s "
1199 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1200 if_name(ifp), error));
1201 /* XXX not very fatal, go on... */
1203 LIST_INSERT_HEAD(&ia->ia6_memberships,
1210 * join interface-local all-nodes address.
1211 * (ff01::1%ifN, and ff01::%ifN/32)
1213 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1214 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1216 goto cleanup; /* XXX: should not fail */
1217 /* XXX: again, do we really need the route? */
1218 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1220 if (memcmp(&mltaddr.sin6_addr,
1221 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1228 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1229 (struct sockaddr *)&ia->ia_addr,
1230 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1231 (struct rtentry **)0);
1237 /* XXX: again, do we really need the route? */
1238 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1240 if (memcmp(&mltaddr.sin6_addr,
1241 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1248 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1249 (struct sockaddr *)&ia->ia_addr,
1250 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1251 (struct rtentry **)0);
1258 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1260 nd6log((LOG_WARNING, "in6_update_ifa: "
1261 "addmulti failed for %s on %s "
1263 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1264 if_name(ifp), error));
1267 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1272 * Perform DAD, if needed.
1273 * XXX It may be of use, if we can administratively
1276 if (hostIsNew && in6if_do_dad(ifp) &&
1277 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1278 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1280 int mindelay, maxdelay;
1283 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1285 * We need to impose a delay before sending an NS
1286 * for DAD. Check if we also needed a delay for the
1287 * corresponding MLD message. If we did, the delay
1288 * should be larger than the MLD delay (this could be
1289 * relaxed a bit, but this simple logic is at least
1293 if (in6m_sol != NULL &&
1294 in6m_sol->in6m_state == MLD_REPORTPENDING) {
1295 mindelay = in6m_sol->in6m_timer;
1297 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1298 if (maxdelay - mindelay == 0)
1302 (arc4random() % (maxdelay - mindelay)) +
1306 nd6_dad_start((struct ifaddr *)ia, delay);
1313 * XXX: if a change of an existing address failed, keep the entry
1317 in6_unlink_ifa(ia, ifp);
1321 in6_purgeaddr(&ia->ia_ifa);
1326 in6_purgeaddr(struct ifaddr *ifa)
1328 struct ifnet *ifp = ifa->ifa_ifp;
1329 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1330 char ip6buf[INET6_ADDRSTRLEN];
1331 struct in6_multi_mship *imm;
1333 /* stop DAD processing */
1337 * delete route to the destination of the address being purged.
1338 * The interface must be p2p or loopback in this case.
1340 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1343 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1345 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1346 "a route to the p2p destination: %s on %s, "
1348 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1350 /* proceed anyway... */
1352 ia->ia_flags &= ~IFA_ROUTE;
1355 /* Remove ownaddr's loopback rtentry, if it exists. */
1356 in6_ifremloop(&(ia->ia_ifa));
1359 * leave from multicast groups we have joined for the interface
1361 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1362 LIST_REMOVE(imm, i6mm_chain);
1363 in6_leavegroup(imm);
1366 in6_unlink_ifa(ia, ifp);
1370 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1372 struct in6_ifaddr *oia;
1375 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1378 if (oia == (ia = in6_ifaddr))
1379 in6_ifaddr = ia->ia_next;
1381 while (ia->ia_next && (ia->ia_next != oia))
1384 ia->ia_next = oia->ia_next;
1387 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1392 * Release the reference to the base prefix. There should be a
1393 * positive reference.
1395 if (oia->ia6_ndpr == NULL) {
1397 "in6_unlink_ifa: autoconf'ed address "
1398 "%p has no prefix\n", oia));
1400 oia->ia6_ndpr->ndpr_refcnt--;
1401 oia->ia6_ndpr = NULL;
1405 * Also, if the address being removed is autoconf'ed, call
1406 * pfxlist_onlink_check() since the release might affect the status of
1407 * other (detached) addresses.
1409 if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) {
1410 pfxlist_onlink_check();
1414 * release another refcnt for the link from in6_ifaddr.
1415 * Note that we should decrement the refcnt at least once for all *BSD.
1417 IFAFREE(&oia->ia_ifa);
1423 in6_purgeif(struct ifnet *ifp)
1425 struct ifaddr *ifa, *nifa;
1427 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1428 nifa = TAILQ_NEXT(ifa, ifa_list);
1429 if (ifa->ifa_addr->sa_family != AF_INET6)
1439 * SIOCGLIFADDR: get first address. (?)
1440 * SIOCGLIFADDR with IFLR_PREFIX:
1441 * get first address that matches the specified prefix.
1442 * SIOCALIFADDR: add the specified address.
1443 * SIOCALIFADDR with IFLR_PREFIX:
1444 * add the specified prefix, filling hostid part from
1445 * the first link-local address. prefixlen must be <= 64.
1446 * SIOCDLIFADDR: delete the specified address.
1447 * SIOCDLIFADDR with IFLR_PREFIX:
1448 * delete the first address that matches the specified prefix.
1450 * EINVAL on invalid parameters
1451 * EADDRNOTAVAIL on prefix match failed/specified address not found
1452 * other values may be returned from in6_ioctl()
1454 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1455 * this is to accomodate address naming scheme other than RFC2374,
1457 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1458 * address encoding scheme. (see figure on page 8)
1461 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1462 struct ifnet *ifp, struct thread *td)
1464 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1466 struct sockaddr *sa;
1469 if (!data || !ifp) {
1470 panic("invalid argument to in6_lifaddr_ioctl");
1476 /* address must be specified on GET with IFLR_PREFIX */
1477 if ((iflr->flags & IFLR_PREFIX) == 0)
1482 /* address must be specified on ADD and DELETE */
1483 sa = (struct sockaddr *)&iflr->addr;
1484 if (sa->sa_family != AF_INET6)
1486 if (sa->sa_len != sizeof(struct sockaddr_in6))
1488 /* XXX need improvement */
1489 sa = (struct sockaddr *)&iflr->dstaddr;
1490 if (sa->sa_family && sa->sa_family != AF_INET6)
1492 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1495 default: /* shouldn't happen */
1497 panic("invalid cmd to in6_lifaddr_ioctl");
1503 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1509 struct in6_aliasreq ifra;
1510 struct in6_addr *hostid = NULL;
1513 if ((iflr->flags & IFLR_PREFIX) != 0) {
1514 struct sockaddr_in6 *sin6;
1517 * hostid is to fill in the hostid part of the
1518 * address. hostid points to the first link-local
1519 * address attached to the interface.
1521 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1523 return EADDRNOTAVAIL;
1524 hostid = IFA_IN6(ifa);
1526 /* prefixlen must be <= 64. */
1527 if (64 < iflr->prefixlen)
1529 prefixlen = iflr->prefixlen;
1531 /* hostid part must be zero. */
1532 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1533 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1534 sin6->sin6_addr.s6_addr32[3] != 0) {
1538 prefixlen = iflr->prefixlen;
1540 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1541 bzero(&ifra, sizeof(ifra));
1542 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1544 bcopy(&iflr->addr, &ifra.ifra_addr,
1545 ((struct sockaddr *)&iflr->addr)->sa_len);
1547 /* fill in hostid part */
1548 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1549 hostid->s6_addr32[2];
1550 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1551 hostid->s6_addr32[3];
1554 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1555 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1556 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1558 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1559 hostid->s6_addr32[2];
1560 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1561 hostid->s6_addr32[3];
1565 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1566 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1568 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1569 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1574 struct in6_ifaddr *ia;
1575 struct in6_addr mask, candidate, match;
1576 struct sockaddr_in6 *sin6;
1579 bzero(&mask, sizeof(mask));
1580 if (iflr->flags & IFLR_PREFIX) {
1581 /* lookup a prefix rather than address. */
1582 in6_prefixlen2mask(&mask, iflr->prefixlen);
1584 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1585 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1586 match.s6_addr32[0] &= mask.s6_addr32[0];
1587 match.s6_addr32[1] &= mask.s6_addr32[1];
1588 match.s6_addr32[2] &= mask.s6_addr32[2];
1589 match.s6_addr32[3] &= mask.s6_addr32[3];
1591 /* if you set extra bits, that's wrong */
1592 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1597 if (cmd == SIOCGLIFADDR) {
1598 /* on getting an address, take the 1st match */
1601 /* on deleting an address, do exact match */
1602 in6_prefixlen2mask(&mask, 128);
1603 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1604 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1610 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1611 if (ifa->ifa_addr->sa_family != AF_INET6)
1617 * XXX: this is adhoc, but is necessary to allow
1618 * a user to specify fe80::/64 (not /10) for a
1619 * link-local address.
1621 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1622 in6_clearscope(&candidate);
1623 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1624 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1625 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1626 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1627 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1631 return EADDRNOTAVAIL;
1634 if (cmd == SIOCGLIFADDR) {
1637 /* fill in the if_laddrreq structure */
1638 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1639 error = sa6_recoverscope(
1640 (struct sockaddr_in6 *)&iflr->addr);
1644 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1645 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1646 ia->ia_dstaddr.sin6_len);
1647 error = sa6_recoverscope(
1648 (struct sockaddr_in6 *)&iflr->dstaddr);
1652 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1655 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1657 iflr->flags = ia->ia6_flags; /* XXX */
1661 struct in6_aliasreq ifra;
1663 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1664 bzero(&ifra, sizeof(ifra));
1665 bcopy(iflr->iflr_name, ifra.ifra_name,
1666 sizeof(ifra.ifra_name));
1668 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1669 ia->ia_addr.sin6_len);
1670 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1671 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1672 ia->ia_dstaddr.sin6_len);
1674 bzero(&ifra.ifra_dstaddr,
1675 sizeof(ifra.ifra_dstaddr));
1677 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1678 ia->ia_prefixmask.sin6_len);
1680 ifra.ifra_flags = ia->ia6_flags;
1681 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1687 return EOPNOTSUPP; /* just for safety */
1691 * Initialize an interface's intetnet6 address
1692 * and routing table entry.
1695 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1696 struct sockaddr_in6 *sin6, int newhost)
1698 int error = 0, plen, ifacount = 0;
1703 * Give the interface a chance to initialize
1704 * if this is its first address,
1705 * and to validate the address if necessary.
1707 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1708 if (ifa->ifa_addr->sa_family != AF_INET6)
1713 ia->ia_addr = *sin6;
1715 if (ifacount <= 1 && ifp->if_ioctl) {
1717 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1718 IFF_UNLOCKGIANT(ifp);
1726 ia->ia_ifa.ifa_metric = ifp->if_metric;
1728 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1732 * set the rtrequest function to create llinfo. It also
1733 * adjust outgoing interface of the route for the local
1734 * address when called via in6_ifaddloop() below.
1736 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1741 * If a new destination address is specified for a point-to-point
1742 * interface, install a route to the destination as an interface
1743 * direct route. In addition, if the link is expected to have neighbor
1744 * cache entries, specify RTF_LLINFO so that a cache entry for the
1745 * destination address will be created.
1747 * XXX: the logic below rejects assigning multiple addresses on a p2p
1748 * interface that share the same destination.
1750 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1751 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1752 ia->ia_dstaddr.sin6_family == AF_INET6) {
1753 int rtflags = RTF_UP | RTF_HOST;
1754 struct rtentry *rt = NULL, **rtp = NULL;
1756 if (nd6_need_cache(ifp) != 0) {
1757 rtflags |= RTF_LLINFO;
1761 error = rtrequest(RTM_ADD,
1762 (struct sockaddr *)&ia->ia_dstaddr,
1763 (struct sockaddr *)&ia->ia_addr,
1764 (struct sockaddr *)&ia->ia_prefixmask,
1765 ia->ia_flags | rtflags, rtp);
1769 struct llinfo_nd6 *ln;
1772 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1775 * Set the state to STALE because we don't
1776 * have to perform address resolution on this
1779 ln->ln_state = ND6_LLINFO_STALE;
1784 ia->ia_flags |= IFA_ROUTE;
1788 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1790 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1793 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1795 in6_ifaddloop(&(ia->ia_ifa));
1800 struct in6_multi_mship *
1801 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
1802 int *errorp, int delay)
1804 struct in6_multi_mship *imm;
1806 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1811 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay);
1812 if (!imm->i6mm_maddr) {
1813 /* *errorp is alrady set */
1814 free(imm, M_IP6MADDR);
1821 in6_leavegroup(struct in6_multi_mship *imm)
1824 if (imm->i6mm_maddr)
1825 in6_delmulti(imm->i6mm_maddr);
1826 free(imm, M_IP6MADDR);
1831 * Find an IPv6 interface link-local address specific to an interface.
1834 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1838 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1839 if (ifa->ifa_addr->sa_family != AF_INET6)
1841 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1842 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1849 return ((struct in6_ifaddr *)ifa);
1854 * find the internet address corresponding to a given interface and address.
1857 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1861 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1862 if (ifa->ifa_addr->sa_family != AF_INET6)
1864 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1868 return ((struct in6_ifaddr *)ifa);
1872 * Convert IP6 address to printable (loggable) representation. Caller
1873 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1875 static char digits[] = "0123456789abcdef";
1877 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1881 const u_int16_t *a = (const u_int16_t *)addr;
1883 int dcolon = 0, zero = 0;
1887 for (i = 0; i < 8; i++) {
1898 if (dcolon == 0 && *(a + 1) == 0) {
1910 d = (const u_char *)a;
1911 /* Try to eliminate leading zeros in printout like in :0001. */
1913 *cp = digits[*d >> 4];
1918 *cp = digits[*d++ & 0xf];
1919 if (zero == 0 || (*cp != '0')) {
1923 *cp = digits[*d >> 4];
1924 if (zero == 0 || (*cp != '0')) {
1928 *cp++ = digits[*d & 0xf];
1937 in6_localaddr(struct in6_addr *in6)
1939 struct in6_ifaddr *ia;
1941 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1944 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1945 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1946 &ia->ia_prefixmask.sin6_addr)) {
1955 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1957 struct in6_ifaddr *ia;
1959 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1960 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1962 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1963 return (1); /* true */
1965 /* XXX: do we still have to go thru the rest of the list? */
1968 return (0); /* false */
1972 * return length of part which dst and src are equal
1976 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1979 u_char *s = (u_char *)src, *d = (u_char *)dst;
1980 u_char *lim = s + 16, r;
1983 if ((r = (*d++ ^ *s++)) != 0) {
1994 /* XXX: to be scope conscious */
1996 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1998 int bytelen, bitlen;
2001 if (0 > len || len > 128) {
2002 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2010 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2013 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2014 p2->s6_addr[bytelen] >> (8 - bitlen))
2021 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2023 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2024 int bytelen, bitlen, i;
2027 if (0 > len || len > 128) {
2028 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2033 bzero(maskp, sizeof(*maskp));
2036 for (i = 0; i < bytelen; i++)
2037 maskp->s6_addr[i] = 0xff;
2039 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2043 * return the best address out of the same scope. if no address was
2044 * found, return the first valid address from designated IF.
2047 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2049 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2051 struct in6_ifaddr *besta = 0;
2052 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2054 dep[0] = dep[1] = NULL;
2057 * We first look for addresses in the same scope.
2058 * If there is one, return it.
2059 * If two or more, return one which matches the dst longest.
2060 * If none, return one of global addresses assigned other ifs.
2062 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2063 if (ifa->ifa_addr->sa_family != AF_INET6)
2065 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2066 continue; /* XXX: is there any case to allow anycast? */
2067 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2068 continue; /* don't use this interface */
2069 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2071 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2072 if (ip6_use_deprecated)
2073 dep[0] = (struct in6_ifaddr *)ifa;
2077 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2079 * call in6_matchlen() as few as possible
2083 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2084 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2087 besta = (struct in6_ifaddr *)ifa;
2090 besta = (struct in6_ifaddr *)ifa;
2096 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2097 if (ifa->ifa_addr->sa_family != AF_INET6)
2099 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2100 continue; /* XXX: is there any case to allow anycast? */
2101 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2102 continue; /* don't use this interface */
2103 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2105 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2106 if (ip6_use_deprecated)
2107 dep[1] = (struct in6_ifaddr *)ifa;
2111 return (struct in6_ifaddr *)ifa;
2114 /* use the last-resort values, that are, deprecated addresses */
2124 * perform DAD when interface becomes IFF_UP.
2127 in6_if_up(struct ifnet *ifp)
2130 struct in6_ifaddr *ia;
2132 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2133 if (ifa->ifa_addr->sa_family != AF_INET6)
2135 ia = (struct in6_ifaddr *)ifa;
2136 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2138 * The TENTATIVE flag was likely set by hand
2139 * beforehand, implicitly indicating the need for DAD.
2140 * We may be able to skip the random delay in this
2141 * case, but we impose delays just in case.
2144 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2149 * special cases, like 6to4, are handled in in6_ifattach
2151 in6_ifattach(ifp, NULL);
2155 in6if_do_dad(struct ifnet *ifp)
2157 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2160 switch (ifp->if_type) {
2166 * These interfaces do not have the IFF_LOOPBACK flag,
2167 * but loop packets back. We do not have to do DAD on such
2168 * interfaces. We should even omit it, because loop-backed
2169 * NS would confuse the DAD procedure.
2174 * Our DAD routine requires the interface up and running.
2175 * However, some interfaces can be up before the RUNNING
2176 * status. Additionaly, users may try to assign addresses
2177 * before the interface becomes up (or running).
2178 * We simply skip DAD in such a case as a work around.
2179 * XXX: we should rather mark "tentative" on such addresses,
2180 * and do DAD after the interface becomes ready.
2182 if (!((ifp->if_flags & IFF_UP) &&
2183 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2191 * Calculate max IPv6 MTU through all the interfaces and store it
2197 unsigned long maxmtu = 0;
2201 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2202 /* this function can be called during ifnet initialization */
2203 if (!ifp->if_afdata[AF_INET6])
2205 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2206 IN6_LINKMTU(ifp) > maxmtu)
2207 maxmtu = IN6_LINKMTU(ifp);
2210 if (maxmtu) /* update only when maxmtu is positive */
2211 in6_maxmtu = maxmtu;
2215 * Provide the length of interface identifiers to be used for the link attached
2216 * to the given interface. The length should be defined in "IPv6 over
2217 * xxx-link" document. Note that address architecture might also define
2218 * the length for a particular set of address prefixes, regardless of the
2219 * link type. As clarified in rfc2462bis, those two definitions should be
2220 * consistent, and those really are as of August 2004.
2223 in6_if2idlen(struct ifnet *ifp)
2225 switch (ifp->if_type) {
2226 case IFT_ETHER: /* RFC2464 */
2227 #ifdef IFT_PROPVIRTUAL
2228 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2231 case IFT_L2VLAN: /* ditto */
2233 #ifdef IFT_IEEE80211
2234 case IFT_IEEE80211: /* ditto */
2237 case IFT_MIP: /* ditto */
2240 case IFT_FDDI: /* RFC2467 */
2242 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2244 case IFT_PPP: /* RFC2472 */
2246 case IFT_ARCNET: /* RFC2497 */
2248 case IFT_FRELAY: /* RFC2590 */
2250 case IFT_IEEE1394: /* RFC3146 */
2253 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2255 return (64); /* XXX: is this really correct? */
2258 * Unknown link type:
2259 * It might be controversial to use the today's common constant
2260 * of 64 for these cases unconditionally. For full compliance,
2261 * we should return an error in this case. On the other hand,
2262 * if we simply miss the standard for the link type or a new
2263 * standard is defined for a new link type, the IFID length
2264 * is very likely to be the common constant. As a compromise,
2265 * we always use the constant, but make an explicit notice
2266 * indicating the "unknown" case.
2268 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2274 in6_domifattach(struct ifnet *ifp)
2276 struct in6_ifextra *ext;
2278 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2279 bzero(ext, sizeof(*ext));
2281 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2282 M_IFADDR, M_WAITOK);
2283 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2286 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2287 M_IFADDR, M_WAITOK);
2288 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2290 ext->nd_ifinfo = nd6_ifattach(ifp);
2291 ext->scope6_id = scope6_ifattach(ifp);
2296 in6_domifdetach(struct ifnet *ifp, void *aux)
2298 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2300 scope6_ifdetach(ext->scope6_id);
2301 nd6_ifdetach(ext->nd_ifinfo);
2302 free(ext->in6_ifstat, M_IFADDR);
2303 free(ext->icmp6_ifstat, M_IFADDR);
2304 free(ext, M_IFADDR);
2308 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2309 * v4 mapped addr or v4 compat addr
2312 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2315 bzero(sin, sizeof(*sin));
2316 sin->sin_len = sizeof(struct sockaddr_in);
2317 sin->sin_family = AF_INET;
2318 sin->sin_port = sin6->sin6_port;
2319 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2322 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2324 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2326 bzero(sin6, sizeof(*sin6));
2327 sin6->sin6_len = sizeof(struct sockaddr_in6);
2328 sin6->sin6_family = AF_INET6;
2329 sin6->sin6_port = sin->sin_port;
2330 sin6->sin6_addr.s6_addr32[0] = 0;
2331 sin6->sin6_addr.s6_addr32[1] = 0;
2332 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2333 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2336 /* Convert sockaddr_in6 into sockaddr_in. */
2338 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2340 struct sockaddr_in *sin_p;
2341 struct sockaddr_in6 sin6;
2344 * Save original sockaddr_in6 addr and convert it
2347 sin6 = *(struct sockaddr_in6 *)nam;
2348 sin_p = (struct sockaddr_in *)nam;
2349 in6_sin6_2_sin(sin_p, &sin6);
2352 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2354 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2356 struct sockaddr_in *sin_p;
2357 struct sockaddr_in6 *sin6_p;
2359 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2361 sin_p = (struct sockaddr_in *)*nam;
2362 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2363 FREE(*nam, M_SONAME);
2364 *nam = (struct sockaddr *)sin6_p;