totemudp.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  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 MontaVista Software, 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 <assert.h>
  36. #include <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <sys/uio.h>
  57. #include <limits.h>
  58. #include <corosync/sq.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/list.h>
  61. #include <qb/qbdefs.h>
  62. #include <qb/qbloop.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/logsys.h>
  65. #include "totemudp.h"
  66. #include "util.h"
  67. #include "totemcrypto.h"
  68. #include <nss.h>
  69. #include <pk11pub.h>
  70. #include <pkcs11.h>
  71. #include <prerror.h>
  72. #ifndef MSG_NOSIGNAL
  73. #define MSG_NOSIGNAL 0
  74. #endif
  75. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  76. #define NETIF_STATE_REPORT_UP 1
  77. #define NETIF_STATE_REPORT_DOWN 2
  78. #define BIND_STATE_UNBOUND 0
  79. #define BIND_STATE_REGULAR 1
  80. #define BIND_STATE_LOOPBACK 2
  81. struct totemudp_socket {
  82. int mcast_recv;
  83. int mcast_send;
  84. int token;
  85. /*
  86. * Socket used for local multicast delivery. We don't rely on multicast
  87. * loop and rather this UNIX DGRAM socket is used. Socket is created by
  88. * socketpair call and they are used in same way as pipe (so [0] is read
  89. * end and [1] is write end)
  90. */
  91. int local_mcast_loop[2];
  92. };
  93. struct totemudp_instance {
  94. struct crypto_instance *crypto_inst;
  95. qb_loop_t *totemudp_poll_handle;
  96. struct totem_interface *totem_interface;
  97. int netif_state_report;
  98. int netif_bind_state;
  99. void *context;
  100. void (*totemudp_deliver_fn) (
  101. void *context,
  102. const void *msg,
  103. unsigned int msg_len);
  104. void (*totemudp_iface_change_fn) (
  105. void *context,
  106. const struct totem_ip_address *iface_address);
  107. void (*totemudp_target_set_completed) (void *context);
  108. /*
  109. * Function and data used to log messages
  110. */
  111. int totemudp_log_level_security;
  112. int totemudp_log_level_error;
  113. int totemudp_log_level_warning;
  114. int totemudp_log_level_notice;
  115. int totemudp_log_level_debug;
  116. int totemudp_subsys_id;
  117. void (*totemudp_log_printf) (
  118. int level,
  119. int subsys,
  120. const char *function,
  121. const char *file,
  122. int line,
  123. const char *format,
  124. ...)__attribute__((format(printf, 6, 7)));
  125. void *udp_context;
  126. char iov_buffer[FRAME_SIZE_MAX];
  127. char iov_buffer_flush[FRAME_SIZE_MAX];
  128. struct iovec totemudp_iov_recv;
  129. struct iovec totemudp_iov_recv_flush;
  130. struct totemudp_socket totemudp_sockets;
  131. struct totem_ip_address mcast_address;
  132. int stats_sent;
  133. int stats_recv;
  134. int stats_delv;
  135. int stats_remcasts;
  136. int stats_orf_token;
  137. struct timeval stats_tv_start;
  138. struct totem_ip_address my_id;
  139. int firstrun;
  140. qb_loop_timer_handle timer_netif_check_timeout;
  141. unsigned int my_memb_entries;
  142. int flushing;
  143. struct totem_config *totem_config;
  144. totemsrp_stats_t *stats;
  145. struct totem_ip_address token_target;
  146. };
  147. struct work_item {
  148. const void *msg;
  149. unsigned int msg_len;
  150. struct totemudp_instance *instance;
  151. };
  152. static int totemudp_build_sockets (
  153. struct totemudp_instance *instance,
  154. struct totem_ip_address *bindnet_address,
  155. struct totem_ip_address *mcastaddress,
  156. struct totemudp_socket *sockets,
  157. struct totem_ip_address *bound_to);
  158. static struct totem_ip_address localhost;
  159. static void totemudp_instance_initialize (struct totemudp_instance *instance)
  160. {
  161. memset (instance, 0, sizeof (struct totemudp_instance));
  162. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  163. instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
  164. instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  165. instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  166. instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  167. /*
  168. * There is always atleast 1 processor
  169. */
  170. instance->my_memb_entries = 1;
  171. }
  172. #define log_printf(level, format, args...) \
  173. do { \
  174. instance->totemudp_log_printf ( \
  175. level, instance->totemudp_subsys_id, \
  176. __FUNCTION__, __FILE__, __LINE__, \
  177. (const char *)format, ##args); \
  178. } while (0);
  179. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  180. do { \
  181. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  182. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  183. instance->totemudp_log_printf ( \
  184. level, instance->totemudp_subsys_id, \
  185. __FUNCTION__, __FILE__, __LINE__, \
  186. fmt ": %s (%d)\n", ##args, _error_ptr, err_num); \
  187. } while(0)
  188. int totemudp_crypto_set (
  189. void *udp_context,
  190. const char *cipher_type,
  191. const char *hash_type)
  192. {
  193. return (0);
  194. }
  195. static inline void ucast_sendmsg (
  196. struct totemudp_instance *instance,
  197. struct totem_ip_address *system_to,
  198. const void *msg,
  199. unsigned int msg_len)
  200. {
  201. struct msghdr msg_ucast;
  202. int res = 0;
  203. size_t buf_out_len;
  204. unsigned char buf_out[FRAME_SIZE_MAX];
  205. struct sockaddr_storage sockaddr;
  206. struct iovec iovec;
  207. int addrlen;
  208. /*
  209. * Encrypt and digest the message
  210. */
  211. if (crypto_encrypt_and_sign (
  212. instance->crypto_inst,
  213. (const unsigned char *)msg,
  214. msg_len,
  215. buf_out,
  216. &buf_out_len) != 0) {
  217. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  218. return;
  219. }
  220. iovec.iov_base = (void *)buf_out;
  221. iovec.iov_len = buf_out_len;
  222. /*
  223. * Build unicast message
  224. */
  225. memset(&msg_ucast, 0, sizeof(msg_ucast));
  226. totemip_totemip_to_sockaddr_convert(system_to,
  227. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  228. msg_ucast.msg_name = &sockaddr;
  229. msg_ucast.msg_namelen = addrlen;
  230. msg_ucast.msg_iov = (void *)&iovec;
  231. msg_ucast.msg_iovlen = 1;
  232. #ifdef HAVE_MSGHDR_CONTROL
  233. msg_ucast.msg_control = 0;
  234. #endif
  235. #ifdef HAVE_MSGHDR_CONTROLLEN
  236. msg_ucast.msg_controllen = 0;
  237. #endif
  238. #ifdef HAVE_MSGHDR_FLAGS
  239. msg_ucast.msg_flags = 0;
  240. #endif
  241. #ifdef HAVE_MSGHDR_ACCRIGHTS
  242. msg_ucast.msg_accrights = NULL;
  243. #endif
  244. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  245. msg_ucast.msg_accrightslen = 0;
  246. #endif
  247. /*
  248. * Transmit unicast message
  249. * An error here is recovered by totemsrp
  250. */
  251. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  252. MSG_NOSIGNAL);
  253. if (res < 0) {
  254. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  255. "sendmsg(ucast) failed (non-critical)");
  256. }
  257. }
  258. static inline void mcast_sendmsg (
  259. struct totemudp_instance *instance,
  260. const void *msg,
  261. unsigned int msg_len)
  262. {
  263. struct msghdr msg_mcast;
  264. int res = 0;
  265. size_t buf_out_len;
  266. unsigned char buf_out[FRAME_SIZE_MAX];
  267. struct iovec iovec;
  268. struct sockaddr_storage sockaddr;
  269. int addrlen;
  270. /*
  271. * Encrypt and digest the message
  272. */
  273. if (crypto_encrypt_and_sign (
  274. instance->crypto_inst,
  275. (const unsigned char *)msg,
  276. msg_len,
  277. buf_out,
  278. &buf_out_len) != 0) {
  279. log_printf(LOGSYS_LEVEL_CRIT, "Error encrypting/signing packet (non-critical)");
  280. return;
  281. }
  282. iovec.iov_base = (void *)&buf_out;
  283. iovec.iov_len = buf_out_len;
  284. /*
  285. * Build multicast message
  286. */
  287. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  288. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  289. memset(&msg_mcast, 0, sizeof(msg_mcast));
  290. msg_mcast.msg_name = &sockaddr;
  291. msg_mcast.msg_namelen = addrlen;
  292. msg_mcast.msg_iov = (void *)&iovec;
  293. msg_mcast.msg_iovlen = 1;
  294. #ifdef HAVE_MSGHDR_CONTROL
  295. msg_mcast.msg_control = 0;
  296. #endif
  297. #ifdef HAVE_MSGHDR_CONTROLLEN
  298. msg_mcast.msg_controllen = 0;
  299. #endif
  300. #ifdef HAVE_MSGHDR_FLAGS
  301. msg_mcast.msg_flags = 0;
  302. #endif
  303. #ifdef HAVE_MSGHDR_ACCRIGHTS
  304. msg_mcast.msg_accrights = NULL;
  305. #endif
  306. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  307. msg_mcast.msg_accrightslen = 0;
  308. #endif
  309. /*
  310. * Transmit multicast message
  311. * An error here is recovered by totemsrp
  312. */
  313. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  314. MSG_NOSIGNAL);
  315. if (res < 0) {
  316. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  317. "sendmsg(mcast) failed (non-critical)");
  318. instance->stats->continuous_sendmsg_failures++;
  319. } else {
  320. instance->stats->continuous_sendmsg_failures = 0;
  321. }
  322. /*
  323. * Transmit multicast message to local unix mcast loop
  324. * An error here is recovered by totemsrp
  325. */
  326. msg_mcast.msg_name = NULL;
  327. msg_mcast.msg_namelen = 0;
  328. res = sendmsg (instance->totemudp_sockets.local_mcast_loop[1], &msg_mcast,
  329. MSG_NOSIGNAL);
  330. if (res < 0) {
  331. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  332. "sendmsg(local mcast loop) failed (non-critical)");
  333. }
  334. }
  335. int totemudp_finalize (
  336. void *udp_context)
  337. {
  338. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  339. int res = 0;
  340. if (instance->totemudp_sockets.mcast_recv > 0) {
  341. qb_loop_poll_del (instance->totemudp_poll_handle,
  342. instance->totemudp_sockets.mcast_recv);
  343. close (instance->totemudp_sockets.mcast_recv);
  344. }
  345. if (instance->totemudp_sockets.mcast_send > 0) {
  346. close (instance->totemudp_sockets.mcast_send);
  347. }
  348. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  349. qb_loop_poll_del (instance->totemudp_poll_handle,
  350. instance->totemudp_sockets.local_mcast_loop[0]);
  351. close (instance->totemudp_sockets.local_mcast_loop[0]);
  352. close (instance->totemudp_sockets.local_mcast_loop[1]);
  353. }
  354. if (instance->totemudp_sockets.token > 0) {
  355. qb_loop_poll_del (instance->totemudp_poll_handle,
  356. instance->totemudp_sockets.token);
  357. close (instance->totemudp_sockets.token);
  358. }
  359. return (res);
  360. }
  361. /*
  362. * Only designed to work with a message with one iov
  363. */
  364. static int net_deliver_fn (
  365. int fd,
  366. int revents,
  367. void *data)
  368. {
  369. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  370. struct msghdr msg_recv;
  371. struct iovec *iovec;
  372. struct sockaddr_storage system_from;
  373. int bytes_received;
  374. int res = 0;
  375. int truncated_packet;
  376. if (instance->flushing == 1) {
  377. iovec = &instance->totemudp_iov_recv_flush;
  378. } else {
  379. iovec = &instance->totemudp_iov_recv;
  380. }
  381. /*
  382. * Receive datagram
  383. */
  384. msg_recv.msg_name = &system_from;
  385. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  386. msg_recv.msg_iov = iovec;
  387. msg_recv.msg_iovlen = 1;
  388. #ifdef HAVE_MSGHDR_CONTROL
  389. msg_recv.msg_control = 0;
  390. #endif
  391. #ifdef HAVE_MSGHDR_CONTROLLEN
  392. msg_recv.msg_controllen = 0;
  393. #endif
  394. #ifdef HAVE_MSGHDR_FLAGS
  395. msg_recv.msg_flags = 0;
  396. #endif
  397. #ifdef HAVE_MSGHDR_ACCRIGHTS
  398. msg_recv.msg_accrights = NULL;
  399. #endif
  400. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  401. msg_recv.msg_accrightslen = 0;
  402. #endif
  403. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  404. if (bytes_received == -1) {
  405. return (0);
  406. } else {
  407. instance->stats_recv += bytes_received;
  408. }
  409. truncated_packet = 0;
  410. #ifdef HAVE_MSGHDR_FLAGS
  411. if (msg_recv.msg_flags & MSG_TRUNC) {
  412. truncated_packet = 1;
  413. }
  414. #else
  415. /*
  416. * We don't have MSGHDR_FLAGS, but we can (hopefully) safely make assumption that
  417. * if bytes_received == FRAME_SIZE_MAX then packet is truncated
  418. */
  419. if (bytes_received == FRAME_SIZE_MAX) {
  420. truncated_packet = 1;
  421. }
  422. #endif
  423. if (truncated_packet) {
  424. log_printf(instance->totemudp_log_level_error,
  425. "Received too big message. This may be because something bad is happening"
  426. "on the network (attack?), or you tried join more nodes than corosync is"
  427. "compiled with (%u) or bug in the code (bad estimation of "
  428. "the FRAME_SIZE_MAX). Dropping packet.", PROCESSOR_COUNT_MAX);
  429. return (0);
  430. }
  431. /*
  432. * Authenticate and if authenticated, decrypt datagram
  433. */
  434. res = crypto_authenticate_and_decrypt (instance->crypto_inst, iovec->iov_base, &bytes_received);
  435. if (res == -1) {
  436. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.");
  437. log_printf (instance->totemudp_log_level_security,
  438. "Invalid packet data");
  439. iovec->iov_len = FRAME_SIZE_MAX;
  440. return 0;
  441. }
  442. iovec->iov_len = bytes_received;
  443. /*
  444. * Handle incoming message
  445. */
  446. instance->totemudp_deliver_fn (
  447. instance->context,
  448. iovec->iov_base,
  449. iovec->iov_len);
  450. iovec->iov_len = FRAME_SIZE_MAX;
  451. return (0);
  452. }
  453. static int netif_determine (
  454. struct totemudp_instance *instance,
  455. struct totem_ip_address *bindnet,
  456. struct totem_ip_address *bound_to,
  457. int *interface_up,
  458. int *interface_num)
  459. {
  460. int res;
  461. res = totemip_iface_check (bindnet, bound_to,
  462. interface_up, interface_num,
  463. instance->totem_config->clear_node_high_bit);
  464. return (res);
  465. }
  466. /*
  467. * If the interface is up, the sockets for totem are built. If the interface is down
  468. * this function is requeued in the timer list to retry building the sockets later.
  469. */
  470. static void timer_function_netif_check_timeout (
  471. void *data)
  472. {
  473. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  474. int interface_up;
  475. int interface_num;
  476. struct totem_ip_address *bind_address;
  477. /*
  478. * Build sockets for every interface
  479. */
  480. netif_determine (instance,
  481. &instance->totem_interface->bindnet,
  482. &instance->totem_interface->boundto,
  483. &interface_up, &interface_num);
  484. /*
  485. * If the network interface isn't back up and we are already
  486. * in loopback mode, add timer to check again and return
  487. */
  488. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  489. interface_up == 0) ||
  490. (instance->my_memb_entries == 1 &&
  491. instance->netif_bind_state == BIND_STATE_REGULAR &&
  492. interface_up == 1)) {
  493. qb_loop_timer_add (instance->totemudp_poll_handle,
  494. QB_LOOP_MED,
  495. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  496. (void *)instance,
  497. timer_function_netif_check_timeout,
  498. &instance->timer_netif_check_timeout);
  499. /*
  500. * Add a timer to check for a downed regular interface
  501. */
  502. return;
  503. }
  504. if (instance->totemudp_sockets.mcast_recv > 0) {
  505. qb_loop_poll_del (instance->totemudp_poll_handle,
  506. instance->totemudp_sockets.mcast_recv);
  507. close (instance->totemudp_sockets.mcast_recv);
  508. }
  509. if (instance->totemudp_sockets.mcast_send > 0) {
  510. close (instance->totemudp_sockets.mcast_send);
  511. }
  512. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  513. qb_loop_poll_del (instance->totemudp_poll_handle,
  514. instance->totemudp_sockets.local_mcast_loop[0]);
  515. close (instance->totemudp_sockets.local_mcast_loop[0]);
  516. close (instance->totemudp_sockets.local_mcast_loop[1]);
  517. }
  518. if (instance->totemudp_sockets.token > 0) {
  519. qb_loop_poll_del (instance->totemudp_poll_handle,
  520. instance->totemudp_sockets.token);
  521. close (instance->totemudp_sockets.token);
  522. }
  523. if (interface_up == 0) {
  524. /*
  525. * Interface is not up
  526. */
  527. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  528. bind_address = &localhost;
  529. /*
  530. * Add a timer to retry building interfaces and request memb_gather_enter
  531. */
  532. qb_loop_timer_add (instance->totemudp_poll_handle,
  533. QB_LOOP_MED,
  534. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  535. (void *)instance,
  536. timer_function_netif_check_timeout,
  537. &instance->timer_netif_check_timeout);
  538. } else {
  539. /*
  540. * Interface is up
  541. */
  542. instance->netif_bind_state = BIND_STATE_REGULAR;
  543. bind_address = &instance->totem_interface->bindnet;
  544. }
  545. /*
  546. * Create and bind the multicast and unicast sockets
  547. */
  548. (void)totemudp_build_sockets (instance,
  549. &instance->mcast_address,
  550. bind_address,
  551. &instance->totemudp_sockets,
  552. &instance->totem_interface->boundto);
  553. qb_loop_poll_add (
  554. instance->totemudp_poll_handle,
  555. QB_LOOP_MED,
  556. instance->totemudp_sockets.mcast_recv,
  557. POLLIN, instance, net_deliver_fn);
  558. qb_loop_poll_add (
  559. instance->totemudp_poll_handle,
  560. QB_LOOP_MED,
  561. instance->totemudp_sockets.local_mcast_loop[0],
  562. POLLIN, instance, net_deliver_fn);
  563. qb_loop_poll_add (
  564. instance->totemudp_poll_handle,
  565. QB_LOOP_MED,
  566. instance->totemudp_sockets.token,
  567. POLLIN, instance, net_deliver_fn);
  568. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  569. /*
  570. * This reports changes in the interface to the user and totemsrp
  571. */
  572. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  573. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  574. log_printf (instance->totemudp_log_level_notice,
  575. "The network interface [%s] is now up.",
  576. totemip_print (&instance->totem_interface->boundto));
  577. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  578. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  579. }
  580. /*
  581. * Add a timer to check for interface going down in single membership
  582. */
  583. if (instance->my_memb_entries == 1) {
  584. qb_loop_timer_add (instance->totemudp_poll_handle,
  585. QB_LOOP_MED,
  586. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  587. (void *)instance,
  588. timer_function_netif_check_timeout,
  589. &instance->timer_netif_check_timeout);
  590. }
  591. } else {
  592. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  593. log_printf (instance->totemudp_log_level_notice,
  594. "The network interface is down.");
  595. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  596. }
  597. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  598. }
  599. }
  600. /* Set the socket priority to INTERACTIVE to ensure
  601. that our messages don't get queued behind anything else */
  602. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  603. {
  604. #ifdef SO_PRIORITY
  605. int prio = 6; /* TC_PRIO_INTERACTIVE */
  606. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  607. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  608. }
  609. #endif
  610. }
  611. static int totemudp_build_sockets_ip (
  612. struct totemudp_instance *instance,
  613. struct totem_ip_address *mcast_address,
  614. struct totem_ip_address *bindnet_address,
  615. struct totemudp_socket *sockets,
  616. struct totem_ip_address *bound_to,
  617. int interface_num)
  618. {
  619. struct sockaddr_storage sockaddr;
  620. struct ipv6_mreq mreq6;
  621. struct ip_mreq mreq;
  622. struct sockaddr_storage mcast_ss, boundto_ss;
  623. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  624. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  625. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  626. unsigned int sendbuf_size;
  627. unsigned int recvbuf_size;
  628. unsigned int optlen = sizeof (sendbuf_size);
  629. unsigned int retries;
  630. int addrlen;
  631. int res;
  632. int flag;
  633. uint8_t sflag;
  634. int i;
  635. /*
  636. * Create multicast recv socket
  637. */
  638. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  639. if (sockets->mcast_recv == -1) {
  640. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  641. "socket() failed");
  642. return (-1);
  643. }
  644. totemip_nosigpipe (sockets->mcast_recv);
  645. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  646. if (res == -1) {
  647. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  648. "Could not set non-blocking operation on multicast socket");
  649. return (-1);
  650. }
  651. /*
  652. * Force reuse
  653. */
  654. flag = 1;
  655. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  656. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  657. "setsockopt(SO_REUSEADDR) failed");
  658. return (-1);
  659. }
  660. /*
  661. * Create local multicast loop socket
  662. */
  663. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets->local_mcast_loop) == -1) {
  664. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  665. "socket() failed");
  666. return (-1);
  667. }
  668. for (i = 0; i < 2; i++) {
  669. totemip_nosigpipe (sockets->local_mcast_loop[i]);
  670. res = fcntl (sockets->local_mcast_loop[i], F_SETFL, O_NONBLOCK);
  671. if (res == -1) {
  672. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  673. "Could not set non-blocking operation on multicast socket");
  674. return (-1);
  675. }
  676. }
  677. /*
  678. * Setup mcast send socket
  679. */
  680. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  681. if (sockets->mcast_send == -1) {
  682. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  683. "socket() failed");
  684. return (-1);
  685. }
  686. totemip_nosigpipe (sockets->mcast_send);
  687. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  688. if (res == -1) {
  689. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  690. "Could not set non-blocking operation on multicast socket");
  691. return (-1);
  692. }
  693. /*
  694. * Force reuse
  695. */
  696. flag = 1;
  697. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  698. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  699. "setsockopt(SO_REUSEADDR) failed");
  700. return (-1);
  701. }
  702. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  703. &sockaddr, &addrlen);
  704. retries = 0;
  705. while (1) {
  706. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  707. if (res == 0) {
  708. break;
  709. }
  710. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  711. "Unable to bind the socket to send multicast packets");
  712. if (++retries > BIND_MAX_RETRIES) {
  713. break;
  714. }
  715. /*
  716. * Wait for a while
  717. */
  718. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  719. }
  720. if (res == -1) {
  721. return (-1);
  722. }
  723. /*
  724. * Setup unicast socket
  725. */
  726. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  727. if (sockets->token == -1) {
  728. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  729. "socket() failed");
  730. return (-1);
  731. }
  732. totemip_nosigpipe (sockets->token);
  733. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  734. if (res == -1) {
  735. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  736. "Could not set non-blocking operation on token socket");
  737. return (-1);
  738. }
  739. /*
  740. * Force reuse
  741. */
  742. flag = 1;
  743. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  744. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  745. "setsockopt(SO_REUSEADDR) failed");
  746. return (-1);
  747. }
  748. /*
  749. * Bind to unicast socket used for token send/receives
  750. * This has the side effect of binding to the correct interface
  751. */
  752. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  753. retries = 0;
  754. while (1) {
  755. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  756. if (res == 0) {
  757. break;
  758. }
  759. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  760. "Unable to bind UDP unicast socket");
  761. if (++retries > BIND_MAX_RETRIES) {
  762. break;
  763. }
  764. /*
  765. * Wait for a while
  766. */
  767. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  768. }
  769. if (res == -1) {
  770. return (-1);
  771. }
  772. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  773. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  774. /*
  775. * Set buffer sizes to avoid overruns
  776. */
  777. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  778. if (res == -1) {
  779. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  780. "Unable to set SO_RCVBUF size on UDP mcast socket");
  781. return (-1);
  782. }
  783. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  784. if (res == -1) {
  785. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  786. "Unable to set SO_SNDBUF size on UDP mcast socket");
  787. return (-1);
  788. }
  789. res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  790. if (res == -1) {
  791. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  792. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  793. return (-1);
  794. }
  795. res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  796. if (res == -1) {
  797. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  798. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  799. return (-1);
  800. }
  801. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  802. if (res == 0) {
  803. log_printf (instance->totemudp_log_level_debug,
  804. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  805. }
  806. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  807. if (res == 0) {
  808. log_printf (instance->totemudp_log_level_debug,
  809. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  810. }
  811. res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  812. if (res == 0) {
  813. log_printf (instance->totemudp_log_level_debug,
  814. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  815. }
  816. res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  817. if (res == 0) {
  818. log_printf (instance->totemudp_log_level_debug,
  819. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  820. }
  821. /*
  822. * Join group membership on socket
  823. */
  824. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  825. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  826. if (instance->totem_config->broadcast_use == 1) {
  827. unsigned int broadcast = 1;
  828. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  829. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  830. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  831. "setting broadcast option failed");
  832. return (-1);
  833. }
  834. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  835. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  836. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  837. "setting broadcast option failed");
  838. return (-1);
  839. }
  840. } else {
  841. switch (bindnet_address->family) {
  842. case AF_INET:
  843. memset(&mreq, 0, sizeof(mreq));
  844. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  845. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  846. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  847. &mreq, sizeof (mreq));
  848. if (res == -1) {
  849. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  850. "join ipv4 multicast group failed");
  851. return (-1);
  852. }
  853. break;
  854. case AF_INET6:
  855. memset(&mreq6, 0, sizeof(mreq6));
  856. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  857. mreq6.ipv6mr_interface = interface_num;
  858. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  859. &mreq6, sizeof (mreq6));
  860. if (res == -1) {
  861. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  862. "join ipv6 multicast group failed");
  863. return (-1);
  864. }
  865. break;
  866. }
  867. }
  868. /*
  869. * Turn off multicast loopback
  870. */
  871. flag = 0;
  872. switch ( bindnet_address->family ) {
  873. case AF_INET:
  874. sflag = 0;
  875. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  876. &sflag, sizeof (sflag));
  877. break;
  878. case AF_INET6:
  879. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  880. &flag, sizeof (flag));
  881. }
  882. if (res == -1) {
  883. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  884. "Unable to turn off multicast loopback");
  885. return (-1);
  886. }
  887. /*
  888. * Set multicast packets TTL
  889. */
  890. flag = instance->totem_interface->ttl;
  891. if (bindnet_address->family == AF_INET6) {
  892. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  893. &flag, sizeof (flag));
  894. if (res == -1) {
  895. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  896. "set mcast v6 TTL failed");
  897. return (-1);
  898. }
  899. } else {
  900. sflag = flag;
  901. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  902. &sflag, sizeof(sflag));
  903. if (res == -1) {
  904. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  905. "set mcast v4 TTL failed");
  906. return (-1);
  907. }
  908. }
  909. /*
  910. * Bind to a specific interface for multicast send and receive
  911. */
  912. switch ( bindnet_address->family ) {
  913. case AF_INET:
  914. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  915. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  916. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  917. "cannot select interface for multicast packets (send)");
  918. return (-1);
  919. }
  920. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  921. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  922. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  923. "cannot select interface for multicast packets (recv)");
  924. return (-1);
  925. }
  926. break;
  927. case AF_INET6:
  928. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  929. &interface_num, sizeof (interface_num)) < 0) {
  930. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  931. "cannot select interface for multicast packets (send v6)");
  932. return (-1);
  933. }
  934. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  935. &interface_num, sizeof (interface_num)) < 0) {
  936. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  937. "cannot select interface for multicast packets (recv v6)");
  938. return (-1);
  939. }
  940. break;
  941. }
  942. /*
  943. * Bind to multicast socket used for multicast receives
  944. * This needs to happen after all of the multicast setsockopt() calls
  945. * as the kernel seems to only put them into effect (for IPV6) when bind()
  946. * is called.
  947. */
  948. totemip_totemip_to_sockaddr_convert(mcast_address,
  949. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  950. retries = 0;
  951. while (1) {
  952. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  953. if (res == 0) {
  954. break;
  955. }
  956. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  957. "Unable to bind the socket to receive multicast packets");
  958. if (++retries > BIND_MAX_RETRIES) {
  959. break;
  960. }
  961. /*
  962. * Wait for a while
  963. */
  964. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  965. }
  966. if (res == -1) {
  967. return (-1);
  968. }
  969. return 0;
  970. }
  971. static int totemudp_build_sockets (
  972. struct totemudp_instance *instance,
  973. struct totem_ip_address *mcast_address,
  974. struct totem_ip_address *bindnet_address,
  975. struct totemudp_socket *sockets,
  976. struct totem_ip_address *bound_to)
  977. {
  978. int interface_num;
  979. int interface_up;
  980. int res;
  981. /*
  982. * Determine the ip address bound to and the interface name
  983. */
  984. res = netif_determine (instance,
  985. bindnet_address,
  986. bound_to,
  987. &interface_up,
  988. &interface_num);
  989. if (res == -1) {
  990. return (-1);
  991. }
  992. totemip_copy(&instance->my_id, bound_to);
  993. res = totemudp_build_sockets_ip (instance, mcast_address,
  994. bindnet_address, sockets, bound_to, interface_num);
  995. if (res == -1) {
  996. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  997. LOGSYS_PERROR (errno, instance->totemudp_log_level_error,
  998. "Unable to create sockets, exiting");
  999. exit(EXIT_FAILURE);
  1000. }
  1001. /* We only send out of the token socket */
  1002. totemudp_traffic_control_set(instance, sockets->token);
  1003. return res;
  1004. }
  1005. /*
  1006. * Totem Network interface - also does encryption/decryption
  1007. * depends on poll abstraction, POSIX, IPV4
  1008. */
  1009. /*
  1010. * Create an instance
  1011. */
  1012. int totemudp_initialize (
  1013. qb_loop_t *poll_handle,
  1014. void **udp_context,
  1015. struct totem_config *totem_config,
  1016. totemsrp_stats_t *stats,
  1017. int interface_no,
  1018. void *context,
  1019. void (*deliver_fn) (
  1020. void *context,
  1021. const void *msg,
  1022. unsigned int msg_len),
  1023. void (*iface_change_fn) (
  1024. void *context,
  1025. const struct totem_ip_address *iface_address),
  1026. void (*target_set_completed) (
  1027. void *context))
  1028. {
  1029. struct totemudp_instance *instance;
  1030. instance = malloc (sizeof (struct totemudp_instance));
  1031. if (instance == NULL) {
  1032. return (-1);
  1033. }
  1034. totemudp_instance_initialize (instance);
  1035. instance->totem_config = totem_config;
  1036. instance->stats = stats;
  1037. /*
  1038. * Configure logging
  1039. */
  1040. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1041. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1042. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1043. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1044. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1045. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1046. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1047. /*
  1048. * Initialize random number generator for later use to generate salt
  1049. */
  1050. instance->crypto_inst = crypto_init (totem_config->private_key,
  1051. totem_config->private_key_len,
  1052. totem_config->crypto_cipher_type,
  1053. totem_config->crypto_hash_type,
  1054. instance->totemudp_log_printf,
  1055. instance->totemudp_log_level_security,
  1056. instance->totemudp_log_level_notice,
  1057. instance->totemudp_log_level_error,
  1058. instance->totemudp_subsys_id);
  1059. if (instance->crypto_inst == NULL) {
  1060. free(instance);
  1061. return (-1);
  1062. }
  1063. /*
  1064. * Initialize local variables for totemudp
  1065. */
  1066. instance->totem_interface = &totem_config->interfaces[interface_no];
  1067. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1068. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1069. instance->totemudp_poll_handle = poll_handle;
  1070. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1071. instance->context = context;
  1072. instance->totemudp_deliver_fn = deliver_fn;
  1073. instance->totemudp_iface_change_fn = iface_change_fn;
  1074. instance->totemudp_target_set_completed = target_set_completed;
  1075. totemip_localhost (instance->mcast_address.family, &localhost);
  1076. localhost.nodeid = instance->totem_config->node_id;
  1077. /*
  1078. * RRP layer isn't ready to receive message because it hasn't
  1079. * initialized yet. Add short timer to check the interfaces.
  1080. */
  1081. qb_loop_timer_add (instance->totemudp_poll_handle,
  1082. QB_LOOP_MED,
  1083. 100*QB_TIME_NS_IN_MSEC,
  1084. (void *)instance,
  1085. timer_function_netif_check_timeout,
  1086. &instance->timer_netif_check_timeout);
  1087. *udp_context = instance;
  1088. return (0);
  1089. }
  1090. void *totemudp_buffer_alloc (void)
  1091. {
  1092. return malloc (FRAME_SIZE_MAX);
  1093. }
  1094. void totemudp_buffer_release (void *ptr)
  1095. {
  1096. return free (ptr);
  1097. }
  1098. int totemudp_processor_count_set (
  1099. void *udp_context,
  1100. int processor_count)
  1101. {
  1102. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1103. int res = 0;
  1104. instance->my_memb_entries = processor_count;
  1105. qb_loop_timer_del (instance->totemudp_poll_handle,
  1106. instance->timer_netif_check_timeout);
  1107. if (processor_count == 1) {
  1108. qb_loop_timer_add (instance->totemudp_poll_handle,
  1109. QB_LOOP_MED,
  1110. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1111. (void *)instance,
  1112. timer_function_netif_check_timeout,
  1113. &instance->timer_netif_check_timeout);
  1114. }
  1115. return (res);
  1116. }
  1117. int totemudp_recv_flush (void *udp_context)
  1118. {
  1119. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1120. struct pollfd ufd;
  1121. int nfds;
  1122. int res = 0;
  1123. int i;
  1124. int sock;
  1125. instance->flushing = 1;
  1126. for (i = 0; i < 2; i++) {
  1127. sock = -1;
  1128. if (i == 0) {
  1129. sock = instance->totemudp_sockets.mcast_recv;
  1130. }
  1131. if (i == 1) {
  1132. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1133. }
  1134. assert(sock != -1);
  1135. do {
  1136. ufd.fd = sock;
  1137. ufd.events = POLLIN;
  1138. nfds = poll (&ufd, 1, 0);
  1139. if (nfds == 1 && ufd.revents & POLLIN) {
  1140. net_deliver_fn (sock, ufd.revents, instance);
  1141. }
  1142. } while (nfds == 1);
  1143. }
  1144. instance->flushing = 0;
  1145. return (res);
  1146. }
  1147. int totemudp_send_flush (void *udp_context)
  1148. {
  1149. return 0;
  1150. }
  1151. int totemudp_token_send (
  1152. void *udp_context,
  1153. const void *msg,
  1154. unsigned int msg_len)
  1155. {
  1156. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1157. int res = 0;
  1158. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1159. return (res);
  1160. }
  1161. int totemudp_mcast_flush_send (
  1162. void *udp_context,
  1163. const void *msg,
  1164. unsigned int msg_len)
  1165. {
  1166. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1167. int res = 0;
  1168. mcast_sendmsg (instance, msg, msg_len);
  1169. return (res);
  1170. }
  1171. int totemudp_mcast_noflush_send (
  1172. void *udp_context,
  1173. const void *msg,
  1174. unsigned int msg_len)
  1175. {
  1176. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1177. int res = 0;
  1178. mcast_sendmsg (instance, msg, msg_len);
  1179. return (res);
  1180. }
  1181. extern int totemudp_iface_check (void *udp_context)
  1182. {
  1183. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1184. int res = 0;
  1185. timer_function_netif_check_timeout (instance);
  1186. return (res);
  1187. }
  1188. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1189. {
  1190. assert(totem_config->interface_count > 0);
  1191. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1192. totem_config->crypto_hash_type) +
  1193. totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  1194. }
  1195. const char *totemudp_iface_print (void *udp_context) {
  1196. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1197. const char *ret_char;
  1198. ret_char = totemip_print (&instance->my_id);
  1199. return (ret_char);
  1200. }
  1201. int totemudp_iface_get (
  1202. void *udp_context,
  1203. struct totem_ip_address *addr)
  1204. {
  1205. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1206. int res = 0;
  1207. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1208. return (res);
  1209. }
  1210. int totemudp_token_target_set (
  1211. void *udp_context,
  1212. const struct totem_ip_address *token_target)
  1213. {
  1214. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1215. int res = 0;
  1216. memcpy (&instance->token_target, token_target,
  1217. sizeof (struct totem_ip_address));
  1218. instance->totemudp_target_set_completed (instance->context);
  1219. return (res);
  1220. }
  1221. extern int totemudp_recv_mcast_empty (
  1222. void *udp_context)
  1223. {
  1224. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1225. unsigned int res;
  1226. struct sockaddr_storage system_from;
  1227. struct msghdr msg_recv;
  1228. struct pollfd ufd;
  1229. int nfds;
  1230. int msg_processed = 0;
  1231. int i;
  1232. int sock;
  1233. /*
  1234. * Receive datagram
  1235. */
  1236. msg_recv.msg_name = &system_from;
  1237. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1238. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1239. msg_recv.msg_iovlen = 1;
  1240. #ifdef HAVE_MSGHDR_CONTROL
  1241. msg_recv.msg_control = 0;
  1242. #endif
  1243. #ifdef HAVE_MSGHDR_CONTROLLEN
  1244. msg_recv.msg_controllen = 0;
  1245. #endif
  1246. #ifdef HAVE_MSGHDR_FLAGS
  1247. msg_recv.msg_flags = 0;
  1248. #endif
  1249. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1250. msg_recv.msg_accrights = NULL;
  1251. #endif
  1252. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1253. msg_recv.msg_accrightslen = 0;
  1254. #endif
  1255. for (i = 0; i < 2; i++) {
  1256. sock = -1;
  1257. if (i == 0) {
  1258. sock = instance->totemudp_sockets.mcast_recv;
  1259. }
  1260. if (i == 1) {
  1261. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1262. }
  1263. assert(sock != -1);
  1264. do {
  1265. ufd.fd = sock;
  1266. ufd.events = POLLIN;
  1267. nfds = poll (&ufd, 1, 0);
  1268. if (nfds == 1 && ufd.revents & POLLIN) {
  1269. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1270. if (res != -1) {
  1271. msg_processed = 1;
  1272. } else {
  1273. msg_processed = -1;
  1274. }
  1275. }
  1276. } while (nfds == 1);
  1277. }
  1278. return (msg_processed);
  1279. }