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