corosync-qdevice-net.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Copyright (c) 2015 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 <config.h>
  35. #include <stdio.h>
  36. #include <nss.h>
  37. #include <secerr.h>
  38. #include <sslerr.h>
  39. #include <pk11func.h>
  40. #include <certt.h>
  41. #include <ssl.h>
  42. #include <prio.h>
  43. #include <prnetdb.h>
  44. #include <prerror.h>
  45. #include <prinit.h>
  46. #include <getopt.h>
  47. #include <err.h>
  48. #include <keyhi.h>
  49. #include <poll.h>
  50. /*
  51. * Needed for creating nspr handle from unix fd
  52. */
  53. #include <private/pprio.h>
  54. #include <cmap.h>
  55. #include <votequorum.h>
  56. #include "qnet-config.h"
  57. #include "dynar.h"
  58. #include "nss-sock.h"
  59. #include "tlv.h"
  60. #include "msg.h"
  61. #include "msgio.h"
  62. #include "qdevice-net-log.h"
  63. #include "timer-list.h"
  64. #include "send-buffer-list.h"
  65. #include "qdevice-net-instance.h"
  66. #include "qdevice-net-send.h"
  67. #include "qdevice-net-votequorum.h"
  68. #include "qdevice-net-cast-vote-timer.h"
  69. #include "utils.h"
  70. static SECStatus
  71. qdevice_net_nss_bad_cert_hook(void *arg, PRFileDesc *fd) {
  72. if (PR_GetError() == SEC_ERROR_EXPIRED_CERTIFICATE ||
  73. PR_GetError() == SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE ||
  74. PR_GetError() == SEC_ERROR_CRL_EXPIRED ||
  75. PR_GetError() == SEC_ERROR_KRL_EXPIRED ||
  76. PR_GetError() == SSL_ERROR_EXPIRED_CERT_ALERT) {
  77. qdevice_net_log(LOG_WARNING, "Server certificate is expired.");
  78. return (SECSuccess);
  79. }
  80. qdevice_net_log_nss(LOG_ERR, "Server certificate verification failure.");
  81. return (SECFailure);
  82. }
  83. static SECStatus
  84. qdevice_net_nss_get_client_auth_data(void *arg, PRFileDesc *sock, struct CERTDistNamesStr *caNames,
  85. struct CERTCertificateStr **pRetCert, struct SECKEYPrivateKeyStr **pRetKey)
  86. {
  87. qdevice_net_log(LOG_DEBUG, "Sending client auth data.");
  88. return (NSS_GetClientAuthData(arg, sock, caNames, pRetCert, pRetKey));
  89. }
  90. static void
  91. qdevice_net_log_msg_decode_error(int ret)
  92. {
  93. switch (ret) {
  94. case -1:
  95. qdevice_net_log(LOG_WARNING, "Received message with option with invalid length");
  96. break;
  97. case -2:
  98. qdevice_net_log(LOG_CRIT, "Can't allocate memory");
  99. break;
  100. case -3:
  101. qdevice_net_log(LOG_WARNING, "Received inconsistent msg (tlv len > msg size)");
  102. break;
  103. case -4:
  104. qdevice_net_log(LOG_ERR, "Received message with option with invalid value");
  105. break;
  106. default:
  107. qdevice_net_log(LOG_ERR, "Unknown error occured when decoding message");
  108. break;
  109. }
  110. }
  111. /*
  112. * -1 - Incompatible tls combination
  113. * 0 - Don't use TLS
  114. * 1 - Use TLS
  115. */
  116. static int
  117. qdevice_net_check_tls_compatibility(enum tlv_tls_supported server_tls,
  118. enum tlv_tls_supported client_tls)
  119. {
  120. int res;
  121. res = -1;
  122. switch (server_tls) {
  123. case TLV_TLS_UNSUPPORTED:
  124. switch (client_tls) {
  125. case TLV_TLS_UNSUPPORTED: res = 0; break;
  126. case TLV_TLS_SUPPORTED: res = 0; break;
  127. case TLV_TLS_REQUIRED: res = -1; break;
  128. }
  129. break;
  130. case TLV_TLS_SUPPORTED:
  131. switch (client_tls) {
  132. case TLV_TLS_UNSUPPORTED: res = 0; break;
  133. case TLV_TLS_SUPPORTED: res = 1; break;
  134. case TLV_TLS_REQUIRED: res = 1; break;
  135. }
  136. break;
  137. case TLV_TLS_REQUIRED:
  138. switch (client_tls) {
  139. case TLV_TLS_UNSUPPORTED: res = -1; break;
  140. case TLV_TLS_SUPPORTED: res = 1; break;
  141. case TLV_TLS_REQUIRED: res = 1; break;
  142. }
  143. break;
  144. }
  145. return (res);
  146. }
  147. static int
  148. qdevice_net_msg_received_unexpected_msg(struct qdevice_net_instance *instance,
  149. const struct msg_decoded *msg, const char *msg_str)
  150. {
  151. qdevice_net_log(LOG_ERR, "Received unexpected %s message. Disconnecting from server",
  152. msg_str);
  153. return (-1);
  154. }
  155. static int
  156. qdevice_net_msg_received_preinit(struct qdevice_net_instance *instance,
  157. const struct msg_decoded *msg)
  158. {
  159. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "preinit"));
  160. }
  161. static int
  162. qdevice_net_msg_check_seq_number(struct qdevice_net_instance *instance,
  163. const struct msg_decoded *msg)
  164. {
  165. if (!msg->seq_number_set || msg->seq_number != instance->last_msg_seq_num) {
  166. qdevice_net_log(LOG_ERR, "Received message doesn't contain seq_number or "
  167. "it's not expected one.");
  168. return (-1);
  169. }
  170. return (0);
  171. }
  172. static int
  173. qdevice_net_msg_check_echo_reply_seq_number(struct qdevice_net_instance *instance,
  174. const struct msg_decoded *msg)
  175. {
  176. if (!msg->seq_number_set) {
  177. qdevice_net_log(LOG_ERR, "Received echo reply message doesn't contain seq_number.");
  178. return (-1);
  179. }
  180. if (msg->seq_number != instance->echo_request_expected_msg_seq_num) {
  181. qdevice_net_log(LOG_ERR, "Server doesn't replied in expected time. "
  182. "Closing connection");
  183. return (-1);
  184. }
  185. return (0);
  186. }
  187. static int
  188. qdevice_net_msg_received_preinit_reply(struct qdevice_net_instance *instance,
  189. const struct msg_decoded *msg)
  190. {
  191. int res;
  192. struct send_buffer_list_entry *send_buffer;
  193. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY) {
  194. qdevice_net_log(LOG_ERR, "Received unexpected preinit reply message. "
  195. "Disconnecting from server");
  196. return (-1);
  197. }
  198. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  199. return (-1);
  200. }
  201. /*
  202. * Check TLS support
  203. */
  204. if (!msg->tls_supported_set || !msg->tls_client_cert_required_set) {
  205. qdevice_net_log(LOG_ERR, "Required tls_supported or tls_client_cert_required "
  206. "option is unset");
  207. return (-1);
  208. }
  209. res = qdevice_net_check_tls_compatibility(msg->tls_supported, instance->tls_supported);
  210. if (res == -1) {
  211. qdevice_net_log(LOG_ERR, "Incompatible tls configuration (server %u client %u)",
  212. msg->tls_supported, instance->tls_supported);
  213. return (-1);
  214. } else if (res == 1) {
  215. /*
  216. * Start TLS
  217. */
  218. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  219. if (send_buffer == NULL) {
  220. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for "
  221. "starttls msg");
  222. return (-1);
  223. }
  224. instance->last_msg_seq_num++;
  225. if (msg_create_starttls(&send_buffer->buffer, 1,
  226. instance->last_msg_seq_num) == 0) {
  227. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for starttls msg");
  228. return (-1);
  229. }
  230. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  231. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_STARTTLS_BEING_SENT;
  232. } else if (res == 0) {
  233. if (qdevice_net_send_init(instance) != 0) {
  234. return (-1);
  235. }
  236. }
  237. return (0);
  238. }
  239. static int
  240. qdevice_net_msg_received_init_reply(struct qdevice_net_instance *instance,
  241. const struct msg_decoded *msg)
  242. {
  243. size_t zi;
  244. int res;
  245. struct send_buffer_list_entry *send_buffer;
  246. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_INIT_REPLY) {
  247. qdevice_net_log(LOG_ERR, "Received unexpected init reply message. "
  248. "Disconnecting from server");
  249. return (-1);
  250. }
  251. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  252. return (-1);
  253. }
  254. if (!msg->reply_error_code_set) {
  255. qdevice_net_log(LOG_ERR, "Received init reply message without error code."
  256. "Disconnecting from server");
  257. return (-1);
  258. }
  259. if (msg->reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  260. qdevice_net_log(LOG_ERR, "Received init reply message with error code %"PRIu16". "
  261. "Disconnecting from server", msg->reply_error_code);
  262. return (-1);
  263. }
  264. if (!msg->server_maximum_request_size_set || !msg->server_maximum_reply_size_set) {
  265. qdevice_net_log(LOG_ERR, "Required maximum_request_size or maximum_reply_size "
  266. "option is unset");
  267. return (-1);
  268. }
  269. if (msg->supported_messages == NULL || msg->supported_options == NULL) {
  270. qdevice_net_log(LOG_ERR, "Required supported messages or supported options "
  271. "option is unset");
  272. return (-1);
  273. }
  274. if (msg->supported_decision_algorithms == NULL) {
  275. qdevice_net_log(LOG_ERR, "Required supported decision algorithms option is unset");
  276. return (-1);
  277. }
  278. if (msg->server_maximum_request_size < instance->min_send_size) {
  279. qdevice_net_log(LOG_ERR,
  280. "Server accepts maximum %zu bytes message but this client minimum "
  281. "is %zu bytes.", msg->server_maximum_request_size, instance->min_send_size);
  282. return (-1);
  283. }
  284. if (msg->server_maximum_reply_size > instance->max_receive_size) {
  285. qdevice_net_log(LOG_ERR,
  286. "Server may send message up to %zu bytes message but this client maximum "
  287. "is %zu bytes.", msg->server_maximum_reply_size, instance->max_receive_size);
  288. return (-1);
  289. }
  290. /*
  291. * Change buffer sizes
  292. */
  293. dynar_set_max_size(&instance->receive_buffer, msg->server_maximum_reply_size);
  294. send_buffer_list_set_max_buffer_size(&instance->send_buffer_list,
  295. msg->server_maximum_request_size);
  296. /*
  297. * Check if server supports decision algorithm we need
  298. */
  299. res = 0;
  300. for (zi = 0; zi < msg->no_supported_decision_algorithms && !res; zi++) {
  301. if (msg->supported_decision_algorithms[zi] == instance->decision_algorithm) {
  302. res = 1;
  303. }
  304. }
  305. if (!res) {
  306. qdevice_net_log(LOG_ERR, "Server doesn't support required decision algorithm");
  307. return (-1);
  308. }
  309. /*
  310. * Send set options message
  311. */
  312. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  313. if (send_buffer == NULL) {
  314. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for set option msg");
  315. return (-1);
  316. }
  317. instance->last_msg_seq_num++;
  318. if (msg_create_set_option(&send_buffer->buffer, 1, instance->last_msg_seq_num,
  319. 1, instance->heartbeat_interval) == 0) {
  320. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for set option msg");
  321. return (-1);
  322. }
  323. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  324. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_SET_OPTION_REPLY;
  325. return (0);
  326. }
  327. static int
  328. qdevice_net_msg_received_starttls(struct qdevice_net_instance *instance,
  329. const struct msg_decoded *msg)
  330. {
  331. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "starttls"));
  332. }
  333. static int
  334. qdevice_net_msg_received_server_error(struct qdevice_net_instance *instance,
  335. const struct msg_decoded *msg)
  336. {
  337. if (!msg->reply_error_code_set) {
  338. qdevice_net_log(LOG_ERR, "Received server error without error code set. "
  339. "Disconnecting from server");
  340. } else {
  341. qdevice_net_log(LOG_ERR, "Received server error %"PRIu16". "
  342. "Disconnecting from server", msg->reply_error_code);
  343. }
  344. return (-1);
  345. }
  346. static int
  347. qdevice_net_msg_received_set_option(struct qdevice_net_instance *instance,
  348. const struct msg_decoded *msg)
  349. {
  350. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "set option"));
  351. }
  352. static int
  353. qdevice_net_timer_send_heartbeat(void *data1, void *data2)
  354. {
  355. struct qdevice_net_instance *instance;
  356. instance = (struct qdevice_net_instance *)data1;
  357. if (qdevice_net_send_echo_request(instance) == -1) {
  358. instance->schedule_disconnect = 1;
  359. return (0);
  360. }
  361. /*
  362. * Schedule this function callback again
  363. */
  364. return (-1);
  365. }
  366. static int
  367. qdevice_net_register_votequorum_callbacks(struct qdevice_net_instance *instance)
  368. {
  369. cs_error_t res;
  370. if ((res = votequorum_trackstart(instance->votequorum_handle, 0,
  371. CS_TRACK_CHANGES)) != CS_OK) {
  372. qdevice_net_log(LOG_ERR, "Can't start tracking votequorum changes. Error %s",
  373. cs_strerror(res));
  374. return (-1);
  375. }
  376. return (0);
  377. }
  378. static int
  379. qdevice_net_msg_received_set_option_reply(struct qdevice_net_instance *instance,
  380. const struct msg_decoded *msg)
  381. {
  382. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_SET_OPTION_REPLY) {
  383. qdevice_net_log(LOG_ERR, "Received unexpected set option reply message. "
  384. "Disconnecting from server");
  385. return (-1);
  386. }
  387. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  388. return (-1);
  389. }
  390. if (!msg->decision_algorithm_set || !msg->heartbeat_interval_set) {
  391. qdevice_net_log(LOG_ERR, "Received set option reply message without "
  392. "required options. Disconnecting from server");
  393. }
  394. if (msg->decision_algorithm != instance->decision_algorithm ||
  395. msg->heartbeat_interval != instance->heartbeat_interval) {
  396. qdevice_net_log(LOG_ERR, "Server doesn't accept sent decision algorithm or "
  397. "heartbeat interval.");
  398. return (-1);
  399. }
  400. /*
  401. * Server accepted heartbeat interval -> schedule regular sending of echo request
  402. */
  403. if (instance->heartbeat_interval > 0) {
  404. instance->echo_request_timer = timer_list_add(&instance->main_timer_list,
  405. instance->heartbeat_interval, qdevice_net_timer_send_heartbeat,
  406. (void *)instance, NULL);
  407. if (instance->echo_request_timer == NULL) {
  408. qdevice_net_log(LOG_ERR, "Can't schedule regular sending of heartbeat.");
  409. return (-1);
  410. }
  411. }
  412. /*
  413. * Now we can finally really send node list and initialize qdevice
  414. */
  415. if (qdevice_net_send_config_node_list(instance, 1) != 0) {
  416. return (-1);
  417. }
  418. if (qdevice_net_register_votequorum_callbacks(instance) != 0) {
  419. return (-1);
  420. }
  421. if (qdevice_net_cast_vote_timer_update(instance, TLV_VOTE_WAIT_FOR_REPLY) != 0) {
  422. errx(1, "qdevice_net_msg_received_set_option_reply fatal error. Can't update "
  423. "cast vote timer vote");
  424. }
  425. instance->state = QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS;
  426. return (0);
  427. }
  428. static int
  429. qdevice_net_msg_received_echo_request(struct qdevice_net_instance *instance,
  430. const struct msg_decoded *msg)
  431. {
  432. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "echo request"));
  433. }
  434. static int
  435. qdevice_net_msg_received_echo_reply(struct qdevice_net_instance *instance,
  436. const struct msg_decoded *msg)
  437. {
  438. if (qdevice_net_msg_check_echo_reply_seq_number(instance, msg) != 0) {
  439. return (-1);
  440. }
  441. instance->echo_reply_received_msg_seq_num = msg->seq_number;
  442. return (0);
  443. }
  444. static int
  445. qdevice_net_msg_received_node_list(struct qdevice_net_instance *instance,
  446. const struct msg_decoded *msg)
  447. {
  448. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "node list"));
  449. }
  450. static int
  451. qdevice_net_msg_received_node_list_reply(struct qdevice_net_instance *instance,
  452. const struct msg_decoded *msg)
  453. {
  454. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  455. qdevice_net_log(LOG_ERR, "Received unexpected node list reply message. "
  456. "Disconnecting from server");
  457. return (-1);
  458. }
  459. if (!msg->seq_number_set) {
  460. qdevice_net_log(LOG_ERR, "Received node list reply message without "
  461. "required options. Disconnecting from server");
  462. return (-1);
  463. }
  464. /*
  465. * TODO API
  466. */
  467. qdevice_net_log(LOG_INFO, "Received node list reply seq=%"PRIu32", vote_set=%u, vote=%u",
  468. msg->seq_number, msg->vote_set, (msg->vote_set ? msg->vote : 0));
  469. if (msg->vote_set && qdevice_net_cast_vote_timer_update(instance, msg->vote) != 0) {
  470. return (-1);
  471. }
  472. return (0);
  473. }
  474. static int
  475. qdevice_net_msg_received_ask_for_vote(struct qdevice_net_instance *instance,
  476. const struct msg_decoded *msg)
  477. {
  478. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "ask for vote"));
  479. }
  480. static int
  481. qdevice_net_msg_received_ask_for_vote_reply(struct qdevice_net_instance *instance,
  482. const struct msg_decoded *msg)
  483. {
  484. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  485. qdevice_net_log(LOG_ERR, "Received unexpected ask for vote reply message. "
  486. "Disconnecting from server");
  487. return (-1);
  488. }
  489. if (!msg->vote_set || !msg->seq_number_set) {
  490. qdevice_net_log(LOG_ERR, "Received node list reply message without "
  491. "required options. Disconnecting from server");
  492. }
  493. /*
  494. * TODO API
  495. */
  496. qdevice_net_log(LOG_INFO, "Received ask for vote reply seq=%"PRIu32", vote=%u",
  497. msg->seq_number, msg->vote);
  498. if (qdevice_net_cast_vote_timer_update(instance, msg->vote) != 0) {
  499. return (-1);
  500. }
  501. return (0);
  502. }
  503. static int
  504. qdevice_net_msg_received_vote_info(struct qdevice_net_instance *instance,
  505. const struct msg_decoded *msg)
  506. {
  507. struct send_buffer_list_entry *send_buffer;
  508. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  509. qdevice_net_log(LOG_ERR, "Received unexpected vote info message. "
  510. "Disconnecting from server");
  511. return (-1);
  512. }
  513. if (!msg->vote_set || !msg->seq_number_set) {
  514. qdevice_net_log(LOG_ERR, "Received node list reply message without "
  515. "required options. Disconnecting from server");
  516. }
  517. /*
  518. * TODO API
  519. */
  520. qdevice_net_log(LOG_INFO, "Received vote info seq=%"PRIu32", vote=%u",
  521. msg->seq_number, msg->vote);
  522. if (qdevice_net_cast_vote_timer_update(instance, msg->vote) != 0) {
  523. return (-1);
  524. }
  525. /*
  526. * Create reply message
  527. */
  528. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  529. if (send_buffer == NULL) {
  530. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for "
  531. "vote info reply msg");
  532. return (-1);
  533. }
  534. if (msg_create_vote_info_reply(&send_buffer->buffer, msg->seq_number) == 0) {
  535. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for "
  536. "vote info reply list msg");
  537. return (-1);
  538. }
  539. return (0);
  540. }
  541. static int
  542. qdevice_net_msg_received_vote_info_reply(struct qdevice_net_instance *instance,
  543. const struct msg_decoded *msg)
  544. {
  545. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "vote info reply"));
  546. }
  547. static int
  548. qdevice_net_msg_received(struct qdevice_net_instance *instance)
  549. {
  550. struct msg_decoded msg;
  551. int res;
  552. int ret_val;
  553. msg_decoded_init(&msg);
  554. res = msg_decode(&instance->receive_buffer, &msg);
  555. if (res != 0) {
  556. /*
  557. * Error occurred. Disconnect.
  558. */
  559. qdevice_net_log_msg_decode_error(res);
  560. qdevice_net_log(LOG_ERR, "Disconnecting from server");
  561. return (-1);
  562. }
  563. ret_val = 0;
  564. switch (msg.type) {
  565. case MSG_TYPE_PREINIT:
  566. ret_val = qdevice_net_msg_received_preinit(instance, &msg);
  567. break;
  568. case MSG_TYPE_PREINIT_REPLY:
  569. ret_val = qdevice_net_msg_received_preinit_reply(instance, &msg);
  570. break;
  571. case MSG_TYPE_STARTTLS:
  572. ret_val = qdevice_net_msg_received_starttls(instance, &msg);
  573. break;
  574. case MSG_TYPE_SERVER_ERROR:
  575. ret_val = qdevice_net_msg_received_server_error(instance, &msg);
  576. break;
  577. case MSG_TYPE_INIT_REPLY:
  578. ret_val = qdevice_net_msg_received_init_reply(instance, &msg);
  579. break;
  580. case MSG_TYPE_SET_OPTION:
  581. ret_val = qdevice_net_msg_received_set_option(instance, &msg);
  582. break;
  583. case MSG_TYPE_SET_OPTION_REPLY:
  584. ret_val = qdevice_net_msg_received_set_option_reply(instance, &msg);
  585. break;
  586. case MSG_TYPE_ECHO_REQUEST:
  587. ret_val = qdevice_net_msg_received_echo_request(instance, &msg);
  588. break;
  589. case MSG_TYPE_ECHO_REPLY:
  590. ret_val = qdevice_net_msg_received_echo_reply(instance, &msg);
  591. break;
  592. case MSG_TYPE_NODE_LIST:
  593. ret_val = qdevice_net_msg_received_node_list(instance, &msg);
  594. break;
  595. case MSG_TYPE_NODE_LIST_REPLY:
  596. ret_val = qdevice_net_msg_received_node_list_reply(instance, &msg);
  597. break;
  598. case MSG_TYPE_ASK_FOR_VOTE:
  599. ret_val = qdevice_net_msg_received_ask_for_vote(instance, &msg);
  600. break;
  601. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  602. ret_val = qdevice_net_msg_received_ask_for_vote_reply(instance, &msg);
  603. break;
  604. case MSG_TYPE_VOTE_INFO:
  605. ret_val = qdevice_net_msg_received_vote_info(instance, &msg);
  606. break;
  607. case MSG_TYPE_VOTE_INFO_REPLY:
  608. ret_val = qdevice_net_msg_received_vote_info_reply(instance, &msg);
  609. break;
  610. default:
  611. qdevice_net_log(LOG_ERR, "Received unsupported message %u. "
  612. "Disconnecting from server", msg.type);
  613. ret_val = -1;
  614. break;
  615. }
  616. msg_decoded_destroy(&msg);
  617. return (ret_val);
  618. }
  619. /*
  620. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  621. */
  622. static int
  623. qdevice_net_socket_read(struct qdevice_net_instance *instance)
  624. {
  625. int res;
  626. int ret_val;
  627. int orig_skipping_msg;
  628. orig_skipping_msg = instance->skipping_msg;
  629. res = msgio_read(instance->socket, &instance->receive_buffer,
  630. &instance->msg_already_received_bytes, &instance->skipping_msg);
  631. if (!orig_skipping_msg && instance->skipping_msg) {
  632. qdevice_net_log(LOG_DEBUG, "msgio_read set skipping_msg");
  633. }
  634. ret_val = 0;
  635. switch (res) {
  636. case 0:
  637. /*
  638. * Partial read
  639. */
  640. break;
  641. case -1:
  642. qdevice_net_log(LOG_DEBUG, "Server closed connection");
  643. ret_val = -1;
  644. break;
  645. case -2:
  646. qdevice_net_log_nss(LOG_ERR, "Unhandled error when reading from server. "
  647. "Disconnecting from server");
  648. ret_val = -1;
  649. break;
  650. case -3:
  651. qdevice_net_log(LOG_ERR, "Can't store message header from server. "
  652. "Disconnecting from server");
  653. ret_val = -1;
  654. break;
  655. case -4:
  656. qdevice_net_log(LOG_ERR, "Can't store message from server. "
  657. "Disconnecting from server");
  658. ret_val = -1;
  659. break;
  660. case -5:
  661. qdevice_net_log(LOG_WARNING, "Server sent unsupported msg type %u. "
  662. "Disconnecting from server", msg_get_type(&instance->receive_buffer));
  663. ret_val = -1;
  664. break;
  665. case -6:
  666. qdevice_net_log(LOG_WARNING,
  667. "Server wants to send too long message %u bytes. Disconnecting from server",
  668. msg_get_len(&instance->receive_buffer));
  669. ret_val = -1;
  670. break;
  671. case 1:
  672. /*
  673. * Full message received / skipped
  674. */
  675. if (!instance->skipping_msg) {
  676. if (qdevice_net_msg_received(instance) == -1) {
  677. ret_val = -1;
  678. }
  679. } else {
  680. errx(1, "net_socket_read in skipping msg state");
  681. }
  682. instance->skipping_msg = 0;
  683. instance->msg_already_received_bytes = 0;
  684. dynar_clean(&instance->receive_buffer);
  685. break;
  686. default:
  687. errx(1, "qdevice_net_socket_read unhandled error %d", res);
  688. break;
  689. }
  690. return (ret_val);
  691. }
  692. static int
  693. qdevice_net_socket_write_finished(struct qdevice_net_instance *instance)
  694. {
  695. PRFileDesc *new_pr_fd;
  696. if (instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_STARTTLS_BEING_SENT) {
  697. /*
  698. * StartTLS sent to server. Begin with TLS handshake
  699. */
  700. if ((new_pr_fd = nss_sock_start_ssl_as_client(instance->socket, QNETD_NSS_SERVER_CN,
  701. qdevice_net_nss_bad_cert_hook,
  702. qdevice_net_nss_get_client_auth_data,
  703. (void *)QDEVICE_NET_NSS_CLIENT_CERT_NICKNAME, 0, NULL)) == NULL) {
  704. qdevice_net_log_nss(LOG_ERR, "Can't start TLS");
  705. return (-1);
  706. }
  707. /*
  708. * And send init msg
  709. */
  710. if (qdevice_net_send_init(instance) != 0) {
  711. return (-1);
  712. }
  713. instance->socket = new_pr_fd;
  714. }
  715. return (0);
  716. }
  717. static int
  718. qdevice_net_socket_write(struct qdevice_net_instance *instance)
  719. {
  720. int res;
  721. struct send_buffer_list_entry *send_buffer;
  722. enum msg_type sent_msg_type;
  723. send_buffer = send_buffer_list_get_active(&instance->send_buffer_list);
  724. if (send_buffer == NULL) {
  725. qdevice_net_log(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  726. return (-1);
  727. }
  728. res = msgio_write(instance->socket, &send_buffer->buffer,
  729. &send_buffer->msg_already_sent_bytes);
  730. if (res == 1) {
  731. sent_msg_type = msg_get_type(&send_buffer->buffer);
  732. send_buffer_list_delete(&instance->send_buffer_list, send_buffer);
  733. if (sent_msg_type != MSG_TYPE_ECHO_REQUEST) {
  734. if (qdevice_net_socket_write_finished(instance) == -1) {
  735. return (-1);
  736. }
  737. }
  738. }
  739. if (res == -1) {
  740. qdevice_net_log_nss(LOG_CRIT, "PR_Send returned 0");
  741. return (-1);
  742. }
  743. if (res == -2) {
  744. qdevice_net_log_nss(LOG_ERR, "Unhandled error when sending message to server");
  745. return (-1);
  746. }
  747. return (0);
  748. }
  749. #define QDEVICE_NET_POLL_NO_FDS 2
  750. #define QDEVICE_NET_POLL_SOCKET 0
  751. #define QDEVICE_NET_POLL_VOTEQUORUM 1
  752. static int
  753. qdevice_net_poll(struct qdevice_net_instance *instance)
  754. {
  755. PRPollDesc pfds[QDEVICE_NET_POLL_NO_FDS];
  756. PRInt32 poll_res;
  757. int i;
  758. pfds[QDEVICE_NET_POLL_SOCKET].fd = instance->socket;
  759. pfds[QDEVICE_NET_POLL_SOCKET].in_flags = PR_POLL_READ;
  760. if (!send_buffer_list_empty(&instance->send_buffer_list)) {
  761. pfds[QDEVICE_NET_POLL_SOCKET].in_flags |= PR_POLL_WRITE;
  762. }
  763. pfds[QDEVICE_NET_POLL_VOTEQUORUM].fd = instance->votequorum_poll_fd;
  764. pfds[QDEVICE_NET_POLL_VOTEQUORUM].in_flags = PR_POLL_READ;
  765. instance->schedule_disconnect = 0;
  766. if ((poll_res = PR_Poll(pfds, QDEVICE_NET_POLL_NO_FDS,
  767. timer_list_time_to_expire(&instance->main_timer_list))) > 0) {
  768. for (i = 0; i < QDEVICE_NET_POLL_NO_FDS; i++) {
  769. if (pfds[i].out_flags & PR_POLL_READ) {
  770. switch (i) {
  771. case QDEVICE_NET_POLL_SOCKET:
  772. if (qdevice_net_socket_read(instance) == -1) {
  773. instance->schedule_disconnect = 1;
  774. }
  775. break;
  776. case QDEVICE_NET_POLL_VOTEQUORUM:
  777. if (votequorum_dispatch(instance->votequorum_handle,
  778. CS_DISPATCH_ALL) != CS_OK) {
  779. errx(1, "Can't dispatch votequorum messages");
  780. }
  781. break;
  782. default:
  783. errx(1, "Unhandled read poll descriptor %u", i);
  784. break;
  785. }
  786. }
  787. if (!instance->schedule_disconnect && pfds[i].out_flags & PR_POLL_WRITE) {
  788. switch (i) {
  789. case QDEVICE_NET_POLL_SOCKET:
  790. if (qdevice_net_socket_write(instance) == -1) {
  791. instance->schedule_disconnect = 1;
  792. }
  793. break;
  794. default:
  795. errx(1, "Unhandled write poll descriptor %u", i);
  796. break;
  797. }
  798. }
  799. if (!instance->schedule_disconnect &&
  800. pfds[i].out_flags &
  801. (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) {
  802. switch (i) {
  803. case QDEVICE_NET_POLL_SOCKET:
  804. qdevice_net_log(LOG_CRIT, "POLL_ERR (%u) on main socket",
  805. pfds[i].out_flags);
  806. return (-1);
  807. break;
  808. default:
  809. errx(1, "Unhandled poll err on descriptor %u", i);
  810. break;
  811. }
  812. }
  813. }
  814. }
  815. if (!instance->schedule_disconnect) {
  816. timer_list_expire(&instance->main_timer_list);
  817. }
  818. if (instance->schedule_disconnect) {
  819. /*
  820. * Schedule disconnect can be set by this function, by some timer_list callback
  821. * or cmap/votequorum callbacks
  822. */
  823. return (-1);
  824. }
  825. return (0);
  826. }
  827. static void
  828. qdevice_net_instance_init_from_cmap(struct qdevice_net_instance *instance,
  829. cmap_handle_t cmap_handle)
  830. {
  831. uint32_t node_id;
  832. enum tlv_tls_supported tls_supported;
  833. int i;
  834. char *str;
  835. enum tlv_decision_algorithm_type decision_algorithm;
  836. uint32_t heartbeat_interval;
  837. uint32_t sync_heartbeat_interval;
  838. uint32_t cast_vote_timer_interval;
  839. char *host_addr;
  840. int host_port;
  841. char *ep;
  842. char *cluster_name;
  843. /*
  844. * Check if provider is net
  845. */
  846. if (cmap_get_string(cmap_handle, "quorum.device.model", &str) != CS_OK) {
  847. errx(1, "Can't read quorum.device.model cmap key.");
  848. }
  849. if (strcmp(str, "net") != 0) {
  850. free(str);
  851. errx(1, "Configured device model is not net. "
  852. "This qdevice provider is only for net.");
  853. }
  854. free(str);
  855. /*
  856. * Get nodeid
  857. */
  858. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &node_id) != CS_OK) {
  859. errx(1, "Unable to retrive this node nodeid.");
  860. }
  861. /*
  862. * Check tls
  863. */
  864. if (cmap_get_string(cmap_handle, "quorum.device.net.tls", &str) == CS_OK) {
  865. if ((i = utils_parse_bool_str(str)) == -1) {
  866. free(str);
  867. errx(1, "quorum.device.net.tls value is not valid.");
  868. }
  869. if (i == 1) {
  870. tls_supported = TLV_TLS_SUPPORTED;
  871. } else {
  872. tls_supported = TLV_TLS_UNSUPPORTED;
  873. }
  874. free(str);
  875. }
  876. /*
  877. * Host
  878. */
  879. if (cmap_get_string(cmap_handle, "quorum.device.net.host", &str) != CS_OK) {
  880. errx(1, "Qdevice net daemon address is not defined (quorum.device.net.host)");
  881. }
  882. host_addr = str;
  883. if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
  884. host_port = strtol(str, &ep, 10);
  885. if (host_port <= 0 || host_port > ((uint16_t)~0) || *ep != '\0') {
  886. errx(1, "quorum.device.net.port must be in range 0-65535");
  887. }
  888. free(str);
  889. } else {
  890. host_port = QNETD_DEFAULT_HOST_PORT;
  891. }
  892. /*
  893. * Cluster name
  894. */
  895. if (cmap_get_string(cmap_handle, "totem.cluster_name", &str) != CS_OK) {
  896. errx(1, "Cluster name (totem.cluster_name) has to be defined.");
  897. }
  898. cluster_name = str;
  899. /*
  900. * Configure timeouts
  901. */
  902. if (cmap_get_uint32(cmap_handle, "quorum.device.timeout", &heartbeat_interval) != CS_OK) {
  903. heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  904. }
  905. cast_vote_timer_interval = heartbeat_interval * 0.5;
  906. heartbeat_interval = heartbeat_interval * 0.8;
  907. if (cmap_get_uint32(cmap_handle, "quorum.device.sync_timeout",
  908. &sync_heartbeat_interval) != CS_OK) {
  909. sync_heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_SYNC_TIMEOUT;
  910. }
  911. sync_heartbeat_interval = sync_heartbeat_interval * 0.8;
  912. /*
  913. * Choose decision algorithm
  914. */
  915. if (cmap_get_string(cmap_handle, "quorum.device.net.algorithm", &str) != CS_OK) {
  916. decision_algorithm = QDEVICE_NET_DEFAULT_ALGORITHM;
  917. } else {
  918. if (strcmp(str, "test") == 0) {
  919. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_TEST;
  920. } else if (strcmp(str, "ffsplit") == 0) {
  921. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_FFSPLIT;
  922. } else if (strcmp(str, "2nodelms") == 0) {
  923. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_2NODELMS;
  924. } else if (strcmp(str, "lms") == 0) {
  925. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_LMS;
  926. } else {
  927. errx(1, "Unknown decision algorithm %s", str);
  928. }
  929. free(str);
  930. }
  931. /*
  932. * Really initialize instance
  933. */
  934. if (qdevice_net_instance_init(instance,
  935. QDEVICE_NET_INITIAL_MSG_RECEIVE_SIZE, QDEVICE_NET_INITIAL_MSG_SEND_SIZE,
  936. QDEVICE_NET_MIN_MSG_SEND_SIZE, QDEVICE_NET_MAX_SEND_BUFFERS, QDEVICE_NET_MAX_MSG_RECEIVE_SIZE,
  937. tls_supported, node_id, decision_algorithm,
  938. heartbeat_interval, sync_heartbeat_interval, cast_vote_timer_interval,
  939. host_addr, host_port, cluster_name) == -1) {
  940. errx(1, "Can't initialize qdevice-net");
  941. }
  942. instance->cmap_handle = cmap_handle;
  943. }
  944. int
  945. main(void)
  946. {
  947. struct qdevice_net_instance instance;
  948. cmap_handle_t cmap_handle;
  949. struct send_buffer_list_entry *send_buffer;
  950. /*
  951. * Init
  952. */
  953. qdevice_net_cmap_init(&cmap_handle);
  954. qdevice_net_instance_init_from_cmap(&instance, cmap_handle);
  955. qdevice_net_log_init(QDEVICE_NET_LOG_TARGET_STDERR);
  956. qdevice_net_log_set_debug(1);
  957. if (nss_sock_init_nss((instance.tls_supported != TLV_TLS_UNSUPPORTED ?
  958. (char *)QDEVICE_NET_NSS_DB_DIR : NULL)) != 0) {
  959. nss_sock_err(1);
  960. }
  961. /*
  962. * Try to connect to qnetd host
  963. */
  964. instance.socket = nss_sock_create_client_socket(instance.host_addr, instance.host_port,
  965. PR_AF_UNSPEC, 100);
  966. if (instance.socket == NULL) {
  967. nss_sock_err(1);
  968. }
  969. if (nss_sock_set_nonblocking(instance.socket) != 0) {
  970. nss_sock_err(1);
  971. }
  972. qdevice_net_votequorum_init(&instance);
  973. /*
  974. * Create and schedule send of preinit message to qnetd
  975. */
  976. send_buffer = send_buffer_list_get_new(&instance.send_buffer_list);
  977. if (send_buffer == NULL) {
  978. errx(1, "Can't allocate send buffer list");
  979. }
  980. instance.last_msg_seq_num = 1;
  981. if (msg_create_preinit(&send_buffer->buffer, instance.cluster_name, 1,
  982. instance.last_msg_seq_num) == 0) {
  983. errx(1, "Can't allocate buffer");
  984. }
  985. send_buffer_list_put(&instance.send_buffer_list, send_buffer);
  986. instance.state = QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY;
  987. /*
  988. * Main loop
  989. */
  990. while (qdevice_net_poll(&instance) == 0) {
  991. }
  992. /*
  993. * Cleanup
  994. */
  995. if (PR_Close(instance.socket) != PR_SUCCESS) {
  996. qdevice_net_log_nss(LOG_WARNING, "Unable to close connection");
  997. }
  998. /*
  999. * Close cmap and votequorum connections
  1000. */
  1001. if (votequorum_qdevice_unregister(instance.votequorum_handle,
  1002. QDEVICE_NET_VOTEQUORUM_DEVICE_NAME) != CS_OK) {
  1003. qdevice_net_log_nss(LOG_WARNING, "Unable to unregister votequorum device");
  1004. }
  1005. votequorum_finalize(instance.votequorum_handle);
  1006. cmap_finalize(instance.cmap_handle);
  1007. qdevice_net_instance_destroy(&instance);
  1008. SSL_ClearSessionCache();
  1009. if (NSS_Shutdown() != SECSuccess) {
  1010. nss_sock_err(1);
  1011. }
  1012. PR_Cleanup();
  1013. qdevice_net_log_close();
  1014. return (0);
  1015. }