hv_kvp_daemon.c 43 KB

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