corosync  2.4.6
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, 1);
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  */
894  &sockaddr, &addrlen, 1);
895 
896  retries = 0;
897  while (1) {
898  res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
899  if (res == 0) {
900  break;
901  }
902  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
903  "Unable to bind UDP unicast socket");
904  if (++retries > BIND_MAX_RETRIES) {
905  break;
906  }
907 
908  /*
909  * Wait for a while
910  */
911  (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
912  }
913  if (res == -1) {
914  return (-1);
915  }
916 
917  recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
918  sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
919  /*
920  * Set buffer sizes to avoid overruns
921  */
922  res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
923  if (res == -1) {
924  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
925  "Unable to set SO_RCVBUF size on UDP mcast socket");
926  return (-1);
927  }
928  res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
929  if (res == -1) {
930  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
931  "Unable to set SO_SNDBUF size on UDP mcast socket");
932  return (-1);
933  }
934  res = setsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
935  if (res == -1) {
936  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
937  "Unable to set SO_RCVBUF size on UDP local mcast loop socket");
938  return (-1);
939  }
940  res = setsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
941  if (res == -1) {
942  LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
943  "Unable to set SO_SNDBUF size on UDP local mcast loop socket");
944  return (-1);
945  }
946 
947  res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
948  if (res == 0) {
950  "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
951  }
952 
953  res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
954  if (res == 0) {
956  "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
957  }
958 
959  res = getsockopt (sockets->local_mcast_loop[0], SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
960  if (res == 0) {
962  "Local receive multicast loop socket recv buffer size (%d bytes).", recvbuf_size);
963  }
964 
965  res = getsockopt (sockets->local_mcast_loop[1], SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
966  if (res == 0) {
968  "Local transmit multicast loop socket send buffer size (%d bytes).", sendbuf_size);
969  }
970 
971 
972  /*
973  * Join group membership on socket
974  */
975  totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
976  totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
977 
978  if (instance->totem_config->broadcast_use == 1) {
979  unsigned int broadcast = 1;
980 
981  if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
982  SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
983  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
984  "setting broadcast option failed");
985  return (-1);
986  }
987  if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
988  SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
989  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
990  "setting broadcast option failed");
991  return (-1);
992  }
993  } else {
994  switch (bindnet_address->family) {
995  case AF_INET:
996  memset(&mreq, 0, sizeof(mreq));
997  mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
998  mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
999  res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1000  &mreq, sizeof (mreq));
1001  if (res == -1) {
1002  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1003  "join ipv4 multicast group failed");
1004  return (-1);
1005  }
1006  break;
1007  case AF_INET6:
1008  memset(&mreq6, 0, sizeof(mreq6));
1009  memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
1010  mreq6.ipv6mr_interface = interface_num;
1011 
1012  res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1013  &mreq6, sizeof (mreq6));
1014  if (res == -1) {
1015  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1016  "join ipv6 multicast group failed");
1017  return (-1);
1018  }
1019  break;
1020  }
1021  }
1022 
1023  /*
1024  * Turn off multicast loopback
1025  */
1026 
1027  flag = 0;
1028  switch ( bindnet_address->family ) {
1029  case AF_INET:
1030  sflag = 0;
1031  res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
1032  &sflag, sizeof (sflag));
1033  break;
1034  case AF_INET6:
1035  res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1036  &flag, sizeof (flag));
1037  }
1038  if (res == -1) {
1039  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1040  "Unable to turn off multicast loopback");
1041  return (-1);
1042  }
1043 
1044  /*
1045  * Set multicast packets TTL
1046  */
1047  flag = instance->totem_interface->ttl;
1048  if (bindnet_address->family == AF_INET6) {
1049  res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
1050  &flag, sizeof (flag));
1051  if (res == -1) {
1052  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1053  "set mcast v6 TTL failed");
1054  return (-1);
1055  }
1056  } else {
1057  sflag = flag;
1058  res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
1059  &sflag, sizeof(sflag));
1060  if (res == -1) {
1061  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1062  "set mcast v4 TTL failed");
1063  return (-1);
1064  }
1065  }
1066 
1067  /*
1068  * Bind to a specific interface for multicast send and receive
1069  */
1070  switch ( bindnet_address->family ) {
1071  case AF_INET:
1072  if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
1073  &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
1074  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1075  "cannot select interface for multicast packets (send)");
1076  return (-1);
1077  }
1078  if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
1079  &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
1080  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1081  "cannot select interface for multicast packets (recv)");
1082  return (-1);
1083  }
1084  break;
1085  case AF_INET6:
1086  if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1087  &interface_num, sizeof (interface_num)) < 0) {
1088  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1089  "cannot select interface for multicast packets (send v6)");
1090  return (-1);
1091  }
1092  if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1093  &interface_num, sizeof (interface_num)) < 0) {
1094  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1095  "cannot select interface for multicast packets (recv v6)");
1096  return (-1);
1097  }
1098  break;
1099  }
1100 
1101  /*
1102  * Bind to multicast socket used for multicast receives
1103  * This needs to happen after all of the multicast setsockopt() calls
1104  * as the kernel seems to only put them into effect (for IPV6) when bind()
1105  * is called.
1106  */
1108  instance->totem_interface->ip_port, &sockaddr, &addrlen);
1109 
1110  retries = 0;
1111  while (1) {
1112  res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
1113  if (res == 0) {
1114  break;
1115  }
1116  LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
1117  "Unable to bind the socket to receive multicast packets");
1118  if (++retries > BIND_MAX_RETRIES) {
1119  break;
1120  }
1121 
1122  /*
1123  * Wait for a while
1124  */
1125  (void)poll(NULL, 0, BIND_RETRIES_INTERVAL * retries);
1126  }
1127 
1128  if (res == -1) {
1129  return (-1);
1130  }
1131  return 0;
1132 }
1133 
1134 static int totemudp_build_sockets (
1135  struct totemudp_instance *instance,
1136  struct totem_ip_address *mcast_address,
1137  struct totem_ip_address *bindnet_address,
1138  struct totemudp_socket *sockets,
1139  struct totem_ip_address *bound_to)
1140 {
1141  int interface_num;
1142  int interface_up;
1143  int res;
1144 
1145  /*
1146  * Determine the ip address bound to and the interface name
1147  */
1148  res = netif_determine (instance,
1149  bindnet_address,
1150  bound_to,
1151  &interface_up,
1152  &interface_num);
1153 
1154  if (res == -1) {
1155  return (-1);
1156  }
1157 
1158  totemip_copy(&instance->my_id, bound_to);
1159 
1160  res = totemudp_build_sockets_ip (instance, mcast_address,
1161  bindnet_address, sockets, bound_to, interface_num);
1162 
1163  if (res == -1) {
1164  /* if we get here, corosync won't work anyway, so better leaving than faking to work */
1165  LOGSYS_PERROR (errno, instance->totemudp_log_level_error,
1166  "Unable to create sockets, exiting");
1167  exit(EXIT_FAILURE);
1168  }
1169 
1170  /* We only send out of the token socket */
1171  totemudp_traffic_control_set(instance, sockets->token);
1172  return res;
1173 }
1174 
1175 /*
1176  * Totem Network interface - also does encryption/decryption
1177  * depends on poll abstraction, POSIX, IPV4
1178  */
1179 
1180 /*
1181  * Create an instance
1182  */
1184  qb_loop_t *poll_handle,
1185  void **udp_context,
1186  struct totem_config *totem_config,
1187  totemsrp_stats_t *stats,
1188  int interface_no,
1189  void *context,
1190 
1191  void (*deliver_fn) (
1192  void *context,
1193  const void *msg,
1194  unsigned int msg_len),
1195 
1196  void (*iface_change_fn) (
1197  void *context,
1198  const struct totem_ip_address *iface_address),
1199 
1200  void (*target_set_completed) (
1201  void *context))
1202 {
1203  struct totemudp_instance *instance;
1204 
1205  instance = malloc (sizeof (struct totemudp_instance));
1206  if (instance == NULL) {
1207  return (-1);
1208  }
1209 
1210  totemudp_instance_initialize (instance);
1211 
1212  instance->totem_config = totem_config;
1213  instance->stats = stats;
1214 
1215  /*
1216  * Configure logging
1217  */
1218  instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
1225 
1226  /*
1227  * Initialize random number generator for later use to generate salt
1228  */
1229  instance->crypto_inst = crypto_init (totem_config->private_key,
1230  totem_config->private_key_len,
1231  totem_config->crypto_cipher_type,
1232  totem_config->crypto_hash_type,
1233  instance->totemudp_log_printf,
1234  instance->totemudp_log_level_security,
1235  instance->totemudp_log_level_notice,
1236  instance->totemudp_log_level_error,
1237  instance->totemudp_subsys_id);
1238  if (instance->crypto_inst == NULL) {
1239  free(instance);
1240  return (-1);
1241  }
1242  /*
1243  * Initialize local variables for totemudp
1244  */
1245  instance->totem_interface = &totem_config->interfaces[interface_no];
1246  totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
1247  memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
1248 
1249  instance->totemudp_poll_handle = poll_handle;
1250 
1251  instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
1252 
1253  instance->context = context;
1254  instance->totemudp_deliver_fn = deliver_fn;
1255 
1256  instance->totemudp_iface_change_fn = iface_change_fn;
1257 
1258  instance->totemudp_target_set_completed = target_set_completed;
1259 
1260  totemip_localhost (instance->mcast_address.family, &localhost);
1261  localhost.nodeid = instance->totem_config->node_id;
1262 
1263  /*
1264  * RRP layer isn't ready to receive message because it hasn't
1265  * initialized yet. Add short timer to check the interfaces.
1266  */
1267  qb_loop_timer_add (instance->totemudp_poll_handle,
1268  QB_LOOP_MED,
1269  100*QB_TIME_NS_IN_MSEC,
1270  (void *)instance,
1271  timer_function_netif_check_timeout,
1272  &instance->timer_netif_check_timeout);
1273 
1274  *udp_context = instance;
1275  return (0);
1276 }
1277 
1279 {
1280  return malloc (FRAME_SIZE_MAX);
1281 }
1282 
1283 void totemudp_buffer_release (void *ptr)
1284 {
1285  return free (ptr);
1286 }
1287 
1289  void *udp_context,
1290  int processor_count)
1291 {
1292  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1293  int res = 0;
1294 
1295  instance->my_memb_entries = processor_count;
1296  qb_loop_timer_del (instance->totemudp_poll_handle,
1297  instance->timer_netif_check_timeout);
1298  if (processor_count == 1) {
1299  qb_loop_timer_add (instance->totemudp_poll_handle,
1300  QB_LOOP_MED,
1301  instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
1302  (void *)instance,
1303  timer_function_netif_check_timeout,
1304  &instance->timer_netif_check_timeout);
1305  }
1306 
1307  return (res);
1308 }
1309 
1310 int totemudp_recv_flush (void *udp_context)
1311 {
1312  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1313  struct pollfd ufd;
1314  int nfds;
1315  int res = 0;
1316  int i;
1317  int sock;
1318 
1319  instance->flushing = 1;
1320 
1321  for (i = 0; i < 2; i++) {
1322  sock = -1;
1323  if (i == 0) {
1324  sock = instance->totemudp_sockets.mcast_recv;
1325  }
1326  if (i == 1) {
1327  sock = instance->totemudp_sockets.local_mcast_loop[0];
1328  }
1329  assert(sock != -1);
1330 
1331  do {
1332  ufd.fd = sock;
1333  ufd.events = POLLIN;
1334  nfds = poll (&ufd, 1, 0);
1335  if (nfds == 1 && ufd.revents & POLLIN) {
1336  net_deliver_fn (sock, ufd.revents, instance);
1337  }
1338  } while (nfds == 1);
1339  }
1340 
1341  instance->flushing = 0;
1342 
1343  return (res);
1344 }
1345 
1346 int totemudp_send_flush (void *udp_context)
1347 {
1348  return 0;
1349 }
1350 
1352  void *udp_context,
1353  const void *msg,
1354  unsigned int msg_len)
1355 {
1356  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1357  int res = 0;
1358 
1359  ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
1360 
1361  return (res);
1362 }
1364  void *udp_context,
1365  const void *msg,
1366  unsigned int msg_len)
1367 {
1368  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1369  int res = 0;
1370 
1371  mcast_sendmsg (instance, msg, msg_len);
1372 
1373  return (res);
1374 }
1375 
1377  void *udp_context,
1378  const void *msg,
1379  unsigned int msg_len)
1380 {
1381  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1382  int res = 0;
1383 
1384  mcast_sendmsg (instance, msg, msg_len);
1385 
1386  return (res);
1387 }
1388 
1389 extern int totemudp_iface_check (void *udp_context)
1390 {
1391  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1392  int res = 0;
1393 
1394  timer_function_netif_check_timeout (instance);
1395 
1396  return (res);
1397 }
1398 
1399 extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
1400 {
1401 
1402  assert(totem_config->interface_count > 0);
1403 
1404  totem_config->net_mtu -= crypto_sec_header_size(totem_config->crypto_cipher_type,
1405  totem_config->crypto_hash_type) +
1407 }
1408 
1409 const char *totemudp_iface_print (void *udp_context) {
1410  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1411  const char *ret_char;
1412 
1413  ret_char = totemip_print (&instance->my_id);
1414 
1415  return (ret_char);
1416 }
1417 
1419  void *udp_context,
1420  struct totem_ip_address *addr)
1421 {
1422  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1423  int res = 0;
1424 
1425  memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
1426 
1427  return (res);
1428 }
1429 
1431  void *udp_context,
1432  const struct totem_ip_address *token_target)
1433 {
1434  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1435  int res = 0;
1436 
1437  memcpy (&instance->token_target, token_target,
1438  sizeof (struct totem_ip_address));
1439 
1440  instance->totemudp_target_set_completed (instance->context);
1441 
1442  return (res);
1443 }
1444 
1446  void *udp_context)
1447 {
1448  struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
1449  unsigned int res;
1450  struct sockaddr_storage system_from;
1451  struct msghdr msg_recv;
1452  struct pollfd ufd;
1453  int nfds;
1454  int msg_processed = 0;
1455  int i;
1456  int sock;
1457 
1458  /*
1459  * Receive datagram
1460  */
1461  msg_recv.msg_name = &system_from;
1462  msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
1463  msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
1464  msg_recv.msg_iovlen = 1;
1465 #ifdef HAVE_MSGHDR_CONTROL
1466  msg_recv.msg_control = 0;
1467 #endif
1468 #ifdef HAVE_MSGHDR_CONTROLLEN
1469  msg_recv.msg_controllen = 0;
1470 #endif
1471 #ifdef HAVE_MSGHDR_FLAGS
1472  msg_recv.msg_flags = 0;
1473 #endif
1474 #ifdef HAVE_MSGHDR_ACCRIGHTS
1475  msg_recv.msg_accrights = NULL;
1476 #endif
1477 #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
1478  msg_recv.msg_accrightslen = 0;
1479 #endif
1480 
1481  for (i = 0; i < 2; i++) {
1482  sock = -1;
1483  if (i == 0) {
1484  sock = instance->totemudp_sockets.mcast_recv;
1485  }
1486  if (i == 1) {
1487  sock = instance->totemudp_sockets.local_mcast_loop[0];
1488  }
1489  assert(sock != -1);
1490 
1491  do {
1492  ufd.fd = sock;
1493  ufd.events = POLLIN;
1494  nfds = poll (&ufd, 1, 0);
1495  if (nfds == 1 && ufd.revents & POLLIN) {
1496  res = recvmsg (sock, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
1497  if (res != -1) {
1498  msg_processed = 1;
1499  } else {
1500  msg_processed = -1;
1501  }
1502  }
1503  } while (nfds == 1);
1504  }
1505 
1506  return (msg_processed);
1507 }
1508 
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:1288
int totemip_localhost(int family, struct totem_ip_address *localhost)
Definition: totemip.c:210
#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:801
The totem_ip_address struct.
Definition: coroapi.h:111
const char * totemip_print(const struct totem_ip_address *addr)
Definition: totemip.c:264
void(* totemudp_iface_change_fn)(void *context, const struct totem_ip_address *iface_address)
Definition: totemudp.c:122
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:955
size_t crypto_get_current_sec_header_size(const struct crypto_instance *instance)
Definition: totemcrypto.c:836
struct timeval stats_tv_start
Definition: totemudp.c:176
void(* totemudp_deliver_fn)(void *context, const void *msg, unsigned int msg_len)
Definition: totemudp.c:117
void totemudp_net_mtu_adjust(void *udp_context, struct totem_config *totem_config)
Definition: totemudp.c:1399
void * totemudp_buffer_alloc(void)
Definition: totemudp.c:1278
int totemip_totemip_to_sockaddr_convert_with_scopeid(const struct totem_ip_address *ip_addr, uint16_t port, struct sockaddr_storage *saddr, int *addrlen, int fill_scopeid)
Definition: totemip.c:317
int totemudp_mcast_flush_send(void *udp_context, const void *msg, unsigned int msg_len)
Definition: totemudp.c:1363
int totemudp_token_target_set(void *udp_context, const struct totem_ip_address *token_target)
Definition: totemudp.c:1430
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
void totemip_copy(struct totem_ip_address *addr1, const struct totem_ip_address *addr2)
Definition: totemip.c:123
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:1376
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:1346
uint16_t ttl
Definition: totem.h:72
int totemudp_recv_flush(void *udp_context)
Definition: totemudp.c:1310
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:515
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:857
int totemudp_token_send(void *udp_context, const void *msg, unsigned int msg_len)
Definition: totemudp.c:1351
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:1183
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(* 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
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:606
const char * totemudp_iface_print(void *udp_context)
Definition: totemudp.c:1409
#define NETIF_STATE_REPORT_UP
Definition: totemudp.c:84
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
uint16_t ip_port
Definition: totem.h:71
#define MCAST_SOCKET_BUFFER_SIZE
Definition: totemudp.c:83
void(*) void udp_context)
Definition: totemudp.c:150
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:879
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:1418
#define FRAME_SIZE_MAX
Definition: totem.h:50
#define LOGSYS_LEVEL_CRIT
Definition: logsys.h:69
void(* totemudp_target_set_completed)(void *context)
Definition: totemudp.c:126
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:362
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
int totemudp_recv_mcast_empty(void *udp_context)
Definition: totemudp.c:1445
int totemudp_iface_check(void *udp_context)
Definition: totemudp.c:1389
struct totem_ip_address bindnet
Definition: totem.h:68
uint32_t continuous_sendmsg_failures
Definition: totem.h:276
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:1283
int totemudp_log_level_warning
Definition: totemudp.c:135