Last active
September 19, 2025 12:34
-
-
Save haxibami/8da1ad0e5558d97bf04cf5a64a879903 to your computer and use it in GitHub Desktop.
nftables-adf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/sbin/nft -f | |
| # キー: SNAT後の送信元ポート + 宛先グローバルIP | |
| # 値: SNAT前の (送信元プライベートIP + 送信元ポート) | |
| map known-entries { | |
| type inet_service . ipv4_addr : ipv4_addr . inet_service | |
| flags dynamic, timeout | |
| timeout 10m | |
| } | |
| # WAN側から来た未知の下りパケットの (宛先ポート + 送信元IP) をキーとして、 | |
| # 対応する値 (プライベートIP + ポート) があればそこにDNAT | |
| # (既知のパケットはどうやらこのチェインにヒットしない模様) | |
| chain prerouting-dnat { | |
| type nat hook prerouting priority dstnat; policy accept; | |
| iif "wan" dnat ip to udp dport . ip saddr map @known-entries | |
| } | |
| # DNATした下りパケットの転送を許可(省略) | |
| chain forward { | |
| type filter hook forward priority filter; policy drop; | |
| # ... | |
| } | |
| # 上りのSNAT(省略) | |
| chain postrouting-snat { | |
| type nat hook postrouting priority srcnat; policy accept; | |
| # ... | |
| } | |
| # SNAT後に上りパケットの情報を記録 | |
| chain postrouting-after-snat { | |
| type filter hook postrouting priority srcnat + 5; policy accept; | |
| oif "wan" update @known-entries { udp sport . ip daddr : ct original ip saddr . ct original proto-src } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment