qdevice-net-msg-received.c 30 KB

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