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>
35 #include <arpa/inet.h>
36 #include <linux/connector.h>
37 #include <linux/hyperv.h>
38 #include <linux/netlink.h>
46 * KVP protocol: The user mode component first registers with the
47 * the kernel component. Subsequently, the kernel component requests, data
48 * for the specified keys. In response to this message the user mode component
49 * fills in the value corresponding to the specified key. We overload the
50 * sequence field in the cn_msg header to define our KVP message types.
52 * We use this infrastructure for also supporting queries from user mode
53 * application for state that may be maintained in the KVP kernel component.
59 FullyQualifiedDomainName = 0,
60 IntegrationServicesVersion, /*This key is serviced in the kernel*/
71 static char kvp_send_buffer[4096];
72 static char kvp_recv_buffer[4096 * 2];
73 static struct sockaddr_nl addr;
74 static int in_hand_shake = 1;
76 static char *os_name = "";
77 static char *os_major = "";
78 static char *os_minor = "";
79 static char *processor_arch;
80 static char *os_build;
81 static char *lic_version = "Unknown version";
82 static struct utsname uts_buf;
85 #define MAX_FILE_NAME 100
86 #define ENTRIES_PER_BLOCK 50
89 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
90 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
93 struct kvp_file_state {
96 struct kvp_record *records;
98 char fname[MAX_FILE_NAME];
101 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
103 static void kvp_acquire_lock(int pool)
105 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
108 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
109 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
114 static void kvp_release_lock(int pool)
116 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
119 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
121 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
126 static void kvp_update_file(int pool)
129 size_t bytes_written;
132 * We are going to write our in-memory registry out to
133 * disk; acquire the lock first.
135 kvp_acquire_lock(pool);
137 filep = fopen(kvp_file_info[pool].fname, "w");
139 kvp_release_lock(pool);
140 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
144 bytes_written = fwrite(kvp_file_info[pool].records,
145 sizeof(struct kvp_record),
146 kvp_file_info[pool].num_records, filep);
149 kvp_release_lock(pool);
152 static void kvp_update_mem_state(int pool)
155 size_t records_read = 0;
156 struct kvp_record *record = kvp_file_info[pool].records;
157 struct kvp_record *readp;
158 int num_blocks = kvp_file_info[pool].num_blocks;
159 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
161 kvp_acquire_lock(pool);
163 filep = fopen(kvp_file_info[pool].fname, "r");
165 kvp_release_lock(pool);
166 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
169 while (!feof(filep)) {
170 readp = &record[records_read];
171 records_read += fread(readp, sizeof(struct kvp_record),
172 ENTRIES_PER_BLOCK * num_blocks,
177 * We have more data to read.
180 record = realloc(record, alloc_unit * num_blocks);
182 if (record == NULL) {
183 syslog(LOG_ERR, "malloc failed");
191 kvp_file_info[pool].num_blocks = num_blocks;
192 kvp_file_info[pool].records = record;
193 kvp_file_info[pool].num_records = records_read;
195 kvp_release_lock(pool);
197 static int kvp_file_init(void)
203 struct kvp_record *record;
204 struct kvp_record *readp;
207 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
209 if (access("/var/opt/hyperv", F_OK)) {
210 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
211 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
216 for (i = 0; i < KVP_POOL_COUNT; i++) {
217 fname = kvp_file_info[i].fname;
220 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
221 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
227 filep = fopen(fname, "r");
231 record = malloc(alloc_unit * num_blocks);
232 if (record == NULL) {
236 while (!feof(filep)) {
237 readp = &record[records_read];
238 records_read += fread(readp, sizeof(struct kvp_record),
244 * We have more data to read.
247 record = realloc(record, alloc_unit *
249 if (record == NULL) {
257 kvp_file_info[i].fd = fd;
258 kvp_file_info[i].num_blocks = num_blocks;
259 kvp_file_info[i].records = record;
260 kvp_file_info[i].num_records = records_read;
268 static int kvp_key_delete(int pool, __u8 *key, int key_size)
273 struct kvp_record *record;
276 * First update the in-memory state.
278 kvp_update_mem_state(pool);
280 num_records = kvp_file_info[pool].num_records;
281 record = kvp_file_info[pool].records;
283 for (i = 0; i < num_records; i++) {
284 if (memcmp(key, record[i].key, key_size))
287 * Found a match; just move the remaining
290 if (i == num_records) {
291 kvp_file_info[pool].num_records--;
292 kvp_update_file(pool);
298 for (; k < num_records; k++) {
299 strcpy(record[j].key, record[k].key);
300 strcpy(record[j].value, record[k].value);
304 kvp_file_info[pool].num_records--;
305 kvp_update_file(pool);
311 static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
316 struct kvp_record *record;
319 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
320 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
324 * First update the in-memory state.
326 kvp_update_mem_state(pool);
328 num_records = kvp_file_info[pool].num_records;
329 record = kvp_file_info[pool].records;
330 num_blocks = kvp_file_info[pool].num_blocks;
332 for (i = 0; i < num_records; i++) {
333 if (memcmp(key, record[i].key, key_size))
336 * Found a match; just update the value -
337 * this is the modify case.
339 memcpy(record[i].value, value, value_size);
340 kvp_update_file(pool);
345 * Need to add a new entry;
347 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
348 /* Need to allocate a larger array for reg entries. */
349 record = realloc(record, sizeof(struct kvp_record) *
350 ENTRIES_PER_BLOCK * (num_blocks + 1));
354 kvp_file_info[pool].num_blocks++;
357 memcpy(record[i].value, value, value_size);
358 memcpy(record[i].key, key, key_size);
359 kvp_file_info[pool].records = record;
360 kvp_file_info[pool].num_records++;
361 kvp_update_file(pool);
365 static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
370 struct kvp_record *record;
372 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
373 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
377 * First update the in-memory state.
379 kvp_update_mem_state(pool);
381 num_records = kvp_file_info[pool].num_records;
382 record = kvp_file_info[pool].records;
384 for (i = 0; i < num_records; i++) {
385 if (memcmp(key, record[i].key, key_size))
388 * Found a match; just copy the value out.
390 memcpy(value, record[i].value, value_size);
397 static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
398 __u8 *value, int value_size)
400 struct kvp_record *record;
403 * First update our in-memory database.
405 kvp_update_mem_state(pool);
406 record = kvp_file_info[pool].records;
408 if (index >= kvp_file_info[pool].num_records) {
412 memcpy(key, record[index].key, key_size);
413 memcpy(value, record[index].value, value_size);
418 void kvp_get_os_info(void)
424 os_build = uts_buf.release;
425 processor_arch = uts_buf.machine;
428 * The current windows host (win7) expects the build
429 * string to be of the form: x.y.z
430 * Strip additional information we may have.
432 p = strchr(os_build, '-');
436 file = fopen("/etc/SuSE-release", "r");
438 goto kvp_osinfo_found;
439 file = fopen("/etc/redhat-release", "r");
441 goto kvp_osinfo_found;
443 * Add code for other supported platforms.
447 * We don't have information about the os.
449 os_name = uts_buf.sysname;
453 /* up to three lines */
454 p = fgets(buf, sizeof(buf), file);
456 p = strchr(buf, '\n');
465 p = fgets(buf, sizeof(buf), file);
467 p = strchr(buf, '\n');
476 p = fgets(buf, sizeof(buf), file);
478 p = strchr(buf, '\n');
493 static void kvp_process_ipconfig_file(char *cmd,
494 char *config_buf, int len,
495 int element_size, int offset)
503 * First execute the command.
505 file = popen(cmd, "r");
510 memset(config_buf, 0, len);
511 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
512 if ((len - strlen(config_buf)) < (element_size + 1))
517 strcat(config_buf, p);
518 strcat(config_buf, ";");
523 static void kvp_get_ipconfig_info(char *if_name,
524 struct hv_kvp_ipaddr_value *buffer)
529 * Get the address of default gateway (ipv4).
531 sprintf(cmd, "%s %s", "ip route show dev", if_name);
532 strcat(cmd, " | awk '/default/ {print $3 }'");
535 * Execute the command to gather gateway info.
537 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
538 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
541 * Get the address of default gateway (ipv6).
543 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
544 strcat(cmd, " | awk '/default/ {print $3 }'");
547 * Execute the command to gather gateway info (ipv6).
549 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
550 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
554 * Gather the DNS state.
555 * Since there is no standard way to get this information
556 * across various distributions of interest; we just invoke
557 * an external script that needs to be ported across distros
560 * Following is the expected format of the information from the script:
562 * ipaddr1 (nameserver1)
563 * ipaddr2 (nameserver2)
568 sprintf(cmd, "%s", "hv_get_dns_info");
571 * Execute the command to gather DNS info.
573 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
574 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
578 static unsigned int hweight32(unsigned int *w)
580 unsigned int res = *w - ((*w >> 1) & 0x55555555);
581 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
582 res = (res + (res >> 4)) & 0x0F0F0F0F;
583 res = res + (res >> 8);
584 return (res + (res >> 16)) & 0x000000FF;
587 static int kvp_process_ip_address(void *addrp,
588 int family, char *buffer,
589 int length, int *offset)
591 struct sockaddr_in *addr;
592 struct sockaddr_in6 *addr6;
597 if (family == AF_INET) {
598 addr = (struct sockaddr_in *)addrp;
599 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
600 addr_length = INET_ADDRSTRLEN;
602 addr6 = (struct sockaddr_in6 *)addrp;
603 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
604 addr_length = INET6_ADDRSTRLEN;
607 if ((length - *offset) < addr_length + 1)
610 strcpy(buffer, "inet_ntop failed\n");
619 *offset += strlen(str) + 1;
624 kvp_get_ip_address(int family, char *if_name, int op,
625 void *out_buffer, int length)
627 struct ifaddrs *ifap;
628 struct ifaddrs *curp;
633 struct hv_kvp_ipaddr_value *ip_buffer;
634 char cidr_mask[5]; /* /xyz */
639 struct sockaddr_in6 *addr6;
641 if (op == KVP_OP_ENUMERATE) {
644 ip_buffer = out_buffer;
645 buffer = (char *)ip_buffer->ip_addr;
646 ip_buffer->addr_family = 0;
649 * On entry into this function, the buffer is capable of holding the
653 if (getifaddrs(&ifap)) {
654 strcpy(buffer, "getifaddrs failed\n");
659 while (curp != NULL) {
660 if (curp->ifa_addr == NULL) {
661 curp = curp->ifa_next;
665 if ((if_name != NULL) &&
666 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
668 * We want info about a specific interface;
671 curp = curp->ifa_next;
676 * We only support two address families: AF_INET and AF_INET6.
677 * If a family value of 0 is specified, we collect both
678 * supported address families; if not we gather info on
679 * the specified address family.
681 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
682 curp = curp->ifa_next;
685 if ((curp->ifa_addr->sa_family != AF_INET) &&
686 (curp->ifa_addr->sa_family != AF_INET6)) {
687 curp = curp->ifa_next;
691 if (op == KVP_OP_GET_IP_INFO) {
693 * Gather info other than the IP address.
694 * IP address info will be gathered later.
696 if (curp->ifa_addr->sa_family == AF_INET) {
697 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
701 error = kvp_process_ip_address(
711 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
714 * Get subnet info in CIDR format.
717 sn_str = (char *)ip_buffer->sub_net;
718 addr6 = (struct sockaddr_in6 *)
720 w = addr6->sin6_addr.s6_addr32;
722 for (i = 0; i < 4; i++)
723 weight += hweight32(&w[i]);
725 sprintf(cidr_mask, "/%d", weight);
726 if ((length - sn_offset) <
727 (strlen(cidr_mask) + 1))
731 strcpy(sn_str, cidr_mask);
733 strcat(sn_str, cidr_mask);
734 strcat((char *)ip_buffer->sub_net, ";");
735 sn_offset += strlen(sn_str) + 1;
739 * Collect other ip related configuration info.
742 kvp_get_ipconfig_info(if_name, ip_buffer);
746 error = kvp_process_ip_address(curp->ifa_addr,
747 curp->ifa_addr->sa_family,
753 curp = curp->ifa_next;
763 kvp_get_domain_name(char *buffer, int length)
765 struct addrinfo hints, *info ;
768 gethostname(buffer, length);
769 memset(&hints, 0, sizeof(hints));
770 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
771 hints.ai_socktype = SOCK_STREAM;
772 hints.ai_flags = AI_CANONNAME;
774 error = getaddrinfo(buffer, NULL, &hints, &info);
776 strcpy(buffer, "getaddrinfo failed\n");
779 strcpy(buffer, info->ai_canonname);
785 netlink_send(int fd, struct cn_msg *msg)
787 struct nlmsghdr *nlh;
789 struct msghdr message;
793 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
795 nlh = (struct nlmsghdr *)buffer;
797 nlh->nlmsg_pid = getpid();
798 nlh->nlmsg_type = NLMSG_DONE;
799 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
800 nlh->nlmsg_flags = 0;
802 iov[0].iov_base = nlh;
803 iov[0].iov_len = sizeof(*nlh);
805 iov[1].iov_base = msg;
806 iov[1].iov_len = size;
808 memset(&message, 0, sizeof(message));
809 message.msg_name = &addr;
810 message.msg_namelen = sizeof(addr);
811 message.msg_iov = iov;
812 message.msg_iovlen = 2;
814 return sendmsg(fd, &message, 0);
819 int fd, len, sock_opt;
821 struct cn_msg *message;
823 struct nlmsghdr *incoming_msg;
824 struct cn_msg *incoming_cn_msg;
825 struct hv_kvp_msg *hv_msg;
833 openlog("KVP", 0, LOG_USER);
834 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
836 * Retrieve OS release information.
840 if (kvp_file_init()) {
841 syslog(LOG_ERR, "Failed to initialize the pools");
845 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
847 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
850 addr.nl_family = AF_NETLINK;
853 addr.nl_groups = CN_KVP_IDX;
856 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
858 syslog(LOG_ERR, "bind failed; error:%d", error);
862 sock_opt = addr.nl_groups;
863 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
865 * Register ourselves with the kernel.
867 message = (struct cn_msg *)kvp_send_buffer;
868 message->id.idx = CN_KVP_IDX;
869 message->id.val = CN_KVP_VAL;
871 hv_msg = (struct hv_kvp_msg *)message->data;
872 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
874 message->len = sizeof(struct hv_kvp_msg);
876 len = netlink_send(fd, message);
878 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
886 struct sockaddr *addr_p = (struct sockaddr *) &addr;
887 socklen_t addr_l = sizeof(addr);
892 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
895 if (len < 0 || addr.nl_pid) {
896 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
897 addr.nl_pid, errno, strerror(errno));
902 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
903 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
904 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
907 * We will use the KVP header information to pass back
908 * the error from this daemon. So, first copy the state
909 * and set the error code to success.
911 op = hv_msg->kvp_hdr.operation;
912 pool = hv_msg->kvp_hdr.pool;
913 hv_msg->error = HV_S_OK;
915 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
917 * Driver is registering with us; stash away the version
921 p = (char *)hv_msg->body.kvp_register.version;
922 lic_version = malloc(strlen(p) + 1);
924 strcpy(lic_version, p);
925 syslog(LOG_INFO, "KVP LIC Version: %s",
928 syslog(LOG_ERR, "malloc failed");
935 if (kvp_key_add_or_modify(pool,
936 hv_msg->body.kvp_set.data.key,
937 hv_msg->body.kvp_set.data.key_size,
938 hv_msg->body.kvp_set.data.value,
939 hv_msg->body.kvp_set.data.value_size))
940 hv_msg->error = HV_S_CONT;
944 if (kvp_get_value(pool,
945 hv_msg->body.kvp_set.data.key,
946 hv_msg->body.kvp_set.data.key_size,
947 hv_msg->body.kvp_set.data.value,
948 hv_msg->body.kvp_set.data.value_size))
949 hv_msg->error = HV_S_CONT;
953 if (kvp_key_delete(pool,
954 hv_msg->body.kvp_delete.key,
955 hv_msg->body.kvp_delete.key_size))
956 hv_msg->error = HV_S_CONT;
963 if (op != KVP_OP_ENUMERATE)
967 * If the pool is KVP_POOL_AUTO, dynamically generate
968 * both the key and the value; if not read from the
971 if (pool != KVP_POOL_AUTO) {
972 if (kvp_pool_enumerate(pool,
973 hv_msg->body.kvp_enum_data.index,
974 hv_msg->body.kvp_enum_data.data.key,
975 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
976 hv_msg->body.kvp_enum_data.data.value,
977 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
978 hv_msg->error = HV_S_CONT;
982 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
983 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
984 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
986 switch (hv_msg->body.kvp_enum_data.index) {
987 case FullyQualifiedDomainName:
988 kvp_get_domain_name(key_value,
989 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
990 strcpy(key_name, "FullyQualifiedDomainName");
992 case IntegrationServicesVersion:
993 strcpy(key_name, "IntegrationServicesVersion");
994 strcpy(key_value, lic_version);
996 case NetworkAddressIPv4:
997 kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
998 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
999 strcpy(key_name, "NetworkAddressIPv4");
1001 case NetworkAddressIPv6:
1002 kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
1003 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1004 strcpy(key_name, "NetworkAddressIPv6");
1007 strcpy(key_value, os_build);
1008 strcpy(key_name, "OSBuildNumber");
1011 strcpy(key_value, os_name);
1012 strcpy(key_name, "OSName");
1014 case OSMajorVersion:
1015 strcpy(key_value, os_major);
1016 strcpy(key_name, "OSMajorVersion");
1018 case OSMinorVersion:
1019 strcpy(key_value, os_minor);
1020 strcpy(key_name, "OSMinorVersion");
1023 strcpy(key_value, os_build);
1024 strcpy(key_name, "OSVersion");
1026 case ProcessorArchitecture:
1027 strcpy(key_value, processor_arch);
1028 strcpy(key_name, "ProcessorArchitecture");
1031 hv_msg->error = HV_S_CONT;
1035 * Send the value back to the kernel. The response is
1036 * already in the receive buffer. Update the cn_msg header to
1037 * reflect the key value that has been added to the message
1041 incoming_cn_msg->id.idx = CN_KVP_IDX;
1042 incoming_cn_msg->id.val = CN_KVP_VAL;
1043 incoming_cn_msg->ack = 0;
1044 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
1046 len = netlink_send(fd, incoming_cn_msg);
1048 syslog(LOG_ERR, "net_link send failed; error:%d", len);