Add the management part of address selection policy described in RFC
[dragonfly.git] / sys / netinet6 / in6.c
blobe227c06a2140f411a2ecda39fcf26b4dd0bf8476
1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6.c,v 1.30 2008/10/03 07:59:20 hasso Exp $ */
3 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
66 * @(#)in.c 8.2 (Berkeley) 11/15/93
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
72 #include <sys/param.h>
73 #include <sys/errno.h>
74 #include <sys/malloc.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80 #include <sys/time.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
83 #include <sys/thread2.h>
85 #include <net/if.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/if_ether.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet6/mld6_var.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_ifattach.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet6/in6_var.h>
107 #include <net/net_osdep.h>
110 * Definitions of some costant IP6 addresses.
112 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
113 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
114 const struct in6_addr kin6addr_nodelocal_allnodes =
115 IN6ADDR_NODELOCAL_ALLNODES_INIT;
116 const struct in6_addr kin6addr_linklocal_allnodes =
117 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
118 const struct in6_addr kin6addr_linklocal_allrouters =
119 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
121 const struct in6_addr in6mask0 = IN6MASK0;
122 const struct in6_addr in6mask32 = IN6MASK32;
123 const struct in6_addr in6mask64 = IN6MASK64;
124 const struct in6_addr in6mask96 = IN6MASK96;
125 const struct in6_addr in6mask128 = IN6MASK128;
127 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
128 0, 0, IN6ADDR_ANY_INIT, 0};
130 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
131 struct ifnet *, struct thread *);
132 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
133 struct sockaddr_in6 *, int);
134 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
135 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
137 struct in6_multihead in6_multihead; /* XXX BSS initialization */
139 int (*faithprefix_p)(struct in6_addr *);
142 * Subroutine for in6_ifaddloop() and in6_ifremloop().
143 * This routine does actual work.
145 static void
146 in6_ifloop_request(int cmd, struct ifaddr *ifa)
148 struct sockaddr_in6 all1_sa;
149 struct rt_addrinfo rtinfo;
150 int error;
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;
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.
165 bzero(&rtinfo, sizeof(struct rt_addrinfo));
166 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
167 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
168 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
169 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
171 error = rtrequest1_global(cmd, &rtinfo,
172 in6_ifloop_request_callback, ifa);
173 if (error != 0) {
174 log(LOG_ERR, "in6_ifloop_request: "
175 "%s operation failed for %s (errno=%d)\n",
176 cmd == RTM_ADD ? "ADD" : "DELETE",
177 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
178 error);
182 static void
183 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
184 struct rtentry *rt, void *arg)
186 struct ifaddr *ifa = arg;
188 if (error)
189 goto done;
192 * Make sure rt_ifa be equal to IFA, the second argument of the
193 * function.
194 * We need this because when we refer to rt_ifa->ia6_flags in
195 * ip6_input, we assume that the rt_ifa points to the address instead
196 * of the loopback address.
198 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
199 ++rt->rt_refcnt;
200 IFAFREE(rt->rt_ifa);
201 IFAREF(ifa);
202 rt->rt_ifa = ifa;
203 --rt->rt_refcnt;
207 * Report the addition/removal of the address to the routing socket.
208 * XXX: since we called rtinit for a p2p interface with a destination,
209 * we end up reporting twice in such a case. Should we rather
210 * omit the second report?
212 if (rt) {
213 if (mycpuid == 0)
214 rt_newaddrmsg(cmd, ifa, error, rt);
215 if (cmd == RTM_DELETE) {
216 if (rt->rt_refcnt == 0) {
217 ++rt->rt_refcnt;
218 rtfree(rt);
222 done:
223 /* no way to return any new error */
228 * Add ownaddr as loopback rtentry. We previously add the route only if
229 * necessary (ex. on a p2p link). However, since we now manage addresses
230 * separately from prefixes, we should always add the route. We can't
231 * rely on the cloning mechanism from the corresponding interface route
232 * any more.
234 void
235 in6_ifaddloop(struct ifaddr *ifa)
237 struct rtentry *rt;
239 /* If there is no loopback entry, allocate one. */
240 rt = rtpurelookup(ifa->ifa_addr);
241 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
242 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
243 in6_ifloop_request(RTM_ADD, ifa);
244 if (rt != NULL)
245 rt->rt_refcnt--;
249 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
250 * if it exists.
252 void
253 in6_ifremloop(struct ifaddr *ifa)
255 struct in6_ifaddr *ia;
256 struct rtentry *rt;
257 int ia_count = 0;
260 * Some of BSD variants do not remove cloned routes
261 * from an interface direct route, when removing the direct route
262 * (see comments in net/net_osdep.h). Even for variants that do remove
263 * cloned routes, they could fail to remove the cloned routes when
264 * we handle multple addresses that share a common prefix.
265 * So, we should remove the route corresponding to the deleted address
266 * regardless of the result of in6_is_ifloop_auto().
270 * Delete the entry only if exact one ifa exists. More than one ifa
271 * can exist if we assign a same single address to multiple
272 * (probably p2p) interfaces.
273 * XXX: we should avoid such a configuration in IPv6...
275 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
276 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
277 ia_count++;
278 if (ia_count > 1)
279 break;
283 if (ia_count == 1) {
285 * Before deleting, check if a corresponding loopbacked host
286 * route surely exists. With this check, we can avoid to
287 * delete an interface direct route whose destination is same
288 * as the address being removed. This can happen when remofing
289 * a subnet-router anycast address on an interface attahced
290 * to a shared medium.
292 rt = rtpurelookup(ifa->ifa_addr);
293 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
294 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
295 rt->rt_refcnt--;
296 in6_ifloop_request(RTM_DELETE, ifa);
302 in6_ifindex2scopeid(int idx)
304 struct ifnet *ifp;
305 struct sockaddr_in6 *sin6;
306 struct ifaddr_container *ifac;
308 if (idx < 0 || if_index < idx)
309 return -1;
310 ifp = ifindex2ifnet[idx];
312 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
314 struct ifaddr *ifa = ifac->ifa;
316 if (ifa->ifa_addr->sa_family != AF_INET6)
317 continue;
318 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
319 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
320 return sin6->sin6_scope_id & 0xffff;
323 return -1;
327 in6_mask2len(struct in6_addr *mask, u_char *lim0)
329 int x = 0, y;
330 u_char *lim = lim0, *p;
332 if (lim0 == NULL ||
333 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
334 lim = (u_char *)mask + sizeof(*mask);
335 for (p = (u_char *)mask; p < lim; x++, p++) {
336 if (*p != 0xff)
337 break;
339 y = 0;
340 if (p < lim) {
341 for (y = 0; y < 8; y++) {
342 if ((*p & (0x80 >> y)) == 0)
343 break;
348 * when the limit pointer is given, do a stricter check on the
349 * remaining bits.
351 if (p < lim) {
352 if (y != 0 && (*p & (0x00ff >> y)) != 0)
353 return (-1);
354 for (p = p + 1; p < lim; p++)
355 if (*p != 0)
356 return (-1);
359 return x * 8 + y;
362 void
363 in6_len2mask(struct in6_addr *mask, int len)
365 int i;
367 bzero(mask, sizeof(*mask));
368 for (i = 0; i < len / 8; i++)
369 mask->s6_addr8[i] = 0xff;
370 if (len % 8)
371 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
374 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
375 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
378 in6_control(struct socket *so, u_long cmd, caddr_t data,
379 struct ifnet *ifp, struct thread *td)
381 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
382 struct in6_ifaddr *ia = NULL;
383 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
384 int privileged;
385 int error;
387 privileged = 0;
388 if (suser(td) == 0)
389 privileged++;
391 switch (cmd) {
392 case SIOCGETSGCNT_IN6:
393 case SIOCGETMIFCNT_IN6:
394 return (mrt6_ioctl(cmd, data));
397 switch(cmd) {
398 case SIOCAADDRCTL_POLICY:
399 case SIOCDADDRCTL_POLICY:
400 if (!privileged)
401 return (EPERM);
402 return (in6_src_ioctl(cmd, data));
405 if (ifp == NULL)
406 return (EOPNOTSUPP);
408 switch (cmd) {
409 case SIOCSNDFLUSH_IN6:
410 case SIOCSPFXFLUSH_IN6:
411 case SIOCSRTRFLUSH_IN6:
412 case SIOCSDEFIFACE_IN6:
413 case SIOCSIFINFO_FLAGS:
414 if (!privileged)
415 return (EPERM);
416 /* fall through */
417 case OSIOCGIFINFO_IN6:
418 case SIOCGIFINFO_IN6:
419 case SIOCGDRLST_IN6:
420 case SIOCGPRLST_IN6:
421 case SIOCGNBRINFO_IN6:
422 case SIOCGDEFIFACE_IN6:
423 return (nd6_ioctl(cmd, data, ifp));
426 switch (cmd) {
427 case SIOCSIFPREFIX_IN6:
428 case SIOCDIFPREFIX_IN6:
429 case SIOCAIFPREFIX_IN6:
430 case SIOCCIFPREFIX_IN6:
431 case SIOCSGIFPREFIX_IN6:
432 case SIOCGIFPREFIX_IN6:
433 log(LOG_NOTICE,
434 "prefix ioctls are now invalidated. "
435 "please use ifconfig.\n");
436 return (EOPNOTSUPP);
439 switch (cmd) {
440 case SIOCSSCOPE6:
441 if (!privileged)
442 return (EPERM);
443 return (scope6_set(ifp,
444 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
445 break;
446 case SIOCGSCOPE6:
447 return (scope6_get(ifp,
448 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
449 break;
450 case SIOCGSCOPE6DEF:
451 return (scope6_get_default((struct scope6_id *)
452 ifr->ifr_ifru.ifru_scope_id));
453 break;
456 switch (cmd) {
457 case SIOCALIFADDR:
458 case SIOCDLIFADDR:
459 if (!privileged)
460 return (EPERM);
461 /* fall through */
462 case SIOCGLIFADDR:
463 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
467 * Find address for this interface, if it exists.
469 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
470 struct sockaddr_in6 *sa6 =
471 (struct sockaddr_in6 *)&ifra->ifra_addr;
473 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
474 if (sa6->sin6_addr.s6_addr16[1] == 0) {
475 /* link ID is not embedded by the user */
476 sa6->sin6_addr.s6_addr16[1] =
477 htons(ifp->if_index);
478 } else if (sa6->sin6_addr.s6_addr16[1] !=
479 htons(ifp->if_index)) {
480 return (EINVAL); /* link ID contradicts */
482 if (sa6->sin6_scope_id) {
483 if (sa6->sin6_scope_id !=
484 (u_int32_t)ifp->if_index)
485 return (EINVAL);
486 sa6->sin6_scope_id = 0; /* XXX: good way? */
489 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
492 switch (cmd) {
493 case SIOCSIFADDR_IN6:
494 case SIOCSIFDSTADDR_IN6:
495 case SIOCSIFNETMASK_IN6:
497 * Since IPv6 allows a node to assign multiple addresses
498 * on a single interface, SIOCSIFxxx ioctls are not suitable
499 * and should be unused.
501 /* we decided to obsolete this command (20000704) */
502 return (EINVAL);
504 case SIOCDIFADDR_IN6:
506 * for IPv4, we look for existing in_ifaddr here to allow
507 * "ifconfig if0 delete" to remove first IPv4 address on the
508 * interface. For IPv6, as the spec allow multiple interface
509 * address from the day one, we consider "remove the first one"
510 * semantics to be not preferable.
512 if (ia == NULL)
513 return (EADDRNOTAVAIL);
514 /* FALLTHROUGH */
515 case SIOCAIFADDR_IN6:
517 * We always require users to specify a valid IPv6 address for
518 * the corresponding operation.
520 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
521 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
522 return (EAFNOSUPPORT);
523 if (!privileged)
524 return (EPERM);
526 break;
528 case SIOCGIFADDR_IN6:
529 /* This interface is basically deprecated. use SIOCGIFCONF. */
530 /* fall through */
531 case SIOCGIFAFLAG_IN6:
532 case SIOCGIFNETMASK_IN6:
533 case SIOCGIFDSTADDR_IN6:
534 case SIOCGIFALIFETIME_IN6:
535 /* must think again about its semantics */
536 if (ia == NULL)
537 return (EADDRNOTAVAIL);
538 break;
539 case SIOCSIFALIFETIME_IN6:
541 struct in6_addrlifetime *lt;
543 if (!privileged)
544 return (EPERM);
545 if (ia == NULL)
546 return (EADDRNOTAVAIL);
547 /* sanity for overflow - beware unsigned */
548 lt = &ifr->ifr_ifru.ifru_lifetime;
549 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
550 && lt->ia6t_vltime + time_second < time_second) {
551 return EINVAL;
553 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
554 && lt->ia6t_pltime + time_second < time_second) {
555 return EINVAL;
557 break;
561 switch (cmd) {
563 case SIOCGIFADDR_IN6:
564 ifr->ifr_addr = ia->ia_addr;
565 break;
567 case SIOCGIFDSTADDR_IN6:
568 if (!(ifp->if_flags & IFF_POINTOPOINT))
569 return (EINVAL);
571 * XXX: should we check if ifa_dstaddr is NULL and return
572 * an error?
574 ifr->ifr_dstaddr = ia->ia_dstaddr;
575 break;
577 case SIOCGIFNETMASK_IN6:
578 ifr->ifr_addr = ia->ia_prefixmask;
579 break;
581 case SIOCGIFAFLAG_IN6:
582 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
583 break;
585 case SIOCGIFSTAT_IN6:
586 if (ifp == NULL)
587 return EINVAL;
588 bzero(&ifr->ifr_ifru.ifru_stat,
589 sizeof(ifr->ifr_ifru.ifru_stat));
590 ifr->ifr_ifru.ifru_stat =
591 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
592 break;
594 case SIOCGIFSTAT_ICMP6:
595 bzero(&ifr->ifr_ifru.ifru_stat,
596 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
597 ifr->ifr_ifru.ifru_icmp6stat =
598 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
599 break;
601 case SIOCGIFALIFETIME_IN6:
602 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
603 break;
605 case SIOCSIFALIFETIME_IN6:
606 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
607 /* for sanity */
608 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
609 ia->ia6_lifetime.ia6t_expire =
610 time_second + ia->ia6_lifetime.ia6t_vltime;
611 } else
612 ia->ia6_lifetime.ia6t_expire = 0;
613 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
614 ia->ia6_lifetime.ia6t_preferred =
615 time_second + ia->ia6_lifetime.ia6t_pltime;
616 } else
617 ia->ia6_lifetime.ia6t_preferred = 0;
618 break;
620 case SIOCAIFADDR_IN6:
622 int i, error = 0, iaIsNew;
623 struct nd_prefix pr0, *pr;
625 if (ia != NULL)
626 iaIsNew = 0;
627 else
628 iaIsNew = 1;
631 * first, make or update the interface address structure,
632 * and link it to the list.
634 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
635 return (error);
638 * then, make the prefix on-link on the interface.
639 * XXX: we'd rather create the prefix before the address, but
640 * we need at least one address to install the corresponding
641 * interface route, so we configure the address first.
645 * convert mask to prefix length (prefixmask has already
646 * been validated in in6_update_ifa().
648 bzero(&pr0, sizeof(pr0));
649 pr0.ndpr_ifp = ifp;
650 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
651 NULL);
652 if (pr0.ndpr_plen == 128)
653 break; /* we don't need to install a host route. */
654 pr0.ndpr_prefix = ifra->ifra_addr;
655 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
656 /* apply the mask for safety. */
657 for (i = 0; i < 4; i++) {
658 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
659 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
662 * XXX: since we don't have an API to set prefix (not address)
663 * lifetimes, we just use the same lifetimes as addresses.
664 * The (temporarily) installed lifetimes can be overridden by
665 * later advertised RAs (when accept_rtadv is non 0), which is
666 * an intended behavior.
668 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
669 pr0.ndpr_raf_auto =
670 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
671 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
672 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
674 /* add the prefix if there's one. */
675 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
677 * nd6_prelist_add will install the corresponding
678 * interface route.
680 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
681 return (error);
682 if (pr == NULL) {
683 log(LOG_ERR, "nd6_prelist_add succeeded but "
684 "no prefix\n");
685 return (EINVAL); /* XXX panic here? */
688 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
689 == NULL) {
690 /* XXX: this should not happen! */
691 log(LOG_ERR, "in6_control: addition succeeded, but"
692 " no ifaddr\n");
693 } else {
694 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
695 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
696 ia->ia6_ndpr = pr;
697 pr->ndpr_refcnt++;
700 * If this is the first autoconf address from
701 * the prefix, create a temporary address
702 * as well (when specified).
704 if (ip6_use_tempaddr &&
705 pr->ndpr_refcnt == 1) {
706 int e;
707 if ((e = in6_tmpifadd(ia, 1)) != 0) {
708 log(LOG_NOTICE, "in6_control: "
709 "failed to create a "
710 "temporary address, "
711 "errno=%d\n",
718 * this might affect the status of autoconfigured
719 * addresses, that is, this address might make
720 * other addresses detached.
722 pfxlist_onlink_check();
724 if (error == 0 && ia) {
725 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
726 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
727 &ia->ia_ifa);
729 break;
732 case SIOCDIFADDR_IN6:
734 int i = 0;
735 struct nd_prefix pr0, *pr;
738 * If the address being deleted is the only one that owns
739 * the corresponding prefix, expire the prefix as well.
740 * XXX: theoretically, we don't have to warry about such
741 * relationship, since we separate the address management
742 * and the prefix management. We do this, however, to provide
743 * as much backward compatibility as possible in terms of
744 * the ioctl operation.
746 bzero(&pr0, sizeof(pr0));
747 pr0.ndpr_ifp = ifp;
748 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
749 NULL);
750 if (pr0.ndpr_plen == 128)
751 goto purgeaddr;
752 pr0.ndpr_prefix = ia->ia_addr;
753 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
754 for (i = 0; i < 4; i++) {
755 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
756 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
759 * The logic of the following condition is a bit complicated.
760 * We expire the prefix when
761 * 1. the address obeys autoconfiguration and it is the
762 * only owner of the associated prefix, or
763 * 2. the address does not obey autoconf and there is no
764 * other owner of the prefix.
766 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
767 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
768 pr->ndpr_refcnt == 1) ||
769 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
770 pr->ndpr_refcnt == 0))) {
771 pr->ndpr_expire = 1; /* XXX: just for expiration */
774 purgeaddr:
775 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
776 &ia->ia_ifa);
777 in6_purgeaddr(&ia->ia_ifa);
778 break;
781 default:
782 if (ifp == NULL || ifp->if_ioctl == 0)
783 return (EOPNOTSUPP);
784 lwkt_serialize_enter(ifp->if_serializer);
785 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
786 lwkt_serialize_exit(ifp->if_serializer);
787 return (error);
790 return (0);
794 * Update parameters of an IPv6 interface address.
795 * If necessary, a new entry is created and linked into address chains.
796 * This function is separated from in6_control().
797 * XXX: should this be performed under splnet()?
800 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
801 struct in6_ifaddr *ia)
803 int error = 0, hostIsNew = 0, plen = -1;
804 struct in6_ifaddr *oia;
805 struct sockaddr_in6 dst6;
806 struct in6_addrlifetime *lt;
808 /* Validate parameters */
809 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
810 return (EINVAL);
813 * The destination address for a p2p link must have a family
814 * of AF_UNSPEC or AF_INET6.
816 if ((ifp->if_flags & IFF_POINTOPOINT) &&
817 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
818 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
819 return (EAFNOSUPPORT);
821 * validate ifra_prefixmask. don't check sin6_family, netmask
822 * does not carry fields other than sin6_len.
824 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
825 return (EINVAL);
827 * Because the IPv6 address architecture is classless, we require
828 * users to specify a (non 0) prefix length (mask) for a new address.
829 * We also require the prefix (when specified) mask is valid, and thus
830 * reject a non-consecutive mask.
832 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
833 return (EINVAL);
834 if (ifra->ifra_prefixmask.sin6_len != 0) {
835 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
836 (u_char *)&ifra->ifra_prefixmask +
837 ifra->ifra_prefixmask.sin6_len);
838 if (plen <= 0)
839 return (EINVAL);
841 else {
843 * In this case, ia must not be NULL. We just use its prefix
844 * length.
846 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
849 * If the destination address on a p2p interface is specified,
850 * and the address is a scoped one, validate/set the scope
851 * zone identifier.
853 dst6 = ifra->ifra_dstaddr;
854 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
855 (dst6.sin6_family == AF_INET6)) {
856 int scopeid;
858 if ((error = in6_recoverscope(&dst6,
859 &ifra->ifra_dstaddr.sin6_addr,
860 ifp)) != 0)
861 return (error);
862 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
863 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
864 dst6.sin6_scope_id = scopeid;
865 else if (dst6.sin6_scope_id != scopeid)
866 return (EINVAL); /* scope ID mismatch. */
867 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
868 != 0)
869 return (error);
870 dst6.sin6_scope_id = 0; /* XXX */
873 * The destination address can be specified only for a p2p or a
874 * loopback interface. If specified, the corresponding prefix length
875 * must be 128.
877 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
878 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
879 /* XXX: noisy message */
880 log(LOG_INFO, "in6_update_ifa: a destination can be "
881 "specified for a p2p or a loopback IF only\n");
882 return (EINVAL);
884 if (plen != 128) {
886 * The following message seems noisy, but we dare to
887 * add it for diagnosis.
889 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
890 "when dstaddr is specified\n");
891 return (EINVAL);
894 /* lifetime consistency check */
895 lt = &ifra->ifra_lifetime;
896 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
897 && lt->ia6t_vltime + time_second < time_second) {
898 return EINVAL;
900 if (lt->ia6t_vltime == 0) {
902 * the following log might be noisy, but this is a typical
903 * configuration mistake or a tool's bug.
905 log(LOG_INFO,
906 "in6_update_ifa: valid lifetime is 0 for %s\n",
907 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
909 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
910 && lt->ia6t_pltime + time_second < time_second) {
911 return EINVAL;
915 * If this is a new address, allocate a new ifaddr and link it
916 * into chains.
918 if (ia == NULL) {
919 hostIsNew = 1;
921 * When in6_update_ifa() is called in a process of a received
922 * RA, it is called under splnet(). So, we should call malloc
923 * with M_NOWAIT.
925 ia = ifa_create(sizeof(*ia), M_NOWAIT);
926 if (ia == NULL)
927 return (ENOBUFS);
928 /* Initialize the address and masks */
929 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
930 ia->ia_addr.sin6_family = AF_INET6;
931 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
932 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
934 * XXX: some functions expect that ifa_dstaddr is not
935 * NULL for p2p interfaces.
937 ia->ia_ifa.ifa_dstaddr
938 = (struct sockaddr *)&ia->ia_dstaddr;
939 } else {
940 ia->ia_ifa.ifa_dstaddr = NULL;
942 ia->ia_ifa.ifa_netmask
943 = (struct sockaddr *)&ia->ia_prefixmask;
945 ia->ia_ifp = ifp;
946 if ((oia = in6_ifaddr) != NULL) {
947 for ( ; oia->ia_next; oia = oia->ia_next)
948 continue;
949 oia->ia_next = ia;
950 } else
951 in6_ifaddr = ia;
953 ifa_iflink(&ia->ia_ifa, ifp, 1);
956 /* set prefix mask */
957 if (ifra->ifra_prefixmask.sin6_len) {
959 * We prohibit changing the prefix length of an existing
960 * address, because
961 * + such an operation should be rare in IPv6, and
962 * + the operation would confuse prefix management.
964 if (ia->ia_prefixmask.sin6_len &&
965 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
966 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
967 " existing (%s) address should not be changed\n",
968 ip6_sprintf(&ia->ia_addr.sin6_addr));
969 error = EINVAL;
970 goto unlink;
972 ia->ia_prefixmask = ifra->ifra_prefixmask;
976 * If a new destination address is specified, scrub the old one and
977 * install the new destination. Note that the interface must be
978 * p2p or loopback (see the check above.)
980 if (dst6.sin6_family == AF_INET6 &&
981 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
982 &ia->ia_dstaddr.sin6_addr)) {
983 int e;
985 if ((ia->ia_flags & IFA_ROUTE) &&
986 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
987 != 0) {
988 log(LOG_ERR, "in6_update_ifa: failed to remove "
989 "a route to the old destination: %s\n",
990 ip6_sprintf(&ia->ia_addr.sin6_addr));
991 /* proceed anyway... */
993 else
994 ia->ia_flags &= ~IFA_ROUTE;
995 ia->ia_dstaddr = dst6;
998 /* reset the interface and routing table appropriately. */
999 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1000 goto unlink;
1003 * Beyond this point, we should call in6_purgeaddr upon an error,
1004 * not just go to unlink.
1007 #if 0 /* disable this mechanism for now */
1008 /* update prefix list */
1009 if (hostIsNew &&
1010 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
1011 int iilen;
1013 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
1014 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
1015 in6_purgeaddr((struct ifaddr *)ia);
1016 return (error);
1019 #endif
1021 if (ifp->if_flags & IFF_MULTICAST) {
1022 struct sockaddr_in6 mltaddr, mltmask;
1023 struct in6_multi *in6m;
1025 if (hostIsNew) {
1027 * join solicited multicast addr for new host id
1029 struct in6_addr llsol;
1030 bzero(&llsol, sizeof(struct in6_addr));
1031 llsol.s6_addr16[0] = htons(0xff02);
1032 llsol.s6_addr16[1] = htons(ifp->if_index);
1033 llsol.s6_addr32[1] = 0;
1034 llsol.s6_addr32[2] = htonl(1);
1035 llsol.s6_addr32[3] =
1036 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1037 llsol.s6_addr8[12] = 0xff;
1038 in6_addmulti(&llsol, ifp, &error);
1039 if (error != 0) {
1040 log(LOG_WARNING,
1041 "in6_update_ifa: addmulti failed for "
1042 "%s on %s (errno=%d)\n",
1043 ip6_sprintf(&llsol), if_name(ifp),
1044 error);
1045 in6_purgeaddr((struct ifaddr *)ia);
1046 return (error);
1050 bzero(&mltmask, sizeof(mltmask));
1051 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1052 mltmask.sin6_family = AF_INET6;
1053 mltmask.sin6_addr = in6mask32;
1056 * join link-local all-nodes address
1058 bzero(&mltaddr, sizeof(mltaddr));
1059 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1060 mltaddr.sin6_family = AF_INET6;
1061 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1062 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1064 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1065 if (in6m == NULL) {
1066 rtrequest_global(RTM_ADD,
1067 (struct sockaddr *)&mltaddr,
1068 (struct sockaddr *)&ia->ia_addr,
1069 (struct sockaddr *)&mltmask,
1070 RTF_UP|RTF_CLONING); /* xxx */
1071 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1072 if (error != 0) {
1073 log(LOG_WARNING,
1074 "in6_update_ifa: addmulti failed for "
1075 "%s on %s (errno=%d)\n",
1076 ip6_sprintf(&mltaddr.sin6_addr),
1077 if_name(ifp), error);
1082 * join node information group address
1084 #define hostnamelen strlen(hostname)
1085 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1086 == 0) {
1087 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1088 if (in6m == NULL && ia != NULL) {
1089 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1090 if (error != 0) {
1091 log(LOG_WARNING, "in6_update_ifa: "
1092 "addmulti failed for "
1093 "%s on %s (errno=%d)\n",
1094 ip6_sprintf(&mltaddr.sin6_addr),
1095 if_name(ifp), error);
1099 #undef hostnamelen
1102 * join node-local all-nodes address, on loopback.
1103 * XXX: since "node-local" is obsoleted by interface-local,
1104 * we have to join the group on every interface with
1105 * some interface-boundary restriction.
1107 if (ifp->if_flags & IFF_LOOPBACK) {
1108 struct in6_ifaddr *ia_loop;
1110 struct in6_addr loop6 = kin6addr_loopback;
1111 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1113 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1115 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1116 if (in6m == NULL && ia_loop != NULL) {
1117 rtrequest_global(RTM_ADD,
1118 (struct sockaddr *)&mltaddr,
1119 (struct sockaddr *)&ia_loop->ia_addr,
1120 (struct sockaddr *)&mltmask,
1121 RTF_UP);
1122 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1123 if (error != 0) {
1124 log(LOG_WARNING, "in6_update_ifa: "
1125 "addmulti failed for %s on %s "
1126 "(errno=%d)\n",
1127 ip6_sprintf(&mltaddr.sin6_addr),
1128 if_name(ifp), error);
1134 ia->ia6_flags = ifra->ifra_flags;
1135 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1136 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1138 ia->ia6_lifetime = ifra->ifra_lifetime;
1139 /* for sanity */
1140 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1141 ia->ia6_lifetime.ia6t_expire =
1142 time_second + ia->ia6_lifetime.ia6t_vltime;
1143 } else
1144 ia->ia6_lifetime.ia6t_expire = 0;
1145 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1146 ia->ia6_lifetime.ia6t_preferred =
1147 time_second + ia->ia6_lifetime.ia6t_pltime;
1148 } else
1149 ia->ia6_lifetime.ia6t_preferred = 0;
1152 * Perform DAD, if needed.
1153 * XXX It may be of use, if we can administratively
1154 * disable DAD.
1156 if (in6if_do_dad(ifp) && !(ifra->ifra_flags & IN6_IFF_NODAD)) {
1157 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1158 nd6_dad_start((struct ifaddr *)ia, NULL);
1161 return (error);
1163 unlink:
1165 * XXX: if a change of an existing address failed, keep the entry
1166 * anyway.
1168 if (hostIsNew)
1169 in6_unlink_ifa(ia, ifp);
1170 return (error);
1173 void
1174 in6_purgeaddr(struct ifaddr *ifa)
1176 struct ifnet *ifp = ifa->ifa_ifp;
1177 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1179 /* stop DAD processing */
1180 nd6_dad_stop(ifa);
1183 * delete route to the destination of the address being purged.
1184 * The interface must be p2p or loopback in this case.
1186 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1187 int e;
1189 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1190 != 0) {
1191 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1192 "a route to the p2p destination: %s on %s, "
1193 "errno=%d\n",
1194 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1196 /* proceed anyway... */
1198 else
1199 ia->ia_flags &= ~IFA_ROUTE;
1202 /* Remove ownaddr's loopback rtentry, if it exists. */
1203 in6_ifremloop(&(ia->ia_ifa));
1205 if (ifp->if_flags & IFF_MULTICAST) {
1207 * delete solicited multicast addr for deleting host id
1209 struct in6_multi *in6m;
1210 struct in6_addr llsol;
1211 bzero(&llsol, sizeof(struct in6_addr));
1212 llsol.s6_addr16[0] = htons(0xff02);
1213 llsol.s6_addr16[1] = htons(ifp->if_index);
1214 llsol.s6_addr32[1] = 0;
1215 llsol.s6_addr32[2] = htonl(1);
1216 llsol.s6_addr32[3] =
1217 ia->ia_addr.sin6_addr.s6_addr32[3];
1218 llsol.s6_addr8[12] = 0xff;
1220 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1221 if (in6m)
1222 in6_delmulti(in6m);
1225 in6_unlink_ifa(ia, ifp);
1228 static void
1229 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1231 int plen, iilen;
1232 struct in6_ifaddr *oia;
1234 crit_enter();
1236 ifa_ifunlink(&ia->ia_ifa, ifp);
1238 oia = ia;
1239 if (oia == (ia = in6_ifaddr))
1240 in6_ifaddr = ia->ia_next;
1241 else {
1242 while (ia->ia_next && (ia->ia_next != oia))
1243 ia = ia->ia_next;
1244 if (ia->ia_next)
1245 ia->ia_next = oia->ia_next;
1246 else {
1247 /* search failed */
1248 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1252 if (oia->ia6_ifpr) { /* check for safety */
1253 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1254 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1255 in6_prefix_remove_ifid(iilen, oia);
1259 * When an autoconfigured address is being removed, release the
1260 * reference to the base prefix. Also, since the release might
1261 * affect the status of other (detached) addresses, call
1262 * pfxlist_onlink_check().
1264 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1265 if (oia->ia6_ndpr == NULL) {
1266 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1267 "%p has no prefix\n", oia);
1268 } else {
1269 oia->ia6_ndpr->ndpr_refcnt--;
1270 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1271 oia->ia6_ndpr = NULL;
1274 pfxlist_onlink_check();
1278 * release another refcnt for the link from in6_ifaddr.
1279 * Note that we should decrement the refcnt at least once for all *BSD.
1281 ifa_destroy(&oia->ia_ifa);
1283 crit_exit();
1286 void
1287 in6_purgeif(struct ifnet *ifp)
1289 struct ifaddr_container *ifac, *next;
1291 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1292 ifa_link, next) {
1293 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1294 continue;
1295 in6_purgeaddr(ifac->ifa);
1298 in6_ifdetach(ifp);
1302 * SIOC[GAD]LIFADDR.
1303 * SIOCGLIFADDR: get first address. (?)
1304 * SIOCGLIFADDR with IFLR_PREFIX:
1305 * get first address that matches the specified prefix.
1306 * SIOCALIFADDR: add the specified address.
1307 * SIOCALIFADDR with IFLR_PREFIX:
1308 * add the specified prefix, filling hostid part from
1309 * the first link-local address. prefixlen must be <= 64.
1310 * SIOCDLIFADDR: delete the specified address.
1311 * SIOCDLIFADDR with IFLR_PREFIX:
1312 * delete the first address that matches the specified prefix.
1313 * return values:
1314 * EINVAL on invalid parameters
1315 * EADDRNOTAVAIL on prefix match failed/specified address not found
1316 * other values may be returned from in6_ioctl()
1318 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1319 * this is to accomodate address naming scheme other than RFC2374,
1320 * in the future.
1321 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1322 * address encoding scheme. (see figure on page 8)
1324 static int
1325 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1326 struct ifnet *ifp, struct thread *td)
1328 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1329 struct sockaddr *sa;
1331 /* sanity checks */
1332 if (!data || !ifp) {
1333 panic("invalid argument to in6_lifaddr_ioctl");
1334 /*NOTRECHED*/
1337 switch (cmd) {
1338 case SIOCGLIFADDR:
1339 /* address must be specified on GET with IFLR_PREFIX */
1340 if (!(iflr->flags & IFLR_PREFIX))
1341 break;
1342 /* FALLTHROUGH */
1343 case SIOCALIFADDR:
1344 case SIOCDLIFADDR:
1345 /* address must be specified on ADD and DELETE */
1346 sa = (struct sockaddr *)&iflr->addr;
1347 if (sa->sa_family != AF_INET6)
1348 return EINVAL;
1349 if (sa->sa_len != sizeof(struct sockaddr_in6))
1350 return EINVAL;
1351 /* XXX need improvement */
1352 sa = (struct sockaddr *)&iflr->dstaddr;
1353 if (sa->sa_family && sa->sa_family != AF_INET6)
1354 return EINVAL;
1355 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1356 return EINVAL;
1357 break;
1358 default: /* shouldn't happen */
1359 #if 0
1360 panic("invalid cmd to in6_lifaddr_ioctl");
1361 /* NOTREACHED */
1362 #else
1363 return EOPNOTSUPP;
1364 #endif
1366 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1367 return EINVAL;
1369 switch (cmd) {
1370 case SIOCALIFADDR:
1372 struct in6_aliasreq ifra;
1373 struct in6_addr *hostid = NULL;
1374 int prefixlen;
1376 if (iflr->flags & IFLR_PREFIX) {
1377 struct ifaddr *ifa;
1378 struct sockaddr_in6 *sin6;
1381 * hostid is to fill in the hostid part of the
1382 * address. hostid points to the first link-local
1383 * address attached to the interface.
1385 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1386 if (!ifa)
1387 return EADDRNOTAVAIL;
1388 hostid = IFA_IN6(ifa);
1390 /* prefixlen must be <= 64. */
1391 if (64 < iflr->prefixlen)
1392 return EINVAL;
1393 prefixlen = iflr->prefixlen;
1395 /* hostid part must be zero. */
1396 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1397 if (sin6->sin6_addr.s6_addr32[2] != 0
1398 || sin6->sin6_addr.s6_addr32[3] != 0) {
1399 return EINVAL;
1401 } else
1402 prefixlen = iflr->prefixlen;
1404 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1405 bzero(&ifra, sizeof(ifra));
1406 bcopy(iflr->iflr_name, ifra.ifra_name,
1407 sizeof(ifra.ifra_name));
1409 bcopy(&iflr->addr, &ifra.ifra_addr,
1410 ((struct sockaddr *)&iflr->addr)->sa_len);
1411 if (hostid) {
1412 /* fill in hostid part */
1413 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1414 hostid->s6_addr32[2];
1415 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1416 hostid->s6_addr32[3];
1419 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1420 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1421 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1422 if (hostid) {
1423 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1424 hostid->s6_addr32[2];
1425 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1426 hostid->s6_addr32[3];
1430 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1431 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1433 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1434 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1436 case SIOCGLIFADDR:
1437 case SIOCDLIFADDR:
1439 struct ifaddr_container *ifac;
1440 struct in6_ifaddr *ia;
1441 struct in6_addr mask, candidate, match;
1442 struct sockaddr_in6 *sin6;
1443 int cmp;
1445 bzero(&mask, sizeof(mask));
1446 if (iflr->flags & IFLR_PREFIX) {
1447 /* lookup a prefix rather than address. */
1448 in6_len2mask(&mask, iflr->prefixlen);
1450 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1451 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1452 match.s6_addr32[0] &= mask.s6_addr32[0];
1453 match.s6_addr32[1] &= mask.s6_addr32[1];
1454 match.s6_addr32[2] &= mask.s6_addr32[2];
1455 match.s6_addr32[3] &= mask.s6_addr32[3];
1457 /* if you set extra bits, that's wrong */
1458 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1459 return EINVAL;
1461 cmp = 1;
1462 } else {
1463 if (cmd == SIOCGLIFADDR) {
1464 /* on getting an address, take the 1st match */
1465 cmp = 0; /* XXX */
1466 } else {
1467 /* on deleting an address, do exact match */
1468 in6_len2mask(&mask, 128);
1469 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1470 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1472 cmp = 1;
1476 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1477 struct ifaddr *ifa = ifac->ifa;
1479 if (ifa->ifa_addr->sa_family != AF_INET6)
1480 continue;
1481 if (!cmp)
1482 break;
1484 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1486 * XXX: this is adhoc, but is necessary to allow
1487 * a user to specify fe80::/64 (not /10) for a
1488 * link-local address.
1490 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1491 candidate.s6_addr16[1] = 0;
1492 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1493 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1494 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1495 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1496 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1497 break;
1499 if (ifac == NULL)
1500 return EADDRNOTAVAIL;
1501 ia = ifa2ia6(ifac->ifa);
1503 if (cmd == SIOCGLIFADDR) {
1504 struct sockaddr_in6 *s6;
1506 /* fill in the if_laddrreq structure */
1507 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1508 s6 = (struct sockaddr_in6 *)&iflr->addr;
1509 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1510 s6->sin6_addr.s6_addr16[1] = 0;
1511 s6->sin6_scope_id =
1512 in6_addr2scopeid(ifp, &s6->sin6_addr);
1514 if (ifp->if_flags & IFF_POINTOPOINT) {
1515 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1516 ia->ia_dstaddr.sin6_len);
1517 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1518 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1519 s6->sin6_addr.s6_addr16[1] = 0;
1520 s6->sin6_scope_id =
1521 in6_addr2scopeid(ifp,
1522 &s6->sin6_addr);
1524 } else
1525 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1527 iflr->prefixlen =
1528 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1529 NULL);
1531 iflr->flags = ia->ia6_flags; /* XXX */
1533 return 0;
1534 } else {
1535 struct in6_aliasreq ifra;
1537 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1538 bzero(&ifra, sizeof(ifra));
1539 bcopy(iflr->iflr_name, ifra.ifra_name,
1540 sizeof(ifra.ifra_name));
1542 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1543 ia->ia_addr.sin6_len);
1544 if (ifp->if_flags & IFF_POINTOPOINT)
1545 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1546 ia->ia_dstaddr.sin6_len);
1547 else
1548 bzero(&ifra.ifra_dstaddr,
1549 sizeof(ifra.ifra_dstaddr));
1550 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1551 ia->ia_prefixmask.sin6_len);
1553 ifra.ifra_flags = ia->ia6_flags;
1554 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1555 ifp, td);
1560 return EOPNOTSUPP; /* just for safety */
1564 * Initialize an interface's intetnet6 address
1565 * and routing table entry.
1567 static int
1568 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1569 int newhost)
1571 int error = 0, plen, ifacount = 0;
1572 struct ifaddr_container *ifac;
1575 * Give the interface a chance to initialize
1576 * if this is its first address,
1577 * and to validate the address if necessary.
1579 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1580 if (ifac->ifa->ifa_addr == NULL)
1581 continue; /* just for safety */
1582 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1583 continue;
1584 ifacount++;
1587 lwkt_serialize_enter(ifp->if_serializer);
1589 ia->ia_addr = *sin6;
1591 if (ifacount <= 1 && ifp->if_ioctl &&
1592 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia,
1593 (struct ucred *)NULL))) {
1594 lwkt_serialize_exit(ifp->if_serializer);
1595 return (error);
1597 lwkt_serialize_exit(ifp->if_serializer);
1599 ia->ia_ifa.ifa_metric = ifp->if_metric;
1601 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1604 * Special case:
1605 * If the destination address is specified for a point-to-point
1606 * interface, install a route to the destination as an interface
1607 * direct route.
1609 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1610 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1611 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1612 RTF_UP | RTF_HOST)) != 0)
1613 return (error);
1614 ia->ia_flags |= IFA_ROUTE;
1616 if (plen < 128) {
1618 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1620 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1623 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1624 if (newhost) {
1625 /* set the rtrequest function to create llinfo */
1626 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1627 in6_ifaddloop(&(ia->ia_ifa));
1630 return (error);
1633 struct in6_multi_mship *
1634 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1636 struct in6_multi_mship *imm;
1638 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1639 if (!imm) {
1640 *errorp = ENOBUFS;
1641 return NULL;
1643 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1644 if (!imm->i6mm_maddr) {
1645 /* *errorp is alrady set */
1646 kfree(imm, M_IPMADDR);
1647 return NULL;
1649 return imm;
1653 in6_leavegroup(struct in6_multi_mship *imm)
1656 if (imm->i6mm_maddr)
1657 in6_delmulti(imm->i6mm_maddr);
1658 kfree(imm, M_IPMADDR);
1659 return 0;
1663 * Add an address to the list of IP6 multicast addresses for a
1664 * given interface.
1666 struct in6_multi *
1667 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1669 struct in6_multi *in6m;
1670 struct sockaddr_in6 sin6;
1671 struct ifmultiaddr *ifma;
1673 *errorp = 0;
1675 crit_enter();
1678 * Call generic routine to add membership or increment
1679 * refcount. It wants addresses in the form of a sockaddr,
1680 * so we build one here (being careful to zero the unused bytes).
1682 bzero(&sin6, sizeof sin6);
1683 sin6.sin6_family = AF_INET6;
1684 sin6.sin6_len = sizeof sin6;
1685 sin6.sin6_addr = *maddr6;
1686 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1687 if (*errorp) {
1688 crit_exit();
1689 return 0;
1693 * If ifma->ifma_protospec is null, then if_addmulti() created
1694 * a new record. Otherwise, we are done.
1696 if (ifma->ifma_protospec != 0) {
1697 crit_exit();
1698 return ifma->ifma_protospec;
1701 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1702 at interrupt time? If so, need to fix if_addmulti. XXX */
1703 in6m = (struct in6_multi *)kmalloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1704 if (in6m == NULL) {
1705 crit_exit();
1706 return (NULL);
1709 bzero(in6m, sizeof *in6m);
1710 in6m->in6m_addr = *maddr6;
1711 in6m->in6m_ifp = ifp;
1712 in6m->in6m_ifma = ifma;
1713 ifma->ifma_protospec = in6m;
1714 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1717 * Let MLD6 know that we have joined a new IP6 multicast
1718 * group.
1720 mld6_start_listening(in6m);
1721 crit_exit();
1722 return (in6m);
1726 * Delete a multicast address record.
1728 void
1729 in6_delmulti(struct in6_multi *in6m)
1731 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1733 crit_enter();
1735 if (ifma->ifma_refcount == 1) {
1737 * No remaining claims to this record; let MLD6 know
1738 * that we are leaving the multicast group.
1740 mld6_stop_listening(in6m);
1741 ifma->ifma_protospec = 0;
1742 LIST_REMOVE(in6m, in6m_entry);
1743 kfree(in6m, M_IPMADDR);
1745 /* XXX - should be separate API for when we have an ifma? */
1746 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1747 crit_exit();
1751 * Find an IPv6 interface link-local address specific to an interface.
1753 struct in6_ifaddr *
1754 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1756 struct ifaddr_container *ifac;
1758 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1759 struct ifaddr *ifa = ifac->ifa;
1761 if (ifa->ifa_addr == NULL)
1762 continue; /* just for safety */
1763 if (ifa->ifa_addr->sa_family != AF_INET6)
1764 continue;
1765 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1766 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1767 ignoreflags) != 0)
1768 continue;
1769 break;
1772 if (ifac != NULL)
1773 return ((struct in6_ifaddr *)(ifac->ifa));
1774 else
1775 return (NULL);
1780 * find the internet address corresponding to a given interface and address.
1782 struct in6_ifaddr *
1783 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1785 struct ifaddr_container *ifac;
1787 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1788 struct ifaddr *ifa = ifac->ifa;
1790 if (ifa->ifa_addr == NULL)
1791 continue; /* just for safety */
1792 if (ifa->ifa_addr->sa_family != AF_INET6)
1793 continue;
1794 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1795 break;
1797 if (ifac != NULL)
1798 return ((struct in6_ifaddr *)(ifac->ifa));
1799 else
1800 return (NULL);
1804 * find the internet address on a given interface corresponding to a neighbor's
1805 * address.
1807 struct in6_ifaddr *
1808 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1810 struct ifaddr *ifa;
1811 struct in6_ifaddr *ia;
1812 struct ifaddr_container *ifac;
1814 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1815 ifa = ifac->ifa;
1817 if (ifa->ifa_addr == NULL)
1818 continue; /* just for safety */
1819 if (ifa->ifa_addr->sa_family != AF_INET6)
1820 continue;
1821 ia = (struct in6_ifaddr *)ifa;
1822 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1823 &ia->ia_addr.sin6_addr,
1824 &ia->ia_prefixmask.sin6_addr))
1825 return ia;
1828 return NULL;
1832 * Convert IP6 address to printable (loggable) representation.
1834 static char digits[] = "0123456789abcdef";
1835 static int ip6round = 0;
1836 char *
1837 ip6_sprintf(const struct in6_addr *addr)
1839 static char ip6buf[8][48];
1840 int i;
1841 char *cp;
1842 const u_short *a = (const u_short *)addr;
1843 const u_char *d;
1844 int dcolon = 0;
1846 ip6round = (ip6round + 1) & 7;
1847 cp = ip6buf[ip6round];
1849 for (i = 0; i < 8; i++) {
1850 if (dcolon == 1) {
1851 if (*a == 0) {
1852 if (i == 7)
1853 *cp++ = ':';
1854 a++;
1855 continue;
1856 } else
1857 dcolon = 2;
1859 if (*a == 0) {
1860 if (dcolon == 0 && *(a + 1) == 0) {
1861 if (i == 0)
1862 *cp++ = ':';
1863 *cp++ = ':';
1864 dcolon = 1;
1865 } else {
1866 *cp++ = '0';
1867 *cp++ = ':';
1869 a++;
1870 continue;
1872 d = (const u_char *)a;
1873 *cp++ = digits[*d >> 4];
1874 *cp++ = digits[*d++ & 0xf];
1875 *cp++ = digits[*d >> 4];
1876 *cp++ = digits[*d & 0xf];
1877 *cp++ = ':';
1878 a++;
1880 *--cp = 0;
1881 return (ip6buf[ip6round]);
1885 in6_localaddr(struct in6_addr *in6)
1887 struct in6_ifaddr *ia;
1889 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1890 return 1;
1892 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1893 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1894 &ia->ia_prefixmask.sin6_addr))
1895 return 1;
1897 return (0);
1901 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1903 struct in6_ifaddr *ia;
1905 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1906 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1907 &sa6->sin6_addr) &&
1908 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1909 return (1); /* true */
1911 /* XXX: do we still have to go thru the rest of the list? */
1914 return (0); /* false */
1918 * return length of part which dst and src are equal
1919 * hard coding...
1922 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1924 int match = 0;
1925 u_char *s = (u_char *)src, *d = (u_char *)dst;
1926 u_char *lim = s + 16, r;
1928 while (s < lim)
1929 if ((r = (*d++ ^ *s++)) != 0) {
1930 while (r < 128) {
1931 match++;
1932 r <<= 1;
1934 break;
1935 } else
1936 match += 8;
1937 return match;
1940 /* XXX: to be scope conscious */
1942 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1944 int bytelen, bitlen;
1946 /* sanity check */
1947 if (0 > len || len > 128) {
1948 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1949 len);
1950 return (0);
1953 bytelen = len / 8;
1954 bitlen = len % 8;
1956 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1957 return (0);
1958 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1959 p2->s6_addr[bytelen] >> (8 - bitlen))
1960 return (0);
1962 return (1);
1965 void
1966 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1968 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1969 int bytelen, bitlen, i;
1971 /* sanity check */
1972 if (0 > len || len > 128) {
1973 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1974 len);
1975 return;
1978 bzero(maskp, sizeof(*maskp));
1979 bytelen = len / 8;
1980 bitlen = len % 8;
1981 for (i = 0; i < bytelen; i++)
1982 maskp->s6_addr[i] = 0xff;
1983 if (bitlen)
1984 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1988 * return the best address out of the same scope
1990 struct in6_ifaddr *
1991 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1993 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1994 int blen = -1;
1995 struct ifnet *ifp;
1996 struct in6_ifaddr *ifa_best = NULL;
1998 if (oifp == NULL) {
1999 #if 0
2000 kprintf("in6_ifawithscope: output interface is not specified\n");
2001 #endif
2002 return (NULL);
2006 * We search for all addresses on all interfaces from the beginning.
2007 * Comparing an interface with the outgoing interface will be done
2008 * only at the final stage of tiebreaking.
2010 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2012 struct ifaddr_container *ifac;
2015 * We can never take an address that breaks the scope zone
2016 * of the destination.
2018 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2019 continue;
2021 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2022 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2023 struct ifaddr *ifa = ifac->ifa;
2025 if (ifa->ifa_addr->sa_family != AF_INET6)
2026 continue;
2028 src_scope = in6_addrscope(IFA_IN6(ifa));
2031 * Don't use an address before completing DAD
2032 * nor a duplicated address.
2034 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2035 IN6_IFF_NOTREADY)
2036 continue;
2038 /* XXX: is there any case to allow anycasts? */
2039 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2040 IN6_IFF_ANYCAST)
2041 continue;
2043 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2044 IN6_IFF_DETACHED)
2045 continue;
2048 * If this is the first address we find,
2049 * keep it anyway.
2051 if (ifa_best == NULL)
2052 goto replace;
2055 * ifa_best is never NULL beyond this line except
2056 * within the block labeled "replace".
2060 * If ifa_best has a smaller scope than dst and
2061 * the current address has a larger one than
2062 * (or equal to) dst, always replace ifa_best.
2063 * Also, if the current address has a smaller scope
2064 * than dst, ignore it unless ifa_best also has a
2065 * smaller scope.
2066 * Consequently, after the two if-clause below,
2067 * the followings must be satisfied:
2068 * (scope(src) < scope(dst) &&
2069 * scope(best) < scope(dst))
2070 * OR
2071 * (scope(best) >= scope(dst) &&
2072 * scope(src) >= scope(dst))
2074 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2075 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2076 goto replace; /* (A) */
2077 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2078 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2079 continue; /* (B) */
2082 * A deprecated address SHOULD NOT be used in new
2083 * communications if an alternate (non-deprecated)
2084 * address is available and has sufficient scope.
2085 * RFC 2462, Section 5.5.4.
2087 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2088 IN6_IFF_DEPRECATED) {
2090 * Ignore any deprecated addresses if
2091 * specified by configuration.
2093 if (!ip6_use_deprecated)
2094 continue;
2097 * If we have already found a non-deprecated
2098 * candidate, just ignore deprecated addresses.
2100 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2101 continue;
2105 * A non-deprecated address is always preferred
2106 * to a deprecated one regardless of scopes and
2107 * address matching (Note invariants ensured by the
2108 * conditions (A) and (B) above.)
2110 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2111 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2112 IN6_IFF_DEPRECATED))
2113 goto replace;
2116 * When we use temporary addresses described in
2117 * RFC 3041, we prefer temporary addresses to
2118 * public autoconf addresses. Again, note the
2119 * invariants from (A) and (B). Also note that we
2120 * don't have any preference between static addresses
2121 * and autoconf addresses (despite of whether or not
2122 * the latter is temporary or public.)
2124 if (ip6_use_tempaddr) {
2125 struct in6_ifaddr *ifat;
2127 ifat = (struct in6_ifaddr *)ifa;
2128 if ((ifa_best->ia6_flags &
2129 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2130 == IN6_IFF_AUTOCONF &&
2131 (ifat->ia6_flags &
2132 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2133 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2134 goto replace;
2136 if ((ifa_best->ia6_flags &
2137 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2138 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2139 (ifat->ia6_flags &
2140 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2141 == IN6_IFF_AUTOCONF) {
2142 continue;
2147 * At this point, we have two cases:
2148 * 1. we are looking at a non-deprecated address,
2149 * and ifa_best is also non-deprecated.
2150 * 2. we are looking at a deprecated address,
2151 * and ifa_best is also deprecated.
2152 * Also, we do not have to consider a case where
2153 * the scope of if_best is larger(smaller) than dst and
2154 * the scope of the current address is smaller(larger)
2155 * than dst. Such a case has already been covered.
2156 * Tiebreaking is done according to the following
2157 * items:
2158 * - the scope comparison between the address and
2159 * dst (dscopecmp)
2160 * - the scope comparison between the address and
2161 * ifa_best (bscopecmp)
2162 * - if the address match dst longer than ifa_best
2163 * (matchcmp)
2164 * - if the address is on the outgoing I/F (outI/F)
2166 * Roughly speaking, the selection policy is
2167 * - the most important item is scope. The same scope
2168 * is best. Then search for a larger scope.
2169 * Smaller scopes are the last resort.
2170 * - A deprecated address is chosen only when we have
2171 * no address that has an enough scope, but is
2172 * prefered to any addresses of smaller scopes
2173 * (this must be already done above.)
2174 * - addresses on the outgoing I/F are preferred to
2175 * ones on other interfaces if none of above
2176 * tiebreaks. In the table below, the column "bI"
2177 * means if the best_ifa is on the outgoing
2178 * interface, and the column "sI" means if the ifa
2179 * is on the outgoing interface.
2180 * - If there is no other reasons to choose one,
2181 * longest address match against dst is considered.
2183 * The precise decision table is as follows:
2184 * dscopecmp bscopecmp match bI oI | replace?
2185 * N/A equal N/A Y N | No (1)
2186 * N/A equal N/A N Y | Yes (2)
2187 * N/A equal larger N/A | Yes (3)
2188 * N/A equal !larger N/A | No (4)
2189 * larger larger N/A N/A | No (5)
2190 * larger smaller N/A N/A | Yes (6)
2191 * smaller larger N/A N/A | Yes (7)
2192 * smaller smaller N/A N/A | No (8)
2193 * equal smaller N/A N/A | Yes (9)
2194 * equal larger (already done at A above)
2196 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2197 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2199 if (bscopecmp == 0) {
2200 struct ifnet *bifp = ifa_best->ia_ifp;
2202 if (bifp == oifp && ifp != oifp) /* (1) */
2203 continue;
2204 if (bifp != oifp && ifp == oifp) /* (2) */
2205 goto replace;
2208 * Both bifp and ifp are on the outgoing
2209 * interface, or both two are on a different
2210 * interface from the outgoing I/F.
2211 * now we need address matching against dst
2212 * for tiebreaking.
2214 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2215 matchcmp = tlen - blen;
2216 if (matchcmp > 0) /* (3) */
2217 goto replace;
2218 continue; /* (4) */
2220 if (dscopecmp > 0) {
2221 if (bscopecmp > 0) /* (5) */
2222 continue;
2223 goto replace; /* (6) */
2225 if (dscopecmp < 0) {
2226 if (bscopecmp > 0) /* (7) */
2227 goto replace;
2228 continue; /* (8) */
2231 /* now dscopecmp must be 0 */
2232 if (bscopecmp < 0)
2233 goto replace; /* (9) */
2235 replace:
2236 ifa_best = (struct in6_ifaddr *)ifa;
2237 blen = tlen >= 0 ? tlen :
2238 in6_matchlen(IFA_IN6(ifa), dst);
2239 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2243 /* count statistics for future improvements */
2244 if (ifa_best == NULL)
2245 ip6stat.ip6s_sources_none++;
2246 else {
2247 if (oifp == ifa_best->ia_ifp)
2248 ip6stat.ip6s_sources_sameif[best_scope]++;
2249 else
2250 ip6stat.ip6s_sources_otherif[best_scope]++;
2252 if (best_scope == dst_scope)
2253 ip6stat.ip6s_sources_samescope[best_scope]++;
2254 else
2255 ip6stat.ip6s_sources_otherscope[best_scope]++;
2257 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2258 ip6stat.ip6s_sources_deprecated[best_scope]++;
2261 return (ifa_best);
2265 * return the best address out of the same scope. if no address was
2266 * found, return the first valid address from designated IF.
2268 struct in6_ifaddr *
2269 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2271 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2272 struct ifaddr_container *ifac;
2273 struct in6_ifaddr *besta = 0;
2274 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2276 dep[0] = dep[1] = NULL;
2279 * We first look for addresses in the same scope.
2280 * If there is one, return it.
2281 * If two or more, return one which matches the dst longest.
2282 * If none, return one of global addresses assigned other ifs.
2284 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2285 struct ifaddr *ifa = ifac->ifa;
2287 if (ifa->ifa_addr->sa_family != AF_INET6)
2288 continue;
2289 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2290 continue; /* XXX: is there any case to allow anycast? */
2291 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2292 continue; /* don't use this interface */
2293 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2294 continue;
2295 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2296 if (ip6_use_deprecated)
2297 dep[0] = (struct in6_ifaddr *)ifa;
2298 continue;
2301 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2303 * call in6_matchlen() as few as possible
2305 if (besta) {
2306 if (blen == -1)
2307 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2308 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2309 if (tlen > blen) {
2310 blen = tlen;
2311 besta = (struct in6_ifaddr *)ifa;
2313 } else
2314 besta = (struct in6_ifaddr *)ifa;
2317 if (besta)
2318 return (besta);
2320 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2321 struct ifaddr *ifa = ifac->ifa;
2323 if (ifa->ifa_addr->sa_family != AF_INET6)
2324 continue;
2325 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2326 continue; /* XXX: is there any case to allow anycast? */
2327 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2328 continue; /* don't use this interface */
2329 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2330 continue;
2331 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2332 if (ip6_use_deprecated)
2333 dep[1] = (struct in6_ifaddr *)ifa;
2334 continue;
2337 return (struct in6_ifaddr *)ifa;
2340 /* use the last-resort values, that are, deprecated addresses */
2341 if (dep[0])
2342 return dep[0];
2343 if (dep[1])
2344 return dep[1];
2346 return NULL;
2350 * perform DAD when interface becomes IFF_UP.
2352 void
2353 in6_if_up(struct ifnet *ifp)
2355 struct ifaddr_container *ifac;
2356 struct in6_ifaddr *ia;
2357 int dad_delay; /* delay ticks before DAD output */
2360 * special cases, like 6to4, are handled in in6_ifattach
2362 in6_ifattach(ifp, NULL);
2364 dad_delay = 0;
2365 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2366 struct ifaddr *ifa = ifac->ifa;
2368 if (ifa->ifa_addr->sa_family != AF_INET6)
2369 continue;
2370 ia = (struct in6_ifaddr *)ifa;
2371 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2372 nd6_dad_start(ifa, &dad_delay);
2377 in6if_do_dad(struct ifnet *ifp)
2379 if (ifp->if_flags & IFF_LOOPBACK)
2380 return (0);
2382 switch (ifp->if_type) {
2383 #ifdef IFT_DUMMY
2384 case IFT_DUMMY:
2385 #endif
2386 case IFT_FAITH:
2388 * These interfaces do not have the IFF_LOOPBACK flag,
2389 * but loop packets back. We do not have to do DAD on such
2390 * interfaces. We should even omit it, because loop-backed
2391 * NS would confuse the DAD procedure.
2393 return (0);
2394 default:
2396 * Our DAD routine requires the interface up and running.
2397 * However, some interfaces can be up before the RUNNING
2398 * status. Additionaly, users may try to assign addresses
2399 * before the interface becomes up (or running).
2400 * We simply skip DAD in such a case as a work around.
2401 * XXX: we should rather mark "tentative" on such addresses,
2402 * and do DAD after the interface becomes ready.
2404 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2405 (IFF_UP|IFF_RUNNING))
2406 return (0);
2408 return (1);
2413 * Calculate max IPv6 MTU through all the interfaces and store it
2414 * to in6_maxmtu.
2416 void
2417 in6_setmaxmtu(void)
2419 unsigned long maxmtu = 0;
2420 struct ifnet *ifp;
2422 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2424 if (!(ifp->if_flags & IFF_LOOPBACK) &&
2425 ND_IFINFO(ifp)->linkmtu > maxmtu)
2426 maxmtu = ND_IFINFO(ifp)->linkmtu;
2428 if (maxmtu) /* update only when maxmtu is positive */
2429 in6_maxmtu = maxmtu;
2432 void *
2433 in6_domifattach(struct ifnet *ifp)
2435 struct in6_ifextra *ext;
2437 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2438 bzero(ext, sizeof(*ext));
2440 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2441 M_IFADDR, M_WAITOK);
2442 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2444 ext->icmp6_ifstat =
2445 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2446 M_IFADDR, M_WAITOK);
2447 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2449 ext->nd_ifinfo = nd6_ifattach(ifp);
2450 ext->scope6_id = scope6_ifattach(ifp);
2451 return ext;
2454 void
2455 in6_domifdetach(struct ifnet *ifp, void *aux)
2457 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2458 scope6_ifdetach(ext->scope6_id);
2459 nd6_ifdetach(ext->nd_ifinfo);
2460 kfree(ext->in6_ifstat, M_IFADDR);
2461 kfree(ext->icmp6_ifstat, M_IFADDR);
2462 kfree(ext, M_IFADDR);
2466 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2467 * v4 mapped addr or v4 compat addr
2469 void
2470 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2472 bzero(sin, sizeof(*sin));
2473 sin->sin_len = sizeof(struct sockaddr_in);
2474 sin->sin_family = AF_INET;
2475 sin->sin_port = sin6->sin6_port;
2476 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2479 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2480 void
2481 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2483 bzero(sin6, sizeof(*sin6));
2484 sin6->sin6_len = sizeof(struct sockaddr_in6);
2485 sin6->sin6_family = AF_INET6;
2486 sin6->sin6_port = sin->sin_port;
2487 sin6->sin6_addr.s6_addr32[0] = 0;
2488 sin6->sin6_addr.s6_addr32[1] = 0;
2489 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2490 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2493 /* Convert sockaddr_in6 into sockaddr_in. */
2494 void
2495 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2497 struct sockaddr_in *sin_p;
2498 struct sockaddr_in6 sin6;
2501 * Save original sockaddr_in6 addr and convert it
2502 * to sockaddr_in.
2504 sin6 = *(struct sockaddr_in6 *)nam;
2505 sin_p = (struct sockaddr_in *)nam;
2506 in6_sin6_2_sin(sin_p, &sin6);
2509 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2510 void
2511 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2513 struct sockaddr_in *sin_p;
2514 struct sockaddr_in6 *sin6_p;
2516 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2517 M_WAITOK);
2518 sin_p = (struct sockaddr_in *)*nam;
2519 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2520 FREE(*nam, M_SONAME);
2521 *nam = (struct sockaddr *)sin6_p;