]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6.c
35ffb4a8f6e011d61838a9c600c665584efc998f
[apple/xnu.git] / bsd / netinet6 / in6.c
1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.7 2001/08/06 20:26:22 ume Exp $ */
2 /* $KAME: in6.c,v 1.187 2001/05/24 07:43:59 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)in.c 8.2 (Berkeley) 11/15/93
66 */
67
68
69 #include <sys/param.h>
70 #include <sys/ioctl.h>
71 #include <sys/errno.h>
72 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/systm.h>
77 #include <sys/time.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/kern_event.h>
81
82 #include <net/if.h>
83 #include <net/if_types.h>
84 #include <net/route.h>
85 #include <net/if_dl.h>
86
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/if_ether.h>
90 #ifndef SCOPEDROUTING
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #endif
95
96 #include <netinet6/nd6.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet6/ip6_mroute.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/scope6_var.h>
103 #ifndef SCOPEDROUTING
104 #include <netinet6/in6_pcb.h>
105 #endif
106
107 #include <net/net_osdep.h>
108
109 #ifndef __APPLE__
110 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
111 #endif
112 /*
113 * Definitions of some costant IP6 addresses.
114 */
115 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
116 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
117 const struct in6_addr in6addr_nodelocal_allnodes =
118 IN6ADDR_NODELOCAL_ALLNODES_INIT;
119 const struct in6_addr in6addr_linklocal_allnodes =
120 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
121 const struct in6_addr in6addr_linklocal_allrouters =
122 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
123
124 const struct in6_addr in6mask0 = IN6MASK0;
125 const struct in6_addr in6mask32 = IN6MASK32;
126 const struct in6_addr in6mask64 = IN6MASK64;
127 const struct in6_addr in6mask96 = IN6MASK96;
128 const struct in6_addr in6mask128 = IN6MASK128;
129
130 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
131 0, 0, IN6ADDR_ANY_INIT, 0};
132
133 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
134 struct ifnet *, struct proc *));
135 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
136 struct sockaddr_in6 *, int));
137 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
138
139 struct in6_multihead in6_multihead; /* XXX BSS initialization */
140
141 /*
142 * Subroutine for in6_ifaddloop() and in6_ifremloop().
143 * This routine does actual work.
144 */
145 static void
146 in6_ifloop_request(int cmd, struct ifaddr *ifa)
147 {
148 struct sockaddr_in6 all1_sa;
149 struct rtentry *nrt = NULL;
150 int e;
151
152 bzero(&all1_sa, sizeof(all1_sa));
153 all1_sa.sin6_family = AF_INET6;
154 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
155 all1_sa.sin6_addr = in6mask128;
156
157 /*
158 * We specify the address itself as the gateway, and set the
159 * RTF_LLINFO flag, so that the corresponding host route would have
160 * the flag, and thus applications that assume traditional behavior
161 * would be happy. Note that we assume the caller of the function
162 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
163 * which changes the outgoing interface to the loopback interface.
164 */
165 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
166 (struct sockaddr *)&all1_sa,
167 RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
168 if (e != 0) {
169 log(LOG_ERR, "in6_ifloop_request: "
170 "%s operation failed for %s (errno=%d)\n",
171 cmd == RTM_ADD ? "ADD" : "DELETE",
172 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
173 e);
174 }
175
176 /*
177 * Make sure rt_ifa be equal to IFA, the second argument of the
178 * function.
179 * We need this because when we refer to rt_ifa->ia6_flags in
180 * ip6_input, we assume that the rt_ifa points to the address instead
181 * of the loopback address.
182 */
183 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
184 rtsetifa(nrt, ifa);
185 nrt->rt_dlt = ifa->ifa_dlt;
186 }
187
188 /*
189 * Report the addition/removal of the address to the routing socket.
190 * XXX: since we called rtinit for a p2p interface with a destination,
191 * we end up reporting twice in such a case. Should we rather
192 * omit the second report?
193 */
194 if (nrt) {
195 rt_newaddrmsg(cmd, ifa, e, nrt);
196 if (cmd == RTM_DELETE) {
197 if (nrt->rt_refcnt <= 0) {
198 /* XXX: we should free the entry ourselves. */
199 rtref(nrt);
200 rtfree(nrt);
201 }
202 } else {
203 /* the cmd must be RTM_ADD here */
204 rtunref(nrt);
205 }
206 }
207 }
208
209 /*
210 * Add ownaddr as loopback rtentry. We previously add the route only if
211 * necessary (ex. on a p2p link). However, since we now manage addresses
212 * separately from prefixes, we should always add the route. We can't
213 * rely on the cloning mechanism from the corresponding interface route
214 * any more.
215 */
216 static void
217 in6_ifaddloop(struct ifaddr *ifa)
218 {
219 struct rtentry *rt;
220
221 /* If there is no loopback entry, allocate one. */
222 rt = rtalloc1(ifa->ifa_addr, 0, 0);
223 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
224 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
225 in6_ifloop_request(RTM_ADD, ifa);
226 if (rt)
227 rt->rt_refcnt--;
228 }
229
230 /*
231 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
232 * if it exists.
233 */
234 static void
235 in6_ifremloop(struct ifaddr *ifa)
236 {
237 struct in6_ifaddr *ia;
238 struct rtentry *rt;
239 int ia_count = 0;
240
241 /*
242 * Some of BSD variants do not remove cloned routes
243 * from an interface direct route, when removing the direct route
244 * (see comments in net/net_osdep.h). Even for variants that do remove
245 * cloned routes, they could fail to remove the cloned routes when
246 * we handle multple addresses that share a common prefix.
247 * So, we should remove the route corresponding to the deleted address
248 * regardless of the result of in6_is_ifloop_auto().
249 */
250
251 /*
252 * Delete the entry only if exact one ifa exists. More than one ifa
253 * can exist if we assign a same single address to multiple
254 * (probably p2p) interfaces.
255 * XXX: we should avoid such a configuration in IPv6...
256 */
257 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
258 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
259 ia_count++;
260 if (ia_count > 1)
261 break;
262 }
263 }
264
265 if (ia_count == 1) {
266 /*
267 * Before deleting, check if a corresponding loopbacked host
268 * route surely exists. With this check, we can avoid to
269 * delete an interface direct route whose destination is same
270 * as the address being removed. This can happen when remofing
271 * a subnet-router anycast address on an interface attahced
272 * to a shared medium.
273 */
274 rt = rtalloc1(ifa->ifa_addr, 0, 0);
275 if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
276 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
277 rt->rt_refcnt--;
278 in6_ifloop_request(RTM_DELETE, ifa);
279 }
280 }
281 }
282
283 int
284 in6_ifindex2scopeid(idx)
285 int idx;
286 {
287 struct ifnet *ifp;
288 struct ifaddr *ifa;
289 struct sockaddr_in6 *sin6;
290
291 if (idx < 0 || if_index < idx)
292 return -1;
293 ifp = ifindex2ifnet[idx];
294
295 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
296 {
297 if (ifa->ifa_addr->sa_family != AF_INET6)
298 continue;
299 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
300 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
301 return sin6->sin6_scope_id & 0xffff;
302 }
303
304 return -1;
305 }
306
307 int
308 in6_mask2len(mask, lim0)
309 struct in6_addr *mask;
310 u_char *lim0;
311 {
312 int x = 0, y;
313 u_char *lim = lim0, *p;
314
315 if (lim0 == NULL ||
316 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
317 lim = (u_char *)mask + sizeof(*mask);
318 for (p = (u_char *)mask; p < lim; x++, p++) {
319 if (*p != 0xff)
320 break;
321 }
322 y = 0;
323 if (p < lim) {
324 for (y = 0; y < 8; y++) {
325 if ((*p & (0x80 >> y)) == 0)
326 break;
327 }
328 }
329
330 /*
331 * when the limit pointer is given, do a stricter check on the
332 * remaining bits.
333 */
334 if (p < lim) {
335 if (y != 0 && (*p & (0x00ff >> y)) != 0)
336 return(-1);
337 for (p = p + 1; p < lim; p++)
338 if (*p != 0)
339 return(-1);
340 }
341
342 return x * 8 + y;
343 }
344
345 void
346 in6_len2mask(mask, len)
347 struct in6_addr *mask;
348 int len;
349 {
350 int i;
351
352 bzero(mask, sizeof(*mask));
353 for (i = 0; i < len / 8; i++)
354 mask->s6_addr8[i] = 0xff;
355 if (len % 8)
356 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
357 }
358
359 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
360 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
361
362 int
363 in6_control(so, cmd, data, ifp, p)
364 struct socket *so;
365 u_long cmd;
366 caddr_t data;
367 struct ifnet *ifp;
368 struct proc *p;
369 {
370 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
371 struct in6_ifaddr *ia = NULL;
372 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
373 int privileged, error = 0;
374 u_long dl_tag;
375
376 privileged = 0;
377 #ifdef __APPLE__
378 if (p == NULL || !suser(p->p_ucred, &p->p_acflag))
379 #else
380 if (p == NULL || !suser(p))
381 #endif
382 privileged++;
383
384 switch (cmd) {
385 case SIOCGETSGCNT_IN6:
386 case SIOCGETMIFCNT_IN6:
387 return (mrt6_ioctl(cmd, data));
388 }
389
390 if (ifp == NULL)
391 return(EOPNOTSUPP);
392
393 switch (cmd) {
394 case SIOCSNDFLUSH_IN6:
395 case SIOCSPFXFLUSH_IN6:
396 case SIOCSRTRFLUSH_IN6:
397 case SIOCSDEFIFACE_IN6:
398 case SIOCSIFINFO_FLAGS:
399 if (!privileged)
400 return(EPERM);
401 /*fall through*/
402 case OSIOCGIFINFO_IN6:
403 case SIOCGIFINFO_IN6:
404 case SIOCGDRLST_IN6:
405 case SIOCGPRLST_IN6:
406 case SIOCGNBRINFO_IN6:
407 case SIOCGDEFIFACE_IN6:
408 return(nd6_ioctl(cmd, data, ifp));
409 }
410
411 switch (cmd) {
412 case SIOCSIFPREFIX_IN6:
413 case SIOCDIFPREFIX_IN6:
414 case SIOCAIFPREFIX_IN6:
415 case SIOCCIFPREFIX_IN6:
416 case SIOCSGIFPREFIX_IN6:
417 case SIOCGIFPREFIX_IN6:
418 log(LOG_NOTICE,
419 "prefix ioctls are now invalidated. "
420 "please use ifconfig.\n");
421 return(EOPNOTSUPP);
422 }
423
424 switch(cmd) {
425 case SIOCSSCOPE6:
426 if (!privileged)
427 return(EPERM);
428 return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
429 break;
430 case SIOCGSCOPE6:
431 return(scope6_get(ifp, ifr->ifr_ifru.ifru_scope_id));
432 break;
433 case SIOCGSCOPE6DEF:
434 return(scope6_get_default(ifr->ifr_ifru.ifru_scope_id));
435 break;
436 }
437
438 switch (cmd) {
439 case SIOCALIFADDR:
440 case SIOCDLIFADDR:
441 if (!privileged)
442 return(EPERM);
443 /*fall through*/
444 case SIOCGLIFADDR:
445 return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
446 }
447
448 #ifdef __APPLE__
449
450 switch (cmd) {
451
452 case SIOCPROTOATTACH:
453 in6_if_up(ifp);
454 break;
455 case SIOCPROTODETACH:
456 in6_purgeif(ifp);
457 switch (ifp->if_type) {
458 case IFT_ETHER:
459 error = ether_detach_inet6(ifp);
460 break;
461 case IFT_GIF:
462 error = gif_detach_proto_family(ifp, PF_INET6);
463 break;
464 case IFT_STF:
465 error = stf_detach_inet6(ifp);
466 break;
467 case IFT_LOOP: /* do not detach loopback */
468 break;
469 default:
470 printf("SIOCPROTODETACH: %s%d unknown type, can't detach\n",
471 ifp->if_name, ifp->if_unit);
472 return(ENOENT);
473 break;
474 }
475 if (error) {
476 printf("SIOCPROTODETACH: %s%d ether_detach_inet6 error=%x\n",
477 ifp->if_name, ifp->if_unit, error);
478 return(error);
479 }
480 break;
481
482 }
483 #endif
484 /*
485 * Find address for this interface, if it exists.
486 */
487 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
488 struct sockaddr_in6 *sa6 =
489 (struct sockaddr_in6 *)&ifra->ifra_addr;
490
491 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
492 if (sa6->sin6_addr.s6_addr16[1] == 0) {
493 /* link ID is not embedded by the user */
494 sa6->sin6_addr.s6_addr16[1] =
495 htons(ifp->if_index);
496 } else if (sa6->sin6_addr.s6_addr16[1] !=
497 htons(ifp->if_index)) {
498 return(EINVAL); /* link ID contradicts */
499 }
500 if (sa6->sin6_scope_id) {
501 if (sa6->sin6_scope_id !=
502 (u_int32_t)ifp->if_index)
503 return(EINVAL);
504 sa6->sin6_scope_id = 0; /* XXX: good way? */
505 }
506 }
507 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
508 }
509
510 switch (cmd) {
511 case SIOCSIFADDR_IN6:
512 case SIOCSIFDSTADDR_IN6:
513 case SIOCSIFNETMASK_IN6:
514 /*
515 * Since IPv6 allows a node to assign multiple addresses
516 * on a single interface, SIOCSIFxxx ioctls are not suitable
517 * and should be unused.
518 */
519 /* we decided to obsolete this command (20000704) */
520 return(EINVAL);
521
522 case SIOCDIFADDR_IN6:
523 /*
524 * for IPv4, we look for existing in_ifaddr here to allow
525 * "ifconfig if0 delete" to remove first IPv4 address on the
526 * interface. For IPv6, as the spec allow multiple interface
527 * address from the day one, we consider "remove the first one"
528 * semantics to be not preferable.
529 */
530 if (ia == NULL)
531 return(EADDRNOTAVAIL);
532 /* FALLTHROUGH */
533 case SIOCAIFADDR_IN6:
534 /*
535 * We always require users to specify a valid IPv6 address for
536 * the corresponding operation.
537 */
538 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
539 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
540 return(EAFNOSUPPORT);
541 if (!privileged)
542 return(EPERM);
543
544 break;
545
546 case SIOCGIFADDR_IN6:
547 /* This interface is basically deprecated. use SIOCGIFCONF. */
548 /* fall through */
549 case SIOCGIFAFLAG_IN6:
550 case SIOCGIFNETMASK_IN6:
551 case SIOCGIFDSTADDR_IN6:
552 case SIOCGIFALIFETIME_IN6:
553 /* must think again about its semantics */
554 if (ia == NULL)
555 return(EADDRNOTAVAIL);
556 break;
557 case SIOCSIFALIFETIME_IN6:
558 {
559 struct in6_addrlifetime *lt;
560
561 if (!privileged)
562 return(EPERM);
563 if (ia == NULL)
564 return(EADDRNOTAVAIL);
565 /* sanity for overflow - beware unsigned */
566 lt = &ifr->ifr_ifru.ifru_lifetime;
567 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
568 && lt->ia6t_vltime + time_second < time_second) {
569 return EINVAL;
570 }
571 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
572 && lt->ia6t_pltime + time_second < time_second) {
573 return EINVAL;
574 }
575 break;
576 }
577 }
578
579 switch (cmd) {
580
581 case SIOCGIFADDR_IN6:
582 ifr->ifr_addr = ia->ia_addr;
583 break;
584
585 case SIOCGIFDSTADDR_IN6:
586 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
587 return(EINVAL);
588 /*
589 * XXX: should we check if ifa_dstaddr is NULL and return
590 * an error?
591 */
592 ifr->ifr_dstaddr = ia->ia_dstaddr;
593 break;
594
595 case SIOCGIFNETMASK_IN6:
596 ifr->ifr_addr = ia->ia_prefixmask;
597 break;
598
599 case SIOCGIFAFLAG_IN6:
600 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
601 break;
602
603 case SIOCGIFSTAT_IN6:
604 if (ifp == NULL)
605 return EINVAL;
606 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
607 || in6_ifstat[ifp->if_index] == NULL) {
608 /* return EAFNOSUPPORT? */
609 bzero(&ifr->ifr_ifru.ifru_stat,
610 sizeof(ifr->ifr_ifru.ifru_stat));
611 } else
612 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
613 break;
614
615 case SIOCGIFSTAT_ICMP6:
616 if (ifp == NULL)
617 return EINVAL;
618 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
619 icmp6_ifstat[ifp->if_index] == NULL) {
620 /* return EAFNOSUPPORT? */
621 bzero(&ifr->ifr_ifru.ifru_stat,
622 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
623 } else
624 ifr->ifr_ifru.ifru_icmp6stat =
625 *icmp6_ifstat[ifp->if_index];
626 break;
627
628 case SIOCGIFALIFETIME_IN6:
629 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
630 break;
631
632 case SIOCSIFALIFETIME_IN6:
633 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
634 /* for sanity */
635 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
636 ia->ia6_lifetime.ia6t_expire =
637 time_second + ia->ia6_lifetime.ia6t_vltime;
638 } else
639 ia->ia6_lifetime.ia6t_expire = 0;
640 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
641 ia->ia6_lifetime.ia6t_preferred =
642 time_second + ia->ia6_lifetime.ia6t_pltime;
643 } else
644 ia->ia6_lifetime.ia6t_preferred = 0;
645 break;
646
647 case SIOCAIFADDR_IN6:
648 {
649 int i, error = 0;
650 struct nd_prefix pr0, *pr;
651
652 if (dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET6, &dl_tag) == EPROTONOSUPPORT) {
653 in6_if_up(ifp); /* no dl_tag, the interface is not "up" for IPv6 yet */
654 }
655
656 /*
657 * first, make or update the interface address structure,
658 * and link it to the list.
659 */
660 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
661 return(error);
662
663 /*
664 * then, make the prefix on-link on the interface.
665 * XXX: we'd rather create the prefix before the address, but
666 * we need at least one address to install the corresponding
667 * interface route, so we configure the address first.
668 */
669
670 /*
671 * convert mask to prefix length (prefixmask has already
672 * been validated in in6_update_ifa().
673 */
674 bzero(&pr0, sizeof(pr0));
675 pr0.ndpr_ifp = ifp;
676 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
677 NULL);
678 if (pr0.ndpr_plen == 128)
679 break; /* we don't need to install a host route. */
680 pr0.ndpr_prefix = ifra->ifra_addr;
681 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
682 /* apply the mask for safety. */
683 for (i = 0; i < 4; i++) {
684 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
685 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
686 }
687 /*
688 * XXX: since we don't have enough APIs, we just set inifinity
689 * to lifetimes. They can be overridden by later advertised
690 * RAs (when accept_rtadv is non 0), but we'd rather intend
691 * such a behavior.
692 */
693 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
694 pr0.ndpr_raf_auto =
695 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
696 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
697 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
698
699 /* add the prefix if there's one. */
700 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
701 /*
702 * nd6_prelist_add will install the corresponding
703 * interface route.
704 */
705 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
706 return(error);
707 if (pr == NULL) {
708 log(LOG_ERR, "nd6_prelist_add succedded but "
709 "no prefix\n");
710 return(EINVAL); /* XXX panic here? */
711 }
712 }
713 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
714 == NULL) {
715 /* XXX: this should not happen! */
716 log(LOG_ERR, "in6_control: addition succeeded, but"
717 " no ifaddr\n");
718 } else {
719 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
720 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
721 ia->ia6_ndpr = pr;
722 pr->ndpr_refcnt++;
723
724 /*
725 * If this is the first autoconf address from
726 * the prefix, create a temporary address
727 * as well (when specified).
728 */
729 if (ip6_use_tempaddr &&
730 pr->ndpr_refcnt == 1) {
731 int e;
732 if ((e = in6_tmpifadd(ia, 1)) != 0) {
733 log(LOG_NOTICE, "in6_control: "
734 "failed to create a "
735 "temporary address, "
736 "errno=%d\n",
737 e);
738 }
739 }
740 }
741
742 /*
743 * this might affect the status of autoconfigured
744 * addresses, that is, this address might make
745 * other addresses detached.
746 */
747 pfxlist_onlink_check();
748 }
749
750 dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET6, &dl_tag);
751 ia->ia_ifa.ifa_dlt = dl_tag;
752
753 in6_post_msg(ifp, KEV_INET6_NEW_USER_ADDR, ia);
754 break;
755 }
756
757 case SIOCDIFADDR_IN6:
758 {
759 int i = 0;
760 struct nd_prefix pr0, *pr;
761
762 /*
763 * If the address being deleted is the only one that owns
764 * the corresponding prefix, expire the prefix as well.
765 * XXX: theoretically, we don't have to warry about such
766 * relationship, since we separate the address management
767 * and the prefix management. We do this, however, to provide
768 * as much backward compatibility as possible in terms of
769 * the ioctl operation.
770 */
771 bzero(&pr0, sizeof(pr0));
772 pr0.ndpr_ifp = ifp;
773 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
774 NULL);
775 if (pr0.ndpr_plen == 128)
776 goto purgeaddr;
777 pr0.ndpr_prefix = ia->ia_addr;
778 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
779 for (i = 0; i < 4; i++) {
780 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
781 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
782 }
783 /*
784 * The logic of the following condition is a bit complicated.
785 * We expire the prefix when
786 * 1. the address obeys autoconfiguration and it is the
787 * only owner of the associated prefix, or
788 * 2. the address does not obey autoconf and there is no
789 * other owner of the prefix.
790 */
791 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
792 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
793 pr->ndpr_refcnt == 1) ||
794 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
795 pr->ndpr_refcnt == 0))) {
796 pr->ndpr_expire = 1; /* XXX: just for expiration */
797 }
798
799 purgeaddr:
800 in6_purgeaddr(&ia->ia_ifa);
801 break;
802 }
803
804 default:
805 #ifdef __APPLE__
806 error = dlil_ioctl(0, ifp, cmd, (caddr_t)data);
807 if (error == EOPNOTSUPP)
808 error = 0;
809 return error;
810
811 #else
812 if (ifp == NULL || ifp->if_ioctl == 0)
813 return(EOPNOTSUPP);
814 return((*ifp->if_ioctl)(ifp, cmd, data));
815 #endif
816 }
817
818 return(0);
819 }
820
821 /*
822 * Update parameters of an IPv6 interface address.
823 * If necessary, a new entry is created and linked into address chains.
824 * This function is separated from in6_control().
825 * XXX: should this be performed under splnet()?
826 */
827 int
828 in6_update_ifa(ifp, ifra, ia)
829 struct ifnet *ifp;
830 struct in6_aliasreq *ifra;
831 struct in6_ifaddr *ia;
832 {
833 int error = 0, hostIsNew = 0, plen = -1;
834 struct in6_ifaddr *oia;
835 struct sockaddr_in6 dst6;
836 struct in6_addrlifetime *lt;
837
838 /* Validate parameters */
839 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
840 return(EINVAL);
841
842 /*
843 * The destination address for a p2p link must have a family
844 * of AF_UNSPEC or AF_INET6.
845 */
846 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
847 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
848 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
849 return(EAFNOSUPPORT);
850 /*
851 * validate ifra_prefixmask. don't check sin6_family, netmask
852 * does not carry fields other than sin6_len.
853 */
854 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
855 return(EINVAL);
856 /*
857 * Because the IPv6 address architecture is classless, we require
858 * users to specify a (non 0) prefix length (mask) for a new address.
859 * We also require the prefix (when specified) mask is valid, and thus
860 * reject a non-consecutive mask.
861 */
862 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
863 return(EINVAL);
864 if (ifra->ifra_prefixmask.sin6_len != 0) {
865 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
866 (u_char *)&ifra->ifra_prefixmask +
867 ifra->ifra_prefixmask.sin6_len);
868 if (plen <= 0)
869 return(EINVAL);
870 }
871 else {
872 /*
873 * In this case, ia must not be NULL. We just use its prefix
874 * length.
875 */
876 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
877 }
878 /*
879 * If the destination address on a p2p interface is specified,
880 * and the address is a scoped one, validate/set the scope
881 * zone identifier.
882 */
883 dst6 = ifra->ifra_dstaddr;
884 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
885 (dst6.sin6_family == AF_INET6)) {
886 int scopeid;
887
888 #ifndef SCOPEDROUTING
889 if ((error = in6_recoverscope(&dst6,
890 &ifra->ifra_dstaddr.sin6_addr,
891 ifp)) != 0)
892 return(error);
893 #endif
894 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
895 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
896 dst6.sin6_scope_id = scopeid;
897 else if (dst6.sin6_scope_id != scopeid)
898 return(EINVAL); /* scope ID mismatch. */
899 #ifndef SCOPEDROUTING
900 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
901 != 0)
902 return(error);
903 dst6.sin6_scope_id = 0; /* XXX */
904 #endif
905 }
906 /*
907 * The destination address can be specified only for a p2p or a
908 * loopback interface. If specified, the corresponding prefix length
909 * must be 128.
910 */
911 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
912 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
913 /* XXX: noisy message */
914 log(LOG_INFO, "in6_update_ifa: a destination can be "
915 "specified for a p2p or a loopback IF only\n");
916 return(EINVAL);
917 }
918 if (plen != 128) {
919 /*
920 * The following message seems noisy, but we dare to
921 * add it for diagnosis.
922 */
923 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
924 "when dstaddr is specified\n");
925 return(EINVAL);
926 }
927 }
928 /* lifetime consistency check */
929 lt = &ifra->ifra_lifetime;
930 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
931 && lt->ia6t_vltime + time_second < time_second) {
932 return EINVAL;
933 }
934 if (lt->ia6t_vltime == 0) {
935 /*
936 * the following log might be noisy, but this is a typical
937 * configuration mistake or a tool's bug.
938 */
939 log(LOG_INFO,
940 "in6_update_ifa: valid lifetime is 0 for %s\n",
941 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
942 }
943 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
944 && lt->ia6t_pltime + time_second < time_second) {
945 return EINVAL;
946 }
947
948 /*
949 * If this is a new address, allocate a new ifaddr and link it
950 * into chains.
951 */
952 if (ia == NULL) {
953 hostIsNew = 1;
954 /*
955 * When in6_update_ifa() is called in a process of a received
956 * RA, it is called under splnet(). So, we should call malloc
957 * with M_NOWAIT.
958 */
959 ia = (struct in6_ifaddr *)
960 _MALLOC(sizeof(*ia), M_IFADDR, M_NOWAIT);
961 if (ia == NULL)
962 return (ENOBUFS);
963 bzero((caddr_t)ia, sizeof(*ia));
964 /* Initialize the address and masks */
965 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
966 ia->ia_addr.sin6_family = AF_INET6;
967 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
968 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
969 /*
970 * XXX: some functions expect that ifa_dstaddr is not
971 * NULL for p2p interfaces.
972 */
973 ia->ia_ifa.ifa_dstaddr
974 = (struct sockaddr *)&ia->ia_dstaddr;
975 } else {
976 ia->ia_ifa.ifa_dstaddr = NULL;
977 }
978 ia->ia_ifa.ifa_netmask
979 = (struct sockaddr *)&ia->ia_prefixmask;
980
981 ia->ia_ifp = ifp;
982 if ((oia = in6_ifaddr) != NULL) {
983 for ( ; oia->ia_next; oia = oia->ia_next)
984 continue;
985 oia->ia_next = ia;
986 } else
987 in6_ifaddr = ia;
988
989 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
990 ifa_list);
991 }
992
993 /* set prefix mask */
994 if (ifra->ifra_prefixmask.sin6_len) {
995 /*
996 * We prohibit changing the prefix length of an existing
997 * address, because
998 * + such an operation should be rare in IPv6, and
999 * + the operation would confuse prefix management.
1000 */
1001 if (ia->ia_prefixmask.sin6_len &&
1002 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1003 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
1004 " existing (%s) address should not be changed\n",
1005 ip6_sprintf(&ia->ia_addr.sin6_addr));
1006 error = EINVAL;
1007 goto unlink;
1008 }
1009 ia->ia_prefixmask = ifra->ifra_prefixmask;
1010 }
1011
1012 /*
1013 * If a new destination address is specified, scrub the old one and
1014 * install the new destination. Note that the interface must be
1015 * p2p or loopback (see the check above.)
1016 */
1017 if (dst6.sin6_family == AF_INET6 &&
1018 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
1019 &ia->ia_dstaddr.sin6_addr)) {
1020 int e;
1021
1022 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1023 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1024 != 0) {
1025 log(LOG_ERR, "in6_update_ifa: failed to remove "
1026 "a route to the old destination: %s\n",
1027 ip6_sprintf(&ia->ia_addr.sin6_addr));
1028 /* proceed anyway... */
1029 }
1030 else
1031 ia->ia_flags &= ~IFA_ROUTE;
1032 ia->ia_dstaddr = dst6;
1033 }
1034
1035 /* reset the interface and routing table appropriately. */
1036 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1037 goto unlink;
1038
1039 /*
1040 * Beyond this point, we should call in6_purgeaddr upon an error,
1041 * not just go to unlink.
1042 */
1043
1044 #if 0 /* disable this mechanism for now */
1045 /* update prefix list */
1046 if (hostIsNew &&
1047 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
1048 int iilen;
1049
1050 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
1051 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
1052 in6_purgeaddr((struct ifaddr *)ia);
1053 return(error);
1054 }
1055 }
1056 #endif
1057
1058 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1059 struct sockaddr_in6 mltaddr, mltmask;
1060 struct in6_multi *in6m;
1061
1062 if (hostIsNew) {
1063 /*
1064 * join solicited multicast addr for new host id
1065 */
1066 struct in6_addr llsol;
1067 bzero(&llsol, sizeof(struct in6_addr));
1068 llsol.s6_addr16[0] = htons(0xff02);
1069 llsol.s6_addr16[1] = htons(ifp->if_index);
1070 llsol.s6_addr32[1] = 0;
1071 llsol.s6_addr32[2] = htonl(1);
1072 llsol.s6_addr32[3] =
1073 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1074 llsol.s6_addr8[12] = 0xff;
1075 (void)in6_addmulti(&llsol, ifp, &error);
1076 if (error != 0) {
1077 log(LOG_WARNING,
1078 "in6_update_ifa: addmulti failed for "
1079 "%s on %s (errno=%d)\n",
1080 ip6_sprintf(&llsol), if_name(ifp),
1081 error);
1082 in6_purgeaddr((struct ifaddr *)ia);
1083 return(error);
1084 }
1085 }
1086
1087 bzero(&mltmask, sizeof(mltmask));
1088 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1089 mltmask.sin6_family = AF_INET6;
1090 mltmask.sin6_addr = in6mask32;
1091
1092 /*
1093 * join link-local all-nodes address
1094 */
1095 bzero(&mltaddr, sizeof(mltaddr));
1096 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1097 mltaddr.sin6_family = AF_INET6;
1098 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1099 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1100
1101 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1102 if (in6m == NULL) {
1103 rtrequest(RTM_ADD,
1104 (struct sockaddr *)&mltaddr,
1105 (struct sockaddr *)&ia->ia_addr,
1106 (struct sockaddr *)&mltmask,
1107 RTF_UP|RTF_CLONING, /* xxx */
1108 (struct rtentry **)0);
1109 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1110 if (error != 0) {
1111 log(LOG_WARNING,
1112 "in6_update_ifa: addmulti failed for "
1113 "%s on %s (errno=%d)\n",
1114 ip6_sprintf(&mltaddr.sin6_addr),
1115 if_name(ifp), error);
1116 }
1117 }
1118
1119 /*
1120 * join node information group address
1121 */
1122 #define hostnamelen strlen(hostname)
1123 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1124 == 0) {
1125 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1126 if (in6m == NULL && ia != NULL) {
1127 (void)in6_addmulti(&mltaddr.sin6_addr,
1128 ifp, &error);
1129 if (error != 0) {
1130 log(LOG_WARNING, "in6_update_ifa: "
1131 "addmulti failed for "
1132 "%s on %s (errno=%d)\n",
1133 ip6_sprintf(&mltaddr.sin6_addr),
1134 if_name(ifp), error);
1135 }
1136 }
1137 }
1138 #undef hostnamelen
1139
1140 /*
1141 * join node-local all-nodes address, on loopback.
1142 * XXX: since "node-local" is obsoleted by interface-local,
1143 * we have to join the group on every interface with
1144 * some interface-boundary restriction.
1145 */
1146 if (ifp->if_flags & IFF_LOOPBACK) {
1147 struct in6_ifaddr *ia_loop;
1148
1149 struct in6_addr loop6 = in6addr_loopback;
1150 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1151
1152 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1153
1154 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1155 if (in6m == NULL && ia_loop != NULL) {
1156 rtrequest(RTM_ADD,
1157 (struct sockaddr *)&mltaddr,
1158 (struct sockaddr *)&ia_loop->ia_addr,
1159 (struct sockaddr *)&mltmask,
1160 RTF_UP,
1161 (struct rtentry **)0);
1162 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1163 &error);
1164 if (error != 0) {
1165 log(LOG_WARNING, "in6_update_ifa: "
1166 "addmulti failed for %s on %s "
1167 "(errno=%d)\n",
1168 ip6_sprintf(&mltaddr.sin6_addr),
1169 if_name(ifp), error);
1170 }
1171 }
1172 }
1173 }
1174
1175 ia->ia6_flags = ifra->ifra_flags;
1176 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1177 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1178
1179 ia->ia6_lifetime = ifra->ifra_lifetime;
1180 /* for sanity */
1181 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1182 ia->ia6_lifetime.ia6t_expire =
1183 time_second + ia->ia6_lifetime.ia6t_vltime;
1184 } else
1185 ia->ia6_lifetime.ia6t_expire = 0;
1186 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1187 ia->ia6_lifetime.ia6t_preferred =
1188 time_second + ia->ia6_lifetime.ia6t_pltime;
1189 } else
1190 ia->ia6_lifetime.ia6t_preferred = 0;
1191
1192 /*
1193 * make sure to initialize ND6 information. this is to workaround
1194 * issues with interfaces with IPv6 addresses, which have never brought
1195 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1196 */
1197 nd6_ifattach(ifp);
1198
1199 /*
1200 * Perform DAD, if needed.
1201 * XXX It may be of use, if we can administratively
1202 * disable DAD.
1203 */
1204 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1205 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1206 nd6_dad_start((struct ifaddr *)ia, NULL);
1207 }
1208
1209 return(error);
1210
1211 unlink:
1212 /*
1213 * XXX: if a change of an existing address failed, keep the entry
1214 * anyway.
1215 */
1216 if (hostIsNew)
1217 in6_unlink_ifa(ia, ifp);
1218 return(error);
1219 }
1220
1221 void
1222 in6_purgeaddr(ifa)
1223 struct ifaddr *ifa;
1224 {
1225 struct ifnet *ifp = ifa->ifa_ifp;
1226 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1227
1228 /* stop DAD processing */
1229 nd6_dad_stoptimer(ifa);
1230
1231 /*
1232 * delete route to the destination of the address being purged.
1233 * The interface must be p2p or loopback in this case.
1234 */
1235 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1236 int e;
1237
1238 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1239 != 0) {
1240 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1241 "a route to the p2p destination: %s on %s, "
1242 "errno=%d\n",
1243 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1244 e);
1245 /* proceed anyway... */
1246 }
1247 else
1248 ia->ia_flags &= ~IFA_ROUTE;
1249 }
1250
1251 /* Remove ownaddr's loopback rtentry, if it exists. */
1252 in6_ifremloop(&(ia->ia_ifa));
1253
1254 if (ifp->if_flags & IFF_MULTICAST) {
1255 /*
1256 * delete solicited multicast addr for deleting host id
1257 */
1258 struct in6_multi *in6m;
1259 struct in6_addr llsol;
1260 bzero(&llsol, sizeof(struct in6_addr));
1261 llsol.s6_addr16[0] = htons(0xff02);
1262 llsol.s6_addr16[1] = htons(ifp->if_index);
1263 llsol.s6_addr32[1] = 0;
1264 llsol.s6_addr32[2] = htonl(1);
1265 llsol.s6_addr32[3] =
1266 ia->ia_addr.sin6_addr.s6_addr32[3];
1267 llsol.s6_addr8[12] = 0xff;
1268
1269 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1270 if (in6m)
1271 in6_delmulti(in6m);
1272 }
1273
1274 in6_post_msg(ifp, KEV_INET6_ADDR_DELETED, ia);
1275 in6_unlink_ifa(ia, ifp);
1276 }
1277
1278 static void
1279 in6_unlink_ifa(ia, ifp)
1280 struct in6_ifaddr *ia;
1281 struct ifnet *ifp;
1282 {
1283 int plen, iilen;
1284 struct in6_ifaddr *oia;
1285 int s = splnet();
1286
1287 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1288
1289 oia = ia;
1290 if (oia == (ia = in6_ifaddr))
1291 in6_ifaddr = ia->ia_next;
1292 else {
1293 while (ia->ia_next && (ia->ia_next != oia))
1294 ia = ia->ia_next;
1295 if (ia->ia_next)
1296 ia->ia_next = oia->ia_next;
1297 else {
1298 /* search failed */
1299 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1300 }
1301 }
1302
1303 if (oia->ia6_ifpr) { /* check for safety */
1304 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1305 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1306 in6_prefix_remove_ifid(iilen, oia);
1307 }
1308
1309 /*
1310 * When an autoconfigured address is being removed, release the
1311 * reference to the base prefix. Also, since the release might
1312 * affect the status of other (detached) addresses, call
1313 * pfxlist_onlink_check().
1314 */
1315 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1316 if (oia->ia6_ndpr == NULL) {
1317 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1318 "%p has no prefix\n", oia);
1319 } else {
1320 oia->ia6_ndpr->ndpr_refcnt--;
1321 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1322 oia->ia6_ndpr = NULL;
1323 }
1324
1325 pfxlist_onlink_check();
1326 }
1327
1328 /*
1329 * release another refcnt for the link from in6_ifaddr.
1330 * Note that we should decrement the refcnt at least once for all *BSD.
1331 */
1332 ifafree(&oia->ia_ifa);
1333
1334 splx(s);
1335 }
1336
1337 void
1338 in6_purgeif(ifp)
1339 struct ifnet *ifp;
1340 {
1341 struct ifaddr *ifa, *nifa = NULL;
1342
1343 if (ifp == NULL || &ifp->if_addrlist == NULL)
1344 return;
1345
1346 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1347 {
1348 nifa = TAILQ_NEXT(ifa, ifa_list);
1349 if (ifa->ifa_addr == NULL)
1350 continue;
1351 if (ifa->ifa_addr->sa_family != AF_INET6)
1352 continue;
1353 in6_purgeaddr(ifa);
1354 }
1355
1356 in6_ifdetach(ifp);
1357 }
1358
1359 /*
1360 * SIOC[GAD]LIFADDR.
1361 * SIOCGLIFADDR: get first address. (?)
1362 * SIOCGLIFADDR with IFLR_PREFIX:
1363 * get first address that matches the specified prefix.
1364 * SIOCALIFADDR: add the specified address.
1365 * SIOCALIFADDR with IFLR_PREFIX:
1366 * add the specified prefix, filling hostid part from
1367 * the first link-local address. prefixlen must be <= 64.
1368 * SIOCDLIFADDR: delete the specified address.
1369 * SIOCDLIFADDR with IFLR_PREFIX:
1370 * delete the first address that matches the specified prefix.
1371 * return values:
1372 * EINVAL on invalid parameters
1373 * EADDRNOTAVAIL on prefix match failed/specified address not found
1374 * other values may be returned from in6_ioctl()
1375 *
1376 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1377 * this is to accomodate address naming scheme other than RFC2374,
1378 * in the future.
1379 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1380 * address encoding scheme. (see figure on page 8)
1381 */
1382 static int
1383 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
1384 struct socket *so;
1385 u_long cmd;
1386 caddr_t data;
1387 struct ifnet *ifp;
1388 struct proc *p;
1389 {
1390 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1391 struct ifaddr *ifa;
1392 struct sockaddr *sa;
1393
1394 /* sanity checks */
1395 if (!data || !ifp) {
1396 panic("invalid argument to in6_lifaddr_ioctl");
1397 /*NOTRECHED*/
1398 }
1399
1400 switch (cmd) {
1401 case SIOCGLIFADDR:
1402 /* address must be specified on GET with IFLR_PREFIX */
1403 if ((iflr->flags & IFLR_PREFIX) == 0)
1404 break;
1405 /*FALLTHROUGH*/
1406 case SIOCALIFADDR:
1407 case SIOCDLIFADDR:
1408 /* address must be specified on ADD and DELETE */
1409 sa = (struct sockaddr *)&iflr->addr;
1410 if (sa->sa_family != AF_INET6)
1411 return EINVAL;
1412 if (sa->sa_len != sizeof(struct sockaddr_in6))
1413 return EINVAL;
1414 /* XXX need improvement */
1415 sa = (struct sockaddr *)&iflr->dstaddr;
1416 if (sa->sa_family && sa->sa_family != AF_INET6)
1417 return EINVAL;
1418 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1419 return EINVAL;
1420 break;
1421 default: /*shouldn't happen*/
1422 #if 0
1423 panic("invalid cmd to in6_lifaddr_ioctl");
1424 /*NOTREACHED*/
1425 #else
1426 return EOPNOTSUPP;
1427 #endif
1428 }
1429 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1430 return EINVAL;
1431
1432 switch (cmd) {
1433 case SIOCALIFADDR:
1434 {
1435 struct in6_aliasreq ifra;
1436 struct in6_addr *hostid = NULL;
1437 int prefixlen;
1438
1439 if ((iflr->flags & IFLR_PREFIX) != 0) {
1440 struct sockaddr_in6 *sin6;
1441
1442 /*
1443 * hostid is to fill in the hostid part of the
1444 * address. hostid points to the first link-local
1445 * address attached to the interface.
1446 */
1447 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1448 if (!ifa)
1449 return EADDRNOTAVAIL;
1450 hostid = IFA_IN6(ifa);
1451
1452 /* prefixlen must be <= 64. */
1453 if (64 < iflr->prefixlen)
1454 return EINVAL;
1455 prefixlen = iflr->prefixlen;
1456
1457 /* hostid part must be zero. */
1458 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1459 if (sin6->sin6_addr.s6_addr32[2] != 0
1460 || sin6->sin6_addr.s6_addr32[3] != 0) {
1461 return EINVAL;
1462 }
1463 } else
1464 prefixlen = iflr->prefixlen;
1465
1466 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1467 bzero(&ifra, sizeof(ifra));
1468 bcopy(iflr->iflr_name, ifra.ifra_name,
1469 sizeof(ifra.ifra_name));
1470
1471 bcopy(&iflr->addr, &ifra.ifra_addr,
1472 ((struct sockaddr *)&iflr->addr)->sa_len);
1473 if (hostid) {
1474 /* fill in hostid part */
1475 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1476 hostid->s6_addr32[2];
1477 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1478 hostid->s6_addr32[3];
1479 }
1480
1481 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1482 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1483 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1484 if (hostid) {
1485 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1486 hostid->s6_addr32[2];
1487 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1488 hostid->s6_addr32[3];
1489 }
1490 }
1491
1492 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1493 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1494
1495 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1496 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1497 }
1498 case SIOCGLIFADDR:
1499 case SIOCDLIFADDR:
1500 {
1501 struct in6_ifaddr *ia;
1502 struct in6_addr mask, candidate, match;
1503 struct sockaddr_in6 *sin6;
1504 int cmp;
1505
1506 bzero(&mask, sizeof(mask));
1507 if (iflr->flags & IFLR_PREFIX) {
1508 /* lookup a prefix rather than address. */
1509 in6_len2mask(&mask, iflr->prefixlen);
1510
1511 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1512 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1513 match.s6_addr32[0] &= mask.s6_addr32[0];
1514 match.s6_addr32[1] &= mask.s6_addr32[1];
1515 match.s6_addr32[2] &= mask.s6_addr32[2];
1516 match.s6_addr32[3] &= mask.s6_addr32[3];
1517
1518 /* if you set extra bits, that's wrong */
1519 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1520 return EINVAL;
1521
1522 cmp = 1;
1523 } else {
1524 if (cmd == SIOCGLIFADDR) {
1525 /* on getting an address, take the 1st match */
1526 cmp = 0; /*XXX*/
1527 } else {
1528 /* on deleting an address, do exact match */
1529 in6_len2mask(&mask, 128);
1530 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1531 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1532
1533 cmp = 1;
1534 }
1535 }
1536
1537 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1538 {
1539 if (ifa->ifa_addr->sa_family != AF_INET6)
1540 continue;
1541 if (!cmp)
1542 break;
1543
1544 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1545 #ifndef SCOPEDROUTING
1546 /*
1547 * XXX: this is adhoc, but is necessary to allow
1548 * a user to specify fe80::/64 (not /10) for a
1549 * link-local address.
1550 */
1551 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1552 candidate.s6_addr16[1] = 0;
1553 #endif
1554 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1555 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1556 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1557 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1558 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1559 break;
1560 }
1561 if (!ifa)
1562 return EADDRNOTAVAIL;
1563 ia = ifa2ia6(ifa);
1564
1565 if (cmd == SIOCGLIFADDR) {
1566 #ifndef SCOPEDROUTING
1567 struct sockaddr_in6 *s6;
1568 #endif
1569
1570 /* fill in the if_laddrreq structure */
1571 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1572 #ifndef SCOPEDROUTING /* XXX see above */
1573 s6 = (struct sockaddr_in6 *)&iflr->addr;
1574 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1575 s6->sin6_addr.s6_addr16[1] = 0;
1576 s6->sin6_scope_id =
1577 in6_addr2scopeid(ifp, &s6->sin6_addr);
1578 }
1579 #endif
1580 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1581 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1582 ia->ia_dstaddr.sin6_len);
1583 #ifndef SCOPEDROUTING /* XXX see above */
1584 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1585 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1586 s6->sin6_addr.s6_addr16[1] = 0;
1587 s6->sin6_scope_id =
1588 in6_addr2scopeid(ifp,
1589 &s6->sin6_addr);
1590 }
1591 #endif
1592 } else
1593 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1594
1595 iflr->prefixlen =
1596 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1597 NULL);
1598
1599 iflr->flags = ia->ia6_flags; /*XXX*/
1600
1601 return 0;
1602 } else {
1603 struct in6_aliasreq ifra;
1604
1605 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1606 bzero(&ifra, sizeof(ifra));
1607 bcopy(iflr->iflr_name, ifra.ifra_name,
1608 sizeof(ifra.ifra_name));
1609
1610 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1611 ia->ia_addr.sin6_len);
1612 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1613 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1614 ia->ia_dstaddr.sin6_len);
1615 } else {
1616 bzero(&ifra.ifra_dstaddr,
1617 sizeof(ifra.ifra_dstaddr));
1618 }
1619 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1620 ia->ia_prefixmask.sin6_len);
1621
1622 ifra.ifra_flags = ia->ia6_flags;
1623 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1624 ifp, p);
1625 }
1626 }
1627 }
1628
1629 return EOPNOTSUPP; /*just for safety*/
1630 }
1631
1632 /*
1633 * Initialize an interface's intetnet6 address
1634 * and routing table entry.
1635 */
1636 static int
1637 in6_ifinit(ifp, ia, sin6, newhost)
1638 struct ifnet *ifp;
1639 struct in6_ifaddr *ia;
1640 struct sockaddr_in6 *sin6;
1641 int newhost;
1642 {
1643 int error = 0, plen, ifacount = 0;
1644 int s = splimp();
1645 struct ifaddr *ifa;
1646
1647 /*
1648 * Give the interface a chance to initialize
1649 * if this is its first address,
1650 * and to validate the address if necessary.
1651 */
1652 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1653 {
1654 if (ifa->ifa_addr == NULL)
1655 continue; /* just for safety */
1656 if (ifa->ifa_addr->sa_family != AF_INET6)
1657 continue;
1658 ifacount++;
1659 }
1660
1661 ia->ia_addr = *sin6;
1662
1663
1664 if (ifacount <= 1 &&
1665 #ifdef __APPLE__
1666 (error = dlil_ioctl(0, ifp, SIOCSIFADDR, (caddr_t)ia))) {
1667 if (error == EOPNOTSUPP)
1668 error = 0;
1669 if (error) {
1670 splx(s);
1671 return(error);
1672 }
1673 }
1674 #else
1675 ifp->if_ioctl && (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1676 splx(s);
1677 return(error);
1678 }
1679 #endif
1680 splx(s);
1681
1682 ia->ia_ifa.ifa_metric = ifp->if_metric;
1683
1684 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1685
1686 /*
1687 * Special case:
1688 * If the destination address is specified for a point-to-point
1689 * interface, install a route to the destination as an interface
1690 * direct route.
1691 */
1692 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1693 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1694 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1695 RTF_UP | RTF_HOST)) != 0)
1696 return(error);
1697 ia->ia_flags |= IFA_ROUTE;
1698 }
1699 if (plen < 128) {
1700 /*
1701 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1702 */
1703 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1704 }
1705
1706 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1707 if (newhost) {
1708 /* set the rtrequest function to create llinfo */
1709 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1710 in6_ifaddloop(&(ia->ia_ifa));
1711 }
1712
1713 return(error);
1714 }
1715
1716 /*
1717 * Add an address to the list of IP6 multicast addresses for a
1718 * given interface.
1719 */
1720 struct in6_multi *
1721 in6_addmulti(maddr6, ifp, errorp)
1722 struct in6_addr *maddr6;
1723 struct ifnet *ifp;
1724 int *errorp;
1725 {
1726 struct in6_multi *in6m;
1727 struct sockaddr_in6 sin6;
1728 struct ifmultiaddr *ifma;
1729 int s = splnet();
1730
1731 *errorp = 0;
1732
1733 /*
1734 * Call generic routine to add membership or increment
1735 * refcount. It wants addresses in the form of a sockaddr,
1736 * so we build one here (being careful to zero the unused bytes).
1737 */
1738 bzero(&sin6, sizeof sin6);
1739 sin6.sin6_family = AF_INET6;
1740 sin6.sin6_len = sizeof sin6;
1741 sin6.sin6_addr = *maddr6;
1742 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1743 if (*errorp) {
1744 splx(s);
1745 return 0;
1746 }
1747
1748 /*
1749 * If ifma->ifma_protospec is null, then if_addmulti() created
1750 * a new record. Otherwise, we are done.
1751 */
1752 if (ifma->ifma_protospec != 0)
1753 return ifma->ifma_protospec;
1754
1755 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1756 at interrupt time? If so, need to fix if_addmulti. XXX */
1757 in6m = (struct in6_multi *)_MALLOC(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1758 if (in6m == NULL) {
1759 splx(s);
1760 return (NULL);
1761 }
1762
1763 bzero(in6m, sizeof *in6m);
1764 in6m->in6m_addr = *maddr6;
1765 in6m->in6m_ifp = ifp;
1766 in6m->in6m_ifma = ifma;
1767 ifma->ifma_protospec = in6m;
1768 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1769
1770 /*
1771 * Let MLD6 know that we have joined a new IP6 multicast
1772 * group.
1773 */
1774 mld6_start_listening(in6m);
1775 splx(s);
1776 return(in6m);
1777 }
1778
1779 /*
1780 * Delete a multicast address record.
1781 */
1782 void
1783 in6_delmulti(in6m)
1784 struct in6_multi *in6m;
1785 {
1786 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1787 int s = splnet();
1788
1789 if (ifma->ifma_refcount == 1) {
1790 /*
1791 * No remaining claims to this record; let MLD6 know
1792 * that we are leaving the multicast group.
1793 */
1794 mld6_stop_listening(in6m);
1795 ifma->ifma_protospec = 0;
1796 LIST_REMOVE(in6m, in6m_entry);
1797 FREE(in6m, M_IPMADDR);
1798 }
1799 /* XXX - should be separate API for when we have an ifma? */
1800 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1801 splx(s);
1802 }
1803
1804 /*
1805 * Find an IPv6 interface link-local address specific to an interface.
1806 */
1807 struct in6_ifaddr *
1808 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1809 struct ifnet *ifp;
1810 int ignoreflags;
1811 {
1812 struct ifaddr *ifa;
1813
1814 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1815 {
1816 if (ifa->ifa_addr == NULL)
1817 continue; /* just for safety */
1818 if (ifa->ifa_addr->sa_family != AF_INET6)
1819 continue;
1820 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1821 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1822 ignoreflags) != 0)
1823 continue;
1824 break;
1825 }
1826 }
1827
1828 return((struct in6_ifaddr *)ifa);
1829 }
1830
1831
1832 /*
1833 * find the internet address corresponding to a given interface and address.
1834 */
1835 struct in6_ifaddr *
1836 in6ifa_ifpwithaddr(ifp, addr)
1837 struct ifnet *ifp;
1838 struct in6_addr *addr;
1839 {
1840 struct ifaddr *ifa;
1841
1842 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1843 {
1844 if (ifa->ifa_addr == NULL)
1845 continue; /* just for safety */
1846 if (ifa->ifa_addr->sa_family != AF_INET6)
1847 continue;
1848 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1849 break;
1850 }
1851
1852 return((struct in6_ifaddr *)ifa);
1853 }
1854
1855 /*
1856 * Convert IP6 address to printable (loggable) representation.
1857 */
1858 static char digits[] = "0123456789abcdef";
1859 static int ip6round = 0;
1860 char *
1861 ip6_sprintf(addr)
1862 const struct in6_addr *addr;
1863 {
1864 static char ip6buf[8][48];
1865 int i;
1866 char *cp;
1867 u_short *a = (u_short *)addr;
1868 u_char *d;
1869 int dcolon = 0;
1870
1871 ip6round = (ip6round + 1) & 7;
1872 cp = ip6buf[ip6round];
1873
1874 for (i = 0; i < 8; i++) {
1875 if (dcolon == 1) {
1876 if (*a == 0) {
1877 if (i == 7)
1878 *cp++ = ':';
1879 a++;
1880 continue;
1881 } else
1882 dcolon = 2;
1883 }
1884 if (*a == 0) {
1885 if (dcolon == 0 && *(a + 1) == 0) {
1886 if (i == 0)
1887 *cp++ = ':';
1888 *cp++ = ':';
1889 dcolon = 1;
1890 } else {
1891 *cp++ = '0';
1892 *cp++ = ':';
1893 }
1894 a++;
1895 continue;
1896 }
1897 d = (u_char *)a;
1898 *cp++ = digits[*d >> 4];
1899 *cp++ = digits[*d++ & 0xf];
1900 *cp++ = digits[*d >> 4];
1901 *cp++ = digits[*d & 0xf];
1902 *cp++ = ':';
1903 a++;
1904 }
1905 *--cp = 0;
1906 return(ip6buf[ip6round]);
1907 }
1908
1909 int
1910 in6_localaddr(in6)
1911 struct in6_addr *in6;
1912 {
1913 struct in6_ifaddr *ia;
1914
1915 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1916 return 1;
1917
1918 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1919 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1920 &ia->ia_prefixmask.sin6_addr))
1921 return 1;
1922
1923 return (0);
1924 }
1925
1926 int
1927 in6_is_addr_deprecated(sa6)
1928 struct sockaddr_in6 *sa6;
1929 {
1930 struct in6_ifaddr *ia;
1931
1932 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1933 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1934 &sa6->sin6_addr) &&
1935 #if SCOPEDROUTING
1936 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1937 #endif
1938 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1939 return(1); /* true */
1940
1941 /* XXX: do we still have to go thru the rest of the list? */
1942 }
1943
1944 return(0); /* false */
1945 }
1946
1947 /*
1948 * return length of part which dst and src are equal
1949 * hard coding...
1950 */
1951 int
1952 in6_matchlen(src, dst)
1953 struct in6_addr *src, *dst;
1954 {
1955 int match = 0;
1956 u_char *s = (u_char *)src, *d = (u_char *)dst;
1957 u_char *lim = s + 16, r;
1958
1959 while (s < lim)
1960 if ((r = (*d++ ^ *s++)) != 0) {
1961 while (r < 128) {
1962 match++;
1963 r <<= 1;
1964 }
1965 break;
1966 } else
1967 match += 8;
1968 return match;
1969 }
1970
1971 /* XXX: to be scope conscious */
1972 int
1973 in6_are_prefix_equal(p1, p2, len)
1974 struct in6_addr *p1, *p2;
1975 int len;
1976 {
1977 int bytelen, bitlen;
1978
1979 /* sanity check */
1980 if (0 > len || len > 128) {
1981 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1982 len);
1983 return(0);
1984 }
1985
1986 bytelen = len / 8;
1987 bitlen = len % 8;
1988
1989 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1990 return(0);
1991 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1992 p2->s6_addr[bytelen] >> (8 - bitlen))
1993 return(0);
1994
1995 return(1);
1996 }
1997
1998 void
1999 in6_prefixlen2mask(maskp, len)
2000 struct in6_addr *maskp;
2001 int len;
2002 {
2003 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2004 int bytelen, bitlen, i;
2005
2006 /* sanity check */
2007 if (0 > len || len > 128) {
2008 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2009 len);
2010 return;
2011 }
2012
2013 bzero(maskp, sizeof(*maskp));
2014 bytelen = len / 8;
2015 bitlen = len % 8;
2016 for (i = 0; i < bytelen; i++)
2017 maskp->s6_addr[i] = 0xff;
2018 if (bitlen)
2019 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2020 }
2021
2022 /*
2023 * return the best address out of the same scope
2024 */
2025 struct in6_ifaddr *
2026 in6_ifawithscope(oifp, dst)
2027 struct ifnet *oifp;
2028 struct in6_addr *dst;
2029 {
2030 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2031 int blen = -1;
2032 struct ifaddr *ifa;
2033 struct ifnet *ifp;
2034 struct in6_ifaddr *ifa_best = NULL;
2035
2036 if (oifp == NULL) {
2037 #if 0
2038 printf("in6_ifawithscope: output interface is not specified\n");
2039 #endif
2040 return(NULL);
2041 }
2042
2043 /*
2044 * We search for all addresses on all interfaces from the beginning.
2045 * Comparing an interface with the outgoing interface will be done
2046 * only at the final stage of tiebreaking.
2047 */
2048 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2049 {
2050 /*
2051 * We can never take an address that breaks the scope zone
2052 * of the destination.
2053 */
2054 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2055 continue;
2056
2057 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2058 {
2059 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2060
2061 if (ifa->ifa_addr->sa_family != AF_INET6)
2062 continue;
2063
2064 src_scope = in6_addrscope(IFA_IN6(ifa));
2065
2066 /*
2067 * Don't use an address before completing DAD
2068 * nor a duplicated address.
2069 */
2070 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2071 IN6_IFF_NOTREADY)
2072 continue;
2073
2074 /* XXX: is there any case to allow anycasts? */
2075 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2076 IN6_IFF_ANYCAST)
2077 continue;
2078
2079 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2080 IN6_IFF_DETACHED)
2081 continue;
2082
2083 /*
2084 * If this is the first address we find,
2085 * keep it anyway.
2086 */
2087 if (ifa_best == NULL)
2088 goto replace;
2089
2090 /*
2091 * ifa_best is never NULL beyond this line except
2092 * within the block labeled "replace".
2093 */
2094
2095 /*
2096 * If ifa_best has a smaller scope than dst and
2097 * the current address has a larger one than
2098 * (or equal to) dst, always replace ifa_best.
2099 * Also, if the current address has a smaller scope
2100 * than dst, ignore it unless ifa_best also has a
2101 * smaller scope.
2102 * Consequently, after the two if-clause below,
2103 * the followings must be satisfied:
2104 * (scope(src) < scope(dst) &&
2105 * scope(best) < scope(dst))
2106 * OR
2107 * (scope(best) >= scope(dst) &&
2108 * scope(src) >= scope(dst))
2109 */
2110 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2111 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2112 goto replace; /* (A) */
2113 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2114 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2115 continue; /* (B) */
2116
2117 /*
2118 * A deprecated address SHOULD NOT be used in new
2119 * communications if an alternate (non-deprecated)
2120 * address is available and has sufficient scope.
2121 * RFC 2462, Section 5.5.4.
2122 */
2123 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2124 IN6_IFF_DEPRECATED) {
2125 /*
2126 * Ignore any deprecated addresses if
2127 * specified by configuration.
2128 */
2129 if (!ip6_use_deprecated)
2130 continue;
2131
2132 /*
2133 * If we have already found a non-deprecated
2134 * candidate, just ignore deprecated addresses.
2135 */
2136 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2137 == 0)
2138 continue;
2139 }
2140
2141 /*
2142 * A non-deprecated address is always preferred
2143 * to a deprecated one regardless of scopes and
2144 * address matching (Note invariants ensured by the
2145 * conditions (A) and (B) above.)
2146 */
2147 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2148 (((struct in6_ifaddr *)ifa)->ia6_flags &
2149 IN6_IFF_DEPRECATED) == 0)
2150 goto replace;
2151
2152 /*
2153 * When we use temporary addresses described in
2154 * RFC 3041, we prefer temporary addresses to
2155 * public autoconf addresses. Again, note the
2156 * invariants from (A) and (B). Also note that we
2157 * don't have any preference between static addresses
2158 * and autoconf addresses (despite of whether or not
2159 * the latter is temporary or public.)
2160 */
2161 if (ip6_use_tempaddr) {
2162 struct in6_ifaddr *ifat;
2163
2164 ifat = (struct in6_ifaddr *)ifa;
2165 if ((ifa_best->ia6_flags &
2166 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2167 == IN6_IFF_AUTOCONF &&
2168 (ifat->ia6_flags &
2169 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2170 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2171 goto replace;
2172 }
2173 if ((ifa_best->ia6_flags &
2174 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2175 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2176 (ifat->ia6_flags &
2177 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2178 == IN6_IFF_AUTOCONF) {
2179 continue;
2180 }
2181 }
2182
2183 /*
2184 * At this point, we have two cases:
2185 * 1. we are looking at a non-deprecated address,
2186 * and ifa_best is also non-deprecated.
2187 * 2. we are looking at a deprecated address,
2188 * and ifa_best is also deprecated.
2189 * Also, we do not have to consider a case where
2190 * the scope of if_best is larger(smaller) than dst and
2191 * the scope of the current address is smaller(larger)
2192 * than dst. Such a case has already been covered.
2193 * Tiebreaking is done according to the following
2194 * items:
2195 * - the scope comparison between the address and
2196 * dst (dscopecmp)
2197 * - the scope comparison between the address and
2198 * ifa_best (bscopecmp)
2199 * - if the address match dst longer than ifa_best
2200 * (matchcmp)
2201 * - if the address is on the outgoing I/F (outI/F)
2202 *
2203 * Roughly speaking, the selection policy is
2204 * - the most important item is scope. The same scope
2205 * is best. Then search for a larger scope.
2206 * Smaller scopes are the last resort.
2207 * - A deprecated address is chosen only when we have
2208 * no address that has an enough scope, but is
2209 * prefered to any addresses of smaller scopes
2210 * (this must be already done above.)
2211 * - addresses on the outgoing I/F are preferred to
2212 * ones on other interfaces if none of above
2213 * tiebreaks. In the table below, the column "bI"
2214 * means if the best_ifa is on the outgoing
2215 * interface, and the column "sI" means if the ifa
2216 * is on the outgoing interface.
2217 * - If there is no other reasons to choose one,
2218 * longest address match against dst is considered.
2219 *
2220 * The precise decision table is as follows:
2221 * dscopecmp bscopecmp match bI oI | replace?
2222 * N/A equal N/A Y N | No (1)
2223 * N/A equal N/A N Y | Yes (2)
2224 * N/A equal larger N/A | Yes (3)
2225 * N/A equal !larger N/A | No (4)
2226 * larger larger N/A N/A | No (5)
2227 * larger smaller N/A N/A | Yes (6)
2228 * smaller larger N/A N/A | Yes (7)
2229 * smaller smaller N/A N/A | No (8)
2230 * equal smaller N/A N/A | Yes (9)
2231 * equal larger (already done at A above)
2232 */
2233 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2234 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2235
2236 if (bscopecmp == 0) {
2237 struct ifnet *bifp = ifa_best->ia_ifp;
2238
2239 if (bifp == oifp && ifp != oifp) /* (1) */
2240 continue;
2241 if (bifp != oifp && ifp == oifp) /* (2) */
2242 goto replace;
2243
2244 /*
2245 * Both bifp and ifp are on the outgoing
2246 * interface, or both two are on a different
2247 * interface from the outgoing I/F.
2248 * now we need address matching against dst
2249 * for tiebreaking.
2250 */
2251 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2252 matchcmp = tlen - blen;
2253 if (matchcmp > 0) /* (3) */
2254 goto replace;
2255 continue; /* (4) */
2256 }
2257 if (dscopecmp > 0) {
2258 if (bscopecmp > 0) /* (5) */
2259 continue;
2260 goto replace; /* (6) */
2261 }
2262 if (dscopecmp < 0) {
2263 if (bscopecmp > 0) /* (7) */
2264 goto replace;
2265 continue; /* (8) */
2266 }
2267
2268 /* now dscopecmp must be 0 */
2269 if (bscopecmp < 0)
2270 goto replace; /* (9) */
2271
2272 replace:
2273 ifa_best = (struct in6_ifaddr *)ifa;
2274 blen = tlen >= 0 ? tlen :
2275 in6_matchlen(IFA_IN6(ifa), dst);
2276 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2277 }
2278 }
2279
2280 /* count statistics for future improvements */
2281 if (ifa_best == NULL)
2282 ip6stat.ip6s_sources_none++;
2283 else {
2284 if (oifp == ifa_best->ia_ifp)
2285 ip6stat.ip6s_sources_sameif[best_scope]++;
2286 else
2287 ip6stat.ip6s_sources_otherif[best_scope]++;
2288
2289 if (best_scope == dst_scope)
2290 ip6stat.ip6s_sources_samescope[best_scope]++;
2291 else
2292 ip6stat.ip6s_sources_otherscope[best_scope]++;
2293
2294 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2295 ip6stat.ip6s_sources_deprecated[best_scope]++;
2296 }
2297
2298 return(ifa_best);
2299 }
2300
2301 /*
2302 * return the best address out of the same scope. if no address was
2303 * found, return the first valid address from designated IF.
2304 */
2305 struct in6_ifaddr *
2306 in6_ifawithifp(ifp, dst)
2307 struct ifnet *ifp;
2308 struct in6_addr *dst;
2309 {
2310 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2311 struct ifaddr *ifa;
2312 struct in6_ifaddr *besta = 0;
2313 struct in6_ifaddr *dep[2]; /*last-resort: deprecated*/
2314
2315 dep[0] = dep[1] = NULL;
2316
2317 /*
2318 * We first look for addresses in the same scope.
2319 * If there is one, return it.
2320 * If two or more, return one which matches the dst longest.
2321 * If none, return one of global addresses assigned other ifs.
2322 */
2323 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2324 {
2325 if (ifa->ifa_addr->sa_family != AF_INET6)
2326 continue;
2327 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2328 continue; /* XXX: is there any case to allow anycast? */
2329 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2330 continue; /* don't use this interface */
2331 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2332 continue;
2333 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2334 if (ip6_use_deprecated)
2335 dep[0] = (struct in6_ifaddr *)ifa;
2336 continue;
2337 }
2338
2339 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2340 /*
2341 * call in6_matchlen() as few as possible
2342 */
2343 if (besta) {
2344 if (blen == -1)
2345 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2346 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2347 if (tlen > blen) {
2348 blen = tlen;
2349 besta = (struct in6_ifaddr *)ifa;
2350 }
2351 } else
2352 besta = (struct in6_ifaddr *)ifa;
2353 }
2354 }
2355 if (besta)
2356 return(besta);
2357
2358 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2359 {
2360 if (ifa->ifa_addr->sa_family != AF_INET6)
2361 continue;
2362 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2363 continue; /* XXX: is there any case to allow anycast? */
2364 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2365 continue; /* don't use this interface */
2366 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2367 continue;
2368 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2369 if (ip6_use_deprecated)
2370 dep[1] = (struct in6_ifaddr *)ifa;
2371 continue;
2372 }
2373
2374 return (struct in6_ifaddr *)ifa;
2375 }
2376
2377 /* use the last-resort values, that are, deprecated addresses */
2378 if (dep[0])
2379 return dep[0];
2380 if (dep[1])
2381 return dep[1];
2382
2383 return NULL;
2384 }
2385
2386 extern int in6_init2done;
2387
2388 /*
2389 * perform DAD when interface becomes IFF_UP.
2390 */
2391 void
2392 in6_if_up(ifp)
2393 struct ifnet *ifp;
2394 {
2395 struct ifaddr *ifa;
2396 struct in6_ifaddr *ia;
2397 int dad_delay; /* delay ticks before DAD output */
2398
2399 if (!in6_init2done)
2400 return;
2401
2402 /*
2403 * special cases, like 6to4, are handled in in6_ifattach
2404 */
2405 in6_ifattach(ifp, NULL);
2406
2407 dad_delay = 0;
2408 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2409 {
2410 if (ifa->ifa_addr->sa_family != AF_INET6)
2411 continue;
2412 ia = (struct in6_ifaddr *)ifa;
2413 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2414 nd6_dad_start(ifa, &dad_delay);
2415 }
2416 }
2417
2418 int
2419 in6if_do_dad(ifp)
2420 struct ifnet *ifp;
2421 {
2422 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2423 return(0);
2424
2425 switch (ifp->if_type) {
2426 #if IFT_DUMMY
2427 case IFT_DUMMY:
2428 #endif
2429 case IFT_FAITH:
2430 /*
2431 * These interfaces do not have the IFF_LOOPBACK flag,
2432 * but loop packets back. We do not have to do DAD on such
2433 * interfaces. We should even omit it, because loop-backed
2434 * NS would confuse the DAD procedure.
2435 */
2436 return(0);
2437 default:
2438 /*
2439 * Our DAD routine requires the interface up and running.
2440 * However, some interfaces can be up before the RUNNING
2441 * status. Additionaly, users may try to assign addresses
2442 * before the interface becomes up (or running).
2443 * We simply skip DAD in such a case as a work around.
2444 * XXX: we should rather mark "tentative" on such addresses,
2445 * and do DAD after the interface becomes ready.
2446 */
2447 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2448 (IFF_UP|IFF_RUNNING))
2449 return(0);
2450
2451 return(1);
2452 }
2453 }
2454
2455 /*
2456 * Calculate max IPv6 MTU through all the interfaces and store it
2457 * to in6_maxmtu.
2458 */
2459 void
2460 in6_setmaxmtu()
2461 {
2462 unsigned long maxmtu = 0;
2463 struct ifnet *ifp;
2464
2465 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2466 {
2467 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2468 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2469 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2470 }
2471 if (maxmtu) /* update only when maxmtu is positive */
2472 in6_maxmtu = maxmtu;
2473 }
2474
2475 /*
2476 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2477 * v4 mapped addr or v4 compat addr
2478 */
2479 void
2480 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2481 {
2482 bzero(sin, sizeof(*sin));
2483 sin->sin_len = sizeof(struct sockaddr_in);
2484 sin->sin_family = AF_INET;
2485 sin->sin_port = sin6->sin6_port;
2486 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2487 }
2488
2489 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2490 void
2491 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2492 {
2493 bzero(sin6, sizeof(*sin6));
2494 sin6->sin6_len = sizeof(struct sockaddr_in6);
2495 sin6->sin6_family = AF_INET6;
2496 sin6->sin6_port = sin->sin_port;
2497 sin6->sin6_addr.s6_addr32[0] = 0;
2498 sin6->sin6_addr.s6_addr32[1] = 0;
2499 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2500 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2501 }
2502
2503 /* Convert sockaddr_in6 into sockaddr_in. */
2504 void
2505 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2506 {
2507 struct sockaddr_in *sin_p;
2508 struct sockaddr_in6 sin6;
2509
2510 /*
2511 * Save original sockaddr_in6 addr and convert it
2512 * to sockaddr_in.
2513 */
2514 sin6 = *(struct sockaddr_in6 *)nam;
2515 sin_p = (struct sockaddr_in *)nam;
2516 in6_sin6_2_sin(sin_p, &sin6);
2517 }
2518
2519 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2520 void
2521 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2522 {
2523 struct sockaddr_in *sin_p;
2524 struct sockaddr_in6 *sin6_p;
2525
2526 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2527 M_WAITOK);
2528 sin_p = (struct sockaddr_in *)*nam;
2529 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2530 FREE(*nam, M_SONAME);
2531 *nam = (struct sockaddr *)sin6_p;
2532 }
2533
2534 /* Posts in6_event_data message kernel events */
2535 void
2536 in6_post_msg(struct ifnet *ifp, u_long event_code, struct in6_ifaddr *ifa)
2537 {
2538 struct kev_msg ev_msg;
2539 struct kev_in6_data in6_event_data;
2540
2541 ev_msg.vendor_code = KEV_VENDOR_APPLE;
2542 ev_msg.kev_class = KEV_NETWORK_CLASS;
2543 ev_msg.kev_subclass = KEV_INET6_SUBCLASS;
2544 ev_msg.event_code = event_code;
2545
2546 in6_event_data.ia_addr = ifa->ia_addr;
2547 in6_event_data.ia_net = ifa->ia_net;
2548 in6_event_data.ia_dstaddr = ifa->ia_dstaddr;
2549 in6_event_data.ia_prefixmask = ifa->ia_prefixmask;
2550 in6_event_data.ia_plen = ifa->ia_plen;
2551 in6_event_data.ia6_flags = (u_int32_t)ifa->ia6_flags;
2552 in6_event_data.ia_lifetime = ifa->ia6_lifetime;
2553
2554 if (ifp != NULL) {
2555 strncpy(&in6_event_data.link_data.if_name[0], ifp->if_name, IFNAMSIZ);
2556 in6_event_data.link_data.if_family = ifp->if_family;
2557 in6_event_data.link_data.if_unit = (unsigned long) ifp->if_unit;
2558 }
2559
2560 ev_msg.dv[0].data_ptr = &in6_event_data;
2561 ev_msg.dv[0].data_length = sizeof(struct kev_in6_data);
2562 ev_msg.dv[1].data_length = 0;
2563
2564 kev_post_msg(&ev_msg);
2565 }