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