qnetd-client-msg-received.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 <sys/types.h>
  35. #include "qnetd-algorithm.h"
  36. #include "qnetd-instance.h"
  37. #include "qnetd-log.h"
  38. #include "qnetd-log-debug.h"
  39. #include "qnetd-client-send.h"
  40. #include "msg.h"
  41. #include "nss-sock.h"
  42. #include "qnetd-client-msg-received.h"
  43. /*
  44. * 0 - Success
  45. * -1 - Disconnect client
  46. * -2 - Error reply sent, but no need to disconnect client
  47. */
  48. static int
  49. qnetd_client_msg_received_check_tls(struct qnetd_instance *instance, struct qnetd_client *client,
  50. const struct msg_decoded *msg)
  51. {
  52. int check_certificate;
  53. int tls_required;
  54. CERTCertificate *peer_cert;
  55. check_certificate = 0;
  56. tls_required = 0;
  57. switch (instance->tls_supported) {
  58. case TLV_TLS_UNSUPPORTED:
  59. tls_required = 0;
  60. check_certificate = 0;
  61. break;
  62. case TLV_TLS_SUPPORTED:
  63. tls_required = 0;
  64. if (client->tls_started && instance->tls_client_cert_required &&
  65. !client->tls_peer_certificate_verified) {
  66. check_certificate = 1;
  67. }
  68. break;
  69. case TLV_TLS_REQUIRED:
  70. tls_required = 1;
  71. if (instance->tls_client_cert_required && !client->tls_peer_certificate_verified) {
  72. check_certificate = 1;
  73. }
  74. break;
  75. default:
  76. qnetd_log(LOG_ERR, "Unhandled instance tls supported %u", instance->tls_supported);
  77. exit(1);
  78. break;
  79. }
  80. if (tls_required && !client->tls_started) {
  81. qnetd_log(LOG_ERR, "TLS is required but doesn't started yet. "
  82. "Sending back error message");
  83. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  84. TLV_REPLY_ERROR_CODE_TLS_REQUIRED) != 0) {
  85. return (-1);
  86. }
  87. return (-2);
  88. }
  89. if (check_certificate) {
  90. peer_cert = SSL_PeerCertificate(client->socket);
  91. if (peer_cert == NULL) {
  92. qnetd_log(LOG_ERR, "Client doesn't sent valid certificate. "
  93. "Disconnecting client");
  94. return (-1);
  95. }
  96. if (CERT_VerifyCertName(peer_cert, client->cluster_name) != SECSuccess) {
  97. qnetd_log(LOG_ERR, "Client doesn't sent certificate with valid CN. "
  98. "Disconnecting client");
  99. CERT_DestroyCertificate(peer_cert);
  100. return (-1);
  101. }
  102. CERT_DestroyCertificate(peer_cert);
  103. client->tls_peer_certificate_verified = 1;
  104. }
  105. return (0);
  106. }
  107. static int
  108. qnetd_client_msg_received_preinit(struct qnetd_instance *instance, struct qnetd_client *client,
  109. const struct msg_decoded *msg)
  110. {
  111. struct send_buffer_list_entry *send_buffer;
  112. if (msg->cluster_name == NULL) {
  113. qnetd_log(LOG_ERR, "Received preinit message without cluster name. "
  114. "Sending error reply.");
  115. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  116. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  117. return (-1);
  118. }
  119. return (0);
  120. }
  121. client->cluster_name = malloc(msg->cluster_name_len + 1);
  122. if (client->cluster_name == NULL) {
  123. qnetd_log(LOG_ERR, "Can't allocate cluster name. Sending error reply.");
  124. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  125. TLV_REPLY_ERROR_CODE_INTERNAL_ERROR) != 0) {
  126. return (-1);
  127. }
  128. return (0);
  129. }
  130. memset(client->cluster_name, 0, msg->cluster_name_len + 1);
  131. memcpy(client->cluster_name, msg->cluster_name, msg->cluster_name_len);
  132. client->cluster_name_len = msg->cluster_name_len;
  133. client->preinit_received = 1;
  134. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  135. if (send_buffer == NULL) {
  136. qnetd_log(LOG_ERR, "Can't alloc preinit reply msg from list. "
  137. "Disconnecting client connection.");
  138. return (-1);
  139. }
  140. if (msg_create_preinit_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  141. instance->tls_supported, instance->tls_client_cert_required) == 0) {
  142. qnetd_log(LOG_ERR, "Can't alloc preinit reply msg. "
  143. "Disconnecting client connection.");
  144. return (-1);
  145. };
  146. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  147. return (0);
  148. }
  149. static int
  150. qnetd_client_msg_received_unexpected_msg(struct qnetd_client *client,
  151. const struct msg_decoded *msg, const char *msg_str)
  152. {
  153. qnetd_log(LOG_ERR, "Received %s message. Sending back error message", msg_str);
  154. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  155. TLV_REPLY_ERROR_CODE_UNEXPECTED_MESSAGE) != 0) {
  156. return (-1);
  157. }
  158. return (0);
  159. }
  160. static int
  161. qnetd_client_msg_received_preinit_reply(struct qnetd_instance *instance,
  162. struct qnetd_client *client, const struct msg_decoded *msg)
  163. {
  164. return (qnetd_client_msg_received_unexpected_msg(client, msg, "preinit reply"));
  165. }
  166. static int
  167. qnetd_client_msg_received_starttls(struct qnetd_instance *instance, struct qnetd_client *client,
  168. const struct msg_decoded *msg)
  169. {
  170. PRFileDesc *new_pr_fd;
  171. if (!client->preinit_received) {
  172. qnetd_log(LOG_ERR, "Received starttls before preinit message. "
  173. "Sending error reply.");
  174. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  175. TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED) != 0) {
  176. return (-1);
  177. }
  178. return (0);
  179. }
  180. if ((new_pr_fd = nss_sock_start_ssl_as_server(client->socket, instance->server.cert,
  181. instance->server.private_key, instance->tls_client_cert_required, 0, NULL)) == NULL) {
  182. qnetd_log_nss(LOG_ERR, "Can't start TLS. Disconnecting client.");
  183. return (-1);
  184. }
  185. client->tls_started = 1;
  186. client->tls_peer_certificate_verified = 0;
  187. client->socket = new_pr_fd;
  188. return (0);
  189. }
  190. static int
  191. qnetd_client_msg_received_server_error(struct qnetd_instance *instance, struct qnetd_client *client,
  192. const struct msg_decoded *msg)
  193. {
  194. return (qnetd_client_msg_received_unexpected_msg(client, msg, "server error"));
  195. }
  196. static int
  197. qnetd_client_msg_received_init(struct qnetd_instance *instance, struct qnetd_client *client,
  198. const struct msg_decoded *msg)
  199. {
  200. int res;
  201. size_t zi;
  202. enum msg_type *supported_msgs;
  203. size_t no_supported_msgs;
  204. enum tlv_opt_type *supported_opts;
  205. size_t no_supported_opts;
  206. struct send_buffer_list_entry *send_buffer;
  207. enum tlv_reply_error_code reply_error_code;
  208. struct qnetd_cluster *cluster;
  209. supported_msgs = NULL;
  210. supported_opts = NULL;
  211. no_supported_msgs = 0;
  212. no_supported_opts = 0;
  213. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  214. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  215. return (res == -1 ? -1 : 0);
  216. }
  217. if (!client->preinit_received) {
  218. qnetd_log(LOG_ERR, "Received init before preinit message. Sending error reply.");
  219. reply_error_code = TLV_REPLY_ERROR_CODE_PREINIT_REQUIRED;
  220. }
  221. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->node_id_set) {
  222. qnetd_log(LOG_ERR, "Received init message without node id set. "
  223. "Sending error reply.");
  224. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  225. } else {
  226. client->node_id_set = 1;
  227. client->node_id = msg->node_id;
  228. }
  229. if (msg->supported_messages != NULL) {
  230. /*
  231. * Client sent supported messages. For now this is ignored but in the future
  232. * this may be used to ensure backward compatibility.
  233. */
  234. /*
  235. for (i = 0; i < msg->no_supported_messages; i++) {
  236. qnetd_log(LOG_DEBUG, "Client supports %u message",
  237. (int)msg->supported_messages[i]);
  238. }
  239. */
  240. /*
  241. * Sent back supported messages
  242. */
  243. msg_get_supported_messages(&supported_msgs, &no_supported_msgs);
  244. }
  245. if (msg->supported_options != NULL) {
  246. /*
  247. * Client sent supported options. For now this is ignored but in the future
  248. * this may be used to ensure backward compatibility.
  249. */
  250. /*
  251. for (i = 0; i < msg->no_supported_options; i++) {
  252. qnetd_log(LOG_DEBUG, "Client supports %u option",
  253. (int)msg->supported_messages[i]);
  254. }
  255. */
  256. /*
  257. * Send back supported options
  258. */
  259. tlv_get_supported_options(&supported_opts, &no_supported_opts);
  260. }
  261. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR && !msg->decision_algorithm_set) {
  262. qnetd_log(LOG_ERR, "Received init message without decision algorithm. "
  263. "Sending error reply.");
  264. reply_error_code = TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION;
  265. } else {
  266. /*
  267. * Check if decision algorithm requested by client is supported
  268. */
  269. res = 0;
  270. for (zi = 0; zi < QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE && !res; zi++) {
  271. if (qnetd_static_supported_decision_algorithms[zi] ==
  272. msg->decision_algorithm) {
  273. res = 1;
  274. }
  275. }
  276. if (!res) {
  277. qnetd_log(LOG_ERR, "Client requested unsupported decision algorithm %u. "
  278. "Sending error reply.", msg->decision_algorithm);
  279. reply_error_code = TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM;
  280. }
  281. client->decision_algorithm = msg->decision_algorithm;
  282. }
  283. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  284. cluster = qnetd_cluster_list_add_client(&instance->clusters, client);
  285. if (cluster == NULL) {
  286. qnetd_log(LOG_ERR, "Can't add client to cluster list. "
  287. "Sending error reply.");
  288. reply_error_code = TLV_REPLY_ERROR_CODE_INTERNAL_ERROR;
  289. } else {
  290. client->cluster = cluster;
  291. client->cluster_list = &instance->clusters;
  292. }
  293. }
  294. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  295. qnetd_log_debug_new_client_connected(client);
  296. reply_error_code = qnetd_algorithm_client_init(client);
  297. }
  298. if (reply_error_code == TLV_REPLY_ERROR_CODE_NO_ERROR) {
  299. /*
  300. * Correct init received
  301. */
  302. client->init_received = 1;
  303. } else {
  304. qnetd_log(LOG_ERR, "Algorithm returned error code. Sending error reply.");
  305. }
  306. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  307. if (send_buffer == NULL) {
  308. qnetd_log(LOG_ERR, "Can't alloc init reply msg from list. "
  309. "Disconnecting client connection.");
  310. return (-1);
  311. }
  312. if (msg_create_init_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  313. reply_error_code,
  314. supported_msgs, no_supported_msgs, supported_opts, no_supported_opts,
  315. instance->max_client_receive_size, instance->max_client_send_size,
  316. qnetd_static_supported_decision_algorithms,
  317. QNETD_STATIC_SUPPORTED_DECISION_ALGORITHMS_SIZE) == -1) {
  318. qnetd_log(LOG_ERR, "Can't alloc init reply msg. Disconnecting client connection.");
  319. return (-1);
  320. }
  321. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  322. return (0);
  323. }
  324. static int
  325. qnetd_client_msg_received_init_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  326. const struct msg_decoded *msg)
  327. {
  328. return (qnetd_client_msg_received_unexpected_msg(client, msg, "init reply"));
  329. }
  330. static int
  331. qnetd_client_msg_received_set_option_reply(struct qnetd_instance *instance,
  332. struct qnetd_client *client, const struct msg_decoded *msg)
  333. {
  334. return (qnetd_client_msg_received_unexpected_msg(client, msg, "set option reply"));
  335. }
  336. static int
  337. qnetd_client_msg_received_set_option(struct qnetd_instance *instance, struct qnetd_client *client,
  338. const struct msg_decoded *msg)
  339. {
  340. int res;
  341. struct send_buffer_list_entry *send_buffer;
  342. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  343. return (res == -1 ? -1 : 0);
  344. }
  345. if (!client->init_received) {
  346. qnetd_log(LOG_ERR, "Received set option message before init message. "
  347. "Sending error reply.");
  348. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  349. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  350. return (-1);
  351. }
  352. return (0);
  353. }
  354. if (msg->heartbeat_interval_set) {
  355. /*
  356. * Check if heartbeat interval is valid
  357. */
  358. if (msg->heartbeat_interval != 0 &&
  359. (msg->heartbeat_interval < QNETD_HEARTBEAT_INTERVAL_MIN ||
  360. msg->heartbeat_interval > QNETD_HEARTBEAT_INTERVAL_MAX)) {
  361. qnetd_log(LOG_ERR, "Client requested invalid heartbeat interval %u. "
  362. "Sending error reply.", msg->heartbeat_interval);
  363. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  364. TLV_REPLY_ERROR_CODE_INVALID_HEARTBEAT_INTERVAL) != 0) {
  365. return (-1);
  366. }
  367. return (0);
  368. }
  369. client->heartbeat_interval = msg->heartbeat_interval;
  370. }
  371. if (msg->tie_breaker_set) {
  372. memcpy(&client->tie_breaker, &msg->tie_breaker, sizeof(msg->tie_breaker));
  373. }
  374. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  375. if (send_buffer == NULL) {
  376. qnetd_log(LOG_ERR, "Can't alloc set option reply msg from list. "
  377. "Disconnecting client connection.");
  378. return (-1);
  379. }
  380. if (msg_create_set_option_reply(&send_buffer->buffer, msg->seq_number_set, msg->seq_number,
  381. client->decision_algorithm, client->heartbeat_interval,
  382. msg->tie_breaker_set, &msg->tie_breaker) == -1) {
  383. qnetd_log(LOG_ERR, "Can't alloc set option reply msg. "
  384. "Disconnecting client connection.");
  385. return (-1);
  386. }
  387. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  388. return (0);
  389. }
  390. static int
  391. qnetd_client_msg_received_echo_reply(struct qnetd_instance *instance, struct qnetd_client *client,
  392. const struct msg_decoded *msg)
  393. {
  394. return (qnetd_client_msg_received_unexpected_msg(client, msg, "echo reply"));
  395. }
  396. static int
  397. qnetd_client_msg_received_echo_request(struct qnetd_instance *instance, struct qnetd_client *client,
  398. const struct msg_decoded *msg, const struct dynar *msg_orig)
  399. {
  400. int res;
  401. struct send_buffer_list_entry *send_buffer;
  402. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  403. return (res == -1 ? -1 : 0);
  404. }
  405. if (!client->init_received) {
  406. qnetd_log(LOG_ERR, "Received echo request before init message. "
  407. "Sending error reply.");
  408. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  409. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  410. return (-1);
  411. }
  412. return (0);
  413. }
  414. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  415. if (send_buffer == NULL) {
  416. qnetd_log(LOG_ERR, "Can't alloc echo reply msg from list. "
  417. "Disconnecting client connection.");
  418. return (-1);
  419. }
  420. if (msg_create_echo_reply(&send_buffer->buffer, msg_orig) == -1) {
  421. qnetd_log(LOG_ERR, "Can't alloc echo reply msg. Disconnecting client connection.");
  422. return (-1);
  423. }
  424. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  425. return (0);
  426. }
  427. static int
  428. qnetd_client_msg_received_node_list(struct qnetd_instance *instance, struct qnetd_client *client,
  429. const struct msg_decoded *msg)
  430. {
  431. int res;
  432. struct send_buffer_list_entry *send_buffer;
  433. enum tlv_reply_error_code reply_error_code;
  434. enum tlv_vote result_vote;
  435. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  436. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  437. return (res == -1 ? -1 : 0);
  438. }
  439. if (!client->init_received) {
  440. qnetd_log(LOG_ERR, "Received node list message before init message. "
  441. "Sending error reply.");
  442. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  443. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  444. return (-1);
  445. }
  446. return (0);
  447. }
  448. if (!msg->node_list_type_set) {
  449. qnetd_log(LOG_ERR, "Received node list message without node list type set. "
  450. "Sending error reply.");
  451. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  452. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  453. return (-1);
  454. }
  455. return (0);
  456. }
  457. if (!msg->seq_number_set) {
  458. qnetd_log(LOG_ERR, "Received node list message without seq number set. "
  459. "Sending error reply.");
  460. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  461. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  462. return (-1);
  463. }
  464. return (0);
  465. }
  466. result_vote = TLV_VOTE_NO_CHANGE;
  467. switch (msg->node_list_type) {
  468. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  469. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  470. qnetd_log_debug_config_node_list_received(client, msg->seq_number,
  471. msg->config_version_set, msg->config_version, &msg->nodes,
  472. (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG));
  473. reply_error_code = qnetd_algorithm_config_node_list_received(client,
  474. msg->seq_number, msg->config_version_set, msg->config_version,
  475. &msg->nodes,
  476. (msg->node_list_type == TLV_NODE_LIST_TYPE_INITIAL_CONFIG),
  477. &result_vote);
  478. break;
  479. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  480. if (!msg->ring_id_set) {
  481. qnetd_log(LOG_ERR, "Received node list message without ring id number set. "
  482. "Sending error reply.");
  483. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  484. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  485. return (-1);
  486. }
  487. return (0);
  488. }
  489. qnetd_log_debug_membership_node_list_received(client, msg->seq_number, &msg->ring_id,
  490. &msg->nodes);
  491. reply_error_code = qnetd_algorithm_membership_node_list_received(client,
  492. msg->seq_number, &msg->ring_id, &msg->nodes, &result_vote);
  493. break;
  494. case TLV_NODE_LIST_TYPE_QUORUM:
  495. if (!msg->quorate_set) {
  496. qnetd_log(LOG_ERR, "Received quorum list message without quorate set. "
  497. "Sending error reply.");
  498. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  499. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  500. return (-1);
  501. }
  502. return (0);
  503. }
  504. qnetd_log_debug_quorum_node_list_received(client, msg->seq_number,msg->quorate,
  505. &msg->nodes);
  506. reply_error_code = qnetd_algorithm_quorum_node_list_received(client,
  507. msg->seq_number,msg->quorate, &msg->nodes, &result_vote);
  508. break;
  509. default:
  510. qnetd_log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. "
  511. "Unhandled node_list_type");
  512. exit(1);
  513. break;
  514. }
  515. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  516. qnetd_log(LOG_ERR, "Algorithm returned error code. "
  517. "Sending error reply.");
  518. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  519. reply_error_code) != 0) {
  520. return (-1);
  521. }
  522. return (0);
  523. } else {
  524. qnetd_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  525. }
  526. if (msg->node_list_type == TLV_NODE_LIST_TYPE_MEMBERSHIP &&
  527. result_vote == TLV_VOTE_NO_CHANGE) {
  528. qnetd_log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. "
  529. "node_list_type is membership and algorithm result vote is no_change");
  530. exit(1);
  531. }
  532. /*
  533. * Store node list for future use
  534. */
  535. switch (msg->node_list_type) {
  536. case TLV_NODE_LIST_TYPE_INITIAL_CONFIG:
  537. case TLV_NODE_LIST_TYPE_CHANGED_CONFIG:
  538. node_list_free(&client->configuration_node_list);
  539. if (node_list_clone(&client->configuration_node_list, &msg->nodes) == -1) {
  540. qnetd_log(LOG_ERR, "Can't alloc config node list clone. "
  541. "Disconnecting client connection.");
  542. return (-1);
  543. }
  544. break;
  545. case TLV_NODE_LIST_TYPE_MEMBERSHIP:
  546. node_list_free(&client->last_membership_node_list);
  547. if (node_list_clone(&client->last_membership_node_list, &msg->nodes) == -1) {
  548. qnetd_log(LOG_ERR, "Can't alloc membership node list clone. "
  549. "Disconnecting client connection.");
  550. return (-1);
  551. }
  552. memcpy(&client->last_ring_id, &msg->ring_id, sizeof(struct tlv_ring_id));
  553. break;
  554. case TLV_NODE_LIST_TYPE_QUORUM:
  555. node_list_free(&client->last_quorum_node_list);
  556. if (node_list_clone(&client->last_quorum_node_list, &msg->nodes) == -1) {
  557. qnetd_log(LOG_ERR, "Can't alloc quorum node list clone. "
  558. "Disconnecting client connection.");
  559. return (-1);
  560. }
  561. break;
  562. default:
  563. qnetd_log(LOG_ERR, "qnetd_client_msg_received_node_list fatal error. "
  564. "Unhandled node_list_type");
  565. exit(1);
  566. break;
  567. }
  568. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  569. if (send_buffer == NULL) {
  570. qnetd_log(LOG_ERR, "Can't alloc node list reply msg from list. "
  571. "Disconnecting client connection.");
  572. return (-1);
  573. }
  574. if (msg_create_node_list_reply(&send_buffer->buffer, msg->seq_number, msg->node_list_type,
  575. msg->ring_id_set, &msg->ring_id, result_vote) == -1) {
  576. qnetd_log(LOG_ERR, "Can't alloc node list reply msg. "
  577. "Disconnecting client connection.");
  578. return (-1);
  579. }
  580. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  581. return (0);
  582. }
  583. static int
  584. qnetd_client_msg_received_node_list_reply(struct qnetd_instance *instance,
  585. struct qnetd_client *client, const struct msg_decoded *msg)
  586. {
  587. return (qnetd_client_msg_received_unexpected_msg(client, msg, "node list reply"));
  588. }
  589. static int
  590. qnetd_client_msg_received_ask_for_vote(struct qnetd_instance *instance, struct qnetd_client *client,
  591. const struct msg_decoded *msg)
  592. {
  593. int res;
  594. struct send_buffer_list_entry *send_buffer;
  595. enum tlv_reply_error_code reply_error_code;
  596. enum tlv_vote result_vote;
  597. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  598. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  599. return (res == -1 ? -1 : 0);
  600. }
  601. if (!client->init_received) {
  602. qnetd_log(LOG_ERR, "Received ask for vote message before init message. "
  603. "Sending error reply.");
  604. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  605. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  606. return (-1);
  607. }
  608. return (0);
  609. }
  610. if (!msg->seq_number_set) {
  611. qnetd_log(LOG_ERR, "Received ask for vote message without seq number set. "
  612. "Sending error reply.");
  613. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  614. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  615. return (-1);
  616. }
  617. return (0);
  618. }
  619. qnetd_log_debug_ask_for_vote_received(client, msg->seq_number);
  620. reply_error_code = qnetd_algorithm_ask_for_vote_received(client, msg->seq_number,
  621. &result_vote);
  622. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  623. qnetd_log(LOG_ERR, "Algorithm returned error code. "
  624. "Sending error reply.");
  625. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  626. reply_error_code) != 0) {
  627. return (-1);
  628. }
  629. return (0);
  630. } else {
  631. qnetd_log(LOG_DEBUG, "Algorithm result vote is %s", tlv_vote_to_str(result_vote));
  632. }
  633. send_buffer = send_buffer_list_get_new(&client->send_buffer_list);
  634. if (send_buffer == NULL) {
  635. qnetd_log(LOG_ERR, "Can't alloc ask for vote reply msg from list. "
  636. "Disconnecting client connection.");
  637. return (-1);
  638. }
  639. if (msg_create_ask_for_vote_reply(&send_buffer->buffer, msg->seq_number,
  640. result_vote) == -1) {
  641. qnetd_log(LOG_ERR, "Can't alloc ask for vote reply msg. "
  642. "Disconnecting client connection.");
  643. return (-1);
  644. }
  645. send_buffer_list_put(&client->send_buffer_list, send_buffer);
  646. return (0);
  647. }
  648. static int
  649. qnetd_client_msg_received_ask_for_vote_reply(struct qnetd_instance *instance,
  650. struct qnetd_client *client, const struct msg_decoded *msg)
  651. {
  652. return (qnetd_client_msg_received_unexpected_msg(client, msg, "ask for vote reply"));
  653. }
  654. static int
  655. qnetd_client_msg_received_vote_info(struct qnetd_instance *instance, struct qnetd_client *client,
  656. const struct msg_decoded *msg)
  657. {
  658. return (qnetd_client_msg_received_unexpected_msg(client, msg, "vote info"));
  659. }
  660. static int
  661. qnetd_client_msg_received_vote_info_reply(struct qnetd_instance *instance,
  662. struct qnetd_client *client, const struct msg_decoded *msg)
  663. {
  664. int res;
  665. enum tlv_reply_error_code reply_error_code;
  666. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  667. if ((res = qnetd_client_msg_received_check_tls(instance, client, msg)) != 0) {
  668. return (res == -1 ? -1 : 0);
  669. }
  670. if (!client->init_received) {
  671. qnetd_log(LOG_ERR, "Received vote info reply before init message. "
  672. "Sending error reply.");
  673. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  674. TLV_REPLY_ERROR_CODE_INIT_REQUIRED) != 0) {
  675. return (-1);
  676. }
  677. return (0);
  678. }
  679. if (!msg->seq_number_set) {
  680. qnetd_log(LOG_ERR, "Received vote info reply message without seq number set. "
  681. "Sending error reply.");
  682. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  683. TLV_REPLY_ERROR_CODE_DOESNT_CONTAIN_REQUIRED_OPTION) != 0) {
  684. return (-1);
  685. }
  686. return (0);
  687. }
  688. qnetd_log_debug_vote_info_reply_received(client, msg->seq_number);
  689. reply_error_code = qnetd_algorithm_vote_info_reply_received(client, msg->seq_number);
  690. if (reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  691. qnetd_log(LOG_ERR, "Algorithm returned error code. "
  692. "Sending error reply.");
  693. if (qnetd_client_send_err(client, msg->seq_number_set, msg->seq_number,
  694. reply_error_code) != 0) {
  695. return (-1);
  696. }
  697. return (0);
  698. }
  699. return (0);
  700. }
  701. int
  702. qnetd_client_msg_received(struct qnetd_instance *instance, struct qnetd_client *client)
  703. {
  704. struct msg_decoded msg;
  705. int res;
  706. int ret_val;
  707. msg_decoded_init(&msg);
  708. res = msg_decode(&client->receive_buffer, &msg);
  709. if (res != 0) {
  710. /*
  711. * Error occurred. Send server error.
  712. */
  713. qnetd_log_msg_decode_error(res);
  714. qnetd_log(LOG_INFO, "Sending back error message");
  715. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  716. TLV_REPLY_ERROR_CODE_ERROR_DECODING_MSG) != 0) {
  717. return (-1);
  718. }
  719. return (0);
  720. }
  721. ret_val = 0;
  722. switch (msg.type) {
  723. case MSG_TYPE_PREINIT:
  724. ret_val = qnetd_client_msg_received_preinit(instance, client, &msg);
  725. break;
  726. case MSG_TYPE_PREINIT_REPLY:
  727. ret_val = qnetd_client_msg_received_preinit_reply(instance, client, &msg);
  728. break;
  729. case MSG_TYPE_STARTTLS:
  730. ret_val = qnetd_client_msg_received_starttls(instance, client, &msg);
  731. break;
  732. case MSG_TYPE_INIT:
  733. ret_val = qnetd_client_msg_received_init(instance, client, &msg);
  734. break;
  735. case MSG_TYPE_INIT_REPLY:
  736. ret_val = qnetd_client_msg_received_init_reply(instance, client, &msg);
  737. break;
  738. case MSG_TYPE_SERVER_ERROR:
  739. ret_val = qnetd_client_msg_received_server_error(instance, client, &msg);
  740. break;
  741. case MSG_TYPE_SET_OPTION:
  742. ret_val = qnetd_client_msg_received_set_option(instance, client, &msg);
  743. break;
  744. case MSG_TYPE_SET_OPTION_REPLY:
  745. ret_val = qnetd_client_msg_received_set_option_reply(instance, client, &msg);
  746. break;
  747. case MSG_TYPE_ECHO_REQUEST:
  748. ret_val = qnetd_client_msg_received_echo_request(instance, client, &msg,
  749. &client->receive_buffer);
  750. break;
  751. case MSG_TYPE_ECHO_REPLY:
  752. ret_val = qnetd_client_msg_received_echo_reply(instance, client, &msg);
  753. break;
  754. case MSG_TYPE_NODE_LIST:
  755. ret_val = qnetd_client_msg_received_node_list(instance, client, &msg);
  756. break;
  757. case MSG_TYPE_NODE_LIST_REPLY:
  758. ret_val = qnetd_client_msg_received_node_list_reply(instance, client, &msg);
  759. break;
  760. case MSG_TYPE_ASK_FOR_VOTE:
  761. ret_val = qnetd_client_msg_received_ask_for_vote(instance, client, &msg);
  762. break;
  763. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  764. ret_val = qnetd_client_msg_received_ask_for_vote_reply(instance, client, &msg);
  765. break;
  766. case MSG_TYPE_VOTE_INFO:
  767. ret_val = qnetd_client_msg_received_vote_info(instance, client, &msg);
  768. break;
  769. case MSG_TYPE_VOTE_INFO_REPLY:
  770. ret_val = qnetd_client_msg_received_vote_info_reply(instance, client, &msg);
  771. break;
  772. default:
  773. qnetd_log(LOG_ERR, "Unsupported message %u received from client. "
  774. "Sending back error message", msg.type);
  775. if (qnetd_client_send_err(client, msg.seq_number_set, msg.seq_number,
  776. TLV_REPLY_ERROR_CODE_UNSUPPORTED_MESSAGE) != 0) {
  777. ret_val = -1;
  778. }
  779. break;
  780. }
  781. msg_decoded_destroy(&msg);
  782. return (ret_val);
  783. }