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 1001–1813 of 1,813

 
                n += r;
        }
 
        if (n > 0) /* Not properly NUL terminated */
                return -EBADMSG;
 
        return ndisc_option_add_dnssl(options, offset, l, lifetime);
}
 
 static int ndisc_option_build_dnssl(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
        int r;
 
        assert(option);
        assert(option->type == SD_NDISC_OPTION_DNSSL);
        assert(ret);
 
        size_t len = 8;
        STRV_FOREACH(s, option->dnssl.domains)
                len += strlen(*s) + 2;
        len = DIV_ROUND_UP(len, 8);
 
        be32_t lifetime = usec_to_be32_sec(MIN(option->dnssl.lifetime,
                                               usec_sub_unsigned(option->dnssl.valid_until, timestamp)));
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
        if (!buf)
                return -ENOMEM;
 
        buf[0] = SD_NDISC_OPTION_DNSSL;
        buf[1] = len;
        buf[2] = 0;
        buf[3] = 0;
        memcpy(buf + 4, &lifetime, sizeof(be32_t));
 
        size_t remaining = len * 8 - 8;
        uint8_t *p = buf + 8;
 
        STRV_FOREACH(s, option->dnssl.domains) {
                r = dns_name_to_wire_format(*s, p, remaining, /* canonical= */ false);
                if (r < 0)
                        return r;
 
                assert(remaining >= (size_t) r);
                p += r;
                remaining -= r;
        }
 
        memzero(p, remaining);
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_captive_portal(Set **options, size_t offset, const char *portal) {
        assert(options);
 
        if (isempty(portal))
                return -EINVAL;
 
        if (!in_charset(portal, URI_VALID))
                return -EINVAL;
 
        sd_ndisc_option *p = ndisc_option_get_by_type(*options, SD_NDISC_OPTION_CAPTIVE_PORTAL);
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                return free_and_strdup(&p->captive_portal, portal);
        }
 
        _cleanup_free_ char *copy = strdup(portal);
        if (!copy)
                return -ENOMEM;
 
        p = ndisc_option_new(SD_NDISC_OPTION_CAPTIVE_PORTAL, offset);
        if (!p)
                return -ENOMEM;
 
        p->captive_portal = TAKE_PTR(copy);
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_captive_portal(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_CAPTIVE_PORTAL)
                return -EBADMSG;
 
        _cleanup_free_ char *portal = memdup_suffix0(opt + 2, len - 2);
        if (!portal)
                return -ENOMEM;
 
        size_t size = strlen(portal);
        if (size == 0)
                return -EBADMSG;
 
        /* Check that the message is not truncated by an embedded NUL.
         * NUL padding to a multiple of 8 is expected. */
        if (DIV_ROUND_UP(size + 2, 8) * 8 != len && DIV_ROUND_UP(size + 3, 8) * 8 != len)
                return -EBADMSG;
 
        return ndisc_option_add_captive_portal(options, offset, portal);
}
 
static int ndisc_option_build_captive_portal(const sd_ndisc_option *option, uint8_t **ret) {
        assert(option);
        assert(option->type == SD_NDISC_OPTION_CAPTIVE_PORTAL);
        assert(ret);
 
        size_t len_portal = strlen(option->captive_portal);
        size_t len = DIV_ROUND_UP(len_portal + 1 + 2, 8);
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, len * 8);
        if (!buf)
                return -ENOMEM;
 
        buf[0] = SD_NDISC_OPTION_CAPTIVE_PORTAL;
        buf[1] = len;
 
        uint8_t *p = mempcpy(buf + 2, option->captive_portal, len_portal);
        size_t remaining = len * 8 - 2 - len_portal;
 
        memzero(p, remaining);
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
static const uint8_t prefix_length_code_to_prefix_length[_PREFIX_LENGTH_CODE_MAX] = {
        [PREFIX_LENGTH_CODE_96] = 96,
        [PREFIX_LENGTH_CODE_64] = 64,
        [PREFIX_LENGTH_CODE_56] = 56,
        [PREFIX_LENGTH_CODE_48] = 48,
        [PREFIX_LENGTH_CODE_40] = 40,
        [PREFIX_LENGTH_CODE_32] = 32,
};
 
int pref64_prefix_length_to_plc(uint8_t prefixlen, uint8_t *ret) {
        for (size_t i = 0; i < ELEMENTSOF(prefix_length_code_to_prefix_length); i++)
                if (prefix_length_code_to_prefix_length[i] == prefixlen) {
                        if (ret)
                                *ret = i;
                        return 0;
                }
 
        return -EINVAL;
}
 
static int pref64_lifetime_and_plc_parse(uint16_t lifetime_and_plc, uint8_t *ret_prefixlen, usec_t *ret_lifetime) {
        uint16_t plc = lifetime_and_plc & PREF64_PLC_MASK;
        if (plc >= _PREFIX_LENGTH_CODE_MAX)
                return -EINVAL;
 
        if (ret_prefixlen)
                *ret_prefixlen = prefix_length_code_to_prefix_length[plc];
        if (ret_lifetime)
                *ret_lifetime = (lifetime_and_plc & PREF64_SCALED_LIFETIME_MASK) * USEC_PER_SEC;
        return 0;
}
 
int ndisc_option_add_prefix64_internal(
                Set **options,
                size_t offset,
                uint8_t prefixlen,
                const struct in6_addr *prefix,
                usec_t lifetime,
                usec_t valid_until) {
 
        int r;
 
        assert(options);
        assert(prefix);
 
        r = pref64_prefix_length_to_plc(prefixlen, NULL);
        if (r < 0)
                return r;
 
        if (lifetime > PREF64_MAX_LIFETIME_USEC)
                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_PREF64,
                                        .prefix64.prefixlen = prefixlen,
                                        .prefix64.prefix = addr,
                                });
        if (p) {
                if (offset != 0)
                        return -EEXIST;
 
                p->prefix64.lifetime = lifetime;
                p->prefix64.valid_until = valid_until;
                return 0;
        }
 
        p = ndisc_option_new(SD_NDISC_OPTION_PREF64, offset);
        if (!p)
                return -ENOMEM;
 
        p->prefix64 = (sd_ndisc_prefix64) {
                .prefixlen = prefixlen,
                .prefix = addr,
                .lifetime = lifetime,
                .valid_until = valid_until,
        };
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_option_parse_prefix64(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_PREF64)
                return -EBADMSG;
 
        uint8_t prefixlen;
        usec_t lifetime;
        r = pref64_lifetime_and_plc_parse(unaligned_read_be16(opt + 2), &prefixlen, &lifetime);
        if (r < 0)
                return r;
 
        struct in6_addr prefix;
        memcpy(&prefix, opt + 4, len - 4);
        in6_addr_mask(&prefix, prefixlen);
 
        return ndisc_option_add_prefix64(options, offset, prefixlen, &prefix, lifetime);
}
 
static int ndisc_option_build_prefix64(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
        int r;
 
        assert(option);
        assert(option->type == SD_NDISC_OPTION_PREF64);
        assert(ret);
 
        uint8_t code;
        r = pref64_prefix_length_to_plc(option->prefix64.prefixlen, &code);
        if (r < 0)
                return r;
 
        uint16_t lifetime = (uint16_t) DIV_ROUND_UP(MIN(option->prefix64.lifetime,
                                                        usec_sub_unsigned(option->prefix64.valid_until, timestamp)),
                                                    USEC_PER_SEC) & PREF64_SCALED_LIFETIME_MASK;
 
        _cleanup_free_ uint8_t *buf = new(uint8_t, 2 * 8);
        if (!buf)
                return -ENOMEM;
 
        buf[0] = SD_NDISC_OPTION_PREF64;
        buf[1] = 2;
        unaligned_write_be16(buf + 2, lifetime | code);
        memcpy(buf + 4, &option->prefix64.prefix, 12);
 
        *ret = TAKE_PTR(buf);
        return 0;
}
 
int ndisc_option_add_encrypted_dns_internal(
                Set **options,
                size_t offset,
                sd_dns_resolver *res, /* This takes the ownership of this object. */
                usec_t lifetime,
                usec_t valid_until) {
 
        assert(options);
        assert(res);
 
        _cleanup_(sd_dns_resolver_unrefp) sd_dns_resolver *resolver = TAKE_PTR(res);
 
        sd_ndisc_option *p = ndisc_option_new(SD_NDISC_OPTION_ENCRYPTED_DNS, offset);
        if (!p)
                return -ENOMEM;
 
        p->encrypted_dns = (sd_ndisc_dnr) {
                .resolver = TAKE_PTR(resolver),
                .lifetime = lifetime,
                .valid_until = valid_until,
        };
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_get_dns_name(const uint8_t *optval, size_t optlen, char **ret) {
        _cleanup_free_ char *name = NULL;
        int r;
 
        assert(optval || optlen == 0);
        assert(ret);
 
        r = dns_name_from_wire_format(&optval, &optlen, &name);
        if (r < 0)
                return r;
        if (r == 0 || optlen != 0)
                return -EBADMSG;
 
        *ret = TAKE_PTR(name);
        return r;
}
 
static int ndisc_option_parse_encrypted_dns(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        int r;
 
        assert(options);
        assert(opt);
        _cleanup_(sd_dns_resolver_done) sd_dns_resolver res = {};
        usec_t lifetime;
        size_t ilen;
 
        /* Every field up to and including adn must be present */
        if (len < 2*8)
                return -EBADMSG;
 
        if (opt[0] != SD_NDISC_OPTION_ENCRYPTED_DNS)
                return -EBADMSG;
 
        size_t off = 2;
 
        /* Priority */
        res.priority = unaligned_read_be16(opt + off);
        /* Alias mode is not allowed */
        if (res.priority == 0)
                return -EBADMSG;
        off += sizeof(uint16_t);
 
        /* Lifetime */
        lifetime = unaligned_be32_sec_to_usec(opt + off, /* max_as_infinity= */ true);
        off += sizeof(uint32_t);
 
        /* adn field (length + dns-name) */
        ilen = unaligned_read_be16(opt + off);
        off += sizeof(uint16_t);
        if (off + ilen > len)
                return -EBADMSG;
 
        r = ndisc_get_dns_name(opt + off, ilen, &res.auth_name);
        if (r < 0)
                return r;
        r = dns_name_is_valid_ldh(res.auth_name);
        if (r < 0)
                return r;
        if (!r)
                return -EBADMSG;
        if (dns_name_is_root(res.auth_name))
                return -EBADMSG;
        off += ilen;
 
        /* This is the last field in adn-only mode, sans padding */
        if (8 * DIV_ROUND_UP(off, 8) == len && memeqzero(opt + off, len - off))
                return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Received ADN-only encrypted DNS option, ignoring.");
 
        /* Fields following the variable (octets) length adn field are no longer certain to be aligned. */
 
        /* addrs (length + packed struct in6_addr) */
        if (off + sizeof(uint16_t) > len)
                return -EBADMSG;
        ilen = unaligned_read_be16(opt + off);
        off += sizeof(uint16_t);
        if (off + ilen > len || ilen % (sizeof(struct in6_addr)) != 0)
                return -EBADMSG;
 
        size_t n_addrs = ilen / (sizeof(struct in6_addr));
        if (n_addrs == 0)
                return -EBADMSG;
        res.addrs = new(union in_addr_union, n_addrs);
        if (!res.addrs)
                return -ENOMEM;
 
        for (size_t i = 0; i < n_addrs; i++) {
                union in_addr_union addr;
                memcpy(&addr.in6, opt + off, sizeof(struct in6_addr));
                if (in_addr_is_multicast(AF_INET6, &addr) ||
                    in_addr_is_localhost(AF_INET6, &addr))
                        return -EBADMSG;
                res.addrs[i] = addr;
                off += sizeof(struct in6_addr);
        }
        res.n_addrs = n_addrs;
        res.family = AF_INET6;
 
        /* SvcParam field. (length + SvcParams) */
        if (off + sizeof(uint16_t) > len)
                return -EBADMSG;
        ilen = unaligned_read_be16(opt + off);
        off += sizeof(uint16_t);
        if (off + ilen > len)
                return -EBADMSG;
 
        r = dnr_parse_svc_params(opt + off, ilen, &res);
        if (r < 0)
                return r;
        if (r == 0) /* This indicates a valid message we don't support */
                return -EOPNOTSUPP;
        off += ilen;
 
        /* the remaining padding bytes must be zeroed */
        if (len - off >= 8 || !memeqzero(opt + off, len - off))
                return -EBADMSG;
 
        sd_dns_resolver *new_res = new(sd_dns_resolver, 1);
        if (!new_res)
                return -ENOMEM;
 
        *new_res = TAKE_STRUCT(res);
 
        return ndisc_option_add_encrypted_dns(options, offset, new_res, lifetime);
}
 
static int ndisc_option_build_encrypted_dns(const sd_ndisc_option *option, usec_t timestamp, uint8_t **ret) {
        int r;
 
        assert(option);
        assert(option->type == SD_NDISC_OPTION_ENCRYPTED_DNS);
        assert(ret);
 
        size_t off, len, ilen, plen, poff;
 
        /* Everything up to adn field is required, so we need at least 2*8 bytes */
        _cleanup_free_ uint8_t *buf = new(uint8_t, 2 * 8);
        if (!buf)
                return -ENOMEM;
 
        _cleanup_strv_free_ char **alpns = NULL;
        const sd_dns_resolver *res = option->encrypted_dns.resolver;
        be32_t lifetime = usec_to_be32_sec(MIN(option->encrypted_dns.lifetime,
                                               usec_sub_unsigned(option->encrypted_dns.valid_until, timestamp)));
 
        /* Type (Length field filled in last) */
        buf[0] = option->type;
 
        /* Priority */
        off = 2;
        unaligned_write_be16(buf + off, res->priority);
        off += sizeof(be16_t);
 
        /* Lifetime */
        memcpy(buf + off, &lifetime, sizeof(be32_t));
        off += sizeof(be32_t);
 
        /* ADN */
        //FIXME can the wire format be longer than this?
        ilen = strlen(res->auth_name) + 2;
 
        /* From now on, there isn't guaranteed to be enough space to put each field */
        if (!GREEDY_REALLOC(buf, off + sizeof(uint16_t) + ilen))
                return -ENOMEM;
 
        r = dns_name_to_wire_format(res->auth_name, buf + off + sizeof(uint16_t), ilen, /* canonical= */ false);
        if (r < 0)
                return r;
        unaligned_write_be16(buf + off, (uint16_t) r);
        off += sizeof(uint16_t) + r;
 
        /* ADN-only mode */
        if (res->n_addrs == 0)
                goto padding;
 
        /* addrs */
        if (size_multiply_overflow(sizeof(struct in6_addr), res->n_addrs))
                return -ENOMEM;
 
        ilen = res->n_addrs * sizeof(struct in6_addr);
        if (!GREEDY_REALLOC(buf, off + sizeof(uint16_t) + ilen))
                return -ENOMEM;
 
        unaligned_write_be16(buf + off, ilen);
        off += sizeof(uint16_t);
 
        FOREACH_ARRAY(addr, res->addrs, res->n_addrs) {
                memcpy(buf + off, &addr->in6, sizeof(struct in6_addr));
                off += sizeof(struct in6_addr);
        }
 
        /* SvcParam, MUST appear in order */
        poff = off + sizeof(uint16_t);
 
        /* ALPN */
        dns_resolver_transports_to_strv(res->transports, &alpns);
 
        /* res needs to have at least one valid transport */
        if (strv_isempty(alpns))
                return -EINVAL;
 
        plen = 0;
        STRV_FOREACH(alpn, alpns)
                plen += sizeof(uint8_t) + strlen(*alpn);
 
        if (!GREEDY_REALLOC(buf, poff + 2 * sizeof(uint16_t) + plen))
                return -ENOMEM;
 
        unaligned_write_be16(buf + poff, (uint16_t) DNS_SVC_PARAM_KEY_ALPN);
        poff += sizeof(uint16_t);
        unaligned_write_be16(buf + poff, plen);
        poff += sizeof(uint16_t);
 
        STRV_FOREACH(alpn, alpns) {
                size_t alen = strlen(*alpn);
                buf[poff++] = alen;
                memcpy(buf + poff, *alpn, alen);
                poff += alen;
        }
 
        /* port */
        if (res->port > 0) {
                plen = 2;
                if (!GREEDY_REALLOC(buf, poff + 2 * sizeof(uint16_t) + plen))
                        return -ENOMEM;
 
                unaligned_write_be16(buf + poff, (uint16_t) DNS_SVC_PARAM_KEY_PORT);
                poff += sizeof(uint16_t);
                unaligned_write_be16(buf + poff, plen);
                poff += sizeof(uint16_t);
                unaligned_write_be16(buf + poff, res->port);
                poff += sizeof(uint16_t);
        }
 
        /* dohpath */
        if (res->dohpath) {
                plen = strlen(res->dohpath);
                if (!GREEDY_REALLOC(buf, poff + 2 * sizeof(uint16_t) + plen))
                        return -ENOMEM;
 
                unaligned_write_be16(buf + poff, (uint16_t) DNS_SVC_PARAM_KEY_DOHPATH);
                poff += sizeof(uint16_t);
                unaligned_write_be16(buf + poff, plen);
                poff += sizeof(uint16_t);
                memcpy(buf + poff, res->dohpath, plen);
                poff += plen;
        }
 
        unaligned_write_be16(buf + off, LESS_BY(poff, off + sizeof(uint16_t)));
        off = poff;
 
padding:
        len = DIV_ROUND_UP(off, 8);
        if (!GREEDY_REALLOC(buf, 8*len))
                return -ENOMEM;
        memzero(buf + off, 8*len - off);
 
        buf[1] = len;
        *ret = TAKE_PTR(buf);
        return 0;
}
 
static int ndisc_option_parse_default(Set **options, size_t offset, size_t len, const uint8_t *opt) {
        assert(options);
        assert(opt);
        assert(len > 0);
 
        sd_ndisc_option *p = ndisc_option_new(opt[0], offset);
        if (!p)
                return -ENOMEM;
 
        return ndisc_option_consume(options, p);
}
 
static int ndisc_header_size(uint8_t icmp6_type) {
        switch (icmp6_type) {
        case ND_ROUTER_SOLICIT:
                return sizeof(struct nd_router_solicit);
        case ND_ROUTER_ADVERT:
                return sizeof(struct nd_router_advert);
        case ND_NEIGHBOR_SOLICIT:
                return sizeof(struct nd_neighbor_solicit);
        case ND_NEIGHBOR_ADVERT:
                return sizeof(struct nd_neighbor_advert);
        case ND_REDIRECT:
                return sizeof(struct nd_redirect);
        default:
                return -EINVAL;
        }
}
 
int ndisc_parse_options(ICMP6Packet *packet, Set **ret_options) {
        _cleanup_set_free_ Set *options = NULL;
        int r;
 
        assert(packet);
        assert(ret_options);
 
        r = icmp6_packet_get_type(packet);
        if (r < 0)
                return r;
 
        r = ndisc_header_size(r);
        if (r < 0)
                return -EBADMSG;
        size_t header_size = r;
 
        if (packet->raw_size < header_size)
                return -EBADMSG;
 
        for (size_t length, offset = header_size; offset < packet->raw_size; offset += length) {
                uint8_t type;
                const uint8_t *opt;
 
                r = ndisc_option_parse(packet, offset, &type, &length, &opt);
                if (r < 0)
                        return log_debug_errno(r, "Failed to parse NDisc option header: %m");
 
                switch (type) {
                case 0:
                        r = -EBADMSG;
                        break;
 
                case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
                case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
                        r = ndisc_option_parse_link_layer_address(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_PREFIX_INFORMATION:
                        r = ndisc_option_parse_prefix(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_REDIRECTED_HEADER:
                        r = ndisc_option_parse_redirected_header(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_MTU:
                        r = ndisc_option_parse_mtu(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_HOME_AGENT:
                        r = ndisc_option_parse_home_agent(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_ROUTE_INFORMATION:
                        r = ndisc_option_parse_route(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_RDNSS:
                        r = ndisc_option_parse_rdnss(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_FLAGS_EXTENSION:
                        r = ndisc_option_parse_flags_extension(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_DNSSL:
                        r = ndisc_option_parse_dnssl(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_CAPTIVE_PORTAL:
                        r = ndisc_option_parse_captive_portal(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_PREF64:
                        r = ndisc_option_parse_prefix64(&options, offset, length, opt);
                        break;
 
                case SD_NDISC_OPTION_ENCRYPTED_DNS:
                        r = ndisc_option_parse_encrypted_dns(&options, offset, length, opt);
                        break;
 
                default:
                        r = ndisc_option_parse_default(&options, offset, length, opt);
                }
                if (r == -ENOMEM)
                        return log_oom_debug();
                if (r < 0)
                        log_debug_errno(r, "Failed to parse NDisc option %u, ignoring: %m", type);
        }
 
        *ret_options = TAKE_PTR(options);
        return 0;
}
 
sd_ndisc_option* ndisc_option_get(Set *options, const sd_ndisc_option *p) {
        return set_get(options, ASSERT_PTR(p));
}
 
sd_ndisc_option* ndisc_option_get_by_type(Set *options, uint8_t type) {
        return ndisc_option_get(options, &(const sd_ndisc_option) { .type = type });
}
 
int ndisc_option_get_mac(Set *options, uint8_t type, struct ether_addr *ret) {
        assert(IN_SET(type, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, SD_NDISC_OPTION_TARGET_LL_ADDRESS));
 
        sd_ndisc_option *p = ndisc_option_get_by_type(options, type);
        if (!p)
                return -ENODATA;
 
        if (ret)
                *ret = p->mac;
        return 0;
}
 
void ndisc_option_remove(Set *options, const sd_ndisc_option *p) {
        ndisc_option_free(set_remove(options, ASSERT_PTR(p)));
}
 
int ndisc_send(int fd, const struct in6_addr *dst, const struct icmp6_hdr *hdr, Set *options, usec_t timestamp) {
        int r;
 
        assert(fd >= 0);
        assert(dst);
        assert(hdr);
 
        size_t n;
        _cleanup_free_ sd_ndisc_option **list = NULL;
        r = set_dump_sorted(options, (void***) &list, &n);
        if (r < 0)
                return r;
 
        struct iovec *iov = NULL;
        size_t n_iov = 0;
        CLEANUP_ARRAY(iov, n_iov, iovec_array_free);
 
        iov = new(struct iovec, 1 + n);
        if (!iov)
                return -ENOMEM;
 
        r = ndisc_header_size(hdr->icmp6_type);
        if (r < 0)
                return r;
        size_t hdr_size = r;
 
        _cleanup_free_ uint8_t *copy = newdup(uint8_t, hdr, hdr_size);
        if (!copy)
                return -ENOMEM;
 
        iov[n_iov++] = IOVEC_MAKE(TAKE_PTR(copy), hdr_size);
 
        FOREACH_ARRAY(p, list, n) {
                _cleanup_free_ uint8_t *buf = NULL;
                sd_ndisc_option *option = *p;
 
                switch (option->type) {
                case 0:
                        r = ndisc_option_build_raw(option, &buf);
                        break;
 
                case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
                case SD_NDISC_OPTION_TARGET_LL_ADDRESS:
                        r = ndisc_option_build_link_layer_address(option, &buf);
                        break;
 
                case SD_NDISC_OPTION_PREFIX_INFORMATION:
                        r = ndisc_option_build_prefix(option, timestamp, &buf);
                        break;
 
                case SD_NDISC_OPTION_REDIRECTED_HEADER:
                        r = ndisc_option_build_redirected_header(option, &buf);
                        break;
 
                case SD_NDISC_OPTION_MTU:
                        r = ndisc_option_build_mtu(option, &buf);
                        break;
 
                case SD_NDISC_OPTION_HOME_AGENT:
                        r = ndisc_option_build_home_agent(option, timestamp, &buf);
                        break;
 
                case SD_NDISC_OPTION_ROUTE_INFORMATION:
                        r = ndisc_option_build_route(option, timestamp, &buf);
                        break;
 
                case SD_NDISC_OPTION_RDNSS:
                        r = ndisc_option_build_rdnss(option, timestamp, &buf);
                        break;
 
                case SD_NDISC_OPTION_FLAGS_EXTENSION:
                        r = ndisc_option_build_flags_extension(option, &buf);
                        break;
 
                case SD_NDISC_OPTION_DNSSL:
                        r = ndisc_option_build_dnssl(option, timestamp, &buf);
                        break;
 
                case SD_NDISC_OPTION_CAPTIVE_PORTAL:
                        r = ndisc_option_build_captive_portal(option, &buf);
                        break;
 
                case SD_NDISC_OPTION_PREF64:
                        r = ndisc_option_build_prefix64(option, timestamp, &buf);
                        break;
 
                case SD_NDISC_OPTION_ENCRYPTED_DNS:
                        r = ndisc_option_build_encrypted_dns(option, timestamp, &buf);
                        break;
 
                default:
                        continue;
                }
                if (r == -ENOMEM)
                        return log_oom_debug();
                if (r < 0) {
                        log_debug_errno(r, "Failed to build NDisc option %u, ignoring: %m", option->type);
                        continue;
                }
 
                iov[n_iov++] = IOVEC_MAKE(buf, buf[1] * 8);
                TAKE_PTR(buf);
        }
 
        return icmp6_send(fd, dst, iov, n_iov);
}