2 * An implementation of key value pair (KVP) functionality for Linux.
5 * Copyright (C) 2010, Novell, Inc.
6 * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
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.
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
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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <sys/utsname.h>
29 #include <linux/types.h>
35 #include <arpa/inet.h>
36 #include <linux/connector.h>
37 #include <linux/netlink.h>
43 * KYS: TODO. Need to register these in the kernel.
45 * The following definitions are shared with the in-kernel component; do not
46 * change any of this without making the corresponding changes in
47 * the KVP kernel component.
49 #define CN_KVP_IDX 0x9 /* MSFT KVP functionality */
50 #define CN_KVP_VAL 0x1 /* This supports queries from the kernel */
51 #define CN_KVP_USER_VAL 0x2 /* This supports queries from the user */
54 * KVP protocol: The user mode component first registers with the
55 * the kernel component. Subsequently, the kernel component requests, data
56 * for the specified keys. In response to this message the user mode component
57 * fills in the value corresponding to the specified key. We overload the
58 * sequence field in the cn_msg header to define our KVP message types.
60 * We use this infrastructure for also supporting queries from user mode
61 * application for state that may be maintained in the KVP kernel component.
63 * XXXKYS: Have a shared header file between the user and kernel (TODO)
67 KVP_REGISTER
= 0, /* Register the user mode component*/
68 KVP_KERNEL_GET
, /*Kernel is requesting the value for the specified key*/
69 KVP_KERNEL_SET
, /*Kernel is providing the value for the specified key*/
70 KVP_USER_GET
, /*User is requesting the value for the specified key*/
71 KVP_USER_SET
/*User is providing the value for the specified key*/
74 #define HV_KVP_EXCHANGE_MAX_KEY_SIZE 512
75 #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE 2048
79 __u8 kvp_key
[HV_KVP_EXCHANGE_MAX_KEY_SIZE
]; /* Key name */
80 __u8 kvp_value
[HV_KVP_EXCHANGE_MAX_VALUE_SIZE
]; /* Key value */
84 FullyQualifiedDomainName
= 0,
85 IntegrationServicesVersion
, /*This key is serviced in the kernel*/
97 * End of shared definitions.
100 static char kvp_send_buffer
[4096];
101 static char kvp_recv_buffer
[4096];
102 static struct sockaddr_nl addr
;
104 static char *os_name
= "";
105 static char *os_major
= "";
106 static char *os_minor
= "";
107 static char *processor_arch
;
108 static char *os_build
;
109 static char *lic_version
;
110 static struct utsname uts_buf
;
112 void kvp_get_os_info(void)
118 os_build
= uts_buf
.release
;
119 processor_arch
= uts_buf
.machine
;
122 * The current windows host (win7) expects the build
123 * string to be of the form: x.y.z
124 * Strip additional information we may have.
126 p
= strchr(os_build
, '-');
130 file
= fopen("/etc/SuSE-release", "r");
132 goto kvp_osinfo_found
;
133 file
= fopen("/etc/redhat-release", "r");
135 goto kvp_osinfo_found
;
137 * Add code for other supported platforms.
141 * We don't have information about the os.
143 os_name
= uts_buf
.sysname
;
147 /* up to three lines */
148 p
= fgets(buf
, sizeof(buf
), file
);
150 p
= strchr(buf
, '\n');
159 p
= fgets(buf
, sizeof(buf
), file
);
161 p
= strchr(buf
, '\n');
170 p
= fgets(buf
, sizeof(buf
), file
);
172 p
= strchr(buf
, '\n');
188 kvp_get_ip_address(int family
, char *buffer
, int length
)
190 struct ifaddrs
*ifap
;
191 struct ifaddrs
*curp
;
192 int ipv4_len
= strlen("255.255.255.255") + 1;
193 int ipv6_len
= strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
200 * On entry into this function, the buffer is capable of holding the
201 * maximum key value (2048 bytes).
204 if (getifaddrs(&ifap
)) {
205 strcpy(buffer
, "getifaddrs failed\n");
210 while (curp
!= NULL
) {
211 if ((curp
->ifa_addr
!= NULL
) &&
212 (curp
->ifa_addr
->sa_family
== family
)) {
213 if (family
== AF_INET
) {
214 struct sockaddr_in
*addr
=
215 (struct sockaddr_in
*) curp
->ifa_addr
;
217 str
= inet_ntop(family
, &addr
->sin_addr
,
220 strcpy(buffer
, "inet_ntop failed\n");
230 offset
+= strlen(str
) + 1;
231 if ((length
- offset
) < (ipv4_len
+ 1))
237 * We only support AF_INET and AF_INET6
238 * and the list of addresses is separated by a ";".
240 struct sockaddr_in6
*addr
=
241 (struct sockaddr_in6
*) curp
->ifa_addr
;
243 str
= inet_ntop(family
,
244 &addr
->sin6_addr
.s6_addr
,
247 strcpy(buffer
, "inet_ntop failed\n");
256 offset
+= strlen(str
) + 1;
257 if ((length
- offset
) < (ipv6_len
+ 1))
263 curp
= curp
->ifa_next
;
273 kvp_get_domain_name(char *buffer
, int length
)
275 struct addrinfo hints
, *info
;
278 gethostname(buffer
, length
);
279 memset(&hints
, 0, sizeof(hints
));
280 hints
.ai_family
= AF_INET
; /*Get only ipv4 addrinfo. */
281 hints
.ai_socktype
= SOCK_STREAM
;
282 hints
.ai_flags
= AI_CANONNAME
;
284 error
= getaddrinfo(buffer
, NULL
, &hints
, &info
);
286 strcpy(buffer
, "getaddrinfo failed\n");
289 strcpy(buffer
, info
->ai_canonname
);
295 netlink_send(int fd
, struct cn_msg
*msg
)
297 struct nlmsghdr
*nlh
;
299 struct msghdr message
;
303 size
= NLMSG_SPACE(sizeof(struct cn_msg
) + msg
->len
);
305 nlh
= (struct nlmsghdr
*)buffer
;
307 nlh
->nlmsg_pid
= getpid();
308 nlh
->nlmsg_type
= NLMSG_DONE
;
309 nlh
->nlmsg_len
= NLMSG_LENGTH(size
- sizeof(*nlh
));
310 nlh
->nlmsg_flags
= 0;
312 iov
[0].iov_base
= nlh
;
313 iov
[0].iov_len
= sizeof(*nlh
);
315 iov
[1].iov_base
= msg
;
316 iov
[1].iov_len
= size
;
318 memset(&message
, 0, sizeof(message
));
319 message
.msg_name
= &addr
;
320 message
.msg_namelen
= sizeof(addr
);
321 message
.msg_iov
= iov
;
322 message
.msg_iovlen
= 2;
324 return sendmsg(fd
, &message
, 0);
329 int fd
, len
, sock_opt
;
331 struct cn_msg
*message
;
333 struct nlmsghdr
*incoming_msg
;
334 struct cn_msg
*incoming_cn_msg
;
335 struct hv_ku_msg
*hv_msg
;
341 openlog("KVP", 0, LOG_USER
);
342 syslog(LOG_INFO
, "KVP starting; pid is:%d", getpid());
344 * Retrieve OS release information.
348 fd
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_CONNECTOR
);
350 syslog(LOG_ERR
, "netlink socket creation failed; error:%d", fd
);
353 addr
.nl_family
= AF_NETLINK
;
356 addr
.nl_groups
= CN_KVP_IDX
;
359 error
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
361 syslog(LOG_ERR
, "bind failed; error:%d", error
);
365 sock_opt
= addr
.nl_groups
;
366 setsockopt(fd
, 270, 1, &sock_opt
, sizeof(sock_opt
));
368 * Register ourselves with the kernel.
370 message
= (struct cn_msg
*)kvp_send_buffer
;
371 message
->id
.idx
= CN_KVP_IDX
;
372 message
->id
.val
= CN_KVP_VAL
;
373 message
->seq
= KVP_REGISTER
;
377 len
= netlink_send(fd
, message
);
379 syslog(LOG_ERR
, "netlink_send failed; error:%d", len
);
391 len
= recv(fd
, kvp_recv_buffer
, sizeof(kvp_recv_buffer
), 0);
394 syslog(LOG_ERR
, "recv failed; error:%d", len
);
399 incoming_msg
= (struct nlmsghdr
*)kvp_recv_buffer
;
400 incoming_cn_msg
= (struct cn_msg
*)NLMSG_DATA(incoming_msg
);
402 switch (incoming_cn_msg
->seq
) {
405 * Driver is registering with us; stash away the version
408 p
= (char *)incoming_cn_msg
->data
;
409 lic_version
= malloc(strlen(p
) + 1);
411 strcpy(lic_version
, p
);
412 syslog(LOG_INFO
, "KVP LIC Version: %s",
415 syslog(LOG_ERR
, "malloc failed");
425 hv_msg
= (struct hv_ku_msg
*)incoming_cn_msg
->data
;
426 key_name
= (char *)hv_msg
->kvp_key
;
427 key_value
= (char *)hv_msg
->kvp_value
;
429 switch (hv_msg
->kvp_index
) {
430 case FullyQualifiedDomainName
:
431 kvp_get_domain_name(key_value
,
432 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
433 strcpy(key_name
, "FullyQualifiedDomainName");
435 case IntegrationServicesVersion
:
436 strcpy(key_name
, "IntegrationServicesVersion");
437 strcpy(key_value
, lic_version
);
439 case NetworkAddressIPv4
:
440 kvp_get_ip_address(AF_INET
, key_value
,
441 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
442 strcpy(key_name
, "NetworkAddressIPv4");
444 case NetworkAddressIPv6
:
445 kvp_get_ip_address(AF_INET6
, key_value
,
446 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
447 strcpy(key_name
, "NetworkAddressIPv6");
450 strcpy(key_value
, os_build
);
451 strcpy(key_name
, "OSBuildNumber");
454 strcpy(key_value
, os_name
);
455 strcpy(key_name
, "OSName");
458 strcpy(key_value
, os_major
);
459 strcpy(key_name
, "OSMajorVersion");
462 strcpy(key_value
, os_minor
);
463 strcpy(key_name
, "OSMinorVersion");
466 strcpy(key_value
, os_build
);
467 strcpy(key_name
, "OSVersion");
469 case ProcessorArchitecture
:
470 strcpy(key_value
, processor_arch
);
471 strcpy(key_name
, "ProcessorArchitecture");
474 strcpy(key_value
, "Unknown Key");
476 * We use a null key name to terminate enumeration.
478 strcpy(key_name
, "");
482 * Send the value back to the kernel. The response is
483 * already in the receive buffer. Update the cn_msg header to
484 * reflect the key value that has been added to the message
487 incoming_cn_msg
->id
.idx
= CN_KVP_IDX
;
488 incoming_cn_msg
->id
.val
= CN_KVP_VAL
;
489 incoming_cn_msg
->seq
= KVP_USER_SET
;
490 incoming_cn_msg
->ack
= 0;
491 incoming_cn_msg
->len
= sizeof(struct hv_ku_msg
);
493 len
= netlink_send(fd
, incoming_cn_msg
);
495 syslog(LOG_ERR
, "net_link send failed; error:%d", len
);