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