1/* 2 * Copyright (c) 2003-2014 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29/* 30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. Neither the name of the project nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58/* 59 * Copyright (c) 1982, 1986, 1991, 1993 60 * The Regents of the University of California. All rights reserved. 61 * 62 * Redistribution and use in source and binary forms, with or without 63 * modification, are permitted provided that the following conditions 64 * are met: 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 2. Redistributions in binary form must reproduce the above copyright 68 * notice, this list of conditions and the following disclaimer in the 69 * documentation and/or other materials provided with the distribution. 70 * 3. All advertising materials mentioning features or use of this software 71 * must display the following acknowledgement: 72 * This product includes software developed by the University of 73 * California, Berkeley and its contributors. 74 * 4. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)in.c 8.2 (Berkeley) 11/15/93 91 */ 92 93 94#include <sys/param.h> 95#include <sys/ioctl.h> 96#include <sys/errno.h> 97#include <sys/malloc.h> 98#include <sys/socket.h> 99#include <sys/socketvar.h> 100#include <sys/sockio.h> 101#include <sys/systm.h> 102#include <sys/time.h> 103#include <sys/kernel.h> 104#include <sys/syslog.h> 105#include <sys/kern_event.h> 106#include <sys/mcache.h> 107#include <sys/protosw.h> 108 109#include <kern/locks.h> 110#include <kern/zalloc.h> 111#include <libkern/OSAtomic.h> 112#include <machine/machine_routines.h> 113#include <mach/boolean.h> 114 115#include <net/if.h> 116#include <net/if_types.h> 117#include <net/if_var.h> 118#include <net/route.h> 119#include <net/if_dl.h> 120#include <net/kpi_protocol.h> 121 122#include <netinet/in.h> 123#include <netinet/in_var.h> 124#include <netinet/if_ether.h> 125#include <netinet/in_systm.h> 126#include <netinet/ip.h> 127#include <netinet/in_pcb.h> 128#include <netinet/icmp6.h> 129#include <netinet/tcp.h> 130#include <netinet/tcp_seq.h> 131#include <netinet/tcp_var.h> 132 133#include <netinet6/nd6.h> 134#include <netinet/ip6.h> 135#include <netinet6/ip6_var.h> 136#include <netinet6/mld6_var.h> 137#include <netinet6/in6_ifattach.h> 138#include <netinet6/scope6_var.h> 139#include <netinet6/in6_var.h> 140#include <netinet6/in6_pcb.h> 141 142#include <net/net_osdep.h> 143 144#if PF 145#include <net/pfvar.h> 146#endif /* PF */ 147 148/* 149 * Definitions of some costant IP6 addresses. 150 */ 151const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 152const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 153const struct in6_addr in6addr_nodelocal_allnodes = 154 IN6ADDR_NODELOCAL_ALLNODES_INIT; 155const struct in6_addr in6addr_linklocal_allnodes = 156 IN6ADDR_LINKLOCAL_ALLNODES_INIT; 157const struct in6_addr in6addr_linklocal_allrouters = 158 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 159const struct in6_addr in6addr_linklocal_allv2routers = 160 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT; 161 162const struct in6_addr in6mask0 = IN6MASK0; 163const struct in6_addr in6mask7 = IN6MASK7; 164const struct in6_addr in6mask16 = IN6MASK16; 165const struct in6_addr in6mask32 = IN6MASK32; 166const struct in6_addr in6mask64 = IN6MASK64; 167const struct in6_addr in6mask96 = IN6MASK96; 168const struct in6_addr in6mask128 = IN6MASK128; 169 170const struct sockaddr_in6 sa6_any = { 171 sizeof (sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 172}; 173 174static int in6ctl_associd(struct socket *, u_long, caddr_t); 175static int in6ctl_connid(struct socket *, u_long, caddr_t); 176static int in6ctl_conninfo(struct socket *, u_long, caddr_t); 177static int in6ctl_llstart(struct ifnet *, u_long, caddr_t); 178static int in6ctl_llstop(struct ifnet *); 179static int in6ctl_cgastart(struct ifnet *, u_long, caddr_t); 180static int in6ctl_gifaddr(struct ifnet *, struct in6_ifaddr *, u_long, 181 struct in6_ifreq *); 182static int in6ctl_gifstat(struct ifnet *, u_long, struct in6_ifreq *); 183static int in6ctl_alifetime(struct in6_ifaddr *, u_long, struct in6_ifreq *, 184 boolean_t); 185static int in6ctl_aifaddr(struct ifnet *, struct in6_aliasreq *); 186static void in6ctl_difaddr(struct ifnet *, struct in6_ifaddr *); 187static int in6_autoconf(struct ifnet *, int); 188static int in6_setrouter(struct ifnet *, int); 189static int in6_ifinit(struct ifnet *, struct in6_ifaddr *, int); 190static int in6_ifaupdate_aux(struct in6_ifaddr *, struct ifnet *, int); 191static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 192static struct in6_ifaddr *in6_ifaddr_alloc(int); 193static void in6_ifaddr_attached(struct ifaddr *); 194static void in6_ifaddr_detached(struct ifaddr *); 195static void in6_ifaddr_free(struct ifaddr *); 196static void in6_ifaddr_trace(struct ifaddr *, int); 197#if defined(__LP64__) 198static void in6_llstartreq_32_to_64(struct in6_llstartreq_32 *, 199 struct in6_llstartreq_64 *); 200#else 201static void in6_llstartreq_64_to_32(struct in6_llstartreq_64 *, 202 struct in6_llstartreq_32 *); 203#endif 204static struct in6_aliasreq *in6_aliasreq_to_native(void *, int, 205 struct in6_aliasreq *); 206static struct in6_llstartreq *in6_llstartreq_to_native(void *, int, 207 struct in6_llstartreq *); 208static int in6_to_kamescope(struct sockaddr_in6 *, struct ifnet *); 209 210static void in6_ifaddr_set_dadprogress(struct in6_ifaddr *); 211 212static int in6_getassocids(struct socket *, uint32_t *, user_addr_t); 213static int in6_getconnids(struct socket *, associd_t, uint32_t *, user_addr_t); 214static int in6_getconninfo(struct socket *, connid_t, uint32_t *, 215 uint32_t *, int32_t *, user_addr_t, socklen_t *, user_addr_t, socklen_t *, 216 uint32_t *, user_addr_t, uint32_t *); 217 218static void in6_if_up_dad_start(struct ifnet *); 219 220extern lck_mtx_t *nd6_mutex; 221extern int in6_init2done; 222 223#define IN6IFA_TRACE_HIST_SIZE 32 /* size of trace history */ 224 225/* For gdb */ 226__private_extern__ unsigned int in6ifa_trace_hist_size = IN6IFA_TRACE_HIST_SIZE; 227 228struct in6_ifaddr_dbg { 229 struct in6_ifaddr in6ifa; /* in6_ifaddr */ 230 struct in6_ifaddr in6ifa_old; /* saved in6_ifaddr */ 231 u_int16_t in6ifa_refhold_cnt; /* # of IFA_ADDREF */ 232 u_int16_t in6ifa_refrele_cnt; /* # of IFA_REMREF */ 233 /* 234 * Alloc and free callers. 235 */ 236 ctrace_t in6ifa_alloc; 237 ctrace_t in6ifa_free; 238 /* 239 * Circular lists of IFA_ADDREF and IFA_REMREF callers. 240 */ 241 ctrace_t in6ifa_refhold[IN6IFA_TRACE_HIST_SIZE]; 242 ctrace_t in6ifa_refrele[IN6IFA_TRACE_HIST_SIZE]; 243 /* 244 * Trash list linkage 245 */ 246 TAILQ_ENTRY(in6_ifaddr_dbg) in6ifa_trash_link; 247}; 248 249/* List of trash in6_ifaddr entries protected by in6ifa_trash_lock */ 250static TAILQ_HEAD(, in6_ifaddr_dbg) in6ifa_trash_head; 251static decl_lck_mtx_data(, in6ifa_trash_lock); 252 253#if DEBUG 254static unsigned int in6ifa_debug = 1; /* debugging (enabled) */ 255#else 256static unsigned int in6ifa_debug; /* debugging (disabled) */ 257#endif /* !DEBUG */ 258static unsigned int in6ifa_size; /* size of zone element */ 259static struct zone *in6ifa_zone; /* zone for in6_ifaddr */ 260 261#define IN6IFA_ZONE_MAX 64 /* maximum elements in zone */ 262#define IN6IFA_ZONE_NAME "in6_ifaddr" /* zone name */ 263 264/* 265 * Subroutine for in6_ifaddloop() and in6_ifremloop(). 266 * This routine does actual work. 267 */ 268static void 269in6_ifloop_request(int cmd, struct ifaddr *ifa) 270{ 271 struct sockaddr_in6 all1_sa; 272 struct rtentry *nrt = NULL; 273 int e; 274 275 bzero(&all1_sa, sizeof (all1_sa)); 276 all1_sa.sin6_family = AF_INET6; 277 all1_sa.sin6_len = sizeof (struct sockaddr_in6); 278 all1_sa.sin6_addr = in6mask128; 279 280 /* 281 * We specify the address itself as the gateway, and set the 282 * RTF_LLINFO flag, so that the corresponding host route would have 283 * the flag, and thus applications that assume traditional behavior 284 * would be happy. Note that we assume the caller of the function 285 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest, 286 * which changes the outgoing interface to the loopback interface. 287 * ifa_addr for INET6 is set once during init; no need to hold lock. 288 */ 289 lck_mtx_lock(rnh_lock); 290 e = rtrequest_locked(cmd, ifa->ifa_addr, ifa->ifa_addr, 291 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt); 292 if (e != 0) { 293 log(LOG_ERR, "in6_ifloop_request: " 294 "%s operation failed for %s (errno=%d)\n", 295 cmd == RTM_ADD ? "ADD" : "DELETE", 296 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr), 297 e); 298 } 299 300 if (nrt != NULL) 301 RT_LOCK(nrt); 302 /* 303 * Make sure rt_ifa be equal to IFA, the second argument of the 304 * function. 305 * We need this because when we refer to rt_ifa->ia6_flags in 306 * ip6_input, we assume that the rt_ifa points to the address instead 307 * of the loopback address. 308 */ 309 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) { 310 rtsetifa(nrt, ifa); 311 } 312 313 /* 314 * Report the addition/removal of the address to the routing socket. 315 * XXX: since we called rtinit for a p2p interface with a destination, 316 * we end up reporting twice in such a case. Should we rather 317 * omit the second report? 318 */ 319 if (nrt != NULL) { 320 rt_newaddrmsg(cmd, ifa, e, nrt); 321 if (cmd == RTM_DELETE) { 322 RT_UNLOCK(nrt); 323 rtfree_locked(nrt); 324 } else { 325 /* the cmd must be RTM_ADD here */ 326 RT_REMREF_LOCKED(nrt); 327 RT_UNLOCK(nrt); 328 } 329 } 330 lck_mtx_unlock(rnh_lock); 331} 332 333/* 334 * Add ownaddr as loopback rtentry. We previously add the route only if 335 * necessary (ex. on a p2p link). However, since we now manage addresses 336 * separately from prefixes, we should always add the route. We can't 337 * rely on the cloning mechanism from the corresponding interface route 338 * any more. 339 */ 340static void 341in6_ifaddloop(struct ifaddr *ifa) 342{ 343 struct rtentry *rt; 344 345 /* 346 * If there is no loopback entry, allocate one. ifa_addr for 347 * INET6 is set once during init; no need to hold lock. 348 */ 349 rt = rtalloc1(ifa->ifa_addr, 0, 0); 350 if (rt != NULL) 351 RT_LOCK(rt); 352 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 || 353 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) { 354 if (rt != NULL) { 355 RT_REMREF_LOCKED(rt); 356 RT_UNLOCK(rt); 357 } 358 in6_ifloop_request(RTM_ADD, ifa); 359 } else if (rt != NULL) { 360 RT_REMREF_LOCKED(rt); 361 RT_UNLOCK(rt); 362 } 363} 364 365/* 366 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(), 367 * if it exists. 368 */ 369static void 370in6_ifremloop(struct ifaddr *ifa) 371{ 372 struct in6_ifaddr *ia; 373 struct rtentry *rt; 374 int ia_count = 0; 375 376 /* 377 * Some of BSD variants do not remove cloned routes 378 * from an interface direct route, when removing the direct route 379 * (see comments in net/net_osdep.h). Even for variants that do remove 380 * cloned routes, they could fail to remove the cloned routes when 381 * we handle multple addresses that share a common prefix. 382 * So, we should remove the route corresponding to the deleted address 383 * regardless of the result of in6_is_ifloop_auto(). 384 */ 385 386 /* 387 * Delete the entry only if exact one ifa exists. More than one ifa 388 * can exist if we assign a same single address to multiple 389 * (probably p2p) interfaces. 390 * XXX: we should avoid such a configuration in IPv6... 391 */ 392 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 393 for (ia = in6_ifaddrs; ia; ia = ia->ia_next) { 394 IFA_LOCK(&ia->ia_ifa); 395 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) { 396 ia_count++; 397 if (ia_count > 1) { 398 IFA_UNLOCK(&ia->ia_ifa); 399 break; 400 } 401 } 402 IFA_UNLOCK(&ia->ia_ifa); 403 } 404 lck_rw_done(&in6_ifaddr_rwlock); 405 406 if (ia_count == 1) { 407 /* 408 * Before deleting, check if a corresponding loopbacked host 409 * route surely exists. With this check, we can avoid to 410 * delete an interface direct route whose destination is same 411 * as the address being removed. This can happen when removing 412 * a subnet-router anycast address on an interface attahced 413 * to a shared medium. ifa_addr for INET6 is set once during 414 * init; no need to hold lock. 415 */ 416 rt = rtalloc1(ifa->ifa_addr, 0, 0); 417 if (rt != NULL) { 418 RT_LOCK(rt); 419 if ((rt->rt_flags & RTF_HOST) != 0 && 420 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { 421 RT_REMREF_LOCKED(rt); 422 RT_UNLOCK(rt); 423 in6_ifloop_request(RTM_DELETE, ifa); 424 } else { 425 RT_UNLOCK(rt); 426 } 427 } 428 } 429} 430 431 432int 433in6_mask2len(mask, lim0) 434 struct in6_addr *mask; 435 u_char *lim0; 436{ 437 int x = 0, y; 438 u_char *lim = lim0, *p; 439 440 /* ignore the scope_id part */ 441 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof (*mask)) 442 lim = (u_char *)mask + sizeof (*mask); 443 for (p = (u_char *)mask; p < lim; x++, p++) { 444 if (*p != 0xff) 445 break; 446 } 447 y = 0; 448 if (p < lim) { 449 for (y = 0; y < 8; y++) { 450 if ((*p & (0x80 >> y)) == 0) 451 break; 452 } 453 } 454 455 /* 456 * when the limit pointer is given, do a stricter check on the 457 * remaining bits. 458 */ 459 if (p < lim) { 460 if (y != 0 && (*p & (0x00ff >> y)) != 0) 461 return (-1); 462 for (p = p + 1; p < lim; p++) 463 if (*p != 0) 464 return (-1); 465 } 466 467 return (x * 8 + y); 468} 469 470void 471in6_len2mask(mask, len) 472 struct in6_addr *mask; 473 int len; 474{ 475 int i; 476 477 bzero(mask, sizeof (*mask)); 478 for (i = 0; i < len / 8; i++) 479 mask->s6_addr8[i] = 0xff; 480 if (len % 8) 481 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff; 482} 483 484void 485in6_aliasreq_64_to_32(struct in6_aliasreq_64 *src, struct in6_aliasreq_32 *dst) 486{ 487 bzero(dst, sizeof (*dst)); 488 bcopy(src->ifra_name, dst->ifra_name, sizeof (dst->ifra_name)); 489 dst->ifra_addr = src->ifra_addr; 490 dst->ifra_dstaddr = src->ifra_dstaddr; 491 dst->ifra_prefixmask = src->ifra_prefixmask; 492 dst->ifra_flags = src->ifra_flags; 493 dst->ifra_lifetime.ia6t_expire = src->ifra_lifetime.ia6t_expire; 494 dst->ifra_lifetime.ia6t_preferred = src->ifra_lifetime.ia6t_preferred; 495 dst->ifra_lifetime.ia6t_vltime = src->ifra_lifetime.ia6t_vltime; 496 dst->ifra_lifetime.ia6t_pltime = src->ifra_lifetime.ia6t_pltime; 497} 498 499void 500in6_aliasreq_32_to_64(struct in6_aliasreq_32 *src, struct in6_aliasreq_64 *dst) 501{ 502 bzero(dst, sizeof (*dst)); 503 bcopy(src->ifra_name, dst->ifra_name, sizeof (dst->ifra_name)); 504 dst->ifra_addr = src->ifra_addr; 505 dst->ifra_dstaddr = src->ifra_dstaddr; 506 dst->ifra_prefixmask = src->ifra_prefixmask; 507 dst->ifra_flags = src->ifra_flags; 508 dst->ifra_lifetime.ia6t_expire = src->ifra_lifetime.ia6t_expire; 509 dst->ifra_lifetime.ia6t_preferred = src->ifra_lifetime.ia6t_preferred; 510 dst->ifra_lifetime.ia6t_vltime = src->ifra_lifetime.ia6t_vltime; 511 dst->ifra_lifetime.ia6t_pltime = src->ifra_lifetime.ia6t_pltime; 512} 513 514#if defined(__LP64__) 515void 516in6_llstartreq_32_to_64(struct in6_llstartreq_32 *src, 517 struct in6_llstartreq_64 *dst) 518{ 519 bzero(dst, sizeof (*dst)); 520 bcopy(src->llsr_name, dst->llsr_name, sizeof (dst->llsr_name)); 521 dst->llsr_flags = src->llsr_flags; 522 bcopy(src->llsr_cgaprep.cga_modifier.octets, 523 dst->llsr_cgaprep.cga_modifier.octets, 524 sizeof (dst->llsr_cgaprep.cga_modifier.octets)); 525 dst->llsr_cgaprep.cga_security_level = 526 src->llsr_cgaprep.cga_security_level; 527 dst->llsr_lifetime.ia6t_expire = src->llsr_lifetime.ia6t_expire; 528 dst->llsr_lifetime.ia6t_preferred = src->llsr_lifetime.ia6t_preferred; 529 dst->llsr_lifetime.ia6t_vltime = src->llsr_lifetime.ia6t_vltime; 530 dst->llsr_lifetime.ia6t_pltime = src->llsr_lifetime.ia6t_pltime; 531} 532#endif 533 534#if !defined(__LP64__) 535void 536in6_llstartreq_64_to_32(struct in6_llstartreq_64 *src, 537 struct in6_llstartreq_32 *dst) 538{ 539 bzero(dst, sizeof (*dst)); 540 bcopy(src->llsr_name, dst->llsr_name, sizeof (dst->llsr_name)); 541 dst->llsr_flags = src->llsr_flags; 542 bcopy(src->llsr_cgaprep.cga_modifier.octets, 543 dst->llsr_cgaprep.cga_modifier.octets, 544 sizeof (dst->llsr_cgaprep.cga_modifier.octets)); 545 dst->llsr_cgaprep.cga_security_level = 546 src->llsr_cgaprep.cga_security_level; 547 dst->llsr_lifetime.ia6t_expire = src->llsr_lifetime.ia6t_expire; 548 dst->llsr_lifetime.ia6t_preferred = src->llsr_lifetime.ia6t_preferred; 549 dst->llsr_lifetime.ia6t_vltime = src->llsr_lifetime.ia6t_vltime; 550 dst->llsr_lifetime.ia6t_pltime = src->llsr_lifetime.ia6t_pltime; 551} 552#endif 553 554static struct in6_aliasreq * 555in6_aliasreq_to_native(void *data, int data_is_64, struct in6_aliasreq *dst) 556{ 557#if defined(__LP64__) 558 if (data_is_64) 559 bcopy(data, dst, sizeof (*dst)); 560 else 561 in6_aliasreq_32_to_64((struct in6_aliasreq_32 *)data, 562 (struct in6_aliasreq_64 *)dst); 563#else 564 if (data_is_64) 565 in6_aliasreq_64_to_32((struct in6_aliasreq_64 *)data, 566 (struct in6_aliasreq_32 *)dst); 567 else 568 bcopy(data, dst, sizeof (*dst)); 569#endif /* __LP64__ */ 570 return (dst); 571} 572 573static struct in6_llstartreq * 574in6_llstartreq_to_native(void *data, int is64, struct in6_llstartreq *dst) 575{ 576#if defined(__LP64__) 577 if (is64) 578 bcopy(data, dst, sizeof (*dst)); 579 else 580 in6_llstartreq_32_to_64((struct in6_llstartreq_32 *)data, 581 (struct in6_llstartreq_64 *)dst); 582#else 583 if (is64) 584 in6_llstartreq_64_to_32((struct in6_llstartreq_64 *)data, 585 (struct in6_llstartreq_32 *)dst); 586 else 587 bcopy(data, dst, sizeof (*dst)); 588#endif /* __LP64__ */ 589 return (dst); 590} 591 592static __attribute__((noinline)) int 593in6ctl_associd(struct socket *so, u_long cmd, caddr_t data) 594{ 595 int error = 0; 596 union { 597 struct so_aidreq32 a32; 598 struct so_aidreq64 a64; 599 } u; 600 601 VERIFY(so != NULL); 602 603 switch (cmd) { 604 case SIOCGASSOCIDS32: { /* struct so_aidreq32 */ 605 bcopy(data, &u.a32, sizeof (u.a32)); 606 error = in6_getassocids(so, &u.a32.sar_cnt, u.a32.sar_aidp); 607 if (error == 0) 608 bcopy(&u.a32, data, sizeof (u.a32)); 609 break; 610 } 611 612 case SIOCGASSOCIDS64: { /* struct so_aidreq64 */ 613 bcopy(data, &u.a64, sizeof (u.a64)); 614 error = in6_getassocids(so, &u.a64.sar_cnt, u.a64.sar_aidp); 615 if (error == 0) 616 bcopy(&u.a64, data, sizeof (u.a64)); 617 break; 618 } 619 620 default: 621 VERIFY(0); 622 /* NOTREACHED */ 623 } 624 625 return (error); 626} 627 628static __attribute__((noinline)) int 629in6ctl_connid(struct socket *so, u_long cmd, caddr_t data) 630{ 631 int error = 0; 632 union { 633 struct so_cidreq32 c32; 634 struct so_cidreq64 c64; 635 } u; 636 637 VERIFY(so != NULL); 638 639 switch (cmd) { 640 case SIOCGCONNIDS32: { /* struct so_cidreq32 */ 641 bcopy(data, &u.c32, sizeof (u.c32)); 642 error = in6_getconnids(so, u.c32.scr_aid, &u.c32.scr_cnt, 643 u.c32.scr_cidp); 644 if (error == 0) 645 bcopy(&u.c32, data, sizeof (u.c32)); 646 break; 647 } 648 649 case SIOCGCONNIDS64: { /* struct so_cidreq64 */ 650 bcopy(data, &u.c64, sizeof (u.c64)); 651 error = in6_getconnids(so, u.c64.scr_aid, &u.c64.scr_cnt, 652 u.c64.scr_cidp); 653 if (error == 0) 654 bcopy(&u.c64, data, sizeof (u.c64)); 655 break; 656 } 657 658 default: 659 VERIFY(0); 660 /* NOTREACHED */ 661 } 662 663 return (error); 664} 665 666static __attribute__((noinline)) int 667in6ctl_conninfo(struct socket *so, u_long cmd, caddr_t data) 668{ 669 int error = 0; 670 union { 671 struct so_cinforeq32 ci32; 672 struct so_cinforeq64 ci64; 673 } u; 674 675 VERIFY(so != NULL); 676 677 switch (cmd) { 678 case SIOCGCONNINFO32: { /* struct so_cinforeq32 */ 679 bcopy(data, &u.ci32, sizeof (u.ci32)); 680 error = in6_getconninfo(so, u.ci32.scir_cid, &u.ci32.scir_flags, 681 &u.ci32.scir_ifindex, &u.ci32.scir_error, u.ci32.scir_src, 682 &u.ci32.scir_src_len, u.ci32.scir_dst, &u.ci32.scir_dst_len, 683 &u.ci32.scir_aux_type, u.ci32.scir_aux_data, 684 &u.ci32.scir_aux_len); 685 if (error == 0) 686 bcopy(&u.ci32, data, sizeof (u.ci32)); 687 break; 688 } 689 690 case SIOCGCONNINFO64: { /* struct so_cinforeq64 */ 691 bcopy(data, &u.ci64, sizeof (u.ci64)); 692 error = in6_getconninfo(so, u.ci64.scir_cid, &u.ci64.scir_flags, 693 &u.ci64.scir_ifindex, &u.ci64.scir_error, u.ci64.scir_src, 694 &u.ci64.scir_src_len, u.ci64.scir_dst, &u.ci64.scir_dst_len, 695 &u.ci64.scir_aux_type, u.ci64.scir_aux_data, 696 &u.ci64.scir_aux_len); 697 if (error == 0) 698 bcopy(&u.ci64, data, sizeof (u.ci64)); 699 break; 700 } 701 702 default: 703 VERIFY(0); 704 /* NOTREACHED */ 705 } 706 707 return (error); 708} 709 710static __attribute__((noinline)) int 711in6ctl_llstart(struct ifnet *ifp, u_long cmd, caddr_t data) 712{ 713 struct in6_aliasreq sifra, *ifra = NULL; 714 boolean_t is64; 715 int error = 0; 716 717 VERIFY(ifp != NULL); 718 719 switch (cmd) { 720 case SIOCLL_START_32: /* struct in6_aliasreq_32 */ 721 case SIOCLL_START_64: /* struct in6_aliasreq_64 */ 722 is64 = (cmd == SIOCLL_START_64); 723 /* 724 * Convert user ifra to the kernel form, when appropriate. 725 * This allows the conversion between different data models 726 * to be centralized, so that it can be passed around to other 727 * routines that are expecting the kernel form. 728 */ 729 ifra = in6_aliasreq_to_native(data, is64, &sifra); 730 731 /* 732 * NOTE: All the interface specific DLIL attachements should 733 * be done here. They are currently done in in6_ifattach_aux() 734 * for the interfaces that need it. 735 */ 736 if ((ifp->if_eflags & IFEF_NOAUTOIPV6LL) != 0 && 737 ifra->ifra_addr.sin6_family == AF_INET6 && 738 /* Only check ifra_dstaddr if valid */ 739 (ifra->ifra_dstaddr.sin6_len == 0 || 740 ifra->ifra_dstaddr.sin6_family == AF_INET6)) { 741 /* some interfaces may provide LinkLocal addresses */ 742 error = in6_ifattach_aliasreq(ifp, NULL, ifra); 743 } else { 744 error = in6_ifattach_aliasreq(ifp, NULL, NULL); 745 } 746 if (error == 0) 747 in6_if_up_dad_start(ifp); 748 break; 749 750 default: 751 VERIFY(0); 752 /* NOTREACHED */ 753 } 754 755 return (error); 756} 757 758static __attribute__((noinline)) int 759in6ctl_llstop(struct ifnet *ifp) 760{ 761 struct in6_ifaddr *ia; 762 struct nd_prefix pr0, *pr; 763 764 VERIFY(ifp != NULL); 765 766 /* Remove link local addresses from interface */ 767 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 768 ia = in6_ifaddrs; 769 while (ia != NULL) { 770 if (ia->ia_ifa.ifa_ifp != ifp) { 771 ia = ia->ia_next; 772 continue; 773 } 774 IFA_LOCK(&ia->ia_ifa); 775 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) { 776 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for us */ 777 IFA_UNLOCK(&ia->ia_ifa); 778 lck_rw_done(&in6_ifaddr_rwlock); 779 in6_purgeaddr(&ia->ia_ifa); 780 IFA_REMREF(&ia->ia_ifa); /* for us */ 781 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 782 /* 783 * Purging the address caused in6_ifaddr_rwlock 784 * to be dropped and reacquired; 785 * therefore search again from the beginning 786 * of in6_ifaddrs list. 787 */ 788 ia = in6_ifaddrs; 789 continue; 790 } 791 IFA_UNLOCK(&ia->ia_ifa); 792 ia = ia->ia_next; 793 } 794 lck_rw_done(&in6_ifaddr_rwlock); 795 796 /* Delete the link local prefix */ 797 bzero(&pr0, sizeof(pr0)); 798 pr0.ndpr_plen = 64; 799 pr0.ndpr_ifp = ifp; 800 pr0.ndpr_prefix.sin6_addr.s6_addr16[0] = IPV6_ADDR_INT16_ULL; 801 in6_setscope(&pr0.ndpr_prefix.sin6_addr, ifp, NULL); 802 pr = nd6_prefix_lookup(&pr0); 803 if (pr) { 804 lck_mtx_lock(nd6_mutex); 805 NDPR_LOCK(pr); 806 prelist_remove(pr); 807 NDPR_UNLOCK(pr); 808 NDPR_REMREF(pr); /* Drop the reference from lookup */ 809 lck_mtx_unlock(nd6_mutex); 810 } 811 812 return (0); 813} 814 815static __attribute__((noinline)) int 816in6ctl_cgastart(struct ifnet *ifp, u_long cmd, caddr_t data) 817{ 818 struct in6_llstartreq llsr; 819 int is64, error = 0; 820 821 VERIFY(ifp != NULL); 822 823 switch (cmd) { 824 case SIOCLL_CGASTART_32: /* struct in6_llstartreq_32 */ 825 case SIOCLL_CGASTART_64: /* struct in6_llstartreq_64 */ 826 is64 = (cmd == SIOCLL_CGASTART_64); 827 /* 828 * Convert user llstartreq to the kernel form, when appropriate. 829 * This allows the conversion between different data models 830 * to be centralized, so that it can be passed around to other 831 * routines that are expecting the kernel form. 832 */ 833 in6_llstartreq_to_native(data, is64, &llsr); 834 835 /* 836 * NOTE: All the interface specific DLIL attachements 837 * should be done here. They are currently done in 838 * in6_ifattach_llstartreq() for the interfaces that 839 * need it. 840 */ 841 error = in6_ifattach_llstartreq(ifp, &llsr); 842 if (error == 0) 843 in6_if_up_dad_start(ifp); 844 break; 845 846 default: 847 VERIFY(0); 848 /* NOTREACHED */ 849 } 850 851 return (error); 852} 853 854/* 855 * Caller passes in the ioctl data pointer directly via "ifr", with the 856 * expectation that this routine always uses bcopy() or other byte-aligned 857 * memory accesses. 858 */ 859static __attribute__((noinline)) int 860in6ctl_gifaddr(struct ifnet *ifp, struct in6_ifaddr *ia, u_long cmd, 861 struct in6_ifreq *ifr) 862{ 863 struct sockaddr_in6 addr; 864 int error = 0; 865 866 VERIFY(ifp != NULL); 867 868 if (ia == NULL) 869 return (EADDRNOTAVAIL); 870 871 switch (cmd) { 872 case SIOCGIFADDR_IN6: /* struct in6_ifreq */ 873 IFA_LOCK(&ia->ia_ifa); 874 bcopy(&ia->ia_addr, &addr, sizeof (addr)); 875 IFA_UNLOCK(&ia->ia_ifa); 876 if ((error = sa6_recoverscope(&addr, TRUE)) != 0) 877 break; 878 bcopy(&addr, &ifr->ifr_addr, sizeof (addr)); 879 break; 880 881 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */ 882 if (!(ifp->if_flags & IFF_POINTOPOINT)) { 883 error = EINVAL; 884 break; 885 } 886 /* 887 * XXX: should we check if ifa_dstaddr is NULL and return 888 * an error? 889 */ 890 IFA_LOCK(&ia->ia_ifa); 891 bcopy(&ia->ia_dstaddr, &addr, sizeof (addr)); 892 IFA_UNLOCK(&ia->ia_ifa); 893 if ((error = sa6_recoverscope(&addr, TRUE)) != 0) 894 break; 895 bcopy(&addr, &ifr->ifr_dstaddr, sizeof (addr)); 896 break; 897 898 default: 899 VERIFY(0); 900 /* NOTREACHED */ 901 } 902 903 return (error); 904} 905 906/* 907 * Caller passes in the ioctl data pointer directly via "ifr", with the 908 * expectation that this routine always uses bcopy() or other byte-aligned 909 * memory accesses. 910 */ 911static __attribute__((noinline)) int 912in6ctl_gifstat(struct ifnet *ifp, u_long cmd, struct in6_ifreq *ifr) 913{ 914 int error = 0, index; 915 916 VERIFY(ifp != NULL); 917 index = ifp->if_index; 918 919 switch (cmd) { 920 case SIOCGIFSTAT_IN6: /* struct in6_ifreq */ 921 /* N.B.: if_inet6data is never freed once set. */ 922 if (IN6_IFEXTRA(ifp) == NULL) { 923 /* return (EAFNOSUPPORT)? */ 924 bzero(&ifr->ifr_ifru.ifru_stat, 925 sizeof (ifr->ifr_ifru.ifru_stat)); 926 } else { 927 bcopy(&IN6_IFEXTRA(ifp)->in6_ifstat, 928 &ifr->ifr_ifru.ifru_stat, 929 sizeof (ifr->ifr_ifru.ifru_stat)); 930 } 931 break; 932 933 case SIOCGIFSTAT_ICMP6: /* struct in6_ifreq */ 934 /* N.B.: if_inet6data is never freed once set. */ 935 if (IN6_IFEXTRA(ifp) == NULL) { 936 /* return (EAFNOSUPPORT)? */ 937 bzero(&ifr->ifr_ifru.ifru_stat, 938 sizeof (ifr->ifr_ifru.ifru_icmp6stat)); 939 } else { 940 bcopy(&IN6_IFEXTRA(ifp)->icmp6_ifstat, 941 &ifr->ifr_ifru.ifru_icmp6stat, 942 sizeof (ifr->ifr_ifru.ifru_icmp6stat)); 943 } 944 break; 945 946 default: 947 VERIFY(0); 948 /* NOTREACHED */ 949 } 950 951 return (error); 952} 953 954/* 955 * Caller passes in the ioctl data pointer directly via "ifr", with the 956 * expectation that this routine always uses bcopy() or other byte-aligned 957 * memory accesses. 958 */ 959static __attribute__((noinline)) int 960in6ctl_alifetime(struct in6_ifaddr *ia, u_long cmd, struct in6_ifreq *ifr, 961 boolean_t p64) 962{ 963 uint64_t timenow = net_uptime(); 964 struct in6_addrlifetime ia6_lt; 965 struct timeval caltime; 966 int error = 0; 967 968 if (ia == NULL) 969 return (EADDRNOTAVAIL); 970 971 switch (cmd) { 972 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */ 973 IFA_LOCK(&ia->ia_ifa); 974 /* retrieve time as calendar time (last arg is 1) */ 975 in6ifa_getlifetime(ia, &ia6_lt, 1); 976 if (p64) { 977 struct in6_addrlifetime_64 lt; 978 979 bzero(<, sizeof (lt)); 980 lt.ia6t_expire = ia6_lt.ia6t_expire; 981 lt.ia6t_preferred = ia6_lt.ia6t_preferred; 982 lt.ia6t_vltime = ia6_lt.ia6t_vltime; 983 lt.ia6t_pltime = ia6_lt.ia6t_pltime; 984 bcopy(<, &ifr->ifr_ifru.ifru_lifetime, sizeof (lt)); 985 } else { 986 struct in6_addrlifetime_32 lt; 987 988 bzero(<, sizeof (lt)); 989 lt.ia6t_expire = (uint32_t)ia6_lt.ia6t_expire; 990 lt.ia6t_preferred = (uint32_t)ia6_lt.ia6t_preferred; 991 lt.ia6t_vltime = (uint32_t)ia6_lt.ia6t_vltime; 992 lt.ia6t_pltime = (uint32_t)ia6_lt.ia6t_pltime; 993 bcopy(<, &ifr->ifr_ifru.ifru_lifetime, sizeof (lt)); 994 } 995 IFA_UNLOCK(&ia->ia_ifa); 996 break; 997 998 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */ 999 getmicrotime(&caltime); 1000 1001 /* sanity for overflow - beware unsigned */ 1002 if (p64) { 1003 struct in6_addrlifetime_64 lt; 1004 1005 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof (lt)); 1006 if (lt.ia6t_vltime != ND6_INFINITE_LIFETIME && 1007 lt.ia6t_vltime + caltime.tv_sec < caltime.tv_sec) { 1008 error = EINVAL; 1009 break; 1010 } 1011 if (lt.ia6t_pltime != ND6_INFINITE_LIFETIME && 1012 lt.ia6t_pltime + caltime.tv_sec < caltime.tv_sec) { 1013 error = EINVAL; 1014 break; 1015 } 1016 } else { 1017 struct in6_addrlifetime_32 lt; 1018 1019 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof (lt)); 1020 if (lt.ia6t_vltime != ND6_INFINITE_LIFETIME && 1021 lt.ia6t_vltime + caltime.tv_sec < caltime.tv_sec) { 1022 error = EINVAL; 1023 break; 1024 } 1025 if (lt.ia6t_pltime != ND6_INFINITE_LIFETIME && 1026 lt.ia6t_pltime + caltime.tv_sec < caltime.tv_sec) { 1027 error = EINVAL; 1028 break; 1029 } 1030 } 1031 1032 IFA_LOCK(&ia->ia_ifa); 1033 if (p64) { 1034 struct in6_addrlifetime_64 lt; 1035 1036 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof (lt)); 1037 ia6_lt.ia6t_expire = lt.ia6t_expire; 1038 ia6_lt.ia6t_preferred = lt.ia6t_preferred; 1039 ia6_lt.ia6t_vltime = lt.ia6t_vltime; 1040 ia6_lt.ia6t_pltime = lt.ia6t_pltime; 1041 } else { 1042 struct in6_addrlifetime_32 lt; 1043 1044 bcopy(&ifr->ifr_ifru.ifru_lifetime, <, sizeof (lt)); 1045 ia6_lt.ia6t_expire = (uint32_t)lt.ia6t_expire; 1046 ia6_lt.ia6t_preferred = (uint32_t)lt.ia6t_preferred; 1047 ia6_lt.ia6t_vltime = lt.ia6t_vltime; 1048 ia6_lt.ia6t_pltime = lt.ia6t_pltime; 1049 } 1050 /* for sanity */ 1051 if (ia6_lt.ia6t_vltime != ND6_INFINITE_LIFETIME) 1052 ia6_lt.ia6t_expire = timenow + ia6_lt.ia6t_vltime; 1053 else 1054 ia6_lt.ia6t_expire = 0; 1055 1056 if (ia6_lt.ia6t_pltime != ND6_INFINITE_LIFETIME) 1057 ia6_lt.ia6t_preferred = timenow + ia6_lt.ia6t_pltime; 1058 else 1059 ia6_lt.ia6t_preferred = 0; 1060 1061 in6ifa_setlifetime(ia, &ia6_lt); 1062 IFA_UNLOCK(&ia->ia_ifa); 1063 break; 1064 1065 default: 1066 VERIFY(0); 1067 /* NOTREACHED */ 1068 } 1069 1070 return (error); 1071} 1072 1073#define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa)) 1074 1075/* 1076 * Generic INET6 control operations (ioctl's). 1077 * 1078 * ifp is NULL if not an interface-specific ioctl. 1079 * 1080 * Most of the routines called to handle the ioctls would end up being 1081 * tail-call optimized, which unfortunately causes this routine to 1082 * consume too much stack space; this is the reason for the "noinline" 1083 * attribute used on those routines. 1084 * 1085 * If called directly from within the networking stack (as opposed to via 1086 * pru_control), the socket parameter may be NULL. 1087 */ 1088int 1089in6_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 1090 struct proc *p) 1091{ 1092 struct in6_ifreq *ifr = (struct in6_ifreq *)(void *)data; 1093 struct in6_aliasreq sifra, *ifra = NULL; 1094 struct in6_ifaddr *ia = NULL; 1095 struct sockaddr_in6 sin6, *sa6 = NULL; 1096 boolean_t privileged = (proc_suser(p) == 0); 1097 boolean_t p64 = proc_is64bit(p); 1098 boolean_t so_unlocked = FALSE; 1099 int intval, error = 0; 1100 1101 /* In case it's NULL, make sure it came from the kernel */ 1102 VERIFY(so != NULL || p == kernproc); 1103 1104 /* 1105 * ioctls which don't require ifp, may require socket. 1106 */ 1107 switch (cmd) { 1108 case SIOCAADDRCTL_POLICY: /* struct in6_addrpolicy */ 1109 case SIOCDADDRCTL_POLICY: /* struct in6_addrpolicy */ 1110 if (!privileged) 1111 return (EPERM); 1112 return (in6_src_ioctl(cmd, data)); 1113 /* NOTREACHED */ 1114 1115 case SIOCDRADD_IN6_32: /* struct in6_defrouter_32 */ 1116 case SIOCDRADD_IN6_64: /* struct in6_defrouter_64 */ 1117 case SIOCDRDEL_IN6_32: /* struct in6_defrouter_32 */ 1118 case SIOCDRDEL_IN6_64: /* struct in6_defrouter_64 */ 1119 if (!privileged) 1120 return (EPERM); 1121 return (defrtrlist_ioctl(cmd, data)); 1122 /* NOTREACHED */ 1123 1124 case SIOCGASSOCIDS32: /* struct so_aidreq32 */ 1125 case SIOCGASSOCIDS64: /* struct so_aidreq64 */ 1126 return (in6ctl_associd(so, cmd, data)); 1127 /* NOTREACHED */ 1128 1129 case SIOCGCONNIDS32: /* struct so_cidreq32 */ 1130 case SIOCGCONNIDS64: /* struct so_cidreq64 */ 1131 return (in6ctl_connid(so, cmd, data)); 1132 /* NOTREACHED */ 1133 1134 case SIOCGCONNINFO32: /* struct so_cinforeq32 */ 1135 case SIOCGCONNINFO64: /* struct so_cinforeq64 */ 1136 return (in6ctl_conninfo(so, cmd, data)); 1137 /* NOTREACHED */ 1138 } 1139 1140 /* 1141 * The rest of ioctls require ifp; reject if we don't have one; 1142 * return ENXIO to be consistent with ifioctl(). 1143 */ 1144 if (ifp == NULL) 1145 return (ENXIO); 1146 1147 /* 1148 * ioctls which require ifp but not interface address. 1149 */ 1150 switch (cmd) { 1151 case SIOCAUTOCONF_START: /* struct in6_ifreq */ 1152 if (!privileged) 1153 return (EPERM); 1154 return (in6_autoconf(ifp, TRUE)); 1155 /* NOTREACHED */ 1156 1157 case SIOCAUTOCONF_STOP: /* struct in6_ifreq */ 1158 if (!privileged) 1159 return (EPERM); 1160 return (in6_autoconf(ifp, FALSE)); 1161 /* NOTREACHED */ 1162 1163 case SIOCLL_START_32: /* struct in6_aliasreq_32 */ 1164 case SIOCLL_START_64: /* struct in6_aliasreq_64 */ 1165 if (!privileged) 1166 return (EPERM); 1167 return (in6ctl_llstart(ifp, cmd, data)); 1168 /* NOTREACHED */ 1169 1170 case SIOCLL_STOP: /* struct in6_ifreq */ 1171 if (!privileged) 1172 return (EPERM); 1173 return (in6ctl_llstop(ifp)); 1174 /* NOTREACHED */ 1175 1176 case SIOCSETROUTERMODE_IN6: /* struct in6_ifreq */ 1177 if (!privileged) 1178 return (EPERM); 1179 1180 bcopy(&((struct in6_ifreq *)(void *)data)->ifr_intval, 1181 &intval, sizeof (intval)); 1182 1183 return (in6_setrouter(ifp, intval)); 1184 /* NOTREACHED */ 1185 1186 case SIOCPROTOATTACH_IN6_32: /* struct in6_aliasreq_32 */ 1187 case SIOCPROTOATTACH_IN6_64: /* struct in6_aliasreq_64 */ 1188 if (!privileged) 1189 return (EPERM); 1190 return (in6_domifattach(ifp)); 1191 /* NOTREACHED */ 1192 1193 case SIOCPROTODETACH_IN6: /* struct in6_ifreq */ 1194 if (!privileged) 1195 return (EPERM); 1196 1197 /* Cleanup interface routes and addresses */ 1198 in6_purgeif(ifp); 1199 1200 if ((error = proto_unplumb(PF_INET6, ifp))) 1201 log(LOG_ERR, "SIOCPROTODETACH_IN6: %s error=%d\n", 1202 if_name(ifp), error); 1203 return (error); 1204 /* NOTREACHED */ 1205 1206 case SIOCSNDFLUSH_IN6: /* struct in6_ifreq */ 1207 case SIOCSPFXFLUSH_IN6: /* struct in6_ifreq */ 1208 case SIOCSRTRFLUSH_IN6: /* struct in6_ifreq */ 1209 case SIOCSDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */ 1210 case SIOCSDEFIFACE_IN6_64: /* struct in6_ndifreq_64 */ 1211 case SIOCSIFINFO_FLAGS: /* struct in6_ndireq */ 1212 if (!privileged) 1213 return (EPERM); 1214 /* FALLTHRU */ 1215 case OSIOCGIFINFO_IN6: /* struct in6_ondireq */ 1216 case SIOCGIFINFO_IN6: /* struct in6_ondireq */ 1217 case SIOCGDRLST_IN6_32: /* struct in6_drlist_32 */ 1218 case SIOCGDRLST_IN6_64: /* struct in6_drlist_64 */ 1219 case SIOCGPRLST_IN6_32: /* struct in6_prlist_32 */ 1220 case SIOCGPRLST_IN6_64: /* struct in6_prlist_64 */ 1221 case SIOCGNBRINFO_IN6_32: /* struct in6_nbrinfo_32 */ 1222 case SIOCGNBRINFO_IN6_64: /* struct in6_nbrinfo_64 */ 1223 case SIOCGDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */ 1224 case SIOCGDEFIFACE_IN6_64: /* struct in6_ndifreq_64 */ 1225 return (nd6_ioctl(cmd, data, ifp)); 1226 /* NOTREACHED */ 1227 1228 case SIOCSIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */ 1229 case SIOCDIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */ 1230 case SIOCAIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */ 1231 case SIOCCIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */ 1232 case SIOCSGIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */ 1233 case SIOCGIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */ 1234 log(LOG_NOTICE, 1235 "prefix ioctls are now invalidated. " 1236 "please use ifconfig.\n"); 1237 return (EOPNOTSUPP); 1238 /* NOTREACHED */ 1239 1240 case SIOCSSCOPE6: /* struct in6_ifreq (deprecated) */ 1241 case SIOCGSCOPE6: /* struct in6_ifreq (deprecated) */ 1242 case SIOCGSCOPE6DEF: /* struct in6_ifreq (deprecated) */ 1243 return (EOPNOTSUPP); 1244 /* NOTREACHED */ 1245 1246 case SIOCLL_CGASTART_32: /* struct in6_llstartreq_32 */ 1247 case SIOCLL_CGASTART_64: /* struct in6_llstartreq_64 */ 1248 if (!privileged) 1249 return (EPERM); 1250 return (in6ctl_cgastart(ifp, cmd, data)); 1251 /* NOTREACHED */ 1252 1253 case SIOCGIFSTAT_IN6: /* struct in6_ifreq */ 1254 case SIOCGIFSTAT_ICMP6: /* struct in6_ifreq */ 1255 return (in6ctl_gifstat(ifp, cmd, ifr)); 1256 /* NOTREACHED */ 1257 } 1258 1259 /* 1260 * ioctls which require interface address; obtain sockaddr_in6. 1261 */ 1262 switch (cmd) { 1263 case SIOCSIFADDR_IN6: /* struct in6_ifreq (deprecated) */ 1264 case SIOCSIFDSTADDR_IN6: /* struct in6_ifreq (deprecated) */ 1265 case SIOCSIFNETMASK_IN6: /* struct in6_ifreq (deprecated) */ 1266 /* 1267 * Since IPv6 allows a node to assign multiple addresses 1268 * on a single interface, SIOCSIFxxx ioctls are deprecated. 1269 */ 1270 /* we decided to obsolete this command (20000704) */ 1271 return (EOPNOTSUPP); 1272 /* NOTREACHED */ 1273 1274 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */ 1275 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */ 1276 if (!privileged) 1277 return (EPERM); 1278 /* 1279 * Convert user ifra to the kernel form, when appropriate. 1280 * This allows the conversion between different data models 1281 * to be centralized, so that it can be passed around to other 1282 * routines that are expecting the kernel form. 1283 */ 1284 ifra = in6_aliasreq_to_native(data, 1285 (cmd == SIOCAIFADDR_IN6_64), &sifra); 1286 bcopy(&ifra->ifra_addr, &sin6, sizeof (sin6)); 1287 sa6 = &sin6; 1288 break; 1289 1290 case SIOCDIFADDR_IN6: /* struct in6_ifreq */ 1291 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */ 1292 if (!privileged) 1293 return (EPERM); 1294 /* FALLTHRU */ 1295 case SIOCGIFADDR_IN6: /* struct in6_ifreq */ 1296 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */ 1297 case SIOCGIFNETMASK_IN6: /* struct in6_ifreq */ 1298 case SIOCGIFAFLAG_IN6: /* struct in6_ifreq */ 1299 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */ 1300 bcopy(&ifr->ifr_addr, &sin6, sizeof (sin6)); 1301 sa6 = &sin6; 1302 break; 1303 } 1304 1305 /* 1306 * Find address for this interface, if it exists. 1307 * 1308 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 1309 * only, and used the first interface address as the target of other 1310 * operations (without checking ifra_addr). This was because netinet 1311 * code/API assumed at most 1 interface address per interface. 1312 * Since IPv6 allows a node to assign multiple addresses 1313 * on a single interface, we almost always look and check the 1314 * presence of ifra_addr, and reject invalid ones here. 1315 * It also decreases duplicated code among SIOC*_IN6 operations. 1316 */ 1317 VERIFY(ia == NULL); 1318 if (sa6 != NULL && sa6->sin6_family == AF_INET6) { 1319 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) { 1320 if (sa6->sin6_addr.s6_addr16[1] == 0) { 1321 /* link ID is not embedded by the user */ 1322 sa6->sin6_addr.s6_addr16[1] = 1323 htons(ifp->if_index); 1324 } else if (sa6->sin6_addr.s6_addr16[1] != 1325 htons(ifp->if_index)) { 1326 return (EINVAL); /* link ID contradicts */ 1327 } 1328 if (sa6->sin6_scope_id) { 1329 if (sa6->sin6_scope_id != 1330 (u_int32_t)ifp->if_index) 1331 return (EINVAL); 1332 sa6->sin6_scope_id = 0; /* XXX: good way? */ 1333 } 1334 } 1335 /* 1336 * Any failures from this point on must take into account 1337 * a non-NULL "ia" with an outstanding reference count, and 1338 * therefore requires IFA_REMREF. Jump to "done" label 1339 * instead of calling return if "ia" is valid. 1340 */ 1341 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 1342 } 1343 1344 /* 1345 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests. 1346 */ 1347 switch (cmd) { 1348 case SIOCDIFADDR_IN6: /* struct in6_ifreq */ 1349 if (ia == NULL) 1350 return (EADDRNOTAVAIL); 1351 /* FALLTHROUGH */ 1352 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */ 1353 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */ 1354 VERIFY(sa6 != NULL); 1355 /* 1356 * We always require users to specify a valid IPv6 address for 1357 * the corresponding operation. Use "sa6" instead of "ifra" 1358 * since SIOCDIFADDR_IN6 falls thru above. 1359 */ 1360 if (sa6->sin6_family != AF_INET6 || 1361 sa6->sin6_len != sizeof (struct sockaddr_in6)) { 1362 error = EAFNOSUPPORT; 1363 goto done; 1364 } 1365 break; 1366 } 1367 1368 /* 1369 * Unlock the socket since ifnet_ioctl() may be invoked by 1370 * one of the ioctl handlers below. Socket will be re-locked 1371 * prior to returning. 1372 */ 1373 if (so != NULL) { 1374 socket_unlock(so, 0); 1375 so_unlocked = TRUE; 1376 } 1377 1378 /* 1379 * And finally process address-related ioctls. 1380 */ 1381 switch (cmd) { 1382 case SIOCGIFADDR_IN6: /* struct in6_ifreq */ 1383 /* This interface is basically deprecated. use SIOCGIFCONF. */ 1384 /* FALLTHRU */ 1385 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */ 1386 error = in6ctl_gifaddr(ifp, ia, cmd, ifr); 1387 break; 1388 1389 case SIOCGIFNETMASK_IN6: /* struct in6_ifreq */ 1390 if (ia != NULL) { 1391 IFA_LOCK(&ia->ia_ifa); 1392 bcopy(&ia->ia_prefixmask, &ifr->ifr_addr, 1393 sizeof (struct sockaddr_in6)); 1394 IFA_UNLOCK(&ia->ia_ifa); 1395 } else { 1396 error = EADDRNOTAVAIL; 1397 } 1398 break; 1399 1400 case SIOCGIFAFLAG_IN6: /* struct in6_ifreq */ 1401 if (ia != NULL) { 1402 IFA_LOCK(&ia->ia_ifa); 1403 bcopy(&ia->ia6_flags, &ifr->ifr_ifru.ifru_flags6, 1404 sizeof (ifr->ifr_ifru.ifru_flags6)); 1405 IFA_UNLOCK(&ia->ia_ifa); 1406 } else { 1407 error = EADDRNOTAVAIL; 1408 } 1409 break; 1410 1411 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */ 1412 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */ 1413 error = in6ctl_alifetime(ia, cmd, ifr, p64); 1414 break; 1415 1416 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */ 1417 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */ 1418 error = in6ctl_aifaddr(ifp, ifra); 1419 break; 1420 1421 case SIOCDIFADDR_IN6: 1422 in6ctl_difaddr(ifp, ia); 1423 break; 1424 1425 default: 1426 error = ifnet_ioctl(ifp, PF_INET6, cmd, data); 1427 break; 1428 } 1429 1430done: 1431 if (ia != NULL) 1432 IFA_REMREF(&ia->ia_ifa); 1433 if (so_unlocked) 1434 socket_lock(so, 0); 1435 1436 return (error); 1437} 1438 1439static __attribute__((noinline)) int 1440in6ctl_aifaddr(struct ifnet *ifp, struct in6_aliasreq *ifra) 1441{ 1442 int i, error, addtmp, plen; 1443 struct nd_prefix pr0, *pr; 1444 struct in6_ifaddr *ia; 1445 1446 VERIFY(ifp != NULL && ifra != NULL); 1447 ia = NULL; 1448 1449 /* Attempt to attach the protocol, in case it isn't attached */ 1450 error = in6_domifattach(ifp); 1451 if (error == 0) { 1452 /* PF_INET6 wasn't previously attached */ 1453 error = in6_ifattach_aliasreq(ifp, NULL, NULL); 1454 if (error != 0) 1455 goto done; 1456 1457 in6_if_up_dad_start(ifp); 1458 } else if (error != EEXIST) { 1459 goto done; 1460 } 1461 1462 /* 1463 * First, make or update the interface address structure, and link it 1464 * to the list. 1465 */ 1466 error = in6_update_ifa(ifp, ifra, 0, &ia); 1467 if (error != 0) 1468 goto done; 1469 VERIFY(ia != NULL); 1470 1471 /* Now, make the prefix on-link on the interface. */ 1472 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, NULL); 1473 if (plen == 128) 1474 goto done; 1475 1476 /* 1477 * NOTE: We'd rather create the prefix before the address, but we need 1478 * at least one address to install the corresponding interface route, 1479 * so we configure the address first. 1480 */ 1481 1482 /* 1483 * Convert mask to prefix length (prefixmask has already been validated 1484 * in in6_update_ifa(). 1485 */ 1486 bzero(&pr0, sizeof (pr0)); 1487 pr0.ndpr_plen = plen; 1488 pr0.ndpr_ifp = ifp; 1489 pr0.ndpr_prefix = ifra->ifra_addr; 1490 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr; 1491 1492 /* apply the mask for safety. */ 1493 for (i = 0; i < 4; i++) { 1494 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 1495 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i]; 1496 } 1497 1498 /* 1499 * Since we don't have an API to set prefix (not address) lifetimes, we 1500 * just use the same lifetimes as addresses. The (temporarily) 1501 * installed lifetimes can be overridden by later advertised RAs (when 1502 * accept_rtadv is non 0), which is an intended behavior. 1503 */ 1504 pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 1505 pr0.ndpr_raf_auto = !!(ifra->ifra_flags & IN6_IFF_AUTOCONF); 1506 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 1507 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 1508 pr0.ndpr_stateflags |= NDPRF_STATIC; 1509 lck_mtx_init(&pr0.ndpr_lock, ifa_mtx_grp, ifa_mtx_attr); 1510 1511 /* add the prefix if there's one. */ 1512 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 1513 /* 1514 * nd6_prelist_add will install the corresponding interface 1515 * route. 1516 */ 1517 error = nd6_prelist_add(&pr0, NULL, &pr, FALSE); 1518 if (error != 0) 1519 goto done; 1520 1521 if (pr == NULL) { 1522 log(LOG_ERR, "%s: nd6_prelist_add okay, but" 1523 " no prefix.\n", __func__); 1524 error = EINVAL; 1525 goto done; 1526 } 1527 } 1528 1529 IFA_LOCK(&ia->ia_ifa); 1530 1531 /* if this is a new autoconfed addr */ 1532 addtmp = FALSE; 1533 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 && ia->ia6_ndpr == NULL) { 1534 NDPR_LOCK(pr); 1535 ++pr->ndpr_addrcnt; 1536 VERIFY(pr->ndpr_addrcnt != 0); 1537 ia->ia6_ndpr = pr; 1538 NDPR_ADDREF_LOCKED(pr); /* for addr reference */ 1539 1540 /* 1541 * If this is the first autoconf address from the prefix, 1542 * create a temporary address as well (when specified). 1543 */ 1544 addtmp = (ip6_use_tempaddr && pr->ndpr_addrcnt == 1); 1545 NDPR_UNLOCK(pr); 1546 } 1547 1548 IFA_UNLOCK(&ia->ia_ifa); 1549 1550 if (addtmp) { 1551 int e; 1552 e = in6_tmpifadd(ia, 1); 1553 if (e != 0) 1554 log(LOG_NOTICE, "%s: failed to create a" 1555 " temporary address, error=%d\n", 1556 __func__, e); 1557 } 1558 1559 /* 1560 * This might affect the status of autoconfigured addresses, that is, 1561 * this address might make other addresses detached. 1562 */ 1563 lck_mtx_lock(nd6_mutex); 1564 pfxlist_onlink_check(); 1565 lck_mtx_unlock(nd6_mutex); 1566 1567 /* Drop use count held above during lookup/add */ 1568 NDPR_REMREF(pr); 1569 1570done: 1571 if (ia != NULL) 1572 IFA_REMREF(&ia->ia_ifa); 1573 return (error); 1574} 1575 1576static __attribute__((noinline)) void 1577in6ctl_difaddr(struct ifnet *ifp, struct in6_ifaddr *ia) 1578{ 1579 int i = 0; 1580 struct nd_prefix pr0, *pr; 1581 1582 VERIFY(ifp != NULL && ia != NULL); 1583 1584 /* 1585 * If the address being deleted is the only one that owns 1586 * the corresponding prefix, expire the prefix as well. 1587 * XXX: theoretically, we don't have to worry about such 1588 * relationship, since we separate the address management 1589 * and the prefix management. We do this, however, to provide 1590 * as much backward compatibility as possible in terms of 1591 * the ioctl operation. 1592 * Note that in6_purgeaddr() will decrement ndpr_addrcnt. 1593 */ 1594 IFA_LOCK(&ia->ia_ifa); 1595 bzero(&pr0, sizeof (pr0)); 1596 pr0.ndpr_ifp = ifp; 1597 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 1598 if (pr0.ndpr_plen == 128) { 1599 IFA_UNLOCK(&ia->ia_ifa); 1600 goto purgeaddr; 1601 } 1602 pr0.ndpr_prefix = ia->ia_addr; 1603 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr; 1604 for (i = 0; i < 4; i++) { 1605 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &= 1606 ia->ia_prefixmask.sin6_addr.s6_addr32[i]; 1607 } 1608 IFA_UNLOCK(&ia->ia_ifa); 1609 /* 1610 * The logic of the following condition is a bit complicated. 1611 * We expire the prefix when 1612 * 1. the address obeys autoconfiguration and it is the 1613 * only owner of the associated prefix, or 1614 * 2. the address does not obey autoconf and there is no 1615 * other owner of the prefix. 1616 */ 1617 if ((pr = nd6_prefix_lookup(&pr0)) != NULL) { 1618 IFA_LOCK(&ia->ia_ifa); 1619 NDPR_LOCK(pr); 1620 if (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 && 1621 pr->ndpr_addrcnt == 1) || 1622 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 && 1623 pr->ndpr_addrcnt == 0)) { 1624 /* XXX: just for expiration */ 1625 pr->ndpr_expire = 1; 1626 } 1627 NDPR_UNLOCK(pr); 1628 IFA_UNLOCK(&ia->ia_ifa); 1629 1630 /* Drop use count held above during lookup */ 1631 NDPR_REMREF(pr); 1632 } 1633 1634purgeaddr: 1635 in6_purgeaddr(&ia->ia_ifa); 1636} 1637 1638static __attribute__((noinline)) int 1639in6_autoconf(struct ifnet *ifp, int enable) 1640{ 1641 int error = 0; 1642 1643 VERIFY(ifp != NULL); 1644 1645 if (ifp->if_flags & IFF_LOOPBACK) 1646 return (EINVAL); 1647 1648 if (enable) { 1649 /* 1650 * An interface in IPv6 router mode implies that it 1651 * is either configured with a static IP address or 1652 * autoconfigured via a locally-generated RA. Prevent 1653 * SIOCAUTOCONF_START from being set in that mode. 1654 */ 1655 ifnet_lock_exclusive(ifp); 1656 if (ifp->if_eflags & IFEF_IPV6_ROUTER) { 1657 ifp->if_eflags &= ~IFEF_ACCEPT_RTADV; 1658 error = EBUSY; 1659 } else { 1660 ifp->if_eflags |= IFEF_ACCEPT_RTADV; 1661 } 1662 ifnet_lock_done(ifp); 1663 } else { 1664 struct in6_ifaddr *ia = NULL; 1665 1666 ifnet_lock_exclusive(ifp); 1667 ifp->if_eflags &= ~IFEF_ACCEPT_RTADV; 1668 ifnet_lock_done(ifp); 1669 1670 /* Remove autoconfigured address from interface */ 1671 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 1672 ia = in6_ifaddrs; 1673 while (ia != NULL) { 1674 if (ia->ia_ifa.ifa_ifp != ifp) { 1675 ia = ia->ia_next; 1676 continue; 1677 } 1678 IFA_LOCK(&ia->ia_ifa); 1679 if (ia->ia6_flags & IN6_IFF_AUTOCONF) { 1680 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for us */ 1681 IFA_UNLOCK(&ia->ia_ifa); 1682 lck_rw_done(&in6_ifaddr_rwlock); 1683 in6_purgeaddr(&ia->ia_ifa); 1684 IFA_REMREF(&ia->ia_ifa); /* for us */ 1685 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 1686 /* 1687 * Purging the address caused in6_ifaddr_rwlock 1688 * to be dropped and reacquired; 1689 * therefore search again from the beginning 1690 * of in6_ifaddrs list. 1691 */ 1692 ia = in6_ifaddrs; 1693 continue; 1694 } 1695 IFA_UNLOCK(&ia->ia_ifa); 1696 ia = ia->ia_next; 1697 } 1698 lck_rw_done(&in6_ifaddr_rwlock); 1699 } 1700 return (error); 1701} 1702 1703/* 1704 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on 1705 * the interface. Entering or exiting this mode will result in the removal of 1706 * autoconfigured IPv6 addresses on the interface. 1707 */ 1708static __attribute__((noinline)) int 1709in6_setrouter(struct ifnet *ifp, int enable) 1710{ 1711 VERIFY(ifp != NULL); 1712 1713 if (ifp->if_flags & IFF_LOOPBACK) 1714 return (ENODEV); 1715 1716 if (enable) { 1717 struct nd_ifinfo *ndi; 1718 1719 lck_rw_lock_shared(nd_if_rwlock); 1720 ndi = ND_IFINFO(ifp); 1721 if (ndi != NULL && ndi->initialized) { 1722 lck_mtx_lock(&ndi->lock); 1723 if (ndi->flags & ND6_IFF_PROXY_PREFIXES) { 1724 /* No proxy if we are an advertising router */ 1725 ndi->flags &= ~ND6_IFF_PROXY_PREFIXES; 1726 lck_mtx_unlock(&ndi->lock); 1727 lck_rw_done(nd_if_rwlock); 1728 (void) nd6_if_prproxy(ifp, FALSE); 1729 } else { 1730 lck_mtx_unlock(&ndi->lock); 1731 lck_rw_done(nd_if_rwlock); 1732 } 1733 } else { 1734 lck_rw_done(nd_if_rwlock); 1735 } 1736 } 1737 1738 ifnet_lock_exclusive(ifp); 1739 if (enable) { 1740 ifp->if_eflags |= IFEF_IPV6_ROUTER; 1741 } else { 1742 ifp->if_eflags &= ~IFEF_IPV6_ROUTER; 1743 } 1744 ifnet_lock_done(ifp); 1745 1746 lck_mtx_lock(nd6_mutex); 1747 defrouter_select(ifp); 1748 lck_mtx_unlock(nd6_mutex); 1749 1750 if_allmulti(ifp, enable); 1751 1752 return (in6_autoconf(ifp, FALSE)); 1753} 1754 1755static int 1756in6_to_kamescope(struct sockaddr_in6 *sin6, struct ifnet *ifp) 1757{ 1758 struct sockaddr_in6 tmp; 1759 int error, id; 1760 1761 VERIFY(sin6 != NULL); 1762 tmp = *sin6; 1763 1764 error = in6_recoverscope(&tmp, &sin6->sin6_addr, ifp); 1765 if (error != 0) 1766 return (error); 1767 1768 id = in6_addr2scopeid(ifp, &tmp.sin6_addr); 1769 if (tmp.sin6_scope_id == 0) 1770 tmp.sin6_scope_id = id; 1771 else if (tmp.sin6_scope_id != id) 1772 return (EINVAL); /* scope ID mismatch. */ 1773 1774 error = in6_embedscope(&tmp.sin6_addr, &tmp, NULL, NULL, NULL); 1775 if (error != 0) 1776 return (error); 1777 1778 tmp.sin6_scope_id = 0; 1779 *sin6 = tmp; 1780 return (0); 1781} 1782 1783static int 1784in6_ifaupdate_aux(struct in6_ifaddr *ia, struct ifnet *ifp, int ifaupflags) 1785{ 1786 struct sockaddr_in6 mltaddr, mltmask; 1787 struct in6_addr llsol; 1788 struct ifaddr *ifa; 1789 struct in6_multi *in6m_sol; 1790 struct in6_multi_mship *imm; 1791 struct rtentry *rt; 1792 int delay, error; 1793 1794 VERIFY(ifp != NULL && ia != NULL); 1795 ifa = &ia->ia_ifa; 1796 in6m_sol = NULL; 1797 1798 nd6log2((LOG_DEBUG, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n", 1799 __func__, 1800 ip6_sprintf(&ia->ia_addr.sin6_addr), 1801 if_name(ia->ia_ifp), 1802 ia->ia6_flags, 1803 ifaupflags)); 1804 1805 /* 1806 * Mark the address as tentative before joining multicast addresses, 1807 * so that corresponding MLD responses would not have a tentative 1808 * source address. 1809 */ 1810 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 1811 if (in6if_do_dad(ifp)) 1812 in6_ifaddr_set_dadprogress(ia); 1813 1814 /* Join necessary multicast groups */ 1815 if ((ifp->if_flags & IFF_MULTICAST) != 0) { 1816 1817 /* join solicited multicast addr for new host id */ 1818 bzero(&llsol, sizeof (struct in6_addr)); 1819 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL; 1820 llsol.s6_addr32[1] = 0; 1821 llsol.s6_addr32[2] = htonl(1); 1822 llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3]; 1823 llsol.s6_addr8[12] = 0xff; 1824 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) { 1825 /* XXX: should not happen */ 1826 log(LOG_ERR, "%s: in6_setscope failed\n", __func__); 1827 goto unwind; 1828 } 1829 delay = 0; 1830 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY)) { 1831 /* 1832 * We need a random delay for DAD on the address 1833 * being configured. It also means delaying 1834 * transmission of the corresponding MLD report to 1835 * avoid report collision. [RFC 4862] 1836 */ 1837 delay = random() % MAX_RTR_SOLICITATION_DELAY; 1838 } 1839 imm = in6_joingroup(ifp, &llsol, &error, delay); 1840 if (imm == NULL) { 1841 nd6log((LOG_WARNING, 1842 "%s: addmulti failed for %s on %s (errno=%d)\n", 1843 __func__, ip6_sprintf(&llsol), if_name(ifp), 1844 error)); 1845 VERIFY(error != 0); 1846 goto unwind; 1847 } 1848 in6m_sol = imm->i6mm_maddr; 1849 /* take a refcount for this routine */ 1850 IN6M_ADDREF(in6m_sol); 1851 1852 IFA_LOCK_SPIN(ifa); 1853 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1854 IFA_UNLOCK(ifa); 1855 1856 bzero(&mltmask, sizeof (mltmask)); 1857 mltmask.sin6_len = sizeof (struct sockaddr_in6); 1858 mltmask.sin6_family = AF_INET6; 1859 mltmask.sin6_addr = in6mask32; 1860#define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 1861 1862 /* 1863 * join link-local all-nodes address 1864 */ 1865 bzero(&mltaddr, sizeof (mltaddr)); 1866 mltaddr.sin6_len = sizeof (struct sockaddr_in6); 1867 mltaddr.sin6_family = AF_INET6; 1868 mltaddr.sin6_addr = in6addr_linklocal_allnodes; 1869 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) 1870 goto unwind; /* XXX: should not fail */ 1871 1872 /* 1873 * XXX: do we really need this automatic routes? 1874 * We should probably reconsider this stuff. Most applications 1875 * actually do not need the routes, since they usually specify 1876 * the outgoing interface. 1877 */ 1878 rt = rtalloc1_scoped((struct sockaddr *)&mltaddr, 0, 0UL, 1879 ia->ia_ifp->if_index); 1880 if (rt) { 1881 if (memcmp(&mltaddr.sin6_addr, &((struct sockaddr_in6 *) 1882 (void *)rt_key(rt))->sin6_addr, MLTMASK_LEN)) { 1883 rtfree(rt); 1884 rt = NULL; 1885 } 1886 } 1887 if (!rt) { 1888 error = rtrequest_scoped(RTM_ADD, 1889 (struct sockaddr *)&mltaddr, 1890 (struct sockaddr *)&ia->ia_addr, 1891 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING, 1892 NULL, ia->ia_ifp->if_index); 1893 if (error) 1894 goto unwind; 1895 } else { 1896 rtfree(rt); 1897 } 1898 1899 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1900 if (!imm) { 1901 nd6log((LOG_WARNING, 1902 "%s: addmulti failed for %s on %s (errno=%d)\n", 1903 __func__, ip6_sprintf(&mltaddr.sin6_addr), 1904 if_name(ifp), error)); 1905 VERIFY(error != 0); 1906 goto unwind; 1907 } 1908 IFA_LOCK_SPIN(ifa); 1909 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1910 IFA_UNLOCK(ifa); 1911 1912 /* 1913 * join node information group address 1914 */ 1915#define hostnamelen strlen(hostname) 1916 delay = 0; 1917 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY)) { 1918 /* 1919 * The spec doesn't say anything about delay for this 1920 * group, but the same logic should apply. 1921 */ 1922 delay = random() % MAX_RTR_SOLICITATION_DELAY; 1923 } 1924 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr) 1925 == 0) { 1926 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 1927 delay); /* XXX jinmei */ 1928 if (!imm) { 1929 nd6log((LOG_WARNING, 1930 "%s: addmulti failed for %s on %s " 1931 "(errno=%d)\n", 1932 __func__, ip6_sprintf(&mltaddr.sin6_addr), 1933 if_name(ifp), error)); 1934 /* XXX not very fatal, go on... */ 1935 error = 0; 1936 } else { 1937 IFA_LOCK_SPIN(ifa); 1938 LIST_INSERT_HEAD(&ia->ia6_memberships, 1939 imm, i6mm_chain); 1940 IFA_UNLOCK(ifa); 1941 } 1942 } 1943#undef hostnamelen 1944 1945 /* 1946 * join interface-local all-nodes address. 1947 * (ff01::1%ifN, and ff01::%ifN/32) 1948 */ 1949 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 1950 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) 1951 goto unwind; /* XXX: should not fail */ 1952 /* XXX: again, do we really need the route? */ 1953 rt = rtalloc1_scoped((struct sockaddr *)&mltaddr, 0, 0UL, 1954 ia->ia_ifp->if_index); 1955 if (rt) { 1956 if (memcmp(&mltaddr.sin6_addr, &((struct sockaddr_in6 *) 1957 (void *)rt_key(rt))->sin6_addr, MLTMASK_LEN)) { 1958 rtfree(rt); 1959 rt = NULL; 1960 } 1961 } 1962 if (!rt) { 1963 error = rtrequest_scoped(RTM_ADD, 1964 (struct sockaddr *)&mltaddr, 1965 (struct sockaddr *)&ia->ia_addr, 1966 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING, 1967 NULL, ia->ia_ifp->if_index); 1968 if (error) 1969 goto unwind; 1970 } else 1971 rtfree(rt); 1972 1973 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 1974 if (!imm) { 1975 nd6log((LOG_WARNING, 1976 "%s: addmulti failed for %s on %s (errno=%d)\n", 1977 __func__, ip6_sprintf(&mltaddr.sin6_addr), 1978 if_name(ifp), error)); 1979 VERIFY(error != 0); 1980 goto unwind; 1981 } 1982 IFA_LOCK(ifa); 1983 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 1984 IFA_UNLOCK(ifa); 1985 } 1986#undef MLTMASK_LEN 1987 1988 /* 1989 * Make sure to initialize ND6 information. this is to workaround 1990 * issues with interfaces with IPv6 addresses, which have never brought 1991 * up. We are assuming that it is safe to nd6_ifattach multiple times. 1992 * NOTE: this is how stf0 gets initialized 1993 */ 1994 if ((error = nd6_ifattach(ifp)) != 0) 1995 goto unwind; 1996 1997 /* Ensure nd6_service() is scheduled as soon as it's convenient */ 1998 ++nd6_sched_timeout_want; 1999 2000 /* 2001 * Perform DAD, if needed. 2002 * XXX It may be of use, if we can administratively 2003 * disable DAD. 2004 */ 2005 IFA_LOCK_SPIN(ifa); 2006 if (in6if_do_dad(ifp) && ((ifa->ifa_flags & IN6_IFF_NODAD) == 0) && 2007 (ia->ia6_flags & IN6_IFF_DADPROGRESS)) { 2008 int mindelay, maxdelay; 2009 int *delayptr, delayval; 2010 2011 IFA_UNLOCK(ifa); 2012 delayptr = NULL; 2013 /* 2014 * Avoid the DAD delay if the caller wants us to skip it. 2015 * This is not compliant with RFC 2461, but it's only being 2016 * used for signalling and not for actual DAD. 2017 */ 2018 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY) && 2019 !(ia->ia6_flags & IN6_IFF_SWIFTDAD)) { 2020 /* 2021 * We need to impose a delay before sending an NS 2022 * for DAD. Check if we also needed a delay for the 2023 * corresponding MLD message. If we did, the delay 2024 * should be larger than the MLD delay (this could be 2025 * relaxed a bit, but this simple logic is at least 2026 * safe). 2027 */ 2028 mindelay = 0; 2029 if (in6m_sol != NULL) { 2030 IN6M_LOCK(in6m_sol); 2031 if (in6m_sol->in6m_state == 2032 MLD_REPORTING_MEMBER) 2033 mindelay = in6m_sol->in6m_timer; 2034 IN6M_UNLOCK(in6m_sol); 2035 } 2036 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 2037 if (maxdelay - mindelay == 0) 2038 delayval = 0; 2039 else { 2040 delayval = 2041 (random() % (maxdelay - mindelay)) + 2042 mindelay; 2043 } 2044 delayptr = &delayval; 2045 } 2046 2047 nd6_dad_start((struct ifaddr *)ia, delayptr); 2048 } else { 2049 IFA_UNLOCK(ifa); 2050 } 2051 2052 goto done; 2053 2054unwind: 2055 VERIFY(error != 0); 2056 in6_purgeaddr(&ia->ia_ifa); 2057 2058done: 2059 /* release reference held for this routine */ 2060 if (in6m_sol != NULL) 2061 IN6M_REMREF(in6m_sol); 2062 return (error); 2063} 2064 2065/* 2066 * Request an IPv6 interface address. If the address is new, then it will be 2067 * constructed and appended to the interface address chains. The interface 2068 * address structure is optionally returned with a reference for the caller. 2069 */ 2070int 2071in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int ifaupflags, 2072 struct in6_ifaddr **iar) 2073{ 2074 struct in6_addrlifetime ia6_lt; 2075 struct in6_ifaddr *ia; 2076 struct ifaddr *ifa; 2077 struct ifaddr *xifa; 2078 struct in6_addrlifetime *lt; 2079 uint64_t timenow; 2080 int plen, error; 2081 2082 /* Sanity check parameters and initialize locals */ 2083 VERIFY(ifp != NULL && ifra != NULL && iar != NULL); 2084 ia = NULL; 2085 ifa = NULL; 2086 error = 0; 2087 2088 /* 2089 * We always require users to specify a valid IPv6 address for 2090 * the corresponding operation. 2091 */ 2092 if (ifra->ifra_addr.sin6_family != AF_INET6 || 2093 ifra->ifra_addr.sin6_len != sizeof (struct sockaddr_in6)) { 2094 error = EAFNOSUPPORT; 2095 goto unwind; 2096 } 2097 2098 /* Validate ifra_prefixmask.sin6_len is properly bounded. */ 2099 if (ifra->ifra_prefixmask.sin6_len == 0 || 2100 ifra->ifra_prefixmask.sin6_len > sizeof (struct sockaddr_in6)) { 2101 error = EINVAL; 2102 goto unwind; 2103 } 2104 2105 /* Validate prefix length extracted from ifra_prefixmask structure. */ 2106 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 2107 (u_char *)&ifra->ifra_prefixmask + ifra->ifra_prefixmask.sin6_len); 2108 if (plen <= 0) { 2109 error = EINVAL; 2110 goto unwind; 2111 } 2112 2113 /* Validate lifetimes */ 2114 lt = &ifra->ifra_lifetime; 2115 if (lt->ia6t_pltime > lt->ia6t_vltime) { 2116 log(LOG_INFO, 2117 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__, 2118 lt->ia6t_pltime, lt->ia6t_vltime, 2119 ip6_sprintf(&ifra->ifra_addr.sin6_addr)); 2120 error = EINVAL; 2121 goto unwind; 2122 } 2123 if (lt->ia6t_vltime == 0) { 2124 /* 2125 * the following log might be noisy, but this is a typical 2126 * configuration mistake or a tool's bug. 2127 */ 2128 log(LOG_INFO, "%s: valid lifetime is 0 for %s\n", __func__, 2129 ip6_sprintf(&ifra->ifra_addr.sin6_addr)); 2130 } 2131 2132 /* 2133 * Before we lock the ifnet structure, we first check to see if the 2134 * address already exists. If so, then we don't allocate and link a 2135 * new one here. 2136 */ 2137 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr); 2138 if (ia != NULL) 2139 ifa = &ia->ia_ifa; 2140 2141 /* 2142 * Validate destination address on interface types that require it. 2143 */ 2144 if ((ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) != 0) { 2145 switch (ifra->ifra_dstaddr.sin6_family) { 2146 case AF_INET6: 2147 if (plen != 128) { 2148 /* noisy message for diagnostic purposes */ 2149 log(LOG_INFO, 2150 "%s: prefix length < 128 with" 2151 " explicit dstaddr.\n", __func__); 2152 error = EINVAL; 2153 goto unwind; 2154 } 2155 break; 2156 2157 case AF_UNSPEC: 2158 break; 2159 2160 default: 2161 error = EAFNOSUPPORT; 2162 goto unwind; 2163 } 2164 } else if (ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) { 2165 log(LOG_INFO, 2166 "%s: dstaddr valid only on p2p and loopback interfaces.\n", 2167 __func__); 2168 error = EINVAL; 2169 goto unwind; 2170 } 2171 2172 timenow = net_uptime(); 2173 2174 if (ia == NULL) { 2175 int how; 2176 2177 /* Is this the first new IPv6 address for the interface? */ 2178 ifaupflags |= IN6_IFAUPDATE_NEWADDR; 2179 2180 /* Allocate memory for IPv6 interface address structure. */ 2181 how = !(ifaupflags & IN6_IFAUPDATE_NOWAIT) ? M_WAITOK : 0; 2182 ia = in6_ifaddr_alloc(how); 2183 if (ia == NULL) { 2184 error = ENOBUFS; 2185 goto unwind; 2186 } 2187 2188 ifa = &ia->ia_ifa; 2189 2190 /* 2191 * Initialize interface address structure. 2192 * 2193 * Note well: none of these sockaddr_in6 structures contain a 2194 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field. 2195 * We still embed link-local scope identifiers at the end of an 2196 * arbitrary fe80::/32 prefix, for historical reasons. Also, the 2197 * ifa_dstaddr field is always non-NULL on point-to-point and 2198 * loopback interfaces, and conventionally points to a socket 2199 * address of AF_UNSPEC family when there is no destination. 2200 * 2201 * Please enjoy the dancing sea turtle. 2202 */ 2203 IFA_ADDREF(ifa); /* for this and optionally for caller */ 2204 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 2205 if (ifra->ifra_dstaddr.sin6_family == AF_INET6 || 2206 (ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) 2207 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 2208 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 2209 ifa->ifa_ifp = ifp; 2210 ifa->ifa_metric = ifp->if_metric; 2211 ifa->ifa_rtrequest = nd6_rtrequest; 2212 2213 LIST_INIT(&ia->ia6_memberships); 2214 ia->ia_addr.sin6_family = AF_INET6; 2215 ia->ia_addr.sin6_len = sizeof (ia->ia_addr); 2216 ia->ia_addr.sin6_addr = ifra->ifra_addr.sin6_addr; 2217 ia->ia_prefixmask.sin6_family = AF_INET6; 2218 ia->ia_prefixmask.sin6_len = sizeof (ia->ia_prefixmask); 2219 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr; 2220 error = in6_to_kamescope(&ia->ia_addr, ifp); 2221 if (error != 0) 2222 goto unwind; 2223 if (ifa->ifa_dstaddr != NULL) { 2224 ia->ia_dstaddr = ifra->ifra_dstaddr; 2225 error = in6_to_kamescope(&ia->ia_dstaddr, ifp); 2226 if (error != 0) 2227 goto unwind; 2228 } 2229 2230 /* Append to address chains */ 2231 ifnet_lock_exclusive(ifp); 2232 ifaupflags |= IN6_IFAUPDATE_1STADDR; 2233 TAILQ_FOREACH(xifa, &ifp->if_addrlist, ifa_list) { 2234 IFA_LOCK_SPIN(xifa); 2235 if (xifa->ifa_addr->sa_family != AF_INET6) { 2236 IFA_UNLOCK(xifa); 2237 ifaupflags &= ~IN6_IFAUPDATE_1STADDR; 2238 break; 2239 } 2240 IFA_UNLOCK(xifa); 2241 } 2242 2243 IFA_LOCK_SPIN(ifa); 2244 if_attach_ifa(ifp, ifa); /* holds reference for ifnet link */ 2245 IFA_UNLOCK(ifa); 2246 ifnet_lock_done(ifp); 2247 2248 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 2249 if (in6_ifaddrs != NULL) { 2250 struct in6_ifaddr *iac; 2251 for (iac = in6_ifaddrs; iac->ia_next != NULL; 2252 iac = iac->ia_next) 2253 continue; 2254 iac->ia_next = ia; 2255 } else { 2256 in6_ifaddrs = ia; 2257 } 2258 IFA_ADDREF(ifa); /* hold for in6_ifaddrs link */ 2259 lck_rw_done(&in6_ifaddr_rwlock); 2260 } else { 2261 ifa = &ia->ia_ifa; 2262 ifaupflags &= ~(IN6_IFAUPDATE_NEWADDR|IN6_IFAUPDATE_1STADDR); 2263 } 2264 2265 VERIFY(ia != NULL && ifa == &ia->ia_ifa); 2266 IFA_LOCK(ifa); 2267 2268 /* 2269 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 2270 * to see if the address is deprecated or invalidated, but initialize 2271 * these members for applications. 2272 */ 2273 ia->ia6_updatetime = ia->ia6_createtime = timenow; 2274 ia6_lt = *lt; 2275 if (ia6_lt.ia6t_vltime != ND6_INFINITE_LIFETIME) 2276 ia6_lt.ia6t_expire = timenow + ia6_lt.ia6t_vltime; 2277 else 2278 ia6_lt.ia6t_expire = 0; 2279 if (ia6_lt.ia6t_pltime != ND6_INFINITE_LIFETIME) 2280 ia6_lt.ia6t_preferred = timenow + ia6_lt.ia6t_pltime; 2281 else 2282 ia6_lt.ia6t_preferred = 0; 2283 in6ifa_setlifetime(ia, &ia6_lt); 2284 2285 /* 2286 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the 2287 * userland, make it deprecated. 2288 */ 2289 if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { 2290 ia->ia6_lifetime.ia6ti_pltime = 0; 2291 ia->ia6_lifetime.ia6ti_preferred = timenow; 2292 } 2293 2294 /* 2295 * Do not delay sending neighbor solicitations when using optimistic 2296 * duplicate address detection, c.f. RFC 4429. 2297 */ 2298 if ((ia->ia6_flags & IN6_IFF_OPTIMISTIC) == 0) 2299 ifaupflags |= IN6_IFAUPDATE_DADDELAY; 2300 2301 /* 2302 * Update flag or prefix length 2303 */ 2304 ia->ia_plen = plen; 2305 ia->ia6_flags = ifra->ifra_flags; 2306 2307 /* Release locks (new address available to concurrent tasks) */ 2308 IFA_UNLOCK(ifa); 2309 2310 /* Further initialization of the interface address */ 2311 error = in6_ifinit(ifp, ia, ifaupflags); 2312 if (error != 0) 2313 goto unwind; 2314 2315 /* Finish updating the address while other tasks are working with it */ 2316 error = in6_ifaupdate_aux(ia, ifp, ifaupflags); 2317 if (error != 0) 2318 goto unwind; 2319 2320 /* Return success (optionally w/ address for caller). */ 2321 VERIFY(error == 0); 2322 (void) ifnet_notify_address(ifp, AF_INET6); 2323 goto done; 2324 2325unwind: 2326 VERIFY(error != 0); 2327 if (ia != NULL) { 2328 VERIFY(ifa == &ia->ia_ifa); 2329 IFA_REMREF(ifa); 2330 ia = NULL; 2331 } 2332 2333done: 2334 *iar = ia; 2335 return (error); 2336} 2337 2338void 2339in6_purgeaddr(struct ifaddr *ifa) 2340{ 2341 struct ifnet *ifp = ifa->ifa_ifp; 2342 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa; 2343 struct in6_multi_mship *imm; 2344 2345 lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED); 2346 2347 /* stop DAD processing */ 2348 nd6_dad_stop(ifa); 2349 2350 /* 2351 * delete route to the destination of the address being purged. 2352 * The interface must be p2p or loopback in this case. 2353 */ 2354 IFA_LOCK(ifa); 2355 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_plen == 128) { 2356 int error, rtf; 2357 2358 IFA_UNLOCK(ifa); 2359 rtf = (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0; 2360 error = rtinit(&(ia->ia_ifa), RTM_DELETE, rtf); 2361 if (error != 0) { 2362 log(LOG_ERR, "in6_purgeaddr: failed to remove " 2363 "a route to the p2p destination: %s on %s, " 2364 "errno=%d\n", 2365 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp), 2366 error); 2367 /* proceed anyway... */ 2368 } 2369 IFA_LOCK_SPIN(ifa); 2370 ia->ia_flags &= ~IFA_ROUTE; 2371 } 2372 IFA_UNLOCK(ifa); 2373 2374 /* Remove ownaddr's loopback rtentry, if it exists. */ 2375 in6_ifremloop(&(ia->ia_ifa)); 2376 2377 /* 2378 * leave from multicast groups we have joined for the interface 2379 */ 2380 IFA_LOCK(ifa); 2381 while ((imm = ia->ia6_memberships.lh_first) != NULL) { 2382 LIST_REMOVE(imm, i6mm_chain); 2383 IFA_UNLOCK(ifa); 2384 in6_leavegroup(imm); 2385 IFA_LOCK(ifa); 2386 } 2387 IFA_UNLOCK(ifa); 2388 2389 /* in6_unlink_ifa() will need exclusive access */ 2390 in6_unlink_ifa(ia, ifp); 2391 in6_post_msg(ifp, KEV_INET6_ADDR_DELETED, ia, NULL); 2392 2393 (void) ifnet_notify_address(ifp, AF_INET6); 2394} 2395 2396static void 2397in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 2398{ 2399 struct in6_ifaddr *oia; 2400 struct ifaddr *ifa; 2401 int unlinked; 2402 2403 lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED); 2404 2405 ifa = &ia->ia_ifa; 2406 IFA_ADDREF(ifa); 2407 2408 ifnet_lock_exclusive(ifp); 2409 IFA_LOCK(ifa); 2410 if (ifa->ifa_debug & IFD_ATTACHED) 2411 if_detach_ifa(ifp, ifa); 2412 IFA_UNLOCK(ifa); 2413 ifnet_lock_done(ifp); 2414 2415 unlinked = 1; 2416 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 2417 oia = ia; 2418 if (oia == (ia = in6_ifaddrs)) { 2419 in6_ifaddrs = ia->ia_next; 2420 } else { 2421 while (ia->ia_next && (ia->ia_next != oia)) 2422 ia = ia->ia_next; 2423 if (ia->ia_next) { 2424 ia->ia_next = oia->ia_next; 2425 } else { 2426 /* search failed */ 2427 log(LOG_NOTICE, "%s: search failed.\n", __func__); 2428 unlinked = 0; 2429 } 2430 } 2431 2432 /* 2433 * When an autoconfigured address is being removed, release the 2434 * reference to the base prefix. Also, since the release might 2435 * affect the status of other (detached) addresses, call 2436 * pfxlist_onlink_check(). 2437 */ 2438 ifa = &oia->ia_ifa; 2439 IFA_LOCK(ifa); 2440 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) { 2441 if (oia->ia6_ndpr == NULL) { 2442 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address " 2443 "0x%llx has no prefix\n", 2444 (uint64_t)VM_KERNEL_ADDRPERM(oia)); 2445 } else { 2446 struct nd_prefix *pr = oia->ia6_ndpr; 2447 2448 oia->ia6_flags &= ~IN6_IFF_AUTOCONF; 2449 oia->ia6_ndpr = NULL; 2450 NDPR_LOCK(pr); 2451 VERIFY(pr->ndpr_addrcnt != 0); 2452 pr->ndpr_addrcnt--; 2453 NDPR_UNLOCK(pr); 2454 NDPR_REMREF(pr); /* release addr reference */ 2455 } 2456 IFA_UNLOCK(ifa); 2457 lck_rw_done(&in6_ifaddr_rwlock); 2458 lck_mtx_lock(nd6_mutex); 2459 pfxlist_onlink_check(); 2460 lck_mtx_unlock(nd6_mutex); 2461 } else { 2462 IFA_UNLOCK(ifa); 2463 lck_rw_done(&in6_ifaddr_rwlock); 2464 } 2465 2466 /* 2467 * release another refcnt for the link from in6_ifaddrs. 2468 * Do this only if it's not already unlinked in the event that we lost 2469 * the race, since in6_ifaddr_rwlock was momentarily dropped above. 2470 */ 2471 if (unlinked) 2472 IFA_REMREF(ifa); 2473 2474 /* release reference held for this routine */ 2475 IFA_REMREF(ifa); 2476 2477 /* invalidate route caches */ 2478 routegenid_inet6_update(); 2479} 2480 2481void 2482in6_purgeif(struct ifnet *ifp) 2483{ 2484 struct in6_ifaddr *ia; 2485 2486 if (ifp == NULL) 2487 return; 2488 2489 lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED); 2490 2491 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 2492 ia = in6_ifaddrs; 2493 while (ia != NULL) { 2494 if (ia->ia_ifa.ifa_ifp != ifp) { 2495 ia = ia->ia_next; 2496 continue; 2497 } 2498 IFA_ADDREF(&ia->ia_ifa); /* for us */ 2499 lck_rw_done(&in6_ifaddr_rwlock); 2500 in6_purgeaddr(&ia->ia_ifa); 2501 IFA_REMREF(&ia->ia_ifa); /* for us */ 2502 lck_rw_lock_exclusive(&in6_ifaddr_rwlock); 2503 /* 2504 * Purging the address would have caused 2505 * in6_ifaddr_rwlock to be dropped and reacquired; 2506 * therefore search again from the beginning 2507 * of in6_ifaddrs list. 2508 */ 2509 ia = in6_ifaddrs; 2510 } 2511 lck_rw_done(&in6_ifaddr_rwlock); 2512 2513 in6_ifdetach(ifp); 2514} 2515 2516/* 2517 * Initialize an interface's internet6 address and routing table entry. 2518 */ 2519static int 2520in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, int ifaupflags) 2521{ 2522 int error; 2523 struct ifaddr *ifa; 2524 2525 error = 0; 2526 ifa = &ia->ia_ifa; 2527 2528 /* 2529 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter, 2530 * but here we are sending it down to the interface with a pointer 2531 * to struct ifaddr, for legacy reasons. 2532 */ 2533 if ((ifaupflags & IN6_IFAUPDATE_1STADDR) != 0) { 2534 error = ifnet_ioctl(ifp, PF_INET6, SIOCSIFADDR, ia); 2535 if (error != 0) { 2536 if (error != EOPNOTSUPP) 2537 return (error); 2538 error = 0; 2539 } 2540 } 2541 2542 IFA_LOCK(ifa); 2543 2544 /* 2545 * Special case: 2546 * If the destination address is specified for a point-to-point 2547 * interface, install a route to the destination as an interface 2548 * direct route. 2549 */ 2550 if (!(ia->ia_flags & IFA_ROUTE) && ia->ia_plen == 128 && 2551 ia->ia_dstaddr.sin6_family == AF_INET6) { 2552 IFA_UNLOCK(ifa); 2553 error = rtinit(ifa, RTM_ADD, RTF_UP | RTF_HOST); 2554 if (error != 0) 2555 return (error); 2556 IFA_LOCK(ifa); 2557 ia->ia_flags |= IFA_ROUTE; 2558 } 2559 IFA_LOCK_ASSERT_HELD(ifa); 2560 if (ia->ia_plen < 128) { 2561 /* 2562 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto(). 2563 */ 2564 ia->ia_flags |= RTF_CLONING; 2565 } 2566 2567 IFA_UNLOCK(ifa); 2568 2569 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 2570 if ((ifaupflags & IN6_IFAUPDATE_NEWADDR) != 0) 2571 in6_ifaddloop(ifa); 2572 2573 /* invalidate route caches */ 2574 routegenid_inet6_update(); 2575 2576 VERIFY(error == 0); 2577 return (0); 2578} 2579 2580void 2581in6_purgeaddrs(struct ifnet *ifp) 2582{ 2583 in6_purgeif(ifp); 2584} 2585 2586/* 2587 * Find an IPv6 interface link-local address specific to an interface. 2588 */ 2589struct in6_ifaddr * 2590in6ifa_ifpforlinklocal(ifp, ignoreflags) 2591 struct ifnet *ifp; 2592 int ignoreflags; 2593{ 2594 struct ifaddr *ifa; 2595 2596 ifnet_lock_shared(ifp); 2597 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 2598 { 2599 IFA_LOCK_SPIN(ifa); 2600 if (ifa->ifa_addr->sa_family != AF_INET6) { 2601 IFA_UNLOCK(ifa); 2602 continue; 2603 } 2604 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 2605 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 2606 ignoreflags) != 0) { 2607 IFA_UNLOCK(ifa); 2608 continue; 2609 } 2610 IFA_ADDREF_LOCKED(ifa); /* for caller */ 2611 IFA_UNLOCK(ifa); 2612 break; 2613 } 2614 IFA_UNLOCK(ifa); 2615 } 2616 ifnet_lock_done(ifp); 2617 2618 return ((struct in6_ifaddr *)ifa); 2619} 2620 2621/* 2622 * find the internet address corresponding to a given interface and address. 2623 */ 2624struct in6_ifaddr * 2625in6ifa_ifpwithaddr(ifp, addr) 2626 struct ifnet *ifp; 2627 struct in6_addr *addr; 2628{ 2629 struct ifaddr *ifa; 2630 2631 ifnet_lock_shared(ifp); 2632 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) 2633 { 2634 IFA_LOCK_SPIN(ifa); 2635 if (ifa->ifa_addr->sa_family != AF_INET6) { 2636 IFA_UNLOCK(ifa); 2637 continue; 2638 } 2639 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 2640 IFA_ADDREF_LOCKED(ifa); /* for caller */ 2641 IFA_UNLOCK(ifa); 2642 break; 2643 } 2644 IFA_UNLOCK(ifa); 2645 } 2646 ifnet_lock_done(ifp); 2647 2648 return ((struct in6_ifaddr *)ifa); 2649} 2650 2651struct in6_ifaddr * 2652in6ifa_prproxyaddr(struct in6_addr *addr) 2653{ 2654 struct in6_ifaddr *ia; 2655 2656 lck_rw_lock_shared(&in6_ifaddr_rwlock); 2657 for (ia = in6_ifaddrs; ia; ia = ia->ia_next) { 2658 IFA_LOCK(&ia->ia_ifa); 2659 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(&ia->ia_ifa))) { 2660 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for caller */ 2661 IFA_UNLOCK(&ia->ia_ifa); 2662 break; 2663 } 2664 IFA_UNLOCK(&ia->ia_ifa); 2665 } 2666 lck_rw_done(&in6_ifaddr_rwlock); 2667 2668 if (ia != NULL && !nd6_prproxy_ifaddr(ia)) { 2669 IFA_REMREF(&ia->ia_ifa); 2670 ia = NULL; 2671 } 2672 2673 return (ia); 2674} 2675 2676void 2677in6ifa_getlifetime(struct in6_ifaddr *ia6, struct in6_addrlifetime *t_dst, 2678 int iscalendar) 2679{ 2680 struct in6_addrlifetime_i *t_src = &ia6->ia6_lifetime; 2681 struct timeval caltime; 2682 2683 t_dst->ia6t_vltime = t_src->ia6ti_vltime; 2684 t_dst->ia6t_pltime = t_src->ia6ti_pltime; 2685 t_dst->ia6t_expire = 0; 2686 t_dst->ia6t_preferred = 0; 2687 2688 /* account for system time change */ 2689 getmicrotime(&caltime); 2690 t_src->ia6ti_base_calendartime += 2691 NET_CALCULATE_CLOCKSKEW(caltime, 2692 t_src->ia6ti_base_calendartime, net_uptime(), 2693 t_src->ia6ti_base_uptime); 2694 2695 if (iscalendar) { 2696 if (t_src->ia6ti_expire != 0 && 2697 t_src->ia6ti_vltime != ND6_INFINITE_LIFETIME) 2698 t_dst->ia6t_expire = t_src->ia6ti_base_calendartime + 2699 t_src->ia6ti_expire - t_src->ia6ti_base_uptime; 2700 2701 if (t_src->ia6ti_preferred != 0 && 2702 t_src->ia6ti_pltime != ND6_INFINITE_LIFETIME) 2703 t_dst->ia6t_preferred = t_src->ia6ti_base_calendartime + 2704 t_src->ia6ti_preferred - t_src->ia6ti_base_uptime; 2705 } else { 2706 if (t_src->ia6ti_expire != 0 && 2707 t_src->ia6ti_vltime != ND6_INFINITE_LIFETIME) 2708 t_dst->ia6t_expire = t_src->ia6ti_expire; 2709 2710 if (t_src->ia6ti_preferred != 0 && 2711 t_src->ia6ti_pltime != ND6_INFINITE_LIFETIME) 2712 t_dst->ia6t_preferred = t_src->ia6ti_preferred; 2713 } 2714} 2715 2716void 2717in6ifa_setlifetime(struct in6_ifaddr *ia6, struct in6_addrlifetime *t_src) 2718{ 2719 struct in6_addrlifetime_i *t_dst = &ia6->ia6_lifetime; 2720 struct timeval caltime; 2721 2722 /* account for system time change */ 2723 getmicrotime(&caltime); 2724 t_dst->ia6ti_base_calendartime += 2725 NET_CALCULATE_CLOCKSKEW(caltime, 2726 t_dst->ia6ti_base_calendartime, net_uptime(), 2727 t_dst->ia6ti_base_uptime); 2728 2729 /* trust the caller for the values */ 2730 t_dst->ia6ti_expire = t_src->ia6t_expire; 2731 t_dst->ia6ti_preferred = t_src->ia6t_preferred; 2732 t_dst->ia6ti_vltime = t_src->ia6t_vltime; 2733 t_dst->ia6ti_pltime = t_src->ia6t_pltime; 2734} 2735 2736/* 2737 * Convert IP6 address to printable (loggable) representation. 2738 */ 2739char * 2740ip6_sprintf(const struct in6_addr *addr) 2741{ 2742 static const char digits[] = "0123456789abcdef"; 2743 static int ip6round = 0; 2744 static char ip6buf[8][48]; 2745 2746 int i; 2747 char *cp; 2748 const u_short *a = (const u_short *)addr; 2749 const u_char *d; 2750 u_char n; 2751 int dcolon = 0; 2752 int zpad = 0; 2753 2754 ip6round = (ip6round + 1) & 7; 2755 cp = ip6buf[ip6round]; 2756 2757 for (i = 0; i < 8; i++) { 2758 if (dcolon == 1) { 2759 if (*a == 0) { 2760 if (i == 7) 2761 *cp++ = ':'; 2762 a++; 2763 continue; 2764 } else 2765 dcolon = 2; 2766 } 2767 if (*a == 0) { 2768 if (dcolon == 0 && *(a + 1) == 0) { 2769 if (i == 0) 2770 *cp++ = ':'; 2771 *cp++ = ':'; 2772 dcolon = 1; 2773 } else { 2774 *cp++ = '0'; 2775 *cp++ = ':'; 2776 } 2777 a++; 2778 continue; 2779 } 2780 d = (const u_char *)a; 2781 zpad = 0; 2782 if ((n = *d >> 4) != 0) { 2783 *cp++ = digits[n]; 2784 zpad = 1; 2785 } 2786 if ((n = *d++ & 0xf) != 0 || zpad) { 2787 *cp++ = digits[n]; 2788 zpad = 1; 2789 } 2790 if ((n = *d >> 4) != 0 || zpad) { 2791 *cp++ = digits[n]; 2792 zpad = 1; 2793 } 2794 if ((n = *d & 0xf) != 0 || zpad) 2795 *cp++ = digits[n]; 2796 *cp++ = ':'; 2797 a++; 2798 } 2799 *--cp = 0; 2800 return (ip6buf[ip6round]); 2801} 2802 2803int 2804in6addr_local(struct in6_addr *in6) 2805{ 2806 struct rtentry *rt; 2807 struct sockaddr_in6 sin6; 2808 int local = 0; 2809 2810 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_SCOPE_LINKLOCAL(in6)) 2811 return (1); 2812 2813 sin6.sin6_family = AF_INET6; 2814 sin6.sin6_len = sizeof (sin6); 2815 bcopy(in6, &sin6.sin6_addr, sizeof (*in6)); 2816 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0); 2817 2818 if (rt != NULL) { 2819 RT_LOCK_SPIN(rt); 2820 if (rt->rt_gateway->sa_family == AF_LINK) 2821 local = 1; 2822 RT_UNLOCK(rt); 2823 rtfree(rt); 2824 } else { 2825 local = in6_localaddr(in6); 2826 } 2827 return (local); 2828} 2829 2830int 2831in6_localaddr(struct in6_addr *in6) 2832{ 2833 struct in6_ifaddr *ia; 2834 2835 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 2836 return (1); 2837 2838 lck_rw_lock_shared(&in6_ifaddr_rwlock); 2839 for (ia = in6_ifaddrs; ia; ia = ia->ia_next) { 2840 IFA_LOCK_SPIN(&ia->ia_ifa); 2841 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 2842 &ia->ia_prefixmask.sin6_addr)) { 2843 IFA_UNLOCK(&ia->ia_ifa); 2844 lck_rw_done(&in6_ifaddr_rwlock); 2845 return (1); 2846 } 2847 IFA_UNLOCK(&ia->ia_ifa); 2848 } 2849 lck_rw_done(&in6_ifaddr_rwlock); 2850 return (0); 2851} 2852 2853int 2854in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 2855{ 2856 struct in6_ifaddr *ia; 2857 2858 lck_rw_lock_shared(&in6_ifaddr_rwlock); 2859 for (ia = in6_ifaddrs; ia; ia = ia->ia_next) { 2860 IFA_LOCK_SPIN(&ia->ia_ifa); 2861 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 2862 &sa6->sin6_addr) && 2863 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { 2864 IFA_UNLOCK(&ia->ia_ifa); 2865 lck_rw_done(&in6_ifaddr_rwlock); 2866 return (1); /* true */ 2867 } 2868 /* XXX: do we still have to go thru the rest of the list? */ 2869 IFA_UNLOCK(&ia->ia_ifa); 2870 } 2871 2872 lck_rw_done(&in6_ifaddr_rwlock); 2873 return (0); /* false */ 2874} 2875 2876/* 2877 * return length of part which dst and src are equal 2878 * hard coding... 2879 */ 2880int 2881in6_matchlen(src, dst) 2882struct in6_addr *src, *dst; 2883{ 2884 int match = 0; 2885 u_char *s = (u_char *)src, *d = (u_char *)dst; 2886 u_char *lim = s + 16, r; 2887 2888 while (s < lim) 2889 if ((r = (*d++ ^ *s++)) != 0) { 2890 while (r < 128) { 2891 match++; 2892 r <<= 1; 2893 } 2894 break; 2895 } else 2896 match += 8; 2897 return (match); 2898} 2899 2900/* XXX: to be scope conscious */ 2901int 2902in6_are_prefix_equal(p1, p2, len) 2903 struct in6_addr *p1, *p2; 2904 int len; 2905{ 2906 int bytelen, bitlen; 2907 2908 /* sanity check */ 2909 if (0 > len || len > 128) { 2910 log(LOG_ERR, "%s: invalid prefix length(%d)\n", __func__, len); 2911 return (0); 2912 } 2913 2914 bytelen = len / 8; 2915 bitlen = len % 8; 2916 2917 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2918 return (0); 2919 if (bitlen != 0 && 2920 p1->s6_addr[bytelen] >> (8 - bitlen) != 2921 p2->s6_addr[bytelen] >> (8 - bitlen)) 2922 return (0); 2923 2924 return (1); 2925} 2926 2927void 2928in6_prefixlen2mask(maskp, len) 2929 struct in6_addr *maskp; 2930 int len; 2931{ 2932 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 2933 int bytelen, bitlen, i; 2934 2935 /* sanity check */ 2936 if (0 > len || len > 128) { 2937 log(LOG_ERR, "%s: invalid prefix length(%d)\n", __func__, len); 2938 return; 2939 } 2940 2941 bzero(maskp, sizeof (*maskp)); 2942 bytelen = len / 8; 2943 bitlen = len % 8; 2944 for (i = 0; i < bytelen; i++) 2945 maskp->s6_addr[i] = 0xff; 2946 if (bitlen) 2947 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 2948} 2949 2950/* 2951 * return the best address out of the same scope 2952 */ 2953struct in6_ifaddr * 2954in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst) 2955{ 2956 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0; 2957 int blen = -1; 2958 struct ifaddr *ifa; 2959 struct ifnet *ifp; 2960 struct in6_ifaddr *ifa_best = NULL; 2961 2962 if (oifp == NULL) { 2963 return (NULL); 2964 } 2965 2966 /* 2967 * We search for all addresses on all interfaces from the beginning. 2968 * Comparing an interface with the outgoing interface will be done 2969 * only at the final stage of tiebreaking. 2970 */ 2971 ifnet_head_lock_shared(); 2972 TAILQ_FOREACH(ifp, &ifnet_head, if_list) { 2973 /* 2974 * We can never take an address that breaks the scope zone 2975 * of the destination. 2976 */ 2977 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst)) 2978 continue; 2979 2980 ifnet_lock_shared(ifp); 2981 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 2982 int tlen = -1, dscopecmp, bscopecmp, matchcmp; 2983 2984 IFA_LOCK(ifa); 2985 if (ifa->ifa_addr->sa_family != AF_INET6) { 2986 IFA_UNLOCK(ifa); 2987 continue; 2988 } 2989 src_scope = in6_addrscope(IFA_IN6(ifa)); 2990 2991 /* 2992 * Don't use an address before completing DAD 2993 * nor a duplicated address. 2994 */ 2995 if (((struct in6_ifaddr *)ifa)->ia6_flags & 2996 IN6_IFF_NOTREADY) { 2997 IFA_UNLOCK(ifa); 2998 continue; 2999 } 3000 /* XXX: is there any case to allow anycasts? */ 3001 if (((struct in6_ifaddr *)ifa)->ia6_flags & 3002 IN6_IFF_ANYCAST) { 3003 IFA_UNLOCK(ifa); 3004 continue; 3005 } 3006 if (((struct in6_ifaddr *)ifa)->ia6_flags & 3007 IN6_IFF_DETACHED) { 3008 IFA_UNLOCK(ifa); 3009 continue; 3010 } 3011 /* 3012 * If this is the first address we find, 3013 * keep it anyway. 3014 */ 3015 if (ifa_best == NULL) 3016 goto replace; 3017 3018 /* 3019 * ifa_best is never NULL beyond this line except 3020 * within the block labeled "replace". 3021 */ 3022 3023 /* 3024 * If ifa_best has a smaller scope than dst and 3025 * the current address has a larger one than 3026 * (or equal to) dst, always replace ifa_best. 3027 * Also, if the current address has a smaller scope 3028 * than dst, ignore it unless ifa_best also has a 3029 * smaller scope. 3030 * Consequently, after the two if-clause below, 3031 * the followings must be satisfied: 3032 * (scope(src) < scope(dst) && 3033 * scope(best) < scope(dst)) 3034 * OR 3035 * (scope(best) >= scope(dst) && 3036 * scope(src) >= scope(dst)) 3037 */ 3038 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 && 3039 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0) 3040 goto replace; /* (A) */ 3041 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 && 3042 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) { 3043 IFA_UNLOCK(ifa); 3044 continue; /* (B) */ 3045 } 3046 /* 3047 * A deprecated address SHOULD NOT be used in new 3048 * communications if an alternate (non-deprecated) 3049 * address is available and has sufficient scope. 3050 * RFC 4862, Section 5.5.4. 3051 */ 3052 if (((struct in6_ifaddr *)ifa)->ia6_flags & 3053 IN6_IFF_DEPRECATED) { 3054 /* 3055 * Ignore any deprecated addresses if 3056 * specified by configuration. 3057 */ 3058 if (!ip6_use_deprecated) { 3059 IFA_UNLOCK(ifa); 3060 continue; 3061 } 3062 /* 3063 * If we have already found a non-deprecated 3064 * candidate, just ignore deprecated addresses. 3065 */ 3066 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) 3067 == 0) { 3068 IFA_UNLOCK(ifa); 3069 continue; 3070 } 3071 } 3072 3073 /* 3074 * A non-deprecated address is always preferred 3075 * to a deprecated one regardless of scopes and 3076 * address matching (Note invariants ensured by the 3077 * conditions (A) and (B) above.) 3078 */ 3079 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) && 3080 (((struct in6_ifaddr *)ifa)->ia6_flags & 3081 IN6_IFF_DEPRECATED) == 0) 3082 goto replace; 3083 3084 /* 3085 * When we use temporary addresses described in 3086 * RFC 4941, we prefer temporary addresses to 3087 * public autoconf addresses. Again, note the 3088 * invariants from (A) and (B). Also note that we 3089 * don't have any preference between static addresses 3090 * and autoconf addresses (despite of whether or not 3091 * the latter is temporary or public.) 3092 */ 3093 if (ip6_use_tempaddr) { 3094 struct in6_ifaddr *ifat; 3095 3096 ifat = (struct in6_ifaddr *)ifa; 3097 if ((ifa_best->ia6_flags & 3098 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 3099 == IN6_IFF_AUTOCONF && 3100 (ifat->ia6_flags & 3101 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 3102 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) { 3103 goto replace; 3104 } 3105 if ((ifa_best->ia6_flags & 3106 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 3107 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) && 3108 (ifat->ia6_flags & 3109 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) 3110 == IN6_IFF_AUTOCONF) { 3111 IFA_UNLOCK(ifa); 3112 continue; 3113 } 3114 } 3115 3116 /* 3117 * At this point, we have two cases: 3118 * 1. we are looking at a non-deprecated address, 3119 * and ifa_best is also non-deprecated. 3120 * 2. we are looking at a deprecated address, 3121 * and ifa_best is also deprecated. 3122 * Also, we do not have to consider a case where 3123 * the scope of if_best is larger(smaller) than dst and 3124 * the scope of the current address is smaller(larger) 3125 * than dst. Such a case has already been covered. 3126 * Tiebreaking is done according to the following 3127 * items: 3128 * - the scope comparison between the address and 3129 * dst (dscopecmp) 3130 * - the scope comparison between the address and 3131 * ifa_best (bscopecmp) 3132 * - if the address match dst longer than ifa_best 3133 * (matchcmp) 3134 * - if the address is on the outgoing I/F (outI/F) 3135 * 3136 * Roughly speaking, the selection policy is 3137 * - the most important item is scope. The same scope 3138 * is best. Then search for a larger scope. 3139 * Smaller scopes are the last resort. 3140 * - A deprecated address is chosen only when we have 3141 * no address that has an enough scope, but is 3142 * prefered to any addresses of smaller scopes 3143 * (this must be already done above.) 3144 * - addresses on the outgoing I/F are preferred to 3145 * ones on other interfaces if none of above 3146 * tiebreaks. In the table below, the column "bI" 3147 * means if the best_ifa is on the outgoing 3148 * interface, and the column "sI" means if the ifa 3149 * is on the outgoing interface. 3150 * - If there is no other reasons to choose one, 3151 * longest address match against dst is considered. 3152 * 3153 * The precise decision table is as follows: 3154 * dscopecmp bscopecmp match bI oI | replace? 3155 * N/A equal N/A Y N | No (1) 3156 * N/A equal N/A N Y | Yes (2) 3157 * N/A equal larger N/A | Yes (3) 3158 * N/A equal !larger N/A | No (4) 3159 * larger larger N/A N/A | No (5) 3160 * larger smaller N/A N/A | Yes (6) 3161 * smaller larger N/A N/A | Yes (7) 3162 * smaller smaller N/A N/A | No (8) 3163 * equal smaller N/A N/A | Yes (9) 3164 * equal larger (already done at A above) 3165 */ 3166 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope); 3167 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope); 3168 3169 if (bscopecmp == 0) { 3170 struct ifnet *bifp = ifa_best->ia_ifp; 3171 3172 if (bifp == oifp && ifp != oifp) { /* (1) */ 3173 IFA_UNLOCK(ifa); 3174 continue; 3175 } 3176 if (bifp != oifp && ifp == oifp) /* (2) */ 3177 goto replace; 3178 3179 /* 3180 * Both bifp and ifp are on the outgoing 3181 * interface, or both two are on a different 3182 * interface from the outgoing I/F. 3183 * now we need address matching against dst 3184 * for tiebreaking. 3185 */ 3186 tlen = in6_matchlen(IFA_IN6(ifa), dst); 3187 matchcmp = tlen - blen; 3188 if (matchcmp > 0) /* (3) */ 3189 goto replace; 3190 IFA_UNLOCK(ifa); 3191 continue; /* (4) */ 3192 } 3193 if (dscopecmp > 0) { 3194 if (bscopecmp > 0) { /* (5) */ 3195 IFA_UNLOCK(ifa); 3196 continue; 3197 } 3198 goto replace; /* (6) */ 3199 } 3200 if (dscopecmp < 0) { 3201 if (bscopecmp > 0) /* (7) */ 3202 goto replace; 3203 IFA_UNLOCK(ifa); 3204 continue; /* (8) */ 3205 } 3206 3207 /* now dscopecmp must be 0 */ 3208 if (bscopecmp < 0) 3209 goto replace; /* (9) */ 3210 3211replace: 3212 IFA_ADDREF_LOCKED(ifa); /* for ifa_best */ 3213 blen = tlen >= 0 ? tlen : 3214 in6_matchlen(IFA_IN6(ifa), dst); 3215 best_scope = 3216 in6_addrscope(&ifa2ia6(ifa)->ia_addr.sin6_addr); 3217 IFA_UNLOCK(ifa); 3218 if (ifa_best) 3219 IFA_REMREF(&ifa_best->ia_ifa); 3220 ifa_best = (struct in6_ifaddr *)ifa; 3221 } 3222 ifnet_lock_done(ifp); 3223 } 3224 ifnet_head_done(); 3225 3226 /* count statistics for future improvements */ 3227 if (ifa_best == NULL) 3228 ip6stat.ip6s_sources_none++; 3229 else { 3230 IFA_LOCK_SPIN(&ifa_best->ia_ifa); 3231 if (oifp == ifa_best->ia_ifp) 3232 ip6stat.ip6s_sources_sameif[best_scope]++; 3233 else 3234 ip6stat.ip6s_sources_otherif[best_scope]++; 3235 3236 if (best_scope == dst_scope) 3237 ip6stat.ip6s_sources_samescope[best_scope]++; 3238 else 3239 ip6stat.ip6s_sources_otherscope[best_scope]++; 3240 3241 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0) 3242 ip6stat.ip6s_sources_deprecated[best_scope]++; 3243 IFA_UNLOCK(&ifa_best->ia_ifa); 3244 } 3245 3246 return (ifa_best); 3247} 3248 3249/* 3250 * return the best address out of the same scope. if no address was 3251 * found, return the first valid address from designated IF. 3252 */ 3253struct in6_ifaddr * 3254in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 3255{ 3256 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 3257 struct ifaddr *ifa; 3258 struct in6_ifaddr *besta = NULL; 3259 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 3260 3261 dep[0] = dep[1] = NULL; 3262 3263 /* 3264 * We first look for addresses in the same scope. 3265 * If there is one, return it. 3266 * If two or more, return one which matches the dst longest. 3267 * If none, return one of global addresses assigned other ifs. 3268 */ 3269 ifnet_lock_shared(ifp); 3270 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 3271 IFA_LOCK(ifa); 3272 if (ifa->ifa_addr->sa_family != AF_INET6) { 3273 IFA_UNLOCK(ifa); 3274 continue; 3275 } 3276 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_ANYCAST) { 3277 IFA_UNLOCK(ifa); 3278 continue; /* XXX: is there any case to allow anycast? */ 3279 } 3280 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_NOTREADY) { 3281 IFA_UNLOCK(ifa); 3282 continue; /* don't use this interface */ 3283 } 3284 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DETACHED) { 3285 IFA_UNLOCK(ifa); 3286 continue; 3287 } 3288 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 3289 if (ip6_use_deprecated) { 3290 IFA_ADDREF_LOCKED(ifa); /* for dep[0] */ 3291 IFA_UNLOCK(ifa); 3292 if (dep[0] != NULL) 3293 IFA_REMREF(&dep[0]->ia_ifa); 3294 dep[0] = (struct in6_ifaddr *)ifa; 3295 } else { 3296 IFA_UNLOCK(ifa); 3297 } 3298 continue; 3299 } 3300 3301 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 3302 /* 3303 * call in6_matchlen() as few as possible 3304 */ 3305 if (besta) { 3306 if (blen == -1) { 3307 IFA_UNLOCK(ifa); 3308 IFA_LOCK(&besta->ia_ifa); 3309 blen = in6_matchlen( 3310 &besta->ia_addr.sin6_addr, dst); 3311 IFA_UNLOCK(&besta->ia_ifa); 3312 IFA_LOCK(ifa); 3313 } 3314 tlen = in6_matchlen(IFA_IN6(ifa), dst); 3315 if (tlen > blen) { 3316 blen = tlen; 3317 IFA_ADDREF_LOCKED(ifa); /* for besta */ 3318 IFA_UNLOCK(ifa); 3319 IFA_REMREF(&besta->ia_ifa); 3320 besta = (struct in6_ifaddr *)ifa; 3321 } else { 3322 IFA_UNLOCK(ifa); 3323 } 3324 } else { 3325 besta = (struct in6_ifaddr *)ifa; 3326 IFA_ADDREF_LOCKED(ifa); /* for besta */ 3327 IFA_UNLOCK(ifa); 3328 } 3329 } else { 3330 IFA_UNLOCK(ifa); 3331 } 3332 } 3333 if (besta) { 3334 ifnet_lock_done(ifp); 3335 if (dep[0] != NULL) 3336 IFA_REMREF(&dep[0]->ia_ifa); 3337 return (besta); 3338 } 3339 3340 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 3341 IFA_LOCK(ifa); 3342 if (ifa->ifa_addr->sa_family != AF_INET6) { 3343 IFA_UNLOCK(ifa); 3344 continue; 3345 } 3346 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_ANYCAST) { 3347 IFA_UNLOCK(ifa); 3348 continue; /* XXX: is there any case to allow anycast? */ 3349 } 3350 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_NOTREADY) { 3351 IFA_UNLOCK(ifa); 3352 continue; /* don't use this interface */ 3353 } 3354 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DETACHED) { 3355 IFA_UNLOCK(ifa); 3356 continue; 3357 } 3358 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 3359 if (ip6_use_deprecated) { 3360 IFA_ADDREF_LOCKED(ifa); /* for dep[1] */ 3361 IFA_UNLOCK(ifa); 3362 if (dep[1] != NULL) 3363 IFA_REMREF(&dep[1]->ia_ifa); 3364 dep[1] = (struct in6_ifaddr *)ifa; 3365 } else { 3366 IFA_UNLOCK(ifa); 3367 } 3368 continue; 3369 } 3370 IFA_ADDREF_LOCKED(ifa); /* for caller */ 3371 IFA_UNLOCK(ifa); 3372 ifnet_lock_done(ifp); 3373 if (dep[0] != NULL) 3374 IFA_REMREF(&dep[0]->ia_ifa); 3375 if (dep[1] != NULL) 3376 IFA_REMREF(&dep[1]->ia_ifa); 3377 return ((struct in6_ifaddr *)ifa); 3378 } 3379 ifnet_lock_done(ifp); 3380 3381 /* use the last-resort values, that are, deprecated addresses */ 3382 if (dep[0]) { 3383 if (dep[1] != NULL) 3384 IFA_REMREF(&dep[1]->ia_ifa); 3385 return (dep[0]); 3386 } 3387 if (dep[1]) 3388 return (dep[1]); 3389 3390 return (NULL); 3391} 3392 3393/* 3394 * perform DAD when interface becomes IFF_UP. 3395 */ 3396static void 3397in6_if_up_dad_start(struct ifnet *ifp) 3398{ 3399 struct ifaddr *ifa; 3400 3401 /* start DAD on all the interface addresses */ 3402 ifnet_lock_exclusive(ifp); 3403 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 3404 struct in6_ifaddr *ia6; 3405 3406 IFA_LOCK_SPIN(ifa); 3407 if (ifa->ifa_addr->sa_family != AF_INET6) { 3408 IFA_UNLOCK(ifa); 3409 continue; 3410 } 3411 ia6 = (struct in6_ifaddr *)ifa; 3412 if (ia6->ia6_flags & IN6_IFF_DADPROGRESS) { 3413 int delay = 0; /* delay ticks before DAD output */ 3414 IFA_UNLOCK(ifa); 3415 nd6_dad_start(ifa, &delay); 3416 } else { 3417 IFA_UNLOCK(ifa); 3418 } 3419 } 3420 ifnet_lock_done(ifp); 3421} 3422 3423int 3424in6if_do_dad( 3425 struct ifnet *ifp) 3426{ 3427 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 3428 return (0); 3429 3430 /* 3431 * If we are using the alternative neighbor discovery 3432 * interface on this interface, then skip DAD. 3433 * 3434 * Also, skip it for interfaces marked "local private" 3435 * for now, even when not marked as using the alternative 3436 * interface. This is for historical reasons. 3437 */ 3438 if (ifp->if_eflags & 3439 (IFEF_IPV6_ND6ALT|IFEF_LOCALNET_PRIVATE|IFEF_DIRECTLINK)) 3440 return (0); 3441 3442 switch (ifp->if_type) { 3443#if IFT_DUMMY 3444 case IFT_DUMMY: 3445#endif 3446 case IFT_FAITH: 3447 /* 3448 * These interfaces do not have the IFF_LOOPBACK flag, 3449 * but loop packets back. We do not have to do DAD on such 3450 * interfaces. We should even omit it, because loop-backed 3451 * NS would confuse the DAD procedure. 3452 */ 3453 return (0); 3454 default: 3455 /* 3456 * Our DAD routine requires the interface up and running. 3457 * However, some interfaces can be up before the RUNNING 3458 * status. Additionaly, users may try to assign addresses 3459 * before the interface becomes up (or running). 3460 * We simply skip DAD in such a case as a work around. 3461 * XXX: we should rather mark "tentative" on such addresses, 3462 * and do DAD after the interface becomes ready. 3463 */ 3464 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != 3465 (IFF_UP|IFF_RUNNING)) 3466 return (0); 3467 3468 return (1); 3469 } 3470} 3471 3472/* 3473 * Calculate max IPv6 MTU through all the interfaces and store it 3474 * to in6_maxmtu. 3475 */ 3476void 3477in6_setmaxmtu(void) 3478{ 3479 u_int32_t maxmtu = 0; 3480 struct ifnet *ifp; 3481 3482 ifnet_head_lock_shared(); 3483 TAILQ_FOREACH(ifp, &ifnet_head, if_list) { 3484 struct nd_ifinfo *ndi; 3485 3486 lck_rw_lock_shared(nd_if_rwlock); 3487 if ((ndi = ND_IFINFO(ifp)) != NULL && !ndi->initialized) 3488 ndi = NULL; 3489 if (ndi != NULL) 3490 lck_mtx_lock(&ndi->lock); 3491 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 3492 IN6_LINKMTU(ifp) > maxmtu) 3493 maxmtu = IN6_LINKMTU(ifp); 3494 if (ndi != NULL) 3495 lck_mtx_unlock(&ndi->lock); 3496 lck_rw_done(nd_if_rwlock); 3497 } 3498 ifnet_head_done(); 3499 if (maxmtu) /* update only when maxmtu is positive */ 3500 in6_maxmtu = maxmtu; 3501} 3502/* 3503 * Provide the length of interface identifiers to be used for the link attached 3504 * to the given interface. The length should be defined in "IPv6 over 3505 * xxx-link" document. Note that address architecture might also define 3506 * the length for a particular set of address prefixes, regardless of the 3507 * link type. Also see RFC 4862 for additional background. 3508 */ 3509int 3510in6_if2idlen(struct ifnet *ifp) 3511{ 3512 switch (ifp->if_type) { 3513 case IFT_ETHER: /* RFC2464 */ 3514 case IFT_IEEE8023ADLAG: /* IEEE802.3ad Link Aggregate */ 3515#ifdef IFT_PROPVIRTUAL 3516 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 3517#endif 3518#ifdef IFT_L2VLAN 3519 case IFT_L2VLAN: /* ditto */ 3520#endif 3521#ifdef IFT_IEEE80211 3522 case IFT_IEEE80211: /* ditto */ 3523#endif 3524#ifdef IFT_MIP 3525 case IFT_MIP: /* ditto */ 3526#endif 3527 return (64); 3528 case IFT_FDDI: /* RFC2467 */ 3529 return (64); 3530 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 3531 return (64); 3532 case IFT_PPP: /* RFC2472 */ 3533 return (64); 3534 case IFT_ARCNET: /* RFC2497 */ 3535 return (64); 3536 case IFT_FRELAY: /* RFC2590 */ 3537 return (64); 3538 case IFT_IEEE1394: /* RFC3146 */ 3539 return (64); 3540 case IFT_GIF: 3541 return (64); /* draft-ietf-v6ops-mech-v2-07 */ 3542 case IFT_LOOP: 3543 return (64); /* XXX: is this really correct? */ 3544 case IFT_OTHER: 3545 return (64); /* for utun interfaces */ 3546 case IFT_CELLULAR: 3547 return (64); /* Packet Data over Cellular */ 3548 case IFT_BRIDGE: 3549 return (64); /* Transparent bridge interface */ 3550 default: 3551 /* 3552 * Unknown link type: 3553 * It might be controversial to use the today's common constant 3554 * of 64 for these cases unconditionally. For full compliance, 3555 * we should return an error in this case. On the other hand, 3556 * if we simply miss the standard for the link type or a new 3557 * standard is defined for a new link type, the IFID length 3558 * is very likely to be the common constant. As a compromise, 3559 * we always use the constant, but make an explicit notice 3560 * indicating the "unknown" case. 3561 */ 3562 log(LOG_NOTICE, "%s: unknown link type (%d)\n", __func__, 3563 ifp->if_type); 3564 return (64); 3565 } 3566} 3567/* 3568 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 3569 * v4 mapped addr or v4 compat addr 3570 */ 3571void 3572in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 3573{ 3574 bzero(sin, sizeof (*sin)); 3575 sin->sin_len = sizeof (struct sockaddr_in); 3576 sin->sin_family = AF_INET; 3577 sin->sin_port = sin6->sin6_port; 3578 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 3579} 3580 3581/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 3582void 3583in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 3584{ 3585 bzero(sin6, sizeof (*sin6)); 3586 sin6->sin6_len = sizeof (struct sockaddr_in6); 3587 sin6->sin6_family = AF_INET6; 3588 sin6->sin6_port = sin->sin_port; 3589 sin6->sin6_addr.s6_addr32[0] = 0; 3590 sin6->sin6_addr.s6_addr32[1] = 0; 3591 if (sin->sin_addr.s_addr) { 3592 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 3593 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 3594 } else { 3595 sin6->sin6_addr.s6_addr32[2] = 0; 3596 sin6->sin6_addr.s6_addr32[3] = 0; 3597 } 3598} 3599 3600/* Convert sockaddr_in6 into sockaddr_in. */ 3601void 3602in6_sin6_2_sin_in_sock(struct sockaddr *nam) 3603{ 3604 struct sockaddr_in *sin_p; 3605 struct sockaddr_in6 sin6; 3606 3607 /* 3608 * Save original sockaddr_in6 addr and convert it 3609 * to sockaddr_in. 3610 */ 3611 sin6 = *(struct sockaddr_in6 *)(void *)nam; 3612 sin_p = (struct sockaddr_in *)(void *)nam; 3613 in6_sin6_2_sin(sin_p, &sin6); 3614} 3615 3616/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 3617int 3618in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 3619{ 3620 struct sockaddr_in *sin_p; 3621 struct sockaddr_in6 *sin6_p; 3622 3623 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof (*sin6_p), M_SONAME, 3624 M_WAITOK); 3625 if (sin6_p == NULL) 3626 return (ENOBUFS); 3627 sin_p = (struct sockaddr_in *)(void *)*nam; 3628 in6_sin_2_v4mapsin6(sin_p, sin6_p); 3629 FREE(*nam, M_SONAME); 3630 *nam = (struct sockaddr *)sin6_p; 3631 3632 return (0); 3633} 3634 3635/* 3636 * Posts in6_event_data message kernel events. 3637 * 3638 * To get the same size of kev_in6_data between ILP32 and LP64 data models 3639 * we are using a special version of the in6_addrlifetime structure that 3640 * uses only 32 bits fields to be compatible with Leopard, and that 3641 * are large enough to span 68 years. 3642 */ 3643void 3644in6_post_msg(struct ifnet *ifp, u_int32_t event_code, struct in6_ifaddr *ifa, 3645 uint8_t *mac) 3646{ 3647 struct kev_msg ev_msg; 3648 struct kev_in6_data in6_event_data; 3649 struct in6_addrlifetime ia6_lt; 3650 3651 bzero(&in6_event_data, sizeof (struct kev_in6_data)); 3652 bzero(&ev_msg, sizeof (struct kev_msg)); 3653 ev_msg.vendor_code = KEV_VENDOR_APPLE; 3654 ev_msg.kev_class = KEV_NETWORK_CLASS; 3655 ev_msg.kev_subclass = KEV_INET6_SUBCLASS; 3656 ev_msg.event_code = event_code; 3657 3658 IFA_LOCK(&ifa->ia_ifa); 3659 in6_event_data.ia_addr = ifa->ia_addr; 3660 in6_event_data.ia_net = ifa->ia_net; 3661 in6_event_data.ia_dstaddr = ifa->ia_dstaddr; 3662 in6_event_data.ia_prefixmask = ifa->ia_prefixmask; 3663 in6_event_data.ia_plen = ifa->ia_plen; 3664 in6_event_data.ia6_flags = (u_int32_t)ifa->ia6_flags; 3665 3666 /* retrieve time as calendar time (last arg is 1) */ 3667 in6ifa_getlifetime(ifa, &ia6_lt, 1); 3668 in6_event_data.ia_lifetime.ia6t_expire = ia6_lt.ia6t_expire; 3669 in6_event_data.ia_lifetime.ia6t_preferred = ia6_lt.ia6t_preferred; 3670 in6_event_data.ia_lifetime.ia6t_vltime = ia6_lt.ia6t_vltime; 3671 in6_event_data.ia_lifetime.ia6t_pltime = ia6_lt.ia6t_pltime; 3672 IFA_UNLOCK(&ifa->ia_ifa); 3673 3674 if (ifp != NULL) { 3675 (void) strlcpy(&in6_event_data.link_data.if_name[0], 3676 ifp->if_name, IFNAMSIZ); 3677 in6_event_data.link_data.if_family = ifp->if_family; 3678 in6_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit; 3679 } 3680 3681 if (mac != NULL) 3682 memcpy(&in6_event_data.ia_mac, mac, 3683 sizeof(in6_event_data.ia_mac)); 3684 3685 ev_msg.dv[0].data_ptr = &in6_event_data; 3686 ev_msg.dv[0].data_length = sizeof (in6_event_data); 3687 ev_msg.dv[1].data_length = 0; 3688 3689 kev_post_msg(&ev_msg); 3690} 3691 3692/* 3693 * Called as part of ip6_init 3694 */ 3695void 3696in6_ifaddr_init(void) 3697{ 3698 in6_cga_init(); 3699 in6_multi_init(); 3700 3701 PE_parse_boot_argn("ifa_debug", &in6ifa_debug, sizeof (in6ifa_debug)); 3702 3703 in6ifa_size = (in6ifa_debug == 0) ? sizeof (struct in6_ifaddr) : 3704 sizeof (struct in6_ifaddr_dbg); 3705 3706 in6ifa_zone = zinit(in6ifa_size, IN6IFA_ZONE_MAX * in6ifa_size, 3707 0, IN6IFA_ZONE_NAME); 3708 if (in6ifa_zone == NULL) { 3709 panic("%s: failed allocating %s", __func__, IN6IFA_ZONE_NAME); 3710 /* NOTREACHED */ 3711 } 3712 zone_change(in6ifa_zone, Z_EXPAND, TRUE); 3713 zone_change(in6ifa_zone, Z_CALLERACCT, FALSE); 3714 3715 lck_mtx_init(&in6ifa_trash_lock, ifa_mtx_grp, ifa_mtx_attr); 3716 TAILQ_INIT(&in6ifa_trash_head); 3717} 3718 3719static struct in6_ifaddr * 3720in6_ifaddr_alloc(int how) 3721{ 3722 struct in6_ifaddr *in6ifa; 3723 3724 in6ifa = (how == M_WAITOK) ? zalloc(in6ifa_zone) : 3725 zalloc_noblock(in6ifa_zone); 3726 if (in6ifa != NULL) { 3727 bzero(in6ifa, in6ifa_size); 3728 in6ifa->ia_ifa.ifa_free = in6_ifaddr_free; 3729 in6ifa->ia_ifa.ifa_debug |= IFD_ALLOC; 3730 ifa_lock_init(&in6ifa->ia_ifa); 3731 if (in6ifa_debug != 0) { 3732 struct in6_ifaddr_dbg *in6ifa_dbg = 3733 (struct in6_ifaddr_dbg *)in6ifa; 3734 in6ifa->ia_ifa.ifa_debug |= IFD_DEBUG; 3735 in6ifa->ia_ifa.ifa_trace = in6_ifaddr_trace; 3736 in6ifa->ia_ifa.ifa_attached = in6_ifaddr_attached; 3737 in6ifa->ia_ifa.ifa_detached = in6_ifaddr_detached; 3738 ctrace_record(&in6ifa_dbg->in6ifa_alloc); 3739 } 3740 } 3741 3742 return (in6ifa); 3743} 3744 3745static void 3746in6_ifaddr_free(struct ifaddr *ifa) 3747{ 3748 IFA_LOCK_ASSERT_HELD(ifa); 3749 3750 if (ifa->ifa_refcnt != 0) { 3751 panic("%s: ifa %p bad ref cnt", __func__, ifa); 3752 /* NOTREACHED */ 3753 } else if (!(ifa->ifa_debug & IFD_ALLOC)) { 3754 panic("%s: ifa %p cannot be freed", __func__, ifa); 3755 /* NOTREACHED */ 3756 } 3757 if (ifa->ifa_debug & IFD_DEBUG) { 3758 struct in6_ifaddr_dbg *in6ifa_dbg = 3759 (struct in6_ifaddr_dbg *)ifa; 3760 ctrace_record(&in6ifa_dbg->in6ifa_free); 3761 bcopy(&in6ifa_dbg->in6ifa, &in6ifa_dbg->in6ifa_old, 3762 sizeof (struct in6_ifaddr)); 3763 if (ifa->ifa_debug & IFD_TRASHED) { 3764 /* Become a regular mutex, just in case */ 3765 IFA_CONVERT_LOCK(ifa); 3766 lck_mtx_lock(&in6ifa_trash_lock); 3767 TAILQ_REMOVE(&in6ifa_trash_head, in6ifa_dbg, 3768 in6ifa_trash_link); 3769 lck_mtx_unlock(&in6ifa_trash_lock); 3770 ifa->ifa_debug &= ~IFD_TRASHED; 3771 } 3772 } 3773 IFA_UNLOCK(ifa); 3774 ifa_lock_destroy(ifa); 3775 bzero(ifa, sizeof (struct in6_ifaddr)); 3776 zfree(in6ifa_zone, ifa); 3777} 3778 3779static void 3780in6_ifaddr_attached(struct ifaddr *ifa) 3781{ 3782 struct in6_ifaddr_dbg *in6ifa_dbg = (struct in6_ifaddr_dbg *)ifa; 3783 3784 IFA_LOCK_ASSERT_HELD(ifa); 3785 3786 if (!(ifa->ifa_debug & IFD_DEBUG)) { 3787 panic("%s: ifa %p has no debug structure", __func__, ifa); 3788 /* NOTREACHED */ 3789 } 3790 if (ifa->ifa_debug & IFD_TRASHED) { 3791 /* Become a regular mutex, just in case */ 3792 IFA_CONVERT_LOCK(ifa); 3793 lck_mtx_lock(&in6ifa_trash_lock); 3794 TAILQ_REMOVE(&in6ifa_trash_head, in6ifa_dbg, in6ifa_trash_link); 3795 lck_mtx_unlock(&in6ifa_trash_lock); 3796 ifa->ifa_debug &= ~IFD_TRASHED; 3797 } 3798} 3799 3800static void 3801in6_ifaddr_detached(struct ifaddr *ifa) 3802{ 3803 struct in6_ifaddr_dbg *in6ifa_dbg = (struct in6_ifaddr_dbg *)ifa; 3804 3805 IFA_LOCK_ASSERT_HELD(ifa); 3806 3807 if (!(ifa->ifa_debug & IFD_DEBUG)) { 3808 panic("%s: ifa %p has no debug structure", __func__, ifa); 3809 /* NOTREACHED */ 3810 } else if (ifa->ifa_debug & IFD_TRASHED) { 3811 panic("%s: ifa %p is already in trash list", __func__, ifa); 3812 /* NOTREACHED */ 3813 } 3814 ifa->ifa_debug |= IFD_TRASHED; 3815 /* Become a regular mutex, just in case */ 3816 IFA_CONVERT_LOCK(ifa); 3817 lck_mtx_lock(&in6ifa_trash_lock); 3818 TAILQ_INSERT_TAIL(&in6ifa_trash_head, in6ifa_dbg, in6ifa_trash_link); 3819 lck_mtx_unlock(&in6ifa_trash_lock); 3820} 3821 3822static void 3823in6_ifaddr_trace(struct ifaddr *ifa, int refhold) 3824{ 3825 struct in6_ifaddr_dbg *in6ifa_dbg = (struct in6_ifaddr_dbg *)ifa; 3826 ctrace_t *tr; 3827 u_int32_t idx; 3828 u_int16_t *cnt; 3829 3830 if (!(ifa->ifa_debug & IFD_DEBUG)) { 3831 panic("%s: ifa %p has no debug structure", __func__, ifa); 3832 /* NOTREACHED */ 3833 } 3834 if (refhold) { 3835 cnt = &in6ifa_dbg->in6ifa_refhold_cnt; 3836 tr = in6ifa_dbg->in6ifa_refhold; 3837 } else { 3838 cnt = &in6ifa_dbg->in6ifa_refrele_cnt; 3839 tr = in6ifa_dbg->in6ifa_refrele; 3840 } 3841 3842 idx = atomic_add_16_ov(cnt, 1) % IN6IFA_TRACE_HIST_SIZE; 3843 ctrace_record(&tr[idx]); 3844} 3845 3846static void 3847in6_ifaddr_set_dadprogress(struct in6_ifaddr *ia) 3848{ 3849 struct ifnet* ifp = ia->ia_ifp; 3850 uint32_t flags = IN6_IFF_TENTATIVE; 3851 uint32_t optdad = nd6_optimistic_dad; 3852 3853 if (optdad) { 3854 if ((ifp->if_eflags & IFEF_IPV6_ROUTER) != 0) { 3855 optdad = 0; 3856 } else { 3857 struct nd_ifinfo *ndi; 3858 3859 lck_rw_lock_shared(nd_if_rwlock); 3860 ndi = ND_IFINFO(ifp); 3861 VERIFY (ndi != NULL && ndi->initialized); 3862 lck_mtx_lock(&ndi->lock); 3863 if ((ndi->flags & ND6_IFF_REPLICATED) != 0) { 3864 optdad = 0; 3865 } 3866 lck_mtx_unlock(&ndi->lock); 3867 lck_rw_done(nd_if_rwlock); 3868 } 3869 } 3870 3871 if (optdad) { 3872 if ((optdad & ND6_OPTIMISTIC_DAD_LINKLOCAL) && 3873 IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) 3874 flags = IN6_IFF_OPTIMISTIC; 3875 else if ((optdad & ND6_OPTIMISTIC_DAD_AUTOCONF) && 3876 (ia->ia6_flags & IN6_IFF_AUTOCONF)) { 3877 if (ia->ia6_flags & IN6_IFF_TEMPORARY) { 3878 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) 3879 flags = IN6_IFF_OPTIMISTIC; 3880 } else if (ia->ia6_flags & IN6_IFF_SECURED) { 3881 if (optdad & ND6_OPTIMISTIC_DAD_SECURED) 3882 flags = IN6_IFF_OPTIMISTIC; 3883 } 3884 } else if ((optdad & ND6_OPTIMISTIC_DAD_DYNAMIC) && 3885 (ia->ia6_flags & IN6_IFF_DYNAMIC)) { 3886 if (ia->ia6_flags & IN6_IFF_TEMPORARY) { 3887 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY) 3888 flags = IN6_IFF_OPTIMISTIC; 3889 } else { 3890 flags = IN6_IFF_OPTIMISTIC; 3891 } 3892 } else if ((optdad & ND6_OPTIMISTIC_DAD_MANUAL) && 3893 (ia->ia6_flags & IN6_IFF_OPTIMISTIC)) { 3894 /* 3895 * rdar://17483438 3896 * Bypass tentative for address assignments 3897 * not covered above (e.g. manual) upon request 3898 */ 3899 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) && 3900 !(ia->ia6_flags & IN6_IFF_AUTOCONF) && 3901 !(ia->ia6_flags & IN6_IFF_DYNAMIC)) 3902 flags = IN6_IFF_OPTIMISTIC; 3903 } 3904 } 3905 3906 ia->ia6_flags &= ~(IN6_IFF_DUPLICATED | IN6_IFF_DADPROGRESS); 3907 ia->ia6_flags |= flags; 3908 3909 nd6log2((LOG_DEBUG, "%s - %s ifp %s ia6_flags 0x%x\n", 3910 __func__, 3911 ip6_sprintf(&ia->ia_addr.sin6_addr), 3912 if_name(ia->ia_ifp), 3913 ia->ia6_flags)); 3914} 3915 3916/* 3917 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain. 3918 */ 3919static int 3920in6_getassocids(struct socket *so, uint32_t *cnt, user_addr_t aidp) 3921{ 3922 struct in6pcb *in6p = sotoin6pcb(so); 3923 associd_t aid; 3924 3925 if (in6p == NULL || in6p->inp_state == INPCB_STATE_DEAD) 3926 return (EINVAL); 3927 3928 /* IN6PCB has no concept of association */ 3929 aid = ASSOCID_ANY; 3930 *cnt = 0; 3931 3932 /* just asking how many there are? */ 3933 if (aidp == USER_ADDR_NULL) 3934 return (0); 3935 3936 return (copyout(&aid, aidp, sizeof (aid))); 3937} 3938 3939/* 3940 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain. 3941 */ 3942static int 3943in6_getconnids(struct socket *so, associd_t aid, uint32_t *cnt, 3944 user_addr_t cidp) 3945{ 3946 struct in6pcb *in6p = sotoin6pcb(so); 3947 connid_t cid; 3948 3949 if (in6p == NULL || in6p->inp_state == INPCB_STATE_DEAD) 3950 return (EINVAL); 3951 3952 if (aid != ASSOCID_ANY && aid != ASSOCID_ALL) 3953 return (EINVAL); 3954 3955 /* if connected, return 1 connection count */ 3956 *cnt = ((so->so_state & SS_ISCONNECTED) ? 1 : 0); 3957 3958 /* just asking how many there are? */ 3959 if (cidp == USER_ADDR_NULL) 3960 return (0); 3961 3962 /* if IN6PCB is connected, assign it connid 1 */ 3963 cid = ((*cnt != 0) ? 1 : CONNID_ANY); 3964 3965 return (copyout(&cid, cidp, sizeof (cid))); 3966} 3967 3968/* 3969 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain. 3970 */ 3971static int 3972in6_getconninfo(struct socket *so, connid_t cid, uint32_t *flags, 3973 uint32_t *ifindex, int32_t *soerror, user_addr_t src, socklen_t *src_len, 3974 user_addr_t dst, socklen_t *dst_len, uint32_t *aux_type, 3975 user_addr_t aux_data, uint32_t *aux_len) 3976{ 3977#pragma unused(aux_data) 3978 struct in6pcb *in6p = sotoin6pcb(so); 3979 struct sockaddr_in6 sin6; 3980 struct ifnet *ifp = NULL; 3981 int error = 0; 3982 u_int32_t copy_len = 0; 3983 3984 /* 3985 * Don't test for INPCB_STATE_DEAD since this may be called 3986 * after SOF_PCBCLEARING is set, e.g. after tcp_close(). 3987 */ 3988 if (in6p == NULL) { 3989 error = EINVAL; 3990 goto out; 3991 } 3992 3993 if (cid != CONNID_ANY && cid != CONNID_ALL && cid != 1) { 3994 error = EINVAL; 3995 goto out; 3996 } 3997 3998 ifp = in6p->in6p_last_outifp; 3999 *ifindex = ((ifp != NULL) ? ifp->if_index : 0); 4000 *soerror = so->so_error; 4001 *flags = 0; 4002 if (so->so_state & SS_ISCONNECTED) 4003 *flags |= (CIF_CONNECTED | CIF_PREFERRED); 4004 if (in6p->in6p_flags & INP_BOUND_IF) 4005 *flags |= CIF_BOUND_IF; 4006 if (!(in6p->in6p_flags & INP_IN6ADDR_ANY)) 4007 *flags |= CIF_BOUND_IP; 4008 if (!(in6p->in6p_flags & INP_ANONPORT)) 4009 *flags |= CIF_BOUND_PORT; 4010 4011 bzero(&sin6, sizeof (sin6)); 4012 sin6.sin6_len = sizeof (sin6); 4013 sin6.sin6_family = AF_INET6; 4014 4015 /* source address and port */ 4016 sin6.sin6_port = in6p->in6p_lport; 4017 in6_recoverscope(&sin6, &in6p->in6p_laddr, NULL); 4018 if (*src_len == 0) { 4019 *src_len = sin6.sin6_len; 4020 } else { 4021 if (src != USER_ADDR_NULL) { 4022 copy_len = min(*src_len, sizeof (sin6)); 4023 error = copyout(&sin6, src, copy_len); 4024 if (error != 0) 4025 goto out; 4026 *src_len = copy_len; 4027 } 4028 } 4029 4030 /* destination address and port */ 4031 sin6.sin6_port = in6p->in6p_fport; 4032 in6_recoverscope(&sin6, &in6p->in6p_faddr, NULL); 4033 if (*dst_len == 0) { 4034 *dst_len = sin6.sin6_len; 4035 } else { 4036 if (dst != USER_ADDR_NULL) { 4037 copy_len = min(*dst_len, sizeof (sin6)); 4038 error = copyout(&sin6, dst, copy_len); 4039 if (error != 0) 4040 goto out; 4041 *dst_len = copy_len; 4042 } 4043 } 4044 4045 *aux_type = 0; 4046 *aux_len = 0; 4047 if (SOCK_PROTO(so) == IPPROTO_TCP) { 4048 struct conninfo_tcp tcp_ci; 4049 4050 *aux_type = CIAUX_TCP; 4051 if (*aux_len == 0) { 4052 *aux_len = sizeof (tcp_ci); 4053 } else { 4054 if (aux_data != USER_ADDR_NULL) { 4055 copy_len = min(*aux_len, sizeof (tcp_ci)); 4056 bzero(&tcp_ci, sizeof (tcp_ci)); 4057 tcp_getconninfo(so, &tcp_ci); 4058 error = copyout(&tcp_ci, aux_data, copy_len); 4059 if (error != 0) 4060 goto out; 4061 *aux_len = copy_len; 4062 } 4063 } 4064 } 4065 4066out: 4067 return (error); 4068} 4069 4070/* 4071 * 'u' group ioctls. 4072 * 4073 * The switch statement below does nothing at runtime, as it serves as a 4074 * compile time check to ensure that all of the socket 'u' ioctls (those 4075 * in the 'u' group going thru soo_ioctl) that are made available by the 4076 * networking stack is unique. This works as long as this routine gets 4077 * updated each time a new interface ioctl gets added. 4078 * 4079 * Any failures at compile time indicates duplicated ioctl values. 4080 */ 4081static __attribute__((unused)) void 4082in6ioctl_cassert(void) 4083{ 4084 /* 4085 * This is equivalent to _CASSERT() and the compiler wouldn't 4086 * generate any instructions, thus for compile time only. 4087 */ 4088 switch ((u_long)0) { 4089 case 0: 4090 4091 /* bsd/netinet6/in6_var.h */ 4092 case SIOCAADDRCTL_POLICY: 4093 case SIOCDADDRCTL_POLICY: 4094 case SIOCDRADD_IN6_32: 4095 case SIOCDRADD_IN6_64: 4096 case SIOCDRDEL_IN6_32: 4097 case SIOCDRDEL_IN6_64: 4098 ; 4099 } 4100} 4101