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