第 15 章 在 nftables 命令中使用判决映射
判决映射(也称为字典),使 nft
能够通过将匹配条件映射到某个操作来根据数据包信息执行操作。
15.1. 在 nftables 中使用匿名映射 复制链接链接已复制到粘贴板!
匿名映射是直接在规则中使用的 { match_criteria : action }
语句。这个语句可以包含多个用逗号分开的映射。
匿名映射的缺点是,如果要修改映射,则必须替换规则。对于动态解决方案,请使用命名映射,如 在 nftables 中使用命名映射 中所述。
例如,您可以使用匿名映射将 IPv4 和 IPv6 协议的 TCP 和 UDP 数据包路由到不同的链,以分别计算传入的 TCP 和 UDP 数据包。
流程
创建新表:
nft add table inet example_table
# nft add table inet example_table
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 在
example_table
中创建tcp_packets
链:nft add chain inet example_table tcp_packets
# nft add chain inet example_table tcp_packets
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 向统计此链中流量的
tcp_packets
中添加一条规则:nft add rule inet example_table tcp_packets counter
# nft add rule inet example_table tcp_packets counter
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 在
example_table
中创建udp_packets
链nft add chain inet example_table udp_packets
# nft add chain inet example_table udp_packets
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 向统计此链中流量的
udp_packets
中添加一条规则:nft add rule inet example_table udp_packets counter
# nft add rule inet example_table udp_packets counter
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 为传入的流量创建一个链。例如,要在过滤传入的流量的
example_table
中创建一个名为incoming_traffic
的链:nft add chain inet example_table incoming_traffic { type filter hook input priority 0 \; }
# nft add chain inet example_table incoming_traffic { type filter hook input priority 0 \; }
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 添加一条带有到
incoming_traffic
匿名映射的规则 :nft add rule inet example_table incoming_traffic ip protocol vmap { tcp : jump tcp_packets, udp : jump udp_packets }
# nft add rule inet example_table incoming_traffic ip protocol vmap { tcp : jump tcp_packets, udp : jump udp_packets }
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 匿名映射区分数据包,并根据它们的协议将它们发送到不同的计数链。
要列出流量计数器,请显示
example_table
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow tcp_packets
和udp_packets
链中的计数器显示两者接收的数据包和字节数。