tc 匹配 IPv4 片段偏移

tc 匹配 IPv4 片段偏移

对于匹配具有 tc 中的偏移量 > 0 的片段,正确的语法是什么?

我努力了:

... u32 match u8 255 ff at 7 flowid 2:1

但无济于事

答案1

根据IPv4 报头 格式(稍后思考位时不要被编号所迷惑),您必须比较偏移量 6 和 7 处组合的两个字节的低 13 位。

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Version|  IHL  |Type of Service|          Total Length         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |         Identification        |Flags|      Fragment Offset    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Time to Live |    Protocol   |         Header Checksum       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                       Source Address                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Destination Address                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

因此无法匹配u8但需要例如u16。 还,tc u32似乎缺乏不是逻辑运算符,因此它只能可靠地匹配数据包没有一条规则中包含片段偏移(或没有偏移但有 MF 设置的数据包等)。如果您还想在检查中包含 MF 标志,则甚至还有缩写:

... u32 match ip nofrag flowid 2:1 # would match the opposite of what's wanted

无 MF 标志:

... protocol ip u32 match u16 0 0x1fff flowid 2:1

因此,您可以使用两个过滤器来反转逻辑,第一个过滤器绕过第二个过滤器(您可以补充行动和另外一个流量标识如果需要的话):

... prio 1 protocol ip u32 match u16 0 0x1fff action pass
... prio 2 protocol ip matchall flowid 2:1

或者你可以完全改变过滤模块并使用TC 基础版匹配包括逻辑运算符的功能不是,从而通过一个过滤器实现目标:

... protocol ip basic match 'not cmp(u16 at 6 layer network mask 0x1fff eq 0)' flowid 2:1

相关内容