2 /* $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $ */
5 * Copyright (C) 2000 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
33 #include <sys/param.h>
34 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/queue.h>
39 #include <sys/syslog.h>
41 #include <net/route.h>
44 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet6/scope6_var.h>
49 #ifdef ENABLE_DEFAULT_SCOPE
50 int ip6_use_defzone = 1;
52 int ip6_use_defzone = 0;
56 * The scope6_lock protects the global sid default stored in
59 static struct mtx scope6_lock;
60 #define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
61 #define SCOPE6_LOCK() mtx_lock(&scope6_lock)
62 #define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
63 #define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
65 static struct scope6_id sid_default;
67 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
74 bzero(&sid_default, sizeof(sid_default));
81 struct scope6_id *sid;
83 sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
84 bzero(sid, sizeof(*sid));
87 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
88 * Should we rather hardcode here?
90 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
91 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
93 /* by default, we don't care about scope boundary for these scopes. */
94 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
95 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
103 struct scope6_id *sid;
110 scope6_set(ifp, idlist)
112 struct scope6_id *idlist;
116 struct scope6_id *sid = NULL;
121 if (!sid) { /* paranoid? */
122 IF_AFDATA_UNLOCK(ifp);
127 * XXX: We need more consistency checks of the relationship among
128 * scopes (e.g. an organization should be larger than a site).
132 * TODO(XXX): after setting, we should reflect the changes to
133 * interface addresses, routing table entries, PCB entries...
137 for (i = 0; i < 16; i++) {
138 if (idlist->s6id_list[i] &&
139 idlist->s6id_list[i] != sid->s6id_list[i]) {
141 * An interface zone ID must be the corresponding
142 * interface index by definition.
144 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
145 idlist->s6id_list[i] != ifp->if_index) {
146 IF_AFDATA_UNLOCK(ifp);
151 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
152 idlist->s6id_list[i] > if_index) {
154 * XXX: theoretically, there should be no
155 * relationship between link IDs and interface
156 * IDs, but we check the consistency for
157 * safety in later use.
159 IF_AFDATA_UNLOCK(ifp);
165 * XXX: we must need lots of work in this case,
166 * but we simply set the new value in this initial
169 sid->s6id_list[i] = idlist->s6id_list[i];
173 IF_AFDATA_UNLOCK(ifp);
179 scope6_get(ifp, idlist)
181 struct scope6_id *idlist;
183 /* We only need to lock the interface's afdata for SID() to work. */
185 struct scope6_id *sid = SID(ifp);
187 if (sid == NULL) { /* paranoid? */
188 IF_AFDATA_UNLOCK(ifp);
196 IF_AFDATA_UNLOCK(ifp);
202 * Get a scope of the address. Node-local, link-local, site-local or global.
206 struct in6_addr *addr;
210 if (addr->s6_addr[0] == 0xfe) {
211 scope = addr->s6_addr[1] & 0xc0;
215 return IPV6_ADDR_SCOPE_LINKLOCAL;
218 return IPV6_ADDR_SCOPE_SITELOCAL;
221 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
227 if (addr->s6_addr[0] == 0xff) {
228 scope = addr->s6_addr[1] & 0x0f;
231 * due to other scope such as reserved,
232 * return scope doesn't work.
235 case IPV6_ADDR_SCOPE_INTFACELOCAL:
236 return IPV6_ADDR_SCOPE_INTFACELOCAL;
238 case IPV6_ADDR_SCOPE_LINKLOCAL:
239 return IPV6_ADDR_SCOPE_LINKLOCAL;
241 case IPV6_ADDR_SCOPE_SITELOCAL:
242 return IPV6_ADDR_SCOPE_SITELOCAL;
245 return IPV6_ADDR_SCOPE_GLOBAL;
251 * Regard loopback and unspecified addresses as global, since
252 * they have no ambiguity.
254 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
255 if (addr->s6_addr[15] == 1) /* loopback */
256 return IPV6_ADDR_SCOPE_LINKLOCAL;
257 if (addr->s6_addr[15] == 0) /* unspecified */
258 return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
261 return IPV6_ADDR_SCOPE_GLOBAL;
265 scope6_setdefault(ifp)
266 struct ifnet *ifp; /* note that this might be NULL */
269 * Currently, this function just sets the default "interfaces"
270 * and "links" according to the given interface.
271 * We might eventually have to separate the notion of "link" from
272 * "interface" and provide a user interface to set the default.
276 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
278 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
281 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
282 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
288 scope6_get_default(idlist)
289 struct scope6_id *idlist;
293 *idlist = sid_default;
300 scope6_addr2default(addr)
301 struct in6_addr *addr;
306 * special case: The loopback address should be considered as
307 * link-local, but there's no ambiguity in the syntax.
309 if (IN6_IS_ADDR_LOOPBACK(addr))
313 * XXX: 32-bit read is atomic on all our platforms, is it OK
317 id = sid_default.s6id_list[in6_addrscope(addr)];
323 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
324 * is unspecified (=0), needs to be specified, and the default zone ID can be
325 * used, the default value will be used.
326 * This routine then generates the kernel-internal form: if the address scope
327 * of is interface-local or link-local, embed the interface index in the
331 sa6_embedscope(sin6, defaultok)
332 struct sockaddr_in6 *sin6;
338 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
339 zoneid = scope6_addr2default(&sin6->sin6_addr);
342 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
343 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
345 * At this moment, we only check interface-local and
346 * link-local scope IDs, and use interface indices as the
347 * zone IDs assuming a one-to-one mapping between interfaces
350 if (if_index < zoneid)
352 ifp = ifnet_byindex(zoneid);
353 if (ifp == NULL) /* XXX: this can happen for some OS */
356 /* XXX assignment to 16bit from 32bit variable */
357 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
359 sin6->sin6_scope_id = 0;
366 * generate standard sockaddr_in6 from embedded form.
369 sa6_recoverscope(sin6)
370 struct sockaddr_in6 *sin6;
374 if (sin6->sin6_scope_id != 0) {
376 "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
377 ip6_sprintf(&sin6->sin6_addr), sin6->sin6_scope_id);
378 /* XXX: proceed anyway... */
380 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
381 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
383 * KAME assumption: link id == interface id
385 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
388 if (zoneid < 0 || if_index < zoneid)
390 if (!ifnet_byindex(zoneid))
392 sin6->sin6_addr.s6_addr16[1] = 0;
393 sin6->sin6_scope_id = zoneid;
401 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
402 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
403 * in the in6_addr structure, in6 will be modified.
406 in6_setscope(in6, ifp, ret_id)
407 struct in6_addr *in6;
409 u_int32_t *ret_id; /* unnecessary? */
412 u_int32_t zoneid = 0;
413 struct scope6_id *sid;
420 if (sid == NULL) { /* should not happen */
421 panic("in6_setscope: scope array is NULL");
427 * special case: the loopback address can only belong to a loopback
430 if (IN6_IS_ADDR_LOOPBACK(in6)) {
431 if (!(ifp->if_flags & IFF_LOOPBACK)) {
432 IF_AFDATA_UNLOCK(ifp);
436 *ret_id = 0; /* there's no ambiguity */
437 IF_AFDATA_UNLOCK(ifp);
442 scope = in6_addrscope(in6);
446 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
447 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
450 case IPV6_ADDR_SCOPE_LINKLOCAL:
451 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
454 case IPV6_ADDR_SCOPE_SITELOCAL:
455 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
458 case IPV6_ADDR_SCOPE_ORGLOCAL:
459 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
463 zoneid = 0; /* XXX: treat as global. */
467 IF_AFDATA_UNLOCK(ifp);
472 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
473 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
479 * Just clear the embedded scope identifier. Return 0 if the original address
480 * is intact; return non 0 if the address is modified.
484 struct in6_addr *in6;
488 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
489 if (in6->s6_addr16[1] != 0)
491 in6->s6_addr16[1] = 0;