blob: d12d71448f0c0f8ad9fbbb663743edfa33731dc1 [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{
K. Y. Srinivasan00b83352012-09-04 14:46:33 -0700199 int fd;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700200 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;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700315 int num_records;
316 struct kvp_record *record;
317 int num_blocks;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700318
319 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
320 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
321 return 1;
322
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700323 /*
324 * First update the in-memory state.
325 */
326 kvp_update_mem_state(pool);
327
328 num_records = kvp_file_info[pool].num_records;
329 record = kvp_file_info[pool].records;
330 num_blocks = kvp_file_info[pool].num_blocks;
331
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700332 for (i = 0; i < num_records; i++) {
333 if (memcmp(key, record[i].key, key_size))
334 continue;
335 /*
336 * Found a match; just update the value -
337 * this is the modify case.
338 */
339 memcpy(record[i].value, value, value_size);
340 kvp_update_file(pool);
341 return 0;
342 }
343
344 /*
345 * Need to add a new entry;
346 */
347 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
348 /* Need to allocate a larger array for reg entries. */
349 record = realloc(record, sizeof(struct kvp_record) *
350 ENTRIES_PER_BLOCK * (num_blocks + 1));
351
352 if (record == NULL)
353 return 1;
354 kvp_file_info[pool].num_blocks++;
355
356 }
357 memcpy(record[i].value, value, value_size);
358 memcpy(record[i].key, key, key_size);
359 kvp_file_info[pool].records = record;
360 kvp_file_info[pool].num_records++;
361 kvp_update_file(pool);
362 return 0;
363}
364
365static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
366 int value_size)
367{
368 int i;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700369 int num_records;
370 struct kvp_record *record;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700371
372 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
373 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
374 return 1;
375
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700376 /*
377 * First update the in-memory state.
378 */
379 kvp_update_mem_state(pool);
380
381 num_records = kvp_file_info[pool].num_records;
382 record = kvp_file_info[pool].records;
383
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700384 for (i = 0; i < num_records; i++) {
385 if (memcmp(key, record[i].key, key_size))
386 continue;
387 /*
388 * Found a match; just copy the value out.
389 */
390 memcpy(value, record[i].value, value_size);
391 return 0;
392 }
393
394 return 1;
395}
396
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700397static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700398 __u8 *value, int value_size)
399{
400 struct kvp_record *record;
401
402 /*
403 * First update our in-memory database.
404 */
405 kvp_update_mem_state(pool);
406 record = kvp_file_info[pool].records;
407
408 if (index >= kvp_file_info[pool].num_records) {
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700409 return 1;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700410 }
411
412 memcpy(key, record[index].key, key_size);
413 memcpy(value, record[index].value, value_size);
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700414 return 0;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700415}
416
417
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700418void kvp_get_os_info(void)
419{
420 FILE *file;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100421 char *p, buf[512];
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700422
Olaf Hering7989f7d2011-03-22 10:02:17 +0100423 uname(&uts_buf);
424 os_build = uts_buf.release;
K. Y. Srinivasan064931d2011-07-19 11:44:20 -0700425 processor_arch = uts_buf.machine;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700426
K. Y. Srinivasane54bbc62011-07-22 10:14:31 -0700427 /*
428 * The current windows host (win7) expects the build
429 * string to be of the form: x.y.z
430 * Strip additional information we may have.
431 */
432 p = strchr(os_build, '-');
433 if (p)
434 *p = '\0';
435
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700436 file = fopen("/etc/SuSE-release", "r");
437 if (file != NULL)
438 goto kvp_osinfo_found;
439 file = fopen("/etc/redhat-release", "r");
440 if (file != NULL)
441 goto kvp_osinfo_found;
442 /*
443 * Add code for other supported platforms.
444 */
445
446 /*
447 * We don't have information about the os.
448 */
Olaf Hering7989f7d2011-03-22 10:02:17 +0100449 os_name = uts_buf.sysname;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700450 return;
451
452kvp_osinfo_found:
Olaf Hering7989f7d2011-03-22 10:02:17 +0100453 /* up to three lines */
454 p = fgets(buf, sizeof(buf), file);
455 if (p) {
456 p = strchr(buf, '\n');
457 if (p)
458 *p = '\0';
459 p = strdup(buf);
460 if (!p)
461 goto done;
462 os_name = p;
463
464 /* second line */
465 p = fgets(buf, sizeof(buf), file);
466 if (p) {
467 p = strchr(buf, '\n');
468 if (p)
469 *p = '\0';
470 p = strdup(buf);
471 if (!p)
472 goto done;
473 os_major = p;
474
475 /* third line */
476 p = fgets(buf, sizeof(buf), file);
477 if (p) {
478 p = strchr(buf, '\n');
479 if (p)
480 *p = '\0';
481 p = strdup(buf);
482 if (p)
483 os_minor = p;
484 }
485 }
486 }
487
488done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700489 fclose(file);
490 return;
491}
492
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700493static void kvp_process_ipconfig_file(char *cmd,
494 char *config_buf, int len,
495 int element_size, int offset)
496{
497 char buf[256];
498 char *p;
499 char *x;
500 FILE *file;
501
502 /*
503 * First execute the command.
504 */
505 file = popen(cmd, "r");
506 if (file == NULL)
507 return;
508
509 if (offset == 0)
510 memset(config_buf, 0, len);
511 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
512 if ((len - strlen(config_buf)) < (element_size + 1))
513 break;
514
515 x = strchr(p, '\n');
516 *x = '\0';
517 strcat(config_buf, p);
518 strcat(config_buf, ";");
519 }
520 pclose(file);
521}
522
523static void kvp_get_ipconfig_info(char *if_name,
524 struct hv_kvp_ipaddr_value *buffer)
525{
526 char cmd[512];
527
528 /*
529 * Get the address of default gateway (ipv4).
530 */
531 sprintf(cmd, "%s %s", "ip route show dev", if_name);
532 strcat(cmd, " | awk '/default/ {print $3 }'");
533
534 /*
535 * Execute the command to gather gateway info.
536 */
537 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
538 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
539
540 /*
541 * Get the address of default gateway (ipv6).
542 */
543 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
544 strcat(cmd, " | awk '/default/ {print $3 }'");
545
546 /*
547 * Execute the command to gather gateway info (ipv6).
548 */
549 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
550 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
551
552}
553
554
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700555static unsigned int hweight32(unsigned int *w)
556{
557 unsigned int res = *w - ((*w >> 1) & 0x55555555);
558 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
559 res = (res + (res >> 4)) & 0x0F0F0F0F;
560 res = res + (res >> 8);
561 return (res + (res >> 16)) & 0x000000FF;
562}
563
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700564static int kvp_process_ip_address(void *addrp,
565 int family, char *buffer,
566 int length, int *offset)
567{
568 struct sockaddr_in *addr;
569 struct sockaddr_in6 *addr6;
570 int addr_length;
571 char tmp[50];
572 const char *str;
573
574 if (family == AF_INET) {
575 addr = (struct sockaddr_in *)addrp;
576 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
577 addr_length = INET_ADDRSTRLEN;
578 } else {
579 addr6 = (struct sockaddr_in6 *)addrp;
580 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
581 addr_length = INET6_ADDRSTRLEN;
582 }
583
584 if ((length - *offset) < addr_length + 1)
585 return 1;
586 if (str == NULL) {
587 strcpy(buffer, "inet_ntop failed\n");
588 return 1;
589 }
590 if (*offset == 0)
591 strcpy(buffer, tmp);
592 else
593 strcat(buffer, tmp);
594 strcat(buffer, ";");
595
596 *offset += strlen(str) + 1;
597 return 0;
598}
599
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700600static int
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700601kvp_get_ip_address(int family, char *if_name, int op,
602 void *out_buffer, int length)
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700603{
604 struct ifaddrs *ifap;
605 struct ifaddrs *curp;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700606 int offset = 0;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700607 int sn_offset = 0;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700608 int error = 0;
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700609 char *buffer;
610 struct hv_kvp_ipaddr_value *ip_buffer;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700611 char cidr_mask[5]; /* /xyz */
612 int weight;
613 int i;
614 unsigned int *w;
615 char *sn_str;
616 struct sockaddr_in6 *addr6;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700617
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700618 if (op == KVP_OP_ENUMERATE) {
619 buffer = out_buffer;
620 } else {
621 ip_buffer = out_buffer;
622 buffer = (char *)ip_buffer->ip_addr;
623 ip_buffer->addr_family = 0;
624 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700625 /*
626 * On entry into this function, the buffer is capable of holding the
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700627 * maximum key value.
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700628 */
629
630 if (getifaddrs(&ifap)) {
631 strcpy(buffer, "getifaddrs failed\n");
632 return 1;
633 }
634
635 curp = ifap;
636 while (curp != NULL) {
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700637 if (curp->ifa_addr == NULL) {
638 curp = curp->ifa_next;
639 continue;
640 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700641
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700642 if ((if_name != NULL) &&
643 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
644 /*
645 * We want info about a specific interface;
646 * just continue.
647 */
648 curp = curp->ifa_next;
649 continue;
650 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700651
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700652 /*
653 * We only support two address families: AF_INET and AF_INET6.
654 * If a family value of 0 is specified, we collect both
655 * supported address families; if not we gather info on
656 * the specified address family.
657 */
658 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
659 curp = curp->ifa_next;
660 continue;
661 }
662 if ((curp->ifa_addr->sa_family != AF_INET) &&
663 (curp->ifa_addr->sa_family != AF_INET6)) {
664 curp = curp->ifa_next;
665 continue;
666 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700667
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700668 if (op == KVP_OP_GET_IP_INFO) {
669 /*
670 * Gather info other than the IP address.
671 * IP address info will be gathered later.
672 */
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700673 if (curp->ifa_addr->sa_family == AF_INET) {
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700674 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700675 /*
676 * Get subnet info.
677 */
678 error = kvp_process_ip_address(
679 curp->ifa_netmask,
680 AF_INET,
681 (char *)
682 ip_buffer->sub_net,
683 length,
684 &sn_offset);
685 if (error)
686 goto gather_ipaddr;
687 } else {
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700688 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700689
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700690 /*
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700691 * Get subnet info in CIDR format.
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700692 */
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700693 weight = 0;
694 sn_str = (char *)ip_buffer->sub_net;
695 addr6 = (struct sockaddr_in6 *)
696 curp->ifa_netmask;
697 w = addr6->sin6_addr.s6_addr32;
698
699 for (i = 0; i < 4; i++)
700 weight += hweight32(&w[i]);
701
702 sprintf(cidr_mask, "/%d", weight);
703 if ((length - sn_offset) <
704 (strlen(cidr_mask) + 1))
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700705 goto gather_ipaddr;
K. Y. Srinivasan6a60a6a2012-08-16 18:32:17 -0700706
707 if (sn_offset == 0)
708 strcpy(sn_str, cidr_mask);
709 else
710 strcat(sn_str, cidr_mask);
711 strcat((char *)ip_buffer->sub_net, ";");
712 sn_offset += strlen(sn_str) + 1;
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700713 }
K. Y. Srinivasan4a52c4a2012-08-16 18:32:18 -0700714
715 /*
716 * Collect other ip related configuration info.
717 */
718
719 kvp_get_ipconfig_info(if_name, ip_buffer);
K. Y. Srinivasan0d5b6b12012-08-16 18:32:15 -0700720 }
721
K. Y. Srinivasan04405782012-08-16 18:32:16 -0700722gather_ipaddr:
K. Y. Srinivasanaf733012012-08-16 18:32:14 -0700723 error = kvp_process_ip_address(curp->ifa_addr,
724 curp->ifa_addr->sa_family,
725 buffer,
726 length, &offset);
727 if (error)
728 goto getaddr_done;
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700729
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700730 curp = curp->ifa_next;
731 }
732
733getaddr_done:
734 freeifaddrs(ifap);
735 return error;
736}
737
738
739static int
740kvp_get_domain_name(char *buffer, int length)
741{
742 struct addrinfo hints, *info ;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700743 int error = 0;
744
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700745 gethostname(buffer, length);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700746 memset(&hints, 0, sizeof(hints));
747 hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
748 hints.ai_socktype = SOCK_STREAM;
749 hints.ai_flags = AI_CANONNAME;
750
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700751 error = getaddrinfo(buffer, NULL, &hints, &info);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700752 if (error != 0) {
753 strcpy(buffer, "getaddrinfo failed\n");
K. Y. Srinivasan5be528c2011-07-26 11:03:10 -0700754 return error;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700755 }
756 strcpy(buffer, info->ai_canonname);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700757 freeaddrinfo(info);
758 return error;
759}
760
761static int
762netlink_send(int fd, struct cn_msg *msg)
763{
764 struct nlmsghdr *nlh;
765 unsigned int size;
766 struct msghdr message;
767 char buffer[64];
768 struct iovec iov[2];
769
770 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
771
772 nlh = (struct nlmsghdr *)buffer;
773 nlh->nlmsg_seq = 0;
774 nlh->nlmsg_pid = getpid();
775 nlh->nlmsg_type = NLMSG_DONE;
776 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
777 nlh->nlmsg_flags = 0;
778
779 iov[0].iov_base = nlh;
780 iov[0].iov_len = sizeof(*nlh);
781
782 iov[1].iov_base = msg;
783 iov[1].iov_len = size;
784
785 memset(&message, 0, sizeof(message));
786 message.msg_name = &addr;
787 message.msg_namelen = sizeof(addr);
788 message.msg_iov = iov;
789 message.msg_iovlen = 2;
790
791 return sendmsg(fd, &message, 0);
792}
793
Olaf Hering7989f7d2011-03-22 10:02:17 +0100794int main(void)
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700795{
796 int fd, len, sock_opt;
797 int error;
798 struct cn_msg *message;
799 struct pollfd pfd;
800 struct nlmsghdr *incoming_msg;
801 struct cn_msg *incoming_cn_msg;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800802 struct hv_kvp_msg *hv_msg;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100803 char *p;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700804 char *key_value;
805 char *key_name;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700806 int op;
807 int pool;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700808
809 daemon(1, 0);
810 openlog("KVP", 0, LOG_USER);
811 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
812 /*
813 * Retrieve OS release information.
814 */
815 kvp_get_os_info();
816
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700817 if (kvp_file_init()) {
818 syslog(LOG_ERR, "Failed to initialize the pools");
819 exit(-1);
820 }
821
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700822 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
823 if (fd < 0) {
824 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
825 exit(-1);
826 }
827 addr.nl_family = AF_NETLINK;
828 addr.nl_pad = 0;
829 addr.nl_pid = 0;
830 addr.nl_groups = CN_KVP_IDX;
831
832
833 error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
834 if (error < 0) {
835 syslog(LOG_ERR, "bind failed; error:%d", error);
836 close(fd);
837 exit(-1);
838 }
839 sock_opt = addr.nl_groups;
840 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
841 /*
842 * Register ourselves with the kernel.
843 */
844 message = (struct cn_msg *)kvp_send_buffer;
845 message->id.idx = CN_KVP_IDX;
846 message->id.val = CN_KVP_VAL;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800847
848 hv_msg = (struct hv_kvp_msg *)message->data;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700849 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700850 message->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800851 message->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700852
853 len = netlink_send(fd, message);
854 if (len < 0) {
855 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
856 close(fd);
857 exit(-1);
858 }
859
860 pfd.fd = fd;
861
862 while (1) {
Olaf Heringbcc2c9c2012-05-31 16:40:06 +0200863 struct sockaddr *addr_p = (struct sockaddr *) &addr;
864 socklen_t addr_l = sizeof(addr);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700865 pfd.events = POLLIN;
866 pfd.revents = 0;
867 poll(&pfd, 1, -1);
868
Olaf Heringbcc2c9c2012-05-31 16:40:06 +0200869 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
870 addr_p, &addr_l);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700871
Olaf Heringbcc2c9c2012-05-31 16:40:06 +0200872 if (len < 0 || addr.nl_pid) {
873 syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
874 addr.nl_pid, errno, strerror(errno));
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700875 close(fd);
876 return -1;
877 }
878
879 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
880 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800881 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700882
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700883 /*
884 * We will use the KVP header information to pass back
885 * the error from this daemon. So, first copy the state
886 * and set the error code to success.
887 */
888 op = hv_msg->kvp_hdr.operation;
889 pool = hv_msg->kvp_hdr.pool;
890 hv_msg->error = HV_S_OK;
891
892 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700893 /*
894 * Driver is registering with us; stash away the version
895 * information.
896 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700897 in_hand_shake = 0;
K. Y. Srinivasane485ceac2012-03-10 15:32:08 -0800898 p = (char *)hv_msg->body.kvp_register.version;
Olaf Hering7989f7d2011-03-22 10:02:17 +0100899 lic_version = malloc(strlen(p) + 1);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700900 if (lic_version) {
Olaf Hering7989f7d2011-03-22 10:02:17 +0100901 strcpy(lic_version, p);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700902 syslog(LOG_INFO, "KVP LIC Version: %s",
903 lic_version);
904 } else {
905 syslog(LOG_ERR, "malloc failed");
906 }
907 continue;
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700908 }
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700909
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700910 switch (op) {
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700911 case KVP_OP_SET:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700912 if (kvp_key_add_or_modify(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700913 hv_msg->body.kvp_set.data.key,
914 hv_msg->body.kvp_set.data.key_size,
915 hv_msg->body.kvp_set.data.value,
916 hv_msg->body.kvp_set.data.value_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700917 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700918 break;
919
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700920 case KVP_OP_GET:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700921 if (kvp_get_value(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700922 hv_msg->body.kvp_set.data.key,
923 hv_msg->body.kvp_set.data.key_size,
924 hv_msg->body.kvp_set.data.value,
925 hv_msg->body.kvp_set.data.value_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700926 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700927 break;
928
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700929 case KVP_OP_DELETE:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700930 if (kvp_key_delete(pool,
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700931 hv_msg->body.kvp_delete.key,
932 hv_msg->body.kvp_delete.key_size))
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700933 hv_msg->error = HV_S_CONT;
K. Y. Srinivasandb425332012-03-16 08:02:26 -0700934 break;
935
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700936 default:
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800937 break;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700938 }
939
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700940 if (op != KVP_OP_ENUMERATE)
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -0700941 goto kvp_done;
942
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700943 /*
944 * If the pool is KVP_POOL_AUTO, dynamically generate
945 * both the key and the value; if not read from the
946 * appropriate pool.
947 */
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700948 if (pool != KVP_POOL_AUTO) {
949 if (kvp_pool_enumerate(pool,
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700950 hv_msg->body.kvp_enum_data.index,
951 hv_msg->body.kvp_enum_data.data.key,
952 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
953 hv_msg->body.kvp_enum_data.data.value,
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -0700954 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
955 hv_msg->error = HV_S_CONT;
K. Y. Srinivasanadc80ae2012-03-16 08:02:27 -0700956 goto kvp_done;
957 }
958
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800959 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
960 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
961 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700962
K. Y. Srinivasan26403352012-02-02 16:56:50 -0800963 switch (hv_msg->body.kvp_enum_data.index) {
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700964 case FullyQualifiedDomainName:
965 kvp_get_domain_name(key_value,
966 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
967 strcpy(key_name, "FullyQualifiedDomainName");
968 break;
969 case IntegrationServicesVersion:
970 strcpy(key_name, "IntegrationServicesVersion");
971 strcpy(key_value, lic_version);
972 break;
973 case NetworkAddressIPv4:
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700974 kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
975 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700976 strcpy(key_name, "NetworkAddressIPv4");
977 break;
978 case NetworkAddressIPv6:
K. Y. Srinivasan0ecaa192012-08-16 18:32:13 -0700979 kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
980 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
Ky Srinivasancc04acf2010-12-16 18:56:54 -0700981 strcpy(key_name, "NetworkAddressIPv6");
982 break;
983 case OSBuildNumber:
984 strcpy(key_value, os_build);
985 strcpy(key_name, "OSBuildNumber");
986 break;
987 case OSName:
988 strcpy(key_value, os_name);
989 strcpy(key_name, "OSName");
990 break;
991 case OSMajorVersion:
992 strcpy(key_value, os_major);
993 strcpy(key_name, "OSMajorVersion");
994 break;
995 case OSMinorVersion:
996 strcpy(key_value, os_minor);
997 strcpy(key_name, "OSMinorVersion");
998 break;
999 case OSVersion:
1000 strcpy(key_value, os_build);
1001 strcpy(key_name, "OSVersion");
1002 break;
1003 case ProcessorArchitecture:
1004 strcpy(key_value, processor_arch);
1005 strcpy(key_name, "ProcessorArchitecture");
1006 break;
1007 default:
K. Y. Srinivasanb47a81d2012-08-13 10:06:52 -07001008 hv_msg->error = HV_S_CONT;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001009 break;
1010 }
1011 /*
1012 * Send the value back to the kernel. The response is
1013 * already in the receive buffer. Update the cn_msg header to
1014 * reflect the key value that has been added to the message
1015 */
K. Y. Srinivasanfa3d5b82012-03-16 08:02:25 -07001016kvp_done:
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001017
1018 incoming_cn_msg->id.idx = CN_KVP_IDX;
1019 incoming_cn_msg->id.val = CN_KVP_VAL;
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001020 incoming_cn_msg->ack = 0;
K. Y. Srinivasan26403352012-02-02 16:56:50 -08001021 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
Ky Srinivasancc04acf2010-12-16 18:56:54 -07001022
1023 len = netlink_send(fd, incoming_cn_msg);
1024 if (len < 0) {
1025 syslog(LOG_ERR, "net_link send failed; error:%d", len);
1026 exit(-1);
1027 }
1028 }
1029
1030}