hv_kvp_daemon.c 35 KB

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