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"
20 bool in4_addr_is_null(const struct in_addr
*a
) {
23 return a
->s_addr
== 0;
26 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
29 if (family
== AF_INET
)
30 return in4_addr_is_null(&u
->in
);
32 if (family
== AF_INET6
)
33 return IN6_IS_ADDR_UNSPECIFIED(&u
->in6
);
38 bool in4_addr_is_link_local(const struct in_addr
*a
) {
41 return (be32toh(a
->s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
44 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
47 if (family
== AF_INET
)
48 return in4_addr_is_link_local(&u
->in
);
50 if (family
== AF_INET6
)
51 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
56 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
59 if (family
== AF_INET
)
60 return IN_MULTICAST(be32toh(u
->in
.s_addr
));
62 if (family
== AF_INET6
)
63 return IN6_IS_ADDR_MULTICAST(&u
->in6
);
68 bool in4_addr_is_localhost(const struct in_addr
*a
) {
71 /* All of 127.x.x.x is localhost. */
72 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
75 bool in4_addr_is_non_local(const struct in_addr
*a
) {
76 /* Whether the address is not null and not localhost.
78 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
79 return !in4_addr_is_null(a
) &&
80 !in4_addr_is_localhost(a
);
83 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
86 if (family
== AF_INET
)
87 return in4_addr_is_localhost(&u
->in
);
89 if (family
== AF_INET6
)
90 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
95 bool in4_addr_equal(const struct in_addr
*a
, const struct in_addr
*b
) {
99 return a
->s_addr
== b
->s_addr
;
102 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
106 if (family
== AF_INET
)
107 return in4_addr_equal(&a
->in
, &b
->in
);
109 if (family
== AF_INET6
)
111 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
112 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
113 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
114 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
116 return -EAFNOSUPPORT
;
119 int in_addr_prefix_intersect(
121 const union in_addr_union
*a
,
123 const union in_addr_union
*b
,
124 unsigned bprefixlen
) {
131 /* Checks whether there are any addresses that are in both
134 m
= MIN(aprefixlen
, bprefixlen
);
136 if (family
== AF_INET
) {
139 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
140 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
142 return (x
& nm
) == 0;
145 if (family
== AF_INET6
) {
151 for (i
= 0; i
< 16; i
++) {
154 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
157 nm
= 0xFF << (8 - m
);
173 return -EAFNOSUPPORT
;
176 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
179 /* Increases the network part of an address by one. Returns
180 * positive if that succeeds, or -ERANGE if this overflows. */
182 return in_addr_prefix_nth(family
, u
, prefixlen
, 1);
186 * Calculates the nth prefix of size prefixlen starting from the address denoted by u.
188 * On success 1 will be returned and the calculated prefix will be available in
189 * u. In the case nth == 0 the input will be left unchanged and 1 will be returned.
190 * In case the calculation cannot be performed (invalid prefix length,
191 * overflows would occur) -ERANGE is returned. If the address family given isn't
192 * supported -EAFNOSUPPORT will be returned.
196 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 1, writes 192.168.2.0 to u
197 * - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 1, no data written
198 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
199 * - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
200 * - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 1, writes 2001:0db8:0000:ff00:: to u
202 int in_addr_prefix_nth(int family
, union in_addr_union
*u
, unsigned prefixlen
, uint64_t nth
) {
211 if (family
== AF_INET
) {
216 c
= be32toh(u
->in
.s_addr
);
218 t
= nth
<< (32 - prefixlen
);
221 if (c
> UINT32_MAX
- t
)
226 n
&= UINT32_C(0xFFFFFFFF) << (32 - prefixlen
);
227 u
->in
.s_addr
= htobe32(n
);
231 if (family
== AF_INET6
) {
232 struct in6_addr result
= {};
233 uint8_t overflow
= 0;
234 uint64_t delta
; /* this assumes that we only ever have to up to 1<<64 subnets */
235 unsigned start_byte
= (prefixlen
- 1) / 8;
240 /* First calculate what we have to add */
241 delta
= nth
<< ((128 - prefixlen
) % 8);
243 for (unsigned i
= 16; i
> 0; i
--) {
247 if (j
<= start_byte
) {
253 t
= u
->in6
.s6_addr
[j
] + d
+ overflow
;
254 overflow
= t
> UINT8_MAX
? t
- UINT8_MAX
: 0;
256 result
.s6_addr
[j
] = (uint8_t)t
;
258 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
];
261 if (overflow
|| delta
!= 0)
268 return -EAFNOSUPPORT
;
271 int in_addr_random_prefix(
273 union in_addr_union
*u
,
274 unsigned prefixlen_fixed_part
,
275 unsigned prefixlen
) {
279 /* Random network part of an address by one. */
284 if (family
== AF_INET
) {
287 if (prefixlen_fixed_part
> 32)
288 prefixlen_fixed_part
= 32;
291 if (prefixlen_fixed_part
>= prefixlen
)
294 c
= be32toh(u
->in
.s_addr
);
295 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
297 random_bytes(&n
, sizeof(n
));
298 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
300 u
->in
.s_addr
= htobe32(n
| c
);
304 if (family
== AF_INET6
) {
308 if (prefixlen_fixed_part
> 128)
309 prefixlen_fixed_part
= 128;
312 if (prefixlen_fixed_part
>= prefixlen
)
315 random_bytes(&n
, sizeof(n
));
317 for (i
= 0; i
< 16; i
++) {
318 uint8_t mask_fixed_part
= 0, mask
= 0;
320 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
321 if (i
< prefixlen_fixed_part
/ 8)
322 mask_fixed_part
= 0xffu
;
324 j
= prefixlen_fixed_part
% 8;
325 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
329 if (i
< (prefixlen
+ 7) / 8) {
330 if (i
< prefixlen
/ 8)
331 mask
= 0xffu
^ mask_fixed_part
;
334 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
338 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
339 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
345 return -EAFNOSUPPORT
;
348 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
349 _cleanup_free_
char *x
= NULL
;
355 if (family
== AF_INET
)
357 else if (family
== AF_INET6
)
358 l
= INET6_ADDRSTRLEN
;
360 return -EAFNOSUPPORT
;
367 if (!inet_ntop(family
, u
, x
, l
))
368 return errno_or_else(EINVAL
);
374 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
375 _cleanup_free_
char *x
= NULL
;
382 if (family
== AF_INET
)
383 l
= INET_ADDRSTRLEN
+ 3;
384 else if (family
== AF_INET6
)
385 l
= INET6_ADDRSTRLEN
+ 4;
387 return -EAFNOSUPPORT
;
389 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
397 if (!inet_ntop(family
, u
, x
, l
))
398 return errno_or_else(EINVAL
);
402 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
408 int in_addr_ifindex_to_string(int family
, const union in_addr_union
*u
, int ifindex
, char **ret
) {
409 _cleanup_free_
char *x
= NULL
;
416 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
417 * handle IPv6 link-local addresses. */
419 if (family
!= AF_INET6
)
424 r
= in_addr_is_link_local(family
, u
);
430 l
= INET6_ADDRSTRLEN
+ 1 + DECIMAL_STR_MAX(ifindex
) + 1;
436 if (!inet_ntop(family
, u
, x
, l
))
437 return errno_or_else(EINVAL
);
439 sprintf(strchr(x
, 0), "%%%i", ifindex
);
445 return in_addr_to_string(family
, u
, ret
);
448 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
449 union in_addr_union buffer
;
452 if (!IN_SET(family
, AF_INET
, AF_INET6
))
453 return -EAFNOSUPPORT
;
456 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
457 return errno_or_else(EINVAL
);
462 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
467 r
= in_addr_from_string(AF_INET
, s
, ret
);
470 *ret_family
= AF_INET
;
474 r
= in_addr_from_string(AF_INET6
, s
, ret
);
477 *ret_family
= AF_INET6
;
484 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
487 return 32U - u32ctz(be32toh(addr
->s_addr
));
490 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
492 assert(prefixlen
<= 32);
494 /* Shifting beyond 32 is not defined, handle this specially. */
498 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
503 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
504 uint8_t msb_octet
= *(uint8_t*) addr
;
506 /* addr may not be aligned, so make sure we only access it byte-wise */
512 /* class A, leading bits: 0 */
514 else if (msb_octet
< 192)
515 /* class B, leading bits 10 */
517 else if (msb_octet
< 224)
518 /* class C, leading bits 110 */
521 /* class D or E, no default prefixlen */
527 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
528 unsigned char prefixlen
;
534 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
538 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
542 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
545 if (family
== AF_INET
) {
548 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
551 addr
->in
.s_addr
&= mask
.s_addr
;
555 if (family
== AF_INET6
) {
558 for (i
= 0; i
< 16; i
++) {
561 if (prefixlen
>= 8) {
565 mask
= 0xFF << (8 - prefixlen
);
569 addr
->in6
.s6_addr
[i
] &= mask
;
575 return -EAFNOSUPPORT
;
578 int in_addr_prefix_covers(int family
,
579 const union in_addr_union
*prefix
,
580 unsigned char prefixlen
,
581 const union in_addr_union
*address
) {
583 union in_addr_union masked_prefix
, masked_address
;
589 masked_prefix
= *prefix
;
590 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
594 masked_address
= *address
;
595 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
599 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
602 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
606 if (!IN_SET(family
, AF_INET
, AF_INET6
))
607 return -EAFNOSUPPORT
;
609 r
= safe_atou8(p
, &u
);
613 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
620 int in_addr_prefix_from_string(
623 union in_addr_union
*ret_prefix
,
624 unsigned char *ret_prefixlen
) {
626 _cleanup_free_
char *str
= NULL
;
627 union in_addr_union buffer
;
634 if (!IN_SET(family
, AF_INET
, AF_INET6
))
635 return -EAFNOSUPPORT
;
639 str
= strndup(p
, e
- p
);
647 r
= in_addr_from_string(family
, l
, &buffer
);
652 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
656 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
659 *ret_prefix
= buffer
;
666 int in_addr_prefix_from_string_auto_internal(
668 InAddrPrefixLenMode mode
,
670 union in_addr_union
*ret_prefix
,
671 unsigned char *ret_prefixlen
) {
673 _cleanup_free_
char *str
= NULL
;
674 union in_addr_union buffer
;
683 str
= strndup(p
, e
- p
);
691 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
696 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
702 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
704 case PREFIXLEN_REFUSE
:
705 return -ENOANO
; /* To distinguish this error from others. */
706 case PREFIXLEN_LEGACY
:
707 if (family
== AF_INET
) {
708 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
715 assert_not_reached("Invalid prefixlen mode");
719 *ret_family
= family
;
721 *ret_prefix
= buffer
;
729 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
730 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
731 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
734 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
737 r
= CMP(x
->family
, y
->family
);
741 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
744 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);
746 static void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
749 siphash24_compress(addr
, sizeof(*addr
), state
);
752 static int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
753 return memcmp(a
, b
, sizeof(*a
));
756 DEFINE_HASH_OPS(in6_addr_hash_ops
, struct in6_addr
, in6_addr_hash_func
, in6_addr_compare_func
);