qdevice-net-msg-received.c 28 KB

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