]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-radv: make sd_radv_set_router_lifetime() take usec_t (uint64_t)
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 24 Oct 2021 15:15:06 +0000 (00:15 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 27 Oct 2021 07:13:09 +0000 (16:13 +0900)
src/libsystemd-network/radv-internal.h
src/libsystemd-network/sd-radv.c
src/network/networkd-radv.c
src/systemd/sd-radv.h

index ef566ed8ec2fa65dac9d1655dd1753c75e1e302f..21a8e042a0863828c284def092eae5b1ed18ae16 100644 (file)
@@ -10,6 +10,7 @@
 #include "list.h"
 #include "network-common.h"
 #include "sparse-endian.h"
+#include "time-util.h"
 
 assert_cc(SD_RADV_DEFAULT_MIN_TIMEOUT_USEC <= SD_RADV_DEFAULT_MAX_TIMEOUT_USEC);
 
@@ -50,7 +51,7 @@ struct sd_radv {
         uint8_t hop_limit;
         uint8_t flags;
         uint32_t mtu;
-        uint16_t lifetime;
+        usec_t lifetime_usec; /* timespan */
 
         int fd;
         unsigned ra_sent;
index 2d91a406f836f8367f9b6ce842effd32cffec362..d4958b0f08a0acaba5265bfa08fad713b9e0ef8c 100644 (file)
@@ -128,7 +128,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, uint32_t router_lifetime) {
+static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_usec) {
         sd_radv_route_prefix *rt;
         sd_radv_prefix *p;
         struct sockaddr_in6 dst_addr = {
@@ -158,6 +158,7 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, uint32_t router_li
                 .msg_namelen = sizeof(dst_addr),
                 .msg_iov = iov,
         };
+        uint16_t lifetime_sec;
         usec_t time_now;
         int r;
 
@@ -167,13 +168,21 @@ static int radv_send(sd_radv *ra, const struct in6_addr *dst, uint32_t router_li
         if (r < 0)
                 return r;
 
+        /* a value of UINT16_MAX represents infinity, 0x0 means this host is not a router */
+        if (lifetime_usec == USEC_INFINITY)
+                lifetime_sec = UINT16_MAX;
+        else if (lifetime_usec > (UINT16_MAX - 1) * USEC_PER_SEC)
+                lifetime_sec = UINT16_MAX - 1;
+        else
+                lifetime_sec = DIV_ROUND_UP(lifetime_usec, USEC_PER_SEC);
+
         if (dst && in6_addr_is_set(dst))
                 dst_addr.sin6_addr = *dst;
 
         adv.nd_ra_type = ND_ROUTER_ADVERT;
         adv.nd_ra_curhoplimit = ra->hop_limit;
         adv.nd_ra_flags_reserved = ra->flags;
-        adv.nd_ra_router_lifetime = htobe16(router_lifetime);
+        adv.nd_ra_router_lifetime = htobe16(lifetime_sec);
         iov[msg.msg_iovlen++] = IOVEC_MAKE(&adv, sizeof(adv));
 
         /* MAC address is optional, either because the link does not use L2
@@ -274,7 +283,7 @@ static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdat
 
         (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr);
 
-        r = radv_send(ra, &src, ra->lifetime);
+        r = radv_send(ra, &src, ra->lifetime_usec);
         if (r < 0)
                 log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", strnull(addr));
         else
@@ -294,11 +303,9 @@ static usec_t radv_compute_timeout(usec_t min, usec_t max) {
 }
 
 static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
-        int r;
+        usec_t min_timeout, max_timeout, time_now, timeout;
         sd_radv *ra = userdata;
-        usec_t min_timeout = SD_RADV_DEFAULT_MIN_TIMEOUT_USEC;
-        usec_t max_timeout = SD_RADV_DEFAULT_MAX_TIMEOUT_USEC;
-        usec_t time_now, timeout;
+        int r;
 
         assert(s);
         assert(ra);
@@ -308,7 +315,7 @@ static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
         if (r < 0)
                 goto fail;
 
-        r = radv_send(ra, NULL, ra->lifetime);
+        r = radv_send(ra, NULL, ra->lifetime_usec);
         if (r < 0)
                 log_radv_errno(ra, r, "Unable to send Router Advertisement: %m");
 
@@ -316,12 +323,15 @@ static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
         if (ra->ra_sent < SD_RADV_MAX_INITIAL_RTR_ADVERTISEMENTS) {
                 max_timeout = SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC;
                 min_timeout = SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC / 3;
+        } else {
+                max_timeout = SD_RADV_DEFAULT_MAX_TIMEOUT_USEC;
+                min_timeout = SD_RADV_DEFAULT_MIN_TIMEOUT_USEC;
         }
 
         /* RFC 4861, Section 6.2.1, lifetime must be at least MaxRtrAdvInterval,
            so lower the interval here */
-        if (ra->lifetime > 0 && (ra->lifetime * USEC_PER_SEC) < max_timeout) {
-                max_timeout = ra->lifetime * USEC_PER_SEC;
+        if (ra->lifetime_usec > 0 && ra->lifetime_usec < max_timeout) {
+                max_timeout = ra->lifetime_usec;
                 min_timeout = max_timeout / 3;
         }
 
@@ -487,7 +497,7 @@ _public_ int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit) {
         return 0;
 }
 
-_public_ int sd_radv_set_router_lifetime(sd_radv *ra, uint16_t router_lifetime) {
+_public_ int sd_radv_set_router_lifetime(sd_radv *ra, uint64_t lifetime_usec) {
         assert_return(ra, -EINVAL);
 
         if (ra->state != SD_RADV_STATE_IDLE)
@@ -495,11 +505,11 @@ _public_ int sd_radv_set_router_lifetime(sd_radv *ra, uint16_t router_lifetime)
 
         /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the preference value MUST be set
          * to (00) by the sender..." */
-        if (router_lifetime == 0 &&
+        if (lifetime_usec == 0 &&
             (ra->flags & (0x3 << 3)) != (SD_NDISC_PREFERENCE_MEDIUM << 3))
                 return -ETIME;
 
-        ra->lifetime = router_lifetime;
+        ra->lifetime_usec = lifetime_usec;
 
         return 0;
 }
@@ -535,7 +545,7 @@ _public_ int sd_radv_set_preference(sd_radv *ra, unsigned preference) {
 
         /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the preference value MUST be set
          * to (00) by the sender..." */
-        if (ra->lifetime == 0 && preference != SD_NDISC_PREFERENCE_MEDIUM)
+        if (ra->lifetime_usec == 0 && preference != SD_NDISC_PREFERENCE_MEDIUM)
                 return -EINVAL;
 
         ra->flags = (ra->flags & ~(0x3 << 3)) | (preference << 3);
@@ -601,7 +611,7 @@ _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p, int dynamic) {
 
         /* If RAs have already been sent, send an RA immediately to announce the newly-added prefix */
         if (ra->ra_sent > 0) {
-                r = radv_send(ra, NULL, ra->lifetime);
+                r = radv_send(ra, NULL, ra->lifetime_usec);
                 if (r < 0)
                         log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix: %m");
                 else
@@ -710,7 +720,7 @@ _public_ int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p, int
 
         /* If RAs have already been sent, send an RA immediately to announce the newly-added route prefix */
         if (ra->ra_sent > 0) {
-                r = radv_send(ra, NULL, ra->lifetime);
+                r = radv_send(ra, NULL, ra->lifetime_usec);
                 if (r < 0)
                         log_radv_errno(ra, r, "Unable to send Router Advertisement for added route prefix: %m");
                 else
index a290b3730afc5b6fc4d76e4281011711cf7a0911..7b92e6c4f963f51603b7a6b6e634cbba097ffb70 100644 (file)
@@ -429,7 +429,6 @@ static int radv_find_uplink(Link *link, Link **ret) {
 }
 
 static int radv_configure(Link *link) {
-        uint16_t router_lifetime;
         Link *uplink = NULL;
         RoutePrefix *q;
         Prefix *p;
@@ -465,19 +464,11 @@ static int radv_configure(Link *link) {
         if (r < 0)
                 return r;
 
-        /* a value of UINT16_MAX represents infinity, 0x0 means this host is not a router */
-        if (link->network->router_lifetime_usec == USEC_INFINITY)
-                router_lifetime = UINT16_MAX;
-        else if (link->network->router_lifetime_usec > (UINT16_MAX - 1) * USEC_PER_SEC)
-                router_lifetime = UINT16_MAX - 1;
-        else
-                router_lifetime = DIV_ROUND_UP(link->network->router_lifetime_usec, USEC_PER_SEC);
-
-        r = sd_radv_set_router_lifetime(link->radv, router_lifetime);
+        r = sd_radv_set_router_lifetime(link->radv, link->network->router_lifetime_usec);
         if (r < 0)
                 return r;
 
-        if (router_lifetime > 0) {
+        if (link->network->router_lifetime_usec > 0) {
                 r = sd_radv_set_preference(link->radv, link->network->router_preference);
                 if (r < 0)
                         return r;
index f597c2c33daaf2714ebf7629e82b29debac8edb5..658c87fa45030298206332c83a8fbb02f78aee84 100644 (file)
@@ -58,7 +58,7 @@ int sd_radv_get_ifname(sd_radv *ra, const char **ret);
 int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr);
 int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu);
 int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit);
-int sd_radv_set_router_lifetime(sd_radv *ra, uint16_t router_lifetime);
+int sd_radv_set_router_lifetime(sd_radv *ra, uint64_t lifetime_usec);
 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);