qdevice-net-msg-received.c 29 KB

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