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
54VNET_DEFINE(int, ip6_use_defzone) = 1;
55#else
56VNET_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 */
63static 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
69static 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
75void
76scope6_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
87struct scope6_id *
88scope6_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
110void
111scope6_ifdetach(struct scope6_id *sid)
112{
113
114	free(sid, M_IFADDR);
115}
116
117int
118scope6_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
180int
181scope6_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 */
203int
204in6_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
266void
267scope6_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
289int
290scope6_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
300u_int32_t
301scope6_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 */
330int
331sa6_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 */
366int
367sa6_recoverscope(struct sockaddr_in6 *sin6)
368{
369	char ip6buf[INET6_ADDRSTRLEN];
370	u_int32_t zoneid;
371
372	if (sin6->sin6_scope_id != 0) {
373		log(LOG_NOTICE,
374		    "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
375		    ip6_sprintf(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id);
376		/* XXX: proceed anyway... */
377	}
378	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
379	    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
380		/*
381		 * KAME assumption: link id == interface id
382		 */
383		zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
384		if (zoneid) {
385			/* sanity check */
386			if (V_if_index < zoneid)
387				return (ENXIO);
388			if (!ifnet_byindex(zoneid))
389				return (ENXIO);
390			sin6->sin6_addr.s6_addr16[1] = 0;
391			sin6->sin6_scope_id = zoneid;
392		}
393	}
394
395	return 0;
396}
397
398/*
399 * Determine the appropriate scope zone ID for in6 and ifp.  If ret_id is
400 * non NULL, it is set to the zone ID.  If the zone ID needs to be embedded
401 * in the in6_addr structure, in6 will be modified.
402 *
403 * ret_id - unnecessary?
404 */
405int
406in6_setscope(struct in6_addr *in6, struct ifnet *ifp, u_int32_t *ret_id)
407{
408	int scope;
409	u_int32_t zoneid = 0;
410	struct scope6_id *sid;
411
412	IF_AFDATA_RLOCK(ifp);
413
414	sid = SID(ifp);
415
416#ifdef DIAGNOSTIC
417	if (sid == NULL) { /* should not happen */
418		panic("in6_setscope: scope array is NULL");
419		/* NOTREACHED */
420	}
421#endif
422
423	/*
424	 * special case: the loopback address can only belong to a loopback
425	 * interface.
426	 */
427	if (IN6_IS_ADDR_LOOPBACK(in6)) {
428		if (!(ifp->if_flags & IFF_LOOPBACK)) {
429			IF_AFDATA_RUNLOCK(ifp);
430			return (EINVAL);
431		} else {
432			if (ret_id != NULL)
433				*ret_id = 0; /* there's no ambiguity */
434			IF_AFDATA_RUNLOCK(ifp);
435			return (0);
436		}
437	}
438
439	scope = in6_addrscope(in6);
440	switch (scope) {
441	case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
442		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
443		break;
444
445	case IPV6_ADDR_SCOPE_LINKLOCAL:
446		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
447		break;
448
449	case IPV6_ADDR_SCOPE_SITELOCAL:
450		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
451		break;
452
453	case IPV6_ADDR_SCOPE_ORGLOCAL:
454		zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
455		break;
456
457	default:
458		zoneid = 0;	/* XXX: treat as global. */
459		break;
460	}
461	IF_AFDATA_RUNLOCK(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 */
476int
477in6_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
490/*
491 * Return the scope identifier or zero.
492 */
493uint16_t
494in6_getscope(struct in6_addr *in6)
495{
496
497	if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6))
498		return (in6->s6_addr16[1]);
499
500	return (0);
501}
502