hv_kvp_daemon.c 37 KB

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