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