xref: /freebsd/sys/netinet6/scope6.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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 "opt_route.h"
36 
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/systm.h>
42 #include <sys/queue.h>
43 #include <sys/syslog.h>
44 #include <sys/vimage.h>
45 
46 #include <net/route.h>
47 #include <net/if.h>
48 #include <net/vnet.h>
49 
50 #include <netinet/in.h>
51 
52 #include <netinet/ip6.h>
53 #include <netinet6/in6_var.h>
54 #include <netinet6/scope6_var.h>
55 #include <netinet6/vinet6.h>
56 
57 
58 /*
59  * The scope6_lock protects the global sid default stored in
60  * sid_default below.
61  */
62 static struct mtx scope6_lock;
63 #define	SCOPE6_LOCK_INIT()	mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
64 #define	SCOPE6_LOCK()		mtx_lock(&scope6_lock)
65 #define	SCOPE6_UNLOCK()		mtx_unlock(&scope6_lock)
66 #define	SCOPE6_LOCK_ASSERT()	mtx_assert(&scope6_lock, MA_OWNED)
67 
68 #ifdef VIMAGE_GLOBALS
69 static struct scope6_id sid_default;
70 int ip6_use_defzone;
71 #endif
72 
73 #define SID(ifp) \
74 	(((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
75 
76 void
77 scope6_init(void)
78 {
79 	INIT_VNET_INET6(curvnet);
80 
81 #ifdef ENABLE_DEFAULT_SCOPE
82 	V_ip6_use_defzone = 1;
83 #else
84 	V_ip6_use_defzone = 0;
85 #endif
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 = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
100 	bzero(sid, sizeof(*sid));
101 
102 	/*
103 	 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
104 	 * Should we rather hardcode here?
105 	 */
106 	sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
107 	sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
108 #ifdef MULTI_SCOPE
109 	/* by default, we don't care about scope boundary for these scopes. */
110 	sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
111 	sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
112 #endif
113 
114 	return sid;
115 }
116 
117 void
118 scope6_ifdetach(struct scope6_id *sid)
119 {
120 
121 	free(sid, M_IFADDR);
122 }
123 
124 int
125 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
126 {
127 	INIT_VNET_NET(ifp->if_vnet);
128 	int i;
129 	int error = 0;
130 	struct scope6_id *sid = NULL;
131 
132 	IF_AFDATA_LOCK(ifp);
133 	sid = SID(ifp);
134 
135 	if (!sid) {	/* paranoid? */
136 		IF_AFDATA_UNLOCK(ifp);
137 		return (EINVAL);
138 	}
139 
140 	/*
141 	 * XXX: We need more consistency checks of the relationship among
142 	 * scopes (e.g. an organization should be larger than a site).
143 	 */
144 
145 	/*
146 	 * TODO(XXX): after setting, we should reflect the changes to
147 	 * interface addresses, routing table entries, PCB entries...
148 	 */
149 
150 	SCOPE6_LOCK();
151 	for (i = 0; i < 16; i++) {
152 		if (idlist->s6id_list[i] &&
153 		    idlist->s6id_list[i] != sid->s6id_list[i]) {
154 			/*
155 			 * An interface zone ID must be the corresponding
156 			 * interface index by definition.
157 			 */
158 			if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
159 			    idlist->s6id_list[i] != ifp->if_index) {
160 				IF_AFDATA_UNLOCK(ifp);
161 				SCOPE6_UNLOCK();
162 				return (EINVAL);
163 			}
164 
165 			if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
166 			    idlist->s6id_list[i] > V_if_index) {
167 				/*
168 				 * XXX: theoretically, there should be no
169 				 * relationship between link IDs and interface
170 				 * IDs, but we check the consistency for
171 				 * safety in later use.
172 				 */
173 				IF_AFDATA_UNLOCK(ifp);
174 				SCOPE6_UNLOCK();
175 				return (EINVAL);
176 			}
177 
178 			/*
179 			 * XXX: we must need lots of work in this case,
180 			 * but we simply set the new value in this initial
181 			 * implementation.
182 			 */
183 			sid->s6id_list[i] = idlist->s6id_list[i];
184 		}
185 	}
186 	SCOPE6_UNLOCK();
187 	IF_AFDATA_UNLOCK(ifp);
188 
189 	return (error);
190 }
191 
192 int
193 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
194 {
195 	/* We only need to lock the interface's afdata for SID() to work. */
196 	IF_AFDATA_LOCK(ifp);
197 	struct scope6_id *sid = SID(ifp);
198 
199 	if (sid == NULL) {	/* paranoid? */
200 		IF_AFDATA_UNLOCK(ifp);
201 		return (EINVAL);
202 	}
203 
204 	SCOPE6_LOCK();
205 	*idlist = *sid;
206 	SCOPE6_UNLOCK();
207 
208 	IF_AFDATA_UNLOCK(ifp);
209 	return (0);
210 }
211 
212 
213 /*
214  * Get a scope of the address. Node-local, link-local, site-local or global.
215  */
216 int
217 in6_addrscope(struct in6_addr *addr)
218 {
219 	int scope;
220 
221 	if (addr->s6_addr[0] == 0xfe) {
222 		scope = addr->s6_addr[1] & 0xc0;
223 
224 		switch (scope) {
225 		case 0x80:
226 			return IPV6_ADDR_SCOPE_LINKLOCAL;
227 			break;
228 		case 0xc0:
229 			return IPV6_ADDR_SCOPE_SITELOCAL;
230 			break;
231 		default:
232 			return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
233 			break;
234 		}
235 	}
236 
237 
238 	if (addr->s6_addr[0] == 0xff) {
239 		scope = addr->s6_addr[1] & 0x0f;
240 
241 		/*
242 		 * due to other scope such as reserved,
243 		 * return scope doesn't work.
244 		 */
245 		switch (scope) {
246 		case IPV6_ADDR_SCOPE_INTFACELOCAL:
247 			return IPV6_ADDR_SCOPE_INTFACELOCAL;
248 			break;
249 		case IPV6_ADDR_SCOPE_LINKLOCAL:
250 			return IPV6_ADDR_SCOPE_LINKLOCAL;
251 			break;
252 		case IPV6_ADDR_SCOPE_SITELOCAL:
253 			return IPV6_ADDR_SCOPE_SITELOCAL;
254 			break;
255 		default:
256 			return IPV6_ADDR_SCOPE_GLOBAL;
257 			break;
258 		}
259 	}
260 
261 	/*
262 	 * Regard loopback and unspecified addresses as global, since
263 	 * they have no ambiguity.
264 	 */
265 	if (bcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
266 		if (addr->s6_addr[15] == 1) /* loopback */
267 			return IPV6_ADDR_SCOPE_LINKLOCAL;
268 		if (addr->s6_addr[15] == 0) /* unspecified */
269 			return IPV6_ADDR_SCOPE_GLOBAL; /* XXX: correct? */
270 	}
271 
272 	return IPV6_ADDR_SCOPE_GLOBAL;
273 }
274 
275 /*
276  * ifp - note that this might be NULL
277  */
278 
279 void
280 scope6_setdefault(struct ifnet *ifp)
281 {
282 	INIT_VNET_INET6(ifp->if_vnet);
283 
284 	/*
285 	 * Currently, this function just sets the default "interfaces"
286 	 * and "links" according to the given interface.
287 	 * We might eventually have to separate the notion of "link" from
288 	 * "interface" and provide a user interface to set the default.
289 	 */
290 	SCOPE6_LOCK();
291 	if (ifp) {
292 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
293 			ifp->if_index;
294 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
295 			ifp->if_index;
296 	} else {
297 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
298 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
299 	}
300 	SCOPE6_UNLOCK();
301 }
302 
303 int
304 scope6_get_default(struct scope6_id *idlist)
305 {
306 	INIT_VNET_INET6(curvnet);
307 
308 	SCOPE6_LOCK();
309 	*idlist = V_sid_default;
310 	SCOPE6_UNLOCK();
311 
312 	return (0);
313 }
314 
315 u_int32_t
316 scope6_addr2default(struct in6_addr *addr)
317 {
318 	INIT_VNET_INET6(curvnet);
319 	u_int32_t id;
320 
321 	/*
322 	 * special case: The loopback address should be considered as
323 	 * link-local, but there's no ambiguity in the syntax.
324 	 */
325 	if (IN6_IS_ADDR_LOOPBACK(addr))
326 		return (0);
327 
328 	/*
329 	 * XXX: 32-bit read is atomic on all our platforms, is it OK
330 	 * not to lock here?
331 	 */
332 	SCOPE6_LOCK();
333 	id = V_sid_default.s6id_list[in6_addrscope(addr)];
334 	SCOPE6_UNLOCK();
335 	return (id);
336 }
337 
338 /*
339  * Validate the specified scope zone ID in the sin6_scope_id field.  If the ID
340  * is unspecified (=0), needs to be specified, and the default zone ID can be
341  * used, the default value will be used.
342  * This routine then generates the kernel-internal form: if the address scope
343  * of is interface-local or link-local, embed the interface index in the
344  * address.
345  */
346 int
347 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
348 {
349 	INIT_VNET_NET(curvnet);
350 	struct ifnet *ifp;
351 	u_int32_t zoneid;
352 
353 	if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
354 		zoneid = scope6_addr2default(&sin6->sin6_addr);
355 
356 	if (zoneid != 0 &&
357 	    (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
358 	    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
359 		/*
360 		 * At this moment, we only check interface-local and
361 		 * link-local scope IDs, and use interface indices as the
362 		 * zone IDs assuming a one-to-one mapping between interfaces
363 		 * and links.
364 		 */
365 		if (V_if_index < zoneid)
366 			return (ENXIO);
367 		ifp = ifnet_byindex(zoneid);
368 		if (ifp == NULL) /* XXX: this can happen for some OS */
369 			return (ENXIO);
370 
371 		/* XXX assignment to 16bit from 32bit variable */
372 		sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
373 
374 		sin6->sin6_scope_id = 0;
375 	}
376 
377 	return 0;
378 }
379 
380 /*
381  * generate standard sockaddr_in6 from embedded form.
382  */
383 int
384 sa6_recoverscope(struct sockaddr_in6 *sin6)
385 {
386 	INIT_VNET_NET(curvnet);
387 	char ip6buf[INET6_ADDRSTRLEN];
388 	u_int32_t zoneid;
389 
390 	if (sin6->sin6_scope_id != 0) {
391 		log(LOG_NOTICE,
392 		    "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
393 		    ip6_sprintf(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id);
394 		/* XXX: proceed anyway... */
395 	}
396 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
397 	    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
398 		/*
399 		 * KAME assumption: link id == interface id
400 		 */
401 		zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
402 		if (zoneid) {
403 			/* sanity check */
404 			if (zoneid < 0 || V_if_index < zoneid)
405 				return (ENXIO);
406 			if (!ifnet_byindex(zoneid))
407 				return (ENXIO);
408 			sin6->sin6_addr.s6_addr16[1] = 0;
409 			sin6->sin6_scope_id = zoneid;
410 		}
411 	}
412 
413 	return 0;
414 }
415 
416 /*
417  * Determine the appropriate scope zone ID for in6 and ifp.  If ret_id is
418  * non NULL, it is set to the zone ID.  If the zone ID needs to be embedded
419  * in the in6_addr structure, in6 will be modified.
420  *
421  * ret_id - unnecessary?
422  */
423 int
424 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
425 {
426 	int scope;
427 	u_int32_t zoneid = 0;
428 	struct scope6_id *sid;
429 
430 	IF_AFDATA_LOCK(ifp);
431 
432 	sid = SID(ifp);
433 
434 #ifdef DIAGNOSTIC
435 	if (sid == NULL) { /* should not happen */
436 		panic("in6_setscope: scope array is NULL");
437 		/* NOTREACHED */
438 	}
439 #endif
440 
441 	/*
442 	 * special case: the loopback address can only belong to a loopback
443 	 * interface.
444 	 */
445 	if (IN6_IS_ADDR_LOOPBACK(in6)) {
446 		if (!(ifp->if_flags & IFF_LOOPBACK)) {
447 			IF_AFDATA_UNLOCK(ifp);
448 			return (EINVAL);
449 		} else {
450 			if (ret_id != NULL)
451 				*ret_id = 0; /* there's no ambiguity */
452 			IF_AFDATA_UNLOCK(ifp);
453 			return (0);
454 		}
455 	}
456 
457 	scope = in6_addrscope(in6);
458 
459 	SCOPE6_LOCK();
460 	switch (scope) {
461 	case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
462 		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
463 		break;
464 
465 	case IPV6_ADDR_SCOPE_LINKLOCAL:
466 		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
467 		break;
468 
469 	case IPV6_ADDR_SCOPE_SITELOCAL:
470 		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
471 		break;
472 
473 	case IPV6_ADDR_SCOPE_ORGLOCAL:
474 		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
475 		break;
476 
477 	default:
478 		zoneid = 0;	/* XXX: treat as global. */
479 		break;
480 	}
481 	SCOPE6_UNLOCK();
482 	IF_AFDATA_UNLOCK(ifp);
483 
484 	if (ret_id != NULL)
485 		*ret_id = zoneid;
486 
487 	if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
488 		in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
489 
490 	return (0);
491 }
492 
493 /*
494  * Just clear the embedded scope identifier.  Return 0 if the original address
495  * is intact; return non 0 if the address is modified.
496  */
497 int
498 in6_clearscope(struct in6_addr *in6)
499 {
500 	int modified = 0;
501 
502 	if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
503 		if (in6->s6_addr16[1] != 0)
504 			modified = 1;
505 		in6->s6_addr16[1] = 0;
506 	}
507 
508 	return (modified);
509 }
510