HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-link.c

119,885 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: ea4965d466277b64dca8045abd509ddb28094d14

Lines 1–1000 of 3,219

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_link.h>
#include <linux/netdevice.h>
#include <net/if.h>
#include <sys/socket.h>
#include <unistd.h>
 
#include "sd-bus.h"
#include "sd-dhcp-client.h"
#include "sd-dhcp-relay.h"
#include "sd-dhcp-server.h"
#include "sd-dhcp6-client.h"
#include "sd-dhcp6-lease.h"
#include "sd-ipv4ll.h"
#include "sd-lldp-rx.h"
#include "sd-ndisc.h"
#include "sd-netlink.h"
#include "sd-radv.h"
#include "sd-varlink.h"
 
#include "alloc-util.h"
#include "arphrd-util.h"
#include "bitfield.h"
#include "device-private.h"
#include "device-util.h"
#include "dns-domain.h"
#include "errno-util.h"
#include "ethtool-util.h"
#include "event-util.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "logarithm.h"
#include "netif-util.h"
#include "netlink-util.h"
#include "networkd-address.h"
#include "networkd-address-label.h"
#include "networkd-bridge-fdb.h"
#include "networkd-bridge-mdb.h"
#include "networkd-bridge-vlan.h"
#include "networkd-dhcp-prefix-delegation.h"
#include "networkd-dhcp-relay.h"
#include "networkd-dhcp-server.h"
#include "networkd-dhcp4.h"
#include "networkd-dhcp6.h"
#include "networkd-ipv4acd.h"
#include "networkd-ipv4ll.h"
#include "networkd-link.h"
#include "networkd-link-bus.h"
#include "networkd-lldp-tx.h"
#include "networkd-manager.h"
#include "networkd-ndisc.h"
#include "networkd-neighbor.h"
#include "networkd-neighbor-proxy.h"
#include "networkd-nexthop.h"
#include "networkd-queue.h"
#include "networkd-radv.h"
#include "networkd-resolve-hook.h"
#include "networkd-route.h"
#include "networkd-route-util.h"
#include "networkd-routing-policy-rule.h"
#include "networkd-setlink.h"
#include "networkd-sriov.h"
#include "networkd-state-file.h"
#include "networkd-sysctl.h"
#include "networkd-wifi.h"
#include "networkd-wwan-bus.h"
#include "ordered-set.h"
#include "set.h"
#include "socket-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "tc.h"
#include "udev-util.h"
 
void link_required_operstate_for_online(Link *link, LinkOperationalStateRange *ret) {
        assert(link);
        assert(ret);
 
        if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
                /* If explicitly specified, use it as is. */
                *ret = link->network->required_operstate_for_online;
        else if (link->iftype == ARPHRD_CAN)
                /* CAN devices do not support addressing, hence defaults to 'carrier'. */
                *ret = (const LinkOperationalStateRange) {
                        .min = LINK_OPERSTATE_CARRIER,
                        .max = LINK_OPERSTATE_CARRIER,
                };
        else if (link->network && link->network->bond)
                /* Bonding slaves do not support addressing. */
                *ret = (const LinkOperationalStateRange) {
                        .min = LINK_OPERSTATE_ENSLAVED,
                        .max = LINK_OPERSTATE_ENSLAVED,
                };
        else if (STRPTR_IN_SET(link->kind, "batadv", "bond", "bridge", "vrf"))
                /* Some of slave interfaces may be offline. */
                *ret = (const LinkOperationalStateRange) {
                        .min = LINK_OPERSTATE_DEGRADED_CARRIER,
                        .max = LINK_OPERSTATE_ROUTABLE,
                };
        else
                *ret = LINK_OPERSTATE_RANGE_DEFAULT;
}
 
AddressFamily link_required_family_for_online(Link *link) {
        assert(link);
 
        if (link->network && link->network->required_family_for_online >= 0)
                return link->network->required_family_for_online;
 
        if (link->network && operational_state_range_is_valid(&link->network->required_operstate_for_online))
                /* If RequiredForOnline= is explicitly specified, defaults to no. */
                return ADDRESS_FAMILY_NO;
 
        if (STRPTR_IN_SET(link->kind, "batadv", "bond", "bridge", "vrf"))
                /* As the minimum required operstate for master interfaces is 'degraded-carrier',
                 * we should request an address assigned to the link for backward compatibility. */
                return ADDRESS_FAMILY_YES;
 
        return ADDRESS_FAMILY_NO;
}
 
bool link_ipv6_enabled(Link *link) {
        assert(link);
 
        if (!socket_ipv6_is_supported())
                return false;
 
        if (link->iftype == ARPHRD_CAN)
                return false;
 
        if (!link->network)
                return false;
 
        if (link->network->bond)
                return false;
 
        if (link_ipv6ll_enabled(link))
                return true;
 
        if (network_has_static_ipv6_configurations(link->network))
                return true;
 
        return false;
}
 
bool link_has_ipv6_connectivity(Link *link) {
        LinkAddressState ipv6_address_state;
 
        assert(link);
 
        link_get_address_states(link, NULL, &ipv6_address_state, NULL);
 
        switch (ipv6_address_state) {
        case LINK_ADDRESS_STATE_ROUTABLE:
                /* If the interface has a routable IPv6 address, then we assume yes. */
                return true;
 
        case LINK_ADDRESS_STATE_DEGRADED:
                /* If the interface has only degraded IPv6 address (mostly, link-local address), then let's check
                 * there is an IPv6 default gateway. */
                return link_has_default_gateway(link, AF_INET6);
 
        case LINK_ADDRESS_STATE_OFF:
                /* No IPv6 address. */
                return false;
 
        default:
                assert_not_reached();
        }
}
 
static bool link_is_ready_to_configure_one(Link *link, bool allow_unmanaged) {
        assert(link);
 
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED, LINK_STATE_UNMANAGED))
                return false;
 
        if (!link->network)
                return allow_unmanaged;
 
        if (!link->network->configure_without_carrier) {
                if (link->set_flags_messages > 0)
                        return false;
 
                if (!link_has_carrier(link))
                        return false;
        }
 
        if (link->set_link_messages > 0)
                return false;
 
        if (!link->activated)
                return false;
 
        return true;
}
 
bool link_is_ready_to_configure(Link *link, bool allow_unmanaged) {
        return check_ready_for_all_sr_iov_ports(link, allow_unmanaged, link_is_ready_to_configure_one);
}
 
bool link_is_ready_to_configure_by_name(Manager *manager, const char *name, bool allow_unmanaged) {
        assert(manager);
        assert(name);
 
        Link *link;
        if (link_get_by_name(manager, name, &link) < 0)
                return false;
 
        return link_is_ready_to_configure(link, allow_unmanaged);
}
 
void link_ntp_settings_clear(Link *link) {
        link->ntp = strv_free(link->ntp);
}
 
void link_dns_settings_clear(Link *link) {
        if (link->n_dns != UINT_MAX)
                for (unsigned i = 0; i < link->n_dns; i++)
                        in_addr_full_free(link->dns[i]);
        link->dns = mfree(link->dns);
        link->n_dns = UINT_MAX;
 
        link->search_domains = ordered_set_free(link->search_domains);
        link->route_domains = ordered_set_free(link->route_domains);
 
        link->dns_default_route = -1;
        link->llmnr = _RESOLVE_SUPPORT_INVALID;
        link->mdns = _RESOLVE_SUPPORT_INVALID;
        link->dnssec_mode = _DNSSEC_MODE_INVALID;
        link->dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID;
 
        link->dnssec_negative_trust_anchors = set_free(link->dnssec_negative_trust_anchors);
}
 
static void link_free_engines(Link *link) {
        if (!link)
                return;
 
        link->dhcp_relay_interface = sd_dhcp_relay_interface_unref(link->dhcp_relay_interface);
        link->dhcp_relay_interface_compat = sd_dhcp_relay_interface_unref(link->dhcp_relay_interface_compat);
        link->dhcp_server = sd_dhcp_server_unref(link->dhcp_server);
 
        link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
        link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
        link->dhcp4_6rd_tunnel_name = mfree(link->dhcp4_6rd_tunnel_name);
 
        link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
        link->lldp_tx = sd_lldp_tx_unref(link->lldp_tx);
 
        link->ipv4acd_by_address = hashmap_free(link->ipv4acd_by_address);
 
        link->ipv4ll = sd_ipv4ll_unref(link->ipv4ll);
 
        link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
        link->dhcp6_lease = sd_dhcp6_lease_unref(link->dhcp6_lease);
 
        link->ndisc = sd_ndisc_unref(link->ndisc);
        link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire);
        ndisc_flush(link);
 
        link->radv = sd_radv_unref(link->radv);
}
 
static Link* link_free(Link *link) {
        assert(link);
 
        (void) link_clear_sysctl_shadows(link);
 
        link_ntp_settings_clear(link);
        link_dns_settings_clear(link);
 
        link->neighbors = set_free(link->neighbors);
        link->addresses = set_free(link->addresses);
        link->qdiscs = set_free(link->qdiscs);
        link->tclasses = set_free(link->tclasses);
 
        link->dhcp_pd_prefixes = set_free(link->dhcp_pd_prefixes);
 
        link_free_engines(link);
 
        set_free(link->sr_iov_virt_port_ifindices);
        free(link->ifname);
        strv_free(link->alternative_names);
        free(link->kind);
        free(link->ssid);
        free(link->previous_ssid);
        free(link->driver);
 
        unlink_and_free(link->state_file);
 
        sd_device_unref(link->dev);
        netdev_unref(link->netdev);
 
        hashmap_free(link->bound_to_links);
        hashmap_free(link->bound_by_links);
 
        set_free(link->slaves);
 
        network_unref(link->network);
 
        sd_event_source_disable_unref(link->carrier_lost_timer);
        sd_event_source_disable_unref(link->ipv6_mtu_wait_synced_event_source);
 
        return mfree(link);
}
 
DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free);
 
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
                link_hash_ops,
                void, trivial_hash_func, trivial_compare_func,
                Link, link_unref);
 
int link_get_by_index(Manager *m, int ifindex, Link **ret) {
        Link *link;
 
        assert(m);
 
        if (ifindex <= 0)
                return -EINVAL;
 
        link = hashmap_get(m->links_by_index, INT_TO_PTR(ifindex));
        if (!link)
                return -ENODEV;
 
        if (ret)
                *ret = link;
        return 0;
}
 
int link_get_by_name(Manager *m, const char *ifname, Link **ret) {
        Link *link;
 
        assert(m);
        assert(ifname);
 
        link = hashmap_get(m->links_by_name, ifname);
        if (!link)
                return -ENODEV;
 
        if (ret)
                *ret = link;
        return 0;
}
 
int link_get_by_hw_addr(Manager *m, const struct hw_addr_data *hw_addr, Link **ret) {
        Link *link;
 
        assert(m);
        assert(hw_addr);
 
        link = hashmap_get(m->links_by_hw_addr, hw_addr);
        if (!link)
                return -ENODEV;
 
        if (ret)
                *ret = link;
        return 0;
}
 
int link_get_master(Link *link, Link **ret) {
        assert(link);
        assert(link->manager);
        assert(ret);
 
        if (link->master_ifindex <= 0 || link->master_ifindex == link->ifindex)
                return -ENODEV;
 
        return link_get_by_index(link->manager, link->master_ifindex, ret);
}
 
void link_set_state(Link *link, LinkState state) {
        assert(link);
 
        if (link->state == state)
                return;
 
        log_link_debug(link, "State changed: %s -> %s",
                       link_state_to_string(link->state),
                       link_state_to_string(state));
 
        link->state = state;
 
        link_send_changed(link, "AdministrativeState");
        link_dirty(link);
}
 
int link_stop_engines(Link *link, bool may_keep_dynamic) {
        int r, ret = 0;
 
        assert(link);
        assert(link->manager);
        assert(link->manager->event);
 
        bool keep_dynamic =
                may_keep_dynamic &&
                link->network &&
                (link->manager->state == MANAGER_RESTARTING ||
                 FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC_ON_STOP));
 
        if (!keep_dynamic) {
                r = sd_dhcp_client_stop(link->dhcp_client);
                if (r < 0)
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"));
 
                r = sd_ipv4ll_stop(link->ipv4ll);
                if (r < 0)
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"));
 
                r = sd_dhcp6_client_stop(link->dhcp6_client);
                if (r < 0)
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
 
                r = dhcp_pd_remove(link, /* only_marked= */ false);
                if (r < 0)
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not remove DHCPv6 PD addresses and routes: %m"));
 
                r = ndisc_stop(link);
                if (r < 0)
                        RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
 
                ndisc_flush(link);
        }
 
        r = sd_dhcp_relay_interface_stop(link->dhcp_relay_interface);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCP relay agent: %m"));
 
        r = sd_dhcp_relay_interface_stop(link->dhcp_relay_interface_compat);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCP relay agent (compat): %m"));
 
        r = sd_dhcp_server_stop(link->dhcp_server);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv4 server: %m"));
 
        r = sd_lldp_rx_stop(link->lldp_rx);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Rx: %m"));
 
        r = sd_lldp_tx_stop(link->lldp_tx);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop LLDP Tx: %m"));
 
        r = ipv4acd_stop(link);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv4 ACD client: %m"));
 
        r = sd_radv_stop(link->radv);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
 
        return ret;
}
 
void link_enter_failed(Link *link) {
        assert(link);
 
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return;
 
        log_link_warning(link, "Failed");
 
        link_set_state(link, LINK_STATE_FAILED);
 
        if (!ratelimit_below(&link->automatic_reconfigure_ratelimit)) {
                log_link_warning(link, "The interface entered the failed state frequently, refusing to reconfigure it automatically.");
                goto stop;
        }
 
        log_link_info(link, "Trying to reconfigure the interface.");
        if (link_reconfigure(link, LINK_RECONFIGURE_UNCONDITIONALLY) > 0)
                return;
 
stop:
        (void) link_stop_engines(link, /* may_keep_dynamic= */ false);
}
 
void link_check_ready(Link *link) {
        Address *a;
 
        assert(link);
 
        if (link->state == LINK_STATE_CONFIGURED)
                return;
 
        if (link->state != LINK_STATE_CONFIGURING)
                return (void) log_link_debug(link, "%s(): link is in %s state.", __func__, link_state_to_string(link->state));
 
        if (!link->network)
                return (void) log_link_debug(link, "%s(): link is unmanaged.", __func__);
 
        if (!link->tc_configured)
                return (void) log_link_debug(link, "%s(): traffic controls are not configured.", __func__);
 
        if (link->set_link_messages > 0)
                return (void) log_link_debug(link, "%s(): link layer is configuring.", __func__);
 
        if (!link->activated)
                return (void) log_link_debug(link, "%s(): link is not activated.", __func__);
 
        if (link->iftype == ARPHRD_CAN)
                /* let's shortcut things for CAN which doesn't need most of checks below. */
                goto ready;
 
        if (!link->stacked_netdevs_created)
                return (void) log_link_debug(link, "%s(): stacked netdevs are not created.", __func__);
 
        if (!link->static_addresses_configured)
                return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
 
        if (!link->static_address_labels_configured)
                return (void) log_link_debug(link, "%s(): static address labels are not configured.", __func__);
 
        if (!link->static_bridge_fdb_configured)
                return (void) log_link_debug(link, "%s(): static bridge FDB entries are not configured.", __func__);
 
        if (!link->static_bridge_mdb_configured)
                return (void) log_link_debug(link, "%s(): static bridge MDB entries are not configured.", __func__);
 
        if (!link->static_neighbors_configured)
                return (void) log_link_debug(link, "%s(): static neighbors are not configured.", __func__);
 
        if (!link->static_neighbor_proxy_configured)
                return (void) log_link_debug(link, "%s(): static neighbor proxy addresses are not configured.", __func__);
 
        if (!link->static_nexthops_configured)
                return (void) log_link_debug(link, "%s(): static nexthops are not configured.", __func__);
 
        if (!link->static_routes_configured)
                return (void) log_link_debug(link, "%s(): static routes are not configured.", __func__);
 
        if (!link->static_routing_policy_rules_configured)
                return (void) log_link_debug(link, "%s(): static routing policy rules are not configured.", __func__);
 
        if (!link->sr_iov_configured)
                return (void) log_link_debug(link, "%s(): SR-IOV is not configured.", __func__);
 
        if (!link->bearer_configured)
                return (void) log_link_debug(link, "%s(): Bearer has not been applied.", __func__);
 
        /* IPv6LL is assigned after the link gains its carrier. */
        if (!link->network->configure_without_carrier &&
            link_ipv6ll_enabled(link) &&
            !in6_addr_is_set(&link->ipv6ll_address))
                return (void) log_link_debug(link, "%s(): IPv6LL is not configured yet.", __func__);
 
        /* All static addresses must be ready. */
        bool has_static_address = false;
        SET_FOREACH(a, link->addresses) {
                if (!IN_SET(a->source, NETWORK_CONFIG_SOURCE_STATIC, NETWORK_CONFIG_SOURCE_MODEM_MANAGER))
                        continue;
                if (!address_is_ready(a))
                        return (void) log_link_debug(link, "%s(): static address %s is not ready.", __func__,
                                                     IN_ADDR_PREFIX_TO_STRING(a->family, &a->in_addr, a->prefixlen));
                has_static_address = true;
        }
 
        /* If at least one static address is requested, do not request that dynamic addressing protocols are finished. */
        if (has_static_address)
                goto ready;
 
        /* If no dynamic addressing protocol enabled, assume the interface is ready.
         * Note, ignore NDisc when ConfigureWithoutCarrier= is enabled, as IPv6AcceptRA= is enabled by default. */
        if (!link_ipv4ll_enabled(link) && !link_dhcp4_enabled(link) &&
            !link_dhcp6_enabled(link) && !link_dhcp_pd_is_enabled(link) &&
            (link->network->configure_without_carrier || !link_ndisc_enabled(link)))
                goto ready;
 
        bool ipv4ll_ready =
                link_ipv4ll_enabled(link) && link->ipv4ll_address_configured &&
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_IPV4LL);
        bool dhcp4_ready =
                link_dhcp4_enabled(link) && link->dhcp4_configured &&
                link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP4);
        bool dhcp6_ready =
                link_dhcp6_enabled(link) && link->dhcp6_configured &&
                (!link->network->dhcp6_use_address ||
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP6));
        bool dhcp_pd_ready =
                link_dhcp_pd_is_enabled(link) && link->dhcp_pd_configured &&
                (!link->network->dhcp_pd_assign ||
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_DHCP_PD));
        bool ndisc_ready =
                link_ndisc_enabled(link) && link->ndisc_configured &&
                (!link->network->ndisc_use_autonomous_prefix ||
                 link_check_addresses_ready(link, NETWORK_CONFIG_SOURCE_NDISC));
 
        /* If the uplink for PD is self, then request the corresponding DHCP protocol is also ready. */
        if (dhcp_pd_is_uplink(link, link, /* accept_auto= */ false)) {
                if (link_dhcp4_enabled(link) && link->network->dhcp_use_6rd &&
                    sd_dhcp_lease_has_6rd(link->dhcp_lease)) {
                        if (!link->dhcp4_configured)
                                return (void) log_link_debug(link, "%s(): DHCPv4 6rd prefix is assigned, but DHCPv4 protocol is not finished yet.", __func__);
                        if (!dhcp_pd_ready)
                                return (void) log_link_debug(link, "%s(): DHCPv4 is finished, but prefix acquired by DHCPv4-6rd is not assigned yet.", __func__);
                }
 
                if (link_dhcp6_enabled(link) && link->network->dhcp6_use_pd_prefix &&
                    sd_dhcp6_lease_has_pd_prefix(link->dhcp6_lease)) {
                        if (!link->dhcp6_configured)
                                return (void) log_link_debug(link, "%s(): DHCPv6 IA_PD prefix is assigned, but DHCPv6 protocol is not finished yet.", __func__);
                        if (!dhcp_pd_ready)
                                return (void) log_link_debug(link, "%s(): DHCPv6 is finished, but prefix acquired by DHCPv6 IA_PD is not assigned yet.", __func__);
                }
        }
 
        /* At least one dynamic addressing protocol is finished. */
        if (!ipv4ll_ready && !dhcp4_ready && !dhcp6_ready && !dhcp_pd_ready && !ndisc_ready)
                return (void) log_link_debug(link, "%s(): dynamic addressing protocols are enabled but none of them finished yet.", __func__);
 
        log_link_debug(link, "%s(): IPv4LL:%s DHCPv4:%s DHCPv6:%s DHCP-PD:%s NDisc:%s",
                       __func__,
                       yes_no(ipv4ll_ready),
                       yes_no(dhcp4_ready),
                       yes_no(dhcp6_ready),
                       yes_no(dhcp_pd_ready),
                       yes_no(ndisc_ready));
 
ready:
        link_set_state(link, LINK_STATE_CONFIGURED);
}
 
static int link_request_static_configs(Link *link) {
        int r;
 
        assert(link);
        assert(link->network);
        assert(link->state != _LINK_STATE_INVALID);
 
        r = link_request_static_addresses(link);
        if (r < 0)
                return r;
 
        r = link_request_static_address_labels(link);
        if (r < 0)
                return r;
 
        r = link_request_static_bridge_fdb(link);
        if (r < 0)
                return r;
 
        r = link_request_static_bridge_mdb(link);
        if (r < 0)
                return r;
 
        r = link_request_static_neighbors(link);
        if (r < 0)
                return r;
 
        r = link_request_static_neighbor_proxy_addresses(link);
        if (r < 0)
                return r;
 
        r = link_request_static_nexthops(link, false);
        if (r < 0)
                return r;
 
        r = link_request_static_routes(link, false);
        if (r < 0)
                return r;
 
        r = link_request_static_routing_policy_rules(link);
        if (r < 0)
                return r;
 
        return 0;
}
 
int link_request_stacked_netdevs(Link *link, NetDevLocalAddressType type) {
        NetDev *netdev;
        int r;
 
        assert(link);
 
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
                return 0;
 
        assert(link->network);
 
        link->stacked_netdevs_created = false;
 
        HASHMAP_FOREACH(netdev, link->network->stacked_netdevs) {
                if (!netdev_needs_reconfigure(netdev, type))
                        continue;
 
                r = link_request_stacked_netdev(link, netdev);
                if (r < 0)
                        return r;
        }
 
        if (link->create_stacked_netdev_messages == 0) {
                link->stacked_netdevs_created = true;
                link_check_ready(link);
        }
 
        return 0;
}
 
static int link_acquire_dynamic_ipv6_conf(Link *link) {
        int r;
 
        assert(link);
 
        r = radv_start(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Advertisement engine: %m");
 
        r = ndisc_start(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
 
        r = dhcp6_start(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to start DHCPv6 client: %m");
 
        return 0;
}
 
static int link_acquire_dynamic_ipv4_conf(Link *link) {
        int r;
 
        assert(link);
        assert(link->manager);
        assert(link->manager->event);
 
        if (link->dhcp_client) {
                r = dhcp4_start(link);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m");
 
                log_link_debug(link, "Acquiring DHCPv4 lease.");
 
        } else if (link->ipv4ll) {
                if (in4_addr_is_set(&link->network->ipv4ll_start_address)) {
                        r = sd_ipv4ll_set_address(link->ipv4ll, &link->network->ipv4ll_start_address);
                        if (r < 0)
                                return log_link_warning_errno(link, r, "Could not set IPv4 link-local start address: %m");
                }
 
                r = ipv4ll_start(link);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m");
                if (r > 0)
                        log_link_debug(link, "Acquiring IPv4 link-local address.");
        }
 
        r = link_start_dhcp_relay(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not start DHCP relay agent: %m");
 
        r = link_start_dhcp4_server(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not start DHCP server: %m");
 
        r = ipv4acd_start(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not start IPv4 ACD client: %m");
 
        return 0;
}
 
static int link_acquire_dynamic_conf(Link *link) {
        int r;
 
        assert(link);
        assert(link->network);
 
        r = link_acquire_dynamic_ipv4_conf(link);
        if (r < 0)
                return r;
 
        if (in6_addr_is_set(&link->ipv6ll_address)) {
                r = link_acquire_dynamic_ipv6_conf(link);
                if (r < 0)
                        return r;
        }
 
        if (!link_radv_enabled(link) || !link->network->dhcp_pd_announce) {
                /* DHCPv6PD downstream does not require IPv6LL address. But may require RADV to be
                 * configured, and RADV may not be configured yet here. Only acquire subnet prefix when
                 * RADV is disabled, or the announcement of the prefix is disabled. Otherwise, the
                 * below will be called in radv_start(). */
                r = dhcp_request_prefix_delegation(link);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Failed to request DHCP delegated subnet prefix: %m");
        }
 
        if (link->lldp_tx) {
                r = sd_lldp_tx_start(link->lldp_tx);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
        }
 
        if (link->lldp_rx) {
                r = sd_lldp_rx_start(link->lldp_rx);
                if (r < 0)
                        return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
        }
 
        return 0;
}
 
int link_ipv6ll_gained(Link *link) {
        int r;
 
        assert(link);
 
        log_link_info(link, "Gained IPv6LL");
 
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
                return 0;
 
        r = link_acquire_dynamic_ipv6_conf(link);
        if (r < 0)
                return r;
 
        r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
        if (r < 0)
                return r;
 
        link_check_ready(link);
        return 0;
}
 
int link_ipv6ll_lost(Link *link, const struct in6_addr *dropped_ipv6ll, bool has_replacement) {
        int ret = 0, r;
 
        assert(link);
        assert(dropped_ipv6ll);
 
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
                return 0;
 
        log_link_info(link, "Lost IPv6LL address %s%s.",
                      IN6_ADDR_TO_STRING(dropped_ipv6ll),
                      has_replacement ? ", switching to alternate IPv6LL source" : "");
 
        r = sd_dhcp6_client_stop(link->dhcp6_client);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"));
 
        /* DHCPv6 must be restarted to switch the client's source address, while NDisc and
         * RADV can switch to the replacement IPv6LL in link_ipv6ll_gained() without flushing
         * learned state. Keep link->ndisc_configured as-is in this path. */
        if (has_replacement)
                return ret;
 
        r = ndisc_stop(link);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Discovery: %m"));
        link->ndisc_configured = false;
        ndisc_flush(link);
 
        r = sd_radv_stop(link->radv);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not stop IPv6 Router Advertisement: %m"));
 
        r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_IPV6LL);
        if (r < 0)
                RET_GATHER(ret, log_link_warning_errno(link, r, "Could not reconfigure stacked netdevs after IPv6LL loss: %m"));
 
        return ret;
}
 
int link_handle_bound_to_list(Link *link) {
        bool required_up = false;
        Link *l;
 
        assert(link);
 
        /* If at least one interface in bound_to_links has carrier, then make this interface up.
         * If all interfaces in bound_to_links do not, then make this interface down. */
 
        if (hashmap_isempty(link->bound_to_links))
                return 0;
 
        HASHMAP_FOREACH(l, link->bound_to_links)
                if (link_has_carrier(l)) {
                        required_up = true;
                        break;
                }
 
        if (!required_up && link_is_up(link))
                return link_request_to_bring_up_or_down(link, /* up= */ false);
        if (required_up && !link_is_up(link))
                return link_request_to_bring_up_or_down(link, /* up= */ true);
 
        return 0;
}
 
static int link_handle_bound_by_list(Link *link) {
        Link *l;
        int r;
 
        assert(link);
 
        /* Update up or down state of interfaces which depend on this interface's carrier state. */
 
        HASHMAP_FOREACH(l, link->bound_by_links) {
                r = link_handle_bound_to_list(l);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
        int r;
 
        assert(link);
        assert(carrier);
        assert(h);
 
        if (link == carrier)
                return 0;
 
        if (hashmap_contains(*h, INT_TO_PTR(carrier->ifindex)))
                return 0;
 
        r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
        if (r < 0)
                return r;
 
        link_dirty(link);
 
        return 0;
}
 
static int link_new_bound_by_list(Link *link) {
        Manager *m;
        Link *carrier;
        int r;
 
        assert(link);
        assert(link->manager);
 
        m = link->manager;
 
        HASHMAP_FOREACH(carrier, m->links_by_index) {
                if (!carrier->network)
                        continue;
 
                if (strv_isempty(carrier->network->bind_carrier))
                        continue;
 
                if (strv_fnmatch(carrier->network->bind_carrier, link->ifname)) {
                        r = link_put_carrier(link, carrier, &link->bound_by_links);
                        if (r < 0)
                                return r;
                }
        }
 
        HASHMAP_FOREACH(carrier, link->bound_by_links) {
                r = link_put_carrier(carrier, link, &carrier->bound_to_links);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int link_new_bound_to_list(Link *link) {
        Manager *m;
        Link *carrier;
        int r;
 
        assert(link);
        assert(link->manager);
 
        if (!link->network)
                return 0;
 
        if (strv_isempty(link->network->bind_carrier))
                return 0;
 
        m = link->manager;
 
        HASHMAP_FOREACH(carrier, m->links_by_index) {
                if (strv_fnmatch(link->network->bind_carrier, carrier->ifname)) {
                        r = link_put_carrier(link, carrier, &link->bound_to_links);
                        if (r < 0)
                                return r;
                }
        }
 
        HASHMAP_FOREACH(carrier, link->bound_to_links) {
                r = link_put_carrier(carrier, link, &carrier->bound_by_links);
                if (r < 0)
                        return r;
        }
 
        return 0;
}