qdevice-net-msg-received.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * Copyright (c) 2015-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "log.h"
  35. #include "log-common.h"
  36. #include "qdevice-net-algorithm.h"
  37. #include "qdevice-net-cast-vote-timer.h"
  38. #include "qdevice-net-heuristics.h"
  39. #include "qdevice-net-msg-received.h"
  40. #include "qdevice-net-send.h"
  41. #include "qdevice-net-votequorum.h"
  42. #include "qdevice-net-echo-request-timer.h"
  43. #include "msg.h"
  44. #include "utils.h"
  45. /*
  46. * -1 - Incompatible tls combination
  47. * 0 - Don't use TLS
  48. * 1 - Use TLS
  49. */
  50. static int
  51. qdevice_net_msg_received_check_tls_compatibility(enum tlv_tls_supported server_tls,
  52. enum tlv_tls_supported client_tls)
  53. {
  54. int res;
  55. res = -1;
  56. switch (server_tls) {
  57. case TLV_TLS_UNSUPPORTED:
  58. switch (client_tls) {
  59. case TLV_TLS_UNSUPPORTED: res = 0; break;
  60. case TLV_TLS_SUPPORTED: res = 0; break;
  61. case TLV_TLS_REQUIRED: res = -1; break;
  62. }
  63. break;
  64. case TLV_TLS_SUPPORTED:
  65. switch (client_tls) {
  66. case TLV_TLS_UNSUPPORTED: res = 0; break;
  67. case TLV_TLS_SUPPORTED: res = 1; break;
  68. case TLV_TLS_REQUIRED: res = 1; break;
  69. }
  70. break;
  71. case TLV_TLS_REQUIRED:
  72. switch (client_tls) {
  73. case TLV_TLS_UNSUPPORTED: res = -1; break;
  74. case TLV_TLS_SUPPORTED: res = 1; break;
  75. case TLV_TLS_REQUIRED: res = 1; break;
  76. }
  77. break;
  78. }
  79. return (res);
  80. }
  81. static int
  82. qdevice_net_msg_received_unexpected_msg(struct qdevice_net_instance *instance,
  83. const struct msg_decoded *msg, const char *msg_str)
  84. {
  85. log(LOG_ERR, "Received unexpected %s message. Disconnecting from server",
  86. msg_str);
  87. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  88. return (-1);
  89. }
  90. static int
  91. qdevice_net_msg_received_init(struct qdevice_net_instance *instance,
  92. const struct msg_decoded *msg)
  93. {
  94. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "init"));
  95. }
  96. static int
  97. qdevice_net_msg_received_preinit(struct qdevice_net_instance *instance,
  98. const struct msg_decoded *msg)
  99. {
  100. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "preinit"));
  101. }
  102. static int
  103. qdevice_net_msg_check_seq_number(struct qdevice_net_instance *instance,
  104. const struct msg_decoded *msg)
  105. {
  106. if (!msg->seq_number_set || msg->seq_number != instance->last_msg_seq_num) {
  107. log(LOG_ERR, "Received message doesn't contain seq_number or "
  108. "it's not expected one.");
  109. return (-1);
  110. }
  111. return (0);
  112. }
  113. static int
  114. qdevice_net_msg_received_preinit_reply(struct qdevice_net_instance *instance,
  115. const struct msg_decoded *msg)
  116. {
  117. int res;
  118. struct send_buffer_list_entry *send_buffer;
  119. log(LOG_DEBUG, "Received preinit reply msg");
  120. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY) {
  121. log(LOG_ERR, "Received unexpected preinit reply message. "
  122. "Disconnecting from server");
  123. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  124. return (-1);
  125. }
  126. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  127. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  128. return (-1);
  129. }
  130. /*
  131. * Check TLS support
  132. */
  133. if (!msg->tls_supported_set || !msg->tls_client_cert_required_set) {
  134. log(LOG_ERR, "Required tls_supported or tls_client_cert_required "
  135. "option is unset");
  136. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  137. return (-1);
  138. }
  139. res = qdevice_net_msg_received_check_tls_compatibility(msg->tls_supported, instance->tls_supported);
  140. if (res == -1) {
  141. log(LOG_ERR, "Incompatible tls configuration (server %u client %u)",
  142. msg->tls_supported, instance->tls_supported);
  143. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_INCOMPATIBLE_TLS;
  144. return (-1);
  145. } else if (res == 1) {
  146. /*
  147. * Start TLS
  148. */
  149. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  150. if (send_buffer == NULL) {
  151. log(LOG_ERR, "Can't allocate send list buffer for "
  152. "starttls msg");
  153. instance->disconnect_reason =
  154. QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  155. return (-1);
  156. }
  157. instance->last_msg_seq_num++;
  158. if (msg_create_starttls(&send_buffer->buffer, 1,
  159. instance->last_msg_seq_num) == 0) {
  160. log(LOG_ERR, "Can't allocate send buffer for starttls msg");
  161. instance->disconnect_reason =
  162. QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  163. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  164. return (-1);
  165. }
  166. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  167. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_STARTTLS_BEING_SENT;
  168. } else if (res == 0) {
  169. if (qdevice_net_send_init(instance) != 0) {
  170. instance->disconnect_reason =
  171. QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  172. return (-1);
  173. }
  174. }
  175. return (0);
  176. }
  177. static int
  178. qdevice_net_msg_received_init_reply(struct qdevice_net_instance *instance,
  179. const struct msg_decoded *msg)
  180. {
  181. size_t zi;
  182. int res;
  183. enum qdevice_heuristics_mode active_heuristics_mode;
  184. log(LOG_DEBUG, "Received init reply msg");
  185. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_INIT_REPLY) {
  186. log(LOG_ERR, "Received unexpected init reply message. "
  187. "Disconnecting from server");
  188. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  189. return (-1);
  190. }
  191. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  192. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  193. return (-1);
  194. }
  195. if (!msg->reply_error_code_set) {
  196. log(LOG_ERR, "Received init reply message without error code."
  197. "Disconnecting from server");
  198. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  199. return (-1);
  200. }
  201. if (msg->reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  202. log(LOG_ERR, "Received init reply message with error code %"PRIu16". "
  203. "Disconnecting from server", msg->reply_error_code);
  204. if (msg->reply_error_code == TLV_REPLY_ERROR_CODE_DUPLICATE_NODE_ID) {
  205. log(LOG_ERR, "Duplicate node id may be result of server not yet "
  206. "accepted this node disconnect. Retry again.");
  207. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_SENT_DUPLICATE_NODE_ID_ERROR;
  208. } else if (msg->reply_error_code == TLV_REPLY_ERROR_CODE_TIE_BREAKER_DIFFERS_FROM_OTHER_NODES) {
  209. log(LOG_ERR, "Configured tie-breaker differs in cluster. This may be "
  210. "result of server not yet accepted this node disconnect. Retry again.");
  211. instance->disconnect_reason =
  212. QDEVICE_NET_DISCONNECT_REASON_SERVER_SENT_TIE_BREAKER_DIFFERS_FROM_OTHER_NODES_ERROR;
  213. } else if (msg->reply_error_code == TLV_REPLY_ERROR_CODE_ALGORITHM_DIFFERS_FROM_OTHER_NODES) {
  214. log(LOG_ERR, "Configured algorithm differs in cluster. This may be "
  215. "result of server not yet accepted this node disconnect. Retry again.");
  216. instance->disconnect_reason =
  217. QDEVICE_NET_DISCONNECT_REASON_SERVER_SENT_ALGORITHM_DIFFERS_FROM_OTHER_NODES_ERROR;
  218. } else {
  219. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_SENT_ERROR;
  220. }
  221. return (-1);
  222. }
  223. if (!msg->server_maximum_request_size_set || !msg->server_maximum_reply_size_set) {
  224. log(LOG_ERR, "Required maximum_request_size or maximum_reply_size "
  225. "option is unset");
  226. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  227. return (-1);
  228. }
  229. if (msg->supported_messages == NULL || msg->supported_options == NULL) {
  230. log(LOG_ERR, "Required supported messages or supported options "
  231. "option is unset");
  232. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  233. return (-1);
  234. }
  235. if (msg->supported_decision_algorithms == NULL) {
  236. log(LOG_ERR, "Required supported decision algorithms option is unset");
  237. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  238. return (-1);
  239. }
  240. if (msg->server_maximum_request_size < instance->advanced_settings->net_min_msg_send_size) {
  241. log(LOG_ERR,
  242. "Server accepts maximum %zu bytes message but this client minimum "
  243. "is %zu bytes.", msg->server_maximum_request_size,
  244. instance->advanced_settings->net_min_msg_send_size);
  245. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_INCOMPATIBLE_MSG_SIZE;
  246. return (-1);
  247. }
  248. if (msg->server_maximum_reply_size > instance->advanced_settings->net_max_msg_receive_size) {
  249. log(LOG_ERR,
  250. "Server may send message up to %zu bytes message but this client maximum "
  251. "is %zu bytes.", msg->server_maximum_reply_size,
  252. instance->advanced_settings->net_max_msg_receive_size);
  253. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_INCOMPATIBLE_MSG_SIZE;
  254. return (-1);
  255. }
  256. /*
  257. * Change buffer sizes
  258. */
  259. dynar_set_max_size(&instance->receive_buffer, msg->server_maximum_reply_size);
  260. send_buffer_list_set_max_buffer_size(&instance->send_buffer_list,
  261. msg->server_maximum_request_size);
  262. /*
  263. * Check if server supports decision algorithm we need
  264. */
  265. res = 0;
  266. for (zi = 0; zi < msg->no_supported_decision_algorithms && !res; zi++) {
  267. if (msg->supported_decision_algorithms[zi] == instance->decision_algorithm) {
  268. res = 1;
  269. }
  270. }
  271. if (!res) {
  272. log(LOG_ERR, "Server doesn't support required decision algorithm");
  273. instance->disconnect_reason =
  274. QDEVICE_NET_DISCONNECT_REASON_SERVER_DOESNT_SUPPORT_REQUIRED_ALGORITHM;
  275. return (-1);
  276. }
  277. /*
  278. * Check if server supports heuristics/keep active partition tie breaker
  279. */
  280. instance->server_supports_heuristics = 0;
  281. instance->server_supports_keep_active_partition_tie_breaker = 0;
  282. for (zi = 0; zi < msg->no_supported_options; zi++) {
  283. if (msg->supported_options[zi] == TLV_OPT_HEURISTICS) {
  284. instance->server_supports_heuristics = 1;
  285. }
  286. if (msg->supported_options[zi] == TLV_OPT_KEEP_ACTIVE_PARTITION_TIE_BREAKER) {
  287. instance->server_supports_keep_active_partition_tie_breaker = 1;
  288. }
  289. }
  290. if (!instance->server_supports_heuristics) {
  291. active_heuristics_mode = instance->qdevice_instance_ptr->heuristics_instance.mode;
  292. if (active_heuristics_mode == QDEVICE_HEURISTICS_MODE_ENABLED ||
  293. active_heuristics_mode == QDEVICE_HEURISTICS_MODE_SYNC) {
  294. log(LOG_ERR, "Heuristics are enabled but not supported by server");
  295. instance->disconnect_reason =
  296. QDEVICE_NET_DISCONNECT_REASON_SERVER_DOESNT_SUPPORT_REQUIRED_OPT;
  297. return (-1);
  298. }
  299. }
  300. /*
  301. * Finally fully connected so it's possible to remove connection timer
  302. */
  303. if (instance->connect_timer != NULL) {
  304. timer_list_entry_delete(
  305. pr_poll_loop_get_timer_list(&instance->qdevice_instance_ptr->main_poll_loop),
  306. instance->connect_timer);
  307. instance->connect_timer = NULL;
  308. }
  309. /*
  310. * Server accepted heartbeat interval -> schedule regular sending of echo request
  311. */
  312. if (qdevice_net_echo_request_timer_schedule(instance) != 0) {
  313. return (-1);
  314. }
  315. /*
  316. * Run heuristics (even when it is disabled, undefined result is ok, rest of sending
  317. * is handled by qdevice_net_connect_heuristics_exec_result_callback
  318. */
  319. if (qdevice_net_heuristics_exec_after_connect(instance) != 0) {
  320. return (-1);
  321. }
  322. return (0);
  323. }
  324. static int
  325. qdevice_net_msg_received_starttls(struct qdevice_net_instance *instance,
  326. const struct msg_decoded *msg)
  327. {
  328. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "starttls"));
  329. }
  330. static int
  331. qdevice_net_msg_received_server_error(struct qdevice_net_instance *instance,
  332. const struct msg_decoded *msg)
  333. {
  334. if (!msg->reply_error_code_set) {
  335. log(LOG_ERR, "Received server error without error code set. "
  336. "Disconnecting from server");
  337. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  338. } else {
  339. log(LOG_ERR, "Received server error %"PRIu16". "
  340. "Disconnecting from server", msg->reply_error_code);
  341. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_SENT_ERROR;
  342. }
  343. return (-1);
  344. }
  345. static int
  346. qdevice_net_msg_received_set_option(struct qdevice_net_instance *instance,
  347. const struct msg_decoded *msg)
  348. {
  349. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "set option"));
  350. }
  351. static int
  352. qdevice_net_msg_received_set_option_reply(struct qdevice_net_instance *instance,
  353. const struct msg_decoded *msg)
  354. {
  355. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  356. log(LOG_ERR, "Received unexpected set option reply message. "
  357. "Disconnecting from server");
  358. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  359. return (-1);
  360. }
  361. log(LOG_DEBUG, "Received set option reply seq(%u) = "UTILS_PRI_MSG_SEQ ", "
  362. "HB(%u) = %" PRIu32 "ms, KAP Tie-breaker(%u) = %s",
  363. msg->seq_number_set, msg->seq_number,
  364. msg->heartbeat_interval_set, msg->heartbeat_interval,
  365. msg->keep_active_partition_tie_breaker_set,
  366. tlv_keep_active_partition_tie_breaker_to_str(msg->keep_active_partition_tie_breaker));
  367. if (msg->heartbeat_interval_set) {
  368. if (qdevice_net_echo_request_timer_schedule(instance) != 0) {
  369. return (-1);
  370. }
  371. }
  372. return (0);
  373. }
  374. static int
  375. qdevice_net_msg_received_echo_request(struct qdevice_net_instance *instance,
  376. const struct msg_decoded *msg)
  377. {
  378. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "echo request"));
  379. }
  380. static int
  381. qdevice_net_msg_received_echo_reply(struct qdevice_net_instance *instance,
  382. const struct msg_decoded *msg)
  383. {
  384. if (!msg->seq_number_set) {
  385. log(LOG_ERR, "Received echo reply message doesn't contain seq_number.");
  386. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  387. return (-1);
  388. }
  389. if (msg->seq_number != instance->echo_request_expected_msg_seq_num) {
  390. log(LOG_WARNING, "Received echo reply message seq_number is not expected one.");
  391. }
  392. if (qdevice_net_algorithm_echo_reply_received(instance, msg->seq_number,
  393. msg->seq_number == instance->echo_request_expected_msg_seq_num) != 0) {
  394. log(LOG_DEBUG, "Algorithm returned error. Disconnecting");
  395. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_ECHO_REPLY_RECEIVED_ERR;
  396. return (-1);
  397. }
  398. instance->echo_reply_received_msg_seq_num = msg->seq_number;
  399. instance->last_echo_reply_received_time = time(NULL);
  400. return (0);
  401. }
  402. static int
  403. qdevice_net_msg_received_node_list(struct qdevice_net_instance *instance,
  404. const struct msg_decoded *msg)
  405. {
  406. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "node list"));
  407. }
  408. static int
  409. qdevice_net_msg_received_node_list_reply(struct qdevice_net_instance *instance,
  410. const struct msg_decoded *msg)
  411. {
  412. const char *str;
  413. enum tlv_vote result_vote;
  414. int res;
  415. int case_processed;
  416. int ring_id_is_valid;
  417. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  418. log(LOG_ERR, "Received unexpected node list reply message. "
  419. "Disconnecting from server");
  420. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  421. return (-1);
  422. }
  423. if (!msg->vote_set || !msg->seq_number_set || !msg->node_list_type_set) {
  424. log(LOG_ERR, "Received node list reply message without "
  425. "required options. Disconnecting from server");
  426. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  427. return (-1);
  428. }
  429. if (!msg->ring_id_set) {
  430. log(LOG_ERR, "Received node list reply message "
  431. "without ring id set. Disconnecting from server");
  432. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  433. return (-1);
  434. }
  435. str = NULL;
  436. switch (msg->node_list_type) {
  437. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG: str = "initial config"; break;
  438. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG: str = "changed config"; break;
  439. case TLV_NODE_LIST_TYPE_MEMBERSHIP: str ="membership"; break;
  440. case TLV_NODE_LIST_TYPE_QUORUM: str ="quorum"; break;
  441. /*
  442. * Default is not defined intentionally. Compiler shows warning when new node list type
  443. * is added
  444. */
  445. }
  446. if (str == NULL) {
  447. log(LOG_CRIT, "qdevice_net_msg_received_node_list_reply fatal error. "
  448. "Unhandled node_list_type (debug output)");
  449. exit(EXIT_FAILURE);
  450. }
  451. log(LOG_DEBUG, "Received %s node list reply", str);
  452. log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  453. log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  454. log(LOG_DEBUG, " ring id = ("UTILS_PRI_RING_ID")",
  455. msg->ring_id.node_id, msg->ring_id.seq);
  456. /*
  457. * Call algorithm
  458. */
  459. result_vote = msg->vote;
  460. if (!tlv_ring_id_eq(&msg->ring_id, &instance->last_sent_ring_id)) {
  461. ring_id_is_valid = 0;
  462. log(LOG_DEBUG, "Received node list reply with old ring id.");
  463. } else {
  464. ring_id_is_valid = 1;
  465. }
  466. case_processed = 0;
  467. switch (msg->node_list_type) {
  468. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  469. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  470. case_processed = 1;
  471. res = qdevice_net_algorithm_config_node_list_reply_received(instance,
  472. msg->seq_number, (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG),
  473. &msg->ring_id, ring_id_is_valid, &result_vote);
  474. break;
  475. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  476. case_processed = 1;
  477. res = qdevice_net_algorithm_membership_node_list_reply_received(instance,
  478. msg->seq_number, &msg->ring_id, ring_id_is_valid, &result_vote);
  479. break;
  480. case TLV_NODE_LIST_TYPE_QUORUM:
  481. case_processed = 1;
  482. res = qdevice_net_algorithm_quorum_node_list_reply_received(instance,
  483. msg->seq_number, &msg->ring_id, ring_id_is_valid, &result_vote);
  484. break;
  485. /*
  486. * Default is not defined intentionally. Compiler shows warning when new node list type
  487. * is added
  488. */
  489. }
  490. if (!case_processed) {
  491. log(LOG_CRIT, "qdevice_net_msg_received_node_list_reply fatal error. "
  492. "Unhandled node_list_type (algorithm call)");
  493. exit(EXIT_FAILURE);
  494. }
  495. if (res != 0) {
  496. log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  497. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_NODE_LIST_REPLY_ERR;
  498. return (-1);
  499. } else {
  500. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  501. }
  502. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  503. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  504. return (-1);
  505. }
  506. return (0);
  507. }
  508. static int
  509. qdevice_net_msg_received_ask_for_vote(struct qdevice_net_instance *instance,
  510. const struct msg_decoded *msg)
  511. {
  512. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "ask for vote"));
  513. }
  514. static int
  515. qdevice_net_msg_received_ask_for_vote_reply(struct qdevice_net_instance *instance,
  516. const struct msg_decoded *msg)
  517. {
  518. enum tlv_vote result_vote;
  519. int ring_id_is_valid;
  520. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  521. log(LOG_ERR, "Received unexpected ask for vote reply message. "
  522. "Disconnecting from server");
  523. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  524. return (-1);
  525. }
  526. if (!msg->vote_set || !msg->seq_number_set || !msg->ring_id_set) {
  527. log(LOG_ERR, "Received ask for vote reply message without "
  528. "required options. Disconnecting from server");
  529. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  530. return (-1);
  531. }
  532. log(LOG_DEBUG, "Received ask for vote reply");
  533. log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  534. log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  535. log(LOG_DEBUG, " ring id = ("UTILS_PRI_RING_ID")",
  536. msg->ring_id.node_id, msg->ring_id.seq);
  537. result_vote = msg->vote;
  538. if (!tlv_ring_id_eq(&msg->ring_id, &instance->last_sent_ring_id)) {
  539. ring_id_is_valid = 0;
  540. log(LOG_DEBUG, "Received ask for vote reply with old ring id.");
  541. } else {
  542. ring_id_is_valid = 1;
  543. }
  544. if (qdevice_net_algorithm_ask_for_vote_reply_received(instance, msg->seq_number,
  545. &msg->ring_id, ring_id_is_valid, &result_vote) != 0) {
  546. log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  547. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_ASK_FOR_VOTE_REPLY_ERR;
  548. return (-1);
  549. } else {
  550. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  551. }
  552. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  553. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  554. return (-1);
  555. }
  556. return (0);
  557. }
  558. static int
  559. qdevice_net_msg_received_vote_info(struct qdevice_net_instance *instance,
  560. const struct msg_decoded *msg)
  561. {
  562. struct send_buffer_list_entry *send_buffer;
  563. enum tlv_vote result_vote;
  564. int ring_id_is_valid;
  565. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  566. log(LOG_ERR, "Received unexpected vote info message. "
  567. "Disconnecting from server");
  568. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  569. return (-1);
  570. }
  571. if (!msg->vote_set || !msg->seq_number_set || !msg->ring_id_set) {
  572. log(LOG_ERR, "Received node list reply message without "
  573. "required options. Disconnecting from server");
  574. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  575. return (-1);
  576. }
  577. log(LOG_DEBUG, "Received vote info");
  578. log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  579. log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  580. log(LOG_DEBUG, " ring id = ("UTILS_PRI_RING_ID")",
  581. msg->ring_id.node_id, msg->ring_id.seq);
  582. result_vote = msg->vote;
  583. if (!tlv_ring_id_eq(&msg->ring_id, &instance->last_sent_ring_id)) {
  584. ring_id_is_valid = 0;
  585. log(LOG_DEBUG, "Received vote info with old ring id.");
  586. } else {
  587. ring_id_is_valid = 1;
  588. }
  589. if (qdevice_net_algorithm_vote_info_received(instance, msg->seq_number,
  590. &msg->ring_id, ring_id_is_valid, &result_vote) != 0) {
  591. log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  592. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTE_INFO_ERR;
  593. return (-1);
  594. } else {
  595. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  596. }
  597. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  598. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  599. return (-1);
  600. }
  601. /*
  602. * Create reply message
  603. */
  604. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  605. if (send_buffer == NULL) {
  606. log(LOG_ERR, "Can't allocate send list buffer for "
  607. "vote info reply msg");
  608. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  609. return (-1);
  610. }
  611. if (msg_create_vote_info_reply(&send_buffer->buffer, msg->seq_number) == 0) {
  612. log(LOG_ERR, "Can't allocate send buffer for "
  613. "vote info reply list msg");
  614. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  615. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  616. return (-1);
  617. }
  618. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  619. return (0);
  620. }
  621. static int
  622. qdevice_net_msg_received_vote_info_reply(struct qdevice_net_instance *instance,
  623. const struct msg_decoded *msg)
  624. {
  625. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "vote info reply"));
  626. }
  627. static int
  628. qdevice_net_msg_received_heuristics_change(struct qdevice_net_instance *instance,
  629. const struct msg_decoded *msg)
  630. {
  631. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "heuristics change"));
  632. }
  633. static int
  634. qdevice_net_msg_received_heuristics_change_reply(struct qdevice_net_instance *instance,
  635. const struct msg_decoded *msg)
  636. {
  637. enum tlv_vote result_vote;
  638. int ring_id_is_valid;
  639. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  640. log(LOG_ERR, "Received unexpected heuristics change reply message. "
  641. "Disconnecting from server");
  642. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  643. return (-1);
  644. }
  645. if (!msg->vote_set || !msg->seq_number_set || !msg->ring_id_set ||
  646. msg->heuristics == TLV_HEURISTICS_UNDEFINED) {
  647. log(LOG_ERR, "Received heuristics change reply message without "
  648. "required options. Disconnecting from server");
  649. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  650. return (-1);
  651. }
  652. log(LOG_DEBUG, "Received heuristics change reply");
  653. log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  654. log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  655. log(LOG_DEBUG, " ring id = ("UTILS_PRI_RING_ID")",
  656. msg->ring_id.node_id, msg->ring_id.seq);
  657. log(LOG_DEBUG, " heuristics = %s", tlv_heuristics_to_str(msg->heuristics));
  658. result_vote = msg->vote;
  659. if (!tlv_ring_id_eq(&msg->ring_id, &instance->last_sent_ring_id)) {
  660. ring_id_is_valid = 0;
  661. log(LOG_DEBUG, "Received heuristics change reply with old ring id.");
  662. } else {
  663. ring_id_is_valid = 1;
  664. }
  665. if (qdevice_net_algorithm_heuristics_change_reply_received(instance, msg->seq_number,
  666. &msg->ring_id, ring_id_is_valid, msg->heuristics, &result_vote) != 0) {
  667. log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  668. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_HEURISTICS_CHANGE_REPLY_ERR;
  669. return (-1);
  670. } else {
  671. log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  672. }
  673. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  674. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  675. return (-1);
  676. }
  677. return (0);
  678. }
  679. int
  680. qdevice_net_msg_received(struct qdevice_net_instance *instance)
  681. {
  682. struct msg_decoded msg;
  683. int res;
  684. int ret_val;
  685. int msg_processed;
  686. msg_decoded_init(&msg);
  687. res = msg_decode(&instance->receive_buffer, &msg);
  688. if (res != 0) {
  689. /*
  690. * Error occurred. Disconnect.
  691. */
  692. log_common_msg_decode_error(res);
  693. log(LOG_ERR, "Disconnecting from server");
  694. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_MSG_DECODE_ERROR;
  695. return (-1);
  696. }
  697. ret_val = 0;
  698. msg_processed = 0;
  699. switch (msg.type) {
  700. case MSG_TYPE_INIT:
  701. msg_processed = 1;
  702. ret_val = qdevice_net_msg_received_init(instance, &msg);
  703. break;
  704. case MSG_TYPE_PREINIT:
  705. msg_processed = 1;
  706. ret_val = qdevice_net_msg_received_preinit(instance, &msg);
  707. break;
  708. case MSG_TYPE_PREINIT_REPLY:
  709. msg_processed = 1;
  710. ret_val = qdevice_net_msg_received_preinit_reply(instance, &msg);
  711. break;
  712. case MSG_TYPE_STARTTLS:
  713. msg_processed = 1;
  714. ret_val = qdevice_net_msg_received_starttls(instance, &msg);
  715. break;
  716. case MSG_TYPE_SERVER_ERROR:
  717. msg_processed = 1;
  718. ret_val = qdevice_net_msg_received_server_error(instance, &msg);
  719. break;
  720. case MSG_TYPE_INIT_REPLY:
  721. msg_processed = 1;
  722. ret_val = qdevice_net_msg_received_init_reply(instance, &msg);
  723. break;
  724. case MSG_TYPE_SET_OPTION:
  725. msg_processed = 1;
  726. ret_val = qdevice_net_msg_received_set_option(instance, &msg);
  727. break;
  728. case MSG_TYPE_SET_OPTION_REPLY:
  729. msg_processed = 1;
  730. ret_val = qdevice_net_msg_received_set_option_reply(instance, &msg);
  731. break;
  732. case MSG_TYPE_ECHO_REQUEST:
  733. msg_processed = 1;
  734. ret_val = qdevice_net_msg_received_echo_request(instance, &msg);
  735. break;
  736. case MSG_TYPE_ECHO_REPLY:
  737. msg_processed = 1;
  738. ret_val = qdevice_net_msg_received_echo_reply(instance, &msg);
  739. break;
  740. case MSG_TYPE_NODE_LIST:
  741. msg_processed = 1;
  742. ret_val = qdevice_net_msg_received_node_list(instance, &msg);
  743. break;
  744. case MSG_TYPE_NODE_LIST_REPLY:
  745. msg_processed = 1;
  746. ret_val = qdevice_net_msg_received_node_list_reply(instance, &msg);
  747. break;
  748. case MSG_TYPE_ASK_FOR_VOTE:
  749. msg_processed = 1;
  750. ret_val = qdevice_net_msg_received_ask_for_vote(instance, &msg);
  751. break;
  752. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  753. msg_processed = 1;
  754. ret_val = qdevice_net_msg_received_ask_for_vote_reply(instance, &msg);
  755. break;
  756. case MSG_TYPE_VOTE_INFO:
  757. msg_processed = 1;
  758. ret_val = qdevice_net_msg_received_vote_info(instance, &msg);
  759. break;
  760. case MSG_TYPE_VOTE_INFO_REPLY:
  761. msg_processed = 1;
  762. ret_val = qdevice_net_msg_received_vote_info_reply(instance, &msg);
  763. break;
  764. case MSG_TYPE_HEURISTICS_CHANGE:
  765. msg_processed = 1;
  766. ret_val = qdevice_net_msg_received_heuristics_change(instance, &msg);
  767. break;
  768. case MSG_TYPE_HEURISTICS_CHANGE_REPLY:
  769. msg_processed = 1;
  770. ret_val = qdevice_net_msg_received_heuristics_change_reply(instance, &msg);
  771. /*
  772. * Default is not defined intentionally. Compiler shows warning when msg type is added
  773. */
  774. }
  775. if (!msg_processed) {
  776. log(LOG_ERR, "Received unsupported message %u. "
  777. "Disconnecting from server", msg.type);
  778. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  779. ret_val = -1;
  780. }
  781. msg_decoded_destroy(&msg);
  782. return (ret_val);
  783. }