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