1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "in-addr-util.h"
13 #include "parse-util.h"
14 #include "random-util.h"
18 bool in4_addr_is_null(const struct in_addr
*a
) {
21 return a
->s_addr
== 0;
24 int in_addr_is_null(int family
, const union in_addr_union
*u
) {
27 if (family
== AF_INET
)
28 return in4_addr_is_null(&u
->in
);
30 if (family
== AF_INET6
)
31 return IN6_IS_ADDR_UNSPECIFIED(&u
->in6
);
36 bool in4_addr_is_link_local(const struct in_addr
*a
) {
39 return (be32toh(a
->s_addr
) & UINT32_C(0xFFFF0000)) == (UINT32_C(169) << 24 | UINT32_C(254) << 16);
42 int in_addr_is_link_local(int family
, const union in_addr_union
*u
) {
45 if (family
== AF_INET
)
46 return in4_addr_is_link_local(&u
->in
);
48 if (family
== AF_INET6
)
49 return IN6_IS_ADDR_LINKLOCAL(&u
->in6
);
54 int in_addr_is_multicast(int family
, const union in_addr_union
*u
) {
57 if (family
== AF_INET
)
58 return IN_MULTICAST(be32toh(u
->in
.s_addr
));
60 if (family
== AF_INET6
)
61 return IN6_IS_ADDR_MULTICAST(&u
->in6
);
66 bool in4_addr_is_localhost(const struct in_addr
*a
) {
69 /* All of 127.x.x.x is localhost. */
70 return (be32toh(a
->s_addr
) & UINT32_C(0xFF000000)) == UINT32_C(127) << 24;
73 bool in4_addr_is_non_local(const struct in_addr
*a
) {
74 /* Whether the address is not null and not localhost.
76 * As such, it is suitable to configure as DNS/NTP server from DHCP. */
77 return !in4_addr_is_null(a
) &&
78 !in4_addr_is_localhost(a
);
81 int in_addr_is_localhost(int family
, const union in_addr_union
*u
) {
84 if (family
== AF_INET
)
85 return in4_addr_is_localhost(&u
->in
);
87 if (family
== AF_INET6
)
88 return IN6_IS_ADDR_LOOPBACK(&u
->in6
);
93 int in_addr_equal(int family
, const union in_addr_union
*a
, const union in_addr_union
*b
) {
97 if (family
== AF_INET
)
98 return a
->in
.s_addr
== b
->in
.s_addr
;
100 if (family
== AF_INET6
)
102 a
->in6
.s6_addr32
[0] == b
->in6
.s6_addr32
[0] &&
103 a
->in6
.s6_addr32
[1] == b
->in6
.s6_addr32
[1] &&
104 a
->in6
.s6_addr32
[2] == b
->in6
.s6_addr32
[2] &&
105 a
->in6
.s6_addr32
[3] == b
->in6
.s6_addr32
[3];
107 return -EAFNOSUPPORT
;
110 int in_addr_prefix_intersect(
112 const union in_addr_union
*a
,
114 const union in_addr_union
*b
,
115 unsigned bprefixlen
) {
122 /* Checks whether there are any addresses that are in both
125 m
= MIN(aprefixlen
, bprefixlen
);
127 if (family
== AF_INET
) {
130 x
= be32toh(a
->in
.s_addr
^ b
->in
.s_addr
);
131 nm
= (m
== 0) ? 0 : 0xFFFFFFFFUL
<< (32 - m
);
133 return (x
& nm
) == 0;
136 if (family
== AF_INET6
) {
142 for (i
= 0; i
< 16; i
++) {
145 x
= a
->in6
.s6_addr
[i
] ^ b
->in6
.s6_addr
[i
];
148 nm
= 0xFF << (8 - m
);
164 return -EAFNOSUPPORT
;
167 int in_addr_prefix_next(int family
, union in_addr_union
*u
, unsigned prefixlen
) {
170 /* Increases the network part of an address by one. Returns
171 * positive it that succeeds, or 0 if this overflows. */
176 if (family
== AF_INET
) {
182 c
= be32toh(u
->in
.s_addr
);
183 n
= c
+ (1UL << (32 - prefixlen
));
186 n
&= 0xFFFFFFFFUL
<< (32 - prefixlen
);
188 u
->in
.s_addr
= htobe32(n
);
192 if (family
== AF_INET6
) {
193 struct in6_addr add
= {}, result
;
194 uint8_t overflow
= 0;
200 /* First calculate what we have to add */
201 add
.s6_addr
[(prefixlen
-1) / 8] = 1 << (7 - (prefixlen
-1) % 8);
203 for (i
= 16; i
> 0; i
--) {
206 result
.s6_addr
[j
] = u
->in6
.s6_addr
[j
] + add
.s6_addr
[j
] + overflow
;
207 overflow
= (result
.s6_addr
[j
] < u
->in6
.s6_addr
[j
]);
217 return -EAFNOSUPPORT
;
220 int in_addr_random_prefix(
222 union in_addr_union
*u
,
223 unsigned prefixlen_fixed_part
,
224 unsigned prefixlen
) {
228 /* Random network part of an address by one. */
233 if (family
== AF_INET
) {
236 if (prefixlen_fixed_part
> 32)
237 prefixlen_fixed_part
= 32;
240 if (prefixlen_fixed_part
>= prefixlen
)
243 c
= be32toh(u
->in
.s_addr
);
244 c
&= ((UINT32_C(1) << prefixlen_fixed_part
) - 1) << (32 - prefixlen_fixed_part
);
246 random_bytes(&n
, sizeof(n
));
247 n
&= ((UINT32_C(1) << (prefixlen
- prefixlen_fixed_part
)) - 1) << (32 - prefixlen
);
249 u
->in
.s_addr
= htobe32(n
| c
);
253 if (family
== AF_INET6
) {
257 if (prefixlen_fixed_part
> 128)
258 prefixlen_fixed_part
= 128;
261 if (prefixlen_fixed_part
>= prefixlen
)
264 random_bytes(&n
, sizeof(n
));
266 for (i
= 0; i
< 16; i
++) {
267 uint8_t mask_fixed_part
= 0, mask
= 0;
269 if (i
< (prefixlen_fixed_part
+ 7) / 8) {
270 if (i
< prefixlen_fixed_part
/ 8)
271 mask_fixed_part
= 0xffu
;
273 j
= prefixlen_fixed_part
% 8;
274 mask_fixed_part
= ((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
);
278 if (i
< (prefixlen
+ 7) / 8) {
279 if (i
< prefixlen
/ 8)
280 mask
= 0xffu
^ mask_fixed_part
;
283 mask
= (((UINT8_C(1) << (j
+ 1)) - 1) << (8 - j
)) ^ mask_fixed_part
;
287 u
->in6
.s6_addr
[i
] &= mask_fixed_part
;
288 u
->in6
.s6_addr
[i
] |= n
.s6_addr
[i
] & mask
;
294 return -EAFNOSUPPORT
;
297 int in_addr_to_string(int family
, const union in_addr_union
*u
, char **ret
) {
298 _cleanup_free_
char *x
= NULL
;
304 if (family
== AF_INET
)
306 else if (family
== AF_INET6
)
307 l
= INET6_ADDRSTRLEN
;
309 return -EAFNOSUPPORT
;
316 if (!inet_ntop(family
, u
, x
, l
))
317 return errno
> 0 ? -errno
: -EINVAL
;
323 int in_addr_prefix_to_string(int family
, const union in_addr_union
*u
, unsigned prefixlen
, char **ret
) {
324 _cleanup_free_
char *x
= NULL
;
331 if (family
== AF_INET
)
332 l
= INET_ADDRSTRLEN
+ 3;
333 else if (family
== AF_INET6
)
334 l
= INET6_ADDRSTRLEN
+ 4;
336 return -EAFNOSUPPORT
;
338 if (prefixlen
> FAMILY_ADDRESS_SIZE(family
) * 8)
346 if (!inet_ntop(family
, u
, x
, l
))
347 return errno
> 0 ? -errno
: -EINVAL
;
351 (void) strpcpyf(&p
, l
, "/%u", prefixlen
);
357 int in_addr_ifindex_to_string(int family
, const union in_addr_union
*u
, int ifindex
, char **ret
) {
358 _cleanup_free_
char *x
= NULL
;
365 /* Much like in_addr_to_string(), but optionally appends the zone interface index to the address, to properly
366 * handle IPv6 link-local addresses. */
368 if (family
!= AF_INET6
)
373 r
= in_addr_is_link_local(family
, u
);
379 l
= INET6_ADDRSTRLEN
+ 1 + DECIMAL_STR_MAX(ifindex
) + 1;
385 if (!inet_ntop(family
, u
, x
, l
))
386 return errno
> 0 ? -errno
: -EINVAL
;
388 sprintf(strchr(x
, 0), "%%%i", ifindex
);
394 return in_addr_to_string(family
, u
, ret
);
397 int in_addr_from_string(int family
, const char *s
, union in_addr_union
*ret
) {
398 union in_addr_union buffer
;
401 if (!IN_SET(family
, AF_INET
, AF_INET6
))
402 return -EAFNOSUPPORT
;
405 if (inet_pton(family
, s
, ret
?: &buffer
) <= 0)
406 return errno
> 0 ? -errno
: -EINVAL
;
411 int in_addr_from_string_auto(const char *s
, int *ret_family
, union in_addr_union
*ret
) {
416 r
= in_addr_from_string(AF_INET
, s
, ret
);
419 *ret_family
= AF_INET
;
423 r
= in_addr_from_string(AF_INET6
, s
, ret
);
426 *ret_family
= AF_INET6
;
433 int in_addr_ifindex_from_string_auto(const char *s
, int *family
, union in_addr_union
*ret
, int *ifindex
) {
434 _cleanup_free_
char *buf
= NULL
;
442 /* Similar to in_addr_from_string_auto() but also parses an optionally appended IPv6 zone suffix ("scope id")
443 * if one is found. */
445 suffix
= strchr(s
, '%');
449 /* If we shall return the interface index, try to parse it */
450 r
= parse_ifindex(suffix
+ 1, &ifi
);
454 u
= if_nametoindex(suffix
+ 1);
462 buf
= strndup(s
, suffix
- s
);
469 r
= in_addr_from_string_auto(s
, family
, ret
);
479 unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr
*addr
) {
482 return 32U - u32ctz(be32toh(addr
->s_addr
));
485 struct in_addr
* in4_addr_prefixlen_to_netmask(struct in_addr
*addr
, unsigned char prefixlen
) {
487 assert(prefixlen
<= 32);
489 /* Shifting beyond 32 is not defined, handle this specially. */
493 addr
->s_addr
= htobe32((0xffffffff << (32 - prefixlen
)) & 0xffffffff);
498 int in4_addr_default_prefixlen(const struct in_addr
*addr
, unsigned char *prefixlen
) {
499 uint8_t msb_octet
= *(uint8_t*) addr
;
501 /* addr may not be aligned, so make sure we only access it byte-wise */
507 /* class A, leading bits: 0 */
509 else if (msb_octet
< 192)
510 /* class B, leading bits 10 */
512 else if (msb_octet
< 224)
513 /* class C, leading bits 110 */
516 /* class D or E, no default prefixlen */
522 int in4_addr_default_subnet_mask(const struct in_addr
*addr
, struct in_addr
*mask
) {
523 unsigned char prefixlen
;
529 r
= in4_addr_default_prefixlen(addr
, &prefixlen
);
533 in4_addr_prefixlen_to_netmask(mask
, prefixlen
);
537 int in_addr_mask(int family
, union in_addr_union
*addr
, unsigned char prefixlen
) {
540 if (family
== AF_INET
) {
543 if (!in4_addr_prefixlen_to_netmask(&mask
, prefixlen
))
546 addr
->in
.s_addr
&= mask
.s_addr
;
550 if (family
== AF_INET6
) {
553 for (i
= 0; i
< 16; i
++) {
556 if (prefixlen
>= 8) {
560 mask
= 0xFF << (8 - prefixlen
);
564 addr
->in6
.s6_addr
[i
] &= mask
;
570 return -EAFNOSUPPORT
;
573 int in_addr_prefix_covers(int family
,
574 const union in_addr_union
*prefix
,
575 unsigned char prefixlen
,
576 const union in_addr_union
*address
) {
578 union in_addr_union masked_prefix
, masked_address
;
584 masked_prefix
= *prefix
;
585 r
= in_addr_mask(family
, &masked_prefix
, prefixlen
);
589 masked_address
= *address
;
590 r
= in_addr_mask(family
, &masked_address
, prefixlen
);
594 return in_addr_equal(family
, &masked_prefix
, &masked_address
);
597 int in_addr_parse_prefixlen(int family
, const char *p
, unsigned char *ret
) {
601 if (!IN_SET(family
, AF_INET
, AF_INET6
))
602 return -EAFNOSUPPORT
;
604 r
= safe_atou8(p
, &u
);
608 if (u
> FAMILY_ADDRESS_SIZE(family
) * 8)
615 int in_addr_prefix_from_string(
618 union in_addr_union
*ret_prefix
,
619 unsigned char *ret_prefixlen
) {
621 _cleanup_free_
char *str
= NULL
;
622 union in_addr_union buffer
;
629 if (!IN_SET(family
, AF_INET
, AF_INET6
))
630 return -EAFNOSUPPORT
;
634 str
= strndup(p
, e
- p
);
642 r
= in_addr_from_string(family
, l
, &buffer
);
647 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
651 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
654 *ret_prefix
= buffer
;
661 int in_addr_prefix_from_string_auto_internal(
663 InAddrPrefixLenMode mode
,
665 union in_addr_union
*ret_prefix
,
666 unsigned char *ret_prefixlen
) {
668 _cleanup_free_
char *str
= NULL
;
669 union in_addr_union buffer
;
678 str
= strndup(p
, e
- p
);
686 r
= in_addr_from_string_auto(l
, &family
, &buffer
);
691 r
= in_addr_parse_prefixlen(family
, e
+1, &k
);
697 k
= FAMILY_ADDRESS_SIZE(family
) * 8;
699 case PREFIXLEN_REFUSE
:
700 return -ENOANO
; /* To distinguish this error from others. */
701 case PREFIXLEN_LEGACY
:
702 if (family
== AF_INET
) {
703 r
= in4_addr_default_prefixlen(&buffer
.in
, &k
);
710 assert_not_reached("Invalid prefixlen mode");
714 *ret_family
= family
;
716 *ret_prefix
= buffer
;
724 static void in_addr_data_hash_func(const struct in_addr_data
*a
, struct siphash
*state
) {
725 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
726 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
729 static int in_addr_data_compare_func(const struct in_addr_data
*x
, const struct in_addr_data
*y
) {
732 r
= CMP(x
->family
, y
->family
);
736 return memcmp(&x
->address
, &y
->address
, FAMILY_ADDRESS_SIZE(x
->family
));
739 DEFINE_HASH_OPS(in_addr_data_hash_ops
, struct in_addr_data
, in_addr_data_hash_func
, in_addr_data_compare_func
);