hv_kvp_daemon.c 36 KB

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