hv_kvp_daemon.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <sys/poll.h>
  26. #include <sys/utsname.h>
  27. #include <linux/types.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <arpa/inet.h>
  34. #include <linux/connector.h>
  35. #include <linux/hyperv.h>
  36. #include <linux/netlink.h>
  37. #include <ifaddrs.h>
  38. #include <netdb.h>
  39. #include <syslog.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. /*
  43. * KVP protocol: The user mode component first registers with the
  44. * the kernel component. Subsequently, the kernel component requests, data
  45. * for the specified keys. In response to this message the user mode component
  46. * fills in the value corresponding to the specified key. We overload the
  47. * sequence field in the cn_msg header to define our KVP message types.
  48. *
  49. * We use this infrastructure for also supporting queries from user mode
  50. * application for state that may be maintained in the KVP kernel component.
  51. *
  52. */
  53. enum key_index {
  54. FullyQualifiedDomainName = 0,
  55. IntegrationServicesVersion, /*This key is serviced in the kernel*/
  56. NetworkAddressIPv4,
  57. NetworkAddressIPv6,
  58. OSBuildNumber,
  59. OSName,
  60. OSMajorVersion,
  61. OSMinorVersion,
  62. OSVersion,
  63. ProcessorArchitecture
  64. };
  65. static char kvp_send_buffer[4096];
  66. static char kvp_recv_buffer[4096 * 2];
  67. static struct sockaddr_nl addr;
  68. static int in_hand_shake = 1;
  69. static char *os_name = "";
  70. static char *os_major = "";
  71. static char *os_minor = "";
  72. static char *processor_arch;
  73. static char *os_build;
  74. static char *lic_version = "Unknown version";
  75. static struct utsname uts_buf;
  76. #define MAX_FILE_NAME 100
  77. #define ENTRIES_PER_BLOCK 50
  78. struct kvp_record {
  79. __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  80. __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
  81. };
  82. struct kvp_file_state {
  83. int fd;
  84. int num_blocks;
  85. struct kvp_record *records;
  86. int num_records;
  87. __u8 fname[MAX_FILE_NAME];
  88. };
  89. static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
  90. static void kvp_acquire_lock(int pool)
  91. {
  92. struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
  93. fl.l_pid = getpid();
  94. if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
  95. syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
  96. exit(-1);
  97. }
  98. }
  99. static void kvp_release_lock(int pool)
  100. {
  101. struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
  102. fl.l_pid = getpid();
  103. if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
  104. perror("fcntl");
  105. syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
  106. exit(-1);
  107. }
  108. }
  109. static void kvp_update_file(int pool)
  110. {
  111. FILE *filep;
  112. size_t bytes_written;
  113. /*
  114. * We are going to write our in-memory registry out to
  115. * disk; acquire the lock first.
  116. */
  117. kvp_acquire_lock(pool);
  118. filep = fopen(kvp_file_info[pool].fname, "w");
  119. if (!filep) {
  120. kvp_release_lock(pool);
  121. syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
  122. exit(-1);
  123. }
  124. bytes_written = fwrite(kvp_file_info[pool].records,
  125. sizeof(struct kvp_record),
  126. kvp_file_info[pool].num_records, filep);
  127. fflush(filep);
  128. kvp_release_lock(pool);
  129. }
  130. static void kvp_update_mem_state(int pool)
  131. {
  132. FILE *filep;
  133. size_t records_read = 0;
  134. struct kvp_record *record = kvp_file_info[pool].records;
  135. struct kvp_record *readp;
  136. int num_blocks = kvp_file_info[pool].num_blocks;
  137. int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
  138. kvp_acquire_lock(pool);
  139. filep = fopen(kvp_file_info[pool].fname, "r");
  140. if (!filep) {
  141. kvp_release_lock(pool);
  142. syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
  143. exit(-1);
  144. }
  145. while (!feof(filep)) {
  146. readp = &record[records_read];
  147. records_read += fread(readp, sizeof(struct kvp_record),
  148. ENTRIES_PER_BLOCK * num_blocks,
  149. filep);
  150. if (!feof(filep)) {
  151. /*
  152. * We have more data to read.
  153. */
  154. num_blocks++;
  155. record = realloc(record, alloc_unit * num_blocks);
  156. if (record == NULL) {
  157. syslog(LOG_ERR, "malloc failed");
  158. exit(-1);
  159. }
  160. continue;
  161. }
  162. break;
  163. }
  164. kvp_file_info[pool].num_blocks = num_blocks;
  165. kvp_file_info[pool].records = record;
  166. kvp_file_info[pool].num_records = records_read;
  167. kvp_release_lock(pool);
  168. }
  169. static int kvp_file_init(void)
  170. {
  171. int ret, fd;
  172. FILE *filep;
  173. size_t records_read;
  174. __u8 *fname;
  175. struct kvp_record *record;
  176. struct kvp_record *readp;
  177. int num_blocks;
  178. int i;
  179. int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
  180. if (access("/var/opt/hyperv", F_OK)) {
  181. if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
  182. syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
  183. exit(-1);
  184. }
  185. }
  186. for (i = 0; i < KVP_POOL_COUNT; i++) {
  187. fname = kvp_file_info[i].fname;
  188. records_read = 0;
  189. num_blocks = 1;
  190. sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
  191. fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
  192. if (fd == -1)
  193. return 1;
  194. filep = fopen(fname, "r");
  195. if (!filep)
  196. return 1;
  197. record = malloc(alloc_unit * num_blocks);
  198. if (record == NULL) {
  199. fclose(filep);
  200. return 1;
  201. }
  202. while (!feof(filep)) {
  203. readp = &record[records_read];
  204. records_read += fread(readp, sizeof(struct kvp_record),
  205. ENTRIES_PER_BLOCK,
  206. filep);
  207. if (!feof(filep)) {
  208. /*
  209. * We have more data to read.
  210. */
  211. num_blocks++;
  212. record = realloc(record, alloc_unit *
  213. num_blocks);
  214. if (record == NULL) {
  215. fclose(filep);
  216. return 1;
  217. }
  218. continue;
  219. }
  220. break;
  221. }
  222. kvp_file_info[i].fd = fd;
  223. kvp_file_info[i].num_blocks = num_blocks;
  224. kvp_file_info[i].records = record;
  225. kvp_file_info[i].num_records = records_read;
  226. fclose(filep);
  227. }
  228. return 0;
  229. }
  230. static int kvp_key_delete(int pool, __u8 *key, int key_size)
  231. {
  232. int i;
  233. int j, k;
  234. int num_records;
  235. struct kvp_record *record;
  236. /*
  237. * First update the in-memory state.
  238. */
  239. kvp_update_mem_state(pool);
  240. num_records = kvp_file_info[pool].num_records;
  241. record = kvp_file_info[pool].records;
  242. for (i = 0; i < num_records; i++) {
  243. if (memcmp(key, record[i].key, key_size))
  244. continue;
  245. /*
  246. * Found a match; just move the remaining
  247. * entries up.
  248. */
  249. if (i == num_records) {
  250. kvp_file_info[pool].num_records--;
  251. kvp_update_file(pool);
  252. return 0;
  253. }
  254. j = i;
  255. k = j + 1;
  256. for (; k < num_records; k++) {
  257. strcpy(record[j].key, record[k].key);
  258. strcpy(record[j].value, record[k].value);
  259. j++;
  260. }
  261. kvp_file_info[pool].num_records--;
  262. kvp_update_file(pool);
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
  268. int value_size)
  269. {
  270. int i;
  271. int j, k;
  272. int num_records;
  273. struct kvp_record *record;
  274. int num_blocks;
  275. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  276. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  277. return 1;
  278. /*
  279. * First update the in-memory state.
  280. */
  281. kvp_update_mem_state(pool);
  282. num_records = kvp_file_info[pool].num_records;
  283. record = kvp_file_info[pool].records;
  284. num_blocks = kvp_file_info[pool].num_blocks;
  285. for (i = 0; i < num_records; i++) {
  286. if (memcmp(key, record[i].key, key_size))
  287. continue;
  288. /*
  289. * Found a match; just update the value -
  290. * this is the modify case.
  291. */
  292. memcpy(record[i].value, value, value_size);
  293. kvp_update_file(pool);
  294. return 0;
  295. }
  296. /*
  297. * Need to add a new entry;
  298. */
  299. if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
  300. /* Need to allocate a larger array for reg entries. */
  301. record = realloc(record, sizeof(struct kvp_record) *
  302. ENTRIES_PER_BLOCK * (num_blocks + 1));
  303. if (record == NULL)
  304. return 1;
  305. kvp_file_info[pool].num_blocks++;
  306. }
  307. memcpy(record[i].value, value, value_size);
  308. memcpy(record[i].key, key, key_size);
  309. kvp_file_info[pool].records = record;
  310. kvp_file_info[pool].num_records++;
  311. kvp_update_file(pool);
  312. return 0;
  313. }
  314. static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
  315. int value_size)
  316. {
  317. int i;
  318. int num_records;
  319. struct kvp_record *record;
  320. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  321. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  322. return 1;
  323. /*
  324. * First update the in-memory state.
  325. */
  326. kvp_update_mem_state(pool);
  327. num_records = kvp_file_info[pool].num_records;
  328. record = kvp_file_info[pool].records;
  329. for (i = 0; i < num_records; i++) {
  330. if (memcmp(key, record[i].key, key_size))
  331. continue;
  332. /*
  333. * Found a match; just copy the value out.
  334. */
  335. memcpy(value, record[i].value, value_size);
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
  341. __u8 *value, int value_size)
  342. {
  343. struct kvp_record *record;
  344. /*
  345. * First update our in-memory database.
  346. */
  347. kvp_update_mem_state(pool);
  348. record = kvp_file_info[pool].records;
  349. if (index >= kvp_file_info[pool].num_records) {
  350. return 1;
  351. }
  352. memcpy(key, record[index].key, key_size);
  353. memcpy(value, record[index].value, value_size);
  354. return 0;
  355. }
  356. void kvp_get_os_info(void)
  357. {
  358. FILE *file;
  359. char *p, buf[512];
  360. uname(&uts_buf);
  361. os_build = uts_buf.release;
  362. processor_arch = uts_buf.machine;
  363. /*
  364. * The current windows host (win7) expects the build
  365. * string to be of the form: x.y.z
  366. * Strip additional information we may have.
  367. */
  368. p = strchr(os_build, '-');
  369. if (p)
  370. *p = '\0';
  371. file = fopen("/etc/SuSE-release", "r");
  372. if (file != NULL)
  373. goto kvp_osinfo_found;
  374. file = fopen("/etc/redhat-release", "r");
  375. if (file != NULL)
  376. goto kvp_osinfo_found;
  377. /*
  378. * Add code for other supported platforms.
  379. */
  380. /*
  381. * We don't have information about the os.
  382. */
  383. os_name = uts_buf.sysname;
  384. return;
  385. kvp_osinfo_found:
  386. /* up to three lines */
  387. p = fgets(buf, sizeof(buf), file);
  388. if (p) {
  389. p = strchr(buf, '\n');
  390. if (p)
  391. *p = '\0';
  392. p = strdup(buf);
  393. if (!p)
  394. goto done;
  395. os_name = p;
  396. /* second line */
  397. p = fgets(buf, sizeof(buf), file);
  398. if (p) {
  399. p = strchr(buf, '\n');
  400. if (p)
  401. *p = '\0';
  402. p = strdup(buf);
  403. if (!p)
  404. goto done;
  405. os_major = p;
  406. /* third line */
  407. p = fgets(buf, sizeof(buf), file);
  408. if (p) {
  409. p = strchr(buf, '\n');
  410. if (p)
  411. *p = '\0';
  412. p = strdup(buf);
  413. if (p)
  414. os_minor = p;
  415. }
  416. }
  417. }
  418. done:
  419. fclose(file);
  420. return;
  421. }
  422. static int kvp_process_ip_address(void *addrp,
  423. int family, char *buffer,
  424. int length, int *offset)
  425. {
  426. struct sockaddr_in *addr;
  427. struct sockaddr_in6 *addr6;
  428. int addr_length;
  429. char tmp[50];
  430. const char *str;
  431. if (family == AF_INET) {
  432. addr = (struct sockaddr_in *)addrp;
  433. str = inet_ntop(family, &addr->sin_addr, tmp, 50);
  434. addr_length = INET_ADDRSTRLEN;
  435. } else {
  436. addr6 = (struct sockaddr_in6 *)addrp;
  437. str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
  438. addr_length = INET6_ADDRSTRLEN;
  439. }
  440. if ((length - *offset) < addr_length + 1)
  441. return 1;
  442. if (str == NULL) {
  443. strcpy(buffer, "inet_ntop failed\n");
  444. return 1;
  445. }
  446. if (*offset == 0)
  447. strcpy(buffer, tmp);
  448. else
  449. strcat(buffer, tmp);
  450. strcat(buffer, ";");
  451. *offset += strlen(str) + 1;
  452. return 0;
  453. }
  454. static int
  455. kvp_get_ip_address(int family, char *if_name, int op,
  456. void *out_buffer, int length)
  457. {
  458. struct ifaddrs *ifap;
  459. struct ifaddrs *curp;
  460. int offset = 0;
  461. const char *str;
  462. int error = 0;
  463. char *buffer;
  464. struct hv_kvp_ipaddr_value *ip_buffer;
  465. if (op == KVP_OP_ENUMERATE) {
  466. buffer = out_buffer;
  467. } else {
  468. ip_buffer = out_buffer;
  469. buffer = (char *)ip_buffer->ip_addr;
  470. ip_buffer->addr_family = 0;
  471. }
  472. /*
  473. * On entry into this function, the buffer is capable of holding the
  474. * maximum key value.
  475. */
  476. if (getifaddrs(&ifap)) {
  477. strcpy(buffer, "getifaddrs failed\n");
  478. return 1;
  479. }
  480. curp = ifap;
  481. while (curp != NULL) {
  482. if (curp->ifa_addr == NULL) {
  483. curp = curp->ifa_next;
  484. continue;
  485. }
  486. if ((if_name != NULL) &&
  487. (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
  488. /*
  489. * We want info about a specific interface;
  490. * just continue.
  491. */
  492. curp = curp->ifa_next;
  493. continue;
  494. }
  495. /*
  496. * We only support two address families: AF_INET and AF_INET6.
  497. * If a family value of 0 is specified, we collect both
  498. * supported address families; if not we gather info on
  499. * the specified address family.
  500. */
  501. if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
  502. curp = curp->ifa_next;
  503. continue;
  504. }
  505. if ((curp->ifa_addr->sa_family != AF_INET) &&
  506. (curp->ifa_addr->sa_family != AF_INET6)) {
  507. curp = curp->ifa_next;
  508. continue;
  509. }
  510. if (op == KVP_OP_GET_IP_INFO) {
  511. /*
  512. * Gather info other than the IP address.
  513. * IP address info will be gathered later.
  514. */
  515. if (curp->ifa_addr->sa_family == AF_INET)
  516. ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
  517. else
  518. ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
  519. }
  520. error = kvp_process_ip_address(curp->ifa_addr,
  521. curp->ifa_addr->sa_family,
  522. buffer,
  523. length, &offset);
  524. if (error)
  525. goto getaddr_done;
  526. curp = curp->ifa_next;
  527. }
  528. getaddr_done:
  529. freeifaddrs(ifap);
  530. return error;
  531. }
  532. static int
  533. kvp_get_domain_name(char *buffer, int length)
  534. {
  535. struct addrinfo hints, *info ;
  536. int error = 0;
  537. gethostname(buffer, length);
  538. memset(&hints, 0, sizeof(hints));
  539. hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
  540. hints.ai_socktype = SOCK_STREAM;
  541. hints.ai_flags = AI_CANONNAME;
  542. error = getaddrinfo(buffer, NULL, &hints, &info);
  543. if (error != 0) {
  544. strcpy(buffer, "getaddrinfo failed\n");
  545. return error;
  546. }
  547. strcpy(buffer, info->ai_canonname);
  548. freeaddrinfo(info);
  549. return error;
  550. }
  551. static int
  552. netlink_send(int fd, struct cn_msg *msg)
  553. {
  554. struct nlmsghdr *nlh;
  555. unsigned int size;
  556. struct msghdr message;
  557. char buffer[64];
  558. struct iovec iov[2];
  559. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  560. nlh = (struct nlmsghdr *)buffer;
  561. nlh->nlmsg_seq = 0;
  562. nlh->nlmsg_pid = getpid();
  563. nlh->nlmsg_type = NLMSG_DONE;
  564. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  565. nlh->nlmsg_flags = 0;
  566. iov[0].iov_base = nlh;
  567. iov[0].iov_len = sizeof(*nlh);
  568. iov[1].iov_base = msg;
  569. iov[1].iov_len = size;
  570. memset(&message, 0, sizeof(message));
  571. message.msg_name = &addr;
  572. message.msg_namelen = sizeof(addr);
  573. message.msg_iov = iov;
  574. message.msg_iovlen = 2;
  575. return sendmsg(fd, &message, 0);
  576. }
  577. int main(void)
  578. {
  579. int fd, len, sock_opt;
  580. int error;
  581. struct cn_msg *message;
  582. struct pollfd pfd;
  583. struct nlmsghdr *incoming_msg;
  584. struct cn_msg *incoming_cn_msg;
  585. struct hv_kvp_msg *hv_msg;
  586. char *p;
  587. char *key_value;
  588. char *key_name;
  589. int op;
  590. int pool;
  591. daemon(1, 0);
  592. openlog("KVP", 0, LOG_USER);
  593. syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
  594. /*
  595. * Retrieve OS release information.
  596. */
  597. kvp_get_os_info();
  598. if (kvp_file_init()) {
  599. syslog(LOG_ERR, "Failed to initialize the pools");
  600. exit(-1);
  601. }
  602. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  603. if (fd < 0) {
  604. syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
  605. exit(-1);
  606. }
  607. addr.nl_family = AF_NETLINK;
  608. addr.nl_pad = 0;
  609. addr.nl_pid = 0;
  610. addr.nl_groups = CN_KVP_IDX;
  611. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  612. if (error < 0) {
  613. syslog(LOG_ERR, "bind failed; error:%d", error);
  614. close(fd);
  615. exit(-1);
  616. }
  617. sock_opt = addr.nl_groups;
  618. setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
  619. /*
  620. * Register ourselves with the kernel.
  621. */
  622. message = (struct cn_msg *)kvp_send_buffer;
  623. message->id.idx = CN_KVP_IDX;
  624. message->id.val = CN_KVP_VAL;
  625. hv_msg = (struct hv_kvp_msg *)message->data;
  626. hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
  627. message->ack = 0;
  628. message->len = sizeof(struct hv_kvp_msg);
  629. len = netlink_send(fd, message);
  630. if (len < 0) {
  631. syslog(LOG_ERR, "netlink_send failed; error:%d", len);
  632. close(fd);
  633. exit(-1);
  634. }
  635. pfd.fd = fd;
  636. while (1) {
  637. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  638. socklen_t addr_l = sizeof(addr);
  639. pfd.events = POLLIN;
  640. pfd.revents = 0;
  641. poll(&pfd, 1, -1);
  642. len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
  643. addr_p, &addr_l);
  644. if (len < 0 || addr.nl_pid) {
  645. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  646. addr.nl_pid, errno, strerror(errno));
  647. close(fd);
  648. return -1;
  649. }
  650. incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
  651. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  652. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  653. /*
  654. * We will use the KVP header information to pass back
  655. * the error from this daemon. So, first copy the state
  656. * and set the error code to success.
  657. */
  658. op = hv_msg->kvp_hdr.operation;
  659. pool = hv_msg->kvp_hdr.pool;
  660. hv_msg->error = HV_S_OK;
  661. if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
  662. /*
  663. * Driver is registering with us; stash away the version
  664. * information.
  665. */
  666. in_hand_shake = 0;
  667. p = (char *)hv_msg->body.kvp_register.version;
  668. lic_version = malloc(strlen(p) + 1);
  669. if (lic_version) {
  670. strcpy(lic_version, p);
  671. syslog(LOG_INFO, "KVP LIC Version: %s",
  672. lic_version);
  673. } else {
  674. syslog(LOG_ERR, "malloc failed");
  675. }
  676. continue;
  677. }
  678. switch (op) {
  679. case KVP_OP_SET:
  680. if (kvp_key_add_or_modify(pool,
  681. hv_msg->body.kvp_set.data.key,
  682. hv_msg->body.kvp_set.data.key_size,
  683. hv_msg->body.kvp_set.data.value,
  684. hv_msg->body.kvp_set.data.value_size))
  685. hv_msg->error = HV_S_CONT;
  686. break;
  687. case KVP_OP_GET:
  688. if (kvp_get_value(pool,
  689. hv_msg->body.kvp_set.data.key,
  690. hv_msg->body.kvp_set.data.key_size,
  691. hv_msg->body.kvp_set.data.value,
  692. hv_msg->body.kvp_set.data.value_size))
  693. hv_msg->error = HV_S_CONT;
  694. break;
  695. case KVP_OP_DELETE:
  696. if (kvp_key_delete(pool,
  697. hv_msg->body.kvp_delete.key,
  698. hv_msg->body.kvp_delete.key_size))
  699. hv_msg->error = HV_S_CONT;
  700. break;
  701. default:
  702. break;
  703. }
  704. if (op != KVP_OP_ENUMERATE)
  705. goto kvp_done;
  706. /*
  707. * If the pool is KVP_POOL_AUTO, dynamically generate
  708. * both the key and the value; if not read from the
  709. * appropriate pool.
  710. */
  711. if (pool != KVP_POOL_AUTO) {
  712. if (kvp_pool_enumerate(pool,
  713. hv_msg->body.kvp_enum_data.index,
  714. hv_msg->body.kvp_enum_data.data.key,
  715. HV_KVP_EXCHANGE_MAX_KEY_SIZE,
  716. hv_msg->body.kvp_enum_data.data.value,
  717. HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  718. hv_msg->error = HV_S_CONT;
  719. goto kvp_done;
  720. }
  721. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  722. key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
  723. key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
  724. switch (hv_msg->body.kvp_enum_data.index) {
  725. case FullyQualifiedDomainName:
  726. kvp_get_domain_name(key_value,
  727. HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  728. strcpy(key_name, "FullyQualifiedDomainName");
  729. break;
  730. case IntegrationServicesVersion:
  731. strcpy(key_name, "IntegrationServicesVersion");
  732. strcpy(key_value, lic_version);
  733. break;
  734. case NetworkAddressIPv4:
  735. kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
  736. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  737. strcpy(key_name, "NetworkAddressIPv4");
  738. break;
  739. case NetworkAddressIPv6:
  740. kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
  741. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  742. strcpy(key_name, "NetworkAddressIPv6");
  743. break;
  744. case OSBuildNumber:
  745. strcpy(key_value, os_build);
  746. strcpy(key_name, "OSBuildNumber");
  747. break;
  748. case OSName:
  749. strcpy(key_value, os_name);
  750. strcpy(key_name, "OSName");
  751. break;
  752. case OSMajorVersion:
  753. strcpy(key_value, os_major);
  754. strcpy(key_name, "OSMajorVersion");
  755. break;
  756. case OSMinorVersion:
  757. strcpy(key_value, os_minor);
  758. strcpy(key_name, "OSMinorVersion");
  759. break;
  760. case OSVersion:
  761. strcpy(key_value, os_build);
  762. strcpy(key_name, "OSVersion");
  763. break;
  764. case ProcessorArchitecture:
  765. strcpy(key_value, processor_arch);
  766. strcpy(key_name, "ProcessorArchitecture");
  767. break;
  768. default:
  769. hv_msg->error = HV_S_CONT;
  770. break;
  771. }
  772. /*
  773. * Send the value back to the kernel. The response is
  774. * already in the receive buffer. Update the cn_msg header to
  775. * reflect the key value that has been added to the message
  776. */
  777. kvp_done:
  778. incoming_cn_msg->id.idx = CN_KVP_IDX;
  779. incoming_cn_msg->id.val = CN_KVP_VAL;
  780. incoming_cn_msg->ack = 0;
  781. incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
  782. len = netlink_send(fd, incoming_cn_msg);
  783. if (len < 0) {
  784. syslog(LOG_ERR, "net_link send failed; error:%d", len);
  785. exit(-1);
  786. }
  787. }
  788. }