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