HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-address.c

93,831 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 135c3a04a54923540a2b9413509b31656798e500

Lines 1–1000 of 2,534

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <net/if.h>
#include <stdio.h>
 
#include "sd-event.h"
#include "sd-netlink.h"
 
#include "af-list.h"
#include "alloc-util.h"
#include "bitfield.h"
#include "conf-parser.h"
#include "errno-util.h"
#include "firewall-util.h"
#include "in-addr-prefix-util.h"
#include "logarithm.h"
#include "netlink-util.h"
#include "networkd-address-generation.h"
#include "networkd-address.h"
#include "networkd-address-pool.h"
#include "networkd-dhcp-prefix-delegation.h"
#include "networkd-dhcp-relay.h"
#include "networkd-dhcp-server.h"
#include "networkd-ipv4acd.h"
#include "networkd-link.h"
#include "networkd-manager.h"
#include "networkd-ndisc.h"
#include "networkd-netlabel.h"
#include "networkd-network.h"
#include "networkd-queue.h"
#include "networkd-route.h"
#include "networkd-route-util.h"
#include "ordered-set.h"
#include "parse-util.h"
#include "set.h"
#include "siphash24.h"
#include "socket-util.h"
#include "string-util.h"
#include "strv.h"
 
#define ADDRESSES_PER_LINK_MAX 16384U
#define STATIC_ADDRESSES_PER_NETWORK_MAX 8192U
 
#define KNOWN_FLAGS                             \
        (IFA_F_SECONDARY |                      \
         IFA_F_NODAD |                          \
         IFA_F_OPTIMISTIC |                     \
         IFA_F_DADFAILED |                      \
         IFA_F_HOMEADDRESS |                    \
         IFA_F_DEPRECATED |                     \
         IFA_F_TENTATIVE |                      \
         IFA_F_PERMANENT |                      \
         IFA_F_MANAGETEMPADDR |                 \
         IFA_F_NOPREFIXROUTE |                  \
         IFA_F_MCAUTOJOIN |                     \
         IFA_F_STABLE_PRIVACY)
 
/* From net/ipv4/devinet.c */
#define IPV6ONLY_FLAGS                          \
        (IFA_F_NODAD |                          \
         IFA_F_OPTIMISTIC |                     \
         IFA_F_DADFAILED |                      \
         IFA_F_HOMEADDRESS |                    \
         IFA_F_TENTATIVE |                      \
         IFA_F_MANAGETEMPADDR |                 \
         IFA_F_STABLE_PRIVACY)
 
/* We do not control the following flags. */
#define UNMANAGED_FLAGS                         \
        (IFA_F_SECONDARY |                      \
         IFA_F_DADFAILED |                      \
         IFA_F_DEPRECATED |                     \
         IFA_F_TENTATIVE |                      \
         IFA_F_PERMANENT |                      \
         IFA_F_STABLE_PRIVACY)
 
int address_flags_to_string_alloc(uint32_t flags, int family, char **ret) {
        _cleanup_free_ char *str = NULL;
        static const char* map[] = {
                [LOG2U(IFA_F_SECONDARY)]      = "secondary", /* This is also called "temporary" for ipv6. */
                [LOG2U(IFA_F_NODAD)]          = "nodad",
                [LOG2U(IFA_F_OPTIMISTIC)]     = "optimistic",
                [LOG2U(IFA_F_DADFAILED)]      = "dadfailed",
                [LOG2U(IFA_F_HOMEADDRESS)]    = "home-address",
                [LOG2U(IFA_F_DEPRECATED)]     = "deprecated",
                [LOG2U(IFA_F_TENTATIVE)]      = "tentative",
                [LOG2U(IFA_F_PERMANENT)]      = "permanent",
                [LOG2U(IFA_F_MANAGETEMPADDR)] = "manage-temporary-address",
                [LOG2U(IFA_F_NOPREFIXROUTE)]  = "no-prefixroute",
                [LOG2U(IFA_F_MCAUTOJOIN)]     = "auto-join",
                [LOG2U(IFA_F_STABLE_PRIVACY)] = "stable-privacy",
        };
 
        assert(IN_SET(family, AF_INET, AF_INET6));
        assert(ret);
 
        for (size_t i = 0; i < ELEMENTSOF(map); i++)
                if (BIT_SET(flags, i) && map[i])
                        if (!strextend_with_separator(
                                            &str, ",",
                                            family == AF_INET6 && (1 << i) == IFA_F_SECONDARY ? "temporary" : map[i]))
                                return -ENOMEM;
 
        *ret = TAKE_PTR(str);
        return 0;
}
 
static LinkAddressState address_state_from_scope(uint8_t scope) {
        if (scope < RT_SCOPE_SITE)
                /* universally accessible addresses found */
                return LINK_ADDRESS_STATE_ROUTABLE;
 
        if (scope < RT_SCOPE_HOST)
                /* only link or site local addresses found */
                return LINK_ADDRESS_STATE_DEGRADED;
 
        /* no useful addresses found */
        return LINK_ADDRESS_STATE_OFF;
}
 
void link_get_address_states(
                Link *link,
                LinkAddressState *ret_ipv4,
                LinkAddressState *ret_ipv6,
                LinkAddressState *ret_all) {
 
        uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE;
        Address *address;
 
        assert(link);
 
        SET_FOREACH(address, link->addresses) {
                if (!address_is_ready(address))
                        continue;
 
                if (address->family == AF_INET)
                        ipv4_scope = MIN(ipv4_scope, address->scope);
 
                if (address->family == AF_INET6)
                        ipv6_scope = MIN(ipv6_scope, address->scope);
        }
 
        if (ret_ipv4)
                *ret_ipv4 = address_state_from_scope(ipv4_scope);
        if (ret_ipv6)
                *ret_ipv6 = address_state_from_scope(ipv6_scope);
        if (ret_all)
                *ret_all = address_state_from_scope(MIN(ipv4_scope, ipv6_scope));
}
 
static void address_detach(Address *address);
 
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
        address_hash_ops_detach,
        Address,
        address_hash_func,
        address_compare_func,
        address_detach);
 
DEFINE_HASH_OPS(
        address_hash_ops,
        Address,
        address_hash_func,
        address_compare_func);
 
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
        address_section_hash_ops,
        ConfigSection,
        config_section_hash_func,
        config_section_compare_func,
        Address,
        address_detach);
 
int address_new(Address **ret) {
        _cleanup_(address_unrefp) Address *address = NULL;
 
        assert(ret);
 
        address = new(Address, 1);
        if (!address)
                return -ENOMEM;
 
        *address = (Address) {
                .n_ref = 1,
                .family = AF_UNSPEC,
                .scope = RT_SCOPE_UNIVERSE,
                .lifetime_valid_usec = USEC_INFINITY,
                .lifetime_preferred_usec = USEC_INFINITY,
                .set_broadcast = -1,
        };
 
        *ret = TAKE_PTR(address);
 
        return 0;
}
 
int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
        _cleanup_(config_section_freep) ConfigSection *n = NULL;
        _cleanup_(address_unrefp) Address *address = NULL;
        int r;
 
        assert(network);
        assert(ret);
        assert(filename);
        assert(section_line > 0);
 
        r = config_section_new(filename, section_line, &n);
        if (r < 0)
                return r;
 
        address = ordered_hashmap_get(network->addresses_by_section, n);
        if (address) {
                *ret = TAKE_PTR(address);
                return 0;
        }
 
        if (ordered_hashmap_size(network->addresses_by_section) >= STATIC_ADDRESSES_PER_NETWORK_MAX)
                return -E2BIG;
 
        r = address_new(&address);
        if (r < 0)
                return r;
 
        address->network = network;
        address->section = TAKE_PTR(n);
        address->source = NETWORK_CONFIG_SOURCE_STATIC;
        /* This will be adjusted in address_section_verify(). */
        address->duplicate_address_detection = _ADDRESS_FAMILY_INVALID;
 
        r = ordered_hashmap_ensure_put(&network->addresses_by_section, &address_section_hash_ops, address->section, address);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(address);
        return 0;
}
 
static Address* address_detach_impl(Address *address) {
        assert(address);
        assert(!address->link || !address->network);
 
        if (address->network) {
                assert(address->section);
                ordered_hashmap_remove(address->network->addresses_by_section, address->section);
 
                if (address->network->dhcp_server_address == address)
                        address->network->dhcp_server_address = NULL;
 
                if (address->network->dhcp_relay_agent_address == address)
                        address->network->dhcp_relay_agent_address = NULL;
 
                address->network = NULL;
                return address;
        }
 
        if (address->link) {
                set_remove(address->link->addresses, address);
 
                address->link = NULL;
                return address;
        }
 
        return NULL;
}
 
static void address_detach(Address *address) {
        assert(address);
 
        address_unref(address_detach_impl(address));
}
 
static Address* address_free(Address *address) {
        if (!address)
                return NULL;
 
        address_detach_impl(address);
 
        config_section_free(address->section);
        free(address->label);
        free(address->netlabel);
        ipv6_token_unref(address->token);
        nft_set_context_clear(&address->nft_set_context);
        return mfree(address);
}
 
DEFINE_TRIVIAL_REF_UNREF_FUNC(Address, address, address_free);
 
static bool address_lifetime_is_valid(const Address *a) {
        assert(a);
 
        return
                a->lifetime_valid_usec == USEC_INFINITY ||
                a->lifetime_valid_usec > now(CLOCK_BOOTTIME);
}
 
bool address_is_ready(const Address *a) {
        assert(a);
        assert(a->link);
 
        if (!ipv4acd_bound(a->link, a))
                return false;
 
        if (FLAGS_SET(a->flags, IFA_F_TENTATIVE))
                return false;
 
        if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_REMOVING))
                return false;
 
        if (!FLAGS_SET(a->state, NETWORK_CONFIG_STATE_CONFIGURED))
                return false;
 
        return address_lifetime_is_valid(a);
}
 
bool link_check_addresses_ready(Link *link, NetworkConfigSource source) {
        Address *a;
        bool has = false;
 
        assert(link);
 
        /* Check if all addresses on the interface are ready. If there is no address, this will return false. */
 
        SET_FOREACH(a, link->addresses) {
                if (source >= 0 && a->source != source)
                        continue;
                if (address_is_marked(a))
                        continue;
                if (!address_exists(a))
                        continue;
                if (!address_is_ready(a))
                        return false;
                has = true;
        }
 
        if (has || source != NETWORK_CONFIG_SOURCE_DHCP6)
                return has;
 
        /* If there is no DHCPv6 addresses, but all conflicting addresses are successfully configured, then
         * let's handle the DHCPv6 client successfully configured addresses. Otherwise, if the conflicted
         * addresses are static or foreign, and there is no other dynamic addressing protocol enabled, then
         * the link will never enter the configured state. See also link_check_ready(). */
        SET_FOREACH(a, link->addresses) {
                if (source >= 0 && a->source == NETWORK_CONFIG_SOURCE_DHCP6)
                        continue;
                if (!a->also_requested_by_dhcp6)
                        continue;
                if (address_is_marked(a))
                        continue;
                if (!address_exists(a))
                        continue;
                if (!address_is_ready(a))
                        return false;
                has = true;
        }
 
        return has;
}
 
void link_mark_addresses(Link *link, NetworkConfigSource source) {
        Address *a;
 
        assert(link);
 
        SET_FOREACH(a, link->addresses) {
                if (a->source != source)
                        continue;
 
                address_mark(a);
        }
}
 
static int address_get_broadcast(const Address *a, Link *link, struct in_addr *ret) {
        struct in_addr b_addr = {};
 
        assert(a);
        assert(link);
 
        /* Returns 0 when broadcast address is null, 1 when non-null broadcast address, -EAGAIN when the main
         * address is null. */
 
        /* broadcast is only for IPv4. */
        if (a->family != AF_INET)
                goto finalize;
 
        /* broadcast address cannot be used when peer address is specified. */
        if (in4_addr_is_set(&a->in_addr_peer.in))
                goto finalize;
 
        /* A /31 or /32 IPv4 address does not have a broadcast address.
         * See https://tools.ietf.org/html/rfc3021 */
        if (a->prefixlen > 30)
                goto finalize;
 
        /* If explicitly configured, use the address as is. */
        if (in4_addr_is_set(&a->broadcast)) {
                b_addr = a->broadcast;
                goto finalize;
        }
 
        /* If explicitly disabled, then return null address. */
        if (a->set_broadcast == 0)
                goto finalize;
 
        /* For wireguard interfaces, broadcast is disabled by default. */
        if (a->set_broadcast < 0 && streq_ptr(link->kind, "wireguard"))
                goto finalize;
 
        /* If the main address is null, e.g. Address=0.0.0.0/24, the broadcast address will be automatically
         * determined after an address is acquired. */
        if (!in4_addr_is_set(&a->in_addr.in))
                return -EAGAIN;
 
        /* Otherwise, generate a broadcast address from the main address and prefix length. */
        b_addr.s_addr = a->in_addr.in.s_addr | htobe32(UINT32_C(0xffffffff) >> a->prefixlen);
 
finalize:
        if (ret)
                *ret = b_addr;
 
        return in4_addr_is_set(&b_addr);
}
 
static void address_set_broadcast(Address *a, Link *link) {
        assert(a);
        assert_se(address_get_broadcast(a, link, &a->broadcast) >= 0);
}
 
static void address_set_cinfo(Manager *m, const Address *a, struct ifa_cacheinfo *cinfo) {
        usec_t now_usec;
 
        assert(m);
        assert(a);
        assert(cinfo);
 
        assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0);
 
        *cinfo = (struct ifa_cacheinfo) {
                .ifa_valid = usec_to_sec(a->lifetime_valid_usec, now_usec),
                .ifa_prefered = usec_to_sec(a->lifetime_preferred_usec, now_usec),
        };
}
 
static void address_set_lifetime(Manager *m, Address *a, const struct ifa_cacheinfo *cinfo) {
        usec_t now_usec;
 
        assert(m);
        assert(a);
        assert(cinfo);
 
        assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0);
 
        a->lifetime_valid_usec = sec_to_usec(cinfo->ifa_valid, now_usec);
        a->lifetime_preferred_usec = sec_to_usec(cinfo->ifa_prefered, now_usec);
}
 
static bool address_is_static_null(const Address *address) {
        assert(address);
 
        if (!address->network)
                return false;
 
        if (!address->requested_as_null)
                return false;
 
        assert(!in_addr_is_set(address->family, &address->in_addr));
        return true;
}
 
static int address_ipv4_prefix(const Address *a, struct in_addr *ret) {
        struct in_addr p;
        int r;
 
        assert(a);
        assert(a->family == AF_INET);
        assert(ret);
 
        p = in4_addr_is_set(&a->in_addr_peer.in) ? a->in_addr_peer.in : a->in_addr.in;
        r = in4_addr_mask(&p, a->prefixlen);
        if (r < 0)
                return r;
 
        *ret = p;
        return 0;
}
 
void address_hash_func(const Address *a, struct siphash *state) {
        assert(a);
 
        siphash24_compress_typesafe(a->family, state);
 
        switch (a->family) {
        case AF_INET: {
                struct in_addr prefix;
 
                siphash24_compress_typesafe(a->prefixlen, state);
 
                assert_se(address_ipv4_prefix(a, &prefix) >= 0);
                siphash24_compress_typesafe(prefix, state);
 
                siphash24_compress_typesafe(a->in_addr.in, state);
                break;
        }
        case AF_INET6:
                siphash24_compress_typesafe(a->in_addr.in6, state);
 
                if (in6_addr_is_null(&a->in_addr.in6))
                        siphash24_compress_typesafe(a->prefixlen, state);
                break;
 
        default:
                assert_not_reached();
        }
}
 
int address_compare_func(const Address *a1, const Address *a2) {
        int r;
 
        r = CMP(a1->family, a2->family);
        if (r != 0)
                return r;
 
        switch (a1->family) {
        case AF_INET: {
                struct in_addr p1, p2;
 
                /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */
                r = CMP(a1->prefixlen, a2->prefixlen);
                if (r != 0)
                        return r;
 
                assert_se(address_ipv4_prefix(a1, &p1) >= 0);
                assert_se(address_ipv4_prefix(a2, &p2) >= 0);
                r = memcmp(&p1, &p2, sizeof(p1));
                if (r != 0)
                        return r;
 
                return memcmp(&a1->in_addr.in, &a2->in_addr.in, sizeof(a1->in_addr.in));
        }
        case AF_INET6:
                /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */
                r = memcmp(&a1->in_addr.in6, &a2->in_addr.in6, sizeof(a1->in_addr.in6));
                if (r != 0)
                        return r;
 
                /* To distinguish IPv6 null addresses with different prefixlen, e.g. ::48 vs ::64, let's
                 * compare the prefix length. */
                if (in6_addr_is_null(&a1->in_addr.in6))
                        r = CMP(a1->prefixlen, a2->prefixlen);
 
                return r;
 
        default:
                assert_not_reached();
        }
}
 
bool address_can_update(const Address *existing, const Address *requesting) {
        assert(existing);
        assert(requesting);
 
        /*
         * property     |    IPv4     |  IPv6
         * -----------------------------------------
         * family       |      ✗      |     ✗
         * prefixlen    |      ✗      |     ✗
         * address      |      ✗      |     ✗
         * scope        |      ✗      |     -
         * label        |      ✗      |     -
         * broadcast    |      ✗      |     -
         * peer         |      ✗      |     ✓
         * flags        |      ✗      |     ✓
         * lifetime     |      ✓      |     ✓
         * route metric |      ✓      |     ✓
         * protocol     |      ✓      |     ✓
         *
         * ✗ : cannot be changed
         * ✓ : can be changed
         * - : unused
         *
         * IPv4 : See inet_rtm_newaddr() in net/ipv4/devinet.c.
         * IPv6 : See inet6_addr_modify() in net/ipv6/addrconf.c.
         */
 
        if (existing->family != requesting->family)
                return false;
 
        if (existing->prefixlen != requesting->prefixlen)
                return false;
 
        /* When a null address is requested, the address to be assigned/updated will be determined later. */
        if (!address_is_static_null(requesting) &&
            in_addr_equal(existing->family, &existing->in_addr, &requesting->in_addr) <= 0)
                return false;
 
        switch (existing->family) {
        case AF_INET: {
                struct in_addr bcast;
 
                if (existing->scope != requesting->scope)
                        return false;
                if (((existing->flags ^ requesting->flags) & KNOWN_FLAGS & ~IPV6ONLY_FLAGS & ~UNMANAGED_FLAGS) != 0)
                        return false;
                if (!streq_ptr(existing->label, requesting->label))
                        return false;
                if (!in4_addr_equal(&existing->in_addr_peer.in, &requesting->in_addr_peer.in))
                        return false;
                if (existing->link && address_get_broadcast(requesting, existing->link, &bcast) >= 0) {
                        /* If the broadcast address can be determined now, check if they match. */
                        if (!in4_addr_equal(&existing->broadcast, &bcast))
                                return false;
                } else {
                        /* When a null address is requested, then the broadcast address will be
                         * automatically calculated from the acquired address, e.g.
                         *     192.168.0.10/24 -> 192.168.0.255
                         * So, here let's only check if the broadcast is the last address in the range, e.g.
                         *     0.0.0.0/24 -> 0.0.0.255 */
                        if (!FLAGS_SET(existing->broadcast.s_addr, htobe32(UINT32_C(0xffffffff) >> existing->prefixlen)))
                                return false;
                }
                break;
        }
        case AF_INET6:
                break;
 
        default:
                assert_not_reached();
        }
 
        return true;
}
 
int address_dup(const Address *src, Address **ret) {
        _cleanup_(address_unrefp) Address *dest = NULL;
        int r;
 
        assert(src);
        assert(ret);
 
        dest = newdup(Address, src, 1);
        if (!dest)
                return -ENOMEM;
 
        /* clear the reference counter and all pointers */
        dest->n_ref = 1;
        dest->network = NULL;
        dest->section = NULL;
        dest->link = NULL;
        dest->label = NULL;
        dest->token = ipv6_token_ref(src->token);
        dest->netlabel = NULL;
        dest->nft_set_context.sets = NULL;
        dest->nft_set_context.n_sets = 0;
 
        if (src->family == AF_INET) {
                r = strdup_to(&dest->label, src->label);
                if (r < 0)
                        return r;
        }
 
        r = strdup_to(&dest->netlabel, src->netlabel);
        if (r < 0)
                return r;
 
        r = nft_set_context_dup(&src->nft_set_context, &dest->nft_set_context);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(dest);
        return 0;
}
 
static int address_set_masquerade(Address *address, bool add) {
        union in_addr_union masked;
        int r;
 
        assert(address);
        assert(address->link);
        assert(address->link->manager);
 
        if (!address->link->manager->nfnl)
                return 0;
 
        if (!address->link->network)
                return 0;
 
        if (!FLAGS_SET(address->link->network->ip_masquerade, AF_TO_ADDRESS_FAMILY(address->family)))
                return 0;
 
        if (address->scope >= RT_SCOPE_LINK)
                return 0;
 
        if (address->ip_masquerade_done == add)
                return 0;
 
        masked = address->in_addr;
        r = in_addr_mask(address->family, &masked, address->prefixlen);
        if (r < 0)
                return r;
 
        r = fw_nftables_add_masquerade(address->link->manager->nfnl, add, address->family, &masked, address->prefixlen);
        if (r < 0)
                return r;
 
        address->ip_masquerade_done = add;
 
        return 0;
}
 
static void address_modify_nft_set_context(Address *address, bool add, NFTSetContext *nft_set_context) {
        int r;
 
        assert(address);
        assert(address->link);
        assert(address->link->manager);
        assert(address->link->manager->nfnl);
        assert(nft_set_context);
 
        FOREACH_ARRAY(nft_set, nft_set_context->sets, nft_set_context->n_sets) {
                uint32_t ifindex;
 
                assert(nft_set);
 
                switch (nft_set->source) {
                case NFT_SET_SOURCE_ADDRESS:
                        r = nft_set_element_modify_ip(address->link->manager->nfnl, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set,
                                                      &address->in_addr);
                        break;
                case NFT_SET_SOURCE_PREFIX:
                        r = nft_set_element_modify_iprange(address->link->manager->nfnl, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set,
                                                           &address->in_addr, address->prefixlen);
                        break;
                case NFT_SET_SOURCE_IFINDEX:
                        ifindex = address->link->ifindex;
                        r = nft_set_element_modify_any(address->link->manager->nfnl, add, nft_set->nfproto, nft_set->table, nft_set->set,
                                                       &ifindex, sizeof(ifindex));
                        break;
                default:
                        assert_not_reached();
                }
 
                if (r < 0)
                        log_warning_errno(r, "Failed to %s NFT set entry: family %s, table %s, set %s, IP address %s, ignoring: %m",
                                          add ? "add" : "delete",
                                          nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set,
                                          IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
                else
                        log_debug("%s NFT set entry: family %s, table %s, set %s, IP address %s",
                                  add ? "Added" : "Deleted",
                                  nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set,
                                  IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen));
        }
}
 
static void address_modify_nft_set(Address *address, bool add) {
        assert(address);
        assert(address->link);
        assert(address->link->manager);
 
        if (!address->link->manager->nfnl)
                return;
 
        if (!IN_SET(address->family, AF_INET, AF_INET6))
                return;
 
        if (!address->link->network)
                return;
 
        switch (address->source) {
        case NETWORK_CONFIG_SOURCE_DHCP4:
                return address_modify_nft_set_context(address, add, &address->link->network->dhcp_nft_set_context);
        case NETWORK_CONFIG_SOURCE_DHCP6:
                return address_modify_nft_set_context(address, add, &address->link->network->dhcp6_nft_set_context);
        case NETWORK_CONFIG_SOURCE_DHCP_PD:
                return address_modify_nft_set_context(address, add, &address->link->network->dhcp_pd_nft_set_context);
        case NETWORK_CONFIG_SOURCE_NDISC:
                return address_modify_nft_set_context(address, add, &address->link->network->ndisc_nft_set_context);
        case NETWORK_CONFIG_SOURCE_STATIC:
                return address_modify_nft_set_context(address, add, &address->nft_set_context);
        default:
                return;
        }
}
 
static int address_attach(Link *link, Address *address) {
        int r;
 
        assert(link);
        assert(address);
        assert(!address->link);
 
        r = set_ensure_put(&link->addresses, &address_hash_ops_detach, address);
        if (r < 0)
                return r;
        if (r == 0)
                return -EEXIST;
 
        address->link = link;
        address_ref(address);
        return 0;
}
 
static int address_update(Address *address) {
        Link *link = ASSERT_PTR(ASSERT_PTR(address)->link);
        int r;
 
        if (address_is_ready(address) &&
            address->family == AF_INET6 &&
            in6_addr_is_link_local(&address->in_addr.in6) &&
            in6_addr_is_null(&link->ipv6ll_address)) {
 
                link->ipv6ll_address = address->in_addr.in6;
 
                r = link_ipv6ll_gained(link);
                if (r < 0) {
                        link->ipv6ll_address = (const struct in6_addr) {};
                        return r;
                }
        }
 
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return 0;
 
        r = address_set_masquerade(address, /* add= */ true);
        if (r < 0)
                return log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
 
        address_add_netlabel(address);
 
        address_modify_nft_set(address, /* add= */ true);
 
        if (address_is_ready(address) && address->callback) {
                r = address->callback(address);
                if (r < 0)
                        return r;
        }
 
        link_update_operstate(link, /* also_update_master= */ true);
        link_check_ready(link);
        return 0;
}
 
static int address_removed_maybe_kernel_dad(Link *link, Address *address) {
        int r;
 
        assert(link);
        assert(address);
 
        if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
                return 0;
 
        if (address->family != AF_INET6)
                return 0;
 
        if (!FLAGS_SET(address->flags, IFA_F_TENTATIVE))
                return 0;
 
        log_link_info(link, "Address %s with tentative flag is removed, maybe a duplicated address is assigned on another node or link?",
                      IN6_ADDR_TO_STRING(&address->in_addr.in6));
 
        /* Reset the address state, as the object may be reused in the below. */
        address->state = 0;
 
        switch (address->source) {
        case NETWORK_CONFIG_SOURCE_STATIC:
                r = link_reconfigure_radv_address(address, link);
                break;
        case NETWORK_CONFIG_SOURCE_DHCP_PD:
                r = dhcp_pd_reconfigure_address(address, link);
                break;
        case NETWORK_CONFIG_SOURCE_NDISC:
                r = ndisc_reconfigure_address(address, link);
                break;
        default:
                r = 0;
        }
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to configure an alternative address: %m");
 
        return 0;
}
 
static int address_drop(Address *in, bool removed_by_us) {
        _cleanup_(address_unrefp) Address *address = address_ref(ASSERT_PTR(in));
        Link *link = ASSERT_PTR(address->link);
        int r;
 
        r = address_set_masquerade(address, /* add= */ false);
        if (r < 0)
                log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m");
 
        address_modify_nft_set(address, /* add= */ false);
 
        address_del_netlabel(address);
 
        if (address->family == AF_INET6 &&
            in6_addr_equal(&address->in_addr.in6, &link->ipv6ll_address)) {
                link->ipv6ll_address = (const struct in6_addr) {};
 
                Address *a;
                bool has_replacement;
 
                /* If another ready IPv6LL address exists on this link, use it instead. */
                SET_FOREACH(a, link->addresses) {
                        if (a == address)
                                continue;
                        if (a->family != AF_INET6)
                                continue;
                        if (!in6_addr_is_link_local(&a->in_addr.in6))
                                continue;
                        if (!address_is_ready(a))
                                continue;
                        link->ipv6ll_address = a->in_addr.in6;
                        break;
                }
 
                has_replacement = in6_addr_is_set(&link->ipv6ll_address);
 
                /* Stop engines bound to the dropped IPv6LL source address. Do not return early on error.
                 * address_detach() and link_update_operstate() must run to keep link state consistent. */
                r = link_ipv6ll_lost(link, &address->in_addr.in6, has_replacement);
                if (r < 0)
                        log_link_warning_errno(link, r, "Failed to stop IPv6 services after IPv6LL loss, ignoring: %m");
 
                /* If another IPv6LL address is available, restart engines with it. */
                if (has_replacement) {
                        r = link_ipv6ll_gained(link);
                        if (r < 0)
                                log_link_warning_errno(link, r, "Failed to restart IPv6 services with alternate IPv6LL address, ignoring: %m");
                }
        }
 
        ipv4acd_detach(link, address);
 
        (void) link_dhcp_relay_address_dropped(link, address);
 
        address_detach(address);
 
        if (!removed_by_us) {
                r = address_removed_maybe_kernel_dad(link, address);
                if (r < 0) {
                        link_enter_failed(link);
                        return r;
                }
        }
 
        link_update_operstate(link, /* also_update_master= */ true);
        link_check_ready(link);
        return 0;
}
 
static bool address_match_null(const Address *a, const Address *null_address) {
        assert(a);
        assert(null_address);
 
        if (!a->requested_as_null)
                return false;
 
        /* Currently, null address is supported only by static addresses. Note that static
         * address may be set as foreign during reconfiguring the interface. */
        if (!IN_SET(a->source, NETWORK_CONFIG_SOURCE_FOREIGN, NETWORK_CONFIG_SOURCE_STATIC))
                return false;
 
        if (a->family != null_address->family)
                return false;
 
        if (a->prefixlen != null_address->prefixlen)
                return false;
 
        return true;
}
 
static int address_get_request(Link *link, const Address *address, Request **ret) {
        Request *req;
 
        assert(link);
        assert(link->manager);
        assert(address);
 
        req = ordered_set_get(
                        link->manager->request_queue,
                        &(Request) {
                                .link = link,
                                .type = REQUEST_TYPE_ADDRESS,
                                .userdata = (void*) address,
                                .hash_func = (hash_func_t) address_hash_func,
                                .compare_func = (compare_func_t) address_compare_func,
                        });
        if (req) {
                if (ret)
                        *ret = req;
                return 0;
        }
 
        if (address_is_static_null(address))
                ORDERED_SET_FOREACH(req, link->manager->request_queue) {
                        if (req->link != link)
                                continue;
                        if (req->type != REQUEST_TYPE_ADDRESS)
                                continue;