Tools: hv: Rename the function kvp_get_ip_address()
[linux-3.10.git] / tools / hv / hv_kvp_daemon.c
1 /*
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 <ctype.h>
35 #include <errno.h>
36 #include <arpa/inet.h>
37 #include <linux/connector.h>
38 #include <linux/hyperv.h>
39 #include <linux/netlink.h>
40 #include <ifaddrs.h>
41 #include <netdb.h>
42 #include <syslog.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <dirent.h>
46
47 /*
48  * KVP protocol: The user mode component first registers with the
49  * the kernel component. Subsequently, the kernel component requests, data
50  * for the specified keys. In response to this message the user mode component
51  * fills in the value corresponding to the specified key. We overload the
52  * sequence field in the cn_msg header to define our KVP message types.
53  *
54  * We use this infrastructure for also supporting queries from user mode
55  * application for state that may be maintained in the KVP kernel component.
56  *
57  */
58
59
60 enum key_index {
61         FullyQualifiedDomainName = 0,
62         IntegrationServicesVersion, /*This key is serviced in the kernel*/
63         NetworkAddressIPv4,
64         NetworkAddressIPv6,
65         OSBuildNumber,
66         OSName,
67         OSMajorVersion,
68         OSMinorVersion,
69         OSVersion,
70         ProcessorArchitecture
71 };
72
73
74 enum {
75         IPADDR = 0,
76         NETMASK,
77         GATEWAY,
78         DNS
79 };
80
81 static char kvp_send_buffer[4096];
82 static char kvp_recv_buffer[4096 * 2];
83 static struct sockaddr_nl addr;
84 static int in_hand_shake = 1;
85
86 static char *os_name = "";
87 static char *os_major = "";
88 static char *os_minor = "";
89 static char *processor_arch;
90 static char *os_build;
91 static char *lic_version = "Unknown version";
92 static struct utsname uts_buf;
93
94 /*
95  * The location of the interface configuration file.
96  */
97
98 #define KVP_CONFIG_LOC  "/var/opt/"
99
100 #define MAX_FILE_NAME 100
101 #define ENTRIES_PER_BLOCK 50
102
103 struct kvp_record {
104         char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
105         char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
106 };
107
108 struct kvp_file_state {
109         int fd;
110         int num_blocks;
111         struct kvp_record *records;
112         int num_records;
113         char fname[MAX_FILE_NAME];
114 };
115
116 static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
117
118 static void kvp_acquire_lock(int pool)
119 {
120         struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
121         fl.l_pid = getpid();
122
123         if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
124                 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
125                 exit(-1);
126         }
127 }
128
129 static void kvp_release_lock(int pool)
130 {
131         struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
132         fl.l_pid = getpid();
133
134         if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
135                 perror("fcntl");
136                 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
137                 exit(-1);
138         }
139 }
140
141 static void kvp_update_file(int pool)
142 {
143         FILE *filep;
144         size_t bytes_written;
145
146         /*
147          * We are going to write our in-memory registry out to
148          * disk; acquire the lock first.
149          */
150         kvp_acquire_lock(pool);
151
152         filep = fopen(kvp_file_info[pool].fname, "w");
153         if (!filep) {
154                 kvp_release_lock(pool);
155                 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
156                 exit(-1);
157         }
158
159         bytes_written = fwrite(kvp_file_info[pool].records,
160                                 sizeof(struct kvp_record),
161                                 kvp_file_info[pool].num_records, filep);
162
163         fflush(filep);
164         kvp_release_lock(pool);
165 }
166
167 static void kvp_update_mem_state(int pool)
168 {
169         FILE *filep;
170         size_t records_read = 0;
171         struct kvp_record *record = kvp_file_info[pool].records;
172         struct kvp_record *readp;
173         int num_blocks = kvp_file_info[pool].num_blocks;
174         int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
175
176         kvp_acquire_lock(pool);
177
178         filep = fopen(kvp_file_info[pool].fname, "r");
179         if (!filep) {
180                 kvp_release_lock(pool);
181                 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
182                 exit(-1);
183         }
184         while (!feof(filep)) {
185                 readp = &record[records_read];
186                 records_read += fread(readp, sizeof(struct kvp_record),
187                                         ENTRIES_PER_BLOCK * num_blocks,
188                                         filep);
189
190                 if (!feof(filep)) {
191                         /*
192                          * We have more data to read.
193                          */
194                         num_blocks++;
195                         record = realloc(record, alloc_unit * num_blocks);
196
197                         if (record == NULL) {
198                                 syslog(LOG_ERR, "malloc failed");
199                                 exit(-1);
200                         }
201                         continue;
202                 }
203                 break;
204         }
205
206         kvp_file_info[pool].num_blocks = num_blocks;
207         kvp_file_info[pool].records = record;
208         kvp_file_info[pool].num_records = records_read;
209
210         kvp_release_lock(pool);
211 }
212 static int kvp_file_init(void)
213 {
214         int  fd;
215         FILE *filep;
216         size_t records_read;
217         char *fname;
218         struct kvp_record *record;
219         struct kvp_record *readp;
220         int num_blocks;
221         int i;
222         int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
223
224         if (access("/var/opt/hyperv", F_OK)) {
225                 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
226                         syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
227                         exit(-1);
228                 }
229         }
230
231         for (i = 0; i < KVP_POOL_COUNT; i++) {
232                 fname = kvp_file_info[i].fname;
233                 records_read = 0;
234                 num_blocks = 1;
235                 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
236                 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
237
238                 if (fd == -1)
239                         return 1;
240
241
242                 filep = fopen(fname, "r");
243                 if (!filep)
244                         return 1;
245
246                 record = malloc(alloc_unit * num_blocks);
247                 if (record == NULL) {
248                         fclose(filep);
249                         return 1;
250                 }
251                 while (!feof(filep)) {
252                         readp = &record[records_read];
253                         records_read += fread(readp, sizeof(struct kvp_record),
254                                         ENTRIES_PER_BLOCK,
255                                         filep);
256
257                         if (!feof(filep)) {
258                                 /*
259                                  * We have more data to read.
260                                  */
261                                 num_blocks++;
262                                 record = realloc(record, alloc_unit *
263                                                 num_blocks);
264                                 if (record == NULL) {
265                                         fclose(filep);
266                                         return 1;
267                                 }
268                                 continue;
269                         }
270                         break;
271                 }
272                 kvp_file_info[i].fd = fd;
273                 kvp_file_info[i].num_blocks = num_blocks;
274                 kvp_file_info[i].records = record;
275                 kvp_file_info[i].num_records = records_read;
276                 fclose(filep);
277
278         }
279
280         return 0;
281 }
282
283 static int kvp_key_delete(int pool, __u8 *key, int key_size)
284 {
285         int i;
286         int j, k;
287         int num_records;
288         struct kvp_record *record;
289
290         /*
291          * First update the in-memory state.
292          */
293         kvp_update_mem_state(pool);
294
295         num_records = kvp_file_info[pool].num_records;
296         record = kvp_file_info[pool].records;
297
298         for (i = 0; i < num_records; i++) {
299                 if (memcmp(key, record[i].key, key_size))
300                         continue;
301                 /*
302                  * Found a match; just move the remaining
303                  * entries up.
304                  */
305                 if (i == num_records) {
306                         kvp_file_info[pool].num_records--;
307                         kvp_update_file(pool);
308                         return 0;
309                 }
310
311                 j = i;
312                 k = j + 1;
313                 for (; k < num_records; k++) {
314                         strcpy(record[j].key, record[k].key);
315                         strcpy(record[j].value, record[k].value);
316                         j++;
317                 }
318
319                 kvp_file_info[pool].num_records--;
320                 kvp_update_file(pool);
321                 return 0;
322         }
323         return 1;
324 }
325
326 static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
327                         int value_size)
328 {
329         int i;
330         int num_records;
331         struct kvp_record *record;
332         int num_blocks;
333
334         if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
335                 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
336                 return 1;
337
338         /*
339          * First update the in-memory state.
340          */
341         kvp_update_mem_state(pool);
342
343         num_records = kvp_file_info[pool].num_records;
344         record = kvp_file_info[pool].records;
345         num_blocks = kvp_file_info[pool].num_blocks;
346
347         for (i = 0; i < num_records; i++) {
348                 if (memcmp(key, record[i].key, key_size))
349                         continue;
350                 /*
351                  * Found a match; just update the value -
352                  * this is the modify case.
353                  */
354                 memcpy(record[i].value, value, value_size);
355                 kvp_update_file(pool);
356                 return 0;
357         }
358
359         /*
360          * Need to add a new entry;
361          */
362         if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
363                 /* Need to allocate a larger array for reg entries. */
364                 record = realloc(record, sizeof(struct kvp_record) *
365                          ENTRIES_PER_BLOCK * (num_blocks + 1));
366
367                 if (record == NULL)
368                         return 1;
369                 kvp_file_info[pool].num_blocks++;
370
371         }
372         memcpy(record[i].value, value, value_size);
373         memcpy(record[i].key, key, key_size);
374         kvp_file_info[pool].records = record;
375         kvp_file_info[pool].num_records++;
376         kvp_update_file(pool);
377         return 0;
378 }
379
380 static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
381                         int value_size)
382 {
383         int i;
384         int num_records;
385         struct kvp_record *record;
386
387         if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
388                 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
389                 return 1;
390
391         /*
392          * First update the in-memory state.
393          */
394         kvp_update_mem_state(pool);
395
396         num_records = kvp_file_info[pool].num_records;
397         record = kvp_file_info[pool].records;
398
399         for (i = 0; i < num_records; i++) {
400                 if (memcmp(key, record[i].key, key_size))
401                         continue;
402                 /*
403                  * Found a match; just copy the value out.
404                  */
405                 memcpy(value, record[i].value, value_size);
406                 return 0;
407         }
408
409         return 1;
410 }
411
412 static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
413                                 __u8 *value, int value_size)
414 {
415         struct kvp_record *record;
416
417         /*
418          * First update our in-memory database.
419          */
420         kvp_update_mem_state(pool);
421         record = kvp_file_info[pool].records;
422
423         if (index >= kvp_file_info[pool].num_records) {
424                 return 1;
425         }
426
427         memcpy(key, record[index].key, key_size);
428         memcpy(value, record[index].value, value_size);
429         return 0;
430 }
431
432
433 void kvp_get_os_info(void)
434 {
435         FILE    *file;
436         char    *p, buf[512];
437
438         uname(&uts_buf);
439         os_build = uts_buf.release;
440         processor_arch = uts_buf.machine;
441
442         /*
443          * The current windows host (win7) expects the build
444          * string to be of the form: x.y.z
445          * Strip additional information we may have.
446          */
447         p = strchr(os_build, '-');
448         if (p)
449                 *p = '\0';
450
451         file = fopen("/etc/SuSE-release", "r");
452         if (file != NULL)
453                 goto kvp_osinfo_found;
454         file  = fopen("/etc/redhat-release", "r");
455         if (file != NULL)
456                 goto kvp_osinfo_found;
457         /*
458          * Add code for other supported platforms.
459          */
460
461         /*
462          * We don't have information about the os.
463          */
464         os_name = uts_buf.sysname;
465         return;
466
467 kvp_osinfo_found:
468         /* up to three lines */
469         p = fgets(buf, sizeof(buf), file);
470         if (p) {
471                 p = strchr(buf, '\n');
472                 if (p)
473                         *p = '\0';
474                 p = strdup(buf);
475                 if (!p)
476                         goto done;
477                 os_name = p;
478
479                 /* second line */
480                 p = fgets(buf, sizeof(buf), file);
481                 if (p) {
482                         p = strchr(buf, '\n');
483                         if (p)
484                                 *p = '\0';
485                         p = strdup(buf);
486                         if (!p)
487                                 goto done;
488                         os_major = p;
489
490                         /* third line */
491                         p = fgets(buf, sizeof(buf), file);
492                         if (p)  {
493                                 p = strchr(buf, '\n');
494                                 if (p)
495                                         *p = '\0';
496                                 p = strdup(buf);
497                                 if (p)
498                                         os_minor = p;
499                         }
500                 }
501         }
502
503 done:
504         fclose(file);
505         return;
506 }
507
508
509
510 /*
511  * Retrieve an interface name corresponding to the specified guid.
512  * If there is a match, the function returns a pointer
513  * to the interface name and if not, a NULL is returned.
514  * If a match is found, the caller is responsible for
515  * freeing the memory.
516  */
517
518 static char *kvp_get_if_name(char *guid)
519 {
520         DIR *dir;
521         struct dirent *entry;
522         FILE    *file;
523         char    *p, *q, *x;
524         char    *if_name = NULL;
525         char    buf[256];
526         char *kvp_net_dir = "/sys/class/net/";
527         char dev_id[256];
528
529         dir = opendir(kvp_net_dir);
530         if (dir == NULL)
531                 return NULL;
532
533         snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
534         q = dev_id + strlen(kvp_net_dir);
535
536         while ((entry = readdir(dir)) != NULL) {
537                 /*
538                  * Set the state for the next pass.
539                  */
540                 *q = '\0';
541                 strcat(dev_id, entry->d_name);
542                 strcat(dev_id, "/device/device_id");
543
544                 file = fopen(dev_id, "r");
545                 if (file == NULL)
546                         continue;
547
548                 p = fgets(buf, sizeof(buf), file);
549                 if (p) {
550                         x = strchr(p, '\n');
551                         if (x)
552                                 *x = '\0';
553
554                         if (!strcmp(p, guid)) {
555                                 /*
556                                  * Found the guid match; return the interface
557                                  * name. The caller will free the memory.
558                                  */
559                                 if_name = strdup(entry->d_name);
560                                 fclose(file);
561                                 break;
562                         }
563                 }
564                 fclose(file);
565         }
566
567         closedir(dir);
568         return if_name;
569 }
570
571 /*
572  * Retrieve the MAC address given the interface name.
573  */
574
575 static char *kvp_if_name_to_mac(char *if_name)
576 {
577         FILE    *file;
578         char    *p, *x;
579         char    buf[256];
580         char addr_file[256];
581         int i;
582         char *mac_addr = NULL;
583
584         snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
585                 if_name, "/address");
586
587         file = fopen(addr_file, "r");
588         if (file == NULL)
589                 return NULL;
590
591         p = fgets(buf, sizeof(buf), file);
592         if (p) {
593                 x = strchr(p, '\n');
594                 if (x)
595                         *x = '\0';
596                 for (i = 0; i < strlen(p); i++)
597                         p[i] = toupper(p[i]);
598                 mac_addr = strdup(p);
599         }
600
601         fclose(file);
602         return mac_addr;
603 }
604
605
606 static void kvp_process_ipconfig_file(char *cmd,
607                                         char *config_buf, int len,
608                                         int element_size, int offset)
609 {
610         char buf[256];
611         char *p;
612         char *x;
613         FILE *file;
614
615         /*
616          * First execute the command.
617          */
618         file = popen(cmd, "r");
619         if (file == NULL)
620                 return;
621
622         if (offset == 0)
623                 memset(config_buf, 0, len);
624         while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
625                 if ((len - strlen(config_buf)) < (element_size + 1))
626                         break;
627
628                 x = strchr(p, '\n');
629                 *x = '\0';
630                 strcat(config_buf, p);
631                 strcat(config_buf, ";");
632         }
633         pclose(file);
634 }
635
636 static void kvp_get_ipconfig_info(char *if_name,
637                                  struct hv_kvp_ipaddr_value *buffer)
638 {
639         char cmd[512];
640         char dhcp_info[128];
641         char *p;
642         FILE *file;
643
644         /*
645          * Get the address of default gateway (ipv4).
646          */
647         sprintf(cmd, "%s %s", "ip route show dev", if_name);
648         strcat(cmd, " | awk '/default/ {print $3 }'");
649
650         /*
651          * Execute the command to gather gateway info.
652          */
653         kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
654                                 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
655
656         /*
657          * Get the address of default gateway (ipv6).
658          */
659         sprintf(cmd, "%s %s", "ip -f inet6  route show dev", if_name);
660         strcat(cmd, " | awk '/default/ {print $3 }'");
661
662         /*
663          * Execute the command to gather gateway info (ipv6).
664          */
665         kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
666                                 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
667
668
669         /*
670          * Gather the DNS  state.
671          * Since there is no standard way to get this information
672          * across various distributions of interest; we just invoke
673          * an external script that needs to be ported across distros
674          * of interest.
675          *
676          * Following is the expected format of the information from the script:
677          *
678          * ipaddr1 (nameserver1)
679          * ipaddr2 (nameserver2)
680          * .
681          * .
682          */
683
684         sprintf(cmd, "%s",  "hv_get_dns_info");
685
686         /*
687          * Execute the command to gather DNS info.
688          */
689         kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
690                                 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
691
692         /*
693          * Gather the DHCP state.
694          * We will gather this state by invoking an external script.
695          * The parameter to the script is the interface name.
696          * Here is the expected output:
697          *
698          * Enabled: DHCP enabled.
699          */
700
701         sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
702
703         file = popen(cmd, "r");
704         if (file == NULL)
705                 return;
706
707         p = fgets(dhcp_info, sizeof(dhcp_info), file);
708         if (p == NULL) {
709                 pclose(file);
710                 return;
711         }
712
713         if (!strncmp(p, "Enabled", 7))
714                 buffer->dhcp_enabled = 1;
715         else
716                 buffer->dhcp_enabled = 0;
717
718         pclose(file);
719 }
720
721
722 static unsigned int hweight32(unsigned int *w)
723 {
724         unsigned int res = *w - ((*w >> 1) & 0x55555555);
725         res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
726         res = (res + (res >> 4)) & 0x0F0F0F0F;
727         res = res + (res >> 8);
728         return (res + (res >> 16)) & 0x000000FF;
729 }
730
731 static int kvp_process_ip_address(void *addrp,
732                                 int family, char *buffer,
733                                 int length,  int *offset)
734 {
735         struct sockaddr_in *addr;
736         struct sockaddr_in6 *addr6;
737         int addr_length;
738         char tmp[50];
739         const char *str;
740
741         if (family == AF_INET) {
742                 addr = (struct sockaddr_in *)addrp;
743                 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
744                 addr_length = INET_ADDRSTRLEN;
745         } else {
746                 addr6 = (struct sockaddr_in6 *)addrp;
747                 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
748                 addr_length = INET6_ADDRSTRLEN;
749         }
750
751         if ((length - *offset) < addr_length + 1)
752                 return 1;
753         if (str == NULL) {
754                 strcpy(buffer, "inet_ntop failed\n");
755                 return 1;
756         }
757         if (*offset == 0)
758                 strcpy(buffer, tmp);
759         else
760                 strcat(buffer, tmp);
761         strcat(buffer, ";");
762
763         *offset += strlen(str) + 1;
764         return 0;
765 }
766
767 static int
768 kvp_get_ip_info(int family, char *if_name, int op,
769                  void  *out_buffer, int length)
770 {
771         struct ifaddrs *ifap;
772         struct ifaddrs *curp;
773         int offset = 0;
774         int sn_offset = 0;
775         int error = 0;
776         char *buffer;
777         struct hv_kvp_ipaddr_value *ip_buffer;
778         char cidr_mask[5]; /* /xyz */
779         int weight;
780         int i;
781         unsigned int *w;
782         char *sn_str;
783         struct sockaddr_in6 *addr6;
784
785         if (op == KVP_OP_ENUMERATE) {
786                 buffer = out_buffer;
787         } else {
788                 ip_buffer = out_buffer;
789                 buffer = (char *)ip_buffer->ip_addr;
790                 ip_buffer->addr_family = 0;
791         }
792         /*
793          * On entry into this function, the buffer is capable of holding the
794          * maximum key value.
795          */
796
797         if (getifaddrs(&ifap)) {
798                 strcpy(buffer, "getifaddrs failed\n");
799                 return 1;
800         }
801
802         curp = ifap;
803         while (curp != NULL) {
804                 if (curp->ifa_addr == NULL) {
805                         curp = curp->ifa_next;
806                         continue;
807                 }
808
809                 if ((if_name != NULL) &&
810                         (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
811                         /*
812                          * We want info about a specific interface;
813                          * just continue.
814                          */
815                         curp = curp->ifa_next;
816                         continue;
817                 }
818
819                 /*
820                  * We only support two address families: AF_INET and AF_INET6.
821                  * If a family value of 0 is specified, we collect both
822                  * supported address families; if not we gather info on
823                  * the specified address family.
824                  */
825                 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
826                         curp = curp->ifa_next;
827                         continue;
828                 }
829                 if ((curp->ifa_addr->sa_family != AF_INET) &&
830                         (curp->ifa_addr->sa_family != AF_INET6)) {
831                         curp = curp->ifa_next;
832                         continue;
833                 }
834
835                 if (op == KVP_OP_GET_IP_INFO) {
836                         /*
837                          * Gather info other than the IP address.
838                          * IP address info will be gathered later.
839                          */
840                         if (curp->ifa_addr->sa_family == AF_INET) {
841                                 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
842                                 /*
843                                  * Get subnet info.
844                                  */
845                                 error = kvp_process_ip_address(
846                                                              curp->ifa_netmask,
847                                                              AF_INET,
848                                                              (char *)
849                                                              ip_buffer->sub_net,
850                                                              length,
851                                                              &sn_offset);
852                                 if (error)
853                                         goto gather_ipaddr;
854                         } else {
855                                 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
856
857                                 /*
858                                  * Get subnet info in CIDR format.
859                                  */
860                                 weight = 0;
861                                 sn_str = (char *)ip_buffer->sub_net;
862                                 addr6 = (struct sockaddr_in6 *)
863                                         curp->ifa_netmask;
864                                 w = addr6->sin6_addr.s6_addr32;
865
866                                 for (i = 0; i < 4; i++)
867                                         weight += hweight32(&w[i]);
868
869                                 sprintf(cidr_mask, "/%d", weight);
870                                 if ((length - sn_offset) <
871                                         (strlen(cidr_mask) + 1))
872                                         goto gather_ipaddr;
873
874                                 if (sn_offset == 0)
875                                         strcpy(sn_str, cidr_mask);
876                                 else
877                                         strcat(sn_str, cidr_mask);
878                                 strcat((char *)ip_buffer->sub_net, ";");
879                                 sn_offset += strlen(sn_str) + 1;
880                         }
881
882                         /*
883                          * Collect other ip related configuration info.
884                          */
885
886                         kvp_get_ipconfig_info(if_name, ip_buffer);
887                 }
888
889 gather_ipaddr:
890                 error = kvp_process_ip_address(curp->ifa_addr,
891                                                 curp->ifa_addr->sa_family,
892                                                 buffer,
893                                                 length, &offset);
894                 if (error)
895                         goto getaddr_done;
896
897                 curp = curp->ifa_next;
898         }
899
900 getaddr_done:
901         freeifaddrs(ifap);
902         return error;
903 }
904
905
906 static int expand_ipv6(char *addr, int type)
907 {
908         int ret;
909         struct in6_addr v6_addr;
910
911         ret = inet_pton(AF_INET6, addr, &v6_addr);
912
913         if (ret != 1) {
914                 if (type == NETMASK)
915                         return 1;
916                 return 0;
917         }
918
919         sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
920                 "%02x%02x:%02x%02x:%02x%02x",
921                 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
922                 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
923                 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
924                 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
925                 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
926                 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
927                 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
928                 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
929
930         return 1;
931
932 }
933
934 static int is_ipv4(char *addr)
935 {
936         int ret;
937         struct in_addr ipv4_addr;
938
939         ret = inet_pton(AF_INET, addr, &ipv4_addr);
940
941         if (ret == 1)
942                 return 1;
943         return 0;
944 }
945
946 static int parse_ip_val_buffer(char *in_buf, int *offset,
947                                 char *out_buf, int out_len)
948 {
949         char *x;
950         char *start;
951
952         /*
953          * in_buf has sequence of characters that are seperated by
954          * the character ';'. The last sequence does not have the
955          * terminating ";" character.
956          */
957         start = in_buf + *offset;
958
959         x = strchr(start, ';');
960         if (x)
961                 *x = 0;
962         else
963                 x = start + strlen(start);
964
965         if (strlen(start) != 0) {
966                 int i = 0;
967                 /*
968                  * Get rid of leading spaces.
969                  */
970                 while (start[i] == ' ')
971                         i++;
972
973                 if ((x - start) <= out_len) {
974                         strcpy(out_buf, (start + i));
975                         *offset += (x - start) + 1;
976                         return 1;
977                 }
978         }
979         return 0;
980 }
981
982 static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
983 {
984         int ret;
985
986         ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
987
988         if (ret < 0)
989                 return HV_E_FAIL;
990
991         return 0;
992 }
993
994
995 static int process_ip_string(FILE *f, char *ip_string, int type)
996 {
997         int error = 0;
998         char addr[INET6_ADDRSTRLEN];
999         int i = 0;
1000         int j = 0;
1001         char str[256];
1002         char sub_str[10];
1003         int offset = 0;
1004
1005         memset(addr, 0, sizeof(addr));
1006
1007         while (parse_ip_val_buffer(ip_string, &offset, addr,
1008                                         (MAX_IP_ADDR_SIZE * 2))) {
1009
1010                 sub_str[0] = 0;
1011                 if (is_ipv4(addr)) {
1012                         switch (type) {
1013                         case IPADDR:
1014                                 snprintf(str, sizeof(str), "%s", "IPADDR");
1015                                 break;
1016                         case NETMASK:
1017                                 snprintf(str, sizeof(str), "%s", "NETMASK");
1018                                 break;
1019                         case GATEWAY:
1020                                 snprintf(str, sizeof(str), "%s", "GATEWAY");
1021                                 break;
1022                         case DNS:
1023                                 snprintf(str, sizeof(str), "%s", "DNS");
1024                                 break;
1025                         }
1026                         if (i != 0) {
1027                                 if (type != DNS) {
1028                                         snprintf(sub_str, sizeof(sub_str),
1029                                                 "_%d", i++);
1030                                 } else {
1031                                         snprintf(sub_str, sizeof(sub_str),
1032                                                 "%d", ++i);
1033                                 }
1034                         } else if (type == DNS) {
1035                                 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1036                         }
1037
1038
1039                 } else if (expand_ipv6(addr, type)) {
1040                         switch (type) {
1041                         case IPADDR:
1042                                 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1043                                 break;
1044                         case NETMASK:
1045                                 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1046                                 break;
1047                         case GATEWAY:
1048                                 snprintf(str, sizeof(str), "%s",
1049                                         "IPV6_DEFAULTGW");
1050                                 break;
1051                         case DNS:
1052                                 snprintf(str, sizeof(str), "%s",  "DNS");
1053                                 break;
1054                         }
1055                         if ((j != 0) || (type == DNS)) {
1056                                 if (type != DNS) {
1057                                         snprintf(sub_str, sizeof(sub_str),
1058                                                 "_%d", j++);
1059                                 } else {
1060                                         snprintf(sub_str, sizeof(sub_str),
1061                                                 "%d", ++i);
1062                                 }
1063                         } else if (type == DNS) {
1064                                 snprintf(sub_str, sizeof(sub_str),
1065                                         "%d", ++i);
1066                         }
1067                 } else {
1068                         return  HV_INVALIDARG;
1069                 }
1070
1071                 error = kvp_write_file(f, str, sub_str, addr);
1072                 if (error)
1073                         return error;
1074                 memset(addr, 0, sizeof(addr));
1075         }
1076
1077         return 0;
1078 }
1079
1080 static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1081 {
1082         int error = 0;
1083         char if_file[128];
1084         FILE *file;
1085         char cmd[512];
1086         char *mac_addr;
1087
1088         /*
1089          * Set the configuration for the specified interface with
1090          * the information provided. Since there is no standard
1091          * way to configure an interface, we will have an external
1092          * script that does the job of configuring the interface and
1093          * flushing the configuration.
1094          *
1095          * The parameters passed to this external script are:
1096          * 1. A configuration file that has the specified configuration.
1097          *
1098          * We will embed the name of the interface in the configuration
1099          * file: ifcfg-ethx (where ethx is the interface name).
1100          *
1101          * The information provided here may be more than what is needed
1102          * in a given distro to configure the interface and so are free
1103          * ignore information that may not be relevant.
1104          *
1105          * Here is the format of the ip configuration file:
1106          *
1107          * HWADDR=macaddr
1108          * IF_NAME=interface name
1109          * DHCP=yes (This is optional; if yes, DHCP is configured)
1110          *
1111          * IPADDR=ipaddr1
1112          * IPADDR_1=ipaddr2
1113          * IPADDR_x=ipaddry (where y = x + 1)
1114          *
1115          * NETMASK=netmask1
1116          * NETMASK_x=netmasky (where y = x + 1)
1117          *
1118          * GATEWAY=ipaddr1
1119          * GATEWAY_x=ipaddry (where y = x + 1)
1120          *
1121          * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1122          *
1123          * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1124          * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1125          * IPV6NETMASK.
1126          *
1127          * The host can specify multiple ipv4 and ipv6 addresses to be
1128          * configured for the interface. Furthermore, the configuration
1129          * needs to be persistent. A subsequent GET call on the interface
1130          * is expected to return the configuration that is set via the SET
1131          * call.
1132          */
1133
1134         snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1135                 "hyperv/ifcfg-", if_name);
1136
1137         file = fopen(if_file, "w");
1138
1139         if (file == NULL) {
1140                 syslog(LOG_ERR, "Failed to open config file");
1141                 return HV_E_FAIL;
1142         }
1143
1144         /*
1145          * First write out the MAC address.
1146          */
1147
1148         mac_addr = kvp_if_name_to_mac(if_name);
1149         if (mac_addr == NULL) {
1150                 error = HV_E_FAIL;
1151                 goto setval_error;
1152         }
1153
1154         error = kvp_write_file(file, "HWADDR", "", mac_addr);
1155         if (error)
1156                 goto setval_error;
1157
1158         error = kvp_write_file(file, "IF_NAME", "", if_name);
1159         if (error)
1160                 goto setval_error;
1161
1162         if (new_val->dhcp_enabled) {
1163                 error = kvp_write_file(file, "DHCP", "", "yes");
1164                 if (error)
1165                         goto setval_error;
1166
1167                 /*
1168                  * We are done!.
1169                  */
1170                 goto setval_done;
1171         }
1172
1173         /*
1174          * Write the configuration for ipaddress, netmask, gateway and
1175          * name servers.
1176          */
1177
1178         error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1179         if (error)
1180                 goto setval_error;
1181
1182         error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1183         if (error)
1184                 goto setval_error;
1185
1186         error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1187         if (error)
1188                 goto setval_error;
1189
1190         error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1191         if (error)
1192                 goto setval_error;
1193
1194 setval_done:
1195         free(mac_addr);
1196         fclose(file);
1197
1198         /*
1199          * Now that we have populated the configuration file,
1200          * invoke the external script to do its magic.
1201          */
1202
1203         snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1204         system(cmd);
1205         return 0;
1206
1207 setval_error:
1208         syslog(LOG_ERR, "Failed to write config file");
1209         free(mac_addr);
1210         fclose(file);
1211         return error;
1212 }
1213
1214
1215 static int
1216 kvp_get_domain_name(char *buffer, int length)
1217 {
1218         struct addrinfo hints, *info ;
1219         int error = 0;
1220
1221         gethostname(buffer, length);
1222         memset(&hints, 0, sizeof(hints));
1223         hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
1224         hints.ai_socktype = SOCK_STREAM;
1225         hints.ai_flags = AI_CANONNAME;
1226
1227         error = getaddrinfo(buffer, NULL, &hints, &info);
1228         if (error != 0) {
1229                 strcpy(buffer, "getaddrinfo failed\n");
1230                 return error;
1231         }
1232         strcpy(buffer, info->ai_canonname);
1233         freeaddrinfo(info);
1234         return error;
1235 }
1236
1237 static int
1238 netlink_send(int fd, struct cn_msg *msg)
1239 {
1240         struct nlmsghdr *nlh;
1241         unsigned int size;
1242         struct msghdr message;
1243         char buffer[64];
1244         struct iovec iov[2];
1245
1246         size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
1247
1248         nlh = (struct nlmsghdr *)buffer;
1249         nlh->nlmsg_seq = 0;
1250         nlh->nlmsg_pid = getpid();
1251         nlh->nlmsg_type = NLMSG_DONE;
1252         nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
1253         nlh->nlmsg_flags = 0;
1254
1255         iov[0].iov_base = nlh;
1256         iov[0].iov_len = sizeof(*nlh);
1257
1258         iov[1].iov_base = msg;
1259         iov[1].iov_len = size;
1260
1261         memset(&message, 0, sizeof(message));
1262         message.msg_name = &addr;
1263         message.msg_namelen = sizeof(addr);
1264         message.msg_iov = iov;
1265         message.msg_iovlen = 2;
1266
1267         return sendmsg(fd, &message, 0);
1268 }
1269
1270 int main(void)
1271 {
1272         int fd, len, sock_opt;
1273         int error;
1274         struct cn_msg *message;
1275         struct pollfd pfd;
1276         struct nlmsghdr *incoming_msg;
1277         struct cn_msg   *incoming_cn_msg;
1278         struct hv_kvp_msg *hv_msg;
1279         char    *p;
1280         char    *key_value;
1281         char    *key_name;
1282         int     op;
1283         int     pool;
1284         char    *if_name;
1285         struct hv_kvp_ipaddr_value *kvp_ip_val;
1286
1287         daemon(1, 0);
1288         openlog("KVP", 0, LOG_USER);
1289         syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1290         /*
1291          * Retrieve OS release information.
1292          */
1293         kvp_get_os_info();
1294
1295         if (kvp_file_init()) {
1296                 syslog(LOG_ERR, "Failed to initialize the pools");
1297                 exit(-1);
1298         }
1299
1300         fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
1301         if (fd < 0) {
1302                 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
1303                 exit(-1);
1304         }
1305         addr.nl_family = AF_NETLINK;
1306         addr.nl_pad = 0;
1307         addr.nl_pid = 0;
1308         addr.nl_groups = CN_KVP_IDX;
1309
1310
1311         error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
1312         if (error < 0) {
1313                 syslog(LOG_ERR, "bind failed; error:%d", error);
1314                 close(fd);
1315                 exit(-1);
1316         }
1317         sock_opt = addr.nl_groups;
1318         setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
1319         /*
1320          * Register ourselves with the kernel.
1321          */
1322         message = (struct cn_msg *)kvp_send_buffer;
1323         message->id.idx = CN_KVP_IDX;
1324         message->id.val = CN_KVP_VAL;
1325
1326         hv_msg = (struct hv_kvp_msg *)message->data;
1327         hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
1328         message->ack = 0;
1329         message->len = sizeof(struct hv_kvp_msg);
1330
1331         len = netlink_send(fd, message);
1332         if (len < 0) {
1333                 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
1334                 close(fd);
1335                 exit(-1);
1336         }
1337
1338         pfd.fd = fd;
1339
1340         while (1) {
1341                 struct sockaddr *addr_p = (struct sockaddr *) &addr;
1342                 socklen_t addr_l = sizeof(addr);
1343                 pfd.events = POLLIN;
1344                 pfd.revents = 0;
1345                 poll(&pfd, 1, -1);
1346
1347                 len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
1348                                 addr_p, &addr_l);
1349
1350                 if (len < 0 || addr.nl_pid) {
1351                         syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
1352                                         addr.nl_pid, errno, strerror(errno));
1353                         close(fd);
1354                         return -1;
1355                 }
1356
1357                 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
1358                 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
1359                 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1360
1361                 /*
1362                  * We will use the KVP header information to pass back
1363                  * the error from this daemon. So, first copy the state
1364                  * and set the error code to success.
1365                  */
1366                 op = hv_msg->kvp_hdr.operation;
1367                 pool = hv_msg->kvp_hdr.pool;
1368                 hv_msg->error = HV_S_OK;
1369
1370                 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
1371                         /*
1372                          * Driver is registering with us; stash away the version
1373                          * information.
1374                          */
1375                         in_hand_shake = 0;
1376                         p = (char *)hv_msg->body.kvp_register.version;
1377                         lic_version = malloc(strlen(p) + 1);
1378                         if (lic_version) {
1379                                 strcpy(lic_version, p);
1380                                 syslog(LOG_INFO, "KVP LIC Version: %s",
1381                                         lic_version);
1382                         } else {
1383                                 syslog(LOG_ERR, "malloc failed");
1384                         }
1385                         continue;
1386                 }
1387
1388                 switch (op) {
1389                 case KVP_OP_SET_IP_INFO:
1390                         kvp_ip_val = &hv_msg->body.kvp_ip_val;
1391                         if_name = kvp_get_if_name(
1392                                         (char *)kvp_ip_val->adapter_id);
1393                         if (if_name == NULL) {
1394                                 /*
1395                                  * We could not map the guid to an
1396                                  * interface name; return error.
1397                                  */
1398                                 hv_msg->error = HV_GUID_NOTFOUND;
1399                                 break;
1400                         }
1401                         error = kvp_set_ip_info(if_name, kvp_ip_val);
1402                         if (error)
1403                                 hv_msg->error = error;
1404
1405                         free(if_name);
1406                         break;
1407
1408                 case KVP_OP_SET:
1409                         if (kvp_key_add_or_modify(pool,
1410                                         hv_msg->body.kvp_set.data.key,
1411                                         hv_msg->body.kvp_set.data.key_size,
1412                                         hv_msg->body.kvp_set.data.value,
1413                                         hv_msg->body.kvp_set.data.value_size))
1414                                         hv_msg->error = HV_S_CONT;
1415                         break;
1416
1417                 case KVP_OP_GET:
1418                         if (kvp_get_value(pool,
1419                                         hv_msg->body.kvp_set.data.key,
1420                                         hv_msg->body.kvp_set.data.key_size,
1421                                         hv_msg->body.kvp_set.data.value,
1422                                         hv_msg->body.kvp_set.data.value_size))
1423                                         hv_msg->error = HV_S_CONT;
1424                         break;
1425
1426                 case KVP_OP_DELETE:
1427                         if (kvp_key_delete(pool,
1428                                         hv_msg->body.kvp_delete.key,
1429                                         hv_msg->body.kvp_delete.key_size))
1430                                         hv_msg->error = HV_S_CONT;
1431                         break;
1432
1433                 default:
1434                         break;
1435                 }
1436
1437                 if (op != KVP_OP_ENUMERATE)
1438                         goto kvp_done;
1439
1440                 /*
1441                  * If the pool is KVP_POOL_AUTO, dynamically generate
1442                  * both the key and the value; if not read from the
1443                  * appropriate pool.
1444                  */
1445                 if (pool != KVP_POOL_AUTO) {
1446                         if (kvp_pool_enumerate(pool,
1447                                         hv_msg->body.kvp_enum_data.index,
1448                                         hv_msg->body.kvp_enum_data.data.key,
1449                                         HV_KVP_EXCHANGE_MAX_KEY_SIZE,
1450                                         hv_msg->body.kvp_enum_data.data.value,
1451                                         HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1452                                         hv_msg->error = HV_S_CONT;
1453                         goto kvp_done;
1454                 }
1455
1456                 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
1457                 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
1458                 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
1459
1460                 switch (hv_msg->body.kvp_enum_data.index) {
1461                 case FullyQualifiedDomainName:
1462                         kvp_get_domain_name(key_value,
1463                                         HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1464                         strcpy(key_name, "FullyQualifiedDomainName");
1465                         break;
1466                 case IntegrationServicesVersion:
1467                         strcpy(key_name, "IntegrationServicesVersion");
1468                         strcpy(key_value, lic_version);
1469                         break;
1470                 case NetworkAddressIPv4:
1471                         kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
1472                                 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1473                         strcpy(key_name, "NetworkAddressIPv4");
1474                         break;
1475                 case NetworkAddressIPv6:
1476                         kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
1477                                 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
1478                         strcpy(key_name, "NetworkAddressIPv6");
1479                         break;
1480                 case OSBuildNumber:
1481                         strcpy(key_value, os_build);
1482                         strcpy(key_name, "OSBuildNumber");
1483                         break;
1484                 case OSName:
1485                         strcpy(key_value, os_name);
1486                         strcpy(key_name, "OSName");
1487                         break;
1488                 case OSMajorVersion:
1489                         strcpy(key_value, os_major);
1490                         strcpy(key_name, "OSMajorVersion");
1491                         break;
1492                 case OSMinorVersion:
1493                         strcpy(key_value, os_minor);
1494                         strcpy(key_name, "OSMinorVersion");
1495                         break;
1496                 case OSVersion:
1497                         strcpy(key_value, os_build);
1498                         strcpy(key_name, "OSVersion");
1499                         break;
1500                 case ProcessorArchitecture:
1501                         strcpy(key_value, processor_arch);
1502                         strcpy(key_name, "ProcessorArchitecture");
1503                         break;
1504                 default:
1505                         hv_msg->error = HV_S_CONT;
1506                         break;
1507                 }
1508                 /*
1509                  * Send the value back to the kernel. The response is
1510                  * already in the receive buffer. Update the cn_msg header to
1511                  * reflect the key value that has been added to the message
1512                  */
1513 kvp_done:
1514
1515                 incoming_cn_msg->id.idx = CN_KVP_IDX;
1516                 incoming_cn_msg->id.val = CN_KVP_VAL;
1517                 incoming_cn_msg->ack = 0;
1518                 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
1519
1520                 len = netlink_send(fd, incoming_cn_msg);
1521                 if (len < 0) {
1522                         syslog(LOG_ERR, "net_link send failed; error:%d", len);
1523                         exit(-1);
1524                 }
1525         }
1526
1527 }