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