]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address-pool.c
tree-wide: remove Lennart's copyright lines
[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 int address_pool_new(
10 Manager *m,
11 AddressPool **ret,
12 int family,
13 const union in_addr_union *u,
14 unsigned prefixlen) {
15
16 AddressPool *p;
17
18 assert(m);
19 assert(ret);
20 assert(u);
21
22 p = new0(AddressPool, 1);
23 if (!p)
24 return -ENOMEM;
25
26 p->manager = m;
27 p->family = family;
28 p->prefixlen = prefixlen;
29 p->in_addr = *u;
30
31 LIST_PREPEND(address_pools, m->address_pools, p);
32
33 *ret = p;
34 return 0;
35 }
36
37 int address_pool_new_from_string(
38 Manager *m,
39 AddressPool **ret,
40 int family,
41 const char *p,
42 unsigned prefixlen) {
43
44 union in_addr_union u;
45 int r;
46
47 assert(m);
48 assert(ret);
49 assert(p);
50
51 r = in_addr_from_string(family, p, &u);
52 if (r < 0)
53 return r;
54
55 return address_pool_new(m, ret, family, &u, prefixlen);
56 }
57
58 void address_pool_free(AddressPool *p) {
59
60 if (!p)
61 return;
62
63 if (p->manager)
64 LIST_REMOVE(address_pools, p->manager->address_pools, p);
65
66 free(p);
67 }
68
69 static bool address_pool_prefix_is_taken(
70 AddressPool *p,
71 const union in_addr_union *u,
72 unsigned prefixlen) {
73
74 Iterator i;
75 Link *l;
76 Network *n;
77
78 assert(p);
79 assert(u);
80
81 HASHMAP_FOREACH(l, p->manager->links, i) {
82 Address *a;
83 Iterator j;
84
85 /* Don't clash with assigned addresses */
86 SET_FOREACH(a, l->addresses, j) {
87 if (a->family != p->family)
88 continue;
89
90 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
91 return true;
92 }
93
94 /* Don't clash with addresses already pulled from the pool, but not assigned yet */
95 LIST_FOREACH(addresses, a, l->pool_addresses) {
96 if (a->family != p->family)
97 continue;
98
99 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
100 return true;
101 }
102 }
103
104 /* And don't clash with configured but un-assigned addresses either */
105 LIST_FOREACH(networks, n, p->manager->networks) {
106 Address *a;
107
108 LIST_FOREACH(addresses, a, n->static_addresses) {
109 if (a->family != p->family)
110 continue;
111
112 if (in_addr_prefix_intersect(p->family, u, prefixlen, &a->in_addr, a->prefixlen))
113 return true;
114 }
115 }
116
117 return false;
118 }
119
120 int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found) {
121 union in_addr_union u;
122
123 assert(p);
124 assert(prefixlen > 0);
125 assert(found);
126
127 if (p->prefixlen > prefixlen)
128 return 0;
129
130 u = p->in_addr;
131 for (;;) {
132 if (!address_pool_prefix_is_taken(p, &u, prefixlen)) {
133 _cleanup_free_ char *s = NULL;
134 int r;
135
136 r = in_addr_to_string(p->family, &u, &s);
137 if (r < 0)
138 return r;
139
140 log_debug("Found range %s/%u", strna(s), prefixlen);
141
142 *found = u;
143 return 1;
144 }
145
146 if (!in_addr_prefix_next(p->family, &u, prefixlen))
147 return 0;
148
149 if (!in_addr_prefix_intersect(p->family, &p->in_addr, p->prefixlen, &u, prefixlen))
150 return 0;
151 }
152
153 return 0;
154 }