qdevice-net-msg-received.c 28 KB

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