1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013-2014 Tom Gundersen <teg@jklm.no>
6 #include <netinet/ether.h>
9 #include "network-internal.h"
10 #include "networkd-address.h"
11 #include "networkd-manager.h"
12 #include "networkd-link.h"
14 static int ipv4ll_address_lost(Link
*link
) {
15 _cleanup_(address_freep
) Address
*address
= NULL
;
16 _cleanup_(route_freep
) Route
*route
= NULL
;
22 link
->ipv4ll_route
= false;
23 link
->ipv4ll_address
= false;
25 r
= sd_ipv4ll_get_address(link
->ipv4ll
, &addr
);
29 log_link_debug(link
, "IPv4 link-local release %u.%u.%u.%u", ADDRESS_FMT_VAL(addr
));
31 r
= address_new(&address
);
33 log_link_error_errno(link
, r
, "Could not allocate address: %m");
37 address
->family
= AF_INET
;
38 address
->in_addr
.in
= addr
;
39 address
->prefixlen
= 16;
40 address
->scope
= RT_SCOPE_LINK
;
42 address_remove(address
, link
, link_address_remove_handler
);
44 r
= route_new(&route
);
46 log_link_error_errno(link
, r
, "Could not allocate route: %m");
50 route
->family
= AF_INET
;
51 route
->scope
= RT_SCOPE_LINK
;
52 route
->priority
= IPV4LL_ROUTE_METRIC
;
54 route_remove(route
, link
, link_route_remove_handler
);
56 link_check_ready(link
);
61 static int ipv4ll_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
62 _cleanup_(link_unrefp
) Link
*link
= userdata
;
66 assert(!link
->ipv4ll_route
);
68 r
= sd_netlink_message_get_errno(m
);
69 if (r
< 0 && r
!= -EEXIST
) {
70 log_link_error_errno(link
, r
, "could not set ipv4ll route: %m");
71 link_enter_failed(link
);
74 link
->ipv4ll_route
= true;
76 if (link
->ipv4ll_address
== true)
77 link_check_ready(link
);
82 static int ipv4ll_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, void *userdata
) {
83 _cleanup_(link_unrefp
) Link
*link
= userdata
;
87 assert(!link
->ipv4ll_address
);
89 r
= sd_netlink_message_get_errno(m
);
90 if (r
< 0 && r
!= -EEXIST
) {
91 log_link_error_errno(link
, r
, "could not set ipv4ll address: %m");
92 link_enter_failed(link
);
94 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
96 link
->ipv4ll_address
= true;
98 if (link
->ipv4ll_route
)
99 link_check_ready(link
);
104 static int ipv4ll_address_claimed(sd_ipv4ll
*ll
, Link
*link
) {
105 _cleanup_(address_freep
) Address
*ll_addr
= NULL
;
106 _cleanup_(route_freep
) Route
*route
= NULL
;
107 struct in_addr address
;
113 r
= sd_ipv4ll_get_address(ll
, &address
);
119 log_link_debug(link
, "IPv4 link-local claim %u.%u.%u.%u",
120 ADDRESS_FMT_VAL(address
));
122 r
= address_new(&ll_addr
);
126 ll_addr
->family
= AF_INET
;
127 ll_addr
->in_addr
.in
= address
;
128 ll_addr
->prefixlen
= 16;
129 ll_addr
->broadcast
.s_addr
= ll_addr
->in_addr
.in
.s_addr
| htobe32(0xfffffffflu
>> ll_addr
->prefixlen
);
130 ll_addr
->scope
= RT_SCOPE_LINK
;
132 r
= address_configure(ll_addr
, link
, ipv4ll_address_handler
, false);
136 link
->ipv4ll_address
= false;
138 r
= route_new(&route
);
142 route
->family
= AF_INET
;
143 route
->scope
= RT_SCOPE_LINK
;
144 route
->protocol
= RTPROT_STATIC
;
145 route
->priority
= IPV4LL_ROUTE_METRIC
;
147 r
= route_configure(route
, link
, ipv4ll_route_handler
);
151 link
->ipv4ll_route
= false;
156 static void ipv4ll_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
157 Link
*link
= userdata
;
161 assert(link
->network
);
163 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
167 case SD_IPV4LL_EVENT_STOP
:
168 r
= ipv4ll_address_lost(link
);
170 link_enter_failed(link
);
174 case SD_IPV4LL_EVENT_CONFLICT
:
175 r
= ipv4ll_address_lost(link
);
177 link_enter_failed(link
);
181 r
= sd_ipv4ll_restart(ll
);
183 log_link_warning(link
, "Could not acquire IPv4 link-local address");
185 case SD_IPV4LL_EVENT_BIND
:
186 r
= ipv4ll_address_claimed(ll
, link
);
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
);
207 r
= sd_ipv4ll_new(&link
->ipv4ll
);
212 if (link
->udev_device
) {
213 r
= net_get_unique_predictable_data(link
->udev_device
, &seed
);
215 r
= sd_ipv4ll_set_address_seed(link
->ipv4ll
, seed
);
221 r
= sd_ipv4ll_attach_event(link
->ipv4ll
, NULL
, 0);
225 r
= sd_ipv4ll_set_mac(link
->ipv4ll
, &link
->mac
);
229 r
= sd_ipv4ll_set_ifindex(link
->ipv4ll
, link
->ifindex
);
233 r
= sd_ipv4ll_set_callback(link
->ipv4ll
, ipv4ll_handler
, link
);