2 * Copyright (C) 2000 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/syslog.h>
46 #include <netinet/in.h>
48 #include <netinet/ip6.h>
49 #include <netinet6/in6_var.h>
50 #include <netinet6/ip6_var.h>
51 #include <netinet6/scope6_var.h>
53 #ifdef ENABLE_DEFAULT_SCOPE
54 VNET_DEFINE(int, ip6_use_defzone) = 1;
56 VNET_DEFINE(int, ip6_use_defzone) = 0;
60 * The scope6_lock protects the global sid default stored in
63 static struct mtx scope6_lock;
64 #define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
65 #define SCOPE6_LOCK() mtx_lock(&scope6_lock)
66 #define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
67 #define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
69 static VNET_DEFINE(struct scope6_id, sid_default);
70 #define V_sid_default VNET(sid_default)
73 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
79 bzero(&V_sid_default, sizeof(V_sid_default));
81 if (!IS_DEFAULT_VNET(curvnet))
88 scope6_ifattach(struct ifnet *ifp)
90 struct scope6_id *sid;
92 sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
93 bzero(sid, sizeof(*sid));
96 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
97 * Should we rather hardcode here?
99 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
100 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
102 /* by default, we don't care about scope boundary for these scopes. */
103 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
104 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
111 scope6_ifdetach(struct scope6_id *sid)
118 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
122 struct scope6_id *sid = NULL;
127 if (!sid) { /* paranoid? */
128 IF_AFDATA_UNLOCK(ifp);
133 * XXX: We need more consistency checks of the relationship among
134 * scopes (e.g. an organization should be larger than a site).
138 * TODO(XXX): after setting, we should reflect the changes to
139 * interface addresses, routing table entries, PCB entries...
143 for (i = 0; i < 16; i++) {
144 if (idlist->s6id_list[i] &&
145 idlist->s6id_list[i] != sid->s6id_list[i]) {
147 * An interface zone ID must be the corresponding
148 * interface index by definition.
150 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
151 idlist->s6id_list[i] != ifp->if_index) {
152 IF_AFDATA_UNLOCK(ifp);
157 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
158 idlist->s6id_list[i] > V_if_index) {
160 * XXX: theoretically, there should be no
161 * relationship between link IDs and interface
162 * IDs, but we check the consistency for
163 * safety in later use.
165 IF_AFDATA_UNLOCK(ifp);
171 * XXX: we must need lots of work in this case,
172 * but we simply set the new value in this initial
175 sid->s6id_list[i] = idlist->s6id_list[i];
179 IF_AFDATA_UNLOCK(ifp);
185 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
187 /* We only need to lock the interface's afdata for SID() to work. */
189 struct scope6_id *sid = SID(ifp);
191 if (sid == NULL) { /* paranoid? */
192 IF_AFDATA_UNLOCK(ifp);
200 IF_AFDATA_UNLOCK(ifp);
206 * Get a scope of the address. Node-local, link-local, site-local or global.
209 in6_addrscope(struct in6_addr *addr)
213 if (addr->s6_addr[0] == 0xfe) {
214 scope = addr->s6_addr[1] & 0xc0;
218 return IPV6_ADDR_SCOPE_LINKLOCAL;
221 return IPV6_ADDR_SCOPE_SITELOCAL;
224 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
230 if (addr->s6_addr[0] == 0xff) {
231 scope = addr->s6_addr[1] & 0x0f;
234 * due to other scope such as reserved,
235 * return scope doesn't work.
238 case IPV6_ADDR_SCOPE_INTFACELOCAL:
239 return IPV6_ADDR_SCOPE_INTFACELOCAL;
241 case IPV6_ADDR_SCOPE_LINKLOCAL:
242 return IPV6_ADDR_SCOPE_LINKLOCAL;
244 case IPV6_ADDR_SCOPE_SITELOCAL:
245 return IPV6_ADDR_SCOPE_SITELOCAL;
248 return IPV6_ADDR_SCOPE_GLOBAL;
254 * Regard loopback and unspecified addresses as global, since
255 * they have no ambiguity.
257 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
258 if (addr->s6_addr[15] == 1) /* loopback */
259 return IPV6_ADDR_SCOPE_LINKLOCAL;
260 if (addr->s6_addr[15] == 0) /* unspecified */
261 return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
264 return IPV6_ADDR_SCOPE_GLOBAL;
268 * ifp - note that this might be NULL
272 scope6_setdefault(struct ifnet *ifp)
276 * Currently, this function just sets the default "interfaces"
277 * and "links" according to the given interface.
278 * We might eventually have to separate the notion of "link" from
279 * "interface" and provide a user interface to set the default.
283 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
285 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
288 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
289 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
295 scope6_get_default(struct scope6_id *idlist)
299 *idlist = V_sid_default;
306 scope6_addr2default(struct in6_addr *addr)
311 * special case: The loopback address should be considered as
312 * link-local, but there's no ambiguity in the syntax.
314 if (IN6_IS_ADDR_LOOPBACK(addr))
318 * XXX: 32-bit read is atomic on all our platforms, is it OK
322 id = V_sid_default.s6id_list[in6_addrscope(addr)];
328 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
329 * is unspecified (=0), needs to be specified, and the default zone ID can be
330 * used, the default value will be used.
331 * This routine then generates the kernel-internal form: if the address scope
332 * of is interface-local or link-local, embed the interface index in the
336 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
341 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
342 zoneid = scope6_addr2default(&sin6->sin6_addr);
345 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
346 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
348 * At this moment, we only check interface-local and
349 * link-local scope IDs, and use interface indices as the
350 * zone IDs assuming a one-to-one mapping between interfaces
353 if (V_if_index < zoneid)
355 ifp = ifnet_byindex(zoneid);
356 if (ifp == NULL) /* XXX: this can happen for some OS */
359 /* XXX assignment to 16bit from 32bit variable */
360 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
362 sin6->sin6_scope_id = 0;
369 * generate standard sockaddr_in6 from embedded form.
372 sa6_recoverscope(struct sockaddr_in6 *sin6)
374 char ip6buf[INET6_ADDRSTRLEN];
377 if (sin6->sin6_scope_id != 0) {
379 "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
380 ip6_sprintf(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id);
381 /* XXX: proceed anyway... */
383 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
384 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
386 * KAME assumption: link id == interface id
388 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
391 if (zoneid < 0 || V_if_index < zoneid)
393 if (!ifnet_byindex(zoneid))
395 sin6->sin6_addr.s6_addr16[1] = 0;
396 sin6->sin6_scope_id = zoneid;
404 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
405 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
406 * in the in6_addr structure, in6 will be modified.
408 * ret_id - unnecessary?
411 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
414 u_int32_t zoneid = 0;
415 struct scope6_id *sid;
422 if (sid == NULL) { /* should not happen */
423 panic("in6_setscope: scope array is NULL");
429 * special case: the loopback address can only belong to a loopback
432 if (IN6_IS_ADDR_LOOPBACK(in6)) {
433 if (!(ifp->if_flags & IFF_LOOPBACK)) {
434 IF_AFDATA_UNLOCK(ifp);
438 *ret_id = 0; /* there's no ambiguity */
439 IF_AFDATA_UNLOCK(ifp);
444 scope = in6_addrscope(in6);
448 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
449 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
452 case IPV6_ADDR_SCOPE_LINKLOCAL:
453 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
456 case IPV6_ADDR_SCOPE_SITELOCAL:
457 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
460 case IPV6_ADDR_SCOPE_ORGLOCAL:
461 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
465 zoneid = 0; /* XXX: treat as global. */
469 IF_AFDATA_UNLOCK(ifp);
474 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
475 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
481 * Just clear the embedded scope identifier. Return 0 if the original address
482 * is intact; return non 0 if the address is modified.
485 in6_clearscope(struct in6_addr *in6)
489 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
490 if (in6->s6_addr16[1] != 0)
492 in6->s6_addr16[1] = 0;