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