2 * An implementation of key value pair (KVP) functionality for Linux.
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <sys/utsname.h>
29 #include <linux/types.h>
36 #include <arpa/inet.h>
37 #include <linux/connector.h>
38 #include <linux/hyperv.h>
39 #include <linux/netlink.h>
48 * KVP protocol: The user mode component first registers with the
49 * the kernel component. Subsequently, the kernel component requests, data
50 * for the specified keys. In response to this message the user mode component
51 * fills in the value corresponding to the specified key. We overload the
52 * sequence field in the cn_msg header to define our KVP message types.
54 * We use this infrastructure for also supporting queries from user mode
55 * application for state that may be maintained in the KVP kernel component.
61 FullyQualifiedDomainName = 0,
62 IntegrationServicesVersion, /*This key is serviced in the kernel*/
81 static char kvp_send_buffer[4096];
82 static char kvp_recv_buffer[4096 * 2];
83 static struct sockaddr_nl addr;
84 static int in_hand_shake = 1;
86 static char *os_name = "";
87 static char *os_major = "";
88 static char *os_minor = "";
89 static char *processor_arch;
90 static char *os_build;
91 static char *os_version;
92 static char *lic_version = "Unknown version";
93 static struct utsname uts_buf;
96 * The location of the interface configuration file.
99 #define KVP_CONFIG_LOC "/var/opt/"
101 #define MAX_FILE_NAME 100
102 #define ENTRIES_PER_BLOCK 50
105 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
106 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
109 struct kvp_file_state {
112 struct kvp_record *records;
114 char fname[MAX_FILE_NAME];
117 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
119 static void kvp_acquire_lock(int pool)
121 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
124 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
125 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
130 static void kvp_release_lock(int pool)
132 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
135 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
137 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
142 static void kvp_update_file(int pool)
145 size_t bytes_written;
148 * We are going to write our in-memory registry out to
149 * disk; acquire the lock first.
151 kvp_acquire_lock(pool);
153 filep = fopen(kvp_file_info[pool].fname, "w");
155 kvp_release_lock(pool);
156 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
160 bytes_written = fwrite(kvp_file_info[pool].records,
161 sizeof(struct kvp_record),
162 kvp_file_info[pool].num_records, filep);
164 if (ferror(filep) || fclose(filep)) {
165 kvp_release_lock(pool);
166 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
170 kvp_release_lock(pool);
173 static void kvp_update_mem_state(int pool)
176 size_t records_read = 0;
177 struct kvp_record *record = kvp_file_info[pool].records;
178 struct kvp_record *readp;
179 int num_blocks = kvp_file_info[pool].num_blocks;
180 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
182 kvp_acquire_lock(pool);
184 filep = fopen(kvp_file_info[pool].fname, "r");
186 kvp_release_lock(pool);
187 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
191 readp = &record[records_read];
192 records_read += fread(readp, sizeof(struct kvp_record),
193 ENTRIES_PER_BLOCK * num_blocks,
197 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
203 * We have more data to read.
206 record = realloc(record, alloc_unit * num_blocks);
208 if (record == NULL) {
209 syslog(LOG_ERR, "malloc failed");
217 kvp_file_info[pool].num_blocks = num_blocks;
218 kvp_file_info[pool].records = record;
219 kvp_file_info[pool].num_records = records_read;
222 kvp_release_lock(pool);
224 static int kvp_file_init(void)
230 struct kvp_record *record;
231 struct kvp_record *readp;
234 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
236 if (access("/var/opt/hyperv", F_OK)) {
237 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
238 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
243 for (i = 0; i < KVP_POOL_COUNT; i++) {
244 fname = kvp_file_info[i].fname;
247 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
248 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
254 filep = fopen(fname, "r");
258 record = malloc(alloc_unit * num_blocks);
259 if (record == NULL) {
264 readp = &record[records_read];
265 records_read += fread(readp, sizeof(struct kvp_record),
270 syslog(LOG_ERR, "Failed to read file, pool: %d",
277 * We have more data to read.
280 record = realloc(record, alloc_unit *
282 if (record == NULL) {
290 kvp_file_info[i].fd = fd;
291 kvp_file_info[i].num_blocks = num_blocks;
292 kvp_file_info[i].records = record;
293 kvp_file_info[i].num_records = records_read;
301 static int kvp_key_delete(int pool, __u8 *key, int key_size)
306 struct kvp_record *record;
309 * First update the in-memory state.
311 kvp_update_mem_state(pool);
313 num_records = kvp_file_info[pool].num_records;
314 record = kvp_file_info[pool].records;
316 for (i = 0; i < num_records; i++) {
317 if (memcmp(key, record[i].key, key_size))
320 * Found a match; just move the remaining
323 if (i == num_records) {
324 kvp_file_info[pool].num_records--;
325 kvp_update_file(pool);
331 for (; k < num_records; k++) {
332 strcpy(record[j].key, record[k].key);
333 strcpy(record[j].value, record[k].value);
337 kvp_file_info[pool].num_records--;
338 kvp_update_file(pool);
344 static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
349 struct kvp_record *record;
352 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
353 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
357 * First update the in-memory state.
359 kvp_update_mem_state(pool);
361 num_records = kvp_file_info[pool].num_records;
362 record = kvp_file_info[pool].records;
363 num_blocks = kvp_file_info[pool].num_blocks;
365 for (i = 0; i < num_records; i++) {
366 if (memcmp(key, record[i].key, key_size))
369 * Found a match; just update the value -
370 * this is the modify case.
372 memcpy(record[i].value, value, value_size);
373 kvp_update_file(pool);
378 * Need to add a new entry;
380 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
381 /* Need to allocate a larger array for reg entries. */
382 record = realloc(record, sizeof(struct kvp_record) *
383 ENTRIES_PER_BLOCK * (num_blocks + 1));
387 kvp_file_info[pool].num_blocks++;
390 memcpy(record[i].value, value, value_size);
391 memcpy(record[i].key, key, key_size);
392 kvp_file_info[pool].records = record;
393 kvp_file_info[pool].num_records++;
394 kvp_update_file(pool);
398 static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
403 struct kvp_record *record;
405 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
406 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
410 * First update the in-memory state.
412 kvp_update_mem_state(pool);
414 num_records = kvp_file_info[pool].num_records;
415 record = kvp_file_info[pool].records;
417 for (i = 0; i < num_records; i++) {
418 if (memcmp(key, record[i].key, key_size))
421 * Found a match; just copy the value out.
423 memcpy(value, record[i].value, value_size);
430 static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
431 __u8 *value, int value_size)
433 struct kvp_record *record;
436 * First update our in-memory database.
438 kvp_update_mem_state(pool);
439 record = kvp_file_info[pool].records;
441 if (index >= kvp_file_info[pool].num_records) {
445 memcpy(key, record[index].key, key_size);
446 memcpy(value, record[index].value, value_size);
451 void kvp_get_os_info(void)
457 os_version = uts_buf.release;
458 os_build = strdup(uts_buf.release);
460 os_name = uts_buf.sysname;
461 processor_arch = uts_buf.machine;
464 * The current windows host (win7) expects the build
465 * string to be of the form: x.y.z
466 * Strip additional information we may have.
468 p = strchr(os_version, '-');
473 * Parse the /etc/os-release file if present:
474 * http://www.freedesktop.org/software/systemd/man/os-release.html
476 file = fopen("/etc/os-release", "r");
478 while (fgets(buf, sizeof(buf), file)) {
481 /* Ignore comments */
485 /* Split into name=value */
486 p = strchr(buf, '=');
491 /* Remove quotes and newline; un-escape */
500 } else if (*p == '\'' || *p == '"' ||
509 if (!strcmp(buf, "NAME")) {
514 } else if (!strcmp(buf, "VERSION_ID")) {
525 /* Fallback for older RH/SUSE releases */
526 file = fopen("/etc/SuSE-release", "r");
528 goto kvp_osinfo_found;
529 file = fopen("/etc/redhat-release", "r");
531 goto kvp_osinfo_found;
534 * We don't have information about the os.
539 /* up to three lines */
540 p = fgets(buf, sizeof(buf), file);
542 p = strchr(buf, '\n');
551 p = fgets(buf, sizeof(buf), file);
553 p = strchr(buf, '\n');
562 p = fgets(buf, sizeof(buf), file);
564 p = strchr(buf, '\n');
582 * Retrieve an interface name corresponding to the specified guid.
583 * If there is a match, the function returns a pointer
584 * to the interface name and if not, a NULL is returned.
585 * If a match is found, the caller is responsible for
586 * freeing the memory.
589 static char *kvp_get_if_name(char *guid)
592 struct dirent *entry;
595 char *if_name = NULL;
597 char *kvp_net_dir = "/sys/class/net/";
600 dir = opendir(kvp_net_dir);
604 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
605 q = dev_id + strlen(kvp_net_dir);
607 while ((entry = readdir(dir)) != NULL) {
609 * Set the state for the next pass.
612 strcat(dev_id, entry->d_name);
613 strcat(dev_id, "/device/device_id");
615 file = fopen(dev_id, "r");
619 p = fgets(buf, sizeof(buf), file);
625 if (!strcmp(p, guid)) {
627 * Found the guid match; return the interface
628 * name. The caller will free the memory.
630 if_name = strdup(entry->d_name);
643 * Retrieve the MAC address given the interface name.
646 static char *kvp_if_name_to_mac(char *if_name)
653 char *mac_addr = NULL;
655 snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
656 if_name, "/address");
658 file = fopen(addr_file, "r");
662 p = fgets(buf, sizeof(buf), file);
667 for (i = 0; i < strlen(p); i++)
668 p[i] = toupper(p[i]);
669 mac_addr = strdup(p);
678 * Retrieve the interface name given tha MAC address.
681 static char *kvp_mac_to_if_name(char *mac)
684 struct dirent *entry;
687 char *if_name = NULL;
689 char *kvp_net_dir = "/sys/class/net/";
693 dir = opendir(kvp_net_dir);
697 snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
698 q = dev_id + strlen(kvp_net_dir);
700 while ((entry = readdir(dir)) != NULL) {
702 * Set the state for the next pass.
706 strcat(dev_id, entry->d_name);
707 strcat(dev_id, "/address");
709 file = fopen(dev_id, "r");
713 p = fgets(buf, sizeof(buf), file);
719 for (i = 0; i < strlen(p); i++)
720 p[i] = toupper(p[i]);
722 if (!strcmp(p, mac)) {
724 * Found the MAC match; return the interface
725 * name. The caller will free the memory.
727 if_name = strdup(entry->d_name);
740 static void kvp_process_ipconfig_file(char *cmd,
741 char *config_buf, int len,
742 int element_size, int offset)
750 * First execute the command.
752 file = popen(cmd, "r");
757 memset(config_buf, 0, len);
758 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
759 if ((len - strlen(config_buf)) < (element_size + 1))
764 strcat(config_buf, p);
765 strcat(config_buf, ";");
770 static void kvp_get_ipconfig_info(char *if_name,
771 struct hv_kvp_ipaddr_value *buffer)
779 * Get the address of default gateway (ipv4).
781 sprintf(cmd, "%s %s", "ip route show dev", if_name);
782 strcat(cmd, " | awk '/default/ {print $3 }'");
785 * Execute the command to gather gateway info.
787 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
788 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
791 * Get the address of default gateway (ipv6).
793 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
794 strcat(cmd, " | awk '/default/ {print $3 }'");
797 * Execute the command to gather gateway info (ipv6).
799 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
800 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
804 * Gather the DNS state.
805 * Since there is no standard way to get this information
806 * across various distributions of interest; we just invoke
807 * an external script that needs to be ported across distros
810 * Following is the expected format of the information from the script:
812 * ipaddr1 (nameserver1)
813 * ipaddr2 (nameserver2)
818 sprintf(cmd, "%s", "hv_get_dns_info");
821 * Execute the command to gather DNS info.
823 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
824 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
827 * Gather the DHCP state.
828 * We will gather this state by invoking an external script.
829 * The parameter to the script is the interface name.
830 * Here is the expected output:
832 * Enabled: DHCP enabled.
835 sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
837 file = popen(cmd, "r");
841 p = fgets(dhcp_info, sizeof(dhcp_info), file);
847 if (!strncmp(p, "Enabled", 7))
848 buffer->dhcp_enabled = 1;
850 buffer->dhcp_enabled = 0;
856 static unsigned int hweight32(unsigned int *w)
858 unsigned int res = *w - ((*w >> 1) & 0x55555555);
859 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
860 res = (res + (res >> 4)) & 0x0F0F0F0F;
861 res = res + (res >> 8);
862 return (res + (res >> 16)) & 0x000000FF;
865 static int kvp_process_ip_address(void *addrp,
866 int family, char *buffer,
867 int length, int *offset)
869 struct sockaddr_in *addr;
870 struct sockaddr_in6 *addr6;
875 if (family == AF_INET) {
876 addr = (struct sockaddr_in *)addrp;
877 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
878 addr_length = INET_ADDRSTRLEN;
880 addr6 = (struct sockaddr_in6 *)addrp;
881 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
882 addr_length = INET6_ADDRSTRLEN;
885 if ((length - *offset) < addr_length + 1)
888 strcpy(buffer, "inet_ntop failed\n");
897 *offset += strlen(str) + 1;
902 kvp_get_ip_info(int family, char *if_name, int op,
903 void *out_buffer, int length)
905 struct ifaddrs *ifap;
906 struct ifaddrs *curp;
911 struct hv_kvp_ipaddr_value *ip_buffer;
912 char cidr_mask[5]; /* /xyz */
917 struct sockaddr_in6 *addr6;
919 if (op == KVP_OP_ENUMERATE) {
922 ip_buffer = out_buffer;
923 buffer = (char *)ip_buffer->ip_addr;
924 ip_buffer->addr_family = 0;
927 * On entry into this function, the buffer is capable of holding the
931 if (getifaddrs(&ifap)) {
932 strcpy(buffer, "getifaddrs failed\n");
937 while (curp != NULL) {
938 if (curp->ifa_addr == NULL) {
939 curp = curp->ifa_next;
943 if ((if_name != NULL) &&
944 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
946 * We want info about a specific interface;
949 curp = curp->ifa_next;
954 * We only support two address families: AF_INET and AF_INET6.
955 * If a family value of 0 is specified, we collect both
956 * supported address families; if not we gather info on
957 * the specified address family.
959 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
960 curp = curp->ifa_next;
963 if ((curp->ifa_addr->sa_family != AF_INET) &&
964 (curp->ifa_addr->sa_family != AF_INET6)) {
965 curp = curp->ifa_next;
969 if (op == KVP_OP_GET_IP_INFO) {
971 * Gather info other than the IP address.
972 * IP address info will be gathered later.
974 if (curp->ifa_addr->sa_family == AF_INET) {
975 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
979 error = kvp_process_ip_address(
989 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
992 * Get subnet info in CIDR format.
995 sn_str = (char *)ip_buffer->sub_net;
996 addr6 = (struct sockaddr_in6 *)
998 w = addr6->sin6_addr.s6_addr32;
1000 for (i = 0; i < 4; i++)
1001 weight += hweight32(&w[i]);
1003 sprintf(cidr_mask, "/%d", weight);
1004 if ((length - sn_offset) <
1005 (strlen(cidr_mask) + 1))
1009 strcpy(sn_str, cidr_mask);
1011 strcat(sn_str, cidr_mask);
1012 strcat((char *)ip_buffer->sub_net, ";");
1013 sn_offset += strlen(sn_str) + 1;
1017 * Collect other ip related configuration info.
1020 kvp_get_ipconfig_info(if_name, ip_buffer);
1024 error = kvp_process_ip_address(curp->ifa_addr,
1025 curp->ifa_addr->sa_family,
1031 curp = curp->ifa_next;
1040 static int expand_ipv6(char *addr, int type)
1043 struct in6_addr v6_addr;
1045 ret = inet_pton(AF_INET6, addr, &v6_addr);
1048 if (type == NETMASK)
1053 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1054 "%02x%02x:%02x%02x:%02x%02x",
1055 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
1056 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
1057 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
1058 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
1059 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
1060 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
1061 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
1062 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
1068 static int is_ipv4(char *addr)
1071 struct in_addr ipv4_addr;
1073 ret = inet_pton(AF_INET, addr, &ipv4_addr);
1080 static int parse_ip_val_buffer(char *in_buf, int *offset,
1081 char *out_buf, int out_len)
1087 * in_buf has sequence of characters that are seperated by
1088 * the character ';'. The last sequence does not have the
1089 * terminating ";" character.
1091 start = in_buf + *offset;
1093 x = strchr(start, ';');
1097 x = start + strlen(start);
1099 if (strlen(start) != 0) {
1102 * Get rid of leading spaces.
1104 while (start[i] == ' ')
1107 if ((x - start) <= out_len) {
1108 strcpy(out_buf, (start + i));
1109 *offset += (x - start) + 1;
1116 static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
1120 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
1129 static int process_ip_string(FILE *f, char *ip_string, int type)
1132 char addr[INET6_ADDRSTRLEN];
1139 memset(addr, 0, sizeof(addr));
1141 while (parse_ip_val_buffer(ip_string, &offset, addr,
1142 (MAX_IP_ADDR_SIZE * 2))) {
1145 if (is_ipv4(addr)) {
1148 snprintf(str, sizeof(str), "%s", "IPADDR");
1151 snprintf(str, sizeof(str), "%s", "NETMASK");
1154 snprintf(str, sizeof(str), "%s", "GATEWAY");
1157 snprintf(str, sizeof(str), "%s", "DNS");
1162 snprintf(sub_str, sizeof(sub_str),
1165 snprintf(sub_str, sizeof(sub_str),
1168 } else if (type == DNS) {
1169 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1173 } else if (expand_ipv6(addr, type)) {
1176 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1179 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1182 snprintf(str, sizeof(str), "%s",
1186 snprintf(str, sizeof(str), "%s", "DNS");
1189 if ((j != 0) || (type == DNS)) {
1191 snprintf(sub_str, sizeof(sub_str),
1194 snprintf(sub_str, sizeof(sub_str),
1197 } else if (type == DNS) {
1198 snprintf(sub_str, sizeof(sub_str),
1202 return HV_INVALIDARG;
1205 error = kvp_write_file(f, str, sub_str, addr);
1208 memset(addr, 0, sizeof(addr));
1214 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1223 * Set the configuration for the specified interface with
1224 * the information provided. Since there is no standard
1225 * way to configure an interface, we will have an external
1226 * script that does the job of configuring the interface and
1227 * flushing the configuration.
1229 * The parameters passed to this external script are:
1230 * 1. A configuration file that has the specified configuration.
1232 * We will embed the name of the interface in the configuration
1233 * file: ifcfg-ethx (where ethx is the interface name).
1235 * The information provided here may be more than what is needed
1236 * in a given distro to configure the interface and so are free
1237 * ignore information that may not be relevant.
1239 * Here is the format of the ip configuration file:
1242 * IF_NAME=interface name
1243 * DHCP=yes (This is optional; if yes, DHCP is configured)
1247 * IPADDR_x=ipaddry (where y = x + 1)
1250 * NETMASK_x=netmasky (where y = x + 1)
1253 * GATEWAY_x=ipaddry (where y = x + 1)
1255 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1257 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1258 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1261 * The host can specify multiple ipv4 and ipv6 addresses to be
1262 * configured for the interface. Furthermore, the configuration
1263 * needs to be persistent. A subsequent GET call on the interface
1264 * is expected to return the configuration that is set via the SET
1268 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1269 "hyperv/ifcfg-", if_name);
1271 file = fopen(if_file, "w");
1274 syslog(LOG_ERR, "Failed to open config file");
1279 * First write out the MAC address.
1282 mac_addr = kvp_if_name_to_mac(if_name);
1283 if (mac_addr == NULL) {
1288 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1292 error = kvp_write_file(file, "IF_NAME", "", if_name);
1296 if (new_val->dhcp_enabled) {
1297 error = kvp_write_file(file, "DHCP", "", "yes");
1308 * Write the configuration for ipaddress, netmask, gateway and
1312 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1316 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1320 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1324 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1333 * Now that we have populated the configuration file,
1334 * invoke the external script to do its magic.
1337 snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1342 syslog(LOG_ERR, "Failed to write config file");
1350 kvp_get_domain_name(char *buffer, int length)
1352 struct addrinfo hints, *info ;
1355 gethostname(buffer, length);
1356 memset(&hints, 0, sizeof(hints));
1357 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1358 hints.ai_socktype = SOCK_STREAM;
1359 hints.ai_flags = AI_CANONNAME;
1361 error = getaddrinfo(buffer, NULL, &hints, &info);
1363 strcpy(buffer, "getaddrinfo failed\n");
1366 strcpy(buffer, info->ai_canonname);
1372 netlink_send(int fd, struct cn_msg *msg)
1374 struct nlmsghdr *nlh;
1376 struct msghdr message;
1378 struct iovec iov[2];
1380 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
1382 nlh = (struct nlmsghdr *)buffer;
1384 nlh->nlmsg_pid = getpid();
1385 nlh->nlmsg_type = NLMSG_DONE;
1386 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
1387 nlh->nlmsg_flags = 0;
1389 iov[0].iov_base = nlh;
1390 iov[0].iov_len = sizeof(*nlh);
1392 iov[1].iov_base = msg;
1393 iov[1].iov_len = size;
1395 memset(&message, 0, sizeof(message));
1396 message.msg_name = &addr;
1397 message.msg_namelen = sizeof(addr);
1398 message.msg_iov = iov;
1399 message.msg_iovlen = 2;
1401 return sendmsg(fd, &message, 0);
1406 int fd, len, sock_opt;
1408 struct cn_msg *message;
1410 struct nlmsghdr *incoming_msg;
1411 struct cn_msg *incoming_cn_msg;
1412 struct hv_kvp_msg *hv_msg;
1419 struct hv_kvp_ipaddr_value *kvp_ip_val;
1422 openlog("KVP", 0, LOG_USER);
1423 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1425 * Retrieve OS release information.
1429 if (kvp_file_init()) {
1430 syslog(LOG_ERR, "Failed to initialize the pools");
1434 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
1436 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
1439 addr.nl_family = AF_NETLINK;
1442 addr.nl_groups = CN_KVP_IDX;
1445 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
1447 syslog(LOG_ERR, "bind failed; error:%d", error);
1451 sock_opt = addr.nl_groups;
1452 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
1454 * Register ourselves with the kernel.
1456 message = (struct cn_msg *)kvp_send_buffer;
1457 message->id.idx = CN_KVP_IDX;
1458 message->id.val = CN_KVP_VAL;
1460 hv_msg = (struct hv_kvp_msg *)message->data;
1461 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
1463 message->len = sizeof(struct hv_kvp_msg);
1465 len = netlink_send(fd, message);
1467 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
1475 struct sockaddr *addr_p = (struct sockaddr *) &addr;
1476 socklen_t addr_l = sizeof(addr);
1477 pfd.events = POLLIN;
1481 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
1484 if (len < 0 || addr.nl_pid) {
1485 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
1486 addr.nl_pid, errno, strerror(errno));
1491 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
1492 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
1493 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1496 * We will use the KVP header information to pass back
1497 * the error from this daemon. So, first copy the state
1498 * and set the error code to success.
1500 op = hv_msg->kvp_hdr.operation;
1501 pool = hv_msg->kvp_hdr.pool;
1502 hv_msg->error = HV_S_OK;
1504 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
1506 * Driver is registering with us; stash away the version
1510 p = (char *)hv_msg->body.kvp_register.version;
1511 lic_version = malloc(strlen(p) + 1);
1513 strcpy(lic_version, p);
1514 syslog(LOG_INFO, "KVP LIC Version: %s",
1517 syslog(LOG_ERR, "malloc failed");
1523 case KVP_OP_GET_IP_INFO:
1524 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1526 kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
1528 if (if_name == NULL) {
1530 * We could not map the mac address to an
1531 * interface name; return error.
1533 hv_msg->error = HV_E_FAIL;
1536 error = kvp_get_ip_info(
1537 0, if_name, KVP_OP_GET_IP_INFO,
1539 (MAX_IP_ADDR_SIZE * 2));
1542 hv_msg->error = error;
1547 case KVP_OP_SET_IP_INFO:
1548 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1549 if_name = kvp_get_if_name(
1550 (char *)kvp_ip_val->adapter_id);
1551 if (if_name == NULL) {
1553 * We could not map the guid to an
1554 * interface name; return error.
1556 hv_msg->error = HV_GUID_NOTFOUND;
1559 error = kvp_set_ip_info(if_name, kvp_ip_val);
1561 hv_msg->error = error;
1567 if (kvp_key_add_or_modify(pool,
1568 hv_msg->body.kvp_set.data.key,
1569 hv_msg->body.kvp_set.data.key_size,
1570 hv_msg->body.kvp_set.data.value,
1571 hv_msg->body.kvp_set.data.value_size))
1572 hv_msg->error = HV_S_CONT;
1576 if (kvp_get_value(pool,
1577 hv_msg->body.kvp_set.data.key,
1578 hv_msg->body.kvp_set.data.key_size,
1579 hv_msg->body.kvp_set.data.value,
1580 hv_msg->body.kvp_set.data.value_size))
1581 hv_msg->error = HV_S_CONT;
1585 if (kvp_key_delete(pool,
1586 hv_msg->body.kvp_delete.key,
1587 hv_msg->body.kvp_delete.key_size))
1588 hv_msg->error = HV_S_CONT;
1595 if (op != KVP_OP_ENUMERATE)
1599 * If the pool is KVP_POOL_AUTO, dynamically generate
1600 * both the key and the value; if not read from the
1603 if (pool != KVP_POOL_AUTO) {
1604 if (kvp_pool_enumerate(pool,
1605 hv_msg->body.kvp_enum_data.index,
1606 hv_msg->body.kvp_enum_data.data.key,
1607 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1608 hv_msg->body.kvp_enum_data.data.value,
1609 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1610 hv_msg->error = HV_S_CONT;
1614 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1615 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1616 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
1618 switch (hv_msg->body.kvp_enum_data.index) {
1619 case FullyQualifiedDomainName:
1620 kvp_get_domain_name(key_value,
1621 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1622 strcpy(key_name, "FullyQualifiedDomainName");
1624 case IntegrationServicesVersion:
1625 strcpy(key_name, "IntegrationServicesVersion");
1626 strcpy(key_value, lic_version);
1628 case NetworkAddressIPv4:
1629 kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
1630 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1631 strcpy(key_name, "NetworkAddressIPv4");
1633 case NetworkAddressIPv6:
1634 kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
1635 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1636 strcpy(key_name, "NetworkAddressIPv6");
1639 strcpy(key_value, os_build);
1640 strcpy(key_name, "OSBuildNumber");
1643 strcpy(key_value, os_name);
1644 strcpy(key_name, "OSName");
1646 case OSMajorVersion:
1647 strcpy(key_value, os_major);
1648 strcpy(key_name, "OSMajorVersion");
1650 case OSMinorVersion:
1651 strcpy(key_value, os_minor);
1652 strcpy(key_name, "OSMinorVersion");
1655 strcpy(key_value, os_version);
1656 strcpy(key_name, "OSVersion");
1658 case ProcessorArchitecture:
1659 strcpy(key_value, processor_arch);
1660 strcpy(key_name, "ProcessorArchitecture");
1663 hv_msg->error = HV_S_CONT;
1667 * Send the value back to the kernel. The response is
1668 * already in the receive buffer. Update the cn_msg header to
1669 * reflect the key value that has been added to the message
1673 incoming_cn_msg->id.idx = CN_KVP_IDX;
1674 incoming_cn_msg->id.val = CN_KVP_VAL;
1675 incoming_cn_msg->ack = 0;
1676 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
1678 len = netlink_send(fd, incoming_cn_msg);
1680 syslog(LOG_ERR, "net_link send failed; error:%d", len);