我可以将所有四个 iptables 行合并为一个语句吗?

我可以将所有四个 iptables 行合并为一个语句吗?

我确实在寻找RETURNiptable 目标的含义。

RETURN means stop traversing this chain  and  resume  at
       the  next rule in the previous (calling) chain.  If the end of a built-
       in chain is reached or a rule in a built-in chain with target RETURN is
       matched,  the  target specified by the chain policy determines the fate
       of the packet.

我觉得很难理解,对于以下四个iptables命令

iptables -N syn-flood
iptables -A syn-flood -m limit --limit 100/s --limit-burst 150 -j RETURN
iptables -A syn-flood -j DROP
iptables -I INPUT -j syn-flood 

我可以将它们全部合并为一个语句吗?

iptables -A INPUT -m limit --limit 100/s --limit-burst 150 -j DROP 

如果不是,请对上述四个 iptables 命令及其RETURN内容做出清晰的解释。阅读更多材料和 Tomek 的帖子后,我知道第二种情况是第一种情况的逻辑否定。

下面两行等于四行吗?

iptables -A INPUT -m limit --limit 100/s --limit-burst 150 -j ACCEPT
iptables -A INPUT -j DROP

它们有同样的效果吗?

答案1

让我们看一下手册页来了解limit模块如何工作:

   limit
       This module matches at a limited rate using a token bucket  filter.   A
       rule  using  this extension will match until this limit is reached.

在第一种情况下,数据包进入INPUT链,然后进入syn-flood链(在某种程度上,这类似于函数调用)。 的第一行syn-flood被评估,所有匹配的数据包(即达到限制之前的所有数据包)都命中目标RETURN。手册页对此进行了说明:

RETURN means stop traversing this chain  and  resume  at the  next rule
in the previous (calling) chain.

如果您理解我的评论,认为它类似于函数调用,这基本上意味着数据包基本上离开了链syn-flood,并在调用者链的下一个语句处恢复评估。当INPUT链在这里结束时,您基本上会达到INPUT链的策略,默认情况下为ACCEPT

在第二种情况下,所有符合规则的数据包都会命中目标DROP

从某种程度上来说,第二种情况是第一种情况的逻辑否定。

答案2

iptables 非常巧妙,但有一个缺点。插入或删除条目需要从内核拉出整个 iptables,修改它,然后再推回。

每次调用一个普通iptables命令都会导致两个内核空间到用户空间的操作。iptables-restore允许您将拉取和推送的次数保持在正好两个的总和,而不管您在其中执行了多少次操作。

现在,在表-N内创建一个新的链filter(默认)。

第一个-A命令将新规则(在末尾)附加到此链,并限制指定的已处理数据包数量,如果未达到限制,RETURN则结束此链的遍历。这意味着第二个命令-A不会受到打击。

然而,如果已经达到限制,则遍历继续,并且下一个-A被命中并DROP导致数据包被丢弃。

最后一条-I插入一条无条件跳转至此新建的链syn-flood。若未指定数字-I,则规则将插入到顶部。

希望这能涵盖它。

编辑:如何使用的示例iptables-restore

创建一个包含以下内容的文件,并根据您的喜好更改内置链的默认策略:

*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-N syn-flood
-A syn-flood -m limit --limit 100/s --limit-burst 150 -j RETURN
-A syn-flood -j DROP
-I INPUT -j syn-flood 
COMMIT

然后运行iptables-restore <filename>

相关内容