corosync-qdevice-net.c 46 KB

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