scope6.c revision 1.5 1 /* $NetBSD: scope6.c,v 1.5 2007/10/24 06:37:22 dyoung Exp $ */
2 /* $KAME$ */
3
4 /*-
5 * Copyright (C) 2000 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/mbuf.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/queue.h>
39 #include <sys/syslog.h>
40
41 #include <net/route.h>
42 #include <net/if.h>
43
44 #include <netinet/in.h>
45
46 #include <netinet6/in6_var.h>
47 #include <netinet6/scope6_var.h>
48
49 #ifdef ENABLE_DEFAULT_SCOPE
50 int ip6_use_defzone = 1;
51 #else
52 int ip6_use_defzone = 0;
53 #endif
54
55 static struct scope6_id sid_default;
56 #define SID(ifp) \
57 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
58
59 void
60 scope6_init()
61 {
62
63 memset(&sid_default, 0, sizeof(sid_default));
64 }
65
66 struct scope6_id *
67 scope6_ifattach(struct ifnet *ifp)
68 {
69 struct scope6_id *sid;
70
71 sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
72 memset(sid, 0, sizeof(*sid));
73
74 /*
75 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
76 * Should we rather hardcode here?
77 */
78 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
79 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
80 #ifdef MULTI_SCOPE
81 /* by default, we don't care about scope boundary for these scopes. */
82 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
83 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
84 #endif
85
86 return sid;
87 }
88
89 void
90 scope6_ifdetach(struct scope6_id *sid)
91 {
92
93 free(sid, M_IFADDR);
94 }
95
96 int
97 scope6_set(struct ifnet *ifp, const struct scope6_id *idlist)
98 {
99 int i;
100 int error = 0;
101 struct scope6_id *sid = NULL;
102
103 sid = SID(ifp);
104
105 if (!sid) /* paranoid? */
106 return (EINVAL);
107
108 /*
109 * XXX: We need more consistency checks of the relationship among
110 * scopes (e.g. an organization should be larger than a site).
111 */
112
113 /*
114 * TODO(XXX): after setting, we should reflect the changes to
115 * interface addresses, routing table entries, PCB entries...
116 */
117
118 for (i = 0; i < 16; i++) {
119 if (idlist->s6id_list[i] &&
120 idlist->s6id_list[i] != sid->s6id_list[i]) {
121 /*
122 * An interface zone ID must be the corresponding
123 * interface index by definition.
124 */
125 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
126 idlist->s6id_list[i] != ifp->if_index)
127 return (EINVAL);
128
129 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
130 idlist->s6id_list[i] >= if_indexlim) {
131 /*
132 * XXX: theoretically, there should be no
133 * relationship between link IDs and interface
134 * IDs, but we check the consistency for
135 * safety in later use.
136 */
137 return (EINVAL);
138 }
139
140 /*
141 * XXX: we must need lots of work in this case,
142 * but we simply set the new value in this initial
143 * implementation.
144 */
145 sid->s6id_list[i] = idlist->s6id_list[i];
146 }
147 }
148
149 return (error);
150 }
151
152 int
153 scope6_get(const struct ifnet *ifp, struct scope6_id *idlist)
154 {
155 /* We only need to lock the interface's afdata for SID() to work. */
156 const struct scope6_id *sid = SID(ifp);
157
158 if (sid == NULL) /* paranoid? */
159 return EINVAL;
160
161 *idlist = *sid;
162
163 return 0;
164 }
165
166 /*
167 * Get a scope of the address. Interface-local, link-local, site-local
168 * or global.
169 */
170 int
171 in6_addrscope(const struct in6_addr *addr)
172 {
173 int scope;
174
175 if (addr->s6_addr[0] == 0xfe) {
176 scope = addr->s6_addr[1] & 0xc0;
177
178 switch (scope) {
179 case 0x80:
180 return IPV6_ADDR_SCOPE_LINKLOCAL;
181 break;
182 case 0xc0:
183 return IPV6_ADDR_SCOPE_SITELOCAL;
184 break;
185 default:
186 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
187 break;
188 }
189 }
190
191
192 if (addr->s6_addr[0] == 0xff) {
193 scope = addr->s6_addr[1] & 0x0f;
194
195 /*
196 * due to other scope such as reserved,
197 * return scope doesn't work.
198 */
199 switch (scope) {
200 case IPV6_ADDR_SCOPE_INTFACELOCAL:
201 return IPV6_ADDR_SCOPE_INTFACELOCAL;
202 break;
203 case IPV6_ADDR_SCOPE_LINKLOCAL:
204 return IPV6_ADDR_SCOPE_LINKLOCAL;
205 break;
206 case IPV6_ADDR_SCOPE_SITELOCAL:
207 return IPV6_ADDR_SCOPE_SITELOCAL;
208 break;
209 default:
210 return IPV6_ADDR_SCOPE_GLOBAL;
211 break;
212 }
213 }
214
215 if (memcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
216 if (addr->s6_addr[15] == 1) /* loopback */
217 return IPV6_ADDR_SCOPE_LINKLOCAL;
218 if (addr->s6_addr[15] == 0) {
219 /*
220 * Regard the unspecified addresses as global,
221 * since it has no ambiguity.
222 * XXX: not sure if it's correct...
223 */
224 return IPV6_ADDR_SCOPE_GLOBAL;
225 }
226 }
227
228 return IPV6_ADDR_SCOPE_GLOBAL;
229 }
230
231 /* note that ifp argument might be NULL */
232 void
233 scope6_setdefault(struct ifnet *ifp)
234 {
235
236 /*
237 * Currently, this function just sets the default "interfaces"
238 * and "links" according to the given interface.
239 * We might eventually have to separate the notion of "link" from
240 * "interface" and provide a user interface to set the default.
241 */
242 if (ifp) {
243 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
244 ifp->if_index;
245 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
246 ifp->if_index;
247 } else {
248 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
249 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
250 }
251 }
252
253 int
254 scope6_get_default(struct scope6_id *idlist)
255 {
256
257 *idlist = sid_default;
258
259 return (0);
260 }
261
262 uint32_t
263 scope6_addr2default(const struct in6_addr *addr)
264 {
265 uint32_t id;
266
267 /*
268 * special case: The loopback address should be considered as
269 * link-local, but there's no ambiguity in the syntax.
270 */
271 if (IN6_IS_ADDR_LOOPBACK(addr))
272 return (0);
273
274 /*
275 * XXX: 32-bit read is atomic on all our platforms, is it OK
276 * not to lock here?
277 */
278 id = sid_default.s6id_list[in6_addrscope(addr)];
279
280 return (id);
281 }
282
283 /*
284 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
285 * is unspecified (=0), needs to be specified, and the default zone ID can be
286 * used, the default value will be used.
287 * This routine then generates the kernel-internal form: if the address scope
288 * of is interface-local or link-local, embed the interface index in the
289 * address.
290 */
291 int
292 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
293 {
294 struct ifnet *ifp;
295 uint32_t zoneid;
296
297 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
298 zoneid = scope6_addr2default(&sin6->sin6_addr);
299
300 if (zoneid != 0 &&
301 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
302 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
303 /*
304 * At this moment, we only check interface-local and
305 * link-local scope IDs, and use interface indices as the
306 * zone IDs assuming a one-to-one mapping between interfaces
307 * and links.
308 */
309 if (if_indexlim <= zoneid)
310 return (ENXIO);
311 #ifdef __FreeBSD__
312 ifp = ifnet_byindex(zoneid);
313 #else
314 ifp = ifindex2ifnet[zoneid];
315 #endif
316 if (ifp == NULL) /* XXX: this can happen for some OS */
317 return (ENXIO);
318
319 /* XXX assignment to 16bit from 32bit variable */
320 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
321
322 sin6->sin6_scope_id = 0;
323 }
324
325 return 0;
326 }
327
328 /*
329 * generate standard sockaddr_in6 from embedded form.
330 */
331 int
332 sa6_recoverscope(struct sockaddr_in6 *sin6)
333 {
334 uint32_t zoneid;
335
336 if (sin6->sin6_scope_id != 0) {
337 log(LOG_NOTICE,
338 "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
339 ip6_sprintf(&sin6->sin6_addr), sin6->sin6_scope_id);
340 /* XXX: proceed anyway... */
341 }
342 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
343 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
344 /*
345 * KAME assumption: link id == interface id
346 */
347 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
348 if (zoneid) {
349 /* sanity check */
350 if (/* zoneid < 0 || */ if_indexlim <= zoneid)
351 return (ENXIO);
352 #ifdef __FreeBSD__
353 if (!ifnet_byindex(zoneid))
354 #else
355 if (!ifindex2ifnet[zoneid])
356 #endif
357 return (ENXIO);
358 sin6->sin6_addr.s6_addr16[1] = 0;
359 sin6->sin6_scope_id = zoneid;
360 }
361 }
362
363 return 0;
364 }
365
366 int
367 in6_setzoneid(struct in6_addr *in6, uint32_t zoneid)
368 {
369 if (IN6_IS_SCOPE_EMBEDDABLE(in6))
370 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
371
372 return 0;
373 }
374
375 /*
376 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
377 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
378 * in the in6_addr structure, in6 will be modified.
379 */
380 int
381 in6_setscope(struct in6_addr *in6, const struct ifnet *ifp, uint32_t *ret_id)
382 {
383 int scope;
384 uint32_t zoneid = 0;
385 const struct scope6_id *sid = SID(ifp);
386
387 #ifdef DIAGNOSTIC
388 if (sid == NULL) { /* should not happen */
389 panic("in6_setscope: scope array is NULL");
390 /* NOTREACHED */
391 }
392 #endif
393
394 /*
395 * special case: the loopback address can only belong to a loopback
396 * interface.
397 */
398 if (IN6_IS_ADDR_LOOPBACK(in6)) {
399 if (!(ifp->if_flags & IFF_LOOPBACK))
400 return (EINVAL);
401 else {
402 if (ret_id != NULL)
403 *ret_id = 0; /* there's no ambiguity */
404 return (0);
405 }
406 }
407
408 scope = in6_addrscope(in6);
409
410 if (!sid->s6id_list)
411 return 0;
412
413 switch (scope) {
414 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
415 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
416 break;
417
418 case IPV6_ADDR_SCOPE_LINKLOCAL:
419 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
420 break;
421
422 case IPV6_ADDR_SCOPE_SITELOCAL:
423 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
424 break;
425
426 case IPV6_ADDR_SCOPE_ORGLOCAL:
427 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
428 break;
429
430 default:
431 zoneid = 0; /* XXX: treat as global. */
432 break;
433 }
434
435 if (ret_id != NULL)
436 *ret_id = zoneid;
437
438 return in6_setzoneid(in6, zoneid);
439 }
440
441 /*
442 * Just clear the embedded scope identifier. Return 0 if the original address
443 * is intact; return non 0 if the address is modified.
444 */
445 int
446 in6_clearscope(struct in6_addr *in6)
447 {
448 int modified = 0;
449
450 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
451 if (in6->s6_addr16[1] != 0)
452 modified = 1;
453 in6->s6_addr16[1] = 0;
454 }
455
456 return (modified);
457 }
458