When using IPv6, we must re-try bind() to handle the potential IPv6 DAD
(duplicate address detection) race condition.
After an IPv6 address has been added to an interface, there will be a short
period when the address is considered tentative and bind() will fail with
EADDRNOTAVAIL.
This commit also fixes a race in between the creation of the TCP listening
socket and the node discovery broadcast, by moving the socket creation to the
main thread.
---
src/base/Makefile.am | 2 ++
src/base/osaf_socket.c | 37 ++++++++++++++++++++++++++
src/base/osaf_socket.h | 55 +++++++++++++++++++++++++++++++++++++++
src/dtm/dtmnd/dtm_main.cc | 8 ++++++
src/dtm/dtmnd/dtm_node.cc | 8 ------
src/dtm/dtmnd/dtm_node_sockets.cc | 5 ++--
src/dtm/dtmnd/multicast.cc | 10 ++++---
7 files changed, 111 insertions(+), 14 deletions(-)
create mode 100644 src/base/osaf_socket.c
create mode 100644 src/base/osaf_socket.h
diff --git a/src/base/Makefile.am b/src/base/Makefile.am
index 956cce6a5..540c6dfe7 100644
--- a/src/base/Makefile.am
+++ b/src/base/Makefile.am
@@ -56,6 +56,7 @@ lib_libopensaf_core_la_SOURCES += \
src/base/osaf_extended_name.c \
src/base/osaf_poll.c \
src/base/osaf_secutil.c \
+ src/base/osaf_socket.c \
src/base/osaf_time.c \
src/base/osaf_timerfd.c \
src/base/osaf_unicode.c \
@@ -129,6 +130,7 @@ noinst_HEADERS += \
src/base/osaf_gcov.h \
src/base/osaf_poll.h \
src/base/osaf_secutil.h \
+ src/base/osaf_socket.h \
src/base/osaf_time.h \
src/base/osaf_timerfd.h \
src/base/osaf_unicode.h \
diff --git a/src/base/osaf_socket.c b/src/base/osaf_socket.c
new file mode 100644
index 000000000..e4838415b
--- /dev/null
+++ b/src/base/osaf_socket.c
@@ -0,0 +1,37 @@
+/* -*- OpenSAF -*-
+ *
+ * Copyright Ericsson AB 2018 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+#include "base/osaf_socket.h"
+#include <errno.h>
+#include "base/osaf_time.h"
+#include "base/osaf_utility.h"
+
+int osaf_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
+{
+ int rc;
+ int e;
+ for (int tries = 0; tries != 300; ++tries) {
+ rc = bind(sockfd, addr, addrlen);
+ e = errno;
+ if (rc == 0 || addr->sa_family != AF_INET6 || e != EADDRNOTAVAIL) break;
+ osaf_nanosleep(&kHundredMilliseconds);
+ }
+ errno = e;
+ if (rc < 0 && (e == EBADF || e == EINVAL || e == ENOTSOCK || e == EFAULT ||
+ e == EDESTADDRREQ || e == EISCONN)) {
+ osaf_abort(sockfd);
+ }
+ return rc;
+}
diff --git a/src/base/osaf_socket.h b/src/base/osaf_socket.h
new file mode 100644
index 000000000..04abf9d53
--- /dev/null
+++ b/src/base/osaf_socket.h
@@ -0,0 +1,55 @@
+/* -*- OpenSAF -*-
+ *
+ * Copyright Ericsson AB 2018 - All Rights Reserved.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
+ * under the GNU Lesser General Public License Version 2.1, February 1999.
+ * The complete license can be accessed from the following location:
+ * http://opensource.org/licenses/lgpl-license.php
+ * See the Copying file included with the OpenSAF distribution for full
+ * licensing terms.
+ *
+ */
+
+/** @file
+ *
+ * This file contains OpenSAF replacement of some POSIX sockets API
+ * functions. The definitions in this file are for internal use within OpenSAF
+ * only.
+ */
+
+#ifndef BASE_OSAF_SOCKET_H_
+#define BASE_OSAF_SOCKET_H_
+
+#include <sys/socket.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Bind a name to a socket.
+ *
+ * This is a convenience function that behaves exactly like the POSIX function
+ * bind(3P), except that if bind() fails with errno EADDRNOTAVAIL and the
+ * address family is IPv6, it will re-try binding for up to 30 seconds. The
+ * reason why this is useful is that binding an IPv6 address may temporary fail
+ * for a while when IPv6 Duplicate Address Detection (DAD) is in progress and
+ * the address is still considered to be tentative. The failure will be
reported
+ * back to the caller only after bind() has failed with EADDRNOTAVAIL for 30
+ * seconds. No re-try will be attempted for IPv6 addresses.
+ *
+ * In addition, this function will abort the process for the following errno
+ * values that can only happen due to software bugs: EBADF, EINVAL, ENOTSOCK,
+ * EFAULT, EDESTADDRREQ, EISCONN. This check is performed regardless of
+ * address family.
+ */
+int osaf_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // BASE_OSAF_SOCKET_H_
diff --git a/src/dtm/dtmnd/dtm_main.cc b/src/dtm/dtmnd/dtm_main.cc
index 3260a81f1..585e11ed1 100644
--- a/src/dtm/dtmnd/dtm_main.cc
+++ b/src/dtm/dtmnd/dtm_main.cc
@@ -276,6 +276,14 @@ int main(int argc, char *argv[]) {
}
/*************************************************************/
+ /* Set up the initial listening socket */
+ /*************************************************************/
+ if (NCSCC_RC_SUCCESS != dtm_stream_nonblocking_listener(dtms_cb)) {
+ LOG_ER("DTM: Set up the initial stream nonblocking serv failed");
+ exit(EXIT_FAILURE);
+ }
+
+ /*************************************************************/
/* Set up the initialservice_discovery_task */
/*************************************************************/
rc = dtm_service_discovery_init(dtms_cb);
diff --git a/src/dtm/dtmnd/dtm_node.cc b/src/dtm/dtmnd/dtm_node.cc
index b899d5a28..de2f94738 100644
--- a/src/dtm/dtmnd/dtm_node.cc
+++ b/src/dtm/dtmnd/dtm_node.cc
@@ -354,14 +354,6 @@ void node_discovery_process(void *arg) {
int node_info_buffer_len = 0;
uint8_t node_info_hrd[NODE_INFO_PKT_SIZE];
- /*************************************************************/
- /* Set up the initial listening socket */
- /*************************************************************/
- if (NCSCC_RC_SUCCESS != dtm_stream_nonblocking_listener(dtms_cb)) {
- LOG_ER("DTM: Set up the initial stream nonblocking serv failed");
- exit(EXIT_FAILURE);
- }
-
dgram_sock_rcvr.comm_socket = dtms_cb->multicast_->fd();
stream_sock.comm_socket = dtms_cb->stream_sock;
mbx_fd.comm_socket = dtms_cb->mbx_fd;
diff --git a/src/dtm/dtmnd/dtm_node_sockets.cc
b/src/dtm/dtmnd/dtm_node_sockets.cc
index 2b49b988d..0ddfc6f58 100644
--- a/src/dtm/dtmnd/dtm_node_sockets.cc
+++ b/src/dtm/dtmnd/dtm_node_sockets.cc
@@ -25,6 +25,7 @@
#include <cerrno>
#include <cstring>
#include "base/ncsencdec_pub.h"
+#include "base/osaf_socket.h"
#include "base/usrbuf.h"
#include "dtm/dtmnd/dtm.h"
#include "dtm/dtmnd/dtm_node.h"
@@ -357,7 +358,7 @@ static uint32_t stream_sock_bind(DTM_INTERNODE_CB *dtms_cb,
struct addrinfo *stream_addr) {
/* Bind to the local address and set socket to list */
TRACE_ENTER();
- if (bind(dtms_cb->stream_sock, stream_addr->ai_addr,
+ if (osaf_bind(dtms_cb->stream_sock, stream_addr->ai_addr,
stream_addr->ai_addrlen) == 0) {
if (listen(dtms_cb->stream_sock, MAXPENDING) == 0) {
/* get local address of socket */
@@ -377,7 +378,7 @@ static uint32_t stream_sock_bind(DTM_INTERNODE_CB *dtms_cb,
return NCSCC_RC_FAILURE;
}
} else {
- LOG_ER("bind(%d) failed with errno %d", dtms_cb->stream_sock, errno);
+ LOG_ER("osaf_bind(%d) failed with errno %d", dtms_cb->stream_sock, errno);
TRACE_LEAVE2("rc: %d", NCSCC_RC_FAILURE);
return NCSCC_RC_FAILURE;
}
diff --git a/src/dtm/dtmnd/multicast.cc b/src/dtm/dtmnd/multicast.cc
index bf67b9cee..cadc002bd 100644
--- a/src/dtm/dtmnd/multicast.cc
+++ b/src/dtm/dtmnd/multicast.cc
@@ -27,6 +27,7 @@
#include <cstdio>
#include <fstream>
#include "base/logtrace.h"
+#include "base/osaf_socket.h"
#include "base/osaf_utility.h"
Multicast::Multicast(uint16_t cluster_id, uint32_t node_id,
@@ -434,7 +435,7 @@ bool Multicast::InitializeUnicastReceiver() {
sizeof(v6only)) < 0)
LOG_ER("setsockopt(IPV6_V6ONLY) failed: %s", strerror(errno));
}
- if (bind(dgram_sock_rcvr, p->ai_addr, p->ai_addrlen) == -1) {
+ if (osaf_bind(dgram_sock_rcvr, p->ai_addr, p->ai_addrlen) == -1) {
LOG_ER("DTM:Socket bind failed err :%s", strerror(errno));
close(dgram_sock_rcvr);
TRACE_LEAVE2("rc :%d", NCSCC_RC_FAILURE);
@@ -977,8 +978,9 @@ uint32_t Multicast::dtm_dgram_mcast_listener() {
LOG_ER("setsockopt(IPV6_V6ONLY) failed: %s", strerror(errno));
}
- if (bind(dgram_sock_rcvr, addr_list->ai_addr, addr_list->ai_addrlen) < 0) {
- LOG_ER("DTM : bind() failed err :%s ", strerror(errno));
+ if (osaf_bind(dgram_sock_rcvr, addr_list->ai_addr, addr_list->ai_addrlen) <
+ 0) {
+ LOG_ER("DTM : osaf_bind() failed err :%s ", strerror(errno));
if (close(dgram_sock_rcvr) != 0 && errno != EINTR)
LOG_ER("close() failed, errno=%d", errno);
freeaddrinfo(addr_list);
@@ -1149,7 +1151,7 @@ uint32_t Multicast::dtm_dgram_bcast_listener() {
return NCSCC_RC_FAILURE;
}
}
- if (bind(dgram_sock_rcvr, p->ai_addr, p->ai_addrlen) == -1) {
+ if (osaf_bind(dgram_sock_rcvr, p->ai_addr, p->ai_addrlen) == -1) {
LOG_ER("DTM:Socket bind failed err :%s", strerror(errno));
close(dgram_sock_rcvr);
TRACE_LEAVE2("rc :%d", NCSCC_RC_FAILURE);
--
2.13.3
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Opensaf-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensaf-devel