【问题标题】:Changing the incoming interface of the packet using iptables MARK and ip route commands使用 iptables MARK 和 ip route 命令更改数据包的传入接口
【发布时间】:2013-04-25 02:18:42
【问题描述】:

我尝试使用 iptables MARK 和 ip route 命令更改数据包的传入接口。

我已经尝试过this approach,它建议使用 iptable Mark 和 iproute2 实用程序而不是 ROUTE 目标,但在接收路径中更改数据包的传入接口时找不到任何成功。

我有一个应用程序“A”,它绑定到 linux 中的接口 intfA,即应用程序仅通过 intfA 发送和接收数据包。

发送路径

在基于隧道的设置中,我能够使用以下iptable 命令将数据包从intfA 发送到tap0

iptables -t mangle -A OUTPUT -d 8.9.10.11/32 -j MARK --set-mark 1
ip rule add fwmark 1 priority 9000 table  1

我在主表中的默认路由条目指向intfA

在表 '1' 中,我已将默认路由表入口点设为 tap0

route add default dev tap0 table 1

我可以从内核 dmesg 日志中观察到数据包的传出接口成功地从 intfA 更改为 tap0

在返回路径中

tap0 从隧道中获取加密数据包后,将其解密并通过tap0 接口将目标为“a.b.c.d”的纯文本数据包注入 linux 内核。

所以在内核预路由钩子中,我看到带有 IP = 'a.b.c.d' 和传入 tap0 的数据包。

以下 iptable 规则用于更改传入接口:

iptables -t mangle -A PREROUTING -d a.b.c.d/32 -j MARK --set-mark 2
ip rule add fwmark 2 priority 8000 table  2

在表'2'中,我将默认路由表入口点设为intfA

route add default dev intfA table 2

我尝试使用以下 iptable 命令在 PREROUTING 钩子和 INPUT 钩子上记录数据包:

iptables -t mangle -A PREROUTING -d a.b.c.d/32 -j LOG --log-level 0 --log-prefix "VPN PREOUTING: "
iptables -t mangle -A INPUT -d a.b.c.d/32 -j LOG --log-level 0 --log-prefix "VPN INPUT: "

我希望在 PREROUTING 钩子和 INPUT 钩子之间将数据包的传入接口从 tap0 更改为 intfA。但令人惊讶的是,内核日志跟踪中的接口都没有改变。我也可以看到标有标记 2 的传入数据包。

从 dmesg 日志中,我确认 Incoming 接口在 INPUT 钩子之后仍然保持为tap0

我想了解我使用的 iptables 命令的这种行为背后的原因。不确定我是否遗漏了导致此问题的 iptables 命令中的任何内容。

【问题讨论】:

    标签: interface routing packet iptables


    【解决方案1】:

    我也有同样的问题。作为一个临时解决方案,我发现 -j TEE 目标有帮助:

    iptables -t mangle -A PREROUTING [options] -j TEE --gateway IP

    在您的情况下,用您的值填写[options] 和网关的IP(intfA 地址)。

    【讨论】:

    • 我和原来的 OP 有同样的问题。我尝试了 -j TEE,但复制的数据包随后将传入接口设置为lo,而不是intfA 地址的接口。
    猜你喜欢
    • 2012-07-26
    • 1970-01-01
    • 2023-03-08
    • 2014-12-13
    • 2022-06-24
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多