redirect-gateway flag for port-level direct route
Posted: Mon Mar 30, 2015 4:30 am
Hi buddies,
The current openvpn implementation --redirect-gateway def1 adds a direct route to the default routing table. This means that traffic to other ports of the vpn server ip will go outside the vpn tunnel. This can be fixed with the following networky bash commands:
(this could be the up script)
VPNIP=1.2.3.4
VPNPORT=1194
DEFDEV=$(ip route show | grep "default via" | sed -n "s/.*default via [^ ]* dev \([^ ]*\).*/\1/p")
DEFIP=$(ip route show | grep "default via" | sed -n 's/.*default via \([^ ]*\).*/\1/p')
sudo ip route add default via "$DEFIP" table 3
sudo ip rule add fwmark 2 table 3
sudo iptables --table mangle --append OUTPUT --protocol udp --destination "$VPNIP" --destination-port "$VPNPORT" --jump MARK --set-mark 2
sudo iptables --table nat --append POSTROUTING --match mark --mark 2 --jump MASQUERADE
sudo sysctl -w "net.ipv4.conf.${DEFDEV}.rp_filter=2"
sudo ip route del "$VPNIP" via "$DEFIP" dev "$DEFDEV"
Could there be a flag that kind of does this or some other simplification, please?
The current openvpn implementation --redirect-gateway def1 adds a direct route to the default routing table. This means that traffic to other ports of the vpn server ip will go outside the vpn tunnel. This can be fixed with the following networky bash commands:
(this could be the up script)
VPNIP=1.2.3.4
VPNPORT=1194
DEFDEV=$(ip route show | grep "default via" | sed -n "s/.*default via [^ ]* dev \([^ ]*\).*/\1/p")
DEFIP=$(ip route show | grep "default via" | sed -n 's/.*default via \([^ ]*\).*/\1/p')
sudo ip route add default via "$DEFIP" table 3
sudo ip rule add fwmark 2 table 3
sudo iptables --table mangle --append OUTPUT --protocol udp --destination "$VPNIP" --destination-port "$VPNPORT" --jump MARK --set-mark 2
sudo iptables --table nat --append POSTROUTING --match mark --mark 2 --jump MASQUERADE
sudo sysctl -w "net.ipv4.conf.${DEFDEV}.rp_filter=2"
sudo ip route del "$VPNIP" via "$DEFIP" dev "$DEFDEV"
Could there be a flag that kind of does this or some other simplification, please?