in6.c revision 197227
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 197227 2009-09-15 19:18:34Z qingli $"); 65174510Sobrien 6662587Sitojun#include "opt_inet.h" 6762587Sitojun#include "opt_inet6.h" 6862587Sitojun 6953541Sshin#include <sys/param.h> 7053541Sshin#include <sys/errno.h> 71186948Sbz#include <sys/jail.h> 7253541Sshin#include <sys/malloc.h> 7353541Sshin#include <sys/socket.h> 7453541Sshin#include <sys/socketvar.h> 7553541Sshin#include <sys/sockio.h> 7653541Sshin#include <sys/systm.h> 77164033Srwatson#include <sys/priv.h> 7853541Sshin#include <sys/proc.h> 7953541Sshin#include <sys/time.h> 8053541Sshin#include <sys/kernel.h> 8153541Sshin#include <sys/syslog.h> 8253541Sshin 8353541Sshin#include <net/if.h> 84197227Sqingli#include <net/if_var.h> 8553541Sshin#include <net/if_types.h> 8653541Sshin#include <net/route.h> 8753541Sshin#include <net/if_dl.h> 88185571Sbz#include <net/vnet.h> 8953541Sshin 9053541Sshin#include <netinet/in.h> 9153541Sshin#include <netinet/in_var.h> 92186119Sqingli#include <net/if_llatbl.h> 9353541Sshin#include <netinet/if_ether.h> 9478064Sume#include <netinet/in_systm.h> 9578064Sume#include <netinet/ip.h> 9678064Sume#include <netinet/in_pcb.h> 9753541Sshin 9862587Sitojun#include <netinet/ip6.h> 9953541Sshin#include <netinet6/ip6_var.h> 10095023Ssuz#include <netinet6/nd6.h> 10153541Sshin#include <netinet6/mld6_var.h> 10262587Sitojun#include <netinet6/ip6_mroute.h> 10353541Sshin#include <netinet6/in6_ifattach.h> 10462587Sitojun#include <netinet6/scope6_var.h> 10578064Sume#include <netinet6/in6_pcb.h> 10662587Sitojun 10753541Sshin/* 10853541Sshin * Definitions of some costant IP6 addresses. 10953541Sshin */ 11062587Sitojunconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 11162587Sitojunconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 11262587Sitojunconst struct in6_addr in6addr_nodelocal_allnodes = 11353541Sshin IN6ADDR_NODELOCAL_ALLNODES_INIT; 11462587Sitojunconst struct in6_addr in6addr_linklocal_allnodes = 11553541Sshin IN6ADDR_LINKLOCAL_ALLNODES_INIT; 11662587Sitojunconst struct in6_addr in6addr_linklocal_allrouters = 11753541Sshin IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 118191672Sbmsconst struct in6_addr in6addr_linklocal_allv2routers = 119191672Sbms IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT; 12053541Sshin 12162587Sitojunconst struct in6_addr in6mask0 = IN6MASK0; 12262587Sitojunconst struct in6_addr in6mask32 = IN6MASK32; 12362587Sitojunconst struct in6_addr in6mask64 = IN6MASK64; 12462587Sitojunconst struct in6_addr in6mask96 = IN6MASK96; 12562587Sitojunconst struct in6_addr in6mask128 = IN6MASK128; 12653541Sshin 127126552Sumeconst struct sockaddr_in6 sa6_any = 128126552Sume { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 }; 12978064Sume 13062587Sitojunstatic int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, 13183366Sjulian struct ifnet *, struct thread *)); 13278064Sumestatic int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *, 133120891Sume struct sockaddr_in6 *, int)); 134175162Sobrienstatic void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 13553541Sshin 13683934Sbrooksint (*faithprefix_p)(struct in6_addr *); 13783934Sbrooks 138120891Sume 13978064Sume 14053541Sshinint 141171259Sdelphijin6_mask2len(struct in6_addr *mask, u_char *lim0) 14253541Sshin{ 14378064Sume int x = 0, y; 14478064Sume u_char *lim = lim0, *p; 14553541Sshin 146120891Sume /* ignore the scope_id part */ 147120891Sume if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 14878064Sume lim = (u_char *)mask + sizeof(*mask); 14978064Sume for (p = (u_char *)mask; p < lim; x++, p++) { 15078064Sume if (*p != 0xff) 15153541Sshin break; 15253541Sshin } 15353541Sshin y = 0; 15478064Sume if (p < lim) { 15553541Sshin for (y = 0; y < 8; y++) { 15678064Sume if ((*p & (0x80 >> y)) == 0) 15753541Sshin break; 15853541Sshin } 15953541Sshin } 16078064Sume 16178064Sume /* 16278064Sume * when the limit pointer is given, do a stricter check on the 16378064Sume * remaining bits. 16478064Sume */ 16578064Sume if (p < lim) { 16678064Sume if (y != 0 && (*p & (0x00ff >> y)) != 0) 167120856Sume return (-1); 16878064Sume for (p = p + 1; p < lim; p++) 16978064Sume if (*p != 0) 170120856Sume return (-1); 17178064Sume } 172120891Sume 17353541Sshin return x * 8 + y; 17453541Sshin} 17553541Sshin 17653541Sshin#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 17762587Sitojun#define ia62ifa(ia6) (&((ia6)->ia_ifa)) 17853541Sshin 17953541Sshinint 180171259Sdelphijin6_control(struct socket *so, u_long cmd, caddr_t data, 181171259Sdelphij struct ifnet *ifp, struct thread *td) 18253541Sshin{ 18353541Sshin struct in6_ifreq *ifr = (struct in6_ifreq *)data; 18478064Sume struct in6_ifaddr *ia = NULL; 18553541Sshin struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 186151539Ssuz struct sockaddr_in6 *sa6; 187164033Srwatson int error; 18853541Sshin 18962587Sitojun switch (cmd) { 19062587Sitojun case SIOCGETSGCNT_IN6: 19162587Sitojun case SIOCGETMIFCNT_IN6: 192166938Sbms return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP); 19362587Sitojun } 19453541Sshin 195121742Sume switch(cmd) { 196121742Sume case SIOCAADDRCTL_POLICY: 197121742Sume case SIOCDADDRCTL_POLICY: 198164033Srwatson if (td != NULL) { 199164033Srwatson error = priv_check(td, PRIV_NETINET_ADDRCTRL6); 200164033Srwatson if (error) 201164033Srwatson return (error); 202164033Srwatson } 203121742Sume return (in6_src_ioctl(cmd, data)); 204121742Sume } 205121742Sume 20662587Sitojun if (ifp == NULL) 207120856Sume return (EOPNOTSUPP); 20853541Sshin 20953541Sshin switch (cmd) { 21053541Sshin case SIOCSNDFLUSH_IN6: 21153541Sshin case SIOCSPFXFLUSH_IN6: 21253541Sshin case SIOCSRTRFLUSH_IN6: 21362587Sitojun case SIOCSDEFIFACE_IN6: 21462587Sitojun case SIOCSIFINFO_FLAGS: 215193893Scperciva case SIOCSIFINFO_IN6: 216164033Srwatson if (td != NULL) { 217164033Srwatson error = priv_check(td, PRIV_NETINET_ND6); 218164033Srwatson if (error) 219164033Srwatson return (error); 220164033Srwatson } 221120891Sume /* FALLTHROUGH */ 22278064Sume case OSIOCGIFINFO_IN6: 22353541Sshin case SIOCGIFINFO_IN6: 22453541Sshin case SIOCGDRLST_IN6: 22553541Sshin case SIOCGPRLST_IN6: 22653541Sshin case SIOCGNBRINFO_IN6: 22762587Sitojun case SIOCGDEFIFACE_IN6: 228120856Sume return (nd6_ioctl(cmd, data, ifp)); 22953541Sshin } 23053541Sshin 23153541Sshin switch (cmd) { 23253541Sshin case SIOCSIFPREFIX_IN6: 23353541Sshin case SIOCDIFPREFIX_IN6: 23453541Sshin case SIOCAIFPREFIX_IN6: 23553541Sshin case SIOCCIFPREFIX_IN6: 23653541Sshin case SIOCSGIFPREFIX_IN6: 23753541Sshin case SIOCGIFPREFIX_IN6: 23878064Sume log(LOG_NOTICE, 23978064Sume "prefix ioctls are now invalidated. " 24078064Sume "please use ifconfig.\n"); 241120856Sume return (EOPNOTSUPP); 24253541Sshin } 24353541Sshin 24495023Ssuz switch (cmd) { 24562587Sitojun case SIOCSSCOPE6: 246164033Srwatson if (td != NULL) { 247164033Srwatson error = priv_check(td, PRIV_NETINET_SCOPE6); 248164033Srwatson if (error) 249164033Srwatson return (error); 250164033Srwatson } 251121161Sume return (scope6_set(ifp, 252121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 25362587Sitojun case SIOCGSCOPE6: 254121161Sume return (scope6_get(ifp, 255121161Sume (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id)); 25662587Sitojun case SIOCGSCOPE6DEF: 257121161Sume return (scope6_get_default((struct scope6_id *) 258121161Sume ifr->ifr_ifru.ifru_scope_id)); 25962587Sitojun } 26062587Sitojun 26153541Sshin switch (cmd) { 26253541Sshin case SIOCALIFADDR: 263175630Sbz if (td != NULL) { 264175630Sbz error = priv_check(td, PRIV_NET_ADDIFADDR); 265175630Sbz if (error) 266175630Sbz return (error); 267175630Sbz } 268175630Sbz return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 269175630Sbz 27053541Sshin case SIOCDLIFADDR: 271164033Srwatson if (td != NULL) { 272175630Sbz error = priv_check(td, PRIV_NET_DELIFADDR); 273164033Srwatson if (error) 274164033Srwatson return (error); 275164033Srwatson } 276120891Sume /* FALLTHROUGH */ 27753541Sshin case SIOCGLIFADDR: 27883366Sjulian return in6_lifaddr_ioctl(so, cmd, data, ifp, td); 27953541Sshin } 28053541Sshin 28153541Sshin /* 28253541Sshin * Find address for this interface, if it exists. 283151539Ssuz * 284151539Ssuz * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 285151539Ssuz * only, and used the first interface address as the target of other 286151539Ssuz * operations (without checking ifra_addr). This was because netinet 287151539Ssuz * code/API assumed at most 1 interface address per interface. 288151539Ssuz * Since IPv6 allows a node to assign multiple addresses 289151539Ssuz * on a single interface, we almost always look and check the 290151539Ssuz * presence of ifra_addr, and reject invalid ones here. 291151539Ssuz * It also decreases duplicated code among SIOC*_IN6 operations. 29253541Sshin */ 293151539Ssuz switch (cmd) { 294151539Ssuz case SIOCAIFADDR_IN6: 295151539Ssuz case SIOCSIFPHYADDR_IN6: 296151539Ssuz sa6 = &ifra->ifra_addr; 297151539Ssuz break; 298151539Ssuz case SIOCSIFADDR_IN6: 299151539Ssuz case SIOCGIFADDR_IN6: 300151539Ssuz case SIOCSIFDSTADDR_IN6: 301151539Ssuz case SIOCSIFNETMASK_IN6: 302151539Ssuz case SIOCGIFDSTADDR_IN6: 303151539Ssuz case SIOCGIFNETMASK_IN6: 304151539Ssuz case SIOCDIFADDR_IN6: 305151539Ssuz case SIOCGIFPSRCADDR_IN6: 306151539Ssuz case SIOCGIFPDSTADDR_IN6: 307151539Ssuz case SIOCGIFAFLAG_IN6: 308151539Ssuz case SIOCSNDFLUSH_IN6: 309151539Ssuz case SIOCSPFXFLUSH_IN6: 310151539Ssuz case SIOCSRTRFLUSH_IN6: 311151539Ssuz case SIOCGIFALIFETIME_IN6: 312151539Ssuz case SIOCSIFALIFETIME_IN6: 313151539Ssuz case SIOCGIFSTAT_IN6: 314151539Ssuz case SIOCGIFSTAT_ICMP6: 315151539Ssuz sa6 = &ifr->ifr_addr; 316151539Ssuz break; 317151539Ssuz default: 318151539Ssuz sa6 = NULL; 319151539Ssuz break; 320151539Ssuz } 321151539Ssuz if (sa6 && sa6->sin6_family == AF_INET6) { 322151539Ssuz if (sa6->sin6_scope_id != 0) 323151539Ssuz error = sa6_embedscope(sa6, 0); 324148385Sume else 325151539Ssuz error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 326148385Sume if (error != 0) 327148385Sume return (error); 328188144Sjamie if (td != NULL && (error = prison_check_ip6(td->td_ucred, 329188144Sjamie &sa6->sin6_addr)) != 0) 330188144Sjamie return (error); 331151539Ssuz ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 332151539Ssuz } else 333151539Ssuz ia = NULL; 33453541Sshin 33553541Sshin switch (cmd) { 33678064Sume case SIOCSIFADDR_IN6: 33778064Sume case SIOCSIFDSTADDR_IN6: 33878064Sume case SIOCSIFNETMASK_IN6: 33978064Sume /* 34078064Sume * Since IPv6 allows a node to assign multiple addresses 341151465Ssuz * on a single interface, SIOCSIFxxx ioctls are deprecated. 34278064Sume */ 34378064Sume /* we decided to obsolete this command (20000704) */ 344194760Srwatson error = EINVAL; 345194760Srwatson goto out; 34653541Sshin 34753541Sshin case SIOCDIFADDR_IN6: 34862587Sitojun /* 34978064Sume * for IPv4, we look for existing in_ifaddr here to allow 350151465Ssuz * "ifconfig if0 delete" to remove the first IPv4 address on 351151465Ssuz * the interface. For IPv6, as the spec allows multiple 352151465Ssuz * interface address from the day one, we consider "remove the 353151465Ssuz * first one" semantics to be not preferable. 35462587Sitojun */ 355194760Srwatson if (ia == NULL) { 356194760Srwatson error = EADDRNOTAVAIL; 357194760Srwatson goto out; 358194760Srwatson } 35953541Sshin /* FALLTHROUGH */ 36053541Sshin case SIOCAIFADDR_IN6: 36162587Sitojun /* 36278064Sume * We always require users to specify a valid IPv6 address for 36378064Sume * the corresponding operation. 36462587Sitojun */ 36578064Sume if (ifra->ifra_addr.sin6_family != AF_INET6 || 366194760Srwatson ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) { 367194760Srwatson error = EAFNOSUPPORT; 368194760Srwatson goto out; 369194760Srwatson } 37053541Sshin 371164033Srwatson if (td != NULL) { 372175630Sbz error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ? 373175630Sbz PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 374164033Srwatson if (error) 375194760Srwatson goto out; 376164033Srwatson } 37753541Sshin break; 37853541Sshin 37953541Sshin case SIOCGIFADDR_IN6: 38053541Sshin /* This interface is basically deprecated. use SIOCGIFCONF. */ 381120891Sume /* FALLTHROUGH */ 38253541Sshin case SIOCGIFAFLAG_IN6: 38353541Sshin case SIOCGIFNETMASK_IN6: 38453541Sshin case SIOCGIFDSTADDR_IN6: 38553541Sshin case SIOCGIFALIFETIME_IN6: 38653541Sshin /* must think again about its semantics */ 387194760Srwatson if (ia == NULL) { 388194760Srwatson error = EADDRNOTAVAIL; 389194760Srwatson goto out; 390194760Srwatson } 39153541Sshin break; 392194760Srwatson 39353541Sshin case SIOCSIFALIFETIME_IN6: 39453541Sshin { 39553541Sshin struct in6_addrlifetime *lt; 39653541Sshin 397164033Srwatson if (td != NULL) { 398164033Srwatson error = priv_check(td, PRIV_NETINET_ALIFETIME6); 399164033Srwatson if (error) 400194760Srwatson goto out; 401164033Srwatson } 402194760Srwatson if (ia == NULL) { 403194760Srwatson error = EADDRNOTAVAIL; 404194760Srwatson goto out; 405194760Srwatson } 40653541Sshin /* sanity for overflow - beware unsigned */ 40753541Sshin lt = &ifr->ifr_ifru.ifru_lifetime; 408126552Sume if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME && 409126552Sume lt->ia6t_vltime + time_second < time_second) { 410194760Srwatson error = EINVAL; 411194760Srwatson goto out; 41253541Sshin } 413126552Sume if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME && 414126552Sume lt->ia6t_pltime + time_second < time_second) { 415194760Srwatson error = EINVAL; 416194760Srwatson goto out; 41753541Sshin } 41853541Sshin break; 41953541Sshin } 42053541Sshin } 42153541Sshin 42253541Sshin switch (cmd) { 42353541Sshin case SIOCGIFADDR_IN6: 42453541Sshin ifr->ifr_addr = ia->ia_addr; 425148385Sume if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 426194760Srwatson goto out; 42753541Sshin break; 42853541Sshin 42953541Sshin case SIOCGIFDSTADDR_IN6: 430194760Srwatson if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 431194760Srwatson error = EINVAL; 432194760Srwatson goto out; 433194760Srwatson } 43462587Sitojun /* 43562587Sitojun * XXX: should we check if ifa_dstaddr is NULL and return 43662587Sitojun * an error? 43762587Sitojun */ 43853541Sshin ifr->ifr_dstaddr = ia->ia_dstaddr; 439148385Sume if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 440194760Srwatson goto out; 44153541Sshin break; 44253541Sshin 44353541Sshin case SIOCGIFNETMASK_IN6: 44453541Sshin ifr->ifr_addr = ia->ia_prefixmask; 44553541Sshin break; 44653541Sshin 44753541Sshin case SIOCGIFAFLAG_IN6: 44853541Sshin ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 44953541Sshin break; 45053541Sshin 45153541Sshin case SIOCGIFSTAT_IN6: 452194760Srwatson if (ifp == NULL) { 453194760Srwatson error = EINVAL; 454194760Srwatson goto out; 455194760Srwatson } 456121161Sume bzero(&ifr->ifr_ifru.ifru_stat, 457121161Sume sizeof(ifr->ifr_ifru.ifru_stat)); 458121161Sume ifr->ifr_ifru.ifru_stat = 459121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; 46053541Sshin break; 46153541Sshin 46253541Sshin case SIOCGIFSTAT_ICMP6: 463194760Srwatson if (ifp == NULL) { 464194760Srwatson error = EINVAL; 465194760Srwatson goto out; 466194760Srwatson } 467155454Sgnn bzero(&ifr->ifr_ifru.ifru_icmp6stat, 468121161Sume sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 469121161Sume ifr->ifr_ifru.ifru_icmp6stat = 470121161Sume *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; 47153541Sshin break; 47253541Sshin 47353541Sshin case SIOCGIFALIFETIME_IN6: 47453541Sshin ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 475151539Ssuz if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 476151539Ssuz time_t maxexpire; 477151539Ssuz struct in6_addrlifetime *retlt = 478151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 479151539Ssuz 480151539Ssuz /* 481151539Ssuz * XXX: adjust expiration time assuming time_t is 482151539Ssuz * signed. 483151539Ssuz */ 484151539Ssuz maxexpire = (-1) & 485151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 486151539Ssuz if (ia->ia6_lifetime.ia6t_vltime < 487151539Ssuz maxexpire - ia->ia6_updatetime) { 488151539Ssuz retlt->ia6t_expire = ia->ia6_updatetime + 489151539Ssuz ia->ia6_lifetime.ia6t_vltime; 490151539Ssuz } else 491151539Ssuz retlt->ia6t_expire = maxexpire; 492151539Ssuz } 493151539Ssuz if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 494151539Ssuz time_t maxexpire; 495151539Ssuz struct in6_addrlifetime *retlt = 496151539Ssuz &ifr->ifr_ifru.ifru_lifetime; 497151539Ssuz 498151539Ssuz /* 499151539Ssuz * XXX: adjust expiration time assuming time_t is 500151539Ssuz * signed. 501151539Ssuz */ 502151539Ssuz maxexpire = (-1) & 503151546Ssuz ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 504151539Ssuz if (ia->ia6_lifetime.ia6t_pltime < 505151539Ssuz maxexpire - ia->ia6_updatetime) { 506151539Ssuz retlt->ia6t_preferred = ia->ia6_updatetime + 507151539Ssuz ia->ia6_lifetime.ia6t_pltime; 508151539Ssuz } else 509151539Ssuz retlt->ia6t_preferred = maxexpire; 510151539Ssuz } 51153541Sshin break; 51253541Sshin 51353541Sshin case SIOCSIFALIFETIME_IN6: 51453541Sshin ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime; 51553541Sshin /* for sanity */ 51653541Sshin if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 51753541Sshin ia->ia6_lifetime.ia6t_expire = 51853541Sshin time_second + ia->ia6_lifetime.ia6t_vltime; 51953541Sshin } else 52053541Sshin ia->ia6_lifetime.ia6t_expire = 0; 52153541Sshin if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 52253541Sshin ia->ia6_lifetime.ia6t_preferred = 52353541Sshin time_second + ia->ia6_lifetime.ia6t_pltime; 52453541Sshin } else 52553541Sshin ia->ia6_lifetime.ia6t_preferred = 0; 52653541Sshin break; 52753541Sshin 52878064Sume case SIOCAIFADDR_IN6: 52978064Sume { 530194760Srwatson int i; 531151539Ssuz struct nd_prefixctl pr0; 532151539Ssuz struct nd_prefix *pr; 53378064Sume 53462587Sitojun /* 53578064Sume * first, make or update the interface address structure, 53678064Sume * and link it to the list. 53762587Sitojun */ 538151539Ssuz if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) 539194760Srwatson goto out; 540194760Srwatson if (ia != NULL) 541194760Srwatson ifa_free(&ia->ia_ifa); 542151915Ssuz if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) 543151915Ssuz == NULL) { 544171260Sdelphij /* 545151915Ssuz * this can happen when the user specify the 0 valid 546151915Ssuz * lifetime. 547151915Ssuz */ 548151915Ssuz break; 549151915Ssuz } 55053541Sshin 55178064Sume /* 55278064Sume * then, make the prefix on-link on the interface. 55378064Sume * XXX: we'd rather create the prefix before the address, but 55478064Sume * we need at least one address to install the corresponding 55578064Sume * interface route, so we configure the address first. 55678064Sume */ 55778064Sume 55878064Sume /* 55978064Sume * convert mask to prefix length (prefixmask has already 56078064Sume * been validated in in6_update_ifa(). 56178064Sume */ 56278064Sume bzero(&pr0, sizeof(pr0)); 56378064Sume pr0.ndpr_ifp = ifp; 56478064Sume pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 565120891Sume NULL); 566120891Sume if (pr0.ndpr_plen == 128) { 56778064Sume break; /* we don't need to install a host route. */ 568120891Sume } 56978064Sume pr0.ndpr_prefix = ifra->ifra_addr; 57078064Sume /* apply the mask for safety. */ 57178064Sume for (i = 0; i < 4; i++) { 57278064Sume pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 573120891Sume ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 57478064Sume } 57578064Sume /* 57695023Ssuz * XXX: since we don't have an API to set prefix (not address) 57795023Ssuz * lifetimes, we just use the same lifetimes as addresses. 57895023Ssuz * The (temporarily) installed lifetimes can be overridden by 57995023Ssuz * later advertised RAs (when accept_rtadv is non 0), which is 58095023Ssuz * an intended behavior. 58178064Sume */ 58278064Sume pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 58378064Sume pr0.ndpr_raf_auto = 584120891Sume ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 58578064Sume pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 58678064Sume pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 58778064Sume 588120891Sume /* add the prefix if not yet. */ 58978064Sume if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 59078064Sume /* 59178064Sume * nd6_prelist_add will install the corresponding 59278064Sume * interface route. 59378064Sume */ 59478064Sume if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) 595194760Srwatson goto out; 59678064Sume if (pr == NULL) { 597120891Sume log(LOG_ERR, "nd6_prelist_add succeeded but " 59878064Sume "no prefix\n"); 599194760Srwatson error = EINVAL; 600194760Srwatson goto out; 60178064Sume } 60278064Sume } 60378064Sume 604151915Ssuz /* relate the address to the prefix */ 605151915Ssuz if (ia->ia6_ndpr == NULL) { 606151915Ssuz ia->ia6_ndpr = pr; 607151915Ssuz pr->ndpr_refcnt++; 60878064Sume 60978064Sume /* 610151915Ssuz * If this is the first autoconf address from the 611151915Ssuz * prefix, create a temporary address as well 612151915Ssuz * (when required). 61378064Sume */ 614151915Ssuz if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 615181803Sbz V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) { 616151915Ssuz int e; 617151915Ssuz if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { 618151915Ssuz log(LOG_NOTICE, "in6_control: failed " 619151915Ssuz "to create a temporary address, " 620151915Ssuz "errno=%d\n", e); 621151915Ssuz } 622151915Ssuz } 62362587Sitojun } 624151915Ssuz 625151915Ssuz /* 626151915Ssuz * this might affect the status of autoconfigured addresses, 627151915Ssuz * that is, this address might make other addresses detached. 628151915Ssuz */ 629151915Ssuz pfxlist_onlink_check(); 630126264Smlaier if (error == 0 && ia) 631126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 63278064Sume break; 63378064Sume } 63462587Sitojun 63578064Sume case SIOCDIFADDR_IN6: 63678064Sume { 637151539Ssuz struct nd_prefix *pr; 63878064Sume 63978064Sume /* 64078064Sume * If the address being deleted is the only one that owns 64178064Sume * the corresponding prefix, expire the prefix as well. 642120891Sume * XXX: theoretically, we don't have to worry about such 64378064Sume * relationship, since we separate the address management 64478064Sume * and the prefix management. We do this, however, to provide 64578064Sume * as much backward compatibility as possible in terms of 64678064Sume * the ioctl operation. 647151915Ssuz * Note that in6_purgeaddr() will decrement ndpr_refcnt. 64878064Sume */ 649151915Ssuz pr = ia->ia6_ndpr; 65078064Sume in6_purgeaddr(&ia->ia_ifa); 651151915Ssuz if (pr && pr->ndpr_refcnt == 0) 652151915Ssuz prelist_remove(pr); 653126264Smlaier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 65453541Sshin break; 65578064Sume } 65653541Sshin 65778064Sume default: 658194760Srwatson if (ifp == NULL || ifp->if_ioctl == 0) { 659194760Srwatson error = EOPNOTSUPP; 660194760Srwatson goto out; 661194760Srwatson } 662194760Srwatson error = (*ifp->if_ioctl)(ifp, cmd, data); 663194760Srwatson goto out; 66478064Sume } 66553541Sshin 666194760Srwatson error = 0; 667194760Srwatsonout: 668194760Srwatson if (ia != NULL) 669194760Srwatson ifa_free(&ia->ia_ifa); 670194760Srwatson return (error); 67178064Sume} 67253541Sshin 67378064Sume/* 67478064Sume * Update parameters of an IPv6 interface address. 67578064Sume * If necessary, a new entry is created and linked into address chains. 67678064Sume * This function is separated from in6_control(). 67778064Sume * XXX: should this be performed under splnet()? 67878064Sume */ 67978064Sumeint 680171259Sdelphijin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 681171259Sdelphij struct in6_ifaddr *ia, int flags) 68278064Sume{ 68378064Sume int error = 0, hostIsNew = 0, plen = -1; 68478064Sume struct sockaddr_in6 dst6; 68578064Sume struct in6_addrlifetime *lt; 686151539Ssuz struct in6_multi_mship *imm; 687151539Ssuz struct in6_multi *in6m_sol; 688148385Sume struct rtentry *rt; 689151539Ssuz int delay; 690165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 69178064Sume 69278064Sume /* Validate parameters */ 69378064Sume if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 694120856Sume return (EINVAL); 69578064Sume 69678064Sume /* 69778064Sume * The destination address for a p2p link must have a family 69878064Sume * of AF_UNSPEC or AF_INET6. 69978064Sume */ 70078064Sume if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 70178064Sume ifra->ifra_dstaddr.sin6_family != AF_INET6 && 70278064Sume ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 703120856Sume return (EAFNOSUPPORT); 70478064Sume /* 70578064Sume * validate ifra_prefixmask. don't check sin6_family, netmask 70678064Sume * does not carry fields other than sin6_len. 70778064Sume */ 70878064Sume if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 709120856Sume return (EINVAL); 71078064Sume /* 71178064Sume * Because the IPv6 address architecture is classless, we require 71278064Sume * users to specify a (non 0) prefix length (mask) for a new address. 71378064Sume * We also require the prefix (when specified) mask is valid, and thus 71478064Sume * reject a non-consecutive mask. 71578064Sume */ 71678064Sume if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 717120856Sume return (EINVAL); 71878064Sume if (ifra->ifra_prefixmask.sin6_len != 0) { 71978064Sume plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 720120891Sume (u_char *)&ifra->ifra_prefixmask + 721120891Sume ifra->ifra_prefixmask.sin6_len); 72278064Sume if (plen <= 0) 723120856Sume return (EINVAL); 724120891Sume } else { 72562587Sitojun /* 72695023Ssuz * In this case, ia must not be NULL. We just use its prefix 72778064Sume * length. 72862587Sitojun */ 72978064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 73078064Sume } 73178064Sume /* 73278064Sume * If the destination address on a p2p interface is specified, 73378064Sume * and the address is a scoped one, validate/set the scope 73478064Sume * zone identifier. 73578064Sume */ 73678064Sume dst6 = ifra->ifra_dstaddr; 737120891Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 73878064Sume (dst6.sin6_family == AF_INET6)) { 739148385Sume struct in6_addr in6_tmp; 740126552Sume u_int32_t zoneid; 74178064Sume 742148385Sume in6_tmp = dst6.sin6_addr; 743148385Sume if (in6_setscope(&in6_tmp, ifp, &zoneid)) 744148385Sume return (EINVAL); /* XXX: should be impossible */ 745148385Sume 746148385Sume if (dst6.sin6_scope_id != 0) { 747148385Sume if (dst6.sin6_scope_id != zoneid) 748148385Sume return (EINVAL); 749148385Sume } else /* user omit to specify the ID. */ 750126552Sume dst6.sin6_scope_id = zoneid; 751148385Sume 752148385Sume /* convert into the internal form */ 753148385Sume if (sa6_embedscope(&dst6, 0)) 754148385Sume return (EINVAL); /* XXX: should be impossible */ 75578064Sume } 75678064Sume /* 75778064Sume * The destination address can be specified only for a p2p or a 75878064Sume * loopback interface. If specified, the corresponding prefix length 75978064Sume * must be 128. 76078064Sume */ 76178064Sume if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 76278064Sume if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 763126552Sume /* XXX: noisy message */ 764122059Sume nd6log((LOG_INFO, "in6_update_ifa: a destination can " 765122059Sume "be specified for a p2p or a loopback IF only\n")); 766120856Sume return (EINVAL); 76778064Sume } 76878064Sume if (plen != 128) { 769122059Sume nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 770122059Sume "be 128 when dstaddr is specified\n")); 771120856Sume return (EINVAL); 77278064Sume } 77378064Sume } 77478064Sume /* lifetime consistency check */ 77578064Sume lt = &ifra->ifra_lifetime; 776151539Ssuz if (lt->ia6t_pltime > lt->ia6t_vltime) 777151539Ssuz return (EINVAL); 77878064Sume if (lt->ia6t_vltime == 0) { 77962587Sitojun /* 78078064Sume * the following log might be noisy, but this is a typical 78178064Sume * configuration mistake or a tool's bug. 78262587Sitojun */ 783122059Sume nd6log((LOG_INFO, 78478064Sume "in6_update_ifa: valid lifetime is 0 for %s\n", 785165118Sbz ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 786151539Ssuz 787151539Ssuz if (ia == NULL) 788151539Ssuz return (0); /* there's nothing to do */ 78978064Sume } 79062587Sitojun 79178064Sume /* 79278064Sume * If this is a new address, allocate a new ifaddr and link it 79378064Sume * into chains. 79478064Sume */ 79578064Sume if (ia == NULL) { 79678064Sume hostIsNew = 1; 79779763Sume /* 79879763Sume * When in6_update_ifa() is called in a process of a received 799120891Sume * RA, it is called under an interrupt context. So, we should 800120891Sume * call malloc with M_NOWAIT. 80179763Sume */ 802120891Sume ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR, 803120891Sume M_NOWAIT); 80478064Sume if (ia == NULL) 80578064Sume return (ENOBUFS); 80678064Sume bzero((caddr_t)ia, sizeof(*ia)); 807194602Srwatson ifa_init(&ia->ia_ifa); 808170202Sjinmei LIST_INIT(&ia->ia6_memberships); 809151539Ssuz /* Initialize the address and masks, and put time stamp */ 81078064Sume ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 81178064Sume ia->ia_addr.sin6_family = AF_INET6; 81278064Sume ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 813151539Ssuz ia->ia6_createtime = time_second; 81478064Sume if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 81578064Sume /* 81678064Sume * XXX: some functions expect that ifa_dstaddr is not 81778064Sume * NULL for p2p interfaces. 81878064Sume */ 819120891Sume ia->ia_ifa.ifa_dstaddr = 820120891Sume (struct sockaddr *)&ia->ia_dstaddr; 82178064Sume } else { 82278064Sume ia->ia_ifa.ifa_dstaddr = NULL; 82353541Sshin } 824108033Shsu ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 82578064Sume ia->ia_ifp = ifp; 826194760Srwatson ifa_ref(&ia->ia_ifa); /* if_addrhead */ 827191323Srwatson IF_ADDR_LOCK(ifp); 828191340Srwatson TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 829191323Srwatson IF_ADDR_UNLOCK(ifp); 830194907Srwatson 831194971Srwatson ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */ 832194971Srwatson IN6_IFADDR_WLOCK(); 833194907Srwatson TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link); 834194971Srwatson IN6_IFADDR_WUNLOCK(); 83578064Sume } 83678064Sume 837151539Ssuz /* update timestamp */ 838151539Ssuz ia->ia6_updatetime = time_second; 839151539Ssuz 84078064Sume /* set prefix mask */ 84178064Sume if (ifra->ifra_prefixmask.sin6_len) { 84278064Sume /* 84378064Sume * We prohibit changing the prefix length of an existing 84478064Sume * address, because 84578064Sume * + such an operation should be rare in IPv6, and 84678064Sume * + the operation would confuse prefix management. 84778064Sume */ 84878064Sume if (ia->ia_prefixmask.sin6_len && 84978064Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 850122059Sume nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an" 85178064Sume " existing (%s) address should not be changed\n", 852165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 85378064Sume error = EINVAL; 85478064Sume goto unlink; 85553541Sshin } 85678064Sume ia->ia_prefixmask = ifra->ifra_prefixmask; 85778064Sume } 85878064Sume 85978064Sume /* 86078064Sume * If a new destination address is specified, scrub the old one and 86178064Sume * install the new destination. Note that the interface must be 862120891Sume * p2p or loopback (see the check above.) 86378064Sume */ 86478064Sume if (dst6.sin6_family == AF_INET6 && 865120891Sume !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 86678064Sume int e; 86778064Sume 86878064Sume if ((ia->ia_flags & IFA_ROUTE) != 0 && 869120891Sume (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) { 870122059Sume nd6log((LOG_ERR, "in6_update_ifa: failed to remove " 87178064Sume "a route to the old destination: %s\n", 872165118Sbz ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 87378064Sume /* proceed anyway... */ 874120891Sume } else 87578064Sume ia->ia_flags &= ~IFA_ROUTE; 87678064Sume ia->ia_dstaddr = dst6; 87778064Sume } 87853541Sshin 879148385Sume /* 880148385Sume * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 881148385Sume * to see if the address is deprecated or invalidated, but initialize 882148385Sume * these members for applications. 883148385Sume */ 884148385Sume ia->ia6_lifetime = ifra->ifra_lifetime; 885148385Sume if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 886148385Sume ia->ia6_lifetime.ia6t_expire = 887148385Sume time_second + ia->ia6_lifetime.ia6t_vltime; 888148385Sume } else 889148385Sume ia->ia6_lifetime.ia6t_expire = 0; 890148385Sume if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 891148385Sume ia->ia6_lifetime.ia6t_preferred = 892148385Sume time_second + ia->ia6_lifetime.ia6t_pltime; 893148385Sume } else 894148385Sume ia->ia6_lifetime.ia6t_preferred = 0; 895148385Sume 89678064Sume /* reset the interface and routing table appropriately. */ 89778064Sume if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0) 89878064Sume goto unlink; 89978064Sume 90078064Sume /* 901148385Sume * configure address flags. 902148385Sume */ 903148385Sume ia->ia6_flags = ifra->ifra_flags; 904148385Sume /* 905148385Sume * backward compatibility - if IN6_IFF_DEPRECATED is set from the 906148385Sume * userland, make it deprecated. 907148385Sume */ 908148385Sume if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 909148385Sume ia->ia6_lifetime.ia6t_pltime = 0; 910148385Sume ia->ia6_lifetime.ia6t_preferred = time_second; 911148385Sume } 912148385Sume /* 913151539Ssuz * Make the address tentative before joining multicast addresses, 914151539Ssuz * so that corresponding MLD responses would not have a tentative 915151539Ssuz * source address. 916148385Sume */ 917151539Ssuz ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 918151539Ssuz if (hostIsNew && in6if_do_dad(ifp)) 919148385Sume ia->ia6_flags |= IN6_IFF_TENTATIVE; 920148385Sume 921197138Shrs /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */ 922197138Shrs if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 923197138Shrs ia->ia6_flags |= IN6_IFF_TENTATIVE; 924197138Shrs 925148385Sume /* 926148385Sume * We are done if we have simply modified an existing address. 927148385Sume */ 928148385Sume if (!hostIsNew) 929148385Sume return (error); 930148385Sume 931148385Sume /* 93278064Sume * Beyond this point, we should call in6_purgeaddr upon an error, 933120891Sume * not just go to unlink. 93478064Sume */ 93578064Sume 936151465Ssuz /* Join necessary multicast groups */ 937151539Ssuz in6m_sol = NULL; 93878064Sume if ((ifp->if_flags & IFF_MULTICAST) != 0) { 93978064Sume struct sockaddr_in6 mltaddr, mltmask; 940148385Sume struct in6_addr llsol; 94178064Sume 942148385Sume /* join solicited multicast addr for new host id */ 943148385Sume bzero(&llsol, sizeof(struct in6_addr)); 944151465Ssuz llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL; 945148385Sume llsol.s6_addr32[1] = 0; 946148385Sume llsol.s6_addr32[2] = htonl(1); 947148385Sume llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3]; 948148385Sume llsol.s6_addr8[12] = 0xff; 949148385Sume if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 950148385Sume /* XXX: should not happen */ 951148385Sume log(LOG_ERR, "in6_update_ifa: " 952148385Sume "in6_setscope failed\n"); 953148385Sume goto cleanup; 95453541Sshin } 955151539Ssuz delay = 0; 956151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 957151539Ssuz /* 958151539Ssuz * We need a random delay for DAD on the address 959151539Ssuz * being configured. It also means delaying 960151539Ssuz * transmission of the corresponding MLD report to 961151539Ssuz * avoid report collision. 962197138Shrs * [RFC 4861, Section 6.3.7] 963151539Ssuz */ 964151539Ssuz delay = arc4random() % 965151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 966151539Ssuz } 967151539Ssuz imm = in6_joingroup(ifp, &llsol, &error, delay); 968170202Sjinmei if (imm == NULL) { 969148385Sume nd6log((LOG_WARNING, 970148385Sume "in6_update_ifa: addmulti failed for " 971148385Sume "%s on %s (errno=%d)\n", 972165118Sbz ip6_sprintf(ip6buf, &llsol), if_name(ifp), 973148385Sume error)); 974195102Srwatson goto cleanup; 975148385Sume } 976170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 977170202Sjinmei imm, i6mm_chain); 978151539Ssuz in6m_sol = imm->i6mm_maddr; 97953541Sshin 98078064Sume bzero(&mltmask, sizeof(mltmask)); 98178064Sume mltmask.sin6_len = sizeof(struct sockaddr_in6); 98278064Sume mltmask.sin6_family = AF_INET6; 98378064Sume mltmask.sin6_addr = in6mask32; 984151465Ssuz#define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 98553541Sshin 98653541Sshin /* 98778064Sume * join link-local all-nodes address 98871207Sitojun */ 98978064Sume bzero(&mltaddr, sizeof(mltaddr)); 99078064Sume mltaddr.sin6_len = sizeof(struct sockaddr_in6); 99178064Sume mltaddr.sin6_family = AF_INET6; 99278064Sume mltaddr.sin6_addr = in6addr_linklocal_allnodes; 993148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 994148385Sume 0) 995148385Sume goto cleanup; /* XXX: should not fail */ 99671207Sitojun 997148385Sume /* 998148385Sume * XXX: do we really need this automatic routes? 999148385Sume * We should probably reconsider this stuff. Most applications 1000148385Sume * actually do not need the routes, since they usually specify 1001148385Sume * the outgoing interface. 1002148385Sume */ 1003148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1004148385Sume if (rt) { 1005174375Sjulian /* XXX: only works in !SCOPEDROUTING case. */ 1006148385Sume if (memcmp(&mltaddr.sin6_addr, 1007148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1008151465Ssuz MLTMASK_LEN)) { 1009148385Sume RTFREE_LOCKED(rt); 1010148385Sume rt = NULL; 1011148385Sume } 1012148385Sume } 1013148385Sume if (!rt) { 1014148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1015148385Sume (struct sockaddr *)&ia->ia_addr, 1016186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1017148385Sume (struct rtentry **)0); 1018148385Sume if (error) 1019148385Sume goto cleanup; 1020151539Ssuz } else { 1021151539Ssuz RTFREE_LOCKED(rt); 1022151539Ssuz } 102378064Sume 1024151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1025151539Ssuz if (!imm) { 1026151539Ssuz nd6log((LOG_WARNING, 1027151539Ssuz "in6_update_ifa: addmulti failed for " 1028151539Ssuz "%s on %s (errno=%d)\n", 1029165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1030151539Ssuz if_name(ifp), error)); 1031151539Ssuz goto cleanup; 1032151539Ssuz } 1033170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1034151539Ssuz 103571207Sitojun /* 103678064Sume * join node information group address 103753541Sshin */ 1038151539Ssuz delay = 0; 1039151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1040151539Ssuz /* 1041151539Ssuz * The spec doesn't say anything about delay for this 1042151539Ssuz * group, but the same logic should apply. 1043151539Ssuz */ 1044151539Ssuz delay = arc4random() % 1045151539Ssuz (MAX_RTR_SOLICITATION_DELAY * hz); 1046151539Ssuz } 1047192895Sjamie if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) { 1048151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 1049151539Ssuz delay); /* XXX jinmei */ 1050151539Ssuz if (!imm) { 1051151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1052151539Ssuz "addmulti failed for %s on %s " 1053151539Ssuz "(errno=%d)\n", 1054165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1055151539Ssuz if_name(ifp), error)); 1056151539Ssuz /* XXX not very fatal, go on... */ 1057170202Sjinmei } else { 1058170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, 1059170202Sjinmei imm, i6mm_chain); 106062587Sitojun } 1061192895Sjamie } 106253541Sshin 106378064Sume /* 1064148385Sume * join interface-local all-nodes address. 1065148385Sume * (ff01::1%ifN, and ff01::%ifN/32) 106678064Sume */ 1067148385Sume mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1068148385Sume if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) 1069148385Sume != 0) 1070148385Sume goto cleanup; /* XXX: should not fail */ 1071148385Sume /* XXX: again, do we really need the route? */ 1072148385Sume rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1073148385Sume if (rt) { 1074148385Sume if (memcmp(&mltaddr.sin6_addr, 1075148385Sume &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 1076151465Ssuz MLTMASK_LEN)) { 1077148385Sume RTFREE_LOCKED(rt); 1078148385Sume rt = NULL; 1079148385Sume } 1080148385Sume } 1081148385Sume if (!rt) { 1082148385Sume error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr, 1083148385Sume (struct sockaddr *)&ia->ia_addr, 1084186119Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1085148385Sume (struct rtentry **)0); 1086148385Sume if (error) 1087148385Sume goto cleanup; 1088148385Sume } else 1089148385Sume RTFREE_LOCKED(rt); 109081115Sume 1091151539Ssuz imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1092151539Ssuz if (!imm) { 1093151539Ssuz nd6log((LOG_WARNING, "in6_update_ifa: " 1094151539Ssuz "addmulti failed for %s on %s " 1095151539Ssuz "(errno=%d)\n", 1096165118Sbz ip6_sprintf(ip6buf, &mltaddr.sin6_addr), 1097151539Ssuz if_name(ifp), error)); 1098151539Ssuz goto cleanup; 1099151539Ssuz } 1100170202Sjinmei LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1101151465Ssuz#undef MLTMASK_LEN 110278064Sume } 110353541Sshin 1104151539Ssuz /* 1105151539Ssuz * Perform DAD, if needed. 1106151539Ssuz * XXX It may be of use, if we can administratively 1107151539Ssuz * disable DAD. 1108151539Ssuz */ 1109194760Srwatson if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1110151539Ssuz (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1111151539Ssuz { 1112151539Ssuz int mindelay, maxdelay; 1113151539Ssuz 1114151539Ssuz delay = 0; 1115151539Ssuz if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1116151539Ssuz /* 1117151539Ssuz * We need to impose a delay before sending an NS 1118151539Ssuz * for DAD. Check if we also needed a delay for the 1119151539Ssuz * corresponding MLD message. If we did, the delay 1120151539Ssuz * should be larger than the MLD delay (this could be 1121151539Ssuz * relaxed a bit, but this simple logic is at least 1122151539Ssuz * safe). 1123191672Sbms * XXX: Break data hiding guidelines and look at 1124191672Sbms * state for the solicited multicast group. 1125151539Ssuz */ 1126151539Ssuz mindelay = 0; 1127151539Ssuz if (in6m_sol != NULL && 1128191672Sbms in6m_sol->in6m_state == MLD_REPORTING_MEMBER) { 1129151539Ssuz mindelay = in6m_sol->in6m_timer; 1130151539Ssuz } 1131151539Ssuz maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1132151539Ssuz if (maxdelay - mindelay == 0) 1133151539Ssuz delay = 0; 1134151539Ssuz else { 1135151539Ssuz delay = 1136151539Ssuz (arc4random() % (maxdelay - mindelay)) + 1137151539Ssuz mindelay; 1138151539Ssuz } 1139151539Ssuz } 1140151539Ssuz nd6_dad_start((struct ifaddr *)ia, delay); 1141151539Ssuz } 1142151539Ssuz 1143194760Srwatson KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew")); 1144194760Srwatson ifa_free(&ia->ia_ifa); 1145120856Sume return (error); 114678064Sume 114778064Sume unlink: 114878064Sume /* 114978064Sume * XXX: if a change of an existing address failed, keep the entry 115078064Sume * anyway. 115178064Sume */ 1152194760Srwatson if (hostIsNew) { 1153194943Srwatson in6_unlink_ifa(ia, ifp); 1154194760Srwatson ifa_free(&ia->ia_ifa); 1155194760Srwatson } 1156120856Sume return (error); 1157148385Sume 1158148385Sume cleanup: 1159194760Srwatson KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew")); 1160194760Srwatson ifa_free(&ia->ia_ifa); 1161148385Sume in6_purgeaddr(&ia->ia_ifa); 1162148385Sume return error; 116353541Sshin} 116453541Sshin 116562587Sitojunvoid 1166171259Sdelphijin6_purgeaddr(struct ifaddr *ifa) 116762587Sitojun{ 116878064Sume struct ifnet *ifp = ifa->ifa_ifp; 116978064Sume struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1170170202Sjinmei struct in6_multi_mship *imm; 1171192282Sqingli struct sockaddr_in6 mltaddr, mltmask; 1172192282Sqingli struct rtentry rt0; 1173192282Sqingli struct sockaddr_dl gateway; 1174192282Sqingli struct sockaddr_in6 mask, addr; 1175192282Sqingli int plen, error; 1176192282Sqingli struct rtentry *rt; 1177192282Sqingli struct ifaddr *ifa0, *nifa; 117862587Sitojun 1179192282Sqingli /* 1180192282Sqingli * find another IPv6 address as the gateway for the 1181192282Sqingli * link-local and node-local all-nodes multicast 1182192282Sqingli * address routes 1183192282Sqingli */ 1184194760Srwatson IF_ADDR_LOCK(ifp); 1185192282Sqingli TAILQ_FOREACH_SAFE(ifa0, &ifp->if_addrhead, ifa_link, nifa) { 1186192282Sqingli if ((ifa0->ifa_addr->sa_family != AF_INET6) || 1187192282Sqingli memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr, 1188192282Sqingli &ia->ia_addr.sin6_addr, 1189192282Sqingli sizeof(struct in6_addr)) == 0) 1190192282Sqingli continue; 1191192282Sqingli else 1192192282Sqingli break; 1193192282Sqingli } 1194194760Srwatson if (ifa0 != NULL) 1195194760Srwatson ifa_ref(ifa0); 1196194760Srwatson IF_ADDR_UNLOCK(ifp); 1197192282Sqingli 1198195914Sqingli /* 1199195914Sqingli * Remove the loopback route to the interface address. 1200196871Sqingli * The check for the current setting of "nd6_useloopback" 1201196871Sqingli * is not needed. 1202195914Sqingli */ 1203197227Sqingli error = ifa_del_loopback_route((struct ifaddr *)ia, 1204197227Sqingli (struct sockaddr *)&ia->ia_addr); 1205195643Sqingli 120678064Sume /* stop DAD processing */ 120778064Sume nd6_dad_stop(ifa); 120862587Sitojun 1209186119Sqingli IF_AFDATA_LOCK(ifp); 1210186119Sqingli lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR), 1211186119Sqingli (struct sockaddr *)&ia->ia_addr); 1212186119Sqingli IF_AFDATA_UNLOCK(ifp); 1213192282Sqingli 121478064Sume /* 1215192282Sqingli * initialize for rtmsg generation 1216192282Sqingli */ 1217192282Sqingli bzero(&gateway, sizeof(gateway)); 1218192282Sqingli gateway.sdl_len = sizeof(gateway); 1219192282Sqingli gateway.sdl_family = AF_LINK; 1220192282Sqingli gateway.sdl_nlen = 0; 1221192282Sqingli gateway.sdl_alen = ifp->if_addrlen; 1222192282Sqingli /* */ 1223192282Sqingli bzero(&rt0, sizeof(rt0)); 1224192282Sqingli rt0.rt_gateway = (struct sockaddr *)&gateway; 1225192282Sqingli memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1226192282Sqingli memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1227192282Sqingli rt_mask(&rt0) = (struct sockaddr *)&mask; 1228192282Sqingli rt_key(&rt0) = (struct sockaddr *)&addr; 1229192282Sqingli rt0.rt_flags = RTF_HOST | RTF_STATIC; 1230192282Sqingli rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0); 1231192282Sqingli 1232192282Sqingli /* 1233170202Sjinmei * leave from multicast groups we have joined for the interface 1234170202Sjinmei */ 1235170202Sjinmei while ((imm = ia->ia6_memberships.lh_first) != NULL) { 1236170202Sjinmei LIST_REMOVE(imm, i6mm_chain); 1237170202Sjinmei in6_leavegroup(imm); 123862587Sitojun } 123962587Sitojun 1240192282Sqingli /* 1241192282Sqingli * remove the link-local all-nodes address 1242192282Sqingli */ 1243192282Sqingli bzero(&mltmask, sizeof(mltmask)); 1244192282Sqingli mltmask.sin6_len = sizeof(struct sockaddr_in6); 1245192282Sqingli mltmask.sin6_family = AF_INET6; 1246192282Sqingli mltmask.sin6_addr = in6mask32; 1247192282Sqingli 1248192282Sqingli bzero(&mltaddr, sizeof(mltaddr)); 1249192282Sqingli mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1250192282Sqingli mltaddr.sin6_family = AF_INET6; 1251192282Sqingli mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1252192282Sqingli 1253192282Sqingli if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1254192282Sqingli 0) 1255192282Sqingli goto cleanup; 1256192282Sqingli 1257192282Sqingli rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1258192282Sqingli if (rt != NULL && rt->rt_gateway != NULL && 1259192282Sqingli (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1260192282Sqingli &ia->ia_addr.sin6_addr, 1261192282Sqingli sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1262192282Sqingli /* 1263192282Sqingli * if no more IPv6 address exists on this interface 1264192282Sqingli * then remove the multicast address route 1265192282Sqingli */ 1266192282Sqingli if (ifa0 == NULL) { 1267192282Sqingli memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1268192282Sqingli sizeof(mltaddr.sin6_addr)); 1269192282Sqingli RTFREE_LOCKED(rt); 1270192282Sqingli error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1271192282Sqingli (struct sockaddr *)&ia->ia_addr, 1272192282Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1273192282Sqingli (struct rtentry **)0); 1274192282Sqingli if (error) 1275192282Sqingli log(LOG_INFO, "in6_purgeaddr: link-local all-nodes" 1276192282Sqingli "multicast address deletion error\n"); 1277192282Sqingli } else { 1278192282Sqingli /* 1279192282Sqingli * replace the gateway of the route 1280192282Sqingli */ 1281192282Sqingli struct sockaddr_in6 sa; 1282192282Sqingli 1283192282Sqingli bzero(&sa, sizeof(sa)); 1284192282Sqingli sa.sin6_len = sizeof(struct sockaddr_in6); 1285192282Sqingli sa.sin6_family = AF_INET6; 1286192282Sqingli memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1287192282Sqingli sizeof(sa.sin6_addr)); 1288192282Sqingli in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1289192282Sqingli memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1290192282Sqingli RTFREE_LOCKED(rt); 1291192282Sqingli } 1292192282Sqingli } else { 1293192282Sqingli if (rt != NULL) 1294192282Sqingli RTFREE_LOCKED(rt); 1295192282Sqingli } 1296192282Sqingli 1297192282Sqingli /* 1298192282Sqingli * remove the node-local all-nodes address 1299192282Sqingli */ 1300192282Sqingli mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1301192282Sqingli if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1302192282Sqingli 0) 1303192282Sqingli goto cleanup; 1304192282Sqingli 1305192282Sqingli rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1306192282Sqingli if (rt != NULL && rt->rt_gateway != NULL && 1307192282Sqingli (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1308192282Sqingli &ia->ia_addr.sin6_addr, 1309192282Sqingli sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1310192282Sqingli /* 1311192282Sqingli * if no more IPv6 address exists on this interface 1312192282Sqingli * then remove the multicast address route 1313192282Sqingli */ 1314192282Sqingli if (ifa0 == NULL) { 1315192282Sqingli memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1316192282Sqingli sizeof(mltaddr.sin6_addr)); 1317192282Sqingli 1318192282Sqingli RTFREE_LOCKED(rt); 1319192282Sqingli error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1320192282Sqingli (struct sockaddr *)&ia->ia_addr, 1321192282Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1322192282Sqingli (struct rtentry **)0); 1323192282Sqingli 1324192282Sqingli if (error) 1325192282Sqingli log(LOG_INFO, "in6_purgeaddr: node-local all-nodes" 1326192282Sqingli "multicast address deletion error\n"); 1327192282Sqingli } else { 1328192282Sqingli /* 1329192282Sqingli * replace the gateway of the route 1330192282Sqingli */ 1331192282Sqingli struct sockaddr_in6 sa; 1332192282Sqingli 1333192282Sqingli bzero(&sa, sizeof(sa)); 1334192282Sqingli sa.sin6_len = sizeof(struct sockaddr_in6); 1335192282Sqingli sa.sin6_family = AF_INET6; 1336192282Sqingli memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1337192282Sqingli sizeof(sa.sin6_addr)); 1338192282Sqingli in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1339192282Sqingli memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1340192282Sqingli RTFREE_LOCKED(rt); 1341192282Sqingli } 1342192282Sqingli } else { 1343192282Sqingli if (rt != NULL) 1344192282Sqingli RTFREE_LOCKED(rt); 1345192282Sqingli } 1346192282Sqingli 1347192282Sqinglicleanup: 1348192282Sqingli 1349192282Sqingli plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1350192282Sqingli if ((ia->ia_flags & IFA_ROUTE) && plen == 128) { 1351192282Sqingli int error; 1352192282Sqingli struct sockaddr *dstaddr; 1353192282Sqingli 1354192282Sqingli /* 1355192282Sqingli * use the interface address if configuring an 1356192282Sqingli * interface address with a /128 prefix len 1357192282Sqingli */ 1358192282Sqingli if (ia->ia_dstaddr.sin6_family == AF_INET6) 1359192282Sqingli dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 1360192282Sqingli else 1361192282Sqingli dstaddr = (struct sockaddr *)&ia->ia_addr; 1362192282Sqingli 1363192282Sqingli error = rtrequest(RTM_DELETE, 1364192282Sqingli (struct sockaddr *)dstaddr, 1365192282Sqingli (struct sockaddr *)&ia->ia_addr, 1366192282Sqingli (struct sockaddr *)&ia->ia_prefixmask, 1367192282Sqingli ia->ia_flags | RTF_HOST, NULL); 1368192282Sqingli if (error != 0) 1369192282Sqingli return; 1370192282Sqingli ia->ia_flags &= ~IFA_ROUTE; 1371192282Sqingli } 1372194760Srwatson if (ifa0 != NULL) 1373194760Srwatson ifa_free(ifa0); 1374192282Sqingli 137578064Sume in6_unlink_ifa(ia, ifp); 137678064Sume} 137778064Sume 137878064Sumestatic void 1379171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 138078064Sume{ 138178064Sume int s = splnet(); 138278064Sume 1383191323Srwatson IF_ADDR_LOCK(ifp); 1384191340Srwatson TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 1385191323Srwatson IF_ADDR_UNLOCK(ifp); 1386194760Srwatson ifa_free(&ia->ia_ifa); /* if_addrhead */ 138762587Sitojun 1388195102Srwatson /* 1389195102Srwatson * Defer the release of what might be the last reference to the 1390195102Srwatson * in6_ifaddr so that it can't be freed before the remainder of the 1391195102Srwatson * cleanup. 1392195102Srwatson */ 1393194971Srwatson IN6_IFADDR_WLOCK(); 1394194907Srwatson TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link); 1395194971Srwatson IN6_IFADDR_WUNLOCK(); 139662587Sitojun 139762587Sitojun /* 1398151915Ssuz * Release the reference to the base prefix. There should be a 1399151915Ssuz * positive reference. 140062587Sitojun */ 1401194907Srwatson if (ia->ia6_ndpr == NULL) { 1402151915Ssuz nd6log((LOG_NOTICE, 1403151915Ssuz "in6_unlink_ifa: autoconf'ed address " 1404194907Srwatson "%p has no prefix\n", ia)); 1405151915Ssuz } else { 1406194907Srwatson ia->ia6_ndpr->ndpr_refcnt--; 1407194907Srwatson ia->ia6_ndpr = NULL; 1408151915Ssuz } 140962587Sitojun 1410151915Ssuz /* 1411151915Ssuz * Also, if the address being removed is autoconf'ed, call 1412151915Ssuz * pfxlist_onlink_check() since the release might affect the status of 1413171260Sdelphij * other (detached) addresses. 1414151915Ssuz */ 1415194907Srwatson if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) { 141678064Sume pfxlist_onlink_check(); 141762587Sitojun } 1418195102Srwatson ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */ 141978064Sume splx(s); 142062587Sitojun} 142162587Sitojun 142278064Sumevoid 1423171259Sdelphijin6_purgeif(struct ifnet *ifp) 142478064Sume{ 142578064Sume struct ifaddr *ifa, *nifa; 142678064Sume 1427191340Srwatson TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 142878064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 142978064Sume continue; 143078064Sume in6_purgeaddr(ifa); 143178064Sume } 143278064Sume 143378064Sume in6_ifdetach(ifp); 143478064Sume} 143578064Sume 143653541Sshin/* 143753541Sshin * SIOC[GAD]LIFADDR. 143862744Sgrog * SIOCGLIFADDR: get first address. (?) 143953541Sshin * SIOCGLIFADDR with IFLR_PREFIX: 144053541Sshin * get first address that matches the specified prefix. 144153541Sshin * SIOCALIFADDR: add the specified address. 144253541Sshin * SIOCALIFADDR with IFLR_PREFIX: 144353541Sshin * add the specified prefix, filling hostid part from 144453541Sshin * the first link-local address. prefixlen must be <= 64. 144553541Sshin * SIOCDLIFADDR: delete the specified address. 144653541Sshin * SIOCDLIFADDR with IFLR_PREFIX: 144753541Sshin * delete the first address that matches the specified prefix. 144853541Sshin * return values: 144953541Sshin * EINVAL on invalid parameters 145053541Sshin * EADDRNOTAVAIL on prefix match failed/specified address not found 145153541Sshin * other values may be returned from in6_ioctl() 145253541Sshin * 145353541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 145453541Sshin * this is to accomodate address naming scheme other than RFC2374, 145553541Sshin * in the future. 145653541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 145753541Sshin * address encoding scheme. (see figure on page 8) 145853541Sshin */ 145953541Sshinstatic int 1460171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1461171259Sdelphij struct ifnet *ifp, struct thread *td) 146253541Sshin{ 146353541Sshin struct if_laddrreq *iflr = (struct if_laddrreq *)data; 146453541Sshin struct ifaddr *ifa; 146562587Sitojun struct sockaddr *sa; 146653541Sshin 146753541Sshin /* sanity checks */ 146853541Sshin if (!data || !ifp) { 146953541Sshin panic("invalid argument to in6_lifaddr_ioctl"); 1470120891Sume /* NOTREACHED */ 147153541Sshin } 147253541Sshin 147353541Sshin switch (cmd) { 147453541Sshin case SIOCGLIFADDR: 147553541Sshin /* address must be specified on GET with IFLR_PREFIX */ 147653541Sshin if ((iflr->flags & IFLR_PREFIX) == 0) 147753541Sshin break; 147895023Ssuz /* FALLTHROUGH */ 147953541Sshin case SIOCALIFADDR: 148053541Sshin case SIOCDLIFADDR: 148153541Sshin /* address must be specified on ADD and DELETE */ 148262587Sitojun sa = (struct sockaddr *)&iflr->addr; 148362587Sitojun if (sa->sa_family != AF_INET6) 148453541Sshin return EINVAL; 148562587Sitojun if (sa->sa_len != sizeof(struct sockaddr_in6)) 148653541Sshin return EINVAL; 148753541Sshin /* XXX need improvement */ 148862587Sitojun sa = (struct sockaddr *)&iflr->dstaddr; 148962587Sitojun if (sa->sa_family && sa->sa_family != AF_INET6) 149053541Sshin return EINVAL; 149162587Sitojun if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 149253541Sshin return EINVAL; 149353541Sshin break; 149495023Ssuz default: /* shouldn't happen */ 149562587Sitojun#if 0 149662587Sitojun panic("invalid cmd to in6_lifaddr_ioctl"); 149795023Ssuz /* NOTREACHED */ 149862587Sitojun#else 149953541Sshin return EOPNOTSUPP; 150062587Sitojun#endif 150153541Sshin } 150253541Sshin if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 150353541Sshin return EINVAL; 150453541Sshin 150553541Sshin switch (cmd) { 150653541Sshin case SIOCALIFADDR: 150753541Sshin { 150853541Sshin struct in6_aliasreq ifra; 150953541Sshin struct in6_addr *hostid = NULL; 151053541Sshin int prefixlen; 151153541Sshin 1512194760Srwatson ifa = NULL; 151353541Sshin if ((iflr->flags & IFLR_PREFIX) != 0) { 151453541Sshin struct sockaddr_in6 *sin6; 151553541Sshin 151653541Sshin /* 151753541Sshin * hostid is to fill in the hostid part of the 151853541Sshin * address. hostid points to the first link-local 151953541Sshin * address attached to the interface. 152053541Sshin */ 152162587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 152253541Sshin if (!ifa) 152353541Sshin return EADDRNOTAVAIL; 152453541Sshin hostid = IFA_IN6(ifa); 152553541Sshin 1526171260Sdelphij /* prefixlen must be <= 64. */ 152753541Sshin if (64 < iflr->prefixlen) 152853541Sshin return EINVAL; 152953541Sshin prefixlen = iflr->prefixlen; 153053541Sshin 153153541Sshin /* hostid part must be zero. */ 153253541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 1533126552Sume if (sin6->sin6_addr.s6_addr32[2] != 0 || 1534126552Sume sin6->sin6_addr.s6_addr32[3] != 0) { 153553541Sshin return EINVAL; 153653541Sshin } 153753541Sshin } else 153853541Sshin prefixlen = iflr->prefixlen; 153953541Sshin 154053541Sshin /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 154153541Sshin bzero(&ifra, sizeof(ifra)); 1542120891Sume bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 154353541Sshin 154462587Sitojun bcopy(&iflr->addr, &ifra.ifra_addr, 1545120891Sume ((struct sockaddr *)&iflr->addr)->sa_len); 154653541Sshin if (hostid) { 154753541Sshin /* fill in hostid part */ 154853541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1549120891Sume hostid->s6_addr32[2]; 155053541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1551120891Sume hostid->s6_addr32[3]; 155253541Sshin } 155353541Sshin 1554120891Sume if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 155553541Sshin bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1556120891Sume ((struct sockaddr *)&iflr->dstaddr)->sa_len); 155753541Sshin if (hostid) { 155853541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1559120891Sume hostid->s6_addr32[2]; 156053541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1561120891Sume hostid->s6_addr32[3]; 156253541Sshin } 156353541Sshin } 1564194760Srwatson if (ifa != NULL) 1565194760Srwatson ifa_free(ifa); 156653541Sshin 156753541Sshin ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1568121168Sume in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 156953541Sshin 157053541Sshin ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 157183366Sjulian return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 157253541Sshin } 157353541Sshin case SIOCGLIFADDR: 157453541Sshin case SIOCDLIFADDR: 157553541Sshin { 157653541Sshin struct in6_ifaddr *ia; 157753541Sshin struct in6_addr mask, candidate, match; 157853541Sshin struct sockaddr_in6 *sin6; 157953541Sshin int cmp; 158053541Sshin 158153541Sshin bzero(&mask, sizeof(mask)); 158253541Sshin if (iflr->flags & IFLR_PREFIX) { 158353541Sshin /* lookup a prefix rather than address. */ 1584121168Sume in6_prefixlen2mask(&mask, iflr->prefixlen); 158553541Sshin 158653541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 158753541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 158853541Sshin match.s6_addr32[0] &= mask.s6_addr32[0]; 158953541Sshin match.s6_addr32[1] &= mask.s6_addr32[1]; 159053541Sshin match.s6_addr32[2] &= mask.s6_addr32[2]; 159153541Sshin match.s6_addr32[3] &= mask.s6_addr32[3]; 159253541Sshin 159353541Sshin /* if you set extra bits, that's wrong */ 159453541Sshin if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 159553541Sshin return EINVAL; 159653541Sshin 159753541Sshin cmp = 1; 159853541Sshin } else { 159953541Sshin if (cmd == SIOCGLIFADDR) { 160053541Sshin /* on getting an address, take the 1st match */ 160195023Ssuz cmp = 0; /* XXX */ 160253541Sshin } else { 160353541Sshin /* on deleting an address, do exact match */ 1604121168Sume in6_prefixlen2mask(&mask, 128); 160553541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 160653541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 160753541Sshin 160853541Sshin cmp = 1; 160953541Sshin } 161053541Sshin } 161153541Sshin 1612191323Srwatson IF_ADDR_LOCK(ifp); 1613191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 161453541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 161553541Sshin continue; 161653541Sshin if (!cmp) 161753541Sshin break; 161878064Sume 161978064Sume /* 162078064Sume * XXX: this is adhoc, but is necessary to allow 162178064Sume * a user to specify fe80::/64 (not /10) for a 162278064Sume * link-local address. 162378064Sume */ 1624148385Sume bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1625148385Sume in6_clearscope(&candidate); 162653541Sshin candidate.s6_addr32[0] &= mask.s6_addr32[0]; 162753541Sshin candidate.s6_addr32[1] &= mask.s6_addr32[1]; 162853541Sshin candidate.s6_addr32[2] &= mask.s6_addr32[2]; 162953541Sshin candidate.s6_addr32[3] &= mask.s6_addr32[3]; 163053541Sshin if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 163153541Sshin break; 163253541Sshin } 1633191323Srwatson IF_ADDR_UNLOCK(ifp); 163453541Sshin if (!ifa) 163553541Sshin return EADDRNOTAVAIL; 163653541Sshin ia = ifa2ia6(ifa); 163753541Sshin 163853541Sshin if (cmd == SIOCGLIFADDR) { 1639148385Sume int error; 164078064Sume 164153541Sshin /* fill in the if_laddrreq structure */ 164253541Sshin bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1643148385Sume error = sa6_recoverscope( 1644148385Sume (struct sockaddr_in6 *)&iflr->addr); 1645148385Sume if (error != 0) 1646148385Sume return (error); 1647148385Sume 164853541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 164953541Sshin bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1650120891Sume ia->ia_dstaddr.sin6_len); 1651148385Sume error = sa6_recoverscope( 1652148385Sume (struct sockaddr_in6 *)&iflr->dstaddr); 1653148385Sume if (error != 0) 1654148385Sume return (error); 165553541Sshin } else 165653541Sshin bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 165753541Sshin 165853541Sshin iflr->prefixlen = 1659120891Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 166053541Sshin 166195023Ssuz iflr->flags = ia->ia6_flags; /* XXX */ 166253541Sshin 166353541Sshin return 0; 166453541Sshin } else { 166553541Sshin struct in6_aliasreq ifra; 166653541Sshin 166753541Sshin /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 166853541Sshin bzero(&ifra, sizeof(ifra)); 166953541Sshin bcopy(iflr->iflr_name, ifra.ifra_name, 1670120891Sume sizeof(ifra.ifra_name)); 167153541Sshin 167253541Sshin bcopy(&ia->ia_addr, &ifra.ifra_addr, 1673120891Sume ia->ia_addr.sin6_len); 167453541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 167553541Sshin bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1676120891Sume ia->ia_dstaddr.sin6_len); 167762587Sitojun } else { 167862587Sitojun bzero(&ifra.ifra_dstaddr, 167962587Sitojun sizeof(ifra.ifra_dstaddr)); 168053541Sshin } 168153541Sshin bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1682120891Sume ia->ia_prefixmask.sin6_len); 168353541Sshin 168453541Sshin ifra.ifra_flags = ia->ia6_flags; 168553541Sshin return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1686120891Sume ifp, td); 168753541Sshin } 168853541Sshin } 168953541Sshin } 169053541Sshin 169195023Ssuz return EOPNOTSUPP; /* just for safety */ 169253541Sshin} 169353541Sshin 169453541Sshin/* 169553541Sshin * Initialize an interface's intetnet6 address 169653541Sshin * and routing table entry. 169753541Sshin */ 169878064Sumestatic int 1699171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1700171259Sdelphij struct sockaddr_in6 *sin6, int newhost) 170153541Sshin{ 170278064Sume int error = 0, plen, ifacount = 0; 170353541Sshin int s = splimp(); 170478064Sume struct ifaddr *ifa; 170553541Sshin 170653541Sshin /* 170753541Sshin * Give the interface a chance to initialize 170853541Sshin * if this is its first address, 170953541Sshin * and to validate the address if necessary. 171053541Sshin */ 1711191323Srwatson IF_ADDR_LOCK(ifp); 1712191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 171378064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 171478064Sume continue; 171578064Sume ifacount++; 171678064Sume } 1717191323Srwatson IF_ADDR_UNLOCK(ifp); 171878064Sume 171978064Sume ia->ia_addr = *sin6; 172078064Sume 1721146883Siedowse if (ifacount <= 1 && ifp->if_ioctl) { 1722146883Siedowse error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1723146883Siedowse if (error) { 1724146883Siedowse splx(s); 1725146883Siedowse return (error); 1726146883Siedowse } 172753541Sshin } 172878064Sume splx(s); 172953541Sshin 173078064Sume ia->ia_ifa.ifa_metric = ifp->if_metric; 173153541Sshin 173278064Sume /* we could do in(6)_socktrim here, but just omit it at this moment. */ 173378064Sume 173453541Sshin /* 173578064Sume * Special case: 1736124337Sume * If a new destination address is specified for a point-to-point 173778064Sume * interface, install a route to the destination as an interface 1738186119Sqingli * direct route. 1739124337Sume * XXX: the logic below rejects assigning multiple addresses on a p2p 1740159390Sgnn * interface that share the same destination. 174153541Sshin */ 174278064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1743196152Sqingli if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1744196152Sqingli ia->ia_dstaddr.sin6_family == AF_INET6) { 1745159390Sgnn int rtflags = RTF_UP | RTF_HOST; 1746159390Sgnn 1747178888Sjulian error = rtrequest(RTM_ADD, 1748196152Sqingli (struct sockaddr *)&ia->ia_dstaddr, 1749159390Sgnn (struct sockaddr *)&ia->ia_addr, 1750159390Sgnn (struct sockaddr *)&ia->ia_prefixmask, 1751186708Sqingli ia->ia_flags | rtflags, NULL); 1752159390Sgnn if (error != 0) 1753120856Sume return (error); 175478064Sume ia->ia_flags |= IFA_ROUTE; 175553541Sshin } 175653541Sshin 1757195643Sqingli /* 1758195643Sqingli * add a loopback route to self 1759195643Sqingli */ 1760196871Sqingli if (!(ia->ia_flags & IFA_ROUTE) 1761196871Sqingli && (V_nd6_useloopback 1762196871Sqingli || (ifp->if_flags & IFF_LOOPBACK))) { 1763197227Sqingli error = ifa_add_loopback_route((struct ifaddr *)ia, 1764197227Sqingli (struct sockaddr *)&ia->ia_addr); 1765195643Sqingli } 1766195643Sqingli 176795023Ssuz /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1768186119Sqingli if (newhost) { 1769186119Sqingli struct llentry *ln; 1770192282Sqingli struct rtentry rt; 1771192282Sqingli struct sockaddr_dl gateway; 1772192282Sqingli struct sockaddr_in6 mask, addr; 177353541Sshin 1774186119Sqingli IF_AFDATA_LOCK(ifp); 1775186119Sqingli ia->ia_ifa.ifa_rtrequest = NULL; 1776186119Sqingli 1777186119Sqingli /* XXX QL 1778186119Sqingli * we need to report rt_newaddrmsg 1779186119Sqingli */ 1780186119Sqingli ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE), 1781186119Sqingli (struct sockaddr *)&ia->ia_addr); 1782186119Sqingli IF_AFDATA_UNLOCK(ifp); 1783186158Skmacy if (ln != NULL) { 1784186119Sqingli ln->la_expire = 0; /* for IPv6 this means permanent */ 1785186119Sqingli ln->ln_state = ND6_LLINFO_REACHABLE; 1786192282Sqingli /* 1787192282Sqingli * initialize for rtmsg generation 1788192282Sqingli */ 1789192282Sqingli bzero(&gateway, sizeof(gateway)); 1790192282Sqingli gateway.sdl_len = sizeof(gateway); 1791192282Sqingli gateway.sdl_family = AF_LINK; 1792192282Sqingli gateway.sdl_nlen = 0; 1793192282Sqingli gateway.sdl_alen = 6; 1794192282Sqingli memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr)); 1795192282Sqingli /* */ 1796186119Sqingli LLE_WUNLOCK(ln); 1797186119Sqingli } 1798192282Sqingli 1799192282Sqingli bzero(&rt, sizeof(rt)); 1800192282Sqingli rt.rt_gateway = (struct sockaddr *)&gateway; 1801192282Sqingli memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1802192282Sqingli memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1803192282Sqingli rt_mask(&rt) = (struct sockaddr *)&mask; 1804192282Sqingli rt_key(&rt) = (struct sockaddr *)&addr; 1805192282Sqingli rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC; 1806192282Sqingli rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt); 1807186119Sqingli } 1808186119Sqingli 1809120856Sume return (error); 181053541Sshin} 181153541Sshin 181253541Sshin/* 181353541Sshin * Find an IPv6 interface link-local address specific to an interface. 1814194760Srwatson * ifaddr is returned referenced. 181553541Sshin */ 181653541Sshinstruct in6_ifaddr * 1817171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 181853541Sshin{ 181978064Sume struct ifaddr *ifa; 182053541Sshin 1821191323Srwatson IF_ADDR_LOCK(ifp); 1822191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 182353541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 182453541Sshin continue; 182562587Sitojun if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 182662587Sitojun if ((((struct in6_ifaddr *)ifa)->ia6_flags & 182762587Sitojun ignoreflags) != 0) 182862587Sitojun continue; 1829194760Srwatson ifa_ref(ifa); 183053541Sshin break; 183162587Sitojun } 183253541Sshin } 1833191323Srwatson IF_ADDR_UNLOCK(ifp); 183453541Sshin 1835120856Sume return ((struct in6_ifaddr *)ifa); 183653541Sshin} 183753541Sshin 183853541Sshin 183953541Sshin/* 184053541Sshin * find the internet address corresponding to a given interface and address. 1841194760Srwatson * ifaddr is returned referenced. 184253541Sshin */ 184353541Sshinstruct in6_ifaddr * 1844171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 184553541Sshin{ 184678064Sume struct ifaddr *ifa; 184753541Sshin 1848191337Srwatson IF_ADDR_LOCK(ifp); 1849191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 185053541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 185153541Sshin continue; 1852194760Srwatson if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 1853194760Srwatson ifa_ref(ifa); 185453541Sshin break; 1855194760Srwatson } 185653541Sshin } 1857191337Srwatson IF_ADDR_UNLOCK(ifp); 185853541Sshin 1859120856Sume return ((struct in6_ifaddr *)ifa); 186053541Sshin} 186153541Sshin 186253541Sshin/* 1863165118Sbz * Convert IP6 address to printable (loggable) representation. Caller 1864165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 186553541Sshin */ 186653541Sshinstatic char digits[] = "0123456789abcdef"; 186753541Sshinchar * 1868165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr) 186953541Sshin{ 187078064Sume int i; 187178064Sume char *cp; 1872126552Sume const u_int16_t *a = (const u_int16_t *)addr; 1873126552Sume const u_int8_t *d; 1874165287Sbz int dcolon = 0, zero = 0; 187553541Sshin 1876165118Sbz cp = ip6buf; 187753541Sshin 187853541Sshin for (i = 0; i < 8; i++) { 187953541Sshin if (dcolon == 1) { 188053541Sshin if (*a == 0) { 188153541Sshin if (i == 7) 188253541Sshin *cp++ = ':'; 188353541Sshin a++; 188453541Sshin continue; 188553541Sshin } else 188653541Sshin dcolon = 2; 188753541Sshin } 188853541Sshin if (*a == 0) { 188953541Sshin if (dcolon == 0 && *(a + 1) == 0) { 189053541Sshin if (i == 0) 189153541Sshin *cp++ = ':'; 189253541Sshin *cp++ = ':'; 189353541Sshin dcolon = 1; 189453541Sshin } else { 189553541Sshin *cp++ = '0'; 189653541Sshin *cp++ = ':'; 189753541Sshin } 189853541Sshin a++; 189953541Sshin continue; 190053541Sshin } 190191346Salfred d = (const u_char *)a; 1902165287Sbz /* Try to eliminate leading zeros in printout like in :0001. */ 1903165287Sbz zero = 1; 1904165287Sbz *cp = digits[*d >> 4]; 1905165287Sbz if (*cp != '0') { 1906165287Sbz zero = 0; 1907165287Sbz cp++; 1908165287Sbz } 1909165287Sbz *cp = digits[*d++ & 0xf]; 1910165287Sbz if (zero == 0 || (*cp != '0')) { 1911165287Sbz zero = 0; 1912165287Sbz cp++; 1913165287Sbz } 1914165287Sbz *cp = digits[*d >> 4]; 1915165287Sbz if (zero == 0 || (*cp != '0')) { 1916165287Sbz zero = 0; 1917165287Sbz cp++; 1918165287Sbz } 191953541Sshin *cp++ = digits[*d & 0xf]; 192053541Sshin *cp++ = ':'; 192153541Sshin a++; 192253541Sshin } 1923165118Sbz *--cp = '\0'; 1924165118Sbz return (ip6buf); 192553541Sshin} 192653541Sshin 192753541Sshinint 1928171259Sdelphijin6_localaddr(struct in6_addr *in6) 192953541Sshin{ 193053541Sshin struct in6_ifaddr *ia; 193153541Sshin 193253541Sshin if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 193353541Sshin return 1; 193453541Sshin 1935194971Srwatson IN6_IFADDR_RLOCK(); 1936194907Srwatson TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 193753541Sshin if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1938120891Sume &ia->ia_prefixmask.sin6_addr)) { 1939194971Srwatson IN6_IFADDR_RUNLOCK(); 194053541Sshin return 1; 1941120891Sume } 1942120891Sume } 1943194971Srwatson IN6_IFADDR_RUNLOCK(); 194453541Sshin 194553541Sshin return (0); 194653541Sshin} 194753541Sshin 194878064Sumeint 1949171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6) 195078064Sume{ 195178064Sume struct in6_ifaddr *ia; 195278064Sume 1953194971Srwatson IN6_IFADDR_RLOCK(); 1954194907Srwatson TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 195578064Sume if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 195678064Sume &sa6->sin6_addr) && 1957194971Srwatson (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { 1958194971Srwatson IN6_IFADDR_RUNLOCK(); 1959120856Sume return (1); /* true */ 1960194971Srwatson } 196178064Sume 196278064Sume /* XXX: do we still have to go thru the rest of the list? */ 196378064Sume } 1964194971Srwatson IN6_IFADDR_RUNLOCK(); 196578064Sume 1966120856Sume return (0); /* false */ 196778064Sume} 196878064Sume 196953541Sshin/* 197053541Sshin * return length of part which dst and src are equal 197153541Sshin * hard coding... 197253541Sshin */ 197353541Sshinint 1974171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst) 197553541Sshin{ 197653541Sshin int match = 0; 197753541Sshin u_char *s = (u_char *)src, *d = (u_char *)dst; 197853541Sshin u_char *lim = s + 16, r; 197953541Sshin 198053541Sshin while (s < lim) 198153541Sshin if ((r = (*d++ ^ *s++)) != 0) { 198253541Sshin while (r < 128) { 198353541Sshin match++; 198453541Sshin r <<= 1; 198553541Sshin } 198653541Sshin break; 198753541Sshin } else 198853541Sshin match += 8; 198953541Sshin return match; 199053541Sshin} 199153541Sshin 199262587Sitojun/* XXX: to be scope conscious */ 199353541Sshinint 1994171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 199553541Sshin{ 199653541Sshin int bytelen, bitlen; 199753541Sshin 199853541Sshin /* sanity check */ 199953541Sshin if (0 > len || len > 128) { 200053541Sshin log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 200153541Sshin len); 2002120856Sume return (0); 200353541Sshin } 200453541Sshin 200553541Sshin bytelen = len / 8; 200653541Sshin bitlen = len % 8; 200753541Sshin 200853541Sshin if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2009120856Sume return (0); 2010126184Scperciva if (bitlen != 0 && 2011126184Scperciva p1->s6_addr[bytelen] >> (8 - bitlen) != 201253541Sshin p2->s6_addr[bytelen] >> (8 - bitlen)) 2013120856Sume return (0); 201453541Sshin 2015120856Sume return (1); 201653541Sshin} 201753541Sshin 201853541Sshinvoid 2019171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len) 202053541Sshin{ 202153541Sshin u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 202253541Sshin int bytelen, bitlen, i; 202353541Sshin 202453541Sshin /* sanity check */ 202553541Sshin if (0 > len || len > 128) { 202653541Sshin log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 202753541Sshin len); 202853541Sshin return; 202953541Sshin } 203053541Sshin 203153541Sshin bzero(maskp, sizeof(*maskp)); 203253541Sshin bytelen = len / 8; 203353541Sshin bitlen = len % 8; 203453541Sshin for (i = 0; i < bytelen; i++) 203553541Sshin maskp->s6_addr[i] = 0xff; 203653541Sshin if (bitlen) 203753541Sshin maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 203853541Sshin} 203953541Sshin 204053541Sshin/* 204153541Sshin * return the best address out of the same scope. if no address was 204253541Sshin * found, return the first valid address from designated IF. 204353541Sshin */ 204453541Sshinstruct in6_ifaddr * 2045171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 204653541Sshin{ 204753541Sshin int dst_scope = in6_addrscope(dst), blen = -1, tlen; 204853541Sshin struct ifaddr *ifa; 204953541Sshin struct in6_ifaddr *besta = 0; 205095023Ssuz struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 205153541Sshin 205253541Sshin dep[0] = dep[1] = NULL; 205353541Sshin 205453541Sshin /* 205553541Sshin * We first look for addresses in the same scope. 205653541Sshin * If there is one, return it. 205753541Sshin * If two or more, return one which matches the dst longest. 205853541Sshin * If none, return one of global addresses assigned other ifs. 205953541Sshin */ 2060191323Srwatson IF_ADDR_LOCK(ifp); 2061191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 206253541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 206353541Sshin continue; 206453541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 206553541Sshin continue; /* XXX: is there any case to allow anycast? */ 206653541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 206753541Sshin continue; /* don't use this interface */ 206853541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 206953541Sshin continue; 207053541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2071181803Sbz if (V_ip6_use_deprecated) 207253541Sshin dep[0] = (struct in6_ifaddr *)ifa; 207353541Sshin continue; 207453541Sshin } 207553541Sshin 207653541Sshin if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 207753541Sshin /* 207853541Sshin * call in6_matchlen() as few as possible 207953541Sshin */ 208053541Sshin if (besta) { 208153541Sshin if (blen == -1) 208253541Sshin blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 208353541Sshin tlen = in6_matchlen(IFA_IN6(ifa), dst); 208453541Sshin if (tlen > blen) { 208553541Sshin blen = tlen; 208653541Sshin besta = (struct in6_ifaddr *)ifa; 208753541Sshin } 208853541Sshin } else 208953541Sshin besta = (struct in6_ifaddr *)ifa; 209053541Sshin } 209153541Sshin } 2092191323Srwatson if (besta) { 2093194760Srwatson ifa_ref(&besta->ia_ifa); 2094191323Srwatson IF_ADDR_UNLOCK(ifp); 2095120856Sume return (besta); 2096191323Srwatson } 2097194971Srwatson IF_ADDR_UNLOCK(ifp); 209853541Sshin 2099194971Srwatson IN6_IFADDR_RLOCK(); 2100191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 210153541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 210253541Sshin continue; 210353541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 210453541Sshin continue; /* XXX: is there any case to allow anycast? */ 210553541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 210653541Sshin continue; /* don't use this interface */ 210753541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 210853541Sshin continue; 210953541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2110181803Sbz if (V_ip6_use_deprecated) 211153541Sshin dep[1] = (struct in6_ifaddr *)ifa; 211253541Sshin continue; 211353541Sshin } 211453541Sshin 2115194760Srwatson if (ifa != NULL) 2116194760Srwatson ifa_ref(ifa); 2117194971Srwatson IN6_IFADDR_RUNLOCK(); 211853541Sshin return (struct in6_ifaddr *)ifa; 211953541Sshin } 2120194971Srwatson IN6_IFADDR_RUNLOCK(); 212153541Sshin 212253541Sshin /* use the last-resort values, that are, deprecated addresses */ 212353541Sshin if (dep[0]) 212453541Sshin return dep[0]; 212553541Sshin if (dep[1]) 212653541Sshin return dep[1]; 212753541Sshin 212853541Sshin return NULL; 212953541Sshin} 213053541Sshin 213153541Sshin/* 213253541Sshin * perform DAD when interface becomes IFF_UP. 213353541Sshin */ 213453541Sshinvoid 2135171259Sdelphijin6_if_up(struct ifnet *ifp) 213653541Sshin{ 213753541Sshin struct ifaddr *ifa; 213853541Sshin struct in6_ifaddr *ia; 213953541Sshin 2140191323Srwatson IF_ADDR_LOCK(ifp); 2141191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 214253541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 214353541Sshin continue; 214453541Sshin ia = (struct in6_ifaddr *)ifa; 2145151539Ssuz if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2146151539Ssuz /* 2147151539Ssuz * The TENTATIVE flag was likely set by hand 2148151539Ssuz * beforehand, implicitly indicating the need for DAD. 2149151539Ssuz * We may be able to skip the random delay in this 2150151539Ssuz * case, but we impose delays just in case. 2151151539Ssuz */ 2152151539Ssuz nd6_dad_start(ifa, 2153151539Ssuz arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 2154151539Ssuz } 215553541Sshin } 2156191323Srwatson IF_ADDR_UNLOCK(ifp); 2157151539Ssuz 2158151539Ssuz /* 2159151539Ssuz * special cases, like 6to4, are handled in in6_ifattach 2160151539Ssuz */ 2161151539Ssuz in6_ifattach(ifp, NULL); 216253541Sshin} 216353541Sshin 216478064Sumeint 2165171259Sdelphijin6if_do_dad(struct ifnet *ifp) 216678064Sume{ 216778064Sume if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2168120856Sume return (0); 216978064Sume 2170197138Shrs if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 2171197138Shrs return (0); 2172197138Shrs 217378064Sume switch (ifp->if_type) { 217478064Sume#ifdef IFT_DUMMY 217578064Sume case IFT_DUMMY: 217678064Sume#endif 217778064Sume case IFT_FAITH: 217878064Sume /* 217978064Sume * These interfaces do not have the IFF_LOOPBACK flag, 218078064Sume * but loop packets back. We do not have to do DAD on such 218178064Sume * interfaces. We should even omit it, because loop-backed 218278064Sume * NS would confuse the DAD procedure. 218378064Sume */ 2184120856Sume return (0); 218578064Sume default: 218678064Sume /* 218778064Sume * Our DAD routine requires the interface up and running. 218878064Sume * However, some interfaces can be up before the RUNNING 218978064Sume * status. Additionaly, users may try to assign addresses 219078064Sume * before the interface becomes up (or running). 219178064Sume * We simply skip DAD in such a case as a work around. 219278064Sume * XXX: we should rather mark "tentative" on such addresses, 219378064Sume * and do DAD after the interface becomes ready. 219478064Sume */ 2195148887Srwatson if (!((ifp->if_flags & IFF_UP) && 2196148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 2197120856Sume return (0); 219878064Sume 2199120856Sume return (1); 220078064Sume } 220178064Sume} 220278064Sume 220353541Sshin/* 220453541Sshin * Calculate max IPv6 MTU through all the interfaces and store it 220553541Sshin * to in6_maxmtu. 220653541Sshin */ 220753541Sshinvoid 2208171259Sdelphijin6_setmaxmtu(void) 220953541Sshin{ 221053541Sshin unsigned long maxmtu = 0; 221153541Sshin struct ifnet *ifp; 221253541Sshin 2213196481Srwatson IFNET_RLOCK_NOSLEEP(); 2214181887Sjulian for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 2215181888Sjulian ifp = TAILQ_NEXT(ifp, if_list)) { 2216121283Sume /* this function can be called during ifnet initialization */ 2217121283Sume if (!ifp->if_afdata[AF_INET6]) 2218121283Sume continue; 221953541Sshin if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2220121283Sume IN6_LINKMTU(ifp) > maxmtu) 2221121283Sume maxmtu = IN6_LINKMTU(ifp); 222253541Sshin } 2223196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 2224120891Sume if (maxmtu) /* update only when maxmtu is positive */ 2225181803Sbz V_in6_maxmtu = maxmtu; 222653541Sshin} 222753541Sshin 2228151539Ssuz/* 2229151539Ssuz * Provide the length of interface identifiers to be used for the link attached 2230151539Ssuz * to the given interface. The length should be defined in "IPv6 over 2231151539Ssuz * xxx-link" document. Note that address architecture might also define 2232151539Ssuz * the length for a particular set of address prefixes, regardless of the 2233151539Ssuz * link type. As clarified in rfc2462bis, those two definitions should be 2234151539Ssuz * consistent, and those really are as of August 2004. 2235151539Ssuz */ 2236151539Ssuzint 2237171259Sdelphijin6_if2idlen(struct ifnet *ifp) 2238151539Ssuz{ 2239151539Ssuz switch (ifp->if_type) { 2240151539Ssuz case IFT_ETHER: /* RFC2464 */ 2241151539Ssuz#ifdef IFT_PROPVIRTUAL 2242151539Ssuz case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2243151539Ssuz#endif 2244151539Ssuz#ifdef IFT_L2VLAN 2245151539Ssuz case IFT_L2VLAN: /* ditto */ 2246151539Ssuz#endif 2247151539Ssuz#ifdef IFT_IEEE80211 2248151539Ssuz case IFT_IEEE80211: /* ditto */ 2249151539Ssuz#endif 2250151539Ssuz#ifdef IFT_MIP 2251151539Ssuz case IFT_MIP: /* ditto */ 2252151539Ssuz#endif 2253151539Ssuz return (64); 2254151539Ssuz case IFT_FDDI: /* RFC2467 */ 2255151539Ssuz return (64); 2256151539Ssuz case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2257151539Ssuz return (64); 2258151539Ssuz case IFT_PPP: /* RFC2472 */ 2259151539Ssuz return (64); 2260151539Ssuz case IFT_ARCNET: /* RFC2497 */ 2261151539Ssuz return (64); 2262151539Ssuz case IFT_FRELAY: /* RFC2590 */ 2263151539Ssuz return (64); 2264151539Ssuz case IFT_IEEE1394: /* RFC3146 */ 2265151539Ssuz return (64); 2266151539Ssuz case IFT_GIF: 2267151539Ssuz return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2268151539Ssuz case IFT_LOOP: 2269151539Ssuz return (64); /* XXX: is this really correct? */ 2270151539Ssuz default: 2271151539Ssuz /* 2272151539Ssuz * Unknown link type: 2273151539Ssuz * It might be controversial to use the today's common constant 2274151539Ssuz * of 64 for these cases unconditionally. For full compliance, 2275151539Ssuz * we should return an error in this case. On the other hand, 2276151539Ssuz * if we simply miss the standard for the link type or a new 2277151539Ssuz * standard is defined for a new link type, the IFID length 2278151539Ssuz * is very likely to be the common constant. As a compromise, 2279151539Ssuz * we always use the constant, but make an explicit notice 2280151539Ssuz * indicating the "unknown" case. 2281151539Ssuz */ 2282151539Ssuz printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2283151539Ssuz return (64); 2284151539Ssuz } 2285151539Ssuz} 2286151539Ssuz 2287186119Sqingli#include <sys/sysctl.h> 2288186119Sqingli 2289186119Sqinglistruct in6_llentry { 2290186119Sqingli struct llentry base; 2291186119Sqingli struct sockaddr_in6 l3_addr6; 2292186119Sqingli}; 2293186119Sqingli 2294186119Sqinglistatic struct llentry * 2295186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2296186119Sqingli{ 2297186119Sqingli struct in6_llentry *lle; 2298186119Sqingli 2299186119Sqingli lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, 2300186119Sqingli M_DONTWAIT | M_ZERO); 2301186119Sqingli if (lle == NULL) /* NB: caller generates msg */ 2302186119Sqingli return NULL; 2303186119Sqingli 2304186119Sqingli callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE); 2305186119Sqingli lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2306186119Sqingli lle->base.lle_refcnt = 1; 2307186119Sqingli LLE_LOCK_INIT(&lle->base); 2308186119Sqingli return &lle->base; 2309186119Sqingli} 2310186119Sqingli 2311186119Sqingli/* 2312186119Sqingli * Deletes an address from the address table. 2313186119Sqingli * This function is called by the timer functions 2314186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and 2315186119Sqingli * the caller does the locking. 2316186119Sqingli */ 2317186119Sqinglistatic void 2318186119Sqingliin6_lltable_free(struct lltable *llt, struct llentry *lle) 2319186119Sqingli{ 2320186150Skmacy LLE_WUNLOCK(lle); 2321186150Skmacy LLE_LOCK_DESTROY(lle); 2322186119Sqingli free(lle, M_LLTABLE); 2323186119Sqingli} 2324186119Sqingli 2325192476Sqinglistatic void 2326192476Sqingliin6_lltable_prefix_free(struct lltable *llt, 2327192476Sqingli const struct sockaddr *prefix, 2328192476Sqingli const struct sockaddr *mask) 2329192476Sqingli{ 2330192476Sqingli const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix; 2331192476Sqingli const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask; 2332192476Sqingli struct llentry *lle, *next; 2333192476Sqingli register int i; 2334192476Sqingli 2335192476Sqingli for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 2336192476Sqingli LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 2337192476Sqingli if (IN6_ARE_MASKED_ADDR_EQUAL( 2338192476Sqingli &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr, 2339192476Sqingli &pfx->sin6_addr, 2340192476Sqingli &msk->sin6_addr)) { 2341192476Sqingli callout_drain(&lle->la_timer); 2342192476Sqingli LLE_WLOCK(lle); 2343192476Sqingli llentry_free(lle); 2344192476Sqingli } 2345192476Sqingli } 2346192476Sqingli } 2347192476Sqingli} 2348192476Sqingli 2349186119Sqinglistatic int 2350186119Sqingliin6_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 2351186119Sqingli{ 2352186119Sqingli struct rtentry *rt; 2353186119Sqingli char ip6buf[INET6_ADDRSTRLEN]; 2354186119Sqingli 2355186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2356186119Sqingli ("sin_family %d", l3addr->sa_family)); 2357186119Sqingli 2358186119Sqingli /* XXX rtalloc1 should take a const param */ 2359186119Sqingli rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 2360186119Sqingli if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2361186119Sqingli struct ifaddr *ifa; 2362186119Sqingli /* 2363186119Sqingli * Create an ND6 cache for an IPv6 neighbor 2364186119Sqingli * that is not covered by our own prefix. 2365186119Sqingli */ 2366186119Sqingli /* XXX ifaof_ifpforaddr should take a const param */ 2367186119Sqingli ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2368186119Sqingli if (ifa != NULL) { 2369194760Srwatson ifa_free(ifa); 2370186119Sqingli if (rt != NULL) 2371187946Sbz RTFREE_LOCKED(rt); 2372186119Sqingli return 0; 2373186119Sqingli } 2374186119Sqingli log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2375186119Sqingli ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2376186119Sqingli if (rt != NULL) 2377187946Sbz RTFREE_LOCKED(rt); 2378186119Sqingli return EINVAL; 2379186119Sqingli } 2380187946Sbz RTFREE_LOCKED(rt); 2381186119Sqingli return 0; 2382186119Sqingli} 2383186119Sqingli 2384186119Sqinglistatic struct llentry * 2385186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags, 2386186119Sqingli const struct sockaddr *l3addr) 2387186119Sqingli{ 2388186119Sqingli const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2389186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2390186119Sqingli struct llentry *lle; 2391186119Sqingli struct llentries *lleh; 2392186119Sqingli u_int hashkey; 2393186119Sqingli 2394186119Sqingli IF_AFDATA_LOCK_ASSERT(ifp); 2395186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2396186119Sqingli ("sin_family %d", l3addr->sa_family)); 2397186119Sqingli 2398186119Sqingli hashkey = sin6->sin6_addr.s6_addr32[3]; 2399186119Sqingli lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2400186119Sqingli LIST_FOREACH(lle, lleh, lle_next) { 2401186708Sqingli struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle); 2402186119Sqingli if (lle->la_flags & LLE_DELETED) 2403186119Sqingli continue; 2404186708Sqingli if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr, 2405186708Sqingli sizeof(struct in6_addr)) == 0) 2406186119Sqingli break; 2407186119Sqingli } 2408186119Sqingli 2409186119Sqingli if (lle == NULL) { 2410186119Sqingli if (!(flags & LLE_CREATE)) 2411186119Sqingli return (NULL); 2412186119Sqingli /* 2413186119Sqingli * A route that covers the given address must have 2414186119Sqingli * been installed 1st because we are doing a resolution, 2415186119Sqingli * verify this. 2416186119Sqingli */ 2417186119Sqingli if (!(flags & LLE_IFADDR) && 2418186119Sqingli in6_lltable_rtcheck(ifp, l3addr) != 0) 2419186119Sqingli return NULL; 2420186119Sqingli 2421186119Sqingli lle = in6_lltable_new(l3addr, flags); 2422186119Sqingli if (lle == NULL) { 2423186119Sqingli log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2424186119Sqingli return NULL; 2425186119Sqingli } 2426186119Sqingli lle->la_flags = flags & ~LLE_CREATE; 2427186119Sqingli if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2428186119Sqingli bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2429186119Sqingli lle->la_flags |= (LLE_VALID | LLE_STATIC); 2430186119Sqingli } 2431186119Sqingli 2432186119Sqingli lle->lle_tbl = llt; 2433186119Sqingli lle->lle_head = lleh; 2434186119Sqingli LIST_INSERT_HEAD(lleh, lle, lle_next); 2435186119Sqingli } else if (flags & LLE_DELETE) { 2436186392Sqingli if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 2437186392Sqingli LLE_WLOCK(lle); 2438186392Sqingli lle->la_flags = LLE_DELETED; 2439186392Sqingli LLE_WUNLOCK(lle); 2440186119Sqingli#ifdef DIAGNOSTICS 2441186392Sqingli log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2442186119Sqingli#endif 2443186392Sqingli } 2444186119Sqingli lle = (void *)-1; 2445186119Sqingli } 2446186119Sqingli if (LLE_IS_VALID(lle)) { 2447186119Sqingli if (flags & LLE_EXCLUSIVE) 2448186119Sqingli LLE_WLOCK(lle); 2449186119Sqingli else 2450186119Sqingli LLE_RLOCK(lle); 2451186119Sqingli } 2452186119Sqingli return (lle); 2453186119Sqingli} 2454186119Sqingli 2455186119Sqinglistatic int 2456186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2457186119Sqingli{ 2458186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2459186119Sqingli struct llentry *lle; 2460186119Sqingli /* XXX stack use */ 2461186119Sqingli struct { 2462186119Sqingli struct rt_msghdr rtm; 2463186119Sqingli struct sockaddr_in6 sin6; 2464186119Sqingli /* 2465186119Sqingli * ndp.c assumes that sdl is word aligned 2466186119Sqingli */ 2467186119Sqingli#ifdef __LP64__ 2468186119Sqingli uint32_t pad; 2469186119Sqingli#endif 2470186119Sqingli struct sockaddr_dl sdl; 2471186119Sqingli } ndpc; 2472186119Sqingli int i, error; 2473186119Sqingli 2474196864Sqingli if (ifp->if_flags & IFF_LOOPBACK) 2475196864Sqingli return 0; 2476196864Sqingli 2477196535Srwatson LLTABLE_LOCK_ASSERT(); 2478186119Sqingli 2479186119Sqingli error = 0; 2480186119Sqingli for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2481186119Sqingli LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2482186119Sqingli struct sockaddr_dl *sdl; 2483186119Sqingli 2484186119Sqingli /* skip deleted or invalid entries */ 2485186119Sqingli if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2486186119Sqingli continue; 2487186980Sbz /* Skip if jailed and not a valid IP of the prison. */ 2488188144Sjamie if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 2489186980Sbz continue; 2490186119Sqingli /* 2491186119Sqingli * produce a msg made of: 2492186119Sqingli * struct rt_msghdr; 2493186119Sqingli * struct sockaddr_in6 (IPv6) 2494186119Sqingli * struct sockaddr_dl; 2495186119Sqingli */ 2496186119Sqingli bzero(&ndpc, sizeof(ndpc)); 2497186119Sqingli ndpc.rtm.rtm_msglen = sizeof(ndpc); 2498187094Sqingli ndpc.rtm.rtm_version = RTM_VERSION; 2499187094Sqingli ndpc.rtm.rtm_type = RTM_GET; 2500187094Sqingli ndpc.rtm.rtm_flags = RTF_UP; 2501187094Sqingli ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2502186119Sqingli ndpc.sin6.sin6_family = AF_INET6; 2503186119Sqingli ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2504186119Sqingli bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2505186119Sqingli 2506186119Sqingli /* publish */ 2507186119Sqingli if (lle->la_flags & LLE_PUB) 2508186119Sqingli ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2509186119Sqingli 2510186119Sqingli sdl = &ndpc.sdl; 2511186119Sqingli sdl->sdl_family = AF_LINK; 2512186119Sqingli sdl->sdl_len = sizeof(*sdl); 2513186119Sqingli sdl->sdl_alen = ifp->if_addrlen; 2514186119Sqingli sdl->sdl_index = ifp->if_index; 2515186119Sqingli sdl->sdl_type = ifp->if_type; 2516186119Sqingli bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2517186119Sqingli ndpc.rtm.rtm_rmx.rmx_expire = 2518186119Sqingli lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2519186500Sqingli ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2520186119Sqingli if (lle->la_flags & LLE_STATIC) 2521186119Sqingli ndpc.rtm.rtm_flags |= RTF_STATIC; 2522186119Sqingli ndpc.rtm.rtm_index = ifp->if_index; 2523186119Sqingli error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2524186119Sqingli if (error) 2525186119Sqingli break; 2526186119Sqingli } 2527186119Sqingli } 2528186119Sqingli return error; 2529186119Sqingli} 2530186119Sqingli 2531121161Sumevoid * 2532171259Sdelphijin6_domifattach(struct ifnet *ifp) 2533121161Sume{ 2534121161Sume struct in6_ifextra *ext; 2535121161Sume 2536121161Sume ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2537121161Sume bzero(ext, sizeof(*ext)); 2538121161Sume 2539121161Sume ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2540121161Sume M_IFADDR, M_WAITOK); 2541121161Sume bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2542121161Sume 2543121161Sume ext->icmp6_ifstat = 2544121161Sume (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2545121161Sume M_IFADDR, M_WAITOK); 2546121161Sume bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2547121161Sume 2548121161Sume ext->nd_ifinfo = nd6_ifattach(ifp); 2549121161Sume ext->scope6_id = scope6_ifattach(ifp); 2550186119Sqingli ext->lltable = lltable_init(ifp, AF_INET6); 2551186119Sqingli if (ext->lltable != NULL) { 2552186119Sqingli ext->lltable->llt_new = in6_lltable_new; 2553186119Sqingli ext->lltable->llt_free = in6_lltable_free; 2554192476Sqingli ext->lltable->llt_prefix_free = in6_lltable_prefix_free; 2555186119Sqingli ext->lltable->llt_rtcheck = in6_lltable_rtcheck; 2556186119Sqingli ext->lltable->llt_lookup = in6_lltable_lookup; 2557186119Sqingli ext->lltable->llt_dump = in6_lltable_dump; 2558186119Sqingli } 2559191672Sbms 2560191672Sbms ext->mld_ifinfo = mld_domifattach(ifp); 2561191672Sbms 2562121161Sume return ext; 2563121161Sume} 2564121161Sume 2565121161Sumevoid 2566171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux) 2567121161Sume{ 2568121161Sume struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2569121161Sume 2570191672Sbms mld_domifdetach(ifp); 2571121161Sume scope6_ifdetach(ext->scope6_id); 2572121161Sume nd6_ifdetach(ext->nd_ifinfo); 2573186119Sqingli lltable_free(ext->lltable); 2574121161Sume free(ext->in6_ifstat, M_IFADDR); 2575121161Sume free(ext->icmp6_ifstat, M_IFADDR); 2576121161Sume free(ext, M_IFADDR); 2577121161Sume} 2578121161Sume 257953541Sshin/* 258095023Ssuz * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 258153541Sshin * v4 mapped addr or v4 compat addr 258253541Sshin */ 258353541Sshinvoid 258453541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 258553541Sshin{ 2586171259Sdelphij 258753541Sshin bzero(sin, sizeof(*sin)); 258853541Sshin sin->sin_len = sizeof(struct sockaddr_in); 258953541Sshin sin->sin_family = AF_INET; 259053541Sshin sin->sin_port = sin6->sin6_port; 2591120891Sume sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 259253541Sshin} 259353541Sshin 259453541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 259553541Sshinvoid 259653541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 259753541Sshin{ 259853541Sshin bzero(sin6, sizeof(*sin6)); 259953541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 260053541Sshin sin6->sin6_family = AF_INET6; 260153541Sshin sin6->sin6_port = sin->sin_port; 260253541Sshin sin6->sin6_addr.s6_addr32[0] = 0; 260353541Sshin sin6->sin6_addr.s6_addr32[1] = 0; 260453541Sshin sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 260553541Sshin sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 260653541Sshin} 260753541Sshin 260853541Sshin/* Convert sockaddr_in6 into sockaddr_in. */ 260953541Sshinvoid 261053541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam) 261153541Sshin{ 261253541Sshin struct sockaddr_in *sin_p; 261353541Sshin struct sockaddr_in6 sin6; 261453541Sshin 261553541Sshin /* 261653541Sshin * Save original sockaddr_in6 addr and convert it 261753541Sshin * to sockaddr_in. 261853541Sshin */ 261953541Sshin sin6 = *(struct sockaddr_in6 *)nam; 262053541Sshin sin_p = (struct sockaddr_in *)nam; 262153541Sshin in6_sin6_2_sin(sin_p, &sin6); 262253541Sshin} 262353541Sshin 262453541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 262553541Sshinvoid 262653541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 262753541Sshin{ 262853541Sshin struct sockaddr_in *sin_p; 262953541Sshin struct sockaddr_in6 *sin6_p; 263053541Sshin 2631184205Sdes sin6_p = malloc(sizeof *sin6_p, M_SONAME, 2632111119Simp M_WAITOK); 263353541Sshin sin_p = (struct sockaddr_in *)*nam; 263453541Sshin in6_sin_2_v4mapsin6(sin_p, sin6_p); 2635184205Sdes free(*nam, M_SONAME); 263653541Sshin *nam = (struct sockaddr *)sin6_p; 263753541Sshin} 2638