1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <netinet/in.h>
5 #include <linux/if_arp.h>
6 #include <linux/if_link.h>
7 #include <sys/socket.h>
10 #include "alloc-util.h"
15 #include "device-private.h"
16 #include "device-util.h"
17 #include "dhcp-identifier.h"
18 #include "dhcp-lease-internal.h"
20 #include "ethtool-util.h"
25 #include "missing_network.h"
26 #include "netlink-util.h"
27 #include "network-internal.h"
28 #include "networkd-address-label.h"
29 #include "networkd-address.h"
30 #include "networkd-bridge-fdb.h"
31 #include "networkd-bridge-mdb.h"
32 #include "networkd-can.h"
33 #include "networkd-dhcp-server.h"
34 #include "networkd-dhcp4.h"
35 #include "networkd-dhcp6.h"
36 #include "networkd-ipv4ll.h"
37 #include "networkd-ipv6-proxy-ndp.h"
38 #include "networkd-link-bus.h"
39 #include "networkd-link.h"
40 #include "networkd-lldp-tx.h"
41 #include "networkd-manager.h"
42 #include "networkd-ndisc.h"
43 #include "networkd-neighbor.h"
44 #include "networkd-nexthop.h"
45 #include "networkd-queue.h"
46 #include "networkd-radv.h"
47 #include "networkd-routing-policy-rule.h"
48 #include "networkd-setlink.h"
49 #include "networkd-sriov.h"
50 #include "networkd-state-file.h"
51 #include "networkd-sysctl.h"
52 #include "networkd-wifi.h"
54 #include "socket-util.h"
55 #include "stat-util.h"
56 #include "stdio-util.h"
57 #include "string-table.h"
60 #include "tmpfile-util.h"
61 #include "udev-util.h"
65 bool link_ipv4ll_enabled(Link
*link
) {
68 if (link
->flags
& IFF_LOOPBACK
)
74 if (link
->iftype
== ARPHRD_CAN
)
77 if (STRPTR_IN_SET(link
->kind
,
78 "vrf", "wireguard", "ipip", "gre", "ip6gre","ip6tnl", "sit", "vti",
79 "vti6", "nlmon", "xfrm", "bareudp"))
82 /* L3 or L3S mode do not support ARP. */
83 if (IN_SET(link_get_ipvlan_mode(link
), NETDEV_IPVLAN_MODE_L3
, NETDEV_IPVLAN_MODE_L3S
))
86 if (link
->network
->bond
)
89 return link
->network
->link_local
& ADDRESS_FAMILY_IPV4
;
92 bool link_ipv6ll_enabled(Link
*link
) {
95 if (!socket_ipv6_is_supported())
98 if (link
->flags
& IFF_LOOPBACK
)
104 if (link
->iftype
== ARPHRD_CAN
)
107 if (STRPTR_IN_SET(link
->kind
, "vrf", "wireguard", "ipip", "gre", "sit", "vti", "nlmon"))
110 if (link
->network
->bond
)
113 return link
->network
->link_local
& ADDRESS_FAMILY_IPV6
;
116 bool link_ipv6_enabled(Link
*link
) {
119 if (!socket_ipv6_is_supported())
122 if (link
->network
->bond
)
125 if (link
->iftype
== ARPHRD_CAN
)
128 /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */
129 if (link_ipv6ll_enabled(link
))
132 if (network_has_static_ipv6_configurations(link
->network
))
138 bool link_is_ready_to_configure(Link
*link
, bool allow_unmanaged
) {
141 if (!link
->network
) {
142 if (!allow_unmanaged
)
145 return link_has_carrier(link
);
148 if (!IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
))
151 if (!link
->network
->configure_without_carrier
) {
152 if (link
->set_flags_messages
> 0)
155 if (!link_has_carrier(link
))
159 if (link
->set_link_messages
> 0)
162 if (!link
->stacked_netdevs_created
)
165 if (!link
->activated
)
171 void link_ntp_settings_clear(Link
*link
) {
172 link
->ntp
= strv_free(link
->ntp
);
175 void link_dns_settings_clear(Link
*link
) {
176 if (link
->n_dns
!= UINT_MAX
)
177 for (unsigned i
= 0; i
< link
->n_dns
; i
++)
178 in_addr_full_free(link
->dns
[i
]);
179 link
->dns
= mfree(link
->dns
);
180 link
->n_dns
= UINT_MAX
;
182 link
->search_domains
= ordered_set_free(link
->search_domains
);
183 link
->route_domains
= ordered_set_free(link
->route_domains
);
185 link
->dns_default_route
= -1;
186 link
->llmnr
= _RESOLVE_SUPPORT_INVALID
;
187 link
->mdns
= _RESOLVE_SUPPORT_INVALID
;
188 link
->dnssec_mode
= _DNSSEC_MODE_INVALID
;
189 link
->dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
;
191 link
->dnssec_negative_trust_anchors
= set_free_free(link
->dnssec_negative_trust_anchors
);
194 static void link_free_engines(Link
*link
) {
198 link
->dhcp_server
= sd_dhcp_server_unref(link
->dhcp_server
);
199 link
->dhcp_client
= sd_dhcp_client_unref(link
->dhcp_client
);
200 link
->dhcp_lease
= sd_dhcp_lease_unref(link
->dhcp_lease
);
201 link
->dhcp_acd
= sd_ipv4acd_unref(link
->dhcp_acd
);
203 link
->lldp
= sd_lldp_unref(link
->lldp
);
204 link_lldp_emit_stop(link
);
208 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
);
211 link
->ndisc
= sd_ndisc_unref(link
->ndisc
);
212 link
->radv
= sd_radv_unref(link
->radv
);
214 ipv4_dad_unref(link
);
217 static Link
*link_free(Link
*link
) {
220 link_ntp_settings_clear(link
);
221 link_dns_settings_clear(link
);
223 link
->routes
= set_free(link
->routes
);
224 link
->routes_foreign
= set_free(link
->routes_foreign
);
225 link
->dhcp_routes
= set_free(link
->dhcp_routes
);
226 link
->dhcp_routes_old
= set_free(link
->dhcp_routes_old
);
227 link
->dhcp6_routes
= set_free(link
->dhcp6_routes
);
228 link
->dhcp6_routes_old
= set_free(link
->dhcp6_routes_old
);
229 link
->dhcp6_pd_routes
= set_free(link
->dhcp6_pd_routes
);
230 link
->dhcp6_pd_routes_old
= set_free(link
->dhcp6_pd_routes_old
);
231 link
->ndisc_routes
= set_free(link
->ndisc_routes
);
233 link
->nexthops
= set_free(link
->nexthops
);
234 link
->nexthops_foreign
= set_free(link
->nexthops_foreign
);
236 link
->neighbors
= set_free(link
->neighbors
);
237 link
->neighbors_foreign
= set_free(link
->neighbors_foreign
);
239 link
->addresses
= set_free(link
->addresses
);
240 link
->addresses_foreign
= set_free(link
->addresses_foreign
);
241 link
->pool_addresses
= set_free(link
->pool_addresses
);
242 link
->static_addresses
= set_free(link
->static_addresses
);
243 link
->dhcp6_addresses
= set_free(link
->dhcp6_addresses
);
244 link
->dhcp6_addresses_old
= set_free(link
->dhcp6_addresses_old
);
245 link
->dhcp6_pd_addresses
= set_free(link
->dhcp6_pd_addresses
);
246 link
->dhcp6_pd_addresses_old
= set_free(link
->dhcp6_pd_addresses_old
);
247 link
->ndisc_addresses
= set_free(link
->ndisc_addresses
);
249 link
->dhcp6_pd_prefixes
= set_free(link
->dhcp6_pd_prefixes
);
251 link_free_engines(link
);
254 strv_free(link
->alternative_names
);
259 unlink_and_free(link
->lease_file
);
260 unlink_and_free(link
->lldp_file
);
261 unlink_and_free(link
->state_file
);
263 sd_device_unref(link
->sd_device
);
265 hashmap_free(link
->bound_to_links
);
266 hashmap_free(link
->bound_by_links
);
268 set_free_with_destructor(link
->slaves
, link_unref
);
270 network_unref(link
->network
);
275 DEFINE_TRIVIAL_REF_UNREF_FUNC(Link
, link
, link_free
);
277 int link_get(Manager
*m
, int ifindex
, Link
**ret
) {
283 link
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
292 int link_get_by_name(Manager
*m
, const char *ifname
, Link
**ret
) {
298 link
= hashmap_get(m
->links_by_name
, ifname
);
307 int link_get_master(Link
*link
, Link
**ret
) {
309 assert(link
->manager
);
312 if (link
->master_ifindex
<= 0 || link
->master_ifindex
== link
->ifindex
)
315 return link_get(link
->manager
, link
->master_ifindex
, ret
);
318 void link_set_state(Link
*link
, LinkState state
) {
321 if (link
->state
== state
)
324 log_link_debug(link
, "State changed: %s -> %s",
325 link_state_to_string(link
->state
),
326 link_state_to_string(state
));
330 link_send_changed(link
, "AdministrativeState", NULL
);
334 int link_stop_engines(Link
*link
, bool may_keep_dhcp
) {
338 assert(link
->manager
);
339 assert(link
->manager
->event
);
341 bool keep_dhcp
= may_keep_dhcp
&&
343 (link
->manager
->restarting
||
344 FLAGS_SET(link
->network
->keep_configuration
, KEEP_CONFIGURATION_DHCP_ON_STOP
));
347 k
= sd_dhcp_client_stop(link
->dhcp_client
);
349 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 client: %m");
352 k
= sd_ipv4acd_stop(link
->dhcp_acd
);
354 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client for DHCPv4: %m");
356 k
= sd_dhcp_server_stop(link
->dhcp_server
);
358 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv4 server: %m");
360 k
= sd_lldp_stop(link
->lldp
);
362 r
= log_link_warning_errno(link
, k
, "Could not stop LLDP: %m");
364 k
= sd_ipv4ll_stop(link
->ipv4ll
);
366 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 link-local: %m");
368 k
= ipv4_dad_stop(link
);
370 r
= log_link_warning_errno(link
, k
, "Could not stop IPv4 ACD client: %m");
372 k
= sd_dhcp6_client_stop(link
->dhcp6_client
);
374 r
= log_link_warning_errno(link
, k
, "Could not stop DHCPv6 client: %m");
376 k
= dhcp6_pd_remove(link
);
378 r
= log_link_warning_errno(link
, k
, "Could not remove DHCPv6 PD addresses and routes: %m");
380 k
= sd_ndisc_stop(link
->ndisc
);
382 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Discovery: %m");
384 k
= sd_radv_stop(link
->radv
);
386 r
= log_link_warning_errno(link
, k
, "Could not stop IPv6 Router Advertisement: %m");
388 link_lldp_emit_stop(link
);
392 void link_enter_failed(Link
*link
) {
395 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
398 log_link_warning(link
, "Failed");
400 link_set_state(link
, LINK_STATE_FAILED
);
402 (void) link_stop_engines(link
, false);
405 void link_check_ready(Link
*link
) {
410 if (link
->state
== LINK_STATE_CONFIGURED
)
413 if (link
->state
!= LINK_STATE_CONFIGURING
)
414 return (void) log_link_debug(link
, "%s(): link is in %s state.", __func__
, link_state_to_string(link
->state
));
417 return (void) log_link_debug(link
, "%s(): link is unmanaged.", __func__
);
419 if (!link
->tc_configured
)
420 return (void) log_link_debug(link
, "%s(): traffic controls are not configured.", __func__
);
422 if (link
->set_link_messages
> 0)
423 return (void) log_link_debug(link
, "%s(): link layer is configuring.", __func__
);
425 if (!link
->activated
)
426 return (void) log_link_debug(link
, "%s(): link is not activated.", __func__
);
428 if (link
->iftype
== ARPHRD_CAN
) {
429 /* let's shortcut things for CAN which doesn't need most of checks below. */
430 link_set_state(link
, LINK_STATE_CONFIGURED
);
434 if (!link
->static_addresses_configured
)
435 return (void) log_link_debug(link
, "%s(): static addresses are not configured.", __func__
);
437 SET_FOREACH(a
, link
->addresses
)
438 if (!address_is_ready(a
)) {
439 _cleanup_free_
char *str
= NULL
;
441 (void) in_addr_prefix_to_string(a
->family
, &a
->in_addr
, a
->prefixlen
, &str
);
442 return (void) log_link_debug(link
, "%s(): an address %s is not ready.", __func__
, strna(str
));
445 if (!link
->static_address_labels_configured
)
446 return (void) log_link_debug(link
, "%s(): static address labels are not configured.", __func__
);
448 if (!link
->static_bridge_fdb_configured
)
449 return (void) log_link_debug(link
, "%s(): static bridge MDB entries are not configured.", __func__
);
451 if (!link
->static_bridge_mdb_configured
)
452 return (void) log_link_debug(link
, "%s(): static bridge MDB entries are not configured.", __func__
);
454 if (!link
->static_ipv6_proxy_ndp_configured
)
455 return (void) log_link_debug(link
, "%s(): static IPv6 proxy NDP addresses are not configured.", __func__
);
457 if (!link
->static_neighbors_configured
)
458 return (void) log_link_debug(link
, "%s(): static neighbors are not configured.", __func__
);
460 if (!link
->static_nexthops_configured
)
461 return (void) log_link_debug(link
, "%s(): static nexthops are not configured.", __func__
);
463 if (!link
->static_routes_configured
)
464 return (void) log_link_debug(link
, "%s(): static routes are not configured.", __func__
);
466 if (!link
->static_routing_policy_rules_configured
)
467 return (void) log_link_debug(link
, "%s(): static routing policy rules are not configured.", __func__
);
469 if (!link
->sr_iov_configured
)
470 return (void) log_link_debug(link
, "%s(): SR-IOV is not configured.", __func__
);
472 /* IPv6LL is assigned after the link gains its carrier. */
473 if (!link
->network
->configure_without_carrier
&&
474 link_ipv6ll_enabled(link
) &&
475 !in6_addr_is_set(&link
->ipv6ll_address
))
476 return (void) log_link_debug(link
, "%s(): IPv6LL is not configured yet.", __func__
);
478 bool has_ndisc_address
= false;
480 SET_FOREACH(n
, link
->ndisc_addresses
)
482 has_ndisc_address
= true;
486 if ((link_dhcp4_enabled(link
) || link_dhcp6_with_address_enabled(link
) || link_ipv4ll_enabled(link
)) &&
487 !link
->dhcp_address
&& set_isempty(link
->dhcp6_addresses
) && !has_ndisc_address
&&
488 !link
->ipv4ll_address_configured
)
489 /* When DHCP[46] or IPv4LL is enabled, at least one address is acquired by them. */
490 return (void) log_link_debug(link
, "%s(): DHCP4, DHCP6 or IPv4LL is enabled but no dynamic address is assigned yet.", __func__
);
492 /* Ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
493 if (link_dhcp4_enabled(link
) || link_dhcp6_enabled(link
) || link_dhcp6_pd_is_enabled(link
) ||
494 (!link
->network
->configure_without_carrier
&& link_ipv6_accept_ra_enabled(link
)) ||
495 link_ipv4ll_enabled(link
)) {
497 if (!link
->dhcp4_configured
&&
498 !(link
->dhcp6_address_configured
&& link
->dhcp6_route_configured
) &&
499 !(link
->dhcp6_pd_address_configured
&& link
->dhcp6_pd_route_configured
) &&
500 !(link
->ndisc_addresses_configured
&& link
->ndisc_routes_configured
) &&
501 !link
->ipv4ll_address_configured
)
502 /* When DHCP[46], NDisc, or IPv4LL is enabled, at least one protocol must be finished. */
503 return (void) log_link_debug(link
, "%s(): dynamic addresses or routes are not configured.", __func__
);
505 log_link_debug(link
, "%s(): dhcp4:%s ipv4ll:%s dhcp6_addresses:%s dhcp_routes:%s dhcp_pd_addresses:%s dhcp_pd_routes:%s ndisc_addresses:%s ndisc_routes:%s",
507 yes_no(link
->dhcp4_configured
),
508 yes_no(link
->ipv4ll_address_configured
),
509 yes_no(link
->dhcp6_address_configured
),
510 yes_no(link
->dhcp6_route_configured
),
511 yes_no(link
->dhcp6_pd_address_configured
),
512 yes_no(link
->dhcp6_pd_route_configured
),
513 yes_no(link
->ndisc_addresses_configured
),
514 yes_no(link
->ndisc_routes_configured
));
517 link_set_state(link
, LINK_STATE_CONFIGURED
);
520 static int link_request_static_configs(Link
*link
) {
524 assert(link
->network
);
525 assert(link
->state
!= _LINK_STATE_INVALID
);
527 r
= link_request_static_addresses(link
);
531 r
= link_request_static_address_labels(link
);
535 r
= link_request_static_bridge_fdb(link
);
539 r
= link_request_static_bridge_mdb(link
);
543 r
= link_request_static_ipv6_proxy_ndp_addresses(link
);
547 r
= link_request_static_neighbors(link
);
551 r
= link_request_static_nexthops(link
, false);
555 r
= link_request_static_routes(link
, false);
559 r
= link_request_static_routing_policy_rules(link
);
566 static int link_request_stacked_netdevs(Link
*link
) {
572 link
->stacked_netdevs_created
= false;
573 link
->stacked_netdevs_after_configured_created
= false;
575 HASHMAP_FOREACH(netdev
, link
->network
->stacked_netdevs
) {
576 r
= link_request_to_crate_stacked_netdev(link
, netdev
);
581 if (link
->create_stacked_netdev_messages
== 0)
582 link
->stacked_netdevs_created
= true;
583 if (link
->create_stacked_netdev_after_configured_messages
== 0)
584 link
->stacked_netdevs_after_configured_created
= true;
589 static int link_acquire_dynamic_ipv6_conf(Link
*link
) {
596 assert(in6_addr_is_link_local(&link
->ipv6ll_address
));
598 log_link_debug(link
, "Starting IPv6 Router Advertisements");
600 r
= radv_emit_dns(link
);
602 return log_link_warning_errno(link
, r
, "Failed to configure DNS or Domains in IPv6 Router Advertisement: %m");
604 r
= sd_radv_start(link
->radv
);
605 if (r
< 0 && r
!= -EBUSY
)
606 return log_link_warning_errno(link
, r
, "Could not start IPv6 Router Advertisement: %m");
609 r
= ndisc_start(link
);
611 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Discovery: %m");
613 r
= dhcp6_start(link
);
615 return log_link_warning_errno(link
, r
, "Failed to start DHCPv6 client: %m");
617 r
= dhcp6_request_prefix_delegation(link
);
619 return log_link_warning_errno(link
, r
, "Failed to request DHCPv6 prefix delegation: %m");
624 static int link_acquire_dynamic_ipv4_conf(Link
*link
) {
628 assert(link
->manager
);
629 assert(link
->manager
->event
);
631 if (link
->dhcp_client
) {
632 r
= dhcp4_start(link
);
634 return log_link_warning_errno(link
, r
, "Failed to start DHCPv4 client: %m");
636 } else if (link
->ipv4ll
) {
637 log_link_debug(link
, "Acquiring IPv4 link-local address");
639 r
= sd_ipv4ll_start(link
->ipv4ll
);
641 return log_link_warning_errno(link
, r
, "Could not acquire IPv4 link-local address: %m");
644 if (link
->dhcp_server
) {
645 r
= sd_dhcp_server_start(link
->dhcp_server
);
647 return log_link_warning_errno(link
, r
, "Could not start DHCP server: %m");
653 static int link_acquire_dynamic_conf(Link
*link
) {
658 r
= link_acquire_dynamic_ipv4_conf(link
);
662 if (in6_addr_is_set(&link
->ipv6ll_address
)) {
663 r
= link_acquire_dynamic_ipv6_conf(link
);
668 r
= link_lldp_emit_start(link
);
670 return log_link_warning_errno(link
, r
, "Failed to start LLDP transmission: %m");
675 int link_ipv6ll_gained(Link
*link
, const struct in6_addr
*address
) {
680 log_link_info(link
, "Gained IPv6LL");
682 link
->ipv6ll_address
= *address
;
683 link_check_ready(link
);
685 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
686 r
= link_acquire_dynamic_ipv6_conf(link
);
688 link_enter_failed(link
);
696 int link_handle_bound_to_list(Link
*link
) {
697 bool required_up
= false;
698 bool link_is_up
= false;
703 /* If at least one interface in bound_to_links has carrier, then make this interface up.
704 * If all interfaces in bound_to_links do not, then make this interface down. */
706 if (hashmap_isempty(link
->bound_to_links
))
709 if (link
->flags
& IFF_UP
)
712 HASHMAP_FOREACH(l
, link
->bound_to_links
)
713 if (link_has_carrier(l
)) {
718 if (!required_up
&& link_is_up
)
719 return link_request_to_bring_up_or_down(link
, /* up = */ false);
720 if (required_up
&& !link_is_up
)
721 return link_request_to_bring_up_or_down(link
, /* up = */ true);
726 static int link_handle_bound_by_list(Link
*link
) {
732 /* Update up or down state of interfaces which depend on this interface's carrier state. */
734 if (hashmap_isempty(link
->bound_by_links
))
737 HASHMAP_FOREACH(l
, link
->bound_by_links
) {
738 r
= link_handle_bound_to_list(l
);
746 static int link_put_carrier(Link
*link
, Link
*carrier
, Hashmap
**h
) {
755 if (hashmap_get(*h
, INT_TO_PTR(carrier
->ifindex
)))
758 r
= hashmap_ensure_put(h
, NULL
, INT_TO_PTR(carrier
->ifindex
), carrier
);
767 static int link_new_bound_by_list(Link
*link
) {
773 assert(link
->manager
);
777 HASHMAP_FOREACH(carrier
, m
->links
) {
778 if (!carrier
->network
)
781 if (strv_isempty(carrier
->network
->bind_carrier
))
784 if (strv_fnmatch(carrier
->network
->bind_carrier
, link
->ifname
)) {
785 r
= link_put_carrier(link
, carrier
, &link
->bound_by_links
);
791 HASHMAP_FOREACH(carrier
, link
->bound_by_links
) {
792 r
= link_put_carrier(carrier
, link
, &carrier
->bound_to_links
);
800 static int link_new_bound_to_list(Link
*link
) {
806 assert(link
->manager
);
811 if (strv_isempty(link
->network
->bind_carrier
))
816 HASHMAP_FOREACH(carrier
, m
->links
) {
817 if (strv_fnmatch(link
->network
->bind_carrier
, carrier
->ifname
)) {
818 r
= link_put_carrier(link
, carrier
, &link
->bound_to_links
);
824 HASHMAP_FOREACH(carrier
, link
->bound_to_links
) {
825 r
= link_put_carrier(carrier
, link
, &carrier
->bound_by_links
);
833 static int link_new_carrier_maps(Link
*link
) {
836 r
= link_new_bound_by_list(link
);
840 r
= link_handle_bound_by_list(link
);
844 r
= link_new_bound_to_list(link
);
848 return link_handle_bound_to_list(link
);
851 static void link_free_bound_to_list(Link
*link
) {
852 bool updated
= false;
857 while ((bound_to
= hashmap_steal_first(link
->bound_to_links
))) {
860 if (hashmap_remove(bound_to
->bound_by_links
, INT_TO_PTR(link
->ifindex
)))
861 link_dirty(bound_to
);
868 static void link_free_bound_by_list(Link
*link
) {
869 bool updated
= false;
874 while ((bound_by
= hashmap_steal_first(link
->bound_by_links
))) {
877 if (hashmap_remove(bound_by
->bound_to_links
, INT_TO_PTR(link
->ifindex
))) {
878 link_dirty(bound_by
);
879 link_handle_bound_to_list(bound_by
);
887 static void link_free_carrier_maps(Link
*link
) {
890 link_free_bound_to_list(link
);
891 link_free_bound_by_list(link
);
894 static int link_append_to_master(Link
*link
) {
900 /* - The link may have no master.
901 * - RTM_NEWLINK message about master interface may not be received yet. */
902 if (link_get_master(link
, &master
) < 0)
905 r
= set_ensure_put(&master
->slaves
, NULL
, link
);
913 static void link_drop_from_master(Link
*link
) {
921 if (link_get_master(link
, &master
) < 0)
924 link_unref(set_remove(master
->slaves
, link
));
927 static void link_drop_requests(Link
*link
) {
931 assert(link
->manager
);
933 ORDERED_SET_FOREACH(req
, link
->manager
->request_queue
)
934 if (req
->link
== link
)
938 static Link
*link_drop(Link
*link
) {
944 assert(link
->manager
);
946 link_set_state(link
, LINK_STATE_LINGER
);
948 /* Drop all references from other links and manager. Note that async netlink calls may have
949 * references to the link, and they will be dropped when we receive replies. */
951 link_drop_requests(link
);
953 link_free_carrier_maps(link
);
955 link_drop_from_master(link
);
957 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
959 (void) unlink(link
->state_file
);
962 STRV_FOREACH(n
, link
->alternative_names
)
963 hashmap_remove(link
->manager
->links_by_name
, *n
);
965 hashmap_remove(link
->manager
->links_by_name
, link
->ifname
);
967 /* The following must be called at last. */
968 assert_se(hashmap_remove(link
->manager
->links
, INT_TO_PTR(link
->ifindex
)) == link
);
969 return link_unref(link
);
972 static int link_drop_foreign_config(Link
*link
) {
976 assert(link
->manager
);
978 r
= link_drop_foreign_routes(link
);
980 k
= link_drop_foreign_nexthops(link
);
984 k
= link_drop_foreign_addresses(link
);
988 k
= link_drop_foreign_neighbors(link
);
992 k
= manager_drop_foreign_routing_policy_rules(link
->manager
);
999 static int link_drop_config(Link
*link
) {
1003 assert(link
->manager
);
1005 r
= link_drop_routes(link
);
1007 k
= link_drop_nexthops(link
);
1008 if (k
< 0 && r
>= 0)
1011 k
= link_drop_addresses(link
);
1012 if (k
< 0 && r
>= 0)
1015 k
= link_drop_neighbors(link
);
1016 if (k
< 0 && r
>= 0)
1019 k
= manager_drop_routing_policy_rules(link
->manager
, link
);
1020 if (k
< 0 && r
>= 0)
1028 static int link_configure(Link
*link
) {
1032 assert(link
->network
);
1033 assert(link
->state
== LINK_STATE_INITIALIZED
);
1035 link_set_state(link
, LINK_STATE_CONFIGURING
);
1037 r
= link_configure_traffic_control(link
);
1041 if (link
->iftype
== ARPHRD_CAN
) {
1042 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1043 r
= link_request_to_set_can(link
);
1047 return link_request_to_activate(link
);
1050 r
= link_configure_sr_iov(link
);
1054 r
= link_set_sysctl(link
);
1058 r
= link_request_to_set_mac(link
, /* allow_retry = */ true);
1062 r
= link_request_to_set_flags(link
);
1066 r
= link_request_to_set_group(link
);
1070 r
= link_configure_mtu(link
);
1074 r
= link_request_to_set_addrgen_mode(link
);
1078 r
= link_request_to_set_master(link
);
1082 r
= link_request_stacked_netdevs(link
);
1086 r
= link_request_to_set_bond(link
);
1090 r
= link_request_to_set_bridge(link
);
1094 r
= link_request_to_set_bridge_vlan(link
);
1098 r
= link_request_to_activate(link
);
1102 r
= ipv4ll_configure(link
);
1106 r
= dhcp4_configure(link
);
1110 r
= dhcp6_configure(link
);
1114 r
= ndisc_configure(link
);
1118 r
= link_request_dhcp_server(link
);
1122 r
= radv_configure(link
);
1126 r
= link_lldp_rx_configure(link
);
1130 /* Drop foreign config, but ignore loopback or critical devices.
1131 * We do not want to remove loopback address or addresses used for root NFS. */
1132 if (!(link
->flags
& IFF_LOOPBACK
) &&
1133 link
->network
->keep_configuration
!= KEEP_CONFIGURATION_YES
) {
1134 r
= link_drop_foreign_config(link
);
1139 r
= link_request_static_configs(link
);
1143 if (!link_has_carrier(link
))
1146 return link_acquire_dynamic_conf(link
);
1149 static int link_get_network(Link
*link
, Network
**ret
) {
1154 assert(link
->manager
);
1157 ORDERED_HASHMAP_FOREACH(network
, link
->manager
->networks
) {
1160 r
= net_match_config(
1163 &link
->hw_addr
.ether
,
1164 &link
->permanent_mac
,
1168 link
->alternative_names
,
1177 if (network
->match
.ifname
&& link
->sd_device
) {
1178 uint8_t name_assign_type
= NET_NAME_UNKNOWN
;
1181 if (sd_device_get_sysattr_value(link
->sd_device
, "name_assign_type", &attr
) >= 0)
1182 (void) safe_atou8(attr
, &name_assign_type
);
1184 warn
= name_assign_type
== NET_NAME_ENUM
;
1187 log_link_full(link
, warn
? LOG_WARNING
: LOG_DEBUG
,
1188 "found matching network '%s'%s.",
1190 warn
? ", based on potentially unpredictable interface name" : "");
1192 if (network
->unmanaged
)
1202 static int link_reconfigure_impl(Link
*link
, bool force
) {
1208 r
= link_get_network(link
, &network
);
1210 link_set_state(link
, LINK_STATE_UNMANAGED
);
1216 if (link
->network
== network
&& !force
)
1219 log_link_info(link
, "Re-configuring with %s", network
->filename
);
1221 /* Dropping old .network file */
1222 r
= link_stop_engines(link
, false);
1226 link_drop_requests(link
);
1228 r
= link_drop_config(link
);
1232 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
1233 log_link_debug(link
, "State is %s, dropping foreign config", link_state_to_string(link
->state
));
1234 r
= link_drop_foreign_config(link
);
1239 link_free_carrier_maps(link
);
1240 link_free_engines(link
);
1241 link
->network
= network_unref(link
->network
);
1242 link_unref(set_remove(link
->manager
->links_requesting_uuid
, link
));
1244 /* Then, apply new .network file */
1245 link
->network
= network_ref(network
);
1246 link_update_operstate(link
, true);
1249 r
= link_new_carrier_maps(link
);
1253 link_set_state(link
, LINK_STATE_INITIALIZED
);
1254 link
->activated
= false;
1256 r
= link_configure(link
);
1263 static int link_reconfigure_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, bool force
) {
1266 r
= link_getlink_handler_internal(rtnl
, m
, link
, "Failed to update link state");
1270 r
= link_reconfigure_impl(link
, force
);
1272 link_enter_failed(link
);
1277 static int link_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1278 return link_reconfigure_handler_internal(rtnl
, m
, link
, false);
1281 static int link_force_reconfigure_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1282 return link_reconfigure_handler_internal(rtnl
, m
, link
, true);
1285 int link_reconfigure(Link
*link
, bool force
) {
1288 /* When link in pending or initialized state, then link_configure() will be called. To prevent
1289 * the function from being called multiple times simultaneously, refuse to reconfigure the
1290 * interface in these cases. */
1291 if (IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
, LINK_STATE_LINGER
))
1292 return 0; /* 0 means no-op. */
1294 r
= link_call_getlink(link
, force
? link_force_reconfigure_handler
: link_reconfigure_handler
);
1298 return 1; /* 1 means the interface will be reconfigured. */
1301 static int link_initialized_and_synced(Link
*link
) {
1306 assert(link
->ifname
);
1307 assert(link
->manager
);
1309 /* We may get called either from the asynchronous netlink callback,
1310 * or directly from link_check_initialized() if running in a container. */
1311 if (!IN_SET(link
->state
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
))
1314 log_link_debug(link
, "Link state is up-to-date");
1315 link_set_state(link
, LINK_STATE_INITIALIZED
);
1317 r
= link_new_bound_by_list(link
);
1321 r
= link_handle_bound_by_list(link
);
1325 if (!link
->network
) {
1326 r
= wifi_get_info(link
);
1330 r
= link_get_network(link
, &network
);
1332 link_set_state(link
, LINK_STATE_UNMANAGED
);
1338 if (link
->flags
& IFF_LOOPBACK
) {
1339 if (network
->link_local
!= ADDRESS_FAMILY_NO
)
1340 log_link_debug(link
, "Ignoring link-local autoconfiguration for loopback link");
1342 if (network
->dhcp
!= ADDRESS_FAMILY_NO
)
1343 log_link_debug(link
, "Ignoring DHCP clients for loopback link");
1345 if (network
->dhcp_server
)
1346 log_link_debug(link
, "Ignoring DHCP server for loopback link");
1349 link
->network
= network_ref(network
);
1350 link_update_operstate(link
, false);
1354 r
= link_new_bound_to_list(link
);
1358 return link_configure(link
);
1361 static int link_initialized_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
1364 r
= link_getlink_handler_internal(rtnl
, m
, link
, "Failed to wait for the interface to be initialized");
1368 r
= link_initialized_and_synced(link
);
1370 link_enter_failed(link
);
1375 static int link_initialized(Link
*link
, sd_device
*device
) {
1379 if (link
->state
!= LINK_STATE_PENDING
)
1382 if (link
->sd_device
)
1385 log_link_debug(link
, "udev initialized link");
1386 link_set_state(link
, LINK_STATE_INITIALIZED
);
1388 link
->sd_device
= sd_device_ref(device
);
1390 /* udev has initialized the link, but we don't know if we have yet
1391 * processed the NEWLINK messages with the latest state. Do a GETLINK,
1392 * when it returns we know that the pending NEWLINKs have already been
1393 * processed and that we are up-to-date */
1395 return link_call_getlink(link
, link_initialized_handler
);
1398 static int link_check_initialized(Link
*link
) {
1399 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
1404 if (path_is_read_only_fs("/sys") > 0)
1406 return link_initialized_and_synced(link
);
1408 /* udev should be around */
1409 r
= sd_device_new_from_ifindex(&device
, link
->ifindex
);
1411 log_link_debug_errno(link
, r
, "Could not find device, waiting for device initialization: %m");
1415 r
= sd_device_get_is_initialized(device
);
1417 return log_link_warning_errno(link
, r
, "Could not determine whether the device is initialized: %m");
1420 log_link_debug(link
, "link pending udev initialization...");
1424 r
= device_is_renaming(device
);
1426 return log_link_warning_errno(link
, r
, "Failed to determine the device is being renamed: %m");
1428 log_link_debug(link
, "Interface is being renamed, pending initialization.");
1432 return link_initialized(link
, device
);
1435 int manager_udev_process_link(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
1436 sd_device_action_t action
;
1437 Manager
*m
= userdata
;
1444 r
= sd_device_get_action(device
, &action
);
1446 log_device_debug_errno(device
, r
, "Failed to get udev action, ignoring device: %m");
1450 /* Ignore the "remove" uevent — let's remove a device only if rtnetlink says so. All other uevents
1451 * are "positive" events in some form, i.e. inform us about a changed or new network interface, that
1452 * still exists — and we are interested in that. */
1453 if (action
== SD_DEVICE_REMOVE
)
1456 r
= sd_device_get_ifindex(device
, &ifindex
);
1458 log_device_debug_errno(device
, r
, "Ignoring udev %s event for device without ifindex or with invalid ifindex: %m",
1459 device_action_to_string(action
));
1463 r
= device_is_renaming(device
);
1465 log_device_debug_errno(device
, r
, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
1466 device_action_to_string(action
));
1470 log_device_debug(device
, "Interface is under renaming, wait for the interface to be renamed.");
1474 r
= link_get(m
, ifindex
, &link
);
1476 log_device_debug_errno(device
, r
, "Failed to get link from ifindex %i, ignoring: %m", ifindex
);
1480 r
= link_initialized(link
, device
);
1482 link_enter_failed(link
);
1487 static int link_carrier_gained(Link
*link
) {
1492 r
= link_handle_bound_by_list(link
);
1496 if (link
->iftype
== ARPHRD_CAN
)
1497 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1500 if (IN_SET(link
->state
, LINK_STATE_CONFIGURING
, LINK_STATE_CONFIGURED
)) {
1501 r
= link_acquire_dynamic_conf(link
);
1505 r
= link_request_static_configs(link
);
1513 static int link_carrier_lost(Link
*link
) {
1518 r
= link_handle_bound_by_list(link
);
1522 if (link
->iftype
== ARPHRD_CAN
)
1523 /* let's shortcut things for CAN which doesn't need most of what's done below. */
1526 if (link
->network
&& link
->network
->ignore_carrier_loss
)
1529 r
= link_stop_engines(link
, false);
1531 link_enter_failed(link
);
1535 r
= link_drop_config(link
);
1539 if (!IN_SET(link
->state
, LINK_STATE_UNMANAGED
, LINK_STATE_PENDING
, LINK_STATE_INITIALIZED
)) {
1540 log_link_debug(link
, "State is %s, dropping foreign config", link_state_to_string(link
->state
));
1541 r
= link_drop_foreign_config(link
);
1549 int link_carrier_reset(Link
*link
) {
1554 if (!link_has_carrier(link
))
1557 r
= link_carrier_lost(link
);
1561 r
= link_carrier_gained(link
);
1565 log_link_info(link
, "Reset carrier");
1569 static int link_admin_state_up(Link
*link
) {
1574 /* This is called every time an interface admin state changes to up;
1575 * specifically, when IFF_UP flag changes from unset to set. */
1580 if (link
->activated
&& link
->network
->activation_policy
== ACTIVATION_POLICY_ALWAYS_DOWN
) {
1581 log_link_info(link
, "ActivationPolicy is \"always-off\", forcing link down.");
1582 return link_request_to_bring_up_or_down(link
, /* up = */ false);
1585 /* We set the ipv6 mtu after the device mtu, but the kernel resets
1586 * ipv6 mtu on NETDEV_UP, so we need to reset it. */
1587 r
= link_set_ipv6_mtu(link
);
1589 log_link_warning_errno(link
, r
, "Cannot set IPv6 MTU, ignoring: %m");
1594 static int link_admin_state_down(Link
*link
) {
1600 if (link
->activated
&& link
->network
->activation_policy
== ACTIVATION_POLICY_ALWAYS_UP
) {
1601 log_link_info(link
, "ActivationPolicy is \"always-on\", forcing link up.");
1602 return link_request_to_bring_up_or_down(link
, /* up = */ true);
1608 bool link_has_carrier(Link
*link
) {
1609 /* see Documentation/networking/operstates.txt in the kernel sources */
1611 if (link
->kernel_operstate
== IF_OPER_UP
)
1614 if (link
->kernel_operstate
== IF_OPER_UNKNOWN
)
1615 /* operstate may not be implemented, so fall back to flags */
1616 if (FLAGS_SET(link
->flags
, IFF_LOWER_UP
| IFF_RUNNING
) &&
1617 !FLAGS_SET(link
->flags
, IFF_DORMANT
))
1623 static bool link_is_enslaved(Link
*link
) {
1624 if (link
->flags
& IFF_SLAVE
)
1625 /* Even if the link is not managed by networkd, honor IFF_SLAVE flag. */
1631 if (link
->master_ifindex
> 0 && link
->network
->bridge
)
1634 /* TODO: add conditions for other netdevs. */
1639 static LinkAddressState
address_state_from_scope(uint8_t scope
) {
1640 if (scope
< RT_SCOPE_SITE
)
1641 /* universally accessible addresses found */
1642 return LINK_ADDRESS_STATE_ROUTABLE
;
1644 if (scope
< RT_SCOPE_HOST
)
1645 /* only link or site local addresses found */
1646 return LINK_ADDRESS_STATE_DEGRADED
;
1648 /* no useful addresses found */
1649 return LINK_ADDRESS_STATE_OFF
;
1652 void link_update_operstate(Link
*link
, bool also_update_master
) {
1653 LinkOperationalState operstate
;
1654 LinkCarrierState carrier_state
;
1655 LinkAddressState ipv4_address_state
, ipv6_address_state
, address_state
;
1656 LinkOnlineState online_state
;
1657 _cleanup_strv_free_
char **p
= NULL
;
1658 uint8_t ipv4_scope
= RT_SCOPE_NOWHERE
, ipv6_scope
= RT_SCOPE_NOWHERE
;
1659 bool changed
= false;
1664 if (link
->kernel_operstate
== IF_OPER_DORMANT
)
1665 carrier_state
= LINK_CARRIER_STATE_DORMANT
;
1666 else if (link_has_carrier(link
)) {
1667 if (link_is_enslaved(link
))
1668 carrier_state
= LINK_CARRIER_STATE_ENSLAVED
;
1670 carrier_state
= LINK_CARRIER_STATE_CARRIER
;
1671 } else if (link
->flags
& IFF_UP
)
1672 carrier_state
= LINK_CARRIER_STATE_NO_CARRIER
;
1674 carrier_state
= LINK_CARRIER_STATE_OFF
;
1676 if (carrier_state
>= LINK_CARRIER_STATE_CARRIER
) {
1679 SET_FOREACH(slave
, link
->slaves
) {
1680 link_update_operstate(slave
, false);
1682 if (slave
->carrier_state
< LINK_CARRIER_STATE_CARRIER
)
1683 carrier_state
= LINK_CARRIER_STATE_DEGRADED_CARRIER
;
1687 SET_FOREACH(address
, link
->addresses
) {
1688 if (!address_is_ready(address
))
1691 if (address
->family
== AF_INET
)
1692 ipv4_scope
= MIN(ipv4_scope
, address
->scope
);
1694 if (address
->family
== AF_INET6
)
1695 ipv6_scope
= MIN(ipv6_scope
, address
->scope
);
1698 /* for operstate we also take foreign addresses into account */
1699 SET_FOREACH(address
, link
->addresses_foreign
) {
1700 if (!address_is_ready(address
))
1703 if (address
->family
== AF_INET
)
1704 ipv4_scope
= MIN(ipv4_scope
, address
->scope
);
1706 if (address
->family
== AF_INET6
)
1707 ipv6_scope
= MIN(ipv6_scope
, address
->scope
);
1710 ipv4_address_state
= address_state_from_scope(ipv4_scope
);
1711 ipv6_address_state
= address_state_from_scope(ipv6_scope
);
1712 address_state
= address_state_from_scope(MIN(ipv4_scope
, ipv6_scope
));
1714 /* Mapping of address and carrier state vs operational state
1716 * | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1717 * ------------------------------------------------------------------------------
1718 * off | off | no-carrier | dormant | degraded-carrier | carrier | enslaved
1719 * address_state degraded | off | no-carrier | dormant | degraded-carrier | degraded | enslaved
1720 * routable | off | no-carrier | dormant | degraded-carrier | routable | routable
1723 if (carrier_state
< LINK_CARRIER_STATE_CARRIER
|| address_state
== LINK_ADDRESS_STATE_OFF
)
1724 operstate
= (LinkOperationalState
) carrier_state
;
1725 else if (address_state
== LINK_ADDRESS_STATE_ROUTABLE
)
1726 operstate
= LINK_OPERSTATE_ROUTABLE
;
1727 else if (carrier_state
== LINK_CARRIER_STATE_CARRIER
)
1728 operstate
= LINK_OPERSTATE_DEGRADED
;
1730 operstate
= LINK_OPERSTATE_ENSLAVED
;
1732 /* Only determine online state for managed links with RequiredForOnline=yes */
1733 if (!link
->network
|| !link
->network
->required_for_online
)
1734 online_state
= _LINK_ONLINE_STATE_INVALID
;
1735 else if (operstate
< link
->network
->required_operstate_for_online
.min
||
1736 operstate
> link
->network
->required_operstate_for_online
.max
)
1737 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1739 AddressFamily required_family
= link
->network
->required_family_for_online
;
1740 bool needs_ipv4
= required_family
& ADDRESS_FAMILY_IPV4
;
1741 bool needs_ipv6
= required_family
& ADDRESS_FAMILY_IPV6
;
1743 /* The operational state is within the range required for online.
1744 * If a particular address family is also required, we might revert
1745 * to offline in the blocks below. */
1746 online_state
= LINK_ONLINE_STATE_ONLINE
;
1748 if (link
->network
->required_operstate_for_online
.min
>= LINK_OPERSTATE_DEGRADED
) {
1749 if (needs_ipv4
&& ipv4_address_state
< LINK_ADDRESS_STATE_DEGRADED
)
1750 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1751 if (needs_ipv6
&& ipv6_address_state
< LINK_ADDRESS_STATE_DEGRADED
)
1752 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1755 if (link
->network
->required_operstate_for_online
.min
>= LINK_OPERSTATE_ROUTABLE
) {
1756 if (needs_ipv4
&& ipv4_address_state
< LINK_ADDRESS_STATE_ROUTABLE
)
1757 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1758 if (needs_ipv6
&& ipv6_address_state
< LINK_ADDRESS_STATE_ROUTABLE
)
1759 online_state
= LINK_ONLINE_STATE_OFFLINE
;
1763 if (link
->carrier_state
!= carrier_state
) {
1764 link
->carrier_state
= carrier_state
;
1766 if (strv_extend(&p
, "CarrierState") < 0)
1770 if (link
->address_state
!= address_state
) {
1771 link
->address_state
= address_state
;
1773 if (strv_extend(&p
, "AddressState") < 0)
1777 if (link
->ipv4_address_state
!= ipv4_address_state
) {
1778 link
->ipv4_address_state
= ipv4_address_state
;
1780 if (strv_extend(&p
, "IPv4AddressState") < 0)
1784 if (link
->ipv6_address_state
!= ipv6_address_state
) {
1785 link
->ipv6_address_state
= ipv6_address_state
;
1787 if (strv_extend(&p
, "IPv6AddressState") < 0)
1791 if (link
->operstate
!= operstate
) {
1792 link
->operstate
= operstate
;
1794 if (strv_extend(&p
, "OperationalState") < 0)
1798 if (link
->online_state
!= online_state
) {
1799 link
->online_state
= online_state
;
1801 if (strv_extend(&p
, "OnlineState") < 0)
1806 link_send_changed_strv(link
, p
);
1810 if (also_update_master
) {
1813 if (link_get_master(link
, &master
) >= 0)
1814 link_update_operstate(master
, true);
1818 #define FLAG_STRING(string, flag, old, new) \
1819 (((old ^ new) & flag) \
1820 ? ((old & flag) ? (" -" string) : (" +" string)) \
1823 static int link_update_flags(Link
*link
, sd_netlink_message
*message
) {
1824 bool link_was_lower_up
, link_was_admin_up
, had_carrier
;
1832 r
= sd_rtnl_message_link_get_flags(message
, &flags
);
1834 return log_link_debug_errno(link
, r
, "rtnl: failed to read link flags: %m");
1836 r
= sd_netlink_message_read_u8(message
, IFLA_OPERSTATE
, &operstate
);
1838 /* If we got a message without operstate, assume the state was unchanged. */
1839 operstate
= link
->kernel_operstate
;
1841 return log_link_debug_errno(link
, r
, "rtnl: failed to read operational state: %m");
1843 if (link
->flags
== flags
&& link
->kernel_operstate
== operstate
)
1846 if (link
->flags
!= flags
) {
1847 unsigned unknown_flags
, unknown_flags_added
, unknown_flags_removed
;
1849 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",
1850 FLAG_STRING("LOOPBACK", IFF_LOOPBACK
, link
->flags
, flags
),
1851 FLAG_STRING("MASTER", IFF_MASTER
, link
->flags
, flags
),
1852 FLAG_STRING("SLAVE", IFF_SLAVE
, link
->flags
, flags
),
1853 FLAG_STRING("UP", IFF_UP
, link
->flags
, flags
),
1854 FLAG_STRING("DORMANT", IFF_DORMANT
, link
->flags
, flags
),
1855 FLAG_STRING("LOWER_UP", IFF_LOWER_UP
, link
->flags
, flags
),
1856 FLAG_STRING("RUNNING", IFF_RUNNING
, link
->flags
, flags
),
1857 FLAG_STRING("MULTICAST", IFF_MULTICAST
, link
->flags
, flags
),
1858 FLAG_STRING("BROADCAST", IFF_BROADCAST
, link
->flags
, flags
),
1859 FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT
, link
->flags
, flags
),
1860 FLAG_STRING("PROMISC", IFF_PROMISC
, link
->flags
, flags
),
1861 FLAG_STRING("ALLMULTI", IFF_ALLMULTI
, link
->flags
, flags
),
1862 FLAG_STRING("PORTSEL", IFF_PORTSEL
, link
->flags
, flags
),
1863 FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA
, link
->flags
, flags
),
1864 FLAG_STRING("DYNAMIC", IFF_DYNAMIC
, link
->flags
, flags
),
1865 FLAG_STRING("NOARP", IFF_NOARP
, link
->flags
, flags
),
1866 FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS
, link
->flags
, flags
),
1867 FLAG_STRING("DEBUG", IFF_DEBUG
, link
->flags
, flags
),
1868 FLAG_STRING("ECHO", IFF_ECHO
, link
->flags
, flags
));
1870 unknown_flags
= ~(IFF_LOOPBACK
| IFF_MASTER
| IFF_SLAVE
| IFF_UP
|
1871 IFF_DORMANT
| IFF_LOWER_UP
| IFF_RUNNING
|
1872 IFF_MULTICAST
| IFF_BROADCAST
| IFF_POINTOPOINT
|
1873 IFF_PROMISC
| IFF_ALLMULTI
| IFF_PORTSEL
|
1874 IFF_AUTOMEDIA
| IFF_DYNAMIC
| IFF_NOARP
|
1875 IFF_NOTRAILERS
| IFF_DEBUG
| IFF_ECHO
);
1876 unknown_flags_added
= ((link
->flags
^ flags
) & flags
& unknown_flags
);
1877 unknown_flags_removed
= ((link
->flags
^ flags
) & link
->flags
& unknown_flags
);
1879 if (unknown_flags_added
)
1880 log_link_debug(link
, "Unknown link flags gained, ignoring: %#.5x", unknown_flags_added
);
1882 if (unknown_flags_removed
)
1883 log_link_debug(link
, "Unknown link flags lost, ignoring: %#.5x", unknown_flags_removed
);
1886 link_was_lower_up
= link
->flags
& IFF_LOWER_UP
;
1887 link_was_admin_up
= link
->flags
& IFF_UP
;
1888 had_carrier
= link_has_carrier(link
);
1890 link
->flags
= flags
;
1891 link
->kernel_operstate
= operstate
;
1893 link_update_operstate(link
, true);
1895 if (!link_was_lower_up
&& (link
->flags
& IFF_LOWER_UP
)) {
1896 r
= wifi_get_info(link
);
1900 /* All link information is up-to-date. So, it is not necessary to call
1901 * RTM_GETLINK netlink method again. */
1902 r
= link_reconfigure_impl(link
, /* force = */ false);
1908 if (!link_was_admin_up
&& (link
->flags
& IFF_UP
)) {
1909 log_link_info(link
, "Link UP");
1911 r
= link_admin_state_up(link
);
1914 } else if (link_was_admin_up
&& !(link
->flags
& IFF_UP
)) {
1915 log_link_info(link
, "Link DOWN");
1917 r
= link_admin_state_down(link
);
1922 r
= link_update_lldp(link
);
1926 if (!had_carrier
&& link_has_carrier(link
)) {
1927 log_link_info(link
, "Gained carrier");
1929 r
= link_carrier_gained(link
);
1932 } else if (had_carrier
&& !link_has_carrier(link
)) {
1933 log_link_info(link
, "Lost carrier");
1935 r
= link_carrier_lost(link
);
1943 static int link_update_master(Link
*link
, sd_netlink_message
*message
) {
1944 int master_ifindex
, r
;
1949 r
= sd_netlink_message_read_u32(message
, IFLA_MASTER
, (uint32_t*) &master_ifindex
);
1953 return log_link_debug_errno(link
, r
, "rtnl: failed to read master ifindex: %m");
1955 if (master_ifindex
== link
->master_ifindex
)
1958 if (link
->master_ifindex
== 0)
1959 log_link_debug(link
, "Joined to master interface: %i", master_ifindex
);
1960 else if (master_ifindex
== 0)
1961 log_link_debug(link
, "Leaved from master interface: %i", link
->master_ifindex
);
1963 log_link_debug(link
, "Master interface is changed: %i → %i", link
->master_ifindex
, master_ifindex
);
1965 link_drop_from_master(link
);
1967 link
->master_ifindex
= master_ifindex
;
1969 r
= link_append_to_master(link
);
1971 return log_link_debug_errno(link
, r
, "Failed to append link to master: %m");
1976 static int link_update_hardware_address(Link
*link
, sd_netlink_message
*message
) {
1977 struct hw_addr_data hw_addr
;
1983 r
= netlink_message_read_hw_addr(message
, IFLA_BROADCAST
, &link
->bcast_addr
);
1984 if (r
< 0 && r
!= -ENODATA
)
1985 return log_link_debug_errno(link
, r
, "rtnl: failed to read broadcast address: %m");
1987 r
= netlink_message_read_hw_addr(message
, IFLA_ADDRESS
, &hw_addr
);
1991 return log_link_warning_errno(link
, r
, "rtnl: failed to read hardware address: %m");
1993 if (hw_addr_equal(&link
->hw_addr
, &hw_addr
))
1996 link
->hw_addr
= hw_addr
;
1998 log_link_debug(link
, "Gained new hardware address: %s", HW_ADDR_TO_STR(&hw_addr
));
2000 r
= ipv4ll_update_mac(link
);
2002 return log_link_debug_errno(link
, r
, "Could not update MAC address in IPv4LL client: %m");
2004 r
= dhcp4_update_mac(link
);
2006 return log_link_debug_errno(link
, r
, "Could not update MAC address in DHCP client: %m");
2008 r
= dhcp6_update_mac(link
);
2010 return log_link_debug_errno(link
, r
, "Could not update MAC address in DHCPv6 client: %m");
2012 r
= radv_update_mac(link
);
2014 return log_link_debug_errno(link
, r
, "Could not update MAC address for Router Advertisement: %m");
2017 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->hw_addr
.ether
);
2019 return log_link_debug_errno(link
, r
, "Could not update MAC for NDisc: %m");
2023 r
= sd_lldp_set_filter_address(link
->lldp
, &link
->hw_addr
.ether
);
2025 return log_link_debug_errno(link
, r
, "Could not update MAC address for LLDP: %m");
2028 r
= ipv4_dad_update_mac(link
);
2030 return log_link_debug_errno(link
, r
, "Could not update MAC address in IPv4 ACD client: %m");
2035 static int link_update_mtu(Link
*link
, sd_netlink_message
*message
) {
2036 uint32_t mtu
, min_mtu
= 0, max_mtu
= UINT32_MAX
;
2042 r
= sd_netlink_message_read_u32(message
, IFLA_MTU
, &mtu
);
2046 return log_link_debug_errno(link
, r
, "rtnl: failed to read MTU in RTM_NEWLINK message: %m");
2050 r
= sd_netlink_message_read_u32(message
, IFLA_MIN_MTU
, &min_mtu
);
2051 if (r
< 0 && r
!= -ENODATA
)
2052 return log_link_debug_errno(link
, r
, "rtnl: failed to read minimum MTU in RTM_NEWLINK message: %m");
2054 r
= sd_netlink_message_read_u32(message
, IFLA_MAX_MTU
, &max_mtu
);
2055 if (r
< 0 && r
!= -ENODATA
)
2056 return log_link_debug_errno(link
, r
, "rtnl: failed to read maximum MTU in RTM_NEWLINK message: %m");
2059 max_mtu
= UINT32_MAX
;
2061 link
->min_mtu
= min_mtu
;
2062 link
->max_mtu
= max_mtu
;
2064 if (link
->original_mtu
== 0) {
2065 link
->original_mtu
= mtu
;
2066 log_link_debug(link
, "Saved original MTU %" PRIu32
" (min: %"PRIu32
", max: %"PRIu32
")",
2067 link
->original_mtu
, link
->min_mtu
, link
->max_mtu
);
2070 if (link
->mtu
== mtu
)
2074 log_link_debug(link
, "MTU is changed: %"PRIu32
" → %"PRIu32
" (min: %"PRIu32
", max: %"PRIu32
")",
2075 link
->mtu
, mtu
, link
->min_mtu
, link
->max_mtu
);
2079 if (link
->dhcp_client
) {
2080 r
= sd_dhcp_client_set_mtu(link
->dhcp_client
, link
->mtu
);
2082 return log_link_debug_errno(link
, r
, "Could not update MTU in DHCP client: %m");
2086 r
= sd_radv_set_mtu(link
->radv
, link
->mtu
);
2088 return log_link_debug_errno(link
, r
, "Could not set MTU for Router Advertisement: %m");
2094 static int link_update_alternative_names(Link
*link
, sd_netlink_message
*message
) {
2095 _cleanup_strv_free_
char **altnames
= NULL
;
2102 r
= sd_netlink_message_read_strv(message
, IFLA_PROP_LIST
, IFLA_ALT_IFNAME
, &altnames
);
2103 if (r
< 0 && r
!= -ENODATA
)
2104 return log_link_debug_errno(link
, r
, "rtnl: failed to read alternative names: %m");
2106 STRV_FOREACH(n
, link
->alternative_names
)
2107 hashmap_remove(link
->manager
->links_by_name
, *n
);
2109 strv_free_and_replace(link
->alternative_names
, altnames
);
2111 STRV_FOREACH(n
, link
->alternative_names
) {
2112 r
= hashmap_ensure_put(&link
->manager
->links_by_name
, &string_hash_ops
, *n
, link
);
2114 return log_link_debug_errno(link
, r
, "Failed to manage link by its new alternative names: %m");
2120 static int link_update_name(Link
*link
, sd_netlink_message
*message
) {
2127 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &ifname
);
2132 return log_link_debug_errno(link
, r
, "Failed to read interface name in RTM_NEWLINK message: %m");
2134 if (streq(ifname
, link
->ifname
))
2137 log_link_info(link
, "Interface name change detected, renamed to %s.", ifname
);
2139 hashmap_remove(link
->manager
->links_by_name
, link
->ifname
);
2141 r
= free_and_strdup(&link
->ifname
, ifname
);
2143 return log_oom_debug();
2145 r
= hashmap_ensure_put(&link
->manager
->links_by_name
, &string_hash_ops
, link
->ifname
, link
);
2147 return log_link_debug_errno(link
, r
, "Failed to manage link by its new name: %m");
2152 static int link_update(Link
*link
, sd_netlink_message
*message
) {
2158 r
= link_update_name(link
, message
);
2162 r
= link_update_alternative_names(link
, message
);
2166 r
= link_update_mtu(link
, message
);
2170 r
= link_update_hardware_address(link
, message
);
2174 r
= link_update_master(link
, message
);
2178 return link_update_flags(link
, message
);
2181 static Link
*link_drop_or_unref(Link
*link
) {
2185 return link_unref(link
);
2186 return link_drop(link
);
2189 DEFINE_TRIVIAL_CLEANUP_FUNC(Link
*, link_drop_or_unref
);
2191 static int link_new(Manager
*manager
, sd_netlink_message
*message
, Link
**ret
) {
2192 _cleanup_free_
char *ifname
= NULL
, *kind
= NULL
, *state_file
= NULL
, *lease_file
= NULL
, *lldp_file
= NULL
;
2193 _cleanup_(link_drop_or_unrefp
) Link
*link
= NULL
;
2194 unsigned short iftype
;
2201 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
2203 return log_debug_errno(r
, "rtnl: failed to read ifindex from link message: %m");
2204 else if (ifindex
<= 0)
2205 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "rtnl: received link message without valid ifindex.");
2207 r
= sd_rtnl_message_link_get_type(message
, &iftype
);
2209 return log_debug_errno(r
, "rtnl: failed to read interface type from link message: %m");
2211 r
= sd_netlink_message_read_string_strdup(message
, IFLA_IFNAME
, &ifname
);
2213 return log_debug_errno(r
, "rtnl: failed to read interface name from link message: %m");
2215 /* check for link kind */
2216 r
= sd_netlink_message_enter_container(message
, IFLA_LINKINFO
);
2218 r
= sd_netlink_message_read_string_strdup(message
, IFLA_INFO_KIND
, &kind
);
2219 if (r
< 0 && r
!= -ENODATA
)
2220 return log_debug_errno(r
, "rtnl: failed to read interface kind from link message: %m");
2221 r
= sd_netlink_message_exit_container(message
);
2223 return log_debug_errno(r
, "rtnl: failed to exit IFLA_LINKINFO container: %m");
2226 if (asprintf(&state_file
, "/run/systemd/netif/links/%d", ifindex
) < 0)
2227 return log_oom_debug();
2229 if (asprintf(&lease_file
, "/run/systemd/netif/leases/%d", ifindex
) < 0)
2230 return log_oom_debug();
2232 if (asprintf(&lldp_file
, "/run/systemd/netif/lldp/%d", ifindex
) < 0)
2233 return log_oom_debug();
2235 link
= new(Link
, 1);
2241 .state
= LINK_STATE_PENDING
,
2242 .online_state
= _LINK_ONLINE_STATE_INVALID
,
2245 .ifname
= TAKE_PTR(ifname
),
2246 .kind
= TAKE_PTR(kind
),
2248 .state_file
= TAKE_PTR(state_file
),
2249 .lease_file
= TAKE_PTR(lease_file
),
2250 .lldp_file
= TAKE_PTR(lldp_file
),
2253 .dns_default_route
= -1,
2254 .llmnr
= _RESOLVE_SUPPORT_INVALID
,
2255 .mdns
= _RESOLVE_SUPPORT_INVALID
,
2256 .dnssec_mode
= _DNSSEC_MODE_INVALID
,
2257 .dns_over_tls_mode
= _DNS_OVER_TLS_MODE_INVALID
,
2260 r
= hashmap_ensure_put(&manager
->links
, NULL
, INT_TO_PTR(link
->ifindex
), link
);
2262 return log_link_debug_errno(link
, r
, "Failed to store link into manager: %m");
2264 link
->manager
= manager
;
2266 r
= hashmap_ensure_put(&manager
->links_by_name
, &string_hash_ops
, link
->ifname
, link
);
2268 return log_link_debug_errno(link
, r
, "Failed to manage link by its interface name: %m");
2270 r
= ethtool_get_permanent_macaddr(&manager
->ethtool_fd
, link
->ifname
, &link
->permanent_mac
);
2272 log_link_debug_errno(link
, r
, "Permanent MAC address not found for new device, continuing without: %m");
2274 r
= ethtool_get_driver(&manager
->ethtool_fd
, link
->ifname
, &link
->driver
);
2276 log_link_debug_errno(link
, r
, "Failed to get driver, continuing without: %m");
2278 log_link_debug(link
, "Link %d added", link
->ifindex
);
2279 *ret
= TAKE_PTR(link
);
2283 int manager_rtnl_process_link(sd_netlink
*rtnl
, sd_netlink_message
*message
, Manager
*manager
) {
2285 NetDev
*netdev
= NULL
;
2294 if (sd_netlink_message_is_error(message
)) {
2295 r
= sd_netlink_message_get_errno(message
);
2297 log_message_warning_errno(message
, r
, "rtnl: Could not receive link message, ignoring");
2302 r
= sd_netlink_message_get_type(message
, &type
);
2304 log_warning_errno(r
, "rtnl: Could not get message type, ignoring: %m");
2306 } else if (!IN_SET(type
, RTM_NEWLINK
, RTM_DELLINK
)) {
2307 log_warning("rtnl: Received unexpected message type %u when processing link, ignoring.", type
);
2311 r
= sd_rtnl_message_link_get_ifindex(message
, &ifindex
);
2313 log_warning_errno(r
, "rtnl: Could not get ifindex from link message, ignoring: %m");
2315 } else if (ifindex
<= 0) {
2316 log_warning("rtnl: received link message with invalid ifindex %d, ignoring.", ifindex
);
2320 r
= sd_netlink_message_read_string(message
, IFLA_IFNAME
, &name
);
2322 log_warning_errno(r
, "rtnl: Received link message without ifname, ignoring: %m");
2326 (void) link_get(manager
, ifindex
, &link
);
2327 (void) netdev_get(manager
, name
, &netdev
);
2332 /* netdev exists, so make sure the ifindex matches */
2333 r
= netdev_set_ifindex(netdev
, message
);
2335 log_warning_errno(r
, "Could not process new link message for netdev, ignoring: %m");
2341 /* link is new, so add it */
2342 r
= link_new(manager
, message
, &link
);
2344 log_warning_errno(r
, "Could not process new link message: %m");
2348 r
= link_update(link
, message
);
2350 log_warning_errno(r
, "Could not process link message: %m");
2351 link_enter_failed(link
);
2355 r
= link_check_initialized(link
);
2357 log_warning_errno(r
, "Failed to check link is initialized: %m");
2358 link_enter_failed(link
);
2362 r
= link_update(link
, message
);
2364 log_warning_errno(r
, "Could not process link message: %m");
2365 link_enter_failed(link
);
2374 netdev_drop(netdev
);
2379 assert_not_reached("Received link message with invalid RTNL message type.");
2385 int link_getlink_handler_internal(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
, const char *error_msg
) {
2386 uint16_t message_type
;
2393 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
2396 r
= sd_netlink_message_get_errno(m
);
2398 log_link_message_warning_errno(link
, m
, r
, error_msg
);
2399 link_enter_failed(link
);
2403 r
= sd_netlink_message_get_type(m
, &message_type
);
2405 log_link_debug_errno(link
, r
, "rtnl: failed to read link message type, ignoring: %m");
2408 if (message_type
!= RTM_NEWLINK
) {
2409 log_link_debug(link
, "rtnl: received invalid link message type, ignoring.");
2413 r
= link_update(link
, m
);
2415 link_enter_failed(link
);
2422 int link_call_getlink(Link
*link
, link_netlink_message_handler_t callback
) {
2423 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
2427 assert(link
->manager
);
2428 assert(link
->manager
->rtnl
);
2431 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_GETLINK
, link
->ifindex
);
2435 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
2436 link_netlink_destroy_callback
, link
);
2444 static const char* const link_state_table
[_LINK_STATE_MAX
] = {
2445 [LINK_STATE_PENDING
] = "pending",
2446 [LINK_STATE_INITIALIZED
] = "initialized",
2447 [LINK_STATE_CONFIGURING
] = "configuring",
2448 [LINK_STATE_CONFIGURED
] = "configured",
2449 [LINK_STATE_UNMANAGED
] = "unmanaged",
2450 [LINK_STATE_FAILED
] = "failed",
2451 [LINK_STATE_LINGER
] = "linger",
2454 DEFINE_STRING_TABLE_LOOKUP(link_state
, LinkState
);