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>
45 * KVP protocol: The user mode component first registers with the
46 * the kernel component. Subsequently, the kernel component requests, data
47 * for the specified keys. In response to this message the user mode component
48 * fills in the value corresponding to the specified key. We overload the
49 * sequence field in the cn_msg header to define our KVP message types.
51 * We use this infrastructure for also supporting queries from user mode
52 * application for state that may be maintained in the KVP kernel component.
58 FullyQualifiedDomainName = 0,
59 IntegrationServicesVersion, /*This key is serviced in the kernel*/
78 static int in_hand_shake = 1;
80 static char *os_name = "";
81 static char *os_major = "";
82 static char *os_minor = "";
83 static char *processor_arch;
84 static char *os_build;
85 static char *os_version;
86 static char *lic_version = "Unknown version";
87 static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
88 static struct utsname uts_buf;
91 * The location of the interface configuration file.
94 #define KVP_CONFIG_LOC "/var/lib/hyperv"
96 #define MAX_FILE_NAME 100
97 #define ENTRIES_PER_BLOCK 50
100 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
101 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
104 struct kvp_file_state {
107 struct kvp_record *records;
109 char fname[MAX_FILE_NAME];
112 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
114 static void kvp_acquire_lock(int pool)
116 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
119 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
120 syslog(LOG_ERR, "Failed to acquire the lock pool: %d; error: %d %s", pool,
121 errno, strerror(errno));
126 static void kvp_release_lock(int pool)
128 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
131 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
132 syslog(LOG_ERR, "Failed to release the lock pool: %d; error: %d %s", pool,
133 errno, strerror(errno));
138 static void kvp_update_file(int pool)
143 * We are going to write our in-memory registry out to
144 * disk; acquire the lock first.
146 kvp_acquire_lock(pool);
148 filep = fopen(kvp_file_info[pool].fname, "we");
150 syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
151 errno, strerror(errno));
152 kvp_release_lock(pool);
156 fwrite(kvp_file_info[pool].records, sizeof(struct kvp_record),
157 kvp_file_info[pool].num_records, filep);
159 if (ferror(filep) || fclose(filep)) {
160 kvp_release_lock(pool);
161 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
165 kvp_release_lock(pool);
168 static void kvp_update_mem_state(int pool)
171 size_t records_read = 0;
172 struct kvp_record *record = kvp_file_info[pool].records;
173 struct kvp_record *readp;
174 int num_blocks = kvp_file_info[pool].num_blocks;
175 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
177 kvp_acquire_lock(pool);
179 filep = fopen(kvp_file_info[pool].fname, "re");
181 syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
182 errno, strerror(errno));
183 kvp_release_lock(pool);
187 readp = &record[records_read];
188 records_read += fread(readp, sizeof(struct kvp_record),
189 ENTRIES_PER_BLOCK * num_blocks,
193 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
199 * We have more data to read.
202 record = realloc(record, alloc_unit * num_blocks);
204 if (record == NULL) {
205 syslog(LOG_ERR, "malloc failed");
213 kvp_file_info[pool].num_blocks = num_blocks;
214 kvp_file_info[pool].records = record;
215 kvp_file_info[pool].num_records = records_read;
218 kvp_release_lock(pool);
220 static int kvp_file_init(void)
226 struct kvp_record *record;
227 struct kvp_record *readp;
230 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
232 if (access(KVP_CONFIG_LOC, F_OK)) {
233 if (mkdir(KVP_CONFIG_LOC, 0755 /* rwxr-xr-x */)) {
234 syslog(LOG_ERR, "Failed to create '%s'; error: %d %s", KVP_CONFIG_LOC,
235 errno, strerror(errno));
240 for (i = 0; i < KVP_POOL_COUNT; i++) {
241 fname = kvp_file_info[i].fname;
244 sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
245 fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
251 filep = fopen(fname, "re");
257 record = malloc(alloc_unit * num_blocks);
258 if (record == NULL) {
264 readp = &record[records_read];
265 records_read += fread(readp, sizeof(struct kvp_record),
270 syslog(LOG_ERR, "Failed to read file, pool: %d",
277 * We have more data to read.
280 record = realloc(record, alloc_unit *
282 if (record == NULL) {
291 kvp_file_info[i].fd = fd;
292 kvp_file_info[i].num_blocks = num_blocks;
293 kvp_file_info[i].records = record;
294 kvp_file_info[i].num_records = records_read;
302 static int kvp_key_delete(int pool, const __u8 *key, int key_size)
307 struct kvp_record *record;
310 * First update the in-memory state.
312 kvp_update_mem_state(pool);
314 num_records = kvp_file_info[pool].num_records;
315 record = kvp_file_info[pool].records;
317 for (i = 0; i < num_records; i++) {
318 if (memcmp(key, record[i].key, key_size))
321 * Found a match; just move the remaining
324 if (i == num_records) {
325 kvp_file_info[pool].num_records--;
326 kvp_update_file(pool);
332 for (; k < num_records; k++) {
333 strcpy(record[j].key, record[k].key);
334 strcpy(record[j].value, record[k].value);
338 kvp_file_info[pool].num_records--;
339 kvp_update_file(pool);
345 static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
346 const __u8 *value, int value_size)
350 struct kvp_record *record;
353 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
354 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
358 * First update the in-memory state.
360 kvp_update_mem_state(pool);
362 num_records = kvp_file_info[pool].num_records;
363 record = kvp_file_info[pool].records;
364 num_blocks = kvp_file_info[pool].num_blocks;
366 for (i = 0; i < num_records; i++) {
367 if (memcmp(key, record[i].key, key_size))
370 * Found a match; just update the value -
371 * this is the modify case.
373 memcpy(record[i].value, value, value_size);
374 kvp_update_file(pool);
379 * Need to add a new entry;
381 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
382 /* Need to allocate a larger array for reg entries. */
383 record = realloc(record, sizeof(struct kvp_record) *
384 ENTRIES_PER_BLOCK * (num_blocks + 1));
388 kvp_file_info[pool].num_blocks++;
391 memcpy(record[i].value, value, value_size);
392 memcpy(record[i].key, key, key_size);
393 kvp_file_info[pool].records = record;
394 kvp_file_info[pool].num_records++;
395 kvp_update_file(pool);
399 static int kvp_get_value(int pool, const __u8 *key, int key_size, __u8 *value,
404 struct kvp_record *record;
406 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
407 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
411 * First update the in-memory state.
413 kvp_update_mem_state(pool);
415 num_records = kvp_file_info[pool].num_records;
416 record = kvp_file_info[pool].records;
418 for (i = 0; i < num_records; i++) {
419 if (memcmp(key, record[i].key, key_size))
422 * Found a match; just copy the value out.
424 memcpy(value, record[i].value, value_size);
431 static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
432 __u8 *value, int value_size)
434 struct kvp_record *record;
437 * First update our in-memory database.
439 kvp_update_mem_state(pool);
440 record = kvp_file_info[pool].records;
442 if (index >= kvp_file_info[pool].num_records) {
446 memcpy(key, record[index].key, key_size);
447 memcpy(value, record[index].value, value_size);
452 void kvp_get_os_info(void)
458 os_version = uts_buf.release;
459 os_build = strdup(uts_buf.release);
461 os_name = uts_buf.sysname;
462 processor_arch = uts_buf.machine;
465 * The current windows host (win7) expects the build
466 * string to be of the form: x.y.z
467 * Strip additional information we may have.
469 p = strchr(os_version, '-');
474 * Parse the /etc/os-release file if present:
475 * http://www.freedesktop.org/software/systemd/man/os-release.html
477 file = fopen("/etc/os-release", "r");
479 while (fgets(buf, sizeof(buf), file)) {
482 /* Ignore comments */
486 /* Split into name=value */
487 p = strchr(buf, '=');
492 /* Remove quotes and newline; un-escape */
501 } else if (*p == '\'' || *p == '"' ||
510 if (!strcmp(buf, "NAME")) {
515 } else if (!strcmp(buf, "VERSION_ID")) {
526 /* Fallback for older RH/SUSE releases */
527 file = fopen("/etc/SuSE-release", "r");
529 goto kvp_osinfo_found;
530 file = fopen("/etc/redhat-release", "r");
532 goto kvp_osinfo_found;
535 * We don't have information about the os.
540 /* up to three lines */
541 p = fgets(buf, sizeof(buf), file);
543 p = strchr(buf, '\n');
552 p = fgets(buf, sizeof(buf), file);
554 p = strchr(buf, '\n');
563 p = fgets(buf, sizeof(buf), file);
565 p = strchr(buf, '\n');
583 * Retrieve an interface name corresponding to the specified guid.
584 * If there is a match, the function returns a pointer
585 * to the interface name and if not, a NULL is returned.
586 * If a match is found, the caller is responsible for
587 * freeing the memory.
590 static char *kvp_get_if_name(char *guid)
593 struct dirent *entry;
596 char *if_name = NULL;
598 char *kvp_net_dir = "/sys/class/net/";
601 dir = opendir(kvp_net_dir);
605 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
606 q = dev_id + strlen(kvp_net_dir);
608 while ((entry = readdir(dir)) != NULL) {
610 * Set the state for the next pass.
613 strcat(dev_id, entry->d_name);
614 strcat(dev_id, "/device/device_id");
616 file = fopen(dev_id, "r");
620 p = fgets(buf, sizeof(buf), file);
626 if (!strcmp(p, guid)) {
628 * Found the guid match; return the interface
629 * name. The caller will free the memory.
631 if_name = strdup(entry->d_name);
644 * Retrieve the MAC address given the interface name.
647 static char *kvp_if_name_to_mac(char *if_name)
654 char *mac_addr = NULL;
656 snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
657 if_name, "/address");
659 file = fopen(addr_file, "r");
663 p = fgets(buf, sizeof(buf), file);
668 for (i = 0; i < strlen(p); i++)
669 p[i] = toupper(p[i]);
670 mac_addr = strdup(p);
679 * Retrieve the interface name given tha MAC address.
682 static char *kvp_mac_to_if_name(char *mac)
685 struct dirent *entry;
688 char *if_name = NULL;
690 char *kvp_net_dir = "/sys/class/net/";
694 dir = opendir(kvp_net_dir);
698 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
699 q = dev_id + strlen(kvp_net_dir);
701 while ((entry = readdir(dir)) != NULL) {
703 * Set the state for the next pass.
707 strcat(dev_id, entry->d_name);
708 strcat(dev_id, "/address");
710 file = fopen(dev_id, "r");
714 p = fgets(buf, sizeof(buf), file);
720 for (i = 0; i < strlen(p); i++)
721 p[i] = toupper(p[i]);
723 if (!strcmp(p, mac)) {
725 * Found the MAC match; return the interface
726 * name. The caller will free the memory.
728 if_name = strdup(entry->d_name);
741 static void kvp_process_ipconfig_file(char *cmd,
742 char *config_buf, unsigned int len,
743 int element_size, int offset)
751 * First execute the command.
753 file = popen(cmd, "r");
758 memset(config_buf, 0, len);
759 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
760 if (len < strlen(config_buf) + element_size + 1)
767 strcat(config_buf, p);
768 strcat(config_buf, ";");
773 static void kvp_get_ipconfig_info(char *if_name,
774 struct hv_kvp_ipaddr_value *buffer)
782 * Get the address of default gateway (ipv4).
784 sprintf(cmd, "%s %s", "ip route show dev", if_name);
785 strcat(cmd, " | awk '/default/ {print $3 }'");
788 * Execute the command to gather gateway info.
790 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
791 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
794 * Get the address of default gateway (ipv6).
796 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
797 strcat(cmd, " | awk '/default/ {print $3 }'");
800 * Execute the command to gather gateway info (ipv6).
802 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
803 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
807 * Gather the DNS state.
808 * Since there is no standard way to get this information
809 * across various distributions of interest; we just invoke
810 * an external script that needs to be ported across distros
813 * Following is the expected format of the information from the script:
815 * ipaddr1 (nameserver1)
816 * ipaddr2 (nameserver2)
821 sprintf(cmd, "%s", "hv_get_dns_info");
824 * Execute the command to gather DNS info.
826 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
827 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
830 * Gather the DHCP state.
831 * We will gather this state by invoking an external script.
832 * The parameter to the script is the interface name.
833 * Here is the expected output:
835 * Enabled: DHCP enabled.
838 sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
840 file = popen(cmd, "r");
844 p = fgets(dhcp_info, sizeof(dhcp_info), file);
850 if (!strncmp(p, "Enabled", 7))
851 buffer->dhcp_enabled = 1;
853 buffer->dhcp_enabled = 0;
859 static unsigned int hweight32(unsigned int *w)
861 unsigned int res = *w - ((*w >> 1) & 0x55555555);
862 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
863 res = (res + (res >> 4)) & 0x0F0F0F0F;
864 res = res + (res >> 8);
865 return (res + (res >> 16)) & 0x000000FF;
868 static int kvp_process_ip_address(void *addrp,
869 int family, char *buffer,
870 int length, int *offset)
872 struct sockaddr_in *addr;
873 struct sockaddr_in6 *addr6;
878 if (family == AF_INET) {
879 addr = (struct sockaddr_in *)addrp;
880 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
881 addr_length = INET_ADDRSTRLEN;
883 addr6 = (struct sockaddr_in6 *)addrp;
884 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
885 addr_length = INET6_ADDRSTRLEN;
888 if ((length - *offset) < addr_length + 2)
891 strcpy(buffer, "inet_ntop failed\n");
901 *offset += strlen(str) + 1;
907 kvp_get_ip_info(int family, char *if_name, int op,
908 void *out_buffer, unsigned int length)
910 struct ifaddrs *ifap;
911 struct ifaddrs *curp;
916 struct hv_kvp_ipaddr_value *ip_buffer;
917 char cidr_mask[5]; /* /xyz */
922 struct sockaddr_in6 *addr6;
924 if (op == KVP_OP_ENUMERATE) {
927 ip_buffer = out_buffer;
928 buffer = (char *)ip_buffer->ip_addr;
929 ip_buffer->addr_family = 0;
932 * On entry into this function, the buffer is capable of holding the
936 if (getifaddrs(&ifap)) {
937 strcpy(buffer, "getifaddrs failed\n");
942 while (curp != NULL) {
943 if (curp->ifa_addr == NULL) {
944 curp = curp->ifa_next;
948 if ((if_name != NULL) &&
949 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
951 * We want info about a specific interface;
954 curp = curp->ifa_next;
959 * We only support two address families: AF_INET and AF_INET6.
960 * If a family value of 0 is specified, we collect both
961 * supported address families; if not we gather info on
962 * the specified address family.
964 if ((((family != 0) &&
965 (curp->ifa_addr->sa_family != family))) ||
966 (curp->ifa_flags & IFF_LOOPBACK)) {
967 curp = curp->ifa_next;
970 if ((curp->ifa_addr->sa_family != AF_INET) &&
971 (curp->ifa_addr->sa_family != AF_INET6)) {
972 curp = curp->ifa_next;
976 if (op == KVP_OP_GET_IP_INFO) {
978 * Gather info other than the IP address.
979 * IP address info will be gathered later.
981 if (curp->ifa_addr->sa_family == AF_INET) {
982 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
986 error = kvp_process_ip_address(
996 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
999 * Get subnet info in CIDR format.
1002 sn_str = (char *)ip_buffer->sub_net;
1003 addr6 = (struct sockaddr_in6 *)
1005 w = addr6->sin6_addr.s6_addr32;
1007 for (i = 0; i < 4; i++)
1008 weight += hweight32(&w[i]);
1010 sprintf(cidr_mask, "/%d", weight);
1011 if (length < sn_offset + strlen(cidr_mask) + 1)
1015 strcpy(sn_str, cidr_mask);
1017 strcat((char *)ip_buffer->sub_net, ";");
1018 strcat(sn_str, cidr_mask);
1020 sn_offset += strlen(sn_str) + 1;
1024 * Collect other ip related configuration info.
1027 kvp_get_ipconfig_info(if_name, ip_buffer);
1031 error = kvp_process_ip_address(curp->ifa_addr,
1032 curp->ifa_addr->sa_family,
1038 curp = curp->ifa_next;
1047 static int expand_ipv6(char *addr, int type)
1050 struct in6_addr v6_addr;
1052 ret = inet_pton(AF_INET6, addr, &v6_addr);
1055 if (type == NETMASK)
1060 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1061 "%02x%02x:%02x%02x:%02x%02x",
1062 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
1063 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
1064 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
1065 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
1066 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
1067 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
1068 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
1069 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
1075 static int is_ipv4(char *addr)
1078 struct in_addr ipv4_addr;
1080 ret = inet_pton(AF_INET, addr, &ipv4_addr);
1087 static int parse_ip_val_buffer(char *in_buf, int *offset,
1088 char *out_buf, int out_len)
1094 * in_buf has sequence of characters that are seperated by
1095 * the character ';'. The last sequence does not have the
1096 * terminating ";" character.
1098 start = in_buf + *offset;
1100 x = strchr(start, ';');
1104 x = start + strlen(start);
1106 if (strlen(start) != 0) {
1109 * Get rid of leading spaces.
1111 while (start[i] == ' ')
1114 if ((x - start) <= out_len) {
1115 strcpy(out_buf, (start + i));
1116 *offset += (x - start) + 1;
1123 static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
1127 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
1136 static int process_ip_string(FILE *f, char *ip_string, int type)
1139 char addr[INET6_ADDRSTRLEN];
1146 memset(addr, 0, sizeof(addr));
1148 while (parse_ip_val_buffer(ip_string, &offset, addr,
1149 (MAX_IP_ADDR_SIZE * 2))) {
1152 if (is_ipv4(addr)) {
1155 snprintf(str, sizeof(str), "%s", "IPADDR");
1158 snprintf(str, sizeof(str), "%s", "NETMASK");
1161 snprintf(str, sizeof(str), "%s", "GATEWAY");
1164 snprintf(str, sizeof(str), "%s", "DNS");
1169 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1170 } else if (type == GATEWAY && i == 0) {
1173 snprintf(sub_str, sizeof(sub_str), "%d", i++);
1177 } else if (expand_ipv6(addr, type)) {
1180 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1183 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1186 snprintf(str, sizeof(str), "%s",
1190 snprintf(str, sizeof(str), "%s", "DNS");
1195 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1196 } else if (j == 0) {
1199 snprintf(sub_str, sizeof(sub_str), "_%d", j++);
1202 return HV_INVALIDARG;
1205 error = kvp_write_file(f, str, sub_str, addr);
1208 memset(addr, 0, sizeof(addr));
1214 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1223 * Set the configuration for the specified interface with
1224 * the information provided. Since there is no standard
1225 * way to configure an interface, we will have an external
1226 * script that does the job of configuring the interface and
1227 * flushing the configuration.
1229 * The parameters passed to this external script are:
1230 * 1. A configuration file that has the specified configuration.
1232 * We will embed the name of the interface in the configuration
1233 * file: ifcfg-ethx (where ethx is the interface name).
1235 * The information provided here may be more than what is needed
1236 * in a given distro to configure the interface and so are free
1237 * ignore information that may not be relevant.
1239 * Here is the format of the ip configuration file:
1242 * DEVICE=interface name
1243 * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
1244 * or "none" if no boot-time protocol should be used)
1248 * IPADDRx=ipaddry (where y = x + 1)
1251 * NETMASKx=netmasky (where y = x + 1)
1254 * GATEWAYx=ipaddry (where y = x + 1)
1256 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1258 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1259 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1262 * The host can specify multiple ipv4 and ipv6 addresses to be
1263 * configured for the interface. Furthermore, the configuration
1264 * needs to be persistent. A subsequent GET call on the interface
1265 * is expected to return the configuration that is set via the SET
1269 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1270 "/ifcfg-", if_name);
1272 file = fopen(if_file, "w");
1275 syslog(LOG_ERR, "Failed to open config file; error: %d %s",
1276 errno, strerror(errno));
1281 * First write out the MAC address.
1284 mac_addr = kvp_if_name_to_mac(if_name);
1285 if (mac_addr == NULL) {
1290 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1295 error = kvp_write_file(file, "DEVICE", "", if_name);
1300 * The dhcp_enabled flag is only for IPv4. In the case the host only
1301 * injects an IPv6 address, the flag is true, but we still need to
1302 * proceed to parse and pass the IPv6 information to the
1303 * disto-specific script hv_set_ifconfig.
1305 if (new_val->dhcp_enabled) {
1306 error = kvp_write_file(file, "BOOTPROTO", "", "dhcp");
1311 error = kvp_write_file(file, "BOOTPROTO", "", "none");
1317 * Write the configuration for ipaddress, netmask, gateway and
1321 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1325 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1329 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1333 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1340 * Now that we have populated the configuration file,
1341 * invoke the external script to do its magic.
1344 snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1346 syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
1347 cmd, errno, strerror(errno));
1353 syslog(LOG_ERR, "Failed to write config file");
1360 kvp_get_domain_name(char *buffer, int length)
1362 struct addrinfo hints, *info ;
1365 gethostname(buffer, length);
1366 memset(&hints, 0, sizeof(hints));
1367 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1368 hints.ai_socktype = SOCK_STREAM;
1369 hints.ai_flags = AI_CANONNAME;
1371 error = getaddrinfo(buffer, NULL, &hints, &info);
1373 snprintf(buffer, length, "getaddrinfo failed: 0x%x %s",
1374 error, gai_strerror(error));
1377 snprintf(buffer, length, "%s", info->ai_canonname);
1381 void print_usage(char *argv[])
1383 fprintf(stderr, "Usage: %s [options]\n"
1385 " -n, --no-daemon stay in foreground, don't daemonize\n"
1386 " -h, --help print this help\n", argv[0]);
1389 int main(int argc, char *argv[])
1395 struct hv_kvp_msg hv_msg[1];
1401 struct hv_kvp_ipaddr_value *kvp_ip_val;
1402 int daemonize = 1, long_index = 0, opt;
1404 static struct option long_options[] = {
1405 {"help", no_argument, 0, 'h' },
1406 {"no-daemon", no_argument, 0, 'n' },
1410 while ((opt = getopt_long(argc, argv, "hn", long_options,
1411 &long_index)) != -1) {
1423 if (daemonize && daemon(1, 0))
1426 openlog("KVP", 0, LOG_USER);
1427 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1429 kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
1432 syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
1433 errno, strerror(errno));
1438 * Retrieve OS release information.
1442 * Cache Fully Qualified Domain Name because getaddrinfo takes an
1443 * unpredictable amount of time to finish.
1445 kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
1447 if (kvp_file_init()) {
1448 syslog(LOG_ERR, "Failed to initialize the pools");
1453 * Register ourselves with the kernel.
1455 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
1456 len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
1457 if (len != sizeof(struct hv_kvp_msg)) {
1458 syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
1459 errno, strerror(errno));
1467 pfd.events = POLLIN;
1470 if (poll(&pfd, 1, -1) < 0) {
1471 syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
1472 if (errno == EINVAL) {
1480 len = read(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
1482 if (len != sizeof(struct hv_kvp_msg)) {
1483 syslog(LOG_ERR, "read failed; error:%d %s",
1484 errno, strerror(errno));
1487 return EXIT_FAILURE;
1491 * We will use the KVP header information to pass back
1492 * the error from this daemon. So, first copy the state
1493 * and set the error code to success.
1495 op = hv_msg->kvp_hdr.operation;
1496 pool = hv_msg->kvp_hdr.pool;
1497 hv_msg->error = HV_S_OK;
1499 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
1501 * Driver is registering with us; stash away the version
1505 p = (char *)hv_msg->body.kvp_register.version;
1506 lic_version = malloc(strlen(p) + 1);
1508 strcpy(lic_version, p);
1509 syslog(LOG_INFO, "KVP LIC Version: %s",
1512 syslog(LOG_ERR, "malloc failed");
1518 case KVP_OP_GET_IP_INFO:
1519 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1521 kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
1523 if (if_name == NULL) {
1525 * We could not map the mac address to an
1526 * interface name; return error.
1528 hv_msg->error = HV_E_FAIL;
1531 error = kvp_get_ip_info(
1532 0, if_name, KVP_OP_GET_IP_INFO,
1534 (MAX_IP_ADDR_SIZE * 2));
1537 hv_msg->error = error;
1542 case KVP_OP_SET_IP_INFO:
1543 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1544 if_name = kvp_get_if_name(
1545 (char *)kvp_ip_val->adapter_id);
1546 if (if_name == NULL) {
1548 * We could not map the guid to an
1549 * interface name; return error.
1551 hv_msg->error = HV_GUID_NOTFOUND;
1554 error = kvp_set_ip_info(if_name, kvp_ip_val);
1556 hv_msg->error = error;
1562 if (kvp_key_add_or_modify(pool,
1563 hv_msg->body.kvp_set.data.key,
1564 hv_msg->body.kvp_set.data.key_size,
1565 hv_msg->body.kvp_set.data.value,
1566 hv_msg->body.kvp_set.data.value_size))
1567 hv_msg->error = HV_S_CONT;
1571 if (kvp_get_value(pool,
1572 hv_msg->body.kvp_set.data.key,
1573 hv_msg->body.kvp_set.data.key_size,
1574 hv_msg->body.kvp_set.data.value,
1575 hv_msg->body.kvp_set.data.value_size))
1576 hv_msg->error = HV_S_CONT;
1580 if (kvp_key_delete(pool,
1581 hv_msg->body.kvp_delete.key,
1582 hv_msg->body.kvp_delete.key_size))
1583 hv_msg->error = HV_S_CONT;
1590 if (op != KVP_OP_ENUMERATE)
1594 * If the pool is KVP_POOL_AUTO, dynamically generate
1595 * both the key and the value; if not read from the
1598 if (pool != KVP_POOL_AUTO) {
1599 if (kvp_pool_enumerate(pool,
1600 hv_msg->body.kvp_enum_data.index,
1601 hv_msg->body.kvp_enum_data.data.key,
1602 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1603 hv_msg->body.kvp_enum_data.data.value,
1604 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1605 hv_msg->error = HV_S_CONT;
1609 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1610 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
1612 switch (hv_msg->body.kvp_enum_data.index) {
1613 case FullyQualifiedDomainName:
1614 strcpy(key_value, full_domain_name);
1615 strcpy(key_name, "FullyQualifiedDomainName");
1617 case IntegrationServicesVersion:
1618 strcpy(key_name, "IntegrationServicesVersion");
1619 strcpy(key_value, lic_version);
1621 case NetworkAddressIPv4:
1622 kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
1623 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1624 strcpy(key_name, "NetworkAddressIPv4");
1626 case NetworkAddressIPv6:
1627 kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
1628 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1629 strcpy(key_name, "NetworkAddressIPv6");
1632 strcpy(key_value, os_build);
1633 strcpy(key_name, "OSBuildNumber");
1636 strcpy(key_value, os_name);
1637 strcpy(key_name, "OSName");
1639 case OSMajorVersion:
1640 strcpy(key_value, os_major);
1641 strcpy(key_name, "OSMajorVersion");
1643 case OSMinorVersion:
1644 strcpy(key_value, os_minor);
1645 strcpy(key_name, "OSMinorVersion");
1648 strcpy(key_value, os_version);
1649 strcpy(key_name, "OSVersion");
1651 case ProcessorArchitecture:
1652 strcpy(key_value, processor_arch);
1653 strcpy(key_name, "ProcessorArchitecture");
1656 hv_msg->error = HV_S_CONT;
1660 /* Send the value back to the kernel. */
1662 len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
1663 if (len != sizeof(struct hv_kvp_msg)) {
1664 syslog(LOG_ERR, "write failed; error: %d %s", errno,