inet6: require RTF_ANNOUNCE to proxy NS
[dragonfly.git] / sys / netinet6 / in6.c
blob1e5727722bda360cc4d87f40d5a5f9497e1f6865
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/caps.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 */
145 * Subroutine for in6_ifaddloop() and in6_ifremloop().
146 * This routine does actual work.
148 static void
149 in6_ifloop_request(int cmd, struct ifaddr *ifa,
150 void (*callback)(int, int, struct rt_addrinfo *, struct rtentry *, void *))
152 struct sockaddr_in6 all1_sa;
153 struct rt_addrinfo rtinfo;
154 int error;
156 bzero(&all1_sa, sizeof(all1_sa));
157 all1_sa.sin6_family = AF_INET6;
158 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
159 all1_sa.sin6_addr = in6mask128;
162 * We specify the address itself as the gateway, and set the
163 * RTF_LLINFO flag, so that the corresponding host route would have
164 * the flag, and thus applications that assume traditional behavior
165 * would be happy. Note that we assume the caller of the function
166 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
167 * which changes the outgoing interface to the loopback interface.
169 bzero(&rtinfo, sizeof(struct rt_addrinfo));
170 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
171 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
172 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
173 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
175 error = rtrequest1_global(cmd, &rtinfo, callback, ifa, RTREQ_PRIO_NORM);
176 if (error != 0) {
177 log(LOG_ERR, "in6_ifloop_request: "
178 "%s operation failed for %s (errno=%d)\n",
179 cmd == RTM_ADD ? "ADD" : cmd == RTM_DELETE ? "DELETE" : "GET",
180 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
181 error);
185 static void
186 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
187 struct rtentry *rt, void *arg)
189 struct ifaddr *ifa = arg;
191 if (error)
192 goto done;
195 * Make sure rt_ifa be equal to IFA, the second argument of the
196 * function.
197 * We need this because when we refer to rt_ifa->ia6_flags in
198 * ip6_input, we assume that the rt_ifa points to the address instead
199 * of the loopback address.
201 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
202 ++rt->rt_refcnt;
203 IFAFREE(rt->rt_ifa);
204 IFAREF(ifa);
205 rt->rt_ifa = ifa;
206 --rt->rt_refcnt;
210 * Report the addition/removal of the address to the routing socket,
211 * unless the address is marked as tentative, where it will be reported
212 * once DAD completes.
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 struct in6_ifaddr *ia6 = (struct in6_ifaddr *)ifa;
221 if (cmd != RTM_ADD ||
222 !(ia6->ia6_flags & IN6_IFF_TENTATIVE))
223 rt_newaddrmsg(cmd, ifa, error, rt);
225 if (cmd == RTM_DELETE) {
226 if (rt->rt_refcnt == 0) {
227 ++rt->rt_refcnt;
228 rtfree(rt);
232 done:
233 /* no way to return any new error */
237 static void
238 in6_newaddrmsg_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
239 struct rtentry *rt, void *arg)
241 struct ifaddr *ifa = arg;
243 if (error == 0 && rt != NULL && mycpuid == 0)
244 rt_newaddrmsg(RTM_ADD, ifa, error, rt);
247 void
248 in6_newaddrmsg(struct ifaddr *ifa)
250 in6_ifloop_request(RTM_GET, ifa, in6_newaddrmsg_callback);
254 * Add ownaddr as loopback rtentry. We previously add the route only if
255 * necessary (ex. on a p2p link). However, since we now manage addresses
256 * separately from prefixes, we should always add the route. We can't
257 * rely on the cloning mechanism from the corresponding interface route
258 * any more.
260 void
261 in6_ifaddloop(struct ifaddr *ifa)
263 struct rtentry *rt;
265 /* If there is no loopback entry, allocate one. */
266 rt = rtpurelookup(ifa->ifa_addr);
267 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
268 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
269 in6_ifloop_request(RTM_ADD, ifa, in6_ifloop_request_callback);
270 if (rt != NULL)
271 rt->rt_refcnt--;
275 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
276 * if it exists.
278 void
279 in6_ifremloop(struct ifaddr *ifa)
281 struct in6_ifaddr *ia;
282 struct rtentry *rt;
283 int ia_count = 0;
286 * Some of BSD variants do not remove cloned routes
287 * from an interface direct route, when removing the direct route
288 * (see comments in net/net_osdep.h). Even for variants that do remove
289 * cloned routes, they could fail to remove the cloned routes when
290 * we handle multple addresses that share a common prefix.
291 * So, we should remove the route corresponding to the deleted address
292 * regardless of the result of in6_is_ifloop_auto().
296 * Delete the entry only if exact one ifa exists. More than one ifa
297 * can exist if we assign a same single address to multiple
298 * (probably p2p) interfaces.
299 * XXX: we should avoid such a configuration in IPv6...
301 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
302 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
303 ia_count++;
304 if (ia_count > 1)
305 break;
309 if (ia_count == 1) {
311 * Before deleting, check if a corresponding loopbacked host
312 * route surely exists. With this check, we can avoid to
313 * delete an interface direct route whose destination is same
314 * as the address being removed. This can happen when remofing
315 * a subnet-router anycast address on an interface attahced
316 * to a shared medium.
318 rt = rtpurelookup(ifa->ifa_addr);
319 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
320 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
321 rt->rt_refcnt--;
322 in6_ifloop_request(RTM_DELETE, ifa,
323 in6_ifloop_request_callback);
329 in6_mask2len(const struct in6_addr *mask, const u_char *lim0)
331 int x = 0, y;
332 const u_char *lim = lim0, *p;
334 if (lim0 == NULL ||
335 lim0 - (const u_char *)mask > sizeof(*mask)) {
336 /* Ignore the scope_id part */
337 lim = (const u_char *)mask + sizeof(*mask);
339 for (p = (const u_char *)mask; p < lim; x++, p++) {
340 if (*p != 0xff)
341 break;
343 y = 0;
344 if (p < lim) {
345 for (y = 0; y < 8; y++) {
346 if ((*p & (0x80 >> y)) == 0)
347 break;
352 * When the limit pointer is given, do a stricter check on the
353 * remaining bits.
355 if (p < lim) {
356 if (y != 0 && (*p & (0x00ff >> y)) != 0)
357 return (-1);
358 for (p = p + 1; p < lim; p++)
359 if (*p != 0)
360 return (-1);
363 return x * 8 + y;
366 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
367 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
369 void
370 in6_control_dispatch(netmsg_t msg)
372 int error;
374 error = in6_control(msg->control.nm_cmd,
375 msg->control.nm_data,
376 msg->control.nm_ifp,
377 msg->control.nm_td);
378 lwkt_replymsg(&msg->control.base.lmsg, error);
382 in6_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
384 struct netmsg_pru_control msg;
386 switch (cmd) {
387 case SIOCSIFPREFIX_IN6:
388 case SIOCDIFPREFIX_IN6:
389 case SIOCAIFPREFIX_IN6:
390 case SIOCCIFPREFIX_IN6:
391 case SIOCSGIFPREFIX_IN6:
392 case SIOCGIFPREFIX_IN6:
393 log(LOG_NOTICE, "prefix ioctls are now invalidated. "
394 "please use ifconfig.\n");
395 return (EOPNOTSUPP);
397 case SIOCSIFADDR_IN6:
398 case SIOCSIFDSTADDR_IN6:
399 case SIOCSIFNETMASK_IN6:
401 * Since IPv6 allows a node to assign multiple addresses
402 * on a single interface, SIOCSIFxxx ioctls are not suitable
403 * and should be unused.
405 /* We decided to obsolete this command (20000704) */
406 return (EINVAL);
408 case SIOCSIFADDR:
409 case SIOCSIFDSTADDR:
410 case SIOCSIFBRDADDR:
411 case SIOCSIFNETMASK:
413 * Do not pass those ioctl to driver handler since they are not
414 * properly setup. Instead just error out.
416 return (EOPNOTSUPP);
418 /* mroute */
419 case SIOCGETSGCNT_IN6:
420 case SIOCGETMIFCNT_IN6:
421 /* srcsel policy */
422 case SIOCAADDRCTL_POLICY:
423 case SIOCDADDRCTL_POLICY:
424 /* nd6 */
425 case SIOCSNDFLUSH_IN6:
426 case SIOCSPFXFLUSH_IN6:
427 case SIOCSRTRFLUSH_IN6:
428 case SIOCSDEFIFACE_IN6:
429 case SIOCSIFINFO_FLAGS:
430 case SIOCSIFINFO_IN6:
431 case OSIOCGIFINFO_IN6:
432 case SIOCGIFINFO_IN6:
433 case SIOCGDRLST_IN6:
434 case SIOCGPRLST_IN6:
435 case SIOCGNBRINFO_IN6:
436 case SIOCGDEFIFACE_IN6:
437 /* scope6 */
438 case SIOCSSCOPE6:
439 case SIOCGSCOPE6:
440 case SIOCGSCOPE6DEF:
441 /* change address */
442 case SIOCALIFADDR:
443 case SIOCDLIFADDR:
444 case SIOCSIFALIFETIME_IN6:
445 case SIOCAIFADDR_IN6:
446 case SIOCDIFADDR_IN6:
448 * Dispatch these SIOCs to netisr0.
450 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
451 in6_control_internal_dispatch);
452 msg.nm_cmd = cmd;
453 msg.nm_data = data;
454 msg.nm_ifp = ifp;
455 msg.nm_td = td;
456 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
457 return msg.base.lmsg.ms_error;
459 default:
460 return in6_control_internal(cmd, data, ifp, td);
464 static void
465 in6_control_internal_dispatch(netmsg_t msg)
467 int error;
469 error = in6_control_internal(msg->control.nm_cmd, msg->control.nm_data,
470 msg->control.nm_ifp, msg->control.nm_td);
471 lwkt_replymsg(&msg->lmsg, error);
474 static int
475 in6_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
476 struct thread *td)
478 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
479 struct in6_ifaddr *ia = NULL;
480 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
481 struct in6_ifextra *xtra;
482 boolean_t privileged;
483 int error;
485 privileged = FALSE;
486 if (caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT) == 0)
487 privileged = TRUE;
489 switch (cmd) {
490 case SIOCALIFADDR:
491 case SIOCDLIFADDR:
492 if (!privileged)
493 return (EPERM);
494 /* FALLTHROUGH */
495 case SIOCGLIFADDR:
496 if (ifp == NULL)
497 return (EOPNOTSUPP);
498 return in6_lifaddr_ioctl(cmd, data, ifp, td);
501 switch (cmd) {
502 case SIOCGETSGCNT_IN6:
503 case SIOCGETMIFCNT_IN6:
504 return (mrt6_ioctl(cmd, data));
507 switch(cmd) {
508 case SIOCAADDRCTL_POLICY:
509 case SIOCDADDRCTL_POLICY:
510 if (!privileged)
511 return (EPERM);
512 return (in6_src_ioctl(cmd, data));
515 if (ifp == NULL)
516 return (EOPNOTSUPP);
518 switch (cmd) {
519 case SIOCSNDFLUSH_IN6:
520 case SIOCSPFXFLUSH_IN6:
521 case SIOCSRTRFLUSH_IN6:
522 case SIOCSDEFIFACE_IN6:
523 case SIOCSIFINFO_FLAGS:
524 case SIOCSIFINFO_IN6:
525 if (!privileged)
526 return (EPERM);
527 /* FALLTHROUGH */
528 case OSIOCGIFINFO_IN6:
529 case SIOCGIFINFO_IN6:
530 case SIOCGDRLST_IN6:
531 case SIOCGPRLST_IN6:
532 case SIOCGNBRINFO_IN6:
533 case SIOCGDEFIFACE_IN6:
534 return (nd6_ioctl(cmd, data, ifp));
537 switch (cmd) {
538 case SIOCSSCOPE6:
539 if (!privileged)
540 return (EPERM);
541 return (scope6_set(ifp,
542 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
544 case SIOCGSCOPE6:
545 return (scope6_get(ifp,
546 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
548 case SIOCGSCOPE6DEF:
549 return (scope6_get_default((struct scope6_id *)
550 ifr->ifr_ifru.ifru_scope_id));
554 * Find address for this interface, if it exists.
556 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
557 struct sockaddr_in6 *sa6 =
558 (struct sockaddr_in6 *)&ifra->ifra_addr;
560 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
561 if (sa6->sin6_addr.s6_addr16[1] == 0) {
562 /* Link ID is not embedded by the user */
563 sa6->sin6_addr.s6_addr16[1] =
564 htons(ifp->if_index);
565 } else if (sa6->sin6_addr.s6_addr16[1] !=
566 htons(ifp->if_index)) {
567 /* Link ID contradicts */
568 return (EINVAL);
570 if (sa6->sin6_scope_id) {
571 if (sa6->sin6_scope_id !=
572 (u_int32_t)ifp->if_index)
573 return (EINVAL);
574 sa6->sin6_scope_id = 0; /* XXX: good way? */
577 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
580 switch (cmd) {
581 case SIOCDIFADDR_IN6:
583 * For IPv4, we look for existing in_ifaddr here to allow
584 * "ifconfig if0 delete" to remove first IPv4 address on the
585 * interface. For IPv6, as the spec allow multiple interface
586 * address from the day one, we consider "remove the first one"
587 * semantics to be not preferable.
589 if (ia == NULL)
590 return (EADDRNOTAVAIL);
591 /* FALLTHROUGH */
592 case SIOCAIFADDR_IN6:
594 * We always require users to specify a valid IPv6 address for
595 * the corresponding operation.
597 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
598 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
599 return (EAFNOSUPPORT);
600 if (!privileged)
601 return (EPERM);
602 break;
604 case SIOCGIFADDR_IN6:
605 /* This interface is basically deprecated. Use SIOCGIFCONF. */
606 /* FALLTHROUGH */
607 case SIOCGIFAFLAG_IN6:
608 case SIOCGIFNETMASK_IN6:
609 case SIOCGIFDSTADDR_IN6:
610 case SIOCGIFALIFETIME_IN6:
611 /* Must think again about its semantics */
612 if (ia == NULL)
613 return (EADDRNOTAVAIL);
614 break;
616 case SIOCSIFALIFETIME_IN6:
618 const struct in6_addrlifetime *lt;
620 if (!privileged)
621 return (EPERM);
622 if (ia == NULL)
623 return (EADDRNOTAVAIL);
624 /* Sanity for overflow - beware unsigned */
625 lt = &ifr->ifr_ifru.ifru_lifetime;
626 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
627 lt->ia6t_vltime + time_uptime < time_uptime)
628 return EINVAL;
629 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
630 lt->ia6t_pltime + time_uptime < time_uptime)
631 return EINVAL;
632 break;
636 switch (cmd) {
637 case SIOCGIFADDR_IN6:
638 ifr->ifr_addr = ia->ia_addr;
639 break;
641 case SIOCGIFDSTADDR_IN6:
642 if (!(ifp->if_flags & IFF_POINTOPOINT))
643 return (EINVAL);
645 * XXX: Should we check if ifa_dstaddr is NULL and return
646 * an error?
648 ifr->ifr_dstaddr = ia->ia_dstaddr;
649 break;
651 case SIOCGIFNETMASK_IN6:
652 ifr->ifr_addr = ia->ia_prefixmask;
653 break;
655 case SIOCGIFAFLAG_IN6:
656 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
657 break;
659 case SIOCGIFSTAT_IN6:
660 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
661 return EINVAL;
662 bzero(&ifr->ifr_ifru.ifru_stat,
663 sizeof(ifr->ifr_ifru.ifru_stat));
664 ifr->ifr_ifru.ifru_stat = *xtra->in6_ifstat;
665 break;
667 case SIOCGIFSTAT_ICMP6:
668 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
669 return EINVAL;
670 bzero(&ifr->ifr_ifru.ifru_stat,
671 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
672 ifr->ifr_ifru.ifru_icmp6stat = *xtra->icmp6_ifstat;
673 break;
675 case SIOCGIFALIFETIME_IN6:
676 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
677 break;
679 case SIOCSIFALIFETIME_IN6:
680 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
681 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
682 ia->ia6_lifetime.ia6t_expire =
683 time_uptime + ia->ia6_lifetime.ia6t_vltime;
684 } else {
685 ia->ia6_lifetime.ia6t_expire = 0;
687 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
688 ia->ia6_lifetime.ia6t_preferred =
689 time_uptime + ia->ia6_lifetime.ia6t_pltime;
690 } else {
691 ia->ia6_lifetime.ia6t_preferred = 0;
693 break;
695 case SIOCAIFADDR_IN6:
697 int i, error = 0, iaIsNew;
698 struct nd_prefix pr0, *pr;
700 if (ia != NULL)
701 iaIsNew = 0;
702 else
703 iaIsNew = 1;
706 * First, make or update the interface address structure,
707 * and link it to the list.
709 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
710 return (error);
713 * Then, make the prefix on-link on the interface.
714 * XXX: We'd rather create the prefix before the address, but
715 * we need at least one address to install the corresponding
716 * interface route, so we configure the address first.
720 * Convert mask to prefix length (prefixmask has already
721 * been validated in in6_update_ifa().
723 bzero(&pr0, sizeof(pr0));
724 pr0.ndpr_ifp = ifp;
725 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
726 NULL);
727 if (pr0.ndpr_plen == 128)
728 break; /* no need to install a host route. */
729 pr0.ndpr_prefix = ifra->ifra_addr;
730 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
731 /* Apply the mask for safety. */
732 for (i = 0; i < 4; i++) {
733 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
734 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
737 * XXX: Since we don't have an API to set prefix (not address)
738 * lifetimes, we just use the same lifetimes as addresses.
739 * The (temporarily) installed lifetimes can be overridden by
740 * later advertised RAs (when accept_rtadv is non 0), which is
741 * an intended behavior.
743 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
744 pr0.ndpr_raf_auto =
745 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
746 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
747 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
749 /* Add the prefix if there's one. */
750 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
752 * nd6_prelist_add will install the corresponding
753 * interface route.
755 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
756 return (error);
757 if (pr == NULL) {
758 log(LOG_ERR, "nd6_prelist_add succeeded but "
759 "no prefix\n");
760 return (EINVAL); /* XXX panic here? */
764 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
765 if (ia == NULL) {
766 /* XXX: This should not happen! */
767 log(LOG_ERR, "in6_control: addition succeeded, but"
768 " no ifaddr\n");
769 } else {
770 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
771 ia->ia6_ndpr == NULL) {
773 * New autoconf address
775 ia->ia6_ndpr = pr;
776 pr->ndpr_refcnt++;
779 * If this is the first autoconf address from
780 * the prefix, create a temporary address
781 * as well (when specified).
783 if (ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
784 int e;
786 if ((e = in6_tmpifadd(ia, 1)) != 0) {
787 log(LOG_NOTICE, "in6_control: "
788 "failed to create a "
789 "temporary address, "
790 "errno=%d\n", e);
796 * This might affect the status of autoconfigured
797 * addresses, that is, this address might make
798 * other addresses detached.
800 pfxlist_onlink_check();
802 if (error == 0 && ia) {
803 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
804 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
805 &ia->ia_ifa);
807 break;
810 case SIOCDIFADDR_IN6:
812 int i = 0;
813 struct nd_prefix pr0, *pr;
816 * If the address being deleted is the only one that owns
817 * the corresponding prefix, expire the prefix as well.
818 * XXX: Theoretically, we don't have to warry about such
819 * relationship, since we separate the address management
820 * and the prefix management. We do this, however, to provide
821 * as much backward compatibility as possible in terms of
822 * the ioctl operation.
824 bzero(&pr0, sizeof(pr0));
825 pr0.ndpr_ifp = ifp;
826 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
827 NULL);
828 if (pr0.ndpr_plen == 128)
829 goto purgeaddr;
830 pr0.ndpr_prefix = ia->ia_addr;
831 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
832 for (i = 0; i < 4; i++) {
833 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
834 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
837 * The logic of the following condition is a bit complicated.
838 * We expire the prefix when
839 * 1. The address obeys autoconfiguration and it is the
840 * only owner of the associated prefix, or
841 * 2. The address does not obey autoconf and there is no
842 * other owner of the prefix.
844 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
845 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
846 pr->ndpr_refcnt == 1) ||
847 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
848 pr->ndpr_refcnt == 0)))
849 pr->ndpr_expire = 1; /* XXX: just for expiration */
851 purgeaddr:
852 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
853 &ia->ia_ifa);
854 in6_purgeaddr(&ia->ia_ifa);
855 break;
858 default:
859 if (ifp->if_ioctl == NULL)
860 return (EOPNOTSUPP);
861 ifnet_serialize_all(ifp);
862 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
863 ifnet_deserialize_all(ifp);
864 return (error);
867 return (0);
871 * Update parameters of an IPv6 interface address.
872 * If necessary, a new entry is created and linked into address chains.
873 * This function is separated from in6_control().
874 * XXX: should this be performed under splnet()?
877 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
878 struct in6_ifaddr *ia)
880 int error = 0, hostIsNew = 0, was_tentative, plen = -1;
881 struct in6_ifaddr *oia;
882 struct sockaddr_in6 dst6;
883 struct in6_addrlifetime *lt;
885 /* Validate parameters */
886 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
887 return (EINVAL);
890 * The destination address for a p2p link must have a family
891 * of AF_UNSPEC or AF_INET6.
893 if ((ifp->if_flags & IFF_POINTOPOINT) &&
894 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
895 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
896 return (EAFNOSUPPORT);
898 * validate ifra_prefixmask. don't check sin6_family, netmask
899 * does not carry fields other than sin6_len.
901 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
902 return (EINVAL);
904 * Because the IPv6 address architecture is classless, we require
905 * users to specify a (non 0) prefix length (mask) for a new address.
906 * We also require the prefix (when specified) mask is valid, and thus
907 * reject a non-consecutive mask.
909 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
910 return (EINVAL);
911 if (ifra->ifra_prefixmask.sin6_len != 0) {
912 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
913 (u_char *)&ifra->ifra_prefixmask +
914 ifra->ifra_prefixmask.sin6_len);
915 if (plen <= 0)
916 return (EINVAL);
918 else {
920 * In this case, ia must not be NULL. We just use its prefix
921 * length.
923 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
926 * If the destination address on a p2p interface is specified,
927 * and the address is a scoped one, validate/set the scope
928 * zone identifier.
930 dst6 = ifra->ifra_dstaddr;
931 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
932 (dst6.sin6_family == AF_INET6)) {
933 int scopeid;
935 if ((error = in6_recoverscope(&dst6,
936 &ifra->ifra_dstaddr.sin6_addr,
937 ifp)) != 0)
938 return (error);
939 if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &scopeid))
940 return (EINVAL);
941 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
942 dst6.sin6_scope_id = scopeid;
943 else if (dst6.sin6_scope_id != scopeid)
944 return (EINVAL); /* scope ID mismatch. */
945 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
946 != 0)
947 return (error);
948 dst6.sin6_scope_id = 0; /* XXX */
951 * The destination address can be specified only for a p2p or a
952 * loopback interface. If specified, the corresponding prefix length
953 * must be 128.
955 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
956 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
957 /* XXX: noisy message */
958 log(LOG_INFO, "in6_update_ifa: a destination can be "
959 "specified for a p2p or a loopback IF only\n");
960 return (EINVAL);
962 if (plen != 128) {
964 * The following message seems noisy, but we dare to
965 * add it for diagnosis.
967 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
968 "when dstaddr is specified\n");
969 return (EINVAL);
972 /* lifetime consistency check */
973 lt = &ifra->ifra_lifetime;
974 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
975 && lt->ia6t_vltime + time_uptime < time_uptime) {
976 return EINVAL;
978 if (lt->ia6t_vltime == 0) {
980 * the following log might be noisy, but this is a typical
981 * configuration mistake or a tool's bug.
983 log(LOG_INFO,
984 "in6_update_ifa: valid lifetime is 0 for %s\n",
985 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
987 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
988 && lt->ia6t_pltime + time_uptime < time_uptime) {
989 return EINVAL;
993 * If this is a new address, allocate a new ifaddr and link it
994 * into chains.
996 if (ia == NULL) {
997 hostIsNew = 1;
998 ia = ifa_create(sizeof(*ia));
1000 /* Initialize the address and masks */
1001 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1002 ia->ia_addr.sin6_family = AF_INET6;
1003 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1004 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1006 * XXX: some functions expect that ifa_dstaddr is not
1007 * NULL for p2p interfaces.
1009 ia->ia_ifa.ifa_dstaddr
1010 = (struct sockaddr *)&ia->ia_dstaddr;
1011 } else {
1012 ia->ia_ifa.ifa_dstaddr = NULL;
1014 ia->ia_ifa.ifa_netmask
1015 = (struct sockaddr *)&ia->ia_prefixmask;
1017 ia->ia_ifp = ifp;
1018 if ((oia = in6_ifaddr) != NULL) {
1019 for ( ; oia->ia_next; oia = oia->ia_next)
1020 continue;
1021 oia->ia_next = ia;
1022 } else
1023 in6_ifaddr = ia;
1025 ifa_iflink(&ia->ia_ifa, ifp, 1);
1028 /* set prefix mask */
1029 if (ifra->ifra_prefixmask.sin6_len) {
1031 * We prohibit changing the prefix length of an existing
1032 * address, because
1033 * + such an operation should be rare in IPv6, and
1034 * + the operation would confuse prefix management.
1036 if (ia->ia_prefixmask.sin6_len &&
1037 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1038 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
1039 " existing (%s) address should not be changed\n",
1040 ip6_sprintf(&ia->ia_addr.sin6_addr));
1041 error = EINVAL;
1042 goto unlink;
1044 ia->ia_prefixmask = ifra->ifra_prefixmask;
1048 * If a new destination address is specified, scrub the old one and
1049 * install the new destination. Note that the interface must be
1050 * p2p or loopback (see the check above.)
1052 if (dst6.sin6_family == AF_INET6 &&
1053 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
1054 &ia->ia_dstaddr.sin6_addr)) {
1055 int e;
1057 if ((ia->ia_flags & IFA_ROUTE) &&
1058 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1059 != 0) {
1060 log(LOG_ERR, "in6_update_ifa: failed to remove "
1061 "a route to the old destination: %s\n",
1062 ip6_sprintf(&ia->ia_addr.sin6_addr));
1063 /* proceed anyway... */
1065 else
1066 ia->ia_flags &= ~IFA_ROUTE;
1067 ia->ia_dstaddr = dst6;
1070 was_tentative = ia->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED);
1071 ia->ia6_flags = ifra->ifra_flags;
1072 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1073 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1074 if ((hostIsNew || was_tentative) &&
1075 in6if_do_dad(ifp) &&
1076 !(ifra->ifra_flags & IN6_IFF_NODAD))
1077 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1079 ia->ia6_lifetime = ifra->ifra_lifetime;
1080 /* for sanity */
1081 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1082 ia->ia6_lifetime.ia6t_expire =
1083 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1084 } else
1085 ia->ia6_lifetime.ia6t_expire = 0;
1086 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1087 ia->ia6_lifetime.ia6t_preferred =
1088 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1089 } else
1090 ia->ia6_lifetime.ia6t_preferred = 0;
1092 /* reset the interface and routing table appropriately. */
1093 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1094 goto unlink;
1097 * Beyond this point, we should call in6_purgeaddr upon an error,
1098 * not just go to unlink.
1101 if (ifp->if_flags & IFF_MULTICAST) {
1102 struct sockaddr_in6 mltaddr, mltmask;
1103 struct in6_multi *in6m;
1105 if (hostIsNew) {
1107 * join solicited multicast addr for new host id
1109 struct in6_addr llsol;
1110 bzero(&llsol, sizeof(struct in6_addr));
1111 llsol.s6_addr16[0] = htons(0xff02);
1112 llsol.s6_addr16[1] = htons(ifp->if_index);
1113 llsol.s6_addr32[1] = 0;
1114 llsol.s6_addr32[2] = htonl(1);
1115 llsol.s6_addr32[3] =
1116 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1117 llsol.s6_addr8[12] = 0xff;
1118 in6_addmulti(&llsol, ifp, &error);
1119 if (error != 0) {
1120 log(LOG_WARNING,
1121 "in6_update_ifa: addmulti failed for "
1122 "%s on %s (errno=%d)\n",
1123 ip6_sprintf(&llsol), if_name(ifp),
1124 error);
1125 in6_purgeaddr((struct ifaddr *)ia);
1126 return (error);
1130 bzero(&mltmask, sizeof(mltmask));
1131 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1132 mltmask.sin6_family = AF_INET6;
1133 mltmask.sin6_addr = in6mask32;
1136 * join link-local all-nodes address
1138 bzero(&mltaddr, sizeof(mltaddr));
1139 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1140 mltaddr.sin6_family = AF_INET6;
1141 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1142 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1144 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1145 if (in6m == NULL) {
1146 rtrequest_global(RTM_ADD,
1147 (struct sockaddr *)&mltaddr,
1148 (struct sockaddr *)&ia->ia_addr,
1149 (struct sockaddr *)&mltmask,
1150 RTF_UP|RTF_CLONING); /* xxx */
1151 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1152 if (error != 0) {
1153 log(LOG_WARNING,
1154 "in6_update_ifa: addmulti failed for "
1155 "%s on %s (errno=%d)\n",
1156 ip6_sprintf(&mltaddr.sin6_addr),
1157 if_name(ifp), error);
1162 * join node information group address
1164 if (in6_nigroup(ifp, hostname, strlen(hostname),
1165 &mltaddr.sin6_addr) == 0) {
1166 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1167 if (in6m == NULL && ia != NULL) {
1168 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1169 if (error != 0) {
1170 log(LOG_WARNING, "in6_update_ifa: "
1171 "addmulti failed for "
1172 "%s on %s (errno=%d)\n",
1173 ip6_sprintf(&mltaddr.sin6_addr),
1174 if_name(ifp), error);
1180 * join node-local all-nodes address, on loopback.
1181 * XXX: since "node-local" is obsoleted by interface-local,
1182 * we have to join the group on every interface with
1183 * some interface-boundary restriction.
1185 if (ifp->if_flags & IFF_LOOPBACK) {
1186 struct in6_ifaddr *ia_loop;
1188 struct in6_addr loop6 = kin6addr_loopback;
1189 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1191 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1193 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1194 if (in6m == NULL && ia_loop != NULL) {
1195 rtrequest_global(RTM_ADD,
1196 (struct sockaddr *)&mltaddr,
1197 (struct sockaddr *)&ia_loop->ia_addr,
1198 (struct sockaddr *)&mltmask,
1199 RTF_UP);
1200 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1201 if (error != 0) {
1202 log(LOG_WARNING, "in6_update_ifa: "
1203 "addmulti failed for %s on %s "
1204 "(errno=%d)\n",
1205 ip6_sprintf(&mltaddr.sin6_addr),
1206 if_name(ifp), error);
1213 * Perform DAD, if needed.
1214 * XXX It may be of use, if we can administratively
1215 * disable DAD.
1217 if (in6if_do_dad(ifp) &&
1218 !(ifra->ifra_flags & IN6_IFF_NODAD) &&
1219 ia->ia6_flags & IN6_IFF_TENTATIVE)
1220 nd6_dad_start((struct ifaddr *)ia, NULL);
1222 return (error);
1224 unlink:
1226 * XXX: if a change of an existing address failed, keep the entry
1227 * anyway.
1229 if (hostIsNew)
1230 in6_unlink_ifa(ia, ifp);
1231 return (error);
1234 void
1235 in6_purgeaddr(struct ifaddr *ifa)
1237 struct ifnet *ifp = ifa->ifa_ifp;
1238 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1240 /* stop DAD processing */
1241 nd6_dad_stop(ifa);
1244 * delete route to the destination of the address being purged.
1245 * The interface must be p2p or loopback in this case.
1247 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1248 int e;
1250 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1251 != 0) {
1252 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1253 "a route to the p2p destination: %s on %s, "
1254 "errno=%d\n",
1255 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1257 /* proceed anyway... */
1259 else
1260 ia->ia_flags &= ~IFA_ROUTE;
1263 /* Remove ownaddr's loopback rtentry, if it exists. */
1264 in6_ifremloop(&(ia->ia_ifa));
1266 if (ifp->if_flags & IFF_MULTICAST) {
1268 * delete solicited multicast addr for deleting host id
1270 struct in6_multi *in6m;
1271 struct in6_addr llsol;
1272 bzero(&llsol, sizeof(struct in6_addr));
1273 llsol.s6_addr16[0] = htons(0xff02);
1274 llsol.s6_addr16[1] = htons(ifp->if_index);
1275 llsol.s6_addr32[1] = 0;
1276 llsol.s6_addr32[2] = htonl(1);
1277 llsol.s6_addr32[3] =
1278 ia->ia_addr.sin6_addr.s6_addr32[3];
1279 llsol.s6_addr8[12] = 0xff;
1281 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1282 if (in6m)
1283 in6_delmulti(in6m);
1286 in6_unlink_ifa(ia, ifp);
1289 static void
1290 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1292 struct in6_ifaddr *oia;
1294 crit_enter();
1296 ifa_ifunlink(&ia->ia_ifa, ifp);
1298 oia = ia;
1299 if (oia == (ia = in6_ifaddr))
1300 in6_ifaddr = ia->ia_next;
1301 else {
1302 while (ia->ia_next && (ia->ia_next != oia))
1303 ia = ia->ia_next;
1304 if (ia->ia_next)
1305 ia->ia_next = oia->ia_next;
1306 else {
1307 /* search failed */
1308 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1313 * When an autoconfigured address is being removed, release the
1314 * reference to the base prefix. Also, since the release might
1315 * affect the status of other (detached) addresses, call
1316 * pfxlist_onlink_check().
1318 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1319 if (oia->ia6_ndpr == NULL) {
1320 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1321 "%p has no prefix\n", oia);
1322 } else {
1323 oia->ia6_ndpr->ndpr_refcnt--;
1324 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1325 oia->ia6_ndpr = NULL;
1328 pfxlist_onlink_check();
1332 * release another refcnt for the link from in6_ifaddr.
1333 * Note that we should decrement the refcnt at least once for all *BSD.
1335 ifa_destroy(&oia->ia_ifa);
1337 crit_exit();
1340 void
1341 in6_purgeif(struct ifnet *ifp)
1343 struct ifaddr_container *ifac, *next;
1345 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1346 ifa_link, next) {
1347 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1348 continue;
1349 in6_purgeaddr(ifac->ifa);
1352 in6_ifdetach(ifp);
1356 * SIOC[GAD]LIFADDR.
1357 * SIOCGLIFADDR: get first address. (?)
1358 * SIOCGLIFADDR with IFLR_PREFIX:
1359 * get first address that matches the specified prefix.
1360 * SIOCALIFADDR: add the specified address.
1361 * SIOCALIFADDR with IFLR_PREFIX:
1362 * add the specified prefix, filling hostid part from
1363 * the first link-local address. prefixlen must be <= 64.
1364 * SIOCDLIFADDR: delete the specified address.
1365 * SIOCDLIFADDR with IFLR_PREFIX:
1366 * delete the first address that matches the specified prefix.
1367 * return values:
1368 * EINVAL on invalid parameters
1369 * EADDRNOTAVAIL on prefix match failed/specified address not found
1370 * other values may be returned from in6_ioctl()
1372 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1373 * this is to accomodate address naming scheme other than RFC2374,
1374 * in the future.
1375 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1376 * address encoding scheme. (see figure on page 8)
1378 static int
1379 in6_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp,
1380 struct thread *td)
1382 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1383 struct sockaddr *sa;
1385 /* sanity checks */
1386 if (!data || !ifp) {
1387 panic("invalid argument to in6_lifaddr_ioctl");
1388 /*NOTRECHED*/
1391 switch (cmd) {
1392 case SIOCGLIFADDR:
1393 /* address must be specified on GET with IFLR_PREFIX */
1394 if (!(iflr->flags & IFLR_PREFIX))
1395 break;
1396 /* FALLTHROUGH */
1397 case SIOCALIFADDR:
1398 case SIOCDLIFADDR:
1399 /* address must be specified on ADD and DELETE */
1400 sa = (struct sockaddr *)&iflr->addr;
1401 if (sa->sa_family != AF_INET6)
1402 return EINVAL;
1403 if (sa->sa_len != sizeof(struct sockaddr_in6))
1404 return EINVAL;
1405 /* XXX need improvement */
1406 sa = (struct sockaddr *)&iflr->dstaddr;
1407 if (sa->sa_family && sa->sa_family != AF_INET6)
1408 return EINVAL;
1409 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1410 return EINVAL;
1411 break;
1412 default: /* shouldn't happen */
1413 #if 0
1414 panic("invalid cmd to in6_lifaddr_ioctl");
1415 /* NOTREACHED */
1416 #else
1417 return EOPNOTSUPP;
1418 #endif
1420 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1421 return EINVAL;
1423 switch (cmd) {
1424 case SIOCALIFADDR:
1426 struct in6_aliasreq ifra;
1427 struct in6_addr *hostid = NULL;
1428 int prefixlen;
1430 if (iflr->flags & IFLR_PREFIX) {
1431 struct ifaddr *ifa;
1432 struct sockaddr_in6 *sin6;
1435 * hostid is to fill in the hostid part of the
1436 * address. hostid points to the first link-local
1437 * address attached to the interface.
1439 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1440 if (!ifa)
1441 return EADDRNOTAVAIL;
1442 hostid = IFA_IN6(ifa);
1444 /* prefixlen must be <= 64. */
1445 if (64 < iflr->prefixlen)
1446 return EINVAL;
1447 prefixlen = iflr->prefixlen;
1449 /* hostid part must be zero. */
1450 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1451 if (sin6->sin6_addr.s6_addr32[2] != 0
1452 || sin6->sin6_addr.s6_addr32[3] != 0) {
1453 return EINVAL;
1455 } else
1456 prefixlen = iflr->prefixlen;
1458 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1459 bzero(&ifra, sizeof(ifra));
1460 bcopy(iflr->iflr_name, ifra.ifra_name,
1461 sizeof(ifra.ifra_name));
1463 bcopy(&iflr->addr, &ifra.ifra_addr,
1464 ((struct sockaddr *)&iflr->addr)->sa_len);
1465 if (hostid) {
1466 /* fill in hostid part */
1467 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1468 hostid->s6_addr32[2];
1469 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1470 hostid->s6_addr32[3];
1473 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1474 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1475 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1476 if (hostid) {
1477 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1478 hostid->s6_addr32[2];
1479 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1480 hostid->s6_addr32[3];
1484 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1485 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1487 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1488 return in6_control_internal(SIOCAIFADDR_IN6, (caddr_t)&ifra,
1489 ifp, td);
1491 case SIOCGLIFADDR:
1492 case SIOCDLIFADDR:
1494 struct ifaddr_container *ifac;
1495 struct in6_ifaddr *ia;
1496 struct in6_addr mask, candidate, match;
1497 struct sockaddr_in6 *sin6;
1498 int cmp;
1500 bzero(&mask, sizeof(mask));
1501 if (iflr->flags & IFLR_PREFIX) {
1502 /* lookup a prefix rather than address. */
1503 in6_prefixlen2mask(&mask, iflr->prefixlen);
1505 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1506 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1507 match.s6_addr32[0] &= mask.s6_addr32[0];
1508 match.s6_addr32[1] &= mask.s6_addr32[1];
1509 match.s6_addr32[2] &= mask.s6_addr32[2];
1510 match.s6_addr32[3] &= mask.s6_addr32[3];
1512 /* if you set extra bits, that's wrong */
1513 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1514 return EINVAL;
1516 cmp = 1;
1517 } else {
1518 if (cmd == SIOCGLIFADDR) {
1519 /* on getting an address, take the 1st match */
1520 cmp = 0; /* XXX */
1521 } else {
1522 /* on deleting an address, do exact match */
1523 in6_prefixlen2mask(&mask, 128);
1524 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1525 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1527 cmp = 1;
1531 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1532 struct ifaddr *ifa = ifac->ifa;
1534 if (ifa->ifa_addr->sa_family != AF_INET6)
1535 continue;
1536 if (!cmp)
1537 break;
1539 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1541 * XXX: this is adhoc, but is necessary to allow
1542 * a user to specify fe80::/64 (not /10) for a
1543 * link-local address.
1545 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1546 candidate.s6_addr16[1] = 0;
1547 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1548 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1549 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1550 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1551 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1552 break;
1554 if (ifac == NULL)
1555 return EADDRNOTAVAIL;
1556 ia = ifa2ia6(ifac->ifa);
1558 if (cmd == SIOCGLIFADDR) {
1559 struct sockaddr_in6 *s6;
1561 /* fill in the if_laddrreq structure */
1562 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1563 s6 = (struct sockaddr_in6 *)&iflr->addr;
1564 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1565 s6->sin6_addr.s6_addr16[1] = 0;
1566 if (in6_addr2zoneid(ifp, &s6->sin6_addr,
1567 &s6->sin6_scope_id))
1568 return (EINVAL);/* XXX */
1570 if (ifp->if_flags & IFF_POINTOPOINT) {
1571 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1572 ia->ia_dstaddr.sin6_len);
1573 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1574 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1575 s6->sin6_addr.s6_addr16[1] = 0;
1576 if (in6_addr2zoneid(ifp,
1577 &s6->sin6_addr, &s6->sin6_scope_id))
1578 return (EINVAL); /* EINVAL */
1580 } else
1581 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1583 iflr->prefixlen =
1584 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1585 NULL);
1587 iflr->flags = ia->ia6_flags; /* XXX */
1589 return 0;
1590 } else {
1591 struct in6_aliasreq ifra;
1593 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1594 bzero(&ifra, sizeof(ifra));
1595 bcopy(iflr->iflr_name, ifra.ifra_name,
1596 sizeof(ifra.ifra_name));
1598 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1599 ia->ia_addr.sin6_len);
1600 if (ifp->if_flags & IFF_POINTOPOINT)
1601 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1602 ia->ia_dstaddr.sin6_len);
1603 else
1604 bzero(&ifra.ifra_dstaddr,
1605 sizeof(ifra.ifra_dstaddr));
1606 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1607 ia->ia_prefixmask.sin6_len);
1609 ifra.ifra_flags = ia->ia6_flags;
1610 return in6_control_internal(SIOCDIFADDR_IN6,
1611 (caddr_t)&ifra, ifp, td);
1616 return EOPNOTSUPP; /* just for safety */
1620 * Initialize an interface's internet6 address
1621 * and routing table entry.
1623 static int
1624 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1625 int newhost)
1627 int error = 0, plen;
1629 ia->ia_addr = *sin6;
1631 if (ifp->if_ioctl != NULL) {
1632 ifnet_serialize_all(ifp);
1633 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
1634 ifnet_deserialize_all(ifp);
1635 if (error)
1636 return (error);
1639 ia->ia_ifa.ifa_metric = ifp->if_metric;
1641 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1644 * Special case:
1645 * If the destination address is specified for a point-to-point
1646 * interface, install a route to the destination as an interface
1647 * direct route.
1649 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1650 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1651 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1652 RTF_UP | RTF_HOST)) != 0)
1653 return (error);
1654 ia->ia_flags |= IFA_ROUTE;
1656 if (plen < 128) {
1658 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1660 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1663 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1664 if (newhost) {
1665 /* set the rtrequest function to create llinfo */
1666 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1667 in6_ifaddloop(&(ia->ia_ifa));
1670 return (error);
1673 struct in6_multi_mship *
1674 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1676 struct in6_multi_mship *imm;
1678 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1679 if (!imm) {
1680 *errorp = ENOBUFS;
1681 return NULL;
1683 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1684 if (!imm->i6mm_maddr) {
1685 /* *errorp is alrady set */
1686 kfree(imm, M_IPMADDR);
1687 return NULL;
1689 return imm;
1693 in6_leavegroup(struct in6_multi_mship *imm)
1696 if (imm->i6mm_maddr)
1697 in6_delmulti(imm->i6mm_maddr);
1698 kfree(imm, M_IPMADDR);
1699 return 0;
1703 * Add an address to the list of IP6 multicast addresses for a
1704 * given interface.
1706 struct in6_multi *
1707 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1709 struct in6_multi *in6m;
1710 struct sockaddr_in6 sin6;
1711 struct ifmultiaddr *ifma;
1713 *errorp = 0;
1715 crit_enter();
1718 * Call generic routine to add membership or increment
1719 * refcount. It wants addresses in the form of a sockaddr,
1720 * so we build one here (being careful to zero the unused bytes).
1722 bzero(&sin6, sizeof sin6);
1723 sin6.sin6_family = AF_INET6;
1724 sin6.sin6_len = sizeof sin6;
1725 sin6.sin6_addr = *maddr6;
1726 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1727 if (*errorp) {
1728 crit_exit();
1729 return 0;
1733 * If ifma->ifma_protospec is null, then if_addmulti() created
1734 * a new record. Otherwise, we are done.
1736 if (ifma->ifma_protospec != NULL) {
1737 crit_exit();
1738 return ifma->ifma_protospec;
1741 in6m = kmalloc(sizeof(*in6m), M_IPMADDR, M_INTWAIT | M_ZERO);
1742 in6m->in6m_addr = *maddr6;
1743 in6m->in6m_ifp = ifp;
1744 in6m->in6m_ifma = ifma;
1745 ifma->ifma_protospec = in6m;
1746 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1749 * Let MLD6 know that we have joined a new IP6 multicast
1750 * group.
1752 mld6_start_listening(in6m);
1753 crit_exit();
1754 return (in6m);
1758 * Delete a multicast address record.
1760 void
1761 in6_delmulti(struct in6_multi *in6m)
1763 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1765 crit_enter();
1767 if (ifma->ifma_refcount == 1) {
1769 * No remaining claims to this record; let MLD6 know
1770 * that we are leaving the multicast group.
1772 mld6_stop_listening(in6m);
1773 ifma->ifma_protospec = NULL;
1774 LIST_REMOVE(in6m, in6m_entry);
1775 kfree(in6m, M_IPMADDR);
1777 /* XXX - should be separate API for when we have an ifma? */
1778 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1779 crit_exit();
1783 * Find an IPv6 interface link-local address specific to an interface.
1785 struct in6_ifaddr *
1786 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1788 const struct ifaddr_container *ifac;
1790 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1791 struct ifaddr *ifa = ifac->ifa;
1793 if (ifa->ifa_addr == NULL)
1794 continue; /* just for safety */
1795 if (ifa->ifa_addr->sa_family != AF_INET6)
1796 continue;
1797 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1798 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1799 ignoreflags) != 0)
1800 continue;
1801 return (struct in6_ifaddr *)ifa;
1804 return NULL;
1809 * find the internet address corresponding to a given interface and address.
1811 struct in6_ifaddr *
1812 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1814 const struct ifaddr_container *ifac;
1816 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1817 struct ifaddr *ifa = ifac->ifa;
1819 if (ifa->ifa_addr == NULL)
1820 continue; /* just for safety */
1821 if (ifa->ifa_addr->sa_family != AF_INET6)
1822 continue;
1823 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1824 return (struct in6_ifaddr *)ifa;
1826 return NULL;
1830 * Find a link-local scoped address on ifp and return it if any.
1832 struct in6_ifaddr *
1833 in6ifa_llaonifp(struct ifnet *ifp)
1835 const struct ifaddr_container *ifac;
1837 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1838 const struct sockaddr_in6 *sin6;
1839 struct ifaddr *ifa = ifac->ifa;
1841 if (ifa->ifa_addr->sa_family != AF_INET6)
1842 continue;
1843 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1844 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1845 /* XXX why are mcast addresses ifp address list? */
1846 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1847 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1848 return (struct in6_ifaddr *)ifa;
1850 return NULL;
1854 * Convert IP6 address to printable (loggable) representation.
1856 static char digits[] = "0123456789abcdef";
1857 static int ip6round = 0;
1858 char *
1859 ip6_sprintf(const struct in6_addr *addr)
1861 static char ip6buf[8][48];
1862 int i;
1863 char *cp;
1864 const u_short *a = (const u_short *)addr;
1865 const u_char *d;
1866 int dcolon = 0;
1868 ip6round = (ip6round + 1) & 7;
1869 cp = ip6buf[ip6round];
1871 for (i = 0; i < 8; i++) {
1872 if (dcolon == 1) {
1873 if (*a == 0) {
1874 if (i == 7)
1875 *cp++ = ':';
1876 a++;
1877 continue;
1878 } else
1879 dcolon = 2;
1881 if (*a == 0) {
1882 if (dcolon == 0 && *(a + 1) == 0) {
1883 if (i == 0)
1884 *cp++ = ':';
1885 *cp++ = ':';
1886 dcolon = 1;
1887 } else {
1888 *cp++ = '0';
1889 *cp++ = ':';
1891 a++;
1892 continue;
1894 d = (const u_char *)a;
1895 *cp++ = digits[*d >> 4];
1896 *cp++ = digits[*d++ & 0xf];
1897 *cp++ = digits[*d >> 4];
1898 *cp++ = digits[*d & 0xf];
1899 *cp++ = ':';
1900 a++;
1902 *--cp = 0;
1903 return (ip6buf[ip6round]);
1907 in6_localaddr(struct in6_addr *in6)
1909 struct in6_ifaddr *ia;
1911 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1912 return 1;
1914 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1915 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1916 &ia->ia_prefixmask.sin6_addr))
1917 return 1;
1919 return (0);
1923 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1925 struct in6_ifaddr *ia;
1927 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1928 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1929 &sa6->sin6_addr) &&
1930 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1931 return (1); /* true */
1933 /* XXX: do we still have to go thru the rest of the list? */
1936 return (0); /* false */
1940 * return length of part which dst and src are equal
1941 * hard coding...
1944 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1946 int match = 0;
1947 u_char *s = (u_char *)src, *d = (u_char *)dst;
1948 u_char *lim = s + 16, r;
1950 while (s < lim)
1951 if ((r = (*d++ ^ *s++)) != 0) {
1952 while (r < 128) {
1953 match++;
1954 r <<= 1;
1956 break;
1957 } else
1958 match += 8;
1959 return match;
1962 /* XXX: to be scope conscious */
1964 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1966 int bytelen, bitlen;
1968 /* sanity check */
1969 if (0 > len || len > 128) {
1970 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1971 len);
1972 return (0);
1975 bytelen = len / 8;
1976 bitlen = len % 8;
1978 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1979 return (0);
1980 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1981 p2->s6_addr[bytelen] >> (8 - bitlen))
1982 return (0);
1984 return (1);
1987 void
1988 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1990 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1991 int bytelen, bitlen, i;
1993 /* sanity check */
1994 if (0 > len || len > 128) {
1995 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1996 len);
1997 return;
2000 bzero(maskp, sizeof(*maskp));
2001 bytelen = len / 8;
2002 bitlen = len % 8;
2003 for (i = 0; i < bytelen; i++)
2004 maskp->s6_addr[i] = 0xff;
2005 if (bitlen)
2006 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2010 * return the best address out of the same scope
2012 struct in6_ifaddr *
2013 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst, struct ucred *cred)
2015 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2016 int blen = -1;
2017 struct in6_ifaddr *ifa_best = NULL;
2018 u_int32_t dstzone, odstzone;
2019 int jailed = 0;
2020 const struct ifnet_array *arr;
2021 int i;
2023 if(cred && cred->cr_prison)
2024 jailed = 1;
2026 if (oifp == NULL)
2027 return (NULL);
2029 if (in6_addr2zoneid(oifp, dst, &odstzone))
2030 return (NULL);
2033 * We search for all addresses on all interfaces from the beginning.
2034 * Comparing an interface with the outgoing interface will be done
2035 * only at the final stage of tiebreaking.
2037 arr = ifnet_array_get();
2038 for (i = 0; i < arr->ifnet_count; ++i) {
2039 struct ifnet *ifp = arr->ifnet_arr[i];
2040 struct ifaddr_container *ifac;
2043 * We can never take an address that breaks the scope zone
2044 * of the destination.
2046 if (ifp->if_afdata[AF_INET6] == NULL)
2047 continue;
2048 if (in6_addr2zoneid(ifp, dst, &dstzone) || dstzone != odstzone)
2049 continue;
2051 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2052 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2053 struct ifaddr *ifa = ifac->ifa;
2055 if (ifa->ifa_addr->sa_family != AF_INET6)
2056 continue;
2058 src_scope = in6_addrscope(IFA_IN6(ifa));
2061 * Don't use an address before completing DAD
2062 * nor a duplicated address.
2064 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2065 IN6_IFF_NOTREADY)
2066 continue;
2068 /* XXX: is there any case to allow anycasts? */
2069 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2070 IN6_IFF_ANYCAST)
2071 continue;
2073 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2074 IN6_IFF_DETACHED)
2075 continue;
2077 /* Skip adresses not valid for current jail */
2078 if (jailed &&
2079 !(jailed_ip(cred->cr_prison, (struct sockaddr *)(ifa->ifa_addr)) != 0))
2080 continue;
2083 * If this is the first address we find,
2084 * keep it anyway.
2086 if (ifa_best == NULL)
2087 goto replace;
2090 * ifa_best is never NULL beyond this line except
2091 * within the block labeled "replace".
2095 * If ifa_best has a smaller scope than dst and
2096 * the current address has a larger one than
2097 * (or equal to) dst, always replace ifa_best.
2098 * Also, if the current address has a smaller scope
2099 * than dst, ignore it unless ifa_best also has a
2100 * smaller scope.
2101 * Consequently, after the two if-clause below,
2102 * the followings must be satisfied:
2103 * (scope(src) < scope(dst) &&
2104 * scope(best) < scope(dst))
2105 * OR
2106 * (scope(best) >= scope(dst) &&
2107 * scope(src) >= scope(dst))
2109 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2110 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2111 goto replace; /* (A) */
2112 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2113 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2114 continue; /* (B) */
2117 * A deprecated address SHOULD NOT be used in new
2118 * communications if an alternate (non-deprecated)
2119 * address is available and has sufficient scope.
2120 * RFC 2462, Section 5.5.4.
2122 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2123 IN6_IFF_DEPRECATED) {
2125 * Ignore any deprecated addresses if
2126 * specified by configuration.
2128 if (!ip6_use_deprecated)
2129 continue;
2132 * If we have already found a non-deprecated
2133 * candidate, just ignore deprecated addresses.
2135 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2136 continue;
2140 * A non-deprecated address is always preferred
2141 * to a deprecated one regardless of scopes and
2142 * address matching (Note invariants ensured by the
2143 * conditions (A) and (B) above.)
2145 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2146 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2147 IN6_IFF_DEPRECATED))
2148 goto replace;
2151 * When we use temporary addresses described in
2152 * RFC 3041, we prefer temporary addresses to
2153 * public autoconf addresses. Again, note the
2154 * invariants from (A) and (B). Also note that we
2155 * don't have any preference between static addresses
2156 * and autoconf addresses (despite of whether or not
2157 * the latter is temporary or public.)
2159 if (ip6_use_tempaddr) {
2160 struct in6_ifaddr *ifat;
2162 ifat = (struct in6_ifaddr *)ifa;
2163 if ((ifa_best->ia6_flags &
2164 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2165 == IN6_IFF_AUTOCONF &&
2166 (ifat->ia6_flags &
2167 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2168 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2169 goto replace;
2171 if ((ifa_best->ia6_flags &
2172 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2173 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2174 (ifat->ia6_flags &
2175 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2176 == IN6_IFF_AUTOCONF) {
2177 continue;
2182 * At this point, we have two cases:
2183 * 1. we are looking at a non-deprecated address,
2184 * and ifa_best is also non-deprecated.
2185 * 2. we are looking at a deprecated address,
2186 * and ifa_best is also deprecated.
2187 * Also, we do not have to consider a case where
2188 * the scope of if_best is larger(smaller) than dst and
2189 * the scope of the current address is smaller(larger)
2190 * than dst. Such a case has already been covered.
2191 * Tiebreaking is done according to the following
2192 * items:
2193 * - the scope comparison between the address and
2194 * dst (dscopecmp)
2195 * - the scope comparison between the address and
2196 * ifa_best (bscopecmp)
2197 * - if the address match dst longer than ifa_best
2198 * (matchcmp)
2199 * - if the address is on the outgoing I/F (outI/F)
2201 * Roughly speaking, the selection policy is
2202 * - the most important item is scope. The same scope
2203 * is best. Then search for a larger scope.
2204 * Smaller scopes are the last resort.
2205 * - A deprecated address is chosen only when we have
2206 * no address that has an enough scope, but is
2207 * prefered to any addresses of smaller scopes
2208 * (this must be already done above.)
2209 * - addresses on the outgoing I/F are preferred to
2210 * ones on other interfaces if none of above
2211 * tiebreaks. In the table below, the column "bI"
2212 * means if the best_ifa is on the outgoing
2213 * interface, and the column "sI" means if the ifa
2214 * is on the outgoing interface.
2215 * - If there is no other reasons to choose one,
2216 * longest address match against dst is considered.
2218 * The precise decision table is as follows:
2219 * dscopecmp bscopecmp match bI oI | replace?
2220 * N/A equal N/A Y N | No (1)
2221 * N/A equal N/A N Y | Yes (2)
2222 * N/A equal larger N/A | Yes (3)
2223 * N/A equal !larger N/A | No (4)
2224 * larger larger N/A N/A | No (5)
2225 * larger smaller N/A N/A | Yes (6)
2226 * smaller larger N/A N/A | Yes (7)
2227 * smaller smaller N/A N/A | No (8)
2228 * equal smaller N/A N/A | Yes (9)
2229 * equal larger (already done at A above)
2231 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2232 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2234 if (bscopecmp == 0) {
2235 struct ifnet *bifp = ifa_best->ia_ifp;
2237 if (bifp == oifp && ifp != oifp) /* (1) */
2238 continue;
2239 if (bifp != oifp && ifp == oifp) /* (2) */
2240 goto replace;
2243 * Both bifp and ifp are on the outgoing
2244 * interface, or both two are on a different
2245 * interface from the outgoing I/F.
2246 * now we need address matching against dst
2247 * for tiebreaking.
2249 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2250 matchcmp = tlen - blen;
2251 if (matchcmp > 0) /* (3) */
2252 goto replace;
2253 continue; /* (4) */
2255 if (dscopecmp > 0) {
2256 if (bscopecmp > 0) /* (5) */
2257 continue;
2258 goto replace; /* (6) */
2260 if (dscopecmp < 0) {
2261 if (bscopecmp > 0) /* (7) */
2262 goto replace;
2263 continue; /* (8) */
2266 /* now dscopecmp must be 0 */
2267 if (bscopecmp < 0)
2268 goto replace; /* (9) */
2270 replace:
2271 ifa_best = (struct in6_ifaddr *)ifa;
2272 blen = tlen >= 0 ? tlen :
2273 in6_matchlen(IFA_IN6(ifa), dst);
2274 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2278 /* count statistics for future improvements */
2279 if (ifa_best == NULL)
2280 ip6stat.ip6s_sources_none++;
2281 else {
2282 if (oifp == ifa_best->ia_ifp)
2283 ip6stat.ip6s_sources_sameif[best_scope]++;
2284 else
2285 ip6stat.ip6s_sources_otherif[best_scope]++;
2287 if (best_scope == dst_scope)
2288 ip6stat.ip6s_sources_samescope[best_scope]++;
2289 else
2290 ip6stat.ip6s_sources_otherscope[best_scope]++;
2292 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2293 ip6stat.ip6s_sources_deprecated[best_scope]++;
2296 return (ifa_best);
2300 * return the best address out of the same scope. if no address was
2301 * found, return the first valid address from designated IF.
2303 struct in6_ifaddr *
2304 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2306 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2307 struct ifaddr_container *ifac;
2308 struct in6_ifaddr *besta = NULL;
2309 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2311 dep[0] = dep[1] = NULL;
2314 * We first look for addresses in the same scope.
2315 * If there is one, return it.
2316 * If two or more, return one which matches the dst longest.
2317 * If none, return one of global addresses assigned other ifs.
2319 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2320 struct ifaddr *ifa = ifac->ifa;
2322 if (ifa->ifa_addr->sa_family != AF_INET6)
2323 continue;
2324 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2325 continue; /* XXX: is there any case to allow anycast? */
2326 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2327 continue; /* don't use this interface */
2328 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2329 continue;
2330 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2331 if (ip6_use_deprecated)
2332 dep[0] = (struct in6_ifaddr *)ifa;
2333 continue;
2336 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2338 * call in6_matchlen() as few as possible
2340 if (besta) {
2341 if (blen == -1)
2342 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2343 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2344 if (tlen > blen) {
2345 blen = tlen;
2346 besta = (struct in6_ifaddr *)ifa;
2348 } else
2349 besta = (struct in6_ifaddr *)ifa;
2352 if (besta)
2353 return (besta);
2355 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2356 struct ifaddr *ifa = ifac->ifa;
2358 if (ifa->ifa_addr->sa_family != AF_INET6)
2359 continue;
2360 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2361 continue; /* XXX: is there any case to allow anycast? */
2362 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2363 continue; /* don't use this interface */
2364 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2365 continue;
2366 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2367 if (ip6_use_deprecated)
2368 dep[1] = (struct in6_ifaddr *)ifa;
2369 continue;
2372 return (struct in6_ifaddr *)ifa;
2375 /* use the last-resort values, that are, deprecated addresses */
2376 if (dep[0])
2377 return dep[0];
2378 if (dep[1])
2379 return dep[1];
2381 return NULL;
2385 * perform DAD when interface becomes IFF_UP.
2387 static void
2388 in6_if_up_dispatch(netmsg_t nmsg)
2390 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
2391 struct ifaddr_container *ifac;
2392 struct in6_ifaddr *ia;
2393 int dad_delay; /* delay ticks before DAD output */
2395 ASSERT_NETISR0;
2397 in6_ifattach(ifp, NULL); /* will handle special cases */
2399 dad_delay = 0;
2400 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2401 struct ifaddr *ifa = ifac->ifa;
2403 if (ifa->ifa_addr->sa_family != AF_INET6)
2404 continue;
2405 ia = (struct in6_ifaddr *)ifa;
2406 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2407 nd6_dad_start(ifa, &dad_delay);
2410 netisr_replymsg(&nmsg->base, 0);
2413 void
2414 in6_if_up(struct ifnet *ifp)
2416 struct netmsg_base nmsg;
2418 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0, in6_if_up_dispatch);
2419 nmsg.lmsg.u.ms_resultp = ifp;
2420 netisr_domsg(&nmsg, 0);
2423 void
2424 in6_if_down(struct ifnet *ifp)
2426 rt_purgecloned(ifp, AF_INET6);
2430 in6if_do_dad(struct ifnet *ifp)
2432 if (ifp->if_flags & IFF_LOOPBACK)
2433 return (0);
2434 if (!(ifp->if_flags & IFF_MULTICAST))
2435 return (0);
2438 * Our DAD routine requires the interface up and running.
2439 * However, some interfaces can be up before the RUNNING
2440 * status. Additionally, users may try to assign addresses
2441 * before the interface becomes up (or running).
2442 * We simply skip DAD in such a case as a workaround.
2443 * XXX: we should rather mark "tentative" on such addresses,
2444 * and do DAD after the interface becomes ready.
2446 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
2447 return (0);
2449 if (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD)
2450 return (0);
2452 return (1);
2456 * Calculate max IPv6 MTU through all the interfaces and store it
2457 * to in6_maxmtu.
2459 void
2460 in6_setmaxmtu(void)
2462 unsigned long maxmtu = 0;
2463 const struct ifnet_array *arr;
2464 int i;
2466 ASSERT_NETISR0;
2468 arr = ifnet_array_get();
2469 for (i = 0; i < arr->ifnet_count; ++i) {
2470 struct ifnet *ifp = arr->ifnet_arr[i];
2472 /* this function can be called during ifnet initialization */
2473 if (ifp->if_afdata[AF_INET6] == NULL)
2474 continue;
2475 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2476 IN6_LINKMTU(ifp) > maxmtu)
2477 maxmtu = IN6_LINKMTU(ifp);
2479 if (maxmtu) /* update only when maxmtu is positive */
2480 in6_maxmtu = maxmtu;
2483 void *
2484 in6_domifattach(struct ifnet *ifp)
2486 struct in6_ifextra *ext;
2488 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2489 bzero(ext, sizeof(*ext));
2491 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2492 M_IFADDR, M_WAITOK);
2493 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2495 ext->icmp6_ifstat =
2496 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2497 M_IFADDR, M_WAITOK);
2498 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2500 ext->nd_ifinfo = nd6_ifattach(ifp);
2501 ext->scope6_id = scope6_ifattach(ifp);
2502 return ext;
2505 void
2506 in6_domifdetach(struct ifnet *ifp, void *aux)
2508 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2509 scope6_ifdetach(ext->scope6_id);
2510 nd6_ifdetach(ext->nd_ifinfo);
2511 kfree(ext->in6_ifstat, M_IFADDR);
2512 kfree(ext->icmp6_ifstat, M_IFADDR);
2513 kfree(ext, M_IFADDR);