]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address-pool.c
core: move reset_arguments() to the end of main's finish
[thirdparty/systemd.git] / src / network / networkd-address-pool.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "networkd-address-pool.h"
5 #include "networkd-manager.h"
6 #include "set.h"
7 #include "string-util.h"
8
9 #define RANDOM_PREFIX_TRIAL_MAX 1024
10
11 static int address_pool_new(
12 Manager *m,
13 AddressPool **ret,
14 int family,
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
24 p = new(AddressPool, 1);
25 if (!p)
26 return -ENOMEM;
27
28 *p = (AddressPool) {
29 .manager = m,
30 .family = family,
31 .prefixlen = prefixlen,
32 .in_addr = *u,
33 };
34
35 LIST_PREPEND(address_pools, m->address_pools, p);
36
37 *ret = p;
38 return 0;
39 }
40
41 int address_pool_new_from_string(
42 Manager *m,
43 AddressPool **ret,
44 int family,
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
62 void 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
73 static bool address_pool_prefix_is_taken(
74 AddressPool *p,
75 const union in_addr_union *u,
76 unsigned prefixlen) {
77
78 Link *l;
79 Network *n;
80
81 assert(p);
82 assert(u);
83
84 HASHMAP_FOREACH(l, p->manager->links) {
85 Address *a;
86
87 /* Don't clash with assigned addresses */
88 SET_FOREACH(a, l->addresses) {
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 */
107 ORDERED_HASHMAP_FOREACH(n, p->manager->networks) {
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
122 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
123 union in_addr_union u;
124 unsigned i;
125 int r;
126
127 assert(p);
128 assert(prefixlen > 0);
129 assert(found);
130
131 if (p->prefixlen >= prefixlen)
132 return 0;
133
134 u = p->in_addr;
135
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;
140
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 }
148
149 *found = u;
150 return 1;
151 }
152 }
153
154 return 0;
155 }