1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Axis Communications AB. All rights reserved.
13 #include "sd-ipv4acd.h"
14 #include "sd-ipv4ll.h"
16 #include "alloc-util.h"
17 #include "ether-addr-util.h"
18 #include "in-addr-util.h"
20 #include "random-util.h"
21 #include "siphash24.h"
22 #include "sparse-endian.h"
23 #include "string-util.h"
26 #define IPV4LL_NETWORK UINT32_C(0xA9FE0000)
27 #define IPV4LL_NETMASK UINT32_C(0xFFFF0000)
29 #define IPV4LL_DONT_DESTROY(ll) \
30 _cleanup_(sd_ipv4ll_unrefp) _unused_ sd_ipv4ll *_dont_destroy_##ll = sd_ipv4ll_ref(ll)
37 be32_t address
; /* the address pushed to ACD */
38 struct ether_addr mac
;
47 be32_t claimed_address
;
49 sd_ipv4ll_callback_t callback
;
53 #define log_ipv4ll_errno(ll, error, fmt, ...) log_internal(LOG_DEBUG, error, __FILE__, __LINE__, __func__, "IPV4LL: " fmt, ##__VA_ARGS__)
54 #define log_ipv4ll(ll, fmt, ...) log_ipv4ll_errno(ll, 0, fmt, ##__VA_ARGS__)
56 static void ipv4ll_on_acd(sd_ipv4acd
*ll
, int event
, void *userdata
);
58 static sd_ipv4ll
*ipv4ll_free(sd_ipv4ll
*ll
) {
61 sd_ipv4acd_unref(ll
->acd
);
65 DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4ll
, sd_ipv4ll
, ipv4ll_free
);
67 int sd_ipv4ll_new(sd_ipv4ll
**ret
) {
68 _cleanup_(sd_ipv4ll_unrefp
) sd_ipv4ll
*ll
= NULL
;
71 assert_return(ret
, -EINVAL
);
73 ll
= new0(sd_ipv4ll
, 1);
79 r
= sd_ipv4acd_new(&ll
->acd
);
83 r
= sd_ipv4acd_set_callback(ll
->acd
, ipv4ll_on_acd
, ll
);
92 int sd_ipv4ll_stop(sd_ipv4ll
*ll
) {
93 assert_return(ll
, -EINVAL
);
95 return sd_ipv4acd_stop(ll
->acd
);
98 int sd_ipv4ll_set_ifindex(sd_ipv4ll
*ll
, int ifindex
) {
99 assert_return(ll
, -EINVAL
);
100 assert_return(ifindex
> 0, -EINVAL
);
101 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
103 return sd_ipv4acd_set_ifindex(ll
->acd
, ifindex
);
106 int sd_ipv4ll_set_mac(sd_ipv4ll
*ll
, const struct ether_addr
*addr
) {
109 assert_return(ll
, -EINVAL
);
110 assert_return(addr
, -EINVAL
);
111 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
113 r
= sd_ipv4acd_set_mac(ll
->acd
, addr
);
121 int sd_ipv4ll_detach_event(sd_ipv4ll
*ll
) {
122 assert_return(ll
, -EINVAL
);
124 return sd_ipv4acd_detach_event(ll
->acd
);
127 int sd_ipv4ll_attach_event(sd_ipv4ll
*ll
, sd_event
*event
, int64_t priority
) {
128 assert_return(ll
, -EINVAL
);
130 return sd_ipv4acd_attach_event(ll
->acd
, event
, priority
);
133 int sd_ipv4ll_set_callback(sd_ipv4ll
*ll
, sd_ipv4ll_callback_t cb
, void *userdata
) {
134 assert_return(ll
, -EINVAL
);
137 ll
->userdata
= userdata
;
142 int sd_ipv4ll_get_address(sd_ipv4ll
*ll
, struct in_addr
*address
) {
143 assert_return(ll
, -EINVAL
);
144 assert_return(address
, -EINVAL
);
146 if (ll
->claimed_address
== 0)
149 address
->s_addr
= ll
->claimed_address
;
154 int sd_ipv4ll_set_address_seed(sd_ipv4ll
*ll
, uint64_t seed
) {
155 assert_return(ll
, -EINVAL
);
156 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
158 ll
->seed
.value
= htole64(seed
);
164 int sd_ipv4ll_is_running(sd_ipv4ll
*ll
) {
165 assert_return(ll
, false);
167 return sd_ipv4acd_is_running(ll
->acd
);
170 static bool ipv4ll_address_is_valid(const struct in_addr
*address
) {
173 if (!in_addr_is_link_local(AF_INET
, (const union in_addr_union
*) address
))
176 return !IN_SET(be32toh(address
->s_addr
) & 0x0000FF00U
, 0x0000U
, 0xFF00U
);
179 int sd_ipv4ll_set_address(sd_ipv4ll
*ll
, const struct in_addr
*address
) {
182 assert_return(ll
, -EINVAL
);
183 assert_return(address
, -EINVAL
);
184 assert_return(ipv4ll_address_is_valid(address
), -EINVAL
);
186 r
= sd_ipv4acd_set_address(ll
->acd
, address
);
190 ll
->address
= address
->s_addr
;
195 #define PICK_HASH_KEY SD_ID128_MAKE(15,ac,82,a6,d6,3f,49,78,98,77,5d,0c,69,02,94,0b)
197 static int ipv4ll_pick_address(sd_ipv4ll
*ll
) {
198 _cleanup_free_
char *address
= NULL
;
206 h
= siphash24(&ll
->seed
, sizeof(ll
->seed
), PICK_HASH_KEY
.bytes
);
208 /* Increase the generation counter by one */
209 ll
->seed
.generation
= htole64(le64toh(ll
->seed
.generation
) + 1);
211 addr
= htobe32((h
& UINT32_C(0x0000FFFF)) | IPV4LL_NETWORK
);
212 } while (addr
== ll
->address
||
213 IN_SET(be32toh(addr
) & 0x0000FF00U
, 0x0000U
, 0xFF00U
));
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
));
218 return sd_ipv4ll_set_address(ll
, &(struct in_addr
) { addr
});
221 #define MAC_HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
223 static int ipv4ll_start_internal(sd_ipv4ll
*ll
, bool reset_generation
) {
225 bool picked_address
= false;
227 assert_return(ll
, -EINVAL
);
228 assert_return(!ether_addr_is_null(&ll
->mac
), -EINVAL
);
230 /* If no random seed is set, generate some from the MAC address */
232 ll
->seed
.value
= htole64(siphash24(ll
->mac
.ether_addr_octet
, ETH_ALEN
, MAC_HASH_KEY
.bytes
));
234 if (reset_generation
)
235 ll
->seed
.generation
= 0;
237 if (ll
->address
== 0) {
238 r
= ipv4ll_pick_address(ll
);
242 picked_address
= true;
245 r
= sd_ipv4acd_start(ll
->acd
);
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. */
259 int sd_ipv4ll_start(sd_ipv4ll
*ll
) {
260 assert_return(ll
, -EINVAL
);
261 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
263 return ipv4ll_start_internal(ll
, true);
266 int sd_ipv4ll_restart(sd_ipv4ll
*ll
) {
269 return ipv4ll_start_internal(ll
, false);
272 static void ipv4ll_client_notify(sd_ipv4ll
*ll
, int event
) {
276 ll
->callback(ll
, event
, ll
->userdata
);
279 void ipv4ll_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
280 sd_ipv4ll
*ll
= userdata
;
281 IPV4LL_DONT_DESTROY(ll
);
289 case SD_IPV4ACD_EVENT_STOP
:
290 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_STOP
);
291 ll
->claimed_address
= 0;
294 case SD_IPV4ACD_EVENT_BIND
:
295 ll
->claimed_address
= ll
->address
;
296 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_BIND
);
299 case SD_IPV4ACD_EVENT_CONFLICT
:
300 /* if an address was already bound we must call up to the
301 user to handle this, otherwise we just try again */
302 if (ll
->claimed_address
!= 0) {
303 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_CONFLICT
);
305 ll
->claimed_address
= 0;
307 r
= sd_ipv4ll_restart(ll
);
315 assert_not_reached("Invalid IPv4ACD event.");
321 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_STOP
);