From ae25915d24e2c650bf079efa3d03f1d47795bd16 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 21:53:26 +0100 Subject: sd-radv: use ether_addr_is_null() where appropriate --- src/libsystemd-network/sd-radv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index ffe22faac3..a7bc422db0 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -12,6 +12,7 @@ #include "macro.h" #include "alloc-util.h" #include "dns-domain.h" +#include "ether-addr-util.h" #include "event-util.h" #include "fd-util.h" #include "icmp6-util.h" @@ -114,7 +115,6 @@ DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv, sd_radv, radv_free); static int radv_send(sd_radv *ra, const struct in6_addr *dst, const uint32_t router_lifetime) { - static const struct ether_addr mac_zero = {}; sd_radv_prefix *p; struct sockaddr_in6 dst_addr = { .sin6_family = AF_INET6, @@ -163,7 +163,7 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, /* MAC address is optional, either because the link does not use L2 addresses or load sharing is desired. See RFC 4861, Section 4.2 */ - if (memcmp(&mac_zero, &ra->mac_addr, sizeof(mac_zero))) { + if (!ether_addr_is_null(&ra->mac_addr)) { opt_mac.slladdr = ra->mac_addr; iov[msg.msg_iovlen].iov_base = &opt_mac; iov[msg.msg_iovlen].iov_len = sizeof(opt_mac); -- cgit v1.2.3 From 83ef9d141bc15f614993cf04624c44ac1848c2fe Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 21:54:31 +0100 Subject: sd-radv: drop const from non-ptr argument --- src/libsystemd-network/sd-radv.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index a7bc422db0..e853dc8be6 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -113,8 +113,7 @@ static sd_radv *radv_free(sd_radv *ra) { DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv, sd_radv, radv_free); -static int radv_send(sd_radv *ra, const struct in6_addr *dst, - const uint32_t router_lifetime) { +static int radv_send(sd_radv *ra, const struct in6_addr *dst, uint32_t router_lifetime) { sd_radv_prefix *p; struct sockaddr_in6 dst_addr = { .sin6_family = AF_INET6, -- cgit v1.2.3 From dc0ec5e25189125c4f3695b2550159d82c24a70d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 21:54:52 +0100 Subject: sd-radv: simplify IPv6 address is-null check --- src/libsystemd-network/sd-radv.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index e853dc8be6..bedc53e2bd 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -145,11 +145,13 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, uint32_t router_li usec_t time_now; int r; + assert(ra); + r = sd_event_now(ra->event, clock_boottime_or_monotonic(), &time_now); if (r < 0) return r; - if (dst && !in_addr_is_null(AF_INET6, (union in_addr_union*) dst)) + if (dst && !IN6_IS_ADDR_UNSPECIFIED(dst)) dst_addr.sin6_addr = *dst; adv.nd_ra_type = ND_ROUTER_ADVERT; -- cgit v1.2.3 From e866e17b0f818591a6a0725197287a3e8740eef0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 22:09:57 +0100 Subject: sd-radv: follow our usualy rule that destructors can take NULL --- src/libsystemd-network/sd-radv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index bedc53e2bd..b45e80e4c1 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -88,7 +88,8 @@ static void radv_reset(sd_radv *ra) { } static sd_radv *radv_free(sd_radv *ra) { - assert(ra); + if (!ra) + return NULL; while (ra->prefixes) { sd_radv_prefix *p = ra->prefixes; -- cgit v1.2.3 From e55a6eaeb93b9df0a4a0ef68c03dc12c4630a67f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 22:11:12 +0100 Subject: sd-radv: propagate OOM, it's reason to fail --- src/libsystemd-network/sd-radv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index b45e80e4c1..f256cbc984 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -235,7 +235,7 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat buf = new0(char, buflen); if (!buf) - return 0; + return -ENOMEM; r = icmp6_receive(fd, buf, buflen, &src, ×tamp); if (r < 0) { -- cgit v1.2.3 From cfffddeac504190914dc41f7fe5c7cb42bdc1ea6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 22:12:07 +0100 Subject: sd-radv: when receiving a too short packet, make sure we dequeue it --- src/libsystemd-network/sd-radv.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index f256cbc984..7b51f28ff2 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -229,9 +229,8 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat assert(ra->event); buflen = next_datagram_size_fd(fd); - - if ((unsigned) buflen < sizeof(struct nd_router_solicit)) - return log_radv("Too short packet received"); + if (buflen < 0) + return (int) buflen; buf = new0(char, buflen); if (!buf) @@ -264,6 +263,11 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat return 0; } + if ((size_t) buflen < sizeof(struct nd_router_solicit)) { + log_radv("Too short packet received"); + return 0; + } + (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &src, &addr); r = radv_send(ra, &src, ra->lifetime); -- cgit v1.2.3 From 5a9fca258bd03cc7cd8d4fb277b6f4fddac2310e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 22:12:38 +0100 Subject: sd-radv: make sure we can deal with in_addr_to_string() failing --- src/libsystemd-network/sd-radv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 7b51f28ff2..33bf4cf79f 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -272,9 +272,9 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat r = radv_send(ra, &src, ra->lifetime); if (r < 0) - log_radv_errno(r, "Unable to send solicited Router Advertisement to %s: %m", addr); + log_radv_errno(r, "Unable to send solicited Router Advertisement to %s: %m", strnull(addr)); else - log_radv("Sent solicited Router Advertisement to %s", addr); + log_radv("Sent solicited Router Advertisement to %s", strnull(addr)); return 0; } -- cgit v1.2.3 From f474884cb94bca63fe2d58792d85df8c57b7bb60 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 22:12:53 +0100 Subject: sd-radv: no need to initialize r to zero --- src/libsystemd-network/sd-radv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 33bf4cf79f..faf353e866 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -359,7 +359,7 @@ _public_ int sd_radv_stop(sd_radv *ra) { } _public_ int sd_radv_start(sd_radv *ra) { - int r = 0; + int r; assert_return(ra, -EINVAL); assert_return(ra->event, -EINVAL); -- cgit v1.2.3 From a3a98776ff8ee413690880cf371e10ccf14cdbb8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 22:13:15 +0100 Subject: sd-radv: per our CODING_STYLE don't use 'bool' in public headers --- src/libsystemd-network/sd-radv.c | 2 +- src/systemd/sd-radv.h | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index faf353e866..acca734659 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -503,7 +503,7 @@ _public_ int sd_radv_set_preference(sd_radv *ra, unsigned preference) { return r; } -_public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, bool dynamic) { +_public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) { sd_radv_prefix *cur; int r; _cleanup_free_ char *addr_p = NULL; diff --git a/src/systemd/sd-radv.h b/src/systemd/sd-radv.h index 53c507d88c..93861b9d24 100644 --- a/src/systemd/sd-radv.h +++ b/src/systemd/sd-radv.h @@ -22,14 +22,11 @@ #include #include #include -#include #include -#include "sd-ndisc.h" - -#include "sd-event.h" - #include "_sd-common.h" +#include "sd-event.h" +#include "sd-ndisc.h" _SD_BEGIN_DECLARATIONS; @@ -61,7 +58,7 @@ int sd_radv_set_router_lifetime(sd_radv *ra, uint32_t router_lifetime); 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, bool dynamic); +int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic); sd_radv_prefix *sd_radv_remove_prefix(sd_radv *ra, const struct in6_addr *prefix, unsigned char prefixlen); int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime, -- cgit v1.2.3 From d2c8eed219ec14ad6e4513c0f366b6623a14291b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 26 Nov 2018 22:13:36 +0100 Subject: sd-radv: use struct initializers --- src/libsystemd-network/sd-radv.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index acca734659..4df7f273ce 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -698,31 +698,29 @@ _public_ int sd_radv_set_dnssl(sd_radv *ra, uint32_t lifetime, } _public_ int sd_radv_prefix_new(sd_radv_prefix **ret) { - _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL; + sd_radv_prefix *p; assert_return(ret, -EINVAL); - p = new0(sd_radv_prefix, 1); + p = new(sd_radv_prefix, 1); if (!p) return -ENOMEM; - p->n_ref = 1; - - p->opt.type = ND_OPT_PREFIX_INFORMATION; - p->opt.length = (sizeof(p->opt) - 1) /8 + 1; - - p->opt.prefixlen = 64; + *p = (sd_radv_prefix) { + .n_ref = 1, - /* RFC 4861, Section 6.2.1 */ - SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_ONLINK, true); - SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_AUTO, true); - p->opt.preferred_lifetime = htobe32(604800); - p->opt.valid_lifetime = htobe32(2592000); + .opt.type = ND_OPT_PREFIX_INFORMATION, + .opt.length = (sizeof(p->opt) - 1)/8 + 1, + .opt.prefixlen = 64, - LIST_INIT(prefix, p); + /* RFC 4861, Section 6.2.1 */ + .opt.flags = ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO, - *ret = TAKE_PTR(p); + .opt.preferred_lifetime = htobe32(604800), + .opt.valid_lifetime = htobe32(2592000), + }; + *ret = p; return 0; } -- cgit v1.2.3