KF1 nftables firewall rules
  1. #!/usr/sbin/nft -f
  2. #
  3. # KF1 DDoS protection - nftables port of the RouterOS setup.
  4. #
  5. # Hook order in prerouting:
  6. # raw (-300) -> conntrack (-200) -> mangle (-150) -> dstnat (-100)
  7. # so kf1_raw drops before a conntrack entry exists, and `detect` sees
  8. # conntrack state but still the public (pre-DNAT) destination,
  9. # exactly like RouterOS raw / mangle prerouting.
  10.  
  11. define WAN_IFS = { "eth0" } # = interface-list WAN
  12. define KF1_PUB = 7707 # port clients connect to (pre-DNAT)
  13. define KF1_SRV = 192.168.88.10 # game server LAN address
  14. define KF1_PORT = 7707 # port on the server (post-DNAT)
  15.  
  16. # Idempotent reload: create-if-missing, then wipe and redefine.
  17. table ip kf1
  18. delete table ip kf1
  19.  
  20. table ip kf1 {
  21. set active { # filled from the packet path
  22. type ipv4_addr
  23. flags dynamic, timeout
  24. timeout 60m
  25. size 65536
  26. }
  27. set ddos { # auto safety switch, keyed by public port
  28. type inet_service
  29. flags dynamic, timeout
  30. timeout 10m
  31. size 16
  32. }
  33.  
  34. # = /ip firewall raw (before conntrack: drops create no entries)
  35. chain raw_pre {
  36. type filter hook prerouting priority raw; policy accept;
  37. iifname $WAN_IFS udp dport $KF1_PUB jump kf1_raw
  38. }
  39. chain kf1_raw {
  40. ip saddr @active accept
  41. udp dport @ddos counter drop comment "KF1 auto safety switch"
  42. }
  43.  
  44. # = mangle prerouting "KF1 DDoS auto-detect"
  45. chain detect {
  46. type filter hook prerouting priority mangle; policy accept;
  47. iifname $WAN_IFS udp dport $KF1_PUB ct state new ct count over 100 counter update @ddos { udp dport } comment "KF1 DDoS auto-detect"
  48. }
  49.  
  50. # = /ip firewall nat dstnat (drop this if your DNAT lives elsewhere)
  51. chain dstnat {
  52. type nat hook prerouting priority dstnat; policy accept;
  53. iifname $WAN_IFS udp dport $KF1_PUB dnat to $KF1_SRV:$KF1_PORT
  54. }
  55.  
  56. # = /ip firewall filter forward + kf1-new
  57. chain forward {
  58. type filter hook forward priority filter; policy accept;
  59. iifname $WAN_IFS ct status dnat ct state { established, new } udp dport $KF1_PORT jump kf1_new comment "Jump to kf1-new"
  60. }
  61. chain kf1_new {
  62. ct state established update @active { ip saddr } accept comment "Track + accept established KF1"
  63. ct count over 100 counter drop comment "Global connection limit to KF server"
  64. }
  65. }
  66.  
Pasted 2026-10-03 15:58:33

Short link: