hv_kvp_daemon.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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;
  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 seperated 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. /*
  1000. * Set the configuration for the specified interface with
  1001. * the information provided. Since there is no standard
  1002. * way to configure an interface, we will have an external
  1003. * script that does the job of configuring the interface and
  1004. * flushing the configuration.
  1005. *
  1006. * The parameters passed to this external script are:
  1007. * 1. A configuration file that has the specified configuration.
  1008. *
  1009. * We will embed the name of the interface in the configuration
  1010. * file: ifcfg-ethx (where ethx is the interface name).
  1011. *
  1012. * The information provided here may be more than what is needed
  1013. * in a given distro to configure the interface and so are free
  1014. * ignore information that may not be relevant.
  1015. *
  1016. * Here is the format of the ip configuration file:
  1017. *
  1018. * HWADDR=macaddr
  1019. * DEVICE=interface name
  1020. * BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
  1021. * or "none" if no boot-time protocol should be used)
  1022. *
  1023. * IPADDR0=ipaddr1
  1024. * IPADDR1=ipaddr2
  1025. * IPADDRx=ipaddry (where y = x + 1)
  1026. *
  1027. * NETMASK0=netmask1
  1028. * NETMASKx=netmasky (where y = x + 1)
  1029. *
  1030. * GATEWAY=ipaddr1
  1031. * GATEWAYx=ipaddry (where y = x + 1)
  1032. *
  1033. * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
  1034. *
  1035. * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
  1036. * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
  1037. * IPV6NETMASK.
  1038. *
  1039. * The host can specify multiple ipv4 and ipv6 addresses to be
  1040. * configured for the interface. Furthermore, the configuration
  1041. * needs to be persistent. A subsequent GET call on the interface
  1042. * is expected to return the configuration that is set via the SET
  1043. * call.
  1044. */
  1045. snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
  1046. "/ifcfg-", if_name);
  1047. file = fopen(if_file, "w");
  1048. if (file == NULL) {
  1049. syslog(LOG_ERR, "Failed to open config file; error: %d %s",
  1050. errno, strerror(errno));
  1051. return HV_E_FAIL;
  1052. }
  1053. /*
  1054. * First write out the MAC address.
  1055. */
  1056. mac_addr = kvp_if_name_to_mac(if_name);
  1057. if (mac_addr == NULL) {
  1058. error = HV_E_FAIL;
  1059. goto setval_error;
  1060. }
  1061. error = kvp_write_file(file, "HWADDR", "", mac_addr);
  1062. free(mac_addr);
  1063. if (error)
  1064. goto setval_error;
  1065. error = kvp_write_file(file, "DEVICE", "", if_name);
  1066. if (error)
  1067. goto setval_error;
  1068. /*
  1069. * The dhcp_enabled flag is only for IPv4. In the case the host only
  1070. * injects an IPv6 address, the flag is true, but we still need to
  1071. * proceed to parse and pass the IPv6 information to the
  1072. * disto-specific script hv_set_ifconfig.
  1073. */
  1074. if (new_val->dhcp_enabled) {
  1075. error = kvp_write_file(file, "BOOTPROTO", "", "dhcp");
  1076. if (error)
  1077. goto setval_error;
  1078. } else {
  1079. error = kvp_write_file(file, "BOOTPROTO", "", "none");
  1080. if (error)
  1081. goto setval_error;
  1082. }
  1083. /*
  1084. * Write the configuration for ipaddress, netmask, gateway and
  1085. * name servers.
  1086. */
  1087. error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
  1088. if (error)
  1089. goto setval_error;
  1090. error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
  1091. if (error)
  1092. goto setval_error;
  1093. error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
  1094. if (error)
  1095. goto setval_error;
  1096. error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
  1097. if (error)
  1098. goto setval_error;
  1099. fclose(file);
  1100. /*
  1101. * Now that we have populated the configuration file,
  1102. * invoke the external script to do its magic.
  1103. */
  1104. snprintf(cmd, sizeof(cmd), KVP_SCRIPTS_PATH "%s %s",
  1105. "hv_set_ifconfig", if_file);
  1106. if (system(cmd)) {
  1107. syslog(LOG_ERR, "Failed to execute cmd '%s'; error: %d %s",
  1108. cmd, errno, strerror(errno));
  1109. return HV_E_FAIL;
  1110. }
  1111. return 0;
  1112. setval_error:
  1113. syslog(LOG_ERR, "Failed to write config file");
  1114. fclose(file);
  1115. return error;
  1116. }
  1117. static void
  1118. kvp_get_domain_name(char *buffer, int length)
  1119. {
  1120. struct addrinfo hints, *info ;
  1121. int error = 0;
  1122. gethostname(buffer, length);
  1123. memset(&hints, 0, sizeof(hints));
  1124. hints.ai_family = AF_INET; /*Get only ipv4 addrinfo. */
  1125. hints.ai_socktype = SOCK_STREAM;
  1126. hints.ai_flags = AI_CANONNAME;
  1127. error = getaddrinfo(buffer, NULL, &hints, &info);
  1128. if (error != 0) {
  1129. snprintf(buffer, length, "getaddrinfo failed: 0x%x %s",
  1130. error, gai_strerror(error));
  1131. return;
  1132. }
  1133. snprintf(buffer, length, "%s", info->ai_canonname);
  1134. freeaddrinfo(info);
  1135. }
  1136. void print_usage(char *argv[])
  1137. {
  1138. fprintf(stderr, "Usage: %s [options]\n"
  1139. "Options are:\n"
  1140. " -n, --no-daemon stay in foreground, don't daemonize\n"
  1141. " -h, --help print this help\n", argv[0]);
  1142. }
  1143. int main(int argc, char *argv[])
  1144. {
  1145. int kvp_fd, len;
  1146. int error;
  1147. struct pollfd pfd;
  1148. char *p;
  1149. struct hv_kvp_msg hv_msg[1];
  1150. char *key_value;
  1151. char *key_name;
  1152. int op;
  1153. int pool;
  1154. char *if_name;
  1155. struct hv_kvp_ipaddr_value *kvp_ip_val;
  1156. int daemonize = 1, long_index = 0, opt;
  1157. static struct option long_options[] = {
  1158. {"help", no_argument, 0, 'h' },
  1159. {"no-daemon", no_argument, 0, 'n' },
  1160. {0, 0, 0, 0 }
  1161. };
  1162. while ((opt = getopt_long(argc, argv, "hn", long_options,
  1163. &long_index)) != -1) {
  1164. switch (opt) {
  1165. case 'n':
  1166. daemonize = 0;
  1167. break;
  1168. case 'h':
  1169. default:
  1170. print_usage(argv);
  1171. exit(EXIT_FAILURE);
  1172. }
  1173. }
  1174. if (daemonize && daemon(1, 0))
  1175. return 1;
  1176. openlog("KVP", 0, LOG_USER);
  1177. syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
  1178. kvp_fd = open("/dev/vmbus/hv_kvp", O_RDWR | O_CLOEXEC);
  1179. if (kvp_fd < 0) {
  1180. syslog(LOG_ERR, "open /dev/vmbus/hv_kvp failed; error: %d %s",
  1181. errno, strerror(errno));
  1182. exit(EXIT_FAILURE);
  1183. }
  1184. /*
  1185. * Retrieve OS release information.
  1186. */
  1187. kvp_get_os_info();
  1188. /*
  1189. * Cache Fully Qualified Domain Name because getaddrinfo takes an
  1190. * unpredictable amount of time to finish.
  1191. */
  1192. kvp_get_domain_name(full_domain_name, sizeof(full_domain_name));
  1193. if (kvp_file_init()) {
  1194. syslog(LOG_ERR, "Failed to initialize the pools");
  1195. exit(EXIT_FAILURE);
  1196. }
  1197. /*
  1198. * Register ourselves with the kernel.
  1199. */
  1200. hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
  1201. len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
  1202. if (len != sizeof(struct hv_kvp_msg)) {
  1203. syslog(LOG_ERR, "registration to kernel failed; error: %d %s",
  1204. errno, strerror(errno));
  1205. close(kvp_fd);
  1206. exit(EXIT_FAILURE);
  1207. }
  1208. pfd.fd = kvp_fd;
  1209. while (1) {
  1210. pfd.events = POLLIN;
  1211. pfd.revents = 0;
  1212. if (poll(&pfd, 1, -1) < 0) {
  1213. syslog(LOG_ERR, "poll failed; error: %d %s", errno, strerror(errno));
  1214. if (errno == EINVAL) {
  1215. close(kvp_fd);
  1216. exit(EXIT_FAILURE);
  1217. }
  1218. else
  1219. continue;
  1220. }
  1221. len = read(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
  1222. if (len != sizeof(struct hv_kvp_msg)) {
  1223. syslog(LOG_ERR, "read failed; error:%d %s",
  1224. errno, strerror(errno));
  1225. close(kvp_fd);
  1226. return EXIT_FAILURE;
  1227. }
  1228. /*
  1229. * We will use the KVP header information to pass back
  1230. * the error from this daemon. So, first copy the state
  1231. * and set the error code to success.
  1232. */
  1233. op = hv_msg->kvp_hdr.operation;
  1234. pool = hv_msg->kvp_hdr.pool;
  1235. hv_msg->error = HV_S_OK;
  1236. if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
  1237. /*
  1238. * Driver is registering with us; stash away the version
  1239. * information.
  1240. */
  1241. in_hand_shake = 0;
  1242. p = (char *)hv_msg->body.kvp_register.version;
  1243. lic_version = malloc(strlen(p) + 1);
  1244. if (lic_version) {
  1245. strcpy(lic_version, p);
  1246. syslog(LOG_INFO, "KVP LIC Version: %s",
  1247. lic_version);
  1248. } else {
  1249. syslog(LOG_ERR, "malloc failed");
  1250. }
  1251. continue;
  1252. }
  1253. switch (op) {
  1254. case KVP_OP_GET_IP_INFO:
  1255. kvp_ip_val = &hv_msg->body.kvp_ip_val;
  1256. error = kvp_mac_to_ip(kvp_ip_val);
  1257. if (error)
  1258. hv_msg->error = error;
  1259. break;
  1260. case KVP_OP_SET_IP_INFO:
  1261. kvp_ip_val = &hv_msg->body.kvp_ip_val;
  1262. if_name = kvp_get_if_name(
  1263. (char *)kvp_ip_val->adapter_id);
  1264. if (if_name == NULL) {
  1265. /*
  1266. * We could not map the guid to an
  1267. * interface name; return error.
  1268. */
  1269. hv_msg->error = HV_GUID_NOTFOUND;
  1270. break;
  1271. }
  1272. error = kvp_set_ip_info(if_name, kvp_ip_val);
  1273. if (error)
  1274. hv_msg->error = error;
  1275. free(if_name);
  1276. break;
  1277. case KVP_OP_SET:
  1278. if (kvp_key_add_or_modify(pool,
  1279. hv_msg->body.kvp_set.data.key,
  1280. hv_msg->body.kvp_set.data.key_size,
  1281. hv_msg->body.kvp_set.data.value,
  1282. hv_msg->body.kvp_set.data.value_size))
  1283. hv_msg->error = HV_S_CONT;
  1284. break;
  1285. case KVP_OP_GET:
  1286. if (kvp_get_value(pool,
  1287. hv_msg->body.kvp_set.data.key,
  1288. hv_msg->body.kvp_set.data.key_size,
  1289. hv_msg->body.kvp_set.data.value,
  1290. hv_msg->body.kvp_set.data.value_size))
  1291. hv_msg->error = HV_S_CONT;
  1292. break;
  1293. case KVP_OP_DELETE:
  1294. if (kvp_key_delete(pool,
  1295. hv_msg->body.kvp_delete.key,
  1296. hv_msg->body.kvp_delete.key_size))
  1297. hv_msg->error = HV_S_CONT;
  1298. break;
  1299. default:
  1300. break;
  1301. }
  1302. if (op != KVP_OP_ENUMERATE)
  1303. goto kvp_done;
  1304. /*
  1305. * If the pool is KVP_POOL_AUTO, dynamically generate
  1306. * both the key and the value; if not read from the
  1307. * appropriate pool.
  1308. */
  1309. if (pool != KVP_POOL_AUTO) {
  1310. if (kvp_pool_enumerate(pool,
  1311. hv_msg->body.kvp_enum_data.index,
  1312. hv_msg->body.kvp_enum_data.data.key,
  1313. HV_KVP_EXCHANGE_MAX_KEY_SIZE,
  1314. hv_msg->body.kvp_enum_data.data.value,
  1315. HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
  1316. hv_msg->error = HV_S_CONT;
  1317. goto kvp_done;
  1318. }
  1319. key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
  1320. key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
  1321. switch (hv_msg->body.kvp_enum_data.index) {
  1322. case FullyQualifiedDomainName:
  1323. strcpy(key_value, full_domain_name);
  1324. strcpy(key_name, "FullyQualifiedDomainName");
  1325. break;
  1326. case IntegrationServicesVersion:
  1327. strcpy(key_name, "IntegrationServicesVersion");
  1328. strcpy(key_value, lic_version);
  1329. break;
  1330. case NetworkAddressIPv4:
  1331. kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
  1332. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  1333. strcpy(key_name, "NetworkAddressIPv4");
  1334. break;
  1335. case NetworkAddressIPv6:
  1336. kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
  1337. key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
  1338. strcpy(key_name, "NetworkAddressIPv6");
  1339. break;
  1340. case OSBuildNumber:
  1341. strcpy(key_value, os_build);
  1342. strcpy(key_name, "OSBuildNumber");
  1343. break;
  1344. case OSName:
  1345. strcpy(key_value, os_name);
  1346. strcpy(key_name, "OSName");
  1347. break;
  1348. case OSMajorVersion:
  1349. strcpy(key_value, os_major);
  1350. strcpy(key_name, "OSMajorVersion");
  1351. break;
  1352. case OSMinorVersion:
  1353. strcpy(key_value, os_minor);
  1354. strcpy(key_name, "OSMinorVersion");
  1355. break;
  1356. case OSVersion:
  1357. strcpy(key_value, os_version);
  1358. strcpy(key_name, "OSVersion");
  1359. break;
  1360. case ProcessorArchitecture:
  1361. strcpy(key_value, processor_arch);
  1362. strcpy(key_name, "ProcessorArchitecture");
  1363. break;
  1364. default:
  1365. hv_msg->error = HV_S_CONT;
  1366. break;
  1367. }
  1368. /* Send the value back to the kernel. */
  1369. kvp_done:
  1370. len = write(kvp_fd, hv_msg, sizeof(struct hv_kvp_msg));
  1371. if (len != sizeof(struct hv_kvp_msg)) {
  1372. syslog(LOG_ERR, "write failed; error: %d %s", errno,
  1373. strerror(errno));
  1374. exit(EXIT_FAILURE);
  1375. }
  1376. }
  1377. close(kvp_fd);
  1378. exit(0);
  1379. }