blob: edab6e90038a76571d8eecbefab984a6a95f8a8c [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 Hutchings37b6d802012-09-05 14:37:35 -0700147 fclose(filep);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700148 kvp_release_lock(pool);
149}
150
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700151static void kvp_update_mem_state(int pool)
152{
153 FILE *filep;
154 size_t records_read = 0;
155 struct kvp_record *record = kvp_file_info[pool].records;
156 struct kvp_record *readp;
157 int num_blocks = kvp_file_info[pool].num_blocks;
158 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
159
160 kvp_acquire_lock(pool);
161
162 filep = fopen(kvp_file_info[pool].fname, "r");
163 if (!filep) {
164 kvp_release_lock(pool);
165 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700166 exit(EXIT_FAILURE);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700167 }
168 while (!feof(filep)) {
169 readp = &record[records_read];
170 records_read += fread(readp, sizeof(struct kvp_record),
171 ENTRIES_PER_BLOCK * num_blocks,
172 filep);
173
174 if (!feof(filep)) {
175 /*
176 * We have more data to read.
177 */
178 num_blocks++;
179 record = realloc(record, alloc_unit * num_blocks);
180
181 if (record == NULL) {
182 syslog(LOG_ERR, "malloc failed");
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700183 exit(EXIT_FAILURE);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700184 }
185 continue;
186 }
187 break;
188 }
189
190 kvp_file_info[pool].num_blocks = num_blocks;
191 kvp_file_info[pool].records = record;
192 kvp_file_info[pool].num_records = records_read;
193
Ben Hutchings37b6d802012-09-05 14:37:35 -0700194 fclose(filep);
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700195 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");
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700212 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700213 }
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. Srinivasanadc80ae2012-03-16 08:02:27 -0700398static void kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
399 __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) {
410 /*
411 * This is an invalid index; terminate enumeration;
412 * - a NULL value will do the trick.
413 */
414 strcpy(value, "");
415 return;
416 }
417
418 memcpy(key, record[index].key, key_size);
419 memcpy(value, record[index].value, value_size);
420}
421
422
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700423void kvp_get_os_info(void)
424{
425 FILE *file;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100426 char *p, buf[512];
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700427
Olaf Hering7989f7d2011-03-22 10:02:17 +0100428 uname(&uts_buf);
429 os_build = uts_buf.release;
K. Y. Srinivasan064931d2011-07-19 11:44:20 -0700430 processor_arch = uts_buf.machine;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700431
K. Y. Srinivasane54bbc62011-07-22 10:14:31 -0700432 /*
433 * The current windows host (win7) expects the build
434 * string to be of the form: x.y.z
435 * Strip additional information we may have.
436 */
437 p = strchr(os_build, '-');
438 if (p)
439 *p = '\0';
440
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700441 file = fopen("/etc/SuSE-release", "r");
442 if (file != NULL)
443 goto kvp_osinfo_found;
444 file = fopen("/etc/redhat-release", "r");
445 if (file != NULL)
446 goto kvp_osinfo_found;
447 /*
448 * Add code for other supported platforms.
449 */
450
451 /*
452 * We don't have information about the os.
453 */
Olaf Hering7989f7d2011-03-22 10:02:17 +0100454 os_name = uts_buf.sysname;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700455 return;
456
457kvp_osinfo_found:
Olaf Hering7989f7d2011-03-22 10:02:17 +0100458 /* up to three lines */
459 p = fgets(buf, sizeof(buf), file);
460 if (p) {
461 p = strchr(buf, '\n');
462 if (p)
463 *p = '\0';
464 p = strdup(buf);
465 if (!p)
466 goto done;
467 os_name = p;
468
469 /* second line */
470 p = fgets(buf, sizeof(buf), file);
471 if (p) {
472 p = strchr(buf, '\n');
473 if (p)
474 *p = '\0';
475 p = strdup(buf);
476 if (!p)
477 goto done;
478 os_major = p;
479
480 /* third line */
481 p = fgets(buf, sizeof(buf), file);
482 if (p) {
483 p = strchr(buf, '\n');
484 if (p)
485 *p = '\0';
486 p = strdup(buf);
487 if (p)
488 os_minor = p;
489 }
490 }
491 }
492
493done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700494 fclose(file);
495 return;
496}
497
498static int
499kvp_get_ip_address(int family, char *buffer, int length)
500{
501 struct ifaddrs *ifap;
502 struct ifaddrs *curp;
503 int ipv4_len = strlen("255.255.255.255") + 1;
504 int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
505 int offset = 0;
506 const char *str;
507 char tmp[50];
508 int error = 0;
509
510 /*
511 * On entry into this function, the buffer is capable of holding the
512 * maximum key value (2048 bytes).
513 */
514
515 if (getifaddrs(&ifap)) {
516 strcpy(buffer, "getifaddrs failed\n");
517 return 1;
518 }
519
520 curp = ifap;
521 while (curp != NULL) {
522 if ((curp->ifa_addr != NULL) &&
523 (curp->ifa_addr->sa_family == family)) {
524 if (family == AF_INET) {
525 struct sockaddr_in *addr =
526 (struct sockaddr_in *) curp->ifa_addr;
527
528 str = inet_ntop(family, &addr->sin_addr,
529 tmp, 50);
530 if (str == NULL) {
531 strcpy(buffer, "inet_ntop failed\n");
532 error = 1;
533 goto getaddr_done;
534 }
535 if (offset == 0)
536 strcpy(buffer, tmp);
537 else
538 strcat(buffer, tmp);
539 strcat(buffer, ";");
540
541 offset += strlen(str) + 1;
542 if ((length - offset) < (ipv4_len + 1))
543 goto getaddr_done;
544
545 } else {
546
547 /*
548 * We only support AF_INET and AF_INET6
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300549 * and the list of addresses is separated by a ";".
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700550 */
551 struct sockaddr_in6 *addr =
552 (struct sockaddr_in6 *) curp->ifa_addr;
553
554 str = inet_ntop(family,
555 &addr->sin6_addr.s6_addr,
556 tmp, 50);
557 if (str == NULL) {
558 strcpy(buffer, "inet_ntop failed\n");
559 error = 1;
560 goto getaddr_done;
561 }
562 if (offset == 0)
563 strcpy(buffer, tmp);
564 else
565 strcat(buffer, tmp);
566 strcat(buffer, ";");
567 offset += strlen(str) + 1;
568 if ((length - offset) < (ipv6_len + 1))
569 goto getaddr_done;
570
571 }
572
573 }
574 curp = curp->ifa_next;
575 }
576
577getaddr_done:
578 freeifaddrs(ifap);
579 return error;
580}
581
582
583static int
584kvp_get_domain_name(char *buffer, int length)
585{
586 struct addrinfo hints, *info ;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700587 int error = 0;
588
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700589 gethostname(buffer, length);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700590 memset(&hints, 0, sizeof(hints));
591 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
592 hints.ai_socktype = SOCK_STREAM;
593 hints.ai_flags = AI_CANONNAME;
594
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700595 error = getaddrinfo(buffer, NULL, &hints, &info);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700596 if (error != 0) {
597 strcpy(buffer, "getaddrinfo failed\n");
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700598 return error;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700599 }
600 strcpy(buffer, info->ai_canonname);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700601 freeaddrinfo(info);
602 return error;
603}
604
605static int
606netlink_send(int fd, struct cn_msg *msg)
607{
608 struct nlmsghdr *nlh;
609 unsigned int size;
610 struct msghdr message;
611 char buffer[64];
612 struct iovec iov[2];
613
614 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
615
616 nlh = (struct nlmsghdr *)buffer;
617 nlh->nlmsg_seq = 0;
618 nlh->nlmsg_pid = getpid();
619 nlh->nlmsg_type = NLMSG_DONE;
620 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
621 nlh->nlmsg_flags = 0;
622
623 iov[0].iov_base = nlh;
624 iov[0].iov_len = sizeof(*nlh);
625
626 iov[1].iov_base = msg;
627 iov[1].iov_len = size;
628
629 memset(&message, 0, sizeof(message));
630 message.msg_name = &addr;
631 message.msg_namelen = sizeof(addr);
632 message.msg_iov = iov;
633 message.msg_iovlen = 2;
634
635 return sendmsg(fd, &message, 0);
636}
637
Olaf Hering7989f7d2011-03-22 10:02:17 +0100638int main(void)
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700639{
640 int fd, len, sock_opt;
641 int error;
642 struct cn_msg *message;
643 struct pollfd pfd;
644 struct nlmsghdr *incoming_msg;
645 struct cn_msg *incoming_cn_msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800646 struct hv_kvp_msg *hv_msg;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100647 char *p;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700648 char *key_value;
649 char *key_name;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700650
651 daemon(1, 0);
652 openlog("KVP", 0, LOG_USER);
653 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
654 /*
655 * Retrieve OS release information.
656 */
657 kvp_get_os_info();
658
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700659 if (kvp_file_init()) {
660 syslog(LOG_ERR, "Failed to initialize the pools");
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700661 exit(EXIT_FAILURE);
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700662 }
663
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700664 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
665 if (fd < 0) {
666 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700667 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700668 }
669 addr.nl_family = AF_NETLINK;
670 addr.nl_pad = 0;
671 addr.nl_pid = 0;
672 addr.nl_groups = CN_KVP_IDX;
673
674
675 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
676 if (error < 0) {
677 syslog(LOG_ERR, "bind failed; error:%d", error);
678 close(fd);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700679 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700680 }
681 sock_opt = addr.nl_groups;
682 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
683 /*
684 * Register ourselves with the kernel.
685 */
686 message = (struct cn_msg *)kvp_send_buffer;
687 message->id.idx = CN_KVP_IDX;
688 message->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800689
690 hv_msg = (struct hv_kvp_msg *)message->data;
691 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700692 message->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800693 message->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700694
695 len = netlink_send(fd, message);
696 if (len < 0) {
697 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
698 close(fd);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700699 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700700 }
701
702 pfd.fd = fd;
703
704 while (1) {
Olaf Heringc84299b2012-05-31 16:40:06 +0200705 struct sockaddr *addr_p = (struct sockaddr *) &addr;
706 socklen_t addr_l = sizeof(addr);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700707 pfd.events = POLLIN;
708 pfd.revents = 0;
709 poll(&pfd, 1, -1);
710
Olaf Heringc84299b2012-05-31 16:40:06 +0200711 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
712 addr_p, &addr_l);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700713
Olaf Heringc84299b2012-05-31 16:40:06 +0200714 if (len < 0 || addr.nl_pid) {
715 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
716 addr.nl_pid, errno, strerror(errno));
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700717 close(fd);
718 return -1;
719 }
720
721 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
722 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800723 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700724
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800725 switch (hv_msg->kvp_hdr.operation) {
726 case KVP_OP_REGISTER:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700727 /*
728 * Driver is registering with us; stash away the version
729 * information.
730 */
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800731 p = (char *)hv_msg->body.kvp_register.version;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100732 lic_version = malloc(strlen(p) + 1);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700733 if (lic_version) {
Olaf Hering7989f7d2011-03-22 10:02:17 +0100734 strcpy(lic_version, p);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700735 syslog(LOG_INFO, "KVP LIC Version: %s",
736 lic_version);
737 } else {
738 syslog(LOG_ERR, "malloc failed");
739 }
740 continue;
741
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700742 /*
743 * The current protocol with the kernel component uses a
744 * NULL key name to pass an error condition.
745 * For the SET, GET and DELETE operations,
746 * use the existing protocol to pass back error.
747 */
748
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700749 case KVP_OP_SET:
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700750 if (kvp_key_add_or_modify(hv_msg->kvp_hdr.pool,
751 hv_msg->body.kvp_set.data.key,
752 hv_msg->body.kvp_set.data.key_size,
753 hv_msg->body.kvp_set.data.value,
754 hv_msg->body.kvp_set.data.value_size))
755 strcpy(hv_msg->body.kvp_set.data.key, "");
756 break;
757
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700758 case KVP_OP_GET:
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700759 if (kvp_get_value(hv_msg->kvp_hdr.pool,
760 hv_msg->body.kvp_set.data.key,
761 hv_msg->body.kvp_set.data.key_size,
762 hv_msg->body.kvp_set.data.value,
763 hv_msg->body.kvp_set.data.value_size))
764 strcpy(hv_msg->body.kvp_set.data.key, "");
765 break;
766
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700767 case KVP_OP_DELETE:
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700768 if (kvp_key_delete(hv_msg->kvp_hdr.pool,
769 hv_msg->body.kvp_delete.key,
770 hv_msg->body.kvp_delete.key_size))
771 strcpy(hv_msg->body.kvp_delete.key, "");
772 break;
773
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700774 default:
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800775 break;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700776 }
777
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700778 if (hv_msg->kvp_hdr.operation != KVP_OP_ENUMERATE)
779 goto kvp_done;
780
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700781 /*
782 * If the pool is KVP_POOL_AUTO, dynamically generate
783 * both the key and the value; if not read from the
784 * appropriate pool.
785 */
786 if (hv_msg->kvp_hdr.pool != KVP_POOL_AUTO) {
787 kvp_pool_enumerate(hv_msg->kvp_hdr.pool,
788 hv_msg->body.kvp_enum_data.index,
789 hv_msg->body.kvp_enum_data.data.key,
790 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
791 hv_msg->body.kvp_enum_data.data.value,
792 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
793 goto kvp_done;
794 }
795
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800796 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
797 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
798 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700799
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800800 switch (hv_msg->body.kvp_enum_data.index) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700801 case FullyQualifiedDomainName:
802 kvp_get_domain_name(key_value,
803 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
804 strcpy(key_name, "FullyQualifiedDomainName");
805 break;
806 case IntegrationServicesVersion:
807 strcpy(key_name, "IntegrationServicesVersion");
808 strcpy(key_value, lic_version);
809 break;
810 case NetworkAddressIPv4:
811 kvp_get_ip_address(AF_INET, key_value,
812 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
813 strcpy(key_name, "NetworkAddressIPv4");
814 break;
815 case NetworkAddressIPv6:
816 kvp_get_ip_address(AF_INET6, key_value,
817 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
818 strcpy(key_name, "NetworkAddressIPv6");
819 break;
820 case OSBuildNumber:
821 strcpy(key_value, os_build);
822 strcpy(key_name, "OSBuildNumber");
823 break;
824 case OSName:
825 strcpy(key_value, os_name);
826 strcpy(key_name, "OSName");
827 break;
828 case OSMajorVersion:
829 strcpy(key_value, os_major);
830 strcpy(key_name, "OSMajorVersion");
831 break;
832 case OSMinorVersion:
833 strcpy(key_value, os_minor);
834 strcpy(key_name, "OSMinorVersion");
835 break;
836 case OSVersion:
837 strcpy(key_value, os_build);
838 strcpy(key_name, "OSVersion");
839 break;
840 case ProcessorArchitecture:
841 strcpy(key_value, processor_arch);
842 strcpy(key_name, "ProcessorArchitecture");
843 break;
844 default:
845 strcpy(key_value, "Unknown Key");
846 /*
847 * We use a null key name to terminate enumeration.
848 */
849 strcpy(key_name, "");
850 break;
851 }
852 /*
853 * Send the value back to the kernel. The response is
854 * already in the receive buffer. Update the cn_msg header to
855 * reflect the key value that has been added to the message
856 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700857kvp_done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700858
859 incoming_cn_msg->id.idx = CN_KVP_IDX;
860 incoming_cn_msg->id.val = CN_KVP_VAL;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700861 incoming_cn_msg->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800862 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700863
864 len = netlink_send(fd, incoming_cn_msg);
865 if (len < 0) {
866 syslog(LOG_ERR, "net_link send failed; error:%d", len);
Ben Hutchingsde5d66e2012-09-05 14:37:36 -0700867 exit(EXIT_FAILURE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700868 }
869 }
870
871}