2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 2000 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/systm.h>
43 #include <sys/queue.h>
44 #include <sys/sysctl.h>
45 #include <sys/syslog.h>
48 #include <net/if_var.h>
51 #include <netinet/in.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/in6_var.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet6/scope6_var.h>
58 #ifdef ENABLE_DEFAULT_SCOPE
59 VNET_DEFINE(int, ip6_use_defzone) = 1;
61 VNET_DEFINE(int, ip6_use_defzone) = 0;
63 SYSCTL_DECL(_net_inet6_ip6);
66 * The scope6_lock protects the global sid default stored in
69 static struct mtx scope6_lock;
70 #define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
71 #define SCOPE6_LOCK() mtx_lock(&scope6_lock)
72 #define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
73 #define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
75 VNET_DEFINE_STATIC(struct scope6_id, sid_default);
76 #define V_sid_default VNET(sid_default)
79 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
81 static int scope6_get(struct ifnet *, struct scope6_id *);
82 static int scope6_set(struct ifnet *, struct scope6_id *);
88 bzero(&V_sid_default, sizeof(V_sid_default));
90 if (!IS_DEFAULT_VNET(curvnet))
97 scope6_ifattach(struct ifnet *ifp)
99 struct scope6_id *sid;
101 sid = malloc(sizeof(*sid), M_IFADDR, M_WAITOK | M_ZERO);
103 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
104 * Should we rather hardcode here?
106 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
107 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
112 scope6_ifdetach(struct scope6_id *sid)
119 scope6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
121 struct in6_ifreq *ifr;
123 if (ifp->if_afdata[AF_INET6] == NULL)
124 return (EPFNOSUPPORT);
126 ifr = (struct in6_ifreq *)data;
129 return (scope6_set(ifp,
130 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
132 return (scope6_get(ifp,
133 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
135 return (scope6_get_default(
136 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
143 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
147 struct scope6_id *sid = NULL;
149 IF_AFDATA_WLOCK(ifp);
152 if (!sid) { /* paranoid? */
153 IF_AFDATA_WUNLOCK(ifp);
158 * XXX: We need more consistency checks of the relationship among
159 * scopes (e.g. an organization should be larger than a site).
163 * TODO(XXX): after setting, we should reflect the changes to
164 * interface addresses, routing table entries, PCB entries...
167 for (i = 0; i < 16; i++) {
168 if (idlist->s6id_list[i] &&
169 idlist->s6id_list[i] != sid->s6id_list[i]) {
171 * An interface zone ID must be the corresponding
172 * interface index by definition.
174 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
175 idlist->s6id_list[i] != ifp->if_index) {
176 IF_AFDATA_WUNLOCK(ifp);
180 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
181 idlist->s6id_list[i] > V_if_index) {
183 * XXX: theoretically, there should be no
184 * relationship between link IDs and interface
185 * IDs, but we check the consistency for
186 * safety in later use.
188 IF_AFDATA_WUNLOCK(ifp);
193 * XXX: we must need lots of work in this case,
194 * but we simply set the new value in this initial
197 sid->s6id_list[i] = idlist->s6id_list[i];
200 IF_AFDATA_WUNLOCK(ifp);
206 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
208 struct epoch_tracker et;
209 struct scope6_id *sid;
211 /* We only need to lock the interface's afdata for SID() to work. */
214 if (sid == NULL) { /* paranoid? */
226 * Get a scope of the address. Node-local, link-local, site-local or global.
229 in6_addrscope(const struct in6_addr *addr)
232 if (IN6_IS_ADDR_MULTICAST(addr)) {
234 * Addresses with reserved value F must be treated as
235 * global multicast addresses.
237 if (IPV6_ADDR_MC_SCOPE(addr) == 0x0f)
238 return (IPV6_ADDR_SCOPE_GLOBAL);
239 return (IPV6_ADDR_MC_SCOPE(addr));
241 if (IN6_IS_ADDR_LINKLOCAL(addr) ||
242 IN6_IS_ADDR_LOOPBACK(addr))
243 return (IPV6_ADDR_SCOPE_LINKLOCAL);
244 if (IN6_IS_ADDR_SITELOCAL(addr))
245 return (IPV6_ADDR_SCOPE_SITELOCAL);
246 return (IPV6_ADDR_SCOPE_GLOBAL);
250 * ifp - note that this might be NULL
254 scope6_setdefault(struct ifnet *ifp)
258 * Currently, this function just sets the default "interfaces"
259 * and "links" according to the given interface.
260 * We might eventually have to separate the notion of "link" from
261 * "interface" and provide a user interface to set the default.
265 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
267 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
270 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
271 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
277 scope6_get_default(struct scope6_id *idlist)
281 *idlist = V_sid_default;
288 scope6_addr2default(struct in6_addr *addr)
293 * special case: The loopback address should be considered as
294 * link-local, but there's no ambiguity in the syntax.
296 if (IN6_IS_ADDR_LOOPBACK(addr))
300 * XXX: 32-bit read is atomic on all our platforms, is it OK
304 id = V_sid_default.s6id_list[in6_addrscope(addr)];
310 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
311 * is unspecified (=0), needs to be specified, and the default zone ID can be
312 * used, the default value will be used.
313 * This routine then generates the kernel-internal form: if the address scope
314 * of is interface-local or link-local, embed the interface index in the
318 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
322 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
323 zoneid = scope6_addr2default(&sin6->sin6_addr);
326 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
327 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
329 * At this moment, we only check interface-local and
330 * link-local scope IDs, and use interface indices as the
331 * zone IDs assuming a one-to-one mapping between interfaces
334 if (V_if_index < zoneid || ifnet_byindex(zoneid) == NULL)
337 /* XXX assignment to 16bit from 32bit variable */
338 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
339 sin6->sin6_scope_id = 0;
346 * generate standard sockaddr_in6 from embedded form.
349 sa6_recoverscope(struct sockaddr_in6 *sin6)
351 char ip6buf[INET6_ADDRSTRLEN];
354 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
355 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
357 * KAME assumption: link id == interface id
359 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
362 if (V_if_index < zoneid)
365 /* XXX: Disabled due to possible deadlock. */
366 if (!ifnet_byindex(zoneid))
369 if (sin6->sin6_scope_id != 0 &&
370 zoneid != sin6->sin6_scope_id) {
372 "%s: embedded scope mismatch: %s%%%d. "
373 "sin6_scope_id was overridden\n", __func__,
374 ip6_sprintf(ip6buf, &sin6->sin6_addr),
375 sin6->sin6_scope_id);
377 sin6->sin6_addr.s6_addr16[1] = 0;
378 sin6->sin6_scope_id = zoneid;
386 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
387 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
388 * in the in6_addr structure, in6 will be modified.
390 * ret_id - unnecessary?
393 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
396 u_int32_t zoneid = 0;
397 struct scope6_id *sid;
400 * special case: the loopback address can only belong to a loopback
403 if (IN6_IS_ADDR_LOOPBACK(in6)) {
404 if (!(ifp->if_flags & IFF_LOOPBACK))
407 scope = in6_addrscope(in6);
408 if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
409 scope == IPV6_ADDR_SCOPE_LINKLOCAL) {
411 * Currently we use interface indices as the
412 * zone IDs for interface-local and link-local
415 zoneid = ifp->if_index;
416 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
417 } else if (scope != IPV6_ADDR_SCOPE_GLOBAL) {
418 struct epoch_tracker et;
421 if (ifp->if_afdata[AF_INET6] == NULL) {
426 zoneid = sid->s6id_list[scope];
438 * Just clear the embedded scope identifier. Return 0 if the original address
439 * is intact; return non 0 if the address is modified.
442 in6_clearscope(struct in6_addr *in6)
446 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
447 if (in6->s6_addr16[1] != 0)
449 in6->s6_addr16[1] = 0;
456 * Return the scope identifier or zero.
459 in6_getscope(const struct in6_addr *in6)
462 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
463 return (in6->s6_addr16[1]);
469 * Returns scope zone id for the unicast address @in6.
471 * Returns 0 for global unicast and loopback addresses.
472 * Returns interface index for the link-local addresses.
475 in6_get_unicast_scopeid(const struct in6_addr *in6, const struct ifnet *ifp)
478 if (IN6_IS_SCOPE_LINKLOCAL(in6))
479 return (ifp->if_index);
484 in6_set_unicast_scopeid(struct in6_addr *in6, uint32_t scopeid)
487 in6->s6_addr16[1] = htons(scopeid & 0xffff);
491 * Return pointer to ifnet structure, corresponding to the zone id of
495 in6_getlinkifnet(uint32_t zoneid)
498 return (ifnet_byindex((u_short)zoneid));
502 * Return zone id for the specified scope.
505 in6_getscopezone(const struct ifnet *ifp, int scope)
508 if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
509 scope == IPV6_ADDR_SCOPE_LINKLOCAL)
510 return (ifp->if_index);
511 if (scope >= 0 && scope < IPV6_ADDR_SCOPES_COUNT)
512 return (SID(ifp)->s6id_list[scope]);
517 * Extracts scope from adddress @dst, stores cleared address
518 * inside @dst and zone inside @scopeid
521 in6_splitscope(const struct in6_addr *src, struct in6_addr *dst,
527 zoneid = ntohs(in6_getscope(dst));
533 * This function is for checking sockaddr_in6 structure passed
534 * from the application level (usually).
536 * sin6_scope_id should be set for link-local unicast, link-local and
537 * interface-local multicast addresses.
539 * If it is zero, then look into default zone ids. If default zone id is
540 * not set or disabled, then return error.
543 sa6_checkzone(struct sockaddr_in6 *sa6)
547 scope = in6_addrscope(&sa6->sin6_addr);
548 if (scope == IPV6_ADDR_SCOPE_GLOBAL)
549 return (sa6->sin6_scope_id ? EINVAL: 0);
550 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr) &&
551 scope != IPV6_ADDR_SCOPE_LINKLOCAL &&
552 scope != IPV6_ADDR_SCOPE_INTFACELOCAL) {
553 if (sa6->sin6_scope_id == 0 && V_ip6_use_defzone != 0)
554 sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
558 * Since ::1 address always configured on the lo0, we can
559 * automatically set its zone id, when it is not specified.
560 * Return error, when specified zone id doesn't match with
563 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) {
564 if (sa6->sin6_scope_id == 0)
565 sa6->sin6_scope_id = in6_getscopezone(V_loif, scope);
566 else if (sa6->sin6_scope_id != in6_getscopezone(V_loif, scope))
567 return (EADDRNOTAVAIL);
569 /* XXX: we can validate sin6_scope_id here */
570 if (sa6->sin6_scope_id != 0)
572 if (V_ip6_use_defzone != 0)
573 sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
574 /* Return error if we can't determine zone id */
575 return (sa6->sin6_scope_id ? 0: EADDRNOTAVAIL);
579 * This function is similar to sa6_checkzone, but it uses given ifp
580 * to initialize sin6_scope_id.
583 sa6_checkzone_ifp(struct ifnet *ifp, struct sockaddr_in6 *sa6)
587 scope = in6_addrscope(&sa6->sin6_addr);
588 if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
589 scope == IPV6_ADDR_SCOPE_INTFACELOCAL) {
590 if (sa6->sin6_scope_id == 0) {
591 sa6->sin6_scope_id = in6_getscopezone(ifp, scope);
593 } else if (sa6->sin6_scope_id != in6_getscopezone(ifp, scope))
594 return (EADDRNOTAVAIL);
596 return (sa6_checkzone(sa6));