RouterOS firewall is a powerful packet filtering and manipulation system that provides stateful packet inspection, NAT, traffic shaping, and advanced security features for network protection and traffic control.
In WinBox you can configure firewall in IP -> Firewall, or you can use terminal with command /ip firewall
The RouterOS firewall operates on multiple chains and provides comprehensive control over network traffic through filtering, NAT, and mangle rules.
Firewall fundamentals
How RouterOS firewall works
Core components:
Filter rules - Accept, drop, or reject packets based on criteria
NAT rules - Network Address Translation for routing and security
Mangle rules - Mark packets for QoS and advanced routing
Connection tracking - Stateful inspection of network connections
Address lists - Dynamic IP address groupings for rules
Processing order:
Raw - Before connection tracking (advanced users)
Mangle - Packet marking and modification
NAT - Address translation
Filter - Accept/drop decisions
Connection tracking - State management throughout
Basic firewall concepts
Chains and processing
RouterOS uses predefined chains for packet processing:
Filter chains:
input - Packets destined for the router itself
forward - Packets routed through the router
output - Packets originating from the router
NAT chains:
srcnat - Source NAT (typically for internet access)
# Connection states for stateful filtering
new # First packet of new connection
established # Packets of established connections
related # Packets related to established connections (FTP data, ICMP errors)
invalid # Packets that don't belong to any connection
untracked # Packets not tracked by connection tracking
# Allow established and related connections
/ip firewall filter add chain=input action=accept connection-state=established,related comment="Accept established/related"
# Allow loopback traffic
/ip firewall filter add chain=input action=accept in-interface=lo comment="Accept loopback"
# Allow ICMP (ping)
/ip firewall filter add chain=input action=accept protocol=icmp comment="Accept ICMP"
# Allow SSH from LAN
/ip firewall filter add chain=input action=accept protocol=tcp dst-port=22 src-address-list=LAN comment="Accept SSH from LAN"
# Allow WinBox from LAN
/ip firewall filter add chain=input action=accept protocol=tcp dst-port=8291 src-address-list=LAN comment="Accept WinBox from LAN"
# Allow web management from LAN
/ip firewall filter add chain=input action=accept protocol=tcp dst-port=80 src-address-list=LAN comment="Accept HTTP from LAN"
# Drop everything else to router
/ip firewall filter add chain=input action=drop comment="Drop all other input"
# Create LAN address list
/ip firewall address-list add list=LAN address=192.168.1.0/24 comment="Local network"
/ip firewall address-list add list=LAN address=10.0.0.0/8 comment="RFC1918 - Class A"
/ip firewall address-list add list=LAN address=172.16.0.0/12 comment="RFC1918 - Class B"
# Create WAN interface list
/interface list add name=WAN comment="Internet-facing interfaces"
/interface list member add list=WAN interface=ether1 comment="Main internet connection"
# Create LAN interface list
/interface list add name=LAN comment="Internal network interfaces"
/interface list member add list=LAN interface=bridge comment="LAN bridge"
# Disable unnecessary services
/ip service disable telnet,ftp,www,api,api-ssl
# Secure SSH
/ip service set ssh port=2222 address=192.168.1.0/24
# Secure WinBox
/ip service set winbox port=8291 address=192.168.1.0/24
# Secure web interface
/ip service set www-ssl port=8443 address=192.168.1.0/24 certificate=https-cert
# API security
/ip service set api-ssl port=8729 address=192.168.1.0/24 certificate=api-cert
# Allow DNS queries from LAN only
/ip firewall filter add chain=input action=accept protocol=udp dst-port=53 src-address-list=LAN comment="Accept DNS from LAN"
/ip firewall filter add chain=input action=drop protocol=udp dst-port=53 comment="Drop external DNS queries"
# Allow NTP from LAN
/ip firewall filter add chain=input action=accept protocol=udp dst-port=123 src-address-list=LAN comment="Accept NTP from LAN"
# Secure SNMP (if needed)
/ip firewall filter add chain=input action=accept protocol=udp dst-port=161 src-address-list=SNMP-MGMT comment="Accept SNMP from management"
# Accept established and related
/ipv6 firewall filter add chain=input action=accept connection-state=established,related comment="Accept established/related"
# Accept ICMPv6 (essential for IPv6)
/ipv6 firewall filter add chain=input action=accept protocol=icmpv6 comment="Accept ICMPv6"
# Accept link-local
/ipv6 firewall filter add chain=input action=accept src-address=fe80::/16 comment="Accept link-local"
# Accept loopback
/ipv6 firewall filter add chain=input action=accept src-address=::1 comment="Accept loopback"
# Drop invalid
/ipv6 firewall filter add chain=input action=drop connection-state=invalid comment="Drop invalid"
# Allow management from LAN
/ipv6 firewall filter add chain=input action=accept src-address=2001:db8::/32 protocol=tcp dst-port=22 comment="SSH from IPv6 LAN"
# Drop all other input
/ipv6 firewall filter add chain=input action=drop comment="Drop all other IPv6 input"
# Temporarily log all traffic to debug
/ip firewall filter add chain=forward action=log log-prefix="FWD-DEBUG: " place-before=0
# Check specific rule matches
/ip firewall filter print stats where comment~"problem-rule"
# Disable all rules temporarily (DANGEROUS - do only locally!)
/ip firewall filter disable [find]
# Enable rules one by one to identify issue
/ip firewall filter enable 0,1,2
# Monitor in real time
/log print follow where topics~"firewall"
# Check connection tracking usage
/ip firewall connection tracking print
# Monitor CPU usage
/system resource print
# Check if FastTrack is working
/ip firewall filter print stats where action=fasttrack-connection
# Disable connection tracking for specific traffic (if needed)
/ip firewall raw add chain=prerouting action=notrack dst-port=80 protocol=tcp comment="NoTrack HTTP"