diff options
Diffstat (limited to 'drivers/staging/hv/tools')
| -rw-r--r-- | drivers/staging/hv/tools/hv_kvp_daemon.c | 493 |
1 files changed, 493 insertions, 0 deletions
diff --git a/drivers/staging/hv/tools/hv_kvp_daemon.c b/drivers/staging/hv/tools/hv_kvp_daemon.c new file mode 100644 index 00000000000..a4a407f7052 --- /dev/null +++ b/drivers/staging/hv/tools/hv_kvp_daemon.c | |||
| @@ -0,0 +1,493 @@ | |||
| 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 <errno.h> | ||
| 35 | #include <arpa/inet.h> | ||
| 36 | #include <linux/connector.h> | ||
| 37 | #include <linux/netlink.h> | ||
| 38 | #include <ifaddrs.h> | ||
| 39 | #include <netdb.h> | ||
| 40 | #include <syslog.h> | ||
| 41 | |||
| 42 | /* | ||
| 43 | * KYS: TODO. Need to register these in the kernel. | ||
| 44 | * | ||
| 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. | ||
| 48 | */ | ||
| 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 */ | ||
| 52 | |||
| 53 | /* | ||
| 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. | ||
| 59 | * | ||
| 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. | ||
| 62 | * | ||
| 63 | * XXXKYS: Have a shared header file between the user and kernel (TODO) | ||
| 64 | */ | ||
| 65 | |||
| 66 | enum kvp_op { | ||
| 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*/ | ||
| 72 | }; | ||
| 73 | |||
| 74 | #define HV_KVP_EXCHANGE_MAX_KEY_SIZE 512 | ||
| 75 | #define HV_KVP_EXCHANGE_MAX_VALUE_SIZE 2048 | ||
| 76 | |||
| 77 | struct hv_ku_msg { | ||
| 78 | __u32 kvp_index; | ||
| 79 | __u8 kvp_key[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; /* Key name */ | ||
| 80 | __u8 kvp_value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; /* Key value */ | ||
| 81 | }; | ||
| 82 | |||
| 83 | enum key_index { | ||
| 84 | FullyQualifiedDomainName = 0, | ||
| 85 | IntegrationServicesVersion, /*This key is serviced in the kernel*/ | ||
| 86 | NetworkAddressIPv4, | ||
| 87 | NetworkAddressIPv6, | ||
| 88 | OSBuildNumber, | ||
| 89 | OSName, | ||
| 90 | OSMajorVersion, | ||
| 91 | OSMinorVersion, | ||
| 92 | OSVersion, | ||
| 93 | ProcessorArchitecture | ||
| 94 | }; | ||
| 95 | |||
| 96 | /* | ||
| 97 | * End of shared definitions. | ||
| 98 | */ | ||
| 99 | |||
| 100 | static char kvp_send_buffer[4096]; | ||
| 101 | static char kvp_recv_buffer[4096]; | ||
| 102 | static struct sockaddr_nl addr; | ||
| 103 | |||
| 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; | ||
| 111 | |||
| 112 | void kvp_get_os_info(void) | ||
| 113 | { | ||
| 114 | FILE *file; | ||
| 115 | char *p, buf[512]; | ||
| 116 | |||
| 117 | uname(&uts_buf); | ||
| 118 | os_build = uts_buf.release; | ||
| 119 | processor_arch= uts_buf.machine; | ||
| 120 | |||
| 121 | file = fopen("/etc/SuSE-release", "r"); | ||
| 122 | if (file != NULL) | ||
| 123 | goto kvp_osinfo_found; | ||
| 124 | file = fopen("/etc/redhat-release", "r"); | ||
| 125 | if (file != NULL) | ||
| 126 | goto kvp_osinfo_found; | ||
| 127 | /* | ||
| 128 | * Add code for other supported platforms. | ||
| 129 | */ | ||
| 130 | |||
| 131 | /* | ||
| 132 | * We don't have information about the os. | ||
| 133 | */ | ||
| 134 | os_name = uts_buf.sysname; | ||
| 135 | return; | ||
| 136 | |||
| 137 | kvp_osinfo_found: | ||
| 138 | /* up to three lines */ | ||
| 139 | p = fgets(buf, sizeof(buf), file); | ||
| 140 | if (p) { | ||
| 141 | p = strchr(buf, '\n'); | ||
| 142 | if (p) | ||
| 143 | *p = '\0'; | ||
| 144 | p = strdup(buf); | ||
| 145 | if (!p) | ||
| 146 | goto done; | ||
| 147 | os_name = p; | ||
| 148 | |||
| 149 | /* second line */ | ||
| 150 | p = fgets(buf, sizeof(buf), file); | ||
| 151 | if (p) { | ||
| 152 | p = strchr(buf, '\n'); | ||
| 153 | if (p) | ||
| 154 | *p = '\0'; | ||
| 155 | p = strdup(buf); | ||
| 156 | if (!p) | ||
| 157 | goto done; | ||
| 158 | os_major = p; | ||
| 159 | |||
| 160 | /* third line */ | ||
| 161 | p = fgets(buf, sizeof(buf), file); | ||
| 162 | if (p) { | ||
| 163 | p = strchr(buf, '\n'); | ||
| 164 | if (p) | ||
| 165 | *p = '\0'; | ||
| 166 | p = strdup(buf); | ||
| 167 | if (p) | ||
| 168 | os_minor = p; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | done: | ||
| 174 | fclose(file); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 178 | static int | ||
| 179 | kvp_get_ip_address(int family, char *buffer, int length) | ||
| 180 | { | ||
| 181 | struct ifaddrs *ifap; | ||
| 182 | struct ifaddrs *curp; | ||
| 183 | int ipv4_len = strlen("255.255.255.255") + 1; | ||
| 184 | int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1; | ||
| 185 | int offset = 0; | ||
| 186 | const char *str; | ||
| 187 | char tmp[50]; | ||
| 188 | int error = 0; | ||
| 189 | |||
| 190 | /* | ||
| 191 | * On entry into this function, the buffer is capable of holding the | ||
| 192 | * maximum key value (2048 bytes). | ||
| 193 | */ | ||
| 194 | |||
| 195 | if (getifaddrs(&ifap)) { | ||
| 196 | strcpy(buffer, "getifaddrs failed\n"); | ||
| 197 | return 1; | ||
| 198 | } | ||
| 199 | |||
| 200 | curp = ifap; | ||
| 201 | while (curp != NULL) { | ||
| 202 | if ((curp->ifa_addr != NULL) && | ||
| 203 | (curp->ifa_addr->sa_family == family)) { | ||
| 204 | if (family == AF_INET) { | ||
| 205 | struct sockaddr_in *addr = | ||
| 206 | (struct sockaddr_in *) curp->ifa_addr; | ||
| 207 | |||
| 208 | str = inet_ntop(family, &addr->sin_addr, | ||
| 209 | tmp, 50); | ||
| 210 | if (str == NULL) { | ||
| 211 | strcpy(buffer, "inet_ntop failed\n"); | ||
| 212 | error = 1; | ||
| 213 | goto getaddr_done; | ||
| 214 | } | ||
| 215 | if (offset == 0) | ||
| 216 | strcpy(buffer, tmp); | ||
| 217 | else | ||
| 218 | strcat(buffer, tmp); | ||
| 219 | strcat(buffer, ";"); | ||
| 220 | |||
| 221 | offset += strlen(str) + 1; | ||
| 222 | if ((length - offset) < (ipv4_len + 1)) | ||
| 223 | goto getaddr_done; | ||
| 224 | |||
| 225 | } else { | ||
| 226 | |||
| 227 | /* | ||
| 228 | * We only support AF_INET and AF_INET6 | ||
| 229 | * and the list of addresses is separated by a ";". | ||
| 230 | */ | ||
| 231 | struct sockaddr_in6 *addr = | ||
| 232 | (struct sockaddr_in6 *) curp->ifa_addr; | ||
| 233 | |||
| 234 | str = inet_ntop(family, | ||
| 235 | &addr->sin6_addr.s6_addr, | ||
| 236 | tmp, 50); | ||
| 237 | if (str == NULL) { | ||
| 238 | strcpy(buffer, "inet_ntop failed\n"); | ||
| 239 | error = 1; | ||
| 240 | goto getaddr_done; | ||
| 241 | } | ||
| 242 | if (offset == 0) | ||
| 243 | strcpy(buffer, tmp); | ||
| 244 | else | ||
| 245 | strcat(buffer, tmp); | ||
| 246 | strcat(buffer, ";"); | ||
| 247 | offset += strlen(str) + 1; | ||
| 248 | if ((length - offset) < (ipv6_len + 1)) | ||
| 249 | goto getaddr_done; | ||
| 250 | |||
| 251 | } | ||
| 252 | |||
| 253 | } | ||
| 254 | curp = curp->ifa_next; | ||
| 255 | } | ||
| 256 | |||
| 257 | getaddr_done: | ||
| 258 | freeifaddrs(ifap); | ||
| 259 | return error; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | static int | ||
| 264 | kvp_get_domain_name(char *buffer, int length) | ||
| 265 | { | ||
| 266 | struct addrinfo hints, *info ; | ||
| 267 | gethostname(buffer, length); | ||
| 268 | int error = 0; | ||
| 269 | |||
| 270 | memset(&hints, 0, sizeof(hints)); | ||
| 271 | hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */ | ||
| 272 | hints.ai_socktype = SOCK_STREAM; | ||
| 273 | hints.ai_flags = AI_CANONNAME; | ||
| 274 | |||
| 275 | error = getaddrinfo(buffer, "http", &hints, &info); | ||
| 276 | if (error != 0) { | ||
| 277 | strcpy(buffer, "getaddrinfo failed\n"); | ||
| 278 | error = 1; | ||
| 279 | goto get_domain_done; | ||
| 280 | } | ||
| 281 | strcpy(buffer, info->ai_canonname); | ||
| 282 | get_domain_done: | ||
| 283 | freeaddrinfo(info); | ||
| 284 | return error; | ||
| 285 | } | ||
| 286 | |||
| 287 | static int | ||
| 288 | netlink_send(int fd, struct cn_msg *msg) | ||
| 289 | { | ||
| 290 | struct nlmsghdr *nlh; | ||
| 291 | unsigned int size; | ||
| 292 | struct msghdr message; | ||
| 293 | char buffer[64]; | ||
| 294 | struct iovec iov[2]; | ||
| 295 | |||
| 296 | size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len); | ||
| 297 | |||
| 298 | nlh = (struct nlmsghdr *)buffer; | ||
| 299 | nlh->nlmsg_seq = 0; | ||
| 300 | nlh->nlmsg_pid = getpid(); | ||
| 301 | nlh->nlmsg_type = NLMSG_DONE; | ||
| 302 | nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh)); | ||
| 303 | nlh->nlmsg_flags = 0; | ||
| 304 | |||
| 305 | iov[0].iov_base = nlh; | ||
| 306 | iov[0].iov_len = sizeof(*nlh); | ||
| 307 | |||
| 308 | iov[1].iov_base = msg; | ||
| 309 | iov[1].iov_len = size; | ||
| 310 | |||
| 311 | memset(&message, 0, sizeof(message)); | ||
| 312 | message.msg_name = &addr; | ||
| 313 | message.msg_namelen = sizeof(addr); | ||
| 314 | message.msg_iov = iov; | ||
| 315 | message.msg_iovlen = 2; | ||
| 316 | |||
| 317 | return sendmsg(fd, &message, 0); | ||
| 318 | } | ||
| 319 | |||
| 320 | int main(void) | ||
| 321 | { | ||
| 322 | int fd, len, sock_opt; | ||
| 323 | int error; | ||
| 324 | struct cn_msg *message; | ||
| 325 | struct pollfd pfd; | ||
| 326 | struct nlmsghdr *incoming_msg; | ||
| 327 | struct cn_msg *incoming_cn_msg; | ||
| 328 | struct hv_ku_msg *hv_msg; | ||
| 329 | char *p; | ||
| 330 | char *key_value; | ||
| 331 | char *key_name; | ||
| 332 | |||
| 333 | daemon(1, 0); | ||
| 334 | openlog("KVP", 0, LOG_USER); | ||
| 335 | syslog(LOG_INFO, "KVP starting; pid is:%d", getpid()); | ||
| 336 | /* | ||
| 337 | * Retrieve OS release information. | ||
| 338 | */ | ||
| 339 | kvp_get_os_info(); | ||
| 340 | |||
| 341 | fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); | ||
| 342 | if (fd < 0) { | ||
| 343 | syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd); | ||
| 344 | exit(-1); | ||
| 345 | } | ||
| 346 | addr.nl_family = AF_NETLINK; | ||
| 347 | addr.nl_pad = 0; | ||
| 348 | addr.nl_pid = 0; | ||
| 349 | addr.nl_groups = CN_KVP_IDX; | ||
| 350 | |||
| 351 | |||
| 352 | error = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); | ||
| 353 | if (error < 0) { | ||
| 354 | syslog(LOG_ERR, "bind failed; error:%d", error); | ||
| 355 | close(fd); | ||
| 356 | exit(-1); | ||
| 357 | } | ||
| 358 | sock_opt = addr.nl_groups; | ||
| 359 | setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt)); | ||
| 360 | /* | ||
| 361 | * Register ourselves with the kernel. | ||
| 362 | */ | ||
| 363 | message = (struct cn_msg *)kvp_send_buffer; | ||
| 364 | message->id.idx = CN_KVP_IDX; | ||
| 365 | message->id.val = CN_KVP_VAL; | ||
| 366 | message->seq = KVP_REGISTER; | ||
| 367 | message->ack = 0; | ||
| 368 | message->len = 0; | ||
| 369 | |||
| 370 | len = netlink_send(fd, message); | ||
| 371 | if (len < 0) { | ||
| 372 | syslog(LOG_ERR, "netlink_send failed; error:%d", len); | ||
| 373 | close(fd); | ||
| 374 | exit(-1); | ||
| 375 | } | ||
| 376 | |||
| 377 | pfd.fd = fd; | ||
| 378 | |||
| 379 | while (1) { | ||
| 380 | pfd.events = POLLIN; | ||
| 381 | pfd.revents = 0; | ||
| 382 | poll(&pfd, 1, -1); | ||
| 383 | |||
| 384 | len = recv(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0); | ||
| 385 | |||
| 386 | if (len < 0) { | ||
| 387 | syslog(LOG_ERR, "recv failed; error:%d", len); | ||
| 388 | close(fd); | ||
| 389 | return -1; | ||
| 390 | } | ||
| 391 | |||
| 392 | incoming_msg = (struct nlmsghdr *)kvp_recv_buffer; | ||
| 393 | incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg); | ||
| 394 | |||
| 395 | switch (incoming_cn_msg->seq) { | ||
| 396 | case KVP_REGISTER: | ||
| 397 | /* | ||
| 398 | * Driver is registering with us; stash away the version | ||
| 399 | * information. | ||
| 400 | */ | ||
| 401 | p = (char *)incoming_cn_msg->data; | ||
| 402 | lic_version = malloc(strlen(p) + 1); | ||
| 403 | if (lic_version) { | ||
| 404 | strcpy(lic_version, p); | ||
| 405 | syslog(LOG_INFO, "KVP LIC Version: %s", | ||
| 406 | lic_version); | ||
| 407 | } else { | ||
| 408 | syslog(LOG_ERR, "malloc failed"); | ||
| 409 | } | ||
| 410 | continue; | ||
| 411 | |||
| 412 | case KVP_KERNEL_GET: | ||
| 413 | break; | ||
| 414 | default: | ||
| 415 | continue; | ||
| 416 | } | ||
| 417 | |||
| 418 | hv_msg = (struct hv_ku_msg *)incoming_cn_msg->data; | ||
| 419 | key_name = (char *)hv_msg->kvp_key; | ||
| 420 | key_value = (char *)hv_msg->kvp_value; | ||
| 421 | |||
| 422 | switch (hv_msg->kvp_index) { | ||
| 423 | case FullyQualifiedDomainName: | ||
| 424 | kvp_get_domain_name(key_value, | ||
| 425 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE); | ||
| 426 | strcpy(key_name, "FullyQualifiedDomainName"); | ||
| 427 | break; | ||
| 428 | case IntegrationServicesVersion: | ||
| 429 | strcpy(key_name, "IntegrationServicesVersion"); | ||
| 430 | strcpy(key_value, lic_version); | ||
| 431 | break; | ||
| 432 | case NetworkAddressIPv4: | ||
| 433 | kvp_get_ip_address(AF_INET, key_value, | ||
| 434 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE); | ||
| 435 | strcpy(key_name, "NetworkAddressIPv4"); | ||
| 436 | break; | ||
| 437 | case NetworkAddressIPv6: | ||
| 438 | kvp_get_ip_address(AF_INET6, key_value, | ||
| 439 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE); | ||
| 440 | strcpy(key_name, "NetworkAddressIPv6"); | ||
| 441 | break; | ||
| 442 | case OSBuildNumber: | ||
| 443 | strcpy(key_value, os_build); | ||
| 444 | strcpy(key_name, "OSBuildNumber"); | ||
| 445 | break; | ||
| 446 | case OSName: | ||
| 447 | strcpy(key_value, os_name); | ||
| 448 | strcpy(key_name, "OSName"); | ||
| 449 | break; | ||
| 450 | case OSMajorVersion: | ||
| 451 | strcpy(key_value, os_major); | ||
| 452 | strcpy(key_name, "OSMajorVersion"); | ||
| 453 | break; | ||
| 454 | case OSMinorVersion: | ||
| 455 | strcpy(key_value, os_minor); | ||
| 456 | strcpy(key_name, "OSMinorVersion"); | ||
| 457 | break; | ||
| 458 | case OSVersion: | ||
| 459 | strcpy(key_value, os_build); | ||
| 460 | strcpy(key_name, "OSVersion"); | ||
| 461 | break; | ||
| 462 | case ProcessorArchitecture: | ||
| 463 | strcpy(key_value, processor_arch); | ||
| 464 | strcpy(key_name, "ProcessorArchitecture"); | ||
| 465 | break; | ||
| 466 | default: | ||
| 467 | strcpy(key_value, "Unknown Key"); | ||
| 468 | /* | ||
| 469 | * We use a null key name to terminate enumeration. | ||
| 470 | */ | ||
| 471 | strcpy(key_name, ""); | ||
| 472 | break; | ||
| 473 | } | ||
| 474 | /* | ||
| 475 | * Send the value back to the kernel. The response is | ||
| 476 | * already in the receive buffer. Update the cn_msg header to | ||
| 477 | * reflect the key value that has been added to the message | ||
| 478 | */ | ||
| 479 | |||
| 480 | incoming_cn_msg->id.idx = CN_KVP_IDX; | ||
| 481 | incoming_cn_msg->id.val = CN_KVP_VAL; | ||
| 482 | incoming_cn_msg->seq = KVP_USER_SET; | ||
| 483 | incoming_cn_msg->ack = 0; | ||
| 484 | incoming_cn_msg->len = sizeof(struct hv_ku_msg); | ||
| 485 | |||
| 486 | len = netlink_send(fd, incoming_cn_msg); | ||
| 487 | if (len < 0) { | ||
| 488 | syslog(LOG_ERR, "net_link send failed; error:%d", len); | ||
| 489 | exit(-1); | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | } | ||
