Page 1 of 1

Nat trafic behind the client won't reache the Tun0 Interface

Posted: Tue Jun 04, 2019 1:50 pm
by profetaii
Hello,

I have deploy a setup for the traffic that is behind the client to be nat to a diferent subnet(10.9.1.0 255.255.255.240)
In the client I see the tcpdump in the tunnel interface with the traffic already natted with the subnet that I define,
but on the server side tunnel interface nothing appears.
I have configure the client-config-dir ccd and in the route table of the server this subnet has routing but with no luck.

*****Server******
Croot@pc:/etc/openvpn# tcpdump -i tun0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on tun0, link-type RAW (Raw IP), capture size 262144 bytes
^C
0 packets captured
0 packets received by filter
0 packets dropped by kernel
root@pc:/etc/openvpn#

root@pc:/etc/openvpn# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 0 0 0 enp0s3
10.8.0.0 0.0.0.0 255.255.0.0 U 0 0 0 tun0
10.9.1.0 10.8.0.2 255.255.255.240 UG 0 0 0 tun0
link-local 0.0.0.0 255.255.0.0 U 1000 0 0 enp0s3
172.31.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3

root@pc:/etc/openvpn# cat server.conf
local 172.31.1.140
port 1194
proto udp
dev tun

ca ca.crt
cert OpenVPN-Srv.crt
key OpenVPN-Srv.key # This file should be kept secret

dh dh.pem

topology subnet

server 10.8.0.0 255.255.0.0

ifconfig-pool-persist /var/log/openvpn/ipp.txt

;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100

;server-bridge

##RESERVE IP
;push "route 10.8.0.0 255.255.0.0"
push "route 172.31.1.0 255.255.255.0"

##RESERVE IP
client-config-dir ccd
route 10.9.1.0 255.255.255.240

;learn-address ./script

push "redirect-gateway def1 bypass-dhcp"

;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"

;client-to-client

;duplicate-cn

keepalive 10 120

tls-auth ta.key 0 # This file is secret
key-direction 0
cipher AES-256-CBC
auth SHA256

;compress lz4-v2
;push "compress lz4-v2"
;comp-lzo

max-clients 256

user nobody
group nogroup

persist-key
persist-tun

status /var/log/openvpn/openvpn-status.log

log /var/log/openvpn/openvpn.log
log-append /var/log/openvpn/openvpn.log

;verb 6

;mute 20

;explicit-exit-notify 1

****Client****
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on tun0, link-type RAW (Raw IP), capture size 262144 bytes
14:07:34.845666 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23426, length 40
14:07:39.414788 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23427, length 40
14:07:44.414406 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23428, length 40
14:07:49.414856 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23429, length 40
14:07:54.415613 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23430, length 40
14:07:59.416217 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23431, length 40
14:08:04.415035 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23432, length 40
14:08:09.415608 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23433, length 40
14:08:14.415104 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23434, length 40
14:08:19.416200 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23435, length 40
14:08:24.415511 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23436, length 40
14:08:29.417222 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23437, length 40
14:08:34.415518 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23438, length 40
14:08:38.272179 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23439, length 40
14:08:45.047177 IP 10.9.1.8.2769 > 172.31.1.140.openvpn: Flags [S], seq 1078605312, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
14:08:48.044475 IP 10.9.1.8.2769 > 172.31.1.140.openvpn: Flags [S], seq 1078605312, win 8192, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
14:08:54.046454 IP 10.9.1.8.2769 > 172.31.1.140.openvpn: Flags [S], seq 1078605312, win 8192, options [mss 1460,nop,nop,sackOK], length 0
14:09:09.472731 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23440, length 40
14:09:14.418068 IP 10.9.1.8 > 172.31.1.140: ICMP echo request, id 1, seq 23441, length 40


root@hostname:~ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.8.0.1 128.0.0.0 UG 0 0 0 tun0
default 172.24.1.1 0.0.0.0 UG 20 0 0 br0
10.8.0.0 * 255.255.0.0 U 0 0 0 tun0
128.0.0.0 10.8.0.1 128.0.0.0 UG 0 0 0 tun0
172.24.1.0 * 255.255.255.0 U 0 0 0 br0
172.31.1.0 10.8.0.1 255.255.255.0 UG 0 0 0 tun0
172.31.1.1 172.24.1.1 255.255.255.255 UGH 0 0 0 br0
224.0.0.0 * 224.0.0.0 U 0 0 0 br0



root@hostname:~ cat /home/user/iptables.sh
echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -F
iptables -t nat -F

iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

### NAT ROUTING
iptables -t nat -A POSTROUTING -s 172.24.1.143 -o tun0 -j SNAT --to 10.9.1.8
iptables -t nat -A PREROUTING -s 10.8.0.1 -d 10.9.1.8 -i tun0 -j DNAT --to 172.24.1.143




root@hostname:~ cat /etc/openvpn/client1.conf
##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server. #
# #
# This configuration can be used by multiple #
# clients, however each client should have #
# its own cert and key files. #
# #
# On Windows, you might want to rename this #
# file so it has a .ovpn extension #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one. On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server? Use the same setting as
# on the server.
;proto tcp
proto udp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote 172.31.1.1 1194
;remote my-server-2 1194

# Choose a random host from the remote
# list for load-balancing. Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server. Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here. See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets. Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
#ca ca.crt
#cert client.crt
#key client.key

# Verify server certificate by checking that the
# certicate has the correct key usage set.
# This is an important precaution to protect against
# a potential attack discussed here:
# http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the keyUsage set to
# digitalSignature, keyEncipherment
# and the extendedKeyUsage to
# serverAuth
# EasyRSA can do this for you.
remote-cert-tls server

# If a tls-auth key is used on the server
# then every client must also have the key.
#tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
# Note that v2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the ncp-cipher option in the manpage
cipher AES-256-CBC
auth SHA256
key-direction 1

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
#comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20
script-security 2
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf