]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address-pool.c
Merge pull request #2719 from evverx/add-test-to-makefile
[thirdparty/systemd.git] / src / network / networkd-address-pool.c
CommitLineData
11bf3cce
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
b5efdb8a 20#include "alloc-util.h"
fc2f9534 21#include "networkd-address-pool.h"
07630cea 22#include "networkd.h"
cf1d700d 23#include "set.h"
07630cea 24#include "string-util.h"
11bf3cce
LP
25
26int address_pool_new(
27 Manager *m,
28 AddressPool **ret,
0dd25fb9 29 int family,
11bf3cce
LP
30 const union in_addr_union *u,
31 unsigned prefixlen) {
32
33 AddressPool *p;
34
35 assert(m);
36 assert(ret);
37 assert(u);
38
39 p = new0(AddressPool, 1);
40 if (!p)
41 return -ENOMEM;
42
43 p->manager = m;
44 p->family = family;
45 p->prefixlen = prefixlen;
46 p->in_addr = *u;
47
48 LIST_PREPEND(address_pools, m->address_pools, p);
49
50 *ret = p;
51 return 0;
52}
53
54int address_pool_new_from_string(
55 Manager *m,
56 AddressPool **ret,
0dd25fb9 57 int family,
11bf3cce
LP
58 const char *p,
59 unsigned prefixlen) {
60
61 union in_addr_union u;
62 int r;
63
64 assert(m);
65 assert(ret);
66 assert(p);
67
68 r = in_addr_from_string(family, p, &u);
69 if (r < 0)
70 return r;
71
72 return address_pool_new(m, ret, family, &u, prefixlen);
73}
74
75void address_pool_free(AddressPool *p) {
76
77 if (!p)
78 return;
79
80 if (p->manager)
81 LIST_REMOVE(address_pools, p->manager->address_pools, p);
82
83 free(p);
84}
85
86static bool address_pool_prefix_is_taken(
87 AddressPool *p,
88 const union in_addr_union *u,
89 unsigned prefixlen) {
90
91 Iterator i;
92 Link *l;
93 Network *n;
94
95 assert(p);
96 assert(u);
97
98 HASHMAP_FOREACH(l, p->manager->links, i) {
99 Address *a;
cf1d700d 100 Iterator j;
11bf3cce
LP
101
102 /* Don't clash with assigned addresses */
cf1d700d 103 SET_FOREACH(a, l->addresses, j) {
11bf3cce
LP
104 if (a->family != p->family)
105 continue;
106
107 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
108 return true;
109 }
110
111 /* Don't clash with addresses already pulled from the pool, but not assigned yet */
112 LIST_FOREACH(addresses, a, l->pool_addresses) {
113 if (a->family != p->family)
114 continue;
115
116 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
117 return true;
118 }
119 }
120
121 /* And don't clash with configured but un-assigned addresses either */
122 LIST_FOREACH(networks, n, p->manager->networks) {
123 Address *a;
124
125 LIST_FOREACH(addresses, a, n->static_addresses) {
126 if (a->family != p->family)
127 continue;
128
129 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
130 return true;
131 }
132 }
133
134 return false;
135}
136
137int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
138 union in_addr_union u;
139
140 assert(p);
141 assert(prefixlen > 0);
142 assert(found);
143
144 if (p->prefixlen > prefixlen)
145 return 0;
146
147 u = p->in_addr;
148 for (;;) {
149 if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
150 _cleanup_free_ char *s = NULL;
151
152 in_addr_to_string(p->family, &u, &s);
153 log_debug("Found range %s/%u", strna(s), prefixlen);
154
155 *found = u;
156 return 1;
157 }
158
159 if (!in_addr_prefix_next(p->family, &u, prefixlen))
160 return 0;
161
162 if (!in_addr_prefix_intersect(p->family, &p->in_addr, p->prefixlen, &u, prefixlen))
163 return 0;
164 }
165
166 return 0;
167}