corosync-qdevice-net.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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 {
  955. errx(1, "Unknown decision algorithm %s", str);
  956. }
  957. free(str);
  958. }
  959. /*
  960. * Really initialize instance
  961. */
  962. if (qdevice_net_instance_init(instance,
  963. QDEVICE_NET_INITIAL_MSG_RECEIVE_SIZE, QDEVICE_NET_INITIAL_MSG_SEND_SIZE,
  964. QDEVICE_NET_MIN_MSG_SEND_SIZE, QDEVICE_NET_MAX_SEND_BUFFERS, QDEVICE_NET_MAX_MSG_RECEIVE_SIZE,
  965. tls_supported, node_id, decision_algorithm,
  966. heartbeat_interval, sync_heartbeat_interval, cast_vote_timer_interval,
  967. host_addr, host_port, cluster_name) == -1) {
  968. errx(1, "Can't initialize qdevice-net");
  969. }
  970. instance->cmap_handle = cmap_handle;
  971. }
  972. int
  973. main(void)
  974. {
  975. struct qdevice_net_instance instance;
  976. cmap_handle_t cmap_handle;
  977. struct send_buffer_list_entry *send_buffer;
  978. /*
  979. * Init
  980. */
  981. qdevice_net_init_cmap(&cmap_handle);
  982. qdevice_net_instance_init_from_cmap(&instance, cmap_handle);
  983. qdevice_net_log_init(QDEVICE_NET_LOG_TARGET_STDERR);
  984. qdevice_net_log_set_debug(1);
  985. if (nss_sock_init_nss((instance.tls_supported != TLV_TLS_UNSUPPORTED ?
  986. (char *)QDEVICE_NET_NSS_DB_DIR : NULL)) != 0) {
  987. nss_sock_err(1);
  988. }
  989. /*
  990. * Try to connect to qnetd host
  991. */
  992. instance.socket = nss_sock_create_client_socket(instance.host_addr, instance.host_port,
  993. PR_AF_UNSPEC, 100);
  994. if (instance.socket == NULL) {
  995. nss_sock_err(1);
  996. }
  997. if (nss_sock_set_nonblocking(instance.socket) != 0) {
  998. nss_sock_err(1);
  999. }
  1000. qdevice_net_votequorum_init(&instance);
  1001. /*
  1002. * Create and schedule send of preinit message to qnetd
  1003. */
  1004. send_buffer = send_buffer_list_get_new(&instance.send_buffer_list);
  1005. if (send_buffer == NULL) {
  1006. errx(1, "Can't allocate send buffer list");
  1007. }
  1008. instance.last_msg_seq_num = 1;
  1009. if (msg_create_preinit(&send_buffer->buffer, instance.cluster_name, 1,
  1010. instance.last_msg_seq_num) == 0) {
  1011. errx(1, "Can't allocate buffer");
  1012. }
  1013. send_buffer_list_put(&instance.send_buffer_list, send_buffer);
  1014. instance.state = QDEVICE_NET_INSTANCE_STATE_WAITING_PREINIT_REPLY;
  1015. /*
  1016. * Main loop
  1017. */
  1018. while (qdevice_net_poll(&instance) == 0) {
  1019. }
  1020. /*
  1021. * Cleanup
  1022. */
  1023. if (PR_Close(instance.socket) != PR_SUCCESS) {
  1024. qdevice_net_log_nss(LOG_WARNING, "Unable to close connection");
  1025. }
  1026. /*
  1027. * Close cmap and votequorum connections
  1028. */
  1029. if (votequorum_qdevice_unregister(instance.votequorum_handle,
  1030. QDEVICE_NET_VOTEQUORUM_DEVICE_NAME) != CS_OK) {
  1031. qdevice_net_log_nss(LOG_WARNING, "Unable to unregister votequorum device");
  1032. }
  1033. votequorum_finalize(instance.votequorum_handle);
  1034. cmap_finalize(instance.cmap_handle);
  1035. qdevice_net_instance_destroy(&instance);
  1036. SSL_ClearSessionCache();
  1037. if (NSS_Shutdown() != SECSuccess) {
  1038. nss_sock_err(1);
  1039. }
  1040. PR_Cleanup();
  1041. qdevice_net_log_close();
  1042. return (0);
  1043. }