Blame SOURCES/hv_kvp_daemon.c

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