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 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
60 if (family
== AF_INET
)
61 return IN_MULTICAST(be32toh(u
->in
.s_addr
));
63 if (family
== AF_INET6
)
64 return IN6_IS_ADDR_MULTICAST(&u
->in6
);
69 bool in4_addr_is_localhost(const struct in_addr
*a
) {
72 /* All of 127.x.x.x is localhost. */
73 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
76 bool in4_addr_is_non_local(const struct in_addr
*a
) {
77 /* Whether the address is not null and not localhost.
79 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
80 return !in4_addr_is_null(a
) &&
81 !in4_addr_is_localhost(a
);
84 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
87 if (family
== AF_INET
)
88 return in4_addr_is_localhost(&u
->in
);
90 if (family
== AF_INET6
)
91 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
96 bool in4_addr_equal(const struct in_addr
*a
, const struct in_addr
*b
) {
100 return a
->s_addr
== b
->s_addr
;
103 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
107 if (family
== AF_INET
)
108 return in4_addr_equal(&a
->in
, &b
->in
);
110 if (family
== AF_INET6
)
112 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
113 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
114 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
115 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
117 return -EAFNOSUPPORT
;
120 int in_addr_prefix_intersect(
122 const union in_addr_union
*a
,
124 const union in_addr_union
*b
,
125 unsigned bprefixlen
) {
132 /* Checks whether there are any addresses that are in both
135 m
= MIN(aprefixlen
, bprefixlen
);
137 if (family
== AF_INET
) {
140 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
141 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
143 return (x
& nm
) == 0;
146 if (family
== AF_INET6
) {
152 for (i
= 0; i
< 16; i
++) {
155 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
158 nm
= 0xFF << (8 - m
);
174 return -EAFNOSUPPORT
;
177 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
180 /* Increases the network part of an address by one. Returns
181 * positive if that succeeds, or -ERANGE if this overflows. */
183 return in_addr_prefix_nth(family
, u
, prefixlen
, 1);
187 * Calculates the nth prefix of size prefixlen starting from the address denoted by u.
189 * On success 1 will be returned and the calculated prefix will be available in
190 * u. In the case nth == 0 the input will be left unchanged and 1 will be returned.
191 * In case the calculation cannot be performed (invalid prefix length,
192 * overflows would occur) -ERANGE is returned. If the address family given isn't
193 * supported -EAFNOSUPPORT will be returned.
197 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 1, writes 192.168.2.0 to u
198 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 1, no data written
199 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
200 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
201 * - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 1, writes 2001:0db8:0000:ff00:: to u
203 int in_addr_prefix_nth(int family
, union in_addr_union
*u
, unsigned prefixlen
, uint64_t nth
) {
212 if (family
== AF_INET
) {
217 c
= be32toh(u
->in
.s_addr
);
219 t
= nth
<< (32 - prefixlen
);
222 if (c
> UINT32_MAX
- t
)
227 n
&= UINT32_C(0xFFFFFFFF) << (32 - prefixlen
);
228 u
->in
.s_addr
= htobe32(n
);
232 if (family
== AF_INET6
) {
233 struct in6_addr result
= {};
234 uint8_t overflow
= 0;
235 uint64_t delta
; /* this assumes that we only ever have to up to 1<<64 subnets */
236 unsigned start_byte
= (prefixlen
- 1) / 8;
241 /* First calculate what we have to add */
242 delta
= nth
<< ((128 - prefixlen
) % 8);
244 for (unsigned i
= 16; i
> 0; i
--) {
248 if (j
<= start_byte
) {
254 t
= u
->in6
.s6_addr
[j
] + d
+ overflow
;
255 overflow
= t
> UINT8_MAX
? t
- UINT8_MAX
: 0;
257 result
.s6_addr
[j
] = (uint8_t)t
;
259 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
];
262 if (overflow
|| delta
!= 0)
269 return -EAFNOSUPPORT
;
272 int in_addr_random_prefix(
274 union in_addr_union
*u
,
275 unsigned prefixlen_fixed_part
,
276 unsigned prefixlen
) {
280 /* Random network part of an address by one. */
285 if (family
== AF_INET
) {
288 if (prefixlen_fixed_part
> 32)
289 prefixlen_fixed_part
= 32;
292 if (prefixlen_fixed_part
>= prefixlen
)
295 c
= be32toh(u
->in
.s_addr
);
296 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
298 random_bytes(&n
, sizeof(n
));
299 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
301 u
->in
.s_addr
= htobe32(n
| c
);
305 if (family
== AF_INET6
) {
309 if (prefixlen_fixed_part
> 128)
310 prefixlen_fixed_part
= 128;
313 if (prefixlen_fixed_part
>= prefixlen
)
316 random_bytes(&n
, sizeof(n
));
318 for (i
= 0; i
< 16; i
++) {
319 uint8_t mask_fixed_part
= 0, mask
= 0;
321 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
322 if (i
< prefixlen_fixed_part
/ 8)
323 mask_fixed_part
= 0xffu
;
325 j
= prefixlen_fixed_part
% 8;
326 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
330 if (i
< (prefixlen
+ 7) / 8) {
331 if (i
< prefixlen
/ 8)
332 mask
= 0xffu
^ mask_fixed_part
;
335 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
339 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
340 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
346 return -EAFNOSUPPORT
;
349 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
350 _cleanup_free_
char *x
= NULL
;
356 if (family
== AF_INET
)
358 else if (family
== AF_INET6
)
359 l
= INET6_ADDRSTRLEN
;
361 return -EAFNOSUPPORT
;
368 if (!inet_ntop(family
, u
, x
, l
))
369 return errno_or_else(EINVAL
);
375 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
376 _cleanup_free_
char *x
= NULL
;
383 if (family
== AF_INET
)
384 l
= INET_ADDRSTRLEN
+ 3;
385 else if (family
== AF_INET6
)
386 l
= INET6_ADDRSTRLEN
+ 4;
388 return -EAFNOSUPPORT
;
390 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
398 if (!inet_ntop(family
, u
, x
, l
))
399 return errno_or_else(EINVAL
);
403 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
409 int in_addr_ifindex_to_string(int family
, const union in_addr_union
*u
, int ifindex
, char **ret
) {
410 _cleanup_free_
char *x
= NULL
;
417 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
418 * handle IPv6 link-local addresses. */
420 if (family
!= AF_INET6
)
425 r
= in_addr_is_link_local(family
, u
);
431 l
= INET6_ADDRSTRLEN
+ 1 + DECIMAL_STR_MAX(ifindex
) + 1;
437 if (!inet_ntop(family
, u
, x
, l
))
438 return errno_or_else(EINVAL
);
440 sprintf(strchr(x
, 0), "%%%i", ifindex
);
446 return in_addr_to_string(family
, u
, ret
);
449 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
) {
450 _cleanup_free_
char *ip_str
= NULL
, *x
= NULL
;
453 assert(IN_SET(family
, AF_INET
, AF_INET6
));
457 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
458 * handle IPv6 link-local addresses. */
460 r
= in_addr_to_string(family
, u
, &ip_str
);
464 if (family
== AF_INET6
) {
465 r
= in_addr_is_link_local(family
, u
);
471 ifindex
= 0; /* For IPv4 address, ifindex is always ignored. */
473 if (port
== 0 && ifindex
== 0 && isempty(server_name
)) {
474 *ret
= TAKE_PTR(ip_str
);
478 const char *separator
= isempty(server_name
) ? "" : "#";
479 server_name
= strempty(server_name
);
482 if (family
== AF_INET6
) {
484 r
= asprintf(&x
, "[%s]:%"PRIu16
"%%%i%s%s", ip_str
, port
, ifindex
, separator
, server_name
);
486 r
= asprintf(&x
, "[%s]:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
488 r
= asprintf(&x
, "%s:%"PRIu16
"%s%s", ip_str
, port
, separator
, server_name
);
491 r
= asprintf(&x
, "%s%%%i%s%s", ip_str
, ifindex
, separator
, server_name
);
493 x
= strjoin(ip_str
, separator
, server_name
);
504 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
505 union in_addr_union buffer
;
508 if (!IN_SET(family
, AF_INET
, AF_INET6
))
509 return -EAFNOSUPPORT
;
512 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
513 return errno_or_else(EINVAL
);
518 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
523 r
= in_addr_from_string(AF_INET
, s
, ret
);
526 *ret_family
= AF_INET
;
530 r
= in_addr_from_string(AF_INET6
, s
, ret
);
533 *ret_family
= AF_INET6
;
540 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
543 return 32U - u32ctz(be32toh(addr
->s_addr
));
546 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
548 assert(prefixlen
<= 32);
550 /* Shifting beyond 32 is not defined, handle this specially. */
554 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
559 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
560 uint8_t msb_octet
= *(uint8_t*) addr
;
562 /* addr may not be aligned, so make sure we only access it byte-wise */
568 /* class A, leading bits: 0 */
570 else if (msb_octet
< 192)
571 /* class B, leading bits 10 */
573 else if (msb_octet
< 224)
574 /* class C, leading bits 110 */
577 /* class D or E, no default prefixlen */
583 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
584 unsigned char prefixlen
;
590 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
594 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
598 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
601 if (family
== AF_INET
) {
604 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
607 addr
->in
.s_addr
&= mask
.s_addr
;
611 if (family
== AF_INET6
) {
614 for (i
= 0; i
< 16; i
++) {
617 if (prefixlen
>= 8) {
621 mask
= 0xFF << (8 - prefixlen
);
625 addr
->in6
.s6_addr
[i
] &= mask
;
631 return -EAFNOSUPPORT
;
634 int in_addr_prefix_covers(int family
,
635 const union in_addr_union
*prefix
,
636 unsigned char prefixlen
,
637 const union in_addr_union
*address
) {
639 union in_addr_union masked_prefix
, masked_address
;
645 masked_prefix
= *prefix
;
646 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
650 masked_address
= *address
;
651 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
655 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
658 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
662 if (!IN_SET(family
, AF_INET
, AF_INET6
))
663 return -EAFNOSUPPORT
;
665 r
= safe_atou8(p
, &u
);
669 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
676 int in_addr_prefix_from_string(
679 union in_addr_union
*ret_prefix
,
680 unsigned char *ret_prefixlen
) {
682 _cleanup_free_
char *str
= NULL
;
683 union in_addr_union buffer
;
690 if (!IN_SET(family
, AF_INET
, AF_INET6
))
691 return -EAFNOSUPPORT
;
695 str
= strndup(p
, e
- p
);
703 r
= in_addr_from_string(family
, l
, &buffer
);
708 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
712 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
715 *ret_prefix
= buffer
;
722 int in_addr_prefix_from_string_auto_internal(
724 InAddrPrefixLenMode mode
,
726 union in_addr_union
*ret_prefix
,
727 unsigned char *ret_prefixlen
) {
729 _cleanup_free_
char *str
= NULL
;
730 union in_addr_union buffer
;
739 str
= strndup(p
, e
- p
);
747 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
752 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
758 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
760 case PREFIXLEN_REFUSE
:
761 return -ENOANO
; /* To distinguish this error from others. */
762 case PREFIXLEN_LEGACY
:
763 if (family
== AF_INET
) {
764 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
771 assert_not_reached("Invalid prefixlen mode");
775 *ret_family
= family
;
777 *ret_prefix
= buffer
;
785 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
786 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
787 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
790 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
793 r
= CMP(x
->family
, y
->family
);
797 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
800 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);
802 static void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
805 siphash24_compress(addr
, sizeof(*addr
), state
);
808 static int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
809 return memcmp(a
, b
, sizeof(*a
));
812 DEFINE_HASH_OPS(in6_addr_hash_ops
, struct in6_addr
, in6_addr_hash_func
, in6_addr_compare_func
);