From e43a8393eaf058c4ecd2a5968f59d83b911d4f1f Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Thu, 9 Jul 2015 18:04:01 +0200 Subject: dhcp: add support for vendor specific DHCP option 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 | 2 + src/libsystemd-network/dhcp-protocol.h | 1 + src/libsystemd-network/sd-dhcp-lease.c | 56 ++++++++++++++++++++++++++-- src/systemd/sd-dhcp-lease.h | 2 + 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/libsystemd-network/dhcp-lease-internal.h b/src/libsystemd-network/dhcp-lease-internal.h index 9e184ac4b..6e00b1ad3 100644 --- a/src/libsystemd-network/dhcp-lease-internal.h +++ b/src/libsystemd-network/dhcp-lease-internal.h @@ -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); diff --git a/src/libsystemd-network/dhcp-protocol.h b/src/libsystemd-network/dhcp-protocol.h index abca9422c..aa37e9b0b 100644 --- a/src/libsystemd-network/dhcp-protocol.h +++ b/src/libsystemd-network/dhcp-protocol.h @@ -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, diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 7548e50b7..e9bf3c9fe 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -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; diff --git a/src/systemd/sd-dhcp-lease.h b/src/systemd/sd-dhcp-lease.h index 4296b91d8..5afa50a9d 100644 --- a/src/systemd/sd-dhcp-lease.h +++ b/src/systemd/sd-dhcp-lease.h @@ -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); -- cgit v1.2.3