有我的测试nft规则集,除了表inet测试之外,所有工作都可以,但f2b表非常相似(除了drop和accept(,并且工作良好:
table inet f2b-table {
set addr-set-sshd {
type ipv4_addr
elements = { 0.0.0.0 }
}
chain input {
type filter hook input priority filter - 1; policy accept;
tcp dport { 222 } ip saddr @addr-set-sshd drop
}
}
table inet default {
set full_op_port {
type inet_service
elements = { 222 }
}
set allowed_ips {
type ipv4_addr
elements = { 0.0.0.0 }
}
chain INPUT {
type filter hook input priority filter; policy drop;
ct state invalid drop
ct state { established, related } accept
iif "lo" accept
tcp dport @full_op_port accept
ip saddr @allowed_ips accept
ip protocol icmp accept
counter packets 17 bytes 884
}
chain FORWARD {
type filter hook forward priority filter; policy drop;
}
chain OUTPUT {
type filter hook output priority filter; policy accept;
}
}
table ip test {
chain PREROUTING {
type nat hook prerouting priority filter; policy accept;
}
chain POSTROUTING {
type nat hook postrouting priority srcnat; policy accept;
}
chain FORWARD {
type filter hook forward priority filter; policy drop;
}
}
table inet test {
set op_port {
type inet_service
elements = { 8888 }
}
chain INPUT {
type filter hook input priority filter - 2; policy accept;
tcp dport @op_port accept
}
}
我在tcpdump中看到包,当我在表inettest中使计数时看到包,但包不被接受。我做错了什么?
我在这里添加另一个答案和示例,以澄清将策略与同一家族、类型和钩子的多个基链混合的意外后果。尽管可以在这些问题上给予同样的优先权,但永远不应该。较低的优先级意味着较高的优先级,并且将首先运行。不正确地应用丢弃策略可能会对您打算接受的流量造成意想不到的后果。
至于将混合家庭inet与ip和ip6混合的效果,我甚至不会开始高谈阔论,只是说这可能是个坏主意。
警告:这些例子严重破坏了ipv4流量,并且是在虚拟机上执行的-买家要小心!
一个糟糕的投放策略的例子:
table inet filter {
chain input1 {
type filter hook input priority filter + 1; policy drop;
tcp dport 80 log prefix "input1_" # SEEN
}
# input2 chain not evaluated as there is no traffic left after input1
chain input2 {
type filter hook input priority filter + 2; policy accept;
tcp dport 80 accept
tcp dport 80 log prefix "input2_"
}
}
ok drop策略的一个示例:
table inet filter {
chain input1 {
type filter hook input priority filter + 1; policy accept;
tcp dport 80 log prefix "input1_" # SEEN
}
chain input2 {
type filter hook input priority filter + 2; policy drop;
tcp dport 80 accept
tcp dport 80 log prefix "input2_" # NOT SEEN due previous accept
}
}
错误接受策略的一个例子:
table inet filter {
chain input1 {
type filter hook input priority filter + 1; policy accept;
tcp dport 80 accept
tcp dport 80 log prefix "input1_" # NOT SEEN due to previous accept
}
chain input2 {
type filter hook input priority filter + 2; policy drop;
tcp dport 80 log prefix "input2_" # SEEN - chain evaluates
# all traffic dropped here by policy including accepted input1 traffic
}
}
可以接受政策的一个例子:
table inet filter {
chain input1 {
type filter hook input priority filter + 1; policy accept;
tcp dport 80 log prefix "input1_" # SEEN
}
chain input2 {
type filter hook input priority filter + 2; policy drop;
tcp dport 80 accept
tcp dport 80 log prefix "input2_" # NOT SEEN due to previous accept
}
}
正如nft的手册页中所述,丢弃规则或策略会立即丢弃,而无需进一步处理优先级较低的基本链。accept不接受。它将当前优先级的剩余规则短路,并移交给下一个较低优先级的规则,但在这里,如果规则明确放弃了它,它仍然会被放弃;如果没有规则可接受,则策略会隐式放弃它。
也许最简单的方法是使用单个基本链和跳转/转到非基本链,这实际上是iptables的工作方式。
答案来自A.B,他说:
只是为了澄清一个数据包可以在同一个钩子中多次被接受(或不被接受(:
并从nft手册页发布
接受
终止规则集评估并接受数据包。数据包仍然可以丢弃稍后通过另一个钩子,例如在前钩子中接受仍然允许放下后路由挂钩中稍后的数据包,或具有更高优先级编号,然后在处理流水线中进行评估。
您的默认表基链优先级0将在测试表基链优先权-2之后进行评估,因为它有丢弃策略,并且数据包在那里不匹配,所以它将被丢弃。
手册页对此感到困惑。在它所说的允许判决中;终止规则集评估并接受分组";,它实际上只终止给定基本链优先级的规则集视图。由于优先级较高,具有较低优先级的其他相同类型、钩子和族的基链仍将在之后运行,并且可以被规则或策略覆盖。这与丢弃判决不同,在丢弃判决中,所有操作都被停止,数据包被立即丢弃。您可以使用日志记录看到这一点:
nft flush ruleset
nft create table ip table1
nft add chain ip table1 input1 { type filter hook input priority filter; policy drop; }
nft add rule ip table1 input1 tcp dport != 8888 accept
nft add rule ip table1 input1 tcp dport 8888 log prefix "TABLE1_INPUT1 DROPPING " level info
nft create table ip table2
nft add chain ip table2 input2 { type filter hook input priority filter - 1; policy accept; }
nft add rule ip table2 input2 tcp dport != 8888 accept
nft add rule ip table2 input2 tcp dport 8888 log prefix "TABLE2_INPUT2 BEFORE " level info
nft add rule ip table2 input2 tcp dport 8888 accept
nft add rule ip table2 input2 tcp dport 8888 log prefix "TABLE2_INPUT2 AFTER " level info