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
);
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
);
121 return -EAFNOSUPPORT
;
124 bool in4_addr_equal(const struct in_addr
*a
, const struct in_addr
*b
) {
128 return a
->s_addr
== b
->s_addr
;
131 bool in6_addr_equal(const struct in6_addr
*a
, const struct in6_addr
*b
) {
135 return IN6_ARE_ADDR_EQUAL(a
, b
);
138 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
142 if (family
== AF_INET
)
143 return in4_addr_equal(&a
->in
, &b
->in
);
145 if (family
== AF_INET6
)
146 return in6_addr_equal(&a
->in6
, &b
->in6
);
148 return -EAFNOSUPPORT
;
151 int in_addr_prefix_intersect(
153 const union in_addr_union
*a
,
155 const union in_addr_union
*b
,
156 unsigned bprefixlen
) {
163 /* Checks whether there are any addresses that are in both
166 m
= MIN(aprefixlen
, bprefixlen
);
168 if (family
== AF_INET
) {
171 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
172 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
174 return (x
& nm
) == 0;
177 if (family
== AF_INET6
) {
183 for (i
= 0; i
< 16; i
++) {
186 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
189 nm
= 0xFF << (8 - m
);
205 return -EAFNOSUPPORT
;
208 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
211 /* Increases the network part of an address by one. Returns 0 if that succeeds, or -ERANGE if
214 return in_addr_prefix_nth(family
, u
, prefixlen
, 1);
218 * Calculates the nth prefix of size prefixlen starting from the address denoted by u.
220 * On success 0 will be returned and the calculated prefix will be available in
221 * u. In case the calculation cannot be performed (invalid prefix length,
222 * overflows would occur) -ERANGE is returned. If the address family given isn't
223 * supported -EAFNOSUPPORT will be returned.
226 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 0, writes 192.168.2.0 to u
227 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 0, no data written
228 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
229 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
230 * - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 0, writes 2001:0db8:0000:ff00:: to u
232 int in_addr_prefix_nth(int family
, union in_addr_union
*u
, unsigned prefixlen
, uint64_t nth
) {
238 if (family
== AF_INET
) {
244 c
= be32toh(u
->in
.s_addr
);
246 t
= nth
<< (32 - prefixlen
);
249 if (c
> UINT32_MAX
- t
)
254 n
&= UINT32_C(0xFFFFFFFF) << (32 - prefixlen
);
255 u
->in
.s_addr
= htobe32(n
);
259 if (family
== AF_INET6
) {
260 bool overflow
= false;
265 for (unsigned i
= 16; i
> 0; i
--) {
266 unsigned t
, j
= i
- 1, p
= j
* 8;
268 if (p
>= prefixlen
) {
269 u
->in6
.s6_addr
[j
] = 0;
273 if (prefixlen
- p
< 8) {
274 u
->in6
.s6_addr
[j
] &= 0xff << (8 - (prefixlen
- p
));
275 t
= u
->in6
.s6_addr
[j
] + ((nth
& 0xff) << (8 - (prefixlen
- p
)));
276 nth
>>= prefixlen
- p
;
278 t
= u
->in6
.s6_addr
[j
] + (nth
& 0xff) + overflow
;
282 overflow
= t
> UINT8_MAX
;
283 u
->in6
.s6_addr
[j
] = (uint8_t) (t
& 0xff);
286 if (overflow
|| nth
!= 0)
292 return -EAFNOSUPPORT
;
295 int in_addr_random_prefix(
297 union in_addr_union
*u
,
298 unsigned prefixlen_fixed_part
,
299 unsigned prefixlen
) {
303 /* Random network part of an address by one. */
308 if (family
== AF_INET
) {
311 if (prefixlen_fixed_part
> 32)
312 prefixlen_fixed_part
= 32;
315 if (prefixlen_fixed_part
>= prefixlen
)
318 c
= be32toh(u
->in
.s_addr
);
319 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
321 random_bytes(&n
, sizeof(n
));
322 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
324 u
->in
.s_addr
= htobe32(n
| c
);
328 if (family
== AF_INET6
) {
332 if (prefixlen_fixed_part
> 128)
333 prefixlen_fixed_part
= 128;
336 if (prefixlen_fixed_part
>= prefixlen
)
339 random_bytes(&n
, sizeof(n
));
341 for (i
= 0; i
< 16; i
++) {
342 uint8_t mask_fixed_part
= 0, mask
= 0;
344 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
345 if (i
< prefixlen_fixed_part
/ 8)
346 mask_fixed_part
= 0xffu
;
348 j
= prefixlen_fixed_part
% 8;
349 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
353 if (i
< (prefixlen
+ 7) / 8) {
354 if (i
< prefixlen
/ 8)
355 mask
= 0xffu
^ mask_fixed_part
;
358 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
362 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
363 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
369 return -EAFNOSUPPORT
;
372 int in_addr_prefix_range(
374 const union in_addr_union
*in
,
376 union in_addr_union
*ret_start
,
377 union in_addr_union
*ret_end
) {
379 union in_addr_union start
, end
;
384 if (!IN_SET(family
, AF_INET
, AF_INET6
))
385 return -EAFNOSUPPORT
;
389 r
= in_addr_prefix_nth(family
, &start
, prefixlen
, 0);
396 r
= in_addr_prefix_nth(family
, &end
, prefixlen
, 1);
409 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
410 _cleanup_free_
char *x
= NULL
;
416 if (family
== AF_INET
)
418 else if (family
== AF_INET6
)
419 l
= INET6_ADDRSTRLEN
;
421 return -EAFNOSUPPORT
;
428 if (!inet_ntop(family
, u
, x
, l
))
429 return errno_or_else(EINVAL
);
435 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
436 _cleanup_free_
char *x
= NULL
;
443 if (family
== AF_INET
)
444 l
= INET_ADDRSTRLEN
+ 3;
445 else if (family
== AF_INET6
)
446 l
= INET6_ADDRSTRLEN
+ 4;
448 return -EAFNOSUPPORT
;
450 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
458 if (!inet_ntop(family
, u
, x
, l
))
459 return errno_or_else(EINVAL
);
463 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
469 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
) {
470 _cleanup_free_
char *ip_str
= NULL
, *x
= NULL
;
473 assert(IN_SET(family
, AF_INET
, AF_INET6
));
477 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
478 * handle IPv6 link-local addresses. */
480 r
= in_addr_to_string(family
, u
, &ip_str
);
484 if (family
== AF_INET6
) {
485 r
= in_addr_is_link_local(family
, u
);
491 ifindex
= 0; /* For IPv4 address, ifindex is always ignored. */
493 if (port
== 0 && ifindex
== 0 && isempty(server_name
)) {
494 *ret
= TAKE_PTR(ip_str
);
498 const char *separator
= isempty(server_name
) ? "" : "#";
499 server_name
= strempty(server_name
);
502 if (family
== AF_INET6
) {
504 r
= asprintf(&x
, "[%s]:%"PRIu16
"%%%i%s%s", ip_str
, port
, ifindex
, separator
, server_name
);
506 r
= asprintf(&x
, "[%s]:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
508 r
= asprintf(&x
, "%s:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
511 r
= asprintf(&x
, "%s%%%i%s%s", ip_str
, ifindex
, separator
, server_name
);
513 x
= strjoin(ip_str
, separator
, server_name
);
524 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
525 union in_addr_union buffer
;
528 if (!IN_SET(family
, AF_INET
, AF_INET6
))
529 return -EAFNOSUPPORT
;
532 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
533 return errno_or_else(EINVAL
);
538 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
543 r
= in_addr_from_string(AF_INET
, s
, ret
);
546 *ret_family
= AF_INET
;
550 r
= in_addr_from_string(AF_INET6
, s
, ret
);
553 *ret_family
= AF_INET6
;
560 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
563 return 32U - u32ctz(be32toh(addr
->s_addr
));
566 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
568 assert(prefixlen
<= 32);
570 /* Shifting beyond 32 is not defined, handle this specially. */
574 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
579 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
580 uint8_t msb_octet
= *(uint8_t*) addr
;
582 /* addr may not be aligned, so make sure we only access it byte-wise */
588 /* class A, leading bits: 0 */
590 else if (msb_octet
< 192)
591 /* class B, leading bits 10 */
593 else if (msb_octet
< 224)
594 /* class C, leading bits 110 */
597 /* class D or E, no default prefixlen */
603 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
604 unsigned char prefixlen
;
610 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
614 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
618 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
621 if (family
== AF_INET
) {
624 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
627 addr
->in
.s_addr
&= mask
.s_addr
;
631 if (family
== AF_INET6
) {
634 for (i
= 0; i
< 16; i
++) {
637 if (prefixlen
>= 8) {
641 mask
= 0xFF << (8 - prefixlen
);
645 addr
->in6
.s6_addr
[i
] &= mask
;
651 return -EAFNOSUPPORT
;
654 int in_addr_prefix_covers(int family
,
655 const union in_addr_union
*prefix
,
656 unsigned char prefixlen
,
657 const union in_addr_union
*address
) {
659 union in_addr_union masked_prefix
, masked_address
;
665 masked_prefix
= *prefix
;
666 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
670 masked_address
= *address
;
671 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
675 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
678 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
682 if (!IN_SET(family
, AF_INET
, AF_INET6
))
683 return -EAFNOSUPPORT
;
685 r
= safe_atou8(p
, &u
);
689 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
696 int in_addr_prefix_from_string(
699 union in_addr_union
*ret_prefix
,
700 unsigned char *ret_prefixlen
) {
702 _cleanup_free_
char *str
= NULL
;
703 union in_addr_union buffer
;
710 if (!IN_SET(family
, AF_INET
, AF_INET6
))
711 return -EAFNOSUPPORT
;
715 str
= strndup(p
, e
- p
);
723 r
= in_addr_from_string(family
, l
, &buffer
);
728 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
732 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
735 *ret_prefix
= buffer
;
742 int in_addr_prefix_from_string_auto_internal(
744 InAddrPrefixLenMode mode
,
746 union in_addr_union
*ret_prefix
,
747 unsigned char *ret_prefixlen
) {
749 _cleanup_free_
char *str
= NULL
;
750 union in_addr_union buffer
;
759 str
= strndup(p
, e
- p
);
767 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
772 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
778 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
780 case PREFIXLEN_REFUSE
:
781 return -ENOANO
; /* To distinguish this error from others. */
782 case PREFIXLEN_LEGACY
:
783 if (family
== AF_INET
) {
784 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
791 assert_not_reached("Invalid prefixlen mode");
795 *ret_family
= family
;
797 *ret_prefix
= buffer
;
805 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
806 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
807 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
810 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
813 r
= CMP(x
->family
, y
->family
);
817 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
820 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);
822 void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
825 siphash24_compress(addr
, sizeof(*addr
), state
);
828 int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
829 return memcmp(a
, b
, sizeof(*a
));
832 DEFINE_HASH_OPS(in6_addr_hash_ops
, struct in6_addr
, in6_addr_hash_func
, in6_addr_compare_func
);