1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <netinet/in.h>
6 #include <linux/if_arp.h>
7 #include <linux/if_link.h>
8 #include <linux/netdevice.h>
9 #include <sys/socket.h>
12 #include "alloc-util.h"
13 #include "arphrd-util.h"
18 #include "device-private.h"
19 #include "device-util.h"
20 #include "dhcp-lease-internal.h"
22 #include "ethtool-util.h"
23 #include "event-util.h"
26 #include "format-util.h"
28 #include "glyph-util.h"
29 #include "logarithm.h"
30 #include "missing_network.h"
31 #include "netlink-util.h"
32 #include "network-internal.h"
33 #include "networkd-address-label.h"
34 #include "networkd-address.h"
35 #include "networkd-bridge-fdb.h"
36 #include "networkd-bridge-mdb.h"
37 #include "networkd-bridge-vlan.h"
38 #include "networkd-can.h"
39 #include "networkd-dhcp-prefix-delegation.h"
40 #include "networkd-dhcp-server.h"
41 #include "networkd-dhcp4.h"
42 #include "networkd-dhcp6.h"
43 #include "networkd-ipv4acd.h"
44 #include "networkd-ipv4ll.h"
45 #include "networkd-ipv6-proxy-ndp.h"
46 #include "networkd-link-bus.h"
47 #include "networkd-link.h"
48 #include "networkd-lldp-tx.h"
49 #include "networkd-manager.h"
50 #include "networkd-ndisc.h"
51 #include "networkd-neighbor.h"
52 #include "networkd-nexthop.h"
53 #include "networkd-queue.h"
54 #include "networkd-radv.h"
55 #include "networkd-route-util.h"
56 #include "networkd-route.h"
57 #include "networkd-routing-policy-rule.h"
58 #include "networkd-setlink.h"
59 #include "networkd-sriov.h"
60 #include "networkd-state-file.h"
61 #include "networkd-sysctl.h"
62 #include "networkd-wifi.h"
64 #include "socket-util.h"
65 #include "stdio-util.h"
66 #include "string-table.h"
69 #include "tmpfile-util.h"
71 #include "udev-util.h"
74 void link_required_operstate_for_online(Link
*link
, LinkOperationalStateRange
*ret
) {
78 if (link
->network
&& operational_state_range_is_valid(&link
->network
->required_operstate_for_online
))
79 *ret
= link
->network
->required_operstate_for_online
;
80 else if (link
->iftype
== ARPHRD_CAN
)
81 *ret
= (const LinkOperationalStateRange
) {
82 .min
= LINK_OPERSTATE_CARRIER
,
83 .max
= LINK_OPERSTATE_CARRIER
,
86 *ret
= LINK_OPERSTATE_RANGE_DEFAULT
;
89 bool link_ipv6_enabled(Link
*link
) {
92 if (!socket_ipv6_is_supported())
95 if (link
->iftype
== ARPHRD_CAN
)
101 if (link
->network
->bond
)
104 if (link_may_have_ipv6ll(link
, /* check_multicast = */ false))
107 if (network_has_static_ipv6_configurations(link
->network
))
113 bool link_has_ipv6_connectivity(Link
*link
) {
114 LinkAddressState ipv6_address_state
;
118 link_get_address_states(link
, NULL
, &ipv6_address_state
, NULL
);
120 switch (ipv6_address_state
) {
121 case LINK_ADDRESS_STATE_ROUTABLE
:
122 /* If the interface has a routable IPv6 address, then we assume yes. */
125 case LINK_ADDRESS_STATE_DEGRADED
:
126 /* If the interface has only degraded IPv6 address (mostly, link-local address), then let's check
127 * there is an IPv6 default gateway. */
128 return link_has_default_gateway(link
, AF_INET6
);
130 case LINK_ADDRESS_STATE_OFF
:
131 /* No IPv6 address. */
135 assert_not_reached();
139 static bool link_is_ready_to_configure_one(Link
*link
, bool allow_unmanaged
) {
142 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
, LINK_STATE_UNMANAGED
))
146 return allow_unmanaged
;
148 if (!link
->network
->configure_without_carrier
) {
149 if (link
->set_flags_messages
> 0)
152 if (!link_has_carrier(link
))
156 if (link
->set_link_messages
> 0)
159 if (!link
->activated
)
165 bool link_is_ready_to_configure(Link
*link
, bool allow_unmanaged
) {
166 return check_ready_for_all_sr_iov_ports(link
, allow_unmanaged
, link_is_ready_to_configure_one
);
169 void link_ntp_settings_clear(Link
*link
) {
170 link
->ntp
= strv_free(link
->ntp
);
173 void link_dns_settings_clear(Link
*link
) {
174 if (link
->n_dns
!= UINT_MAX
)
175 for (unsigned i
= 0; i
< link
->n_dns
; i
++)
176 in_addr_full_free(link
->dns
[i
]);
177 link
->dns
= mfree(link
->dns
);
178 link
->n_dns
= UINT_MAX
;
180 link
->search_domains
= ordered_set_free(link
->search_domains
);
181 link
->route_domains
= ordered_set_free(link
->route_domains
);
183 link
->dns_default_route
= -1;
184 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
185 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
186 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
187 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
189 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
192 static void link_free_engines(Link
*link
) {
196 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
198 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
199 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
200 link
->dhcp4_6rd_tunnel_name
= mfree(link
->dhcp4_6rd_tunnel_name
);
202 link
->lldp_rx
= sd_lldp_rx_unref(link
->lldp_rx
);
203 link
->lldp_tx
= sd_lldp_tx_unref(link
->lldp_tx
);
205 link
->ipv4acd_by_address
= hashmap_free(link
->ipv4acd_by_address
);
207 link
->ipv4ll
= sd_ipv4ll_unref(link
->ipv4ll
);
209 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
210 link
->dhcp6_lease
= sd_dhcp6_lease_unref(link
->dhcp6_lease
);
212 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
213 link
->ndisc_expire
= sd_event_source_disable_unref(link
->ndisc_expire
);
216 link
->radv
= sd_radv_unref(link
->radv
);
219 static Link
*link_free(Link
*link
) {
222 link_ntp_settings_clear(link
);
223 link_dns_settings_clear(link
);
225 link
->routes
= set_free(link
->routes
);
226 link
->neighbors
= set_free(link
->neighbors
);
227 link
->addresses
= set_free(link
->addresses
);
228 link
->qdiscs
= set_free(link
->qdiscs
);
229 link
->tclasses
= set_free(link
->tclasses
);
231 link
->dhcp_pd_prefixes
= set_free(link
->dhcp_pd_prefixes
);
233 link_free_engines(link
);
235 set_free(link
->sr_iov_virt_port_ifindices
);
237 strv_free(link
->alternative_names
);
240 free(link
->previous_ssid
);
243 unlink_and_free(link
->lease_file
);
244 unlink_and_free(link
->lldp_file
);
245 unlink_and_free(link
->state_file
);
247 sd_device_unref(link
->dev
);
248 netdev_unref(link
->netdev
);
250 hashmap_free(link
->bound_to_links
);
251 hashmap_free(link
->bound_by_links
);
253 set_free_with_destructor(link
->slaves
, link_unref
);
255 network_unref(link
->network
);
257 sd_event_source_disable_unref(link
->carrier_lost_timer
);
262 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
264 int link_get_by_index(Manager
*m
, int ifindex
, Link
**ret
) {
272 link
= hashmap_get(m
->links_by_index
, INT_TO_PTR(ifindex
));
281 int link_get_by_name(Manager
*m
, const char *ifname
, Link
**ret
) {
287 link
= hashmap_get(m
->links_by_name
, ifname
);
296 int link_get_by_hw_addr(Manager
*m
, const struct hw_addr_data
*hw_addr
, Link
**ret
) {
302 link
= hashmap_get(m
->links_by_hw_addr
, hw_addr
);
311 int link_get_master(Link
*link
, Link
**ret
) {
313 assert(link
->manager
);
316 if (link
->master_ifindex
<= 0 || link
->master_ifindex
== link
->ifindex
)
319 return link_get_by_index(link
->manager
, link
->master_ifindex
, ret
);
322 void link_set_state(Link
*link
, LinkState state
) {
325 if (link
->state
== state
)
328 log_link_debug(link
, "State changed: %s -> %s",
329 link_state_to_string(link
->state
),
330 link_state_to_string(state
));
334 link_send_changed(link
, "AdministrativeState", NULL
);
338 int link_stop_engines(Link
*link
, bool may_keep_dhcp
) {
342 assert(link
->manager
);
343 assert(link
->manager
->event
);
345 bool keep_dhcp
= may_keep_dhcp
&&
347 !link
->network
->dhcp_send_decline
&& /* IPv4 ACD for the DHCPv4 address is running. */
348 (link
->manager
->restarting
||
349 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
352 r
= sd_dhcp_client_stop(link
->dhcp_client
);
354 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop DHCPv4 client: %m"));
357 r
= sd_dhcp_server_stop(link
->dhcp_server
);
359 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop DHCPv4 server: %m"));
361 r
= sd_lldp_rx_stop(link
->lldp_rx
);
363 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop LLDP Rx: %m"));
365 r
= sd_lldp_tx_stop(link
->lldp_tx
);
367 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop LLDP Tx: %m"));
369 r
= sd_ipv4ll_stop(link
->ipv4ll
);
371 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop IPv4 link-local: %m"));
373 r
= ipv4acd_stop(link
);
375 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop IPv4 ACD client: %m"));
377 r
= sd_dhcp6_client_stop(link
->dhcp6_client
);
379 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop DHCPv6 client: %m"));
381 r
= dhcp_pd_remove(link
, /* only_marked = */ false);
383 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not remove DHCPv6 PD addresses and routes: %m"));
385 r
= ndisc_stop(link
);
387 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Discovery: %m"));
391 r
= sd_radv_stop(link
->radv
);
393 RET_GATHER(ret
, log_link_warning_errno(link
, r
, "Could not stop IPv6 Router Advertisement: %m"));
398 void link_enter_failed(Link
*link
) {
403 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
406 log_link_warning(link
, "Failed");
408 link_set_state(link
, LINK_STATE_FAILED
);
410 if (!ratelimit_below(&link
->automatic_reconfigure_ratelimit
)) {
411 log_link_warning(link
, "The interface entered the failed state frequently, refusing to reconfigure it automatically.");
415 log_link_info(link
, "Trying to reconfigure the interface.");
416 r
= link_reconfigure(link
, /* force = */ true);
418 log_link_warning_errno(link
, r
, "Failed to reconfigure interface: %m");
425 (void) link_stop_engines(link
, /* may_keep_dhcp = */ false);
428 void link_check_ready(Link
*link
) {
433 if (link
->state
== LINK_STATE_CONFIGURED
)
436 if (link
->state
!= LINK_STATE_CONFIGURING
)
437 return (void) log_link_debug(link
, "%s(): link is in %s state.", __func__
, link_state_to_string(link
->state
));
440 return (void) log_link_debug(link
, "%s(): link is unmanaged.", __func__
);
442 if (!link
->tc_configured
)
443 return (void) log_link_debug(link
, "%s(): traffic controls are not configured.", __func__
);
445 if (link
->set_link_messages
> 0)
446 return (void) log_link_debug(link
, "%s(): link layer is configuring.", __func__
);
448 if (!link
->activated
)
449 return (void) log_link_debug(link
, "%s(): link is not activated.", __func__
);
451 if (link
->iftype
== ARPHRD_CAN
)
452 /* let's shortcut things for CAN which doesn't need most of checks below. */
455 if (!link
->stacked_netdevs_created
)
456 return (void) log_link_debug(link
, "%s(): stacked netdevs are not created.", __func__
);
458 if (!link
->static_addresses_configured
)
459 return (void) log_link_debug(link
, "%s(): static addresses are not configured.", __func__
);
461 if (!link
->static_address_labels_configured
)
462 return (void) log_link_debug(link
, "%s(): static address labels are not configured.", __func__
);
464 if (!link
->static_bridge_fdb_configured
)
465 return (void) log_link_debug(link
, "%s(): static bridge MDB entries are not configured.", __func__
);
467 if (!link
->static_bridge_mdb_configured
)
468 return (void) log_link_debug(link
, "%s(): static bridge MDB entries are not configured.", __func__
);
470 if (!link
->static_ipv6_proxy_ndp_configured
)
471 return (void) log_link_debug(link
, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__
);
473 if (!link
->static_neighbors_configured
)
474 return (void) log_link_debug(link
, "%s(): static neighbors are not configured.", __func__
);
476 if (!link
->static_nexthops_configured
)
477 return (void) log_link_debug(link
, "%s(): static nexthops are not configured.", __func__
);
479 if (!link
->static_routes_configured
)
480 return (void) log_link_debug(link
, "%s(): static routes are not configured.", __func__
);
482 if (!link
->static_routing_policy_rules_configured
)
483 return (void) log_link_debug(link
, "%s(): static routing policy rules are not configured.", __func__
);
485 if (!link
->sr_iov_configured
)
486 return (void) log_link_debug(link
, "%s(): SR-IOV is not configured.", __func__
);
488 /* IPv6LL is assigned after the link gains its carrier. */
489 if (!link
->network
->configure_without_carrier
&&
490 link_ipv6ll_enabled(link
) &&
491 !in6_addr_is_set(&link
->ipv6ll_address
))
492 return (void) log_link_debug(link
, "%s(): IPv6LL is not configured yet.", __func__
);
494 /* All static addresses must be ready. */
495 bool has_static_address
= false;
496 SET_FOREACH(a
, link
->addresses
) {
497 if (a
->source
!= NETWORK_CONFIG_SOURCE_STATIC
)
499 if (!address_is_ready(a
))
500 return (void) log_link_debug(link
, "%s(): static address %s is not ready.", __func__
,
501 IN_ADDR_PREFIX_TO_STRING(a
->family
, &a
->in_addr
, a
->prefixlen
));
502 has_static_address
= true;
505 /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
506 if (has_static_address
)
509 /* If no dynamic addressing protocol enabled, assume the interface is ready.
510 * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
511 if (!link_ipv4ll_enabled(link
) && !link_dhcp4_enabled(link
) &&
512 !link_dhcp6_enabled(link
) && !link_dhcp_pd_is_enabled(link
) &&
513 (link
->network
->configure_without_carrier
|| !link_ipv6_accept_ra_enabled(link
)))
517 link_ipv4ll_enabled(link
) && link
->ipv4ll_address_configured
&&
518 link_check_addresses_ready(link
, NETWORK_CONFIG_SOURCE_IPV4LL
);
520 link_dhcp4_enabled(link
) && link
->dhcp4_configured
&&
521 link_check_addresses_ready(link
, NETWORK_CONFIG_SOURCE_DHCP4
);
523 link_dhcp6_enabled(link
) && link
->dhcp6_configured
&&
524 (!link
->network
->dhcp6_use_address
||
525 link_check_addresses_ready(link
, NETWORK_CONFIG_SOURCE_DHCP6
));
527 link_dhcp_pd_is_enabled(link
) && link
->dhcp_pd_configured
&&
528 (!link
->network
->dhcp_pd_assign
||
529 link_check_addresses_ready(link
, NETWORK_CONFIG_SOURCE_DHCP_PD
));
531 link_ipv6_accept_ra_enabled(link
) && link
->ndisc_configured
&&
532 (!link
->network
->ipv6_accept_ra_use_autonomous_prefix
||
533 link_check_addresses_ready(link
, NETWORK_CONFIG_SOURCE_NDISC
));
535 /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
536 if (dhcp_pd_is_uplink(link
, link
, /* accept_auto = */ false)) {
537 if (link_dhcp4_enabled(link
) && link
->network
->dhcp_use_6rd
&&
538 sd_dhcp_lease_has_6rd(link
->dhcp_lease
)) {
540 return (void) log_link_debug(link
, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__
);
542 return (void) log_link_debug(link
, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__
);
545 if (link_dhcp6_enabled(link
) && link
->network
->dhcp6_use_pd_prefix
&&
546 sd_dhcp6_lease_has_pd_prefix(link
->dhcp6_lease
)) {
548 return (void) log_link_debug(link
, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__
);
550 return (void) log_link_debug(link
, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__
);
554 /* At least one dynamic addressing protocol is finished. */
555 if (!ipv4ll_ready
&& !dhcp4_ready
&& !dhcp6_ready
&& !dhcp_pd_ready
&& !ndisc_ready
)
556 return (void) log_link_debug(link
, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__
);
558 log_link_debug(link
, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
560 yes_no(ipv4ll_ready
),
563 yes_no(dhcp_pd_ready
),
564 yes_no(ndisc_ready
));
567 link_set_state(link
, LINK_STATE_CONFIGURED
);
570 static int link_request_static_configs(Link
*link
) {
574 assert(link
->network
);
575 assert(link
->state
!= _LINK_STATE_INVALID
);
577 r
= link_request_static_addresses(link
);
581 r
= link_request_static_address_labels(link
);
585 r
= link_request_static_bridge_fdb(link
);
589 r
= link_request_static_bridge_mdb(link
);
593 r
= link_request_static_ipv6_proxy_ndp_addresses(link
);
597 r
= link_request_static_neighbors(link
);
601 r
= link_request_static_nexthops(link
, false);
605 r
= link_request_static_routes(link
, false);
609 r
= link_request_static_routing_policy_rules(link
);
616 static int link_request_stacked_netdevs(Link
*link
) {
622 link
->stacked_netdevs_created
= false;
624 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
) {
625 r
= link_request_stacked_netdev(link
, netdev
);
630 if (link
->create_stacked_netdev_messages
== 0) {
631 link
->stacked_netdevs_created
= true;
632 link_check_ready(link
);
638 static int link_acquire_dynamic_ipv6_conf(Link
*link
) {
643 r
= radv_start(link
);
645 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Advertisement engine: %m");
647 r
= ndisc_start(link
);
649 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Discovery: %m");
651 r
= dhcp6_start(link
);
653 return log_link_warning_errno(link
, r
, "Failed to start DHCPv6 client: %m");
658 static int link_acquire_dynamic_ipv4_conf(Link
*link
) {
662 assert(link
->manager
);
663 assert(link
->manager
->event
);
665 if (link
->dhcp_client
) {
666 r
= dhcp4_start(link
);
668 return log_link_warning_errno(link
, r
, "Failed to start DHCPv4 client: %m");
670 log_link_debug(link
, "Acquiring DHCPv4 lease.");
672 } else if (link
->ipv4ll
) {
673 if (in4_addr_is_set(&link
->network
->ipv4ll_start_address
)) {
674 r
= sd_ipv4ll_set_address(link
->ipv4ll
, &link
->network
->ipv4ll_start_address
);
676 return log_link_warning_errno(link
, r
, "Could not set IPv4 link-local start address: %m");
679 r
= sd_ipv4ll_start(link
->ipv4ll
);
681 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
683 log_link_debug(link
, "Acquiring IPv4 link-local address.");
686 if (link
->dhcp_server
) {
687 r
= sd_dhcp_server_start(link
->dhcp_server
);
689 return log_link_warning_errno(link
, r
, "Could not start DHCP server: %m");
692 r
= ipv4acd_start(link
);
694 return log_link_warning_errno(link
, r
, "Could not start IPv4 ACD client: %m");
699 static int link_acquire_dynamic_conf(Link
*link
) {
703 assert(link
->network
);
705 r
= link_acquire_dynamic_ipv4_conf(link
);
709 if (in6_addr_is_set(&link
->ipv6ll_address
)) {
710 r
= link_acquire_dynamic_ipv6_conf(link
);
715 if (!link_radv_enabled(link
) || !link
->network
->dhcp_pd_announce
) {
716 /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
717 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
718 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
719 * below will be called in radv_start(). */
720 r
= dhcp_request_prefix_delegation(link
);
722 return log_link_warning_errno(link
, r
, "Failed to request DHCP delegated subnet prefix: %m");
726 r
= sd_lldp_tx_start(link
->lldp_tx
);
728 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
732 r
= sd_lldp_rx_start(link
->lldp_rx
);
734 return log_link_warning_errno(link
, r
, "Failed to start LLDP client: %m");
740 int link_ipv6ll_gained(Link
*link
) {
745 log_link_info(link
, "Gained IPv6LL");
747 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
750 r
= link_acquire_dynamic_ipv6_conf(link
);
754 link_check_ready(link
);
758 int link_handle_bound_to_list(Link
*link
) {
759 bool required_up
= false;
760 bool link_is_up
= false;
765 /* If at least one interface in bound_to_links has carrier, then make this interface up.
766 * If all interfaces in bound_to_links do not, then make this interface down. */
768 if (hashmap_isempty(link
->bound_to_links
))
771 if (link
->flags
& IFF_UP
)
774 HASHMAP_FOREACH(l
, link
->bound_to_links
)
775 if (link_has_carrier(l
)) {
780 if (!required_up
&& link_is_up
)
781 return link_request_to_bring_up_or_down(link
, /* up = */ false);
782 if (required_up
&& !link_is_up
)
783 return link_request_to_bring_up_or_down(link
, /* up = */ true);
788 static int link_handle_bound_by_list(Link
*link
) {
794 /* Update up or down state of interfaces which depend on this interface's carrier state. */
796 if (hashmap_isempty(link
->bound_by_links
))
799 HASHMAP_FOREACH(l
, link
->bound_by_links
) {
800 r
= link_handle_bound_to_list(l
);
808 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
817 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
820 r
= hashmap_ensure_put(h
, NULL
, INT_TO_PTR(carrier
->ifindex
), carrier
);
829 static int link_new_bound_by_list(Link
*link
) {
835 assert(link
->manager
);
839 HASHMAP_FOREACH(carrier
, m
->links_by_index
) {
840 if (!carrier
->network
)
843 if (strv_isempty(carrier
->network
->bind_carrier
))
846 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
)) {
847 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
853 HASHMAP_FOREACH(carrier
, link
->bound_by_links
) {
854 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
862 static int link_new_bound_to_list(Link
*link
) {
868 assert(link
->manager
);
873 if (strv_isempty(link
->network
->bind_carrier
))
878 HASHMAP_FOREACH(carrier
, m
->links_by_index
) {
879 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
)) {
880 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
886 HASHMAP_FOREACH(carrier
, link
->bound_to_links
) {
887 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
895 static void link_free_bound_to_list(Link
*link
) {
896 bool updated
= false;
901 while ((bound_to
= hashmap_steal_first(link
->bound_to_links
))) {
904 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
905 link_dirty(bound_to
);
912 static void link_free_bound_by_list(Link
*link
) {
913 bool updated
= false;
918 while ((bound_by
= hashmap_steal_first(link
->bound_by_links
))) {
921 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
922 link_dirty(bound_by
);
923 link_handle_bound_to_list(bound_by
);
931 static int link_append_to_master(Link
*link
) {
937 /* - The link may have no master.
938 * - RTM_NEWLINK message about master interface may not be received yet. */
939 if (link_get_master(link
, &master
) < 0)
942 r
= set_ensure_put(&master
->slaves
, NULL
, link
);
950 static void link_drop_from_master(Link
*link
) {
958 if (link_get_master(link
, &master
) < 0)
961 link_unref(set_remove(master
->slaves
, link
));
964 static int link_drop_requests(Link
*link
) {
969 assert(link
->manager
);
971 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
) {
972 if (req
->link
!= link
)
975 /* If the request is already called, but its reply is not received, then we need to
976 * drop the configuration (e.g. address) here. Note, if the configuration is known,
977 * it will be handled later by link_drop_foreign_addresses() or so. */
978 if (req
->waiting_reply
&& link
->state
!= LINK_STATE_LINGER
)
980 case REQUEST_TYPE_ADDRESS
: {
981 Address
*address
= ASSERT_PTR(req
->userdata
);
983 if (address_get(link
, address
, NULL
) < 0)
984 RET_GATHER(ret
, address_remove(address
, link
));
987 case REQUEST_TYPE_NEIGHBOR
: {
988 Neighbor
*neighbor
= ASSERT_PTR(req
->userdata
);
990 if (neighbor_get(link
, neighbor
, NULL
) < 0)
991 RET_GATHER(ret
, neighbor_remove(neighbor
, link
));
994 case REQUEST_TYPE_NEXTHOP
: {
995 NextHop
*nexthop
= ASSERT_PTR(req
->userdata
);
997 if (nexthop_get_by_id(link
->manager
, nexthop
->id
, NULL
) < 0)
998 RET_GATHER(ret
, nexthop_remove(nexthop
, link
->manager
));
1005 request_detach(req
);
1011 static Link
*link_drop(Link
*link
) {
1015 assert(link
->manager
);
1017 link_set_state(link
, LINK_STATE_LINGER
);
1019 /* Drop all references from other links and manager. Note that async netlink calls may have
1020 * references to the link, and they will be dropped when we receive replies. */
1022 (void) link_drop_requests(link
);
1024 link_free_bound_to_list(link
);
1025 link_free_bound_by_list(link
);
1027 link_clear_sr_iov_ifindices(link
);
1029 link_drop_from_master(link
);
1031 if (link
->state_file
)
1032 (void) unlink(link
->state_file
);
1036 STRV_FOREACH(n
, link
->alternative_names
)
1037 hashmap_remove(link
->manager
->links_by_name
, *n
);
1038 hashmap_remove(link
->manager
->links_by_name
, link
->ifname
);
1040 /* bonding master and its slaves have the same hardware address. */
1041 hashmap_remove_value(link
->manager
->links_by_hw_addr
, &link
->hw_addr
, link
);
1043 /* The following must be called at last. */
1044 assert_se(hashmap_remove(link
->manager
->links_by_index
, INT_TO_PTR(link
->ifindex
)) == link
);
1045 return link_unref(link
);
1048 static int link_drop_foreign_config(Link
*link
) {
1052 assert(link
->manager
);
1054 /* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
1055 * to remove loopback address or addresses used for root NFS. */
1057 if (IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
1059 if (FLAGS_SET(link
->flags
, IFF_LOOPBACK
))
1061 if (link
->network
->keep_configuration
== KEEP_CONFIGURATION_YES
)
1064 r
= link_drop_foreign_routes(link
);
1066 RET_GATHER(r
, link_drop_foreign_nexthops(link
));
1067 RET_GATHER(r
, link_drop_foreign_addresses(link
));
1068 RET_GATHER(r
, link_drop_foreign_neighbors(link
));
1069 RET_GATHER(r
, manager_drop_foreign_routing_policy_rules(link
->manager
));
1074 static int link_drop_managed_config(Link
*link
) {
1078 assert(link
->manager
);
1080 r
= link_drop_managed_routes(link
);
1082 RET_GATHER(r
, link_drop_managed_nexthops(link
));
1083 RET_GATHER(r
, link_drop_managed_addresses(link
));
1084 RET_GATHER(r
, link_drop_managed_neighbors(link
));
1085 RET_GATHER(r
, link_drop_managed_routing_policy_rules(link
));
1090 static void link_foreignize_config(Link
*link
) {
1092 assert(link
->manager
);
1094 link_foreignize_routes(link
);
1095 link_foreignize_nexthops(link
);
1096 link_foreignize_addresses(link
);
1097 link_foreignize_neighbors(link
);
1098 link_foreignize_routing_policy_rules(link
);
1101 static int link_configure(Link
*link
) {
1105 assert(link
->network
);
1106 assert(link
->state
== LINK_STATE_INITIALIZED
);
1108 link_set_state(link
, LINK_STATE_CONFIGURING
);
1110 r
= link_new_bound_to_list(link
);
1114 r
= link_request_traffic_control(link
);
1118 r
= link_configure_mtu(link
);
1122 if (link
->iftype
== ARPHRD_CAN
) {
1123 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1124 r
= link_request_to_set_can(link
);
1128 return link_request_to_activate(link
);
1131 r
= link_request_sr_iov_vfs(link
);
1135 r
= link_set_sysctl(link
);
1139 r
= link_request_to_set_mac(link
, /* allow_retry = */ true);
1143 r
= link_request_to_set_ipoib(link
);
1147 r
= link_request_to_set_flags(link
);
1151 r
= link_request_to_set_group(link
);
1155 r
= link_request_to_set_addrgen_mode(link
);
1159 r
= link_request_to_set_master(link
);
1163 r
= link_request_stacked_netdevs(link
);
1167 r
= link_request_to_set_bond(link
);
1171 r
= link_request_to_set_bridge(link
);
1175 r
= link_request_to_set_bridge_vlan(link
);
1179 r
= link_request_to_activate(link
);
1183 r
= ipv4ll_configure(link
);
1187 r
= link_request_dhcp4_client(link
);
1191 r
= link_request_dhcp6_client(link
);
1195 r
= link_request_ndisc(link
);
1199 r
= link_request_dhcp_server(link
);
1203 r
= link_request_radv(link
);
1207 r
= link_lldp_rx_configure(link
);
1211 r
= link_lldp_tx_configure(link
);
1215 r
= link_drop_foreign_config(link
);
1219 r
= link_request_static_configs(link
);
1223 if (!link_has_carrier(link
))
1226 return link_acquire_dynamic_conf(link
);
1229 static int link_get_network(Link
*link
, Network
**ret
) {
1234 assert(link
->manager
);
1237 ORDERED_HASHMAP_FOREACH(network
, link
->manager
->networks
) {
1240 r
= net_match_config(
1244 &link
->permanent_hw_addr
,
1249 link
->alternative_names
,
1258 if (network
->match
.ifname
&& link
->dev
) {
1259 uint8_t name_assign_type
= NET_NAME_UNKNOWN
;
1262 if (sd_device_get_sysattr_value(link
->dev
, "name_assign_type", &attr
) >= 0)
1263 (void) safe_atou8(attr
, &name_assign_type
);
1265 warn
= name_assign_type
== NET_NAME_ENUM
;
1268 log_link_full(link
, warn
? LOG_WARNING
: LOG_DEBUG
,
1269 "found matching network '%s'%s.",
1271 warn
? ", based on potentially unpredictable interface name" : "");
1273 if (network
->unmanaged
)
1283 int link_reconfigure_impl(Link
*link
, bool force
) {
1284 Network
*network
= NULL
;
1285 NetDev
*netdev
= NULL
;
1290 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_LINGER
))
1293 r
= netdev_get(link
->manager
, link
->ifname
, &netdev
);
1294 if (r
< 0 && r
!= -ENOENT
)
1297 r
= link_get_network(link
, &network
);
1298 if (r
< 0 && r
!= -ENOENT
)
1301 if (link
->state
!= LINK_STATE_UNMANAGED
&& !network
)
1302 /* If link is in initialized state, then link->network is also NULL. */
1305 if (link
->network
== network
&& !force
)
1309 _cleanup_free_
char *joined
= strv_join(network
->dropins
, ", ");
1311 if (link
->state
== LINK_STATE_INITIALIZED
)
1312 log_link_info(link
, "Configuring with %s%s%s%s.",
1314 isempty(joined
) ? "" : " (dropins: ",
1316 isempty(joined
) ? "" : ")");
1318 log_link_info(link
, "Reconfiguring with %s%s%s%s.",
1320 isempty(joined
) ? "" : " (dropins: ",
1322 isempty(joined
) ? "" : ")");
1324 log_link_full(link
, link
->state
== LINK_STATE_INITIALIZED
? LOG_DEBUG
: LOG_INFO
,
1325 "Unmanaging interface.");
1327 /* Dropping old .network file */
1328 r
= link_stop_engines(link
, false);
1332 r
= link_drop_requests(link
);
1336 if (network
&& !force
&& network
->keep_configuration
!= KEEP_CONFIGURATION_YES
)
1337 /* When a new/updated .network file is assigned, first make all configs (addresses,
1338 * routes, and so on) foreign, and then drop unnecessary configs later by
1339 * link_drop_foreign_config() in link_configure().
1340 * Note, when KeepConfiguration=yes, link_drop_foreign_config() does nothing. Hence,
1341 * here we need to drop the configs such as addresses, routes, and so on configured by
1342 * the previously assigned .network file. */
1343 link_foreignize_config(link
);
1345 /* Remove all managed configs. Note, foreign configs are removed in later by
1346 * link_configure() -> link_drop_foreign_config() if the link is managed by us. */
1347 r
= link_drop_managed_config(link
);
1352 /* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
1353 * bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
1354 * not propagated to other interfaces anymore. Moreover, it is not necessary to recreate the
1355 * map here, as it depends on .network files assigned to other links. */
1356 link_free_bound_to_list(link
);
1358 link_free_engines(link
);
1359 link
->network
= network_unref(link
->network
);
1361 netdev_unref(link
->netdev
);
1362 link
->netdev
= netdev_ref(netdev
);
1365 link_set_state(link
, LINK_STATE_UNMANAGED
);
1369 /* Then, apply new .network file */
1370 link
->network
= network_ref(network
);
1371 link_update_operstate(link
, true);
1374 link_set_state(link
, LINK_STATE_INITIALIZED
);
1375 link
->activated
= false;
1377 r
= link_configure(link
);
1384 static int link_reconfigure_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, bool force
) {
1389 r
= link_getlink_handler_internal(rtnl
, m
, link
, "Failed to update link state");
1393 r
= link_reconfigure_impl(link
, force
);
1395 link_enter_failed(link
);
1402 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1403 return link_reconfigure_handler_internal(rtnl
, m
, link
, /* force = */ false);
1406 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1407 return link_reconfigure_handler_internal(rtnl
, m
, link
, /* force = */ true);
1410 int link_reconfigure(Link
*link
, bool force
) {
1415 /* When link in pending or initialized state, then link_configure() will be called. To prevent
1416 * the function from being called multiple times simultaneously, refuse to reconfigure the
1417 * interface in these cases. */
1418 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
, LINK_STATE_LINGER
))
1419 return 0; /* 0 means no-op. */
1421 r
= link_call_getlink(link
, force
? link_force_reconfigure_handler
: link_reconfigure_handler
);
1425 return 1; /* 1 means the interface will be reconfigured. */
1428 static int link_initialized_and_synced(Link
*link
) {
1432 assert(link
->manager
);
1434 if (link
->manager
->test_mode
) {
1435 log_link_debug(link
, "Running in test mode, refusing to enter initialized state.");
1436 link_set_state(link
, LINK_STATE_UNMANAGED
);
1440 if (link
->state
== LINK_STATE_PENDING
) {
1441 log_link_debug(link
, "Link state is up-to-date");
1442 link_set_state(link
, LINK_STATE_INITIALIZED
);
1444 r
= link_new_bound_by_list(link
);
1448 r
= link_handle_bound_by_list(link
);
1453 return link_reconfigure_impl(link
, /* force = */ false);
1456 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1459 r
= link_getlink_handler_internal(rtnl
, m
, link
, "Failed to wait for the interface to be initialized");
1463 r
= link_initialized_and_synced(link
);
1465 link_enter_failed(link
);
1470 static int link_initialized(Link
*link
, sd_device
*device
) {
1476 /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1477 * or sysattrs) may be outdated. */
1478 device_unref_and_replace(link
->dev
, device
);
1480 if (link
->dhcp_client
) {
1481 r
= sd_dhcp_client_attach_device(link
->dhcp_client
, link
->dev
);
1483 log_link_warning_errno(link
, r
, "Failed to attach device to DHCPv4 client, ignoring: %m");
1486 if (link
->dhcp6_client
) {
1487 r
= sd_dhcp6_client_attach_device(link
->dhcp6_client
, link
->dev
);
1489 log_link_warning_errno(link
, r
, "Failed to attach device to DHCPv6 client, ignoring: %m");
1492 r
= link_set_sr_iov_ifindices(link
);
1494 log_link_warning_errno(link
, r
, "Failed to manage SR-IOV PF and VF ports, ignoring: %m");
1496 if (link
->state
!= LINK_STATE_PENDING
)
1497 return link_reconfigure(link
, /* force = */ false);
1499 log_link_debug(link
, "udev initialized link");
1501 /* udev has initialized the link, but we don't know if we have yet
1502 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1503 * when it returns we know that the pending NEWLINKs have already been
1504 * processed and that we are up-to-date */
1506 return link_call_getlink(link
, link_initialized_handler
);
1509 static int link_check_initialized(Link
*link
) {
1510 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
1515 if (!udev_available())
1516 return link_initialized_and_synced(link
);
1518 /* udev should be around */
1519 r
= sd_device_new_from_ifindex(&device
, link
->ifindex
);
1521 log_link_debug_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
1525 r
= sd_device_get_is_initialized(device
);
1527 return log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
1530 log_link_debug(link
, "link pending udev initialization...");
1534 r
= device_is_renaming(device
);
1536 return log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
1538 log_link_debug(link
, "Interface is being renamed, pending initialization.");
1542 return link_initialized(link
, device
);
1545 int manager_udev_process_link(Manager
*m
, sd_device
*device
, sd_device_action_t action
) {
1553 r
= sd_device_get_ifindex(device
, &ifindex
);
1555 return log_device_debug_errno(device
, r
, "Failed to get ifindex: %m");
1557 r
= link_get_by_index(m
, ifindex
, &link
);
1559 /* This error is not critical, as the corresponding rtnl message may be received later. */
1560 log_device_debug_errno(device
, r
, "Failed to get link from ifindex %i, ignoring: %m", ifindex
);
1564 /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
1565 * object here. It will be removed only when rtnetlink says so. */
1566 if (action
== SD_DEVICE_REMOVE
) {
1567 link
->dev
= sd_device_unref(link
->dev
);
1571 r
= device_is_renaming(device
);
1573 return log_device_debug_errno(device
, r
, "Failed to determine if the device is renaming or not: %m");
1575 log_device_debug(device
, "Device is renaming, waiting for the interface to be renamed.");
1577 * What happens when a device is initialized, then soon renamed after that? When we detect
1578 * such, maybe we should cancel or postpone all queued requests for the interface. */
1582 r
= sd_device_get_property_value(device
, "ID_NET_MANAGED_BY", &s
);
1583 if (r
< 0 && r
!= -ENOENT
)
1584 log_device_debug_errno(device
, r
, "Failed to get ID_NET_MANAGED_BY udev property, ignoring: %m");
1585 if (r
>= 0 && !streq(s
, "io.systemd.Network")) {
1586 log_device_debug(device
, "Interface is requested to be managed by '%s', not managing the interface.", s
);
1587 link_set_state(link
, LINK_STATE_UNMANAGED
);
1591 r
= link_initialized(link
, device
);
1593 link_enter_failed(link
);
1598 static int link_carrier_gained(Link
*link
) {
1599 bool force_reconfigure
;
1604 r
= event_source_disable(link
->carrier_lost_timer
);
1606 log_link_warning_errno(link
, r
, "Failed to disable carrier lost timer, ignoring: %m");
1608 /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1609 * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1610 * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1611 * restarted, and the correct network information will be gained.
1613 * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1614 * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1615 * already assigned to the interface (in that case, the .network file does not have the SSID=
1616 * setting in the [Match] section), and the interface is already being configured. Of course,
1617 * there may exist another .network file with higher priority and a matching SSID= setting. But
1618 * in that case, link_reconfigure_impl() can handle that without the force_reconfigure flag.
1620 * For non-wireless interfaces, we have no way to detect the connected network change. So,
1621 * setting force_reconfigure = false. Note, both ssid and previous_ssid are NULL in that case. */
1622 force_reconfigure
= link
->previous_ssid
&& !streq_ptr(link
->previous_ssid
, link
->ssid
);
1623 link
->previous_ssid
= mfree(link
->previous_ssid
);
1625 /* AP and P2P-GO interfaces may have a new SSID - update the link properties in case a new .network
1626 * profile wants to match on it with SSID= in its [Match] section.
1628 if (IN_SET(link
->wlan_iftype
, NL80211_IFTYPE_AP
, NL80211_IFTYPE_P2P_GO
)) {
1629 r
= link_get_wlan_interface(link
);
1634 /* At this stage, both wlan and link information should be up-to-date. Hence, it is not necessary to
1635 * call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or NL80211_CMD_GET_STATION commands, and simply call
1636 * link_reconfigure_impl(). Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1637 r
= link_reconfigure_impl(link
, force_reconfigure
);
1641 r
= link_handle_bound_by_list(link
);
1645 if (link
->iftype
== ARPHRD_CAN
)
1646 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1649 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
1650 r
= link_acquire_dynamic_conf(link
);
1654 r
= link_request_static_configs(link
);
1662 static int link_carrier_lost_impl(Link
*link
) {
1667 link
->previous_ssid
= mfree(link
->previous_ssid
);
1669 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1675 r
= link_stop_engines(link
, false);
1679 r
= link_drop_managed_config(link
);
1680 if (r
< 0 && ret
>= 0)
1686 static int link_carrier_lost_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
1687 Link
*link
= ASSERT_PTR(userdata
);
1690 r
= link_carrier_lost_impl(link
);
1692 log_link_warning_errno(link
, r
, "Failed to process carrier lost event: %m");
1693 link_enter_failed(link
);
1699 static int link_carrier_lost(Link
*link
) {
1706 r
= link_handle_bound_by_list(link
);
1710 if (link
->iftype
== ARPHRD_CAN
)
1711 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1717 if (link
->network
->ignore_carrier_loss_set
)
1718 /* If IgnoreCarrierLoss= is explicitly specified, then use the specified value. */
1719 usec
= link
->network
->ignore_carrier_loss_usec
;
1721 else if (link
->network
->bond
&& link
->wlan_iftype
> 0)
1722 /* Enslaving wlan interface to a bond disconnects from the connected AP, and causes its
1723 * carrier to be lost. See #19832. */
1724 usec
= 3 * USEC_PER_SEC
;
1726 else if (link
->network
->dhcp_use_mtu
&&
1728 sd_dhcp_lease_get_mtu(link
->dhcp_lease
, &dhcp_mtu
) >= 0 &&
1729 dhcp_mtu
!= link
->original_mtu
)
1730 /* Some drivers reset interfaces when changing MTU. Resetting interfaces by the static
1731 * MTU should not cause any issues, as MTU is changed only once. However, setting MTU
1732 * through DHCP lease causes an infinite loop of resetting the interface. See #18738. */
1733 usec
= 5 * USEC_PER_SEC
;
1736 /* Otherwise, use the implied default value. */
1737 usec
= link
->network
->ignore_carrier_loss_usec
;
1739 if (usec
== USEC_INFINITY
)
1743 return link_carrier_lost_impl(link
);
1745 return event_reset_time_relative(link
->manager
->event
,
1746 &link
->carrier_lost_timer
,
1750 link_carrier_lost_handler
,
1753 "link-carrier-loss",
1757 static int link_admin_state_up(Link
*link
) {
1762 /* This is called every time an interface admin state changes to up;
1763 * specifically, when IFF_UP flag changes from unset to set. */
1768 if (link
->activated
&& link
->network
->activation_policy
== ACTIVATION_POLICY_ALWAYS_DOWN
) {
1769 log_link_info(link
, "Activation policy is \"always-down\", forcing link down.");
1770 return link_request_to_bring_up_or_down(link
, /* up = */ false);
1773 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1774 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1775 r
= link_set_ipv6_mtu(link
);
1777 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU, ignoring: %m");
1782 static int link_admin_state_down(Link
*link
) {
1788 if (link
->activated
&& link
->network
->activation_policy
== ACTIVATION_POLICY_ALWAYS_UP
) {
1789 log_link_info(link
, "Activation policy is \"always-up\", forcing link up.");
1790 return link_request_to_bring_up_or_down(link
, /* up = */ true);
1796 static bool link_is_enslaved(Link
*link
) {
1797 if (link
->flags
& IFF_SLAVE
)
1800 if (link
->master_ifindex
> 0)
1806 void link_update_operstate(Link
*link
, bool also_update_master
) {
1807 LinkOperationalState operstate
;
1808 LinkCarrierState carrier_state
;
1809 LinkAddressState ipv4_address_state
, ipv6_address_state
, address_state
;
1810 LinkOnlineState online_state
;
1811 _cleanup_strv_free_
char **p
= NULL
;
1812 bool changed
= false;
1816 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
1817 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
1818 else if (link_has_carrier(link
)) {
1819 if (link_is_enslaved(link
))
1820 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
1822 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
1823 } else if (link
->flags
& IFF_UP
)
1824 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
1826 carrier_state
= LINK_CARRIER_STATE_OFF
;
1828 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
1831 SET_FOREACH(slave
, link
->slaves
) {
1832 link_update_operstate(slave
, false);
1834 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
1835 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
1839 link_get_address_states(link
, &ipv4_address_state
, &ipv6_address_state
, &address_state
);
1841 /* Mapping of address and carrier state vs operational state
1843 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1844 * ------------------------------------------------------------------------------
1845 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1846 * address_state degraded | off | no-carrier | dormant | degraded | degraded | enslaved
1847 * routable | off | no-carrier | dormant | routable | routable | routable
1850 if (carrier_state
== LINK_CARRIER_STATE_DEGRADED_CARRIER
&& address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
1851 operstate
= LINK_OPERSTATE_ROUTABLE
;
1852 else if (carrier_state
== LINK_CARRIER_STATE_DEGRADED_CARRIER
&& address_state
== LINK_ADDRESS_STATE_DEGRADED
)
1853 operstate
= LINK_OPERSTATE_DEGRADED
;
1854 else if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
1855 operstate
= (LinkOperationalState
) carrier_state
;
1856 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
1857 operstate
= LINK_OPERSTATE_ROUTABLE
;
1858 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
1859 operstate
= LINK_OPERSTATE_DEGRADED
;
1861 operstate
= LINK_OPERSTATE_ENSLAVED
;
1863 LinkOperationalStateRange req
;
1864 link_required_operstate_for_online(link
, &req
);
1866 /* Only determine online state for managed links with RequiredForOnline=yes */
1867 if (!link
->network
|| !link
->network
->required_for_online
)
1868 online_state
= _LINK_ONLINE_STATE_INVALID
;
1870 else if (!operational_state_is_in_range(operstate
, &req
))
1871 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1874 AddressFamily required_family
= link
->network
->required_family_for_online
;
1875 bool needs_ipv4
= required_family
& ADDRESS_FAMILY_IPV4
;
1876 bool needs_ipv6
= required_family
& ADDRESS_FAMILY_IPV6
;
1878 /* The operational state is within the range required for online.
1879 * If a particular address family is also required, we might revert
1880 * to offline in the blocks below. */
1881 online_state
= LINK_ONLINE_STATE_ONLINE
;
1883 if (req
.min
>= LINK_OPERSTATE_DEGRADED
) {
1884 if (needs_ipv4
&& ipv4_address_state
< LINK_ADDRESS_STATE_DEGRADED
)
1885 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1886 if (needs_ipv6
&& ipv6_address_state
< LINK_ADDRESS_STATE_DEGRADED
)
1887 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1890 if (req
.min
>= LINK_OPERSTATE_ROUTABLE
) {
1891 if (needs_ipv4
&& ipv4_address_state
< LINK_ADDRESS_STATE_ROUTABLE
)
1892 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1893 if (needs_ipv6
&& ipv6_address_state
< LINK_ADDRESS_STATE_ROUTABLE
)
1894 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1898 if (link
->carrier_state
!= carrier_state
) {
1899 link
->carrier_state
= carrier_state
;
1901 if (strv_extend(&p
, "CarrierState") < 0)
1905 if (link
->address_state
!= address_state
) {
1906 link
->address_state
= address_state
;
1908 if (strv_extend(&p
, "AddressState") < 0)
1912 if (link
->ipv4_address_state
!= ipv4_address_state
) {
1913 link
->ipv4_address_state
= ipv4_address_state
;
1915 if (strv_extend(&p
, "IPv4AddressState") < 0)
1919 if (link
->ipv6_address_state
!= ipv6_address_state
) {
1920 link
->ipv6_address_state
= ipv6_address_state
;
1922 if (strv_extend(&p
, "IPv6AddressState") < 0)
1926 if (link
->operstate
!= operstate
) {
1927 link
->operstate
= operstate
;
1929 if (strv_extend(&p
, "OperationalState") < 0)
1933 if (link
->online_state
!= online_state
) {
1934 link
->online_state
= online_state
;
1936 if (strv_extend(&p
, "OnlineState") < 0)
1941 link_send_changed_strv(link
, p
);
1945 if (also_update_master
) {
1948 if (link_get_master(link
, &master
) >= 0)
1949 link_update_operstate(master
, true);
1953 #define FLAG_STRING(string, flag, old, new) \
1954 (((old ^ new) & flag) \
1955 ? ((old & flag) ? (" -" string) : (" +" string)) \
1958 static int link_update_flags(Link
*link
, sd_netlink_message
*message
) {
1959 bool link_was_admin_up
, had_carrier
;
1967 r
= sd_rtnl_message_link_get_flags(message
, &flags
);
1969 return log_link_debug_errno(link
, r
, "rtnl: failed to read link flags: %m");
1971 r
= sd_netlink_message_read_u8(message
, IFLA_OPERSTATE
, &operstate
);
1973 /* If we got a message without operstate, assume the state was unchanged. */
1974 operstate
= link
->kernel_operstate
;
1976 return log_link_debug_errno(link
, r
, "rtnl: failed to read operational state: %m");
1978 if (link
->flags
== flags
&& link
->kernel_operstate
== operstate
)
1981 if (link
->flags
!= flags
) {
1982 unsigned unknown_flags
, unknown_flags_added
, unknown_flags_removed
;
1984 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",
1985 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
1986 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
1987 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
1988 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
1989 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
1990 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
1991 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
1992 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
1993 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
1994 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
1995 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
1996 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
1997 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
1998 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
1999 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
2000 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
2001 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
2002 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
2003 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
2005 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
2006 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
2007 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
2008 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
2009 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
2010 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
2011 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
2012 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
2014 if (unknown_flags_added
)
2015 log_link_debug(link
, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added
);
2017 if (unknown_flags_removed
)
2018 log_link_debug(link
, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed
);
2021 link_was_admin_up
= link
->flags
& IFF_UP
;
2022 had_carrier
= link_has_carrier(link
);
2024 link
->flags
= flags
;
2025 link
->kernel_operstate
= operstate
;
2027 link_update_operstate(link
, true);
2029 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
2030 log_link_info(link
, "Link UP");
2032 r
= link_admin_state_up(link
);
2035 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
)) {
2036 log_link_info(link
, "Link DOWN");
2038 r
= link_admin_state_down(link
);
2043 if (!had_carrier
&& link_has_carrier(link
)) {
2044 log_link_info(link
, "Gained carrier");
2046 r
= link_carrier_gained(link
);
2049 } else if (had_carrier
&& !link_has_carrier(link
)) {
2050 log_link_info(link
, "Lost carrier");
2052 r
= link_carrier_lost(link
);
2060 static int link_update_master(Link
*link
, sd_netlink_message
*message
) {
2061 int master_ifindex
, r
;
2066 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t*) &master_ifindex
);
2070 return log_link_debug_errno(link
, r
, "rtnl: failed to read master ifindex: %m");
2072 if (master_ifindex
== link
->ifindex
)
2075 if (master_ifindex
!= link
->master_ifindex
) {
2076 if (link
->master_ifindex
== 0)
2077 log_link_debug(link
, "Attached to master interface: %i", master_ifindex
);
2078 else if (master_ifindex
== 0)
2079 log_link_debug(link
, "Detached from master interface: %i", link
->master_ifindex
);
2081 log_link_debug(link
, "Master interface changed: %i %s %i", link
->master_ifindex
,
2082 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), master_ifindex
);
2084 link_drop_from_master(link
);
2085 link
->master_ifindex
= master_ifindex
;
2088 r
= link_append_to_master(link
);
2090 return log_link_debug_errno(link
, r
, "Failed to append link to master: %m");
2095 static int link_update_driver(Link
*link
, sd_netlink_message
*message
) {
2099 assert(link
->manager
);
2102 /* Driver is already read. Assuming the driver is never changed. */
2103 if (link
->ethtool_driver_read
)
2106 /* When udevd is running, read the driver after the interface is initialized by udevd.
2107 * Otherwise, ethtool may not work correctly. See issue #22538.
2108 * When udevd is not running, read the value when the interface is detected. */
2109 if (udev_available() && !link
->dev
)
2112 link
->ethtool_driver_read
= true;
2114 r
= ethtool_get_driver(&link
->manager
->ethtool_fd
, link
->ifname
, &link
->driver
);
2116 log_link_debug_errno(link
, r
, "Failed to get driver, continuing without: %m");
2120 log_link_debug(link
, "Found driver: %s", strna(link
->driver
));
2122 if (streq_ptr(link
->driver
, "dsa")) {
2123 uint32_t dsa_master_ifindex
= 0;
2125 r
= sd_netlink_message_read_u32(message
, IFLA_LINK
, &dsa_master_ifindex
);
2126 if (r
< 0 && r
!= -ENODATA
)
2127 return log_link_debug_errno(link
, r
, "rtnl: failed to read ifindex of the DSA master interface: %m");
2129 if (dsa_master_ifindex
> INT_MAX
) {
2130 log_link_debug(link
, "rtnl: received too large DSA master ifindex (%"PRIu32
" > INT_MAX), ignoring.",
2131 dsa_master_ifindex
);
2132 dsa_master_ifindex
= 0;
2135 link
->dsa_master_ifindex
= (int) dsa_master_ifindex
;
2138 return 1; /* needs reconfigure */
2141 static int link_update_permanent_hardware_address_from_ethtool(Link
*link
, sd_netlink_message
*message
) {
2145 assert(link
->manager
);
2148 if (link
->ethtool_permanent_hw_addr_read
)
2151 /* When udevd is running, read the permanent hardware address after the interface is
2152 * initialized by udevd. Otherwise, ethtool may not work correctly. See issue #22538.
2153 * When udevd is not running, read the value when the interface is detected. */
2154 if (udev_available() && !link
->dev
)
2157 /* If the interface does not have a hardware address, then it will not have a permanent address either. */
2158 r
= netlink_message_read_hw_addr(message
, IFLA_ADDRESS
, NULL
);
2162 return log_link_debug_errno(link
, r
, "Failed to read IFLA_ADDRESS attribute: %m");
2164 link
->ethtool_permanent_hw_addr_read
= true;
2166 r
= ethtool_get_permanent_hw_addr(&link
->manager
->ethtool_fd
, link
->ifname
, &link
->permanent_hw_addr
);
2168 log_link_debug_errno(link
, r
, "Permanent hardware address not found, continuing without: %m");
2173 static int link_update_permanent_hardware_address(Link
*link
, sd_netlink_message
*message
) {
2177 assert(link
->manager
);
2180 if (link
->permanent_hw_addr
.length
> 0)
2183 r
= netlink_message_read_hw_addr(message
, IFLA_PERM_ADDRESS
, &link
->permanent_hw_addr
);
2186 return log_link_debug_errno(link
, r
, "Failed to read IFLA_PERM_ADDRESS attribute: %m");
2188 /* Fallback to ethtool for older kernels. */
2189 r
= link_update_permanent_hardware_address_from_ethtool(link
, message
);
2194 if (link
->permanent_hw_addr
.length
> 0)
2195 log_link_debug(link
, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link
->permanent_hw_addr
));
2197 return 1; /* needs reconfigure */
2200 static int link_update_hardware_address(Link
*link
, sd_netlink_message
*message
) {
2201 struct hw_addr_data addr
;
2207 r
= netlink_message_read_hw_addr(message
, IFLA_BROADCAST
, &link
->bcast_addr
);
2208 if (r
< 0 && r
!= -ENODATA
)
2209 return log_link_debug_errno(link
, r
, "rtnl: failed to read broadcast address: %m");
2211 r
= netlink_message_read_hw_addr(message
, IFLA_ADDRESS
, &addr
);
2215 return log_link_debug_errno(link
, r
, "rtnl: failed to read hardware address: %m");
2217 if (hw_addr_equal(&link
->hw_addr
, &addr
))
2220 if (link
->hw_addr
.length
== 0)
2221 log_link_debug(link
, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr
));
2223 log_link_debug(link
, "Hardware address is changed: %s %s %s",
2224 HW_ADDR_TO_STR(&link
->hw_addr
),
2225 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
),
2226 HW_ADDR_TO_STR(&addr
));
2228 hashmap_remove_value(link
->manager
->links_by_hw_addr
, &link
->hw_addr
, link
);
2231 link
->hw_addr
= addr
;
2233 if (!hw_addr_is_null(&link
->hw_addr
)) {
2234 r
= hashmap_ensure_put(&link
->manager
->links_by_hw_addr
, &hw_addr_hash_ops
, &link
->hw_addr
, link
);
2235 if (r
== -EEXIST
&& streq_ptr(link
->kind
, "bond"))
2236 /* bonding master and its slaves have the same hardware address. */
2237 r
= hashmap_replace(link
->manager
->links_by_hw_addr
, &link
->hw_addr
, link
);
2239 log_link_debug_errno(link
, r
, "Failed to manage link by its new hardware address, ignoring: %m");
2242 r
= ipv4acd_update_mac(link
);
2244 return log_link_debug_errno(link
, r
, "Could not update MAC address in IPv4 ACD client: %m");
2246 r
= ipv4ll_update_mac(link
);
2248 return log_link_debug_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2250 r
= dhcp4_update_mac(link
);
2252 return log_link_debug_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2254 r
= dhcp6_update_mac(link
);
2256 return log_link_debug_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2258 r
= radv_update_mac(link
);
2260 return log_link_debug_errno(link
, r
, "Could not update MAC address for Router Advertisement: %m");
2262 if (link
->ndisc
&& link
->hw_addr
.length
== ETH_ALEN
) {
2263 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->hw_addr
.ether
);
2265 return log_link_debug_errno(link
, r
, "Could not update MAC for NDisc: %m");
2268 if (link
->lldp_rx
) {
2269 r
= sd_lldp_rx_set_filter_address(link
->lldp_rx
, &link
->hw_addr
.ether
);
2271 return log_link_debug_errno(link
, r
, "Could not update MAC address for LLDP Rx: %m");
2274 if (link
->lldp_tx
) {
2275 r
= sd_lldp_tx_set_hwaddr(link
->lldp_tx
, &link
->hw_addr
.ether
);
2277 return log_link_debug_errno(link
, r
, "Could not update MAC address for LLDP Tx: %m");
2280 return 1; /* needs reconfigure */
2283 static int link_update_mtu(Link
*link
, sd_netlink_message
*message
) {
2284 uint32_t mtu
, min_mtu
= 0, max_mtu
= UINT32_MAX
;
2290 r
= sd_netlink_message_read_u32(message
, IFLA_MTU
, &mtu
);
2294 return log_link_debug_errno(link
, r
, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
2298 r
= sd_netlink_message_read_u32(message
, IFLA_MIN_MTU
, &min_mtu
);
2299 if (r
< 0 && r
!= -ENODATA
)
2300 return log_link_debug_errno(link
, r
, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2302 r
= sd_netlink_message_read_u32(message
, IFLA_MAX_MTU
, &max_mtu
);
2303 if (r
< 0 && r
!= -ENODATA
)
2304 return log_link_debug_errno(link
, r
, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2307 max_mtu
= UINT32_MAX
;
2309 link
->min_mtu
= min_mtu
;
2310 link
->max_mtu
= max_mtu
;
2312 if (link
->original_mtu
== 0) {
2313 link
->original_mtu
= mtu
;
2314 log_link_debug(link
, "Saved original MTU %" PRIu32
" (min: %"PRIu32
", max: %"PRIu32
")",
2315 link
->original_mtu
, link
->min_mtu
, link
->max_mtu
);
2318 if (link
->mtu
== mtu
)
2322 log_link_debug(link
, "MTU is changed: %"PRIu32
" %s %"PRIu32
" (min: %"PRIu32
", max: %"PRIu32
")",
2323 link
->mtu
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), mtu
,
2324 link
->min_mtu
, link
->max_mtu
);
2328 if (link
->dhcp_client
) {
2329 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
2331 return log_link_debug_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2335 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
2337 return log_link_debug_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
2343 static int link_update_alternative_names(Link
*link
, sd_netlink_message
*message
) {
2344 _cleanup_strv_free_
char **altnames
= NULL
;
2350 r
= sd_netlink_message_read_strv(message
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
2352 /* The message does not have IFLA_PROP_LIST container attribute. It does not mean the
2353 * interface has no alternative name. */
2356 return log_link_debug_errno(link
, r
, "rtnl: failed to read alternative names: %m");
2358 if (strv_equal(altnames
, link
->alternative_names
))
2361 STRV_FOREACH(n
, link
->alternative_names
)
2362 hashmap_remove(link
->manager
->links_by_name
, *n
);
2364 strv_free_and_replace(link
->alternative_names
, altnames
);
2366 STRV_FOREACH(n
, link
->alternative_names
) {
2367 r
= hashmap_ensure_put(&link
->manager
->links_by_name
, &string_hash_ops
, *n
, link
);
2369 return log_link_debug_errno(link
, r
, "Failed to manage link by its new alternative names: %m");
2372 return 1; /* needs reconfigure */
2375 static int link_update_name(Link
*link
, sd_netlink_message
*message
) {
2376 char ifname_from_index
[IF_NAMESIZE
];
2383 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
2388 return log_link_debug_errno(link
, r
, "Failed to read interface name in RTM_NEWLINK message: %m");
2390 if (streq(ifname
, link
->ifname
))
2393 r
= format_ifname(link
->ifindex
, ifname_from_index
);
2395 return log_link_debug_errno(link
, r
, "Could not get interface name for index %i.", link
->ifindex
);
2397 if (!streq(ifname
, ifname_from_index
)) {
2398 log_link_debug(link
, "New interface name '%s' received from the kernel does not correspond "
2399 "with the name currently configured on the actual interface '%s'. Ignoring.",
2400 ifname
, ifname_from_index
);
2404 log_link_info(link
, "Interface name change detected, renamed to %s.", ifname
);
2406 hashmap_remove(link
->manager
->links_by_name
, link
->ifname
);
2408 r
= free_and_strdup(&link
->ifname
, ifname
);
2410 return log_oom_debug();
2412 r
= hashmap_ensure_put(&link
->manager
->links_by_name
, &string_hash_ops
, link
->ifname
, link
);
2414 return log_link_debug_errno(link
, r
, "Failed to manage link by its new name: %m");
2416 if (link
->dhcp_client
) {
2417 r
= sd_dhcp_client_set_ifname(link
->dhcp_client
, link
->ifname
);
2419 return log_link_debug_errno(link
, r
, "Failed to update interface name in DHCP client: %m");
2422 if (link
->dhcp6_client
) {
2423 r
= sd_dhcp6_client_set_ifname(link
->dhcp6_client
, link
->ifname
);
2425 return log_link_debug_errno(link
, r
, "Failed to update interface name in DHCP6 client: %m");
2429 r
= sd_ndisc_set_ifname(link
->ndisc
, link
->ifname
);
2431 return log_link_debug_errno(link
, r
, "Failed to update interface name in NDisc: %m");
2434 if (link
->dhcp_server
) {
2435 r
= sd_dhcp_server_set_ifname(link
->dhcp_server
, link
->ifname
);
2437 return log_link_debug_errno(link
, r
, "Failed to update interface name in DHCP server: %m");
2441 r
= sd_radv_set_ifname(link
->radv
, link
->ifname
);
2443 return log_link_debug_errno(link
, r
, "Failed to update interface name in Router Advertisement: %m");
2446 if (link
->lldp_rx
) {
2447 r
= sd_lldp_rx_set_ifname(link
->lldp_rx
, link
->ifname
);
2449 return log_link_debug_errno(link
, r
, "Failed to update interface name in LLDP Rx: %m");
2452 if (link
->lldp_tx
) {
2453 r
= sd_lldp_tx_set_ifname(link
->lldp_tx
, link
->ifname
);
2455 return log_link_debug_errno(link
, r
, "Failed to update interface name in LLDP Tx: %m");
2459 r
= sd_ipv4ll_set_ifname(link
->ipv4ll
, link
->ifname
);
2461 return log_link_debug_errno(link
, r
, "Failed to update interface name in IPv4LL client: %m");
2464 r
= ipv4acd_set_ifname(link
);
2466 return log_link_debug_errno(link
, r
, "Failed to update interface name in IPv4ACD client: %m");
2468 return 1; /* needs reconfigure */
2471 static int link_update(Link
*link
, sd_netlink_message
*message
) {
2472 bool needs_reconfigure
= false;
2478 r
= link_update_name(link
, message
);
2481 needs_reconfigure
= needs_reconfigure
|| r
> 0;
2483 r
= link_update_alternative_names(link
, message
);
2486 needs_reconfigure
= needs_reconfigure
|| r
> 0;
2488 r
= link_update_mtu(link
, message
);
2492 r
= link_update_driver(link
, message
);
2495 needs_reconfigure
= needs_reconfigure
|| r
> 0;
2497 r
= link_update_permanent_hardware_address(link
, message
);
2500 needs_reconfigure
= needs_reconfigure
|| r
> 0;
2502 r
= link_update_hardware_address(link
, message
);
2505 needs_reconfigure
= needs_reconfigure
|| r
> 0;
2507 r
= link_update_master(link
, message
);
2511 r
= link_update_ipv6ll_addrgen_mode(link
, message
);
2515 r
= link_update_flags(link
, message
);
2519 r
= link_update_bridge_vlan(link
, message
);
2523 return needs_reconfigure
;
2526 static Link
*link_drop_or_unref(Link
*link
) {
2530 return link_unref(link
);
2531 return link_drop(link
);
2534 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_drop_or_unref
);
2536 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
2537 _cleanup_free_
char *ifname
= NULL
, *kind
= NULL
, *state_file
= NULL
, *lease_file
= NULL
, *lldp_file
= NULL
;
2538 _cleanup_(link_drop_or_unrefp
) Link
*link
= NULL
;
2539 unsigned short iftype
;
2546 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
2548 return log_debug_errno(r
, "rtnl: failed to read ifindex from link message: %m");
2549 else if (ifindex
<= 0)
2550 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "rtnl: received link message without valid ifindex.");
2552 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
2554 return log_debug_errno(r
, "rtnl: failed to read interface type from link message: %m");
2556 r
= sd_netlink_message_read_string_strdup(message
, IFLA_IFNAME
, &ifname
);
2558 return log_debug_errno(r
, "rtnl: failed to read interface name from link message: %m");
2560 /* check for link kind */
2561 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
2563 r
= sd_netlink_message_read_string_strdup(message
, IFLA_INFO_KIND
, &kind
);
2564 if (r
< 0 && r
!= -ENODATA
)
2565 return log_debug_errno(r
, "rtnl: failed to read interface kind from link message: %m");
2566 r
= sd_netlink_message_exit_container(message
);
2568 return log_debug_errno(r
, "rtnl: failed to exit IFLA_LINKINFO container: %m");
2571 if (!manager
->test_mode
) {
2572 /* Do not update state files when running in test mode. */
2573 if (asprintf(&state_file
, "/run/systemd/netif/links/%d", ifindex
) < 0)
2574 return log_oom_debug();
2576 if (asprintf(&lease_file
, "/run/systemd/netif/leases/%d", ifindex
) < 0)
2577 return log_oom_debug();
2579 if (asprintf(&lldp_file
, "/run/systemd/netif/lldp/%d", ifindex
) < 0)
2580 return log_oom_debug();
2583 link
= new(Link
, 1);
2589 .state
= LINK_STATE_PENDING
,
2590 .online_state
= _LINK_ONLINE_STATE_INVALID
,
2591 .automatic_reconfigure_ratelimit
= (const RateLimit
) { .interval
= 10 * USEC_PER_SEC
, .burst
= 5 },
2594 .ifname
= TAKE_PTR(ifname
),
2595 .kind
= TAKE_PTR(kind
),
2597 .bridge_vlan_pvid
= UINT16_MAX
,
2599 .ipv6ll_address_gen_mode
= _IPV6_LINK_LOCAL_ADDRESS_GEN_MODE_INVALID
,
2601 .state_file
= TAKE_PTR(state_file
),
2602 .lease_file
= TAKE_PTR(lease_file
),
2603 .lldp_file
= TAKE_PTR(lldp_file
),
2606 .dns_default_route
= -1,
2607 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
2608 .mdns
= _RESOLVE_SUPPORT_INVALID
,
2609 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
2610 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
2613 r
= hashmap_ensure_put(&manager
->links_by_index
, NULL
, INT_TO_PTR(link
->ifindex
), link
);
2615 return log_link_debug_errno(link
, r
, "Failed to store link into manager: %m");
2617 link
->manager
= manager
;
2619 r
= hashmap_ensure_put(&manager
->links_by_name
, &string_hash_ops
, link
->ifname
, link
);
2621 return log_link_debug_errno(link
, r
, "Failed to manage link by its interface name: %m");
2623 log_link_debug(link
, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2624 link
->ifindex
, strna(arphrd_to_name(link
->iftype
)), link
->iftype
, strna(link
->kind
));
2626 /* If contained in this set, the link is wireless and the corresponding NL80211_CMD_NEW_INTERFACE
2627 * message arrived too early. Request the wireless link information again.
2629 if (set_remove(manager
->new_wlan_ifindices
, INT_TO_PTR(link
->ifindex
))) {
2630 r
= link_get_wlan_interface(link
);
2632 log_link_warning_errno(link
, r
, "Failed to get wireless interface, ignoring: %m");
2635 *ret
= TAKE_PTR(link
);
2639 int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*manager
) {
2641 NetDev
*netdev
= NULL
;
2650 if (sd_netlink_message_is_error(message
)) {
2651 r
= sd_netlink_message_get_errno(message
);
2653 log_message_warning_errno(message
, r
, "rtnl: Could not receive link message, ignoring");
2658 r
= sd_netlink_message_get_type(message
, &type
);
2660 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
2662 } else if (!IN_SET(type
, RTM_NEWLINK
, RTM_DELLINK
)) {
2663 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type
);
2667 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
2669 log_warning_errno(r
, "rtnl: Could not get ifindex from link message, ignoring: %m");
2671 } else if (ifindex
<= 0) {
2672 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex
);
2676 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
2678 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
2682 (void) link_get_by_index(manager
, ifindex
, &link
);
2683 (void) netdev_get(manager
, name
, &netdev
);
2688 /* netdev exists, so make sure the ifindex matches */
2689 r
= netdev_set_ifindex(netdev
, message
);
2691 log_netdev_warning_errno(netdev
, r
, "Could not process new link message for netdev, ignoring: %m");
2697 /* link is new, so add it */
2698 r
= link_new(manager
, message
, &link
);
2700 log_warning_errno(r
, "Could not process new link message: %m");
2704 r
= link_update(link
, message
);
2706 log_link_warning_errno(link
, r
, "Could not process link message: %m");
2707 link_enter_failed(link
);
2711 r
= link_check_initialized(link
);
2713 log_link_warning_errno(link
, r
, "Failed to check link is initialized: %m");
2714 link_enter_failed(link
);
2718 r
= link_update(link
, message
);
2720 log_link_warning_errno(link
, r
, "Could not process link message: %m");
2721 link_enter_failed(link
);
2725 r
= link_reconfigure_impl(link
, /* force = */ false);
2727 log_link_warning_errno(link
, r
, "Failed to reconfigure interface: %m");
2728 link_enter_failed(link
);
2737 netdev_drop(netdev
);
2741 assert_not_reached();
2747 int link_getlink_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, const char *error_msg
) {
2748 uint16_t message_type
;
2755 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2758 r
= sd_netlink_message_get_errno(m
);
2760 log_link_message_warning_errno(link
, m
, r
, error_msg
);
2761 link_enter_failed(link
);
2765 r
= sd_netlink_message_get_type(m
, &message_type
);
2767 log_link_debug_errno(link
, r
, "rtnl: failed to read link message type, ignoring: %m");
2770 if (message_type
!= RTM_NEWLINK
) {
2771 log_link_debug(link
, "rtnl: received invalid link message type, ignoring.");
2775 r
= link_update(link
, m
);
2777 link_enter_failed(link
);
2784 int link_call_getlink(Link
*link
, link_netlink_message_handler_t callback
) {
2785 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2789 assert(link
->manager
);
2790 assert(link
->manager
->rtnl
);
2793 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
, link
->ifindex
);
2797 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
2798 link_netlink_destroy_callback
, link
);
2806 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
2807 [LINK_STATE_PENDING
] = "pending",
2808 [LINK_STATE_INITIALIZED
] = "initialized",
2809 [LINK_STATE_CONFIGURING
] = "configuring",
2810 [LINK_STATE_CONFIGURED
] = "configured",
2811 [LINK_STATE_UNMANAGED
] = "unmanaged",
2812 [LINK_STATE_FAILED
] = "failed",
2813 [LINK_STATE_LINGER
] = "linger",
2816 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
2818 int link_flags_to_string_alloc(uint32_t flags
, char **ret
) {
2819 _cleanup_free_
char *str
= NULL
;
2820 static const char* map
[] = {
2821 [LOG2U(IFF_UP
)] = "up", /* interface is up. */
2822 [LOG2U(IFF_BROADCAST
)] = "broadcast", /* broadcast address valid. */
2823 [LOG2U(IFF_DEBUG
)] = "debug", /* turn on debugging. */
2824 [LOG2U(IFF_LOOPBACK
)] = "loopback", /* interface is a loopback net. */
2825 [LOG2U(IFF_POINTOPOINT
)] = "point-to-point", /* interface has p-p link. */
2826 [LOG2U(IFF_NOTRAILERS
)] = "no-trailers", /* avoid use of trailers. */
2827 [LOG2U(IFF_RUNNING
)] = "running", /* interface RFC2863 OPER_UP. */
2828 [LOG2U(IFF_NOARP
)] = "no-arp", /* no ARP protocol. */
2829 [LOG2U(IFF_PROMISC
)] = "promiscuous", /* receive all packets. */
2830 [LOG2U(IFF_ALLMULTI
)] = "all-multicast", /* receive all multicast packets. */
2831 [LOG2U(IFF_MASTER
)] = "master", /* master of a load balancer. */
2832 [LOG2U(IFF_SLAVE
)] = "slave", /* slave of a load balancer. */
2833 [LOG2U(IFF_MULTICAST
)] = "multicast", /* supports multicast. */
2834 [LOG2U(IFF_PORTSEL
)] = "portsel", /* can set media type. */
2835 [LOG2U(IFF_AUTOMEDIA
)] = "auto-media", /* auto media select active. */
2836 [LOG2U(IFF_DYNAMIC
)] = "dynamic", /* dialup device with changing addresses. */
2837 [LOG2U(IFF_LOWER_UP
)] = "lower-up", /* driver signals L1 up. */
2838 [LOG2U(IFF_DORMANT
)] = "dormant", /* driver signals dormant. */
2839 [LOG2U(IFF_ECHO
)] = "echo", /* echo sent packets. */
2844 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
2845 if (FLAGS_SET(flags
, 1 << i
) && map
[i
])
2846 if (!strextend_with_separator(&str
, ",", map
[i
]))
2849 *ret
= TAKE_PTR(str
);
2853 static const char * const kernel_operstate_table
[] = {
2854 [IF_OPER_UNKNOWN
] = "unknown",
2855 [IF_OPER_NOTPRESENT
] = "not-present",
2856 [IF_OPER_DOWN
] = "down",
2857 [IF_OPER_LOWERLAYERDOWN
] = "lower-layer-down",
2858 [IF_OPER_TESTING
] = "testing",
2859 [IF_OPER_DORMANT
] = "dormant",
2860 [IF_OPER_UP
] = "up",
2863 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate
, int);