hv_kvp_daemon.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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 <ctype.h>
  33. #include <errno.h>
  34. #include <arpa/inet.h>
  35. #include <linux/connector.h>
  36. #include <linux/hyperv.h>
  37. #include <linux/netlink.h>
  38. #include <ifaddrs.h>
  39. #include <netdb.h>
  40. #include <syslog.h>
  41. #include <sys/stat.h>
  42. #include <fcntl.h>
  43. #include <dirent.h>
  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. *
  54. */
  55. enum key_index {
  56. FullyQualifiedDomainName = 0,
  57. IntegrationServicesVersion, /*This key is serviced in the kernel*/
  58. NetworkAddressIPv4,
  59. NetworkAddressIPv6,
  60. OSBuildNumber,
  61. OSName,
  62. OSMajorVersion,
  63. OSMinorVersion,
  64. OSVersion,
  65. ProcessorArchitecture
  66. };
  67. enum {
  68. IPADDR = 0,
  69. NETMASK,
  70. GATEWAY,
  71. DNS
  72. };
  73. static char kvp_send_buffer[4096];
  74. static char kvp_recv_buffer[4096 * 2];
  75. static struct sockaddr_nl addr;
  76. static int in_hand_shake = 1;
  77. static char *os_name = "";
  78. static char *os_major = "";
  79. static char *os_minor = "";
  80. static char *processor_arch;
  81. static char *os_build;
  82. static char *lic_version = "Unknown version";
  83. static struct utsname uts_buf;
  84. /*
  85. * The location of the interface configuration file.
  86. */
  87. #define KVP_CONFIG_LOC "/var/opt/"
  88. #define MAX_FILE_NAME 100
  89. #define ENTRIES_PER_BLOCK 50
  90. struct kvp_record {
  91. char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
  92. char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
  93. };
  94. struct kvp_file_state {
  95. int fd;
  96. int num_blocks;
  97. struct kvp_record *records;
  98. int num_records;
  99. char fname[MAX_FILE_NAME];
  100. };
  101. static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
  102. static void kvp_acquire_lock(int pool)
  103. {
  104. struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
  105. fl.l_pid = getpid();
  106. if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
  107. syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
  108. exit(-1);
  109. }
  110. }
  111. static void kvp_release_lock(int pool)
  112. {
  113. struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
  114. fl.l_pid = getpid();
  115. if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
  116. perror("fcntl");
  117. syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
  118. exit(-1);
  119. }
  120. }
  121. static void kvp_update_file(int pool)
  122. {
  123. FILE *filep;
  124. size_t bytes_written;
  125. /*
  126. * We are going to write our in-memory registry out to
  127. * disk; acquire the lock first.
  128. */
  129. kvp_acquire_lock(pool);
  130. filep = fopen(kvp_file_info[pool].fname, "w");
  131. if (!filep) {
  132. kvp_release_lock(pool);
  133. syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
  134. exit(-1);
  135. }
  136. bytes_written = fwrite(kvp_file_info[pool].records,
  137. sizeof(struct kvp_record),
  138. kvp_file_info[pool].num_records, filep);
  139. fflush(filep);
  140. kvp_release_lock(pool);
  141. }
  142. static void kvp_update_mem_state(int pool)
  143. {
  144. FILE *filep;
  145. size_t records_read = 0;
  146. struct kvp_record *record = kvp_file_info[pool].records;
  147. struct kvp_record *readp;
  148. int num_blocks = kvp_file_info[pool].num_blocks;
  149. int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
  150. kvp_acquire_lock(pool);
  151. filep = fopen(kvp_file_info[pool].fname, "r");
  152. if (!filep) {
  153. kvp_release_lock(pool);
  154. syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
  155. exit(-1);
  156. }
  157. while (!feof(filep)) {
  158. readp = &record[records_read];
  159. records_read += fread(readp, sizeof(struct kvp_record),
  160. ENTRIES_PER_BLOCK * num_blocks,
  161. filep);
  162. if (!feof(filep)) {
  163. /*
  164. * We have more data to read.
  165. */
  166. num_blocks++;
  167. record = realloc(record, alloc_unit * num_blocks);
  168. if (record == NULL) {
  169. syslog(LOG_ERR, "malloc failed");
  170. exit(-1);
  171. }
  172. continue;
  173. }
  174. break;
  175. }
  176. kvp_file_info[pool].num_blocks = num_blocks;
  177. kvp_file_info[pool].records = record;
  178. kvp_file_info[pool].num_records = records_read;
  179. kvp_release_lock(pool);
  180. }
  181. static int kvp_file_init(void)
  182. {
  183. int fd;
  184. FILE *filep;
  185. size_t records_read;
  186. char *fname;
  187. struct kvp_record *record;
  188. struct kvp_record *readp;
  189. int num_blocks;
  190. int i;
  191. int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
  192. if (access("/var/opt/hyperv", F_OK)) {
  193. if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
  194. syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
  195. exit(-1);
  196. }
  197. }
  198. for (i = 0; i < KVP_POOL_COUNT; i++) {
  199. fname = kvp_file_info[i].fname;
  200. records_read = 0;
  201. num_blocks = 1;
  202. sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
  203. fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
  204. if (fd == -1)
  205. return 1;
  206. filep = fopen(fname, "r");
  207. if (!filep)
  208. return 1;
  209. record = malloc(alloc_unit * num_blocks);
  210. if (record == NULL) {
  211. fclose(filep);
  212. return 1;
  213. }
  214. while (!feof(filep)) {
  215. readp = &record[records_read];
  216. records_read += fread(readp, sizeof(struct kvp_record),
  217. ENTRIES_PER_BLOCK,
  218. filep);
  219. if (!feof(filep)) {
  220. /*
  221. * We have more data to read.
  222. */
  223. num_blocks++;
  224. record = realloc(record, alloc_unit *
  225. num_blocks);
  226. if (record == NULL) {
  227. fclose(filep);
  228. return 1;
  229. }
  230. continue;
  231. }
  232. break;
  233. }
  234. kvp_file_info[i].fd = fd;
  235. kvp_file_info[i].num_blocks = num_blocks;
  236. kvp_file_info[i].records = record;
  237. kvp_file_info[i].num_records = records_read;
  238. fclose(filep);
  239. }
  240. return 0;
  241. }
  242. static int kvp_key_delete(int pool, __u8 *key, int key_size)
  243. {
  244. int i;
  245. int j, k;
  246. int num_records;
  247. struct kvp_record *record;
  248. /*
  249. * First update the in-memory state.
  250. */
  251. kvp_update_mem_state(pool);
  252. num_records = kvp_file_info[pool].num_records;
  253. record = kvp_file_info[pool].records;
  254. for (i = 0; i < num_records; i++) {
  255. if (memcmp(key, record[i].key, key_size))
  256. continue;
  257. /*
  258. * Found a match; just move the remaining
  259. * entries up.
  260. */
  261. if (i == num_records) {
  262. kvp_file_info[pool].num_records--;
  263. kvp_update_file(pool);
  264. return 0;
  265. }
  266. j = i;
  267. k = j + 1;
  268. for (; k < num_records; k++) {
  269. strcpy(record[j].key, record[k].key);
  270. strcpy(record[j].value, record[k].value);
  271. j++;
  272. }
  273. kvp_file_info[pool].num_records--;
  274. kvp_update_file(pool);
  275. return 0;
  276. }
  277. return 1;
  278. }
  279. static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
  280. int value_size)
  281. {
  282. int i;
  283. int num_records;
  284. struct kvp_record *record;
  285. int num_blocks;
  286. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  287. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  288. return 1;
  289. /*
  290. * First update the in-memory state.
  291. */
  292. kvp_update_mem_state(pool);
  293. num_records = kvp_file_info[pool].num_records;
  294. record = kvp_file_info[pool].records;
  295. num_blocks = kvp_file_info[pool].num_blocks;
  296. for (i = 0; i < num_records; i++) {
  297. if (memcmp(key, record[i].key, key_size))
  298. continue;
  299. /*
  300. * Found a match; just update the value -
  301. * this is the modify case.
  302. */
  303. memcpy(record[i].value, value, value_size);
  304. kvp_update_file(pool);
  305. return 0;
  306. }
  307. /*
  308. * Need to add a new entry;
  309. */
  310. if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
  311. /* Need to allocate a larger array for reg entries. */
  312. record = realloc(record, sizeof(struct kvp_record) *
  313. ENTRIES_PER_BLOCK * (num_blocks + 1));
  314. if (record == NULL)
  315. return 1;
  316. kvp_file_info[pool].num_blocks++;
  317. }
  318. memcpy(record[i].value, value, value_size);
  319. memcpy(record[i].key, key, key_size);
  320. kvp_file_info[pool].records = record;
  321. kvp_file_info[pool].num_records++;
  322. kvp_update_file(pool);
  323. return 0;
  324. }
  325. static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
  326. int value_size)
  327. {
  328. int i;
  329. int num_records;
  330. struct kvp_record *record;
  331. if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
  332. (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  333. return 1;
  334. /*
  335. * First update the in-memory state.
  336. */
  337. kvp_update_mem_state(pool);
  338. num_records = kvp_file_info[pool].num_records;
  339. record = kvp_file_info[pool].records;
  340. for (i = 0; i < num_records; i++) {
  341. if (memcmp(key, record[i].key, key_size))
  342. continue;
  343. /*
  344. * Found a match; just copy the value out.
  345. */
  346. memcpy(value, record[i].value, value_size);
  347. return 0;
  348. }
  349. return 1;
  350. }
  351. static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
  352. __u8 *value, int value_size)
  353. {
  354. struct kvp_record *record;
  355. /*
  356. * First update our in-memory database.
  357. */
  358. kvp_update_mem_state(pool);
  359. record = kvp_file_info[pool].records;
  360. if (index >= kvp_file_info[pool].num_records) {
  361. return 1;
  362. }
  363. memcpy(key, record[index].key, key_size);
  364. memcpy(value, record[index].value, value_size);
  365. return 0;
  366. }
  367. void kvp_get_os_info(void)
  368. {
  369. FILE *file;
  370. char *p, buf[512];
  371. uname(&uts_buf);
  372. os_build = uts_buf.release;
  373. processor_arch = uts_buf.machine;
  374. /*
  375. * The current windows host (win7) expects the build
  376. * string to be of the form: x.y.z
  377. * Strip additional information we may have.
  378. */
  379. p = strchr(os_build, '-');
  380. if (p)
  381. *p = '\0';
  382. file = fopen("/etc/SuSE-release", "r");
  383. if (file != NULL)
  384. goto kvp_osinfo_found;
  385. file = fopen("/etc/redhat-release", "r");
  386. if (file != NULL)
  387. goto kvp_osinfo_found;
  388. /*
  389. * Add code for other supported platforms.
  390. */
  391. /*
  392. * We don't have information about the os.
  393. */
  394. os_name = uts_buf.sysname;
  395. return;
  396. kvp_osinfo_found:
  397. /* up to three lines */
  398. p = fgets(buf, sizeof(buf), file);
  399. if (p) {
  400. p = strchr(buf, '\n');
  401. if (p)
  402. *p = '\0';
  403. p = strdup(buf);
  404. if (!p)
  405. goto done;
  406. os_name = p;
  407. /* second line */
  408. p = fgets(buf, sizeof(buf), file);
  409. if (p) {
  410. p = strchr(buf, '\n');
  411. if (p)
  412. *p = '\0';
  413. p = strdup(buf);
  414. if (!p)
  415. goto done;
  416. os_major = p;
  417. /* third line */
  418. p = fgets(buf, sizeof(buf), file);
  419. if (p) {
  420. p = strchr(buf, '\n');
  421. if (p)
  422. *p = '\0';
  423. p = strdup(buf);
  424. if (p)
  425. os_minor = p;
  426. }
  427. }
  428. }
  429. done:
  430. fclose(file);
  431. return;
  432. }
  433. /*
  434. * Retrieve an interface name corresponding to the specified guid.
  435. * If there is a match, the function returns a pointer
  436. * to the interface name and if not, a NULL is returned.
  437. * If a match is found, the caller is responsible for
  438. * freeing the memory.
  439. */
  440. static char *kvp_get_if_name(char *guid)
  441. {
  442. DIR *dir;
  443. struct dirent *entry;
  444. FILE *file;
  445. char *p, *q, *x;
  446. char *if_name = NULL;
  447. char buf[256];
  448. char *kvp_net_dir = "/sys/class/net/";
  449. char dev_id[256];
  450. dir = opendir(kvp_net_dir);
  451. if (dir == NULL)
  452. return NULL;
  453. snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
  454. q = dev_id + strlen(kvp_net_dir);
  455. while ((entry = readdir(dir)) != NULL) {
  456. /*
  457. * Set the state for the next pass.
  458. */
  459. *q = '\0';
  460. strcat(dev_id, entry->d_name);
  461. strcat(dev_id, "/device/device_id");
  462. file = fopen(dev_id, "r");
  463. if (file == NULL)
  464. continue;
  465. p = fgets(buf, sizeof(buf), file);
  466. if (p) {
  467. x = strchr(p, '\n');
  468. if (x)
  469. *x = '\0';
  470. if (!strcmp(p, guid)) {
  471. /*
  472. * Found the guid match; return the interface
  473. * name. The caller will free the memory.
  474. */
  475. if_name = strdup(entry->d_name);
  476. fclose(file);
  477. break;
  478. }
  479. }
  480. fclose(file);
  481. }
  482. closedir(dir);
  483. return if_name;
  484. }
  485. /*
  486. * Retrieve the MAC address given the interface name.
  487. */
  488. static char *kvp_if_name_to_mac(char *if_name)
  489. {
  490. FILE *file;
  491. char *p, *x;
  492. char buf[256];
  493. char addr_file[256];
  494. int i;
  495. char *mac_addr = NULL;
  496. snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
  497. if_name, "/address");
  498. file = fopen(addr_file, "r");
  499. if (file == NULL)
  500. return NULL;
  501. p = fgets(buf, sizeof(buf), file);
  502. if (p) {
  503. x = strchr(p, '\n');
  504. if (x)
  505. *x = '\0';
  506. for (i = 0; i < strlen(p); i++)
  507. p[i] = toupper(p[i]);
  508. mac_addr = strdup(p);
  509. }
  510. fclose(file);
  511. return mac_addr;
  512. }
  513. static void kvp_process_ipconfig_file(char *cmd,
  514. char *config_buf, int len,
  515. int element_size, int offset)
  516. {
  517. char buf[256];
  518. char *p;
  519. char *x;
  520. FILE *file;
  521. /*
  522. * First execute the command.
  523. */
  524. file = popen(cmd, "r");
  525. if (file == NULL)
  526. return;
  527. if (offset == 0)
  528. memset(config_buf, 0, len);
  529. while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
  530. if ((len - strlen(config_buf)) < (element_size + 1))
  531. break;
  532. x = strchr(p, '\n');
  533. *x = '\0';
  534. strcat(config_buf, p);
  535. strcat(config_buf, ";");
  536. }
  537. pclose(file);
  538. }
  539. static void kvp_get_ipconfig_info(char *if_name,
  540. struct hv_kvp_ipaddr_value *buffer)
  541. {
  542. char cmd[512];
  543. char dhcp_info[128];
  544. char *p;
  545. FILE *file;
  546. /*
  547. * Get the address of default gateway (ipv4).
  548. */
  549. sprintf(cmd, "%s %s", "ip route show dev", if_name);
  550. strcat(cmd, " | awk '/default/ {print $3 }'");
  551. /*
  552. * Execute the command to gather gateway info.
  553. */
  554. kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
  555. (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
  556. /*
  557. * Get the address of default gateway (ipv6).
  558. */
  559. sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
  560. strcat(cmd, " | awk '/default/ {print $3 }'");
  561. /*
  562. * Execute the command to gather gateway info (ipv6).
  563. */
  564. kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
  565. (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
  566. /*
  567. * Gather the DNS state.
  568. * Since there is no standard way to get this information
  569. * across various distributions of interest; we just invoke
  570. * an external script that needs to be ported across distros
  571. * of interest.
  572. *
  573. * Following is the expected format of the information from the script:
  574. *
  575. * ipaddr1 (nameserver1)
  576. * ipaddr2 (nameserver2)
  577. * .
  578. * .
  579. */
  580. sprintf(cmd, "%s", "hv_get_dns_info");
  581. /*
  582. * Execute the command to gather DNS info.
  583. */
  584. kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
  585. (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
  586. /*
  587. * Gather the DHCP state.
  588. * We will gather this state by invoking an external script.
  589. * The parameter to the script is the interface name.
  590. * Here is the expected output:
  591. *
  592. * Enabled: DHCP enabled.
  593. */
  594. sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
  595. file = popen(cmd, "r");
  596. if (file == NULL)
  597. return;
  598. p = fgets(dhcp_info, sizeof(dhcp_info), file);
  599. if (p == NULL) {
  600. pclose(file);
  601. return;
  602. }
  603. if (!strncmp(p, "Enabled", 7))
  604. buffer->dhcp_enabled = 1;
  605. else
  606. buffer->dhcp_enabled = 0;
  607. pclose(file);
  608. }
  609. static unsigned int hweight32(unsigned int *w)
  610. {
  611. unsigned int res = *w - ((*w >> 1) & 0x55555555);
  612. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  613. res = (res + (res >> 4)) & 0x0F0F0F0F;
  614. res = res + (res >> 8);
  615. return (res + (res >> 16)) & 0x000000FF;
  616. }
  617. static int kvp_process_ip_address(void *addrp,
  618. int family, char *buffer,
  619. int length, int *offset)
  620. {
  621. struct sockaddr_in *addr;
  622. struct sockaddr_in6 *addr6;
  623. int addr_length;
  624. char tmp[50];
  625. const char *str;
  626. if (family == AF_INET) {
  627. addr = (struct sockaddr_in *)addrp;
  628. str = inet_ntop(family, &addr->sin_addr, tmp, 50);
  629. addr_length = INET_ADDRSTRLEN;
  630. } else {
  631. addr6 = (struct sockaddr_in6 *)addrp;
  632. str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
  633. addr_length = INET6_ADDRSTRLEN;
  634. }
  635. if ((length - *offset) < addr_length + 1)
  636. return 1;
  637. if (str == NULL) {
  638. strcpy(buffer, "inet_ntop failed\n");
  639. return 1;
  640. }
  641. if (*offset == 0)
  642. strcpy(buffer, tmp);
  643. else
  644. strcat(buffer, tmp);
  645. strcat(buffer, ";");
  646. *offset += strlen(str) + 1;
  647. return 0;
  648. }
  649. static int
  650. kvp_get_ip_info(int family, char *if_name, int op,
  651. void *out_buffer, int length)
  652. {
  653. struct ifaddrs *ifap;
  654. struct ifaddrs *curp;
  655. int offset = 0;
  656. int sn_offset = 0;
  657. int error = 0;
  658. char *buffer;
  659. struct hv_kvp_ipaddr_value *ip_buffer;
  660. char cidr_mask[5]; /* /xyz */
  661. int weight;
  662. int i;
  663. unsigned int *w;
  664. char *sn_str;
  665. struct sockaddr_in6 *addr6;
  666. if (op == KVP_OP_ENUMERATE) {
  667. buffer = out_buffer;
  668. } else {
  669. ip_buffer = out_buffer;
  670. buffer = (char *)ip_buffer->ip_addr;
  671. ip_buffer->addr_family = 0;
  672. }
  673. /*
  674. * On entry into this function, the buffer is capable of holding the
  675. * maximum key value.
  676. */
  677. if (getifaddrs(&ifap)) {
  678. strcpy(buffer, "getifaddrs failed\n");
  679. return 1;
  680. }
  681. curp = ifap;
  682. while (curp != NULL) {
  683. if (curp->ifa_addr == NULL) {
  684. curp = curp->ifa_next;
  685. continue;
  686. }
  687. if ((if_name != NULL) &&
  688. (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
  689. /*
  690. * We want info about a specific interface;
  691. * just continue.
  692. */
  693. curp = curp->ifa_next;
  694. continue;
  695. }
  696. /*
  697. * We only support two address families: AF_INET and AF_INET6.
  698. * If a family value of 0 is specified, we collect both
  699. * supported address families; if not we gather info on
  700. * the specified address family.
  701. */
  702. if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
  703. curp = curp->ifa_next;
  704. continue;
  705. }
  706. if ((curp->ifa_addr->sa_family != AF_INET) &&
  707. (curp->ifa_addr->sa_family != AF_INET6)) {
  708. curp = curp->ifa_next;
  709. continue;
  710. }
  711. if (op == KVP_OP_GET_IP_INFO) {
  712. /*
  713. * Gather info other than the IP address.
  714. * IP address info will be gathered later.
  715. */
  716. if (curp->ifa_addr->sa_family == AF_INET) {
  717. ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
  718. /*
  719. * Get subnet info.
  720. */
  721. error = kvp_process_ip_address(
  722. curp->ifa_netmask,
  723. AF_INET,
  724. (char *)
  725. ip_buffer->sub_net,
  726. length,
  727. &sn_offset);
  728. if (error)
  729. goto gather_ipaddr;
  730. } else {
  731. ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
  732. /*
  733. * Get subnet info in CIDR format.
  734. */
  735. weight = 0;
  736. sn_str = (char *)ip_buffer->sub_net;
  737. addr6 = (struct sockaddr_in6 *)
  738. curp->ifa_netmask;
  739. w = addr6->sin6_addr.s6_addr32;
  740. for (i = 0; i < 4; i++)
  741. weight += hweight32(&w[i]);
  742. sprintf(cidr_mask, "/%d", weight);
  743. if ((length - sn_offset) <
  744. (strlen(cidr_mask) + 1))
  745. goto gather_ipaddr;
  746. if (sn_offset == 0)
  747. strcpy(sn_str, cidr_mask);
  748. else
  749. strcat(sn_str, cidr_mask);
  750. strcat((char *)ip_buffer->sub_net, ";");
  751. sn_offset += strlen(sn_str) + 1;
  752. }
  753. /*
  754. * Collect other ip related configuration info.
  755. */
  756. kvp_get_ipconfig_info(if_name, ip_buffer);
  757. }
  758. gather_ipaddr:
  759. error = kvp_process_ip_address(curp->ifa_addr,
  760. curp->ifa_addr->sa_family,
  761. buffer,
  762. length, &offset);
  763. if (error)
  764. goto getaddr_done;
  765. curp = curp->ifa_next;
  766. }
  767. getaddr_done:
  768. freeifaddrs(ifap);
  769. return error;
  770. }
  771. static int expand_ipv6(char *addr, int type)
  772. {
  773. int ret;
  774. struct in6_addr v6_addr;
  775. ret = inet_pton(AF_INET6, addr, &v6_addr);
  776. if (ret != 1) {
  777. if (type == NETMASK)
  778. return 1;
  779. return 0;
  780. }
  781. sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
  782. "%02x%02x:%02x%02x:%02x%02x",
  783. (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
  784. (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
  785. (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
  786. (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
  787. (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
  788. (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
  789. (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
  790. (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
  791. return 1;
  792. }
  793. static int is_ipv4(char *addr)
  794. {
  795. int ret;
  796. struct in_addr ipv4_addr;
  797. ret = inet_pton(AF_INET, addr, &ipv4_addr);
  798. if (ret == 1)
  799. return 1;
  800. return 0;
  801. }
  802. static int parse_ip_val_buffer(char *in_buf, int *offset,
  803. char *out_buf, int out_len)
  804. {
  805. char *x;
  806. char *start;
  807. /*
  808. * in_buf has sequence of characters that are seperated by
  809. * the character ';'. The last sequence does not have the
  810. * terminating ";" character.
  811. */
  812. start = in_buf + *offset;
  813. x = strchr(start, ';');
  814. if (x)
  815. *x = 0;
  816. else
  817. x = start + strlen(start);
  818. if (strlen(start) != 0) {
  819. int i = 0;
  820. /*
  821. * Get rid of leading spaces.
  822. */
  823. while (start[i] == ' ')
  824. i++;
  825. if ((x - start) <= out_len) {
  826. strcpy(out_buf, (start + i));
  827. *offset += (x - start) + 1;
  828. return 1;
  829. }
  830. }
  831. return 0;
  832. }
  833. static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
  834. {
  835. int ret;
  836. ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
  837. if (ret < 0)
  838. return HV_E_FAIL;
  839. return 0;
  840. }
  841. static int process_ip_string(FILE *f, char *ip_string, int type)
  842. {
  843. int error = 0;
  844. char addr[INET6_ADDRSTRLEN];
  845. int i = 0;
  846. int j = 0;
  847. char str[256];
  848. char sub_str[10];
  849. int offset = 0;
  850. memset(addr, 0, sizeof(addr));
  851. while (parse_ip_val_buffer(ip_string, &offset, addr,
  852. (MAX_IP_ADDR_SIZE * 2))) {
  853. sub_str[0] = 0;
  854. if (is_ipv4(addr)) {
  855. switch (type) {
  856. case IPADDR:
  857. snprintf(str, sizeof(str), "%s", "IPADDR");
  858. break;
  859. case NETMASK:
  860. snprintf(str, sizeof(str), "%s", "NETMASK");
  861. break;
  862. case GATEWAY:
  863. snprintf(str, sizeof(str), "%s", "GATEWAY");
  864. break;
  865. case DNS:
  866. snprintf(str, sizeof(str), "%s", "DNS");
  867. break;
  868. }
  869. if (i != 0) {
  870. if (type != DNS) {
  871. snprintf(sub_str, sizeof(sub_str),
  872. "_%d", i++);
  873. } else {
  874. snprintf(sub_str, sizeof(sub_str),
  875. "%d", ++i);
  876. }
  877. } else if (type == DNS) {
  878. snprintf(sub_str, sizeof(sub_str), "%d", ++i);
  879. }
  880. } else if (expand_ipv6(addr, type)) {
  881. switch (type) {
  882. case IPADDR:
  883. snprintf(str, sizeof(str), "%s", "IPV6ADDR");
  884. break;
  885. case NETMASK:
  886. snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
  887. break;
  888. case GATEWAY:
  889. snprintf(str, sizeof(str), "%s",
  890. "IPV6_DEFAULTGW");
  891. break;
  892. case DNS:
  893. snprintf(str, sizeof(str), "%s", "DNS");
  894. break;
  895. }
  896. if ((j != 0) || (type == DNS)) {
  897. if (type != DNS) {
  898. snprintf(sub_str, sizeof(sub_str),
  899. "_%d", j++);
  900. } else {
  901. snprintf(sub_str, sizeof(sub_str),
  902. "%d", ++i);
  903. }
  904. } else if (type == DNS) {
  905. snprintf(sub_str, sizeof(sub_str),
  906. "%d", ++i);
  907. }
  908. } else {
  909. return HV_INVALIDARG;
  910. }
  911. error = kvp_write_file(f, str, sub_str, addr);
  912. if (error)
  913. return error;
  914. memset(addr, 0, sizeof(addr));
  915. }
  916. return 0;
  917. }
  918. static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
  919. {
  920. int error = 0;
  921. char if_file[128];
  922. FILE *file;
  923. char cmd[512];
  924. char *mac_addr;
  925. /*
  926. * Set the configuration for the specified interface with
  927. * the information provided. Since there is no standard
  928. * way to configure an interface, we will have an external
  929. * script that does the job of configuring the interface and
  930. * flushing the configuration.
  931. *
  932. * The parameters passed to this external script are:
  933. * 1. A configuration file that has the specified configuration.
  934. *
  935. * We will embed the name of the interface in the configuration
  936. * file: ifcfg-ethx (where ethx is the interface name).
  937. *
  938. * The information provided here may be more than what is needed
  939. * in a given distro to configure the interface and so are free
  940. * ignore information that may not be relevant.
  941. *
  942. * Here is the format of the ip configuration file:
  943. *
  944. * HWADDR=macaddr
  945. * IF_NAME=interface name
  946. * DHCP=yes (This is optional; if yes, DHCP is configured)
  947. *
  948. * IPADDR=ipaddr1
  949. * IPADDR_1=ipaddr2
  950. * IPADDR_x=ipaddry (where y = x + 1)
  951. *
  952. * NETMASK=netmask1
  953. * NETMASK_x=netmasky (where y = x + 1)
  954. *
  955. * GATEWAY=ipaddr1
  956. * GATEWAY_x=ipaddry (where y = x + 1)
  957. *
  958. * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
  959. *
  960. * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
  961. * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
  962. * IPV6NETMASK.
  963. *
  964. * The host can specify multiple ipv4 and ipv6 addresses to be
  965. * configured for the interface. Furthermore, the configuration
  966. * needs to be persistent. A subsequent GET call on the interface
  967. * is expected to return the configuration that is set via the SET
  968. * call.
  969. */
  970. snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
  971. "hyperv/ifcfg-", if_name);
  972. file = fopen(if_file, "w");
  973. if (file == NULL) {
  974. syslog(LOG_ERR, "Failed to open config file");
  975. return HV_E_FAIL;
  976. }
  977. /*
  978. * First write out the MAC address.
  979. */
  980. mac_addr = kvp_if_name_to_mac(if_name);
  981. if (mac_addr == NULL) {
  982. error = HV_E_FAIL;
  983. goto setval_error;
  984. }
  985. error = kvp_write_file(file, "HWADDR", "", mac_addr);
  986. if (error)
  987. goto setval_error;
  988. error = kvp_write_file(file, "IF_NAME", "", if_name);
  989. if (error)
  990. goto setval_error;
  991. if (new_val->dhcp_enabled) {
  992. error = kvp_write_file(file, "DHCP", "", "yes");
  993. if (error)
  994. goto setval_error;
  995. /*
  996. * We are done!.
  997. */
  998. goto setval_done;
  999. }
  1000. /*
  1001. * Write the configuration for ipaddress, netmask, gateway and
  1002. * name servers.
  1003. */
  1004. error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
  1005. if (error)
  1006. goto setval_error;
  1007. error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
  1008. if (error)
  1009. goto setval_error;
  1010. error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
  1011. if (error)
  1012. goto setval_error;
  1013. error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
  1014. if (error)
  1015. goto setval_error;
  1016. setval_done:
  1017. free(mac_addr);
  1018. fclose(file);
  1019. /*
  1020. * Now that we have populated the configuration file,
  1021. * invoke the external script to do its magic.
  1022. */
  1023. snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
  1024. system(cmd);
  1025. return 0;
  1026. setval_error:
  1027. syslog(LOG_ERR, "Failed to write config file");
  1028. free(mac_addr);
  1029. fclose(file);
  1030. return error;
  1031. }
  1032. static int
  1033. kvp_get_domain_name(char *buffer, int length)
  1034. {
  1035. struct addrinfo hints, *info ;
  1036. int error = 0;
  1037. gethostname(buffer, length);
  1038. memset(&hints, 0, sizeof(hints));
  1039. hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
  1040. hints.ai_socktype = SOCK_STREAM;
  1041. hints.ai_flags = AI_CANONNAME;
  1042. error = getaddrinfo(buffer, NULL, &hints, &info);
  1043. if (error != 0) {
  1044. strcpy(buffer, "getaddrinfo failed\n");
  1045. return error;
  1046. }
  1047. strcpy(buffer, info->ai_canonname);
  1048. freeaddrinfo(info);
  1049. return error;
  1050. }
  1051. static int
  1052. netlink_send(int fd, struct cn_msg *msg)
  1053. {
  1054. struct nlmsghdr *nlh;
  1055. unsigned int size;
  1056. struct msghdr message;
  1057. char buffer[64];
  1058. struct iovec iov[2];
  1059. size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
  1060. nlh = (struct nlmsghdr *)buffer;
  1061. nlh->nlmsg_seq = 0;
  1062. nlh->nlmsg_pid = getpid();
  1063. nlh->nlmsg_type = NLMSG_DONE;
  1064. nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
  1065. nlh->nlmsg_flags = 0;
  1066. iov[0].iov_base = nlh;
  1067. iov[0].iov_len = sizeof(*nlh);
  1068. iov[1].iov_base = msg;
  1069. iov[1].iov_len = size;
  1070. memset(&message, 0, sizeof(message));
  1071. message.msg_name = &addr;
  1072. message.msg_namelen = sizeof(addr);
  1073. message.msg_iov = iov;
  1074. message.msg_iovlen = 2;
  1075. return sendmsg(fd, &message, 0);
  1076. }
  1077. int main(void)
  1078. {
  1079. int fd, len, sock_opt;
  1080. int error;
  1081. struct cn_msg *message;
  1082. struct pollfd pfd;
  1083. struct nlmsghdr *incoming_msg;
  1084. struct cn_msg *incoming_cn_msg;
  1085. struct hv_kvp_msg *hv_msg;
  1086. char *p;
  1087. char *key_value;
  1088. char *key_name;
  1089. int op;
  1090. int pool;
  1091. char *if_name;
  1092. struct hv_kvp_ipaddr_value *kvp_ip_val;
  1093. daemon(1, 0);
  1094. openlog("KVP", 0, LOG_USER);
  1095. syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
  1096. /*
  1097. * Retrieve OS release information.
  1098. */
  1099. kvp_get_os_info();
  1100. if (kvp_file_init()) {
  1101. syslog(LOG_ERR, "Failed to initialize the pools");
  1102. exit(-1);
  1103. }
  1104. fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
  1105. if (fd < 0) {
  1106. syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
  1107. exit(-1);
  1108. }
  1109. addr.nl_family = AF_NETLINK;
  1110. addr.nl_pad = 0;
  1111. addr.nl_pid = 0;
  1112. addr.nl_groups = CN_KVP_IDX;
  1113. error = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  1114. if (error < 0) {
  1115. syslog(LOG_ERR, "bind failed; error:%d", error);
  1116. close(fd);
  1117. exit(-1);
  1118. }
  1119. sock_opt = addr.nl_groups;
  1120. setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
  1121. /*
  1122. * Register ourselves with the kernel.
  1123. */
  1124. message = (struct cn_msg *)kvp_send_buffer;
  1125. message->id.idx = CN_KVP_IDX;
  1126. message->id.val = CN_KVP_VAL;
  1127. hv_msg = (struct hv_kvp_msg *)message->data;
  1128. hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
  1129. message->ack = 0;
  1130. message->len = sizeof(struct hv_kvp_msg);
  1131. len = netlink_send(fd, message);
  1132. if (len < 0) {
  1133. syslog(LOG_ERR, "netlink_send failed; error:%d", len);
  1134. close(fd);
  1135. exit(-1);
  1136. }
  1137. pfd.fd = fd;
  1138. while (1) {
  1139. struct sockaddr *addr_p = (struct sockaddr *) &addr;
  1140. socklen_t addr_l = sizeof(addr);
  1141. pfd.events = POLLIN;
  1142. pfd.revents = 0;
  1143. poll(&pfd, 1, -1);
  1144. len = recvfrom(fd, kvp_recv_buffer, sizeof(kvp_recv_buffer), 0,
  1145. addr_p, &addr_l);
  1146. if (len < 0 || addr.nl_pid) {
  1147. syslog(LOG_ERR, "recvfrom failed; pid:%u error:%d %s",
  1148. addr.nl_pid, errno, strerror(errno));
  1149. close(fd);
  1150. return -1;
  1151. }
  1152. incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
  1153. incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
  1154. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  1155. /*
  1156. * We will use the KVP header information to pass back
  1157. * the error from this daemon. So, first copy the state
  1158. * and set the error code to success.
  1159. */
  1160. op = hv_msg->kvp_hdr.operation;
  1161. pool = hv_msg->kvp_hdr.pool;
  1162. hv_msg->error = HV_S_OK;
  1163. if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
  1164. /*
  1165. * Driver is registering with us; stash away the version
  1166. * information.
  1167. */
  1168. in_hand_shake = 0;
  1169. p = (char *)hv_msg->body.kvp_register.version;
  1170. lic_version = malloc(strlen(p) + 1);
  1171. if (lic_version) {
  1172. strcpy(lic_version, p);
  1173. syslog(LOG_INFO, "KVP LIC Version: %s",
  1174. lic_version);
  1175. } else {
  1176. syslog(LOG_ERR, "malloc failed");
  1177. }
  1178. continue;
  1179. }
  1180. switch (op) {
  1181. case KVP_OP_SET_IP_INFO:
  1182. kvp_ip_val = &hv_msg->body.kvp_ip_val;
  1183. if_name = kvp_get_if_name(
  1184. (char *)kvp_ip_val->adapter_id);
  1185. if (if_name == NULL) {
  1186. /*
  1187. * We could not map the guid to an
  1188. * interface name; return error.
  1189. */
  1190. hv_msg->error = HV_GUID_NOTFOUND;
  1191. break;
  1192. }
  1193. error = kvp_set_ip_info(if_name, kvp_ip_val);
  1194. if (error)
  1195. hv_msg->error = error;
  1196. free(if_name);
  1197. break;
  1198. case KVP_OP_SET:
  1199. if (kvp_key_add_or_modify(pool,
  1200. hv_msg->body.kvp_set.data.key,
  1201. hv_msg->body.kvp_set.data.key_size,
  1202. hv_msg->body.kvp_set.data.value,
  1203. hv_msg->body.kvp_set.data.value_size))
  1204. hv_msg->error = HV_S_CONT;
  1205. break;
  1206. case KVP_OP_GET:
  1207. if (kvp_get_value(pool,
  1208. hv_msg->body.kvp_set.data.key,
  1209. hv_msg->body.kvp_set.data.key_size,
  1210. hv_msg->body.kvp_set.data.value,
  1211. hv_msg->body.kvp_set.data.value_size))
  1212. hv_msg->error = HV_S_CONT;
  1213. break;
  1214. case KVP_OP_DELETE:
  1215. if (kvp_key_delete(pool,
  1216. hv_msg->body.kvp_delete.key,
  1217. hv_msg->body.kvp_delete.key_size))
  1218. hv_msg->error = HV_S_CONT;
  1219. break;
  1220. default:
  1221. break;
  1222. }
  1223. if (op != KVP_OP_ENUMERATE)
  1224. goto kvp_done;
  1225. /*
  1226. * If the pool is KVP_POOL_AUTO, dynamically generate
  1227. * both the key and the value; if not read from the
  1228. * appropriate pool.
  1229. */
  1230. if (pool != KVP_POOL_AUTO) {
  1231. if (kvp_pool_enumerate(pool,
  1232. hv_msg->body.kvp_enum_data.index,
  1233. hv_msg->body.kvp_enum_data.data.key,
  1234. HV_KVP_EXCHANGE_MAX_KEY_SIZE,
  1235. hv_msg->body.kvp_enum_data.data.value,
  1236. HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  1237. hv_msg->error = HV_S_CONT;
  1238. goto kvp_done;
  1239. }
  1240. hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
  1241. key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
  1242. key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
  1243. switch (hv_msg->body.kvp_enum_data.index) {
  1244. case FullyQualifiedDomainName:
  1245. kvp_get_domain_name(key_value,
  1246. HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  1247. strcpy(key_name, "FullyQualifiedDomainName");
  1248. break;
  1249. case IntegrationServicesVersion:
  1250. strcpy(key_name, "IntegrationServicesVersion");
  1251. strcpy(key_value, lic_version);
  1252. break;
  1253. case NetworkAddressIPv4:
  1254. kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
  1255. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  1256. strcpy(key_name, "NetworkAddressIPv4");
  1257. break;
  1258. case NetworkAddressIPv6:
  1259. kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
  1260. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  1261. strcpy(key_name, "NetworkAddressIPv6");
  1262. break;
  1263. case OSBuildNumber:
  1264. strcpy(key_value, os_build);
  1265. strcpy(key_name, "OSBuildNumber");
  1266. break;
  1267. case OSName:
  1268. strcpy(key_value, os_name);
  1269. strcpy(key_name, "OSName");
  1270. break;
  1271. case OSMajorVersion:
  1272. strcpy(key_value, os_major);
  1273. strcpy(key_name, "OSMajorVersion");
  1274. break;
  1275. case OSMinorVersion:
  1276. strcpy(key_value, os_minor);
  1277. strcpy(key_name, "OSMinorVersion");
  1278. break;
  1279. case OSVersion:
  1280. strcpy(key_value, os_build);
  1281. strcpy(key_name, "OSVersion");
  1282. break;
  1283. case ProcessorArchitecture:
  1284. strcpy(key_value, processor_arch);
  1285. strcpy(key_name, "ProcessorArchitecture");
  1286. break;
  1287. default:
  1288. hv_msg->error = HV_S_CONT;
  1289. break;
  1290. }
  1291. /*
  1292. * Send the value back to the kernel. The response is
  1293. * already in the receive buffer. Update the cn_msg header to
  1294. * reflect the key value that has been added to the message
  1295. */
  1296. kvp_done:
  1297. incoming_cn_msg->id.idx = CN_KVP_IDX;
  1298. incoming_cn_msg->id.val = CN_KVP_VAL;
  1299. incoming_cn_msg->ack = 0;
  1300. incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
  1301. len = netlink_send(fd, incoming_cn_msg);
  1302. if (len < 0) {
  1303. syslog(LOG_ERR, "net_link send failed; error:%d", len);
  1304. exit(-1);
  1305. }
  1306. }
  1307. }