Layer-7 filtering enables RouterOS to inspect packet contents and identify applications, protocols, and specific traffic patterns for advanced filtering, QoS, and bandwidth management beyond simple port-based rules.
In WinBox you can configure layer-7 protocols in IP -> Firewall -> Layer7 Protocols, then use them in filter, mangle, or queue rules.
Layer-7 inspection allows identification of applications that use dynamic ports, encrypted traffic, or protocols that tunnel through standard ports.
Layer-7 fundamentals
How layer-7 detection works
Detection methods:
Regular expressions - Pattern matching against packet payload
Connection state tracking - First few packets of connections analyzed
Signature matching - Known application signatures and behaviors
Port-independent - Identifies protocols regardless of port used
Processing characteristics:
CPU intensive - Requires packet content inspection
Connection-based - Only first packets analyzed, then connection marked
Memory usage - Stores connection states and patterns
Performance impact - More complex patterns affect throughput
Common use cases:
Application blocking - Block P2P, social media, streaming
Bandwidth management - Limit specific application bandwidth
QoS policies - Prioritize business vs recreational traffic
# Block Facebook during business hours
/ip firewall filter add chain=forward action=drop layer7-protocol=facebook \
time=8h-17h,mon,tue,wed,thu,fri comment="Block Facebook during work hours"
# Block multiple social platforms
/ip firewall filter add chain=forward action=drop \
layer7-protocol=facebook,twitter,instagram,tiktok \
src-address-list=EMPLOYEES comment="Block social media for employees"
# Allow social media for management
/ip firewall filter add chain=forward action=accept \
layer7-protocol=facebook,twitter,instagram \
src-address-list=MANAGEMENT comment="Allow social media for management"
# Block BitTorrent completely
/ip firewall filter add chain=forward action=drop layer7-protocol=bittorrent \
comment="Block BitTorrent traffic"
# Block during business hours only
/ip firewall filter add chain=forward action=drop \
layer7-protocol=bittorrent,edonkey \
time=8h-18h,mon,tue,wed,thu,fri comment="Block P2P during business hours"
# Limit P2P to specific users
/ip firewall filter add chain=forward action=accept layer7-protocol=bittorrent \
src-address-list=P2P-ALLOWED comment="Allow P2P for specific users"
/ip firewall filter add chain=forward action=drop layer7-protocol=bittorrent \
comment="Block P2P for all others"
# Block video streaming during peak hours
/ip firewall filter add chain=forward action=drop \
layer7-protocol=youtube,netflix,twitch \
time=9h-12h,14h-17h,mon,tue,wed,thu,fri \
comment="Block streaming during peak hours"
# Allow streaming for VIP users
/ip firewall filter add chain=forward action=accept \
layer7-protocol=youtube,netflix \
src-address-list=VIP-USERS comment="Allow streaming for VIP users"
# Mark VoIP traffic for high priority
/ip firewall mangle add chain=prerouting action=mark-connection \
layer7-protocol=sip-voip new-connection-mark=voip-conn passthrough=yes \
comment="Mark VoIP connections"
/ip firewall mangle add chain=prerouting action=mark-packet \
connection-mark=voip-conn new-packet-mark=voip-priority passthrough=no \
comment="Mark VoIP packets for priority"
# Mark streaming for bandwidth limiting
/ip firewall mangle add chain=prerouting action=mark-connection \
layer7-protocol=youtube,netflix new-connection-mark=streaming-conn \
passthrough=yes comment="Mark streaming connections"
/ip firewall mangle add chain=prerouting action=mark-packet \
connection-mark=streaming-conn new-packet-mark=streaming-traffic \
passthrough=no comment="Mark streaming traffic for limiting"
# Mark P2P for lowest priority
/ip firewall mangle add chain=prerouting action=mark-connection \
layer7-protocol=bittorrent new-connection-mark=p2p-conn passthrough=yes \
comment="Mark P2P connections"
/ip firewall mangle add chain=prerouting action=mark-packet \
connection-mark=p2p-conn new-packet-mark=p2p-traffic passthrough=no \
comment="Mark P2P traffic for lowest priority"
# Create queue tree for application-based bandwidth management
# (This requires the mangle marks created above)
# High priority for VoIP (guarantee 1Mbps, max 2Mbps)
/queue tree add name=voip-queue parent=global packet-mark=voip-priority \
limit-at=1M max-limit=2M priority=1 comment="VoIP guaranteed bandwidth"
# Medium priority for business apps (guarantee 5Mbps, max 15Mbps)
/queue tree add name=business-queue parent=global packet-mark=business-traffic \
limit-at=5M max-limit=15M priority=3 comment="Business applications"
# Limited bandwidth for streaming (max 10Mbps per connection)
/queue tree add name=streaming-queue parent=global packet-mark=streaming-traffic \
max-limit=10M priority=6 comment="Streaming bandwidth limit"
# Lowest priority for P2P (use remaining bandwidth only)
/queue tree add name=p2p-queue parent=global packet-mark=p2p-traffic \
max-limit=50M priority=8 comment="P2P lowest priority"
# Detect custom application by HTTP header
/ip firewall layer7-protocol add name=custom-app \
regexp="^.*User-Agent: CustomApp/.*$"
# Detect by specific URL patterns
/ip firewall layer7-protocol add name=internal-api \
regexp="^GET /api/v[0-9]+/.*$"
# Detect by custom protocol signature
/ip firewall layer7-protocol add name=proprietary-protocol \
regexp="^\\x4d\\x59\\x41\\x50\\x50.*$"
# Use these patterns in firewall rules
/ip firewall filter add chain=forward action=accept layer7-protocol=custom-app \
comment="Allow custom application"