totemconfig.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2011 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * Jan Friesse (jfriesse@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <config.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <errno.h>
  41. #include <unistd.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <fcntl.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <sys/param.h>
  49. #include <corosync/swab.h>
  50. #include <corosync/list.h>
  51. #include <qb/qbdefs.h>
  52. #include <corosync/totem/totem.h>
  53. #include <corosync/config.h>
  54. #include <corosync/logsys.h>
  55. #include <corosync/icmap.h>
  56. #ifdef HAVE_LIBNSS
  57. #include <nss.h>
  58. #include <pk11pub.h>
  59. #include <pkcs11.h>
  60. #include <prerror.h>
  61. #endif
  62. #include "util.h"
  63. #include "totemconfig.h"
  64. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  65. #define TOKEN_TIMEOUT 1000
  66. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  67. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  68. #define JOIN_TIMEOUT 50
  69. #define MERGE_TIMEOUT 200
  70. #define DOWNCHECK_TIMEOUT 1000
  71. #define FAIL_TO_RECV_CONST 2500
  72. #define SEQNO_UNCHANGED_CONST 30
  73. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  74. #define MAX_NETWORK_DELAY 50
  75. #define WINDOW_SIZE 50
  76. #define MAX_MESSAGES 17
  77. #define MISS_COUNT_CONST 5
  78. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  79. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  80. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 2
  81. #define RRP_AUTORECOVERY_CHECK_TIMEOUT 1000
  82. static char error_string_response[512];
  83. static void add_totem_config_notification(struct totem_config *totem_config);
  84. static void totem_volatile_config_read (struct totem_config *totem_config)
  85. {
  86. char *str;
  87. icmap_get_uint32("totem.token", &totem_config->token_timeout);
  88. icmap_get_uint32("totem.token_retransmit", &totem_config->token_retransmit_timeout);
  89. icmap_get_uint32("totem.hold", &totem_config->token_hold_timeout);
  90. icmap_get_uint32("totem.token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  91. icmap_get_uint32("totem.join", &totem_config->join_timeout);
  92. icmap_get_uint32("totem.send_join", &totem_config->send_join_timeout);
  93. icmap_get_uint32("totem.consensus", &totem_config->consensus_timeout);
  94. icmap_get_uint32("totem.merge", &totem_config->merge_timeout);
  95. icmap_get_uint32("totem.downcheck", &totem_config->downcheck_timeout);
  96. icmap_get_uint32("totem.fail_recv_const", &totem_config->fail_to_recv_const);
  97. icmap_get_uint32("totem.seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  98. icmap_get_uint32("totem.rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  99. icmap_get_uint32("totem.rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  100. icmap_get_uint32("totem.rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  101. icmap_get_uint32("totem.rrp_problem_count_mcast_threshold", &totem_config->rrp_problem_count_mcast_threshold);
  102. icmap_get_uint32("totem.rrp_autorecovery_check_timeout", &totem_config->rrp_autorecovery_check_timeout);
  103. icmap_get_uint32("totem.heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  104. icmap_get_uint32("totem.max_network_delay", &totem_config->max_network_delay);
  105. icmap_get_uint32("totem.window_size", &totem_config->window_size);
  106. icmap_get_uint32("totem.max_messages", &totem_config->max_messages);
  107. icmap_get_uint32("totem.miss_count_const", &totem_config->miss_count_const);
  108. if (icmap_get_string("totem.vsftype", &str) == CS_OK) {
  109. totem_config->vsf_type = str;
  110. }
  111. }
  112. static void totem_get_crypto_type(struct totem_config *totem_config)
  113. {
  114. char *str;
  115. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_OLD;
  116. if (icmap_get_string("totem.crypto_accept", &str) == CS_OK) {
  117. if (strcmp(str, "new") == 0) {
  118. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_NEW;
  119. }
  120. free(str);
  121. }
  122. totem_config->crypto_type = TOTEM_CRYPTO_SOBER;
  123. #ifdef HAVE_LIBNSS
  124. /*
  125. * We must set these even if the key does not exist.
  126. * Encryption type can be set on-the-fly using CFG
  127. */
  128. totem_config->crypto_crypt_type = CKM_AES_CBC_PAD;
  129. totem_config->crypto_sign_type = CKM_SHA256_RSA_PKCS;
  130. #endif
  131. if (icmap_get_string("totem.crypto_type", &str) == CS_OK) {
  132. if (strcmp(str, "sober") == 0) {
  133. free(str);
  134. return;
  135. }
  136. #ifdef HAVE_LIBNSS
  137. if (strcmp(str, "nss") == 0) {
  138. totem_config->crypto_type = TOTEM_CRYPTO_NSS;
  139. }
  140. free(str);
  141. #endif
  142. }
  143. }
  144. static uint16_t generate_cluster_id (const char *cluster_name)
  145. {
  146. int i;
  147. int value = 0;
  148. for (i = 0; i < strlen(cluster_name); i++) {
  149. value <<= 1;
  150. value += cluster_name[i];
  151. }
  152. return (value & 0xFFFF);
  153. }
  154. static int get_cluster_mcast_addr (
  155. const char *cluster_name,
  156. const struct totem_ip_address *bindnet,
  157. unsigned int ringnumber,
  158. struct totem_ip_address *res)
  159. {
  160. uint16_t clusterid;
  161. char addr[INET6_ADDRSTRLEN + 1];
  162. int err;
  163. if (cluster_name == NULL) {
  164. return (-1);
  165. }
  166. clusterid = generate_cluster_id(cluster_name) + ringnumber;
  167. memset (res, 0, sizeof(res));
  168. switch (bindnet->family) {
  169. case AF_INET:
  170. snprintf(addr, sizeof(addr), "239.192.%d.%d", clusterid >> 8, clusterid % 0xFF);
  171. break;
  172. case AF_INET6:
  173. snprintf(addr, sizeof(addr), "ff15::%x", clusterid);
  174. break;
  175. default:
  176. /*
  177. * Unknown family
  178. */
  179. return (-1);
  180. }
  181. err = totemip_parse (res, addr, 0);
  182. return (err);
  183. }
  184. extern int totem_config_read (
  185. struct totem_config *totem_config,
  186. const char **error_string)
  187. {
  188. int res = 0;
  189. char *str;
  190. unsigned int ringnumber = 0;
  191. int member_count = 0;
  192. icmap_iter_t iter, member_iter;
  193. const char *iter_key;
  194. const char *member_iter_key;
  195. char ringnumber_key[ICMAP_KEYNAME_MAXLEN];
  196. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  197. uint8_t u8;
  198. uint16_t u16;
  199. char *cluster_name = NULL;
  200. int i;
  201. memset (totem_config, 0, sizeof (struct totem_config));
  202. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  203. if (totem_config->interfaces == 0) {
  204. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  205. return -1;
  206. }
  207. memset (totem_config->interfaces, 0,
  208. sizeof (struct totem_interface) * INTERFACE_MAX);
  209. totem_config->secauth = 1;
  210. strcpy (totem_config->rrp_mode, "none");
  211. icmap_get_uint32("totem.version", (uint32_t *)&totem_config->version);
  212. if (icmap_get_string("totem.secauth", &str) == CS_OK) {
  213. if (strcmp (str, "on") == 0) {
  214. totem_config->secauth = 1;
  215. }
  216. if (strcmp (str, "off") == 0) {
  217. totem_config->secauth = 0;
  218. }
  219. free(str);
  220. }
  221. if (totem_config->secauth == 1) {
  222. totem_get_crypto_type(totem_config);
  223. }
  224. if (icmap_get_string("totem.rrp_mode", &str) == CS_OK) {
  225. strcpy (totem_config->rrp_mode, str);
  226. free(str);
  227. }
  228. /*
  229. * Get interface node id
  230. */
  231. icmap_get_uint32("totem.nodeid", &totem_config->node_id);
  232. totem_config->clear_node_high_bit = 0;
  233. if (icmap_get_string("totem.clear_node_high_bit", &str) == CS_OK) {
  234. if (strcmp (str, "yes") == 0) {
  235. totem_config->clear_node_high_bit = 1;
  236. }
  237. free(str);
  238. }
  239. icmap_get_uint32("totem.threads", &totem_config->threads);
  240. icmap_get_uint32("totem.netmtu", &totem_config->net_mtu);
  241. icmap_get_string("totem.cluster_name", &cluster_name);
  242. /*
  243. * Get things that might change in the future
  244. */
  245. totem_volatile_config_read(totem_config);
  246. iter = icmap_iter_init("totem.interface.");
  247. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  248. res = sscanf(iter_key, "totem.interface.%[^.].%s", ringnumber_key, tmp_key);
  249. if (res != 2) {
  250. continue;
  251. }
  252. if (strcmp(tmp_key, "bindnetaddr") != 0) {
  253. continue;
  254. }
  255. member_count = 0;
  256. ringnumber = atoi(ringnumber_key);
  257. /*
  258. * Get the bind net address
  259. */
  260. if (icmap_get_string(iter_key, &str) == CS_OK) {
  261. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  262. totem_config->interfaces[ringnumber].mcast_addr.family);
  263. free(str);
  264. }
  265. /*
  266. * Get interface multicast address
  267. */
  268. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", ringnumber);
  269. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  270. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  271. free(str);
  272. } else {
  273. /*
  274. * User not specified address -> autogenerate one from cluster_name key
  275. * (if available)
  276. */
  277. res = get_cluster_mcast_addr (cluster_name,
  278. &totem_config->interfaces[ringnumber].bindnet,
  279. ringnumber,
  280. &totem_config->interfaces[ringnumber].mcast_addr);
  281. }
  282. totem_config->broadcast_use = 0;
  283. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.broadcast", ringnumber);
  284. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  285. if (strcmp (str, "yes") == 0) {
  286. totem_config->broadcast_use = 1;
  287. totemip_parse (
  288. &totem_config->interfaces[ringnumber].mcast_addr,
  289. "255.255.255.255", 0);
  290. }
  291. free(str);
  292. }
  293. /*
  294. * Get mcast port
  295. */
  296. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", ringnumber);
  297. icmap_get_uint16(tmp_key, &totem_config->interfaces[ringnumber].ip_port);
  298. /*
  299. * Get the TTL
  300. */
  301. totem_config->interfaces[ringnumber].ttl = 1;
  302. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.ttl", ringnumber);
  303. if (icmap_get_uint8(tmp_key, &u8) == CS_OK) {
  304. totem_config->interfaces[ringnumber].ttl = u8;
  305. }
  306. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.member.", ringnumber);
  307. member_iter = icmap_iter_init(tmp_key);
  308. while ((member_iter_key = icmap_iter_next(member_iter, NULL, NULL)) != NULL) {
  309. if (icmap_get_string(member_iter_key, &str) == CS_OK) {
  310. res = totemip_parse (&totem_config->interfaces[ringnumber].member_list[member_count++],
  311. str, 0);
  312. }
  313. }
  314. icmap_iter_finalize(member_iter);
  315. totem_config->interfaces[ringnumber].member_count = member_count;
  316. totem_config->interface_count++;
  317. }
  318. icmap_iter_finalize(iter);
  319. /*
  320. * Store automatically generated items back to icmap
  321. */
  322. for (i = 0; i < totem_config->interface_count; i++) {
  323. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastaddr", i);
  324. if (icmap_get_string(tmp_key, &str) == CS_OK) {
  325. free(str);
  326. } else {
  327. str = (char *)totemip_print(&totem_config->interfaces[i].mcast_addr);
  328. icmap_set_string(tmp_key, str);
  329. }
  330. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "totem.interface.%u.mcastport", i);
  331. if (icmap_get_uint16(tmp_key, &u16) != CS_OK) {
  332. icmap_set_uint16(tmp_key, totem_config->interfaces[i].ip_port);
  333. }
  334. }
  335. totem_config->transport_number = TOTEM_TRANSPORT_UDP;
  336. if (icmap_get_string("totem.transport", &str) == CS_OK) {
  337. if (strcmp (str, "udpu") == 0) {
  338. totem_config->transport_number = TOTEM_TRANSPORT_UDPU;
  339. }
  340. if (strcmp (str, "iba") == 0) {
  341. totem_config->transport_number = TOTEM_TRANSPORT_RDMA;
  342. }
  343. free(str);
  344. }
  345. free(cluster_name);
  346. add_totem_config_notification(totem_config);
  347. return 0;
  348. }
  349. int totem_config_validate (
  350. struct totem_config *totem_config,
  351. const char **error_string)
  352. {
  353. static char local_error_reason[512];
  354. char parse_error[512];
  355. const char *error_reason = local_error_reason;
  356. int i;
  357. unsigned int interface_max = INTERFACE_MAX;
  358. if (totem_config->interface_count == 0) {
  359. error_reason = "No interfaces defined";
  360. goto parse_error;
  361. }
  362. for (i = 0; i < totem_config->interface_count; i++) {
  363. /*
  364. * Some error checking of parsed data to make sure its valid
  365. */
  366. struct totem_ip_address null_addr;
  367. memset (&null_addr, 0, sizeof (struct totem_ip_address));
  368. if ((totem_config->transport_number == 0) &&
  369. memcmp (&totem_config->interfaces[i].mcast_addr, &null_addr,
  370. sizeof (struct totem_ip_address)) == 0) {
  371. error_reason = "No multicast address specified";
  372. goto parse_error;
  373. }
  374. if (totem_config->interfaces[i].ip_port == 0) {
  375. error_reason = "No multicast port specified";
  376. goto parse_error;
  377. }
  378. if (totem_config->interfaces[i].ttl > 255) {
  379. error_reason = "Invalid TTL (should be 0..255)";
  380. goto parse_error;
  381. }
  382. if (totem_config->transport_number != TOTEM_TRANSPORT_UDP &&
  383. totem_config->interfaces[i].ttl != 1) {
  384. error_reason = "Can only set ttl on multicast transport types";
  385. goto parse_error;
  386. }
  387. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  388. totem_config->node_id == 0) {
  389. error_reason = "An IPV6 network requires that a node ID be specified.";
  390. goto parse_error;
  391. }
  392. if (totem_config->broadcast_use == 0 && totem_config->transport_number == 0) {
  393. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  394. error_reason = "Multicast address family does not match bind address family";
  395. goto parse_error;
  396. }
  397. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  398. error_reason = "Not all bind address belong to the same IP family";
  399. goto parse_error;
  400. }
  401. if (totemip_is_mcast (&totem_config->interfaces[i].mcast_addr) != 0) {
  402. error_reason = "mcastaddr is not a correct multicast address.";
  403. goto parse_error;
  404. }
  405. }
  406. }
  407. if (totem_config->version != 2) {
  408. error_reason = "This totem parser can only parse version 2 configurations.";
  409. goto parse_error;
  410. }
  411. if (totem_config->token_retransmits_before_loss_const == 0) {
  412. totem_config->token_retransmits_before_loss_const =
  413. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  414. }
  415. /*
  416. * Setup timeout values that are not setup by user
  417. */
  418. if (totem_config->token_timeout == 0) {
  419. totem_config->token_timeout = TOKEN_TIMEOUT;
  420. if (totem_config->token_retransmits_before_loss_const == 0) {
  421. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  422. }
  423. if (totem_config->token_retransmit_timeout == 0) {
  424. totem_config->token_retransmit_timeout =
  425. (int)(totem_config->token_timeout /
  426. (totem_config->token_retransmits_before_loss_const + 0.2));
  427. }
  428. if (totem_config->token_hold_timeout == 0) {
  429. totem_config->token_hold_timeout =
  430. (int)(totem_config->token_retransmit_timeout * 0.8 -
  431. (1000/HZ));
  432. }
  433. }
  434. if (totem_config->max_network_delay == 0) {
  435. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  436. }
  437. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  438. snprintf (local_error_reason, sizeof(local_error_reason),
  439. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  440. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  441. goto parse_error;
  442. }
  443. if (totem_config->window_size == 0) {
  444. totem_config->window_size = WINDOW_SIZE;
  445. }
  446. if (totem_config->max_messages == 0) {
  447. totem_config->max_messages = MAX_MESSAGES;
  448. }
  449. if (totem_config->miss_count_const == 0) {
  450. totem_config->miss_count_const = MISS_COUNT_CONST;
  451. }
  452. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  453. snprintf (local_error_reason, sizeof(local_error_reason),
  454. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  455. totem_config->token_timeout, MINIMUM_TIMEOUT);
  456. goto parse_error;
  457. }
  458. if (totem_config->token_retransmit_timeout == 0) {
  459. totem_config->token_retransmit_timeout =
  460. (int)(totem_config->token_timeout /
  461. (totem_config->token_retransmits_before_loss_const + 0.2));
  462. }
  463. if (totem_config->token_hold_timeout == 0) {
  464. totem_config->token_hold_timeout =
  465. (int)(totem_config->token_retransmit_timeout * 0.8 -
  466. (1000/HZ));
  467. }
  468. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  469. snprintf (local_error_reason, sizeof(local_error_reason),
  470. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  471. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  472. goto parse_error;
  473. }
  474. if (totem_config->token_hold_timeout == 0) {
  475. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  476. }
  477. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  478. snprintf (local_error_reason, sizeof(local_error_reason),
  479. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  480. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  481. goto parse_error;
  482. }
  483. if (totem_config->join_timeout == 0) {
  484. totem_config->join_timeout = JOIN_TIMEOUT;
  485. }
  486. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  487. snprintf (local_error_reason, sizeof(local_error_reason),
  488. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  489. totem_config->join_timeout, MINIMUM_TIMEOUT);
  490. goto parse_error;
  491. }
  492. if (totem_config->consensus_timeout == 0) {
  493. totem_config->consensus_timeout = (int)(float)(1.2 * totem_config->token_timeout);
  494. }
  495. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  496. snprintf (local_error_reason, sizeof(local_error_reason),
  497. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  498. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  499. goto parse_error;
  500. }
  501. if (totem_config->merge_timeout == 0) {
  502. totem_config->merge_timeout = MERGE_TIMEOUT;
  503. }
  504. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  505. snprintf (local_error_reason, sizeof(local_error_reason),
  506. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  507. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  508. goto parse_error;
  509. }
  510. if (totem_config->downcheck_timeout == 0) {
  511. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  512. }
  513. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  514. snprintf (local_error_reason, sizeof(local_error_reason),
  515. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  516. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  517. goto parse_error;
  518. }
  519. /*
  520. * RRP values validation
  521. */
  522. if (strcmp (totem_config->rrp_mode, "none") &&
  523. strcmp (totem_config->rrp_mode, "active") &&
  524. strcmp (totem_config->rrp_mode, "passive")) {
  525. snprintf (local_error_reason, sizeof(local_error_reason),
  526. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  527. goto parse_error;
  528. }
  529. if (totem_config->rrp_problem_count_timeout == 0) {
  530. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  531. }
  532. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  533. snprintf (local_error_reason, sizeof(local_error_reason),
  534. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  535. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  536. goto parse_error;
  537. }
  538. if (totem_config->rrp_problem_count_threshold == 0) {
  539. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  540. }
  541. if (totem_config->rrp_problem_count_mcast_threshold == 0) {
  542. totem_config->rrp_problem_count_mcast_threshold = totem_config->rrp_problem_count_threshold * 10;
  543. }
  544. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  545. snprintf (local_error_reason, sizeof(local_error_reason),
  546. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  547. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  548. goto parse_error;
  549. }
  550. if (totem_config->rrp_problem_count_mcast_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  551. snprintf (local_error_reason, sizeof(local_error_reason),
  552. "The RRP multicast problem count threshold (%d problem count) may not be less then (%d problem count).",
  553. totem_config->rrp_problem_count_mcast_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  554. goto parse_error;
  555. }
  556. if (totem_config->rrp_token_expired_timeout == 0) {
  557. totem_config->rrp_token_expired_timeout =
  558. totem_config->token_retransmit_timeout;
  559. }
  560. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  561. snprintf (local_error_reason, sizeof(local_error_reason),
  562. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  563. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  564. goto parse_error;
  565. }
  566. if (totem_config->rrp_autorecovery_check_timeout == 0) {
  567. totem_config->rrp_autorecovery_check_timeout = RRP_AUTORECOVERY_CHECK_TIMEOUT;
  568. }
  569. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  570. interface_max = 1;
  571. }
  572. if (interface_max < totem_config->interface_count) {
  573. snprintf (parse_error, sizeof(parse_error),
  574. "%d is too many configured interfaces for the rrp_mode setting %s.",
  575. totem_config->interface_count,
  576. totem_config->rrp_mode);
  577. error_reason = parse_error;
  578. goto parse_error;
  579. }
  580. if (totem_config->fail_to_recv_const == 0) {
  581. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  582. }
  583. if (totem_config->seqno_unchanged_const == 0) {
  584. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  585. }
  586. if (totem_config->net_mtu == 0) {
  587. totem_config->net_mtu = 1500;
  588. }
  589. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  590. snprintf (local_error_reason, sizeof(local_error_reason),
  591. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  592. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  593. goto parse_error;
  594. }
  595. if (totem_config->threads > SEND_THREADS_MAX) {
  596. totem_config->threads = SEND_THREADS_MAX;
  597. }
  598. if (totem_config->secauth == 0) {
  599. totem_config->threads = 0;
  600. }
  601. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  602. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  603. goto parse_error;
  604. }
  605. if (totem_config->vsf_type == NULL) {
  606. totem_config->vsf_type = "none";
  607. }
  608. return (0);
  609. parse_error:
  610. snprintf (error_string_response, sizeof(error_string_response),
  611. "parse error in config: %s\n", error_reason);
  612. *error_string = error_string_response;
  613. return (-1);
  614. }
  615. static int read_keyfile (
  616. const char *key_location,
  617. struct totem_config *totem_config,
  618. const char **error_string)
  619. {
  620. int fd;
  621. int res;
  622. ssize_t expected_key_len = sizeof (totem_config->private_key);
  623. int saved_errno;
  624. char error_str[100];
  625. const char *error_ptr;
  626. fd = open (key_location, O_RDONLY);
  627. if (fd == -1) {
  628. error_ptr = qb_strerror_r(errno, error_str, sizeof(error_str));
  629. snprintf (error_string_response, sizeof(error_string_response),
  630. "Could not open %s: %s\n",
  631. key_location, error_ptr);
  632. goto parse_error;
  633. }
  634. res = read (fd, totem_config->private_key, expected_key_len);
  635. saved_errno = errno;
  636. close (fd);
  637. if (res == -1) {
  638. error_ptr = qb_strerror_r (saved_errno, error_str, sizeof(error_str));
  639. snprintf (error_string_response, sizeof(error_string_response),
  640. "Could not read %s: %s\n",
  641. key_location, error_ptr);
  642. goto parse_error;
  643. }
  644. totem_config->private_key_len = expected_key_len;
  645. if (res != expected_key_len) {
  646. snprintf (error_string_response, sizeof(error_string_response),
  647. "Could only read %d bits of 1024 bits from %s.\n",
  648. res * 8, key_location);
  649. goto parse_error;
  650. }
  651. return 0;
  652. parse_error:
  653. *error_string = error_string_response;
  654. return (-1);
  655. }
  656. int totem_config_keyread (
  657. struct totem_config *totem_config,
  658. const char **error_string)
  659. {
  660. int got_key = 0;
  661. char *key_location = NULL;
  662. int res;
  663. size_t key_len;
  664. memset (totem_config->private_key, 0, 128);
  665. totem_config->private_key_len = 128;
  666. if (totem_config->secauth == 0) {
  667. return (0);
  668. }
  669. /* cmap may store the location of the key file */
  670. if (icmap_get_string("totem.keyfile", &key_location) == CS_OK) {
  671. res = read_keyfile(key_location, totem_config, error_string);
  672. free(key_location);
  673. if (res) {
  674. goto key_error;
  675. }
  676. got_key = 1;
  677. } else { /* Or the key itself may be in the cmap */
  678. if (icmap_get("totem.key", NULL, &key_len, NULL) == CS_OK) {
  679. if (key_len > sizeof (totem_config->private_key)) {
  680. sprintf(error_string_response, "key is too long");
  681. goto key_error;
  682. }
  683. if (icmap_get("totem.key", totem_config->private_key, &key_len, NULL) == CS_OK) {
  684. totem_config->private_key_len = key_len;
  685. got_key = 1;
  686. } else {
  687. sprintf(error_string_response, "can't store private key");
  688. goto key_error;
  689. }
  690. }
  691. }
  692. /* In desperation we read the default filename */
  693. if (!got_key) {
  694. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  695. if (!filename)
  696. filename = COROSYSCONFDIR "/authkey";
  697. res = read_keyfile(filename, totem_config, error_string);
  698. if (res)
  699. goto key_error;
  700. }
  701. return (0);
  702. key_error:
  703. *error_string = error_string_response;
  704. return (-1);
  705. }
  706. static void totem_change_notify(
  707. int32_t event,
  708. const char *key_name,
  709. struct icmap_notify_value new_val,
  710. struct icmap_notify_value old_val,
  711. void *user_data)
  712. {
  713. totem_volatile_config_read((struct totem_config *)user_data);
  714. }
  715. static void add_totem_config_notification(struct totem_config *totem_config)
  716. {
  717. icmap_track_t icmap_track;
  718. icmap_track_add("totem.",
  719. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  720. totem_change_notify,
  721. totem_config,
  722. &icmap_track);
  723. }