Setting source address for UDP replies on multihomed server
My server that I hope to use as an OPENVPN server is multi-homed. i.e. it has multiple network interfaces and multiple addresses on multiple subnets.
When my openvpn client (on my notebook) tries to talk to it, it sometimes gets a reply from a different IP address than it sent the request to, and it doesn't like that.
This can be alleviated by using the --float option. However that isn't a complete solution.
When I try using openvpn from home, behind a NAT (masquerading) firewall, the reply from a different IP address doesn't get back to me, as the firewall doesn't know where to route those UDP packets.
The "correct" solution would be to have openvpn get the destination address that was used for each incoming packet, record that, and set is as the source address when sending a reply.
This is relatively easy to do (at least in Linux, possibly other POSIX os's). The code link_socket_read_udp_posix in the patch below successfully gets the destination address, and the code added to link_socket_write_udp_posix sets the source address properly. I have checked this by removing the "if (0)" in the latter function, so the address is passed around in a global variable.
Where I am having trouble is in tracing the path that an address takes when it is extract from an incoming packets, and then used to direct an outgoing reply. As you can see from the patch below, I have tried to carry the extra address around various places, but I still haven't got it to work.
I admit that the code as it is (apart from not working) is rather ugly. It would probably be best to define a new structure that contains both the Local and Remote addresses, and pass that around as necessary. But first I would like to get it to work.
So my request is:
What am I missing? Where else to I need to pass around the local-endpoint address?
NeilBrown
### Diffstat output
./forward.c | 12 ++++++----- ./openvpn.h | 1 ./socket.c | 45 +++++++++++++++++++++++++++++++++++++++++- ./socket.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++++----------- ./ssl.c | 25 ++++++++++++++++++----- ./ssl.h | 4 +++ 6 files changed, 127 insertions(+), 23 deletions(-)
forward.c renamed from forward.c~current~ +7 -5
@@ -87,6 +87,7 @@ check_tls_dowork (struct context *c) {if (tls_multi_process (c->c2.tls_multi, &c->c2.to_link, &c->c2.to_link_addr, + &c->c2.to,get_link_socket_info (c), &wakeup)) {update_time ();
@@ -342,7 +343,7 @@ encrypt_sign (struct context *c, bool co* Get the address we will be sending the packet to. */ link_socket_get_outgoing_addr (&c->c2.buf,get_link_socket_info (c), - &c->c2.to_link_addr); + &c->c2.to_link_addr, &c->c2.to);#ifdef USE_CRYPTO #ifdef USE_SSL /*
@@ -505,7 +506,7 @@ read_incoming_link (struct context *c) c->c2.buf = c->c2.buffers->read_link_buf;ASSERT (buf_init (&c->c2.buf,FRAME_HEADROOM (&c->c2.frame))); - status =link_socket_read (c->c2.link_socket, &c->c2.buf,MAX_RW_SIZE_LINK (&c->c2.frame), &c->c2.from); + status =link_socket_read (c->c2.link_socket, &c->c2.buf,MAX_RW_SIZE_LINK (&c->c2.frame), &c->c2.from, &c->c2.to);if (socket_connection_reset (c->c2.link_socket, status)) {
@@ -591,7 +592,7 @@ process_incoming_link (struct context *c* and return false. */ //tls_mutex_lock (c->c2.tls_multi); -if (tls_pre_decrypt (c->c2.tls_multi, &c->c2.from, &c->c2.buf, &c->c2.crypto_options)) +if (tls_pre_decrypt (c->c2.tls_multi, &c->c2.from, &c->c2.to, &c->c2.buf, &c->c2.crypto_options)) {interval_action (&c->c2.tmp_int);
@@ -641,7 +642,7 @@ process_incoming_link (struct context *c* Also, update the persisted version of our packet-id. */ if (!TLS_MODE ) -link_socket_set_outgoing_addr (&c->c2.buf, lsi, &c->c2.from,NULL ); +link_socket_set_outgoing_addr (&c->c2.buf, lsi, &c->c2.from, &c->c2.to,NULL );/* reset packet received timer */ if (c->options.ping_rec_timeout && c->c2.buf.len >0 )
@@ -826,7 +827,8 @@ process_outgoing_link (struct context *csocks_preprocess_outgoing_link (c, &to_addr, &size_delta);/* Send packet */ - size =link_socket_write (c->c2.link_socket, &c->c2.to_link, to_addr); + size =link_socket_write (c->c2.link_socket, &c->c2.to_link, to_addr, + &c->c2.to);/* Undo effect of prepend */ link_socket_write_post_size_adjust (&size, size_delta, &c->c2.to_link);
openvpn.h renamed from openvpn.h~current~ +1 -0
@@ -168,6 +168,7 @@ struct context_2struct sockaddr_in to_link_addr;/* IP address of remote */ struct sockaddr_in from;/* address of incoming datagram */ +struct sockaddr_in to;/* address incoming datagram was to */ /* MTU frame parameters */ struct frame frame;
socket.c renamed from socket.c~current~ +44 -1
80 lines not on this page · raw message
socket.h renamed from socket.h~current~ +52 -11
147 lines not on this page · raw message
ssl.c renamed from ssl.c~current~ +19 -6
@@ -1494,6 +1494,7 @@ write_control_auth (struct tls_session *struct key_state *ks, struct buffer *buf, struct sockaddr_in *to_link_addr, + struct sockaddr_in *from_link_addr, int opcode, int max_ack, bool prepend_ack)
@@ -1514,6 +1515,7 @@ write_control_auth (struct tls_session *ASSERT (swap_hmac (buf, &session->tls_auth, false)); } *to_link_addr = ks->remote_addr; + *from_link_addr = ks->local_addr; }/*
@@ -1916,6 +1918,7 @@ key_state_soft_reset (struct tls_sessionkey_state_init (session, ks); ks->session_id_remote = ks_lame->session_id_remote; ks->remote_addr = ks_lame->remote_addr; + ks->local_addr = ks_lame->local_addr; }/*
@@ -1932,6 +1935,7 @@ tls_process (struct tls_multi *multi,struct tls_session *session, struct buffer *to_link, struct sockaddr_in *to_link_addr, + struct sockaddr_in *from_link_addr, struct link_socket_info *to_link_socket_info, interval_t *wakeup) {
@@ -2052,7 +2056,8 @@ tls_process (struct tls_multi *multi,INCR_SUCCESS ;/* Set outgoing address for data channel packets */ -link_socket_set_outgoing_addr (NULL , to_link_socket_info, &ks->remote_addr, session->common_name); +link_socket_set_outgoing_addr (NULL , to_link_socket_info, &ks->remote_addr, + &ks->local_addr, session->common_name);#ifdef MEASURE_TLS_HANDSHAKE_STATS show_tls_performance_stats ();
@@ -2072,7 +2077,8 @@ tls_process (struct tls_multi *multi, b = *buf;INCR_SENT ; -write_control_auth (session, ks, &b, to_link_addr, opcode, +write_control_auth (session, ks, &b, to_link_addr, + from_link_addr, opcode,CONTROL_SEND_ACK_MAX , true); *to_link = b; active = true;
@@ -2087,7 +2093,8 @@ tls_process (struct tls_multi *multi, { buf = &ks->ack_write_buf;ASSERT (buf_init (buf,FRAME_HEADROOM (&multi->opt.frame))); -write_control_auth (session, ks, buf, to_link_addr,P_ACK_V1 , +write_control_auth (session, ks, buf, to_link_addr, from_link_addr, +P_ACK_V1 ,RELIABLE_ACK_SIZE , false); *to_link = *buf; active = true;
@@ -2391,7 +2398,7 @@ tls_process (struct tls_multi *multi, { buf = &ks->ack_write_buf;ASSERT (buf_init (buf,FRAME_HEADROOM (&multi->opt.frame))); -write_control_auth (session, ks, buf, to_link_addr,P_ACK_V1 , +write_control_auth (session, ks, buf, to_link_addr, from_link_addr,P_ACK_V1 ,RELIABLE_ACK_SIZE , false); *to_link = *buf; active = true;
@@ -2453,6 +2460,7 @@ bool tls_multi_process (struct tls_multi *multi, struct buffer *to_link, struct sockaddr_in *to_link_addr, + struct sockaddr_in *from_link_addr, struct link_socket_info *to_link_socket_info, interval_t *wakeup) {
@@ -2473,8 +2481,10 @@ tls_multi_process (struct tls_multi *mul/* set initial remote address */ if (i ==TM_ACTIVE && ks->state ==S_INITIAL && -addr_defined (&to_link_socket_info->lsa->actual)) +addr_defined (&to_link_socket_info->lsa->actual)) { ks->remote_addr = to_link_socket_info->lsa->actual; + ks->local_addr = to_link_socket_info->lsa->actuallocal; + } msg (D_TLS_DEBUG, "TLS: tls_multi_process: i=%d state=%s, mysid=%s, stored-sid=%s, stored-ip=%s",
@@ -2488,7 +2498,7 @@ tls_multi_process (struct tls_multi *mul {update_time (); -if (tls_process (multi, session, to_link, to_link_addr, +if (tls_process (multi, session, to_link, to_link_addr, from_link_addr, to_link_socket_info, wakeup)) active = true;
@@ -2617,6 +2627,7 @@ tls_rec_payload (struct tls_multi *multibool tls_pre_decrypt (struct tls_multi *multi,struct sockaddr_in *from, +struct sockaddr_in *to,struct buffer *buf,struct crypto_options *opt) {
@@ -2919,6 +2930,7 @@ tls_pre_decrypt (struct tls_multi *multi { ks->session_id_remote = sid; ks->remote_addr = *from; + ks->local_addr = *to; ++multi->n_sessions; } else if (!addr_port_match (&ks->remote_addr, from))
@@ -3023,6 +3035,7 @@ tls_pre_decrypt (struct tls_multi *multibool tls_pre_decrypt_lite (const struct tls_auth_standalone *tas,const struct sockaddr_in *from, +const struct sockaddr_in *to,const struct buffer *buf) {struct gc_arena gc =gc_new ();
ssl.h renamed from ssl.h~current~ +4 -0
@@ -236,6 +236,7 @@ struct key_stateint initial_opcode;/* our initial P_ opcode */ struct session_id session_id_remote;/* peer's random session ID */ struct sockaddr_in remote_addr;/* peer's IP addr */ +struct sockaddr_in local_addr;/* address peer expects reply from */ struct packet_id packet_id;/* for data channel, to prevent replay attacks */ struct key_ctx_bi key;/* data channel keys for encrypt/decrypt/hmac */
@@ -456,6 +457,7 @@ void tls_multi_init_set_options(struct tbool tls_multi_process (struct tls_multi *multi,struct buffer *to_link,struct sockaddr_in *to_link_addr, +struct sockaddr_in *from_link_addr,struct link_socket_info *to_link_socket_info,interval_t *wakeup);
@@ -463,11 +465,13 @@ void tls_multi_free (struct tls_multi *mbool tls_pre_decrypt (struct tls_multi *multi,struct sockaddr_in *from, +struct sockaddr_in *to,struct buffer *buf,struct crypto_options *opt);bool tls_pre_decrypt_lite (const struct tls_auth_standalone *tas,const struct sockaddr_in *from, +const struct sockaddr_in *to,const struct buffer *buf); void tls_pre_encrypt (struct tls_multi *multi,
Hi Neil.
Quoted 18 lines · Neil Brown
My server that I hope to use as an OPENVPN server is multi-homed. i.e. it has multiple network interfaces and multiple addresses on multiple subnets.
When my openvpn client (on my notebook) tries to talk to it, it sometimes gets a reply from a different IP address than it sent the request to, and it doesn't like that.
This can be alleviated by using the --float option. However that isn't a complete solution.When I try using openvpn from home, behind a NAT (masquerading) firewall, the reply from a different IP address doesn't get back to me, as the firewall doesn't know where to route those UDP packets.
The "correct" solution would be to have openvpn get the destination address that was used for each incoming packet, record that, and set is as the source address when sending a reply.
Quoted 6 lines
This is relatively easy to do (at least in Linux, possibly other POSIX os's). The code
link_socket_read_udp_posixin the patch below successfully gets the destination address, and the code added tolink_socket_write_udp_posixsets the source address properly. I have checked this by removing the "if (0)" in the latter function, so the address is passed around in a global variable.
This is a common problem with UDP servers.
Recently I tried to fix this in ntp. To my great surprise and disappoinment, ntp folks do not want it to be fixed.
Details at http://bugzilla.ntp.org/show_bug.cgi?id=314
I hope openvpn folks have clue and your patch will fare better. --
vda
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 3 lines · Neil Brown
My server that I hope to use as an OPENVPN server is multi-homed. i.e. it has multiple network interfaces and multiple addresses on multiple subnets.
I have the same problem on my multihomed server, which is running PBR (policy based routing).
eth0 is the device with the default route.
eth3.81 ist a device (on a VLAN trunk) with vlan 81
I have a PBR setup which routes traffic outgoing on eth3.81 through another default gateway connected to eth3.81.
Like this:
ifconfig eth0 192.168.2.100
route add default gw 192.168.2.1 dev eth0
ifconfig eth3.81 192.168.1.100
ip rule add from 192.168.1.100 lookup table 100
ip route add default via 192.168.1.1 table 100
ip route flush cache
OpenVPN seems to answer all incoming OpenVPN connections on eth3.81to 192.168.1.100 through the default route on eth0 with source adress 192.168.2.100
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFA7qzrosmtDiD9dbIRArbEAKCcopV+QLnMPDye4Tv+6OWEyr/jtwCgvnam EMkoCEWy4jku32YPl7Wwf2I=
=YYVq
-----END PGP SIGNATURE-----
Quoted 31 lines · Neil Brown
My server that I hope to use as an OPENVPN server is multi-homed. i.e. it has multiple network interfaces and multiple addresses on multiple subnets.
When my openvpn client (on my notebook) tries to talk to it, it sometimes gets a reply from a different IP address than it sent the request to, and it doesn't like that.
This can be alleviated by using the --float option. However that isn't a complete solution.When I try using openvpn from home, behind a NAT (masquerading) firewall, the reply from a different IP address doesn't get back to me, as the firewall doesn't know where to route those UDP packets.
The "correct" solution would be to have openvpn get the destination address that was used for each incoming packet, record that, and set is as the source address when sending a reply.
This is relatively easy to do (at least in Linux, possibly other POSIX os's). The code
link_socket_read_udp_posixin the patch below successfully gets the destination address, and the code added tolink_socket_write_udp_posixsets the source address properly. I have checked this by removing the "if (0)" in the latter function, so the address is passed around in a global variable.Where I am having trouble is in tracing the path that an address takes when it is extract from an incoming packets, and then used to direct an outgoing reply. As you can see from the patch below, I have tried to carry the extra address around various places, but I still haven't got it to work.
Quoted 8 lines
I admit that the code as it is (apart from not working) is rather ugly. It would probably be best to define a new structure that contains both the Local and Remote addresses, and pass that around as necessary. But first I would like to get it to work.
So my request is:
What am I missing? Where else to I need to pass around the local-endpoint address?
I think you have the right idea.
You want to make a new sockaddr_in in struct link_socket_info to hold the destination address of received datagrams which will then be used as the source address of sent datagrams.
link_socket_get_outgoing_addr is called to save the destination address for future outgoing datagrams, based on the source address of a recently received datagram which has successfully authenticated. Here you also want to save the destination address of the received datagram in your new link_socket_info member.
link_socket_set_outgoing_addr is called to get the previously saved destination address for a pending outgoing packet. Here you can get your previously saved source address for the outgoing packet as well.
James