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

Is it possible to route all communication to one port, to another network interface?

· 52CE 我爱测

Short: I wonder if it's possible to route (with iptables), so all communication that are directed to/from 192.168.1.10 (eth0) port 40000 are sent to tun0? (VPN) Longer: When I'm connecting to my VPN server, all data is routed thr…

问题原文

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

Short: I wonder if it's possible to route (with iptables), so all communication that are directed to/from 192.168.1.10 (eth0) port 40000 are sent to tun0? (VPN)

Longer: When I'm connecting to my VPN server, all data is routed thru the tun0 network, except this app (that's not going anywhere) that totally refuses to understand (bad programming). This causes the app to be headbutting the system without getting out..

Is it possible to force it by routing all data that's going to 192.168.1.10:40000 to be sent to tun0 (VPN network interface), and route all incoming data from tun0:40000 to be routed back to 192.168.1.10

Update: This is the result of IP -4 route:

*0.0.0.0/1 via 10.128.0.1 dev tun0 
default via 192.168.1.1 dev enp0s3 proto dhcp metric 20100 
10.128.0.0/22 dev tun0 proto kernel scope link src 10.128.2.129 
128.0.0.0/1 via 10.128.0.1 dev tun0 
169.254.0.0/16 dev tun0 scope link metric 1000 
192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.173 metric 100 
217.64.xxx.xx via 192.168.1.1 dev enp0s3* 

已采纳解答

原帖链接 · Piotr P. Karwasz · CC BY-SA 4.0

When I'm connecting to my VPN server, all data is routed thru the tun0 network, except this app, that totally refuses to understand (bad programming).

Network programming has a layered model, it means the author of the app just tells the system to connect to a certain IP address on a certain port, he does not have to bother about the rest, since how to reach that TCP/IP socket is up to the operating system.

You problem is caused by a bad routing configuration. Let's take a couple of lines like this:

1.2.3.0/29 dev tun0
1.2.0.0/22 dev tun0 via 1.2.3.4 

The first means: if the leftmost 29 bits of the destination address are identical to those of 1.2.3.0 (i.e. from 1.2.3.0 to 1.2.3.7) send the packet directly on tun0. The second line means that addresses from 1.2.0.0 to 1.2.3.255, which are not in the /29 line must be sent to 1.2.3.4. The kernel knows how to find 1.2.3.4 from the first line.

Windows routing tables are almost identical (they originally took the TCP/IP stack from BSD).

Now to your situation. You have two routing entries:

0.0.0.0/1 via 10.128.0.1 dev tun0 
default via 192.168.1.1 dev enp0s3

where default is a shortcut for 0.0.0.0/0 (all addresses). So: if the destination address starts with 0 up to 128, it is sent through tun0, otherwise it is sent through enp0s3.

If you want to route almost all traffic through tun0, first find the address of the other endpoint of the VPN tunnel (probably ip link can help, but you didn't say what are you using for your tunnel). Let's say it is 5.5.5.5. tun0 is a virtual interface, which encrypts the packets sent through it and sends them to the other endpoint through a physical interface. So we need to add a route so packets to 5.5.5.5 are sent through enp0s3:

ip route add 5.5.5.5 via 192.168.1.1 dev enp0s3

(Edit: after reading again your routing table, this is actually already there, in the last line)

Then you can route the remaining traffic through tun0:

ip route del default
ip route add default via 10.128.0.1 dev tun0

Depending on the way you setup your interfaces (and tunnel), you can do those things automatically, when the interfaces goes up.

TL;DR Just replace 0.0.0.0/1 with default, wherever you put it.

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

资料来源与适用范围

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

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

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