HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd-network/ndisc-option.c

59,719 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 228674adecffa20b291be978145cdb1d20d19cf1

Lines 1–1000 of 1,813

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <linux/ipv6.h>
#include <netinet/icmp6.h>
 
#include "sd-ndisc-protocol.h"
 
#include "alloc-util.h"
#include "dns-def.h"
#include "dns-domain.h"
#include "dns-packet.h"
#include "dns-resolver-internal.h"
#include "ether-addr-util.h"
#include "hash-funcs.h"
#include "hostname-util.h"
#include "icmp6-packet.h"
#include "icmp6-util.h"
#include "in-addr-util.h"
#include "iovec-util.h"
#include "ndisc-option.h"
#include "network-common.h"
#include "set.h"
#include "siphash24.h"
#include "string-util.h"
#include "strv.h"
#include "unaligned.h"
 
/* RFC does not say anything about the maximum number of options, but let's limit the number of options for
 * safety. Typically, the number of options in an ICMPv6 message should be only a few. */
#define MAX_OPTIONS 128
 
int ndisc_option_parse(
                ICMP6Packet *p,
                size_t offset,
                uint8_t *ret_type,
                size_t *ret_len,
                const uint8_t **ret_opt) {
 
        assert(p);
 
        if (offset == p->raw_size)
                return -ESPIPE; /* end of the packet */
 
        if (offset > p->raw_size)
                return -EBADMSG;
 
        if (p->raw_size - offset < sizeof(struct nd_opt_hdr))
                return -EBADMSG;
 
        assert_cc(alignof(struct nd_opt_hdr) == 1);
        const struct nd_opt_hdr *hdr = (const struct nd_opt_hdr*) (p->raw_packet + offset);
        if (hdr->nd_opt_len == 0)
                return -EBADMSG;
 
        size_t len = hdr->nd_opt_len * 8;
        if (p->raw_size - offset < len)
                return -EBADMSG;
 
        if (ret_type)
                *ret_type = hdr->nd_opt_type;
        if (ret_len)
                *ret_len = len;
        if (ret_opt)
                *ret_opt = p->raw_packet + offset;
 
        return 0;
}
 
static sd_ndisc_option* ndisc_option_new(uint8_t type, size_t offset) {
        sd_ndisc_option *p = new0(sd_ndisc_option, 1); /* use new0() here to make the fuzzers silent. */
        if (!p)
                return NULL;
 
        /* As the same reason in the above, do not use the structured initializer here. */
        p->type = type;
        p->offset = offset;
 
        return p;
}
 
static void ndisc_raw_done(sd_ndisc_raw *raw) {
        if (!raw)
                return;
 
        free(raw->bytes);
}
 
static void ndisc_rdnss_done(sd_ndisc_rdnss *rdnss) {
        if (!rdnss)
                return;
 
        free(rdnss->addresses);
}
 
static void ndisc_dnssl_done(sd_ndisc_dnssl *dnssl) {
        if (!dnssl)
                return;
 
        strv_free(dnssl->domains);
}
 
static void ndisc_dnr_done(sd_ndisc_dnr *dnr) {
        if (!dnr)
                return;
 
        sd_dns_resolver_unref(dnr->resolver);
}
 
sd_ndisc_option* ndisc_option_free(sd_ndisc_option *option) {
        if (!option)
                return NULL;
 
        switch (option->type) {
        case 0:
                ndisc_raw_done(&option->raw);
                break;
 
        case SD_NDISC_OPTION_RDNSS:
                ndisc_rdnss_done(&option->rdnss);
                break;
 
        case SD_NDISC_OPTION_DNSSL:
                ndisc_dnssl_done(&option->dnssl);
                break;
 
        case SD_NDISC_OPTION_CAPTIVE_PORTAL:
                free(option->captive_portal);
                break;
 
        case SD_NDISC_OPTION_ENCRYPTED_DNS:
                ndisc_dnr_done(&option->encrypted_dns);
                break;
        }
 
        return mfree(option);
}
 
static int ndisc_option_compare_func(const sd_ndisc_option *x, const sd_ndisc_option *y) {
        int r;
 
        assert(x);
        assert(y);
 
        r = CMP(x->type, y->type);
        if (r != 0)
                return r;
 
        switch (x->type) {
        case 0:
                return memcmp_nn(x->raw.bytes, x->raw.length, y->raw.bytes, y->raw.length);
 
        case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
        case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
        case SD_NDISC_OPTION_REDIRECTED_HEADER:
        case SD_NDISC_OPTION_MTU:
        case SD_NDISC_OPTION_HOME_AGENT:
        case SD_NDISC_OPTION_FLAGS_EXTENSION:
        case SD_NDISC_OPTION_CAPTIVE_PORTAL:
                /* These options cannot be specified multiple times. */
                return 0;
 
        case SD_NDISC_OPTION_PREFIX_INFORMATION:
                /* Should not specify the same prefix multiple times. */
                r = CMP(x->prefix.prefixlen, y->prefix.prefixlen);
                if (r != 0)
                        return r;
 
                return memcmp(&x->prefix.address, &y->prefix.address, sizeof(struct in6_addr));
 
        case SD_NDISC_OPTION_ROUTE_INFORMATION:
                r = CMP(x->route.prefixlen, y->route.prefixlen);
                if (r != 0)
                        return r;
 
                return memcmp(&x->route.address, &y->route.address, sizeof(struct in6_addr));
 
        case SD_NDISC_OPTION_PREF64:
                r = CMP(x->prefix64.prefixlen, y->prefix64.prefixlen);
                if (r != 0)
                        return r;
 
                return memcmp(&x->prefix64.prefix, &y->prefix64.prefix, sizeof(struct in6_addr));
 
        default:
                /* DNSSL, RDNSS, and other unsupported options can be specified multiple times. */
                return trivial_compare_func(x, y);
        }
}
 
static void ndisc_option_hash_func(const sd_ndisc_option *option, struct siphash *state) {
        assert(option);
        assert(state);
 
        siphash24_compress_typesafe(option->type, state);
 
        switch (option->type) {
        case 0:
                siphash24_compress(option->raw.bytes, option->raw.length, state);
                break;
 
        case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
        case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
        case SD_NDISC_OPTION_REDIRECTED_HEADER:
        case SD_NDISC_OPTION_MTU:
        case SD_NDISC_OPTION_HOME_AGENT:
        case SD_NDISC_OPTION_FLAGS_EXTENSION:
        case SD_NDISC_OPTION_CAPTIVE_PORTAL:
                break;
 
        case SD_NDISC_OPTION_PREFIX_INFORMATION:
                siphash24_compress_typesafe(option->prefix.prefixlen, state);
                siphash24_compress_typesafe(option->prefix.address, state);
                break;
 
        case SD_NDISC_OPTION_ROUTE_INFORMATION:
                siphash24_compress_typesafe(option->route.prefixlen, state);
                siphash24_compress_typesafe(option->route.address, state);
                break;
 
        case SD_NDISC_OPTION_PREF64:
                siphash24_compress_typesafe(option->prefix64.prefixlen, state);
                siphash24_compress_typesafe(option->prefix64.prefix, state);
                break;
 
        default:
                trivial_hash_func(option, state);
        }
}
 
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
                ndisc_option_hash_ops,
                sd_ndisc_option,
                ndisc_option_hash_func,
                ndisc_option_compare_func,
                ndisc_option_free);
 
static int ndisc_option_consume(Set **options, sd_ndisc_option *p) {
        assert(options);
        assert(p);
 
        if (set_size(*options) >= MAX_OPTIONS) {
                ndisc_option_free(p);
                return -ETOOMANYREFS; /* recognizable error code */
        }
 
        return set_ensure_consume(options, &ndisc_option_hash_ops, p);
}
 
static int ndisc_option_build_raw(const sd_ndisc_option *option, uint8_t **ret) {
        assert(option);
        assert(option->type == 0);
        assert(ret);
 
        _cleanup_free_ uint8_t *buf = newdup(uint8_t, option->raw.bytes, option->raw.length);
        if (!buf)
                return -ENOMEM;
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_link_layer_address(Set **options, uint8_t type, size_t offset, const struct ether_addr *mac) {
        assert(options);
        assert(IN_SET(type, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS));
 
        if (!mac || ether_addr_is_null(mac)) {
                ndisc_option_remove_by_type(*options, type);
                return 0;
        }
 
        sd_ndisc_option *p = ndisc_option_get_by_type(*options, type);
        if (p) {
                /* offset == 0 means that we are now building a packet to be sent, and in that case we allow
                 * to override the option we previously set.
                 * offset != 0 means that we are now parsing a received packet, and we refuse to override
                 * conflicting options. */
                if (offset != 0)
                        return -EEXIST;
 
                p->mac = *mac;
                return 0;
        }
 
        p = ndisc_option_new(type, offset);
        if (!p)
                return -ENOMEM;
 
        p->mac = *mac;
 
        return set_ensure_consume(options, &ndisc_option_hash_ops, p);
}
 
static int ndisc_option_parse_link_layer_address(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        assert(options);
        assert(opt);
 
        if (len != sizeof(struct ether_addr) + 2)
                return -EBADMSG;
 
        if (!IN_SET(opt[0], SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS))
                return -EBADMSG;
 
        struct ether_addr mac;
        memcpy(&mac, opt + 2, sizeof(struct ether_addr));
 
        if (ether_addr_is_null(&mac))
                return -EBADMSG;
 
        return ndisc_option_add_link_layer_address(options, opt[0], offset, &mac);
}
 
static int ndisc_option_build_link_layer_address(const sd_ndisc_option *option, uint8_t **ret) {
        assert(option);
        assert(IN_SET(option->type, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS));
        assert(ret);
 
        assert_cc(2 + sizeof(struct ether_addr) == 8);
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, 2 + sizeof(struct ether_addr));
        if (!buf)
                return -ENOMEM;
 
        buf[0] = option->type;
        buf[1] = 1;
        memcpy(buf + 2, &option->mac, sizeof(struct ether_addr));
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_prefix_internal(
                Set **options,
                size_t offset,
                uint8_t flags,
                uint8_t prefixlen,
                const struct in6_addr *address,
                usec_t valid_lifetime,
                usec_t preferred_lifetime,
                usec_t valid_until,
                usec_t preferred_until) {
 
        assert(options);
        assert(address);
 
        if (prefixlen > 128)
                return -EINVAL;
 
        struct in6_addr addr = *address;
        in6_addr_mask(&addr, prefixlen);
 
        /* RFC 4861 and 4862 only state that link-local prefix should be ignored.
         * But here we also ignore null and multicast addresses. */
        if (in6_addr_is_link_local(&addr) || in6_addr_is_null(&addr) || in6_addr_is_multicast(&addr))
                return -EINVAL;
 
        if (preferred_lifetime > valid_lifetime)
                return -EINVAL;
 
        if (preferred_until > valid_until)
                return -EINVAL;
 
        sd_ndisc_option *p = ndisc_option_get(
                                *options,
                                &(const sd_ndisc_option) {
                                        .type = SD_NDISC_OPTION_PREFIX_INFORMATION,
                                        .prefix.prefixlen = prefixlen,
                                        .prefix.address = addr,
                                });
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                p->prefix.flags = flags;
                p->prefix.valid_lifetime = valid_lifetime;
                p->prefix.preferred_lifetime = preferred_lifetime;
                p->prefix.valid_until = valid_until;
                p->prefix.preferred_until = preferred_until;
                return 0;
        }
 
        p = ndisc_option_new(SD_NDISC_OPTION_PREFIX_INFORMATION, offset);
        if (!p)
                return -ENOMEM;
 
        p->prefix = (sd_ndisc_prefix) {
                .flags = flags,
                .prefixlen = prefixlen,
                .address = addr,
                .valid_lifetime = valid_lifetime,
                .preferred_lifetime = preferred_lifetime,
                .valid_until = valid_until,
                .preferred_until = preferred_until,
        };
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_prefix(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        const struct nd_opt_prefix_info *pi = (const struct nd_opt_prefix_info*) ASSERT_PTR(opt);
 
        assert(options);
 
        if (len != sizeof(struct nd_opt_prefix_info))
                return -EBADMSG;
 
        if (pi->nd_opt_pi_type != SD_NDISC_OPTION_PREFIX_INFORMATION)
                return -EBADMSG;
 
        usec_t valid = be32_sec_to_usec(pi->nd_opt_pi_valid_time, /* max_as_infinity= */ true);
        usec_t pref = be32_sec_to_usec(pi->nd_opt_pi_preferred_time, /* max_as_infinity= */ true);
 
        /* We only support 64 bits interface identifier for addrconf. */
        uint8_t flags = pi->nd_opt_pi_flags_reserved;
        if (FLAGS_SET(flags, ND_OPT_PI_FLAG_AUTO) && pi->nd_opt_pi_prefix_len != 64)
                flags &= ~ND_OPT_PI_FLAG_AUTO;
 
        return ndisc_option_add_prefix(options, offset, flags,
                                       pi->nd_opt_pi_prefix_len, &pi->nd_opt_pi_prefix,
                                       valid, pref);
}
 
static int ndisc_option_build_prefix(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_PREFIX_INFORMATION);
        assert(ret);
 
        assert_cc(sizeof(struct nd_opt_prefix_info) % 8 == 0);
 
        _cleanup_free_ struct nd_opt_prefix_info *buf = new(struct nd_opt_prefix_info, 1);
        if (!buf)
                return -ENOMEM;
 
        usec_t valid = MIN(option->prefix.valid_lifetime,
                           usec_sub_unsigned(option->prefix.valid_until, timestamp));
        usec_t pref = MIN3(valid,
                           option->prefix.preferred_lifetime,
                           usec_sub_unsigned(option->prefix.preferred_until, timestamp));
 
        *buf = (struct nd_opt_prefix_info) {
                .nd_opt_pi_type = SD_NDISC_OPTION_PREFIX_INFORMATION,
                .nd_opt_pi_len = sizeof(struct nd_opt_prefix_info) / 8,
                .nd_opt_pi_prefix_len = option->prefix.prefixlen,
                .nd_opt_pi_flags_reserved = option->prefix.flags,
                .nd_opt_pi_valid_time = usec_to_be32_sec(valid),
                .nd_opt_pi_preferred_time = usec_to_be32_sec(pref),
                .nd_opt_pi_prefix = option->prefix.address,
        };
 
        *ret = (uint8_t*) TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_redirected_header(Set **options, size_t offset, const struct ip6_hdr *hdr) {
        assert(options);
 
        if (!hdr) {
                ndisc_option_remove_by_type(*options, SD_NDISC_OPTION_REDIRECTED_HEADER);
                return 0;
        }
 
        sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_REDIRECTED_HEADER);
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                memcpy(&p->hdr, hdr, sizeof(struct ip6_hdr));
                return 0;
        }
 
        p = ndisc_option_new(SD_NDISC_OPTION_REDIRECTED_HEADER, offset);
        if (!p)
                return -ENOMEM;
 
        /* For safety, here we copy only IPv6 header. */
        memcpy(&p->hdr, hdr, sizeof(struct ip6_hdr));
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_redirected_header(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        assert(options);
        assert(opt);
 
        if (len < sizeof(struct nd_opt_rd_hdr) + sizeof(struct ip6_hdr))
                return -EBADMSG;
 
        if (opt[0] != SD_NDISC_OPTION_REDIRECTED_HEADER)
                return -EBADMSG;
 
        return ndisc_option_add_redirected_header(options, offset, (const struct ip6_hdr*) (opt + sizeof(struct nd_opt_rd_hdr)));
}
 
static int ndisc_option_build_redirected_header(const sd_ndisc_option *option, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_REDIRECTED_HEADER);
        assert(ret);
 
        assert_cc((sizeof(struct nd_opt_rd_hdr) + sizeof(struct ip6_hdr)) % 8 == 0);
 
        size_t len = DIV_ROUND_UP(sizeof(struct nd_opt_rd_hdr) + sizeof(struct ip6_hdr), 8);
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
        if (!buf)
                return -ENOMEM;
 
        uint8_t *p;
        p = mempcpy(buf,
                    &(const struct nd_opt_rd_hdr) {
                            .nd_opt_rh_type = SD_NDISC_OPTION_REDIRECTED_HEADER,
                            .nd_opt_rh_len = len,
                    },
                    sizeof(struct nd_opt_rd_hdr));
        memcpy(p, &option->hdr, sizeof(struct ip6_hdr));
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_mtu(Set **options, size_t offset, uint32_t mtu) {
        assert(options);
 
        if (mtu < IPV6_MIN_MTU)
                return -EINVAL;
 
        sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_MTU);
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                p->mtu = mtu;
                return 0;
        }
 
        p = ndisc_option_new(SD_NDISC_OPTION_MTU, offset);
        if (!p)
                return -ENOMEM;
 
        p->mtu = mtu;
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_mtu(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        const struct nd_opt_mtu *pm = (const struct nd_opt_mtu*) ASSERT_PTR(opt);
 
        assert(options);
 
        if (len != sizeof(struct nd_opt_mtu))
                return -EBADMSG;
 
        if (pm->nd_opt_mtu_type != SD_NDISC_OPTION_MTU)
                return -EBADMSG;
 
        return ndisc_option_add_mtu(options, offset, be32toh(pm->nd_opt_mtu_mtu));
}
 
static int ndisc_option_build_mtu(const sd_ndisc_option *option, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_MTU);
        assert(ret);
 
        assert_cc(sizeof(struct nd_opt_mtu) % 8 == 0);
 
        _cleanup_free_ struct nd_opt_mtu *buf = new(struct nd_opt_mtu, 1);
        if (!buf)
                return -ENOMEM;
 
        *buf = (struct nd_opt_mtu) {
                .nd_opt_mtu_type = SD_NDISC_OPTION_MTU,
                .nd_opt_mtu_len = sizeof(struct nd_opt_mtu) / 8,
                .nd_opt_mtu_mtu = htobe32(option->mtu),
        };
 
        *ret = (uint8_t*) TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_home_agent_internal(
                Set **options,
                size_t offset,
                uint16_t preference,
                usec_t lifetime,
                usec_t valid_until) {
 
        assert(options);
 
        if (lifetime > UINT16_MAX * USEC_PER_SEC)
                return -EINVAL;
 
        sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_HOME_AGENT);
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                p->home_agent = (sd_ndisc_home_agent) {
                        .preference = preference,
                        .lifetime = lifetime,
                        .valid_until = valid_until,
                };
                return 0;
        }
 
        p = ndisc_option_new(SD_NDISC_OPTION_HOME_AGENT, offset);
        if (!p)
                return -ENOMEM;
 
        p->home_agent = (sd_ndisc_home_agent) {
                .preference = preference,
                .lifetime = lifetime,
                .valid_until = valid_until,
        };
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_home_agent(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        const struct nd_opt_home_agent_info *p = (const struct nd_opt_home_agent_info*) ASSERT_PTR(opt);
 
        assert(options);
 
        if (len != sizeof(struct nd_opt_home_agent_info))
                return -EBADMSG;
 
        if (p->nd_opt_home_agent_info_type != SD_NDISC_OPTION_HOME_AGENT)
                return -EBADMSG;
 
        return ndisc_option_add_home_agent(
                        options, offset,
                        be16toh(p->nd_opt_home_agent_info_preference),
                        be16_sec_to_usec(p->nd_opt_home_agent_info_lifetime, /* max_as_infinity= */ false));
}
 
static int ndisc_option_build_home_agent(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_HOME_AGENT);
        assert(ret);
 
        assert_cc(sizeof(struct nd_opt_home_agent_info) % 8 == 0);
 
        usec_t lifetime = MIN(option->home_agent.lifetime,
                              usec_sub_unsigned(option->home_agent.valid_until, timestamp));
 
        _cleanup_free_ struct nd_opt_home_agent_info *buf = new(struct nd_opt_home_agent_info, 1);
        if (!buf)
                return -ENOMEM;
 
        *buf = (struct nd_opt_home_agent_info) {
                .nd_opt_home_agent_info_type = SD_NDISC_OPTION_HOME_AGENT,
                .nd_opt_home_agent_info_len = sizeof(struct nd_opt_home_agent_info) / 8,
                .nd_opt_home_agent_info_preference = htobe16(option->home_agent.preference),
                .nd_opt_home_agent_info_lifetime = usec_to_be16_sec(lifetime),
        };
 
        *ret = (uint8_t*) TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_route_internal(
                Set **options,
                size_t offset,
                uint8_t preference,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime,
                usec_t valid_until) {
 
        assert(options);
        assert(prefix);
 
        if (prefixlen > 128)
                return -EINVAL;
 
        /* RFC 4191 section 2.3
         * Prf (Route Preference)
         * 2-bit signed integer. The Route Preference indicates whether to prefer the router associated with
         * this prefix over others, when multiple identical prefixes (for different routers) have been
         * received. If the Reserved (10) value is received, the Route Information Option MUST be ignored. */
        if (!IN_SET(preference, SD_NDISC_PREFERENCE_LOW, SD_NDISC_PREFERENCE_MEDIUM, SD_NDISC_PREFERENCE_HIGH))
                return -EINVAL;
 
        struct in6_addr addr = *prefix;
        in6_addr_mask(&addr, prefixlen);
 
        sd_ndisc_option *p = ndisc_option_get(
                                *options,
                                &(const sd_ndisc_option) {
                                        .type = SD_NDISC_OPTION_ROUTE_INFORMATION,
                                        .route.prefixlen = prefixlen,
                                        .route.address = addr,
                                });
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                p->route.preference = preference;
                p->route.lifetime = lifetime;
                p->route.valid_until = valid_until;
                return 0;
        }
 
        p = ndisc_option_new(SD_NDISC_OPTION_ROUTE_INFORMATION, offset);
        if (!p)
                return -ENOMEM;
 
        p->route = (sd_ndisc_route) {
                .preference = preference,
                .prefixlen = prefixlen,
                .address = addr,
                .lifetime = lifetime,
                .valid_until = valid_until,
        };
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_route(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        assert(options);
        assert(opt);
 
        if (!IN_SET(len, 1*8, 2*8, 3*8))
                return -EBADMSG;
 
        if (opt[0] != SD_NDISC_OPTION_ROUTE_INFORMATION)
                return -EBADMSG;
 
        uint8_t prefixlen = opt[2];
        if (prefixlen > 128)
                return -EBADMSG;
 
        if (len < (size_t) (DIV_ROUND_UP(prefixlen, 64) + 1) * 8)
                return -EBADMSG;
 
        uint8_t preference = (opt[3] >> 3) & 0x03;
        usec_t lifetime = unaligned_be32_sec_to_usec(opt + 4, /* max_as_infinity= */ true);
 
        struct in6_addr prefix;
        memcpy_safe(&prefix, opt + 8, len - 8);
        in6_addr_mask(&prefix, prefixlen);
 
        return ndisc_option_add_route(options, offset, preference, prefixlen, &prefix, lifetime);
}
 
static int ndisc_option_build_route(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_ROUTE_INFORMATION);
        assert(option->route.prefixlen <= 128);
        assert(ret);
 
        size_t len = 1 + DIV_ROUND_UP(option->route.prefixlen, 64);
        be32_t lifetime = usec_to_be32_sec(MIN(option->route.lifetime,
                                               usec_sub_unsigned(option->route.valid_until, timestamp)));
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
        if (!buf)
                return -ENOMEM;
 
        buf[0] = SD_NDISC_OPTION_ROUTE_INFORMATION;
        buf[1] = len;
        buf[2] = option->route.prefixlen;
        buf[3] = option->route.preference << 3;
        memcpy(buf + 4, &lifetime, sizeof(be32_t));
        memcpy_safe(buf + 8, &option->route.address, (len - 1) * 8);
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_rdnss_internal(
                Set **options,
                size_t offset,
                size_t n_addresses,
                const struct in6_addr *addresses,
                usec_t lifetime,
                usec_t valid_until) {
 
        assert(options);
        assert(addresses);
 
        if (n_addresses == 0)
                return -EINVAL;
 
        _cleanup_free_ struct in6_addr *addrs = newdup(struct in6_addr, addresses, n_addresses);
        if (!addrs)
                return -ENOMEM;
 
        sd_ndisc_option *p = ndisc_option_new(SD_NDISC_OPTION_RDNSS, offset);
        if (!p)
                return -ENOMEM;
 
        p->rdnss = (sd_ndisc_rdnss) {
                .n_addresses = n_addresses,
                .addresses = TAKE_PTR(addrs),
                .lifetime = lifetime,
                .valid_until = valid_until,
        };
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_rdnss(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        assert(options);
        assert(opt);
 
        if (len < 8 + sizeof(struct in6_addr) || (len % sizeof(struct in6_addr)) != 8)
                return -EBADMSG;
 
        if (opt[0] != SD_NDISC_OPTION_RDNSS)
                return -EBADMSG;
 
        usec_t lifetime = unaligned_be32_sec_to_usec(opt + 4, /* max_as_infinity= */ true);
        size_t n_addrs = len / sizeof(struct in6_addr);
 
        return ndisc_option_add_rdnss(options, offset, n_addrs, (const struct in6_addr*) (opt + 8), lifetime);
}
 
static int ndisc_option_build_rdnss(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_RDNSS);
        assert(ret);
 
        size_t len = option->rdnss.n_addresses * 2 + 1;
        be32_t lifetime = usec_to_be32_sec(MIN(option->rdnss.lifetime,
                                               usec_sub_unsigned(option->rdnss.valid_until, timestamp)));
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
        if (!buf)
                return -ENOMEM;
 
        buf[0] = SD_NDISC_OPTION_RDNSS;
        buf[1] = len;
        buf[2] = 0;
        buf[3] = 0;
        memcpy(buf + 4, &lifetime, sizeof(be32_t));
        memcpy(buf + 8, option->rdnss.addresses, sizeof(struct in6_addr) * option->rdnss.n_addresses);
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_flags_extension(Set **options, size_t offset, uint64_t flags) {
        assert(options);
 
        if ((flags & UINT64_C(0x00ffffffffffff00)) != flags)
                return -EINVAL;
 
        sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_FLAGS_EXTENSION);
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                p->extended_flags = flags;
                return 0;
        }
 
        p = ndisc_option_new(SD_NDISC_OPTION_FLAGS_EXTENSION, offset);
        if (!p)
                return -ENOMEM;
 
        p->extended_flags = flags;
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_flags_extension(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        assert(options);
        assert(opt);
 
        if (len != 8)
                return -EBADMSG;
 
        if (opt[0] != SD_NDISC_OPTION_FLAGS_EXTENSION)
                return -EBADMSG;
 
        uint64_t flags = 0;
        for (size_t i = 2; i < 8; i++)
                flags |= ((uint64_t) opt[i]) << ((i - 1) * 8);
 
        return ndisc_option_add_flags_extension(options, offset, flags);
}
 
static int ndisc_option_build_flags_extension(const sd_ndisc_option *option, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_FLAGS_EXTENSION);
        assert(ret);
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, 8);
        if (!buf)
                return -ENOMEM;
 
        buf[0] = SD_NDISC_OPTION_FLAGS_EXTENSION;
        buf[1] = 1;
 
        for (size_t i = 2; i < 8; i++)
                buf[i] = (option->extended_flags >> ((i - 1) * 8)) & 0xff;
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_dnssl_internal(
                Set **options,
                size_t offset, char *
                const *domains,
                usec_t lifetime,
                usec_t valid_until) {
 
        int r;
 
        assert(options);
 
        if (strv_isempty(domains))
                return -EINVAL;
 
        STRV_FOREACH(s, domains) {
                r = dns_name_is_valid(*s);
                if (r < 0)
                        return r;
 
                if (is_localhost(*s) || dns_name_is_root(*s))
                        return -EINVAL;
        }
 
        _cleanup_strv_free_ char **copy = strv_copy(domains);
        if (!copy)
                return -ENOMEM;
 
        sd_ndisc_option *p = ndisc_option_new(SD_NDISC_OPTION_DNSSL, offset);
        if (!p)
                return -ENOMEM;
 
        p->dnssl = (sd_ndisc_dnssl) {
                .domains = TAKE_PTR(copy),
                .lifetime = lifetime,
                .valid_until = valid_until,
        };
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_dnssl(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        int r;
 
        assert(options);
        assert(opt);
 
        if (len < 2*8)
                return -EBADMSG;
 
        if (opt[0] != SD_NDISC_OPTION_DNSSL)
                return -EBADMSG;
 
        usec_t lifetime = unaligned_be32_sec_to_usec(opt + 4, /* max_as_infinity= */ true);
 
        _cleanup_strv_free_ char **l = NULL;
        _cleanup_free_ char *e = NULL;
        size_t n = 0;
        for (size_t c, pos = 8; pos < len; pos += c) {
 
                c = opt[pos];
                pos++;
 
                if (c == 0) {
                        /* Found NUL termination */
 
                        if (n > 0) {
                                _cleanup_free_ char *normalized = NULL;
 
                                e[n] = 0;
                                r = dns_name_normalize(e, 0, &normalized);
                                if (r < 0)
                                        return r;
 
                                /* Ignore the root domain name or "localhost" and friends */
                                if (!is_localhost(normalized) && !dns_name_is_root(normalized)) {
                                        r = strv_consume(&l, TAKE_PTR(normalized));
                                        if (r < 0)
                                                return r;
                                }
                        }
 
                        n = 0;
                        continue;
                }
 
                /* Check for compression (which is not allowed) */
                if (c > 63)
                        return -EBADMSG;
 
                if (pos + c >= len)
                        return -EBADMSG;
 
                if (!GREEDY_REALLOC(e, n + (n != 0) + DNS_LABEL_ESCAPED_MAX + 1U))
                        return -ENOMEM;
 
                if (n != 0)
                        e[n++] = '.';
 
                r = dns_label_escape((const char*) (opt + pos), c, e + n, DNS_LABEL_ESCAPED_MAX);
                if (r < 0)
                        return r;