corosync-qdevice-net.c 31 KB

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