corosync-qdevice-net.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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 "qnetd-defines.h"
  57. #include "dynar.h"
  58. #include "nss-sock.h"
  59. #include "tlv.h"
  60. #include "msg.h"
  61. #include "msgio.h"
  62. #include "qnetd-log.h"
  63. #include "timer-list.h"
  64. #include "send-buffer-list.h"
  65. #define NSS_DB_DIR COROSYSCONFDIR "/qdevice-net/nssdb"
  66. /*
  67. * It's usually not a good idea to change following defines
  68. */
  69. #define QDEVICE_NET_INITIAL_MSG_RECEIVE_SIZE (1 << 15)
  70. #define QDEVICE_NET_INITIAL_MSG_SEND_SIZE (1 << 15)
  71. #define QDEVICE_NET_MIN_MSG_SEND_SIZE QDEVICE_NET_INITIAL_MSG_SEND_SIZE
  72. #define QDEVICE_NET_MAX_MSG_RECEIVE_SIZE (1 << 24)
  73. #define QNETD_NSS_SERVER_CN "Qnetd Server"
  74. #define QDEVICE_NET_NSS_CLIENT_CERT_NICKNAME "Cluster Cert"
  75. #define QDEVICE_NET_VOTEQUORUM_DEVICE_NAME "QdeviceNet"
  76. #define QDEVICE_NET_MAX_SEND_BUFFERS 10
  77. #define qdevice_net_log qnetd_log
  78. #define qdevice_net_log_nss qnetd_log_nss
  79. #define qdevice_net_log_init qnetd_log_init
  80. #define qdevice_net_log_close qnetd_log_close
  81. #define qdevice_net_log_set_debug qnetd_log_set_debug
  82. #define QDEVICE_NET_LOG_TARGET_STDERR QNETD_LOG_TARGET_STDERR
  83. #define QDEVICE_NET_LOG_TARGET_SYSLOG QNETD_LOG_TARGET_SYSLOG
  84. #define MAX_CS_TRY_AGAIN 10
  85. enum qdevice_net_state {
  86. QDEVICE_NET_STATE_WAITING_PREINIT_REPLY,
  87. QDEVICE_NET_STATE_WAITING_STARTTLS_BEING_SENT,
  88. QDEVICE_NET_STATE_WAITING_INIT_REPLY,
  89. QDEVICE_NET_STATE_WAITING_SET_OPTION_REPLY,
  90. QDEVICE_NET_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS,
  91. };
  92. struct qdevice_net_instance {
  93. PRFileDesc *socket;
  94. size_t initial_send_size;
  95. size_t initial_receive_size;
  96. size_t max_receive_size;
  97. size_t min_send_size;
  98. struct dynar receive_buffer;
  99. struct send_buffer_list send_buffer_list;
  100. int skipping_msg;
  101. size_t msg_already_received_bytes;
  102. enum qdevice_net_state state;
  103. uint32_t last_msg_seq_num;
  104. uint32_t echo_request_expected_msg_seq_num;
  105. uint32_t echo_reply_received_msg_seq_num;
  106. enum tlv_tls_supported tls_supported;
  107. int using_tls;
  108. uint32_t node_id;
  109. uint32_t heartbeat_interval; /* Heartbeat interval during normal operation */
  110. uint32_t sync_heartbeat_interval; /* Heartbeat interval during corosync sync */
  111. const char *host_addr;
  112. uint16_t host_port;
  113. const char *cluster_name;
  114. enum tlv_decision_algorithm_type decision_algorithm;
  115. struct timer_list main_timer_list;
  116. struct timer_list_entry *echo_request_timer;
  117. int schedule_disconnect;
  118. cmap_handle_t cmap_handle;
  119. votequorum_handle_t votequorum_handle;
  120. PRFileDesc *votequorum_poll_fd;
  121. };
  122. static votequorum_ring_id_t global_last_received_ring_id;
  123. static void
  124. err_nss(void) {
  125. errx(1, "nss error %d: %s", PR_GetError(), PR_ErrorToString(PR_GetError(),
  126. PR_LANGUAGE_I_DEFAULT));
  127. }
  128. static SECStatus
  129. qdevice_net_nss_bad_cert_hook(void *arg, PRFileDesc *fd) {
  130. if (PR_GetError() == SEC_ERROR_EXPIRED_CERTIFICATE ||
  131. PR_GetError() == SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE ||
  132. PR_GetError() == SEC_ERROR_CRL_EXPIRED ||
  133. PR_GetError() == SEC_ERROR_KRL_EXPIRED ||
  134. PR_GetError() == SSL_ERROR_EXPIRED_CERT_ALERT) {
  135. qdevice_net_log(LOG_WARNING, "Server certificate is expired.");
  136. return (SECSuccess);
  137. }
  138. qdevice_net_log_nss(LOG_ERR, "Server certificate verification failure.");
  139. return (SECFailure);
  140. }
  141. static SECStatus
  142. qdevice_net_nss_get_client_auth_data(void *arg, PRFileDesc *sock, struct CERTDistNamesStr *caNames,
  143. struct CERTCertificateStr **pRetCert, struct SECKEYPrivateKeyStr **pRetKey)
  144. {
  145. qdevice_net_log(LOG_DEBUG, "Sending client auth data.");
  146. return (NSS_GetClientAuthData(arg, sock, caNames, pRetCert, pRetKey));
  147. }
  148. static int
  149. qdevice_net_schedule_echo_request_send(struct qdevice_net_instance *instance)
  150. {
  151. struct send_buffer_list_entry *send_buffer;
  152. if (instance->echo_reply_received_msg_seq_num !=
  153. instance->echo_request_expected_msg_seq_num) {
  154. qdevice_net_log(LOG_ERR, "Server didn't send echo reply message on time. "
  155. "Disconnecting from server.");
  156. return (-1);
  157. }
  158. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  159. if (send_buffer == NULL) {
  160. qdevice_net_log(LOG_CRIT, "Can't allocate send list buffer for reply msg.");
  161. return (-1);
  162. }
  163. instance->echo_request_expected_msg_seq_num++;
  164. if (msg_create_echo_request(&send_buffer->buffer, 1,
  165. instance->echo_request_expected_msg_seq_num) == -1) {
  166. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for echo request msg");
  167. return (-1);
  168. }
  169. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  170. return (0);
  171. }
  172. static void
  173. qdevice_net_log_msg_decode_error(int ret)
  174. {
  175. switch (ret) {
  176. case -1:
  177. qdevice_net_log(LOG_WARNING, "Received message with option with invalid length");
  178. break;
  179. case -2:
  180. qdevice_net_log(LOG_CRIT, "Can't allocate memory");
  181. break;
  182. case -3:
  183. qdevice_net_log(LOG_WARNING, "Received inconsistent msg (tlv len > msg size)");
  184. break;
  185. case -4:
  186. qdevice_net_log(LOG_ERR, "Received message with option with invalid value");
  187. break;
  188. default:
  189. qdevice_net_log(LOG_ERR, "Unknown error occured when decoding message");
  190. break;
  191. }
  192. }
  193. /*
  194. * -1 - Incompatible tls combination
  195. * 0 - Don't use TLS
  196. * 1 - Use TLS
  197. */
  198. static int
  199. qdevice_net_check_tls_compatibility(enum tlv_tls_supported server_tls,
  200. enum tlv_tls_supported client_tls)
  201. {
  202. int res;
  203. res = -1;
  204. switch (server_tls) {
  205. case TLV_TLS_UNSUPPORTED:
  206. switch (client_tls) {
  207. case TLV_TLS_UNSUPPORTED: res = 0; break;
  208. case TLV_TLS_SUPPORTED: res = 0; break;
  209. case TLV_TLS_REQUIRED: res = -1; break;
  210. }
  211. break;
  212. case TLV_TLS_SUPPORTED:
  213. switch (client_tls) {
  214. case TLV_TLS_UNSUPPORTED: res = 0; break;
  215. case TLV_TLS_SUPPORTED: res = 1; break;
  216. case TLV_TLS_REQUIRED: res = 1; break;
  217. }
  218. break;
  219. case TLV_TLS_REQUIRED:
  220. switch (client_tls) {
  221. case TLV_TLS_UNSUPPORTED: res = -1; break;
  222. case TLV_TLS_SUPPORTED: res = 1; break;
  223. case TLV_TLS_REQUIRED: res = 1; break;
  224. }
  225. break;
  226. }
  227. return (res);
  228. }
  229. static int
  230. qdevice_net_msg_received_preinit(struct qdevice_net_instance *instance,
  231. const struct msg_decoded *msg)
  232. {
  233. qdevice_net_log(LOG_ERR, "Received unexpected preinit message. Disconnecting from server");
  234. return (-1);
  235. }
  236. static int
  237. qdevice_net_msg_check_seq_number(struct qdevice_net_instance *instance,
  238. const struct msg_decoded *msg)
  239. {
  240. if (!msg->seq_number_set || msg->seq_number != instance->last_msg_seq_num) {
  241. qdevice_net_log(LOG_ERR, "Received message doesn't contain seq_number or "
  242. "it's not expected one.");
  243. return (-1);
  244. }
  245. return (0);
  246. }
  247. static int
  248. qdevice_net_msg_check_echo_reply_seq_number(struct qdevice_net_instance *instance,
  249. const struct msg_decoded *msg)
  250. {
  251. if (!msg->seq_number_set) {
  252. qdevice_net_log(LOG_ERR, "Received echo reply message doesn't contain seq_number.");
  253. return (-1);
  254. }
  255. if (msg->seq_number != instance->echo_request_expected_msg_seq_num) {
  256. qdevice_net_log(LOG_ERR, "Server doesn't replied in expected time. "
  257. "Closing connection");
  258. return (-1);
  259. }
  260. return (0);
  261. }
  262. static int
  263. qdevice_net_send_init(struct qdevice_net_instance *instance)
  264. {
  265. enum msg_type *supported_msgs;
  266. size_t no_supported_msgs;
  267. enum tlv_opt_type *supported_opts;
  268. size_t no_supported_opts;
  269. struct send_buffer_list_entry *send_buffer;
  270. tlv_get_supported_options(&supported_opts, &no_supported_opts);
  271. msg_get_supported_messages(&supported_msgs, &no_supported_msgs);
  272. instance->last_msg_seq_num++;
  273. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  274. if (send_buffer == NULL) {
  275. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for init msg");
  276. return (-1);
  277. }
  278. if (msg_create_init(&send_buffer->buffer, 1, instance->last_msg_seq_num,
  279. instance->decision_algorithm,
  280. supported_msgs, no_supported_msgs, supported_opts, no_supported_opts,
  281. instance->node_id) == 0) {
  282. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for init msg");
  283. return (-1);
  284. }
  285. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  286. instance->state = QDEVICE_NET_STATE_WAITING_INIT_REPLY;
  287. return (0);
  288. }
  289. static int
  290. qdevice_net_msg_received_preinit_reply(struct qdevice_net_instance *instance,
  291. const struct msg_decoded *msg)
  292. {
  293. int res;
  294. struct send_buffer_list_entry *send_buffer;
  295. if (instance->state != QDEVICE_NET_STATE_WAITING_PREINIT_REPLY) {
  296. qdevice_net_log(LOG_ERR, "Received unexpected preinit reply message. "
  297. "Disconnecting from server");
  298. return (-1);
  299. }
  300. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  301. return (-1);
  302. }
  303. /*
  304. * Check TLS support
  305. */
  306. if (!msg->tls_supported_set || !msg->tls_client_cert_required_set) {
  307. qdevice_net_log(LOG_ERR, "Required tls_supported or tls_client_cert_required "
  308. "option is unset");
  309. return (-1);
  310. }
  311. res = qdevice_net_check_tls_compatibility(msg->tls_supported, instance->tls_supported);
  312. if (res == -1) {
  313. qdevice_net_log(LOG_ERR, "Incompatible tls configuration (server %u client %u)",
  314. msg->tls_supported, instance->tls_supported);
  315. return (-1);
  316. } else if (res == 1) {
  317. /*
  318. * Start TLS
  319. */
  320. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  321. if (send_buffer == NULL) {
  322. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for "
  323. "starttls msg");
  324. return (-1);
  325. }
  326. instance->last_msg_seq_num++;
  327. if (msg_create_starttls(&send_buffer->buffer, 1,
  328. instance->last_msg_seq_num) == 0) {
  329. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for starttls msg");
  330. return (-1);
  331. }
  332. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  333. instance->state = QDEVICE_NET_STATE_WAITING_STARTTLS_BEING_SENT;
  334. } else if (res == 0) {
  335. if (qdevice_net_send_init(instance) != 0) {
  336. return (-1);
  337. }
  338. }
  339. return (0);
  340. }
  341. static int
  342. qdevice_net_msg_received_init_reply(struct qdevice_net_instance *instance,
  343. const struct msg_decoded *msg)
  344. {
  345. size_t zi;
  346. int res;
  347. struct send_buffer_list_entry *send_buffer;
  348. if (instance->state != QDEVICE_NET_STATE_WAITING_INIT_REPLY) {
  349. qdevice_net_log(LOG_ERR, "Received unexpected init reply message. "
  350. "Disconnecting from server");
  351. return (-1);
  352. }
  353. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  354. return (-1);
  355. }
  356. if (!msg->reply_error_code_set) {
  357. qdevice_net_log(LOG_ERR, "Received init reply message without error code."
  358. "Disconnecting from server");
  359. return (-1);
  360. }
  361. if (msg->reply_error_code != TLV_REPLY_ERROR_CODE_NO_ERROR) {
  362. qdevice_net_log(LOG_ERR, "Received init reply message with error code %"PRIu16". "
  363. "Disconnecting from server", msg->reply_error_code);
  364. return (-1);
  365. }
  366. if (!msg->server_maximum_request_size_set || !msg->server_maximum_reply_size_set) {
  367. qdevice_net_log(LOG_ERR, "Required maximum_request_size or maximum_reply_size "
  368. "option is unset");
  369. return (-1);
  370. }
  371. if (msg->supported_messages == NULL || msg->supported_options == NULL) {
  372. qdevice_net_log(LOG_ERR, "Required supported messages or supported options "
  373. "option is unset");
  374. return (-1);
  375. }
  376. if (msg->supported_decision_algorithms == NULL) {
  377. qdevice_net_log(LOG_ERR, "Required supported decision algorithms option is unset");
  378. return (-1);
  379. }
  380. if (msg->server_maximum_request_size < instance->min_send_size) {
  381. qdevice_net_log(LOG_ERR,
  382. "Server accepts maximum %zu bytes message but this client minimum "
  383. "is %zu bytes.", msg->server_maximum_request_size, instance->min_send_size);
  384. return (-1);
  385. }
  386. if (msg->server_maximum_reply_size > instance->max_receive_size) {
  387. qdevice_net_log(LOG_ERR,
  388. "Server may send message up to %zu bytes message but this client maximum "
  389. "is %zu bytes.", msg->server_maximum_reply_size, instance->max_receive_size);
  390. return (-1);
  391. }
  392. /*
  393. * Change buffer sizes
  394. */
  395. dynar_set_max_size(&instance->receive_buffer, msg->server_maximum_reply_size);
  396. send_buffer_list_set_max_buffer_size(&instance->send_buffer_list,
  397. msg->server_maximum_request_size);
  398. /*
  399. * Check if server supports decision algorithm we need
  400. */
  401. res = 0;
  402. for (zi = 0; zi < msg->no_supported_decision_algorithms && !res; zi++) {
  403. if (msg->supported_decision_algorithms[zi] == instance->decision_algorithm) {
  404. res = 1;
  405. }
  406. }
  407. if (!res) {
  408. qdevice_net_log(LOG_ERR, "Server doesn't support required decision algorithm");
  409. return (-1);
  410. }
  411. /*
  412. * Send set options message
  413. */
  414. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  415. if (send_buffer == NULL) {
  416. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for set option msg");
  417. return (-1);
  418. }
  419. instance->last_msg_seq_num++;
  420. if (msg_create_set_option(&send_buffer->buffer, 1, instance->last_msg_seq_num,
  421. 1, instance->heartbeat_interval) == 0) {
  422. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for set option msg");
  423. return (-1);
  424. }
  425. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  426. instance->state = QDEVICE_NET_STATE_WAITING_SET_OPTION_REPLY;
  427. return (0);
  428. }
  429. static int
  430. qdevice_net_msg_received_starttls(struct qdevice_net_instance *instance,
  431. const struct msg_decoded *msg)
  432. {
  433. qdevice_net_log(LOG_ERR, "Received unexpected starttls message. Disconnecting from server");
  434. return (-1);
  435. }
  436. static int
  437. qdevice_net_msg_received_server_error(struct qdevice_net_instance *instance,
  438. const struct msg_decoded *msg)
  439. {
  440. if (!msg->reply_error_code_set) {
  441. qdevice_net_log(LOG_ERR, "Received server error without error code set. "
  442. "Disconnecting from server");
  443. } else {
  444. qdevice_net_log(LOG_ERR, "Received server error %"PRIu16". "
  445. "Disconnecting from server", msg->reply_error_code);
  446. }
  447. return (-1);
  448. }
  449. static int
  450. qdevice_net_msg_received_set_option(struct qdevice_net_instance *instance,
  451. const struct msg_decoded *msg)
  452. {
  453. qdevice_net_log(LOG_ERR, "Received unexpected set option message. "
  454. "Disconnecting from server");
  455. return (-1);
  456. }
  457. static int
  458. qdevice_net_timer_send_heartbeat(void *data1, void *data2)
  459. {
  460. struct qdevice_net_instance *instance;
  461. instance = (struct qdevice_net_instance *)data1;
  462. if (qdevice_net_schedule_echo_request_send(instance) == -1) {
  463. instance->schedule_disconnect = 1;
  464. return (0);
  465. }
  466. /*
  467. * Schedule this function callback again
  468. */
  469. return (-1);
  470. }
  471. static uint32_t
  472. qdevice_net_autogenerate_node_id(const char *addr, int clear_node_high_byte)
  473. {
  474. struct addrinfo *ainfo;
  475. struct addrinfo ahints;
  476. int ret, i;
  477. memset(&ahints, 0, sizeof(ahints));
  478. ahints.ai_socktype = SOCK_DGRAM;
  479. ahints.ai_protocol = IPPROTO_UDP;
  480. /*
  481. * Hardcoded AF_INET because autogenerated nodeid is valid only for ipv4
  482. */
  483. ahints.ai_family = AF_INET;
  484. ret = getaddrinfo(addr, NULL, &ahints, &ainfo);
  485. if (ret != 0)
  486. return (0);
  487. if (ainfo->ai_family != AF_INET) {
  488. freeaddrinfo(ainfo);
  489. return (0);
  490. }
  491. memcpy(&i, &((struct sockaddr_in *)ainfo->ai_addr)->sin_addr, sizeof(struct in_addr));
  492. freeaddrinfo(ainfo);
  493. ret = htonl(i);
  494. if (clear_node_high_byte) {
  495. ret &= 0x7FFFFFFF;
  496. }
  497. return (ret);
  498. }
  499. static int
  500. qdevice_net_get_nodelist(cmap_handle_t cmap_handle, struct node_list *list)
  501. {
  502. cs_error_t cs_err;
  503. cmap_iter_handle_t iter_handle;
  504. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  505. char tmp_key[CMAP_KEYNAME_MAXLEN + 1];
  506. int res;
  507. int ret_value;
  508. unsigned int node_pos;
  509. uint32_t node_id;
  510. uint32_t data_center_id;
  511. char *tmp_str;
  512. char *addr0_str;
  513. int clear_node_high_byte;
  514. ret_value = 0;
  515. node_list_init(list);
  516. cs_err = cmap_iter_init(cmap_handle, "nodelist.node.", &iter_handle);
  517. if (cs_err != CS_OK) {
  518. return (-1);
  519. }
  520. while ((cs_err = cmap_iter_next(cmap_handle, iter_handle, key_name, NULL, NULL)) == CS_OK) {
  521. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  522. if (res != 2) {
  523. continue;
  524. }
  525. if (strcmp(tmp_key, "ring0_addr") != 0) {
  526. continue;
  527. }
  528. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  529. cs_err = cmap_get_uint32(cmap_handle, tmp_key, &node_id);
  530. if (cs_err == CS_ERR_NOT_EXIST) {
  531. /*
  532. * Nodeid doesn't exists -> autogenerate node id
  533. */
  534. clear_node_high_byte = 0;
  535. if (cmap_get_string(cmap_handle, "totem.clear_node_high_bit", &tmp_str) == CS_OK) {
  536. if (strcmp (tmp_str, "yes") == 0) {
  537. clear_node_high_byte = 1;
  538. }
  539. free(tmp_str);
  540. }
  541. if (cmap_get_string(cmap_handle, key_name, &addr0_str) != CS_OK) {
  542. return (-1);
  543. }
  544. node_id = qdevice_net_autogenerate_node_id(addr0_str, clear_node_high_byte);
  545. free(addr0_str);
  546. } else if (cs_err != CS_OK) {
  547. ret_value = -1;
  548. goto iter_finalize;
  549. }
  550. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.datacenterid", node_pos);
  551. if (cmap_get_uint32(cmap_handle, tmp_key, &data_center_id) != CS_OK) {
  552. data_center_id = 0;
  553. }
  554. if (node_list_add(list, node_id, data_center_id, TLV_NODE_STATE_NOT_SET) == NULL) {
  555. ret_value = -1;
  556. goto iter_finalize;
  557. }
  558. }
  559. iter_finalize:
  560. cmap_iter_finalize(cmap_handle, iter_handle);
  561. if (ret_value != 0) {
  562. node_list_free(list);
  563. }
  564. return (ret_value);
  565. }
  566. static
  567. int qdevice_net_get_cmap_config_version(cmap_handle_t cmap_handle, uint64_t *config_version)
  568. {
  569. int res;
  570. if (cmap_get_uint64(cmap_handle, "totem.config_version", config_version) == CS_OK) {
  571. res = 1;
  572. } else {
  573. *config_version = 0;
  574. res = 0;
  575. }
  576. return (res);
  577. }
  578. static int
  579. qdevice_net_send_config_node_list(struct qdevice_net_instance *instance, int initial)
  580. {
  581. struct node_list nlist;
  582. struct send_buffer_list_entry *send_buffer;
  583. uint64_t config_version;
  584. int send_config_version;
  585. /*
  586. * Send initial node list
  587. */
  588. if (qdevice_net_get_nodelist(instance->cmap_handle, &nlist) != 0) {
  589. qdevice_net_log(LOG_ERR, "Can't get initial configuration node list.");
  590. return (-1);
  591. }
  592. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  593. if (send_buffer == NULL) {
  594. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for config "
  595. "node list msg");
  596. node_list_free(&nlist);
  597. return (-1);
  598. }
  599. send_config_version = qdevice_net_get_cmap_config_version(instance->cmap_handle,
  600. &config_version);
  601. instance->last_msg_seq_num++;
  602. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  603. (initial ? TLV_NODE_LIST_TYPE_INITIAL_CONFIG : TLV_NODE_LIST_TYPE_CHANGED_CONFIG),
  604. 0, NULL, send_config_version, config_version, 0, TLV_QUORATE_INQUORATE, &nlist) == 0) {
  605. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for config list msg");
  606. node_list_free(&nlist);
  607. return (-1);
  608. }
  609. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  610. return (0);
  611. }
  612. static int
  613. qdevice_net_register_votequorum_callbacks(struct qdevice_net_instance *instance)
  614. {
  615. cs_error_t res;
  616. if ((res = votequorum_trackstart(instance->votequorum_handle, 0, CS_TRACK_CHANGES)) != CS_OK) {
  617. qdevice_net_log(LOG_ERR, "Can't start tracking votequorum changes. Error %s",
  618. cs_strerror(res));
  619. return (-1);
  620. }
  621. return (0);
  622. }
  623. static int
  624. qdevice_net_msg_received_set_option_reply(struct qdevice_net_instance *instance,
  625. const struct msg_decoded *msg)
  626. {
  627. if (qdevice_net_msg_check_seq_number(instance, msg) != 0) {
  628. return (-1);
  629. }
  630. if (!msg->decision_algorithm_set || !msg->heartbeat_interval_set) {
  631. qdevice_net_log(LOG_ERR, "Received set option reply message without "
  632. "required options. Disconnecting from server");
  633. }
  634. if (msg->decision_algorithm != instance->decision_algorithm ||
  635. msg->heartbeat_interval != instance->heartbeat_interval) {
  636. qdevice_net_log(LOG_ERR, "Server doesn't accept sent decision algorithm or "
  637. "heartbeat interval.");
  638. return (-1);
  639. }
  640. /*
  641. * Server accepted heartbeat interval -> schedule regular sending of echo request
  642. */
  643. if (instance->heartbeat_interval > 0) {
  644. instance->echo_request_timer = timer_list_add(&instance->main_timer_list,
  645. instance->heartbeat_interval, qdevice_net_timer_send_heartbeat,
  646. (void *)instance, NULL);
  647. if (instance->echo_request_timer == NULL) {
  648. qdevice_net_log(LOG_ERR, "Can't schedule regular sending of heartbeat.");
  649. return (-1);
  650. }
  651. }
  652. /*
  653. * Now we can finally really send node list and initialize qdevice
  654. */
  655. if (qdevice_net_send_config_node_list(instance, 1) != 0) {
  656. return (-1);
  657. }
  658. if (qdevice_net_register_votequorum_callbacks(instance) != 0) {
  659. return (-1);
  660. }
  661. instance->state = QDEVICE_NET_STATE_WAITING_VOTEQUORUM_CMAP_EVENTS;
  662. return (0);
  663. }
  664. static int
  665. qdevice_net_msg_received_echo_request(struct qdevice_net_instance *instance,
  666. const struct msg_decoded *msg)
  667. {
  668. qdevice_net_log(LOG_ERR, "Received unexpected echo request message. "
  669. "Disconnecting from server");
  670. return (-1);
  671. }
  672. static int
  673. qdevice_net_msg_received_echo_reply(struct qdevice_net_instance *instance,
  674. const struct msg_decoded *msg)
  675. {
  676. if (qdevice_net_msg_check_echo_reply_seq_number(instance, msg) != 0) {
  677. return (-1);
  678. }
  679. instance->echo_reply_received_msg_seq_num = msg->seq_number;
  680. return (0);
  681. }
  682. static int
  683. qdevice_net_msg_received_node_list(struct qdevice_net_instance *instance,
  684. const struct msg_decoded *msg)
  685. {
  686. qdevice_net_log(LOG_ERR, "Received unexpected echo request message. "
  687. "Disconnecting from server");
  688. return (-1);
  689. }
  690. static int
  691. qdevice_net_msg_received_node_list_reply(struct qdevice_net_instance *instance,
  692. const struct msg_decoded *msg)
  693. {
  694. qdevice_net_log(LOG_INFO, "Received node list reply %u", msg->vote);
  695. return (0);
  696. }
  697. static int
  698. qdevice_net_msg_received(struct qdevice_net_instance *instance)
  699. {
  700. struct msg_decoded msg;
  701. int res;
  702. int ret_val;
  703. msg_decoded_init(&msg);
  704. res = msg_decode(&instance->receive_buffer, &msg);
  705. if (res != 0) {
  706. /*
  707. * Error occurred. Disconnect.
  708. */
  709. qdevice_net_log_msg_decode_error(res);
  710. qdevice_net_log(LOG_ERR, "Disconnecting from server");
  711. return (-1);
  712. }
  713. ret_val = 0;
  714. switch (msg.type) {
  715. case MSG_TYPE_PREINIT:
  716. ret_val = qdevice_net_msg_received_preinit(instance, &msg);
  717. break;
  718. case MSG_TYPE_PREINIT_REPLY:
  719. ret_val = qdevice_net_msg_received_preinit_reply(instance, &msg);
  720. break;
  721. case MSG_TYPE_STARTTLS:
  722. ret_val = qdevice_net_msg_received_starttls(instance, &msg);
  723. break;
  724. case MSG_TYPE_SERVER_ERROR:
  725. ret_val = qdevice_net_msg_received_server_error(instance, &msg);
  726. break;
  727. case MSG_TYPE_INIT_REPLY:
  728. ret_val = qdevice_net_msg_received_init_reply(instance, &msg);
  729. break;
  730. case MSG_TYPE_SET_OPTION:
  731. ret_val = qdevice_net_msg_received_set_option(instance, &msg);
  732. break;
  733. case MSG_TYPE_SET_OPTION_REPLY:
  734. ret_val = qdevice_net_msg_received_set_option_reply(instance, &msg);
  735. break;
  736. case MSG_TYPE_ECHO_REQUEST:
  737. ret_val = qdevice_net_msg_received_echo_request(instance, &msg);
  738. break;
  739. case MSG_TYPE_ECHO_REPLY:
  740. ret_val = qdevice_net_msg_received_echo_reply(instance, &msg);
  741. break;
  742. case MSG_TYPE_NODE_LIST:
  743. ret_val = qdevice_net_msg_received_node_list(instance, &msg);
  744. break;
  745. case MSG_TYPE_NODE_LIST_REPLY:
  746. ret_val = qdevice_net_msg_received_node_list_reply(instance, &msg);
  747. break;
  748. default:
  749. qdevice_net_log(LOG_ERR, "Received unsupported message %u. "
  750. "Disconnecting from server", msg.type);
  751. ret_val = -1;
  752. break;
  753. }
  754. msg_decoded_destroy(&msg);
  755. return (ret_val);
  756. }
  757. /*
  758. * -1 means end of connection (EOF) or some other unhandled error. 0 = success
  759. */
  760. static int
  761. qdevice_net_socket_read(struct qdevice_net_instance *instance)
  762. {
  763. int res;
  764. int ret_val;
  765. int orig_skipping_msg;
  766. orig_skipping_msg = instance->skipping_msg;
  767. res = msgio_read(instance->socket, &instance->receive_buffer,
  768. &instance->msg_already_received_bytes, &instance->skipping_msg);
  769. if (!orig_skipping_msg && instance->skipping_msg) {
  770. qdevice_net_log(LOG_DEBUG, "msgio_read set skipping_msg");
  771. }
  772. ret_val = 0;
  773. switch (res) {
  774. case 0:
  775. /*
  776. * Partial read
  777. */
  778. break;
  779. case -1:
  780. qdevice_net_log(LOG_DEBUG, "Server closed connection");
  781. ret_val = -1;
  782. break;
  783. case -2:
  784. qdevice_net_log_nss(LOG_ERR, "Unhandled error when reading from server. "
  785. "Disconnecting from server");
  786. ret_val = -1;
  787. break;
  788. case -3:
  789. qdevice_net_log(LOG_ERR, "Can't store message header from server. "
  790. "Disconnecting from server");
  791. ret_val = -1;
  792. break;
  793. case -4:
  794. qdevice_net_log(LOG_ERR, "Can't store message from server. "
  795. "Disconnecting from server");
  796. ret_val = -1;
  797. break;
  798. case -5:
  799. qdevice_net_log(LOG_WARNING, "Server sent unsupported msg type %u. "
  800. "Disconnecting from server", msg_get_type(&instance->receive_buffer));
  801. ret_val = -1;
  802. break;
  803. case -6:
  804. qdevice_net_log(LOG_WARNING,
  805. "Server wants to send too long message %u bytes. Disconnecting from server",
  806. msg_get_len(&instance->receive_buffer));
  807. ret_val = -1;
  808. break;
  809. case 1:
  810. /*
  811. * Full message received / skipped
  812. */
  813. if (!instance->skipping_msg) {
  814. if (qdevice_net_msg_received(instance) == -1) {
  815. ret_val = -1;
  816. }
  817. } else {
  818. errx(1, "net_socket_read in skipping msg state");
  819. }
  820. instance->skipping_msg = 0;
  821. instance->msg_already_received_bytes = 0;
  822. dynar_clean(&instance->receive_buffer);
  823. break;
  824. default:
  825. errx(1, "qdevice_net_socket_read unhandled error %d", res);
  826. break;
  827. }
  828. return (ret_val);
  829. }
  830. static int
  831. qdevice_net_socket_write_finished(struct qdevice_net_instance *instance)
  832. {
  833. PRFileDesc *new_pr_fd;
  834. if (instance->state == QDEVICE_NET_STATE_WAITING_STARTTLS_BEING_SENT) {
  835. /*
  836. * StartTLS sent to server. Begin with TLS handshake
  837. */
  838. if ((new_pr_fd = nss_sock_start_ssl_as_client(instance->socket, QNETD_NSS_SERVER_CN,
  839. qdevice_net_nss_bad_cert_hook,
  840. qdevice_net_nss_get_client_auth_data,
  841. (void *)QDEVICE_NET_NSS_CLIENT_CERT_NICKNAME, 0, NULL)) == NULL) {
  842. qdevice_net_log_nss(LOG_ERR, "Can't start TLS");
  843. return (-1);
  844. }
  845. /*
  846. * And send init msg
  847. */
  848. if (qdevice_net_send_init(instance) != 0) {
  849. return (-1);
  850. }
  851. instance->socket = new_pr_fd;
  852. }
  853. return (0);
  854. }
  855. static int
  856. qdevice_net_socket_write(struct qdevice_net_instance *instance)
  857. {
  858. int res;
  859. struct send_buffer_list_entry *send_buffer;
  860. enum msg_type sent_msg_type;
  861. send_buffer = send_buffer_list_get_active(&instance->send_buffer_list);
  862. if (send_buffer == NULL) {
  863. qdevice_net_log(LOG_CRIT, "send_buffer_list_get_active returned NULL");
  864. return (-1);
  865. }
  866. res = msgio_write(instance->socket, &send_buffer->buffer,
  867. &send_buffer->msg_already_sent_bytes);
  868. if (res == 1) {
  869. sent_msg_type = msg_get_type(&send_buffer->buffer);
  870. send_buffer_list_delete(&instance->send_buffer_list, send_buffer);
  871. if (sent_msg_type != MSG_TYPE_ECHO_REQUEST) {
  872. if (qdevice_net_socket_write_finished(instance) == -1) {
  873. return (-1);
  874. }
  875. }
  876. }
  877. if (res == -1) {
  878. qdevice_net_log_nss(LOG_CRIT, "PR_Send returned 0");
  879. return (-1);
  880. }
  881. if (res == -2) {
  882. qdevice_net_log_nss(LOG_ERR, "Unhandled error when sending message to server");
  883. return (-1);
  884. }
  885. return (0);
  886. }
  887. #define QDEVICE_NET_POLL_NO_FDS 2
  888. #define QDEVICE_NET_POLL_SOCKET 0
  889. #define QDEVICE_NET_POLL_VOTEQUORUM 1
  890. static int
  891. qdevice_net_poll(struct qdevice_net_instance *instance)
  892. {
  893. PRPollDesc pfds[QDEVICE_NET_POLL_NO_FDS];
  894. PRInt32 poll_res;
  895. int i;
  896. pfds[QDEVICE_NET_POLL_SOCKET].fd = instance->socket;
  897. pfds[QDEVICE_NET_POLL_SOCKET].in_flags = PR_POLL_READ;
  898. if (!send_buffer_list_empty(&instance->send_buffer_list)) {
  899. pfds[QDEVICE_NET_POLL_SOCKET].in_flags |= PR_POLL_WRITE;
  900. }
  901. pfds[QDEVICE_NET_POLL_VOTEQUORUM].fd = instance->votequorum_poll_fd;
  902. pfds[QDEVICE_NET_POLL_VOTEQUORUM].in_flags = PR_POLL_READ;
  903. instance->schedule_disconnect = 0;
  904. if ((poll_res = PR_Poll(pfds, QDEVICE_NET_POLL_NO_FDS,
  905. timer_list_time_to_expire(&instance->main_timer_list))) > 0) {
  906. for (i = 0; i < QDEVICE_NET_POLL_NO_FDS; i++) {
  907. if (pfds[i].out_flags & PR_POLL_READ) {
  908. switch (i) {
  909. case QDEVICE_NET_POLL_SOCKET:
  910. if (qdevice_net_socket_read(instance) == -1) {
  911. instance->schedule_disconnect = 1;
  912. }
  913. break;
  914. case QDEVICE_NET_POLL_VOTEQUORUM:
  915. if (votequorum_dispatch(instance->votequorum_handle,
  916. CS_DISPATCH_ALL) != CS_OK) {
  917. errx(1, "Can't dispatch votequorum messages");
  918. }
  919. break;
  920. default:
  921. errx(1, "Unhandled read poll descriptor %u", i);
  922. break;
  923. }
  924. }
  925. if (!instance->schedule_disconnect && pfds[i].out_flags & PR_POLL_WRITE) {
  926. switch (i) {
  927. case QDEVICE_NET_POLL_SOCKET:
  928. if (qdevice_net_socket_write(instance) == -1) {
  929. instance->schedule_disconnect = 1;
  930. }
  931. break;
  932. default:
  933. errx(1, "Unhandled write poll descriptor %u", i);
  934. break;
  935. }
  936. }
  937. if (!instance->schedule_disconnect &&
  938. pfds[i].out_flags &
  939. (PR_POLL_ERR|PR_POLL_NVAL|PR_POLL_HUP|PR_POLL_EXCEPT)) {
  940. switch (i) {
  941. case QDEVICE_NET_POLL_SOCKET:
  942. qdevice_net_log(LOG_CRIT, "POLL_ERR (%u) on main socket",
  943. pfds[i].out_flags);
  944. return (-1);
  945. break;
  946. default:
  947. errx(1, "Unhandled poll err on descriptor %u", i);
  948. break;
  949. }
  950. }
  951. }
  952. }
  953. if (!instance->schedule_disconnect) {
  954. timer_list_expire(&instance->main_timer_list);
  955. }
  956. if (instance->schedule_disconnect) {
  957. /*
  958. * Schedule disconnect can be set by this function, by some timer_list callback
  959. * or cmap/votequorum callbacks
  960. */
  961. return (-1);
  962. }
  963. return (0);
  964. }
  965. static int
  966. qdevice_net_instance_init(struct qdevice_net_instance *instance, size_t initial_receive_size,
  967. size_t initial_send_size, size_t min_send_size, size_t max_receive_size,
  968. enum tlv_tls_supported tls_supported, uint32_t node_id,
  969. enum tlv_decision_algorithm_type decision_algorithm, uint32_t heartbeat_interval,
  970. const char *host_addr, uint16_t host_port, const char *cluster_name)
  971. {
  972. memset(instance, 0, sizeof(*instance));
  973. instance->initial_receive_size = initial_receive_size;
  974. instance->initial_send_size = initial_send_size;
  975. instance->min_send_size = min_send_size;
  976. instance->max_receive_size = max_receive_size;
  977. instance->node_id = node_id;
  978. instance->decision_algorithm = decision_algorithm;
  979. instance->heartbeat_interval = heartbeat_interval;
  980. instance->host_addr = host_addr;
  981. instance->host_port = host_port;
  982. instance->cluster_name = cluster_name;
  983. dynar_init(&instance->receive_buffer, initial_receive_size);
  984. send_buffer_list_init(&instance->send_buffer_list, QDEVICE_NET_MAX_SEND_BUFFERS,
  985. initial_send_size);
  986. timer_list_init(&instance->main_timer_list);
  987. instance->tls_supported = tls_supported;
  988. return (0);
  989. }
  990. static int
  991. qdevice_net_instance_destroy(struct qdevice_net_instance *instance)
  992. {
  993. timer_list_free(&instance->main_timer_list);
  994. dynar_destroy(&instance->receive_buffer);
  995. send_buffer_list_free(&instance->send_buffer_list);
  996. /*
  997. * Close cmap and votequorum connections
  998. */
  999. if (votequorum_qdevice_unregister(instance->votequorum_handle,
  1000. QDEVICE_NET_VOTEQUORUM_DEVICE_NAME) != CS_OK) {
  1001. qdevice_net_log_nss(LOG_WARNING, "Unable to unregister votequorum device");
  1002. }
  1003. votequorum_finalize(instance->votequorum_handle);
  1004. cmap_finalize(instance->cmap_handle);
  1005. return (0);
  1006. }
  1007. static void
  1008. qdevice_net_init_cmap(cmap_handle_t *handle)
  1009. {
  1010. cs_error_t res;
  1011. int no_retries;
  1012. no_retries = 0;
  1013. while ((res = cmap_initialize(handle)) == CS_ERR_TRY_AGAIN &&
  1014. no_retries++ < MAX_CS_TRY_AGAIN) {
  1015. poll(NULL, 0, 1000);
  1016. }
  1017. if (res != CS_OK) {
  1018. errx(1, "Failed to initialize the cmap API. Error %s", cs_strerror(res));
  1019. }
  1020. }
  1021. /*
  1022. * Check string to value on, off, yes, no, 0, 1. Return 1 if value is on, yes or 1, 0 if
  1023. * value is off, no or 0 and -1 otherwise.
  1024. */
  1025. static int
  1026. qdevice_net_parse_bool_str(const char *str)
  1027. {
  1028. if (strcasecmp(str, "yes") == 0 ||
  1029. strcasecmp(str, "on") == 0 ||
  1030. strcasecmp(str, "1") == 0) {
  1031. return (1);
  1032. } else if (strcasecmp(str, "no") == 0 ||
  1033. strcasecmp(str, "off") == 0 ||
  1034. strcasecmp(str, "0") == 0) {
  1035. return (0);
  1036. }
  1037. return (-1);
  1038. }
  1039. static void
  1040. qdevice_net_instance_init_from_cmap(struct qdevice_net_instance *instance,
  1041. cmap_handle_t cmap_handle)
  1042. {
  1043. uint32_t node_id;
  1044. enum tlv_tls_supported tls_supported;
  1045. int i;
  1046. char *str;
  1047. enum tlv_decision_algorithm_type decision_algorithm;
  1048. uint32_t heartbeat_interval;
  1049. uint32_t sync_heartbeat_interval;
  1050. char *host_addr;
  1051. int host_port;
  1052. char *ep;
  1053. char *cluster_name;
  1054. /*
  1055. * Check if provider is net
  1056. */
  1057. if (cmap_get_string(cmap_handle, "quorum.device.model", &str) != CS_OK) {
  1058. errx(1, "Can't read quorum.device.model cmap key.");
  1059. }
  1060. if (strcmp(str, "net") != 0) {
  1061. free(str);
  1062. errx(1, "Configured device model is not net. "
  1063. "This qdevice provider is only for net.");
  1064. }
  1065. free(str);
  1066. /*
  1067. * Get nodeid
  1068. */
  1069. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &node_id) != CS_OK) {
  1070. errx(1, "Unable to retrive this node nodeid.");
  1071. }
  1072. /*
  1073. * Check tls
  1074. */
  1075. if (cmap_get_string(cmap_handle, "quorum.device.net.tls", &str) == CS_OK) {
  1076. if ((i = qdevice_net_parse_bool_str(str)) == -1) {
  1077. free(str);
  1078. errx(1, "quorum.device.net.tls value is not valid.");
  1079. }
  1080. if (i == 1) {
  1081. tls_supported = TLV_TLS_SUPPORTED;
  1082. } else {
  1083. tls_supported = TLV_TLS_UNSUPPORTED;
  1084. }
  1085. free(str);
  1086. }
  1087. /*
  1088. * Host
  1089. */
  1090. if (cmap_get_string(cmap_handle, "quorum.device.net.host", &str) != CS_OK) {
  1091. free(str);
  1092. errx(1, "Qdevice net daemon address is not defined (quorum.device.net.host)");
  1093. }
  1094. host_addr = str;
  1095. if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
  1096. host_port = strtol(str, &ep, 10);
  1097. if (host_port <= 0 || host_port > ((uint16_t)~0) || *ep != '\0') {
  1098. errx(1, "quorum.device.net.port must be in range 0-65535");
  1099. }
  1100. free(str);
  1101. } else {
  1102. host_port = QNETD_DEFAULT_HOST_PORT;
  1103. }
  1104. /*
  1105. * Cluster name
  1106. */
  1107. if (cmap_get_string(cmap_handle, "totem.cluster_name", &str) != CS_OK) {
  1108. errx(1, "Cluster name (totem.cluster_name) has to be defined.");
  1109. }
  1110. cluster_name = str;
  1111. /*
  1112. * Configure timeouts
  1113. */
  1114. if (cmap_get_uint32(cmap_handle, "quorum.device.timeout", &heartbeat_interval) != CS_OK) {
  1115. heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  1116. }
  1117. heartbeat_interval = heartbeat_interval * 0.8;
  1118. if (cmap_get_uint32(cmap_handle, "quorum.device.sync_timeout",
  1119. &sync_heartbeat_interval) != CS_OK) {
  1120. sync_heartbeat_interval = VOTEQUORUM_QDEVICE_DEFAULT_SYNC_TIMEOUT;
  1121. }
  1122. sync_heartbeat_interval = sync_heartbeat_interval * 0.8;
  1123. /*
  1124. * Choose decision algorithm
  1125. */
  1126. decision_algorithm = TLV_DECISION_ALGORITHM_TYPE_TEST;
  1127. /*
  1128. * Really initialize instance
  1129. */
  1130. if (qdevice_net_instance_init(instance,
  1131. QDEVICE_NET_INITIAL_MSG_RECEIVE_SIZE, QDEVICE_NET_INITIAL_MSG_SEND_SIZE,
  1132. QDEVICE_NET_MIN_MSG_SEND_SIZE, QDEVICE_NET_MAX_MSG_RECEIVE_SIZE,
  1133. tls_supported, node_id, decision_algorithm,
  1134. heartbeat_interval,
  1135. host_addr, host_port, cluster_name) == -1) {
  1136. errx(1, "Can't initialize qdevice-net");
  1137. }
  1138. instance->cmap_handle = cmap_handle;
  1139. }
  1140. static enum tlv_node_state
  1141. qdevice_net_convert_votequorum_to_tlv_node_state(uint32_t votequorum_node_state)
  1142. {
  1143. enum tlv_node_state res;
  1144. switch (votequorum_node_state) {
  1145. case VOTEQUORUM_NODESTATE_MEMBER: res = TLV_NODE_STATE_MEMBER; break;
  1146. case VOTEQUORUM_NODESTATE_DEAD: res = TLV_NODE_STATE_DEAD; break;
  1147. case VOTEQUORUM_NODESTATE_LEAVING: res = TLV_NODE_STATE_LEAVING; break;
  1148. default:
  1149. errx(1, "qdevice_net_convert_votequorum_to_tlv_node_state: Unhandled votequorum "
  1150. "node state %"PRIu32, votequorum_node_state);
  1151. break;
  1152. }
  1153. return (res);
  1154. }
  1155. static int
  1156. qdevice_net_send_membership_node_list(struct qdevice_net_instance *instance,
  1157. enum tlv_quorate quorate, const struct tlv_ring_id *ring_id,
  1158. uint32_t node_list_entries, votequorum_node_t node_list[])
  1159. {
  1160. struct node_list nlist;
  1161. struct send_buffer_list_entry *send_buffer;
  1162. uint64_t config_version;
  1163. int send_config_version;
  1164. uint32_t i;
  1165. node_list_init(&nlist);
  1166. for (i = 0; i < node_list_entries; i++) {
  1167. if (node_list[i].nodeid == 0) {
  1168. continue;
  1169. }
  1170. if (node_list_add(&nlist, node_list[i].nodeid, 0,
  1171. qdevice_net_convert_votequorum_to_tlv_node_state(node_list[i].state)) == NULL) {
  1172. qdevice_net_log(LOG_ERR, "Can't allocate membership node list.");
  1173. node_list_free(&nlist);
  1174. return (-1);
  1175. }
  1176. }
  1177. send_buffer = send_buffer_list_get_new(&instance->send_buffer_list);
  1178. if (send_buffer == NULL) {
  1179. qdevice_net_log(LOG_ERR, "Can't allocate send list buffer for config "
  1180. "node list msg");
  1181. node_list_free(&nlist);
  1182. return (-1);
  1183. }
  1184. instance->last_msg_seq_num++;
  1185. send_config_version = qdevice_net_get_cmap_config_version(instance->cmap_handle,
  1186. &config_version);
  1187. if (msg_create_node_list(&send_buffer->buffer, instance->last_msg_seq_num,
  1188. TLV_NODE_LIST_TYPE_MEMBERSHIP,
  1189. 1, ring_id, send_config_version, config_version, 1, quorate, &nlist) == 0) {
  1190. qdevice_net_log(LOG_ERR, "Can't allocate send buffer for config list msg");
  1191. node_list_free(&nlist);
  1192. return (-1);
  1193. }
  1194. send_buffer_list_put(&instance->send_buffer_list, send_buffer);
  1195. return (0);
  1196. }
  1197. static void
  1198. qdevice_net_convert_votequorum_to_tlv_ring_id(struct tlv_ring_id *tlv_rid,
  1199. const votequorum_ring_id_t *votequorum_rid)
  1200. {
  1201. tlv_rid->node_id = votequorum_rid->nodeid;
  1202. tlv_rid->seq = votequorum_rid->seq;
  1203. }
  1204. static void
  1205. qdevice_net_votequorum_notification(votequorum_handle_t votequorum_handle,
  1206. uint64_t context, uint32_t quorate,
  1207. votequorum_ring_id_t votequorum_ring_id,
  1208. uint32_t node_list_entries, votequorum_node_t node_list[])
  1209. {
  1210. struct qdevice_net_instance *instance;
  1211. struct tlv_ring_id ring_id;
  1212. if (votequorum_context_get(votequorum_handle, (void **)&instance) != CS_OK) {
  1213. errx(1, "Fatal error. Can't get votequorum context");
  1214. }
  1215. qdevice_net_convert_votequorum_to_tlv_ring_id(&ring_id, &votequorum_ring_id);
  1216. if (qdevice_net_send_membership_node_list(instance,
  1217. (quorate ? TLV_QUORATE_QUORATE : TLV_QUORATE_INQUORATE),
  1218. &ring_id, node_list_entries, node_list) != 0) {
  1219. /*
  1220. * Fatal error -> schedule disconnect
  1221. */
  1222. instance->schedule_disconnect = 1;
  1223. }
  1224. memcpy(&global_last_received_ring_id, &ring_id, sizeof(ring_id));
  1225. }
  1226. static void
  1227. qdevice_net_init_votequorum(struct qdevice_net_instance *instance)
  1228. {
  1229. votequorum_callbacks_t votequorum_callbacks;
  1230. votequorum_handle_t votequorum_handle;
  1231. cs_error_t res;
  1232. int no_retries;
  1233. int fd;
  1234. memset(&votequorum_callbacks, 0, sizeof(votequorum_callbacks));
  1235. votequorum_callbacks.votequorum_notify_fn = qdevice_net_votequorum_notification;
  1236. no_retries = 0;
  1237. while ((res = votequorum_initialize(&votequorum_handle,
  1238. &votequorum_callbacks)) == CS_ERR_TRY_AGAIN && no_retries++ < MAX_CS_TRY_AGAIN) {
  1239. poll(NULL, 0, 1000);
  1240. }
  1241. if (res != CS_OK) {
  1242. errx(1, "Failed to initialize the votequorum API. Error %s", cs_strerror(res));
  1243. }
  1244. if ((res = votequorum_qdevice_register(votequorum_handle,
  1245. QDEVICE_NET_VOTEQUORUM_DEVICE_NAME)) != CS_OK) {
  1246. errx(1, "Can't register votequorum device. Error %s", cs_strerror(res));
  1247. }
  1248. if ((res = votequorum_context_set(votequorum_handle, (void *)instance)) != CS_OK) {
  1249. errx(1, "Can't set votequorum context. Error %s", cs_strerror(res));
  1250. }
  1251. instance->votequorum_handle = votequorum_handle;
  1252. votequorum_fd_get(votequorum_handle, &fd);
  1253. if ((instance->votequorum_poll_fd = PR_CreateSocketPollFd(fd)) == NULL) {
  1254. err_nss();
  1255. }
  1256. }
  1257. int
  1258. main(void)
  1259. {
  1260. struct qdevice_net_instance instance;
  1261. cmap_handle_t cmap_handle;
  1262. struct send_buffer_list_entry *send_buffer;
  1263. /*
  1264. * Init
  1265. */
  1266. qdevice_net_init_cmap(&cmap_handle);
  1267. qdevice_net_instance_init_from_cmap(&instance, cmap_handle);
  1268. qdevice_net_log_init(QDEVICE_NET_LOG_TARGET_STDERR);
  1269. qdevice_net_log_set_debug(1);
  1270. if (nss_sock_init_nss((instance.tls_supported != TLV_TLS_UNSUPPORTED ?
  1271. (char *)NSS_DB_DIR : NULL)) != 0) {
  1272. err_nss();
  1273. }
  1274. /*
  1275. * Try to connect to qnetd host
  1276. */
  1277. instance.socket = nss_sock_create_client_socket(instance.host_addr, instance.host_port,
  1278. PR_AF_UNSPEC, 100);
  1279. if (instance.socket == NULL) {
  1280. err_nss();
  1281. }
  1282. if (nss_sock_set_nonblocking(instance.socket) != 0) {
  1283. err_nss();
  1284. }
  1285. qdevice_net_init_votequorum(&instance);
  1286. /*
  1287. * Create and schedule send of preinit message to qnetd
  1288. */
  1289. send_buffer = send_buffer_list_get_new(&instance.send_buffer_list);
  1290. if (send_buffer == NULL) {
  1291. errx(1, "Can't allocate send buffer list");
  1292. }
  1293. instance.last_msg_seq_num = 1;
  1294. if (msg_create_preinit(&send_buffer->buffer, instance.cluster_name, 1,
  1295. instance.last_msg_seq_num) == 0) {
  1296. errx(1, "Can't allocate buffer");
  1297. }
  1298. send_buffer_list_put(&instance.send_buffer_list, send_buffer);
  1299. instance.state = QDEVICE_NET_STATE_WAITING_PREINIT_REPLY;
  1300. /*
  1301. * Main loop
  1302. */
  1303. while (qdevice_net_poll(&instance) == 0) {
  1304. }
  1305. /*
  1306. * Cleanup
  1307. */
  1308. if (PR_Close(instance.socket) != PR_SUCCESS) {
  1309. err_nss();
  1310. }
  1311. qdevice_net_instance_destroy(&instance);
  1312. SSL_ClearSessionCache();
  1313. if (NSS_Shutdown() != SECSuccess) {
  1314. err_nss();
  1315. }
  1316. PR_Cleanup();
  1317. qdevice_net_log_close();
  1318. return (0);
  1319. }