大网数据广告活动
服务器运维问答

iptables forward traffic coming from openvpn tunnel to lan

· 52CE 我爱测

Below is the network topology: ------------------------ 123.45.67.89 (WAN) ---------------------- | pfSense |-------------------------| Public client | ------------------------ ---------------------- | 10.1.1.1 (tun) | | | 10.1.1…

问题原文

原帖链接 · Lefteris · CC BY-SA 4.0

Below is the network topology:

------------------------ 123.45.67.89 (WAN)      ----------------------
|    pfSense           |-------------------------|     Public client  |
------------------------                         ----------------------
         | 10.1.1.1 (tun)
         |
         |
         | 10.1.1.2 (tun)
----------------------- 192.168.0.2 (LAN)   192.168.0.3 (LAN)----------------
|       RPi           | -------------------------------------|  VNC Server  | 
-----------------------                                      ----------------

Scenario:

  1. Public client accesses pfSense on WAN ip and port 5900
  2. Traffic is forwarded by pfSense to OpenVPN ip 10.1.1.2:5900 (RPi)
  3. RPi performs SNAT and DNAT and forwards to 192.168.0.3:5900 (VNC Server)
  4. VNC Server responds to Source IP, i.e. 192.168.0.2
  5. PROBLEM: Response is not forwarded to pfSense by RPi, unless I set it up so that all traffic goes through tun (using routes). However, I would like only the responses from VNC Server, related to VNC traffic to go through tun interface.

Below is the iptables setup on RPi

pi@raspberrypi:~ $ sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNAT       tcp  --  anywhere             anywhere             tcp dpts:5900:5905 to:192.168.0.3:5900-5905

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
SNAT       tcp  --  anywhere             anywhere             tcp dpts:5900:5905 to:192.168.0.2

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

I though about puting ip 10.1.1.2 in SNAT but since VNC Server does not know how to route this subnet, I ended up with asymmetric routing.

Below is the not working routing table:

pi@raspberrypi:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         speedport-entry 0.0.0.0         UG    202    0        0 eth0
10.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 tun0
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0

Below is the working routing table (all traffic goes through tun):

pi@raspberrypi:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.1.1.1        128.0.0.0       UG    0      0        0 tun0
default         speedport-entry 0.0.0.0         UG    202    0        0 eth0
10.1.1.0        0.0.0.0         255.255.255.0   U     0      0        0 tun0
static.89.67.45 speedport-entry 255.255.255.255 UGH   0      0        0 eth0
128.0.0.0       10.1.1.1        128.0.0.0       UG    0      0        0 tun0
192.168.0.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0

Question: How do I forward traffic coming to 192.168.0.2 from VNC server to tun ?

Thank you

已采纳解答

原帖链接 · Lefteris · CC BY-SA 4.0

Using tcpdump on RPi I managed to understand the problem better and solve it. I am posting here the solution for anyone who may face the same issue.

Solution To achieve what I wanted, I had to perform SNAT on pfSense for traffic going out of OpenVPN interface for the ports used for VNC.

Explanation I realized using tcpdump, that the source address of the packets going to VNC Server was the IP address of the Public Client (internet IP address). Therefore, when the RPi received the response from VNC server, it was trying to route the traffic to the Public Client's IP address (an internet IP address). The route to this public IP, was through the LAN Gateway (i.e. not through the tunnel).

By performing SNAT on pfSense and replacing the Public Client's IP address (in the source) to the pfSense VPN address, the RPi now knows where to send the responses to (to pfSense through the tunnel).

Hope it helps.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-03-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。