1 /* $FreeBSD: src/sys/netinet6/scope6.c,v 1.1.2.3 2002/04/01 15:29:04 ume Exp $ */
2 /* $DragonFly: src/sys/netinet6/scope6.c,v 1.11 2008/01/05 14:02:40 swildner Exp $ */
3 /* $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $ */
6 * Copyright (C) 2000 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
39 #include <sys/queue.h>
40 #include <sys/thread2.h>
42 #include <net/route.h>
45 #include <netinet/in.h>
47 #include <netinet6/in6_var.h>
48 #include <netinet6/scope6_var.h>
50 static struct scope6_id sid_default
;
54 SID(struct ifnet
*ifp
)
56 struct in6_ifextra
*xtra
;
58 xtra
= ifp
->if_afdata
[AF_INET6
];
60 return (xtra
->scope6_id
);
67 bzero(&sid_default
, sizeof(sid_default
));
71 scope6_ifattach(struct ifnet
*ifp
)
73 struct scope6_id
*sid
;
75 sid
= (struct scope6_id
*)kmalloc(sizeof(*sid
), M_IFADDR
,
81 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
82 * Should we rather hardcode here?
84 sid
->s6id_list
[IPV6_ADDR_SCOPE_INTFACELOCAL
] = ifp
->if_index
;
85 sid
->s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
] = ifp
->if_index
;
87 /* by default, we don't care about scope boundary for these scopes. */
88 sid
->s6id_list
[IPV6_ADDR_SCOPE_SITELOCAL
] = 1;
89 sid
->s6id_list
[IPV6_ADDR_SCOPE_ORGLOCAL
] = 1;
97 scope6_ifdetach(struct scope6_id
*sid
)
103 scope6_set(struct ifnet
*ifp
, struct scope6_id
*idlist
)
107 struct scope6_id
*sid
= SID(ifp
);
109 if (!sid
) /* paranoid? */
113 * XXX: We need more consistency checks of the relationship among
114 * scopes (e.g. an organization should be larger than a site).
118 * TODO(XXX): after setting, we should reflect the changes to
119 * interface addresses, routing table entries, PCB entries...
124 for (i
= 0; i
< 16; i
++) {
125 if (idlist
->s6id_list
[i
] &&
126 idlist
->s6id_list
[i
] != sid
->s6id_list
[i
]) {
128 * An interface zone ID must be the corresponding
129 * interface index by definition.
131 if (i
== IPV6_ADDR_SCOPE_INTFACELOCAL
&&
132 idlist
->s6id_list
[i
] != ifp
->if_index
) {
137 if (i
== IPV6_ADDR_SCOPE_LINKLOCAL
&&
138 idlist
->s6id_list
[i
] > if_index
) {
140 * XXX: theoretically, there should be no
141 * relationship between link IDs and interface
142 * IDs, but we check the consistency for
143 * safety in later use.
150 * XXX: we must need lots of work in this case,
151 * but we simply set the new value in this initial
154 sid
->s6id_list
[i
] = idlist
->s6id_list
[i
];
163 scope6_get(struct ifnet
*ifp
, struct scope6_id
*idlist
)
165 struct scope6_id
*sid
= SID(ifp
);
167 if (sid
== NULL
) /* paranoid? */
177 * Get a scope of the address. Node-local, link-local, site-local or global.
180 in6_addrscope(struct in6_addr
*addr
)
184 if (addr
->s6_addr
[0] == 0xfe) {
185 scope
= addr
->s6_addr
[1] & 0xc0;
189 return IPV6_ADDR_SCOPE_LINKLOCAL
;
192 return IPV6_ADDR_SCOPE_SITELOCAL
;
195 return IPV6_ADDR_SCOPE_GLOBAL
; /* just in case */
201 if (addr
->s6_addr
[0] == 0xff) {
202 scope
= addr
->s6_addr
[1] & 0x0f;
205 * due to other scope such as reserved,
206 * return scope doesn't work.
209 case IPV6_ADDR_SCOPE_INTFACELOCAL
:
210 return IPV6_ADDR_SCOPE_INTFACELOCAL
;
212 case IPV6_ADDR_SCOPE_LINKLOCAL
:
213 return IPV6_ADDR_SCOPE_LINKLOCAL
;
215 case IPV6_ADDR_SCOPE_SITELOCAL
:
216 return IPV6_ADDR_SCOPE_SITELOCAL
;
219 return IPV6_ADDR_SCOPE_GLOBAL
;
225 * Regard loopback and unspecified addresses as global, since
226 * they have no ambiguity.
228 if (bcmp(&kin6addr_loopback
, addr
, sizeof(*addr
) - 1) == 0) {
229 if (addr
->s6_addr
[15] == 1) /* loopback */
230 return IPV6_ADDR_SCOPE_LINKLOCAL
;
231 if (addr
->s6_addr
[15] == 0) /* unspecified */
232 return IPV6_ADDR_SCOPE_GLOBAL
; /* XXX: correct? */
235 return IPV6_ADDR_SCOPE_GLOBAL
;
239 * When we introduce the "4+28" split semantics in sin6_scope_id,
240 * a 32bit integer is not enough to tell a large ID from an error (-1).
241 * So, we intentionally use a large type as the return value.
244 in6_addr2zoneid(struct ifnet
*ifp
, /* must not be NULL */
245 struct in6_addr
*addr
, /* must not be NULL */
246 u_int32_t
*ret_id
) /* must not be NULL */
249 u_int32_t zoneid
= 0;
250 struct scope6_id
*sid
= SID(ifp
);
253 if (sid
== NULL
) { /* should not happen */
254 panic("in6_addr2zoneid: scope array is NULL");
257 if (ret_id
== NULL
) {
258 panic("in6_addr2zoneid: return ID is null");
264 * special case: the loopback address can only belong to a loopback
267 if (IN6_IS_ADDR_LOOPBACK(addr
)) {
268 if (!(ifp
->if_flags
& IFF_LOOPBACK
)) {
271 *ret_id
= 0; /* there's no ambiguity */
276 scope
= in6_addrscope(addr
);
279 case IPV6_ADDR_SCOPE_INTFACELOCAL
: /* should be interface index */
280 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_INTFACELOCAL
];
283 case IPV6_ADDR_SCOPE_LINKLOCAL
:
284 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
];
287 case IPV6_ADDR_SCOPE_SITELOCAL
:
288 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_SITELOCAL
];
291 case IPV6_ADDR_SCOPE_ORGLOCAL
:
292 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_ORGLOCAL
];
296 zoneid
= 0; /* XXX: treat as global. */
304 scope6_setdefault(struct ifnet
*ifp
) /* note that this might be NULL */
307 * Currently, this function just set the default "interfaces"
308 * and "links" according to the given interface.
309 * We might eventually have to separate the notion of "link" from
310 * "interface" and provide a user interface to set the default.
313 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_INTFACELOCAL
] =
315 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
] =
318 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_INTFACELOCAL
] = 0;
319 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
] = 0;
324 scope6_get_default(struct scope6_id
*idlist
)
326 *idlist
= sid_default
;
332 scope6_addr2default(struct in6_addr
*addr
)
335 * special case: The loopback address should be considered as
336 * link-local, but there's no ambiguity in the syntax.
338 if (IN6_IS_ADDR_LOOPBACK(addr
))
341 return (sid_default
.s6id_list
[in6_addrscope(addr
)]);
345 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
346 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
347 * in the in6_addr structure, in6 will be modified.
350 in6_setscope(struct in6_addr
*in6
, struct ifnet
*ifp
, u_int32_t
*ret_id
)
353 u_int32_t zoneid
= 0;
354 struct scope6_id
*sid
;
356 ifnet_serialize_all(ifp
);
361 if (sid
== NULL
) { /* should not happen */
362 panic("in6_setscope: scope array is NULL");
368 * special case: the loopback address can only belong to a loopback
371 if (IN6_IS_ADDR_LOOPBACK(in6
)) {
372 if (!(ifp
->if_flags
& IFF_LOOPBACK
)) {
373 ifnet_deserialize_all(ifp
);
377 *ret_id
= 0; /* there's no ambiguity */
378 ifnet_deserialize_all(ifp
);
383 scope
= in6_addrscope(in6
);
386 case IPV6_ADDR_SCOPE_NODELOCAL
: /* should be interface index */
387 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_NODELOCAL
];
390 case IPV6_ADDR_SCOPE_LINKLOCAL
:
391 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
];
394 case IPV6_ADDR_SCOPE_SITELOCAL
:
395 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_SITELOCAL
];
398 case IPV6_ADDR_SCOPE_ORGLOCAL
:
399 zoneid
= sid
->s6id_list
[IPV6_ADDR_SCOPE_ORGLOCAL
];
403 zoneid
= 0; /* XXX: treat as global. */
407 ifnet_deserialize_all(ifp
);
412 if (IN6_IS_SCOPE_LINKLOCAL(in6
) || IN6_IS_ADDR_MC_NODELOCAL(in6
) )
413 in6
->s6_addr16
[1] = htons(zoneid
& 0xffff); /* XXX */