]> git.proxmox.com Git - systemd.git/blame - src/basic/in-addr-util.c
New upstream version 242
[systemd.git] / src / basic / in-addr-util.c
CommitLineData
52ad194e 1/* SPDX-License-Identifier: LGPL-2.1+ */
5eef597e
MP
2
3#include <arpa/inet.h>
4c89c718
MP
4#include <endian.h>
5#include <errno.h>
5a920b42 6#include <net/if.h>
4c89c718 7#include <stdint.h>
bb4f798a 8#include <stdio.h>
4c89c718 9#include <stdlib.h>
5eef597e 10
db2df898 11#include "alloc-util.h"
5eef597e 12#include "in-addr-util.h"
4c89c718 13#include "macro.h"
5a920b42 14#include "parse-util.h"
bb4f798a
MB
15#include "random-util.h"
16#include "strxcpyx.h"
4c89c718 17#include "util.h"
5eef597e 18
5a920b42 19bool in4_addr_is_null(const struct in_addr *a) {
2897b343 20 assert(a);
5a920b42 21
2897b343 22 return a->s_addr == 0;
5a920b42
MP
23}
24
5eef597e
MP
25int in_addr_is_null(int family, const union in_addr_union *u) {
26 assert(u);
27
28 if (family == AF_INET)
5a920b42 29 return in4_addr_is_null(&u->in);
5eef597e
MP
30
31 if (family == AF_INET6)
2897b343 32 return IN6_IS_ADDR_UNSPECIFIED(&u->in6);
5eef597e
MP
33
34 return -EAFNOSUPPORT;
35}
36
2897b343
MP
37bool in4_addr_is_link_local(const struct in_addr *a) {
38 assert(a);
39
40 return (be32toh(a->s_addr) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
41}
42
5eef597e
MP
43int in_addr_is_link_local(int family, const union in_addr_union *u) {
44 assert(u);
45
46 if (family == AF_INET)
2897b343 47 return in4_addr_is_link_local(&u->in);
5eef597e
MP
48
49 if (family == AF_INET6)
50 return IN6_IS_ADDR_LINKLOCAL(&u->in6);
51
52 return -EAFNOSUPPORT;
53}
54
2897b343
MP
55int in_addr_is_multicast(int family, const union in_addr_union *u) {
56 assert(u);
57
58 if (family == AF_INET)
59 return IN_MULTICAST(be32toh(u->in.s_addr));
60
61 if (family == AF_INET6)
62 return IN6_IS_ADDR_MULTICAST(&u->in6);
63
64 return -EAFNOSUPPORT;
65}
66
67bool in4_addr_is_localhost(const struct in_addr *a) {
68 assert(a);
69
70 /* All of 127.x.x.x is localhost. */
71 return (be32toh(a->s_addr) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
72}
73
bb4f798a
MB
74bool in4_addr_is_non_local(const struct in_addr *a) {
75 /* Whether the address is not null and not localhost.
76 *
77 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
78 return !in4_addr_is_null(a) &&
79 !in4_addr_is_localhost(a);
80}
81
4c89c718
MP
82int in_addr_is_localhost(int family, const union in_addr_union *u) {
83 assert(u);
84
85 if (family == AF_INET)
2897b343 86 return in4_addr_is_localhost(&u->in);
4c89c718
MP
87
88 if (family == AF_INET6)
89 return IN6_IS_ADDR_LOOPBACK(&u->in6);
90
91 return -EAFNOSUPPORT;
92}
93
5eef597e
MP
94int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
95 assert(a);
96 assert(b);
97
98 if (family == AF_INET)
99 return a->in.s_addr == b->in.s_addr;
100
101 if (family == AF_INET6)
102 return
103 a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
104 a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
105 a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
106 a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
107
108 return -EAFNOSUPPORT;
109}
110
111int in_addr_prefix_intersect(
112 int family,
113 const union in_addr_union *a,
114 unsigned aprefixlen,
115 const union in_addr_union *b,
116 unsigned bprefixlen) {
117
118 unsigned m;
119
120 assert(a);
121 assert(b);
122
123 /* Checks whether there are any addresses that are in both
124 * networks */
125
126 m = MIN(aprefixlen, bprefixlen);
127
128 if (family == AF_INET) {
129 uint32_t x, nm;
130
131 x = be32toh(a->in.s_addr ^ b->in.s_addr);
132 nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
133
134 return (x & nm) == 0;
135 }
136
137 if (family == AF_INET6) {
138 unsigned i;
139
140 if (m > 128)
141 m = 128;
142
143 for (i = 0; i < 16; i++) {
144 uint8_t x, nm;
145
146 x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
147
148 if (m < 8)
149 nm = 0xFF << (8 - m);
150 else
151 nm = 0xFF;
152
153 if ((x & nm) != 0)
154 return 0;
155
156 if (m > 8)
157 m -= 8;
158 else
159 m = 0;
160 }
161
162 return 1;
163 }
164
165 return -EAFNOSUPPORT;
166}
167
168int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
169 assert(u);
170
171 /* Increases the network part of an address by one. Returns
172 * positive it that succeeds, or 0 if this overflows. */
173
174 if (prefixlen <= 0)
175 return 0;
176
177 if (family == AF_INET) {
178 uint32_t c, n;
179
180 if (prefixlen > 32)
181 prefixlen = 32;
182
183 c = be32toh(u->in.s_addr);
184 n = c + (1UL << (32 - prefixlen));
185 if (n < c)
186 return 0;
187 n &= 0xFFFFFFFFUL << (32 - prefixlen);
188
189 u->in.s_addr = htobe32(n);
190 return 1;
191 }
192
193 if (family == AF_INET6) {
194 struct in6_addr add = {}, result;
195 uint8_t overflow = 0;
196 unsigned i;
197
198 if (prefixlen > 128)
199 prefixlen = 128;
200
201 /* First calculate what we have to add */
202 add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
203
204 for (i = 16; i > 0; i--) {
205 unsigned j = i - 1;
206
207 result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
208 overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
209 }
210
211 if (overflow)
212 return 0;
213
214 u->in6 = result;
215 return 1;
216 }
217
218 return -EAFNOSUPPORT;
219}
220
bb4f798a
MB
221int in_addr_random_prefix(
222 int family,
223 union in_addr_union *u,
224 unsigned prefixlen_fixed_part,
225 unsigned prefixlen) {
226
227 assert(u);
228
229 /* Random network part of an address by one. */
230
231 if (prefixlen <= 0)
232 return 0;
233
234 if (family == AF_INET) {
235 uint32_t c, n;
236
237 if (prefixlen_fixed_part > 32)
238 prefixlen_fixed_part = 32;
239 if (prefixlen > 32)
240 prefixlen = 32;
241 if (prefixlen_fixed_part >= prefixlen)
242 return -EINVAL;
243
244 c = be32toh(u->in.s_addr);
245 c &= ((UINT32_C(1) << prefixlen_fixed_part) - 1) << (32 - prefixlen_fixed_part);
246
247 random_bytes(&n, sizeof(n));
248 n &= ((UINT32_C(1) << (prefixlen - prefixlen_fixed_part)) - 1) << (32 - prefixlen);
249
250 u->in.s_addr = htobe32(n | c);
251 return 1;
252 }
253
254 if (family == AF_INET6) {
255 struct in6_addr n;
256 unsigned i, j;
257
258 if (prefixlen_fixed_part > 128)
259 prefixlen_fixed_part = 128;
260 if (prefixlen > 128)
261 prefixlen = 128;
262 if (prefixlen_fixed_part >= prefixlen)
263 return -EINVAL;
264
265 random_bytes(&n, sizeof(n));
266
267 for (i = 0; i < 16; i++) {
268 uint8_t mask_fixed_part = 0, mask = 0;
269
270 if (i < (prefixlen_fixed_part + 7) / 8) {
271 if (i < prefixlen_fixed_part / 8)
272 mask_fixed_part = 0xffu;
273 else {
274 j = prefixlen_fixed_part % 8;
275 mask_fixed_part = ((UINT8_C(1) << (j + 1)) - 1) << (8 - j);
276 }
277 }
278
279 if (i < (prefixlen + 7) / 8) {
280 if (i < prefixlen / 8)
281 mask = 0xffu ^ mask_fixed_part;
282 else {
283 j = prefixlen % 8;
284 mask = (((UINT8_C(1) << (j + 1)) - 1) << (8 - j)) ^ mask_fixed_part;
285 }
286 }
287
288 u->in6.s6_addr[i] &= mask_fixed_part;
289 u->in6.s6_addr[i] |= n.s6_addr[i] & mask;
290 }
291
292 return 1;
293 }
294
295 return -EAFNOSUPPORT;
296}
297
5eef597e 298int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
bb4f798a 299 _cleanup_free_ char *x = NULL;
5eef597e
MP
300 size_t l;
301
302 assert(u);
303 assert(ret);
304
305 if (family == AF_INET)
306 l = INET_ADDRSTRLEN;
307 else if (family == AF_INET6)
308 l = INET6_ADDRSTRLEN;
309 else
310 return -EAFNOSUPPORT;
311
312 x = new(char, l);
313 if (!x)
314 return -ENOMEM;
315
316 errno = 0;
bb4f798a 317 if (!inet_ntop(family, u, x, l))
4c89c718 318 return errno > 0 ? -errno : -EINVAL;
5eef597e 319
bb4f798a
MB
320 *ret = TAKE_PTR(x);
321 return 0;
322}
323
324int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned prefixlen, char **ret) {
325 _cleanup_free_ char *x = NULL;
326 char *p;
327 size_t l;
328
329 assert(u);
330 assert(ret);
331
332 if (family == AF_INET)
333 l = INET_ADDRSTRLEN + 3;
334 else if (family == AF_INET6)
335 l = INET6_ADDRSTRLEN + 4;
336 else
337 return -EAFNOSUPPORT;
338
339 if (prefixlen > FAMILY_ADDRESS_SIZE(family) * 8)
340 return -EINVAL;
341
342 x = new(char, l);
343 if (!x)
344 return -ENOMEM;
345
346 errno = 0;
347 if (!inet_ntop(family, u, x, l))
348 return errno > 0 ? -errno : -EINVAL;
349
350 p = x + strlen(x);
351 l -= strlen(x);
352 (void) strpcpyf(&p, l, "/%u", prefixlen);
353
354 *ret = TAKE_PTR(x);
5eef597e
MP
355 return 0;
356}
357
5a920b42 358int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret) {
bb4f798a 359 _cleanup_free_ char *x = NULL;
5a920b42 360 size_t l;
5a920b42
MP
361 int r;
362
363 assert(u);
364 assert(ret);
365
366 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
367 * handle IPv6 link-local addresses. */
368
369 if (family != AF_INET6)
370 goto fallback;
371 if (ifindex <= 0)
372 goto fallback;
373
374 r = in_addr_is_link_local(family, u);
375 if (r < 0)
376 return r;
377 if (r == 0)
378 goto fallback;
379
380 l = INET6_ADDRSTRLEN + 1 + DECIMAL_STR_MAX(ifindex) + 1;
381 x = new(char, l);
382 if (!x)
383 return -ENOMEM;
384
385 errno = 0;
bb4f798a 386 if (!inet_ntop(family, u, x, l))
5a920b42 387 return errno > 0 ? -errno : -EINVAL;
5a920b42
MP
388
389 sprintf(strchr(x, 0), "%%%i", ifindex);
5a920b42 390
bb4f798a 391 *ret = TAKE_PTR(x);
5a920b42
MP
392 return 0;
393
394fallback:
395 return in_addr_to_string(family, u, ret);
396}
397
5eef597e 398int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
2897b343 399 union in_addr_union buffer;
5eef597e 400 assert(s);
5eef597e
MP
401
402 if (!IN_SET(family, AF_INET, AF_INET6))
403 return -EAFNOSUPPORT;
404
405 errno = 0;
2897b343 406 if (inet_pton(family, s, ret ?: &buffer) <= 0)
4c89c718 407 return errno > 0 ? -errno : -EINVAL;
5eef597e
MP
408
409 return 0;
410}
411
f5e65279 412int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret) {
5eef597e
MP
413 int r;
414
415 assert(s);
5eef597e
MP
416
417 r = in_addr_from_string(AF_INET, s, ret);
418 if (r >= 0) {
f5e65279
MB
419 if (ret_family)
420 *ret_family = AF_INET;
5eef597e
MP
421 return 0;
422 }
423
424 r = in_addr_from_string(AF_INET6, s, ret);
425 if (r >= 0) {
f5e65279
MB
426 if (ret_family)
427 *ret_family = AF_INET6;
5eef597e
MP
428 return 0;
429 }
430
431 return -EINVAL;
432}
433
5a920b42 434int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_union *ret, int *ifindex) {
6e866b33 435 _cleanup_free_ char *buf = NULL;
5a920b42
MP
436 const char *suffix;
437 int r, ifi = 0;
438
439 assert(s);
440 assert(family);
441 assert(ret);
442
443 /* Similar to in_addr_from_string_auto() but also parses an optionally appended IPv6 zone suffix ("scope id")
444 * if one is found. */
445
446 suffix = strchr(s, '%');
447 if (suffix) {
448
449 if (ifindex) {
450 /* If we shall return the interface index, try to parse it */
451 r = parse_ifindex(suffix + 1, &ifi);
452 if (r < 0) {
453 unsigned u;
454
455 u = if_nametoindex(suffix + 1);
456 if (u <= 0)
457 return -errno;
458
459 ifi = (int) u;
460 }
461 }
462
6e866b33
MB
463 buf = strndup(s, suffix - s);
464 if (!buf)
465 return -ENOMEM;
466
467 s = buf;
5a920b42
MP
468 }
469
470 r = in_addr_from_string_auto(s, family, ret);
471 if (r < 0)
472 return r;
473
474 if (ifindex)
475 *ifindex = ifi;
476
477 return r;
478}
479
f5e65279 480unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr) {
5eef597e
MP
481 assert(addr);
482
6e866b33 483 return 32U - u32ctz(be32toh(addr->s_addr));
5eef597e
MP
484}
485
f5e65279 486struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
e735f4d4
MP
487 assert(addr);
488 assert(prefixlen <= 32);
489
490 /* Shifting beyond 32 is not defined, handle this specially. */
491 if (prefixlen == 0)
492 addr->s_addr = 0;
493 else
494 addr->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
495
496 return addr;
497}
498
f5e65279 499int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
f47781d8
MP
500 uint8_t msb_octet = *(uint8_t*) addr;
501
502 /* addr may not be aligned, so make sure we only access it byte-wise */
5eef597e
MP
503
504 assert(addr);
5eef597e
MP
505 assert(prefixlen);
506
f47781d8 507 if (msb_octet < 128)
5eef597e
MP
508 /* class A, leading bits: 0 */
509 *prefixlen = 8;
f47781d8 510 else if (msb_octet < 192)
5eef597e
MP
511 /* class B, leading bits 10 */
512 *prefixlen = 16;
f47781d8 513 else if (msb_octet < 224)
5eef597e
MP
514 /* class C, leading bits 110 */
515 *prefixlen = 24;
516 else
517 /* class D or E, no default prefixlen */
518 return -ERANGE;
519
520 return 0;
521}
522
f5e65279 523int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
5eef597e
MP
524 unsigned char prefixlen;
525 int r;
526
527 assert(addr);
528 assert(mask);
529
f5e65279 530 r = in4_addr_default_prefixlen(addr, &prefixlen);
5eef597e
MP
531 if (r < 0)
532 return r;
533
f5e65279 534 in4_addr_prefixlen_to_netmask(mask, prefixlen);
e735f4d4
MP
535 return 0;
536}
537
538int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) {
539 assert(addr);
5eef597e 540
e735f4d4
MP
541 if (family == AF_INET) {
542 struct in_addr mask;
5eef597e 543
f5e65279 544 if (!in4_addr_prefixlen_to_netmask(&mask, prefixlen))
e735f4d4
MP
545 return -EINVAL;
546
547 addr->in.s_addr &= mask.s_addr;
548 return 0;
549 }
550
551 if (family == AF_INET6) {
552 unsigned i;
553
554 for (i = 0; i < 16; i++) {
555 uint8_t mask;
556
557 if (prefixlen >= 8) {
558 mask = 0xFF;
559 prefixlen -= 8;
560 } else {
561 mask = 0xFF << (8 - prefixlen);
562 prefixlen = 0;
563 }
564
565 addr->in6.s6_addr[i] &= mask;
566 }
567
568 return 0;
569 }
570
571 return -EAFNOSUPPORT;
5eef597e 572}
81c58355 573
f5e65279
MB
574int in_addr_prefix_covers(int family,
575 const union in_addr_union *prefix,
576 unsigned char prefixlen,
577 const union in_addr_union *address) {
578
579 union in_addr_union masked_prefix, masked_address;
580 int r;
581
582 assert(prefix);
583 assert(address);
584
585 masked_prefix = *prefix;
586 r = in_addr_mask(family, &masked_prefix, prefixlen);
587 if (r < 0)
588 return r;
589
590 masked_address = *address;
591 r = in_addr_mask(family, &masked_address, prefixlen);
592 if (r < 0)
593 return r;
594
595 return in_addr_equal(family, &masked_prefix, &masked_address);
596}
597
598int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret) {
599 uint8_t u;
600 int r;
601
602 if (!IN_SET(family, AF_INET, AF_INET6))
603 return -EAFNOSUPPORT;
604
605 r = safe_atou8(p, &u);
606 if (r < 0)
607 return r;
608
609 if (u > FAMILY_ADDRESS_SIZE(family) * 8)
610 return -ERANGE;
611
612 *ret = u;
613 return 0;
614}
615
7c20daf6 616int in_addr_prefix_from_string(
f5e65279
MB
617 const char *p,
618 int family,
619 union in_addr_union *ret_prefix,
620 unsigned char *ret_prefixlen) {
621
6e866b33 622 _cleanup_free_ char *str = NULL;
81c58355
MB
623 union in_addr_union buffer;
624 const char *e, *l;
f5e65279 625 unsigned char k;
81c58355
MB
626 int r;
627
628 assert(p);
629
630 if (!IN_SET(family, AF_INET, AF_INET6))
631 return -EAFNOSUPPORT;
632
633 e = strchr(p, '/');
6e866b33
MB
634 if (e) {
635 str = strndup(p, e - p);
636 if (!str)
637 return -ENOMEM;
638
639 l = str;
640 } else
81c58355
MB
641 l = p;
642
643 r = in_addr_from_string(family, l, &buffer);
644 if (r < 0)
645 return r;
646
81c58355 647 if (e) {
f5e65279 648 r = in_addr_parse_prefixlen(family, e+1, &k);
81c58355
MB
649 if (r < 0)
650 return r;
f5e65279
MB
651 } else
652 k = FAMILY_ADDRESS_SIZE(family) * 8;
81c58355 653
f5e65279
MB
654 if (ret_prefix)
655 *ret_prefix = buffer;
656 if (ret_prefixlen)
657 *ret_prefixlen = k;
81c58355 658
f5e65279
MB
659 return 0;
660}
661
6e866b33 662int in_addr_prefix_from_string_auto_internal(
f5e65279 663 const char *p,
7c20daf6 664 InAddrPrefixLenMode mode,
f5e65279
MB
665 int *ret_family,
666 union in_addr_union *ret_prefix,
667 unsigned char *ret_prefixlen) {
668
6e866b33 669 _cleanup_free_ char *str = NULL;
f5e65279
MB
670 union in_addr_union buffer;
671 const char *e, *l;
672 unsigned char k;
673 int family, r;
674
675 assert(p);
676
677 e = strchr(p, '/');
6e866b33
MB
678 if (e) {
679 str = strndup(p, e - p);
680 if (!str)
681 return -ENOMEM;
682
683 l = str;
684 } else
f5e65279 685 l = p;
81c58355 686
f5e65279
MB
687 r = in_addr_from_string_auto(l, &family, &buffer);
688 if (r < 0)
689 return r;
690
691 if (e) {
692 r = in_addr_parse_prefixlen(family, e+1, &k);
693 if (r < 0)
694 return r;
695 } else
7c20daf6
FS
696 switch (mode) {
697 case PREFIXLEN_FULL:
698 k = FAMILY_ADDRESS_SIZE(family) * 8;
699 break;
700 case PREFIXLEN_REFUSE:
701 return -ENOANO; /* To distinguish this error from others. */
702 case PREFIXLEN_LEGACY:
703 if (family == AF_INET) {
704 r = in4_addr_default_prefixlen(&buffer.in, &k);
705 if (r < 0)
706 return r;
707 } else
708 k = 0;
709 break;
710 default:
711 assert_not_reached("Invalid prefixlen mode");
712 }
f5e65279
MB
713
714 if (ret_family)
715 *ret_family = family;
716 if (ret_prefix)
717 *ret_prefix = buffer;
718 if (ret_prefixlen)
719 *ret_prefixlen = k;
81c58355
MB
720
721 return 0;
f5e65279 722
81c58355 723}
6e866b33
MB
724
725static void in_addr_data_hash_func(const struct in_addr_data *a, struct siphash *state) {
726 siphash24_compress(&a->family, sizeof(a->family), state);
727 siphash24_compress(&a->address, FAMILY_ADDRESS_SIZE(a->family), state);
728}
729
730static int in_addr_data_compare_func(const struct in_addr_data *x, const struct in_addr_data *y) {
731 int r;
732
733 r = CMP(x->family, y->family);
734 if (r != 0)
735 return r;
736
737 return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
738}
739
740DEFINE_HASH_OPS(in_addr_data_hash_ops, struct in_addr_data, in_addr_data_hash_func, in_addr_data_compare_func);