hv_kvp_daemon.c 36 KB

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