blob: 60a8e29c4faef75714d4431b6334ccf26b760e7b [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];
72static char kvp_recv_buffer[4096];
73static struct sockaddr_nl addr;
74
Olaf Hering7989f7d2011-03-22 10:02:17 +010075static char *os_name = "";
76static char *os_major = "";
77static char *os_minor = "";
78static char *processor_arch;
79static char *os_build;
Ky Srinivasancc04acf2010-12-16 18:56:54 -070080static char *lic_version;
Olaf Hering7989f7d2011-03-22 10:02:17 +010081static struct utsname uts_buf;
Ky Srinivasancc04acf2010-12-16 18:56:54 -070082
K. Y. Srinivasandb425332012-03-16 08:02:26 -070083
84#define MAX_FILE_NAME 100
85#define ENTRIES_PER_BLOCK 50
86
87struct kvp_record {
88 __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
89 __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
90};
91
92struct kvp_file_state {
93 int fd;
94 int num_blocks;
95 struct kvp_record *records;
96 int num_records;
97 __u8 fname[MAX_FILE_NAME];
98};
99
100static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
101
102static void kvp_acquire_lock(int pool)
103{
104 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
105 fl.l_pid = getpid();
106
107 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
108 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700109 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700110 }
111}
112
113static void kvp_release_lock(int pool)
114{
115 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
116 fl.l_pid = getpid();
117
118 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
119 perror("fcntl");
120 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700121 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700122 }
123}
124
125static void kvp_update_file(int pool)
126{
127 FILE *filep;
128 size_t bytes_written;
129
130 /*
131 * We are going to write our in-memory registry out to
132 * disk; acquire the lock first.
133 */
134 kvp_acquire_lock(pool);
135
136 filep = fopen(kvp_file_info[pool].fname, "w");
137 if (!filep) {
138 kvp_release_lock(pool);
139 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700140 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700141 }
142
143 bytes_written = fwrite(kvp_file_info[pool].records,
144 sizeof(struct kvp_record),
145 kvp_file_info[pool].num_records, filep);
146
Ben Hutchings1eafb022012-09-05 14:37:37 -0700147 if (ferror(filep) || fclose(filep)) {
148 kvp_release_lock(pool);
149 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
150 exit(EXIT_FAILURE);
151 }
152
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700153 kvp_release_lock(pool);
154}
155
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700156static void kvp_update_mem_state(int pool)
157{
158 FILE *filep;
159 size_t records_read = 0;
160 struct kvp_record *record = kvp_file_info[pool].records;
161 struct kvp_record *readp;
162 int num_blocks = kvp_file_info[pool].num_blocks;
163 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
164
165 kvp_acquire_lock(pool);
166
167 filep = fopen(kvp_file_info[pool].fname, "r");
168 if (!filep) {
169 kvp_release_lock(pool);
170 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700171 exit(EXIT_FAILURE);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700172 }
Ben Hutchings1eafb022012-09-05 14:37:37 -0700173 for (;;) {
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700174 readp = &record[records_read];
175 records_read += fread(readp, sizeof(struct kvp_record),
176 ENTRIES_PER_BLOCK * num_blocks,
177 filep);
178
Ben Hutchings1eafb022012-09-05 14:37:37 -0700179 if (ferror(filep)) {
180 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
181 exit(EXIT_FAILURE);
182 }
183
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700184 if (!feof(filep)) {
185 /*
186 * We have more data to read.
187 */
188 num_blocks++;
189 record = realloc(record, alloc_unit * num_blocks);
190
191 if (record == NULL) {
192 syslog(LOG_ERR, "malloc failed");
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700193 exit(EXIT_FAILURE);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700194 }
195 continue;
196 }
197 break;
198 }
199
200 kvp_file_info[pool].num_blocks = num_blocks;
201 kvp_file_info[pool].records = record;
202 kvp_file_info[pool].num_records = records_read;
203
Ben Hutchings37b6d802012-09-05 14:37:35 -0700204 fclose(filep);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700205 kvp_release_lock(pool);
206}
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700207static int kvp_file_init(void)
208{
209 int ret, fd;
210 FILE *filep;
211 size_t records_read;
212 __u8 *fname;
213 struct kvp_record *record;
214 struct kvp_record *readp;
215 int num_blocks;
216 int i;
217 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
218
219 if (access("/var/opt/hyperv", F_OK)) {
220 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
221 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700222 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700223 }
224 }
225
226 for (i = 0; i < KVP_POOL_COUNT; i++) {
227 fname = kvp_file_info[i].fname;
228 records_read = 0;
229 num_blocks = 1;
230 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
231 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
232
233 if (fd == -1)
234 return 1;
235
236
237 filep = fopen(fname, "r");
238 if (!filep)
239 return 1;
240
241 record = malloc(alloc_unit * num_blocks);
242 if (record == NULL) {
243 fclose(filep);
244 return 1;
245 }
Ben Hutchings1eafb022012-09-05 14:37:37 -0700246 for (;;) {
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700247 readp = &record[records_read];
248 records_read += fread(readp, sizeof(struct kvp_record),
249 ENTRIES_PER_BLOCK,
250 filep);
251
Ben Hutchings1eafb022012-09-05 14:37:37 -0700252 if (ferror(filep)) {
253 syslog(LOG_ERR, "Failed to read file, pool: %d",
254 i);
255 exit(EXIT_FAILURE);
256 }
257
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700258 if (!feof(filep)) {
259 /*
260 * We have more data to read.
261 */
262 num_blocks++;
263 record = realloc(record, alloc_unit *
264 num_blocks);
265 if (record == NULL) {
266 fclose(filep);
267 return 1;
268 }
269 continue;
270 }
271 break;
272 }
273 kvp_file_info[i].fd = fd;
274 kvp_file_info[i].num_blocks = num_blocks;
275 kvp_file_info[i].records = record;
276 kvp_file_info[i].num_records = records_read;
277 fclose(filep);
278
279 }
280
281 return 0;
282}
283
284static int kvp_key_delete(int pool, __u8 *key, int key_size)
285{
286 int i;
287 int j, k;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700288 int num_records;
289 struct kvp_record *record;
290
291 /*
292 * First update the in-memory state.
293 */
294 kvp_update_mem_state(pool);
295
296 num_records = kvp_file_info[pool].num_records;
297 record = kvp_file_info[pool].records;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700298
299 for (i = 0; i < num_records; i++) {
300 if (memcmp(key, record[i].key, key_size))
301 continue;
302 /*
303 * Found a match; just move the remaining
304 * entries up.
305 */
306 if (i == num_records) {
307 kvp_file_info[pool].num_records--;
308 kvp_update_file(pool);
309 return 0;
310 }
311
312 j = i;
313 k = j + 1;
314 for (; k < num_records; k++) {
315 strcpy(record[j].key, record[k].key);
316 strcpy(record[j].value, record[k].value);
317 j++;
318 }
319
320 kvp_file_info[pool].num_records--;
321 kvp_update_file(pool);
322 return 0;
323 }
324 return 1;
325}
326
327static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
328 int value_size)
329{
330 int i;
331 int j, k;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700332 int num_records;
333 struct kvp_record *record;
334 int num_blocks;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700335
336 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
337 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
338 return 1;
339
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700340 /*
341 * First update the in-memory state.
342 */
343 kvp_update_mem_state(pool);
344
345 num_records = kvp_file_info[pool].num_records;
346 record = kvp_file_info[pool].records;
347 num_blocks = kvp_file_info[pool].num_blocks;
348
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700349 for (i = 0; i < num_records; i++) {
350 if (memcmp(key, record[i].key, key_size))
351 continue;
352 /*
353 * Found a match; just update the value -
354 * this is the modify case.
355 */
356 memcpy(record[i].value, value, value_size);
357 kvp_update_file(pool);
358 return 0;
359 }
360
361 /*
362 * Need to add a new entry;
363 */
364 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
365 /* Need to allocate a larger array for reg entries. */
366 record = realloc(record, sizeof(struct kvp_record) *
367 ENTRIES_PER_BLOCK * (num_blocks + 1));
368
369 if (record == NULL)
370 return 1;
371 kvp_file_info[pool].num_blocks++;
372
373 }
374 memcpy(record[i].value, value, value_size);
375 memcpy(record[i].key, key, key_size);
376 kvp_file_info[pool].records = record;
377 kvp_file_info[pool].num_records++;
378 kvp_update_file(pool);
379 return 0;
380}
381
382static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
383 int value_size)
384{
385 int i;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700386 int num_records;
387 struct kvp_record *record;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700388
389 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
390 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
391 return 1;
392
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700393 /*
394 * First update the in-memory state.
395 */
396 kvp_update_mem_state(pool);
397
398 num_records = kvp_file_info[pool].num_records;
399 record = kvp_file_info[pool].records;
400
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700401 for (i = 0; i < num_records; i++) {
402 if (memcmp(key, record[i].key, key_size))
403 continue;
404 /*
405 * Found a match; just copy the value out.
406 */
407 memcpy(value, record[i].value, value_size);
408 return 0;
409 }
410
411 return 1;
412}
413
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700414static void kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
415 __u8 *value, int value_size)
416{
417 struct kvp_record *record;
418
419 /*
420 * First update our in-memory database.
421 */
422 kvp_update_mem_state(pool);
423 record = kvp_file_info[pool].records;
424
425 if (index >= kvp_file_info[pool].num_records) {
426 /*
427 * This is an invalid index; terminate enumeration;
428 * - a NULL value will do the trick.
429 */
430 strcpy(value, "");
431 return;
432 }
433
434 memcpy(key, record[index].key, key_size);
435 memcpy(value, record[index].value, value_size);
436}
437
438
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700439void kvp_get_os_info(void)
440{
441 FILE *file;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100442 char *p, buf[512];
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700443
Olaf Hering7989f7d2011-03-22 10:02:17 +0100444 uname(&uts_buf);
445 os_build = uts_buf.release;
K. Y. Srinivasan064931d2011-07-19 11:44:20 -0700446 processor_arch = uts_buf.machine;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700447
K. Y. Srinivasane54bbc62011-07-22 10:14:31 -0700448 /*
449 * The current windows host (win7) expects the build
450 * string to be of the form: x.y.z
451 * Strip additional information we may have.
452 */
453 p = strchr(os_build, '-');
454 if (p)
455 *p = '\0';
456
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700457 file = fopen("/etc/SuSE-release", "r");
458 if (file != NULL)
459 goto kvp_osinfo_found;
460 file = fopen("/etc/redhat-release", "r");
461 if (file != NULL)
462 goto kvp_osinfo_found;
463 /*
464 * Add code for other supported platforms.
465 */
466
467 /*
468 * We don't have information about the os.
469 */
Olaf Hering7989f7d2011-03-22 10:02:17 +0100470 os_name = uts_buf.sysname;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700471 return;
472
473kvp_osinfo_found:
Olaf Hering7989f7d2011-03-22 10:02:17 +0100474 /* up to three lines */
475 p = fgets(buf, sizeof(buf), file);
476 if (p) {
477 p = strchr(buf, '\n');
478 if (p)
479 *p = '\0';
480 p = strdup(buf);
481 if (!p)
482 goto done;
483 os_name = p;
484
485 /* second line */
486 p = fgets(buf, sizeof(buf), file);
487 if (p) {
488 p = strchr(buf, '\n');
489 if (p)
490 *p = '\0';
491 p = strdup(buf);
492 if (!p)
493 goto done;
494 os_major = p;
495
496 /* third line */
497 p = fgets(buf, sizeof(buf), file);
498 if (p) {
499 p = strchr(buf, '\n');
500 if (p)
501 *p = '\0';
502 p = strdup(buf);
503 if (p)
504 os_minor = p;
505 }
506 }
507 }
508
509done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700510 fclose(file);
511 return;
512}
513
514static int
515kvp_get_ip_address(int family, char *buffer, int length)
516{
517 struct ifaddrs *ifap;
518 struct ifaddrs *curp;
519 int ipv4_len = strlen("255.255.255.255") + 1;
520 int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
521 int offset = 0;
522 const char *str;
523 char tmp[50];
524 int error = 0;
525
526 /*
527 * On entry into this function, the buffer is capable of holding the
528 * maximum key value (2048 bytes).
529 */
530
531 if (getifaddrs(&ifap)) {
532 strcpy(buffer, "getifaddrs failed\n");
533 return 1;
534 }
535
536 curp = ifap;
537 while (curp != NULL) {
538 if ((curp->ifa_addr != NULL) &&
539 (curp->ifa_addr->sa_family == family)) {
540 if (family == AF_INET) {
541 struct sockaddr_in *addr =
542 (struct sockaddr_in *) curp->ifa_addr;
543
544 str = inet_ntop(family, &addr->sin_addr,
545 tmp, 50);
546 if (str == NULL) {
547 strcpy(buffer, "inet_ntop failed\n");
548 error = 1;
549 goto getaddr_done;
550 }
551 if (offset == 0)
552 strcpy(buffer, tmp);
553 else
554 strcat(buffer, tmp);
555 strcat(buffer, ";");
556
557 offset += strlen(str) + 1;
558 if ((length - offset) < (ipv4_len + 1))
559 goto getaddr_done;
560
561 } else {
562
563 /*
564 * We only support AF_INET and AF_INET6
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300565 * and the list of addresses is separated by a ";".
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700566 */
567 struct sockaddr_in6 *addr =
568 (struct sockaddr_in6 *) curp->ifa_addr;
569
570 str = inet_ntop(family,
571 &addr->sin6_addr.s6_addr,
572 tmp, 50);
573 if (str == NULL) {
574 strcpy(buffer, "inet_ntop failed\n");
575 error = 1;
576 goto getaddr_done;
577 }
578 if (offset == 0)
579 strcpy(buffer, tmp);
580 else
581 strcat(buffer, tmp);
582 strcat(buffer, ";");
583 offset += strlen(str) + 1;
584 if ((length - offset) < (ipv6_len + 1))
585 goto getaddr_done;
586
587 }
588
589 }
590 curp = curp->ifa_next;
591 }
592
593getaddr_done:
594 freeifaddrs(ifap);
595 return error;
596}
597
598
599static int
600kvp_get_domain_name(char *buffer, int length)
601{
602 struct addrinfo hints, *info ;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700603 int error = 0;
604
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700605 gethostname(buffer, length);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700606 memset(&hints, 0, sizeof(hints));
607 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
608 hints.ai_socktype = SOCK_STREAM;
609 hints.ai_flags = AI_CANONNAME;
610
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700611 error = getaddrinfo(buffer, NULL, &hints, &info);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700612 if (error != 0) {
613 strcpy(buffer, "getaddrinfo failed\n");
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700614 return error;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700615 }
616 strcpy(buffer, info->ai_canonname);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700617 freeaddrinfo(info);
618 return error;
619}
620
621static int
622netlink_send(int fd, struct cn_msg *msg)
623{
624 struct nlmsghdr *nlh;
625 unsigned int size;
626 struct msghdr message;
627 char buffer[64];
628 struct iovec iov[2];
629
630 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
631
632 nlh = (struct nlmsghdr *)buffer;
633 nlh->nlmsg_seq = 0;
634 nlh->nlmsg_pid = getpid();
635 nlh->nlmsg_type = NLMSG_DONE;
636 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
637 nlh->nlmsg_flags = 0;
638
639 iov[0].iov_base = nlh;
640 iov[0].iov_len = sizeof(*nlh);
641
642 iov[1].iov_base = msg;
643 iov[1].iov_len = size;
644
645 memset(&message, 0, sizeof(message));
646 message.msg_name = &addr;
647 message.msg_namelen = sizeof(addr);
648 message.msg_iov = iov;
649 message.msg_iovlen = 2;
650
651 return sendmsg(fd, &message, 0);
652}
653
Olaf Hering7989f7d2011-03-22 10:02:17 +0100654int main(void)
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700655{
656 int fd, len, sock_opt;
657 int error;
658 struct cn_msg *message;
659 struct pollfd pfd;
660 struct nlmsghdr *incoming_msg;
661 struct cn_msg *incoming_cn_msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800662 struct hv_kvp_msg *hv_msg;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100663 char *p;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700664 char *key_value;
665 char *key_name;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700666
667 daemon(1, 0);
668 openlog("KVP", 0, LOG_USER);
669 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
670 /*
671 * Retrieve OS release information.
672 */
673 kvp_get_os_info();
674
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700675 if (kvp_file_init()) {
676 syslog(LOG_ERR, "Failed to initialize the pools");
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700677 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700678 }
679
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700680 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
681 if (fd < 0) {
682 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700683 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700684 }
685 addr.nl_family = AF_NETLINK;
686 addr.nl_pad = 0;
687 addr.nl_pid = 0;
688 addr.nl_groups = CN_KVP_IDX;
689
690
691 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
692 if (error < 0) {
693 syslog(LOG_ERR, "bind failed; error:%d", error);
694 close(fd);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700695 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700696 }
697 sock_opt = addr.nl_groups;
698 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
699 /*
700 * Register ourselves with the kernel.
701 */
702 message = (struct cn_msg *)kvp_send_buffer;
703 message->id.idx = CN_KVP_IDX;
704 message->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800705
706 hv_msg = (struct hv_kvp_msg *)message->data;
707 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700708 message->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800709 message->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700710
711 len = netlink_send(fd, message);
712 if (len < 0) {
713 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
714 close(fd);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700715 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700716 }
717
718 pfd.fd = fd;
719
720 while (1) {
Olaf Heringc84299b2012-05-31 16:40:06 +0200721 struct sockaddr *addr_p = (struct sockaddr *) &addr;
722 socklen_t addr_l = sizeof(addr);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700723 pfd.events = POLLIN;
724 pfd.revents = 0;
725 poll(&pfd, 1, -1);
726
Olaf Heringc84299b2012-05-31 16:40:06 +0200727 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
728 addr_p, &addr_l);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700729
Tomas Hozza5fb91492012-11-08 10:53:29 +0100730 if (len < 0) {
Olaf Heringc84299b2012-05-31 16:40:06 +0200731 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
732 addr.nl_pid, errno, strerror(errno));
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700733 close(fd);
734 return -1;
735 }
736
Tomas Hozza5fb91492012-11-08 10:53:29 +0100737 if (addr.nl_pid) {
738 syslog(LOG_WARNING, "Received packet from untrusted pid:%u",
739 addr.nl_pid);
740 continue;
741 }
742
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700743 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
744 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800745 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700746
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800747 switch (hv_msg->kvp_hdr.operation) {
748 case KVP_OP_REGISTER:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700749 /*
750 * Driver is registering with us; stash away the version
751 * information.
752 */
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800753 p = (char *)hv_msg->body.kvp_register.version;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100754 lic_version = malloc(strlen(p) + 1);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700755 if (lic_version) {
Olaf Hering7989f7d2011-03-22 10:02:17 +0100756 strcpy(lic_version, p);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700757 syslog(LOG_INFO, "KVP LIC Version: %s",
758 lic_version);
759 } else {
760 syslog(LOG_ERR, "malloc failed");
761 }
762 continue;
763
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700764 /*
765 * The current protocol with the kernel component uses a
766 * NULL key name to pass an error condition.
767 * For the SET, GET and DELETE operations,
768 * use the existing protocol to pass back error.
769 */
770
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700771 case KVP_OP_SET:
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700772 if (kvp_key_add_or_modify(hv_msg->kvp_hdr.pool,
773 hv_msg->body.kvp_set.data.key,
774 hv_msg->body.kvp_set.data.key_size,
775 hv_msg->body.kvp_set.data.value,
776 hv_msg->body.kvp_set.data.value_size))
777 strcpy(hv_msg->body.kvp_set.data.key, "");
778 break;
779
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700780 case KVP_OP_GET:
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700781 if (kvp_get_value(hv_msg->kvp_hdr.pool,
782 hv_msg->body.kvp_set.data.key,
783 hv_msg->body.kvp_set.data.key_size,
784 hv_msg->body.kvp_set.data.value,
785 hv_msg->body.kvp_set.data.value_size))
786 strcpy(hv_msg->body.kvp_set.data.key, "");
787 break;
788
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700789 case KVP_OP_DELETE:
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700790 if (kvp_key_delete(hv_msg->kvp_hdr.pool,
791 hv_msg->body.kvp_delete.key,
792 hv_msg->body.kvp_delete.key_size))
793 strcpy(hv_msg->body.kvp_delete.key, "");
794 break;
795
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700796 default:
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800797 break;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700798 }
799
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700800 if (hv_msg->kvp_hdr.operation != KVP_OP_ENUMERATE)
801 goto kvp_done;
802
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700803 /*
804 * If the pool is KVP_POOL_AUTO, dynamically generate
805 * both the key and the value; if not read from the
806 * appropriate pool.
807 */
808 if (hv_msg->kvp_hdr.pool != KVP_POOL_AUTO) {
809 kvp_pool_enumerate(hv_msg->kvp_hdr.pool,
810 hv_msg->body.kvp_enum_data.index,
811 hv_msg->body.kvp_enum_data.data.key,
812 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
813 hv_msg->body.kvp_enum_data.data.value,
814 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
815 goto kvp_done;
816 }
817
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800818 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
819 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
820 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700821
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800822 switch (hv_msg->body.kvp_enum_data.index) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700823 case FullyQualifiedDomainName:
824 kvp_get_domain_name(key_value,
825 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
826 strcpy(key_name, "FullyQualifiedDomainName");
827 break;
828 case IntegrationServicesVersion:
829 strcpy(key_name, "IntegrationServicesVersion");
830 strcpy(key_value, lic_version);
831 break;
832 case NetworkAddressIPv4:
833 kvp_get_ip_address(AF_INET, key_value,
834 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
835 strcpy(key_name, "NetworkAddressIPv4");
836 break;
837 case NetworkAddressIPv6:
838 kvp_get_ip_address(AF_INET6, key_value,
839 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
840 strcpy(key_name, "NetworkAddressIPv6");
841 break;
842 case OSBuildNumber:
843 strcpy(key_value, os_build);
844 strcpy(key_name, "OSBuildNumber");
845 break;
846 case OSName:
847 strcpy(key_value, os_name);
848 strcpy(key_name, "OSName");
849 break;
850 case OSMajorVersion:
851 strcpy(key_value, os_major);
852 strcpy(key_name, "OSMajorVersion");
853 break;
854 case OSMinorVersion:
855 strcpy(key_value, os_minor);
856 strcpy(key_name, "OSMinorVersion");
857 break;
858 case OSVersion:
859 strcpy(key_value, os_build);
860 strcpy(key_name, "OSVersion");
861 break;
862 case ProcessorArchitecture:
863 strcpy(key_value, processor_arch);
864 strcpy(key_name, "ProcessorArchitecture");
865 break;
866 default:
867 strcpy(key_value, "Unknown Key");
868 /*
869 * We use a null key name to terminate enumeration.
870 */
871 strcpy(key_name, "");
872 break;
873 }
874 /*
875 * Send the value back to the kernel. The response is
876 * already in the receive buffer. Update the cn_msg header to
877 * reflect the key value that has been added to the message
878 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700879kvp_done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700880
881 incoming_cn_msg->id.idx = CN_KVP_IDX;
882 incoming_cn_msg->id.val = CN_KVP_VAL;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700883 incoming_cn_msg->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800884 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700885
886 len = netlink_send(fd, incoming_cn_msg);
887 if (len < 0) {
888 syslog(LOG_ERR, "net_link send failed; error:%d", len);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700889 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700890 }
891 }
892
893}