]>
git.proxmox.com Git - systemd.git/blob - src/network/networkd-address-pool.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "networkd-address-pool.h"
26 #include "string-util.h"
32 const union in_addr_union
*u
,
41 p
= new0(AddressPool
, 1);
47 p
->prefixlen
= prefixlen
;
50 LIST_PREPEND(address_pools
, m
->address_pools
, p
);
56 int address_pool_new_from_string(
63 union in_addr_union u
;
70 r
= in_addr_from_string(family
, p
, &u
);
74 return address_pool_new(m
, ret
, family
, &u
, prefixlen
);
77 void address_pool_free(AddressPool
*p
) {
83 LIST_REMOVE(address_pools
, p
->manager
->address_pools
, p
);
88 static bool address_pool_prefix_is_taken(
90 const union in_addr_union
*u
,
100 HASHMAP_FOREACH(l
, p
->manager
->links
, i
) {
104 /* Don't clash with assigned addresses */
105 SET_FOREACH(a
, l
->addresses
, j
) {
106 if (a
->family
!= p
->family
)
109 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
113 /* Don't clash with addresses already pulled from the pool, but not assigned yet */
114 LIST_FOREACH(addresses
, a
, l
->pool_addresses
) {
115 if (a
->family
!= p
->family
)
118 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
123 /* And don't clash with configured but un-assigned addresses either */
124 LIST_FOREACH(networks
, n
, p
->manager
->networks
) {
127 LIST_FOREACH(addresses
, a
, n
->static_addresses
) {
128 if (a
->family
!= p
->family
)
131 if (in_addr_prefix_intersect(p
->family
, u
, prefixlen
, &a
->in_addr
, a
->prefixlen
))
139 int address_pool_acquire(AddressPool
*p
, unsigned prefixlen
, union in_addr_union
*found
) {
140 union in_addr_union u
;
143 assert(prefixlen
> 0);
146 if (p
->prefixlen
> prefixlen
)
151 if (!address_pool_prefix_is_taken(p
, &u
, prefixlen
)) {
152 _cleanup_free_
char *s
= NULL
;
154 in_addr_to_string(p
->family
, &u
, &s
);
155 log_debug("Found range %s/%u", strna(s
), prefixlen
);
161 if (!in_addr_prefix_next(p
->family
, &u
, prefixlen
))
164 if (!in_addr_prefix_intersect(p
->family
, &p
->in_addr
, p
->prefixlen
, &u
, prefixlen
))