Blame SOURCES/hv_kvp_daemon.c

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