totemudp.c 39 KB

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