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>
42 #include <sys/vimage.h>
47 #include <netinet/in.h>
49 #include <netinet/ip6.h>
50 #include <netinet6/in6_var.h>
51 #include <netinet6/scope6_var.h>
52 #include <netinet6/vinet6.h>
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)
66 static struct scope6_id sid_default;
71 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
76 INIT_VNET_INET6(curvnet);
78 #ifdef ENABLE_DEFAULT_SCOPE
79 V_ip6_use_defzone = 1;
81 V_ip6_use_defzone = 0;
83 bzero(&V_sid_default, sizeof(V_sid_default));
85 if (!IS_DEFAULT_VNET(curvnet))
92 scope6_ifattach(struct ifnet *ifp)
94 struct scope6_id *sid;
96 sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
97 bzero(sid, sizeof(*sid));
100 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
101 * Should we rather hardcode here?
103 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
104 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
106 /* by default, we don't care about scope boundary for these scopes. */
107 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
108 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
115 scope6_ifdetach(struct scope6_id *sid)
122 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
124 INIT_VNET_NET(ifp->if_vnet);
127 struct scope6_id *sid = NULL;
132 if (!sid) { /* paranoid? */
133 IF_AFDATA_UNLOCK(ifp);
138 * XXX: We need more consistency checks of the relationship among
139 * scopes (e.g. an organization should be larger than a site).
143 * TODO(XXX): after setting, we should reflect the changes to
144 * interface addresses, routing table entries, PCB entries...
148 for (i = 0; i < 16; i++) {
149 if (idlist->s6id_list[i] &&
150 idlist->s6id_list[i] != sid->s6id_list[i]) {
152 * An interface zone ID must be the corresponding
153 * interface index by definition.
155 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
156 idlist->s6id_list[i] != ifp->if_index) {
157 IF_AFDATA_UNLOCK(ifp);
162 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
163 idlist->s6id_list[i] > V_if_index) {
165 * XXX: theoretically, there should be no
166 * relationship between link IDs and interface
167 * IDs, but we check the consistency for
168 * safety in later use.
170 IF_AFDATA_UNLOCK(ifp);
176 * XXX: we must need lots of work in this case,
177 * but we simply set the new value in this initial
180 sid->s6id_list[i] = idlist->s6id_list[i];
184 IF_AFDATA_UNLOCK(ifp);
190 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
192 /* We only need to lock the interface's afdata for SID() to work. */
194 struct scope6_id *sid = SID(ifp);
196 if (sid == NULL) { /* paranoid? */
197 IF_AFDATA_UNLOCK(ifp);
205 IF_AFDATA_UNLOCK(ifp);
211 * Get a scope of the address. Node-local, link-local, site-local or global.
214 in6_addrscope(struct in6_addr *addr)
218 if (addr->s6_addr[0] == 0xfe) {
219 scope = addr->s6_addr[1] & 0xc0;
223 return IPV6_ADDR_SCOPE_LINKLOCAL;
226 return IPV6_ADDR_SCOPE_SITELOCAL;
229 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
235 if (addr->s6_addr[0] == 0xff) {
236 scope = addr->s6_addr[1] & 0x0f;
239 * due to other scope such as reserved,
240 * return scope doesn't work.
243 case IPV6_ADDR_SCOPE_INTFACELOCAL:
244 return IPV6_ADDR_SCOPE_INTFACELOCAL;
246 case IPV6_ADDR_SCOPE_LINKLOCAL:
247 return IPV6_ADDR_SCOPE_LINKLOCAL;
249 case IPV6_ADDR_SCOPE_SITELOCAL:
250 return IPV6_ADDR_SCOPE_SITELOCAL;
253 return IPV6_ADDR_SCOPE_GLOBAL;
259 * Regard loopback and unspecified addresses as global, since
260 * they have no ambiguity.
262 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
263 if (addr->s6_addr[15] == 1) /* loopback */
264 return IPV6_ADDR_SCOPE_LINKLOCAL;
265 if (addr->s6_addr[15] == 0) /* unspecified */
266 return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
269 return IPV6_ADDR_SCOPE_GLOBAL;
273 * ifp - note that this might be NULL
277 scope6_setdefault(struct ifnet *ifp)
279 INIT_VNET_INET6(ifp->if_vnet);
282 * Currently, this function just sets the default "interfaces"
283 * and "links" according to the given interface.
284 * We might eventually have to separate the notion of "link" from
285 * "interface" and provide a user interface to set the default.
289 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
291 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
294 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
295 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
301 scope6_get_default(struct scope6_id *idlist)
303 INIT_VNET_INET6(curvnet);
306 *idlist = V_sid_default;
313 scope6_addr2default(struct in6_addr *addr)
315 INIT_VNET_INET6(curvnet);
319 * special case: The loopback address should be considered as
320 * link-local, but there's no ambiguity in the syntax.
322 if (IN6_IS_ADDR_LOOPBACK(addr))
326 * XXX: 32-bit read is atomic on all our platforms, is it OK
330 id = V_sid_default.s6id_list[in6_addrscope(addr)];
336 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
337 * is unspecified (=0), needs to be specified, and the default zone ID can be
338 * used, the default value will be used.
339 * This routine then generates the kernel-internal form: if the address scope
340 * of is interface-local or link-local, embed the interface index in the
344 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
346 INIT_VNET_NET(curvnet);
350 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
351 zoneid = scope6_addr2default(&sin6->sin6_addr);
354 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
355 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
357 * At this moment, we only check interface-local and
358 * link-local scope IDs, and use interface indices as the
359 * zone IDs assuming a one-to-one mapping between interfaces
362 if (V_if_index < zoneid)
364 ifp = ifnet_byindex(zoneid);
365 if (ifp == NULL) /* XXX: this can happen for some OS */
368 /* XXX assignment to 16bit from 32bit variable */
369 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
371 sin6->sin6_scope_id = 0;
378 * generate standard sockaddr_in6 from embedded form.
381 sa6_recoverscope(struct sockaddr_in6 *sin6)
383 INIT_VNET_NET(curvnet);
384 char ip6buf[INET6_ADDRSTRLEN];
387 if (sin6->sin6_scope_id != 0) {
389 "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
390 ip6_sprintf(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id);
391 /* XXX: proceed anyway... */
393 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
394 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
396 * KAME assumption: link id == interface id
398 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
401 if (zoneid < 0 || V_if_index < zoneid)
403 if (!ifnet_byindex(zoneid))
405 sin6->sin6_addr.s6_addr16[1] = 0;
406 sin6->sin6_scope_id = zoneid;
414 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
415 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
416 * in the in6_addr structure, in6 will be modified.
418 * ret_id - unnecessary?
421 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
424 u_int32_t zoneid = 0;
425 struct scope6_id *sid;
432 if (sid == NULL) { /* should not happen */
433 panic("in6_setscope: scope array is NULL");
439 * special case: the loopback address can only belong to a loopback
442 if (IN6_IS_ADDR_LOOPBACK(in6)) {
443 if (!(ifp->if_flags & IFF_LOOPBACK)) {
444 IF_AFDATA_UNLOCK(ifp);
448 *ret_id = 0; /* there's no ambiguity */
449 IF_AFDATA_UNLOCK(ifp);
454 scope = in6_addrscope(in6);
458 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
459 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
462 case IPV6_ADDR_SCOPE_LINKLOCAL:
463 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
466 case IPV6_ADDR_SCOPE_SITELOCAL:
467 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
470 case IPV6_ADDR_SCOPE_ORGLOCAL:
471 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
475 zoneid = 0; /* XXX: treat as global. */
479 IF_AFDATA_UNLOCK(ifp);
484 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
485 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
491 * Just clear the embedded scope identifier. Return 0 if the original address
492 * is intact; return non 0 if the address is modified.
495 in6_clearscope(struct in6_addr *in6)
499 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
500 if (in6->s6_addr16[1] != 0)
502 in6->s6_addr16[1] = 0;