Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * An implementation of key value pair (KVP) functionality for Linux. |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2010, Novell, Inc. |
| 6 | * Author : K. Y. Srinivasan <ksrinivasan@novell.com> |
| 7 | * |
| 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. |
| 11 | * |
| 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 |
| 16 | * details. |
| 17 | * |
| 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. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 25 | #include <sys/poll.h> |
| 26 | #include <sys/utsname.h> |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <unistd.h> |
| 30 | #include <string.h> |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 31 | #include <ctype.h> |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <arpa/inet.h> |
K. Y. Srinivasan | eab6af7 | 2012-02-02 16:56:49 -0800 | [diff] [blame] | 34 | #include <linux/hyperv.h> |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 35 | #include <ifaddrs.h> |
| 36 | #include <netdb.h> |
| 37 | #include <syslog.h> |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 38 | #include <sys/stat.h> |
| 39 | #include <fcntl.h> |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 40 | #include <dirent.h> |
K. Y. Srinivasan | 3321e73 | 2012-10-25 14:15:50 -0700 | [diff] [blame] | 41 | #include <net/if.h> |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 42 | #include <limits.h> |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 43 | #include <getopt.h> |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
| 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. |
| 51 | * |
| 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. |
| 54 | * |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 55 | */ |
| 56 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 57 | |
| 58 | enum key_index { |
| 59 | FullyQualifiedDomainName = 0, |
| 60 | IntegrationServicesVersion, /*This key is serviced in the kernel*/ |
| 61 | NetworkAddressIPv4, |
| 62 | NetworkAddressIPv6, |
| 63 | OSBuildNumber, |
| 64 | OSName, |
| 65 | OSMajorVersion, |
| 66 | OSMinorVersion, |
| 67 | OSVersion, |
| 68 | ProcessorArchitecture |
| 69 | }; |
| 70 | |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 71 | |
| 72 | enum { |
| 73 | IPADDR = 0, |
| 74 | NETMASK, |
| 75 | GATEWAY, |
| 76 | DNS |
| 77 | }; |
| 78 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 79 | static int in_hand_shake = 1; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 80 | |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 81 | static char *os_name = ""; |
| 82 | static char *os_major = ""; |
| 83 | static char *os_minor = ""; |
| 84 | static char *processor_arch; |
| 85 | static char *os_build; |
K. Y. Srinivasan | f426a36 | 2012-10-25 14:15:49 -0700 | [diff] [blame] | 86 | static char *os_version; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 87 | static char *lic_version = "Unknown version"; |
Olaf Hering | 58125210 | 2013-08-07 19:14:37 +0200 | [diff] [blame] | 88 | static char full_domain_name[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 89 | static struct utsname uts_buf; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 90 | |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 91 | /* |
| 92 | * The location of the interface configuration file. |
| 93 | */ |
| 94 | |
Tomas Hozza | 40424f5 | 2012-11-27 08:56:33 +0100 | [diff] [blame] | 95 | #define KVP_CONFIG_LOC "/var/lib/hyperv" |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 96 | |
Alex Fluter | 2eb72d4 | 2016-12-03 12:34:41 -0800 | [diff] [blame] | 97 | #ifndef KVP_SCRIPTS_PATH |
| 98 | #define KVP_SCRIPTS_PATH "/usr/libexec/hypervkvpd/" |
| 99 | #endif |
| 100 | |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 101 | #define KVP_NET_DIR "/sys/class/net/" |
| 102 | |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 103 | #define MAX_FILE_NAME 100 |
| 104 | #define ENTRIES_PER_BLOCK 50 |
| 105 | |
| 106 | struct kvp_record { |
K. Y. Srinivasan | d0cbc15 | 2012-09-04 14:46:34 -0700 | [diff] [blame] | 107 | char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; |
| 108 | char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | struct kvp_file_state { |
| 112 | int fd; |
| 113 | int num_blocks; |
| 114 | struct kvp_record *records; |
| 115 | int num_records; |
K. Y. Srinivasan | d0cbc15 | 2012-09-04 14:46:34 -0700 | [diff] [blame] | 116 | char fname[MAX_FILE_NAME]; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT]; |
| 120 | |
| 121 | static void kvp_acquire_lock(int pool) |
| 122 | { |
| 123 | struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0}; |
| 124 | fl.l_pid = getpid(); |
| 125 | |
| 126 | if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) { |
Tomas Hozza | 12e50c3 | 2013-06-17 10:39:44 +0200 | [diff] [blame] | 127 | syslog(LOG_ERR, "Failed to acquire the lock pool: %d; error: %d %s", pool, |
| 128 | errno, strerror(errno)); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 129 | exit(EXIT_FAILURE); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | static void kvp_release_lock(int pool) |
| 134 | { |
| 135 | struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0}; |
| 136 | fl.l_pid = getpid(); |
| 137 | |
| 138 | if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) { |
Tomas Hozza | 12e50c3 | 2013-06-17 10:39:44 +0200 | [diff] [blame] | 139 | syslog(LOG_ERR, "Failed to release the lock pool: %d; error: %d %s", pool, |
| 140 | errno, strerror(errno)); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 141 | exit(EXIT_FAILURE); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | static void kvp_update_file(int pool) |
| 146 | { |
| 147 | FILE *filep; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * We are going to write our in-memory registry out to |
| 151 | * disk; acquire the lock first. |
| 152 | */ |
| 153 | kvp_acquire_lock(pool); |
| 154 | |
Tomas Hozza | 8467fdbb | 2013-01-18 15:23:41 +0100 | [diff] [blame] | 155 | filep = fopen(kvp_file_info[pool].fname, "we"); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 156 | if (!filep) { |
Tomas Hozza | 12e50c3 | 2013-06-17 10:39:44 +0200 | [diff] [blame] | 157 | syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool, |
| 158 | errno, strerror(errno)); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 159 | kvp_release_lock(pool); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 160 | exit(EXIT_FAILURE); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Vitaly Kuznetsov | 77ce247 | 2015-01-09 22:18:52 -0800 | [diff] [blame] | 163 | fwrite(kvp_file_info[pool].records, sizeof(struct kvp_record), |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 164 | kvp_file_info[pool].num_records, filep); |
| 165 | |
Ben Hutchings | 436473b | 2012-09-05 14:37:37 -0700 | [diff] [blame] | 166 | if (ferror(filep) || fclose(filep)) { |
| 167 | kvp_release_lock(pool); |
| 168 | syslog(LOG_ERR, "Failed to write file, pool: %d", pool); |
| 169 | exit(EXIT_FAILURE); |
| 170 | } |
| 171 | |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 172 | kvp_release_lock(pool); |
| 173 | } |
| 174 | |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 175 | static void kvp_update_mem_state(int pool) |
| 176 | { |
| 177 | FILE *filep; |
| 178 | size_t records_read = 0; |
| 179 | struct kvp_record *record = kvp_file_info[pool].records; |
| 180 | struct kvp_record *readp; |
| 181 | int num_blocks = kvp_file_info[pool].num_blocks; |
| 182 | int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK; |
| 183 | |
| 184 | kvp_acquire_lock(pool); |
| 185 | |
Tomas Hozza | 8467fdbb | 2013-01-18 15:23:41 +0100 | [diff] [blame] | 186 | filep = fopen(kvp_file_info[pool].fname, "re"); |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 187 | if (!filep) { |
Tomas Hozza | 12e50c3 | 2013-06-17 10:39:44 +0200 | [diff] [blame] | 188 | syslog(LOG_ERR, "Failed to open file, pool: %d; error: %d %s", pool, |
| 189 | errno, strerror(errno)); |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 190 | kvp_release_lock(pool); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 191 | exit(EXIT_FAILURE); |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 192 | } |
Ben Hutchings | 436473b | 2012-09-05 14:37:37 -0700 | [diff] [blame] | 193 | for (;;) { |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 194 | readp = &record[records_read]; |
| 195 | records_read += fread(readp, sizeof(struct kvp_record), |
| 196 | ENTRIES_PER_BLOCK * num_blocks, |
| 197 | filep); |
| 198 | |
Ben Hutchings | 436473b | 2012-09-05 14:37:37 -0700 | [diff] [blame] | 199 | if (ferror(filep)) { |
| 200 | syslog(LOG_ERR, "Failed to read file, pool: %d", pool); |
| 201 | exit(EXIT_FAILURE); |
| 202 | } |
| 203 | |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 204 | if (!feof(filep)) { |
| 205 | /* |
| 206 | * We have more data to read. |
| 207 | */ |
| 208 | num_blocks++; |
| 209 | record = realloc(record, alloc_unit * num_blocks); |
| 210 | |
| 211 | if (record == NULL) { |
| 212 | syslog(LOG_ERR, "malloc failed"); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 213 | exit(EXIT_FAILURE); |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 214 | } |
| 215 | continue; |
| 216 | } |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | kvp_file_info[pool].num_blocks = num_blocks; |
| 221 | kvp_file_info[pool].records = record; |
| 222 | kvp_file_info[pool].num_records = records_read; |
| 223 | |
Ben Hutchings | d5ab482 | 2012-09-05 14:37:35 -0700 | [diff] [blame] | 224 | fclose(filep); |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 225 | kvp_release_lock(pool); |
| 226 | } |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 227 | static int kvp_file_init(void) |
| 228 | { |
K. Y. Srinivasan | 00b8335 | 2012-09-04 14:46:33 -0700 | [diff] [blame] | 229 | int fd; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 230 | FILE *filep; |
| 231 | size_t records_read; |
K. Y. Srinivasan | d0cbc15 | 2012-09-04 14:46:34 -0700 | [diff] [blame] | 232 | char *fname; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 233 | struct kvp_record *record; |
| 234 | struct kvp_record *readp; |
| 235 | int num_blocks; |
| 236 | int i; |
| 237 | int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK; |
| 238 | |
Tomas Hozza | 40424f5 | 2012-11-27 08:56:33 +0100 | [diff] [blame] | 239 | if (access(KVP_CONFIG_LOC, F_OK)) { |
Ben Hutchings | 0bffd25 | 2012-11-27 08:56:34 +0100 | [diff] [blame] | 240 | if (mkdir(KVP_CONFIG_LOC, 0755 /* rwxr-xr-x */)) { |
Tomas Hozza | 12e50c3 | 2013-06-17 10:39:44 +0200 | [diff] [blame] | 241 | syslog(LOG_ERR, "Failed to create '%s'; error: %d %s", KVP_CONFIG_LOC, |
| 242 | errno, strerror(errno)); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 243 | exit(EXIT_FAILURE); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | for (i = 0; i < KVP_POOL_COUNT; i++) { |
| 248 | fname = kvp_file_info[i].fname; |
| 249 | records_read = 0; |
| 250 | num_blocks = 1; |
Tomas Hozza | 40424f5 | 2012-11-27 08:56:33 +0100 | [diff] [blame] | 251 | sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i); |
Tomas Hozza | 8467fdbb | 2013-01-18 15:23:41 +0100 | [diff] [blame] | 252 | fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 253 | |
| 254 | if (fd == -1) |
| 255 | return 1; |
| 256 | |
| 257 | |
Tomas Hozza | 8467fdbb | 2013-01-18 15:23:41 +0100 | [diff] [blame] | 258 | filep = fopen(fname, "re"); |
Tomas Hozza | fca5975 | 2013-05-22 14:54:33 +0200 | [diff] [blame] | 259 | if (!filep) { |
| 260 | close(fd); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 261 | return 1; |
Tomas Hozza | fca5975 | 2013-05-22 14:54:33 +0200 | [diff] [blame] | 262 | } |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 263 | |
| 264 | record = malloc(alloc_unit * num_blocks); |
| 265 | if (record == NULL) { |
| 266 | fclose(filep); |
Tomas Hozza | fca5975 | 2013-05-22 14:54:33 +0200 | [diff] [blame] | 267 | close(fd); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 268 | return 1; |
| 269 | } |
Ben Hutchings | 436473b | 2012-09-05 14:37:37 -0700 | [diff] [blame] | 270 | for (;;) { |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 271 | readp = &record[records_read]; |
| 272 | records_read += fread(readp, sizeof(struct kvp_record), |
| 273 | ENTRIES_PER_BLOCK, |
| 274 | filep); |
| 275 | |
Ben Hutchings | 436473b | 2012-09-05 14:37:37 -0700 | [diff] [blame] | 276 | if (ferror(filep)) { |
| 277 | syslog(LOG_ERR, "Failed to read file, pool: %d", |
| 278 | i); |
| 279 | exit(EXIT_FAILURE); |
| 280 | } |
| 281 | |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 282 | if (!feof(filep)) { |
| 283 | /* |
| 284 | * We have more data to read. |
| 285 | */ |
| 286 | num_blocks++; |
| 287 | record = realloc(record, alloc_unit * |
| 288 | num_blocks); |
| 289 | if (record == NULL) { |
| 290 | fclose(filep); |
Tomas Hozza | fca5975 | 2013-05-22 14:54:33 +0200 | [diff] [blame] | 291 | close(fd); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 292 | return 1; |
| 293 | } |
| 294 | continue; |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | kvp_file_info[i].fd = fd; |
| 299 | kvp_file_info[i].num_blocks = num_blocks; |
| 300 | kvp_file_info[i].records = record; |
| 301 | kvp_file_info[i].num_records = records_read; |
| 302 | fclose(filep); |
| 303 | |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 309 | static int kvp_key_delete(int pool, const __u8 *key, int key_size) |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 310 | { |
| 311 | int i; |
| 312 | int j, k; |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 313 | int num_records; |
| 314 | struct kvp_record *record; |
| 315 | |
| 316 | /* |
| 317 | * First update the in-memory state. |
| 318 | */ |
| 319 | kvp_update_mem_state(pool); |
| 320 | |
| 321 | num_records = kvp_file_info[pool].num_records; |
| 322 | record = kvp_file_info[pool].records; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 323 | |
| 324 | for (i = 0; i < num_records; i++) { |
| 325 | if (memcmp(key, record[i].key, key_size)) |
| 326 | continue; |
| 327 | /* |
| 328 | * Found a match; just move the remaining |
| 329 | * entries up. |
| 330 | */ |
| 331 | if (i == num_records) { |
| 332 | kvp_file_info[pool].num_records--; |
| 333 | kvp_update_file(pool); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | j = i; |
| 338 | k = j + 1; |
| 339 | for (; k < num_records; k++) { |
| 340 | strcpy(record[j].key, record[k].key); |
| 341 | strcpy(record[j].value, record[k].value); |
| 342 | j++; |
| 343 | } |
| 344 | |
| 345 | kvp_file_info[pool].num_records--; |
| 346 | kvp_update_file(pool); |
| 347 | return 0; |
| 348 | } |
| 349 | return 1; |
| 350 | } |
| 351 | |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 352 | static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size, |
| 353 | const __u8 *value, int value_size) |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 354 | { |
| 355 | int i; |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 356 | int num_records; |
| 357 | struct kvp_record *record; |
| 358 | int num_blocks; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 359 | |
| 360 | if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) || |
| 361 | (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) |
| 362 | return 1; |
| 363 | |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 364 | /* |
| 365 | * First update the in-memory state. |
| 366 | */ |
| 367 | kvp_update_mem_state(pool); |
| 368 | |
| 369 | num_records = kvp_file_info[pool].num_records; |
| 370 | record = kvp_file_info[pool].records; |
| 371 | num_blocks = kvp_file_info[pool].num_blocks; |
| 372 | |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 373 | for (i = 0; i < num_records; i++) { |
| 374 | if (memcmp(key, record[i].key, key_size)) |
| 375 | continue; |
| 376 | /* |
| 377 | * Found a match; just update the value - |
| 378 | * this is the modify case. |
| 379 | */ |
| 380 | memcpy(record[i].value, value, value_size); |
| 381 | kvp_update_file(pool); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Need to add a new entry; |
| 387 | */ |
| 388 | if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) { |
| 389 | /* Need to allocate a larger array for reg entries. */ |
| 390 | record = realloc(record, sizeof(struct kvp_record) * |
| 391 | ENTRIES_PER_BLOCK * (num_blocks + 1)); |
| 392 | |
| 393 | if (record == NULL) |
| 394 | return 1; |
| 395 | kvp_file_info[pool].num_blocks++; |
| 396 | |
| 397 | } |
| 398 | memcpy(record[i].value, value, value_size); |
| 399 | memcpy(record[i].key, key, key_size); |
| 400 | kvp_file_info[pool].records = record; |
| 401 | kvp_file_info[pool].num_records++; |
| 402 | kvp_update_file(pool); |
| 403 | return 0; |
| 404 | } |
| 405 | |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 406 | static int kvp_get_value(int pool, const __u8 *key, int key_size, __u8 *value, |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 407 | int value_size) |
| 408 | { |
| 409 | int i; |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 410 | int num_records; |
| 411 | struct kvp_record *record; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 412 | |
| 413 | if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) || |
| 414 | (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) |
| 415 | return 1; |
| 416 | |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 417 | /* |
| 418 | * First update the in-memory state. |
| 419 | */ |
| 420 | kvp_update_mem_state(pool); |
| 421 | |
| 422 | num_records = kvp_file_info[pool].num_records; |
| 423 | record = kvp_file_info[pool].records; |
| 424 | |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 425 | for (i = 0; i < num_records; i++) { |
| 426 | if (memcmp(key, record[i].key, key_size)) |
| 427 | continue; |
| 428 | /* |
| 429 | * Found a match; just copy the value out. |
| 430 | */ |
| 431 | memcpy(value, record[i].value, value_size); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | return 1; |
| 436 | } |
| 437 | |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 438 | static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size, |
| 439 | __u8 *value, int value_size) |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 440 | { |
| 441 | struct kvp_record *record; |
| 442 | |
| 443 | /* |
| 444 | * First update our in-memory database. |
| 445 | */ |
| 446 | kvp_update_mem_state(pool); |
| 447 | record = kvp_file_info[pool].records; |
| 448 | |
| 449 | if (index >= kvp_file_info[pool].num_records) { |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 450 | return 1; |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | memcpy(key, record[index].key, key_size); |
| 454 | memcpy(value, record[index].value, value_size); |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 455 | return 0; |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 459 | void kvp_get_os_info(void) |
| 460 | { |
| 461 | FILE *file; |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 462 | char *p, buf[512]; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 463 | |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 464 | uname(&uts_buf); |
K. Y. Srinivasan | f426a36 | 2012-10-25 14:15:49 -0700 | [diff] [blame] | 465 | os_version = uts_buf.release; |
| 466 | os_build = strdup(uts_buf.release); |
| 467 | |
Ben Hutchings | 44c8b25 | 2012-09-06 13:32:14 -0700 | [diff] [blame] | 468 | os_name = uts_buf.sysname; |
K. Y. Srinivasan | 064931d | 2011-07-19 11:44:20 -0700 | [diff] [blame] | 469 | processor_arch = uts_buf.machine; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 470 | |
K. Y. Srinivasan | e54bbc6 | 2011-07-22 10:14:31 -0700 | [diff] [blame] | 471 | /* |
| 472 | * The current windows host (win7) expects the build |
| 473 | * string to be of the form: x.y.z |
| 474 | * Strip additional information we may have. |
| 475 | */ |
K. Y. Srinivasan | f426a36 | 2012-10-25 14:15:49 -0700 | [diff] [blame] | 476 | p = strchr(os_version, '-'); |
K. Y. Srinivasan | e54bbc6 | 2011-07-22 10:14:31 -0700 | [diff] [blame] | 477 | if (p) |
| 478 | *p = '\0'; |
| 479 | |
Ben Hutchings | 44c8b25 | 2012-09-06 13:32:14 -0700 | [diff] [blame] | 480 | /* |
| 481 | * Parse the /etc/os-release file if present: |
| 482 | * http://www.freedesktop.org/software/systemd/man/os-release.html |
| 483 | */ |
| 484 | file = fopen("/etc/os-release", "r"); |
| 485 | if (file != NULL) { |
| 486 | while (fgets(buf, sizeof(buf), file)) { |
| 487 | char *value, *q; |
| 488 | |
| 489 | /* Ignore comments */ |
| 490 | if (buf[0] == '#') |
| 491 | continue; |
| 492 | |
| 493 | /* Split into name=value */ |
| 494 | p = strchr(buf, '='); |
| 495 | if (!p) |
| 496 | continue; |
| 497 | *p++ = 0; |
| 498 | |
| 499 | /* Remove quotes and newline; un-escape */ |
| 500 | value = p; |
| 501 | q = p; |
| 502 | while (*p) { |
| 503 | if (*p == '\\') { |
| 504 | ++p; |
| 505 | if (!*p) |
| 506 | break; |
| 507 | *q++ = *p++; |
| 508 | } else if (*p == '\'' || *p == '"' || |
| 509 | *p == '\n') { |
| 510 | ++p; |
| 511 | } else { |
| 512 | *q++ = *p++; |
| 513 | } |
| 514 | } |
| 515 | *q = 0; |
| 516 | |
| 517 | if (!strcmp(buf, "NAME")) { |
| 518 | p = strdup(value); |
| 519 | if (!p) |
| 520 | break; |
| 521 | os_name = p; |
| 522 | } else if (!strcmp(buf, "VERSION_ID")) { |
| 523 | p = strdup(value); |
| 524 | if (!p) |
| 525 | break; |
| 526 | os_major = p; |
| 527 | } |
| 528 | } |
| 529 | fclose(file); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | /* Fallback for older RH/SUSE releases */ |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 534 | file = fopen("/etc/SuSE-release", "r"); |
| 535 | if (file != NULL) |
| 536 | goto kvp_osinfo_found; |
| 537 | file = fopen("/etc/redhat-release", "r"); |
| 538 | if (file != NULL) |
| 539 | goto kvp_osinfo_found; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 540 | |
| 541 | /* |
| 542 | * We don't have information about the os. |
| 543 | */ |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 544 | return; |
| 545 | |
| 546 | kvp_osinfo_found: |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 547 | /* up to three lines */ |
| 548 | p = fgets(buf, sizeof(buf), file); |
| 549 | if (p) { |
| 550 | p = strchr(buf, '\n'); |
| 551 | if (p) |
| 552 | *p = '\0'; |
| 553 | p = strdup(buf); |
| 554 | if (!p) |
| 555 | goto done; |
| 556 | os_name = p; |
| 557 | |
| 558 | /* second line */ |
| 559 | p = fgets(buf, sizeof(buf), file); |
| 560 | if (p) { |
| 561 | p = strchr(buf, '\n'); |
| 562 | if (p) |
| 563 | *p = '\0'; |
| 564 | p = strdup(buf); |
| 565 | if (!p) |
| 566 | goto done; |
| 567 | os_major = p; |
| 568 | |
| 569 | /* third line */ |
| 570 | p = fgets(buf, sizeof(buf), file); |
| 571 | if (p) { |
| 572 | p = strchr(buf, '\n'); |
| 573 | if (p) |
| 574 | *p = '\0'; |
| 575 | p = strdup(buf); |
| 576 | if (p) |
| 577 | os_minor = p; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | done: |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 583 | fclose(file); |
| 584 | return; |
| 585 | } |
| 586 | |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 587 | |
| 588 | |
| 589 | /* |
| 590 | * Retrieve an interface name corresponding to the specified guid. |
| 591 | * If there is a match, the function returns a pointer |
| 592 | * to the interface name and if not, a NULL is returned. |
| 593 | * If a match is found, the caller is responsible for |
| 594 | * freeing the memory. |
| 595 | */ |
| 596 | |
| 597 | static char *kvp_get_if_name(char *guid) |
| 598 | { |
| 599 | DIR *dir; |
| 600 | struct dirent *entry; |
| 601 | FILE *file; |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 602 | char *p, *x; |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 603 | char *if_name = NULL; |
| 604 | char buf[256]; |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 605 | char dev_id[PATH_MAX]; |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 606 | |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 607 | dir = opendir(KVP_NET_DIR); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 608 | if (dir == NULL) |
| 609 | return NULL; |
| 610 | |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 611 | while ((entry = readdir(dir)) != NULL) { |
| 612 | /* |
| 613 | * Set the state for the next pass. |
| 614 | */ |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 615 | snprintf(dev_id, sizeof(dev_id), "%s%s/device/device_id", |
| 616 | KVP_NET_DIR, entry->d_name); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 617 | |
| 618 | file = fopen(dev_id, "r"); |
| 619 | if (file == NULL) |
| 620 | continue; |
| 621 | |
| 622 | p = fgets(buf, sizeof(buf), file); |
| 623 | if (p) { |
| 624 | x = strchr(p, '\n'); |
| 625 | if (x) |
| 626 | *x = '\0'; |
| 627 | |
| 628 | if (!strcmp(p, guid)) { |
| 629 | /* |
| 630 | * Found the guid match; return the interface |
| 631 | * name. The caller will free the memory. |
| 632 | */ |
| 633 | if_name = strdup(entry->d_name); |
| 634 | fclose(file); |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | fclose(file); |
| 639 | } |
| 640 | |
| 641 | closedir(dir); |
| 642 | return if_name; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Retrieve the MAC address given the interface name. |
| 647 | */ |
| 648 | |
| 649 | static char *kvp_if_name_to_mac(char *if_name) |
| 650 | { |
| 651 | FILE *file; |
| 652 | char *p, *x; |
| 653 | char buf[256]; |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 654 | char addr_file[PATH_MAX]; |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 655 | unsigned int i; |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 656 | char *mac_addr = NULL; |
| 657 | |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 658 | snprintf(addr_file, sizeof(addr_file), "%s%s%s", KVP_NET_DIR, |
| 659 | if_name, "/address"); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 660 | |
| 661 | file = fopen(addr_file, "r"); |
| 662 | if (file == NULL) |
| 663 | return NULL; |
| 664 | |
| 665 | p = fgets(buf, sizeof(buf), file); |
| 666 | if (p) { |
| 667 | x = strchr(p, '\n'); |
| 668 | if (x) |
| 669 | *x = '\0'; |
| 670 | for (i = 0; i < strlen(p); i++) |
| 671 | p[i] = toupper(p[i]); |
| 672 | mac_addr = strdup(p); |
| 673 | } |
| 674 | |
| 675 | fclose(file); |
| 676 | return mac_addr; |
| 677 | } |
| 678 | |
| 679 | |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 680 | /* |
| 681 | * Retrieve the interface name given tha MAC address. |
| 682 | */ |
| 683 | |
| 684 | static char *kvp_mac_to_if_name(char *mac) |
| 685 | { |
| 686 | DIR *dir; |
| 687 | struct dirent *entry; |
| 688 | FILE *file; |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 689 | char *p, *x; |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 690 | char *if_name = NULL; |
| 691 | char buf[256]; |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 692 | char dev_id[PATH_MAX]; |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 693 | unsigned int i; |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 694 | |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 695 | dir = opendir(KVP_NET_DIR); |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 696 | if (dir == NULL) |
| 697 | return NULL; |
| 698 | |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 699 | while ((entry = readdir(dir)) != NULL) { |
| 700 | /* |
| 701 | * Set the state for the next pass. |
| 702 | */ |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 703 | snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR, |
| 704 | entry->d_name); |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 705 | |
| 706 | file = fopen(dev_id, "r"); |
| 707 | if (file == NULL) |
| 708 | continue; |
| 709 | |
| 710 | p = fgets(buf, sizeof(buf), file); |
| 711 | if (p) { |
| 712 | x = strchr(p, '\n'); |
| 713 | if (x) |
| 714 | *x = '\0'; |
| 715 | |
| 716 | for (i = 0; i < strlen(p); i++) |
| 717 | p[i] = toupper(p[i]); |
| 718 | |
| 719 | if (!strcmp(p, mac)) { |
| 720 | /* |
| 721 | * Found the MAC match; return the interface |
| 722 | * name. The caller will free the memory. |
| 723 | */ |
| 724 | if_name = strdup(entry->d_name); |
| 725 | fclose(file); |
| 726 | break; |
| 727 | } |
| 728 | } |
| 729 | fclose(file); |
| 730 | } |
| 731 | |
| 732 | closedir(dir); |
| 733 | return if_name; |
| 734 | } |
| 735 | |
| 736 | |
K. Y. Srinivasan | 4a52c4a | 2012-08-16 18:32:18 -0700 | [diff] [blame] | 737 | static void kvp_process_ipconfig_file(char *cmd, |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 738 | char *config_buf, unsigned int len, |
K. Y. Srinivasan | 4a52c4a | 2012-08-16 18:32:18 -0700 | [diff] [blame] | 739 | int element_size, int offset) |
| 740 | { |
| 741 | char buf[256]; |
| 742 | char *p; |
| 743 | char *x; |
| 744 | FILE *file; |
| 745 | |
| 746 | /* |
| 747 | * First execute the command. |
| 748 | */ |
| 749 | file = popen(cmd, "r"); |
| 750 | if (file == NULL) |
| 751 | return; |
| 752 | |
| 753 | if (offset == 0) |
| 754 | memset(config_buf, 0, len); |
| 755 | while ((p = fgets(buf, sizeof(buf), file)) != NULL) { |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 756 | if (len < strlen(config_buf) + element_size + 1) |
K. Y. Srinivasan | 4a52c4a | 2012-08-16 18:32:18 -0700 | [diff] [blame] | 757 | break; |
| 758 | |
| 759 | x = strchr(p, '\n'); |
Tomas Hozza | f14e600 | 2013-05-22 14:54:32 +0200 | [diff] [blame] | 760 | if (x) |
| 761 | *x = '\0'; |
| 762 | |
K. Y. Srinivasan | 4a52c4a | 2012-08-16 18:32:18 -0700 | [diff] [blame] | 763 | strcat(config_buf, p); |
| 764 | strcat(config_buf, ";"); |
| 765 | } |
| 766 | pclose(file); |
| 767 | } |
| 768 | |
| 769 | static void kvp_get_ipconfig_info(char *if_name, |
| 770 | struct hv_kvp_ipaddr_value *buffer) |
| 771 | { |
| 772 | char cmd[512]; |
K. Y. Srinivasan | c050372 | 2012-09-05 13:50:11 -0700 | [diff] [blame] | 773 | char dhcp_info[128]; |
| 774 | char *p; |
| 775 | FILE *file; |
K. Y. Srinivasan | 4a52c4a | 2012-08-16 18:32:18 -0700 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * Get the address of default gateway (ipv4). |
| 779 | */ |
| 780 | sprintf(cmd, "%s %s", "ip route show dev", if_name); |
| 781 | strcat(cmd, " | awk '/default/ {print $3 }'"); |
| 782 | |
| 783 | /* |
| 784 | * Execute the command to gather gateway info. |
| 785 | */ |
| 786 | kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way, |
| 787 | (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0); |
| 788 | |
| 789 | /* |
| 790 | * Get the address of default gateway (ipv6). |
| 791 | */ |
| 792 | sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name); |
| 793 | strcat(cmd, " | awk '/default/ {print $3 }'"); |
| 794 | |
| 795 | /* |
| 796 | * Execute the command to gather gateway info (ipv6). |
| 797 | */ |
| 798 | kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way, |
| 799 | (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1); |
| 800 | |
K. Y. Srinivasan | 9692988 | 2012-09-04 14:46:36 -0700 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * Gather the DNS state. |
| 804 | * Since there is no standard way to get this information |
| 805 | * across various distributions of interest; we just invoke |
| 806 | * an external script that needs to be ported across distros |
| 807 | * of interest. |
| 808 | * |
| 809 | * Following is the expected format of the information from the script: |
| 810 | * |
| 811 | * ipaddr1 (nameserver1) |
| 812 | * ipaddr2 (nameserver2) |
| 813 | * . |
| 814 | * . |
| 815 | */ |
| 816 | |
Alex Fluter | 2eb72d4 | 2016-12-03 12:34:41 -0800 | [diff] [blame] | 817 | sprintf(cmd, KVP_SCRIPTS_PATH "%s", "hv_get_dns_info"); |
K. Y. Srinivasan | 9692988 | 2012-09-04 14:46:36 -0700 | [diff] [blame] | 818 | |
| 819 | /* |
| 820 | * Execute the command to gather DNS info. |
| 821 | */ |
| 822 | kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr, |
| 823 | (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0); |
K. Y. Srinivasan | c050372 | 2012-09-05 13:50:11 -0700 | [diff] [blame] | 824 | |
| 825 | /* |
| 826 | * Gather the DHCP state. |
| 827 | * We will gather this state by invoking an external script. |
| 828 | * The parameter to the script is the interface name. |
| 829 | * Here is the expected output: |
| 830 | * |
| 831 | * Enabled: DHCP enabled. |
| 832 | */ |
| 833 | |
Alex Fluter | 2eb72d4 | 2016-12-03 12:34:41 -0800 | [diff] [blame] | 834 | sprintf(cmd, KVP_SCRIPTS_PATH "%s %s", "hv_get_dhcp_info", if_name); |
K. Y. Srinivasan | c050372 | 2012-09-05 13:50:11 -0700 | [diff] [blame] | 835 | |
| 836 | file = popen(cmd, "r"); |
| 837 | if (file == NULL) |
| 838 | return; |
| 839 | |
| 840 | p = fgets(dhcp_info, sizeof(dhcp_info), file); |
| 841 | if (p == NULL) { |
| 842 | pclose(file); |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | if (!strncmp(p, "Enabled", 7)) |
| 847 | buffer->dhcp_enabled = 1; |
| 848 | else |
| 849 | buffer->dhcp_enabled = 0; |
| 850 | |
| 851 | pclose(file); |
K. Y. Srinivasan | 4a52c4a | 2012-08-16 18:32:18 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 855 | static unsigned int hweight32(unsigned int *w) |
| 856 | { |
| 857 | unsigned int res = *w - ((*w >> 1) & 0x55555555); |
| 858 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
| 859 | res = (res + (res >> 4)) & 0x0F0F0F0F; |
| 860 | res = res + (res >> 8); |
| 861 | return (res + (res >> 16)) & 0x000000FF; |
| 862 | } |
| 863 | |
K. Y. Srinivasan | af73301 | 2012-08-16 18:32:14 -0700 | [diff] [blame] | 864 | static int kvp_process_ip_address(void *addrp, |
| 865 | int family, char *buffer, |
| 866 | int length, int *offset) |
| 867 | { |
| 868 | struct sockaddr_in *addr; |
| 869 | struct sockaddr_in6 *addr6; |
| 870 | int addr_length; |
| 871 | char tmp[50]; |
| 872 | const char *str; |
| 873 | |
| 874 | if (family == AF_INET) { |
| 875 | addr = (struct sockaddr_in *)addrp; |
| 876 | str = inet_ntop(family, &addr->sin_addr, tmp, 50); |
| 877 | addr_length = INET_ADDRSTRLEN; |
| 878 | } else { |
| 879 | addr6 = (struct sockaddr_in6 *)addrp; |
| 880 | str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50); |
| 881 | addr_length = INET6_ADDRSTRLEN; |
| 882 | } |
| 883 | |
K. Y. Srinivasan | 3321e73 | 2012-10-25 14:15:50 -0700 | [diff] [blame] | 884 | if ((length - *offset) < addr_length + 2) |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 885 | return HV_E_FAIL; |
K. Y. Srinivasan | af73301 | 2012-08-16 18:32:14 -0700 | [diff] [blame] | 886 | if (str == NULL) { |
| 887 | strcpy(buffer, "inet_ntop failed\n"); |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 888 | return HV_E_FAIL; |
K. Y. Srinivasan | af73301 | 2012-08-16 18:32:14 -0700 | [diff] [blame] | 889 | } |
| 890 | if (*offset == 0) |
| 891 | strcpy(buffer, tmp); |
K. Y. Srinivasan | 3321e73 | 2012-10-25 14:15:50 -0700 | [diff] [blame] | 892 | else { |
| 893 | strcat(buffer, ";"); |
K. Y. Srinivasan | af73301 | 2012-08-16 18:32:14 -0700 | [diff] [blame] | 894 | strcat(buffer, tmp); |
K. Y. Srinivasan | 3321e73 | 2012-10-25 14:15:50 -0700 | [diff] [blame] | 895 | } |
K. Y. Srinivasan | af73301 | 2012-08-16 18:32:14 -0700 | [diff] [blame] | 896 | |
| 897 | *offset += strlen(str) + 1; |
K. Y. Srinivasan | 3321e73 | 2012-10-25 14:15:50 -0700 | [diff] [blame] | 898 | |
K. Y. Srinivasan | af73301 | 2012-08-16 18:32:14 -0700 | [diff] [blame] | 899 | return 0; |
| 900 | } |
| 901 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 902 | static int |
K. Y. Srinivasan | 4a3b97e5 | 2012-09-05 13:50:14 -0700 | [diff] [blame] | 903 | kvp_get_ip_info(int family, char *if_name, int op, |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 904 | void *out_buffer, unsigned int length) |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 905 | { |
| 906 | struct ifaddrs *ifap; |
| 907 | struct ifaddrs *curp; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 908 | int offset = 0; |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 909 | int sn_offset = 0; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 910 | int error = 0; |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 911 | char *buffer; |
| 912 | struct hv_kvp_ipaddr_value *ip_buffer; |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 913 | char cidr_mask[5]; /* /xyz */ |
| 914 | int weight; |
| 915 | int i; |
| 916 | unsigned int *w; |
| 917 | char *sn_str; |
| 918 | struct sockaddr_in6 *addr6; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 919 | |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 920 | if (op == KVP_OP_ENUMERATE) { |
| 921 | buffer = out_buffer; |
| 922 | } else { |
| 923 | ip_buffer = out_buffer; |
| 924 | buffer = (char *)ip_buffer->ip_addr; |
| 925 | ip_buffer->addr_family = 0; |
| 926 | } |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 927 | /* |
| 928 | * On entry into this function, the buffer is capable of holding the |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 929 | * maximum key value. |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 930 | */ |
| 931 | |
| 932 | if (getifaddrs(&ifap)) { |
| 933 | strcpy(buffer, "getifaddrs failed\n"); |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 934 | return HV_E_FAIL; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | curp = ifap; |
| 938 | while (curp != NULL) { |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 939 | if (curp->ifa_addr == NULL) { |
| 940 | curp = curp->ifa_next; |
| 941 | continue; |
| 942 | } |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 943 | |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 944 | if ((if_name != NULL) && |
| 945 | (strncmp(curp->ifa_name, if_name, strlen(if_name)))) { |
| 946 | /* |
| 947 | * We want info about a specific interface; |
| 948 | * just continue. |
| 949 | */ |
| 950 | curp = curp->ifa_next; |
| 951 | continue; |
| 952 | } |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 953 | |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 954 | /* |
| 955 | * We only support two address families: AF_INET and AF_INET6. |
| 956 | * If a family value of 0 is specified, we collect both |
| 957 | * supported address families; if not we gather info on |
| 958 | * the specified address family. |
| 959 | */ |
K. Y. Srinivasan | 3321e73 | 2012-10-25 14:15:50 -0700 | [diff] [blame] | 960 | if ((((family != 0) && |
| 961 | (curp->ifa_addr->sa_family != family))) || |
| 962 | (curp->ifa_flags & IFF_LOOPBACK)) { |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 963 | curp = curp->ifa_next; |
| 964 | continue; |
| 965 | } |
| 966 | if ((curp->ifa_addr->sa_family != AF_INET) && |
| 967 | (curp->ifa_addr->sa_family != AF_INET6)) { |
| 968 | curp = curp->ifa_next; |
| 969 | continue; |
| 970 | } |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 971 | |
K. Y. Srinivasan | 0d5b6b1 | 2012-08-16 18:32:15 -0700 | [diff] [blame] | 972 | if (op == KVP_OP_GET_IP_INFO) { |
| 973 | /* |
| 974 | * Gather info other than the IP address. |
| 975 | * IP address info will be gathered later. |
| 976 | */ |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 977 | if (curp->ifa_addr->sa_family == AF_INET) { |
K. Y. Srinivasan | 0d5b6b1 | 2012-08-16 18:32:15 -0700 | [diff] [blame] | 978 | ip_buffer->addr_family |= ADDR_FAMILY_IPV4; |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 979 | /* |
| 980 | * Get subnet info. |
| 981 | */ |
| 982 | error = kvp_process_ip_address( |
| 983 | curp->ifa_netmask, |
| 984 | AF_INET, |
| 985 | (char *) |
| 986 | ip_buffer->sub_net, |
| 987 | length, |
| 988 | &sn_offset); |
| 989 | if (error) |
| 990 | goto gather_ipaddr; |
| 991 | } else { |
K. Y. Srinivasan | 0d5b6b1 | 2012-08-16 18:32:15 -0700 | [diff] [blame] | 992 | ip_buffer->addr_family |= ADDR_FAMILY_IPV6; |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 993 | |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 994 | /* |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 995 | * Get subnet info in CIDR format. |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 996 | */ |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 997 | weight = 0; |
| 998 | sn_str = (char *)ip_buffer->sub_net; |
| 999 | addr6 = (struct sockaddr_in6 *) |
| 1000 | curp->ifa_netmask; |
| 1001 | w = addr6->sin6_addr.s6_addr32; |
| 1002 | |
| 1003 | for (i = 0; i < 4; i++) |
| 1004 | weight += hweight32(&w[i]); |
| 1005 | |
| 1006 | sprintf(cidr_mask, "/%d", weight); |
Vitaly Kuznetsov | 69258c0 | 2015-01-09 22:18:53 -0800 | [diff] [blame] | 1007 | if (length < sn_offset + strlen(cidr_mask) + 1) |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 1008 | goto gather_ipaddr; |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 1009 | |
| 1010 | if (sn_offset == 0) |
| 1011 | strcpy(sn_str, cidr_mask); |
K. Y. Srinivasan | ed4bb97 | 2013-07-11 12:03:31 -0700 | [diff] [blame] | 1012 | else { |
| 1013 | strcat((char *)ip_buffer->sub_net, ";"); |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 1014 | strcat(sn_str, cidr_mask); |
K. Y. Srinivasan | ed4bb97 | 2013-07-11 12:03:31 -0700 | [diff] [blame] | 1015 | } |
K. Y. Srinivasan | 6a60a6a | 2012-08-16 18:32:17 -0700 | [diff] [blame] | 1016 | sn_offset += strlen(sn_str) + 1; |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 1017 | } |
K. Y. Srinivasan | 4a52c4a | 2012-08-16 18:32:18 -0700 | [diff] [blame] | 1018 | |
| 1019 | /* |
| 1020 | * Collect other ip related configuration info. |
| 1021 | */ |
| 1022 | |
| 1023 | kvp_get_ipconfig_info(if_name, ip_buffer); |
K. Y. Srinivasan | 0d5b6b1 | 2012-08-16 18:32:15 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
K. Y. Srinivasan | 0440578 | 2012-08-16 18:32:16 -0700 | [diff] [blame] | 1026 | gather_ipaddr: |
K. Y. Srinivasan | af73301 | 2012-08-16 18:32:14 -0700 | [diff] [blame] | 1027 | error = kvp_process_ip_address(curp->ifa_addr, |
| 1028 | curp->ifa_addr->sa_family, |
| 1029 | buffer, |
| 1030 | length, &offset); |
| 1031 | if (error) |
| 1032 | goto getaddr_done; |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 1033 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1034 | curp = curp->ifa_next; |
| 1035 | } |
| 1036 | |
| 1037 | getaddr_done: |
| 1038 | freeifaddrs(ifap); |
| 1039 | return error; |
| 1040 | } |
| 1041 | |
| 1042 | |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1043 | static int expand_ipv6(char *addr, int type) |
| 1044 | { |
| 1045 | int ret; |
| 1046 | struct in6_addr v6_addr; |
| 1047 | |
| 1048 | ret = inet_pton(AF_INET6, addr, &v6_addr); |
| 1049 | |
| 1050 | if (ret != 1) { |
| 1051 | if (type == NETMASK) |
| 1052 | return 1; |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:" |
| 1057 | "%02x%02x:%02x%02x:%02x%02x", |
| 1058 | (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1], |
| 1059 | (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3], |
| 1060 | (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5], |
| 1061 | (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7], |
| 1062 | (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9], |
| 1063 | (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11], |
| 1064 | (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13], |
| 1065 | (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]); |
| 1066 | |
| 1067 | return 1; |
| 1068 | |
| 1069 | } |
| 1070 | |
| 1071 | static int is_ipv4(char *addr) |
| 1072 | { |
| 1073 | int ret; |
| 1074 | struct in_addr ipv4_addr; |
| 1075 | |
| 1076 | ret = inet_pton(AF_INET, addr, &ipv4_addr); |
| 1077 | |
| 1078 | if (ret == 1) |
| 1079 | return 1; |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | static int parse_ip_val_buffer(char *in_buf, int *offset, |
| 1084 | char *out_buf, int out_len) |
| 1085 | { |
| 1086 | char *x; |
| 1087 | char *start; |
| 1088 | |
| 1089 | /* |
| 1090 | * in_buf has sequence of characters that are seperated by |
| 1091 | * the character ';'. The last sequence does not have the |
| 1092 | * terminating ";" character. |
| 1093 | */ |
| 1094 | start = in_buf + *offset; |
| 1095 | |
| 1096 | x = strchr(start, ';'); |
| 1097 | if (x) |
| 1098 | *x = 0; |
| 1099 | else |
| 1100 | x = start + strlen(start); |
| 1101 | |
| 1102 | if (strlen(start) != 0) { |
| 1103 | int i = 0; |
| 1104 | /* |
| 1105 | * Get rid of leading spaces. |
| 1106 | */ |
| 1107 | while (start[i] == ' ') |
| 1108 | i++; |
| 1109 | |
| 1110 | if ((x - start) <= out_len) { |
| 1111 | strcpy(out_buf, (start + i)); |
| 1112 | *offset += (x - start) + 1; |
| 1113 | return 1; |
| 1114 | } |
| 1115 | } |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3) |
| 1120 | { |
| 1121 | int ret; |
| 1122 | |
| 1123 | ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3); |
| 1124 | |
| 1125 | if (ret < 0) |
| 1126 | return HV_E_FAIL; |
| 1127 | |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | static int process_ip_string(FILE *f, char *ip_string, int type) |
| 1133 | { |
| 1134 | int error = 0; |
| 1135 | char addr[INET6_ADDRSTRLEN]; |
| 1136 | int i = 0; |
| 1137 | int j = 0; |
| 1138 | char str[256]; |
| 1139 | char sub_str[10]; |
| 1140 | int offset = 0; |
| 1141 | |
| 1142 | memset(addr, 0, sizeof(addr)); |
| 1143 | |
| 1144 | while (parse_ip_val_buffer(ip_string, &offset, addr, |
| 1145 | (MAX_IP_ADDR_SIZE * 2))) { |
| 1146 | |
| 1147 | sub_str[0] = 0; |
| 1148 | if (is_ipv4(addr)) { |
| 1149 | switch (type) { |
| 1150 | case IPADDR: |
| 1151 | snprintf(str, sizeof(str), "%s", "IPADDR"); |
| 1152 | break; |
| 1153 | case NETMASK: |
| 1154 | snprintf(str, sizeof(str), "%s", "NETMASK"); |
| 1155 | break; |
| 1156 | case GATEWAY: |
| 1157 | snprintf(str, sizeof(str), "%s", "GATEWAY"); |
| 1158 | break; |
| 1159 | case DNS: |
| 1160 | snprintf(str, sizeof(str), "%s", "DNS"); |
| 1161 | break; |
| 1162 | } |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1163 | |
| 1164 | if (type == DNS) { |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1165 | snprintf(sub_str, sizeof(sub_str), "%d", ++i); |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1166 | } else if (type == GATEWAY && i == 0) { |
| 1167 | ++i; |
| 1168 | } else { |
| 1169 | snprintf(sub_str, sizeof(sub_str), "%d", i++); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | } else if (expand_ipv6(addr, type)) { |
| 1174 | switch (type) { |
| 1175 | case IPADDR: |
| 1176 | snprintf(str, sizeof(str), "%s", "IPV6ADDR"); |
| 1177 | break; |
| 1178 | case NETMASK: |
| 1179 | snprintf(str, sizeof(str), "%s", "IPV6NETMASK"); |
| 1180 | break; |
| 1181 | case GATEWAY: |
| 1182 | snprintf(str, sizeof(str), "%s", |
| 1183 | "IPV6_DEFAULTGW"); |
| 1184 | break; |
| 1185 | case DNS: |
| 1186 | snprintf(str, sizeof(str), "%s", "DNS"); |
| 1187 | break; |
| 1188 | } |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1189 | |
| 1190 | if (type == DNS) { |
| 1191 | snprintf(sub_str, sizeof(sub_str), "%d", ++i); |
| 1192 | } else if (j == 0) { |
| 1193 | ++j; |
| 1194 | } else { |
| 1195 | snprintf(sub_str, sizeof(sub_str), "_%d", j++); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1196 | } |
| 1197 | } else { |
| 1198 | return HV_INVALIDARG; |
| 1199 | } |
| 1200 | |
| 1201 | error = kvp_write_file(f, str, sub_str, addr); |
| 1202 | if (error) |
| 1203 | return error; |
| 1204 | memset(addr, 0, sizeof(addr)); |
| 1205 | } |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val) |
| 1211 | { |
| 1212 | int error = 0; |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 1213 | char if_file[PATH_MAX]; |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1214 | FILE *file; |
Vitaly Kuznetsov | a1a7ea6 | 2017-04-30 16:21:15 -0700 | [diff] [blame^] | 1215 | char cmd[PATH_MAX]; |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1216 | char *mac_addr; |
| 1217 | |
| 1218 | /* |
| 1219 | * Set the configuration for the specified interface with |
| 1220 | * the information provided. Since there is no standard |
| 1221 | * way to configure an interface, we will have an external |
| 1222 | * script that does the job of configuring the interface and |
| 1223 | * flushing the configuration. |
| 1224 | * |
| 1225 | * The parameters passed to this external script are: |
| 1226 | * 1. A configuration file that has the specified configuration. |
| 1227 | * |
| 1228 | * We will embed the name of the interface in the configuration |
| 1229 | * file: ifcfg-ethx (where ethx is the interface name). |
| 1230 | * |
| 1231 | * The information provided here may be more than what is needed |
| 1232 | * in a given distro to configure the interface and so are free |
| 1233 | * ignore information that may not be relevant. |
| 1234 | * |
| 1235 | * Here is the format of the ip configuration file: |
| 1236 | * |
| 1237 | * HWADDR=macaddr |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1238 | * DEVICE=interface name |
| 1239 | * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured |
| 1240 | * or "none" if no boot-time protocol should be used) |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1241 | * |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1242 | * IPADDR0=ipaddr1 |
| 1243 | * IPADDR1=ipaddr2 |
| 1244 | * IPADDRx=ipaddry (where y = x + 1) |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1245 | * |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1246 | * NETMASK0=netmask1 |
| 1247 | * NETMASKx=netmasky (where y = x + 1) |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1248 | * |
| 1249 | * GATEWAY=ipaddr1 |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1250 | * GATEWAYx=ipaddry (where y = x + 1) |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1251 | * |
| 1252 | * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc) |
| 1253 | * |
| 1254 | * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be |
| 1255 | * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as |
| 1256 | * IPV6NETMASK. |
| 1257 | * |
| 1258 | * The host can specify multiple ipv4 and ipv6 addresses to be |
| 1259 | * configured for the interface. Furthermore, the configuration |
| 1260 | * needs to be persistent. A subsequent GET call on the interface |
| 1261 | * is expected to return the configuration that is set via the SET |
| 1262 | * call. |
| 1263 | */ |
| 1264 | |
| 1265 | snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC, |
Tomas Hozza | 40424f5 | 2012-11-27 08:56:33 +0100 | [diff] [blame] | 1266 | "/ifcfg-", if_name); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1267 | |
| 1268 | file = fopen(if_file, "w"); |
| 1269 | |
| 1270 | if (file == NULL) { |
Tomas Hozza | 12e50c3 | 2013-06-17 10:39:44 +0200 | [diff] [blame] | 1271 | syslog(LOG_ERR, "Failed to open config file; error: %d %s", |
| 1272 | errno, strerror(errno)); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1273 | return HV_E_FAIL; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * First write out the MAC address. |
| 1278 | */ |
| 1279 | |
| 1280 | mac_addr = kvp_if_name_to_mac(if_name); |
| 1281 | if (mac_addr == NULL) { |
| 1282 | error = HV_E_FAIL; |
| 1283 | goto setval_error; |
| 1284 | } |
| 1285 | |
| 1286 | error = kvp_write_file(file, "HWADDR", "", mac_addr); |
Olaf Hering | 57969af | 2013-08-04 16:41:24 +0200 | [diff] [blame] | 1287 | free(mac_addr); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1288 | if (error) |
| 1289 | goto setval_error; |
| 1290 | |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1291 | error = kvp_write_file(file, "DEVICE", "", if_name); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1292 | if (error) |
| 1293 | goto setval_error; |
| 1294 | |
Dexuan Cui | 787d618 | 2014-12-10 03:33:20 -0800 | [diff] [blame] | 1295 | /* |
| 1296 | * The dhcp_enabled flag is only for IPv4. In the case the host only |
| 1297 | * injects an IPv6 address, the flag is true, but we still need to |
| 1298 | * proceed to parse and pass the IPv6 information to the |
| 1299 | * disto-specific script hv_set_ifconfig. |
| 1300 | */ |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1301 | if (new_val->dhcp_enabled) { |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1302 | error = kvp_write_file(file, "BOOTPROTO", "", "dhcp"); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1303 | if (error) |
| 1304 | goto setval_error; |
| 1305 | |
Tomas Hozza | 0783d72 | 2013-01-13 22:27:40 +0100 | [diff] [blame] | 1306 | } else { |
| 1307 | error = kvp_write_file(file, "BOOTPROTO", "", "none"); |
| 1308 | if (error) |
| 1309 | goto setval_error; |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * Write the configuration for ipaddress, netmask, gateway and |
| 1314 | * name servers. |
| 1315 | */ |
| 1316 | |
| 1317 | error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR); |
| 1318 | if (error) |
| 1319 | goto setval_error; |
| 1320 | |
| 1321 | error = process_ip_string(file, (char *)new_val->sub_net, NETMASK); |
| 1322 | if (error) |
| 1323 | goto setval_error; |
| 1324 | |
| 1325 | error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY); |
| 1326 | if (error) |
| 1327 | goto setval_error; |
| 1328 | |
| 1329 | error = process_ip_string(file, (char *)new_val->dns_addr, DNS); |
| 1330 | if (error) |
| 1331 | goto setval_error; |
| 1332 | |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1333 | fclose(file); |
| 1334 | |
| 1335 | /* |
| 1336 | * Now that we have populated the configuration file, |
| 1337 | * invoke the external script to do its magic. |
| 1338 | */ |
| 1339 | |
Alex Fluter | 2eb72d4 | 2016-12-03 12:34:41 -0800 | [diff] [blame] | 1340 | snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s", |
| 1341 | "hv_set_ifconfig", if_file); |
Olaf Hering | d3b688c | 2013-08-04 16:40:44 +0200 | [diff] [blame] | 1342 | if (system(cmd)) { |
| 1343 | syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s", |
| 1344 | cmd, errno, strerror(errno)); |
| 1345 | return HV_E_FAIL; |
| 1346 | } |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1347 | return 0; |
| 1348 | |
| 1349 | setval_error: |
| 1350 | syslog(LOG_ERR, "Failed to write config file"); |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1351 | fclose(file); |
| 1352 | return error; |
| 1353 | } |
| 1354 | |
| 1355 | |
Olaf Hering | 58125210 | 2013-08-07 19:14:37 +0200 | [diff] [blame] | 1356 | static void |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1357 | kvp_get_domain_name(char *buffer, int length) |
| 1358 | { |
| 1359 | struct addrinfo hints, *info ; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1360 | int error = 0; |
| 1361 | |
K. Y. Srinivasan | 5be528c | 2011-07-26 11:03:10 -0700 | [diff] [blame] | 1362 | gethostname(buffer, length); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1363 | memset(&hints, 0, sizeof(hints)); |
| 1364 | hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */ |
| 1365 | hints.ai_socktype = SOCK_STREAM; |
| 1366 | hints.ai_flags = AI_CANONNAME; |
| 1367 | |
K. Y. Srinivasan | 5be528c | 2011-07-26 11:03:10 -0700 | [diff] [blame] | 1368 | error = getaddrinfo(buffer, NULL, &hints, &info); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1369 | if (error != 0) { |
Olaf Hering | 58125210 | 2013-08-07 19:14:37 +0200 | [diff] [blame] | 1370 | snprintf(buffer, length, "getaddrinfo failed: 0x%x %s", |
| 1371 | error, gai_strerror(error)); |
| 1372 | return; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1373 | } |
Olaf Hering | 58125210 | 2013-08-07 19:14:37 +0200 | [diff] [blame] | 1374 | snprintf(buffer, length, "%s", info->ai_canonname); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1375 | freeaddrinfo(info); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 1378 | void print_usage(char *argv[]) |
| 1379 | { |
| 1380 | fprintf(stderr, "Usage: %s [options]\n" |
| 1381 | "Options are:\n" |
| 1382 | " -n, --no-daemon stay in foreground, don't daemonize\n" |
| 1383 | " -h, --help print this help\n", argv[0]); |
| 1384 | } |
| 1385 | |
| 1386 | int main(int argc, char *argv[]) |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1387 | { |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1388 | int kvp_fd, len; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1389 | int error; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1390 | struct pollfd pfd; |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1391 | char *p; |
| 1392 | struct hv_kvp_msg hv_msg[1]; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1393 | char *key_value; |
| 1394 | char *key_name; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1395 | int op; |
| 1396 | int pool; |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1397 | char *if_name; |
| 1398 | struct hv_kvp_ipaddr_value *kvp_ip_val; |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 1399 | int daemonize = 1, long_index = 0, opt; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1400 | |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 1401 | static struct option long_options[] = { |
| 1402 | {"help", no_argument, 0, 'h' }, |
| 1403 | {"no-daemon", no_argument, 0, 'n' }, |
| 1404 | {0, 0, 0, 0 } |
| 1405 | }; |
| 1406 | |
| 1407 | while ((opt = getopt_long(argc, argv, "hn", long_options, |
| 1408 | &long_index)) != -1) { |
| 1409 | switch (opt) { |
| 1410 | case 'n': |
| 1411 | daemonize = 0; |
| 1412 | break; |
| 1413 | case 'h': |
| 1414 | default: |
| 1415 | print_usage(argv); |
| 1416 | exit(EXIT_FAILURE); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | if (daemonize && daemon(1, 0)) |
Olaf Hering | 00663d7 | 2013-08-01 14:43:12 +0200 | [diff] [blame] | 1421 | return 1; |
Vitaly Kuznetsov | 170f4be | 2014-10-22 18:07:11 +0200 | [diff] [blame] | 1422 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1423 | openlog("KVP", 0, LOG_USER); |
| 1424 | syslog(LOG_INFO, "KVP starting; pid is:%d", getpid()); |
Olaf Hering | b4919a5 | 2013-08-01 14:34:26 +0200 | [diff] [blame] | 1425 | |
Vitaly Kuznetsov | 2684043 | 2016-07-06 18:24:10 -0700 | [diff] [blame] | 1426 | kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC); |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1427 | |
| 1428 | if (kvp_fd < 0) { |
| 1429 | syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s", |
| 1430 | errno, strerror(errno)); |
Olaf Hering | b4919a5 | 2013-08-01 14:34:26 +0200 | [diff] [blame] | 1431 | exit(EXIT_FAILURE); |
| 1432 | } |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1433 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1434 | /* |
| 1435 | * Retrieve OS release information. |
| 1436 | */ |
| 1437 | kvp_get_os_info(); |
Olaf Hering | 58125210 | 2013-08-07 19:14:37 +0200 | [diff] [blame] | 1438 | /* |
| 1439 | * Cache Fully Qualified Domain Name because getaddrinfo takes an |
| 1440 | * unpredictable amount of time to finish. |
| 1441 | */ |
| 1442 | kvp_get_domain_name(full_domain_name, sizeof(full_domain_name)); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1443 | |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1444 | if (kvp_file_init()) { |
| 1445 | syslog(LOG_ERR, "Failed to initialize the pools"); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 1446 | exit(EXIT_FAILURE); |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1449 | /* |
| 1450 | * Register ourselves with the kernel. |
| 1451 | */ |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1452 | hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1; |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1453 | len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg)); |
| 1454 | if (len != sizeof(struct hv_kvp_msg)) { |
| 1455 | syslog(LOG_ERR, "registration to kernel failed; error: %d %s", |
| 1456 | errno, strerror(errno)); |
| 1457 | close(kvp_fd); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 1458 | exit(EXIT_FAILURE); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1461 | pfd.fd = kvp_fd; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1462 | |
| 1463 | while (1) { |
| 1464 | pfd.events = POLLIN; |
| 1465 | pfd.revents = 0; |
Tomas Hozza | 4d81e30 | 2013-05-22 14:54:31 +0200 | [diff] [blame] | 1466 | |
| 1467 | if (poll(&pfd, 1, -1) < 0) { |
| 1468 | syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno)); |
| 1469 | if (errno == EINVAL) { |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1470 | close(kvp_fd); |
Tomas Hozza | 4d81e30 | 2013-05-22 14:54:31 +0200 | [diff] [blame] | 1471 | exit(EXIT_FAILURE); |
| 1472 | } |
| 1473 | else |
| 1474 | continue; |
| 1475 | } |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1476 | |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1477 | len = read(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg)); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1478 | |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1479 | if (len != sizeof(struct hv_kvp_msg)) { |
| 1480 | syslog(LOG_ERR, "read failed; error:%d %s", |
| 1481 | errno, strerror(errno)); |
Dexuan Cui | 4300f26 | 2014-11-19 21:51:22 -0800 | [diff] [blame] | 1482 | |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1483 | close(kvp_fd); |
| 1484 | return EXIT_FAILURE; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1487 | /* |
| 1488 | * We will use the KVP header information to pass back |
| 1489 | * the error from this daemon. So, first copy the state |
| 1490 | * and set the error code to success. |
| 1491 | */ |
| 1492 | op = hv_msg->kvp_hdr.operation; |
| 1493 | pool = hv_msg->kvp_hdr.pool; |
| 1494 | hv_msg->error = HV_S_OK; |
| 1495 | |
| 1496 | if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) { |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1497 | /* |
| 1498 | * Driver is registering with us; stash away the version |
| 1499 | * information. |
| 1500 | */ |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1501 | in_hand_shake = 0; |
K. Y. Srinivasan | e485ceac | 2012-03-10 15:32:08 -0800 | [diff] [blame] | 1502 | p = (char *)hv_msg->body.kvp_register.version; |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 1503 | lic_version = malloc(strlen(p) + 1); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1504 | if (lic_version) { |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 1505 | strcpy(lic_version, p); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1506 | syslog(LOG_INFO, "KVP LIC Version: %s", |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1507 | lic_version); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1508 | } else { |
| 1509 | syslog(LOG_ERR, "malloc failed"); |
| 1510 | } |
| 1511 | continue; |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1512 | } |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1513 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1514 | switch (op) { |
K. Y. Srinivasan | 16e8710 | 2012-09-05 13:50:15 -0700 | [diff] [blame] | 1515 | case KVP_OP_GET_IP_INFO: |
| 1516 | kvp_ip_val = &hv_msg->body.kvp_ip_val; |
| 1517 | if_name = |
| 1518 | kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id); |
| 1519 | |
| 1520 | if (if_name == NULL) { |
| 1521 | /* |
| 1522 | * We could not map the mac address to an |
| 1523 | * interface name; return error. |
| 1524 | */ |
| 1525 | hv_msg->error = HV_E_FAIL; |
| 1526 | break; |
| 1527 | } |
| 1528 | error = kvp_get_ip_info( |
| 1529 | 0, if_name, KVP_OP_GET_IP_INFO, |
| 1530 | kvp_ip_val, |
| 1531 | (MAX_IP_ADDR_SIZE * 2)); |
| 1532 | |
| 1533 | if (error) |
| 1534 | hv_msg->error = error; |
| 1535 | |
| 1536 | free(if_name); |
| 1537 | break; |
| 1538 | |
K. Y. Srinivasan | 32061b4 | 2012-09-05 13:50:13 -0700 | [diff] [blame] | 1539 | case KVP_OP_SET_IP_INFO: |
| 1540 | kvp_ip_val = &hv_msg->body.kvp_ip_val; |
| 1541 | if_name = kvp_get_if_name( |
| 1542 | (char *)kvp_ip_val->adapter_id); |
| 1543 | if (if_name == NULL) { |
| 1544 | /* |
| 1545 | * We could not map the guid to an |
| 1546 | * interface name; return error. |
| 1547 | */ |
| 1548 | hv_msg->error = HV_GUID_NOTFOUND; |
| 1549 | break; |
| 1550 | } |
| 1551 | error = kvp_set_ip_info(if_name, kvp_ip_val); |
| 1552 | if (error) |
| 1553 | hv_msg->error = error; |
| 1554 | |
| 1555 | free(if_name); |
| 1556 | break; |
| 1557 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 1558 | case KVP_OP_SET: |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1559 | if (kvp_key_add_or_modify(pool, |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1560 | hv_msg->body.kvp_set.data.key, |
| 1561 | hv_msg->body.kvp_set.data.key_size, |
| 1562 | hv_msg->body.kvp_set.data.value, |
| 1563 | hv_msg->body.kvp_set.data.value_size)) |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1564 | hv_msg->error = HV_S_CONT; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1565 | break; |
| 1566 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 1567 | case KVP_OP_GET: |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1568 | if (kvp_get_value(pool, |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1569 | hv_msg->body.kvp_set.data.key, |
| 1570 | hv_msg->body.kvp_set.data.key_size, |
| 1571 | hv_msg->body.kvp_set.data.value, |
| 1572 | hv_msg->body.kvp_set.data.value_size)) |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1573 | hv_msg->error = HV_S_CONT; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1574 | break; |
| 1575 | |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 1576 | case KVP_OP_DELETE: |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1577 | if (kvp_key_delete(pool, |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1578 | hv_msg->body.kvp_delete.key, |
| 1579 | hv_msg->body.kvp_delete.key_size)) |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1580 | hv_msg->error = HV_S_CONT; |
K. Y. Srinivasan | db425334e | 2012-03-16 08:02:26 -0700 | [diff] [blame] | 1581 | break; |
| 1582 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1583 | default: |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 1584 | break; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1587 | if (op != KVP_OP_ENUMERATE) |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 1588 | goto kvp_done; |
| 1589 | |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 1590 | /* |
| 1591 | * If the pool is KVP_POOL_AUTO, dynamically generate |
| 1592 | * both the key and the value; if not read from the |
| 1593 | * appropriate pool. |
| 1594 | */ |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1595 | if (pool != KVP_POOL_AUTO) { |
| 1596 | if (kvp_pool_enumerate(pool, |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 1597 | hv_msg->body.kvp_enum_data.index, |
| 1598 | hv_msg->body.kvp_enum_data.data.key, |
| 1599 | HV_KVP_EXCHANGE_MAX_KEY_SIZE, |
| 1600 | hv_msg->body.kvp_enum_data.data.value, |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1601 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE)) |
| 1602 | hv_msg->error = HV_S_CONT; |
K. Y. Srinivasan | adc80ae | 2012-03-16 08:02:27 -0700 | [diff] [blame] | 1603 | goto kvp_done; |
| 1604 | } |
| 1605 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 1606 | key_name = (char *)hv_msg->body.kvp_enum_data.data.key; |
| 1607 | key_value = (char *)hv_msg->body.kvp_enum_data.data.value; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1608 | |
K. Y. Srinivasan | 2640335 | 2012-02-02 16:56:50 -0800 | [diff] [blame] | 1609 | switch (hv_msg->body.kvp_enum_data.index) { |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1610 | case FullyQualifiedDomainName: |
Olaf Hering | 58125210 | 2013-08-07 19:14:37 +0200 | [diff] [blame] | 1611 | strcpy(key_value, full_domain_name); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1612 | strcpy(key_name, "FullyQualifiedDomainName"); |
| 1613 | break; |
| 1614 | case IntegrationServicesVersion: |
| 1615 | strcpy(key_name, "IntegrationServicesVersion"); |
| 1616 | strcpy(key_value, lic_version); |
| 1617 | break; |
| 1618 | case NetworkAddressIPv4: |
K. Y. Srinivasan | 4a3b97e5 | 2012-09-05 13:50:14 -0700 | [diff] [blame] | 1619 | kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE, |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 1620 | key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1621 | strcpy(key_name, "NetworkAddressIPv4"); |
| 1622 | break; |
| 1623 | case NetworkAddressIPv6: |
K. Y. Srinivasan | 4a3b97e5 | 2012-09-05 13:50:14 -0700 | [diff] [blame] | 1624 | kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE, |
K. Y. Srinivasan | 0ecaa19 | 2012-08-16 18:32:13 -0700 | [diff] [blame] | 1625 | key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1626 | strcpy(key_name, "NetworkAddressIPv6"); |
| 1627 | break; |
| 1628 | case OSBuildNumber: |
| 1629 | strcpy(key_value, os_build); |
| 1630 | strcpy(key_name, "OSBuildNumber"); |
| 1631 | break; |
| 1632 | case OSName: |
| 1633 | strcpy(key_value, os_name); |
| 1634 | strcpy(key_name, "OSName"); |
| 1635 | break; |
| 1636 | case OSMajorVersion: |
| 1637 | strcpy(key_value, os_major); |
| 1638 | strcpy(key_name, "OSMajorVersion"); |
| 1639 | break; |
| 1640 | case OSMinorVersion: |
| 1641 | strcpy(key_value, os_minor); |
| 1642 | strcpy(key_name, "OSMinorVersion"); |
| 1643 | break; |
| 1644 | case OSVersion: |
K. Y. Srinivasan | f426a36 | 2012-10-25 14:15:49 -0700 | [diff] [blame] | 1645 | strcpy(key_value, os_version); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1646 | strcpy(key_name, "OSVersion"); |
| 1647 | break; |
| 1648 | case ProcessorArchitecture: |
| 1649 | strcpy(key_value, processor_arch); |
| 1650 | strcpy(key_name, "ProcessorArchitecture"); |
| 1651 | break; |
| 1652 | default: |
K. Y. Srinivasan | b47a81d | 2012-08-13 10:06:52 -0700 | [diff] [blame] | 1653 | hv_msg->error = HV_S_CONT; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1654 | break; |
| 1655 | } |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1656 | |
| 1657 | /* Send the value back to the kernel. */ |
K. Y. Srinivasan | fa3d5b8 | 2012-03-16 08:02:25 -0700 | [diff] [blame] | 1658 | kvp_done: |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1659 | len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg)); |
| 1660 | if (len != sizeof(struct hv_kvp_msg)) { |
| 1661 | syslog(LOG_ERR, "write failed; error: %d %s", errno, |
| 1662 | strerror(errno)); |
Ben Hutchings | 6bb22fe | 2012-09-05 14:37:36 -0700 | [diff] [blame] | 1663 | exit(EXIT_FAILURE); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
Vitaly Kuznetsov | 8ddca80 | 2015-04-11 18:07:55 -0700 | [diff] [blame] | 1667 | close(kvp_fd); |
| 1668 | exit(0); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 1669 | } |