hv_kvp_daemon.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 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 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. static unsigned int hweight32(unsigned int *w)
  473. {
  474. unsigned int res = *w - ((*w >> 1) & 0x55555555);
  475. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  476. res = (res + (res >> 4)) & 0x0F0F0F0F;
  477. res = res + (res >> 8);
  478. return (res + (res >> 16)) & 0x000000FF;
  479. }
  480. static int kvp_process_ip_address(void *addrp,
  481. int family, char *buffer,
  482. int length, int *offset)
  483. {
  484. struct sockaddr_in *addr;
  485. struct sockaddr_in6 *addr6;
  486. int addr_length;
  487. char tmp[50];
  488. const char *str;
  489. if (family == AF_INET) {
  490. addr = (struct sockaddr_in *)addrp;
  491. str = inet_ntop(family, &addr->sin_addr, tmp, 50);
  492. addr_length = INET_ADDRSTRLEN;
  493. } else {
  494. addr6 = (struct sockaddr_in6 *)addrp;
  495. str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
  496. addr_length = INET6_ADDRSTRLEN;
  497. }
  498. if ((length - *offset) < addr_length + 1)
  499. return 1;
  500. if (str == NULL) {
  501. strcpy(buffer, "inet_ntop failed\n");
  502. return 1;
  503. }
  504. if (*offset == 0)
  505. strcpy(buffer, tmp);
  506. else
  507. strcat(buffer, tmp);
  508. strcat(buffer, ";");
  509. *offset += strlen(str) + 1;
  510. return 0;
  511. }
  512. static int
  513. kvp_get_ip_address(int family, char *if_name, int op,
  514. void *out_buffer, int length)
  515. {
  516. struct ifaddrs *ifap;
  517. struct ifaddrs *curp;
  518. int offset = 0;
  519. int sn_offset = 0;
  520. int error = 0;
  521. char *buffer;
  522. struct hv_kvp_ipaddr_value *ip_buffer;
  523. char cidr_mask[5]; /* /xyz */
  524. int weight;
  525. int i;
  526. unsigned int *w;
  527. char *sn_str;
  528. struct sockaddr_in6 *addr6;
  529. if (op == KVP_OP_ENUMERATE) {
  530. buffer = out_buffer;
  531. } else {
  532. ip_buffer = out_buffer;
  533. buffer = (char *)ip_buffer->ip_addr;
  534. ip_buffer->addr_family = 0;
  535. }
  536. /*
  537. * On entry into this function, the buffer is capable of holding the
  538. * maximum key value.
  539. */
  540. if (getifaddrs(&ifap)) {
  541. strcpy(buffer, "getifaddrs failed\n");
  542. return 1;
  543. }
  544. curp = ifap;
  545. while (curp != NULL) {
  546. if (curp->ifa_addr == NULL) {
  547. curp = curp->ifa_next;
  548. continue;
  549. }
  550. if ((if_name != NULL) &&
  551. (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
  552. /*
  553. * We want info about a specific interface;
  554. * just continue.
  555. */
  556. curp = curp->ifa_next;
  557. continue;
  558. }
  559. /*
  560. * We only support two address families: AF_INET and AF_INET6.
  561. * If a family value of 0 is specified, we collect both
  562. * supported address families; if not we gather info on
  563. * the specified address family.
  564. */
  565. if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
  566. curp = curp->ifa_next;
  567. continue;
  568. }
  569. if ((curp->ifa_addr->sa_family != AF_INET) &&
  570. (curp->ifa_addr->sa_family != AF_INET6)) {
  571. curp = curp->ifa_next;
  572. continue;
  573. }
  574. if (op == KVP_OP_GET_IP_INFO) {
  575. /*
  576. * Gather info other than the IP address.
  577. * IP address info will be gathered later.
  578. */
  579. if (curp->ifa_addr->sa_family == AF_INET) {
  580. ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
  581. /*
  582. * Get subnet info.
  583. */
  584. error = kvp_process_ip_address(
  585. curp->ifa_netmask,
  586. AF_INET,
  587. (char *)
  588. ip_buffer->sub_net,
  589. length,
  590. &sn_offset);
  591. if (error)
  592. goto gather_ipaddr;
  593. } else {
  594. ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
  595. /*
  596. * Get subnet info in CIDR format.
  597. */
  598. weight = 0;
  599. sn_str = (char *)ip_buffer->sub_net;
  600. addr6 = (struct sockaddr_in6 *)
  601. curp->ifa_netmask;
  602. w = addr6->sin6_addr.s6_addr32;
  603. for (i = 0; i < 4; i++)
  604. weight += hweight32(&w[i]);
  605. sprintf(cidr_mask, "/%d", weight);
  606. if ((length - sn_offset) <
  607. (strlen(cidr_mask) + 1))
  608. goto gather_ipaddr;
  609. if (sn_offset == 0)
  610. strcpy(sn_str, cidr_mask);
  611. else
  612. strcat(sn_str, cidr_mask);
  613. strcat((char *)ip_buffer->sub_net, ";");
  614. sn_offset += strlen(sn_str) + 1;
  615. }
  616. /*
  617. * Collect other ip related configuration info.
  618. */
  619. kvp_get_ipconfig_info(if_name, ip_buffer);
  620. }
  621. gather_ipaddr:
  622. error = kvp_process_ip_address(curp->ifa_addr,
  623. curp->ifa_addr->sa_family,
  624. buffer,
  625. length, &offset);
  626. if (error)
  627. goto getaddr_done;
  628. curp = curp->ifa_next;
  629. }
  630. getaddr_done:
  631. freeifaddrs(ifap);
  632. return error;
  633. }
  634. static int
  635. kvp_get_domain_name(char *buffer, int length)
  636. {
  637. struct addrinfo hints, *info ;
  638. int error = 0;
  639. gethostname(buffer, length);
  640. memset(&hints, 0, sizeof(hints));
  641. hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
  642. hints.ai_socktype = SOCK_STREAM;
  643. hints.ai_flags = AI_CANONNAME;
  644. error = getaddrinfo(buffer, NULL, &hints, &info);
  645. if (error != 0) {
  646. strcpy(buffer, "getaddrinfo failed\n");
  647. return error;
  648. }
  649. strcpy(buffer, info->ai_canonname);
  650. freeaddrinfo(info);
  651. return error;
  652. }
  653. static int
  654. netlink_send(int fd, struct cn_msg *msg)
  655. {
  656. struct nlmsghdr *nlh;
  657. unsigned int size;
  658. struct msghdr message;
  659. char buffer[64];
  660. struct iovec iov[2];
  661. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  662. nlh = (struct nlmsghdr *)buffer;
  663. nlh->nlmsg_seq = 0;
  664. nlh->nlmsg_pid = getpid();
  665. nlh->nlmsg_type = NLMSG_DONE;
  666. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  667. nlh->nlmsg_flags = 0;
  668. iov[0].iov_base = nlh;
  669. iov[0].iov_len = sizeof(*nlh);
  670. iov[1].iov_base = msg;
  671. iov[1].iov_len = size;
  672. memset(&message, 0, sizeof(message));
  673. message.msg_name = &addr;
  674. message.msg_namelen = sizeof(addr);
  675. message.msg_iov = iov;
  676. message.msg_iovlen = 2;
  677. return sendmsg(fd, &message, 0);
  678. }
  679. int main(void)
  680. {
  681. int fd, len, sock_opt;
  682. int error;
  683. struct cn_msg *message;
  684. struct pollfd pfd;
  685. struct nlmsghdr *incoming_msg;
  686. struct cn_msg *incoming_cn_msg;
  687. struct hv_kvp_msg *hv_msg;
  688. char *p;
  689. char *key_value;
  690. char *key_name;
  691. int op;
  692. int pool;
  693. daemon(1, 0);
  694. openlog("KVP", 0, LOG_USER);
  695. syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
  696. /*
  697. * Retrieve OS release information.
  698. */
  699. kvp_get_os_info();
  700. if (kvp_file_init()) {
  701. syslog(LOG_ERR, "Failed to initialize the pools");
  702. exit(-1);
  703. }
  704. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  705. if (fd < 0) {
  706. syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
  707. exit(-1);
  708. }
  709. addr.nl_family = AF_NETLINK;
  710. addr.nl_pad = 0;
  711. addr.nl_pid = 0;
  712. addr.nl_groups = CN_KVP_IDX;
  713. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  714. if (error < 0) {
  715. syslog(LOG_ERR, "bind failed; error:%d", error);
  716. close(fd);
  717. exit(-1);
  718. }
  719. sock_opt = addr.nl_groups;
  720. setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
  721. /*
  722. * Register ourselves with the kernel.
  723. */
  724. message = (struct cn_msg *)kvp_send_buffer;
  725. message->id.idx = CN_KVP_IDX;
  726. message->id.val = CN_KVP_VAL;
  727. hv_msg = (struct hv_kvp_msg *)message->data;
  728. hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
  729. message->ack = 0;
  730. message->len = sizeof(struct hv_kvp_msg);
  731. len = netlink_send(fd, message);
  732. if (len < 0) {
  733. syslog(LOG_ERR, "netlink_send failed; error:%d", len);
  734. close(fd);
  735. exit(-1);
  736. }
  737. pfd.fd = fd;
  738. while (1) {
  739. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  740. socklen_t addr_l = sizeof(addr);
  741. pfd.events = POLLIN;
  742. pfd.revents = 0;
  743. poll(&pfd, 1, -1);
  744. len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
  745. addr_p, &addr_l);
  746. if (len < 0 || addr.nl_pid) {
  747. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  748. addr.nl_pid, errno, strerror(errno));
  749. close(fd);
  750. return -1;
  751. }
  752. incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
  753. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  754. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  755. /*
  756. * We will use the KVP header information to pass back
  757. * the error from this daemon. So, first copy the state
  758. * and set the error code to success.
  759. */
  760. op = hv_msg->kvp_hdr.operation;
  761. pool = hv_msg->kvp_hdr.pool;
  762. hv_msg->error = HV_S_OK;
  763. if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
  764. /*
  765. * Driver is registering with us; stash away the version
  766. * information.
  767. */
  768. in_hand_shake = 0;
  769. p = (char *)hv_msg->body.kvp_register.version;
  770. lic_version = malloc(strlen(p) + 1);
  771. if (lic_version) {
  772. strcpy(lic_version, p);
  773. syslog(LOG_INFO, "KVP LIC Version: %s",
  774. lic_version);
  775. } else {
  776. syslog(LOG_ERR, "malloc failed");
  777. }
  778. continue;
  779. }
  780. switch (op) {
  781. case KVP_OP_SET:
  782. if (kvp_key_add_or_modify(pool,
  783. hv_msg->body.kvp_set.data.key,
  784. hv_msg->body.kvp_set.data.key_size,
  785. hv_msg->body.kvp_set.data.value,
  786. hv_msg->body.kvp_set.data.value_size))
  787. hv_msg->error = HV_S_CONT;
  788. break;
  789. case KVP_OP_GET:
  790. if (kvp_get_value(pool,
  791. hv_msg->body.kvp_set.data.key,
  792. hv_msg->body.kvp_set.data.key_size,
  793. hv_msg->body.kvp_set.data.value,
  794. hv_msg->body.kvp_set.data.value_size))
  795. hv_msg->error = HV_S_CONT;
  796. break;
  797. case KVP_OP_DELETE:
  798. if (kvp_key_delete(pool,
  799. hv_msg->body.kvp_delete.key,
  800. hv_msg->body.kvp_delete.key_size))
  801. hv_msg->error = HV_S_CONT;
  802. break;
  803. default:
  804. break;
  805. }
  806. if (op != KVP_OP_ENUMERATE)
  807. goto kvp_done;
  808. /*
  809. * If the pool is KVP_POOL_AUTO, dynamically generate
  810. * both the key and the value; if not read from the
  811. * appropriate pool.
  812. */
  813. if (pool != KVP_POOL_AUTO) {
  814. if (kvp_pool_enumerate(pool,
  815. hv_msg->body.kvp_enum_data.index,
  816. hv_msg->body.kvp_enum_data.data.key,
  817. HV_KVP_EXCHANGE_MAX_KEY_SIZE,
  818. hv_msg->body.kvp_enum_data.data.value,
  819. HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  820. hv_msg->error = HV_S_CONT;
  821. goto kvp_done;
  822. }
  823. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  824. key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
  825. key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
  826. switch (hv_msg->body.kvp_enum_data.index) {
  827. case FullyQualifiedDomainName:
  828. kvp_get_domain_name(key_value,
  829. HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  830. strcpy(key_name, "FullyQualifiedDomainName");
  831. break;
  832. case IntegrationServicesVersion:
  833. strcpy(key_name, "IntegrationServicesVersion");
  834. strcpy(key_value, lic_version);
  835. break;
  836. case NetworkAddressIPv4:
  837. kvp_get_ip_address(AF_INET, NULL, KVP_OP_ENUMERATE,
  838. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  839. strcpy(key_name, "NetworkAddressIPv4");
  840. break;
  841. case NetworkAddressIPv6:
  842. kvp_get_ip_address(AF_INET6, NULL, KVP_OP_ENUMERATE,
  843. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  844. strcpy(key_name, "NetworkAddressIPv6");
  845. break;
  846. case OSBuildNumber:
  847. strcpy(key_value, os_build);
  848. strcpy(key_name, "OSBuildNumber");
  849. break;
  850. case OSName:
  851. strcpy(key_value, os_name);
  852. strcpy(key_name, "OSName");
  853. break;
  854. case OSMajorVersion:
  855. strcpy(key_value, os_major);
  856. strcpy(key_name, "OSMajorVersion");
  857. break;
  858. case OSMinorVersion:
  859. strcpy(key_value, os_minor);
  860. strcpy(key_name, "OSMinorVersion");
  861. break;
  862. case OSVersion:
  863. strcpy(key_value, os_build);
  864. strcpy(key_name, "OSVersion");
  865. break;
  866. case ProcessorArchitecture:
  867. strcpy(key_value, processor_arch);
  868. strcpy(key_name, "ProcessorArchitecture");
  869. break;
  870. default:
  871. hv_msg->error = HV_S_CONT;
  872. break;
  873. }
  874. /*
  875. * Send the value back to the kernel. The response is
  876. * already in the receive buffer. Update the cn_msg header to
  877. * reflect the key value that has been added to the message
  878. */
  879. kvp_done:
  880. incoming_cn_msg->id.idx = CN_KVP_IDX;
  881. incoming_cn_msg->id.val = CN_KVP_VAL;
  882. incoming_cn_msg->ack = 0;
  883. incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
  884. len = netlink_send(fd, incoming_cn_msg);
  885. if (len < 0) {
  886. syslog(LOG_ERR, "net_link send failed; error:%d", len);
  887. exit(-1);
  888. }
  889. }
  890. }