1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright (C) 2014 Axis Communications AB. All rights reserved.
4 Copyright (C) 2015 Tom Gundersen
14 #include "sd-ipv4acd.h"
15 #include "sd-ipv4ll.h"
17 #include "alloc-util.h"
18 #include "ether-addr-util.h"
19 #include "in-addr-util.h"
21 #include "random-util.h"
22 #include "siphash24.h"
23 #include "sparse-endian.h"
24 #include "string-util.h"
27 #define IPV4LL_NETWORK UINT32_C(0xA9FE0000)
28 #define IPV4LL_NETMASK UINT32_C(0xFFFF0000)
30 #define IPV4LL_DONT_DESTROY(ll) \
31 _cleanup_(sd_ipv4ll_unrefp) _unused_ sd_ipv4ll *_dont_destroy_##ll = sd_ipv4ll_ref(ll)
38 be32_t address
; /* the address pushed to ACD */
39 struct ether_addr mac
;
48 be32_t claimed_address
;
50 sd_ipv4ll_callback_t callback
;
54 #define log_ipv4ll_errno(ll, error, fmt, ...) log_internal(LOG_DEBUG, error, __FILE__, __LINE__, __func__, "IPV4LL: " fmt, ##__VA_ARGS__)
55 #define log_ipv4ll(ll, fmt, ...) log_ipv4ll_errno(ll, 0, fmt, ##__VA_ARGS__)
57 static void ipv4ll_on_acd(sd_ipv4acd
*ll
, int event
, void *userdata
);
59 sd_ipv4ll
*sd_ipv4ll_ref(sd_ipv4ll
*ll
) {
63 assert(ll
->n_ref
>= 1);
69 sd_ipv4ll
*sd_ipv4ll_unref(sd_ipv4ll
*ll
) {
73 assert(ll
->n_ref
>= 1);
79 sd_ipv4acd_unref(ll
->acd
);
83 int sd_ipv4ll_new(sd_ipv4ll
**ret
) {
84 _cleanup_(sd_ipv4ll_unrefp
) sd_ipv4ll
*ll
= NULL
;
87 assert_return(ret
, -EINVAL
);
89 ll
= new0(sd_ipv4ll
, 1);
95 r
= sd_ipv4acd_new(&ll
->acd
);
99 r
= sd_ipv4acd_set_callback(ll
->acd
, ipv4ll_on_acd
, ll
);
108 int sd_ipv4ll_stop(sd_ipv4ll
*ll
) {
109 assert_return(ll
, -EINVAL
);
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_set_mac(sd_ipv4ll
*ll
, const struct ether_addr
*addr
) {
125 assert_return(ll
, -EINVAL
);
126 assert_return(addr
, -EINVAL
);
127 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
129 r
= sd_ipv4acd_set_mac(ll
->acd
, addr
);
137 int sd_ipv4ll_detach_event(sd_ipv4ll
*ll
) {
138 assert_return(ll
, -EINVAL
);
140 return sd_ipv4acd_detach_event(ll
->acd
);
143 int sd_ipv4ll_attach_event(sd_ipv4ll
*ll
, sd_event
*event
, int64_t priority
) {
144 assert_return(ll
, -EINVAL
);
146 return sd_ipv4acd_attach_event(ll
->acd
, event
, priority
);
149 int sd_ipv4ll_set_callback(sd_ipv4ll
*ll
, sd_ipv4ll_callback_t cb
, void *userdata
) {
150 assert_return(ll
, -EINVAL
);
153 ll
->userdata
= userdata
;
158 int sd_ipv4ll_get_address(sd_ipv4ll
*ll
, struct in_addr
*address
) {
159 assert_return(ll
, -EINVAL
);
160 assert_return(address
, -EINVAL
);
162 if (ll
->claimed_address
== 0)
165 address
->s_addr
= ll
->claimed_address
;
170 int sd_ipv4ll_set_address_seed(sd_ipv4ll
*ll
, uint64_t seed
) {
171 assert_return(ll
, -EINVAL
);
172 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
174 ll
->seed
.value
= htole64(seed
);
180 int sd_ipv4ll_is_running(sd_ipv4ll
*ll
) {
181 assert_return(ll
, false);
183 return sd_ipv4acd_is_running(ll
->acd
);
186 static bool ipv4ll_address_is_valid(const struct in_addr
*address
) {
189 if (!in_addr_is_link_local(AF_INET
, (const union in_addr_union
*) address
))
192 return !IN_SET(be32toh(address
->s_addr
) & 0x0000FF00U
, 0x0000U
, 0xFF00U
);
195 int sd_ipv4ll_set_address(sd_ipv4ll
*ll
, const struct in_addr
*address
) {
198 assert_return(ll
, -EINVAL
);
199 assert_return(address
, -EINVAL
);
200 assert_return(ipv4ll_address_is_valid(address
), -EINVAL
);
202 r
= sd_ipv4acd_set_address(ll
->acd
, address
);
206 ll
->address
= address
->s_addr
;
211 #define PICK_HASH_KEY SD_ID128_MAKE(15,ac,82,a6,d6,3f,49,78,98,77,5d,0c,69,02,94,0b)
213 static int ipv4ll_pick_address(sd_ipv4ll
*ll
) {
214 _cleanup_free_
char *address
= NULL
;
222 h
= siphash24(&ll
->seed
, sizeof(ll
->seed
), PICK_HASH_KEY
.bytes
);
224 /* Increase the generation counter by one */
225 ll
->seed
.generation
= htole64(le64toh(ll
->seed
.generation
) + 1);
227 addr
= htobe32((h
& UINT32_C(0x0000FFFF)) | IPV4LL_NETWORK
);
228 } while (addr
== ll
->address
||
229 IN_SET(be32toh(addr
) & 0x0000FF00U
, 0x0000U
, 0xFF00U
));
231 (void) in_addr_to_string(AF_INET
, &(union in_addr_union
) { .in
.s_addr
= addr
}, &address
);
232 log_ipv4ll(ll
, "Picked new IP address %s.", strna(address
));
234 return sd_ipv4ll_set_address(ll
, &(struct in_addr
) { addr
});
237 int sd_ipv4ll_restart(sd_ipv4ll
*ll
) {
240 return sd_ipv4ll_start(ll
);
243 #define MAC_HASH_KEY SD_ID128_MAKE(df,04,22,98,3f,ad,14,52,f9,87,2e,d1,9c,70,e2,f2)
245 int sd_ipv4ll_start(sd_ipv4ll
*ll
) {
247 bool picked_address
= false;
249 assert_return(ll
, -EINVAL
);
250 assert_return(!ether_addr_is_null(&ll
->mac
), -EINVAL
);
251 assert_return(sd_ipv4ll_is_running(ll
) == 0, -EBUSY
);
253 /* If no random seed is set, generate some from the MAC address */
255 ll
->seed
.value
= htole64(siphash24(ll
->mac
.ether_addr_octet
, ETH_ALEN
, MAC_HASH_KEY
.bytes
));
257 /* Restart the generation counter. */
258 ll
->seed
.generation
= 0;
260 if (ll
->address
== 0) {
261 r
= ipv4ll_pick_address(ll
);
265 picked_address
= true;
268 r
= sd_ipv4acd_start(ll
->acd
);
271 /* We couldn't start? If so, let's forget the picked address again, the user might make a change and
272 * retry, and we want the new data to take effect when picking an address. */
282 static void ipv4ll_client_notify(sd_ipv4ll
*ll
, int event
) {
286 ll
->callback(ll
, event
, ll
->userdata
);
289 void ipv4ll_on_acd(sd_ipv4acd
*acd
, int event
, void *userdata
) {
290 sd_ipv4ll
*ll
= userdata
;
291 IPV4LL_DONT_DESTROY(ll
);
299 case SD_IPV4ACD_EVENT_STOP
:
300 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_STOP
);
301 ll
->claimed_address
= 0;
304 case SD_IPV4ACD_EVENT_BIND
:
305 ll
->claimed_address
= ll
->address
;
306 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_BIND
);
309 case SD_IPV4ACD_EVENT_CONFLICT
:
310 /* if an address was already bound we must call up to the
311 user to handle this, otherwise we just try again */
312 if (ll
->claimed_address
!= 0) {
313 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_CONFLICT
);
315 ll
->claimed_address
= 0;
317 r
= ipv4ll_pick_address(ll
);
321 r
= sd_ipv4acd_start(ll
->acd
);
329 assert_not_reached("Invalid IPv4ACD event.");
335 ipv4ll_client_notify(ll
, SD_IPV4LL_EVENT_STOP
);