4
0

totemconfig.c 26 KB

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