สารบัญ
VLAN คืออะไร?
VLAN (Virtual Local Area Network) คือเทคโนโลยีที่ช่วยแบ่งเครือข่ายฟิสิคัลหนึ่งเครือข่าย ออกเป็นหลายเครือข่ายเสมือน (Virtual Network) โดยใช้ VLAN ID ตามมาตรฐาน IEEE 802.1Q
ข้อดีของการใช้ VLAN บน MikroTik:
- Security: แยก Broadcast Domain ช่วยลดความเสี่ยงด้านความปลอดภัย
- Performance: ลด Broadcast Traffic ทำให้เครือข่ายมีประสิทธิภาพมากขึ้น
- Cost Effective: ไม่ต้องซื้อ Switch แยกสำหรับแต่ละแผนก
- Easy Management: จัดการผ่าน RouterOS ได้ทั้งหมด
MikroTik VLAN Methods:
Bridge VLAN Filtering (แนะนำ)
วิธีที่แนะนำสำหรับ MikroTik สมัยใหม่
- รองรับ Hardware Offloading
- ใช้ Switch Chip ได้เต็มที่
- Configuration ง่ายและเข้าใจง่าย
- รองรับ RouterOS v7
VLAN Interface (Traditional)
วิธีแบบดั้งเดิม
- สร้าง VLAN เป็น Interface แยก
- เหมาะสำหรับ Router-on-a-Stick
- ใช้ CPU ในการประมวลผล
สถาปัตยกรรมเครือข่าย
ตัวอย่างสถาปัตยกรรมที่เราจะใช้ในบทความนี้ เป็นการตั้งค่า VLAN บน MikroTik Router โดยใช้ Bridge VLAN Filtering ซึ่งเป็นวิธีที่แนะนำสำหรับ RouterOS 7
| VLAN ID | ชื่อ | Subnet | Gateway | Port |
|---|---|---|---|---|
| 10 | Management | 192.168.10.0/24 | 192.168.10.1 | ether3 (untagged) |
| 20 | Office | 192.168.20.0/24 | 192.168.20.1 | ether4 (untagged) |
| 30 | Guest | 192.168.30.0/24 | 192.168.30.1 | ether5 (untagged) |
สิ่งที่ต้องเตรียม
Hardware Requirements
- MikroTik Router (RB750, RB951, hAP, CCR, หรือรุ่นอื่น)
- Managed Switch ที่รองรับ 802.1Q VLAN (optional)
- คอมพิวเตอร์สำหรับทดสอบ
- สาย LAN
Software Requirements
- RouterOS v6.40 ขึ้นไป (แนะนำ v7.x)
- WinBox หรือ WebFig สำหรับ Configuration
- Packages: system, bridge (มาติดตั้งแล้ว)
- พื้นฐาน TCP/IP Networking
ข้อควรระวัง
เมื่อเปิดใช้งาน Bridge VLAN Filtering การเชื่อมต่ออาจขาดหายได้ชั่วคราว แนะนำให้ทำผ่าน Console หรือ Port ที่ไม่ได้อยู่ใน Bridge และควร Backup Configuration ก่อนเริ่มต้น
สร้าง Bridge และเพิ่ม Ports
สร้าง Bridge สำหรับรวม Ports ทั้งหมด
วิธี WinBox
- 1 เปิด WinBox และ Login เข้า Router
-
2
ไปที่ Bridge → คลิก + → ตั้งชื่อ
bridge1→ OK - 3 ไปที่ Bridge → Ports → เพิ่ม Ports ที่ต้องการ
วิธี CLI (Terminal)
# สร้าง Bridge /interface bridge add name=bridge1 # เพิ่ม Ports เข้า Bridge /interface bridge port add bridge=bridge1 interface=ether2 add bridge=bridge1 interface=ether3 add bridge=bridge1 interface=ether4 add bridge=bridge1 interface=ether5 # ตรวจสอบ Bridge Ports /interface bridge port print
ตั้งค่า Bridge VLAN Filtering
กำหนด VLAN IDs และ Tagged/Untagged Ports
Bridge VLAN Filtering เป็นวิธีที่แนะนำสำหรับ MikroTik RouterOS 7 เพราะรองรับ Hardware Offloading และทำงานได้เร็วกว่าวิธีดั้งเดิม
Tagged (Trunk Port)
Port ที่ส่ง/รับ packets พร้อม VLAN Tag (802.1Q header)
- • ใช้สำหรับเชื่อมต่อกับ Switch หรือ Router ตัวอื่น
- • รองรับหลาย VLAN ใน Port เดียว
- • ต้องเพิ่ม bridge เองด้วย (สำหรับ CPU)
Untagged (Access Port)
Port ที่ส่ง/รับ packets แบบไม่มี VLAN Tag
- • ใช้สำหรับเชื่อมต่อกับ End Devices (PC, Printer)
- • 1 Port = 1 VLAN เท่านั้น
- • ใช้ PVID กำหนด VLAN
Configuration
# สร้าง VLAN entries ใน Bridge # VLAN 10 - Management (ether3 untagged, bridge tagged) /interface bridge vlan add bridge=bridge1 tagged=bridge1 untagged=ether3 vlan-ids=10 # VLAN 20 - Office (ether4 untagged, bridge tagged) /interface bridge vlan add bridge=bridge1 tagged=bridge1 untagged=ether4 vlan-ids=20 # VLAN 30 - Guest (ether5 untagged, bridge tagged) /interface bridge vlan add bridge=bridge1 tagged=bridge1 untagged=ether5 vlan-ids=30 # ตรวจสอง VLAN Table /interface bridge vlan print
ขั้นตอนสำคัญ: เปิด VLAN Filtering
หลังจากตั้งค่า VLAN Table เสร็จแล้ว ต้องเปิด VLAN Filtering บน Bridge การเปิดจะทำให้การเชื่อมต่อขาดหายชั่วคราว!
# เปิด VLAN Filtering (ทำหลังตั้งค่า VLAN เสร็จแล้ว) /interface bridge set bridge1 vlan-filtering=yes # ตรวจสอบสถานะ /interface bridge print
สร้าง VLAN Interface และกำหนด IP
สร้าง Interface สำหรับแต่ละ VLAN เพื่อใช้เป็น Gateway
VLAN Interface จะสร้างเป็น "sub-interface" บน Bridge เพื่อให้ Router สามารถ Routing ระหว่าง VLAN และทำหน้าที่เป็น Gateway ได้
# สร้าง VLAN Interfaces บน bridge1 /interface vlan add name=vlan10 interface=bridge1 vlan-id=10 comment="Management" add name=vlan20 interface=bridge1 vlan-id=20 comment="Office" add name=vlan30 interface=bridge1 vlan-id=30 comment="Guest" # กำหนด IP Addresses /ip address add address=192.168.10.1/24 interface=vlan10 comment="Management Gateway" add address=192.168.20.1/24 interface=vlan20 comment="Office Gateway" add address=192.168.30.1/24 interface=vlan30 comment="Guest Gateway" # ตรวจสอบ Interfaces /interface vlan print /ip address print
ผลลัพธ์
ตอนนี้ Router มี Gateway IP สำหรับแต่ละ VLAN:
• VLAN 10: 192.168.10.1
• VLAN 20: 192.168.20.1
• VLAN 30: 192.168.30.1
ตั้งค่า DHCP Server
กำหนด DHCP Server สำหรับแต่ละ VLAN
# สร้าง DHCP Address Pools /ip pool add name=dhcp_pool10 ranges=192.168.10.100-192.168.10.200 add name=dhcp_pool20 ranges=192.168.20.100-192.168.20.200 add name=dhcp_pool30 ranges=192.168.30.100-192.168.30.200 # สร้าง DHCP Networks /ip dhcp-server network add address=192.168.10.0/24 gateway=192.168.10.1 dns-server=8.8.8.8,8.8.4.4 add address=192.168.20.0/24 gateway=192.168.20.1 dns-server=8.8.8.8,8.8.4.4 add address=192.168.30.0/24 gateway=192.168.30.1 dns-server=8.8.8.8,8.8.4.4 # สร้าง DHCP Servers /ip dhcp-server add name=dhcp10 interface=vlan10 address-pool=dhcp_pool10 disabled=no add name=dhcp20 interface=vlan20 address-pool=dhcp_pool20 disabled=no add name=dhcp30 interface=vlan30 address-pool=dhcp_pool30 disabled=no # ตรวจสอบ DHCP Servers /ip dhcp-server print
Firewall Rules สำหรับ Inter-VLAN
ควบคุมการสื่อสารระหว่าง VLAN
Enable IP Firewall on Bridge
เพื่อให้ Firewall Rules ทำงานกับ Bridge Traffic ต้องเปิด
use-ip-firewall=yes
# เปิดใช้งาน IP Firewall บน Bridge
/interface bridge settings
set use-ip-firewall=yes
Firewall Filter Rules
# ============================================ # Forward Chain - Inter-VLAN Traffic Control # ============================================ # 1. Block Guest VLAN จากการเข้าถึง Management และ Office /ip firewall filter add chain=forward action=drop src-address=192.168.30.0/24 \ dst-address=192.168.10.0/24 comment="Block Guest -> Management" add chain=forward action=drop src-address=192.168.30.0/24 \ dst-address=192.168.20.0/24 comment="Block Guest -> Office" # 2. Block Office VLAN จากการเข้าถึง Management /ip firewall filter add chain=forward action=drop src-address=192.168.20.0/24 \ dst-address=192.168.10.0/24 comment="Block Office -> Management" # 3. Allow Management เข้าถึงทุก VLAN /ip firewall filter add chain=forward action=accept src-address=192.168.10.0/24 \ dst-address=192.168.0.0/16 comment="Allow Management -> All VLANs" # 4. Allow established connections /ip firewall filter add chain=forward action=accept connection-state=established,related # 5. Allow Internet Access for all VLANs (NAT required) # ดู Step 6 สำหรับ NAT Configuration # 6. Drop everything else between VLANs /ip firewall filter add chain=forward action=drop connection-state=new
NAT Configuration (Internet Access)
# สร้าง NAT Rule สำหรับออกอินเทอร์เน็ต /ip firewall nat add chain=srcnat action=masquerade out-interface=ether1 \ src-address=192.168.0.0/16 comment="NAT for all VLANs" # ตรวจสอบ NAT Rules /ip firewall nat print
หมายเหตุ
- • Order สำคัญ! Rules จะถูกประมวลผลจากบนลงล่าง
- • วาง accept rules ก่อน drop rules
- • ทดสอบทีละ rule และตรวจสอบด้วย
/ip firewall filter print stats
ทดสอบการเชื่อมต่อ
ตรวจสอบว่า VLAN ทำงานถูกต้อง
คำสั่งตรวจสอบบน MikroTik
# ตรวจสอบ Bridge VLAN Table /interface bridge vlan print detail # ตรวจสอบ VLAN Interfaces /interface vlan print # ตรวจสอบ IP Addresses /ip address print # ตรวจสอบ DHCP Server /ip dhcp-server print /ip dhcp-server lease print # Ping ทดสอบ ping 192.168.10.1 ping 192.168.20.1 ping 192.168.30.1
ผลลัพธ์ที่คาดหวัง
การทดสอบที่ควรสำเร็จ
- ✓ PC ใน VLAN เดียวกัน Ping กันได้
- ✓ PC ได้รับ IP จาก DHCP
- ✓ PC ออกอินเทอร์เน็ตได้
- ✓ Management เข้าถึง VLAN อื่นได้
การทดสอบที่ควรล้มเหลว (Security)
- ✗ Guest ไม่สามารถ Ping Management
- ✗ Guest ไม่สามารถ Ping Office
- ✗ Office ไม่สามารถ Ping Management
การแก้ไขปัญหา
ปัญหา: PC ไม่ได้รับ IP จาก DHCP
สาเหตุที่เป็นไปได้:
- • Port ไม่ได้กำหนด PVID ที่ถูกต้อง
- • VLAN Filtering ยังไม่เปิด
- • DHCP Server disabled
วิธีแก้:
- • ตรวจสอบ
/interface bridge vlan print - • เปิด VLAN Filtering:
set bridge1 vlan-filtering=yes - • ตรวจสอบ DHCP Server status
ปัญหา: Inter-VLAN ไม่สามารถสื่อสารกันได้
สาเหตุที่เป็นไปได้:
- • Firewall Rules block traffic
- • use-ip-firewall ไม่ได้เปิด
- • Gateway ผิดบน PC
วิธีแก้:
- • ตรวจสอบ firewall filter rules
- • เปิด
use-ip-firewall=yes - • ตรวจสอบ Default Gateway บน PC
ปัญหา: ออกอินเทอร์เน็ตไม่ได้
สาเหตุที่เป็นไปได้:
- • NAT Rule ไม่ถูกต้อง
- • DNS Server ไม่ทำงาน
- • Firewall block outbound
วิธีแก้:
- • ตรวจสอบ NAT:
/ip firewall nat print - • ทดสอบ ping 8.8.8.8 ก่อน
- • ตรวจสอบ DNS settings
คำสั่ง Debug
# ดู packet flow /tool sniffer quick interface=bridge1 # ดู firewall connections /ip firewall connection print # ดู firewall statistics /ip firewall filter print stats # Reset firewall counters /ip firewall filter reset-counters-all
คำถามที่พบบ่อย (FAQ)
Q: Bridge VLAN Filtering กับ VLAN Interface ต่างกันอย่างไร?
A: Bridge VLAN Filtering ใช้ Switch Chip ในการประมวลผล (Hardware Offload) ทำให้เร็วกว่า ส่วน VLAN Interface แบบดั้งเดิมจะใช้ CPU ในการประมวลผล สำหรับ RouterOS 7 แนะนำใช้ Bridge VLAN Filtering
Q: ทำไมต้องเพิ่ม bridge1 เข้าใน Tagged Ports?
A: เพื่อให้ CPU ของ Router สามารถรับส่ง traffic กับ VLAN ได้ ถ้าไม่เพิ่ม Router จะไม่สามารถทำหน้าที่เป็น Gateway หรือ DHCP Server ได้
Q: สามารถใช้ Switch ธรรมดาแทน Managed Switch ได้ไหม?
A: ได้ ถ้า MikroTik ทำหน้าที่เป็น Switch ด้วย (Bridge) แต่ถ้าต้องการใช้ Switch ตัวอื่นต้องเป็น Managed Switch ที่รองรับ 802.1Q VLAN เพื่อให้สามารถ Tag/Untag packets ได้
Q: ถ้าต้องการเชื่อมต่อ MikroTik กับ Cisco Switch ต้องทำอย่างไร?
A: ต้องตั้งค่า Trunk Port ทั้งสองฝั่งให้ตรงกัน:
• MikroTik: เพิ่ม port นั้นใน tagged ของทุก VLAN
• Cisco: ตั้งค่า switchport mode trunk และ switchport trunk allowed vlan 10,20,30
Q: RouterOS 6 และ 7 ต่างกันอย่างไร?
A: RouterOS 7 มี Bridge VLAN Filtering ที่ดีกว่าและรองรับ Hardware Offloading ได้ดีกว่า แนะนำให้อัปเกรดเป็น RouterOS 7 ถ้า Hardware รองรับ
สรุป
สิ่งที่เรียนรู้:
- สร้าง Bridge และเพิ่ม Ports
- ตั้งค่า Bridge VLAN Filtering
- สร้าง VLAN Interface และกำหนด IP
- ตั้งค่า DHCP Server
- Firewall Rules สำหรับ Inter-VLAN
- NAT สำหรับออกอินเทอร์เน็ต
Best Practices:
- ใช้ Bridge VLAN Filtering สำหรับ RouterOS 7
- เพิ่ม bridge เองใน tagged ports เสมอ
- เปิด use-ip-firewall เพื่อใช้ Firewall
- วาง accept rules ก่อน drop rules
- ทดสอบทีละขั้นตอน