Debian Codegraph @

src/network/networkd-link.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if.h>
4 #include <linux/if_arp.h>
5 #include <linux/if_link.h>
6 #include <linux/netdevice.h>
7 #include <net/if.h>
8 #include <sys/socket.h>
9 #include <unistd.h>
10
11 #include "sd-bus.h"
12 #include "sd-dhcp-client.h"
13 #include "sd-dhcp-relay.h"
14 #include "sd-dhcp-server.h"
15 #include "sd-dhcp6-client.h"
16 #include "sd-dhcp6-lease.h"
17 #include "sd-ipv4ll.h"
18 #include "sd-lldp-rx.h"
19 #include "sd-ndisc.h"
20 #include "sd-netlink.h"
21 #include "sd-radv.h"
22 #include "sd-varlink.h"
23
24 #include "alloc-util.h"
25 #include "arphrd-util.h"
26 #include "bitfield.h"
27 #include "device-private.h"
28 #include "device-util.h"
29 #include "dns-domain.h"
30 #include "errno-util.h"
31 #include "ethtool-util.h"
32 #include "event-util.h"
33 #include "fs-util.h"
34 #include "glyph-util.h"
35 #include "logarithm.h"
36 #include "netif-util.h"
37 #include "netlink-util.h"
38 #include "networkd-address.h"
39 #include "networkd-address-label.h"
40 #include "networkd-bridge-fdb.h"
41 #include "networkd-bridge-mdb.h"
42 #include "networkd-bridge-vlan.h"
43 #include "networkd-dhcp-prefix-delegation.h"
44 #include "networkd-dhcp-relay.h"
45 #include "networkd-dhcp-server.h"
46 #include "networkd-dhcp4.h"
47 #include "networkd-dhcp6.h"
48 #include "networkd-ipv4acd.h"
49 #include "networkd-ipv4ll.h"
50 #include "networkd-ipv6-proxy-ndp.h"
51 #include "networkd-link.h"
52 #include "networkd-link-bus.h"
53 #include "networkd-lldp-tx.h"
54 #include "networkd-manager.h"
55 #include "networkd-ndisc.h"
56 #include "networkd-neighbor.h"
57 #include "networkd-nexthop.h"
58 #include "networkd-queue.h"
59 #include "networkd-radv.h"
60 #include "networkd-resolve-hook.h"
61 #include "networkd-route.h"
62 #include "networkd-route-util.h"
63 #include "networkd-routing-policy-rule.h"
64 #include "networkd-setlink.h"
65 #include "networkd-sriov.h"
66 #include "networkd-state-file.h"
67 #include "networkd-sysctl.h"
68 #include "networkd-wifi.h"
69 #include "networkd-wwan-bus.h"
70 #include "ordered-set.h"
71 #include "set.h"
72 #include "socket-util.h"
73 #include "string-table.h"
74 #include "string-util.h"
75 #include "strv.h"
76 #include "tc.h"
77 #include "udev-util.h"
78
79 void link_required_operstate_for_online(Link *link, LinkOperationalStateRange *ret) {
80 assert(link);
81 assert(ret);
82
83 if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
84 /* If explicitly specified, use it as is. */
85 *ret = link->network->required_operstate_for_online;
86 else if (link->iftype == ARPHRD_CAN)
87 /* CAN devices do not support addressing, hence defaults to 'carrier'. */
88 *ret = (const LinkOperationalStateRange) {
89 .min = LINK_OPERSTATE_CARRIER,
90 .max = LINK_OPERSTATE_CARRIER,
91 };
92 else if (link->network && link->network->bond)
93 /* Bonding slaves do not support addressing. */
94 *ret = (const LinkOperationalStateRange) {
95 .min = LINK_OPERSTATE_ENSLAVED,
96 .max = LINK_OPERSTATE_ENSLAVED,
97 };
98 else if (STRPTR_IN_SET(link->kind, "batadv", "bond", "bridge", "vrf"))
99 /* Some of slave interfaces may be offline. */
100 *ret = (const LinkOperationalStateRange) {
101 .min = LINK_OPERSTATE_DEGRADED_CARRIER,
102 .max = LINK_OPERSTATE_ROUTABLE,
103 };
104 else
105 *ret = LINK_OPERSTATE_RANGE_DEFAULT;
106 }
107
108 AddressFamily link_required_family_for_online(Link *link) {
109 assert(link);
110
111 if (link->network && link->network->required_family_for_online >= 0)
112 return link->network->required_family_for_online;
113
114 if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
115 /* If RequiredForOnline= is explicitly specified, defaults to no. */
116 return ADDRESS_FAMILY_NO;
117
118 if (STRPTR_IN_SET(link->kind, "batadv", "bond", "bridge", "vrf"))
119 /* As the minimum required operstate for master interfaces is 'degraded-carrier',
120 * we should request an address assigned to the link for backward compatibility. */
121 return ADDRESS_FAMILY_YES;
122
123 return ADDRESS_FAMILY_NO;
124 }
125
126 bool link_ipv6_enabled(Link *link) {
127 assert(link);
128
129 if (!socket_ipv6_is_supported())
130 return false;
131
132 if (link->iftype == ARPHRD_CAN)
133 return false;
134
135 if (!link->network)
136 return false;
137
138 if (link->network->bond)
139 return false;
140
141 if (link_ipv6ll_enabled(link))
142 return true;
143
144 if (network_has_static_ipv6_configurations(link->network))
145 return true;
146
147 return false;
148 }
149
150 bool link_has_ipv6_connectivity(Link *link) {
151 LinkAddressState ipv6_address_state;
152
153 assert(link);
154
155 link_get_address_states(link, NULL, &ipv6_address_state, NULL);
156
157 switch (ipv6_address_state) {
158 case LINK_ADDRESS_STATE_ROUTABLE:
159 /* If the interface has a routable IPv6 address, then we assume yes. */
160 return true;
161
162 case LINK_ADDRESS_STATE_DEGRADED:
163 /* If the interface has only degraded IPv6 address (mostly, link-local address), then let's check
164 * there is an IPv6 default gateway. */
165 return link_has_default_gateway(link, AF_INET6);
166
167 case LINK_ADDRESS_STATE_OFF:
168 /* No IPv6 address. */
169 return false;
170
171 default:
172 assert_not_reached();
173 }
174 }
175
176 static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
177 assert(link);
178
179 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
180 return false;
181
182 if (!link->network)
183 return allow_unmanaged;
184
185 if (!link->network->configure_without_carrier) {
186 if (link->set_flags_messages > 0)
187 return false;
188
189 if (!link_has_carrier(link))
190 return false;
191 }
192
193 if (link->set_link_messages > 0)
194 return false;
195
196 if (!link->activated)
197 return false;
198
199 return true;
200 }
201
202 bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
203 return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
204 }
205
206 bool link_is_ready_to_configure_by_name(Manager *manager, const char *name, bool allow_unmanaged) {
207 assert(manager);
208 assert(name);
209
210 Link *link;
211 if (link_get_by_name(manager, name, &link) < 0)
212 return false;
213
214 return link_is_ready_to_configure(link, allow_unmanaged);
215 }
216
217 void link_ntp_settings_clear(Link *link) {
218 link->ntp = strv_free(link->ntp);
219 }
220
221 void link_dns_settings_clear(Link *link) {
222 if (link->n_dns != UINT_MAX)
223 for (unsigned i = 0; i < link->n_dns; i++)
224 in_addr_full_free(link->dns[i]);
225 link->dns = mfree(link->dns);
226 link->n_dns = UINT_MAX;
227
228 link->search_domains = ordered_set_free(link->search_domains);
229 link->route_domains = ordered_set_free(link->route_domains);
230
231 link->dns_default_route = -1;
232 link->llmnr = _RESOLVE_SUPPORT_INVALID;
233 link->mdns = _RESOLVE_SUPPORT_INVALID;
234 link->dnssec_mode = _DNSSEC_MODE_INVALID;
235 link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
236
237 link->dnssec_negative_trust_anchors = set_free(link->dnssec_negative_trust_anchors);
238 }
239
240 static void link_free_engines(Link *link) {
241 if (!link)
242 return;
243
244 link->dhcp_relay_interface = sd_dhcp_relay_interface_unref(link->dhcp_relay_interface);
245 link->dhcp_relay_interface_compat = sd_dhcp_relay_interface_unref(link->dhcp_relay_interface_compat);
246 link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
247
248 link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
249 link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
250 link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
251
252 link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
253 link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
254
255 link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
256
257 link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
258
259 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
260 link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
261
262 link->ndisc = sd_ndisc_unref(link->ndisc);
263 link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire);
264 ndisc_flush(link);
265
266 link->radv = sd_radv_unref(link->radv);
267 }
268
269 static Link* link_free(Link *link) {
270 assert(link);
271
272 (void) link_clear_sysctl_shadows(link);
273
274 link_ntp_settings_clear(link);
275 link_dns_settings_clear(link);
276
277 link->neighbors = set_free(link->neighbors);
278 link->addresses = set_free(link->addresses);
279 link->qdiscs = set_free(link->qdiscs);
280 link->tclasses = set_free(link->tclasses);
281
282 link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
283
284 link_free_engines(link);
285
286 set_free(link->sr_iov_virt_port_ifindices);
287 free(link->ifname);
288 strv_free(link->alternative_names);
289 free(link->kind);
290 free(link->ssid);
291 free(link->previous_ssid);
292 free(link->driver);
293
294 unlink_and_free(link->state_file);
295
296 sd_device_unref(link->dev);
297 netdev_unref(link->netdev);
298
299 hashmap_free(link->bound_to_links);
300 hashmap_free(link->bound_by_links);
301
302 set_free(link->slaves);
303
304 network_unref(link->network);
305
306 sd_event_source_disable_unref(link->carrier_lost_timer);
307 sd_event_source_disable_unref(link->ipv6_mtu_wait_synced_event_source);
308
309 return mfree(link);
310 }
311
312 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
313
314 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
315 link_hash_ops,
316 void, trivial_hash_func, trivial_compare_func,
317 Link, link_unref);
318
319 int link_get_by_index(Manager *m, int ifindex, Link **ret) {
320 Link *link;
321
322 assert(m);
323
324 if (ifindex <= 0)
325 return -EINVAL;
326
327 link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
328 if (!link)
329 return -ENODEV;
330
331 if (ret)
332 *ret = link;
333 return 0;
334 }
335
336 int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
337 Link *link;
338
339 assert(m);
340 assert(ifname);
341
342 link = hashmap_get(m->links_by_name, ifname);
343 if (!link)
344 return -ENODEV;
345
346 if (ret)
347 *ret = link;
348 return 0;
349 }
350
351 int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
352 Link *link;
353
354 assert(m);
355 assert(hw_addr);
356
357 link = hashmap_get(m->links_by_hw_addr, hw_addr);
358 if (!link)
359 return -ENODEV;
360
361 if (ret)
362 *ret = link;
363 return 0;
364 }
365
366 int link_get_master(Link *link, Link **ret) {
367 assert(link);
368 assert(link->manager);
369 assert(ret);
370
371 if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
372 return -ENODEV;
373
374 return link_get_by_index(link->manager, link->master_ifindex, ret);
375 }
376
377 void link_set_state(Link *link, LinkState state) {
378 assert(link);
379
380 if (link->state == state)
381 return;
382
383 log_link_debug(link, "State changed: %s -> %s",
384 link_state_to_string(link->state),
385 link_state_to_string(state));
386
387 link->state = state;
388
389 link_send_changed(link, "AdministrativeState");
390 link_dirty(link);
391 }
392
393 int link_stop_engines(Link *link, bool may_keep_dynamic) {
394 int r, ret = 0;
395
396 assert(link);
397 assert(link->manager);
398 assert(link->manager->event);
399
400 bool keep_dynamic =
401 may_keep_dynamic &&
402 link->network &&
403 (link->manager->state == MANAGER_RESTARTING ||
404 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
405
406 if (!keep_dynamic) {
407 r = sd_dhcp_client_stop(link->dhcp_client);
408 if (r < 0)
409 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"));
410
411 r = sd_ipv4ll_stop(link->ipv4ll);
412 if (r < 0)
413 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"));
414
415 r = sd_dhcp6_client_stop(link->dhcp6_client);
416 if (r < 0)
417 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
418
419 r = dhcp_pd_remove(link, /* only_marked= */ false);
420 if (r < 0)
421 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not remove DHCPv6 PD addresses and routes: %m"));
422
423 r = ndisc_stop(link);
424 if (r < 0)
425 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
426
427 ndisc_flush(link);
428 }
429
430 r = sd_dhcp_relay_interface_stop(link->dhcp_relay_interface);
431 if (r < 0)
432 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCP relay agent: %m"));
433
434 r = sd_dhcp_relay_interface_stop(link->dhcp_relay_interface_compat);
435 if (r < 0)
436 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCP relay agent (compat): %m"));
437
438 r = sd_dhcp_server_stop(link->dhcp_server);
439 if (r < 0)
440 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 server: %m"));
441
442 r = sd_lldp_rx_stop(link->lldp_rx);
443 if (r < 0)
444 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Rx: %m"));
445
446 r = sd_lldp_tx_stop(link->lldp_tx);
447 if (r < 0)
448 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Tx: %m"));
449
450 r = ipv4acd_stop(link);
451 if (r < 0)
452 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 ACD client: %m"));
453
454 r = sd_radv_stop(link->radv);
455 if (r < 0)
456 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
457
458 return ret;
459 }
460
461 void link_enter_failed(Link *link) {
462 assert(link);
463
464 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
465 return;
466
467 log_link_warning(link, "Failed");
468
469 link_set_state(link, LINK_STATE_FAILED);
470
471 if (!ratelimit_below(&link->automatic_reconfigure_ratelimit)) {
472 log_link_warning(link, "The interface entered the failed state frequently, refusing to reconfigure it automatically.");
473 goto stop;
474 }
475
476 log_link_info(link, "Trying to reconfigure the interface.");
477 if (link_reconfigure(link, LINK_RECONFIGURE_UNCONDITIONALLY) > 0)
478 return;
479
480 stop:
481 (void) link_stop_engines(link, /* may_keep_dynamic= */ false);
482 }
483
484 void link_check_ready(Link *link) {
485 Address *a;
486
487 assert(link);
488
489 if (link->state == LINK_STATE_CONFIGURED)
490 return;
491
492 if (link->state != LINK_STATE_CONFIGURING)
493 return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
494
495 if (!link->network)
496 return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
497
498 if (!link->tc_configured)
499 return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
500
501 if (link->set_link_messages > 0)
502 return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
503
504 if (!link->activated)
505 return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
506
507 if (link->iftype == ARPHRD_CAN)
508 /* let's shortcut things for CAN which doesn't need most of checks below. */
509 goto ready;
510
511 if (!link->stacked_netdevs_created)
512 return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
513
514 if (!link->static_addresses_configured)
515 return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
516
517 if (!link->static_address_labels_configured)
518 return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
519
520 if (!link->static_bridge_fdb_configured)
521 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
522
523 if (!link->static_bridge_mdb_configured)
524 return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
525
526 if (!link->static_ipv6_proxy_ndp_configured)
527 return (void) log_link_debug(link, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__);
528
529 if (!link->static_neighbors_configured)
530 return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
531
532 if (!link->static_nexthops_configured)
533 return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
534
535 if (!link->static_routes_configured)
536 return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
537
538 if (!link->static_routing_policy_rules_configured)
539 return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
540
541 if (!link->sr_iov_configured)
542 return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
543
544 if (!link->bearer_configured)
545 return (void) log_link_debug(link, "%s(): Bearer has not been applied.", __func__);
546
547 /* IPv6LL is assigned after the link gains its carrier. */
548 if (!link->network->configure_without_carrier &&
549 link_ipv6ll_enabled(link) &&
550 !in6_addr_is_set(&link->ipv6ll_address))
551 return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
552
553 /* All static addresses must be ready. */
554 bool has_static_address = false;
555 SET_FOREACH(a, link->addresses) {
556 if (!IN_SET(a->source, NETWORK_CONFIG_SOURCE_STATIC, NETWORK_CONFIG_SOURCE_MODEM_MANAGER))
557 continue;
558 if (!address_is_ready(a))
559 return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
560 IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
561 has_static_address = true;
562 }
563
564 /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
565 if (has_static_address)
566 goto ready;
567
568 /* If no dynamic addressing protocol enabled, assume the interface is ready.
569 * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
570 if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
571 !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
572 (link->network->configure_without_carrier || !link_ndisc_enabled(link)))
573 goto ready;
574
575 bool ipv4ll_ready =
576 link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
577 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
578 bool dhcp4_ready =
579 link_dhcp4_enabled(link) && link->dhcp4_configured &&
580 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
581 bool dhcp6_ready =
582 link_dhcp6_enabled(link) && link->dhcp6_configured &&
583 (!link->network->dhcp6_use_address ||
584 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
585 bool dhcp_pd_ready =
586 link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
587 (!link->network->dhcp_pd_assign ||
588 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
589 bool ndisc_ready =
590 link_ndisc_enabled(link) && link->ndisc_configured &&
591 (!link->network->ndisc_use_autonomous_prefix ||
592 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
593
594 /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
595 if (dhcp_pd_is_uplink(link, link, /* accept_auto= */ false)) {
596 if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
597 sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
598 if (!link->dhcp4_configured)
599 return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
600 if (!dhcp_pd_ready)
601 return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
602 }
603
604 if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
605 sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
606 if (!link->dhcp6_configured)
607 return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
608 if (!dhcp_pd_ready)
609 return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
610 }
611 }
612
613 /* At least one dynamic addressing protocol is finished. */
614 if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
615 return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
616
617 log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
618 __func__,
619 yes_no(ipv4ll_ready),
620 yes_no(dhcp4_ready),
621 yes_no(dhcp6_ready),
622 yes_no(dhcp_pd_ready),
623 yes_no(ndisc_ready));
624
625 ready:
626 link_set_state(link, LINK_STATE_CONFIGURED);
627 }
628
629 static int link_request_static_configs(Link *link) {
630 int r;
631
632 assert(link);
633 assert(link->network);
634 assert(link->state != _LINK_STATE_INVALID);
635
636 r = link_request_static_addresses(link);
637 if (r < 0)
638 return r;
639
640 r = link_request_static_address_labels(link);
641 if (r < 0)
642 return r;
643
644 r = link_request_static_bridge_fdb(link);
645 if (r < 0)
646 return r;
647
648 r = link_request_static_bridge_mdb(link);
649 if (r < 0)
650 return r;
651
652 r = link_request_static_ipv6_proxy_ndp_addresses(link);
653 if (r < 0)
654 return r;
655
656 r = link_request_static_neighbors(link);
657 if (r < 0)
658 return r;
659
660 r = link_request_static_nexthops(link, false);
661 if (r < 0)
662 return r;
663
664 r = link_request_static_routes(link, false);
665 if (r < 0)
666 return r;
667
668 r = link_request_static_routing_policy_rules(link);
669 if (r < 0)
670 return r;
671
672 return 0;
673 }
674
675 int link_request_stacked_netdevs(Link *link, NetDevLocalAddressType type) {
676 NetDev *netdev;
677 int r;
678
679 assert(link);
680
681 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
682 return 0;
683
684 assert(link->network);
685
686 link->stacked_netdevs_created = false;
687
688 HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
689 if (!netdev_needs_reconfigure(netdev, type))
690 continue;
691
692 r = link_request_stacked_netdev(link, netdev);
693 if (r < 0)
694 return r;
695 }
696
697 if (link->create_stacked_netdev_messages == 0) {
698 link->stacked_netdevs_created = true;
699 link_check_ready(link);
700 }
701
702 return 0;
703 }
704
705 static int link_acquire_dynamic_ipv6_conf(Link *link) {
706 int r;
707
708 assert(link);
709
710 r = radv_start(link);
711 if (r < 0)
712 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
713
714 r = ndisc_start(link);
715 if (r < 0)
716 return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
717
718 r = dhcp6_start(link);
719 if (r < 0)
720 return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
721
722 return 0;
723 }
724
725 static int link_acquire_dynamic_ipv4_conf(Link *link) {
726 int r;
727
728 assert(link);
729 assert(link->manager);
730 assert(link->manager->event);
731
732 if (link->dhcp_client) {
733 r = dhcp4_start(link);
734 if (r < 0)
735 return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
736
737 log_link_debug(link, "Acquiring DHCPv4 lease.");
738
739 } else if (link->ipv4ll) {
740 if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
741 r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
742 if (r < 0)
743 return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
744 }
745
746 r = ipv4ll_start(link);
747 if (r < 0)
748 return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
749 if (r > 0)
750 log_link_debug(link, "Acquiring IPv4 link-local address.");
751 }
752
753 r = link_start_dhcp_relay(link);
754 if (r < 0)
755 return log_link_warning_errno(link, r, "Could not start DHCP relay agent: %m");
756
757 r = link_start_dhcp4_server(link);
758 if (r < 0)
759 return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
760
761 r = ipv4acd_start(link);
762 if (r < 0)
763 return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
764
765 return 0;
766 }
767
768 static int link_acquire_dynamic_conf(Link *link) {
769 int r;
770
771 assert(link);
772 assert(link->network);
773
774 r = link_acquire_dynamic_ipv4_conf(link);
775 if (r < 0)
776 return r;
777
778 if (in6_addr_is_set(&link->ipv6ll_address)) {
779 r = link_acquire_dynamic_ipv6_conf(link);
780 if (r < 0)
781 return r;
782 }
783
784 if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
785 /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
786 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
787 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
788 * below will be called in radv_start(). */
789 r = dhcp_request_prefix_delegation(link);
790 if (r < 0)
791 return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
792 }
793
794 if (link->lldp_tx) {
795 r = sd_lldp_tx_start(link->lldp_tx);
796 if (r < 0)
797 return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
798 }
799
800 if (link->lldp_rx) {
801 r = sd_lldp_rx_start(link->lldp_rx);
802 if (r < 0)
803 return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
804 }
805
806 return 0;
807 }
808
809 int link_ipv6ll_gained(Link *link) {
810 int r;
811
812 assert(link);
813
814 log_link_info(link, "Gained IPv6LL");
815
816 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
817 return 0;
818
819 r = link_acquire_dynamic_ipv6_conf(link);
820 if (r < 0)
821 return r;
822
823 r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
824 if (r < 0)
825 return r;
826
827 link_check_ready(link);
828 return 0;
829 }
830
831 int link_ipv6ll_lost(Link *link, const struct in6_addr *dropped_ipv6ll, bool has_replacement) {
832 int ret = 0, r;
833
834 assert(link);
835 assert(dropped_ipv6ll);
836
837 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
838 return 0;
839
840 log_link_info(link, "Lost IPv6LL address %s%s.",
841 IN6_ADDR_TO_STRING(dropped_ipv6ll),
842 has_replacement ? ", switching to alternate IPv6LL source" : "");
843
844 r = sd_dhcp6_client_stop(link->dhcp6_client);
845 if (r < 0)
846 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
847
848 /* DHCPv6 must be restarted to switch the client's source address, while NDisc and
849 * RADV can switch to the replacement IPv6LL in link_ipv6ll_gained() without flushing
850 * learned state. Keep link->ndisc_configured as-is in this path. */
851 if (has_replacement)
852 return ret;
853
854 r = ndisc_stop(link);
855 if (r < 0)
856 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
857 link->ndisc_configured = false;
858 ndisc_flush(link);
859
860 r = sd_radv_stop(link->radv);
861 if (r < 0)
862 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
863
864 r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
865 if (r < 0)
866 RET_GATHER(ret, log_link_warning_errno(link, r, "Could not reconfigure stacked netdevs after IPv6LL loss: %m"));
867
868 return ret;
869 }
870
871 int link_handle_bound_to_list(Link *link) {
872 bool required_up = false;
873 Link *l;
874
875 assert(link);
876
877 /* If at least one interface in bound_to_links has carrier, then make this interface up.
878 * If all interfaces in bound_to_links do not, then make this interface down. */
879
880 if (hashmap_isempty(link->bound_to_links))
881 return 0;
882
883 HASHMAP_FOREACH(l, link->bound_to_links)
884 if (link_has_carrier(l)) {
885 required_up = true;
886 break;
887 }
888
889 if (!required_up && link_is_up(link))
890 return link_request_to_bring_up_or_down(link, /* up= */ false);
891 if (required_up && !link_is_up(link))
892 return link_request_to_bring_up_or_down(link, /* up= */ true);
893
894 return 0;
895 }
896
897 static int link_handle_bound_by_list(Link *link) {
898 Link *l;
899 int r;
900
901 assert(link);
902
903 /* Update up or down state of interfaces which depend on this interface's carrier state. */
904
905 HASHMAP_FOREACH(l, link->bound_by_links) {
906 r = link_handle_bound_to_list(l);
907 if (r < 0)
908 return r;
909 }
910
911 return 0;
912 }
913
914 static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
915 int r;
916
917 assert(link);
918 assert(carrier);
919 assert(h);
920
921 if (link == carrier)
922 return 0;
923
924 if (hashmap_contains(*h, INT_TO_PTR(carrier->ifindex)))
925 return 0;
926
927 r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
928 if (r < 0)
929 return r;
930
931 link_dirty(link);
932
933 return 0;
934 }
935
936 static int link_new_bound_by_list(Link *link) {
937 Manager *m;
938 Link *carrier;
939 int r;
940
941 assert(link);
942 assert(link->manager);
943
944 m = link->manager;
945
946 HASHMAP_FOREACH(carrier, m->links_by_index) {
947 if (!carrier->network)
948 continue;
949
950 if (strv_isempty(carrier->network->bind_carrier))
951 continue;
952
953 if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
954 r = link_put_carrier(link, carrier, &link->bound_by_links);
955 if (r < 0)
956 return r;
957 }
958 }
959
960 HASHMAP_FOREACH(carrier, link->bound_by_links) {
961 r = link_put_carrier(carrier, link, &carrier->bound_to_links);
962 if (r < 0)
963 return r;
964 }
965
966 return 0;
967 }
968
969 static int link_new_bound_to_list(Link *link) {
970 Manager *m;
971 Link *carrier;
972 int r;
973
974 assert(link);
975 assert(link->manager);
976
977 if (!link->network)
978 return 0;
979
980 if (strv_isempty(link->network->bind_carrier))
981 return 0;
982
983 m = link->manager;
984
985 HASHMAP_FOREACH(carrier, m->links_by_index) {
986 if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
987 r = link_put_carrier(link, carrier, &link->bound_to_links);
988 if (r < 0)
989 return r;
990 }
991 }
992
993 HASHMAP_FOREACH(carrier, link->bound_to_links) {
994 r = link_put_carrier(carrier, link, &carrier->bound_by_links);
995 if (r < 0)
996 return r;
997 }
998
999 return 0;
1000 }
1001
1002 static void link_free_bound_to_list(Link *link) {
1003 bool updated = false;
1004 Link *bound_to;
1005
1006 assert(link);
1007
1008 while ((bound_to = hashmap_steal_first(link->bound_to_links))) {
1009 updated = true;
1010
1011 if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex)))
1012 link_dirty(bound_to);
1013 }
1014
1015 if (updated)
1016 link_dirty(link);
1017 }
1018
1019 static void link_free_bound_by_list(Link *link) {
1020 bool updated = false;
1021 Link *bound_by;
1022
1023 assert(link);
1024
1025 while ((bound_by = hashmap_steal_first(link->bound_by_links))) {
1026 updated = true;
1027
1028 if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) {
1029 link_dirty(bound_by);
1030 link_handle_bound_to_list(bound_by);
1031 }
1032 }
1033
1034 if (updated)
1035 link_dirty(link);
1036 }
1037
1038 static int link_append_to_master(Link *link) {
1039 Link *master;
1040 int r;
1041
1042 assert(link);
1043
1044 /* - The link may have no master.
1045 * - RTM_NEWLINK message about master interface may not be received yet. */
1046 if (link_get_master(link, &master) < 0)
1047 return 0;
1048
1049 r = set_ensure_put(&master->slaves, &link_hash_ops, link);
1050 if (r <= 0)
1051 return r;
1052
1053 link_ref(link);
1054 return 0;
1055 }
1056
1057 static void link_drop_from_master(Link *link) {
1058 Link *master;
1059
1060 assert(link);
1061
1062 if (!link->manager)
1063 return;
1064
1065 if (link_get_master(link, &master) < 0)
1066 return;
1067
1068 link_unref(set_remove(master->slaves, link));
1069 }
1070
1071 static int link_drop_requests(Link *link) {
1072 Request *req;
1073 int ret = 0;
1074
1075 assert(link);
1076 assert(link->manager);
1077
1078 ORDERED_SET_FOREACH(req, link->manager->request_queue) {
1079 if (req->link != link)
1080 continue;
1081
1082 /* If the request is already called, but its reply is not received, then we need to
1083 * drop the configuration (e.g. address) here. Note, if the configuration is known,
1084 * it will be handled later by link_drop_unmanaged_addresses() or so. */
1085 if (req->waiting_reply && link->state != LINK_STATE_LINGER)
1086 switch (req->type) {
1087 case REQUEST_TYPE_ADDRESS: {
1088 Address *address = ASSERT_PTR(req->userdata);
1089
1090 if (address_get(link, address, NULL) < 0)
1091 RET_GATHER(ret, address_remove(address, link));
1092 break;
1093 }
1094 case REQUEST_TYPE_NEIGHBOR: {
1095 Neighbor *neighbor = ASSERT_PTR(req->userdata);
1096
1097 if (neighbor_get(link, neighbor, NULL) < 0)
1098 RET_GATHER(ret, neighbor_remove(neighbor, link));
1099 break;
1100 }
1101 case REQUEST_TYPE_NEXTHOP: {
1102 NextHop *nexthop = ASSERT_PTR(req->userdata);
1103
1104 if (nexthop_get_by_id(link->manager, nexthop->id, NULL) < 0)
1105 RET_GATHER(ret, nexthop_remove(nexthop, link->manager));
1106 break;
1107 }
1108 case REQUEST_TYPE_ROUTE: {
1109 Route *route = ASSERT_PTR(req->userdata);
1110
1111 if (route_get(link->manager, route, NULL) < 0)
1112 RET_GATHER(ret, route_remove(route, link->manager));
1113 break;
1114 }
1115 default:
1116 ;
1117 }
1118
1119 request_detach(req);
1120 }
1121
1122 return ret;
1123 }
1124
1125 static Link *link_drop(Link *link) {
1126 if (!link)
1127 return NULL;
1128
1129 assert(link->manager);
1130
1131 bool notify = link_has_local_lease_domain(link);
1132
1133 link_set_state(link, LINK_STATE_LINGER);
1134
1135 /* Drop all references from other links and manager. Note that async netlink calls may have
1136 * references to the link, and they will be dropped when we receive replies. */
1137
1138 (void) link_drop_requests(link);
1139
1140 link_free_bound_to_list(link);
1141 link_free_bound_by_list(link);
1142
1143 link_clear_sr_iov_ifindices(link);
1144
1145 link_drop_from_master(link);
1146
1147 if (link->state_file)
1148 (void) unlink(link->state_file);
1149
1150 link_clean(link);
1151
1152 STRV_FOREACH(n, link->alternative_names)
1153 hashmap_remove_value(link->manager->links_by_name, *n, link);
1154 hashmap_remove_value(link->manager->links_by_name, link->ifname, link);
1155
1156 /* bonding master and its slaves have the same hardware address. */
1157 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
1158
1159 /* The following must be called at last. */
1160 hashmap_remove_value(link->manager->links_by_index, INT_TO_PTR(link->ifindex), link);
1161
1162 if (notify)
1163 manager_notify_hook_filters(link->manager);
1164
1165 return link_unref(link);
1166 }
1167
1168 static int link_drop_unmanaged_config(Link *link) {
1169 int r;
1170
1171 assert(link);
1172 assert(link->state == LINK_STATE_CONFIGURING);
1173 assert(link->manager);
1174
1175 r = link_drop_unmanaged_routes(link);
1176 RET_GATHER(r, link_drop_unmanaged_nexthops(link));
1177 RET_GATHER(r, link_drop_unmanaged_addresses(link));
1178 RET_GATHER(r, link_drop_unmanaged_neighbors(link));
1179 RET_GATHER(r, link_drop_unmanaged_routing_policy_rules(link));
1180
1181 return r;
1182 }
1183
1184 static int link_drop_static_config(Link *link) {
1185 int r;
1186
1187 assert(link);
1188 assert(link->manager);
1189
1190 r = link_drop_static_routes(link);
1191 RET_GATHER(r, link_drop_static_nexthops(link));
1192 RET_GATHER(r, link_drop_static_addresses(link));
1193 RET_GATHER(r, link_drop_static_neighbors(link));
1194 RET_GATHER(r, link_drop_static_routing_policy_rules(link));
1195
1196 return r;
1197 }
1198
1199 static int link_drop_dynamic_config(Link *link, Network *network) {
1200 int r;
1201
1202 assert(link);
1203 assert(link->network);
1204
1205 /* Drop unnecessary dynamic configurations gracefully, e.g. drop DHCP lease in the case that
1206 * previously DHCP=yes and now DHCP=no, but keep DHCP lease when DHCP setting is unchanged. */
1207
1208 r = link_drop_ndisc_config(link, network);
1209 RET_GATHER(r, link_drop_radv_config(link, network)); /* Stop before dropping DHCP-PD prefixes. */
1210 RET_GATHER(r, link_drop_ipv4ll_config(link, network)); /* Stop before DHCPv4 client. */
1211 RET_GATHER(r, link_drop_dhcp4_config(link, network));
1212 RET_GATHER(r, link_drop_dhcp6_config(link, network));
1213 RET_GATHER(r, link_drop_dhcp_pd_config(link, network));
1214 link->dhcp_relay_interface = sd_dhcp_relay_interface_unref(link->dhcp_relay_interface);
1215 link->dhcp_relay_interface_compat = sd_dhcp_relay_interface_unref(link->dhcp_relay_interface_compat);
1216 link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
1217 link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx); /* TODO: keep the received neighbors. */
1218 link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
1219
1220 /* Even if we do not release DHCP lease or so, reset 'configured' flags. Otherwise, e.g. if
1221 * previously UseDNS= was disabled but is now enabled, link will enter configured state before
1222 * expected DNS servers being acquired. */
1223 link->ipv4ll_address_configured = false;
1224 link->dhcp4_configured = false;
1225 link->dhcp6_configured = false;
1226 link->dhcp_pd_configured = false;
1227 link->ndisc_configured = false;
1228
1229 return r;
1230 }
1231
1232 static int link_configure(Link *link) {
1233 int r;
1234
1235 assert(link);
1236 assert(link->network);
1237 assert(link->state == LINK_STATE_INITIALIZED);
1238
1239 link_set_state(link, LINK_STATE_CONFIGURING);
1240
1241 r = link_drop_unmanaged_config(link);
1242 if (r < 0)
1243 return r;
1244
1245 r = link_new_bound_to_list(link);
1246 if (r < 0)
1247 return r;
1248
1249 r = link_request_traffic_control(link);
1250 if (r < 0)
1251 return r;
1252
1253 r = link_configure_mtu(link);
1254 if (r < 0)
1255 return r;
1256
1257 if (link->iftype == ARPHRD_CAN) {
1258 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1259 r = link_request_to_set_can(link);
1260 if (r < 0)
1261 return r;
1262
1263 return link_request_to_activate(link);
1264 }
1265
1266 r = link_request_sr_iov_vfs(link);
1267 if (r < 0)
1268 return r;
1269
1270 r = link_set_sysctl(link);
1271 if (r < 0)
1272 return r;
1273
1274 r = link_request_to_set_mac(link, /* allow_retry= */ true);
1275 if (r < 0)
1276 return r;
1277
1278 r = link_request_to_set_ipoib(link);
1279 if (r < 0)
1280 return r;
1281
1282 r = link_request_to_set_flags(link);
1283 if (r < 0)
1284 return r;
1285
1286 r = link_request_to_set_group(link);
1287 if (r < 0)
1288 return r;
1289
1290 r = link_request_to_set_addrgen_mode(link);
1291 if (r < 0)
1292 return r;
1293
1294 r = link_request_to_set_master(link);
1295 if (r < 0)
1296 return r;
1297
1298 r = link_request_stacked_netdevs(link, _NETDEV_LOCAL_ADDRESS_TYPE_INVALID);
1299 if (r < 0)
1300 return r;
1301
1302 r = link_request_to_set_bond(link);
1303 if (r < 0)
1304 return r;
1305
1306 r = link_request_to_set_bridge(link);
1307 if (r < 0)
1308 return r;
1309
1310 r = link_request_to_set_bridge_vlan(link);
1311 if (r < 0)
1312 return r;
1313
1314 r = link_request_to_activate(link);
1315 if (r < 0)
1316 return r;
1317
1318 r = ipv4ll_configure(link);
1319 if (r < 0)
1320 return r;
1321
1322 r = link_request_dhcp4_client(link);
1323 if (r < 0)
1324 return r;
1325
1326 r = link_request_dhcp6_client(link);
1327 if (r < 0)
1328 return r;
1329
1330 r = link_request_ndisc(link);
1331 if (r < 0)
1332 return r;
1333
1334 r = link_request_dhcp_relay(link);
1335 if (r < 0)
1336 return r;
1337
1338 r = link_request_dhcp_server(link);
1339 if (r < 0)
1340 return r;
1341
1342 r = link_request_radv(link);
1343 if (r < 0)
1344 return r;
1345
1346 r = link_lldp_rx_configure(link);
1347 if (r < 0)
1348 return r;
1349
1350 r = link_lldp_tx_configure(link);
1351 if (r < 0)
1352 return r;
1353
1354 r = link_request_static_configs(link);
1355 if (r < 0)
1356 return r;
1357
1358 r = link_modem_reconfigure(link);
1359 if (r < 0)
1360 return r;
1361
1362 if (!link_has_carrier(link))
1363 return 0;
1364
1365 return link_acquire_dynamic_conf(link);
1366 }
1367
1368 static int link_get_network(Link *link, Network **ret) {
1369 Network *network;
1370 int r;
1371
1372 assert(link);
1373 assert(link->manager);
1374 assert(ret);
1375
1376 ORDERED_HASHMAP_FOREACH(network, link->manager->networks) {
1377 bool warn = false;
1378
1379 r = net_match_config(
1380 &network->match,
1381 link->dev,
1382 &link->hw_addr,
1383 &link->permanent_hw_addr,
1384 link->driver,
1385 link->iftype,
1386 link->kind,
1387 link->ifname,
1388 link->alternative_names,
1389 link->wlan_iftype,
1390 link->ssid,
1391 &link->bssid);
1392 if (r < 0)
1393 return r;
1394 if (r == 0)
1395 continue;
1396
1397 if (network->match.ifname && link->dev) {
1398 uint8_t name_assign_type;
1399 if (device_get_sysattr_u8(link->dev, "name_assign_type", &name_assign_type) >= 0)
1400 warn = name_assign_type == NET_NAME_ENUM;
1401 }
1402
1403 log_link_full(link, warn ? LOG_WARNING : LOG_DEBUG,
1404 "Found matching .network file%s: %s",
1405 warn ? ", based on potentially unpredictable interface name" : "",
1406 network->filename);
1407
1408 if (network->unmanaged)
1409 return -ENOENT;
1410
1411 *ret = network;
1412 return 0;
1413 }
1414
1415 return log_link_debug_errno(link, SYNTHETIC_ERRNO(ENOENT), "No matching .network found.");
1416 }
1417
1418 static void link_enter_unmanaged(Link *link) {
1419 assert(link);
1420
1421 if (link->state == LINK_STATE_UNMANAGED)
1422 return;
1423
1424 bool notify = link_has_local_lease_domain(link);
1425
1426 log_link_full(link, link->state == LINK_STATE_INITIALIZED ? LOG_DEBUG : LOG_INFO,
1427 "Unmanaging interface.");
1428
1429 (void) link_stop_engines(link, /* may_keep_dynamic= */ false);
1430 (void) link_drop_requests(link);
1431 (void) link_drop_static_config(link);
1432
1433 /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1434 * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1435 * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1436 * map here, as it depends on .network files assigned to other links. */
1437 link_free_bound_to_list(link);
1438 link_free_engines(link);
1439
1440 link->network = network_unref(link->network);
1441 link_set_state(link, LINK_STATE_UNMANAGED);
1442
1443 if (notify)
1444 manager_notify_hook_filters(link->manager);
1445 }
1446
1447 static int link_managed_by_us(Link *link) {
1448 int r;
1449
1450 assert(link);
1451
1452 if (!link->dev)
1453 return true;
1454
1455 const char *s;
1456 r = sd_device_get_property_value(link->dev, "ID_NET_MANAGED_BY", &s);
1457 if (r == -ENOENT)
1458 return true;
1459 if (r < 0)
1460 return log_link_warning_errno(link, r, "Failed to get ID_NET_MANAGED_BY udev property: %m");
1461
1462 if (streq(s, "io.systemd.Network"))
1463 return true;
1464
1465 if (link->state == LINK_STATE_UNMANAGED)
1466 return false; /* Already in unmanaged state */
1467
1468 log_link_debug(link, "Interface is requested to be managed by '%s', unmanaging the interface.", s);
1469 link_set_state(link, LINK_STATE_UNMANAGED);
1470 return false;
1471 }
1472
1473 int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
1474 Network *network = NULL;
1475 int r;
1476
1477 assert(link);
1478 assert(link->manager);
1479
1480 link_assign_netdev(link);
1481
1482 if (link->manager->state != MANAGER_RUNNING)
1483 return 0;
1484
1485 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_LINGER))
1486 return 0;
1487
1488 r = link_managed_by_us(link);
1489 if (r <= 0)
1490 return r;
1491
1492 r = link_get_network(link, &network);
1493 if (r == -ENOENT) {
1494 link_enter_unmanaged(link);
1495 return 0;
1496 }
1497 if (r < 0)
1498 return r;
1499
1500 if (link->network == network && !FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY))
1501 return 0;
1502
1503 _cleanup_free_ char *joined = strv_join(network->dropins, ", ");
1504 if (link->network)
1505 log_link_info(link, "Reconfiguring with %s%s%s%s.",
1506 network->filename,
1507 isempty(joined) ? "" : " (dropins: ",
1508 joined,
1509 isempty(joined) ? "" : ")");
1510 else
1511 log_link_info(link, "Configuring with %s%s%s%s.",
1512 network->filename,
1513 isempty(joined) ? "" : " (dropins: ",
1514 joined,
1515 isempty(joined) ? "" : ")");
1516
1517 /* Dropping configurations based on the old .network file. */
1518 r = link_drop_requests(link);
1519 if (r < 0)
1520 return r;
1521
1522 /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1523 * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1524 * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1525 * map here, as it depends on .network files assigned to other links. */
1526 link_free_bound_to_list(link);
1527
1528 _cleanup_(network_unrefp) Network *old_network = TAKE_PTR(link->network);
1529
1530 /* Then, apply new .network file */
1531 link->network = network_ref(network);
1532
1533 if (FLAGS_SET(network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC) ||
1534 !FLAGS_SET(flags, LINK_RECONFIGURE_CLEANLY)) {
1535 /* To make 'networkctl reconfigure INTERFACE' work safely for an interface whose new .network
1536 * file has KeepConfiguration=dynamic or yes, even if a clean reconfiguration is requested,
1537 * drop only unnecessary or possibly being changed dynamic configurations here. */
1538 r = link_drop_dynamic_config(link, old_network);
1539 if (r < 0)
1540 return r;
1541 } else {
1542 /* Otherwise, stop DHCP client and friends unconditionally, and drop all dynamic
1543 * configurations like DHCP address and routes. */
1544 r = link_stop_engines(link, /* may_keep_dynamic= */ false);
1545 if (r < 0)
1546 return r;
1547
1548 /* Free DHCP client and friends. */
1549 link_free_engines(link);
1550 }
1551
1552 link_update_operstate(link, true);
1553 link_dirty(link);
1554
1555 link_set_state(link, LINK_STATE_INITIALIZED);
1556 link->activated = false;
1557
1558 r = link_configure(link);
1559 if (r < 0)
1560 return r;
1561
1562 return 1;
1563 }
1564
1565 typedef struct LinkReconfigurationData {
1566 Manager *manager;
1567 Link *link;
1568 LinkReconfigurationFlag flags;
1569 sd_bus_message *message;
1570 sd_varlink *varlink;
1571 unsigned *counter;
1572 } LinkReconfigurationData;
1573
1574 static LinkReconfigurationData* link_reconfiguration_data_free(LinkReconfigurationData *data) {
1575 if (!data)
1576 return NULL;
1577
1578 link_unref(data->link);
1579 sd_bus_message_unref(data->message);
1580 sd_varlink_unref(data->varlink);
1581
1582 return mfree(data);
1583 }
1584
1585 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkReconfigurationData*, link_reconfiguration_data_free);
1586
1587 static void link_reconfiguration_data_destroy_callback(LinkReconfigurationData *data) {
1588 int r;
1589
1590 assert(data);
1591 assert(!data->message || !data->varlink); /* D-Bus and Varlink callers are mutually exclusive */
1592
1593 if (data->counter) {
1594 assert(*data->counter > 0);
1595 (*data->counter)--;
1596 }
1597
1598 if (!data->counter || *data->counter == 0) {
1599 /* Update the state files before replying. Otherwise, systemd-networkd-wait-online following
1600 * networkctl reload/reconfigure may read an outdated state file and wrongly consider an
1601 * interface already in the configured state. */
1602 (void) manager_clean_all(data->manager);
1603
1604 if (data->message) {
1605 r = sd_bus_reply_method_return(data->message, NULL);
1606 if (r < 0)
1607 log_warning_errno(r, "Failed to reply for DBus method, ignoring: %m");
1608 }
1609
1610 if (data->varlink) {
1611 r = sd_varlink_reply(data->varlink, NULL);
1612 if (r < 0)
1613 log_warning_errno(r, "Failed to reply to Varlink request, ignoring: %m");
1614 }
1615 }
1616
1617 link_reconfiguration_data_free(data);
1618 }
1619
1620 static int link_reconfigure_handler(sd_netlink *rtnl, sd_netlink_message *m, LinkReconfigurationData *data) {
1621 Link *link = ASSERT_PTR(ASSERT_PTR(data)->link);
1622 int r;
1623
1624 r = link_getlink_handler_internal(rtnl, m, link, "Failed to update link state");
1625 if (r <= 0)
1626 return r;
1627
1628 r = link_reconfigure_impl(link, data->flags);
1629 if (r < 0) {
1630 link_enter_failed(link);
1631 return 0;
1632 }
1633
1634 return r;
1635 }
1636
1637 int link_reconfigure_full(Link *link, LinkReconfigurationFlag flags, sd_bus_message *message, sd_varlink *varlink, unsigned *counter) {
1638 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1639 _cleanup_(link_reconfiguration_data_freep) LinkReconfigurationData *data = NULL;
1640 int r;
1641
1642 assert(link);
1643 assert(link->manager);
1644 assert(link->manager->rtnl);
1645 assert(!message || !varlink); /* D-Bus and Varlink callers are mutually exclusive */
1646
1647 /* When the link is in the pending or initialized state, link_reconfigure_impl() will be called later
1648 * by link_initialized() or link_initialized_and_synced(). To prevent the function from being called
1649 * multiple times simultaneously, let's refuse to reconfigure the interface here in such cases. */
1650 if (IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED, LINK_STATE_LINGER))
1651 return 0; /* 0 means no-op. */
1652
1653 data = new(LinkReconfigurationData, 1);
1654 if (!data) {
1655 r = -ENOMEM;
1656 goto failed;
1657 }
1658
1659 *data = (LinkReconfigurationData) {
1660 .manager = link->manager,
1661 .link = link_ref(link),
1662 .flags = flags,
1663 .message = sd_bus_message_ref(message), /* message may be NULL, but _ref() works fine. */
1664 .varlink = sd_varlink_ref(varlink), /* varlink may be NULL, but _ref() works fine. */
1665 .counter = counter,
1666 };
1667
1668 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
1669 if (r < 0)
1670 goto failed;
1671
1672 r = netlink_call_async(link->manager->rtnl, NULL, req,
1673 link_reconfigure_handler,
1674 link_reconfiguration_data_destroy_callback, data);
1675 if (r < 0)
1676 goto failed;
1677
1678 TAKE_PTR(data);
1679 if (counter)
1680 (*counter)++;
1681
1682 if (link->state == LINK_STATE_FAILED)
1683 link_set_state(link, LINK_STATE_INITIALIZED);
1684
1685 return 1; /* 1 means the interface will be reconfigured, and bus method will be replied later. */
1686
1687 failed:
1688 log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
1689 link_enter_failed(link);
1690 return r;
1691 }
1692
1693 static int link_initialized_and_synced(Link *link) {
1694 int r;
1695
1696 assert(link);
1697 assert(link->manager);
1698
1699 if (link->state == LINK_STATE_PENDING) {
1700 log_link_debug(link, "Link state is up-to-date");
1701 link_set_state(link, LINK_STATE_INITIALIZED);
1702
1703 r = link_new_bound_by_list(link);
1704 if (r < 0)
1705 return r;
1706
1707 r = link_handle_bound_by_list(link);
1708 if (r < 0)
1709 return r;
1710 }
1711
1712 return link_reconfigure_impl(link, /* flags= */ 0);
1713 }
1714
1715 static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
1716 int r;
1717
1718 r = link_getlink_handler_internal(rtnl, m, link, "Failed to wait for the interface to be initialized");
1719 if (r <= 0)
1720 return r;
1721
1722 r = link_initialized_and_synced(link);
1723 if (r < 0)
1724 link_enter_failed(link);
1725
1726 return 0;
1727 }
1728
1729 static int link_initialized(Link *link, sd_device *device) {
1730 int r;
1731
1732 assert(link);
1733 assert(device);
1734
1735 /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1736 * or sysattrs) may be outdated. */
1737 device_unref_and_replace_new_ref(link->dev, device);
1738
1739 r = link_managed_by_us(link);
1740 if (r <= 0)
1741 return r;
1742
1743 if (link->dhcp_client) {
1744 r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev);
1745 if (r < 0)
1746 log_link_warning_errno(link, r, "Failed to attach device to DHCPv4 client, ignoring: %m");
1747 }
1748
1749 if (link->dhcp6_client) {
1750 r = sd_dhcp6_client_attach_device(link->dhcp6_client, link->dev);
1751 if (r < 0)
1752 log_link_warning_errno(link, r, "Failed to attach device to DHCPv6 client, ignoring: %m");
1753 }
1754
1755 r = link_set_sr_iov_ifindices(link);
1756 if (r < 0)
1757 log_link_warning_errno(link, r, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
1758
1759 if (link->state != LINK_STATE_PENDING)
1760 return link_reconfigure(link, /* flags= */ 0);
1761
1762 log_link_debug(link, "udev initialized link");
1763
1764 /* udev has initialized the link, but we don't know if we have yet
1765 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1766 * when it returns we know that the pending NEWLINKs have already been
1767 * processed and that we are up-to-date */
1768
1769 return link_call_getlink(link, link_initialized_handler);
1770 }
1771
1772 int link_check_initialized(Link *link) {
1773 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
1774 int r;
1775
1776 assert(link);
1777
1778 if (!udev_available())
1779 return link_initialized_and_synced(link);
1780
1781 /* udev should be around */
1782 r = sd_device_new_from_ifindex(&device, link->ifindex);
1783 if (r < 0) {
1784 log_link_debug_errno(link, r, "Could not find device, waiting for device initialization: %m");
1785 return 0;
1786 }
1787
1788 r = device_is_processed(device);
1789 if (r < 0)
1790 return log_link_warning_errno(link, r, "Could not determine whether the device is processed by udevd: %m");
1791 if (r == 0) {
1792 /* not yet ready */
1793 log_link_debug(link, "link pending udev initialization...");
1794 return 0;
1795 }
1796
1797 r = device_is_renaming(device);
1798 if (r < 0)
1799 return log_link_warning_errno(link, r, "Failed to determine the device is being renamed: %m");
1800 if (r > 0) {
1801 log_link_debug(link, "Interface is being renamed, pending initialization.");
1802 return 0;
1803 }
1804
1805 return link_initialized(link, device);
1806 }
1807
1808 int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t action) {
1809 int r, ifindex;
1810 Link *link;
1811
1812 assert(m);
1813 assert(device);
1814
1815 r = sd_device_get_ifindex(device, &ifindex);
1816 if (r < 0)
1817 return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
1818
1819 r = link_get_by_index(m, ifindex, &link);
1820 if (r < 0) {
1821 /* This error is not critical, as the corresponding rtnl message may be received later. */
1822 log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
1823 return 0;
1824 }
1825
1826 /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1827 * object here. It will be removed only when rtnetlink says so. */
1828 if (action == SD_DEVICE_REMOVE) {
1829 link->dev = sd_device_unref(link->dev);
1830 return 0;
1831 }
1832
1833 r = device_is_renaming(device);
1834 if (r < 0)
1835 return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
1836 if (r > 0) {
1837 log_device_debug(device, "Device is renaming, waiting for the interface to be renamed.");
1838 /* TODO:
1839 * What happens when a device is initialized, then soon renamed after that? When we detect
1840 * such, maybe we should cancel or postpone all queued requests for the interface. */
1841 return 0;
1842 }
1843
1844 r = link_initialized(link, device);
1845 if (r < 0)
1846 link_enter_failed(link);
1847
1848 return 0;
1849 }
1850
1851 static int link_carrier_gained(Link *link) {
1852 LinkReconfigurationFlag flags = 0;
1853 int r;
1854
1855 assert(link);
1856
1857 log_link_info(link, "Gained carrier");
1858
1859 r = event_source_disable(link->carrier_lost_timer);
1860 if (r < 0)
1861 log_link_warning_errno(link, r, "Failed to disable carrier lost timer, ignoring: %m");
1862
1863 /* Process BindCarrier= setting specified by other interfaces. This is independent of the .network
1864 * file assigned to this interface, but depends on .network files assigned to other interfaces.
1865 * Hence, this can and should be called earlier. */
1866 r = link_handle_bound_by_list(link);
1867 if (r < 0)
1868 return r;
1869
1870 /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1871 * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1872 * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1873 * restarted, and the correct network information will be gained.
1874 *
1875 * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1876 * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1877 * already assigned to the interface (in that case, the .network file does not have the SSID=
1878 * setting in the [Match] section), and the interface is already being configured. Of course,
1879 * there may exist another .network file with higher priority and a matching SSID= setting. But
1880 * in that case, link_reconfigure_impl() can handle that without any flags.
1881 *
1882 * For non-wireless interfaces, we have no way to detect the connected network change. So,
1883 * we do not set any flags here. Note, both ssid and previous_ssid are NULL in that case. */
1884 if (link->previous_ssid && !streq_ptr(link->previous_ssid, link->ssid))
1885 flags |= LINK_RECONFIGURE_UNCONDITIONALLY | LINK_RECONFIGURE_CLEANLY;
1886 link->previous_ssid = mfree(link->previous_ssid);
1887
1888 /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1889 * profile wants to match on it with SSID= in its [Match] section.
1890 */
1891 if (IN_SET(link->wlan_iftype, NL80211_IFTYPE_AP, NL80211_IFTYPE_P2P_GO)) {
1892 r = link_get_wlan_interface(link);
1893 if (r < 0)
1894 return r;
1895 }
1896
1897 /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1898 * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1899 * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1900 r = link_reconfigure_impl(link, flags);
1901 if (r != 0)
1902 return r;
1903
1904 if (link->iftype == ARPHRD_CAN)
1905 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1906 return 0;
1907
1908 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) {
1909 r = link_acquire_dynamic_conf(link);
1910 if (r < 0)
1911 return r;
1912
1913 r = link_request_static_configs(link);
1914 if (r < 0)
1915 return r;
1916 }
1917
1918 return 0;
1919 }
1920
1921 static int link_carrier_lost_impl(Link *link) {
1922 int ret = 0;
1923
1924 assert(link);
1925
1926 link->previous_ssid = mfree(link->previous_ssid);
1927
1928 ret = link_handle_bound_by_list(link);
1929
1930 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1931 return ret;
1932
1933 if (!link->network)
1934 return ret;
1935
1936 RET_GATHER(ret, link_stop_engines(link, /* may_keep_dynamic= */ false));
1937 RET_GATHER(ret, link_drop_static_config(link));
1938
1939 return ret;
1940 }
1941
1942 static int link_carrier_lost_handler(sd_event_source *s, uint64_t usec, void *userdata) {
1943 Link *link = ASSERT_PTR(userdata);
1944 int r;
1945
1946 r = link_carrier_lost_impl(link);
1947 if (r < 0) {
1948 log_link_warning_errno(link, r, "Failed to process carrier lost event: %m");
1949 link_enter_failed(link);
1950 }
1951
1952 return 0;
1953 }
1954
1955 static int link_carrier_lost(Link *link) {
1956 uint16_t dhcp_mtu;
1957 usec_t usec;
1958
1959 assert(link);
1960
1961 log_link_info(link, "Lost carrier");
1962
1963 if (link->iftype == ARPHRD_CAN)
1964 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1965 usec = 0;
1966
1967 else if (!link->network)
1968 usec = 0;
1969
1970 else if (link->network->ignore_carrier_loss_set)
1971 /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1972 usec = link->network->ignore_carrier_loss_usec;
1973
1974 else if (link->network->bond && link->wlan_iftype > 0)
1975 /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1976 * carrier to be lost. See #19832. */
1977 usec = 3 * USEC_PER_SEC;
1978
1979 else if (link->network->dhcp_use_mtu &&
1980 link->dhcp_lease &&
1981 sd_dhcp_lease_get_mtu(link->dhcp_lease, &dhcp_mtu) >= 0 &&
1982 dhcp_mtu != link->original_mtu)
1983 /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1984 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1985 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1986 usec = 5 * USEC_PER_SEC;
1987
1988 else
1989 /* Otherwise, use the implied default value. */
1990 usec = link->network->ignore_carrier_loss_usec;
1991
1992 if (usec == USEC_INFINITY)
1993 return 0;
1994
1995 if (usec == 0)
1996 return link_carrier_lost_impl(link);
1997
1998 return event_reset_time_relative(link->manager->event,
1999 &link->carrier_lost_timer,
2000 CLOCK_BOOTTIME,
2001 usec,
2002 0,
2003 link_carrier_lost_handler,
2004 link,
2005 0,
2006 "link-carrier-loss",
2007 true);
2008 }
2009
2010 static int link_admin_state_up(Link *link) {
2011 assert(link);
2012
2013 /* This is called every time an interface admin state changes to up;
2014 * specifically, when IFF_UP flag changes from unset to set. */
2015
2016 log_link_info(link, "Link UP");
2017
2018 if (!link->network)
2019 return 0;
2020
2021 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_DOWN) {
2022 log_link_info(link, "Activation policy is \"always-down\", forcing link down.");
2023 return link_request_to_bring_up_or_down(link, /* up= */ false);
2024 }
2025
2026 /* We set the ipv6 mtu after the device mtu, but the kernel resets
2027 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
2028 (void) link_set_ipv6_mtu(link, LOG_INFO);
2029
2030 return 0;
2031 }
2032
2033 static int link_admin_state_down(Link *link) {
2034 assert(link);
2035
2036 log_link_info(link, "Link DOWN");
2037
2038 link_forget_nexthops(link);
2039 link_forget_routes(link);
2040
2041 if (!link->network)
2042 return 0;
2043
2044 if (link->activated && link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
2045 log_link_info(link, "Activation policy is \"always-up\", forcing link up.");
2046 return link_request_to_bring_up_or_down(link, /* up= */ true);
2047 }
2048
2049 return 0;
2050 }
2051
2052 static bool link_is_enslaved(Link *link) {
2053 if (link->flags & IFF_SLAVE)
2054 return true;
2055
2056 if (link->master_ifindex > 0)
2057 return true;
2058
2059 return false;
2060 }
2061
2062 void link_update_operstate(Link *link, bool also_update_master) {
2063 LinkOperationalState operstate;
2064 LinkCarrierState carrier_state;
2065 LinkAddressState ipv4_address_state, ipv6_address_state, address_state;
2066 LinkOnlineState online_state;
2067 _cleanup_strv_free_ char **p = NULL;
2068 bool changed = false;
2069
2070 assert(link);
2071
2072 if (link->kernel_operstate == IF_OPER_DORMANT)
2073 carrier_state = LINK_CARRIER_STATE_DORMANT;
2074 else if (link_has_carrier(link)) {
2075 if (link_is_enslaved(link))
2076 carrier_state = LINK_CARRIER_STATE_ENSLAVED;
2077 else
2078 carrier_state = LINK_CARRIER_STATE_CARRIER;
2079 } else if (link_is_up(link))
2080 carrier_state = LINK_CARRIER_STATE_NO_CARRIER;
2081 else
2082 carrier_state = LINK_CARRIER_STATE_OFF;
2083
2084 if (carrier_state >= LINK_CARRIER_STATE_CARRIER) {
2085 Link *slave;
2086
2087 SET_FOREACH(slave, link->slaves) {
2088 link_update_operstate(slave, false);
2089
2090 if (slave->carrier_state < LINK_CARRIER_STATE_CARRIER)
2091 carrier_state = LINK_CARRIER_STATE_DEGRADED_CARRIER;
2092 }
2093 }
2094
2095 link_get_address_states(link, &ipv4_address_state, &ipv6_address_state, &address_state);
2096
2097 /* Mapping of address and carrier state vs operational state
2098 * carrier state
2099 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
2100 * ------------------------------------------------------------------------------
2101 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
2102 * address_state degraded | off | no-carrier | dormant | degraded | degraded | enslaved
2103 * routable | off | no-carrier | dormant | routable | routable | routable
2104 */
2105
2106 if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_ROUTABLE)
2107 operstate = LINK_OPERSTATE_ROUTABLE;
2108 else if (carrier_state == LINK_CARRIER_STATE_DEGRADED_CARRIER && address_state == LINK_ADDRESS_STATE_DEGRADED)
2109 operstate = LINK_OPERSTATE_DEGRADED;
2110 else if (carrier_state < LINK_CARRIER_STATE_CARRIER || address_state == LINK_ADDRESS_STATE_OFF)
2111 operstate = (LinkOperationalState) carrier_state;
2112 else if (address_state == LINK_ADDRESS_STATE_ROUTABLE)
2113 operstate = LINK_OPERSTATE_ROUTABLE;
2114 else if (carrier_state == LINK_CARRIER_STATE_CARRIER)
2115 operstate = LINK_OPERSTATE_DEGRADED;
2116 else
2117 operstate = LINK_OPERSTATE_ENSLAVED;
2118
2119 LinkOperationalStateRange req;
2120 link_required_operstate_for_online(link, &req);
2121
2122 /* Only determine online state for managed links with RequiredForOnline=yes */
2123 if (!link->network || !link->network->required_for_online)
2124 online_state = _LINK_ONLINE_STATE_INVALID;
2125
2126 else if (!operational_state_is_in_range(operstate, &req))
2127 online_state = LINK_ONLINE_STATE_OFFLINE;
2128
2129 else {
2130 AddressFamily required_family = link_required_family_for_online(link);
2131 bool needs_ipv4 = required_family & ADDRESS_FAMILY_IPV4;
2132 bool needs_ipv6 = required_family & ADDRESS_FAMILY_IPV6;
2133
2134 /* The operational state is within the range required for online.
2135 * If a particular address family is also required, we might revert
2136 * to offline in the blocks below. */
2137 online_state = LINK_ONLINE_STATE_ONLINE;
2138
2139 if (req.min >= LINK_OPERSTATE_DEGRADED) {
2140 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_DEGRADED)
2141 online_state = LINK_ONLINE_STATE_OFFLINE;
2142 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_DEGRADED)
2143 online_state = LINK_ONLINE_STATE_OFFLINE;
2144 }
2145
2146 if (req.min >= LINK_OPERSTATE_ROUTABLE) {
2147 if (needs_ipv4 && ipv4_address_state < LINK_ADDRESS_STATE_ROUTABLE)
2148 online_state = LINK_ONLINE_STATE_OFFLINE;
2149 if (needs_ipv6 && ipv6_address_state < LINK_ADDRESS_STATE_ROUTABLE)
2150 online_state = LINK_ONLINE_STATE_OFFLINE;
2151 }
2152 }
2153
2154 if (link->carrier_state != carrier_state) {
2155 link->carrier_state = carrier_state;
2156 changed = true;
2157 if (strv_extend(&p, "CarrierState") < 0)
2158 log_oom();
2159 }
2160
2161 if (link->address_state != address_state) {
2162 link->address_state = address_state;
2163 changed = true;
2164 if (strv_extend(&p, "AddressState") < 0)
2165 log_oom();
2166 }
2167
2168 if (link->ipv4_address_state != ipv4_address_state) {
2169 link->ipv4_address_state = ipv4_address_state;
2170 changed = true;
2171 if (strv_extend(&p, "IPv4AddressState") < 0)
2172 log_oom();
2173 }
2174
2175 if (link->ipv6_address_state != ipv6_address_state) {
2176 link->ipv6_address_state = ipv6_address_state;
2177 changed = true;
2178 if (strv_extend(&p, "IPv6AddressState") < 0)
2179 log_oom();
2180 }
2181
2182 if (link->operstate != operstate) {
2183 link->operstate = operstate;
2184 changed = true;
2185 if (strv_extend(&p, "OperationalState") < 0)
2186 log_oom();
2187 }
2188
2189 if (link->online_state != online_state) {
2190 link->online_state = online_state;
2191 changed = true;
2192 if (strv_extend(&p, "OnlineState") < 0)
2193 log_oom();
2194 }
2195
2196 if (p)
2197 link_send_changed_strv(link, p);
2198 if (changed)
2199 link_dirty(link);
2200
2201 if (also_update_master) {
2202 Link *master;
2203
2204 if (link_get_master(link, &master) >= 0)
2205 link_update_operstate(master, true);
2206 }
2207 }
2208
2209 bool link_has_carrier(Link *link) {
2210 assert(link);
2211 return netif_has_carrier(link->kernel_operstate, link->flags);
2212 }
2213
2214 bool link_is_up(Link *link) {
2215 assert(link);
2216 return FLAGS_SET(link->flags, IFF_UP);
2217 }
2218
2219 bool link_multicast_enabled(Link *link) {
2220 assert(link);
2221
2222 /* If Multicast= is specified, use the value. */
2223 if (link->network && link->network->multicast >= 0)
2224 return link->network->multicast;
2225
2226 /* Otherwise, return the current state. */
2227 return FLAGS_SET(link->flags, IFF_MULTICAST);
2228 }
2229
2230 #define FLAG_STRING(string, flag, old, new) \
2231 (((old ^ new) & flag) \
2232 ? ((old & flag) ? (" -" string) : (" +" string)) \
2233 : "")
2234
2235 static int link_update_flags(Link *link, sd_netlink_message *message) {
2236 bool link_was_admin_up, had_carrier;
2237 uint8_t operstate;
2238 unsigned flags;
2239 int r;
2240
2241 assert(link);
2242 assert(message);
2243
2244 r = sd_rtnl_message_link_get_flags(message, &flags);
2245 if (r < 0)
2246 return log_link_debug_errno(link, r, "rtnl: failed to read link flags: %m");
2247
2248 r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
2249 if (r == -ENODATA)
2250 /* If we got a message without operstate, assume the state was unchanged. */
2251 operstate = link->kernel_operstate;
2252 else if (r < 0)
2253 return log_link_debug_errno(link, r, "rtnl: failed to read operational state: %m");
2254
2255 if (link->flags == flags && link->kernel_operstate == operstate)
2256 return 0;
2257
2258 if (link->flags != flags) {
2259 unsigned unknown_flags, unknown_flags_added, unknown_flags_removed;
2260
2261 log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
2262 FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
2263 FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
2264 FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
2265 FLAG_STRING("UP", IFF_UP, link->flags, flags),
2266 FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
2267 FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
2268 FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
2269 FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
2270 FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
2271 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
2272 FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
2273 FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
2274 FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
2275 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
2276 FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
2277 FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
2278 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
2279 FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
2280 FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
2281
2282 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
2283 IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
2284 IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
2285 IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
2286 IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
2287 IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
2288 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
2289 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
2290
2291 if (unknown_flags_added)
2292 log_link_debug(link, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added);
2293
2294 if (unknown_flags_removed)
2295 log_link_debug(link, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed);
2296 }
2297
2298 link_was_admin_up = link_is_up(link);
2299 had_carrier = link_has_carrier(link);
2300
2301 link->flags = flags;
2302 link->kernel_operstate = operstate;
2303
2304 link_update_operstate(link, true);
2305
2306 r = 0;
2307
2308 if (!link_was_admin_up && link_is_up(link))
2309 r = link_admin_state_up(link);
2310 else if (link_was_admin_up && !link_is_up(link))
2311 r = link_admin_state_down(link);
2312 if (r < 0)
2313 return r;
2314
2315 if (!had_carrier && link_has_carrier(link))
2316 r = link_carrier_gained(link);
2317 else if (had_carrier && !link_has_carrier(link))
2318 r = link_carrier_lost(link);
2319 if (r < 0)
2320 return r;
2321
2322 return 0;
2323 }
2324
2325 static int link_update_master(Link *link, sd_netlink_message *message) {
2326 int master_ifindex, r;
2327
2328 assert(link);
2329 assert(message);
2330
2331 r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &master_ifindex);
2332 if (r == -ENODATA)
2333 master_ifindex = 0; /* no master interface */
2334 else if (r < 0)
2335 return log_link_debug_errno(link, r, "rtnl: failed to read master ifindex: %m");
2336
2337 if (master_ifindex == link->ifindex)
2338 master_ifindex = 0;
2339
2340 if (master_ifindex != link->master_ifindex) {
2341 if (link->master_ifindex == 0)
2342 log_link_debug(link, "Attached to master interface: %i", master_ifindex);
2343 else if (master_ifindex == 0)
2344 log_link_debug(link, "Detached from master interface: %i", link->master_ifindex);
2345 else
2346 log_link_debug(link, "Master interface changed: %i %s %i", link->master_ifindex,
2347 glyph(GLYPH_ARROW_RIGHT), master_ifindex);
2348
2349 link_drop_from_master(link);
2350 link->master_ifindex = master_ifindex;
2351
2352 /* Updating master ifindex may cause operational state change, e.g. carrier <-> enslaved */
2353 link_dirty(link);
2354 }
2355
2356 r = link_append_to_master(link);
2357 if (r < 0)
2358 return log_link_debug_errno(link, r, "Failed to append link to master: %m");
2359
2360 return 0;
2361 }
2362
2363 static int link_update_driver(Link *link, sd_netlink_message *message) {
2364 int r;
2365
2366 assert(link);
2367 assert(link->manager);
2368 assert(message);
2369
2370 /* Driver is already read. Assuming the driver is never changed. */
2371 if (link->ethtool_driver_read)
2372 return 0;
2373
2374 /* When udevd is running, read the driver after the interface is initialized by udevd.
2375 * Otherwise, ethtool may not work correctly. See issue #22538.
2376 * When udevd is not running, read the value when the interface is detected. */
2377 if (udev_available() && !link->dev)
2378 return 0;
2379
2380 link->ethtool_driver_read = true;
2381
2382 r = ethtool_get_driver(&link->manager->ethtool_fd, link->ifname, &link->driver);
2383 if (r < 0) {
2384 log_link_debug_errno(link, r, "Failed to get driver, continuing without: %m");
2385 return 0;
2386 }
2387
2388 log_link_debug(link, "Found driver: %s", strna(link->driver));
2389
2390 if (streq_ptr(link->driver, "dsa")) {
2391 uint32_t dsa_master_ifindex = 0;
2392
2393 r = sd_netlink_message_read_u32(message, IFLA_LINK, &dsa_master_ifindex);
2394 if (r < 0 && r != -ENODATA)
2395 return log_link_debug_errno(link, r, "rtnl: failed to read ifindex of the DSA master interface: %m");
2396
2397 if (dsa_master_ifindex > INT_MAX) {
2398 log_link_debug(link, "rtnl: received too large DSA master ifindex (%"PRIu32" > INT_MAX), ignoring.",
2399 dsa_master_ifindex);
2400 dsa_master_ifindex = 0;
2401 }
2402
2403 link->dsa_master_ifindex = (int) dsa_master_ifindex;
2404 }
2405
2406 return 1; /* needs reconfigure */
2407 }
2408
2409 static int link_update_permanent_hardware_address(Link *link, sd_netlink_message *message) {
2410 int r;
2411
2412 assert(link);
2413 assert(link->manager);
2414 assert(message);
2415
2416 if (link->permanent_hw_addr.length > 0)
2417 return 0;
2418
2419 r = netlink_message_read_hw_addr(message, IFLA_PERM_ADDRESS, &link->permanent_hw_addr);
2420 if (r == -ENODATA)
2421 return 0;
2422 if (r < 0)
2423 return log_link_debug_errno(link, r, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
2424
2425 if (link->permanent_hw_addr.length > 0)
2426 log_link_debug(link, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link->permanent_hw_addr));
2427
2428 return 1; /* needs reconfigure */
2429 }
2430
2431 static int link_update_hardware_address(Link *link, sd_netlink_message *message) {
2432 struct hw_addr_data addr;
2433 int r;
2434
2435 assert(link);
2436 assert(message);
2437
2438 r = netlink_message_read_hw_addr(message, IFLA_BROADCAST, &link->bcast_addr);
2439 if (r < 0 && r != -ENODATA)
2440 return log_link_debug_errno(link, r, "rtnl: failed to read broadcast address: %m");
2441
2442 r = netlink_message_read_hw_addr(message, IFLA_ADDRESS, &addr);
2443 if (r == -ENODATA)
2444 return 0;
2445 if (r < 0)
2446 return log_link_debug_errno(link, r, "rtnl: failed to read hardware address: %m");
2447
2448 if (hw_addr_equal(&link->hw_addr, &addr))
2449 return 0;
2450
2451 if (link->hw_addr.length == 0)
2452 log_link_debug(link, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr));
2453 else {
2454 log_link_debug(link, "Hardware address is changed: %s %s %s",
2455 HW_ADDR_TO_STR(&link->hw_addr),
2456 glyph(GLYPH_ARROW_RIGHT),
2457 HW_ADDR_TO_STR(&addr));
2458
2459 hashmap_remove_value(link->manager->links_by_hw_addr, &link->hw_addr, link);
2460 }
2461
2462 link->hw_addr = addr;
2463
2464 if (!hw_addr_is_null(&link->hw_addr)) {
2465 r = hashmap_ensure_put(&link->manager->links_by_hw_addr, &hw_addr_hash_ops, &link->hw_addr, link);
2466 if (r == -EEXIST && streq_ptr(link->kind, "bond"))
2467 /* bonding master and its slaves have the same hardware address. */
2468 r = hashmap_replace(link->manager->links_by_hw_addr, &link->hw_addr, link);
2469 if (r < 0)
2470 log_link_debug_errno(link, r, "Failed to manage link by its new hardware address, ignoring: %m");
2471 }
2472
2473 r = ipv4acd_update_mac(link);
2474 if (r < 0)
2475 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4 ACD client: %m");
2476
2477 r = ipv4ll_update_mac(link);
2478 if (r < 0)
2479 return log_link_debug_errno(link, r, "Could not update MAC address in IPv4LL client: %m");
2480
2481 r = dhcp4_update_mac(link);
2482 if (r < 0)
2483 return log_link_debug_errno(link, r, "Could not update MAC address in DHCP client: %m");
2484
2485 r = dhcp6_update_mac(link);
2486 if (r < 0)
2487 return log_link_debug_errno(link, r, "Could not update MAC address in DHCPv6 client: %m");
2488
2489 r = radv_update_mac(link);
2490 if (r < 0)
2491 return log_link_debug_errno(link, r, "Could not update MAC address for Router Advertisement: %m");
2492
2493 if (link->ndisc && link->hw_addr.length == ETH_ALEN) {
2494 r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
2495 if (r < 0)
2496 return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
2497 }
2498
2499 if (link->lldp_rx) {
2500 r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
2501 if (r < 0)
2502 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Rx: %m");
2503 }
2504
2505 if (link->lldp_tx) {
2506 r = sd_lldp_tx_set_hwaddr(link->lldp_tx, &link->hw_addr.ether);
2507 if (r < 0)
2508 return log_link_debug_errno(link, r, "Could not update MAC address for LLDP Tx: %m");
2509 }
2510
2511 return 1; /* needs reconfigure */
2512 }
2513
2514 static int link_update_mtu(Link *link, sd_netlink_message *message) {
2515 uint32_t mtu, min_mtu = 0, max_mtu = UINT32_MAX;
2516 int r;
2517
2518 assert(link);
2519 assert(message);
2520
2521 r = sd_netlink_message_read_u32(message, IFLA_MTU, &mtu);
2522 if (r == -ENODATA)
2523 return 0;
2524 if (r < 0)
2525 return log_link_debug_errno(link, r, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
2526 if (mtu == 0)
2527 return 0;
2528
2529 r = sd_netlink_message_read_u32(message, IFLA_MIN_MTU, &min_mtu);
2530 if (r < 0 && r != -ENODATA)
2531 return log_link_debug_errno(link, r, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2532
2533 r = sd_netlink_message_read_u32(message, IFLA_MAX_MTU, &max_mtu);
2534 if (r < 0 && r != -ENODATA)
2535 return log_link_debug_errno(link, r, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2536
2537 if (max_mtu == 0)
2538 max_mtu = UINT32_MAX;
2539
2540 link->min_mtu = min_mtu;
2541 link->max_mtu = max_mtu;
2542
2543 if (link->original_mtu == 0) {
2544 link->original_mtu = mtu;
2545 log_link_debug(link, "Saved original MTU %" PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2546 link->original_mtu, link->min_mtu, link->max_mtu);
2547 }
2548
2549 if (link->mtu == mtu)
2550 return 0;
2551
2552 if (link->mtu != 0)
2553 log_link_debug(link, "MTU is changed: %"PRIu32" %s %"PRIu32" (min: %"PRIu32", max: %"PRIu32")",
2554 link->mtu, glyph(GLYPH_ARROW_RIGHT), mtu,
2555 link->min_mtu, link->max_mtu);
2556
2557 link->mtu = mtu;
2558
2559 if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
2560 /* The kernel resets IPv6 MTU after changing device MTU. So, we need to re-set IPv6 MTU again. */
2561 (void) link_set_ipv6_mtu_async(link);
2562
2563 if (link->dhcp_client) {
2564 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
2565 if (r < 0)
2566 return log_link_debug_errno(link, r, "Could not update MTU in DHCP client: %m");
2567 }
2568
2569 if (link->radv) {
2570 r = sd_radv_set_mtu(link->radv, link->mtu);
2571 if (r < 0)
2572 return log_link_debug_errno(link, r, "Could not set MTU for Router Advertisement: %m");
2573 }
2574
2575 return 0;
2576 }
2577
2578 static int link_update_alternative_names(Link *link, sd_netlink_message *message) {
2579 _cleanup_strv_free_ char **altnames = NULL;
2580 int r;
2581
2582 assert(link);
2583 assert(message);
2584
2585 r = sd_netlink_message_read_strv(message, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &altnames);
2586 if (r == -ENODATA)
2587 /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
2588 * interface has no alternative name. */
2589 return 0;
2590 if (r < 0)
2591 return log_link_debug_errno(link, r, "rtnl: failed to read alternative names: %m");
2592
2593 if (strv_equal(altnames, link->alternative_names))
2594 return 0;
2595
2596 /* See the comment in link_update_name(). If one of the new alternative names is already in use by
2597 * another interface, the alternative-name information in this message is likely stale. Ignore the
2598 * entire IFLA_ALT_IFNAME attribute in that case. */
2599 STRV_FOREACH(n, altnames) {
2600 Link *existing;
2601 if (link_get_by_name(link->manager, *n, &existing) >= 0 && existing != link) {
2602 log_link_debug(link,
2603 "Alternative interface name update detected, but the new name '%s' is already in use by another interface (ifindex=%i), ignoring the update as it may be stale.",
2604 *n, existing->ifindex);
2605 return 0;
2606 }
2607 }
2608
2609 STRV_FOREACH(n, link->alternative_names)
2610 hashmap_remove_value(link->manager->links_by_name, *n, link);
2611
2612 strv_free_and_replace(link->alternative_names, altnames);
2613
2614 STRV_FOREACH(n, link->alternative_names) {
2615 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, *n, link);
2616 if (r < 0)
2617 return log_link_debug_errno(link, r, "Failed to manage link by its new alternative names: %m");
2618 }
2619
2620 return 1; /* needs reconfigure */
2621 }
2622
2623 static int link_update_name(Link *link, sd_netlink_message *message) {
2624 const char *ifname;
2625 int r;
2626
2627 assert(link);
2628 assert(message);
2629
2630 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
2631 if (r == -ENODATA)
2632 /* Hmm?? But ok. */
2633 return 0;
2634 if (r < 0)
2635 return log_link_debug_errno(link, r, "Failed to read interface name in RTM_NEWLINK message: %m");
2636
2637 if (streq(ifname, link->ifname))
2638 return 0;
2639
2640 /* Check if the new interface name is already used by another interface. If so, the rename is likely
2641 * stale. Consider the following race:
2642 *
2643 * 1. networkd enables rtnl matches in manager_connect_rtnl().
2644 * 2. The kernel sends an RTM_NEWLINK notification for an interface (say, ifindex=2, ifname="eth0"),
2645 * and the notification message (A) is queued in networkd's sd-netlink object.
2646 * 3. The interface is renamed (say, "eth0" -> "enp0"), e.g. by udevd. The kernel sends another
2647 * RTM_NEWLINK notification for the rename, and the notification message (B) is also queued in
2648 * networkd's sd-netlink object.
2649 * 4. The kernel detects another new interface (say, ifindex=3). Since the name "eth0" is now unused,
2650 * the interface is named "eth0".
2651 * 5. networkd enumerates links and creates Link objects for:
2652 * - ifindex=2, ifname="enp0"
2653 * - ifindex=3, ifname="eth0"
2654 * 6. After enumeration, when processing message (A), networkd becomes confused and thinks that the
2655 * interface with ifindex=2 was renamed from "enp0" to "eth0". However, it fails to update the
2656 * Manager.links_by_name hashmap because "eth0" is already used by the interface with ifindex=3.
2657 * 7. When processing message (B), networkd thinks that the interface with ifindex=2 has been renamed
2658 * again from "eth0" to "enp0", and renames the Link object back to "enp0".
2659 *
2660 * When this happens, we get something like the following:
2661 *
2662 * systemd-networkd[5164]: enp0: Interface name change detected, renamed to eth0.
2663 * systemd-networkd[5164]: eth0: Failed to manage link by its new name: File exists
2664 * systemd-networkd[5164]: Could not process link message: File exists
2665 * systemd-networkd[5164]: eth0: Failed
2666 * systemd-networkd[5164]: eth0: State changed: initialized -> failed
2667 * systemd-networkd[5164]: eth0: Interface name change detected, renamed to enp0.
2668 *
2669 * See also #20203.
2670 *
2671 * To avoid the race, ignore the rename. A subsequent RTM_NEWLINK message should eventually provide
2672 * the current interface name, e.g. message (B) above. */
2673 Link *existing;
2674 if (link_get_by_name(link->manager, ifname, &existing) >= 0 && existing != link) {
2675 log_link_debug(link,
2676 "Interface name change detected, but the new interface name '%s' is already in use by another interface (ifindex=%i), ignoring the rename as it may be stale.",
2677 ifname, existing->ifindex);
2678 return 0;
2679 }
2680
2681 log_link_info(link, "Interface name change detected, renamed to %s.", ifname);
2682
2683 hashmap_remove_value(link->manager->links_by_name, link->ifname, link);
2684
2685 r = free_and_strdup(&link->ifname, ifname);
2686 if (r < 0)
2687 return log_oom_debug();
2688
2689 r = hashmap_ensure_put(&link->manager->links_by_name, &string_hash_ops, link->ifname, link);
2690 if (r < 0)
2691 return log_link_debug_errno(link, r, "Failed to manage link by its new name: %m");
2692
2693 if (link->dhcp_client) {
2694 r = sd_dhcp_client_set_ifname(link->dhcp_client, link->ifname);
2695 if (r < 0)
2696 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP client: %m");
2697 }
2698
2699 if (link->dhcp6_client) {
2700 r = sd_dhcp6_client_set_ifname(link->dhcp6_client, link->ifname);
2701 if (r < 0)
2702 return log_link_debug_errno(link, r, "Failed to update interface name in DHCPv6 client: %m");
2703 }
2704
2705 if (link->ndisc) {
2706 r = sd_ndisc_set_ifname(link->ndisc, link->ifname);
2707 if (r < 0)
2708 return log_link_debug_errno(link, r, "Failed to update interface name in NDisc: %m");
2709 }
2710
2711 if (link->dhcp_relay_interface) {
2712 r = sd_dhcp_relay_interface_set_ifname(link->dhcp_relay_interface, link->ifname);
2713 if (r < 0)
2714 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP relay interface: %m");
2715 }
2716
2717 if (link->dhcp_server) {
2718 r = sd_dhcp_server_set_ifname(link->dhcp_server, link->ifname);
2719 if (r < 0)
2720 return log_link_debug_errno(link, r, "Failed to update interface name in DHCP server: %m");
2721 }
2722
2723 if (link->radv) {
2724 r = sd_radv_set_ifname(link->radv, link->ifname);
2725 if (r < 0)
2726 return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
2727 }
2728
2729 if (link->lldp_rx) {
2730 r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
2731 if (r < 0)
2732 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Rx: %m");
2733 }
2734
2735 if (link->lldp_tx) {
2736 r = sd_lldp_tx_set_ifname(link->lldp_tx, link->ifname);
2737 if (r < 0)
2738 return log_link_debug_errno(link, r, "Failed to update interface name in LLDP Tx: %m");
2739 }
2740
2741 if (link->ipv4ll) {
2742 r = sd_ipv4ll_set_ifname(link->ipv4ll, link->ifname);
2743 if (r < 0)
2744 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4LL client: %m");
2745 }
2746
2747 r = ipv4acd_set_ifname(link);
2748 if (r < 0)
2749 return log_link_debug_errno(link, r, "Failed to update interface name in IPv4ACD client: %m");
2750
2751 return 1; /* needs reconfigure */
2752 }
2753
2754 static int link_update(Link *link, sd_netlink_message *message) {
2755 bool needs_reconfigure = false;
2756 int r;
2757
2758 assert(link);
2759 assert(message);
2760
2761 r = link_update_name(link, message);
2762 if (r < 0)
2763 return r;
2764 needs_reconfigure = needs_reconfigure || r > 0;
2765
2766 r = link_update_alternative_names(link, message);
2767 if (r < 0)
2768 return r;
2769 needs_reconfigure = needs_reconfigure || r > 0;
2770
2771 r = link_update_mtu(link, message);
2772 if (r < 0)
2773 return r;
2774
2775 r = link_update_driver(link, message);
2776 if (r < 0)
2777 return r;
2778 needs_reconfigure = needs_reconfigure || r > 0;
2779
2780 r = link_update_permanent_hardware_address(link, message);
2781 if (r < 0)
2782 return r;
2783 needs_reconfigure = needs_reconfigure || r > 0;
2784
2785 r = link_update_hardware_address(link, message);
2786 if (r < 0)
2787 return r;
2788 needs_reconfigure = needs_reconfigure || r > 0;
2789
2790 r = link_update_master(link, message);
2791 if (r < 0)
2792 return r;
2793
2794 r = link_update_ipv6ll_addrgen_mode(link, message);
2795 if (r < 0)
2796 return r;
2797
2798 r = link_update_flags(link, message);
2799 if (r < 0)
2800 return r;
2801
2802 r = link_update_bridge_vlan(link, message);
2803 if (r < 0)
2804 return r;
2805
2806 return needs_reconfigure;
2807 }
2808
2809 static Link *link_drop_or_unref(Link *link) {
2810 if (!link)
2811 return NULL;
2812 if (!link->manager)
2813 return link_unref(link);
2814 return link_drop(link);
2815 }
2816
2817 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
2818
2819 static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
2820 _cleanup_free_ char *ifname = NULL, *kind = NULL, *state_file = NULL;
2821 _cleanup_(link_drop_or_unrefp) Link *link = NULL;
2822 unsigned short iftype;
2823 int r, ifindex;
2824
2825 assert(manager);
2826 assert(message);
2827 assert(ret);
2828
2829 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2830 if (r < 0)
2831 return log_debug_errno(r, "rtnl: failed to read ifindex from link message: %m");
2832 else if (ifindex <= 0)
2833 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "rtnl: received link message without valid ifindex.");
2834
2835 r = sd_rtnl_message_link_get_type(message, &iftype);
2836 if (r < 0)
2837 return log_debug_errno(r, "rtnl: failed to read interface type from link message: %m");
2838
2839 r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
2840 if (r < 0)
2841 return log_debug_errno(r, "rtnl: failed to read interface name from link message: %m");
2842
2843 /* check for link kind */
2844 r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
2845 if (r >= 0) {
2846 r = sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
2847 if (r < 0 && r != -ENODATA)
2848 return log_debug_errno(r, "rtnl: failed to read interface kind from link message: %m");
2849 r = sd_netlink_message_exit_container(message);
2850 if (r < 0)
2851 return log_debug_errno(r, "rtnl: failed to exit IFLA_LINKINFO container: %m");
2852 }
2853
2854 if (!manager->test_mode) {
2855 /* Do not update state files when running in test mode. */
2856 if (asprintf(&state_file, "/run/systemd/netif/links/%d", ifindex) < 0)
2857 return log_oom_debug();
2858 }
2859
2860 link = new(Link, 1);
2861 if (!link)
2862 return -ENOMEM;
2863
2864 *link = (Link) {
2865 .n_ref = 1,
2866 .state = LINK_STATE_PENDING,
2867 .online_state = _LINK_ONLINE_STATE_INVALID,
2868 .automatic_reconfigure_ratelimit = (const RateLimit) { .interval = 10 * USEC_PER_SEC, .burst = 5 },
2869 .ifindex = ifindex,
2870 .iftype = iftype,
2871 .ifname = TAKE_PTR(ifname),
2872 .kind = TAKE_PTR(kind),
2873
2874 .bridge_vlan_pvid = UINT16_MAX,
2875
2876 .ipv6ll_address_gen_mode = _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID,
2877
2878 .state_file = TAKE_PTR(state_file),
2879
2880 .n_dns = UINT_MAX,
2881 .dns_default_route = -1,
2882 .llmnr = _RESOLVE_SUPPORT_INVALID,
2883 .mdns = _RESOLVE_SUPPORT_INVALID,
2884 .dnssec_mode = _DNSSEC_MODE_INVALID,
2885 .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
2886 };
2887
2888 r = hashmap_ensure_put(&manager->links_by_index, &link_hash_ops, INT_TO_PTR(link->ifindex), link);
2889 if (r < 0)
2890 return log_link_debug_errno(link, r, "Failed to store link into manager: %m");
2891
2892 link->manager = manager;
2893
2894 r = hashmap_ensure_put(&manager->links_by_name, &string_hash_ops, link->ifname, link);
2895 if (r < 0)
2896 return log_link_debug_errno(link, r, "Failed to manage link by its interface name: %m");
2897
2898 log_link_debug(link, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2899 link->ifindex, strna(arphrd_to_name(link->iftype)), link->iftype, strna(link->kind));
2900
2901 /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2902 * message arrived too early. Request the wireless link information again.
2903 */
2904 if (set_remove(manager->new_wlan_ifindices, INT_TO_PTR(link->ifindex))) {
2905 r = link_get_wlan_interface(link);
2906 if (r < 0)
2907 log_link_warning_errno(link, r, "Failed to get wireless interface, ignoring: %m");
2908 }
2909
2910 *ret = TAKE_PTR(link);
2911 return 0;
2912 }
2913
2914 int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, Manager *manager) {
2915 Link *link = NULL;
2916 NetDev *netdev = NULL;
2917 uint16_t type;
2918 const char *name;
2919 int r, ifindex;
2920
2921 assert(rtnl);
2922 assert(message);
2923 assert(manager);
2924
2925 if (sd_netlink_message_is_error(message)) {
2926 r = sd_netlink_message_get_errno(message);
2927 if (r < 0)
2928 log_message_warning_errno(message, r, "rtnl: Could not receive link message, ignoring");
2929
2930 return 0;
2931 }
2932
2933 r = sd_netlink_message_get_type(message, &type);
2934 if (r < 0) {
2935 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
2936 return 0;
2937 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
2938 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type);
2939 return 0;
2940 }
2941
2942 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
2943 if (r < 0) {
2944 log_warning_errno(r, "rtnl: Could not get ifindex from link message, ignoring: %m");
2945 return 0;
2946 } else if (ifindex <= 0) {
2947 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex);
2948 return 0;
2949 }
2950
2951 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
2952 if (r < 0) {
2953 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
2954 return 0;
2955 }
2956
2957 (void) link_get_by_index(manager, ifindex, &link);
2958 (void) netdev_get(manager, name, &netdev);
2959
2960 switch (type) {
2961 case RTM_NEWLINK:
2962 if (netdev) {
2963 /* netdev exists, so make sure the ifindex matches */
2964 r = netdev_set_ifindex(netdev, message);
2965 if (r < 0) {
2966 log_netdev_warning_errno(netdev, r, "Could not process new link message for netdev, ignoring: %m");
2967 netdev_enter_failed(netdev);
2968 return 0;
2969 }
2970 }
2971
2972 if (!link) {
2973 /* link is new, so add it */
2974 r = link_new(manager, message, &link);
2975 if (r < 0) {
2976 log_warning_errno(r, "Could not process new link message: %m");
2977 return 0;
2978 }
2979
2980 r = link_update(link, message);
2981 if (r < 0) {
2982 log_link_warning_errno(link, r, "Could not process link message: %m");
2983 link_enter_failed(link);
2984 return 0;
2985 }
2986
2987 if (link->manager->test_mode) {
2988 log_link_debug(link, "Running in test mode, refusing to enter initialized state.");
2989 link_set_state(link, LINK_STATE_UNMANAGED);
2990 return 0;
2991 }
2992
2993 /* Do not enter initialized state if we are enumerating. */
2994 if (manager->enumerating)
2995 return 0;
2996
2997 r = link_check_initialized(link);
2998 if (r < 0) {
2999 log_link_warning_errno(link, r, "Failed to check link is initialized: %m");
3000 link_enter_failed(link);
3001 return 0;
3002 }
3003 } else {
3004 r = link_update(link, message);
3005 if (r < 0) {
3006 log_link_warning_errno(link, r, "Could not process link message: %m");
3007 link_enter_failed(link);
3008 return 0;
3009 }
3010 if (r == 0)
3011 return 0;
3012
3013 if (link->manager->test_mode) {
3014 log_link_debug(link, "Running in test mode, refusing to configure interface.");
3015 link_set_state(link, LINK_STATE_UNMANAGED);
3016 return 0;
3017 }
3018
3019 /* Do not configure interface if we are enumerating. */
3020 if (manager->enumerating)
3021 return 0;
3022
3023 r = link_reconfigure_impl(link, /* flags= */ 0);
3024 if (r < 0) {
3025 log_link_warning_errno(link, r, "Failed to reconfigure interface: %m");
3026 link_enter_failed(link);
3027 return 0;
3028 }
3029 }
3030 break;
3031
3032 case RTM_DELLINK:
3033 link_drop(link);
3034 netdev_drop(netdev);
3035 break;
3036
3037 default:
3038 assert_not_reached();
3039 }
3040
3041 return 1;
3042 }
3043
3044 int link_getlink_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
3045 uint16_t message_type;
3046 int r;
3047
3048 assert(m);
3049 assert(link);
3050 assert(error_msg);
3051
3052 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
3053 return 0;
3054
3055 r = sd_netlink_message_get_errno(m);
3056 if (r < 0) {
3057 log_link_message_warning_errno(link, m, r, "%s", error_msg);
3058 link_enter_failed(link);
3059 return 0;
3060 }
3061
3062 r = sd_netlink_message_get_type(m, &message_type);
3063 if (r < 0) {
3064 log_link_debug_errno(link, r, "rtnl: failed to read link message type, ignoring: %m");
3065 return 0;
3066 }
3067 if (message_type != RTM_NEWLINK) {
3068 log_link_debug(link, "rtnl: received invalid link message type, ignoring.");
3069 return 0;
3070 }
3071
3072 r = link_update(link, m);
3073 if (r < 0) {
3074 link_enter_failed(link);
3075 return 0;
3076 }
3077
3078 return 1;
3079 }
3080
3081 int link_call_getlink(Link *link, link_netlink_message_handler_t callback) {
3082 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
3083 int r;
3084
3085 assert(link);
3086 assert(link->manager);
3087 assert(link->manager->rtnl);
3088 assert(callback);
3089
3090 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
3091 if (r < 0)
3092 return r;
3093
3094 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
3095 link_netlink_destroy_callback, link);
3096 if (r < 0)
3097 return r;
3098
3099 link_ref(link);
3100 return 0;
3101 }
3102
3103 static const char* const link_state_table[_LINK_STATE_MAX] = {
3104 [LINK_STATE_PENDING] = "pending",
3105 [LINK_STATE_INITIALIZED] = "initialized",
3106 [LINK_STATE_CONFIGURING] = "configuring",
3107 [LINK_STATE_CONFIGURED] = "configured",
3108 [LINK_STATE_UNMANAGED] = "unmanaged",
3109 [LINK_STATE_FAILED] = "failed",
3110 [LINK_STATE_LINGER] = "linger",
3111 };
3112
3113 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
3114
3115 int link_flags_to_string_alloc(uint32_t flags, char **ret) {
3116 _cleanup_free_ char *str = NULL;
3117 static const char* map[] = {
3118 [LOG2U(IFF_UP)] = "up", /* interface is up. */
3119 [LOG2U(IFF_BROADCAST)] = "broadcast", /* broadcast address valid. */
3120 [LOG2U(IFF_DEBUG)] = "debug", /* turn on debugging. */
3121 [LOG2U(IFF_LOOPBACK)] = "loopback", /* interface is a loopback net. */
3122 [LOG2U(IFF_POINTOPOINT)] = "point-to-point", /* interface has p-p link. */
3123 [LOG2U(IFF_NOTRAILERS)] = "no-trailers", /* avoid use of trailers. */
3124 [LOG2U(IFF_RUNNING)] = "running", /* interface RFC2863 OPER_UP. */
3125 [LOG2U(IFF_NOARP)] = "no-arp", /* no ARP protocol. */
3126 [LOG2U(IFF_PROMISC)] = "promiscuous", /* receive all packets. */
3127 [LOG2U(IFF_ALLMULTI)] = "all-multicast", /* receive all multicast packets. */
3128 [LOG2U(IFF_MASTER)] = "master", /* master of a load balancer. */
3129 [LOG2U(IFF_SLAVE)] = "slave", /* slave of a load balancer. */
3130 [LOG2U(IFF_MULTICAST)] = "multicast", /* supports multicast. */
3131 [LOG2U(IFF_PORTSEL)] = "portsel", /* can set media type. */
3132 [LOG2U(IFF_AUTOMEDIA)] = "auto-media", /* auto media select active. */
3133 [LOG2U(IFF_DYNAMIC)] = "dynamic", /* dialup device with changing addresses. */
3134 [LOG2U(IFF_LOWER_UP)] = "lower-up", /* driver signals L1 up. */
3135 [LOG2U(IFF_DORMANT)] = "dormant", /* driver signals dormant. */
3136 [LOG2U(IFF_ECHO)] = "echo", /* echo sent packets. */
3137 };
3138
3139 assert(ret);
3140
3141 for (size_t i = 0; i < ELEMENTSOF(map); i++)
3142 if (BIT_SET(flags, i) && map[i])
3143 if (!strextend_with_separator(&str, ",", map[i]))
3144 return -ENOMEM;
3145
3146 *ret = TAKE_PTR(str);
3147 return 0;
3148 }
3149
3150 static const char * const kernel_operstate_table[] = {
3151 [IF_OPER_UNKNOWN] = "unknown",
3152 [IF_OPER_NOTPRESENT] = "not-present",
3153 [IF_OPER_DOWN] = "down",
3154 [IF_OPER_LOWERLAYERDOWN] = "lower-layer-down",
3155 [IF_OPER_TESTING] = "testing",
3156 [IF_OPER_DORMANT] = "dormant",
3157 [IF_OPER_UP] = "up",
3158 };
3159
3160 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate, int);
3161
3162 bool link_has_local_lease_domain(Link *link) {
3163 assert(link);
3164
3165 return link->dhcp_server &&
3166 link->network &&
3167 link->network->dhcp_server_local_lease_domain &&
3168 !dns_name_is_root(link->network->dhcp_server_local_lease_domain);
3169 }