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.
26 #include <sys/utsname.h>
33 #include <arpa/inet.h>
34 #include <linux/hyperv.h>
46 * KVP protocol: The user mode component first registers with the
47 * 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*/
85 static int in_hand_shake
;
87 static char *os_name
= "";
88 static char *os_major
= "";
89 static char *os_minor
= "";
90 static char *processor_arch
;
91 static char *os_build
;
92 static char *os_version
;
93 static char *lic_version
= "Unknown version";
94 static char full_domain_name
[HV_KVP_EXCHANGE_MAX_VALUE_SIZE
];
95 static struct utsname uts_buf
;
98 * The location of the interface configuration file.
101 #define KVP_CONFIG_LOC "/var/lib/hyperv"
103 #ifndef KVP_SCRIPTS_PATH
104 #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/"
107 #define KVP_NET_DIR "/sys/class/net/"
109 #define MAX_FILE_NAME 100
110 #define ENTRIES_PER_BLOCK 50
112 * Change this entry if the number of addresses increases in future
114 #define MAX_IP_ENTRIES 64
115 #define OUTSTR_BUF_SIZE ((INET6_ADDRSTRLEN + 1) * MAX_IP_ENTRIES)
118 char key
[HV_KVP_EXCHANGE_MAX_KEY_SIZE
];
119 char value
[HV_KVP_EXCHANGE_MAX_VALUE_SIZE
];
122 struct kvp_file_state
{
125 struct kvp_record
*records
;
127 char fname
[MAX_FILE_NAME
];
130 static struct kvp_file_state kvp_file_info
[KVP_POOL_COUNT
];
132 static void kvp_acquire_lock(int pool
)
134 struct flock fl
= {F_WRLCK
, SEEK_SET
, 0, 0, 0};
137 if (fcntl(kvp_file_info
[pool
].fd
, F_SETLKW
, &fl
) == -1) {
138 syslog(LOG_ERR
, "Failed to acquire the lock pool: %d; error: %d %s", pool
,
139 errno
, strerror(errno
));
144 static void kvp_release_lock(int pool
)
146 struct flock fl
= {F_UNLCK
, SEEK_SET
, 0, 0, 0};
149 if (fcntl(kvp_file_info
[pool
].fd
, F_SETLK
, &fl
) == -1) {
150 syslog(LOG_ERR
, "Failed to release the lock pool: %d; error: %d %s", pool
,
151 errno
, strerror(errno
));
156 static void kvp_update_file(int pool
)
161 * We are going to write our in-memory registry out to
162 * disk; acquire the lock first.
164 kvp_acquire_lock(pool
);
166 filep
= fopen(kvp_file_info
[pool
].fname
, "we");
168 syslog(LOG_ERR
, "Failed to open file, pool: %d; error: %d %s", pool
,
169 errno
, strerror(errno
));
170 kvp_release_lock(pool
);
174 fwrite(kvp_file_info
[pool
].records
, sizeof(struct kvp_record
),
175 kvp_file_info
[pool
].num_records
, filep
);
177 if (ferror(filep
) || fclose(filep
)) {
178 kvp_release_lock(pool
);
179 syslog(LOG_ERR
, "Failed to write file, pool: %d", pool
);
183 kvp_release_lock(pool
);
186 static void kvp_update_mem_state(int pool
)
189 size_t records_read
= 0;
190 struct kvp_record
*record
= kvp_file_info
[pool
].records
;
191 struct kvp_record
*readp
;
192 int num_blocks
= kvp_file_info
[pool
].num_blocks
;
193 int alloc_unit
= sizeof(struct kvp_record
) * ENTRIES_PER_BLOCK
;
195 kvp_acquire_lock(pool
);
197 filep
= fopen(kvp_file_info
[pool
].fname
, "re");
199 syslog(LOG_ERR
, "Failed to open file, pool: %d; error: %d %s", pool
,
200 errno
, strerror(errno
));
201 kvp_release_lock(pool
);
205 readp
= &record
[records_read
];
206 records_read
+= fread(readp
, sizeof(struct kvp_record
),
207 ENTRIES_PER_BLOCK
* num_blocks
- records_read
,
212 "Failed to read file, pool: %d; error: %d %s",
213 pool
, errno
, strerror(errno
));
214 kvp_release_lock(pool
);
220 * We have more data to read.
223 record
= realloc(record
, alloc_unit
* num_blocks
);
225 if (record
== NULL
) {
226 syslog(LOG_ERR
, "malloc failed");
227 kvp_release_lock(pool
);
235 kvp_file_info
[pool
].num_blocks
= num_blocks
;
236 kvp_file_info
[pool
].records
= record
;
237 kvp_file_info
[pool
].num_records
= records_read
;
240 kvp_release_lock(pool
);
243 static int kvp_file_init(void)
248 int alloc_unit
= sizeof(struct kvp_record
) * ENTRIES_PER_BLOCK
;
250 if (access(KVP_CONFIG_LOC
, F_OK
)) {
251 if (mkdir(KVP_CONFIG_LOC
, 0755 /* rwxr-xr-x */)) {
252 syslog(LOG_ERR
, "Failed to create '%s'; error: %d %s", KVP_CONFIG_LOC
,
253 errno
, strerror(errno
));
258 for (i
= 0; i
< KVP_POOL_COUNT
; i
++) {
259 fname
= kvp_file_info
[i
].fname
;
260 sprintf(fname
, "%s/.kvp_pool_%d", KVP_CONFIG_LOC
, i
);
261 fd
= open(fname
, O_RDWR
| O_CREAT
| O_CLOEXEC
, 0644 /* rw-r--r-- */);
266 kvp_file_info
[i
].fd
= fd
;
267 kvp_file_info
[i
].num_blocks
= 1;
268 kvp_file_info
[i
].records
= malloc(alloc_unit
);
269 if (kvp_file_info
[i
].records
== NULL
)
271 kvp_file_info
[i
].num_records
= 0;
272 kvp_update_mem_state(i
);
278 static int kvp_key_delete(int pool
, const __u8
*key
, int key_size
)
283 struct kvp_record
*record
;
286 * First update the in-memory state.
288 kvp_update_mem_state(pool
);
290 num_records
= kvp_file_info
[pool
].num_records
;
291 record
= kvp_file_info
[pool
].records
;
293 for (i
= 0; i
< num_records
; i
++) {
294 if (memcmp(key
, record
[i
].key
, key_size
))
297 * Found a match; just move the remaining
300 if (i
== (num_records
- 1)) {
301 kvp_file_info
[pool
].num_records
--;
302 kvp_update_file(pool
);
308 for (; k
< num_records
; k
++) {
309 strcpy(record
[j
].key
, record
[k
].key
);
310 strcpy(record
[j
].value
, record
[k
].value
);
314 kvp_file_info
[pool
].num_records
--;
315 kvp_update_file(pool
);
321 static int kvp_key_add_or_modify(int pool
, const __u8
*key
, int key_size
,
322 const __u8
*value
, int value_size
)
326 struct kvp_record
*record
;
329 if ((key_size
> HV_KVP_EXCHANGE_MAX_KEY_SIZE
) ||
330 (value_size
> HV_KVP_EXCHANGE_MAX_VALUE_SIZE
))
334 * First update the in-memory state.
336 kvp_update_mem_state(pool
);
338 num_records
= kvp_file_info
[pool
].num_records
;
339 record
= kvp_file_info
[pool
].records
;
340 num_blocks
= kvp_file_info
[pool
].num_blocks
;
342 for (i
= 0; i
< num_records
; i
++) {
343 if (memcmp(key
, record
[i
].key
, key_size
))
346 * Found a match; just update the value -
347 * this is the modify case.
349 memcpy(record
[i
].value
, value
, value_size
);
350 kvp_update_file(pool
);
355 * Need to add a new entry;
357 if (num_records
== (ENTRIES_PER_BLOCK
* num_blocks
)) {
358 /* Need to allocate a larger array for reg entries. */
359 record
= realloc(record
, sizeof(struct kvp_record
) *
360 ENTRIES_PER_BLOCK
* (num_blocks
+ 1));
364 kvp_file_info
[pool
].num_blocks
++;
367 memcpy(record
[i
].value
, value
, value_size
);
368 memcpy(record
[i
].key
, key
, key_size
);
369 kvp_file_info
[pool
].records
= record
;
370 kvp_file_info
[pool
].num_records
++;
371 kvp_update_file(pool
);
375 static int kvp_get_value(int pool
, const __u8
*key
, int key_size
, __u8
*value
,
380 struct kvp_record
*record
;
382 if ((key_size
> HV_KVP_EXCHANGE_MAX_KEY_SIZE
) ||
383 (value_size
> HV_KVP_EXCHANGE_MAX_VALUE_SIZE
))
387 * First update the in-memory state.
389 kvp_update_mem_state(pool
);
391 num_records
= kvp_file_info
[pool
].num_records
;
392 record
= kvp_file_info
[pool
].records
;
394 for (i
= 0; i
< num_records
; i
++) {
395 if (memcmp(key
, record
[i
].key
, key_size
))
398 * Found a match; just copy the value out.
400 memcpy(value
, record
[i
].value
, value_size
);
407 static int kvp_pool_enumerate(int pool
, int index
, __u8
*key
, int key_size
,
408 __u8
*value
, int value_size
)
410 struct kvp_record
*record
;
413 * First update our in-memory database.
415 kvp_update_mem_state(pool
);
416 record
= kvp_file_info
[pool
].records
;
418 if (index
>= kvp_file_info
[pool
].num_records
) {
422 memcpy(key
, record
[index
].key
, key_size
);
423 memcpy(value
, record
[index
].value
, value_size
);
428 void kvp_get_os_info(void)
434 os_version
= uts_buf
.release
;
435 os_build
= strdup(uts_buf
.release
);
437 os_name
= uts_buf
.sysname
;
438 processor_arch
= uts_buf
.machine
;
441 * The current windows host (win7) expects the build
442 * string to be of the form: x.y.z
443 * Strip additional information we may have.
445 p
= strchr(os_version
, '-');
450 * Parse the /etc/os-release file if present:
451 * https://www.freedesktop.org/software/systemd/man/os-release.html
453 file
= fopen("/etc/os-release", "r");
455 while (fgets(buf
, sizeof(buf
), file
)) {
458 /* Ignore comments */
462 /* Split into name=value */
463 p
= strchr(buf
, '=');
468 /* Remove quotes and newline; un-escape */
477 } else if (*p
== '\'' || *p
== '"' ||
486 if (!strcmp(buf
, "NAME")) {
491 } else if (!strcmp(buf
, "VERSION_ID")) {
502 /* Fallback for older RH/SUSE releases */
503 file
= fopen("/etc/SuSE-release", "r");
505 goto kvp_osinfo_found
;
506 file
= fopen("/etc/redhat-release", "r");
508 goto kvp_osinfo_found
;
511 * We don't have information about the os.
516 /* up to three lines */
517 p
= fgets(buf
, sizeof(buf
), file
);
519 p
= strchr(buf
, '\n');
528 p
= fgets(buf
, sizeof(buf
), file
);
530 p
= strchr(buf
, '\n');
539 p
= fgets(buf
, sizeof(buf
), file
);
541 p
= strchr(buf
, '\n');
559 * Retrieve an interface name corresponding to the specified guid.
560 * If there is a match, the function returns a pointer
561 * to the interface name and if not, a NULL is returned.
562 * If a match is found, the caller is responsible for
563 * freeing the memory.
566 static char *kvp_get_if_name(char *guid
)
569 struct dirent
*entry
;
572 char *if_name
= NULL
;
574 char dev_id
[PATH_MAX
];
576 dir
= opendir(KVP_NET_DIR
);
580 while ((entry
= readdir(dir
)) != NULL
) {
582 * Set the state for the next pass.
584 snprintf(dev_id
, sizeof(dev_id
), "%s%s/device/device_id",
585 KVP_NET_DIR
, entry
->d_name
);
587 file
= fopen(dev_id
, "r");
591 p
= fgets(buf
, sizeof(buf
), file
);
597 if (!strcmp(p
, guid
)) {
599 * Found the guid match; return the interface
600 * name. The caller will free the memory.
602 if_name
= strdup(entry
->d_name
);
615 * Retrieve the MAC address given the interface name.
618 static char *kvp_if_name_to_mac(char *if_name
)
623 char addr_file
[PATH_MAX
];
625 char *mac_addr
= NULL
;
627 snprintf(addr_file
, sizeof(addr_file
), "%s%s%s", KVP_NET_DIR
,
628 if_name
, "/address");
630 file
= fopen(addr_file
, "r");
634 p
= fgets(buf
, sizeof(buf
), file
);
639 for (i
= 0; i
< strlen(p
); i
++)
640 p
[i
] = toupper(p
[i
]);
641 mac_addr
= strdup(p
);
648 static void kvp_process_ipconfig_file(char *cmd
,
649 char *config_buf
, unsigned int len
,
650 int element_size
, int offset
)
658 * First execute the command.
660 file
= popen(cmd
, "r");
665 memset(config_buf
, 0, len
);
666 while ((p
= fgets(buf
, sizeof(buf
), file
)) != NULL
) {
667 if (len
< strlen(config_buf
) + element_size
+ 1)
674 strcat(config_buf
, p
);
675 strcat(config_buf
, ";");
680 static void kvp_get_ipconfig_info(char *if_name
,
681 struct hv_kvp_ipaddr_value
*buffer
)
689 * Get the address of default gateway (ipv4).
691 sprintf(cmd
, "%s %s", "ip route show dev", if_name
);
692 strcat(cmd
, " | awk '/default/ {print $3 }'");
695 * Execute the command to gather gateway info.
697 kvp_process_ipconfig_file(cmd
, (char *)buffer
->gate_way
,
698 (MAX_GATEWAY_SIZE
* 2), INET_ADDRSTRLEN
, 0);
701 * Get the address of default gateway (ipv6).
703 sprintf(cmd
, "%s %s", "ip -f inet6 route show dev", if_name
);
704 strcat(cmd
, " | awk '/default/ {print $3 }'");
707 * Execute the command to gather gateway info (ipv6).
709 kvp_process_ipconfig_file(cmd
, (char *)buffer
->gate_way
,
710 (MAX_GATEWAY_SIZE
* 2), INET6_ADDRSTRLEN
, 1);
714 * Gather the DNS state.
715 * Since there is no standard way to get this information
716 * across various distributions of interest; we just invoke
717 * an external script that needs to be ported across distros
720 * Following is the expected format of the information from the script:
722 * ipaddr1 (nameserver1)
723 * ipaddr2 (nameserver2)
728 sprintf(cmd
, KVP_SCRIPTS_PATH
"%s", "hv_get_dns_info");
731 * Execute the command to gather DNS info.
733 kvp_process_ipconfig_file(cmd
, (char *)buffer
->dns_addr
,
734 (MAX_IP_ADDR_SIZE
* 2), INET_ADDRSTRLEN
, 0);
737 * Gather the DHCP state.
738 * We will gather this state by invoking an external script.
739 * The parameter to the script is the interface name.
740 * Here is the expected output:
742 * Enabled: DHCP enabled.
745 sprintf(cmd
, KVP_SCRIPTS_PATH
"%s %s", "hv_get_dhcp_info", if_name
);
747 file
= popen(cmd
, "r");
751 p
= fgets(dhcp_info
, sizeof(dhcp_info
), file
);
757 if (!strncmp(p
, "Enabled", 7))
758 buffer
->dhcp_enabled
= 1;
760 buffer
->dhcp_enabled
= 0;
766 static unsigned int hweight32(unsigned int *w
)
768 unsigned int res
= *w
- ((*w
>> 1) & 0x55555555);
769 res
= (res
& 0x33333333) + ((res
>> 2) & 0x33333333);
770 res
= (res
+ (res
>> 4)) & 0x0F0F0F0F;
771 res
= res
+ (res
>> 8);
772 return (res
+ (res
>> 16)) & 0x000000FF;
775 static int kvp_process_ip_address(void *addrp
,
776 int family
, char *buffer
,
777 int length
, int *offset
)
779 struct sockaddr_in
*addr
;
780 struct sockaddr_in6
*addr6
;
785 if (family
== AF_INET
) {
787 str
= inet_ntop(family
, &addr
->sin_addr
, tmp
, 50);
788 addr_length
= INET_ADDRSTRLEN
;
791 str
= inet_ntop(family
, &addr6
->sin6_addr
.s6_addr
, tmp
, 50);
792 addr_length
= INET6_ADDRSTRLEN
;
795 if ((length
- *offset
) < addr_length
+ 2)
798 strcpy(buffer
, "inet_ntop failed\n");
808 *offset
+= strlen(str
) + 1;
814 kvp_get_ip_info(int family
, char *if_name
, int op
,
815 void *out_buffer
, unsigned int length
)
817 struct ifaddrs
*ifap
;
818 struct ifaddrs
*curp
;
823 struct hv_kvp_ipaddr_value
*ip_buffer
= NULL
;
824 char cidr_mask
[5]; /* /xyz */
829 struct sockaddr_in6
*addr6
;
831 if (op
== KVP_OP_ENUMERATE
) {
834 ip_buffer
= out_buffer
;
835 buffer
= (char *)ip_buffer
->ip_addr
;
836 ip_buffer
->addr_family
= 0;
839 * On entry into this function, the buffer is capable of holding the
843 if (getifaddrs(&ifap
)) {
844 strcpy(buffer
, "getifaddrs failed\n");
849 while (curp
!= NULL
) {
850 if (curp
->ifa_addr
== NULL
) {
851 curp
= curp
->ifa_next
;
855 if ((if_name
!= NULL
) &&
856 (strncmp(curp
->ifa_name
, if_name
, strlen(if_name
)))) {
858 * We want info about a specific interface;
861 curp
= curp
->ifa_next
;
866 * We only support two address families: AF_INET and AF_INET6.
867 * If a family value of 0 is specified, we collect both
868 * supported address families; if not we gather info on
869 * the specified address family.
871 if ((((family
!= 0) &&
872 (curp
->ifa_addr
->sa_family
!= family
))) ||
873 (curp
->ifa_flags
& IFF_LOOPBACK
)) {
874 curp
= curp
->ifa_next
;
877 if ((curp
->ifa_addr
->sa_family
!= AF_INET
) &&
878 (curp
->ifa_addr
->sa_family
!= AF_INET6
)) {
879 curp
= curp
->ifa_next
;
883 if (op
== KVP_OP_GET_IP_INFO
) {
885 * Gather info other than the IP address.
886 * IP address info will be gathered later.
888 if (curp
->ifa_addr
->sa_family
== AF_INET
) {
889 ip_buffer
->addr_family
|= ADDR_FAMILY_IPV4
;
893 error
= kvp_process_ip_address(
903 ip_buffer
->addr_family
|= ADDR_FAMILY_IPV6
;
906 * Get subnet info in CIDR format.
909 sn_str
= (char *)ip_buffer
->sub_net
;
910 addr6
= (struct sockaddr_in6
*)
912 w
= addr6
->sin6_addr
.s6_addr32
;
914 for (i
= 0; i
< 4; i
++)
915 weight
+= hweight32(&w
[i
]);
917 sprintf(cidr_mask
, "/%d", weight
);
918 if (length
< sn_offset
+ strlen(cidr_mask
) + 1)
922 strcpy(sn_str
, cidr_mask
);
924 strcat((char *)ip_buffer
->sub_net
, ";");
925 strcat(sn_str
, cidr_mask
);
927 sn_offset
+= strlen(sn_str
) + 1;
931 * Collect other ip related configuration info.
934 kvp_get_ipconfig_info(if_name
, ip_buffer
);
938 error
= kvp_process_ip_address(curp
->ifa_addr
,
939 curp
->ifa_addr
->sa_family
,
945 curp
= curp
->ifa_next
;
954 * Retrieve the IP given the MAC address.
956 static int kvp_mac_to_ip(struct hv_kvp_ipaddr_value
*kvp_ip_val
)
958 char *mac
= (char *)kvp_ip_val
->adapter_id
;
960 struct dirent
*entry
;
963 char *if_name
= NULL
;
965 char dev_id
[PATH_MAX
];
967 int error
= HV_E_FAIL
;
969 dir
= opendir(KVP_NET_DIR
);
973 while ((entry
= readdir(dir
)) != NULL
) {
975 * Set the state for the next pass.
977 snprintf(dev_id
, sizeof(dev_id
), "%s%s/address", KVP_NET_DIR
,
980 file
= fopen(dev_id
, "r");
984 p
= fgets(buf
, sizeof(buf
), file
);
993 for (i
= 0; i
< strlen(p
); i
++)
994 p
[i
] = toupper(p
[i
]);
1000 * Found the MAC match.
1001 * A NIC (e.g. VF) matching the MAC, but without IP, is skipped.
1003 if_name
= entry
->d_name
;
1007 error
= kvp_get_ip_info(0, if_name
, KVP_OP_GET_IP_INFO
,
1008 kvp_ip_val
, MAX_IP_ADDR_SIZE
* 2);
1010 if (!error
&& strlen((char *)kvp_ip_val
->ip_addr
))
1018 static int expand_ipv6(char *addr
, int type
)
1021 struct in6_addr v6_addr
;
1023 ret
= inet_pton(AF_INET6
, addr
, &v6_addr
);
1026 if (type
== NETMASK
)
1031 sprintf(addr
, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1032 "%02x%02x:%02x%02x:%02x%02x",
1033 (int)v6_addr
.s6_addr
[0], (int)v6_addr
.s6_addr
[1],
1034 (int)v6_addr
.s6_addr
[2], (int)v6_addr
.s6_addr
[3],
1035 (int)v6_addr
.s6_addr
[4], (int)v6_addr
.s6_addr
[5],
1036 (int)v6_addr
.s6_addr
[6], (int)v6_addr
.s6_addr
[7],
1037 (int)v6_addr
.s6_addr
[8], (int)v6_addr
.s6_addr
[9],
1038 (int)v6_addr
.s6_addr
[10], (int)v6_addr
.s6_addr
[11],
1039 (int)v6_addr
.s6_addr
[12], (int)v6_addr
.s6_addr
[13],
1040 (int)v6_addr
.s6_addr
[14], (int)v6_addr
.s6_addr
[15]);
1046 static int is_ipv4(char *addr
)
1049 struct in_addr ipv4_addr
;
1051 ret
= inet_pton(AF_INET
, addr
, &ipv4_addr
);
1058 static int parse_ip_val_buffer(char *in_buf
, int *offset
,
1059 char *out_buf
, int out_len
)
1065 * in_buf has sequence of characters that are separated by
1066 * the character ';'. The last sequence does not have the
1067 * terminating ";" character.
1069 start
= in_buf
+ *offset
;
1071 x
= strchr(start
, ';');
1075 x
= start
+ strlen(start
);
1077 if (strlen(start
) != 0) {
1080 * Get rid of leading spaces.
1082 while (start
[i
] == ' ')
1085 if ((x
- start
) <= out_len
) {
1086 strcpy(out_buf
, (start
+ i
));
1087 *offset
+= (x
- start
) + 1;
1094 static int kvp_write_file(FILE *f
, char *s1
, char *s2
, char *s3
)
1098 ret
= fprintf(f
, "%s%s%s%s\n", s1
, s2
, "=", s3
);
1107 static int process_ip_string(FILE *f
, char *ip_string
, int type
)
1110 char addr
[INET6_ADDRSTRLEN
];
1117 memset(addr
, 0, sizeof(addr
));
1119 while (parse_ip_val_buffer(ip_string
, &offset
, addr
,
1120 (MAX_IP_ADDR_SIZE
* 2))) {
1123 if (is_ipv4(addr
)) {
1126 snprintf(str
, sizeof(str
), "%s", "IPADDR");
1129 snprintf(str
, sizeof(str
), "%s", "NETMASK");
1132 snprintf(str
, sizeof(str
), "%s", "GATEWAY");
1135 snprintf(str
, sizeof(str
), "%s", "DNS");
1140 snprintf(sub_str
, sizeof(sub_str
), "%d", ++i
);
1141 } else if (type
== GATEWAY
&& i
== 0) {
1144 snprintf(sub_str
, sizeof(sub_str
), "%d", i
++);
1148 } else if (expand_ipv6(addr
, type
)) {
1151 snprintf(str
, sizeof(str
), "%s", "IPV6ADDR");
1154 snprintf(str
, sizeof(str
), "%s", "IPV6NETMASK");
1157 snprintf(str
, sizeof(str
), "%s",
1161 snprintf(str
, sizeof(str
), "%s", "DNS");
1166 snprintf(sub_str
, sizeof(sub_str
), "%d", ++i
);
1167 } else if (j
== 0) {
1170 snprintf(sub_str
, sizeof(sub_str
), "_%d", j
++);
1173 return HV_INVALIDARG
;
1176 error
= kvp_write_file(f
, str
, sub_str
, addr
);
1179 memset(addr
, 0, sizeof(addr
));
1185 int ip_version_check(const char *input_addr
)
1187 struct in6_addr addr
;
1189 if (inet_pton(AF_INET
, input_addr
, &addr
))
1191 else if (inet_pton(AF_INET6
, input_addr
, &addr
))
1198 * Only IPv4 subnet strings needs to be converted to plen
1199 * For IPv6 the subnet is already privided in plen format
1201 static int kvp_subnet_to_plen(char *subnet_addr_str
)
1204 struct in_addr subnet_addr4
;
1207 * Convert subnet address to binary representation
1209 if (inet_pton(AF_INET
, subnet_addr_str
, &subnet_addr4
) == 1) {
1210 uint32_t subnet_mask
= ntohl(subnet_addr4
.s_addr
);
1212 while (subnet_mask
& 0x80000000) {
1223 static int process_dns_gateway_nm(FILE *f
, char *ip_string
, int type
,
1226 char addr
[INET6_ADDRSTRLEN
], *output_str
;
1227 int ip_offset
= 0, error
= 0, ip_ver
;
1232 else if (type
== GATEWAY
)
1233 param_name
= "gateway";
1237 output_str
= (char *)calloc(OUTSTR_BUF_SIZE
, sizeof(char));
1242 memset(addr
, 0, sizeof(addr
));
1244 if (!parse_ip_val_buffer(ip_string
, &ip_offset
, addr
,
1245 (MAX_IP_ADDR_SIZE
* 2)))
1248 ip_ver
= ip_version_check(addr
);
1252 if ((ip_ver
== IPV4
&& ip_sec
== IPV4
) ||
1253 (ip_ver
== IPV6
&& ip_sec
== IPV6
)) {
1255 * do a bound check to avoid out-of bound writes
1257 if ((OUTSTR_BUF_SIZE
- strlen(output_str
)) >
1258 (strlen(addr
) + 1)) {
1259 strncat(output_str
, addr
,
1261 strlen(output_str
) - 1);
1262 strncat(output_str
, ",",
1264 strlen(output_str
) - 1);
1271 if (strlen(output_str
)) {
1273 * This is to get rid of that extra comma character
1274 * in the end of the string
1276 output_str
[strlen(output_str
) - 1] = '\0';
1277 error
= fprintf(f
, "%s=%s\n", param_name
, output_str
);
1284 static int process_ip_string_nm(FILE *f
, char *ip_string
, char *subnet
,
1287 char addr
[INET6_ADDRSTRLEN
];
1288 char subnet_addr
[INET6_ADDRSTRLEN
];
1289 int error
= 0, i
= 0;
1290 int ip_offset
= 0, subnet_offset
= 0;
1293 memset(addr
, 0, sizeof(addr
));
1294 memset(subnet_addr
, 0, sizeof(subnet_addr
));
1296 while (parse_ip_val_buffer(ip_string
, &ip_offset
, addr
,
1297 (MAX_IP_ADDR_SIZE
* 2)) &&
1298 parse_ip_val_buffer(subnet
,
1303 ip_ver
= ip_version_check(addr
);
1307 if (ip_ver
== IPV4
&& ip_sec
== IPV4
)
1308 plen
= kvp_subnet_to_plen((char *)subnet_addr
);
1309 else if (ip_ver
== IPV6
&& ip_sec
== IPV6
)
1310 plen
= atoi(subnet_addr
);
1317 error
= fprintf(f
, "address%d=%s/%d\n", ++i
, (char *)addr
,
1322 memset(addr
, 0, sizeof(addr
));
1323 memset(subnet_addr
, 0, sizeof(subnet_addr
));
1329 static int kvp_set_ip_info(char *if_name
, struct hv_kvp_ipaddr_value
*new_val
)
1331 int error
= 0, ip_ver
;
1332 char if_filename
[PATH_MAX
];
1333 char nm_filename
[PATH_MAX
];
1334 FILE *ifcfg_file
, *nmfile
;
1340 * Set the configuration for the specified interface with
1341 * the information provided. Since there is no standard
1342 * way to configure an interface, we will have an external
1343 * script that does the job of configuring the interface and
1344 * flushing the configuration.
1346 * The parameters passed to this external script are:
1347 * 1. A configuration file that has the specified configuration.
1349 * We will embed the name of the interface in the configuration
1350 * file: ifcfg-ethx (where ethx is the interface name).
1352 * The information provided here may be more than what is needed
1353 * in a given distro to configure the interface and so are free
1354 * ignore information that may not be relevant.
1356 * Here is the ifcfg format of the ip configuration file:
1359 * DEVICE=interface name
1360 * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
1361 * or "none" if no boot-time protocol should be used)
1365 * IPADDRx=ipaddry (where y = x + 1)
1368 * NETMASKx=netmasky (where y = x + 1)
1371 * GATEWAYx=ipaddry (where y = x + 1)
1373 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1375 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1376 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1379 * Here is the keyfile format of the ip configuration file:
1382 * mac-address=macaddr
1384 * interface-name=interface name
1387 * method=<protocol> (where <protocol> is "auto" if DHCP is configured
1388 * or "manual" if no boot-time protocol should be used)
1390 * address1=ipaddr1/plen
1391 * address2=ipaddr2/plen
1393 * gateway=gateway1;gateway2
1398 * address1=ipaddr1/plen
1399 * address2=ipaddr2/plen
1401 * gateway=gateway1;gateway2
1405 * The host can specify multiple ipv4 and ipv6 addresses to be
1406 * configured for the interface. Furthermore, the configuration
1407 * needs to be persistent. A subsequent GET call on the interface
1408 * is expected to return the configuration that is set via the SET
1413 * We are populating both ifcfg and nmconnection files
1415 snprintf(if_filename
, sizeof(if_filename
), "%s%s%s", KVP_CONFIG_LOC
,
1416 "/ifcfg-", if_name
);
1418 ifcfg_file
= fopen(if_filename
, "w");
1421 syslog(LOG_ERR
, "Failed to open config file; error: %d %s",
1422 errno
, strerror(errno
));
1426 snprintf(nm_filename
, sizeof(nm_filename
), "%s%s%s%s", KVP_CONFIG_LOC
,
1427 "/", if_name
, ".nmconnection");
1429 nmfile
= fopen(nm_filename
, "w");
1432 syslog(LOG_ERR
, "Failed to open config file; error: %d %s",
1433 errno
, strerror(errno
));
1439 * First write out the MAC address.
1442 mac_addr
= kvp_if_name_to_mac(if_name
);
1443 if (mac_addr
== NULL
) {
1448 error
= kvp_write_file(ifcfg_file
, "HWADDR", "", mac_addr
);
1452 error
= kvp_write_file(ifcfg_file
, "DEVICE", "", if_name
);
1456 error
= fprintf(nmfile
, "\n[connection]\n");
1460 error
= kvp_write_file(nmfile
, "interface-name", "", if_name
);
1464 error
= fprintf(nmfile
, "\n[ethernet]\n");
1468 error
= kvp_write_file(nmfile
, "mac-address", "", mac_addr
);
1475 * The dhcp_enabled flag is only for IPv4. In the case the host only
1476 * injects an IPv6 address, the flag is true, but we still need to
1477 * proceed to parse and pass the IPv6 information to the
1478 * disto-specific script hv_set_ifconfig.
1482 * First populate the ifcfg file format
1484 if (new_val
->dhcp_enabled
) {
1485 error
= kvp_write_file(ifcfg_file
, "BOOTPROTO", "", "dhcp");
1489 error
= kvp_write_file(ifcfg_file
, "BOOTPROTO", "", "none");
1494 error
= process_ip_string(ifcfg_file
, (char *)new_val
->ip_addr
,
1499 error
= process_ip_string(ifcfg_file
, (char *)new_val
->sub_net
,
1504 error
= process_ip_string(ifcfg_file
, (char *)new_val
->gate_way
,
1509 error
= process_ip_string(ifcfg_file
, (char *)new_val
->dns_addr
, DNS
);
1514 * Now we populate the keyfile format
1516 * The keyfile format expects the IPv6 and IPv4 configuration in
1517 * different sections. Therefore we iterate through the list twice,
1518 * once to populate the IPv4 section and the next time for IPv6
1522 if (ip_ver
== IPV4
) {
1523 error
= fprintf(nmfile
, "\n[ipv4]\n");
1527 error
= fprintf(nmfile
, "\n[ipv6]\n");
1533 * Write the configuration for ipaddress, netmask, gateway and
1536 error
= process_ip_string_nm(nmfile
, (char *)new_val
->ip_addr
,
1537 (char *)new_val
->sub_net
,
1543 * As dhcp_enabled is only valid for ipv4, we do not set dhcp
1544 * methods for ipv6 based on dhcp_enabled flag.
1546 * For ipv4, set method to manual only when dhcp_enabled is
1547 * false and specific ipv4 addresses are configured. If neither
1548 * dhcp_enabled is true and no ipv4 addresses are configured,
1549 * set method to 'disabled'.
1551 * For ipv6, set method to manual when we configure ipv6
1552 * addresses. Otherwise set method to 'auto' so that SLAAC from
1555 if (ip_ver
== IPV4
) {
1556 if (new_val
->dhcp_enabled
) {
1557 error
= kvp_write_file(nmfile
, "method", "",
1562 error
= kvp_write_file(nmfile
, "method", "",
1567 error
= kvp_write_file(nmfile
, "method", "",
1572 } else if (ip_ver
== IPV6
) {
1574 error
= kvp_write_file(nmfile
, "method", "",
1579 error
= kvp_write_file(nmfile
, "method", "",
1586 error
= process_dns_gateway_nm(nmfile
,
1587 (char *)new_val
->gate_way
,
1592 error
= process_dns_gateway_nm(nmfile
,
1593 (char *)new_val
->dns_addr
, DNS
,
1599 } while (ip_ver
< IP_TYPE_MAX
);
1605 * Now that we have populated the configuration file,
1606 * invoke the external script to do its magic.
1609 str_len
= snprintf(cmd
, sizeof(cmd
), KVP_SCRIPTS_PATH
"%s %s %s",
1610 "hv_set_ifconfig", if_filename
, nm_filename
);
1612 * This is a little overcautious, but it's necessary to suppress some
1613 * false warnings from gcc 8.0.1.
1615 if (str_len
<= 0 || (unsigned int)str_len
>= sizeof(cmd
)) {
1616 syslog(LOG_ERR
, "Cmd '%s' (len=%d) may be too long",
1622 syslog(LOG_ERR
, "Failed to execute cmd '%s'; error: %d %s",
1623 cmd
, errno
, strerror(errno
));
1630 syslog(LOG_ERR
, "Failed to write config file");
1638 kvp_get_domain_name(char *buffer
, int length
)
1640 struct addrinfo hints
, *info
;
1643 gethostname(buffer
, length
);
1644 memset(&hints
, 0, sizeof(hints
));
1645 hints
.ai_family
= AF_INET
; /*Get only ipv4 addrinfo. */
1646 hints
.ai_socktype
= SOCK_STREAM
;
1647 hints
.ai_flags
= AI_CANONNAME
;
1649 error
= getaddrinfo(buffer
, NULL
, &hints
, &info
);
1651 snprintf(buffer
, length
, "getaddrinfo failed: 0x%x %s",
1652 error
, gai_strerror(error
));
1655 snprintf(buffer
, length
, "%s", info
->ai_canonname
);
1659 void print_usage(char *argv
[])
1661 fprintf(stderr
, "Usage: %s [options]\n"
1663 " -n, --no-daemon stay in foreground, don't daemonize\n"
1664 " -h, --help print this help\n", argv
[0]);
1667 int main(int argc
, char *argv
[])
1669 int kvp_fd
= -1, len
;
1673 struct hv_kvp_msg hv_msg
[1];
1679 struct hv_kvp_ipaddr_value
*kvp_ip_val
;
1680 int daemonize
= 1, long_index
= 0, opt
;
1682 static struct option long_options
[] = {
1683 {"help", no_argument
, 0, 'h' },
1684 {"no-daemon", no_argument
, 0, 'n' },
1688 while ((opt
= getopt_long(argc
, argv
, "hn", long_options
,
1689 &long_index
)) != -1) {
1703 if (daemonize
&& daemon(1, 0))
1706 openlog("KVP", 0, LOG_USER
);
1707 syslog(LOG_INFO
, "KVP starting; pid is:%d", getpid());
1710 * Retrieve OS release information.
1714 * Cache Fully Qualified Domain Name because getaddrinfo takes an
1715 * unpredictable amount of time to finish.
1717 kvp_get_domain_name(full_domain_name
, sizeof(full_domain_name
));
1719 if (kvp_file_init()) {
1720 syslog(LOG_ERR
, "Failed to initialize the pools");
1728 kvp_fd
= open("/dev/vmbus/hv_kvp", O_RDWR
| O_CLOEXEC
);
1731 syslog(LOG_ERR
, "open /dev/vmbus/hv_kvp failed; error: %d %s",
1732 errno
, strerror(errno
));
1737 * Register ourselves with the kernel.
1739 hv_msg
->kvp_hdr
.operation
= KVP_OP_REGISTER1
;
1740 len
= write(kvp_fd
, hv_msg
, sizeof(struct hv_kvp_msg
));
1741 if (len
!= sizeof(struct hv_kvp_msg
)) {
1742 syslog(LOG_ERR
, "registration to kernel failed; error: %d %s",
1743 errno
, strerror(errno
));
1751 pfd
.events
= POLLIN
;
1754 if (poll(&pfd
, 1, -1) < 0) {
1755 syslog(LOG_ERR
, "poll failed; error: %d %s", errno
, strerror(errno
));
1756 if (errno
== EINVAL
) {
1764 len
= read(kvp_fd
, hv_msg
, sizeof(struct hv_kvp_msg
));
1766 if (len
!= sizeof(struct hv_kvp_msg
)) {
1767 syslog(LOG_ERR
, "read failed; error:%d %s",
1768 errno
, strerror(errno
));
1773 * We will use the KVP header information to pass back
1774 * the error from this daemon. So, first copy the state
1775 * and set the error code to success.
1777 op
= hv_msg
->kvp_hdr
.operation
;
1778 pool
= hv_msg
->kvp_hdr
.pool
;
1779 hv_msg
->error
= HV_S_OK
;
1781 if ((in_hand_shake
) && (op
== KVP_OP_REGISTER1
)) {
1783 * Driver is registering with us; stash away the version
1787 p
= (char *)hv_msg
->body
.kvp_register
.version
;
1788 lic_version
= malloc(strlen(p
) + 1);
1790 strcpy(lic_version
, p
);
1791 syslog(LOG_INFO
, "KVP LIC Version: %s",
1794 syslog(LOG_ERR
, "malloc failed");
1800 case KVP_OP_GET_IP_INFO
:
1801 kvp_ip_val
= &hv_msg
->body
.kvp_ip_val
;
1803 error
= kvp_mac_to_ip(kvp_ip_val
);
1806 hv_msg
->error
= error
;
1810 case KVP_OP_SET_IP_INFO
:
1811 kvp_ip_val
= &hv_msg
->body
.kvp_ip_val
;
1812 if_name
= kvp_get_if_name(
1813 (char *)kvp_ip_val
->adapter_id
);
1814 if (if_name
== NULL
) {
1816 * We could not map the guid to an
1817 * interface name; return error.
1819 hv_msg
->error
= HV_GUID_NOTFOUND
;
1822 error
= kvp_set_ip_info(if_name
, kvp_ip_val
);
1824 hv_msg
->error
= error
;
1830 if (kvp_key_add_or_modify(pool
,
1831 hv_msg
->body
.kvp_set
.data
.key
,
1832 hv_msg
->body
.kvp_set
.data
.key_size
,
1833 hv_msg
->body
.kvp_set
.data
.value
,
1834 hv_msg
->body
.kvp_set
.data
.value_size
))
1835 hv_msg
->error
= HV_S_CONT
;
1839 if (kvp_get_value(pool
,
1840 hv_msg
->body
.kvp_set
.data
.key
,
1841 hv_msg
->body
.kvp_set
.data
.key_size
,
1842 hv_msg
->body
.kvp_set
.data
.value
,
1843 hv_msg
->body
.kvp_set
.data
.value_size
))
1844 hv_msg
->error
= HV_S_CONT
;
1848 if (kvp_key_delete(pool
,
1849 hv_msg
->body
.kvp_delete
.key
,
1850 hv_msg
->body
.kvp_delete
.key_size
))
1851 hv_msg
->error
= HV_S_CONT
;
1858 if (op
!= KVP_OP_ENUMERATE
)
1862 * If the pool is KVP_POOL_AUTO, dynamically generate
1863 * both the key and the value; if not read from the
1866 if (pool
!= KVP_POOL_AUTO
) {
1867 if (kvp_pool_enumerate(pool
,
1868 hv_msg
->body
.kvp_enum_data
.index
,
1869 hv_msg
->body
.kvp_enum_data
.data
.key
,
1870 HV_KVP_EXCHANGE_MAX_KEY_SIZE
,
1871 hv_msg
->body
.kvp_enum_data
.data
.value
,
1872 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
))
1873 hv_msg
->error
= HV_S_CONT
;
1877 key_name
= (char *)hv_msg
->body
.kvp_enum_data
.data
.key
;
1878 key_value
= (char *)hv_msg
->body
.kvp_enum_data
.data
.value
;
1880 switch (hv_msg
->body
.kvp_enum_data
.index
) {
1881 case FullyQualifiedDomainName
:
1882 strcpy(key_value
, full_domain_name
);
1883 strcpy(key_name
, "FullyQualifiedDomainName");
1885 case IntegrationServicesVersion
:
1886 strcpy(key_name
, "IntegrationServicesVersion");
1887 strcpy(key_value
, lic_version
);
1889 case NetworkAddressIPv4
:
1890 kvp_get_ip_info(AF_INET
, NULL
, KVP_OP_ENUMERATE
,
1891 key_value
, HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
1892 strcpy(key_name
, "NetworkAddressIPv4");
1894 case NetworkAddressIPv6
:
1895 kvp_get_ip_info(AF_INET6
, NULL
, KVP_OP_ENUMERATE
,
1896 key_value
, HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
1897 strcpy(key_name
, "NetworkAddressIPv6");
1900 strcpy(key_value
, os_build
);
1901 strcpy(key_name
, "OSBuildNumber");
1904 strcpy(key_value
, os_name
);
1905 strcpy(key_name
, "OSName");
1907 case OSMajorVersion
:
1908 strcpy(key_value
, os_major
);
1909 strcpy(key_name
, "OSMajorVersion");
1911 case OSMinorVersion
:
1912 strcpy(key_value
, os_minor
);
1913 strcpy(key_name
, "OSMinorVersion");
1916 strcpy(key_value
, os_version
);
1917 strcpy(key_name
, "OSVersion");
1919 case ProcessorArchitecture
:
1920 strcpy(key_value
, processor_arch
);
1921 strcpy(key_name
, "ProcessorArchitecture");
1924 hv_msg
->error
= HV_S_CONT
;
1929 * Send the value back to the kernel. Note: the write() may
1930 * return an error due to hibernation; we can ignore the error
1931 * by resetting the dev file, i.e. closing and re-opening it.
1934 len
= write(kvp_fd
, hv_msg
, sizeof(struct hv_kvp_msg
));
1935 if (len
!= sizeof(struct hv_kvp_msg
)) {
1936 syslog(LOG_ERR
, "write failed; error: %d %s", errno
,