blob: d55ce4040b74910d1de363cbaf301aba7407e68e [file] [log] [blame]
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001/*
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
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <sys/poll.h>
28#include <sys/utsname.h>
29#include <linux/types.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
33#include <string.h>
K. Y. Srinivasan32061b42012-09-05 13:50:13 -070034#include <ctype.h>
Ky Srinivasancc04acf2010-12-16 18:56:54 -070035#include <errno.h>
36#include <arpa/inet.h>
37#include <linux/connector.h>
K. Y. Srinivasaneab6af72012-02-02 16:56:49 -080038#include <linux/hyperv.h>
Ky Srinivasancc04acf2010-12-16 18:56:54 -070039#include <linux/netlink.h>
Ky Srinivasancc04acf2010-12-16 18:56:54 -070040#include <ifaddrs.h>
41#include <netdb.h>
42#include <syslog.h>
K. Y. Srinivasandb425332012-03-16 08:02:26 -070043#include <sys/stat.h>
44#include <fcntl.h>
K. Y. Srinivasan32061b42012-09-05 13:50:13 -070045#include <dirent.h>
K. Y. Srinivasan3321e732012-10-25 14:15:50 -070046#include <net/if.h>
Ky Srinivasancc04acf2010-12-16 18:56:54 -070047
48/*
49 * KVP protocol: The user mode component first registers with the
50 * the kernel component. Subsequently, the kernel component requests, data
51 * for the specified keys. In response to this message the user mode component
52 * fills in the value corresponding to the specified key. We overload the
53 * sequence field in the cn_msg header to define our KVP message types.
54 *
55 * We use this infrastructure for also supporting queries from user mode
56 * application for state that may be maintained in the KVP kernel component.
57 *
Ky Srinivasancc04acf2010-12-16 18:56:54 -070058 */
59
Ky Srinivasancc04acf2010-12-16 18:56:54 -070060
61enum key_index {
62 FullyQualifiedDomainName = 0,
63 IntegrationServicesVersion, /*This key is serviced in the kernel*/
64 NetworkAddressIPv4,
65 NetworkAddressIPv6,
66 OSBuildNumber,
67 OSName,
68 OSMajorVersion,
69 OSMinorVersion,
70 OSVersion,
71 ProcessorArchitecture
72};
73
K. Y. Srinivasan32061b42012-09-05 13:50:13 -070074
75enum {
76 IPADDR = 0,
77 NETMASK,
78 GATEWAY,
79 DNS
80};
81
Ky Srinivasancc04acf2010-12-16 18:56:54 -070082static char kvp_send_buffer[4096];
K. Y. Srinivasan9b595782012-08-13 10:06:51 -070083static char kvp_recv_buffer[4096 * 2];
Ky Srinivasancc04acf2010-12-16 18:56:54 -070084static struct sockaddr_nl addr;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070085static int in_hand_shake = 1;
Ky Srinivasancc04acf2010-12-16 18:56:54 -070086
Olaf Hering7989f7d2011-03-22 10:02:17 +010087static char *os_name = "";
88static char *os_major = "";
89static char *os_minor = "";
90static char *processor_arch;
91static char *os_build;
K. Y. Srinivasanf426a362012-10-25 14:15:49 -070092static char *os_version;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070093static char *lic_version = "Unknown version";
Olaf Hering7989f7d2011-03-22 10:02:17 +010094static struct utsname uts_buf;
Ky Srinivasancc04acf2010-12-16 18:56:54 -070095
K. Y. Srinivasan32061b42012-09-05 13:50:13 -070096/*
97 * The location of the interface configuration file.
98 */
99
Tomas Hozza40424f52012-11-27 08:56:33 +0100100#define KVP_CONFIG_LOC "/var/lib/hyperv"
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700101
102#define MAX_FILE_NAME 100
103#define ENTRIES_PER_BLOCK 50
104
Tomas Hozzaf4685fa2013-03-13 14:14:13 +0100105#ifndef SOL_NETLINK
106#define SOL_NETLINK 270
107#endif
108
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700109struct kvp_record {
K. Y. Srinivasand0cbc152012-09-04 14:46:34 -0700110 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
111 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700112};
113
114struct kvp_file_state {
115 int fd;
116 int num_blocks;
117 struct kvp_record *records;
118 int num_records;
K. Y. Srinivasand0cbc152012-09-04 14:46:34 -0700119 char fname[MAX_FILE_NAME];
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700120};
121
122static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
123
124static void kvp_acquire_lock(int pool)
125{
126 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
127 fl.l_pid = getpid();
128
129 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
130 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -0700131 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700132 }
133}
134
135static void kvp_release_lock(int pool)
136{
137 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
138 fl.l_pid = getpid();
139
140 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
141 perror("fcntl");
142 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -0700143 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700144 }
145}
146
147static void kvp_update_file(int pool)
148{
149 FILE *filep;
150 size_t bytes_written;
151
152 /*
153 * We are going to write our in-memory registry out to
154 * disk; acquire the lock first.
155 */
156 kvp_acquire_lock(pool);
157
Tomas Hozza8467fdb2013-01-18 15:23:41 +0100158 filep = fopen(kvp_file_info[pool].fname, "we");
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700159 if (!filep) {
160 kvp_release_lock(pool);
161 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -0700162 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700163 }
164
165 bytes_written = fwrite(kvp_file_info[pool].records,
166 sizeof(struct kvp_record),
167 kvp_file_info[pool].num_records, filep);
168
Ben Hutchings436473b2012-09-05 14:37:37 -0700169 if (ferror(filep) || fclose(filep)) {
170 kvp_release_lock(pool);
171 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
172 exit(EXIT_FAILURE);
173 }
174
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700175 kvp_release_lock(pool);
176}
177
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700178static void kvp_update_mem_state(int pool)
179{
180 FILE *filep;
181 size_t records_read = 0;
182 struct kvp_record *record = kvp_file_info[pool].records;
183 struct kvp_record *readp;
184 int num_blocks = kvp_file_info[pool].num_blocks;
185 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
186
187 kvp_acquire_lock(pool);
188
Tomas Hozza8467fdb2013-01-18 15:23:41 +0100189 filep = fopen(kvp_file_info[pool].fname, "re");
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700190 if (!filep) {
191 kvp_release_lock(pool);
192 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -0700193 exit(EXIT_FAILURE);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700194 }
Ben Hutchings436473b2012-09-05 14:37:37 -0700195 for (;;) {
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700196 readp = &record[records_read];
197 records_read += fread(readp, sizeof(struct kvp_record),
198 ENTRIES_PER_BLOCK * num_blocks,
199 filep);
200
Ben Hutchings436473b2012-09-05 14:37:37 -0700201 if (ferror(filep)) {
202 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
203 exit(EXIT_FAILURE);
204 }
205
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700206 if (!feof(filep)) {
207 /*
208 * We have more data to read.
209 */
210 num_blocks++;
211 record = realloc(record, alloc_unit * num_blocks);
212
213 if (record == NULL) {
214 syslog(LOG_ERR, "malloc failed");
Ben Hutchings6bb22fe2012-09-05 14:37:36 -0700215 exit(EXIT_FAILURE);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700216 }
217 continue;
218 }
219 break;
220 }
221
222 kvp_file_info[pool].num_blocks = num_blocks;
223 kvp_file_info[pool].records = record;
224 kvp_file_info[pool].num_records = records_read;
225
Ben Hutchingsd5ab4822012-09-05 14:37:35 -0700226 fclose(filep);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700227 kvp_release_lock(pool);
228}
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700229static int kvp_file_init(void)
230{
K. Y. Srinivasan00b83352012-09-04 14:46:33 -0700231 int fd;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700232 FILE *filep;
233 size_t records_read;
K. Y. Srinivasand0cbc152012-09-04 14:46:34 -0700234 char *fname;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700235 struct kvp_record *record;
236 struct kvp_record *readp;
237 int num_blocks;
238 int i;
239 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
240
Tomas Hozza40424f52012-11-27 08:56:33 +0100241 if (access(KVP_CONFIG_LOC, F_OK)) {
Ben Hutchings0bffd252012-11-27 08:56:34 +0100242 if (mkdir(KVP_CONFIG_LOC, 0755 /* rwxr-xr-x */)) {
Tomas Hozza40424f52012-11-27 08:56:33 +0100243 syslog(LOG_ERR, " Failed to create %s", KVP_CONFIG_LOC);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -0700244 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700245 }
246 }
247
248 for (i = 0; i < KVP_POOL_COUNT; i++) {
249 fname = kvp_file_info[i].fname;
250 records_read = 0;
251 num_blocks = 1;
Tomas Hozza40424f52012-11-27 08:56:33 +0100252 sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
Tomas Hozza8467fdb2013-01-18 15:23:41 +0100253 fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700254
255 if (fd == -1)
256 return 1;
257
258
Tomas Hozza8467fdb2013-01-18 15:23:41 +0100259 filep = fopen(fname, "re");
Tomas Hozzafca59752013-05-22 14:54:33 +0200260 if (!filep) {
261 close(fd);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700262 return 1;
Tomas Hozzafca59752013-05-22 14:54:33 +0200263 }
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700264
265 record = malloc(alloc_unit * num_blocks);
266 if (record == NULL) {
267 fclose(filep);
Tomas Hozzafca59752013-05-22 14:54:33 +0200268 close(fd);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700269 return 1;
270 }
Ben Hutchings436473b2012-09-05 14:37:37 -0700271 for (;;) {
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700272 readp = &record[records_read];
273 records_read += fread(readp, sizeof(struct kvp_record),
274 ENTRIES_PER_BLOCK,
275 filep);
276
Ben Hutchings436473b2012-09-05 14:37:37 -0700277 if (ferror(filep)) {
278 syslog(LOG_ERR, "Failed to read file, pool: %d",
279 i);
280 exit(EXIT_FAILURE);
281 }
282
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700283 if (!feof(filep)) {
284 /*
285 * We have more data to read.
286 */
287 num_blocks++;
288 record = realloc(record, alloc_unit *
289 num_blocks);
290 if (record == NULL) {
291 fclose(filep);
Tomas Hozzafca59752013-05-22 14:54:33 +0200292 close(fd);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700293 return 1;
294 }
295 continue;
296 }
297 break;
298 }
299 kvp_file_info[i].fd = fd;
300 kvp_file_info[i].num_blocks = num_blocks;
301 kvp_file_info[i].records = record;
302 kvp_file_info[i].num_records = records_read;
303 fclose(filep);
304
305 }
306
307 return 0;
308}
309
Tomas Hozzad892de82012-11-09 15:01:20 +0100310static int kvp_key_delete(int pool, const char *key, int key_size)
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700311{
312 int i;
313 int j, k;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700314 int num_records;
315 struct kvp_record *record;
316
317 /*
318 * First update the in-memory state.
319 */
320 kvp_update_mem_state(pool);
321
322 num_records = kvp_file_info[pool].num_records;
323 record = kvp_file_info[pool].records;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700324
325 for (i = 0; i < num_records; i++) {
326 if (memcmp(key, record[i].key, key_size))
327 continue;
328 /*
329 * Found a match; just move the remaining
330 * entries up.
331 */
332 if (i == num_records) {
333 kvp_file_info[pool].num_records--;
334 kvp_update_file(pool);
335 return 0;
336 }
337
338 j = i;
339 k = j + 1;
340 for (; k < num_records; k++) {
341 strcpy(record[j].key, record[k].key);
342 strcpy(record[j].value, record[k].value);
343 j++;
344 }
345
346 kvp_file_info[pool].num_records--;
347 kvp_update_file(pool);
348 return 0;
349 }
350 return 1;
351}
352
Tomas Hozzad892de82012-11-09 15:01:20 +0100353static int kvp_key_add_or_modify(int pool, const char *key, int key_size, const char *value,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700354 int value_size)
355{
356 int i;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700357 int num_records;
358 struct kvp_record *record;
359 int num_blocks;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700360
361 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
362 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
363 return 1;
364
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700365 /*
366 * First update the in-memory state.
367 */
368 kvp_update_mem_state(pool);
369
370 num_records = kvp_file_info[pool].num_records;
371 record = kvp_file_info[pool].records;
372 num_blocks = kvp_file_info[pool].num_blocks;
373
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700374 for (i = 0; i < num_records; i++) {
375 if (memcmp(key, record[i].key, key_size))
376 continue;
377 /*
378 * Found a match; just update the value -
379 * this is the modify case.
380 */
381 memcpy(record[i].value, value, value_size);
382 kvp_update_file(pool);
383 return 0;
384 }
385
386 /*
387 * Need to add a new entry;
388 */
389 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
390 /* Need to allocate a larger array for reg entries. */
391 record = realloc(record, sizeof(struct kvp_record) *
392 ENTRIES_PER_BLOCK * (num_blocks + 1));
393
394 if (record == NULL)
395 return 1;
396 kvp_file_info[pool].num_blocks++;
397
398 }
399 memcpy(record[i].value, value, value_size);
400 memcpy(record[i].key, key, key_size);
401 kvp_file_info[pool].records = record;
402 kvp_file_info[pool].num_records++;
403 kvp_update_file(pool);
404 return 0;
405}
406
Tomas Hozzad892de82012-11-09 15:01:20 +0100407static int kvp_get_value(int pool, const char *key, int key_size, char *value,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700408 int value_size)
409{
410 int i;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700411 int num_records;
412 struct kvp_record *record;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700413
414 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
415 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
416 return 1;
417
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700418 /*
419 * First update the in-memory state.
420 */
421 kvp_update_mem_state(pool);
422
423 num_records = kvp_file_info[pool].num_records;
424 record = kvp_file_info[pool].records;
425
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700426 for (i = 0; i < num_records; i++) {
427 if (memcmp(key, record[i].key, key_size))
428 continue;
429 /*
430 * Found a match; just copy the value out.
431 */
432 memcpy(value, record[i].value, value_size);
433 return 0;
434 }
435
436 return 1;
437}
438
Tomas Hozzad892de82012-11-09 15:01:20 +0100439static int kvp_pool_enumerate(int pool, int index, char *key, int key_size,
440 char *value, int value_size)
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700441{
442 struct kvp_record *record;
443
444 /*
445 * First update our in-memory database.
446 */
447 kvp_update_mem_state(pool);
448 record = kvp_file_info[pool].records;
449
450 if (index >= kvp_file_info[pool].num_records) {
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700451 return 1;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700452 }
453
454 memcpy(key, record[index].key, key_size);
455 memcpy(value, record[index].value, value_size);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700456 return 0;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700457}
458
459
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700460void kvp_get_os_info(void)
461{
462 FILE *file;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100463 char *p, buf[512];
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700464
Olaf Hering7989f7d2011-03-22 10:02:17 +0100465 uname(&uts_buf);
K. Y. Srinivasanf426a362012-10-25 14:15:49 -0700466 os_version = uts_buf.release;
467 os_build = strdup(uts_buf.release);
468
Ben Hutchings44c8b252012-09-06 13:32:14 -0700469 os_name = uts_buf.sysname;
K. Y. Srinivasan064931d2011-07-19 11:44:20 -0700470 processor_arch = uts_buf.machine;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700471
K. Y. Srinivasane54bbc62011-07-22 10:14:31 -0700472 /*
473 * The current windows host (win7) expects the build
474 * string to be of the form: x.y.z
475 * Strip additional information we may have.
476 */
K. Y. Srinivasanf426a362012-10-25 14:15:49 -0700477 p = strchr(os_version, '-');
K. Y. Srinivasane54bbc62011-07-22 10:14:31 -0700478 if (p)
479 *p = '\0';
480
Ben Hutchings44c8b252012-09-06 13:32:14 -0700481 /*
482 * Parse the /etc/os-release file if present:
483 * http://www.freedesktop.org/software/systemd/man/os-release.html
484 */
485 file = fopen("/etc/os-release", "r");
486 if (file != NULL) {
487 while (fgets(buf, sizeof(buf), file)) {
488 char *value, *q;
489
490 /* Ignore comments */
491 if (buf[0] == '#')
492 continue;
493
494 /* Split into name=value */
495 p = strchr(buf, '=');
496 if (!p)
497 continue;
498 *p++ = 0;
499
500 /* Remove quotes and newline; un-escape */
501 value = p;
502 q = p;
503 while (*p) {
504 if (*p == '\\') {
505 ++p;
506 if (!*p)
507 break;
508 *q++ = *p++;
509 } else if (*p == '\'' || *p == '"' ||
510 *p == '\n') {
511 ++p;
512 } else {
513 *q++ = *p++;
514 }
515 }
516 *q = 0;
517
518 if (!strcmp(buf, "NAME")) {
519 p = strdup(value);
520 if (!p)
521 break;
522 os_name = p;
523 } else if (!strcmp(buf, "VERSION_ID")) {
524 p = strdup(value);
525 if (!p)
526 break;
527 os_major = p;
528 }
529 }
530 fclose(file);
531 return;
532 }
533
534 /* Fallback for older RH/SUSE releases */
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700535 file = fopen("/etc/SuSE-release", "r");
536 if (file != NULL)
537 goto kvp_osinfo_found;
538 file = fopen("/etc/redhat-release", "r");
539 if (file != NULL)
540 goto kvp_osinfo_found;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700541
542 /*
543 * We don't have information about the os.
544 */
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700545 return;
546
547kvp_osinfo_found:
Olaf Hering7989f7d2011-03-22 10:02:17 +0100548 /* up to three lines */
549 p = fgets(buf, sizeof(buf), file);
550 if (p) {
551 p = strchr(buf, '\n');
552 if (p)
553 *p = '\0';
554 p = strdup(buf);
555 if (!p)
556 goto done;
557 os_name = p;
558
559 /* second line */
560 p = fgets(buf, sizeof(buf), file);
561 if (p) {
562 p = strchr(buf, '\n');
563 if (p)
564 *p = '\0';
565 p = strdup(buf);
566 if (!p)
567 goto done;
568 os_major = p;
569
570 /* third line */
571 p = fgets(buf, sizeof(buf), file);
572 if (p) {
573 p = strchr(buf, '\n');
574 if (p)
575 *p = '\0';
576 p = strdup(buf);
577 if (p)
578 os_minor = p;
579 }
580 }
581 }
582
583done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700584 fclose(file);
585 return;
586}
587
K. Y. Srinivasan32061b42012-09-05 13:50:13 -0700588
589
590/*
591 * Retrieve an interface name corresponding to the specified guid.
592 * If there is a match, the function returns a pointer
593 * to the interface name and if not, a NULL is returned.
594 * If a match is found, the caller is responsible for
595 * freeing the memory.
596 */
597
598static char *kvp_get_if_name(char *guid)
599{
600 DIR *dir;
601 struct dirent *entry;
602 FILE *file;
603 char *p, *q, *x;
604 char *if_name = NULL;
605 char buf[256];
606 char *kvp_net_dir = "/sys/class/net/";
607 char dev_id[256];
608
609 dir = opendir(kvp_net_dir);
610 if (dir == NULL)
611 return NULL;
612
613 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
614 q = dev_id + strlen(kvp_net_dir);
615
616 while ((entry = readdir(dir)) != NULL) {
617 /*
618 * Set the state for the next pass.
619 */
620 *q = '\0';
621 strcat(dev_id, entry->d_name);
622 strcat(dev_id, "/device/device_id");
623
624 file = fopen(dev_id, "r");
625 if (file == NULL)
626 continue;
627
628 p = fgets(buf, sizeof(buf), file);
629 if (p) {
630 x = strchr(p, '\n');
631 if (x)
632 *x = '\0';
633
634 if (!strcmp(p, guid)) {
635 /*
636 * Found the guid match; return the interface
637 * name. The caller will free the memory.
638 */
639 if_name = strdup(entry->d_name);
640 fclose(file);
641 break;
642 }
643 }
644 fclose(file);
645 }
646
647 closedir(dir);
648 return if_name;
649}
650
651/*
652 * Retrieve the MAC address given the interface name.
653 */
654
655static char *kvp_if_name_to_mac(char *if_name)
656{
657 FILE *file;
658 char *p, *x;
659 char buf[256];
660 char addr_file[256];
661 int i;
662 char *mac_addr = NULL;
663
664 snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
665 if_name, "/address");
666
667 file = fopen(addr_file, "r");
668 if (file == NULL)
669 return NULL;
670
671 p = fgets(buf, sizeof(buf), file);
672 if (p) {
673 x = strchr(p, '\n');
674 if (x)
675 *x = '\0';
676 for (i = 0; i < strlen(p); i++)
677 p[i] = toupper(p[i]);
678 mac_addr = strdup(p);
679 }
680
681 fclose(file);
682 return mac_addr;
683}
684
685
K. Y. Srinivasan16e87102012-09-05 13:50:15 -0700686/*
687 * Retrieve the interface name given tha MAC address.
688 */
689
690static char *kvp_mac_to_if_name(char *mac)
691{
692 DIR *dir;
693 struct dirent *entry;
694 FILE *file;
695 char *p, *q, *x;
696 char *if_name = NULL;
697 char buf[256];
698 char *kvp_net_dir = "/sys/class/net/";
699 char dev_id[256];
700 int i;
701
702 dir = opendir(kvp_net_dir);
703 if (dir == NULL)
704 return NULL;
705
706 snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
707 q = dev_id + strlen(kvp_net_dir);
708
709 while ((entry = readdir(dir)) != NULL) {
710 /*
711 * Set the state for the next pass.
712 */
713 *q = '\0';
714
715 strcat(dev_id, entry->d_name);
716 strcat(dev_id, "/address");
717
718 file = fopen(dev_id, "r");
719 if (file == NULL)
720 continue;
721
722 p = fgets(buf, sizeof(buf), file);
723 if (p) {
724 x = strchr(p, '\n');
725 if (x)
726 *x = '\0';
727
728 for (i = 0; i < strlen(p); i++)
729 p[i] = toupper(p[i]);
730
731 if (!strcmp(p, mac)) {
732 /*
733 * Found the MAC match; return the interface
734 * name. The caller will free the memory.
735 */
736 if_name = strdup(entry->d_name);
737 fclose(file);
738 break;
739 }
740 }
741 fclose(file);
742 }
743
744 closedir(dir);
745 return if_name;
746}
747
748
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700749static void kvp_process_ipconfig_file(char *cmd,
750 char *config_buf, int len,
751 int element_size, int offset)
752{
753 char buf[256];
754 char *p;
755 char *x;
756 FILE *file;
757
758 /*
759 * First execute the command.
760 */
761 file = popen(cmd, "r");
762 if (file == NULL)
763 return;
764
765 if (offset == 0)
766 memset(config_buf, 0, len);
767 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
768 if ((len - strlen(config_buf)) < (element_size + 1))
769 break;
770
771 x = strchr(p, '\n');
Tomas Hozzaf14e6002013-05-22 14:54:32 +0200772 if (x)
773 *x = '\0';
774
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700775 strcat(config_buf, p);
776 strcat(config_buf, ";");
777 }
778 pclose(file);
779}
780
781static void kvp_get_ipconfig_info(char *if_name,
782 struct hv_kvp_ipaddr_value *buffer)
783{
784 char cmd[512];
K. Y. Srinivasanc0503722012-09-05 13:50:11 -0700785 char dhcp_info[128];
786 char *p;
787 FILE *file;
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700788
789 /*
790 * Get the address of default gateway (ipv4).
791 */
792 sprintf(cmd, "%s %s", "ip route show dev", if_name);
793 strcat(cmd, " | awk '/default/ {print $3 }'");
794
795 /*
796 * Execute the command to gather gateway info.
797 */
798 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
799 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
800
801 /*
802 * Get the address of default gateway (ipv6).
803 */
804 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
805 strcat(cmd, " | awk '/default/ {print $3 }'");
806
807 /*
808 * Execute the command to gather gateway info (ipv6).
809 */
810 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
811 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
812
K. Y. Srinivasan96929882012-09-04 14:46:36 -0700813
814 /*
815 * Gather the DNS state.
816 * Since there is no standard way to get this information
817 * across various distributions of interest; we just invoke
818 * an external script that needs to be ported across distros
819 * of interest.
820 *
821 * Following is the expected format of the information from the script:
822 *
823 * ipaddr1 (nameserver1)
824 * ipaddr2 (nameserver2)
825 * .
826 * .
827 */
828
829 sprintf(cmd, "%s", "hv_get_dns_info");
830
831 /*
832 * Execute the command to gather DNS info.
833 */
834 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
835 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
K. Y. Srinivasanc0503722012-09-05 13:50:11 -0700836
837 /*
838 * Gather the DHCP state.
839 * We will gather this state by invoking an external script.
840 * The parameter to the script is the interface name.
841 * Here is the expected output:
842 *
843 * Enabled: DHCP enabled.
844 */
845
846 sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
847
848 file = popen(cmd, "r");
849 if (file == NULL)
850 return;
851
852 p = fgets(dhcp_info, sizeof(dhcp_info), file);
853 if (p == NULL) {
854 pclose(file);
855 return;
856 }
857
858 if (!strncmp(p, "Enabled", 7))
859 buffer->dhcp_enabled = 1;
860 else
861 buffer->dhcp_enabled = 0;
862
863 pclose(file);
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700864}
865
866
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700867static unsigned int hweight32(unsigned int *w)
868{
869 unsigned int res = *w - ((*w >> 1) & 0x55555555);
870 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
871 res = (res + (res >> 4)) & 0x0F0F0F0F;
872 res = res + (res >> 8);
873 return (res + (res >> 16)) & 0x000000FF;
874}
875
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700876static int kvp_process_ip_address(void *addrp,
877 int family, char *buffer,
878 int length, int *offset)
879{
880 struct sockaddr_in *addr;
881 struct sockaddr_in6 *addr6;
882 int addr_length;
883 char tmp[50];
884 const char *str;
885
886 if (family == AF_INET) {
887 addr = (struct sockaddr_in *)addrp;
888 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
889 addr_length = INET_ADDRSTRLEN;
890 } else {
891 addr6 = (struct sockaddr_in6 *)addrp;
892 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
893 addr_length = INET6_ADDRSTRLEN;
894 }
895
K. Y. Srinivasan3321e732012-10-25 14:15:50 -0700896 if ((length - *offset) < addr_length + 2)
K. Y. Srinivasan16e87102012-09-05 13:50:15 -0700897 return HV_E_FAIL;
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700898 if (str == NULL) {
899 strcpy(buffer, "inet_ntop failed\n");
K. Y. Srinivasan16e87102012-09-05 13:50:15 -0700900 return HV_E_FAIL;
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700901 }
902 if (*offset == 0)
903 strcpy(buffer, tmp);
K. Y. Srinivasan3321e732012-10-25 14:15:50 -0700904 else {
905 strcat(buffer, ";");
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700906 strcat(buffer, tmp);
K. Y. Srinivasan3321e732012-10-25 14:15:50 -0700907 }
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700908
909 *offset += strlen(str) + 1;
K. Y. Srinivasan3321e732012-10-25 14:15:50 -0700910
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700911 return 0;
912}
913
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700914static int
K. Y. Srinivasan4a3b97e52012-09-05 13:50:14 -0700915kvp_get_ip_info(int family, char *if_name, int op,
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700916 void *out_buffer, int length)
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700917{
918 struct ifaddrs *ifap;
919 struct ifaddrs *curp;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700920 int offset = 0;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700921 int sn_offset = 0;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700922 int error = 0;
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700923 char *buffer;
924 struct hv_kvp_ipaddr_value *ip_buffer;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700925 char cidr_mask[5]; /* /xyz */
926 int weight;
927 int i;
928 unsigned int *w;
929 char *sn_str;
930 struct sockaddr_in6 *addr6;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700931
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700932 if (op == KVP_OP_ENUMERATE) {
933 buffer = out_buffer;
934 } else {
935 ip_buffer = out_buffer;
936 buffer = (char *)ip_buffer->ip_addr;
937 ip_buffer->addr_family = 0;
938 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700939 /*
940 * On entry into this function, the buffer is capable of holding the
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700941 * maximum key value.
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700942 */
943
944 if (getifaddrs(&ifap)) {
945 strcpy(buffer, "getifaddrs failed\n");
K. Y. Srinivasan16e87102012-09-05 13:50:15 -0700946 return HV_E_FAIL;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700947 }
948
949 curp = ifap;
950 while (curp != NULL) {
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700951 if (curp->ifa_addr == NULL) {
952 curp = curp->ifa_next;
953 continue;
954 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700955
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700956 if ((if_name != NULL) &&
957 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
958 /*
959 * We want info about a specific interface;
960 * just continue.
961 */
962 curp = curp->ifa_next;
963 continue;
964 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700965
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700966 /*
967 * We only support two address families: AF_INET and AF_INET6.
968 * If a family value of 0 is specified, we collect both
969 * supported address families; if not we gather info on
970 * the specified address family.
971 */
K. Y. Srinivasan3321e732012-10-25 14:15:50 -0700972 if ((((family != 0) &&
973 (curp->ifa_addr->sa_family != family))) ||
974 (curp->ifa_flags & IFF_LOOPBACK)) {
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700975 curp = curp->ifa_next;
976 continue;
977 }
978 if ((curp->ifa_addr->sa_family != AF_INET) &&
979 (curp->ifa_addr->sa_family != AF_INET6)) {
980 curp = curp->ifa_next;
981 continue;
982 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700983
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700984 if (op == KVP_OP_GET_IP_INFO) {
985 /*
986 * Gather info other than the IP address.
987 * IP address info will be gathered later.
988 */
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700989 if (curp->ifa_addr->sa_family == AF_INET) {
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700990 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700991 /*
992 * Get subnet info.
993 */
994 error = kvp_process_ip_address(
995 curp->ifa_netmask,
996 AF_INET,
997 (char *)
998 ip_buffer->sub_net,
999 length,
1000 &sn_offset);
1001 if (error)
1002 goto gather_ipaddr;
1003 } else {
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -07001004 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -07001005
K. Y. Srinivasan04405782012-08-16 18:32:16 -07001006 /*
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -07001007 * Get subnet info in CIDR format.
K. Y. Srinivasan04405782012-08-16 18:32:16 -07001008 */
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -07001009 weight = 0;
1010 sn_str = (char *)ip_buffer->sub_net;
1011 addr6 = (struct sockaddr_in6 *)
1012 curp->ifa_netmask;
1013 w = addr6->sin6_addr.s6_addr32;
1014
1015 for (i = 0; i < 4; i++)
1016 weight += hweight32(&w[i]);
1017
1018 sprintf(cidr_mask, "/%d", weight);
1019 if ((length - sn_offset) <
1020 (strlen(cidr_mask) + 1))
K. Y. Srinivasan04405782012-08-16 18:32:16 -07001021 goto gather_ipaddr;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -07001022
1023 if (sn_offset == 0)
1024 strcpy(sn_str, cidr_mask);
1025 else
1026 strcat(sn_str, cidr_mask);
1027 strcat((char *)ip_buffer->sub_net, ";");
1028 sn_offset += strlen(sn_str) + 1;
K. Y. Srinivasan04405782012-08-16 18:32:16 -07001029 }
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -07001030
1031 /*
1032 * Collect other ip related configuration info.
1033 */
1034
1035 kvp_get_ipconfig_info(if_name, ip_buffer);
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -07001036 }
1037
K. Y. Srinivasan04405782012-08-16 18:32:16 -07001038gather_ipaddr:
K. Y. Srinivasanaf733012012-08-16 18:32:14 -07001039 error = kvp_process_ip_address(curp->ifa_addr,
1040 curp->ifa_addr->sa_family,
1041 buffer,
1042 length, &offset);
1043 if (error)
1044 goto getaddr_done;
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -07001045
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001046 curp = curp->ifa_next;
1047 }
1048
1049getaddr_done:
1050 freeifaddrs(ifap);
1051 return error;
1052}
1053
1054
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001055static int expand_ipv6(char *addr, int type)
1056{
1057 int ret;
1058 struct in6_addr v6_addr;
1059
1060 ret = inet_pton(AF_INET6, addr, &v6_addr);
1061
1062 if (ret != 1) {
1063 if (type == NETMASK)
1064 return 1;
1065 return 0;
1066 }
1067
1068 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1069 "%02x%02x:%02x%02x:%02x%02x",
1070 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
1071 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
1072 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
1073 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
1074 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
1075 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
1076 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
1077 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
1078
1079 return 1;
1080
1081}
1082
1083static int is_ipv4(char *addr)
1084{
1085 int ret;
1086 struct in_addr ipv4_addr;
1087
1088 ret = inet_pton(AF_INET, addr, &ipv4_addr);
1089
1090 if (ret == 1)
1091 return 1;
1092 return 0;
1093}
1094
1095static int parse_ip_val_buffer(char *in_buf, int *offset,
1096 char *out_buf, int out_len)
1097{
1098 char *x;
1099 char *start;
1100
1101 /*
1102 * in_buf has sequence of characters that are seperated by
1103 * the character ';'. The last sequence does not have the
1104 * terminating ";" character.
1105 */
1106 start = in_buf + *offset;
1107
1108 x = strchr(start, ';');
1109 if (x)
1110 *x = 0;
1111 else
1112 x = start + strlen(start);
1113
1114 if (strlen(start) != 0) {
1115 int i = 0;
1116 /*
1117 * Get rid of leading spaces.
1118 */
1119 while (start[i] == ' ')
1120 i++;
1121
1122 if ((x - start) <= out_len) {
1123 strcpy(out_buf, (start + i));
1124 *offset += (x - start) + 1;
1125 return 1;
1126 }
1127 }
1128 return 0;
1129}
1130
1131static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
1132{
1133 int ret;
1134
1135 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
1136
1137 if (ret < 0)
1138 return HV_E_FAIL;
1139
1140 return 0;
1141}
1142
1143
1144static int process_ip_string(FILE *f, char *ip_string, int type)
1145{
1146 int error = 0;
1147 char addr[INET6_ADDRSTRLEN];
1148 int i = 0;
1149 int j = 0;
1150 char str[256];
1151 char sub_str[10];
1152 int offset = 0;
1153
1154 memset(addr, 0, sizeof(addr));
1155
1156 while (parse_ip_val_buffer(ip_string, &offset, addr,
1157 (MAX_IP_ADDR_SIZE * 2))) {
1158
1159 sub_str[0] = 0;
1160 if (is_ipv4(addr)) {
1161 switch (type) {
1162 case IPADDR:
1163 snprintf(str, sizeof(str), "%s", "IPADDR");
1164 break;
1165 case NETMASK:
1166 snprintf(str, sizeof(str), "%s", "NETMASK");
1167 break;
1168 case GATEWAY:
1169 snprintf(str, sizeof(str), "%s", "GATEWAY");
1170 break;
1171 case DNS:
1172 snprintf(str, sizeof(str), "%s", "DNS");
1173 break;
1174 }
Tomas Hozza0783d722013-01-13 22:27:40 +01001175
1176 if (type == DNS) {
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001177 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
Tomas Hozza0783d722013-01-13 22:27:40 +01001178 } else if (type == GATEWAY && i == 0) {
1179 ++i;
1180 } else {
1181 snprintf(sub_str, sizeof(sub_str), "%d", i++);
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001182 }
1183
1184
1185 } else if (expand_ipv6(addr, type)) {
1186 switch (type) {
1187 case IPADDR:
1188 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1189 break;
1190 case NETMASK:
1191 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1192 break;
1193 case GATEWAY:
1194 snprintf(str, sizeof(str), "%s",
1195 "IPV6_DEFAULTGW");
1196 break;
1197 case DNS:
1198 snprintf(str, sizeof(str), "%s", "DNS");
1199 break;
1200 }
Tomas Hozza0783d722013-01-13 22:27:40 +01001201
1202 if (type == DNS) {
1203 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1204 } else if (j == 0) {
1205 ++j;
1206 } else {
1207 snprintf(sub_str, sizeof(sub_str), "_%d", j++);
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001208 }
1209 } else {
1210 return HV_INVALIDARG;
1211 }
1212
1213 error = kvp_write_file(f, str, sub_str, addr);
1214 if (error)
1215 return error;
1216 memset(addr, 0, sizeof(addr));
1217 }
1218
1219 return 0;
1220}
1221
1222static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1223{
1224 int error = 0;
1225 char if_file[128];
1226 FILE *file;
1227 char cmd[512];
1228 char *mac_addr;
1229
1230 /*
1231 * Set the configuration for the specified interface with
1232 * the information provided. Since there is no standard
1233 * way to configure an interface, we will have an external
1234 * script that does the job of configuring the interface and
1235 * flushing the configuration.
1236 *
1237 * The parameters passed to this external script are:
1238 * 1. A configuration file that has the specified configuration.
1239 *
1240 * We will embed the name of the interface in the configuration
1241 * file: ifcfg-ethx (where ethx is the interface name).
1242 *
1243 * The information provided here may be more than what is needed
1244 * in a given distro to configure the interface and so are free
1245 * ignore information that may not be relevant.
1246 *
1247 * Here is the format of the ip configuration file:
1248 *
1249 * HWADDR=macaddr
Tomas Hozza0783d722013-01-13 22:27:40 +01001250 * DEVICE=interface name
1251 * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
1252 * or "none" if no boot-time protocol should be used)
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001253 *
Tomas Hozza0783d722013-01-13 22:27:40 +01001254 * IPADDR0=ipaddr1
1255 * IPADDR1=ipaddr2
1256 * IPADDRx=ipaddry (where y = x + 1)
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001257 *
Tomas Hozza0783d722013-01-13 22:27:40 +01001258 * NETMASK0=netmask1
1259 * NETMASKx=netmasky (where y = x + 1)
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001260 *
1261 * GATEWAY=ipaddr1
Tomas Hozza0783d722013-01-13 22:27:40 +01001262 * GATEWAYx=ipaddry (where y = x + 1)
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001263 *
1264 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1265 *
1266 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1267 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1268 * IPV6NETMASK.
1269 *
1270 * The host can specify multiple ipv4 and ipv6 addresses to be
1271 * configured for the interface. Furthermore, the configuration
1272 * needs to be persistent. A subsequent GET call on the interface
1273 * is expected to return the configuration that is set via the SET
1274 * call.
1275 */
1276
1277 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
Tomas Hozza40424f52012-11-27 08:56:33 +01001278 "/ifcfg-", if_name);
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001279
1280 file = fopen(if_file, "w");
1281
1282 if (file == NULL) {
1283 syslog(LOG_ERR, "Failed to open config file");
1284 return HV_E_FAIL;
1285 }
1286
1287 /*
1288 * First write out the MAC address.
1289 */
1290
1291 mac_addr = kvp_if_name_to_mac(if_name);
1292 if (mac_addr == NULL) {
1293 error = HV_E_FAIL;
1294 goto setval_error;
1295 }
1296
1297 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1298 if (error)
1299 goto setval_error;
1300
Tomas Hozza0783d722013-01-13 22:27:40 +01001301 error = kvp_write_file(file, "DEVICE", "", if_name);
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001302 if (error)
1303 goto setval_error;
1304
1305 if (new_val->dhcp_enabled) {
Tomas Hozza0783d722013-01-13 22:27:40 +01001306 error = kvp_write_file(file, "BOOTPROTO", "", "dhcp");
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001307 if (error)
1308 goto setval_error;
1309
1310 /*
1311 * We are done!.
1312 */
1313 goto setval_done;
Tomas Hozza0783d722013-01-13 22:27:40 +01001314
1315 } else {
1316 error = kvp_write_file(file, "BOOTPROTO", "", "none");
1317 if (error)
1318 goto setval_error;
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001319 }
1320
1321 /*
1322 * Write the configuration for ipaddress, netmask, gateway and
1323 * name servers.
1324 */
1325
1326 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1327 if (error)
1328 goto setval_error;
1329
1330 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1331 if (error)
1332 goto setval_error;
1333
1334 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1335 if (error)
1336 goto setval_error;
1337
1338 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1339 if (error)
1340 goto setval_error;
1341
1342setval_done:
1343 free(mac_addr);
1344 fclose(file);
1345
1346 /*
1347 * Now that we have populated the configuration file,
1348 * invoke the external script to do its magic.
1349 */
1350
1351 snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1352 system(cmd);
1353 return 0;
1354
1355setval_error:
1356 syslog(LOG_ERR, "Failed to write config file");
1357 free(mac_addr);
1358 fclose(file);
1359 return error;
1360}
1361
1362
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001363static int
1364kvp_get_domain_name(char *buffer, int length)
1365{
1366 struct addrinfo hints, *info ;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001367 int error = 0;
1368
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -07001369 gethostname(buffer, length);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001370 memset(&hints, 0, sizeof(hints));
1371 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1372 hints.ai_socktype = SOCK_STREAM;
1373 hints.ai_flags = AI_CANONNAME;
1374
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -07001375 error = getaddrinfo(buffer, NULL, &hints, &info);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001376 if (error != 0) {
1377 strcpy(buffer, "getaddrinfo failed\n");
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -07001378 return error;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001379 }
1380 strcpy(buffer, info->ai_canonname);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001381 freeaddrinfo(info);
1382 return error;
1383}
1384
1385static int
1386netlink_send(int fd, struct cn_msg *msg)
1387{
1388 struct nlmsghdr *nlh;
1389 unsigned int size;
1390 struct msghdr message;
1391 char buffer[64];
1392 struct iovec iov[2];
1393
1394 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
1395
1396 nlh = (struct nlmsghdr *)buffer;
1397 nlh->nlmsg_seq = 0;
1398 nlh->nlmsg_pid = getpid();
1399 nlh->nlmsg_type = NLMSG_DONE;
1400 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
1401 nlh->nlmsg_flags = 0;
1402
1403 iov[0].iov_base = nlh;
1404 iov[0].iov_len = sizeof(*nlh);
1405
1406 iov[1].iov_base = msg;
1407 iov[1].iov_len = size;
1408
1409 memset(&message, 0, sizeof(message));
1410 message.msg_name = &addr;
1411 message.msg_namelen = sizeof(addr);
1412 message.msg_iov = iov;
1413 message.msg_iovlen = 2;
1414
1415 return sendmsg(fd, &message, 0);
1416}
1417
Olaf Hering7989f7d2011-03-22 10:02:17 +01001418int main(void)
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001419{
Tomas Hozzaf4685fa2013-03-13 14:14:13 +01001420 int fd, len, nl_group;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001421 int error;
1422 struct cn_msg *message;
1423 struct pollfd pfd;
1424 struct nlmsghdr *incoming_msg;
1425 struct cn_msg *incoming_cn_msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001426 struct hv_kvp_msg *hv_msg;
Olaf Hering7989f7d2011-03-22 10:02:17 +01001427 char *p;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001428 char *key_value;
1429 char *key_name;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001430 int op;
1431 int pool;
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001432 char *if_name;
1433 struct hv_kvp_ipaddr_value *kvp_ip_val;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001434
1435 daemon(1, 0);
1436 openlog("KVP", 0, LOG_USER);
1437 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1438 /*
1439 * Retrieve OS release information.
1440 */
1441 kvp_get_os_info();
1442
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001443 if (kvp_file_init()) {
1444 syslog(LOG_ERR, "Failed to initialize the pools");
Ben Hutchings6bb22fe2012-09-05 14:37:36 -07001445 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001446 }
1447
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001448 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
1449 if (fd < 0) {
1450 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -07001451 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001452 }
1453 addr.nl_family = AF_NETLINK;
1454 addr.nl_pad = 0;
1455 addr.nl_pid = 0;
Tomas Hozza77d6a522013-03-13 14:14:12 +01001456 addr.nl_groups = 0;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001457
1458
1459 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
1460 if (error < 0) {
1461 syslog(LOG_ERR, "bind failed; error:%d", error);
1462 close(fd);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -07001463 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001464 }
Tomas Hozzaf4685fa2013-03-13 14:14:13 +01001465 nl_group = CN_KVP_IDX;
Tomas Hozza35901602013-05-22 14:54:30 +02001466
1467 if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &nl_group, sizeof(nl_group)) < 0) {
1468 syslog(LOG_ERR, "setsockopt failed; error: %d %s", errno, strerror(errno));
1469 close(fd);
1470 exit(EXIT_FAILURE);
1471 }
1472
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001473 /*
1474 * Register ourselves with the kernel.
1475 */
1476 message = (struct cn_msg *)kvp_send_buffer;
1477 message->id.idx = CN_KVP_IDX;
1478 message->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001479
1480 hv_msg = (struct hv_kvp_msg *)message->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001481 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001482 message->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001483 message->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001484
1485 len = netlink_send(fd, message);
1486 if (len < 0) {
1487 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
1488 close(fd);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -07001489 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001490 }
1491
1492 pfd.fd = fd;
1493
1494 while (1) {
Olaf Heringbcc2c9c2012-05-31 16:40:06 +02001495 struct sockaddr *addr_p = (struct sockaddr *) &addr;
1496 socklen_t addr_l = sizeof(addr);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001497 pfd.events = POLLIN;
1498 pfd.revents = 0;
Tomas Hozza4d81e302013-05-22 14:54:31 +02001499
1500 if (poll(&pfd, 1, -1) < 0) {
1501 syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
1502 if (errno == EINVAL) {
1503 close(fd);
1504 exit(EXIT_FAILURE);
1505 }
1506 else
1507 continue;
1508 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001509
Olaf Heringbcc2c9c2012-05-31 16:40:06 +02001510 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
1511 addr_p, &addr_l);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001512
Tomas Hozza95a69ad2012-11-08 10:53:29 +01001513 if (len < 0) {
Olaf Heringbcc2c9c2012-05-31 16:40:06 +02001514 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
1515 addr.nl_pid, errno, strerror(errno));
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001516 close(fd);
1517 return -1;
1518 }
1519
Tomas Hozza95a69ad2012-11-08 10:53:29 +01001520 if (addr.nl_pid) {
1521 syslog(LOG_WARNING, "Received packet from untrusted pid:%u",
1522 addr.nl_pid);
1523 continue;
1524 }
1525
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001526 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
Tomas Hozza75db6012013-03-13 14:14:14 +01001527
1528 if (incoming_msg->nlmsg_type != NLMSG_DONE)
1529 continue;
1530
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001531 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001532 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001533
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001534 /*
1535 * We will use the KVP header information to pass back
1536 * the error from this daemon. So, first copy the state
1537 * and set the error code to success.
1538 */
1539 op = hv_msg->kvp_hdr.operation;
1540 pool = hv_msg->kvp_hdr.pool;
1541 hv_msg->error = HV_S_OK;
1542
1543 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001544 /*
1545 * Driver is registering with us; stash away the version
1546 * information.
1547 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001548 in_hand_shake = 0;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -08001549 p = (char *)hv_msg->body.kvp_register.version;
Olaf Hering7989f7d2011-03-22 10:02:17 +01001550 lic_version = malloc(strlen(p) + 1);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001551 if (lic_version) {
Olaf Hering7989f7d2011-03-22 10:02:17 +01001552 strcpy(lic_version, p);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001553 syslog(LOG_INFO, "KVP LIC Version: %s",
1554 lic_version);
1555 } else {
1556 syslog(LOG_ERR, "malloc failed");
1557 }
1558 continue;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001559 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001560
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001561 switch (op) {
K. Y. Srinivasan16e87102012-09-05 13:50:15 -07001562 case KVP_OP_GET_IP_INFO:
1563 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1564 if_name =
1565 kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
1566
1567 if (if_name == NULL) {
1568 /*
1569 * We could not map the mac address to an
1570 * interface name; return error.
1571 */
1572 hv_msg->error = HV_E_FAIL;
1573 break;
1574 }
1575 error = kvp_get_ip_info(
1576 0, if_name, KVP_OP_GET_IP_INFO,
1577 kvp_ip_val,
1578 (MAX_IP_ADDR_SIZE * 2));
1579
1580 if (error)
1581 hv_msg->error = error;
1582
1583 free(if_name);
1584 break;
1585
K. Y. Srinivasan32061b42012-09-05 13:50:13 -07001586 case KVP_OP_SET_IP_INFO:
1587 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1588 if_name = kvp_get_if_name(
1589 (char *)kvp_ip_val->adapter_id);
1590 if (if_name == NULL) {
1591 /*
1592 * We could not map the guid to an
1593 * interface name; return error.
1594 */
1595 hv_msg->error = HV_GUID_NOTFOUND;
1596 break;
1597 }
1598 error = kvp_set_ip_info(if_name, kvp_ip_val);
1599 if (error)
1600 hv_msg->error = error;
1601
1602 free(if_name);
1603 break;
1604
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -07001605 case KVP_OP_SET:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001606 if (kvp_key_add_or_modify(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001607 hv_msg->body.kvp_set.data.key,
1608 hv_msg->body.kvp_set.data.key_size,
1609 hv_msg->body.kvp_set.data.value,
1610 hv_msg->body.kvp_set.data.value_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001611 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001612 break;
1613
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -07001614 case KVP_OP_GET:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001615 if (kvp_get_value(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001616 hv_msg->body.kvp_set.data.key,
1617 hv_msg->body.kvp_set.data.key_size,
1618 hv_msg->body.kvp_set.data.value,
1619 hv_msg->body.kvp_set.data.value_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001620 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001621 break;
1622
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -07001623 case KVP_OP_DELETE:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001624 if (kvp_key_delete(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001625 hv_msg->body.kvp_delete.key,
1626 hv_msg->body.kvp_delete.key_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001627 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -07001628 break;
1629
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001630 default:
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001631 break;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001632 }
1633
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001634 if (op != KVP_OP_ENUMERATE)
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -07001635 goto kvp_done;
1636
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -07001637 /*
1638 * If the pool is KVP_POOL_AUTO, dynamically generate
1639 * both the key and the value; if not read from the
1640 * appropriate pool.
1641 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001642 if (pool != KVP_POOL_AUTO) {
1643 if (kvp_pool_enumerate(pool,
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -07001644 hv_msg->body.kvp_enum_data.index,
1645 hv_msg->body.kvp_enum_data.data.key,
1646 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1647 hv_msg->body.kvp_enum_data.data.value,
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001648 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1649 hv_msg->error = HV_S_CONT;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -07001650 goto kvp_done;
1651 }
1652
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001653 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1654 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1655 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001656
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001657 switch (hv_msg->body.kvp_enum_data.index) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001658 case FullyQualifiedDomainName:
1659 kvp_get_domain_name(key_value,
1660 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1661 strcpy(key_name, "FullyQualifiedDomainName");
1662 break;
1663 case IntegrationServicesVersion:
1664 strcpy(key_name, "IntegrationServicesVersion");
1665 strcpy(key_value, lic_version);
1666 break;
1667 case NetworkAddressIPv4:
K. Y. Srinivasan4a3b97e52012-09-05 13:50:14 -07001668 kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -07001669 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001670 strcpy(key_name, "NetworkAddressIPv4");
1671 break;
1672 case NetworkAddressIPv6:
K. Y. Srinivasan4a3b97e52012-09-05 13:50:14 -07001673 kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -07001674 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001675 strcpy(key_name, "NetworkAddressIPv6");
1676 break;
1677 case OSBuildNumber:
1678 strcpy(key_value, os_build);
1679 strcpy(key_name, "OSBuildNumber");
1680 break;
1681 case OSName:
1682 strcpy(key_value, os_name);
1683 strcpy(key_name, "OSName");
1684 break;
1685 case OSMajorVersion:
1686 strcpy(key_value, os_major);
1687 strcpy(key_name, "OSMajorVersion");
1688 break;
1689 case OSMinorVersion:
1690 strcpy(key_value, os_minor);
1691 strcpy(key_name, "OSMinorVersion");
1692 break;
1693 case OSVersion:
K. Y. Srinivasanf426a362012-10-25 14:15:49 -07001694 strcpy(key_value, os_version);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001695 strcpy(key_name, "OSVersion");
1696 break;
1697 case ProcessorArchitecture:
1698 strcpy(key_value, processor_arch);
1699 strcpy(key_name, "ProcessorArchitecture");
1700 break;
1701 default:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001702 hv_msg->error = HV_S_CONT;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001703 break;
1704 }
1705 /*
1706 * Send the value back to the kernel. The response is
1707 * already in the receive buffer. Update the cn_msg header to
1708 * reflect the key value that has been added to the message
1709 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -07001710kvp_done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001711
1712 incoming_cn_msg->id.idx = CN_KVP_IDX;
1713 incoming_cn_msg->id.val = CN_KVP_VAL;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001714 incoming_cn_msg->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001715 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001716
1717 len = netlink_send(fd, incoming_cn_msg);
1718 if (len < 0) {
1719 syslog(LOG_ERR, "net_link send failed; error:%d", len);
Ben Hutchings6bb22fe2012-09-05 14:37:36 -07001720 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001721 }
1722 }
1723
1724}