Bring CARP into the tree. CARP = Common Address Redundancy Protocol, which
[dragonfly.git] / sys / netinet6 / scope6.c
CommitLineData
984263bc 1/* $FreeBSD: src/sys/netinet6/scope6.c,v 1.1.2.3 2002/04/01 15:29:04 ume Exp $ */
0d16ba1d 2/* $DragonFly: src/sys/netinet6/scope6.c,v 1.10 2007/08/16 20:03:58 dillon Exp $ */
984263bc
MD
3/* $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $ */
4
5/*
6 * Copyright (C) 2000 WIDE Project.
7 * All rights reserved.
bde3511a 8 *
984263bc
MD
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
bde3511a 20 *
984263bc
MD
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
31 * SUCH DAMAGE.
32 */
33
34#include <sys/param.h>
35#include <sys/malloc.h>
36#include <sys/mbuf.h>
37#include <sys/socket.h>
38#include <sys/systm.h>
39#include <sys/queue.h>
abf782b3 40#include <sys/thread2.h>
984263bc
MD
41
42#include <net/route.h>
43#include <net/if.h>
44
45#include <netinet/in.h>
46
47#include <netinet6/in6_var.h>
48#include <netinet6/scope6_var.h>
49
698ac46c
HS
50static struct scope6_id sid_default;
51#define SID(ifp) \
52 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
984263bc
MD
53
54void
0bdb1448 55scope6_init(void)
698ac46c
HS
56{
57 bzero(&sid_default, sizeof(sid_default));
58}
59
60struct scope6_id *
122ebd49 61scope6_ifattach(struct ifnet *ifp)
984263bc 62{
698ac46c 63 struct scope6_id *sid;
984263bc 64
efda3bd0 65 sid = (struct scope6_id *)kmalloc(sizeof(*sid), M_IFADDR, M_WAITOK);
698ac46c 66 bzero(sid, sizeof(*sid));
984263bc 67
abf782b3
EN
68 crit_enter();
69
984263bc
MD
70 /*
71 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
72 * Should we rather hardcode here?
73 */
698ac46c
HS
74 sid->s6id_list[IPV6_ADDR_SCOPE_NODELOCAL] = ifp->if_index;
75 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
984263bc
MD
76#ifdef MULTI_SCOPE
77 /* by default, we don't care about scope boundary for these scopes. */
698ac46c
HS
78 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
79 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
984263bc 80#endif
984263bc 81
abf782b3 82 crit_exit();
698ac46c
HS
83 return sid;
84}
85
86void
87scope6_ifdetach(struct scope6_id *sid)
88{
efda3bd0 89 kfree(sid, M_IFADDR);
984263bc
MD
90}
91
92int
698ac46c 93scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
984263bc 94{
abf782b3 95 int i;
984263bc 96 int error = 0;
698ac46c 97 struct scope6_id *sid = SID(ifp);
984263bc 98
698ac46c 99 if (!sid) /* paranoid? */
bde3511a 100 return (EINVAL);
984263bc
MD
101
102 /*
103 * XXX: We need more consistency checks of the relationship among
104 * scopes (e.g. an organization should be larger than a site).
105 */
106
107 /*
108 * TODO(XXX): after setting, we should reflect the changes to
bde3511a 109 * interface addresses, routing table entries, PCB entries...
984263bc
MD
110 */
111
abf782b3 112 crit_enter();
984263bc
MD
113
114 for (i = 0; i < 16; i++) {
698ac46c
HS
115 if (idlist->s6id_list[i] &&
116 idlist->s6id_list[i] != sid->s6id_list[i]) {
984263bc 117 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
698ac46c 118 idlist->s6id_list[i] > if_index) {
984263bc
MD
119 /*
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.
124 */
abf782b3 125 crit_exit();
bde3511a 126 return (EINVAL);
984263bc
MD
127 }
128
129 /*
130 * XXX: we must need lots of work in this case,
131 * but we simply set the new value in this initial
132 * implementation.
133 */
698ac46c 134 sid->s6id_list[i] = idlist->s6id_list[i];
984263bc
MD
135 }
136 }
abf782b3 137 crit_exit();
984263bc 138
bde3511a 139 return (error);
984263bc
MD
140}
141
142int
698ac46c 143scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
984263bc 144{
698ac46c
HS
145 struct scope6_id *sid = SID(ifp);
146
147 if (sid == NULL) /* paranoid? */
bde3511a 148 return (EINVAL);
984263bc 149
698ac46c 150 *idlist = *sid;
984263bc 151
bde3511a 152 return (0);
984263bc
MD
153}
154
155
156/*
157 * Get a scope of the address. Node-local, link-local, site-local or global.
158 */
159int
122ebd49 160in6_addrscope(struct in6_addr *addr)
984263bc
MD
161{
162 int scope;
163
164 if (addr->s6_addr8[0] == 0xfe) {
165 scope = addr->s6_addr8[1] & 0xc0;
166
167 switch (scope) {
168 case 0x80:
169 return IPV6_ADDR_SCOPE_LINKLOCAL;
170 break;
171 case 0xc0:
172 return IPV6_ADDR_SCOPE_SITELOCAL;
173 break;
174 default:
175 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
176 break;
177 }
178 }
179
180
181 if (addr->s6_addr8[0] == 0xff) {
182 scope = addr->s6_addr8[1] & 0x0f;
183
184 /*
185 * due to other scope such as reserved,
186 * return scope doesn't work.
187 */
188 switch (scope) {
189 case IPV6_ADDR_SCOPE_NODELOCAL:
190 return IPV6_ADDR_SCOPE_NODELOCAL;
191 break;
192 case IPV6_ADDR_SCOPE_LINKLOCAL:
193 return IPV6_ADDR_SCOPE_LINKLOCAL;
194 break;
195 case IPV6_ADDR_SCOPE_SITELOCAL:
196 return IPV6_ADDR_SCOPE_SITELOCAL;
197 break;
198 default:
199 return IPV6_ADDR_SCOPE_GLOBAL;
200 break;
201 }
202 }
203
84204577 204 if (bcmp(&kin6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
984263bc
MD
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;
209 }
210
211 return IPV6_ADDR_SCOPE_GLOBAL;
212}
213
214int
122ebd49
CP
215in6_addr2scopeid(struct ifnet *ifp, /* must not be NULL */
216 struct in6_addr *addr) /* must not be NULL */
984263bc 217{
698ac46c
HS
218 int scope;
219 struct scope6_id *sid = SID(ifp);
220
221#ifdef DIAGNOSTIC
222 if (sid == NULL) { /* should not happen */
223 panic("in6_addr2zoneid: scope array is NULL");
224 /* NOTREACHED */
225 }
226#endif
984263bc 227
698ac46c
HS
228 /*
229 * special case: the loopback address can only belong to a loopback
230 * interface.
231 */
232 if (IN6_IS_ADDR_LOOPBACK(addr)) {
233 if (!(ifp->if_flags & IFF_LOOPBACK))
234 return (-1);
235 else
236 return (0); /* there's no ambiguity */
237 }
238
239 scope = in6_addrscope(addr);
984263bc 240
984263bc
MD
241 switch(scope) {
242 case IPV6_ADDR_SCOPE_NODELOCAL:
bde3511a 243 return (-1); /* XXX: is this an appropriate value? */
984263bc
MD
244
245 case IPV6_ADDR_SCOPE_LINKLOCAL:
698ac46c 246 return (sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]);
984263bc
MD
247
248 case IPV6_ADDR_SCOPE_SITELOCAL:
698ac46c 249 return (sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL]);
984263bc
MD
250
251 case IPV6_ADDR_SCOPE_ORGLOCAL:
698ac46c 252 return (sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL]);
984263bc
MD
253
254 default:
bde3511a 255 return (0); /* XXX: treat as global. */
984263bc 256 }
984263bc
MD
257}
258
259void
122ebd49 260scope6_setdefault(struct ifnet *ifp) /* note that this might be NULL */
984263bc
MD
261{
262 /*
698ac46c
HS
263 * Currently, this function just set the default "interfaces"
264 * and "links" according to the given interface.
984263bc
MD
265 * We might eventually have to separate the notion of "link" from
266 * "interface" and provide a user interface to set the default.
267 */
268 if (ifp) {
698ac46c
HS
269 sid_default.s6id_list[IPV6_ADDR_SCOPE_NODELOCAL] =
270 ifp->if_index;
271 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
984263bc 272 ifp->if_index;
698ac46c
HS
273 } else {
274 sid_default.s6id_list[IPV6_ADDR_SCOPE_NODELOCAL] = 0;
275 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
984263bc 276 }
984263bc
MD
277}
278
279int
698ac46c 280scope6_get_default(struct scope6_id *idlist)
984263bc 281{
698ac46c 282 *idlist = sid_default;
984263bc 283
bde3511a 284 return (0);
984263bc
MD
285}
286
287u_int32_t
122ebd49 288scope6_addr2default(struct in6_addr *addr)
984263bc 289{
698ac46c
HS
290 /*
291 * special case: The loopback address should be considered as
292 * link-local, but there's no ambiguity in the syntax.
293 */
294 if (IN6_IS_ADDR_LOOPBACK(addr))
295 return (0);
296
297 return (sid_default.s6id_list[in6_addrscope(addr)]);
984263bc 298}
0d16ba1d
MD
299
300/*
301 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
302 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
303 * in the in6_addr structure, in6 will be modified.
304 */
305int
306in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
307{
308 int scope;
309 u_int32_t zoneid = 0;
310 struct scope6_id *sid;
311
312 lwkt_serialize_enter(ifp->if_serializer);
313
314 sid = SID(ifp);
315
316#ifdef DIAGNOSTIC
317 if (sid == NULL) { /* should not happen */
318 panic("in6_setscope: scope array is NULL");
319 /* NOTREACHED */
320 }
321#endif
322
323 /*
324 * special case: the loopback address can only belong to a loopback
325 * interface.
326 */
327 if (IN6_IS_ADDR_LOOPBACK(in6)) {
328 if (!(ifp->if_flags & IFF_LOOPBACK)) {
329 lwkt_serialize_exit(ifp->if_serializer);
330 return (EINVAL);
331 } else {
332 if (ret_id != NULL)
333 *ret_id = 0; /* there's no ambiguity */
334 lwkt_serialize_exit(ifp->if_serializer);
335 return (0);
336 }
337 }
338
339 scope = in6_addrscope(in6);
340
341 switch (scope) {
342 case IPV6_ADDR_SCOPE_NODELOCAL: /* should be interface index */
343 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_NODELOCAL];
344 break;
345
346 case IPV6_ADDR_SCOPE_LINKLOCAL:
347 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
348 break;
349
350 case IPV6_ADDR_SCOPE_SITELOCAL:
351 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
352 break;
353
354 case IPV6_ADDR_SCOPE_ORGLOCAL:
355 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
356 break;
357
358 default:
359 zoneid = 0; /* XXX: treat as global. */
360 break;
361 }
362 lwkt_serialize_exit(ifp->if_serializer);
363
364 if (ret_id != NULL)
365 *ret_id = zoneid;
366
367 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_NODELOCAL(in6) )
368 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
369
370 return (0);
371}