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