corosync-qdevice-net.c 42 KB

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