in6.c revision 186119
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 186119 2008-12-15 06:10:57Z qingli $"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 6862587Sitojun 6953541Sshin#include <sys/param.h> 7053541Sshin#include <sys/errno.h> 7153541Sshin#include <sys/malloc.h> 7253541Sshin#include <sys/socket.h> 7353541Sshin#include <sys/socketvar.h> 7453541Sshin#include <sys/sockio.h> 7553541Sshin#include <sys/systm.h> 76164033Srwatson#include <sys/priv.h> 7753541Sshin#include <sys/proc.h> 7853541Sshin#include <sys/time.h> 7953541Sshin#include <sys/kernel.h> 8053541Sshin#include <sys/syslog.h> 81181803Sbz#include <sys/vimage.h> 8253541Sshin 8353541Sshin#include <net/if.h> 8453541Sshin#include <net/if_types.h> 8553541Sshin#include <net/route.h> 8653541Sshin#include <net/if_dl.h> 87185571Sbz#include <net/vnet.h> 8853541Sshin 8953541Sshin#include <netinet/in.h> 9053541Sshin#include <netinet/in_var.h> 91186119Sqingli#include <net/if_llatbl.h> 9253541Sshin#include <netinet/if_ether.h> 9378064Sume#include <netinet/in_systm.h> 9478064Sume#include <netinet/ip.h> 9578064Sume#include <netinet/in_pcb.h> 9653541Sshin 9762587Sitojun#include <netinet/ip6.h> 9853541Sshin#include <netinet6/ip6_var.h> 9995023Ssuz#include <netinet6/nd6.h> 10053541Sshin#include <netinet6/mld6_var.h> 10162587Sitojun#include <netinet6/ip6_mroute.h> 10253541Sshin#include <netinet6/in6_ifattach.h> 10362587Sitojun#include <netinet6/scope6_var.h> 10478064Sume#include <netinet6/in6_pcb.h> 105185571Sbz#include <netinet6/vinet6.h> 10662587Sitojun 107149849SobrienMALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address"); 10853541Sshin 10953541Sshin/* 11053541Sshin * Definitions of some costant IP6 addresses. 11153541Sshin */ 11262587Sitojunconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 11362587Sitojunconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 11462587Sitojunconst struct in6_addr in6addr_nodelocal_allnodes = 11553541Sshin IN6ADDR_NODELOCAL_ALLNODES_INIT; 11662587Sitojunconst struct in6_addr in6addr_linklocal_allnodes = 11753541Sshin IN6ADDR_LINKLOCAL_ALLNODES_INIT; 11862587Sitojunconst struct in6_addr in6addr_linklocal_allrouters = 11953541Sshin IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 12053541Sshin 12162587Sitojunconst struct in6_addr in6mask0 = IN6MASK0; 12262587Sitojunconst struct in6_addr in6mask32 = IN6MASK32; 12362587Sitojunconst struct in6_addr in6mask64 = IN6MASK64; 12462587Sitojunconst struct in6_addr in6mask96 = IN6MASK96; 12562587Sitojunconst struct in6_addr in6mask128 = IN6MASK128; 12653541Sshin 127126552Sumeconst struct sockaddr_in6 sa6_any = 128126552Sume { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 }; 12978064Sume 13062587Sitojunstatic int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 13183366Sjulian struct ifnet *, struct thread *)); 13278064Sumestatic int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *, 133120891Sume struct sockaddr_in6 *, int)); 134175162Sobrienstatic void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 13553541Sshin 13662587Sitojunstruct in6_multihead in6_multihead; /* XXX BSS initialization */ 13783934Sbrooksint (*faithprefix_p)(struct in6_addr *); 13883934Sbrooks 139120891Sume 14078064Sume 14153541Sshinint 142171259Sdelphijin6_mask2len(struct in6_addr *mask, u_char *lim0) 14353541Sshin{ 14478064Sume int x = 0, y; 14578064Sume u_char *lim = lim0, *p; 14653541Sshin 147120891Sume /* ignore the scope_id part */ 148120891Sume if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 14978064Sume lim = (u_char *)mask + sizeof(*mask); 15078064Sume for (p = (u_char *)mask; p < lim; x++, p++) { 15178064Sume if (*p != 0xff) 15253541Sshin break; 15353541Sshin } 15453541Sshin y = 0; 15578064Sume if (p < lim) { 15653541Sshin for (y = 0; y < 8; y++) { 15778064Sume if ((*p & (0x80 >> y)) == 0) 15853541Sshin break; 15953541Sshin } 16053541Sshin } 16178064Sume 16278064Sume /* 16378064Sume * when the limit pointer is given, do a stricter check on the 16478064Sume * remaining bits. 16578064Sume */ 16678064Sume if (p < lim) { 16778064Sume if (y != 0 && (*p & (0x00ff >> y)) != 0) 168120856Sume return (-1); 16978064Sume for (p = p + 1; p < lim; p++) 17078064Sume if (*p != 0) 171120856Sume return (-1); 17278064Sume } 173120891Sume 17453541Sshin return x * 8 + y; 17553541Sshin} 17653541Sshin 17753541Sshin#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 17862587Sitojun#define ia62ifa(ia6) (&((ia6)->ia_ifa)) 17953541Sshin 18053541Sshinint 181171259Sdelphijin6_control(struct socket *so, u_long cmd, caddr_t data, 182171259Sdelphij struct ifnet *ifp, struct thread *td) 18353541Sshin{ 184183550Szec INIT_VNET_INET6(curvnet); 18553541Sshin struct in6_ifreq *ifr = (struct in6_ifreq *)data; 18678064Sume struct in6_ifaddr *ia = NULL; 18753541Sshin struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 188151539Ssuz struct sockaddr_in6 *sa6; 189164033Srwatson int error; 19053541Sshin 19162587Sitojun switch (cmd) { 19262587Sitojun case SIOCGETSGCNT_IN6: 19362587Sitojun case SIOCGETMIFCNT_IN6: 194166938Sbms return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP); 19562587Sitojun } 19653541Sshin 197121742Sume switch(cmd) { 198121742Sume case SIOCAADDRCTL_POLICY: 199121742Sume case SIOCDADDRCTL_POLICY: 200164033Srwatson if (td != NULL) { 201164033Srwatson error = priv_check(td, PRIV_NETINET_ADDRCTRL6); 202164033Srwatson if (error) 203164033Srwatson return (error); 204164033Srwatson } 205121742Sume return (in6_src_ioctl(cmd, data)); 206121742Sume } 207121742Sume 20862587Sitojun if (ifp == NULL) 209120856Sume return (EOPNOTSUPP); 21053541Sshin 21153541Sshin switch (cmd) { 21253541Sshin case SIOCSNDFLUSH_IN6: 21353541Sshin case SIOCSPFXFLUSH_IN6: 21453541Sshin case SIOCSRTRFLUSH_IN6: 21562587Sitojun case SIOCSDEFIFACE_IN6: 21662587Sitojun case SIOCSIFINFO_FLAGS: 217164033Srwatson if (td != NULL) { 218164033Srwatson error = priv_check(td, PRIV_NETINET_ND6); 219164033Srwatson if (error) 220164033Srwatson return (error); 221164033Srwatson } 222120891Sume /* FALLTHROUGH */ 22378064Sume case OSIOCGIFINFO_IN6: 22453541Sshin case SIOCGIFINFO_IN6: 225151468Ssuz case SIOCSIFINFO_IN6: 22653541Sshin case SIOCGDRLST_IN6: 22753541Sshin case SIOCGPRLST_IN6: 22853541Sshin case SIOCGNBRINFO_IN6: 22962587Sitojun case SIOCGDEFIFACE_IN6: 230120856Sume return (nd6_ioctl(cmd, data, ifp)); 23153541Sshin } 23253541Sshin 23353541Sshin switch (cmd) { 23453541Sshin case SIOCSIFPREFIX_IN6: 23553541Sshin case SIOCDIFPREFIX_IN6: 23653541Sshin case SIOCAIFPREFIX_IN6: 23753541Sshin case SIOCCIFPREFIX_IN6: 23853541Sshin case SIOCSGIFPREFIX_IN6: 23953541Sshin case SIOCGIFPREFIX_IN6: 24078064Sume log(LOG_NOTICE, 24178064Sume "prefix ioctls are now invalidated. " 24278064Sume "please use ifconfig.\n"); 243120856Sume return (EOPNOTSUPP); 24453541Sshin } 24553541Sshin 24695023Ssuz switch (cmd) { 24762587Sitojun case SIOCSSCOPE6: 248164033Srwatson if (td != NULL) { 249164033Srwatson error = priv_check(td, PRIV_NETINET_SCOPE6); 250164033Srwatson if (error) 251164033Srwatson return (error); 252164033Srwatson } 253121161Sume return (scope6_set(ifp, 254121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 25562587Sitojun case SIOCGSCOPE6: 256121161Sume return (scope6_get(ifp, 257121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 25862587Sitojun case SIOCGSCOPE6DEF: 259121161Sume return (scope6_get_default((struct scope6_id *) 260121161Sume ifr->ifr_ifru.ifru_scope_id)); 26162587Sitojun } 26262587Sitojun 26353541Sshin switch (cmd) { 26453541Sshin case SIOCALIFADDR: 265175630Sbz if (td != NULL) { 266175630Sbz error = priv_check(td, PRIV_NET_ADDIFADDR); 267175630Sbz if (error) 268175630Sbz return (error); 269175630Sbz } 270175630Sbz return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 271175630Sbz 27253541Sshin case SIOCDLIFADDR: 273164033Srwatson if (td != NULL) { 274175630Sbz error = priv_check(td, PRIV_NET_DELIFADDR); 275164033Srwatson if (error) 276164033Srwatson return (error); 277164033Srwatson } 278120891Sume /* FALLTHROUGH */ 27953541Sshin case SIOCGLIFADDR: 28083366Sjulian return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 28153541Sshin } 28253541Sshin 28353541Sshin /* 28453541Sshin * Find address for this interface, if it exists. 285151539Ssuz * 286151539Ssuz * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 287151539Ssuz * only, and used the first interface address as the target of other 288151539Ssuz * operations (without checking ifra_addr). This was because netinet 289151539Ssuz * code/API assumed at most 1 interface address per interface. 290151539Ssuz * Since IPv6 allows a node to assign multiple addresses 291151539Ssuz * on a single interface, we almost always look and check the 292151539Ssuz * presence of ifra_addr, and reject invalid ones here. 293151539Ssuz * It also decreases duplicated code among SIOC*_IN6 operations. 29453541Sshin */ 295151539Ssuz switch (cmd) { 296151539Ssuz case SIOCAIFADDR_IN6: 297151539Ssuz case SIOCSIFPHYADDR_IN6: 298151539Ssuz sa6 = &ifra->ifra_addr; 299151539Ssuz break; 300151539Ssuz case SIOCSIFADDR_IN6: 301151539Ssuz case SIOCGIFADDR_IN6: 302151539Ssuz case SIOCSIFDSTADDR_IN6: 303151539Ssuz case SIOCSIFNETMASK_IN6: 304151539Ssuz case SIOCGIFDSTADDR_IN6: 305151539Ssuz case SIOCGIFNETMASK_IN6: 306151539Ssuz case SIOCDIFADDR_IN6: 307151539Ssuz case SIOCGIFPSRCADDR_IN6: 308151539Ssuz case SIOCGIFPDSTADDR_IN6: 309151539Ssuz case SIOCGIFAFLAG_IN6: 310151539Ssuz case SIOCSNDFLUSH_IN6: 311151539Ssuz case SIOCSPFXFLUSH_IN6: 312151539Ssuz case SIOCSRTRFLUSH_IN6: 313151539Ssuz case SIOCGIFALIFETIME_IN6: 314151539Ssuz case SIOCSIFALIFETIME_IN6: 315151539Ssuz case SIOCGIFSTAT_IN6: 316151539Ssuz case SIOCGIFSTAT_ICMP6: 317151539Ssuz sa6 = &ifr->ifr_addr; 318151539Ssuz break; 319151539Ssuz default: 320151539Ssuz sa6 = NULL; 321151539Ssuz break; 322151539Ssuz } 323151539Ssuz if (sa6 && sa6->sin6_family == AF_INET6) { 324148385Sume int error = 0; 32553541Sshin 326151539Ssuz if (sa6->sin6_scope_id != 0) 327151539Ssuz error = sa6_embedscope(sa6, 0); 328148385Sume else 329151539Ssuz error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 330148385Sume if (error != 0) 331148385Sume return (error); 332151539Ssuz ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 333151539Ssuz } else 334151539Ssuz ia = NULL; 33553541Sshin 33653541Sshin switch (cmd) { 33778064Sume case SIOCSIFADDR_IN6: 33878064Sume case SIOCSIFDSTADDR_IN6: 33978064Sume case SIOCSIFNETMASK_IN6: 34078064Sume /* 34178064Sume * Since IPv6 allows a node to assign multiple addresses 342151465Ssuz * on a single interface, SIOCSIFxxx ioctls are deprecated. 34378064Sume */ 34478064Sume /* we decided to obsolete this command (20000704) */ 345120856Sume return (EINVAL); 34653541Sshin 34753541Sshin case SIOCDIFADDR_IN6: 34862587Sitojun /* 34978064Sume * for IPv4, we look for existing in_ifaddr here to allow 350151465Ssuz * "ifconfig if0 delete" to remove the first IPv4 address on 351151465Ssuz * the interface. For IPv6, as the spec allows multiple 352151465Ssuz * interface address from the day one, we consider "remove the 353151465Ssuz * first one" semantics to be not preferable. 35462587Sitojun */ 35562587Sitojun if (ia == NULL) 356120856Sume return (EADDRNOTAVAIL); 35753541Sshin /* FALLTHROUGH */ 35853541Sshin case SIOCAIFADDR_IN6: 35962587Sitojun /* 36078064Sume * We always require users to specify a valid IPv6 address for 36178064Sume * the corresponding operation. 36262587Sitojun */ 36378064Sume if (ifra->ifra_addr.sin6_family != AF_INET6 || 36478064Sume ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) 365120856Sume return (EAFNOSUPPORT); 36653541Sshin 367164033Srwatson if (td != NULL) { 368175630Sbz error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ? 369175630Sbz PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 370164033Srwatson if (error) 371164033Srwatson return (error); 372164033Srwatson } 373164033Srwatson 37453541Sshin break; 37553541Sshin 37653541Sshin case SIOCGIFADDR_IN6: 37753541Sshin /* This interface is basically deprecated. use SIOCGIFCONF. */ 378120891Sume /* FALLTHROUGH */ 37953541Sshin case SIOCGIFAFLAG_IN6: 38053541Sshin case SIOCGIFNETMASK_IN6: 38153541Sshin case SIOCGIFDSTADDR_IN6: 38253541Sshin case SIOCGIFALIFETIME_IN6: 38353541Sshin /* must think again about its semantics */ 38462587Sitojun if (ia == NULL) 385120856Sume return (EADDRNOTAVAIL); 38653541Sshin break; 38753541Sshin case SIOCSIFALIFETIME_IN6: 38853541Sshin { 38953541Sshin struct in6_addrlifetime *lt; 39053541Sshin 391164033Srwatson if (td != NULL) { 392164033Srwatson error = priv_check(td, PRIV_NETINET_ALIFETIME6); 393164033Srwatson if (error) 394164033Srwatson return (error); 395164033Srwatson } 39662587Sitojun if (ia == NULL) 397120856Sume return (EADDRNOTAVAIL); 39853541Sshin /* sanity for overflow - beware unsigned */ 39953541Sshin lt = &ifr->ifr_ifru.ifru_lifetime; 400126552Sume if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME && 401126552Sume lt->ia6t_vltime + time_second < time_second) { 40253541Sshin return EINVAL; 40353541Sshin } 404126552Sume if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME && 405126552Sume lt->ia6t_pltime + time_second < time_second) { 40653541Sshin return EINVAL; 40753541Sshin } 40853541Sshin break; 40953541Sshin } 41053541Sshin } 41153541Sshin 41253541Sshin switch (cmd) { 41353541Sshin 41453541Sshin case SIOCGIFADDR_IN6: 41553541Sshin ifr->ifr_addr = ia->ia_addr; 416148385Sume if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 417148385Sume return (error); 41853541Sshin break; 41953541Sshin 42053541Sshin case SIOCGIFDSTADDR_IN6: 42153541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 422120856Sume return (EINVAL); 42362587Sitojun /* 42462587Sitojun * XXX: should we check if ifa_dstaddr is NULL and return 42562587Sitojun * an error? 42662587Sitojun */ 42753541Sshin ifr->ifr_dstaddr = ia->ia_dstaddr; 428148385Sume if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 429148385Sume return (error); 43053541Sshin break; 43153541Sshin 43253541Sshin case SIOCGIFNETMASK_IN6: 43353541Sshin ifr->ifr_addr = ia->ia_prefixmask; 43453541Sshin break; 43553541Sshin 43653541Sshin case SIOCGIFAFLAG_IN6: 43753541Sshin ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 43853541Sshin break; 43953541Sshin 44053541Sshin case SIOCGIFSTAT_IN6: 441121167Sume if (ifp == NULL) 442121167Sume return EINVAL; 443121161Sume bzero(&ifr->ifr_ifru.ifru_stat, 444121161Sume sizeof(ifr->ifr_ifru.ifru_stat)); 445121161Sume ifr->ifr_ifru.ifru_stat = 446121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; 44753541Sshin break; 44853541Sshin 44953541Sshin case SIOCGIFSTAT_ICMP6: 45053541Sshin if (ifp == NULL) 45153541Sshin return EINVAL; 452155454Sgnn bzero(&ifr->ifr_ifru.ifru_icmp6stat, 453121161Sume sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 454121161Sume ifr->ifr_ifru.ifru_icmp6stat = 455121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; 45653541Sshin break; 45753541Sshin 45853541Sshin case SIOCGIFALIFETIME_IN6: 45953541Sshin ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 460151539Ssuz if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 461151539Ssuz time_t maxexpire; 462151539Ssuz struct in6_addrlifetime *retlt = 463151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 464151539Ssuz 465151539Ssuz /* 466151539Ssuz * XXX: adjust expiration time assuming time_t is 467151539Ssuz * signed. 468151539Ssuz */ 469151539Ssuz maxexpire = (-1) & 470151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 471151539Ssuz if (ia->ia6_lifetime.ia6t_vltime < 472151539Ssuz maxexpire - ia->ia6_updatetime) { 473151539Ssuz retlt->ia6t_expire = ia->ia6_updatetime + 474151539Ssuz ia->ia6_lifetime.ia6t_vltime; 475151539Ssuz } else 476151539Ssuz retlt->ia6t_expire = maxexpire; 477151539Ssuz } 478151539Ssuz if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 479151539Ssuz time_t maxexpire; 480151539Ssuz struct in6_addrlifetime *retlt = 481151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 482151539Ssuz 483151539Ssuz /* 484151539Ssuz * XXX: adjust expiration time assuming time_t is 485151539Ssuz * signed. 486151539Ssuz */ 487151539Ssuz maxexpire = (-1) & 488151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 489151539Ssuz if (ia->ia6_lifetime.ia6t_pltime < 490151539Ssuz maxexpire - ia->ia6_updatetime) { 491151539Ssuz retlt->ia6t_preferred = ia->ia6_updatetime + 492151539Ssuz ia->ia6_lifetime.ia6t_pltime; 493151539Ssuz } else 494151539Ssuz retlt->ia6t_preferred = maxexpire; 495151539Ssuz } 49653541Sshin break; 49753541Sshin 49853541Sshin case SIOCSIFALIFETIME_IN6: 49953541Sshin ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 50053541Sshin /* for sanity */ 50153541Sshin if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 50253541Sshin ia->ia6_lifetime.ia6t_expire = 50353541Sshin time_second + ia->ia6_lifetime.ia6t_vltime; 50453541Sshin } else 50553541Sshin ia->ia6_lifetime.ia6t_expire = 0; 50653541Sshin if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 50753541Sshin ia->ia6_lifetime.ia6t_preferred = 50853541Sshin time_second + ia->ia6_lifetime.ia6t_pltime; 50953541Sshin } else 51053541Sshin ia->ia6_lifetime.ia6t_preferred = 0; 51153541Sshin break; 51253541Sshin 51378064Sume case SIOCAIFADDR_IN6: 51478064Sume { 51578064Sume int i, error = 0; 516151539Ssuz struct nd_prefixctl pr0; 517151539Ssuz struct nd_prefix *pr; 51878064Sume 51962587Sitojun /* 52078064Sume * first, make or update the interface address structure, 52178064Sume * and link it to the list. 52262587Sitojun */ 523151539Ssuz if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) 524120856Sume return (error); 525151915Ssuz if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) 526151915Ssuz == NULL) { 527171260Sdelphij /* 528151915Ssuz * this can happen when the user specify the 0 valid 529151915Ssuz * lifetime. 530151915Ssuz */ 531151915Ssuz break; 532151915Ssuz } 53353541Sshin 53478064Sume /* 53578064Sume * then, make the prefix on-link on the interface. 53678064Sume * XXX: we'd rather create the prefix before the address, but 53778064Sume * we need at least one address to install the corresponding 53878064Sume * interface route, so we configure the address first. 53978064Sume */ 54078064Sume 54178064Sume /* 54278064Sume * convert mask to prefix length (prefixmask has already 54378064Sume * been validated in in6_update_ifa(). 54478064Sume */ 54578064Sume bzero(&pr0, sizeof(pr0)); 54678064Sume pr0.ndpr_ifp = ifp; 54778064Sume pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 548120891Sume NULL); 549120891Sume if (pr0.ndpr_plen == 128) { 55078064Sume break; /* we don't need to install a host route. */ 551120891Sume } 55278064Sume pr0.ndpr_prefix = ifra->ifra_addr; 55378064Sume /* apply the mask for safety. */ 55478064Sume for (i = 0; i < 4; i++) { 55578064Sume pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 556120891Sume ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 55778064Sume } 55878064Sume /* 55995023Ssuz * XXX: since we don't have an API to set prefix (not address) 56095023Ssuz * lifetimes, we just use the same lifetimes as addresses. 56195023Ssuz * The (temporarily) installed lifetimes can be overridden by 56295023Ssuz * later advertised RAs (when accept_rtadv is non 0), which is 56395023Ssuz * an intended behavior. 56478064Sume */ 56578064Sume pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 56678064Sume pr0.ndpr_raf_auto = 567120891Sume ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 56878064Sume pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 56978064Sume pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 57078064Sume 571120891Sume /* add the prefix if not yet. */ 57278064Sume if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 57378064Sume /* 57478064Sume * nd6_prelist_add will install the corresponding 57578064Sume * interface route. 57678064Sume */ 57778064Sume if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 578120856Sume return (error); 57978064Sume if (pr == NULL) { 580120891Sume log(LOG_ERR, "nd6_prelist_add succeeded but " 58178064Sume "no prefix\n"); 582120856Sume return (EINVAL); /* XXX panic here? */ 58378064Sume } 58478064Sume } 58578064Sume 586151915Ssuz /* relate the address to the prefix */ 587151915Ssuz if (ia->ia6_ndpr == NULL) { 588151915Ssuz ia->ia6_ndpr = pr; 589151915Ssuz pr->ndpr_refcnt++; 59078064Sume 59178064Sume /* 592151915Ssuz * If this is the first autoconf address from the 593151915Ssuz * prefix, create a temporary address as well 594151915Ssuz * (when required). 59578064Sume */ 596151915Ssuz if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 597181803Sbz V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) { 598151915Ssuz int e; 599151915Ssuz if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { 600151915Ssuz log(LOG_NOTICE, "in6_control: failed " 601151915Ssuz "to create a temporary address, " 602151915Ssuz "errno=%d\n", e); 603151915Ssuz } 604151915Ssuz } 60562587Sitojun } 606151915Ssuz 607151915Ssuz /* 608151915Ssuz * this might affect the status of autoconfigured addresses, 609151915Ssuz * that is, this address might make other addresses detached. 610151915Ssuz */ 611151915Ssuz pfxlist_onlink_check(); 612126264Smlaier if (error == 0 && ia) 613126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 61478064Sume break; 61578064Sume } 61662587Sitojun 61778064Sume case SIOCDIFADDR_IN6: 61878064Sume { 619151539Ssuz struct nd_prefix *pr; 62078064Sume 62178064Sume /* 62278064Sume * If the address being deleted is the only one that owns 62378064Sume * the corresponding prefix, expire the prefix as well. 624120891Sume * XXX: theoretically, we don't have to worry about such 62578064Sume * relationship, since we separate the address management 62678064Sume * and the prefix management. We do this, however, to provide 62778064Sume * as much backward compatibility as possible in terms of 62878064Sume * the ioctl operation. 629151915Ssuz * Note that in6_purgeaddr() will decrement ndpr_refcnt. 63078064Sume */ 631151915Ssuz pr = ia->ia6_ndpr; 63278064Sume in6_purgeaddr(&ia->ia_ifa); 633151915Ssuz if (pr && pr->ndpr_refcnt == 0) 634151915Ssuz prelist_remove(pr); 635126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 63653541Sshin break; 63778064Sume } 63853541Sshin 63978064Sume default: 64078064Sume if (ifp == NULL || ifp->if_ioctl == 0) 641120856Sume return (EOPNOTSUPP); 642120856Sume return ((*ifp->if_ioctl)(ifp, cmd, data)); 64378064Sume } 64453541Sshin 645120856Sume return (0); 64678064Sume} 64753541Sshin 64878064Sume/* 64978064Sume * Update parameters of an IPv6 interface address. 65078064Sume * If necessary, a new entry is created and linked into address chains. 65178064Sume * This function is separated from in6_control(). 65278064Sume * XXX: should this be performed under splnet()? 65378064Sume */ 65478064Sumeint 655171259Sdelphijin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 656171259Sdelphij struct in6_ifaddr *ia, int flags) 65778064Sume{ 658183550Szec INIT_VNET_INET6(ifp->if_vnet); 659183550Szec INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */ 66078064Sume int error = 0, hostIsNew = 0, plen = -1; 66178064Sume struct in6_ifaddr *oia; 66278064Sume struct sockaddr_in6 dst6; 66378064Sume struct in6_addrlifetime *lt; 664151539Ssuz struct in6_multi_mship *imm; 665151539Ssuz struct in6_multi *in6m_sol; 666148385Sume struct rtentry *rt; 667151539Ssuz int delay; 668165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 66978064Sume 67078064Sume /* Validate parameters */ 67178064Sume if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 672120856Sume return (EINVAL); 67378064Sume 67478064Sume /* 67578064Sume * The destination address for a p2p link must have a family 67678064Sume * of AF_UNSPEC or AF_INET6. 67778064Sume */ 67878064Sume if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 67978064Sume ifra->ifra_dstaddr.sin6_family != AF_INET6 && 68078064Sume ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 681120856Sume return (EAFNOSUPPORT); 68278064Sume /* 68378064Sume * validate ifra_prefixmask. don't check sin6_family, netmask 68478064Sume * does not carry fields other than sin6_len. 68578064Sume */ 68678064Sume if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 687120856Sume return (EINVAL); 68878064Sume /* 68978064Sume * Because the IPv6 address architecture is classless, we require 69078064Sume * users to specify a (non 0) prefix length (mask) for a new address. 69178064Sume * We also require the prefix (when specified) mask is valid, and thus 69278064Sume * reject a non-consecutive mask. 69378064Sume */ 69478064Sume if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 695120856Sume return (EINVAL); 69678064Sume if (ifra->ifra_prefixmask.sin6_len != 0) { 69778064Sume plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 698120891Sume (u_char *)&ifra->ifra_prefixmask + 699120891Sume ifra->ifra_prefixmask.sin6_len); 70078064Sume if (plen <= 0) 701120856Sume return (EINVAL); 702120891Sume } else { 70362587Sitojun /* 70495023Ssuz * In this case, ia must not be NULL. We just use its prefix 70578064Sume * length. 70662587Sitojun */ 70778064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 70878064Sume } 70978064Sume /* 71078064Sume * If the destination address on a p2p interface is specified, 71178064Sume * and the address is a scoped one, validate/set the scope 71278064Sume * zone identifier. 71378064Sume */ 71478064Sume dst6 = ifra->ifra_dstaddr; 715120891Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 71678064Sume (dst6.sin6_family == AF_INET6)) { 717148385Sume struct in6_addr in6_tmp; 718126552Sume u_int32_t zoneid; 71978064Sume 720148385Sume in6_tmp = dst6.sin6_addr; 721148385Sume if (in6_setscope(&in6_tmp, ifp, &zoneid)) 722148385Sume return (EINVAL); /* XXX: should be impossible */ 723148385Sume 724148385Sume if (dst6.sin6_scope_id != 0) { 725148385Sume if (dst6.sin6_scope_id != zoneid) 726148385Sume return (EINVAL); 727148385Sume } else /* user omit to specify the ID. */ 728126552Sume dst6.sin6_scope_id = zoneid; 729148385Sume 730148385Sume /* convert into the internal form */ 731148385Sume if (sa6_embedscope(&dst6, 0)) 732148385Sume return (EINVAL); /* XXX: should be impossible */ 73378064Sume } 73478064Sume /* 73578064Sume * The destination address can be specified only for a p2p or a 73678064Sume * loopback interface. If specified, the corresponding prefix length 73778064Sume * must be 128. 73878064Sume */ 73978064Sume if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 74078064Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 741126552Sume /* XXX: noisy message */ 742122059Sume nd6log((LOG_INFO, "in6_update_ifa: a destination can " 743122059Sume "be specified for a p2p or a loopback IF only\n")); 744120856Sume return (EINVAL); 74578064Sume } 74678064Sume if (plen != 128) { 747122059Sume nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 748122059Sume "be 128 when dstaddr is specified\n")); 749120856Sume return (EINVAL); 75078064Sume } 75178064Sume } 75278064Sume /* lifetime consistency check */ 75378064Sume lt = &ifra->ifra_lifetime; 754151539Ssuz if (lt->ia6t_pltime > lt->ia6t_vltime) 755151539Ssuz return (EINVAL); 75678064Sume if (lt->ia6t_vltime == 0) { 75762587Sitojun /* 75878064Sume * the following log might be noisy, but this is a typical 75978064Sume * configuration mistake or a tool's bug. 76062587Sitojun */ 761122059Sume nd6log((LOG_INFO, 76278064Sume "in6_update_ifa: valid lifetime is 0 for %s\n", 763165118Sbz ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 764151539Ssuz 765151539Ssuz if (ia == NULL) 766151539Ssuz return (0); /* there's nothing to do */ 76778064Sume } 76862587Sitojun 76978064Sume /* 77078064Sume * If this is a new address, allocate a new ifaddr and link it 77178064Sume * into chains. 77278064Sume */ 77378064Sume if (ia == NULL) { 77478064Sume hostIsNew = 1; 77579763Sume /* 77679763Sume * When in6_update_ifa() is called in a process of a received 777120891Sume * RA, it is called under an interrupt context. So, we should 778120891Sume * call malloc with M_NOWAIT. 77979763Sume */ 780120891Sume ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, 781120891Sume M_NOWAIT); 78278064Sume if (ia == NULL) 78378064Sume return (ENOBUFS); 78478064Sume bzero((caddr_t)ia, sizeof(*ia)); 785170202Sjinmei LIST_INIT(&ia->ia6_memberships); 786151539Ssuz /* Initialize the address and masks, and put time stamp */ 787108033Shsu IFA_LOCK_INIT(&ia->ia_ifa); 78878064Sume ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 78978064Sume ia->ia_addr.sin6_family = AF_INET6; 79078064Sume ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 791151539Ssuz ia->ia6_createtime = time_second; 79278064Sume if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 79378064Sume /* 79478064Sume * XXX: some functions expect that ifa_dstaddr is not 79578064Sume * NULL for p2p interfaces. 79678064Sume */ 797120891Sume ia->ia_ifa.ifa_dstaddr = 798120891Sume (struct sockaddr *)&ia->ia_dstaddr; 79978064Sume } else { 80078064Sume ia->ia_ifa.ifa_dstaddr = NULL; 80153541Sshin } 802108033Shsu ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 80378064Sume 80478064Sume ia->ia_ifp = ifp; 805181803Sbz if ((oia = V_in6_ifaddr) != NULL) { 80678064Sume for ( ; oia->ia_next; oia = oia->ia_next) 80778064Sume continue; 80878064Sume oia->ia_next = ia; 80978064Sume } else 810181803Sbz V_in6_ifaddr = ia; 81178064Sume 812108033Shsu ia->ia_ifa.ifa_refcnt = 1; 813108033Shsu TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 81478064Sume } 81578064Sume 816151539Ssuz /* update timestamp */ 817151539Ssuz ia->ia6_updatetime = time_second; 818151539Ssuz 81978064Sume /* set prefix mask */ 82078064Sume if (ifra->ifra_prefixmask.sin6_len) { 82178064Sume /* 82278064Sume * We prohibit changing the prefix length of an existing 82378064Sume * address, because 82478064Sume * + such an operation should be rare in IPv6, and 82578064Sume * + the operation would confuse prefix management. 82678064Sume */ 82778064Sume if (ia->ia_prefixmask.sin6_len && 82878064Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 829122059Sume nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an" 83078064Sume " existing (%s) address should not be changed\n", 831165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 83278064Sume error = EINVAL; 83378064Sume goto unlink; 83453541Sshin } 83578064Sume ia->ia_prefixmask = ifra->ifra_prefixmask; 83678064Sume } 83778064Sume 83878064Sume /* 83978064Sume * If a new destination address is specified, scrub the old one and 84078064Sume * install the new destination. Note that the interface must be 841120891Sume * p2p or loopback (see the check above.) 84278064Sume */ 84378064Sume if (dst6.sin6_family == AF_INET6 && 844120891Sume !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 84578064Sume int e; 84678064Sume 84778064Sume if ((ia->ia_flags & IFA_ROUTE) != 0 && 848120891Sume (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 849122059Sume nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 85078064Sume "a route to the old destination: %s\n", 851165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 85278064Sume /* proceed anyway... */ 853120891Sume } else 85478064Sume ia->ia_flags &= ~IFA_ROUTE; 85578064Sume ia->ia_dstaddr = dst6; 85678064Sume } 85753541Sshin 858148385Sume /* 859148385Sume * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 860148385Sume * to see if the address is deprecated or invalidated, but initialize 861148385Sume * these members for applications. 862148385Sume */ 863148385Sume ia->ia6_lifetime = ifra->ifra_lifetime; 864148385Sume if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 865148385Sume ia->ia6_lifetime.ia6t_expire = 866148385Sume time_second + ia->ia6_lifetime.ia6t_vltime; 867148385Sume } else 868148385Sume ia->ia6_lifetime.ia6t_expire = 0; 869148385Sume if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 870148385Sume ia->ia6_lifetime.ia6t_preferred = 871148385Sume time_second + ia->ia6_lifetime.ia6t_pltime; 872148385Sume } else 873148385Sume ia->ia6_lifetime.ia6t_preferred = 0; 874148385Sume 87578064Sume /* reset the interface and routing table appropriately. */ 87678064Sume if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 87778064Sume goto unlink; 87878064Sume 87978064Sume /* 880148385Sume * configure address flags. 881148385Sume */ 882148385Sume ia->ia6_flags = ifra->ifra_flags; 883148385Sume /* 884148385Sume * backward compatibility - if IN6_IFF_DEPRECATED is set from the 885148385Sume * userland, make it deprecated. 886148385Sume */ 887148385Sume if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 888148385Sume ia->ia6_lifetime.ia6t_pltime = 0; 889148385Sume ia->ia6_lifetime.ia6t_preferred = time_second; 890148385Sume } 891148385Sume /* 892151539Ssuz * Make the address tentative before joining multicast addresses, 893151539Ssuz * so that corresponding MLD responses would not have a tentative 894151539Ssuz * source address. 895148385Sume */ 896151539Ssuz ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 897151539Ssuz if (hostIsNew && in6if_do_dad(ifp)) 898148385Sume ia->ia6_flags |= IN6_IFF_TENTATIVE; 899148385Sume 900148385Sume /* 901148385Sume * We are done if we have simply modified an existing address. 902148385Sume */ 903148385Sume if (!hostIsNew) 904148385Sume return (error); 905148385Sume 906148385Sume /* 90778064Sume * Beyond this point, we should call in6_purgeaddr upon an error, 908120891Sume * not just go to unlink. 90978064Sume */ 91078064Sume 911151465Ssuz /* Join necessary multicast groups */ 912151539Ssuz in6m_sol = NULL; 91378064Sume if ((ifp->if_flags & IFF_MULTICAST) != 0) { 91478064Sume struct sockaddr_in6 mltaddr, mltmask; 915148385Sume struct in6_addr llsol; 91678064Sume 917148385Sume /* join solicited multicast addr for new host id */ 918148385Sume bzero(&llsol, sizeof(struct in6_addr)); 919151465Ssuz llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL; 920148385Sume llsol.s6_addr32[1] = 0; 921148385Sume llsol.s6_addr32[2] = htonl(1); 922148385Sume llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; 923148385Sume llsol.s6_addr8[12] = 0xff; 924148385Sume if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 925148385Sume /* XXX: should not happen */ 926148385Sume log(LOG_ERR, "in6_update_ifa: " 927148385Sume "in6_setscope failed\n"); 928148385Sume goto cleanup; 92953541Sshin } 930151539Ssuz delay = 0; 931151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 932151539Ssuz /* 933151539Ssuz * We need a random delay for DAD on the address 934151539Ssuz * being configured. It also means delaying 935151539Ssuz * transmission of the corresponding MLD report to 936151539Ssuz * avoid report collision. 937151539Ssuz * [draft-ietf-ipv6-rfc2462bis-02.txt] 938151539Ssuz */ 939151539Ssuz delay = arc4random() % 940151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 941151539Ssuz } 942151539Ssuz imm = in6_joingroup(ifp, &llsol, &error, delay); 943170202Sjinmei if (imm == NULL) { 944148385Sume nd6log((LOG_WARNING, 945148385Sume "in6_update_ifa: addmulti failed for " 946148385Sume "%s on %s (errno=%d)\n", 947165118Sbz ip6_sprintf(ip6buf, &llsol), if_name(ifp), 948148385Sume error)); 949151539Ssuz in6_purgeaddr((struct ifaddr *)ia); 950151539Ssuz return (error); 951148385Sume } 952170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 953170202Sjinmei imm, i6mm_chain); 954151539Ssuz in6m_sol = imm->i6mm_maddr; 95553541Sshin 95678064Sume bzero(&mltmask, sizeof(mltmask)); 95778064Sume mltmask.sin6_len = sizeof(struct sockaddr_in6); 95878064Sume mltmask.sin6_family = AF_INET6; 95978064Sume mltmask.sin6_addr = in6mask32; 960151465Ssuz#define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 96153541Sshin 96253541Sshin /* 96378064Sume * join link-local all-nodes address 96471207Sitojun */ 96578064Sume bzero(&mltaddr, sizeof(mltaddr)); 96678064Sume mltaddr.sin6_len = sizeof(struct sockaddr_in6); 96778064Sume mltaddr.sin6_family = AF_INET6; 96878064Sume mltaddr.sin6_addr = in6addr_linklocal_allnodes; 969148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 970148385Sume 0) 971148385Sume goto cleanup; /* XXX: should not fail */ 97271207Sitojun 973148385Sume /* 974148385Sume * XXX: do we really need this automatic routes? 975148385Sume * We should probably reconsider this stuff. Most applications 976148385Sume * actually do not need the routes, since they usually specify 977148385Sume * the outgoing interface. 978148385Sume */ 979148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 980148385Sume if (rt) { 981174375Sjulian /* XXX: only works in !SCOPEDROUTING case. */ 982148385Sume if (memcmp(&mltaddr.sin6_addr, 983148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 984151465Ssuz MLTMASK_LEN)) { 985148385Sume RTFREE_LOCKED(rt); 986148385Sume rt = NULL; 987148385Sume } 988148385Sume } 989148385Sume if (!rt) { 990148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 991148385Sume (struct sockaddr *)&ia->ia_addr, 992186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 993148385Sume (struct rtentry **)0); 994148385Sume if (error) 995148385Sume goto cleanup; 996151539Ssuz } else { 997151539Ssuz RTFREE_LOCKED(rt); 998151539Ssuz } 99978064Sume 1000151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1001151539Ssuz if (!imm) { 1002151539Ssuz nd6log((LOG_WARNING, 1003151539Ssuz "in6_update_ifa: addmulti failed for " 1004151539Ssuz "%s on %s (errno=%d)\n", 1005165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1006151539Ssuz if_name(ifp), error)); 1007151539Ssuz goto cleanup; 1008151539Ssuz } 1009170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1010151539Ssuz 101171207Sitojun /* 101278064Sume * join node information group address 101353541Sshin */ 1014181803Sbz#define hostnamelen strlen(V_hostname) 1015151539Ssuz delay = 0; 1016151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1017151539Ssuz /* 1018151539Ssuz * The spec doesn't say anything about delay for this 1019151539Ssuz * group, but the same logic should apply. 1020151539Ssuz */ 1021151539Ssuz delay = arc4random() % 1022151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 1023151539Ssuz } 1024180291Srwatson mtx_lock(&hostname_mtx); 1025181803Sbz if (in6_nigroup(ifp, V_hostname, hostnamelen, 1026181803Sbz &mltaddr.sin6_addr) == 0) { 1027180291Srwatson mtx_unlock(&hostname_mtx); 1028151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 1029151539Ssuz delay); /* XXX jinmei */ 1030151539Ssuz if (!imm) { 1031151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1032151539Ssuz "addmulti failed for %s on %s " 1033151539Ssuz "(errno=%d)\n", 1034165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1035151539Ssuz if_name(ifp), error)); 1036151539Ssuz /* XXX not very fatal, go on... */ 1037170202Sjinmei } else { 1038170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 1039170202Sjinmei imm, i6mm_chain); 104062587Sitojun } 1041180291Srwatson } else 1042180291Srwatson mtx_unlock(&hostname_mtx); 104378064Sume#undef hostnamelen 104453541Sshin 104578064Sume /* 1046148385Sume * join interface-local all-nodes address. 1047148385Sume * (ff01::1%ifN, and ff01::%ifN/32) 104878064Sume */ 1049148385Sume mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1050148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) 1051148385Sume != 0) 1052148385Sume goto cleanup; /* XXX: should not fail */ 1053148385Sume /* XXX: again, do we really need the route? */ 1054148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1055148385Sume if (rt) { 1056148385Sume if (memcmp(&mltaddr.sin6_addr, 1057148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1058151465Ssuz MLTMASK_LEN)) { 1059148385Sume RTFREE_LOCKED(rt); 1060148385Sume rt = NULL; 1061148385Sume } 1062148385Sume } 1063148385Sume if (!rt) { 1064148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1065148385Sume (struct sockaddr *)&ia->ia_addr, 1066186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1067148385Sume (struct rtentry **)0); 1068148385Sume if (error) 1069148385Sume goto cleanup; 1070148385Sume } else 1071148385Sume RTFREE_LOCKED(rt); 107281115Sume 1073151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1074151539Ssuz if (!imm) { 1075151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1076151539Ssuz "addmulti failed for %s on %s " 1077151539Ssuz "(errno=%d)\n", 1078165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1079151539Ssuz if_name(ifp), error)); 1080151539Ssuz goto cleanup; 1081151539Ssuz } 1082170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1083151465Ssuz#undef MLTMASK_LEN 108478064Sume } 108553541Sshin 1086151539Ssuz /* 1087151539Ssuz * Perform DAD, if needed. 1088151539Ssuz * XXX It may be of use, if we can administratively 1089151539Ssuz * disable DAD. 1090151539Ssuz */ 1091151539Ssuz if (hostIsNew && in6if_do_dad(ifp) && 1092151539Ssuz ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1093151539Ssuz (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1094151539Ssuz { 1095151539Ssuz int mindelay, maxdelay; 1096151539Ssuz 1097151539Ssuz delay = 0; 1098151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1099151539Ssuz /* 1100151539Ssuz * We need to impose a delay before sending an NS 1101151539Ssuz * for DAD. Check if we also needed a delay for the 1102151539Ssuz * corresponding MLD message. If we did, the delay 1103151539Ssuz * should be larger than the MLD delay (this could be 1104151539Ssuz * relaxed a bit, but this simple logic is at least 1105151539Ssuz * safe). 1106151539Ssuz */ 1107151539Ssuz mindelay = 0; 1108151539Ssuz if (in6m_sol != NULL && 1109151539Ssuz in6m_sol->in6m_state == MLD_REPORTPENDING) { 1110151539Ssuz mindelay = in6m_sol->in6m_timer; 1111151539Ssuz } 1112151539Ssuz maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1113151539Ssuz if (maxdelay - mindelay == 0) 1114151539Ssuz delay = 0; 1115151539Ssuz else { 1116151539Ssuz delay = 1117151539Ssuz (arc4random() % (maxdelay - mindelay)) + 1118151539Ssuz mindelay; 1119151539Ssuz } 1120151539Ssuz } 1121151539Ssuz nd6_dad_start((struct ifaddr *)ia, delay); 1122151539Ssuz } 1123151539Ssuz 1124120856Sume return (error); 112578064Sume 112678064Sume unlink: 112778064Sume /* 112878064Sume * XXX: if a change of an existing address failed, keep the entry 112978064Sume * anyway. 113078064Sume */ 113178064Sume if (hostIsNew) 113278064Sume in6_unlink_ifa(ia, ifp); 1133120856Sume return (error); 1134148385Sume 1135148385Sume cleanup: 1136148385Sume in6_purgeaddr(&ia->ia_ifa); 1137148385Sume return error; 113853541Sshin} 113953541Sshin 114062587Sitojunvoid 1141171259Sdelphijin6_purgeaddr(struct ifaddr *ifa) 114262587Sitojun{ 114378064Sume struct ifnet *ifp = ifa->ifa_ifp; 114478064Sume struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1145170202Sjinmei struct in6_multi_mship *imm; 114662587Sitojun 114778064Sume /* stop DAD processing */ 114878064Sume nd6_dad_stop(ifa); 114962587Sitojun 1150186119Sqingli IF_AFDATA_LOCK(ifp); 1151186119Sqingli lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR), 1152186119Sqingli (struct sockaddr *)&ia->ia_addr); 1153186119Sqingli IF_AFDATA_UNLOCK(ifp); 1154186119Sqingli 115578064Sume /* 1156170202Sjinmei * leave from multicast groups we have joined for the interface 1157170202Sjinmei */ 1158170202Sjinmei while ((imm = ia->ia6_memberships.lh_first) != NULL) { 1159170202Sjinmei LIST_REMOVE(imm, i6mm_chain); 1160170202Sjinmei in6_leavegroup(imm); 116162587Sitojun } 116262587Sitojun 116378064Sume in6_unlink_ifa(ia, ifp); 116478064Sume} 116578064Sume 116678064Sumestatic void 1167171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 116878064Sume{ 1169183550Szec INIT_VNET_INET6(ifp->if_vnet); 117078064Sume struct in6_ifaddr *oia; 117178064Sume int s = splnet(); 117278064Sume 117362587Sitojun TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 117462587Sitojun 117562587Sitojun oia = ia; 1176181803Sbz if (oia == (ia = V_in6_ifaddr)) 1177181803Sbz V_in6_ifaddr = ia->ia_next; 117862587Sitojun else { 117962587Sitojun while (ia->ia_next && (ia->ia_next != oia)) 118062587Sitojun ia = ia->ia_next; 118162587Sitojun if (ia->ia_next) 118262587Sitojun ia->ia_next = oia->ia_next; 118378064Sume else { 118478064Sume /* search failed */ 118578064Sume printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n"); 118678064Sume } 118762587Sitojun } 118862587Sitojun 118962587Sitojun /* 1190151915Ssuz * Release the reference to the base prefix. There should be a 1191151915Ssuz * positive reference. 119262587Sitojun */ 1193151915Ssuz if (oia->ia6_ndpr == NULL) { 1194151915Ssuz nd6log((LOG_NOTICE, 1195151915Ssuz "in6_unlink_ifa: autoconf'ed address " 1196151915Ssuz "%p has no prefix\n", oia)); 1197151915Ssuz } else { 1198151915Ssuz oia->ia6_ndpr->ndpr_refcnt--; 1199151915Ssuz oia->ia6_ndpr = NULL; 1200151915Ssuz } 120162587Sitojun 1202151915Ssuz /* 1203151915Ssuz * Also, if the address being removed is autoconf'ed, call 1204151915Ssuz * pfxlist_onlink_check() since the release might affect the status of 1205171260Sdelphij * other (detached) addresses. 1206151915Ssuz */ 1207151915Ssuz if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) { 120878064Sume pfxlist_onlink_check(); 120962587Sitojun } 121078064Sume 121178064Sume /* 121278064Sume * release another refcnt for the link from in6_ifaddr. 121378064Sume * Note that we should decrement the refcnt at least once for all *BSD. 121478064Sume */ 121562587Sitojun IFAFREE(&oia->ia_ifa); 121678064Sume 121778064Sume splx(s); 121862587Sitojun} 121962587Sitojun 122078064Sumevoid 1221171259Sdelphijin6_purgeif(struct ifnet *ifp) 122278064Sume{ 122378064Sume struct ifaddr *ifa, *nifa; 122478064Sume 1225120891Sume for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) { 122678064Sume nifa = TAILQ_NEXT(ifa, ifa_list); 122778064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 122878064Sume continue; 122978064Sume in6_purgeaddr(ifa); 123078064Sume } 123178064Sume 123278064Sume in6_ifdetach(ifp); 123378064Sume} 123478064Sume 123553541Sshin/* 123653541Sshin * SIOC[GAD]LIFADDR. 123762744Sgrog * SIOCGLIFADDR: get first address. (?) 123853541Sshin * SIOCGLIFADDR with IFLR_PREFIX: 123953541Sshin * get first address that matches the specified prefix. 124053541Sshin * SIOCALIFADDR: add the specified address. 124153541Sshin * SIOCALIFADDR with IFLR_PREFIX: 124253541Sshin * add the specified prefix, filling hostid part from 124353541Sshin * the first link-local address. prefixlen must be <= 64. 124453541Sshin * SIOCDLIFADDR: delete the specified address. 124553541Sshin * SIOCDLIFADDR with IFLR_PREFIX: 124653541Sshin * delete the first address that matches the specified prefix. 124753541Sshin * return values: 124853541Sshin * EINVAL on invalid parameters 124953541Sshin * EADDRNOTAVAIL on prefix match failed/specified address not found 125053541Sshin * other values may be returned from in6_ioctl() 125153541Sshin * 125253541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 125353541Sshin * this is to accomodate address naming scheme other than RFC2374, 125453541Sshin * in the future. 125553541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 125653541Sshin * address encoding scheme. (see figure on page 8) 125753541Sshin */ 125853541Sshinstatic int 1259171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1260171259Sdelphij struct ifnet *ifp, struct thread *td) 126153541Sshin{ 126253541Sshin struct if_laddrreq *iflr = (struct if_laddrreq *)data; 126353541Sshin struct ifaddr *ifa; 126462587Sitojun struct sockaddr *sa; 126553541Sshin 126653541Sshin /* sanity checks */ 126753541Sshin if (!data || !ifp) { 126853541Sshin panic("invalid argument to in6_lifaddr_ioctl"); 1269120891Sume /* NOTREACHED */ 127053541Sshin } 127153541Sshin 127253541Sshin switch (cmd) { 127353541Sshin case SIOCGLIFADDR: 127453541Sshin /* address must be specified on GET with IFLR_PREFIX */ 127553541Sshin if ((iflr->flags & IFLR_PREFIX) == 0) 127653541Sshin break; 127795023Ssuz /* FALLTHROUGH */ 127853541Sshin case SIOCALIFADDR: 127953541Sshin case SIOCDLIFADDR: 128053541Sshin /* address must be specified on ADD and DELETE */ 128162587Sitojun sa = (struct sockaddr *)&iflr->addr; 128262587Sitojun if (sa->sa_family != AF_INET6) 128353541Sshin return EINVAL; 128462587Sitojun if (sa->sa_len != sizeof(struct sockaddr_in6)) 128553541Sshin return EINVAL; 128653541Sshin /* XXX need improvement */ 128762587Sitojun sa = (struct sockaddr *)&iflr->dstaddr; 128862587Sitojun if (sa->sa_family && sa->sa_family != AF_INET6) 128953541Sshin return EINVAL; 129062587Sitojun if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 129153541Sshin return EINVAL; 129253541Sshin break; 129395023Ssuz default: /* shouldn't happen */ 129462587Sitojun#if 0 129562587Sitojun panic("invalid cmd to in6_lifaddr_ioctl"); 129695023Ssuz /* NOTREACHED */ 129762587Sitojun#else 129853541Sshin return EOPNOTSUPP; 129962587Sitojun#endif 130053541Sshin } 130153541Sshin if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 130253541Sshin return EINVAL; 130353541Sshin 130453541Sshin switch (cmd) { 130553541Sshin case SIOCALIFADDR: 130653541Sshin { 130753541Sshin struct in6_aliasreq ifra; 130853541Sshin struct in6_addr *hostid = NULL; 130953541Sshin int prefixlen; 131053541Sshin 131153541Sshin if ((iflr->flags & IFLR_PREFIX) != 0) { 131253541Sshin struct sockaddr_in6 *sin6; 131353541Sshin 131453541Sshin /* 131553541Sshin * hostid is to fill in the hostid part of the 131653541Sshin * address. hostid points to the first link-local 131753541Sshin * address attached to the interface. 131853541Sshin */ 131962587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 132053541Sshin if (!ifa) 132153541Sshin return EADDRNOTAVAIL; 132253541Sshin hostid = IFA_IN6(ifa); 132353541Sshin 1324171260Sdelphij /* prefixlen must be <= 64. */ 132553541Sshin if (64 < iflr->prefixlen) 132653541Sshin return EINVAL; 132753541Sshin prefixlen = iflr->prefixlen; 132853541Sshin 132953541Sshin /* hostid part must be zero. */ 133053541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 1331126552Sume if (sin6->sin6_addr.s6_addr32[2] != 0 || 1332126552Sume sin6->sin6_addr.s6_addr32[3] != 0) { 133353541Sshin return EINVAL; 133453541Sshin } 133553541Sshin } else 133653541Sshin prefixlen = iflr->prefixlen; 133753541Sshin 133853541Sshin /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 133953541Sshin bzero(&ifra, sizeof(ifra)); 1340120891Sume bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 134153541Sshin 134262587Sitojun bcopy(&iflr->addr, &ifra.ifra_addr, 1343120891Sume ((struct sockaddr *)&iflr->addr)->sa_len); 134453541Sshin if (hostid) { 134553541Sshin /* fill in hostid part */ 134653541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1347120891Sume hostid->s6_addr32[2]; 134853541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1349120891Sume hostid->s6_addr32[3]; 135053541Sshin } 135153541Sshin 1352120891Sume if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 135353541Sshin bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1354120891Sume ((struct sockaddr *)&iflr->dstaddr)->sa_len); 135553541Sshin if (hostid) { 135653541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1357120891Sume hostid->s6_addr32[2]; 135853541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1359120891Sume hostid->s6_addr32[3]; 136053541Sshin } 136153541Sshin } 136253541Sshin 136353541Sshin ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1364121168Sume in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 136553541Sshin 136653541Sshin ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 136783366Sjulian return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 136853541Sshin } 136953541Sshin case SIOCGLIFADDR: 137053541Sshin case SIOCDLIFADDR: 137153541Sshin { 137253541Sshin struct in6_ifaddr *ia; 137353541Sshin struct in6_addr mask, candidate, match; 137453541Sshin struct sockaddr_in6 *sin6; 137553541Sshin int cmp; 137653541Sshin 137753541Sshin bzero(&mask, sizeof(mask)); 137853541Sshin if (iflr->flags & IFLR_PREFIX) { 137953541Sshin /* lookup a prefix rather than address. */ 1380121168Sume in6_prefixlen2mask(&mask, iflr->prefixlen); 138153541Sshin 138253541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 138353541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 138453541Sshin match.s6_addr32[0] &= mask.s6_addr32[0]; 138553541Sshin match.s6_addr32[1] &= mask.s6_addr32[1]; 138653541Sshin match.s6_addr32[2] &= mask.s6_addr32[2]; 138753541Sshin match.s6_addr32[3] &= mask.s6_addr32[3]; 138853541Sshin 138953541Sshin /* if you set extra bits, that's wrong */ 139053541Sshin if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 139153541Sshin return EINVAL; 139253541Sshin 139353541Sshin cmp = 1; 139453541Sshin } else { 139553541Sshin if (cmd == SIOCGLIFADDR) { 139653541Sshin /* on getting an address, take the 1st match */ 139795023Ssuz cmp = 0; /* XXX */ 139853541Sshin } else { 139953541Sshin /* on deleting an address, do exact match */ 1400121168Sume in6_prefixlen2mask(&mask, 128); 140153541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 140253541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 140353541Sshin 140453541Sshin cmp = 1; 140553541Sshin } 140653541Sshin } 140753541Sshin 1408120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 140953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 141053541Sshin continue; 141153541Sshin if (!cmp) 141253541Sshin break; 141378064Sume 141478064Sume /* 141578064Sume * XXX: this is adhoc, but is necessary to allow 141678064Sume * a user to specify fe80::/64 (not /10) for a 141778064Sume * link-local address. 141878064Sume */ 1419148385Sume bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1420148385Sume in6_clearscope(&candidate); 142153541Sshin candidate.s6_addr32[0] &= mask.s6_addr32[0]; 142253541Sshin candidate.s6_addr32[1] &= mask.s6_addr32[1]; 142353541Sshin candidate.s6_addr32[2] &= mask.s6_addr32[2]; 142453541Sshin candidate.s6_addr32[3] &= mask.s6_addr32[3]; 142553541Sshin if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 142653541Sshin break; 142753541Sshin } 142853541Sshin if (!ifa) 142953541Sshin return EADDRNOTAVAIL; 143053541Sshin ia = ifa2ia6(ifa); 143153541Sshin 143253541Sshin if (cmd == SIOCGLIFADDR) { 1433148385Sume int error; 143478064Sume 143553541Sshin /* fill in the if_laddrreq structure */ 143653541Sshin bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1437148385Sume error = sa6_recoverscope( 1438148385Sume (struct sockaddr_in6 *)&iflr->addr); 1439148385Sume if (error != 0) 1440148385Sume return (error); 1441148385Sume 144253541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 144353541Sshin bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1444120891Sume ia->ia_dstaddr.sin6_len); 1445148385Sume error = sa6_recoverscope( 1446148385Sume (struct sockaddr_in6 *)&iflr->dstaddr); 1447148385Sume if (error != 0) 1448148385Sume return (error); 144953541Sshin } else 145053541Sshin bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 145153541Sshin 145253541Sshin iflr->prefixlen = 1453120891Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 145453541Sshin 145595023Ssuz iflr->flags = ia->ia6_flags; /* XXX */ 145653541Sshin 145753541Sshin return 0; 145853541Sshin } else { 145953541Sshin struct in6_aliasreq ifra; 146053541Sshin 146153541Sshin /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 146253541Sshin bzero(&ifra, sizeof(ifra)); 146353541Sshin bcopy(iflr->iflr_name, ifra.ifra_name, 1464120891Sume sizeof(ifra.ifra_name)); 146553541Sshin 146653541Sshin bcopy(&ia->ia_addr, &ifra.ifra_addr, 1467120891Sume ia->ia_addr.sin6_len); 146853541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 146953541Sshin bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1470120891Sume ia->ia_dstaddr.sin6_len); 147162587Sitojun } else { 147262587Sitojun bzero(&ifra.ifra_dstaddr, 147362587Sitojun sizeof(ifra.ifra_dstaddr)); 147453541Sshin } 147553541Sshin bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1476120891Sume ia->ia_prefixmask.sin6_len); 147753541Sshin 147853541Sshin ifra.ifra_flags = ia->ia6_flags; 147953541Sshin return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1480120891Sume ifp, td); 148153541Sshin } 148253541Sshin } 148353541Sshin } 148453541Sshin 148595023Ssuz return EOPNOTSUPP; /* just for safety */ 148653541Sshin} 148753541Sshin 148853541Sshin/* 148953541Sshin * Initialize an interface's intetnet6 address 149053541Sshin * and routing table entry. 149153541Sshin */ 149278064Sumestatic int 1493171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1494171259Sdelphij struct sockaddr_in6 *sin6, int newhost) 149553541Sshin{ 149678064Sume int error = 0, plen, ifacount = 0; 149753541Sshin int s = splimp(); 149878064Sume struct ifaddr *ifa; 149953541Sshin 150053541Sshin /* 150153541Sshin * Give the interface a chance to initialize 150253541Sshin * if this is its first address, 150353541Sshin * and to validate the address if necessary. 150453541Sshin */ 1505120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 150678064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 150778064Sume continue; 150878064Sume ifacount++; 150978064Sume } 151078064Sume 151178064Sume ia->ia_addr = *sin6; 151278064Sume 1513146883Siedowse if (ifacount <= 1 && ifp->if_ioctl) { 1514146883Siedowse IFF_LOCKGIANT(ifp); 1515146883Siedowse error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1516146883Siedowse IFF_UNLOCKGIANT(ifp); 1517146883Siedowse if (error) { 1518146883Siedowse splx(s); 1519146883Siedowse return (error); 1520146883Siedowse } 152153541Sshin } 152278064Sume splx(s); 152353541Sshin 152478064Sume ia->ia_ifa.ifa_metric = ifp->if_metric; 152553541Sshin 152678064Sume /* we could do in(6)_socktrim here, but just omit it at this moment. */ 152778064Sume 152853541Sshin /* 152978064Sume * Special case: 1530124337Sume * If a new destination address is specified for a point-to-point 153178064Sume * interface, install a route to the destination as an interface 1532186119Sqingli * direct route. 1533124337Sume * XXX: the logic below rejects assigning multiple addresses on a p2p 1534159390Sgnn * interface that share the same destination. 153553541Sshin */ 1536186119Sqingli#if 0 /* QL - verify */ 153778064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1538124337Sume if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1539124337Sume ia->ia_dstaddr.sin6_family == AF_INET6) { 1540159390Sgnn int rtflags = RTF_UP | RTF_HOST; 1541159390Sgnn struct rtentry *rt = NULL, **rtp = NULL; 1542159390Sgnn 1543159390Sgnn if (nd6_need_cache(ifp) != 0) { 1544159390Sgnn rtp = &rt; 1545159390Sgnn } 1546159390Sgnn 1547178888Sjulian error = rtrequest(RTM_ADD, 1548178888Sjulian (struct sockaddr *)&ia->ia_dstaddr, 1549159390Sgnn (struct sockaddr *)&ia->ia_addr, 1550159390Sgnn (struct sockaddr *)&ia->ia_prefixmask, 1551159390Sgnn ia->ia_flags | rtflags, rtp); 1552159390Sgnn if (error != 0) 1553120856Sume return (error); 1554159390Sgnn if (rt != NULL) { 1555159390Sgnn struct llinfo_nd6 *ln; 1556159390Sgnn 1557159390Sgnn RT_LOCK(rt); 1558159390Sgnn ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1559159390Sgnn if (ln != NULL) { 1560159390Sgnn /* 1561159390Sgnn * Set the state to STALE because we don't 1562159390Sgnn * have to perform address resolution on this 1563159390Sgnn * link. 1564159390Sgnn */ 1565159390Sgnn ln->ln_state = ND6_LLINFO_STALE; 1566159390Sgnn } 1567159390Sgnn RT_REMREF(rt); 1568159390Sgnn RT_UNLOCK(rt); 1569159390Sgnn } 157078064Sume ia->ia_flags |= IFA_ROUTE; 157153541Sshin } 1572186119Sqingli#else 1573186119Sqingli plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1574186119Sqingli if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1575186119Sqingli ia->ia_dstaddr.sin6_family == AF_INET6) { 1576186119Sqingli if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, 1577186119Sqingli RTF_UP | RTF_HOST)) != 0) 1578186119Sqingli return (error); 1579186119Sqingli ia->ia_flags |= IFA_ROUTE; 158078064Sume } 1581186119Sqingli#endif 158253541Sshin 158395023Ssuz /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1584186119Sqingli if (newhost) { 1585186119Sqingli struct llentry *ln; 158653541Sshin 1587186119Sqingli IF_AFDATA_LOCK(ifp); 1588186119Sqingli ia->ia_ifa.ifa_rtrequest = NULL; 1589186119Sqingli 1590186119Sqingli /* XXX QL 1591186119Sqingli * we need to report rt_newaddrmsg 1592186119Sqingli */ 1593186119Sqingli ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE), 1594186119Sqingli (struct sockaddr *)&ia->ia_addr); 1595186119Sqingli IF_AFDATA_UNLOCK(ifp); 1596186119Sqingli if (ln) { 1597186119Sqingli ln->la_expire = 0; /* for IPv6 this means permanent */ 1598186119Sqingli ln->ln_state = ND6_LLINFO_REACHABLE; 1599186119Sqingli LLE_WUNLOCK(ln); 1600186119Sqingli } 1601186119Sqingli } 1602186119Sqingli 1603120856Sume return (error); 160453541Sshin} 160553541Sshin 1606142215Sglebiusstruct in6_multi_mship * 1607171259Sdelphijin6_joingroup(struct ifnet *ifp, struct in6_addr *addr, 1608171259Sdelphij int *errorp, int delay) 1609142215Sglebius{ 1610142215Sglebius struct in6_multi_mship *imm; 1611142215Sglebius 1612149849Sobrien imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT); 1613142215Sglebius if (!imm) { 1614142215Sglebius *errorp = ENOBUFS; 1615142215Sglebius return NULL; 1616142215Sglebius } 1617151539Ssuz imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay); 1618142215Sglebius if (!imm->i6mm_maddr) { 1619142215Sglebius /* *errorp is alrady set */ 1620149849Sobrien free(imm, M_IP6MADDR); 1621142215Sglebius return NULL; 1622142215Sglebius } 1623142215Sglebius return imm; 1624142215Sglebius} 1625142215Sglebius 1626142215Sglebiusint 1627171259Sdelphijin6_leavegroup(struct in6_multi_mship *imm) 1628142215Sglebius{ 1629142215Sglebius 1630142215Sglebius if (imm->i6mm_maddr) 1631142215Sglebius in6_delmulti(imm->i6mm_maddr); 1632149849Sobrien free(imm, M_IP6MADDR); 1633142215Sglebius return 0; 1634142215Sglebius} 1635142215Sglebius 163653541Sshin/* 163753541Sshin * Find an IPv6 interface link-local address specific to an interface. 163853541Sshin */ 163953541Sshinstruct in6_ifaddr * 1640171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 164153541Sshin{ 164278064Sume struct ifaddr *ifa; 164353541Sshin 1644120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 164553541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 164653541Sshin continue; 164762587Sitojun if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 164862587Sitojun if ((((struct in6_ifaddr *)ifa)->ia6_flags & 164962587Sitojun ignoreflags) != 0) 165062587Sitojun continue; 165153541Sshin break; 165262587Sitojun } 165353541Sshin } 165453541Sshin 1655120856Sume return ((struct in6_ifaddr *)ifa); 165653541Sshin} 165753541Sshin 165853541Sshin 165953541Sshin/* 166053541Sshin * find the internet address corresponding to a given interface and address. 166153541Sshin */ 166253541Sshinstruct in6_ifaddr * 1663171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 166453541Sshin{ 166578064Sume struct ifaddr *ifa; 166653541Sshin 1667120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 166853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 166953541Sshin continue; 167053541Sshin if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 167153541Sshin break; 167253541Sshin } 167353541Sshin 1674120856Sume return ((struct in6_ifaddr *)ifa); 167553541Sshin} 167653541Sshin 167753541Sshin/* 1678165118Sbz * Convert IP6 address to printable (loggable) representation. Caller 1679165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 168053541Sshin */ 168153541Sshinstatic char digits[] = "0123456789abcdef"; 168253541Sshinchar * 1683165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr) 168453541Sshin{ 168578064Sume int i; 168678064Sume char *cp; 1687126552Sume const u_int16_t *a = (const u_int16_t *)addr; 1688126552Sume const u_int8_t *d; 1689165287Sbz int dcolon = 0, zero = 0; 169053541Sshin 1691165118Sbz cp = ip6buf; 169253541Sshin 169353541Sshin for (i = 0; i < 8; i++) { 169453541Sshin if (dcolon == 1) { 169553541Sshin if (*a == 0) { 169653541Sshin if (i == 7) 169753541Sshin *cp++ = ':'; 169853541Sshin a++; 169953541Sshin continue; 170053541Sshin } else 170153541Sshin dcolon = 2; 170253541Sshin } 170353541Sshin if (*a == 0) { 170453541Sshin if (dcolon == 0 && *(a + 1) == 0) { 170553541Sshin if (i == 0) 170653541Sshin *cp++ = ':'; 170753541Sshin *cp++ = ':'; 170853541Sshin dcolon = 1; 170953541Sshin } else { 171053541Sshin *cp++ = '0'; 171153541Sshin *cp++ = ':'; 171253541Sshin } 171353541Sshin a++; 171453541Sshin continue; 171553541Sshin } 171691346Salfred d = (const u_char *)a; 1717165287Sbz /* Try to eliminate leading zeros in printout like in :0001. */ 1718165287Sbz zero = 1; 1719165287Sbz *cp = digits[*d >> 4]; 1720165287Sbz if (*cp != '0') { 1721165287Sbz zero = 0; 1722165287Sbz cp++; 1723165287Sbz } 1724165287Sbz *cp = digits[*d++ & 0xf]; 1725165287Sbz if (zero == 0 || (*cp != '0')) { 1726165287Sbz zero = 0; 1727165287Sbz cp++; 1728165287Sbz } 1729165287Sbz *cp = digits[*d >> 4]; 1730165287Sbz if (zero == 0 || (*cp != '0')) { 1731165287Sbz zero = 0; 1732165287Sbz cp++; 1733165287Sbz } 173453541Sshin *cp++ = digits[*d & 0xf]; 173553541Sshin *cp++ = ':'; 173653541Sshin a++; 173753541Sshin } 1738165118Sbz *--cp = '\0'; 1739165118Sbz return (ip6buf); 174053541Sshin} 174153541Sshin 174253541Sshinint 1743171259Sdelphijin6_localaddr(struct in6_addr *in6) 174453541Sshin{ 1745183550Szec INIT_VNET_INET6(curvnet); 174653541Sshin struct in6_ifaddr *ia; 174753541Sshin 174853541Sshin if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 174953541Sshin return 1; 175053541Sshin 1751181803Sbz for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) { 175253541Sshin if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1753120891Sume &ia->ia_prefixmask.sin6_addr)) { 175453541Sshin return 1; 1755120891Sume } 1756120891Sume } 175753541Sshin 175853541Sshin return (0); 175953541Sshin} 176053541Sshin 176178064Sumeint 1762171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6) 176378064Sume{ 1764183550Szec INIT_VNET_INET6(curvnet); 176578064Sume struct in6_ifaddr *ia; 176678064Sume 1767181803Sbz for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) { 176878064Sume if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 176978064Sume &sa6->sin6_addr) && 177078064Sume (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) 1771120856Sume return (1); /* true */ 177278064Sume 177378064Sume /* XXX: do we still have to go thru the rest of the list? */ 177478064Sume } 177578064Sume 1776120856Sume return (0); /* false */ 177778064Sume} 177878064Sume 177953541Sshin/* 178053541Sshin * return length of part which dst and src are equal 178153541Sshin * hard coding... 178253541Sshin */ 178353541Sshinint 1784171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst) 178553541Sshin{ 178653541Sshin int match = 0; 178753541Sshin u_char *s = (u_char *)src, *d = (u_char *)dst; 178853541Sshin u_char *lim = s + 16, r; 178953541Sshin 179053541Sshin while (s < lim) 179153541Sshin if ((r = (*d++ ^ *s++)) != 0) { 179253541Sshin while (r < 128) { 179353541Sshin match++; 179453541Sshin r <<= 1; 179553541Sshin } 179653541Sshin break; 179753541Sshin } else 179853541Sshin match += 8; 179953541Sshin return match; 180053541Sshin} 180153541Sshin 180262587Sitojun/* XXX: to be scope conscious */ 180353541Sshinint 1804171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 180553541Sshin{ 180653541Sshin int bytelen, bitlen; 180753541Sshin 180853541Sshin /* sanity check */ 180953541Sshin if (0 > len || len > 128) { 181053541Sshin log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 181153541Sshin len); 1812120856Sume return (0); 181353541Sshin } 181453541Sshin 181553541Sshin bytelen = len / 8; 181653541Sshin bitlen = len % 8; 181753541Sshin 181853541Sshin if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1819120856Sume return (0); 1820126184Scperciva if (bitlen != 0 && 1821126184Scperciva p1->s6_addr[bytelen] >> (8 - bitlen) != 182253541Sshin p2->s6_addr[bytelen] >> (8 - bitlen)) 1823120856Sume return (0); 182453541Sshin 1825120856Sume return (1); 182653541Sshin} 182753541Sshin 182853541Sshinvoid 1829171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len) 183053541Sshin{ 183153541Sshin u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 183253541Sshin int bytelen, bitlen, i; 183353541Sshin 183453541Sshin /* sanity check */ 183553541Sshin if (0 > len || len > 128) { 183653541Sshin log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 183753541Sshin len); 183853541Sshin return; 183953541Sshin } 184053541Sshin 184153541Sshin bzero(maskp, sizeof(*maskp)); 184253541Sshin bytelen = len / 8; 184353541Sshin bitlen = len % 8; 184453541Sshin for (i = 0; i < bytelen; i++) 184553541Sshin maskp->s6_addr[i] = 0xff; 184653541Sshin if (bitlen) 184753541Sshin maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 184853541Sshin} 184953541Sshin 185053541Sshin/* 185153541Sshin * return the best address out of the same scope. if no address was 185253541Sshin * found, return the first valid address from designated IF. 185353541Sshin */ 185453541Sshinstruct in6_ifaddr * 1855171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 185653541Sshin{ 1857183550Szec INIT_VNET_INET6(curvnet); 185853541Sshin int dst_scope = in6_addrscope(dst), blen = -1, tlen; 185953541Sshin struct ifaddr *ifa; 186053541Sshin struct in6_ifaddr *besta = 0; 186195023Ssuz struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 186253541Sshin 186353541Sshin dep[0] = dep[1] = NULL; 186453541Sshin 186553541Sshin /* 186653541Sshin * We first look for addresses in the same scope. 186753541Sshin * If there is one, return it. 186853541Sshin * If two or more, return one which matches the dst longest. 186953541Sshin * If none, return one of global addresses assigned other ifs. 187053541Sshin */ 1871120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 187253541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 187353541Sshin continue; 187453541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 187553541Sshin continue; /* XXX: is there any case to allow anycast? */ 187653541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 187753541Sshin continue; /* don't use this interface */ 187853541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 187953541Sshin continue; 188053541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1881181803Sbz if (V_ip6_use_deprecated) 188253541Sshin dep[0] = (struct in6_ifaddr *)ifa; 188353541Sshin continue; 188453541Sshin } 188553541Sshin 188653541Sshin if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 188753541Sshin /* 188853541Sshin * call in6_matchlen() as few as possible 188953541Sshin */ 189053541Sshin if (besta) { 189153541Sshin if (blen == -1) 189253541Sshin blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 189353541Sshin tlen = in6_matchlen(IFA_IN6(ifa), dst); 189453541Sshin if (tlen > blen) { 189553541Sshin blen = tlen; 189653541Sshin besta = (struct in6_ifaddr *)ifa; 189753541Sshin } 189853541Sshin } else 189953541Sshin besta = (struct in6_ifaddr *)ifa; 190053541Sshin } 190153541Sshin } 190253541Sshin if (besta) 1903120856Sume return (besta); 190453541Sshin 1905120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 190653541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 190753541Sshin continue; 190853541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 190953541Sshin continue; /* XXX: is there any case to allow anycast? */ 191053541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 191153541Sshin continue; /* don't use this interface */ 191253541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 191353541Sshin continue; 191453541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1915181803Sbz if (V_ip6_use_deprecated) 191653541Sshin dep[1] = (struct in6_ifaddr *)ifa; 191753541Sshin continue; 191853541Sshin } 191953541Sshin 192053541Sshin return (struct in6_ifaddr *)ifa; 192153541Sshin } 192253541Sshin 192353541Sshin /* use the last-resort values, that are, deprecated addresses */ 192453541Sshin if (dep[0]) 192553541Sshin return dep[0]; 192653541Sshin if (dep[1]) 192753541Sshin return dep[1]; 192853541Sshin 192953541Sshin return NULL; 193053541Sshin} 193153541Sshin 193253541Sshin/* 193353541Sshin * perform DAD when interface becomes IFF_UP. 193453541Sshin */ 193553541Sshinvoid 1936171259Sdelphijin6_if_up(struct ifnet *ifp) 193753541Sshin{ 193853541Sshin struct ifaddr *ifa; 193953541Sshin struct in6_ifaddr *ia; 194053541Sshin 1941120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 194253541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 194353541Sshin continue; 194453541Sshin ia = (struct in6_ifaddr *)ifa; 1945151539Ssuz if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 1946151539Ssuz /* 1947151539Ssuz * The TENTATIVE flag was likely set by hand 1948151539Ssuz * beforehand, implicitly indicating the need for DAD. 1949151539Ssuz * We may be able to skip the random delay in this 1950151539Ssuz * case, but we impose delays just in case. 1951151539Ssuz */ 1952151539Ssuz nd6_dad_start(ifa, 1953151539Ssuz arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 1954151539Ssuz } 195553541Sshin } 1956151539Ssuz 1957151539Ssuz /* 1958151539Ssuz * special cases, like 6to4, are handled in in6_ifattach 1959151539Ssuz */ 1960151539Ssuz in6_ifattach(ifp, NULL); 196153541Sshin} 196253541Sshin 196378064Sumeint 1964171259Sdelphijin6if_do_dad(struct ifnet *ifp) 196578064Sume{ 196678064Sume if ((ifp->if_flags & IFF_LOOPBACK) != 0) 1967120856Sume return (0); 196878064Sume 196978064Sume switch (ifp->if_type) { 197078064Sume#ifdef IFT_DUMMY 197178064Sume case IFT_DUMMY: 197278064Sume#endif 197378064Sume case IFT_FAITH: 197478064Sume /* 197578064Sume * These interfaces do not have the IFF_LOOPBACK flag, 197678064Sume * but loop packets back. We do not have to do DAD on such 197778064Sume * interfaces. We should even omit it, because loop-backed 197878064Sume * NS would confuse the DAD procedure. 197978064Sume */ 1980120856Sume return (0); 198178064Sume default: 198278064Sume /* 198378064Sume * Our DAD routine requires the interface up and running. 198478064Sume * However, some interfaces can be up before the RUNNING 198578064Sume * status. Additionaly, users may try to assign addresses 198678064Sume * before the interface becomes up (or running). 198778064Sume * We simply skip DAD in such a case as a work around. 198878064Sume * XXX: we should rather mark "tentative" on such addresses, 198978064Sume * and do DAD after the interface becomes ready. 199078064Sume */ 1991148887Srwatson if (!((ifp->if_flags & IFF_UP) && 1992148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 1993120856Sume return (0); 199478064Sume 1995120856Sume return (1); 199678064Sume } 199778064Sume} 199878064Sume 199953541Sshin/* 200053541Sshin * Calculate max IPv6 MTU through all the interfaces and store it 200153541Sshin * to in6_maxmtu. 200253541Sshin */ 200353541Sshinvoid 2004171259Sdelphijin6_setmaxmtu(void) 200553541Sshin{ 2006183550Szec INIT_VNET_NET(curvnet); 2007183550Szec INIT_VNET_INET6(curvnet); 200853541Sshin unsigned long maxmtu = 0; 200953541Sshin struct ifnet *ifp; 201053541Sshin 2011108172Shsu IFNET_RLOCK(); 2012181887Sjulian for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 2013181888Sjulian ifp = TAILQ_NEXT(ifp, if_list)) { 2014121283Sume /* this function can be called during ifnet initialization */ 2015121283Sume if (!ifp->if_afdata[AF_INET6]) 2016121283Sume continue; 201753541Sshin if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2018121283Sume IN6_LINKMTU(ifp) > maxmtu) 2019121283Sume maxmtu = IN6_LINKMTU(ifp); 202053541Sshin } 2021108172Shsu IFNET_RUNLOCK(); 2022120891Sume if (maxmtu) /* update only when maxmtu is positive */ 2023181803Sbz V_in6_maxmtu = maxmtu; 202453541Sshin} 202553541Sshin 2026151539Ssuz/* 2027151539Ssuz * Provide the length of interface identifiers to be used for the link attached 2028151539Ssuz * to the given interface. The length should be defined in "IPv6 over 2029151539Ssuz * xxx-link" document. Note that address architecture might also define 2030151539Ssuz * the length for a particular set of address prefixes, regardless of the 2031151539Ssuz * link type. As clarified in rfc2462bis, those two definitions should be 2032151539Ssuz * consistent, and those really are as of August 2004. 2033151539Ssuz */ 2034151539Ssuzint 2035171259Sdelphijin6_if2idlen(struct ifnet *ifp) 2036151539Ssuz{ 2037151539Ssuz switch (ifp->if_type) { 2038151539Ssuz case IFT_ETHER: /* RFC2464 */ 2039151539Ssuz#ifdef IFT_PROPVIRTUAL 2040151539Ssuz case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2041151539Ssuz#endif 2042151539Ssuz#ifdef IFT_L2VLAN 2043151539Ssuz case IFT_L2VLAN: /* ditto */ 2044151539Ssuz#endif 2045151539Ssuz#ifdef IFT_IEEE80211 2046151539Ssuz case IFT_IEEE80211: /* ditto */ 2047151539Ssuz#endif 2048151539Ssuz#ifdef IFT_MIP 2049151539Ssuz case IFT_MIP: /* ditto */ 2050151539Ssuz#endif 2051151539Ssuz return (64); 2052151539Ssuz case IFT_FDDI: /* RFC2467 */ 2053151539Ssuz return (64); 2054151539Ssuz case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2055151539Ssuz return (64); 2056151539Ssuz case IFT_PPP: /* RFC2472 */ 2057151539Ssuz return (64); 2058151539Ssuz case IFT_ARCNET: /* RFC2497 */ 2059151539Ssuz return (64); 2060151539Ssuz case IFT_FRELAY: /* RFC2590 */ 2061151539Ssuz return (64); 2062151539Ssuz case IFT_IEEE1394: /* RFC3146 */ 2063151539Ssuz return (64); 2064151539Ssuz case IFT_GIF: 2065151539Ssuz return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2066151539Ssuz case IFT_LOOP: 2067151539Ssuz return (64); /* XXX: is this really correct? */ 2068151539Ssuz default: 2069151539Ssuz /* 2070151539Ssuz * Unknown link type: 2071151539Ssuz * It might be controversial to use the today's common constant 2072151539Ssuz * of 64 for these cases unconditionally. For full compliance, 2073151539Ssuz * we should return an error in this case. On the other hand, 2074151539Ssuz * if we simply miss the standard for the link type or a new 2075151539Ssuz * standard is defined for a new link type, the IFID length 2076151539Ssuz * is very likely to be the common constant. As a compromise, 2077151539Ssuz * we always use the constant, but make an explicit notice 2078151539Ssuz * indicating the "unknown" case. 2079151539Ssuz */ 2080151539Ssuz printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2081151539Ssuz return (64); 2082151539Ssuz } 2083151539Ssuz} 2084151539Ssuz 2085186119Sqingli#include <sys/sysctl.h> 2086186119Sqingli 2087186119Sqinglistruct in6_llentry { 2088186119Sqingli struct llentry base; 2089186119Sqingli struct sockaddr_in6 l3_addr6; 2090186119Sqingli}; 2091186119Sqingli 2092186119Sqinglistatic struct llentry * 2093186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2094186119Sqingli{ 2095186119Sqingli struct in6_llentry *lle; 2096186119Sqingli 2097186119Sqingli lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, 2098186119Sqingli M_DONTWAIT | M_ZERO); 2099186119Sqingli if (lle == NULL) /* NB: caller generates msg */ 2100186119Sqingli return NULL; 2101186119Sqingli 2102186119Sqingli callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE); 2103186119Sqingli lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2104186119Sqingli lle->base.lle_refcnt = 1; 2105186119Sqingli LLE_LOCK_INIT(&lle->base); 2106186119Sqingli return &lle->base; 2107186119Sqingli} 2108186119Sqingli 2109186119Sqingli/* 2110186119Sqingli * Deletes an address from the address table. 2111186119Sqingli * This function is called by the timer functions 2112186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and 2113186119Sqingli * the caller does the locking. 2114186119Sqingli */ 2115186119Sqinglistatic void 2116186119Sqingliin6_lltable_free(struct lltable *llt, struct llentry *lle) 2117186119Sqingli{ 2118186119Sqingli free(lle, M_LLTABLE); 2119186119Sqingli} 2120186119Sqingli 2121186119Sqinglistatic int 2122186119Sqingliin6_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 2123186119Sqingli{ 2124186119Sqingli struct rtentry *rt; 2125186119Sqingli char ip6buf[INET6_ADDRSTRLEN]; 2126186119Sqingli 2127186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2128186119Sqingli ("sin_family %d", l3addr->sa_family)); 2129186119Sqingli 2130186119Sqingli /* XXX rtalloc1 should take a const param */ 2131186119Sqingli rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 2132186119Sqingli if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2133186119Sqingli struct ifaddr *ifa; 2134186119Sqingli /* 2135186119Sqingli * Create an ND6 cache for an IPv6 neighbor 2136186119Sqingli * that is not covered by our own prefix. 2137186119Sqingli */ 2138186119Sqingli /* XXX ifaof_ifpforaddr should take a const param */ 2139186119Sqingli ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2140186119Sqingli if (ifa != NULL) { 2141186119Sqingli if (rt != NULL) 2142186119Sqingli rtfree(rt); 2143186119Sqingli return 0; 2144186119Sqingli } 2145186119Sqingli log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2146186119Sqingli ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2147186119Sqingli if (rt != NULL) 2148186119Sqingli rtfree(rt); 2149186119Sqingli return EINVAL; 2150186119Sqingli } 2151186119Sqingli rtfree(rt); 2152186119Sqingli return 0; 2153186119Sqingli} 2154186119Sqingli 2155186119Sqinglistatic struct llentry * 2156186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags, 2157186119Sqingli const struct sockaddr *l3addr) 2158186119Sqingli{ 2159186119Sqingli const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2160186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2161186119Sqingli struct llentry *lle; 2162186119Sqingli struct llentries *lleh; 2163186119Sqingli u_int hashkey; 2164186119Sqingli 2165186119Sqingli IF_AFDATA_LOCK_ASSERT(ifp); 2166186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2167186119Sqingli ("sin_family %d", l3addr->sa_family)); 2168186119Sqingli 2169186119Sqingli hashkey = sin6->sin6_addr.s6_addr32[3]; 2170186119Sqingli lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2171186119Sqingli LIST_FOREACH(lle, lleh, lle_next) { 2172186119Sqingli if (lle->la_flags & LLE_DELETED) 2173186119Sqingli continue; 2174186119Sqingli if (bcmp(L3_ADDR(lle), l3addr, l3addr->sa_len) == 0) 2175186119Sqingli break; 2176186119Sqingli } 2177186119Sqingli 2178186119Sqingli if (lle == NULL) { 2179186119Sqingli if (!(flags & LLE_CREATE)) 2180186119Sqingli return (NULL); 2181186119Sqingli /* 2182186119Sqingli * A route that covers the given address must have 2183186119Sqingli * been installed 1st because we are doing a resolution, 2184186119Sqingli * verify this. 2185186119Sqingli */ 2186186119Sqingli if (!(flags & LLE_IFADDR) && 2187186119Sqingli in6_lltable_rtcheck(ifp, l3addr) != 0) 2188186119Sqingli return NULL; 2189186119Sqingli 2190186119Sqingli lle = in6_lltable_new(l3addr, flags); 2191186119Sqingli if (lle == NULL) { 2192186119Sqingli log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2193186119Sqingli return NULL; 2194186119Sqingli } 2195186119Sqingli lle->la_flags = flags & ~LLE_CREATE; 2196186119Sqingli if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2197186119Sqingli bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2198186119Sqingli lle->la_flags |= (LLE_VALID | LLE_STATIC); 2199186119Sqingli } 2200186119Sqingli 2201186119Sqingli lle->lle_tbl = llt; 2202186119Sqingli lle->lle_head = lleh; 2203186119Sqingli LIST_INSERT_HEAD(lleh, lle, lle_next); 2204186119Sqingli } else if (flags & LLE_DELETE) { 2205186119Sqingli LLE_WLOCK(lle); 2206186119Sqingli lle->la_flags = LLE_DELETED; 2207186119Sqingli LLE_WUNLOCK(lle); 2208186119Sqingli#ifdef DIAGNOSTICS 2209186119Sqingli log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2210186119Sqingli#endif 2211186119Sqingli lle = (void *)-1; 2212186119Sqingli } 2213186119Sqingli if (LLE_IS_VALID(lle)) { 2214186119Sqingli if (flags & LLE_EXCLUSIVE) 2215186119Sqingli LLE_WLOCK(lle); 2216186119Sqingli else 2217186119Sqingli LLE_RLOCK(lle); 2218186119Sqingli } 2219186119Sqingli return (lle); 2220186119Sqingli} 2221186119Sqingli 2222186119Sqinglistatic int 2223186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2224186119Sqingli{ 2225186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2226186119Sqingli struct llentry *lle; 2227186119Sqingli /* XXX stack use */ 2228186119Sqingli struct { 2229186119Sqingli struct rt_msghdr rtm; 2230186119Sqingli struct sockaddr_in6 sin6; 2231186119Sqingli /* 2232186119Sqingli * ndp.c assumes that sdl is word aligned 2233186119Sqingli */ 2234186119Sqingli#ifdef __LP64__ 2235186119Sqingli uint32_t pad; 2236186119Sqingli#endif 2237186119Sqingli struct sockaddr_dl sdl; 2238186119Sqingli } ndpc; 2239186119Sqingli int i, error; 2240186119Sqingli 2241186119Sqingli /* XXXXX 2242186119Sqingli * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 2243186119Sqingli * so it is okay to use this ASSERT, change it when 2244186119Sqingli * IFNET lock is finalized 2245186119Sqingli */ 2246186119Sqingli IFNET_WLOCK_ASSERT(); 2247186119Sqingli 2248186119Sqingli error = 0; 2249186119Sqingli for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2250186119Sqingli LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2251186119Sqingli struct sockaddr_dl *sdl; 2252186119Sqingli 2253186119Sqingli /* skip deleted or invalid entries */ 2254186119Sqingli if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2255186119Sqingli continue; 2256186119Sqingli /* 2257186119Sqingli * produce a msg made of: 2258186119Sqingli * struct rt_msghdr; 2259186119Sqingli * struct sockaddr_in6 (IPv6) 2260186119Sqingli * struct sockaddr_dl; 2261186119Sqingli */ 2262186119Sqingli bzero(&ndpc, sizeof(ndpc)); 2263186119Sqingli ndpc.rtm.rtm_msglen = sizeof(ndpc); 2264186119Sqingli ndpc.sin6.sin6_family = AF_INET6; 2265186119Sqingli ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2266186119Sqingli bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2267186119Sqingli 2268186119Sqingli /* publish */ 2269186119Sqingli if (lle->la_flags & LLE_PUB) 2270186119Sqingli ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2271186119Sqingli 2272186119Sqingli sdl = &ndpc.sdl; 2273186119Sqingli sdl->sdl_family = AF_LINK; 2274186119Sqingli sdl->sdl_len = sizeof(*sdl); 2275186119Sqingli sdl->sdl_alen = ifp->if_addrlen; 2276186119Sqingli sdl->sdl_index = ifp->if_index; 2277186119Sqingli sdl->sdl_type = ifp->if_type; 2278186119Sqingli bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2279186119Sqingli ndpc.rtm.rtm_rmx.rmx_expire = 2280186119Sqingli lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2281186119Sqingli ndpc.rtm.rtm_flags |= RTF_HOST; 2282186119Sqingli if (lle->la_flags & LLE_STATIC) 2283186119Sqingli ndpc.rtm.rtm_flags |= RTF_STATIC; 2284186119Sqingli ndpc.rtm.rtm_index = ifp->if_index; 2285186119Sqingli error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2286186119Sqingli if (error) 2287186119Sqingli break; 2288186119Sqingli } 2289186119Sqingli } 2290186119Sqingli return error; 2291186119Sqingli} 2292186119Sqingli 2293121161Sumevoid * 2294171259Sdelphijin6_domifattach(struct ifnet *ifp) 2295121161Sume{ 2296121161Sume struct in6_ifextra *ext; 2297121161Sume 2298121161Sume ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2299121161Sume bzero(ext, sizeof(*ext)); 2300121161Sume 2301121161Sume ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2302121161Sume M_IFADDR, M_WAITOK); 2303121161Sume bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2304121161Sume 2305121161Sume ext->icmp6_ifstat = 2306121161Sume (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2307121161Sume M_IFADDR, M_WAITOK); 2308121161Sume bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2309121161Sume 2310121161Sume ext->nd_ifinfo = nd6_ifattach(ifp); 2311121161Sume ext->scope6_id = scope6_ifattach(ifp); 2312186119Sqingli ext->lltable = lltable_init(ifp, AF_INET6); 2313186119Sqingli if (ext->lltable != NULL) { 2314186119Sqingli ext->lltable->llt_new = in6_lltable_new; 2315186119Sqingli ext->lltable->llt_free = in6_lltable_free; 2316186119Sqingli ext->lltable->llt_rtcheck = in6_lltable_rtcheck; 2317186119Sqingli ext->lltable->llt_lookup = in6_lltable_lookup; 2318186119Sqingli ext->lltable->llt_dump = in6_lltable_dump; 2319186119Sqingli } 2320121161Sume return ext; 2321121161Sume} 2322121161Sume 2323121161Sumevoid 2324171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux) 2325121161Sume{ 2326121161Sume struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2327121161Sume 2328121161Sume scope6_ifdetach(ext->scope6_id); 2329121161Sume nd6_ifdetach(ext->nd_ifinfo); 2330186119Sqingli lltable_free(ext->lltable); 2331121161Sume free(ext->in6_ifstat, M_IFADDR); 2332121161Sume free(ext->icmp6_ifstat, M_IFADDR); 2333121161Sume free(ext, M_IFADDR); 2334121161Sume} 2335121161Sume 233653541Sshin/* 233795023Ssuz * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 233853541Sshin * v4 mapped addr or v4 compat addr 233953541Sshin */ 234053541Sshinvoid 234153541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 234253541Sshin{ 2343171259Sdelphij 234453541Sshin bzero(sin, sizeof(*sin)); 234553541Sshin sin->sin_len = sizeof(struct sockaddr_in); 234653541Sshin sin->sin_family = AF_INET; 234753541Sshin sin->sin_port = sin6->sin6_port; 2348120891Sume sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 234953541Sshin} 235053541Sshin 235153541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 235253541Sshinvoid 235353541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 235453541Sshin{ 235553541Sshin bzero(sin6, sizeof(*sin6)); 235653541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 235753541Sshin sin6->sin6_family = AF_INET6; 235853541Sshin sin6->sin6_port = sin->sin_port; 235953541Sshin sin6->sin6_addr.s6_addr32[0] = 0; 236053541Sshin sin6->sin6_addr.s6_addr32[1] = 0; 236153541Sshin sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 236253541Sshin sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 236353541Sshin} 236453541Sshin 236553541Sshin/* Convert sockaddr_in6 into sockaddr_in. */ 236653541Sshinvoid 236753541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam) 236853541Sshin{ 236953541Sshin struct sockaddr_in *sin_p; 237053541Sshin struct sockaddr_in6 sin6; 237153541Sshin 237253541Sshin /* 237353541Sshin * Save original sockaddr_in6 addr and convert it 237453541Sshin * to sockaddr_in. 237553541Sshin */ 237653541Sshin sin6 = *(struct sockaddr_in6 *)nam; 237753541Sshin sin_p = (struct sockaddr_in *)nam; 237853541Sshin in6_sin6_2_sin(sin_p, &sin6); 237953541Sshin} 238053541Sshin 238153541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 238253541Sshinvoid 238353541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 238453541Sshin{ 238553541Sshin struct sockaddr_in *sin_p; 238653541Sshin struct sockaddr_in6 *sin6_p; 238753541Sshin 2388184205Sdes sin6_p = malloc(sizeof *sin6_p, M_SONAME, 2389111119Simp M_WAITOK); 239053541Sshin sin_p = (struct sockaddr_in *)*nam; 239153541Sshin in6_sin_2_v4mapsin6(sin_p, sin6_p); 2392184205Sdes free(*nam, M_SONAME); 239353541Sshin *nam = (struct sockaddr *)sin6_p; 239453541Sshin} 2395