scope6.c revision 1.2.12.5 1 1.2.12.5 yamt /* $NetBSD: scope6.c,v 1.2.12.5 2008/01/21 09:47:25 yamt Exp $ */
2 1.2.12.2 yamt /* $KAME$ */
3 1.2.12.2 yamt
4 1.2.12.2 yamt /*-
5 1.2.12.2 yamt * Copyright (C) 2000 WIDE Project.
6 1.2.12.2 yamt * All rights reserved.
7 1.2.12.2 yamt *
8 1.2.12.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.2.12.2 yamt * modification, are permitted provided that the following conditions
10 1.2.12.2 yamt * are met:
11 1.2.12.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.2.12.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.2.12.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.12.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.2.12.2 yamt * documentation and/or other materials provided with the distribution.
16 1.2.12.2 yamt * 3. Neither the name of the project nor the names of its contributors
17 1.2.12.2 yamt * may be used to endorse or promote products derived from this software
18 1.2.12.2 yamt * without specific prior written permission.
19 1.2.12.2 yamt *
20 1.2.12.2 yamt * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2.12.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2.12.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2.12.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2.12.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2.12.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2.12.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2.12.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2.12.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2.12.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2.12.2 yamt * SUCH DAMAGE.
31 1.2.12.2 yamt */
32 1.2.12.2 yamt
33 1.2.12.5 yamt #include <sys/cdefs.h>
34 1.2.12.5 yamt __KERNEL_RCSID(0, "$NetBSD: scope6.c,v 1.2.12.5 2008/01/21 09:47:25 yamt Exp $");
35 1.2.12.5 yamt
36 1.2.12.2 yamt #include <sys/param.h>
37 1.2.12.2 yamt #include <sys/malloc.h>
38 1.2.12.2 yamt #include <sys/mbuf.h>
39 1.2.12.2 yamt #include <sys/socket.h>
40 1.2.12.2 yamt #include <sys/systm.h>
41 1.2.12.2 yamt #include <sys/queue.h>
42 1.2.12.2 yamt #include <sys/syslog.h>
43 1.2.12.2 yamt
44 1.2.12.2 yamt #include <net/route.h>
45 1.2.12.2 yamt #include <net/if.h>
46 1.2.12.2 yamt
47 1.2.12.2 yamt #include <netinet/in.h>
48 1.2.12.2 yamt
49 1.2.12.2 yamt #include <netinet6/in6_var.h>
50 1.2.12.2 yamt #include <netinet6/scope6_var.h>
51 1.2.12.2 yamt
52 1.2.12.2 yamt #ifdef ENABLE_DEFAULT_SCOPE
53 1.2.12.2 yamt int ip6_use_defzone = 1;
54 1.2.12.2 yamt #else
55 1.2.12.2 yamt int ip6_use_defzone = 0;
56 1.2.12.2 yamt #endif
57 1.2.12.2 yamt
58 1.2.12.2 yamt static struct scope6_id sid_default;
59 1.2.12.2 yamt #define SID(ifp) \
60 1.2.12.2 yamt (((struct in6_ifextra *)(ifp)->if_afdata[AF_INET6])->scope6_id)
61 1.2.12.2 yamt
62 1.2.12.2 yamt void
63 1.2.12.2 yamt scope6_init()
64 1.2.12.2 yamt {
65 1.2.12.2 yamt
66 1.2.12.2 yamt memset(&sid_default, 0, sizeof(sid_default));
67 1.2.12.2 yamt }
68 1.2.12.2 yamt
69 1.2.12.2 yamt struct scope6_id *
70 1.2.12.2 yamt scope6_ifattach(struct ifnet *ifp)
71 1.2.12.2 yamt {
72 1.2.12.2 yamt struct scope6_id *sid;
73 1.2.12.2 yamt
74 1.2.12.2 yamt sid = (struct scope6_id *)malloc(sizeof(*sid), M_IFADDR, M_WAITOK);
75 1.2.12.2 yamt memset(sid, 0, sizeof(*sid));
76 1.2.12.2 yamt
77 1.2.12.2 yamt /*
78 1.2.12.2 yamt * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard.
79 1.2.12.2 yamt * Should we rather hardcode here?
80 1.2.12.2 yamt */
81 1.2.12.2 yamt sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index;
82 1.2.12.2 yamt sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index;
83 1.2.12.2 yamt #ifdef MULTI_SCOPE
84 1.2.12.2 yamt /* by default, we don't care about scope boundary for these scopes. */
85 1.2.12.2 yamt sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1;
86 1.2.12.2 yamt sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1;
87 1.2.12.2 yamt #endif
88 1.2.12.2 yamt
89 1.2.12.2 yamt return sid;
90 1.2.12.2 yamt }
91 1.2.12.2 yamt
92 1.2.12.2 yamt void
93 1.2.12.2 yamt scope6_ifdetach(struct scope6_id *sid)
94 1.2.12.2 yamt {
95 1.2.12.2 yamt
96 1.2.12.2 yamt free(sid, M_IFADDR);
97 1.2.12.2 yamt }
98 1.2.12.2 yamt
99 1.2.12.2 yamt int
100 1.2.12.4 yamt scope6_set(struct ifnet *ifp, const struct scope6_id *idlist)
101 1.2.12.2 yamt {
102 1.2.12.2 yamt int i;
103 1.2.12.2 yamt int error = 0;
104 1.2.12.2 yamt struct scope6_id *sid = NULL;
105 1.2.12.2 yamt
106 1.2.12.2 yamt sid = SID(ifp);
107 1.2.12.2 yamt
108 1.2.12.2 yamt if (!sid) /* paranoid? */
109 1.2.12.2 yamt return (EINVAL);
110 1.2.12.2 yamt
111 1.2.12.2 yamt /*
112 1.2.12.2 yamt * XXX: We need more consistency checks of the relationship among
113 1.2.12.2 yamt * scopes (e.g. an organization should be larger than a site).
114 1.2.12.2 yamt */
115 1.2.12.2 yamt
116 1.2.12.2 yamt /*
117 1.2.12.2 yamt * TODO(XXX): after setting, we should reflect the changes to
118 1.2.12.2 yamt * interface addresses, routing table entries, PCB entries...
119 1.2.12.2 yamt */
120 1.2.12.2 yamt
121 1.2.12.2 yamt for (i = 0; i < 16; i++) {
122 1.2.12.2 yamt if (idlist->s6id_list[i] &&
123 1.2.12.2 yamt idlist->s6id_list[i] != sid->s6id_list[i]) {
124 1.2.12.2 yamt /*
125 1.2.12.2 yamt * An interface zone ID must be the corresponding
126 1.2.12.2 yamt * interface index by definition.
127 1.2.12.2 yamt */
128 1.2.12.2 yamt if (i == IPV6_ADDR_SCOPE_INTFACELOCAL &&
129 1.2.12.2 yamt idlist->s6id_list[i] != ifp->if_index)
130 1.2.12.2 yamt return (EINVAL);
131 1.2.12.2 yamt
132 1.2.12.2 yamt if (i == IPV6_ADDR_SCOPE_LINKLOCAL &&
133 1.2.12.2 yamt idlist->s6id_list[i] >= if_indexlim) {
134 1.2.12.2 yamt /*
135 1.2.12.2 yamt * XXX: theoretically, there should be no
136 1.2.12.2 yamt * relationship between link IDs and interface
137 1.2.12.2 yamt * IDs, but we check the consistency for
138 1.2.12.2 yamt * safety in later use.
139 1.2.12.2 yamt */
140 1.2.12.2 yamt return (EINVAL);
141 1.2.12.2 yamt }
142 1.2.12.2 yamt
143 1.2.12.2 yamt /*
144 1.2.12.2 yamt * XXX: we must need lots of work in this case,
145 1.2.12.2 yamt * but we simply set the new value in this initial
146 1.2.12.2 yamt * implementation.
147 1.2.12.2 yamt */
148 1.2.12.2 yamt sid->s6id_list[i] = idlist->s6id_list[i];
149 1.2.12.2 yamt }
150 1.2.12.2 yamt }
151 1.2.12.2 yamt
152 1.2.12.2 yamt return (error);
153 1.2.12.2 yamt }
154 1.2.12.2 yamt
155 1.2.12.2 yamt int
156 1.2.12.4 yamt scope6_get(const struct ifnet *ifp, struct scope6_id *idlist)
157 1.2.12.2 yamt {
158 1.2.12.2 yamt /* We only need to lock the interface's afdata for SID() to work. */
159 1.2.12.4 yamt const struct scope6_id *sid = SID(ifp);
160 1.2.12.2 yamt
161 1.2.12.2 yamt if (sid == NULL) /* paranoid? */
162 1.2.12.4 yamt return EINVAL;
163 1.2.12.2 yamt
164 1.2.12.2 yamt *idlist = *sid;
165 1.2.12.2 yamt
166 1.2.12.4 yamt return 0;
167 1.2.12.2 yamt }
168 1.2.12.2 yamt
169 1.2.12.2 yamt /*
170 1.2.12.2 yamt * Get a scope of the address. Interface-local, link-local, site-local
171 1.2.12.2 yamt * or global.
172 1.2.12.2 yamt */
173 1.2.12.2 yamt int
174 1.2.12.4 yamt in6_addrscope(const struct in6_addr *addr)
175 1.2.12.2 yamt {
176 1.2.12.2 yamt int scope;
177 1.2.12.2 yamt
178 1.2.12.2 yamt if (addr->s6_addr[0] == 0xfe) {
179 1.2.12.2 yamt scope = addr->s6_addr[1] & 0xc0;
180 1.2.12.2 yamt
181 1.2.12.2 yamt switch (scope) {
182 1.2.12.2 yamt case 0x80:
183 1.2.12.2 yamt return IPV6_ADDR_SCOPE_LINKLOCAL;
184 1.2.12.2 yamt break;
185 1.2.12.2 yamt case 0xc0:
186 1.2.12.2 yamt return IPV6_ADDR_SCOPE_SITELOCAL;
187 1.2.12.2 yamt break;
188 1.2.12.2 yamt default:
189 1.2.12.2 yamt return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */
190 1.2.12.2 yamt break;
191 1.2.12.2 yamt }
192 1.2.12.2 yamt }
193 1.2.12.2 yamt
194 1.2.12.2 yamt
195 1.2.12.2 yamt if (addr->s6_addr[0] == 0xff) {
196 1.2.12.2 yamt scope = addr->s6_addr[1] & 0x0f;
197 1.2.12.2 yamt
198 1.2.12.2 yamt /*
199 1.2.12.2 yamt * due to other scope such as reserved,
200 1.2.12.2 yamt * return scope doesn't work.
201 1.2.12.2 yamt */
202 1.2.12.2 yamt switch (scope) {
203 1.2.12.2 yamt case IPV6_ADDR_SCOPE_INTFACELOCAL:
204 1.2.12.2 yamt return IPV6_ADDR_SCOPE_INTFACELOCAL;
205 1.2.12.2 yamt break;
206 1.2.12.2 yamt case IPV6_ADDR_SCOPE_LINKLOCAL:
207 1.2.12.2 yamt return IPV6_ADDR_SCOPE_LINKLOCAL;
208 1.2.12.2 yamt break;
209 1.2.12.2 yamt case IPV6_ADDR_SCOPE_SITELOCAL:
210 1.2.12.2 yamt return IPV6_ADDR_SCOPE_SITELOCAL;
211 1.2.12.2 yamt break;
212 1.2.12.2 yamt default:
213 1.2.12.2 yamt return IPV6_ADDR_SCOPE_GLOBAL;
214 1.2.12.2 yamt break;
215 1.2.12.2 yamt }
216 1.2.12.2 yamt }
217 1.2.12.2 yamt
218 1.2.12.4 yamt if (memcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) {
219 1.2.12.2 yamt if (addr->s6_addr[15] == 1) /* loopback */
220 1.2.12.2 yamt return IPV6_ADDR_SCOPE_LINKLOCAL;
221 1.2.12.2 yamt if (addr->s6_addr[15] == 0) {
222 1.2.12.2 yamt /*
223 1.2.12.2 yamt * Regard the unspecified addresses as global,
224 1.2.12.2 yamt * since it has no ambiguity.
225 1.2.12.2 yamt * XXX: not sure if it's correct...
226 1.2.12.2 yamt */
227 1.2.12.2 yamt return IPV6_ADDR_SCOPE_GLOBAL;
228 1.2.12.2 yamt }
229 1.2.12.2 yamt }
230 1.2.12.2 yamt
231 1.2.12.2 yamt return IPV6_ADDR_SCOPE_GLOBAL;
232 1.2.12.2 yamt }
233 1.2.12.2 yamt
234 1.2.12.2 yamt /* note that ifp argument might be NULL */
235 1.2.12.2 yamt void
236 1.2.12.2 yamt scope6_setdefault(struct ifnet *ifp)
237 1.2.12.2 yamt {
238 1.2.12.2 yamt
239 1.2.12.2 yamt /*
240 1.2.12.2 yamt * Currently, this function just sets the default "interfaces"
241 1.2.12.2 yamt * and "links" according to the given interface.
242 1.2.12.2 yamt * We might eventually have to separate the notion of "link" from
243 1.2.12.2 yamt * "interface" and provide a user interface to set the default.
244 1.2.12.2 yamt */
245 1.2.12.2 yamt if (ifp) {
246 1.2.12.2 yamt sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] =
247 1.2.12.2 yamt ifp->if_index;
248 1.2.12.2 yamt sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] =
249 1.2.12.2 yamt ifp->if_index;
250 1.2.12.2 yamt } else {
251 1.2.12.2 yamt sid_default.s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = 0;
252 1.2.12.2 yamt sid_default.s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = 0;
253 1.2.12.2 yamt }
254 1.2.12.2 yamt }
255 1.2.12.2 yamt
256 1.2.12.2 yamt int
257 1.2.12.2 yamt scope6_get_default(struct scope6_id *idlist)
258 1.2.12.2 yamt {
259 1.2.12.2 yamt
260 1.2.12.2 yamt *idlist = sid_default;
261 1.2.12.2 yamt
262 1.2.12.2 yamt return (0);
263 1.2.12.2 yamt }
264 1.2.12.2 yamt
265 1.2.12.2 yamt uint32_t
266 1.2.12.4 yamt scope6_addr2default(const struct in6_addr *addr)
267 1.2.12.2 yamt {
268 1.2.12.2 yamt uint32_t id;
269 1.2.12.2 yamt
270 1.2.12.2 yamt /*
271 1.2.12.2 yamt * special case: The loopback address should be considered as
272 1.2.12.2 yamt * link-local, but there's no ambiguity in the syntax.
273 1.2.12.2 yamt */
274 1.2.12.2 yamt if (IN6_IS_ADDR_LOOPBACK(addr))
275 1.2.12.2 yamt return (0);
276 1.2.12.2 yamt
277 1.2.12.2 yamt /*
278 1.2.12.2 yamt * XXX: 32-bit read is atomic on all our platforms, is it OK
279 1.2.12.2 yamt * not to lock here?
280 1.2.12.2 yamt */
281 1.2.12.2 yamt id = sid_default.s6id_list[in6_addrscope(addr)];
282 1.2.12.2 yamt
283 1.2.12.2 yamt return (id);
284 1.2.12.2 yamt }
285 1.2.12.2 yamt
286 1.2.12.2 yamt /*
287 1.2.12.2 yamt * Validate the specified scope zone ID in the sin6_scope_id field. If the ID
288 1.2.12.2 yamt * is unspecified (=0), needs to be specified, and the default zone ID can be
289 1.2.12.2 yamt * used, the default value will be used.
290 1.2.12.2 yamt * This routine then generates the kernel-internal form: if the address scope
291 1.2.12.2 yamt * of is interface-local or link-local, embed the interface index in the
292 1.2.12.2 yamt * address.
293 1.2.12.2 yamt */
294 1.2.12.2 yamt int
295 1.2.12.2 yamt sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok)
296 1.2.12.2 yamt {
297 1.2.12.2 yamt struct ifnet *ifp;
298 1.2.12.2 yamt uint32_t zoneid;
299 1.2.12.2 yamt
300 1.2.12.2 yamt if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok)
301 1.2.12.2 yamt zoneid = scope6_addr2default(&sin6->sin6_addr);
302 1.2.12.2 yamt
303 1.2.12.2 yamt if (zoneid != 0 &&
304 1.2.12.2 yamt (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
305 1.2.12.2 yamt IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) {
306 1.2.12.2 yamt /*
307 1.2.12.2 yamt * At this moment, we only check interface-local and
308 1.2.12.2 yamt * link-local scope IDs, and use interface indices as the
309 1.2.12.2 yamt * zone IDs assuming a one-to-one mapping between interfaces
310 1.2.12.2 yamt * and links.
311 1.2.12.2 yamt */
312 1.2.12.2 yamt if (if_indexlim <= zoneid)
313 1.2.12.2 yamt return (ENXIO);
314 1.2.12.2 yamt #ifdef __FreeBSD__
315 1.2.12.2 yamt ifp = ifnet_byindex(zoneid);
316 1.2.12.2 yamt #else
317 1.2.12.2 yamt ifp = ifindex2ifnet[zoneid];
318 1.2.12.2 yamt #endif
319 1.2.12.2 yamt if (ifp == NULL) /* XXX: this can happen for some OS */
320 1.2.12.2 yamt return (ENXIO);
321 1.2.12.2 yamt
322 1.2.12.2 yamt /* XXX assignment to 16bit from 32bit variable */
323 1.2.12.2 yamt sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff);
324 1.2.12.2 yamt
325 1.2.12.2 yamt sin6->sin6_scope_id = 0;
326 1.2.12.2 yamt }
327 1.2.12.2 yamt
328 1.2.12.2 yamt return 0;
329 1.2.12.2 yamt }
330 1.2.12.2 yamt
331 1.2.12.2 yamt /*
332 1.2.12.2 yamt * generate standard sockaddr_in6 from embedded form.
333 1.2.12.2 yamt */
334 1.2.12.2 yamt int
335 1.2.12.2 yamt sa6_recoverscope(struct sockaddr_in6 *sin6)
336 1.2.12.2 yamt {
337 1.2.12.2 yamt uint32_t zoneid;
338 1.2.12.2 yamt
339 1.2.12.2 yamt if (sin6->sin6_scope_id != 0) {
340 1.2.12.2 yamt log(LOG_NOTICE,
341 1.2.12.2 yamt "sa6_recoverscope: assumption failure (non 0 ID): %s%%%d\n",
342 1.2.12.2 yamt ip6_sprintf(&sin6->sin6_addr), sin6->sin6_scope_id);
343 1.2.12.2 yamt /* XXX: proceed anyway... */
344 1.2.12.2 yamt }
345 1.2.12.2 yamt if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
346 1.2.12.2 yamt IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) {
347 1.2.12.2 yamt /*
348 1.2.12.2 yamt * KAME assumption: link id == interface id
349 1.2.12.2 yamt */
350 1.2.12.2 yamt zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
351 1.2.12.2 yamt if (zoneid) {
352 1.2.12.2 yamt /* sanity check */
353 1.2.12.3 yamt if (/* zoneid < 0 || */ if_indexlim <= zoneid)
354 1.2.12.2 yamt return (ENXIO);
355 1.2.12.2 yamt #ifdef __FreeBSD__
356 1.2.12.2 yamt if (!ifnet_byindex(zoneid))
357 1.2.12.2 yamt #else
358 1.2.12.2 yamt if (!ifindex2ifnet[zoneid])
359 1.2.12.2 yamt #endif
360 1.2.12.2 yamt return (ENXIO);
361 1.2.12.2 yamt sin6->sin6_addr.s6_addr16[1] = 0;
362 1.2.12.2 yamt sin6->sin6_scope_id = zoneid;
363 1.2.12.2 yamt }
364 1.2.12.2 yamt }
365 1.2.12.2 yamt
366 1.2.12.2 yamt return 0;
367 1.2.12.2 yamt }
368 1.2.12.2 yamt
369 1.2.12.4 yamt int
370 1.2.12.4 yamt in6_setzoneid(struct in6_addr *in6, uint32_t zoneid)
371 1.2.12.4 yamt {
372 1.2.12.4 yamt if (IN6_IS_SCOPE_EMBEDDABLE(in6))
373 1.2.12.4 yamt in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */
374 1.2.12.4 yamt
375 1.2.12.4 yamt return 0;
376 1.2.12.4 yamt }
377 1.2.12.4 yamt
378 1.2.12.2 yamt /*
379 1.2.12.2 yamt * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is
380 1.2.12.2 yamt * non NULL, it is set to the zone ID. If the zone ID needs to be embedded
381 1.2.12.2 yamt * in the in6_addr structure, in6 will be modified.
382 1.2.12.2 yamt */
383 1.2.12.2 yamt int
384 1.2.12.4 yamt in6_setscope(struct in6_addr *in6, const struct ifnet *ifp, uint32_t *ret_id)
385 1.2.12.2 yamt {
386 1.2.12.2 yamt int scope;
387 1.2.12.2 yamt uint32_t zoneid = 0;
388 1.2.12.4 yamt const struct scope6_id *sid = SID(ifp);
389 1.2.12.2 yamt
390 1.2.12.2 yamt #ifdef DIAGNOSTIC
391 1.2.12.2 yamt if (sid == NULL) { /* should not happen */
392 1.2.12.2 yamt panic("in6_setscope: scope array is NULL");
393 1.2.12.2 yamt /* NOTREACHED */
394 1.2.12.2 yamt }
395 1.2.12.2 yamt #endif
396 1.2.12.2 yamt
397 1.2.12.2 yamt /*
398 1.2.12.2 yamt * special case: the loopback address can only belong to a loopback
399 1.2.12.2 yamt * interface.
400 1.2.12.2 yamt */
401 1.2.12.2 yamt if (IN6_IS_ADDR_LOOPBACK(in6)) {
402 1.2.12.2 yamt if (!(ifp->if_flags & IFF_LOOPBACK))
403 1.2.12.2 yamt return (EINVAL);
404 1.2.12.2 yamt else {
405 1.2.12.2 yamt if (ret_id != NULL)
406 1.2.12.2 yamt *ret_id = 0; /* there's no ambiguity */
407 1.2.12.2 yamt return (0);
408 1.2.12.2 yamt }
409 1.2.12.2 yamt }
410 1.2.12.2 yamt
411 1.2.12.2 yamt scope = in6_addrscope(in6);
412 1.2.12.2 yamt
413 1.2.12.2 yamt if (!sid->s6id_list)
414 1.2.12.2 yamt return 0;
415 1.2.12.2 yamt
416 1.2.12.2 yamt switch (scope) {
417 1.2.12.2 yamt case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */
418 1.2.12.2 yamt zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL];
419 1.2.12.2 yamt break;
420 1.2.12.2 yamt
421 1.2.12.2 yamt case IPV6_ADDR_SCOPE_LINKLOCAL:
422 1.2.12.2 yamt zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL];
423 1.2.12.2 yamt break;
424 1.2.12.2 yamt
425 1.2.12.2 yamt case IPV6_ADDR_SCOPE_SITELOCAL:
426 1.2.12.2 yamt zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL];
427 1.2.12.2 yamt break;
428 1.2.12.2 yamt
429 1.2.12.2 yamt case IPV6_ADDR_SCOPE_ORGLOCAL:
430 1.2.12.2 yamt zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL];
431 1.2.12.2 yamt break;
432 1.2.12.2 yamt
433 1.2.12.2 yamt default:
434 1.2.12.2 yamt zoneid = 0; /* XXX: treat as global. */
435 1.2.12.2 yamt break;
436 1.2.12.2 yamt }
437 1.2.12.2 yamt
438 1.2.12.2 yamt if (ret_id != NULL)
439 1.2.12.2 yamt *ret_id = zoneid;
440 1.2.12.2 yamt
441 1.2.12.4 yamt return in6_setzoneid(in6, zoneid);
442 1.2.12.2 yamt }
443 1.2.12.2 yamt
444 1.2.12.2 yamt /*
445 1.2.12.2 yamt * Just clear the embedded scope identifier. Return 0 if the original address
446 1.2.12.2 yamt * is intact; return non 0 if the address is modified.
447 1.2.12.2 yamt */
448 1.2.12.2 yamt int
449 1.2.12.2 yamt in6_clearscope(struct in6_addr *in6)
450 1.2.12.2 yamt {
451 1.2.12.2 yamt int modified = 0;
452 1.2.12.2 yamt
453 1.2.12.2 yamt if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
454 1.2.12.2 yamt if (in6->s6_addr16[1] != 0)
455 1.2.12.2 yamt modified = 1;
456 1.2.12.2 yamt in6->s6_addr16[1] = 0;
457 1.2.12.2 yamt }
458 1.2.12.2 yamt
459 1.2.12.2 yamt return (modified);
460 1.2.12.2 yamt }
461