1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013-2014 Tom Gundersen <teg@jklm.no>
8 #include <netinet/ether.h>
11 #include "network-internal.h"
12 #include "networkd-address.h"
13 #include "networkd-manager.h"
14 #include "networkd-link.h"
16 static int ipv4ll_address_lost(Link
*link
) {
17 _cleanup_(address_freep
) Address
*address
= NULL
;
18 _cleanup_(route_freep
) Route
*route
= NULL
;
24 link
->ipv4ll_route
= false;
25 link
->ipv4ll_address
= false;
27 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &addr
);
31 log_link_debug(link
, "IPv4 link-local release %u.%u.%u.%u", ADDRESS_FMT_VAL(addr
));
33 r
= address_new(&address
);
35 log_link_error_errno(link
, r
, "Could not allocate address: %m");
39 address
->family
= AF_INET
;
40 address
->in_addr
.in
= addr
;
41 address
->prefixlen
= 16;
42 address
->scope
= RT_SCOPE_LINK
;
44 address_remove(address
, link
, link_address_remove_handler
);
46 r
= route_new(&route
);
48 log_link_error_errno(link
, r
, "Could not allocate route: %m");
52 route
->family
= AF_INET
;
53 route
->scope
= RT_SCOPE_LINK
;
54 route
->priority
= IPV4LL_ROUTE_METRIC
;
56 route_remove(route
, link
, link_route_remove_handler
);
58 link_check_ready(link
);
63 static int ipv4ll_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
64 _cleanup_(link_unrefp
) Link
*link
= userdata
;
68 assert(!link
->ipv4ll_route
);
70 r
= sd_netlink_message_get_errno(m
);
71 if (r
< 0 && r
!= -EEXIST
) {
72 log_link_error_errno(link
, r
, "could not set ipv4ll route: %m");
73 link_enter_failed(link
);
76 link
->ipv4ll_route
= true;
78 if (link
->ipv4ll_address
== true)
79 link_check_ready(link
);
84 static int ipv4ll_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
85 _cleanup_(link_unrefp
) Link
*link
= userdata
;
89 assert(!link
->ipv4ll_address
);
91 r
= sd_netlink_message_get_errno(m
);
92 if (r
< 0 && r
!= -EEXIST
) {
93 log_link_error_errno(link
, r
, "could not set ipv4ll address: %m");
94 link_enter_failed(link
);
96 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
98 link
->ipv4ll_address
= true;
100 if (link
->ipv4ll_route
)
101 link_check_ready(link
);
106 static int ipv4ll_address_claimed(sd_ipv4ll
*ll
, Link
*link
) {
107 _cleanup_(address_freep
) Address
*ll_addr
= NULL
;
108 _cleanup_(route_freep
) Route
*route
= NULL
;
109 struct in_addr address
;
115 r
= sd_ipv4ll_get_address(ll
, &address
);
121 log_link_debug(link
, "IPv4 link-local claim %u.%u.%u.%u",
122 ADDRESS_FMT_VAL(address
));
124 r
= address_new(&ll_addr
);
128 ll_addr
->family
= AF_INET
;
129 ll_addr
->in_addr
.in
= address
;
130 ll_addr
->prefixlen
= 16;
131 ll_addr
->broadcast
.s_addr
= ll_addr
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> ll_addr
->prefixlen
);
132 ll_addr
->scope
= RT_SCOPE_LINK
;
134 r
= address_configure(ll_addr
, link
, ipv4ll_address_handler
, false);
138 link
->ipv4ll_address
= false;
140 r
= route_new(&route
);
144 route
->family
= AF_INET
;
145 route
->scope
= RT_SCOPE_LINK
;
146 route
->protocol
= RTPROT_STATIC
;
147 route
->priority
= IPV4LL_ROUTE_METRIC
;
149 r
= route_configure(route
, link
, ipv4ll_route_handler
);
153 link
->ipv4ll_route
= false;
158 static void ipv4ll_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
159 Link
*link
= userdata
;
163 assert(link
->network
);
165 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
169 case SD_IPV4LL_EVENT_STOP
:
170 r
= ipv4ll_address_lost(link
);
172 link_enter_failed(link
);
176 case SD_IPV4LL_EVENT_CONFLICT
:
177 r
= ipv4ll_address_lost(link
);
179 link_enter_failed(link
);
183 r
= sd_ipv4ll_restart(ll
);
185 log_link_warning(link
, "Could not acquire IPv4 link-local address");
187 case SD_IPV4LL_EVENT_BIND
:
188 r
= ipv4ll_address_claimed(ll
, link
);
190 link_enter_failed(link
);
195 log_link_warning(link
, "IPv4 link-local unknown event: %d", event
);
200 int ipv4ll_configure(Link
*link
) {
205 assert(link
->network
);
206 assert(link
->network
->link_local
& ADDRESS_FAMILY_IPV4
);
209 r
= sd_ipv4ll_new(&link
->ipv4ll
);
214 if (link
->udev_device
) {
215 r
= net_get_unique_predictable_data(link
->udev_device
, &seed
);
217 r
= sd_ipv4ll_set_address_seed(link
->ipv4ll
, seed
);
223 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
227 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
231 r
= sd_ipv4ll_set_ifindex(link
->ipv4ll
, link
->ifindex
);
235 r
= sd_ipv4ll_set_callback(link
->ipv4ll
, ipv4ll_handler
, link
);