Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 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> |
K. Y. Srinivasan | eab6af7 | 2012-02-02 16:56:49 -0800 | [diff] [blame^] | 37 | #include <linux/hyperv.h> |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 38 | #include <linux/netlink.h> |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 39 | #include <ifaddrs.h> |
| 40 | #include <netdb.h> |
| 41 | #include <syslog.h> |
| 42 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 43 | |
| 44 | /* |
| 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. |
| 50 | * |
| 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. |
| 53 | * |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 54 | */ |
| 55 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 56 | |
| 57 | enum key_index { |
| 58 | FullyQualifiedDomainName = 0, |
| 59 | IntegrationServicesVersion, /*This key is serviced in the kernel*/ |
| 60 | NetworkAddressIPv4, |
| 61 | NetworkAddressIPv6, |
| 62 | OSBuildNumber, |
| 63 | OSName, |
| 64 | OSMajorVersion, |
| 65 | OSMinorVersion, |
| 66 | OSVersion, |
| 67 | ProcessorArchitecture |
| 68 | }; |
| 69 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 70 | static char kvp_send_buffer[4096]; |
| 71 | static char kvp_recv_buffer[4096]; |
| 72 | static struct sockaddr_nl addr; |
| 73 | |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 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; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 79 | static char *lic_version; |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 80 | static struct utsname uts_buf; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 81 | |
| 82 | void kvp_get_os_info(void) |
| 83 | { |
| 84 | FILE *file; |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 85 | char *p, buf[512]; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 86 | |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 87 | uname(&uts_buf); |
| 88 | os_build = uts_buf.release; |
K. Y. Srinivasan | 064931d | 2011-07-19 11:44:20 -0700 | [diff] [blame] | 89 | processor_arch = uts_buf.machine; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 90 | |
K. Y. Srinivasan | e54bbc6 | 2011-07-22 10:14:31 -0700 | [diff] [blame] | 91 | /* |
| 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. |
| 95 | */ |
| 96 | p = strchr(os_build, '-'); |
| 97 | if (p) |
| 98 | *p = '\0'; |
| 99 | |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 100 | file = fopen("/etc/SuSE-release", "r"); |
| 101 | if (file != NULL) |
| 102 | goto kvp_osinfo_found; |
| 103 | file = fopen("/etc/redhat-release", "r"); |
| 104 | if (file != NULL) |
| 105 | goto kvp_osinfo_found; |
| 106 | /* |
| 107 | * Add code for other supported platforms. |
| 108 | */ |
| 109 | |
| 110 | /* |
| 111 | * We don't have information about the os. |
| 112 | */ |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 113 | os_name = uts_buf.sysname; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 114 | return; |
| 115 | |
| 116 | kvp_osinfo_found: |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 117 | /* up to three lines */ |
| 118 | p = fgets(buf, sizeof(buf), file); |
| 119 | if (p) { |
| 120 | p = strchr(buf, '\n'); |
| 121 | if (p) |
| 122 | *p = '\0'; |
| 123 | p = strdup(buf); |
| 124 | if (!p) |
| 125 | goto done; |
| 126 | os_name = p; |
| 127 | |
| 128 | /* second line */ |
| 129 | p = fgets(buf, sizeof(buf), file); |
| 130 | if (p) { |
| 131 | p = strchr(buf, '\n'); |
| 132 | if (p) |
| 133 | *p = '\0'; |
| 134 | p = strdup(buf); |
| 135 | if (!p) |
| 136 | goto done; |
| 137 | os_major = p; |
| 138 | |
| 139 | /* third line */ |
| 140 | p = fgets(buf, sizeof(buf), file); |
| 141 | if (p) { |
| 142 | p = strchr(buf, '\n'); |
| 143 | if (p) |
| 144 | *p = '\0'; |
| 145 | p = strdup(buf); |
| 146 | if (p) |
| 147 | os_minor = p; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | done: |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 153 | fclose(file); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | static int |
| 158 | kvp_get_ip_address(int family, char *buffer, int length) |
| 159 | { |
| 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; |
| 164 | int offset = 0; |
| 165 | const char *str; |
| 166 | char tmp[50]; |
| 167 | int error = 0; |
| 168 | |
| 169 | /* |
| 170 | * On entry into this function, the buffer is capable of holding the |
| 171 | * maximum key value (2048 bytes). |
| 172 | */ |
| 173 | |
| 174 | if (getifaddrs(&ifap)) { |
| 175 | strcpy(buffer, "getifaddrs failed\n"); |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | curp = ifap; |
| 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; |
| 186 | |
| 187 | str = inet_ntop(family, &addr->sin_addr, |
| 188 | tmp, 50); |
| 189 | if (str == NULL) { |
| 190 | strcpy(buffer, "inet_ntop failed\n"); |
| 191 | error = 1; |
| 192 | goto getaddr_done; |
| 193 | } |
| 194 | if (offset == 0) |
| 195 | strcpy(buffer, tmp); |
| 196 | else |
| 197 | strcat(buffer, tmp); |
| 198 | strcat(buffer, ";"); |
| 199 | |
| 200 | offset += strlen(str) + 1; |
| 201 | if ((length - offset) < (ipv4_len + 1)) |
| 202 | goto getaddr_done; |
| 203 | |
| 204 | } else { |
| 205 | |
| 206 | /* |
| 207 | * We only support AF_INET and AF_INET6 |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 208 | * and the list of addresses is separated by a ";". |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 209 | */ |
| 210 | struct sockaddr_in6 *addr = |
| 211 | (struct sockaddr_in6 *) curp->ifa_addr; |
| 212 | |
| 213 | str = inet_ntop(family, |
| 214 | &addr->sin6_addr.s6_addr, |
| 215 | tmp, 50); |
| 216 | if (str == NULL) { |
| 217 | strcpy(buffer, "inet_ntop failed\n"); |
| 218 | error = 1; |
| 219 | goto getaddr_done; |
| 220 | } |
| 221 | if (offset == 0) |
| 222 | strcpy(buffer, tmp); |
| 223 | else |
| 224 | strcat(buffer, tmp); |
| 225 | strcat(buffer, ";"); |
| 226 | offset += strlen(str) + 1; |
| 227 | if ((length - offset) < (ipv6_len + 1)) |
| 228 | goto getaddr_done; |
| 229 | |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | curp = curp->ifa_next; |
| 234 | } |
| 235 | |
| 236 | getaddr_done: |
| 237 | freeifaddrs(ifap); |
| 238 | return error; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | static int |
| 243 | kvp_get_domain_name(char *buffer, int length) |
| 244 | { |
| 245 | struct addrinfo hints, *info ; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 246 | int error = 0; |
| 247 | |
K. Y. Srinivasan | 5be528c | 2011-07-26 11:03:10 -0700 | [diff] [blame] | 248 | gethostname(buffer, length); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 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; |
| 253 | |
K. Y. Srinivasan | 5be528c | 2011-07-26 11:03:10 -0700 | [diff] [blame] | 254 | error = getaddrinfo(buffer, NULL, &hints, &info); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 255 | if (error != 0) { |
| 256 | strcpy(buffer, "getaddrinfo failed\n"); |
K. Y. Srinivasan | 5be528c | 2011-07-26 11:03:10 -0700 | [diff] [blame] | 257 | return error; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 258 | } |
| 259 | strcpy(buffer, info->ai_canonname); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 260 | freeaddrinfo(info); |
| 261 | return error; |
| 262 | } |
| 263 | |
| 264 | static int |
| 265 | netlink_send(int fd, struct cn_msg *msg) |
| 266 | { |
| 267 | struct nlmsghdr *nlh; |
| 268 | unsigned int size; |
| 269 | struct msghdr message; |
| 270 | char buffer[64]; |
| 271 | struct iovec iov[2]; |
| 272 | |
| 273 | size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len); |
| 274 | |
| 275 | nlh = (struct nlmsghdr *)buffer; |
| 276 | nlh->nlmsg_seq = 0; |
| 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; |
| 281 | |
| 282 | iov[0].iov_base = nlh; |
| 283 | iov[0].iov_len = sizeof(*nlh); |
| 284 | |
| 285 | iov[1].iov_base = msg; |
| 286 | iov[1].iov_len = size; |
| 287 | |
| 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; |
| 293 | |
| 294 | return sendmsg(fd, &message, 0); |
| 295 | } |
| 296 | |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 297 | int main(void) |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 298 | { |
| 299 | int fd, len, sock_opt; |
| 300 | int error; |
| 301 | struct cn_msg *message; |
| 302 | struct pollfd pfd; |
| 303 | struct nlmsghdr *incoming_msg; |
| 304 | struct cn_msg *incoming_cn_msg; |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 305 | struct hv_ku_msg *hv_msg; |
| 306 | char *p; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 307 | char *key_value; |
| 308 | char *key_name; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 309 | |
| 310 | daemon(1, 0); |
| 311 | openlog("KVP", 0, LOG_USER); |
| 312 | syslog(LOG_INFO, "KVP starting; pid is:%d", getpid()); |
| 313 | /* |
| 314 | * Retrieve OS release information. |
| 315 | */ |
| 316 | kvp_get_os_info(); |
| 317 | |
| 318 | fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); |
| 319 | if (fd < 0) { |
| 320 | syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd); |
| 321 | exit(-1); |
| 322 | } |
| 323 | addr.nl_family = AF_NETLINK; |
| 324 | addr.nl_pad = 0; |
| 325 | addr.nl_pid = 0; |
| 326 | addr.nl_groups = CN_KVP_IDX; |
| 327 | |
| 328 | |
| 329 | error = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 330 | if (error < 0) { |
| 331 | syslog(LOG_ERR, "bind failed; error:%d", error); |
| 332 | close(fd); |
| 333 | exit(-1); |
| 334 | } |
| 335 | sock_opt = addr.nl_groups; |
| 336 | setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt)); |
| 337 | /* |
| 338 | * Register ourselves with the kernel. |
| 339 | */ |
| 340 | message = (struct cn_msg *)kvp_send_buffer; |
| 341 | message->id.idx = CN_KVP_IDX; |
| 342 | message->id.val = CN_KVP_VAL; |
| 343 | message->seq = KVP_REGISTER; |
| 344 | message->ack = 0; |
| 345 | message->len = 0; |
| 346 | |
| 347 | len = netlink_send(fd, message); |
| 348 | if (len < 0) { |
| 349 | syslog(LOG_ERR, "netlink_send failed; error:%d", len); |
| 350 | close(fd); |
| 351 | exit(-1); |
| 352 | } |
| 353 | |
| 354 | pfd.fd = fd; |
| 355 | |
| 356 | while (1) { |
| 357 | pfd.events = POLLIN; |
| 358 | pfd.revents = 0; |
| 359 | poll(&pfd, 1, -1); |
| 360 | |
| 361 | len = recv(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0); |
| 362 | |
| 363 | if (len < 0) { |
| 364 | syslog(LOG_ERR, "recv failed; error:%d", len); |
| 365 | close(fd); |
| 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | incoming_msg = (struct nlmsghdr *)kvp_recv_buffer; |
| 370 | incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg); |
| 371 | |
| 372 | switch (incoming_cn_msg->seq) { |
| 373 | case KVP_REGISTER: |
| 374 | /* |
| 375 | * Driver is registering with us; stash away the version |
| 376 | * information. |
| 377 | */ |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 378 | p = (char *)incoming_cn_msg->data; |
| 379 | lic_version = malloc(strlen(p) + 1); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 380 | if (lic_version) { |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 381 | strcpy(lic_version, p); |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 382 | syslog(LOG_INFO, "KVP LIC Version: %s", |
| 383 | lic_version); |
| 384 | } else { |
| 385 | syslog(LOG_ERR, "malloc failed"); |
| 386 | } |
| 387 | continue; |
| 388 | |
| 389 | case KVP_KERNEL_GET: |
| 390 | break; |
| 391 | default: |
| 392 | continue; |
| 393 | } |
| 394 | |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 395 | hv_msg = (struct hv_ku_msg *)incoming_cn_msg->data; |
| 396 | key_name = (char *)hv_msg->kvp_key; |
| 397 | key_value = (char *)hv_msg->kvp_value; |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 398 | |
Olaf Hering | 7989f7d | 2011-03-22 10:02:17 +0100 | [diff] [blame] | 399 | switch (hv_msg->kvp_index) { |
Ky Srinivasan | cc04acf | 2010-12-16 18:56:54 -0700 | [diff] [blame] | 400 | case FullyQualifiedDomainName: |
| 401 | kvp_get_domain_name(key_value, |
| 402 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE); |
| 403 | strcpy(key_name, "FullyQualifiedDomainName"); |
| 404 | break; |
| 405 | case IntegrationServicesVersion: |
| 406 | strcpy(key_name, "IntegrationServicesVersion"); |
| 407 | strcpy(key_value, lic_version); |
| 408 | break; |
| 409 | case NetworkAddressIPv4: |
| 410 | kvp_get_ip_address(AF_INET, key_value, |
| 411 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE); |
| 412 | strcpy(key_name, "NetworkAddressIPv4"); |
| 413 | break; |
| 414 | case NetworkAddressIPv6: |
| 415 | kvp_get_ip_address(AF_INET6, key_value, |
| 416 | HV_KVP_EXCHANGE_MAX_VALUE_SIZE); |
| 417 | strcpy(key_name, "NetworkAddressIPv6"); |
| 418 | break; |
| 419 | case OSBuildNumber: |
| 420 | strcpy(key_value, os_build); |
| 421 | strcpy(key_name, "OSBuildNumber"); |
| 422 | break; |
| 423 | case OSName: |
| 424 | strcpy(key_value, os_name); |
| 425 | strcpy(key_name, "OSName"); |
| 426 | break; |
| 427 | case OSMajorVersion: |
| 428 | strcpy(key_value, os_major); |
| 429 | strcpy(key_name, "OSMajorVersion"); |
| 430 | break; |
| 431 | case OSMinorVersion: |
| 432 | strcpy(key_value, os_minor); |
| 433 | strcpy(key_name, "OSMinorVersion"); |
| 434 | break; |
| 435 | case OSVersion: |
| 436 | strcpy(key_value, os_build); |
| 437 | strcpy(key_name, "OSVersion"); |
| 438 | break; |
| 439 | case ProcessorArchitecture: |
| 440 | strcpy(key_value, processor_arch); |
| 441 | strcpy(key_name, "ProcessorArchitecture"); |
| 442 | break; |
| 443 | default: |
| 444 | strcpy(key_value, "Unknown Key"); |
| 445 | /* |
| 446 | * We use a null key name to terminate enumeration. |
| 447 | */ |
| 448 | strcpy(key_name, ""); |
| 449 | break; |
| 450 | } |
| 451 | /* |
| 452 | * Send the value back to the kernel. The response is |
| 453 | * already in the receive buffer. Update the cn_msg header to |
| 454 | * reflect the key value that has been added to the message |
| 455 | */ |
| 456 | |
| 457 | incoming_cn_msg->id.idx = CN_KVP_IDX; |
| 458 | incoming_cn_msg->id.val = CN_KVP_VAL; |
| 459 | incoming_cn_msg->seq = KVP_USER_SET; |
| 460 | incoming_cn_msg->ack = 0; |
| 461 | incoming_cn_msg->len = sizeof(struct hv_ku_msg); |
| 462 | |
| 463 | len = netlink_send(fd, incoming_cn_msg); |
| 464 | if (len < 0) { |
| 465 | syslog(LOG_ERR, "net_link send failed; error:%d", len); |
| 466 | exit(-1); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | } |