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