FreeBSD/Linux Kernel Cross Reference
sys/netinet6/scope6.c
1 /*-
2 * Copyright (C) 2000 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 * $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/syslog.h>
42
43 #include <net/route.h>
44 #include <net/if.h>
45
46 #include <netinet/in.h>
47
48 #include <netinet6/in6_var.h>
49 #include <netinet6/scope6_var.h>
50
51 #ifdef ENABLE_DEFAULT_SCOPE
52 int ip6_use_defzone = 1;
53 #else
54 int ip6_use_defzone = 0;
55 #endif
56
57 /*
58 * The scope6_lock protects the global sid default stored in
59 * sid_default below.
60 */
61 static struct mtx scope6_lock;
62 #define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
63 #define SCOPE6_LOCK() mtx_lock(&scope6_lock)
64 #define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
65 #define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
66
67 static struct scope6_id sid_default;
68 #define SID(ifp) \
69 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
70
71 void
72 scope6_init()
73 {
74
75 SCOPE6_LOCK_INIT();
76 bzero(&sid_default, sizeof(sid_default));
77 }
78
79 struct scope6_id *
80 scope6_ifattach(ifp)
81 struct ifnet *ifp;
82 {
83 struct scope6_id *sid;
84
85 sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
86 bzero(sid, sizeof(*sid));
87
88 /*
89 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
90 * Should we rather hardcode here?
91 */
92 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
93 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
94 #ifdef MULTI_SCOPE
95 /* by default, we don't care about scope boundary for these scopes. */
96 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
97 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
98 #endif
99
100 return sid;
101 }
102
103 void
104 scope6_ifdetach(sid)
105 struct scope6_id *sid;
106 {
107
108 free(sid, M_IFADDR);
109 }
110
111 int
112 scope6_set(ifp, idlist)
113 struct ifnet *ifp;
114 struct scope6_id *idlist;
115 {
116 int i;
117 int error = 0;
118 struct scope6_id *sid = NULL;
119
120 IF_AFDATA_LOCK(ifp);
121 sid = SID(ifp);
122
123 if (!sid) { /* paranoid? */
124 IF_AFDATA_UNLOCK(ifp);
125 return (EINVAL);
126 }
127
128 /*
129 * XXX: We need more consistency checks of the relationship among
130 * scopes (e.g. an organization should be larger than a site).
131 */
132
133 /*
134 * TODO(XXX): after setting, we should reflect the changes to
135 * interface addresses, routing table entries, PCB entries...
136 */
137
138 SCOPE6_LOCK();
139 for (i = 0; i < 16; i++) {
140 if (idlist->s6id_list[i] &&
141 idlist->s6id_list[i] != sid->s6id_list[i]) {
142 /*
143 * An interface zone ID must be the corresponding
144 * interface index by definition.
145 */
146 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
147 idlist->s6id_list[i] != ifp->if_index) {
148 IF_AFDATA_UNLOCK(ifp);
149 SCOPE6_UNLOCK();
150 return (EINVAL);
151 }
152
153 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
154 idlist->s6id_list[i] > if_index) {
155 /*
156 * XXX: theoretically, there should be no
157 * relationship between link IDs and interface
158 * IDs, but we check the consistency for
159 * safety in later use.
160 */
161 IF_AFDATA_UNLOCK(ifp);
162 SCOPE6_UNLOCK();
163 return (EINVAL);
164 }
165
166 /*
167 * XXX: we must need lots of work in this case,
168 * but we simply set the new value in this initial
169 * implementation.
170 */
171 sid->s6id_list[i] = idlist->s6id_list[i];
172 }
173 }
174 SCOPE6_UNLOCK();
175 IF_AFDATA_UNLOCK(ifp);
176
177 return (error);
178 }
179
180 int
181 scope6_get(ifp, idlist)
182 struct ifnet *ifp;
183 struct scope6_id *idlist;
184 {
185 /* We only need to lock the interface's afdata for SID() to work. */
186 IF_AFDATA_LOCK(ifp);
187 struct scope6_id *sid = SID(ifp);
188
189 if (sid == NULL) { /* paranoid? */
190 IF_AFDATA_UNLOCK(ifp);
191 return (EINVAL);
192 }
193
194 SCOPE6_LOCK();
195 *idlist = *sid;
196 SCOPE6_UNLOCK();
197
198 IF_AFDATA_UNLOCK(ifp);
199 return (0);
200 }
201
202
203 /*
204 * Get a scope of the address. Node-local, link-local, site-local or global.
205 */
206 int
207 in6_addrscope(addr)
208 struct in6_addr *addr;
209 {
210 int scope;
211
212 if (addr->s6_addr[0] == 0xfe) {
213 scope = addr->s6_addr[1] & 0xc0;
214
215 switch (scope) {
216 case 0x80:
217 return IPV6_ADDR_SCOPE_LINKLOCAL;
218 break;
219 case 0xc0:
220 return IPV6_ADDR_SCOPE_SITELOCAL;
221 break;
222 default:
223 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
224 break;
225 }
226 }
227
228
229 if (addr->s6_addr[0] == 0xff) {
230 scope = addr->s6_addr[1] & 0x0f;
231
232 /*
233 * due to other scope such as reserved,
234 * return scope doesn't work.
235 */
236 switch (scope) {
237 case IPV6_ADDR_SCOPE_INTFACELOCAL:
238 return IPV6_ADDR_SCOPE_INTFACELOCAL;
239 break;
240 case IPV6_ADDR_SCOPE_LINKLOCAL:
241 return IPV6_ADDR_SCOPE_LINKLOCAL;
242 break;
243 case IPV6_ADDR_SCOPE_SITELOCAL:
244 return IPV6_ADDR_SCOPE_SITELOCAL;
245 break;
246 default:
247 return IPV6_ADDR_SCOPE_GLOBAL;
248 break;
249 }
250 }
251
252 /*
253 * Regard loopback and unspecified addresses as global, since
254 * they have no ambiguity.
255 */
256 if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
257 if (addr->s6_addr[15] == 1) /* loopback */
258 return IPV6_ADDR_SCOPE_LINKLOCAL;
259 if (addr->s6_addr[15] == 0) /* unspecified */
260 return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
261 }
262
263 return IPV6_ADDR_SCOPE_GLOBAL;
264 }
265
266 void
267 scope6_setdefault(ifp)
268 struct ifnet *ifp; /* note that this might be NULL */
269 {
270 /*
271 * Currently, this function just sets the default "interfaces"
272 * and "links" according to the given interface.
273 * We might eventually have to separate the notion of "link" from
274 * "interface" and provide a user interface to set the default.
275 */
276 SCOPE6_LOCK();
277 if (ifp) {
278 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
279 ifp->if_index;
280 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
281 ifp->if_index;
282 } else {
283 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
284 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
285 }
286 SCOPE6_UNLOCK();
287 }
288
289 int
290 scope6_get_default(idlist)
291 struct scope6_id *idlist;
292 {
293
294 SCOPE6_LOCK();
295 *idlist = sid_default;
296 SCOPE6_UNLOCK();
297
298 return (0);
299 }
300
301 u_int32_t
302 scope6_addr2default(addr)
303 struct in6_addr *addr;
304 {
305 u_int32_t id;
306
307 /*
308 * special case: The loopback address should be considered as
309 * link-local, but there's no ambiguity in the syntax.
310 */
311 if (IN6_IS_ADDR_LOOPBACK(addr))
312 return (0);
313
314 /*
315 * XXX: 32-bit read is atomic on all our platforms, is it OK
316 * not to lock here?
317 */
318 SCOPE6_LOCK();
319 id = sid_default.s6id_list[in6_addrscope(addr)];
320 SCOPE6_UNLOCK();
321 return (id);
322 }
323
324 /*
325 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
326 * is unspecified (=0), needs to be specified, and the default zone ID can be
327 * used, the default value will be used.
328 * This routine then generates the kernel-internal form: if the address scope
329 * of is interface-local or link-local, embed the interface index in the
330 * address.
331 */
332 int
333 sa6_embedscope(sin6, defaultok)
334 struct sockaddr_in6 *sin6;
335 int defaultok;
336 {
337 struct ifnet *ifp;
338 u_int32_t zoneid;
339
340 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
341 zoneid = scope6_addr2default(&sin6->sin6_addr);
342
343 if (zoneid != 0 &&
344 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
345 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
346 /*
347 * At this moment, we only check interface-local and
348 * link-local scope IDs, and use interface indices as the
349 * zone IDs assuming a one-to-one mapping between interfaces
350 * and links.
351 */
352 if (if_index < zoneid)
353 return (ENXIO);
354 ifp = ifnet_byindex(zoneid);
355 if (ifp == NULL) /* XXX: this can happen for some OS */
356 return (ENXIO);
357
358 /* XXX assignment to 16bit from 32bit variable */
359 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
360
361 sin6->sin6_scope_id = 0;
362 }
363
364 return 0;
365 }
366
367 /*
368 * generate standard sockaddr_in6 from embedded form.
369 */
370 int
371 sa6_recoverscope(sin6)
372 struct sockaddr_in6 *sin6;
373 {
374 u_int32_t zoneid;
375
376 if (sin6->sin6_scope_id != 0) {
377 log(LOG_NOTICE,
378 "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
379 ip6_sprintf(&sin6->sin6_addr), sin6->sin6_scope_id);
380 /* XXX: proceed anyway... */
381 }
382 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
383 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
384 /*
385 * KAME assumption: link id == interface id
386 */
387 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
388 if (zoneid) {
389 /* sanity check */
390 if (zoneid < 0 || if_index < zoneid)
391 return (ENXIO);
392 if (!ifnet_byindex(zoneid))
393 return (ENXIO);
394 sin6->sin6_addr.s6_addr16[1] = 0;
395 sin6->sin6_scope_id = zoneid;
396 }
397 }
398
399 return 0;
400 }
401
402 /*
403 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
404 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
405 * in the in6_addr structure, in6 will be modified.
406 */
407 int
408 in6_setscope(in6, ifp, ret_id)
409 struct in6_addr *in6;
410 struct ifnet *ifp;
411 u_int32_t *ret_id; /* unnecessary? */
412 {
413 int scope;
414 u_int32_t zoneid = 0;
415 struct scope6_id *sid;
416
417 IF_AFDATA_LOCK(ifp);
418
419 sid = SID(ifp);
420
421 #ifdef DIAGNOSTIC
422 if (sid == NULL) { /* should not happen */
423 panic("in6_setscope: scope array is NULL");
424 /* NOTREACHED */
425 }
426 #endif
427
428 /*
429 * special case: the loopback address can only belong to a loopback
430 * interface.
431 */
432 if (IN6_IS_ADDR_LOOPBACK(in6)) {
433 if (!(ifp->if_flags & IFF_LOOPBACK)) {
434 IF_AFDATA_UNLOCK(ifp);
435 return (EINVAL);
436 } else {
437 if (ret_id != NULL)
438 *ret_id = 0; /* there's no ambiguity */
439 IF_AFDATA_UNLOCK(ifp);
440 return (0);
441 }
442 }
443
444 scope = in6_addrscope(in6);
445
446 SCOPE6_LOCK();
447 switch (scope) {
448 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
449 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
450 break;
451
452 case IPV6_ADDR_SCOPE_LINKLOCAL:
453 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
454 break;
455
456 case IPV6_ADDR_SCOPE_SITELOCAL:
457 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
458 break;
459
460 case IPV6_ADDR_SCOPE_ORGLOCAL:
461 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
462 break;
463
464 default:
465 zoneid = 0; /* XXX: treat as global. */
466 break;
467 }
468 SCOPE6_UNLOCK();
469 IF_AFDATA_UNLOCK(ifp);
470
471 if (ret_id != NULL)
472 *ret_id = zoneid;
473
474 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
475 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
476
477 return (0);
478 }
479
480 /*
481 * Just clear the embedded scope identifier. Return 0 if the original address
482 * is intact; return non 0 if the address is modified.
483 */
484 int
485 in6_clearscope(in6)
486 struct in6_addr *in6;
487 {
488 int modified = 0;
489
490 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
491 if (in6->s6_addr16[1] != 0)
492 modified = 1;
493 in6->s6_addr16[1] = 0;
494 }
495
496 return (modified);
497 }
Cache object: 1f006c8465900f3fc501b51cffc10606
|