1. BGP คืออะไร?
BGP (Border Gateway Protocol) เป็น EGP (Exterior Gateway Protocol) ที่ทำหน้าที่เป็น "Glue of the Internet" เชื่อมต่อ Autonomous Systems (AS) ต่างๆ เข้าด้วยกัน
BGP เป็น Path-Vector Protocol ที่ใช้ AS-Path ในการตัดสินใจเลือกเส้นทาง และป้องกัน Routing Loops โดยธรรมชาติ
Autonomous System (AS) Concept
Massive Scale
รองรับ 900,000+ routes (Full Internet Table) เพียงพอสำหรับทั้ง Internet
Policy Control
ควบคุมเส้นทางด้วย Attributes มากมาย (Local Pref, MED, Communities)
Loop Prevention
AS-Path ป้องกัน Loops โดยอัตโนมัติ (ไม่รับ Route ที่มี AS ของตัวเอง)
BGP vs OSPF: เปรียบเทียบ
| Feature | BGP | OSPF |
|---|---|---|
| ประเภท | Path-Vector EGP | Link-State IGP |
| Use Case | Inter-AS, ISP Peering | Intra-AS, Enterprise |
| Scale | 900,000+ routes | ~10,000 routes |
| Convergence | ช้า (นาที) | เร็ว (วินาที) |
| Metric | AS-Path + Attributes | Cost (Bandwidth) |
| Policy Control | มาก (Fine-grained) | จำกัด |
| TCP Port | 179 | IP Protocol 89 |
2. IBGP vs EBGP
BGP แบ่งเป็น 2 ประเภทตามการเชื่อมต่อ:
- eBGP External BGP - เชื่อมต่อระหว่าง AS ต่างกัน (เช่น เรากับ ISP)
- iBGP Internal BGP - เชื่อมต่อภายใน AS เดียวกัน (เช่น Edge Router กับ Core Router)
IBGP vs EBGP Topology
เปรียบเทียบ IBGP vs EBGP
| Feature | eBGP | iBGP |
|---|---|---|
| Peer AS | AS ต่างกัน | AS เดียวกัน |
| TTL | 1 (Default) | 255 |
| Next Hop | เปลี่ยนเป็นตัวเอง | ไม่เปลี่ยน (ต้อง IGP) |
| Local Pref | ไม่ Propagate ไป eBGP | Propagate ใน AS |
| MED | Propagate ไป eBGP เท่านั้น | Propagate ใน AS |
| Route Propagation | ประกาศได้ทุก Peer | ไม่ Propagate ไป iBGP อื่น* |
* ต้องใช้ Route Reflector หรือ Full Mesh สำหรับ iBGP Propagation
3. BGP Attributes
BGP Attributes เป็นข้อมูลที่แนบมากับแต่ละ Route ใช้ในการตัดสินใจเลือกเส้นทางและ Policy Control
BGP Path Attributes
| Attribute | Type | Description | Scope |
|---|---|---|---|
| AS-Path | Well-Known Mandatory | ลำดับ AS ที่ Route วิ่งผ่านมา (Loop Prevention) | Global |
| Next-Hop | Well-Known Mandatory | IP Address ของ Router ถัดไป | Global |
| Local Preference | Well-Known Discretionary | Priority ภายใน AS (Default: 100) | iBGP Only |
| MED (Multi-Exit Discriminator) | Optional Non-Transitive | แนะนำให้neighbor AS เข้าทาง Path ไหน (Default: 0) | eBGP Only |
| Origin | Well-Known Mandatory | แหล่งที่มาของ Route (IGP=0, EGP=1, Incomplete=2) | Global |
| Communities | Optional Transitive | Tags สำหรับจัดกลุ่ม Routes | Global |
| Weight (Cisco Proprietary) | Proprietary | Priority บน Router ตัวเอง (ไม่ Propagate) | Local Only |
Attribute Priority ใน Route Selection
- 1 Highest Weight (Cisco only, MikroTik ใช้ Local-Pref)
- 2 Highest Local Preference (สำคัญที่สุดใน AS)
- 3 Locally Originated (Route ที่เราประกาศเอง)
- 4 Shortest AS-Path (จำนวน AS น้อยสุด)
- 5 Lowest Origin Type (IGP < EGP < Incomplete)
- 6 Lowest MED (ถ้า Same Neighbor AS)
- 7 eBGP over iBGP (Prefer External)
- 8 Lowest IGP Metric to Next-Hop
- 9 Oldest Route / Lowest Router ID
4. BGP Route Selection Process
BGP ใช้ Deterministic Algorithm ในการเลือก Best Path โดยเรียงลำดับความสำคัญของ Attributes
Route Selection Flowchart
5. BGP Configuration บน MikroTik RouterOS v7
Basic BGP Configuration
# ========================================
# 1. ตั้งค่า BGP Instance
# ========================================
/routing/bgp/connection
add name=bgp-main \
as=65001 \
router-id=1.1.1.1 \
comment="BGP Instance for AS65001"
# ========================================
# 2. เพิ่ม eBGP Peer (ISP 1)
# ========================================
/routing/bgp/connection
add name=isp1-peer \
instance=bgp-main \
remote.address=203.0.113.1 \
remote.as=100 \
local.address=203.0.113.2 \
output.procname=isp1-out \
input.procname=isp1-in \
comment="ISP 1 eBGP Peer"
# ========================================
# 3. เพิ่ม eBGP Peer (ISP 2)
# ========================================
/routing/bgp/connection
add name=isp2-peer \
instance=bgp-main \
remote.address=198.51.100.1 \
remote.as=200 \
local.address=198.51.100.2 \
output.procname=isp2-out \
input.procname=isp2-in \
comment="ISP 2 eBGP Peer"
# ========================================
# 4. ประกาศ Networks ของเรา
# ========================================
/routing/bgp/network
add network=203.0.113.0/24 synchronize=no
add network=198.51.100.0/24 synchronize=no
# ========================================
# 5. ตรวจสอบ BGP Status
# ========================================
/routing/bgp/connection/print
/routing/bgp/peer/print
/routing/bgp/routes/print
BGP Templates (RouterOS v7.8+)
# ========================================
# สร้าง BGP Template (RouterOS v7.8+)
# ========================================
# Template สำหรับ eBGP Peers
/routing/bgp/template
add name=ebgp-template \
as=65001 \
router-id=1.1.1.1 \
output.default-originate=if-installed \
input.accept-remote-as=yes \
hold-time=3m \
keepalive-time=1m
# Template สำหรับ iBGP Peers
/routing/bgp/template
add name=ibgp-template \
as=65001 \
router-id=1.1.1.1 \
output.default-originate=never \
input.accept-remote-as=no \
hold-time=3m
# ========================================
# ใช้ Template กับ Connection
# ========================================
/routing/bgp/connection
add name=isp1-peer \
template=ebgp-template \
remote.address=203.0.113.1 \
remote.as=100
Verification Commands
# ========================================
# ตรวจสอบ BGP Sessions
# ========================================
/routing/bgp/connection/print
# ดู State ควรเป็น "established"
# ========================================
# ตรวจสอบ BGP Routes ที่รับ
# ========================================
/routing/bgp/routes/print
/routing/bgp/routes/print where peer=isp1-peer
# ========================================
# ดู BGP Advertisements
# ========================================
/routing/bgp/advertisements/print
# ========================================
# ดู BGP Route ใน Routing Table
# ========================================
/ip/route/print where bgp
# ========================================
# Debug BGP Events
# ========================================
/routing/bgp/monitor
# ========================================
# ดู BGP Session Statistics
# ========================================
/routing/bgp/connection/stats/print
6. Multi-homing Configuration
Multi-homing คือการเชื่อมต่อกับ ISP หลายเจ้าเพื่อเพิ่ม Reliability และ Performance โดยใช้ BGP Policy ควบคุม Traffic
Scenario: Primary/Backup ISP
# ========================================
# Scenario: ISP1 เป็น Primary, ISP2 เป็น Backup
# ========================================
# วิธีที่ 1: ใช้ Local Preference
# ISP1 = Primary (Local-Pref 200)
# ISP2 = Backup (Local-Pref 100)
/routing/filter/rule
add chain=isp1-in \
action=accept \
set-bgp-local-pref=200 \
comment="Prefer ISP1 (Primary)"
add chain=isp2-in \
action=accept \
set-bgp-local-pref=100 \
comment="ISP2 Backup (Lower Pref)"
# ========================================
# วิธีที่ 2: ใช้ AS-Path Prepending (Inbound)
# ทำให้ ISP2 มี AS-Path ยาวกว่า
# ========================================
/routing/filter/rule
add chain=isp2-out \
action=accept \
set-bgp-as-path-prepend=65001,65001,65001 \
comment="Prepend AS to make ISP2 less attractive"
# ========================================
# วิธีที่ 3: ใช้ MED (Outbound)
# บอก ISP ว่าให้เข้ามาทาง ISP1
# ========================================
/routing/filter/rule
add chain=isp1-out \
action=accept \
set-bgp-med=10 \
comment="Lower MED = Prefer ISP1"
add chain=isp2-out \
action=accept \
set-bgp-med=100 \
comment="Higher MED = ISP2 Backup"
# ========================================
# Load Balancing (Equal Preference)
# ========================================
/routing/filter/rule
add chain=isp1-in action=accept set-bgp-local-pref=150
add chain=isp2-in action=accept set-bgp-local-pref=150
# Traffic จะกระจายไปทั้งสอง ISP
Multi-homing Traffic Flow
7. BGP Communities
BGP Communities เป็นวิธี Tagging Routes เพื่อให้ ISP หรือ Peer จัดการ Policy ตามที่เราต้องการ เช่น กำหนดให้ไม่โฆษณา Route ไปต่อ
Well-Known BGP Communities
| Community | Name | Description |
|---|---|---|
| 0:0 | NO_EXPORT | ไม่โฆษณาไป eBGP Peer (โฆษณาใน iBGP เท่านั้น) |
| 0xFF:0xFF | NO_ADVERTISE | ไม่โฆษณาไปไหนเลย (ทั้ง iBGP และ eBGP) |
| 0xFFFF:0 | NO_EXPORT_SUBCONFED | ไม่โฆษณาออกจาก Confederation |
| 0:65535 | Graceful Shutdown | บอกให้ Neighbor ใช้เส้นทางอื่น |
Community Configuration
# ========================================
# กำหนด Community ให้กับ Route
# ========================================
# วิธีที่ 1: กำหนดตอนประกาศ Network
/routing/bgp/network
add network=203.0.113.0/24 \
synchronize=no \
bgp-communities=65001:100
# วิธีที่ 2: ใช้ Filter Rule
/routing/filter/rule
add chain=isp1-out \
action=accept \
set-bgp-communities=65001:100,no-export \
comment="Tag route with community + no-export"
# ========================================
# กรอง Route ตาม Community
# ========================================
/routing/filter/rule
add chain=isp1-in \
bgp-communities=65001:100 \
action=accept \
set-bgp-local-pref=200 \
comment="Routes with community 65001:100 get high pref"
# ========================================
# ISP-Specific Communities (ตัวอย่าง)
# ========================================
# 65001:0 = Local Only
# 65001:100 = Customers
# 65001:200 = Peers
# 65001:300 = Transit
# 65001:666 = Blackhole
# Blackhole Route (DDoS Mitigation)
/routing/filter/rule
add chain=blackhole-in \
bgp-communities=65001:666 \
action=accept \
set-type=blackhole \
comment="Blackhole traffic for DDoS mitigation"
8. Route Filtering
Route Filtering เป็นการควบคุม Routes ที่รับและส่ง ป้องกันการรับ Routes ที่ไม่ต้องการ และป้องกันการ Leak Routes
Route Filtering Examples
# ========================================
# 1. กรอง Inbound - รับเฉพาะ Default Route
# ========================================
/routing/filter/rule
add chain=isp1-in \
prefix=0.0.0.0/0 \
prefix-length=0-0 \
action=accept \
comment="Accept only default route"
add chain=isp1-in \
action=discard \
comment="Discard everything else"
# ========================================
# 2. กรอง Inbound - ไม่รับ Private ASN
# ========================================
/routing/filter/rule
add chain=isp1-in \
bgp-as-path="^6[4-9][0-9][0-9][0-9]$" \
action=discard \
comment="Reject private AS numbers"
# ========================================
# 3. กรอง Outbound - ไม่ส่ง Routes ที่ไม่ใช่ของเรา
# ========================================
/routing/filter/rule
add chain=isp1-out \
prefix=203.0.113.0/24 \
prefix-length=24-24 \
action=accept \
comment="Advertise our prefix only"
add chain=isp1-out \
action=discard \
comment="Discard everything else"
# ========================================
# 4. กรองตาม AS-Path (Regex)
# ========================================
# ไม่รับ Routes ที่ผ่าน AS 12345
add chain=isp1-in \
bgp-as-path=".*12345.*" \
action=discard \
comment="Reject routes via AS12345"
# ========================================
# 5. Prefix List Filtering
# ========================================
# รับเฉพาะ /24 ถึง /32
add chain=isp1-in \
prefix=0.0.0.0/0 \
prefix-length=24-32 \
action=accept
# ปฏิเสธ Prefix ที่ยาวเกินไป (Bogon)
add chain=isp1-in \
prefix=0.0.0.0/0 \
prefix-length=33-128 \
action=discard
9. Troubleshooting BGP
ปัญหา: BGP Session ไม่ Established (Active/Idle/Connect)
สาเหตุที่พบบ่อย:
- • TCP Port 179 ถูกบล็อก
- • AS Number ไม่ตรงกัน
- • IP Address ผิด
- • TTL ไม่เพียงพอ (eBGP Multihop)
- • Hold Timer Expire
# ตรวจสอบ Session State
/routing/bgp/connection/print
# ตรวจสอบ Firewall
/ip/firewall/filter/print where port=179
# ตรวจสอบ TCP Connection
/ip/firewall/connection/print where dst-port=179
# เปิด Debug
/routing/bgp/monitor
ปัญหา: ไม่ได้รับ Routes จาก Peer
วิธีตรวจสอบ:
# ดู Routes ที่รับ
/routing/bgp/routes/print where peer=isp1-peer
# ดูว่า Route ถูก Reject หรือไม่
/routing/bgp/adj-rib-in/print
# ตรวจสอบ Filter Rules
/routing/filter/rule/print
# ตรวจสอบ Next-Hop Reachability
/ping [bgp-route-next-hop]
# ดู Rib
/routing/route/print where bgp
ปัญหา: Routes ไม่ถูกโฆษณาไปยัง Peer
วิธีแก้:
# ตรวจสอบว่า Network ถูกประกาศ
/routing/bgp/network/print
# ตรวจสอบว่า Route อยู่ใน RIB
/ip/route/print where dst-address=203.0.113.0/24
# ตรวจสอบ Advertisements
/routing/bgp/advertisements/print
# ตรวจสอบ Output Filter
/routing/filter/rule/print where chain=isp1-out
# synchronize=no สำคัญ!
/routing/bgp/network/set [find network=203.0.113.0/24] synchronize=no
10. สรุป
สิ่งที่ได้เรียนรู้
BGP Fundamentals
- • Path-Vector Protocol สำหรับ Internet
- • Scale 900,000+ routes
- • AS-Path Loop Prevention
BGP Attributes
- • Local Preference (iBGP)
- • AS-Path, MED, Communities
- • Route Selection Algorithm
Multi-homing
- • Primary/Backup Configuration
- • Traffic Engineering
- • Load Balancing
MikroTik Configuration
- • BGP Instance & Connections
- • Route Filtering
- • Communities & Policy