Debian Codegraph @

src/network/networkd-ipv4acd.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h> /* IFF_LOOPBACK */
4 #include <net/if_arp.h> /* ARPHRD_ETHER */
5
6 #include "sd-dhcp-client.h"
7 #include "sd-ipv4acd.h"
8
9 #include "hashmap.h"
10 #include "ipvlan.h"
11 #include "networkd-address.h"
12 #include "networkd-dhcp4.h"
13 #include "networkd-ipv4acd.h"
14 #include "networkd-link.h"
15 #include "networkd-manager.h"
16 #include "string-util.h"
17
18 DEFINE_PRIVATE_HASH_OPS_FULL(
19 ipv4acd_hash_ops,
20 Address,
21 address_hash_func,
22 address_compare_func,
23 address_unref,
24 sd_ipv4acd,
25 sd_ipv4acd_unref);
26
27 bool link_ipv4acd_supported(Link *link) {
28 assert(link);
29
30 if (link->flags & IFF_LOOPBACK)
31 return false;
32
33 /* ARPHRD_INFINIBAND seems to potentially support IPv4ACD.
34 * But currently sd-ipv4acd only supports ARPHRD_ETHER. */
35 if (link->iftype != ARPHRD_ETHER)
36 return false;
37
38 if (link->hw_addr.length != ETH_ALEN)
39 return false;
40
41 if (ether_addr_is_null(&link->hw_addr.ether))
42 return false;
43
44 if (streq_ptr(link->kind, "vrf"))
45 return false;
46
47 /* L3 or L3S mode do not support ARP. */
48 if (IN_SET(link_get_ipvlan_mode(link), NETDEV_IPVLAN_MODE_L3, NETDEV_IPVLAN_MODE_L3S))
49 return false;
50
51 return true;
52 }
53
54 static bool address_ipv4acd_enabled(Link *link, const Address *address) {
55 assert(link);
56 assert(address);
57
58 if (address->family != AF_INET)
59 return false;
60
61 if (!FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV4))
62 return false;
63
64 /* Currently, only static and DHCP4 addresses are supported. */
65 if (!IN_SET(address->source, NETWORK_CONFIG_SOURCE_STATIC, NETWORK_CONFIG_SOURCE_DHCP4))
66 return false;
67
68 return link_ipv4acd_supported(link);
69 }
70
71 bool link_ipv4acd_enabled(Link *link) {
72 assert(link);
73 assert(link->network);
74
75 Address *address;
76 ORDERED_HASHMAP_FOREACH(address, link->network->addresses_by_section)
77 if (address_ipv4acd_enabled(link, address))
78 return true;
79
80 return false;
81 }
82
83 bool ipv4acd_bound(Link *link, const Address *address) {
84 sd_ipv4acd *acd;
85
86 assert(link);
87 assert(address);
88
89 if (address->family != AF_INET)
90 return true;
91
92 acd = hashmap_get(link->ipv4acd_by_address, address);
93 if (!acd)
94 return true;
95
96 return sd_ipv4acd_is_bound(acd) > 0;
97 }
98
99 static int static_ipv4acd_address_remove(Link *link, Address *address, bool on_conflict) {
100 int r;
101
102 assert(link);
103 assert(address);
104
105 if (!address_exists(address))
106 return 0; /* Not assigned. */
107
108 if (on_conflict)
109 log_link_warning(link, "Dropping address %s, as an address conflict was detected.", IN4_ADDR_TO_STRING(&address->in_addr.in));
110 else
111 log_link_debug(link, "Removing address %s, as the ACD client is stopped.", IN4_ADDR_TO_STRING(&address->in_addr.in));
112
113 /* Do not call address_remove_and_cancel() here. Otherwise, the request is cancelled, and the
114 * interface may be in configured state without the address. */
115 r = address_remove(address, link);
116 if (r < 0)
117 return log_link_warning_errno(link, r, "Failed to remove address %s: %m", IN4_ADDR_TO_STRING(&address->in_addr.in));
118
119 return 0;
120 }
121
122 static int dhcp4_address_on_conflict(Link *link) {
123 int r;
124
125 assert(link);
126 assert(link->dhcp_client);
127
128 log_link_warning(link, "Dropping DHCPv4 lease, as an address conflict was detected.");
129
130 r = sd_dhcp_client_send_decline(link->dhcp_client);
131 if (r < 0)
132 log_link_warning_errno(link, r, "Failed to send DHCP DECLINE, ignoring: %m");
133
134 if (!link->dhcp_lease)
135 return 0;
136
137 r = dhcp4_lease_lost(link);
138 if (r < 0)
139 return log_link_warning_errno(link, r, "Failed to drop DHCPv4 lease: %m");
140
141 /* It is not necessary to call address_remove() here, as dhcp4_lease_lost() removes the address. */
142 return 0;
143 }
144
145 static void on_acd(sd_ipv4acd *acd, int event, void *userdata) {
146 Link *link = ASSERT_PTR(userdata);
147 Address *address = NULL;
148 int r;
149
150 assert(acd);
151
152 void *val, *key;
153 HASHMAP_FOREACH_KEY(val, key, link->ipv4acd_by_address)
154 if (val == acd) {
155 (void) address_get(link, key, &address);
156 break;
157 }
158
159 if (!address)
160 return;
161
162 switch (event) {
163 case SD_IPV4ACD_EVENT_STOP:
164 if (address->source == NETWORK_CONFIG_SOURCE_STATIC) {
165 r = static_ipv4acd_address_remove(link, address, /* on_conflict= */ false);
166 if (r < 0)
167 link_enter_failed(link);
168 }
169
170 /* We have nothing to do for DHCPv4 lease here, as the dhcp client is already stopped
171 * when stopping the ipv4acd client. See link_stop_engines(). */
172 break;
173
174 case SD_IPV4ACD_EVENT_BIND:
175 log_link_debug(link, "Successfully claimed address %s", IN4_ADDR_TO_STRING(&address->in_addr.in));
176 break;
177
178 case SD_IPV4ACD_EVENT_CONFLICT:
179 log_link_warning(link, "Dropping address %s, as an address conflict was detected.", IN4_ADDR_TO_STRING(&address->in_addr.in));
180
181 if (address->source == NETWORK_CONFIG_SOURCE_STATIC)
182 r = static_ipv4acd_address_remove(link, address, /* on_conflict= */ true);
183 else
184 r = dhcp4_address_on_conflict(link);
185 if (r < 0)
186 link_enter_failed(link);
187 break;
188
189 default:
190 assert_not_reached();
191 }
192 }
193
194 static int ipv4acd_check_mac(sd_ipv4acd *acd, const struct ether_addr *mac, void *userdata) {
195 Manager *m = ASSERT_PTR(userdata);
196 struct hw_addr_data hw_addr;
197
198 assert(mac);
199
200 hw_addr = (struct hw_addr_data) {
201 .length = ETH_ALEN,
202 .ether = *mac,
203 };
204
205 return link_get_by_hw_addr(m, &hw_addr, NULL) >= 0;
206 }
207
208 static int ipv4acd_start_one(Link *link, sd_ipv4acd *acd) {
209 assert(link);
210 assert(acd);
211
212 if (sd_ipv4acd_is_running(acd))
213 return 0;
214
215 if (!link_has_carrier(link))
216 return 0;
217
218 return sd_ipv4acd_start(acd, /* reset_conflicts= */ true);
219 }
220
221 int ipv4acd_configure(Link *link, const Address *address) {
222 _cleanup_(sd_ipv4acd_unrefp) sd_ipv4acd *acd = NULL;
223 _cleanup_(address_unrefp) Address *a = NULL;
224 sd_ipv4acd *existing;
225 int r;
226
227 assert(link);
228 assert(link->manager);
229 assert(link->network);
230 assert(address);
231
232 if (address->family != AF_INET)
233 return 0;
234
235 existing = hashmap_get(link->ipv4acd_by_address, address);
236
237 if (!address_ipv4acd_enabled(link, address))
238 return sd_ipv4acd_stop(existing);
239
240 if (existing)
241 return ipv4acd_start_one(link, existing);
242
243 log_link_debug(link, "Configuring IPv4ACD for address %s.", IN4_ADDR_TO_STRING(&address->in_addr.in));
244
245 r = address_new(&a);
246 if (r < 0)
247 return r;
248
249 a->family = AF_INET;
250 a->in_addr = address->in_addr;
251 a->in_addr_peer = address->in_addr_peer;
252 a->prefixlen = address->prefixlen;
253
254 r = sd_ipv4acd_new(&acd);
255 if (r < 0)
256 return r;
257
258 r = sd_ipv4acd_attach_event(acd, link->manager->event, 0);
259 if (r < 0)
260 return r;
261
262 r = sd_ipv4acd_set_ifindex(acd, link->ifindex);
263 if (r < 0)
264 return r;
265
266 r = sd_ipv4acd_set_mac(acd, &link->hw_addr.ether);
267 if (r < 0)
268 return r;
269
270 r = sd_ipv4acd_set_address(acd, &address->in_addr.in);
271 if (r < 0)
272 return r;
273
274 r = sd_ipv4acd_set_timeout(acd, link->network->ipv4_dad_timeout_usec);
275 if (r < 0)
276 return r;
277
278 r = sd_ipv4acd_set_callback(acd, on_acd, link);
279 if (r < 0)
280 return r;
281
282 r = sd_ipv4acd_set_check_mac_callback(acd, ipv4acd_check_mac, link->manager);
283 if (r < 0)
284 return r;
285
286 r = ipv4acd_start_one(link, acd);
287 if (r < 0)
288 return r;
289
290 r = hashmap_ensure_put(&link->ipv4acd_by_address, &ipv4acd_hash_ops, a, acd);
291 if (r < 0)
292 return r;
293
294 TAKE_PTR(a);
295 TAKE_PTR(acd);
296 return 0;
297 }
298
299 void ipv4acd_detach(Link *link, const Address *address) {
300 assert(link);
301 assert(address);
302
303 if (address->family != AF_INET)
304 return;
305
306 Address *a;
307 sd_ipv4acd_unref(hashmap_remove2(link->ipv4acd_by_address, address, (void**) &a));
308 address_unref(a);
309 }
310
311 int ipv4acd_update_mac(Link *link) {
312 sd_ipv4acd *acd;
313 int r;
314
315 assert(link);
316
317 if (link->hw_addr.length != ETH_ALEN)
318 return 0;
319 if (ether_addr_is_null(&link->hw_addr.ether))
320 return 0;
321
322 HASHMAP_FOREACH(acd, link->ipv4acd_by_address) {
323 r = sd_ipv4acd_set_mac(acd, &link->hw_addr.ether);
324 if (r < 0)
325 return r;
326 }
327
328 return 0;
329 }
330
331 int ipv4acd_start(Link *link) {
332 sd_ipv4acd *acd;
333 int r;
334
335 assert(link);
336
337 HASHMAP_FOREACH(acd, link->ipv4acd_by_address) {
338 r = ipv4acd_start_one(link, acd);
339 if (r < 0)
340 return r;
341 }
342
343 return 0;
344 }
345
346 int ipv4acd_stop(Link *link) {
347 sd_ipv4acd *acd;
348 int k, r = 0;
349
350 assert(link);
351
352 HASHMAP_FOREACH(acd, link->ipv4acd_by_address) {
353 k = sd_ipv4acd_stop(acd);
354 if (k < 0)
355 r = k;
356 }
357
358 return r;
359 }
360
361 int ipv4acd_set_ifname(Link *link) {
362 sd_ipv4acd *acd;
363 int r;
364
365 assert(link);
366
367 HASHMAP_FOREACH(acd, link->ipv4acd_by_address) {
368 r = sd_ipv4acd_set_ifname(acd, link->ifname);
369 if (r < 0)
370 return r;
371 }
372
373 return 0;
374 }