hv_kvp_daemon.c 35 KB

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