1 //==========================================================================
3 // src/sys/netinet6/scope6.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $KAME: scope6.c,v 1.27 2001/12/18 02:23:45 itojun Exp $ */
25 * Copyright (C) 2000 WIDE Project.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 #include <sys/param.h>
54 #include <sys/malloc.h>
56 #include <sys/socket.h>
57 #include <sys/queue.h>
59 #include <net/route.h>
62 #include <netinet/in.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet6/scope6_var.h>
67 #ifdef ENABLE_DEFAULT_SCOPE
68 int ip6_use_defzone = 1;
70 int ip6_use_defzone = 0;
75 * 16 is correspondent to 4bit multicast scope field.
76 * i.e. from node-local to global with some reserved/unassigned types.
78 u_int32_t s6id_list[16];
80 static size_t if_indexlim = 8;
81 struct scope6_id *scope6_ids = NULL;
94 * We have some arrays that should be indexed by if_index.
95 * since if_index will grow dynamically, they should grow too.
97 if (scope6_ids == NULL || if_index >= if_indexlim) {
101 while (if_index >= if_indexlim)
104 /* grow scope index array */
105 n = if_indexlim * sizeof(struct scope6_id);
106 /* XXX: need new malloc type? */
107 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
110 bcopy((caddr_t)scope6_ids, q, n/2);
111 free((caddr_t)scope6_ids, M_IFADDR);
113 scope6_ids = (struct scope6_id *)q;
116 #define SID scope6_ids[ifp->if_index]
118 /* don't initialize if called twice */
119 if (SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]) {
125 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
126 * Should we rather hardcode here?
128 SID.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
129 SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
131 /* by default, we don't care about scope boundary for these scopes. */
132 SID.s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
133 SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
141 scope6_set(ifp, idlist)
148 if (scope6_ids == NULL) /* paranoid? */
152 * XXX: We need more consistency checks of the relationship among
153 * scopes (e.g. an organization should be larger than a site).
157 * TODO(XXX): after setting, we should reflect the changes to
158 * interface addresses, routing table entries, PCB entries...
167 for (i = 0; i < 16; i++) {
169 idlist[i] != scope6_ids[ifp->if_index].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[i] != ifp->if_index) {
180 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
181 idlist[i] > 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.
193 * XXX: we must need lots of work in this case,
194 * but we simply set the new value in this initial
197 scope6_ids[ifp->if_index].s6id_list[i] = idlist[i];
206 scope6_get(ifp, idlist)
210 if (scope6_ids == NULL) /* paranoid? */
213 bcopy(scope6_ids[ifp->if_index].s6id_list, idlist,
214 sizeof(scope6_ids[ifp->if_index].s6id_list));
221 * Get a scope of the address. Node-local, link-local, site-local or global.
225 struct in6_addr *addr;
229 if (addr->s6_addr[0] == 0xfe) {
230 scope = addr->s6_addr[1] & 0xc0;
234 return IPV6_ADDR_SCOPE_LINKLOCAL;
237 return IPV6_ADDR_SCOPE_SITELOCAL;
240 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
246 if (addr->s6_addr[0] == 0xff) {
247 scope = addr->s6_addr[1] & 0x0f;
250 * due to other scope such as reserved,
251 * return scope doesn't work.
254 case IPV6_ADDR_SCOPE_INTFACELOCAL:
255 return IPV6_ADDR_SCOPE_INTFACELOCAL;
257 case IPV6_ADDR_SCOPE_LINKLOCAL:
258 return IPV6_ADDR_SCOPE_LINKLOCAL;
260 case IPV6_ADDR_SCOPE_SITELOCAL:
261 return IPV6_ADDR_SCOPE_SITELOCAL;
264 return IPV6_ADDR_SCOPE_GLOBAL;
270 * Regard loopback and unspecified addresses as global, since
271 * they have no ambiguity.
273 if (bcmp(&in6addr_loopback, addr, sizeof(addr) - 1) == 0) {
274 if (addr->s6_addr[15] == 1) /* loopback */
275 return IPV6_ADDR_SCOPE_LINKLOCAL;
276 if (addr->s6_addr[15] == 0) /* unspecified */
277 return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
280 return IPV6_ADDR_SCOPE_GLOBAL;
284 * When we introduce the "4+28" split semantics in sin6_scope_id,
285 * a 32bit integer is not enough to tell a large ID from an error (-1).
286 * So, we intentionally use a large type as the return value.
289 in6_addr2zoneid(ifp, addr)
290 struct ifnet *ifp; /* must not be NULL */
291 struct in6_addr *addr; /* must not be NULL */
296 if (scope6_ids == NULL) { /* should not happen */
297 panic("in6_addr2zoneid: scope array is NULL");
300 if (ifp->if_index >= if_indexlim) {
301 panic("in6_addr2zoneid: invalid interface");
307 * special case: the loopback address can only belong to a loopback
310 if (IN6_IS_ADDR_LOOPBACK(addr)) {
311 if (!(ifp->if_flags & IFF_LOOPBACK))
314 return(0); /* there's no ambiguity */
317 scope = in6_addrscope(addr);
319 #define SID scope6_ids[ifp->if_index]
321 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
322 return(SID.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL]);
324 case IPV6_ADDR_SCOPE_LINKLOCAL:
325 return(SID.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]);
327 case IPV6_ADDR_SCOPE_SITELOCAL:
328 return(SID.s6id_list[IPV6_ADDR_SCOPE_SITELOCAL]);
330 case IPV6_ADDR_SCOPE_ORGLOCAL:
331 return(SID.s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL]);
334 return(0); /* XXX: treat as global. */
340 scope6_setdefault(ifp)
341 struct ifnet *ifp; /* note that this might be NULL */
344 * Currently, this function just set the default "interfaces"
345 * and "links" according to the given interface.
346 * We might eventually have to separate the notion of "link" from
347 * "interface" and provide a user interface to set the default.
350 scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
352 scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
356 scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
357 scope6_ids[0].s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
362 scope6_get_default(idlist)
365 if (scope6_ids == NULL) /* paranoid? */
368 bcopy(scope6_ids[0].s6id_list, idlist,
369 sizeof(scope6_ids[0].s6id_list));
375 scope6_addr2default(addr)
376 struct in6_addr *addr;
379 * special case: The loopback address should be considered as
380 * link-local, but there's no ambiguity in the syntax.
382 if (IN6_IS_ADDR_LOOPBACK(addr))
385 return(scope6_ids[0].s6id_list[in6_addrscope(addr)]);
389 * XXX: some applications assume the kernel guesses a correct zone ID when the
390 * outgoing interface is explicitly specified, and omit specifying the ID.
391 * This is a bad manner, but we handle such cases anyway.
392 * Note that this function intentionally overrides the argument SIN6.
395 scope6_setzoneid(ifp, sin6)
397 struct sockaddr_in6 *sin6;
402 zoneid = in6_addr2zoneid(ifp, &sin6->sin6_addr);
407 sin6->sin6_scope_id = zoneid;
408 if ((error = in6_embedscope(&sin6->sin6_addr, sin6)) != 0)
416 * Check if the zone ID in SIN6 is valid according to the scope of the
417 * address. If DFEFAULTOK is true and the zone ID is unspecified, fill the
418 * default value for the scope type in the ID field.
419 * This function also embeds the zone ID into the 128bit address field if
420 * necessary. This format is internally used in the kernel.
421 * As a result, it is ensured that SIN6 has a valid scope zone ID and the
422 * address part is converted to the internal form.
424 * The caller must expect that SIN6 can be modified within this function;
425 * if it is not desired to override the original value a local copy must be
429 scope6_check_id(sin6, defaultok)
430 struct sockaddr_in6 *sin6;
434 struct in6_addr *in6 = &sin6->sin6_addr;
437 if ((zoneid = sin6->sin6_scope_id) != 0) {
439 * At this moment, we only check interface-local and
440 * link-local scope IDs, and use interface indices as the
441 * zone IDs assuming a one-to-one mapping between interfaces
443 * XXX: in6_embedscope() below does the same check (in case
444 * of !SCOPEDROUTING). We should eventually centralize the
445 * check in this function.
447 if (IN6_IS_SCOPE_LINKLOCAL(in6) ||
448 IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
449 if (if_index < zoneid)
451 #if defined(__FreeBSD__) && __FreeBSD__ >= 5
452 ifp = ifnet_byindex(zoneid);
454 ifp = ifindex2ifnet[zoneid];
456 if (ifp == NULL) /* XXX: this can happen for some OS */
459 } else if (defaultok)
460 sin6->sin6_scope_id = scope6_addr2default(in6);
462 /* KAME hack: embed scopeid */
463 if (in6_embedscope(in6, sin6) != 0)