]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-dhcp6-lease: adjust information refresh time with lifetime of IA_PD
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 15 Sep 2024 20:02:06 +0000 (05:02 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 15 Oct 2024 09:23:59 +0000 (18:23 +0900)
For the case when IRT is too large but lifetime of IA_PD is too short.

src/libsystemd-network/sd-dhcp6-lease.c

index 3f8183c2c6fd5ba5fc981b6387323bc8b00edc38..261ab83c6489c89c479d788517c0f2a964f82285 100644 (file)
@@ -71,6 +71,32 @@ static void dhcp6_lease_set_lifetime(sd_dhcp6_lease *lease) {
         lease->lifetime_t2 = t2;
 }
 
+static void dhcp6_client_set_information_refresh_time(sd_dhcp6_client *client, sd_dhcp6_lease *lease, usec_t irt) {
+        usec_t t1 = USEC_INFINITY, t2 = USEC_INFINITY, min_valid_lt = USEC_INFINITY;
+
+        if (lease->ia_pd) {
+                t1 = be32_sec_to_usec(lease->ia_pd->header.lifetime_t1, /* max_as_infinity = */ true);
+                t2 = be32_sec_to_usec(lease->ia_pd->header.lifetime_t2, /* max_as_infinity = */ true);
+
+                LIST_FOREACH(addresses, a, lease->ia_pd->addresses)
+                        min_valid_lt = MIN(min_valid_lt, be32_sec_to_usec(a->iapdprefix.lifetime_valid, /* max_as_infinity = */ true));
+
+                if (t2 == 0 || t2 > min_valid_lt) {
+                        /* If T2 is zero or longer than the minimum valid lifetime of the prefixes,
+                         * then adjust lifetime with it. */
+                        t1 = min_valid_lt / 2;
+                        t2 = min_valid_lt / 10 * 8;
+                }
+
+                /* Adjust the received information refresh time with T1. */
+                irt = MIN(irt, t1);
+        }
+
+        client->information_refresh_time_usec = MAX(irt, IRT_MINIMUM);
+        log_dhcp6_client(client, "New information request will be refused in %s.",
+                         FORMAT_TIMESPAN(client->information_refresh_time_usec, USEC_PER_SEC));
+}
+
 #define DEFINE_GET_TIME_FUNCTIONS(name, val)                            \
         int sd_dhcp6_lease_get_##name(                                  \
                         sd_dhcp6_lease *lease,                          \
@@ -865,12 +891,9 @@ static int dhcp6_lease_parse_message(
                                               "The client ID in %s message does not match. Ignoring.",
                                               dhcp6_message_type_to_string(message->type));
 
-        if (client->state == DHCP6_STATE_INFORMATION_REQUEST) {
-                client->information_refresh_time_usec = MAX(irt, IRT_MINIMUM);
-                log_dhcp6_client(client, "New information request will be refused in %s.",
-                                 FORMAT_TIMESPAN(client->information_refresh_time_usec, USEC_PER_SEC));
-
-        } else {
+        if (client->state == DHCP6_STATE_INFORMATION_REQUEST)
+                dhcp6_client_set_information_refresh_time(client, lease, irt);
+        else {
                 r = dhcp6_lease_get_serverid(lease, NULL, NULL);
                 if (r < 0)
                         return log_dhcp6_client_errno(client, r, "%s has no server id",