除了一些 IP 的规则,当 vpn 断开连接时,不允许通过 openwrt + openvpn 上网

除了一些 IP 的规则,当 vpn 断开连接时,不允许通过 openwrt + openvpn 上网

我的网络上有以下工作设置:

A。ISP 路由器(连接到 inet,内部 ip 192.168.0.1 )<---->B.eth0 - OpenWrt 路由器(OpenVPN 客户端运行)br-lan(桥接 eth1 + wlan0,ip 192.168.1.0)<---->C。多个客户端

这个想法是,所有客户端连接都通过 B。B 通过 VPN 路由所有内容。如果 VPN 连接中断,客户端将无法再访问互联网。因此,如果 VPN 连接出现问题,我可以防止客户端暴露自己。

我的 OpenWrt 路由器设置取自这里:https://blog.ipredator.se/howto/openwrt/configuring-openvpn-on-openwrt.html

总结一下:

1.相关设备:

root@OpenWrt:~# ifconfig
br-lan    Link encap:Ethernet  
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
eth0      Link encap:Ethernet  
          inet addr:192.168.0.16  Bcast:192.168.0.255  Mask:255.255.255.0
tun0      Link encap:UNSPEC  
          inet addr:10.33.197.41  P-t-P:10.33.197.41  Mask:255.255.0.0

2 个相关防火墙区域:

config zone
option name 'lan'
option network 'lan'
option input 'ACCEPT'
option output 'ACCEPT'
option forward 'REJECT'

config zone
option name 'wan'
option output 'ACCEPT'
option forward 'REJECT'
option network 'wan'
option input 'ACCEPT'

config zone
option name 'vpn'
option input 'REJECT'
option output 'ACCEPT'
option forward 'REJECT'
option masq '1'
option mtu_fix '1'
option network 'vpn'

config forwarding
option dest 'vpn'
option src 'lan'

3.路由表如下:

root@OpenWrt:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.33.0.1       128.0.0.0       UG    0      0        0 tun0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
10.33.0.0       0.0.0.0         255.255.0.0     U     0      0        0 tun0
46.122.122.89   192.168.0.1     255.255.255.255 UGH   0      0        0 eth0
128.0.0.0       10.33.0.1       128.0.0.0       UG    0      0        0 tun0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.1     0.0.0.0         255.255.255.255 UH    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-lan

到目前为止,此设置的所有功能都运行良好。但是,现在我想绕过特定 IP/主机的 VPN。因此,即使 VPN 断开连接,它们也可以使用。我的想法是为这些 IP 添加一条直接通过 A 的路由 (ip rule add ...)。但这行不通,因为我似乎还必须调整防火墙设置。不幸的是,阅读有关 iptables 基础知识的文章还不能让我明白要进行哪些更改。

/编辑:经过尝试和进一步研究,我认为理论上有两种解决方案。但是,我不知道如何让它们发挥作用:

  1. 为了牢记上述概念,我需要:

    • 为我的 wan_zone 添加 MASQUERADE(我就是这么做的)
    • 添加从 LAN 到 WAN 以及从 WAN 到 LAN 的转发规则(我可以这样做,但这样我会失去“vpn 故障保护”),这些规则取决于我想要访问的 IP(我不知道是否有效以及如何有效)
  2. 改变概念,摆脱 iptables,转而使用 iproute2 和基于策略的路由(http://www.linupedia.org/opensuse/Policy_Based_Routing

    • 默认情况下,所有内容仅通过 vpn 路由
    • 有条件地直接通过 A 路由特定 IP

但是,这似乎更加复杂,至少对我来说是这样,因为我从来没有用过它。

答案1

/编辑:好的,我找到了一个非常适合我需要的解决方案。

我已设法改进下面的第一个工作思路。特别是,现在我可以在外部列表中管理要直接路由/转发到 A 的 IP 列表。我已将以下内容添加到 /etc/firewall.user

# If connection was established before, accept it (so we dont have to deal with inbound connections)
iptables -A forwarding_rule -m state --state ESTABLISHED,RELATED -j ACCEPT
# Read ips from file
FORWARDIPS=$(egrep -v -E "^#|^$" /etc/forward_ip)

# create new iptables and route table entries for each ip in the file
# allow forward if packet matches destination and route it through "table admin", for which we set a default gateway below 
for ipblock in $FORWARDIPS
do
   iptables -A forwarding_rule -d $ipblock -j ACCEPT -p all
   ip rule add to $ipblock table admin
done
~

root@OpenWrt:~# cat /etc/forward_ip 
54.164.36.0/24
8.31.8.0/22     

我需要在启动时从另一个文件调用以下命令(我将其放入/etc/rc.local):

# Route everything in table admin by default through ISP-Router
ip route add default via 192.168.0.1 dev eth0 table admin  

还要编辑 /etc/iproute2/rt_tables 并添加以下行:

10     admin

/编辑,这是我的第一个解决方案:

我找到了一个解决方案。但是,它还可以大大改进。这个想法遵循我在 /edit 1 中的直觉。我为我的 wan 区域启用伪装,并为特定目标 ip 启用我的 lan 和 wan 区域之间的条件转发。但是,我无法为配置规则添加多个“option dest_ip”。因此,最好将我在 uci 防火墙配置中所做的操作作为 iptables 规则,然后将其添加到 /etc/firewall.user

我编辑了 /etc/config/firewall 并更改了配置区域并添加了配置规则

config zone
        option input 'ACCEPT'
        option output 'ACCEPT'
        option name 'wan'
        option network 'wan'
        option forward 'REJECT'
        option masq '1'
        option mtu_fix '1'

config rule
        option target 'ACCEPT'
        option src 'lan'
        option dest 'wan'
        option name 'Lan to Wan'
        option proto 'all'
        option dest_ip '54.164.36.190'
        option enabled '0'

经过多次尝试后,我放弃了 /edit 2 中的想法,因为存在许多无法预见的障碍。例如,如果没有从 B 到 A 的默认网关,我根本无法建立 vpn 连接。尽管如此,我认为如果您比我更了解路由,就可以做到这一点。

相关内容