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