]>
git.proxmox.com Git - systemd.git/blob - src/network/networkd-ipv4ll.c
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 link_check_ready(link
);
73 static int ipv4ll_route_configure(Link
*link
) {
74 _cleanup_(route_freep
) Route
*route
= NULL
;
77 r
= route_new(&route
);
81 route
->family
= AF_INET
;
82 route
->scope
= RT_SCOPE_LINK
;
83 route
->protocol
= RTPROT_STATIC
;
84 route
->priority
= IPV4LL_ROUTE_METRIC
;
85 route
->table
= link_get_vrf_table(link
);
87 return route_configure(route
, link
, ipv4ll_route_handler
);
90 static int ipv4ll_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
94 assert(!link
->ipv4ll_address
);
96 r
= sd_netlink_message_get_errno(m
);
97 if (r
< 0 && r
!= -EEXIST
) {
98 log_link_error_errno(link
, r
, "could not set ipv4ll address: %m");
99 link_enter_failed(link
);
101 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
103 link
->ipv4ll_address
= true;
105 r
= ipv4ll_route_configure(link
);
107 log_link_error_errno(link
, r
, "Failed to configure ipv4ll route: %m");
108 link_enter_failed(link
);
114 static int ipv4ll_address_claimed(sd_ipv4ll
*ll
, Link
*link
) {
115 _cleanup_(address_freep
) Address
*ll_addr
= NULL
;
116 struct in_addr address
;
122 link
->ipv4ll_address
= false;
123 link
->ipv4ll_route
= false;
125 r
= sd_ipv4ll_get_address(ll
, &address
);
131 log_link_debug(link
, "IPv4 link-local claim %u.%u.%u.%u",
132 ADDRESS_FMT_VAL(address
));
134 r
= address_new(&ll_addr
);
138 ll_addr
->family
= AF_INET
;
139 ll_addr
->in_addr
.in
= address
;
140 ll_addr
->prefixlen
= 16;
141 ll_addr
->broadcast
.s_addr
= ll_addr
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> ll_addr
->prefixlen
);
142 ll_addr
->scope
= RT_SCOPE_LINK
;
144 r
= address_configure(ll_addr
, link
, ipv4ll_address_handler
, false);
151 static void ipv4ll_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
152 Link
*link
= userdata
;
156 assert(link
->network
);
158 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
162 case SD_IPV4LL_EVENT_STOP
:
163 r
= ipv4ll_address_lost(link
);
165 link_enter_failed(link
);
169 case SD_IPV4LL_EVENT_CONFLICT
:
170 r
= ipv4ll_address_lost(link
);
172 link_enter_failed(link
);
176 r
= sd_ipv4ll_restart(ll
);
178 log_link_warning(link
, "Could not acquire IPv4 link-local address");
180 case SD_IPV4LL_EVENT_BIND
:
181 r
= ipv4ll_address_claimed(ll
, link
);
183 log_link_error(link
, "Failed to configure ipv4ll address: %m");
184 link_enter_failed(link
);
189 log_link_warning(link
, "IPv4 link-local unknown event: %d", event
);
194 int ipv4ll_configure(Link
*link
) {
199 assert(link
->network
);
200 assert(link
->network
->link_local
& ADDRESS_FAMILY_IPV4
);
203 r
= sd_ipv4ll_new(&link
->ipv4ll
);
208 if (link
->sd_device
&&
209 net_get_unique_predictable_data(link
->sd_device
, &seed
) >= 0) {
210 r
= sd_ipv4ll_set_address_seed(link
->ipv4ll
, seed
);
215 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
219 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
223 r
= sd_ipv4ll_set_ifindex(link
->ipv4ll
, link
->ifindex
);
227 r
= sd_ipv4ll_set_callback(link
->ipv4ll
, ipv4ll_handler
, link
);