129194
/*
129194
 * An implementation of key value pair (KVP) functionality for Linux.
129194
 *
129194
 *
129194
 * Copyright (C) 2010, Novell, Inc.
129194
 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
129194
 *
129194
 * This program is free software; you can redistribute it and/or modify it
129194
 * under the terms of the GNU General Public License version 2 as published
129194
 * by the Free Software Foundation.
129194
 *
129194
 * This program is distributed in the hope that it will be useful, but
129194
 * WITHOUT ANY WARRANTY; without even the implied warranty of
129194
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
129194
 * NON INFRINGEMENT.  See the GNU General Public License for more
129194
 * details.
129194
 *
129194
 * You should have received a copy of the GNU General Public License
129194
 * along with this program; if not, write to the Free Software
129194
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
129194
 *
129194
 */
129194
129194
129194
#include <sys/poll.h>
129194
#include <sys/utsname.h>
129194
#include <stdio.h>
129194
#include <stdlib.h>
129194
#include <unistd.h>
129194
#include <string.h>
129194
#include <ctype.h>
129194
#include <errno.h>
129194
#include <arpa/inet.h>
129194
#include <linux/hyperv.h>
129194
#include <ifaddrs.h>
129194
#include <netdb.h>
129194
#include <syslog.h>
129194
#include <sys/stat.h>
129194
#include <fcntl.h>
129194
#include <dirent.h>
129194
#include <net/if.h>
129194
#include <limits.h>
129194
#include <getopt.h>
129194
129194
/*
129194
 * KVP protocol: The user mode component first registers with the
129194
 * the kernel component. Subsequently, the kernel component requests, data
129194
 * for the specified keys. In response to this message the user mode component
129194
 * fills in the value corresponding to the specified key. We overload the
129194
 * sequence field in the cn_msg header to define our KVP message types.
129194
 *
129194
 * We use this infrastructure for also supporting queries from user mode
129194
 * application for state that may be maintained in the KVP kernel component.
129194
 *
129194
 */
129194
129194
129194
enum key_index {
129194
	FullyQualifiedDomainName = 0,
129194
	IntegrationServicesVersion, /*This key is serviced in the kernel*/
129194
	NetworkAddressIPv4,
129194
	NetworkAddressIPv6,
129194
	OSBuildNumber,
129194
	OSName,
129194
	OSMajorVersion,
129194
	OSMinorVersion,
129194
	OSVersion,
129194
	ProcessorArchitecture
129194
};
129194
129194
129194
enum {
129194
	IPADDR = 0,
129194
	NETMASK,
129194
	GATEWAY,
129194
	DNS
129194
};
129194
129194
static int in_hand_shake = 1;
129194
129194
static char *os_name = "";
129194
static char *os_major = "";
129194
static char *os_minor = "";
129194
static char *processor_arch;
129194
static char *os_build;
129194
static char *os_version;
129194
static char *lic_version = "Unknown version";
129194
static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
129194
static struct utsname uts_buf;
129194
129194
/*
129194
 * The location of the interface configuration file.
129194
 */
129194
129194
#define KVP_CONFIG_LOC	"/var/lib/hyperv"
129194
129194
#ifndef KVP_SCRIPTS_PATH
129194
#define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/"
129194
#endif
129194
129194
#define KVP_NET_DIR "/sys/class/net/"
129194
129194
#define MAX_FILE_NAME 100
129194
#define ENTRIES_PER_BLOCK 50
129194
129194
struct kvp_record {
129194
	char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
129194
	char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
129194
};
129194
129194
struct kvp_file_state {
129194
	int fd;
129194
	int num_blocks;
129194
	struct kvp_record *records;
129194
	int num_records;
129194
	char fname[MAX_FILE_NAME];
129194
};
129194
129194
static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
129194
129194
static void kvp_acquire_lock(int pool)
129194
{
129194
	struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
129194
	fl.l_pid = getpid();
129194
129194
	if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
129194
		syslog(LOG_ERR, "Failed to acquire the lock pool: %d; error: %d %s", pool,
129194
				errno, strerror(errno));
129194
		exit(EXIT_FAILURE);
129194
	}
129194
}
129194
129194
static void kvp_release_lock(int pool)
129194
{
129194
	struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
129194
	fl.l_pid = getpid();
129194
129194
	if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
129194
		syslog(LOG_ERR, "Failed to release the lock pool: %d; error: %d %s", pool,
129194
				errno, strerror(errno));
129194
		exit(EXIT_FAILURE);
129194
	}
129194
}
129194
129194
static void kvp_update_file(int pool)
129194
{
129194
	FILE *filep;
129194
129194
	/*
129194
	 * We are going to write our in-memory registry out to
129194
	 * disk; acquire the lock first.
129194
	 */
129194
	kvp_acquire_lock(pool);
129194
129194
	filep = fopen(kvp_file_info[pool].fname, "we");
129194
	if (!filep) {
129194
		syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
129194
				errno, strerror(errno));
129194
		kvp_release_lock(pool);
129194
		exit(EXIT_FAILURE);
129194
	}
129194
129194
	fwrite(kvp_file_info[pool].records, sizeof(struct kvp_record),
129194
				kvp_file_info[pool].num_records, filep);
129194
129194
	if (ferror(filep) || fclose(filep)) {
129194
		kvp_release_lock(pool);
129194
		syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
129194
		exit(EXIT_FAILURE);
129194
	}
129194
129194
	kvp_release_lock(pool);
129194
}
129194
129194
static void kvp_update_mem_state(int pool)
129194
{
129194
	FILE *filep;
129194
	size_t records_read = 0;
129194
	struct kvp_record *record = kvp_file_info[pool].records;
129194
	struct kvp_record *readp;
129194
	int num_blocks = kvp_file_info[pool].num_blocks;
129194
	int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
129194
129194
	kvp_acquire_lock(pool);
129194
129194
	filep = fopen(kvp_file_info[pool].fname, "re");
129194
	if (!filep) {
129194
		syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
129194
				errno, strerror(errno));
129194
		kvp_release_lock(pool);
129194
		exit(EXIT_FAILURE);
129194
	}
129194
	for (;;) {
129194
		readp = &record[records_read];
129194
		records_read += fread(readp, sizeof(struct kvp_record),
129194
				ENTRIES_PER_BLOCK * num_blocks - records_read,
129194
				filep);
129194
129194
		if (ferror(filep)) {
129194
			syslog(LOG_ERR,
129194
				"Failed to read file, pool: %d; error: %d %s",
129194
				 pool, errno, strerror(errno));
129194
			kvp_release_lock(pool);
129194
			exit(EXIT_FAILURE);
129194
		}
129194
129194
		if (!feof(filep)) {
129194
			/*
129194
			 * We have more data to read.
129194
			 */
129194
			num_blocks++;
129194
			record = realloc(record, alloc_unit * num_blocks);
129194
129194
			if (record == NULL) {
129194
				syslog(LOG_ERR, "malloc failed");
129194
				kvp_release_lock(pool);
129194
				exit(EXIT_FAILURE);
129194
			}
129194
			continue;
129194
		}
129194
		break;
129194
	}
129194
129194
	kvp_file_info[pool].num_blocks = num_blocks;
129194
	kvp_file_info[pool].records = record;
129194
	kvp_file_info[pool].num_records = records_read;
129194
129194
	fclose(filep);
129194
	kvp_release_lock(pool);
129194
}
129194
129194
static int kvp_file_init(void)
129194
{
129194
	int  fd;
129194
	char *fname;
129194
	int i;
129194
	int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
129194
129194
	if (access(KVP_CONFIG_LOC, F_OK)) {
129194
		if (mkdir(KVP_CONFIG_LOC, 0755 /* rwxr-xr-x */)) {
129194
			syslog(LOG_ERR, "Failed to create '%s'; error: %d %s", KVP_CONFIG_LOC,
129194
					errno, strerror(errno));
129194
			exit(EXIT_FAILURE);
129194
		}
129194
	}
129194
129194
	for (i = 0; i < KVP_POOL_COUNT; i++) {
129194
		fname = kvp_file_info[i].fname;
129194
		sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
129194
		fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
129194
129194
		if (fd == -1)
129194
			return 1;
129194
129194
		kvp_file_info[i].fd = fd;
129194
		kvp_file_info[i].num_blocks = 1;
129194
		kvp_file_info[i].records = malloc(alloc_unit);
129194
		if (kvp_file_info[i].records == NULL)
129194
			return 1;
129194
		kvp_file_info[i].num_records = 0;
129194
		kvp_update_mem_state(i);
129194
	}
129194
129194
	return 0;
129194
}
129194
129194
static int kvp_key_delete(int pool, const __u8 *key, int key_size)
129194
{
129194
	int i;
129194
	int j, k;
129194
	int num_records;
129194
	struct kvp_record *record;
129194
129194
	/*
129194
	 * First update the in-memory state.
129194
	 */
129194
	kvp_update_mem_state(pool);
129194
129194
	num_records = kvp_file_info[pool].num_records;
129194
	record = kvp_file_info[pool].records;
129194
129194
	for (i = 0; i < num_records; i++) {
129194
		if (memcmp(key, record[i].key, key_size))
129194
			continue;
129194
		/*
129194
		 * Found a match; just move the remaining
129194
		 * entries up.
129194
		 */
129194
		if (i == num_records) {
129194
			kvp_file_info[pool].num_records--;
129194
			kvp_update_file(pool);
129194
			return 0;
129194
		}
129194
129194
		j = i;
129194
		k = j + 1;
129194
		for (; k < num_records; k++) {
129194
			strcpy(record[j].key, record[k].key);
129194
			strcpy(record[j].value, record[k].value);
129194
			j++;
129194
		}
129194
129194
		kvp_file_info[pool].num_records--;
129194
		kvp_update_file(pool);
129194
		return 0;
129194
	}
129194
	return 1;
129194
}
129194
129194
static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
129194
				 const __u8 *value, int value_size)
129194
{
129194
	int i;
129194
	int num_records;
129194
	struct kvp_record *record;
129194
	int num_blocks;
129194
129194
	if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
129194
		(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
129194
		return 1;
129194
129194
	/*
129194
	 * First update the in-memory state.
129194
	 */
129194
	kvp_update_mem_state(pool);
129194
129194
	num_records = kvp_file_info[pool].num_records;
129194
	record = kvp_file_info[pool].records;
129194
	num_blocks = kvp_file_info[pool].num_blocks;
129194
129194
	for (i = 0; i < num_records; i++) {
129194
		if (memcmp(key, record[i].key, key_size))
129194
			continue;
129194
		/*
129194
		 * Found a match; just update the value -
129194
		 * this is the modify case.
129194
		 */
129194
		memcpy(record[i].value, value, value_size);
129194
		kvp_update_file(pool);
129194
		return 0;
129194
	}
129194
129194
	/*
129194
	 * Need to add a new entry;
129194
	 */
129194
	if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
129194
		/* Need to allocate a larger array for reg entries. */
129194
		record = realloc(record, sizeof(struct kvp_record) *
129194
			 ENTRIES_PER_BLOCK * (num_blocks + 1));
129194
129194
		if (record == NULL)
129194
			return 1;
129194
		kvp_file_info[pool].num_blocks++;
129194
129194
	}
129194
	memcpy(record[i].value, value, value_size);
129194
	memcpy(record[i].key, key, key_size);
129194
	kvp_file_info[pool].records = record;
129194
	kvp_file_info[pool].num_records++;
129194
	kvp_update_file(pool);
129194
	return 0;
129194
}
129194
129194
static int kvp_get_value(int pool, const __u8 *key, int key_size, __u8 *value,
129194
			int value_size)
129194
{
129194
	int i;
129194
	int num_records;
129194
	struct kvp_record *record;
129194
129194
	if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
129194
		(value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
129194
		return 1;
129194
129194
	/*
129194
	 * First update the in-memory state.
129194
	 */
129194
	kvp_update_mem_state(pool);
129194
129194
	num_records = kvp_file_info[pool].num_records;
129194
	record = kvp_file_info[pool].records;
129194
129194
	for (i = 0; i < num_records; i++) {
129194
		if (memcmp(key, record[i].key, key_size))
129194
			continue;
129194
		/*
129194
		 * Found a match; just copy the value out.
129194
		 */
129194
		memcpy(value, record[i].value, value_size);
129194
		return 0;
129194
	}
129194
129194
	return 1;
129194
}
129194
129194
static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
129194
				__u8 *value, int value_size)
129194
{
129194
	struct kvp_record *record;
129194
129194
	/*
129194
	 * First update our in-memory database.
129194
	 */
129194
	kvp_update_mem_state(pool);
129194
	record = kvp_file_info[pool].records;
129194
129194
	if (index >= kvp_file_info[pool].num_records) {
129194
		return 1;
129194
	}
129194
129194
	memcpy(key, record[index].key, key_size);
129194
	memcpy(value, record[index].value, value_size);
129194
	return 0;
129194
}
129194
129194
129194
void kvp_get_os_info(void)
129194
{
129194
	FILE	*file;
129194
	char	*p, buf[512];
129194
129194
	uname(&uts_buf);
129194
	os_version = uts_buf.release;
129194
	os_build = strdup(uts_buf.release);
129194
129194
	os_name = uts_buf.sysname;
129194
	processor_arch = uts_buf.machine;
129194
129194
	/*
129194
	 * The current windows host (win7) expects the build
129194
	 * string to be of the form: x.y.z
129194
	 * Strip additional information we may have.
129194
	 */
129194
	p = strchr(os_version, '-');
129194
	if (p)
129194
		*p = '\0';
129194
129194
	/*
129194
	 * Parse the /etc/os-release file if present:
129194
	 * http://www.freedesktop.org/software/systemd/man/os-release.html
129194
	 */
129194
	file = fopen("/etc/os-release", "r");
129194
	if (file != NULL) {
129194
		while (fgets(buf, sizeof(buf), file)) {
129194
			char *value, *q;
129194
129194
			/* Ignore comments */
129194
			if (buf[0] == '#')
129194
				continue;
129194
129194
			/* Split into name=value */
129194
			p = strchr(buf, '=');
129194
			if (!p)
129194
				continue;
129194
			*p++ = 0;
129194
129194
			/* Remove quotes and newline; un-escape */
129194
			value = p;
129194
			q = p;
129194
			while (*p) {
129194
				if (*p == '\\') {
129194
					++p;
129194
					if (!*p)
129194
						break;
129194
					*q++ = *p++;
129194
				} else if (*p == '\'' || *p == '"' ||
129194
					   *p == '\n') {
129194
					++p;
129194
				} else {
129194
					*q++ = *p++;
129194
				}
129194
			}
129194
			*q = 0;
129194
129194
			if (!strcmp(buf, "NAME")) {
129194
				p = strdup(value);
129194
				if (!p)
129194
					break;
129194
				os_name = p;
129194
			} else if (!strcmp(buf, "VERSION_ID")) {
129194
				p = strdup(value);
129194
				if (!p)
129194
					break;
129194
				os_major = p;
129194
			}
129194
		}
129194
		fclose(file);
129194
		return;
129194
	}
129194
129194
	/* Fallback for older RH/SUSE releases */
129194
	file = fopen("/etc/SuSE-release", "r");
129194
	if (file != NULL)
129194
		goto kvp_osinfo_found;
129194
	file  = fopen("/etc/redhat-release", "r");
129194
	if (file != NULL)
129194
		goto kvp_osinfo_found;
129194
129194
	/*
129194
	 * We don't have information about the os.
129194
	 */
129194
	return;
129194
129194
kvp_osinfo_found:
129194
	/* up to three lines */
129194
	p = fgets(buf, sizeof(buf), file);
129194
	if (p) {
129194
		p = strchr(buf, '\n');
129194
		if (p)
129194
			*p = '\0';
129194
		p = strdup(buf);
129194
		if (!p)
129194
			goto done;
129194
		os_name = p;
129194
129194
		/* second line */
129194
		p = fgets(buf, sizeof(buf), file);
129194
		if (p) {
129194
			p = strchr(buf, '\n');
129194
			if (p)
129194
				*p = '\0';
129194
			p = strdup(buf);
129194
			if (!p)
129194
				goto done;
129194
			os_major = p;
129194
129194
			/* third line */
129194
			p = fgets(buf, sizeof(buf), file);
129194
			if (p)  {
129194
				p = strchr(buf, '\n');
129194
				if (p)
129194
					*p = '\0';
129194
				p = strdup(buf);
129194
				if (p)
129194
					os_minor = p;
129194
			}
129194
		}
129194
	}
129194
129194
done:
129194
	fclose(file);
129194
	return;
129194
}
129194
129194
129194
129194
/*
129194
 * Retrieve an interface name corresponding to the specified guid.
129194
 * If there is a match, the function returns a pointer
129194
 * to the interface name and if not, a NULL is returned.
129194
 * If a match is found, the caller is responsible for
129194
 * freeing the memory.
129194
 */
129194
129194
static char *kvp_get_if_name(char *guid)
129194
{
129194
	DIR *dir;
129194
	struct dirent *entry;
129194
	FILE    *file;
129194
	char    *p, *x;
129194
	char    *if_name = NULL;
129194
	char    buf[256];
129194
	char dev_id[PATH_MAX];
129194
129194
	dir = opendir(KVP_NET_DIR);
129194
	if (dir == NULL)
129194
		return NULL;
129194
129194
	while ((entry = readdir(dir)) != NULL) {
129194
		/*
129194
		 * Set the state for the next pass.
129194
		 */
129194
		snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id",
129194
			 KVP_NET_DIR, entry->d_name);
129194
129194
		file = fopen(dev_id, "r");
129194
		if (file == NULL)
129194
			continue;
129194
129194
		p = fgets(buf, sizeof(buf), file);
129194
		if (p) {
129194
			x = strchr(p, '\n');
129194
			if (x)
129194
				*x = '\0';
129194
129194
			if (!strcmp(p, guid)) {
129194
				/*
129194
				 * Found the guid match; return the interface
129194
				 * name. The caller will free the memory.
129194
				 */
129194
				if_name = strdup(entry->d_name);
129194
				fclose(file);
129194
				break;
129194
			}
129194
		}
129194
		fclose(file);
129194
	}
129194
129194
	closedir(dir);
129194
	return if_name;
129194
}
129194
129194
/*
129194
 * Retrieve the MAC address given the interface name.
129194
 */
129194
129194
static char *kvp_if_name_to_mac(char *if_name)
129194
{
129194
	FILE    *file;
129194
	char    *p, *x;
129194
	char    buf[256];
129194
	char addr_file[PATH_MAX];
129194
	unsigned int i;
129194
	char *mac_addr = NULL;
129194
129194
	snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR,
129194
		 if_name, "/address");
129194
129194
	file = fopen(addr_file, "r");
129194
	if (file == NULL)
129194
		return NULL;
129194
129194
	p = fgets(buf, sizeof(buf), file);
129194
	if (p) {
129194
		x = strchr(p, '\n');
129194
		if (x)
129194
			*x = '\0';
129194
		for (i = 0; i < strlen(p); i++)
129194
			p[i] = toupper(p[i]);
129194
		mac_addr = strdup(p);
129194
	}
129194
129194
	fclose(file);
129194
	return mac_addr;
129194
}
129194
129194
static void kvp_process_ipconfig_file(char *cmd,
129194
					char *config_buf, unsigned int len,
129194
					int element_size, int offset)
129194
{
129194
	char buf[256];
129194
	char *p;
129194
	char *x;
129194
	FILE *file;
129194
129194
	/*
129194
	 * First execute the command.
129194
	 */
129194
	file = popen(cmd, "r");
129194
	if (file == NULL)
129194
		return;
129194
129194
	if (offset == 0)
129194
		memset(config_buf, 0, len);
129194
	while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
129194
		if (len < strlen(config_buf) + element_size + 1)
129194
			break;
129194
129194
		x = strchr(p, '\n');
129194
		if (x)
129194
			*x = '\0';
129194
129194
		strcat(config_buf, p);
129194
		strcat(config_buf, ";");
129194
	}
129194
	pclose(file);
129194
}
129194
129194
static void kvp_get_ipconfig_info(char *if_name,
129194
				 struct hv_kvp_ipaddr_value *buffer)
129194
{
129194
	char cmd[512];
129194
	char dhcp_info[128];
129194
	char *p;
129194
	FILE *file;
129194
129194
	/*
129194
	 * Get the address of default gateway (ipv4).
129194
	 */
129194
	sprintf(cmd, "%s %s", "ip route show dev", if_name);
129194
	strcat(cmd, " | awk '/default/ {print $3 }'");
129194
129194
	/*
129194
	 * Execute the command to gather gateway info.
129194
	 */
129194
	kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
129194
				(MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
129194
129194
	/*
129194
	 * Get the address of default gateway (ipv6).
129194
	 */
129194
	sprintf(cmd, "%s %s", "ip -f inet6  route show dev", if_name);
129194
	strcat(cmd, " | awk '/default/ {print $3 }'");
129194
129194
	/*
129194
	 * Execute the command to gather gateway info (ipv6).
129194
	 */
129194
	kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
129194
				(MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
129194
129194
129194
	/*
129194
	 * Gather the DNS  state.
129194
	 * Since there is no standard way to get this information
129194
	 * across various distributions of interest; we just invoke
129194
	 * an external script that needs to be ported across distros
129194
	 * of interest.
129194
	 *
129194
	 * Following is the expected format of the information from the script:
129194
	 *
129194
	 * ipaddr1 (nameserver1)
129194
	 * ipaddr2 (nameserver2)
129194
	 * .
129194
	 * .
129194
	 */
129194
129194
	sprintf(cmd, KVP_SCRIPTS_PATH "%s",  "hv_get_dns_info");
129194
129194
	/*
129194
	 * Execute the command to gather DNS info.
129194
	 */
129194
	kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
129194
				(MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
129194
129194
	/*
129194
	 * Gather the DHCP state.
129194
	 * We will gather this state by invoking an external script.
129194
	 * The parameter to the script is the interface name.
129194
	 * Here is the expected output:
129194
	 *
129194
	 * Enabled: DHCP enabled.
129194
	 */
129194
129194
	sprintf(cmd, KVP_SCRIPTS_PATH "%s %s", "hv_get_dhcp_info", if_name);
129194
129194
	file = popen(cmd, "r");
129194
	if (file == NULL)
129194
		return;
129194
129194
	p = fgets(dhcp_info, sizeof(dhcp_info), file);
129194
	if (p == NULL) {
129194
		pclose(file);
129194
		return;
129194
	}
129194
129194
	if (!strncmp(p, "Enabled", 7))
129194
		buffer->dhcp_enabled = 1;
129194
	else
129194
		buffer->dhcp_enabled = 0;
129194
129194
	pclose(file);
129194
}
129194
129194
129194
static unsigned int hweight32(unsigned int *w)
129194
{
129194
	unsigned int res = *w - ((*w >> 1) & 0x55555555);
129194
	res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
129194
	res = (res + (res >> 4)) & 0x0F0F0F0F;
129194
	res = res + (res >> 8);
129194
	return (res + (res >> 16)) & 0x000000FF;
129194
}
129194
129194
static int kvp_process_ip_address(void *addrp,
129194
				int family, char *buffer,
129194
				int length,  int *offset)
129194
{
129194
	struct sockaddr_in *addr;
129194
	struct sockaddr_in6 *addr6;
129194
	int addr_length;
129194
	char tmp[50];
129194
	const char *str;
129194
129194
	if (family == AF_INET) {
129194
		addr = (struct sockaddr_in *)addrp;
129194
		str = inet_ntop(family, &addr->sin_addr, tmp, 50);
129194
		addr_length = INET_ADDRSTRLEN;
129194
	} else {
129194
		addr6 = (struct sockaddr_in6 *)addrp;
129194
		str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
129194
		addr_length = INET6_ADDRSTRLEN;
129194
	}
129194
129194
	if ((length - *offset) < addr_length + 2)
129194
		return HV_E_FAIL;
129194
	if (str == NULL) {
129194
		strcpy(buffer, "inet_ntop failed\n");
129194
		return HV_E_FAIL;
129194
	}
129194
	if (*offset == 0)
129194
		strcpy(buffer, tmp);
129194
	else {
129194
		strcat(buffer, ";");
129194
		strcat(buffer, tmp);
129194
	}
129194
129194
	*offset += strlen(str) + 1;
129194
129194
	return 0;
129194
}
129194
129194
static int
129194
kvp_get_ip_info(int family, char *if_name, int op,
129194
		 void  *out_buffer, unsigned int length)
129194
{
129194
	struct ifaddrs *ifap;
129194
	struct ifaddrs *curp;
129194
	int offset = 0;
129194
	int sn_offset = 0;
129194
	int error = 0;
129194
	char *buffer;
129194
	struct hv_kvp_ipaddr_value *ip_buffer;
129194
	char cidr_mask[5]; /* /xyz */
129194
	int weight;
129194
	int i;
129194
	unsigned int *w;
129194
	char *sn_str;
129194
	struct sockaddr_in6 *addr6;
129194
129194
	if (op == KVP_OP_ENUMERATE) {
129194
		buffer = out_buffer;
129194
	} else {
129194
		ip_buffer = out_buffer;
129194
		buffer = (char *)ip_buffer->ip_addr;
129194
		ip_buffer->addr_family = 0;
129194
	}
129194
	/*
129194
	 * On entry into this function, the buffer is capable of holding the
129194
	 * maximum key value.
129194
	 */
129194
129194
	if (getifaddrs(&ifap)) {
129194
		strcpy(buffer, "getifaddrs failed\n");
129194
		return HV_E_FAIL;
129194
	}
129194
129194
	curp = ifap;
129194
	while (curp != NULL) {
129194
		if (curp->ifa_addr == NULL) {
129194
			curp = curp->ifa_next;
129194
			continue;
129194
		}
129194
129194
		if ((if_name != NULL) &&
129194
			(strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
129194
			/*
129194
			 * We want info about a specific interface;
129194
			 * just continue.
129194
			 */
129194
			curp = curp->ifa_next;
129194
			continue;
129194
		}
129194
129194
		/*
129194
		 * We only support two address families: AF_INET and AF_INET6.
129194
		 * If a family value of 0 is specified, we collect both
129194
		 * supported address families; if not we gather info on
129194
		 * the specified address family.
129194
		 */
129194
		if ((((family != 0) &&
129194
			 (curp->ifa_addr->sa_family != family))) ||
129194
			 (curp->ifa_flags & IFF_LOOPBACK)) {
129194
			curp = curp->ifa_next;
129194
			continue;
129194
		}
129194
		if ((curp->ifa_addr->sa_family != AF_INET) &&
129194
			(curp->ifa_addr->sa_family != AF_INET6)) {
129194
			curp = curp->ifa_next;
129194
			continue;
129194
		}
129194
129194
		if (op == KVP_OP_GET_IP_INFO) {
129194
			/*
129194
			 * Gather info other than the IP address.
129194
			 * IP address info will be gathered later.
129194
			 */
129194
			if (curp->ifa_addr->sa_family == AF_INET) {
129194
				ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
129194
				/*
129194
				 * Get subnet info.
129194
				 */
129194
				error = kvp_process_ip_address(
129194
							     curp->ifa_netmask,
129194
							     AF_INET,
129194
							     (char *)
129194
							     ip_buffer->sub_net,
129194
							     length,
129194
							     &sn_offset);
129194
				if (error)
129194
					goto gather_ipaddr;
129194
			} else {
129194
				ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
129194
129194
				/*
129194
				 * Get subnet info in CIDR format.
129194
				 */
129194
				weight = 0;
129194
				sn_str = (char *)ip_buffer->sub_net;
129194
				addr6 = (struct sockaddr_in6 *)
129194
					curp->ifa_netmask;
129194
				w = addr6->sin6_addr.s6_addr32;
129194
129194
				for (i = 0; i < 4; i++)
129194
					weight += hweight32(&w[i]);
129194
129194
				sprintf(cidr_mask, "/%d", weight);
129194
				if (length < sn_offset + strlen(cidr_mask) + 1)
129194
					goto gather_ipaddr;
129194
129194
				if (sn_offset == 0)
129194
					strcpy(sn_str, cidr_mask);
129194
				else {
129194
					strcat((char *)ip_buffer->sub_net, ";");
129194
					strcat(sn_str, cidr_mask);
129194
				}
129194
				sn_offset += strlen(sn_str) + 1;
129194
			}
129194
129194
			/*
129194
			 * Collect other ip related configuration info.
129194
			 */
129194
129194
			kvp_get_ipconfig_info(if_name, ip_buffer);
129194
		}
129194
129194
gather_ipaddr:
129194
		error = kvp_process_ip_address(curp->ifa_addr,
129194
						curp->ifa_addr->sa_family,
129194
						buffer,
129194
						length, &offset);
129194
		if (error)
129194
			goto getaddr_done;
129194
129194
		curp = curp->ifa_next;
129194
	}
129194
129194
getaddr_done:
129194
	freeifaddrs(ifap);
129194
	return error;
129194
}
129194
129194
/*
129194
 * Retrieve the IP given the MAC address.
129194
 */
129194
static int kvp_mac_to_ip(struct hv_kvp_ipaddr_value *kvp_ip_val)
129194
{
129194
	char *mac = (char *)kvp_ip_val->adapter_id;
129194
	DIR *dir;
129194
	struct dirent *entry;
129194
	FILE    *file;
129194
	char    *p, *x;
129194
	char    *if_name = NULL;
129194
	char    buf[256];
129194
	char dev_id[PATH_MAX];
129194
	unsigned int i;
129194
	int error = HV_E_FAIL;
129194
129194
	dir = opendir(KVP_NET_DIR);
129194
	if (dir == NULL)
129194
		return HV_E_FAIL;
129194
129194
	while ((entry = readdir(dir)) != NULL) {
129194
		/*
129194
		 * Set the state for the next pass.
129194
		 */
129194
		snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR,
129194
			 entry->d_name);
129194
129194
		file = fopen(dev_id, "r");
129194
		if (file == NULL)
129194
			continue;
129194
129194
		p = fgets(buf, sizeof(buf), file);
129194
		fclose(file);
129194
		if (!p)
129194
			continue;
129194
129194
		x = strchr(p, '\n');
129194
		if (x)
129194
			*x = '\0';
129194
129194
		for (i = 0; i < strlen(p); i++)
129194
			p[i] = toupper(p[i]);
129194
129194
		if (strcmp(p, mac))
129194
			continue;
129194
129194
		/*
129194
		 * Found the MAC match.
129194
		 * A NIC (e.g. VF) matching the MAC, but without IP, is skipped.
129194
		 */
129194
		if_name = entry->d_name;
129194
		if (!if_name)
129194
			continue;
129194
129194
		error = kvp_get_ip_info(0, if_name, KVP_OP_GET_IP_INFO,
129194
					kvp_ip_val, MAX_IP_ADDR_SIZE * 2);
129194
129194
		if (!error && strlen((char *)kvp_ip_val->ip_addr))
129194
			break;
129194
	}
129194
129194
	closedir(dir);
129194
	return error;
129194
}
129194
129194
static int expand_ipv6(char *addr, int type)
129194
{
129194
	int ret;
129194
	struct in6_addr v6_addr;
129194
129194
	ret = inet_pton(AF_INET6, addr, &v6_addr);
129194
129194
	if (ret != 1) {
129194
		if (type == NETMASK)
129194
			return 1;
129194
		return 0;
129194
	}
129194
129194
	sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
129194
		"%02x%02x:%02x%02x:%02x%02x",
129194
		(int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
129194
		(int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
129194
		(int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
129194
		(int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
129194
		(int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
129194
		(int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
129194
		(int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
129194
		(int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
129194
129194
	return 1;
129194
129194
}
129194
129194
static int is_ipv4(char *addr)
129194
{
129194
	int ret;
129194
	struct in_addr ipv4_addr;
129194
129194
	ret = inet_pton(AF_INET, addr, &ipv4_addr);
129194
129194
	if (ret == 1)
129194
		return 1;
129194
	return 0;
129194
}
129194
129194
static int parse_ip_val_buffer(char *in_buf, int *offset,
129194
				char *out_buf, int out_len)
129194
{
129194
	char *x;
129194
	char *start;
129194
129194
	/*
129194
	 * in_buf has sequence of characters that are seperated by
129194
	 * the character ';'. The last sequence does not have the
129194
	 * terminating ";" character.
129194
	 */
129194
	start = in_buf + *offset;
129194
129194
	x = strchr(start, ';');
129194
	if (x)
129194
		*x = 0;
129194
	else
129194
		x = start + strlen(start);
129194
129194
	if (strlen(start) != 0) {
129194
		int i = 0;
129194
		/*
129194
		 * Get rid of leading spaces.
129194
		 */
129194
		while (start[i] == ' ')
129194
			i++;
129194
129194
		if ((x - start) <= out_len) {
129194
			strcpy(out_buf, (start + i));
129194
			*offset += (x - start) + 1;
129194
			return 1;
129194
		}
129194
	}
129194
	return 0;
129194
}
129194
129194
static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
129194
{
129194
	int ret;
129194
129194
	ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
129194
129194
	if (ret < 0)
129194
		return HV_E_FAIL;
129194
129194
	return 0;
129194
}
129194
129194
129194
static int process_ip_string(FILE *f, char *ip_string, int type)
129194
{
129194
	int error = 0;
129194
	char addr[INET6_ADDRSTRLEN];
129194
	int i = 0;
129194
	int j = 0;
129194
	char str[256];
129194
	char sub_str[13];
129194
	int offset = 0;
129194
129194
	memset(addr, 0, sizeof(addr));
129194
129194
	while (parse_ip_val_buffer(ip_string, &offset, addr,
129194
					(MAX_IP_ADDR_SIZE * 2))) {
129194
129194
		sub_str[0] = 0;
129194
		if (is_ipv4(addr)) {
129194
			switch (type) {
129194
			case IPADDR:
129194
				snprintf(str, sizeof(str), "%s", "IPADDR");
129194
				break;
129194
			case NETMASK:
129194
				snprintf(str, sizeof(str), "%s", "NETMASK");
129194
				break;
129194
			case GATEWAY:
129194
				snprintf(str, sizeof(str), "%s", "GATEWAY");
129194
				break;
129194
			case DNS:
129194
				snprintf(str, sizeof(str), "%s", "DNS");
129194
				break;
129194
			}
129194
129194
			if (type == DNS) {
129194
				snprintf(sub_str, sizeof(sub_str), "%d", ++i);
129194
			} else if (type == GATEWAY && i == 0) {
129194
				++i;
129194
			} else {
129194
				snprintf(sub_str, sizeof(sub_str), "%d", i++);
129194
			}
129194
129194
129194
		} else if (expand_ipv6(addr, type)) {
129194
			switch (type) {
129194
			case IPADDR:
129194
				snprintf(str, sizeof(str), "%s", "IPV6ADDR");
129194
				break;
129194
			case NETMASK:
129194
				snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
129194
				break;
129194
			case GATEWAY:
129194
				snprintf(str, sizeof(str), "%s",
129194
					"IPV6_DEFAULTGW");
129194
				break;
129194
			case DNS:
129194
				snprintf(str, sizeof(str), "%s",  "DNS");
129194
				break;
129194
			}
129194
129194
			if (type == DNS) {
129194
				snprintf(sub_str, sizeof(sub_str), "%d", ++i);
129194
			} else if (j == 0) {
129194
				++j;
129194
			} else {
129194
				snprintf(sub_str, sizeof(sub_str), "_%d", j++);
129194
			}
129194
		} else {
129194
			return  HV_INVALIDARG;
129194
		}
129194
129194
		error = kvp_write_file(f, str, sub_str, addr);
129194
		if (error)
129194
			return error;
129194
		memset(addr, 0, sizeof(addr));
129194
	}
129194
129194
	return 0;
129194
}
129194
129194
static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
129194
{
129194
	int error = 0;
129194
	char if_file[PATH_MAX];
129194
	FILE *file;
129194
	char cmd[PATH_MAX];
129194
	char *mac_addr;
129194
129194
	/*
129194
	 * Set the configuration for the specified interface with
129194
	 * the information provided. Since there is no standard
129194
	 * way to configure an interface, we will have an external
129194
	 * script that does the job of configuring the interface and
129194
	 * flushing the configuration.
129194
	 *
129194
	 * The parameters passed to this external script are:
129194
	 * 1. A configuration file that has the specified configuration.
129194
	 *
129194
	 * We will embed the name of the interface in the configuration
129194
	 * file: ifcfg-ethx (where ethx is the interface name).
129194
	 *
129194
	 * The information provided here may be more than what is needed
129194
	 * in a given distro to configure the interface and so are free
129194
	 * ignore information that may not be relevant.
129194
	 *
129194
	 * Here is the format of the ip configuration file:
129194
	 *
129194
	 * HWADDR=macaddr
129194
	 * DEVICE=interface name
129194
	 * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
129194
	 *                       or "none" if no boot-time protocol should be used)
129194
	 *
129194
	 * IPADDR0=ipaddr1
129194
	 * IPADDR1=ipaddr2
129194
	 * IPADDRx=ipaddry (where y = x + 1)
129194
	 *
129194
	 * NETMASK0=netmask1
129194
	 * NETMASKx=netmasky (where y = x + 1)
129194
	 *
129194
	 * GATEWAY=ipaddr1
129194
	 * GATEWAYx=ipaddry (where y = x + 1)
129194
	 *
129194
	 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
129194
	 *
129194
	 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
129194
	 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
129194
	 * IPV6NETMASK.
129194
	 *
129194
	 * The host can specify multiple ipv4 and ipv6 addresses to be
129194
	 * configured for the interface. Furthermore, the configuration
129194
	 * needs to be persistent. A subsequent GET call on the interface
129194
	 * is expected to return the configuration that is set via the SET
129194
	 * call.
129194
	 */
129194
129194
	snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
129194
		"/ifcfg-", if_name);
129194
129194
	file = fopen(if_file, "w");
129194
129194
	if (file == NULL) {
129194
		syslog(LOG_ERR, "Failed to open config file; error: %d %s",
129194
				errno, strerror(errno));
129194
		return HV_E_FAIL;
129194
	}
129194
129194
	/*
129194
	 * First write out the MAC address.
129194
	 */
129194
129194
	mac_addr = kvp_if_name_to_mac(if_name);
129194
	if (mac_addr == NULL) {
129194
		error = HV_E_FAIL;
129194
		goto setval_error;
129194
	}
129194
129194
	error = kvp_write_file(file, "HWADDR", "", mac_addr);
129194
	free(mac_addr);
129194
	if (error)
129194
		goto setval_error;
129194
129194
	error = kvp_write_file(file, "DEVICE", "", if_name);
129194
	if (error)
129194
		goto setval_error;
129194
129194
	/*
129194
	 * The dhcp_enabled flag is only for IPv4. In the case the host only
129194
	 * injects an IPv6 address, the flag is true, but we still need to
129194
	 * proceed to parse and pass the IPv6 information to the
129194
	 * disto-specific script hv_set_ifconfig.
129194
	 */
129194
	if (new_val->dhcp_enabled) {
129194
		error = kvp_write_file(file, "BOOTPROTO", "", "dhcp");
129194
		if (error)
129194
			goto setval_error;
129194
129194
	} else {
129194
		error = kvp_write_file(file, "BOOTPROTO", "", "none");
129194
		if (error)
129194
			goto setval_error;
129194
	}
129194
129194
	/*
129194
	 * Write the configuration for ipaddress, netmask, gateway and
129194
	 * name servers.
129194
	 */
129194
129194
	error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
129194
	if (error)
129194
		goto setval_error;
129194
129194
	error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
129194
	if (error)
129194
		goto setval_error;
129194
129194
	error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
129194
	if (error)
129194
		goto setval_error;
129194
129194
	error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
129194
	if (error)
129194
		goto setval_error;
129194
129194
	fclose(file);
129194
129194
	/*
129194
	 * Now that we have populated the configuration file,
129194
	 * invoke the external script to do its magic.
129194
	 */
129194
129194
	snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s",
129194
		 "hv_set_ifconfig", if_file);
129194
	if (system(cmd)) {
129194
		syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
129194
				cmd, errno, strerror(errno));
129194
		return HV_E_FAIL;
129194
	}
129194
	return 0;
129194
129194
setval_error:
129194
	syslog(LOG_ERR, "Failed to write config file");
129194
	fclose(file);
129194
	return error;
129194
}
129194
129194
129194
static void
129194
kvp_get_domain_name(char *buffer, int length)
129194
{
129194
	struct addrinfo	hints, *info ;
129194
	int error = 0;
129194
129194
	gethostname(buffer, length);
129194
	memset(&hints, 0, sizeof(hints));
129194
	hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
129194
	hints.ai_socktype = SOCK_STREAM;
129194
	hints.ai_flags = AI_CANONNAME;
129194
129194
	error = getaddrinfo(buffer, NULL, &hints, &info;;
129194
	if (error != 0) {
129194
		snprintf(buffer, length, "getaddrinfo failed: 0x%x %s",
129194
			error, gai_strerror(error));
129194
		return;
129194
	}
129194
	snprintf(buffer, length, "%s", info->ai_canonname);
129194
	freeaddrinfo(info);
129194
}
129194
129194
void print_usage(char *argv[])
129194
{
129194
	fprintf(stderr, "Usage: %s [options]\n"
129194
		"Options are:\n"
129194
		"  -n, --no-daemon        stay in foreground, don't daemonize\n"
129194
		"  -h, --help             print this help\n", argv[0]);
129194
}
129194
129194
int main(int argc, char *argv[])
129194
{
129194
	int kvp_fd, len;
129194
	int error;
129194
	struct pollfd pfd;
129194
	char    *p;
129194
	struct hv_kvp_msg hv_msg[1];
129194
	char	*key_value;
129194
	char	*key_name;
129194
	int	op;
129194
	int	pool;
129194
	char	*if_name;
129194
	struct hv_kvp_ipaddr_value *kvp_ip_val;
129194
	int daemonize = 1, long_index = 0, opt;
129194
129194
	static struct option long_options[] = {
129194
		{"help",	no_argument,	   0,  'h' },
129194
		{"no-daemon",	no_argument,	   0,  'n' },
129194
		{0,		0,		   0,  0   }
129194
	};
129194
129194
	while ((opt = getopt_long(argc, argv, "hn", long_options,
129194
				  &long_index)) != -1) {
129194
		switch (opt) {
129194
		case 'n':
129194
			daemonize = 0;
129194
			break;
129194
		case 'h':
129194
		default:
129194
			print_usage(argv);
129194
			exit(EXIT_FAILURE);
129194
		}
129194
	}
129194
129194
	if (daemonize && daemon(1, 0))
129194
		return 1;
129194
129194
	openlog("KVP", 0, LOG_USER);
129194
	syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
129194
129194
	kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
129194
129194
	if (kvp_fd < 0) {
129194
		syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
129194
			errno, strerror(errno));
129194
		exit(EXIT_FAILURE);
129194
	}
129194
129194
	/*
129194
	 * Retrieve OS release information.
129194
	 */
129194
	kvp_get_os_info();
129194
	/*
129194
	 * Cache Fully Qualified Domain Name because getaddrinfo takes an
129194
	 * unpredictable amount of time to finish.
129194
	 */
129194
	kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
129194
129194
	if (kvp_file_init()) {
129194
		syslog(LOG_ERR, "Failed to initialize the pools");
129194
		exit(EXIT_FAILURE);
129194
	}
129194
129194
	/*
129194
	 * Register ourselves with the kernel.
129194
	 */
129194
	hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
129194
	len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
129194
	if (len != sizeof(struct hv_kvp_msg)) {
129194
		syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
129194
		       errno, strerror(errno));
129194
		close(kvp_fd);
129194
		exit(EXIT_FAILURE);
129194
	}
129194
129194
	pfd.fd = kvp_fd;
129194
129194
	while (1) {
129194
		pfd.events = POLLIN;
129194
		pfd.revents = 0;
129194
129194
		if (poll(&pfd, 1, -1) < 0) {
129194
			syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
129194
			if (errno == EINVAL) {
129194
				close(kvp_fd);
129194
				exit(EXIT_FAILURE);
129194
			}
129194
			else
129194
				continue;
129194
		}
129194
129194
		len = read(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
129194
129194
		if (len != sizeof(struct hv_kvp_msg)) {
129194
			syslog(LOG_ERR, "read failed; error:%d %s",
129194
			       errno, strerror(errno));
129194
129194
			close(kvp_fd);
129194
			return EXIT_FAILURE;
129194
		}
129194
129194
		/*
129194
		 * We will use the KVP header information to pass back
129194
		 * the error from this daemon. So, first copy the state
129194
		 * and set the error code to success.
129194
		 */
129194
		op = hv_msg->kvp_hdr.operation;
129194
		pool = hv_msg->kvp_hdr.pool;
129194
		hv_msg->error = HV_S_OK;
129194
129194
		if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
129194
			/*
129194
			 * Driver is registering with us; stash away the version
129194
			 * information.
129194
			 */
129194
			in_hand_shake = 0;
129194
			p = (char *)hv_msg->body.kvp_register.version;
129194
			lic_version = malloc(strlen(p) + 1);
129194
			if (lic_version) {
129194
				strcpy(lic_version, p);
129194
				syslog(LOG_INFO, "KVP LIC Version: %s",
129194
				       lic_version);
129194
			} else {
129194
				syslog(LOG_ERR, "malloc failed");
129194
			}
129194
			continue;
129194
		}
129194
129194
		switch (op) {
129194
		case KVP_OP_GET_IP_INFO:
129194
			kvp_ip_val = &hv_msg->body.kvp_ip_val;
129194
129194
			error =  kvp_mac_to_ip(kvp_ip_val);
129194
129194
			if (error)
129194
				hv_msg->error = error;
129194
129194
			break;
129194
129194
		case KVP_OP_SET_IP_INFO:
129194
			kvp_ip_val = &hv_msg->body.kvp_ip_val;
129194
			if_name = kvp_get_if_name(
129194
					(char *)kvp_ip_val->adapter_id);
129194
			if (if_name == NULL) {
129194
				/*
129194
				 * We could not map the guid to an
129194
				 * interface name; return error.
129194
				 */
129194
				hv_msg->error = HV_GUID_NOTFOUND;
129194
				break;
129194
			}
129194
			error = kvp_set_ip_info(if_name, kvp_ip_val);
129194
			if (error)
129194
				hv_msg->error = error;
129194
129194
			free(if_name);
129194
			break;
129194
129194
		case KVP_OP_SET:
129194
			if (kvp_key_add_or_modify(pool,
129194
					hv_msg->body.kvp_set.data.key,
129194
					hv_msg->body.kvp_set.data.key_size,
129194
					hv_msg->body.kvp_set.data.value,
129194
					hv_msg->body.kvp_set.data.value_size))
129194
					hv_msg->error = HV_S_CONT;
129194
			break;
129194
129194
		case KVP_OP_GET:
129194
			if (kvp_get_value(pool,
129194
					hv_msg->body.kvp_set.data.key,
129194
					hv_msg->body.kvp_set.data.key_size,
129194
					hv_msg->body.kvp_set.data.value,
129194
					hv_msg->body.kvp_set.data.value_size))
129194
					hv_msg->error = HV_S_CONT;
129194
			break;
129194
129194
		case KVP_OP_DELETE:
129194
			if (kvp_key_delete(pool,
129194
					hv_msg->body.kvp_delete.key,
129194
					hv_msg->body.kvp_delete.key_size))
129194
					hv_msg->error = HV_S_CONT;
129194
			break;
129194
129194
		default:
129194
			break;
129194
		}
129194
129194
		if (op != KVP_OP_ENUMERATE)
129194
			goto kvp_done;
129194
129194
		/*
129194
		 * If the pool is KVP_POOL_AUTO, dynamically generate
129194
		 * both the key and the value; if not read from the
129194
		 * appropriate pool.
129194
		 */
129194
		if (pool != KVP_POOL_AUTO) {
129194
			if (kvp_pool_enumerate(pool,
129194
					hv_msg->body.kvp_enum_data.index,
129194
					hv_msg->body.kvp_enum_data.data.key,
129194
					HV_KVP_EXCHANGE_MAX_KEY_SIZE,
129194
					hv_msg->body.kvp_enum_data.data.value,
129194
					HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
129194
					hv_msg->error = HV_S_CONT;
129194
			goto kvp_done;
129194
		}
129194
129194
		key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
129194
		key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
129194
129194
		switch (hv_msg->body.kvp_enum_data.index) {
129194
		case FullyQualifiedDomainName:
129194
			strcpy(key_value, full_domain_name);
129194
			strcpy(key_name, "FullyQualifiedDomainName");
129194
			break;
129194
		case IntegrationServicesVersion:
129194
			strcpy(key_name, "IntegrationServicesVersion");
129194
			strcpy(key_value, lic_version);
129194
			break;
129194
		case NetworkAddressIPv4:
129194
			kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
129194
				key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
129194
			strcpy(key_name, "NetworkAddressIPv4");
129194
			break;
129194
		case NetworkAddressIPv6:
129194
			kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
129194
				key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
129194
			strcpy(key_name, "NetworkAddressIPv6");
129194
			break;
129194
		case OSBuildNumber:
129194
			strcpy(key_value, os_build);
129194
			strcpy(key_name, "OSBuildNumber");
129194
			break;
129194
		case OSName:
129194
			strcpy(key_value, os_name);
129194
			strcpy(key_name, "OSName");
129194
			break;
129194
		case OSMajorVersion:
129194
			strcpy(key_value, os_major);
129194
			strcpy(key_name, "OSMajorVersion");
129194
			break;
129194
		case OSMinorVersion:
129194
			strcpy(key_value, os_minor);
129194
			strcpy(key_name, "OSMinorVersion");
129194
			break;
129194
		case OSVersion:
129194
			strcpy(key_value, os_version);
129194
			strcpy(key_name, "OSVersion");
129194
			break;
129194
		case ProcessorArchitecture:
129194
			strcpy(key_value, processor_arch);
129194
			strcpy(key_name, "ProcessorArchitecture");
129194
			break;
129194
		default:
129194
			hv_msg->error = HV_S_CONT;
129194
			break;
129194
		}
129194
129194
		/* Send the value back to the kernel. */
129194
kvp_done:
129194
		len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
129194
		if (len != sizeof(struct hv_kvp_msg)) {
129194
			syslog(LOG_ERR, "write failed; error: %d %s", errno,
129194
			       strerror(errno));
129194
			exit(EXIT_FAILURE);
129194
		}
129194
	}
129194
129194
	close(kvp_fd);
129194
	exit(0);
129194
}