1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <netinet/in.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 r
= address_remove(address
, link
, NULL
);
41 r
= route_new(&route
);
43 return log_link_error_errno(link
, r
, "Could not allocate route: %m");
45 route
->family
= AF_INET
;
46 route
->scope
= RT_SCOPE_LINK
;
47 route
->priority
= IPV4LL_ROUTE_METRIC
;
49 r
= route_remove(route
, link
, NULL
);
53 link_check_ready(link
);
58 static int ipv4ll_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
62 assert(!link
->ipv4ll_route
);
64 r
= sd_netlink_message_get_errno(m
);
65 if (r
< 0 && r
!= -EEXIST
) {
66 log_link_error_errno(link
, r
, "could not set ipv4ll route: %m");
67 link_enter_failed(link
);
70 link
->ipv4ll_route
= true;
72 link_check_ready(link
);
77 static int ipv4ll_route_configure(Link
*link
) {
78 _cleanup_(route_freep
) Route
*route
= NULL
;
81 r
= route_new(&route
);
85 route
->family
= AF_INET
;
86 route
->scope
= RT_SCOPE_LINK
;
87 route
->protocol
= RTPROT_STATIC
;
88 route
->priority
= IPV4LL_ROUTE_METRIC
;
89 route
->table
= link_get_vrf_table(link
);
91 return route_configure(route
, link
, ipv4ll_route_handler
);
94 static int ipv4ll_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
98 assert(!link
->ipv4ll_address
);
100 r
= sd_netlink_message_get_errno(m
);
101 if (r
< 0 && r
!= -EEXIST
) {
102 log_link_error_errno(link
, r
, "could not set ipv4ll address: %m");
103 link_enter_failed(link
);
105 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
107 link
->ipv4ll_address
= true;
109 r
= ipv4ll_route_configure(link
);
111 log_link_error_errno(link
, r
, "Failed to configure ipv4ll route: %m");
112 link_enter_failed(link
);
118 static int ipv4ll_address_claimed(sd_ipv4ll
*ll
, Link
*link
) {
119 _cleanup_(address_freep
) Address
*ll_addr
= NULL
;
120 struct in_addr address
;
126 link
->ipv4ll_address
= false;
127 link
->ipv4ll_route
= false;
129 r
= sd_ipv4ll_get_address(ll
, &address
);
135 log_link_debug(link
, "IPv4 link-local claim %u.%u.%u.%u",
136 ADDRESS_FMT_VAL(address
));
138 r
= address_new(&ll_addr
);
142 ll_addr
->family
= AF_INET
;
143 ll_addr
->in_addr
.in
= address
;
144 ll_addr
->prefixlen
= 16;
145 ll_addr
->broadcast
.s_addr
= ll_addr
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> ll_addr
->prefixlen
);
146 ll_addr
->scope
= RT_SCOPE_LINK
;
148 r
= address_configure(ll_addr
, link
, ipv4ll_address_handler
, false);
155 static void ipv4ll_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
156 Link
*link
= userdata
;
160 assert(link
->network
);
162 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
166 case SD_IPV4LL_EVENT_STOP
:
167 r
= ipv4ll_address_lost(link
);
169 link_enter_failed(link
);
173 case SD_IPV4LL_EVENT_CONFLICT
:
174 r
= ipv4ll_address_lost(link
);
176 link_enter_failed(link
);
180 r
= sd_ipv4ll_restart(ll
);
182 log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
184 case SD_IPV4LL_EVENT_BIND
:
185 r
= ipv4ll_address_claimed(ll
, link
);
187 log_link_error(link
, "Failed to configure ipv4ll address: %m");
188 link_enter_failed(link
);
193 log_link_warning(link
, "IPv4 link-local unknown event: %d", event
);
198 int ipv4ll_configure(Link
*link
) {
203 assert(link
->network
);
204 assert(link
->network
->link_local
& (ADDRESS_FAMILY_IPV4
| ADDRESS_FAMILY_FALLBACK_IPV4
));
207 r
= sd_ipv4ll_new(&link
->ipv4ll
);
212 if (link
->sd_device
&&
213 net_get_unique_predictable_data(link
->sd_device
, &seed
) >= 0) {
214 r
= sd_ipv4ll_set_address_seed(link
->ipv4ll
, seed
);
219 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
223 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
227 r
= sd_ipv4ll_set_ifindex(link
->ipv4ll
, link
->ifindex
);
231 r
= sd_ipv4ll_set_callback(link
->ipv4ll
, ipv4ll_handler
, link
);