in6.c revision 188144
1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 3353541Sshin * Copyright (c) 1982, 1986, 1991, 1993 3453541Sshin * The Regents of the University of California. All rights reserved. 3553541Sshin * 3653541Sshin * Redistribution and use in source and binary forms, with or without 3753541Sshin * modification, are permitted provided that the following conditions 3853541Sshin * are met: 3953541Sshin * 1. Redistributions of source code must retain the above copyright 4053541Sshin * notice, this list of conditions and the following disclaimer. 4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright 4253541Sshin * notice, this list of conditions and the following disclaimer in the 4353541Sshin * documentation and/or other materials provided with the distribution. 4453541Sshin * 4. Neither the name of the University nor the names of its contributors 4553541Sshin * may be used to endorse or promote products derived from this software 4653541Sshin * without specific prior written permission. 4753541Sshin * 4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5153541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin * @(#)in.c 8.2 (Berkeley) 11/15/93 6153541Sshin */ 6253541Sshin 63174510Sobrien#include <sys/cdefs.h> 64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6.c 188144 2009-02-05 14:06:09Z jamie $"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 6862587Sitojun 6953541Sshin#include <sys/param.h> 7053541Sshin#include <sys/errno.h> 71186948Sbz#include <sys/jail.h> 7253541Sshin#include <sys/malloc.h> 7353541Sshin#include <sys/socket.h> 7453541Sshin#include <sys/socketvar.h> 7553541Sshin#include <sys/sockio.h> 7653541Sshin#include <sys/systm.h> 77164033Srwatson#include <sys/priv.h> 7853541Sshin#include <sys/proc.h> 7953541Sshin#include <sys/time.h> 8053541Sshin#include <sys/kernel.h> 8153541Sshin#include <sys/syslog.h> 82181803Sbz#include <sys/vimage.h> 8353541Sshin 8453541Sshin#include <net/if.h> 8553541Sshin#include <net/if_types.h> 8653541Sshin#include <net/route.h> 8753541Sshin#include <net/if_dl.h> 88185571Sbz#include <net/vnet.h> 8953541Sshin 9053541Sshin#include <netinet/in.h> 9153541Sshin#include <netinet/in_var.h> 92186119Sqingli#include <net/if_llatbl.h> 9353541Sshin#include <netinet/if_ether.h> 9478064Sume#include <netinet/in_systm.h> 9578064Sume#include <netinet/ip.h> 9678064Sume#include <netinet/in_pcb.h> 9753541Sshin 9862587Sitojun#include <netinet/ip6.h> 9953541Sshin#include <netinet6/ip6_var.h> 10095023Ssuz#include <netinet6/nd6.h> 10153541Sshin#include <netinet6/mld6_var.h> 10262587Sitojun#include <netinet6/ip6_mroute.h> 10353541Sshin#include <netinet6/in6_ifattach.h> 10462587Sitojun#include <netinet6/scope6_var.h> 10578064Sume#include <netinet6/in6_pcb.h> 106185571Sbz#include <netinet6/vinet6.h> 10762587Sitojun 108149849SobrienMALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address"); 10953541Sshin 11053541Sshin/* 11153541Sshin * Definitions of some costant IP6 addresses. 11253541Sshin */ 11362587Sitojunconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 11462587Sitojunconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 11562587Sitojunconst struct in6_addr in6addr_nodelocal_allnodes = 11653541Sshin IN6ADDR_NODELOCAL_ALLNODES_INIT; 11762587Sitojunconst struct in6_addr in6addr_linklocal_allnodes = 11853541Sshin IN6ADDR_LINKLOCAL_ALLNODES_INIT; 11962587Sitojunconst struct in6_addr in6addr_linklocal_allrouters = 12053541Sshin IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 12153541Sshin 12262587Sitojunconst struct in6_addr in6mask0 = IN6MASK0; 12362587Sitojunconst struct in6_addr in6mask32 = IN6MASK32; 12462587Sitojunconst struct in6_addr in6mask64 = IN6MASK64; 12562587Sitojunconst struct in6_addr in6mask96 = IN6MASK96; 12662587Sitojunconst struct in6_addr in6mask128 = IN6MASK128; 12753541Sshin 128126552Sumeconst struct sockaddr_in6 sa6_any = 129126552Sume { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 }; 13078064Sume 13162587Sitojunstatic int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 13283366Sjulian struct ifnet *, struct thread *)); 13378064Sumestatic int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *, 134120891Sume struct sockaddr_in6 *, int)); 135175162Sobrienstatic void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 13653541Sshin 13762587Sitojunstruct in6_multihead in6_multihead; /* XXX BSS initialization */ 13883934Sbrooksint (*faithprefix_p)(struct in6_addr *); 13983934Sbrooks 140120891Sume 14178064Sume 14253541Sshinint 143171259Sdelphijin6_mask2len(struct in6_addr *mask, u_char *lim0) 14453541Sshin{ 14578064Sume int x = 0, y; 14678064Sume u_char *lim = lim0, *p; 14753541Sshin 148120891Sume /* ignore the scope_id part */ 149120891Sume if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 15078064Sume lim = (u_char *)mask + sizeof(*mask); 15178064Sume for (p = (u_char *)mask; p < lim; x++, p++) { 15278064Sume if (*p != 0xff) 15353541Sshin break; 15453541Sshin } 15553541Sshin y = 0; 15678064Sume if (p < lim) { 15753541Sshin for (y = 0; y < 8; y++) { 15878064Sume if ((*p & (0x80 >> y)) == 0) 15953541Sshin break; 16053541Sshin } 16153541Sshin } 16278064Sume 16378064Sume /* 16478064Sume * when the limit pointer is given, do a stricter check on the 16578064Sume * remaining bits. 16678064Sume */ 16778064Sume if (p < lim) { 16878064Sume if (y != 0 && (*p & (0x00ff >> y)) != 0) 169120856Sume return (-1); 17078064Sume for (p = p + 1; p < lim; p++) 17178064Sume if (*p != 0) 172120856Sume return (-1); 17378064Sume } 174120891Sume 17553541Sshin return x * 8 + y; 17653541Sshin} 17753541Sshin 17853541Sshin#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 17962587Sitojun#define ia62ifa(ia6) (&((ia6)->ia_ifa)) 18053541Sshin 18153541Sshinint 182171259Sdelphijin6_control(struct socket *so, u_long cmd, caddr_t data, 183171259Sdelphij struct ifnet *ifp, struct thread *td) 18453541Sshin{ 185183550Szec INIT_VNET_INET6(curvnet); 18653541Sshin struct in6_ifreq *ifr = (struct in6_ifreq *)data; 18778064Sume struct in6_ifaddr *ia = NULL; 18853541Sshin struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 189151539Ssuz struct sockaddr_in6 *sa6; 190164033Srwatson int error; 19153541Sshin 19262587Sitojun switch (cmd) { 19362587Sitojun case SIOCGETSGCNT_IN6: 19462587Sitojun case SIOCGETMIFCNT_IN6: 195166938Sbms return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP); 19662587Sitojun } 19753541Sshin 198121742Sume switch(cmd) { 199121742Sume case SIOCAADDRCTL_POLICY: 200121742Sume case SIOCDADDRCTL_POLICY: 201164033Srwatson if (td != NULL) { 202164033Srwatson error = priv_check(td, PRIV_NETINET_ADDRCTRL6); 203164033Srwatson if (error) 204164033Srwatson return (error); 205164033Srwatson } 206121742Sume return (in6_src_ioctl(cmd, data)); 207121742Sume } 208121742Sume 20962587Sitojun if (ifp == NULL) 210120856Sume return (EOPNOTSUPP); 21153541Sshin 21253541Sshin switch (cmd) { 21353541Sshin case SIOCSNDFLUSH_IN6: 21453541Sshin case SIOCSPFXFLUSH_IN6: 21553541Sshin case SIOCSRTRFLUSH_IN6: 21662587Sitojun case SIOCSDEFIFACE_IN6: 21762587Sitojun case SIOCSIFINFO_FLAGS: 218164033Srwatson if (td != NULL) { 219164033Srwatson error = priv_check(td, PRIV_NETINET_ND6); 220164033Srwatson if (error) 221164033Srwatson return (error); 222164033Srwatson } 223120891Sume /* FALLTHROUGH */ 22478064Sume case OSIOCGIFINFO_IN6: 22553541Sshin case SIOCGIFINFO_IN6: 226151468Ssuz case SIOCSIFINFO_IN6: 22753541Sshin case SIOCGDRLST_IN6: 22853541Sshin case SIOCGPRLST_IN6: 22953541Sshin case SIOCGNBRINFO_IN6: 23062587Sitojun case SIOCGDEFIFACE_IN6: 231120856Sume return (nd6_ioctl(cmd, data, ifp)); 23253541Sshin } 23353541Sshin 23453541Sshin switch (cmd) { 23553541Sshin case SIOCSIFPREFIX_IN6: 23653541Sshin case SIOCDIFPREFIX_IN6: 23753541Sshin case SIOCAIFPREFIX_IN6: 23853541Sshin case SIOCCIFPREFIX_IN6: 23953541Sshin case SIOCSGIFPREFIX_IN6: 24053541Sshin case SIOCGIFPREFIX_IN6: 24178064Sume log(LOG_NOTICE, 24278064Sume "prefix ioctls are now invalidated. " 24378064Sume "please use ifconfig.\n"); 244120856Sume return (EOPNOTSUPP); 24553541Sshin } 24653541Sshin 24795023Ssuz switch (cmd) { 24862587Sitojun case SIOCSSCOPE6: 249164033Srwatson if (td != NULL) { 250164033Srwatson error = priv_check(td, PRIV_NETINET_SCOPE6); 251164033Srwatson if (error) 252164033Srwatson return (error); 253164033Srwatson } 254121161Sume return (scope6_set(ifp, 255121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 25662587Sitojun case SIOCGSCOPE6: 257121161Sume return (scope6_get(ifp, 258121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 25962587Sitojun case SIOCGSCOPE6DEF: 260121161Sume return (scope6_get_default((struct scope6_id *) 261121161Sume ifr->ifr_ifru.ifru_scope_id)); 26262587Sitojun } 26362587Sitojun 26453541Sshin switch (cmd) { 26553541Sshin case SIOCALIFADDR: 266175630Sbz if (td != NULL) { 267175630Sbz error = priv_check(td, PRIV_NET_ADDIFADDR); 268175630Sbz if (error) 269175630Sbz return (error); 270175630Sbz } 271175630Sbz return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 272175630Sbz 27353541Sshin case SIOCDLIFADDR: 274164033Srwatson if (td != NULL) { 275175630Sbz error = priv_check(td, PRIV_NET_DELIFADDR); 276164033Srwatson if (error) 277164033Srwatson return (error); 278164033Srwatson } 279120891Sume /* FALLTHROUGH */ 28053541Sshin case SIOCGLIFADDR: 28183366Sjulian return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 28253541Sshin } 28353541Sshin 28453541Sshin /* 28553541Sshin * Find address for this interface, if it exists. 286151539Ssuz * 287151539Ssuz * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 288151539Ssuz * only, and used the first interface address as the target of other 289151539Ssuz * operations (without checking ifra_addr). This was because netinet 290151539Ssuz * code/API assumed at most 1 interface address per interface. 291151539Ssuz * Since IPv6 allows a node to assign multiple addresses 292151539Ssuz * on a single interface, we almost always look and check the 293151539Ssuz * presence of ifra_addr, and reject invalid ones here. 294151539Ssuz * It also decreases duplicated code among SIOC*_IN6 operations. 29553541Sshin */ 296151539Ssuz switch (cmd) { 297151539Ssuz case SIOCAIFADDR_IN6: 298151539Ssuz case SIOCSIFPHYADDR_IN6: 299151539Ssuz sa6 = &ifra->ifra_addr; 300151539Ssuz break; 301151539Ssuz case SIOCSIFADDR_IN6: 302151539Ssuz case SIOCGIFADDR_IN6: 303151539Ssuz case SIOCSIFDSTADDR_IN6: 304151539Ssuz case SIOCSIFNETMASK_IN6: 305151539Ssuz case SIOCGIFDSTADDR_IN6: 306151539Ssuz case SIOCGIFNETMASK_IN6: 307151539Ssuz case SIOCDIFADDR_IN6: 308151539Ssuz case SIOCGIFPSRCADDR_IN6: 309151539Ssuz case SIOCGIFPDSTADDR_IN6: 310151539Ssuz case SIOCGIFAFLAG_IN6: 311151539Ssuz case SIOCSNDFLUSH_IN6: 312151539Ssuz case SIOCSPFXFLUSH_IN6: 313151539Ssuz case SIOCSRTRFLUSH_IN6: 314151539Ssuz case SIOCGIFALIFETIME_IN6: 315151539Ssuz case SIOCSIFALIFETIME_IN6: 316151539Ssuz case SIOCGIFSTAT_IN6: 317151539Ssuz case SIOCGIFSTAT_ICMP6: 318151539Ssuz sa6 = &ifr->ifr_addr; 319151539Ssuz break; 320151539Ssuz default: 321151539Ssuz sa6 = NULL; 322151539Ssuz break; 323151539Ssuz } 324151539Ssuz if (sa6 && sa6->sin6_family == AF_INET6) { 325148385Sume int error = 0; 32653541Sshin 327151539Ssuz if (sa6->sin6_scope_id != 0) 328151539Ssuz error = sa6_embedscope(sa6, 0); 329148385Sume else 330151539Ssuz error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 331148385Sume if (error != 0) 332148385Sume return (error); 333188144Sjamie if (td != NULL && (error = prison_check_ip6(td->td_ucred, 334188144Sjamie &sa6->sin6_addr)) != 0) 335188144Sjamie return (error); 336151539Ssuz ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 337151539Ssuz } else 338151539Ssuz ia = NULL; 33953541Sshin 34053541Sshin switch (cmd) { 34178064Sume case SIOCSIFADDR_IN6: 34278064Sume case SIOCSIFDSTADDR_IN6: 34378064Sume case SIOCSIFNETMASK_IN6: 34478064Sume /* 34578064Sume * Since IPv6 allows a node to assign multiple addresses 346151465Ssuz * on a single interface, SIOCSIFxxx ioctls are deprecated. 34778064Sume */ 34878064Sume /* we decided to obsolete this command (20000704) */ 349120856Sume return (EINVAL); 35053541Sshin 35153541Sshin case SIOCDIFADDR_IN6: 35262587Sitojun /* 35378064Sume * for IPv4, we look for existing in_ifaddr here to allow 354151465Ssuz * "ifconfig if0 delete" to remove the first IPv4 address on 355151465Ssuz * the interface. For IPv6, as the spec allows multiple 356151465Ssuz * interface address from the day one, we consider "remove the 357151465Ssuz * first one" semantics to be not preferable. 35862587Sitojun */ 35962587Sitojun if (ia == NULL) 360120856Sume return (EADDRNOTAVAIL); 36153541Sshin /* FALLTHROUGH */ 36253541Sshin case SIOCAIFADDR_IN6: 36362587Sitojun /* 36478064Sume * We always require users to specify a valid IPv6 address for 36578064Sume * the corresponding operation. 36662587Sitojun */ 36778064Sume if (ifra->ifra_addr.sin6_family != AF_INET6 || 36878064Sume ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) 369120856Sume return (EAFNOSUPPORT); 37053541Sshin 371164033Srwatson if (td != NULL) { 372175630Sbz error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ? 373175630Sbz PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 374164033Srwatson if (error) 375164033Srwatson return (error); 376164033Srwatson } 377164033Srwatson 37853541Sshin break; 37953541Sshin 38053541Sshin case SIOCGIFADDR_IN6: 38153541Sshin /* This interface is basically deprecated. use SIOCGIFCONF. */ 382120891Sume /* FALLTHROUGH */ 38353541Sshin case SIOCGIFAFLAG_IN6: 38453541Sshin case SIOCGIFNETMASK_IN6: 38553541Sshin case SIOCGIFDSTADDR_IN6: 38653541Sshin case SIOCGIFALIFETIME_IN6: 38753541Sshin /* must think again about its semantics */ 38862587Sitojun if (ia == NULL) 389120856Sume return (EADDRNOTAVAIL); 39053541Sshin break; 39153541Sshin case SIOCSIFALIFETIME_IN6: 39253541Sshin { 39353541Sshin struct in6_addrlifetime *lt; 39453541Sshin 395164033Srwatson if (td != NULL) { 396164033Srwatson error = priv_check(td, PRIV_NETINET_ALIFETIME6); 397164033Srwatson if (error) 398164033Srwatson return (error); 399164033Srwatson } 40062587Sitojun if (ia == NULL) 401120856Sume return (EADDRNOTAVAIL); 40253541Sshin /* sanity for overflow - beware unsigned */ 40353541Sshin lt = &ifr->ifr_ifru.ifru_lifetime; 404126552Sume if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME && 405126552Sume lt->ia6t_vltime + time_second < time_second) { 40653541Sshin return EINVAL; 40753541Sshin } 408126552Sume if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME && 409126552Sume lt->ia6t_pltime + time_second < time_second) { 41053541Sshin return EINVAL; 41153541Sshin } 41253541Sshin break; 41353541Sshin } 41453541Sshin } 41553541Sshin 41653541Sshin switch (cmd) { 41753541Sshin 41853541Sshin case SIOCGIFADDR_IN6: 41953541Sshin ifr->ifr_addr = ia->ia_addr; 420148385Sume if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 421148385Sume return (error); 42253541Sshin break; 42353541Sshin 42453541Sshin case SIOCGIFDSTADDR_IN6: 42553541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 426120856Sume return (EINVAL); 42762587Sitojun /* 42862587Sitojun * XXX: should we check if ifa_dstaddr is NULL and return 42962587Sitojun * an error? 43062587Sitojun */ 43153541Sshin ifr->ifr_dstaddr = ia->ia_dstaddr; 432148385Sume if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 433148385Sume return (error); 43453541Sshin break; 43553541Sshin 43653541Sshin case SIOCGIFNETMASK_IN6: 43753541Sshin ifr->ifr_addr = ia->ia_prefixmask; 43853541Sshin break; 43953541Sshin 44053541Sshin case SIOCGIFAFLAG_IN6: 44153541Sshin ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 44253541Sshin break; 44353541Sshin 44453541Sshin case SIOCGIFSTAT_IN6: 445121167Sume if (ifp == NULL) 446121167Sume return EINVAL; 447121161Sume bzero(&ifr->ifr_ifru.ifru_stat, 448121161Sume sizeof(ifr->ifr_ifru.ifru_stat)); 449121161Sume ifr->ifr_ifru.ifru_stat = 450121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; 45153541Sshin break; 45253541Sshin 45353541Sshin case SIOCGIFSTAT_ICMP6: 45453541Sshin if (ifp == NULL) 45553541Sshin return EINVAL; 456155454Sgnn bzero(&ifr->ifr_ifru.ifru_icmp6stat, 457121161Sume sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 458121161Sume ifr->ifr_ifru.ifru_icmp6stat = 459121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; 46053541Sshin break; 46153541Sshin 46253541Sshin case SIOCGIFALIFETIME_IN6: 46353541Sshin ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 464151539Ssuz if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 465151539Ssuz time_t maxexpire; 466151539Ssuz struct in6_addrlifetime *retlt = 467151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 468151539Ssuz 469151539Ssuz /* 470151539Ssuz * XXX: adjust expiration time assuming time_t is 471151539Ssuz * signed. 472151539Ssuz */ 473151539Ssuz maxexpire = (-1) & 474151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 475151539Ssuz if (ia->ia6_lifetime.ia6t_vltime < 476151539Ssuz maxexpire - ia->ia6_updatetime) { 477151539Ssuz retlt->ia6t_expire = ia->ia6_updatetime + 478151539Ssuz ia->ia6_lifetime.ia6t_vltime; 479151539Ssuz } else 480151539Ssuz retlt->ia6t_expire = maxexpire; 481151539Ssuz } 482151539Ssuz if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 483151539Ssuz time_t maxexpire; 484151539Ssuz struct in6_addrlifetime *retlt = 485151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 486151539Ssuz 487151539Ssuz /* 488151539Ssuz * XXX: adjust expiration time assuming time_t is 489151539Ssuz * signed. 490151539Ssuz */ 491151539Ssuz maxexpire = (-1) & 492151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 493151539Ssuz if (ia->ia6_lifetime.ia6t_pltime < 494151539Ssuz maxexpire - ia->ia6_updatetime) { 495151539Ssuz retlt->ia6t_preferred = ia->ia6_updatetime + 496151539Ssuz ia->ia6_lifetime.ia6t_pltime; 497151539Ssuz } else 498151539Ssuz retlt->ia6t_preferred = maxexpire; 499151539Ssuz } 50053541Sshin break; 50153541Sshin 50253541Sshin case SIOCSIFALIFETIME_IN6: 50353541Sshin ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 50453541Sshin /* for sanity */ 50553541Sshin if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 50653541Sshin ia->ia6_lifetime.ia6t_expire = 50753541Sshin time_second + ia->ia6_lifetime.ia6t_vltime; 50853541Sshin } else 50953541Sshin ia->ia6_lifetime.ia6t_expire = 0; 51053541Sshin if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 51153541Sshin ia->ia6_lifetime.ia6t_preferred = 51253541Sshin time_second + ia->ia6_lifetime.ia6t_pltime; 51353541Sshin } else 51453541Sshin ia->ia6_lifetime.ia6t_preferred = 0; 51553541Sshin break; 51653541Sshin 51778064Sume case SIOCAIFADDR_IN6: 51878064Sume { 51978064Sume int i, error = 0; 520151539Ssuz struct nd_prefixctl pr0; 521151539Ssuz struct nd_prefix *pr; 52278064Sume 52362587Sitojun /* 52478064Sume * first, make or update the interface address structure, 52578064Sume * and link it to the list. 52662587Sitojun */ 527151539Ssuz if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) 528120856Sume return (error); 529151915Ssuz if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) 530151915Ssuz == NULL) { 531171260Sdelphij /* 532151915Ssuz * this can happen when the user specify the 0 valid 533151915Ssuz * lifetime. 534151915Ssuz */ 535151915Ssuz break; 536151915Ssuz } 53753541Sshin 53878064Sume /* 53978064Sume * then, make the prefix on-link on the interface. 54078064Sume * XXX: we'd rather create the prefix before the address, but 54178064Sume * we need at least one address to install the corresponding 54278064Sume * interface route, so we configure the address first. 54378064Sume */ 54478064Sume 54578064Sume /* 54678064Sume * convert mask to prefix length (prefixmask has already 54778064Sume * been validated in in6_update_ifa(). 54878064Sume */ 54978064Sume bzero(&pr0, sizeof(pr0)); 55078064Sume pr0.ndpr_ifp = ifp; 55178064Sume pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 552120891Sume NULL); 553120891Sume if (pr0.ndpr_plen == 128) { 55478064Sume break; /* we don't need to install a host route. */ 555120891Sume } 55678064Sume pr0.ndpr_prefix = ifra->ifra_addr; 55778064Sume /* apply the mask for safety. */ 55878064Sume for (i = 0; i < 4; i++) { 55978064Sume pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 560120891Sume ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 56178064Sume } 56278064Sume /* 56395023Ssuz * XXX: since we don't have an API to set prefix (not address) 56495023Ssuz * lifetimes, we just use the same lifetimes as addresses. 56595023Ssuz * The (temporarily) installed lifetimes can be overridden by 56695023Ssuz * later advertised RAs (when accept_rtadv is non 0), which is 56795023Ssuz * an intended behavior. 56878064Sume */ 56978064Sume pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 57078064Sume pr0.ndpr_raf_auto = 571120891Sume ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 57278064Sume pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 57378064Sume pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 57478064Sume 575120891Sume /* add the prefix if not yet. */ 57678064Sume if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 57778064Sume /* 57878064Sume * nd6_prelist_add will install the corresponding 57978064Sume * interface route. 58078064Sume */ 58178064Sume if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 582120856Sume return (error); 58378064Sume if (pr == NULL) { 584120891Sume log(LOG_ERR, "nd6_prelist_add succeeded but " 58578064Sume "no prefix\n"); 586120856Sume return (EINVAL); /* XXX panic here? */ 58778064Sume } 58878064Sume } 58978064Sume 590151915Ssuz /* relate the address to the prefix */ 591151915Ssuz if (ia->ia6_ndpr == NULL) { 592151915Ssuz ia->ia6_ndpr = pr; 593151915Ssuz pr->ndpr_refcnt++; 59478064Sume 59578064Sume /* 596151915Ssuz * If this is the first autoconf address from the 597151915Ssuz * prefix, create a temporary address as well 598151915Ssuz * (when required). 59978064Sume */ 600151915Ssuz if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 601181803Sbz V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) { 602151915Ssuz int e; 603151915Ssuz if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { 604151915Ssuz log(LOG_NOTICE, "in6_control: failed " 605151915Ssuz "to create a temporary address, " 606151915Ssuz "errno=%d\n", e); 607151915Ssuz } 608151915Ssuz } 60962587Sitojun } 610151915Ssuz 611151915Ssuz /* 612151915Ssuz * this might affect the status of autoconfigured addresses, 613151915Ssuz * that is, this address might make other addresses detached. 614151915Ssuz */ 615151915Ssuz pfxlist_onlink_check(); 616126264Smlaier if (error == 0 && ia) 617126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 61878064Sume break; 61978064Sume } 62062587Sitojun 62178064Sume case SIOCDIFADDR_IN6: 62278064Sume { 623151539Ssuz struct nd_prefix *pr; 62478064Sume 62578064Sume /* 62678064Sume * If the address being deleted is the only one that owns 62778064Sume * the corresponding prefix, expire the prefix as well. 628120891Sume * XXX: theoretically, we don't have to worry about such 62978064Sume * relationship, since we separate the address management 63078064Sume * and the prefix management. We do this, however, to provide 63178064Sume * as much backward compatibility as possible in terms of 63278064Sume * the ioctl operation. 633151915Ssuz * Note that in6_purgeaddr() will decrement ndpr_refcnt. 63478064Sume */ 635151915Ssuz pr = ia->ia6_ndpr; 63678064Sume in6_purgeaddr(&ia->ia_ifa); 637151915Ssuz if (pr && pr->ndpr_refcnt == 0) 638151915Ssuz prelist_remove(pr); 639126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 64053541Sshin break; 64178064Sume } 64253541Sshin 64378064Sume default: 64478064Sume if (ifp == NULL || ifp->if_ioctl == 0) 645120856Sume return (EOPNOTSUPP); 646120856Sume return ((*ifp->if_ioctl)(ifp, cmd, data)); 64778064Sume } 64853541Sshin 649120856Sume return (0); 65078064Sume} 65153541Sshin 65278064Sume/* 65378064Sume * Update parameters of an IPv6 interface address. 65478064Sume * If necessary, a new entry is created and linked into address chains. 65578064Sume * This function is separated from in6_control(). 65678064Sume * XXX: should this be performed under splnet()? 65778064Sume */ 65878064Sumeint 659171259Sdelphijin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 660171259Sdelphij struct in6_ifaddr *ia, int flags) 66178064Sume{ 662183550Szec INIT_VNET_INET6(ifp->if_vnet); 663183550Szec INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */ 66478064Sume int error = 0, hostIsNew = 0, plen = -1; 66578064Sume struct in6_ifaddr *oia; 66678064Sume struct sockaddr_in6 dst6; 66778064Sume struct in6_addrlifetime *lt; 668151539Ssuz struct in6_multi_mship *imm; 669151539Ssuz struct in6_multi *in6m_sol; 670148385Sume struct rtentry *rt; 671151539Ssuz int delay; 672165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 67378064Sume 67478064Sume /* Validate parameters */ 67578064Sume if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 676120856Sume return (EINVAL); 67778064Sume 67878064Sume /* 67978064Sume * The destination address for a p2p link must have a family 68078064Sume * of AF_UNSPEC or AF_INET6. 68178064Sume */ 68278064Sume if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 68378064Sume ifra->ifra_dstaddr.sin6_family != AF_INET6 && 68478064Sume ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 685120856Sume return (EAFNOSUPPORT); 68678064Sume /* 68778064Sume * validate ifra_prefixmask. don't check sin6_family, netmask 68878064Sume * does not carry fields other than sin6_len. 68978064Sume */ 69078064Sume if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 691120856Sume return (EINVAL); 69278064Sume /* 69378064Sume * Because the IPv6 address architecture is classless, we require 69478064Sume * users to specify a (non 0) prefix length (mask) for a new address. 69578064Sume * We also require the prefix (when specified) mask is valid, and thus 69678064Sume * reject a non-consecutive mask. 69778064Sume */ 69878064Sume if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 699120856Sume return (EINVAL); 70078064Sume if (ifra->ifra_prefixmask.sin6_len != 0) { 70178064Sume plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 702120891Sume (u_char *)&ifra->ifra_prefixmask + 703120891Sume ifra->ifra_prefixmask.sin6_len); 70478064Sume if (plen <= 0) 705120856Sume return (EINVAL); 706120891Sume } else { 70762587Sitojun /* 70895023Ssuz * In this case, ia must not be NULL. We just use its prefix 70978064Sume * length. 71062587Sitojun */ 71178064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 71278064Sume } 71378064Sume /* 71478064Sume * If the destination address on a p2p interface is specified, 71578064Sume * and the address is a scoped one, validate/set the scope 71678064Sume * zone identifier. 71778064Sume */ 71878064Sume dst6 = ifra->ifra_dstaddr; 719120891Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 72078064Sume (dst6.sin6_family == AF_INET6)) { 721148385Sume struct in6_addr in6_tmp; 722126552Sume u_int32_t zoneid; 72378064Sume 724148385Sume in6_tmp = dst6.sin6_addr; 725148385Sume if (in6_setscope(&in6_tmp, ifp, &zoneid)) 726148385Sume return (EINVAL); /* XXX: should be impossible */ 727148385Sume 728148385Sume if (dst6.sin6_scope_id != 0) { 729148385Sume if (dst6.sin6_scope_id != zoneid) 730148385Sume return (EINVAL); 731148385Sume } else /* user omit to specify the ID. */ 732126552Sume dst6.sin6_scope_id = zoneid; 733148385Sume 734148385Sume /* convert into the internal form */ 735148385Sume if (sa6_embedscope(&dst6, 0)) 736148385Sume return (EINVAL); /* XXX: should be impossible */ 73778064Sume } 73878064Sume /* 73978064Sume * The destination address can be specified only for a p2p or a 74078064Sume * loopback interface. If specified, the corresponding prefix length 74178064Sume * must be 128. 74278064Sume */ 74378064Sume if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 74478064Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 745126552Sume /* XXX: noisy message */ 746122059Sume nd6log((LOG_INFO, "in6_update_ifa: a destination can " 747122059Sume "be specified for a p2p or a loopback IF only\n")); 748120856Sume return (EINVAL); 74978064Sume } 75078064Sume if (plen != 128) { 751122059Sume nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 752122059Sume "be 128 when dstaddr is specified\n")); 753120856Sume return (EINVAL); 75478064Sume } 75578064Sume } 75678064Sume /* lifetime consistency check */ 75778064Sume lt = &ifra->ifra_lifetime; 758151539Ssuz if (lt->ia6t_pltime > lt->ia6t_vltime) 759151539Ssuz return (EINVAL); 76078064Sume if (lt->ia6t_vltime == 0) { 76162587Sitojun /* 76278064Sume * the following log might be noisy, but this is a typical 76378064Sume * configuration mistake or a tool's bug. 76462587Sitojun */ 765122059Sume nd6log((LOG_INFO, 76678064Sume "in6_update_ifa: valid lifetime is 0 for %s\n", 767165118Sbz ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 768151539Ssuz 769151539Ssuz if (ia == NULL) 770151539Ssuz return (0); /* there's nothing to do */ 77178064Sume } 77262587Sitojun 77378064Sume /* 77478064Sume * If this is a new address, allocate a new ifaddr and link it 77578064Sume * into chains. 77678064Sume */ 77778064Sume if (ia == NULL) { 77878064Sume hostIsNew = 1; 77979763Sume /* 78079763Sume * When in6_update_ifa() is called in a process of a received 781120891Sume * RA, it is called under an interrupt context. So, we should 782120891Sume * call malloc with M_NOWAIT. 78379763Sume */ 784120891Sume ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, 785120891Sume M_NOWAIT); 78678064Sume if (ia == NULL) 78778064Sume return (ENOBUFS); 78878064Sume bzero((caddr_t)ia, sizeof(*ia)); 789170202Sjinmei LIST_INIT(&ia->ia6_memberships); 790151539Ssuz /* Initialize the address and masks, and put time stamp */ 791108033Shsu IFA_LOCK_INIT(&ia->ia_ifa); 79278064Sume ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 79378064Sume ia->ia_addr.sin6_family = AF_INET6; 79478064Sume ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 795151539Ssuz ia->ia6_createtime = time_second; 79678064Sume if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 79778064Sume /* 79878064Sume * XXX: some functions expect that ifa_dstaddr is not 79978064Sume * NULL for p2p interfaces. 80078064Sume */ 801120891Sume ia->ia_ifa.ifa_dstaddr = 802120891Sume (struct sockaddr *)&ia->ia_dstaddr; 80378064Sume } else { 80478064Sume ia->ia_ifa.ifa_dstaddr = NULL; 80553541Sshin } 806108033Shsu ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 80778064Sume 80878064Sume ia->ia_ifp = ifp; 809181803Sbz if ((oia = V_in6_ifaddr) != NULL) { 81078064Sume for ( ; oia->ia_next; oia = oia->ia_next) 81178064Sume continue; 81278064Sume oia->ia_next = ia; 81378064Sume } else 814181803Sbz V_in6_ifaddr = ia; 81578064Sume 816108033Shsu ia->ia_ifa.ifa_refcnt = 1; 817108033Shsu TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 81878064Sume } 81978064Sume 820151539Ssuz /* update timestamp */ 821151539Ssuz ia->ia6_updatetime = time_second; 822151539Ssuz 82378064Sume /* set prefix mask */ 82478064Sume if (ifra->ifra_prefixmask.sin6_len) { 82578064Sume /* 82678064Sume * We prohibit changing the prefix length of an existing 82778064Sume * address, because 82878064Sume * + such an operation should be rare in IPv6, and 82978064Sume * + the operation would confuse prefix management. 83078064Sume */ 83178064Sume if (ia->ia_prefixmask.sin6_len && 83278064Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 833122059Sume nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an" 83478064Sume " existing (%s) address should not be changed\n", 835165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 83678064Sume error = EINVAL; 83778064Sume goto unlink; 83853541Sshin } 83978064Sume ia->ia_prefixmask = ifra->ifra_prefixmask; 84078064Sume } 84178064Sume 84278064Sume /* 84378064Sume * If a new destination address is specified, scrub the old one and 84478064Sume * install the new destination. Note that the interface must be 845120891Sume * p2p or loopback (see the check above.) 84678064Sume */ 84778064Sume if (dst6.sin6_family == AF_INET6 && 848120891Sume !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 84978064Sume int e; 85078064Sume 85178064Sume if ((ia->ia_flags & IFA_ROUTE) != 0 && 852120891Sume (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 853122059Sume nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 85478064Sume "a route to the old destination: %s\n", 855165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 85678064Sume /* proceed anyway... */ 857120891Sume } else 85878064Sume ia->ia_flags &= ~IFA_ROUTE; 85978064Sume ia->ia_dstaddr = dst6; 86078064Sume } 86153541Sshin 862148385Sume /* 863148385Sume * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 864148385Sume * to see if the address is deprecated or invalidated, but initialize 865148385Sume * these members for applications. 866148385Sume */ 867148385Sume ia->ia6_lifetime = ifra->ifra_lifetime; 868148385Sume if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 869148385Sume ia->ia6_lifetime.ia6t_expire = 870148385Sume time_second + ia->ia6_lifetime.ia6t_vltime; 871148385Sume } else 872148385Sume ia->ia6_lifetime.ia6t_expire = 0; 873148385Sume if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 874148385Sume ia->ia6_lifetime.ia6t_preferred = 875148385Sume time_second + ia->ia6_lifetime.ia6t_pltime; 876148385Sume } else 877148385Sume ia->ia6_lifetime.ia6t_preferred = 0; 878148385Sume 87978064Sume /* reset the interface and routing table appropriately. */ 88078064Sume if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 88178064Sume goto unlink; 88278064Sume 88378064Sume /* 884148385Sume * configure address flags. 885148385Sume */ 886148385Sume ia->ia6_flags = ifra->ifra_flags; 887148385Sume /* 888148385Sume * backward compatibility - if IN6_IFF_DEPRECATED is set from the 889148385Sume * userland, make it deprecated. 890148385Sume */ 891148385Sume if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 892148385Sume ia->ia6_lifetime.ia6t_pltime = 0; 893148385Sume ia->ia6_lifetime.ia6t_preferred = time_second; 894148385Sume } 895148385Sume /* 896151539Ssuz * Make the address tentative before joining multicast addresses, 897151539Ssuz * so that corresponding MLD responses would not have a tentative 898151539Ssuz * source address. 899148385Sume */ 900151539Ssuz ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 901151539Ssuz if (hostIsNew && in6if_do_dad(ifp)) 902148385Sume ia->ia6_flags |= IN6_IFF_TENTATIVE; 903148385Sume 904148385Sume /* 905148385Sume * We are done if we have simply modified an existing address. 906148385Sume */ 907148385Sume if (!hostIsNew) 908148385Sume return (error); 909148385Sume 910148385Sume /* 91178064Sume * Beyond this point, we should call in6_purgeaddr upon an error, 912120891Sume * not just go to unlink. 91378064Sume */ 91478064Sume 915151465Ssuz /* Join necessary multicast groups */ 916151539Ssuz in6m_sol = NULL; 91778064Sume if ((ifp->if_flags & IFF_MULTICAST) != 0) { 91878064Sume struct sockaddr_in6 mltaddr, mltmask; 919148385Sume struct in6_addr llsol; 92078064Sume 921148385Sume /* join solicited multicast addr for new host id */ 922148385Sume bzero(&llsol, sizeof(struct in6_addr)); 923151465Ssuz llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL; 924148385Sume llsol.s6_addr32[1] = 0; 925148385Sume llsol.s6_addr32[2] = htonl(1); 926148385Sume llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; 927148385Sume llsol.s6_addr8[12] = 0xff; 928148385Sume if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 929148385Sume /* XXX: should not happen */ 930148385Sume log(LOG_ERR, "in6_update_ifa: " 931148385Sume "in6_setscope failed\n"); 932148385Sume goto cleanup; 93353541Sshin } 934151539Ssuz delay = 0; 935151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 936151539Ssuz /* 937151539Ssuz * We need a random delay for DAD on the address 938151539Ssuz * being configured. It also means delaying 939151539Ssuz * transmission of the corresponding MLD report to 940151539Ssuz * avoid report collision. 941151539Ssuz * [draft-ietf-ipv6-rfc2462bis-02.txt] 942151539Ssuz */ 943151539Ssuz delay = arc4random() % 944151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 945151539Ssuz } 946151539Ssuz imm = in6_joingroup(ifp, &llsol, &error, delay); 947170202Sjinmei if (imm == NULL) { 948148385Sume nd6log((LOG_WARNING, 949148385Sume "in6_update_ifa: addmulti failed for " 950148385Sume "%s on %s (errno=%d)\n", 951165118Sbz ip6_sprintf(ip6buf, &llsol), if_name(ifp), 952148385Sume error)); 953151539Ssuz in6_purgeaddr((struct ifaddr *)ia); 954151539Ssuz return (error); 955148385Sume } 956170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 957170202Sjinmei imm, i6mm_chain); 958151539Ssuz in6m_sol = imm->i6mm_maddr; 95953541Sshin 96078064Sume bzero(&mltmask, sizeof(mltmask)); 96178064Sume mltmask.sin6_len = sizeof(struct sockaddr_in6); 96278064Sume mltmask.sin6_family = AF_INET6; 96378064Sume mltmask.sin6_addr = in6mask32; 964151465Ssuz#define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 96553541Sshin 96653541Sshin /* 96778064Sume * join link-local all-nodes address 96871207Sitojun */ 96978064Sume bzero(&mltaddr, sizeof(mltaddr)); 97078064Sume mltaddr.sin6_len = sizeof(struct sockaddr_in6); 97178064Sume mltaddr.sin6_family = AF_INET6; 97278064Sume mltaddr.sin6_addr = in6addr_linklocal_allnodes; 973148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 974148385Sume 0) 975148385Sume goto cleanup; /* XXX: should not fail */ 97671207Sitojun 977148385Sume /* 978148385Sume * XXX: do we really need this automatic routes? 979148385Sume * We should probably reconsider this stuff. Most applications 980148385Sume * actually do not need the routes, since they usually specify 981148385Sume * the outgoing interface. 982148385Sume */ 983148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 984148385Sume if (rt) { 985174375Sjulian /* XXX: only works in !SCOPEDROUTING case. */ 986148385Sume if (memcmp(&mltaddr.sin6_addr, 987148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 988151465Ssuz MLTMASK_LEN)) { 989148385Sume RTFREE_LOCKED(rt); 990148385Sume rt = NULL; 991148385Sume } 992148385Sume } 993148385Sume if (!rt) { 994148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 995148385Sume (struct sockaddr *)&ia->ia_addr, 996186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 997148385Sume (struct rtentry **)0); 998148385Sume if (error) 999148385Sume goto cleanup; 1000151539Ssuz } else { 1001151539Ssuz RTFREE_LOCKED(rt); 1002151539Ssuz } 100378064Sume 1004151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1005151539Ssuz if (!imm) { 1006151539Ssuz nd6log((LOG_WARNING, 1007151539Ssuz "in6_update_ifa: addmulti failed for " 1008151539Ssuz "%s on %s (errno=%d)\n", 1009165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1010151539Ssuz if_name(ifp), error)); 1011151539Ssuz goto cleanup; 1012151539Ssuz } 1013170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1014151539Ssuz 101571207Sitojun /* 101678064Sume * join node information group address 101753541Sshin */ 1018181803Sbz#define hostnamelen strlen(V_hostname) 1019151539Ssuz delay = 0; 1020151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1021151539Ssuz /* 1022151539Ssuz * The spec doesn't say anything about delay for this 1023151539Ssuz * group, but the same logic should apply. 1024151539Ssuz */ 1025151539Ssuz delay = arc4random() % 1026151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 1027151539Ssuz } 1028180291Srwatson mtx_lock(&hostname_mtx); 1029181803Sbz if (in6_nigroup(ifp, V_hostname, hostnamelen, 1030181803Sbz &mltaddr.sin6_addr) == 0) { 1031180291Srwatson mtx_unlock(&hostname_mtx); 1032151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 1033151539Ssuz delay); /* XXX jinmei */ 1034151539Ssuz if (!imm) { 1035151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1036151539Ssuz "addmulti failed for %s on %s " 1037151539Ssuz "(errno=%d)\n", 1038165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1039151539Ssuz if_name(ifp), error)); 1040151539Ssuz /* XXX not very fatal, go on... */ 1041170202Sjinmei } else { 1042170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 1043170202Sjinmei imm, i6mm_chain); 104462587Sitojun } 1045180291Srwatson } else 1046180291Srwatson mtx_unlock(&hostname_mtx); 104778064Sume#undef hostnamelen 104853541Sshin 104978064Sume /* 1050148385Sume * join interface-local all-nodes address. 1051148385Sume * (ff01::1%ifN, and ff01::%ifN/32) 105278064Sume */ 1053148385Sume mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1054148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) 1055148385Sume != 0) 1056148385Sume goto cleanup; /* XXX: should not fail */ 1057148385Sume /* XXX: again, do we really need the route? */ 1058148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1059148385Sume if (rt) { 1060148385Sume if (memcmp(&mltaddr.sin6_addr, 1061148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1062151465Ssuz MLTMASK_LEN)) { 1063148385Sume RTFREE_LOCKED(rt); 1064148385Sume rt = NULL; 1065148385Sume } 1066148385Sume } 1067148385Sume if (!rt) { 1068148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1069148385Sume (struct sockaddr *)&ia->ia_addr, 1070186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1071148385Sume (struct rtentry **)0); 1072148385Sume if (error) 1073148385Sume goto cleanup; 1074148385Sume } else 1075148385Sume RTFREE_LOCKED(rt); 107681115Sume 1077151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1078151539Ssuz if (!imm) { 1079151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1080151539Ssuz "addmulti failed for %s on %s " 1081151539Ssuz "(errno=%d)\n", 1082165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1083151539Ssuz if_name(ifp), error)); 1084151539Ssuz goto cleanup; 1085151539Ssuz } 1086170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1087151465Ssuz#undef MLTMASK_LEN 108878064Sume } 108953541Sshin 1090151539Ssuz /* 1091151539Ssuz * Perform DAD, if needed. 1092151539Ssuz * XXX It may be of use, if we can administratively 1093151539Ssuz * disable DAD. 1094151539Ssuz */ 1095151539Ssuz if (hostIsNew && in6if_do_dad(ifp) && 1096151539Ssuz ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1097151539Ssuz (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1098151539Ssuz { 1099151539Ssuz int mindelay, maxdelay; 1100151539Ssuz 1101151539Ssuz delay = 0; 1102151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1103151539Ssuz /* 1104151539Ssuz * We need to impose a delay before sending an NS 1105151539Ssuz * for DAD. Check if we also needed a delay for the 1106151539Ssuz * corresponding MLD message. If we did, the delay 1107151539Ssuz * should be larger than the MLD delay (this could be 1108151539Ssuz * relaxed a bit, but this simple logic is at least 1109151539Ssuz * safe). 1110151539Ssuz */ 1111151539Ssuz mindelay = 0; 1112151539Ssuz if (in6m_sol != NULL && 1113151539Ssuz in6m_sol->in6m_state == MLD_REPORTPENDING) { 1114151539Ssuz mindelay = in6m_sol->in6m_timer; 1115151539Ssuz } 1116151539Ssuz maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1117151539Ssuz if (maxdelay - mindelay == 0) 1118151539Ssuz delay = 0; 1119151539Ssuz else { 1120151539Ssuz delay = 1121151539Ssuz (arc4random() % (maxdelay - mindelay)) + 1122151539Ssuz mindelay; 1123151539Ssuz } 1124151539Ssuz } 1125151539Ssuz nd6_dad_start((struct ifaddr *)ia, delay); 1126151539Ssuz } 1127151539Ssuz 1128120856Sume return (error); 112978064Sume 113078064Sume unlink: 113178064Sume /* 113278064Sume * XXX: if a change of an existing address failed, keep the entry 113378064Sume * anyway. 113478064Sume */ 113578064Sume if (hostIsNew) 113678064Sume in6_unlink_ifa(ia, ifp); 1137120856Sume return (error); 1138148385Sume 1139148385Sume cleanup: 1140148385Sume in6_purgeaddr(&ia->ia_ifa); 1141148385Sume return error; 114253541Sshin} 114353541Sshin 114462587Sitojunvoid 1145171259Sdelphijin6_purgeaddr(struct ifaddr *ifa) 114662587Sitojun{ 114778064Sume struct ifnet *ifp = ifa->ifa_ifp; 114878064Sume struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1149170202Sjinmei struct in6_multi_mship *imm; 115062587Sitojun 115178064Sume /* stop DAD processing */ 115278064Sume nd6_dad_stop(ifa); 115362587Sitojun 1154186119Sqingli IF_AFDATA_LOCK(ifp); 1155186119Sqingli lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR), 1156186119Sqingli (struct sockaddr *)&ia->ia_addr); 1157186119Sqingli IF_AFDATA_UNLOCK(ifp); 1158186119Sqingli 115978064Sume /* 1160170202Sjinmei * leave from multicast groups we have joined for the interface 1161170202Sjinmei */ 1162170202Sjinmei while ((imm = ia->ia6_memberships.lh_first) != NULL) { 1163170202Sjinmei LIST_REMOVE(imm, i6mm_chain); 1164170202Sjinmei in6_leavegroup(imm); 116562587Sitojun } 116662587Sitojun 116778064Sume in6_unlink_ifa(ia, ifp); 116878064Sume} 116978064Sume 117078064Sumestatic void 1171171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 117278064Sume{ 1173183550Szec INIT_VNET_INET6(ifp->if_vnet); 117478064Sume struct in6_ifaddr *oia; 117578064Sume int s = splnet(); 117678064Sume 117762587Sitojun TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 117862587Sitojun 117962587Sitojun oia = ia; 1180181803Sbz if (oia == (ia = V_in6_ifaddr)) 1181181803Sbz V_in6_ifaddr = ia->ia_next; 118262587Sitojun else { 118362587Sitojun while (ia->ia_next && (ia->ia_next != oia)) 118462587Sitojun ia = ia->ia_next; 118562587Sitojun if (ia->ia_next) 118662587Sitojun ia->ia_next = oia->ia_next; 118778064Sume else { 118878064Sume /* search failed */ 118978064Sume printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n"); 119078064Sume } 119162587Sitojun } 119262587Sitojun 119362587Sitojun /* 1194151915Ssuz * Release the reference to the base prefix. There should be a 1195151915Ssuz * positive reference. 119662587Sitojun */ 1197151915Ssuz if (oia->ia6_ndpr == NULL) { 1198151915Ssuz nd6log((LOG_NOTICE, 1199151915Ssuz "in6_unlink_ifa: autoconf'ed address " 1200151915Ssuz "%p has no prefix\n", oia)); 1201151915Ssuz } else { 1202151915Ssuz oia->ia6_ndpr->ndpr_refcnt--; 1203151915Ssuz oia->ia6_ndpr = NULL; 1204151915Ssuz } 120562587Sitojun 1206151915Ssuz /* 1207151915Ssuz * Also, if the address being removed is autoconf'ed, call 1208151915Ssuz * pfxlist_onlink_check() since the release might affect the status of 1209171260Sdelphij * other (detached) addresses. 1210151915Ssuz */ 1211151915Ssuz if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) { 121278064Sume pfxlist_onlink_check(); 121362587Sitojun } 121478064Sume 121578064Sume /* 121678064Sume * release another refcnt for the link from in6_ifaddr. 121778064Sume * Note that we should decrement the refcnt at least once for all *BSD. 121878064Sume */ 121962587Sitojun IFAFREE(&oia->ia_ifa); 122078064Sume 122178064Sume splx(s); 122262587Sitojun} 122362587Sitojun 122478064Sumevoid 1225171259Sdelphijin6_purgeif(struct ifnet *ifp) 122678064Sume{ 122778064Sume struct ifaddr *ifa, *nifa; 122878064Sume 1229120891Sume for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) { 123078064Sume nifa = TAILQ_NEXT(ifa, ifa_list); 123178064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 123278064Sume continue; 123378064Sume in6_purgeaddr(ifa); 123478064Sume } 123578064Sume 123678064Sume in6_ifdetach(ifp); 123778064Sume} 123878064Sume 123953541Sshin/* 124053541Sshin * SIOC[GAD]LIFADDR. 124162744Sgrog * SIOCGLIFADDR: get first address. (?) 124253541Sshin * SIOCGLIFADDR with IFLR_PREFIX: 124353541Sshin * get first address that matches the specified prefix. 124453541Sshin * SIOCALIFADDR: add the specified address. 124553541Sshin * SIOCALIFADDR with IFLR_PREFIX: 124653541Sshin * add the specified prefix, filling hostid part from 124753541Sshin * the first link-local address. prefixlen must be <= 64. 124853541Sshin * SIOCDLIFADDR: delete the specified address. 124953541Sshin * SIOCDLIFADDR with IFLR_PREFIX: 125053541Sshin * delete the first address that matches the specified prefix. 125153541Sshin * return values: 125253541Sshin * EINVAL on invalid parameters 125353541Sshin * EADDRNOTAVAIL on prefix match failed/specified address not found 125453541Sshin * other values may be returned from in6_ioctl() 125553541Sshin * 125653541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 125753541Sshin * this is to accomodate address naming scheme other than RFC2374, 125853541Sshin * in the future. 125953541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 126053541Sshin * address encoding scheme. (see figure on page 8) 126153541Sshin */ 126253541Sshinstatic int 1263171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1264171259Sdelphij struct ifnet *ifp, struct thread *td) 126553541Sshin{ 126653541Sshin struct if_laddrreq *iflr = (struct if_laddrreq *)data; 126753541Sshin struct ifaddr *ifa; 126862587Sitojun struct sockaddr *sa; 126953541Sshin 127053541Sshin /* sanity checks */ 127153541Sshin if (!data || !ifp) { 127253541Sshin panic("invalid argument to in6_lifaddr_ioctl"); 1273120891Sume /* NOTREACHED */ 127453541Sshin } 127553541Sshin 127653541Sshin switch (cmd) { 127753541Sshin case SIOCGLIFADDR: 127853541Sshin /* address must be specified on GET with IFLR_PREFIX */ 127953541Sshin if ((iflr->flags & IFLR_PREFIX) == 0) 128053541Sshin break; 128195023Ssuz /* FALLTHROUGH */ 128253541Sshin case SIOCALIFADDR: 128353541Sshin case SIOCDLIFADDR: 128453541Sshin /* address must be specified on ADD and DELETE */ 128562587Sitojun sa = (struct sockaddr *)&iflr->addr; 128662587Sitojun if (sa->sa_family != AF_INET6) 128753541Sshin return EINVAL; 128862587Sitojun if (sa->sa_len != sizeof(struct sockaddr_in6)) 128953541Sshin return EINVAL; 129053541Sshin /* XXX need improvement */ 129162587Sitojun sa = (struct sockaddr *)&iflr->dstaddr; 129262587Sitojun if (sa->sa_family && sa->sa_family != AF_INET6) 129353541Sshin return EINVAL; 129462587Sitojun if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 129553541Sshin return EINVAL; 129653541Sshin break; 129795023Ssuz default: /* shouldn't happen */ 129862587Sitojun#if 0 129962587Sitojun panic("invalid cmd to in6_lifaddr_ioctl"); 130095023Ssuz /* NOTREACHED */ 130162587Sitojun#else 130253541Sshin return EOPNOTSUPP; 130362587Sitojun#endif 130453541Sshin } 130553541Sshin if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 130653541Sshin return EINVAL; 130753541Sshin 130853541Sshin switch (cmd) { 130953541Sshin case SIOCALIFADDR: 131053541Sshin { 131153541Sshin struct in6_aliasreq ifra; 131253541Sshin struct in6_addr *hostid = NULL; 131353541Sshin int prefixlen; 131453541Sshin 131553541Sshin if ((iflr->flags & IFLR_PREFIX) != 0) { 131653541Sshin struct sockaddr_in6 *sin6; 131753541Sshin 131853541Sshin /* 131953541Sshin * hostid is to fill in the hostid part of the 132053541Sshin * address. hostid points to the first link-local 132153541Sshin * address attached to the interface. 132253541Sshin */ 132362587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 132453541Sshin if (!ifa) 132553541Sshin return EADDRNOTAVAIL; 132653541Sshin hostid = IFA_IN6(ifa); 132753541Sshin 1328171260Sdelphij /* prefixlen must be <= 64. */ 132953541Sshin if (64 < iflr->prefixlen) 133053541Sshin return EINVAL; 133153541Sshin prefixlen = iflr->prefixlen; 133253541Sshin 133353541Sshin /* hostid part must be zero. */ 133453541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 1335126552Sume if (sin6->sin6_addr.s6_addr32[2] != 0 || 1336126552Sume sin6->sin6_addr.s6_addr32[3] != 0) { 133753541Sshin return EINVAL; 133853541Sshin } 133953541Sshin } else 134053541Sshin prefixlen = iflr->prefixlen; 134153541Sshin 134253541Sshin /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 134353541Sshin bzero(&ifra, sizeof(ifra)); 1344120891Sume bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 134553541Sshin 134662587Sitojun bcopy(&iflr->addr, &ifra.ifra_addr, 1347120891Sume ((struct sockaddr *)&iflr->addr)->sa_len); 134853541Sshin if (hostid) { 134953541Sshin /* fill in hostid part */ 135053541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1351120891Sume hostid->s6_addr32[2]; 135253541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1353120891Sume hostid->s6_addr32[3]; 135453541Sshin } 135553541Sshin 1356120891Sume if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 135753541Sshin bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1358120891Sume ((struct sockaddr *)&iflr->dstaddr)->sa_len); 135953541Sshin if (hostid) { 136053541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1361120891Sume hostid->s6_addr32[2]; 136253541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1363120891Sume hostid->s6_addr32[3]; 136453541Sshin } 136553541Sshin } 136653541Sshin 136753541Sshin ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1368121168Sume in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 136953541Sshin 137053541Sshin ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 137183366Sjulian return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 137253541Sshin } 137353541Sshin case SIOCGLIFADDR: 137453541Sshin case SIOCDLIFADDR: 137553541Sshin { 137653541Sshin struct in6_ifaddr *ia; 137753541Sshin struct in6_addr mask, candidate, match; 137853541Sshin struct sockaddr_in6 *sin6; 137953541Sshin int cmp; 138053541Sshin 138153541Sshin bzero(&mask, sizeof(mask)); 138253541Sshin if (iflr->flags & IFLR_PREFIX) { 138353541Sshin /* lookup a prefix rather than address. */ 1384121168Sume in6_prefixlen2mask(&mask, iflr->prefixlen); 138553541Sshin 138653541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 138753541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 138853541Sshin match.s6_addr32[0] &= mask.s6_addr32[0]; 138953541Sshin match.s6_addr32[1] &= mask.s6_addr32[1]; 139053541Sshin match.s6_addr32[2] &= mask.s6_addr32[2]; 139153541Sshin match.s6_addr32[3] &= mask.s6_addr32[3]; 139253541Sshin 139353541Sshin /* if you set extra bits, that's wrong */ 139453541Sshin if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 139553541Sshin return EINVAL; 139653541Sshin 139753541Sshin cmp = 1; 139853541Sshin } else { 139953541Sshin if (cmd == SIOCGLIFADDR) { 140053541Sshin /* on getting an address, take the 1st match */ 140195023Ssuz cmp = 0; /* XXX */ 140253541Sshin } else { 140353541Sshin /* on deleting an address, do exact match */ 1404121168Sume in6_prefixlen2mask(&mask, 128); 140553541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 140653541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 140753541Sshin 140853541Sshin cmp = 1; 140953541Sshin } 141053541Sshin } 141153541Sshin 1412120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 141353541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 141453541Sshin continue; 141553541Sshin if (!cmp) 141653541Sshin break; 141778064Sume 141878064Sume /* 141978064Sume * XXX: this is adhoc, but is necessary to allow 142078064Sume * a user to specify fe80::/64 (not /10) for a 142178064Sume * link-local address. 142278064Sume */ 1423148385Sume bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1424148385Sume in6_clearscope(&candidate); 142553541Sshin candidate.s6_addr32[0] &= mask.s6_addr32[0]; 142653541Sshin candidate.s6_addr32[1] &= mask.s6_addr32[1]; 142753541Sshin candidate.s6_addr32[2] &= mask.s6_addr32[2]; 142853541Sshin candidate.s6_addr32[3] &= mask.s6_addr32[3]; 142953541Sshin if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 143053541Sshin break; 143153541Sshin } 143253541Sshin if (!ifa) 143353541Sshin return EADDRNOTAVAIL; 143453541Sshin ia = ifa2ia6(ifa); 143553541Sshin 143653541Sshin if (cmd == SIOCGLIFADDR) { 1437148385Sume int error; 143878064Sume 143953541Sshin /* fill in the if_laddrreq structure */ 144053541Sshin bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1441148385Sume error = sa6_recoverscope( 1442148385Sume (struct sockaddr_in6 *)&iflr->addr); 1443148385Sume if (error != 0) 1444148385Sume return (error); 1445148385Sume 144653541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 144753541Sshin bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1448120891Sume ia->ia_dstaddr.sin6_len); 1449148385Sume error = sa6_recoverscope( 1450148385Sume (struct sockaddr_in6 *)&iflr->dstaddr); 1451148385Sume if (error != 0) 1452148385Sume return (error); 145353541Sshin } else 145453541Sshin bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 145553541Sshin 145653541Sshin iflr->prefixlen = 1457120891Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 145853541Sshin 145995023Ssuz iflr->flags = ia->ia6_flags; /* XXX */ 146053541Sshin 146153541Sshin return 0; 146253541Sshin } else { 146353541Sshin struct in6_aliasreq ifra; 146453541Sshin 146553541Sshin /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 146653541Sshin bzero(&ifra, sizeof(ifra)); 146753541Sshin bcopy(iflr->iflr_name, ifra.ifra_name, 1468120891Sume sizeof(ifra.ifra_name)); 146953541Sshin 147053541Sshin bcopy(&ia->ia_addr, &ifra.ifra_addr, 1471120891Sume ia->ia_addr.sin6_len); 147253541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 147353541Sshin bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1474120891Sume ia->ia_dstaddr.sin6_len); 147562587Sitojun } else { 147662587Sitojun bzero(&ifra.ifra_dstaddr, 147762587Sitojun sizeof(ifra.ifra_dstaddr)); 147853541Sshin } 147953541Sshin bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1480120891Sume ia->ia_prefixmask.sin6_len); 148153541Sshin 148253541Sshin ifra.ifra_flags = ia->ia6_flags; 148353541Sshin return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1484120891Sume ifp, td); 148553541Sshin } 148653541Sshin } 148753541Sshin } 148853541Sshin 148995023Ssuz return EOPNOTSUPP; /* just for safety */ 149053541Sshin} 149153541Sshin 149253541Sshin/* 149353541Sshin * Initialize an interface's intetnet6 address 149453541Sshin * and routing table entry. 149553541Sshin */ 149678064Sumestatic int 1497171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1498171259Sdelphij struct sockaddr_in6 *sin6, int newhost) 149953541Sshin{ 150078064Sume int error = 0, plen, ifacount = 0; 150153541Sshin int s = splimp(); 150278064Sume struct ifaddr *ifa; 150353541Sshin 150453541Sshin /* 150553541Sshin * Give the interface a chance to initialize 150653541Sshin * if this is its first address, 150753541Sshin * and to validate the address if necessary. 150853541Sshin */ 1509120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 151078064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 151178064Sume continue; 151278064Sume ifacount++; 151378064Sume } 151478064Sume 151578064Sume ia->ia_addr = *sin6; 151678064Sume 1517146883Siedowse if (ifacount <= 1 && ifp->if_ioctl) { 1518146883Siedowse IFF_LOCKGIANT(ifp); 1519146883Siedowse error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1520146883Siedowse IFF_UNLOCKGIANT(ifp); 1521146883Siedowse if (error) { 1522146883Siedowse splx(s); 1523146883Siedowse return (error); 1524146883Siedowse } 152553541Sshin } 152678064Sume splx(s); 152753541Sshin 152878064Sume ia->ia_ifa.ifa_metric = ifp->if_metric; 152953541Sshin 153078064Sume /* we could do in(6)_socktrim here, but just omit it at this moment. */ 153178064Sume 153253541Sshin /* 153378064Sume * Special case: 1534124337Sume * If a new destination address is specified for a point-to-point 153578064Sume * interface, install a route to the destination as an interface 1536186119Sqingli * direct route. 1537124337Sume * XXX: the logic below rejects assigning multiple addresses on a p2p 1538159390Sgnn * interface that share the same destination. 153953541Sshin */ 154078064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1541186708Sqingli if (!(ia->ia_flags & IFA_ROUTE) && plen == 128) { 1542186708Sqingli struct sockaddr *dstaddr; 1543159390Sgnn int rtflags = RTF_UP | RTF_HOST; 1544159390Sgnn 1545186708Sqingli /* 1546186708Sqingli * use the interface address if configuring an 1547186708Sqingli * interface address with a /128 prefix len 1548186708Sqingli */ 1549186708Sqingli if (ia->ia_dstaddr.sin6_family == AF_INET6) 1550186708Sqingli dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 1551186708Sqingli else 1552186708Sqingli dstaddr = (struct sockaddr *)&ia->ia_addr; 1553159390Sgnn 1554178888Sjulian error = rtrequest(RTM_ADD, 1555186708Sqingli (struct sockaddr *)dstaddr, 1556159390Sgnn (struct sockaddr *)&ia->ia_addr, 1557159390Sgnn (struct sockaddr *)&ia->ia_prefixmask, 1558186708Sqingli ia->ia_flags | rtflags, NULL); 1559159390Sgnn if (error != 0) 1560120856Sume return (error); 156178064Sume ia->ia_flags |= IFA_ROUTE; 156253541Sshin } 156353541Sshin 156495023Ssuz /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1565186119Sqingli if (newhost) { 1566186119Sqingli struct llentry *ln; 156753541Sshin 1568186119Sqingli IF_AFDATA_LOCK(ifp); 1569186119Sqingli ia->ia_ifa.ifa_rtrequest = NULL; 1570186119Sqingli 1571186119Sqingli /* XXX QL 1572186119Sqingli * we need to report rt_newaddrmsg 1573186119Sqingli */ 1574186119Sqingli ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE), 1575186119Sqingli (struct sockaddr *)&ia->ia_addr); 1576186119Sqingli IF_AFDATA_UNLOCK(ifp); 1577186158Skmacy if (ln != NULL) { 1578186119Sqingli ln->la_expire = 0; /* for IPv6 this means permanent */ 1579186119Sqingli ln->ln_state = ND6_LLINFO_REACHABLE; 1580186119Sqingli LLE_WUNLOCK(ln); 1581186119Sqingli } 1582186119Sqingli } 1583186119Sqingli 1584120856Sume return (error); 158553541Sshin} 158653541Sshin 1587142215Sglebiusstruct in6_multi_mship * 1588171259Sdelphijin6_joingroup(struct ifnet *ifp, struct in6_addr *addr, 1589171259Sdelphij int *errorp, int delay) 1590142215Sglebius{ 1591142215Sglebius struct in6_multi_mship *imm; 1592142215Sglebius 1593149849Sobrien imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT); 1594142215Sglebius if (!imm) { 1595142215Sglebius *errorp = ENOBUFS; 1596142215Sglebius return NULL; 1597142215Sglebius } 1598151539Ssuz imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay); 1599142215Sglebius if (!imm->i6mm_maddr) { 1600142215Sglebius /* *errorp is alrady set */ 1601149849Sobrien free(imm, M_IP6MADDR); 1602142215Sglebius return NULL; 1603142215Sglebius } 1604142215Sglebius return imm; 1605142215Sglebius} 1606142215Sglebius 1607142215Sglebiusint 1608171259Sdelphijin6_leavegroup(struct in6_multi_mship *imm) 1609142215Sglebius{ 1610142215Sglebius 1611142215Sglebius if (imm->i6mm_maddr) 1612142215Sglebius in6_delmulti(imm->i6mm_maddr); 1613149849Sobrien free(imm, M_IP6MADDR); 1614142215Sglebius return 0; 1615142215Sglebius} 1616142215Sglebius 161753541Sshin/* 161853541Sshin * Find an IPv6 interface link-local address specific to an interface. 161953541Sshin */ 162053541Sshinstruct in6_ifaddr * 1621171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 162253541Sshin{ 162378064Sume struct ifaddr *ifa; 162453541Sshin 1625120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 162653541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 162753541Sshin continue; 162862587Sitojun if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 162962587Sitojun if ((((struct in6_ifaddr *)ifa)->ia6_flags & 163062587Sitojun ignoreflags) != 0) 163162587Sitojun continue; 163253541Sshin break; 163362587Sitojun } 163453541Sshin } 163553541Sshin 1636120856Sume return ((struct in6_ifaddr *)ifa); 163753541Sshin} 163853541Sshin 163953541Sshin 164053541Sshin/* 164153541Sshin * find the internet address corresponding to a given interface and address. 164253541Sshin */ 164353541Sshinstruct in6_ifaddr * 1644171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 164553541Sshin{ 164678064Sume struct ifaddr *ifa; 164753541Sshin 1648120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 164953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 165053541Sshin continue; 165153541Sshin if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 165253541Sshin break; 165353541Sshin } 165453541Sshin 1655120856Sume return ((struct in6_ifaddr *)ifa); 165653541Sshin} 165753541Sshin 165853541Sshin/* 1659165118Sbz * Convert IP6 address to printable (loggable) representation. Caller 1660165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 166153541Sshin */ 166253541Sshinstatic char digits[] = "0123456789abcdef"; 166353541Sshinchar * 1664165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr) 166553541Sshin{ 166678064Sume int i; 166778064Sume char *cp; 1668126552Sume const u_int16_t *a = (const u_int16_t *)addr; 1669126552Sume const u_int8_t *d; 1670165287Sbz int dcolon = 0, zero = 0; 167153541Sshin 1672165118Sbz cp = ip6buf; 167353541Sshin 167453541Sshin for (i = 0; i < 8; i++) { 167553541Sshin if (dcolon == 1) { 167653541Sshin if (*a == 0) { 167753541Sshin if (i == 7) 167853541Sshin *cp++ = ':'; 167953541Sshin a++; 168053541Sshin continue; 168153541Sshin } else 168253541Sshin dcolon = 2; 168353541Sshin } 168453541Sshin if (*a == 0) { 168553541Sshin if (dcolon == 0 && *(a + 1) == 0) { 168653541Sshin if (i == 0) 168753541Sshin *cp++ = ':'; 168853541Sshin *cp++ = ':'; 168953541Sshin dcolon = 1; 169053541Sshin } else { 169153541Sshin *cp++ = '0'; 169253541Sshin *cp++ = ':'; 169353541Sshin } 169453541Sshin a++; 169553541Sshin continue; 169653541Sshin } 169791346Salfred d = (const u_char *)a; 1698165287Sbz /* Try to eliminate leading zeros in printout like in :0001. */ 1699165287Sbz zero = 1; 1700165287Sbz *cp = digits[*d >> 4]; 1701165287Sbz if (*cp != '0') { 1702165287Sbz zero = 0; 1703165287Sbz cp++; 1704165287Sbz } 1705165287Sbz *cp = digits[*d++ & 0xf]; 1706165287Sbz if (zero == 0 || (*cp != '0')) { 1707165287Sbz zero = 0; 1708165287Sbz cp++; 1709165287Sbz } 1710165287Sbz *cp = digits[*d >> 4]; 1711165287Sbz if (zero == 0 || (*cp != '0')) { 1712165287Sbz zero = 0; 1713165287Sbz cp++; 1714165287Sbz } 171553541Sshin *cp++ = digits[*d & 0xf]; 171653541Sshin *cp++ = ':'; 171753541Sshin a++; 171853541Sshin } 1719165118Sbz *--cp = '\0'; 1720165118Sbz return (ip6buf); 172153541Sshin} 172253541Sshin 172353541Sshinint 1724171259Sdelphijin6_localaddr(struct in6_addr *in6) 172553541Sshin{ 1726183550Szec INIT_VNET_INET6(curvnet); 172753541Sshin struct in6_ifaddr *ia; 172853541Sshin 172953541Sshin if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 173053541Sshin return 1; 173153541Sshin 1732181803Sbz for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) { 173353541Sshin if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1734120891Sume &ia->ia_prefixmask.sin6_addr)) { 173553541Sshin return 1; 1736120891Sume } 1737120891Sume } 173853541Sshin 173953541Sshin return (0); 174053541Sshin} 174153541Sshin 174278064Sumeint 1743171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6) 174478064Sume{ 1745183550Szec INIT_VNET_INET6(curvnet); 174678064Sume struct in6_ifaddr *ia; 174778064Sume 1748181803Sbz for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) { 174978064Sume if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 175078064Sume &sa6->sin6_addr) && 175178064Sume (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) 1752120856Sume return (1); /* true */ 175378064Sume 175478064Sume /* XXX: do we still have to go thru the rest of the list? */ 175578064Sume } 175678064Sume 1757120856Sume return (0); /* false */ 175878064Sume} 175978064Sume 176053541Sshin/* 176153541Sshin * return length of part which dst and src are equal 176253541Sshin * hard coding... 176353541Sshin */ 176453541Sshinint 1765171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst) 176653541Sshin{ 176753541Sshin int match = 0; 176853541Sshin u_char *s = (u_char *)src, *d = (u_char *)dst; 176953541Sshin u_char *lim = s + 16, r; 177053541Sshin 177153541Sshin while (s < lim) 177253541Sshin if ((r = (*d++ ^ *s++)) != 0) { 177353541Sshin while (r < 128) { 177453541Sshin match++; 177553541Sshin r <<= 1; 177653541Sshin } 177753541Sshin break; 177853541Sshin } else 177953541Sshin match += 8; 178053541Sshin return match; 178153541Sshin} 178253541Sshin 178362587Sitojun/* XXX: to be scope conscious */ 178453541Sshinint 1785171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 178653541Sshin{ 178753541Sshin int bytelen, bitlen; 178853541Sshin 178953541Sshin /* sanity check */ 179053541Sshin if (0 > len || len > 128) { 179153541Sshin log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 179253541Sshin len); 1793120856Sume return (0); 179453541Sshin } 179553541Sshin 179653541Sshin bytelen = len / 8; 179753541Sshin bitlen = len % 8; 179853541Sshin 179953541Sshin if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1800120856Sume return (0); 1801126184Scperciva if (bitlen != 0 && 1802126184Scperciva p1->s6_addr[bytelen] >> (8 - bitlen) != 180353541Sshin p2->s6_addr[bytelen] >> (8 - bitlen)) 1804120856Sume return (0); 180553541Sshin 1806120856Sume return (1); 180753541Sshin} 180853541Sshin 180953541Sshinvoid 1810171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len) 181153541Sshin{ 181253541Sshin u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 181353541Sshin int bytelen, bitlen, i; 181453541Sshin 181553541Sshin /* sanity check */ 181653541Sshin if (0 > len || len > 128) { 181753541Sshin log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 181853541Sshin len); 181953541Sshin return; 182053541Sshin } 182153541Sshin 182253541Sshin bzero(maskp, sizeof(*maskp)); 182353541Sshin bytelen = len / 8; 182453541Sshin bitlen = len % 8; 182553541Sshin for (i = 0; i < bytelen; i++) 182653541Sshin maskp->s6_addr[i] = 0xff; 182753541Sshin if (bitlen) 182853541Sshin maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 182953541Sshin} 183053541Sshin 183153541Sshin/* 183253541Sshin * return the best address out of the same scope. if no address was 183353541Sshin * found, return the first valid address from designated IF. 183453541Sshin */ 183553541Sshinstruct in6_ifaddr * 1836171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 183753541Sshin{ 1838183550Szec INIT_VNET_INET6(curvnet); 183953541Sshin int dst_scope = in6_addrscope(dst), blen = -1, tlen; 184053541Sshin struct ifaddr *ifa; 184153541Sshin struct in6_ifaddr *besta = 0; 184295023Ssuz struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 184353541Sshin 184453541Sshin dep[0] = dep[1] = NULL; 184553541Sshin 184653541Sshin /* 184753541Sshin * We first look for addresses in the same scope. 184853541Sshin * If there is one, return it. 184953541Sshin * If two or more, return one which matches the dst longest. 185053541Sshin * If none, return one of global addresses assigned other ifs. 185153541Sshin */ 1852120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 185353541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 185453541Sshin continue; 185553541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 185653541Sshin continue; /* XXX: is there any case to allow anycast? */ 185753541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 185853541Sshin continue; /* don't use this interface */ 185953541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 186053541Sshin continue; 186153541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1862181803Sbz if (V_ip6_use_deprecated) 186353541Sshin dep[0] = (struct in6_ifaddr *)ifa; 186453541Sshin continue; 186553541Sshin } 186653541Sshin 186753541Sshin if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 186853541Sshin /* 186953541Sshin * call in6_matchlen() as few as possible 187053541Sshin */ 187153541Sshin if (besta) { 187253541Sshin if (blen == -1) 187353541Sshin blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 187453541Sshin tlen = in6_matchlen(IFA_IN6(ifa), dst); 187553541Sshin if (tlen > blen) { 187653541Sshin blen = tlen; 187753541Sshin besta = (struct in6_ifaddr *)ifa; 187853541Sshin } 187953541Sshin } else 188053541Sshin besta = (struct in6_ifaddr *)ifa; 188153541Sshin } 188253541Sshin } 188353541Sshin if (besta) 1884120856Sume return (besta); 188553541Sshin 1886120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 188753541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 188853541Sshin continue; 188953541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 189053541Sshin continue; /* XXX: is there any case to allow anycast? */ 189153541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 189253541Sshin continue; /* don't use this interface */ 189353541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 189453541Sshin continue; 189553541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1896181803Sbz if (V_ip6_use_deprecated) 189753541Sshin dep[1] = (struct in6_ifaddr *)ifa; 189853541Sshin continue; 189953541Sshin } 190053541Sshin 190153541Sshin return (struct in6_ifaddr *)ifa; 190253541Sshin } 190353541Sshin 190453541Sshin /* use the last-resort values, that are, deprecated addresses */ 190553541Sshin if (dep[0]) 190653541Sshin return dep[0]; 190753541Sshin if (dep[1]) 190853541Sshin return dep[1]; 190953541Sshin 191053541Sshin return NULL; 191153541Sshin} 191253541Sshin 191353541Sshin/* 191453541Sshin * perform DAD when interface becomes IFF_UP. 191553541Sshin */ 191653541Sshinvoid 1917171259Sdelphijin6_if_up(struct ifnet *ifp) 191853541Sshin{ 191953541Sshin struct ifaddr *ifa; 192053541Sshin struct in6_ifaddr *ia; 192153541Sshin 1922120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 192353541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 192453541Sshin continue; 192553541Sshin ia = (struct in6_ifaddr *)ifa; 1926151539Ssuz if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 1927151539Ssuz /* 1928151539Ssuz * The TENTATIVE flag was likely set by hand 1929151539Ssuz * beforehand, implicitly indicating the need for DAD. 1930151539Ssuz * We may be able to skip the random delay in this 1931151539Ssuz * case, but we impose delays just in case. 1932151539Ssuz */ 1933151539Ssuz nd6_dad_start(ifa, 1934151539Ssuz arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 1935151539Ssuz } 193653541Sshin } 1937151539Ssuz 1938151539Ssuz /* 1939151539Ssuz * special cases, like 6to4, are handled in in6_ifattach 1940151539Ssuz */ 1941151539Ssuz in6_ifattach(ifp, NULL); 194253541Sshin} 194353541Sshin 194478064Sumeint 1945171259Sdelphijin6if_do_dad(struct ifnet *ifp) 194678064Sume{ 194778064Sume if ((ifp->if_flags & IFF_LOOPBACK) != 0) 1948120856Sume return (0); 194978064Sume 195078064Sume switch (ifp->if_type) { 195178064Sume#ifdef IFT_DUMMY 195278064Sume case IFT_DUMMY: 195378064Sume#endif 195478064Sume case IFT_FAITH: 195578064Sume /* 195678064Sume * These interfaces do not have the IFF_LOOPBACK flag, 195778064Sume * but loop packets back. We do not have to do DAD on such 195878064Sume * interfaces. We should even omit it, because loop-backed 195978064Sume * NS would confuse the DAD procedure. 196078064Sume */ 1961120856Sume return (0); 196278064Sume default: 196378064Sume /* 196478064Sume * Our DAD routine requires the interface up and running. 196578064Sume * However, some interfaces can be up before the RUNNING 196678064Sume * status. Additionaly, users may try to assign addresses 196778064Sume * before the interface becomes up (or running). 196878064Sume * We simply skip DAD in such a case as a work around. 196978064Sume * XXX: we should rather mark "tentative" on such addresses, 197078064Sume * and do DAD after the interface becomes ready. 197178064Sume */ 1972148887Srwatson if (!((ifp->if_flags & IFF_UP) && 1973148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 1974120856Sume return (0); 197578064Sume 1976120856Sume return (1); 197778064Sume } 197878064Sume} 197978064Sume 198053541Sshin/* 198153541Sshin * Calculate max IPv6 MTU through all the interfaces and store it 198253541Sshin * to in6_maxmtu. 198353541Sshin */ 198453541Sshinvoid 1985171259Sdelphijin6_setmaxmtu(void) 198653541Sshin{ 1987183550Szec INIT_VNET_NET(curvnet); 1988183550Szec INIT_VNET_INET6(curvnet); 198953541Sshin unsigned long maxmtu = 0; 199053541Sshin struct ifnet *ifp; 199153541Sshin 1992108172Shsu IFNET_RLOCK(); 1993181887Sjulian for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 1994181888Sjulian ifp = TAILQ_NEXT(ifp, if_list)) { 1995121283Sume /* this function can be called during ifnet initialization */ 1996121283Sume if (!ifp->if_afdata[AF_INET6]) 1997121283Sume continue; 199853541Sshin if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 1999121283Sume IN6_LINKMTU(ifp) > maxmtu) 2000121283Sume maxmtu = IN6_LINKMTU(ifp); 200153541Sshin } 2002108172Shsu IFNET_RUNLOCK(); 2003120891Sume if (maxmtu) /* update only when maxmtu is positive */ 2004181803Sbz V_in6_maxmtu = maxmtu; 200553541Sshin} 200653541Sshin 2007151539Ssuz/* 2008151539Ssuz * Provide the length of interface identifiers to be used for the link attached 2009151539Ssuz * to the given interface. The length should be defined in "IPv6 over 2010151539Ssuz * xxx-link" document. Note that address architecture might also define 2011151539Ssuz * the length for a particular set of address prefixes, regardless of the 2012151539Ssuz * link type. As clarified in rfc2462bis, those two definitions should be 2013151539Ssuz * consistent, and those really are as of August 2004. 2014151539Ssuz */ 2015151539Ssuzint 2016171259Sdelphijin6_if2idlen(struct ifnet *ifp) 2017151539Ssuz{ 2018151539Ssuz switch (ifp->if_type) { 2019151539Ssuz case IFT_ETHER: /* RFC2464 */ 2020151539Ssuz#ifdef IFT_PROPVIRTUAL 2021151539Ssuz case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2022151539Ssuz#endif 2023151539Ssuz#ifdef IFT_L2VLAN 2024151539Ssuz case IFT_L2VLAN: /* ditto */ 2025151539Ssuz#endif 2026151539Ssuz#ifdef IFT_IEEE80211 2027151539Ssuz case IFT_IEEE80211: /* ditto */ 2028151539Ssuz#endif 2029151539Ssuz#ifdef IFT_MIP 2030151539Ssuz case IFT_MIP: /* ditto */ 2031151539Ssuz#endif 2032151539Ssuz return (64); 2033151539Ssuz case IFT_FDDI: /* RFC2467 */ 2034151539Ssuz return (64); 2035151539Ssuz case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2036151539Ssuz return (64); 2037151539Ssuz case IFT_PPP: /* RFC2472 */ 2038151539Ssuz return (64); 2039151539Ssuz case IFT_ARCNET: /* RFC2497 */ 2040151539Ssuz return (64); 2041151539Ssuz case IFT_FRELAY: /* RFC2590 */ 2042151539Ssuz return (64); 2043151539Ssuz case IFT_IEEE1394: /* RFC3146 */ 2044151539Ssuz return (64); 2045151539Ssuz case IFT_GIF: 2046151539Ssuz return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2047151539Ssuz case IFT_LOOP: 2048151539Ssuz return (64); /* XXX: is this really correct? */ 2049151539Ssuz default: 2050151539Ssuz /* 2051151539Ssuz * Unknown link type: 2052151539Ssuz * It might be controversial to use the today's common constant 2053151539Ssuz * of 64 for these cases unconditionally. For full compliance, 2054151539Ssuz * we should return an error in this case. On the other hand, 2055151539Ssuz * if we simply miss the standard for the link type or a new 2056151539Ssuz * standard is defined for a new link type, the IFID length 2057151539Ssuz * is very likely to be the common constant. As a compromise, 2058151539Ssuz * we always use the constant, but make an explicit notice 2059151539Ssuz * indicating the "unknown" case. 2060151539Ssuz */ 2061151539Ssuz printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2062151539Ssuz return (64); 2063151539Ssuz } 2064151539Ssuz} 2065151539Ssuz 2066186119Sqingli#include <sys/sysctl.h> 2067186119Sqingli 2068186119Sqinglistruct in6_llentry { 2069186119Sqingli struct llentry base; 2070186119Sqingli struct sockaddr_in6 l3_addr6; 2071186119Sqingli}; 2072186119Sqingli 2073186119Sqinglistatic struct llentry * 2074186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2075186119Sqingli{ 2076186119Sqingli struct in6_llentry *lle; 2077186119Sqingli 2078186119Sqingli lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, 2079186119Sqingli M_DONTWAIT | M_ZERO); 2080186119Sqingli if (lle == NULL) /* NB: caller generates msg */ 2081186119Sqingli return NULL; 2082186119Sqingli 2083186119Sqingli callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE); 2084186119Sqingli lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2085186119Sqingli lle->base.lle_refcnt = 1; 2086186119Sqingli LLE_LOCK_INIT(&lle->base); 2087186119Sqingli return &lle->base; 2088186119Sqingli} 2089186119Sqingli 2090186119Sqingli/* 2091186119Sqingli * Deletes an address from the address table. 2092186119Sqingli * This function is called by the timer functions 2093186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and 2094186119Sqingli * the caller does the locking. 2095186119Sqingli */ 2096186119Sqinglistatic void 2097186119Sqingliin6_lltable_free(struct lltable *llt, struct llentry *lle) 2098186119Sqingli{ 2099186150Skmacy LLE_WUNLOCK(lle); 2100186150Skmacy LLE_LOCK_DESTROY(lle); 2101186119Sqingli free(lle, M_LLTABLE); 2102186119Sqingli} 2103186119Sqingli 2104186119Sqinglistatic int 2105186119Sqingliin6_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 2106186119Sqingli{ 2107186119Sqingli struct rtentry *rt; 2108186119Sqingli char ip6buf[INET6_ADDRSTRLEN]; 2109186119Sqingli 2110186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2111186119Sqingli ("sin_family %d", l3addr->sa_family)); 2112186119Sqingli 2113186119Sqingli /* XXX rtalloc1 should take a const param */ 2114186119Sqingli rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 2115186119Sqingli if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2116186119Sqingli struct ifaddr *ifa; 2117186119Sqingli /* 2118186119Sqingli * Create an ND6 cache for an IPv6 neighbor 2119186119Sqingli * that is not covered by our own prefix. 2120186119Sqingli */ 2121186119Sqingli /* XXX ifaof_ifpforaddr should take a const param */ 2122186119Sqingli ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2123186119Sqingli if (ifa != NULL) { 2124186119Sqingli if (rt != NULL) 2125187946Sbz RTFREE_LOCKED(rt); 2126186119Sqingli return 0; 2127186119Sqingli } 2128186119Sqingli log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2129186119Sqingli ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2130186119Sqingli if (rt != NULL) 2131187946Sbz RTFREE_LOCKED(rt); 2132186119Sqingli return EINVAL; 2133186119Sqingli } 2134187946Sbz RTFREE_LOCKED(rt); 2135186119Sqingli return 0; 2136186119Sqingli} 2137186119Sqingli 2138186119Sqinglistatic struct llentry * 2139186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags, 2140186119Sqingli const struct sockaddr *l3addr) 2141186119Sqingli{ 2142186119Sqingli const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2143186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2144186119Sqingli struct llentry *lle; 2145186119Sqingli struct llentries *lleh; 2146186119Sqingli u_int hashkey; 2147186119Sqingli 2148186119Sqingli IF_AFDATA_LOCK_ASSERT(ifp); 2149186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2150186119Sqingli ("sin_family %d", l3addr->sa_family)); 2151186119Sqingli 2152186119Sqingli hashkey = sin6->sin6_addr.s6_addr32[3]; 2153186119Sqingli lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2154186119Sqingli LIST_FOREACH(lle, lleh, lle_next) { 2155186708Sqingli struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle); 2156186119Sqingli if (lle->la_flags & LLE_DELETED) 2157186119Sqingli continue; 2158186708Sqingli if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr, 2159186708Sqingli sizeof(struct in6_addr)) == 0) 2160186119Sqingli break; 2161186119Sqingli } 2162186119Sqingli 2163186119Sqingli if (lle == NULL) { 2164186119Sqingli if (!(flags & LLE_CREATE)) 2165186119Sqingli return (NULL); 2166186119Sqingli /* 2167186119Sqingli * A route that covers the given address must have 2168186119Sqingli * been installed 1st because we are doing a resolution, 2169186119Sqingli * verify this. 2170186119Sqingli */ 2171186119Sqingli if (!(flags & LLE_IFADDR) && 2172186119Sqingli in6_lltable_rtcheck(ifp, l3addr) != 0) 2173186119Sqingli return NULL; 2174186119Sqingli 2175186119Sqingli lle = in6_lltable_new(l3addr, flags); 2176186119Sqingli if (lle == NULL) { 2177186119Sqingli log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2178186119Sqingli return NULL; 2179186119Sqingli } 2180186119Sqingli lle->la_flags = flags & ~LLE_CREATE; 2181186119Sqingli if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2182186119Sqingli bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2183186119Sqingli lle->la_flags |= (LLE_VALID | LLE_STATIC); 2184186119Sqingli } 2185186119Sqingli 2186186119Sqingli lle->lle_tbl = llt; 2187186119Sqingli lle->lle_head = lleh; 2188186119Sqingli LIST_INSERT_HEAD(lleh, lle, lle_next); 2189186119Sqingli } else if (flags & LLE_DELETE) { 2190186392Sqingli if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 2191186392Sqingli LLE_WLOCK(lle); 2192186392Sqingli lle->la_flags = LLE_DELETED; 2193186392Sqingli LLE_WUNLOCK(lle); 2194186119Sqingli#ifdef DIAGNOSTICS 2195186392Sqingli log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2196186119Sqingli#endif 2197186392Sqingli } 2198186119Sqingli lle = (void *)-1; 2199186119Sqingli } 2200186119Sqingli if (LLE_IS_VALID(lle)) { 2201186119Sqingli if (flags & LLE_EXCLUSIVE) 2202186119Sqingli LLE_WLOCK(lle); 2203186119Sqingli else 2204186119Sqingli LLE_RLOCK(lle); 2205186119Sqingli } 2206186119Sqingli return (lle); 2207186119Sqingli} 2208186119Sqingli 2209186119Sqinglistatic int 2210186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2211186119Sqingli{ 2212186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2213186119Sqingli struct llentry *lle; 2214186119Sqingli /* XXX stack use */ 2215186119Sqingli struct { 2216186119Sqingli struct rt_msghdr rtm; 2217186119Sqingli struct sockaddr_in6 sin6; 2218186119Sqingli /* 2219186119Sqingli * ndp.c assumes that sdl is word aligned 2220186119Sqingli */ 2221186119Sqingli#ifdef __LP64__ 2222186119Sqingli uint32_t pad; 2223186119Sqingli#endif 2224186119Sqingli struct sockaddr_dl sdl; 2225186119Sqingli } ndpc; 2226186119Sqingli int i, error; 2227186119Sqingli 2228186119Sqingli /* XXXXX 2229186119Sqingli * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 2230186119Sqingli * so it is okay to use this ASSERT, change it when 2231186119Sqingli * IFNET lock is finalized 2232186119Sqingli */ 2233186119Sqingli IFNET_WLOCK_ASSERT(); 2234186119Sqingli 2235186119Sqingli error = 0; 2236186119Sqingli for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2237186119Sqingli LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2238186119Sqingli struct sockaddr_dl *sdl; 2239186119Sqingli 2240186119Sqingli /* skip deleted or invalid entries */ 2241186119Sqingli if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2242186119Sqingli continue; 2243186980Sbz /* Skip if jailed and not a valid IP of the prison. */ 2244188144Sjamie if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 2245186980Sbz continue; 2246186119Sqingli /* 2247186119Sqingli * produce a msg made of: 2248186119Sqingli * struct rt_msghdr; 2249186119Sqingli * struct sockaddr_in6 (IPv6) 2250186119Sqingli * struct sockaddr_dl; 2251186119Sqingli */ 2252186119Sqingli bzero(&ndpc, sizeof(ndpc)); 2253186119Sqingli ndpc.rtm.rtm_msglen = sizeof(ndpc); 2254187094Sqingli ndpc.rtm.rtm_version = RTM_VERSION; 2255187094Sqingli ndpc.rtm.rtm_type = RTM_GET; 2256187094Sqingli ndpc.rtm.rtm_flags = RTF_UP; 2257187094Sqingli ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2258186119Sqingli ndpc.sin6.sin6_family = AF_INET6; 2259186119Sqingli ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2260186119Sqingli bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2261186119Sqingli 2262186119Sqingli /* publish */ 2263186119Sqingli if (lle->la_flags & LLE_PUB) 2264186119Sqingli ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2265186119Sqingli 2266186119Sqingli sdl = &ndpc.sdl; 2267186119Sqingli sdl->sdl_family = AF_LINK; 2268186119Sqingli sdl->sdl_len = sizeof(*sdl); 2269186119Sqingli sdl->sdl_alen = ifp->if_addrlen; 2270186119Sqingli sdl->sdl_index = ifp->if_index; 2271186119Sqingli sdl->sdl_type = ifp->if_type; 2272186119Sqingli bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2273186119Sqingli ndpc.rtm.rtm_rmx.rmx_expire = 2274186119Sqingli lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2275186500Sqingli ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2276186119Sqingli if (lle->la_flags & LLE_STATIC) 2277186119Sqingli ndpc.rtm.rtm_flags |= RTF_STATIC; 2278186119Sqingli ndpc.rtm.rtm_index = ifp->if_index; 2279186119Sqingli error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2280186119Sqingli if (error) 2281186119Sqingli break; 2282186119Sqingli } 2283186119Sqingli } 2284186119Sqingli return error; 2285186119Sqingli} 2286186119Sqingli 2287121161Sumevoid * 2288171259Sdelphijin6_domifattach(struct ifnet *ifp) 2289121161Sume{ 2290121161Sume struct in6_ifextra *ext; 2291121161Sume 2292121161Sume ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2293121161Sume bzero(ext, sizeof(*ext)); 2294121161Sume 2295121161Sume ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2296121161Sume M_IFADDR, M_WAITOK); 2297121161Sume bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2298121161Sume 2299121161Sume ext->icmp6_ifstat = 2300121161Sume (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2301121161Sume M_IFADDR, M_WAITOK); 2302121161Sume bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2303121161Sume 2304121161Sume ext->nd_ifinfo = nd6_ifattach(ifp); 2305121161Sume ext->scope6_id = scope6_ifattach(ifp); 2306186119Sqingli ext->lltable = lltable_init(ifp, AF_INET6); 2307186119Sqingli if (ext->lltable != NULL) { 2308186119Sqingli ext->lltable->llt_new = in6_lltable_new; 2309186119Sqingli ext->lltable->llt_free = in6_lltable_free; 2310186119Sqingli ext->lltable->llt_rtcheck = in6_lltable_rtcheck; 2311186119Sqingli ext->lltable->llt_lookup = in6_lltable_lookup; 2312186119Sqingli ext->lltable->llt_dump = in6_lltable_dump; 2313186119Sqingli } 2314121161Sume return ext; 2315121161Sume} 2316121161Sume 2317121161Sumevoid 2318171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux) 2319121161Sume{ 2320121161Sume struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2321121161Sume 2322121161Sume scope6_ifdetach(ext->scope6_id); 2323121161Sume nd6_ifdetach(ext->nd_ifinfo); 2324186119Sqingli lltable_free(ext->lltable); 2325121161Sume free(ext->in6_ifstat, M_IFADDR); 2326121161Sume free(ext->icmp6_ifstat, M_IFADDR); 2327121161Sume free(ext, M_IFADDR); 2328121161Sume} 2329121161Sume 233053541Sshin/* 233195023Ssuz * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 233253541Sshin * v4 mapped addr or v4 compat addr 233353541Sshin */ 233453541Sshinvoid 233553541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 233653541Sshin{ 2337171259Sdelphij 233853541Sshin bzero(sin, sizeof(*sin)); 233953541Sshin sin->sin_len = sizeof(struct sockaddr_in); 234053541Sshin sin->sin_family = AF_INET; 234153541Sshin sin->sin_port = sin6->sin6_port; 2342120891Sume sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 234353541Sshin} 234453541Sshin 234553541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 234653541Sshinvoid 234753541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 234853541Sshin{ 234953541Sshin bzero(sin6, sizeof(*sin6)); 235053541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 235153541Sshin sin6->sin6_family = AF_INET6; 235253541Sshin sin6->sin6_port = sin->sin_port; 235353541Sshin sin6->sin6_addr.s6_addr32[0] = 0; 235453541Sshin sin6->sin6_addr.s6_addr32[1] = 0; 235553541Sshin sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 235653541Sshin sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 235753541Sshin} 235853541Sshin 235953541Sshin/* Convert sockaddr_in6 into sockaddr_in. */ 236053541Sshinvoid 236153541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam) 236253541Sshin{ 236353541Sshin struct sockaddr_in *sin_p; 236453541Sshin struct sockaddr_in6 sin6; 236553541Sshin 236653541Sshin /* 236753541Sshin * Save original sockaddr_in6 addr and convert it 236853541Sshin * to sockaddr_in. 236953541Sshin */ 237053541Sshin sin6 = *(struct sockaddr_in6 *)nam; 237153541Sshin sin_p = (struct sockaddr_in *)nam; 237253541Sshin in6_sin6_2_sin(sin_p, &sin6); 237353541Sshin} 237453541Sshin 237553541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 237653541Sshinvoid 237753541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 237853541Sshin{ 237953541Sshin struct sockaddr_in *sin_p; 238053541Sshin struct sockaddr_in6 *sin6_p; 238153541Sshin 2382184205Sdes sin6_p = malloc(sizeof *sin6_p, M_SONAME, 2383111119Simp M_WAITOK); 238453541Sshin sin_p = (struct sockaddr_in *)*nam; 238553541Sshin in6_sin_2_v4mapsin6(sin_p, sin6_p); 2386184205Sdes free(*nam, M_SONAME); 238753541Sshin *nam = (struct sockaddr *)sin6_p; 238853541Sshin} 2389