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