1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 2000 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $KAME: scope6.c,v 1.10 2000/07/24 13:29:31 itojun Exp $
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/sockio.h>
39 #include <sys/systm.h>
40 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 #include <sys/syslog.h>
43
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_private.h>
47 #include <net/vnet.h>
48
49 #include <netinet/in.h>
50
51 #include <netinet/ip6.h>
52 #include <netinet6/in6_var.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet6/scope6_var.h>
55
56 #ifdef ENABLE_DEFAULT_SCOPE
57 VNET_DEFINE(int, ip6_use_defzone) = 1;
58 #else
59 VNET_DEFINE(int, ip6_use_defzone) = 0;
60 #endif
61 SYSCTL_DECL(_net_inet6_ip6);
62
63 /*
64 * The scope6_lock protects the global sid default stored in
65 * sid_default below.
66 */
67 static struct mtx scope6_lock;
68 #define SCOPE6_LOCK_INIT() mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
69 #define SCOPE6_LOCK() mtx_lock(&scope6_lock)
70 #define SCOPE6_UNLOCK() mtx_unlock(&scope6_lock)
71 #define SCOPE6_LOCK_ASSERT() mtx_assert(&scope6_lock, MA_OWNED)
72
73 VNET_DEFINE_STATIC(struct scope6_id, sid_default);
74 #define V_sid_default VNET(sid_default)
75
76 #define SID(ifp) \
77 (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
78
79 static int scope6_get(struct ifnet *, struct scope6_id *);
80 static int scope6_set(struct ifnet *, struct scope6_id *);
81
82 void
scope6_init(void)83 scope6_init(void)
84 {
85
86 bzero(&V_sid_default, sizeof(V_sid_default));
87
88 if (!IS_DEFAULT_VNET(curvnet))
89 return;
90
91 SCOPE6_LOCK_INIT();
92 }
93
94 struct scope6_id *
scope6_ifattach(struct ifnet * ifp)95 scope6_ifattach(struct ifnet *ifp)
96 {
97 struct scope6_id *sid;
98
99 sid = malloc(sizeof(*sid), M_IFADDR, M_WAITOK | M_ZERO);
100 /*
101 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
102 * Should we rather hardcode here?
103 */
104 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
105 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
106 return (sid);
107 }
108
109 void
scope6_ifdetach(struct scope6_id * sid)110 scope6_ifdetach(struct scope6_id *sid)
111 {
112
113 free(sid, M_IFADDR);
114 }
115
116 int
scope6_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp)117 scope6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
118 {
119 struct in6_ifreq *ifr;
120
121 if (ifp->if_afdata[AF_INET6] == NULL)
122 return (EPFNOSUPPORT);
123
124 ifr = (struct in6_ifreq *)data;
125 switch (cmd) {
126 case SIOCSSCOPE6:
127 return (scope6_set(ifp,
128 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
129 case SIOCGSCOPE6:
130 return (scope6_get(ifp,
131 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
132 case SIOCGSCOPE6DEF:
133 return (scope6_get_default(
134 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
135 default:
136 return (EOPNOTSUPP);
137 }
138 }
139
140 /*
141 * XXXGL: The use of IF_ADDR_WLOCK (previously it was IF_AFDATA_LOCK) in this
142 * function is quite strange.
143 */
144 static int
scope6_set(struct ifnet * ifp,struct scope6_id * idlist)145 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
146 {
147 int i;
148 int error = 0;
149 struct scope6_id *sid = NULL;
150
151 IF_ADDR_WLOCK(ifp);
152 sid = SID(ifp);
153
154 if (!sid) { /* paranoid? */
155 IF_ADDR_WUNLOCK(ifp);
156 return (EINVAL);
157 }
158
159 /*
160 * XXX: We need more consistency checks of the relationship among
161 * scopes (e.g. an organization should be larger than a site).
162 */
163
164 /*
165 * TODO(XXX): after setting, we should reflect the changes to
166 * interface addresses, routing table entries, PCB entries...
167 */
168
169 for (i = 0; i < 16; i++) {
170 if (idlist->s6id_list[i] &&
171 idlist->s6id_list[i] != sid->s6id_list[i]) {
172 /*
173 * An interface zone ID must be the corresponding
174 * interface index by definition.
175 */
176 if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
177 idlist->s6id_list[i] != ifp->if_index) {
178 IF_ADDR_WUNLOCK(ifp);
179 return (EINVAL);
180 }
181
182 if (i == IPV6_ADDR_SCOPE_LINKLOCAL) {
183 struct epoch_tracker et;
184
185 NET_EPOCH_ENTER(et);
186 if (!ifnet_byindex(idlist->s6id_list[i])) {
187 /*
188 * XXX: theoretically, there should be
189 * no relationship between link IDs and
190 * interface IDs, but we check the
191 * consistency for safety in later use.
192 */
193 NET_EPOCH_EXIT(et);
194 IF_ADDR_WUNLOCK(ifp);
195 return (EINVAL);
196 }
197 NET_EPOCH_EXIT(et);
198 }
199
200 /*
201 * XXX: we must need lots of work in this case,
202 * but we simply set the new value in this initial
203 * implementation.
204 */
205 sid->s6id_list[i] = idlist->s6id_list[i];
206 }
207 }
208 IF_ADDR_WUNLOCK(ifp);
209
210 return (error);
211 }
212
213 static int
scope6_get(struct ifnet * ifp,struct scope6_id * idlist)214 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
215 {
216 struct epoch_tracker et;
217 struct scope6_id *sid;
218
219 /* We only need to lock the interface's afdata for SID() to work. */
220 NET_EPOCH_ENTER(et);
221 sid = SID(ifp);
222 if (sid == NULL) { /* paranoid? */
223 NET_EPOCH_EXIT(et);
224 return (EINVAL);
225 }
226
227 *idlist = *sid;
228
229 NET_EPOCH_EXIT(et);
230 return (0);
231 }
232
233 /*
234 * Get a scope of the address. Node-local, link-local, site-local or global.
235 */
236 int
in6_addrscope(const struct in6_addr * addr)237 in6_addrscope(const struct in6_addr *addr)
238 {
239
240 if (IN6_IS_ADDR_MULTICAST(addr)) {
241 /*
242 * Addresses with reserved value F must be treated as
243 * global multicast addresses.
244 */
245 if (IPV6_ADDR_MC_SCOPE(addr) == 0x0f)
246 return (IPV6_ADDR_SCOPE_GLOBAL);
247 return (IPV6_ADDR_MC_SCOPE(addr));
248 }
249 if (IN6_IS_ADDR_LINKLOCAL(addr) ||
250 IN6_IS_ADDR_LOOPBACK(addr))
251 return (IPV6_ADDR_SCOPE_LINKLOCAL);
252 if (IN6_IS_ADDR_SITELOCAL(addr))
253 return (IPV6_ADDR_SCOPE_SITELOCAL);
254 return (IPV6_ADDR_SCOPE_GLOBAL);
255 }
256
257 /*
258 * ifp - note that this might be NULL
259 */
260
261 void
scope6_setdefault(struct ifnet * ifp)262 scope6_setdefault(struct ifnet *ifp)
263 {
264
265 /*
266 * Currently, this function just sets the default "interfaces"
267 * and "links" according to the given interface.
268 * We might eventually have to separate the notion of "link" from
269 * "interface" and provide a user interface to set the default.
270 */
271 SCOPE6_LOCK();
272 if (ifp) {
273 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
274 ifp->if_index;
275 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
276 ifp->if_index;
277 } else {
278 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
279 V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
280 }
281 SCOPE6_UNLOCK();
282 }
283
284 int
scope6_get_default(struct scope6_id * idlist)285 scope6_get_default(struct scope6_id *idlist)
286 {
287
288 SCOPE6_LOCK();
289 *idlist = V_sid_default;
290 SCOPE6_UNLOCK();
291
292 return (0);
293 }
294
295 u_int32_t
scope6_addr2default(struct in6_addr * addr)296 scope6_addr2default(struct in6_addr *addr)
297 {
298 u_int32_t id;
299
300 /*
301 * special case: The loopback address should be considered as
302 * link-local, but there's no ambiguity in the syntax.
303 */
304 if (IN6_IS_ADDR_LOOPBACK(addr))
305 return (0);
306
307 /*
308 * XXX: 32-bit read is atomic on all our platforms, is it OK
309 * not to lock here?
310 */
311 SCOPE6_LOCK();
312 id = V_sid_default.s6id_list[in6_addrscope(addr)];
313 SCOPE6_UNLOCK();
314 return (id);
315 }
316
317 /*
318 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
319 * is unspecified (=0), needs to be specified, and the default zone ID can be
320 * used, the default value will be used.
321 * This routine then generates the kernel-internal form: if the address scope
322 * of is interface-local or link-local, embed the interface index in the
323 * address.
324 */
325 int
sa6_embedscope(struct sockaddr_in6 * sin6,int defaultok)326 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
327 {
328 u_int32_t zoneid;
329
330 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
331 zoneid = scope6_addr2default(&sin6->sin6_addr);
332
333 if (zoneid != 0 &&
334 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
335 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
336 struct epoch_tracker et;
337
338 /*
339 * At this moment, we only check interface-local and
340 * link-local scope IDs, and use interface indices as the
341 * zone IDs assuming a one-to-one mapping between interfaces
342 * and links.
343 */
344 NET_EPOCH_ENTER(et);
345 if (ifnet_byindex(zoneid) == NULL) {
346 NET_EPOCH_EXIT(et);
347 return (ENXIO);
348 }
349 NET_EPOCH_EXIT(et);
350
351 /* XXX assignment to 16bit from 32bit variable */
352 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
353 sin6->sin6_scope_id = 0;
354 }
355
356 return 0;
357 }
358
359 /*
360 * generate standard sockaddr_in6 from embedded form.
361 */
362 int
sa6_recoverscope(struct sockaddr_in6 * sin6)363 sa6_recoverscope(struct sockaddr_in6 *sin6)
364 {
365 char ip6buf[INET6_ADDRSTRLEN];
366 u_int32_t zoneid;
367
368 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
369 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
370 /*
371 * KAME assumption: link id == interface id
372 */
373 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
374 if (zoneid) {
375 struct epoch_tracker et;
376
377 NET_EPOCH_ENTER(et);
378 /* sanity check */
379 if (!ifnet_byindex(zoneid)) {
380 NET_EPOCH_EXIT(et);
381 return (ENXIO);
382 }
383 NET_EPOCH_EXIT(et);
384 if (sin6->sin6_scope_id != 0 &&
385 zoneid != sin6->sin6_scope_id) {
386 log(LOG_NOTICE,
387 "%s: embedded scope mismatch: %s%%%d. "
388 "sin6_scope_id was overridden\n", __func__,
389 ip6_sprintf(ip6buf, &sin6->sin6_addr),
390 sin6->sin6_scope_id);
391 }
392 sin6->sin6_addr.s6_addr16[1] = 0;
393 sin6->sin6_scope_id = zoneid;
394 }
395 }
396
397 return 0;
398 }
399
400 /*
401 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
402 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
403 * in the in6_addr structure, in6 will be modified.
404 *
405 * ret_id - unnecessary?
406 */
407 int
in6_setscope(struct in6_addr * in6,struct ifnet * ifp,u_int32_t * ret_id)408 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
409 {
410 int scope;
411 u_int32_t zoneid = 0;
412 struct scope6_id *sid;
413
414 /*
415 * special case: the loopback address can only belong to a loopback
416 * interface.
417 */
418 if (IN6_IS_ADDR_LOOPBACK(in6)) {
419 if (!(ifp->if_flags & IFF_LOOPBACK))
420 return (EINVAL);
421 } else {
422 scope = in6_addrscope(in6);
423 if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
424 scope == IPV6_ADDR_SCOPE_LINKLOCAL) {
425 /*
426 * Currently we use interface indices as the
427 * zone IDs for interface-local and link-local
428 * scopes.
429 */
430 zoneid = ifp->if_index;
431 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
432 } else if (scope != IPV6_ADDR_SCOPE_GLOBAL) {
433 struct epoch_tracker et;
434
435 NET_EPOCH_ENTER(et);
436 if (ifp->if_afdata[AF_INET6] == NULL) {
437 NET_EPOCH_EXIT(et);
438 return (ENETDOWN);
439 }
440 sid = SID(ifp);
441 zoneid = sid->s6id_list[scope];
442 NET_EPOCH_EXIT(et);
443 }
444 }
445
446 if (ret_id != NULL)
447 *ret_id = zoneid;
448
449 return (0);
450 }
451
452 /*
453 * Just clear the embedded scope identifier. Return 0 if the original address
454 * is intact; return non 0 if the address is modified.
455 */
456 int
in6_clearscope(struct in6_addr * in6)457 in6_clearscope(struct in6_addr *in6)
458 {
459 int modified = 0;
460
461 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
462 if (in6->s6_addr16[1] != 0)
463 modified = 1;
464 in6->s6_addr16[1] = 0;
465 }
466
467 return (modified);
468 }
469
470 /*
471 * Return the scope identifier or zero.
472 */
473 uint16_t
in6_getscope(const struct in6_addr * in6)474 in6_getscope(const struct in6_addr *in6)
475 {
476
477 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
478 return (in6->s6_addr16[1]);
479
480 return (0);
481 }
482
483 /*
484 * Returns scope zone id for the unicast address @in6.
485 *
486 * Returns 0 for global unicast and loopback addresses.
487 * Returns interface index for the link-local addresses.
488 */
489 uint32_t
in6_get_unicast_scopeid(const struct in6_addr * in6,const struct ifnet * ifp)490 in6_get_unicast_scopeid(const struct in6_addr *in6, const struct ifnet *ifp)
491 {
492
493 if (IN6_IS_SCOPE_LINKLOCAL(in6))
494 return (ifp->if_index);
495 return (0);
496 }
497
498 void
in6_set_unicast_scopeid(struct in6_addr * in6,uint32_t scopeid)499 in6_set_unicast_scopeid(struct in6_addr *in6, uint32_t scopeid)
500 {
501
502 in6->s6_addr16[1] = htons(scopeid & 0xffff);
503 }
504
505 /*
506 * Return pointer to ifnet structure, corresponding to the zone id of
507 * link-local scope.
508 */
509 struct ifnet*
in6_getlinkifnet(uint32_t zoneid)510 in6_getlinkifnet(uint32_t zoneid)
511 {
512 struct ifnet *ifp;
513
514 ifp = ifnet_byindex((u_short)zoneid);
515
516 if (ifp == NULL)
517 return (NULL);
518
519 /* An interface might not be IPv6 capable. */
520 if (ifp->if_afdata[AF_INET6] == NULL) {
521 log(LOG_NOTICE,
522 "%s: embedded scope points to an interface without "
523 "IPv6: %s%%%d.\n", __func__,
524 if_name(ifp), zoneid);
525 return (NULL);
526 }
527
528 return (ifp);
529 }
530
531 /*
532 * Return zone id for the specified scope.
533 */
534 uint32_t
in6_getscopezone(const struct ifnet * ifp,int scope)535 in6_getscopezone(const struct ifnet *ifp, int scope)
536 {
537
538 if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
539 scope == IPV6_ADDR_SCOPE_LINKLOCAL)
540 return (ifp->if_index);
541 if (scope >= 0 && scope < IPV6_ADDR_SCOPES_COUNT)
542 return (SID(ifp)->s6id_list[scope]);
543 return (0);
544 }
545
546 /*
547 * Extracts scope from address @dst, stores cleared address
548 * inside @dst and zone inside @scopeid
549 */
550 void
in6_splitscope(const struct in6_addr * src,struct in6_addr * dst,uint32_t * scopeid)551 in6_splitscope(const struct in6_addr *src, struct in6_addr *dst,
552 uint32_t *scopeid)
553 {
554 uint32_t zoneid;
555
556 *dst = *src;
557 zoneid = ntohs(in6_getscope(dst));
558 in6_clearscope(dst);
559 *scopeid = zoneid;
560 }
561
562 /*
563 * This function is for checking sockaddr_in6 structure passed
564 * from the application level (usually).
565 *
566 * sin6_scope_id should be set for link-local unicast, link-local and
567 * interface-local multicast addresses.
568 *
569 * If it is zero, then look into default zone ids. If default zone id is
570 * not set or disabled, then return error.
571 */
572 int
sa6_checkzone(struct sockaddr_in6 * sa6)573 sa6_checkzone(struct sockaddr_in6 *sa6)
574 {
575 int scope;
576
577 scope = in6_addrscope(&sa6->sin6_addr);
578 if (scope == IPV6_ADDR_SCOPE_GLOBAL)
579 return (sa6->sin6_scope_id ? EINVAL: 0);
580 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr) &&
581 scope != IPV6_ADDR_SCOPE_LINKLOCAL &&
582 scope != IPV6_ADDR_SCOPE_INTFACELOCAL) {
583 if (sa6->sin6_scope_id == 0 && V_ip6_use_defzone != 0)
584 sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
585 return (0);
586 }
587 /*
588 * Since ::1 address always configured on the lo0, we can
589 * automatically set its zone id, when it is not specified.
590 * Return error, when specified zone id doesn't match with
591 * actual value.
592 */
593 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) {
594 if (sa6->sin6_scope_id == 0)
595 sa6->sin6_scope_id = in6_getscopezone(V_loif, scope);
596 else if (sa6->sin6_scope_id != in6_getscopezone(V_loif, scope))
597 return (EADDRNOTAVAIL);
598 }
599 /* XXX: we can validate sin6_scope_id here */
600 if (sa6->sin6_scope_id != 0)
601 return (0);
602 if (V_ip6_use_defzone != 0)
603 sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
604 /* Return error if we can't determine zone id */
605 return (sa6->sin6_scope_id ? 0: EADDRNOTAVAIL);
606 }
607
608 /*
609 * This function is similar to sa6_checkzone, but it uses given ifp
610 * to initialize sin6_scope_id.
611 */
612 int
sa6_checkzone_ifp(struct ifnet * ifp,struct sockaddr_in6 * sa6)613 sa6_checkzone_ifp(struct ifnet *ifp, struct sockaddr_in6 *sa6)
614 {
615 int scope;
616
617 scope = in6_addrscope(&sa6->sin6_addr);
618 if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
619 scope == IPV6_ADDR_SCOPE_INTFACELOCAL) {
620 if (sa6->sin6_scope_id == 0) {
621 sa6->sin6_scope_id = in6_getscopezone(ifp, scope);
622 return (0);
623 } else if (sa6->sin6_scope_id != in6_getscopezone(ifp, scope))
624 return (EADDRNOTAVAIL);
625 }
626 return (sa6_checkzone(sa6));
627 }
628