hv_kvp_daemon.c 38 KB

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