1 /* $OpenBSD: addr.c,v 1.6 2022/10/28 02:29:34 djm Exp $ */ 2 3 /* 4 * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <netinet/in.h> 22 #include <arpa/inet.h> 23 24 #include <netdb.h> 25 #include <string.h> 26 #include <stdlib.h> 27 #include <stdio.h> 28 29 #include "addr.h" 30 31 #define _SA(x) ((struct sockaddr *)(x)) 32 33 int 34 addr_unicast_masklen(int af) 35 { 36 switch (af) { 37 case AF_INET: 38 return 32; 39 case AF_INET6: 40 return 128; 41 default: 42 return -1; 43 } 44 } 45 46 static inline int 47 masklen_valid(int af, u_int masklen) 48 { 49 switch (af) { 50 case AF_INET: 51 return masklen <= 32 ? 0 : -1; 52 case AF_INET6: 53 return masklen <= 128 ? 0 : -1; 54 default: 55 return -1; 56 } 57 } 58 59 int 60 addr_xaddr_to_sa(const struct xaddr *xa, struct sockaddr *sa, socklen_t *len, 61 u_int16_t port) 62 { 63 struct sockaddr_in *in4 = (struct sockaddr_in *)sa; 64 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa; 65 66 if (xa == NULL || sa == NULL || len == NULL) 67 return -1; 68 69 switch (xa->af) { 70 case AF_INET: 71 if (*len < sizeof(*in4)) 72 return -1; 73 memset(sa, '\0', sizeof(*in4)); 74 *len = sizeof(*in4); 75 #ifdef SOCK_HAS_LEN 76 in4->sin_len = sizeof(*in4); 77 #endif 78 in4->sin_family = AF_INET; 79 in4->sin_port = htons(port); 80 memcpy(&in4->sin_addr, &xa->v4, sizeof(in4->sin_addr)); 81 break; 82 case AF_INET6: 83 if (*len < sizeof(*in6)) 84 return -1; 85 memset(sa, '\0', sizeof(*in6)); 86 *len = sizeof(*in6); 87 #ifdef SOCK_HAS_LEN 88 in6->sin6_len = sizeof(*in6); 89 #endif 90 in6->sin6_family = AF_INET6; 91 in6->sin6_port = htons(port); 92 memcpy(&in6->sin6_addr, &xa->v6, sizeof(in6->sin6_addr)); 93 in6->sin6_scope_id = xa->scope_id; 94 break; 95 default: 96 return -1; 97 } 98 return 0; 99 } 100 101 /* 102 * Convert struct sockaddr to struct xaddr 103 * Returns 0 on success, -1 on failure. 104 */ 105 int 106 addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa) 107 { 108 struct sockaddr_in *in4 = (struct sockaddr_in *)sa; 109 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa; 110 111 memset(xa, '\0', sizeof(*xa)); 112 113 switch (sa->sa_family) { 114 case AF_INET: 115 if (slen < (socklen_t)sizeof(*in4)) 116 return -1; 117 xa->af = AF_INET; 118 memcpy(&xa->v4, &in4->sin_addr, sizeof(xa->v4)); 119 break; 120 case AF_INET6: 121 if (slen < (socklen_t)sizeof(*in6)) 122 return -1; 123 xa->af = AF_INET6; 124 memcpy(&xa->v6, &in6->sin6_addr, sizeof(xa->v6)); 125 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID 126 xa->scope_id = in6->sin6_scope_id; 127 #endif 128 break; 129 default: 130 return -1; 131 } 132 133 return 0; 134 } 135 136 int 137 addr_invert(struct xaddr *n) 138 { 139 int i; 140 141 if (n == NULL) 142 return -1; 143 144 switch (n->af) { 145 case AF_INET: 146 n->v4.s_addr = ~n->v4.s_addr; 147 return 0; 148 case AF_INET6: 149 for (i = 0; i < 4; i++) 150 n->addr32[i] = ~n->addr32[i]; 151 return 0; 152 default: 153 return -1; 154 } 155 } 156 157 /* 158 * Calculate a netmask of length 'l' for address family 'af' and 159 * store it in 'n'. 160 * Returns 0 on success, -1 on failure. 161 */ 162 int 163 addr_netmask(int af, u_int l, struct xaddr *n) 164 { 165 int i; 166 167 if (masklen_valid(af, l) != 0 || n == NULL) 168 return -1; 169 170 memset(n, '\0', sizeof(*n)); 171 switch (af) { 172 case AF_INET: 173 n->af = AF_INET; 174 if (l == 0) 175 return 0; 176 n->v4.s_addr = htonl((0xffffffff << (32 - l)) & 0xffffffff); 177 return 0; 178 case AF_INET6: 179 n->af = AF_INET6; 180 for (i = 0; i < 4 && l >= 32; i++, l -= 32) 181 n->addr32[i] = 0xffffffffU; 182 if (i < 4 && l != 0) 183 n->addr32[i] = htonl((0xffffffff << (32 - l)) & 184 0xffffffff); 185 return 0; 186 default: 187 return -1; 188 } 189 } 190 191 int 192 addr_hostmask(int af, u_int l, struct xaddr *n) 193 { 194 if (addr_netmask(af, l, n) == -1 || addr_invert(n) == -1) 195 return -1; 196 return 0; 197 } 198 199 /* 200 * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'. 201 * Returns 0 on success, -1 on failure. 202 */ 203 int 204 addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b) 205 { 206 int i; 207 208 if (dst == NULL || a == NULL || b == NULL || a->af != b->af) 209 return -1; 210 211 memcpy(dst, a, sizeof(*dst)); 212 switch (a->af) { 213 case AF_INET: 214 dst->v4.s_addr &= b->v4.s_addr; 215 return 0; 216 case AF_INET6: 217 dst->scope_id = a->scope_id; 218 for (i = 0; i < 4; i++) 219 dst->addr32[i] &= b->addr32[i]; 220 return 0; 221 default: 222 return -1; 223 } 224 } 225 226 int 227 addr_or(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b) 228 { 229 int i; 230 231 if (dst == NULL || a == NULL || b == NULL || a->af != b->af) 232 return (-1); 233 234 memcpy(dst, a, sizeof(*dst)); 235 switch (a->af) { 236 case AF_INET: 237 dst->v4.s_addr |= b->v4.s_addr; 238 return (0); 239 case AF_INET6: 240 for (i = 0; i < 4; i++) 241 dst->addr32[i] |= b->addr32[i]; 242 return (0); 243 default: 244 return (-1); 245 } 246 } 247 248 int 249 addr_cmp(const struct xaddr *a, const struct xaddr *b) 250 { 251 int i; 252 253 if (a->af != b->af) 254 return (a->af == AF_INET6 ? 1 : -1); 255 256 switch (a->af) { 257 case AF_INET: 258 /* 259 * Can't just subtract here as 255.255.255.255 - 0.0.0.0 is 260 * too big to fit into a signed int 261 */ 262 if (a->v4.s_addr == b->v4.s_addr) 263 return 0; 264 return (ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr) ? 1 : -1); 265 case AF_INET6: 266 /* 267 * Do this a byte at a time to avoid the above issue and 268 * any endian problems 269 */ 270 for (i = 0; i < 16; i++) 271 if (a->addr8[i] - b->addr8[i] != 0) 272 return (a->addr8[i] - b->addr8[i]); 273 if (a->scope_id == b->scope_id) 274 return (0); 275 return (a->scope_id > b->scope_id ? 1 : -1); 276 default: 277 return (-1); 278 } 279 } 280 281 int 282 addr_is_all0s(const struct xaddr *a) 283 { 284 int i; 285 286 switch (a->af) { 287 case AF_INET: 288 return (a->v4.s_addr == 0 ? 0 : -1); 289 case AF_INET6: 290 for (i = 0; i < 4; i++) 291 if (a->addr32[i] != 0) 292 return -1; 293 return 0; 294 default: 295 return -1; 296 } 297 } 298 299 /* Increment the specified address. Note, does not do overflow checking */ 300 void 301 addr_increment(struct xaddr *a) 302 { 303 int i; 304 uint32_t n; 305 306 switch (a->af) { 307 case AF_INET: 308 a->v4.s_addr = htonl(ntohl(a->v4.s_addr) + 1); 309 break; 310 case AF_INET6: 311 for (i = 0; i < 4; i++) { 312 /* Increment with carry */ 313 n = ntohl(a->addr32[3 - i]) + 1; 314 a->addr32[3 - i] = htonl(n); 315 if (n != 0) 316 break; 317 } 318 break; 319 } 320 } 321 322 /* 323 * Test whether host portion of address 'a', as determined by 'masklen' 324 * is all zeros. 325 * Returns 0 if host portion of address is all-zeros, 326 * -1 if not all zeros or on failure. 327 */ 328 int 329 addr_host_is_all0s(const struct xaddr *a, u_int masklen) 330 { 331 struct xaddr tmp_addr, tmp_mask, tmp_result; 332 333 memcpy(&tmp_addr, a, sizeof(tmp_addr)); 334 if (addr_hostmask(a->af, masklen, &tmp_mask) == -1) 335 return -1; 336 if (addr_and(&tmp_result, &tmp_addr, &tmp_mask) == -1) 337 return -1; 338 return addr_is_all0s(&tmp_result); 339 } 340 341 #if 0 342 int 343 addr_host_to_all0s(struct xaddr *a, u_int masklen) 344 { 345 struct xaddr tmp_mask; 346 347 if (addr_netmask(a->af, masklen, &tmp_mask) == -1) 348 return (-1); 349 if (addr_and(a, a, &tmp_mask) == -1) 350 return (-1); 351 return (0); 352 } 353 #endif 354 355 int 356 addr_host_to_all1s(struct xaddr *a, u_int masklen) 357 { 358 struct xaddr tmp_mask; 359 360 if (addr_hostmask(a->af, masklen, &tmp_mask) == -1) 361 return (-1); 362 if (addr_or(a, a, &tmp_mask) == -1) 363 return (-1); 364 return (0); 365 } 366 367 /* 368 * Parse string address 'p' into 'n'. 369 * Returns 0 on success, -1 on failure. 370 */ 371 int 372 addr_pton(const char *p, struct xaddr *n) 373 { 374 struct addrinfo hints, *ai; 375 376 memset(&hints, '\0', sizeof(hints)); 377 hints.ai_flags = AI_NUMERICHOST; 378 379 if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0) 380 return -1; 381 382 if (ai == NULL) 383 return -1; 384 385 if (ai->ai_addr == NULL) { 386 freeaddrinfo(ai); 387 return -1; 388 } 389 390 if (n != NULL && addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen, 391 n) == -1) { 392 freeaddrinfo(ai); 393 return -1; 394 } 395 396 freeaddrinfo(ai); 397 return 0; 398 } 399 400 int 401 addr_sa_pton(const char *h, const char *s, struct sockaddr *sa, socklen_t slen) 402 { 403 struct addrinfo hints, *ai; 404 405 memset(&hints, '\0', sizeof(hints)); 406 hints.ai_flags = AI_NUMERICHOST; 407 408 if (h == NULL || getaddrinfo(h, s, &hints, &ai) != 0) 409 return -1; 410 411 if (ai == NULL) 412 return -1; 413 414 if (ai->ai_addr == NULL) { 415 freeaddrinfo(ai); 416 return -1; 417 } 418 419 if (sa != NULL) { 420 if (slen < ai->ai_addrlen) { 421 freeaddrinfo(ai); 422 return -1; 423 } 424 memcpy(sa, &ai->ai_addr, ai->ai_addrlen); 425 } 426 427 freeaddrinfo(ai); 428 return 0; 429 } 430 431 int 432 addr_ntop(const struct xaddr *n, char *p, size_t len) 433 { 434 struct sockaddr_storage ss; 435 socklen_t slen = sizeof(ss); 436 437 if (addr_xaddr_to_sa(n, _SA(&ss), &slen, 0) == -1) 438 return -1; 439 if (p == NULL || len == 0) 440 return -1; 441 if (getnameinfo(_SA(&ss), slen, p, len, NULL, 0, 442 NI_NUMERICHOST) == -1) 443 return -1; 444 445 return 0; 446 } 447 448 /* 449 * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z). 450 * Return -1 on parse error, -2 on inconsistency or 0 on success. 451 */ 452 int 453 addr_pton_cidr(const char *p, struct xaddr *n, u_int *l) 454 { 455 struct xaddr tmp; 456 long unsigned int masklen = 999; 457 char addrbuf[64], *mp, *cp; 458 459 /* Don't modify argument */ 460 if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) >= sizeof(addrbuf)) 461 return -1; 462 463 if ((mp = strchr(addrbuf, '/')) != NULL) { 464 *mp = '\0'; 465 mp++; 466 masklen = strtoul(mp, &cp, 10); 467 if (*mp < '0' || *mp > '9' || *cp != '\0' || masklen > 128) 468 return -1; 469 } 470 471 if (addr_pton(addrbuf, &tmp) == -1) 472 return -1; 473 474 if (mp == NULL) 475 masklen = addr_unicast_masklen(tmp.af); 476 if (masklen_valid(tmp.af, masklen) == -1) 477 return -2; 478 if (addr_host_is_all0s(&tmp, masklen) != 0) 479 return -2; 480 481 if (n != NULL) 482 memcpy(n, &tmp, sizeof(*n)); 483 if (l != NULL) 484 *l = masklen; 485 486 return 0; 487 } 488 489 int 490 addr_netmatch(const struct xaddr *host, const struct xaddr *net, u_int masklen) 491 { 492 struct xaddr tmp_mask, tmp_result; 493 494 if (host->af != net->af) 495 return -1; 496 497 if (addr_netmask(host->af, masklen, &tmp_mask) == -1) 498 return -1; 499 if (addr_and(&tmp_result, host, &tmp_mask) == -1) 500 return -1; 501 return addr_cmp(&tmp_result, net); 502 } 503