Virtual Routing and Forwarding (VRF)
Virtual Routing and Forwarding (VRF)
Section titled “Virtual Routing and Forwarding (VRF)”TL;DR (Quick Start)
Section titled “TL;DR (Quick Start)”For the impatient: isolate an interface into its own routing domain.
# Create VRF and assign interface/ip vrf add name=customer1 interfaces=ether2
# Add IP address (automatically uses VRF's routing table)/ip address add address=192.168.100.1/24 interface=ether2
# Verify VRF routes/ip route print where routing-table=customer1Overview
Section titled “Overview”What this does: Virtual Routing and Forwarding (VRF) creates multiple independent routing tables on a single router. Each VRF operates as if it were a separate router, with its own routing table, interfaces, and forwarding decisions. Traffic in one VRF cannot reach another VRF unless explicitly configured (route leaking).
When to use this:
- Service provider networks - Isolate customer traffic on shared infrastructure
- Multi-tenant environments - Separate routing for different clients
- Management networks - Isolate management traffic from production
- Overlapping IP spaces - Support multiple networks using the same address ranges
- BGP/MPLS VPNs - L3VPN implementation with route distinguishers
Prerequisites:
- RouterOS 7.0 or newer (v6 uses different routing-mark approach)
- Understanding of routing tables and IP addressing
- For BGP VPN: BGP package and MPLS configuration
7.14 Firewall Changes
RouterOS 7.14 introduced significant changes to how VRFs interact with firewalls. When interfaces are added to a VRF, firewall rules must reference the VRF virtual interface instead of individual physical interfaces. Review the Firewall Integration section if upgrading.
Key Concepts
Section titled “Key Concepts”VRF vs Routing Table
Section titled “VRF vs Routing Table”In RouterOS, creating a VRF automatically creates a corresponding routing table with the same name. The VRF defines which interfaces belong to the isolated network domain, while the routing table holds the routes for that domain.
Route Distinguisher (RD)
Section titled “Route Distinguisher (RD)”For BGP/MPLS VPN configurations, the Route Distinguisher makes routes globally unique even when IP prefixes overlap between VRFs. Format: ASN:number or IP:number (e.g., 65000:1 or 10.0.0.1:1).
Route Targets (RT)
Section titled “Route Targets (RT)”Control which routes are imported/exported between VRFs in BGP VPN configurations. Import RT determines which routes a VRF accepts; export RT tags routes leaving a VRF.
Configuration Steps
Section titled “Configuration Steps”Step 1: Create a VRF Instance
Section titled “Step 1: Create a VRF Instance”/ip vrf add name=customer1 interfaces=ether2,ether3This creates:
- A VRF named “customer1”
- Associates ether2 and ether3 with this VRF
- Automatically creates a routing table named “customer1”
Step 2: Verify VRF Creation
Section titled “Step 2: Verify VRF Creation”/ip vrf printExpected Output:
Flags: X - disabled # NAME INTERFACES 0 customer1 ether2,ether3Step 3: Check Routing Table Created
Section titled “Step 3: Check Routing Table Created”/routing table printExpected Output:
# NAME FIB 0 main 1 customer1Step 4: Add Routes to the VRF
Section titled “Step 4: Add Routes to the VRF”Routes for VRF interfaces are automatically added to the VRF’s routing table. To add static routes:
/ip route add dst-address=10.0.0.0/24 gateway=192.168.1.1 routing-table=customer1Step 5: Verify Routes in VRF
Section titled “Step 5: Verify Routes in VRF”/ip route print where routing-table=customer1Common Configuration Scenarios
Section titled “Common Configuration Scenarios”Scenario 1: Basic VRF Isolation
Section titled “Scenario 1: Basic VRF Isolation”Isolate two customer networks on the same router:
# Create VRFs/ip vrf add name=customer1 interfaces=ether2/ip vrf add name=customer2 interfaces=ether3
# Add addresses (each in its own VRF context)/ip address add address=192.168.1.1/24 interface=ether2/ip address add address=192.168.1.1/24 interface=ether3
# Note: Same IP range works because they're in different VRFsScenario 2: VRF with Internet Access (Route Leaking)
Section titled “Scenario 2: VRF with Internet Access (Route Leaking)”Provide internet access to a VRF using the main routing table’s default gateway:
# Create VRF/ip vrf add name=customer1 interfaces=ether2
# Add customer gateway address/ip address add address=192.168.100.1/24 interface=ether2
# Add default route that resolves in main table (note @main)/ip route add dst-address=0.0.0.0/0 gateway=10.0.0.1@main routing-table=customer1
# Configure NAT for customer traffic/ip firewall nat add chain=srcnat out-interface=ether1-wan action=masqueradeKey syntax: gateway=IP@table specifies which routing table resolves the gateway.
Scenario 3: VRF with DHCP Server
Section titled “Scenario 3: VRF with DHCP Server”Run a DHCP server within a VRF:
# Create VRF and assign interface/ip vrf add name=customer1 interfaces=ether2
# Add IP address/ip address add address=192.168.100.1/24 interface=ether2
# Create DHCP pool/ip pool add name=customer1-pool ranges=192.168.100.100-192.168.100.200
# Configure DHCP server (uses VRF automatically via interface)/ip dhcp-server add name=customer1-dhcp interface=ether2 address-pool=customer1-pool
# Add DHCP network/ip dhcp-server network add address=192.168.100.0/24 gateway=192.168.100.1 dns-server=8.8.8.8Scenario 4: BGP VPN with Route Distinguisher
Section titled “Scenario 4: BGP VPN with Route Distinguisher”For MPLS L3VPN configurations:
# Create VRF with route distinguisher/ip vrf add name=customer1 interfaces=ether2
# Configure route distinguisher and targets/ip route vrfadd vrf=customer1 route-distinguisher=65000:1 \ import-route-targets=65000:1 export-route-targets=65000:1Scenario 5: Services Bound to VRF
Section titled “Scenario 5: Services Bound to VRF”Bind services to listen on specific VRF:
# DNS server in VRF/ip dns set servers=8.8.8.8 vrf=customer1
# SSH listening on VRF/ip service set ssh address=0.0.0.0/0 vrf=customer1
# Or use @vrf syntax for one-off operations/tool fetch url="http://example.com" vrf=customer1Firewall Integration
Section titled “Firewall Integration”RouterOS 7.14+ Changes
Section titled “RouterOS 7.14+ Changes”Starting with RouterOS 7.14, when interfaces are added to a VRF, a virtual VRF interface is automatically created. Firewall rules must reference this VRF interface instead of individual physical interfaces.
Before 7.14:
# Matched individual interfaces in VRF/ip firewall filter add chain=input in-interface=ether2 action=acceptAfter 7.14:
# Must match VRF virtual interface/ip firewall filter add chain=input in-interface=customer1 action=acceptMatching Specific Interfaces Within VRF
Section titled “Matching Specific Interfaces Within VRF”To match specific physical interfaces within a VRF (7.14+), use connection marking:
# Mark connections from specific interface/ip firewall mangle add chain=prerouting in-interface=ether2 action=mark-connection new-connection-mark=from-ether2
# Then filter based on connection mark/ip firewall filter add chain=forward connection-mark=from-ether2 action=acceptNAT with VRF
Section titled “NAT with VRF”# Masquerade traffic from VRF going to internet/ip firewall nat add chain=srcnat out-interface=ether1-wan src-address=192.168.100.0/24 action=masqueradeVerification
Section titled “Verification”Check 1: Verify VRF Configuration
Section titled “Check 1: Verify VRF Configuration”/ip vrf printExpected: VRF listed with correct interfaces.
Check 2: Verify Routing Tables
Section titled “Check 2: Verify Routing Tables”/routing table printExpected: Each VRF has a corresponding routing table.
Check 3: Check Routes in VRF
Section titled “Check 3: Check Routes in VRF”/ip route print where routing-table=customer1Expected: Routes specific to the VRF, including connected routes for assigned interfaces.
Check 4: Test Connectivity from VRF
Section titled “Check 4: Test Connectivity from VRF”/ping 8.8.8.8 vrf=customer1Expected: Successful if VRF has internet access configured.
Check 5: Verify Interface Assignment
Section titled “Check 5: Verify Interface Assignment”/ip address print detail where interface=ether2Expected: Shows actual-interface reflecting VRF membership.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Solution |
|---|---|---|
| Routes not active in VRF | Gateway looking in wrong routing table | Use gateway=IP@main to specify table for gateway resolution |
| VRF interface not in firewall list (7.14+) | 7.14 changed interface behavior | Use VRF name as interface: in-interface=customer1 |
| Services not accessible in VRF | Service not bound to VRF | Configure vrf= parameter on service |
| Overlapping addresses conflicting | Interface not in VRF when address added | Create VRF before adding IP addresses |
| Wrong VRF matched | VRF matching is top-to-bottom | Reorder VRFs with /ip vrf move |
| Firewall rules broken after 7.14 upgrade | Interface matching changed | Use VRF virtual interface or connection marks |
| Ping fails from VRF | Tool using main table | Specify VRF: /ping 8.8.8.8 vrf=customer1 |
Debug: Gateway Resolution
Section titled “Debug: Gateway Resolution”If routes show inactive, check gateway resolution:
# Wrong - gateway looks in customer1 table where it doesn't exist/ip route add dst-address=0.0.0.0/0 gateway=10.0.0.1 routing-table=customer1
# Right - gateway explicitly resolves in main table/ip route add dst-address=0.0.0.0/0 gateway=10.0.0.1@main routing-table=customer1Debug: VRF Order
Section titled “Debug: VRF Order”Check current VRF order:
/ip vrf printMove specific VRF to top for priority matching:
/ip vrf move [find name=specific-vrf] destination=0Common Mistakes
- Forgetting @table for gateway resolution - Use
gateway=10.0.0.1@mainwhen the gateway is in a different routing table - Adding address before VRF assignment - Always create VRF and assign interface first, then add IP addresses
- Using physical interface in firewall (7.14+) - After 7.14, use VRF name (
in-interface=customer1) not physical interface - Expecting automatic NAT isolation - NAT rules must explicitly reference VRF source addresses
- Not specifying VRF for diagnostic tools - Use
vrf=parameter:/ping 8.8.8.8 vrf=customer1 - Assuming services automatically use VRF - Most services require explicit
vrf=configuration
Services with VRF Support
Section titled “Services with VRF Support”| Service | VRF Parameter | Notes |
|---|---|---|
| BGP | Per-connection | Full VRF support |
| OSPF | vrf | Per-instance |
| DNS | vrf | Client and server |
| DHCP Relay | vrf | Added in 7.15 |
| SSH/Telnet/WWW | vrf | Via /ip service |
| NTP | vrf | Client and server |
| SNMP | vrf | |
| RADIUS | vrf | |
| Ping/Traceroute | vrf | Diagnostic tools |
| Fetch | vrf | HTTP client |
| Netwatch | vrf | Monitoring |
Limitations
Section titled “Limitations”- Maximum 1024 VRF instances (routing table limit)
- Not all services support VRF (check documentation)
- VRF order matters - matching is top-to-bottom
- RouterOS 7.14+ changed firewall interface matching behavior
- Some features require specific RouterOS versions for VRF support
Related Topics
Section titled “Related Topics”Routing
Section titled “Routing”- Static Routes - basic routing configuration
- Routing Tables - multiple routing table management
- Routing Rules - policy-based routing
- BGP - BGP configuration for VPN
Network Services
Section titled “Network Services”- DHCP Relay - DHCP relay with VRF support (7.15+)
- DHCP Server - per-VRF DHCP services
- DNS Server - DNS with VRF
Security
Section titled “Security”- Firewall Basics - firewall rules with VRF
- IP Services - VRF-bound management services
Reference
Section titled “Reference”VRF Properties (/ip vrf)
Section titled “VRF Properties (/ip vrf)”| Property | Type | Default | Description |
|---|---|---|---|
name | string | - | VRF identifier (also becomes routing table name) |
interfaces | list | - | Interfaces assigned to this VRF |
disabled | yes/no | no | Disable VRF |
Route VRF Properties (/ip route vrf)
Section titled “Route VRF Properties (/ip route vrf)”| Property | Type | Default | Description |
|---|---|---|---|
vrf | string | - | VRF name reference |
route-distinguisher | string | - | RD format: ASN:number or IP:number |
import-route-targets | list | - | Route targets to import |
export-route-targets | list | - | Route targets to export |
Common VRF Parameters for Services
Section titled “Common VRF Parameters for Services”| Parameter | Usage | Example |
|---|---|---|
vrf= | Specify VRF for service/command | /ping 8.8.8.8 vrf=customer1 |
@table | Gateway resolution in specific table | gateway=10.0.0.1@main |
routing-table= | Target routing table for routes | routing-table=customer1 |
Commands
Section titled “Commands”| Command | Description |
|---|---|
/ip vrf add | Create new VRF |
/ip vrf print | List VRF instances |
/ip vrf move | Reorder VRF (affects matching priority) |
/routing table print | List all routing tables including VRF tables |
/ip route print where routing-table=X | Show routes in specific VRF |