qdevice-net-msg-received.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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->advanced_settings->net_min_msg_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,
  252. instance->advanced_settings->net_min_msg_send_size);
  253. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_INCOMPATIBLE_MSG_SIZE;
  254. return (-1);
  255. }
  256. if (msg->server_maximum_reply_size > instance->advanced_settings->net_max_msg_receive_size) {
  257. qdevice_log(LOG_ERR,
  258. "Server may send message up to %zu bytes message but this client maximum "
  259. "is %zu bytes.", msg->server_maximum_reply_size,
  260. instance->advanced_settings->net_max_msg_receive_size);
  261. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_INCOMPATIBLE_MSG_SIZE;
  262. return (-1);
  263. }
  264. /*
  265. * Change buffer sizes
  266. */
  267. dynar_set_max_size(&instance->receive_buffer, msg->server_maximum_reply_size);
  268. send_buffer_list_set_max_buffer_size(&instance->send_buffer_list,
  269. msg->server_maximum_request_size);
  270. /*
  271. * Check if server supports decision algorithm we need
  272. */
  273. res = 0;
  274. for (zi = 0; zi < msg->no_supported_decision_algorithms && !res; zi++) {
  275. if (msg->supported_decision_algorithms[zi] == instance->decision_algorithm) {
  276. res = 1;
  277. }
  278. }
  279. if (!res) {
  280. qdevice_log(LOG_ERR, "Server doesn't support required decision algorithm");
  281. instance->disconnect_reason =
  282. QDEVICE_NET_DISCONNECT_REASON_SERVER_DOESNT_SUPPORT_REQUIRED_ALGORITHM;
  283. return (-1);
  284. }
  285. /*
  286. * Finally fully connected so it's possible to remove connection timer
  287. */
  288. if (instance->connect_timer != NULL) {
  289. timer_list_delete(&instance->main_timer_list, instance->connect_timer);
  290. instance->connect_timer = NULL;
  291. }
  292. /*
  293. * Server accepted heartbeat interval -> schedule regular sending of echo request
  294. */
  295. qdevice_net_echo_request_timer_schedule(instance);
  296. send_config_node_list = 1;
  297. send_membership_node_list = 1;
  298. send_quorum_node_list = 1;
  299. vote = TLV_VOTE_WAIT_FOR_REPLY;
  300. if (qdevice_net_algorithm_connected(instance, &send_config_node_list, &send_membership_node_list,
  301. &send_quorum_node_list, &vote) != 0) {
  302. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  303. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_CONNECTED_ERR;
  304. return (-1);
  305. } else {
  306. qdevice_log(LOG_DEBUG, "Algorithm decided to %s config node list, %s membership "
  307. "node list, %s quorum node list and result vote is %s",
  308. (send_config_node_list ? "send" : "not send"),
  309. (send_membership_node_list ? "send" : "not send"),
  310. (send_quorum_node_list ? "send" : "not send"),
  311. tlv_vote_to_str(vote));
  312. }
  313. /*
  314. * Now we can finally really send node list, votequorum node list and update timer
  315. */
  316. if (send_config_node_list) {
  317. if (qdevice_net_send_config_node_list(instance,
  318. &instance->qdevice_instance_ptr->config_node_list,
  319. instance->qdevice_instance_ptr->config_node_list_version_set,
  320. instance->qdevice_instance_ptr->config_node_list_version, 1) != 0) {
  321. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  322. return (-1);
  323. }
  324. }
  325. if (send_membership_node_list) {
  326. qdevice_net_votequorum_ring_id_to_tlv(&tlv_rid,
  327. &instance->qdevice_instance_ptr->vq_node_list_ring_id);
  328. if (qdevice_net_send_membership_node_list(instance, &tlv_rid,
  329. instance->qdevice_instance_ptr->vq_node_list_entries,
  330. instance->qdevice_instance_ptr->vq_node_list) != 0) {
  331. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  332. return (-1);
  333. }
  334. }
  335. if (send_quorum_node_list) {
  336. quorate = (instance->qdevice_instance_ptr->vq_quorum_quorate ?
  337. TLV_QUORATE_QUORATE : TLV_QUORATE_INQUORATE);
  338. if (qdevice_net_send_quorum_node_list(instance,
  339. quorate,
  340. instance->qdevice_instance_ptr->vq_quorum_node_list_entries,
  341. instance->qdevice_instance_ptr->vq_quorum_node_list) != 0) {
  342. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  343. return (-1);
  344. }
  345. }
  346. if (qdevice_net_cast_vote_timer_update(instance, vote) != 0) {
  347. qdevice_log(LOG_CRIT, "qdevice_net_msg_received_set_option_reply fatal error. "
  348. " Can't update cast vote timer vote");
  349. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  350. }
  351. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS;
  352. instance->connected_since_time = time(NULL);
  353. return (0);
  354. }
  355. static int
  356. qdevice_net_msg_received_starttls(struct qdevice_net_instance *instance,
  357. const struct msg_decoded *msg)
  358. {
  359. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "starttls"));
  360. }
  361. static int
  362. qdevice_net_msg_received_server_error(struct qdevice_net_instance *instance,
  363. const struct msg_decoded *msg)
  364. {
  365. if (!msg->reply_error_code_set) {
  366. qdevice_log(LOG_ERR, "Received server error without error code set. "
  367. "Disconnecting from server");
  368. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  369. } else {
  370. qdevice_log(LOG_ERR, "Received server error %"PRIu16". "
  371. "Disconnecting from server", msg->reply_error_code);
  372. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_SENT_ERROR;
  373. }
  374. return (-1);
  375. }
  376. static int
  377. qdevice_net_msg_received_set_option(struct qdevice_net_instance *instance,
  378. const struct msg_decoded *msg)
  379. {
  380. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "set option"));
  381. }
  382. static int
  383. qdevice_net_msg_received_set_option_reply(struct qdevice_net_instance *instance,
  384. const struct msg_decoded *msg)
  385. {
  386. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  387. qdevice_log(LOG_ERR, "Received unexpected set option reply message. "
  388. "Disconnecting from server");
  389. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  390. return (-1);
  391. }
  392. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  393. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  394. return (-1);
  395. }
  396. qdevice_net_echo_request_timer_schedule(instance);
  397. return (0);
  398. }
  399. static int
  400. qdevice_net_msg_received_echo_request(struct qdevice_net_instance *instance,
  401. const struct msg_decoded *msg)
  402. {
  403. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "echo request"));
  404. }
  405. static int
  406. qdevice_net_msg_received_echo_reply(struct qdevice_net_instance *instance,
  407. const struct msg_decoded *msg)
  408. {
  409. if (!msg->seq_number_set) {
  410. qdevice_log(LOG_ERR, "Received echo reply message doesn't contain seq_number.");
  411. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  412. return (-1);
  413. }
  414. if (msg->seq_number != instance->echo_request_expected_msg_seq_num) {
  415. qdevice_log(LOG_WARNING, "Received echo reply message seq_number is not expected one.");
  416. }
  417. if (qdevice_net_algorithm_echo_reply_received(instance, msg->seq_number,
  418. msg->seq_number == instance->echo_request_expected_msg_seq_num) != 0) {
  419. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting");
  420. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_ECHO_REPLY_RECEIVED_ERR;
  421. return (-1);
  422. }
  423. instance->echo_reply_received_msg_seq_num = msg->seq_number;
  424. instance->last_echo_reply_received_time = time(NULL);
  425. return (0);
  426. }
  427. static int
  428. qdevice_net_msg_received_node_list(struct qdevice_net_instance *instance,
  429. const struct msg_decoded *msg)
  430. {
  431. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "node list"));
  432. }
  433. static int
  434. qdevice_net_msg_received_node_list_reply(struct qdevice_net_instance *instance,
  435. const struct msg_decoded *msg)
  436. {
  437. const char *str;
  438. enum tlv_vote result_vote;
  439. int res;
  440. int case_processed;
  441. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  442. qdevice_log(LOG_ERR, "Received unexpected node list reply message. "
  443. "Disconnecting from server");
  444. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  445. return (-1);
  446. }
  447. if (!msg->vote_set || !msg->seq_number_set || !msg->node_list_type_set) {
  448. qdevice_log(LOG_ERR, "Received node list reply message without "
  449. "required options. Disconnecting from server");
  450. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  451. return (-1);
  452. }
  453. if (msg->node_list_type == TLV_NODE_LIST_TYPE_MEMBERSHIP && !msg->ring_id_set) {
  454. qdevice_log(LOG_ERR, "Received node list reply message with type membership "
  455. "without ring id set. Disconnecting from server");
  456. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  457. return (-1);
  458. }
  459. str = NULL;
  460. switch (msg->node_list_type) {
  461. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG: str = "initial config"; break;
  462. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG: str = "changed config"; break;
  463. case TLV_NODE_LIST_TYPE_MEMBERSHIP: str ="membership"; break;
  464. case TLV_NODE_LIST_TYPE_QUORUM: str ="quorum"; break;
  465. /*
  466. * Default is not defined intentionally. Compiler shows warning when new node list type
  467. * is added
  468. */
  469. }
  470. if (str == NULL) {
  471. qdevice_log(LOG_CRIT, "qdevice_net_msg_received_node_list_reply fatal error. "
  472. "Unhandled node_list_type (debug output)");
  473. exit(1);
  474. }
  475. qdevice_log(LOG_DEBUG, "Received %s node list reply", str);
  476. qdevice_log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  477. qdevice_log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  478. if (msg->ring_id_set) {
  479. qdevice_log(LOG_DEBUG, " ring id = ("UTILS_PRI_RING_ID")",
  480. msg->ring_id.node_id, msg->ring_id.seq);
  481. }
  482. /*
  483. * Call algorithm
  484. */
  485. result_vote = msg->vote;
  486. case_processed = 0;
  487. switch (msg->node_list_type) {
  488. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  489. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  490. case_processed = 1;
  491. res = qdevice_net_algorithm_config_node_list_reply_received(instance,
  492. msg->seq_number, (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG),
  493. &result_vote);
  494. break;
  495. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  496. case_processed = 1;
  497. res = qdevice_net_algorithm_membership_node_list_reply_received(instance,
  498. msg->seq_number, &msg->ring_id, &result_vote);
  499. break;
  500. case TLV_NODE_LIST_TYPE_QUORUM:
  501. case_processed = 1;
  502. res = qdevice_net_algorithm_quorum_node_list_reply_received(instance,
  503. msg->seq_number, &result_vote);
  504. break;
  505. /*
  506. * Default is not defined intentionally. Compiler shows warning when new node list type
  507. * is added
  508. */
  509. }
  510. if (!case_processed) {
  511. qdevice_log(LOG_CRIT, "qdevice_net_msg_received_node_list_reply fatal error. "
  512. "Unhandled node_list_type (algorithm call)");
  513. exit(1);
  514. }
  515. if (res != 0) {
  516. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  517. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_NODE_LIST_REPLY_ERR;
  518. return (-1);
  519. } else {
  520. qdevice_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(msg->vote));
  521. }
  522. if (result_vote != TLV_VOTE_NO_CHANGE) {
  523. if (msg->node_list_type == TLV_NODE_LIST_TYPE_MEMBERSHIP &&
  524. !tlv_ring_id_eq(&msg->ring_id, &instance->last_sent_ring_id)) {
  525. qdevice_log(LOG_INFO, "Received membership node list reply with "
  526. "old ring id. Not updating timer");
  527. } else {
  528. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  529. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  530. return (-1);
  531. }
  532. }
  533. }
  534. return (0);
  535. }
  536. static int
  537. qdevice_net_msg_received_ask_for_vote(struct qdevice_net_instance *instance,
  538. const struct msg_decoded *msg)
  539. {
  540. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "ask for vote"));
  541. }
  542. static int
  543. qdevice_net_msg_received_ask_for_vote_reply(struct qdevice_net_instance *instance,
  544. const struct msg_decoded *msg)
  545. {
  546. enum tlv_vote result_vote;
  547. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  548. qdevice_log(LOG_ERR, "Received unexpected ask for vote reply message. "
  549. "Disconnecting from server");
  550. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  551. return (-1);
  552. }
  553. if (!msg->vote_set || !msg->seq_number_set) {
  554. qdevice_log(LOG_ERR, "Received node list reply message without "
  555. "required options. Disconnecting from server");
  556. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  557. return (-1);
  558. }
  559. qdevice_log(LOG_DEBUG, "Received ask for vote reply");
  560. qdevice_log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  561. qdevice_log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  562. result_vote = msg->vote;
  563. if (qdevice_net_algorithm_ask_for_vote_reply_received(instance, msg->seq_number,
  564. &result_vote) != 0) {
  565. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  566. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_ASK_FOR_VOTE_REPLY_ERR;
  567. return (-1);
  568. } else {
  569. qdevice_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(msg->vote));
  570. }
  571. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  572. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  573. return (-1);
  574. }
  575. return (0);
  576. }
  577. static int
  578. qdevice_net_msg_received_vote_info(struct qdevice_net_instance *instance,
  579. const struct msg_decoded *msg)
  580. {
  581. struct send_buffer_list_entry *send_buffer;
  582. enum tlv_vote result_vote;
  583. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  584. qdevice_log(LOG_ERR, "Received unexpected vote info message. "
  585. "Disconnecting from server");
  586. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  587. return (-1);
  588. }
  589. if (!msg->vote_set || !msg->seq_number_set) {
  590. qdevice_log(LOG_ERR, "Received node list reply message without "
  591. "required options. Disconnecting from server");
  592. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  593. return (-1);
  594. }
  595. qdevice_log(LOG_DEBUG, "Received vote info");
  596. qdevice_log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  597. qdevice_log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  598. result_vote = msg->vote;
  599. if (qdevice_net_algorithm_vote_info_received(instance, msg->seq_number,
  600. &result_vote) != 0) {
  601. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  602. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTE_INFO_ERR;
  603. return (-1);
  604. } else {
  605. qdevice_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  606. }
  607. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  608. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  609. return (-1);
  610. }
  611. /*
  612. * Create reply message
  613. */
  614. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  615. if (send_buffer == NULL) {
  616. qdevice_log(LOG_ERR, "Can't allocate send list buffer for "
  617. "vote info reply msg");
  618. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  619. return (-1);
  620. }
  621. if (msg_create_vote_info_reply(&send_buffer->buffer, msg->seq_number) == 0) {
  622. qdevice_log(LOG_ERR, "Can't allocate send buffer for "
  623. "vote info reply list msg");
  624. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  625. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  626. return (-1);
  627. }
  628. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  629. return (0);
  630. }
  631. static int
  632. qdevice_net_msg_received_vote_info_reply(struct qdevice_net_instance *instance,
  633. const struct msg_decoded *msg)
  634. {
  635. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "vote info reply"));
  636. }
  637. int
  638. qdevice_net_msg_received(struct qdevice_net_instance *instance)
  639. {
  640. struct msg_decoded msg;
  641. int res;
  642. int ret_val;
  643. int msg_processed;
  644. msg_decoded_init(&msg);
  645. res = msg_decode(&instance->receive_buffer, &msg);
  646. if (res != 0) {
  647. /*
  648. * Error occurred. Disconnect.
  649. */
  650. qdevice_net_msg_received_log_msg_decode_error(res);
  651. qdevice_log(LOG_ERR, "Disconnecting from server");
  652. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_MSG_DECODE_ERROR;
  653. return (-1);
  654. }
  655. ret_val = 0;
  656. msg_processed = 0;
  657. switch (msg.type) {
  658. case MSG_TYPE_INIT:
  659. msg_processed = 1;
  660. ret_val = qdevice_net_msg_received_init(instance, &msg);
  661. break;
  662. case MSG_TYPE_PREINIT:
  663. msg_processed = 1;
  664. ret_val = qdevice_net_msg_received_preinit(instance, &msg);
  665. break;
  666. case MSG_TYPE_PREINIT_REPLY:
  667. msg_processed = 1;
  668. ret_val = qdevice_net_msg_received_preinit_reply(instance, &msg);
  669. break;
  670. case MSG_TYPE_STARTTLS:
  671. msg_processed = 1;
  672. ret_val = qdevice_net_msg_received_starttls(instance, &msg);
  673. break;
  674. case MSG_TYPE_SERVER_ERROR:
  675. msg_processed = 1;
  676. ret_val = qdevice_net_msg_received_server_error(instance, &msg);
  677. break;
  678. case MSG_TYPE_INIT_REPLY:
  679. msg_processed = 1;
  680. ret_val = qdevice_net_msg_received_init_reply(instance, &msg);
  681. break;
  682. case MSG_TYPE_SET_OPTION:
  683. msg_processed = 1;
  684. ret_val = qdevice_net_msg_received_set_option(instance, &msg);
  685. break;
  686. case MSG_TYPE_SET_OPTION_REPLY:
  687. msg_processed = 1;
  688. ret_val = qdevice_net_msg_received_set_option_reply(instance, &msg);
  689. break;
  690. case MSG_TYPE_ECHO_REQUEST:
  691. msg_processed = 1;
  692. ret_val = qdevice_net_msg_received_echo_request(instance, &msg);
  693. break;
  694. case MSG_TYPE_ECHO_REPLY:
  695. msg_processed = 1;
  696. ret_val = qdevice_net_msg_received_echo_reply(instance, &msg);
  697. break;
  698. case MSG_TYPE_NODE_LIST:
  699. msg_processed = 1;
  700. ret_val = qdevice_net_msg_received_node_list(instance, &msg);
  701. break;
  702. case MSG_TYPE_NODE_LIST_REPLY:
  703. msg_processed = 1;
  704. ret_val = qdevice_net_msg_received_node_list_reply(instance, &msg);
  705. break;
  706. case MSG_TYPE_ASK_FOR_VOTE:
  707. msg_processed = 1;
  708. ret_val = qdevice_net_msg_received_ask_for_vote(instance, &msg);
  709. break;
  710. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  711. msg_processed = 1;
  712. ret_val = qdevice_net_msg_received_ask_for_vote_reply(instance, &msg);
  713. break;
  714. case MSG_TYPE_VOTE_INFO:
  715. msg_processed = 1;
  716. ret_val = qdevice_net_msg_received_vote_info(instance, &msg);
  717. break;
  718. case MSG_TYPE_VOTE_INFO_REPLY:
  719. msg_processed = 1;
  720. ret_val = qdevice_net_msg_received_vote_info_reply(instance, &msg);
  721. break;
  722. /*
  723. * Default is not defined intentionally. Compiler shows warning when msg type is added
  724. */
  725. }
  726. if (!msg_processed) {
  727. qdevice_log(LOG_ERR, "Received unsupported message %u. "
  728. "Disconnecting from server", msg.type);
  729. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  730. ret_val = -1;
  731. }
  732. msg_decoded_destroy(&msg);
  733. return (ret_val);
  734. }