hv_kvp_daemon.c 36 KB

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