]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-ipv4ll.c
3562e90535ddce1f5ea1736060a109d8581e06e3
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
, NULL
);
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
, NULL
);
49 link_check_ready(link
);
54 static int ipv4ll_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
58 assert(!link
->ipv4ll_route
);
60 r
= sd_netlink_message_get_errno(m
);
61 if (r
< 0 && r
!= -EEXIST
) {
62 log_link_error_errno(link
, r
, "could not set ipv4ll route: %m");
63 link_enter_failed(link
);
66 link
->ipv4ll_route
= true;
68 if (link
->ipv4ll_address
== true)
69 link_check_ready(link
);
74 static int ipv4ll_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
78 assert(!link
->ipv4ll_address
);
80 r
= sd_netlink_message_get_errno(m
);
81 if (r
< 0 && r
!= -EEXIST
) {
82 log_link_error_errno(link
, r
, "could not set ipv4ll address: %m");
83 link_enter_failed(link
);
85 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
87 link
->ipv4ll_address
= true;
89 if (link
->ipv4ll_route
)
90 link_check_ready(link
);
95 static int ipv4ll_address_claimed(sd_ipv4ll
*ll
, Link
*link
) {
96 _cleanup_(address_freep
) Address
*ll_addr
= NULL
;
97 _cleanup_(route_freep
) Route
*route
= NULL
;
98 struct in_addr address
;
104 r
= sd_ipv4ll_get_address(ll
, &address
);
110 log_link_debug(link
, "IPv4 link-local claim %u.%u.%u.%u",
111 ADDRESS_FMT_VAL(address
));
113 r
= address_new(&ll_addr
);
117 ll_addr
->family
= AF_INET
;
118 ll_addr
->in_addr
.in
= address
;
119 ll_addr
->prefixlen
= 16;
120 ll_addr
->broadcast
.s_addr
= ll_addr
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> ll_addr
->prefixlen
);
121 ll_addr
->scope
= RT_SCOPE_LINK
;
123 r
= address_configure(ll_addr
, link
, ipv4ll_address_handler
, false);
127 link
->ipv4ll_address
= false;
129 r
= route_new(&route
);
133 route
->family
= AF_INET
;
134 route
->scope
= RT_SCOPE_LINK
;
135 route
->protocol
= RTPROT_STATIC
;
136 route
->priority
= IPV4LL_ROUTE_METRIC
;
138 r
= route_configure(route
, link
, ipv4ll_route_handler
);
142 link
->ipv4ll_route
= false;
147 static void ipv4ll_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
148 Link
*link
= userdata
;
152 assert(link
->network
);
154 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
158 case SD_IPV4LL_EVENT_STOP
:
159 r
= ipv4ll_address_lost(link
);
161 link_enter_failed(link
);
165 case SD_IPV4LL_EVENT_CONFLICT
:
166 r
= ipv4ll_address_lost(link
);
168 link_enter_failed(link
);
172 r
= sd_ipv4ll_restart(ll
);
174 log_link_warning(link
, "Could not acquire IPv4 link-local address");
176 case SD_IPV4LL_EVENT_BIND
:
177 r
= ipv4ll_address_claimed(ll
, link
);
179 link_enter_failed(link
);
184 log_link_warning(link
, "IPv4 link-local unknown event: %d", event
);
189 int ipv4ll_configure(Link
*link
) {
194 assert(link
->network
);
195 assert(link
->network
->link_local
& ADDRESS_FAMILY_IPV4
);
198 r
= sd_ipv4ll_new(&link
->ipv4ll
);
203 if (link
->sd_device
&&
204 net_get_unique_predictable_data(link
->sd_device
, &seed
) >= 0) {
205 r
= sd_ipv4ll_set_address_seed(link
->ipv4ll
, seed
);
210 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
214 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
218 r
= sd_ipv4ll_set_ifindex(link
->ipv4ll
, link
->ifindex
);
222 r
= sd_ipv4ll_set_callback(link
->ipv4ll
, ipv4ll_handler
, link
);