netfilter:如何通过 reqid 标记数据包?

netfilter:如何通过 reqid 标记数据包?

我遇到了以下问题。我有使用防火墙标记的 SA。因此,只有带有该标记的数据包才会被编码和解码。我设法为需要编码的数据包设置了标记,但我无法让另一端正常工作。

我有需要解密的传入数据包,我需要为这些数据包设置正确的标记。我实际上可以使用以下命令设置标记:

iptables -t mangle -A PREROUTING --proto esp -j MARK --set-mark 1

但该规则匹配所有传入的 esp 数据包。但我将有多个 SA,并且需要设置不同的标记。我尝试使用 select by reqid 或 by spi,但一旦我尝试这样做,规则就不再匹配任何内容。

有人能帮助我正确理解 iptables 命令吗?

谨致问候,
斯特芬

root@vpn-b:~# setkey -D
10.5.0.2 10.5.0.1
        esp mode=tunnel spi=3296784692(0xc480f134) reqid=1(0x00000001)
        E: aes-cbc  c5eb72ab 906d5717 67e405f5 cfe73f7a
        A: hmac-sha1  6935290e e51f0965 06577876 0d6237d6 45a0083d
        seq=0x00000000 replay=32 flags=0x00000000 state=mature
        created: May 15 22:23:06 2012   current: May 15 22:24:43 2012
        diff: 97(s)     hard: 1200(s)   soft: 907(s)
        last: May 15 22:23:19 2012      hard: 0(s)      soft: 0(s)
        current: 7140(bytes)    hard: 0(bytes)  soft: 0(bytes)
        allocated: 85   hard: 0 soft: 0
        sadb_seq=1 pid=8282 refcnt=0
10.5.0.1 10.5.0.2
        esp mode=tunnel spi=3470192236(0xced6ee6c) reqid=1(0x00000001)
        E: aes-cbc  e6fad1a5 ff31325b b4856748 c8997ea1
        A: hmac-sha1  e401cc9d 59668c9f 866d7e86 b5a38d2c 1dcb2f2d
        seq=0x00000000 replay=32 flags=0x00000000 state=mature
        created: May 15 22:23:06 2012   current: May 15 22:24:43 2012
        diff: 97(s)     hard: 1200(s)   soft: 888(s)
        last: May 15 22:23:19 2012      hard: 0(s)      soft: 0(s)
        current: 7140(bytes)    hard: 0(bytes)  soft: 0(bytes)
        allocated: 85   hard: 0 soft: 0
        sadb_seq=0 pid=8282 refcnt=0

root@vpn-b:~# ip -s xfrm policy
src 10.1.1.0/24 dst 10.2.1.0/24 uid 0
        dir fwd action allow index 1218 priority 1859 share any flag  (0x00000000)
        lifetime config:
          limit: soft (INF)(bytes), hard (INF)(bytes)
          limit: soft (INF)(packets), hard (INF)(packets)
          expire add: soft 0(sec), hard 0(sec)
          expire use: soft 0(sec), hard 0(sec)
        lifetime current:
          0(bytes), 0(packets)
          add 2012-05-15 22:08:11 use 2012-05-15 22:18:27
        mark 1/0xffffffff
        tmpl src 10.5.0.1 dst 10.5.0.2
                proto esp spi 0x00000000(0) reqid 1(0x00000001) mode tunnel
                level required share any
                enc-mask ffffffff auth-mask ffffffff comp-mask ffffffff
src 10.1.1.0/24 dst 10.2.1.0/24 uid 0
        dir in action allow index 1208 priority 1859 share any flag  (0x00000000)
        lifetime config:
          limit: soft (INF)(bytes), hard (INF)(bytes)
          limit: soft (INF)(packets), hard (INF)(packets)
          expire add: soft 0(sec), hard 0(sec)
          expire use: soft 0(sec), hard 0(sec)
        lifetime current:
          0(bytes), 0(packets)
          add 2012-05-15 22:08:11 use -
        mark 1/0xffffffff
        tmpl src 10.5.0.1 dst 10.5.0.2
                proto esp spi 0x00000000(0) reqid 1(0x00000001) mode tunnel
                level required share any
                enc-mask ffffffff auth-mask ffffffff comp-mask ffffffff
src 10.2.1.0/24 dst 10.1.1.0/24 uid 0
        dir out action allow index 1201 priority 1859 share any flag  (0x00000000)
        lifetime config:
          limit: soft (INF)(bytes), hard (INF)(bytes)
          limit: soft (INF)(packets), hard (INF)(packets)
          expire add: soft 0(sec), hard 0(sec)
          expire use: soft 0(sec), hard 0(sec)
        lifetime current:
          0(bytes), 0(packets)
          add 2012-05-15 22:08:11 use 2012-05-15 22:18:27
        mark 1/0xffffffff
        tmpl src 10.5.0.2 dst 10.5.0.1
                proto esp spi 0x00000000(0) reqid 1(0x00000001) mode tunnel
                level required share any
                enc-mask ffffffff auth-mask ffffffff comp-mask ffffffff

答案1

我的搜索过程让我找到了这个页面,并决定写下来。由于 ESP 数据包在对等点之间流动,您可以向 iptables 添加其他参数,例如源和目标 IP 地址。这将区分 esp 数据包,您可以在之后应用不同的标记。例如,从对等点 100.100.100.100 到对等点 200.200.200.200(假设 200.200.200.200 是您的一方),您将在 /etc/iptables.up.rules 中有这样的规则

*mangle

# Marking the ESP packets between peer A and peer B
-A INPUT -p esp -s 100.100.100.100 -d 200.200.200.200 -j MARK  --set-mark 10

属于另一个隧道的 ESP 数据包将被标记为这样(再次假设 200.200.200.200 是您所在的一端,而 150.100.100.100 是远程对等体)

# Marking the ESP packets between peer C and peer B
-A INPUT -p esp -s 150.100.100.100 -d 200.200.200.200 -j MARK  --set-mark 20

答案2

尝试使用 iptables 中的策略匹配模块:

-m policy --reqid $ID

更多详情手册 iptables(8)

相关内容