Relocate private panel library to intended location
[dragonfly.git] / sys / netinet6 / in6.c
blobb67e309604e7d81e0f23bb603c0335e2d83e6005
1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */
2 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
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.
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.
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
61 * @(#)in.c 8.2 (Berkeley) 11/15/93
64 #include "opt_inet.h"
65 #include "opt_inet6.h"
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/priv.h>
76 #include <sys/time.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
79 #include <sys/jail.h>
81 #include <sys/thread2.h>
82 #include <sys/msgport2.h>
84 #include <net/if.h>
85 #include <net/if_types.h>
86 #include <net/route.h>
87 #include <net/if_dl.h>
88 #include <net/netmsg2.h>
89 #include <net/netisr2.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet/if_ether.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/in_pcb.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet6/mld6_var.h>
102 #include <netinet6/ip6_mroute.h>
103 #include <netinet6/in6_ifattach.h>
104 #include <netinet6/scope6_var.h>
105 #include <netinet6/in6_pcb.h>
106 #include <netinet6/in6_var.h>
108 #include <net/net_osdep.h>
111 * Definitions of some costant IP6 addresses.
113 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
114 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
115 const struct in6_addr kin6addr_nodelocal_allnodes =
116 IN6ADDR_NODELOCAL_ALLNODES_INIT;
117 const struct in6_addr kin6addr_linklocal_allnodes =
118 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
119 const struct in6_addr kin6addr_linklocal_allrouters =
120 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
122 const struct in6_addr in6mask0 = IN6MASK0;
123 const struct in6_addr in6mask32 = IN6MASK32;
124 const struct in6_addr in6mask64 = IN6MASK64;
125 const struct in6_addr in6mask96 = IN6MASK96;
126 const struct in6_addr in6mask128 = IN6MASK128;
128 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
129 0, 0, IN6ADDR_ANY_INIT, 0};
131 static int in6_lifaddr_ioctl (u_long, caddr_t, struct ifnet *,
132 struct thread *);
133 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
134 struct sockaddr_in6 *, int);
135 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
136 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
138 static void in6_control_internal_dispatch(netmsg_t);
139 static int in6_control_internal(u_long, caddr_t, struct ifnet *,
140 struct thread *);
142 struct in6_multihead in6_multihead; /* XXX BSS initialization */
144 int (*faithprefix_p)(struct in6_addr *);
147 * Subroutine for in6_ifaddloop() and in6_ifremloop().
148 * This routine does actual work.
150 static void
151 in6_ifloop_request(int cmd, struct ifaddr *ifa)
153 struct sockaddr_in6 all1_sa;
154 struct rt_addrinfo rtinfo;
155 int error;
157 bzero(&all1_sa, sizeof(all1_sa));
158 all1_sa.sin6_family = AF_INET6;
159 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
160 all1_sa.sin6_addr = in6mask128;
163 * We specify the address itself as the gateway, and set the
164 * RTF_LLINFO flag, so that the corresponding host route would have
165 * the flag, and thus applications that assume traditional behavior
166 * would be happy. Note that we assume the caller of the function
167 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
168 * which changes the outgoing interface to the loopback interface.
170 bzero(&rtinfo, sizeof(struct rt_addrinfo));
171 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
172 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
173 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
174 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
176 error = rtrequest1_global(cmd, &rtinfo,
177 in6_ifloop_request_callback, ifa, RTREQ_PRIO_NORM);
178 if (error != 0) {
179 log(LOG_ERR, "in6_ifloop_request: "
180 "%s operation failed for %s (errno=%d)\n",
181 cmd == RTM_ADD ? "ADD" : "DELETE",
182 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
183 error);
187 static void
188 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
189 struct rtentry *rt, void *arg)
191 struct ifaddr *ifa = arg;
193 if (error)
194 goto done;
197 * Make sure rt_ifa be equal to IFA, the second argument of the
198 * function.
199 * We need this because when we refer to rt_ifa->ia6_flags in
200 * ip6_input, we assume that the rt_ifa points to the address instead
201 * of the loopback address.
203 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
204 ++rt->rt_refcnt;
205 IFAFREE(rt->rt_ifa);
206 IFAREF(ifa);
207 rt->rt_ifa = ifa;
208 --rt->rt_refcnt;
212 * Report the addition/removal of the address to the routing socket.
213 * XXX: since we called rtinit for a p2p interface with a destination,
214 * we end up reporting twice in such a case. Should we rather
215 * omit the second report?
217 if (rt) {
218 if (mycpuid == 0)
219 rt_newaddrmsg(cmd, ifa, error, rt);
220 if (cmd == RTM_DELETE) {
221 if (rt->rt_refcnt == 0) {
222 ++rt->rt_refcnt;
223 rtfree(rt);
227 done:
228 /* no way to return any new error */
233 * Add ownaddr as loopback rtentry. We previously add the route only if
234 * necessary (ex. on a p2p link). However, since we now manage addresses
235 * separately from prefixes, we should always add the route. We can't
236 * rely on the cloning mechanism from the corresponding interface route
237 * any more.
239 void
240 in6_ifaddloop(struct ifaddr *ifa)
242 struct rtentry *rt;
244 /* If there is no loopback entry, allocate one. */
245 rt = rtpurelookup(ifa->ifa_addr);
246 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
247 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
248 in6_ifloop_request(RTM_ADD, ifa);
249 if (rt != NULL)
250 rt->rt_refcnt--;
254 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
255 * if it exists.
257 void
258 in6_ifremloop(struct ifaddr *ifa)
260 struct in6_ifaddr *ia;
261 struct rtentry *rt;
262 int ia_count = 0;
265 * Some of BSD variants do not remove cloned routes
266 * from an interface direct route, when removing the direct route
267 * (see comments in net/net_osdep.h). Even for variants that do remove
268 * cloned routes, they could fail to remove the cloned routes when
269 * we handle multple addresses that share a common prefix.
270 * So, we should remove the route corresponding to the deleted address
271 * regardless of the result of in6_is_ifloop_auto().
275 * Delete the entry only if exact one ifa exists. More than one ifa
276 * can exist if we assign a same single address to multiple
277 * (probably p2p) interfaces.
278 * XXX: we should avoid such a configuration in IPv6...
280 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
281 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
282 ia_count++;
283 if (ia_count > 1)
284 break;
288 if (ia_count == 1) {
290 * Before deleting, check if a corresponding loopbacked host
291 * route surely exists. With this check, we can avoid to
292 * delete an interface direct route whose destination is same
293 * as the address being removed. This can happen when remofing
294 * a subnet-router anycast address on an interface attahced
295 * to a shared medium.
297 rt = rtpurelookup(ifa->ifa_addr);
298 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
299 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
300 rt->rt_refcnt--;
301 in6_ifloop_request(RTM_DELETE, ifa);
307 in6_mask2len(const struct in6_addr *mask, const u_char *lim0)
309 int x = 0, y;
310 const u_char *lim = lim0, *p;
312 if (lim0 == NULL ||
313 lim0 - (const u_char *)mask > sizeof(*mask)) {
314 /* Ignore the scope_id part */
315 lim = (const u_char *)mask + sizeof(*mask);
317 for (p = (const u_char *)mask; p < lim; x++, p++) {
318 if (*p != 0xff)
319 break;
321 y = 0;
322 if (p < lim) {
323 for (y = 0; y < 8; y++) {
324 if ((*p & (0x80 >> y)) == 0)
325 break;
330 * When the limit pointer is given, do a stricter check on the
331 * remaining bits.
333 if (p < lim) {
334 if (y != 0 && (*p & (0x00ff >> y)) != 0)
335 return (-1);
336 for (p = p + 1; p < lim; p++)
337 if (*p != 0)
338 return (-1);
341 return x * 8 + y;
344 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
345 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
347 void
348 in6_control_dispatch(netmsg_t msg)
350 int error;
352 error = in6_control(msg->control.nm_cmd,
353 msg->control.nm_data,
354 msg->control.nm_ifp,
355 msg->control.nm_td);
356 lwkt_replymsg(&msg->control.base.lmsg, error);
360 in6_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
362 struct netmsg_pru_control msg;
364 switch (cmd) {
365 case SIOCSIFPREFIX_IN6:
366 case SIOCDIFPREFIX_IN6:
367 case SIOCAIFPREFIX_IN6:
368 case SIOCCIFPREFIX_IN6:
369 case SIOCSGIFPREFIX_IN6:
370 case SIOCGIFPREFIX_IN6:
371 log(LOG_NOTICE, "prefix ioctls are now invalidated. "
372 "please use ifconfig.\n");
373 return (EOPNOTSUPP);
375 case SIOCSIFADDR_IN6:
376 case SIOCSIFDSTADDR_IN6:
377 case SIOCSIFNETMASK_IN6:
379 * Since IPv6 allows a node to assign multiple addresses
380 * on a single interface, SIOCSIFxxx ioctls are not suitable
381 * and should be unused.
383 /* We decided to obsolete this command (20000704) */
384 return (EINVAL);
386 case SIOCSIFADDR:
387 case SIOCSIFDSTADDR:
388 case SIOCSIFBRDADDR:
389 case SIOCSIFNETMASK:
391 * Do not pass those ioctl to driver handler since they are not
392 * properly setup. Instead just error out.
394 return (EOPNOTSUPP);
396 /* mroute */
397 case SIOCGETSGCNT_IN6:
398 case SIOCGETMIFCNT_IN6:
399 /* srcsel policy */
400 case SIOCAADDRCTL_POLICY:
401 case SIOCDADDRCTL_POLICY:
402 /* nd6 */
403 case SIOCSNDFLUSH_IN6:
404 case SIOCSPFXFLUSH_IN6:
405 case SIOCSRTRFLUSH_IN6:
406 case SIOCSDEFIFACE_IN6:
407 case SIOCSIFINFO_FLAGS:
408 case OSIOCGIFINFO_IN6:
409 case SIOCGIFINFO_IN6:
410 case SIOCGDRLST_IN6:
411 case SIOCGPRLST_IN6:
412 case SIOCGNBRINFO_IN6:
413 case SIOCGDEFIFACE_IN6:
414 /* scope6 */
415 case SIOCSSCOPE6:
416 case SIOCGSCOPE6:
417 case SIOCGSCOPE6DEF:
418 /* change address */
419 case SIOCALIFADDR:
420 case SIOCDLIFADDR:
421 case SIOCSIFALIFETIME_IN6:
422 case SIOCAIFADDR_IN6:
423 case SIOCDIFADDR_IN6:
425 * Dispatch these SIOCs to netisr0.
427 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
428 in6_control_internal_dispatch);
429 msg.nm_cmd = cmd;
430 msg.nm_data = data;
431 msg.nm_ifp = ifp;
432 msg.nm_td = td;
433 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
434 return msg.base.lmsg.ms_error;
436 default:
437 return in6_control_internal(cmd, data, ifp, td);
441 static void
442 in6_control_internal_dispatch(netmsg_t msg)
444 int error;
446 error = in6_control_internal(msg->control.nm_cmd, msg->control.nm_data,
447 msg->control.nm_ifp, msg->control.nm_td);
448 lwkt_replymsg(&msg->lmsg, error);
451 static int
452 in6_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
453 struct thread *td)
455 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
456 struct in6_ifaddr *ia = NULL;
457 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
458 struct in6_ifextra *xtra;
459 boolean_t privileged;
460 int error;
462 privileged = FALSE;
463 if (priv_check(td, PRIV_ROOT) == 0)
464 privileged = TRUE;
466 switch (cmd) {
467 case SIOCALIFADDR:
468 case SIOCDLIFADDR:
469 if (!privileged)
470 return (EPERM);
471 /* FALLTHROUGH */
472 case SIOCGLIFADDR:
473 if (ifp == NULL)
474 return (EOPNOTSUPP);
475 return in6_lifaddr_ioctl(cmd, data, ifp, td);
478 switch (cmd) {
479 case SIOCGETSGCNT_IN6:
480 case SIOCGETMIFCNT_IN6:
481 return (mrt6_ioctl(cmd, data));
484 switch(cmd) {
485 case SIOCAADDRCTL_POLICY:
486 case SIOCDADDRCTL_POLICY:
487 if (!privileged)
488 return (EPERM);
489 return (in6_src_ioctl(cmd, data));
492 if (ifp == NULL)
493 return (EOPNOTSUPP);
495 switch (cmd) {
496 case SIOCSNDFLUSH_IN6:
497 case SIOCSPFXFLUSH_IN6:
498 case SIOCSRTRFLUSH_IN6:
499 case SIOCSDEFIFACE_IN6:
500 case SIOCSIFINFO_FLAGS:
501 if (!privileged)
502 return (EPERM);
503 /* FALLTHROUGH */
504 case OSIOCGIFINFO_IN6:
505 case SIOCGIFINFO_IN6:
506 case SIOCGDRLST_IN6:
507 case SIOCGPRLST_IN6:
508 case SIOCGNBRINFO_IN6:
509 case SIOCGDEFIFACE_IN6:
510 return (nd6_ioctl(cmd, data, ifp));
513 switch (cmd) {
514 case SIOCSSCOPE6:
515 if (!privileged)
516 return (EPERM);
517 return (scope6_set(ifp,
518 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
520 case SIOCGSCOPE6:
521 return (scope6_get(ifp,
522 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
524 case SIOCGSCOPE6DEF:
525 return (scope6_get_default((struct scope6_id *)
526 ifr->ifr_ifru.ifru_scope_id));
530 * Find address for this interface, if it exists.
532 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
533 struct sockaddr_in6 *sa6 =
534 (struct sockaddr_in6 *)&ifra->ifra_addr;
536 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
537 if (sa6->sin6_addr.s6_addr16[1] == 0) {
538 /* Link ID is not embedded by the user */
539 sa6->sin6_addr.s6_addr16[1] =
540 htons(ifp->if_index);
541 } else if (sa6->sin6_addr.s6_addr16[1] !=
542 htons(ifp->if_index)) {
543 /* Link ID contradicts */
544 return (EINVAL);
546 if (sa6->sin6_scope_id) {
547 if (sa6->sin6_scope_id !=
548 (u_int32_t)ifp->if_index)
549 return (EINVAL);
550 sa6->sin6_scope_id = 0; /* XXX: good way? */
553 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
556 switch (cmd) {
557 case SIOCDIFADDR_IN6:
559 * For IPv4, we look for existing in_ifaddr here to allow
560 * "ifconfig if0 delete" to remove first IPv4 address on the
561 * interface. For IPv6, as the spec allow multiple interface
562 * address from the day one, we consider "remove the first one"
563 * semantics to be not preferable.
565 if (ia == NULL)
566 return (EADDRNOTAVAIL);
567 /* FALLTHROUGH */
568 case SIOCAIFADDR_IN6:
570 * We always require users to specify a valid IPv6 address for
571 * the corresponding operation.
573 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
574 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
575 return (EAFNOSUPPORT);
576 if (!privileged)
577 return (EPERM);
578 break;
580 case SIOCGIFADDR_IN6:
581 /* This interface is basically deprecated. Use SIOCGIFCONF. */
582 /* FALLTHROUGH */
583 case SIOCGIFAFLAG_IN6:
584 case SIOCGIFNETMASK_IN6:
585 case SIOCGIFDSTADDR_IN6:
586 case SIOCGIFALIFETIME_IN6:
587 /* Must think again about its semantics */
588 if (ia == NULL)
589 return (EADDRNOTAVAIL);
590 break;
592 case SIOCSIFALIFETIME_IN6:
594 const struct in6_addrlifetime *lt;
596 if (!privileged)
597 return (EPERM);
598 if (ia == NULL)
599 return (EADDRNOTAVAIL);
600 /* Sanity for overflow - beware unsigned */
601 lt = &ifr->ifr_ifru.ifru_lifetime;
602 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
603 lt->ia6t_vltime + time_uptime < time_uptime)
604 return EINVAL;
605 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
606 lt->ia6t_pltime + time_uptime < time_uptime)
607 return EINVAL;
608 break;
612 switch (cmd) {
613 case SIOCGIFADDR_IN6:
614 ifr->ifr_addr = ia->ia_addr;
615 break;
617 case SIOCGIFDSTADDR_IN6:
618 if (!(ifp->if_flags & IFF_POINTOPOINT))
619 return (EINVAL);
621 * XXX: Should we check if ifa_dstaddr is NULL and return
622 * an error?
624 ifr->ifr_dstaddr = ia->ia_dstaddr;
625 break;
627 case SIOCGIFNETMASK_IN6:
628 ifr->ifr_addr = ia->ia_prefixmask;
629 break;
631 case SIOCGIFAFLAG_IN6:
632 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
633 break;
635 case SIOCGIFSTAT_IN6:
636 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
637 return EINVAL;
638 bzero(&ifr->ifr_ifru.ifru_stat,
639 sizeof(ifr->ifr_ifru.ifru_stat));
640 ifr->ifr_ifru.ifru_stat = *xtra->in6_ifstat;
641 break;
643 case SIOCGIFSTAT_ICMP6:
644 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
645 return EINVAL;
646 bzero(&ifr->ifr_ifru.ifru_stat,
647 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
648 ifr->ifr_ifru.ifru_icmp6stat = *xtra->icmp6_ifstat;
649 break;
651 case SIOCGIFALIFETIME_IN6:
652 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
653 break;
655 case SIOCSIFALIFETIME_IN6:
656 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
657 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
658 ia->ia6_lifetime.ia6t_expire =
659 time_uptime + ia->ia6_lifetime.ia6t_vltime;
660 } else {
661 ia->ia6_lifetime.ia6t_expire = 0;
663 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
664 ia->ia6_lifetime.ia6t_preferred =
665 time_uptime + ia->ia6_lifetime.ia6t_pltime;
666 } else {
667 ia->ia6_lifetime.ia6t_preferred = 0;
669 break;
671 case SIOCAIFADDR_IN6:
673 int i, error = 0, iaIsNew;
674 struct nd_prefix pr0, *pr;
676 if (ia != NULL)
677 iaIsNew = 0;
678 else
679 iaIsNew = 1;
682 * First, make or update the interface address structure,
683 * and link it to the list.
685 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
686 return (error);
689 * Then, make the prefix on-link on the interface.
690 * XXX: We'd rather create the prefix before the address, but
691 * we need at least one address to install the corresponding
692 * interface route, so we configure the address first.
696 * Convert mask to prefix length (prefixmask has already
697 * been validated in in6_update_ifa().
699 bzero(&pr0, sizeof(pr0));
700 pr0.ndpr_ifp = ifp;
701 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
702 NULL);
703 if (pr0.ndpr_plen == 128)
704 break; /* no need to install a host route. */
705 pr0.ndpr_prefix = ifra->ifra_addr;
706 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
707 /* Apply the mask for safety. */
708 for (i = 0; i < 4; i++) {
709 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
710 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
713 * XXX: Since we don't have an API to set prefix (not address)
714 * lifetimes, we just use the same lifetimes as addresses.
715 * The (temporarily) installed lifetimes can be overridden by
716 * later advertised RAs (when accept_rtadv is non 0), which is
717 * an intended behavior.
719 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
720 pr0.ndpr_raf_auto =
721 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
722 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
723 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
725 /* Add the prefix if there's one. */
726 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
728 * nd6_prelist_add will install the corresponding
729 * interface route.
731 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
732 return (error);
733 if (pr == NULL) {
734 log(LOG_ERR, "nd6_prelist_add succeeded but "
735 "no prefix\n");
736 return (EINVAL); /* XXX panic here? */
740 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
741 if (ia == NULL) {
742 /* XXX: This should not happen! */
743 log(LOG_ERR, "in6_control: addition succeeded, but"
744 " no ifaddr\n");
745 } else {
746 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
747 ia->ia6_ndpr == NULL) {
749 * New autoconf address
751 ia->ia6_ndpr = pr;
752 pr->ndpr_refcnt++;
755 * If this is the first autoconf address from
756 * the prefix, create a temporary address
757 * as well (when specified).
759 if (ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
760 int e;
762 if ((e = in6_tmpifadd(ia, 1)) != 0) {
763 log(LOG_NOTICE, "in6_control: "
764 "failed to create a "
765 "temporary address, "
766 "errno=%d\n", e);
772 * This might affect the status of autoconfigured
773 * addresses, that is, this address might make
774 * other addresses detached.
776 pfxlist_onlink_check();
778 if (error == 0 && ia) {
779 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
780 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
781 &ia->ia_ifa);
783 break;
786 case SIOCDIFADDR_IN6:
788 int i = 0;
789 struct nd_prefix pr0, *pr;
792 * If the address being deleted is the only one that owns
793 * the corresponding prefix, expire the prefix as well.
794 * XXX: Theoretically, we don't have to warry about such
795 * relationship, since we separate the address management
796 * and the prefix management. We do this, however, to provide
797 * as much backward compatibility as possible in terms of
798 * the ioctl operation.
800 bzero(&pr0, sizeof(pr0));
801 pr0.ndpr_ifp = ifp;
802 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
803 NULL);
804 if (pr0.ndpr_plen == 128)
805 goto purgeaddr;
806 pr0.ndpr_prefix = ia->ia_addr;
807 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
808 for (i = 0; i < 4; i++) {
809 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
810 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
813 * The logic of the following condition is a bit complicated.
814 * We expire the prefix when
815 * 1. The address obeys autoconfiguration and it is the
816 * only owner of the associated prefix, or
817 * 2. The address does not obey autoconf and there is no
818 * other owner of the prefix.
820 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
821 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
822 pr->ndpr_refcnt == 1) ||
823 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
824 pr->ndpr_refcnt == 0)))
825 pr->ndpr_expire = 1; /* XXX: just for expiration */
827 purgeaddr:
828 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
829 &ia->ia_ifa);
830 in6_purgeaddr(&ia->ia_ifa);
831 break;
834 default:
835 if (ifp->if_ioctl == NULL)
836 return (EOPNOTSUPP);
837 ifnet_serialize_all(ifp);
838 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
839 ifnet_deserialize_all(ifp);
840 return (error);
843 return (0);
847 * Update parameters of an IPv6 interface address.
848 * If necessary, a new entry is created and linked into address chains.
849 * This function is separated from in6_control().
850 * XXX: should this be performed under splnet()?
853 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
854 struct in6_ifaddr *ia)
856 int error = 0, hostIsNew = 0, plen = -1;
857 struct in6_ifaddr *oia;
858 struct sockaddr_in6 dst6;
859 struct in6_addrlifetime *lt;
861 /* Validate parameters */
862 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
863 return (EINVAL);
866 * The destination address for a p2p link must have a family
867 * of AF_UNSPEC or AF_INET6.
869 if ((ifp->if_flags & IFF_POINTOPOINT) &&
870 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
871 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
872 return (EAFNOSUPPORT);
874 * validate ifra_prefixmask. don't check sin6_family, netmask
875 * does not carry fields other than sin6_len.
877 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
878 return (EINVAL);
880 * Because the IPv6 address architecture is classless, we require
881 * users to specify a (non 0) prefix length (mask) for a new address.
882 * We also require the prefix (when specified) mask is valid, and thus
883 * reject a non-consecutive mask.
885 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
886 return (EINVAL);
887 if (ifra->ifra_prefixmask.sin6_len != 0) {
888 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
889 (u_char *)&ifra->ifra_prefixmask +
890 ifra->ifra_prefixmask.sin6_len);
891 if (plen <= 0)
892 return (EINVAL);
894 else {
896 * In this case, ia must not be NULL. We just use its prefix
897 * length.
899 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
902 * If the destination address on a p2p interface is specified,
903 * and the address is a scoped one, validate/set the scope
904 * zone identifier.
906 dst6 = ifra->ifra_dstaddr;
907 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
908 (dst6.sin6_family == AF_INET6)) {
909 int scopeid;
911 if ((error = in6_recoverscope(&dst6,
912 &ifra->ifra_dstaddr.sin6_addr,
913 ifp)) != 0)
914 return (error);
915 if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &scopeid))
916 return (EINVAL);
917 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
918 dst6.sin6_scope_id = scopeid;
919 else if (dst6.sin6_scope_id != scopeid)
920 return (EINVAL); /* scope ID mismatch. */
921 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
922 != 0)
923 return (error);
924 dst6.sin6_scope_id = 0; /* XXX */
927 * The destination address can be specified only for a p2p or a
928 * loopback interface. If specified, the corresponding prefix length
929 * must be 128.
931 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
932 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
933 /* XXX: noisy message */
934 log(LOG_INFO, "in6_update_ifa: a destination can be "
935 "specified for a p2p or a loopback IF only\n");
936 return (EINVAL);
938 if (plen != 128) {
940 * The following message seems noisy, but we dare to
941 * add it for diagnosis.
943 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
944 "when dstaddr is specified\n");
945 return (EINVAL);
948 /* lifetime consistency check */
949 lt = &ifra->ifra_lifetime;
950 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
951 && lt->ia6t_vltime + time_uptime < time_uptime) {
952 return EINVAL;
954 if (lt->ia6t_vltime == 0) {
956 * the following log might be noisy, but this is a typical
957 * configuration mistake or a tool's bug.
959 log(LOG_INFO,
960 "in6_update_ifa: valid lifetime is 0 for %s\n",
961 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
963 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
964 && lt->ia6t_pltime + time_uptime < time_uptime) {
965 return EINVAL;
969 * If this is a new address, allocate a new ifaddr and link it
970 * into chains.
972 if (ia == NULL) {
973 hostIsNew = 1;
974 ia = ifa_create(sizeof(*ia));
976 /* Initialize the address and masks */
977 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
978 ia->ia_addr.sin6_family = AF_INET6;
979 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
980 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
982 * XXX: some functions expect that ifa_dstaddr is not
983 * NULL for p2p interfaces.
985 ia->ia_ifa.ifa_dstaddr
986 = (struct sockaddr *)&ia->ia_dstaddr;
987 } else {
988 ia->ia_ifa.ifa_dstaddr = NULL;
990 ia->ia_ifa.ifa_netmask
991 = (struct sockaddr *)&ia->ia_prefixmask;
993 ia->ia_ifp = ifp;
994 if ((oia = in6_ifaddr) != NULL) {
995 for ( ; oia->ia_next; oia = oia->ia_next)
996 continue;
997 oia->ia_next = ia;
998 } else
999 in6_ifaddr = ia;
1001 ifa_iflink(&ia->ia_ifa, ifp, 1);
1004 /* set prefix mask */
1005 if (ifra->ifra_prefixmask.sin6_len) {
1007 * We prohibit changing the prefix length of an existing
1008 * address, because
1009 * + such an operation should be rare in IPv6, and
1010 * + the operation would confuse prefix management.
1012 if (ia->ia_prefixmask.sin6_len &&
1013 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1014 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
1015 " existing (%s) address should not be changed\n",
1016 ip6_sprintf(&ia->ia_addr.sin6_addr));
1017 error = EINVAL;
1018 goto unlink;
1020 ia->ia_prefixmask = ifra->ifra_prefixmask;
1024 * If a new destination address is specified, scrub the old one and
1025 * install the new destination. Note that the interface must be
1026 * p2p or loopback (see the check above.)
1028 if (dst6.sin6_family == AF_INET6 &&
1029 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
1030 &ia->ia_dstaddr.sin6_addr)) {
1031 int e;
1033 if ((ia->ia_flags & IFA_ROUTE) &&
1034 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1035 != 0) {
1036 log(LOG_ERR, "in6_update_ifa: failed to remove "
1037 "a route to the old destination: %s\n",
1038 ip6_sprintf(&ia->ia_addr.sin6_addr));
1039 /* proceed anyway... */
1041 else
1042 ia->ia_flags &= ~IFA_ROUTE;
1043 ia->ia_dstaddr = dst6;
1046 /* reset the interface and routing table appropriately. */
1047 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1048 goto unlink;
1051 * Beyond this point, we should call in6_purgeaddr upon an error,
1052 * not just go to unlink.
1055 if (ifp->if_flags & IFF_MULTICAST) {
1056 struct sockaddr_in6 mltaddr, mltmask;
1057 struct in6_multi *in6m;
1059 if (hostIsNew) {
1061 * join solicited multicast addr for new host id
1063 struct in6_addr llsol;
1064 bzero(&llsol, sizeof(struct in6_addr));
1065 llsol.s6_addr16[0] = htons(0xff02);
1066 llsol.s6_addr16[1] = htons(ifp->if_index);
1067 llsol.s6_addr32[1] = 0;
1068 llsol.s6_addr32[2] = htonl(1);
1069 llsol.s6_addr32[3] =
1070 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1071 llsol.s6_addr8[12] = 0xff;
1072 in6_addmulti(&llsol, ifp, &error);
1073 if (error != 0) {
1074 log(LOG_WARNING,
1075 "in6_update_ifa: addmulti failed for "
1076 "%s on %s (errno=%d)\n",
1077 ip6_sprintf(&llsol), if_name(ifp),
1078 error);
1079 in6_purgeaddr((struct ifaddr *)ia);
1080 return (error);
1084 bzero(&mltmask, sizeof(mltmask));
1085 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1086 mltmask.sin6_family = AF_INET6;
1087 mltmask.sin6_addr = in6mask32;
1090 * join link-local all-nodes address
1092 bzero(&mltaddr, sizeof(mltaddr));
1093 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1094 mltaddr.sin6_family = AF_INET6;
1095 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1096 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1098 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1099 if (in6m == NULL) {
1100 rtrequest_global(RTM_ADD,
1101 (struct sockaddr *)&mltaddr,
1102 (struct sockaddr *)&ia->ia_addr,
1103 (struct sockaddr *)&mltmask,
1104 RTF_UP|RTF_CLONING); /* xxx */
1105 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1106 if (error != 0) {
1107 log(LOG_WARNING,
1108 "in6_update_ifa: addmulti failed for "
1109 "%s on %s (errno=%d)\n",
1110 ip6_sprintf(&mltaddr.sin6_addr),
1111 if_name(ifp), error);
1116 * join node information group address
1118 #define hostnamelen strlen(hostname)
1119 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1120 == 0) {
1121 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1122 if (in6m == NULL && ia != NULL) {
1123 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1124 if (error != 0) {
1125 log(LOG_WARNING, "in6_update_ifa: "
1126 "addmulti failed for "
1127 "%s on %s (errno=%d)\n",
1128 ip6_sprintf(&mltaddr.sin6_addr),
1129 if_name(ifp), error);
1133 #undef hostnamelen
1136 * join node-local all-nodes address, on loopback.
1137 * XXX: since "node-local" is obsoleted by interface-local,
1138 * we have to join the group on every interface with
1139 * some interface-boundary restriction.
1141 if (ifp->if_flags & IFF_LOOPBACK) {
1142 struct in6_ifaddr *ia_loop;
1144 struct in6_addr loop6 = kin6addr_loopback;
1145 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1147 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1149 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1150 if (in6m == NULL && ia_loop != NULL) {
1151 rtrequest_global(RTM_ADD,
1152 (struct sockaddr *)&mltaddr,
1153 (struct sockaddr *)&ia_loop->ia_addr,
1154 (struct sockaddr *)&mltmask,
1155 RTF_UP);
1156 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1157 if (error != 0) {
1158 log(LOG_WARNING, "in6_update_ifa: "
1159 "addmulti failed for %s on %s "
1160 "(errno=%d)\n",
1161 ip6_sprintf(&mltaddr.sin6_addr),
1162 if_name(ifp), error);
1168 ia->ia6_flags = ifra->ifra_flags;
1169 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1170 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1172 ia->ia6_lifetime = ifra->ifra_lifetime;
1173 /* for sanity */
1174 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1175 ia->ia6_lifetime.ia6t_expire =
1176 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1177 } else
1178 ia->ia6_lifetime.ia6t_expire = 0;
1179 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1180 ia->ia6_lifetime.ia6t_preferred =
1181 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1182 } else
1183 ia->ia6_lifetime.ia6t_preferred = 0;
1186 * Perform DAD, if needed.
1187 * XXX It may be of use, if we can administratively
1188 * disable DAD.
1190 if (in6if_do_dad(ifp) && !(ifra->ifra_flags & IN6_IFF_NODAD)) {
1191 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1192 nd6_dad_start((struct ifaddr *)ia, NULL);
1195 return (error);
1197 unlink:
1199 * XXX: if a change of an existing address failed, keep the entry
1200 * anyway.
1202 if (hostIsNew)
1203 in6_unlink_ifa(ia, ifp);
1204 return (error);
1207 void
1208 in6_purgeaddr(struct ifaddr *ifa)
1210 struct ifnet *ifp = ifa->ifa_ifp;
1211 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1213 /* stop DAD processing */
1214 nd6_dad_stop(ifa);
1217 * delete route to the destination of the address being purged.
1218 * The interface must be p2p or loopback in this case.
1220 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1221 int e;
1223 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1224 != 0) {
1225 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1226 "a route to the p2p destination: %s on %s, "
1227 "errno=%d\n",
1228 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1230 /* proceed anyway... */
1232 else
1233 ia->ia_flags &= ~IFA_ROUTE;
1236 /* Remove ownaddr's loopback rtentry, if it exists. */
1237 in6_ifremloop(&(ia->ia_ifa));
1239 if (ifp->if_flags & IFF_MULTICAST) {
1241 * delete solicited multicast addr for deleting host id
1243 struct in6_multi *in6m;
1244 struct in6_addr llsol;
1245 bzero(&llsol, sizeof(struct in6_addr));
1246 llsol.s6_addr16[0] = htons(0xff02);
1247 llsol.s6_addr16[1] = htons(ifp->if_index);
1248 llsol.s6_addr32[1] = 0;
1249 llsol.s6_addr32[2] = htonl(1);
1250 llsol.s6_addr32[3] =
1251 ia->ia_addr.sin6_addr.s6_addr32[3];
1252 llsol.s6_addr8[12] = 0xff;
1254 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1255 if (in6m)
1256 in6_delmulti(in6m);
1259 in6_unlink_ifa(ia, ifp);
1262 static void
1263 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1265 struct in6_ifaddr *oia;
1267 crit_enter();
1269 ifa_ifunlink(&ia->ia_ifa, ifp);
1271 oia = ia;
1272 if (oia == (ia = in6_ifaddr))
1273 in6_ifaddr = ia->ia_next;
1274 else {
1275 while (ia->ia_next && (ia->ia_next != oia))
1276 ia = ia->ia_next;
1277 if (ia->ia_next)
1278 ia->ia_next = oia->ia_next;
1279 else {
1280 /* search failed */
1281 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1286 * When an autoconfigured address is being removed, release the
1287 * reference to the base prefix. Also, since the release might
1288 * affect the status of other (detached) addresses, call
1289 * pfxlist_onlink_check().
1291 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1292 if (oia->ia6_ndpr == NULL) {
1293 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1294 "%p has no prefix\n", oia);
1295 } else {
1296 oia->ia6_ndpr->ndpr_refcnt--;
1297 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1298 oia->ia6_ndpr = NULL;
1301 pfxlist_onlink_check();
1305 * release another refcnt for the link from in6_ifaddr.
1306 * Note that we should decrement the refcnt at least once for all *BSD.
1308 ifa_destroy(&oia->ia_ifa);
1310 crit_exit();
1313 void
1314 in6_purgeif(struct ifnet *ifp)
1316 struct ifaddr_container *ifac, *next;
1318 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1319 ifa_link, next) {
1320 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1321 continue;
1322 in6_purgeaddr(ifac->ifa);
1325 in6_ifdetach(ifp);
1329 * SIOC[GAD]LIFADDR.
1330 * SIOCGLIFADDR: get first address. (?)
1331 * SIOCGLIFADDR with IFLR_PREFIX:
1332 * get first address that matches the specified prefix.
1333 * SIOCALIFADDR: add the specified address.
1334 * SIOCALIFADDR with IFLR_PREFIX:
1335 * add the specified prefix, filling hostid part from
1336 * the first link-local address. prefixlen must be <= 64.
1337 * SIOCDLIFADDR: delete the specified address.
1338 * SIOCDLIFADDR with IFLR_PREFIX:
1339 * delete the first address that matches the specified prefix.
1340 * return values:
1341 * EINVAL on invalid parameters
1342 * EADDRNOTAVAIL on prefix match failed/specified address not found
1343 * other values may be returned from in6_ioctl()
1345 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1346 * this is to accomodate address naming scheme other than RFC2374,
1347 * in the future.
1348 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1349 * address encoding scheme. (see figure on page 8)
1351 static int
1352 in6_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp,
1353 struct thread *td)
1355 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1356 struct sockaddr *sa;
1358 /* sanity checks */
1359 if (!data || !ifp) {
1360 panic("invalid argument to in6_lifaddr_ioctl");
1361 /*NOTRECHED*/
1364 switch (cmd) {
1365 case SIOCGLIFADDR:
1366 /* address must be specified on GET with IFLR_PREFIX */
1367 if (!(iflr->flags & IFLR_PREFIX))
1368 break;
1369 /* FALLTHROUGH */
1370 case SIOCALIFADDR:
1371 case SIOCDLIFADDR:
1372 /* address must be specified on ADD and DELETE */
1373 sa = (struct sockaddr *)&iflr->addr;
1374 if (sa->sa_family != AF_INET6)
1375 return EINVAL;
1376 if (sa->sa_len != sizeof(struct sockaddr_in6))
1377 return EINVAL;
1378 /* XXX need improvement */
1379 sa = (struct sockaddr *)&iflr->dstaddr;
1380 if (sa->sa_family && sa->sa_family != AF_INET6)
1381 return EINVAL;
1382 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1383 return EINVAL;
1384 break;
1385 default: /* shouldn't happen */
1386 #if 0
1387 panic("invalid cmd to in6_lifaddr_ioctl");
1388 /* NOTREACHED */
1389 #else
1390 return EOPNOTSUPP;
1391 #endif
1393 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1394 return EINVAL;
1396 switch (cmd) {
1397 case SIOCALIFADDR:
1399 struct in6_aliasreq ifra;
1400 struct in6_addr *hostid = NULL;
1401 int prefixlen;
1403 if (iflr->flags & IFLR_PREFIX) {
1404 struct ifaddr *ifa;
1405 struct sockaddr_in6 *sin6;
1408 * hostid is to fill in the hostid part of the
1409 * address. hostid points to the first link-local
1410 * address attached to the interface.
1412 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1413 if (!ifa)
1414 return EADDRNOTAVAIL;
1415 hostid = IFA_IN6(ifa);
1417 /* prefixlen must be <= 64. */
1418 if (64 < iflr->prefixlen)
1419 return EINVAL;
1420 prefixlen = iflr->prefixlen;
1422 /* hostid part must be zero. */
1423 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1424 if (sin6->sin6_addr.s6_addr32[2] != 0
1425 || sin6->sin6_addr.s6_addr32[3] != 0) {
1426 return EINVAL;
1428 } else
1429 prefixlen = iflr->prefixlen;
1431 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1432 bzero(&ifra, sizeof(ifra));
1433 bcopy(iflr->iflr_name, ifra.ifra_name,
1434 sizeof(ifra.ifra_name));
1436 bcopy(&iflr->addr, &ifra.ifra_addr,
1437 ((struct sockaddr *)&iflr->addr)->sa_len);
1438 if (hostid) {
1439 /* fill in hostid part */
1440 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1441 hostid->s6_addr32[2];
1442 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1443 hostid->s6_addr32[3];
1446 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1447 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1448 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1449 if (hostid) {
1450 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1451 hostid->s6_addr32[2];
1452 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1453 hostid->s6_addr32[3];
1457 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1458 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1460 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1461 return in6_control_internal(SIOCAIFADDR_IN6, (caddr_t)&ifra,
1462 ifp, td);
1464 case SIOCGLIFADDR:
1465 case SIOCDLIFADDR:
1467 struct ifaddr_container *ifac;
1468 struct in6_ifaddr *ia;
1469 struct in6_addr mask, candidate, match;
1470 struct sockaddr_in6 *sin6;
1471 int cmp;
1473 bzero(&mask, sizeof(mask));
1474 if (iflr->flags & IFLR_PREFIX) {
1475 /* lookup a prefix rather than address. */
1476 in6_prefixlen2mask(&mask, iflr->prefixlen);
1478 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1479 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1480 match.s6_addr32[0] &= mask.s6_addr32[0];
1481 match.s6_addr32[1] &= mask.s6_addr32[1];
1482 match.s6_addr32[2] &= mask.s6_addr32[2];
1483 match.s6_addr32[3] &= mask.s6_addr32[3];
1485 /* if you set extra bits, that's wrong */
1486 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1487 return EINVAL;
1489 cmp = 1;
1490 } else {
1491 if (cmd == SIOCGLIFADDR) {
1492 /* on getting an address, take the 1st match */
1493 cmp = 0; /* XXX */
1494 } else {
1495 /* on deleting an address, do exact match */
1496 in6_prefixlen2mask(&mask, 128);
1497 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1498 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1500 cmp = 1;
1504 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1505 struct ifaddr *ifa = ifac->ifa;
1507 if (ifa->ifa_addr->sa_family != AF_INET6)
1508 continue;
1509 if (!cmp)
1510 break;
1512 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1514 * XXX: this is adhoc, but is necessary to allow
1515 * a user to specify fe80::/64 (not /10) for a
1516 * link-local address.
1518 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1519 candidate.s6_addr16[1] = 0;
1520 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1521 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1522 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1523 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1524 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1525 break;
1527 if (ifac == NULL)
1528 return EADDRNOTAVAIL;
1529 ia = ifa2ia6(ifac->ifa);
1531 if (cmd == SIOCGLIFADDR) {
1532 struct sockaddr_in6 *s6;
1534 /* fill in the if_laddrreq structure */
1535 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1536 s6 = (struct sockaddr_in6 *)&iflr->addr;
1537 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1538 s6->sin6_addr.s6_addr16[1] = 0;
1539 if (in6_addr2zoneid(ifp, &s6->sin6_addr,
1540 &s6->sin6_scope_id))
1541 return (EINVAL);/* XXX */
1543 if (ifp->if_flags & IFF_POINTOPOINT) {
1544 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1545 ia->ia_dstaddr.sin6_len);
1546 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1547 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1548 s6->sin6_addr.s6_addr16[1] = 0;
1549 if (in6_addr2zoneid(ifp,
1550 &s6->sin6_addr, &s6->sin6_scope_id))
1551 return (EINVAL); /* EINVAL */
1553 } else
1554 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1556 iflr->prefixlen =
1557 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1558 NULL);
1560 iflr->flags = ia->ia6_flags; /* XXX */
1562 return 0;
1563 } else {
1564 struct in6_aliasreq ifra;
1566 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1567 bzero(&ifra, sizeof(ifra));
1568 bcopy(iflr->iflr_name, ifra.ifra_name,
1569 sizeof(ifra.ifra_name));
1571 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1572 ia->ia_addr.sin6_len);
1573 if (ifp->if_flags & IFF_POINTOPOINT)
1574 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1575 ia->ia_dstaddr.sin6_len);
1576 else
1577 bzero(&ifra.ifra_dstaddr,
1578 sizeof(ifra.ifra_dstaddr));
1579 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1580 ia->ia_prefixmask.sin6_len);
1582 ifra.ifra_flags = ia->ia6_flags;
1583 return in6_control_internal(SIOCDIFADDR_IN6,
1584 (caddr_t)&ifra, ifp, td);
1589 return EOPNOTSUPP; /* just for safety */
1593 * Initialize an interface's intetnet6 address
1594 * and routing table entry.
1596 static int
1597 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1598 int newhost)
1600 int error = 0, plen;
1602 ia->ia_addr = *sin6;
1604 if (ifp->if_ioctl != NULL) {
1605 ifnet_serialize_all(ifp);
1606 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
1607 ifnet_deserialize_all(ifp);
1608 if (error)
1609 return (error);
1612 ia->ia_ifa.ifa_metric = ifp->if_metric;
1614 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1617 * Special case:
1618 * If the destination address is specified for a point-to-point
1619 * interface, install a route to the destination as an interface
1620 * direct route.
1622 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1623 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1624 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1625 RTF_UP | RTF_HOST)) != 0)
1626 return (error);
1627 ia->ia_flags |= IFA_ROUTE;
1629 if (plen < 128) {
1631 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1633 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1636 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1637 if (newhost) {
1638 /* set the rtrequest function to create llinfo */
1639 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1640 in6_ifaddloop(&(ia->ia_ifa));
1643 return (error);
1646 struct in6_multi_mship *
1647 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1649 struct in6_multi_mship *imm;
1651 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1652 if (!imm) {
1653 *errorp = ENOBUFS;
1654 return NULL;
1656 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1657 if (!imm->i6mm_maddr) {
1658 /* *errorp is alrady set */
1659 kfree(imm, M_IPMADDR);
1660 return NULL;
1662 return imm;
1666 in6_leavegroup(struct in6_multi_mship *imm)
1669 if (imm->i6mm_maddr)
1670 in6_delmulti(imm->i6mm_maddr);
1671 kfree(imm, M_IPMADDR);
1672 return 0;
1676 * Add an address to the list of IP6 multicast addresses for a
1677 * given interface.
1679 struct in6_multi *
1680 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1682 struct in6_multi *in6m;
1683 struct sockaddr_in6 sin6;
1684 struct ifmultiaddr *ifma;
1686 *errorp = 0;
1688 crit_enter();
1691 * Call generic routine to add membership or increment
1692 * refcount. It wants addresses in the form of a sockaddr,
1693 * so we build one here (being careful to zero the unused bytes).
1695 bzero(&sin6, sizeof sin6);
1696 sin6.sin6_family = AF_INET6;
1697 sin6.sin6_len = sizeof sin6;
1698 sin6.sin6_addr = *maddr6;
1699 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1700 if (*errorp) {
1701 crit_exit();
1702 return 0;
1706 * If ifma->ifma_protospec is null, then if_addmulti() created
1707 * a new record. Otherwise, we are done.
1709 if (ifma->ifma_protospec != NULL) {
1710 crit_exit();
1711 return ifma->ifma_protospec;
1714 in6m = kmalloc(sizeof(*in6m), M_IPMADDR, M_INTWAIT | M_ZERO);
1715 in6m->in6m_addr = *maddr6;
1716 in6m->in6m_ifp = ifp;
1717 in6m->in6m_ifma = ifma;
1718 ifma->ifma_protospec = in6m;
1719 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1722 * Let MLD6 know that we have joined a new IP6 multicast
1723 * group.
1725 mld6_start_listening(in6m);
1726 crit_exit();
1727 return (in6m);
1731 * Delete a multicast address record.
1733 void
1734 in6_delmulti(struct in6_multi *in6m)
1736 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1738 crit_enter();
1740 if (ifma->ifma_refcount == 1) {
1742 * No remaining claims to this record; let MLD6 know
1743 * that we are leaving the multicast group.
1745 mld6_stop_listening(in6m);
1746 ifma->ifma_protospec = NULL;
1747 LIST_REMOVE(in6m, in6m_entry);
1748 kfree(in6m, M_IPMADDR);
1750 /* XXX - should be separate API for when we have an ifma? */
1751 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1752 crit_exit();
1756 * Find an IPv6 interface link-local address specific to an interface.
1758 struct in6_ifaddr *
1759 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1761 const struct ifaddr_container *ifac;
1763 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1764 struct ifaddr *ifa = ifac->ifa;
1766 if (ifa->ifa_addr == NULL)
1767 continue; /* just for safety */
1768 if (ifa->ifa_addr->sa_family != AF_INET6)
1769 continue;
1770 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1771 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1772 ignoreflags) != 0)
1773 continue;
1774 return (struct in6_ifaddr *)ifa;
1777 return NULL;
1782 * find the internet address corresponding to a given interface and address.
1784 struct in6_ifaddr *
1785 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1787 const struct ifaddr_container *ifac;
1789 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1790 struct ifaddr *ifa = ifac->ifa;
1792 if (ifa->ifa_addr == NULL)
1793 continue; /* just for safety */
1794 if (ifa->ifa_addr->sa_family != AF_INET6)
1795 continue;
1796 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1797 return (struct in6_ifaddr *)ifa;
1799 return NULL;
1803 * Find a link-local scoped address on ifp and return it if any.
1805 struct in6_ifaddr *
1806 in6ifa_llaonifp(struct ifnet *ifp)
1808 const struct ifaddr_container *ifac;
1810 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1811 const struct sockaddr_in6 *sin6;
1812 struct ifaddr *ifa = ifac->ifa;
1814 if (ifa->ifa_addr->sa_family != AF_INET6)
1815 continue;
1816 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1817 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1818 /* XXX why are mcast addresses ifp address list? */
1819 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1820 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1821 return (struct in6_ifaddr *)ifa;
1823 return NULL;
1827 * find the internet address on a given interface corresponding to a neighbor's
1828 * address.
1830 struct in6_ifaddr *
1831 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1833 struct ifaddr *ifa;
1834 struct in6_ifaddr *ia;
1835 struct ifaddr_container *ifac;
1837 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1838 ifa = ifac->ifa;
1840 if (ifa->ifa_addr == NULL)
1841 continue; /* just for safety */
1842 if (ifa->ifa_addr->sa_family != AF_INET6)
1843 continue;
1844 ia = (struct in6_ifaddr *)ifa;
1845 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1846 &ia->ia_addr.sin6_addr,
1847 &ia->ia_prefixmask.sin6_addr))
1848 return ia;
1851 return NULL;
1855 * Convert IP6 address to printable (loggable) representation.
1857 static char digits[] = "0123456789abcdef";
1858 static int ip6round = 0;
1859 char *
1860 ip6_sprintf(const struct in6_addr *addr)
1862 static char ip6buf[8][48];
1863 int i;
1864 char *cp;
1865 const u_short *a = (const u_short *)addr;
1866 const u_char *d;
1867 int dcolon = 0;
1869 ip6round = (ip6round + 1) & 7;
1870 cp = ip6buf[ip6round];
1872 for (i = 0; i < 8; i++) {
1873 if (dcolon == 1) {
1874 if (*a == 0) {
1875 if (i == 7)
1876 *cp++ = ':';
1877 a++;
1878 continue;
1879 } else
1880 dcolon = 2;
1882 if (*a == 0) {
1883 if (dcolon == 0 && *(a + 1) == 0) {
1884 if (i == 0)
1885 *cp++ = ':';
1886 *cp++ = ':';
1887 dcolon = 1;
1888 } else {
1889 *cp++ = '0';
1890 *cp++ = ':';
1892 a++;
1893 continue;
1895 d = (const u_char *)a;
1896 *cp++ = digits[*d >> 4];
1897 *cp++ = digits[*d++ & 0xf];
1898 *cp++ = digits[*d >> 4];
1899 *cp++ = digits[*d & 0xf];
1900 *cp++ = ':';
1901 a++;
1903 *--cp = 0;
1904 return (ip6buf[ip6round]);
1908 in6_localaddr(struct in6_addr *in6)
1910 struct in6_ifaddr *ia;
1912 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1913 return 1;
1915 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1916 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1917 &ia->ia_prefixmask.sin6_addr))
1918 return 1;
1920 return (0);
1924 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1926 struct in6_ifaddr *ia;
1928 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1929 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1930 &sa6->sin6_addr) &&
1931 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1932 return (1); /* true */
1934 /* XXX: do we still have to go thru the rest of the list? */
1937 return (0); /* false */
1941 * return length of part which dst and src are equal
1942 * hard coding...
1945 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1947 int match = 0;
1948 u_char *s = (u_char *)src, *d = (u_char *)dst;
1949 u_char *lim = s + 16, r;
1951 while (s < lim)
1952 if ((r = (*d++ ^ *s++)) != 0) {
1953 while (r < 128) {
1954 match++;
1955 r <<= 1;
1957 break;
1958 } else
1959 match += 8;
1960 return match;
1963 /* XXX: to be scope conscious */
1965 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1967 int bytelen, bitlen;
1969 /* sanity check */
1970 if (0 > len || len > 128) {
1971 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1972 len);
1973 return (0);
1976 bytelen = len / 8;
1977 bitlen = len % 8;
1979 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1980 return (0);
1981 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1982 p2->s6_addr[bytelen] >> (8 - bitlen))
1983 return (0);
1985 return (1);
1988 void
1989 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1991 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1992 int bytelen, bitlen, i;
1994 /* sanity check */
1995 if (0 > len || len > 128) {
1996 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1997 len);
1998 return;
2001 bzero(maskp, sizeof(*maskp));
2002 bytelen = len / 8;
2003 bitlen = len % 8;
2004 for (i = 0; i < bytelen; i++)
2005 maskp->s6_addr[i] = 0xff;
2006 if (bitlen)
2007 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2011 * return the best address out of the same scope
2013 struct in6_ifaddr *
2014 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst, struct ucred *cred)
2016 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2017 int blen = -1;
2018 struct in6_ifaddr *ifa_best = NULL;
2019 u_int32_t dstzone, odstzone;
2020 int jailed = 0;
2021 const struct ifnet_array *arr;
2022 int i;
2024 if(cred && cred->cr_prison)
2025 jailed = 1;
2027 if (oifp == NULL)
2028 return (NULL);
2030 if (in6_addr2zoneid(oifp, dst, &odstzone))
2031 return (NULL);
2034 * We search for all addresses on all interfaces from the beginning.
2035 * Comparing an interface with the outgoing interface will be done
2036 * only at the final stage of tiebreaking.
2038 arr = ifnet_array_get();
2039 for (i = 0; i < arr->ifnet_count; ++i) {
2040 struct ifnet *ifp = arr->ifnet_arr[i];
2041 struct ifaddr_container *ifac;
2044 * We can never take an address that breaks the scope zone
2045 * of the destination.
2047 if (ifp->if_afdata[AF_INET6] == NULL)
2048 continue;
2049 if (in6_addr2zoneid(ifp, dst, &dstzone) || dstzone != odstzone)
2050 continue;
2052 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2053 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2054 struct ifaddr *ifa = ifac->ifa;
2056 if (ifa->ifa_addr->sa_family != AF_INET6)
2057 continue;
2059 src_scope = in6_addrscope(IFA_IN6(ifa));
2062 * Don't use an address before completing DAD
2063 * nor a duplicated address.
2065 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2066 IN6_IFF_NOTREADY)
2067 continue;
2069 /* XXX: is there any case to allow anycasts? */
2070 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2071 IN6_IFF_ANYCAST)
2072 continue;
2074 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2075 IN6_IFF_DETACHED)
2076 continue;
2078 /* Skip adresses not valid for current jail */
2079 if (jailed &&
2080 !(jailed_ip(cred->cr_prison, (struct sockaddr *)(ifa->ifa_addr)) != 0))
2081 continue;
2084 * If this is the first address we find,
2085 * keep it anyway.
2087 if (ifa_best == NULL)
2088 goto replace;
2091 * ifa_best is never NULL beyond this line except
2092 * within the block labeled "replace".
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))
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) */
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.
2123 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2124 IN6_IFF_DEPRECATED) {
2126 * Ignore any deprecated addresses if
2127 * specified by configuration.
2129 if (!ip6_use_deprecated)
2130 continue;
2133 * If we have already found a non-deprecated
2134 * candidate, just ignore deprecated addresses.
2136 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2137 continue;
2141 * A non-deprecated address is always preferred
2142 * to a deprecated one regardless of scopes and
2143 * address matching (Note invariants ensured by the
2144 * conditions (A) and (B) above.)
2146 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2147 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2148 IN6_IFF_DEPRECATED))
2149 goto replace;
2152 * When we use temporary addresses described in
2153 * RFC 3041, we prefer temporary addresses to
2154 * public autoconf addresses. Again, note the
2155 * invariants from (A) and (B). Also note that we
2156 * don't have any preference between static addresses
2157 * and autoconf addresses (despite of whether or not
2158 * the latter is temporary or public.)
2160 if (ip6_use_tempaddr) {
2161 struct in6_ifaddr *ifat;
2163 ifat = (struct in6_ifaddr *)ifa;
2164 if ((ifa_best->ia6_flags &
2165 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2166 == IN6_IFF_AUTOCONF &&
2167 (ifat->ia6_flags &
2168 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2169 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2170 goto replace;
2172 if ((ifa_best->ia6_flags &
2173 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2174 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2175 (ifat->ia6_flags &
2176 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2177 == IN6_IFF_AUTOCONF) {
2178 continue;
2183 * At this point, we have two cases:
2184 * 1. we are looking at a non-deprecated address,
2185 * and ifa_best is also non-deprecated.
2186 * 2. we are looking at a deprecated address,
2187 * and ifa_best is also deprecated.
2188 * Also, we do not have to consider a case where
2189 * the scope of if_best is larger(smaller) than dst and
2190 * the scope of the current address is smaller(larger)
2191 * than dst. Such a case has already been covered.
2192 * Tiebreaking is done according to the following
2193 * items:
2194 * - the scope comparison between the address and
2195 * dst (dscopecmp)
2196 * - the scope comparison between the address and
2197 * ifa_best (bscopecmp)
2198 * - if the address match dst longer than ifa_best
2199 * (matchcmp)
2200 * - if the address is on the outgoing I/F (outI/F)
2202 * Roughly speaking, the selection policy is
2203 * - the most important item is scope. The same scope
2204 * is best. Then search for a larger scope.
2205 * Smaller scopes are the last resort.
2206 * - A deprecated address is chosen only when we have
2207 * no address that has an enough scope, but is
2208 * prefered to any addresses of smaller scopes
2209 * (this must be already done above.)
2210 * - addresses on the outgoing I/F are preferred to
2211 * ones on other interfaces if none of above
2212 * tiebreaks. In the table below, the column "bI"
2213 * means if the best_ifa is on the outgoing
2214 * interface, and the column "sI" means if the ifa
2215 * is on the outgoing interface.
2216 * - If there is no other reasons to choose one,
2217 * longest address match against dst is considered.
2219 * The precise decision table is as follows:
2220 * dscopecmp bscopecmp match bI oI | replace?
2221 * N/A equal N/A Y N | No (1)
2222 * N/A equal N/A N Y | Yes (2)
2223 * N/A equal larger N/A | Yes (3)
2224 * N/A equal !larger N/A | No (4)
2225 * larger larger N/A N/A | No (5)
2226 * larger smaller N/A N/A | Yes (6)
2227 * smaller larger N/A N/A | Yes (7)
2228 * smaller smaller N/A N/A | No (8)
2229 * equal smaller N/A N/A | Yes (9)
2230 * equal larger (already done at A above)
2232 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2233 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2235 if (bscopecmp == 0) {
2236 struct ifnet *bifp = ifa_best->ia_ifp;
2238 if (bifp == oifp && ifp != oifp) /* (1) */
2239 continue;
2240 if (bifp != oifp && ifp == oifp) /* (2) */
2241 goto replace;
2244 * Both bifp and ifp are on the outgoing
2245 * interface, or both two are on a different
2246 * interface from the outgoing I/F.
2247 * now we need address matching against dst
2248 * for tiebreaking.
2250 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2251 matchcmp = tlen - blen;
2252 if (matchcmp > 0) /* (3) */
2253 goto replace;
2254 continue; /* (4) */
2256 if (dscopecmp > 0) {
2257 if (bscopecmp > 0) /* (5) */
2258 continue;
2259 goto replace; /* (6) */
2261 if (dscopecmp < 0) {
2262 if (bscopecmp > 0) /* (7) */
2263 goto replace;
2264 continue; /* (8) */
2267 /* now dscopecmp must be 0 */
2268 if (bscopecmp < 0)
2269 goto replace; /* (9) */
2271 replace:
2272 ifa_best = (struct in6_ifaddr *)ifa;
2273 blen = tlen >= 0 ? tlen :
2274 in6_matchlen(IFA_IN6(ifa), dst);
2275 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2279 /* count statistics for future improvements */
2280 if (ifa_best == NULL)
2281 ip6stat.ip6s_sources_none++;
2282 else {
2283 if (oifp == ifa_best->ia_ifp)
2284 ip6stat.ip6s_sources_sameif[best_scope]++;
2285 else
2286 ip6stat.ip6s_sources_otherif[best_scope]++;
2288 if (best_scope == dst_scope)
2289 ip6stat.ip6s_sources_samescope[best_scope]++;
2290 else
2291 ip6stat.ip6s_sources_otherscope[best_scope]++;
2293 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2294 ip6stat.ip6s_sources_deprecated[best_scope]++;
2297 return (ifa_best);
2301 * return the best address out of the same scope. if no address was
2302 * found, return the first valid address from designated IF.
2304 struct in6_ifaddr *
2305 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2307 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2308 struct ifaddr_container *ifac;
2309 struct in6_ifaddr *besta = NULL;
2310 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2312 dep[0] = dep[1] = NULL;
2315 * We first look for addresses in the same scope.
2316 * If there is one, return it.
2317 * If two or more, return one which matches the dst longest.
2318 * If none, return one of global addresses assigned other ifs.
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[0] = (struct in6_ifaddr *)ifa;
2334 continue;
2337 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2339 * call in6_matchlen() as few as possible
2341 if (besta) {
2342 if (blen == -1)
2343 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2344 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2345 if (tlen > blen) {
2346 blen = tlen;
2347 besta = (struct in6_ifaddr *)ifa;
2349 } else
2350 besta = (struct in6_ifaddr *)ifa;
2353 if (besta)
2354 return (besta);
2356 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2357 struct ifaddr *ifa = ifac->ifa;
2359 if (ifa->ifa_addr->sa_family != AF_INET6)
2360 continue;
2361 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2362 continue; /* XXX: is there any case to allow anycast? */
2363 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2364 continue; /* don't use this interface */
2365 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2366 continue;
2367 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2368 if (ip6_use_deprecated)
2369 dep[1] = (struct in6_ifaddr *)ifa;
2370 continue;
2373 return (struct in6_ifaddr *)ifa;
2376 /* use the last-resort values, that are, deprecated addresses */
2377 if (dep[0])
2378 return dep[0];
2379 if (dep[1])
2380 return dep[1];
2382 return NULL;
2386 * perform DAD when interface becomes IFF_UP.
2388 static void
2389 in6_if_up_dispatch(netmsg_t nmsg)
2391 struct lwkt_msg *lmsg = &nmsg->lmsg;
2392 struct ifnet *ifp = lmsg->u.ms_resultp;
2393 struct ifaddr_container *ifac;
2394 struct in6_ifaddr *ia;
2395 int dad_delay; /* delay ticks before DAD output */
2397 ASSERT_IN_NETISR(0);
2400 * special cases, like 6to4, are handled in in6_ifattach
2402 in6_ifattach(ifp, NULL);
2404 dad_delay = 0;
2405 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2406 struct ifaddr *ifa = ifac->ifa;
2408 if (ifa->ifa_addr->sa_family != AF_INET6)
2409 continue;
2410 ia = (struct in6_ifaddr *)ifa;
2411 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2412 nd6_dad_start(ifa, &dad_delay);
2415 lwkt_replymsg(lmsg, 0);
2418 void
2419 in6_if_up(struct ifnet *ifp)
2421 struct netmsg_base nmsg;
2422 struct lwkt_msg *lmsg = &nmsg.lmsg;
2424 ASSERT_CANDOMSG_NETISR0(curthread);
2426 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0, in6_if_up_dispatch);
2427 lmsg->u.ms_resultp = ifp;
2428 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
2432 in6if_do_dad(struct ifnet *ifp)
2434 if (ifp->if_flags & IFF_LOOPBACK)
2435 return (0);
2437 switch (ifp->if_type) {
2438 #ifdef IFT_DUMMY
2439 case IFT_DUMMY:
2440 #endif
2441 case IFT_FAITH:
2443 * These interfaces do not have the IFF_LOOPBACK flag,
2444 * but loop packets back. We do not have to do DAD on such
2445 * interfaces. We should even omit it, because loop-backed
2446 * NS would confuse the DAD procedure.
2448 return (0);
2449 default:
2451 * Our DAD routine requires the interface up and running.
2452 * However, some interfaces can be up before the RUNNING
2453 * status. Additionaly, users may try to assign addresses
2454 * before the interface becomes up (or running).
2455 * We simply skip DAD in such a case as a work around.
2456 * XXX: we should rather mark "tentative" on such addresses,
2457 * and do DAD after the interface becomes ready.
2459 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2460 (IFF_UP|IFF_RUNNING))
2461 return (0);
2463 return (1);
2468 * Calculate max IPv6 MTU through all the interfaces and store it
2469 * to in6_maxmtu.
2471 void
2472 in6_setmaxmtu(void)
2474 unsigned long maxmtu = 0;
2475 const struct ifnet_array *arr;
2476 int i;
2478 ASSERT_IN_NETISR(0);
2480 arr = ifnet_array_get();
2481 for (i = 0; i < arr->ifnet_count; ++i) {
2482 struct ifnet *ifp = arr->ifnet_arr[i];
2484 /* this function can be called during ifnet initialization */
2485 if (ifp->if_afdata[AF_INET6] == NULL)
2486 continue;
2487 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2488 IN6_LINKMTU(ifp) > maxmtu)
2489 maxmtu = IN6_LINKMTU(ifp);
2491 if (maxmtu) /* update only when maxmtu is positive */
2492 in6_maxmtu = maxmtu;
2495 void *
2496 in6_domifattach(struct ifnet *ifp)
2498 struct in6_ifextra *ext;
2500 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2501 bzero(ext, sizeof(*ext));
2503 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2504 M_IFADDR, M_WAITOK);
2505 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2507 ext->icmp6_ifstat =
2508 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2509 M_IFADDR, M_WAITOK);
2510 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2512 ext->nd_ifinfo = nd6_ifattach(ifp);
2513 ext->scope6_id = scope6_ifattach(ifp);
2514 return ext;
2517 void
2518 in6_domifdetach(struct ifnet *ifp, void *aux)
2520 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2521 scope6_ifdetach(ext->scope6_id);
2522 nd6_ifdetach(ext->nd_ifinfo);
2523 kfree(ext->in6_ifstat, M_IFADDR);
2524 kfree(ext->icmp6_ifstat, M_IFADDR);
2525 kfree(ext, M_IFADDR);