Deep Dive Series

MikroTik OSPF Routing

OSPF v2 & v3 ฉบับสมบูรณ์

ตั้งแต่พื้นฐาน Link-State Protocol จนถึง Multi-Area Design, Authentication และ Troubleshooting

Link-State
Multi-Area
Authentication
Fast Convergence

1. OSPF คืออะไร?

OSPF (Open Shortest Path First) เป็น Dynamic Routing Protocol ประเภท Link-State ที่ใช้ Algorithm Dijkstra's Shortest Path First (SPF) ในการคำนวณหาเส้นทางที่ดีที่สุด

OSPF เป็น IGP (Interior Gateway Protocol) หมายถึงใช้ภายใน Autonomous System เดียว เหมาะสำหรับเครือข่ายขนาดกลางถึงใหญ่ เช่น Campus, Enterprise, Data Center

OSPF ทำงานอย่างไร?

1. Hello Packets ค้นหา Neighbors สร้าง Adjacency 2. LSDB Sync แลกเปลี่ยน LSAs สร้าง LSDB 3. SPF Calc Dijkstra Algorithm หา Shortest Path 4. RIB Update อัปเดต Routing Table 5. Forwarding ส่ง Packet ไปปลายทาง Key Concepts LSA = Link-State Advertisement | LSDB = Link-State Database | SPF = Shortest Path First

Hello Protocol

ส่ง Hello Packets เพื่อค้นหาและรักษา Neighbor Relationships ส่งทุก 10 วินาที (Broadcast) หรือ 30 วินาที (NBMA)

Link-State Database

เก็บข้อมูล Topology ทั้งหมดของ Area ใช้ Dijkstra Algorithm คำนวณหาเส้นทางที่สั้นที่สุด

Fast Convergence

ตรวจจับ Link Failure ได้เร็ว (1-10 วินาที) และคำนวณเส้นทางใหม่อัตโนมัติ

OSPF vs Routing Protocols อื่นๆ

Feature OSPF BGP RIP EIGRP
ประเภท Link-State Path-Vector Distance-Vector Hybrid
Metric Cost (Bandwidth) AS-Path + Attributes Hop Count Composite
Convergence เร็ว (วินาที) ช้า (นาที) ช้า (นาที) เร็วมาก
Scale ~10,000 routes 900,000+ routes < 500 routes ~10,000 routes
Vendor Standard (RFC 2328) Standard Standard Cisco Only*
ใช้กับ Enterprise, Campus ISP, Internet เครือข่ายเล็ก Cisco Networks

* EIGRP เปิดเป็น Standard แล้วแต่ยังใช้ส่วนใหญ่กับ Cisco

2. LSA Types (Link-State Advertisements)

LSA (Link-State Advertisement) เป็น Data Unit ที่ Router ใช้แจ้งข้อมูล Link State ของตัวเองให้ Neighbor ทราบ แต่ละประเภทมีหน้าที่แตกต่างกัน:

LSA Types ใน OSPF v2

Type Name Source Scope Function
Type 1 Router LSA ทุก Router Within Area แจ้ง Links, Cost และ State ของ Router
Type 2 Network LSA DR Only Within Area แจ้ง Router ที่อยู่ใน Multi-Access Network
Type 3 Summary LSA (Network) ABR Adjacent Areas โฆษณา Network จาก Area หนึ่งไปอีก Area
Type 4 Summary LSA (ASBR) ABR Adjacent Areas โฆษณาตำแหน่งของ ASBR
Type 5 AS External LSA ASBR Entire AS โฆษณา External Routes (Redistributed)
Type 7 NSSA External LSA ASBR in NSSA Within NSSA โฆษณา External Routes ใน NSSA Area

LSA Propagation Flow

Area 0 (Backbone) Area 1 Area 2 R1 Type 1 LSA ABR Type 3, 4 LSA ASBR Type 5 LSA External BGP/Static Type 1,2 = Intra-Area | Type 3,4 = Inter-Area | Type 5 = External | Type 7 = NSSA

3. Area Types

OSPF ใช้ Hierarchical Design แบ่งเครือข่ายเป็น Areas เพื่อลดขนาด LSDB และ Routing Table ทำให้ Scalable มากขึ้น

Backbone Area (Area 0)

Area หลักที่ทุก Area อื่นต้องเชื่อมต่อ จำเป็นสำหรับ Inter-Area Routing

Characteristics:
  • • Area ID = 0.0.0.0
  • • ทุก Area ต้องผ่าน Area 0
  • • มี LSAs ทุกประเภท

Standard Area (Normal)

Area ปกติที่รับ LSAs ทุกประเภท เหมาะสำหรับทุกกรณี

Characteristics:
  • • รับ Type 1, 2, 3, 4, 5 LSAs
  • • Full Routing Table
  • • ยืดหยุ่นที่สุด

Stub Area

Area ที่ไม่รับ External Routes (Type 5) ใช้ Default Route แทน

Characteristics:
  • • ไม่รับ Type 5 LSAs
  • • ABR ส่ง Default Route
  • • ลด Routing Table
  • ห้ามมี ASBR

Totally Stubby Area

Stub Area ที่ไม่รับ Summary Routes (Type 3, 4) ด้วย

Characteristics:
  • • ไม่รับ Type 3, 4, 5 LSAs
  • • มีเฉพาะ Intra-Area + Default
  • • Routing Table เล็กสุด
  • Cisco Extension

NSSA (Not-So-Stubby Area)

Stub Area ที่อนุญาตให้มี ASBR ได้ ใช้ Type 7 LSAs

Characteristics:
  • • รับ Type 7 LSAs (แปลงเป็น Type 5)
  • • มี ASBR ได้
  • • เหมาะกับ Branch Office

Totally NSSA

NSSA ที่ไม่รับ Summary Routes (Type 3, 4) ด้วย

Characteristics:
  • • ไม่รับ Type 3, 4, 5
  • • รับ Type 7 + Default Route
  • • ผสม Totally Stub + NSSA

เปรียบเทียบ Area Types

Area Type Type 1,2 Type 3 Type 4 Type 5 Type 7 ASBR?
Backbone -
Standard -
Stub -
Totally Stub ✗ (Default only) -
NSSA
Totally NSSA ✗ (Default only)

4. Router Types

ใน OSPF Network, Router แต่ละตัวมีบทบาท (Role) แตกต่างกันขึ้นอยู่กับตำแหน่งและหน้าที่:

Internal Router

Router ที่ Interface ทั้งหมดอยู่ใน Area เดียวกัน

  • • มี LSDB เฉพาะ Area ของตัวเอง
  • • ส่ง Type 1, 2 LSAs
  • • ตัวอย่าง: Branch Router, Access Switch

ABR (Area Border Router)

Router ที่มี Interface ในหลาย Area (ต้องมี Area 0)

  • • มี LSDB ของทุก Area ที่ตัวเองเชื่อม
  • • ส่ง Type 3, 4 LSAs (Summary)
  • • ตัวอย่าง: Core Router, Distribution Switch

ASBR (Autonomous System Boundary Router)

Router ที่ Redistribute Routes จาก Protocol อื่นเข้า OSPF

  • • ส่ง Type 5 LSAs (External)
  • • เชื่อม BGP, Static, RIP เข้า OSPF
  • • ตัวอย่าง: Internet Gateway, VPN Gateway

Backbone Router

Router ที่มี Interface อย่างน้อย 1 Port ใน Area 0

  • • อาจเป็น Internal หรือ ABR
  • • จำเป็นสำหรับ Inter-Area Traffic
  • • ตัวอย่าง: Data Center Core

5. Configuration บน MikroTik RouterOS v7

Basic OSPF v2 Configuration

OSPF Instance & Area Setup
# ========================================
# 1. สร้าง OSPF Instance
# ========================================
/routing/ospf/instance
add name=ospf-internal \
    router-id=1.1.1.1 \
    version=2 \
    distribute-default=if-installed-as-type-1 \
    redistribute-connected=as-type-1 \
    comment="OSPF Instance for Internal Network"

# ========================================
# 2. สร้าง OSPF Areas
# ========================================
# Area 0 - Backbone (จำเป็น)
/routing/ospf/area
add instance=ospf-internal \
    name=backbone \
    area-id=0.0.0.0 \
    comment="Backbone Area"

# Area 1 - Standard Area
/routing/ospf/area
add instance=ospf-internal \
    name=area1-office \
    area-id=0.0.0.1 \
    comment="Office Area 1"

# Area 2 - Stub Area (ไม่รับ External Routes)
/routing/ospf/area
add instance=ospf-internal \
    name=area2-branch \
    area-id=0.0.0.2 \
    type=stub \
    comment="Branch Office - Stub Area"

# ========================================
# 3. กำหนด Interface Templates
# ========================================
# Backbone Interfaces
/routing/ospf/interface-template
add interfaces=ether1-core \
    area=backbone \
    networks=10.0.0.0/30 \
    cost=10 \
    priority=1 \
    hello-interval=10s \
    dead-interval=40s \
    comment="Core Link to ABR"

# Area 1 Interfaces
add interfaces=bridge-lan1 \
    area=area1-office \
    networks=192.168.1.0/24 \
    cost=20 \
    comment="Office LAN 1"

# Area 2 Interfaces (Branch)
add interfaces=eoip-branch1 \
    area=area2-branch \
    networks=10.0.2.0/30 \
    cost=50 \
    comment="Branch 1 Tunnel"

# ========================================
# 4. ตรวจสอบ Configuration
# ========================================
/routing/ospf/instance/print
/routing/ospf/area/print
/routing/ospf/interface-template/print

OSPF Instance Parameters

Parameter Default Description
router-id Auto Unique ID สำหรับ Router (ควรกำหนด Manual)
version 2 2 = IPv4, 3 = IPv6
distribute-default none if-installed-as-type-1/2, always-as-type-1/2
redistribute-connected none ประกาศ Connected Networks
redistribute-static none ประกาศ Static Routes
metric-bgp auto Metric สำหรับ BGP Routes

Interface Template Parameters

Parameter Default Description
cost Auto (10Mbps=100, 1Gbps=10) Path Cost สำหรับ SPF Calculation
hello-interval 10s ช่วงเวลาส่ง Hello Packets
dead-interval 40s เวลาที่รอก่อนถือว่า Neighbor Down
priority 1 DR/BDR Election Priority (0-255)
networks - Network ที่จะโฆษณาผ่าน Interface
passive no ไม่ส่ง Hello (สำหรับ LAN Segments)

Verification Commands

Verify OSPF Status
# ========================================
# ตรวจสอบ OSPF Neighbors
# ========================================
/routing/ospf/neighbor/print
# ตรวจสอบ State ควรเป็น "Full"
# ตรวจสอบ Router ID, Address, Interface

# ========================================
# ตรวจสอบ OSPF Routes
# ========================================
/routing/ospf/route/print
# แสดง Routes ที่ได้จาก OSPF

# ========================================
# ตรวจสอบ LSDB (Link-State Database)
# ========================================
/routing/ospf/lsa/print
# แสดง LSAs ทั้งหมดใน LSDB

# ========================================
# ตรวจสอบ Interface State
# ========================================
/routing/ospf/interface/print
# แสดง DR, BDR, State ของแต่ละ Interface

# ========================================
# Debug OSPF Events
# ========================================
/routing/ospf/monitor
# Real-time monitoring

# ========================================
# ดู OSPF Routes ใน Routing Table
# ========================================
/ip/route/print where ospf=yes
/ip/route/print where route-tag~"ospf"

6. Multi-Area Design Best Practices

Enterprise Multi-Area Topology

Area 0 - Backbone ABR 1 ABR 2 Area 1 - HQ R1 R2 Area 2 - Branch (Stub Area) R3 R4 Area 3 - DMZ (NSSA - with ASBR) ASBR FW Area 1: Standard | Area 2: Stub | Area 3: NSSA (with External Routes)

ABR Configuration สำหรับ Multi-Area

ABR Configuration
# ========================================
# ABR Configuration (Router ID: 1.1.1.1)
# เชื่อม Area 0, Area 1 (Standard), Area 2 (Stub)
# ========================================

# OSPF Instance
/routing/ospf/instance
add name=ospf-enterprise \
    router-id=1.1.1.1 \
    distribute-default=if-installed-as-type-1

# Area 0 - Backbone
/routing/ospf/area
add instance=ospf-enterprise name=backbone area-id=0.0.0.0

# Area 1 - Standard (HQ)
/routing/ospf/area
add instance=ospf-enterprise name=area1-hq area-id=0.0.0.1

# Area 2 - Stub (Branch Office - ลด Routing Table)
/routing/ospf/area
add instance=ospf-enterprise name=area2-branch \
    area-id=0.0.0.2 \
    type=stub \
    no-summaries=yes \
    default-cost=10
    # no-summaries=yes = Totally Stubby (ไม่ส่ง Type 3 LSAs)

# Interface Templates
/routing/ospf/interface-template
# Area 0 Interfaces
add interfaces=ether1-core area=backbone \
    networks=10.0.0.0/30 cost=10

# Area 1 Interfaces
add interfaces=ether2-hq area=area1-hq \
    networks=10.0.1.0/30 cost=20

# Area 2 Interfaces (Branch - ต่อผ่าน VPN)
add interfaces=eoip-branch area=area2-branch \
    networks=10.0.2.0/30 cost=50 passive=no

# ========================================
# ตรวจสอบ Inter-Area Routes
# ========================================
/routing/ospf/route/print where area!=backbone
# ควรเห็น Routes จาก Area 1, 2

7. OSPF Authentication

OSPF Authentication ป้องกันการโจมตีแบบ Rogue Router และ LSA Spoofing โดยตรวจสอบความถูกต้องของ OSPF Packets

None

ไม่มี Authentication (Default)

⚠️ ไม่ปลอดภัย

Simple Password

ส่ง Password แบบ Clear Text

⚠️ สามารถดักฟังได้

Cryptographic (MD5/SHA)

เข้ารหัสด้วย MD5 หรือ HMAC-SHA

✓ แนะนำ

OSPF Authentication Configuration

MD5 Authentication
# ========================================
# MD5 Authentication (แนะนำ)
# ========================================

# วิธีที่ 1: กำหนดที่ Interface Template
/routing/ospf/interface-template
set [find name=template1] \
    authentication=simple \
    authentication-key="your-secret-password"

# วิธีที่ 2: ใช้ Key Chain (RouterOS v7.8+)
/routing/ospf/key-chain
add name=ospf-keys

/routing/ospf/key-chain/key
add chain=ospf-keys key-id=1 key="your-md5-secret-key"

# กำหนดให้ Interface Template
/routing/ospf/interface-template
set [find name=template1] \
    authentication=key-chain \
    key-chain=ospf-keys

# ========================================
# หมายเหตุ:
# - Router ทุกตัวใน Area ต้องใช้ Key เดียวกัน
# - เปลี่ยน Key ใหม่ทุก 3-6 เดือน
# - ใช้ Key ที่ซับซ้อน (12+ ตัวอักษร)
# ========================================

9. Troubleshooting OSPF

1

ปัญหา: OSPF Neighbor ไม่ขึ้น (State ค้างที่ Init/Down)

สาเหตุที่พบบ่อย:

  • • Hello/Dead Interval ไม่ตรงกัน
  • • MTU ไม่เท่ากัน (Mismatch)
  • • Firewall บล็อก OSPF (Protocol 89)
  • • Area ID ไม่ตรงกัน
  • • Authentication Key ผิด
  • • Router ID ซ้ำกัน
Troubleshooting Commands
# ตรวจสอบ Neighbor State
/routing/ospf/neighbor/print

# ตรวจสอบ Hello/Dead Interval
/routing/ospf/interface/print

# ตรวจสอบ MTU
/interface/print where name=ether1

# ตรวจสอบ Firewall
/ip/firewall/filter/print where protocol=89

# Debug OSPF Packets
/routing/ospf/monitor
2

ปัญหา: Routes ไม่ปรากฏใน Routing Table

วิธีตรวจสอบ:

# 1. ตรวจสอบ LSDB
/routing/ospf/lsa/print
# ควรเห็น Type 1, 2 LSAs

# 2. ตรวจสอบ OSPF Routes
/routing/ospf/route/print

# 3. ตรวจสอบว่า Route ติด Routing Table
/ip/route/print where ospf

# 4. ตรวจสอบ Network Statements
/routing/ospf/interface-template/print

# 5. ตรวจสอบ Filter
/routing/filter/rule/print
3

ปัญหา: DR/BDR Election ไม่ถูกต้อง

วิธีแก้:

# กำหนด Priority สูงสุดบน Router ที่ต้องการเป็น DR
/routing/ospf/interface-template
set [find interfaces=ether1] priority=255

# กำหนด Priority ต่ำบน Router ที่ไม่ต้องการเป็น DR/BDR
set [find interfaces=ether1] priority=0

# รีเซ็ต DR/BDR (ต้องรอ Dead Interval)
# หรือ Restart OSPF Process
/routing/ospf/instance/restart [find name=ospf-internal]

10. สรุป

สิ่งที่ได้เรียนรู้

OSPF Fundamentals

  • • Link-State Protocol ที่ใช้ Dijkstra SPF
  • • Convergence เร็ว วินาที
  • • Hierarchical Design ด้วย Areas

LSA Types

  • • Type 1,2 = Intra-Area
  • • Type 3,4 = Inter-Area (ABR)
  • • Type 5,7 = External (ASBR)

Area Types

  • • Standard, Stub, Totally Stub
  • • NSSA, Totally NSSA
  • • เลือกตามความต้องการ

MikroTik Configuration

  • • Instance → Area → Interface Template
  • • Authentication (MD5/SHA)
  • • Multi-Area Design