hv_kvp_daemon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  80. char 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. char 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 fd;
  172. FILE *filep;
  173. size_t records_read;
  174. char *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 num_records;
  272. struct kvp_record *record;
  273. int num_blocks;
  274. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  275. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  276. return 1;
  277. /*
  278. * First update the in-memory state.
  279. */
  280. kvp_update_mem_state(pool);
  281. num_records = kvp_file_info[pool].num_records;
  282. record = kvp_file_info[pool].records;
  283. num_blocks = kvp_file_info[pool].num_blocks;
  284. for (i = 0; i < num_records; i++) {
  285. if (memcmp(key, record[i].key, key_size))
  286. continue;
  287. /*
  288. * Found a match; just update the value -
  289. * this is the modify case.
  290. */
  291. memcpy(record[i].value, value, value_size);
  292. kvp_update_file(pool);
  293. return 0;
  294. }
  295. /*
  296. * Need to add a new entry;
  297. */
  298. if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
  299. /* Need to allocate a larger array for reg entries. */
  300. record = realloc(record, sizeof(struct kvp_record) *
  301. ENTRIES_PER_BLOCK * (num_blocks + 1));
  302. if (record == NULL)
  303. return 1;
  304. kvp_file_info[pool].num_blocks++;
  305. }
  306. memcpy(record[i].value, value, value_size);
  307. memcpy(record[i].key, key, key_size);
  308. kvp_file_info[pool].records = record;
  309. kvp_file_info[pool].num_records++;
  310. kvp_update_file(pool);
  311. return 0;
  312. }
  313. static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
  314. int value_size)
  315. {
  316. int i;
  317. int num_records;
  318. struct kvp_record *record;
  319. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  320. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  321. return 1;
  322. /*
  323. * First update the in-memory state.
  324. */
  325. kvp_update_mem_state(pool);
  326. num_records = kvp_file_info[pool].num_records;
  327. record = kvp_file_info[pool].records;
  328. for (i = 0; i < num_records; i++) {
  329. if (memcmp(key, record[i].key, key_size))
  330. continue;
  331. /*
  332. * Found a match; just copy the value out.
  333. */
  334. memcpy(value, record[i].value, value_size);
  335. return 0;
  336. }
  337. return 1;
  338. }
  339. static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
  340. __u8 *value, int value_size)
  341. {
  342. struct kvp_record *record;
  343. /*
  344. * First update our in-memory database.
  345. */
  346. kvp_update_mem_state(pool);
  347. record = kvp_file_info[pool].records;
  348. if (index >= kvp_file_info[pool].num_records) {
  349. return 1;
  350. }
  351. memcpy(key, record[index].key, key_size);
  352. memcpy(value, record[index].value, value_size);
  353. return 0;
  354. }
  355. void kvp_get_os_info(void)
  356. {
  357. FILE *file;
  358. char *p, buf[512];
  359. uname(&uts_buf);
  360. os_build = uts_buf.release;
  361. processor_arch = uts_buf.machine;
  362. /*
  363. * The current windows host (win7) expects the build
  364. * string to be of the form: x.y.z
  365. * Strip additional information we may have.
  366. */
  367. p = strchr(os_build, '-');
  368. if (p)
  369. *p = '\0';
  370. file = fopen("/etc/SuSE-release", "r");
  371. if (file != NULL)
  372. goto kvp_osinfo_found;
  373. file = fopen("/etc/redhat-release", "r");
  374. if (file != NULL)
  375. goto kvp_osinfo_found;
  376. /*
  377. * Add code for other supported platforms.
  378. */
  379. /*
  380. * We don't have information about the os.
  381. */
  382. os_name = uts_buf.sysname;
  383. return;
  384. kvp_osinfo_found:
  385. /* up to three lines */
  386. p = fgets(buf, sizeof(buf), file);
  387. if (p) {
  388. p = strchr(buf, '\n');
  389. if (p)
  390. *p = '\0';
  391. p = strdup(buf);
  392. if (!p)
  393. goto done;
  394. os_name = p;
  395. /* second line */
  396. p = fgets(buf, sizeof(buf), file);
  397. if (p) {
  398. p = strchr(buf, '\n');
  399. if (p)
  400. *p = '\0';
  401. p = strdup(buf);
  402. if (!p)
  403. goto done;
  404. os_major = p;
  405. /* third line */
  406. p = fgets(buf, sizeof(buf), file);
  407. if (p) {
  408. p = strchr(buf, '\n');
  409. if (p)
  410. *p = '\0';
  411. p = strdup(buf);
  412. if (p)
  413. os_minor = p;
  414. }
  415. }
  416. }
  417. done:
  418. fclose(file);
  419. return;
  420. }
  421. static void kvp_process_ipconfig_file(char *cmd,
  422. char *config_buf, int len,
  423. int element_size, int offset)
  424. {
  425. char buf[256];
  426. char *p;
  427. char *x;
  428. FILE *file;
  429. /*
  430. * First execute the command.
  431. */
  432. file = popen(cmd, "r");
  433. if (file == NULL)
  434. return;
  435. if (offset == 0)
  436. memset(config_buf, 0, len);
  437. while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
  438. if ((len - strlen(config_buf)) < (element_size + 1))
  439. break;
  440. x = strchr(p, '\n');
  441. *x = '\0';
  442. strcat(config_buf, p);
  443. strcat(config_buf, ";");
  444. }
  445. pclose(file);
  446. }
  447. static void kvp_get_ipconfig_info(char *if_name,
  448. struct hv_kvp_ipaddr_value *buffer)
  449. {
  450. char cmd[512];
  451. /*
  452. * Get the address of default gateway (ipv4).
  453. */
  454. sprintf(cmd, "%s %s", "ip route show dev", if_name);
  455. strcat(cmd, " | awk '/default/ {print $3 }'");
  456. /*
  457. * Execute the command to gather gateway info.
  458. */
  459. kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
  460. (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
  461. /*
  462. * Get the address of default gateway (ipv6).
  463. */
  464. sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
  465. strcat(cmd, " | awk '/default/ {print $3 }'");
  466. /*
  467. * Execute the command to gather gateway info (ipv6).
  468. */
  469. kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
  470. (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
  471. /*
  472. * Gather the DNS state.
  473. * Since there is no standard way to get this information
  474. * across various distributions of interest; we just invoke
  475. * an external script that needs to be ported across distros
  476. * of interest.
  477. *
  478. * Following is the expected format of the information from the script:
  479. *
  480. * ipaddr1 (nameserver1)
  481. * ipaddr2 (nameserver2)
  482. * .
  483. * .
  484. */
  485. sprintf(cmd, "%s", "hv_get_dns_info");
  486. /*
  487. * Execute the command to gather DNS info.
  488. */
  489. kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
  490. (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
  491. }
  492. static unsigned int hweight32(unsigned int *w)
  493. {
  494. unsigned int res = *w - ((*w >> 1) & 0x55555555);
  495. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  496. res = (res + (res >> 4)) & 0x0F0F0F0F;
  497. res = res + (res >> 8);
  498. return (res + (res >> 16)) & 0x000000FF;
  499. }
  500. static int kvp_process_ip_address(void *addrp,
  501. int family, char *buffer,
  502. int length, int *offset)
  503. {
  504. struct sockaddr_in *addr;
  505. struct sockaddr_in6 *addr6;
  506. int addr_length;
  507. char tmp[50];
  508. const char *str;
  509. if (family == AF_INET) {
  510. addr = (struct sockaddr_in *)addrp;
  511. str = inet_ntop(family, &addr->sin_addr, tmp, 50);
  512. addr_length = INET_ADDRSTRLEN;
  513. } else {
  514. addr6 = (struct sockaddr_in6 *)addrp;
  515. str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
  516. addr_length = INET6_ADDRSTRLEN;
  517. }
  518. if ((length - *offset) < addr_length + 1)
  519. return 1;
  520. if (str == NULL) {
  521. strcpy(buffer, "inet_ntop failed\n");
  522. return 1;
  523. }
  524. if (*offset == 0)
  525. strcpy(buffer, tmp);
  526. else
  527. strcat(buffer, tmp);
  528. strcat(buffer, ";");
  529. *offset += strlen(str) + 1;
  530. return 0;
  531. }
  532. static int
  533. kvp_get_ip_address(int family, char *if_name, int op,
  534. void *out_buffer, int length)
  535. {
  536. struct ifaddrs *ifap;
  537. struct ifaddrs *curp;
  538. int offset = 0;
  539. int sn_offset = 0;
  540. int error = 0;
  541. char *buffer;
  542. struct hv_kvp_ipaddr_value *ip_buffer;
  543. char cidr_mask[5]; /* /xyz */
  544. int weight;
  545. int i;
  546. unsigned int *w;
  547. char *sn_str;
  548. struct sockaddr_in6 *addr6;
  549. if (op == KVP_OP_ENUMERATE) {
  550. buffer = out_buffer;
  551. } else {
  552. ip_buffer = out_buffer;
  553. buffer = (char *)ip_buffer->ip_addr;
  554. ip_buffer->addr_family = 0;
  555. }
  556. /*
  557. * On entry into this function, the buffer is capable of holding the
  558. * maximum key value.
  559. */
  560. if (getifaddrs(&ifap)) {
  561. strcpy(buffer, "getifaddrs failed\n");
  562. return 1;
  563. }
  564. curp = ifap;
  565. while (curp != NULL) {
  566. if (curp->ifa_addr == NULL) {
  567. curp = curp->ifa_next;
  568. continue;
  569. }
  570. if ((if_name != NULL) &&
  571. (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
  572. /*
  573. * We want info about a specific interface;
  574. * just continue.
  575. */
  576. curp = curp->ifa_next;
  577. continue;
  578. }
  579. /*
  580. * We only support two address families: AF_INET and AF_INET6.
  581. * If a family value of 0 is specified, we collect both
  582. * supported address families; if not we gather info on
  583. * the specified address family.
  584. */
  585. if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
  586. curp = curp->ifa_next;
  587. continue;
  588. }
  589. if ((curp->ifa_addr->sa_family != AF_INET) &&
  590. (curp->ifa_addr->sa_family != AF_INET6)) {
  591. curp = curp->ifa_next;
  592. continue;
  593. }
  594. if (op == KVP_OP_GET_IP_INFO) {
  595. /*
  596. * Gather info other than the IP address.
  597. * IP address info will be gathered later.
  598. */
  599. if (curp->ifa_addr->sa_family == AF_INET) {
  600. ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
  601. /*
  602. * Get subnet info.
  603. */
  604. error = kvp_process_ip_address(
  605. curp->ifa_netmask,
  606. AF_INET,
  607. (char *)
  608. ip_buffer->sub_net,
  609. length,
  610. &sn_offset);
  611. if (error)
  612. goto gather_ipaddr;
  613. } else {
  614. ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
  615. /*
  616. * Get subnet info in CIDR format.
  617. */
  618. weight = 0;
  619. sn_str = (char *)ip_buffer->sub_net;
  620. addr6 = (struct sockaddr_in6 *)
  621. curp->ifa_netmask;
  622. w = addr6->sin6_addr.s6_addr32;
  623. for (i = 0; i < 4; i++)
  624. weight += hweight32(&w[i]);
  625. sprintf(cidr_mask, "/%d", weight);
  626. if ((length - sn_offset) <
  627. (strlen(cidr_mask) + 1))
  628. goto gather_ipaddr;
  629. if (sn_offset == 0)
  630. strcpy(sn_str, cidr_mask);
  631. else
  632. strcat(sn_str, cidr_mask);
  633. strcat((char *)ip_buffer->sub_net, ";");
  634. sn_offset += strlen(sn_str) + 1;
  635. }
  636. /*
  637. * Collect other ip related configuration info.
  638. */
  639. kvp_get_ipconfig_info(if_name, ip_buffer);
  640. }
  641. gather_ipaddr:
  642. error = kvp_process_ip_address(curp->ifa_addr,
  643. curp->ifa_addr->sa_family,
  644. buffer,
  645. length, &offset);
  646. if (error)
  647. goto getaddr_done;
  648. curp = curp->ifa_next;
  649. }
  650. getaddr_done:
  651. freeifaddrs(ifap);
  652. return error;
  653. }
  654. static int
  655. kvp_get_domain_name(char *buffer, int length)
  656. {
  657. struct addrinfo hints, *info ;
  658. int error = 0;
  659. gethostname(buffer, length);
  660. memset(&hints, 0, sizeof(hints));
  661. hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
  662. hints.ai_socktype = SOCK_STREAM;
  663. hints.ai_flags = AI_CANONNAME;
  664. error = getaddrinfo(buffer, NULL, &hints, &info);
  665. if (error != 0) {
  666. strcpy(buffer, "getaddrinfo failed\n");
  667. return error;
  668. }
  669. strcpy(buffer, info->ai_canonname);
  670. freeaddrinfo(info);
  671. return error;
  672. }
  673. static int
  674. netlink_send(int fd, struct cn_msg *msg)
  675. {
  676. struct nlmsghdr *nlh;
  677. unsigned int size;
  678. struct msghdr message;
  679. char buffer[64];
  680. struct iovec iov[2];
  681. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  682. nlh = (struct nlmsghdr *)buffer;
  683. nlh->nlmsg_seq = 0;
  684. nlh->nlmsg_pid = getpid();
  685. nlh->nlmsg_type = NLMSG_DONE;
  686. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  687. nlh->nlmsg_flags = 0;
  688. iov[0].iov_base = nlh;
  689. iov[0].iov_len = sizeof(*nlh);
  690. iov[1].iov_base = msg;
  691. iov[1].iov_len = size;
  692. memset(&message, 0, sizeof(message));
  693. message.msg_name = &addr;
  694. message.msg_namelen = sizeof(addr);
  695. message.msg_iov = iov;
  696. message.msg_iovlen = 2;
  697. return sendmsg(fd, &message, 0);
  698. }
  699. int main(void)
  700. {
  701. int fd, len, sock_opt;
  702. int error;
  703. struct cn_msg *message;
  704. struct pollfd pfd;
  705. struct nlmsghdr *incoming_msg;
  706. struct cn_msg *incoming_cn_msg;
  707. struct hv_kvp_msg *hv_msg;
  708. char *p;
  709. char *key_value;
  710. char *key_name;
  711. int op;
  712. int pool;
  713. daemon(1, 0);
  714. openlog("KVP", 0, LOG_USER);
  715. syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
  716. /*
  717. * Retrieve OS release information.
  718. */
  719. kvp_get_os_info();
  720. if (kvp_file_init()) {
  721. syslog(LOG_ERR, "Failed to initialize the pools");
  722. exit(-1);
  723. }
  724. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  725. if (fd < 0) {
  726. syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
  727. exit(-1);
  728. }
  729. addr.nl_family = AF_NETLINK;
  730. addr.nl_pad = 0;
  731. addr.nl_pid = 0;
  732. addr.nl_groups = CN_KVP_IDX;
  733. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  734. if (error < 0) {
  735. syslog(LOG_ERR, "bind failed; error:%d", error);
  736. close(fd);
  737. exit(-1);
  738. }
  739. sock_opt = addr.nl_groups;
  740. setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
  741. /*
  742. * Register ourselves with the kernel.
  743. */
  744. message = (struct cn_msg *)kvp_send_buffer;
  745. message->id.idx = CN_KVP_IDX;
  746. message->id.val = CN_KVP_VAL;
  747. hv_msg = (struct hv_kvp_msg *)message->data;
  748. hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
  749. message->ack = 0;
  750. message->len = sizeof(struct hv_kvp_msg);
  751. len = netlink_send(fd, message);
  752. if (len < 0) {
  753. syslog(LOG_ERR, "netlink_send failed; error:%d", len);
  754. close(fd);
  755. exit(-1);
  756. }
  757. pfd.fd = fd;
  758. while (1) {
  759. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  760. socklen_t addr_l = sizeof(addr);
  761. pfd.events = POLLIN;
  762. pfd.revents = 0;
  763. poll(&pfd, 1, -1);
  764. len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
  765. addr_p, &addr_l);
  766. if (len < 0 || addr.nl_pid) {
  767. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  768. addr.nl_pid, errno, strerror(errno));
  769. close(fd);
  770. return -1;
  771. }
  772. incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
  773. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  774. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  775. /*
  776. * We will use the KVP header information to pass back
  777. * the error from this daemon. So, first copy the state
  778. * and set the error code to success.
  779. */
  780. op = hv_msg->kvp_hdr.operation;
  781. pool = hv_msg->kvp_hdr.pool;
  782. hv_msg->error = HV_S_OK;
  783. if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
  784. /*
  785. * Driver is registering with us; stash away the version
  786. * information.
  787. */
  788. in_hand_shake = 0;
  789. p = (char *)hv_msg->body.kvp_register.version;
  790. lic_version = malloc(strlen(p) + 1);
  791. if (lic_version) {
  792. strcpy(lic_version, p);
  793. syslog(LOG_INFO, "KVP LIC Version: %s",
  794. lic_version);
  795. } else {
  796. syslog(LOG_ERR, "malloc failed");
  797. }
  798. continue;
  799. }
  800. switch (op) {
  801. case KVP_OP_SET:
  802. if (kvp_key_add_or_modify(pool,
  803. hv_msg->body.kvp_set.data.key,
  804. hv_msg->body.kvp_set.data.key_size,
  805. hv_msg->body.kvp_set.data.value,
  806. hv_msg->body.kvp_set.data.value_size))
  807. hv_msg->error = HV_S_CONT;
  808. break;
  809. case KVP_OP_GET:
  810. if (kvp_get_value(pool,
  811. hv_msg->body.kvp_set.data.key,
  812. hv_msg->body.kvp_set.data.key_size,
  813. hv_msg->body.kvp_set.data.value,
  814. hv_msg->body.kvp_set.data.value_size))
  815. hv_msg->error = HV_S_CONT;
  816. break;
  817. case KVP_OP_DELETE:
  818. if (kvp_key_delete(pool,
  819. hv_msg->body.kvp_delete.key,
  820. hv_msg->body.kvp_delete.key_size))
  821. hv_msg->error = HV_S_CONT;
  822. break;
  823. default:
  824. break;
  825. }
  826. if (op != KVP_OP_ENUMERATE)
  827. goto kvp_done;
  828. /*
  829. * If the pool is KVP_POOL_AUTO, dynamically generate
  830. * both the key and the value; if not read from the
  831. * appropriate pool.
  832. */
  833. if (pool != KVP_POOL_AUTO) {
  834. if (kvp_pool_enumerate(pool,
  835. hv_msg->body.kvp_enum_data.index,
  836. hv_msg->body.kvp_enum_data.data.key,
  837. HV_KVP_EXCHANGE_MAX_KEY_SIZE,
  838. hv_msg->body.kvp_enum_data.data.value,
  839. HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  840. hv_msg->error = HV_S_CONT;
  841. goto kvp_done;
  842. }
  843. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  844. key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
  845. key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
  846. switch (hv_msg->body.kvp_enum_data.index) {
  847. case FullyQualifiedDomainName:
  848. kvp_get_domain_name(key_value,
  849. HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  850. strcpy(key_name, "FullyQualifiedDomainName");
  851. break;
  852. case IntegrationServicesVersion:
  853. strcpy(key_name, "IntegrationServicesVersion");
  854. strcpy(key_value, lic_version);
  855. break;
  856. case NetworkAddressIPv4:
  857. kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
  858. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  859. strcpy(key_name, "NetworkAddressIPv4");
  860. break;
  861. case NetworkAddressIPv6:
  862. kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
  863. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  864. strcpy(key_name, "NetworkAddressIPv6");
  865. break;
  866. case OSBuildNumber:
  867. strcpy(key_value, os_build);
  868. strcpy(key_name, "OSBuildNumber");
  869. break;
  870. case OSName:
  871. strcpy(key_value, os_name);
  872. strcpy(key_name, "OSName");
  873. break;
  874. case OSMajorVersion:
  875. strcpy(key_value, os_major);
  876. strcpy(key_name, "OSMajorVersion");
  877. break;
  878. case OSMinorVersion:
  879. strcpy(key_value, os_minor);
  880. strcpy(key_name, "OSMinorVersion");
  881. break;
  882. case OSVersion:
  883. strcpy(key_value, os_build);
  884. strcpy(key_name, "OSVersion");
  885. break;
  886. case ProcessorArchitecture:
  887. strcpy(key_value, processor_arch);
  888. strcpy(key_name, "ProcessorArchitecture");
  889. break;
  890. default:
  891. hv_msg->error = HV_S_CONT;
  892. break;
  893. }
  894. /*
  895. * Send the value back to the kernel. The response is
  896. * already in the receive buffer. Update the cn_msg header to
  897. * reflect the key value that has been added to the message
  898. */
  899. kvp_done:
  900. incoming_cn_msg->id.idx = CN_KVP_IDX;
  901. incoming_cn_msg->id.val = CN_KVP_VAL;
  902. incoming_cn_msg->ack = 0;
  903. incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
  904. len = netlink_send(fd, incoming_cn_msg);
  905. if (len < 0) {
  906. syslog(LOG_ERR, "net_link send failed; error:%d", len);
  907. exit(-1);
  908. }
  909. }
  910. }