Blame SOURCES/hv_kvp_daemon.c

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