Merge pull request #6616 from pfl/rdnss

networkd: RDNSS option for systemd-networkd prefix delegation
This commit is contained in:
Lennart Poettering
2017-09-07 19:01:57 +02:00
committed by GitHub
9 changed files with 265 additions and 4 deletions
+12
View File
@@ -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 {
+46 -2
View File
@@ -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;
+19 -2
View File
@@ -53,7 +53,7 @@ static uint8_t advertisement[] = {
0x00, 0x09, 0x3a, 0x80, 0x00, 0x00, 0x00, 0x00,
0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x01, 0x0d, 0xad,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Recursive DNS Server Option - not yet supported */
/* Recursive DNS Server Option */
0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
@@ -106,6 +106,11 @@ static struct {
false },
};
static const struct in6_addr test_rdnss = { { { 0x20, 0x01, 0x0d, 0xb8,
0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01 } } };
static int test_rs_hangcheck(sd_event_source *s, uint64_t usec,
void *userdata) {
assert_se(false);
@@ -207,6 +212,14 @@ static void test_radv(void) {
assert_se(sd_radv_set_other_information(ra, true) >= 0);
assert_se(sd_radv_set_other_information(ra, false) >= 0);
assert_se(sd_radv_set_rdnss(NULL, 0, NULL, 0) < 0);
assert_se(sd_radv_set_rdnss(ra, 0, NULL, 0) >= 0);
assert_se(sd_radv_set_rdnss(ra, 0, NULL, 128) < 0);
assert_se(sd_radv_set_rdnss(ra, 600, &test_rdnss, 0) >= 0);
assert_se(sd_radv_set_rdnss(ra, 600, &test_rdnss, 1) >= 0);
assert_se(sd_radv_set_rdnss(ra, 0, &test_rdnss, 1) >= 0);
assert_se(sd_radv_set_rdnss(ra, 0, NULL, 0) >= 0);
ra = sd_radv_unref(ra);
assert_se(!ra);
}
@@ -238,7 +251,7 @@ int icmp6_receive(int fd, void *iov_base, size_t iov_len,
static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
sd_radv *ra = userdata;
unsigned char buf[120];
unsigned char buf[144];
size_t i;
read(test_fd[0], &buf, sizeof(buf));
@@ -254,6 +267,9 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat
/* test only up to buf size, rest is not yet implemented */
for (i = 0; i < sizeof(buf); i++) {
if (!(i % 8))
printf("%3zd: ", i);
printf("0x%02x", buf[i]);
assert_se(buf[i] == advertisement[i]);
@@ -302,6 +318,7 @@ static void test_ra(void) {
assert_se(sd_radv_set_hop_limit(ra, 64) >= 0);
assert_se(sd_radv_set_managed_information(ra, true) >= 0);
assert_se(sd_radv_set_other_information(ra, true) >= 0);
assert_se(sd_radv_set_rdnss(ra, 60, &test_rdnss, 1) >= 0);
for (i = 0; i < ELEMENTSOF(prefix); i++) {
sd_radv_prefix *p;