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