xref: /freebsd/sys/netinet6/scope6.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
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/sockio.h>
40 #include <sys/systm.h>
41 #include <sys/queue.h>
42 #include <sys/sysctl.h>
43 #include <sys/syslog.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/vnet.h>
48 
49 #include <netinet/in.h>
50 
51 #include <netinet/ip6.h>
52 #include <netinet6/in6_var.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet6/scope6_var.h>
55 
56 #ifdef ENABLE_DEFAULT_SCOPE
57 VNET_DEFINE(int, ip6_use_defzone) = 1;
58 #else
59 VNET_DEFINE(int, ip6_use_defzone) = 0;
60 #endif
61 VNET_DEFINE(int, deembed_scopeid) = 1;
62 SYSCTL_DECL(_net_inet6_ip6);
63 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, deembed_scopeid, CTLFLAG_VNET | CTLFLAG_RW,
64     &VNET_NAME(deembed_scopeid), 0,
65     "Extract embedded zone ID and set it to sin6_scope_id in sockaddr_in6.");
66 
67 /*
68  * The scope6_lock protects the global sid default stored in
69  * sid_default below.
70  */
71 static struct mtx scope6_lock;
72 #define	SCOPE6_LOCK_INIT()	mtx_init(&scope6_lock, "scope6_lock", NULL, MTX_DEF)
73 #define	SCOPE6_LOCK()		mtx_lock(&scope6_lock)
74 #define	SCOPE6_UNLOCK()		mtx_unlock(&scope6_lock)
75 #define	SCOPE6_LOCK_ASSERT()	mtx_assert(&scope6_lock, MA_OWNED)
76 
77 static VNET_DEFINE(struct scope6_id, sid_default);
78 #define	V_sid_default			VNET(sid_default)
79 
80 #define SID(ifp) \
81 	(((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
82 
83 static int	scope6_get(struct ifnet *, struct scope6_id *);
84 static int	scope6_set(struct ifnet *, struct scope6_id *);
85 
86 void
87 scope6_init(void)
88 {
89 
90 	bzero(&V_sid_default, sizeof(V_sid_default));
91 
92 	if (!IS_DEFAULT_VNET(curvnet))
93 		return;
94 
95 	SCOPE6_LOCK_INIT();
96 }
97 
98 struct scope6_id *
99 scope6_ifattach(struct ifnet *ifp)
100 {
101 	struct scope6_id *sid;
102 
103 	sid = malloc(sizeof(*sid), M_IFADDR, M_WAITOK | M_ZERO);
104 	/*
105 	 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
106 	 * Should we rather hardcode here?
107 	 */
108 	sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
109 	sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
110 	return (sid);
111 }
112 
113 void
114 scope6_ifdetach(struct scope6_id *sid)
115 {
116 
117 	free(sid, M_IFADDR);
118 }
119 
120 int
121 scope6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
122 {
123 	struct in6_ifreq *ifr;
124 
125 	if (ifp->if_afdata[AF_INET6] == NULL)
126 		return (EPFNOSUPPORT);
127 
128 	ifr = (struct in6_ifreq *)data;
129 	switch (cmd) {
130 	case SIOCSSCOPE6:
131 		return (scope6_set(ifp,
132 		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
133 	case SIOCGSCOPE6:
134 		return (scope6_get(ifp,
135 		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
136 	case SIOCGSCOPE6DEF:
137 		return (scope6_get_default(
138 		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
139 	default:
140 		return (EOPNOTSUPP);
141 	}
142 }
143 
144 static int
145 scope6_set(struct ifnet *ifp, struct scope6_id *idlist)
146 {
147 	int i;
148 	int error = 0;
149 	struct scope6_id *sid = NULL;
150 
151 	IF_AFDATA_WLOCK(ifp);
152 	sid = SID(ifp);
153 
154 	if (!sid) {	/* paranoid? */
155 		IF_AFDATA_WUNLOCK(ifp);
156 		return (EINVAL);
157 	}
158 
159 	/*
160 	 * XXX: We need more consistency checks of the relationship among
161 	 * scopes (e.g. an organization should be larger than a site).
162 	 */
163 
164 	/*
165 	 * TODO(XXX): after setting, we should reflect the changes to
166 	 * interface addresses, routing table entries, PCB entries...
167 	 */
168 
169 	for (i = 0; i < 16; i++) {
170 		if (idlist->s6id_list[i] &&
171 		    idlist->s6id_list[i] != sid->s6id_list[i]) {
172 			/*
173 			 * An interface zone ID must be the corresponding
174 			 * interface index by definition.
175 			 */
176 			if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
177 			    idlist->s6id_list[i] != ifp->if_index) {
178 				IF_AFDATA_WUNLOCK(ifp);
179 				return (EINVAL);
180 			}
181 
182 			if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
183 			    idlist->s6id_list[i] > V_if_index) {
184 				/*
185 				 * XXX: theoretically, there should be no
186 				 * relationship between link IDs and interface
187 				 * IDs, but we check the consistency for
188 				 * safety in later use.
189 				 */
190 				IF_AFDATA_WUNLOCK(ifp);
191 				return (EINVAL);
192 			}
193 
194 			/*
195 			 * XXX: we must need lots of work in this case,
196 			 * but we simply set the new value in this initial
197 			 * implementation.
198 			 */
199 			sid->s6id_list[i] = idlist->s6id_list[i];
200 		}
201 	}
202 	IF_AFDATA_WUNLOCK(ifp);
203 
204 	return (error);
205 }
206 
207 static int
208 scope6_get(struct ifnet *ifp, struct scope6_id *idlist)
209 {
210 	struct scope6_id *sid;
211 
212 	/* We only need to lock the interface's afdata for SID() to work. */
213 	IF_AFDATA_RLOCK(ifp);
214 	sid = SID(ifp);
215 	if (sid == NULL) {	/* paranoid? */
216 		IF_AFDATA_RUNLOCK(ifp);
217 		return (EINVAL);
218 	}
219 
220 	*idlist = *sid;
221 
222 	IF_AFDATA_RUNLOCK(ifp);
223 	return (0);
224 }
225 
226 /*
227  * Get a scope of the address. Node-local, link-local, site-local or global.
228  */
229 int
230 in6_addrscope(const struct in6_addr *addr)
231 {
232 
233 	if (IN6_IS_ADDR_MULTICAST(addr)) {
234 		/*
235 		 * Addresses with reserved value F must be treated as
236 		 * global multicast addresses.
237 		 */
238 		if (IPV6_ADDR_MC_SCOPE(addr) == 0x0f)
239 			return (IPV6_ADDR_SCOPE_GLOBAL);
240 		return (IPV6_ADDR_MC_SCOPE(addr));
241 	}
242 	if (IN6_IS_ADDR_LINKLOCAL(addr) ||
243 	    IN6_IS_ADDR_LOOPBACK(addr))
244 		return (IPV6_ADDR_SCOPE_LINKLOCAL);
245 	if (IN6_IS_ADDR_SITELOCAL(addr))
246 		return (IPV6_ADDR_SCOPE_SITELOCAL);
247 	return (IPV6_ADDR_SCOPE_GLOBAL);
248 }
249 
250 /*
251  * ifp - note that this might be NULL
252  */
253 
254 void
255 scope6_setdefault(struct ifnet *ifp)
256 {
257 
258 	/*
259 	 * Currently, this function just sets the default "interfaces"
260 	 * and "links" according to the given interface.
261 	 * We might eventually have to separate the notion of "link" from
262 	 * "interface" and provide a user interface to set the default.
263 	 */
264 	SCOPE6_LOCK();
265 	if (ifp) {
266 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
267 			ifp->if_index;
268 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
269 			ifp->if_index;
270 	} else {
271 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
272 		V_sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
273 	}
274 	SCOPE6_UNLOCK();
275 }
276 
277 int
278 scope6_get_default(struct scope6_id *idlist)
279 {
280 
281 	SCOPE6_LOCK();
282 	*idlist = V_sid_default;
283 	SCOPE6_UNLOCK();
284 
285 	return (0);
286 }
287 
288 u_int32_t
289 scope6_addr2default(struct in6_addr *addr)
290 {
291 	u_int32_t id;
292 
293 	/*
294 	 * special case: The loopback address should be considered as
295 	 * link-local, but there's no ambiguity in the syntax.
296 	 */
297 	if (IN6_IS_ADDR_LOOPBACK(addr))
298 		return (0);
299 
300 	/*
301 	 * XXX: 32-bit read is atomic on all our platforms, is it OK
302 	 * not to lock here?
303 	 */
304 	SCOPE6_LOCK();
305 	id = V_sid_default.s6id_list[in6_addrscope(addr)];
306 	SCOPE6_UNLOCK();
307 	return (id);
308 }
309 
310 /*
311  * Validate the specified scope zone ID in the sin6_scope_id field.  If the ID
312  * is unspecified (=0), needs to be specified, and the default zone ID can be
313  * used, the default value will be used.
314  * This routine then generates the kernel-internal form: if the address scope
315  * of is interface-local or link-local, embed the interface index in the
316  * address.
317  */
318 int
319 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
320 {
321 	u_int32_t zoneid;
322 
323 	if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
324 		zoneid = scope6_addr2default(&sin6->sin6_addr);
325 
326 	if (zoneid != 0 &&
327 	    (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
328 	    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
329 		/*
330 		 * At this moment, we only check interface-local and
331 		 * link-local scope IDs, and use interface indices as the
332 		 * zone IDs assuming a one-to-one mapping between interfaces
333 		 * and links.
334 		 */
335 		if (V_if_index < zoneid || ifnet_byindex(zoneid) == NULL)
336 			return (ENXIO);
337 
338 		/* XXX assignment to 16bit from 32bit variable */
339 		sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
340 		sin6->sin6_scope_id = 0;
341 	}
342 
343 	return 0;
344 }
345 
346 /*
347  * generate standard sockaddr_in6 from embedded form.
348  */
349 int
350 sa6_recoverscope(struct sockaddr_in6 *sin6)
351 {
352 	char ip6buf[INET6_ADDRSTRLEN];
353 	u_int32_t zoneid;
354 
355 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
356 	    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
357 		/*
358 		 * KAME assumption: link id == interface id
359 		 */
360 		zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
361 		if (zoneid) {
362 			/* sanity check */
363 			if (V_if_index < zoneid)
364 				return (ENXIO);
365 #if 0
366 			/* XXX: Disabled due to possible deadlock. */
367 			if (!ifnet_byindex(zoneid))
368 				return (ENXIO);
369 #endif
370 			if (sin6->sin6_scope_id != 0 &&
371 			    zoneid != sin6->sin6_scope_id) {
372 				log(LOG_NOTICE,
373 				    "%s: embedded scope mismatch: %s%%%d. "
374 				    "sin6_scope_id was overridden.", __func__,
375 				    ip6_sprintf(ip6buf, &sin6->sin6_addr),
376 				    sin6->sin6_scope_id);
377 			}
378 			sin6->sin6_addr.s6_addr16[1] = 0;
379 			sin6->sin6_scope_id = zoneid;
380 		}
381 	}
382 
383 	return 0;
384 }
385 
386 /*
387  * Determine the appropriate scope zone ID for in6 and ifp.  If ret_id is
388  * non NULL, it is set to the zone ID.  If the zone ID needs to be embedded
389  * in the in6_addr structure, in6 will be modified.
390  *
391  * ret_id - unnecessary?
392  */
393 int
394 in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
395 {
396 	int scope;
397 	u_int32_t zoneid = 0;
398 	struct scope6_id *sid;
399 
400 	/*
401 	 * special case: the loopback address can only belong to a loopback
402 	 * interface.
403 	 */
404 	if (IN6_IS_ADDR_LOOPBACK(in6)) {
405 		if (!(ifp->if_flags & IFF_LOOPBACK))
406 			return (EINVAL);
407 	} else {
408 		scope = in6_addrscope(in6);
409 		if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
410 		    scope == IPV6_ADDR_SCOPE_LINKLOCAL) {
411 			/*
412 			 * Currently we use interface indeces as the
413 			 * zone IDs for interface-local and link-local
414 			 * scopes.
415 			 */
416 			zoneid = ifp->if_index;
417 			in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
418 		} else if (scope != IPV6_ADDR_SCOPE_GLOBAL) {
419 			IF_AFDATA_RLOCK(ifp);
420 			sid = SID(ifp);
421 			zoneid = sid->s6id_list[scope];
422 			IF_AFDATA_RUNLOCK(ifp);
423 		}
424 	}
425 
426 	if (ret_id != NULL)
427 		*ret_id = zoneid;
428 
429 	return (0);
430 }
431 
432 /*
433  * Just clear the embedded scope identifier.  Return 0 if the original address
434  * is intact; return non 0 if the address is modified.
435  */
436 int
437 in6_clearscope(struct in6_addr *in6)
438 {
439 	int modified = 0;
440 
441 	if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
442 		if (in6->s6_addr16[1] != 0)
443 			modified = 1;
444 		in6->s6_addr16[1] = 0;
445 	}
446 
447 	return (modified);
448 }
449 
450 /*
451  * Return the scope identifier or zero.
452  */
453 uint16_t
454 in6_getscope(struct in6_addr *in6)
455 {
456 
457 	if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
458 		return (in6->s6_addr16[1]);
459 
460 	return (0);
461 }
462 
463 /*
464  * Return pointer to ifnet structure, corresponding to the zone id of
465  * link-local scope.
466  */
467 struct ifnet*
468 in6_getlinkifnet(uint32_t zoneid)
469 {
470 
471 	return (ifnet_byindex((u_short)zoneid));
472 }
473 
474 /*
475  * Return zone id for the specified scope.
476  */
477 uint32_t
478 in6_getscopezone(const struct ifnet *ifp, int scope)
479 {
480 
481 	if (scope == IPV6_ADDR_SCOPE_INTFACELOCAL ||
482 	    scope == IPV6_ADDR_SCOPE_LINKLOCAL)
483 		return (ifp->if_index);
484 	if (scope >= 0 && scope < IPV6_ADDR_SCOPES_COUNT)
485 		return (SID(ifp)->s6id_list[scope]);
486 	return (0);
487 }
488 
489 /*
490  * This function is for checking sockaddr_in6 structure passed
491  * from the application level (usually).
492  *
493  * sin6_scope_id should be set for link-local unicast, link-local and
494  * interface-local  multicast addresses.
495  *
496  * If it is zero, then look into default zone ids. If default zone id is
497  * not set or disabled, then return error.
498  */
499 int
500 sa6_checkzone(struct sockaddr_in6 *sa6)
501 {
502 	int scope;
503 
504 	scope = in6_addrscope(&sa6->sin6_addr);
505 	if (scope == IPV6_ADDR_SCOPE_GLOBAL)
506 		return (sa6->sin6_scope_id ? EINVAL: 0);
507 	if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr) &&
508 	    scope != IPV6_ADDR_SCOPE_LINKLOCAL &&
509 	    scope != IPV6_ADDR_SCOPE_INTFACELOCAL) {
510 		if (sa6->sin6_scope_id == 0 && V_ip6_use_defzone != 0)
511 			sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
512 		return (0);
513 	}
514 	/*
515 	 * Since ::1 address always configured on the lo0, we can
516 	 * automatically set its zone id, when it is not specified.
517 	 * Return error, when specified zone id doesn't match with
518 	 * actual value.
519 	 */
520 	if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) {
521 		if (sa6->sin6_scope_id == 0)
522 			sa6->sin6_scope_id = in6_getscopezone(V_loif, scope);
523 		else if (sa6->sin6_scope_id != in6_getscopezone(V_loif, scope))
524 			return (EADDRNOTAVAIL);
525 	}
526 	/* XXX: we can validate sin6_scope_id here */
527 	if (sa6->sin6_scope_id != 0)
528 		return (0);
529 	if (V_ip6_use_defzone != 0)
530 		sa6->sin6_scope_id = V_sid_default.s6id_list[scope];
531 	/* Return error if we can't determine zone id */
532 	return (sa6->sin6_scope_id ? 0: EADDRNOTAVAIL);
533 }
534 
535 /*
536  * This function is similar to sa6_checkzone, but it uses given ifp
537  * to initialize sin6_scope_id.
538  */
539 int
540 sa6_checkzone_ifp(struct ifnet *ifp, struct sockaddr_in6 *sa6)
541 {
542 	int scope;
543 
544 	scope = in6_addrscope(&sa6->sin6_addr);
545 	if (scope == IPV6_ADDR_SCOPE_LINKLOCAL ||
546 	    scope == IPV6_ADDR_SCOPE_INTFACELOCAL) {
547 		if (sa6->sin6_scope_id == 0) {
548 			sa6->sin6_scope_id = in6_getscopezone(ifp, scope);
549 			return (0);
550 		} else if (sa6->sin6_scope_id != in6_getscopezone(ifp, scope))
551 			return (EADDRNOTAVAIL);
552 	}
553 	return (sa6_checkzone(sa6));
554 }
555 
556 
557