From: Lev Stipakov <lev@openvpn.net>
create_socket_udp_configured()'s "optional" argument covers only the
socket() call: socket_bind() is fatal, so a caller that can do without
the socket still dies if the address is unavailable.
That is a problem for the upcoming --server-probe, which binds a probe
socket for every address family in use, while a connection binds only
the family of the remote it is about to try. A --lport whose IPv6 port
happens to be taken by another process would therefore kill a client
that could have connected over IPv4.
Give socket_bind() a msglevel and a return value, and let "optional"
apply to the bind as well: the socket is closed and SOCKET_UNDEFINED is
returned, so the caller can skip that address family. Every existing
caller passes M_FATAL and keeps its behaviour. With more than one return
path the gc arena no longer belongs at the top of the function, so it
moves into the bind failure, the only branch that formats an address.
The failure message now reports the address that was actually bound,
cur, rather than the head of the list, local, which may belong to a
different address family. Until now that message was always followed by
a fatal exit; with an optional bind it is printed while the client
carries on, so it needs to name the right address.
Change-Id: I24e5925a72aaede53ef9e863fe96fcd7363df402
Signed-off-by: Lev Stipakov <lev@openvpn.net>
Acked-by: Razvan Cojocaru <razvanc@mailbox.org>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
---
This change was reviewed on Gerrit and approved by at least one
developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1915
This mail reflects revision 13 of this Change.
Acked-by according to Gerrit (reflected above):
Razvan Cojocaru <razvanc@mailbox.org>
@@ -2080,7 +2080,7 @@
{
man->connection.sd_top = create_socket_tcp(man->settings.local);
socket_bind(man->connection.sd_top, man->settings.local, man->settings.local->ai_family,
- "MANAGEMENT", false);
+ "MANAGEMENT", false, M_FATAL);
}
/*
@@ -268,12 +268,13 @@
{
if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
{
- socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
+ socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false,
+ M_FATAL);
}
else
{
socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
- sock->info.bind_ipv6_only);
+ sock->info.bind_ipv6_only, M_FATAL);
}
}
}
@@ -319,9 +320,11 @@
}
socket_apply_options(sd, sbs, mark, bind_dev);
- if (bind_addr)
+ if (bind_addr
+ && !socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only, optional ? D_LOW : M_FATAL))
{
- socket_bind(sd, bind_addr, af, "TCP/UDP", bind_ipv6_only);
+ openvpn_close_socket(sd);
+ return SOCKET_UNDEFINED;
}
return sd;
}
@@ -558,12 +561,10 @@
return new_sd;
}
-void
+bool
socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family,
- const char *prefix, bool ipv6only)
+ const char *prefix, bool ipv6only, msglvl_t msglevel)
{
- struct gc_arena gc = gc_new();
-
/* FIXME (schwabe)
* getaddrinfo for the bind address might return multiple AF_INET/AF_INET6
* entries for the requested protocol.
@@ -584,8 +585,9 @@
}
if (!cur)
{
- msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
+ msg(msglevel, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
addr_family_name(ai_family));
+ return false;
}
if (ai_family == AF_INET6)
@@ -600,10 +602,13 @@
}
if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
{
- msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
- print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
+ struct gc_arena gc = gc_new();
+ msg(msglevel | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
+ print_sockaddr_ex(cur->ai_addr, ":", PS_SHOW_PORT, &gc));
+ gc_free(&gc);
+ return false;
}
- gc_free(&gc);
+ return true;
}
int
@@ -346,8 +346,15 @@
struct link_socket *link_socket_new(void);
-void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
- const char *prefix, bool ipv6only);
+/**
+ * Bind @p sd to the first @p af_family address of @p local. A failure is
+ * reported at @p msglevel, so a caller that can do without the bind passes a
+ * non-fatal level and checks the result.
+ *
+ * @return whether the socket was bound
+ */
+bool socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
+ const char *prefix, bool ipv6only, msglvl_t msglevel);
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
volatile int *signal_received);
@@ -401,9 +408,9 @@
* Create a UDP socket for @p af set up the way a link socket is: --sndbuf/--rcvbuf,
* --mark, --bind-dev and, when @p bind_addr is given, the local bind (IPV6_V6ONLY
* per @p bind_ipv6_only). Shared by create_socket() and the --server-probe sockets,
- * so a probe socket is the connection socket it may become. A failed bind is
- * fatal; with @p optional a socket the host cannot create is not, so
- * --server-probe can skip that address family (SOCKET_UNDEFINED is returned).
+ * so a probe socket is the connection socket it may become. With @p optional a
+ * socket the host cannot create or bind is not fatal, so --server-probe can skip
+ * that address family (SOCKET_UNDEFINED is returned).
*/
socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags,
const struct socket_buffer_size *sbs, int mark,