hv_kvp_daemon.c 39 KB

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