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-identifier.h"
21 #include "dhcp-lease-internal.h"
23 #include "ethtool-util.h"
24 #include "event-util.h"
27 #include "format-util.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-can.h"
38 #include "networkd-dhcp-prefix-delegation.h"
39 #include "networkd-dhcp-server.h"
40 #include "networkd-dhcp4.h"
41 #include "networkd-dhcp6.h"
42 #include "networkd-ipv4acd.h"
43 #include "networkd-ipv4ll.h"
44 #include "networkd-ipv6-proxy-ndp.h"
45 #include "networkd-link-bus.h"
46 #include "networkd-link.h"
47 #include "networkd-lldp-tx.h"
48 #include "networkd-manager.h"
49 #include "networkd-ndisc.h"
50 #include "networkd-neighbor.h"
51 #include "networkd-nexthop.h"
52 #include "networkd-queue.h"
53 #include "networkd-radv.h"
54 #include "networkd-route.h"
55 #include "networkd-routing-policy-rule.h"
56 #include "networkd-setlink.h"
57 #include "networkd-sriov.h"
58 #include "networkd-state-file.h"
59 #include "networkd-sysctl.h"
61 #include "socket-util.h"
62 #include "stat-util.h"
63 #include "stdio-util.h"
64 #include "string-table.h"
67 #include "tmpfile-util.h"
68 #include "udev-util.h"
72 bool link_ipv4ll_enabled(Link
*link
) {
75 if (link
->flags
& IFF_LOOPBACK
)
81 if (link
->iftype
== ARPHRD_CAN
)
84 if (link
->hw_addr
.length
!= ETH_ALEN
)
87 if (ether_addr_is_null(&link
->hw_addr
.ether
))
90 /* ARPHRD_INFINIBAND seems to potentially support IPv4LL.
91 * But currently sd-ipv4ll and sd-ipv4acd only support ARPHRD_ETHER. */
92 if (link
->iftype
!= ARPHRD_ETHER
)
95 if (streq_ptr(link
->kind
, "vrf"))
98 /* L3 or L3S mode do not support ARP. */
99 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
102 if (link
->network
->bond
)
105 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
108 bool link_ipv6ll_enabled(Link
*link
) {
111 if (!socket_ipv6_is_supported())
114 if (link
->flags
& IFF_LOOPBACK
)
120 if (link
->iftype
== ARPHRD_CAN
)
123 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
126 if (link
->network
->bond
)
129 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
132 bool link_may_have_ipv6ll(Link
*link
) {
136 * This is equivalent to link_ipv6ll_enabled() for non-WireGuard interfaces.
138 * For WireGuard interface, the kernel does not assign any IPv6LL addresses, but we can assign
139 * it manually. It is necessary to set an IPv6LL address manually to run NDisc or RADV on
140 * WireGuard interface. Note, also Multicast=yes must be set. See #17380.
142 * TODO: May be better to introduce GenerateIPv6LinkLocalAddress= setting, and use algorithms
143 * used in networkd-address-generation.c
146 if (link_ipv6ll_enabled(link
))
149 /* IPv6LL address can be manually assigned on WireGuard interface. */
150 if (streq_ptr(link
->kind
, "wireguard")) {
156 ORDERED_HASHMAP_FOREACH(a
, link
->network
->addresses_by_section
) {
157 if (a
->family
!= AF_INET6
)
159 if (in6_addr_is_set(&a
->in_addr_peer
.in6
))
161 if (in6_addr_is_link_local(&a
->in_addr
.in6
))
169 bool link_ipv6_enabled(Link
*link
) {
172 if (!socket_ipv6_is_supported())
175 if (link
->network
->bond
)
178 if (link
->iftype
== ARPHRD_CAN
)
181 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
182 if (link_ipv6ll_enabled(link
))
185 if (network_has_static_ipv6_configurations(link
->network
))
191 bool link_is_ready_to_configure(Link
*link
, bool allow_unmanaged
) {
194 if (!link
->network
) {
195 if (!allow_unmanaged
)
198 return link_has_carrier(link
);
201 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
204 if (!link
->network
->configure_without_carrier
) {
205 if (link
->set_flags_messages
> 0)
208 if (!link_has_carrier(link
))
212 if (link
->set_link_messages
> 0)
215 if (!link
->activated
)
221 void link_ntp_settings_clear(Link
*link
) {
222 link
->ntp
= strv_free(link
->ntp
);
225 void link_dns_settings_clear(Link
*link
) {
226 if (link
->n_dns
!= UINT_MAX
)
227 for (unsigned i
= 0; i
< link
->n_dns
; i
++)
228 in_addr_full_free(link
->dns
[i
]);
229 link
->dns
= mfree(link
->dns
);
230 link
->n_dns
= UINT_MAX
;
232 link
->search_domains
= ordered_set_free(link
->search_domains
);
233 link
->route_domains
= ordered_set_free(link
->route_domains
);
235 link
->dns_default_route
= -1;
236 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
237 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
238 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
239 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
241 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
244 static void link_free_engines(Link
*link
) {
248 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
249 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
250 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
251 link
->dhcp4_6rd_tunnel_name
= mfree(link
->dhcp4_6rd_tunnel_name
);
253 link
->lldp_rx
= sd_lldp_rx_unref(link
->lldp_rx
);
254 link
->lldp_tx
= sd_lldp_tx_unref(link
->lldp_tx
);
258 link
->ipv4ll
= sd_ipv4ll_unref(link
->ipv4ll
);
259 link
->dhcp6_client
= sd_dhcp6_client_unref(link
->dhcp6_client
);
260 link
->dhcp6_lease
= sd_dhcp6_lease_unref(link
->dhcp6_lease
);
261 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
262 link
->radv
= sd_radv_unref(link
->radv
);
265 static Link
*link_free(Link
*link
) {
268 link_ntp_settings_clear(link
);
269 link_dns_settings_clear(link
);
271 link
->routes
= set_free(link
->routes
);
273 link
->nexthops
= set_free(link
->nexthops
);
275 link
->neighbors
= set_free(link
->neighbors
);
277 link
->addresses
= set_free(link
->addresses
);
279 link
->dhcp_pd_prefixes
= set_free(link
->dhcp_pd_prefixes
);
281 link_free_engines(link
);
284 strv_free(link
->alternative_names
);
287 free(link
->previous_ssid
);
290 unlink_and_free(link
->lease_file
);
291 unlink_and_free(link
->lldp_file
);
292 unlink_and_free(link
->state_file
);
294 sd_device_unref(link
->sd_device
);
296 hashmap_free(link
->bound_to_links
);
297 hashmap_free(link
->bound_by_links
);
299 set_free_with_destructor(link
->slaves
, link_unref
);
301 network_unref(link
->network
);
303 sd_event_source_disable_unref(link
->carrier_lost_timer
);
308 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
310 int link_get_by_index(Manager
*m
, int ifindex
, Link
**ret
) {
316 link
= hashmap_get(m
->links_by_index
, INT_TO_PTR(ifindex
));
325 int link_get_by_name(Manager
*m
, const char *ifname
, Link
**ret
) {
331 link
= hashmap_get(m
->links_by_name
, ifname
);
340 int link_get_by_hw_addr(Manager
*m
, const struct hw_addr_data
*hw_addr
, Link
**ret
) {
346 link
= hashmap_get(m
->links_by_hw_addr
, hw_addr
);
355 int link_get_master(Link
*link
, Link
**ret
) {
357 assert(link
->manager
);
360 if (link
->master_ifindex
<= 0 || link
->master_ifindex
== link
->ifindex
)
363 return link_get_by_index(link
->manager
, link
->master_ifindex
, ret
);
366 void link_set_state(Link
*link
, LinkState state
) {
369 if (link
->state
== state
)
372 log_link_debug(link
, "State changed: %s -> %s",
373 link_state_to_string(link
->state
),
374 link_state_to_string(state
));
378 link_send_changed(link
, "AdministrativeState", NULL
);
382 int link_stop_engines(Link
*link
, bool may_keep_dhcp
) {
386 assert(link
->manager
);
387 assert(link
->manager
->event
);
389 bool keep_dhcp
= may_keep_dhcp
&&
391 !link
->network
->dhcp_send_decline
&& /* IPv4 ACD for the DHCPv4 address is running. */
392 (link
->manager
->restarting
||
393 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
396 k
= sd_dhcp_client_stop(link
->dhcp_client
);
398 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
401 k
= sd_dhcp_server_stop(link
->dhcp_server
);
403 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 server: %m");
405 k
= sd_lldp_rx_stop(link
->lldp_rx
);
407 r
= log_link_warning_errno(link
, k
, "Could not stop LLDP Rx: %m");
409 k
= sd_lldp_tx_stop(link
->lldp_tx
);
411 r
= log_link_warning_errno(link
, k
, "Could not stop LLDP Tx: %m");
413 k
= sd_ipv4ll_stop(link
->ipv4ll
);
415 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
417 k
= ipv4acd_stop(link
);
419 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client: %m");
421 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
423 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
425 k
= dhcp_pd_remove(link
, /* only_marked = */ false);
427 r
= log_link_warning_errno(link
, k
, "Could not remove DHCPv6 PD addresses and routes: %m");
429 k
= sd_ndisc_stop(link
->ndisc
);
431 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
433 k
= sd_radv_stop(link
->radv
);
435 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
440 void link_enter_failed(Link
*link
) {
443 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
446 log_link_warning(link
, "Failed");
448 link_set_state(link
, LINK_STATE_FAILED
);
450 (void) link_stop_engines(link
, false);
453 void link_check_ready(Link
*link
) {
458 if (link
->state
== LINK_STATE_CONFIGURED
)
461 if (link
->state
!= LINK_STATE_CONFIGURING
)
462 return (void) log_link_debug(link
, "%s(): link is in %s state.", __func__
, link_state_to_string(link
->state
));
465 return (void) log_link_debug(link
, "%s(): link is unmanaged.", __func__
);
467 if (!link
->tc_configured
)
468 return (void) log_link_debug(link
, "%s(): traffic controls are not configured.", __func__
);
470 if (link
->set_link_messages
> 0)
471 return (void) log_link_debug(link
, "%s(): link layer is configuring.", __func__
);
473 if (!link
->activated
)
474 return (void) log_link_debug(link
, "%s(): link is not activated.", __func__
);
476 if (link
->iftype
== ARPHRD_CAN
) {
477 /* let's shortcut things for CAN which doesn't need most of checks below. */
478 link_set_state(link
, LINK_STATE_CONFIGURED
);
482 if (!link
->stacked_netdevs_created
)
483 return (void) log_link_debug(link
, "%s(): stacked netdevs are not created.", __func__
);
485 if (!link
->static_addresses_configured
)
486 return (void) log_link_debug(link
, "%s(): static addresses are not configured.", __func__
);
488 SET_FOREACH(a
, link
->addresses
)
489 if (!address_is_ready(a
)) {
490 _cleanup_free_
char *str
= NULL
;
492 (void) in_addr_prefix_to_string(a
->family
, &a
->in_addr
, a
->prefixlen
, &str
);
493 return (void) log_link_debug(link
, "%s(): address %s is not ready.", __func__
, strna(str
));
496 if (!link
->static_address_labels_configured
)
497 return (void) log_link_debug(link
, "%s(): static address labels are not configured.", __func__
);
499 if (!link
->static_bridge_fdb_configured
)
500 return (void) log_link_debug(link
, "%s(): static bridge MDB entries are not configured.", __func__
);
502 if (!link
->static_bridge_mdb_configured
)
503 return (void) log_link_debug(link
, "%s(): static bridge MDB entries are not configured.", __func__
);
505 if (!link
->static_ipv6_proxy_ndp_configured
)
506 return (void) log_link_debug(link
, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__
);
508 if (!link
->static_neighbors_configured
)
509 return (void) log_link_debug(link
, "%s(): static neighbors are not configured.", __func__
);
511 if (!link
->static_nexthops_configured
)
512 return (void) log_link_debug(link
, "%s(): static nexthops are not configured.", __func__
);
514 if (!link
->static_routes_configured
)
515 return (void) log_link_debug(link
, "%s(): static routes are not configured.", __func__
);
517 if (!link
->static_routing_policy_rules_configured
)
518 return (void) log_link_debug(link
, "%s(): static routing policy rules are not configured.", __func__
);
520 if (!link
->sr_iov_configured
)
521 return (void) log_link_debug(link
, "%s(): SR-IOV is not configured.", __func__
);
523 /* IPv6LL is assigned after the link gains its carrier. */
524 if (!link
->network
->configure_without_carrier
&&
525 link_ipv6ll_enabled(link
) &&
526 !in6_addr_is_set(&link
->ipv6ll_address
))
527 return (void) log_link_debug(link
, "%s(): IPv6LL is not configured yet.", __func__
);
529 bool has_dynamic_address
= false;
530 SET_FOREACH(a
, link
->addresses
) {
531 if (address_is_marked(a
))
533 if (!address_exists(a
))
535 if (IN_SET(a
->source
,
536 NETWORK_CONFIG_SOURCE_IPV4LL
,
537 NETWORK_CONFIG_SOURCE_DHCP4
,
538 NETWORK_CONFIG_SOURCE_DHCP6
,
539 NETWORK_CONFIG_SOURCE_DHCP_PD
,
540 NETWORK_CONFIG_SOURCE_NDISC
)) {
541 has_dynamic_address
= true;
546 if ((link_ipv4ll_enabled(link
) || link_dhcp4_enabled(link
) || link_dhcp6_with_address_enabled(link
) ||
547 (link_dhcp_pd_is_enabled(link
) && link
->network
->dhcp_pd_assign
)) && !has_dynamic_address
)
548 /* When DHCP[46] or IPv4LL is enabled, at least one address is acquired by them. */
549 return (void) log_link_debug(link
, "%s(): DHCPv4, DHCPv6, DHCP-PD or IPv4LL is enabled but no dynamic address is assigned yet.", __func__
);
551 /* Ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
552 if (link_ipv4ll_enabled(link
) || link_dhcp4_enabled(link
) ||
553 link_dhcp6_enabled(link
) || link_dhcp_pd_is_enabled(link
) ||
554 (!link
->network
->configure_without_carrier
&& link_ipv6_accept_ra_enabled(link
))) {
556 if (!link
->ipv4ll_address_configured
&& !link
->dhcp4_configured
&&
557 !link
->dhcp6_configured
&& !link
->dhcp_pd_configured
&& !link
->ndisc_configured
)
558 /* When DHCP[46], NDisc, or IPv4LL is enabled, at least one protocol must be finished. */
559 return (void) log_link_debug(link
, "%s(): dynamic addresses or routes are not configured.", __func__
);
561 log_link_debug(link
, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
563 yes_no(link
->ipv4ll_address_configured
),
564 yes_no(link
->dhcp4_configured
),
565 yes_no(link
->dhcp6_configured
),
566 yes_no(link
->dhcp_pd_configured
),
567 yes_no(link
->ndisc_configured
));
570 link_set_state(link
, LINK_STATE_CONFIGURED
);
573 static int link_request_static_configs(Link
*link
) {
577 assert(link
->network
);
578 assert(link
->state
!= _LINK_STATE_INVALID
);
580 r
= link_request_static_addresses(link
);
584 r
= link_request_static_address_labels(link
);
588 r
= link_request_static_bridge_fdb(link
);
592 r
= link_request_static_bridge_mdb(link
);
596 r
= link_request_static_ipv6_proxy_ndp_addresses(link
);
600 r
= link_request_static_neighbors(link
);
604 r
= link_request_static_nexthops(link
, false);
608 r
= link_request_static_routes(link
, false);
612 r
= link_request_static_routing_policy_rules(link
);
619 static int link_request_stacked_netdevs(Link
*link
) {
625 link
->stacked_netdevs_created
= false;
626 link
->stacked_netdevs_after_configured_created
= false;
628 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
) {
629 r
= link_request_stacked_netdev(link
, netdev
);
634 if (link
->create_stacked_netdev_messages
== 0) {
635 link
->stacked_netdevs_created
= true;
636 link_check_ready(link
);
638 if (link
->create_stacked_netdev_after_configured_messages
== 0)
639 link
->stacked_netdevs_after_configured_created
= true;
644 static int link_acquire_dynamic_ipv6_conf(Link
*link
) {
649 r
= radv_start(link
);
651 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Advertisement engine: %m");
653 r
= ndisc_start(link
);
655 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Discovery: %m");
657 r
= dhcp6_start(link
);
659 return log_link_warning_errno(link
, r
, "Failed to start DHCPv6 client: %m");
664 static int link_acquire_dynamic_ipv4_conf(Link
*link
) {
668 assert(link
->manager
);
669 assert(link
->manager
->event
);
671 if (link
->dhcp_client
) {
672 r
= dhcp4_start(link
);
674 return log_link_warning_errno(link
, r
, "Failed to start DHCPv4 client: %m");
676 log_link_debug(link
, "Acquiring DHCPv4 lease.");
678 } else if (link
->ipv4ll
) {
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 int link_new_carrier_maps(Link
*link
) {
898 r
= link_new_bound_by_list(link
);
902 r
= link_handle_bound_by_list(link
);
906 r
= link_new_bound_to_list(link
);
910 return link_handle_bound_to_list(link
);
913 static void link_free_bound_to_list(Link
*link
) {
914 bool updated
= false;
919 while ((bound_to
= hashmap_steal_first(link
->bound_to_links
))) {
922 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
923 link_dirty(bound_to
);
930 static void link_free_bound_by_list(Link
*link
) {
931 bool updated
= false;
936 while ((bound_by
= hashmap_steal_first(link
->bound_by_links
))) {
939 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
940 link_dirty(bound_by
);
941 link_handle_bound_to_list(bound_by
);
949 static void link_free_carrier_maps(Link
*link
) {
952 link_free_bound_to_list(link
);
953 link_free_bound_by_list(link
);
956 static int link_append_to_master(Link
*link
) {
962 /* - The link may have no master.
963 * - RTM_NEWLINK message about master interface may not be received yet. */
964 if (link_get_master(link
, &master
) < 0)
967 r
= set_ensure_put(&master
->slaves
, NULL
, link
);
975 static void link_drop_from_master(Link
*link
) {
983 if (link_get_master(link
, &master
) < 0)
986 link_unref(set_remove(master
->slaves
, link
));
989 static void link_drop_requests(Link
*link
) {
993 assert(link
->manager
);
995 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
)
996 if (req
->link
== link
)
1000 static Link
*link_drop(Link
*link
) {
1006 assert(link
->manager
);
1008 link_set_state(link
, LINK_STATE_LINGER
);
1010 /* Drop all references from other links and manager. Note that async netlink calls may have
1011 * references to the link, and they will be dropped when we receive replies. */
1013 link_drop_requests(link
);
1015 link_free_carrier_maps(link
);
1017 link_drop_from_master(link
);
1019 if (link
->state_file
)
1020 (void) unlink(link
->state_file
);
1024 STRV_FOREACH(n
, link
->alternative_names
)
1025 hashmap_remove(link
->manager
->links_by_name
, *n
);
1026 hashmap_remove(link
->manager
->links_by_name
, link
->ifname
);
1028 /* bonding master and its slaves have the same hardware address. */
1029 if (hashmap_get(link
->manager
->links_by_hw_addr
, &link
->hw_addr
) == link
)
1030 hashmap_remove(link
->manager
->links_by_hw_addr
, &link
->hw_addr
);
1032 /* The following must be called at last. */
1033 assert_se(hashmap_remove(link
->manager
->links_by_index
, INT_TO_PTR(link
->ifindex
)) == link
);
1034 return link_unref(link
);
1037 static int link_drop_foreign_config(Link
*link
) {
1041 assert(link
->manager
);
1043 /* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
1044 * to remove loopback address or addresses used for root NFS. */
1046 if (IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
1048 if (FLAGS_SET(link
->flags
, IFF_LOOPBACK
))
1050 if (link
->network
->keep_configuration
== KEEP_CONFIGURATION_YES
)
1053 r
= link_drop_foreign_routes(link
);
1055 k
= link_drop_foreign_nexthops(link
);
1056 if (k
< 0 && r
>= 0)
1059 k
= link_drop_foreign_addresses(link
);
1060 if (k
< 0 && r
>= 0)
1063 k
= link_drop_foreign_neighbors(link
);
1064 if (k
< 0 && r
>= 0)
1067 k
= manager_drop_foreign_routing_policy_rules(link
->manager
);
1068 if (k
< 0 && r
>= 0)
1074 static int link_drop_config(Link
*link
) {
1078 assert(link
->manager
);
1080 r
= link_drop_routes(link
);
1082 k
= link_drop_nexthops(link
);
1083 if (k
< 0 && r
>= 0)
1086 k
= link_drop_addresses(link
);
1087 if (k
< 0 && r
>= 0)
1090 k
= link_drop_neighbors(link
);
1091 if (k
< 0 && r
>= 0)
1094 k
= link_drop_routing_policy_rules(link
);
1095 if (k
< 0 && r
>= 0)
1103 static void link_foreignize_config(Link
*link
) {
1105 assert(link
->manager
);
1107 link_foreignize_routes(link
);
1108 link_foreignize_nexthops(link
);
1109 link_foreignize_addresses(link
);
1110 link_foreignize_neighbors(link
);
1111 link_foreignize_routing_policy_rules(link
);
1114 static int link_configure(Link
*link
) {
1118 assert(link
->network
);
1119 assert(link
->state
== LINK_STATE_INITIALIZED
);
1121 link_set_state(link
, LINK_STATE_CONFIGURING
);
1123 r
= link_configure_traffic_control(link
);
1127 if (link
->iftype
== ARPHRD_CAN
) {
1128 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1129 r
= link_request_to_set_can(link
);
1133 return link_request_to_activate(link
);
1136 r
= link_configure_sr_iov(link
);
1140 r
= link_set_sysctl(link
);
1144 r
= link_request_to_set_mac(link
, /* allow_retry = */ true);
1148 r
= link_request_to_set_ipoib(link
);
1152 r
= link_request_to_set_flags(link
);
1156 r
= link_request_to_set_group(link
);
1160 r
= link_configure_mtu(link
);
1164 r
= link_request_to_set_addrgen_mode(link
);
1168 r
= link_request_to_set_master(link
);
1172 r
= link_request_stacked_netdevs(link
);
1176 r
= link_request_to_set_bond(link
);
1180 r
= link_request_to_set_bridge(link
);
1184 r
= link_request_to_set_bridge_vlan(link
);
1188 r
= link_request_to_activate(link
);
1192 r
= ipv4ll_configure(link
);
1196 r
= link_request_dhcp4_client(link
);
1200 r
= link_request_dhcp6_client(link
);
1204 r
= ndisc_configure(link
);
1208 r
= link_request_dhcp_server(link
);
1212 r
= link_request_radv(link
);
1216 r
= link_lldp_rx_configure(link
);
1220 r
= link_lldp_tx_configure(link
);
1224 r
= link_drop_foreign_config(link
);
1228 r
= link_request_static_configs(link
);
1232 if (!link_has_carrier(link
))
1235 return link_acquire_dynamic_conf(link
);
1238 static int link_get_network(Link
*link
, Network
**ret
) {
1243 assert(link
->manager
);
1246 ORDERED_HASHMAP_FOREACH(network
, link
->manager
->networks
) {
1249 r
= net_match_config(
1253 &link
->permanent_hw_addr
,
1257 link
->alternative_names
,
1266 if (network
->match
.ifname
&& link
->sd_device
) {
1267 uint8_t name_assign_type
= NET_NAME_UNKNOWN
;
1270 if (sd_device_get_sysattr_value(link
->sd_device
, "name_assign_type", &attr
) >= 0)
1271 (void) safe_atou8(attr
, &name_assign_type
);
1273 warn
= name_assign_type
== NET_NAME_ENUM
;
1276 log_link_full(link
, warn
? LOG_WARNING
: LOG_DEBUG
,
1277 "found matching network '%s'%s.",
1279 warn
? ", based on potentially unpredictable interface name" : "");
1281 if (network
->unmanaged
)
1291 static int link_reconfigure_impl(Link
*link
, bool force
) {
1292 Network
*network
= NULL
;
1297 r
= link_get_network(link
, &network
);
1298 if (r
< 0 && r
!= -ENOENT
)
1301 if (link
->network
== network
&& !force
)
1305 log_link_info(link
, "Reconfiguring with %s.", network
->filename
);
1307 log_link_info(link
, "Unmanaging interface.");
1309 /* Dropping old .network file */
1310 r
= link_stop_engines(link
, false);
1314 link_drop_requests(link
);
1316 if (network
&& !force
)
1317 /* When a new/updated .network file is assigned, first make all configs (addresses,
1318 * routes, and so on) foreign, and then drop unnecessary configs later by
1319 * link_drop_foreign_config() in link_configure(). */
1320 link_foreignize_config(link
);
1322 r
= link_drop_config(link
);
1327 link_free_carrier_maps(link
);
1328 link_free_engines(link
);
1329 link
->network
= network_unref(link
->network
);
1332 link_set_state(link
, LINK_STATE_UNMANAGED
);
1336 /* Then, apply new .network file */
1337 link
->network
= network_ref(network
);
1338 link_update_operstate(link
, true);
1341 r
= link_new_carrier_maps(link
);
1345 link_set_state(link
, LINK_STATE_INITIALIZED
);
1346 link
->activated
= false;
1348 r
= link_configure(link
);
1355 static int link_reconfigure_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, bool force
) {
1360 r
= link_getlink_handler_internal(rtnl
, m
, link
, "Failed to update link state");
1364 r
= link_reconfigure_impl(link
, force
);
1366 link_enter_failed(link
);
1373 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1374 return link_reconfigure_handler_internal(rtnl
, m
, link
, /* force = */ false);
1377 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1378 return link_reconfigure_handler_internal(rtnl
, m
, link
, /* force = */ true);
1381 static int link_reconfigure_after_sleep_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1386 r
= link_reconfigure_handler_internal(rtnl
, m
, link
, /* force = */ false);
1390 /* r == 0 means an error occurs, the link is unmanaged, or the matching network file is unchanged. */
1391 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
1394 /* re-request static configs, and restart engines. */
1395 r
= link_stop_engines(link
, false);
1397 link_enter_failed(link
);
1401 r
= link_acquire_dynamic_conf(link
);
1403 link_enter_failed(link
);
1407 r
= link_request_static_configs(link
);
1409 link_enter_failed(link
);
1416 static int link_reconfigure_internal(Link
*link
, link_netlink_message_handler_t callback
) {
1422 /* When link in pending or initialized state, then link_configure() will be called. To prevent
1423 * the function from being called multiple times simultaneously, refuse to reconfigure the
1424 * interface in these cases. */
1425 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
, LINK_STATE_LINGER
))
1426 return 0; /* 0 means no-op. */
1428 r
= link_call_getlink(link
, callback
);
1432 return 1; /* 1 means the interface will be reconfigured. */
1435 int link_reconfigure(Link
*link
, bool force
) {
1436 return link_reconfigure_internal(link
, force
? link_force_reconfigure_handler
: link_reconfigure_handler
);
1439 int link_reconfigure_after_sleep(Link
*link
) {
1440 return link_reconfigure_internal(link
, link_reconfigure_after_sleep_handler
);
1443 static int link_initialized_and_synced(Link
*link
) {
1448 assert(link
->ifname
);
1449 assert(link
->manager
);
1451 if (link
->manager
->test_mode
) {
1452 log_link_debug(link
, "Running in test mode, refusing to enter initialized state.");
1453 link_set_state(link
, LINK_STATE_UNMANAGED
);
1457 /* We may get called either from the asynchronous netlink callback,
1458 * or directly from link_check_initialized() if running in a container. */
1459 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
1462 log_link_debug(link
, "Link state is up-to-date");
1463 link_set_state(link
, LINK_STATE_INITIALIZED
);
1465 r
= link_new_bound_by_list(link
);
1469 r
= link_handle_bound_by_list(link
);
1473 if (!link
->network
) {
1474 r
= link_get_network(link
, &network
);
1476 link_set_state(link
, LINK_STATE_UNMANAGED
);
1482 if (link
->flags
& IFF_LOOPBACK
) {
1483 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
1484 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
1486 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
1487 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
1489 if (network
->dhcp_server
)
1490 log_link_debug(link
, "Ignoring DHCP server for loopback link");
1493 link
->network
= network_ref(network
);
1494 link_update_operstate(link
, false);
1498 r
= link_new_bound_to_list(link
);
1502 return link_configure(link
);
1505 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1508 r
= link_getlink_handler_internal(rtnl
, m
, link
, "Failed to wait for the interface to be initialized");
1512 r
= link_initialized_and_synced(link
);
1514 link_enter_failed(link
);
1519 static int link_initialized(Link
*link
, sd_device
*device
) {
1523 /* Always replace with the new sd_device object. As the sysname (and possibly other properties
1524 * or sysattrs) may be outdated. */
1525 sd_device_ref(device
);
1526 sd_device_unref(link
->sd_device
);
1527 link
->sd_device
= device
;
1529 /* Do not ignore unamanaged state case here. If an interface is renamed after being once
1530 * configured, and the corresponding .network file has Name= in [Match] section, then the
1531 * interface may be already in unmanaged state. See #20657. */
1532 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_UNMANAGED
))
1535 log_link_debug(link
, "udev initialized link");
1536 link_set_state(link
, LINK_STATE_INITIALIZED
);
1538 /* udev has initialized the link, but we don't know if we have yet
1539 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1540 * when it returns we know that the pending NEWLINKs have already been
1541 * processed and that we are up-to-date */
1543 return link_call_getlink(link
, link_initialized_handler
);
1546 static int link_check_initialized(Link
*link
) {
1547 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
1552 if (path_is_read_only_fs("/sys") > 0)
1554 return link_initialized_and_synced(link
);
1556 /* udev should be around */
1557 r
= sd_device_new_from_ifindex(&device
, link
->ifindex
);
1559 log_link_debug_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
1563 r
= sd_device_get_is_initialized(device
);
1565 return log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
1568 log_link_debug(link
, "link pending udev initialization...");
1572 r
= device_is_renaming(device
);
1574 return log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
1576 log_link_debug(link
, "Interface is being renamed, pending initialization.");
1580 return link_initialized(link
, device
);
1583 int manager_udev_process_link(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
1584 sd_device_action_t action
;
1585 Manager
*m
= userdata
;
1592 r
= sd_device_get_action(device
, &action
);
1594 log_device_debug_errno(device
, r
, "Failed to get udev action, ignoring device: %m");
1598 /* Ignore the "remove" uevent — let's remove a device only if rtnetlink says so. All other uevents
1599 * are "positive" events in some form, i.e. inform us about a changed or new network interface, that
1600 * still exists — and we are interested in that. */
1601 if (action
== SD_DEVICE_REMOVE
)
1604 r
= sd_device_get_ifindex(device
, &ifindex
);
1606 log_device_debug_errno(device
, r
, "Ignoring udev %s event for device without ifindex or with invalid ifindex: %m",
1607 device_action_to_string(action
));
1611 r
= device_is_renaming(device
);
1613 log_device_debug_errno(device
, r
, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
1614 device_action_to_string(action
));
1618 log_device_debug(device
, "Interface is under renaming, wait for the interface to be renamed.");
1622 r
= link_get_by_index(m
, ifindex
, &link
);
1624 log_device_debug_errno(device
, r
, "Failed to get link from ifindex %i, ignoring: %m", ifindex
);
1628 r
= link_initialized(link
, device
);
1630 link_enter_failed(link
);
1635 static int link_carrier_gained(Link
*link
) {
1636 bool force_reconfigure
;
1641 r
= event_source_disable(link
->carrier_lost_timer
);
1643 log_link_warning_errno(link
, r
, "Failed to disable carrier lost timer, ignoring: %m");
1645 /* If a wireless interface was connected to an access point, and the SSID is changed (that is,
1646 * both previous_ssid and ssid are non-NULL), then the connected wireless network could be
1647 * changed. So, always reconfigure the link. Which means e.g. the DHCP client will be
1648 * restarted, and the correct network information will be gained.
1650 * However, do not reconfigure the wireless interface forcibly if it was not connected to any
1651 * access points previously (previous_ssid is NULL in this case). As, a .network file may be
1652 * already assigned to the interface (in that case, the .network file does not have the SSID=
1653 * setting in the [Match] section), and the interface is already being configured. Of course,
1654 * there may exist another .network file with higher priority and a matching SSID= setting. But
1655 * in that case, link_reconfigure_impl() can handle that without the force_reconfigure flag.
1657 * For non-wireless interfaces, we have no way to detect the connected network change. So,
1658 * setting force_reconfigure = false. Note, both ssid and previous_ssid are NULL in that case. */
1659 force_reconfigure
= link
->previous_ssid
&& !streq_ptr(link
->previous_ssid
, link
->ssid
);
1660 link
->previous_ssid
= mfree(link
->previous_ssid
);
1662 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_FAILED
, LINK_STATE_LINGER
)) {
1663 /* At this stage, both wlan and link information should be up-to-date. Hence,
1664 * it is not necessary to call RTM_GETLINK, NL80211_CMD_GET_INTERFACE, or
1665 * NL80211_CMD_GET_STATION commands, and simply call link_reconfigure_impl().
1666 * Note, link_reconfigure_impl() returns 1 when the link is reconfigured. */
1667 r
= link_reconfigure_impl(link
, force_reconfigure
);
1672 r
= link_handle_bound_by_list(link
);
1676 if (link
->iftype
== ARPHRD_CAN
)
1677 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1680 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
1681 r
= link_acquire_dynamic_conf(link
);
1685 r
= link_request_static_configs(link
);
1693 static int link_carrier_lost_impl(Link
*link
) {
1698 link
->previous_ssid
= mfree(link
->previous_ssid
);
1700 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1706 r
= link_stop_engines(link
, false);
1710 r
= link_drop_config(link
);
1711 if (r
< 0 && ret
>= 0)
1717 static int link_carrier_lost_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
1718 Link
*link
= userdata
;
1723 r
= link_carrier_lost_impl(link
);
1725 log_link_warning_errno(link
, r
, "Failed to process carrier lost event: %m");
1726 link_enter_failed(link
);
1732 static int link_carrier_lost(Link
*link
) {
1737 r
= link_handle_bound_by_list(link
);
1741 if (link
->iftype
== ARPHRD_CAN
)
1742 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1748 if (link
->network
->ignore_carrier_loss_usec
== USEC_INFINITY
)
1751 if (link
->network
->ignore_carrier_loss_usec
== 0)
1752 return link_carrier_lost_impl(link
);
1754 return event_reset_time_relative(link
->manager
->event
,
1755 &link
->carrier_lost_timer
,
1756 clock_boottime_or_monotonic(),
1757 link
->network
->ignore_carrier_loss_usec
,
1759 link_carrier_lost_handler
,
1762 "link-carrier-loss",
1766 static int link_admin_state_up(Link
*link
) {
1771 /* This is called every time an interface admin state changes to up;
1772 * specifically, when IFF_UP flag changes from unset to set. */
1777 if (link
->activated
&& link
->network
->activation_policy
== ACTIVATION_POLICY_ALWAYS_DOWN
) {
1778 log_link_info(link
, "ActivationPolicy is \"always-off\", forcing link down.");
1779 return link_request_to_bring_up_or_down(link
, /* up = */ false);
1782 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1783 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1784 r
= link_set_ipv6_mtu(link
);
1786 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU, ignoring: %m");
1791 static int link_admin_state_down(Link
*link
) {
1797 if (link
->activated
&& link
->network
->activation_policy
== ACTIVATION_POLICY_ALWAYS_UP
) {
1798 log_link_info(link
, "ActivationPolicy is \"always-on\", forcing link up.");
1799 return link_request_to_bring_up_or_down(link
, /* up = */ true);
1805 bool link_has_carrier(Link
*link
) {
1806 /* see Documentation/networking/operstates.txt in the kernel sources */
1808 if (link
->kernel_operstate
== IF_OPER_UP
)
1811 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1812 /* operstate may not be implemented, so fall back to flags */
1813 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1814 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1820 static bool link_is_enslaved(Link
*link
) {
1821 if (link
->flags
& IFF_SLAVE
)
1824 if (link
->master_ifindex
> 0)
1830 static LinkAddressState
address_state_from_scope(uint8_t scope
) {
1831 if (scope
< RT_SCOPE_SITE
)
1832 /* universally accessible addresses found */
1833 return LINK_ADDRESS_STATE_ROUTABLE
;
1835 if (scope
< RT_SCOPE_HOST
)
1836 /* only link or site local addresses found */
1837 return LINK_ADDRESS_STATE_DEGRADED
;
1839 /* no useful addresses found */
1840 return LINK_ADDRESS_STATE_OFF
;
1843 void link_update_operstate(Link
*link
, bool also_update_master
) {
1844 LinkOperationalState operstate
;
1845 LinkCarrierState carrier_state
;
1846 LinkAddressState ipv4_address_state
, ipv6_address_state
, address_state
;
1847 LinkOnlineState online_state
;
1848 _cleanup_strv_free_
char **p
= NULL
;
1849 uint8_t ipv4_scope
= RT_SCOPE_NOWHERE
, ipv6_scope
= RT_SCOPE_NOWHERE
;
1850 bool changed
= false;
1855 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
1856 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
1857 else if (link_has_carrier(link
)) {
1858 if (link_is_enslaved(link
))
1859 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
1861 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
1862 } else if (link
->flags
& IFF_UP
)
1863 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
1865 carrier_state
= LINK_CARRIER_STATE_OFF
;
1867 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
1870 SET_FOREACH(slave
, link
->slaves
) {
1871 link_update_operstate(slave
, false);
1873 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
1874 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
1878 SET_FOREACH(address
, link
->addresses
) {
1879 if (!address_is_ready(address
))
1882 if (address
->family
== AF_INET
)
1883 ipv4_scope
= MIN(ipv4_scope
, address
->scope
);
1885 if (address
->family
== AF_INET6
)
1886 ipv6_scope
= MIN(ipv6_scope
, address
->scope
);
1889 ipv4_address_state
= address_state_from_scope(ipv4_scope
);
1890 ipv6_address_state
= address_state_from_scope(ipv6_scope
);
1891 address_state
= address_state_from_scope(MIN(ipv4_scope
, ipv6_scope
));
1893 /* Mapping of address and carrier state vs operational state
1895 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1896 * ------------------------------------------------------------------------------
1897 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1898 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
1899 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
1902 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
1903 operstate
= (LinkOperationalState
) carrier_state
;
1904 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
1905 operstate
= LINK_OPERSTATE_ROUTABLE
;
1906 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
1907 operstate
= LINK_OPERSTATE_DEGRADED
;
1909 operstate
= LINK_OPERSTATE_ENSLAVED
;
1911 /* Only determine online state for managed links with RequiredForOnline=yes */
1912 if (!link
->network
|| !link
->network
->required_for_online
)
1913 online_state
= _LINK_ONLINE_STATE_INVALID
;
1914 else if (operstate
< link
->network
->required_operstate_for_online
.min
||
1915 operstate
> link
->network
->required_operstate_for_online
.max
)
1916 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1918 AddressFamily required_family
= link
->network
->required_family_for_online
;
1919 bool needs_ipv4
= required_family
& ADDRESS_FAMILY_IPV4
;
1920 bool needs_ipv6
= required_family
& ADDRESS_FAMILY_IPV6
;
1922 /* The operational state is within the range required for online.
1923 * If a particular address family is also required, we might revert
1924 * to offline in the blocks below. */
1925 online_state
= LINK_ONLINE_STATE_ONLINE
;
1927 if (link
->network
->required_operstate_for_online
.min
>= LINK_OPERSTATE_DEGRADED
) {
1928 if (needs_ipv4
&& ipv4_address_state
< LINK_ADDRESS_STATE_DEGRADED
)
1929 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1930 if (needs_ipv6
&& ipv6_address_state
< LINK_ADDRESS_STATE_DEGRADED
)
1931 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1934 if (link
->network
->required_operstate_for_online
.min
>= LINK_OPERSTATE_ROUTABLE
) {
1935 if (needs_ipv4
&& ipv4_address_state
< LINK_ADDRESS_STATE_ROUTABLE
)
1936 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1937 if (needs_ipv6
&& ipv6_address_state
< LINK_ADDRESS_STATE_ROUTABLE
)
1938 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1942 if (link
->carrier_state
!= carrier_state
) {
1943 link
->carrier_state
= carrier_state
;
1945 if (strv_extend(&p
, "CarrierState") < 0)
1949 if (link
->address_state
!= address_state
) {
1950 link
->address_state
= address_state
;
1952 if (strv_extend(&p
, "AddressState") < 0)
1956 if (link
->ipv4_address_state
!= ipv4_address_state
) {
1957 link
->ipv4_address_state
= ipv4_address_state
;
1959 if (strv_extend(&p
, "IPv4AddressState") < 0)
1963 if (link
->ipv6_address_state
!= ipv6_address_state
) {
1964 link
->ipv6_address_state
= ipv6_address_state
;
1966 if (strv_extend(&p
, "IPv6AddressState") < 0)
1970 if (link
->operstate
!= operstate
) {
1971 link
->operstate
= operstate
;
1973 if (strv_extend(&p
, "OperationalState") < 0)
1977 if (link
->online_state
!= online_state
) {
1978 link
->online_state
= online_state
;
1980 if (strv_extend(&p
, "OnlineState") < 0)
1985 link_send_changed_strv(link
, p
);
1989 if (also_update_master
) {
1992 if (link_get_master(link
, &master
) >= 0)
1993 link_update_operstate(master
, true);
1997 #define FLAG_STRING(string, flag, old, new) \
1998 (((old ^ new) & flag) \
1999 ? ((old & flag) ? (" -" string) : (" +" string)) \
2002 static int link_update_flags(Link
*link
, sd_netlink_message
*message
) {
2003 bool link_was_admin_up
, had_carrier
;
2011 r
= sd_rtnl_message_link_get_flags(message
, &flags
);
2013 return log_link_debug_errno(link
, r
, "rtnl: failed to read link flags: %m");
2015 r
= sd_netlink_message_read_u8(message
, IFLA_OPERSTATE
, &operstate
);
2017 /* If we got a message without operstate, assume the state was unchanged. */
2018 operstate
= link
->kernel_operstate
;
2020 return log_link_debug_errno(link
, r
, "rtnl: failed to read operational state: %m");
2022 if (link
->flags
== flags
&& link
->kernel_operstate
== operstate
)
2025 if (link
->flags
!= flags
) {
2026 unsigned unknown_flags
, unknown_flags_added
, unknown_flags_removed
;
2028 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",
2029 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
2030 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
2031 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
2032 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
2033 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
2034 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
2035 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
2036 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
2037 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
2038 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
2039 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
2040 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
2041 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
2042 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
2043 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
2044 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
2045 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
2046 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
2047 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
2049 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
2050 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
2051 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
2052 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
2053 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
2054 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
2055 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
2056 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
2058 if (unknown_flags_added
)
2059 log_link_debug(link
, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added
);
2061 if (unknown_flags_removed
)
2062 log_link_debug(link
, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed
);
2065 link_was_admin_up
= link
->flags
& IFF_UP
;
2066 had_carrier
= link_has_carrier(link
);
2068 link
->flags
= flags
;
2069 link
->kernel_operstate
= operstate
;
2071 link_update_operstate(link
, true);
2073 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
2074 log_link_info(link
, "Link UP");
2076 r
= link_admin_state_up(link
);
2079 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
)) {
2080 log_link_info(link
, "Link DOWN");
2082 r
= link_admin_state_down(link
);
2087 if (!had_carrier
&& link_has_carrier(link
)) {
2088 log_link_info(link
, "Gained carrier");
2090 r
= link_carrier_gained(link
);
2093 } else if (had_carrier
&& !link_has_carrier(link
)) {
2094 log_link_info(link
, "Lost carrier");
2096 r
= link_carrier_lost(link
);
2104 static int link_update_master(Link
*link
, sd_netlink_message
*message
) {
2105 int master_ifindex
, r
;
2110 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t*) &master_ifindex
);
2114 return log_link_debug_errno(link
, r
, "rtnl: failed to read master ifindex: %m");
2116 if (master_ifindex
== link
->ifindex
)
2119 if (master_ifindex
== link
->master_ifindex
)
2122 if (link
->master_ifindex
== 0)
2123 log_link_debug(link
, "Joined to master interface: %i", master_ifindex
);
2124 else if (master_ifindex
== 0)
2125 log_link_debug(link
, "Leaved from master interface: %i", link
->master_ifindex
);
2127 log_link_debug(link
, "Master interface is changed: %i → %i", link
->master_ifindex
, master_ifindex
);
2129 link_drop_from_master(link
);
2131 link
->master_ifindex
= master_ifindex
;
2133 r
= link_append_to_master(link
);
2135 return log_link_debug_errno(link
, r
, "Failed to append link to master: %m");
2140 static int link_update_hardware_address(Link
*link
, sd_netlink_message
*message
) {
2141 struct hw_addr_data addr
;
2147 r
= netlink_message_read_hw_addr(message
, IFLA_BROADCAST
, &link
->bcast_addr
);
2148 if (r
< 0 && r
!= -ENODATA
)
2149 return log_link_debug_errno(link
, r
, "rtnl: failed to read broadcast address: %m");
2151 r
= netlink_message_read_hw_addr(message
, IFLA_ADDRESS
, &addr
);
2155 return log_link_debug_errno(link
, r
, "rtnl: failed to read hardware address: %m");
2157 if (hw_addr_equal(&link
->hw_addr
, &addr
))
2160 if (link
->hw_addr
.length
== 0)
2161 log_link_debug(link
, "Saved hardware address: %s", HW_ADDR_TO_STR(&addr
));
2163 log_link_debug(link
, "Hardware address is changed: %s → %s",
2164 HW_ADDR_TO_STR(&link
->hw_addr
), HW_ADDR_TO_STR(&addr
));
2166 if (hashmap_get(link
->manager
->links_by_hw_addr
, &link
->hw_addr
) == link
)
2167 hashmap_remove(link
->manager
->links_by_hw_addr
, &link
->hw_addr
);
2170 link
->hw_addr
= addr
;
2172 if (!hw_addr_is_null(&link
->hw_addr
)) {
2173 r
= hashmap_ensure_put(&link
->manager
->links_by_hw_addr
, &hw_addr_hash_ops
, &link
->hw_addr
, link
);
2174 if (r
== -EEXIST
&& streq_ptr(link
->kind
, "bond"))
2175 /* bonding master and its slaves have the same hardware address. */
2176 r
= hashmap_replace(link
->manager
->links_by_hw_addr
, &link
->hw_addr
, link
);
2178 log_link_debug_errno(link
, r
, "Failed to manage link by its new hardware address, ignoring: %m");
2181 r
= ipv4ll_update_mac(link
);
2183 return log_link_debug_errno(link
, r
, "Could not update MAC address in IPv4 ACD client: %m");
2185 r
= ipv4ll_update_mac(link
);
2187 return log_link_debug_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2189 r
= dhcp4_update_mac(link
);
2191 return log_link_debug_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2193 r
= dhcp6_update_mac(link
);
2195 return log_link_debug_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2197 r
= radv_update_mac(link
);
2199 return log_link_debug_errno(link
, r
, "Could not update MAC address for Router Advertisement: %m");
2202 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->hw_addr
.ether
);
2204 return log_link_debug_errno(link
, r
, "Could not update MAC for NDisc: %m");
2207 if (link
->lldp_rx
) {
2208 r
= sd_lldp_rx_set_filter_address(link
->lldp_rx
, &link
->hw_addr
.ether
);
2210 return log_link_debug_errno(link
, r
, "Could not update MAC address for LLDP Rx: %m");
2213 if (link
->lldp_tx
) {
2214 r
= sd_lldp_tx_set_hwaddr(link
->lldp_tx
, &link
->hw_addr
.ether
);
2216 return log_link_debug_errno(link
, r
, "Could not update MAC address for LLDP Tx: %m");
2222 static int link_update_mtu(Link
*link
, sd_netlink_message
*message
) {
2223 uint32_t mtu
, min_mtu
= 0, max_mtu
= UINT32_MAX
;
2229 r
= sd_netlink_message_read_u32(message
, IFLA_MTU
, &mtu
);
2233 return log_link_debug_errno(link
, r
, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
2237 r
= sd_netlink_message_read_u32(message
, IFLA_MIN_MTU
, &min_mtu
);
2238 if (r
< 0 && r
!= -ENODATA
)
2239 return log_link_debug_errno(link
, r
, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2241 r
= sd_netlink_message_read_u32(message
, IFLA_MAX_MTU
, &max_mtu
);
2242 if (r
< 0 && r
!= -ENODATA
)
2243 return log_link_debug_errno(link
, r
, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2246 max_mtu
= UINT32_MAX
;
2248 link
->min_mtu
= min_mtu
;
2249 link
->max_mtu
= max_mtu
;
2251 if (link
->original_mtu
== 0) {
2252 link
->original_mtu
= mtu
;
2253 log_link_debug(link
, "Saved original MTU %" PRIu32
" (min: %"PRIu32
", max: %"PRIu32
")",
2254 link
->original_mtu
, link
->min_mtu
, link
->max_mtu
);
2257 if (link
->mtu
== mtu
)
2261 log_link_debug(link
, "MTU is changed: %"PRIu32
" → %"PRIu32
" (min: %"PRIu32
", max: %"PRIu32
")",
2262 link
->mtu
, mtu
, link
->min_mtu
, link
->max_mtu
);
2266 if (link
->dhcp_client
) {
2267 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
2269 return log_link_debug_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2273 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
2275 return log_link_debug_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
2281 static int link_update_alternative_names(Link
*link
, sd_netlink_message
*message
) {
2282 _cleanup_strv_free_
char **altnames
= NULL
;
2289 r
= sd_netlink_message_read_strv(message
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
2291 /* The message does not have IFLA_PROP_LIST container attribute. It does not means the
2292 * interface has no alternative name. */
2295 return log_link_debug_errno(link
, r
, "rtnl: failed to read alternative names: %m");
2297 if (strv_equal(altnames
, link
->alternative_names
))
2300 STRV_FOREACH(n
, link
->alternative_names
)
2301 hashmap_remove(link
->manager
->links_by_name
, *n
);
2303 strv_free_and_replace(link
->alternative_names
, altnames
);
2305 STRV_FOREACH(n
, link
->alternative_names
) {
2306 r
= hashmap_ensure_put(&link
->manager
->links_by_name
, &string_hash_ops
, *n
, link
);
2308 return log_link_debug_errno(link
, r
, "Failed to manage link by its new alternative names: %m");
2314 static int link_update_name(Link
*link
, sd_netlink_message
*message
) {
2315 char ifname_from_index
[IF_NAMESIZE
];
2322 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
2327 return log_link_debug_errno(link
, r
, "Failed to read interface name in RTM_NEWLINK message: %m");
2329 if (streq(ifname
, link
->ifname
))
2332 r
= format_ifname(link
->ifindex
, ifname_from_index
);
2334 return log_link_debug_errno(link
, r
, "Could not get interface name for index %i.", link
->ifindex
);
2336 if (!streq(ifname
, ifname_from_index
)) {
2337 log_link_debug(link
, "New interface name '%s' received from the kernel does not correspond "
2338 "with the name currently configured on the actual interface '%s'. Ignoring.",
2339 ifname
, ifname_from_index
);
2343 log_link_info(link
, "Interface name change detected, renamed to %s.", ifname
);
2345 hashmap_remove(link
->manager
->links_by_name
, link
->ifname
);
2347 r
= free_and_strdup(&link
->ifname
, ifname
);
2349 return log_oom_debug();
2351 r
= hashmap_ensure_put(&link
->manager
->links_by_name
, &string_hash_ops
, link
->ifname
, link
);
2353 return log_link_debug_errno(link
, r
, "Failed to manage link by its new name: %m");
2355 if (link
->dhcp_client
) {
2356 r
= sd_dhcp_client_set_ifname(link
->dhcp_client
, link
->ifname
);
2358 return log_link_debug_errno(link
, r
, "Failed to update interface name in DHCP client: %m");
2361 if (link
->dhcp6_client
) {
2362 r
= sd_dhcp6_client_set_ifname(link
->dhcp6_client
, link
->ifname
);
2364 return log_link_debug_errno(link
, r
, "Failed to update interface name in DHCP6 client: %m");
2368 r
= sd_ndisc_set_ifname(link
->ndisc
, link
->ifname
);
2370 return log_link_debug_errno(link
, r
, "Failed to update interface name in NDisc: %m");
2373 if (link
->dhcp_server
) {
2374 r
= sd_dhcp_server_set_ifname(link
->dhcp_server
, link
->ifname
);
2376 return log_link_debug_errno(link
, r
, "Failed to update interface name in DHCP server: %m");
2380 r
= sd_radv_set_ifname(link
->radv
, link
->ifname
);
2382 return log_link_debug_errno(link
, r
, "Failed to update interface name in Router Advertisement: %m");
2385 if (link
->lldp_rx
) {
2386 r
= sd_lldp_rx_set_ifname(link
->lldp_rx
, link
->ifname
);
2388 return log_link_debug_errno(link
, r
, "Failed to update interface name in LLDP Rx: %m");
2391 if (link
->lldp_tx
) {
2392 r
= sd_lldp_tx_set_ifname(link
->lldp_tx
, link
->ifname
);
2394 return log_link_debug_errno(link
, r
, "Failed to update interface name in LLDP Tx: %m");
2398 r
= sd_ipv4ll_set_ifname(link
->ipv4ll
, link
->ifname
);
2400 return log_link_debug_errno(link
, r
, "Failed to update interface name in IPv4LL client: %m");
2403 r
= ipv4acd_set_ifname(link
);
2405 return log_link_debug_errno(link
, r
, "Failed to update interface name in IPv4ACD client: %m");
2410 static int link_update(Link
*link
, sd_netlink_message
*message
) {
2416 r
= link_update_name(link
, message
);
2420 r
= link_update_alternative_names(link
, message
);
2424 r
= link_update_mtu(link
, message
);
2428 r
= link_update_hardware_address(link
, message
);
2432 r
= link_update_master(link
, message
);
2436 return link_update_flags(link
, message
);
2439 static Link
*link_drop_or_unref(Link
*link
) {
2443 return link_unref(link
);
2444 return link_drop(link
);
2447 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_drop_or_unref
);
2449 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
2450 _cleanup_free_
char *ifname
= NULL
, *kind
= NULL
, *state_file
= NULL
, *lease_file
= NULL
, *lldp_file
= NULL
;
2451 _cleanup_(link_drop_or_unrefp
) Link
*link
= NULL
;
2452 unsigned short iftype
;
2459 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
2461 return log_debug_errno(r
, "rtnl: failed to read ifindex from link message: %m");
2462 else if (ifindex
<= 0)
2463 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "rtnl: received link message without valid ifindex.");
2465 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
2467 return log_debug_errno(r
, "rtnl: failed to read interface type from link message: %m");
2469 r
= sd_netlink_message_read_string_strdup(message
, IFLA_IFNAME
, &ifname
);
2471 return log_debug_errno(r
, "rtnl: failed to read interface name from link message: %m");
2473 /* check for link kind */
2474 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
2476 r
= sd_netlink_message_read_string_strdup(message
, IFLA_INFO_KIND
, &kind
);
2477 if (r
< 0 && r
!= -ENODATA
)
2478 return log_debug_errno(r
, "rtnl: failed to read interface kind from link message: %m");
2479 r
= sd_netlink_message_exit_container(message
);
2481 return log_debug_errno(r
, "rtnl: failed to exit IFLA_LINKINFO container: %m");
2484 if (!manager
->test_mode
) {
2485 /* Do not update state files when running in test mode. */
2486 if (asprintf(&state_file
, "/run/systemd/netif/links/%d", ifindex
) < 0)
2487 return log_oom_debug();
2489 if (asprintf(&lease_file
, "/run/systemd/netif/leases/%d", ifindex
) < 0)
2490 return log_oom_debug();
2492 if (asprintf(&lldp_file
, "/run/systemd/netif/lldp/%d", ifindex
) < 0)
2493 return log_oom_debug();
2496 link
= new(Link
, 1);
2502 .state
= LINK_STATE_PENDING
,
2503 .online_state
= _LINK_ONLINE_STATE_INVALID
,
2506 .ifname
= TAKE_PTR(ifname
),
2507 .kind
= TAKE_PTR(kind
),
2509 .state_file
= TAKE_PTR(state_file
),
2510 .lease_file
= TAKE_PTR(lease_file
),
2511 .lldp_file
= TAKE_PTR(lldp_file
),
2514 .dns_default_route
= -1,
2515 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
2516 .mdns
= _RESOLVE_SUPPORT_INVALID
,
2517 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
2518 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
2521 r
= hashmap_ensure_put(&manager
->links_by_index
, NULL
, INT_TO_PTR(link
->ifindex
), link
);
2523 return log_link_debug_errno(link
, r
, "Failed to store link into manager: %m");
2525 link
->manager
= manager
;
2527 r
= hashmap_ensure_put(&manager
->links_by_name
, &string_hash_ops
, link
->ifname
, link
);
2529 return log_link_debug_errno(link
, r
, "Failed to manage link by its interface name: %m");
2531 log_link_debug(link
, "Saved new link: ifindex=%i, iftype=%s(%u), kind=%s",
2532 link
->ifindex
, strna(arphrd_to_name(link
->iftype
)), link
->iftype
, strna(link
->kind
));
2534 r
= netlink_message_read_hw_addr(message
, IFLA_PERM_ADDRESS
, &link
->permanent_hw_addr
);
2537 log_link_debug_errno(link
, r
, "Failed to read IFLA_PERM_ADDRESS attribute, ignoring: %m");
2539 if (netlink_message_read_hw_addr(message
, IFLA_ADDRESS
, NULL
) >= 0) {
2540 /* Fallback to ethtool, if the link has a hardware address. */
2541 r
= ethtool_get_permanent_hw_addr(&manager
->ethtool_fd
, link
->ifname
, &link
->permanent_hw_addr
);
2543 log_link_debug_errno(link
, r
, "Permanent hardware address not found, continuing without: %m");
2546 if (link
->permanent_hw_addr
.length
> 0)
2547 log_link_debug(link
, "Saved permanent hardware address: %s", HW_ADDR_TO_STR(&link
->permanent_hw_addr
));
2549 r
= ethtool_get_driver(&manager
->ethtool_fd
, link
->ifname
, &link
->driver
);
2551 log_link_debug_errno(link
, r
, "Failed to get driver, continuing without: %m");
2553 if (streq_ptr(link
->driver
, "dsa")) {
2554 uint32_t dsa_master_ifindex
;
2556 r
= sd_netlink_message_read_u32(message
, IFLA_LINK
, &dsa_master_ifindex
);
2558 dsa_master_ifindex
= 0;
2560 log_link_warning_errno(link
, r
, "rtnl: failed to read ifindex of the DSA master interface, ignoring: %m");
2561 } else if (dsa_master_ifindex
> INT_MAX
) {
2562 dsa_master_ifindex
= 0;
2563 log_link_warning(link
, "rtnl: received too large DSA master ifindex (%"PRIu32
" > INT_MAX), ignoring.",
2564 dsa_master_ifindex
);
2567 link
->dsa_master_ifindex
= (int) dsa_master_ifindex
;
2570 *ret
= TAKE_PTR(link
);
2574 int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*manager
) {
2576 NetDev
*netdev
= NULL
;
2585 if (sd_netlink_message_is_error(message
)) {
2586 r
= sd_netlink_message_get_errno(message
);
2588 log_message_warning_errno(message
, r
, "rtnl: Could not receive link message, ignoring");
2593 r
= sd_netlink_message_get_type(message
, &type
);
2595 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
2597 } else if (!IN_SET(type
, RTM_NEWLINK
, RTM_DELLINK
)) {
2598 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type
);
2602 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
2604 log_warning_errno(r
, "rtnl: Could not get ifindex from link message, ignoring: %m");
2606 } else if (ifindex
<= 0) {
2607 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex
);
2611 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
2613 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
2617 (void) link_get_by_index(manager
, ifindex
, &link
);
2618 (void) netdev_get(manager
, name
, &netdev
);
2623 /* netdev exists, so make sure the ifindex matches */
2624 r
= netdev_set_ifindex(netdev
, message
);
2626 log_warning_errno(r
, "Could not process new link message for netdev, ignoring: %m");
2632 /* link is new, so add it */
2633 r
= link_new(manager
, message
, &link
);
2635 log_warning_errno(r
, "Could not process new link message: %m");
2639 r
= link_update(link
, message
);
2641 log_warning_errno(r
, "Could not process link message: %m");
2642 link_enter_failed(link
);
2646 r
= link_check_initialized(link
);
2648 log_warning_errno(r
, "Failed to check link is initialized: %m");
2649 link_enter_failed(link
);
2653 r
= link_update(link
, message
);
2655 log_warning_errno(r
, "Could not process link message: %m");
2656 link_enter_failed(link
);
2665 netdev_drop(netdev
);
2670 assert_not_reached();
2676 int link_getlink_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, const char *error_msg
) {
2677 uint16_t message_type
;
2684 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2687 r
= sd_netlink_message_get_errno(m
);
2689 log_link_message_warning_errno(link
, m
, r
, error_msg
);
2690 link_enter_failed(link
);
2694 r
= sd_netlink_message_get_type(m
, &message_type
);
2696 log_link_debug_errno(link
, r
, "rtnl: failed to read link message type, ignoring: %m");
2699 if (message_type
!= RTM_NEWLINK
) {
2700 log_link_debug(link
, "rtnl: received invalid link message type, ignoring.");
2704 r
= link_update(link
, m
);
2706 link_enter_failed(link
);
2713 int link_call_getlink(Link
*link
, link_netlink_message_handler_t callback
) {
2714 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2718 assert(link
->manager
);
2719 assert(link
->manager
->rtnl
);
2722 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
, link
->ifindex
);
2726 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
2727 link_netlink_destroy_callback
, link
);
2735 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
2736 [LINK_STATE_PENDING
] = "pending",
2737 [LINK_STATE_INITIALIZED
] = "initialized",
2738 [LINK_STATE_CONFIGURING
] = "configuring",
2739 [LINK_STATE_CONFIGURED
] = "configured",
2740 [LINK_STATE_UNMANAGED
] = "unmanaged",
2741 [LINK_STATE_FAILED
] = "failed",
2742 [LINK_STATE_LINGER
] = "linger",
2745 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);
2747 int link_flags_to_string_alloc(uint32_t flags
, char **ret
) {
2748 _cleanup_free_
char *str
= NULL
;
2749 static const char* map
[] = {
2750 [LOG2U(IFF_UP
)] = "up", /* interface is up. */
2751 [LOG2U(IFF_BROADCAST
)] = "broadcast", /* broadcast address valid.*/
2752 [LOG2U(IFF_DEBUG
)] = "debug", /* turn on debugging. */
2753 [LOG2U(IFF_LOOPBACK
)] = "loopback", /* interface is a loopback net. */
2754 [LOG2U(IFF_POINTOPOINT
)] = "point-to-point", /* interface has p-p link. */
2755 [LOG2U(IFF_NOTRAILERS
)] = "no-trailers", /* avoid use of trailers. */
2756 [LOG2U(IFF_RUNNING
)] = "running", /* interface RFC2863 OPER_UP. */
2757 [LOG2U(IFF_NOARP
)] = "no-arp", /* no ARP protocol. */
2758 [LOG2U(IFF_PROMISC
)] = "promiscuous", /* receive all packets. */
2759 [LOG2U(IFF_ALLMULTI
)] = "all-multicast", /* receive all multicast packets. */
2760 [LOG2U(IFF_MASTER
)] = "master", /* master of a load balancer. */
2761 [LOG2U(IFF_SLAVE
)] = "slave", /* slave of a load balancer. */
2762 [LOG2U(IFF_MULTICAST
)] = "multicast", /* supports multicast.*/
2763 [LOG2U(IFF_PORTSEL
)] = "portsel", /* can set media type. */
2764 [LOG2U(IFF_AUTOMEDIA
)] = "auto-media", /* auto media select active. */
2765 [LOG2U(IFF_DYNAMIC
)] = "dynamic", /* dialup device with changing addresses. */
2766 [LOG2U(IFF_LOWER_UP
)] = "lower-up", /* driver signals L1 up. */
2767 [LOG2U(IFF_DORMANT
)] = "dormant", /* driver signals dormant. */
2768 [LOG2U(IFF_ECHO
)] = "echo", /* echo sent packets. */
2773 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
2774 if (FLAGS_SET(flags
, 1 << i
) && map
[i
])
2775 if (!strextend_with_separator(&str
, ",", map
[i
]))
2778 *ret
= TAKE_PTR(str
);
2782 static const char * const kernel_operstate_table
[] = {
2783 [IF_OPER_UNKNOWN
] = "unknown",
2784 [IF_OPER_NOTPRESENT
] = "not-present",
2785 [IF_OPER_DOWN
] = "down",
2786 [IF_OPER_LOWERLAYERDOWN
] = "lower-layer-down",
2787 [IF_OPER_TESTING
] = "testing",
2788 [IF_OPER_DORMANT
] = "dormant",
2789 [IF_OPER_UP
] = "up",
2792 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(kernel_operstate
, int);