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/hyperv.h>
38 #include <linux/netlink.h>
45 * KVP protocol: The user mode component first registers with the
46 * the kernel component. Subsequently, the kernel component requests, data
47 * for the specified keys. In response to this message the user mode component
48 * fills in the value corresponding to the specified key. We overload the
49 * sequence field in the cn_msg header to define our KVP message types.
51 * We use this infrastructure for also supporting queries from user mode
52 * application for state that may be maintained in the KVP kernel component.
58 FullyQualifiedDomainName
= 0,
59 IntegrationServicesVersion
, /*This key is serviced in the kernel*/
70 static char kvp_send_buffer
[4096];
71 static char kvp_recv_buffer
[4096];
72 static struct sockaddr_nl addr
;
74 static char *os_name
= "";
75 static char *os_major
= "";
76 static char *os_minor
= "";
77 static char *processor_arch
;
78 static char *os_build
;
79 static char *lic_version
;
80 static struct utsname uts_buf
;
82 void kvp_get_os_info(void)
88 os_build
= uts_buf
.release
;
89 processor_arch
= uts_buf
.machine
;
92 * The current windows host (win7) expects the build
93 * string to be of the form: x.y.z
94 * Strip additional information we may have.
96 p
= strchr(os_build
, '-');
100 file
= fopen("/etc/SuSE-release", "r");
102 goto kvp_osinfo_found
;
103 file
= fopen("/etc/redhat-release", "r");
105 goto kvp_osinfo_found
;
107 * Add code for other supported platforms.
111 * We don't have information about the os.
113 os_name
= uts_buf
.sysname
;
117 /* up to three lines */
118 p
= fgets(buf
, sizeof(buf
), file
);
120 p
= strchr(buf
, '\n');
129 p
= fgets(buf
, sizeof(buf
), file
);
131 p
= strchr(buf
, '\n');
140 p
= fgets(buf
, sizeof(buf
), file
);
142 p
= strchr(buf
, '\n');
158 kvp_get_ip_address(int family
, char *buffer
, int length
)
160 struct ifaddrs
*ifap
;
161 struct ifaddrs
*curp
;
162 int ipv4_len
= strlen("255.255.255.255") + 1;
163 int ipv6_len
= strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
170 * On entry into this function, the buffer is capable of holding the
171 * maximum key value (2048 bytes).
174 if (getifaddrs(&ifap
)) {
175 strcpy(buffer
, "getifaddrs failed\n");
180 while (curp
!= NULL
) {
181 if ((curp
->ifa_addr
!= NULL
) &&
182 (curp
->ifa_addr
->sa_family
== family
)) {
183 if (family
== AF_INET
) {
184 struct sockaddr_in
*addr
=
185 (struct sockaddr_in
*) curp
->ifa_addr
;
187 str
= inet_ntop(family
, &addr
->sin_addr
,
190 strcpy(buffer
, "inet_ntop failed\n");
200 offset
+= strlen(str
) + 1;
201 if ((length
- offset
) < (ipv4_len
+ 1))
207 * We only support AF_INET and AF_INET6
208 * and the list of addresses is separated by a ";".
210 struct sockaddr_in6
*addr
=
211 (struct sockaddr_in6
*) curp
->ifa_addr
;
213 str
= inet_ntop(family
,
214 &addr
->sin6_addr
.s6_addr
,
217 strcpy(buffer
, "inet_ntop failed\n");
226 offset
+= strlen(str
) + 1;
227 if ((length
- offset
) < (ipv6_len
+ 1))
233 curp
= curp
->ifa_next
;
243 kvp_get_domain_name(char *buffer
, int length
)
245 struct addrinfo hints
, *info
;
248 gethostname(buffer
, length
);
249 memset(&hints
, 0, sizeof(hints
));
250 hints
.ai_family
= AF_INET
; /*Get only ipv4 addrinfo. */
251 hints
.ai_socktype
= SOCK_STREAM
;
252 hints
.ai_flags
= AI_CANONNAME
;
254 error
= getaddrinfo(buffer
, NULL
, &hints
, &info
);
256 strcpy(buffer
, "getaddrinfo failed\n");
259 strcpy(buffer
, info
->ai_canonname
);
265 netlink_send(int fd
, struct cn_msg
*msg
)
267 struct nlmsghdr
*nlh
;
269 struct msghdr message
;
273 size
= NLMSG_SPACE(sizeof(struct cn_msg
) + msg
->len
);
275 nlh
= (struct nlmsghdr
*)buffer
;
277 nlh
->nlmsg_pid
= getpid();
278 nlh
->nlmsg_type
= NLMSG_DONE
;
279 nlh
->nlmsg_len
= NLMSG_LENGTH(size
- sizeof(*nlh
));
280 nlh
->nlmsg_flags
= 0;
282 iov
[0].iov_base
= nlh
;
283 iov
[0].iov_len
= sizeof(*nlh
);
285 iov
[1].iov_base
= msg
;
286 iov
[1].iov_len
= size
;
288 memset(&message
, 0, sizeof(message
));
289 message
.msg_name
= &addr
;
290 message
.msg_namelen
= sizeof(addr
);
291 message
.msg_iov
= iov
;
292 message
.msg_iovlen
= 2;
294 return sendmsg(fd
, &message
, 0);
299 int fd
, len
, sock_opt
;
301 struct cn_msg
*message
;
303 struct nlmsghdr
*incoming_msg
;
304 struct cn_msg
*incoming_cn_msg
;
305 struct hv_kvp_msg
*hv_msg
;
311 openlog("KVP", 0, LOG_USER
);
312 syslog(LOG_INFO
, "KVP starting; pid is:%d", getpid());
314 * Retrieve OS release information.
318 fd
= socket(AF_NETLINK
, SOCK_DGRAM
, NETLINK_CONNECTOR
);
320 syslog(LOG_ERR
, "netlink socket creation failed; error:%d", fd
);
323 addr
.nl_family
= AF_NETLINK
;
326 addr
.nl_groups
= CN_KVP_IDX
;
329 error
= bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
331 syslog(LOG_ERR
, "bind failed; error:%d", error
);
335 sock_opt
= addr
.nl_groups
;
336 setsockopt(fd
, 270, 1, &sock_opt
, sizeof(sock_opt
));
338 * Register ourselves with the kernel.
340 message
= (struct cn_msg
*)kvp_send_buffer
;
341 message
->id
.idx
= CN_KVP_IDX
;
342 message
->id
.val
= CN_KVP_VAL
;
344 hv_msg
= (struct hv_kvp_msg
*)message
->data
;
345 hv_msg
->kvp_hdr
.operation
= KVP_OP_REGISTER
;
347 message
->len
= sizeof(struct hv_kvp_msg
);
349 len
= netlink_send(fd
, message
);
351 syslog(LOG_ERR
, "netlink_send failed; error:%d", len
);
363 len
= recv(fd
, kvp_recv_buffer
, sizeof(kvp_recv_buffer
), 0);
366 syslog(LOG_ERR
, "recv failed; error:%d", len
);
371 incoming_msg
= (struct nlmsghdr
*)kvp_recv_buffer
;
372 incoming_cn_msg
= (struct cn_msg
*)NLMSG_DATA(incoming_msg
);
373 hv_msg
= (struct hv_kvp_msg
*)incoming_cn_msg
->data
;
375 switch (hv_msg
->kvp_hdr
.operation
) {
376 case KVP_OP_REGISTER
:
378 * Driver is registering with us; stash away the version
381 p
= (char *)hv_msg
->body
.kvp_version
;
382 lic_version
= malloc(strlen(p
) + 1);
384 strcpy(lic_version
, p
);
385 syslog(LOG_INFO
, "KVP LIC Version: %s",
388 syslog(LOG_ERR
, "malloc failed");
396 hv_msg
= (struct hv_kvp_msg
*)incoming_cn_msg
->data
;
397 key_name
= (char *)hv_msg
->body
.kvp_enum_data
.data
.key
;
398 key_value
= (char *)hv_msg
->body
.kvp_enum_data
.data
.value
;
400 switch (hv_msg
->body
.kvp_enum_data
.index
) {
401 case FullyQualifiedDomainName
:
402 kvp_get_domain_name(key_value
,
403 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
404 strcpy(key_name
, "FullyQualifiedDomainName");
406 case IntegrationServicesVersion
:
407 strcpy(key_name
, "IntegrationServicesVersion");
408 strcpy(key_value
, lic_version
);
410 case NetworkAddressIPv4
:
411 kvp_get_ip_address(AF_INET
, key_value
,
412 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
413 strcpy(key_name
, "NetworkAddressIPv4");
415 case NetworkAddressIPv6
:
416 kvp_get_ip_address(AF_INET6
, key_value
,
417 HV_KVP_EXCHANGE_MAX_VALUE_SIZE
);
418 strcpy(key_name
, "NetworkAddressIPv6");
421 strcpy(key_value
, os_build
);
422 strcpy(key_name
, "OSBuildNumber");
425 strcpy(key_value
, os_name
);
426 strcpy(key_name
, "OSName");
429 strcpy(key_value
, os_major
);
430 strcpy(key_name
, "OSMajorVersion");
433 strcpy(key_value
, os_minor
);
434 strcpy(key_name
, "OSMinorVersion");
437 strcpy(key_value
, os_build
);
438 strcpy(key_name
, "OSVersion");
440 case ProcessorArchitecture
:
441 strcpy(key_value
, processor_arch
);
442 strcpy(key_name
, "ProcessorArchitecture");
445 strcpy(key_value
, "Unknown Key");
447 * We use a null key name to terminate enumeration.
449 strcpy(key_name
, "");
453 * Send the value back to the kernel. The response is
454 * already in the receive buffer. Update the cn_msg header to
455 * reflect the key value that has been added to the message
458 incoming_cn_msg
->id
.idx
= CN_KVP_IDX
;
459 incoming_cn_msg
->id
.val
= CN_KVP_VAL
;
460 incoming_cn_msg
->ack
= 0;
461 incoming_cn_msg
->len
= sizeof(struct hv_kvp_msg
);
463 len
= netlink_send(fd
, incoming_cn_msg
);
465 syslog(LOG_ERR
, "net_link send failed; error:%d", len
);