]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address-pool.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "networkd-address-pool.h"
5 #include "networkd-manager.h"
7 #include "string-util.h"
13 const union in_addr_union
*u
,
22 p
= new(AddressPool
, 1);
29 .prefixlen
= prefixlen
,
33 LIST_PREPEND(address_pools
, m
->address_pools
, p
);
39 int address_pool_new_from_string(
46 union in_addr_union u
;
53 r
= in_addr_from_string(family
, p
, &u
);
57 return address_pool_new(m
, ret
, family
, &u
, prefixlen
);
60 void address_pool_free(AddressPool
*p
) {
66 LIST_REMOVE(address_pools
, p
->manager
->address_pools
, p
);
71 static bool address_pool_prefix_is_taken(
73 const union in_addr_union
*u
,
83 HASHMAP_FOREACH(l
, p
->manager
->links
, i
) {
87 /* Don't clash with assigned addresses */
88 SET_FOREACH(a
, l
->addresses
, j
) {
89 if (a
->family
!= p
->family
)
92 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
96 /* Don't clash with addresses already pulled from the pool, but not assigned yet */
97 LIST_FOREACH(addresses
, a
, l
->pool_addresses
) {
98 if (a
->family
!= p
->family
)
101 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
106 /* And don't clash with configured but un-assigned addresses either */
107 LIST_FOREACH(networks
, n
, p
->manager
->networks
) {
110 LIST_FOREACH(addresses
, a
, n
->static_addresses
) {
111 if (a
->family
!= p
->family
)
114 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
122 int address_pool_acquire(AddressPool
*p
, unsigned prefixlen
, union in_addr_union
*found
) {
123 union in_addr_union u
;
126 assert(prefixlen
> 0);
129 if (p
->prefixlen
> prefixlen
)
134 if (!address_pool_prefix_is_taken(p
, &u
, prefixlen
)) {
135 _cleanup_free_
char *s
= NULL
;
138 r
= in_addr_to_string(p
->family
, &u
, &s
);
142 log_debug("Found range %s/%u", strna(s
), prefixlen
);
148 if (!in_addr_prefix_next(p
->family
, &u
, prefixlen
))
151 if (!in_addr_prefix_intersect(p
->family
, &p
->in_addr
, p
->prefixlen
, &u
, prefixlen
))