通过无线热点“路由回”不起作用

通过无线热点“路由回”不起作用

我有一个运行无线热点的 Linux 设备:

my laptop (LAN only: 192.168.1.100)
  |
 LAN
  |
wireless hotspot laptop (LAN: 192.168.1.101, wireless hotspot: 10.42.0.1, VMs: 10.254.2.x)
  |
wireless printer (wifi: 10.42.0.67)

在我的笔记本电脑上,我可以使用其 LAN IP (192.168.1.101) ping/连接到无线热点笔记本电脑。

在我的笔记本电脑上添加以下路由后,我还能够 ping/连接到其无线热点 IP (10.42.0.1):

ip route add to 10.42.0.0/24 via 192.168.1.101

如果添加以下路由规则,我还可以 ping/连接到笔记本电脑上的虚拟机:

ip route add to 10.254.2.0/24 via 192.168.1.101

但是,ping/连接到无线网络 (10.42.0.0/24) 内的任何设备 - 没有骰子,得到“目标端口无法访问”:

# ping 10.42.0.67
PING 10.42.0.67 (10.42.0.67) 56(84) bytes of data.
From 192.168.1.101 icmp_seq=1 Destination Port Unreachable
From 192.168.1.101 icmp_seq=2 Destination Port Unreachable
^C
--- 10.42.0.67 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1001ms

在无线热点笔记本电脑上添加以下 MASQUERADE 规则允许我的笔记本电脑与 8.8.8.8 进行通信:

iptables -t nat -A POSTROUTING -s 192.168.1.100 -j MASQUERADE

无线热点笔记本电脑上的 tcpdump 表明从我的笔记本电脑到 8.8.8.8 的流量正在通过它:

# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=44.4 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=46.9 ms
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 44.425/45.668/46.912/1.243 ms

尽管如此,连接到 10.42.0.67(无线热点后面的无线打印机)- 没有骰子(下面是无线热点上的 tcpdump):

01:25:20.901823 enp1s0 In  IP (tos 0x0, ttl 64, id 37541, offset 0, flags [DF], proto ICMP (1), length 84)
    192.168.1.100 > 10.42.0.67: ICMP echo request, id 216, seq 16, length 64
01:25:20.902038 enp1s0 Out IP (tos 0xc0, ttl 64, id 11555, offset 0, flags [none], proto ICMP (1), length 112)
    192.168.1.101 > 192.168.1.100: ICMP 10.42.0.67 protocol 1 port 12067 unreachable, length 92
        IP (tos 0x0, ttl 63, id 37541, offset 0, flags [DF], proto ICMP (1), length 84)
    192.168.1.100 > 10.42.0.67: ICMP echo request, id 216, seq 16, length 64

wifi 是否有什么特殊之处,Linux 不想将其路由或 NAT 到其中?

当前的 nftables 规则如下;lxd部分与在那里运行的容器有关;nm-shared-wlp2s0与无线热点相关;没有 iptables 规则(上面例子中没有我的 MASQUERADE):

table inet lxd {
        chain pstrt.lxdbr0 {
                type nat hook postrouting priority srcnat; policy accept;
                ip saddr 10.254.2.0/24 ip daddr != 10.254.2.0/24 masquerade
        }

        chain fwd.lxdbr0 {
                type filter hook forward priority filter; policy accept;
                ip version 4 oifname "lxdbr0" accept
                ip version 4 iifname "lxdbr0" accept
        }

        chain in.lxdbr0 {
                type filter hook input priority filter; policy accept;
                iifname "lxdbr0" tcp dport 53 accept
                iifname "lxdbr0" udp dport 53 accept
                iifname "lxdbr0" icmp type { destination-unreachable, time-exceeded, parameter-problem } accept
                iifname "lxdbr0" udp dport 67 accept
        }

        chain out.lxdbr0 {
                type filter hook output priority filter; policy accept;
                oifname "lxdbr0" tcp sport 53 accept
                oifname "lxdbr0" udp sport 53 accept
                oifname "lxdbr0" icmp type { destination-unreachable, time-exceeded, parameter-problem } accept
                oifname "lxdbr0" udp sport 67 accept
        }
}
table ip nm-shared-wlp2s0 {
        chain nat_postrouting {
                type nat hook postrouting priority srcnat; policy accept;
                ip saddr 10.42.0.0/24 ip daddr != 10.42.0.0/24 masquerade
        }

        chain filter_forward {
                type filter hook forward priority filter; policy accept;
                ip daddr 10.42.0.0/24 oifname "wlp2s0" ct state { established, related } accept
                ip saddr 10.42.0.0/24 iifname "wlp2s0" accept
                iifname "wlp2s0" oifname "wlp2s0" accept
                iifname "wlp2s0" reject
                oifname "wlp2s0" reject
        }
}

答案1

从命名来看,您的系统有两个应用防火墙规则的工具:LXD(不会干扰:没有任何内容丢弃或拒绝)和另一个工具,您必须从它选择的命名中找出它:table nm-shared-wlp2s0

您必须弄清楚这可能是什么工具,并配置此工具以添加例外,以便从其他 LAN 访问您的打印机网络。

同时,在许多等效选项中,您可以手动执行此操作。例如,启用从其他 LAN 到此 LAN 的传入数据包:

nft insert rule nm-shared-wlp2s0 filter_forward ip saddr 192.168.1.0/24 ip daddr 10.42.0.0/24 oifname "wlp2s0" ct state new accept

每次该工具重新配置其防火墙规则时都必须执行此操作(希望它不会“主动”删除您的规则)。


此外,该表(以及创建它的工具)没有遵循在拒绝数据包之前丢弃无效数据包的最佳实践。拒绝(而不是丢弃)无效数据包可能会导致问题,尤其是重新排序的数据包。请参阅此引用后的解释iptables(但也影响nftables)详细信息:

警告:您不应不加区别地将 REJECT 目标应用于连接状态分类为 INVALID 的数据包;相反,你应该只删除这些。

(可选)在上一个命令之后,也应该添加以下命令:

nft insert rule nm-shared-wlp2s0 filter_forward index 2 ct state invalid drop

以防止数据包重新排序时可能发生的流量中断(Wifi 可能会造成这种情况)。

请注意,每个表中存在nftables提供了可能影响数据包的钩链的机会。即使一个表的链接受了一个数据包,它仍然可以被丢弃到另一个挂接到同一位置的链中:这里有两个链挂在 Netfilter 的过滤器/转发中:inet lxd fwd.lxdbr0ip nm-shared-wlp2s0 filter_forward:它们每个都有机会丢弃数据包。

相关内容