我有一个 GNU/Linux 盒子,我已将其设置为家庭网络的路由器。我有三个网络接口...
- ppp0 是上行连接
- lan 是本地网络
- nordvpn 是 VPN 隧道
我正在使用 nftables 作为防火墙,并且正在运行缓存 DNS 服务器 (dnsmasq)。来自 LAN 的 DNS 请求被拦截并发送到路由器上的 DNS 服务器。到目前为止一切顺利,但是...
默认的NordVPN路由全部通过 VPN 隧道传输上行流量,如果这样配置,它就可以正常工作,但我不希望这样。我希望大多数到互联网的流量通过 ppp0 接口,而来自特定 LAN 主机的流量则通过 VPN 发送。使用 VPN 的主机的 DNS 请求应定向到 VPN 提供商的 DNS 服务器,而不是本地服务器。
我读过各种文章,讨论如何做类似(但不完全相同)的事情,到目前为止,我还没能让它发挥作用。我显然至少做错了一件事。据我所知,我应该能够在防火墙中标记数据包,然后使用自定义路由表通过 nordvpn 接口发送这些数据包。到目前为止,我的情况如下...
我已将以下行添加到/etc/iproute2/rt_tables
。
5 nordvpn
为了防止 openvpn 设置它自己的路由,我将这些行添加到/etc/openvpn/client/nordvpn.conf
。
route-noexec
script-security 2
route-up /etc/openvpn/client/mkroute.sh
我认为我需要禁用 nordvpn 接口上的反向路径过滤才能使其正常工作?这是 openvpn 用来设置路由的脚本。
#!/bin/bash
TABLE="nordvpn"
MARK="0x5"
die() {
echo "Error: $1"
exit 1
}
[ $(id -u) -eq 0 ] || die "Not running as root"
ip route flush cache || echo "error 1"
ip route flush table "$TABLE" || echo "error 2"
ip route add 192.168.2.0/24 dev lan src 192.168.2.1 table "$TABLE" || echo "error 3"
ip route add $route_vpn_gateway dev lan src $ifconfig_local table "$TABLE" || echo "error 4"
ip route add default via $route_vpn_gateway dev "$dev" table "$TABLE" || echo "error 5"
ip rule delete fwmark "$MARK" || :
ip rule add fwmark "$MARK" lookup "$TABLE" || echo "error 6"
sysctl -w net.ipv4.conf.nordvpn.rp_filter=0 || echo "error 7"
最后,这是我的 nftables 脚本。
flush ruleset
define i_wan = ppp0
define i_nordvpn = nordvpn
define i_lan = lan
define p_dns = 53
define a4_nordvpn_dns_1 = 103.86.96.100
define m_nordvpn = 5
define h4_nimrod = 192.168.2.21
table ip ip4_firewall {
set s_inet_nordvpn { type ipv4_addr; elements = { $h4_nimrod }; }
chain c_here_to_wan { tcp flags syn tcp option maxseg size set rt mtu; accept; }
chain c_here_to_nordvpn { tcp flags syn tcp option maxseg size set rt mtu; accept; }
chain c_here_to_lan { accept; }
chain c_wan_to_here { ip protocol icmp accept; drop; }
chain c_wan_to_lan { ip protocol icmp accept; drop; }
chain c_nordvpn_to_here { ip protocol icmp accept; drop; }
chain c_nordvpn_to_lan { ip protocol icmp accept; drop; }
chain c_lan_to_here { accept; }
chain c_lan_to_wan { tcp flags syn tcp option maxseg size set rt mtu; accept; }
chain c_lan_to_nordvpn { tcp flags syn tcp option maxseg size set rt mtu; accept; }
chain preroute_nordvpn {
udp dport $p_dns mark set $m_nordvpn dnat $a4_nordvpn_dns_1
tcp dport $p_dns mark set $m_nordvpn dnat $a4_nordvpn_dns_1
oif $i_wan mark set $m_nordvpn
}
chain prerouting { type nat hook prerouting priority -100
ip saddr @s_inet_nordvpn goto preroute_nordvpn
iif $i_lan oif $i_wan udp dport $p_dns redirect
iif $i_lan oif $i_wan tcp dport $p_dns redirect
}
chain postrouting { type nat hook postrouting priority 100
oif $i_nordvpn masquerade
oif $i_wan snat <my public IP address>
}
chain input { type filter hook input priority 0; policy drop
ct state established,related accept
ct state invalid drop
iif vmap {
lo: accept,
$i_wan : jump c_wan_to_here,
$i_nordvpn : jump c_nordvpn_to_here,
$i_lan : jump c_lan_to_here
}
log prefix "Unhandled input: " drop
}
chain output { type filter hook output priority 0; policy drop
ct state established,related accept
ct state invalid drop
oif vmap {
lo: accept,
$i_wan : jump c_here_to_wan,
$i_nordvpn : jump c_here_to_nordvpn,
$i_lan : jump c_here_to_lan
}
log prefix "Unhandled output: " drop
}
chain forward { type filter hook forward priority 0; policy drop
ct state established,related accept
ct state invalid drop
iif . oif vmap {
$i_lan . $i_wan : jump c_lan_to_wan,
$i_wan . $i_lan : jump c_wan_to_lan,
$i_lan . $i_nordvpn : jump c_lan_to_nordvpn,
$i_nordvpn . $i_lan : jump c_nordvpn_to_lan,
}
log prefix "Unhandled forward: " drop
}
}
配置完成后,我有以下内容。
[root@cerberus ~]# ip rule ls
0: from all lookup local
32765: from all fwmark 0x65 lookup nordvpn
32766: from all lookup main
32767: from all lookup default
[root@cerberus ~]# ip route ls
default dev ppp0 scope link
10.0.0.1 dev ppp0 proto kernel scope link src <my public IP address>
10.8.0.0/24 dev nordvpn proto kernel scope link src 10.8.0.12
192.168.2.0/24 dev lan proto kernel scope link src 192.168.2.1
[root@cerberus ~]# ip route ls table nordvpn
default via 10.8.0.1 dev nordvpn
10.8.0.1 dev lan scope link src 10.8.0.12
192.168.2.0/24 dev lan scope link src 192.168.2.1
[root@cerberus ~]# ip rule ls
0: from all lookup local
32764: from all fwmark 0x5 lookup nordvpn
32766: from all lookup main
32767: from all lookup default
启用后,分配给 VPN 的客户端的 DNS 查找会失败。请求似乎是通过 VPN 隧道发送的,但回复到达的是输入链而不是前向链,因此无法到达客户端。我哪里做错了?
提前感谢你的帮助:)
答案1
尝试改变:
chain preroute_nordvpn {
udp dport $p_dns mark set $m_nordvpn dnat $a4_nordvpn_dns_1
tcp dport $p_dns mark set $m_nordvpn dnat $a4_nordvpn_dns_1
oif $i_wan mark set $m_nordvpn
}
chain prerouting { type nat hook prerouting priority -100
ip saddr @s_inet_nordvpn goto preroute_nordvpn
iif $i_lan oif $i_wan udp dport $p_dns redirect
iif $i_lan oif $i_wan tcp dport $p_dns redirect
}
到:
chain fwmark { type filter hook prerouting priority -150
ip saddr @s_inet_nordvpn mark set $m_nordvpn
}
chain lan_dns {
ip saddr @s_inet_nordvpn dnat $a4_nordvpn_dns_1
redirect
}
chain lan_dnat {
udp dport $p_dns goto lan_dns
tcp dport $p_dns goto lan_dns
}
chain prerouting { type nat hook prerouting priority -100
iif $i_lan goto lan_dnat
}