]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ipv4ll.c
Merge pull request #14269 from DaanDeMeyer/enable-mounts-on-root
[thirdparty/systemd.git] / src / libsystemd-network / sd-ipv4ll.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Axis Communications AB. All rights reserved.
4 ***/
5
6 #include <arpa/inet.h>
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "sd-id128.h"
12 #include "sd-ipv4acd.h"
13 #include "sd-ipv4ll.h"
14
15 #include "alloc-util.h"
16 #include "ether-addr-util.h"
17 #include "in-addr-util.h"
18 #include "list.h"
19 #include "random-util.h"
20 #include "siphash24.h"
21 #include "sparse-endian.h"
22 #include "string-util.h"
23 #include "util.h"
24
25 #define IPV4LL_NETWORK UINT32_C(0xA9FE0000)
26 #define IPV4LL_NETMASK UINT32_C(0xFFFF0000)
27
28 #define IPV4LL_DONT_DESTROY(ll) \
29 _cleanup_(sd_ipv4ll_unrefp) _unused_ sd_ipv4ll *_dont_destroy_##ll = sd_ipv4ll_ref(ll)
30
31 struct sd_ipv4ll {
32 unsigned n_ref;
33
34 sd_ipv4acd *acd;
35
36 be32_t address; /* the address pushed to ACD */
37 struct ether_addr mac;
38
39 struct {
40 le64_t value;
41 le64_t generation;
42 } seed;
43 bool seed_set;
44
45 /* External */
46 be32_t claimed_address;
47
48 sd_ipv4ll_callback_t callback;
49 void* userdata;
50 };
51
52 #define log_ipv4ll_errno(ll, error, fmt, ...) log_internal(LOG_DEBUG, error, PROJECT_FILE, __LINE__, __func__, "IPV4LL: " fmt, ##__VA_ARGS__)
53 #define log_ipv4ll(ll, fmt, ...) log_ipv4ll_errno(ll, 0, fmt, ##__VA_ARGS__)
54
55 static void ipv4ll_on_acd(sd_ipv4acd *ll, int event, void *userdata);
56
57 static sd_ipv4ll *ipv4ll_free(sd_ipv4ll *ll) {
58 assert(ll);
59
60 sd_ipv4acd_unref(ll->acd);
61 return mfree(ll);
62 }
63
64 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4ll, sd_ipv4ll, ipv4ll_free);
65
66 int sd_ipv4ll_new(sd_ipv4ll **ret) {
67 _cleanup_(sd_ipv4ll_unrefp) sd_ipv4ll *ll = NULL;
68 int r;
69
70 assert_return(ret, -EINVAL);
71
72 ll = new0(sd_ipv4ll, 1);
73 if (!ll)
74 return -ENOMEM;
75
76 ll->n_ref = 1;
77
78 r = sd_ipv4acd_new(&ll->acd);
79 if (r < 0)
80 return r;
81
82 r = sd_ipv4acd_set_callback(ll->acd, ipv4ll_on_acd, ll);
83 if (r < 0)
84 return r;
85
86 *ret = TAKE_PTR(ll);
87
88 return 0;
89 }
90
91 int sd_ipv4ll_stop(sd_ipv4ll *ll) {
92 assert_return(ll, -EINVAL);
93
94 return sd_ipv4acd_stop(ll->acd);
95 }
96
97 int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int ifindex) {
98 assert_return(ll, -EINVAL);
99 assert_return(ifindex > 0, -EINVAL);
100 assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
101
102 return sd_ipv4acd_set_ifindex(ll->acd, ifindex);
103 }
104
105 int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
106 int r;
107
108 assert_return(ll, -EINVAL);
109 assert_return(addr, -EINVAL);
110 assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
111
112 r = sd_ipv4acd_set_mac(ll->acd, addr);
113 if (r < 0)
114 return r;
115
116 ll->mac = *addr;
117 return 0;
118 }
119
120 int sd_ipv4ll_detach_event(sd_ipv4ll *ll) {
121 assert_return(ll, -EINVAL);
122
123 return sd_ipv4acd_detach_event(ll->acd);
124 }
125
126 int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int64_t priority) {
127 assert_return(ll, -EINVAL);
128
129 return sd_ipv4acd_attach_event(ll->acd, event, priority);
130 }
131
132 int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_callback_t cb, void *userdata) {
133 assert_return(ll, -EINVAL);
134
135 ll->callback = cb;
136 ll->userdata = userdata;
137
138 return 0;
139 }
140
141 int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address) {
142 assert_return(ll, -EINVAL);
143 assert_return(address, -EINVAL);
144
145 if (ll->claimed_address == 0)
146 return -ENOENT;
147
148 address->s_addr = ll->claimed_address;
149
150 return 0;
151 }
152
153 int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed) {
154 assert_return(ll, -EINVAL);
155 assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
156
157 ll->seed.value = htole64(seed);
158 ll->seed_set = true;
159
160 return 0;
161 }
162
163 int sd_ipv4ll_is_running(sd_ipv4ll *ll) {
164 assert_return(ll, false);
165
166 return sd_ipv4acd_is_running(ll->acd);
167 }
168
169 static bool ipv4ll_address_is_valid(const struct in_addr *address) {
170 assert(address);
171
172 if (!in_addr_is_link_local(AF_INET, (const union in_addr_union *) address))
173 return false;
174
175 return !IN_SET(be32toh(address->s_addr) & 0x0000FF00U, 0x0000U, 0xFF00U);
176 }
177
178 int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address) {
179 int r;
180
181 assert_return(ll, -EINVAL);
182 assert_return(address, -EINVAL);
183 assert_return(ipv4ll_address_is_valid(address), -EINVAL);
184
185 r = sd_ipv4acd_set_address(ll->acd, address);
186 if (r < 0)
187 return r;
188
189 ll->address = address->s_addr;
190
191 return 0;
192 }
193
194 #define PICK_HASH_KEY SD_ID128_MAKE(15,ac,82,a6,d6,3f,49,78,98,77,5d,0c,69,02,94,0b)
195
196 static int ipv4ll_pick_address(sd_ipv4ll *ll) {
197 _cleanup_free_ char *address = NULL;
198 be32_t addr;
199
200 assert(ll);
201
202 do {
203 uint64_t h;
204
205 h = siphash24(&ll->seed, sizeof(ll->seed), PICK_HASH_KEY.bytes);
206
207 /* Increase the generation counter by one */
208 ll->seed.generation = htole64(le64toh(ll->seed.generation) + 1);
209
210 addr = htobe32((h & UINT32_C(0x0000FFFF)) | IPV4LL_NETWORK);
211 } while (addr == ll->address ||
212 IN_SET(be32toh(addr) & 0x0000FF00U, 0x0000U, 0xFF00U));
213
214 (void) in_addr_to_string(AF_INET, &(union in_addr_union) { .in.s_addr = addr }, &address);
215 log_ipv4ll(ll, "Picked new IP address %s.", strna(address));
216
217 return sd_ipv4ll_set_address(ll, &(struct in_addr) { addr });
218 }
219
220 #define MAC_HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
221
222 static int ipv4ll_start_internal(sd_ipv4ll *ll, bool reset_generation) {
223 int r;
224 bool picked_address = false;
225
226 assert_return(ll, -EINVAL);
227 assert_return(!ether_addr_is_null(&ll->mac), -EINVAL);
228
229 /* If no random seed is set, generate some from the MAC address */
230 if (!ll->seed_set)
231 ll->seed.value = htole64(siphash24(ll->mac.ether_addr_octet, ETH_ALEN, MAC_HASH_KEY.bytes));
232
233 if (reset_generation)
234 ll->seed.generation = 0;
235
236 if (ll->address == 0) {
237 r = ipv4ll_pick_address(ll);
238 if (r < 0)
239 return r;
240
241 picked_address = true;
242 }
243
244 r = sd_ipv4acd_start(ll->acd, reset_generation);
245 if (r < 0) {
246
247 /* We couldn't start? If so, let's forget the picked address again, the user might make a change and
248 * retry, and we want the new data to take effect when picking an address. */
249 if (picked_address)
250 ll->address = 0;
251
252 return r;
253 }
254
255 return 0;
256 }
257
258 int sd_ipv4ll_start(sd_ipv4ll *ll) {
259 assert_return(ll, -EINVAL);
260 assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
261
262 return ipv4ll_start_internal(ll, true);
263 }
264
265 int sd_ipv4ll_restart(sd_ipv4ll *ll) {
266 ll->address = 0;
267
268 return ipv4ll_start_internal(ll, false);
269 }
270
271 static void ipv4ll_client_notify(sd_ipv4ll *ll, int event) {
272 assert(ll);
273
274 if (ll->callback)
275 ll->callback(ll, event, ll->userdata);
276 }
277
278 void ipv4ll_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
279 sd_ipv4ll *ll = userdata;
280 IPV4LL_DONT_DESTROY(ll);
281 int r;
282
283 assert(acd);
284 assert(ll);
285
286 switch (event) {
287
288 case SD_IPV4ACD_EVENT_STOP:
289 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP);
290 ll->claimed_address = 0;
291 break;
292
293 case SD_IPV4ACD_EVENT_BIND:
294 ll->claimed_address = ll->address;
295 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_BIND);
296 break;
297
298 case SD_IPV4ACD_EVENT_CONFLICT:
299 /* if an address was already bound we must call up to the
300 user to handle this, otherwise we just try again */
301 if (ll->claimed_address != 0) {
302 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_CONFLICT);
303
304 ll->claimed_address = 0;
305 } else {
306 r = sd_ipv4ll_restart(ll);
307 if (r < 0)
308 goto error;
309 }
310
311 break;
312
313 default:
314 assert_not_reached("Invalid IPv4ACD event.");
315 }
316
317 return;
318
319 error:
320 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP);
321 }