李守中

nftables 笔记

Table of Contents

1 在包过滤路径上动态地更新集合

自 nftables v0.7 起,用户可以在包过滤路径上 ( 就是使用规则 ) 更新集合。比如,基于防火墙接收到的数据包更新集合内容。

这个特性经常和元素的超时时间 ( element timeout ) 一起使用,主要使用方式是创建动态更新的黑名单。

命令格式为 nft add rule <family> <table_name> <chain_name> set { update | add } <expression> @<set_name> 。如果使用 update 则每当有新数据包流过,集合元素的超时时间都会被刷新;使用 add 的话,集合元素的超时时间不会被刷新。注意,使用 add 时,虽然集合元素超时时间不会被刷新,但是,在元素过期并被删除后,如果有新的数据包流过,则会再次添加相应元素并设置超时时间。简单来说就是,当有连续数据包发来时, update 会不断重置超时时间; add 不重置超时时间,而是会在元素超时被删除后重新添加元素,以此达到重新计时的目的。

给一个使用 update 的例子:

$ nft add table filter
$ nft add chain filter input { type filter hook input priority 0\; }
$ nft add set filter myset { type inet_service\; flags timeout\; }
$ nft add rule filter input set update tcp dport timeout 60s @myset
$ nft list ruleset
table ip filter {
       set myset {
               type inet_service
               flags timeout
               elements = { http expires 9s}
       }
       chain input {
               type filter hook input priority 0; policy accept;
               update @myset { tcp dport timeout 1m }
       }
}

再给一个使用 add 的例子:

$ nft add table filter
$ nft add chain filter input { type filter hook input priority 0\; }
$ nft add set filter myset { type ipv4_addr\; }
$ nft add rule filter input set add ip saddr @myset
$ nft list ruleset
table ip filter {
       set myset {
               type ipv4_addr
               elements = { 1.1.1.1 }
       }
       chain input {
               type filter hook input priority 0; policy accept;
               add @myset { ip saddr }
       }
}


Last Update: 2022-06-20 Mon 19:45

Contact: [email protected]     Generated by: Emacs 27.1 (Org mode 9.3)

若正文中无特殊说明,本站内容遵循: 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议