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>
43 #include <net/route.h>
46 #include <netinet/in.h>
48 #include <netinet6/in6_var.h>
49 #include <netinet6/scope6_var.h>
51 #ifdef ENABLE_DEFAULT_SCOPE
52 int ip6_use_defzone = 1;
54 int ip6_use_defzone = 0;
58 * The scope6_lock protects the global sid default stored in
61 static struct mtx scope6_lock;
62 #define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
63 #define SCOPE6_LOCK() mtx_lock(&scope6_lock)
64 #define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
65 #define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
67 static struct scope6_id sid_default;
69 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
76 bzero(&sid_default, sizeof(sid_default));
80 scope6_ifattach(struct ifnet *ifp)
82 struct scope6_id *sid;
84 sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
85 bzero(sid, sizeof(*sid));
88 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
89 * Should we rather hardcode here?
91 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
92 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
94 /* by default, we don't care about scope boundary for these scopes. */
95 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
96 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
103 scope6_ifdetach(struct scope6_id *sid)
110 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
114 struct scope6_id *sid = NULL;
119 if (!sid) { /* paranoid? */
120 IF_AFDATA_UNLOCK(ifp);
125 * XXX: We need more consistency checks of the relationship among
126 * scopes (e.g. an organization should be larger than a site).
130 * TODO(XXX): after setting, we should reflect the changes to
131 * interface addresses, routing table entries, PCB entries...
135 for (i = 0; i < 16; i++) {
136 if (idlist->s6id_list[i] &&
137 idlist->s6id_list[i] != sid->s6id_list[i]) {
139 * An interface zone ID must be the corresponding
140 * interface index by definition.
142 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
143 idlist->s6id_list[i] != ifp->if_index) {
144 IF_AFDATA_UNLOCK(ifp);
149 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
150 idlist->s6id_list[i] > if_index) {
152 * XXX: theoretically, there should be no
153 * relationship between link IDs and interface
154 * IDs, but we check the consistency for
155 * safety in later use.
157 IF_AFDATA_UNLOCK(ifp);
163 * XXX: we must need lots of work in this case,
164 * but we simply set the new value in this initial
167 sid->s6id_list[i] = idlist->s6id_list[i];
171 IF_AFDATA_UNLOCK(ifp);
177 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
179 /* We only need to lock the interface's afdata for SID() to work. */
181 struct scope6_id *sid = SID(ifp);
183 if (sid == NULL) { /* paranoid? */
184 IF_AFDATA_UNLOCK(ifp);
192 IF_AFDATA_UNLOCK(ifp);
198 * Get a scope of the address. Node-local, link-local, site-local or global.
201 in6_addrscope(struct in6_addr *addr)
205 if (addr->s6_addr[0] == 0xfe) {
206 scope = addr->s6_addr[1] & 0xc0;
210 return IPV6_ADDR_SCOPE_LINKLOCAL;
213 return IPV6_ADDR_SCOPE_SITELOCAL;
216 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
222 if (addr->s6_addr[0] == 0xff) {
223 scope = addr->s6_addr[1] & 0x0f;
226 * due to other scope such as reserved,
227 * return scope doesn't work.
230 case IPV6_ADDR_SCOPE_INTFACELOCAL:
231 return IPV6_ADDR_SCOPE_INTFACELOCAL;
233 case IPV6_ADDR_SCOPE_LINKLOCAL:
234 return IPV6_ADDR_SCOPE_LINKLOCAL;
236 case IPV6_ADDR_SCOPE_SITELOCAL:
237 return IPV6_ADDR_SCOPE_SITELOCAL;
240 return IPV6_ADDR_SCOPE_GLOBAL;
246 * Regard loopback and unspecified addresses as global, since
247 * they have no ambiguity.
249 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
250 if (addr->s6_addr[15] == 1) /* loopback */
251 return IPV6_ADDR_SCOPE_LINKLOCAL;
252 if (addr->s6_addr[15] == 0) /* unspecified */
253 return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
256 return IPV6_ADDR_SCOPE_GLOBAL;
260 * ifp - note that this might be NULL
264 scope6_setdefault(struct ifnet *ifp)
267 * Currently, this function just sets the default "interfaces"
268 * and "links" according to the given interface.
269 * We might eventually have to separate the notion of "link" from
270 * "interface" and provide a user interface to set the default.
274 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
276 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
279 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
280 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
286 scope6_get_default(struct scope6_id *idlist)
290 *idlist = sid_default;
297 scope6_addr2default(struct in6_addr *addr)
302 * special case: The loopback address should be considered as
303 * link-local, but there's no ambiguity in the syntax.
305 if (IN6_IS_ADDR_LOOPBACK(addr))
309 * XXX: 32-bit read is atomic on all our platforms, is it OK
313 id = sid_default.s6id_list[in6_addrscope(addr)];
319 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
320 * is unspecified (=0), needs to be specified, and the default zone ID can be
321 * used, the default value will be used.
322 * This routine then generates the kernel-internal form: if the address scope
323 * of is interface-local or link-local, embed the interface index in the
327 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
332 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
333 zoneid = scope6_addr2default(&sin6->sin6_addr);
336 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
337 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
339 * At this moment, we only check interface-local and
340 * link-local scope IDs, and use interface indices as the
341 * zone IDs assuming a one-to-one mapping between interfaces
344 if (if_index < zoneid)
346 ifp = ifnet_byindex(zoneid);
347 if (ifp == NULL) /* XXX: this can happen for some OS */
350 /* XXX assignment to 16bit from 32bit variable */
351 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
353 sin6->sin6_scope_id = 0;
360 * generate standard sockaddr_in6 from embedded form.
363 sa6_recoverscope(struct sockaddr_in6 *sin6)
365 char ip6buf[INET6_ADDRSTRLEN];
368 if (sin6->sin6_scope_id != 0) {
370 "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
371 ip6_sprintf(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id);
372 /* XXX: proceed anyway... */
374 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
375 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
377 * KAME assumption: link id == interface id
379 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
382 if (zoneid < 0 || if_index < zoneid)
384 if (!ifnet_byindex(zoneid))
386 sin6->sin6_addr.s6_addr16[1] = 0;
387 sin6->sin6_scope_id = zoneid;
395 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
396 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
397 * in the in6_addr structure, in6 will be modified.
399 * ret_id - unnecessary?
402 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
405 u_int32_t zoneid = 0;
406 struct scope6_id *sid;
413 if (sid == NULL) { /* should not happen */
414 panic("in6_setscope: scope array is NULL");
420 * special case: the loopback address can only belong to a loopback
423 if (IN6_IS_ADDR_LOOPBACK(in6)) {
424 if (!(ifp->if_flags & IFF_LOOPBACK)) {
425 IF_AFDATA_UNLOCK(ifp);
429 *ret_id = 0; /* there's no ambiguity */
430 IF_AFDATA_UNLOCK(ifp);
435 scope = in6_addrscope(in6);
439 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
440 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
443 case IPV6_ADDR_SCOPE_LINKLOCAL:
444 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
447 case IPV6_ADDR_SCOPE_SITELOCAL:
448 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
451 case IPV6_ADDR_SCOPE_ORGLOCAL:
452 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
456 zoneid = 0; /* XXX: treat as global. */
460 IF_AFDATA_UNLOCK(ifp);
465 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
466 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
472 * Just clear the embedded scope identifier. Return 0 if the original address
473 * is intact; return non 0 if the address is modified.
476 in6_clearscope(struct in6_addr *in6)
480 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
481 if (in6->s6_addr16[1] != 0)
483 in6->s6_addr16[1] = 0;