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>
35 #include <arpa/inet.h>
36 #include <linux/connector.h>
37 #include <linux/hyperv.h>
38 #include <linux/netlink.h>
49 * KVP protocol: The user mode component first registers with the
50 * the kernel component. Subsequently, the kernel component requests, data
51 * for the specified keys. In response to this message the user mode component
52 * fills in the value corresponding to the specified key. We overload the
53 * sequence field in the cn_msg header to define our KVP message types.
55 * We use this infrastructure for also supporting queries from user mode
56 * application for state that may be maintained in the KVP kernel component.
62 FullyQualifiedDomainName = 0,
63 IntegrationServicesVersion, /*This key is serviced in the kernel*/
82 static struct sockaddr_nl addr;
83 static int in_hand_shake = 1;
85 static char *os_name = "";
86 static char *os_major = "";
87 static char *os_minor = "";
88 static char *processor_arch;
89 static char *os_build;
90 static char *os_version;
91 static char *lic_version = "Unknown version";
92 static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
93 static struct utsname uts_buf;
96 * The location of the interface configuration file.
99 #define KVP_CONFIG_LOC "/var/lib/hyperv"
101 #define MAX_FILE_NAME 100
102 #define ENTRIES_PER_BLOCK 50
105 #define SOL_NETLINK 270
109 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
110 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
113 struct kvp_file_state {
116 struct kvp_record *records;
118 char fname[MAX_FILE_NAME];
121 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
123 static void kvp_acquire_lock(int pool)
125 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
128 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
129 syslog(LOG_ERR, "Failed to acquire the lock pool: %d; error: %d %s", pool,
130 errno, strerror(errno));
135 static void kvp_release_lock(int pool)
137 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
140 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
141 syslog(LOG_ERR, "Failed to release the lock pool: %d; error: %d %s", pool,
142 errno, strerror(errno));
147 static void kvp_update_file(int pool)
152 * We are going to write our in-memory registry out to
153 * disk; acquire the lock first.
155 kvp_acquire_lock(pool);
157 filep = fopen(kvp_file_info[pool].fname, "we");
159 syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
160 errno, strerror(errno));
161 kvp_release_lock(pool);
165 fwrite(kvp_file_info[pool].records, sizeof(struct kvp_record),
166 kvp_file_info[pool].num_records, filep);
168 if (ferror(filep) || fclose(filep)) {
169 kvp_release_lock(pool);
170 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
174 kvp_release_lock(pool);
177 static void kvp_update_mem_state(int pool)
180 size_t records_read = 0;
181 struct kvp_record *record = kvp_file_info[pool].records;
182 struct kvp_record *readp;
183 int num_blocks = kvp_file_info[pool].num_blocks;
184 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
186 kvp_acquire_lock(pool);
188 filep = fopen(kvp_file_info[pool].fname, "re");
190 syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool,
191 errno, strerror(errno));
192 kvp_release_lock(pool);
196 readp = &record[records_read];
197 records_read += fread(readp, sizeof(struct kvp_record),
198 ENTRIES_PER_BLOCK * num_blocks,
202 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
208 * We have more data to read.
211 record = realloc(record, alloc_unit * num_blocks);
213 if (record == NULL) {
214 syslog(LOG_ERR, "malloc failed");
222 kvp_file_info[pool].num_blocks = num_blocks;
223 kvp_file_info[pool].records = record;
224 kvp_file_info[pool].num_records = records_read;
227 kvp_release_lock(pool);
229 static int kvp_file_init(void)
235 struct kvp_record *record;
236 struct kvp_record *readp;
239 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
241 if (access(KVP_CONFIG_LOC, F_OK)) {
242 if (mkdir(KVP_CONFIG_LOC, 0755 /* rwxr-xr-x */)) {
243 syslog(LOG_ERR, "Failed to create '%s'; error: %d %s", KVP_CONFIG_LOC,
244 errno, strerror(errno));
249 for (i = 0; i < KVP_POOL_COUNT; i++) {
250 fname = kvp_file_info[i].fname;
253 sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
254 fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
260 filep = fopen(fname, "re");
266 record = malloc(alloc_unit * num_blocks);
267 if (record == NULL) {
273 readp = &record[records_read];
274 records_read += fread(readp, sizeof(struct kvp_record),
279 syslog(LOG_ERR, "Failed to read file, pool: %d",
286 * We have more data to read.
289 record = realloc(record, alloc_unit *
291 if (record == NULL) {
300 kvp_file_info[i].fd = fd;
301 kvp_file_info[i].num_blocks = num_blocks;
302 kvp_file_info[i].records = record;
303 kvp_file_info[i].num_records = records_read;
311 static int kvp_key_delete(int pool, const char *key, int key_size)
316 struct kvp_record *record;
319 * First update the in-memory state.
321 kvp_update_mem_state(pool);
323 num_records = kvp_file_info[pool].num_records;
324 record = kvp_file_info[pool].records;
326 for (i = 0; i < num_records; i++) {
327 if (memcmp(key, record[i].key, key_size))
330 * Found a match; just move the remaining
333 if (i == num_records) {
334 kvp_file_info[pool].num_records--;
335 kvp_update_file(pool);
341 for (; k < num_records; k++) {
342 strcpy(record[j].key, record[k].key);
343 strcpy(record[j].value, record[k].value);
347 kvp_file_info[pool].num_records--;
348 kvp_update_file(pool);
354 static int kvp_key_add_or_modify(int pool, const char *key, int key_size, const char *value,
359 struct kvp_record *record;
362 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
363 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
367 * First update the in-memory state.
369 kvp_update_mem_state(pool);
371 num_records = kvp_file_info[pool].num_records;
372 record = kvp_file_info[pool].records;
373 num_blocks = kvp_file_info[pool].num_blocks;
375 for (i = 0; i < num_records; i++) {
376 if (memcmp(key, record[i].key, key_size))
379 * Found a match; just update the value -
380 * this is the modify case.
382 memcpy(record[i].value, value, value_size);
383 kvp_update_file(pool);
388 * Need to add a new entry;
390 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
391 /* Need to allocate a larger array for reg entries. */
392 record = realloc(record, sizeof(struct kvp_record) *
393 ENTRIES_PER_BLOCK * (num_blocks + 1));
397 kvp_file_info[pool].num_blocks++;
400 memcpy(record[i].value, value, value_size);
401 memcpy(record[i].key, key, key_size);
402 kvp_file_info[pool].records = record;
403 kvp_file_info[pool].num_records++;
404 kvp_update_file(pool);
408 static int kvp_get_value(int pool, const char *key, int key_size, char *value,
413 struct kvp_record *record;
415 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
416 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
420 * First update the in-memory state.
422 kvp_update_mem_state(pool);
424 num_records = kvp_file_info[pool].num_records;
425 record = kvp_file_info[pool].records;
427 for (i = 0; i < num_records; i++) {
428 if (memcmp(key, record[i].key, key_size))
431 * Found a match; just copy the value out.
433 memcpy(value, record[i].value, value_size);
440 static int kvp_pool_enumerate(int pool, int index, char *key, int key_size,
441 char *value, int value_size)
443 struct kvp_record *record;
446 * First update our in-memory database.
448 kvp_update_mem_state(pool);
449 record = kvp_file_info[pool].records;
451 if (index >= kvp_file_info[pool].num_records) {
455 memcpy(key, record[index].key, key_size);
456 memcpy(value, record[index].value, value_size);
461 void kvp_get_os_info(void)
467 os_version = uts_buf.release;
468 os_build = strdup(uts_buf.release);
470 os_name = uts_buf.sysname;
471 processor_arch = uts_buf.machine;
474 * The current windows host (win7) expects the build
475 * string to be of the form: x.y.z
476 * Strip additional information we may have.
478 p = strchr(os_version, '-');
483 * Parse the /etc/os-release file if present:
484 * http://www.freedesktop.org/software/systemd/man/os-release.html
486 file = fopen("/etc/os-release", "r");
488 while (fgets(buf, sizeof(buf), file)) {
491 /* Ignore comments */
495 /* Split into name=value */
496 p = strchr(buf, '=');
501 /* Remove quotes and newline; un-escape */
510 } else if (*p == '\'' || *p == '"' ||
519 if (!strcmp(buf, "NAME")) {
524 } else if (!strcmp(buf, "VERSION_ID")) {
535 /* Fallback for older RH/SUSE releases */
536 file = fopen("/etc/SuSE-release", "r");
538 goto kvp_osinfo_found;
539 file = fopen("/etc/redhat-release", "r");
541 goto kvp_osinfo_found;
544 * We don't have information about the os.
549 /* up to three lines */
550 p = fgets(buf, sizeof(buf), file);
552 p = strchr(buf, '\n');
561 p = fgets(buf, sizeof(buf), file);
563 p = strchr(buf, '\n');
572 p = fgets(buf, sizeof(buf), file);
574 p = strchr(buf, '\n');
592 * Retrieve an interface name corresponding to the specified guid.
593 * If there is a match, the function returns a pointer
594 * to the interface name and if not, a NULL is returned.
595 * If a match is found, the caller is responsible for
596 * freeing the memory.
599 static char *kvp_get_if_name(char *guid)
602 struct dirent *entry;
605 char *if_name = NULL;
607 char *kvp_net_dir = "/sys/class/net/";
610 dir = opendir(kvp_net_dir);
614 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
615 q = dev_id + strlen(kvp_net_dir);
617 while ((entry = readdir(dir)) != NULL) {
619 * Set the state for the next pass.
622 strcat(dev_id, entry->d_name);
623 strcat(dev_id, "/device/device_id");
625 file = fopen(dev_id, "r");
629 p = fgets(buf, sizeof(buf), file);
635 if (!strcmp(p, guid)) {
637 * Found the guid match; return the interface
638 * name. The caller will free the memory.
640 if_name = strdup(entry->d_name);
653 * Retrieve the MAC address given the interface name.
656 static char *kvp_if_name_to_mac(char *if_name)
663 char *mac_addr = NULL;
665 snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
666 if_name, "/address");
668 file = fopen(addr_file, "r");
672 p = fgets(buf, sizeof(buf), file);
677 for (i = 0; i < strlen(p); i++)
678 p[i] = toupper(p[i]);
679 mac_addr = strdup(p);
688 * Retrieve the interface name given tha MAC address.
691 static char *kvp_mac_to_if_name(char *mac)
694 struct dirent *entry;
697 char *if_name = NULL;
699 char *kvp_net_dir = "/sys/class/net/";
703 dir = opendir(kvp_net_dir);
707 snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
708 q = dev_id + strlen(kvp_net_dir);
710 while ((entry = readdir(dir)) != NULL) {
712 * Set the state for the next pass.
716 strcat(dev_id, entry->d_name);
717 strcat(dev_id, "/address");
719 file = fopen(dev_id, "r");
723 p = fgets(buf, sizeof(buf), file);
729 for (i = 0; i < strlen(p); i++)
730 p[i] = toupper(p[i]);
732 if (!strcmp(p, mac)) {
734 * Found the MAC match; return the interface
735 * name. The caller will free the memory.
737 if_name = strdup(entry->d_name);
750 static void kvp_process_ipconfig_file(char *cmd,
751 char *config_buf, int len,
752 int element_size, int offset)
760 * First execute the command.
762 file = popen(cmd, "r");
767 memset(config_buf, 0, len);
768 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
769 if ((len - strlen(config_buf)) < (element_size + 1))
776 strcat(config_buf, p);
777 strcat(config_buf, ";");
782 static void kvp_get_ipconfig_info(char *if_name,
783 struct hv_kvp_ipaddr_value *buffer)
791 * Get the address of default gateway (ipv4).
793 sprintf(cmd, "%s %s", "ip route show dev", if_name);
794 strcat(cmd, " | awk '/default/ {print $3 }'");
797 * Execute the command to gather gateway info.
799 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
800 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
803 * Get the address of default gateway (ipv6).
805 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
806 strcat(cmd, " | awk '/default/ {print $3 }'");
809 * Execute the command to gather gateway info (ipv6).
811 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
812 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
816 * Gather the DNS state.
817 * Since there is no standard way to get this information
818 * across various distributions of interest; we just invoke
819 * an external script that needs to be ported across distros
822 * Following is the expected format of the information from the script:
824 * ipaddr1 (nameserver1)
825 * ipaddr2 (nameserver2)
830 sprintf(cmd, "%s", "hv_get_dns_info");
833 * Execute the command to gather DNS info.
835 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
836 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
839 * Gather the DHCP state.
840 * We will gather this state by invoking an external script.
841 * The parameter to the script is the interface name.
842 * Here is the expected output:
844 * Enabled: DHCP enabled.
847 sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
849 file = popen(cmd, "r");
853 p = fgets(dhcp_info, sizeof(dhcp_info), file);
859 if (!strncmp(p, "Enabled", 7))
860 buffer->dhcp_enabled = 1;
862 buffer->dhcp_enabled = 0;
868 static unsigned int hweight32(unsigned int *w)
870 unsigned int res = *w - ((*w >> 1) & 0x55555555);
871 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
872 res = (res + (res >> 4)) & 0x0F0F0F0F;
873 res = res + (res >> 8);
874 return (res + (res >> 16)) & 0x000000FF;
877 static int kvp_process_ip_address(void *addrp,
878 int family, char *buffer,
879 int length, int *offset)
881 struct sockaddr_in *addr;
882 struct sockaddr_in6 *addr6;
887 if (family == AF_INET) {
888 addr = (struct sockaddr_in *)addrp;
889 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
890 addr_length = INET_ADDRSTRLEN;
892 addr6 = (struct sockaddr_in6 *)addrp;
893 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
894 addr_length = INET6_ADDRSTRLEN;
897 if ((length - *offset) < addr_length + 2)
900 strcpy(buffer, "inet_ntop failed\n");
910 *offset += strlen(str) + 1;
916 kvp_get_ip_info(int family, char *if_name, int op,
917 void *out_buffer, int length)
919 struct ifaddrs *ifap;
920 struct ifaddrs *curp;
925 struct hv_kvp_ipaddr_value *ip_buffer;
926 char cidr_mask[5]; /* /xyz */
931 struct sockaddr_in6 *addr6;
933 if (op == KVP_OP_ENUMERATE) {
936 ip_buffer = out_buffer;
937 buffer = (char *)ip_buffer->ip_addr;
938 ip_buffer->addr_family = 0;
941 * On entry into this function, the buffer is capable of holding the
945 if (getifaddrs(&ifap)) {
946 strcpy(buffer, "getifaddrs failed\n");
951 while (curp != NULL) {
952 if (curp->ifa_addr == NULL) {
953 curp = curp->ifa_next;
957 if ((if_name != NULL) &&
958 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
960 * We want info about a specific interface;
963 curp = curp->ifa_next;
968 * We only support two address families: AF_INET and AF_INET6.
969 * If a family value of 0 is specified, we collect both
970 * supported address families; if not we gather info on
971 * the specified address family.
973 if ((((family != 0) &&
974 (curp->ifa_addr->sa_family != family))) ||
975 (curp->ifa_flags & IFF_LOOPBACK)) {
976 curp = curp->ifa_next;
979 if ((curp->ifa_addr->sa_family != AF_INET) &&
980 (curp->ifa_addr->sa_family != AF_INET6)) {
981 curp = curp->ifa_next;
985 if (op == KVP_OP_GET_IP_INFO) {
987 * Gather info other than the IP address.
988 * IP address info will be gathered later.
990 if (curp->ifa_addr->sa_family == AF_INET) {
991 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
995 error = kvp_process_ip_address(
1005 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
1008 * Get subnet info in CIDR format.
1011 sn_str = (char *)ip_buffer->sub_net;
1012 addr6 = (struct sockaddr_in6 *)
1014 w = addr6->sin6_addr.s6_addr32;
1016 for (i = 0; i < 4; i++)
1017 weight += hweight32(&w[i]);
1019 sprintf(cidr_mask, "/%d", weight);
1020 if ((length - sn_offset) <
1021 (strlen(cidr_mask) + 1))
1025 strcpy(sn_str, cidr_mask);
1027 strcat((char *)ip_buffer->sub_net, ";");
1028 strcat(sn_str, cidr_mask);
1030 sn_offset += strlen(sn_str) + 1;
1034 * Collect other ip related configuration info.
1037 kvp_get_ipconfig_info(if_name, ip_buffer);
1041 error = kvp_process_ip_address(curp->ifa_addr,
1042 curp->ifa_addr->sa_family,
1048 curp = curp->ifa_next;
1057 static int expand_ipv6(char *addr, int type)
1060 struct in6_addr v6_addr;
1062 ret = inet_pton(AF_INET6, addr, &v6_addr);
1065 if (type == NETMASK)
1070 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1071 "%02x%02x:%02x%02x:%02x%02x",
1072 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
1073 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
1074 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
1075 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
1076 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
1077 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
1078 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
1079 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
1085 static int is_ipv4(char *addr)
1088 struct in_addr ipv4_addr;
1090 ret = inet_pton(AF_INET, addr, &ipv4_addr);
1097 static int parse_ip_val_buffer(char *in_buf, int *offset,
1098 char *out_buf, int out_len)
1104 * in_buf has sequence of characters that are seperated by
1105 * the character ';'. The last sequence does not have the
1106 * terminating ";" character.
1108 start = in_buf + *offset;
1110 x = strchr(start, ';');
1114 x = start + strlen(start);
1116 if (strlen(start) != 0) {
1119 * Get rid of leading spaces.
1121 while (start[i] == ' ')
1124 if ((x - start) <= out_len) {
1125 strcpy(out_buf, (start + i));
1126 *offset += (x - start) + 1;
1133 static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
1137 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
1146 static int process_ip_string(FILE *f, char *ip_string, int type)
1149 char addr[INET6_ADDRSTRLEN];
1156 memset(addr, 0, sizeof(addr));
1158 while (parse_ip_val_buffer(ip_string, &offset, addr,
1159 (MAX_IP_ADDR_SIZE * 2))) {
1162 if (is_ipv4(addr)) {
1165 snprintf(str, sizeof(str), "%s", "IPADDR");
1168 snprintf(str, sizeof(str), "%s", "NETMASK");
1171 snprintf(str, sizeof(str), "%s", "GATEWAY");
1174 snprintf(str, sizeof(str), "%s", "DNS");
1179 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1180 } else if (type == GATEWAY && i == 0) {
1183 snprintf(sub_str, sizeof(sub_str), "%d", i++);
1187 } else if (expand_ipv6(addr, type)) {
1190 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1193 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1196 snprintf(str, sizeof(str), "%s",
1200 snprintf(str, sizeof(str), "%s", "DNS");
1205 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1206 } else if (j == 0) {
1209 snprintf(sub_str, sizeof(sub_str), "_%d", j++);
1212 return HV_INVALIDARG;
1215 error = kvp_write_file(f, str, sub_str, addr);
1218 memset(addr, 0, sizeof(addr));
1224 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1233 * Set the configuration for the specified interface with
1234 * the information provided. Since there is no standard
1235 * way to configure an interface, we will have an external
1236 * script that does the job of configuring the interface and
1237 * flushing the configuration.
1239 * The parameters passed to this external script are:
1240 * 1. A configuration file that has the specified configuration.
1242 * We will embed the name of the interface in the configuration
1243 * file: ifcfg-ethx (where ethx is the interface name).
1245 * The information provided here may be more than what is needed
1246 * in a given distro to configure the interface and so are free
1247 * ignore information that may not be relevant.
1249 * Here is the format of the ip configuration file:
1252 * DEVICE=interface name
1253 * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
1254 * or "none" if no boot-time protocol should be used)
1258 * IPADDRx=ipaddry (where y = x + 1)
1261 * NETMASKx=netmasky (where y = x + 1)
1264 * GATEWAYx=ipaddry (where y = x + 1)
1266 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1268 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1269 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1272 * The host can specify multiple ipv4 and ipv6 addresses to be
1273 * configured for the interface. Furthermore, the configuration
1274 * needs to be persistent. A subsequent GET call on the interface
1275 * is expected to return the configuration that is set via the SET
1279 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1280 "/ifcfg-", if_name);
1282 file = fopen(if_file, "w");
1285 syslog(LOG_ERR, "Failed to open config file; error: %d %s",
1286 errno, strerror(errno));
1291 * First write out the MAC address.
1294 mac_addr = kvp_if_name_to_mac(if_name);
1295 if (mac_addr == NULL) {
1300 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1305 error = kvp_write_file(file, "DEVICE", "", if_name);
1310 * The dhcp_enabled flag is only for IPv4. In the case the host only
1311 * injects an IPv6 address, the flag is true, but we still need to
1312 * proceed to parse and pass the IPv6 information to the
1313 * disto-specific script hv_set_ifconfig.
1315 if (new_val->dhcp_enabled) {
1316 error = kvp_write_file(file, "BOOTPROTO", "", "dhcp");
1321 error = kvp_write_file(file, "BOOTPROTO", "", "none");
1327 * Write the configuration for ipaddress, netmask, gateway and
1331 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1335 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1339 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1343 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1350 * Now that we have populated the configuration file,
1351 * invoke the external script to do its magic.
1354 snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1356 syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
1357 cmd, errno, strerror(errno));
1363 syslog(LOG_ERR, "Failed to write config file");
1370 kvp_get_domain_name(char *buffer, int length)
1372 struct addrinfo hints, *info ;
1375 gethostname(buffer, length);
1376 memset(&hints, 0, sizeof(hints));
1377 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1378 hints.ai_socktype = SOCK_STREAM;
1379 hints.ai_flags = AI_CANONNAME;
1381 error = getaddrinfo(buffer, NULL, &hints, &info);
1383 snprintf(buffer, length, "getaddrinfo failed: 0x%x %s",
1384 error, gai_strerror(error));
1387 snprintf(buffer, length, "%s", info->ai_canonname);
1392 netlink_send(int fd, struct cn_msg *msg)
1394 struct nlmsghdr nlh = { .nlmsg_type = NLMSG_DONE };
1396 struct msghdr message;
1397 struct iovec iov[2];
1399 size = sizeof(struct cn_msg) + msg->len;
1401 nlh.nlmsg_pid = getpid();
1402 nlh.nlmsg_len = NLMSG_LENGTH(size);
1404 iov[0].iov_base = &nlh;
1405 iov[0].iov_len = sizeof(nlh);
1407 iov[1].iov_base = msg;
1408 iov[1].iov_len = size;
1410 memset(&message, 0, sizeof(message));
1411 message.msg_name = &addr;
1412 message.msg_namelen = sizeof(addr);
1413 message.msg_iov = iov;
1414 message.msg_iovlen = 2;
1416 return sendmsg(fd, &message, 0);
1419 void print_usage(char *argv[])
1421 fprintf(stderr, "Usage: %s [options]\n"
1423 " -n, --no-daemon stay in foreground, don't daemonize\n"
1424 " -h, --help print this help\n", argv[0]);
1427 int main(int argc, char *argv[])
1429 int fd, len, nl_group;
1431 struct cn_msg *message;
1433 struct nlmsghdr *incoming_msg;
1434 struct cn_msg *incoming_cn_msg;
1435 struct hv_kvp_msg *hv_msg;
1442 struct hv_kvp_ipaddr_value *kvp_ip_val;
1443 char *kvp_recv_buffer;
1444 size_t kvp_recv_buffer_len;
1445 int daemonize = 1, long_index = 0, opt;
1447 static struct option long_options[] = {
1448 {"help", no_argument, 0, 'h' },
1449 {"no-daemon", no_argument, 0, 'n' },
1453 while ((opt = getopt_long(argc, argv, "hn", long_options,
1454 &long_index)) != -1) {
1466 if (daemonize && daemon(1, 0))
1469 openlog("KVP", 0, LOG_USER);
1470 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1472 kvp_recv_buffer_len = NLMSG_LENGTH(0) + sizeof(struct cn_msg) + sizeof(struct hv_kvp_msg);
1473 kvp_recv_buffer = calloc(1, kvp_recv_buffer_len);
1474 if (!kvp_recv_buffer) {
1475 syslog(LOG_ERR, "Failed to allocate netlink buffer");
1479 * Retrieve OS release information.
1483 * Cache Fully Qualified Domain Name because getaddrinfo takes an
1484 * unpredictable amount of time to finish.
1486 kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
1488 if (kvp_file_init()) {
1489 syslog(LOG_ERR, "Failed to initialize the pools");
1493 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
1495 syslog(LOG_ERR, "netlink socket creation failed; error: %d %s", errno,
1499 addr.nl_family = AF_NETLINK;
1505 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
1507 syslog(LOG_ERR, "bind failed; error: %d %s", errno, strerror(errno));
1511 nl_group = CN_KVP_IDX;
1513 if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
1514 syslog(LOG_ERR, "setsockopt failed; error: %d %s", errno, strerror(errno));
1520 * Register ourselves with the kernel.
1522 message = (struct cn_msg *)kvp_recv_buffer;
1523 message->id.idx = CN_KVP_IDX;
1524 message->id.val = CN_KVP_VAL;
1526 hv_msg = (struct hv_kvp_msg *)message->data;
1527 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
1529 message->len = sizeof(struct hv_kvp_msg);
1531 len = netlink_send(fd, message);
1533 syslog(LOG_ERR, "netlink_send failed; error: %d %s", errno, strerror(errno));
1541 struct sockaddr *addr_p = (struct sockaddr *) &addr;
1542 socklen_t addr_l = sizeof(addr);
1543 pfd.events = POLLIN;
1546 if (poll(&pfd, 1, -1) < 0) {
1547 syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
1548 if (errno == EINVAL) {
1556 len = recvfrom(fd, kvp_recv_buffer, kvp_recv_buffer_len, 0,
1560 int saved_errno = errno;
1561 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
1562 addr.nl_pid, errno, strerror(errno));
1564 if (saved_errno == ENOBUFS) {
1565 syslog(LOG_ERR, "receive error: ignored");
1574 syslog(LOG_WARNING, "Received packet from untrusted pid:%u",
1579 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
1581 if (incoming_msg->nlmsg_type != NLMSG_DONE)
1584 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
1585 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1588 * We will use the KVP header information to pass back
1589 * the error from this daemon. So, first copy the state
1590 * and set the error code to success.
1592 op = hv_msg->kvp_hdr.operation;
1593 pool = hv_msg->kvp_hdr.pool;
1594 hv_msg->error = HV_S_OK;
1596 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
1598 * Driver is registering with us; stash away the version
1602 p = (char *)hv_msg->body.kvp_register.version;
1603 lic_version = malloc(strlen(p) + 1);
1605 strcpy(lic_version, p);
1606 syslog(LOG_INFO, "KVP LIC Version: %s",
1609 syslog(LOG_ERR, "malloc failed");
1615 case KVP_OP_GET_IP_INFO:
1616 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1618 kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
1620 if (if_name == NULL) {
1622 * We could not map the mac address to an
1623 * interface name; return error.
1625 hv_msg->error = HV_E_FAIL;
1628 error = kvp_get_ip_info(
1629 0, if_name, KVP_OP_GET_IP_INFO,
1631 (MAX_IP_ADDR_SIZE * 2));
1634 hv_msg->error = error;
1639 case KVP_OP_SET_IP_INFO:
1640 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1641 if_name = kvp_get_if_name(
1642 (char *)kvp_ip_val->adapter_id);
1643 if (if_name == NULL) {
1645 * We could not map the guid to an
1646 * interface name; return error.
1648 hv_msg->error = HV_GUID_NOTFOUND;
1651 error = kvp_set_ip_info(if_name, kvp_ip_val);
1653 hv_msg->error = error;
1659 if (kvp_key_add_or_modify(pool,
1660 hv_msg->body.kvp_set.data.key,
1661 hv_msg->body.kvp_set.data.key_size,
1662 hv_msg->body.kvp_set.data.value,
1663 hv_msg->body.kvp_set.data.value_size))
1664 hv_msg->error = HV_S_CONT;
1668 if (kvp_get_value(pool,
1669 hv_msg->body.kvp_set.data.key,
1670 hv_msg->body.kvp_set.data.key_size,
1671 hv_msg->body.kvp_set.data.value,
1672 hv_msg->body.kvp_set.data.value_size))
1673 hv_msg->error = HV_S_CONT;
1677 if (kvp_key_delete(pool,
1678 hv_msg->body.kvp_delete.key,
1679 hv_msg->body.kvp_delete.key_size))
1680 hv_msg->error = HV_S_CONT;
1687 if (op != KVP_OP_ENUMERATE)
1691 * If the pool is KVP_POOL_AUTO, dynamically generate
1692 * both the key and the value; if not read from the
1695 if (pool != KVP_POOL_AUTO) {
1696 if (kvp_pool_enumerate(pool,
1697 hv_msg->body.kvp_enum_data.index,
1698 hv_msg->body.kvp_enum_data.data.key,
1699 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1700 hv_msg->body.kvp_enum_data.data.value,
1701 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1702 hv_msg->error = HV_S_CONT;
1706 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1707 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1708 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
1710 switch (hv_msg->body.kvp_enum_data.index) {
1711 case FullyQualifiedDomainName:
1712 strcpy(key_value, full_domain_name);
1713 strcpy(key_name, "FullyQualifiedDomainName");
1715 case IntegrationServicesVersion:
1716 strcpy(key_name, "IntegrationServicesVersion");
1717 strcpy(key_value, lic_version);
1719 case NetworkAddressIPv4:
1720 kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
1721 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1722 strcpy(key_name, "NetworkAddressIPv4");
1724 case NetworkAddressIPv6:
1725 kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
1726 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1727 strcpy(key_name, "NetworkAddressIPv6");
1730 strcpy(key_value, os_build);
1731 strcpy(key_name, "OSBuildNumber");
1734 strcpy(key_value, os_name);
1735 strcpy(key_name, "OSName");
1737 case OSMajorVersion:
1738 strcpy(key_value, os_major);
1739 strcpy(key_name, "OSMajorVersion");
1741 case OSMinorVersion:
1742 strcpy(key_value, os_minor);
1743 strcpy(key_name, "OSMinorVersion");
1746 strcpy(key_value, os_version);
1747 strcpy(key_name, "OSVersion");
1749 case ProcessorArchitecture:
1750 strcpy(key_value, processor_arch);
1751 strcpy(key_name, "ProcessorArchitecture");
1754 hv_msg->error = HV_S_CONT;
1758 * Send the value back to the kernel. The response is
1759 * already in the receive buffer. Update the cn_msg header to
1760 * reflect the key value that has been added to the message
1764 incoming_cn_msg->id.idx = CN_KVP_IDX;
1765 incoming_cn_msg->id.val = CN_KVP_VAL;
1766 incoming_cn_msg->ack = 0;
1767 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
1769 len = netlink_send(fd, incoming_cn_msg);
1771 int saved_errno = errno;
1772 syslog(LOG_ERR, "net_link send failed; error: %d %s", errno,
1775 if (saved_errno == ENOMEM || saved_errno == ENOBUFS) {
1776 syslog(LOG_ERR, "send error: ignored");