hv_kvp_daemon.c 38 KB

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