]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6.c
CommitLineData
2d21ac55 1/*
3e170ce0 2 * Copyright (c) 2003-2015 Apple Inc. All rights reserved.
2d21ac55
A
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
1c79356b
A
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
1c79356b
A
93
94#include <sys/param.h>
1c79356b 95#include <sys/ioctl.h>
1c79356b
A
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>
9bccf70c 105#include <sys/kern_event.h>
6d2010ae
A
106#include <sys/mcache.h>
107#include <sys/protosw.h>
b0d623f7 108
2d21ac55 109#include <kern/locks.h>
b0d623f7
A
110#include <kern/zalloc.h>
111#include <libkern/OSAtomic.h>
112#include <machine/machine_routines.h>
39236c6e 113#include <mach/boolean.h>
1c79356b
A
114
115#include <net/if.h>
116#include <net/if_types.h>
2d21ac55 117#include <net/if_var.h>
1c79356b 118#include <net/route.h>
1c79356b 119#include <net/if_dl.h>
2d21ac55 120#include <net/kpi_protocol.h>
1c79356b
A
121
122#include <netinet/in.h>
123#include <netinet/in_var.h>
1c79356b 124#include <netinet/if_ether.h>
9bccf70c
A
125#include <netinet/in_systm.h>
126#include <netinet/ip.h>
127#include <netinet/in_pcb.h>
6d2010ae 128#include <netinet/icmp6.h>
39236c6e
A
129#include <netinet/tcp.h>
130#include <netinet/tcp_seq.h>
131#include <netinet/tcp_var.h>
1c79356b
A
132
133#include <netinet6/nd6.h>
134#include <netinet/ip6.h>
135#include <netinet6/ip6_var.h>
136#include <netinet6/mld6_var.h>
1c79356b 137#include <netinet6/in6_ifattach.h>
9bccf70c 138#include <netinet6/scope6_var.h>
6d2010ae 139#include <netinet6/in6_var.h>
9bccf70c 140#include <netinet6/in6_pcb.h>
1c79356b
A
141
142#include <net/net_osdep.h>
143
b0d623f7
A
144#if PF
145#include <net/pfvar.h>
146#endif /* PF */
147
39236c6e 148/*
1c79356b
A
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;
6d2010ae
A
159const struct in6_addr in6addr_linklocal_allv2routers =
160 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
1c79356b
A
161
162const struct in6_addr in6mask0 = IN6MASK0;
6d2010ae
A
163const struct in6_addr in6mask7 = IN6MASK7;
164const struct in6_addr in6mask16 = IN6MASK16;
1c79356b
A
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
39236c6e
A
170const struct sockaddr_in6 sa6_any = {
171 sizeof (sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0
172};
9bccf70c 173
39236c6e
A
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 *);
316670eb
A
187static int in6_autoconf(struct ifnet *, int);
188static int in6_setrouter(struct ifnet *, int);
39236c6e
A
189static int in6_ifinit(struct ifnet *, struct in6_ifaddr *, int);
190static int in6_ifaupdate_aux(struct in6_ifaddr *, struct ifnet *, int);
6d2010ae 191static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
b0d623f7 192static struct in6_ifaddr *in6_ifaddr_alloc(int);
6d2010ae
A
193static void in6_ifaddr_attached(struct ifaddr *);
194static void in6_ifaddr_detached(struct ifaddr *);
b0d623f7
A
195static void in6_ifaddr_free(struct ifaddr *);
196static void in6_ifaddr_trace(struct ifaddr *, int);
39236c6e
A
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
b0d623f7
A
204static struct in6_aliasreq *in6_aliasreq_to_native(void *, int,
205 struct in6_aliasreq *);
39236c6e
A
206static struct in6_llstartreq *in6_llstartreq_to_native(void *, int,
207 struct in6_llstartreq *);
208static int in6_to_kamescope(struct sockaddr_in6 *, struct ifnet *);
1c79356b 209
316670eb
A
210static void in6_ifaddr_set_dadprogress(struct in6_ifaddr *);
211
39236c6e 212static int in6_getassocids(struct socket *, uint32_t *, user_addr_t);
3e170ce0
A
213static int in6_getconnids(struct socket *, sae_associd_t, uint32_t *,
214 user_addr_t);
215static int in6_getconninfo(struct socket *, sae_connid_t, uint32_t *,
39236c6e
A
216 uint32_t *, int32_t *, user_addr_t, socklen_t *, user_addr_t, socklen_t *,
217 uint32_t *, user_addr_t, uint32_t *);
218
219static void in6_if_up_dad_start(struct ifnet *);
220
2d21ac55 221extern lck_mtx_t *nd6_mutex;
1c79356b 222
6d2010ae
A
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
b0d623f7
A
228struct in6_ifaddr_dbg {
229 struct in6_ifaddr in6ifa; /* in6_ifaddr */
230 struct in6_ifaddr in6ifa_old; /* saved in6_ifaddr */
6d2010ae
A
231 u_int16_t in6ifa_refhold_cnt; /* # of IFA_ADDREF */
232 u_int16_t in6ifa_refrele_cnt; /* # of IFA_REMREF */
b0d623f7
A
233 /*
234 * Alloc and free callers.
235 */
236 ctrace_t in6ifa_alloc;
237 ctrace_t in6ifa_free;
238 /*
6d2010ae
A
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
b0d623f7 245 */
6d2010ae 246 TAILQ_ENTRY(in6_ifaddr_dbg) in6ifa_trash_link;
b0d623f7
A
247};
248
6d2010ae
A
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 */
b0d623f7
A
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
1c79356b
A
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{
1c79356b
A
271 struct sockaddr_in6 all1_sa;
272 struct rtentry *nrt = NULL;
9bccf70c 273 int e;
b0d623f7 274
39236c6e 275 bzero(&all1_sa, sizeof (all1_sa));
9bccf70c 276 all1_sa.sin6_family = AF_INET6;
39236c6e 277 all1_sa.sin6_len = sizeof (struct sockaddr_in6);
1c79356b 278 all1_sa.sin6_addr = in6mask128;
9bccf70c
A
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.
6d2010ae 287 * ifa_addr for INET6 is set once during init; no need to hold lock.
9bccf70c 288 */
b0d623f7 289 lck_mtx_lock(rnh_lock);
91447636 290 e = rtrequest_locked(cmd, ifa->ifa_addr, ifa->ifa_addr,
39236c6e 291 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
9bccf70c
A
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 }
1c79356b 299
b0d623f7
A
300 if (nrt != NULL)
301 RT_LOCK(nrt);
1c79356b
A
302 /*
303 * Make sure rt_ifa be equal to IFA, the second argument of the
304 * function.
9bccf70c
A
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.
1c79356b
A
308 */
309 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
9bccf70c 310 rtsetifa(nrt, ifa);
1c79356b 311 }
9bccf70c
A
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,
39236c6e
A
316 * we end up reporting twice in such a case. Should we rather
317 * omit the second report?
9bccf70c 318 */
b0d623f7 319 if (nrt != NULL) {
9bccf70c
A
320 rt_newaddrmsg(cmd, ifa, e, nrt);
321 if (cmd == RTM_DELETE) {
b0d623f7 322 RT_UNLOCK(nrt);
2d21ac55 323 rtfree_locked(nrt);
9bccf70c
A
324 } else {
325 /* the cmd must be RTM_ADD here */
b0d623f7
A
326 RT_REMREF_LOCKED(nrt);
327 RT_UNLOCK(nrt);
9bccf70c
A
328 }
329 }
b0d623f7 330 lck_mtx_unlock(rnh_lock);
1c79356b
A
331}
332
333/*
9bccf70c
A
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.
1c79356b
A
339 */
340static void
341in6_ifaddloop(struct ifaddr *ifa)
342{
9bccf70c
A
343 struct rtentry *rt;
344
6d2010ae
A
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 */
b0d623f7
A
349 rt = rtalloc1(ifa->ifa_addr, 0, 0);
350 if (rt != NULL)
351 RT_LOCK(rt);
9bccf70c 352 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
b0d623f7
A
353 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
354 if (rt != NULL) {
355 RT_REMREF_LOCKED(rt);
356 RT_UNLOCK(rt);
357 }
9bccf70c 358 in6_ifloop_request(RTM_ADD, ifa);
b0d623f7
A
359 } else if (rt != NULL) {
360 RT_REMREF_LOCKED(rt);
361 RT_UNLOCK(rt);
362 }
1c79356b
A
363}
364
365/*
366 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
367 * if it exists.
368 */
369static void
6d2010ae 370in6_ifremloop(struct ifaddr *ifa)
1c79356b 371{
9bccf70c
A
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 /*
55e303ae 387 * Delete the entry only if exact one ifa exists. More than one ifa
9bccf70c
A
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 */
6d2010ae 392 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
91447636 393 for (ia = in6_ifaddrs; ia; ia = ia->ia_next) {
6d2010ae 394 IFA_LOCK(&ia->ia_ifa);
9bccf70c
A
395 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
396 ia_count++;
6d2010ae
A
397 if (ia_count > 1) {
398 IFA_UNLOCK(&ia->ia_ifa);
9bccf70c 399 break;
6d2010ae 400 }
1c79356b 401 }
6d2010ae 402 IFA_UNLOCK(&ia->ia_ifa);
9bccf70c 403 }
6d2010ae 404 lck_rw_done(&in6_ifaddr_rwlock);
9bccf70c
A
405
406 if (ia_count == 1) {
407 /*
408 * Before deleting, check if a corresponding loopbacked host
55e303ae 409 * route surely exists. With this check, we can avoid to
9bccf70c 410 * delete an interface direct route whose destination is same
6d2010ae 411 * as the address being removed. This can happen when removing
9bccf70c 412 * a subnet-router anycast address on an interface attahced
6d2010ae
A
413 * to a shared medium. ifa_addr for INET6 is set once during
414 * init; no need to hold lock.
9bccf70c 415 */
b0d623f7
A
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 }
9bccf70c 427 }
1c79356b
A
428 }
429}
430
1c79356b
A
431
432int
9bccf70c 433in6_mask2len(mask, lim0)
1c79356b 434 struct in6_addr *mask;
9bccf70c 435 u_char *lim0;
1c79356b 436{
9bccf70c
A
437 int x = 0, y;
438 u_char *lim = lim0, *p;
439
6d2010ae 440 /* ignore the scope_id part */
39236c6e
A
441 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof (*mask))
442 lim = (u_char *)mask + sizeof (*mask);
9bccf70c
A
443 for (p = (u_char *)mask; p < lim; x++, p++) {
444 if (*p != 0xff)
1c79356b
A
445 break;
446 }
447 y = 0;
9bccf70c 448 if (p < lim) {
1c79356b 449 for (y = 0; y < 8; y++) {
9bccf70c 450 if ((*p & (0x80 >> y)) == 0)
1c79356b
A
451 break;
452 }
453 }
9bccf70c
A
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)
6d2010ae 461 return (-1);
9bccf70c
A
462 for (p = p + 1; p < lim; p++)
463 if (*p != 0)
6d2010ae 464 return (-1);
9bccf70c 465 }
6d2010ae 466
39236c6e 467 return (x * 8 + y);
1c79356b
A
468}
469
470void
471in6_len2mask(mask, len)
472 struct in6_addr *mask;
473 int len;
474{
475 int i;
476
39236c6e 477 bzero(mask, sizeof (*mask));
1c79356b
A
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
b0d623f7
A
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
39236c6e
A
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
b0d623f7
A
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)
316670eb 559 bcopy(data, dst, sizeof (*dst));
b0d623f7
A
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
316670eb 568 bcopy(data, dst, sizeof (*dst));
b0d623f7
A
569#endif /* __LP64__ */
570 return (dst);
571}
572
39236c6e
A
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}
1c79356b 591
39236c6e
A
592static __attribute__((noinline)) int
593in6ctl_associd(struct socket *so, u_long cmd, caddr_t data)
1c79356b 594{
39236c6e
A
595 int error = 0;
596 union {
597 struct so_aidreq32 a32;
598 struct so_aidreq64 a64;
599 } u;
91447636 600
39236c6e 601 VERIFY(so != NULL);
1c79356b 602
1c79356b 603 switch (cmd) {
39236c6e
A
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 }
6d2010ae 611
39236c6e
A
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 }
6d2010ae 619
39236c6e
A
620 default:
621 VERIFY(0);
316670eb 622 /* NOTREACHED */
6d2010ae
A
623 }
624
39236c6e
A
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);
1c79356b
A
638
639 switch (cmd) {
39236c6e
A
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));
91447636 646 break;
39236c6e 647 }
316670eb 648
39236c6e
A
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 }
1c79356b 657
39236c6e
A
658 default:
659 VERIFY(0);
316670eb 660 /* NOTREACHED */
39236c6e 661 }
316670eb 662
39236c6e
A
663 return (error);
664}
9bccf70c 665
39236c6e
A
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;
316670eb 674
39236c6e 675 VERIFY(so != NULL);
316670eb 676
39236c6e
A
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;
316670eb
A
688 }
689
39236c6e
A
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;
316670eb 700 }
b0d623f7 701
39236c6e
A
702 default:
703 VERIFY(0);
316670eb 704 /* NOTREACHED */
1c79356b
A
705 }
706
39236c6e
A
707 return (error);
708}
316670eb 709
39236c6e
A
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);
b0d623f7 718
b0d623f7 719 switch (cmd) {
316670eb 720 case SIOCLL_START_32: /* struct in6_aliasreq_32 */
39236c6e
A
721 case SIOCLL_START_64: /* struct in6_aliasreq_64 */
722 is64 = (cmd == SIOCLL_START_64);
b0d623f7
A
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 */
39236c6e
A
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);
b0d623f7 748 break;
39236c6e
A
749
750 default:
751 VERIFY(0);
752 /* NOTREACHED */
316670eb 753 }
b0d623f7 754
39236c6e
A
755 return (error);
756}
757
758static __attribute__((noinline)) int
759in6ctl_llstop(struct ifnet *ifp)
760{
761 struct in6_ifaddr *ia;
fe8ab488 762 struct nd_prefix pr0, *pr;
39236c6e
A
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);
fe8ab488
A
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);
3e170ce0 802 pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_UNSPEC);
fe8ab488
A
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
39236c6e
A
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);
316670eb 827 /*
39236c6e 828 * Convert user llstartreq to the kernel form, when appropriate.
316670eb
A
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 */
39236c6e
A
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);
b0d623f7 844 break;
39236c6e
A
845
846 default:
847 VERIFY(0);
848 /* NOTREACHED */
316670eb 849 }
b0d623f7 850
39236c6e
A
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) {
316670eb 872 case SIOCGIFADDR_IN6: /* struct in6_ifreq */
39236c6e
A
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;
316670eb 880
39236c6e
A
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));
b0d623f7
A
896 break;
897
898 default:
39236c6e
A
899 VERIFY(0);
900 /* NOTREACHED */
1c79356b 901 }
9bccf70c 902
39236c6e
A
903 return (error);
904}
55e303ae 905
39236c6e
A
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;
55e303ae 915
39236c6e
A
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));
b0d623f7 926 } else {
39236c6e
A
927 bcopy(&IN6_IFEXTRA(ifp)->in6_ifstat,
928 &ifr->ifr_ifru.ifru_stat,
929 sizeof (ifr->ifr_ifru.ifru_stat));
b0d623f7 930 }
39236c6e
A
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);
b0d623f7 948 /* NOTREACHED */
39236c6e 949 }
55e303ae 950
39236c6e
A
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(&lt, 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(&lt, &ifr->ifr_ifru.ifru_lifetime, sizeof (lt));
985 } else {
986 struct in6_addrlifetime_32 lt;
987
988 bzero(&lt, 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(&lt, &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, &lt, 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;
6d2010ae 1010 }
39236c6e
A
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, &lt, 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;
6d2010ae 1029 }
55e303ae 1030 }
39236c6e
A
1031
1032 IFA_LOCK(&ia->ia_ifa);
1033 if (p64) {
1034 struct in6_addrlifetime_64 lt;
1035
1036 bcopy(&ifr->ifr_ifru.ifru_lifetime, &lt, 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, &lt, 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);
316670eb 1067 /* NOTREACHED */
39236c6e
A
1068 }
1069
1070 return (error);
1071}
316670eb 1072
39236c6e
A
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) {
39236c6e
A
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
3e170ce0
A
1147 /*
1148 * Unlock the socket since ifnet_ioctl() may be invoked by
1149 * one of the ioctl handlers below. Socket will be re-locked
1150 * prior to returning.
1151 */
1152 if (so != NULL) {
1153 socket_unlock(so, 0);
1154 so_unlocked = TRUE;
1155 }
1156
39236c6e
A
1157 /*
1158 * ioctls which require ifp but not interface address.
1159 */
1160 switch (cmd) {
1161 case SIOCAUTOCONF_START: /* struct in6_ifreq */
3e170ce0
A
1162 if (!privileged) {
1163 error = EPERM;
1164 goto done;
1165 }
1166 error = in6_autoconf(ifp, TRUE);
1167 goto done;
39236c6e
A
1168
1169 case SIOCAUTOCONF_STOP: /* struct in6_ifreq */
3e170ce0
A
1170 if (!privileged) {
1171 error = EPERM;
1172 goto done;
1173 }
1174 error = in6_autoconf(ifp, FALSE);
1175 goto done;
39236c6e
A
1176
1177 case SIOCLL_START_32: /* struct in6_aliasreq_32 */
1178 case SIOCLL_START_64: /* struct in6_aliasreq_64 */
3e170ce0
A
1179 if (!privileged) {
1180 error = EPERM;
1181 goto done;
1182 }
1183 error = in6ctl_llstart(ifp, cmd, data);
1184 goto done;
39236c6e
A
1185
1186 case SIOCLL_STOP: /* struct in6_ifreq */
3e170ce0
A
1187 if (!privileged) {
1188 error = EPERM;
1189 goto done;
1190 }
1191 error = in6ctl_llstop(ifp);
1192 goto done;
39236c6e
A
1193
1194 case SIOCSETROUTERMODE_IN6: /* struct in6_ifreq */
3e170ce0
A
1195 if (!privileged) {
1196 error = EPERM;
1197 goto done;
1198 }
316670eb
A
1199 bcopy(&((struct in6_ifreq *)(void *)data)->ifr_intval,
1200 &intval, sizeof (intval));
1201
3e170ce0
A
1202 error = in6_setrouter(ifp, intval);
1203 goto done;
55e303ae 1204
316670eb
A
1205 case SIOCPROTOATTACH_IN6_32: /* struct in6_aliasreq_32 */
1206 case SIOCPROTOATTACH_IN6_64: /* struct in6_aliasreq_64 */
3e170ce0
A
1207 if (!privileged) {
1208 error = EPERM;
1209 goto done;
1210 }
1211 error = in6_domifattach(ifp);
1212 goto done;
55e303ae 1213
316670eb 1214 case SIOCPROTODETACH_IN6: /* struct in6_ifreq */
3e170ce0
A
1215 if (!privileged) {
1216 error = EPERM;
1217 goto done;
1218 }
b0d623f7
A
1219 /* Cleanup interface routes and addresses */
1220 in6_purgeif(ifp);
55e303ae 1221
2d21ac55 1222 if ((error = proto_unplumb(PF_INET6, ifp)))
39236c6e 1223 log(LOG_ERR, "SIOCPROTODETACH_IN6: %s error=%d\n",
b0d623f7 1224 if_name(ifp), error);
3e170ce0 1225 goto done;
39236c6e
A
1226
1227 case SIOCSNDFLUSH_IN6: /* struct in6_ifreq */
1228 case SIOCSPFXFLUSH_IN6: /* struct in6_ifreq */
1229 case SIOCSRTRFLUSH_IN6: /* struct in6_ifreq */
1230 case SIOCSDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
1231 case SIOCSDEFIFACE_IN6_64: /* struct in6_ndifreq_64 */
1232 case SIOCSIFINFO_FLAGS: /* struct in6_ndireq */
3e170ce0
A
1233 if (!privileged) {
1234 error = EPERM;
1235 goto done;
1236 }
39236c6e
A
1237 /* FALLTHRU */
1238 case OSIOCGIFINFO_IN6: /* struct in6_ondireq */
1239 case SIOCGIFINFO_IN6: /* struct in6_ondireq */
1240 case SIOCGDRLST_IN6_32: /* struct in6_drlist_32 */
1241 case SIOCGDRLST_IN6_64: /* struct in6_drlist_64 */
1242 case SIOCGPRLST_IN6_32: /* struct in6_prlist_32 */
1243 case SIOCGPRLST_IN6_64: /* struct in6_prlist_64 */
1244 case SIOCGNBRINFO_IN6_32: /* struct in6_nbrinfo_32 */
1245 case SIOCGNBRINFO_IN6_64: /* struct in6_nbrinfo_64 */
1246 case SIOCGDEFIFACE_IN6_32: /* struct in6_ndifreq_32 */
1247 case SIOCGDEFIFACE_IN6_64: /* struct in6_ndifreq_64 */
3e170ce0
A
1248 error = nd6_ioctl(cmd, data, ifp);
1249 goto done;
39236c6e
A
1250
1251 case SIOCSIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */
1252 case SIOCDIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */
1253 case SIOCAIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */
1254 case SIOCCIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */
1255 case SIOCSGIFPREFIX_IN6: /* struct in6_rrenumreq (deprecated) */
1256 case SIOCGIFPREFIX_IN6: /* struct in6_prefixreq (deprecated) */
1257 log(LOG_NOTICE,
1258 "prefix ioctls are now invalidated. "
1259 "please use ifconfig.\n");
3e170ce0
A
1260 error = EOPNOTSUPP;
1261 goto done;
39236c6e
A
1262
1263 case SIOCSSCOPE6: /* struct in6_ifreq (deprecated) */
1264 case SIOCGSCOPE6: /* struct in6_ifreq (deprecated) */
1265 case SIOCGSCOPE6DEF: /* struct in6_ifreq (deprecated) */
3e170ce0
A
1266 error = EOPNOTSUPP;
1267 goto done;
fe8ab488 1268
39236c6e
A
1269 case SIOCLL_CGASTART_32: /* struct in6_llstartreq_32 */
1270 case SIOCLL_CGASTART_64: /* struct in6_llstartreq_64 */
1271 if (!privileged)
3e170ce0
A
1272 error = EPERM;
1273 else
1274 error = in6ctl_cgastart(ifp, cmd, data);
1275 goto done;
39236c6e
A
1276
1277 case SIOCGIFSTAT_IN6: /* struct in6_ifreq */
1278 case SIOCGIFSTAT_ICMP6: /* struct in6_ifreq */
3e170ce0
A
1279 error = in6ctl_gifstat(ifp, cmd, ifr);
1280 goto done;
39236c6e
A
1281 }
1282
1283 /*
1284 * ioctls which require interface address; obtain sockaddr_in6.
1285 */
1286 switch (cmd) {
1287 case SIOCSIFADDR_IN6: /* struct in6_ifreq (deprecated) */
1288 case SIOCSIFDSTADDR_IN6: /* struct in6_ifreq (deprecated) */
1289 case SIOCSIFNETMASK_IN6: /* struct in6_ifreq (deprecated) */
1290 /*
1291 * Since IPv6 allows a node to assign multiple addresses
1292 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1293 */
1294 /* we decided to obsolete this command (20000704) */
3e170ce0
A
1295 error = EOPNOTSUPP;
1296 goto done;
39236c6e
A
1297
1298 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */
1299 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */
3e170ce0
A
1300 if (!privileged) {
1301 error = EPERM;
1302 goto done;
1303 }
39236c6e
A
1304 /*
1305 * Convert user ifra to the kernel form, when appropriate.
1306 * This allows the conversion between different data models
1307 * to be centralized, so that it can be passed around to other
1308 * routines that are expecting the kernel form.
1309 */
1310 ifra = in6_aliasreq_to_native(data,
1311 (cmd == SIOCAIFADDR_IN6_64), &sifra);
1312 bcopy(&ifra->ifra_addr, &sin6, sizeof (sin6));
1313 sa6 = &sin6;
1314 break;
1315
1316 case SIOCDIFADDR_IN6: /* struct in6_ifreq */
1317 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */
3e170ce0
A
1318 if (!privileged) {
1319 error = EPERM;
1320 goto done;
1321 }
39236c6e
A
1322 /* FALLTHRU */
1323 case SIOCGIFADDR_IN6: /* struct in6_ifreq */
1324 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */
1325 case SIOCGIFNETMASK_IN6: /* struct in6_ifreq */
1326 case SIOCGIFAFLAG_IN6: /* struct in6_ifreq */
1327 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */
1328 bcopy(&ifr->ifr_addr, &sin6, sizeof (sin6));
1329 sa6 = &sin6;
1330 break;
9bccf70c 1331 }
b0d623f7 1332
1c79356b 1333 /*
316670eb
A
1334 * Find address for this interface, if it exists.
1335 *
1336 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1337 * only, and used the first interface address as the target of other
1338 * operations (without checking ifra_addr). This was because netinet
1339 * code/API assumed at most 1 interface address per interface.
1340 * Since IPv6 allows a node to assign multiple addresses
1341 * on a single interface, we almost always look and check the
1342 * presence of ifra_addr, and reject invalid ones here.
1343 * It also decreases duplicated code among SIOC*_IN6 operations.
1c79356b 1344 */
39236c6e 1345 VERIFY(ia == NULL);
b0d623f7 1346 if (sa6 != NULL && sa6->sin6_family == AF_INET6) {
1c79356b
A
1347 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
1348 if (sa6->sin6_addr.s6_addr16[1] == 0) {
9bccf70c 1349 /* link ID is not embedded by the user */
1c79356b 1350 sa6->sin6_addr.s6_addr16[1] =
b0d623f7 1351 htons(ifp->if_index);
1c79356b 1352 } else if (sa6->sin6_addr.s6_addr16[1] !=
b0d623f7 1353 htons(ifp->if_index)) {
3e170ce0
A
1354 error = EINVAL; /* link ID contradicts */
1355 goto done;
1c79356b
A
1356 }
1357 if (sa6->sin6_scope_id) {
1358 if (sa6->sin6_scope_id !=
3e170ce0
A
1359 (u_int32_t)ifp->if_index) {
1360 error = EINVAL;
1361 goto done;
1362 }
1c79356b
A
1363 sa6->sin6_scope_id = 0; /* XXX: good way? */
1364 }
1365 }
39236c6e
A
1366 /*
1367 * Any failures from this point on must take into account
1368 * a non-NULL "ia" with an outstanding reference count, and
1369 * therefore requires IFA_REMREF. Jump to "done" label
1370 * instead of calling return if "ia" is valid.
1371 */
b0d623f7 1372 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
1c79356b
A
1373 }
1374
39236c6e
A
1375 /*
1376 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1377 */
1c79356b 1378 switch (cmd) {
316670eb 1379 case SIOCDIFADDR_IN6: /* struct in6_ifreq */
3e170ce0
A
1380 if (ia == NULL) {
1381 error = EADDRNOTAVAIL;
1382 goto done;
1383 }
1c79356b 1384 /* FALLTHROUGH */
316670eb
A
1385 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */
1386 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */
1387 VERIFY(sa6 != NULL);
1c79356b 1388 /*
9bccf70c 1389 * We always require users to specify a valid IPv6 address for
b0d623f7
A
1390 * the corresponding operation. Use "sa6" instead of "ifra"
1391 * since SIOCDIFADDR_IN6 falls thru above.
1c79356b 1392 */
b0d623f7 1393 if (sa6->sin6_family != AF_INET6 ||
39236c6e 1394 sa6->sin6_len != sizeof (struct sockaddr_in6)) {
91447636 1395 error = EAFNOSUPPORT;
39236c6e 1396 goto done;
91447636 1397 }
1c79356b 1398 break;
39236c6e
A
1399 }
1400
39236c6e
A
1401 /*
1402 * And finally process address-related ioctls.
1403 */
1404 switch (cmd) {
316670eb 1405 case SIOCGIFADDR_IN6: /* struct in6_ifreq */
1c79356b 1406 /* This interface is basically deprecated. use SIOCGIFCONF. */
316670eb 1407 /* FALLTHRU */
316670eb 1408 case SIOCGIFDSTADDR_IN6: /* struct in6_ifreq */
39236c6e 1409 error = in6ctl_gifaddr(ifp, ia, cmd, ifr);
1c79356b 1410 break;
1c79356b 1411
39236c6e
A
1412 case SIOCGIFNETMASK_IN6: /* struct in6_ifreq */
1413 if (ia != NULL) {
1414 IFA_LOCK(&ia->ia_ifa);
1415 bcopy(&ia->ia_prefixmask, &ifr->ifr_addr,
1416 sizeof (struct sockaddr_in6));
1417 IFA_UNLOCK(&ia->ia_ifa);
1418 } else {
91447636 1419 error = EADDRNOTAVAIL;
91447636 1420 }
39236c6e 1421 break;
b0d623f7 1422
39236c6e
A
1423 case SIOCGIFAFLAG_IN6: /* struct in6_ifreq */
1424 if (ia != NULL) {
1425 IFA_LOCK(&ia->ia_ifa);
1426 bcopy(&ia->ia6_flags, &ifr->ifr_ifru.ifru_flags6,
1427 sizeof (ifr->ifr_ifru.ifru_flags6));
1428 IFA_UNLOCK(&ia->ia_ifa);
b0d623f7 1429 } else {
39236c6e 1430 error = EADDRNOTAVAIL;
1c79356b
A
1431 }
1432 break;
6d2010ae 1433
39236c6e
A
1434 case SIOCGIFALIFETIME_IN6: /* struct in6_ifreq */
1435 case SIOCSIFALIFETIME_IN6: /* struct in6_ifreq */
1436 error = in6ctl_alifetime(ia, cmd, ifr, p64);
1c79356b
A
1437 break;
1438
39236c6e
A
1439 case SIOCAIFADDR_IN6_32: /* struct in6_aliasreq_32 */
1440 case SIOCAIFADDR_IN6_64: /* struct in6_aliasreq_64 */
1441 error = in6ctl_aifaddr(ifp, ifra);
1c79356b 1442 break;
316670eb 1443
39236c6e
A
1444 case SIOCDIFADDR_IN6:
1445 in6ctl_difaddr(ifp, ia);
1446 break;
1c79356b 1447
39236c6e
A
1448 default:
1449 error = ifnet_ioctl(ifp, PF_INET6, cmd, data);
1c79356b 1450 break;
316670eb
A
1451 }
1452
39236c6e
A
1453done:
1454 if (ia != NULL)
1455 IFA_REMREF(&ia->ia_ifa);
1456 if (so_unlocked)
1457 socket_lock(so, 0);
1c79356b 1458
39236c6e
A
1459 return (error);
1460}
316670eb 1461
39236c6e
A
1462static __attribute__((noinline)) int
1463in6ctl_aifaddr(struct ifnet *ifp, struct in6_aliasreq *ifra)
1464{
1465 int i, error, addtmp, plen;
1466 struct nd_prefix pr0, *pr;
1467 struct in6_ifaddr *ia;
1c79356b 1468
39236c6e
A
1469 VERIFY(ifp != NULL && ifra != NULL);
1470 ia = NULL;
316670eb 1471
39236c6e
A
1472 /* Attempt to attach the protocol, in case it isn't attached */
1473 error = in6_domifattach(ifp);
1474 if (error == 0) {
1475 /* PF_INET6 wasn't previously attached */
1476 error = in6_ifattach_aliasreq(ifp, NULL, NULL);
1477 if (error != 0)
1478 goto done;
1c79356b 1479
39236c6e
A
1480 in6_if_up_dad_start(ifp);
1481 } else if (error != EEXIST) {
1482 goto done;
316670eb
A
1483 }
1484
39236c6e
A
1485 /*
1486 * First, make or update the interface address structure, and link it
1487 * to the list.
1488 */
1489 error = in6_update_ifa(ifp, ifra, 0, &ia);
1490 if (error != 0)
1491 goto done;
1492 VERIFY(ia != NULL);
1c79356b 1493
39236c6e
A
1494 /* Now, make the prefix on-link on the interface. */
1495 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, NULL);
1496 if (plen == 128)
1497 goto done;
316670eb 1498
39236c6e
A
1499 /*
1500 * NOTE: We'd rather create the prefix before the address, but we need
1501 * at least one address to install the corresponding interface route,
1502 * so we configure the address first.
1503 */
b0d623f7 1504
39236c6e
A
1505 /*
1506 * Convert mask to prefix length (prefixmask has already been validated
1507 * in in6_update_ifa().
1508 */
1509 bzero(&pr0, sizeof (pr0));
1510 pr0.ndpr_plen = plen;
1511 pr0.ndpr_ifp = ifp;
1512 pr0.ndpr_prefix = ifra->ifra_addr;
1513 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
1514
1515 /* apply the mask for safety. */
1516 for (i = 0; i < 4; i++) {
1517 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
1518 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
316670eb
A
1519 }
1520
39236c6e
A
1521 /*
1522 * Since we don't have an API to set prefix (not address) lifetimes, we
1523 * just use the same lifetimes as addresses. The (temporarily)
1524 * installed lifetimes can be overridden by later advertised RAs (when
1525 * accept_rtadv is non 0), which is an intended behavior.
1526 */
1527 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
1528 pr0.ndpr_raf_auto = !!(ifra->ifra_flags & IN6_IFF_AUTOCONF);
1529 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
1530 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
1531 pr0.ndpr_stateflags |= NDPRF_STATIC;
1532 lck_mtx_init(&pr0.ndpr_lock, ifa_mtx_grp, ifa_mtx_attr);
1533
3e170ce0
A
1534 /* add the prefix if there's none. */
1535 if ((pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_NEVER)) == NULL) {
39236c6e
A
1536 /*
1537 * nd6_prelist_add will install the corresponding interface
1538 * route.
1539 */
1540 error = nd6_prelist_add(&pr0, NULL, &pr, FALSE);
1541 if (error != 0)
1542 goto done;
b0d623f7 1543
39236c6e
A
1544 if (pr == NULL) {
1545 log(LOG_ERR, "%s: nd6_prelist_add okay, but"
1546 " no prefix.\n", __func__);
1547 error = EINVAL;
1548 goto done;
b0d623f7 1549 }
316670eb 1550 }
1c79356b 1551
39236c6e 1552 IFA_LOCK(&ia->ia_ifa);
316670eb 1553
39236c6e
A
1554 /* if this is a new autoconfed addr */
1555 addtmp = FALSE;
3e170ce0 1556 if (ia->ia6_ndpr == NULL) {
39236c6e
A
1557 NDPR_LOCK(pr);
1558 ++pr->ndpr_addrcnt;
1559 VERIFY(pr->ndpr_addrcnt != 0);
1560 ia->ia6_ndpr = pr;
1561 NDPR_ADDREF_LOCKED(pr); /* for addr reference */
1c79356b 1562
1c79356b 1563 /*
39236c6e
A
1564 * If this is the first autoconf address from the prefix,
1565 * create a temporary address as well (when specified).
1c79356b 1566 */
3e170ce0
A
1567 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
1568 ip6_use_tempaddr &&
1569 pr->ndpr_addrcnt == 1) {
1570 addtmp = true;
1571 }
39236c6e
A
1572 NDPR_UNLOCK(pr);
1573 }
9bccf70c 1574
39236c6e 1575 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 1576
39236c6e
A
1577 if (addtmp) {
1578 int e;
1579 e = in6_tmpifadd(ia, 1);
1580 if (e != 0)
1581 log(LOG_NOTICE, "%s: failed to create a"
1582 " temporary address, error=%d\n",
1583 __func__, e);
1584 }
9bccf70c 1585
39236c6e
A
1586 /*
1587 * This might affect the status of autoconfigured addresses, that is,
1588 * this address might make other addresses detached.
1589 */
1590 lck_mtx_lock(nd6_mutex);
1591 pfxlist_onlink_check();
1592 lck_mtx_unlock(nd6_mutex);
1c79356b 1593
39236c6e
A
1594 /* Drop use count held above during lookup/add */
1595 NDPR_REMREF(pr);
9bccf70c 1596
39236c6e
A
1597done:
1598 if (ia != NULL)
1599 IFA_REMREF(&ia->ia_ifa);
1600 return (error);
1601}
1c79356b 1602
39236c6e
A
1603static __attribute__((noinline)) void
1604in6ctl_difaddr(struct ifnet *ifp, struct in6_ifaddr *ia)
1605{
1606 int i = 0;
1607 struct nd_prefix pr0, *pr;
1c79356b 1608
39236c6e 1609 VERIFY(ifp != NULL && ia != NULL);
2d21ac55 1610
39236c6e
A
1611 /*
1612 * If the address being deleted is the only one that owns
1613 * the corresponding prefix, expire the prefix as well.
1614 * XXX: theoretically, we don't have to worry about such
1615 * relationship, since we separate the address management
1616 * and the prefix management. We do this, however, to provide
1617 * as much backward compatibility as possible in terms of
1618 * the ioctl operation.
1619 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1620 */
1621 IFA_LOCK(&ia->ia_ifa);
1622 bzero(&pr0, sizeof (pr0));
1623 pr0.ndpr_ifp = ifp;
1624 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1625 if (pr0.ndpr_plen == 128) {
1626 IFA_UNLOCK(&ia->ia_ifa);
1627 goto purgeaddr;
1628 }
1629 pr0.ndpr_prefix = ia->ia_addr;
1630 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
1631 for (i = 0; i < 4; i++) {
1632 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
1633 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
9bccf70c 1634 }
39236c6e 1635 IFA_UNLOCK(&ia->ia_ifa);
3e170ce0
A
1636
1637 if ((pr = nd6_prefix_lookup(&pr0, ND6_PREFIX_EXPIRY_UNSPEC)) != NULL) {
39236c6e
A
1638 IFA_LOCK(&ia->ia_ifa);
1639 NDPR_LOCK(pr);
3e170ce0 1640 if (pr->ndpr_addrcnt == 1) {
39236c6e
A
1641 /* XXX: just for expiration */
1642 pr->ndpr_expire = 1;
1643 }
1644 NDPR_UNLOCK(pr);
1645 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 1646
39236c6e
A
1647 /* Drop use count held above during lookup */
1648 NDPR_REMREF(pr);
1c79356b 1649 }
316670eb 1650
39236c6e
A
1651purgeaddr:
1652 in6_purgeaddr(&ia->ia_ifa);
1c79356b
A
1653}
1654
39236c6e 1655static __attribute__((noinline)) int
316670eb
A
1656in6_autoconf(struct ifnet *ifp, int enable)
1657{
1658 int error = 0;
1659
39236c6e
A
1660 VERIFY(ifp != NULL);
1661
316670eb
A
1662 if (ifp->if_flags & IFF_LOOPBACK)
1663 return (EINVAL);
1664
1665 if (enable) {
1666 /*
1667 * An interface in IPv6 router mode implies that it
1668 * is either configured with a static IP address or
1669 * autoconfigured via a locally-generated RA. Prevent
1670 * SIOCAUTOCONF_START from being set in that mode.
1671 */
1672 ifnet_lock_exclusive(ifp);
1673 if (ifp->if_eflags & IFEF_IPV6_ROUTER) {
1674 ifp->if_eflags &= ~IFEF_ACCEPT_RTADV;
1675 error = EBUSY;
1676 } else {
1677 ifp->if_eflags |= IFEF_ACCEPT_RTADV;
1678 }
1679 ifnet_lock_done(ifp);
1680 } else {
1681 struct in6_ifaddr *ia = NULL;
1682
1683 ifnet_lock_exclusive(ifp);
1684 ifp->if_eflags &= ~IFEF_ACCEPT_RTADV;
1685 ifnet_lock_done(ifp);
1686
1687 /* Remove autoconfigured address from interface */
1688 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1689 ia = in6_ifaddrs;
1690 while (ia != NULL) {
1691 if (ia->ia_ifa.ifa_ifp != ifp) {
1692 ia = ia->ia_next;
1693 continue;
1694 }
1695 IFA_LOCK(&ia->ia_ifa);
1696 if (ia->ia6_flags & IN6_IFF_AUTOCONF) {
1697 IFA_ADDREF_LOCKED(&ia->ia_ifa); /* for us */
1698 IFA_UNLOCK(&ia->ia_ifa);
1699 lck_rw_done(&in6_ifaddr_rwlock);
1700 in6_purgeaddr(&ia->ia_ifa);
1701 IFA_REMREF(&ia->ia_ifa); /* for us */
1702 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1703 /*
1704 * Purging the address caused in6_ifaddr_rwlock
1705 * to be dropped and reacquired;
1706 * therefore search again from the beginning
1707 * of in6_ifaddrs list.
1708 */
1709 ia = in6_ifaddrs;
1710 continue;
1711 }
1712 IFA_UNLOCK(&ia->ia_ifa);
1713 ia = ia->ia_next;
1714 }
1715 lck_rw_done(&in6_ifaddr_rwlock);
1716 }
1717 return (error);
1718}
1719
1720/*
1721 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on
1722 * the interface. Entering or exiting this mode will result in the removal of
1723 * autoconfigured IPv6 addresses on the interface.
1724 */
39236c6e 1725static __attribute__((noinline)) int
316670eb
A
1726in6_setrouter(struct ifnet *ifp, int enable)
1727{
39236c6e
A
1728 VERIFY(ifp != NULL);
1729
316670eb
A
1730 if (ifp->if_flags & IFF_LOOPBACK)
1731 return (ENODEV);
1732
1733 if (enable) {
3e170ce0 1734 struct nd_ifinfo *ndi = NULL;
316670eb 1735
316670eb
A
1736 ndi = ND_IFINFO(ifp);
1737 if (ndi != NULL && ndi->initialized) {
1738 lck_mtx_lock(&ndi->lock);
1739 if (ndi->flags & ND6_IFF_PROXY_PREFIXES) {
1740 /* No proxy if we are an advertising router */
1741 ndi->flags &= ~ND6_IFF_PROXY_PREFIXES;
1742 lck_mtx_unlock(&ndi->lock);
316670eb
A
1743 (void) nd6_if_prproxy(ifp, FALSE);
1744 } else {
1745 lck_mtx_unlock(&ndi->lock);
316670eb 1746 }
316670eb
A
1747 }
1748 }
1749
1750 ifnet_lock_exclusive(ifp);
1751 if (enable) {
1752 ifp->if_eflags |= IFEF_IPV6_ROUTER;
1753 } else {
1754 ifp->if_eflags &= ~IFEF_IPV6_ROUTER;
1755 }
1756 ifnet_lock_done(ifp);
1757
1758 lck_mtx_lock(nd6_mutex);
1759 defrouter_select(ifp);
1760 lck_mtx_unlock(nd6_mutex);
1761
1762 if_allmulti(ifp, enable);
1763
1764 return (in6_autoconf(ifp, FALSE));
1765}
1766
39236c6e
A
1767static int
1768in6_to_kamescope(struct sockaddr_in6 *sin6, struct ifnet *ifp)
1c79356b 1769{
39236c6e
A
1770 struct sockaddr_in6 tmp;
1771 int error, id;
1c79356b 1772
39236c6e
A
1773 VERIFY(sin6 != NULL);
1774 tmp = *sin6;
91447636 1775
39236c6e
A
1776 error = in6_recoverscope(&tmp, &sin6->sin6_addr, ifp);
1777 if (error != 0)
1778 return (error);
9bccf70c 1779
39236c6e
A
1780 id = in6_addr2scopeid(ifp, &tmp.sin6_addr);
1781 if (tmp.sin6_scope_id == 0)
1782 tmp.sin6_scope_id = id;
1783 else if (tmp.sin6_scope_id != id)
1784 return (EINVAL); /* scope ID mismatch. */
91447636 1785
39236c6e
A
1786 error = in6_embedscope(&tmp.sin6_addr, &tmp, NULL, NULL, NULL);
1787 if (error != 0)
1788 return (error);
9bccf70c 1789
39236c6e
A
1790 tmp.sin6_scope_id = 0;
1791 *sin6 = tmp;
1792 return (0);
1793}
9bccf70c 1794
39236c6e
A
1795static int
1796in6_ifaupdate_aux(struct in6_ifaddr *ia, struct ifnet *ifp, int ifaupflags)
1797{
1798 struct sockaddr_in6 mltaddr, mltmask;
1799 struct in6_addr llsol;
1800 struct ifaddr *ifa;
1801 struct in6_multi *in6m_sol;
1802 struct in6_multi_mship *imm;
1803 struct rtentry *rt;
3e170ce0 1804 int delay, error = 0;
9bccf70c 1805
39236c6e 1806 VERIFY(ifp != NULL && ia != NULL);
6d2010ae 1807 ifa = &ia->ia_ifa;
39236c6e 1808 in6m_sol = NULL;
9bccf70c 1809
fe8ab488
A
1810 nd6log2((LOG_DEBUG, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
1811 __func__,
1812 ip6_sprintf(&ia->ia_addr.sin6_addr),
1813 if_name(ia->ia_ifp),
1814 ia->ia6_flags,
1815 ifaupflags));
1816
6d2010ae 1817 /*
316670eb 1818 * Mark the address as tentative before joining multicast addresses,
6d2010ae
A
1819 * so that corresponding MLD responses would not have a tentative
1820 * source address.
1821 */
1822 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
39236c6e 1823 if (in6if_do_dad(ifp))
316670eb
A
1824 in6_ifaddr_set_dadprogress(ia);
1825
6d2010ae 1826 /* Join necessary multicast groups */
9bccf70c 1827 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
9bccf70c 1828
6d2010ae 1829 /* join solicited multicast addr for new host id */
39236c6e 1830 bzero(&llsol, sizeof (struct in6_addr));
6d2010ae
A
1831 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1832 llsol.s6_addr32[1] = 0;
1833 llsol.s6_addr32[2] = htonl(1);
39236c6e 1834 llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
6d2010ae
A
1835 llsol.s6_addr8[12] = 0xff;
1836 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1837 /* XXX: should not happen */
39236c6e
A
1838 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
1839 goto unwind;
6d2010ae
A
1840 }
1841 delay = 0;
39236c6e 1842 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY)) {
9bccf70c 1843 /*
6d2010ae
A
1844 * We need a random delay for DAD on the address
1845 * being configured. It also means delaying
1846 * transmission of the corresponding MLD report to
39236c6e 1847 * avoid report collision. [RFC 4862]
9bccf70c 1848 */
39236c6e 1849 delay = random() % MAX_RTR_SOLICITATION_DELAY;
6d2010ae
A
1850 }
1851 imm = in6_joingroup(ifp, &llsol, &error, delay);
1852 if (imm == NULL) {
1853 nd6log((LOG_WARNING,
39236c6e
A
1854 "%s: addmulti failed for %s on %s (errno=%d)\n",
1855 __func__, ip6_sprintf(&llsol), if_name(ifp),
6d2010ae 1856 error));
39236c6e
A
1857 VERIFY(error != 0);
1858 goto unwind;
9bccf70c 1859 }
6d2010ae
A
1860 in6m_sol = imm->i6mm_maddr;
1861 /* take a refcount for this routine */
1862 IN6M_ADDREF(in6m_sol);
1863
1864 IFA_LOCK_SPIN(ifa);
1865 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1866 IFA_UNLOCK(ifa);
9bccf70c 1867
39236c6e
A
1868 bzero(&mltmask, sizeof (mltmask));
1869 mltmask.sin6_len = sizeof (struct sockaddr_in6);
9bccf70c
A
1870 mltmask.sin6_family = AF_INET6;
1871 mltmask.sin6_addr = in6mask32;
6d2010ae 1872#define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
9bccf70c
A
1873
1874 /*
1875 * join link-local all-nodes address
1876 */
39236c6e
A
1877 bzero(&mltaddr, sizeof (mltaddr));
1878 mltaddr.sin6_len = sizeof (struct sockaddr_in6);
9bccf70c
A
1879 mltaddr.sin6_family = AF_INET6;
1880 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
39236c6e
A
1881 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1882 goto unwind; /* XXX: should not fail */
9bccf70c 1883
6d2010ae
A
1884 /*
1885 * XXX: do we really need this automatic routes?
1886 * We should probably reconsider this stuff. Most applications
1887 * actually do not need the routes, since they usually specify
1888 * the outgoing interface.
1889 */
1890 rt = rtalloc1_scoped((struct sockaddr *)&mltaddr, 0, 0UL,
1891 ia->ia_ifp->if_index);
1892 if (rt) {
316670eb
A
1893 if (memcmp(&mltaddr.sin6_addr, &((struct sockaddr_in6 *)
1894 (void *)rt_key(rt))->sin6_addr, MLTMASK_LEN)) {
6d2010ae
A
1895 rtfree(rt);
1896 rt = NULL;
9bccf70c
A
1897 }
1898 }
6d2010ae
A
1899 if (!rt) {
1900 error = rtrequest_scoped(RTM_ADD,
1901 (struct sockaddr *)&mltaddr,
1902 (struct sockaddr *)&ia->ia_addr,
1903 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1904 NULL, ia->ia_ifp->if_index);
1905 if (error)
39236c6e 1906 goto unwind;
6d2010ae
A
1907 } else {
1908 rtfree(rt);
1909 }
1910
1911 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1912 if (!imm) {
1913 nd6log((LOG_WARNING,
39236c6e
A
1914 "%s: addmulti failed for %s on %s (errno=%d)\n",
1915 __func__, ip6_sprintf(&mltaddr.sin6_addr),
6d2010ae 1916 if_name(ifp), error));
39236c6e
A
1917 VERIFY(error != 0);
1918 goto unwind;
6d2010ae
A
1919 }
1920 IFA_LOCK_SPIN(ifa);
1921 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1922 IFA_UNLOCK(ifa);
9bccf70c
A
1923
1924 /*
1925 * join node information group address
1926 */
39236c6e 1927#define hostnamelen strlen(hostname)
6d2010ae 1928 delay = 0;
39236c6e 1929 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY)) {
6d2010ae
A
1930 /*
1931 * The spec doesn't say anything about delay for this
1932 * group, but the same logic should apply.
1933 */
39236c6e 1934 delay = random() % MAX_RTR_SOLICITATION_DELAY;
6d2010ae 1935 }
9bccf70c
A
1936 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1937 == 0) {
6d2010ae
A
1938 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1939 delay); /* XXX jinmei */
1940 if (!imm) {
39236c6e
A
1941 nd6log((LOG_WARNING,
1942 "%s: addmulti failed for %s on %s "
6d2010ae 1943 "(errno=%d)\n",
39236c6e 1944 __func__, ip6_sprintf(&mltaddr.sin6_addr),
6d2010ae
A
1945 if_name(ifp), error));
1946 /* XXX not very fatal, go on... */
39236c6e 1947 error = 0;
6d2010ae
A
1948 } else {
1949 IFA_LOCK_SPIN(ifa);
1950 LIST_INSERT_HEAD(&ia->ia6_memberships,
1951 imm, i6mm_chain);
1952 IFA_UNLOCK(ifa);
9bccf70c
A
1953 }
1954 }
1955#undef hostnamelen
1956
1957 /*
6d2010ae
A
1958 * join interface-local all-nodes address.
1959 * (ff01::1%ifN, and ff01::%ifN/32)
9bccf70c 1960 */
6d2010ae 1961 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
39236c6e
A
1962 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1963 goto unwind; /* XXX: should not fail */
6d2010ae
A
1964 /* XXX: again, do we really need the route? */
1965 rt = rtalloc1_scoped((struct sockaddr *)&mltaddr, 0, 0UL,
1966 ia->ia_ifp->if_index);
1967 if (rt) {
316670eb
A
1968 if (memcmp(&mltaddr.sin6_addr, &((struct sockaddr_in6 *)
1969 (void *)rt_key(rt))->sin6_addr, MLTMASK_LEN)) {
6d2010ae
A
1970 rtfree(rt);
1971 rt = NULL;
9bccf70c
A
1972 }
1973 }
6d2010ae
A
1974 if (!rt) {
1975 error = rtrequest_scoped(RTM_ADD,
1976 (struct sockaddr *)&mltaddr,
1977 (struct sockaddr *)&ia->ia_addr,
1978 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1979 NULL, ia->ia_ifp->if_index);
1980 if (error)
39236c6e 1981 goto unwind;
6d2010ae
A
1982 } else
1983 rtfree(rt);
1984
1985 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1986 if (!imm) {
39236c6e
A
1987 nd6log((LOG_WARNING,
1988 "%s: addmulti failed for %s on %s (errno=%d)\n",
1989 __func__, ip6_sprintf(&mltaddr.sin6_addr),
6d2010ae 1990 if_name(ifp), error));
39236c6e
A
1991 VERIFY(error != 0);
1992 goto unwind;
6d2010ae
A
1993 }
1994 IFA_LOCK(ifa);
1995 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
39236c6e 1996 IFA_UNLOCK(ifa);
9bccf70c 1997 }
39236c6e
A
1998#undef MLTMASK_LEN
1999
39236c6e
A
2000 /* Ensure nd6_service() is scheduled as soon as it's convenient */
2001 ++nd6_sched_timeout_want;
9bccf70c
A
2002
2003 /*
2004 * Perform DAD, if needed.
2005 * XXX It may be of use, if we can administratively
2006 * disable DAD.
2007 */
39236c6e
A
2008 IFA_LOCK_SPIN(ifa);
2009 if (in6if_do_dad(ifp) && ((ifa->ifa_flags & IN6_IFF_NODAD) == 0) &&
2010 (ia->ia6_flags & IN6_IFF_DADPROGRESS)) {
6d2010ae 2011 int mindelay, maxdelay;
316670eb 2012 int *delayptr, delayval;
9bccf70c 2013
6d2010ae 2014 IFA_UNLOCK(ifa);
39236c6e 2015 delayptr = NULL;
fe8ab488
A
2016 /*
2017 * Avoid the DAD delay if the caller wants us to skip it.
2018 * This is not compliant with RFC 2461, but it's only being
2019 * used for signalling and not for actual DAD.
2020 */
2021 if ((ifaupflags & IN6_IFAUPDATE_DADDELAY) &&
2022 !(ia->ia6_flags & IN6_IFF_SWIFTDAD)) {
39236c6e
A
2023 /*
2024 * We need to impose a delay before sending an NS
2025 * for DAD. Check if we also needed a delay for the
2026 * corresponding MLD message. If we did, the delay
2027 * should be larger than the MLD delay (this could be
2028 * relaxed a bit, but this simple logic is at least
2029 * safe).
2030 */
2031 mindelay = 0;
2032 if (in6m_sol != NULL) {
2033 IN6M_LOCK(in6m_sol);
2034 if (in6m_sol->in6m_state ==
2035 MLD_REPORTING_MEMBER)
2036 mindelay = in6m_sol->in6m_timer;
2037 IN6M_UNLOCK(in6m_sol);
2038 }
2039 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
2040 if (maxdelay - mindelay == 0)
2041 delayval = 0;
2042 else {
2043 delayval =
2044 (random() % (maxdelay - mindelay)) +
2045 mindelay;
2046 }
2047 delayptr = &delayval;
2048 }
2049
2050 nd6_dad_start((struct ifaddr *)ia, delayptr);
2051 } else {
2052 IFA_UNLOCK(ifa);
2053 }
2054
2055 goto done;
2056
2057unwind:
2058 VERIFY(error != 0);
2059 in6_purgeaddr(&ia->ia_ifa);
2060
2061done:
2062 /* release reference held for this routine */
2063 if (in6m_sol != NULL)
2064 IN6M_REMREF(in6m_sol);
2065 return (error);
2066}
2067
2068/*
2069 * Request an IPv6 interface address. If the address is new, then it will be
2070 * constructed and appended to the interface address chains. The interface
2071 * address structure is optionally returned with a reference for the caller.
2072 */
2073int
2074in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int ifaupflags,
2075 struct in6_ifaddr **iar)
2076{
2077 struct in6_addrlifetime ia6_lt;
2078 struct in6_ifaddr *ia;
2079 struct ifaddr *ifa;
2080 struct ifaddr *xifa;
2081 struct in6_addrlifetime *lt;
2082 uint64_t timenow;
2083 int plen, error;
2084
2085 /* Sanity check parameters and initialize locals */
2086 VERIFY(ifp != NULL && ifra != NULL && iar != NULL);
2087 ia = NULL;
2088 ifa = NULL;
2089 error = 0;
2090
2091 /*
2092 * We always require users to specify a valid IPv6 address for
2093 * the corresponding operation.
2094 */
2095 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
2096 ifra->ifra_addr.sin6_len != sizeof (struct sockaddr_in6)) {
2097 error = EAFNOSUPPORT;
2098 goto unwind;
2099 }
2100
2101 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2102 if (ifra->ifra_prefixmask.sin6_len == 0 ||
2103 ifra->ifra_prefixmask.sin6_len > sizeof (struct sockaddr_in6)) {
2104 error = EINVAL;
2105 goto unwind;
2106 }
2107
2108 /* Validate prefix length extracted from ifra_prefixmask structure. */
2109 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
2110 (u_char *)&ifra->ifra_prefixmask + ifra->ifra_prefixmask.sin6_len);
2111 if (plen <= 0) {
2112 error = EINVAL;
2113 goto unwind;
2114 }
2115
2116 /* Validate lifetimes */
2117 lt = &ifra->ifra_lifetime;
2118 if (lt->ia6t_pltime > lt->ia6t_vltime) {
2119 log(LOG_INFO,
2120 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__,
2121 lt->ia6t_pltime, lt->ia6t_vltime,
2122 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
2123 error = EINVAL;
2124 goto unwind;
2125 }
2126 if (lt->ia6t_vltime == 0) {
2127 /*
2128 * the following log might be noisy, but this is a typical
2129 * configuration mistake or a tool's bug.
2130 */
2131 log(LOG_INFO, "%s: valid lifetime is 0 for %s\n", __func__,
2132 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
2133 }
2134
2135 /*
2136 * Before we lock the ifnet structure, we first check to see if the
2137 * address already exists. If so, then we don't allocate and link a
2138 * new one here.
2139 */
2140 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
2141 if (ia != NULL)
2142 ifa = &ia->ia_ifa;
2143
2144 /*
2145 * Validate destination address on interface types that require it.
2146 */
2147 if ((ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) != 0) {
2148 switch (ifra->ifra_dstaddr.sin6_family) {
2149 case AF_INET6:
2150 if (plen != 128) {
2151 /* noisy message for diagnostic purposes */
2152 log(LOG_INFO,
2153 "%s: prefix length < 128 with"
2154 " explicit dstaddr.\n", __func__);
2155 error = EINVAL;
2156 goto unwind;
2157 }
2158 break;
2159
2160 case AF_UNSPEC:
2161 break;
2162
2163 default:
2164 error = EAFNOSUPPORT;
2165 goto unwind;
2166 }
2167 } else if (ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) {
2168 log(LOG_INFO,
2169 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2170 __func__);
2171 error = EINVAL;
2172 goto unwind;
2173 }
2174
2175 timenow = net_uptime();
2176
2177 if (ia == NULL) {
2178 int how;
2179
2180 /* Is this the first new IPv6 address for the interface? */
2181 ifaupflags |= IN6_IFAUPDATE_NEWADDR;
2182
2183 /* Allocate memory for IPv6 interface address structure. */
2184 how = !(ifaupflags & IN6_IFAUPDATE_NOWAIT) ? M_WAITOK : 0;
2185 ia = in6_ifaddr_alloc(how);
2186 if (ia == NULL) {
2187 error = ENOBUFS;
2188 goto unwind;
2189 }
2190
2191 ifa = &ia->ia_ifa;
2192
2193 /*
2194 * Initialize interface address structure.
2195 *
2196 * Note well: none of these sockaddr_in6 structures contain a
2197 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2198 * We still embed link-local scope identifiers at the end of an
2199 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2200 * ifa_dstaddr field is always non-NULL on point-to-point and
2201 * loopback interfaces, and conventionally points to a socket
2202 * address of AF_UNSPEC family when there is no destination.
2203 *
2204 * Please enjoy the dancing sea turtle.
2205 */
2206 IFA_ADDREF(ifa); /* for this and optionally for caller */
2207 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
2208 if (ifra->ifra_dstaddr.sin6_family == AF_INET6 ||
2209 (ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0)
2210 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
2211 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
2212 ifa->ifa_ifp = ifp;
2213 ifa->ifa_metric = ifp->if_metric;
2214 ifa->ifa_rtrequest = nd6_rtrequest;
2215
2216 LIST_INIT(&ia->ia6_memberships);
2217 ia->ia_addr.sin6_family = AF_INET6;
2218 ia->ia_addr.sin6_len = sizeof (ia->ia_addr);
2219 ia->ia_addr.sin6_addr = ifra->ifra_addr.sin6_addr;
2220 ia->ia_prefixmask.sin6_family = AF_INET6;
2221 ia->ia_prefixmask.sin6_len = sizeof (ia->ia_prefixmask);
2222 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr;
2223 error = in6_to_kamescope(&ia->ia_addr, ifp);
2224 if (error != 0)
2225 goto unwind;
2226 if (ifa->ifa_dstaddr != NULL) {
2227 ia->ia_dstaddr = ifra->ifra_dstaddr;
2228 error = in6_to_kamescope(&ia->ia_dstaddr, ifp);
2229 if (error != 0)
2230 goto unwind;
2231 }
2232
2233 /* Append to address chains */
2234 ifnet_lock_exclusive(ifp);
2235 ifaupflags |= IN6_IFAUPDATE_1STADDR;
2236 TAILQ_FOREACH(xifa, &ifp->if_addrlist, ifa_list) {
2237 IFA_LOCK_SPIN(xifa);
2238 if (xifa->ifa_addr->sa_family != AF_INET6) {
2239 IFA_UNLOCK(xifa);
2240 ifaupflags &= ~IN6_IFAUPDATE_1STADDR;
2241 break;
2242 }
2243 IFA_UNLOCK(xifa);
2244 }
2245
2246 IFA_LOCK_SPIN(ifa);
2247 if_attach_ifa(ifp, ifa); /* holds reference for ifnet link */
2248 IFA_UNLOCK(ifa);
2249 ifnet_lock_done(ifp);
2250
2251 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
2252 if (in6_ifaddrs != NULL) {
2253 struct in6_ifaddr *iac;
2254 for (iac = in6_ifaddrs; iac->ia_next != NULL;
2255 iac = iac->ia_next)
2256 continue;
2257 iac->ia_next = ia;
2258 } else {
2259 in6_ifaddrs = ia;
6d2010ae 2260 }
39236c6e
A
2261 IFA_ADDREF(ifa); /* hold for in6_ifaddrs link */
2262 lck_rw_done(&in6_ifaddr_rwlock);
6d2010ae 2263 } else {
39236c6e
A
2264 ifa = &ia->ia_ifa;
2265 ifaupflags &= ~(IN6_IFAUPDATE_NEWADDR|IN6_IFAUPDATE_1STADDR);
6d2010ae 2266 }
9bccf70c 2267
39236c6e
A
2268 VERIFY(ia != NULL && ifa == &ia->ia_ifa);
2269 IFA_LOCK(ifa);
2270
2271 /*
2272 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2273 * to see if the address is deprecated or invalidated, but initialize
2274 * these members for applications.
2275 */
2276 ia->ia6_updatetime = ia->ia6_createtime = timenow;
2277 ia6_lt = *lt;
2278 if (ia6_lt.ia6t_vltime != ND6_INFINITE_LIFETIME)
2279 ia6_lt.ia6t_expire = timenow + ia6_lt.ia6t_vltime;
2280 else
2281 ia6_lt.ia6t_expire = 0;
2282 if (ia6_lt.ia6t_pltime != ND6_INFINITE_LIFETIME)
2283 ia6_lt.ia6t_preferred = timenow + ia6_lt.ia6t_pltime;
2284 else
2285 ia6_lt.ia6t_preferred = 0;
2286 in6ifa_setlifetime(ia, &ia6_lt);
2287
9bccf70c 2288 /*
39236c6e
A
2289 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2290 * userland, make it deprecated.
9bccf70c 2291 */
39236c6e
A
2292 if ((ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
2293 ia->ia6_lifetime.ia6ti_pltime = 0;
2294 ia->ia6_lifetime.ia6ti_preferred = timenow;
6d2010ae 2295 }
6d2010ae 2296
39236c6e
A
2297 /*
2298 * Do not delay sending neighbor solicitations when using optimistic
2299 * duplicate address detection, c.f. RFC 4429.
2300 */
2301 if ((ia->ia6_flags & IN6_IFF_OPTIMISTIC) == 0)
2302 ifaupflags |= IN6_IFAUPDATE_DADDELAY;
2303
2304 /*
2305 * Update flag or prefix length
2306 */
2307 ia->ia_plen = plen;
2308 ia->ia6_flags = ifra->ifra_flags;
2309
2310 /* Release locks (new address available to concurrent tasks) */
2311 IFA_UNLOCK(ifa);
2312
2313 /* Further initialization of the interface address */
2314 error = in6_ifinit(ifp, ia, ifaupflags);
2315 if (error != 0)
2316 goto unwind;
2317
2318 /* Finish updating the address while other tasks are working with it */
2319 error = in6_ifaupdate_aux(ia, ifp, ifaupflags);
2320 if (error != 0)
2321 goto unwind;
2322
2323 /* Return success (optionally w/ address for caller). */
2324 VERIFY(error == 0);
2325 (void) ifnet_notify_address(ifp, AF_INET6);
6d2010ae 2326 goto done;
39236c6e
A
2327
2328unwind:
2329 VERIFY(error != 0);
2330 if (ia != NULL) {
2331 VERIFY(ifa == &ia->ia_ifa);
2332 IFA_REMREF(ifa);
2333 ia = NULL;
2334 }
2335
2336done:
2337 *iar = ia;
2338 return (error);
9bccf70c
A
2339}
2340
2341void
6d2010ae 2342in6_purgeaddr(struct ifaddr *ifa)
9bccf70c
A
2343{
2344 struct ifnet *ifp = ifa->ifa_ifp;
39236c6e 2345 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
6d2010ae
A
2346 struct in6_multi_mship *imm;
2347
2348 lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
9bccf70c
A
2349
2350 /* stop DAD processing */
55e303ae 2351 nd6_dad_stop(ifa);
9bccf70c
A
2352
2353 /*
2354 * delete route to the destination of the address being purged.
2355 * The interface must be p2p or loopback in this case.
2356 */
6d2010ae 2357 IFA_LOCK(ifa);
39236c6e
A
2358 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_plen == 128) {
2359 int error, rtf;
9bccf70c 2360
6d2010ae 2361 IFA_UNLOCK(ifa);
39236c6e
A
2362 rtf = (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0;
2363 error = rtinit(&(ia->ia_ifa), RTM_DELETE, rtf);
2364 if (error != 0) {
9bccf70c
A
2365 log(LOG_ERR, "in6_purgeaddr: failed to remove "
2366 "a route to the p2p destination: %s on %s, "
2367 "errno=%d\n",
2368 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
39236c6e 2369 error);
9bccf70c 2370 /* proceed anyway... */
6d2010ae 2371 }
39236c6e
A
2372 IFA_LOCK_SPIN(ifa);
2373 ia->ia_flags &= ~IFA_ROUTE;
9bccf70c 2374 }
39236c6e 2375 IFA_UNLOCK(ifa);
9bccf70c
A
2376
2377 /* Remove ownaddr's loopback rtentry, if it exists. */
6d2010ae 2378 in6_ifremloop(&(ia->ia_ifa));
1c79356b 2379
6d2010ae
A
2380 /*
2381 * leave from multicast groups we have joined for the interface
2382 */
2383 IFA_LOCK(ifa);
2384 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
2385 LIST_REMOVE(imm, i6mm_chain);
2386 IFA_UNLOCK(ifa);
2387 in6_leavegroup(imm);
2388 IFA_LOCK(ifa);
1c79356b 2389 }
6d2010ae 2390 IFA_UNLOCK(ifa);
1c79356b 2391
6d2010ae
A
2392 /* in6_unlink_ifa() will need exclusive access */
2393 in6_unlink_ifa(ia, ifp);
fe8ab488 2394 in6_post_msg(ifp, KEV_INET6_ADDR_DELETED, ia, NULL);
39236c6e
A
2395
2396 (void) ifnet_notify_address(ifp, AF_INET6);
9bccf70c
A
2397}
2398
2399static void
6d2010ae 2400in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
9bccf70c 2401{
9bccf70c 2402 struct in6_ifaddr *oia;
6d2010ae
A
2403 struct ifaddr *ifa;
2404 int unlinked;
2405
2406 lck_mtx_assert(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
2407
2408 ifa = &ia->ia_ifa;
2409 IFA_ADDREF(ifa);
9bccf70c 2410
91447636 2411 ifnet_lock_exclusive(ifp);
6d2010ae
A
2412 IFA_LOCK(ifa);
2413 if (ifa->ifa_debug & IFD_ATTACHED)
2414 if_detach_ifa(ifp, ifa);
2415 IFA_UNLOCK(ifa);
91447636 2416 ifnet_lock_done(ifp);
1c79356b 2417
6d2010ae
A
2418 unlinked = 1;
2419 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
1c79356b 2420 oia = ia;
6d2010ae 2421 if (oia == (ia = in6_ifaddrs)) {
91447636 2422 in6_ifaddrs = ia->ia_next;
6d2010ae 2423 } else {
1c79356b
A
2424 while (ia->ia_next && (ia->ia_next != oia))
2425 ia = ia->ia_next;
6d2010ae 2426 if (ia->ia_next) {
1c79356b 2427 ia->ia_next = oia->ia_next;
6d2010ae 2428 } else {
9bccf70c 2429 /* search failed */
39236c6e 2430 log(LOG_NOTICE, "%s: search failed.\n", __func__);
6d2010ae 2431 unlinked = 0;
9bccf70c 2432 }
1c79356b 2433 }
1c79356b 2434
9bccf70c 2435 /*
3e170ce0
A
2436 * When IPv6 address is being removed, release the
2437 * reference to the base prefix.
2438 * Also, since the release might, affect the status
2439 * of other (detached) addresses, call
9bccf70c
A
2440 * pfxlist_onlink_check().
2441 */
6d2010ae
A
2442 ifa = &oia->ia_ifa;
2443 IFA_LOCK(ifa);
3e170ce0
A
2444 if (oia->ia6_ndpr == NULL) {
2445 log(LOG_NOTICE, "in6_unlink_ifa: IPv6 address "
2446 "0x%llx has no prefix\n",
2447 (uint64_t)VM_KERNEL_ADDRPERM(oia));
2448 } else {
2449 struct nd_prefix *pr = oia->ia6_ndpr;
2450 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
2451 oia->ia6_ndpr = NULL;
2452 NDPR_LOCK(pr);
2453 VERIFY(pr->ndpr_addrcnt != 0);
2454 pr->ndpr_addrcnt--;
2455 NDPR_UNLOCK(pr);
2456 NDPR_REMREF(pr); /* release addr reference */
2457 }
2458 IFA_UNLOCK(ifa);
2459 lck_rw_done(&in6_ifaddr_rwlock);
6d2010ae 2460
3e170ce0 2461 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
6d2010ae
A
2462 lck_mtx_lock(nd6_mutex);
2463 pfxlist_onlink_check();
91447636 2464 lck_mtx_unlock(nd6_mutex);
6d2010ae 2465 }
9bccf70c 2466 /*
91447636 2467 * release another refcnt for the link from in6_ifaddrs.
6d2010ae
A
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.
9bccf70c 2470 */
6d2010ae
A
2471 if (unlinked)
2472 IFA_REMREF(ifa);
9bccf70c 2473
6d2010ae
A
2474 /* release reference held for this routine */
2475 IFA_REMREF(ifa);
39236c6e
A
2476
2477 /* invalidate route caches */
2478 routegenid_inet6_update();
9bccf70c
A
2479}
2480
2481void
6d2010ae 2482in6_purgeif(struct ifnet *ifp)
9bccf70c 2483{
6d2010ae 2484 struct in6_ifaddr *ia;
9bccf70c 2485
6d2010ae 2486 if (ifp == NULL)
9bccf70c 2487 return;
6d2010ae
A
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;
9bccf70c 2496 continue;
6d2010ae
A
2497 }
2498 IFA_ADDREF(&ia->ia_ifa); /* for us */
2499 lck_rw_done(&in6_ifaddr_rwlock);
2500 in6_purgeaddr(&ia->ia_ifa);
6d2010ae 2501 IFA_REMREF(&ia->ia_ifa); /* for us */
316670eb 2502 lck_rw_lock_exclusive(&in6_ifaddr_rwlock);
6d2010ae
A
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;
9bccf70c 2510 }
6d2010ae 2511 lck_rw_done(&in6_ifaddr_rwlock);
9bccf70c
A
2512
2513 in6_ifdetach(ifp);
1c79356b
A
2514}
2515
1c79356b 2516/*
39236c6e 2517 * Initialize an interface's internet6 address and routing table entry.
1c79356b 2518 */
9bccf70c 2519static int
39236c6e 2520in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, int ifaupflags)
1c79356b 2521{
39236c6e 2522 int error;
9bccf70c 2523 struct ifaddr *ifa;
1c79356b 2524
39236c6e 2525 error = 0;
6d2010ae 2526 ifa = &ia->ia_ifa;
1c79356b 2527
316670eb
A
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 */
39236c6e
A
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 }
1c79356b 2540 }
1c79356b 2541
6d2010ae 2542 IFA_LOCK(ifa);
1c79356b 2543
9bccf70c
A
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 */
39236c6e
A
2550 if (!(ia->ia_flags & IFA_ROUTE) && ia->ia_plen == 128 &&
2551 ia->ia_dstaddr.sin6_family == AF_INET6) {
6d2010ae 2552 IFA_UNLOCK(ifa);
39236c6e
A
2553 error = rtinit(ifa, RTM_ADD, RTF_UP | RTF_HOST);
2554 if (error != 0)
2555 return (error);
6d2010ae 2556 IFA_LOCK(ifa);
9bccf70c
A
2557 ia->ia_flags |= IFA_ROUTE;
2558 }
6d2010ae 2559 IFA_LOCK_ASSERT_HELD(ifa);
39236c6e 2560 if (ia->ia_plen < 128) {
1c79356b 2561 /*
9bccf70c 2562 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1c79356b 2563 */
39236c6e 2564 ia->ia_flags |= RTF_CLONING;
1c79356b 2565 }
9bccf70c 2566
39236c6e
A
2567 IFA_UNLOCK(ifa);
2568
55e303ae 2569 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
39236c6e
A
2570 if ((ifaupflags & IN6_IFAUPDATE_NEWADDR) != 0)
2571 in6_ifaddloop(ifa);
9bccf70c 2572
39236c6e
A
2573 /* invalidate route caches */
2574 routegenid_inet6_update();
2575
2576 VERIFY(error == 0);
2577 return (0);
1c79356b 2578}
9bccf70c 2579
1c79356b 2580void
6d2010ae 2581in6_purgeaddrs(struct ifnet *ifp)
1c79356b 2582{
6d2010ae 2583 in6_purgeif(ifp);
1c79356b 2584}
1c79356b
A
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{
9bccf70c 2594 struct ifaddr *ifa;
1c79356b 2595
91447636 2596 ifnet_lock_shared(ifp);
9bccf70c 2597 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1c79356b 2598 {
6d2010ae
A
2599 IFA_LOCK_SPIN(ifa);
2600 if (ifa->ifa_addr->sa_family != AF_INET6) {
2601 IFA_UNLOCK(ifa);
1c79356b 2602 continue;
6d2010ae 2603 }
1c79356b
A
2604 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
2605 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
39236c6e 2606 ignoreflags) != 0) {
6d2010ae 2607 IFA_UNLOCK(ifa);
1c79356b 2608 continue;
6d2010ae
A
2609 }
2610 IFA_ADDREF_LOCKED(ifa); /* for caller */
2611 IFA_UNLOCK(ifa);
1c79356b
A
2612 break;
2613 }
6d2010ae 2614 IFA_UNLOCK(ifa);
1c79356b 2615 }
91447636 2616 ifnet_lock_done(ifp);
1c79356b 2617
39236c6e 2618 return ((struct in6_ifaddr *)ifa);
1c79356b
A
2619}
2620
1c79356b
A
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{
9bccf70c 2629 struct ifaddr *ifa;
1c79356b 2630
91447636 2631 ifnet_lock_shared(ifp);
9bccf70c 2632 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1c79356b 2633 {
6d2010ae
A
2634 IFA_LOCK_SPIN(ifa);
2635 if (ifa->ifa_addr->sa_family != AF_INET6) {
2636 IFA_UNLOCK(ifa);
1c79356b 2637 continue;
6d2010ae
A
2638 }
2639 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
2640 IFA_ADDREF_LOCKED(ifa); /* for caller */
2641 IFA_UNLOCK(ifa);
1c79356b 2642 break;
6d2010ae
A
2643 }
2644 IFA_UNLOCK(ifa);
1c79356b 2645 }
91447636 2646 ifnet_lock_done(ifp);
1c79356b 2647
39236c6e 2648 return ((struct in6_ifaddr *)ifa);
1c79356b
A
2649}
2650
316670eb
A
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
39236c6e
A
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
1c79356b
A
2736/*
2737 * Convert IP6 address to printable (loggable) representation.
2738 */
1c79356b 2739char *
39236c6e 2740ip6_sprintf(const struct in6_addr *addr)
1c79356b 2741{
39236c6e
A
2742 static const char digits[] = "0123456789abcdef";
2743 static int ip6round = 0;
1c79356b 2744 static char ip6buf[8][48];
39236c6e 2745
9bccf70c
A
2746 int i;
2747 char *cp;
55e303ae
A
2748 const u_short *a = (const u_short *)addr;
2749 const u_char *d;
39236c6e 2750 u_char n;
1c79356b 2751 int dcolon = 0;
39236c6e 2752 int zpad = 0;
1c79356b
A
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 }
55e303ae 2780 d = (const u_char *)a;
39236c6e
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];
1c79356b
A
2796 *cp++ = ':';
2797 a++;
2798 }
2799 *--cp = 0;
39236c6e 2800 return (ip6buf[ip6round]);
1c79356b
A
2801}
2802
2d21ac55
A
2803int
2804in6addr_local(struct in6_addr *in6)
2805{
2806 struct rtentry *rt;
2807 struct sockaddr_in6 sin6;
2808 int local = 0;
2809
6d2010ae 2810 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_SCOPE_LINKLOCAL(in6))
2d21ac55
A
2811 return (1);
2812
2813 sin6.sin6_family = AF_INET6;
2814 sin6.sin6_len = sizeof (sin6);
2815 bcopy(in6, &sin6.sin6_addr, sizeof (*in6));
b0d623f7 2816 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0);
2d21ac55
A
2817
2818 if (rt != NULL) {
b0d623f7 2819 RT_LOCK_SPIN(rt);
2d21ac55
A
2820 if (rt->rt_gateway->sa_family == AF_LINK)
2821 local = 1;
b0d623f7 2822 RT_UNLOCK(rt);
2d21ac55
A
2823 rtfree(rt);
2824 } else {
2825 local = in6_localaddr(in6);
2826 }
2827 return (local);
2828}
2829
1c79356b 2830int
6d2010ae 2831in6_localaddr(struct in6_addr *in6)
1c79356b
A
2832{
2833 struct in6_ifaddr *ia;
2834
2835 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
6d2010ae 2836 return (1);
1c79356b 2837
6d2010ae
A
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);
1c79356b 2841 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
6d2010ae
A
2842 &ia->ia_prefixmask.sin6_addr)) {
2843 IFA_UNLOCK(&ia->ia_ifa);
2844 lck_rw_done(&in6_ifaddr_rwlock);
2845 return (1);
91447636 2846 }
6d2010ae
A
2847 IFA_UNLOCK(&ia->ia_ifa);
2848 }
2849 lck_rw_done(&in6_ifaddr_rwlock);
1c79356b
A
2850 return (0);
2851}
2852
1c79356b 2853int
6d2010ae 2854in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1c79356b 2855{
9bccf70c 2856 struct in6_ifaddr *ia;
1c79356b 2857
6d2010ae 2858 lck_rw_lock_shared(&in6_ifaddr_rwlock);
91447636 2859 for (ia = in6_ifaddrs; ia; ia = ia->ia_next) {
6d2010ae 2860 IFA_LOCK_SPIN(&ia->ia_ifa);
9bccf70c 2861 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
6d2010ae 2862 &sa6->sin6_addr) &&
91447636 2863 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
6d2010ae
A
2864 IFA_UNLOCK(&ia->ia_ifa);
2865 lck_rw_done(&in6_ifaddr_rwlock);
39236c6e 2866 return (1); /* true */
91447636 2867 }
9bccf70c 2868 /* XXX: do we still have to go thru the rest of the list? */
6d2010ae 2869 IFA_UNLOCK(&ia->ia_ifa);
1c79356b
A
2870 }
2871
6d2010ae 2872 lck_rw_done(&in6_ifaddr_rwlock);
39236c6e 2873 return (0); /* false */
1c79356b
A
2874}
2875
2876/*
2877 * return length of part which dst and src are equal
2878 * hard coding...
2879 */
1c79356b
A
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;
39236c6e 2897 return (match);
1c79356b
A
2898}
2899
9bccf70c 2900/* XXX: to be scope conscious */
1c79356b
A
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) {
39236c6e
A
2910 log(LOG_ERR, "%s: invalid prefix length(%d)\n", __func__, len);
2911 return (0);
1c79356b
A
2912 }
2913
2914 bytelen = len / 8;
2915 bitlen = len % 8;
2916
2917 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
39236c6e
A
2918 return (0);
2919 if (bitlen != 0 &&
91447636 2920 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1c79356b 2921 p2->s6_addr[bytelen] >> (8 - bitlen))
39236c6e 2922 return (0);
1c79356b 2923
39236c6e 2924 return (1);
1c79356b
A
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) {
39236c6e 2937 log(LOG_ERR, "%s: invalid prefix length(%d)\n", __func__, len);
1c79356b
A
2938 return;
2939 }
2940
39236c6e 2941 bzero(maskp, sizeof (*maskp));
1c79356b
A
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 *
39236c6e 2954in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1c79356b
A
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;
39236c6e 2961
1c79356b 2962 if (oifp == NULL) {
39236c6e 2963 return (NULL);
1c79356b
A
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 */
91447636
A
2971 ifnet_head_lock_shared();
2972 TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
1c79356b
A
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
91447636 2980 ifnet_lock_shared(ifp);
39236c6e 2981 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1c79356b
A
2982 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2983
6d2010ae
A
2984 IFA_LOCK(ifa);
2985 if (ifa->ifa_addr->sa_family != AF_INET6) {
2986 IFA_UNLOCK(ifa);
1c79356b 2987 continue;
6d2010ae 2988 }
1c79356b
A
2989 src_scope = in6_addrscope(IFA_IN6(ifa));
2990
1c79356b
A
2991 /*
2992 * Don't use an address before completing DAD
2993 * nor a duplicated address.
2994 */
2995 if (((struct in6_ifaddr *)ifa)->ia6_flags &
6d2010ae
A
2996 IN6_IFF_NOTREADY) {
2997 IFA_UNLOCK(ifa);
1c79356b 2998 continue;
6d2010ae 2999 }
1c79356b
A
3000 /* XXX: is there any case to allow anycasts? */
3001 if (((struct in6_ifaddr *)ifa)->ia6_flags &
6d2010ae
A
3002 IN6_IFF_ANYCAST) {
3003 IFA_UNLOCK(ifa);
1c79356b 3004 continue;
6d2010ae 3005 }
1c79356b 3006 if (((struct in6_ifaddr *)ifa)->ia6_flags &
6d2010ae
A
3007 IN6_IFF_DETACHED) {
3008 IFA_UNLOCK(ifa);
1c79356b 3009 continue;
6d2010ae 3010 }
1c79356b
A
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.
9bccf70c
A
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))
1c79356b
A
3037 */
3038 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
3039 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
9bccf70c 3040 goto replace; /* (A) */
1c79356b 3041 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
6d2010ae
A
3042 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0) {
3043 IFA_UNLOCK(ifa);
9bccf70c 3044 continue; /* (B) */
6d2010ae 3045 }
1c79356b
A
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.
39236c6e 3050 * RFC 4862, Section 5.5.4.
1c79356b
A
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 */
6d2010ae
A
3058 if (!ip6_use_deprecated) {
3059 IFA_UNLOCK(ifa);
1c79356b 3060 continue;
6d2010ae 3061 }
1c79356b
A
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)
6d2010ae
A
3067 == 0) {
3068 IFA_UNLOCK(ifa);
1c79356b 3069 continue;
6d2010ae 3070 }
1c79356b
A
3071 }
3072
3073 /*
3074 * A non-deprecated address is always preferred
3075 * to a deprecated one regardless of scopes and
9bccf70c
A
3076 * address matching (Note invariants ensured by the
3077 * conditions (A) and (B) above.)
1c79356b
A
3078 */
3079 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
3080 (((struct in6_ifaddr *)ifa)->ia6_flags &
39236c6e 3081 IN6_IFF_DEPRECATED) == 0)
1c79356b
A
3082 goto replace;
3083
9bccf70c
A
3084 /*
3085 * When we use temporary addresses described in
6d2010ae 3086 * RFC 4941, we prefer temporary addresses to
9bccf70c
A
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 &
39236c6e
A
3098 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
3099 == IN6_IFF_AUTOCONF &&
9bccf70c 3100 (ifat->ia6_flags &
39236c6e
A
3101 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
3102 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
9bccf70c
A
3103 goto replace;
3104 }
3105 if ((ifa_best->ia6_flags &
39236c6e 3106 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
9bccf70c
A
3107 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
3108 (ifat->ia6_flags &
39236c6e
A
3109 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
3110 == IN6_IFF_AUTOCONF) {
6d2010ae 3111 IFA_UNLOCK(ifa);
9bccf70c
A
3112 continue;
3113 }
3114 }
3115
1c79356b
A
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
9bccf70c
A
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.
1c79356b 3150 * - If there is no other reasons to choose one,
9bccf70c 3151 * longest address match against dst is considered.
1c79356b
A
3152 *
3153 * The precise decision table is as follows:
39236c6e
A
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)
1c79356b
A
3165 */
3166 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
3167 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
3168
9bccf70c
A
3169 if (bscopecmp == 0) {
3170 struct ifnet *bifp = ifa_best->ia_ifp;
3171
6d2010ae
A
3172 if (bifp == oifp && ifp != oifp) { /* (1) */
3173 IFA_UNLOCK(ifa);
9bccf70c 3174 continue;
6d2010ae 3175 }
9bccf70c
A
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) */
1c79356b 3189 goto replace;
6d2010ae 3190 IFA_UNLOCK(ifa);
9bccf70c 3191 continue; /* (4) */
1c79356b
A
3192 }
3193 if (dscopecmp > 0) {
6d2010ae
A
3194 if (bscopecmp > 0) { /* (5) */
3195 IFA_UNLOCK(ifa);
1c79356b 3196 continue;
6d2010ae 3197 }
9bccf70c 3198 goto replace; /* (6) */
1c79356b
A
3199 }
3200 if (dscopecmp < 0) {
9bccf70c 3201 if (bscopecmp > 0) /* (7) */
1c79356b 3202 goto replace;
6d2010ae 3203 IFA_UNLOCK(ifa);
9bccf70c 3204 continue; /* (8) */
1c79356b
A
3205 }
3206
3207 /* now dscopecmp must be 0 */
3208 if (bscopecmp < 0)
9bccf70c 3209 goto replace; /* (9) */
1c79356b 3210
6d2010ae
A
3211replace:
3212 IFA_ADDREF_LOCKED(ifa); /* for ifa_best */
1c79356b 3213 blen = tlen >= 0 ? tlen :
39236c6e
A
3214 in6_matchlen(IFA_IN6(ifa), dst);
3215 best_scope =
3216 in6_addrscope(&ifa2ia6(ifa)->ia_addr.sin6_addr);
6d2010ae
A
3217 IFA_UNLOCK(ifa);
3218 if (ifa_best)
3219 IFA_REMREF(&ifa_best->ia_ifa);
3220 ifa_best = (struct in6_ifaddr *)ifa;
1c79356b 3221 }
91447636 3222 ifnet_lock_done(ifp);
1c79356b 3223 }
91447636 3224 ifnet_head_done();
1c79356b
A
3225
3226 /* count statistics for future improvements */
3227 if (ifa_best == NULL)
3228 ip6stat.ip6s_sources_none++;
3229 else {
6d2010ae 3230 IFA_LOCK_SPIN(&ifa_best->ia_ifa);
1c79356b
A
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]++;
6d2010ae 3243 IFA_UNLOCK(&ifa_best->ia_ifa);
1c79356b
A
3244 }
3245
39236c6e 3246 return (ifa_best);
1c79356b
A
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 */
1c79356b 3253struct in6_ifaddr *
39236c6e 3254in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1c79356b
A
3255{
3256 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
3257 struct ifaddr *ifa;
6d2010ae 3258 struct in6_ifaddr *besta = NULL;
55e303ae 3259 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1c79356b
A
3260
3261 dep[0] = dep[1] = NULL;
3262
1c79356b
A
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 */
91447636 3269 ifnet_lock_shared(ifp);
39236c6e 3270 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
6d2010ae
A
3271 IFA_LOCK(ifa);
3272 if (ifa->ifa_addr->sa_family != AF_INET6) {
3273 IFA_UNLOCK(ifa);
1c79356b 3274 continue;
6d2010ae
A
3275 }
3276 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_ANYCAST) {
3277 IFA_UNLOCK(ifa);
1c79356b 3278 continue; /* XXX: is there any case to allow anycast? */
6d2010ae
A
3279 }
3280 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_NOTREADY) {
3281 IFA_UNLOCK(ifa);
1c79356b 3282 continue; /* don't use this interface */
6d2010ae
A
3283 }
3284 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DETACHED) {
3285 IFA_UNLOCK(ifa);
1c79356b 3286 continue;
6d2010ae
A
3287 }
3288 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
b0d623f7 3289 if (ip6_use_deprecated) {
6d2010ae
A
3290 IFA_ADDREF_LOCKED(ifa); /* for dep[0] */
3291 IFA_UNLOCK(ifa);
b0d623f7 3292 if (dep[0] != NULL)
6d2010ae 3293 IFA_REMREF(&dep[0]->ia_ifa);
1c79356b 3294 dep[0] = (struct in6_ifaddr *)ifa;
6d2010ae
A
3295 } else {
3296 IFA_UNLOCK(ifa);
b0d623f7 3297 }
1c79356b
A
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) {
6d2010ae
A
3306 if (blen == -1) {
3307 IFA_UNLOCK(ifa);
3308 IFA_LOCK(&besta->ia_ifa);
39236c6e
A
3309 blen = in6_matchlen(
3310 &besta->ia_addr.sin6_addr, dst);
6d2010ae
A
3311 IFA_UNLOCK(&besta->ia_ifa);
3312 IFA_LOCK(ifa);
3313 }
1c79356b
A
3314 tlen = in6_matchlen(IFA_IN6(ifa), dst);
3315 if (tlen > blen) {
3316 blen = tlen;
6d2010ae
A
3317 IFA_ADDREF_LOCKED(ifa); /* for besta */
3318 IFA_UNLOCK(ifa);
3319 IFA_REMREF(&besta->ia_ifa);
1c79356b 3320 besta = (struct in6_ifaddr *)ifa;
6d2010ae
A
3321 } else {
3322 IFA_UNLOCK(ifa);
1c79356b 3323 }
6d2010ae 3324 } else {
1c79356b 3325 besta = (struct in6_ifaddr *)ifa;
6d2010ae
A
3326 IFA_ADDREF_LOCKED(ifa); /* for besta */
3327 IFA_UNLOCK(ifa);
3328 }
3329 } else {
3330 IFA_UNLOCK(ifa);
1c79356b
A
3331 }
3332 }
91447636
A
3333 if (besta) {
3334 ifnet_lock_done(ifp);
b0d623f7 3335 if (dep[0] != NULL)
6d2010ae 3336 IFA_REMREF(&dep[0]->ia_ifa);
39236c6e 3337 return (besta);
91447636 3338 }
1c79356b 3339
39236c6e 3340 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
6d2010ae
A
3341 IFA_LOCK(ifa);
3342 if (ifa->ifa_addr->sa_family != AF_INET6) {
3343 IFA_UNLOCK(ifa);
1c79356b 3344 continue;
6d2010ae
A
3345 }
3346 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_ANYCAST) {
3347 IFA_UNLOCK(ifa);
1c79356b 3348 continue; /* XXX: is there any case to allow anycast? */
6d2010ae
A
3349 }
3350 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_NOTREADY) {
3351 IFA_UNLOCK(ifa);
1c79356b 3352 continue; /* don't use this interface */
6d2010ae
A
3353 }
3354 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DETACHED) {
3355 IFA_UNLOCK(ifa);
1c79356b 3356 continue;
6d2010ae
A
3357 }
3358 if (ifa2ia6(ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
b0d623f7 3359 if (ip6_use_deprecated) {
6d2010ae
A
3360 IFA_ADDREF_LOCKED(ifa); /* for dep[1] */
3361 IFA_UNLOCK(ifa);
b0d623f7 3362 if (dep[1] != NULL)
6d2010ae 3363 IFA_REMREF(&dep[1]->ia_ifa);
1c79356b 3364 dep[1] = (struct in6_ifaddr *)ifa;
6d2010ae
A
3365 } else {
3366 IFA_UNLOCK(ifa);
b0d623f7 3367 }
1c79356b
A
3368 continue;
3369 }
6d2010ae
A
3370 IFA_ADDREF_LOCKED(ifa); /* for caller */
3371 IFA_UNLOCK(ifa);
91447636 3372 ifnet_lock_done(ifp);
b0d623f7 3373 if (dep[0] != NULL)
6d2010ae 3374 IFA_REMREF(&dep[0]->ia_ifa);
b0d623f7 3375 if (dep[1] != NULL)
6d2010ae 3376 IFA_REMREF(&dep[1]->ia_ifa);
39236c6e 3377 return ((struct in6_ifaddr *)ifa);
1c79356b 3378 }
91447636 3379 ifnet_lock_done(ifp);
1c79356b
A
3380
3381 /* use the last-resort values, that are, deprecated addresses */
b0d623f7
A
3382 if (dep[0]) {
3383 if (dep[1] != NULL)
6d2010ae 3384 IFA_REMREF(&dep[1]->ia_ifa);
39236c6e 3385 return (dep[0]);
b0d623f7 3386 }
1c79356b 3387 if (dep[1])
39236c6e 3388 return (dep[1]);
1c79356b 3389
39236c6e 3390 return (NULL);
1c79356b
A
3391}
3392
3393/*
3394 * perform DAD when interface becomes IFF_UP.
3395 */
39236c6e
A
3396static void
3397in6_if_up_dad_start(struct ifnet *ifp)
1c79356b
A
3398{
3399 struct ifaddr *ifa;
6d2010ae 3400
39236c6e 3401 /* start DAD on all the interface addresses */
91447636 3402 ifnet_lock_exclusive(ifp);
39236c6e
A
3403 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
3404 struct in6_ifaddr *ia6;
3405
6d2010ae
A
3406 IFA_LOCK_SPIN(ifa);
3407 if (ifa->ifa_addr->sa_family != AF_INET6) {
3408 IFA_UNLOCK(ifa);
1c79356b 3409 continue;
6d2010ae 3410 }
39236c6e
A
3411 ia6 = (struct in6_ifaddr *)ifa;
3412 if (ia6->ia6_flags & IN6_IFF_DADPROGRESS) {
3413 int delay = 0; /* delay ticks before DAD output */
6d2010ae 3414 IFA_UNLOCK(ifa);
39236c6e 3415 nd6_dad_start(ifa, &delay);
6d2010ae
A
3416 } else {
3417 IFA_UNLOCK(ifa);
3418 }
1c79356b 3419 }
91447636 3420 ifnet_lock_done(ifp);
9bccf70c
A
3421}
3422
3423int
91447636
A
3424in6if_do_dad(
3425 struct ifnet *ifp)
9bccf70c
A
3426{
3427 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
39236c6e 3428 return (0);
1c79356b 3429
6d2010ae 3430 /*
316670eb
A
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.
6d2010ae 3437 */
fe8ab488
A
3438 if (ifp->if_eflags &
3439 (IFEF_IPV6_ND6ALT|IFEF_LOCALNET_PRIVATE|IFEF_DIRECTLINK))
6d2010ae
A
3440 return (0);
3441
1c79356b 3442 switch (ifp->if_type) {
9bccf70c 3443#if IFT_DUMMY
1c79356b 3444 case IFT_DUMMY:
9bccf70c 3445#endif
1c79356b 3446 case IFT_FAITH:
1c79356b 3447 /*
9bccf70c
A
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.
1c79356b 3452 */
39236c6e 3453 return (0);
1c79356b 3454 default:
9bccf70c
A
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))
39236c6e 3466 return (0);
1c79356b 3467
39236c6e 3468 return (1);
1c79356b
A
3469 }
3470}
3471
3472/*
3473 * Calculate max IPv6 MTU through all the interfaces and store it
3474 * to in6_maxmtu.
3475 */
3476void
316670eb 3477in6_setmaxmtu(void)
1c79356b 3478{
b0d623f7 3479 u_int32_t maxmtu = 0;
1c79356b
A
3480 struct ifnet *ifp;
3481
91447636
A
3482 ifnet_head_lock_shared();
3483 TAILQ_FOREACH(ifp, &ifnet_head, if_list) {
3e170ce0 3484 struct nd_ifinfo *ndi = NULL;
316670eb 3485
316670eb
A
3486 if ((ndi = ND_IFINFO(ifp)) != NULL && !ndi->initialized)
3487 ndi = NULL;
3488 if (ndi != NULL)
3489 lck_mtx_lock(&ndi->lock);
1c79356b 3490 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2d21ac55
A
3491 IN6_LINKMTU(ifp) > maxmtu)
3492 maxmtu = IN6_LINKMTU(ifp);
316670eb
A
3493 if (ndi != NULL)
3494 lck_mtx_unlock(&ndi->lock);
1c79356b 3495 }
91447636 3496 ifnet_head_done();
1c79356b
A
3497 if (maxmtu) /* update only when maxmtu is positive */
3498 in6_maxmtu = maxmtu;
3499}
6d2010ae
A
3500/*
3501 * Provide the length of interface identifiers to be used for the link attached
3502 * to the given interface. The length should be defined in "IPv6 over
3503 * xxx-link" document. Note that address architecture might also define
3504 * the length for a particular set of address prefixes, regardless of the
39236c6e 3505 * link type. Also see RFC 4862 for additional background.
6d2010ae
A
3506 */
3507int
3508in6_if2idlen(struct ifnet *ifp)
3509{
3510 switch (ifp->if_type) {
3511 case IFT_ETHER: /* RFC2464 */
3512 case IFT_IEEE8023ADLAG: /* IEEE802.3ad Link Aggregate */
3513#ifdef IFT_PROPVIRTUAL
3514 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
3515#endif
3516#ifdef IFT_L2VLAN
3517 case IFT_L2VLAN: /* ditto */
3518#endif
3519#ifdef IFT_IEEE80211
3520 case IFT_IEEE80211: /* ditto */
3521#endif
3522#ifdef IFT_MIP
3523 case IFT_MIP: /* ditto */
3524#endif
3525 return (64);
3526 case IFT_FDDI: /* RFC2467 */
3527 return (64);
3528 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
3529 return (64);
3530 case IFT_PPP: /* RFC2472 */
3531 return (64);
3532 case IFT_ARCNET: /* RFC2497 */
3533 return (64);
3534 case IFT_FRELAY: /* RFC2590 */
3535 return (64);
3536 case IFT_IEEE1394: /* RFC3146 */
3537 return (64);
3538 case IFT_GIF:
3539 return (64); /* draft-ietf-v6ops-mech-v2-07 */
3540 case IFT_LOOP:
3541 return (64); /* XXX: is this really correct? */
3542 case IFT_OTHER:
3543 return (64); /* for utun interfaces */
3544 case IFT_CELLULAR:
3545 return (64); /* Packet Data over Cellular */
316670eb
A
3546 case IFT_BRIDGE:
3547 return (64); /* Transparent bridge interface */
6d2010ae
A
3548 default:
3549 /*
3550 * Unknown link type:
3551 * It might be controversial to use the today's common constant
3552 * of 64 for these cases unconditionally. For full compliance,
3553 * we should return an error in this case. On the other hand,
3554 * if we simply miss the standard for the link type or a new
3555 * standard is defined for a new link type, the IFID length
3556 * is very likely to be the common constant. As a compromise,
3557 * we always use the constant, but make an explicit notice
3558 * indicating the "unknown" case.
3559 */
39236c6e
A
3560 log(LOG_NOTICE, "%s: unknown link type (%d)\n", __func__,
3561 ifp->if_type);
6d2010ae
A
3562 return (64);
3563 }
3564}
9bccf70c 3565/*
55e303ae 3566 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
1c79356b
A
3567 * v4 mapped addr or v4 compat addr
3568 */
3569void
3570in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
3571{
39236c6e
A
3572 bzero(sin, sizeof (*sin));
3573 sin->sin_len = sizeof (struct sockaddr_in);
1c79356b
A
3574 sin->sin_family = AF_INET;
3575 sin->sin_port = sin6->sin6_port;
6d2010ae 3576 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
1c79356b
A
3577}
3578
3579/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3580void
3581in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
3582{
39236c6e
A
3583 bzero(sin6, sizeof (*sin6));
3584 sin6->sin6_len = sizeof (struct sockaddr_in6);
1c79356b
A
3585 sin6->sin6_family = AF_INET6;
3586 sin6->sin6_port = sin->sin_port;
3587 sin6->sin6_addr.s6_addr32[0] = 0;
3588 sin6->sin6_addr.s6_addr32[1] = 0;
316670eb
A
3589 if (sin->sin_addr.s_addr) {
3590 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
3591 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
39236c6e 3592 } else {
316670eb
A
3593 sin6->sin6_addr.s6_addr32[2] = 0;
3594 sin6->sin6_addr.s6_addr32[3] = 0;
3595 }
1c79356b
A
3596}
3597
3598/* Convert sockaddr_in6 into sockaddr_in. */
3599void
3600in6_sin6_2_sin_in_sock(struct sockaddr *nam)
3601{
3602 struct sockaddr_in *sin_p;
3603 struct sockaddr_in6 sin6;
3604
3605 /*
3606 * Save original sockaddr_in6 addr and convert it
3607 * to sockaddr_in.
3608 */
316670eb
A
3609 sin6 = *(struct sockaddr_in6 *)(void *)nam;
3610 sin_p = (struct sockaddr_in *)(void *)nam;
1c79356b
A
3611 in6_sin6_2_sin(sin_p, &sin6);
3612}
3613
3614/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
b0d623f7 3615int
1c79356b
A
3616in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
3617{
3618 struct sockaddr_in *sin_p;
3619 struct sockaddr_in6 *sin6_p;
3620
39236c6e
A
3621 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof (*sin6_p), M_SONAME,
3622 M_WAITOK);
b0d623f7 3623 if (sin6_p == NULL)
39236c6e 3624 return (ENOBUFS);
316670eb 3625 sin_p = (struct sockaddr_in *)(void *)*nam;
1c79356b
A
3626 in6_sin_2_v4mapsin6(sin_p, sin6_p);
3627 FREE(*nam, M_SONAME);
3628 *nam = (struct sockaddr *)sin6_p;
b0d623f7 3629
39236c6e 3630 return (0);
1c79356b 3631}
1c79356b 3632
b0d623f7
A
3633/*
3634 * Posts in6_event_data message kernel events.
3635 *
3636 * To get the same size of kev_in6_data between ILP32 and LP64 data models
39236c6e
A
3637 * we are using a special version of the in6_addrlifetime structure that
3638 * uses only 32 bits fields to be compatible with Leopard, and that
b0d623f7
A
3639 * are large enough to span 68 years.
3640 */
9bccf70c 3641void
fe8ab488
A
3642in6_post_msg(struct ifnet *ifp, u_int32_t event_code, struct in6_ifaddr *ifa,
3643 uint8_t *mac)
9bccf70c 3644{
39236c6e
A
3645 struct kev_msg ev_msg;
3646 struct kev_in6_data in6_event_data;
3647 struct in6_addrlifetime ia6_lt;
9bccf70c 3648
39236c6e
A
3649 bzero(&in6_event_data, sizeof (struct kev_in6_data));
3650 bzero(&ev_msg, sizeof (struct kev_msg));
3651 ev_msg.vendor_code = KEV_VENDOR_APPLE;
3652 ev_msg.kev_class = KEV_NETWORK_CLASS;
3653 ev_msg.kev_subclass = KEV_INET6_SUBCLASS;
3654 ev_msg.event_code = event_code;
9bccf70c 3655
6d2010ae 3656 IFA_LOCK(&ifa->ia_ifa);
39236c6e
A
3657 in6_event_data.ia_addr = ifa->ia_addr;
3658 in6_event_data.ia_net = ifa->ia_net;
3659 in6_event_data.ia_dstaddr = ifa->ia_dstaddr;
3660 in6_event_data.ia_prefixmask = ifa->ia_prefixmask;
3661 in6_event_data.ia_plen = ifa->ia_plen;
3662 in6_event_data.ia6_flags = (u_int32_t)ifa->ia6_flags;
3663
3664 /* retrieve time as calendar time (last arg is 1) */
3665 in6ifa_getlifetime(ifa, &ia6_lt, 1);
3666 in6_event_data.ia_lifetime.ia6t_expire = ia6_lt.ia6t_expire;
3667 in6_event_data.ia_lifetime.ia6t_preferred = ia6_lt.ia6t_preferred;
3668 in6_event_data.ia_lifetime.ia6t_vltime = ia6_lt.ia6t_vltime;
3669 in6_event_data.ia_lifetime.ia6t_pltime = ia6_lt.ia6t_pltime;
6d2010ae 3670 IFA_UNLOCK(&ifa->ia_ifa);
9bccf70c
A
3671
3672 if (ifp != NULL) {
fe8ab488 3673 (void) strlcpy(&in6_event_data.link_data.if_name[0],
b0d623f7 3674 ifp->if_name, IFNAMSIZ);
9bccf70c 3675 in6_event_data.link_data.if_family = ifp->if_family;
39236c6e 3676 in6_event_data.link_data.if_unit = (u_int32_t)ifp->if_unit;
9bccf70c
A
3677 }
3678
fe8ab488
A
3679 if (mac != NULL)
3680 memcpy(&in6_event_data.ia_mac, mac,
3681 sizeof(in6_event_data.ia_mac));
3682
9bccf70c 3683 ev_msg.dv[0].data_ptr = &in6_event_data;
b0d623f7 3684 ev_msg.dv[0].data_length = sizeof (in6_event_data);
9bccf70c
A
3685 ev_msg.dv[1].data_length = 0;
3686
3687 kev_post_msg(&ev_msg);
3688}
b0d623f7
A
3689
3690/*
3691 * Called as part of ip6_init
3692 */
3693void
3694in6_ifaddr_init(void)
3695{
39236c6e 3696 in6_cga_init();
6d2010ae
A
3697 in6_multi_init();
3698
b0d623f7
A
3699 PE_parse_boot_argn("ifa_debug", &in6ifa_debug, sizeof (in6ifa_debug));
3700
3701 in6ifa_size = (in6ifa_debug == 0) ? sizeof (struct in6_ifaddr) :
3702 sizeof (struct in6_ifaddr_dbg);
3703
3704 in6ifa_zone = zinit(in6ifa_size, IN6IFA_ZONE_MAX * in6ifa_size,
3705 0, IN6IFA_ZONE_NAME);
6d2010ae 3706 if (in6ifa_zone == NULL) {
b0d623f7 3707 panic("%s: failed allocating %s", __func__, IN6IFA_ZONE_NAME);
6d2010ae
A
3708 /* NOTREACHED */
3709 }
b0d623f7 3710 zone_change(in6ifa_zone, Z_EXPAND, TRUE);
6d2010ae
A
3711 zone_change(in6ifa_zone, Z_CALLERACCT, FALSE);
3712
3713 lck_mtx_init(&in6ifa_trash_lock, ifa_mtx_grp, ifa_mtx_attr);
3714 TAILQ_INIT(&in6ifa_trash_head);
b0d623f7
A
3715}
3716
3717static struct in6_ifaddr *
3718in6_ifaddr_alloc(int how)
3719{
3720 struct in6_ifaddr *in6ifa;
3721
3722 in6ifa = (how == M_WAITOK) ? zalloc(in6ifa_zone) :
3723 zalloc_noblock(in6ifa_zone);
3724 if (in6ifa != NULL) {
3725 bzero(in6ifa, in6ifa_size);
3726 in6ifa->ia_ifa.ifa_free = in6_ifaddr_free;
3727 in6ifa->ia_ifa.ifa_debug |= IFD_ALLOC;
6d2010ae 3728 ifa_lock_init(&in6ifa->ia_ifa);
b0d623f7
A
3729 if (in6ifa_debug != 0) {
3730 struct in6_ifaddr_dbg *in6ifa_dbg =
3731 (struct in6_ifaddr_dbg *)in6ifa;
3732 in6ifa->ia_ifa.ifa_debug |= IFD_DEBUG;
3733 in6ifa->ia_ifa.ifa_trace = in6_ifaddr_trace;
6d2010ae
A
3734 in6ifa->ia_ifa.ifa_attached = in6_ifaddr_attached;
3735 in6ifa->ia_ifa.ifa_detached = in6_ifaddr_detached;
b0d623f7
A
3736 ctrace_record(&in6ifa_dbg->in6ifa_alloc);
3737 }
3738 }
39236c6e 3739
b0d623f7
A
3740 return (in6ifa);
3741}
3742
3743static void
3744in6_ifaddr_free(struct ifaddr *ifa)
3745{
6d2010ae
A
3746 IFA_LOCK_ASSERT_HELD(ifa);
3747
3748 if (ifa->ifa_refcnt != 0) {
b0d623f7 3749 panic("%s: ifa %p bad ref cnt", __func__, ifa);
6d2010ae
A
3750 /* NOTREACHED */
3751 } else if (!(ifa->ifa_debug & IFD_ALLOC)) {
b0d623f7 3752 panic("%s: ifa %p cannot be freed", __func__, ifa);
6d2010ae
A
3753 /* NOTREACHED */
3754 }
b0d623f7
A
3755 if (ifa->ifa_debug & IFD_DEBUG) {
3756 struct in6_ifaddr_dbg *in6ifa_dbg =
3757 (struct in6_ifaddr_dbg *)ifa;
3758 ctrace_record(&in6ifa_dbg->in6ifa_free);
3759 bcopy(&in6ifa_dbg->in6ifa, &in6ifa_dbg->in6ifa_old,
3760 sizeof (struct in6_ifaddr));
6d2010ae
A
3761 if (ifa->ifa_debug & IFD_TRASHED) {
3762 /* Become a regular mutex, just in case */
3763 IFA_CONVERT_LOCK(ifa);
3764 lck_mtx_lock(&in6ifa_trash_lock);
3765 TAILQ_REMOVE(&in6ifa_trash_head, in6ifa_dbg,
3766 in6ifa_trash_link);
3767 lck_mtx_unlock(&in6ifa_trash_lock);
3768 ifa->ifa_debug &= ~IFD_TRASHED;
3769 }
b0d623f7 3770 }
6d2010ae
A
3771 IFA_UNLOCK(ifa);
3772 ifa_lock_destroy(ifa);
b0d623f7
A
3773 bzero(ifa, sizeof (struct in6_ifaddr));
3774 zfree(in6ifa_zone, ifa);
3775}
3776
6d2010ae
A
3777static void
3778in6_ifaddr_attached(struct ifaddr *ifa)
3779{
3780 struct in6_ifaddr_dbg *in6ifa_dbg = (struct in6_ifaddr_dbg *)ifa;
3781
3782 IFA_LOCK_ASSERT_HELD(ifa);
3783
3784 if (!(ifa->ifa_debug & IFD_DEBUG)) {
3785 panic("%s: ifa %p has no debug structure", __func__, ifa);
3786 /* NOTREACHED */
3787 }
3788 if (ifa->ifa_debug & IFD_TRASHED) {
3789 /* Become a regular mutex, just in case */
3790 IFA_CONVERT_LOCK(ifa);
3791 lck_mtx_lock(&in6ifa_trash_lock);
3792 TAILQ_REMOVE(&in6ifa_trash_head, in6ifa_dbg, in6ifa_trash_link);
3793 lck_mtx_unlock(&in6ifa_trash_lock);
3794 ifa->ifa_debug &= ~IFD_TRASHED;
3795 }
3796}
3797
3798static void
3799in6_ifaddr_detached(struct ifaddr *ifa)
3800{
3801 struct in6_ifaddr_dbg *in6ifa_dbg = (struct in6_ifaddr_dbg *)ifa;
3802
3803 IFA_LOCK_ASSERT_HELD(ifa);
3804
3805 if (!(ifa->ifa_debug & IFD_DEBUG)) {
3806 panic("%s: ifa %p has no debug structure", __func__, ifa);
3807 /* NOTREACHED */
3808 } else if (ifa->ifa_debug & IFD_TRASHED) {
3809 panic("%s: ifa %p is already in trash list", __func__, ifa);
3810 /* NOTREACHED */
3811 }
3812 ifa->ifa_debug |= IFD_TRASHED;
3813 /* Become a regular mutex, just in case */
3814 IFA_CONVERT_LOCK(ifa);
3815 lck_mtx_lock(&in6ifa_trash_lock);
3816 TAILQ_INSERT_TAIL(&in6ifa_trash_head, in6ifa_dbg, in6ifa_trash_link);
3817 lck_mtx_unlock(&in6ifa_trash_lock);
3818}
3819
b0d623f7
A
3820static void
3821in6_ifaddr_trace(struct ifaddr *ifa, int refhold)
3822{
3823 struct in6_ifaddr_dbg *in6ifa_dbg = (struct in6_ifaddr_dbg *)ifa;
3824 ctrace_t *tr;
3825 u_int32_t idx;
3826 u_int16_t *cnt;
3827
6d2010ae 3828 if (!(ifa->ifa_debug & IFD_DEBUG)) {
b0d623f7 3829 panic("%s: ifa %p has no debug structure", __func__, ifa);
6d2010ae
A
3830 /* NOTREACHED */
3831 }
b0d623f7
A
3832 if (refhold) {
3833 cnt = &in6ifa_dbg->in6ifa_refhold_cnt;
3834 tr = in6ifa_dbg->in6ifa_refhold;
3835 } else {
3836 cnt = &in6ifa_dbg->in6ifa_refrele_cnt;
3837 tr = in6ifa_dbg->in6ifa_refrele;
3838 }
3839
6d2010ae 3840 idx = atomic_add_16_ov(cnt, 1) % IN6IFA_TRACE_HIST_SIZE;
b0d623f7
A
3841 ctrace_record(&tr[idx]);
3842}
316670eb
A
3843
3844static void
3845in6_ifaddr_set_dadprogress(struct in6_ifaddr *ia)
3846{
fe8ab488 3847 struct ifnet* ifp = ia->ia_ifp;
316670eb
A
3848 uint32_t flags = IN6_IFF_TENTATIVE;
3849 uint32_t optdad = nd6_optimistic_dad;
39236c6e 3850
fe8ab488
A
3851 if (optdad) {
3852 if ((ifp->if_eflags & IFEF_IPV6_ROUTER) != 0) {
3853 optdad = 0;
3854 } else {
3e170ce0 3855 struct nd_ifinfo *ndi = NULL;
fe8ab488 3856
fe8ab488
A
3857 ndi = ND_IFINFO(ifp);
3858 VERIFY (ndi != NULL && ndi->initialized);
3859 lck_mtx_lock(&ndi->lock);
3860 if ((ndi->flags & ND6_IFF_REPLICATED) != 0) {
3861 optdad = 0;
3862 }
3863 lck_mtx_unlock(&ndi->lock);
fe8ab488
A
3864 }
3865 }
3866
3867 if (optdad) {
316670eb
A
3868 if ((optdad & ND6_OPTIMISTIC_DAD_LINKLOCAL) &&
3869 IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr))
3870 flags = IN6_IFF_OPTIMISTIC;
3871 else if ((optdad & ND6_OPTIMISTIC_DAD_AUTOCONF) &&
39236c6e 3872 (ia->ia6_flags & IN6_IFF_AUTOCONF)) {
316670eb
A
3873 if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
3874 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY)
3875 flags = IN6_IFF_OPTIMISTIC;
39236c6e
A
3876 } else if (ia->ia6_flags & IN6_IFF_SECURED) {
3877 if (optdad & ND6_OPTIMISTIC_DAD_SECURED)
3878 flags = IN6_IFF_OPTIMISTIC;
3e170ce0
A
3879 } else {
3880 /*
3881 * Keeping the behavior for temp and CGA
3882 * SLAAC addresses to have a knob for optimistic
3883 * DAD.
3884 * Other than that if ND6_OPTIMISTIC_DAD_AUTOCONF
3885 * is set, we should default to optimistic
3886 * DAD.
3887 * For now this means SLAAC addresses with interface
3888 * identifier derived from modified EUI-64 bit
3889 * identifiers.
3890 */
3891 flags = IN6_IFF_OPTIMISTIC;
316670eb
A
3892 }
3893 } else if ((optdad & ND6_OPTIMISTIC_DAD_DYNAMIC) &&
39236c6e 3894 (ia->ia6_flags & IN6_IFF_DYNAMIC)) {
316670eb
A
3895 if (ia->ia6_flags & IN6_IFF_TEMPORARY) {
3896 if (optdad & ND6_OPTIMISTIC_DAD_TEMPORARY)
3897 flags = IN6_IFF_OPTIMISTIC;
3898 } else {
3899 flags = IN6_IFF_OPTIMISTIC;
3900 }
fe8ab488
A
3901 } else if ((optdad & ND6_OPTIMISTIC_DAD_MANUAL) &&
3902 (ia->ia6_flags & IN6_IFF_OPTIMISTIC)) {
3903 /*
3904 * rdar://17483438
3905 * Bypass tentative for address assignments
3906 * not covered above (e.g. manual) upon request
3907 */
3908 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) &&
3909 !(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
3910 !(ia->ia6_flags & IN6_IFF_DYNAMIC))
3911 flags = IN6_IFF_OPTIMISTIC;
316670eb
A
3912 }
3913 }
39236c6e 3914
316670eb
A
3915 ia->ia6_flags &= ~(IN6_IFF_DUPLICATED | IN6_IFF_DADPROGRESS);
3916 ia->ia6_flags |= flags;
fe8ab488
A
3917
3918 nd6log2((LOG_DEBUG, "%s - %s ifp %s ia6_flags 0x%x\n",
3919 __func__,
3920 ip6_sprintf(&ia->ia_addr.sin6_addr),
3921 if_name(ia->ia_ifp),
3922 ia->ia6_flags));
316670eb
A
3923}
3924
39236c6e
A
3925/*
3926 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
3927 */
3928static int
3929in6_getassocids(struct socket *so, uint32_t *cnt, user_addr_t aidp)
3930{
3931 struct in6pcb *in6p = sotoin6pcb(so);
3e170ce0 3932 sae_associd_t aid;
39236c6e
A
3933
3934 if (in6p == NULL || in6p->inp_state == INPCB_STATE_DEAD)
3935 return (EINVAL);
3936
3937 /* IN6PCB has no concept of association */
3e170ce0 3938 aid = SAE_ASSOCID_ANY;
39236c6e
A
3939 *cnt = 0;
3940
3941 /* just asking how many there are? */
3942 if (aidp == USER_ADDR_NULL)
3943 return (0);
3944
3945 return (copyout(&aid, aidp, sizeof (aid)));
3946}
3947
3948/*
3949 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
3950 */
3951static int
3e170ce0 3952in6_getconnids(struct socket *so, sae_associd_t aid, uint32_t *cnt,
39236c6e
A
3953 user_addr_t cidp)
3954{
3955 struct in6pcb *in6p = sotoin6pcb(so);
3e170ce0 3956 sae_connid_t cid;
39236c6e
A
3957
3958 if (in6p == NULL || in6p->inp_state == INPCB_STATE_DEAD)
3959 return (EINVAL);
3960
3e170ce0 3961 if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL)
39236c6e
A
3962 return (EINVAL);
3963
3964 /* if connected, return 1 connection count */
3965 *cnt = ((so->so_state & SS_ISCONNECTED) ? 1 : 0);
3966
3967 /* just asking how many there are? */
3968 if (cidp == USER_ADDR_NULL)
3969 return (0);
3970
3971 /* if IN6PCB is connected, assign it connid 1 */
3e170ce0 3972 cid = ((*cnt != 0) ? 1 : SAE_CONNID_ANY);
39236c6e
A
3973
3974 return (copyout(&cid, cidp, sizeof (cid)));
3975}
3976
3977/*
3978 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
3979 */
3980static int
3e170ce0 3981in6_getconninfo(struct socket *so, sae_connid_t cid, uint32_t *flags,
39236c6e
A
3982 uint32_t *ifindex, int32_t *soerror, user_addr_t src, socklen_t *src_len,
3983 user_addr_t dst, socklen_t *dst_len, uint32_t *aux_type,
3984 user_addr_t aux_data, uint32_t *aux_len)
3985{
3986#pragma unused(aux_data)
3987 struct in6pcb *in6p = sotoin6pcb(so);
3988 struct sockaddr_in6 sin6;
3989 struct ifnet *ifp = NULL;
3990 int error = 0;
3991 u_int32_t copy_len = 0;
3992
3993 /*
3994 * Don't test for INPCB_STATE_DEAD since this may be called
3995 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
3996 */
3997 if (in6p == NULL) {
3998 error = EINVAL;
3999 goto out;
4000 }
4001
3e170ce0 4002 if (cid != SAE_CONNID_ANY && cid != SAE_CONNID_ALL && cid != 1) {
39236c6e
A
4003 error = EINVAL;
4004 goto out;
4005 }
4006
4007 ifp = in6p->in6p_last_outifp;
4008 *ifindex = ((ifp != NULL) ? ifp->if_index : 0);
4009 *soerror = so->so_error;
4010 *flags = 0;
4011 if (so->so_state & SS_ISCONNECTED)
4012 *flags |= (CIF_CONNECTED | CIF_PREFERRED);
4013 if (in6p->in6p_flags & INP_BOUND_IF)
4014 *flags |= CIF_BOUND_IF;
4015 if (!(in6p->in6p_flags & INP_IN6ADDR_ANY))
4016 *flags |= CIF_BOUND_IP;
4017 if (!(in6p->in6p_flags & INP_ANONPORT))
4018 *flags |= CIF_BOUND_PORT;
4019
4020 bzero(&sin6, sizeof (sin6));
4021 sin6.sin6_len = sizeof (sin6);
4022 sin6.sin6_family = AF_INET6;
4023
4024 /* source address and port */
4025 sin6.sin6_port = in6p->in6p_lport;
fe8ab488 4026 in6_recoverscope(&sin6, &in6p->in6p_laddr, NULL);
39236c6e
A
4027 if (*src_len == 0) {
4028 *src_len = sin6.sin6_len;
4029 } else {
4030 if (src != USER_ADDR_NULL) {
4031 copy_len = min(*src_len, sizeof (sin6));
4032 error = copyout(&sin6, src, copy_len);
4033 if (error != 0)
4034 goto out;
4035 *src_len = copy_len;
4036 }
4037 }
4038
4039 /* destination address and port */
4040 sin6.sin6_port = in6p->in6p_fport;
fe8ab488 4041 in6_recoverscope(&sin6, &in6p->in6p_faddr, NULL);
39236c6e
A
4042 if (*dst_len == 0) {
4043 *dst_len = sin6.sin6_len;
4044 } else {
4045 if (dst != USER_ADDR_NULL) {
4046 copy_len = min(*dst_len, sizeof (sin6));
4047 error = copyout(&sin6, dst, copy_len);
4048 if (error != 0)
4049 goto out;
4050 *dst_len = copy_len;
4051 }
4052 }
4053
4054 *aux_type = 0;
4055 *aux_len = 0;
4056 if (SOCK_PROTO(so) == IPPROTO_TCP) {
4057 struct conninfo_tcp tcp_ci;
4058
4059 *aux_type = CIAUX_TCP;
4060 if (*aux_len == 0) {
4061 *aux_len = sizeof (tcp_ci);
4062 } else {
4063 if (aux_data != USER_ADDR_NULL) {
4064 copy_len = min(*aux_len, sizeof (tcp_ci));
4065 bzero(&tcp_ci, sizeof (tcp_ci));
4066 tcp_getconninfo(so, &tcp_ci);
4067 error = copyout(&tcp_ci, aux_data, copy_len);
4068 if (error != 0)
4069 goto out;
4070 *aux_len = copy_len;
4071 }
4072 }
4073 }
4074
4075out:
4076 return (error);
4077}
4078
4079/*
4080 * 'u' group ioctls.
4081 *
4082 * The switch statement below does nothing at runtime, as it serves as a
4083 * compile time check to ensure that all of the socket 'u' ioctls (those
4084 * in the 'u' group going thru soo_ioctl) that are made available by the
4085 * networking stack is unique. This works as long as this routine gets
4086 * updated each time a new interface ioctl gets added.
4087 *
4088 * Any failures at compile time indicates duplicated ioctl values.
4089 */
4090static __attribute__((unused)) void
4091in6ioctl_cassert(void)
4092{
4093 /*
4094 * This is equivalent to _CASSERT() and the compiler wouldn't
4095 * generate any instructions, thus for compile time only.
4096 */
4097 switch ((u_long)0) {
4098 case 0:
4099
4100 /* bsd/netinet6/in6_var.h */
39236c6e
A
4101 case SIOCAADDRCTL_POLICY:
4102 case SIOCDADDRCTL_POLICY:
4103 case SIOCDRADD_IN6_32:
4104 case SIOCDRADD_IN6_64:
4105 case SIOCDRDEL_IN6_32:
4106 case SIOCDRDEL_IN6_64:
4107 ;
4108 }
4109}