1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
30 if (family
== AF_INET
)
31 return in4_addr_is_null(&u
->in
);
33 if (family
== AF_INET6
)
34 return IN6_IS_ADDR_UNSPECIFIED(&u
->in6
);
39 bool in4_addr_is_link_local(const struct in_addr
*a
) {
42 return (be32toh(a
->s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
45 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
48 if (family
== AF_INET
)
49 return in4_addr_is_link_local(&u
->in
);
51 if (family
== AF_INET6
)
52 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
57 bool in6_addr_is_link_local_all_nodes(const struct in6_addr
*a
) {
61 return be32toh(a
->s6_addr32
[0]) == UINT32_C(0xff020000) &&
62 a
->s6_addr32
[1] == 0 &&
63 a
->s6_addr32
[2] == 0 &&
64 be32toh(a
->s6_addr32
[3]) == UINT32_C(0x00000001);
67 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
70 if (family
== AF_INET
)
71 return IN_MULTICAST(be32toh(u
->in
.s_addr
));
73 if (family
== AF_INET6
)
74 return IN6_IS_ADDR_MULTICAST(&u
->in6
);
79 bool in4_addr_is_localhost(const struct in_addr
*a
) {
82 /* All of 127.x.x.x is localhost. */
83 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
86 bool in4_addr_is_non_local(const struct in_addr
*a
) {
87 /* Whether the address is not null and not localhost.
89 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
90 return !in4_addr_is_null(a
) &&
91 !in4_addr_is_localhost(a
);
94 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
97 if (family
== AF_INET
)
98 return in4_addr_is_localhost(&u
->in
);
100 if (family
== AF_INET6
)
101 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
103 return -EAFNOSUPPORT
;
106 bool in4_addr_equal(const struct in_addr
*a
, const struct in_addr
*b
) {
110 return a
->s_addr
== b
->s_addr
;
113 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
117 if (family
== AF_INET
)
118 return in4_addr_equal(&a
->in
, &b
->in
);
120 if (family
== AF_INET6
)
121 return IN6_ARE_ADDR_EQUAL(&a
->in6
, &b
->in6
);
123 return -EAFNOSUPPORT
;
126 int in_addr_prefix_intersect(
128 const union in_addr_union
*a
,
130 const union in_addr_union
*b
,
131 unsigned bprefixlen
) {
138 /* Checks whether there are any addresses that are in both
141 m
= MIN(aprefixlen
, bprefixlen
);
143 if (family
== AF_INET
) {
146 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
147 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
149 return (x
& nm
) == 0;
152 if (family
== AF_INET6
) {
158 for (i
= 0; i
< 16; i
++) {
161 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
164 nm
= 0xFF << (8 - m
);
180 return -EAFNOSUPPORT
;
183 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
186 /* Increases the network part of an address by one. Returns
187 * positive if that succeeds, or -ERANGE if this overflows. */
189 return in_addr_prefix_nth(family
, u
, prefixlen
, 1);
193 * Calculates the nth prefix of size prefixlen starting from the address denoted by u.
195 * On success 1 will be returned and the calculated prefix will be available in
196 * u. In the case nth == 0 the input will be left unchanged and 1 will be returned.
197 * In case the calculation cannot be performed (invalid prefix length,
198 * overflows would occur) -ERANGE is returned. If the address family given isn't
199 * supported -EAFNOSUPPORT will be returned.
203 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 1, writes 192.168.2.0 to u
204 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 1, no data written
205 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
206 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
207 * - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 1, writes 2001:0db8:0000:ff00:: to u
209 int in_addr_prefix_nth(int family
, union in_addr_union
*u
, unsigned prefixlen
, uint64_t nth
) {
218 if (family
== AF_INET
) {
223 c
= be32toh(u
->in
.s_addr
);
225 t
= nth
<< (32 - prefixlen
);
228 if (c
> UINT32_MAX
- t
)
233 n
&= UINT32_C(0xFFFFFFFF) << (32 - prefixlen
);
234 u
->in
.s_addr
= htobe32(n
);
238 if (family
== AF_INET6
) {
239 struct in6_addr result
= {};
240 uint8_t overflow
= 0;
241 uint64_t delta
; /* this assumes that we only ever have to up to 1<<64 subnets */
242 unsigned start_byte
= (prefixlen
- 1) / 8;
247 /* First calculate what we have to add */
248 delta
= nth
<< ((128 - prefixlen
) % 8);
250 for (unsigned i
= 16; i
> 0; i
--) {
254 if (j
<= start_byte
) {
260 t
= u
->in6
.s6_addr
[j
] + d
+ overflow
;
261 overflow
= t
> UINT8_MAX
? t
- UINT8_MAX
: 0;
263 result
.s6_addr
[j
] = (uint8_t)t
;
265 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
];
268 if (overflow
|| delta
!= 0)
275 return -EAFNOSUPPORT
;
278 int in_addr_random_prefix(
280 union in_addr_union
*u
,
281 unsigned prefixlen_fixed_part
,
282 unsigned prefixlen
) {
286 /* Random network part of an address by one. */
291 if (family
== AF_INET
) {
294 if (prefixlen_fixed_part
> 32)
295 prefixlen_fixed_part
= 32;
298 if (prefixlen_fixed_part
>= prefixlen
)
301 c
= be32toh(u
->in
.s_addr
);
302 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
304 random_bytes(&n
, sizeof(n
));
305 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
307 u
->in
.s_addr
= htobe32(n
| c
);
311 if (family
== AF_INET6
) {
315 if (prefixlen_fixed_part
> 128)
316 prefixlen_fixed_part
= 128;
319 if (prefixlen_fixed_part
>= prefixlen
)
322 random_bytes(&n
, sizeof(n
));
324 for (i
= 0; i
< 16; i
++) {
325 uint8_t mask_fixed_part
= 0, mask
= 0;
327 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
328 if (i
< prefixlen_fixed_part
/ 8)
329 mask_fixed_part
= 0xffu
;
331 j
= prefixlen_fixed_part
% 8;
332 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
336 if (i
< (prefixlen
+ 7) / 8) {
337 if (i
< prefixlen
/ 8)
338 mask
= 0xffu
^ mask_fixed_part
;
341 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
345 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
346 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
352 return -EAFNOSUPPORT
;
355 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
356 _cleanup_free_
char *x
= NULL
;
362 if (family
== AF_INET
)
364 else if (family
== AF_INET6
)
365 l
= INET6_ADDRSTRLEN
;
367 return -EAFNOSUPPORT
;
374 if (!inet_ntop(family
, u
, x
, l
))
375 return errno_or_else(EINVAL
);
381 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
382 _cleanup_free_
char *x
= NULL
;
389 if (family
== AF_INET
)
390 l
= INET_ADDRSTRLEN
+ 3;
391 else if (family
== AF_INET6
)
392 l
= INET6_ADDRSTRLEN
+ 4;
394 return -EAFNOSUPPORT
;
396 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
404 if (!inet_ntop(family
, u
, x
, l
))
405 return errno_or_else(EINVAL
);
409 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
415 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
) {
416 _cleanup_free_
char *ip_str
= NULL
, *x
= NULL
;
419 assert(IN_SET(family
, AF_INET
, AF_INET6
));
423 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
424 * handle IPv6 link-local addresses. */
426 r
= in_addr_to_string(family
, u
, &ip_str
);
430 if (family
== AF_INET6
) {
431 r
= in_addr_is_link_local(family
, u
);
437 ifindex
= 0; /* For IPv4 address, ifindex is always ignored. */
439 if (port
== 0 && ifindex
== 0 && isempty(server_name
)) {
440 *ret
= TAKE_PTR(ip_str
);
444 const char *separator
= isempty(server_name
) ? "" : "#";
445 server_name
= strempty(server_name
);
448 if (family
== AF_INET6
) {
450 r
= asprintf(&x
, "[%s]:%"PRIu16
"%%%i%s%s", ip_str
, port
, ifindex
, separator
, server_name
);
452 r
= asprintf(&x
, "[%s]:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
454 r
= asprintf(&x
, "%s:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
457 r
= asprintf(&x
, "%s%%%i%s%s", ip_str
, ifindex
, separator
, server_name
);
459 x
= strjoin(ip_str
, separator
, server_name
);
470 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
471 union in_addr_union buffer
;
474 if (!IN_SET(family
, AF_INET
, AF_INET6
))
475 return -EAFNOSUPPORT
;
478 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
479 return errno_or_else(EINVAL
);
484 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
489 r
= in_addr_from_string(AF_INET
, s
, ret
);
492 *ret_family
= AF_INET
;
496 r
= in_addr_from_string(AF_INET6
, s
, ret
);
499 *ret_family
= AF_INET6
;
506 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
509 return 32U - u32ctz(be32toh(addr
->s_addr
));
512 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
514 assert(prefixlen
<= 32);
516 /* Shifting beyond 32 is not defined, handle this specially. */
520 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
525 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
526 uint8_t msb_octet
= *(uint8_t*) addr
;
528 /* addr may not be aligned, so make sure we only access it byte-wise */
534 /* class A, leading bits: 0 */
536 else if (msb_octet
< 192)
537 /* class B, leading bits 10 */
539 else if (msb_octet
< 224)
540 /* class C, leading bits 110 */
543 /* class D or E, no default prefixlen */
549 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
550 unsigned char prefixlen
;
556 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
560 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
564 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
567 if (family
== AF_INET
) {
570 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
573 addr
->in
.s_addr
&= mask
.s_addr
;
577 if (family
== AF_INET6
) {
580 for (i
= 0; i
< 16; i
++) {
583 if (prefixlen
>= 8) {
587 mask
= 0xFF << (8 - prefixlen
);
591 addr
->in6
.s6_addr
[i
] &= mask
;
597 return -EAFNOSUPPORT
;
600 int in_addr_prefix_covers(int family
,
601 const union in_addr_union
*prefix
,
602 unsigned char prefixlen
,
603 const union in_addr_union
*address
) {
605 union in_addr_union masked_prefix
, masked_address
;
611 masked_prefix
= *prefix
;
612 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
616 masked_address
= *address
;
617 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
621 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
624 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
628 if (!IN_SET(family
, AF_INET
, AF_INET6
))
629 return -EAFNOSUPPORT
;
631 r
= safe_atou8(p
, &u
);
635 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
642 int in_addr_prefix_from_string(
645 union in_addr_union
*ret_prefix
,
646 unsigned char *ret_prefixlen
) {
648 _cleanup_free_
char *str
= NULL
;
649 union in_addr_union buffer
;
656 if (!IN_SET(family
, AF_INET
, AF_INET6
))
657 return -EAFNOSUPPORT
;
661 str
= strndup(p
, e
- p
);
669 r
= in_addr_from_string(family
, l
, &buffer
);
674 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
678 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
681 *ret_prefix
= buffer
;
688 int in_addr_prefix_from_string_auto_internal(
690 InAddrPrefixLenMode mode
,
692 union in_addr_union
*ret_prefix
,
693 unsigned char *ret_prefixlen
) {
695 _cleanup_free_
char *str
= NULL
;
696 union in_addr_union buffer
;
705 str
= strndup(p
, e
- p
);
713 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
718 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
724 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
726 case PREFIXLEN_REFUSE
:
727 return -ENOANO
; /* To distinguish this error from others. */
728 case PREFIXLEN_LEGACY
:
729 if (family
== AF_INET
) {
730 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
737 assert_not_reached("Invalid prefixlen mode");
741 *ret_family
= family
;
743 *ret_prefix
= buffer
;
751 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
752 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
753 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
756 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
759 r
= CMP(x
->family
, y
->family
);
763 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
766 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);
768 void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
771 siphash24_compress(addr
, sizeof(*addr
), state
);
774 int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
775 return memcmp(a
, b
, sizeof(*a
));
778 DEFINE_HASH_OPS(in6_addr_hash_ops
, struct in6_addr
, in6_addr_hash_func
, in6_addr_compare_func
);