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/sockio.h>
40 #include <sys/systm.h>
41 #include <sys/queue.h>
42 #include <sys/syslog.h>
47 #include <netinet/in.h>
49 #include <netinet/ip6.h>
50 #include <netinet6/in6_var.h>
51 #include <netinet6/ip6_var.h>
52 #include <netinet6/scope6_var.h>
54 #ifdef ENABLE_DEFAULT_SCOPE
55 VNET_DEFINE(int, ip6_use_defzone) = 1;
57 VNET_DEFINE(int, ip6_use_defzone) = 0;
61 * The scope6_lock protects the global sid default stored in
64 static struct mtx scope6_lock;
65 #define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
66 #define SCOPE6_LOCK() mtx_lock(&scope6_lock)
67 #define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
68 #define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
70 static VNET_DEFINE(struct scope6_id, sid_default);
71 #define V_sid_default VNET(sid_default)
74 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
76 static int scope6_get(struct ifnet *, struct scope6_id *);
77 static int scope6_set(struct ifnet *, struct scope6_id *);
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_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
124 struct in6_ifreq *ifr;
126 if (ifp->if_afdata[AF_INET6] == NULL)
127 return (EPFNOSUPPORT);
129 ifr = (struct in6_ifreq *)data;
132 return (scope6_set(ifp,
133 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
135 return (scope6_get(ifp,
136 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
138 return (scope6_get_default(
139 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
146 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
150 struct scope6_id *sid = NULL;
152 IF_AFDATA_WLOCK(ifp);
155 if (!sid) { /* paranoid? */
156 IF_AFDATA_WUNLOCK(ifp);
161 * XXX: We need more consistency checks of the relationship among
162 * scopes (e.g. an organization should be larger than a site).
166 * TODO(XXX): after setting, we should reflect the changes to
167 * interface addresses, routing table entries, PCB entries...
170 for (i = 0; i < 16; i++) {
171 if (idlist->s6id_list[i] &&
172 idlist->s6id_list[i] != sid->s6id_list[i]) {
174 * An interface zone ID must be the corresponding
175 * interface index by definition.
177 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
178 idlist->s6id_list[i] != ifp->if_index) {
179 IF_AFDATA_WUNLOCK(ifp);
183 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
184 idlist->s6id_list[i] > V_if_index) {
186 * XXX: theoretically, there should be no
187 * relationship between link IDs and interface
188 * IDs, but we check the consistency for
189 * safety in later use.
191 IF_AFDATA_WUNLOCK(ifp);
196 * XXX: we must need lots of work in this case,
197 * but we simply set the new value in this initial
200 sid->s6id_list[i] = idlist->s6id_list[i];
203 IF_AFDATA_WUNLOCK(ifp);
209 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
211 struct scope6_id *sid;
213 /* We only need to lock the interface's afdata for SID() to work. */
214 IF_AFDATA_RLOCK(ifp);
216 if (sid == NULL) { /* paranoid? */
217 IF_AFDATA_RUNLOCK(ifp);
223 IF_AFDATA_RUNLOCK(ifp);
228 * Get a scope of the address. Node-local, link-local, site-local or global.
231 in6_addrscope(struct in6_addr *addr)
235 if (addr->s6_addr[0] == 0xfe) {
236 scope = addr->s6_addr[1] & 0xc0;
240 return IPV6_ADDR_SCOPE_LINKLOCAL;
243 return IPV6_ADDR_SCOPE_SITELOCAL;
246 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
252 if (addr->s6_addr[0] == 0xff) {
253 scope = addr->s6_addr[1] & 0x0f;
256 * due to other scope such as reserved,
257 * return scope doesn't work.
260 case IPV6_ADDR_SCOPE_INTFACELOCAL:
261 return IPV6_ADDR_SCOPE_INTFACELOCAL;
263 case IPV6_ADDR_SCOPE_LINKLOCAL:
264 return IPV6_ADDR_SCOPE_LINKLOCAL;
266 case IPV6_ADDR_SCOPE_SITELOCAL:
267 return IPV6_ADDR_SCOPE_SITELOCAL;
270 return IPV6_ADDR_SCOPE_GLOBAL;
276 * Regard loopback and unspecified addresses as global, since
277 * they have no ambiguity.
279 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
280 if (addr->s6_addr[15] == 1) /* loopback */
281 return IPV6_ADDR_SCOPE_LINKLOCAL;
282 if (addr->s6_addr[15] == 0) /* unspecified */
283 return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
286 return IPV6_ADDR_SCOPE_GLOBAL;
290 * ifp - note that this might be NULL
294 scope6_setdefault(struct ifnet *ifp)
298 * Currently, this function just sets the default "interfaces"
299 * and "links" according to the given interface.
300 * We might eventually have to separate the notion of "link" from
301 * "interface" and provide a user interface to set the default.
305 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
307 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
310 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
311 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
317 scope6_get_default(struct scope6_id *idlist)
321 *idlist = V_sid_default;
328 scope6_addr2default(struct in6_addr *addr)
333 * special case: The loopback address should be considered as
334 * link-local, but there's no ambiguity in the syntax.
336 if (IN6_IS_ADDR_LOOPBACK(addr))
340 * XXX: 32-bit read is atomic on all our platforms, is it OK
344 id = V_sid_default.s6id_list[in6_addrscope(addr)];
350 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
351 * is unspecified (=0), needs to be specified, and the default zone ID can be
352 * used, the default value will be used.
353 * This routine then generates the kernel-internal form: if the address scope
354 * of is interface-local or link-local, embed the interface index in the
358 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
363 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
364 zoneid = scope6_addr2default(&sin6->sin6_addr);
367 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
368 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
370 * At this moment, we only check interface-local and
371 * link-local scope IDs, and use interface indices as the
372 * zone IDs assuming a one-to-one mapping between interfaces
375 if (V_if_index < zoneid)
377 ifp = ifnet_byindex(zoneid);
378 if (ifp == NULL) /* XXX: this can happen for some OS */
381 /* XXX assignment to 16bit from 32bit variable */
382 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
384 sin6->sin6_scope_id = 0;
391 * generate standard sockaddr_in6 from embedded form.
394 sa6_recoverscope(struct sockaddr_in6 *sin6)
396 char ip6buf[INET6_ADDRSTRLEN];
399 if (sin6->sin6_scope_id != 0) {
401 "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
402 ip6_sprintf(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id);
403 /* XXX: proceed anyway... */
405 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
406 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
408 * KAME assumption: link id == interface id
410 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
413 if (V_if_index < zoneid)
415 if (!ifnet_byindex(zoneid))
417 sin6->sin6_addr.s6_addr16[1] = 0;
418 sin6->sin6_scope_id = zoneid;
426 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
427 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
428 * in the in6_addr structure, in6 will be modified.
430 * ret_id - unnecessary?
433 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
436 u_int32_t zoneid = 0;
437 struct scope6_id *sid;
439 IF_AFDATA_RLOCK(ifp);
444 if (sid == NULL) { /* should not happen */
445 panic("in6_setscope: scope array is NULL");
451 * special case: the loopback address can only belong to a loopback
454 if (IN6_IS_ADDR_LOOPBACK(in6)) {
455 if (!(ifp->if_flags & IFF_LOOPBACK)) {
456 IF_AFDATA_RUNLOCK(ifp);
460 *ret_id = 0; /* there's no ambiguity */
461 IF_AFDATA_RUNLOCK(ifp);
466 scope = in6_addrscope(in6);
468 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
469 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
472 case IPV6_ADDR_SCOPE_LINKLOCAL:
473 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
476 case IPV6_ADDR_SCOPE_SITELOCAL:
477 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
480 case IPV6_ADDR_SCOPE_ORGLOCAL:
481 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
485 zoneid = 0; /* XXX: treat as global. */
488 IF_AFDATA_RUNLOCK(ifp);
493 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
494 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
500 * Just clear the embedded scope identifier. Return 0 if the original address
501 * is intact; return non 0 if the address is modified.
504 in6_clearscope(struct in6_addr *in6)
508 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
509 if (in6->s6_addr16[1] != 0)
511 in6->s6_addr16[1] = 0;
518 * Return the scope identifier or zero.
521 in6_getscope(struct in6_addr *in6)
524 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
525 return (in6->s6_addr16[1]);