qdevice-net-msg-received.c 26 KB

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