]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd-network/sd-radv.c
sd-radv: follow our usualy rule that destructors can take NULL
[thirdparty/systemd.git] / src / libsystemd-network / sd-radv.c
index f30d6164eafb745bb10dcb4c95d59c3da6542916..b45e80e4c167f212df1ee32bc4c156efdb91a508 100644 (file)
@@ -1,21 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 /***
-  This file is part of systemd.
-
-  Copyright (C) 2017 Intel Corporation. All rights reserved.
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+  Copyright © 2017 Intel Corporation. All rights reserved.
 ***/
 
 #include <netinet/icmp6.h>
@@ -27,6 +12,8 @@
 #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"
 #include "in-addr-util.h"
@@ -42,17 +29,16 @@ _public_ int sd_radv_new(sd_radv **ret) {
 
         assert_return(ret, -EINVAL);
 
-        ra = new0(sd_radv, 1);
+        ra = new(sd_radv, 1);
         if (!ra)
                 return -ENOMEM;
 
-        ra->n_ref = 1;
-        ra->fd = -1;
-
-        LIST_HEAD_INIT(ra->prefixes);
+        *ra = (sd_radv) {
+                .n_ref = 1,
+                .fd = -1,
+        };
 
-        *ret = ra;
-        ra = NULL;
+        *ret = TAKE_PTR(ra);
 
         return 0;
 }
@@ -91,9 +77,9 @@ _public_ sd_event *sd_radv_get_event(sd_radv *ra) {
 }
 
 static void radv_reset(sd_radv *ra) {
+        assert(ra);
 
-        ra->timeout_event_source =
-                sd_event_source_unref(ra->timeout_event_source);
+        (void) event_source_disable(ra->timeout_event_source);
 
         ra->recv_event_source =
                 sd_event_source_unref(ra->recv_event_source);
@@ -101,26 +87,10 @@ static void radv_reset(sd_radv *ra) {
         ra->ra_sent = 0;
 }
 
-_public_ sd_radv *sd_radv_ref(sd_radv *ra) {
+static sd_radv *radv_free(sd_radv *ra) {
         if (!ra)
                 return NULL;
 
-        assert(ra->n_ref > 0);
-        ra->n_ref++;
-
-        return ra;
-}
-
-_public_ sd_radv *sd_radv_unref(sd_radv *ra) {
-        if (!ra)
-                return NULL;
-
-        assert(ra->n_ref > 0);
-        ra->n_ref--;
-
-        if (ra->n_ref > 0)
-                return NULL;
-
         while (ra->prefixes) {
                 sd_radv_prefix *p = ra->prefixes;
 
@@ -131,15 +101,20 @@ _public_ sd_radv *sd_radv_unref(sd_radv *ra) {
         free(ra->rdnss);
         free(ra->dnssl);
 
+        ra->timeout_event_source = sd_event_source_unref(ra->timeout_event_source);
+
         radv_reset(ra);
 
         sd_radv_detach_event(ra);
+
+        ra->fd = safe_close(ra->fd);
+
         return mfree(ra);
 }
 
-static int radv_send(sd_radv *ra, const struct in6_addr *dst,
-                     const uint32_t router_lifetime) {
-        static const struct ether_addr mac_zero = {};
+DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv, sd_radv, radv_free);
+
+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,
@@ -171,11 +146,13 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst,
         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;
@@ -188,7 +165,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);
@@ -276,8 +253,11 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat
                         log_radv("Received invalid source address from ICMPv6 socket. Ignoring.");
                         break;
 
+                case -EAGAIN: /* ignore spurious wakeups */
+                        break;
+
                 default:
-                        log_radv_warning_errno(r, "Error receiving from ICMPv6 socket: %m");
+                        log_radv_errno(r, "Unexpected error receiving from ICMPv6 socket: %m");
                         break;
                 }
 
@@ -288,7 +268,7 @@ 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_warning_errno(r, "Unable to send solicited Router Advertisment to %s: %m", addr);
+                log_radv_errno(r, "Unable to send solicited Router Advertisement to %s: %m", addr);
         else
                 log_radv("Sent solicited Router Advertisement to %s", addr);
 
@@ -313,15 +293,13 @@ static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
         assert(ra);
         assert(ra->event);
 
-        ra->timeout_event_source = sd_event_source_unref(ra->timeout_event_source);
-
         r = sd_event_now(ra->event, clock_boottime_or_monotonic(), &time_now);
         if (r < 0)
                 goto fail;
 
         r = radv_send(ra, NULL, ra->lifetime);
         if (r < 0)
-                log_radv_warning_errno(r, "Unable to send Router Advertisement: %m");
+                log_radv_errno(r, "Unable to send Router Advertisement: %m");
 
         /* RFC 4861, Section 6.2.4, sending initial Router Advertisements */
         if (ra->ra_sent < SD_RADV_MAX_INITIAL_RTR_ADVERTISEMENTS) {
@@ -335,28 +313,20 @@ static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
                  format_timespan(time_string, FORMAT_TIMESPAN_MAX,
                                  timeout, USEC_PER_SEC));
 
-        r = sd_event_add_time(ra->event, &ra->timeout_event_source,
-                              clock_boottime_or_monotonic(),
-                              time_now + timeout, MSEC_PER_SEC,
-                              radv_timeout, ra);
-        if (r < 0)
-                goto fail;
-
-        r = sd_event_source_set_priority(ra->timeout_event_source,
-                                         ra->event_priority);
-        if (r < 0)
-                goto fail;
-
-        r = sd_event_source_set_description(ra->timeout_event_source,
-                                            "radv-timeout");
+        r = event_reset_time(ra->event, &ra->timeout_event_source,
+                             clock_boottime_or_monotonic(),
+                             time_now + timeout, MSEC_PER_SEC,
+                             radv_timeout, ra,
+                             ra->event_priority, "radv-timeout", true);
         if (r < 0)
                 goto fail;
 
         ra->ra_sent++;
 
+        return 0;
+
 fail:
-        if (r < 0)
-                sd_radv_stop(ra);
+        sd_radv_stop(ra);
 
         return 0;
 }
@@ -366,13 +336,16 @@ _public_ int sd_radv_stop(sd_radv *ra) {
 
         assert_return(ra, -EINVAL);
 
+        if (ra->state == SD_RADV_STATE_IDLE)
+                return 0;
+
         log_radv("Stopping IPv6 Router Advertisement daemon");
 
         /* RFC 4861, Section 6.2.5, send at least one Router Advertisement
            with zero lifetime  */
         r = radv_send(ra, NULL, 0);
         if (r < 0)
-                log_radv_warning_errno(r, "Unable to send last Router Advertisement with router lifetime set to zero: %m");
+                log_radv_errno(r, "Unable to send last Router Advertisement with router lifetime set to zero: %m");
 
         radv_reset(ra);
         ra->fd = safe_close(ra->fd);
@@ -391,20 +364,14 @@ _public_ int sd_radv_start(sd_radv *ra) {
         if (ra->state != SD_RADV_STATE_IDLE)
                 return 0;
 
-        r = sd_event_add_time(ra->event, &ra->timeout_event_source,
-                              clock_boottime_or_monotonic(), 0, 0,
-                              radv_timeout, ra);
-        if (r < 0)
-                goto fail;
-
-        r = sd_event_source_set_priority(ra->timeout_event_source,
-                                         ra->event_priority);
+        r = event_reset_time(ra->event, &ra->timeout_event_source,
+                             clock_boottime_or_monotonic(),
+                             0, 0,
+                             radv_timeout, ra,
+                             ra->event_priority, "radv-timeout", true);
         if (r < 0)
                 goto fail;
 
-        (void) sd_event_source_set_description(ra->timeout_event_source,
-                                               "radv-timeout");
-
         r = icmp6_bind_router_advertisement(ra->ifindex);
         if (r < 0)
                 goto fail;
@@ -618,8 +585,8 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, bool dynamic) {
 }
 
 _public_ sd_radv_prefix *sd_radv_remove_prefix(sd_radv *ra,
-                                               struct in6_addr *prefix,
-                                               uint8_t prefixlen) {
+                                               const struct in6_addr *prefix,
+                                               unsigned char prefixlen) {
         sd_radv_prefix *cur, *next;
 
         assert_return(ra, NULL);
@@ -670,9 +637,7 @@ _public_ int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime,
 
         memcpy(opt_rdnss + 1, dns, n_dns * sizeof(struct in6_addr));
 
-        free(ra->rdnss);
-        ra->rdnss = opt_rdnss;
-        opt_rdnss = NULL;
+        free_and_replace(ra->rdnss, opt_rdnss);
 
         ra->n_rdnss = n_dns;
 
@@ -688,9 +653,8 @@ _public_ int sd_radv_set_dnssl(sd_radv *ra, uint32_t lifetime,
 
         assert_return(ra, -EINVAL);
 
-        if (!search_list || *search_list == NULL) {
+        if (strv_isempty(search_list)) {
                 ra->dnssl = mfree(ra->dnssl);
-
                 return 0;
         }
 
@@ -724,9 +688,7 @@ _public_ int sd_radv_set_dnssl(sd_radv *ra, uint32_t lifetime,
                 len -= r;
         }
 
-        free(ra->dnssl);
-        ra->dnssl = opt_dnssl;
-        opt_dnssl = NULL;
+        free_and_replace(ra->dnssl, opt_dnssl);
 
         return 0;
 }
@@ -755,36 +717,14 @@ _public_ int sd_radv_prefix_new(sd_radv_prefix **ret) {
 
         LIST_INIT(prefix, p);
 
-        *ret = p;
-        p = NULL;
+        *ret = TAKE_PTR(p);
 
         return 0;
 }
 
-_public_ sd_radv_prefix *sd_radv_prefix_ref(sd_radv_prefix *p) {
-        if (!p)
-                return NULL;
-
-        assert(p->n_ref > 0);
-        p->n_ref++;
-
-        return p;
-}
-
-_public_ sd_radv_prefix *sd_radv_prefix_unref(sd_radv_prefix *p) {
-        if (!p)
-                return NULL;
-
-        assert(p->n_ref > 0);
-        p->n_ref--;
-
-        if (p->n_ref > 0)
-                return NULL;
-
-        return mfree(p);
-}
+DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_prefix, sd_radv_prefix, mfree);
 
-_public_ int sd_radv_prefix_set_prefix(sd_radv_prefix *p, struct in6_addr *in6_addr,
+_public_ int sd_radv_prefix_set_prefix(sd_radv_prefix *p, const struct in6_addr *in6_addr,
                                        unsigned char prefixlen) {
         assert_return(p, -EINVAL);
         assert_return(in6_addr, -EINVAL);