]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address-pool.c
tree-wide: define iterator inside of the macro
[thirdparty/systemd.git] / src / network / networkd-address-pool.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
11bf3cce 2
b5efdb8a 3#include "alloc-util.h"
fc2f9534 4#include "networkd-address-pool.h"
23f53b99 5#include "networkd-manager.h"
cf1d700d 6#include "set.h"
07630cea 7#include "string-util.h"
11bf3cce 8
304e7e9d
YW
9#define RANDOM_PREFIX_TRIAL_MAX 1024
10
198afaab 11static int address_pool_new(
11bf3cce
LP
12 Manager *m,
13 AddressPool **ret,
0dd25fb9 14 int family,
11bf3cce
LP
15 const union in_addr_union *u,
16 unsigned prefixlen) {
17
18 AddressPool *p;
19
20 assert(m);
21 assert(ret);
22 assert(u);
23
17f9c355 24 p = new(AddressPool, 1);
11bf3cce
LP
25 if (!p)
26 return -ENOMEM;
27
17f9c355
YW
28 *p = (AddressPool) {
29 .manager = m,
30 .family = family,
31 .prefixlen = prefixlen,
32 .in_addr = *u,
33 };
11bf3cce
LP
34
35 LIST_PREPEND(address_pools, m->address_pools, p);
36
37 *ret = p;
38 return 0;
39}
40
41int address_pool_new_from_string(
42 Manager *m,
43 AddressPool **ret,
0dd25fb9 44 int family,
11bf3cce
LP
45 const char *p,
46 unsigned prefixlen) {
47
48 union in_addr_union u;
49 int r;
50
51 assert(m);
52 assert(ret);
53 assert(p);
54
55 r = in_addr_from_string(family, p, &u);
56 if (r < 0)
57 return r;
58
59 return address_pool_new(m, ret, family, &u, prefixlen);
60}
61
62void address_pool_free(AddressPool *p) {
63
64 if (!p)
65 return;
66
67 if (p->manager)
68 LIST_REMOVE(address_pools, p->manager->address_pools, p);
69
70 free(p);
71}
72
73static bool address_pool_prefix_is_taken(
74 AddressPool *p,
75 const union in_addr_union *u,
76 unsigned prefixlen) {
77
11bf3cce
LP
78 Link *l;
79 Network *n;
80
81 assert(p);
82 assert(u);
83
90e74a66 84 HASHMAP_FOREACH(l, p->manager->links) {
11bf3cce
LP
85 Address *a;
86
87 /* Don't clash with assigned addresses */
90e74a66 88 SET_FOREACH(a, l->addresses) {
11bf3cce
LP
89 if (a->family != p->family)
90 continue;
91
92 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
93 return true;
94 }
95
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)
99 continue;
100
101 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
102 return true;
103 }
104 }
105
106 /* And don't clash with configured but un-assigned addresses either */
90e74a66 107 ORDERED_HASHMAP_FOREACH(n, p->manager->networks) {
11bf3cce
LP
108 Address *a;
109
110 LIST_FOREACH(addresses, a, n->static_addresses) {
111 if (a->family != p->family)
112 continue;
113
114 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
115 return true;
116 }
117 }
118
119 return false;
120}
121
122int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
123 union in_addr_union u;
304e7e9d
YW
124 unsigned i;
125 int r;
11bf3cce
LP
126
127 assert(p);
128 assert(prefixlen > 0);
129 assert(found);
130
304e7e9d 131 if (p->prefixlen >= prefixlen)
11bf3cce
LP
132 return 0;
133
134 u = p->in_addr;
0229100b 135
304e7e9d
YW
136 for (i = 0; i < RANDOM_PREFIX_TRIAL_MAX; i++) {
137 r = in_addr_random_prefix(p->family, &u, p->prefixlen, prefixlen);
138 if (r <= 0)
139 return r;
11bf3cce 140
304e7e9d
YW
141 if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
142 if (DEBUG_LOGGING) {
143 _cleanup_free_ char *s = NULL;
144
145 (void) in_addr_to_string(p->family, &u, &s);
146 log_debug("Found range %s/%u", strna(s), prefixlen);
147 }
11bf3cce
LP
148
149 *found = u;
150 return 1;
151 }
11bf3cce
LP
152 }
153
154 return 0;
155}