1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "alloc-util.h"
12 #include "errno-util.h"
13 #include "in-addr-util.h"
15 #include "parse-util.h"
16 #include "random-util.h"
17 #include "string-util.h"
21 bool in4_addr_is_null(const struct in_addr
*a
) {
24 return a
->s_addr
== 0;
27 bool in6_addr_is_null(const struct in6_addr
*a
) {
30 return IN6_IS_ADDR_UNSPECIFIED(a
);
33 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
36 if (family
== AF_INET
)
37 return in4_addr_is_null(&u
->in
);
39 if (family
== AF_INET6
)
40 return in6_addr_is_null(&u
->in6
);
45 bool in4_addr_is_link_local(const struct in_addr
*a
) {
48 return (be32toh(a
->s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
51 bool in6_addr_is_link_local(const struct in6_addr
*a
) {
54 return IN6_IS_ADDR_LINKLOCAL(a
); /* lgtm [cpp/potentially-dangerous-function] */
57 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
60 if (family
== AF_INET
)
61 return in4_addr_is_link_local(&u
->in
);
63 if (family
== AF_INET6
)
64 return in6_addr_is_link_local(&u
->in6
);
69 bool in6_addr_is_link_local_all_nodes(const struct in6_addr
*a
) {
73 return be32toh(a
->s6_addr32
[0]) == UINT32_C(0xff020000) &&
74 a
->s6_addr32
[1] == 0 &&
75 a
->s6_addr32
[2] == 0 &&
76 be32toh(a
->s6_addr32
[3]) == UINT32_C(0x00000001);
79 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
82 if (family
== AF_INET
)
83 return IN_MULTICAST(be32toh(u
->in
.s_addr
));
85 if (family
== AF_INET6
)
86 return IN6_IS_ADDR_MULTICAST(&u
->in6
);
91 bool in4_addr_is_local_multicast(const struct in_addr
*a
) {
94 return (be32toh(a
->s_addr
) & UINT32_C(0xffffff00)) == UINT32_C(0xe0000000);
97 bool in4_addr_is_localhost(const struct in_addr
*a
) {
100 /* All of 127.x.x.x is localhost. */
101 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
104 bool in4_addr_is_non_local(const struct in_addr
*a
) {
105 /* Whether the address is not null and not localhost.
107 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
108 return !in4_addr_is_null(a
) &&
109 !in4_addr_is_localhost(a
);
112 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
115 if (family
== AF_INET
)
116 return in4_addr_is_localhost(&u
->in
);
118 if (family
== AF_INET6
)
119 return IN6_IS_ADDR_LOOPBACK(&u
->in6
); /* lgtm [cpp/potentially-dangerous-function] */
121 return -EAFNOSUPPORT
;
124 bool in6_addr_is_ipv4_mapped_address(const struct in6_addr
*a
) {
125 return a
->s6_addr32
[0] == 0 &&
126 a
->s6_addr32
[1] == 0 &&
127 a
->s6_addr32
[2] == htobe32(UINT32_C(0x0000ffff));
130 bool in4_addr_equal(const struct in_addr
*a
, const struct in_addr
*b
) {
134 return a
->s_addr
== b
->s_addr
;
137 bool in6_addr_equal(const struct in6_addr
*a
, const struct in6_addr
*b
) {
141 return IN6_ARE_ADDR_EQUAL(a
, b
);
144 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
148 if (family
== AF_INET
)
149 return in4_addr_equal(&a
->in
, &b
->in
);
151 if (family
== AF_INET6
)
152 return in6_addr_equal(&a
->in6
, &b
->in6
);
154 return -EAFNOSUPPORT
;
157 int in_addr_prefix_intersect(
159 const union in_addr_union
*a
,
161 const union in_addr_union
*b
,
162 unsigned bprefixlen
) {
169 /* Checks whether there are any addresses that are in both
172 m
= MIN(aprefixlen
, bprefixlen
);
174 if (family
== AF_INET
) {
177 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
178 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
180 return (x
& nm
) == 0;
183 if (family
== AF_INET6
) {
189 for (i
= 0; i
< 16; i
++) {
192 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
195 nm
= 0xFF << (8 - m
);
211 return -EAFNOSUPPORT
;
214 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
217 /* Increases the network part of an address by one. Returns 0 if that succeeds, or -ERANGE if
220 return in_addr_prefix_nth(family
, u
, prefixlen
, 1);
224 * Calculates the nth prefix of size prefixlen starting from the address denoted by u.
226 * On success 0 will be returned and the calculated prefix will be available in
227 * u. In case the calculation cannot be performed (invalid prefix length,
228 * overflows would occur) -ERANGE is returned. If the address family given isn't
229 * supported -EAFNOSUPPORT will be returned.
232 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 0, writes 192.168.2.0 to u
233 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 0, no data written
234 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
235 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
236 * - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 0, writes 2001:0db8:0000:ff00:: to u
238 int in_addr_prefix_nth(int family
, union in_addr_union
*u
, unsigned prefixlen
, uint64_t nth
) {
244 if (family
== AF_INET
) {
250 c
= be32toh(u
->in
.s_addr
);
252 t
= nth
<< (32 - prefixlen
);
255 if (c
> UINT32_MAX
- t
)
260 n
&= UINT32_C(0xFFFFFFFF) << (32 - prefixlen
);
261 u
->in
.s_addr
= htobe32(n
);
265 if (family
== AF_INET6
) {
266 bool overflow
= false;
271 for (unsigned i
= 16; i
> 0; i
--) {
272 unsigned t
, j
= i
- 1, p
= j
* 8;
274 if (p
>= prefixlen
) {
275 u
->in6
.s6_addr
[j
] = 0;
279 if (prefixlen
- p
< 8) {
280 u
->in6
.s6_addr
[j
] &= 0xff << (8 - (prefixlen
- p
));
281 t
= u
->in6
.s6_addr
[j
] + ((nth
& 0xff) << (8 - (prefixlen
- p
)));
282 nth
>>= prefixlen
- p
;
284 t
= u
->in6
.s6_addr
[j
] + (nth
& 0xff) + overflow
;
288 overflow
= t
> UINT8_MAX
;
289 u
->in6
.s6_addr
[j
] = (uint8_t) (t
& 0xff);
292 if (overflow
|| nth
!= 0)
298 return -EAFNOSUPPORT
;
301 int in_addr_random_prefix(
303 union in_addr_union
*u
,
304 unsigned prefixlen_fixed_part
,
305 unsigned prefixlen
) {
309 /* Random network part of an address by one. */
314 if (family
== AF_INET
) {
317 if (prefixlen_fixed_part
> 32)
318 prefixlen_fixed_part
= 32;
321 if (prefixlen_fixed_part
>= prefixlen
)
324 c
= be32toh(u
->in
.s_addr
);
325 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
327 random_bytes(&n
, sizeof(n
));
328 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
330 u
->in
.s_addr
= htobe32(n
| c
);
334 if (family
== AF_INET6
) {
338 if (prefixlen_fixed_part
> 128)
339 prefixlen_fixed_part
= 128;
342 if (prefixlen_fixed_part
>= prefixlen
)
345 random_bytes(&n
, sizeof(n
));
347 for (i
= 0; i
< 16; i
++) {
348 uint8_t mask_fixed_part
= 0, mask
= 0;
350 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
351 if (i
< prefixlen_fixed_part
/ 8)
352 mask_fixed_part
= 0xffu
;
354 j
= prefixlen_fixed_part
% 8;
355 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
359 if (i
< (prefixlen
+ 7) / 8) {
360 if (i
< prefixlen
/ 8)
361 mask
= 0xffu
^ mask_fixed_part
;
364 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
368 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
369 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
375 return -EAFNOSUPPORT
;
378 int in_addr_prefix_range(
380 const union in_addr_union
*in
,
382 union in_addr_union
*ret_start
,
383 union in_addr_union
*ret_end
) {
385 union in_addr_union start
, end
;
390 if (!IN_SET(family
, AF_INET
, AF_INET6
))
391 return -EAFNOSUPPORT
;
395 r
= in_addr_prefix_nth(family
, &start
, prefixlen
, 0);
402 r
= in_addr_prefix_nth(family
, &end
, prefixlen
, 1);
415 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
416 _cleanup_free_
char *x
= NULL
;
422 if (family
== AF_INET
)
424 else if (family
== AF_INET6
)
425 l
= INET6_ADDRSTRLEN
;
427 return -EAFNOSUPPORT
;
434 if (!inet_ntop(family
, u
, x
, l
))
435 return errno_or_else(EINVAL
);
441 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
442 _cleanup_free_
char *x
= NULL
;
449 if (family
== AF_INET
)
450 l
= INET_ADDRSTRLEN
+ 3;
451 else if (family
== AF_INET6
)
452 l
= INET6_ADDRSTRLEN
+ 4;
454 return -EAFNOSUPPORT
;
456 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
464 if (!inet_ntop(family
, u
, x
, l
))
465 return errno_or_else(EINVAL
);
469 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
475 int in_addr_port_ifindex_name_to_string(int family
, const union in_addr_union
*u
, uint16_t port
, int ifindex
, const char *server_name
, char **ret
) {
476 _cleanup_free_
char *ip_str
= NULL
, *x
= NULL
;
479 assert(IN_SET(family
, AF_INET
, AF_INET6
));
483 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
484 * handle IPv6 link-local addresses. */
486 r
= in_addr_to_string(family
, u
, &ip_str
);
490 if (family
== AF_INET6
) {
491 r
= in_addr_is_link_local(family
, u
);
497 ifindex
= 0; /* For IPv4 address, ifindex is always ignored. */
499 if (port
== 0 && ifindex
== 0 && isempty(server_name
)) {
500 *ret
= TAKE_PTR(ip_str
);
504 const char *separator
= isempty(server_name
) ? "" : "#";
505 server_name
= strempty(server_name
);
508 if (family
== AF_INET6
) {
510 r
= asprintf(&x
, "[%s]:%"PRIu16
"%%%i%s%s", ip_str
, port
, ifindex
, separator
, server_name
);
512 r
= asprintf(&x
, "[%s]:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
514 r
= asprintf(&x
, "%s:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
517 r
= asprintf(&x
, "%s%%%i%s%s", ip_str
, ifindex
, separator
, server_name
);
519 x
= strjoin(ip_str
, separator
, server_name
);
530 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
531 union in_addr_union buffer
;
534 if (!IN_SET(family
, AF_INET
, AF_INET6
))
535 return -EAFNOSUPPORT
;
538 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
539 return errno_or_else(EINVAL
);
544 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
549 r
= in_addr_from_string(AF_INET
, s
, ret
);
552 *ret_family
= AF_INET
;
556 r
= in_addr_from_string(AF_INET6
, s
, ret
);
559 *ret_family
= AF_INET6
;
566 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
569 return 32U - u32ctz(be32toh(addr
->s_addr
));
572 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
574 assert(prefixlen
<= 32);
576 /* Shifting beyond 32 is not defined, handle this specially. */
580 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
585 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
586 uint8_t msb_octet
= *(uint8_t*) addr
;
588 /* addr may not be aligned, so make sure we only access it byte-wise */
594 /* class A, leading bits: 0 */
596 else if (msb_octet
< 192)
597 /* class B, leading bits 10 */
599 else if (msb_octet
< 224)
600 /* class C, leading bits 110 */
603 /* class D or E, no default prefixlen */
609 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
610 unsigned char prefixlen
;
616 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
620 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
624 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
627 if (family
== AF_INET
) {
630 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
633 addr
->in
.s_addr
&= mask
.s_addr
;
637 if (family
== AF_INET6
) {
640 for (i
= 0; i
< 16; i
++) {
643 if (prefixlen
>= 8) {
647 mask
= 0xFF << (8 - prefixlen
);
651 addr
->in6
.s6_addr
[i
] &= mask
;
657 return -EAFNOSUPPORT
;
660 int in_addr_prefix_covers(int family
,
661 const union in_addr_union
*prefix
,
662 unsigned char prefixlen
,
663 const union in_addr_union
*address
) {
665 union in_addr_union masked_prefix
, masked_address
;
671 masked_prefix
= *prefix
;
672 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
676 masked_address
= *address
;
677 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
681 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
684 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
688 if (!IN_SET(family
, AF_INET
, AF_INET6
))
689 return -EAFNOSUPPORT
;
691 r
= safe_atou8(p
, &u
);
695 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
702 int in_addr_prefix_from_string(
705 union in_addr_union
*ret_prefix
,
706 unsigned char *ret_prefixlen
) {
708 _cleanup_free_
char *str
= NULL
;
709 union in_addr_union buffer
;
716 if (!IN_SET(family
, AF_INET
, AF_INET6
))
717 return -EAFNOSUPPORT
;
721 str
= strndup(p
, e
- p
);
729 r
= in_addr_from_string(family
, l
, &buffer
);
734 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
738 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
741 *ret_prefix
= buffer
;
748 int in_addr_prefix_from_string_auto_internal(
750 InAddrPrefixLenMode mode
,
752 union in_addr_union
*ret_prefix
,
753 unsigned char *ret_prefixlen
) {
755 _cleanup_free_
char *str
= NULL
;
756 union in_addr_union buffer
;
765 str
= strndup(p
, e
- p
);
773 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
778 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
784 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
786 case PREFIXLEN_REFUSE
:
787 return -ENOANO
; /* To distinguish this error from others. */
788 case PREFIXLEN_LEGACY
:
789 if (family
== AF_INET
) {
790 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
797 assert_not_reached("Invalid prefixlen mode");
801 *ret_family
= family
;
803 *ret_prefix
= buffer
;
811 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
815 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
816 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
819 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
825 r
= CMP(x
->family
, y
->family
);
829 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
832 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);
834 static void in_addr_prefix_hash_func(const struct in_addr_prefix
*a
, struct siphash
*state
) {
838 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
839 siphash24_compress(&a
->prefixlen
, sizeof(a
->prefixlen
), state
);
840 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
843 static int in_addr_prefix_compare_func(const struct in_addr_prefix
*x
, const struct in_addr_prefix
*y
) {
849 r
= CMP(x
->family
, y
->family
);
853 r
= CMP(x
->prefixlen
, y
->prefixlen
);
857 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
860 DEFINE_HASH_OPS(in_addr_prefix_hash_ops
, struct in_addr_prefix
, in_addr_prefix_hash_func
, in_addr_prefix_compare_func
);
861 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(in_addr_prefix_hash_ops_free
, struct in_addr_prefix
, in_addr_prefix_hash_func
, in_addr_prefix_compare_func
, free
);
863 void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
867 siphash24_compress(addr
, sizeof(*addr
), state
);
870 int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
874 return memcmp(a
, b
, sizeof(*a
));
877 DEFINE_HASH_OPS(in6_addr_hash_ops
, struct in6_addr
, in6_addr_hash_func
, in6_addr_compare_func
);