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