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 *lic_version = "Unknown version";
92 static struct utsname uts_buf;
95 * The location of the interface configuration file.
98 #define KVP_CONFIG_LOC "/var/opt/"
100 #define MAX_FILE_NAME 100
101 #define ENTRIES_PER_BLOCK 50
104 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
105 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
108 struct kvp_file_state {
111 struct kvp_record *records;
113 char fname[MAX_FILE_NAME];
116 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
118 static void kvp_acquire_lock(int pool)
120 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
123 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
124 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
129 static void kvp_release_lock(int pool)
131 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
134 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
136 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
141 static void kvp_update_file(int pool)
144 size_t bytes_written;
147 * We are going to write our in-memory registry out to
148 * disk; acquire the lock first.
150 kvp_acquire_lock(pool);
152 filep = fopen(kvp_file_info[pool].fname, "w");
154 kvp_release_lock(pool);
155 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
159 bytes_written = fwrite(kvp_file_info[pool].records,
160 sizeof(struct kvp_record),
161 kvp_file_info[pool].num_records, filep);
163 if (ferror(filep) || fclose(filep)) {
164 kvp_release_lock(pool);
165 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
169 kvp_release_lock(pool);
172 static void kvp_update_mem_state(int pool)
175 size_t records_read = 0;
176 struct kvp_record *record = kvp_file_info[pool].records;
177 struct kvp_record *readp;
178 int num_blocks = kvp_file_info[pool].num_blocks;
179 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
181 kvp_acquire_lock(pool);
183 filep = fopen(kvp_file_info[pool].fname, "r");
185 kvp_release_lock(pool);
186 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
190 readp = &record[records_read];
191 records_read += fread(readp, sizeof(struct kvp_record),
192 ENTRIES_PER_BLOCK * num_blocks,
196 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
202 * We have more data to read.
205 record = realloc(record, alloc_unit * num_blocks);
207 if (record == NULL) {
208 syslog(LOG_ERR, "malloc failed");
216 kvp_file_info[pool].num_blocks = num_blocks;
217 kvp_file_info[pool].records = record;
218 kvp_file_info[pool].num_records = records_read;
221 kvp_release_lock(pool);
223 static int kvp_file_init(void)
229 struct kvp_record *record;
230 struct kvp_record *readp;
233 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
235 if (access("/var/opt/hyperv", F_OK)) {
236 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
237 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
242 for (i = 0; i < KVP_POOL_COUNT; i++) {
243 fname = kvp_file_info[i].fname;
246 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
247 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
253 filep = fopen(fname, "r");
257 record = malloc(alloc_unit * num_blocks);
258 if (record == NULL) {
263 readp = &record[records_read];
264 records_read += fread(readp, sizeof(struct kvp_record),
269 syslog(LOG_ERR, "Failed to read file, pool: %d",
276 * We have more data to read.
279 record = realloc(record, alloc_unit *
281 if (record == NULL) {
289 kvp_file_info[i].fd = fd;
290 kvp_file_info[i].num_blocks = num_blocks;
291 kvp_file_info[i].records = record;
292 kvp_file_info[i].num_records = records_read;
300 static int kvp_key_delete(int pool, __u8 *key, int key_size)
305 struct kvp_record *record;
308 * First update the in-memory state.
310 kvp_update_mem_state(pool);
312 num_records = kvp_file_info[pool].num_records;
313 record = kvp_file_info[pool].records;
315 for (i = 0; i < num_records; i++) {
316 if (memcmp(key, record[i].key, key_size))
319 * Found a match; just move the remaining
322 if (i == num_records) {
323 kvp_file_info[pool].num_records--;
324 kvp_update_file(pool);
330 for (; k < num_records; k++) {
331 strcpy(record[j].key, record[k].key);
332 strcpy(record[j].value, record[k].value);
336 kvp_file_info[pool].num_records--;
337 kvp_update_file(pool);
343 static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
348 struct kvp_record *record;
351 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
352 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
356 * First update the in-memory state.
358 kvp_update_mem_state(pool);
360 num_records = kvp_file_info[pool].num_records;
361 record = kvp_file_info[pool].records;
362 num_blocks = kvp_file_info[pool].num_blocks;
364 for (i = 0; i < num_records; i++) {
365 if (memcmp(key, record[i].key, key_size))
368 * Found a match; just update the value -
369 * this is the modify case.
371 memcpy(record[i].value, value, value_size);
372 kvp_update_file(pool);
377 * Need to add a new entry;
379 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
380 /* Need to allocate a larger array for reg entries. */
381 record = realloc(record, sizeof(struct kvp_record) *
382 ENTRIES_PER_BLOCK * (num_blocks + 1));
386 kvp_file_info[pool].num_blocks++;
389 memcpy(record[i].value, value, value_size);
390 memcpy(record[i].key, key, key_size);
391 kvp_file_info[pool].records = record;
392 kvp_file_info[pool].num_records++;
393 kvp_update_file(pool);
397 static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
402 struct kvp_record *record;
404 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
405 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
409 * First update the in-memory state.
411 kvp_update_mem_state(pool);
413 num_records = kvp_file_info[pool].num_records;
414 record = kvp_file_info[pool].records;
416 for (i = 0; i < num_records; i++) {
417 if (memcmp(key, record[i].key, key_size))
420 * Found a match; just copy the value out.
422 memcpy(value, record[i].value, value_size);
429 static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
430 __u8 *value, int value_size)
432 struct kvp_record *record;
435 * First update our in-memory database.
437 kvp_update_mem_state(pool);
438 record = kvp_file_info[pool].records;
440 if (index >= kvp_file_info[pool].num_records) {
444 memcpy(key, record[index].key, key_size);
445 memcpy(value, record[index].value, value_size);
450 void kvp_get_os_info(void)
456 os_build = uts_buf.release;
457 os_name = uts_buf.sysname;
458 processor_arch = uts_buf.machine;
461 * The current windows host (win7) expects the build
462 * string to be of the form: x.y.z
463 * Strip additional information we may have.
465 p = strchr(os_build, '-');
470 * Parse the /etc/os-release file if present:
471 * http://www.freedesktop.org/software/systemd/man/os-release.html
473 file = fopen("/etc/os-release", "r");
475 while (fgets(buf, sizeof(buf), file)) {
478 /* Ignore comments */
482 /* Split into name=value */
483 p = strchr(buf, '=');
488 /* Remove quotes and newline; un-escape */
497 } else if (*p == '\'' || *p == '"' ||
506 if (!strcmp(buf, "NAME")) {
511 } else if (!strcmp(buf, "VERSION_ID")) {
522 /* Fallback for older RH/SUSE releases */
523 file = fopen("/etc/SuSE-release", "r");
525 goto kvp_osinfo_found;
526 file = fopen("/etc/redhat-release", "r");
528 goto kvp_osinfo_found;
531 * We don't have information about the os.
536 /* up to three lines */
537 p = fgets(buf, sizeof(buf), file);
539 p = strchr(buf, '\n');
548 p = fgets(buf, sizeof(buf), file);
550 p = strchr(buf, '\n');
559 p = fgets(buf, sizeof(buf), file);
561 p = strchr(buf, '\n');
579 * Retrieve an interface name corresponding to the specified guid.
580 * If there is a match, the function returns a pointer
581 * to the interface name and if not, a NULL is returned.
582 * If a match is found, the caller is responsible for
583 * freeing the memory.
586 static char *kvp_get_if_name(char *guid)
589 struct dirent *entry;
592 char *if_name = NULL;
594 char *kvp_net_dir = "/sys/class/net/";
597 dir = opendir(kvp_net_dir);
601 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
602 q = dev_id + strlen(kvp_net_dir);
604 while ((entry = readdir(dir)) != NULL) {
606 * Set the state for the next pass.
609 strcat(dev_id, entry->d_name);
610 strcat(dev_id, "/device/device_id");
612 file = fopen(dev_id, "r");
616 p = fgets(buf, sizeof(buf), file);
622 if (!strcmp(p, guid)) {
624 * Found the guid match; return the interface
625 * name. The caller will free the memory.
627 if_name = strdup(entry->d_name);
640 * Retrieve the MAC address given the interface name.
643 static char *kvp_if_name_to_mac(char *if_name)
650 char *mac_addr = NULL;
652 snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
653 if_name, "/address");
655 file = fopen(addr_file, "r");
659 p = fgets(buf, sizeof(buf), file);
664 for (i = 0; i < strlen(p); i++)
665 p[i] = toupper(p[i]);
666 mac_addr = strdup(p);
675 * Retrieve the interface name given tha MAC address.
678 static char *kvp_mac_to_if_name(char *mac)
681 struct dirent *entry;
684 char *if_name = NULL;
686 char *kvp_net_dir = "/sys/class/net/";
690 dir = opendir(kvp_net_dir);
694 snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
695 q = dev_id + strlen(kvp_net_dir);
697 while ((entry = readdir(dir)) != NULL) {
699 * Set the state for the next pass.
703 strcat(dev_id, entry->d_name);
704 strcat(dev_id, "/address");
706 file = fopen(dev_id, "r");
710 p = fgets(buf, sizeof(buf), file);
716 for (i = 0; i < strlen(p); i++)
717 p[i] = toupper(p[i]);
719 if (!strcmp(p, mac)) {
721 * Found the MAC match; return the interface
722 * name. The caller will free the memory.
724 if_name = strdup(entry->d_name);
737 static void kvp_process_ipconfig_file(char *cmd,
738 char *config_buf, int len,
739 int element_size, int offset)
747 * First execute the command.
749 file = popen(cmd, "r");
754 memset(config_buf, 0, len);
755 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
756 if ((len - strlen(config_buf)) < (element_size + 1))
761 strcat(config_buf, p);
762 strcat(config_buf, ";");
767 static void kvp_get_ipconfig_info(char *if_name,
768 struct hv_kvp_ipaddr_value *buffer)
776 * Get the address of default gateway (ipv4).
778 sprintf(cmd, "%s %s", "ip route show dev", if_name);
779 strcat(cmd, " | awk '/default/ {print $3 }'");
782 * Execute the command to gather gateway info.
784 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
785 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
788 * Get the address of default gateway (ipv6).
790 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
791 strcat(cmd, " | awk '/default/ {print $3 }'");
794 * Execute the command to gather gateway info (ipv6).
796 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
797 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
801 * Gather the DNS state.
802 * Since there is no standard way to get this information
803 * across various distributions of interest; we just invoke
804 * an external script that needs to be ported across distros
807 * Following is the expected format of the information from the script:
809 * ipaddr1 (nameserver1)
810 * ipaddr2 (nameserver2)
815 sprintf(cmd, "%s", "hv_get_dns_info");
818 * Execute the command to gather DNS info.
820 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
821 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
824 * Gather the DHCP state.
825 * We will gather this state by invoking an external script.
826 * The parameter to the script is the interface name.
827 * Here is the expected output:
829 * Enabled: DHCP enabled.
832 sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
834 file = popen(cmd, "r");
838 p = fgets(dhcp_info, sizeof(dhcp_info), file);
844 if (!strncmp(p, "Enabled", 7))
845 buffer->dhcp_enabled = 1;
847 buffer->dhcp_enabled = 0;
853 static unsigned int hweight32(unsigned int *w)
855 unsigned int res = *w - ((*w >> 1) & 0x55555555);
856 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
857 res = (res + (res >> 4)) & 0x0F0F0F0F;
858 res = res + (res >> 8);
859 return (res + (res >> 16)) & 0x000000FF;
862 static int kvp_process_ip_address(void *addrp,
863 int family, char *buffer,
864 int length, int *offset)
866 struct sockaddr_in *addr;
867 struct sockaddr_in6 *addr6;
872 if (family == AF_INET) {
873 addr = (struct sockaddr_in *)addrp;
874 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
875 addr_length = INET_ADDRSTRLEN;
877 addr6 = (struct sockaddr_in6 *)addrp;
878 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
879 addr_length = INET6_ADDRSTRLEN;
882 if ((length - *offset) < addr_length + 1)
885 strcpy(buffer, "inet_ntop failed\n");
894 *offset += strlen(str) + 1;
899 kvp_get_ip_info(int family, char *if_name, int op,
900 void *out_buffer, int length)
902 struct ifaddrs *ifap;
903 struct ifaddrs *curp;
908 struct hv_kvp_ipaddr_value *ip_buffer;
909 char cidr_mask[5]; /* /xyz */
914 struct sockaddr_in6 *addr6;
916 if (op == KVP_OP_ENUMERATE) {
919 ip_buffer = out_buffer;
920 buffer = (char *)ip_buffer->ip_addr;
921 ip_buffer->addr_family = 0;
924 * On entry into this function, the buffer is capable of holding the
928 if (getifaddrs(&ifap)) {
929 strcpy(buffer, "getifaddrs failed\n");
934 while (curp != NULL) {
935 if (curp->ifa_addr == NULL) {
936 curp = curp->ifa_next;
940 if ((if_name != NULL) &&
941 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
943 * We want info about a specific interface;
946 curp = curp->ifa_next;
951 * We only support two address families: AF_INET and AF_INET6.
952 * If a family value of 0 is specified, we collect both
953 * supported address families; if not we gather info on
954 * the specified address family.
956 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
957 curp = curp->ifa_next;
960 if ((curp->ifa_addr->sa_family != AF_INET) &&
961 (curp->ifa_addr->sa_family != AF_INET6)) {
962 curp = curp->ifa_next;
966 if (op == KVP_OP_GET_IP_INFO) {
968 * Gather info other than the IP address.
969 * IP address info will be gathered later.
971 if (curp->ifa_addr->sa_family == AF_INET) {
972 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
976 error = kvp_process_ip_address(
986 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
989 * Get subnet info in CIDR format.
992 sn_str = (char *)ip_buffer->sub_net;
993 addr6 = (struct sockaddr_in6 *)
995 w = addr6->sin6_addr.s6_addr32;
997 for (i = 0; i < 4; i++)
998 weight += hweight32(&w[i]);
1000 sprintf(cidr_mask, "/%d", weight);
1001 if ((length - sn_offset) <
1002 (strlen(cidr_mask) + 1))
1006 strcpy(sn_str, cidr_mask);
1008 strcat(sn_str, cidr_mask);
1009 strcat((char *)ip_buffer->sub_net, ";");
1010 sn_offset += strlen(sn_str) + 1;
1014 * Collect other ip related configuration info.
1017 kvp_get_ipconfig_info(if_name, ip_buffer);
1021 error = kvp_process_ip_address(curp->ifa_addr,
1022 curp->ifa_addr->sa_family,
1028 curp = curp->ifa_next;
1037 static int expand_ipv6(char *addr, int type)
1040 struct in6_addr v6_addr;
1042 ret = inet_pton(AF_INET6, addr, &v6_addr);
1045 if (type == NETMASK)
1050 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1051 "%02x%02x:%02x%02x:%02x%02x",
1052 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
1053 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
1054 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
1055 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
1056 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
1057 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
1058 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
1059 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
1065 static int is_ipv4(char *addr)
1068 struct in_addr ipv4_addr;
1070 ret = inet_pton(AF_INET, addr, &ipv4_addr);
1077 static int parse_ip_val_buffer(char *in_buf, int *offset,
1078 char *out_buf, int out_len)
1084 * in_buf has sequence of characters that are seperated by
1085 * the character ';'. The last sequence does not have the
1086 * terminating ";" character.
1088 start = in_buf + *offset;
1090 x = strchr(start, ';');
1094 x = start + strlen(start);
1096 if (strlen(start) != 0) {
1099 * Get rid of leading spaces.
1101 while (start[i] == ' ')
1104 if ((x - start) <= out_len) {
1105 strcpy(out_buf, (start + i));
1106 *offset += (x - start) + 1;
1113 static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
1117 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
1126 static int process_ip_string(FILE *f, char *ip_string, int type)
1129 char addr[INET6_ADDRSTRLEN];
1136 memset(addr, 0, sizeof(addr));
1138 while (parse_ip_val_buffer(ip_string, &offset, addr,
1139 (MAX_IP_ADDR_SIZE * 2))) {
1142 if (is_ipv4(addr)) {
1145 snprintf(str, sizeof(str), "%s", "IPADDR");
1148 snprintf(str, sizeof(str), "%s", "NETMASK");
1151 snprintf(str, sizeof(str), "%s", "GATEWAY");
1154 snprintf(str, sizeof(str), "%s", "DNS");
1159 snprintf(sub_str, sizeof(sub_str),
1162 snprintf(sub_str, sizeof(sub_str),
1165 } else if (type == DNS) {
1166 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1170 } else if (expand_ipv6(addr, type)) {
1173 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1176 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1179 snprintf(str, sizeof(str), "%s",
1183 snprintf(str, sizeof(str), "%s", "DNS");
1186 if ((j != 0) || (type == DNS)) {
1188 snprintf(sub_str, sizeof(sub_str),
1191 snprintf(sub_str, sizeof(sub_str),
1194 } else if (type == DNS) {
1195 snprintf(sub_str, sizeof(sub_str),
1199 return HV_INVALIDARG;
1202 error = kvp_write_file(f, str, sub_str, addr);
1205 memset(addr, 0, sizeof(addr));
1211 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1220 * Set the configuration for the specified interface with
1221 * the information provided. Since there is no standard
1222 * way to configure an interface, we will have an external
1223 * script that does the job of configuring the interface and
1224 * flushing the configuration.
1226 * The parameters passed to this external script are:
1227 * 1. A configuration file that has the specified configuration.
1229 * We will embed the name of the interface in the configuration
1230 * file: ifcfg-ethx (where ethx is the interface name).
1232 * The information provided here may be more than what is needed
1233 * in a given distro to configure the interface and so are free
1234 * ignore information that may not be relevant.
1236 * Here is the format of the ip configuration file:
1239 * IF_NAME=interface name
1240 * DHCP=yes (This is optional; if yes, DHCP is configured)
1244 * IPADDR_x=ipaddry (where y = x + 1)
1247 * NETMASK_x=netmasky (where y = x + 1)
1250 * GATEWAY_x=ipaddry (where y = x + 1)
1252 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1254 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1255 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1258 * The host can specify multiple ipv4 and ipv6 addresses to be
1259 * configured for the interface. Furthermore, the configuration
1260 * needs to be persistent. A subsequent GET call on the interface
1261 * is expected to return the configuration that is set via the SET
1265 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1266 "hyperv/ifcfg-", if_name);
1268 file = fopen(if_file, "w");
1271 syslog(LOG_ERR, "Failed to open config file");
1276 * First write out the MAC address.
1279 mac_addr = kvp_if_name_to_mac(if_name);
1280 if (mac_addr == NULL) {
1285 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1289 error = kvp_write_file(file, "IF_NAME", "", if_name);
1293 if (new_val->dhcp_enabled) {
1294 error = kvp_write_file(file, "DHCP", "", "yes");
1305 * Write the configuration for ipaddress, netmask, gateway and
1309 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1313 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1317 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1321 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1330 * Now that we have populated the configuration file,
1331 * invoke the external script to do its magic.
1334 snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1339 syslog(LOG_ERR, "Failed to write config file");
1347 kvp_get_domain_name(char *buffer, int length)
1349 struct addrinfo hints, *info ;
1352 gethostname(buffer, length);
1353 memset(&hints, 0, sizeof(hints));
1354 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1355 hints.ai_socktype = SOCK_STREAM;
1356 hints.ai_flags = AI_CANONNAME;
1358 error = getaddrinfo(buffer, NULL, &hints, &info);
1360 strcpy(buffer, "getaddrinfo failed\n");
1363 strcpy(buffer, info->ai_canonname);
1369 netlink_send(int fd, struct cn_msg *msg)
1371 struct nlmsghdr *nlh;
1373 struct msghdr message;
1375 struct iovec iov[2];
1377 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
1379 nlh = (struct nlmsghdr *)buffer;
1381 nlh->nlmsg_pid = getpid();
1382 nlh->nlmsg_type = NLMSG_DONE;
1383 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
1384 nlh->nlmsg_flags = 0;
1386 iov[0].iov_base = nlh;
1387 iov[0].iov_len = sizeof(*nlh);
1389 iov[1].iov_base = msg;
1390 iov[1].iov_len = size;
1392 memset(&message, 0, sizeof(message));
1393 message.msg_name = &addr;
1394 message.msg_namelen = sizeof(addr);
1395 message.msg_iov = iov;
1396 message.msg_iovlen = 2;
1398 return sendmsg(fd, &message, 0);
1403 int fd, len, sock_opt;
1405 struct cn_msg *message;
1407 struct nlmsghdr *incoming_msg;
1408 struct cn_msg *incoming_cn_msg;
1409 struct hv_kvp_msg *hv_msg;
1416 struct hv_kvp_ipaddr_value *kvp_ip_val;
1419 openlog("KVP", 0, LOG_USER);
1420 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1422 * Retrieve OS release information.
1426 if (kvp_file_init()) {
1427 syslog(LOG_ERR, "Failed to initialize the pools");
1431 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
1433 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
1436 addr.nl_family = AF_NETLINK;
1439 addr.nl_groups = CN_KVP_IDX;
1442 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
1444 syslog(LOG_ERR, "bind failed; error:%d", error);
1448 sock_opt = addr.nl_groups;
1449 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
1451 * Register ourselves with the kernel.
1453 message = (struct cn_msg *)kvp_send_buffer;
1454 message->id.idx = CN_KVP_IDX;
1455 message->id.val = CN_KVP_VAL;
1457 hv_msg = (struct hv_kvp_msg *)message->data;
1458 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
1460 message->len = sizeof(struct hv_kvp_msg);
1462 len = netlink_send(fd, message);
1464 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
1472 struct sockaddr *addr_p = (struct sockaddr *) &addr;
1473 socklen_t addr_l = sizeof(addr);
1474 pfd.events = POLLIN;
1478 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
1481 if (len < 0 || addr.nl_pid) {
1482 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
1483 addr.nl_pid, errno, strerror(errno));
1488 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
1489 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
1490 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1493 * We will use the KVP header information to pass back
1494 * the error from this daemon. So, first copy the state
1495 * and set the error code to success.
1497 op = hv_msg->kvp_hdr.operation;
1498 pool = hv_msg->kvp_hdr.pool;
1499 hv_msg->error = HV_S_OK;
1501 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
1503 * Driver is registering with us; stash away the version
1507 p = (char *)hv_msg->body.kvp_register.version;
1508 lic_version = malloc(strlen(p) + 1);
1510 strcpy(lic_version, p);
1511 syslog(LOG_INFO, "KVP LIC Version: %s",
1514 syslog(LOG_ERR, "malloc failed");
1520 case KVP_OP_GET_IP_INFO:
1521 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1523 kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
1525 if (if_name == NULL) {
1527 * We could not map the mac address to an
1528 * interface name; return error.
1530 hv_msg->error = HV_E_FAIL;
1533 error = kvp_get_ip_info(
1534 0, if_name, KVP_OP_GET_IP_INFO,
1536 (MAX_IP_ADDR_SIZE * 2));
1539 hv_msg->error = error;
1544 case KVP_OP_SET_IP_INFO:
1545 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1546 if_name = kvp_get_if_name(
1547 (char *)kvp_ip_val->adapter_id);
1548 if (if_name == NULL) {
1550 * We could not map the guid to an
1551 * interface name; return error.
1553 hv_msg->error = HV_GUID_NOTFOUND;
1556 error = kvp_set_ip_info(if_name, kvp_ip_val);
1558 hv_msg->error = error;
1564 if (kvp_key_add_or_modify(pool,
1565 hv_msg->body.kvp_set.data.key,
1566 hv_msg->body.kvp_set.data.key_size,
1567 hv_msg->body.kvp_set.data.value,
1568 hv_msg->body.kvp_set.data.value_size))
1569 hv_msg->error = HV_S_CONT;
1573 if (kvp_get_value(pool,
1574 hv_msg->body.kvp_set.data.key,
1575 hv_msg->body.kvp_set.data.key_size,
1576 hv_msg->body.kvp_set.data.value,
1577 hv_msg->body.kvp_set.data.value_size))
1578 hv_msg->error = HV_S_CONT;
1582 if (kvp_key_delete(pool,
1583 hv_msg->body.kvp_delete.key,
1584 hv_msg->body.kvp_delete.key_size))
1585 hv_msg->error = HV_S_CONT;
1592 if (op != KVP_OP_ENUMERATE)
1596 * If the pool is KVP_POOL_AUTO, dynamically generate
1597 * both the key and the value; if not read from the
1600 if (pool != KVP_POOL_AUTO) {
1601 if (kvp_pool_enumerate(pool,
1602 hv_msg->body.kvp_enum_data.index,
1603 hv_msg->body.kvp_enum_data.data.key,
1604 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1605 hv_msg->body.kvp_enum_data.data.value,
1606 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1607 hv_msg->error = HV_S_CONT;
1611 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1612 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1613 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
1615 switch (hv_msg->body.kvp_enum_data.index) {
1616 case FullyQualifiedDomainName:
1617 kvp_get_domain_name(key_value,
1618 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1619 strcpy(key_name, "FullyQualifiedDomainName");
1621 case IntegrationServicesVersion:
1622 strcpy(key_name, "IntegrationServicesVersion");
1623 strcpy(key_value, lic_version);
1625 case NetworkAddressIPv4:
1626 kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
1627 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1628 strcpy(key_name, "NetworkAddressIPv4");
1630 case NetworkAddressIPv6:
1631 kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
1632 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1633 strcpy(key_name, "NetworkAddressIPv6");
1636 strcpy(key_value, os_build);
1637 strcpy(key_name, "OSBuildNumber");
1640 strcpy(key_value, os_name);
1641 strcpy(key_name, "OSName");
1643 case OSMajorVersion:
1644 strcpy(key_value, os_major);
1645 strcpy(key_name, "OSMajorVersion");
1647 case OSMinorVersion:
1648 strcpy(key_value, os_minor);
1649 strcpy(key_name, "OSMinorVersion");
1652 strcpy(key_value, os_build);
1653 strcpy(key_name, "OSVersion");
1655 case ProcessorArchitecture:
1656 strcpy(key_value, processor_arch);
1657 strcpy(key_name, "ProcessorArchitecture");
1660 hv_msg->error = HV_S_CONT;
1664 * Send the value back to the kernel. The response is
1665 * already in the receive buffer. Update the cn_msg header to
1666 * reflect the key value that has been added to the message
1670 incoming_cn_msg->id.idx = CN_KVP_IDX;
1671 incoming_cn_msg->id.val = CN_KVP_VAL;
1672 incoming_cn_msg->ack = 0;
1673 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
1675 len = netlink_send(fd, incoming_cn_msg);
1677 syslog(LOG_ERR, "net_link send failed; error:%d", len);