1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2014 Axis Communications AB. All rights reserved.
12 #include "sd-ipv4acd.h"
13 #include "sd-ipv4ll.h"
15 #include "alloc-util.h"
16 #include "ether-addr-util.h"
17 #include "in-addr-util.h"
19 #include "random-util.h"
20 #include "siphash24.h"
21 #include "sparse-endian.h"
22 #include "string-util.h"
25 #define IPV4LL_NETWORK UINT32_C(0xA9FE0000)
26 #define IPV4LL_NETMASK UINT32_C(0xFFFF0000)
28 #define IPV4LL_DONT_DESTROY(ll) \
29 _cleanup_(sd_ipv4ll_unrefp) _unused_ sd_ipv4ll *_dont_destroy_##ll = sd_ipv4ll_ref(ll)
36 be32_t address
; /* the address pushed to ACD */
37 struct ether_addr mac
;
46 be32_t claimed_address
;
48 sd_ipv4ll_callback_t callback
;
51 sd_ipv4ll_check_mac_callback_t check_mac_callback
;
52 void *check_mac_userdata
;
55 #define log_ipv4ll_errno(ll, error, fmt, ...) \
56 log_interface_prefix_full_errno( \
58 sd_ipv4ll_get_ifname(ll), \
59 error, fmt, ##__VA_ARGS__)
60 #define log_ipv4ll(ll, fmt, ...) \
61 log_interface_prefix_full_errno_zerook( \
63 sd_ipv4ll_get_ifname(ll), \
64 0, fmt, ##__VA_ARGS__)
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
);
69 static sd_ipv4ll
*ipv4ll_free(sd_ipv4ll
*ll
) {
72 sd_ipv4acd_unref(ll
->acd
);
76 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4ll
, sd_ipv4ll
, ipv4ll_free
);
78 int sd_ipv4ll_new(sd_ipv4ll
**ret
) {
79 _cleanup_(sd_ipv4ll_unrefp
) sd_ipv4ll
*ll
= NULL
;
82 assert_return(ret
, -EINVAL
);
84 ll
= new0(sd_ipv4ll
, 1);
90 r
= sd_ipv4acd_new(&ll
->acd
);
94 r
= sd_ipv4acd_set_callback(ll
->acd
, ipv4ll_on_acd
, ll
);
98 r
= sd_ipv4acd_set_check_mac_callback(ll
->acd
, ipv4ll_check_mac
, ll
);
107 int sd_ipv4ll_stop(sd_ipv4ll
*ll
) {
111 return sd_ipv4acd_stop(ll
->acd
);
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
);
119 return sd_ipv4acd_set_ifindex(ll
->acd
, ifindex
);
122 int sd_ipv4ll_get_ifindex(sd_ipv4ll
*ll
) {
126 return sd_ipv4acd_get_ifindex(ll
->acd
);
129 int sd_ipv4ll_set_ifname(sd_ipv4ll
*ll
, const char *ifname
) {
130 assert_return(ll
, -EINVAL
);
131 assert_return(ifname
, -EINVAL
);
133 return sd_ipv4acd_set_ifname(ll
->acd
, ifname
);
136 const char *sd_ipv4ll_get_ifname(sd_ipv4ll
*ll
) {
140 return sd_ipv4acd_get_ifname(ll
->acd
);
143 int sd_ipv4ll_set_mac(sd_ipv4ll
*ll
, const struct ether_addr
*addr
) {
146 assert_return(ll
, -EINVAL
);
147 assert_return(addr
, -EINVAL
);
148 assert_return(!ether_addr_is_null(addr
), -EINVAL
);
150 r
= sd_ipv4acd_set_mac(ll
->acd
, addr
);
158 int sd_ipv4ll_detach_event(sd_ipv4ll
*ll
) {
159 assert_return(ll
, -EINVAL
);
161 return sd_ipv4acd_detach_event(ll
->acd
);
164 int sd_ipv4ll_attach_event(sd_ipv4ll
*ll
, sd_event
*event
, int64_t priority
) {
165 assert_return(ll
, -EINVAL
);
167 return sd_ipv4acd_attach_event(ll
->acd
, event
, priority
);
170 int sd_ipv4ll_set_callback(sd_ipv4ll
*ll
, sd_ipv4ll_callback_t cb
, void *userdata
) {
171 assert_return(ll
, -EINVAL
);
174 ll
->userdata
= userdata
;
179 int sd_ipv4ll_set_check_mac_callback(sd_ipv4ll
*ll
, sd_ipv4ll_check_mac_callback_t cb
, void *userdata
) {
180 assert_return(ll
, -EINVAL
);
182 ll
->check_mac_callback
= cb
;
183 ll
->check_mac_userdata
= userdata
;
188 int sd_ipv4ll_get_address(sd_ipv4ll
*ll
, struct in_addr
*address
) {
189 assert_return(ll
, -EINVAL
);
190 assert_return(address
, -EINVAL
);
192 if (ll
->claimed_address
== 0)
195 address
->s_addr
= ll
->claimed_address
;
200 int sd_ipv4ll_set_address_seed(sd_ipv4ll
*ll
, uint64_t seed
) {
201 assert_return(ll
, -EINVAL
);
202 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
204 ll
->seed
.value
= htole64(seed
);
210 int sd_ipv4ll_is_running(sd_ipv4ll
*ll
) {
211 assert_return(ll
, false);
213 return sd_ipv4acd_is_running(ll
->acd
);
216 static bool ipv4ll_address_is_valid(const struct in_addr
*address
) {
219 if (!in4_addr_is_link_local(address
))
222 return !IN_SET(be32toh(address
->s_addr
) & 0x0000FF00U
, 0x0000U
, 0xFF00U
);
225 int sd_ipv4ll_set_address(sd_ipv4ll
*ll
, const struct in_addr
*address
) {
228 assert_return(ll
, -EINVAL
);
229 assert_return(address
, -EINVAL
);
230 assert_return(ipv4ll_address_is_valid(address
), -EINVAL
);
232 r
= sd_ipv4acd_set_address(ll
->acd
, address
);
236 ll
->address
= address
->s_addr
;
241 #define PICK_HASH_KEY SD_ID128_MAKE(15,ac,82,a6,d6,3f,49,78,98,77,5d,0c,69,02,94,0b)
243 static int ipv4ll_pick_address(sd_ipv4ll
*ll
) {
244 _cleanup_free_
char *address
= NULL
;
252 h
= siphash24(&ll
->seed
, sizeof(ll
->seed
), PICK_HASH_KEY
.bytes
);
254 /* Increase the generation counter by one */
255 ll
->seed
.generation
= htole64(le64toh(ll
->seed
.generation
) + 1);
257 addr
= htobe32((h
& UINT32_C(0x0000FFFF)) | IPV4LL_NETWORK
);
258 } while (addr
== ll
->address
||
259 IN_SET(be32toh(addr
) & 0x0000FF00U
, 0x0000U
, 0xFF00U
));
261 (void) in_addr_to_string(AF_INET
, &(union in_addr_union
) { .in
.s_addr
= addr
}, &address
);
262 log_ipv4ll(ll
, "Picked new IP address %s.", strna(address
));
264 return sd_ipv4ll_set_address(ll
, &(struct in_addr
) { addr
});
267 #define MAC_HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
269 static int ipv4ll_start_internal(sd_ipv4ll
*ll
, bool reset_generation
) {
271 bool picked_address
= false;
273 assert_return(ll
, -EINVAL
);
274 assert_return(!ether_addr_is_null(&ll
->mac
), -EINVAL
);
276 /* If no random seed is set, generate some from the MAC address */
278 ll
->seed
.value
= htole64(siphash24(ll
->mac
.ether_addr_octet
, ETH_ALEN
, MAC_HASH_KEY
.bytes
));
280 if (reset_generation
)
281 ll
->seed
.generation
= 0;
283 if (ll
->address
== 0) {
284 r
= ipv4ll_pick_address(ll
);
288 picked_address
= true;
291 r
= sd_ipv4acd_start(ll
->acd
, reset_generation
);
294 /* We couldn't start? If so, let's forget the picked address again, the user might make a change and
295 * retry, and we want the new data to take effect when picking an address. */
305 int sd_ipv4ll_start(sd_ipv4ll
*ll
) {
306 assert_return(ll
, -EINVAL
);
308 if (sd_ipv4ll_is_running(ll
))
311 return ipv4ll_start_internal(ll
, true);
314 int sd_ipv4ll_restart(sd_ipv4ll
*ll
) {
317 return ipv4ll_start_internal(ll
, false);
320 static void ipv4ll_client_notify(sd_ipv4ll
*ll
, int event
) {
324 ll
->callback(ll
, event
, ll
->userdata
);
327 void ipv4ll_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
328 sd_ipv4ll
*ll
= userdata
;
329 IPV4LL_DONT_DESTROY(ll
);
337 case SD_IPV4ACD_EVENT_STOP
:
338 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_STOP
);
339 ll
->claimed_address
= 0;
342 case SD_IPV4ACD_EVENT_BIND
:
343 ll
->claimed_address
= ll
->address
;
344 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_BIND
);
347 case SD_IPV4ACD_EVENT_CONFLICT
:
348 /* if an address was already bound we must call up to the
349 user to handle this, otherwise we just try again */
350 if (ll
->claimed_address
!= 0) {
351 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_CONFLICT
);
353 ll
->claimed_address
= 0;
355 r
= sd_ipv4ll_restart(ll
);
363 assert_not_reached();
369 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_STOP
);
372 static int ipv4ll_check_mac(sd_ipv4acd
*acd
, const struct ether_addr
*mac
, void *userdata
) {
373 sd_ipv4ll
*ll
= userdata
;
377 if (ll
->check_mac_callback
)
378 return ll
->check_mac_callback(ll
, mac
, ll
->check_mac_userdata
);