totemudp.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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. unsigned int retries;
  619. int addrlen;
  620. int res;
  621. int flag;
  622. uint8_t sflag;
  623. int i;
  624. /*
  625. * Create multicast recv socket
  626. */
  627. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  628. if (sockets->mcast_recv == -1) {
  629. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  630. "socket() failed");
  631. return (-1);
  632. }
  633. totemip_nosigpipe (sockets->mcast_recv);
  634. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  635. if (res == -1) {
  636. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  637. "Could not set non-blocking operation on multicast socket");
  638. return (-1);
  639. }
  640. /*
  641. * Force reuse
  642. */
  643. flag = 1;
  644. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  645. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  646. "setsockopt(SO_REUSEADDR) failed");
  647. return (-1);
  648. }
  649. /*
  650. * Create local multicast loop socket
  651. */
  652. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets->local_mcast_loop) == -1) {
  653. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  654. "socket() failed");
  655. return (-1);
  656. }
  657. for (i = 0; i < 2; i++) {
  658. totemip_nosigpipe (sockets->local_mcast_loop[i]);
  659. res = fcntl (sockets->local_mcast_loop[i], F_SETFL, O_NONBLOCK);
  660. if (res == -1) {
  661. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  662. "Could not set non-blocking operation on multicast socket");
  663. return (-1);
  664. }
  665. }
  666. /*
  667. * Setup mcast send socket
  668. */
  669. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  670. if (sockets->mcast_send == -1) {
  671. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  672. "socket() failed");
  673. return (-1);
  674. }
  675. totemip_nosigpipe (sockets->mcast_send);
  676. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  677. if (res == -1) {
  678. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  679. "Could not set non-blocking operation on multicast socket");
  680. return (-1);
  681. }
  682. /*
  683. * Force reuse
  684. */
  685. flag = 1;
  686. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  687. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  688. "setsockopt(SO_REUSEADDR) failed");
  689. return (-1);
  690. }
  691. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  692. &sockaddr, &addrlen);
  693. retries = 0;
  694. while (1) {
  695. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  696. if (res == 0) {
  697. break;
  698. }
  699. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  700. "Unable to bind the socket to send multicast packets");
  701. if (++retries > BIND_MAX_RETRIES) {
  702. break;
  703. }
  704. /*
  705. * Wait for a while
  706. */
  707. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  708. }
  709. if (res == -1) {
  710. return (-1);
  711. }
  712. /*
  713. * Setup unicast socket
  714. */
  715. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  716. if (sockets->token == -1) {
  717. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  718. "socket() failed");
  719. return (-1);
  720. }
  721. totemip_nosigpipe (sockets->token);
  722. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  723. if (res == -1) {
  724. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  725. "Could not set non-blocking operation on token socket");
  726. return (-1);
  727. }
  728. /*
  729. * Force reuse
  730. */
  731. flag = 1;
  732. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  733. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  734. "setsockopt(SO_REUSEADDR) failed");
  735. return (-1);
  736. }
  737. /*
  738. * Bind to unicast socket used for token send/receives
  739. * This has the side effect of binding to the correct interface
  740. */
  741. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  742. retries = 0;
  743. while (1) {
  744. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  745. if (res == 0) {
  746. break;
  747. }
  748. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  749. "Unable to bind UDP unicast socket");
  750. if (++retries > BIND_MAX_RETRIES) {
  751. break;
  752. }
  753. /*
  754. * Wait for a while
  755. */
  756. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  757. }
  758. if (res == -1) {
  759. return (-1);
  760. }
  761. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  762. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  763. /*
  764. * Set buffer sizes to avoid overruns
  765. */
  766. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  767. if (res == -1) {
  768. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  769. "Unable to set SO_RCVBUF size on UDP mcast socket");
  770. return (-1);
  771. }
  772. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  773. if (res == -1) {
  774. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  775. "Unable to set SO_SNDBUF size on UDP mcast socket");
  776. return (-1);
  777. }
  778. res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  779. if (res == -1) {
  780. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  781. "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
  782. return (-1);
  783. }
  784. res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  785. if (res == -1) {
  786. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  787. "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
  788. return (-1);
  789. }
  790. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  791. if (res == 0) {
  792. log_printf (instance->totemudp_log_level_debug,
  793. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  794. }
  795. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  796. if (res == 0) {
  797. log_printf (instance->totemudp_log_level_debug,
  798. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  799. }
  800. res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  801. if (res == 0) {
  802. log_printf (instance->totemudp_log_level_debug,
  803. "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
  804. }
  805. res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  806. if (res == 0) {
  807. log_printf (instance->totemudp_log_level_debug,
  808. "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
  809. }
  810. /*
  811. * Join group membership on socket
  812. */
  813. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  814. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  815. if (instance->totem_config->broadcast_use == 1) {
  816. unsigned int broadcast = 1;
  817. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  818. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  819. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  820. "setting broadcast option failed");
  821. return (-1);
  822. }
  823. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  824. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  825. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  826. "setting broadcast option failed");
  827. return (-1);
  828. }
  829. } else {
  830. switch (bindnet_address->family) {
  831. case AF_INET:
  832. memset(&mreq, 0, sizeof(mreq));
  833. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  834. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  835. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  836. &mreq, sizeof (mreq));
  837. if (res == -1) {
  838. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  839. "join ipv4 multicast group failed");
  840. return (-1);
  841. }
  842. break;
  843. case AF_INET6:
  844. memset(&mreq6, 0, sizeof(mreq6));
  845. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  846. mreq6.ipv6mr_interface = interface_num;
  847. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  848. &mreq6, sizeof (mreq6));
  849. if (res == -1) {
  850. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  851. "join ipv6 multicast group failed");
  852. return (-1);
  853. }
  854. break;
  855. }
  856. }
  857. /*
  858. * Turn off multicast loopback
  859. */
  860. flag = 0;
  861. switch ( bindnet_address->family ) {
  862. case AF_INET:
  863. sflag = 0;
  864. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  865. &sflag, sizeof (sflag));
  866. break;
  867. case AF_INET6:
  868. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  869. &flag, sizeof (flag));
  870. }
  871. if (res == -1) {
  872. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  873. "Unable to turn off multicast loopback");
  874. return (-1);
  875. }
  876. /*
  877. * Set multicast packets TTL
  878. */
  879. flag = instance->totem_interface->ttl;
  880. if (bindnet_address->family == AF_INET6) {
  881. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  882. &flag, sizeof (flag));
  883. if (res == -1) {
  884. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  885. "set mcast v6 TTL failed");
  886. return (-1);
  887. }
  888. } else {
  889. sflag = flag;
  890. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  891. &sflag, sizeof(sflag));
  892. if (res == -1) {
  893. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  894. "set mcast v4 TTL failed");
  895. return (-1);
  896. }
  897. }
  898. /*
  899. * Bind to a specific interface for multicast send and receive
  900. */
  901. switch ( bindnet_address->family ) {
  902. case AF_INET:
  903. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  904. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  905. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  906. "cannot select interface for multicast packets (send)");
  907. return (-1);
  908. }
  909. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  910. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  911. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  912. "cannot select interface for multicast packets (recv)");
  913. return (-1);
  914. }
  915. break;
  916. case AF_INET6:
  917. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  918. &interface_num, sizeof (interface_num)) < 0) {
  919. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  920. "cannot select interface for multicast packets (send v6)");
  921. return (-1);
  922. }
  923. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  924. &interface_num, sizeof (interface_num)) < 0) {
  925. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  926. "cannot select interface for multicast packets (recv v6)");
  927. return (-1);
  928. }
  929. break;
  930. }
  931. /*
  932. * Bind to multicast socket used for multicast receives
  933. * This needs to happen after all of the multicast setsockopt() calls
  934. * as the kernel seems to only put them into effect (for IPV6) when bind()
  935. * is called.
  936. */
  937. totemip_totemip_to_sockaddr_convert(mcast_address,
  938. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  939. retries = 0;
  940. while (1) {
  941. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  942. if (res == 0) {
  943. break;
  944. }
  945. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  946. "Unable to bind the socket to receive multicast packets");
  947. if (++retries > BIND_MAX_RETRIES) {
  948. break;
  949. }
  950. /*
  951. * Wait for a while
  952. */
  953. (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
  954. }
  955. if (res == -1) {
  956. return (-1);
  957. }
  958. return 0;
  959. }
  960. static int totemudp_build_sockets (
  961. struct totemudp_instance *instance,
  962. struct totem_ip_address *mcast_address,
  963. struct totem_ip_address *bindnet_address,
  964. struct totemudp_socket *sockets,
  965. struct totem_ip_address *bound_to)
  966. {
  967. int interface_num;
  968. int interface_up;
  969. int res;
  970. /*
  971. * Determine the ip address bound to and the interface name
  972. */
  973. res = netif_determine (instance,
  974. bindnet_address,
  975. bound_to,
  976. &interface_up,
  977. &interface_num);
  978. if (res == -1) {
  979. return (-1);
  980. }
  981. totemip_copy(&instance->my_id, bound_to);
  982. res = totemudp_build_sockets_ip (instance, mcast_address,
  983. bindnet_address, sockets, bound_to, interface_num);
  984. if (res == -1) {
  985. /* if we get here, corosync won't work anyway, so better leaving than faking to work */
  986. LOGSYS_PERROR (errno, instance->totemudp_log_level_error,
  987. "Unable to create sockets, exiting");
  988. exit(EXIT_FAILURE);
  989. }
  990. /* We only send out of the token socket */
  991. totemudp_traffic_control_set(instance, sockets->token);
  992. return res;
  993. }
  994. /*
  995. * Totem Network interface - also does encryption/decryption
  996. * depends on poll abstraction, POSIX, IPV4
  997. */
  998. /*
  999. * Create an instance
  1000. */
  1001. int totemudp_initialize (
  1002. qb_loop_t *poll_handle,
  1003. void **udp_context,
  1004. struct totem_config *totem_config,
  1005. totemsrp_stats_t *stats,
  1006. int interface_no,
  1007. void *context,
  1008. void (*deliver_fn) (
  1009. void *context,
  1010. const void *msg,
  1011. unsigned int msg_len),
  1012. void (*iface_change_fn) (
  1013. void *context,
  1014. const struct totem_ip_address *iface_address),
  1015. void (*target_set_completed) (
  1016. void *context))
  1017. {
  1018. struct totemudp_instance *instance;
  1019. instance = malloc (sizeof (struct totemudp_instance));
  1020. if (instance == NULL) {
  1021. return (-1);
  1022. }
  1023. totemudp_instance_initialize (instance);
  1024. instance->totem_config = totem_config;
  1025. instance->stats = stats;
  1026. /*
  1027. * Configure logging
  1028. */
  1029. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1030. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1031. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1032. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1033. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1034. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1035. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1036. /*
  1037. * Initialize random number generator for later use to generate salt
  1038. */
  1039. instance->crypto_inst = crypto_init (totem_config->private_key,
  1040. totem_config->private_key_len,
  1041. totem_config->crypto_cipher_type,
  1042. totem_config->crypto_hash_type,
  1043. instance->totemudp_log_printf,
  1044. instance->totemudp_log_level_security,
  1045. instance->totemudp_log_level_notice,
  1046. instance->totemudp_log_level_error,
  1047. instance->totemudp_subsys_id);
  1048. if (instance->crypto_inst == NULL) {
  1049. free(instance);
  1050. return (-1);
  1051. }
  1052. /*
  1053. * Initialize local variables for totemudp
  1054. */
  1055. instance->totem_interface = &totem_config->interfaces[interface_no];
  1056. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1057. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1058. instance->totemudp_poll_handle = poll_handle;
  1059. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1060. instance->context = context;
  1061. instance->totemudp_deliver_fn = deliver_fn;
  1062. instance->totemudp_iface_change_fn = iface_change_fn;
  1063. instance->totemudp_target_set_completed = target_set_completed;
  1064. totemip_localhost (instance->mcast_address.family, &localhost);
  1065. localhost.nodeid = instance->totem_config->node_id;
  1066. /*
  1067. * RRP layer isn't ready to receive message because it hasn't
  1068. * initialized yet. Add short timer to check the interfaces.
  1069. */
  1070. qb_loop_timer_add (instance->totemudp_poll_handle,
  1071. QB_LOOP_MED,
  1072. 100*QB_TIME_NS_IN_MSEC,
  1073. (void *)instance,
  1074. timer_function_netif_check_timeout,
  1075. &instance->timer_netif_check_timeout);
  1076. *udp_context = instance;
  1077. return (0);
  1078. }
  1079. void *totemudp_buffer_alloc (void)
  1080. {
  1081. return malloc (FRAME_SIZE_MAX);
  1082. }
  1083. void totemudp_buffer_release (void *ptr)
  1084. {
  1085. return free (ptr);
  1086. }
  1087. int totemudp_processor_count_set (
  1088. void *udp_context,
  1089. int processor_count)
  1090. {
  1091. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1092. int res = 0;
  1093. instance->my_memb_entries = processor_count;
  1094. qb_loop_timer_del (instance->totemudp_poll_handle,
  1095. instance->timer_netif_check_timeout);
  1096. if (processor_count == 1) {
  1097. qb_loop_timer_add (instance->totemudp_poll_handle,
  1098. QB_LOOP_MED,
  1099. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1100. (void *)instance,
  1101. timer_function_netif_check_timeout,
  1102. &instance->timer_netif_check_timeout);
  1103. }
  1104. return (res);
  1105. }
  1106. int totemudp_recv_flush (void *udp_context)
  1107. {
  1108. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1109. struct pollfd ufd;
  1110. int nfds;
  1111. int res = 0;
  1112. int i;
  1113. int sock;
  1114. instance->flushing = 1;
  1115. for (i = 0; i < 2; i++) {
  1116. sock = -1;
  1117. if (i == 0) {
  1118. sock = instance->totemudp_sockets.mcast_recv;
  1119. }
  1120. if (i == 1) {
  1121. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1122. }
  1123. assert(sock != -1);
  1124. do {
  1125. ufd.fd = sock;
  1126. ufd.events = POLLIN;
  1127. nfds = poll (&ufd, 1, 0);
  1128. if (nfds == 1 && ufd.revents & POLLIN) {
  1129. net_deliver_fn (sock, ufd.revents, instance);
  1130. }
  1131. } while (nfds == 1);
  1132. }
  1133. instance->flushing = 0;
  1134. return (res);
  1135. }
  1136. int totemudp_send_flush (void *udp_context)
  1137. {
  1138. return 0;
  1139. }
  1140. int totemudp_token_send (
  1141. void *udp_context,
  1142. const void *msg,
  1143. unsigned int msg_len)
  1144. {
  1145. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1146. int res = 0;
  1147. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1148. return (res);
  1149. }
  1150. int totemudp_mcast_flush_send (
  1151. void *udp_context,
  1152. const void *msg,
  1153. unsigned int msg_len)
  1154. {
  1155. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1156. int res = 0;
  1157. mcast_sendmsg (instance, msg, msg_len);
  1158. return (res);
  1159. }
  1160. int totemudp_mcast_noflush_send (
  1161. void *udp_context,
  1162. const void *msg,
  1163. unsigned int msg_len)
  1164. {
  1165. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1166. int res = 0;
  1167. mcast_sendmsg (instance, msg, msg_len);
  1168. return (res);
  1169. }
  1170. extern int totemudp_iface_check (void *udp_context)
  1171. {
  1172. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1173. int res = 0;
  1174. timer_function_netif_check_timeout (instance);
  1175. return (res);
  1176. }
  1177. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1178. {
  1179. assert(totem_config->interface_count > 0);
  1180. totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
  1181. totem_config->crypto_hash_type) +
  1182. totemip_udpip_header_size(totem_config->interfaces[0].bindnet.family);
  1183. }
  1184. const char *totemudp_iface_print (void *udp_context) {
  1185. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1186. const char *ret_char;
  1187. ret_char = totemip_print (&instance->my_id);
  1188. return (ret_char);
  1189. }
  1190. int totemudp_iface_get (
  1191. void *udp_context,
  1192. struct totem_ip_address *addr)
  1193. {
  1194. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1195. int res = 0;
  1196. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1197. return (res);
  1198. }
  1199. int totemudp_token_target_set (
  1200. void *udp_context,
  1201. const struct totem_ip_address *token_target)
  1202. {
  1203. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1204. int res = 0;
  1205. memcpy (&instance->token_target, token_target,
  1206. sizeof (struct totem_ip_address));
  1207. instance->totemudp_target_set_completed (instance->context);
  1208. return (res);
  1209. }
  1210. extern int totemudp_recv_mcast_empty (
  1211. void *udp_context)
  1212. {
  1213. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1214. unsigned int res;
  1215. struct sockaddr_storage system_from;
  1216. struct msghdr msg_recv;
  1217. struct pollfd ufd;
  1218. int nfds;
  1219. int msg_processed = 0;
  1220. int i;
  1221. int sock;
  1222. /*
  1223. * Receive datagram
  1224. */
  1225. msg_recv.msg_name = &system_from;
  1226. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1227. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1228. msg_recv.msg_iovlen = 1;
  1229. #ifdef HAVE_MSGHDR_CONTROL
  1230. msg_recv.msg_control = 0;
  1231. #endif
  1232. #ifdef HAVE_MSGHDR_CONTROLLEN
  1233. msg_recv.msg_controllen = 0;
  1234. #endif
  1235. #ifdef HAVE_MSGHDR_FLAGS
  1236. msg_recv.msg_flags = 0;
  1237. #endif
  1238. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1239. msg_recv.msg_accrights = NULL;
  1240. #endif
  1241. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1242. msg_recv.msg_accrightslen = 0;
  1243. #endif
  1244. for (i = 0; i < 2; i++) {
  1245. sock = -1;
  1246. if (i == 0) {
  1247. sock = instance->totemudp_sockets.mcast_recv;
  1248. }
  1249. if (i == 1) {
  1250. sock = instance->totemudp_sockets.local_mcast_loop[0];
  1251. }
  1252. assert(sock != -1);
  1253. do {
  1254. ufd.fd = sock;
  1255. ufd.events = POLLIN;
  1256. nfds = poll (&ufd, 1, 0);
  1257. if (nfds == 1 && ufd.revents & POLLIN) {
  1258. res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1259. if (res != -1) {
  1260. msg_processed = 1;
  1261. } else {
  1262. msg_processed = -1;
  1263. }
  1264. }
  1265. } while (nfds == 1);
  1266. }
  1267. return (msg_processed);
  1268. }