blob: 65d54c89394ea6743148cca6d984afaf03b865b5 [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>
34#include <errno.h>
35#include <arpa/inet.h>
36#include <linux/connector.h>
K. Y. Srinivasaneab6af72012-02-02 16:56:49 -080037#include <linux/hyperv.h>
Ky Srinivasancc04acf2010-12-16 18:56:54 -070038#include <linux/netlink.h>
Ky Srinivasancc04acf2010-12-16 18:56:54 -070039#include <ifaddrs.h>
40#include <netdb.h>
41#include <syslog.h>
K. Y. Srinivasandb425332012-03-16 08:02:26 -070042#include <sys/stat.h>
43#include <fcntl.h>
Ky Srinivasancc04acf2010-12-16 18:56:54 -070044
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 Srinivasancc04acf2010-12-16 18:56:54 -070055 */
56
Ky Srinivasancc04acf2010-12-16 18:56:54 -070057
58enum 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
Ky Srinivasancc04acf2010-12-16 18:56:54 -070071static char kvp_send_buffer[4096];
K. Y. Srinivasan9b595782012-08-13 10:06:51 -070072static char kvp_recv_buffer[4096 * 2];
Ky Srinivasancc04acf2010-12-16 18:56:54 -070073static struct sockaddr_nl addr;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070074static int in_hand_shake = 1;
Ky Srinivasancc04acf2010-12-16 18:56:54 -070075
Olaf Hering7989f7d2011-03-22 10:02:17 +010076static char *os_name = "";
77static char *os_major = "";
78static char *os_minor = "";
79static char *processor_arch;
80static char *os_build;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -070081static char *lic_version = "Unknown version";
Olaf Hering7989f7d2011-03-22 10:02:17 +010082static struct utsname uts_buf;
Ky Srinivasancc04acf2010-12-16 18:56:54 -070083
K. Y. Srinivasandb425332012-03-16 08:02:26 -070084
85#define MAX_FILE_NAME 100
86#define ENTRIES_PER_BLOCK 50
87
88struct kvp_record {
89 __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
90 __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
91};
92
93struct kvp_file_state {
94 int fd;
95 int num_blocks;
96 struct kvp_record *records;
97 int num_records;
98 __u8 fname[MAX_FILE_NAME];
99};
100
101static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
102
103static void kvp_acquire_lock(int pool)
104{
105 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
106 fl.l_pid = getpid();
107
108 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
109 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
110 exit(-1);
111 }
112}
113
114static void kvp_release_lock(int pool)
115{
116 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
117 fl.l_pid = getpid();
118
119 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
120 perror("fcntl");
121 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
122 exit(-1);
123 }
124}
125
126static void kvp_update_file(int pool)
127{
128 FILE *filep;
129 size_t bytes_written;
130
131 /*
132 * We are going to write our in-memory registry out to
133 * disk; acquire the lock first.
134 */
135 kvp_acquire_lock(pool);
136
137 filep = fopen(kvp_file_info[pool].fname, "w");
138 if (!filep) {
139 kvp_release_lock(pool);
140 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
141 exit(-1);
142 }
143
144 bytes_written = fwrite(kvp_file_info[pool].records,
145 sizeof(struct kvp_record),
146 kvp_file_info[pool].num_records, filep);
147
148 fflush(filep);
149 kvp_release_lock(pool);
150}
151
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700152static void kvp_update_mem_state(int pool)
153{
154 FILE *filep;
155 size_t records_read = 0;
156 struct kvp_record *record = kvp_file_info[pool].records;
157 struct kvp_record *readp;
158 int num_blocks = kvp_file_info[pool].num_blocks;
159 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
160
161 kvp_acquire_lock(pool);
162
163 filep = fopen(kvp_file_info[pool].fname, "r");
164 if (!filep) {
165 kvp_release_lock(pool);
166 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
167 exit(-1);
168 }
169 while (!feof(filep)) {
170 readp = &record[records_read];
171 records_read += fread(readp, sizeof(struct kvp_record),
172 ENTRIES_PER_BLOCK * num_blocks,
173 filep);
174
175 if (!feof(filep)) {
176 /*
177 * We have more data to read.
178 */
179 num_blocks++;
180 record = realloc(record, alloc_unit * num_blocks);
181
182 if (record == NULL) {
183 syslog(LOG_ERR, "malloc failed");
184 exit(-1);
185 }
186 continue;
187 }
188 break;
189 }
190
191 kvp_file_info[pool].num_blocks = num_blocks;
192 kvp_file_info[pool].records = record;
193 kvp_file_info[pool].num_records = records_read;
194
195 kvp_release_lock(pool);
196}
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700197static int kvp_file_init(void)
198{
199 int ret, fd;
200 FILE *filep;
201 size_t records_read;
202 __u8 *fname;
203 struct kvp_record *record;
204 struct kvp_record *readp;
205 int num_blocks;
206 int i;
207 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
208
209 if (access("/var/opt/hyperv", F_OK)) {
210 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
211 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
212 exit(-1);
213 }
214 }
215
216 for (i = 0; i < KVP_POOL_COUNT; i++) {
217 fname = kvp_file_info[i].fname;
218 records_read = 0;
219 num_blocks = 1;
220 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
221 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
222
223 if (fd == -1)
224 return 1;
225
226
227 filep = fopen(fname, "r");
228 if (!filep)
229 return 1;
230
231 record = malloc(alloc_unit * num_blocks);
232 if (record == NULL) {
233 fclose(filep);
234 return 1;
235 }
236 while (!feof(filep)) {
237 readp = &record[records_read];
238 records_read += fread(readp, sizeof(struct kvp_record),
239 ENTRIES_PER_BLOCK,
240 filep);
241
242 if (!feof(filep)) {
243 /*
244 * We have more data to read.
245 */
246 num_blocks++;
247 record = realloc(record, alloc_unit *
248 num_blocks);
249 if (record == NULL) {
250 fclose(filep);
251 return 1;
252 }
253 continue;
254 }
255 break;
256 }
257 kvp_file_info[i].fd = fd;
258 kvp_file_info[i].num_blocks = num_blocks;
259 kvp_file_info[i].records = record;
260 kvp_file_info[i].num_records = records_read;
261 fclose(filep);
262
263 }
264
265 return 0;
266}
267
268static int kvp_key_delete(int pool, __u8 *key, int key_size)
269{
270 int i;
271 int j, k;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700272 int num_records;
273 struct kvp_record *record;
274
275 /*
276 * First update the in-memory state.
277 */
278 kvp_update_mem_state(pool);
279
280 num_records = kvp_file_info[pool].num_records;
281 record = kvp_file_info[pool].records;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700282
283 for (i = 0; i < num_records; i++) {
284 if (memcmp(key, record[i].key, key_size))
285 continue;
286 /*
287 * Found a match; just move the remaining
288 * entries up.
289 */
290 if (i == num_records) {
291 kvp_file_info[pool].num_records--;
292 kvp_update_file(pool);
293 return 0;
294 }
295
296 j = i;
297 k = j + 1;
298 for (; k < num_records; k++) {
299 strcpy(record[j].key, record[k].key);
300 strcpy(record[j].value, record[k].value);
301 j++;
302 }
303
304 kvp_file_info[pool].num_records--;
305 kvp_update_file(pool);
306 return 0;
307 }
308 return 1;
309}
310
311static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
312 int value_size)
313{
314 int i;
315 int j, k;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700316 int num_records;
317 struct kvp_record *record;
318 int num_blocks;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700319
320 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
321 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
322 return 1;
323
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700324 /*
325 * First update the in-memory state.
326 */
327 kvp_update_mem_state(pool);
328
329 num_records = kvp_file_info[pool].num_records;
330 record = kvp_file_info[pool].records;
331 num_blocks = kvp_file_info[pool].num_blocks;
332
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700333 for (i = 0; i < num_records; i++) {
334 if (memcmp(key, record[i].key, key_size))
335 continue;
336 /*
337 * Found a match; just update the value -
338 * this is the modify case.
339 */
340 memcpy(record[i].value, value, value_size);
341 kvp_update_file(pool);
342 return 0;
343 }
344
345 /*
346 * Need to add a new entry;
347 */
348 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
349 /* Need to allocate a larger array for reg entries. */
350 record = realloc(record, sizeof(struct kvp_record) *
351 ENTRIES_PER_BLOCK * (num_blocks + 1));
352
353 if (record == NULL)
354 return 1;
355 kvp_file_info[pool].num_blocks++;
356
357 }
358 memcpy(record[i].value, value, value_size);
359 memcpy(record[i].key, key, key_size);
360 kvp_file_info[pool].records = record;
361 kvp_file_info[pool].num_records++;
362 kvp_update_file(pool);
363 return 0;
364}
365
366static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
367 int value_size)
368{
369 int i;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700370 int num_records;
371 struct kvp_record *record;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700372
373 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
374 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
375 return 1;
376
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700377 /*
378 * First update the in-memory state.
379 */
380 kvp_update_mem_state(pool);
381
382 num_records = kvp_file_info[pool].num_records;
383 record = kvp_file_info[pool].records;
384
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700385 for (i = 0; i < num_records; i++) {
386 if (memcmp(key, record[i].key, key_size))
387 continue;
388 /*
389 * Found a match; just copy the value out.
390 */
391 memcpy(value, record[i].value, value_size);
392 return 0;
393 }
394
395 return 1;
396}
397
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700398static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700399 __u8 *value, int value_size)
400{
401 struct kvp_record *record;
402
403 /*
404 * First update our in-memory database.
405 */
406 kvp_update_mem_state(pool);
407 record = kvp_file_info[pool].records;
408
409 if (index >= kvp_file_info[pool].num_records) {
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700410 return 1;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700411 }
412
413 memcpy(key, record[index].key, key_size);
414 memcpy(value, record[index].value, value_size);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700415 return 0;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700416}
417
418
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700419void kvp_get_os_info(void)
420{
421 FILE *file;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100422 char *p, buf[512];
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700423
Olaf Hering7989f7d2011-03-22 10:02:17 +0100424 uname(&uts_buf);
425 os_build = uts_buf.release;
K. Y. Srinivasan064931d2011-07-19 11:44:20 -0700426 processor_arch = uts_buf.machine;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700427
K. Y. Srinivasane54bbc62011-07-22 10:14:31 -0700428 /*
429 * The current windows host (win7) expects the build
430 * string to be of the form: x.y.z
431 * Strip additional information we may have.
432 */
433 p = strchr(os_build, '-');
434 if (p)
435 *p = '\0';
436
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700437 file = fopen("/etc/SuSE-release", "r");
438 if (file != NULL)
439 goto kvp_osinfo_found;
440 file = fopen("/etc/redhat-release", "r");
441 if (file != NULL)
442 goto kvp_osinfo_found;
443 /*
444 * Add code for other supported platforms.
445 */
446
447 /*
448 * We don't have information about the os.
449 */
Olaf Hering7989f7d2011-03-22 10:02:17 +0100450 os_name = uts_buf.sysname;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700451 return;
452
453kvp_osinfo_found:
Olaf Hering7989f7d2011-03-22 10:02:17 +0100454 /* up to three lines */
455 p = fgets(buf, sizeof(buf), file);
456 if (p) {
457 p = strchr(buf, '\n');
458 if (p)
459 *p = '\0';
460 p = strdup(buf);
461 if (!p)
462 goto done;
463 os_name = p;
464
465 /* second line */
466 p = fgets(buf, sizeof(buf), file);
467 if (p) {
468 p = strchr(buf, '\n');
469 if (p)
470 *p = '\0';
471 p = strdup(buf);
472 if (!p)
473 goto done;
474 os_major = p;
475
476 /* third line */
477 p = fgets(buf, sizeof(buf), file);
478 if (p) {
479 p = strchr(buf, '\n');
480 if (p)
481 *p = '\0';
482 p = strdup(buf);
483 if (p)
484 os_minor = p;
485 }
486 }
487 }
488
489done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700490 fclose(file);
491 return;
492}
493
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700494static void kvp_process_ipconfig_file(char *cmd,
495 char *config_buf, int len,
496 int element_size, int offset)
497{
498 char buf[256];
499 char *p;
500 char *x;
501 FILE *file;
502
503 /*
504 * First execute the command.
505 */
506 file = popen(cmd, "r");
507 if (file == NULL)
508 return;
509
510 if (offset == 0)
511 memset(config_buf, 0, len);
512 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
513 if ((len - strlen(config_buf)) < (element_size + 1))
514 break;
515
516 x = strchr(p, '\n');
517 *x = '\0';
518 strcat(config_buf, p);
519 strcat(config_buf, ";");
520 }
521 pclose(file);
522}
523
524static void kvp_get_ipconfig_info(char *if_name,
525 struct hv_kvp_ipaddr_value *buffer)
526{
527 char cmd[512];
528
529 /*
530 * Get the address of default gateway (ipv4).
531 */
532 sprintf(cmd, "%s %s", "ip route show dev", if_name);
533 strcat(cmd, " | awk '/default/ {print $3 }'");
534
535 /*
536 * Execute the command to gather gateway info.
537 */
538 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
539 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
540
541 /*
542 * Get the address of default gateway (ipv6).
543 */
544 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
545 strcat(cmd, " | awk '/default/ {print $3 }'");
546
547 /*
548 * Execute the command to gather gateway info (ipv6).
549 */
550 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
551 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
552
553}
554
555
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700556static unsigned int hweight32(unsigned int *w)
557{
558 unsigned int res = *w - ((*w >> 1) & 0x55555555);
559 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
560 res = (res + (res >> 4)) & 0x0F0F0F0F;
561 res = res + (res >> 8);
562 return (res + (res >> 16)) & 0x000000FF;
563}
564
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700565static int kvp_process_ip_address(void *addrp,
566 int family, char *buffer,
567 int length, int *offset)
568{
569 struct sockaddr_in *addr;
570 struct sockaddr_in6 *addr6;
571 int addr_length;
572 char tmp[50];
573 const char *str;
574
575 if (family == AF_INET) {
576 addr = (struct sockaddr_in *)addrp;
577 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
578 addr_length = INET_ADDRSTRLEN;
579 } else {
580 addr6 = (struct sockaddr_in6 *)addrp;
581 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
582 addr_length = INET6_ADDRSTRLEN;
583 }
584
585 if ((length - *offset) < addr_length + 1)
586 return 1;
587 if (str == NULL) {
588 strcpy(buffer, "inet_ntop failed\n");
589 return 1;
590 }
591 if (*offset == 0)
592 strcpy(buffer, tmp);
593 else
594 strcat(buffer, tmp);
595 strcat(buffer, ";");
596
597 *offset += strlen(str) + 1;
598 return 0;
599}
600
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700601static int
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700602kvp_get_ip_address(int family, char *if_name, int op,
603 void *out_buffer, int length)
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700604{
605 struct ifaddrs *ifap;
606 struct ifaddrs *curp;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700607 int offset = 0;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700608 int sn_offset = 0;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700609 int error = 0;
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700610 char *buffer;
611 struct hv_kvp_ipaddr_value *ip_buffer;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700612 char cidr_mask[5]; /* /xyz */
613 int weight;
614 int i;
615 unsigned int *w;
616 char *sn_str;
617 struct sockaddr_in6 *addr6;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700618
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700619 if (op == KVP_OP_ENUMERATE) {
620 buffer = out_buffer;
621 } else {
622 ip_buffer = out_buffer;
623 buffer = (char *)ip_buffer->ip_addr;
624 ip_buffer->addr_family = 0;
625 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700626 /*
627 * On entry into this function, the buffer is capable of holding the
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700628 * maximum key value.
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700629 */
630
631 if (getifaddrs(&ifap)) {
632 strcpy(buffer, "getifaddrs failed\n");
633 return 1;
634 }
635
636 curp = ifap;
637 while (curp != NULL) {
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700638 if (curp->ifa_addr == NULL) {
639 curp = curp->ifa_next;
640 continue;
641 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700642
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700643 if ((if_name != NULL) &&
644 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
645 /*
646 * We want info about a specific interface;
647 * just continue.
648 */
649 curp = curp->ifa_next;
650 continue;
651 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700652
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700653 /*
654 * We only support two address families: AF_INET and AF_INET6.
655 * If a family value of 0 is specified, we collect both
656 * supported address families; if not we gather info on
657 * the specified address family.
658 */
659 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
660 curp = curp->ifa_next;
661 continue;
662 }
663 if ((curp->ifa_addr->sa_family != AF_INET) &&
664 (curp->ifa_addr->sa_family != AF_INET6)) {
665 curp = curp->ifa_next;
666 continue;
667 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700668
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700669 if (op == KVP_OP_GET_IP_INFO) {
670 /*
671 * Gather info other than the IP address.
672 * IP address info will be gathered later.
673 */
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700674 if (curp->ifa_addr->sa_family == AF_INET) {
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700675 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700676 /*
677 * Get subnet info.
678 */
679 error = kvp_process_ip_address(
680 curp->ifa_netmask,
681 AF_INET,
682 (char *)
683 ip_buffer->sub_net,
684 length,
685 &sn_offset);
686 if (error)
687 goto gather_ipaddr;
688 } else {
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700689 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700690
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700691 /*
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700692 * Get subnet info in CIDR format.
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700693 */
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700694 weight = 0;
695 sn_str = (char *)ip_buffer->sub_net;
696 addr6 = (struct sockaddr_in6 *)
697 curp->ifa_netmask;
698 w = addr6->sin6_addr.s6_addr32;
699
700 for (i = 0; i < 4; i++)
701 weight += hweight32(&w[i]);
702
703 sprintf(cidr_mask, "/%d", weight);
704 if ((length - sn_offset) <
705 (strlen(cidr_mask) + 1))
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700706 goto gather_ipaddr;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700707
708 if (sn_offset == 0)
709 strcpy(sn_str, cidr_mask);
710 else
711 strcat(sn_str, cidr_mask);
712 strcat((char *)ip_buffer->sub_net, ";");
713 sn_offset += strlen(sn_str) + 1;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700714 }
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700715
716 /*
717 * Collect other ip related configuration info.
718 */
719
720 kvp_get_ipconfig_info(if_name, ip_buffer);
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700721 }
722
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700723gather_ipaddr:
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700724 error = kvp_process_ip_address(curp->ifa_addr,
725 curp->ifa_addr->sa_family,
726 buffer,
727 length, &offset);
728 if (error)
729 goto getaddr_done;
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700730
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700731 curp = curp->ifa_next;
732 }
733
734getaddr_done:
735 freeifaddrs(ifap);
736 return error;
737}
738
739
740static int
741kvp_get_domain_name(char *buffer, int length)
742{
743 struct addrinfo hints, *info ;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700744 int error = 0;
745
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700746 gethostname(buffer, length);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700747 memset(&hints, 0, sizeof(hints));
748 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
749 hints.ai_socktype = SOCK_STREAM;
750 hints.ai_flags = AI_CANONNAME;
751
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700752 error = getaddrinfo(buffer, NULL, &hints, &info);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700753 if (error != 0) {
754 strcpy(buffer, "getaddrinfo failed\n");
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700755 return error;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700756 }
757 strcpy(buffer, info->ai_canonname);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700758 freeaddrinfo(info);
759 return error;
760}
761
762static int
763netlink_send(int fd, struct cn_msg *msg)
764{
765 struct nlmsghdr *nlh;
766 unsigned int size;
767 struct msghdr message;
768 char buffer[64];
769 struct iovec iov[2];
770
771 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
772
773 nlh = (struct nlmsghdr *)buffer;
774 nlh->nlmsg_seq = 0;
775 nlh->nlmsg_pid = getpid();
776 nlh->nlmsg_type = NLMSG_DONE;
777 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
778 nlh->nlmsg_flags = 0;
779
780 iov[0].iov_base = nlh;
781 iov[0].iov_len = sizeof(*nlh);
782
783 iov[1].iov_base = msg;
784 iov[1].iov_len = size;
785
786 memset(&message, 0, sizeof(message));
787 message.msg_name = &addr;
788 message.msg_namelen = sizeof(addr);
789 message.msg_iov = iov;
790 message.msg_iovlen = 2;
791
792 return sendmsg(fd, &message, 0);
793}
794
Olaf Hering7989f7d2011-03-22 10:02:17 +0100795int main(void)
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700796{
797 int fd, len, sock_opt;
798 int error;
799 struct cn_msg *message;
800 struct pollfd pfd;
801 struct nlmsghdr *incoming_msg;
802 struct cn_msg *incoming_cn_msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800803 struct hv_kvp_msg *hv_msg;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100804 char *p;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700805 char *key_value;
806 char *key_name;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700807 int op;
808 int pool;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700809
810 daemon(1, 0);
811 openlog("KVP", 0, LOG_USER);
812 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
813 /*
814 * Retrieve OS release information.
815 */
816 kvp_get_os_info();
817
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700818 if (kvp_file_init()) {
819 syslog(LOG_ERR, "Failed to initialize the pools");
820 exit(-1);
821 }
822
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700823 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
824 if (fd < 0) {
825 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
826 exit(-1);
827 }
828 addr.nl_family = AF_NETLINK;
829 addr.nl_pad = 0;
830 addr.nl_pid = 0;
831 addr.nl_groups = CN_KVP_IDX;
832
833
834 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
835 if (error < 0) {
836 syslog(LOG_ERR, "bind failed; error:%d", error);
837 close(fd);
838 exit(-1);
839 }
840 sock_opt = addr.nl_groups;
841 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
842 /*
843 * Register ourselves with the kernel.
844 */
845 message = (struct cn_msg *)kvp_send_buffer;
846 message->id.idx = CN_KVP_IDX;
847 message->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800848
849 hv_msg = (struct hv_kvp_msg *)message->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700850 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700851 message->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800852 message->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700853
854 len = netlink_send(fd, message);
855 if (len < 0) {
856 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
857 close(fd);
858 exit(-1);
859 }
860
861 pfd.fd = fd;
862
863 while (1) {
Olaf Heringbcc2c9c2012-05-31 16:40:06 +0200864 struct sockaddr *addr_p = (struct sockaddr *) &addr;
865 socklen_t addr_l = sizeof(addr);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700866 pfd.events = POLLIN;
867 pfd.revents = 0;
868 poll(&pfd, 1, -1);
869
Olaf Heringbcc2c9c2012-05-31 16:40:06 +0200870 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
871 addr_p, &addr_l);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700872
Olaf Heringbcc2c9c2012-05-31 16:40:06 +0200873 if (len < 0 || addr.nl_pid) {
874 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
875 addr.nl_pid, errno, strerror(errno));
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700876 close(fd);
877 return -1;
878 }
879
880 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
881 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800882 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700883
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700884 /*
885 * We will use the KVP header information to pass back
886 * the error from this daemon. So, first copy the state
887 * and set the error code to success.
888 */
889 op = hv_msg->kvp_hdr.operation;
890 pool = hv_msg->kvp_hdr.pool;
891 hv_msg->error = HV_S_OK;
892
893 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700894 /*
895 * Driver is registering with us; stash away the version
896 * information.
897 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700898 in_hand_shake = 0;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800899 p = (char *)hv_msg->body.kvp_register.version;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100900 lic_version = malloc(strlen(p) + 1);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700901 if (lic_version) {
Olaf Hering7989f7d2011-03-22 10:02:17 +0100902 strcpy(lic_version, p);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700903 syslog(LOG_INFO, "KVP LIC Version: %s",
904 lic_version);
905 } else {
906 syslog(LOG_ERR, "malloc failed");
907 }
908 continue;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700909 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700910
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700911 switch (op) {
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700912 case KVP_OP_SET:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700913 if (kvp_key_add_or_modify(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700914 hv_msg->body.kvp_set.data.key,
915 hv_msg->body.kvp_set.data.key_size,
916 hv_msg->body.kvp_set.data.value,
917 hv_msg->body.kvp_set.data.value_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700918 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700919 break;
920
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700921 case KVP_OP_GET:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700922 if (kvp_get_value(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700923 hv_msg->body.kvp_set.data.key,
924 hv_msg->body.kvp_set.data.key_size,
925 hv_msg->body.kvp_set.data.value,
926 hv_msg->body.kvp_set.data.value_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700927 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700928 break;
929
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700930 case KVP_OP_DELETE:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700931 if (kvp_key_delete(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700932 hv_msg->body.kvp_delete.key,
933 hv_msg->body.kvp_delete.key_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700934 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700935 break;
936
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700937 default:
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800938 break;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700939 }
940
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700941 if (op != KVP_OP_ENUMERATE)
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700942 goto kvp_done;
943
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700944 /*
945 * If the pool is KVP_POOL_AUTO, dynamically generate
946 * both the key and the value; if not read from the
947 * appropriate pool.
948 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700949 if (pool != KVP_POOL_AUTO) {
950 if (kvp_pool_enumerate(pool,
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700951 hv_msg->body.kvp_enum_data.index,
952 hv_msg->body.kvp_enum_data.data.key,
953 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
954 hv_msg->body.kvp_enum_data.data.value,
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700955 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
956 hv_msg->error = HV_S_CONT;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700957 goto kvp_done;
958 }
959
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800960 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
961 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
962 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700963
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800964 switch (hv_msg->body.kvp_enum_data.index) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700965 case FullyQualifiedDomainName:
966 kvp_get_domain_name(key_value,
967 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
968 strcpy(key_name, "FullyQualifiedDomainName");
969 break;
970 case IntegrationServicesVersion:
971 strcpy(key_name, "IntegrationServicesVersion");
972 strcpy(key_value, lic_version);
973 break;
974 case NetworkAddressIPv4:
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700975 kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
976 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700977 strcpy(key_name, "NetworkAddressIPv4");
978 break;
979 case NetworkAddressIPv6:
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700980 kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
981 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700982 strcpy(key_name, "NetworkAddressIPv6");
983 break;
984 case OSBuildNumber:
985 strcpy(key_value, os_build);
986 strcpy(key_name, "OSBuildNumber");
987 break;
988 case OSName:
989 strcpy(key_value, os_name);
990 strcpy(key_name, "OSName");
991 break;
992 case OSMajorVersion:
993 strcpy(key_value, os_major);
994 strcpy(key_name, "OSMajorVersion");
995 break;
996 case OSMinorVersion:
997 strcpy(key_value, os_minor);
998 strcpy(key_name, "OSMinorVersion");
999 break;
1000 case OSVersion:
1001 strcpy(key_value, os_build);
1002 strcpy(key_name, "OSVersion");
1003 break;
1004 case ProcessorArchitecture:
1005 strcpy(key_value, processor_arch);
1006 strcpy(key_name, "ProcessorArchitecture");
1007 break;
1008 default:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001009 hv_msg->error = HV_S_CONT;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001010 break;
1011 }
1012 /*
1013 * Send the value back to the kernel. The response is
1014 * already in the receive buffer. Update the cn_msg header to
1015 * reflect the key value that has been added to the message
1016 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -07001017kvp_done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001018
1019 incoming_cn_msg->id.idx = CN_KVP_IDX;
1020 incoming_cn_msg->id.val = CN_KVP_VAL;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001021 incoming_cn_msg->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001022 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001023
1024 len = netlink_send(fd, incoming_cn_msg);
1025 if (len < 0) {
1026 syslog(LOG_ERR, "net_link send failed; error:%d", len);
1027 exit(-1);
1028 }
1029 }
1030
1031}