hv_kvp_daemon.c 37 KB

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