corosync-qdevice-net.c 32 KB

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