@@ -234,7 +234,59 @@ conntrack -L -p udp --dport 53
234234
235235### IPset {#ipset}
236236
237- IPset 是
237+ IPset 是 Netfilter 的一个扩展模块,用于管理 IP 地址集合(set),可以高效地匹配大量的 IP 地址或网络段。
238+
239+ IPset 的用户态工具为 [ ` ipset(8) ` ] [ ipset.8 ] ,可以创建、删除和管理 IPset 集合。
240+ IPset 支持多种集合类型,如 ` hash:ip ` (单个 IP 地址的哈希集合)、` hash:net ` (CIDR 网段的哈希集合)和 ` bitmap:ip ` (IP 地址的 bitmap 集合)等。
241+ 完整的集合类型和用法可以参考 ` ipset help ` 的输出,以下给出一些基本的示例:
242+
243+ ``` shell title="创建集合、添加和删除地址"
244+ ipset create blacklist hash:ip
245+ ipset add blacklist 192.0.2.1
246+ ipset del blacklist 192.0.2.1
247+
248+ # 即使地址已存在(add 操作)或不存在(del 操作),也不会报错
249+ ipset add -exist blacklist 192.0.2.1
250+ ipset del -exist blacklist 192.0.2.1
251+ ```
252+
253+ IPset 集合可以在 iptables 或 nftables 规则中使用,实现高效的地址匹配。例如:
254+
255+ ``` shell
256+ iptables -A INPUT -m set --match-set blacklist src -j DROP
257+ ```
258+
259+ 其中 ` --match-set blacklist src ` 表示从名为 ` blacklist ` 的 IPset 集合中匹配数据包的源地址。
260+ 相比于在 iptables 规则中逐条列出黑名单地址,使用 IPset 可以显著提高匹配效率,尤其是在黑名单地址数量较多的情况下。
261+
262+ 如果集合是多元素类型的话,还可以指定端口号等其他字段进行匹配,例如:
263+
264+ ``` shell
265+ ipset create blacklist hash:ip,port
266+ iptables -A INPUT -m set --match-set blacklist src,dst -j DROP
267+ ```
268+
269+ 此时 ` blacklist ` 集合中的每个元素都包含一个 IP 地址和一个端口号,命令中的 ` src,dst ` 表示匹配数据包的** 源** 地址和** 目的** 端口号。
270+ 完整的匹配能力请在 [ iptables-extensions(8)] [ iptables-extensions.8 ] 手册页中查阅 ` set ` 模块的说明。
271+
272+ 类似地,IPset 中的元素也可以通过 iptables 增减:
273+
274+ ``` shell
275+ iptables -m my-fantastic-match -j SET --add-set blacklist src [--exist]
276+ iptables -m my-fantastic-match -j SET --del-set blacklist src [--exist]
277+ ```
278+
279+ #### skbinfo {#ipset-skbinfo}
280+
281+ 部分集合类型支持一项 skbinfo 扩展,可以为每个条目存储额外的信息(skbmark、skbprio、skbqueue),并在匹配时将这些信息附加到数据包上(` --map-mark ` 、` --map-prio ` 、` --map-queue ` )。
282+
283+ 例如:
284+
285+ ``` shell
286+ ipset create qos hash:ip skbinfo
287+ ipset add qos 192.0.2.1 mark 0x1 prio 5 queue 2
288+ iptables -t mangle -A PREROUTING -m set --match-set qos src --map-mark --map-prio --map-queue -j ACCEPT
289+ ```
238290
239291## iptables {#iptables}
240292
0 commit comments