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