qdevice-net-msg-received.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. instance->connected_since_time = time(NULL);
  351. return (0);
  352. }
  353. static int
  354. qdevice_net_msg_received_starttls(struct qdevice_net_instance *instance,
  355. const struct msg_decoded *msg)
  356. {
  357. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "starttls"));
  358. }
  359. static int
  360. qdevice_net_msg_received_server_error(struct qdevice_net_instance *instance,
  361. const struct msg_decoded *msg)
  362. {
  363. if (!msg->reply_error_code_set) {
  364. qdevice_log(LOG_ERR, "Received server error without error code set. "
  365. "Disconnecting from server");
  366. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  367. } else {
  368. qdevice_log(LOG_ERR, "Received server error %"PRIu16". "
  369. "Disconnecting from server", msg->reply_error_code);
  370. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_SERVER_SENT_ERROR;
  371. }
  372. return (-1);
  373. }
  374. static int
  375. qdevice_net_msg_received_set_option(struct qdevice_net_instance *instance,
  376. const struct msg_decoded *msg)
  377. {
  378. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "set option"));
  379. }
  380. static int
  381. qdevice_net_msg_received_set_option_reply(struct qdevice_net_instance *instance,
  382. const struct msg_decoded *msg)
  383. {
  384. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  385. qdevice_log(LOG_ERR, "Received unexpected set option reply message. "
  386. "Disconnecting from server");
  387. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  388. return (-1);
  389. }
  390. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  391. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  392. return (-1);
  393. }
  394. qdevice_net_echo_request_timer_schedule(instance);
  395. return (0);
  396. }
  397. static int
  398. qdevice_net_msg_received_echo_request(struct qdevice_net_instance *instance,
  399. const struct msg_decoded *msg)
  400. {
  401. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "echo request"));
  402. }
  403. static int
  404. qdevice_net_msg_received_echo_reply(struct qdevice_net_instance *instance,
  405. const struct msg_decoded *msg)
  406. {
  407. if (!msg->seq_number_set) {
  408. qdevice_log(LOG_ERR, "Received echo reply message doesn't contain seq_number.");
  409. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  410. return (-1);
  411. }
  412. if (msg->seq_number != instance->echo_request_expected_msg_seq_num) {
  413. qdevice_log(LOG_WARNING, "Received echo reply message seq_number is not expected one.");
  414. }
  415. if (qdevice_net_algorithm_echo_reply_received(instance, msg->seq_number,
  416. msg->seq_number == instance->echo_request_expected_msg_seq_num) != 0) {
  417. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting");
  418. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_ECHO_REPLY_RECEIVED_ERR;
  419. return (-1);
  420. }
  421. instance->echo_reply_received_msg_seq_num = msg->seq_number;
  422. instance->last_echo_reply_received_time = time(NULL);
  423. return (0);
  424. }
  425. static int
  426. qdevice_net_msg_received_node_list(struct qdevice_net_instance *instance,
  427. const struct msg_decoded *msg)
  428. {
  429. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "node list"));
  430. }
  431. static int
  432. qdevice_net_msg_received_node_list_reply(struct qdevice_net_instance *instance,
  433. const struct msg_decoded *msg)
  434. {
  435. const char *str;
  436. enum tlv_vote result_vote;
  437. int res;
  438. int case_processed;
  439. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  440. qdevice_log(LOG_ERR, "Received unexpected node list reply message. "
  441. "Disconnecting from server");
  442. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  443. return (-1);
  444. }
  445. if (!msg->vote_set || !msg->seq_number_set || !msg->node_list_type_set) {
  446. qdevice_log(LOG_ERR, "Received node list reply message without "
  447. "required options. Disconnecting from server");
  448. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  449. return (-1);
  450. }
  451. if (msg->node_list_type == TLV_NODE_LIST_TYPE_MEMBERSHIP && !msg->ring_id_set) {
  452. qdevice_log(LOG_ERR, "Received node list reply message with type membership "
  453. "without ring id set. Disconnecting from server");
  454. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  455. return (-1);
  456. }
  457. str = NULL;
  458. switch (msg->node_list_type) {
  459. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG: str = "initial config"; break;
  460. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG: str = "changed config"; break;
  461. case TLV_NODE_LIST_TYPE_MEMBERSHIP: str ="membership"; break;
  462. case TLV_NODE_LIST_TYPE_QUORUM: str ="quorum"; break;
  463. /*
  464. * Default is not defined intentionally. Compiler shows warning when new node list type
  465. * is added
  466. */
  467. }
  468. if (str == NULL) {
  469. qdevice_log(LOG_CRIT, "qdevice_net_msg_received_node_list_reply fatal error. "
  470. "Unhandled node_list_type (debug output)");
  471. exit(1);
  472. }
  473. qdevice_log(LOG_DEBUG, "Received %s node list reply", str);
  474. qdevice_log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  475. qdevice_log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  476. if (msg->ring_id_set) {
  477. qdevice_log(LOG_DEBUG, " ring id = ("UTILS_PRI_RING_ID")",
  478. msg->ring_id.node_id, msg->ring_id.seq);
  479. }
  480. /*
  481. * Call algorithm
  482. */
  483. result_vote = msg->vote;
  484. case_processed = 0;
  485. switch (msg->node_list_type) {
  486. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  487. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  488. case_processed = 1;
  489. res = qdevice_net_algorithm_config_node_list_reply_received(instance,
  490. msg->seq_number, (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG),
  491. &result_vote);
  492. break;
  493. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  494. case_processed = 1;
  495. res = qdevice_net_algorithm_membership_node_list_reply_received(instance,
  496. msg->seq_number, &msg->ring_id, &result_vote);
  497. break;
  498. case TLV_NODE_LIST_TYPE_QUORUM:
  499. case_processed = 1;
  500. res = qdevice_net_algorithm_quorum_node_list_reply_received(instance,
  501. msg->seq_number, &result_vote);
  502. break;
  503. /*
  504. * Default is not defined intentionally. Compiler shows warning when new node list type
  505. * is added
  506. */
  507. }
  508. if (!case_processed) {
  509. qdevice_log(LOG_CRIT, "qdevice_net_msg_received_node_list_reply fatal error. "
  510. "Unhandled node_list_type (algorithm call)");
  511. exit(1);
  512. }
  513. if (res != 0) {
  514. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  515. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_NODE_LIST_REPLY_ERR;
  516. return (-1);
  517. } else {
  518. qdevice_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(msg->vote));
  519. }
  520. if (result_vote != TLV_VOTE_NO_CHANGE) {
  521. if (msg->node_list_type == TLV_NODE_LIST_TYPE_MEMBERSHIP &&
  522. !tlv_ring_id_eq(&msg->ring_id, &instance->last_sent_ring_id)) {
  523. qdevice_log(LOG_INFO, "Received membership node list reply with "
  524. "old ring id. Not updating timer");
  525. } else {
  526. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  527. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  528. return (-1);
  529. }
  530. }
  531. }
  532. return (0);
  533. }
  534. static int
  535. qdevice_net_msg_received_ask_for_vote(struct qdevice_net_instance *instance,
  536. const struct msg_decoded *msg)
  537. {
  538. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "ask for vote"));
  539. }
  540. static int
  541. qdevice_net_msg_received_ask_for_vote_reply(struct qdevice_net_instance *instance,
  542. const struct msg_decoded *msg)
  543. {
  544. enum tlv_vote result_vote;
  545. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  546. qdevice_log(LOG_ERR, "Received unexpected ask for vote reply message. "
  547. "Disconnecting from server");
  548. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  549. return (-1);
  550. }
  551. if (!msg->vote_set || !msg->seq_number_set) {
  552. qdevice_log(LOG_ERR, "Received node list reply message without "
  553. "required options. Disconnecting from server");
  554. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  555. return (-1);
  556. }
  557. qdevice_log(LOG_DEBUG, "Received ask for vote reply");
  558. qdevice_log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  559. qdevice_log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  560. result_vote = msg->vote;
  561. if (qdevice_net_algorithm_ask_for_vote_reply_received(instance, msg->seq_number,
  562. &result_vote) != 0) {
  563. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  564. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_ASK_FOR_VOTE_REPLY_ERR;
  565. return (-1);
  566. } else {
  567. qdevice_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(msg->vote));
  568. }
  569. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  570. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  571. return (-1);
  572. }
  573. return (0);
  574. }
  575. static int
  576. qdevice_net_msg_received_vote_info(struct qdevice_net_instance *instance,
  577. const struct msg_decoded *msg)
  578. {
  579. struct send_buffer_list_entry *send_buffer;
  580. enum tlv_vote result_vote;
  581. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  582. qdevice_log(LOG_ERR, "Received unexpected vote info message. "
  583. "Disconnecting from server");
  584. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  585. return (-1);
  586. }
  587. if (!msg->vote_set || !msg->seq_number_set) {
  588. qdevice_log(LOG_ERR, "Received node list reply message without "
  589. "required options. Disconnecting from server");
  590. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_REQUIRED_OPTION_MISSING;
  591. return (-1);
  592. }
  593. qdevice_log(LOG_DEBUG, "Received vote info");
  594. qdevice_log(LOG_DEBUG, " seq = "UTILS_PRI_MSG_SEQ, msg->seq_number);
  595. qdevice_log(LOG_DEBUG, " vote = %s", tlv_vote_to_str(msg->vote));
  596. result_vote = msg->vote;
  597. if (qdevice_net_algorithm_vote_info_received(instance, msg->seq_number,
  598. &result_vote) != 0) {
  599. qdevice_log(LOG_DEBUG, "Algorithm returned error. Disconnecting.");
  600. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_ALGO_VOTE_INFO_ERR;
  601. return (-1);
  602. } else {
  603. qdevice_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  604. }
  605. if (qdevice_net_cast_vote_timer_update(instance, result_vote) != 0) {
  606. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_SCHEDULE_VOTING_TIMER;
  607. return (-1);
  608. }
  609. /*
  610. * Create reply message
  611. */
  612. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  613. if (send_buffer == NULL) {
  614. qdevice_log(LOG_ERR, "Can't allocate send list buffer for "
  615. "vote info reply msg");
  616. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  617. return (-1);
  618. }
  619. if (msg_create_vote_info_reply(&send_buffer->buffer, msg->seq_number) == 0) {
  620. qdevice_log(LOG_ERR, "Can't allocate send buffer for "
  621. "vote info reply list msg");
  622. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_CANT_ALLOCATE_MSG_BUFFER;
  623. send_buffer_list_discard_new(&instance->send_buffer_list, send_buffer);
  624. return (-1);
  625. }
  626. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  627. return (0);
  628. }
  629. static int
  630. qdevice_net_msg_received_vote_info_reply(struct qdevice_net_instance *instance,
  631. const struct msg_decoded *msg)
  632. {
  633. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "vote info reply"));
  634. }
  635. int
  636. qdevice_net_msg_received(struct qdevice_net_instance *instance)
  637. {
  638. struct msg_decoded msg;
  639. int res;
  640. int ret_val;
  641. int msg_processed;
  642. msg_decoded_init(&msg);
  643. res = msg_decode(&instance->receive_buffer, &msg);
  644. if (res != 0) {
  645. /*
  646. * Error occurred. Disconnect.
  647. */
  648. qdevice_net_msg_received_log_msg_decode_error(res);
  649. qdevice_log(LOG_ERR, "Disconnecting from server");
  650. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_MSG_DECODE_ERROR;
  651. return (-1);
  652. }
  653. ret_val = 0;
  654. msg_processed = 0;
  655. switch (msg.type) {
  656. case MSG_TYPE_INIT:
  657. msg_processed = 1;
  658. ret_val = qdevice_net_msg_received_init(instance, &msg);
  659. break;
  660. case MSG_TYPE_PREINIT:
  661. msg_processed = 1;
  662. ret_val = qdevice_net_msg_received_preinit(instance, &msg);
  663. break;
  664. case MSG_TYPE_PREINIT_REPLY:
  665. msg_processed = 1;
  666. ret_val = qdevice_net_msg_received_preinit_reply(instance, &msg);
  667. break;
  668. case MSG_TYPE_STARTTLS:
  669. msg_processed = 1;
  670. ret_val = qdevice_net_msg_received_starttls(instance, &msg);
  671. break;
  672. case MSG_TYPE_SERVER_ERROR:
  673. msg_processed = 1;
  674. ret_val = qdevice_net_msg_received_server_error(instance, &msg);
  675. break;
  676. case MSG_TYPE_INIT_REPLY:
  677. msg_processed = 1;
  678. ret_val = qdevice_net_msg_received_init_reply(instance, &msg);
  679. break;
  680. case MSG_TYPE_SET_OPTION:
  681. msg_processed = 1;
  682. ret_val = qdevice_net_msg_received_set_option(instance, &msg);
  683. break;
  684. case MSG_TYPE_SET_OPTION_REPLY:
  685. msg_processed = 1;
  686. ret_val = qdevice_net_msg_received_set_option_reply(instance, &msg);
  687. break;
  688. case MSG_TYPE_ECHO_REQUEST:
  689. msg_processed = 1;
  690. ret_val = qdevice_net_msg_received_echo_request(instance, &msg);
  691. break;
  692. case MSG_TYPE_ECHO_REPLY:
  693. msg_processed = 1;
  694. ret_val = qdevice_net_msg_received_echo_reply(instance, &msg);
  695. break;
  696. case MSG_TYPE_NODE_LIST:
  697. msg_processed = 1;
  698. ret_val = qdevice_net_msg_received_node_list(instance, &msg);
  699. break;
  700. case MSG_TYPE_NODE_LIST_REPLY:
  701. msg_processed = 1;
  702. ret_val = qdevice_net_msg_received_node_list_reply(instance, &msg);
  703. break;
  704. case MSG_TYPE_ASK_FOR_VOTE:
  705. msg_processed = 1;
  706. ret_val = qdevice_net_msg_received_ask_for_vote(instance, &msg);
  707. break;
  708. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  709. msg_processed = 1;
  710. ret_val = qdevice_net_msg_received_ask_for_vote_reply(instance, &msg);
  711. break;
  712. case MSG_TYPE_VOTE_INFO:
  713. msg_processed = 1;
  714. ret_val = qdevice_net_msg_received_vote_info(instance, &msg);
  715. break;
  716. case MSG_TYPE_VOTE_INFO_REPLY:
  717. msg_processed = 1;
  718. ret_val = qdevice_net_msg_received_vote_info_reply(instance, &msg);
  719. break;
  720. /*
  721. * Default is not defined intentionally. Compiler shows warning when msg type is added
  722. */
  723. }
  724. if (!msg_processed) {
  725. qdevice_log(LOG_ERR, "Received unsupported message %u. "
  726. "Disconnecting from server", msg.type);
  727. instance->disconnect_reason = QDEVICE_NET_DISCONNECT_REASON_UNEXPECTED_MSG;
  728. ret_val = -1;
  729. }
  730. msg_decoded_destroy(&msg);
  731. return (ret_val);
  732. }