totemconfig.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 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 CONSENSUS_TIMEOUT 800
  69. #define MERGE_TIMEOUT 200
  70. #define DOWNCHECK_TIMEOUT 1000
  71. #define FAIL_TO_RECV_CONST 50
  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 RRP_PROBLEM_COUNT_TIMEOUT 2000
  78. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  79. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 5
  80. static char error_string_response[512];
  81. static struct objdb_iface_ver0 *global_objdb;
  82. static void add_totem_config_notification(
  83. struct objdb_iface_ver0 *objdb,
  84. struct totem_config *totem_config,
  85. hdb_handle_t totem_object_handle);
  86. /* These just makes the code below a little neater */
  87. static inline int objdb_get_string (
  88. const struct objdb_iface_ver0 *objdb,
  89. hdb_handle_t object_service_handle,
  90. const char *key, const char **value)
  91. {
  92. int res;
  93. *value = NULL;
  94. if ( !(res = objdb->object_key_get (object_service_handle,
  95. key,
  96. strlen (key),
  97. (void *)value,
  98. NULL))) {
  99. if (*value) {
  100. return 0;
  101. }
  102. }
  103. return -1;
  104. }
  105. static inline void objdb_get_int (
  106. const struct objdb_iface_ver0 *objdb,
  107. hdb_handle_t object_service_handle,
  108. const char *key, unsigned int *intvalue)
  109. {
  110. char *value = NULL;
  111. if (!objdb->object_key_get (object_service_handle,
  112. key,
  113. strlen (key),
  114. (void *)&value,
  115. NULL)) {
  116. if (value) {
  117. *intvalue = atoi(value);
  118. }
  119. }
  120. }
  121. static unsigned int totem_handle_find (
  122. struct objdb_iface_ver0 *objdb,
  123. hdb_handle_t *totem_find_handle) {
  124. hdb_handle_t object_find_handle;
  125. unsigned int res;
  126. /*
  127. * Find a network section
  128. */
  129. objdb->object_find_create (
  130. OBJECT_PARENT_HANDLE,
  131. "network",
  132. strlen ("network"),
  133. &object_find_handle);
  134. res = objdb->object_find_next (
  135. object_find_handle,
  136. totem_find_handle);
  137. objdb->object_find_destroy (object_find_handle);
  138. /*
  139. * Network section not found in configuration, checking for totem
  140. */
  141. if (res == -1) {
  142. objdb->object_find_create (
  143. OBJECT_PARENT_HANDLE,
  144. "totem",
  145. strlen ("totem"),
  146. &object_find_handle);
  147. res = objdb->object_find_next (
  148. object_find_handle,
  149. totem_find_handle);
  150. objdb->object_find_destroy (object_find_handle);
  151. }
  152. if (res == -1) {
  153. return (-1);
  154. }
  155. return (0);
  156. }
  157. static void totem_volatile_config_read (
  158. struct objdb_iface_ver0 *objdb,
  159. struct totem_config *totem_config,
  160. hdb_handle_t object_totem_handle)
  161. {
  162. objdb_get_int (objdb,object_totem_handle, "token", &totem_config->token_timeout);
  163. objdb_get_int (objdb,object_totem_handle, "token_retransmit", &totem_config->token_retransmit_timeout);
  164. objdb_get_int (objdb,object_totem_handle, "hold", &totem_config->token_hold_timeout);
  165. objdb_get_int (objdb,object_totem_handle, "token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  166. objdb_get_int (objdb,object_totem_handle, "join", &totem_config->join_timeout);
  167. objdb_get_int (objdb,object_totem_handle, "send_join", &totem_config->send_join_timeout);
  168. objdb_get_int (objdb,object_totem_handle, "consensus", &totem_config->consensus_timeout);
  169. objdb_get_int (objdb,object_totem_handle, "merge", &totem_config->merge_timeout);
  170. objdb_get_int (objdb,object_totem_handle, "downcheck", &totem_config->downcheck_timeout);
  171. objdb_get_int (objdb,object_totem_handle, "fail_recv_const", &totem_config->fail_to_recv_const);
  172. objdb_get_int (objdb,object_totem_handle, "seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  173. objdb_get_int (objdb,object_totem_handle, "rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  174. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  175. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  176. objdb_get_int (objdb,object_totem_handle, "heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  177. objdb_get_int (objdb,object_totem_handle, "max_network_delay", &totem_config->max_network_delay);
  178. objdb_get_int (objdb,object_totem_handle, "window_size", &totem_config->window_size);
  179. objdb_get_string (objdb, object_totem_handle, "vsftype", &totem_config->vsf_type);
  180. objdb_get_int (objdb,object_totem_handle, "max_messages", &totem_config->max_messages);
  181. }
  182. static void totem_get_crypto_type(
  183. const struct objdb_iface_ver0 *objdb,
  184. hdb_handle_t object_totem_handle,
  185. struct totem_config *totem_config)
  186. {
  187. const char *str;
  188. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_OLD;
  189. if (!objdb_get_string (objdb, object_totem_handle, "crypto_accept", &str)) {
  190. if (strcmp(str, "new") == 0) {
  191. totem_config->crypto_accept = TOTEM_CRYPTO_ACCEPT_NEW;
  192. }
  193. }
  194. totem_config->crypto_type = TOTEM_CRYPTO_SOBER;
  195. #ifdef HAVE_LIBNSS
  196. /*
  197. * We must set these even if the key does not exist.
  198. * Encryption type can be set on-the-fly using CFG
  199. */
  200. totem_config->crypto_crypt_type = CKM_AES_CBC_PAD;
  201. totem_config->crypto_sign_type = CKM_SHA256_RSA_PKCS;
  202. #endif
  203. if (!objdb_get_string (objdb, object_totem_handle, "crypto_type", &str)) {
  204. if (strcmp(str, "sober") == 0) {
  205. return;
  206. }
  207. #ifdef HAVE_LIBNSS
  208. if (strcmp(str, "nss") == 0) {
  209. totem_config->crypto_type = TOTEM_CRYPTO_NSS;
  210. }
  211. #endif
  212. }
  213. }
  214. extern int totem_config_read (
  215. struct objdb_iface_ver0 *objdb,
  216. struct totem_config *totem_config,
  217. const char **error_string)
  218. {
  219. int res = 0;
  220. hdb_handle_t object_totem_handle;
  221. hdb_handle_t object_interface_handle;
  222. const char *str;
  223. unsigned int ringnumber = 0;
  224. hdb_handle_t object_find_interface_handle;
  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. objdb_get_int (objdb,object_totem_handle, "threads", &totem_config->threads);
  264. objdb_get_int (objdb,object_totem_handle, "netmtu", &totem_config->net_mtu);
  265. /*
  266. * Get things that might change in the future
  267. */
  268. totem_volatile_config_read (objdb, totem_config, object_totem_handle);
  269. objdb->object_find_create (
  270. object_totem_handle,
  271. "interface",
  272. strlen ("interface"),
  273. &object_find_interface_handle);
  274. while (objdb->object_find_next (
  275. object_find_interface_handle,
  276. &object_interface_handle) == 0) {
  277. objdb_get_int (objdb, object_interface_handle, "ringnumber", &ringnumber);
  278. /*
  279. * Get interface multicast address
  280. */
  281. if (!objdb_get_string (objdb, object_interface_handle, "mcastaddr", &str)) {
  282. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  283. }
  284. totem_config->broadcast_use = 0;
  285. if (!objdb_get_string (objdb, object_interface_handle, "broadcast", &str)) {
  286. if (strcmp (str, "yes") == 0) {
  287. totem_config->broadcast_use = 1;
  288. totemip_parse (
  289. &totem_config->interfaces[ringnumber].mcast_addr,
  290. "255.255.255.255", 0);
  291. }
  292. }
  293. /*
  294. * Get mcast port
  295. */
  296. if (!objdb_get_string (objdb, object_interface_handle, "mcastport", &str)) {
  297. totem_config->interfaces[ringnumber].ip_port = atoi (str);
  298. }
  299. /*
  300. * Get the bind net address
  301. */
  302. if (!objdb_get_string (objdb, object_interface_handle, "bindnetaddr", &str)) {
  303. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  304. totem_config->interfaces[ringnumber].mcast_addr.family);
  305. }
  306. totem_config->interface_count++;
  307. }
  308. objdb->object_find_destroy (object_find_interface_handle);
  309. add_totem_config_notification(objdb, totem_config, object_totem_handle);
  310. return 0;
  311. }
  312. int totem_config_validate (
  313. struct totem_config *totem_config,
  314. const char **error_string)
  315. {
  316. static char local_error_reason[512];
  317. char parse_error[512];
  318. const char *error_reason = local_error_reason;
  319. int i;
  320. unsigned int interface_max = INTERFACE_MAX;
  321. if (totem_config->interface_count == 0) {
  322. error_reason = "No interfaces defined";
  323. goto parse_error;
  324. }
  325. for (i = 0; i < totem_config->interface_count; i++) {
  326. /*
  327. * Some error checking of parsed data to make sure its valid
  328. */
  329. if ((int *)&totem_config->interfaces[i].mcast_addr.addr == 0) {
  330. error_reason = "No multicast address specified";
  331. goto parse_error;
  332. }
  333. if (totem_config->interfaces[i].ip_port == 0) {
  334. error_reason = "No multicast port specified";
  335. goto parse_error;
  336. }
  337. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  338. totem_config->node_id == 0) {
  339. error_reason = "An IPV6 network requires that a node ID be specified.";
  340. goto parse_error;
  341. }
  342. if (totem_config->broadcast_use == 0) {
  343. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  344. error_reason = "Multicast address family does not match bind address family";
  345. goto parse_error;
  346. }
  347. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  348. error_reason = "Not all bind address belong to the same IP family";
  349. goto parse_error;
  350. }
  351. }
  352. }
  353. if (totem_config->version != 2) {
  354. error_reason = "This totem parser can only parse version 2 configurations.";
  355. goto parse_error;
  356. }
  357. if (totem_config->token_retransmits_before_loss_const == 0) {
  358. totem_config->token_retransmits_before_loss_const =
  359. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  360. }
  361. /*
  362. * Setup timeout values that are not setup by user
  363. */
  364. if (totem_config->token_timeout == 0) {
  365. totem_config->token_timeout = TOKEN_TIMEOUT;
  366. if (totem_config->token_retransmits_before_loss_const == 0) {
  367. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  368. }
  369. if (totem_config->token_retransmit_timeout == 0) {
  370. totem_config->token_retransmit_timeout =
  371. (int)(totem_config->token_timeout /
  372. (totem_config->token_retransmits_before_loss_const + 0.2));
  373. }
  374. if (totem_config->token_hold_timeout == 0) {
  375. totem_config->token_hold_timeout =
  376. (int)(totem_config->token_retransmit_timeout * 0.8 -
  377. (1000/HZ));
  378. }
  379. }
  380. if (totem_config->max_network_delay == 0) {
  381. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  382. }
  383. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  384. snprintf (local_error_reason, sizeof(local_error_reason),
  385. "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  386. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  387. goto parse_error;
  388. }
  389. if (totem_config->window_size == 0) {
  390. totem_config->window_size = WINDOW_SIZE;
  391. }
  392. if (totem_config->max_messages == 0) {
  393. totem_config->max_messages = MAX_MESSAGES;
  394. }
  395. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  396. snprintf (local_error_reason, sizeof(local_error_reason),
  397. "The token timeout parameter (%d ms) may not be less then (%d ms).",
  398. totem_config->token_timeout, MINIMUM_TIMEOUT);
  399. goto parse_error;
  400. }
  401. if (totem_config->token_retransmit_timeout == 0) {
  402. totem_config->token_retransmit_timeout =
  403. (int)(totem_config->token_timeout /
  404. (totem_config->token_retransmits_before_loss_const + 0.2));
  405. }
  406. if (totem_config->token_hold_timeout == 0) {
  407. totem_config->token_hold_timeout =
  408. (int)(totem_config->token_retransmit_timeout * 0.8 -
  409. (1000/HZ));
  410. }
  411. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  412. snprintf (local_error_reason, sizeof(local_error_reason),
  413. "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  414. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  415. goto parse_error;
  416. }
  417. if (totem_config->token_hold_timeout == 0) {
  418. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  419. }
  420. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  421. snprintf (local_error_reason, sizeof(local_error_reason),
  422. "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  423. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  424. goto parse_error;
  425. }
  426. if (totem_config->join_timeout == 0) {
  427. totem_config->join_timeout = JOIN_TIMEOUT;
  428. }
  429. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  430. snprintf (local_error_reason, sizeof(local_error_reason),
  431. "The join timeout parameter (%d ms) may not be less then (%d ms).",
  432. totem_config->join_timeout, MINIMUM_TIMEOUT);
  433. goto parse_error;
  434. }
  435. if (totem_config->consensus_timeout == 0) {
  436. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  437. }
  438. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  439. snprintf (local_error_reason, sizeof(local_error_reason),
  440. "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  441. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  442. goto parse_error;
  443. }
  444. if (totem_config->merge_timeout == 0) {
  445. totem_config->merge_timeout = MERGE_TIMEOUT;
  446. }
  447. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  448. snprintf (local_error_reason, sizeof(local_error_reason),
  449. "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  450. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  451. goto parse_error;
  452. }
  453. if (totem_config->downcheck_timeout == 0) {
  454. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  455. }
  456. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  457. snprintf (local_error_reason, sizeof(local_error_reason),
  458. "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  459. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  460. goto parse_error;
  461. }
  462. /*
  463. * RRP values validation
  464. */
  465. if (strcmp (totem_config->rrp_mode, "none") &&
  466. strcmp (totem_config->rrp_mode, "active") &&
  467. strcmp (totem_config->rrp_mode, "passive")) {
  468. snprintf (local_error_reason, sizeof(local_error_reason),
  469. "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  470. goto parse_error;
  471. }
  472. if (totem_config->rrp_problem_count_timeout == 0) {
  473. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  474. }
  475. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  476. snprintf (local_error_reason, sizeof(local_error_reason),
  477. "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  478. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  479. goto parse_error;
  480. }
  481. if (totem_config->rrp_problem_count_threshold == 0) {
  482. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  483. }
  484. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  485. snprintf (local_error_reason, sizeof(local_error_reason),
  486. "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  487. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  488. goto parse_error;
  489. }
  490. if (totem_config->rrp_token_expired_timeout == 0) {
  491. totem_config->rrp_token_expired_timeout =
  492. totem_config->token_retransmit_timeout;
  493. }
  494. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  495. snprintf (local_error_reason, sizeof(local_error_reason),
  496. "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  497. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  498. goto parse_error;
  499. }
  500. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  501. interface_max = 1;
  502. }
  503. if (interface_max < totem_config->interface_count) {
  504. snprintf (parse_error, sizeof(parse_error),
  505. "%d is too many configured interfaces for the rrp_mode setting %s.",
  506. totem_config->interface_count,
  507. totem_config->rrp_mode);
  508. error_reason = parse_error;
  509. goto parse_error;
  510. }
  511. if (totem_config->fail_to_recv_const == 0) {
  512. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  513. }
  514. if (totem_config->seqno_unchanged_const == 0) {
  515. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  516. }
  517. if (totem_config->net_mtu == 0) {
  518. totem_config->net_mtu = 1500;
  519. }
  520. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  521. snprintf (local_error_reason, sizeof(local_error_reason),
  522. "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  523. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  524. goto parse_error;
  525. }
  526. if (totem_config->threads > SEND_THREADS_MAX) {
  527. totem_config->threads = SEND_THREADS_MAX;
  528. }
  529. if (totem_config->secauth == 0) {
  530. totem_config->threads = 0;
  531. }
  532. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  533. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  534. goto parse_error;
  535. }
  536. if (totem_config->vsf_type == NULL) {
  537. totem_config->vsf_type = "none";
  538. }
  539. return (0);
  540. parse_error:
  541. snprintf (error_string_response, sizeof(error_string_response),
  542. "parse error in config: %s\n", error_reason);
  543. *error_string = error_string_response;
  544. return (-1);
  545. }
  546. static int read_keyfile (
  547. const char *key_location,
  548. struct totem_config *totem_config,
  549. const char **error_string)
  550. {
  551. int fd;
  552. int res;
  553. ssize_t expected_key_len = sizeof (totem_config->private_key);
  554. int saved_errno;
  555. fd = open (key_location, O_RDONLY);
  556. if (fd == -1) {
  557. snprintf (error_string_response, sizeof(error_string_response),
  558. "Could not open %s: %s\n",
  559. key_location, strerror (errno));
  560. goto parse_error;
  561. }
  562. res = read (fd, totem_config->private_key, expected_key_len);
  563. saved_errno = errno;
  564. close (fd);
  565. if (res == -1) {
  566. snprintf (error_string_response, sizeof(error_string_response),
  567. "Could not read %s: %s\n",
  568. key_location, strerror (saved_errno));
  569. goto parse_error;
  570. }
  571. totem_config->private_key_len = expected_key_len;
  572. if (res != expected_key_len) {
  573. snprintf (error_string_response, sizeof(error_string_response),
  574. "Could only read %d bits of 1024 bits from %s.\n",
  575. res * 8, key_location);
  576. goto parse_error;
  577. }
  578. return 0;
  579. parse_error:
  580. *error_string = error_string_response;
  581. return (-1);
  582. }
  583. int totem_config_keyread (
  584. struct objdb_iface_ver0 *objdb,
  585. struct totem_config *totem_config,
  586. const char **error_string)
  587. {
  588. int got_key = 0;
  589. const char *key_location = NULL;
  590. hdb_handle_t object_totem_handle;
  591. int res;
  592. memset (totem_config->private_key, 0, 128);
  593. totem_config->private_key_len = 128;
  594. if (totem_config->secauth == 0) {
  595. return (0);
  596. }
  597. res = totem_handle_find (objdb, &object_totem_handle);
  598. if (res == -1) {
  599. return (-1);
  600. }
  601. /* objdb may store the location of the key file */
  602. if (!objdb_get_string (objdb,object_totem_handle, "keyfile", &key_location)
  603. && key_location) {
  604. res = read_keyfile(key_location, totem_config, error_string);
  605. if (res) {
  606. goto key_error;
  607. }
  608. got_key = 1;
  609. } else { /* Or the key itself may be in the objdb */
  610. char *key = NULL;
  611. size_t key_len;
  612. res = objdb->object_key_get (object_totem_handle,
  613. "key",
  614. strlen ("key"),
  615. (void *)&key,
  616. &key_len);
  617. if (res == 0 && key) {
  618. if (key_len > sizeof (totem_config->private_key)) {
  619. goto key_error;
  620. }
  621. memcpy(totem_config->private_key, key, key_len);
  622. totem_config->private_key_len = key_len;
  623. got_key = 1;
  624. }
  625. }
  626. /* In desperation we read the default filename */
  627. if (!got_key) {
  628. const char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  629. if (!filename)
  630. filename = SYSCONFDIR "/ais/authkey";
  631. res = read_keyfile(filename, totem_config, error_string);
  632. if (res)
  633. goto key_error;
  634. }
  635. return (0);
  636. *error_string = error_string_response;
  637. key_error:
  638. return (-1);
  639. }
  640. static void totem_key_change_notify(object_change_type_t change_type,
  641. hdb_handle_t parent_object_handle,
  642. hdb_handle_t object_handle,
  643. const void *object_name_pt, size_t object_name_len,
  644. const void *key_name_pt, size_t key_len,
  645. const void *key_value_pt, size_t key_value_len,
  646. void *priv_data_pt)
  647. {
  648. struct totem_config *totem_config = priv_data_pt;
  649. if (memcmp(object_name_pt, "totem", object_name_len) == 0)
  650. totem_volatile_config_read(global_objdb,
  651. totem_config,
  652. object_handle); // CHECK
  653. }
  654. static void totem_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  655. void *priv_data_pt)
  656. {
  657. struct totem_config *totem_config = priv_data_pt;
  658. hdb_handle_t totem_object_handle;
  659. /*
  660. * A new totem {} key might exist, cancel the
  661. * existing notification at the start of reload,
  662. * and start a new one on the new object when
  663. * it's all settled.
  664. */
  665. if (type == OBJDB_RELOAD_NOTIFY_START) {
  666. global_objdb->object_track_stop(
  667. totem_key_change_notify,
  668. NULL,
  669. NULL,
  670. NULL,
  671. NULL);
  672. }
  673. if (type == OBJDB_RELOAD_NOTIFY_END ||
  674. type == OBJDB_RELOAD_NOTIFY_FAILED) {
  675. if (!totem_handle_find(global_objdb,
  676. &totem_object_handle)) {
  677. add_totem_config_notification(global_objdb, totem_config, totem_object_handle);
  678. /*
  679. * Reload the configuration
  680. */
  681. totem_volatile_config_read(global_objdb,
  682. totem_config,
  683. totem_object_handle);
  684. }
  685. else {
  686. log_printf(LOGSYS_LEVEL_ERROR, "totem objdb tracking stopped, cannot find totem{} handle on objdb\n");
  687. }
  688. }
  689. }
  690. static void add_totem_config_notification(
  691. struct objdb_iface_ver0 *objdb,
  692. struct totem_config *totem_config,
  693. hdb_handle_t totem_object_handle)
  694. {
  695. global_objdb = objdb;
  696. objdb->object_track_start(totem_object_handle,
  697. 1,
  698. totem_key_change_notify,
  699. NULL, // object_create_notify,
  700. NULL, // object_destroy_notify,
  701. NULL, // object_reload_notify
  702. totem_config); // priv_data
  703. /*
  704. * Reload notify must be on the parent object
  705. */
  706. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  707. 1,
  708. NULL, // key_change_notify,
  709. NULL, // object_create_notify,
  710. NULL, // object_destroy_notify,
  711. totem_objdb_reload_notify, // object_reload_notify
  712. totem_config); // priv_data
  713. }