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