totemudp.c 38 KB

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