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