hv_kvp_daemon.c 35 KB

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