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);
164 kvp_release_lock(pool);
167 static void kvp_update_mem_state(int pool)
170 size_t records_read = 0;
171 struct kvp_record *record = kvp_file_info[pool].records;
172 struct kvp_record *readp;
173 int num_blocks = kvp_file_info[pool].num_blocks;
174 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
176 kvp_acquire_lock(pool);
178 filep = fopen(kvp_file_info[pool].fname, "r");
180 kvp_release_lock(pool);
181 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
184 while (!feof(filep)) {
185 readp = &record[records_read];
186 records_read += fread(readp, sizeof(struct kvp_record),
187 ENTRIES_PER_BLOCK * num_blocks,
192 * We have more data to read.
195 record = realloc(record, alloc_unit * num_blocks);
197 if (record == NULL) {
198 syslog(LOG_ERR, "malloc failed");
206 kvp_file_info[pool].num_blocks = num_blocks;
207 kvp_file_info[pool].records = record;
208 kvp_file_info[pool].num_records = records_read;
210 kvp_release_lock(pool);
212 static int kvp_file_init(void)
218 struct kvp_record *record;
219 struct kvp_record *readp;
222 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
224 if (access("/var/opt/hyperv", F_OK)) {
225 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
226 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
231 for (i = 0; i < KVP_POOL_COUNT; i++) {
232 fname = kvp_file_info[i].fname;
235 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
236 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
242 filep = fopen(fname, "r");
246 record = malloc(alloc_unit * num_blocks);
247 if (record == NULL) {
251 while (!feof(filep)) {
252 readp = &record[records_read];
253 records_read += fread(readp, sizeof(struct kvp_record),
259 * We have more data to read.
262 record = realloc(record, alloc_unit *
264 if (record == NULL) {
272 kvp_file_info[i].fd = fd;
273 kvp_file_info[i].num_blocks = num_blocks;
274 kvp_file_info[i].records = record;
275 kvp_file_info[i].num_records = records_read;
283 static int kvp_key_delete(int pool, __u8 *key, int key_size)
288 struct kvp_record *record;
291 * First update the in-memory state.
293 kvp_update_mem_state(pool);
295 num_records = kvp_file_info[pool].num_records;
296 record = kvp_file_info[pool].records;
298 for (i = 0; i < num_records; i++) {
299 if (memcmp(key, record[i].key, key_size))
302 * Found a match; just move the remaining
305 if (i == num_records) {
306 kvp_file_info[pool].num_records--;
307 kvp_update_file(pool);
313 for (; k < num_records; k++) {
314 strcpy(record[j].key, record[k].key);
315 strcpy(record[j].value, record[k].value);
319 kvp_file_info[pool].num_records--;
320 kvp_update_file(pool);
326 static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
331 struct kvp_record *record;
334 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
335 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
339 * First update the in-memory state.
341 kvp_update_mem_state(pool);
343 num_records = kvp_file_info[pool].num_records;
344 record = kvp_file_info[pool].records;
345 num_blocks = kvp_file_info[pool].num_blocks;
347 for (i = 0; i < num_records; i++) {
348 if (memcmp(key, record[i].key, key_size))
351 * Found a match; just update the value -
352 * this is the modify case.
354 memcpy(record[i].value, value, value_size);
355 kvp_update_file(pool);
360 * Need to add a new entry;
362 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
363 /* Need to allocate a larger array for reg entries. */
364 record = realloc(record, sizeof(struct kvp_record) *
365 ENTRIES_PER_BLOCK * (num_blocks + 1));
369 kvp_file_info[pool].num_blocks++;
372 memcpy(record[i].value, value, value_size);
373 memcpy(record[i].key, key, key_size);
374 kvp_file_info[pool].records = record;
375 kvp_file_info[pool].num_records++;
376 kvp_update_file(pool);
380 static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
385 struct kvp_record *record;
387 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
388 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
392 * First update the in-memory state.
394 kvp_update_mem_state(pool);
396 num_records = kvp_file_info[pool].num_records;
397 record = kvp_file_info[pool].records;
399 for (i = 0; i < num_records; i++) {
400 if (memcmp(key, record[i].key, key_size))
403 * Found a match; just copy the value out.
405 memcpy(value, record[i].value, value_size);
412 static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
413 __u8 *value, int value_size)
415 struct kvp_record *record;
418 * First update our in-memory database.
420 kvp_update_mem_state(pool);
421 record = kvp_file_info[pool].records;
423 if (index >= kvp_file_info[pool].num_records) {
427 memcpy(key, record[index].key, key_size);
428 memcpy(value, record[index].value, value_size);
433 void kvp_get_os_info(void)
439 os_build = uts_buf.release;
440 processor_arch = uts_buf.machine;
443 * The current windows host (win7) expects the build
444 * string to be of the form: x.y.z
445 * Strip additional information we may have.
447 p = strchr(os_build, '-');
451 file = fopen("/etc/SuSE-release", "r");
453 goto kvp_osinfo_found;
454 file = fopen("/etc/redhat-release", "r");
456 goto kvp_osinfo_found;
458 * Add code for other supported platforms.
462 * We don't have information about the os.
464 os_name = uts_buf.sysname;
468 /* up to three lines */
469 p = fgets(buf, sizeof(buf), file);
471 p = strchr(buf, '\n');
480 p = fgets(buf, sizeof(buf), file);
482 p = strchr(buf, '\n');
491 p = fgets(buf, sizeof(buf), file);
493 p = strchr(buf, '\n');
511 * Retrieve an interface name corresponding to the specified guid.
512 * If there is a match, the function returns a pointer
513 * to the interface name and if not, a NULL is returned.
514 * If a match is found, the caller is responsible for
515 * freeing the memory.
518 static char *kvp_get_if_name(char *guid)
521 struct dirent *entry;
524 char *if_name = NULL;
526 char *kvp_net_dir = "/sys/class/net/";
529 dir = opendir(kvp_net_dir);
533 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
534 q = dev_id + strlen(kvp_net_dir);
536 while ((entry = readdir(dir)) != NULL) {
538 * Set the state for the next pass.
541 strcat(dev_id, entry->d_name);
542 strcat(dev_id, "/device/device_id");
544 file = fopen(dev_id, "r");
548 p = fgets(buf, sizeof(buf), file);
554 if (!strcmp(p, guid)) {
556 * Found the guid match; return the interface
557 * name. The caller will free the memory.
559 if_name = strdup(entry->d_name);
572 * Retrieve the MAC address given the interface name.
575 static char *kvp_if_name_to_mac(char *if_name)
582 char *mac_addr = NULL;
584 snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
585 if_name, "/address");
587 file = fopen(addr_file, "r");
591 p = fgets(buf, sizeof(buf), file);
596 for (i = 0; i < strlen(p); i++)
597 p[i] = toupper(p[i]);
598 mac_addr = strdup(p);
606 static void kvp_process_ipconfig_file(char *cmd,
607 char *config_buf, int len,
608 int element_size, int offset)
616 * First execute the command.
618 file = popen(cmd, "r");
623 memset(config_buf, 0, len);
624 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
625 if ((len - strlen(config_buf)) < (element_size + 1))
630 strcat(config_buf, p);
631 strcat(config_buf, ";");
636 static void kvp_get_ipconfig_info(char *if_name,
637 struct hv_kvp_ipaddr_value *buffer)
645 * Get the address of default gateway (ipv4).
647 sprintf(cmd, "%s %s", "ip route show dev", if_name);
648 strcat(cmd, " | awk '/default/ {print $3 }'");
651 * Execute the command to gather gateway info.
653 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
654 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
657 * Get the address of default gateway (ipv6).
659 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
660 strcat(cmd, " | awk '/default/ {print $3 }'");
663 * Execute the command to gather gateway info (ipv6).
665 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
666 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
670 * Gather the DNS state.
671 * Since there is no standard way to get this information
672 * across various distributions of interest; we just invoke
673 * an external script that needs to be ported across distros
676 * Following is the expected format of the information from the script:
678 * ipaddr1 (nameserver1)
679 * ipaddr2 (nameserver2)
684 sprintf(cmd, "%s", "hv_get_dns_info");
687 * Execute the command to gather DNS info.
689 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
690 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
693 * Gather the DHCP state.
694 * We will gather this state by invoking an external script.
695 * The parameter to the script is the interface name.
696 * Here is the expected output:
698 * Enabled: DHCP enabled.
701 sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
703 file = popen(cmd, "r");
707 p = fgets(dhcp_info, sizeof(dhcp_info), file);
713 if (!strncmp(p, "Enabled", 7))
714 buffer->dhcp_enabled = 1;
716 buffer->dhcp_enabled = 0;
722 static unsigned int hweight32(unsigned int *w)
724 unsigned int res = *w - ((*w >> 1) & 0x55555555);
725 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
726 res = (res + (res >> 4)) & 0x0F0F0F0F;
727 res = res + (res >> 8);
728 return (res + (res >> 16)) & 0x000000FF;
731 static int kvp_process_ip_address(void *addrp,
732 int family, char *buffer,
733 int length, int *offset)
735 struct sockaddr_in *addr;
736 struct sockaddr_in6 *addr6;
741 if (family == AF_INET) {
742 addr = (struct sockaddr_in *)addrp;
743 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
744 addr_length = INET_ADDRSTRLEN;
746 addr6 = (struct sockaddr_in6 *)addrp;
747 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
748 addr_length = INET6_ADDRSTRLEN;
751 if ((length - *offset) < addr_length + 1)
754 strcpy(buffer, "inet_ntop failed\n");
763 *offset += strlen(str) + 1;
768 kvp_get_ip_address(int family, char *if_name, int op,
769 void *out_buffer, int length)
771 struct ifaddrs *ifap;
772 struct ifaddrs *curp;
777 struct hv_kvp_ipaddr_value *ip_buffer;
778 char cidr_mask[5]; /* /xyz */
783 struct sockaddr_in6 *addr6;
785 if (op == KVP_OP_ENUMERATE) {
788 ip_buffer = out_buffer;
789 buffer = (char *)ip_buffer->ip_addr;
790 ip_buffer->addr_family = 0;
793 * On entry into this function, the buffer is capable of holding the
797 if (getifaddrs(&ifap)) {
798 strcpy(buffer, "getifaddrs failed\n");
803 while (curp != NULL) {
804 if (curp->ifa_addr == NULL) {
805 curp = curp->ifa_next;
809 if ((if_name != NULL) &&
810 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
812 * We want info about a specific interface;
815 curp = curp->ifa_next;
820 * We only support two address families: AF_INET and AF_INET6.
821 * If a family value of 0 is specified, we collect both
822 * supported address families; if not we gather info on
823 * the specified address family.
825 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
826 curp = curp->ifa_next;
829 if ((curp->ifa_addr->sa_family != AF_INET) &&
830 (curp->ifa_addr->sa_family != AF_INET6)) {
831 curp = curp->ifa_next;
835 if (op == KVP_OP_GET_IP_INFO) {
837 * Gather info other than the IP address.
838 * IP address info will be gathered later.
840 if (curp->ifa_addr->sa_family == AF_INET) {
841 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
845 error = kvp_process_ip_address(
855 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
858 * Get subnet info in CIDR format.
861 sn_str = (char *)ip_buffer->sub_net;
862 addr6 = (struct sockaddr_in6 *)
864 w = addr6->sin6_addr.s6_addr32;
866 for (i = 0; i < 4; i++)
867 weight += hweight32(&w[i]);
869 sprintf(cidr_mask, "/%d", weight);
870 if ((length - sn_offset) <
871 (strlen(cidr_mask) + 1))
875 strcpy(sn_str, cidr_mask);
877 strcat(sn_str, cidr_mask);
878 strcat((char *)ip_buffer->sub_net, ";");
879 sn_offset += strlen(sn_str) + 1;
883 * Collect other ip related configuration info.
886 kvp_get_ipconfig_info(if_name, ip_buffer);
890 error = kvp_process_ip_address(curp->ifa_addr,
891 curp->ifa_addr->sa_family,
897 curp = curp->ifa_next;
906 static int expand_ipv6(char *addr, int type)
909 struct in6_addr v6_addr;
911 ret = inet_pton(AF_INET6, addr, &v6_addr);
919 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
920 "%02x%02x:%02x%02x:%02x%02x",
921 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
922 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
923 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
924 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
925 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
926 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
927 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
928 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
934 static int is_ipv4(char *addr)
937 struct in_addr ipv4_addr;
939 ret = inet_pton(AF_INET, addr, &ipv4_addr);
946 static int parse_ip_val_buffer(char *in_buf, int *offset,
947 char *out_buf, int out_len)
953 * in_buf has sequence of characters that are seperated by
954 * the character ';'. The last sequence does not have the
955 * terminating ";" character.
957 start = in_buf + *offset;
959 x = strchr(start, ';');
963 x = start + strlen(start);
965 if (strlen(start) != 0) {
968 * Get rid of leading spaces.
970 while (start[i] == ' ')
973 if ((x - start) <= out_len) {
974 strcpy(out_buf, (start + i));
975 *offset += (x - start) + 1;
982 static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
986 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
995 static int process_ip_string(FILE *f, char *ip_string, int type)
998 char addr[INET6_ADDRSTRLEN];
1005 memset(addr, 0, sizeof(addr));
1007 while (parse_ip_val_buffer(ip_string, &offset, addr,
1008 (MAX_IP_ADDR_SIZE * 2))) {
1011 if (is_ipv4(addr)) {
1014 snprintf(str, sizeof(str), "%s", "IPADDR");
1017 snprintf(str, sizeof(str), "%s", "NETMASK");
1020 snprintf(str, sizeof(str), "%s", "GATEWAY");
1023 snprintf(str, sizeof(str), "%s", "DNS");
1028 snprintf(sub_str, sizeof(sub_str),
1031 snprintf(sub_str, sizeof(sub_str),
1034 } else if (type == DNS) {
1035 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1039 } else if (expand_ipv6(addr, type)) {
1042 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1045 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1048 snprintf(str, sizeof(str), "%s",
1052 snprintf(str, sizeof(str), "%s", "DNS");
1055 if ((j != 0) || (type == DNS)) {
1057 snprintf(sub_str, sizeof(sub_str),
1060 snprintf(sub_str, sizeof(sub_str),
1063 } else if (type == DNS) {
1064 snprintf(sub_str, sizeof(sub_str),
1068 return HV_INVALIDARG;
1071 error = kvp_write_file(f, str, sub_str, addr);
1074 memset(addr, 0, sizeof(addr));
1080 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1089 * Set the configuration for the specified interface with
1090 * the information provided. Since there is no standard
1091 * way to configure an interface, we will have an external
1092 * script that does the job of configuring the interface and
1093 * flushing the configuration.
1095 * The parameters passed to this external script are:
1096 * 1. A configuration file that has the specified configuration.
1098 * We will embed the name of the interface in the configuration
1099 * file: ifcfg-ethx (where ethx is the interface name).
1101 * The information provided here may be more than what is needed
1102 * in a given distro to configure the interface and so are free
1103 * ignore information that may not be relevant.
1105 * Here is the format of the ip configuration file:
1108 * IF_NAME=interface name
1109 * DHCP=yes (This is optional; if yes, DHCP is configured)
1113 * IPADDR_x=ipaddry (where y = x + 1)
1116 * NETMASK_x=netmasky (where y = x + 1)
1119 * GATEWAY_x=ipaddry (where y = x + 1)
1121 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1123 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1124 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1127 * The host can specify multiple ipv4 and ipv6 addresses to be
1128 * configured for the interface. Furthermore, the configuration
1129 * needs to be persistent. A subsequent GET call on the interface
1130 * is expected to return the configuration that is set via the SET
1134 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1135 "hyperv/ifcfg-", if_name);
1137 file = fopen(if_file, "w");
1140 syslog(LOG_ERR, "Failed to open config file");
1145 * First write out the MAC address.
1148 mac_addr = kvp_if_name_to_mac(if_name);
1149 if (mac_addr == NULL) {
1154 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1158 error = kvp_write_file(file, "IF_NAME", "", if_name);
1162 if (new_val->dhcp_enabled) {
1163 error = kvp_write_file(file, "DHCP", "", "yes");
1174 * Write the configuration for ipaddress, netmask, gateway and
1178 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1182 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1186 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1190 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1199 * Now that we have populated the configuration file,
1200 * invoke the external script to do its magic.
1203 snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1208 syslog(LOG_ERR, "Failed to write config file");
1216 kvp_get_domain_name(char *buffer, int length)
1218 struct addrinfo hints, *info ;
1221 gethostname(buffer, length);
1222 memset(&hints, 0, sizeof(hints));
1223 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1224 hints.ai_socktype = SOCK_STREAM;
1225 hints.ai_flags = AI_CANONNAME;
1227 error = getaddrinfo(buffer, NULL, &hints, &info);
1229 strcpy(buffer, "getaddrinfo failed\n");
1232 strcpy(buffer, info->ai_canonname);
1238 netlink_send(int fd, struct cn_msg *msg)
1240 struct nlmsghdr *nlh;
1242 struct msghdr message;
1244 struct iovec iov[2];
1246 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
1248 nlh = (struct nlmsghdr *)buffer;
1250 nlh->nlmsg_pid = getpid();
1251 nlh->nlmsg_type = NLMSG_DONE;
1252 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
1253 nlh->nlmsg_flags = 0;
1255 iov[0].iov_base = nlh;
1256 iov[0].iov_len = sizeof(*nlh);
1258 iov[1].iov_base = msg;
1259 iov[1].iov_len = size;
1261 memset(&message, 0, sizeof(message));
1262 message.msg_name = &addr;
1263 message.msg_namelen = sizeof(addr);
1264 message.msg_iov = iov;
1265 message.msg_iovlen = 2;
1267 return sendmsg(fd, &message, 0);
1272 int fd, len, sock_opt;
1274 struct cn_msg *message;
1276 struct nlmsghdr *incoming_msg;
1277 struct cn_msg *incoming_cn_msg;
1278 struct hv_kvp_msg *hv_msg;
1285 struct hv_kvp_ipaddr_value *kvp_ip_val;
1288 openlog("KVP", 0, LOG_USER);
1289 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1291 * Retrieve OS release information.
1295 if (kvp_file_init()) {
1296 syslog(LOG_ERR, "Failed to initialize the pools");
1300 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
1302 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
1305 addr.nl_family = AF_NETLINK;
1308 addr.nl_groups = CN_KVP_IDX;
1311 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
1313 syslog(LOG_ERR, "bind failed; error:%d", error);
1317 sock_opt = addr.nl_groups;
1318 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
1320 * Register ourselves with the kernel.
1322 message = (struct cn_msg *)kvp_send_buffer;
1323 message->id.idx = CN_KVP_IDX;
1324 message->id.val = CN_KVP_VAL;
1326 hv_msg = (struct hv_kvp_msg *)message->data;
1327 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
1329 message->len = sizeof(struct hv_kvp_msg);
1331 len = netlink_send(fd, message);
1333 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
1341 struct sockaddr *addr_p = (struct sockaddr *) &addr;
1342 socklen_t addr_l = sizeof(addr);
1343 pfd.events = POLLIN;
1347 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
1350 if (len < 0 || addr.nl_pid) {
1351 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
1352 addr.nl_pid, errno, strerror(errno));
1357 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
1358 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
1359 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1362 * We will use the KVP header information to pass back
1363 * the error from this daemon. So, first copy the state
1364 * and set the error code to success.
1366 op = hv_msg->kvp_hdr.operation;
1367 pool = hv_msg->kvp_hdr.pool;
1368 hv_msg->error = HV_S_OK;
1370 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
1372 * Driver is registering with us; stash away the version
1376 p = (char *)hv_msg->body.kvp_register.version;
1377 lic_version = malloc(strlen(p) + 1);
1379 strcpy(lic_version, p);
1380 syslog(LOG_INFO, "KVP LIC Version: %s",
1383 syslog(LOG_ERR, "malloc failed");
1389 case KVP_OP_SET_IP_INFO:
1390 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1391 if_name = kvp_get_if_name(
1392 (char *)kvp_ip_val->adapter_id);
1393 if (if_name == NULL) {
1395 * We could not map the guid to an
1396 * interface name; return error.
1398 hv_msg->error = HV_GUID_NOTFOUND;
1401 error = kvp_set_ip_info(if_name, kvp_ip_val);
1403 hv_msg->error = error;
1409 if (kvp_key_add_or_modify(pool,
1410 hv_msg->body.kvp_set.data.key,
1411 hv_msg->body.kvp_set.data.key_size,
1412 hv_msg->body.kvp_set.data.value,
1413 hv_msg->body.kvp_set.data.value_size))
1414 hv_msg->error = HV_S_CONT;
1418 if (kvp_get_value(pool,
1419 hv_msg->body.kvp_set.data.key,
1420 hv_msg->body.kvp_set.data.key_size,
1421 hv_msg->body.kvp_set.data.value,
1422 hv_msg->body.kvp_set.data.value_size))
1423 hv_msg->error = HV_S_CONT;
1427 if (kvp_key_delete(pool,
1428 hv_msg->body.kvp_delete.key,
1429 hv_msg->body.kvp_delete.key_size))
1430 hv_msg->error = HV_S_CONT;
1437 if (op != KVP_OP_ENUMERATE)
1441 * If the pool is KVP_POOL_AUTO, dynamically generate
1442 * both the key and the value; if not read from the
1445 if (pool != KVP_POOL_AUTO) {
1446 if (kvp_pool_enumerate(pool,
1447 hv_msg->body.kvp_enum_data.index,
1448 hv_msg->body.kvp_enum_data.data.key,
1449 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1450 hv_msg->body.kvp_enum_data.data.value,
1451 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1452 hv_msg->error = HV_S_CONT;
1456 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1457 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1458 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
1460 switch (hv_msg->body.kvp_enum_data.index) {
1461 case FullyQualifiedDomainName:
1462 kvp_get_domain_name(key_value,
1463 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1464 strcpy(key_name, "FullyQualifiedDomainName");
1466 case IntegrationServicesVersion:
1467 strcpy(key_name, "IntegrationServicesVersion");
1468 strcpy(key_value, lic_version);
1470 case NetworkAddressIPv4:
1471 kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
1472 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1473 strcpy(key_name, "NetworkAddressIPv4");
1475 case NetworkAddressIPv6:
1476 kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
1477 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1478 strcpy(key_name, "NetworkAddressIPv6");
1481 strcpy(key_value, os_build);
1482 strcpy(key_name, "OSBuildNumber");
1485 strcpy(key_value, os_name);
1486 strcpy(key_name, "OSName");
1488 case OSMajorVersion:
1489 strcpy(key_value, os_major);
1490 strcpy(key_name, "OSMajorVersion");
1492 case OSMinorVersion:
1493 strcpy(key_value, os_minor);
1494 strcpy(key_name, "OSMinorVersion");
1497 strcpy(key_value, os_build);
1498 strcpy(key_name, "OSVersion");
1500 case ProcessorArchitecture:
1501 strcpy(key_value, processor_arch);
1502 strcpy(key_name, "ProcessorArchitecture");
1505 hv_msg->error = HV_S_CONT;
1509 * Send the value back to the kernel. The response is
1510 * already in the receive buffer. Update the cn_msg header to
1511 * reflect the key value that has been added to the message
1515 incoming_cn_msg->id.idx = CN_KVP_IDX;
1516 incoming_cn_msg->id.val = CN_KVP_VAL;
1517 incoming_cn_msg->ack = 0;
1518 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
1520 len = netlink_send(fd, incoming_cn_msg);
1522 syslog(LOG_ERR, "net_link send failed; error:%d", len);