chiark / gitweb /
sd-dhcp6-client: support custom DUIDs
[elogind.git] / src / libsystemd-network / sd-dhcp6-client.c
index 8fdbbfe3201e62d4da0b4f37fc8884feb4f39798..130fe43cf26b40a2588e74ef3fb47c24f111ab77 100644 (file)
@@ -39,6 +39,8 @@
 #define SYSTEMD_PEN 43793
 #define HASH_KEY SD_ID128_MAKE(80,11,8c,c2,fe,4a,03,ee,3e,d6,0c,6f,36,39,14,09)
 
+#define MAX_DUID_LEN 32
+
 struct sd_dhcp6_client {
         RefCount n_ref;
 
@@ -49,6 +51,7 @@ struct sd_dhcp6_client {
         struct ether_addr mac_addr;
         DHCP6IA ia_na;
         be32_t transaction_id;
+        usec_t transaction_start;
         struct sd_dhcp6_lease *lease;
         int fd;
         be16_t *req_opts;
@@ -61,12 +64,8 @@ struct sd_dhcp6_client {
         sd_event_source *timeout_resend_expire;
         sd_dhcp6_client_cb_t cb;
         void *userdata;
-
-        struct duid_en {
-                uint16_t type; /* DHCP6_DUID_EN */
-                uint32_t pen;
-                uint8_t id[8];
-        } _packed_ duid;
+        uint8_t duid[MAX_DUID_LEN];
+        size_t duid_len;
 };
 
 static const uint16_t default_req_opts[] = {
@@ -146,6 +145,19 @@ int sd_dhcp6_client_set_mac(sd_dhcp6_client *client,
         return 0;
 }
 
+int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, uint8_t *duid,
+                             size_t duid_len)
+{
+        assert_return(client, -EINVAL);
+        assert_return(duid, -EINVAL);
+        assert_return(duid_len > 0 && duid_len <= MAX_DUID_LEN, -EINVAL);
+
+        memcpy(&client->duid, duid, duid_len);
+        client->duid_len = duid_len;
+
+        return 0;
+}
+
 int sd_dhcp6_client_set_request_option(sd_dhcp6_client *client,
                                        uint16_t option) {
         size_t t;
@@ -203,6 +215,7 @@ static int client_reset(sd_dhcp6_client *client) {
         client->fd = safe_close(client->fd);
 
         client->transaction_id = 0;
+        client->transaction_start = 0;
 
         client->ia_na.timeout_t1 =
                 sd_event_source_unref(client->ia_na.timeout_t1);
@@ -230,13 +243,15 @@ static void client_stop(sd_dhcp6_client *client, int error) {
         client_reset(client);
 }
 
-static int client_send_message(sd_dhcp6_client *client) {
+static int client_send_message(sd_dhcp6_client *client, usec_t time_now) {
         _cleanup_free_ DHCP6Message *message = NULL;
         struct in6_addr all_servers =
                 IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT;
         size_t len, optlen = 512;
         uint8_t *opt;
         int r;
+        usec_t elapsed_usec;
+        be16_t elapsed_time;
 
         len = sizeof(DHCP6Message) + optlen;
 
@@ -293,7 +308,6 @@ static int client_send_message(sd_dhcp6_client *client) {
                 break;
 
         case DHCP6_STATE_STOPPED:
-        case DHCP6_STATE_RS:
         case DHCP6_STATE_BOUND:
                 return -EINVAL;
         }
@@ -305,7 +319,18 @@ static int client_send_message(sd_dhcp6_client *client) {
                 return r;
 
         r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_CLIENTID,
-                                sizeof(client->duid), &client->duid);
+                                client->duid_len, &client->duid);
+        if (r < 0)
+                return r;
+
+        elapsed_usec = time_now - client->transaction_start;
+        if (elapsed_usec < 0xffff * USEC_PER_MSEC * 10)
+                elapsed_time = htobe16(elapsed_usec / USEC_PER_MSEC / 10);
+        else
+                elapsed_time = 0xffff;
+
+        r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_ELAPSED_TIME,
+                                sizeof(elapsed_time), &elapsed_time);
         if (r < 0)
                 return r;
 
@@ -387,7 +412,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                                  void *userdata) {
         int r = 0;
         sd_dhcp6_client *client = userdata;
-        usec_t time_now, init_retransmit_time, max_retransmit_time;
+        usec_t time_now, init_retransmit_time = 0, max_retransmit_time = 0;
         usec_t max_retransmit_duration = 0;
         uint8_t max_retransmit_count = 0;
         char time_string[FORMAT_TIMESPAN_MAX];
@@ -446,7 +471,6 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                 break;
 
         case DHCP6_STATE_STOPPED:
-        case DHCP6_STATE_RS:
         case DHCP6_STATE_BOUND:
                 return 0;
         }
@@ -457,15 +481,14 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                 return 0;
         }
 
-        r = client_send_message(client);
-        if (r >= 0)
-                client->retransmit_count++;
-
-
-        r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now);
+        r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now);
         if (r < 0)
                 goto error;
 
+        r = client_send_message(client, time_now);
+        if (r >= 0)
+                client->retransmit_count++;
+
         if (!client->retransmit_time) {
                 client->retransmit_time =
                         client_timeout_compute_random(init_retransmit_time);
@@ -486,7 +509,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                                          client->retransmit_time, 0));
 
         r = sd_event_add_time(client->event, &client->timeout_resend,
-                              CLOCK_MONOTONIC,
+                              clock_boottime_or_monotonic(),
                               time_now + client->retransmit_time,
                               10 * USEC_PER_MSEC, client_timeout_resend,
                               client);
@@ -498,6 +521,11 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
         if (r < 0)
                 goto error;
 
+        r = sd_event_source_set_name(client->timeout_resend,
+                                     "dhcp6-resend-timer");
+        if (r < 0)
+                goto error;
+
         if (max_retransmit_duration && !client->timeout_resend_expire) {
 
                 log_dhcp6_client(client, "Max retransmission duration %"PRIu64" secs",
@@ -505,7 +533,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
 
                 r = sd_event_add_time(client->event,
                                       &client->timeout_resend_expire,
-                                      CLOCK_MONOTONIC,
+                                      clock_boottime_or_monotonic(),
                                       time_now + max_retransmit_duration,
                                       USEC_PER_SEC,
                                       client_timeout_resend_expire, client);
@@ -516,6 +544,11 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                                                  client->event_priority);
                 if (r < 0)
                         goto error;
+
+                r = sd_event_source_set_name(client->timeout_resend_expire,
+                                             "dhcp6-resend-expire-timer");
+                if (r < 0)
+                        goto error;
         }
 
 error:
@@ -575,12 +608,15 @@ static int client_parse_message(sd_dhcp6_client *client,
                                 DHCP6Message *message, size_t len,
                                 sd_dhcp6_lease *lease) {
         int r;
-        uint8_t *optval, *option = (uint8_t *)(message + 1), *id = NULL;
+        uint8_t *optval, *option, *id = NULL;
         uint16_t optcode, status;
         size_t optlen, id_len;
         bool clientid = false;
         be32_t iaid_lease;
 
+        option = (uint8_t *)message + sizeof(DHCP6Message);
+        len -= sizeof(DHCP6Message);
+
         while ((r = dhcp6_option_parse(&option, &len, &optcode, &optlen,
                                        &optval)) >= 0) {
                 switch (optcode) {
@@ -591,7 +627,7 @@ static int client_parse_message(sd_dhcp6_client *client,
                                 return -EINVAL;
                         }
 
-                        if (optlen != sizeof(client->duid) ||
+                        if (optlen != client->duid_len ||
                             memcmp(&client->duid, optval, optlen) != 0) {
                                 log_dhcp6_client(client, "%s DUID does not match",
                                                  dhcp6_message_type_to_string(message->type));
@@ -708,7 +744,8 @@ static int client_receive_reply(sd_dhcp6_client *client, DHCP6Message *reply,
                         return 0;
         }
 
-        dhcp6_lease_clear_timers(&client->lease->ia);
+        if (client->lease)
+                dhcp6_lease_clear_timers(&client->lease->ia);
 
         client->lease = sd_dhcp6_lease_unref(client->lease);
         client->lease = lease;
@@ -842,7 +879,6 @@ static int client_receive_message(sd_event_source *s, int fd, uint32_t revents,
                 break;
 
         case DHCP6_STATE_STOPPED:
-        case DHCP6_STATE_RS:
                 return 0;
         }
 
@@ -871,9 +907,17 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
         client->retransmit_time = 0;
         client->retransmit_count = 0;
 
+        if (client->state == DHCP6_STATE_STOPPED) {
+                time_now = now(clock_boottime_or_monotonic());
+        } else {
+                r = sd_event_now(client->event, clock_boottime_or_monotonic(),
+                                 &time_now);
+                if (r < 0)
+                        return r;
+        }
+
         switch (state) {
         case DHCP6_STATE_STOPPED:
-        case DHCP6_STATE_RS:
         case DHCP6_STATE_SOLICITATION:
 
                 r = client_ensure_iaid(client);
@@ -897,6 +941,11 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
                 if (r < 0)
                         return r;
 
+                r = sd_event_source_set_name(client->receive_message,
+                                             "dhcp6-receive-message");
+                if (r < 0)
+                        return r;
+
                 client->state = DHCP6_STATE_SOLICITATION;
 
                 break;
@@ -911,10 +960,6 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
 
         case DHCP6_STATE_BOUND:
 
-                r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now);
-                if (r < 0)
-                        return r;
-
                 if (client->lease->ia.lifetime_t1 == 0xffffffff ||
                     client->lease->ia.lifetime_t2 == 0xffffffff) {
 
@@ -934,7 +979,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
 
                 r = sd_event_add_time(client->event,
                                       &client->lease->ia.timeout_t1,
-                                      CLOCK_MONOTONIC, time_now + timeout,
+                                      clock_boottime_or_monotonic(), time_now + timeout,
                                       10 * USEC_PER_SEC, client_timeout_t1,
                                       client);
                 if (r < 0)
@@ -945,6 +990,11 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
                 if (r < 0)
                         return r;
 
+                r = sd_event_source_set_name(client->lease->ia.timeout_t1,
+                                             "dhcp6-t1-timeout");
+                if (r < 0)
+                        return r;
+
                 timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t2) * USEC_PER_SEC);
 
                 log_dhcp6_client(client, "T2 expires in %s",
@@ -954,7 +1004,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
 
                 r = sd_event_add_time(client->event,
                                       &client->lease->ia.timeout_t2,
-                                      CLOCK_MONOTONIC, time_now + timeout,
+                                      clock_boottime_or_monotonic(), time_now + timeout,
                                       10 * USEC_PER_SEC, client_timeout_t2,
                                       client);
                 if (r < 0)
@@ -965,15 +1015,21 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
                 if (r < 0)
                         return r;
 
+                r = sd_event_source_set_name(client->lease->ia.timeout_t2,
+                                             "dhcp6-t2-timeout");
+                if (r < 0)
+                        return r;
+
                 client->state = state;
 
                 return 0;
         }
 
         client->transaction_id = random_u32() & htobe32(0x00ffffff);
+        client->transaction_start = time_now;
 
         r = sd_event_add_time(client->event, &client->timeout_resend,
-                              CLOCK_MONOTONIC, 0, 0, client_timeout_resend,
+                              clock_boottime_or_monotonic(), 0, 0, client_timeout_resend,
                               client);
         if (r < 0)
                 return r;
@@ -983,6 +1039,11 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
         if (r < 0)
                 return r;
 
+        r = sd_event_source_set_name(client->timeout_resend,
+                                     "dhcp6-resend-timeout");
+        if (r < 0)
+                return r;
+
         return 0;
 }
 
@@ -1066,9 +1127,16 @@ sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) {
         return client;
 }
 
+struct duid_en {
+        uint16_t type; /* DHCP6_DUID_EN */
+        uint32_t pen;
+        uint8_t id[8];
+} _packed_;
+
 int sd_dhcp6_client_new(sd_dhcp6_client **ret)
 {
         _cleanup_dhcp6_client_unref_ sd_dhcp6_client *client = NULL;
+        struct duid_en *duid;
         sd_id128_t machine_id;
         int r;
         size_t t;
@@ -1088,8 +1156,9 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret)
         client->fd = -1;
 
         /* initialize DUID */
-        client->duid.type = htobe16(DHCP6_DUID_EN);
-        client->duid.pen = htobe32(SYSTEMD_PEN);
+        duid = (struct duid_en *) &client->duid;
+        duid->type = htobe16(DHCP6_DUID_EN);
+        duid->pen = htobe32(SYSTEMD_PEN);
 
         r = sd_id128_get_machine(&machine_id);
         if (r < 0)
@@ -1097,8 +1166,8 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret)
 
         /* a bit of snake-oil perhaps, but no need to expose the machine-id
            directly */
-        siphash24(client->duid.id, &machine_id, sizeof(machine_id),
-                  HASH_KEY.bytes);
+        siphash24(duid->id, &machine_id, sizeof(machine_id), HASH_KEY.bytes);
+        client->duid_len = sizeof (struct duid_en);
 
         client->req_opts_len = ELEMENTSOF(default_req_opts);