git master proto udp6+multihome is broken
If I connect to an openvpn server using "proto udp6" and "multihome", it instantly crashes with an assertion failure:
Jan 10 14:25:58 greed ovpn-server[9905]: 2a02:c0:100:0:9e8e:99ff:fed1:5243 TLS: Initial packet from [AF_INET6]2a02:c0:100:0:9e8e:99ff:fed1:5243:56277 (via 2a02:c0:1001:100::145%eth0), sid=31dd697b d84bd1bc Jan 10 14:25:58 greed ovpn-server[9905]: 2a02:c0:100:0:9e8e:99ff:fed1:5243 Assertion failed at socket.c:2878 Jan 10 14:25:58 greed ovpn-server[9905]: 2a02:c0:100:0:9e8e:99ff:fed1:5243 Exiting due to fatal error
This works with openvpn 2.3.2 so it's a regression.
However it works to connect to the same server with the exact same config if the client is using IPv4 (assuming you connect to the primary IPv4 address, because multihome doesn't actually work in this case, cf. ticket #306):
Jan 10 14:27:54 greed ovpn-server[10251]: ::ffff:87.238.42.150 TLS: Initial packet from [AF_INET6]::ffff:87.238.42.150:47707, sid=6b38f3f6 fa385236 Jan 10 14:27:54 greed ovpn-server[10251]: ::ffff:87.238.42.150 VERIFY OK: depth=1, C=KG, ST=NA, L=BISHKEK, O=OpenVPN-TEST, [email protected]...
[...]
Tore
Hi,
Quoted 2 lines · Tore Anderson
If I connect to an openvpn server using "proto udp6" and "multihome", it instantly crashes with an assertion failure:
Just to be sure I understand: it works for "proto udp6" if you do *not* use --multihome on the server?
Quoted 1 line
This works with openvpn 2.3.2 so it's a regression.
Definitely (... and as much that code has been rewritten, I'm not fully surprised that some of the server-side things broke in the process).
Thanks, we'll go work on it.
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...* Gert Doering
Quoted 2 lines
Just to be sure I understand: it works for "proto udp6" if you do *not* use --multihome on the server?
Correct. Apologies for being unclear.
Tore
Am 10.01.14 14:32, schrieb Tore Anderson:
Quoted 9 lines
If I connect to an openvpn server using "proto udp6" and "multihome", it instantly crashes with an assertion failure:
Jan 10 14:25:58 greed ovpn-server[9905]: 2a02:c0:100:0:9e8e:99ff:fed1:5243 TLS: Initial packet from [AF_INET6]2a02:c0:100:0:9e8e:99ff:fed1:5243:56277 (via 2a02:c0:1001:100::145%eth0), sid=31dd697b d84bd1bc Jan 10 14:25:58 greed ovpn-server[9905]: 2a02:c0:100:0:9e8e:99ff:fed1:5243 Assertion failed at socket.c:2878 Jan 10 14:25:58 greed ovpn-server[9905]: 2a02:c0:100:0:9e8e:99ff:fed1:5243 Exiting due to fatal errorThis works with openvpn 2.3.2 so it's a regression.
Quoted 10 lines
However it works to connect to the same server with the exact same config if the client is using IPv4 (assuming you connect to the primary IPv4 address, because multihome doesn't actually work in this case, cf. ticket #306):
Jan 10 14:27:54 greed ovpn-server[10251]: ::ffff:87.238.42.150 TLS: Initial packet from [AF_INET6]::ffff:87.238.42.150:47707, sid=6b38f3f6 fa385236 Jan 10 14:27:54 greed ovpn-server[10251]: ::ffff:87.238.42.150 VERIFY OK: depth=1, C=KG, ST=NA, L=BISHKEK, O=OpenVPN-TEST, [email protected]...[...]
That is very weird. I would expect it to fail for both cases in the same way. The codepath should trigger the same ASSERT(0) in both cases (or work in both). Can you forward a copy of your server configuration to me? Then I will try to reproduce that problem.
Arne
* Arne Schwabe
Quoted 4 lines
That is very weird. I would expect it to fail for both cases in the same way. The codepath should trigger the same ASSERT(0) in both cases (or work in both). Can you forward a copy of your server configuration to me? Then I will try to reproduce that problem.
Sure thing, here:
proto udp6
multihome
port 1194
server 192.0.2.0 255.255.255.0
server-ipv6 2001:db8::/112
dh /usr/share/doc/openvpn/examples/sample-keys/dh1024.pem ca /usr/share/doc/openvpn/examples/sample-keys/ca.crt cert /usr/share/doc/openvpn/examples/sample-keys/server.crt key /usr/share/doc/openvpn/examples/sample-keys/server.key
push "route 10.20.30.0 255.255.255.0"
push "route-ipv6 2001:db8:1::/64"
push "dhcp-option DNS 10.20.30.40"
dev tun
verb 3
keepalive 10 120
user nobody
group nogroup
comp-lzo
persist-key
persist-tun
duplicate-cn
Tore
Am 10.01.14 15:03, schrieb Tore Anderson:
Quoted 7 lines
* Arne Schwabe
Quoted 4 lines
That is very weird. I would expect it to fail for both cases in the same way. The codepath should trigger the same ASSERT(0) in both cases (or work in both). Can you forward a copy of your server configuration to me? Then I will try to reproduce that problem.
Can you try the following one line patch? I will do a better cleanup patch later but this should fix the problem
src/openvpn/socket.c +0 -0
@@ -2828,7 +2828,7 @@ link_socket_write_udp_posix_sendmsg (struct
link_socket *s
iov.iov_len = BLEN (buf);
mesg.msg_iov = &iov;
mesg.msg_iovlen = 1;
- switch (to->ai_family)
+ switch (to->dest.addr.sa.sa_family)
{
case AF_INET:
{* Arne Schwabe
Quoted 2 lines
Can you try the following one line patch? I will do a better cleanup patch later but this should fix the problem
Yep, this works. Thanks!
FWIW: Now I also got to test git master with what I wanted to in the first place, namely to see whether or not all the nice dualstack changes had also made "multihome" work for IPv4-mapped clients coming in to the server on an udp6 socket (ticket #306). Unfortunately that's still broken, and it seems that for a server process "proto udp" is still IPv4-only. So it doesn't appear to be possible to get a functioning dual-stacked UDP server (that has multiple addresses/interfaces) at the moment.
Tore
Am 11.01.14 00:59, schrieb Tore Anderson:
Quoted 6 lines
* Arne Schwabe
Quoted 2 lines
Can you try the following one line patch? I will do a better cleanup patch later but this should fix the problem
Yep, this works. Thanks!
Quoted 8 lines
FWIW: Now I also got to test git master with what I wanted to in the first place, namely to see whether or not all the nice dualstack changes had also made "multihome" work for IPv4-mapped clients coming in to the server on an udp6 socket (ticket #306). Unfortunately that's still broken, and it seems that for a server process "proto udp" is still IPv4-only. So it doesn't appear to be possible to get a functioning dual-stacked UDP server (that has multiple addresses/interfaces) at the moment.
Listening on multiple sockets not yet implemented. But a server having proto udp6 should get IPv6 mapped IPv4 clients working. What is happening/not working for you?
Arne
* Arne Schwabe
Quoted 3 lines
Listening on multiple sockets not yet implemented. But a server having proto udp6 should get IPv6 mapped IPv4 clients working. What is happening/not working for you?
"multihome" doesn't work, the OpenVPN server responds from its primary IPv4 address (the one the OS selects as the default source for routing towards the client), rather than the IPv4 address it was contacted on.
So the initial two packets look like this:
01:07:47.896447 IP 84.209.244.191.38878 > 87.238.35.253.1194: UDP, length 14 01:07:47.897423 IP 87.238.35.145.1194 > 84.209.244.191.38878: UDP, length 26
The client (84.209.244.191) expects the response packet to come from the address it contacted as --remote (87.238.35.253), but instead the server responds from 87.238.35.145 which is its primary address. Doesn't work, after some retransmissions and reconnect attempts the client gives up.
The server logs the following:
Jan 11 01:07:47 greed ovpn-server[10222]: ::ffff:84.209.244.191 TLS: Initial packet from [AF_INET6]::ffff:84.209.244.191:38878 (via 2a02:c0:1001:100::253%eth0), sid=98e4314f 9ea08578
I find the "via 2a02:c0:1001:100::253%eth0" part interesting, as that's not even the primary IPv6 address of the interface.
Tore
Am 11.01.14 01:12, schrieb Tore Anderson:
Quoted 19 lines
* Arne Schwabe
Quoted 3 lines
Listening on multiple sockets not yet implemented. But a server having proto udp6 should get IPv6 mapped IPv4 clients working. What is happening/not working for you?
"multihome" doesn't work, the OpenVPN server responds from its primary IPv4 address (the one the OS selects as the default source for routing towards the client), rather than the IPv4 address it was contacted on.
So the initial two packets look like this:
01:07:47.896447 IP 84.209.244.191.38878 > 87.238.35.253.1194: UDP, length 14 01:07:47.897423 IP 87.238.35.145.1194 > 84.209.244.191.38878: UDP, length 26
The client (84.209.244.191) expects the response packet to come from the address it contacted as --remote (87.238.35.253), but instead the server responds from 87.238.35.145 which is its primary address. Doesn't work, after some retransmissions and reconnect attempts the client gives up.
Quoted 8 lines
The server logs the following:
Jan 11 01:07:47 greed ovpn-server[10222]: ::ffff:84.209.244.191 TLS: Initial packet from [AF_INET6]::ffff:84.209.244.191:38878 (via 2a02:c0:1001:100::253%eth0), sid=98e4314f 9ea08578I find the "via 2a02:c0:1001:100::253%eth0" part interesting, as that's not even the primary IPv6 address of the interface.
Yeah. We are probably out of luck at this point without implemting a socket for v4 and a socket for v6. If the operating system does not give us a valid incoming interface/ip combination, we cannot fix that later.
Arne
Hi,
Quoted 8 lines · Arne Schwabe
Quoted 4 lines
Jan 11 01:07:47 greed ovpn-server[10222]: ::ffff:84.209.244.191 TLS: Initial packet from [AF_INET6]::ffff:84.209.244.191:38878 (via 2a02:c0:1001:100::253%eth0), sid=98e4314f 9ea08578I find the "via 2a02:c0:1001:100::253%eth0" part interesting, as that's not even the primary IPv6 address of the interface.
Yeah. We are probably out of luck at this point without implemting a socket for v4 and a socket for v6. If the operating system does not give us a valid incoming interface/ip combination, we cannot fix that later.
Where's that "via" coming from? Is that the local socket address that IPV6_PKTINFO (etc) told us the UDP packet was addressed to?
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Am 11.01.14 14:07, schrieb Gert Doering:
Quoted 5 lines
Hi,
Quoted 2 lines · Arne Schwabe
Quoted 2 lines
Jan 11 01:07:47 greed ovpn-server[10222]: ::ffff:84.209.244.191 TLS: Initial packet from [AF_INET6]::ffff:84.209.244.191:38878 (via 2a02:c0:1001:100::253%eth0), sid=98e4314f 9ea08578
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I find the "via 2a02:c0:1001:100::253%eth0" part interesting, as that's not even the primary IPv6 address of the interface.
Yeah. We are probably out of luck at this point without implemting a socket for v4 and a socket for v6. If the operating system does not give us a valid incoming interface/ip combination, we cannot fix that later.
Where's that "via" coming from? Is that the local socket address that
IPV6_PKTINFO(etc) told us the UDP packet was addressed to?
Yes.
Arne
Hi,
Quoted 8 lines · Tore Anderson
FWIW: Now I also got to test git master with what I wanted to in the first place, namely to see whether or not all the nice dualstack changes had also made "multihome" work for IPv4-mapped clients coming in to the server on an udp6 socket (ticket #306). Unfortunately that's still broken, and it seems that for a server process "proto udp" is still IPv4-only. So it doesn't appear to be possible to get a functioning dual-stacked UDP server (that has multiple addresses/interfaces) at the moment.
I *thought* that both proto udp and proto udp6 would create an IPv6 socket bound to :: with IPV6_V6ONLY set to 0 (= accepting IPv4 and IPv6 packets to any address). Only "proto udp4" should listen to 0.0.0.0.
(There's a catch to it, IIRC - *BSD will show the socket as "udp46" in this case, while linux will still just display "udp6", even if it *does* "46" on it)
Now, --multihome is a different issue - I accept that this isn't working right in various scenarios, and we need to fix this. But at least dual- stacked servers with "proto udp" are expected to work.
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...* Gert Doering
Quoted 3 lines
I *thought* that both proto udp and proto udp6 would create an IPv6 socket bound to :: with
IPV6_V6ONLYset to 0 (= accepting IPv4 and IPv6 packets to any address). Only "proto udp4" should listen to 0.0.0.0.
This is not how git master behaves for me. "proto udp" and "proto udp4" both yield an IPv4-only socket (AF_INET):
[email protected]...:~$ sudo lsof -i :1194 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME openvpn 31739 nobody 5u IPv4 19461864 0t0 UDP *:openvpn
Only "proto udp6" yields an dual-stacked socket (AF_INET6 + !IPV6_V6ONLY), but I cannot use this because "multihome" fails for IPv4 clients (which are the majority of my users).
[email protected]...:~$ sudo lsof -i :1194 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME openvpn 32126 nobody 5u IPv6 19464798 0t0 UDP *:openvpn
FWIW, "proto tcp-server" also yields an IPv4-only socket. "proto tcp6-server" works (also for IPv4 clients).
Quoted 3 lines
Now, --multihome is a different issue - I accept that this isn't working right in various scenarios, and we need to fix this. But at least dual- stacked servers with "proto udp" are expected to work.
Per the above, I cannot see that it does... I can submit a separate bug report about this if you wish. That said, I would be perfectly happy to simply use "proto udp6" instead, IFF "multihome" had worked correctly.
Tore
Hi,
Quoted 6 lines · Tore Anderson
Quoted 3 lines
I *thought* that both proto udp and proto udp6 would create an IPv6 socket bound to :: with
IPV6_V6ONLYset to 0 (= accepting IPv4 and IPv6 packets to any address). Only "proto udp4" should listen to 0.0.0.0.This is not how git master behaves for me. "proto udp" and "proto udp4" both yield an IPv4-only socket (
AF_INET):
Mmmh. Right you are, at least on *Linux*...
"proto upd6" gives an udp6/TYPE=IPv6 socket, while "proto udp" gives an udp/TYPE=IPv4 socket, on my Gentoo system (and this is not an artifact from netstat or lsof, but it really only listens on 0.0.0.0)...
Sat Jan 11 12:27:35 2014 us=883703 proto = udp Sat Jan 11 12:27:35 2014 us=883794 local = '[UNDEF]' Sat Jan 11 12:27:35 2014 us=883885 local_port = '61194'
Sat Jan 11 12:27:35 2014 us=912736 UDP link local (bound): [AF_INET][undef]:61194
When I tested that, I used a FreeBSD system, which behaves differently - there, "proto udp" gives the same result as "proto udp6", and you need to do "proto udp4" to get an IPv4-only socket...
Sat Jan 11 12:11:44 2014 us=286820 proto = udp Sat Jan 11 12:11:44 2014 us=286849 local = '[UNDEF]' Sat Jan 11 12:11:44 2014 us=286877 local_port = '51194'
Sat Jan 11 12:11:44 2014 us=383743 UDP link local (bound): [AF_INET6][undef]:51194
$ netstat -an |grep 1194 udp46 0 0 *.51194 *.*
I do think I know where the problem is coming from (or, more precise, Arne mentioned this a while ago already) - Arne's new code uses getaddrinfo() with AI_PASSIVE to get "something suitable for listening on", and Linux' getaddrinfo() seems to return INADDR_ANY before IN6ADDR_ANY, which is arguably a pretty stupid thing to do... and yes, I think that we should workaround this.
Quoted 3 lines
I can submit a separate bug
report about this if you wish. That said, I would be perfectly happy to simply use "proto udp6" instead, IFF "multihome" had worked correctly.
Our documentation says that "proto udp" or "proto tcp[-server]" is "use dual-stack", so it should better do that, correctly on all platforms.
Bug opened, #360, and assigned to Arne. Thanks for telling us.
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Hi,
Quoted 8 lines · Tore Anderson
FWIW: Now I also got to test git master with what I wanted to in the first place, namely to see whether or not all the nice dualstack changes had also made "multihome" work for IPv4-mapped clients coming in to the server on an udp6 socket (ticket #306). Unfortunately that's still broken, and it seems that for a server process "proto udp" is still IPv4-only. So it doesn't appear to be possible to get a functioning dual-stacked UDP server (that has multiple addresses/interfaces) at the moment.
FWIW, that seems to be a Linux oddity.
I've extracted the "bind to an UDP socket, listen on it, print all addresses I can find" out of openvpn to see whether I can more easily debug all the multihome oddities people have seen, and that code tells me that for IPv4-mapped clients, we are not actually receiving any "ancilliary data" in recvmsg(), which is needed to see our local socket address.
It works for IPv6 clients on IPv6 sockets, and for IPv4 clients on IPv4-only sockets, but not for IPv4 (-mapped) clients on dual-stack IPv6 sockets.
This is made worse by the OpenVPN code assuming that it would always receive data, so the "if not" case neither triggers a warning, nor is the data structure properly cleaned in that case - so you'll see packet info related to the last properly reported connection.
In case you want to test yourself, the code is appended. It's not pretty, but gets the job done :-)
Compile with "gcc -o mhome mhome.c", run with
./mhome (4|6) <port>
like "./mhome 4 12345" or "./mhome 6 50001".
In lieu of a good tool to generate IPv6 UDP packets, I've used "traceroute6" to trigger this ("traceroute6 -p 50000" generates 50001 and up :-) ).
A *working* sample is here, from FreeBSD 9.2:
$ ./mhome AF_INET6/IPV6_RECVPKTINFO enabled setsockopt(IPV6_V6ONLY=0) Socket bound to local address [AF_INET6][undef]:50001 -- CMSG_NXTHDR=0x0, level=41, type=46 IPV6_PKTINFO read: fromlen=28, r_len=12 from=[AF_INET6]2001:608:4::3:52613 (via 2001:608:0:814::f000:5%[undef]) -- CMSG_NXTHDR=0x0, level=41, type=46 IPV6_PKTINFO read: fromlen=28, r_len=12 from=[AF_INET6]::ffff:193.149.48.167:59559 (via ::ffff:194.97.140.5%[undef]) --
A non-working example from a Linux 3.3.8 kernel:
$ ./mhome AF_INET6/IPV6_RECVPKTINFO enabled setsockopt(IPV6_V6ONLY=0) Socket bound to local address [AF_INET6][undef]:50001 -- CMSG_NXTHDR=(nil), level=41, type=50 IPV6_PKTINFO read: fromlen=28, r_len=12 from=[AF_INET6]2001:608:0:814::f000:5:61802 (via 2001:608:0:814::f000:11%eth0) -- CMSG_FIRSTHDR()=NULL, no packet info available read: fromlen=28, r_len=24 from=[AF_INET6]::ffff:194.97.140.5:54859 (via ::%[undef]) --
meh...
(So if someone really wants to make this work, the next step would be to figure out why it's not working in the Linux IPv4-mapped case, and get it fixed...)
More to come.
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Hi,
Quoted 1 line · Gert Doering
In case you want to test yourself, the code is appended.
... it was not. As always when people say "I have attached..." :-/
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected].../*
* mhome.c - test program for multihoming server tests *
* open UDP socket on port specified on command line, bind IPv4 or IPv6 * to it, log all incoming packets and reply, hopefully with the right * source address
*
* largely based on OpenVPN sources to test those modules */
#define ENABLE_IP_PKTINFO 1
#ifdef linux
/* needed for netinet/in.h */
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <net/if.h>
#include <stdbool.h>
#include <assert.h>
#define ASSERT(x) assert(x)
#define CLEAR(x) memset(&(x), 0, sizeof(x))
/* IP_PKTINFO or IP_RECVDSTADDR are defined in <netinet/in.h> on BSDs
* and Linux just fine, but our configure/ifdef logic is weird */
#ifdef IP_PKTINFO
# define HAVE_IN_PKTINFO 1 #endif
#define msg(a,b) fprintf(stderr,b "\n")
/* glue definitions, copied from syshead.h or socket.h */ typedef int socket_descriptor_t;
# define SF_USE_IP_PKTINFO (1<<0)
#define PS_SHOW_PORT_IF_DEFINED (1<<0)
#define PS_SHOW_PORT (1<<1) #define PS_SHOW_PKTINFO (1<<2) #define PS_DONT_SHOW_ADDR (1<<3)
/* OpenVPN sockaddr struct */
struct openvpn_sockaddr
{
union {
struct sockaddr sa;
struct sockaddr_in in4;
struct sockaddr_in6 in6; } addr;
};
struct link_socket_actual
{
int ai_family; /* PF_xxx */
int ai_socktype; /* SOCK_xxx */
int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
struct openvpn_sockaddr dest;
#if ENABLE_IP_PKTINFO
union {
#ifdef HAVE_IN_PKTINFO
struct in_pktinfo in4;
#elif defined(IP_RECVDSTADDR)
struct in_addr in4;
#endif
struct in6_pktinfo in6; } pi;
#endif
};
/*
* Format IP addresses in ascii
*
* (hacked-together non-safe version of the version in openvpn) */
const char *print_sockaddr_ex (const struct sockaddr *sa,
const char* separator, const unsigned int flags )
{
static char out[1000];
bool addr_is_defined;
char hostaddr[NI_MAXHOST] = "";
char servname[NI_MAXSERV] = "";
int status;
socklen_t salen;
switch(sa->sa_family)
{
case AF_INET:
strcpy(out, "[AF_INET]");
salen = sizeof (struct sockaddr_in);
addr_is_defined = ((struct sockaddr_in*) sa)->sin_addr.s_addr != 0;
break;
case AF_INET6:
strcpy(out, "[AF_INET6]");
salen = sizeof (struct sockaddr_in6);
addr_is_defined = !IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6*) sa)->sin6_addr);
break;
case AF_UNSPEC:
return "[AF_UNSPEC]";
default:
ASSERT(0);
}status = getnameinfo(sa, salen, hostaddr, sizeof (hostaddr),
servname, sizeof(servname), NI_NUMERICHOST | NI_NUMERICSERV);
if(status!=0) {
sprintf(&out[strlen(out)],"[nameinfo() err: %s]",gai_strerror(status));
return out;
}
if (!(flags & PS_DONT_SHOW_ADDR))
{
if (addr_is_defined)
strcat (out, hostaddr);
else
strcat (out, "[undef]");
}
if ((flags & PS_SHOW_PORT) || (flags & PS_SHOW_PORT_IF_DEFINED))
{
if (separator)
strcat (out, separator); strcat (out, servname);
} return out;
}
/* ------- mostly unchanged code from socket.c below this line ----- */
static socket_descriptor_tcreate_socket_udp (const int af, const unsigned int flags) {
socket_descriptor_t sd;
if ((sd = socket (af, SOCK_DGRAM, IPPROTO_UDP)) < 0)
msg (M_ERR, "UDP: Cannot create UDP/UDP6 socket");
#if ENABLE_IP_PKTINFO
else if (flags & SF_USE_IP_PKTINFO)
{
int pad = 1;
if(af == AF_INET)
{
#ifdef IP_PKTINFO
fprintf( stderr, "AF_INET/IP_PKTINFO enabled\n");
if (setsockopt (sd, SOL_IP, IP_PKTINFO,
(void*)&pad, sizeof(pad)) < 0)
msg(M_ERR, "UDP: failed setsockopt for IP_PKTINFO");
#elif defined(IP_RECVDSTADDR)
fprintf( stderr, "AF_INET/IP_RECVDSTADDR enabled\n");
if (setsockopt (sd, IPPROTO_IP, IP_RECVDSTADDR,
(void*)&pad, sizeof(pad)) < 0)
msg(M_ERR, "UDP: failed setsockopt for IP_RECVDSTADDR");
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
}
else if (af == AF_INET6 )
{
#ifndef IPV6_RECVPKTINFO /* Some older Darwin platforms require this */
fprintf( stderr, "AF_INET6/IPV6_PKTINFO enabled\n");
if (setsockopt (sd, IPPROTO_IPV6, IPV6_PKTINFO,
(void*)&pad, sizeof(pad)) < 0)
#else
fprintf( stderr, "AF_INET6/IPV6_RECVPKTINFO enabled\n");
if (setsockopt (sd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
(void*)&pad, sizeof(pad)) < 0)
#endif
msg(M_ERR, "UDP: failed setsockopt for IPV6_RECVPKTINFO");
}
}#endif
return sd;
}
voidsocket_bind (socket_descriptor_t sd,
char* port, int ai_family)
{
struct addrinfo hints;
struct addrinfo* cur;
CLEAR(hints); hints.ai_family = ai_family; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV; int s = getaddrinfo( NULL, port, &hints, &cur );
if ( s != 0 )
{ fprintf( stderr, "getaddrinfo failed: %s (%d)\n",
gai_strerror(s), s ); exit(1); }
if (ai_family == AF_INET6)
{
int v6only = 0; fprintf (stderr, "setsockopt(IPV6_V6ONLY=%d)\n", v6only);
if (setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)))
{
fprintf (stderr, "Setting IPV6_V6ONLY=%d failed\n", v6only);
}
}
if (bind (sd, cur->ai_addr, cur->ai_addrlen))
{
const int err = errno;
fprintf (stderr, "Socket bind failed on local address %s: %s\n",
print_sockaddr_ex (cur->ai_addr, ":", PS_SHOW_PORT),
strerror(err));
exit(1);
}
fprintf (stderr, "Socket bound to local address %s\n",
print_sockaddr_ex (cur->ai_addr, ":", PS_SHOW_PORT) );
}const char *print_link_socket_actual_ex (const struct link_socket_actual *act,
const char *separator, const unsigned int flags)
{
static char out[1000];
if (act)
{
char ifname[IF_NAMESIZE] = "[undef]";
strcpy (out, print_sockaddr_ex (&act->dest.addr.sa, separator, flags));
#if ENABLE_IP_PKTINFO
if ((flags & PS_SHOW_PKTINFO)) /* && addr_defined_ipi(act))*/
{
switch(act->dest.addr.sa.sa_family)
{
case AF_INET:
{
struct openvpn_sockaddr sa;
CLEAR (sa);
sa.addr.in4.sin_family = AF_INET;
#ifdef IP_PKTINFO
sa.addr.in4.sin_addr = act->pi.in4.ipi_spec_dst;
if_indextoname(act->pi.in4.ipi_ifindex, ifname);
#elif defined(IP_RECVDSTADDR)
sa.addr.in4.sin_addr = act->pi.in4;
ifname[0]=0;
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
sprintf (&out[strlen(out)], " (via %s%%%s)",
print_sockaddr_ex (&sa.addr.sa, separator, 0),
ifname);
}
break;
case AF_INET6:
{
struct sockaddr_in6 sin6;
char buf[INET6_ADDRSTRLEN] = "[undef]";
CLEAR(sin6);
sin6.sin6_family = AF_INET6;
sin6.sin6_addr = act->pi.in6.ipi6_addr;
if_indextoname(act->pi.in6.ipi6_ifindex, ifname);
if (getnameinfo((struct sockaddr *)&sin6, sizeof (struct sockaddr_in6),
buf, sizeof (buf), NULL, 0, NI_NUMERICHOST) == 0)
sprintf (&out[strlen(out)], " (via %s%%%s)", buf, ifname);
else
sprintf (&out[strlen(out)], " (via [getnameinfo() err]%%%s)", ifname);
}
break;
}
}
#endif
return out;
}
else
return "[NULL]";
}#pragma pack(1) /* needed to keep structure size consistent for 32 vs. 64-bit architectures */ struct openvpn_in4_pktinfo
{
struct cmsghdr cmsghdr;
#ifdef HAVE_IN_PKTINFO
struct in_pktinfo pi4;
#elif defined(IP_RECVDSTADDR)
struct in_addr pi4;
#endif
};
struct openvpn_in6_pktinfo
{
struct cmsghdr cmsghdr;
struct in6_pktinfo pi6;
};
union openvpn_pktinfo {
struct openvpn_in4_pktinfo msgpi4;
struct openvpn_in6_pktinfo msgpi6;
};#pragma pack()
static socklen_t link_socket_read_udp_posix_recvmsg (socket_descriptor_t sd,
char *buf, int maxsize, struct link_socket_actual *from, int *r_len)
{
struct iovec iov;
union openvpn_pktinfo opi;
struct msghdr mesg;
socklen_t fromlen = sizeof (from->dest.addr); iov.iov_base = buf; iov.iov_len = maxsize; mesg.msg_iov = &iov; mesg.msg_iovlen = 1; mesg.msg_name = &from->dest.addr; mesg.msg_namelen = fromlen; mesg.msg_control = &opi; mesg.msg_controllen = sizeof opi;
*r_len = recvmsg (sd, &mesg, 0);
if (*r_len >= 0)
{
struct cmsghdr *cmsg;
fromlen = mesg.msg_namelen;
cmsg = CMSG_FIRSTHDR (&mesg);
if (cmsg == NULL)
fprintf( stderr, "CMSG_FIRSTHDR()=NULL, no packet info available\n");
else
fprintf( stderr, "CMSG_NXTHDR=%p, level=%d, type=%d\n",
CMSG_NXTHDR (&mesg, cmsg), cmsg->cmsg_level, cmsg->cmsg_type );
if (cmsg != NULL
&& CMSG_NXTHDR (&mesg, cmsg) == NULL
#ifdef IP_PKTINFO
&& cmsg->cmsg_level == SOL_IP
&& cmsg->cmsg_type == IP_PKTINFO
#elif defined(IP_RECVDSTADDR)
&& cmsg->cmsg_level == IPPROTO_IP
&& cmsg->cmsg_type == IP_RECVDSTADDR
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
&& cmsg->cmsg_len >= sizeof (struct openvpn_in4_pktinfo))
{
#ifdef IP_PKTINFO
struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
from->pi.in4.ipi_ifindex = pkti->ipi_ifindex;
from->pi.in4.ipi_spec_dst = pkti->ipi_spec_dst;
fprintf( stderr, "IP_PKTINFO\n" );
#elif defined(IP_RECVDSTADDR)
from->pi.in4 = *(struct in_addr*) CMSG_DATA (cmsg);
fprintf( stderr, "IP_RECVDSTADDR\n" );
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
}
else if (cmsg != NULL
&& CMSG_NXTHDR (&mesg, cmsg) == NULL
&& cmsg->cmsg_level == IPPROTO_IPV6
&& cmsg->cmsg_type == IPV6_PKTINFO
&& cmsg->cmsg_len >= sizeof (struct openvpn_in6_pktinfo))
{
struct in6_pktinfo *pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
from->pi.in6.ipi6_ifindex = pkti6->ipi6_ifindex;
from->pi.in6.ipi6_addr = pkti6->ipi6_addr;
fprintf( stderr, "IPV6_PKTINFO\n" );
}
} return fromlen;
}
size_tlink_socket_write_udp_posix_sendmsg (socket_descriptor_t sd,
char *buf, int len, struct link_socket_actual *to)
{
struct iovec iov;
struct msghdr mesg;
struct cmsghdr *cmsg;
union openvpn_pktinfo opi;
iov.iov_base = buf;
iov.iov_len = len;
mesg.msg_iov = &iov;
mesg.msg_iovlen = 1;
switch (to->ai_family)
{
case AF_INET:
{
mesg.msg_name = &to->dest.addr.sa;
mesg.msg_namelen = sizeof (struct sockaddr_in);
mesg.msg_control = &opi;
mesg.msg_controllen = sizeof (struct openvpn_in4_pktinfo);
mesg.msg_flags = 0;
cmsg = CMSG_FIRSTHDR (&mesg);
cmsg->cmsg_len = sizeof (struct openvpn_in4_pktinfo);
#ifdef HAVE_IN_PKTINFO
cmsg->cmsg_level = SOL_IP;
cmsg->cmsg_type = IP_PKTINFO;
{
struct in_pktinfo *pkti;
pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
pkti->ipi_ifindex = to->pi.in4.ipi_ifindex;
pkti->ipi_spec_dst = to->pi.in4.ipi_spec_dst;
pkti->ipi_addr.s_addr = 0;
}
#elif defined(IP_RECVDSTADDR)
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_RECVDSTADDR;
*(struct in_addr *) CMSG_DATA (cmsg) = to->pi.in4;
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
break;
}
case AF_INET6:
{
struct in6_pktinfo *pkti6;
mesg.msg_name = &to->dest.addr.sa;
mesg.msg_namelen = sizeof (struct sockaddr_in6);
mesg.msg_control = &opi;
mesg.msg_controllen = sizeof (struct openvpn_in6_pktinfo);
mesg.msg_flags = 0;
cmsg = CMSG_FIRSTHDR (&mesg);
cmsg->cmsg_len = sizeof (struct openvpn_in6_pktinfo);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
pkti6->ipi6_ifindex = to->pi.in6.ipi6_ifindex;
pkti6->ipi6_addr = to->pi.in6.ipi6_addr;
break;
}
default: ASSERT(0);
} return sendmsg (sd, &mesg, 0);
}
void loop(int sd)
{
int r_len, fromlen, r;
char buf[10000];
struct link_socket_actual from;
while(1)
{
CLEAR(from);
fprintf( stderr, "--\n" );
fromlen = link_socket_read_udp_posix_recvmsg (sd, buf, sizeof(buf), &from, &r_len);
fprintf( stderr, "read: fromlen=%d, r_len=%d\n", fromlen, r_len );
fprintf( stderr, " from=%s\n",
print_link_socket_actual_ex(&from, ":", PS_SHOW_PORT|PS_SHOW_PKTINFO) );#if 0
/* send back response (just mirror packet) */
r = link_socket_write_udp_posix_sendmsg(sd, buf, r_len, &from);
fprintf( stderr, "sendmsg() returns r=%d\n", r );
#endif
}
}int main(int argc, char **argv)
{
int s;
int af = AF_INET6;
char* port = "50001";
if ( argc > 1 && argv[1][0] == '4' ) { af = AF_INET; }
if ( argc > 2 ) { port = argv[2]; }
s = create_socket_udp( af, SF_USE_IP_PKTINFO );
socket_bind( s, port, af );
loop(s);
exit(0);
}Hi,
Quoted 2 lines · Gert Doering
In case you want to test yourself, the code is appended. It's not pretty, but gets the job done :-)
Here's the latest iteration of the code. The changes compared to openvpn's socket.c are actually bigger than I expected, as what we have seems to be completely broken on some platforms due to structure size/alignment issues...
The current version works on everything I have access to (except MacOS), both 32bit and 64bit. I'll eventually merge that back into OpenVPN proper, and send a patch for that...
What it still confirms is that *some* platforms just do not send IP6_PKTINFO for IPv4-mapped platforms, notably
- Linux
- FreeBSD up to 8.x (7.4, 8.2 do not, 9.1 does)
- OpenBSD (no dual-stacked sockets, no ipv4-mapped packets anywhere)
OTOH, NetBSD 5.1 and FreeBSD 9+ get it right, and this code will handle it correctly.
Next step: find someone who understands kernel code on Linux :-)
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected].../*
* mhome.c - test program for multihoming server tests *
* open UDP socket on port specified on command line, bind IPv4 or IPv6 * to it, log all incoming packets and reply, hopefully with the right * source address * * largely based on OpenVPN sources to test those modules * * v3: FreeBSD 9.2/amd64: padding for openvpn_in6_pktinfo added, * (needed to get non-corrupt ifindex on reception + sending) * patch from #327 applied, slightly adjusted, works * * v4: FreeBSD IPv4 code extended with ASSERT() check * always include padding, get recvmsg()/sendmsg() right by using * CMSG_SPACE()/CMSG_LEN() macros (RFC2292) * works on FreeBSD 9.1/amd64, FreeBSD 8.3/i386 (not for v4-mapped) * * v5: Linux IPv4 code changed to CMSG_SPACE/CMSG_LEN, works on 64 bit now * (but still no v4-mapped for IPv6) * works on OpenBSD 4.9/i386, OpenBSD 5.4/amd64 (no dual-stack sockets), * NetBSD 5.1/amd64 for IPv6 (with v4-mapped!)
* getnameinfo() on NetBSD fails (sa->sa_len not filled in)
*/
#define ENABLE_IP_PKTINFO 1
#ifdef linux
/* needed for netinet/in.h */
#define _GNU_SOURCE 1
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <net/if.h>
#include <stdbool.h>
#include <assert.h>
#define ASSERT(x) assert(x)
#define CLEAR(x) memset(&(x), 0, sizeof(x))
/* IP_PKTINFO or IP_RECVDSTADDR are defined in <netinet/in.h> on BSDs
* and Linux just fine, but our configure/ifdef logic is weird */
#ifdef IP_PKTINFO
# define HAVE_IN_PKTINFO 1 #endif
#define msg(a,b) fprintf(stderr,b "\n")
/* glue definitions, copied from syshead.h or socket.h */ typedef int socket_descriptor_t;
# define SF_USE_IP_PKTINFO (1<<0)
#define PS_SHOW_PORT_IF_DEFINED (1<<0)
#define PS_SHOW_PORT (1<<1) #define PS_SHOW_PKTINFO (1<<2) #define PS_DONT_SHOW_ADDR (1<<3)
/* OpenVPN sockaddr struct */
struct openvpn_sockaddr
{
union {
struct sockaddr sa;
struct sockaddr_in in4;
struct sockaddr_in6 in6; } addr;
};
struct link_socket_actual
{
int ai_family; /* PF_xxx */
int ai_socktype; /* SOCK_xxx */
int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
struct openvpn_sockaddr dest;
#if ENABLE_IP_PKTINFO
union {
#ifdef HAVE_IN_PKTINFO
struct in_pktinfo in4;
#elif defined(IP_RECVDSTADDR)
struct in_addr in4;
#endif
struct in6_pktinfo in6; } pi;
#endif
};
/*
* Format IP addresses in ascii
*
* (hacked-together non-safe version of the version in openvpn) */
const char *print_sockaddr_ex (const struct sockaddr *sa,
const char* separator, const unsigned int flags )
{
static char out[1000];
bool addr_is_defined;
char hostaddr[NI_MAXHOST] = "";
char servname[NI_MAXSERV] = "";
int status;
socklen_t salen;
switch(sa->sa_family)
{
case AF_INET:
strcpy(out, "[AF_INET]");
salen = sizeof (struct sockaddr_in);
addr_is_defined = ((struct sockaddr_in*) sa)->sin_addr.s_addr != 0;
break;
case AF_INET6:
strcpy(out, "[AF_INET6]");
salen = sizeof (struct sockaddr_in6);
addr_is_defined = !IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6*) sa)->sin6_addr);
break;
case AF_UNSPEC:
return "[AF_UNSPEC]";
default:
ASSERT(0);
}status = getnameinfo(sa, salen, hostaddr, sizeof (hostaddr),
servname, sizeof(servname), NI_NUMERICHOST | NI_NUMERICSERV);
if(status!=0) {
sprintf(&out[strlen(out)],"[nameinfo() err: %s]",gai_strerror(status));
return out;
}
if (!(flags & PS_DONT_SHOW_ADDR))
{
if (addr_is_defined)
strcat (out, hostaddr);
else
strcat (out, "[undef]");
}
if ((flags & PS_SHOW_PORT) || (flags & PS_SHOW_PORT_IF_DEFINED))
{
if (separator)
strcat (out, separator); strcat (out, servname);
} return out;
}
/* ------- mostly unchanged code from socket.c below this line ----- */
static socket_descriptor_tcreate_socket_udp (const int af, const unsigned int flags) {
socket_descriptor_t sd;
if ((sd = socket (af, SOCK_DGRAM, IPPROTO_UDP)) < 0)
msg (M_ERR, "UDP: Cannot create UDP/UDP6 socket");
#if ENABLE_IP_PKTINFO
else if (flags & SF_USE_IP_PKTINFO)
{
int pad = 1;
if(af == AF_INET)
{
#ifdef IP_PKTINFO
fprintf( stderr, "AF_INET/IP_PKTINFO enabled\n");
if (setsockopt (sd, SOL_IP, IP_PKTINFO,
(void*)&pad, sizeof(pad)) < 0)
msg(M_ERR, "UDP: failed setsockopt for IP_PKTINFO");
#elif defined(IP_RECVDSTADDR)
fprintf( stderr, "AF_INET/IP_RECVDSTADDR enabled\n");
if (setsockopt (sd, IPPROTO_IP, IP_RECVDSTADDR,
(void*)&pad, sizeof(pad)) < 0)
msg(M_ERR, "UDP: failed setsockopt for IP_RECVDSTADDR");
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
}
else if (af == AF_INET6 )
{
#ifndef IPV6_RECVPKTINFO /* Some older Darwin platforms require this */
fprintf( stderr, "AF_INET6/IPV6_PKTINFO enabled\n");
if (setsockopt (sd, IPPROTO_IPV6, IPV6_PKTINFO,
(void*)&pad, sizeof(pad)) < 0)
#else
fprintf( stderr, "AF_INET6/IPV6_RECVPKTINFO enabled\n");
if (setsockopt (sd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
(void*)&pad, sizeof(pad)) < 0)
#endif
msg(M_ERR, "UDP: failed setsockopt for IPV6_RECVPKTINFO");
}
}#endif
return sd;
}
voidsocket_bind (socket_descriptor_t sd,
char* port, int ai_family)
{
struct addrinfo hints;
struct addrinfo* cur;
CLEAR(hints); hints.ai_family = ai_family; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV; int s = getaddrinfo( NULL, port, &hints, &cur );
if ( s != 0 )
{ fprintf( stderr, "getaddrinfo failed: %s (%d)\n",
gai_strerror(s), s ); exit(1); }
if (ai_family == AF_INET6)
{
int v6only = 0; fprintf (stderr, "setsockopt(IPV6_V6ONLY=%d)\n", v6only);
if (setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)))
{
fprintf (stderr, "Setting IPV6_V6ONLY=%d failed\n", v6only);
}
}
if (bind (sd, cur->ai_addr, cur->ai_addrlen))
{
const int err = errno;
fprintf (stderr, "Socket bind failed on local address %s: %s\n",
print_sockaddr_ex (cur->ai_addr, ":", PS_SHOW_PORT),
strerror(err));
exit(1);
}
fprintf (stderr, "Socket bound to local address %s\n",
print_sockaddr_ex (cur->ai_addr, ":", PS_SHOW_PORT) );
}const char *print_link_socket_actual_ex (const struct link_socket_actual *act,
const char *separator, const unsigned int flags)
{
static char out[1000];
if (act)
{
char ifname[IF_NAMESIZE] = "[undef]";
strcpy (out, print_sockaddr_ex (&act->dest.addr.sa, separator, flags));
#if ENABLE_IP_PKTINFO
if ((flags & PS_SHOW_PKTINFO)) /* && addr_defined_ipi(act))*/
{
switch(act->dest.addr.sa.sa_family)
{
case AF_INET:
{
struct openvpn_sockaddr sa;
CLEAR (sa);
sa.addr.in4.sin_family = AF_INET;
#ifdef IP_PKTINFO
sa.addr.in4.sin_addr = act->pi.in4.ipi_spec_dst;
if_indextoname(act->pi.in4.ipi_ifindex, ifname);
#elif defined(IP_RECVDSTADDR)
sa.addr.in4.sin_addr = act->pi.in4;
ifname[0]=0;
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
sprintf (&out[strlen(out)], " (via %s%%%s)",
print_sockaddr_ex (&sa.addr.sa, separator, 0),
ifname);
}
break;
case AF_INET6:
{
struct sockaddr_in6 sin6;
char buf[INET6_ADDRSTRLEN] = "[undef]";
CLEAR(sin6);
sin6.sin6_family = AF_INET6;
sin6.sin6_addr = act->pi.in6.ipi6_addr;
if_indextoname(act->pi.in6.ipi6_ifindex, ifname);
if (getnameinfo((struct sockaddr *)&sin6, sizeof (struct sockaddr_in6),
buf, sizeof (buf), NULL, 0, NI_NUMERICHOST) == 0)
sprintf (&out[strlen(out)], " (via %s%%%s [%u])", buf, ifname, act->pi.in6.ipi6_ifindex);
else
sprintf (&out[strlen(out)], " (via [getnameinfo() err]%%%s)", ifname);
}
break;
}
}
#endif
return out;
}
else
return "[NULL]";
}struct openvpn_in4_pktinfo
{
struct cmsghdr cmsghdr;
#ifdef HAVE_IN_PKTINFO
struct in_pktinfo pi4;
#elif defined(IP_RECVDSTADDR)
struct in_addr pi4;
#endif
};
struct openvpn_in6_pktinfo
{
struct cmsghdr cmsghdr;uint32_t padding;
struct in6_pktinfo pi6;
};
union openvpn_pktinfo {
struct openvpn_in4_pktinfo msgpi4;
struct openvpn_in6_pktinfo msgpi6;
};#pragma pack()
static socklen_t link_socket_read_udp_posix_recvmsg (socket_descriptor_t sd,
char *buf, int maxsize, struct link_socket_actual *from, int *r_len)
{
struct iovec iov;
union openvpn_pktinfo opi;
struct msghdr mesg;
socklen_t fromlen = sizeof (from->dest.addr); iov.iov_base = buf; iov.iov_len = maxsize; mesg.msg_iov = &iov; mesg.msg_iovlen = 1; mesg.msg_name = &from->dest.addr; mesg.msg_namelen = fromlen; mesg.msg_control = &opi; mesg.msg_controllen = sizeof opi;
*r_len = recvmsg (sd, &mesg, 0);
if (*r_len >= 0)
{
struct cmsghdr *cmsg;
fromlen = mesg.msg_namelen;
cmsg = CMSG_FIRSTHDR (&mesg);
if (cmsg == NULL)
fprintf( stderr, "CMSG_FIRSTHDR()=NULL, no packet info available\n");
else
fprintf( stderr, "CMSG_NXTHDR=%p, level=%d, type=%d\n",
CMSG_NXTHDR (&mesg, cmsg), cmsg->cmsg_level, cmsg->cmsg_type );
if (cmsg != NULL
&& CMSG_NXTHDR (&mesg, cmsg) == NULL
#ifdef IP_PKTINFO
&& cmsg->cmsg_level == SOL_IP
&& cmsg->cmsg_type == IP_PKTINFO
&& cmsg->cmsg_len >= CMSG_LEN (sizeof(struct in_pktinfo)) )
#elif defined(IP_RECVDSTADDR)
&& cmsg->cmsg_level == IPPROTO_IP
&& cmsg->cmsg_type == IP_RECVDSTADDR
&& cmsg->cmsg_len >= CMSG_LEN (sizeof(struct in_addr)) )
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
{
#ifdef IP_PKTINFO
struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
from->pi.in4.ipi_ifindex = pkti->ipi_ifindex;
from->pi.in4.ipi_spec_dst = pkti->ipi_spec_dst;
fprintf( stderr, "IP_PKTINFO\n" );
#elif defined(IP_RECVDSTADDR)
from->pi.in4 = *(struct in_addr*) CMSG_DATA (cmsg);
fprintf( stderr, "IP_RECVDSTADDR\n" );
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
}
else if (cmsg != NULL
&& CMSG_NXTHDR (&mesg, cmsg) == NULL
&& cmsg->cmsg_level == IPPROTO_IPV6
&& cmsg->cmsg_type == IPV6_PKTINFO
&& cmsg->cmsg_len >= CMSG_LEN(sizeof(struct in6_pktinfo)) )
{
struct in6_pktinfo *pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
from->pi.in6.ipi6_ifindex = pkti6->ipi6_ifindex;
from->pi.in6.ipi6_addr = pkti6->ipi6_addr;
fprintf( stderr, "IPV6_PKTINFO\n" );
}
} return fromlen;
}
size_tlink_socket_write_udp_posix_sendmsg (socket_descriptor_t sd,
char *buf, int len, struct link_socket_actual *to)
{
struct iovec iov;
struct msghdr mesg;
struct cmsghdr *cmsg;
union openvpn_pktinfo opi;
iov.iov_base = buf;
iov.iov_len = len;
mesg.msg_iov = &iov;
mesg.msg_iovlen = 1;
switch (to->ai_family)
{
case AF_INET:
{
mesg.msg_name = &to->dest.addr.sa;
mesg.msg_namelen = sizeof (struct sockaddr_in);
mesg.msg_control = &opi;
mesg.msg_flags = 0;
#ifdef HAVE_IN_PKTINFO
mesg.msg_controllen = CMSG_SPACE (sizeof (struct in_pktinfo));
cmsg = CMSG_FIRSTHDR (&mesg);
cmsg->cmsg_len = CMSG_LEN (sizeof(struct in_pktinfo));
cmsg->cmsg_level = SOL_IP;
cmsg->cmsg_type = IP_PKTINFO;
{
struct in_pktinfo *pkti;
pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
pkti->ipi_ifindex = to->pi.in4.ipi_ifindex;
pkti->ipi_spec_dst = to->pi.in4.ipi_spec_dst;
pkti->ipi_addr.s_addr = 0;
}
#elif defined(IP_RECVDSTADDR)
ASSERT( CMSG_SPACE(sizeof (struct in_addr)) <= sizeof(opi) );
mesg.msg_controllen = CMSG_SPACE(sizeof (struct in_addr));
cmsg = CMSG_FIRSTHDR (&mesg);
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_RECVDSTADDR;
*(struct in_addr *) CMSG_DATA (cmsg) = to->pi.in4;
#else
#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
#endif
break;
}
case AF_INET6:
{
struct in6_pktinfo *pkti6;
mesg.msg_name = &to->dest.addr.sa;
mesg.msg_namelen = sizeof (struct sockaddr_in6);
mesg.msg_control = &opi;
mesg.msg_controllen = CMSG_SPACE(sizeof (struct in6_pktinfo));
mesg.msg_flags = 0;
cmsg = CMSG_FIRSTHDR (&mesg);
cmsg->cmsg_len = CMSG_LEN(sizeof (struct in6_pktinfo));
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
fprintf( stderr, "control=%p, controllen=%d, pkti6=%p, struct cmsghdr=%d bytes\n",
mesg.msg_control, (int)mesg.msg_controllen,
(void*)pkti6, (int)sizeof(struct cmsghdr));
pkti6->ipi6_ifindex = to->pi.in6.ipi6_ifindex;
pkti6->ipi6_addr = to->pi.in6.ipi6_addr;
break;
}
default: ASSERT(0);
} return sendmsg (sd, &mesg, 0);
}
void loop(int sd,int af)
{
int r_len, fromlen, r;
char buf[10000];
struct link_socket_actual from;
while(1)
{
CLEAR(from);
fprintf( stderr, "--\n" );
fromlen = link_socket_read_udp_posix_recvmsg (sd, buf, sizeof(buf), &from, &r_len);
fprintf( stderr, "read: fromlen=%d, r_len=%d\n", fromlen, r_len );
fprintf( stderr, " from=%s\n",
print_link_socket_actual_ex(&from, ":", PS_SHOW_PORT|PS_SHOW_PKTINFO) );
/* send back response (just mirror packet) */
from.ai_family = af;
r = link_socket_write_udp_posix_sendmsg(sd, buf, r_len, &from);
if ( r<0 )
fprintf( stderr, "sendmsg() returns r=%d: %s\n", r, strerror(errno) );
}
}int main(int argc, char **argv)
{
int s;
int af = AF_INET6;
char* port = "50001";
if ( argc > 1 && argv[1][0] == '4' ) { af = AF_INET; }
if ( argc > 2 ) { port = argv[2]; }
s = create_socket_udp( af, SF_USE_IP_PKTINFO );
socket_bind( s, port, af );
loop(s,af);
exit(0);
}* Gert Doering
Quoted 11 lines · Tore Anderson
Quoted 8 lines
FWIW: Now I also got to test git master with what I wanted to in the first place, namely to see whether or not all the nice dualstack changes had also made "multihome" work for IPv4-mapped clients coming in to the server on an udp6 socket (ticket #306). Unfortunately that's still broken, and it seems that for a server process "proto udp" is still IPv4-only. So it doesn't appear to be possible to get a functioning dual-stacked UDP server (that has multiple addresses/interfaces) at the moment.
FWIW, that seems to be a Linux oddity.
Now that has been fixed upstream (Linux 3.14), I tried again. Here I am connecting from 84.209.94.36 to 87.238.35.254, which is a secondary address on the server (87.238.35.145 is the primary). The OpenVPN server logs:
::ffff:84.209.94.36 TLS: Initial packet from [AF_INET6]::ffff:84.209.94.36:39970 (via ::ffff:87.238.35.254%eth0), sid=a79ae4ef 7abb8215
So the Linux 3.14 fix seems to work, as OpenVPN now does see which IPv4 address the server was contacted on. Progress! However the VPN doesn't come up, and tcpdump reveals why - OpenVPN is responding from the primary address of the server rater than the one it was contacted on:
20:21:02.555667 IP 84.209.94.36.39970 > 87.238.35.254.1194: UDP, length 14 20:21:02.556719 IP 87.238.35.145.1194 > 84.209.94.36.39970: UDP, length 26
So we're closer, but not quite there yet unfortunately...
This happens both with today's git master and with 2.3.2. BTW Arne's patch to socket.c that fixes the assert crash we originally discussed still isn't included in git master, has it been forgotten about?
Tore
Hi,
Quoted 6 lines · Tore Anderson
Now that has been fixed upstream (Linux 3.14), I tried again. Here I am connecting from 84.209.94.36 to 87.238.35.254, which is a secondary address on the server (87.238.35.145 is the primary). The OpenVPN server logs:
::ffff:84.209.94.36 TLS: Initial packet from [AF_INET6]::ffff:84.209.94.36:39970 (via ::ffff:87.238.35.254%eth0), sid=a79ae4ef
7abb8215
Cool :-)
Quoted 9 lines
So the Linux 3.14 fix seems to work, as OpenVPN now does see which IPv4 address the server was contacted on. Progress! However the VPN doesn't come up, and tcpdump reveals why - OpenVPN is responding from the primary address of the server rater than the one it was contacted on:
20:21:02.555667 IP 84.209.94.36.39970 > 87.238.35.254.1194: UDP, length 14 20:21:02.556719 IP 87.238.35.145.1194 > 84.209.94.36.39970: UDP, length 26
So we're closer, but not quite there yet unfortunately...
Could you run my test program to see whether that gets it right?
http://www.greenie.net/ipv6/mhome.c
For some platforms I also had to change the sending part (data structures, alignment, whatnot) and that has not yet been integrated into OpenVPN proper, and I think Linux was among them, at least for 64bit.
Quoted 3 lines
This happens both with today's git master and with 2.3.2. BTW Arne's patch to
socket.cthat fixes the assert crash we originally discussed still isn't included in git master, has it been forgotten about?
ISTR that Arne wrote something about "this is a quick fix, will send a proper patch later". Then we lost a bit of momentum :-(
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Quoted 7 lines
Quoted 4 lines
This happens both with today's git master and with 2.3.2. BTW Arne's patch to
socket.cthat fixes the assert crash we originally discussed still isn't included in git master, has it been forgotten about?ISTR that Arne wrote something about "this is a quick fix, will send a proper patch later". Then we lost a bit of momentum :-(
I did more socket cleanup to remove the offending variable that triggers that problem:
https://github.com/schwabe/openvpn/commit/94a142d4e8bd09f8a7af4287516c0c34164ce1f4
but the patchset is only really tested in client mode and not server mode and therefore I have not sent it to openvpn-devel yet.
Arne
Hi Gert,
Quoted 1 line
Could you run my test program to see whether that gets it right?
It doesn't:
server$ mhome
AF_INET6/IPV6_RECVPKTINFO enabledsetsockopt(IPV6_V6ONLY=0)
Socket bound to local address [AF_INET6][undef]:50001
--
CMSG_NXTHDR=(nil), level=41, type=50IPV6_PKTINFO
read: fromlen=28, r_len=5
from=[AF_INET6]::ffff:84.209.94.36:35711 (via ::ffff:87.238.35.254%eth0 [2])
control=0x7fff699af0a0, controllen=40, pkti6=0x7fff699af0b0, struct cmsghdr=16 bytes --
server$ tcpdump -ni eth0 port 50001
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 07:11:26.375252 IP 84.209.94.36.35711 > 87.238.35.254.50001: UDP, length 5 07:11:26.376051 IP 87.238.35.145.50001 > 84.209.94.36.35711: UDP, length 5
client$ echo ping | nc -4u 87.238.35.254 50001
Quoted 3 lines
For some platforms I also had to change the sending part (data structures, alignment, whatnot) and that has not yet been integrated into OpenVPN proper, and I think Linux was among them, at least for 64bit.
This is 64-bit Linux, yes:
server$ uname -a
Linux greed.fud.no 3.14.0-031400rc2-generic #201402092235 SMP Mon Feb 10 03:37:33 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Tore
Hi,
Quoted 3 lines · Tore Anderson
Quoted 1 line
Could you run my test program to see whether that gets it right?
It doesn't:
Meh!
Quoted 1 line
This is 64-bit Linux, yes:
I'll re-test. I *did* test it on mine...
Linux gentoo 3.3.8-gentoo #1 SMP Fri Jun 29 14:10:01 CEST 2012 x86_64 Intel(R) Xeon(R) CPU X7350 @ 2.93GHz GenuineIntel GNU/Linux
... but after the first cup of tea this morning, my brain is starting to wake up - of course I couldn't test sending, as v4 already broke for receiving (v6 worked).
Kernel update time... (hrm, and maybe I should clone the VM beforehand)
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Hi,
Quoted 3 lines · Arne Schwabe
That is very weird. I would expect it to fail for both cases in the same way. The codepath should trigger the same ASSERT(0) in both cases (or work in both).
... unless something is overwriting to->ai_family, for example by copying a too-large address structure. There is a trac bug about --multihome that says we're using the wrong size (#327) which could be an indication.
(I have not yet investigated #327 more closely - that one is about sendmsg(), but maybe we do bad things on receive as well)
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Am 10.01.14 15:07, schrieb Gert Doering:
Quoted 6 lines
Hi,
Quoted 3 lines · Arne Schwabe
That is very weird. I would expect it to fail for both cases in the same way. The codepath should trigger the same ASSERT(0) in both cases (or work in both).
Quoted 7 lines
... unless something is overwriting
to->ai_family, for example by copying a too-large address structure. There is a trac bug about --multihome that says we're using the wrong size (#327) which could be an indication.(I have not yet investigated #327 more closely - that one is about
sendmsg(), but maybe we do bad things on receive as well)
But
[AF_INET6]::ffff:87.238.42.150:47707
and
[AF_INET6]2a02:c0:100:0:9e8e:99ff:fed1:5243:56277
should be same the same size and handled both the same way from OpenVPN since they are both IPv6.
Arne
Hi,
Quoted 9 lines · Arne Schwabe
But
[AF_INET6]::ffff:87.238.42.150:47707
and
[AF_INET6]2a02:c0:100:0:9e8e:99ff:fed1:5243:56277
should be same the same size and handled both the same way from OpenVPN since they are both IPv6.
Good point. So why are we ASSERT(0)ing?
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...