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