in6.c revision 191323
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 191323 2009-04-20 16:05:16Z rwatson $"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 68189106Sbz#include "opt_route.h" 6962587Sitojun 7053541Sshin#include <sys/param.h> 7153541Sshin#include <sys/errno.h> 72186948Sbz#include <sys/jail.h> 7353541Sshin#include <sys/malloc.h> 7453541Sshin#include <sys/socket.h> 7553541Sshin#include <sys/socketvar.h> 7653541Sshin#include <sys/sockio.h> 7753541Sshin#include <sys/systm.h> 78164033Srwatson#include <sys/priv.h> 7953541Sshin#include <sys/proc.h> 8053541Sshin#include <sys/time.h> 8153541Sshin#include <sys/kernel.h> 8253541Sshin#include <sys/syslog.h> 83181803Sbz#include <sys/vimage.h> 8453541Sshin 8553541Sshin#include <net/if.h> 8653541Sshin#include <net/if_types.h> 8753541Sshin#include <net/route.h> 8853541Sshin#include <net/if_dl.h> 89185571Sbz#include <net/vnet.h> 9053541Sshin 9153541Sshin#include <netinet/in.h> 9253541Sshin#include <netinet/in_var.h> 93186119Sqingli#include <net/if_llatbl.h> 9453541Sshin#include <netinet/if_ether.h> 9578064Sume#include <netinet/in_systm.h> 9678064Sume#include <netinet/ip.h> 9778064Sume#include <netinet/in_pcb.h> 9853541Sshin 9962587Sitojun#include <netinet/ip6.h> 10053541Sshin#include <netinet6/ip6_var.h> 10195023Ssuz#include <netinet6/nd6.h> 10253541Sshin#include <netinet6/mld6_var.h> 10362587Sitojun#include <netinet6/ip6_mroute.h> 10453541Sshin#include <netinet6/in6_ifattach.h> 10562587Sitojun#include <netinet6/scope6_var.h> 10678064Sume#include <netinet6/in6_pcb.h> 107185571Sbz#include <netinet6/vinet6.h> 10862587Sitojun 109149849SobrienMALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address"); 11053541Sshin 11153541Sshin/* 11253541Sshin * Definitions of some costant IP6 addresses. 11353541Sshin */ 11462587Sitojunconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 11562587Sitojunconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 11662587Sitojunconst struct in6_addr in6addr_nodelocal_allnodes = 11753541Sshin IN6ADDR_NODELOCAL_ALLNODES_INIT; 11862587Sitojunconst struct in6_addr in6addr_linklocal_allnodes = 11953541Sshin IN6ADDR_LINKLOCAL_ALLNODES_INIT; 12062587Sitojunconst struct in6_addr in6addr_linklocal_allrouters = 12153541Sshin IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 12253541Sshin 12362587Sitojunconst struct in6_addr in6mask0 = IN6MASK0; 12462587Sitojunconst struct in6_addr in6mask32 = IN6MASK32; 12562587Sitojunconst struct in6_addr in6mask64 = IN6MASK64; 12662587Sitojunconst struct in6_addr in6mask96 = IN6MASK96; 12762587Sitojunconst struct in6_addr in6mask128 = IN6MASK128; 12853541Sshin 129126552Sumeconst struct sockaddr_in6 sa6_any = 130126552Sume { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 }; 13178064Sume 13262587Sitojunstatic int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 13383366Sjulian struct ifnet *, struct thread *)); 13478064Sumestatic int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *, 135120891Sume struct sockaddr_in6 *, int)); 136175162Sobrienstatic void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 13753541Sshin 13862587Sitojunstruct in6_multihead in6_multihead; /* XXX BSS initialization */ 13983934Sbrooksint (*faithprefix_p)(struct in6_addr *); 14083934Sbrooks 141120891Sume 14278064Sume 14353541Sshinint 144171259Sdelphijin6_mask2len(struct in6_addr *mask, u_char *lim0) 14553541Sshin{ 14678064Sume int x = 0, y; 14778064Sume u_char *lim = lim0, *p; 14853541Sshin 149120891Sume /* ignore the scope_id part */ 150120891Sume if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 15178064Sume lim = (u_char *)mask + sizeof(*mask); 15278064Sume for (p = (u_char *)mask; p < lim; x++, p++) { 15378064Sume if (*p != 0xff) 15453541Sshin break; 15553541Sshin } 15653541Sshin y = 0; 15778064Sume if (p < lim) { 15853541Sshin for (y = 0; y < 8; y++) { 15978064Sume if ((*p & (0x80 >> y)) == 0) 16053541Sshin break; 16153541Sshin } 16253541Sshin } 16378064Sume 16478064Sume /* 16578064Sume * when the limit pointer is given, do a stricter check on the 16678064Sume * remaining bits. 16778064Sume */ 16878064Sume if (p < lim) { 16978064Sume if (y != 0 && (*p & (0x00ff >> y)) != 0) 170120856Sume return (-1); 17178064Sume for (p = p + 1; p < lim; p++) 17278064Sume if (*p != 0) 173120856Sume return (-1); 17478064Sume } 175120891Sume 17653541Sshin return x * 8 + y; 17753541Sshin} 17853541Sshin 17953541Sshin#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 18062587Sitojun#define ia62ifa(ia6) (&((ia6)->ia_ifa)) 18153541Sshin 18253541Sshinint 183171259Sdelphijin6_control(struct socket *so, u_long cmd, caddr_t data, 184171259Sdelphij struct ifnet *ifp, struct thread *td) 18553541Sshin{ 186183550Szec INIT_VNET_INET6(curvnet); 18753541Sshin struct in6_ifreq *ifr = (struct in6_ifreq *)data; 18878064Sume struct in6_ifaddr *ia = NULL; 18953541Sshin struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 190151539Ssuz struct sockaddr_in6 *sa6; 191164033Srwatson int error; 19253541Sshin 19362587Sitojun switch (cmd) { 19462587Sitojun case SIOCGETSGCNT_IN6: 19562587Sitojun case SIOCGETMIFCNT_IN6: 196166938Sbms return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP); 19762587Sitojun } 19853541Sshin 199121742Sume switch(cmd) { 200121742Sume case SIOCAADDRCTL_POLICY: 201121742Sume case SIOCDADDRCTL_POLICY: 202164033Srwatson if (td != NULL) { 203164033Srwatson error = priv_check(td, PRIV_NETINET_ADDRCTRL6); 204164033Srwatson if (error) 205164033Srwatson return (error); 206164033Srwatson } 207121742Sume return (in6_src_ioctl(cmd, data)); 208121742Sume } 209121742Sume 21062587Sitojun if (ifp == NULL) 211120856Sume return (EOPNOTSUPP); 21253541Sshin 21353541Sshin switch (cmd) { 21453541Sshin case SIOCSNDFLUSH_IN6: 21553541Sshin case SIOCSPFXFLUSH_IN6: 21653541Sshin case SIOCSRTRFLUSH_IN6: 21762587Sitojun case SIOCSDEFIFACE_IN6: 21862587Sitojun case SIOCSIFINFO_FLAGS: 219164033Srwatson if (td != NULL) { 220164033Srwatson error = priv_check(td, PRIV_NETINET_ND6); 221164033Srwatson if (error) 222164033Srwatson return (error); 223164033Srwatson } 224120891Sume /* FALLTHROUGH */ 22578064Sume case OSIOCGIFINFO_IN6: 22653541Sshin case SIOCGIFINFO_IN6: 227151468Ssuz case SIOCSIFINFO_IN6: 22853541Sshin case SIOCGDRLST_IN6: 22953541Sshin case SIOCGPRLST_IN6: 23053541Sshin case SIOCGNBRINFO_IN6: 23162587Sitojun case SIOCGDEFIFACE_IN6: 232120856Sume return (nd6_ioctl(cmd, data, ifp)); 23353541Sshin } 23453541Sshin 23553541Sshin switch (cmd) { 23653541Sshin case SIOCSIFPREFIX_IN6: 23753541Sshin case SIOCDIFPREFIX_IN6: 23853541Sshin case SIOCAIFPREFIX_IN6: 23953541Sshin case SIOCCIFPREFIX_IN6: 24053541Sshin case SIOCSGIFPREFIX_IN6: 24153541Sshin case SIOCGIFPREFIX_IN6: 24278064Sume log(LOG_NOTICE, 24378064Sume "prefix ioctls are now invalidated. " 24478064Sume "please use ifconfig.\n"); 245120856Sume return (EOPNOTSUPP); 24653541Sshin } 24753541Sshin 24895023Ssuz switch (cmd) { 24962587Sitojun case SIOCSSCOPE6: 250164033Srwatson if (td != NULL) { 251164033Srwatson error = priv_check(td, PRIV_NETINET_SCOPE6); 252164033Srwatson if (error) 253164033Srwatson return (error); 254164033Srwatson } 255121161Sume return (scope6_set(ifp, 256121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 25762587Sitojun case SIOCGSCOPE6: 258121161Sume return (scope6_get(ifp, 259121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 26062587Sitojun case SIOCGSCOPE6DEF: 261121161Sume return (scope6_get_default((struct scope6_id *) 262121161Sume ifr->ifr_ifru.ifru_scope_id)); 26362587Sitojun } 26462587Sitojun 26553541Sshin switch (cmd) { 26653541Sshin case SIOCALIFADDR: 267175630Sbz if (td != NULL) { 268175630Sbz error = priv_check(td, PRIV_NET_ADDIFADDR); 269175630Sbz if (error) 270175630Sbz return (error); 271175630Sbz } 272175630Sbz return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 273175630Sbz 27453541Sshin case SIOCDLIFADDR: 275164033Srwatson if (td != NULL) { 276175630Sbz error = priv_check(td, PRIV_NET_DELIFADDR); 277164033Srwatson if (error) 278164033Srwatson return (error); 279164033Srwatson } 280120891Sume /* FALLTHROUGH */ 28153541Sshin case SIOCGLIFADDR: 28283366Sjulian return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 28353541Sshin } 28453541Sshin 28553541Sshin /* 28653541Sshin * Find address for this interface, if it exists. 287151539Ssuz * 288151539Ssuz * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 289151539Ssuz * only, and used the first interface address as the target of other 290151539Ssuz * operations (without checking ifra_addr). This was because netinet 291151539Ssuz * code/API assumed at most 1 interface address per interface. 292151539Ssuz * Since IPv6 allows a node to assign multiple addresses 293151539Ssuz * on a single interface, we almost always look and check the 294151539Ssuz * presence of ifra_addr, and reject invalid ones here. 295151539Ssuz * It also decreases duplicated code among SIOC*_IN6 operations. 29653541Sshin */ 297151539Ssuz switch (cmd) { 298151539Ssuz case SIOCAIFADDR_IN6: 299151539Ssuz case SIOCSIFPHYADDR_IN6: 300151539Ssuz sa6 = &ifra->ifra_addr; 301151539Ssuz break; 302151539Ssuz case SIOCSIFADDR_IN6: 303151539Ssuz case SIOCGIFADDR_IN6: 304151539Ssuz case SIOCSIFDSTADDR_IN6: 305151539Ssuz case SIOCSIFNETMASK_IN6: 306151539Ssuz case SIOCGIFDSTADDR_IN6: 307151539Ssuz case SIOCGIFNETMASK_IN6: 308151539Ssuz case SIOCDIFADDR_IN6: 309151539Ssuz case SIOCGIFPSRCADDR_IN6: 310151539Ssuz case SIOCGIFPDSTADDR_IN6: 311151539Ssuz case SIOCGIFAFLAG_IN6: 312151539Ssuz case SIOCSNDFLUSH_IN6: 313151539Ssuz case SIOCSPFXFLUSH_IN6: 314151539Ssuz case SIOCSRTRFLUSH_IN6: 315151539Ssuz case SIOCGIFALIFETIME_IN6: 316151539Ssuz case SIOCSIFALIFETIME_IN6: 317151539Ssuz case SIOCGIFSTAT_IN6: 318151539Ssuz case SIOCGIFSTAT_ICMP6: 319151539Ssuz sa6 = &ifr->ifr_addr; 320151539Ssuz break; 321151539Ssuz default: 322151539Ssuz sa6 = NULL; 323151539Ssuz break; 324151539Ssuz } 325151539Ssuz if (sa6 && sa6->sin6_family == AF_INET6) { 326148385Sume int error = 0; 32753541Sshin 328151539Ssuz if (sa6->sin6_scope_id != 0) 329151539Ssuz error = sa6_embedscope(sa6, 0); 330148385Sume else 331151539Ssuz error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 332148385Sume if (error != 0) 333148385Sume return (error); 334188144Sjamie if (td != NULL && (error = prison_check_ip6(td->td_ucred, 335188144Sjamie &sa6->sin6_addr)) != 0) 336188144Sjamie return (error); 337151539Ssuz ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 338151539Ssuz } else 339151539Ssuz ia = NULL; 34053541Sshin 34153541Sshin switch (cmd) { 34278064Sume case SIOCSIFADDR_IN6: 34378064Sume case SIOCSIFDSTADDR_IN6: 34478064Sume case SIOCSIFNETMASK_IN6: 34578064Sume /* 34678064Sume * Since IPv6 allows a node to assign multiple addresses 347151465Ssuz * on a single interface, SIOCSIFxxx ioctls are deprecated. 34878064Sume */ 34978064Sume /* we decided to obsolete this command (20000704) */ 350120856Sume return (EINVAL); 35153541Sshin 35253541Sshin case SIOCDIFADDR_IN6: 35362587Sitojun /* 35478064Sume * for IPv4, we look for existing in_ifaddr here to allow 355151465Ssuz * "ifconfig if0 delete" to remove the first IPv4 address on 356151465Ssuz * the interface. For IPv6, as the spec allows multiple 357151465Ssuz * interface address from the day one, we consider "remove the 358151465Ssuz * first one" semantics to be not preferable. 35962587Sitojun */ 36062587Sitojun if (ia == NULL) 361120856Sume return (EADDRNOTAVAIL); 36253541Sshin /* FALLTHROUGH */ 36353541Sshin case SIOCAIFADDR_IN6: 36462587Sitojun /* 36578064Sume * We always require users to specify a valid IPv6 address for 36678064Sume * the corresponding operation. 36762587Sitojun */ 36878064Sume if (ifra->ifra_addr.sin6_family != AF_INET6 || 36978064Sume ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) 370120856Sume return (EAFNOSUPPORT); 37153541Sshin 372164033Srwatson if (td != NULL) { 373175630Sbz error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ? 374175630Sbz PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 375164033Srwatson if (error) 376164033Srwatson return (error); 377164033Srwatson } 378164033Srwatson 37953541Sshin break; 38053541Sshin 38153541Sshin case SIOCGIFADDR_IN6: 38253541Sshin /* This interface is basically deprecated. use SIOCGIFCONF. */ 383120891Sume /* FALLTHROUGH */ 38453541Sshin case SIOCGIFAFLAG_IN6: 38553541Sshin case SIOCGIFNETMASK_IN6: 38653541Sshin case SIOCGIFDSTADDR_IN6: 38753541Sshin case SIOCGIFALIFETIME_IN6: 38853541Sshin /* must think again about its semantics */ 38962587Sitojun if (ia == NULL) 390120856Sume return (EADDRNOTAVAIL); 39153541Sshin break; 39253541Sshin case SIOCSIFALIFETIME_IN6: 39353541Sshin { 39453541Sshin struct in6_addrlifetime *lt; 39553541Sshin 396164033Srwatson if (td != NULL) { 397164033Srwatson error = priv_check(td, PRIV_NETINET_ALIFETIME6); 398164033Srwatson if (error) 399164033Srwatson return (error); 400164033Srwatson } 40162587Sitojun if (ia == NULL) 402120856Sume return (EADDRNOTAVAIL); 40353541Sshin /* sanity for overflow - beware unsigned */ 40453541Sshin lt = &ifr->ifr_ifru.ifru_lifetime; 405126552Sume if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME && 406126552Sume lt->ia6t_vltime + time_second < time_second) { 40753541Sshin return EINVAL; 40853541Sshin } 409126552Sume if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME && 410126552Sume lt->ia6t_pltime + time_second < time_second) { 41153541Sshin return EINVAL; 41253541Sshin } 41353541Sshin break; 41453541Sshin } 41553541Sshin } 41653541Sshin 41753541Sshin switch (cmd) { 41853541Sshin 41953541Sshin case SIOCGIFADDR_IN6: 42053541Sshin ifr->ifr_addr = ia->ia_addr; 421148385Sume if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 422148385Sume return (error); 42353541Sshin break; 42453541Sshin 42553541Sshin case SIOCGIFDSTADDR_IN6: 42653541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 427120856Sume return (EINVAL); 42862587Sitojun /* 42962587Sitojun * XXX: should we check if ifa_dstaddr is NULL and return 43062587Sitojun * an error? 43162587Sitojun */ 43253541Sshin ifr->ifr_dstaddr = ia->ia_dstaddr; 433148385Sume if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 434148385Sume return (error); 43553541Sshin break; 43653541Sshin 43753541Sshin case SIOCGIFNETMASK_IN6: 43853541Sshin ifr->ifr_addr = ia->ia_prefixmask; 43953541Sshin break; 44053541Sshin 44153541Sshin case SIOCGIFAFLAG_IN6: 44253541Sshin ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 44353541Sshin break; 44453541Sshin 44553541Sshin case SIOCGIFSTAT_IN6: 446121167Sume if (ifp == NULL) 447121167Sume return EINVAL; 448121161Sume bzero(&ifr->ifr_ifru.ifru_stat, 449121161Sume sizeof(ifr->ifr_ifru.ifru_stat)); 450121161Sume ifr->ifr_ifru.ifru_stat = 451121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; 45253541Sshin break; 45353541Sshin 45453541Sshin case SIOCGIFSTAT_ICMP6: 45553541Sshin if (ifp == NULL) 45653541Sshin return EINVAL; 457155454Sgnn bzero(&ifr->ifr_ifru.ifru_icmp6stat, 458121161Sume sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 459121161Sume ifr->ifr_ifru.ifru_icmp6stat = 460121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; 46153541Sshin break; 46253541Sshin 46353541Sshin case SIOCGIFALIFETIME_IN6: 46453541Sshin ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 465151539Ssuz if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 466151539Ssuz time_t maxexpire; 467151539Ssuz struct in6_addrlifetime *retlt = 468151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 469151539Ssuz 470151539Ssuz /* 471151539Ssuz * XXX: adjust expiration time assuming time_t is 472151539Ssuz * signed. 473151539Ssuz */ 474151539Ssuz maxexpire = (-1) & 475151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 476151539Ssuz if (ia->ia6_lifetime.ia6t_vltime < 477151539Ssuz maxexpire - ia->ia6_updatetime) { 478151539Ssuz retlt->ia6t_expire = ia->ia6_updatetime + 479151539Ssuz ia->ia6_lifetime.ia6t_vltime; 480151539Ssuz } else 481151539Ssuz retlt->ia6t_expire = maxexpire; 482151539Ssuz } 483151539Ssuz if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 484151539Ssuz time_t maxexpire; 485151539Ssuz struct in6_addrlifetime *retlt = 486151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 487151539Ssuz 488151539Ssuz /* 489151539Ssuz * XXX: adjust expiration time assuming time_t is 490151539Ssuz * signed. 491151539Ssuz */ 492151539Ssuz maxexpire = (-1) & 493151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 494151539Ssuz if (ia->ia6_lifetime.ia6t_pltime < 495151539Ssuz maxexpire - ia->ia6_updatetime) { 496151539Ssuz retlt->ia6t_preferred = ia->ia6_updatetime + 497151539Ssuz ia->ia6_lifetime.ia6t_pltime; 498151539Ssuz } else 499151539Ssuz retlt->ia6t_preferred = maxexpire; 500151539Ssuz } 50153541Sshin break; 50253541Sshin 50353541Sshin case SIOCSIFALIFETIME_IN6: 50453541Sshin ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 50553541Sshin /* for sanity */ 50653541Sshin if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 50753541Sshin ia->ia6_lifetime.ia6t_expire = 50853541Sshin time_second + ia->ia6_lifetime.ia6t_vltime; 50953541Sshin } else 51053541Sshin ia->ia6_lifetime.ia6t_expire = 0; 51153541Sshin if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 51253541Sshin ia->ia6_lifetime.ia6t_preferred = 51353541Sshin time_second + ia->ia6_lifetime.ia6t_pltime; 51453541Sshin } else 51553541Sshin ia->ia6_lifetime.ia6t_preferred = 0; 51653541Sshin break; 51753541Sshin 51878064Sume case SIOCAIFADDR_IN6: 51978064Sume { 52078064Sume int i, error = 0; 521151539Ssuz struct nd_prefixctl pr0; 522151539Ssuz struct nd_prefix *pr; 52378064Sume 52462587Sitojun /* 52578064Sume * first, make or update the interface address structure, 52678064Sume * and link it to the list. 52762587Sitojun */ 528151539Ssuz if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) 529120856Sume return (error); 530151915Ssuz if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) 531151915Ssuz == NULL) { 532171260Sdelphij /* 533151915Ssuz * this can happen when the user specify the 0 valid 534151915Ssuz * lifetime. 535151915Ssuz */ 536151915Ssuz break; 537151915Ssuz } 53853541Sshin 53978064Sume /* 54078064Sume * then, make the prefix on-link on the interface. 54178064Sume * XXX: we'd rather create the prefix before the address, but 54278064Sume * we need at least one address to install the corresponding 54378064Sume * interface route, so we configure the address first. 54478064Sume */ 54578064Sume 54678064Sume /* 54778064Sume * convert mask to prefix length (prefixmask has already 54878064Sume * been validated in in6_update_ifa(). 54978064Sume */ 55078064Sume bzero(&pr0, sizeof(pr0)); 55178064Sume pr0.ndpr_ifp = ifp; 55278064Sume pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 553120891Sume NULL); 554120891Sume if (pr0.ndpr_plen == 128) { 55578064Sume break; /* we don't need to install a host route. */ 556120891Sume } 55778064Sume pr0.ndpr_prefix = ifra->ifra_addr; 55878064Sume /* apply the mask for safety. */ 55978064Sume for (i = 0; i < 4; i++) { 56078064Sume pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 561120891Sume ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 56278064Sume } 56378064Sume /* 56495023Ssuz * XXX: since we don't have an API to set prefix (not address) 56595023Ssuz * lifetimes, we just use the same lifetimes as addresses. 56695023Ssuz * The (temporarily) installed lifetimes can be overridden by 56795023Ssuz * later advertised RAs (when accept_rtadv is non 0), which is 56895023Ssuz * an intended behavior. 56978064Sume */ 57078064Sume pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 57178064Sume pr0.ndpr_raf_auto = 572120891Sume ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 57378064Sume pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 57478064Sume pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 57578064Sume 576120891Sume /* add the prefix if not yet. */ 57778064Sume if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 57878064Sume /* 57978064Sume * nd6_prelist_add will install the corresponding 58078064Sume * interface route. 58178064Sume */ 58278064Sume if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 583120856Sume return (error); 58478064Sume if (pr == NULL) { 585120891Sume log(LOG_ERR, "nd6_prelist_add succeeded but " 58678064Sume "no prefix\n"); 587120856Sume return (EINVAL); /* XXX panic here? */ 58878064Sume } 58978064Sume } 59078064Sume 591151915Ssuz /* relate the address to the prefix */ 592151915Ssuz if (ia->ia6_ndpr == NULL) { 593151915Ssuz ia->ia6_ndpr = pr; 594151915Ssuz pr->ndpr_refcnt++; 59578064Sume 59678064Sume /* 597151915Ssuz * If this is the first autoconf address from the 598151915Ssuz * prefix, create a temporary address as well 599151915Ssuz * (when required). 60078064Sume */ 601151915Ssuz if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 602181803Sbz V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) { 603151915Ssuz int e; 604151915Ssuz if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { 605151915Ssuz log(LOG_NOTICE, "in6_control: failed " 606151915Ssuz "to create a temporary address, " 607151915Ssuz "errno=%d\n", e); 608151915Ssuz } 609151915Ssuz } 61062587Sitojun } 611151915Ssuz 612151915Ssuz /* 613151915Ssuz * this might affect the status of autoconfigured addresses, 614151915Ssuz * that is, this address might make other addresses detached. 615151915Ssuz */ 616151915Ssuz pfxlist_onlink_check(); 617126264Smlaier if (error == 0 && ia) 618126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 61978064Sume break; 62078064Sume } 62162587Sitojun 62278064Sume case SIOCDIFADDR_IN6: 62378064Sume { 624151539Ssuz struct nd_prefix *pr; 62578064Sume 62678064Sume /* 62778064Sume * If the address being deleted is the only one that owns 62878064Sume * the corresponding prefix, expire the prefix as well. 629120891Sume * XXX: theoretically, we don't have to worry about such 63078064Sume * relationship, since we separate the address management 63178064Sume * and the prefix management. We do this, however, to provide 63278064Sume * as much backward compatibility as possible in terms of 63378064Sume * the ioctl operation. 634151915Ssuz * Note that in6_purgeaddr() will decrement ndpr_refcnt. 63578064Sume */ 636151915Ssuz pr = ia->ia6_ndpr; 63778064Sume in6_purgeaddr(&ia->ia_ifa); 638151915Ssuz if (pr && pr->ndpr_refcnt == 0) 639151915Ssuz prelist_remove(pr); 640126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 64153541Sshin break; 64278064Sume } 64353541Sshin 64478064Sume default: 64578064Sume if (ifp == NULL || ifp->if_ioctl == 0) 646120856Sume return (EOPNOTSUPP); 647120856Sume return ((*ifp->if_ioctl)(ifp, cmd, data)); 64878064Sume } 64953541Sshin 650120856Sume return (0); 65178064Sume} 65253541Sshin 65378064Sume/* 65478064Sume * Update parameters of an IPv6 interface address. 65578064Sume * If necessary, a new entry is created and linked into address chains. 65678064Sume * This function is separated from in6_control(). 65778064Sume * XXX: should this be performed under splnet()? 65878064Sume */ 65978064Sumeint 660171259Sdelphijin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 661171259Sdelphij struct in6_ifaddr *ia, int flags) 66278064Sume{ 663183550Szec INIT_VNET_INET6(ifp->if_vnet); 664183550Szec INIT_VPROCG(TD_TO_VPROCG(curthread)); /* XXX V_hostname needs this */ 66578064Sume int error = 0, hostIsNew = 0, plen = -1; 66678064Sume struct in6_ifaddr *oia; 66778064Sume struct sockaddr_in6 dst6; 66878064Sume struct in6_addrlifetime *lt; 669151539Ssuz struct in6_multi_mship *imm; 670151539Ssuz struct in6_multi *in6m_sol; 671148385Sume struct rtentry *rt; 672151539Ssuz int delay; 673165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 67478064Sume 67578064Sume /* Validate parameters */ 67678064Sume if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 677120856Sume return (EINVAL); 67878064Sume 67978064Sume /* 68078064Sume * The destination address for a p2p link must have a family 68178064Sume * of AF_UNSPEC or AF_INET6. 68278064Sume */ 68378064Sume if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 68478064Sume ifra->ifra_dstaddr.sin6_family != AF_INET6 && 68578064Sume ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 686120856Sume return (EAFNOSUPPORT); 68778064Sume /* 68878064Sume * validate ifra_prefixmask. don't check sin6_family, netmask 68978064Sume * does not carry fields other than sin6_len. 69078064Sume */ 69178064Sume if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 692120856Sume return (EINVAL); 69378064Sume /* 69478064Sume * Because the IPv6 address architecture is classless, we require 69578064Sume * users to specify a (non 0) prefix length (mask) for a new address. 69678064Sume * We also require the prefix (when specified) mask is valid, and thus 69778064Sume * reject a non-consecutive mask. 69878064Sume */ 69978064Sume if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 700120856Sume return (EINVAL); 70178064Sume if (ifra->ifra_prefixmask.sin6_len != 0) { 70278064Sume plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 703120891Sume (u_char *)&ifra->ifra_prefixmask + 704120891Sume ifra->ifra_prefixmask.sin6_len); 70578064Sume if (plen <= 0) 706120856Sume return (EINVAL); 707120891Sume } else { 70862587Sitojun /* 70995023Ssuz * In this case, ia must not be NULL. We just use its prefix 71078064Sume * length. 71162587Sitojun */ 71278064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 71378064Sume } 71478064Sume /* 71578064Sume * If the destination address on a p2p interface is specified, 71678064Sume * and the address is a scoped one, validate/set the scope 71778064Sume * zone identifier. 71878064Sume */ 71978064Sume dst6 = ifra->ifra_dstaddr; 720120891Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 72178064Sume (dst6.sin6_family == AF_INET6)) { 722148385Sume struct in6_addr in6_tmp; 723126552Sume u_int32_t zoneid; 72478064Sume 725148385Sume in6_tmp = dst6.sin6_addr; 726148385Sume if (in6_setscope(&in6_tmp, ifp, &zoneid)) 727148385Sume return (EINVAL); /* XXX: should be impossible */ 728148385Sume 729148385Sume if (dst6.sin6_scope_id != 0) { 730148385Sume if (dst6.sin6_scope_id != zoneid) 731148385Sume return (EINVAL); 732148385Sume } else /* user omit to specify the ID. */ 733126552Sume dst6.sin6_scope_id = zoneid; 734148385Sume 735148385Sume /* convert into the internal form */ 736148385Sume if (sa6_embedscope(&dst6, 0)) 737148385Sume return (EINVAL); /* XXX: should be impossible */ 73878064Sume } 73978064Sume /* 74078064Sume * The destination address can be specified only for a p2p or a 74178064Sume * loopback interface. If specified, the corresponding prefix length 74278064Sume * must be 128. 74378064Sume */ 74478064Sume if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 74578064Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 746126552Sume /* XXX: noisy message */ 747122059Sume nd6log((LOG_INFO, "in6_update_ifa: a destination can " 748122059Sume "be specified for a p2p or a loopback IF only\n")); 749120856Sume return (EINVAL); 75078064Sume } 75178064Sume if (plen != 128) { 752122059Sume nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 753122059Sume "be 128 when dstaddr is specified\n")); 754120856Sume return (EINVAL); 75578064Sume } 75678064Sume } 75778064Sume /* lifetime consistency check */ 75878064Sume lt = &ifra->ifra_lifetime; 759151539Ssuz if (lt->ia6t_pltime > lt->ia6t_vltime) 760151539Ssuz return (EINVAL); 76178064Sume if (lt->ia6t_vltime == 0) { 76262587Sitojun /* 76378064Sume * the following log might be noisy, but this is a typical 76478064Sume * configuration mistake or a tool's bug. 76562587Sitojun */ 766122059Sume nd6log((LOG_INFO, 76778064Sume "in6_update_ifa: valid lifetime is 0 for %s\n", 768165118Sbz ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 769151539Ssuz 770151539Ssuz if (ia == NULL) 771151539Ssuz return (0); /* there's nothing to do */ 77278064Sume } 77362587Sitojun 77478064Sume /* 77578064Sume * If this is a new address, allocate a new ifaddr and link it 77678064Sume * into chains. 77778064Sume */ 77878064Sume if (ia == NULL) { 77978064Sume hostIsNew = 1; 78079763Sume /* 78179763Sume * When in6_update_ifa() is called in a process of a received 782120891Sume * RA, it is called under an interrupt context. So, we should 783120891Sume * call malloc with M_NOWAIT. 78479763Sume */ 785120891Sume ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, 786120891Sume M_NOWAIT); 78778064Sume if (ia == NULL) 78878064Sume return (ENOBUFS); 78978064Sume bzero((caddr_t)ia, sizeof(*ia)); 790170202Sjinmei LIST_INIT(&ia->ia6_memberships); 791151539Ssuz /* Initialize the address and masks, and put time stamp */ 792108033Shsu IFA_LOCK_INIT(&ia->ia_ifa); 79378064Sume ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 79478064Sume ia->ia_addr.sin6_family = AF_INET6; 79578064Sume ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 796151539Ssuz ia->ia6_createtime = time_second; 79778064Sume if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 79878064Sume /* 79978064Sume * XXX: some functions expect that ifa_dstaddr is not 80078064Sume * NULL for p2p interfaces. 80178064Sume */ 802120891Sume ia->ia_ifa.ifa_dstaddr = 803120891Sume (struct sockaddr *)&ia->ia_dstaddr; 80478064Sume } else { 80578064Sume ia->ia_ifa.ifa_dstaddr = NULL; 80653541Sshin } 807108033Shsu ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 80878064Sume 80978064Sume ia->ia_ifp = ifp; 810181803Sbz if ((oia = V_in6_ifaddr) != NULL) { 81178064Sume for ( ; oia->ia_next; oia = oia->ia_next) 81278064Sume continue; 81378064Sume oia->ia_next = ia; 81478064Sume } else 815181803Sbz V_in6_ifaddr = ia; 81678064Sume 817108033Shsu ia->ia_ifa.ifa_refcnt = 1; 818191323Srwatson IF_ADDR_LOCK(ifp); 819108033Shsu TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 820191323Srwatson IF_ADDR_UNLOCK(ifp); 82178064Sume } 82278064Sume 823151539Ssuz /* update timestamp */ 824151539Ssuz ia->ia6_updatetime = time_second; 825151539Ssuz 82678064Sume /* set prefix mask */ 82778064Sume if (ifra->ifra_prefixmask.sin6_len) { 82878064Sume /* 82978064Sume * We prohibit changing the prefix length of an existing 83078064Sume * address, because 83178064Sume * + such an operation should be rare in IPv6, and 83278064Sume * + the operation would confuse prefix management. 83378064Sume */ 83478064Sume if (ia->ia_prefixmask.sin6_len && 83578064Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 836122059Sume nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an" 83778064Sume " existing (%s) address should not be changed\n", 838165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 83978064Sume error = EINVAL; 84078064Sume goto unlink; 84153541Sshin } 84278064Sume ia->ia_prefixmask = ifra->ifra_prefixmask; 84378064Sume } 84478064Sume 84578064Sume /* 84678064Sume * If a new destination address is specified, scrub the old one and 84778064Sume * install the new destination. Note that the interface must be 848120891Sume * p2p or loopback (see the check above.) 84978064Sume */ 85078064Sume if (dst6.sin6_family == AF_INET6 && 851120891Sume !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 85278064Sume int e; 85378064Sume 85478064Sume if ((ia->ia_flags & IFA_ROUTE) != 0 && 855120891Sume (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 856122059Sume nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 85778064Sume "a route to the old destination: %s\n", 858165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 85978064Sume /* proceed anyway... */ 860120891Sume } else 86178064Sume ia->ia_flags &= ~IFA_ROUTE; 86278064Sume ia->ia_dstaddr = dst6; 86378064Sume } 86453541Sshin 865148385Sume /* 866148385Sume * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 867148385Sume * to see if the address is deprecated or invalidated, but initialize 868148385Sume * these members for applications. 869148385Sume */ 870148385Sume ia->ia6_lifetime = ifra->ifra_lifetime; 871148385Sume if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 872148385Sume ia->ia6_lifetime.ia6t_expire = 873148385Sume time_second + ia->ia6_lifetime.ia6t_vltime; 874148385Sume } else 875148385Sume ia->ia6_lifetime.ia6t_expire = 0; 876148385Sume if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 877148385Sume ia->ia6_lifetime.ia6t_preferred = 878148385Sume time_second + ia->ia6_lifetime.ia6t_pltime; 879148385Sume } else 880148385Sume ia->ia6_lifetime.ia6t_preferred = 0; 881148385Sume 88278064Sume /* reset the interface and routing table appropriately. */ 88378064Sume if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 88478064Sume goto unlink; 88578064Sume 88678064Sume /* 887148385Sume * configure address flags. 888148385Sume */ 889148385Sume ia->ia6_flags = ifra->ifra_flags; 890148385Sume /* 891148385Sume * backward compatibility - if IN6_IFF_DEPRECATED is set from the 892148385Sume * userland, make it deprecated. 893148385Sume */ 894148385Sume if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 895148385Sume ia->ia6_lifetime.ia6t_pltime = 0; 896148385Sume ia->ia6_lifetime.ia6t_preferred = time_second; 897148385Sume } 898148385Sume /* 899151539Ssuz * Make the address tentative before joining multicast addresses, 900151539Ssuz * so that corresponding MLD responses would not have a tentative 901151539Ssuz * source address. 902148385Sume */ 903151539Ssuz ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 904151539Ssuz if (hostIsNew && in6if_do_dad(ifp)) 905148385Sume ia->ia6_flags |= IN6_IFF_TENTATIVE; 906148385Sume 907148385Sume /* 908148385Sume * We are done if we have simply modified an existing address. 909148385Sume */ 910148385Sume if (!hostIsNew) 911148385Sume return (error); 912148385Sume 913148385Sume /* 91478064Sume * Beyond this point, we should call in6_purgeaddr upon an error, 915120891Sume * not just go to unlink. 91678064Sume */ 91778064Sume 918151465Ssuz /* Join necessary multicast groups */ 919151539Ssuz in6m_sol = NULL; 92078064Sume if ((ifp->if_flags & IFF_MULTICAST) != 0) { 92178064Sume struct sockaddr_in6 mltaddr, mltmask; 922148385Sume struct in6_addr llsol; 92378064Sume 924148385Sume /* join solicited multicast addr for new host id */ 925148385Sume bzero(&llsol, sizeof(struct in6_addr)); 926151465Ssuz llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL; 927148385Sume llsol.s6_addr32[1] = 0; 928148385Sume llsol.s6_addr32[2] = htonl(1); 929148385Sume llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; 930148385Sume llsol.s6_addr8[12] = 0xff; 931148385Sume if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 932148385Sume /* XXX: should not happen */ 933148385Sume log(LOG_ERR, "in6_update_ifa: " 934148385Sume "in6_setscope failed\n"); 935148385Sume goto cleanup; 93653541Sshin } 937151539Ssuz delay = 0; 938151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 939151539Ssuz /* 940151539Ssuz * We need a random delay for DAD on the address 941151539Ssuz * being configured. It also means delaying 942151539Ssuz * transmission of the corresponding MLD report to 943151539Ssuz * avoid report collision. 944151539Ssuz * [draft-ietf-ipv6-rfc2462bis-02.txt] 945151539Ssuz */ 946151539Ssuz delay = arc4random() % 947151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 948151539Ssuz } 949151539Ssuz imm = in6_joingroup(ifp, &llsol, &error, delay); 950170202Sjinmei if (imm == NULL) { 951148385Sume nd6log((LOG_WARNING, 952148385Sume "in6_update_ifa: addmulti failed for " 953148385Sume "%s on %s (errno=%d)\n", 954165118Sbz ip6_sprintf(ip6buf, &llsol), if_name(ifp), 955148385Sume error)); 956151539Ssuz in6_purgeaddr((struct ifaddr *)ia); 957151539Ssuz return (error); 958148385Sume } 959170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 960170202Sjinmei imm, i6mm_chain); 961151539Ssuz in6m_sol = imm->i6mm_maddr; 96253541Sshin 96378064Sume bzero(&mltmask, sizeof(mltmask)); 96478064Sume mltmask.sin6_len = sizeof(struct sockaddr_in6); 96578064Sume mltmask.sin6_family = AF_INET6; 96678064Sume mltmask.sin6_addr = in6mask32; 967151465Ssuz#define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 96853541Sshin 96953541Sshin /* 97078064Sume * join link-local all-nodes address 97171207Sitojun */ 97278064Sume bzero(&mltaddr, sizeof(mltaddr)); 97378064Sume mltaddr.sin6_len = sizeof(struct sockaddr_in6); 97478064Sume mltaddr.sin6_family = AF_INET6; 97578064Sume mltaddr.sin6_addr = in6addr_linklocal_allnodes; 976148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 977148385Sume 0) 978148385Sume goto cleanup; /* XXX: should not fail */ 97971207Sitojun 980148385Sume /* 981148385Sume * XXX: do we really need this automatic routes? 982148385Sume * We should probably reconsider this stuff. Most applications 983148385Sume * actually do not need the routes, since they usually specify 984148385Sume * the outgoing interface. 985148385Sume */ 986148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 987148385Sume if (rt) { 988174375Sjulian /* XXX: only works in !SCOPEDROUTING case. */ 989148385Sume if (memcmp(&mltaddr.sin6_addr, 990148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 991151465Ssuz MLTMASK_LEN)) { 992148385Sume RTFREE_LOCKED(rt); 993148385Sume rt = NULL; 994148385Sume } 995148385Sume } 996148385Sume if (!rt) { 997148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 998148385Sume (struct sockaddr *)&ia->ia_addr, 999186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1000148385Sume (struct rtentry **)0); 1001148385Sume if (error) 1002148385Sume goto cleanup; 1003151539Ssuz } else { 1004151539Ssuz RTFREE_LOCKED(rt); 1005151539Ssuz } 100678064Sume 1007151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1008151539Ssuz if (!imm) { 1009151539Ssuz nd6log((LOG_WARNING, 1010151539Ssuz "in6_update_ifa: addmulti failed for " 1011151539Ssuz "%s on %s (errno=%d)\n", 1012165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1013151539Ssuz if_name(ifp), error)); 1014151539Ssuz goto cleanup; 1015151539Ssuz } 1016170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1017151539Ssuz 101871207Sitojun /* 101978064Sume * join node information group address 102053541Sshin */ 1021181803Sbz#define hostnamelen strlen(V_hostname) 1022151539Ssuz delay = 0; 1023151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1024151539Ssuz /* 1025151539Ssuz * The spec doesn't say anything about delay for this 1026151539Ssuz * group, but the same logic should apply. 1027151539Ssuz */ 1028151539Ssuz delay = arc4random() % 1029151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 1030151539Ssuz } 1031180291Srwatson mtx_lock(&hostname_mtx); 1032181803Sbz if (in6_nigroup(ifp, V_hostname, hostnamelen, 1033181803Sbz &mltaddr.sin6_addr) == 0) { 1034180291Srwatson mtx_unlock(&hostname_mtx); 1035151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 1036151539Ssuz delay); /* XXX jinmei */ 1037151539Ssuz if (!imm) { 1038151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1039151539Ssuz "addmulti failed for %s on %s " 1040151539Ssuz "(errno=%d)\n", 1041165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1042151539Ssuz if_name(ifp), error)); 1043151539Ssuz /* XXX not very fatal, go on... */ 1044170202Sjinmei } else { 1045170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 1046170202Sjinmei imm, i6mm_chain); 104762587Sitojun } 1048180291Srwatson } else 1049180291Srwatson mtx_unlock(&hostname_mtx); 105078064Sume#undef hostnamelen 105153541Sshin 105278064Sume /* 1053148385Sume * join interface-local all-nodes address. 1054148385Sume * (ff01::1%ifN, and ff01::%ifN/32) 105578064Sume */ 1056148385Sume mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1057148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) 1058148385Sume != 0) 1059148385Sume goto cleanup; /* XXX: should not fail */ 1060148385Sume /* XXX: again, do we really need the route? */ 1061148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1062148385Sume if (rt) { 1063148385Sume if (memcmp(&mltaddr.sin6_addr, 1064148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1065151465Ssuz MLTMASK_LEN)) { 1066148385Sume RTFREE_LOCKED(rt); 1067148385Sume rt = NULL; 1068148385Sume } 1069148385Sume } 1070148385Sume if (!rt) { 1071148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1072148385Sume (struct sockaddr *)&ia->ia_addr, 1073186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1074148385Sume (struct rtentry **)0); 1075148385Sume if (error) 1076148385Sume goto cleanup; 1077148385Sume } else 1078148385Sume RTFREE_LOCKED(rt); 107981115Sume 1080151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1081151539Ssuz if (!imm) { 1082151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1083151539Ssuz "addmulti failed for %s on %s " 1084151539Ssuz "(errno=%d)\n", 1085165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1086151539Ssuz if_name(ifp), error)); 1087151539Ssuz goto cleanup; 1088151539Ssuz } 1089170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1090151465Ssuz#undef MLTMASK_LEN 109178064Sume } 109253541Sshin 1093151539Ssuz /* 1094151539Ssuz * Perform DAD, if needed. 1095151539Ssuz * XXX It may be of use, if we can administratively 1096151539Ssuz * disable DAD. 1097151539Ssuz */ 1098151539Ssuz if (hostIsNew && in6if_do_dad(ifp) && 1099151539Ssuz ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1100151539Ssuz (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1101151539Ssuz { 1102151539Ssuz int mindelay, maxdelay; 1103151539Ssuz 1104151539Ssuz delay = 0; 1105151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1106151539Ssuz /* 1107151539Ssuz * We need to impose a delay before sending an NS 1108151539Ssuz * for DAD. Check if we also needed a delay for the 1109151539Ssuz * corresponding MLD message. If we did, the delay 1110151539Ssuz * should be larger than the MLD delay (this could be 1111151539Ssuz * relaxed a bit, but this simple logic is at least 1112151539Ssuz * safe). 1113151539Ssuz */ 1114151539Ssuz mindelay = 0; 1115151539Ssuz if (in6m_sol != NULL && 1116151539Ssuz in6m_sol->in6m_state == MLD_REPORTPENDING) { 1117151539Ssuz mindelay = in6m_sol->in6m_timer; 1118151539Ssuz } 1119151539Ssuz maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1120151539Ssuz if (maxdelay - mindelay == 0) 1121151539Ssuz delay = 0; 1122151539Ssuz else { 1123151539Ssuz delay = 1124151539Ssuz (arc4random() % (maxdelay - mindelay)) + 1125151539Ssuz mindelay; 1126151539Ssuz } 1127151539Ssuz } 1128151539Ssuz nd6_dad_start((struct ifaddr *)ia, delay); 1129151539Ssuz } 1130151539Ssuz 1131120856Sume return (error); 113278064Sume 113378064Sume unlink: 113478064Sume /* 113578064Sume * XXX: if a change of an existing address failed, keep the entry 113678064Sume * anyway. 113778064Sume */ 113878064Sume if (hostIsNew) 113978064Sume in6_unlink_ifa(ia, ifp); 1140120856Sume return (error); 1141148385Sume 1142148385Sume cleanup: 1143148385Sume in6_purgeaddr(&ia->ia_ifa); 1144148385Sume return error; 114553541Sshin} 114653541Sshin 114762587Sitojunvoid 1148171259Sdelphijin6_purgeaddr(struct ifaddr *ifa) 114962587Sitojun{ 115078064Sume struct ifnet *ifp = ifa->ifa_ifp; 115178064Sume struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1152170202Sjinmei struct in6_multi_mship *imm; 115362587Sitojun 115478064Sume /* stop DAD processing */ 115578064Sume nd6_dad_stop(ifa); 115662587Sitojun 1157186119Sqingli IF_AFDATA_LOCK(ifp); 1158186119Sqingli lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR), 1159186119Sqingli (struct sockaddr *)&ia->ia_addr); 1160186119Sqingli IF_AFDATA_UNLOCK(ifp); 1161186119Sqingli 116278064Sume /* 1163170202Sjinmei * leave from multicast groups we have joined for the interface 1164170202Sjinmei */ 1165170202Sjinmei while ((imm = ia->ia6_memberships.lh_first) != NULL) { 1166170202Sjinmei LIST_REMOVE(imm, i6mm_chain); 1167170202Sjinmei in6_leavegroup(imm); 116862587Sitojun } 116962587Sitojun 117078064Sume in6_unlink_ifa(ia, ifp); 117178064Sume} 117278064Sume 117378064Sumestatic void 1174171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 117578064Sume{ 1176183550Szec INIT_VNET_INET6(ifp->if_vnet); 117778064Sume struct in6_ifaddr *oia; 117878064Sume int s = splnet(); 117978064Sume 1180191323Srwatson IF_ADDR_LOCK(ifp); 118162587Sitojun TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list); 1182191323Srwatson IF_ADDR_UNLOCK(ifp); 118362587Sitojun 118462587Sitojun oia = ia; 1185181803Sbz if (oia == (ia = V_in6_ifaddr)) 1186181803Sbz V_in6_ifaddr = ia->ia_next; 118762587Sitojun else { 118862587Sitojun while (ia->ia_next && (ia->ia_next != oia)) 118962587Sitojun ia = ia->ia_next; 119062587Sitojun if (ia->ia_next) 119162587Sitojun ia->ia_next = oia->ia_next; 119278064Sume else { 119378064Sume /* search failed */ 119478064Sume printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n"); 119578064Sume } 119662587Sitojun } 119762587Sitojun 119862587Sitojun /* 1199151915Ssuz * Release the reference to the base prefix. There should be a 1200151915Ssuz * positive reference. 120162587Sitojun */ 1202151915Ssuz if (oia->ia6_ndpr == NULL) { 1203151915Ssuz nd6log((LOG_NOTICE, 1204151915Ssuz "in6_unlink_ifa: autoconf'ed address " 1205151915Ssuz "%p has no prefix\n", oia)); 1206151915Ssuz } else { 1207151915Ssuz oia->ia6_ndpr->ndpr_refcnt--; 1208151915Ssuz oia->ia6_ndpr = NULL; 1209151915Ssuz } 121062587Sitojun 1211151915Ssuz /* 1212151915Ssuz * Also, if the address being removed is autoconf'ed, call 1213151915Ssuz * pfxlist_onlink_check() since the release might affect the status of 1214171260Sdelphij * other (detached) addresses. 1215151915Ssuz */ 1216151915Ssuz if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) { 121778064Sume pfxlist_onlink_check(); 121862587Sitojun } 121978064Sume 122078064Sume /* 122178064Sume * release another refcnt for the link from in6_ifaddr. 122278064Sume * Note that we should decrement the refcnt at least once for all *BSD. 122378064Sume */ 122462587Sitojun IFAFREE(&oia->ia_ifa); 122578064Sume 122678064Sume splx(s); 122762587Sitojun} 122862587Sitojun 122978064Sumevoid 1230171259Sdelphijin6_purgeif(struct ifnet *ifp) 123178064Sume{ 123278064Sume struct ifaddr *ifa, *nifa; 123378064Sume 1234120891Sume for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) { 123578064Sume nifa = TAILQ_NEXT(ifa, ifa_list); 123678064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 123778064Sume continue; 123878064Sume in6_purgeaddr(ifa); 123978064Sume } 124078064Sume 124178064Sume in6_ifdetach(ifp); 124278064Sume} 124378064Sume 124453541Sshin/* 124553541Sshin * SIOC[GAD]LIFADDR. 124662744Sgrog * SIOCGLIFADDR: get first address. (?) 124753541Sshin * SIOCGLIFADDR with IFLR_PREFIX: 124853541Sshin * get first address that matches the specified prefix. 124953541Sshin * SIOCALIFADDR: add the specified address. 125053541Sshin * SIOCALIFADDR with IFLR_PREFIX: 125153541Sshin * add the specified prefix, filling hostid part from 125253541Sshin * the first link-local address. prefixlen must be <= 64. 125353541Sshin * SIOCDLIFADDR: delete the specified address. 125453541Sshin * SIOCDLIFADDR with IFLR_PREFIX: 125553541Sshin * delete the first address that matches the specified prefix. 125653541Sshin * return values: 125753541Sshin * EINVAL on invalid parameters 125853541Sshin * EADDRNOTAVAIL on prefix match failed/specified address not found 125953541Sshin * other values may be returned from in6_ioctl() 126053541Sshin * 126153541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 126253541Sshin * this is to accomodate address naming scheme other than RFC2374, 126353541Sshin * in the future. 126453541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 126553541Sshin * address encoding scheme. (see figure on page 8) 126653541Sshin */ 126753541Sshinstatic int 1268171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1269171259Sdelphij struct ifnet *ifp, struct thread *td) 127053541Sshin{ 127153541Sshin struct if_laddrreq *iflr = (struct if_laddrreq *)data; 127253541Sshin struct ifaddr *ifa; 127362587Sitojun struct sockaddr *sa; 127453541Sshin 127553541Sshin /* sanity checks */ 127653541Sshin if (!data || !ifp) { 127753541Sshin panic("invalid argument to in6_lifaddr_ioctl"); 1278120891Sume /* NOTREACHED */ 127953541Sshin } 128053541Sshin 128153541Sshin switch (cmd) { 128253541Sshin case SIOCGLIFADDR: 128353541Sshin /* address must be specified on GET with IFLR_PREFIX */ 128453541Sshin if ((iflr->flags & IFLR_PREFIX) == 0) 128553541Sshin break; 128695023Ssuz /* FALLTHROUGH */ 128753541Sshin case SIOCALIFADDR: 128853541Sshin case SIOCDLIFADDR: 128953541Sshin /* address must be specified on ADD and DELETE */ 129062587Sitojun sa = (struct sockaddr *)&iflr->addr; 129162587Sitojun if (sa->sa_family != AF_INET6) 129253541Sshin return EINVAL; 129362587Sitojun if (sa->sa_len != sizeof(struct sockaddr_in6)) 129453541Sshin return EINVAL; 129553541Sshin /* XXX need improvement */ 129662587Sitojun sa = (struct sockaddr *)&iflr->dstaddr; 129762587Sitojun if (sa->sa_family && sa->sa_family != AF_INET6) 129853541Sshin return EINVAL; 129962587Sitojun if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 130053541Sshin return EINVAL; 130153541Sshin break; 130295023Ssuz default: /* shouldn't happen */ 130362587Sitojun#if 0 130462587Sitojun panic("invalid cmd to in6_lifaddr_ioctl"); 130595023Ssuz /* NOTREACHED */ 130662587Sitojun#else 130753541Sshin return EOPNOTSUPP; 130862587Sitojun#endif 130953541Sshin } 131053541Sshin if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 131153541Sshin return EINVAL; 131253541Sshin 131353541Sshin switch (cmd) { 131453541Sshin case SIOCALIFADDR: 131553541Sshin { 131653541Sshin struct in6_aliasreq ifra; 131753541Sshin struct in6_addr *hostid = NULL; 131853541Sshin int prefixlen; 131953541Sshin 132053541Sshin if ((iflr->flags & IFLR_PREFIX) != 0) { 132153541Sshin struct sockaddr_in6 *sin6; 132253541Sshin 132353541Sshin /* 132453541Sshin * hostid is to fill in the hostid part of the 132553541Sshin * address. hostid points to the first link-local 132653541Sshin * address attached to the interface. 132753541Sshin */ 132862587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 132953541Sshin if (!ifa) 133053541Sshin return EADDRNOTAVAIL; 133153541Sshin hostid = IFA_IN6(ifa); 133253541Sshin 1333171260Sdelphij /* prefixlen must be <= 64. */ 133453541Sshin if (64 < iflr->prefixlen) 133553541Sshin return EINVAL; 133653541Sshin prefixlen = iflr->prefixlen; 133753541Sshin 133853541Sshin /* hostid part must be zero. */ 133953541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 1340126552Sume if (sin6->sin6_addr.s6_addr32[2] != 0 || 1341126552Sume sin6->sin6_addr.s6_addr32[3] != 0) { 134253541Sshin return EINVAL; 134353541Sshin } 134453541Sshin } else 134553541Sshin prefixlen = iflr->prefixlen; 134653541Sshin 134753541Sshin /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 134853541Sshin bzero(&ifra, sizeof(ifra)); 1349120891Sume bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 135053541Sshin 135162587Sitojun bcopy(&iflr->addr, &ifra.ifra_addr, 1352120891Sume ((struct sockaddr *)&iflr->addr)->sa_len); 135353541Sshin if (hostid) { 135453541Sshin /* fill in hostid part */ 135553541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1356120891Sume hostid->s6_addr32[2]; 135753541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1358120891Sume hostid->s6_addr32[3]; 135953541Sshin } 136053541Sshin 1361120891Sume if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 136253541Sshin bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1363120891Sume ((struct sockaddr *)&iflr->dstaddr)->sa_len); 136453541Sshin if (hostid) { 136553541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1366120891Sume hostid->s6_addr32[2]; 136753541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1368120891Sume hostid->s6_addr32[3]; 136953541Sshin } 137053541Sshin } 137153541Sshin 137253541Sshin ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1373121168Sume in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 137453541Sshin 137553541Sshin ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 137683366Sjulian return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 137753541Sshin } 137853541Sshin case SIOCGLIFADDR: 137953541Sshin case SIOCDLIFADDR: 138053541Sshin { 138153541Sshin struct in6_ifaddr *ia; 138253541Sshin struct in6_addr mask, candidate, match; 138353541Sshin struct sockaddr_in6 *sin6; 138453541Sshin int cmp; 138553541Sshin 138653541Sshin bzero(&mask, sizeof(mask)); 138753541Sshin if (iflr->flags & IFLR_PREFIX) { 138853541Sshin /* lookup a prefix rather than address. */ 1389121168Sume in6_prefixlen2mask(&mask, iflr->prefixlen); 139053541Sshin 139153541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 139253541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 139353541Sshin match.s6_addr32[0] &= mask.s6_addr32[0]; 139453541Sshin match.s6_addr32[1] &= mask.s6_addr32[1]; 139553541Sshin match.s6_addr32[2] &= mask.s6_addr32[2]; 139653541Sshin match.s6_addr32[3] &= mask.s6_addr32[3]; 139753541Sshin 139853541Sshin /* if you set extra bits, that's wrong */ 139953541Sshin if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 140053541Sshin return EINVAL; 140153541Sshin 140253541Sshin cmp = 1; 140353541Sshin } else { 140453541Sshin if (cmd == SIOCGLIFADDR) { 140553541Sshin /* on getting an address, take the 1st match */ 140695023Ssuz cmp = 0; /* XXX */ 140753541Sshin } else { 140853541Sshin /* on deleting an address, do exact match */ 1409121168Sume in6_prefixlen2mask(&mask, 128); 141053541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 141153541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 141253541Sshin 141353541Sshin cmp = 1; 141453541Sshin } 141553541Sshin } 141653541Sshin 1417191323Srwatson IF_ADDR_LOCK(ifp); 1418120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 141953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 142053541Sshin continue; 142153541Sshin if (!cmp) 142253541Sshin break; 142378064Sume 142478064Sume /* 142578064Sume * XXX: this is adhoc, but is necessary to allow 142678064Sume * a user to specify fe80::/64 (not /10) for a 142778064Sume * link-local address. 142878064Sume */ 1429148385Sume bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1430148385Sume in6_clearscope(&candidate); 143153541Sshin candidate.s6_addr32[0] &= mask.s6_addr32[0]; 143253541Sshin candidate.s6_addr32[1] &= mask.s6_addr32[1]; 143353541Sshin candidate.s6_addr32[2] &= mask.s6_addr32[2]; 143453541Sshin candidate.s6_addr32[3] &= mask.s6_addr32[3]; 143553541Sshin if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 143653541Sshin break; 143753541Sshin } 1438191323Srwatson IF_ADDR_UNLOCK(ifp); 143953541Sshin if (!ifa) 144053541Sshin return EADDRNOTAVAIL; 144153541Sshin ia = ifa2ia6(ifa); 144253541Sshin 144353541Sshin if (cmd == SIOCGLIFADDR) { 1444148385Sume int error; 144578064Sume 144653541Sshin /* fill in the if_laddrreq structure */ 144753541Sshin bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1448148385Sume error = sa6_recoverscope( 1449148385Sume (struct sockaddr_in6 *)&iflr->addr); 1450148385Sume if (error != 0) 1451148385Sume return (error); 1452148385Sume 145353541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 145453541Sshin bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1455120891Sume ia->ia_dstaddr.sin6_len); 1456148385Sume error = sa6_recoverscope( 1457148385Sume (struct sockaddr_in6 *)&iflr->dstaddr); 1458148385Sume if (error != 0) 1459148385Sume return (error); 146053541Sshin } else 146153541Sshin bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 146253541Sshin 146353541Sshin iflr->prefixlen = 1464120891Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 146553541Sshin 146695023Ssuz iflr->flags = ia->ia6_flags; /* XXX */ 146753541Sshin 146853541Sshin return 0; 146953541Sshin } else { 147053541Sshin struct in6_aliasreq ifra; 147153541Sshin 147253541Sshin /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 147353541Sshin bzero(&ifra, sizeof(ifra)); 147453541Sshin bcopy(iflr->iflr_name, ifra.ifra_name, 1475120891Sume sizeof(ifra.ifra_name)); 147653541Sshin 147753541Sshin bcopy(&ia->ia_addr, &ifra.ifra_addr, 1478120891Sume ia->ia_addr.sin6_len); 147953541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 148053541Sshin bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1481120891Sume ia->ia_dstaddr.sin6_len); 148262587Sitojun } else { 148362587Sitojun bzero(&ifra.ifra_dstaddr, 148462587Sitojun sizeof(ifra.ifra_dstaddr)); 148553541Sshin } 148653541Sshin bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1487120891Sume ia->ia_prefixmask.sin6_len); 148853541Sshin 148953541Sshin ifra.ifra_flags = ia->ia6_flags; 149053541Sshin return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1491120891Sume ifp, td); 149253541Sshin } 149353541Sshin } 149453541Sshin } 149553541Sshin 149695023Ssuz return EOPNOTSUPP; /* just for safety */ 149753541Sshin} 149853541Sshin 149953541Sshin/* 150053541Sshin * Initialize an interface's intetnet6 address 150153541Sshin * and routing table entry. 150253541Sshin */ 150378064Sumestatic int 1504171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1505171259Sdelphij struct sockaddr_in6 *sin6, int newhost) 150653541Sshin{ 150778064Sume int error = 0, plen, ifacount = 0; 150853541Sshin int s = splimp(); 150978064Sume struct ifaddr *ifa; 151053541Sshin 151153541Sshin /* 151253541Sshin * Give the interface a chance to initialize 151353541Sshin * if this is its first address, 151453541Sshin * and to validate the address if necessary. 151553541Sshin */ 1516191323Srwatson IF_ADDR_LOCK(ifp); 1517120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 151878064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 151978064Sume continue; 152078064Sume ifacount++; 152178064Sume } 1522191323Srwatson IF_ADDR_UNLOCK(ifp); 152378064Sume 152478064Sume ia->ia_addr = *sin6; 152578064Sume 1526146883Siedowse if (ifacount <= 1 && ifp->if_ioctl) { 1527146883Siedowse error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1528146883Siedowse if (error) { 1529146883Siedowse splx(s); 1530146883Siedowse return (error); 1531146883Siedowse } 153253541Sshin } 153378064Sume splx(s); 153453541Sshin 153578064Sume ia->ia_ifa.ifa_metric = ifp->if_metric; 153653541Sshin 153778064Sume /* we could do in(6)_socktrim here, but just omit it at this moment. */ 153878064Sume 153953541Sshin /* 154078064Sume * Special case: 1541124337Sume * If a new destination address is specified for a point-to-point 154278064Sume * interface, install a route to the destination as an interface 1543186119Sqingli * direct route. 1544124337Sume * XXX: the logic below rejects assigning multiple addresses on a p2p 1545159390Sgnn * interface that share the same destination. 154653541Sshin */ 154778064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1548186708Sqingli if (!(ia->ia_flags & IFA_ROUTE) && plen == 128) { 1549186708Sqingli struct sockaddr *dstaddr; 1550159390Sgnn int rtflags = RTF_UP | RTF_HOST; 1551159390Sgnn 1552186708Sqingli /* 1553186708Sqingli * use the interface address if configuring an 1554186708Sqingli * interface address with a /128 prefix len 1555186708Sqingli */ 1556186708Sqingli if (ia->ia_dstaddr.sin6_family == AF_INET6) 1557186708Sqingli dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 1558186708Sqingli else 1559186708Sqingli dstaddr = (struct sockaddr *)&ia->ia_addr; 1560159390Sgnn 1561178888Sjulian error = rtrequest(RTM_ADD, 1562186708Sqingli (struct sockaddr *)dstaddr, 1563159390Sgnn (struct sockaddr *)&ia->ia_addr, 1564159390Sgnn (struct sockaddr *)&ia->ia_prefixmask, 1565186708Sqingli ia->ia_flags | rtflags, NULL); 1566159390Sgnn if (error != 0) 1567120856Sume return (error); 156878064Sume ia->ia_flags |= IFA_ROUTE; 156953541Sshin } 157053541Sshin 157195023Ssuz /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1572186119Sqingli if (newhost) { 1573186119Sqingli struct llentry *ln; 157453541Sshin 1575186119Sqingli IF_AFDATA_LOCK(ifp); 1576186119Sqingli ia->ia_ifa.ifa_rtrequest = NULL; 1577186119Sqingli 1578186119Sqingli /* XXX QL 1579186119Sqingli * we need to report rt_newaddrmsg 1580186119Sqingli */ 1581186119Sqingli ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE), 1582186119Sqingli (struct sockaddr *)&ia->ia_addr); 1583186119Sqingli IF_AFDATA_UNLOCK(ifp); 1584186158Skmacy if (ln != NULL) { 1585186119Sqingli ln->la_expire = 0; /* for IPv6 this means permanent */ 1586186119Sqingli ln->ln_state = ND6_LLINFO_REACHABLE; 1587186119Sqingli LLE_WUNLOCK(ln); 1588186119Sqingli } 1589186119Sqingli } 1590186119Sqingli 1591120856Sume return (error); 159253541Sshin} 159353541Sshin 1594142215Sglebiusstruct in6_multi_mship * 1595171259Sdelphijin6_joingroup(struct ifnet *ifp, struct in6_addr *addr, 1596171259Sdelphij int *errorp, int delay) 1597142215Sglebius{ 1598142215Sglebius struct in6_multi_mship *imm; 1599142215Sglebius 1600149849Sobrien imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT); 1601142215Sglebius if (!imm) { 1602142215Sglebius *errorp = ENOBUFS; 1603142215Sglebius return NULL; 1604142215Sglebius } 1605151539Ssuz imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay); 1606142215Sglebius if (!imm->i6mm_maddr) { 1607142215Sglebius /* *errorp is alrady set */ 1608149849Sobrien free(imm, M_IP6MADDR); 1609142215Sglebius return NULL; 1610142215Sglebius } 1611142215Sglebius return imm; 1612142215Sglebius} 1613142215Sglebius 1614142215Sglebiusint 1615171259Sdelphijin6_leavegroup(struct in6_multi_mship *imm) 1616142215Sglebius{ 1617142215Sglebius 1618142215Sglebius if (imm->i6mm_maddr) 1619142215Sglebius in6_delmulti(imm->i6mm_maddr); 1620149849Sobrien free(imm, M_IP6MADDR); 1621142215Sglebius return 0; 1622142215Sglebius} 1623142215Sglebius 162453541Sshin/* 162553541Sshin * Find an IPv6 interface link-local address specific to an interface. 162653541Sshin */ 162753541Sshinstruct in6_ifaddr * 1628171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 162953541Sshin{ 163078064Sume struct ifaddr *ifa; 163153541Sshin 1632191323Srwatson IF_ADDR_LOCK(ifp); 1633120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 163453541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 163553541Sshin continue; 163662587Sitojun if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 163762587Sitojun if ((((struct in6_ifaddr *)ifa)->ia6_flags & 163862587Sitojun ignoreflags) != 0) 163962587Sitojun continue; 164053541Sshin break; 164162587Sitojun } 164253541Sshin } 1643191323Srwatson IF_ADDR_UNLOCK(ifp); 164453541Sshin 1645120856Sume return ((struct in6_ifaddr *)ifa); 164653541Sshin} 164753541Sshin 164853541Sshin 164953541Sshin/* 165053541Sshin * find the internet address corresponding to a given interface and address. 165153541Sshin */ 165253541Sshinstruct in6_ifaddr * 1653171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 165453541Sshin{ 165578064Sume struct ifaddr *ifa; 165653541Sshin 1657120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 165853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 165953541Sshin continue; 166053541Sshin if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 166153541Sshin break; 166253541Sshin } 166353541Sshin 1664120856Sume return ((struct in6_ifaddr *)ifa); 166553541Sshin} 166653541Sshin 166753541Sshin/* 1668165118Sbz * Convert IP6 address to printable (loggable) representation. Caller 1669165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 167053541Sshin */ 167153541Sshinstatic char digits[] = "0123456789abcdef"; 167253541Sshinchar * 1673165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr) 167453541Sshin{ 167578064Sume int i; 167678064Sume char *cp; 1677126552Sume const u_int16_t *a = (const u_int16_t *)addr; 1678126552Sume const u_int8_t *d; 1679165287Sbz int dcolon = 0, zero = 0; 168053541Sshin 1681165118Sbz cp = ip6buf; 168253541Sshin 168353541Sshin for (i = 0; i < 8; i++) { 168453541Sshin if (dcolon == 1) { 168553541Sshin if (*a == 0) { 168653541Sshin if (i == 7) 168753541Sshin *cp++ = ':'; 168853541Sshin a++; 168953541Sshin continue; 169053541Sshin } else 169153541Sshin dcolon = 2; 169253541Sshin } 169353541Sshin if (*a == 0) { 169453541Sshin if (dcolon == 0 && *(a + 1) == 0) { 169553541Sshin if (i == 0) 169653541Sshin *cp++ = ':'; 169753541Sshin *cp++ = ':'; 169853541Sshin dcolon = 1; 169953541Sshin } else { 170053541Sshin *cp++ = '0'; 170153541Sshin *cp++ = ':'; 170253541Sshin } 170353541Sshin a++; 170453541Sshin continue; 170553541Sshin } 170691346Salfred d = (const u_char *)a; 1707165287Sbz /* Try to eliminate leading zeros in printout like in :0001. */ 1708165287Sbz zero = 1; 1709165287Sbz *cp = digits[*d >> 4]; 1710165287Sbz if (*cp != '0') { 1711165287Sbz zero = 0; 1712165287Sbz cp++; 1713165287Sbz } 1714165287Sbz *cp = digits[*d++ & 0xf]; 1715165287Sbz if (zero == 0 || (*cp != '0')) { 1716165287Sbz zero = 0; 1717165287Sbz cp++; 1718165287Sbz } 1719165287Sbz *cp = digits[*d >> 4]; 1720165287Sbz if (zero == 0 || (*cp != '0')) { 1721165287Sbz zero = 0; 1722165287Sbz cp++; 1723165287Sbz } 172453541Sshin *cp++ = digits[*d & 0xf]; 172553541Sshin *cp++ = ':'; 172653541Sshin a++; 172753541Sshin } 1728165118Sbz *--cp = '\0'; 1729165118Sbz return (ip6buf); 173053541Sshin} 173153541Sshin 173253541Sshinint 1733171259Sdelphijin6_localaddr(struct in6_addr *in6) 173453541Sshin{ 1735183550Szec INIT_VNET_INET6(curvnet); 173653541Sshin struct in6_ifaddr *ia; 173753541Sshin 173853541Sshin if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 173953541Sshin return 1; 174053541Sshin 1741181803Sbz for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) { 174253541Sshin if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1743120891Sume &ia->ia_prefixmask.sin6_addr)) { 174453541Sshin return 1; 1745120891Sume } 1746120891Sume } 174753541Sshin 174853541Sshin return (0); 174953541Sshin} 175053541Sshin 175178064Sumeint 1752171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6) 175378064Sume{ 1754183550Szec INIT_VNET_INET6(curvnet); 175578064Sume struct in6_ifaddr *ia; 175678064Sume 1757181803Sbz for (ia = V_in6_ifaddr; ia; ia = ia->ia_next) { 175878064Sume if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 175978064Sume &sa6->sin6_addr) && 176078064Sume (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) 1761120856Sume return (1); /* true */ 176278064Sume 176378064Sume /* XXX: do we still have to go thru the rest of the list? */ 176478064Sume } 176578064Sume 1766120856Sume return (0); /* false */ 176778064Sume} 176878064Sume 176953541Sshin/* 177053541Sshin * return length of part which dst and src are equal 177153541Sshin * hard coding... 177253541Sshin */ 177353541Sshinint 1774171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst) 177553541Sshin{ 177653541Sshin int match = 0; 177753541Sshin u_char *s = (u_char *)src, *d = (u_char *)dst; 177853541Sshin u_char *lim = s + 16, r; 177953541Sshin 178053541Sshin while (s < lim) 178153541Sshin if ((r = (*d++ ^ *s++)) != 0) { 178253541Sshin while (r < 128) { 178353541Sshin match++; 178453541Sshin r <<= 1; 178553541Sshin } 178653541Sshin break; 178753541Sshin } else 178853541Sshin match += 8; 178953541Sshin return match; 179053541Sshin} 179153541Sshin 179262587Sitojun/* XXX: to be scope conscious */ 179353541Sshinint 1794171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 179553541Sshin{ 179653541Sshin int bytelen, bitlen; 179753541Sshin 179853541Sshin /* sanity check */ 179953541Sshin if (0 > len || len > 128) { 180053541Sshin log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 180153541Sshin len); 1802120856Sume return (0); 180353541Sshin } 180453541Sshin 180553541Sshin bytelen = len / 8; 180653541Sshin bitlen = len % 8; 180753541Sshin 180853541Sshin if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1809120856Sume return (0); 1810126184Scperciva if (bitlen != 0 && 1811126184Scperciva p1->s6_addr[bytelen] >> (8 - bitlen) != 181253541Sshin p2->s6_addr[bytelen] >> (8 - bitlen)) 1813120856Sume return (0); 181453541Sshin 1815120856Sume return (1); 181653541Sshin} 181753541Sshin 181853541Sshinvoid 1819171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len) 182053541Sshin{ 182153541Sshin u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 182253541Sshin int bytelen, bitlen, i; 182353541Sshin 182453541Sshin /* sanity check */ 182553541Sshin if (0 > len || len > 128) { 182653541Sshin log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 182753541Sshin len); 182853541Sshin return; 182953541Sshin } 183053541Sshin 183153541Sshin bzero(maskp, sizeof(*maskp)); 183253541Sshin bytelen = len / 8; 183353541Sshin bitlen = len % 8; 183453541Sshin for (i = 0; i < bytelen; i++) 183553541Sshin maskp->s6_addr[i] = 0xff; 183653541Sshin if (bitlen) 183753541Sshin maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 183853541Sshin} 183953541Sshin 184053541Sshin/* 184153541Sshin * return the best address out of the same scope. if no address was 184253541Sshin * found, return the first valid address from designated IF. 184353541Sshin */ 184453541Sshinstruct in6_ifaddr * 1845171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 184653541Sshin{ 1847183550Szec INIT_VNET_INET6(curvnet); 184853541Sshin int dst_scope = in6_addrscope(dst), blen = -1, tlen; 184953541Sshin struct ifaddr *ifa; 185053541Sshin struct in6_ifaddr *besta = 0; 185195023Ssuz struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 185253541Sshin 185353541Sshin dep[0] = dep[1] = NULL; 185453541Sshin 185553541Sshin /* 185653541Sshin * We first look for addresses in the same scope. 185753541Sshin * If there is one, return it. 185853541Sshin * If two or more, return one which matches the dst longest. 185953541Sshin * If none, return one of global addresses assigned other ifs. 186053541Sshin */ 1861191323Srwatson IF_ADDR_LOCK(ifp); 1862120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 186353541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 186453541Sshin continue; 186553541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 186653541Sshin continue; /* XXX: is there any case to allow anycast? */ 186753541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 186853541Sshin continue; /* don't use this interface */ 186953541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 187053541Sshin continue; 187153541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1872181803Sbz if (V_ip6_use_deprecated) 187353541Sshin dep[0] = (struct in6_ifaddr *)ifa; 187453541Sshin continue; 187553541Sshin } 187653541Sshin 187753541Sshin if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 187853541Sshin /* 187953541Sshin * call in6_matchlen() as few as possible 188053541Sshin */ 188153541Sshin if (besta) { 188253541Sshin if (blen == -1) 188353541Sshin blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 188453541Sshin tlen = in6_matchlen(IFA_IN6(ifa), dst); 188553541Sshin if (tlen > blen) { 188653541Sshin blen = tlen; 188753541Sshin besta = (struct in6_ifaddr *)ifa; 188853541Sshin } 188953541Sshin } else 189053541Sshin besta = (struct in6_ifaddr *)ifa; 189153541Sshin } 189253541Sshin } 1893191323Srwatson if (besta) { 1894191323Srwatson IF_ADDR_UNLOCK(ifp); 1895120856Sume return (besta); 1896191323Srwatson } 189753541Sshin 1898120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 189953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 190053541Sshin continue; 190153541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 190253541Sshin continue; /* XXX: is there any case to allow anycast? */ 190353541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 190453541Sshin continue; /* don't use this interface */ 190553541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 190653541Sshin continue; 190753541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1908181803Sbz if (V_ip6_use_deprecated) 190953541Sshin dep[1] = (struct in6_ifaddr *)ifa; 191053541Sshin continue; 191153541Sshin } 191253541Sshin 1913191323Srwatson IF_ADDR_UNLOCK(ifp); 191453541Sshin return (struct in6_ifaddr *)ifa; 191553541Sshin } 1916191323Srwatson IF_ADDR_UNLOCK(ifp); 191753541Sshin 191853541Sshin /* use the last-resort values, that are, deprecated addresses */ 191953541Sshin if (dep[0]) 192053541Sshin return dep[0]; 192153541Sshin if (dep[1]) 192253541Sshin return dep[1]; 192353541Sshin 192453541Sshin return NULL; 192553541Sshin} 192653541Sshin 192753541Sshin/* 192853541Sshin * perform DAD when interface becomes IFF_UP. 192953541Sshin */ 193053541Sshinvoid 1931171259Sdelphijin6_if_up(struct ifnet *ifp) 193253541Sshin{ 193353541Sshin struct ifaddr *ifa; 193453541Sshin struct in6_ifaddr *ia; 193553541Sshin 1936191323Srwatson IF_ADDR_LOCK(ifp); 1937120891Sume TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 193853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 193953541Sshin continue; 194053541Sshin ia = (struct in6_ifaddr *)ifa; 1941151539Ssuz if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 1942151539Ssuz /* 1943151539Ssuz * The TENTATIVE flag was likely set by hand 1944151539Ssuz * beforehand, implicitly indicating the need for DAD. 1945151539Ssuz * We may be able to skip the random delay in this 1946151539Ssuz * case, but we impose delays just in case. 1947151539Ssuz */ 1948151539Ssuz nd6_dad_start(ifa, 1949151539Ssuz arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 1950151539Ssuz } 195153541Sshin } 1952191323Srwatson IF_ADDR_UNLOCK(ifp); 1953151539Ssuz 1954151539Ssuz /* 1955151539Ssuz * special cases, like 6to4, are handled in in6_ifattach 1956151539Ssuz */ 1957151539Ssuz in6_ifattach(ifp, NULL); 195853541Sshin} 195953541Sshin 196078064Sumeint 1961171259Sdelphijin6if_do_dad(struct ifnet *ifp) 196278064Sume{ 196378064Sume if ((ifp->if_flags & IFF_LOOPBACK) != 0) 1964120856Sume return (0); 196578064Sume 196678064Sume switch (ifp->if_type) { 196778064Sume#ifdef IFT_DUMMY 196878064Sume case IFT_DUMMY: 196978064Sume#endif 197078064Sume case IFT_FAITH: 197178064Sume /* 197278064Sume * These interfaces do not have the IFF_LOOPBACK flag, 197378064Sume * but loop packets back. We do not have to do DAD on such 197478064Sume * interfaces. We should even omit it, because loop-backed 197578064Sume * NS would confuse the DAD procedure. 197678064Sume */ 1977120856Sume return (0); 197878064Sume default: 197978064Sume /* 198078064Sume * Our DAD routine requires the interface up and running. 198178064Sume * However, some interfaces can be up before the RUNNING 198278064Sume * status. Additionaly, users may try to assign addresses 198378064Sume * before the interface becomes up (or running). 198478064Sume * We simply skip DAD in such a case as a work around. 198578064Sume * XXX: we should rather mark "tentative" on such addresses, 198678064Sume * and do DAD after the interface becomes ready. 198778064Sume */ 1988148887Srwatson if (!((ifp->if_flags & IFF_UP) && 1989148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 1990120856Sume return (0); 199178064Sume 1992120856Sume return (1); 199378064Sume } 199478064Sume} 199578064Sume 199653541Sshin/* 199753541Sshin * Calculate max IPv6 MTU through all the interfaces and store it 199853541Sshin * to in6_maxmtu. 199953541Sshin */ 200053541Sshinvoid 2001171259Sdelphijin6_setmaxmtu(void) 200253541Sshin{ 2003183550Szec INIT_VNET_NET(curvnet); 2004183550Szec INIT_VNET_INET6(curvnet); 200553541Sshin unsigned long maxmtu = 0; 200653541Sshin struct ifnet *ifp; 200753541Sshin 2008108172Shsu IFNET_RLOCK(); 2009181887Sjulian for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 2010181888Sjulian ifp = TAILQ_NEXT(ifp, if_list)) { 2011121283Sume /* this function can be called during ifnet initialization */ 2012121283Sume if (!ifp->if_afdata[AF_INET6]) 2013121283Sume continue; 201453541Sshin if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2015121283Sume IN6_LINKMTU(ifp) > maxmtu) 2016121283Sume maxmtu = IN6_LINKMTU(ifp); 201753541Sshin } 2018108172Shsu IFNET_RUNLOCK(); 2019120891Sume if (maxmtu) /* update only when maxmtu is positive */ 2020181803Sbz V_in6_maxmtu = maxmtu; 202153541Sshin} 202253541Sshin 2023151539Ssuz/* 2024151539Ssuz * Provide the length of interface identifiers to be used for the link attached 2025151539Ssuz * to the given interface. The length should be defined in "IPv6 over 2026151539Ssuz * xxx-link" document. Note that address architecture might also define 2027151539Ssuz * the length for a particular set of address prefixes, regardless of the 2028151539Ssuz * link type. As clarified in rfc2462bis, those two definitions should be 2029151539Ssuz * consistent, and those really are as of August 2004. 2030151539Ssuz */ 2031151539Ssuzint 2032171259Sdelphijin6_if2idlen(struct ifnet *ifp) 2033151539Ssuz{ 2034151539Ssuz switch (ifp->if_type) { 2035151539Ssuz case IFT_ETHER: /* RFC2464 */ 2036151539Ssuz#ifdef IFT_PROPVIRTUAL 2037151539Ssuz case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2038151539Ssuz#endif 2039151539Ssuz#ifdef IFT_L2VLAN 2040151539Ssuz case IFT_L2VLAN: /* ditto */ 2041151539Ssuz#endif 2042151539Ssuz#ifdef IFT_IEEE80211 2043151539Ssuz case IFT_IEEE80211: /* ditto */ 2044151539Ssuz#endif 2045151539Ssuz#ifdef IFT_MIP 2046151539Ssuz case IFT_MIP: /* ditto */ 2047151539Ssuz#endif 2048151539Ssuz return (64); 2049151539Ssuz case IFT_FDDI: /* RFC2467 */ 2050151539Ssuz return (64); 2051151539Ssuz case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2052151539Ssuz return (64); 2053151539Ssuz case IFT_PPP: /* RFC2472 */ 2054151539Ssuz return (64); 2055151539Ssuz case IFT_ARCNET: /* RFC2497 */ 2056151539Ssuz return (64); 2057151539Ssuz case IFT_FRELAY: /* RFC2590 */ 2058151539Ssuz return (64); 2059151539Ssuz case IFT_IEEE1394: /* RFC3146 */ 2060151539Ssuz return (64); 2061151539Ssuz case IFT_GIF: 2062151539Ssuz return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2063151539Ssuz case IFT_LOOP: 2064151539Ssuz return (64); /* XXX: is this really correct? */ 2065151539Ssuz default: 2066151539Ssuz /* 2067151539Ssuz * Unknown link type: 2068151539Ssuz * It might be controversial to use the today's common constant 2069151539Ssuz * of 64 for these cases unconditionally. For full compliance, 2070151539Ssuz * we should return an error in this case. On the other hand, 2071151539Ssuz * if we simply miss the standard for the link type or a new 2072151539Ssuz * standard is defined for a new link type, the IFID length 2073151539Ssuz * is very likely to be the common constant. As a compromise, 2074151539Ssuz * we always use the constant, but make an explicit notice 2075151539Ssuz * indicating the "unknown" case. 2076151539Ssuz */ 2077151539Ssuz printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2078151539Ssuz return (64); 2079151539Ssuz } 2080151539Ssuz} 2081151539Ssuz 2082186119Sqingli#include <sys/sysctl.h> 2083186119Sqingli 2084186119Sqinglistruct in6_llentry { 2085186119Sqingli struct llentry base; 2086186119Sqingli struct sockaddr_in6 l3_addr6; 2087186119Sqingli}; 2088186119Sqingli 2089186119Sqinglistatic struct llentry * 2090186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2091186119Sqingli{ 2092186119Sqingli struct in6_llentry *lle; 2093186119Sqingli 2094186119Sqingli lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, 2095186119Sqingli M_DONTWAIT | M_ZERO); 2096186119Sqingli if (lle == NULL) /* NB: caller generates msg */ 2097186119Sqingli return NULL; 2098186119Sqingli 2099186119Sqingli callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE); 2100186119Sqingli lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2101186119Sqingli lle->base.lle_refcnt = 1; 2102186119Sqingli LLE_LOCK_INIT(&lle->base); 2103186119Sqingli return &lle->base; 2104186119Sqingli} 2105186119Sqingli 2106186119Sqingli/* 2107186119Sqingli * Deletes an address from the address table. 2108186119Sqingli * This function is called by the timer functions 2109186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and 2110186119Sqingli * the caller does the locking. 2111186119Sqingli */ 2112186119Sqinglistatic void 2113186119Sqingliin6_lltable_free(struct lltable *llt, struct llentry *lle) 2114186119Sqingli{ 2115186150Skmacy LLE_WUNLOCK(lle); 2116186150Skmacy LLE_LOCK_DESTROY(lle); 2117186119Sqingli free(lle, M_LLTABLE); 2118186119Sqingli} 2119186119Sqingli 2120186119Sqinglistatic int 2121186119Sqingliin6_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 2122186119Sqingli{ 2123186119Sqingli struct rtentry *rt; 2124186119Sqingli char ip6buf[INET6_ADDRSTRLEN]; 2125186119Sqingli 2126186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2127186119Sqingli ("sin_family %d", l3addr->sa_family)); 2128186119Sqingli 2129186119Sqingli /* XXX rtalloc1 should take a const param */ 2130186119Sqingli rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 2131186119Sqingli if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2132186119Sqingli struct ifaddr *ifa; 2133186119Sqingli /* 2134186119Sqingli * Create an ND6 cache for an IPv6 neighbor 2135186119Sqingli * that is not covered by our own prefix. 2136186119Sqingli */ 2137186119Sqingli /* XXX ifaof_ifpforaddr should take a const param */ 2138186119Sqingli ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2139186119Sqingli if (ifa != NULL) { 2140186119Sqingli if (rt != NULL) 2141187946Sbz RTFREE_LOCKED(rt); 2142186119Sqingli return 0; 2143186119Sqingli } 2144186119Sqingli log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2145186119Sqingli ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2146186119Sqingli if (rt != NULL) 2147187946Sbz RTFREE_LOCKED(rt); 2148186119Sqingli return EINVAL; 2149186119Sqingli } 2150187946Sbz RTFREE_LOCKED(rt); 2151186119Sqingli return 0; 2152186119Sqingli} 2153186119Sqingli 2154186119Sqinglistatic struct llentry * 2155186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags, 2156186119Sqingli const struct sockaddr *l3addr) 2157186119Sqingli{ 2158186119Sqingli const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2159186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2160186119Sqingli struct llentry *lle; 2161186119Sqingli struct llentries *lleh; 2162186119Sqingli u_int hashkey; 2163186119Sqingli 2164186119Sqingli IF_AFDATA_LOCK_ASSERT(ifp); 2165186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2166186119Sqingli ("sin_family %d", l3addr->sa_family)); 2167186119Sqingli 2168186119Sqingli hashkey = sin6->sin6_addr.s6_addr32[3]; 2169186119Sqingli lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2170186119Sqingli LIST_FOREACH(lle, lleh, lle_next) { 2171186708Sqingli struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle); 2172186119Sqingli if (lle->la_flags & LLE_DELETED) 2173186119Sqingli continue; 2174186708Sqingli if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr, 2175186708Sqingli sizeof(struct in6_addr)) == 0) 2176186119Sqingli break; 2177186119Sqingli } 2178186119Sqingli 2179186119Sqingli if (lle == NULL) { 2180186119Sqingli if (!(flags & LLE_CREATE)) 2181186119Sqingli return (NULL); 2182186119Sqingli /* 2183186119Sqingli * A route that covers the given address must have 2184186119Sqingli * been installed 1st because we are doing a resolution, 2185186119Sqingli * verify this. 2186186119Sqingli */ 2187186119Sqingli if (!(flags & LLE_IFADDR) && 2188186119Sqingli in6_lltable_rtcheck(ifp, l3addr) != 0) 2189186119Sqingli return NULL; 2190186119Sqingli 2191186119Sqingli lle = in6_lltable_new(l3addr, flags); 2192186119Sqingli if (lle == NULL) { 2193186119Sqingli log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2194186119Sqingli return NULL; 2195186119Sqingli } 2196186119Sqingli lle->la_flags = flags & ~LLE_CREATE; 2197186119Sqingli if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2198186119Sqingli bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2199186119Sqingli lle->la_flags |= (LLE_VALID | LLE_STATIC); 2200186119Sqingli } 2201186119Sqingli 2202186119Sqingli lle->lle_tbl = llt; 2203186119Sqingli lle->lle_head = lleh; 2204186119Sqingli LIST_INSERT_HEAD(lleh, lle, lle_next); 2205186119Sqingli } else if (flags & LLE_DELETE) { 2206186392Sqingli if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 2207186392Sqingli LLE_WLOCK(lle); 2208186392Sqingli lle->la_flags = LLE_DELETED; 2209186392Sqingli LLE_WUNLOCK(lle); 2210186119Sqingli#ifdef DIAGNOSTICS 2211186392Sqingli log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2212186119Sqingli#endif 2213186392Sqingli } 2214186119Sqingli lle = (void *)-1; 2215186119Sqingli } 2216186119Sqingli if (LLE_IS_VALID(lle)) { 2217186119Sqingli if (flags & LLE_EXCLUSIVE) 2218186119Sqingli LLE_WLOCK(lle); 2219186119Sqingli else 2220186119Sqingli LLE_RLOCK(lle); 2221186119Sqingli } 2222186119Sqingli return (lle); 2223186119Sqingli} 2224186119Sqingli 2225186119Sqinglistatic int 2226186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2227186119Sqingli{ 2228186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2229186119Sqingli struct llentry *lle; 2230186119Sqingli /* XXX stack use */ 2231186119Sqingli struct { 2232186119Sqingli struct rt_msghdr rtm; 2233186119Sqingli struct sockaddr_in6 sin6; 2234186119Sqingli /* 2235186119Sqingli * ndp.c assumes that sdl is word aligned 2236186119Sqingli */ 2237186119Sqingli#ifdef __LP64__ 2238186119Sqingli uint32_t pad; 2239186119Sqingli#endif 2240186119Sqingli struct sockaddr_dl sdl; 2241186119Sqingli } ndpc; 2242186119Sqingli int i, error; 2243186119Sqingli 2244186119Sqingli /* XXXXX 2245186119Sqingli * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 2246186119Sqingli * so it is okay to use this ASSERT, change it when 2247186119Sqingli * IFNET lock is finalized 2248186119Sqingli */ 2249186119Sqingli IFNET_WLOCK_ASSERT(); 2250186119Sqingli 2251186119Sqingli error = 0; 2252186119Sqingli for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2253186119Sqingli LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2254186119Sqingli struct sockaddr_dl *sdl; 2255186119Sqingli 2256186119Sqingli /* skip deleted or invalid entries */ 2257186119Sqingli if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2258186119Sqingli continue; 2259186980Sbz /* Skip if jailed and not a valid IP of the prison. */ 2260188144Sjamie if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 2261186980Sbz continue; 2262186119Sqingli /* 2263186119Sqingli * produce a msg made of: 2264186119Sqingli * struct rt_msghdr; 2265186119Sqingli * struct sockaddr_in6 (IPv6) 2266186119Sqingli * struct sockaddr_dl; 2267186119Sqingli */ 2268186119Sqingli bzero(&ndpc, sizeof(ndpc)); 2269186119Sqingli ndpc.rtm.rtm_msglen = sizeof(ndpc); 2270187094Sqingli ndpc.rtm.rtm_version = RTM_VERSION; 2271187094Sqingli ndpc.rtm.rtm_type = RTM_GET; 2272187094Sqingli ndpc.rtm.rtm_flags = RTF_UP; 2273187094Sqingli ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2274186119Sqingli ndpc.sin6.sin6_family = AF_INET6; 2275186119Sqingli ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2276186119Sqingli bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2277186119Sqingli 2278186119Sqingli /* publish */ 2279186119Sqingli if (lle->la_flags & LLE_PUB) 2280186119Sqingli ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2281186119Sqingli 2282186119Sqingli sdl = &ndpc.sdl; 2283186119Sqingli sdl->sdl_family = AF_LINK; 2284186119Sqingli sdl->sdl_len = sizeof(*sdl); 2285186119Sqingli sdl->sdl_alen = ifp->if_addrlen; 2286186119Sqingli sdl->sdl_index = ifp->if_index; 2287186119Sqingli sdl->sdl_type = ifp->if_type; 2288186119Sqingli bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2289186119Sqingli ndpc.rtm.rtm_rmx.rmx_expire = 2290186119Sqingli lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2291186500Sqingli ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2292186119Sqingli if (lle->la_flags & LLE_STATIC) 2293186119Sqingli ndpc.rtm.rtm_flags |= RTF_STATIC; 2294186119Sqingli ndpc.rtm.rtm_index = ifp->if_index; 2295186119Sqingli error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2296186119Sqingli if (error) 2297186119Sqingli break; 2298186119Sqingli } 2299186119Sqingli } 2300186119Sqingli return error; 2301186119Sqingli} 2302186119Sqingli 2303121161Sumevoid * 2304171259Sdelphijin6_domifattach(struct ifnet *ifp) 2305121161Sume{ 2306121161Sume struct in6_ifextra *ext; 2307121161Sume 2308121161Sume ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2309121161Sume bzero(ext, sizeof(*ext)); 2310121161Sume 2311121161Sume ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2312121161Sume M_IFADDR, M_WAITOK); 2313121161Sume bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2314121161Sume 2315121161Sume ext->icmp6_ifstat = 2316121161Sume (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2317121161Sume M_IFADDR, M_WAITOK); 2318121161Sume bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2319121161Sume 2320121161Sume ext->nd_ifinfo = nd6_ifattach(ifp); 2321121161Sume ext->scope6_id = scope6_ifattach(ifp); 2322186119Sqingli ext->lltable = lltable_init(ifp, AF_INET6); 2323186119Sqingli if (ext->lltable != NULL) { 2324186119Sqingli ext->lltable->llt_new = in6_lltable_new; 2325186119Sqingli ext->lltable->llt_free = in6_lltable_free; 2326186119Sqingli ext->lltable->llt_rtcheck = in6_lltable_rtcheck; 2327186119Sqingli ext->lltable->llt_lookup = in6_lltable_lookup; 2328186119Sqingli ext->lltable->llt_dump = in6_lltable_dump; 2329186119Sqingli } 2330121161Sume return ext; 2331121161Sume} 2332121161Sume 2333121161Sumevoid 2334171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux) 2335121161Sume{ 2336121161Sume struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2337121161Sume 2338121161Sume scope6_ifdetach(ext->scope6_id); 2339121161Sume nd6_ifdetach(ext->nd_ifinfo); 2340186119Sqingli lltable_free(ext->lltable); 2341121161Sume free(ext->in6_ifstat, M_IFADDR); 2342121161Sume free(ext->icmp6_ifstat, M_IFADDR); 2343121161Sume free(ext, M_IFADDR); 2344121161Sume} 2345121161Sume 234653541Sshin/* 234795023Ssuz * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 234853541Sshin * v4 mapped addr or v4 compat addr 234953541Sshin */ 235053541Sshinvoid 235153541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 235253541Sshin{ 2353171259Sdelphij 235453541Sshin bzero(sin, sizeof(*sin)); 235553541Sshin sin->sin_len = sizeof(struct sockaddr_in); 235653541Sshin sin->sin_family = AF_INET; 235753541Sshin sin->sin_port = sin6->sin6_port; 2358120891Sume sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 235953541Sshin} 236053541Sshin 236153541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 236253541Sshinvoid 236353541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 236453541Sshin{ 236553541Sshin bzero(sin6, sizeof(*sin6)); 236653541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 236753541Sshin sin6->sin6_family = AF_INET6; 236853541Sshin sin6->sin6_port = sin->sin_port; 236953541Sshin sin6->sin6_addr.s6_addr32[0] = 0; 237053541Sshin sin6->sin6_addr.s6_addr32[1] = 0; 237153541Sshin sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 237253541Sshin sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 237353541Sshin} 237453541Sshin 237553541Sshin/* Convert sockaddr_in6 into sockaddr_in. */ 237653541Sshinvoid 237753541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam) 237853541Sshin{ 237953541Sshin struct sockaddr_in *sin_p; 238053541Sshin struct sockaddr_in6 sin6; 238153541Sshin 238253541Sshin /* 238353541Sshin * Save original sockaddr_in6 addr and convert it 238453541Sshin * to sockaddr_in. 238553541Sshin */ 238653541Sshin sin6 = *(struct sockaddr_in6 *)nam; 238753541Sshin sin_p = (struct sockaddr_in *)nam; 238853541Sshin in6_sin6_2_sin(sin_p, &sin6); 238953541Sshin} 239053541Sshin 239153541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 239253541Sshinvoid 239353541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 239453541Sshin{ 239553541Sshin struct sockaddr_in *sin_p; 239653541Sshin struct sockaddr_in6 *sin6_p; 239753541Sshin 2398184205Sdes sin6_p = malloc(sizeof *sin6_p, M_SONAME, 2399111119Simp M_WAITOK); 240053541Sshin sin_p = (struct sockaddr_in *)*nam; 240153541Sshin in6_sin_2_v4mapsin6(sin_p, sin6_p); 2402184205Sdes free(*nam, M_SONAME); 240353541Sshin *nam = (struct sockaddr *)sin6_p; 240453541Sshin} 2405