2 * An implementation of key value pair (KVP) functionality for Linux.
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/utsname.h>
33 #include <arpa/inet.h>
34 #include <linux/hyperv.h>
46 * KVP protocol: The user mode component first registers with the
47 * the kernel component. Subsequently, the kernel component requests, data
48 * for the specified keys. In response to this message the user mode component
49 * fills in the value corresponding to the specified key. We overload the
50 * sequence field in the cn_msg header to define our KVP message types.
52 * We use this infrastructure for also supporting queries from user mode
53 * application for state that may be maintained in the KVP kernel component.
59 FullyQualifiedDomainName = 0,
60 IntegrationServicesVersion, /*This key is serviced in the kernel*/
79 static int in_hand_shake = 1;
81 static char *os_name = "";
82 static char *os_major = "";
83 static char *os_minor = "";
84 static char *processor_arch;
85 static char *os_build;
86 static char *os_version;
87 static char *lic_version = "Unknown version";
88 static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
89 static struct utsname uts_buf;
92 * The location of the interface configuration file.
95 #define KVP_CONFIG_LOC "/var/lib/hyperv"
97 #ifndef KVP_SCRIPTS_PATH
98 #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/"
101 #define KVP_NET_DIR "/sys/class/net/"
103 #define MAX_FILE_NAME 100
104 #define ENTRIES_PER_BLOCK 50
107 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
108 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
111 struct kvp_file_state {
114 struct kvp_record *records;
116 char fname[MAX_FILE_NAME];
119 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
121 static void kvp_acquire_lock(int pool)
123 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
126 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
127 syslog(LOG_ERR, "Failed to acquire the lock pool: %d; error: %d %s", pool,
128 errno, strerror(errno));
133 static void kvp_release_lock(int pool)
135 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
138 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
139 syslog(LOG_ERR, "Failed to release the lock pool: %d; error: %d %s", pool,
140 errno, strerror(errno));
145 static void kvp_update_file(int pool)
150 * We are going to write our in-memory registry out to
151 * disk; acquire the lock first.
153 kvp_acquire_lock(pool);
155 filep = fopen(kvp_file_info[pool].fname, "we");
157 syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
158 errno, strerror(errno));
159 kvp_release_lock(pool);
163 fwrite(kvp_file_info[pool].records, sizeof(struct kvp_record),
164 kvp_file_info[pool].num_records, filep);
166 if (ferror(filep) || fclose(filep)) {
167 kvp_release_lock(pool);
168 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
172 kvp_release_lock(pool);
175 static void kvp_update_mem_state(int pool)
178 size_t records_read = 0;
179 struct kvp_record *record = kvp_file_info[pool].records;
180 struct kvp_record *readp;
181 int num_blocks = kvp_file_info[pool].num_blocks;
182 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
184 kvp_acquire_lock(pool);
186 filep = fopen(kvp_file_info[pool].fname, "re");
188 syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
189 errno, strerror(errno));
190 kvp_release_lock(pool);
194 readp = &record[records_read];
195 records_read += fread(readp, sizeof(struct kvp_record),
196 ENTRIES_PER_BLOCK * num_blocks,
200 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
206 * We have more data to read.
209 record = realloc(record, alloc_unit * num_blocks);
211 if (record == NULL) {
212 syslog(LOG_ERR, "malloc failed");
220 kvp_file_info[pool].num_blocks = num_blocks;
221 kvp_file_info[pool].records = record;
222 kvp_file_info[pool].num_records = records_read;
225 kvp_release_lock(pool);
227 static int kvp_file_init(void)
233 struct kvp_record *record;
234 struct kvp_record *readp;
237 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
239 if (access(KVP_CONFIG_LOC, F_OK)) {
240 if (mkdir(KVP_CONFIG_LOC, 0755 /* rwxr-xr-x */)) {
241 syslog(LOG_ERR, "Failed to create '%s'; error: %d %s", KVP_CONFIG_LOC,
242 errno, strerror(errno));
247 for (i = 0; i < KVP_POOL_COUNT; i++) {
248 fname = kvp_file_info[i].fname;
251 sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
252 fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
258 filep = fopen(fname, "re");
264 record = malloc(alloc_unit * num_blocks);
265 if (record == NULL) {
271 readp = &record[records_read];
272 records_read += fread(readp, sizeof(struct kvp_record),
277 syslog(LOG_ERR, "Failed to read file, pool: %d",
284 * We have more data to read.
287 record = realloc(record, alloc_unit *
289 if (record == NULL) {
298 kvp_file_info[i].fd = fd;
299 kvp_file_info[i].num_blocks = num_blocks;
300 kvp_file_info[i].records = record;
301 kvp_file_info[i].num_records = records_read;
309 static int kvp_key_delete(int pool, const __u8 *key, int key_size)
314 struct kvp_record *record;
317 * First update the in-memory state.
319 kvp_update_mem_state(pool);
321 num_records = kvp_file_info[pool].num_records;
322 record = kvp_file_info[pool].records;
324 for (i = 0; i < num_records; i++) {
325 if (memcmp(key, record[i].key, key_size))
328 * Found a match; just move the remaining
331 if (i == num_records) {
332 kvp_file_info[pool].num_records--;
333 kvp_update_file(pool);
339 for (; k < num_records; k++) {
340 strcpy(record[j].key, record[k].key);
341 strcpy(record[j].value, record[k].value);
345 kvp_file_info[pool].num_records--;
346 kvp_update_file(pool);
352 static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
353 const __u8 *value, int value_size)
357 struct kvp_record *record;
360 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
361 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
365 * First update the in-memory state.
367 kvp_update_mem_state(pool);
369 num_records = kvp_file_info[pool].num_records;
370 record = kvp_file_info[pool].records;
371 num_blocks = kvp_file_info[pool].num_blocks;
373 for (i = 0; i < num_records; i++) {
374 if (memcmp(key, record[i].key, key_size))
377 * Found a match; just update the value -
378 * this is the modify case.
380 memcpy(record[i].value, value, value_size);
381 kvp_update_file(pool);
386 * Need to add a new entry;
388 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
389 /* Need to allocate a larger array for reg entries. */
390 record = realloc(record, sizeof(struct kvp_record) *
391 ENTRIES_PER_BLOCK * (num_blocks + 1));
395 kvp_file_info[pool].num_blocks++;
398 memcpy(record[i].value, value, value_size);
399 memcpy(record[i].key, key, key_size);
400 kvp_file_info[pool].records = record;
401 kvp_file_info[pool].num_records++;
402 kvp_update_file(pool);
406 static int kvp_get_value(int pool, const __u8 *key, int key_size, __u8 *value,
411 struct kvp_record *record;
413 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
414 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
418 * First update the in-memory state.
420 kvp_update_mem_state(pool);
422 num_records = kvp_file_info[pool].num_records;
423 record = kvp_file_info[pool].records;
425 for (i = 0; i < num_records; i++) {
426 if (memcmp(key, record[i].key, key_size))
429 * Found a match; just copy the value out.
431 memcpy(value, record[i].value, value_size);
438 static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
439 __u8 *value, int value_size)
441 struct kvp_record *record;
444 * First update our in-memory database.
446 kvp_update_mem_state(pool);
447 record = kvp_file_info[pool].records;
449 if (index >= kvp_file_info[pool].num_records) {
453 memcpy(key, record[index].key, key_size);
454 memcpy(value, record[index].value, value_size);
459 void kvp_get_os_info(void)
465 os_version = uts_buf.release;
466 os_build = strdup(uts_buf.release);
468 os_name = uts_buf.sysname;
469 processor_arch = uts_buf.machine;
472 * The current windows host (win7) expects the build
473 * string to be of the form: x.y.z
474 * Strip additional information we may have.
476 p = strchr(os_version, '-');
481 * Parse the /etc/os-release file if present:
482 * http://www.freedesktop.org/software/systemd/man/os-release.html
484 file = fopen("/etc/os-release", "r");
486 while (fgets(buf, sizeof(buf), file)) {
489 /* Ignore comments */
493 /* Split into name=value */
494 p = strchr(buf, '=');
499 /* Remove quotes and newline; un-escape */
508 } else if (*p == '\'' || *p == '"' ||
517 if (!strcmp(buf, "NAME")) {
522 } else if (!strcmp(buf, "VERSION_ID")) {
533 /* Fallback for older RH/SUSE releases */
534 file = fopen("/etc/SuSE-release", "r");
536 goto kvp_osinfo_found;
537 file = fopen("/etc/redhat-release", "r");
539 goto kvp_osinfo_found;
542 * We don't have information about the os.
547 /* up to three lines */
548 p = fgets(buf, sizeof(buf), file);
550 p = strchr(buf, '\n');
559 p = fgets(buf, sizeof(buf), file);
561 p = strchr(buf, '\n');
570 p = fgets(buf, sizeof(buf), file);
572 p = strchr(buf, '\n');
590 * Retrieve an interface name corresponding to the specified guid.
591 * If there is a match, the function returns a pointer
592 * to the interface name and if not, a NULL is returned.
593 * If a match is found, the caller is responsible for
594 * freeing the memory.
597 static char *kvp_get_if_name(char *guid)
600 struct dirent *entry;
603 char *if_name = NULL;
605 char dev_id[PATH_MAX];
607 dir = opendir(KVP_NET_DIR);
611 while ((entry = readdir(dir)) != NULL) {
613 * Set the state for the next pass.
615 snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id",
616 KVP_NET_DIR, entry->d_name);
618 file = fopen(dev_id, "r");
622 p = fgets(buf, sizeof(buf), file);
628 if (!strcmp(p, guid)) {
630 * Found the guid match; return the interface
631 * name. The caller will free the memory.
633 if_name = strdup(entry->d_name);
646 * Retrieve the MAC address given the interface name.
649 static char *kvp_if_name_to_mac(char *if_name)
654 char addr_file[PATH_MAX];
656 char *mac_addr = NULL;
658 snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR,
659 if_name, "/address");
661 file = fopen(addr_file, "r");
665 p = fgets(buf, sizeof(buf), file);
670 for (i = 0; i < strlen(p); i++)
671 p[i] = toupper(p[i]);
672 mac_addr = strdup(p);
681 * Retrieve the interface name given tha MAC address.
684 static char *kvp_mac_to_if_name(char *mac)
687 struct dirent *entry;
690 char *if_name = NULL;
692 char dev_id[PATH_MAX];
695 dir = opendir(KVP_NET_DIR);
699 while ((entry = readdir(dir)) != NULL) {
701 * Set the state for the next pass.
703 snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR,
706 file = fopen(dev_id, "r");
710 p = fgets(buf, sizeof(buf), file);
716 for (i = 0; i < strlen(p); i++)
717 p[i] = toupper(p[i]);
719 if (!strcmp(p, mac)) {
721 * Found the MAC match; return the interface
722 * name. The caller will free the memory.
724 if_name = strdup(entry->d_name);
737 static void kvp_process_ipconfig_file(char *cmd,
738 char *config_buf, unsigned int len,
739 int element_size, int offset)
747 * First execute the command.
749 file = popen(cmd, "r");
754 memset(config_buf, 0, len);
755 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
756 if (len < strlen(config_buf) + element_size + 1)
763 strcat(config_buf, p);
764 strcat(config_buf, ";");
769 static void kvp_get_ipconfig_info(char *if_name,
770 struct hv_kvp_ipaddr_value *buffer)
778 * Get the address of default gateway (ipv4).
780 sprintf(cmd, "%s %s", "ip route show dev", if_name);
781 strcat(cmd, " | awk '/default/ {print $3 }'");
784 * Execute the command to gather gateway info.
786 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
787 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
790 * Get the address of default gateway (ipv6).
792 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
793 strcat(cmd, " | awk '/default/ {print $3 }'");
796 * Execute the command to gather gateway info (ipv6).
798 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
799 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
803 * Gather the DNS state.
804 * Since there is no standard way to get this information
805 * across various distributions of interest; we just invoke
806 * an external script that needs to be ported across distros
809 * Following is the expected format of the information from the script:
811 * ipaddr1 (nameserver1)
812 * ipaddr2 (nameserver2)
817 sprintf(cmd, KVP_SCRIPTS_PATH "%s", "hv_get_dns_info");
820 * Execute the command to gather DNS info.
822 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
823 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
826 * Gather the DHCP state.
827 * We will gather this state by invoking an external script.
828 * The parameter to the script is the interface name.
829 * Here is the expected output:
831 * Enabled: DHCP enabled.
834 sprintf(cmd, KVP_SCRIPTS_PATH "%s %s", "hv_get_dhcp_info", if_name);
836 file = popen(cmd, "r");
840 p = fgets(dhcp_info, sizeof(dhcp_info), file);
846 if (!strncmp(p, "Enabled", 7))
847 buffer->dhcp_enabled = 1;
849 buffer->dhcp_enabled = 0;
855 static unsigned int hweight32(unsigned int *w)
857 unsigned int res = *w - ((*w >> 1) & 0x55555555);
858 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
859 res = (res + (res >> 4)) & 0x0F0F0F0F;
860 res = res + (res >> 8);
861 return (res + (res >> 16)) & 0x000000FF;
864 static int kvp_process_ip_address(void *addrp,
865 int family, char *buffer,
866 int length, int *offset)
868 struct sockaddr_in *addr;
869 struct sockaddr_in6 *addr6;
874 if (family == AF_INET) {
875 addr = (struct sockaddr_in *)addrp;
876 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
877 addr_length = INET_ADDRSTRLEN;
879 addr6 = (struct sockaddr_in6 *)addrp;
880 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
881 addr_length = INET6_ADDRSTRLEN;
884 if ((length - *offset) < addr_length + 2)
887 strcpy(buffer, "inet_ntop failed\n");
897 *offset += strlen(str) + 1;
903 kvp_get_ip_info(int family, char *if_name, int op,
904 void *out_buffer, unsigned int length)
906 struct ifaddrs *ifap;
907 struct ifaddrs *curp;
912 struct hv_kvp_ipaddr_value *ip_buffer;
913 char cidr_mask[5]; /* /xyz */
918 struct sockaddr_in6 *addr6;
920 if (op == KVP_OP_ENUMERATE) {
923 ip_buffer = out_buffer;
924 buffer = (char *)ip_buffer->ip_addr;
925 ip_buffer->addr_family = 0;
928 * On entry into this function, the buffer is capable of holding the
932 if (getifaddrs(&ifap)) {
933 strcpy(buffer, "getifaddrs failed\n");
938 while (curp != NULL) {
939 if (curp->ifa_addr == NULL) {
940 curp = curp->ifa_next;
944 if ((if_name != NULL) &&
945 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
947 * We want info about a specific interface;
950 curp = curp->ifa_next;
955 * We only support two address families: AF_INET and AF_INET6.
956 * If a family value of 0 is specified, we collect both
957 * supported address families; if not we gather info on
958 * the specified address family.
960 if ((((family != 0) &&
961 (curp->ifa_addr->sa_family != family))) ||
962 (curp->ifa_flags & IFF_LOOPBACK)) {
963 curp = curp->ifa_next;
966 if ((curp->ifa_addr->sa_family != AF_INET) &&
967 (curp->ifa_addr->sa_family != AF_INET6)) {
968 curp = curp->ifa_next;
972 if (op == KVP_OP_GET_IP_INFO) {
974 * Gather info other than the IP address.
975 * IP address info will be gathered later.
977 if (curp->ifa_addr->sa_family == AF_INET) {
978 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
982 error = kvp_process_ip_address(
992 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
995 * Get subnet info in CIDR format.
998 sn_str = (char *)ip_buffer->sub_net;
999 addr6 = (struct sockaddr_in6 *)
1001 w = addr6->sin6_addr.s6_addr32;
1003 for (i = 0; i < 4; i++)
1004 weight += hweight32(&w[i]);
1006 sprintf(cidr_mask, "/%d", weight);
1007 if (length < sn_offset + strlen(cidr_mask) + 1)
1011 strcpy(sn_str, cidr_mask);
1013 strcat((char *)ip_buffer->sub_net, ";");
1014 strcat(sn_str, cidr_mask);
1016 sn_offset += strlen(sn_str) + 1;
1020 * Collect other ip related configuration info.
1023 kvp_get_ipconfig_info(if_name, ip_buffer);
1027 error = kvp_process_ip_address(curp->ifa_addr,
1028 curp->ifa_addr->sa_family,
1034 curp = curp->ifa_next;
1043 static int expand_ipv6(char *addr, int type)
1046 struct in6_addr v6_addr;
1048 ret = inet_pton(AF_INET6, addr, &v6_addr);
1051 if (type == NETMASK)
1056 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1057 "%02x%02x:%02x%02x:%02x%02x",
1058 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
1059 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
1060 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
1061 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
1062 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
1063 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
1064 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
1065 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
1071 static int is_ipv4(char *addr)
1074 struct in_addr ipv4_addr;
1076 ret = inet_pton(AF_INET, addr, &ipv4_addr);
1083 static int parse_ip_val_buffer(char *in_buf, int *offset,
1084 char *out_buf, int out_len)
1090 * in_buf has sequence of characters that are seperated by
1091 * the character ';'. The last sequence does not have the
1092 * terminating ";" character.
1094 start = in_buf + *offset;
1096 x = strchr(start, ';');
1100 x = start + strlen(start);
1102 if (strlen(start) != 0) {
1105 * Get rid of leading spaces.
1107 while (start[i] == ' ')
1110 if ((x - start) <= out_len) {
1111 strcpy(out_buf, (start + i));
1112 *offset += (x - start) + 1;
1119 static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
1123 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
1132 static int process_ip_string(FILE *f, char *ip_string, int type)
1135 char addr[INET6_ADDRSTRLEN];
1142 memset(addr, 0, sizeof(addr));
1144 while (parse_ip_val_buffer(ip_string, &offset, addr,
1145 (MAX_IP_ADDR_SIZE * 2))) {
1148 if (is_ipv4(addr)) {
1151 snprintf(str, sizeof(str), "%s", "IPADDR");
1154 snprintf(str, sizeof(str), "%s", "NETMASK");
1157 snprintf(str, sizeof(str), "%s", "GATEWAY");
1160 snprintf(str, sizeof(str), "%s", "DNS");
1165 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1166 } else if (type == GATEWAY && i == 0) {
1169 snprintf(sub_str, sizeof(sub_str), "%d", i++);
1173 } else if (expand_ipv6(addr, type)) {
1176 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1179 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1182 snprintf(str, sizeof(str), "%s",
1186 snprintf(str, sizeof(str), "%s", "DNS");
1191 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1192 } else if (j == 0) {
1195 snprintf(sub_str, sizeof(sub_str), "_%d", j++);
1198 return HV_INVALIDARG;
1201 error = kvp_write_file(f, str, sub_str, addr);
1204 memset(addr, 0, sizeof(addr));
1210 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1213 char if_file[PATH_MAX];
1219 * Set the configuration for the specified interface with
1220 * the information provided. Since there is no standard
1221 * way to configure an interface, we will have an external
1222 * script that does the job of configuring the interface and
1223 * flushing the configuration.
1225 * The parameters passed to this external script are:
1226 * 1. A configuration file that has the specified configuration.
1228 * We will embed the name of the interface in the configuration
1229 * file: ifcfg-ethx (where ethx is the interface name).
1231 * The information provided here may be more than what is needed
1232 * in a given distro to configure the interface and so are free
1233 * ignore information that may not be relevant.
1235 * Here is the format of the ip configuration file:
1238 * DEVICE=interface name
1239 * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
1240 * or "none" if no boot-time protocol should be used)
1244 * IPADDRx=ipaddry (where y = x + 1)
1247 * NETMASKx=netmasky (where y = x + 1)
1250 * GATEWAYx=ipaddry (where y = x + 1)
1252 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1254 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1255 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1258 * The host can specify multiple ipv4 and ipv6 addresses to be
1259 * configured for the interface. Furthermore, the configuration
1260 * needs to be persistent. A subsequent GET call on the interface
1261 * is expected to return the configuration that is set via the SET
1265 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1266 "/ifcfg-", if_name);
1268 file = fopen(if_file, "w");
1271 syslog(LOG_ERR, "Failed to open config file; error: %d %s",
1272 errno, strerror(errno));
1277 * First write out the MAC address.
1280 mac_addr = kvp_if_name_to_mac(if_name);
1281 if (mac_addr == NULL) {
1286 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1291 error = kvp_write_file(file, "DEVICE", "", if_name);
1296 * The dhcp_enabled flag is only for IPv4. In the case the host only
1297 * injects an IPv6 address, the flag is true, but we still need to
1298 * proceed to parse and pass the IPv6 information to the
1299 * disto-specific script hv_set_ifconfig.
1301 if (new_val->dhcp_enabled) {
1302 error = kvp_write_file(file, "BOOTPROTO", "", "dhcp");
1307 error = kvp_write_file(file, "BOOTPROTO", "", "none");
1313 * Write the configuration for ipaddress, netmask, gateway and
1317 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1321 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1325 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1329 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1336 * Now that we have populated the configuration file,
1337 * invoke the external script to do its magic.
1340 snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s",
1341 "hv_set_ifconfig", if_file);
1343 syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
1344 cmd, errno, strerror(errno));
1350 syslog(LOG_ERR, "Failed to write config file");
1357 kvp_get_domain_name(char *buffer, int length)
1359 struct addrinfo hints, *info ;
1362 gethostname(buffer, length);
1363 memset(&hints, 0, sizeof(hints));
1364 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1365 hints.ai_socktype = SOCK_STREAM;
1366 hints.ai_flags = AI_CANONNAME;
1368 error = getaddrinfo(buffer, NULL, &hints, &info);
1370 snprintf(buffer, length, "getaddrinfo failed: 0x%x %s",
1371 error, gai_strerror(error));
1374 snprintf(buffer, length, "%s", info->ai_canonname);
1378 void print_usage(char *argv[])
1380 fprintf(stderr, "Usage: %s [options]\n"
1382 " -n, --no-daemon stay in foreground, don't daemonize\n"
1383 " -h, --help print this help\n", argv[0]);
1386 int main(int argc, char *argv[])
1392 struct hv_kvp_msg hv_msg[1];
1398 struct hv_kvp_ipaddr_value *kvp_ip_val;
1399 int daemonize = 1, long_index = 0, opt;
1401 static struct option long_options[] = {
1402 {"help", no_argument, 0, 'h' },
1403 {"no-daemon", no_argument, 0, 'n' },
1407 while ((opt = getopt_long(argc, argv, "hn", long_options,
1408 &long_index)) != -1) {
1420 if (daemonize && daemon(1, 0))
1423 openlog("KVP", 0, LOG_USER);
1424 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1426 kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
1429 syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
1430 errno, strerror(errno));
1435 * Retrieve OS release information.
1439 * Cache Fully Qualified Domain Name because getaddrinfo takes an
1440 * unpredictable amount of time to finish.
1442 kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
1444 if (kvp_file_init()) {
1445 syslog(LOG_ERR, "Failed to initialize the pools");
1450 * Register ourselves with the kernel.
1452 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
1453 len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
1454 if (len != sizeof(struct hv_kvp_msg)) {
1455 syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
1456 errno, strerror(errno));
1464 pfd.events = POLLIN;
1467 if (poll(&pfd, 1, -1) < 0) {
1468 syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
1469 if (errno == EINVAL) {
1477 len = read(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
1479 if (len != sizeof(struct hv_kvp_msg)) {
1480 syslog(LOG_ERR, "read failed; error:%d %s",
1481 errno, strerror(errno));
1484 return EXIT_FAILURE;
1488 * We will use the KVP header information to pass back
1489 * the error from this daemon. So, first copy the state
1490 * and set the error code to success.
1492 op = hv_msg->kvp_hdr.operation;
1493 pool = hv_msg->kvp_hdr.pool;
1494 hv_msg->error = HV_S_OK;
1496 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
1498 * Driver is registering with us; stash away the version
1502 p = (char *)hv_msg->body.kvp_register.version;
1503 lic_version = malloc(strlen(p) + 1);
1505 strcpy(lic_version, p);
1506 syslog(LOG_INFO, "KVP LIC Version: %s",
1509 syslog(LOG_ERR, "malloc failed");
1515 case KVP_OP_GET_IP_INFO:
1516 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1518 kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
1520 if (if_name == NULL) {
1522 * We could not map the mac address to an
1523 * interface name; return error.
1525 hv_msg->error = HV_E_FAIL;
1528 error = kvp_get_ip_info(
1529 0, if_name, KVP_OP_GET_IP_INFO,
1531 (MAX_IP_ADDR_SIZE * 2));
1534 hv_msg->error = error;
1539 case KVP_OP_SET_IP_INFO:
1540 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1541 if_name = kvp_get_if_name(
1542 (char *)kvp_ip_val->adapter_id);
1543 if (if_name == NULL) {
1545 * We could not map the guid to an
1546 * interface name; return error.
1548 hv_msg->error = HV_GUID_NOTFOUND;
1551 error = kvp_set_ip_info(if_name, kvp_ip_val);
1553 hv_msg->error = error;
1559 if (kvp_key_add_or_modify(pool,
1560 hv_msg->body.kvp_set.data.key,
1561 hv_msg->body.kvp_set.data.key_size,
1562 hv_msg->body.kvp_set.data.value,
1563 hv_msg->body.kvp_set.data.value_size))
1564 hv_msg->error = HV_S_CONT;
1568 if (kvp_get_value(pool,
1569 hv_msg->body.kvp_set.data.key,
1570 hv_msg->body.kvp_set.data.key_size,
1571 hv_msg->body.kvp_set.data.value,
1572 hv_msg->body.kvp_set.data.value_size))
1573 hv_msg->error = HV_S_CONT;
1577 if (kvp_key_delete(pool,
1578 hv_msg->body.kvp_delete.key,
1579 hv_msg->body.kvp_delete.key_size))
1580 hv_msg->error = HV_S_CONT;
1587 if (op != KVP_OP_ENUMERATE)
1591 * If the pool is KVP_POOL_AUTO, dynamically generate
1592 * both the key and the value; if not read from the
1595 if (pool != KVP_POOL_AUTO) {
1596 if (kvp_pool_enumerate(pool,
1597 hv_msg->body.kvp_enum_data.index,
1598 hv_msg->body.kvp_enum_data.data.key,
1599 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1600 hv_msg->body.kvp_enum_data.data.value,
1601 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1602 hv_msg->error = HV_S_CONT;
1606 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1607 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
1609 switch (hv_msg->body.kvp_enum_data.index) {
1610 case FullyQualifiedDomainName:
1611 strcpy(key_value, full_domain_name);
1612 strcpy(key_name, "FullyQualifiedDomainName");
1614 case IntegrationServicesVersion:
1615 strcpy(key_name, "IntegrationServicesVersion");
1616 strcpy(key_value, lic_version);
1618 case NetworkAddressIPv4:
1619 kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
1620 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1621 strcpy(key_name, "NetworkAddressIPv4");
1623 case NetworkAddressIPv6:
1624 kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
1625 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1626 strcpy(key_name, "NetworkAddressIPv6");
1629 strcpy(key_value, os_build);
1630 strcpy(key_name, "OSBuildNumber");
1633 strcpy(key_value, os_name);
1634 strcpy(key_name, "OSName");
1636 case OSMajorVersion:
1637 strcpy(key_value, os_major);
1638 strcpy(key_name, "OSMajorVersion");
1640 case OSMinorVersion:
1641 strcpy(key_value, os_minor);
1642 strcpy(key_name, "OSMinorVersion");
1645 strcpy(key_value, os_version);
1646 strcpy(key_name, "OSVersion");
1648 case ProcessorArchitecture:
1649 strcpy(key_value, processor_arch);
1650 strcpy(key_name, "ProcessorArchitecture");
1653 hv_msg->error = HV_S_CONT;
1657 /* Send the value back to the kernel. */
1659 len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
1660 if (len != sizeof(struct hv_kvp_msg)) {
1661 syslog(LOG_ERR, "write failed; error: %d %s", errno,