1 /* $NetBSD: scope6.c,v 1.23 2020/06/16 17:12:18 maxv 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.23 2020/06/16 17:12:18 maxv 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 46 #include <netinet/in.h> 47 48 #include <netinet6/in6_var.h> 49 #include <netinet6/scope6_var.h> 50 51 #ifdef ENABLE_DEFAULT_SCOPE 52 int ip6_use_defzone = 1; 53 #else 54 int ip6_use_defzone = 0; 55 #endif 56 57 static struct scope6_id sid_default; 58 #define SID(ifp) \ 59 ((ifp)->if_afdata[AF_INET6] == NULL ? NULL : \ 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 = malloc(sizeof(*sid), M_IFADDR, M_WAITOK | M_ZERO); 75 76 /* 77 * XXX: IPV6_ADDR_SCOPE_xxx macros are not standard. 78 * Should we rather hardcode here? 79 */ 80 sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL] = ifp->if_index; 81 sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL] = ifp->if_index; 82 #ifdef MULTI_SCOPE 83 /* by default, we don't care about scope boundary for these scopes. */ 84 sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL] = 1; 85 sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL] = 1; 86 #endif 87 88 return sid; 89 } 90 91 void 92 scope6_ifdetach(struct scope6_id *sid) 93 { 94 95 free(sid, M_IFADDR); 96 } 97 98 /* 99 * Get a scope of the address. Interface-local, link-local, site-local 100 * or global. 101 */ 102 int 103 in6_addrscope(const struct in6_addr *addr) 104 { 105 int scope; 106 107 if (addr->s6_addr[0] == 0xfe) { 108 scope = addr->s6_addr[1] & 0xc0; 109 110 switch (scope) { 111 case 0x80: 112 return IPV6_ADDR_SCOPE_LINKLOCAL; 113 case 0xc0: 114 return IPV6_ADDR_SCOPE_SITELOCAL; 115 default: 116 return IPV6_ADDR_SCOPE_GLOBAL; /* just in case */ 117 } 118 } 119 120 if (addr->s6_addr[0] == 0xff) { 121 scope = addr->s6_addr[1] & 0x0f; 122 123 /* 124 * due to other scope such as reserved, 125 * return scope doesn't work. 126 */ 127 switch (scope) { 128 case IPV6_ADDR_SCOPE_INTFACELOCAL: 129 return IPV6_ADDR_SCOPE_INTFACELOCAL; 130 case IPV6_ADDR_SCOPE_LINKLOCAL: 131 return IPV6_ADDR_SCOPE_LINKLOCAL; 132 case IPV6_ADDR_SCOPE_SITELOCAL: 133 return IPV6_ADDR_SCOPE_SITELOCAL; 134 default: 135 return IPV6_ADDR_SCOPE_GLOBAL; 136 } 137 } 138 139 if (memcmp(&in6addr_loopback, addr, sizeof(*addr) - 1) == 0) { 140 if (addr->s6_addr[15] == 1) /* loopback */ 141 return IPV6_ADDR_SCOPE_LINKLOCAL; 142 if (addr->s6_addr[15] == 0) { 143 /* 144 * Regard the unspecified addresses as global, 145 * since it has no ambiguity. 146 * XXX: not sure if it's correct... 147 */ 148 return IPV6_ADDR_SCOPE_GLOBAL; 149 } 150 } 151 152 return IPV6_ADDR_SCOPE_GLOBAL; 153 } 154 155 uint32_t 156 scope6_addr2default(const struct in6_addr *addr) 157 { 158 uint32_t id; 159 160 /* 161 * special case: The loopback address should be considered as 162 * link-local, but there's no ambiguity in the syntax. 163 */ 164 if (IN6_IS_ADDR_LOOPBACK(addr)) 165 return 0; 166 167 /* 168 * XXX: 32-bit read is atomic on all our platforms, is it OK 169 * not to lock here? 170 */ 171 id = sid_default.s6id_list[in6_addrscope(addr)]; 172 173 return id; 174 } 175 176 /* 177 * Validate the specified scope zone ID in the sin6_scope_id field. If the ID 178 * is unspecified (=0), needs to be specified, and the default zone ID can be 179 * used, the default value will be used. 180 * This routine then generates the kernel-internal form: if the address scope 181 * of is interface-local or link-local, embed the interface index in the 182 * address. 183 */ 184 int 185 sa6_embedscope(struct sockaddr_in6 *sin6, int defaultok) 186 { 187 struct ifnet *ifp; 188 uint32_t zoneid; 189 190 if ((zoneid = sin6->sin6_scope_id) == 0 && defaultok) 191 zoneid = scope6_addr2default(&sin6->sin6_addr); 192 193 if (zoneid != 0 && 194 (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) || 195 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr))) { 196 int s; 197 /* 198 * At this moment, we only check interface-local and 199 * link-local scope IDs, and use interface indices as the 200 * zone IDs assuming a one-to-one mapping between interfaces 201 * and links. 202 */ 203 s = pserialize_read_enter(); 204 ifp = if_byindex(zoneid); 205 if (ifp == NULL) { 206 pserialize_read_exit(s); 207 return ENXIO; 208 } 209 pserialize_read_exit(s); 210 211 /* XXX assignment to 16bit from 32bit variable */ 212 sin6->sin6_addr.s6_addr16[1] = htons(zoneid & 0xffff); 213 214 sin6->sin6_scope_id = 0; 215 } 216 217 return 0; 218 } 219 220 struct sockaddr * 221 sockaddr_in6_externalize(struct sockaddr *dst, socklen_t socklen, 222 const struct sockaddr *src) 223 { 224 struct sockaddr_in6 *sin6; 225 226 sin6 = satosin6(sockaddr_copy(dst, socklen, src)); 227 228 if (sin6 == NULL || sa6_recoverscope(sin6) != 0) 229 return NULL; 230 231 return dst; 232 } 233 234 /* 235 * generate standard sockaddr_in6 from embedded form. 236 */ 237 int 238 sa6_recoverscope(struct sockaddr_in6 *sin6) 239 { 240 uint32_t zoneid; 241 char ip6buf[INET6_ADDRSTRLEN]; 242 243 if (sin6->sin6_scope_id != 0) { 244 log(LOG_NOTICE, 245 "%s: assumption failure (non 0 ID): %s%%%d\n", __func__, 246 IN6_PRINT(ip6buf, &sin6->sin6_addr), sin6->sin6_scope_id); 247 /* XXX: proceed anyway... */ 248 } 249 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) || 250 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr)) { 251 /* 252 * KAME assumption: link id == interface id 253 */ 254 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]); 255 if (zoneid) { 256 int s = pserialize_read_enter(); 257 if (!if_byindex(zoneid)) { 258 pserialize_read_exit(s); 259 return ENXIO; 260 } 261 pserialize_read_exit(s); 262 sin6->sin6_addr.s6_addr16[1] = 0; 263 sin6->sin6_scope_id = zoneid; 264 } 265 } 266 267 return 0; 268 } 269 270 int 271 in6_setzoneid(struct in6_addr *in6, uint32_t zoneid) 272 { 273 if (IN6_IS_SCOPE_EMBEDDABLE(in6)) 274 in6->s6_addr16[1] = htons(zoneid & 0xffff); /* XXX */ 275 276 return 0; 277 } 278 279 /* 280 * Determine the appropriate scope zone ID for in6 and ifp. If ret_id is 281 * non NULL, it is set to the zone ID. If the zone ID needs to be embedded 282 * in the in6_addr structure, in6 will be modified. 283 */ 284 int 285 in6_setscope(struct in6_addr *in6, const struct ifnet *ifp, uint32_t *ret_id) 286 { 287 int scope; 288 uint32_t zoneid = 0; 289 const struct scope6_id *sid = SID(ifp); 290 291 if (sid == NULL) { 292 log(LOG_NOTICE, "%s: no scope id for %s\n", __func__, 293 if_name(ifp)); 294 return EINVAL; 295 } 296 297 /* 298 * special case: the loopback address can only belong to a loopback 299 * interface. 300 */ 301 if (IN6_IS_ADDR_LOOPBACK(in6)) { 302 if (!(ifp->if_flags & IFF_LOOPBACK)) { 303 char ip6buf[INET6_ADDRSTRLEN]; 304 log(LOG_NOTICE, "%s: can't set scope for not loopback " 305 "interface %s and loopback address %s\n", 306 __func__, if_name(ifp), IN6_PRINT(ip6buf, in6)); 307 return EINVAL; 308 } else { 309 if (ret_id != NULL) 310 *ret_id = 0; /* there's no ambiguity */ 311 return 0; 312 } 313 } 314 315 scope = in6_addrscope(in6); 316 317 switch (scope) { 318 case IPV6_ADDR_SCOPE_INTFACELOCAL: /* should be interface index */ 319 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_INTFACELOCAL]; 320 break; 321 322 case IPV6_ADDR_SCOPE_LINKLOCAL: 323 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_LINKLOCAL]; 324 break; 325 326 case IPV6_ADDR_SCOPE_SITELOCAL: 327 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_SITELOCAL]; 328 break; 329 330 case IPV6_ADDR_SCOPE_ORGLOCAL: 331 zoneid = sid->s6id_list[IPV6_ADDR_SCOPE_ORGLOCAL]; 332 break; 333 334 default: 335 zoneid = 0; /* XXX: treat as global. */ 336 break; 337 } 338 339 if (ret_id != NULL) 340 *ret_id = zoneid; 341 342 return in6_setzoneid(in6, zoneid); 343 } 344 345 const char * 346 in6_getscopename(const struct in6_addr *addr) 347 { 348 switch (in6_addrscope(addr)) { 349 case IPV6_ADDR_SCOPE_INTFACELOCAL: 350 return "interface"; 351 #if IPV6_ADDR_SCOPE_INTFACELOCAL != IPV6_ADDR_SCOPE_NODELOCAL 352 case IPV6_ADDR_SCOPE_NODELOCAL: 353 return "node"; 354 #endif 355 case IPV6_ADDR_SCOPE_LINKLOCAL: 356 return "link"; 357 case IPV6_ADDR_SCOPE_SITELOCAL: 358 return "site"; 359 case IPV6_ADDR_SCOPE_ORGLOCAL: 360 return "organization"; 361 case IPV6_ADDR_SCOPE_GLOBAL: 362 return "global"; 363 default: 364 return "unknown"; 365 } 366 } 367 368 /* 369 * Just clear the embedded scope identifier. Return 0 if the original address 370 * is intact; return non 0 if the address is modified. 371 */ 372 int 373 in6_clearscope(struct in6_addr *in6) 374 { 375 int modified = 0; 376 377 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) { 378 if (in6->s6_addr16[1] != 0) 379 modified = 1; 380 in6->s6_addr16[1] = 0; 381 } 382 383 return modified; 384 } 385