大网数据广告活动
服务器运维问答

Why does iptables keep looking for the next rule when an extension target is used?

· 52CE 我爱测

The man page of iptables says that if a rule does match, then the next rule is specified by the value of the target. So if I have two rules like this: iptables -t mangle -I FORWARD -s 192.168.1.10 -j DROP iptables -t mangle -I FO…

问题原文

原帖链接 · hgl · CC BY-SA 4.0

The man page of iptables says that if a rule does match, then the next rule is specified by the value of the target.

So if I have two rules like this:

iptables -t mangle -I FORWARD -s 192.168.1.10 -j DROP
iptables -t mangle -I FORWARD -s 192.168.1.10 -j MARK --set-mark

I expect that since MARK rule is matched first and specifies the final target, the DROP rule is never consulted. But in realty, it is and the packets are dropped.

However, if the rules are

iptables -t mangle -I FORWARD -s 192.168.1.10 -j DROP
iptables -t mangle -I FORWARD -s 192.168.1.10 -j ACCEPT

Then the DROP rule is never consulted because the ACCEPT rule short-circuited it.

Is it the case that only the special targets like ACCEPT, DROP prevent iptables from keep looking for the next rule? But I don't see it mentioned anywhere in the man page, did I miss something?

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 4.0

There are two kinds of targets in IPTables chains: Terminating and Non-terminating targets.

ACCEPT, REJECT, DROP are terminating targets. They terminate processing of the packet in the current Netfilter hook.

MARK, LOG and many others are non-terminating targets. They create some action based on the packet, and then proceed to the next rule.

In your example, -j MARK is the first rule in the chain. It marks the packet, and then moves to next rule, which drops the packet.

If you want to mark and accept the packet, then you need something like:

iptables -t mangle -A FORWARD -s 192.168.1.10 -j MARK --set-mark 0x1
iptables -t mangle -A FORWARD -s 192.168.1.10 -m mark ! --mark 0x1 -j DROP

Also, it is not a recommended practice to DROP packets in the mangle table.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-09-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。