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