corosync-qdevice-net.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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, 1, &instance->tie_breaker) == 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->vote_set || !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=%u",
  468. msg->seq_number, msg->vote);
  469. if (msg->vote != TLV_VOTE_NO_CHANGE) {
  470. if (qdevice_net_cast_vote_timer_update(instance, msg->vote) != 0) {
  471. return (-1);
  472. }
  473. }
  474. return (0);
  475. }
  476. static int
  477. qdevice_net_msg_received_ask_for_vote(struct qdevice_net_instance *instance,
  478. const struct msg_decoded *msg)
  479. {
  480. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "ask for vote"));
  481. }
  482. static int
  483. qdevice_net_msg_received_ask_for_vote_reply(struct qdevice_net_instance *instance,
  484. const struct msg_decoded *msg)
  485. {
  486. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  487. qdevice_net_log(LOG_ERR, "Received unexpected ask for vote reply message. "
  488. "Disconnecting from server");
  489. return (-1);
  490. }
  491. if (!msg->vote_set || !msg->seq_number_set) {
  492. qdevice_net_log(LOG_ERR, "Received node list reply message without "
  493. "required options. Disconnecting from server");
  494. }
  495. /*
  496. * TODO API
  497. */
  498. qdevice_net_log(LOG_INFO, "Received ask for vote reply seq=%"PRIu32", vote=%u",
  499. msg->seq_number, msg->vote);
  500. if (qdevice_net_cast_vote_timer_update(instance, msg->vote) != 0) {
  501. return (-1);
  502. }
  503. return (0);
  504. }
  505. static int
  506. qdevice_net_msg_received_vote_info(struct qdevice_net_instance *instance,
  507. const struct msg_decoded *msg)
  508. {
  509. struct send_buffer_list_entry *send_buffer;
  510. if (instance->state != QDEVICE_NET_INSTANCE_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS) {
  511. qdevice_net_log(LOG_ERR, "Received unexpected vote info message. "
  512. "Disconnecting from server");
  513. return (-1);
  514. }
  515. if (!msg->vote_set || !msg->seq_number_set) {
  516. qdevice_net_log(LOG_ERR, "Received node list reply message without "
  517. "required options. Disconnecting from server");
  518. }
  519. /*
  520. * TODO API
  521. */
  522. qdevice_net_log(LOG_INFO, "Received vote info seq=%"PRIu32", vote=%u",
  523. msg->seq_number, msg->vote);
  524. if (qdevice_net_cast_vote_timer_update(instance, msg->vote) != 0) {
  525. return (-1);
  526. }
  527. /*
  528. * Create reply message
  529. */
  530. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  531. if (send_buffer == NULL) {
  532. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for "
  533. "vote info reply msg");
  534. return (-1);
  535. }
  536. if (msg_create_vote_info_reply(&send_buffer->buffer, msg->seq_number) == 0) {
  537. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for "
  538. "vote info reply list msg");
  539. return (-1);
  540. }
  541. return (0);
  542. }
  543. static int
  544. qdevice_net_msg_received_vote_info_reply(struct qdevice_net_instance *instance,
  545. const struct msg_decoded *msg)
  546. {
  547. return (qdevice_net_msg_received_unexpected_msg(instance, msg, "vote info reply"));
  548. }
  549. static int
  550. qdevice_net_msg_received(struct qdevice_net_instance *instance)
  551. {
  552. struct msg_decoded msg;
  553. int res;
  554. int ret_val;
  555. msg_decoded_init(&msg);
  556. res = msg_decode(&instance->receive_buffer, &msg);
  557. if (res != 0) {
  558. /*
  559. * Error occurred. Disconnect.
  560. */
  561. qdevice_net_log_msg_decode_error(res);
  562. qdevice_net_log(LOG_ERR, "Disconnecting from server");
  563. return (-1);
  564. }
  565. ret_val = 0;
  566. switch (msg.type) {
  567. case MSG_TYPE_PREINIT:
  568. ret_val = qdevice_net_msg_received_preinit(instance, &msg);
  569. break;
  570. case MSG_TYPE_PREINIT_REPLY:
  571. ret_val = qdevice_net_msg_received_preinit_reply(instance, &msg);
  572. break;
  573. case MSG_TYPE_STARTTLS:
  574. ret_val = qdevice_net_msg_received_starttls(instance, &msg);
  575. break;
  576. case MSG_TYPE_SERVER_ERROR:
  577. ret_val = qdevice_net_msg_received_server_error(instance, &msg);
  578. break;
  579. case MSG_TYPE_INIT_REPLY:
  580. ret_val = qdevice_net_msg_received_init_reply(instance, &msg);
  581. break;
  582. case MSG_TYPE_SET_OPTION:
  583. ret_val = qdevice_net_msg_received_set_option(instance, &msg);
  584. break;
  585. case MSG_TYPE_SET_OPTION_REPLY:
  586. ret_val = qdevice_net_msg_received_set_option_reply(instance, &msg);
  587. break;
  588. case MSG_TYPE_ECHO_REQUEST:
  589. ret_val = qdevice_net_msg_received_echo_request(instance, &msg);
  590. break;
  591. case MSG_TYPE_ECHO_REPLY:
  592. ret_val = qdevice_net_msg_received_echo_reply(instance, &msg);
  593. break;
  594. case MSG_TYPE_NODE_LIST:
  595. ret_val = qdevice_net_msg_received_node_list(instance, &msg);
  596. break;
  597. case MSG_TYPE_NODE_LIST_REPLY:
  598. ret_val = qdevice_net_msg_received_node_list_reply(instance, &msg);
  599. break;
  600. case MSG_TYPE_ASK_FOR_VOTE:
  601. ret_val = qdevice_net_msg_received_ask_for_vote(instance, &msg);
  602. break;
  603. case MSG_TYPE_ASK_FOR_VOTE_REPLY:
  604. ret_val = qdevice_net_msg_received_ask_for_vote_reply(instance, &msg);
  605. break;
  606. case MSG_TYPE_VOTE_INFO:
  607. ret_val = qdevice_net_msg_received_vote_info(instance, &msg);
  608. break;
  609. case MSG_TYPE_VOTE_INFO_REPLY:
  610. ret_val = qdevice_net_msg_received_vote_info_reply(instance, &msg);
  611. break;
  612. default:
  613. qdevice_net_log(LOG_ERR, "Received unsupported message %u. "
  614. "Disconnecting from server", msg.type);
  615. ret_val = -1;
  616. break;
  617. }
  618. msg_decoded_destroy(&msg);
  619. return (ret_val);
  620. }
  621. /*
  622. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  623. */
  624. static int
  625. qdevice_net_socket_read(struct qdevice_net_instance *instance)
  626. {
  627. int res;
  628. int ret_val;
  629. int orig_skipping_msg;
  630. orig_skipping_msg = instance->skipping_msg;
  631. res = msgio_read(instance->socket, &instance->receive_buffer,
  632. &instance->msg_already_received_bytes, &instance->skipping_msg);
  633. if (!orig_skipping_msg && instance->skipping_msg) {
  634. qdevice_net_log(LOG_DEBUG, "msgio_read set skipping_msg");
  635. }
  636. ret_val = 0;
  637. switch (res) {
  638. case 0:
  639. /*
  640. * Partial read
  641. */
  642. break;
  643. case -1:
  644. qdevice_net_log(LOG_DEBUG, "Server closed connection");
  645. ret_val = -1;
  646. break;
  647. case -2:
  648. qdevice_net_log_nss(LOG_ERR, "Unhandled error when reading from server. "
  649. "Disconnecting from server");
  650. ret_val = -1;
  651. break;
  652. case -3:
  653. qdevice_net_log(LOG_ERR, "Can't store message header from server. "
  654. "Disconnecting from server");
  655. ret_val = -1;
  656. break;
  657. case -4:
  658. qdevice_net_log(LOG_ERR, "Can't store message from server. "
  659. "Disconnecting from server");
  660. ret_val = -1;
  661. break;
  662. case -5:
  663. qdevice_net_log(LOG_WARNING, "Server sent unsupported msg type %u. "
  664. "Disconnecting from server", msg_get_type(&instance->receive_buffer));
  665. ret_val = -1;
  666. break;
  667. case -6:
  668. qdevice_net_log(LOG_WARNING,
  669. "Server wants to send too long message %u bytes. Disconnecting from server",
  670. msg_get_len(&instance->receive_buffer));
  671. ret_val = -1;
  672. break;
  673. case 1:
  674. /*
  675. * Full message received / skipped
  676. */
  677. if (!instance->skipping_msg) {
  678. if (qdevice_net_msg_received(instance) == -1) {
  679. ret_val = -1;
  680. }
  681. } else {
  682. errx(1, "net_socket_read in skipping msg state");
  683. }
  684. instance->skipping_msg = 0;
  685. instance->msg_already_received_bytes = 0;
  686. dynar_clean(&instance->receive_buffer);
  687. break;
  688. default:
  689. errx(1, "qdevice_net_socket_read unhandled error %d", res);
  690. break;
  691. }
  692. return (ret_val);
  693. }
  694. static int
  695. qdevice_net_socket_write_finished(struct qdevice_net_instance *instance)
  696. {
  697. PRFileDesc *new_pr_fd;
  698. if (instance->state == QDEVICE_NET_INSTANCE_STATE_WAITING_STARTTLS_BEING_SENT) {
  699. /*
  700. * StartTLS sent to server. Begin with TLS handshake
  701. */
  702. if ((new_pr_fd = nss_sock_start_ssl_as_client(instance->socket, QNETD_NSS_SERVER_CN,
  703. qdevice_net_nss_bad_cert_hook,
  704. qdevice_net_nss_get_client_auth_data,
  705. (void *)QDEVICE_NET_NSS_CLIENT_CERT_NICKNAME, 0, NULL)) == NULL) {
  706. qdevice_net_log_nss(LOG_ERR, "Can't start TLS");
  707. return (-1);
  708. }
  709. /*
  710. * And send init msg
  711. */
  712. if (qdevice_net_send_init(instance) != 0) {
  713. return (-1);
  714. }
  715. instance->socket = new_pr_fd;
  716. }
  717. return (0);
  718. }
  719. static int
  720. qdevice_net_socket_write(struct qdevice_net_instance *instance)
  721. {
  722. int res;
  723. struct send_buffer_list_entry *send_buffer;
  724. enum msg_type sent_msg_type;
  725. send_buffer = send_buffer_list_get_active(&instance->send_buffer_list);
  726. if (send_buffer == NULL) {
  727. qdevice_net_log(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  728. return (-1);
  729. }
  730. res = msgio_write(instance->socket, &send_buffer->buffer,
  731. &send_buffer->msg_already_sent_bytes);
  732. if (res == 1) {
  733. sent_msg_type = msg_get_type(&send_buffer->buffer);
  734. send_buffer_list_delete(&instance->send_buffer_list, send_buffer);
  735. if (sent_msg_type != MSG_TYPE_ECHO_REQUEST) {
  736. if (qdevice_net_socket_write_finished(instance) == -1) {
  737. return (-1);
  738. }
  739. }
  740. }
  741. if (res == -1) {
  742. qdevice_net_log_nss(LOG_CRIT, "PR_Send returned 0");
  743. return (-1);
  744. }
  745. if (res == -2) {
  746. qdevice_net_log_nss(LOG_ERR, "Unhandled error when sending message to server");
  747. return (-1);
  748. }
  749. return (0);
  750. }
  751. #define QDEVICE_NET_POLL_NO_FDS 2
  752. #define QDEVICE_NET_POLL_SOCKET 0
  753. #define QDEVICE_NET_POLL_VOTEQUORUM 1
  754. static int
  755. qdevice_net_poll(struct qdevice_net_instance *instance)
  756. {
  757. PRPollDesc pfds[QDEVICE_NET_POLL_NO_FDS];
  758. PRInt32 poll_res;
  759. int i;
  760. pfds[QDEVICE_NET_POLL_SOCKET].fd = instance->socket;
  761. pfds[QDEVICE_NET_POLL_SOCKET].in_flags = PR_POLL_READ;
  762. if (!send_buffer_list_empty(&instance->send_buffer_list)) {
  763. pfds[QDEVICE_NET_POLL_SOCKET].in_flags |= PR_POLL_WRITE;
  764. }
  765. pfds[QDEVICE_NET_POLL_VOTEQUORUM].fd = instance->votequorum_poll_fd;
  766. pfds[QDEVICE_NET_POLL_VOTEQUORUM].in_flags = PR_POLL_READ;
  767. instance->schedule_disconnect = 0;
  768. if ((poll_res = PR_Poll(pfds, QDEVICE_NET_POLL_NO_FDS,
  769. timer_list_time_to_expire(&instance->main_timer_list))) > 0) {
  770. for (i = 0; i < QDEVICE_NET_POLL_NO_FDS; i++) {
  771. if (pfds[i].out_flags & PR_POLL_READ) {
  772. switch (i) {
  773. case QDEVICE_NET_POLL_SOCKET:
  774. if (qdevice_net_socket_read(instance) == -1) {
  775. instance->schedule_disconnect = 1;
  776. }
  777. break;
  778. case QDEVICE_NET_POLL_VOTEQUORUM:
  779. if (votequorum_dispatch(instance->votequorum_handle,
  780. CS_DISPATCH_ALL) != CS_OK) {
  781. errx(1, "Can't dispatch votequorum messages");
  782. }
  783. break;
  784. default:
  785. errx(1, "Unhandled read poll descriptor %u", i);
  786. break;
  787. }
  788. }
  789. if (!instance->schedule_disconnect && pfds[i].out_flags & PR_POLL_WRITE) {
  790. switch (i) {
  791. case QDEVICE_NET_POLL_SOCKET:
  792. if (qdevice_net_socket_write(instance) == -1) {
  793. instance->schedule_disconnect = 1;
  794. }
  795. break;
  796. default:
  797. errx(1, "Unhandled write poll descriptor %u", i);
  798. break;
  799. }
  800. }
  801. if (!instance->schedule_disconnect &&
  802. pfds[i].out_flags &
  803. (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) {
  804. switch (i) {
  805. case QDEVICE_NET_POLL_SOCKET:
  806. qdevice_net_log(LOG_CRIT, "POLL_ERR (%u) on main socket",
  807. pfds[i].out_flags);
  808. return (-1);
  809. break;
  810. default:
  811. errx(1, "Unhandled poll err on descriptor %u", i);
  812. break;
  813. }
  814. }
  815. }
  816. }
  817. if (!instance->schedule_disconnect) {
  818. timer_list_expire(&instance->main_timer_list);
  819. }
  820. if (instance->schedule_disconnect) {
  821. /*
  822. * Schedule disconnect can be set by this function, by some timer_list callback
  823. * or cmap/votequorum callbacks
  824. */
  825. return (-1);
  826. }
  827. return (0);
  828. }
  829. static void
  830. qdevice_net_instance_init_from_cmap(struct qdevice_net_instance *instance,
  831. cmap_handle_t cmap_handle)
  832. {
  833. uint32_t node_id;
  834. enum tlv_tls_supported tls_supported;
  835. int i;
  836. long int li;
  837. char *str;
  838. enum tlv_decision_algorithm_type decision_algorithm;
  839. struct tlv_tie_breaker tie_breaker;
  840. uint32_t heartbeat_interval;
  841. uint32_t sync_heartbeat_interval;
  842. uint32_t cast_vote_timer_interval;
  843. char *host_addr;
  844. int host_port;
  845. char *ep;
  846. char *cluster_name;
  847. /*
  848. * Check if provider is net
  849. */
  850. if (cmap_get_string(cmap_handle, "quorum.device.model", &str) != CS_OK) {
  851. errx(1, "Can't read quorum.device.model cmap key.");
  852. }
  853. if (strcmp(str, "net") != 0) {
  854. free(str);
  855. errx(1, "Configured device model is not net. "
  856. "This qdevice provider is only for net.");
  857. }
  858. free(str);
  859. /*
  860. * Get nodeid
  861. */
  862. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &node_id) != CS_OK) {
  863. errx(1, "Unable to retrive this node nodeid.");
  864. }
  865. /*
  866. * Check tls
  867. */
  868. if (cmap_get_string(cmap_handle, "quorum.device.net.tls", &str) == CS_OK) {
  869. if ((i = utils_parse_bool_str(str)) == -1) {
  870. free(str);
  871. errx(1, "quorum.device.net.tls value is not valid.");
  872. }
  873. if (i == 1) {
  874. tls_supported = TLV_TLS_SUPPORTED;
  875. } else {
  876. tls_supported = TLV_TLS_UNSUPPORTED;
  877. }
  878. free(str);
  879. }
  880. /*
  881. * Host
  882. */
  883. if (cmap_get_string(cmap_handle, "quorum.device.net.host", &str) != CS_OK) {
  884. errx(1, "Qdevice net daemon address is not defined (quorum.device.net.host)");
  885. }
  886. host_addr = str;
  887. if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
  888. host_port = strtol(str, &ep, 10);
  889. if (host_port <= 0 || host_port > ((uint16_t)~0) || *ep != '\0') {
  890. errx(1, "quorum.device.net.port must be in range 0-65535");
  891. }
  892. free(str);
  893. } else {
  894. host_port = QNETD_DEFAULT_HOST_PORT;
  895. }
  896. /*
  897. * Cluster name
  898. */
  899. if (cmap_get_string(cmap_handle, "totem.cluster_name", &str) != CS_OK) {
  900. errx(1, "Cluster name (totem.cluster_name) has to be defined.");
  901. }
  902. cluster_name = str;
  903. /*
  904. * Configure timeouts
  905. */
  906. if (cmap_get_uint32(cmap_handle, "quorum.device.timeout", &heartbeat_interval) != CS_OK) {
  907. heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  908. }
  909. cast_vote_timer_interval = heartbeat_interval * 0.5;
  910. heartbeat_interval = heartbeat_interval * 0.8;
  911. if (cmap_get_uint32(cmap_handle, "quorum.device.sync_timeout",
  912. &sync_heartbeat_interval) != CS_OK) {
  913. sync_heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_SYNC_TIMEOUT;
  914. }
  915. sync_heartbeat_interval = sync_heartbeat_interval * 0.8;
  916. /*
  917. * Choose decision algorithm
  918. */
  919. if (cmap_get_string(cmap_handle, "quorum.device.net.algorithm", &str) != CS_OK) {
  920. decision_algorithm = QDEVICE_NET_DEFAULT_ALGORITHM;
  921. } else {
  922. if (strcmp(str, "test") == 0) {
  923. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_TEST;
  924. } else if (strcmp(str, "ffsplit") == 0) {
  925. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_FFSPLIT;
  926. } else if (strcmp(str, "2nodelms") == 0) {
  927. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_2NODELMS;
  928. } else if (strcmp(str, "lms") == 0) {
  929. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_LMS;
  930. } else {
  931. errx(1, "Unknown decision algorithm %s", str);
  932. }
  933. free(str);
  934. }
  935. /*
  936. * Load tie_breaker mode
  937. */
  938. memset(&tie_breaker, 0, sizeof(tie_breaker));
  939. if (cmap_get_string(cmap_handle, "quorum.device.net.tie_breaker", &str) != CS_OK) {
  940. tie_breaker.mode = QDEVICE_NET_DEFAULT_TIE_BREAKER_MODE;
  941. } else {
  942. if (strcmp(str, "lowest") == 0) {
  943. tie_breaker.mode = TLV_TIE_BREAKER_MODE_LOWEST;
  944. } else if (strcmp(str, "highest") == 0) {
  945. tie_breaker.mode = TLV_TIE_BREAKER_MODE_HIGHEST;
  946. } else {
  947. li = strtol(str, &ep, 10);
  948. if (li <= 0 || li > ((uint32_t)~0) || *ep != '\0') {
  949. errx(1, "tie_breaker must be lowest|highest|valid_node_id");
  950. }
  951. tie_breaker.mode = TLV_TIE_BREAKER_MODE_NODE_ID;
  952. tie_breaker.node_id = li;
  953. }
  954. free(str);
  955. }
  956. /*
  957. * Really initialize instance
  958. */
  959. if (qdevice_net_instance_init(instance,
  960. QDEVICE_NET_INITIAL_MSG_RECEIVE_SIZE, QDEVICE_NET_INITIAL_MSG_SEND_SIZE,
  961. QDEVICE_NET_MIN_MSG_SEND_SIZE, QDEVICE_NET_MAX_SEND_BUFFERS,
  962. QDEVICE_NET_MAX_MSG_RECEIVE_SIZE,
  963. tls_supported, node_id, decision_algorithm,
  964. heartbeat_interval, sync_heartbeat_interval, cast_vote_timer_interval,
  965. host_addr, host_port, cluster_name, &tie_breaker) == -1) {
  966. errx(1, "Can't initialize qdevice-net");
  967. }
  968. instance->cmap_handle = cmap_handle;
  969. }
  970. int
  971. main(void)
  972. {
  973. struct qdevice_net_instance instance;
  974. cmap_handle_t cmap_handle;
  975. struct send_buffer_list_entry *send_buffer;
  976. /*
  977. * Init
  978. */
  979. qdevice_net_cmap_init(&cmap_handle);
  980. qdevice_net_instance_init_from_cmap(&instance, cmap_handle);
  981. qdevice_net_log_init(QDEVICE_NET_LOG_TARGET_STDERR);
  982. qdevice_net_log_set_debug(1);
  983. if (nss_sock_init_nss((instance.tls_supported != TLV_TLS_UNSUPPORTED ?
  984. (char *)QDEVICE_NET_NSS_DB_DIR : NULL)) != 0) {
  985. nss_sock_err(1);
  986. }
  987. /*
  988. * Try to connect to qnetd host
  989. */
  990. instance.socket = nss_sock_create_client_socket(instance.host_addr, instance.host_port,
  991. PR_AF_UNSPEC, 100);
  992. if (instance.socket == NULL) {
  993. nss_sock_err(1);
  994. }
  995. if (nss_sock_set_nonblocking(instance.socket) != 0) {
  996. nss_sock_err(1);
  997. }
  998. qdevice_net_votequorum_init(&instance);
  999. /*
  1000. * Create and schedule send of preinit message to qnetd
  1001. */
  1002. send_buffer = send_buffer_list_get_new(&instance.send_buffer_list);
  1003. if (send_buffer == NULL) {
  1004. errx(1, "Can't allocate send buffer list");
  1005. }
  1006. instance.last_msg_seq_num = 1;
  1007. if (msg_create_preinit(&send_buffer->buffer, instance.cluster_name, 1,
  1008. instance.last_msg_seq_num) == 0) {
  1009. errx(1, "Can't allocate buffer");
  1010. }
  1011. send_buffer_list_put(&instance.send_buffer_list, send_buffer);
  1012. instance.state = QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY;
  1013. /*
  1014. * Main loop
  1015. */
  1016. while (qdevice_net_poll(&instance) == 0) {
  1017. }
  1018. /*
  1019. * Cleanup
  1020. */
  1021. if (PR_Close(instance.socket) != PR_SUCCESS) {
  1022. qdevice_net_log_nss(LOG_WARNING, "Unable to close connection");
  1023. }
  1024. /*
  1025. * Close cmap and votequorum connections
  1026. */
  1027. if (votequorum_qdevice_unregister(instance.votequorum_handle,
  1028. QDEVICE_NET_VOTEQUORUM_DEVICE_NAME) != CS_OK) {
  1029. qdevice_net_log_nss(LOG_WARNING, "Unable to unregister votequorum device");
  1030. }
  1031. votequorum_finalize(instance.votequorum_handle);
  1032. cmap_finalize(instance.cmap_handle);
  1033. qdevice_net_instance_destroy(&instance);
  1034. SSL_ClearSessionCache();
  1035. if (NSS_Shutdown() != SECSuccess) {
  1036. nss_sock_err(1);
  1037. }
  1038. PR_Cleanup();
  1039. qdevice_net_log_close();
  1040. return (0);
  1041. }