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