1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "in-addr-util.h"
14 #include "parse-util.h"
15 #include "random-util.h"
19 bool in4_addr_is_null(const struct in_addr
*a
) {
22 return a
->s_addr
== 0;
25 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
28 if (family
== AF_INET
)
29 return in4_addr_is_null(&u
->in
);
31 if (family
== AF_INET6
)
32 return IN6_IS_ADDR_UNSPECIFIED(&u
->in6
);
37 bool in4_addr_is_link_local(const struct in_addr
*a
) {
40 return (be32toh(a
->s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
43 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
46 if (family
== AF_INET
)
47 return in4_addr_is_link_local(&u
->in
);
49 if (family
== AF_INET6
)
50 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
55 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
58 if (family
== AF_INET
)
59 return IN_MULTICAST(be32toh(u
->in
.s_addr
));
61 if (family
== AF_INET6
)
62 return IN6_IS_ADDR_MULTICAST(&u
->in6
);
67 bool in4_addr_is_localhost(const struct in_addr
*a
) {
70 /* All of 127.x.x.x is localhost. */
71 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
74 bool in4_addr_is_non_local(const struct in_addr
*a
) {
75 /* Whether the address is not null and not localhost.
77 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
78 return !in4_addr_is_null(a
) &&
79 !in4_addr_is_localhost(a
);
82 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
85 if (family
== AF_INET
)
86 return in4_addr_is_localhost(&u
->in
);
88 if (family
== AF_INET6
)
89 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
94 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
98 if (family
== AF_INET
)
99 return a
->in
.s_addr
== b
->in
.s_addr
;
101 if (family
== AF_INET6
)
103 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
104 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
105 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
106 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
108 return -EAFNOSUPPORT
;
111 int in_addr_prefix_intersect(
113 const union in_addr_union
*a
,
115 const union in_addr_union
*b
,
116 unsigned bprefixlen
) {
123 /* Checks whether there are any addresses that are in both
126 m
= MIN(aprefixlen
, bprefixlen
);
128 if (family
== AF_INET
) {
131 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
132 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
134 return (x
& nm
) == 0;
137 if (family
== AF_INET6
) {
143 for (i
= 0; i
< 16; i
++) {
146 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
149 nm
= 0xFF << (8 - m
);
165 return -EAFNOSUPPORT
;
168 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
171 /* Increases the network part of an address by one. Returns
172 * positive it that succeeds, or 0 if this overflows. */
177 if (family
== AF_INET
) {
183 c
= be32toh(u
->in
.s_addr
);
184 n
= c
+ (1UL << (32 - prefixlen
));
187 n
&= 0xFFFFFFFFUL
<< (32 - prefixlen
);
189 u
->in
.s_addr
= htobe32(n
);
193 if (family
== AF_INET6
) {
194 struct in6_addr add
= {}, result
;
195 uint8_t overflow
= 0;
201 /* First calculate what we have to add */
202 add
.s6_addr
[(prefixlen
-1) / 8] = 1 << (7 - (prefixlen
-1) % 8);
204 for (i
= 16; i
> 0; i
--) {
207 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
] + add
.s6_addr
[j
] + overflow
;
208 overflow
= (result
.s6_addr
[j
] < u
->in6
.s6_addr
[j
]);
218 return -EAFNOSUPPORT
;
221 int in_addr_random_prefix(
223 union in_addr_union
*u
,
224 unsigned prefixlen_fixed_part
,
225 unsigned prefixlen
) {
229 /* Random network part of an address by one. */
234 if (family
== AF_INET
) {
237 if (prefixlen_fixed_part
> 32)
238 prefixlen_fixed_part
= 32;
241 if (prefixlen_fixed_part
>= prefixlen
)
244 c
= be32toh(u
->in
.s_addr
);
245 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
247 random_bytes(&n
, sizeof(n
));
248 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
250 u
->in
.s_addr
= htobe32(n
| c
);
254 if (family
== AF_INET6
) {
258 if (prefixlen_fixed_part
> 128)
259 prefixlen_fixed_part
= 128;
262 if (prefixlen_fixed_part
>= prefixlen
)
265 random_bytes(&n
, sizeof(n
));
267 for (i
= 0; i
< 16; i
++) {
268 uint8_t mask_fixed_part
= 0, mask
= 0;
270 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
271 if (i
< prefixlen_fixed_part
/ 8)
272 mask_fixed_part
= 0xffu
;
274 j
= prefixlen_fixed_part
% 8;
275 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
279 if (i
< (prefixlen
+ 7) / 8) {
280 if (i
< prefixlen
/ 8)
281 mask
= 0xffu
^ mask_fixed_part
;
284 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
288 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
289 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
295 return -EAFNOSUPPORT
;
298 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
299 _cleanup_free_
char *x
= NULL
;
305 if (family
== AF_INET
)
307 else if (family
== AF_INET6
)
308 l
= INET6_ADDRSTRLEN
;
310 return -EAFNOSUPPORT
;
317 if (!inet_ntop(family
, u
, x
, l
))
318 return errno
> 0 ? -errno
: -EINVAL
;
324 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
325 _cleanup_free_
char *x
= NULL
;
332 if (family
== AF_INET
)
333 l
= INET_ADDRSTRLEN
+ 3;
334 else if (family
== AF_INET6
)
335 l
= INET6_ADDRSTRLEN
+ 4;
337 return -EAFNOSUPPORT
;
339 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
347 if (!inet_ntop(family
, u
, x
, l
))
348 return errno
> 0 ? -errno
: -EINVAL
;
352 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
358 int in_addr_ifindex_to_string(int family
, const union in_addr_union
*u
, int ifindex
, char **ret
) {
359 _cleanup_free_
char *x
= NULL
;
366 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
367 * handle IPv6 link-local addresses. */
369 if (family
!= AF_INET6
)
374 r
= in_addr_is_link_local(family
, u
);
380 l
= INET6_ADDRSTRLEN
+ 1 + DECIMAL_STR_MAX(ifindex
) + 1;
386 if (!inet_ntop(family
, u
, x
, l
))
387 return errno
> 0 ? -errno
: -EINVAL
;
389 sprintf(strchr(x
, 0), "%%%i", ifindex
);
395 return in_addr_to_string(family
, u
, ret
);
398 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
399 union in_addr_union buffer
;
402 if (!IN_SET(family
, AF_INET
, AF_INET6
))
403 return -EAFNOSUPPORT
;
406 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
407 return errno
> 0 ? -errno
: -EINVAL
;
412 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
417 r
= in_addr_from_string(AF_INET
, s
, ret
);
420 *ret_family
= AF_INET
;
424 r
= in_addr_from_string(AF_INET6
, s
, ret
);
427 *ret_family
= AF_INET6
;
434 int in_addr_ifindex_from_string_auto(const char *s
, int *family
, union in_addr_union
*ret
, int *ifindex
) {
435 _cleanup_free_
char *buf
= NULL
;
443 /* Similar to in_addr_from_string_auto() but also parses an optionally appended IPv6 zone suffix ("scope id")
444 * if one is found. */
446 suffix
= strchr(s
, '%');
450 /* If we shall return the interface index, try to parse it */
451 r
= parse_ifindex(suffix
+ 1, &ifi
);
455 u
= if_nametoindex(suffix
+ 1);
463 buf
= strndup(s
, suffix
- s
);
470 r
= in_addr_from_string_auto(s
, family
, ret
);
480 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
483 return 32U - u32ctz(be32toh(addr
->s_addr
));
486 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
488 assert(prefixlen
<= 32);
490 /* Shifting beyond 32 is not defined, handle this specially. */
494 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
499 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
500 uint8_t msb_octet
= *(uint8_t*) addr
;
502 /* addr may not be aligned, so make sure we only access it byte-wise */
508 /* class A, leading bits: 0 */
510 else if (msb_octet
< 192)
511 /* class B, leading bits 10 */
513 else if (msb_octet
< 224)
514 /* class C, leading bits 110 */
517 /* class D or E, no default prefixlen */
523 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
524 unsigned char prefixlen
;
530 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
534 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
538 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
541 if (family
== AF_INET
) {
544 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
547 addr
->in
.s_addr
&= mask
.s_addr
;
551 if (family
== AF_INET6
) {
554 for (i
= 0; i
< 16; i
++) {
557 if (prefixlen
>= 8) {
561 mask
= 0xFF << (8 - prefixlen
);
565 addr
->in6
.s6_addr
[i
] &= mask
;
571 return -EAFNOSUPPORT
;
574 int in_addr_prefix_covers(int family
,
575 const union in_addr_union
*prefix
,
576 unsigned char prefixlen
,
577 const union in_addr_union
*address
) {
579 union in_addr_union masked_prefix
, masked_address
;
585 masked_prefix
= *prefix
;
586 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
590 masked_address
= *address
;
591 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
595 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
598 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
602 if (!IN_SET(family
, AF_INET
, AF_INET6
))
603 return -EAFNOSUPPORT
;
605 r
= safe_atou8(p
, &u
);
609 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
616 int in_addr_prefix_from_string(
619 union in_addr_union
*ret_prefix
,
620 unsigned char *ret_prefixlen
) {
622 _cleanup_free_
char *str
= NULL
;
623 union in_addr_union buffer
;
630 if (!IN_SET(family
, AF_INET
, AF_INET6
))
631 return -EAFNOSUPPORT
;
635 str
= strndup(p
, e
- p
);
643 r
= in_addr_from_string(family
, l
, &buffer
);
648 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
652 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
655 *ret_prefix
= buffer
;
662 int in_addr_prefix_from_string_auto_internal(
664 InAddrPrefixLenMode mode
,
666 union in_addr_union
*ret_prefix
,
667 unsigned char *ret_prefixlen
) {
669 _cleanup_free_
char *str
= NULL
;
670 union in_addr_union buffer
;
679 str
= strndup(p
, e
- p
);
687 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
692 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
698 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
700 case PREFIXLEN_REFUSE
:
701 return -ENOANO
; /* To distinguish this error from others. */
702 case PREFIXLEN_LEGACY
:
703 if (family
== AF_INET
) {
704 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
711 assert_not_reached("Invalid prefixlen mode");
715 *ret_family
= family
;
717 *ret_prefix
= buffer
;
725 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
726 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
727 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
730 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
733 r
= CMP(x
->family
, y
->family
);
737 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
740 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);
742 static void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
745 siphash24_compress(addr
, sizeof(*addr
), state
);
748 static int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
749 return memcmp(a
, b
, sizeof(*a
));
752 DEFINE_HASH_OPS(in6_addr_hash_ops
, struct in6_addr
, in6_addr_hash_func
, in6_addr_compare_func
);