hv_kvp_daemon.c 37 KB

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