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