hv_kvp_daemon.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  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 = 1;
  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. * http://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, 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. kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
  1191. if (kvp_fd < 0) {
  1192. syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
  1193. errno, strerror(errno));
  1194. exit(EXIT_FAILURE);
  1195. }
  1196. /*
  1197. * Retrieve OS release information.
  1198. */
  1199. kvp_get_os_info();
  1200. /*
  1201. * Cache Fully Qualified Domain Name because getaddrinfo takes an
  1202. * unpredictable amount of time to finish.
  1203. */
  1204. kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
  1205. if (kvp_file_init()) {
  1206. syslog(LOG_ERR, "Failed to initialize the pools");
  1207. exit(EXIT_FAILURE);
  1208. }
  1209. /*
  1210. * Register ourselves with the kernel.
  1211. */
  1212. hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
  1213. len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
  1214. if (len != sizeof(struct hv_kvp_msg)) {
  1215. syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
  1216. errno, strerror(errno));
  1217. close(kvp_fd);
  1218. exit(EXIT_FAILURE);
  1219. }
  1220. pfd.fd = kvp_fd;
  1221. while (1) {
  1222. pfd.events = POLLIN;
  1223. pfd.revents = 0;
  1224. if (poll(&pfd, 1, -1) < 0) {
  1225. syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
  1226. if (errno == EINVAL) {
  1227. close(kvp_fd);
  1228. exit(EXIT_FAILURE);
  1229. }
  1230. else
  1231. continue;
  1232. }
  1233. len = read(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
  1234. if (len != sizeof(struct hv_kvp_msg)) {
  1235. syslog(LOG_ERR, "read failed; error:%d %s",
  1236. errno, strerror(errno));
  1237. close(kvp_fd);
  1238. return EXIT_FAILURE;
  1239. }
  1240. /*
  1241. * We will use the KVP header information to pass back
  1242. * the error from this daemon. So, first copy the state
  1243. * and set the error code to success.
  1244. */
  1245. op = hv_msg->kvp_hdr.operation;
  1246. pool = hv_msg->kvp_hdr.pool;
  1247. hv_msg->error = HV_S_OK;
  1248. if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
  1249. /*
  1250. * Driver is registering with us; stash away the version
  1251. * information.
  1252. */
  1253. in_hand_shake = 0;
  1254. p = (char *)hv_msg->body.kvp_register.version;
  1255. lic_version = malloc(strlen(p) + 1);
  1256. if (lic_version) {
  1257. strcpy(lic_version, p);
  1258. syslog(LOG_INFO, "KVP LIC Version: %s",
  1259. lic_version);
  1260. } else {
  1261. syslog(LOG_ERR, "malloc failed");
  1262. }
  1263. continue;
  1264. }
  1265. switch (op) {
  1266. case KVP_OP_GET_IP_INFO:
  1267. kvp_ip_val = &hv_msg->body.kvp_ip_val;
  1268. error = kvp_mac_to_ip(kvp_ip_val);
  1269. if (error)
  1270. hv_msg->error = error;
  1271. break;
  1272. case KVP_OP_SET_IP_INFO:
  1273. kvp_ip_val = &hv_msg->body.kvp_ip_val;
  1274. if_name = kvp_get_if_name(
  1275. (char *)kvp_ip_val->adapter_id);
  1276. if (if_name == NULL) {
  1277. /*
  1278. * We could not map the guid to an
  1279. * interface name; return error.
  1280. */
  1281. hv_msg->error = HV_GUID_NOTFOUND;
  1282. break;
  1283. }
  1284. error = kvp_set_ip_info(if_name, kvp_ip_val);
  1285. if (error)
  1286. hv_msg->error = error;
  1287. free(if_name);
  1288. break;
  1289. case KVP_OP_SET:
  1290. if (kvp_key_add_or_modify(pool,
  1291. hv_msg->body.kvp_set.data.key,
  1292. hv_msg->body.kvp_set.data.key_size,
  1293. hv_msg->body.kvp_set.data.value,
  1294. hv_msg->body.kvp_set.data.value_size))
  1295. hv_msg->error = HV_S_CONT;
  1296. break;
  1297. case KVP_OP_GET:
  1298. if (kvp_get_value(pool,
  1299. hv_msg->body.kvp_set.data.key,
  1300. hv_msg->body.kvp_set.data.key_size,
  1301. hv_msg->body.kvp_set.data.value,
  1302. hv_msg->body.kvp_set.data.value_size))
  1303. hv_msg->error = HV_S_CONT;
  1304. break;
  1305. case KVP_OP_DELETE:
  1306. if (kvp_key_delete(pool,
  1307. hv_msg->body.kvp_delete.key,
  1308. hv_msg->body.kvp_delete.key_size))
  1309. hv_msg->error = HV_S_CONT;
  1310. break;
  1311. default:
  1312. break;
  1313. }
  1314. if (op != KVP_OP_ENUMERATE)
  1315. goto kvp_done;
  1316. /*
  1317. * If the pool is KVP_POOL_AUTO, dynamically generate
  1318. * both the key and the value; if not read from the
  1319. * appropriate pool.
  1320. */
  1321. if (pool != KVP_POOL_AUTO) {
  1322. if (kvp_pool_enumerate(pool,
  1323. hv_msg->body.kvp_enum_data.index,
  1324. hv_msg->body.kvp_enum_data.data.key,
  1325. HV_KVP_EXCHANGE_MAX_KEY_SIZE,
  1326. hv_msg->body.kvp_enum_data.data.value,
  1327. HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  1328. hv_msg->error = HV_S_CONT;
  1329. goto kvp_done;
  1330. }
  1331. key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
  1332. key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
  1333. switch (hv_msg->body.kvp_enum_data.index) {
  1334. case FullyQualifiedDomainName:
  1335. strcpy(key_value, full_domain_name);
  1336. strcpy(key_name, "FullyQualifiedDomainName");
  1337. break;
  1338. case IntegrationServicesVersion:
  1339. strcpy(key_name, "IntegrationServicesVersion");
  1340. strcpy(key_value, lic_version);
  1341. break;
  1342. case NetworkAddressIPv4:
  1343. kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
  1344. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  1345. strcpy(key_name, "NetworkAddressIPv4");
  1346. break;
  1347. case NetworkAddressIPv6:
  1348. kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
  1349. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  1350. strcpy(key_name, "NetworkAddressIPv6");
  1351. break;
  1352. case OSBuildNumber:
  1353. strcpy(key_value, os_build);
  1354. strcpy(key_name, "OSBuildNumber");
  1355. break;
  1356. case OSName:
  1357. strcpy(key_value, os_name);
  1358. strcpy(key_name, "OSName");
  1359. break;
  1360. case OSMajorVersion:
  1361. strcpy(key_value, os_major);
  1362. strcpy(key_name, "OSMajorVersion");
  1363. break;
  1364. case OSMinorVersion:
  1365. strcpy(key_value, os_minor);
  1366. strcpy(key_name, "OSMinorVersion");
  1367. break;
  1368. case OSVersion:
  1369. strcpy(key_value, os_version);
  1370. strcpy(key_name, "OSVersion");
  1371. break;
  1372. case ProcessorArchitecture:
  1373. strcpy(key_value, processor_arch);
  1374. strcpy(key_name, "ProcessorArchitecture");
  1375. break;
  1376. default:
  1377. hv_msg->error = HV_S_CONT;
  1378. break;
  1379. }
  1380. /* Send the value back to the kernel. */
  1381. kvp_done:
  1382. len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
  1383. if (len != sizeof(struct hv_kvp_msg)) {
  1384. syslog(LOG_ERR, "write failed; error: %d %s", errno,
  1385. strerror(errno));
  1386. exit(EXIT_FAILURE);
  1387. }
  1388. }
  1389. close(kvp_fd);
  1390. exit(0);
  1391. }