2 * Copyright (c) 2005 MontaVista Software, Inc.
3 * Copyright (c) 2006 Red Hat, Inc.
4 * Copyright (c) 2006 Sun Microsystems, Inc.
8 * Author: Steven Dake (sdake@mvista.com)
10 * This software licensed under BSD license, the text of which follows:
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
15 * - Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * - Neither the name of the MontaVista Software, Inc. nor the names of its
21 * contributors may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34 * THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
42 #include <sys/socket.h>
45 #include <sys/ioctl.h>
46 #include <sys/param.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
64 #include "../include/queue.h"
65 #include "../include/sq.h"
66 #include "../include/list.h"
67 #include "../include/hdb.h"
72 #ifdef OPENAIS_SOLARIS
73 #define MSG_NOSIGNAL 0
76 #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
78 #define NETIF_STATE_REPORT_UP 1
79 #define NETIF_STATE_REPORT_DOWN 2
81 #define BIND_STATE_UNBOUND 0
82 #define BIND_STATE_REGULAR 1
83 #define BIND_STATE_LOOPBACK 2
85 #define HMAC_HASH_SIZE 20
86 struct security_header
{
87 unsigned char hash_digest
[HMAC_HASH_SIZE
]; /* The hash *MUST* be first in the data structure */
88 unsigned char salt
[16]; /* random number */
90 } __attribute__((packed
));
92 struct totemnet_mcast_thread_state
{
93 unsigned char iobuf
[FRAME_SIZE_MAX
];
94 prng_state prng_state
;
97 struct totemnet_socket
{
103 struct totemnet_instance
{
104 hmac_state totemnet_hmac_state
;
106 prng_state totemnet_prng_state
;
108 unsigned char totemnet_private_key
[1024];
110 unsigned int totemnet_private_key_len
;
112 poll_handle totemnet_poll_handle
;
114 struct totem_interface
*totem_interface
;
116 int netif_state_report
;
118 int netif_bind_state
;
120 struct worker_thread_group worker_thread_group
;
124 void (*totemnet_deliver_fn
) (
129 void (*totemnet_iface_change_fn
) (
131 struct totem_ip_address
*iface_address
);
134 * Function and data used to log messages
136 int totemnet_log_level_security
;
138 int totemnet_log_level_error
;
140 int totemnet_log_level_warning
;
142 int totemnet_log_level_notice
;
144 int totemnet_log_level_debug
;
146 void (*totemnet_log_printf
) (char *file
, int line
, int level
, char *format
, ...) __attribute__((format(printf
, 4, 5)));
148 totemnet_handle handle
;
150 char iov_buffer
[FRAME_SIZE_MAX
];
152 char iov_buffer_flush
[FRAME_SIZE_MAX
];
154 struct iovec totemnet_iov_recv
;
156 struct iovec totemnet_iov_recv_flush
;
158 struct totemnet_socket totemnet_sockets
;
160 struct totem_ip_address mcast_address
;
172 struct timeval stats_tv_start
;
174 struct totem_ip_address my_id
;
178 poll_timer_handle timer_netif_check_timeout
;
180 unsigned int my_memb_entries
;
184 struct totem_config
*totem_config
;
186 struct totem_ip_address token_target
;
190 struct iovec iovec
[20];
192 struct totemnet_instance
*instance
;
195 static void netif_down_check (struct totemnet_instance
*instance
);
197 static int totemnet_build_sockets (
198 struct totemnet_instance
*instance
,
199 struct totem_ip_address
*bindnet_address
,
200 struct totem_ip_address
*mcastaddress
,
201 struct totemnet_socket
*sockets
,
202 struct totem_ip_address
*bound_to
);
204 static struct totem_ip_address localhost
;
207 * All instances in one database
209 static struct hdb_handle_database totemnet_instance_database
= {
213 .mutex
= PTHREAD_MUTEX_INITIALIZER
216 static void totemnet_instance_initialize (struct totemnet_instance
*instance
)
218 memset (instance
, 0, sizeof (struct totemnet_instance
));
220 instance
->netif_state_report
= NETIF_STATE_REPORT_UP
| NETIF_STATE_REPORT_DOWN
;
222 instance
->totemnet_iov_recv
.iov_base
= instance
->iov_buffer
;
224 instance
->totemnet_iov_recv
.iov_len
= FRAME_SIZE_MAX
; //sizeof (instance->iov_buffer);
225 instance
->totemnet_iov_recv_flush
.iov_base
= instance
->iov_buffer_flush
;
227 instance
->totemnet_iov_recv_flush
.iov_len
= FRAME_SIZE_MAX
; //sizeof (instance->iov_buffer);
230 * There is always atleast 1 processor
232 instance
->my_memb_entries
= 1;
235 #define log_printf(level, format, args...) \
236 instance->totemnet_log_printf (__FILE__, __LINE__, level, format, ##args)
238 static int authenticate_and_decrypt (
239 struct totemnet_instance
*instance
,
242 unsigned char keys
[48];
243 struct security_header
*header
=
244 (struct security_header
*)iov
[0].iov_base
;
245 prng_state keygen_prng_state
;
246 prng_state stream_prng_state
;
247 unsigned char *hmac_key
= &keys
[32];
248 unsigned char *cipher_key
= &keys
[16];
249 unsigned char *initial_vector
= &keys
[0];
250 unsigned char digest_comparison
[HMAC_HASH_SIZE
];
254 * Generate MAC, CIPHER, IV keys from private key
256 memset (keys
, 0, sizeof (keys
));
257 sober128_start (&keygen_prng_state
);
258 sober128_add_entropy (instance
->totemnet_private_key
,
259 instance
->totemnet_private_key_len
, &keygen_prng_state
);
260 sober128_add_entropy (header
->salt
, sizeof (header
->salt
), &keygen_prng_state
);
262 sober128_read (keys
, sizeof (keys
), &keygen_prng_state
);
265 * Setup stream cipher
267 sober128_start (&stream_prng_state
);
268 sober128_add_entropy (cipher_key
, 16, &stream_prng_state
);
269 sober128_add_entropy (initial_vector
, 16, &stream_prng_state
);
272 * Authenticate contents of message
274 hmac_init (&instance
->totemnet_hmac_state
, DIGEST_SHA1
, hmac_key
, 16);
276 hmac_process (&instance
->totemnet_hmac_state
,
277 (unsigned char *)iov
->iov_base
+ HMAC_HASH_SIZE
,
278 iov
->iov_len
- HMAC_HASH_SIZE
);
280 len
= hash_descriptor
[DIGEST_SHA1
]->hashsize
;
281 assert (HMAC_HASH_SIZE
>= len
);
282 hmac_done (&instance
->totemnet_hmac_state
, digest_comparison
, &len
);
284 if (memcmp (digest_comparison
, header
->hash_digest
, len
) != 0) {
285 log_printf (instance
->totemnet_log_level_security
, "Received message has invalid digest... ignoring.\n");
290 * Decrypt the contents of the message with the cipher key
292 assert(iov
->iov_len
>= sizeof (struct security_header
));
294 (unsigned char *)iov
->iov_base
+ sizeof (struct security_header
),
295 (unsigned long)iov
->iov_len
- sizeof (struct security_header
),
300 static void encrypt_and_sign_worker (
301 struct totemnet_instance
*instance
,
306 prng_state
*prng_state_in
)
310 unsigned char keys
[48];
311 struct security_header
*header
;
312 unsigned char *hmac_key
= &keys
[32];
313 unsigned char *cipher_key
= &keys
[16];
314 unsigned char *initial_vector
= &keys
[0];
317 hmac_state hmac_state
;
318 prng_state keygen_prng_state
;
319 prng_state stream_prng_state
;
321 header
= (struct security_header
*)buf
;
322 addr
= buf
+ sizeof (struct security_header
);
324 memset (keys
, 0, sizeof (keys
));
325 memset (header
->salt
, 0, sizeof (header
->salt
));
328 * Generate MAC, CIPHER, IV keys from private key
330 sober128_read (header
->salt
, sizeof (header
->salt
), prng_state_in
);
331 sober128_start (&keygen_prng_state
);
332 sober128_add_entropy (instance
->totemnet_private_key
,
333 instance
->totemnet_private_key_len
,
335 sober128_add_entropy (header
->salt
, sizeof (header
->salt
),
338 sober128_read (keys
, sizeof (keys
), &keygen_prng_state
);
341 * Setup stream cipher
343 sober128_start (&stream_prng_state
);
344 sober128_add_entropy (cipher_key
, 16, &stream_prng_state
);
345 sober128_add_entropy (initial_vector
, 16, &stream_prng_state
);
347 outlen
= sizeof (struct security_header
);
349 * Copy remainder of message, then encrypt it
351 for (i
= 1; i
< iov_len
; i
++) {
352 memcpy (addr
, iovec
[i
].iov_base
, iovec
[i
].iov_len
);
353 addr
+= iovec
[i
].iov_len
;
354 outlen
+= iovec
[i
].iov_len
;
358 * Encrypt message by XORing stream cipher data
360 sober128_read (buf
+ sizeof (struct security_header
),
361 outlen
- sizeof (struct security_header
),
364 memset (&hmac_state
, 0, sizeof (hmac_state
));
367 * Sign the contents of the message with the hmac key and store signature in message
369 hmac_init (&hmac_state
, DIGEST_SHA1
, hmac_key
, 16);
371 hmac_process (&hmac_state
,
372 buf
+ HMAC_HASH_SIZE
,
373 outlen
- HMAC_HASH_SIZE
);
375 len
= hash_descriptor
[DIGEST_SHA1
]->hashsize
;
377 hmac_done (&hmac_state
, header
->hash_digest
, &len
);
382 static inline void ucast_sendmsg (
383 struct totemnet_instance
*instance
,
384 struct totem_ip_address
*system_to
,
385 struct iovec
*iovec_in
,
388 struct msghdr msg_ucast
;
391 unsigned char sheader
[sizeof (struct security_header
)];
392 unsigned char encrypt_data
[FRAME_SIZE_MAX
];
393 struct iovec iovec_encrypt
[20];
394 struct iovec
*iovec_sendmsg
;
395 struct sockaddr_storage sockaddr
;
399 if (instance
->totem_config
->secauth
== 1) {
401 iovec_encrypt
[0].iov_base
= (char *)sheader
;
402 iovec_encrypt
[0].iov_len
= sizeof (struct security_header
);
403 memcpy (&iovec_encrypt
[1], &iovec_in
[0],
404 sizeof (struct iovec
) * iov_len_in
);
407 * Encrypt and digest the message
409 encrypt_and_sign_worker (
415 &instance
->totemnet_prng_state
);
417 iovec_encrypt
[0].iov_base
= (char *)encrypt_data
;
418 iovec_encrypt
[0].iov_len
= buf_len
;
419 iovec_sendmsg
= &iovec_encrypt
[0];
422 iovec_sendmsg
= iovec_in
;
423 iov_len
= iov_len_in
;
427 * Build unicast message
429 totemip_totemip_to_sockaddr_convert(system_to
,
430 instance
->totem_interface
->ip_port
, &sockaddr
, &addrlen
);
431 msg_ucast
.msg_name
= &sockaddr
;
432 msg_ucast
.msg_namelen
= addrlen
;
433 msg_ucast
.msg_iov
= iovec_sendmsg
;
434 msg_ucast
.msg_iovlen
= iov_len
;
435 #ifndef OPENAIS_SOLARIS
436 msg_ucast
.msg_control
= 0;
437 msg_ucast
.msg_controllen
= 0;
438 msg_ucast
.msg_flags
= 0;
440 msg_ucast
.msg_accrights
= NULL
;
441 msg_ucast
.msg_accrightslen
= 0;
445 * Transmit multicast message
446 * An error here is recovered by totemsrp
448 res
= sendmsg (instance
->totemnet_sockets
.mcast_send
, &msg_ucast
,
452 static inline void mcast_sendmsg (
453 struct totemnet_instance
*instance
,
454 struct iovec
*iovec_in
,
457 struct msghdr msg_mcast
;
460 unsigned char sheader
[sizeof (struct security_header
)];
461 unsigned char encrypt_data
[FRAME_SIZE_MAX
];
462 struct iovec iovec_encrypt
[20];
463 struct iovec
*iovec_sendmsg
;
464 struct sockaddr_storage sockaddr
;
468 if (instance
->totem_config
->secauth
== 1) {
470 iovec_encrypt
[0].iov_base
= (char *)sheader
;
471 iovec_encrypt
[0].iov_len
= sizeof (struct security_header
);
472 memcpy (&iovec_encrypt
[1], &iovec_in
[0],
473 sizeof (struct iovec
) * iov_len_in
);
476 * Encrypt and digest the message
478 encrypt_and_sign_worker (
484 &instance
->totemnet_prng_state
);
486 iovec_encrypt
[0].iov_base
= (char *)encrypt_data
;
487 iovec_encrypt
[0].iov_len
= buf_len
;
488 iovec_sendmsg
= &iovec_encrypt
[0];
491 iovec_sendmsg
= iovec_in
;
492 iov_len
= iov_len_in
;
496 * Build multicast message
498 totemip_totemip_to_sockaddr_convert(&instance
->mcast_address
,
499 instance
->totem_interface
->ip_port
, &sockaddr
, &addrlen
);
500 msg_mcast
.msg_name
= &sockaddr
;
501 msg_mcast
.msg_namelen
= addrlen
;
502 msg_mcast
.msg_iov
= iovec_sendmsg
;
503 msg_mcast
.msg_iovlen
= iov_len
;
504 #ifndef OPENAIS_SOLARIS
505 msg_mcast
.msg_control
= 0;
506 msg_mcast
.msg_controllen
= 0;
507 msg_mcast
.msg_flags
= 0;
509 msg_mcast
.msg_accrights
= NULL
;
510 msg_mcast
.msg_accrightslen
= 0;
514 * Transmit multicast message
515 * An error here is recovered by totemsrp
517 res
= sendmsg (instance
->totemnet_sockets
.mcast_send
, &msg_mcast
,
521 static void totemnet_mcast_thread_state_constructor (
522 void *totemnet_mcast_thread_state_in
)
524 struct totemnet_mcast_thread_state
*totemnet_mcast_thread_state
=
525 (struct totemnet_mcast_thread_state
*)totemnet_mcast_thread_state_in
;
526 memset (totemnet_mcast_thread_state
, 0,
527 sizeof (totemnet_mcast_thread_state
));
529 rng_make_prng (128, PRNG_SOBER
,
530 &totemnet_mcast_thread_state
->prng_state
, NULL
);
534 static void totemnet_mcast_worker_fn (void *thread_state
, void *work_item_in
)
536 struct work_item
*work_item
= (struct work_item
*)work_item_in
;
537 struct totemnet_mcast_thread_state
*totemnet_mcast_thread_state
=
538 (struct totemnet_mcast_thread_state
*)thread_state
;
539 struct totemnet_instance
*instance
= work_item
->instance
;
540 struct msghdr msg_mcast
;
541 unsigned char sheader
[sizeof (struct security_header
)];
544 struct iovec iovec_encrypted
;
545 struct iovec
*iovec_sendmsg
;
546 struct sockaddr_storage sockaddr
;
550 if (instance
->totem_config
->secauth
== 1) {
551 memmove (&work_item
->iovec
[1], &work_item
->iovec
[0],
552 work_item
->iov_len
* sizeof (struct iovec
));
553 work_item
->iovec
[0].iov_base
= (char *)sheader
;
554 work_item
->iovec
[0].iov_len
= sizeof (struct security_header
);
557 * Encrypt and digest the message
559 encrypt_and_sign_worker (
561 totemnet_mcast_thread_state
->iobuf
, &buf_len
,
562 work_item
->iovec
, work_item
->iov_len
+ 1,
563 &totemnet_mcast_thread_state
->prng_state
);
565 iovec_sendmsg
= &iovec_encrypted
;
566 iovec_sendmsg
->iov_base
= (char *)totemnet_mcast_thread_state
->iobuf
;
567 iovec_sendmsg
->iov_len
= buf_len
;
570 iovec_sendmsg
= work_item
->iovec
;
571 iovs
= work_item
->iov_len
;
574 totemip_totemip_to_sockaddr_convert(&instance
->mcast_address
,
575 instance
->totem_interface
->ip_port
, &sockaddr
, &addrlen
);
577 msg_mcast
.msg_name
= &sockaddr
;
578 msg_mcast
.msg_namelen
= addrlen
;
579 msg_mcast
.msg_iov
= iovec_sendmsg
;
580 msg_mcast
.msg_iovlen
= iovs
;
581 #ifndef OPENAIS_SOLARIS
582 msg_mcast
.msg_control
= 0;
583 msg_mcast
.msg_controllen
= 0;
584 msg_mcast
.msg_flags
= 0;
586 msg_mcast
.msg_accrights
= NULL
;
587 msg_mcast
.msg_accrightslen
= 0;
591 * Transmit multicast message
592 * An error here is recovered by totemnet
594 res
= sendmsg (instance
->totemnet_sockets
.mcast_send
, &msg_mcast
,
597 instance
->stats_sent
+= res
;
601 int totemnet_finalize (
602 totemnet_handle handle
)
604 struct totemnet_instance
*instance
;
607 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
614 worker_thread_group_exit (&instance
->worker_thread_group
);
616 hdb_handle_put (&totemnet_instance_database
, handle
);
623 * Only designed to work with a message with one iov
626 static int net_deliver_fn (
632 struct totemnet_instance
*instance
= (struct totemnet_instance
*)data
;
633 struct msghdr msg_recv
;
635 struct security_header
*security_header
;
636 struct sockaddr_storage system_from
;
640 unsigned int size_delv
;
642 if (instance
->flushing
== 1) {
643 iovec
= &instance
->totemnet_iov_recv_flush
;
645 iovec
= &instance
->totemnet_iov_recv
;
651 msg_recv
.msg_name
= &system_from
;
652 msg_recv
.msg_namelen
= sizeof (struct sockaddr_storage
);
653 msg_recv
.msg_iov
= iovec
;
654 msg_recv
.msg_iovlen
= 1;
655 #ifndef OPENAIS_SOLARIS
656 msg_recv
.msg_control
= 0;
657 msg_recv
.msg_controllen
= 0;
658 msg_recv
.msg_flags
= 0;
660 msg_recv
.msg_accrights
= NULL
;
661 msg_recv
.msg_accrightslen
= 0;
664 bytes_received
= recvmsg (fd
, &msg_recv
, MSG_NOSIGNAL
| MSG_DONTWAIT
);
665 if (bytes_received
== -1) {
668 instance
->stats_recv
+= bytes_received
;
671 if ((instance
->totem_config
->secauth
== 1) &&
672 (bytes_received
< sizeof (struct security_header
))) {
674 log_printf (instance
->totemnet_log_level_security
, "Received message is too short... ignoring %d.\n", bytes_received
);
678 security_header
= (struct security_header
*)iovec
->iov_base
;
680 iovec
->iov_len
= bytes_received
;
681 if (instance
->totem_config
->secauth
== 1) {
683 * Authenticate and if authenticated, decrypt datagram
686 res
= authenticate_and_decrypt (instance
, iovec
);
688 log_printf (instance
->totemnet_log_level_security
,
689 "Invalid packet data\n");
690 iovec
->iov_len
= FRAME_SIZE_MAX
;
693 msg_offset
= iovec
->iov_base
+
694 sizeof (struct security_header
);
695 size_delv
= bytes_received
- sizeof (struct security_header
);
697 msg_offset
= iovec
->iov_base
;
698 size_delv
= bytes_received
;
702 * Handle incoming message
704 instance
->totemnet_deliver_fn (
709 iovec
->iov_len
= FRAME_SIZE_MAX
;
713 static int netif_determine (
714 struct totemnet_instance
*instance
,
715 struct totem_ip_address
*bindnet
,
716 struct totem_ip_address
*bound_to
,
722 res
= totemip_iface_check (bindnet
, bound_to
,
723 interface_up
, interface_num
);
726 * If the desired binding is to an IPV4 network and nodeid isn't
727 * specified, retrieve the node id from this_ip network address
729 * IPV6 networks must have a node ID specified since the node id
730 * field is only 32 bits.
732 if (bound_to
->family
== AF_INET
&& bound_to
->nodeid
== 0) {
733 bound_to
->nodeid
= totemip_compute_nodeid_from_addr(bound_to
);
741 * If the interface is up, the sockets for totem are built. If the interface is down
742 * this function is requeued in the timer list to retry building the sockets later.
744 static void timer_function_netif_check_timeout (
747 struct totemnet_instance
*instance
= (struct totemnet_instance
*)data
;
751 struct totem_ip_address
*bind_address
;
754 * Build sockets for every interface
756 netif_determine (instance
,
757 &instance
->totem_interface
->bindnet
,
758 &instance
->totem_interface
->boundto
,
759 &interface_up
, &interface_num
);
761 * If the network interface isn't back up and we are already
762 * in loopback mode, add timer to check again and return
764 if ((instance
->netif_bind_state
== BIND_STATE_LOOPBACK
&&
765 interface_up
== 0) ||
767 (instance
->my_memb_entries
== 1 &&
768 instance
->netif_bind_state
== BIND_STATE_REGULAR
&&
769 interface_up
== 1)) {
771 poll_timer_add (instance
->totemnet_poll_handle
,
772 instance
->totem_config
->downcheck_timeout
,
774 timer_function_netif_check_timeout
,
775 &instance
->timer_netif_check_timeout
);
778 * Add a timer to check for a downed regular interface
783 if (instance
->totemnet_sockets
.mcast_recv
> 0) {
784 close (instance
->totemnet_sockets
.mcast_recv
);
785 poll_dispatch_delete (instance
->totemnet_poll_handle
,
786 instance
->totemnet_sockets
.mcast_recv
);
788 if (instance
->totemnet_sockets
.mcast_send
> 0) {
789 close (instance
->totemnet_sockets
.mcast_send
);
791 if (instance
->totemnet_sockets
.token
> 0) {
792 close (instance
->totemnet_sockets
.token
);
793 poll_dispatch_delete (instance
->totemnet_poll_handle
,
794 instance
->totemnet_sockets
.token
);
797 if (interface_up
== 0) {
799 * Interface is not up
801 instance
->netif_bind_state
= BIND_STATE_LOOPBACK
;
802 bind_address
= &localhost
;
805 * Add a timer to retry building interfaces and request memb_gather_enter
807 poll_timer_add (instance
->totemnet_poll_handle
,
808 instance
->totem_config
->downcheck_timeout
,
810 timer_function_netif_check_timeout
,
811 &instance
->timer_netif_check_timeout
);
816 instance
->netif_bind_state
= BIND_STATE_REGULAR
;
817 bind_address
= &instance
->totem_interface
->bindnet
;
820 * Create and bind the multicast and unicast sockets
822 res
= totemnet_build_sockets (instance
,
823 &instance
->mcast_address
,
825 &instance
->totemnet_sockets
,
826 &instance
->totem_interface
->boundto
);
829 instance
->totemnet_poll_handle
,
830 instance
->totemnet_sockets
.mcast_recv
,
831 POLLIN
, instance
, net_deliver_fn
);
834 instance
->totemnet_poll_handle
,
835 instance
->totemnet_sockets
.token
,
836 POLLIN
, instance
, net_deliver_fn
);
838 totemip_copy (&instance
->my_id
, &instance
->totem_interface
->boundto
);
841 * This reports changes in the interface to the user and totemsrp
843 if (instance
->netif_bind_state
== BIND_STATE_REGULAR
) {
844 if (instance
->netif_state_report
& NETIF_STATE_REPORT_UP
) {
845 log_printf (instance
->totemnet_log_level_notice
,
846 "The network interface [%s] is now up.\n",
847 totemip_print (&instance
->totem_interface
->boundto
));
848 instance
->netif_state_report
= NETIF_STATE_REPORT_DOWN
;
849 instance
->totemnet_iface_change_fn (instance
->context
, &instance
->my_id
);
852 * Add a timer to check for interface going down in single membership
854 if (instance
->my_memb_entries
== 1) {
855 poll_timer_add (instance
->totemnet_poll_handle
,
856 instance
->totem_config
->downcheck_timeout
,
858 timer_function_netif_check_timeout
,
859 &instance
->timer_netif_check_timeout
);
863 if (instance
->netif_state_report
& NETIF_STATE_REPORT_DOWN
) {
864 log_printf (instance
->totemnet_log_level_notice
,
865 "The network interface is down.\n");
866 instance
->totemnet_iface_change_fn (instance
->context
, &instance
->my_id
);
868 instance
->netif_state_report
= NETIF_STATE_REPORT_UP
;
875 * Check if an interface is down and reconfigure
876 * totemnet waiting for it to come back up
878 static void netif_down_check (struct totemnet_instance
*instance
)
880 timer_function_netif_check_timeout (instance
);
883 /* Set the socket priority to INTERACTIVE to ensure
884 that our messages don't get queued behind anything else */
885 static void totemnet_traffic_control_set(struct totemnet_instance
*instance
, int sock
)
888 int prio
= 6; /* TC_PRIO_INTERACTIVE */
890 if (setsockopt(sock
, SOL_SOCKET
, SO_PRIORITY
, &prio
, sizeof(int)))
891 log_printf (instance
->totemnet_log_level_warning
, "Could not set traffic priority. (%s)\n", strerror (errno
));
895 static int totemnet_build_sockets_ip (
896 struct totemnet_instance
*instance
,
897 struct totem_ip_address
*mcast_address
,
898 struct totem_ip_address
*bindnet_address
,
899 struct totemnet_socket
*sockets
,
900 struct totem_ip_address
*bound_to
,
903 struct sockaddr_storage sockaddr
;
904 struct ipv6_mreq mreq6
;
906 struct sockaddr_storage mcast_ss
, boundto_ss
;
907 struct sockaddr_in6
*mcast_sin6
= (struct sockaddr_in6
*)&mcast_ss
;
908 struct sockaddr_in
*mcast_sin
= (struct sockaddr_in
*)&mcast_ss
;
909 struct sockaddr_in
*boundto_sin
= (struct sockaddr_in
*)&boundto_ss
;
910 unsigned int sendbuf_size
;
911 unsigned int recvbuf_size
;
912 unsigned int optlen
= sizeof (sendbuf_size
);
918 * Create multicast recv socket
920 sockets
->mcast_recv
= socket (bindnet_address
->family
, SOCK_DGRAM
, 0);
921 if (sockets
->mcast_recv
== -1) {
926 totemip_nosigpipe (sockets
->mcast_recv
);
927 res
= fcntl (sockets
->mcast_recv
, F_SETFL
, O_NONBLOCK
);
929 log_printf (instance
->totemnet_log_level_warning
, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno
));
937 if ( setsockopt(sockets
->mcast_recv
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&flag
, sizeof (flag
)) < 0) {
938 perror("setsockopt reuseaddr");
943 * Bind to multicast socket used for multicast receives
945 totemip_totemip_to_sockaddr_convert(mcast_address
,
946 instance
->totem_interface
->ip_port
, &sockaddr
, &addrlen
);
947 res
= bind (sockets
->mcast_recv
, (struct sockaddr
*)&sockaddr
, addrlen
);
949 perror ("bind mcast recv socket failed");
954 * Setup mcast send socket
956 sockets
->mcast_send
= socket (bindnet_address
->family
, SOCK_DGRAM
, 0);
957 if (sockets
->mcast_send
== -1) {
962 totemip_nosigpipe (sockets
->mcast_send
);
963 res
= fcntl (sockets
->mcast_send
, F_SETFL
, O_NONBLOCK
);
965 log_printf (instance
->totemnet_log_level_warning
, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno
));
973 if ( setsockopt(sockets
->mcast_send
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&flag
, sizeof (flag
)) < 0) {
974 perror("setsockopt reuseaddr");
978 totemip_totemip_to_sockaddr_convert(bound_to
, instance
->totem_interface
->ip_port
- 1,
979 &sockaddr
, &addrlen
);
980 res
= bind (sockets
->mcast_send
, (struct sockaddr
*)&sockaddr
, addrlen
);
982 perror ("bind mcast send socket failed");
987 * Setup unicast socket
989 sockets
->token
= socket (bindnet_address
->family
, SOCK_DGRAM
, 0);
990 if (sockets
->token
== -1) {
995 totemip_nosigpipe (sockets
->token
);
996 res
= fcntl (sockets
->token
, F_SETFL
, O_NONBLOCK
);
998 log_printf (instance
->totemnet_log_level_warning
, "Could not set non-blocking operation on token socket: %s\n", strerror (errno
));
1006 if ( setsockopt(sockets
->token
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&flag
, sizeof (flag
)) < 0) {
1007 perror("setsockopt reuseaddr");
1012 * Bind to unicast socket used for token send/receives
1013 * This has the side effect of binding to the correct interface
1015 totemip_totemip_to_sockaddr_convert(bound_to
, instance
->totem_interface
->ip_port
, &sockaddr
, &addrlen
);
1016 res
= bind (sockets
->token
, (struct sockaddr
*)&sockaddr
, addrlen
);
1018 perror ("bind token socket failed");
1022 recvbuf_size
= MCAST_SOCKET_BUFFER_SIZE
;
1023 sendbuf_size
= MCAST_SOCKET_BUFFER_SIZE
;
1025 * Set buffer sizes to avoid overruns
1027 res
= setsockopt (sockets
->mcast_recv
, SOL_SOCKET
, SO_RCVBUF
, &recvbuf_size
, optlen
);
1028 res
= setsockopt (sockets
->mcast_send
, SOL_SOCKET
, SO_SNDBUF
, &sendbuf_size
, optlen
);
1030 res
= getsockopt (sockets
->mcast_recv
, SOL_SOCKET
, SO_RCVBUF
, &recvbuf_size
, &optlen
);
1032 log_printf (instance
->totemnet_log_level_notice
,
1033 "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size
);
1036 res
= getsockopt (sockets
->mcast_send
, SOL_SOCKET
, SO_SNDBUF
, &sendbuf_size
, &optlen
);
1038 log_printf (instance
->totemnet_log_level_notice
,
1039 "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size
);
1043 * Join group membership on socket
1045 totemip_totemip_to_sockaddr_convert(mcast_address
, instance
->totem_interface
->ip_port
, &mcast_ss
, &addrlen
);
1046 totemip_totemip_to_sockaddr_convert(bound_to
, instance
->totem_interface
->ip_port
, &boundto_ss
, &addrlen
);
1048 switch ( bindnet_address
->family
) {
1050 memset(&mreq
, 0, sizeof(mreq
));
1051 mreq
.imr_multiaddr
.s_addr
= mcast_sin
->sin_addr
.s_addr
;
1052 mreq
.imr_interface
.s_addr
= boundto_sin
->sin_addr
.s_addr
;
1053 res
= setsockopt (sockets
->mcast_recv
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1054 &mreq
, sizeof (mreq
));
1056 perror ("join ipv4 multicast group failed");
1061 memset(&mreq6
, 0, sizeof(mreq6
));
1062 memcpy(&mreq6
.ipv6mr_multiaddr
, &mcast_sin6
->sin6_addr
, sizeof(struct in6_addr
));
1063 mreq6
.ipv6mr_interface
= interface_num
;
1065 res
= setsockopt (sockets
->mcast_recv
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
1066 &mreq6
, sizeof (mreq6
));
1068 perror ("join ipv6 multicast group failed");
1074 #ifndef OPENAIS_SOLARIS
1076 * Turn on multicast loopback
1080 switch ( bindnet_address
->family
) {
1082 res
= setsockopt (sockets
->mcast_recv
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1083 &flag
, sizeof (flag
));
1086 res
= setsockopt (sockets
->mcast_recv
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
1087 &flag
, sizeof (flag
));
1090 perror ("turn off loopback");
1096 * Set multicast packets TTL
1099 if ( bindnet_address
->family
== AF_INET6
)
1102 res
= setsockopt (sockets
->mcast_send
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
1103 &flag
, sizeof (flag
));
1105 perror ("setp mcast hops");
1110 #ifndef OPENAIS_SOLARIS
1112 * Bind to a specific interface for multicast send and receive
1114 switch ( bindnet_address
->family
) {
1116 if (setsockopt (sockets
->mcast_send
, IPPROTO_IP
, IP_MULTICAST_IF
,
1117 &boundto_sin
->sin_addr
, sizeof (boundto_sin
->sin_addr
)) < 0) {
1118 perror ("cannot select interface");
1121 if (setsockopt (sockets
->mcast_recv
, IPPROTO_IP
, IP_MULTICAST_IF
,
1122 &boundto_sin
->sin_addr
, sizeof (boundto_sin
->sin_addr
)) < 0) {
1123 perror ("cannot select interface");
1128 if (setsockopt (sockets
->mcast_send
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
,
1129 &interface_num
, sizeof (interface_num
)) < 0) {
1130 perror ("cannot select interface");
1133 if (setsockopt (sockets
->mcast_recv
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
,
1134 &interface_num
, sizeof (interface_num
)) < 0) {
1135 perror ("cannot select interface");
1145 static int totemnet_build_sockets (
1146 struct totemnet_instance
*instance
,
1147 struct totem_ip_address
*mcast_address
,
1148 struct totem_ip_address
*bindnet_address
,
1149 struct totemnet_socket
*sockets
,
1150 struct totem_ip_address
*bound_to
)
1157 * Determine the ip address bound to and the interface name
1159 res
= netif_determine (instance
,
1169 totemip_copy(&instance
->my_id
, bound_to
);
1171 res
= totemnet_build_sockets_ip (instance
, mcast_address
,
1172 bindnet_address
, sockets
, bound_to
, interface_num
);
1174 /* We only send out of the token socket */
1175 totemnet_traffic_control_set(instance
, sockets
->token
);
1180 * Totem Network interface - also does encryption/decryption
1181 * depends on poll abstraction, POSIX, IPV4
1185 * Create an instance
1187 int totemnet_initialize (
1188 poll_handle poll_handle
,
1189 totemnet_handle
*handle
,
1190 struct totem_config
*totem_config
,
1194 void (*deliver_fn
) (
1199 void (*iface_change_fn
) (
1201 struct totem_ip_address
*iface_address
))
1203 struct totemnet_instance
*instance
;
1206 res
= hdb_handle_create (&totemnet_instance_database
,
1207 sizeof (struct totemnet_instance
), handle
);
1211 res
= hdb_handle_get (&totemnet_instance_database
, *handle
,
1217 totemnet_instance_initialize (instance
);
1219 instance
->totem_config
= totem_config
;
1223 instance
->totemnet_log_level_security
= 1; //totem_config->totem_logging_configuration.log_level_security;
1224 instance
->totemnet_log_level_error
= totem_config
->totem_logging_configuration
.log_level_error
;
1225 instance
->totemnet_log_level_warning
= totem_config
->totem_logging_configuration
.log_level_warning
;
1226 instance
->totemnet_log_level_notice
= totem_config
->totem_logging_configuration
.log_level_notice
;
1227 instance
->totemnet_log_level_debug
= totem_config
->totem_logging_configuration
.log_level_debug
;
1228 instance
->totemnet_log_printf
= totem_config
->totem_logging_configuration
.log_printf
;
1231 * Initialize random number generator for later use to generate salt
1233 memcpy (instance
->totemnet_private_key
, totem_config
->private_key
,
1234 totem_config
->private_key_len
);
1236 instance
->totemnet_private_key_len
= totem_config
->private_key_len
;
1238 rng_make_prng (128, PRNG_SOBER
, &instance
->totemnet_prng_state
, NULL
);
1241 * Initialize local variables for totemnet
1243 instance
->totem_interface
= &totem_config
->interfaces
[interface_no
];
1244 totemip_copy (&instance
->mcast_address
, &instance
->totem_interface
->mcast_addr
);
1245 memset (instance
->iov_buffer
, 0, FRAME_SIZE_MAX
);
1248 * If threaded send requested, initialize thread group data structure
1250 if (totem_config
->threads
) {
1251 worker_thread_group_init (
1252 &instance
->worker_thread_group
,
1253 totem_config
->threads
, 128,
1254 sizeof (struct work_item
),
1255 sizeof (struct totemnet_mcast_thread_state
),
1256 totemnet_mcast_thread_state_constructor
,
1257 totemnet_mcast_worker_fn
);
1260 instance
->totemnet_poll_handle
= poll_handle
;
1262 instance
->totem_interface
->bindnet
.nodeid
= instance
->totem_config
->node_id
;
1264 instance
->context
= context
;
1265 instance
->totemnet_deliver_fn
= deliver_fn
;
1267 instance
->totemnet_iface_change_fn
= iface_change_fn
;
1269 instance
->handle
= *handle
;
1271 rng_make_prng (128, PRNG_SOBER
, &instance
->totemnet_prng_state
, NULL
);
1273 totemip_localhost (instance
->mcast_address
.family
, &localhost
);
1275 netif_down_check (instance
);
1278 hdb_handle_put (&totemnet_instance_database
, *handle
);
1282 hdb_handle_destroy (&totemnet_instance_database
, *handle
);
1286 int totemnet_processor_count_set (
1287 totemnet_handle handle
,
1288 int processor_count
)
1290 struct totemnet_instance
*instance
;
1293 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1300 instance
->my_memb_entries
= processor_count
;
1301 poll_timer_delete (instance
->totemnet_poll_handle
,
1302 instance
->timer_netif_check_timeout
);
1303 if (processor_count
== 1) {
1304 poll_timer_add (instance
->totemnet_poll_handle
,
1305 instance
->totem_config
->downcheck_timeout
,
1307 timer_function_netif_check_timeout
,
1308 &instance
->timer_netif_check_timeout
);
1310 hdb_handle_put (&totemnet_instance_database
, handle
);
1316 int totemnet_recv_flush (totemnet_handle handle
)
1318 struct totemnet_instance
*instance
;
1323 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1330 instance
->flushing
= 1;
1333 ufd
.fd
= instance
->totemnet_sockets
.mcast_recv
;
1334 ufd
.events
= POLLIN
;
1335 nfds
= poll (&ufd
, 1, 0);
1336 if (nfds
== 1 && ufd
.revents
& POLLIN
) {
1337 net_deliver_fn (0, instance
->totemnet_sockets
.mcast_recv
,
1338 ufd
.revents
, instance
);
1340 } while (nfds
== 1);
1342 instance
->flushing
= 0;
1344 hdb_handle_put (&totemnet_instance_database
, handle
);
1350 int totemnet_send_flush (totemnet_handle handle
)
1352 struct totemnet_instance
*instance
;
1355 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1362 worker_thread_group_wait (&instance
->worker_thread_group
);
1364 hdb_handle_put (&totemnet_instance_database
, handle
);
1370 int totemnet_token_send (
1371 totemnet_handle handle
,
1372 struct iovec
*iovec
,
1375 struct totemnet_instance
*instance
;
1378 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1385 ucast_sendmsg (instance
, &instance
->token_target
, iovec
, iov_len
);
1387 hdb_handle_put (&totemnet_instance_database
, handle
);
1392 int totemnet_mcast_flush_send (
1393 totemnet_handle handle
,
1394 struct iovec
*iovec
,
1395 unsigned int iov_len
)
1397 struct totemnet_instance
*instance
;
1400 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1407 mcast_sendmsg (instance
, iovec
, iov_len
);
1409 hdb_handle_put (&totemnet_instance_database
, handle
);
1415 int totemnet_mcast_noflush_send (
1416 totemnet_handle handle
,
1417 struct iovec
*iovec
,
1418 unsigned int iov_len
)
1420 struct totemnet_instance
*instance
;
1421 struct work_item work_item
;
1424 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1431 if (instance
->totem_config
->threads
) {
1432 memcpy (&work_item
.iovec
[0], iovec
, iov_len
* sizeof (struct iovec
));
1433 work_item
.iov_len
= iov_len
;
1434 work_item
.instance
= instance
;
1436 worker_thread_group_work_add (&instance
->worker_thread_group
,
1439 mcast_sendmsg (instance
, iovec
, iov_len
);
1442 hdb_handle_put (&totemnet_instance_database
, handle
);
1447 extern int totemnet_iface_check (totemnet_handle handle
)
1449 struct totemnet_instance
*instance
;
1452 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1459 timer_function_netif_check_timeout (instance
);
1461 hdb_handle_put (&totemnet_instance_database
, handle
);
1466 extern void totemnet_net_mtu_adjust (struct totem_config
*totem_config
)
1468 #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
1469 if (totem_config
->secauth
== 1) {
1470 totem_config
->net_mtu
-= sizeof (struct security_header
) +
1473 totem_config
->net_mtu
-= UDPIP_HEADER_SIZE
;
1477 char *totemnet_iface_print (totemnet_handle handle
) {
1478 struct totemnet_instance
*instance
;
1482 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1485 ret_char
= "Invalid totemnet handle";
1489 ret_char
= (char *)totemip_print (&instance
->my_id
);
1491 hdb_handle_put (&totemnet_instance_database
, handle
);
1496 int totemnet_iface_get (
1497 totemnet_handle handle
,
1498 struct totem_ip_address
*addr
)
1500 struct totemnet_instance
*instance
;
1503 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1509 memcpy (addr
, &instance
->my_id
, sizeof (struct totem_ip_address
));
1511 hdb_handle_put (&totemnet_instance_database
, handle
);
1517 int totemnet_token_target_set (
1518 totemnet_handle handle
,
1519 struct totem_ip_address
*token_target
)
1521 struct totemnet_instance
*instance
;
1524 res
= hdb_handle_get (&totemnet_instance_database
, handle
,
1530 memcpy (&instance
->token_target
, token_target
,
1531 sizeof (struct totem_ip_address
));
1533 hdb_handle_put (&totemnet_instance_database
, handle
);