hv_kvp_daemon.c 36 KB

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