]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/sd-ipv4ll.c
update NEWS with even more features for v258
[thirdparty/systemd.git] / src / libsystemd-network / sd-ipv4ll.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
5c1d3fc9 2/***
810adae9 3 Copyright © 2014 Axis Communications AB. All rights reserved.
5c1d3fc9
UTL
4***/
5
dccca82b 6#include "sd-id128.h"
07630cea
LP
7#include "sd-ipv4acd.h"
8#include "sd-ipv4ll.h"
5c1d3fc9 9
b5efdb8a 10#include "alloc-util.h"
96a7979f 11#include "ether-addr-util.h"
129dc1b4 12#include "in-addr-util.h"
5977b71f 13#include "network-common.h"
e3dca008
TG
14#include "siphash24.h"
15#include "sparse-endian.h"
5c1d3fc9 16
96a7979f
LP
17#define IPV4LL_NETWORK UINT32_C(0xA9FE0000)
18#define IPV4LL_NETMASK UINT32_C(0xFFFF0000)
5c1d3fc9 19
b45e4eb6 20#define IPV4LL_DONT_DESTROY(ll) \
4afd3348 21 _cleanup_(sd_ipv4ll_unrefp) _unused_ sd_ipv4ll *_dont_destroy_##ll = sd_ipv4ll_ref(ll)
b45e4eb6 22
5c1d3fc9 23struct sd_ipv4ll {
9c8e3101 24 unsigned n_ref;
56cd007a 25
e3dca008 26 sd_ipv4acd *acd;
96a7979f 27
e3dca008 28 be32_t address; /* the address pushed to ACD */
96a7979f
LP
29 struct ether_addr mac;
30
31 struct {
32 le64_t value;
33 le64_t generation;
34 } seed;
35 bool seed_set;
e3dca008 36
5c1d3fc9
UTL
37 /* External */
38 be32_t claimed_address;
96a7979f 39
45aa74c7 40 sd_ipv4ll_callback_t callback;
1e59122d
YW
41 void *userdata;
42
43 sd_ipv4ll_check_mac_callback_t check_mac_callback;
44 void *check_mac_userdata;
5c1d3fc9
UTL
45};
46
a0c2541b 47#define log_ipv4ll_errno(ll, error, fmt, ...) \
00dd6d77 48 log_interface_prefix_full_errno( \
a0c2541b 49 "IPv4LL: ", \
5977b71f 50 sd_ipv4ll, ll, \
a0c2541b 51 error, fmt, ##__VA_ARGS__)
3f2c0d85 52#define log_ipv4ll(ll, fmt, ...) \
00dd6d77
ZJS
53 log_interface_prefix_full_errno_zerook( \
54 "IPv4LL: ", \
5977b71f 55 sd_ipv4ll, ll, \
00dd6d77 56 0, fmt, ##__VA_ARGS__)
703945c1 57
1e59122d
YW
58static void ipv4ll_on_acd(sd_ipv4acd *acd, int event, void *userdata);
59static int ipv4ll_check_mac(sd_ipv4acd *acd, const struct ether_addr *mac, void *userdata);
96a7979f 60
8301aa0b
YW
61static sd_ipv4ll *ipv4ll_free(sd_ipv4ll *ll) {
62 assert(ll);
b45e4eb6 63
e3dca008 64 sd_ipv4acd_unref(ll->acd);
6b430fdb 65 return mfree(ll);
b45e4eb6
TG
66}
67
8301aa0b
YW
68DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4ll, sd_ipv4ll, ipv4ll_free);
69
b45e4eb6 70int sd_ipv4ll_new(sd_ipv4ll **ret) {
4afd3348 71 _cleanup_(sd_ipv4ll_unrefp) sd_ipv4ll *ll = NULL;
e3dca008 72 int r;
b45e4eb6
TG
73
74 assert_return(ret, -EINVAL);
75
76 ll = new0(sd_ipv4ll, 1);
77 if (!ll)
78 return -ENOMEM;
79
0c28d288
LP
80 ll->n_ref = 1;
81
e3dca008
TG
82 r = sd_ipv4acd_new(&ll->acd);
83 if (r < 0)
84 return r;
85
86 r = sd_ipv4acd_set_callback(ll->acd, ipv4ll_on_acd, ll);
87 if (r < 0)
88 return r;
89
1e59122d
YW
90 r = sd_ipv4acd_set_check_mac_callback(ll->acd, ipv4ll_check_mac, ll);
91 if (r < 0)
92 return r;
93
1cc6c93a 94 *ret = TAKE_PTR(ll);
b45e4eb6
TG
95
96 return 0;
97}
98
b45e4eb6 99int sd_ipv4ll_stop(sd_ipv4ll *ll) {
c8bae363
YW
100 if (!ll)
101 return 0;
94a355a1 102
96a7979f 103 return sd_ipv4acd_stop(ll->acd);
5c1d3fc9
UTL
104}
105
2f8e7633 106int sd_ipv4ll_set_ifindex(sd_ipv4ll *ll, int ifindex) {
e3dca008 107 assert_return(ll, -EINVAL);
2f8e7633 108 assert_return(ifindex > 0, -EINVAL);
96a7979f 109 assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
5c1d3fc9 110
2f8e7633 111 return sd_ipv4acd_set_ifindex(ll->acd, ifindex);
5c1d3fc9
UTL
112}
113
99b06a2f
YW
114int sd_ipv4ll_get_ifindex(sd_ipv4ll *ll) {
115 if (!ll)
116 return -EINVAL;
117
118 return sd_ipv4acd_get_ifindex(ll->acd);
119}
120
61a9fa8f
YW
121int sd_ipv4ll_set_ifname(sd_ipv4ll *ll, const char *ifname) {
122 assert_return(ll, -EINVAL);
123 assert_return(ifname, -EINVAL);
124
125 return sd_ipv4acd_set_ifname(ll->acd, ifname);
126}
127
5977b71f
YW
128int sd_ipv4ll_get_ifname(sd_ipv4ll *ll, const char **ret) {
129 assert_return(ll, -EINVAL);
99b06a2f 130
5977b71f 131 return sd_ipv4acd_get_ifname(ll->acd, ret);
99b06a2f
YW
132}
133
e3dca008 134int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
b26f7e8e
TG
135 int r;
136
e3dca008 137 assert_return(ll, -EINVAL);
96a7979f 138 assert_return(addr, -EINVAL);
9e5aa34f 139 assert_return(!ether_addr_is_null(addr), -EINVAL);
b26f7e8e 140
96a7979f
LP
141 r = sd_ipv4acd_set_mac(ll->acd, addr);
142 if (r < 0)
143 return r;
5c1d3fc9 144
96a7979f
LP
145 ll->mac = *addr;
146 return 0;
5c1d3fc9
UTL
147}
148
c2691d8e
BG
149int sd_ipv4ll_set_timeout(sd_ipv4ll *ll, uint64_t usec) {
150 assert_return(ll, -EINVAL);
151
152 return sd_ipv4acd_set_timeout(ll->acd, usec);
153}
154
5c1d3fc9
UTL
155int sd_ipv4ll_detach_event(sd_ipv4ll *ll) {
156 assert_return(ll, -EINVAL);
157
e3dca008 158 return sd_ipv4acd_detach_event(ll->acd);
5c1d3fc9
UTL
159}
160
32d20645 161int sd_ipv4ll_attach_event(sd_ipv4ll *ll, sd_event *event, int64_t priority) {
5c1d3fc9 162 assert_return(ll, -EINVAL);
5c1d3fc9 163
73e94c0d 164 return sd_ipv4acd_attach_event(ll->acd, event, priority);
5c1d3fc9
UTL
165}
166
ccf86354 167int sd_ipv4ll_set_callback(sd_ipv4ll *ll, sd_ipv4ll_callback_t cb, void *userdata) {
5c1d3fc9
UTL
168 assert_return(ll, -EINVAL);
169
45aa74c7 170 ll->callback = cb;
5c1d3fc9
UTL
171 ll->userdata = userdata;
172
173 return 0;
174}
175
1e59122d
YW
176int sd_ipv4ll_set_check_mac_callback(sd_ipv4ll *ll, sd_ipv4ll_check_mac_callback_t cb, void *userdata) {
177 assert_return(ll, -EINVAL);
178
179 ll->check_mac_callback = cb;
180 ll->check_mac_userdata = userdata;
181
182 return 0;
183}
184
9ed794a3 185int sd_ipv4ll_get_address(sd_ipv4ll *ll, struct in_addr *address) {
5c1d3fc9
UTL
186 assert_return(ll, -EINVAL);
187 assert_return(address, -EINVAL);
188
ece174c5 189 if (ll->claimed_address == 0)
5c1d3fc9 190 return -ENOENT;
5c1d3fc9
UTL
191
192 address->s_addr = ll->claimed_address;
e3dca008 193
5c1d3fc9
UTL
194 return 0;
195}
196
38958cd6 197int sd_ipv4ll_set_address_seed(sd_ipv4ll *ll, uint64_t seed) {
b5db00e5 198 assert_return(ll, -EINVAL);
96a7979f 199 assert_return(sd_ipv4ll_is_running(ll) == 0, -EBUSY);
d9bf4f8c 200
96a7979f
LP
201 ll->seed.value = htole64(seed);
202 ll->seed_set = true;
b5db00e5 203
e3dca008 204 return 0;
b5db00e5
UTL
205}
206
04c01369 207int sd_ipv4ll_is_running(sd_ipv4ll *ll) {
8e91738f
YW
208 if (!ll)
209 return false;
aba496a5 210
e3dca008 211 return sd_ipv4acd_is_running(ll->acd);
aba496a5
UTL
212}
213
129dc1b4
TG
214int sd_ipv4ll_set_address(sd_ipv4ll *ll, const struct in_addr *address) {
215 int r;
216
217 assert_return(ll, -EINVAL);
218 assert_return(address, -EINVAL);
d3efcd2d 219 assert_return(in4_addr_is_link_local_dynamic(address), -EINVAL);
129dc1b4
TG
220
221 r = sd_ipv4acd_set_address(ll->acd, address);
222 if (r < 0)
223 return r;
224
225 ll->address = address->s_addr;
226
227 return 0;
228}
229
96a7979f
LP
230#define PICK_HASH_KEY SD_ID128_MAKE(15,ac,82,a6,d6,3f,49,78,98,77,5d,0c,69,02,94,0b)
231
e3dca008 232static int ipv4ll_pick_address(sd_ipv4ll *ll) {
e3dca008 233 be32_t addr;
5c1d3fc9 234
e3dca008 235 assert(ll);
4d978a46 236
e3dca008 237 do {
96a7979f 238 uint64_t h;
5c1d3fc9 239
96a7979f 240 h = siphash24(&ll->seed, sizeof(ll->seed), PICK_HASH_KEY.bytes);
5c1d3fc9 241
96a7979f
LP
242 /* Increase the generation counter by one */
243 ll->seed.generation = htole64(le64toh(ll->seed.generation) + 1);
b5db00e5 244
96a7979f
LP
245 addr = htobe32((h & UINT32_C(0x0000FFFF)) | IPV4LL_NETWORK);
246 } while (addr == ll->address ||
ae06d1be 247 IN_SET(be32toh(addr) & 0x0000FF00U, 0x0000U, 0xFF00U));
96a7979f 248
84dbb3fd 249 log_ipv4ll(ll, "Picked new IP address %s.", IN4_ADDR_TO_STRING((const struct in_addr*) &addr));
703945c1 250
96a7979f 251 return sd_ipv4ll_set_address(ll, &(struct in_addr) { addr });
e3dca008
TG
252}
253
96a7979f
LP
254#define MAC_HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
255
52cf2b13 256static int ipv4ll_start_internal(sd_ipv4ll *ll, bool reset_generation) {
e3dca008 257 int r;
96a7979f 258 bool picked_address = false;
e3dca008
TG
259
260 assert_return(ll, -EINVAL);
96a7979f 261 assert_return(!ether_addr_is_null(&ll->mac), -EINVAL);
96a7979f
LP
262
263 /* If no random seed is set, generate some from the MAC address */
264 if (!ll->seed_set)
265 ll->seed.value = htole64(siphash24(ll->mac.ether_addr_octet, ETH_ALEN, MAC_HASH_KEY.bytes));
266
52cf2b13
YW
267 if (reset_generation)
268 ll->seed.generation = 0;
b5db00e5
UTL
269
270 if (ll->address == 0) {
e3dca008 271 r = ipv4ll_pick_address(ll);
b5db00e5 272 if (r < 0)
e3dca008 273 return r;
96a7979f
LP
274
275 picked_address = true;
b5db00e5 276 }
5c1d3fc9 277
e92b60b2 278 r = sd_ipv4acd_start(ll->acd, reset_generation);
96a7979f
LP
279 if (r < 0) {
280
281 /* We couldn't start? If so, let's forget the picked address again, the user might make a change and
282 * retry, and we want the new data to take effect when picking an address. */
283 if (picked_address)
284 ll->address = 0;
285
e3dca008 286 return r;
96a7979f 287 }
996d1697 288
6b8a1aa6 289 return 1;
e3dca008 290}
996d1697 291
52cf2b13
YW
292int sd_ipv4ll_start(sd_ipv4ll *ll) {
293 assert_return(ll, -EINVAL);
6b8a1aa6
DS
294
295 if (sd_ipv4ll_is_running(ll))
296 return 0;
52cf2b13
YW
297
298 return ipv4ll_start_internal(ll, true);
299}
300
301int sd_ipv4ll_restart(sd_ipv4ll *ll) {
302 ll->address = 0;
303
304 return ipv4ll_start_internal(ll, false);
305}
306
e3dca008
TG
307static void ipv4ll_client_notify(sd_ipv4ll *ll, int event) {
308 assert(ll);
5c1d3fc9 309
45aa74c7
LP
310 if (ll->callback)
311 ll->callback(ll, event, ll->userdata);
e3dca008 312}
5c1d3fc9 313
e3dca008 314void ipv4ll_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
99534007 315 sd_ipv4ll *ll = ASSERT_PTR(userdata);
e3dca008
TG
316 IPV4LL_DONT_DESTROY(ll);
317 int r;
5c1d3fc9 318
e3dca008 319 assert(acd);
9021bb9f 320
e3dca008 321 switch (event) {
73e94c0d 322
2237aa02 323 case SD_IPV4ACD_EVENT_STOP:
be19c5b5 324 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP);
e3dca008 325 ll->claimed_address = 0;
e3dca008 326 break;
73e94c0d 327
2237aa02 328 case SD_IPV4ACD_EVENT_BIND:
e3dca008 329 ll->claimed_address = ll->address;
be19c5b5 330 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_BIND);
e3dca008 331 break;
73e94c0d 332
2237aa02 333 case SD_IPV4ACD_EVENT_CONFLICT:
e3dca008
TG
334 /* if an address was already bound we must call up to the
335 user to handle this, otherwise we just try again */
336 if (ll->claimed_address != 0) {
be19c5b5 337 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_CONFLICT);
e3dca008
TG
338
339 ll->claimed_address = 0;
340 } else {
52cf2b13 341 r = sd_ipv4ll_restart(ll);
e3dca008
TG
342 if (r < 0)
343 goto error;
344 }
345
346 break;
96a7979f 347
e3dca008 348 default:
04499a70 349 assert_not_reached();
e3dca008
TG
350 }
351
352 return;
353
354error:
be19c5b5 355 ipv4ll_client_notify(ll, SD_IPV4LL_EVENT_STOP);
5c1d3fc9 356}
1e59122d
YW
357
358static int ipv4ll_check_mac(sd_ipv4acd *acd, const struct ether_addr *mac, void *userdata) {
99534007 359 sd_ipv4ll *ll = ASSERT_PTR(userdata);
1e59122d
YW
360
361 if (ll->check_mac_callback)
362 return ll->check_mac_callback(ll, mac, ll->check_mac_userdata);
363
364 return 0;
365}