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