大网数据广告活动
服务器运维问答

How to use nftables dnat and redirect without preceding match

· 52CE 我爱测

I have table ip nat { chain Redirect_to_local { limit rate 3/minute burst 10 packets log prefix "[nft.ip.dnat.8080]: " redirect to :8080 } chain PREROUTING { type nat hook prerouting priority dstnat; policy accept; iifname != { "…

问题原文

原帖链接 · avi9526 · CC BY-SA 4.0

I have

table ip nat {
    chain Redirect_to_local {
            limit rate 3/minute burst 10 packets log prefix "[nft.ip.dnat.8080]: "
            redirect to :8080
    }
    chain PREROUTING {
            type nat hook prerouting priority dstnat; policy accept;
            iifname != { "lo", "ISP*" } fib daddr type != { local, multicast, broadcast } udp dport == 8000 counter jump Redirect_to_local
    }
}

and it's incorrect because transport protocol mapping is only valid after transport protocol match. But why? It DID match, but not directly there.

If I add udp dport == 8000 before redirect it will be accepted as correct, despite duplication of rule (which is annoying).

Is there a way to overcome this? Or did I misunderstand this?

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

ip protocol udp (from packet's content) or meta l4proto udp (from measured packet's properties. Usually this is automatically implied when writing for example udp dport 8000) is the way to tell it's UDP without specifying again the port. Without this piece of information that is only single-rule-wide, the NAT statement will accept to alter only layer 3 (here: IPv4) and leave alone the layer 4: it would accept to change the address but not the port. As redirect is usually meant to change the port (But not required. A single rule line consisting only of redirect is valid), then stating the protocol usually has to be done.

So you can replace:

redirect to :8080

with either:

meta l4proto udp redirect to :8080

or:

ip protocol udp redirect to :8080

Note: for IPv6, using meta l4proto udp is the only correct choice, because ip6 nexthdr udp (which would be the equivalent to ip protocol udp) would fail in presence of IPv6 options, since the IPv6 fixed header doesn't provide the final transport header protocol, but only the next header, which could be an option header and thus differ.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-12-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。