qdevice-net-msg-received.c 27 KB

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