1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/ether.h>
6 #include "network-internal.h"
7 #include "networkd-address.h"
8 #include "networkd-manager.h"
9 #include "networkd-link.h"
11 static int ipv4ll_address_lost(Link
*link
) {
12 _cleanup_(address_freep
) Address
*address
= NULL
;
13 _cleanup_(route_freep
) Route
*route
= NULL
;
19 link
->ipv4ll_route
= false;
20 link
->ipv4ll_address
= false;
22 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &addr
);
26 log_link_debug(link
, "IPv4 link-local release %u.%u.%u.%u", ADDRESS_FMT_VAL(addr
));
28 r
= address_new(&address
);
30 return log_link_error_errno(link
, r
, "Could not allocate address: %m");
32 address
->family
= AF_INET
;
33 address
->in_addr
.in
= addr
;
34 address
->prefixlen
= 16;
35 address
->scope
= RT_SCOPE_LINK
;
37 address_remove(address
, link
, link_address_remove_handler
);
39 r
= route_new(&route
);
41 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
43 route
->family
= AF_INET
;
44 route
->scope
= RT_SCOPE_LINK
;
45 route
->priority
= IPV4LL_ROUTE_METRIC
;
47 route_remove(route
, link
, link_route_remove_handler
);
49 link_check_ready(link
);
54 static int ipv4ll_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
55 Link
*link
= userdata
;
59 assert(!link
->ipv4ll_route
);
61 r
= sd_netlink_message_get_errno(m
);
62 if (r
< 0 && r
!= -EEXIST
) {
63 log_link_error_errno(link
, r
, "could not set ipv4ll route: %m");
64 link_enter_failed(link
);
67 link
->ipv4ll_route
= true;
69 if (link
->ipv4ll_address
== true)
70 link_check_ready(link
);
75 static int ipv4ll_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
76 Link
*link
= userdata
;
80 assert(!link
->ipv4ll_address
);
82 r
= sd_netlink_message_get_errno(m
);
83 if (r
< 0 && r
!= -EEXIST
) {
84 log_link_error_errno(link
, r
, "could not set ipv4ll address: %m");
85 link_enter_failed(link
);
87 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
89 link
->ipv4ll_address
= true;
91 if (link
->ipv4ll_route
)
92 link_check_ready(link
);
97 static int ipv4ll_address_claimed(sd_ipv4ll
*ll
, Link
*link
) {
98 _cleanup_(address_freep
) Address
*ll_addr
= NULL
;
99 _cleanup_(route_freep
) Route
*route
= NULL
;
100 struct in_addr address
;
106 r
= sd_ipv4ll_get_address(ll
, &address
);
112 log_link_debug(link
, "IPv4 link-local claim %u.%u.%u.%u",
113 ADDRESS_FMT_VAL(address
));
115 r
= address_new(&ll_addr
);
119 ll_addr
->family
= AF_INET
;
120 ll_addr
->in_addr
.in
= address
;
121 ll_addr
->prefixlen
= 16;
122 ll_addr
->broadcast
.s_addr
= ll_addr
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> ll_addr
->prefixlen
);
123 ll_addr
->scope
= RT_SCOPE_LINK
;
125 r
= address_configure(ll_addr
, link
, ipv4ll_address_handler
, false);
129 link
->ipv4ll_address
= false;
131 r
= route_new(&route
);
135 route
->family
= AF_INET
;
136 route
->scope
= RT_SCOPE_LINK
;
137 route
->protocol
= RTPROT_STATIC
;
138 route
->priority
= IPV4LL_ROUTE_METRIC
;
140 r
= route_configure(route
, link
, ipv4ll_route_handler
);
144 link
->ipv4ll_route
= false;
149 static void ipv4ll_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
150 Link
*link
= userdata
;
154 assert(link
->network
);
156 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
160 case SD_IPV4LL_EVENT_STOP
:
161 r
= ipv4ll_address_lost(link
);
163 link_enter_failed(link
);
167 case SD_IPV4LL_EVENT_CONFLICT
:
168 r
= ipv4ll_address_lost(link
);
170 link_enter_failed(link
);
174 r
= sd_ipv4ll_restart(ll
);
176 log_link_warning(link
, "Could not acquire IPv4 link-local address");
178 case SD_IPV4LL_EVENT_BIND
:
179 r
= ipv4ll_address_claimed(ll
, link
);
181 link_enter_failed(link
);
186 log_link_warning(link
, "IPv4 link-local unknown event: %d", event
);
191 int ipv4ll_configure(Link
*link
) {
196 assert(link
->network
);
197 assert(link
->network
->link_local
& ADDRESS_FAMILY_IPV4
);
200 r
= sd_ipv4ll_new(&link
->ipv4ll
);
205 if (link
->sd_device
&&
206 net_get_unique_predictable_data(link
->sd_device
, &seed
) >= 0) {
207 r
= sd_ipv4ll_set_address_seed(link
->ipv4ll
, seed
);
212 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
216 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
220 r
= sd_ipv4ll_set_ifindex(link
->ipv4ll
, link
->ifindex
);
224 r
= sd_ipv4ll_set_callback(link
->ipv4ll
, ipv4ll_handler
, link
);