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.9 2006/10/24 06:18:42 hsu 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
;
52 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
57 bzero(&sid_default
, sizeof(sid_default
));
61 scope6_ifattach(struct ifnet
*ifp
)
63 struct scope6_id
*sid
;
65 sid
= (struct scope6_id
*)kmalloc(sizeof(*sid
), M_IFADDR
, M_WAITOK
);
66 bzero(sid
, sizeof(*sid
));
71 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
72 * Should we rather hardcode here?
74 sid
->s6id_list
[IPV6_ADDR_SCOPE_NODELOCAL
] = ifp
->if_index
;
75 sid
->s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
] = ifp
->if_index
;
77 /* by default, we don't care about scope boundary for these scopes. */
78 sid
->s6id_list
[IPV6_ADDR_SCOPE_SITELOCAL
] = 1;
79 sid
->s6id_list
[IPV6_ADDR_SCOPE_ORGLOCAL
] = 1;
87 scope6_ifdetach(struct scope6_id
*sid
)
93 scope6_set(struct ifnet
*ifp
, struct scope6_id
*idlist
)
97 struct scope6_id
*sid
= SID(ifp
);
99 if (!sid
) /* paranoid? */
103 * XXX: We need more consistency checks of the relationship among
104 * scopes (e.g. an organization should be larger than a site).
108 * TODO(XXX): after setting, we should reflect the changes to
109 * interface addresses, routing table entries, PCB entries...
114 for (i
= 0; i
< 16; i
++) {
115 if (idlist
->s6id_list
[i
] &&
116 idlist
->s6id_list
[i
] != sid
->s6id_list
[i
]) {
117 if (i
== IPV6_ADDR_SCOPE_LINKLOCAL
&&
118 idlist
->s6id_list
[i
] > if_index
) {
120 * XXX: theoretically, there should be no
121 * relationship between link IDs and interface
122 * IDs, but we check the consistency for
123 * safety in later use.
130 * XXX: we must need lots of work in this case,
131 * but we simply set the new value in this initial
134 sid
->s6id_list
[i
] = idlist
->s6id_list
[i
];
143 scope6_get(struct ifnet
*ifp
, struct scope6_id
*idlist
)
145 struct scope6_id
*sid
= SID(ifp
);
147 if (sid
== NULL
) /* paranoid? */
157 * Get a scope of the address. Node-local, link-local, site-local or global.
160 in6_addrscope(struct in6_addr
*addr
)
164 if (addr
->s6_addr8
[0] == 0xfe) {
165 scope
= addr
->s6_addr8
[1] & 0xc0;
169 return IPV6_ADDR_SCOPE_LINKLOCAL
;
172 return IPV6_ADDR_SCOPE_SITELOCAL
;
175 return IPV6_ADDR_SCOPE_GLOBAL
; /* just in case */
181 if (addr
->s6_addr8
[0] == 0xff) {
182 scope
= addr
->s6_addr8
[1] & 0x0f;
185 * due to other scope such as reserved,
186 * return scope doesn't work.
189 case IPV6_ADDR_SCOPE_NODELOCAL
:
190 return IPV6_ADDR_SCOPE_NODELOCAL
;
192 case IPV6_ADDR_SCOPE_LINKLOCAL
:
193 return IPV6_ADDR_SCOPE_LINKLOCAL
;
195 case IPV6_ADDR_SCOPE_SITELOCAL
:
196 return IPV6_ADDR_SCOPE_SITELOCAL
;
199 return IPV6_ADDR_SCOPE_GLOBAL
;
204 if (bcmp(&kin6addr_loopback
, addr
, sizeof(*addr
) - 1) == 0) {
205 if (addr
->s6_addr8
[15] == 1) /* loopback */
206 return IPV6_ADDR_SCOPE_NODELOCAL
;
207 if (addr
->s6_addr8
[15] == 0) /* unspecified */
208 return IPV6_ADDR_SCOPE_LINKLOCAL
;
211 return IPV6_ADDR_SCOPE_GLOBAL
;
215 in6_addr2scopeid(struct ifnet
*ifp
, /* must not be NULL */
216 struct in6_addr
*addr
) /* must not be NULL */
219 struct scope6_id
*sid
= SID(ifp
);
222 if (sid
== NULL
) { /* should not happen */
223 panic("in6_addr2zoneid: scope array is NULL");
229 * special case: the loopback address can only belong to a loopback
232 if (IN6_IS_ADDR_LOOPBACK(addr
)) {
233 if (!(ifp
->if_flags
& IFF_LOOPBACK
))
236 return (0); /* there's no ambiguity */
239 scope
= in6_addrscope(addr
);
242 case IPV6_ADDR_SCOPE_NODELOCAL
:
243 return (-1); /* XXX: is this an appropriate value? */
245 case IPV6_ADDR_SCOPE_LINKLOCAL
:
246 return (sid
->s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
]);
248 case IPV6_ADDR_SCOPE_SITELOCAL
:
249 return (sid
->s6id_list
[IPV6_ADDR_SCOPE_SITELOCAL
]);
251 case IPV6_ADDR_SCOPE_ORGLOCAL
:
252 return (sid
->s6id_list
[IPV6_ADDR_SCOPE_ORGLOCAL
]);
255 return (0); /* XXX: treat as global. */
260 scope6_setdefault(struct ifnet
*ifp
) /* note that this might be NULL */
263 * Currently, this function just set the default "interfaces"
264 * and "links" according to the given interface.
265 * We might eventually have to separate the notion of "link" from
266 * "interface" and provide a user interface to set the default.
269 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_NODELOCAL
] =
271 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
] =
274 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_NODELOCAL
] = 0;
275 sid_default
.s6id_list
[IPV6_ADDR_SCOPE_LINKLOCAL
] = 0;
280 scope6_get_default(struct scope6_id
*idlist
)
282 *idlist
= sid_default
;
288 scope6_addr2default(struct in6_addr
*addr
)
291 * special case: The loopback address should be considered as
292 * link-local, but there's no ambiguity in the syntax.
294 if (IN6_IS_ADDR_LOOPBACK(addr
))
297 return (sid_default
.s6id_list
[in6_addrscope(addr
)]);