]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dhcp: add support for vendor specific DHCP option
authorBeniamino Galvani <bgalvani@redhat.com>
Thu, 9 Jul 2015 16:04:01 +0000 (18:04 +0200)
committerDaniel Mack <daniel@zonque.org>
Sun, 12 Jul 2015 18:26:32 +0000 (14:26 -0400)
This adds support for option 43 (Vendor Specific Information) to
libsystemd-network DHCP code. The option carries an opaque object of n
octets, interpreted by vendor-specific code on the clients and
servers.

[@zonque: adopted to new unhexmem() API]

src/libsystemd-network/dhcp-lease-internal.h
src/libsystemd-network/dhcp-protocol.h
src/libsystemd-network/sd-dhcp-lease.c
src/systemd/sd-dhcp-lease.h

index 9e184ac4b59e643895db203f1623835e459a84dc..6e00b1ad301d57882cf1e568063c18d122b8e838 100644 (file)
@@ -72,6 +72,8 @@ struct sd_dhcp_lease {
         char *root_path;
         uint8_t *client_id;
         size_t client_id_len;
+        uint8_t *vendor_specific;
+        size_t vendor_specific_len;
 };
 
 int dhcp_lease_new(sd_dhcp_lease **ret);
index abca9422c5c1281d3f4f53b1326c7c786c88a287..aa37e9b0b5c480741369132c2640565fa64b78c4 100644 (file)
@@ -125,6 +125,7 @@ enum {
         DHCP_OPTION_BROADCAST                   = 28,
         DHCP_OPTION_STATIC_ROUTE                = 33,
         DHCP_OPTION_NTP_SERVER                  = 42,
+        DHCP_OPTION_VENDOR_SPECIFIC             = 43,
         DHCP_OPTION_REQUESTED_IP_ADDRESS        = 50,
         DHCP_OPTION_IP_ADDRESS_LEASE_TIME       = 51,
         DHCP_OPTION_OVERLOAD                    = 52,
index 7548e50b7d9ff483f3a23d96fdb437d3bd5fe359..e9bf3c9fee25ebbcdd6c8e8ac0ffe81ab769081d 100644 (file)
@@ -179,6 +179,21 @@ int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routes
         return 0;
 }
 
+int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const uint8_t **data,
+                                      size_t *data_len) {
+        assert_return(lease, -EINVAL);
+        assert_return(data, -EINVAL);
+        assert_return(data_len, -EINVAL);
+
+        if (!lease->vendor_specific)
+                return -ENOENT;
+
+        *data = lease->vendor_specific;
+        *data_len = lease->vendor_specific_len;
+
+        return 0;
+}
+
 sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
         if (lease)
                 assert_se(REFCNT_INC(lease->n_ref) >= 2);
@@ -194,6 +209,7 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
                 free(lease->ntp);
                 free(lease->static_route);
                 free(lease->client_id);
+                free(lease->vendor_specific);
                 free(lease);
         }
 
@@ -579,6 +595,17 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
                         return r;
 
                 break;
+
+        case DHCP_OPTION_VENDOR_SPECIFIC:
+                if (len >= 1) {
+                        free(lease->vendor_specific);
+                        lease->vendor_specific = memdup(option, len);
+                        if (!lease->vendor_specific)
+                                return -ENOMEM;
+                        lease->vendor_specific_len = len;
+                }
+
+               break;
         }
 
         return 0;
@@ -603,8 +630,8 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         _cleanup_fclose_ FILE *f = NULL;
         struct in_addr address;
         const struct in_addr *addresses;
-        const uint8_t *client_id;
-        size_t client_id_len;
+        const uint8_t *client_id, *data;
+        size_t client_id_len, data_len;
         const char *string;
         uint16_t mtu;
         struct sd_dhcp_route *routes;
@@ -690,6 +717,18 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
                 fprintf(f, "CLIENTID=%s\n", client_id_hex);
         }
 
+        r = sd_dhcp_lease_get_vendor_specific(lease, &data, &data_len);
+        if (r >= 0) {
+                _cleanup_free_ char *option_hex = NULL;
+
+                option_hex = hexmem(data, data_len);
+                if (!option_hex) {
+                        r = -ENOMEM;
+                        goto finish;
+                }
+                fprintf(f, "VENDOR_SPECIFIC=%s\n", option_hex);
+        }
+
         r = 0;
 
         fflush(f);
@@ -712,7 +751,8 @@ int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
         _cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
                             *server_address = NULL, *next_server = NULL,
                             *dns = NULL, *ntp = NULL, *mtu = NULL,
-                            *routes = NULL, *client_id_hex = NULL;
+                            *routes = NULL, *client_id_hex = NULL,
+                            *vendor_specific_hex = NULL;
         struct in_addr addr;
         int r;
 
@@ -737,6 +777,7 @@ int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
                            "ROOT_PATH", &lease->root_path,
                            "ROUTES", &routes,
                            "CLIENTID", &client_id_hex,
+                           "VENDOR_SPECIFIC", &vendor_specific_hex,
                            NULL);
         if (r < 0) {
                 if (r == -ENOENT)
@@ -819,6 +860,15 @@ int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
                         return r;
         }
 
+        if (vendor_specific_hex) {
+                if (strlen(vendor_specific_hex) % 2)
+                        return -EINVAL;
+
+                r = unhexmem(vendor_specific_hex, strlen(vendor_specific_hex), (void**) &lease->vendor_specific, &lease->vendor_specific_len);
+                if (r < 0)
+                        return r;
+        }
+
         *ret = lease;
         lease = NULL;
 
index 4296b91d8a29539482f43bb9864708189b097973..5afa50a9d0e729cf7e734db256028cecbf955074 100644 (file)
@@ -45,6 +45,8 @@ int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname);
 int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname);
 int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path);
 int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routesgn);
+int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const uint8_t **data,
+                                      size_t *data_len);
 int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const uint8_t **client_id,
                                 size_t *client_id_len);