hv_kvp_daemon.c 37 KB

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