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 2001–2534 of 2,534

 
        } else {
                /* Otherwise, update the managed Address object with the netlink notification. */
                address->prefixlen = tmp->prefixlen;
                address->in_addr_peer = tmp->in_addr_peer;
        }
 
        /* Also update information that cannot be obtained through netlink notification. */
        Request *req = NULL;
        (void) address_get_request(link, tmp, &req);
        if (req && req->waiting_reply) {
                Address *a = ASSERT_PTR(req->userdata);
 
                address->source = a->source;
                address->provider = a->provider;
                (void) free_and_strdup_warn(&address->netlabel, a->netlabel);
                nft_set_context_clear(&address->nft_set_context);
                (void) nft_set_context_dup(&a->nft_set_context, &address->nft_set_context);
                address->requested_as_null = a->requested_as_null;
                address->also_requested_by_dhcp6 = a->also_requested_by_dhcp6;
                address->callback = a->callback;
 
                ipv6_token_ref(a->token);
                ipv6_token_unref(address->token);
                address->token = a->token;
        }
 
        /* Then, update miscellaneous info. */
        r = sd_rtnl_message_addr_get_scope(message, &address->scope);
        if (r < 0)
                log_link_debug_errno(link, r, "rtnl: received address message without scope, ignoring: %m");
 
        if (address->family == AF_INET) {
                _cleanup_free_ char *label = NULL;
 
                r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &label);
                if (r >= 0) {
                        if (!streq_ptr(label, link->ifname))
                                free_and_replace(address->label, label);
                } else if (r != -ENODATA)
                        log_link_debug_errno(link, r, "rtnl: could not get label from address message, ignoring: %m");
 
                r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &address->broadcast);
                if (r < 0 && r != -ENODATA)
                        log_link_debug_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m");
        }
 
        r = sd_netlink_message_read_u32(message, IFA_FLAGS, &address->flags);
        if (r < 0)
                log_link_debug_errno(link, r, "rtnl: failed to read IFA_FLAGS attribute, ignoring: %m");
 
        struct ifa_cacheinfo cinfo;
        r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
        if (r >= 0)
                address_set_lifetime(m, address, &cinfo);
        else if (r != -ENODATA)
                log_link_debug_errno(link, r, "rtnl: failed to read IFA_CACHEINFO attribute, ignoring: %m");
 
        r = sd_netlink_message_read_u32(message, IFA_RT_PRIORITY, &address->route_metric);
        if (r < 0 && r != -ENODATA)
                log_link_debug_errno(link, r, "rtnl: failed to read IFA_RT_PRIORITY attribute, ignoring: %m");
 
        address_enter_configured(address);
        if (req)
                address_enter_configured(req->userdata);
 
        log_address_debug(address, is_new ? "Received new": "Received updated", link);
 
        /* address_update() logs internally, so we don't need to here. */
        r = address_update(address);
        if (r < 0)
                link_enter_failed(link);
 
finalize:
        if (tmp->family == AF_INET6) {
                r = dhcp4_update_ipv6_connectivity(link);
                if (r < 0) {
                        log_link_warning_errno(link, r, "Failed to notify IPv6 connectivity to DHCPv4 client: %m");
                        link_enter_failed(link);
                }
        }
 
        return 1;
}
 
static int config_parse_broadcast(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Address *address = ASSERT_PTR(userdata);
        int r;
 
        /* Do not check or set address->family here. It will be checked later in
         * address_section_verify() -> address_section_adjust_broadcast() . */
 
        if (isempty(rvalue)) {
                /* The broadcast address will be calculated based on Address=, and set if the link is
                 * not a wireguard interface. */
                address->broadcast = (struct in_addr) {};
                address->set_broadcast = -1;
                return 1;
        }
 
        r = parse_boolean(rvalue);
        if (r >= 0) {
                /* The broadcast address will be calculated based on Address=. */
                address->broadcast = (struct in_addr) {};
                address->set_broadcast = r;
                return 1;
        }
 
        r = config_parse_in_addr_non_null(
                        unit, filename, line, section, section_line,
                        lvalue, /* ltype= */ AF_INET, rvalue,
                        &address->broadcast, /* userdata= */ NULL);
        if (r <= 0)
                return r;
 
        address->set_broadcast = true;
        return 1;
}
 
static int config_parse_address(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Address *address = ASSERT_PTR(userdata);
        union in_addr_union *a = ASSERT_PTR(data);
        struct in_addr_prefix prefix;
        int r;
 
        assert(rvalue);
 
        if (isempty(rvalue)) {
                /* When an empty string is assigned, clear both Address= and Peer=. */
                address->family = AF_UNSPEC;
                address->prefixlen = 0;
                address->in_addr = IN_ADDR_NULL;
                address->in_addr_peer = IN_ADDR_NULL;
                return 1;
        }
 
        r = config_parse_in_addr_prefix(unit, filename, line, section, section_line, lvalue, /* ltype= */ true, rvalue, &prefix, /* userdata= */ NULL);
        if (r <= 0)
                return r;
 
        if (address->family != AF_UNSPEC && prefix.family != address->family) {
                log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
                return 0;
        }
 
        address->family = prefix.family;
        address->prefixlen = prefix.prefixlen;
        *a = prefix.address;
        return 1;
}
 
static int config_parse_address_label(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        char **label = ASSERT_PTR(data);
        int r;
 
        if (!isempty(rvalue) && !address_label_valid(rvalue)) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
                return 0;
        }
 
        r = free_and_strdup_warn(label, empty_to_null(rvalue));
        if (r < 0)
                return r;
 
        return 1;
}
 
static int config_parse_address_lifetime(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        usec_t *usec = ASSERT_PTR(data);
 
        /* We accept only "forever", "infinity", empty, or "0". */
        if (isempty(rvalue) || STR_IN_SET(rvalue, "forever", "infinity"))
                *usec = USEC_INFINITY;
        else if (streq(rvalue, "0"))
                *usec = 0;
        else {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
                return 0;
        }
 
        return 1;
}
 
static int config_parse_address_scope(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        Address *address = ASSERT_PTR(userdata);
        int r;
 
        if (isempty(rvalue)) {
                address->scope = RT_SCOPE_UNIVERSE;
                address->scope_set = false;
                return 1;
        }
 
        r = route_scope_from_string(rvalue);
        if (r < 0)
                return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
 
        address->scope = r;
        address->scope_set = true;
        return 1;
}
 
static int config_parse_address_dad(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        AddressFamily *p = ASSERT_PTR(data);
        int r;
 
        if (isempty(rvalue)) {
                *p = _ADDRESS_FAMILY_INVALID;
                return 1;
        }
 
        r = parse_boolean(rvalue);
        if (r >= 0) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "For historical reasons, %s=%s means %s=%s. "
                           "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
                           lvalue, rvalue, lvalue, r ? "none" : "both");
                *p = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
                return 1;
        }
 
        AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue);
        if (a < 0)
                return log_syntax_parse_error(unit, filename, line, a, lvalue, rvalue);
 
        *p = a;
        return 1;
}
 
int config_parse_address_section(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {
 
        static const ConfigSectionParser table[_ADDRESS_CONF_PARSER_MAX] = {
                [ADDRESS_ADDRESS]                  = { .parser = config_parse_address,            .ltype = 0,                        .offset = offsetof(Address, in_addr),                     },
                [ADDRESS_PEER]                     = { .parser = config_parse_address,            .ltype = 0,                        .offset = offsetof(Address, in_addr_peer),                },
                [ADDRESS_BROADCAST]                = { .parser = config_parse_broadcast,          .ltype = 0,                        .offset = 0,                                              },
                [ADDRESS_LABEL]                    = { .parser = config_parse_address_label,      .ltype = 0,                        .offset = offsetof(Address, label),                       },
                [ADDRESS_PREFERRED_LIFETIME]       = { .parser = config_parse_address_lifetime,   .ltype = 0,                        .offset = offsetof(Address, lifetime_preferred_usec),     },
                [ADDRESS_HOME_ADDRESS]             = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_HOMEADDRESS,        .offset = offsetof(Address, flags),                       },
                [ADDRESS_MANAGE_TEMPORARY_ADDRESS] = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_MANAGETEMPADDR,     .offset = offsetof(Address, flags),                       },
                [ADDRESS_PREFIX_ROUTE]             = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_NOPREFIXROUTE,      .offset = offsetof(Address, flags),                       },
                [ADDRESS_ADD_PREFIX_ROUTE]         = { .parser = config_parse_uint32_invert_flag, .ltype = IFA_F_NOPREFIXROUTE,      .offset = offsetof(Address, flags),                       },
                [ADDRESS_AUTO_JOIN]                = { .parser = config_parse_uint32_flag,        .ltype = IFA_F_MCAUTOJOIN,         .offset = offsetof(Address, flags),                       },
                [ADDRESS_DAD]                      = { .parser = config_parse_address_dad,        .ltype = 0,                        .offset = offsetof(Address, duplicate_address_detection), },
                [ADDRESS_SCOPE]                    = { .parser = config_parse_address_scope,      .ltype = 0,                        .offset = 0,                                              },
                [ADDRESS_ROUTE_METRIC]             = { .parser = config_parse_uint32,             .ltype = 0,                        .offset = offsetof(Address, route_metric),                },
                [ADDRESS_NET_LABEL]                = { .parser = config_parse_string,             .ltype = CONFIG_PARSE_STRING_SAFE, .offset = offsetof(Address, netlabel),                    },
                [ADDRESS_NFT_SET]                  = { .parser = config_parse_nft_set,            .ltype = NFT_SET_PARSE_NETWORK,    .offset = offsetof(Address, nft_set_context),             },
        };
 
        _cleanup_(address_unref_or_set_invalidp) Address *address = NULL;
        Network *network = ASSERT_PTR(userdata);
        int r;
 
        assert(filename);
        assert(section);
 
        if (streq(section, "Network")) {
                assert(streq(lvalue, "Address"));
 
                if (isempty(rvalue)) {
                        /* If an empty string specified in [Network] section, clear previously assigned addresses. */
                        network->addresses_by_section = ordered_hashmap_free(network->addresses_by_section);
                        return 0;
                }
 
                /* we are not in an Address section, so use line number instead. */
                r = address_new_static(network, filename, line, &address);
        } else
                r = address_new_static(network, filename, section_line, &address);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to allocate new address, ignoring assignment: %m");
                return 0;
        }
 
        r = config_section_parse(table, ELEMENTSOF(table),
                                 unit, filename, line, section, section_line, lvalue, ltype, rvalue, address);
        if (r <= 0)
                return r;
 
        TAKE_PTR(address);
        return 0;
}
 
#define log_broadcast(address, fmt, ...)                                \
        ({                                                              \
                const Address *_address = (address);                    \
                log_section_warning(                                    \
                                _address ? _address->section : NULL,    \
                                fmt " Ignoring Broadcast= setting in the [Address] section.", \
                                ##__VA_ARGS__);                         \
        })
 
static void address_section_adjust_broadcast(Address *address) {
        assert(address);
        assert(address->section);
 
        if (!in4_addr_is_set(&address->broadcast))
                return;
 
        if (address->family == AF_INET6)
                log_broadcast(address, "Broadcast address is set for an IPv6 address.");
        else if (address->prefixlen > 30)
                log_broadcast(address, "Broadcast address is set for an IPv4 address with prefix length larger than 30.");
        else if (in4_addr_is_set(&address->in_addr_peer.in))
                log_broadcast(address, "Broadcast address is set for an IPv4 address with peer address.");
        else if (!in4_addr_is_set(&address->in_addr.in))
                log_broadcast(address, "Broadcast address is set for an IPv4 address with null address.");
        else
                /* Otherwise, keep the specified broadcast address. */
                return;
 
        address->broadcast.s_addr = 0;
}
 
#define log_address_section(address, fmt, ...)                          \
        ({                                                              \
                const Address *_address = (address);                    \
                log_section_warning_errno(                              \
                                _address ? _address->section : NULL,    \
                                SYNTHETIC_ERRNO(EINVAL),                \
                                fmt " Ignoring [Address] section.",     \
                                ##__VA_ARGS__);                         \
        })
 
int address_section_verify(Address *address) {
        assert(address);
        assert(address->section);
 
        if (section_is_invalid(address->section))
                return -EINVAL;
 
        if (address->family == AF_UNSPEC)
                return log_address_section(address, "Address section without Address= field was configured.");
        assert(IN_SET(address->family, AF_INET, AF_INET6));
 
        if (address->family == AF_INET6 && !socket_ipv6_is_supported())
                return log_address_section(address, "An IPv6 address was configured, but the kernel does not support IPv6.");
 
        if (in_addr_is_null(address->family, &address->in_addr)) {
                /* Will use address from address pool. Note that for ipv6 case, prefix of the address
                 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
                 * let's limit the prefix length to 64 or larger. See RFC4193. */
                unsigned min_prefixlen = address->family == AF_INET ? 8 : 64;
                if (address->prefixlen < min_prefixlen)
                        return log_address_section(address, "Prefix length for %s null address must be equal or larger than %u.",
                                                   af_to_ipv4_ipv6(address->family), min_prefixlen);
 
                address->requested_as_null = !in_addr_is_set(address->family, &address->in_addr);
        }
 
        address_section_adjust_broadcast(address);
 
        if (address->family == AF_INET6 && address->label) {
                log_section_warning(address->section, "Address label is set for IPv6 address, ignoring Label= setting.");
                address->label = mfree(address->label);
        }
 
        if (!address->scope_set) {
                if (in_addr_is_localhost(address->family, &address->in_addr) > 0)
                        address->scope = RT_SCOPE_HOST;
                else if (in_addr_is_link_local(address->family, &address->in_addr) > 0)
                        address->scope = RT_SCOPE_LINK;
        }
 
        if (address->duplicate_address_detection < 0) {
                if (address->family == AF_INET6)
                        address->duplicate_address_detection = ADDRESS_FAMILY_IPV6;
                else if (in4_addr_is_link_local(&address->in_addr.in))
                        address->duplicate_address_detection = ADDRESS_FAMILY_IPV4;
                else
                        address->duplicate_address_detection = ADDRESS_FAMILY_NO;
        } else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV6 && address->family == AF_INET)
                log_section_warning(address->section, "DuplicateAddressDetection=ipv6 is specified for IPv4 address, ignoring.");
        else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV4 && address->family == AF_INET6)
                log_section_warning(address->section, "DuplicateAddressDetection=ipv4 is specified for IPv6 address, ignoring.");
 
        if (address->family == AF_INET6 &&
            !FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
                address->flags |= IFA_F_NODAD;
 
        uint32_t filtered_flags = address->family == AF_INET ?
                address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS & ~IPV6ONLY_FLAGS :
                address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS;
        if (address->flags != filtered_flags) {
                _cleanup_free_ char *str = NULL;
 
                (void) address_flags_to_string_alloc(address->flags ^ filtered_flags, address->family, &str);
                return log_address_section(address, "unexpected address flags \"%s\" were configured.", strna(str));
        }
 
        return 0;
}
 
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
        trivial_hash_ops_address_detach,
        void,
        trivial_hash_func,
        trivial_compare_func,
        Address,
        address_detach);
 
int network_drop_invalid_addresses(Network *network) {
        _cleanup_set_free_ Set *addresses = NULL, *duplicated_addresses = NULL;
        Address *address;
        int r;
 
        assert(network);
 
        ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
                if (address_section_verify(address) < 0) {
                        /* Drop invalid [Address] sections or Address= settings in [Network].
                         * Note that address_detach() will drop the address from addresses_by_section. */
                        address_detach(address);
                        continue;
                }
 
                /* Always use the setting specified later. So, remove the previously assigned setting. */
                Address *dup = set_remove(addresses, address);
                if (dup) {
                        log_warning("%s: Duplicated address %s is specified at line %u and %u, "
                                    "dropping the address setting specified at line %u.",
                                    dup->section->filename,
                                    IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen),
                                    address->section->line,
                                    dup->section->line, dup->section->line);
 
                        /* Do not call address_detach() for 'dup' now, as we can remove only the current
                         * entry in the loop. We will drop the address from addresses_by_section later. */
                        r = set_ensure_put(&duplicated_addresses, &trivial_hash_ops_address_detach, dup);
                        if (r < 0)
                                return log_oom();
                        assert(r > 0);
                }
 
                /* Use address_hash_ops, instead of address_hash_ops_detach. Otherwise, the Address objects
                 * will be detached. */
                r = set_ensure_put(&addresses, &address_hash_ops, address);
                if (r < 0)
                        return log_oom();
                assert(r > 0);
        }
 
        /* Detach duplicated entries now. */
        duplicated_addresses = set_free(duplicated_addresses);
 
        r = network_adjust_dhcp_server(network, &addresses);
        if (r < 0)
                return r;
 
        network_adjust_dhcp_relay(network);
        return 0;
}