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 it that succeeds, or 0 if this overflows. */
185 if (family
== AF_INET
) {
191 c
= be32toh(u
->in
.s_addr
);
192 n
= c
+ (1UL << (32 - prefixlen
));
195 n
&= 0xFFFFFFFFUL
<< (32 - prefixlen
);
197 u
->in
.s_addr
= htobe32(n
);
201 if (family
== AF_INET6
) {
202 struct in6_addr add
= {}, result
;
203 uint8_t overflow
= 0;
209 /* First calculate what we have to add */
210 add
.s6_addr
[(prefixlen
-1) / 8] = 1 << (7 - (prefixlen
-1) % 8);
212 for (i
= 16; i
> 0; i
--) {
215 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
] + add
.s6_addr
[j
] + overflow
;
216 overflow
= (result
.s6_addr
[j
] < u
->in6
.s6_addr
[j
]);
226 return -EAFNOSUPPORT
;
229 int in_addr_random_prefix(
231 union in_addr_union
*u
,
232 unsigned prefixlen_fixed_part
,
233 unsigned prefixlen
) {
237 /* Random network part of an address by one. */
242 if (family
== AF_INET
) {
245 if (prefixlen_fixed_part
> 32)
246 prefixlen_fixed_part
= 32;
249 if (prefixlen_fixed_part
>= prefixlen
)
252 c
= be32toh(u
->in
.s_addr
);
253 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
255 random_bytes(&n
, sizeof(n
));
256 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
258 u
->in
.s_addr
= htobe32(n
| c
);
262 if (family
== AF_INET6
) {
266 if (prefixlen_fixed_part
> 128)
267 prefixlen_fixed_part
= 128;
270 if (prefixlen_fixed_part
>= prefixlen
)
273 random_bytes(&n
, sizeof(n
));
275 for (i
= 0; i
< 16; i
++) {
276 uint8_t mask_fixed_part
= 0, mask
= 0;
278 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
279 if (i
< prefixlen_fixed_part
/ 8)
280 mask_fixed_part
= 0xffu
;
282 j
= prefixlen_fixed_part
% 8;
283 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
287 if (i
< (prefixlen
+ 7) / 8) {
288 if (i
< prefixlen
/ 8)
289 mask
= 0xffu
^ mask_fixed_part
;
292 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
296 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
297 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
303 return -EAFNOSUPPORT
;
306 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
307 _cleanup_free_
char *x
= NULL
;
313 if (family
== AF_INET
)
315 else if (family
== AF_INET6
)
316 l
= INET6_ADDRSTRLEN
;
318 return -EAFNOSUPPORT
;
325 if (!inet_ntop(family
, u
, x
, l
))
326 return errno_or_else(EINVAL
);
332 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
333 _cleanup_free_
char *x
= NULL
;
340 if (family
== AF_INET
)
341 l
= INET_ADDRSTRLEN
+ 3;
342 else if (family
== AF_INET6
)
343 l
= INET6_ADDRSTRLEN
+ 4;
345 return -EAFNOSUPPORT
;
347 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
355 if (!inet_ntop(family
, u
, x
, l
))
356 return errno_or_else(EINVAL
);
360 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
366 int in_addr_ifindex_to_string(int family
, const union in_addr_union
*u
, int ifindex
, char **ret
) {
367 _cleanup_free_
char *x
= NULL
;
374 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
375 * handle IPv6 link-local addresses. */
377 if (family
!= AF_INET6
)
382 r
= in_addr_is_link_local(family
, u
);
388 l
= INET6_ADDRSTRLEN
+ 1 + DECIMAL_STR_MAX(ifindex
) + 1;
394 if (!inet_ntop(family
, u
, x
, l
))
395 return errno_or_else(EINVAL
);
397 sprintf(strchr(x
, 0), "%%%i", ifindex
);
403 return in_addr_to_string(family
, u
, ret
);
406 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
407 union in_addr_union buffer
;
410 if (!IN_SET(family
, AF_INET
, AF_INET6
))
411 return -EAFNOSUPPORT
;
414 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
415 return errno_or_else(EINVAL
);
420 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
425 r
= in_addr_from_string(AF_INET
, s
, ret
);
428 *ret_family
= AF_INET
;
432 r
= in_addr_from_string(AF_INET6
, s
, ret
);
435 *ret_family
= AF_INET6
;
442 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
445 return 32U - u32ctz(be32toh(addr
->s_addr
));
448 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
450 assert(prefixlen
<= 32);
452 /* Shifting beyond 32 is not defined, handle this specially. */
456 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
461 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
462 uint8_t msb_octet
= *(uint8_t*) addr
;
464 /* addr may not be aligned, so make sure we only access it byte-wise */
470 /* class A, leading bits: 0 */
472 else if (msb_octet
< 192)
473 /* class B, leading bits 10 */
475 else if (msb_octet
< 224)
476 /* class C, leading bits 110 */
479 /* class D or E, no default prefixlen */
485 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
486 unsigned char prefixlen
;
492 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
496 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
500 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
503 if (family
== AF_INET
) {
506 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
509 addr
->in
.s_addr
&= mask
.s_addr
;
513 if (family
== AF_INET6
) {
516 for (i
= 0; i
< 16; i
++) {
519 if (prefixlen
>= 8) {
523 mask
= 0xFF << (8 - prefixlen
);
527 addr
->in6
.s6_addr
[i
] &= mask
;
533 return -EAFNOSUPPORT
;
536 int in_addr_prefix_covers(int family
,
537 const union in_addr_union
*prefix
,
538 unsigned char prefixlen
,
539 const union in_addr_union
*address
) {
541 union in_addr_union masked_prefix
, masked_address
;
547 masked_prefix
= *prefix
;
548 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
552 masked_address
= *address
;
553 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
557 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
560 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
564 if (!IN_SET(family
, AF_INET
, AF_INET6
))
565 return -EAFNOSUPPORT
;
567 r
= safe_atou8(p
, &u
);
571 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
578 int in_addr_prefix_from_string(
581 union in_addr_union
*ret_prefix
,
582 unsigned char *ret_prefixlen
) {
584 _cleanup_free_
char *str
= NULL
;
585 union in_addr_union buffer
;
592 if (!IN_SET(family
, AF_INET
, AF_INET6
))
593 return -EAFNOSUPPORT
;
597 str
= strndup(p
, e
- p
);
605 r
= in_addr_from_string(family
, l
, &buffer
);
610 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
614 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
617 *ret_prefix
= buffer
;
624 int in_addr_prefix_from_string_auto_internal(
626 InAddrPrefixLenMode mode
,
628 union in_addr_union
*ret_prefix
,
629 unsigned char *ret_prefixlen
) {
631 _cleanup_free_
char *str
= NULL
;
632 union in_addr_union buffer
;
641 str
= strndup(p
, e
- p
);
649 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
654 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
660 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
662 case PREFIXLEN_REFUSE
:
663 return -ENOANO
; /* To distinguish this error from others. */
664 case PREFIXLEN_LEGACY
:
665 if (family
== AF_INET
) {
666 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
673 assert_not_reached("Invalid prefixlen mode");
677 *ret_family
= family
;
679 *ret_prefix
= buffer
;
687 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
688 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
689 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
692 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
695 r
= CMP(x
->family
, y
->family
);
699 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
702 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);
704 static void in6_addr_hash_func(const struct in6_addr
*addr
, struct siphash
*state
) {
707 siphash24_compress(addr
, sizeof(*addr
), state
);
710 static int in6_addr_compare_func(const struct in6_addr
*a
, const struct in6_addr
*b
) {
711 return memcmp(a
, b
, sizeof(*a
));
714 DEFINE_HASH_OPS(in6_addr_hash_ops
, struct in6_addr
, in6_addr_hash_func
, in6_addr_compare_func
);