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