From e9c6da386849a6d262c1d605be911922f2cb7428 Mon Sep 17 00:00:00 2001 From: Patrik Flykt Date: Mon, 14 Aug 2017 12:53:11 +0300 Subject: [PATCH] sd-radv: Add Router Advertisement DNS information Add Router Advertisement Recursive DNS Server information as specified in RFC 8106. --- src/libsystemd-network/radv-internal.h | 12 +++++++++ src/libsystemd-network/sd-radv.c | 48 ++++++++++++++++++++++++++++++++-- src/systemd/sd-radv.h | 2 ++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/libsystemd-network/radv-internal.h b/src/libsystemd-network/radv-internal.h index b21d4e5..c3f847e 100644 --- a/src/libsystemd-network/radv-internal.h +++ b/src/libsystemd-network/radv-internal.h @@ -35,12 +35,21 @@ assert_cc(SD_RADV_DEFAULT_MIN_TIMEOUT_USEC <= SD_RADV_DEFAULT_MAX_TIMEOUT_USEC) #define SD_RADV_MIN_DELAY_BETWEEN_RAS 3 #define SD_RADV_MAX_RA_DELAY_TIME_USEC (500*USEC_PER_MSEC) +#define SD_RADV_OPT_RDNSS 25 + enum RAdvState { SD_RADV_STATE_IDLE = 0, SD_RADV_STATE_ADVERTISING = 1, }; typedef enum RAdvState RAdvState; +struct sd_radv_opt_dns { + uint8_t type; + uint8_t length; + uint16_t reserved; + be32_t lifetime; +} _packed_; + struct sd_radv { unsigned n_ref; RAdvState state; @@ -63,6 +72,9 @@ struct sd_radv { unsigned n_prefixes; LIST_HEAD(sd_radv_prefix, prefixes); + + size_t n_rdnss; + struct sd_radv_opt_dns *rdnss; }; struct sd_radv_prefix { diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index f23275a..70772b4 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -126,6 +126,8 @@ _public_ sd_radv *sd_radv_unref(sd_radv *ra) { sd_radv_prefix_unref(p); } + free(ra->rdnss); + radv_reset(ra); sd_radv_detach_event(ra); @@ -155,8 +157,8 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, .nd_opt_mtu_type = ND_OPT_MTU, .nd_opt_mtu_len = 1, }; - /* Reserve iov space for RA header, linkaddr, MTU + N prefixes */ - struct iovec iov[3 + ra->n_prefixes]; + /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, RDNSS */ + struct iovec iov[4 + ra->n_prefixes]; struct msghdr msg = { .msg_name = &dst_addr, .msg_namelen = sizeof(dst_addr), @@ -196,6 +198,12 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, msg.msg_iovlen++; } + if (ra->rdnss) { + iov[msg.msg_iovlen].iov_base = ra->rdnss; + iov[msg.msg_iovlen].iov_len = ra->rdnss->length * 8; + msg.msg_iovlen++; + } + if (sendmsg(ra->fd, &msg, 0) < 0) return -errno; @@ -546,6 +554,42 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) { return 0; } +_public_ int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime, + const struct in6_addr *dns, size_t n_dns) { + _cleanup_free_ struct sd_radv_opt_dns *opt_rdnss = NULL; + size_t len; + + assert_return(ra, -EINVAL); + assert_return(n_dns < 128, -EINVAL); + + if (!dns || n_dns == 0) { + ra->rdnss = mfree(ra->rdnss); + ra->n_rdnss = 0; + + return 0; + } + + len = sizeof(struct sd_radv_opt_dns) + sizeof(struct in6_addr) * n_dns; + + opt_rdnss = malloc0(len); + if (!opt_rdnss) + return -ENOMEM; + + opt_rdnss->type = SD_RADV_OPT_RDNSS; + opt_rdnss->length = len / 8; + opt_rdnss->lifetime = htobe32(lifetime); + + memcpy(opt_rdnss + 1, dns, n_dns * sizeof(struct in6_addr)); + + free(ra->rdnss); + ra->rdnss = opt_rdnss; + opt_rdnss = NULL; + + ra->n_rdnss = n_dns; + + return 0; +} + _public_ int sd_radv_prefix_new(sd_radv_prefix **ret) { _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL; diff --git a/src/systemd/sd-radv.h b/src/systemd/sd-radv.h index 4cbd80d..ce10ae0 100644 --- a/src/systemd/sd-radv.h +++ b/src/systemd/sd-radv.h @@ -57,6 +57,8 @@ int sd_radv_set_managed_information(sd_radv *ra, int managed); int sd_radv_set_other_information(sd_radv *ra, int other); int sd_radv_set_preference(sd_radv *ra, unsigned preference); int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p); +int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime, + const struct in6_addr *dns, size_t n_dns); /* Advertised prefixes */ int sd_radv_prefix_new(sd_radv_prefix **ret); -- 2.7.4