networkd: rework duid_{type,duid_type,duid,duid_len} setting
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 29 Apr 2016 03:23:45 +0000 (23:23 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 3 May 2016 16:23:01 +0000 (12:23 -0400)
Separate fields are replaced  with a struct.

Second second duid type field is removed. The first field was used to carry
the result of DUIDType= configuration, and the second was either a copy of
this, or contained the type extracted from DuidRawData. The semantics are changed
so that the type specified in DUIDType is always used. DUIDRawData= no longer
overrides the type setting.

The networkd code is now more constrained than the sd-dhcp code:
DUIDRawData cannot have 0 length, length 0 is treated the same as unsetting.
Likewise, it is not possible to set a DUIDType=0. If it ever becomes necessary
to set type=0 or a zero-length duid, the code can be changed to support that.
Nevertheless, I think that's unlikely.

This addresses #3127 § 1 and 3.

v2:
- rename DUID.duid, DUID.duid_len to DUID.raw_data, DUID.raw_data_len

src/network/networkd-conf.c
src/network/networkd-conf.h
src/network/networkd-dhcp4.c
src/network/networkd-dhcp6.c
src/network/networkd-gperf.gperf
src/network/networkd-link.c
src/network/networkd-manager.c
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd-network.h
src/network/networkd.h

index 70f0121..1b2047f 100644 (file)
@@ -41,7 +41,7 @@ static const char* const duid_type_table[_DUID_TYPE_MAX] = {
         [DUID_TYPE_LLT]  = "link-layer-time",
         [DUID_TYPE_EN]   = "vendor",
         [DUID_TYPE_LL]   = "link-layer",
-        [DUID_TYPE_UUID] = "uuid"
+        [DUID_TYPE_UUID] = "uuid",
 };
 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type, DUIDType);
 DEFINE_CONFIG_PARSE_ENUM(config_parse_duid_type, duid_type, DUIDType, "Failed to parse DUID type");
@@ -58,69 +58,29 @@ int config_parse_duid_rawdata(
                 void *data,
                 void *userdata) {
 
-        int r;
-        char *cbyte;
-        const char *pduid = rvalue;
-        Manager *m = userdata;
-        Network *n = userdata;
-        DUIDType duidtype;
-        uint16_t dhcp_duid_type = 0;
-        uint8_t dhcp_duid[MAX_DUID_LEN];
-        size_t len, count = 0, duid_start_offset = 0, dhcp_duid_len = 0;
+        DUID *ret = data;
+        uint8_t raw_data[MAX_DUID_LEN];
+        unsigned count = 0;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
-        assert(userdata);
+        assert(ret);
 
-        duidtype = (ltype == DUID_CONFIG_SOURCE_GLOBAL) ? m->duid_type : n->duid_type;
-
-        if (duidtype == _DUID_TYPE_INVALID)
-                duidtype = DUID_TYPE_RAW;
-
-        switch (duidtype) {
-
-        case DUID_TYPE_LLT:
-                /* RawData contains DUID-LLT link-layer address (offset 6) */
-                duid_start_offset = 6;
-                break;
-
-        case DUID_TYPE_EN:
-                /* RawData contains DUID-EN identifier (offset 4) */
-                duid_start_offset = 4;
-                break;
-
-        case DUID_TYPE_LL:
-                /* RawData contains DUID-LL link-layer address (offset 2) */
-                duid_start_offset = 2;
-                break;
-
-        case DUID_TYPE_UUID:
-                /* RawData specifies UUID (offset 0) - fall thru */
-
-        case DUID_TYPE_RAW:
-                /* First two bytes of RawData is DUID Type - fall thru */
-
-        default:
-                break;
-        }
-
-        if (duidtype != DUID_TYPE_RAW)
-                dhcp_duid_type = (uint16_t) duidtype;
-
-        /* RawData contains DUID in format " NN:NN:NN... " */
+        /* RawData contains DUID in format "NN:NN:NN..." */
         for (;;) {
-                int n1, n2;
+                int n1, n2, len, r;
                 uint32_t byte;
+                char *cbyte;
 
-                r = extract_first_word(&pduid, &cbyte, ":", 0);
+                r = extract_first_word(&rvalue, &cbyte, ":", 0);
                 if (r < 0) {
                         log_syntax(unit, LOG_ERR, filename, line, r, "Failed to read DUID, ignoring assignment: %s.", rvalue);
                         return 0;
                 }
                 if (r == 0)
                         break;
-                if (duid_start_offset + dhcp_duid_len >= MAX_DUID_LEN) {
+                if (count >= MAX_DUID_LEN) {
                         log_syntax(unit, LOG_ERR, filename, line, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue);
                         return 0;
                 }
@@ -142,30 +102,11 @@ int config_parse_duid_rawdata(
                 }
 
                 byte = ((uint8_t) n1 << (4 * (len-1))) | (uint8_t) n2;
-
-                /* If DUID_TYPE_RAW, first two bytes hold DHCP DUID type code */
-                if (duidtype == DUID_TYPE_RAW && count < 2) {
-                        dhcp_duid_type |= (byte << (8 * (1 - count)));
-                        count++;
-                        continue;
-                }
-
-                dhcp_duid[duid_start_offset + dhcp_duid_len] = byte;
-                dhcp_duid_len++;
-        }
-
-        if (ltype == DUID_CONFIG_SOURCE_GLOBAL) {
-                m->duid_type = duidtype;
-                m->dhcp_duid_type = dhcp_duid_type;
-                m->dhcp_duid_len = dhcp_duid_len;
-                memcpy(&m->dhcp_duid[duid_start_offset], dhcp_duid, dhcp_duid_len);
-        } else {
-                /* DUID_CONFIG_SOURCE_NETWORK */
-                n->duid_type = duidtype;
-                n->dhcp_duid_type = dhcp_duid_type;
-                n->dhcp_duid_len = dhcp_duid_len;
-                memcpy(&n->dhcp_duid[duid_start_offset], dhcp_duid, dhcp_duid_len);
+                raw_data[count++] = byte;
         }
 
+        assert_cc(sizeof(raw_data) == sizeof(ret->raw_data));
+        memcpy(ret->raw_data, raw_data, count);
+        ret->raw_data_len = count;
         return 0;
 }
index 671e656..c7bfb42 100644 (file)
 
 #include "networkd.h"
 
-typedef enum DuidConfigSource {
-        DUID_CONFIG_SOURCE_GLOBAL = 0,
-        DUID_CONFIG_SOURCE_NETWORK,
-} DuidConfigSource;
-
 int manager_parse_config_file(Manager *m);
 
 const struct ConfigPerfItem* networkd_gperf_lookup(const char *key, unsigned length);
 
-int config_parse_duid_type(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
-int config_parse_duid_rawdata(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_duid_type(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata);
+int config_parse_duid_rawdata(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata);
index d31ea4d..89926e8 100644 (file)
@@ -628,28 +628,24 @@ int dhcp4_configure(Link *link) {
         }
 
         switch (link->network->dhcp_client_identifier) {
-        case DHCP_CLIENT_ID_DUID:
+        case DHCP_CLIENT_ID_DUID: {
                 /* If configured, apply user specified DUID and/or IAID */
-                if (link->network->duid_type != _DUID_TYPE_INVALID)
-                        r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
-                                                         link->network->iaid,
-                                                         link->network->dhcp_duid_type,
-                                                         link->network->dhcp_duid,
-                                                         link->network->dhcp_duid_len);
-                else
-                        r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
-                                                         link->network->iaid,
-                                                         link->manager->dhcp_duid_type,
-                                                         link->manager->dhcp_duid,
-                                                         link->manager->dhcp_duid_len);
+                const DUID *duid = link_duid(link);
+
+                r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
+                                                 link->network->iaid,
+                                                 duid->type,
+                                                 duid->raw_data_len > 0 ? duid->raw_data : NULL,
+                                                 duid->raw_data_len);
                 if (r < 0)
                         return r;
                 break;
+        }
         case DHCP_CLIENT_ID_MAC:
                 r = sd_dhcp_client_set_client_id(link->dhcp_client,
                                                  ARPHRD_ETHER,
                                                  (const uint8_t *) &link->mac,
-                                                 sizeof (link->mac));
+                                                 sizeof(link->mac));
                 if (r < 0)
                         return r;
                 break;
index b9c4b89..ccca4e9 100644 (file)
@@ -206,6 +206,7 @@ int dhcp6_request_address(Link *link) {
 int dhcp6_configure(Link *link) {
         sd_dhcp6_client *client = NULL;
         int r;
+        const DUID *duid;
 
         assert(link);
 
@@ -234,16 +235,11 @@ int dhcp6_configure(Link *link) {
         if (r < 0)
                 goto error;
 
-        if (link->network->duid_type != _DUID_TYPE_INVALID)
-                r = sd_dhcp6_client_set_duid(client,
-                                             link->network->dhcp_duid_type,
-                                             link->network->dhcp_duid,
-                                             link->network->dhcp_duid_len);
-        else
-                r = sd_dhcp6_client_set_duid(client,
-                                             link->manager->dhcp_duid_type,
-                                             link->manager->dhcp_duid,
-                                             link->manager->dhcp_duid_len);
+        duid = link_duid(link);
+        r = sd_dhcp6_client_set_duid(client,
+                                     duid->type,
+                                     duid->raw_data_len > 0 ? duid->raw_data : NULL,
+                                     duid->raw_data_len);
         if (r < 0)
                 goto error;
 
index afc71b4..3fdfe74 100644 (file)
@@ -14,5 +14,5 @@ struct ConfigPerfItem;
 %struct-type
 %includes
 %%
-DHCP.DUIDType,              config_parse_duid_type,                 0,                                  offsetof(Manager, duid_type)
-DHCP.DUIDRawData,           config_parse_duid_rawdata,              DUID_CONFIG_SOURCE_GLOBAL,          offsetof(Manager, dhcp_duid)
+DHCP.DUIDType,              config_parse_duid_type,                 0,          offsetof(Manager, duid.type)
+DHCP.DUIDRawData,           config_parse_duid_rawdata,              0,          offsetof(Manager, duid)
index a88e4c3..c646af1 100644 (file)
@@ -2879,6 +2879,8 @@ int link_update(Link *link, sd_netlink_message *m) {
                         }
 
                         if (link->dhcp_client) {
+                                const DUID *duid = link_duid(link);
+
                                 r = sd_dhcp_client_set_mac(link->dhcp_client,
                                                            (const uint8_t *) &link->mac,
                                                            sizeof (link->mac),
@@ -2886,23 +2888,18 @@ int link_update(Link *link, sd_netlink_message *m) {
                                 if (r < 0)
                                         return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m");
 
-                                if (link->network->duid_type != _DUID_TYPE_INVALID)
-                                        r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
-                                                                         link->network->iaid,
-                                                                         link->network->dhcp_duid_type,
-                                                                         link->network->dhcp_duid,
-                                                                         link->network->dhcp_duid_len);
-                                else
-                                        r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
-                                                                         link->network->iaid,
-                                                                         link->manager->dhcp_duid_type,
-                                                                         link->manager->dhcp_duid,
-                                                                         link->manager->dhcp_duid_len);
+                                r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
+                                                                 link->network->iaid,
+                                                                 duid->type,
+                                                                 duid->raw_data_len > 0 ? duid->raw_data : NULL,
+                                                                 duid->raw_data_len);
                                 if (r < 0)
                                         return log_link_warning_errno(link, r, "Could not update DUID/IAID in DHCP client: %m");
                         }
 
                         if (link->dhcp6_client) {
+                                const DUID* duid = link_duid(link);
+
                                 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
                                                             (const uint8_t *) &link->mac,
                                                             sizeof (link->mac),
@@ -2915,16 +2912,10 @@ int link_update(Link *link, sd_netlink_message *m) {
                                 if (r < 0)
                                         return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m");
 
-                                if (link->network->duid_type != _DUID_TYPE_INVALID)
-                                        r = sd_dhcp6_client_set_duid(link->dhcp6_client,
-                                                                     link->network->dhcp_duid_type,
-                                                                     link->network->dhcp_duid,
-                                                                     link->network->dhcp_duid_len);
-                                else
-                                        r = sd_dhcp6_client_set_duid(link->dhcp6_client,
-                                                                     link->manager->dhcp_duid_type,
-                                                                     link->manager->dhcp_duid,
-                                                                     link->manager->dhcp_duid_len);
+                                r = sd_dhcp6_client_set_duid(link->dhcp6_client,
+                                                             duid->type,
+                                                             duid->raw_data_len > 0 ? duid->raw_data : NULL,
+                                                             duid->raw_data_len);
                                 if (r < 0)
                                         return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m");
                         }
index 0cb5781..5dcd4df 100644 (file)
@@ -1037,7 +1037,7 @@ int manager_new(Manager **ret) {
         if (r < 0)
                 return r;
 
-        m->duid_type = DUID_TYPE_EN;
+        m->duid.type = DUID_TYPE_EN;
 
         *ret = m;
         m = NULL;
index 654d6a0..6bf57cd 100644 (file)
@@ -83,8 +83,8 @@ DHCP.Hostname,                          config_parse_hostname,
 DHCP.RequestBroadcast,                  config_parse_bool,                              0,                             offsetof(Network, dhcp_broadcast)
 DHCP.CriticalConnection,                config_parse_bool,                              0,                             offsetof(Network, dhcp_critical)
 DHCP.VendorClassIdentifier,             config_parse_string,                            0,                             offsetof(Network, dhcp_vendor_class_identifier)
-DHCP.DUIDType,                          config_parse_duid_type,                         0,                             offsetof(Network, duid_type)
-DHCP.DUIDRawData,                       config_parse_duid_rawdata,                      DUID_CONFIG_SOURCE_NETWORK,    offsetof(Network, dhcp_duid)
+DHCP.DUIDType,                          config_parse_duid_type,                         0,                             offsetof(Network, duid.type)
+DHCP.DUIDRawData,                       config_parse_duid_rawdata,                      0,                             offsetof(Network, duid)
 DHCP.RouteMetric,                       config_parse_unsigned,                          0,                             offsetof(Network, dhcp_route_metric)
 DHCP.UseTimezone,                       config_parse_bool,                              0,                             offsetof(Network, dhcp_use_timezone)
 DHCPServer.MaxLeaseTimeSec,             config_parse_sec,                               0,                             offsetof(Network, dhcp_server_max_lease_time_usec)
index 2ebcdfa..206c270 100644 (file)
@@ -131,7 +131,7 @@ static int network_load_one(Manager *manager, const char *filename) {
         network->ipv6_accept_ra = -1;
         network->ipv6_dad_transmits = -1;
         network->ipv6_hop_limit = -1;
-        network->duid_type = _DUID_TYPE_INVALID;
+        network->duid.type = _DUID_TYPE_INVALID;
         network->proxy_arp = -1;
 
         r = config_parse(NULL, filename, file,
index 9b8096f..ff2414e 100644 (file)
@@ -68,6 +68,14 @@ typedef enum LLDPMode {
         _LLDP_MODE_INVALID = -1,
 } LLDPMode;
 
+typedef struct DUID {
+        /* Value of Type in [DHCP] section */
+        DUIDType type;
+
+        uint8_t raw_data_len;
+        uint8_t raw_data[MAX_DUID_LEN];
+} DUID;
+
 typedef struct Manager Manager;
 
 struct Network {
@@ -150,12 +158,7 @@ struct Network {
         struct ether_addr *mac;
         unsigned mtu;
         uint32_t iaid;
-        /* Value of Type in [DUID] section */
-        DUIDType duid_type;
-        /* DUID type code - RFC 3315 */
-        uint16_t dhcp_duid_type;
-        size_t dhcp_duid_len;
-        uint8_t dhcp_duid[MAX_DUID_LEN];
+        DUID duid;
 
         LLDPMode lldp_mode; /* LLDP reception */
         bool lldp_emit;     /* LLDP transmission */
index 39826a4..26d9e7d 100644 (file)
@@ -74,14 +74,16 @@ struct Manager {
 
         usec_t network_dirs_ts_usec;
 
-        /* Value of Type in [DUID] section */
-        DUIDType duid_type;
-        /* DUID type code - RFC 3315 */
-        uint16_t dhcp_duid_type;
-        size_t dhcp_duid_len;
-        uint8_t dhcp_duid[MAX_DUID_LEN];
+        DUID duid;
 };
 
+static inline const DUID* link_duid(const Link *link) {
+        if (link->network->duid.type != _DUID_TYPE_INVALID)
+                return &link->network->duid;
+        else
+                return &link->manager->duid;
+}
+
 extern const sd_bus_vtable manager_vtable[];
 
 int manager_new(Manager **ret);