scope6.c revision 1.19.2.1 1 /* $NetBSD: scope6.c,v 1.19.2.1 2018/05/02 07:20:24 pgoyette 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/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: scope6.c,v 1.19.2.1 2018/05/02 07:20:24 pgoyette Exp $");
35
36 #include <sys/param.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/queue.h>
42 #include <sys/syslog.h>
43
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 static struct scope6_id sid_default;
58 #define SID(ifp) \
59 ((ifp)->if_afdata[AF_INET6] == NULL ? NULL : \
60 ((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
61
62 void
63 scope6_init(void)
64 {
65
66 memset(&sid_default, 0, sizeof(sid_default));
67 }
68
69 struct scope6_id *
70 scope6_ifattach(struct ifnet *ifp)
71 {
72 struct scope6_id *sid;
73
74 sid = malloc(sizeof(*sid), M_IFADDR, M_WAITOK | M_ZERO);
75
76 /*
77 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
78 * Should we rather hardcode here?
79 */
80 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
81 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
82 #ifdef MULTI_SCOPE
83 /* by default, we don't care about scope boundary for these scopes. */
84 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
85 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
86 #endif
87
88 return sid;
89 }
90
91 void
92 scope6_ifdetach(struct scope6_id *sid)
93 {
94
95 free(sid, M_IFADDR);
96 }
97
98 int
99 scope6_set(struct ifnet *ifp, const struct scope6_id *idlist)
100 {
101 int i;
102 int error = 0;
103 struct scope6_id *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 int s;
122 /*
123 * An interface zone ID must be the corresponding
124 * interface index by definition.
125 */
126 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
127 idlist->s6id_list[i] != ifp->if_index)
128 return EINVAL;
129
130 s = pserialize_read_enter();
131 if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
132 !if_byindex(idlist->s6id_list[i])) {
133 /*
134 * XXX: theoretically, there should be no
135 * relationship between link IDs and interface
136 * IDs, but we check the consistency for
137 * safety in later use.
138 */
139 pserialize_read_exit(s);
140 return EINVAL;
141 }
142 pserialize_read_exit(s);
143
144 /*
145 * XXX: we must need lots of work in this case,
146 * but we simply set the new value in this initial
147 * implementation.
148 */
149 sid->s6id_list[i] = idlist->s6id_list[i];
150 }
151 }
152
153 return error;
154 }
155
156 int
157 scope6_get(const struct ifnet *ifp, struct scope6_id *idlist)
158 {
159 /* We only need to lock the interface's afdata for SID() to work. */
160 const struct scope6_id *sid = SID(ifp);
161
162 if (sid == NULL) /* paranoid? */
163 return EINVAL;
164
165 *idlist = *sid;
166
167 return 0;
168 }
169
170 /*
171 * Get a scope of the address. Interface-local, link-local, site-local
172 * or global.
173 */
174 int
175 in6_addrscope(const struct in6_addr *addr)
176 {
177 int scope;
178
179 if (addr->s6_addr[0] == 0xfe) {
180 scope = addr->s6_addr[1] & 0xc0;
181
182 switch (scope) {
183 case 0x80:
184 return IPV6_ADDR_SCOPE_LINKLOCAL;
185 case 0xc0:
186 return IPV6_ADDR_SCOPE_SITELOCAL;
187 default:
188 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
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 case IPV6_ADDR_SCOPE_LINKLOCAL:
203 return IPV6_ADDR_SCOPE_LINKLOCAL;
204 case IPV6_ADDR_SCOPE_SITELOCAL:
205 return IPV6_ADDR_SCOPE_SITELOCAL;
206 default:
207 return IPV6_ADDR_SCOPE_GLOBAL;
208 }
209 }
210
211 if (memcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
212 if (addr->s6_addr[15] == 1) /* loopback */
213 return IPV6_ADDR_SCOPE_LINKLOCAL;
214 if (addr->s6_addr[15] == 0) {
215 /*
216 * Regard the unspecified addresses as global,
217 * since it has no ambiguity.
218 * XXX: not sure if it's correct...
219 */
220 return IPV6_ADDR_SCOPE_GLOBAL;
221 }
222 }
223
224 return IPV6_ADDR_SCOPE_GLOBAL;
225 }
226
227 /* note that ifp argument might be NULL */
228 void
229 scope6_setdefault(struct ifnet *ifp)
230 {
231
232 /*
233 * Currently, this function just sets the default "interfaces"
234 * and "links" according to the given interface.
235 * We might eventually have to separate the notion of "link" from
236 * "interface" and provide a user interface to set the default.
237 */
238 if (ifp) {
239 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
240 ifp->if_index;
241 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
242 ifp->if_index;
243 } else {
244 sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
245 sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
246 }
247 }
248
249 int
250 scope6_get_default(struct scope6_id *idlist)
251 {
252
253 *idlist = sid_default;
254
255 return 0;
256 }
257
258 uint32_t
259 scope6_addr2default(const struct in6_addr *addr)
260 {
261 uint32_t id;
262
263 /*
264 * special case: The loopback address should be considered as
265 * link-local, but there's no ambiguity in the syntax.
266 */
267 if (IN6_IS_ADDR_LOOPBACK(addr))
268 return 0;
269
270 /*
271 * XXX: 32-bit read is atomic on all our platforms, is it OK
272 * not to lock here?
273 */
274 id = sid_default.s6id_list[in6_addrscope(addr)];
275
276 return id;
277 }
278
279 /*
280 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
281 * is unspecified (=0), needs to be specified, and the default zone ID can be
282 * used, the default value will be used.
283 * This routine then generates the kernel-internal form: if the address scope
284 * of is interface-local or link-local, embed the interface index in the
285 * address.
286 */
287 int
288 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
289 {
290 struct ifnet *ifp;
291 uint32_t zoneid;
292
293 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
294 zoneid = scope6_addr2default(&sin6->sin6_addr);
295
296 if (zoneid != 0 &&
297 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
298 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
299 int s;
300 /*
301 * At this moment, we only check interface-local and
302 * link-local scope IDs, and use interface indices as the
303 * zone IDs assuming a one-to-one mapping between interfaces
304 * and links.
305 */
306 s = pserialize_read_enter();
307 ifp = if_byindex(zoneid);
308 if (ifp == NULL) {
309 pserialize_read_exit(s);
310 return ENXIO;
311 }
312 pserialize_read_exit(s);
313
314 /* XXX assignment to 16bit from 32bit variable */
315 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
316
317 sin6->sin6_scope_id = 0;
318 }
319
320 return 0;
321 }
322
323 struct sockaddr *
324 sockaddr_in6_externalize(struct sockaddr *dst, socklen_t socklen,
325 const struct sockaddr *src)
326 {
327 struct sockaddr_in6 *sin6;
328
329 sin6 = satosin6(sockaddr_copy(dst, socklen, src));
330
331 if (sin6 == NULL || sa6_recoverscope(sin6) != 0)
332 return NULL;
333
334 return dst;
335 }
336
337 /*
338 * generate standard sockaddr_in6 from embedded form.
339 */
340 int
341 sa6_recoverscope(struct sockaddr_in6 *sin6)
342 {
343 uint32_t zoneid;
344 char ip6buf[INET6_ADDRSTRLEN];
345
346 if (sin6->sin6_scope_id != 0) {
347 log(LOG_NOTICE,
348 "%s: assumption failure (non 0 ID): %s%%%d\n", __func__,
349 IN6_PRINT(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id);
350 /* XXX: proceed anyway... */
351 }
352 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
353 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
354 /*
355 * KAME assumption: link id == interface id
356 */
357 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
358 if (zoneid) {
359 int s = pserialize_read_enter();
360 if (!if_byindex(zoneid)) {
361 pserialize_read_exit(s);
362 return ENXIO;
363 }
364 pserialize_read_exit(s);
365 sin6->sin6_addr.s6_addr16[1] = 0;
366 sin6->sin6_scope_id = zoneid;
367 }
368 }
369
370 return 0;
371 }
372
373 int
374 in6_setzoneid(struct in6_addr *in6, uint32_t zoneid)
375 {
376 if (IN6_IS_SCOPE_EMBEDDABLE(in6))
377 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
378
379 return 0;
380 }
381
382 /*
383 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
384 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
385 * in the in6_addr structure, in6 will be modified.
386 */
387 int
388 in6_setscope(struct in6_addr *in6, const struct ifnet *ifp, uint32_t *ret_id)
389 {
390 int scope;
391 uint32_t zoneid = 0;
392 const struct scope6_id *sid = SID(ifp);
393
394 if (sid == NULL) {
395 log(LOG_NOTICE, "%s: no scope id for %s\n", __func__,
396 if_name(ifp));
397 return EINVAL;
398 }
399
400 /*
401 * special case: the loopback address can only belong to a loopback
402 * interface.
403 */
404 if (IN6_IS_ADDR_LOOPBACK(in6)) {
405 if (!(ifp->if_flags & IFF_LOOPBACK)) {
406 char ip6buf[INET6_ADDRSTRLEN];
407 log(LOG_NOTICE, "%s: can't set scope for not loopback "
408 "interface %s and loopback address %s\n",
409 __func__, if_name(ifp), IN6_PRINT(ip6buf, in6));
410 return EINVAL;
411 } else {
412 if (ret_id != NULL)
413 *ret_id = 0; /* there's no ambiguity */
414 return 0;
415 }
416 }
417
418 scope = in6_addrscope(in6);
419
420 switch (scope) {
421 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
422 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
423 break;
424
425 case IPV6_ADDR_SCOPE_LINKLOCAL:
426 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
427 break;
428
429 case IPV6_ADDR_SCOPE_SITELOCAL:
430 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
431 break;
432
433 case IPV6_ADDR_SCOPE_ORGLOCAL:
434 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
435 break;
436
437 default:
438 zoneid = 0; /* XXX: treat as global. */
439 break;
440 }
441
442 if (ret_id != NULL)
443 *ret_id = zoneid;
444
445 return in6_setzoneid(in6, zoneid);
446 }
447
448 const char *
449 in6_getscopename(const struct in6_addr *addr)
450 {
451 switch (in6_addrscope(addr)) {
452 case IPV6_ADDR_SCOPE_INTFACELOCAL:
453 return "interface";
454 #if IPV6_ADDR_SCOPE_INTFACELOCAL != IPV6_ADDR_SCOPE_NODELOCAL
455 case IPV6_ADDR_SCOPE_NODELOCAL:
456 return "node";
457 #endif
458 case IPV6_ADDR_SCOPE_LINKLOCAL:
459 return "link";
460 case IPV6_ADDR_SCOPE_SITELOCAL:
461 return "site";
462 case IPV6_ADDR_SCOPE_ORGLOCAL:
463 return "organization";
464 case IPV6_ADDR_SCOPE_GLOBAL:
465 return "global";
466 default:
467 return "unknown";
468 }
469 }
470
471 /*
472 * Just clear the embedded scope identifier. Return 0 if the original address
473 * is intact; return non 0 if the address is modified.
474 */
475 int
476 in6_clearscope(struct in6_addr *in6)
477 {
478 int modified = 0;
479
480 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
481 if (in6->s6_addr16[1] != 0)
482 modified = 1;
483 in6->s6_addr16[1] = 0;
484 }
485
486 return modified;
487 }
488