hv_kvp_daemon.c 37 KB

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