in6.c revision 201282
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 201282 2009-12-30 21:35: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 */ 1203201282Sqingli if (ia->ia_flags & IFA_RTSELF) { 1204201282Sqingli error = ifa_del_loopback_route((struct ifaddr *)ia, 1205201282Sqingli (struct sockaddr *)&ia->ia_addr); 1206201282Sqingli if (error == 0) 1207201282Sqingli ia->ia_flags &= ~IFA_RTSELF; 1208201282Sqingli } 1209195643Sqingli 121078064Sume /* stop DAD processing */ 121178064Sume nd6_dad_stop(ifa); 121262587Sitojun 1213186119Sqingli IF_AFDATA_LOCK(ifp); 1214186119Sqingli lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR), 1215186119Sqingli (struct sockaddr *)&ia->ia_addr); 1216186119Sqingli IF_AFDATA_UNLOCK(ifp); 1217192282Sqingli 121878064Sume /* 1219192282Sqingli * initialize for rtmsg generation 1220192282Sqingli */ 1221192282Sqingli bzero(&gateway, sizeof(gateway)); 1222192282Sqingli gateway.sdl_len = sizeof(gateway); 1223192282Sqingli gateway.sdl_family = AF_LINK; 1224192282Sqingli gateway.sdl_nlen = 0; 1225192282Sqingli gateway.sdl_alen = ifp->if_addrlen; 1226192282Sqingli /* */ 1227192282Sqingli bzero(&rt0, sizeof(rt0)); 1228192282Sqingli rt0.rt_gateway = (struct sockaddr *)&gateway; 1229192282Sqingli memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1230192282Sqingli memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1231192282Sqingli rt_mask(&rt0) = (struct sockaddr *)&mask; 1232192282Sqingli rt_key(&rt0) = (struct sockaddr *)&addr; 1233192282Sqingli rt0.rt_flags = RTF_HOST | RTF_STATIC; 1234192282Sqingli rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0); 1235192282Sqingli 1236192282Sqingli /* 1237170202Sjinmei * leave from multicast groups we have joined for the interface 1238170202Sjinmei */ 1239170202Sjinmei while ((imm = ia->ia6_memberships.lh_first) != NULL) { 1240170202Sjinmei LIST_REMOVE(imm, i6mm_chain); 1241170202Sjinmei in6_leavegroup(imm); 124262587Sitojun } 124362587Sitojun 1244192282Sqingli /* 1245192282Sqingli * remove the link-local all-nodes address 1246192282Sqingli */ 1247192282Sqingli bzero(&mltmask, sizeof(mltmask)); 1248192282Sqingli mltmask.sin6_len = sizeof(struct sockaddr_in6); 1249192282Sqingli mltmask.sin6_family = AF_INET6; 1250192282Sqingli mltmask.sin6_addr = in6mask32; 1251192282Sqingli 1252192282Sqingli bzero(&mltaddr, sizeof(mltaddr)); 1253192282Sqingli mltaddr.sin6_len = sizeof(struct sockaddr_in6); 1254192282Sqingli mltaddr.sin6_family = AF_INET6; 1255192282Sqingli mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1256192282Sqingli 1257192282Sqingli if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1258192282Sqingli 0) 1259192282Sqingli goto cleanup; 1260192282Sqingli 1261192282Sqingli rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1262192282Sqingli if (rt != NULL && rt->rt_gateway != NULL && 1263192282Sqingli (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1264192282Sqingli &ia->ia_addr.sin6_addr, 1265192282Sqingli sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1266192282Sqingli /* 1267192282Sqingli * if no more IPv6 address exists on this interface 1268192282Sqingli * then remove the multicast address route 1269192282Sqingli */ 1270192282Sqingli if (ifa0 == NULL) { 1271192282Sqingli memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1272192282Sqingli sizeof(mltaddr.sin6_addr)); 1273192282Sqingli RTFREE_LOCKED(rt); 1274192282Sqingli error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1275192282Sqingli (struct sockaddr *)&ia->ia_addr, 1276192282Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1277192282Sqingli (struct rtentry **)0); 1278192282Sqingli if (error) 1279192282Sqingli log(LOG_INFO, "in6_purgeaddr: link-local all-nodes" 1280192282Sqingli "multicast address deletion error\n"); 1281192282Sqingli } else { 1282192282Sqingli /* 1283192282Sqingli * replace the gateway of the route 1284192282Sqingli */ 1285192282Sqingli struct sockaddr_in6 sa; 1286192282Sqingli 1287192282Sqingli bzero(&sa, sizeof(sa)); 1288192282Sqingli sa.sin6_len = sizeof(struct sockaddr_in6); 1289192282Sqingli sa.sin6_family = AF_INET6; 1290192282Sqingli memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1291192282Sqingli sizeof(sa.sin6_addr)); 1292192282Sqingli in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1293192282Sqingli memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1294192282Sqingli RTFREE_LOCKED(rt); 1295192282Sqingli } 1296192282Sqingli } else { 1297192282Sqingli if (rt != NULL) 1298192282Sqingli RTFREE_LOCKED(rt); 1299192282Sqingli } 1300192282Sqingli 1301192282Sqingli /* 1302192282Sqingli * remove the node-local all-nodes address 1303192282Sqingli */ 1304192282Sqingli mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1305192282Sqingli if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 1306192282Sqingli 0) 1307192282Sqingli goto cleanup; 1308192282Sqingli 1309192282Sqingli rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL); 1310192282Sqingli if (rt != NULL && rt->rt_gateway != NULL && 1311192282Sqingli (memcmp(&satosin6(rt->rt_gateway)->sin6_addr, 1312192282Sqingli &ia->ia_addr.sin6_addr, 1313192282Sqingli sizeof(ia->ia_addr.sin6_addr)) == 0)) { 1314192282Sqingli /* 1315192282Sqingli * if no more IPv6 address exists on this interface 1316192282Sqingli * then remove the multicast address route 1317192282Sqingli */ 1318192282Sqingli if (ifa0 == NULL) { 1319192282Sqingli memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr, 1320192282Sqingli sizeof(mltaddr.sin6_addr)); 1321192282Sqingli 1322192282Sqingli RTFREE_LOCKED(rt); 1323192282Sqingli error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr, 1324192282Sqingli (struct sockaddr *)&ia->ia_addr, 1325192282Sqingli (struct sockaddr *)&mltmask, RTF_UP, 1326192282Sqingli (struct rtentry **)0); 1327192282Sqingli 1328192282Sqingli if (error) 1329192282Sqingli log(LOG_INFO, "in6_purgeaddr: node-local all-nodes" 1330192282Sqingli "multicast address deletion error\n"); 1331192282Sqingli } else { 1332192282Sqingli /* 1333192282Sqingli * replace the gateway of the route 1334192282Sqingli */ 1335192282Sqingli struct sockaddr_in6 sa; 1336192282Sqingli 1337192282Sqingli bzero(&sa, sizeof(sa)); 1338192282Sqingli sa.sin6_len = sizeof(struct sockaddr_in6); 1339192282Sqingli sa.sin6_family = AF_INET6; 1340192282Sqingli memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr, 1341192282Sqingli sizeof(sa.sin6_addr)); 1342192282Sqingli in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL); 1343192282Sqingli memcpy(rt->rt_gateway, &sa, sizeof(sa)); 1344192282Sqingli RTFREE_LOCKED(rt); 1345192282Sqingli } 1346192282Sqingli } else { 1347192282Sqingli if (rt != NULL) 1348192282Sqingli RTFREE_LOCKED(rt); 1349192282Sqingli } 1350192282Sqingli 1351192282Sqinglicleanup: 1352192282Sqingli 1353192282Sqingli plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1354192282Sqingli if ((ia->ia_flags & IFA_ROUTE) && plen == 128) { 1355192282Sqingli int error; 1356192282Sqingli struct sockaddr *dstaddr; 1357192282Sqingli 1358192282Sqingli /* 1359192282Sqingli * use the interface address if configuring an 1360192282Sqingli * interface address with a /128 prefix len 1361192282Sqingli */ 1362192282Sqingli if (ia->ia_dstaddr.sin6_family == AF_INET6) 1363192282Sqingli dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 1364192282Sqingli else 1365192282Sqingli dstaddr = (struct sockaddr *)&ia->ia_addr; 1366192282Sqingli 1367192282Sqingli error = rtrequest(RTM_DELETE, 1368192282Sqingli (struct sockaddr *)dstaddr, 1369192282Sqingli (struct sockaddr *)&ia->ia_addr, 1370192282Sqingli (struct sockaddr *)&ia->ia_prefixmask, 1371192282Sqingli ia->ia_flags | RTF_HOST, NULL); 1372192282Sqingli if (error != 0) 1373192282Sqingli return; 1374192282Sqingli ia->ia_flags &= ~IFA_ROUTE; 1375192282Sqingli } 1376194760Srwatson if (ifa0 != NULL) 1377194760Srwatson ifa_free(ifa0); 1378192282Sqingli 137978064Sume in6_unlink_ifa(ia, ifp); 138078064Sume} 138178064Sume 138278064Sumestatic void 1383171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 138478064Sume{ 138578064Sume int s = splnet(); 138678064Sume 1387191323Srwatson IF_ADDR_LOCK(ifp); 1388191340Srwatson TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 1389191323Srwatson IF_ADDR_UNLOCK(ifp); 1390194760Srwatson ifa_free(&ia->ia_ifa); /* if_addrhead */ 139162587Sitojun 1392195102Srwatson /* 1393195102Srwatson * Defer the release of what might be the last reference to the 1394195102Srwatson * in6_ifaddr so that it can't be freed before the remainder of the 1395195102Srwatson * cleanup. 1396195102Srwatson */ 1397194971Srwatson IN6_IFADDR_WLOCK(); 1398194907Srwatson TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link); 1399194971Srwatson IN6_IFADDR_WUNLOCK(); 140062587Sitojun 140162587Sitojun /* 1402151915Ssuz * Release the reference to the base prefix. There should be a 1403151915Ssuz * positive reference. 140462587Sitojun */ 1405194907Srwatson if (ia->ia6_ndpr == NULL) { 1406151915Ssuz nd6log((LOG_NOTICE, 1407151915Ssuz "in6_unlink_ifa: autoconf'ed address " 1408194907Srwatson "%p has no prefix\n", ia)); 1409151915Ssuz } else { 1410194907Srwatson ia->ia6_ndpr->ndpr_refcnt--; 1411194907Srwatson ia->ia6_ndpr = NULL; 1412151915Ssuz } 141362587Sitojun 1414151915Ssuz /* 1415151915Ssuz * Also, if the address being removed is autoconf'ed, call 1416151915Ssuz * pfxlist_onlink_check() since the release might affect the status of 1417171260Sdelphij * other (detached) addresses. 1418151915Ssuz */ 1419194907Srwatson if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) { 142078064Sume pfxlist_onlink_check(); 142162587Sitojun } 1422195102Srwatson ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */ 142378064Sume splx(s); 142462587Sitojun} 142562587Sitojun 142678064Sumevoid 1427171259Sdelphijin6_purgeif(struct ifnet *ifp) 142878064Sume{ 142978064Sume struct ifaddr *ifa, *nifa; 143078064Sume 1431191340Srwatson TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 143278064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 143378064Sume continue; 143478064Sume in6_purgeaddr(ifa); 143578064Sume } 143678064Sume 143778064Sume in6_ifdetach(ifp); 143878064Sume} 143978064Sume 144053541Sshin/* 144153541Sshin * SIOC[GAD]LIFADDR. 144262744Sgrog * SIOCGLIFADDR: get first address. (?) 144353541Sshin * SIOCGLIFADDR with IFLR_PREFIX: 144453541Sshin * get first address that matches the specified prefix. 144553541Sshin * SIOCALIFADDR: add the specified address. 144653541Sshin * SIOCALIFADDR with IFLR_PREFIX: 144753541Sshin * add the specified prefix, filling hostid part from 144853541Sshin * the first link-local address. prefixlen must be <= 64. 144953541Sshin * SIOCDLIFADDR: delete the specified address. 145053541Sshin * SIOCDLIFADDR with IFLR_PREFIX: 145153541Sshin * delete the first address that matches the specified prefix. 145253541Sshin * return values: 145353541Sshin * EINVAL on invalid parameters 145453541Sshin * EADDRNOTAVAIL on prefix match failed/specified address not found 145553541Sshin * other values may be returned from in6_ioctl() 145653541Sshin * 145753541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 145853541Sshin * this is to accomodate address naming scheme other than RFC2374, 145953541Sshin * in the future. 146053541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 146153541Sshin * address encoding scheme. (see figure on page 8) 146253541Sshin */ 146353541Sshinstatic int 1464171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 1465171259Sdelphij struct ifnet *ifp, struct thread *td) 146653541Sshin{ 146753541Sshin struct if_laddrreq *iflr = (struct if_laddrreq *)data; 146853541Sshin struct ifaddr *ifa; 146962587Sitojun struct sockaddr *sa; 147053541Sshin 147153541Sshin /* sanity checks */ 147253541Sshin if (!data || !ifp) { 147353541Sshin panic("invalid argument to in6_lifaddr_ioctl"); 1474120891Sume /* NOTREACHED */ 147553541Sshin } 147653541Sshin 147753541Sshin switch (cmd) { 147853541Sshin case SIOCGLIFADDR: 147953541Sshin /* address must be specified on GET with IFLR_PREFIX */ 148053541Sshin if ((iflr->flags & IFLR_PREFIX) == 0) 148153541Sshin break; 148295023Ssuz /* FALLTHROUGH */ 148353541Sshin case SIOCALIFADDR: 148453541Sshin case SIOCDLIFADDR: 148553541Sshin /* address must be specified on ADD and DELETE */ 148662587Sitojun sa = (struct sockaddr *)&iflr->addr; 148762587Sitojun if (sa->sa_family != AF_INET6) 148853541Sshin return EINVAL; 148962587Sitojun if (sa->sa_len != sizeof(struct sockaddr_in6)) 149053541Sshin return EINVAL; 149153541Sshin /* XXX need improvement */ 149262587Sitojun sa = (struct sockaddr *)&iflr->dstaddr; 149362587Sitojun if (sa->sa_family && sa->sa_family != AF_INET6) 149453541Sshin return EINVAL; 149562587Sitojun if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 149653541Sshin return EINVAL; 149753541Sshin break; 149895023Ssuz default: /* shouldn't happen */ 149962587Sitojun#if 0 150062587Sitojun panic("invalid cmd to in6_lifaddr_ioctl"); 150195023Ssuz /* NOTREACHED */ 150262587Sitojun#else 150353541Sshin return EOPNOTSUPP; 150462587Sitojun#endif 150553541Sshin } 150653541Sshin if (sizeof(struct in6_addr) * 8 < iflr->prefixlen) 150753541Sshin return EINVAL; 150853541Sshin 150953541Sshin switch (cmd) { 151053541Sshin case SIOCALIFADDR: 151153541Sshin { 151253541Sshin struct in6_aliasreq ifra; 151353541Sshin struct in6_addr *hostid = NULL; 151453541Sshin int prefixlen; 151553541Sshin 1516194760Srwatson ifa = NULL; 151753541Sshin if ((iflr->flags & IFLR_PREFIX) != 0) { 151853541Sshin struct sockaddr_in6 *sin6; 151953541Sshin 152053541Sshin /* 152153541Sshin * hostid is to fill in the hostid part of the 152253541Sshin * address. hostid points to the first link-local 152353541Sshin * address attached to the interface. 152453541Sshin */ 152562587Sitojun ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); 152653541Sshin if (!ifa) 152753541Sshin return EADDRNOTAVAIL; 152853541Sshin hostid = IFA_IN6(ifa); 152953541Sshin 1530171260Sdelphij /* prefixlen must be <= 64. */ 153153541Sshin if (64 < iflr->prefixlen) 153253541Sshin return EINVAL; 153353541Sshin prefixlen = iflr->prefixlen; 153453541Sshin 153553541Sshin /* hostid part must be zero. */ 153653541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 1537126552Sume if (sin6->sin6_addr.s6_addr32[2] != 0 || 1538126552Sume sin6->sin6_addr.s6_addr32[3] != 0) { 153953541Sshin return EINVAL; 154053541Sshin } 154153541Sshin } else 154253541Sshin prefixlen = iflr->prefixlen; 154353541Sshin 154453541Sshin /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 154553541Sshin bzero(&ifra, sizeof(ifra)); 1546120891Sume bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name)); 154753541Sshin 154862587Sitojun bcopy(&iflr->addr, &ifra.ifra_addr, 1549120891Sume ((struct sockaddr *)&iflr->addr)->sa_len); 155053541Sshin if (hostid) { 155153541Sshin /* fill in hostid part */ 155253541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1553120891Sume hostid->s6_addr32[2]; 155453541Sshin ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1555120891Sume hostid->s6_addr32[3]; 155653541Sshin } 155753541Sshin 1558120891Sume if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 155953541Sshin bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 1560120891Sume ((struct sockaddr *)&iflr->dstaddr)->sa_len); 156153541Sshin if (hostid) { 156253541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1563120891Sume hostid->s6_addr32[2]; 156453541Sshin ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1565120891Sume hostid->s6_addr32[3]; 156653541Sshin } 156753541Sshin } 1568194760Srwatson if (ifa != NULL) 1569194760Srwatson ifa_free(ifa); 157053541Sshin 157153541Sshin ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1572121168Sume in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 157353541Sshin 157453541Sshin ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 157583366Sjulian return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td); 157653541Sshin } 157753541Sshin case SIOCGLIFADDR: 157853541Sshin case SIOCDLIFADDR: 157953541Sshin { 158053541Sshin struct in6_ifaddr *ia; 158153541Sshin struct in6_addr mask, candidate, match; 158253541Sshin struct sockaddr_in6 *sin6; 158353541Sshin int cmp; 158453541Sshin 158553541Sshin bzero(&mask, sizeof(mask)); 158653541Sshin if (iflr->flags & IFLR_PREFIX) { 158753541Sshin /* lookup a prefix rather than address. */ 1588121168Sume in6_prefixlen2mask(&mask, iflr->prefixlen); 158953541Sshin 159053541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 159153541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 159253541Sshin match.s6_addr32[0] &= mask.s6_addr32[0]; 159353541Sshin match.s6_addr32[1] &= mask.s6_addr32[1]; 159453541Sshin match.s6_addr32[2] &= mask.s6_addr32[2]; 159553541Sshin match.s6_addr32[3] &= mask.s6_addr32[3]; 159653541Sshin 159753541Sshin /* if you set extra bits, that's wrong */ 159853541Sshin if (bcmp(&match, &sin6->sin6_addr, sizeof(match))) 159953541Sshin return EINVAL; 160053541Sshin 160153541Sshin cmp = 1; 160253541Sshin } else { 160353541Sshin if (cmd == SIOCGLIFADDR) { 160453541Sshin /* on getting an address, take the 1st match */ 160595023Ssuz cmp = 0; /* XXX */ 160653541Sshin } else { 160753541Sshin /* on deleting an address, do exact match */ 1608121168Sume in6_prefixlen2mask(&mask, 128); 160953541Sshin sin6 = (struct sockaddr_in6 *)&iflr->addr; 161053541Sshin bcopy(&sin6->sin6_addr, &match, sizeof(match)); 161153541Sshin 161253541Sshin cmp = 1; 161353541Sshin } 161453541Sshin } 161553541Sshin 1616191323Srwatson IF_ADDR_LOCK(ifp); 1617191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 161853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 161953541Sshin continue; 162053541Sshin if (!cmp) 162153541Sshin break; 162278064Sume 162378064Sume /* 162478064Sume * XXX: this is adhoc, but is necessary to allow 162578064Sume * a user to specify fe80::/64 (not /10) for a 162678064Sume * link-local address. 162778064Sume */ 1628148385Sume bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate)); 1629148385Sume in6_clearscope(&candidate); 163053541Sshin candidate.s6_addr32[0] &= mask.s6_addr32[0]; 163153541Sshin candidate.s6_addr32[1] &= mask.s6_addr32[1]; 163253541Sshin candidate.s6_addr32[2] &= mask.s6_addr32[2]; 163353541Sshin candidate.s6_addr32[3] &= mask.s6_addr32[3]; 163453541Sshin if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 163553541Sshin break; 163653541Sshin } 1637191323Srwatson IF_ADDR_UNLOCK(ifp); 163853541Sshin if (!ifa) 163953541Sshin return EADDRNOTAVAIL; 164053541Sshin ia = ifa2ia6(ifa); 164153541Sshin 164253541Sshin if (cmd == SIOCGLIFADDR) { 1643148385Sume int error; 164478064Sume 164553541Sshin /* fill in the if_laddrreq structure */ 164653541Sshin bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len); 1647148385Sume error = sa6_recoverscope( 1648148385Sume (struct sockaddr_in6 *)&iflr->addr); 1649148385Sume if (error != 0) 1650148385Sume return (error); 1651148385Sume 165253541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 165353541Sshin bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 1654120891Sume ia->ia_dstaddr.sin6_len); 1655148385Sume error = sa6_recoverscope( 1656148385Sume (struct sockaddr_in6 *)&iflr->dstaddr); 1657148385Sume if (error != 0) 1658148385Sume return (error); 165953541Sshin } else 166053541Sshin bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 166153541Sshin 166253541Sshin iflr->prefixlen = 1663120891Sume in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 166453541Sshin 166595023Ssuz iflr->flags = ia->ia6_flags; /* XXX */ 166653541Sshin 166753541Sshin return 0; 166853541Sshin } else { 166953541Sshin struct in6_aliasreq ifra; 167053541Sshin 167153541Sshin /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 167253541Sshin bzero(&ifra, sizeof(ifra)); 167353541Sshin bcopy(iflr->iflr_name, ifra.ifra_name, 1674120891Sume sizeof(ifra.ifra_name)); 167553541Sshin 167653541Sshin bcopy(&ia->ia_addr, &ifra.ifra_addr, 1677120891Sume ia->ia_addr.sin6_len); 167853541Sshin if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 167953541Sshin bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 1680120891Sume ia->ia_dstaddr.sin6_len); 168162587Sitojun } else { 168262587Sitojun bzero(&ifra.ifra_dstaddr, 168362587Sitojun sizeof(ifra.ifra_dstaddr)); 168453541Sshin } 168553541Sshin bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr, 1686120891Sume ia->ia_prefixmask.sin6_len); 168753541Sshin 168853541Sshin ifra.ifra_flags = ia->ia6_flags; 168953541Sshin return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra, 1690120891Sume ifp, td); 169153541Sshin } 169253541Sshin } 169353541Sshin } 169453541Sshin 169595023Ssuz return EOPNOTSUPP; /* just for safety */ 169653541Sshin} 169753541Sshin 169853541Sshin/* 169953541Sshin * Initialize an interface's intetnet6 address 170053541Sshin * and routing table entry. 170153541Sshin */ 170278064Sumestatic int 1703171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1704171259Sdelphij struct sockaddr_in6 *sin6, int newhost) 170553541Sshin{ 170678064Sume int error = 0, plen, ifacount = 0; 170753541Sshin int s = splimp(); 170878064Sume struct ifaddr *ifa; 170953541Sshin 171053541Sshin /* 171153541Sshin * Give the interface a chance to initialize 171253541Sshin * if this is its first address, 171353541Sshin * and to validate the address if necessary. 171453541Sshin */ 1715191323Srwatson IF_ADDR_LOCK(ifp); 1716191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 171778064Sume if (ifa->ifa_addr->sa_family != AF_INET6) 171878064Sume continue; 171978064Sume ifacount++; 172078064Sume } 1721191323Srwatson IF_ADDR_UNLOCK(ifp); 172278064Sume 172378064Sume ia->ia_addr = *sin6; 172478064Sume 1725146883Siedowse if (ifacount <= 1 && ifp->if_ioctl) { 1726146883Siedowse error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1727146883Siedowse if (error) { 1728146883Siedowse splx(s); 1729146883Siedowse return (error); 1730146883Siedowse } 173153541Sshin } 173278064Sume splx(s); 173353541Sshin 173478064Sume ia->ia_ifa.ifa_metric = ifp->if_metric; 173553541Sshin 173678064Sume /* we could do in(6)_socktrim here, but just omit it at this moment. */ 173778064Sume 173853541Sshin /* 173978064Sume * Special case: 1740124337Sume * If a new destination address is specified for a point-to-point 174178064Sume * interface, install a route to the destination as an interface 1742186119Sqingli * direct route. 1743124337Sume * XXX: the logic below rejects assigning multiple addresses on a p2p 1744159390Sgnn * interface that share the same destination. 174553541Sshin */ 174678064Sume plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1747196152Sqingli if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 && 1748196152Sqingli ia->ia_dstaddr.sin6_family == AF_INET6) { 1749159390Sgnn int rtflags = RTF_UP | RTF_HOST; 1750159390Sgnn 1751178888Sjulian error = rtrequest(RTM_ADD, 1752196152Sqingli (struct sockaddr *)&ia->ia_dstaddr, 1753159390Sgnn (struct sockaddr *)&ia->ia_addr, 1754159390Sgnn (struct sockaddr *)&ia->ia_prefixmask, 1755186708Sqingli ia->ia_flags | rtflags, NULL); 1756159390Sgnn if (error != 0) 1757120856Sume return (error); 175878064Sume ia->ia_flags |= IFA_ROUTE; 175953541Sshin } 176053541Sshin 1761195643Sqingli /* 1762195643Sqingli * add a loopback route to self 1763195643Sqingli */ 1764196871Sqingli if (!(ia->ia_flags & IFA_ROUTE) 1765196871Sqingli && (V_nd6_useloopback 1766196871Sqingli || (ifp->if_flags & IFF_LOOPBACK))) { 1767197227Sqingli error = ifa_add_loopback_route((struct ifaddr *)ia, 1768197227Sqingli (struct sockaddr *)&ia->ia_addr); 1769201282Sqingli if (error == 0) 1770201282Sqingli ia->ia_flags |= IFA_RTSELF; 1771195643Sqingli } 1772195643Sqingli 177395023Ssuz /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1774186119Sqingli if (newhost) { 1775186119Sqingli struct llentry *ln; 1776192282Sqingli struct rtentry rt; 1777192282Sqingli struct sockaddr_dl gateway; 1778192282Sqingli struct sockaddr_in6 mask, addr; 177953541Sshin 1780186119Sqingli IF_AFDATA_LOCK(ifp); 1781186119Sqingli ia->ia_ifa.ifa_rtrequest = NULL; 1782186119Sqingli 1783186119Sqingli /* XXX QL 1784186119Sqingli * we need to report rt_newaddrmsg 1785186119Sqingli */ 1786186119Sqingli ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE), 1787186119Sqingli (struct sockaddr *)&ia->ia_addr); 1788186119Sqingli IF_AFDATA_UNLOCK(ifp); 1789186158Skmacy if (ln != NULL) { 1790186119Sqingli ln->la_expire = 0; /* for IPv6 this means permanent */ 1791186119Sqingli ln->ln_state = ND6_LLINFO_REACHABLE; 1792192282Sqingli /* 1793192282Sqingli * initialize for rtmsg generation 1794192282Sqingli */ 1795192282Sqingli bzero(&gateway, sizeof(gateway)); 1796192282Sqingli gateway.sdl_len = sizeof(gateway); 1797192282Sqingli gateway.sdl_family = AF_LINK; 1798192282Sqingli gateway.sdl_nlen = 0; 1799192282Sqingli gateway.sdl_alen = 6; 1800192282Sqingli memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr)); 1801192282Sqingli /* */ 1802186119Sqingli LLE_WUNLOCK(ln); 1803186119Sqingli } 1804192282Sqingli 1805192282Sqingli bzero(&rt, sizeof(rt)); 1806192282Sqingli rt.rt_gateway = (struct sockaddr *)&gateway; 1807192282Sqingli memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 1808192282Sqingli memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 1809192282Sqingli rt_mask(&rt) = (struct sockaddr *)&mask; 1810192282Sqingli rt_key(&rt) = (struct sockaddr *)&addr; 1811192282Sqingli rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC; 1812192282Sqingli rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt); 1813186119Sqingli } 1814186119Sqingli 1815120856Sume return (error); 181653541Sshin} 181753541Sshin 181853541Sshin/* 181953541Sshin * Find an IPv6 interface link-local address specific to an interface. 1820194760Srwatson * ifaddr is returned referenced. 182153541Sshin */ 182253541Sshinstruct in6_ifaddr * 1823171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 182453541Sshin{ 182578064Sume struct ifaddr *ifa; 182653541Sshin 1827191323Srwatson IF_ADDR_LOCK(ifp); 1828191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 182953541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 183053541Sshin continue; 183162587Sitojun if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 183262587Sitojun if ((((struct in6_ifaddr *)ifa)->ia6_flags & 183362587Sitojun ignoreflags) != 0) 183462587Sitojun continue; 1835194760Srwatson ifa_ref(ifa); 183653541Sshin break; 183762587Sitojun } 183853541Sshin } 1839191323Srwatson IF_ADDR_UNLOCK(ifp); 184053541Sshin 1841120856Sume return ((struct in6_ifaddr *)ifa); 184253541Sshin} 184353541Sshin 184453541Sshin 184553541Sshin/* 184653541Sshin * find the internet address corresponding to a given interface and address. 1847194760Srwatson * ifaddr is returned referenced. 184853541Sshin */ 184953541Sshinstruct in6_ifaddr * 1850171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr) 185153541Sshin{ 185278064Sume struct ifaddr *ifa; 185353541Sshin 1854191337Srwatson IF_ADDR_LOCK(ifp); 1855191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 185653541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 185753541Sshin continue; 1858194760Srwatson if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 1859194760Srwatson ifa_ref(ifa); 186053541Sshin break; 1861194760Srwatson } 186253541Sshin } 1863191337Srwatson IF_ADDR_UNLOCK(ifp); 186453541Sshin 1865120856Sume return ((struct in6_ifaddr *)ifa); 186653541Sshin} 186753541Sshin 186853541Sshin/* 1869165118Sbz * Convert IP6 address to printable (loggable) representation. Caller 1870165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 187153541Sshin */ 187253541Sshinstatic char digits[] = "0123456789abcdef"; 187353541Sshinchar * 1874165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr) 187553541Sshin{ 187678064Sume int i; 187778064Sume char *cp; 1878126552Sume const u_int16_t *a = (const u_int16_t *)addr; 1879126552Sume const u_int8_t *d; 1880165287Sbz int dcolon = 0, zero = 0; 188153541Sshin 1882165118Sbz cp = ip6buf; 188353541Sshin 188453541Sshin for (i = 0; i < 8; i++) { 188553541Sshin if (dcolon == 1) { 188653541Sshin if (*a == 0) { 188753541Sshin if (i == 7) 188853541Sshin *cp++ = ':'; 188953541Sshin a++; 189053541Sshin continue; 189153541Sshin } else 189253541Sshin dcolon = 2; 189353541Sshin } 189453541Sshin if (*a == 0) { 189553541Sshin if (dcolon == 0 && *(a + 1) == 0) { 189653541Sshin if (i == 0) 189753541Sshin *cp++ = ':'; 189853541Sshin *cp++ = ':'; 189953541Sshin dcolon = 1; 190053541Sshin } else { 190153541Sshin *cp++ = '0'; 190253541Sshin *cp++ = ':'; 190353541Sshin } 190453541Sshin a++; 190553541Sshin continue; 190653541Sshin } 190791346Salfred d = (const u_char *)a; 1908165287Sbz /* Try to eliminate leading zeros in printout like in :0001. */ 1909165287Sbz zero = 1; 1910165287Sbz *cp = digits[*d >> 4]; 1911165287Sbz if (*cp != '0') { 1912165287Sbz zero = 0; 1913165287Sbz cp++; 1914165287Sbz } 1915165287Sbz *cp = digits[*d++ & 0xf]; 1916165287Sbz if (zero == 0 || (*cp != '0')) { 1917165287Sbz zero = 0; 1918165287Sbz cp++; 1919165287Sbz } 1920165287Sbz *cp = digits[*d >> 4]; 1921165287Sbz if (zero == 0 || (*cp != '0')) { 1922165287Sbz zero = 0; 1923165287Sbz cp++; 1924165287Sbz } 192553541Sshin *cp++ = digits[*d & 0xf]; 192653541Sshin *cp++ = ':'; 192753541Sshin a++; 192853541Sshin } 1929165118Sbz *--cp = '\0'; 1930165118Sbz return (ip6buf); 193153541Sshin} 193253541Sshin 193353541Sshinint 1934171259Sdelphijin6_localaddr(struct in6_addr *in6) 193553541Sshin{ 193653541Sshin struct in6_ifaddr *ia; 193753541Sshin 193853541Sshin if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 193953541Sshin return 1; 194053541Sshin 1941194971Srwatson IN6_IFADDR_RLOCK(); 1942194907Srwatson TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 194353541Sshin if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1944120891Sume &ia->ia_prefixmask.sin6_addr)) { 1945194971Srwatson IN6_IFADDR_RUNLOCK(); 194653541Sshin return 1; 1947120891Sume } 1948120891Sume } 1949194971Srwatson IN6_IFADDR_RUNLOCK(); 195053541Sshin 195153541Sshin return (0); 195253541Sshin} 195353541Sshin 195478064Sumeint 1955171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6) 195678064Sume{ 195778064Sume struct in6_ifaddr *ia; 195878064Sume 1959194971Srwatson IN6_IFADDR_RLOCK(); 1960194907Srwatson TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 196178064Sume if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 196278064Sume &sa6->sin6_addr) && 1963194971Srwatson (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { 1964194971Srwatson IN6_IFADDR_RUNLOCK(); 1965120856Sume return (1); /* true */ 1966194971Srwatson } 196778064Sume 196878064Sume /* XXX: do we still have to go thru the rest of the list? */ 196978064Sume } 1970194971Srwatson IN6_IFADDR_RUNLOCK(); 197178064Sume 1972120856Sume return (0); /* false */ 197378064Sume} 197478064Sume 197553541Sshin/* 197653541Sshin * return length of part which dst and src are equal 197753541Sshin * hard coding... 197853541Sshin */ 197953541Sshinint 1980171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst) 198153541Sshin{ 198253541Sshin int match = 0; 198353541Sshin u_char *s = (u_char *)src, *d = (u_char *)dst; 198453541Sshin u_char *lim = s + 16, r; 198553541Sshin 198653541Sshin while (s < lim) 198753541Sshin if ((r = (*d++ ^ *s++)) != 0) { 198853541Sshin while (r < 128) { 198953541Sshin match++; 199053541Sshin r <<= 1; 199153541Sshin } 199253541Sshin break; 199353541Sshin } else 199453541Sshin match += 8; 199553541Sshin return match; 199653541Sshin} 199753541Sshin 199862587Sitojun/* XXX: to be scope conscious */ 199953541Sshinint 2000171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 200153541Sshin{ 200253541Sshin int bytelen, bitlen; 200353541Sshin 200453541Sshin /* sanity check */ 200553541Sshin if (0 > len || len > 128) { 200653541Sshin log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 200753541Sshin len); 2008120856Sume return (0); 200953541Sshin } 201053541Sshin 201153541Sshin bytelen = len / 8; 201253541Sshin bitlen = len % 8; 201353541Sshin 201453541Sshin if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2015120856Sume return (0); 2016126184Scperciva if (bitlen != 0 && 2017126184Scperciva p1->s6_addr[bytelen] >> (8 - bitlen) != 201853541Sshin p2->s6_addr[bytelen] >> (8 - bitlen)) 2019120856Sume return (0); 202053541Sshin 2021120856Sume return (1); 202253541Sshin} 202353541Sshin 202453541Sshinvoid 2025171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len) 202653541Sshin{ 202753541Sshin u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 202853541Sshin int bytelen, bitlen, i; 202953541Sshin 203053541Sshin /* sanity check */ 203153541Sshin if (0 > len || len > 128) { 203253541Sshin log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 203353541Sshin len); 203453541Sshin return; 203553541Sshin } 203653541Sshin 203753541Sshin bzero(maskp, sizeof(*maskp)); 203853541Sshin bytelen = len / 8; 203953541Sshin bitlen = len % 8; 204053541Sshin for (i = 0; i < bytelen; i++) 204153541Sshin maskp->s6_addr[i] = 0xff; 204253541Sshin if (bitlen) 204353541Sshin maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 204453541Sshin} 204553541Sshin 204653541Sshin/* 204753541Sshin * return the best address out of the same scope. if no address was 204853541Sshin * found, return the first valid address from designated IF. 204953541Sshin */ 205053541Sshinstruct in6_ifaddr * 2051171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 205253541Sshin{ 205353541Sshin int dst_scope = in6_addrscope(dst), blen = -1, tlen; 205453541Sshin struct ifaddr *ifa; 205553541Sshin struct in6_ifaddr *besta = 0; 205695023Ssuz struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 205753541Sshin 205853541Sshin dep[0] = dep[1] = NULL; 205953541Sshin 206053541Sshin /* 206153541Sshin * We first look for addresses in the same scope. 206253541Sshin * If there is one, return it. 206353541Sshin * If two or more, return one which matches the dst longest. 206453541Sshin * If none, return one of global addresses assigned other ifs. 206553541Sshin */ 2066191323Srwatson IF_ADDR_LOCK(ifp); 2067191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 206853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 206953541Sshin continue; 207053541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 207153541Sshin continue; /* XXX: is there any case to allow anycast? */ 207253541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 207353541Sshin continue; /* don't use this interface */ 207453541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 207553541Sshin continue; 207653541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2077181803Sbz if (V_ip6_use_deprecated) 207853541Sshin dep[0] = (struct in6_ifaddr *)ifa; 207953541Sshin continue; 208053541Sshin } 208153541Sshin 208253541Sshin if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 208353541Sshin /* 208453541Sshin * call in6_matchlen() as few as possible 208553541Sshin */ 208653541Sshin if (besta) { 208753541Sshin if (blen == -1) 208853541Sshin blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 208953541Sshin tlen = in6_matchlen(IFA_IN6(ifa), dst); 209053541Sshin if (tlen > blen) { 209153541Sshin blen = tlen; 209253541Sshin besta = (struct in6_ifaddr *)ifa; 209353541Sshin } 209453541Sshin } else 209553541Sshin besta = (struct in6_ifaddr *)ifa; 209653541Sshin } 209753541Sshin } 2098191323Srwatson if (besta) { 2099194760Srwatson ifa_ref(&besta->ia_ifa); 2100191323Srwatson IF_ADDR_UNLOCK(ifp); 2101120856Sume return (besta); 2102191323Srwatson } 2103194971Srwatson IF_ADDR_UNLOCK(ifp); 210453541Sshin 2105194971Srwatson IN6_IFADDR_RLOCK(); 2106191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 210753541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 210853541Sshin continue; 210953541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 211053541Sshin continue; /* XXX: is there any case to allow anycast? */ 211153541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 211253541Sshin continue; /* don't use this interface */ 211353541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 211453541Sshin continue; 211553541Sshin if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2116181803Sbz if (V_ip6_use_deprecated) 211753541Sshin dep[1] = (struct in6_ifaddr *)ifa; 211853541Sshin continue; 211953541Sshin } 212053541Sshin 2121194760Srwatson if (ifa != NULL) 2122194760Srwatson ifa_ref(ifa); 2123194971Srwatson IN6_IFADDR_RUNLOCK(); 212453541Sshin return (struct in6_ifaddr *)ifa; 212553541Sshin } 2126194971Srwatson IN6_IFADDR_RUNLOCK(); 212753541Sshin 212853541Sshin /* use the last-resort values, that are, deprecated addresses */ 212953541Sshin if (dep[0]) 213053541Sshin return dep[0]; 213153541Sshin if (dep[1]) 213253541Sshin return dep[1]; 213353541Sshin 213453541Sshin return NULL; 213553541Sshin} 213653541Sshin 213753541Sshin/* 213853541Sshin * perform DAD when interface becomes IFF_UP. 213953541Sshin */ 214053541Sshinvoid 2141171259Sdelphijin6_if_up(struct ifnet *ifp) 214253541Sshin{ 214353541Sshin struct ifaddr *ifa; 214453541Sshin struct in6_ifaddr *ia; 214553541Sshin 2146191323Srwatson IF_ADDR_LOCK(ifp); 2147191340Srwatson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 214853541Sshin if (ifa->ifa_addr->sa_family != AF_INET6) 214953541Sshin continue; 215053541Sshin ia = (struct in6_ifaddr *)ifa; 2151151539Ssuz if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2152151539Ssuz /* 2153151539Ssuz * The TENTATIVE flag was likely set by hand 2154151539Ssuz * beforehand, implicitly indicating the need for DAD. 2155151539Ssuz * We may be able to skip the random delay in this 2156151539Ssuz * case, but we impose delays just in case. 2157151539Ssuz */ 2158151539Ssuz nd6_dad_start(ifa, 2159151539Ssuz arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 2160151539Ssuz } 216153541Sshin } 2162191323Srwatson IF_ADDR_UNLOCK(ifp); 2163151539Ssuz 2164151539Ssuz /* 2165151539Ssuz * special cases, like 6to4, are handled in in6_ifattach 2166151539Ssuz */ 2167151539Ssuz in6_ifattach(ifp, NULL); 216853541Sshin} 216953541Sshin 217078064Sumeint 2171171259Sdelphijin6if_do_dad(struct ifnet *ifp) 217278064Sume{ 217378064Sume if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2174120856Sume return (0); 217578064Sume 2176197138Shrs if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 2177197138Shrs return (0); 2178197138Shrs 217978064Sume switch (ifp->if_type) { 218078064Sume#ifdef IFT_DUMMY 218178064Sume case IFT_DUMMY: 218278064Sume#endif 218378064Sume case IFT_FAITH: 218478064Sume /* 218578064Sume * These interfaces do not have the IFF_LOOPBACK flag, 218678064Sume * but loop packets back. We do not have to do DAD on such 218778064Sume * interfaces. We should even omit it, because loop-backed 218878064Sume * NS would confuse the DAD procedure. 218978064Sume */ 2190120856Sume return (0); 219178064Sume default: 219278064Sume /* 219378064Sume * Our DAD routine requires the interface up and running. 219478064Sume * However, some interfaces can be up before the RUNNING 219578064Sume * status. Additionaly, users may try to assign addresses 219678064Sume * before the interface becomes up (or running). 219778064Sume * We simply skip DAD in such a case as a work around. 219878064Sume * XXX: we should rather mark "tentative" on such addresses, 219978064Sume * and do DAD after the interface becomes ready. 220078064Sume */ 2201148887Srwatson if (!((ifp->if_flags & IFF_UP) && 2202148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 2203120856Sume return (0); 220478064Sume 2205120856Sume return (1); 220678064Sume } 220778064Sume} 220878064Sume 220953541Sshin/* 221053541Sshin * Calculate max IPv6 MTU through all the interfaces and store it 221153541Sshin * to in6_maxmtu. 221253541Sshin */ 221353541Sshinvoid 2214171259Sdelphijin6_setmaxmtu(void) 221553541Sshin{ 221653541Sshin unsigned long maxmtu = 0; 221753541Sshin struct ifnet *ifp; 221853541Sshin 2219196481Srwatson IFNET_RLOCK_NOSLEEP(); 2220181887Sjulian for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 2221181888Sjulian ifp = TAILQ_NEXT(ifp, if_list)) { 2222121283Sume /* this function can be called during ifnet initialization */ 2223121283Sume if (!ifp->if_afdata[AF_INET6]) 2224121283Sume continue; 222553541Sshin if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2226121283Sume IN6_LINKMTU(ifp) > maxmtu) 2227121283Sume maxmtu = IN6_LINKMTU(ifp); 222853541Sshin } 2229196481Srwatson IFNET_RUNLOCK_NOSLEEP(); 2230120891Sume if (maxmtu) /* update only when maxmtu is positive */ 2231181803Sbz V_in6_maxmtu = maxmtu; 223253541Sshin} 223353541Sshin 2234151539Ssuz/* 2235151539Ssuz * Provide the length of interface identifiers to be used for the link attached 2236151539Ssuz * to the given interface. The length should be defined in "IPv6 over 2237151539Ssuz * xxx-link" document. Note that address architecture might also define 2238151539Ssuz * the length for a particular set of address prefixes, regardless of the 2239151539Ssuz * link type. As clarified in rfc2462bis, those two definitions should be 2240151539Ssuz * consistent, and those really are as of August 2004. 2241151539Ssuz */ 2242151539Ssuzint 2243171259Sdelphijin6_if2idlen(struct ifnet *ifp) 2244151539Ssuz{ 2245151539Ssuz switch (ifp->if_type) { 2246151539Ssuz case IFT_ETHER: /* RFC2464 */ 2247151539Ssuz#ifdef IFT_PROPVIRTUAL 2248151539Ssuz case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2249151539Ssuz#endif 2250151539Ssuz#ifdef IFT_L2VLAN 2251151539Ssuz case IFT_L2VLAN: /* ditto */ 2252151539Ssuz#endif 2253151539Ssuz#ifdef IFT_IEEE80211 2254151539Ssuz case IFT_IEEE80211: /* ditto */ 2255151539Ssuz#endif 2256151539Ssuz#ifdef IFT_MIP 2257151539Ssuz case IFT_MIP: /* ditto */ 2258151539Ssuz#endif 2259151539Ssuz return (64); 2260151539Ssuz case IFT_FDDI: /* RFC2467 */ 2261151539Ssuz return (64); 2262151539Ssuz case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2263151539Ssuz return (64); 2264151539Ssuz case IFT_PPP: /* RFC2472 */ 2265151539Ssuz return (64); 2266151539Ssuz case IFT_ARCNET: /* RFC2497 */ 2267151539Ssuz return (64); 2268151539Ssuz case IFT_FRELAY: /* RFC2590 */ 2269151539Ssuz return (64); 2270151539Ssuz case IFT_IEEE1394: /* RFC3146 */ 2271151539Ssuz return (64); 2272151539Ssuz case IFT_GIF: 2273151539Ssuz return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2274151539Ssuz case IFT_LOOP: 2275151539Ssuz return (64); /* XXX: is this really correct? */ 2276151539Ssuz default: 2277151539Ssuz /* 2278151539Ssuz * Unknown link type: 2279151539Ssuz * It might be controversial to use the today's common constant 2280151539Ssuz * of 64 for these cases unconditionally. For full compliance, 2281151539Ssuz * we should return an error in this case. On the other hand, 2282151539Ssuz * if we simply miss the standard for the link type or a new 2283151539Ssuz * standard is defined for a new link type, the IFID length 2284151539Ssuz * is very likely to be the common constant. As a compromise, 2285151539Ssuz * we always use the constant, but make an explicit notice 2286151539Ssuz * indicating the "unknown" case. 2287151539Ssuz */ 2288151539Ssuz printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2289151539Ssuz return (64); 2290151539Ssuz } 2291151539Ssuz} 2292151539Ssuz 2293186119Sqingli#include <sys/sysctl.h> 2294186119Sqingli 2295186119Sqinglistruct in6_llentry { 2296186119Sqingli struct llentry base; 2297186119Sqingli struct sockaddr_in6 l3_addr6; 2298186119Sqingli}; 2299186119Sqingli 2300186119Sqinglistatic struct llentry * 2301186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags) 2302186119Sqingli{ 2303186119Sqingli struct in6_llentry *lle; 2304186119Sqingli 2305186119Sqingli lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, 2306186119Sqingli M_DONTWAIT | M_ZERO); 2307186119Sqingli if (lle == NULL) /* NB: caller generates msg */ 2308186119Sqingli return NULL; 2309186119Sqingli 2310186119Sqingli callout_init(&lle->base.ln_timer_ch, CALLOUT_MPSAFE); 2311186119Sqingli lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr; 2312186119Sqingli lle->base.lle_refcnt = 1; 2313186119Sqingli LLE_LOCK_INIT(&lle->base); 2314186119Sqingli return &lle->base; 2315186119Sqingli} 2316186119Sqingli 2317186119Sqingli/* 2318186119Sqingli * Deletes an address from the address table. 2319186119Sqingli * This function is called by the timer functions 2320186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and 2321186119Sqingli * the caller does the locking. 2322186119Sqingli */ 2323186119Sqinglistatic void 2324186119Sqingliin6_lltable_free(struct lltable *llt, struct llentry *lle) 2325186119Sqingli{ 2326186150Skmacy LLE_WUNLOCK(lle); 2327186150Skmacy LLE_LOCK_DESTROY(lle); 2328186119Sqingli free(lle, M_LLTABLE); 2329186119Sqingli} 2330186119Sqingli 2331192476Sqinglistatic void 2332192476Sqingliin6_lltable_prefix_free(struct lltable *llt, 2333192476Sqingli const struct sockaddr *prefix, 2334192476Sqingli const struct sockaddr *mask) 2335192476Sqingli{ 2336192476Sqingli const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix; 2337192476Sqingli const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask; 2338192476Sqingli struct llentry *lle, *next; 2339192476Sqingli register int i; 2340192476Sqingli 2341192476Sqingli for (i=0; i < LLTBL_HASHTBL_SIZE; i++) { 2342192476Sqingli LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 2343192476Sqingli if (IN6_ARE_MASKED_ADDR_EQUAL( 2344192476Sqingli &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr, 2345192476Sqingli &pfx->sin6_addr, 2346192476Sqingli &msk->sin6_addr)) { 2347192476Sqingli callout_drain(&lle->la_timer); 2348192476Sqingli LLE_WLOCK(lle); 2349192476Sqingli llentry_free(lle); 2350192476Sqingli } 2351192476Sqingli } 2352192476Sqingli } 2353192476Sqingli} 2354192476Sqingli 2355186119Sqinglistatic int 2356201282Sqingliin6_lltable_rtcheck(struct ifnet *ifp, 2357201282Sqingli u_int flags, 2358201282Sqingli const struct sockaddr *l3addr) 2359186119Sqingli{ 2360186119Sqingli struct rtentry *rt; 2361186119Sqingli char ip6buf[INET6_ADDRSTRLEN]; 2362186119Sqingli 2363186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2364186119Sqingli ("sin_family %d", l3addr->sa_family)); 2365186119Sqingli 2366186119Sqingli /* XXX rtalloc1 should take a const param */ 2367186119Sqingli rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 2368186119Sqingli if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2369186119Sqingli struct ifaddr *ifa; 2370186119Sqingli /* 2371186119Sqingli * Create an ND6 cache for an IPv6 neighbor 2372186119Sqingli * that is not covered by our own prefix. 2373186119Sqingli */ 2374186119Sqingli /* XXX ifaof_ifpforaddr should take a const param */ 2375186119Sqingli ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp); 2376186119Sqingli if (ifa != NULL) { 2377194760Srwatson ifa_free(ifa); 2378186119Sqingli if (rt != NULL) 2379187946Sbz RTFREE_LOCKED(rt); 2380186119Sqingli return 0; 2381186119Sqingli } 2382186119Sqingli log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2383186119Sqingli ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2384186119Sqingli if (rt != NULL) 2385187946Sbz RTFREE_LOCKED(rt); 2386186119Sqingli return EINVAL; 2387186119Sqingli } 2388187946Sbz RTFREE_LOCKED(rt); 2389186119Sqingli return 0; 2390186119Sqingli} 2391186119Sqingli 2392186119Sqinglistatic struct llentry * 2393186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags, 2394186119Sqingli const struct sockaddr *l3addr) 2395186119Sqingli{ 2396186119Sqingli const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2397186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2398186119Sqingli struct llentry *lle; 2399186119Sqingli struct llentries *lleh; 2400186119Sqingli u_int hashkey; 2401186119Sqingli 2402186119Sqingli IF_AFDATA_LOCK_ASSERT(ifp); 2403186119Sqingli KASSERT(l3addr->sa_family == AF_INET6, 2404186119Sqingli ("sin_family %d", l3addr->sa_family)); 2405186119Sqingli 2406186119Sqingli hashkey = sin6->sin6_addr.s6_addr32[3]; 2407186119Sqingli lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 2408186119Sqingli LIST_FOREACH(lle, lleh, lle_next) { 2409186708Sqingli struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle); 2410186119Sqingli if (lle->la_flags & LLE_DELETED) 2411186119Sqingli continue; 2412186708Sqingli if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr, 2413186708Sqingli sizeof(struct in6_addr)) == 0) 2414186119Sqingli break; 2415186119Sqingli } 2416186119Sqingli 2417186119Sqingli if (lle == NULL) { 2418186119Sqingli if (!(flags & LLE_CREATE)) 2419186119Sqingli return (NULL); 2420186119Sqingli /* 2421186119Sqingli * A route that covers the given address must have 2422186119Sqingli * been installed 1st because we are doing a resolution, 2423186119Sqingli * verify this. 2424186119Sqingli */ 2425186119Sqingli if (!(flags & LLE_IFADDR) && 2426201282Sqingli in6_lltable_rtcheck(ifp, flags, l3addr) != 0) 2427186119Sqingli return NULL; 2428186119Sqingli 2429186119Sqingli lle = in6_lltable_new(l3addr, flags); 2430186119Sqingli if (lle == NULL) { 2431186119Sqingli log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2432186119Sqingli return NULL; 2433186119Sqingli } 2434186119Sqingli lle->la_flags = flags & ~LLE_CREATE; 2435186119Sqingli if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 2436186119Sqingli bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 2437186119Sqingli lle->la_flags |= (LLE_VALID | LLE_STATIC); 2438186119Sqingli } 2439186119Sqingli 2440186119Sqingli lle->lle_tbl = llt; 2441186119Sqingli lle->lle_head = lleh; 2442186119Sqingli LIST_INSERT_HEAD(lleh, lle, lle_next); 2443186119Sqingli } else if (flags & LLE_DELETE) { 2444186392Sqingli if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 2445186392Sqingli LLE_WLOCK(lle); 2446186392Sqingli lle->la_flags = LLE_DELETED; 2447186392Sqingli LLE_WUNLOCK(lle); 2448198418Sqingli#ifdef DIAGNOSTIC 2449186392Sqingli log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2450186119Sqingli#endif 2451186392Sqingli } 2452186119Sqingli lle = (void *)-1; 2453186119Sqingli } 2454186119Sqingli if (LLE_IS_VALID(lle)) { 2455186119Sqingli if (flags & LLE_EXCLUSIVE) 2456186119Sqingli LLE_WLOCK(lle); 2457186119Sqingli else 2458186119Sqingli LLE_RLOCK(lle); 2459186119Sqingli } 2460186119Sqingli return (lle); 2461186119Sqingli} 2462186119Sqingli 2463186119Sqinglistatic int 2464186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 2465186119Sqingli{ 2466186119Sqingli struct ifnet *ifp = llt->llt_ifp; 2467186119Sqingli struct llentry *lle; 2468186119Sqingli /* XXX stack use */ 2469186119Sqingli struct { 2470186119Sqingli struct rt_msghdr rtm; 2471186119Sqingli struct sockaddr_in6 sin6; 2472186119Sqingli /* 2473186119Sqingli * ndp.c assumes that sdl is word aligned 2474186119Sqingli */ 2475186119Sqingli#ifdef __LP64__ 2476186119Sqingli uint32_t pad; 2477186119Sqingli#endif 2478186119Sqingli struct sockaddr_dl sdl; 2479186119Sqingli } ndpc; 2480186119Sqingli int i, error; 2481186119Sqingli 2482196864Sqingli if (ifp->if_flags & IFF_LOOPBACK) 2483196864Sqingli return 0; 2484196864Sqingli 2485196535Srwatson LLTABLE_LOCK_ASSERT(); 2486186119Sqingli 2487186119Sqingli error = 0; 2488186119Sqingli for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 2489186119Sqingli LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 2490186119Sqingli struct sockaddr_dl *sdl; 2491186119Sqingli 2492186119Sqingli /* skip deleted or invalid entries */ 2493186119Sqingli if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 2494186119Sqingli continue; 2495186980Sbz /* Skip if jailed and not a valid IP of the prison. */ 2496188144Sjamie if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 2497186980Sbz continue; 2498186119Sqingli /* 2499186119Sqingli * produce a msg made of: 2500186119Sqingli * struct rt_msghdr; 2501186119Sqingli * struct sockaddr_in6 (IPv6) 2502186119Sqingli * struct sockaddr_dl; 2503186119Sqingli */ 2504186119Sqingli bzero(&ndpc, sizeof(ndpc)); 2505186119Sqingli ndpc.rtm.rtm_msglen = sizeof(ndpc); 2506187094Sqingli ndpc.rtm.rtm_version = RTM_VERSION; 2507187094Sqingli ndpc.rtm.rtm_type = RTM_GET; 2508187094Sqingli ndpc.rtm.rtm_flags = RTF_UP; 2509187094Sqingli ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2510186119Sqingli ndpc.sin6.sin6_family = AF_INET6; 2511186119Sqingli ndpc.sin6.sin6_len = sizeof(ndpc.sin6); 2512186119Sqingli bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle)); 2513186119Sqingli 2514186119Sqingli /* publish */ 2515186119Sqingli if (lle->la_flags & LLE_PUB) 2516186119Sqingli ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2517186119Sqingli 2518186119Sqingli sdl = &ndpc.sdl; 2519186119Sqingli sdl->sdl_family = AF_LINK; 2520186119Sqingli sdl->sdl_len = sizeof(*sdl); 2521186119Sqingli sdl->sdl_alen = ifp->if_addrlen; 2522186119Sqingli sdl->sdl_index = ifp->if_index; 2523186119Sqingli sdl->sdl_type = ifp->if_type; 2524186119Sqingli bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2525186119Sqingli ndpc.rtm.rtm_rmx.rmx_expire = 2526186119Sqingli lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 2527186500Sqingli ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2528186119Sqingli if (lle->la_flags & LLE_STATIC) 2529186119Sqingli ndpc.rtm.rtm_flags |= RTF_STATIC; 2530186119Sqingli ndpc.rtm.rtm_index = ifp->if_index; 2531186119Sqingli error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2532186119Sqingli if (error) 2533186119Sqingli break; 2534186119Sqingli } 2535186119Sqingli } 2536186119Sqingli return error; 2537186119Sqingli} 2538186119Sqingli 2539121161Sumevoid * 2540171259Sdelphijin6_domifattach(struct ifnet *ifp) 2541121161Sume{ 2542121161Sume struct in6_ifextra *ext; 2543121161Sume 2544121161Sume ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2545121161Sume bzero(ext, sizeof(*ext)); 2546121161Sume 2547121161Sume ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat), 2548121161Sume M_IFADDR, M_WAITOK); 2549121161Sume bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat)); 2550121161Sume 2551121161Sume ext->icmp6_ifstat = 2552121161Sume (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat), 2553121161Sume M_IFADDR, M_WAITOK); 2554121161Sume bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat)); 2555121161Sume 2556121161Sume ext->nd_ifinfo = nd6_ifattach(ifp); 2557121161Sume ext->scope6_id = scope6_ifattach(ifp); 2558186119Sqingli ext->lltable = lltable_init(ifp, AF_INET6); 2559186119Sqingli if (ext->lltable != NULL) { 2560186119Sqingli ext->lltable->llt_new = in6_lltable_new; 2561186119Sqingli ext->lltable->llt_free = in6_lltable_free; 2562192476Sqingli ext->lltable->llt_prefix_free = in6_lltable_prefix_free; 2563186119Sqingli ext->lltable->llt_rtcheck = in6_lltable_rtcheck; 2564186119Sqingli ext->lltable->llt_lookup = in6_lltable_lookup; 2565186119Sqingli ext->lltable->llt_dump = in6_lltable_dump; 2566186119Sqingli } 2567191672Sbms 2568191672Sbms ext->mld_ifinfo = mld_domifattach(ifp); 2569191672Sbms 2570121161Sume return ext; 2571121161Sume} 2572121161Sume 2573121161Sumevoid 2574171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux) 2575121161Sume{ 2576121161Sume struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2577121161Sume 2578191672Sbms mld_domifdetach(ifp); 2579121161Sume scope6_ifdetach(ext->scope6_id); 2580121161Sume nd6_ifdetach(ext->nd_ifinfo); 2581186119Sqingli lltable_free(ext->lltable); 2582121161Sume free(ext->in6_ifstat, M_IFADDR); 2583121161Sume free(ext->icmp6_ifstat, M_IFADDR); 2584121161Sume free(ext, M_IFADDR); 2585121161Sume} 2586121161Sume 258753541Sshin/* 258895023Ssuz * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 258953541Sshin * v4 mapped addr or v4 compat addr 259053541Sshin */ 259153541Sshinvoid 259253541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 259353541Sshin{ 2594171259Sdelphij 259553541Sshin bzero(sin, sizeof(*sin)); 259653541Sshin sin->sin_len = sizeof(struct sockaddr_in); 259753541Sshin sin->sin_family = AF_INET; 259853541Sshin sin->sin_port = sin6->sin6_port; 2599120891Sume sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 260053541Sshin} 260153541Sshin 260253541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 260353541Sshinvoid 260453541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 260553541Sshin{ 260653541Sshin bzero(sin6, sizeof(*sin6)); 260753541Sshin sin6->sin6_len = sizeof(struct sockaddr_in6); 260853541Sshin sin6->sin6_family = AF_INET6; 260953541Sshin sin6->sin6_port = sin->sin_port; 261053541Sshin sin6->sin6_addr.s6_addr32[0] = 0; 261153541Sshin sin6->sin6_addr.s6_addr32[1] = 0; 261253541Sshin sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 261353541Sshin sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 261453541Sshin} 261553541Sshin 261653541Sshin/* Convert sockaddr_in6 into sockaddr_in. */ 261753541Sshinvoid 261853541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam) 261953541Sshin{ 262053541Sshin struct sockaddr_in *sin_p; 262153541Sshin struct sockaddr_in6 sin6; 262253541Sshin 262353541Sshin /* 262453541Sshin * Save original sockaddr_in6 addr and convert it 262553541Sshin * to sockaddr_in. 262653541Sshin */ 262753541Sshin sin6 = *(struct sockaddr_in6 *)nam; 262853541Sshin sin_p = (struct sockaddr_in *)nam; 262953541Sshin in6_sin6_2_sin(sin_p, &sin6); 263053541Sshin} 263153541Sshin 263253541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 263353541Sshinvoid 263453541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 263553541Sshin{ 263653541Sshin struct sockaddr_in *sin_p; 263753541Sshin struct sockaddr_in6 *sin6_p; 263853541Sshin 2639184205Sdes sin6_p = malloc(sizeof *sin6_p, M_SONAME, 2640111119Simp M_WAITOK); 264153541Sshin sin_p = (struct sockaddr_in *)*nam; 264253541Sshin in6_sin_2_v4mapsin6(sin_p, sin6_p); 2643184205Sdes free(*nam, M_SONAME); 264453541Sshin *nam = (struct sockaddr *)sin6_p; 264553541Sshin} 2646