]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address-pool.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "alloc-util.h"
22 #include "networkd-address-pool.h"
23 #include "networkd-manager.h"
25 #include "string-util.h"
31 const union in_addr_union
*u
,
40 p
= new0(AddressPool
, 1);
46 p
->prefixlen
= prefixlen
;
49 LIST_PREPEND(address_pools
, m
->address_pools
, p
);
55 int address_pool_new_from_string(
62 union in_addr_union u
;
69 r
= in_addr_from_string(family
, p
, &u
);
73 return address_pool_new(m
, ret
, family
, &u
, prefixlen
);
76 void address_pool_free(AddressPool
*p
) {
82 LIST_REMOVE(address_pools
, p
->manager
->address_pools
, p
);
87 static bool address_pool_prefix_is_taken(
89 const union in_addr_union
*u
,
99 HASHMAP_FOREACH(l
, p
->manager
->links
, i
) {
103 /* Don't clash with assigned addresses */
104 SET_FOREACH(a
, l
->addresses
, j
) {
105 if (a
->family
!= p
->family
)
108 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
112 /* Don't clash with addresses already pulled from the pool, but not assigned yet */
113 LIST_FOREACH(addresses
, a
, l
->pool_addresses
) {
114 if (a
->family
!= p
->family
)
117 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
122 /* And don't clash with configured but un-assigned addresses either */
123 LIST_FOREACH(networks
, n
, p
->manager
->networks
) {
126 LIST_FOREACH(addresses
, a
, n
->static_addresses
) {
127 if (a
->family
!= p
->family
)
130 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
138 int address_pool_acquire(AddressPool
*p
, unsigned prefixlen
, union in_addr_union
*found
) {
139 union in_addr_union u
;
142 assert(prefixlen
> 0);
145 if (p
->prefixlen
> prefixlen
)
150 if (!address_pool_prefix_is_taken(p
, &u
, prefixlen
)) {
151 _cleanup_free_
char *s
= NULL
;
154 r
= in_addr_to_string(p
->family
, &u
, &s
);
158 log_debug("Found range %s/%u", strna(s
), prefixlen
);
164 if (!in_addr_prefix_next(p
->family
, &u
, prefixlen
))
167 if (!in_addr_prefix_intersect(p
->family
, &p
->in_addr
, p
->prefixlen
, &u
, prefixlen
))