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