totemudp.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. iovec->iov_len = FRAME_SIZE_MAX;
  429. return (0);
  430. }
  431. /*
  432. * Handle incoming message
  433. */
  434. instance->totemudp_deliver_fn (
  435. instance->context,
  436. iovec->iov_base,
  437. iovec->iov_len);
  438. iovec->iov_len = FRAME_SIZE_MAX;
  439. return (0);
  440. }
  441. static int netif_determine (
  442. struct totemudp_instance *instance,
  443. struct totem_ip_address *bindnet,
  444. struct totem_ip_address *bound_to,
  445. int *interface_up,
  446. int *interface_num)
  447. {
  448. int res;
  449. res = totemip_iface_check (bindnet, bound_to,
  450. interface_up, interface_num,
  451. instance->totem_config->clear_node_high_bit);
  452. return (res);
  453. }
  454. /*
  455. * If the interface is up, the sockets for totem are built. If the interface is down
  456. * this function is requeued in the timer list to retry building the sockets later.
  457. */
  458. static void timer_function_netif_check_timeout (
  459. void *data)
  460. {
  461. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  462. int interface_up;
  463. int interface_num;
  464. struct totem_ip_address *bind_address;
  465. /*
  466. * Build sockets for every interface
  467. */
  468. netif_determine (instance,
  469. &instance->totem_interface->bindnet,
  470. &instance->totem_interface->boundto,
  471. &interface_up, &interface_num);
  472. /*
  473. * If the network interface isn't back up and we are already
  474. * in loopback mode, add timer to check again and return
  475. */
  476. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  477. interface_up == 0) ||
  478. (instance->my_memb_entries == 1 &&
  479. instance->netif_bind_state == BIND_STATE_REGULAR &&
  480. interface_up == 1)) {
  481. qb_loop_timer_add (instance->totemudp_poll_handle,
  482. QB_LOOP_MED,
  483. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  484. (void *)instance,
  485. timer_function_netif_check_timeout,
  486. &instance->timer_netif_check_timeout);
  487. /*
  488. * Add a timer to check for a downed regular interface
  489. */
  490. return;
  491. }
  492. if (instance->totemudp_sockets.mcast_recv > 0) {
  493. qb_loop_poll_del (instance->totemudp_poll_handle,
  494. instance->totemudp_sockets.mcast_recv);
  495. close (instance->totemudp_sockets.mcast_recv);
  496. }
  497. if (instance->totemudp_sockets.mcast_send > 0) {
  498. close (instance->totemudp_sockets.mcast_send);
  499. }
  500. if (instance->totemudp_sockets.local_mcast_loop[0] > 0) {
  501. qb_loop_poll_del (instance->totemudp_poll_handle,
  502. instance->totemudp_sockets.local_mcast_loop[0]);
  503. close (instance->totemudp_sockets.local_mcast_loop[0]);
  504. close (instance->totemudp_sockets.local_mcast_loop[1]);
  505. }
  506. if (instance->totemudp_sockets.token > 0) {
  507. qb_loop_poll_del (instance->totemudp_poll_handle,
  508. instance->totemudp_sockets.token);
  509. close (instance->totemudp_sockets.token);
  510. }
  511. if (interface_up == 0) {
  512. /*
  513. * Interface is not up
  514. */
  515. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  516. bind_address = &localhost;
  517. /*
  518. * Add a timer to retry building interfaces and request memb_gather_enter
  519. */
  520. qb_loop_timer_add (instance->totemudp_poll_handle,
  521. QB_LOOP_MED,
  522. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  523. (void *)instance,
  524. timer_function_netif_check_timeout,
  525. &instance->timer_netif_check_timeout);
  526. } else {
  527. /*
  528. * Interface is up
  529. */
  530. instance->netif_bind_state = BIND_STATE_REGULAR;
  531. bind_address = &instance->totem_interface->bindnet;
  532. }
  533. /*
  534. * Create and bind the multicast and unicast sockets
  535. */
  536. (void)totemudp_build_sockets (instance,
  537. &instance->mcast_address,
  538. bind_address,
  539. &instance->totemudp_sockets,
  540. &instance->totem_interface->boundto);
  541. qb_loop_poll_add (
  542. instance->totemudp_poll_handle,
  543. QB_LOOP_MED,
  544. instance->totemudp_sockets.mcast_recv,
  545. POLLIN, instance, net_deliver_fn);
  546. qb_loop_poll_add (
  547. instance->totemudp_poll_handle,
  548. QB_LOOP_MED,
  549. instance->totemudp_sockets.local_mcast_loop[0],
  550. POLLIN, instance, net_deliver_fn);
  551. qb_loop_poll_add (
  552. instance->totemudp_poll_handle,
  553. QB_LOOP_MED,
  554. instance->totemudp_sockets.token,
  555. POLLIN, instance, net_deliver_fn);
  556. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  557. /*
  558. * This reports changes in the interface to the user and totemsrp
  559. */
  560. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  561. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  562. log_printf (instance->totemudp_log_level_notice,
  563. "The network interface [%s] is now up.",
  564. totemip_print (&instance->totem_interface->boundto));
  565. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  566. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  567. }
  568. /*
  569. * Add a timer to check for interface going down in single membership
  570. */
  571. if (instance->my_memb_entries == 1) {
  572. qb_loop_timer_add (instance->totemudp_poll_handle,
  573. QB_LOOP_MED,
  574. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  575. (void *)instance,
  576. timer_function_netif_check_timeout,
  577. &instance->timer_netif_check_timeout);
  578. }
  579. } else {
  580. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  581. log_printf (instance->totemudp_log_level_notice,
  582. "The network interface is down.");
  583. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  584. }
  585. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  586. }
  587. }
  588. /* Set the socket priority to INTERACTIVE to ensure
  589. that our messages don't get queued behind anything else */
  590. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  591. {
  592. #ifdef SO_PRIORITY
  593. int prio = 6; /* TC_PRIO_INTERACTIVE */
  594. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  595. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  596. }
  597. #endif
  598. }
  599. static int totemudp_build_sockets_ip (
  600. struct totemudp_instance *instance,
  601. struct totem_ip_address *mcast_address,
  602. struct totem_ip_address *bindnet_address,
  603. struct totemudp_socket *sockets,
  604. struct totem_ip_address *bound_to,
  605. int interface_num)
  606. {
  607. struct sockaddr_storage sockaddr;
  608. struct ipv6_mreq mreq6;
  609. struct ip_mreq mreq;
  610. struct sockaddr_storage mcast_ss, boundto_ss;
  611. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  612. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  613. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  614. unsigned int sendbuf_size;
  615. unsigned int recvbuf_size;
  616. unsigned int optlen = sizeof (sendbuf_size);
  617. int addrlen;
  618. int res;
  619. int flag;
  620. uint8_t sflag;
  621. int i;
  622. /*
  623. * Create multicast recv socket
  624. */
  625. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  626. if (sockets->mcast_recv == -1) {
  627. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  628. "socket() failed");
  629. return (-1);
  630. }
  631. totemip_nosigpipe (sockets->mcast_recv);
  632. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  633. if (res == -1) {
  634. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  635. "Could not set non-blocking operation on multicast socket");
  636. return (-1);
  637. }
  638. /*
  639. * Force reuse
  640. */
  641. flag = 1;
  642. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  643. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  644. "setsockopt(SO_REUSEADDR) failed");
  645. return (-1);
  646. }
  647. /*
  648. * Bind to multicast socket used for multicast receives
  649. */
  650. totemip_totemip_to_sockaddr_convert(mcast_address,
  651. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  652. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  653. if (res == -1) {
  654. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  655. "Unable to bind the socket to receive multicast packets");
  656. return (-1);
  657. }
  658. /*
  659. * Create local multicast loop socket
  660. */
  661. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets->local_mcast_loop) == -1) {
  662. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  663. "socket() failed");
  664. return (-1);
  665. }
  666. for (i = 0; i < 2; i++) {
  667. totemip_nosigpipe (sockets->local_mcast_loop[i]);
  668. res = fcntl (sockets->local_mcast_loop[i], F_SETFL, O_NONBLOCK);
  669. if (res == -1) {
  670. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  671. "Could not set non-blocking operation on multicast socket");
  672. return (-1);
  673. }
  674. }
  675. /*
  676. * Setup mcast send socket
  677. */
  678. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  679. if (sockets->mcast_send == -1) {
  680. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  681. "socket() failed");
  682. return (-1);
  683. }
  684. totemip_nosigpipe (sockets->mcast_send);
  685. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  686. if (res == -1) {
  687. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  688. "Could not set non-blocking operation on multicast socket");
  689. return (-1);
  690. }
  691. /*
  692. * Force reuse
  693. */
  694. flag = 1;
  695. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  696. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  697. "setsockopt(SO_REUSEADDR) failed");
  698. return (-1);
  699. }
  700. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  701. &sockaddr, &addrlen);
  702. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  703. if (res == -1) {
  704. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  705. "Unable to bind the socket to send multicast packets");
  706. return (-1);
  707. }
  708. /*
  709. * Setup unicast socket
  710. */
  711. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  712. if (sockets->token == -1) {
  713. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  714. "socket() failed");
  715. return (-1);
  716. }
  717. totemip_nosigpipe (sockets->token);
  718. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  719. if (res == -1) {
  720. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  721. "Could not set non-blocking operation on token socket");
  722. return (-1);
  723. }
  724. /*
  725. * Force reuse
  726. */
  727. flag = 1;
  728. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  729. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  730. "setsockopt(SO_REUSEADDR) failed");
  731. return (-1);
  732. }
  733. /*
  734. * Bind to unicast socket used for token send/receives
  735. * This has the side effect of binding to the correct interface
  736. */
  737. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  738. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  739. if (res == -1) {
  740. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  741. "Unable to bind UDP unicast socket");
  742. return (-1);
  743. }
  744. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  745. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  746. /*
  747. * Set buffer sizes to avoid overruns
  748. */
  749. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  750. if (res == -1) {
  751. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  752. "Unable to set SO_RCVBUF size on UDP mcast socket");
  753. return (-1);
  754. }
  755. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  756. if (res == -1) {
  757. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  758. "Unable to set SO_SNDBUF size on UDP mcast socket");
  759. return (-1);
  760. }
  761. res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  762. if (res == -1) {
  763. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  764. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  765. return (-1);
  766. }
  767. res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  768. if (res == -1) {
  769. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  770. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  771. return (-1);
  772. }
  773. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  774. if (res == 0) {
  775. log_printf (instance->totemudp_log_level_debug,
  776. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  777. }
  778. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  779. if (res == 0) {
  780. log_printf (instance->totemudp_log_level_debug,
  781. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  782. }
  783. res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  784. if (res == 0) {
  785. log_printf (instance->totemudp_log_level_debug,
  786. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  787. }
  788. res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  789. if (res == 0) {
  790. log_printf (instance->totemudp_log_level_debug,
  791. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  792. }
  793. /*
  794. * Join group membership on socket
  795. */
  796. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  797. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  798. if (instance->totem_config->broadcast_use == 1) {
  799. unsigned int broadcast = 1;
  800. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  801. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  802. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  803. "setting broadcast option failed");
  804. return (-1);
  805. }
  806. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  807. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  808. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  809. "setting broadcast option failed");
  810. return (-1);
  811. }
  812. } else {
  813. switch (bindnet_address->family) {
  814. case AF_INET:
  815. memset(&mreq, 0, sizeof(mreq));
  816. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  817. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  818. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  819. &mreq, sizeof (mreq));
  820. if (res == -1) {
  821. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  822. "join ipv4 multicast group failed");
  823. return (-1);
  824. }
  825. break;
  826. case AF_INET6:
  827. memset(&mreq6, 0, sizeof(mreq6));
  828. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  829. mreq6.ipv6mr_interface = interface_num;
  830. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  831. &mreq6, sizeof (mreq6));
  832. if (res == -1) {
  833. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  834. "join ipv6 multicast group failed");
  835. return (-1);
  836. }
  837. break;
  838. }
  839. }
  840. /*
  841. * Turn off multicast loopback
  842. */
  843. flag = 0;
  844. switch ( bindnet_address->family ) {
  845. case AF_INET:
  846. sflag = 0;
  847. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  848. &sflag, sizeof (sflag));
  849. break;
  850. case AF_INET6:
  851. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  852. &flag, sizeof (flag));
  853. }
  854. if (res == -1) {
  855. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  856. "Unable to turn off multicast loopback");
  857. return (-1);
  858. }
  859. /*
  860. * Set multicast packets TTL
  861. */
  862. flag = instance->totem_interface->ttl;
  863. if (bindnet_address->family == AF_INET6) {
  864. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  865. &flag, sizeof (flag));
  866. if (res == -1) {
  867. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  868. "set mcast v6 TTL failed");
  869. return (-1);
  870. }
  871. } else {
  872. sflag = flag;
  873. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  874. &sflag, sizeof(sflag));
  875. if (res == -1) {
  876. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  877. "set mcast v4 TTL failed");
  878. return (-1);
  879. }
  880. }
  881. /*
  882. * Bind to a specific interface for multicast send and receive
  883. */
  884. switch ( bindnet_address->family ) {
  885. case AF_INET:
  886. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  887. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  888. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  889. "cannot select interface for multicast packets (send)");
  890. return (-1);
  891. }
  892. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  893. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  894. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  895. "cannot select interface for multicast packets (recv)");
  896. return (-1);
  897. }
  898. break;
  899. case AF_INET6:
  900. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  901. &interface_num, sizeof (interface_num)) < 0) {
  902. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  903. "cannot select interface for multicast packets (send v6)");
  904. return (-1);
  905. }
  906. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  907. &interface_num, sizeof (interface_num)) < 0) {
  908. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  909. "cannot select interface for multicast packets (recv v6)");
  910. return (-1);
  911. }
  912. break;
  913. }
  914. return 0;
  915. }
  916. static int totemudp_build_sockets (
  917. struct totemudp_instance *instance,
  918. struct totem_ip_address *mcast_address,
  919. struct totem_ip_address *bindnet_address,
  920. struct totemudp_socket *sockets,
  921. struct totem_ip_address *bound_to)
  922. {
  923. int interface_num;
  924. int interface_up;
  925. int res;
  926. /*
  927. * Determine the ip address bound to and the interface name
  928. */
  929. res = netif_determine (instance,
  930. bindnet_address,
  931. bound_to,
  932. &interface_up,
  933. &interface_num);
  934. if (res == -1) {
  935. return (-1);
  936. }
  937. totemip_copy(&instance->my_id, bound_to);
  938. res = totemudp_build_sockets_ip (instance, mcast_address,
  939. bindnet_address, sockets, bound_to, interface_num);
  940. /* We only send out of the token socket */
  941. totemudp_traffic_control_set(instance, sockets->token);
  942. return res;
  943. }
  944. /*
  945. * Totem Network interface - also does encryption/decryption
  946. * depends on poll abstraction, POSIX, IPV4
  947. */
  948. /*
  949. * Create an instance
  950. */
  951. int totemudp_initialize (
  952. qb_loop_t *poll_handle,
  953. void **udp_context,
  954. struct totem_config *totem_config,
  955. totemsrp_stats_t *stats,
  956. int interface_no,
  957. void *context,
  958. void (*deliver_fn) (
  959. void *context,
  960. const void *msg,
  961. unsigned int msg_len),
  962. void (*iface_change_fn) (
  963. void *context,
  964. const struct totem_ip_address *iface_address),
  965. void (*target_set_completed) (
  966. void *context))
  967. {
  968. struct totemudp_instance *instance;
  969. instance = malloc (sizeof (struct totemudp_instance));
  970. if (instance == NULL) {
  971. return (-1);
  972. }
  973. totemudp_instance_initialize (instance);
  974. instance->totem_config = totem_config;
  975. instance->stats = stats;
  976. /*
  977. * Configure logging
  978. */
  979. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  980. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  981. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  982. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  983. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  984. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  985. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  986. /*
  987. * Initialize random number generator for later use to generate salt
  988. */
  989. instance->crypto_inst = crypto_init (totem_config->private_key,
  990. totem_config->private_key_len,
  991. totem_config->crypto_cipher_type,
  992. totem_config->crypto_hash_type,
  993. instance->totemudp_log_printf,
  994. instance->totemudp_log_level_security,
  995. instance->totemudp_log_level_notice,
  996. instance->totemudp_log_level_error,
  997. instance->totemudp_subsys_id);
  998. if (instance->crypto_inst == NULL) {
  999. return (-1);
  1000. }
  1001. /*
  1002. * Initialize local variables for totemudp
  1003. */
  1004. instance->totem_interface = &totem_config->interfaces[interface_no];
  1005. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1006. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1007. instance->totemudp_poll_handle = poll_handle;
  1008. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1009. instance->context = context;
  1010. instance->totemudp_deliver_fn = deliver_fn;
  1011. instance->totemudp_iface_change_fn = iface_change_fn;
  1012. instance->totemudp_target_set_completed = target_set_completed;
  1013. totemip_localhost (instance->mcast_address.family, &localhost);
  1014. localhost.nodeid = instance->totem_config->node_id;
  1015. /*
  1016. * RRP layer isn't ready to receive message because it hasn't
  1017. * initialized yet. Add short timer to check the interfaces.
  1018. */
  1019. qb_loop_timer_add (instance->totemudp_poll_handle,
  1020. QB_LOOP_MED,
  1021. 100*QB_TIME_NS_IN_MSEC,
  1022. (void *)instance,
  1023. timer_function_netif_check_timeout,
  1024. &instance->timer_netif_check_timeout);
  1025. *udp_context = instance;
  1026. return (0);
  1027. }
  1028. void *totemudp_buffer_alloc (void)
  1029. {
  1030. return malloc (FRAME_SIZE_MAX);
  1031. }
  1032. void totemudp_buffer_release (void *ptr)
  1033. {
  1034. return free (ptr);
  1035. }
  1036. int totemudp_processor_count_set (
  1037. void *udp_context,
  1038. int processor_count)
  1039. {
  1040. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1041. int res = 0;
  1042. instance->my_memb_entries = processor_count;
  1043. qb_loop_timer_del (instance->totemudp_poll_handle,
  1044. instance->timer_netif_check_timeout);
  1045. if (processor_count == 1) {
  1046. qb_loop_timer_add (instance->totemudp_poll_handle,
  1047. QB_LOOP_MED,
  1048. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1049. (void *)instance,
  1050. timer_function_netif_check_timeout,
  1051. &instance->timer_netif_check_timeout);
  1052. }
  1053. return (res);
  1054. }
  1055. int totemudp_recv_flush (void *udp_context)
  1056. {
  1057. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1058. struct pollfd ufd;
  1059. int nfds;
  1060. int res = 0;
  1061. int i;
  1062. int sock;
  1063. instance->flushing = 1;
  1064. for (i = 0; i < 2; i++) {
  1065. sock = -1;
  1066. if (i == 0) {
  1067. sock = instance->totemudp_sockets.mcast_recv;
  1068. }
  1069. if (i == 1) {
  1070. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1071. }
  1072. assert(sock != -1);
  1073. do {
  1074. ufd.fd = sock;
  1075. ufd.events = POLLIN;
  1076. nfds = poll (&ufd, 1, 0);
  1077. if (nfds == 1 && ufd.revents & POLLIN) {
  1078. net_deliver_fn (sock, ufd.revents, instance);
  1079. }
  1080. } while (nfds == 1);
  1081. }
  1082. instance->flushing = 0;
  1083. return (res);
  1084. }
  1085. int totemudp_send_flush (void *udp_context)
  1086. {
  1087. return 0;
  1088. }
  1089. int totemudp_token_send (
  1090. void *udp_context,
  1091. const void *msg,
  1092. unsigned int msg_len)
  1093. {
  1094. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1095. int res = 0;
  1096. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1097. return (res);
  1098. }
  1099. int totemudp_mcast_flush_send (
  1100. void *udp_context,
  1101. const void *msg,
  1102. unsigned int msg_len)
  1103. {
  1104. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1105. int res = 0;
  1106. mcast_sendmsg (instance, msg, msg_len);
  1107. return (res);
  1108. }
  1109. int totemudp_mcast_noflush_send (
  1110. void *udp_context,
  1111. const void *msg,
  1112. unsigned int msg_len)
  1113. {
  1114. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1115. int res = 0;
  1116. mcast_sendmsg (instance, msg, msg_len);
  1117. return (res);
  1118. }
  1119. extern int totemudp_iface_check (void *udp_context)
  1120. {
  1121. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1122. int res = 0;
  1123. timer_function_netif_check_timeout (instance);
  1124. return (res);
  1125. }
  1126. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1127. {
  1128. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1129. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1130. totem_config->crypto_hash_type) +
  1131. UDPIP_HEADER_SIZE;
  1132. }
  1133. const char *totemudp_iface_print (void *udp_context) {
  1134. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1135. const char *ret_char;
  1136. ret_char = totemip_print (&instance->my_id);
  1137. return (ret_char);
  1138. }
  1139. int totemudp_iface_get (
  1140. void *udp_context,
  1141. struct totem_ip_address *addr)
  1142. {
  1143. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1144. int res = 0;
  1145. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1146. return (res);
  1147. }
  1148. int totemudp_token_target_set (
  1149. void *udp_context,
  1150. const struct totem_ip_address *token_target)
  1151. {
  1152. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1153. int res = 0;
  1154. memcpy (&instance->token_target, token_target,
  1155. sizeof (struct totem_ip_address));
  1156. instance->totemudp_target_set_completed (instance->context);
  1157. return (res);
  1158. }
  1159. extern int totemudp_recv_mcast_empty (
  1160. void *udp_context)
  1161. {
  1162. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1163. unsigned int res;
  1164. struct sockaddr_storage system_from;
  1165. struct msghdr msg_recv;
  1166. struct pollfd ufd;
  1167. int nfds;
  1168. int msg_processed = 0;
  1169. int i;
  1170. int sock;
  1171. /*
  1172. * Receive datagram
  1173. */
  1174. msg_recv.msg_name = &system_from;
  1175. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1176. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1177. msg_recv.msg_iovlen = 1;
  1178. #ifdef HAVE_MSGHDR_CONTROL
  1179. msg_recv.msg_control = 0;
  1180. #endif
  1181. #ifdef HAVE_MSGHDR_CONTROLLEN
  1182. msg_recv.msg_controllen = 0;
  1183. #endif
  1184. #ifdef HAVE_MSGHDR_FLAGS
  1185. msg_recv.msg_flags = 0;
  1186. #endif
  1187. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1188. msg_recv.msg_accrights = NULL;
  1189. #endif
  1190. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1191. msg_recv.msg_accrightslen = 0;
  1192. #endif
  1193. for (i = 0; i < 2; i++) {
  1194. sock = -1;
  1195. if (i == 0) {
  1196. sock = instance->totemudp_sockets.mcast_recv;
  1197. }
  1198. if (i == 1) {
  1199. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1200. }
  1201. assert(sock != -1);
  1202. do {
  1203. ufd.fd = sock;
  1204. ufd.events = POLLIN;
  1205. nfds = poll (&ufd, 1, 0);
  1206. if (nfds == 1 && ufd.revents & POLLIN) {
  1207. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1208. if (res != -1) {
  1209. msg_processed = 1;
  1210. } else {
  1211. msg_processed = -1;
  1212. }
  1213. }
  1214. } while (nfds == 1);
  1215. }
  1216. return (msg_processed);
  1217. }