Thuta Learning
Computer Networking
IntermediateDevOps & Toolsbeginner

Routing နဲ့ Default Gateway

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Routing နဲ့ Default Gateway concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • Diagram ကို ဖတ်ပြီး packet/data ဘယ်လိုသွားလာသလဲ ခြေရာခံနိုင်ရန်
  • နမူနာ code ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်

နားလည်ထားရမယ့် အချက်

host တစ်ခုက packet တစ်ခုကို ပို့တော့မယ်ဆိုရင် သူ ဖြေရမယ့် ပထမဆုံး မေးခွန်းက 'ဒီ destination ဟာ ငါနဲ့ တူညီတဲ့ subnet ထဲမှာ ရှိသလား' ဆိုတာ ဖြစ်တယ်။ စစ်ဆေးနည်းက ရိုးရှင်းတယ် — ကိုယ့် IP နဲ့ destination IP နှစ်ခုစလုံးကို subnet mask နဲ့ bitwise AND လုပ်လိုက်ပြီး ရလာတဲ့ network အပိုင်း တူမတူ နှိုင်းယှဉ်တာ ဖြစ်တယ်။ တူရင် destination ဟာ တစ်ခုတည်းသော link ပေါ်မှာ ရှိလို့ router လုံးဝ မလိုဘူး — host က ARP နဲ့ destination ရဲ့ MAC address ကို မေးပြီး frame ကို တိုက်ရိုက် ပို့လိုက်ရုံပဲ။ မတူရင်တော့ ကိုယ်က ဘယ်လိုမှ တိုက်ရိုက် မရောက်နိုင်လို့ တစ်ယောက်ယောက်ကို အပ်လိုက်ရမယ် — ဒါက default gateway ရဲ့ တာဝန် ဖြစ်တယ်။

ဒီနေရာမှာ များသောအားဖြင့် မှားလေ့ရှိတဲ့ အချက်တစ်ခု ရှိတယ်။ gateway ဆီ ပို့တဲ့အခါ packet ရဲ့ destination IP ကို gateway ရဲ့ IP အဖြစ် မပြောင်းဘူး — IP header ထဲက destination က မူလအတိုင်း ကျန်နေပြီး၊ ပြောင်းသွားတာက frame ရဲ့ destination MAC address ပဲ ဖြစ်တယ်။ ဆိုလိုတာက 'gateway ဆီ ပို့တယ်' ဆိုတာ 'ဒီ frame ကို မင်းယူသွား' လို့ ပြောတာ ဖြစ်ပြီး 'မင်းဆီ သွားတာ' လို့ ပြောတာ မဟုတ်ဘူး။ router တစ်ခုစီက frame ကို ဖြည်၊ IP destination ကို ကြည့်၊ သူ့ routing table ကို ပြန်စစ်၊ ပြီးရင် နောက် hop ရဲ့ MAC နဲ့ frame အသစ် ဆောက်ပြီး ထပ်ပို့တယ်။ hop တစ်ခုဆိုတာ ဒီလုပ်ငန်းစဉ် တစ်ခေါက် ဖြစ်ပြီး၊ TTL တစ်ခု လျော့သွားတာလည်း ဒီနေရာမှာ ဖြစ်တယ်။

route အများကြီး တစ်ပြိုင်နက် ကိုက်ညီနေရင် ဘယ်ဟာ နိုင်မလဲ။ စည်းမျဉ်းက 'ပထမဆုံး ကိုက်တဲ့ဟာ' မဟုတ်ဘဲ 'အတိကျဆုံး ဟာ' ဖြစ်တယ် — prefix အရှည်ဆုံး route က နိုင်တယ်။ 10.20.30.5 ဆိုတဲ့ address ဟာ 10.20.30.0/24, 10.0.0.0/8 နဲ့ 0.0.0.0/0 သုံးခုစလုံးနဲ့ ကိုက်ပေမယ့် /24 က နိုင်တယ်။ default gateway ဆိုတာလည်း အထူးအရာ တစ်ခု မဟုတ်ဘဲ 0.0.0.0/0 — address အားလုံးနဲ့ ကိုက်ပြီး prefix အတိုဆုံး ဖြစ်လို့ ဘယ်တော့မှ ဦးစားပေး မခံရတဲ့ route တစ်ခုသာ ဖြစ်တယ်။ ဒါကြောင့် သီးသန့် route တစ်ခု ထည့်လိုက်ရင် သူက အလိုအလျောက် default ကို လွှမ်းမိုးသွားတယ်။

text
HOW A HOST DECIDES WHERE TO SEND A PACKET
-----------------------------------------
      packet for 10.20.30.5 leaves the application
                          |
                          v
        +----------------------------------+
        | collect EVERY route in the table |
        | whose network contains the dest  |
        +----------------------------------+
                          |
                          v
        +----------------------------------+
        | keep the LONGEST prefix          |
        | /24 beats /8 beats /0 (default)  |
        +----------------------------------+
                          |
            +-------------+-------------+
            |                           |
            v                           v
   route is on-link                route has a
   (my own subnet)                 next-hop gateway
            |                           |
            v                           v
   ARP for the DESTINATION       ARP for the GATEWAY
   frame carries its MAC         frame carries its MAC
            |                           |
            +-------------+-------------+
                          |
                          v
   IP addresses never change; the MAC addresses are
   rewritten at every single hop. that IS a hop.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

office VPN ချိတ်လိုက်တာနဲ့ internet ကုန် ပျောက်သွားတယ် ဆိုတဲ့ တိုင်ကြားချက်ကို စစ်ကြည့်ရအောင်။ VPN client အများစုက connect လုပ်တာနဲ့ routing table ထဲကို 0.0.0.0/0 route အသစ်တစ်ခုကို metric ပိုကောင်းတာနဲ့ ထည့်လိုက်တယ် — ဒါက traffic အားလုံးကို VPN tunnel ထဲ မောင်းသွင်းလိုက်တာ ဖြစ်ပြီး full-tunnel လို့ ခေါ်တယ်။ office firewall က ပြင်ပ internet ကို မထွက်ခွင့်ပြုရင် ရလဒ်က internet ပျောက်သွားတာ ဖြစ်တယ်။ ဒါက bug မဟုတ်ဘဲ longest-prefix match စည်းမျဉ်း အလုပ်လုပ်နေတာ ဖြစ်တယ်။

မှန်ကန်တဲ့ ဖြေရှင်းနည်းက split tunnelling ဖြစ်တယ် — 0.0.0.0/0 အစား office network ဖြစ်တဲ့ 10.0.0.0/8 အတွက်သာ VPN gateway ကို ညွှန်တဲ့ route ထည့်ပြီး ကျန် traffic အားလုံးက မူလ default gateway ဆီ ဆက်သွားစေတာ ဖြစ်တယ်။ /8 က /0 ထက် ရှည်လို့ office traffic က tunnel ထဲ ဝင်ပြီး၊ ကျန်တာက အိမ် router ကနေ တိုက်ရိုက် ထွက်တယ်။ ဒီလို ဖြေရှင်းတဲ့အခါ တွေ့တတ်တဲ့ နောက်ပြဿနာက address overlap ဖြစ်တယ် — အိမ်က WiFi က 192.168.1.0/24 သုံးပြီး office ကလည်း 192.168.1.0/24 သုံးထားရင် host က destination ဟာ local ဖြစ်တယ် လို့ ဆုံးဖြတ်ပြီး VPN ကို လုံးဝ မသုံးတော့ဘဲ ARP လုပ်လိုက်တယ်၊ ဒါကြောင့် office server ဆီ ဘယ်တော့မှ မရောက်တော့ဘူး။ ဒါကြောင့် office network တွေက 192.168.1.0/24 လိုမျိုး ခေတ်စားတဲ့ range တွေကို ရှောင်လေ့ ရှိတာ ဖြစ်တယ်။

အတူတူ စမ်းရေးကြည့်မယ်

python
import ipaddress

# A host's routing table. Order in the list is irrelevant: the rule is
# "longest prefix wins", not "first match wins".
ROUTES = [
    ("0.0.0.0/0",       "192.168.1.1",   "eth0"),   # default gateway
    ("192.168.1.0/24",  None,            "eth0"),   # directly connected
    ("10.0.0.0/8",      "192.168.1.254", "eth0"),   # office VPN
    ("10.20.30.0/24",   "192.168.1.253", "eth0"),   # a more specific carve-out
    ("127.0.0.0/8",     None,            "lo"),     # loopback
]

TABLE = [(ipaddress.ip_network(n), gw, dev) for n, gw, dev in ROUTES]
MY_IP = ipaddress.ip_interface("192.168.1.42/24")


def lookup(dest):
    """Longest-prefix match: every route whose network contains the
    destination is a candidate; the one with the biggest prefix wins."""
    ip = ipaddress.ip_address(dest)
    matches = [r for r in TABLE if ip in r[0]]
    return max(matches, key=lambda r: r[0].prefixlen), len(matches)


print("my address     :", MY_IP)
print("my subnet      :", MY_IP.network)
print()
print("destination      chosen route      next hop         action")
print("---------------  ----------------  ---------------  ----------------")

for dest in ["192.168.1.7", "10.20.30.5", "10.99.0.1",
             "203.0.113.9", "127.0.0.1"]:
    (net, gw, dev), n = lookup(dest)
    if dev == "lo":
        action = "never leaves host"
    elif gw is None:
        action = "ARP for the host"
    else:
        action = "ARP for the gateway"
    print("%-15s  %-16s  %-15s  %s"
          % (dest, str(net), gw or "direct (" + dev + ")", action))

print()
# The AND-with-mask test the host really performs for the local case.
mask = int(MY_IP.network.netmask)
for dest in ["192.168.1.7", "203.0.113.9"]:
    d = int(ipaddress.ip_address(dest))
    same = (d & mask) == (int(MY_IP.ip) & mask)
    print("%-13s AND mask -> %-15s same subnet as me? %s"
          % (dest, ipaddress.ip_address(d & mask), same))

(net, gw, dev), n = lookup("10.20.30.5")
print()
print("10.20.30.5 matched %d routes; /%d won over the less specific ones"
      % (n, net.prefixlen))
You should see
my address     : 192.168.1.42/24
my subnet      : 192.168.1.0/24

destination      chosen route      next hop         action
---------------  ----------------  ---------------  ----------------
192.168.1.7      192.168.1.0/24    direct (eth0)    ARP for the host
10.20.30.5       10.20.30.0/24     192.168.1.253    ARP for the gateway
10.99.0.1        10.0.0.0/8        192.168.1.254    ARP for the gateway
203.0.113.9      0.0.0.0/0         192.168.1.1      ARP for the gateway
127.0.0.1        127.0.0.0/8       direct (lo)      never leaves host

192.168.1.7   AND mask -> 192.168.1.0     same subnet as me? True
203.0.113.9   AND mask -> 203.0.113.0     same subnet as me? False

10.20.30.5 matched 3 routes; /24 won over the less specific ones

၅ မိနစ် စမ်းကြည့်

ROUTES ထဲကို ('10.20.30.0/28', '192.168.1.252', 'eth0') ကို ထည့်ပြီး 10.20.30.5 ဟာ ဘယ် route ကို ရွေးလဲ ကြည့်ပါ။ ပြီးရင် 0.0.0.0/0 route ကို ဖျက်လိုက်ပြီး 203.0.113.9 အတွက် ဘာဖြစ်လဲ၊ တကယ့် host တစ်ခုဆိုရင် ဘာ error ပြမလဲ စဉ်းစားပါ။

သတိလေးတစ်ချက်

gateway ဆီ ပို့တဲ့အခါ destination IP ကို gateway ရဲ့ IP အဖြစ် ပြောင်းတယ် လို့ ထင်တာ။ ပြောင်းတာက frame ရဲ့ MAC address သာ ဖြစ်ပြီး IP header က မထိဘဲ ကျန်တယ်။

route ကို 'ပထမဆုံး ကိုက်တာ နိုင်တယ်' လို့ ထင်ပြီး routing table ကို အစီအစဉ်လိုက် စဉ်းစားတာ။ table ရဲ့ အစီအစဉ်က လုံးဝ အရေးမကြီးဘဲ prefix အရှည်သာ အရေးကြီးတယ်။

RFC 4632 - Classless Inter-domain Routing (CIDR)Computer Networking

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • gateway ဆီ ပို့တဲ့အခါ destination IP ကို gateway ရဲ့ IP အဖြစ် ပြောင်းတယ် လို့ ထင်တာ။ ပြောင်းတာက frame ရဲ့ MAC address သာ ဖြစ်ပြီး IP header က မထိဘဲ ကျန်တယ်။
  • route ကို 'ပထမဆုံး ကိုက်တာ နိုင်တယ်' လို့ ထင်ပြီး routing table ကို အစီအစဉ်လိုက် စဉ်းစားတာ။ table ရဲ့ အစီအစဉ်က လုံးဝ အရေးမကြီးဘဲ prefix အရှည်သာ အရေးကြီးတယ်။
  • နမူနာ code ကို production network ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test environment တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

ROUTES ထဲကို ('10.20.30.0/28', '192.168.1.252', 'eth0') ကို ထည့်ပြီး 10.20.30.5 ဟာ ဘယ် route ကို ရွေးလဲ ကြည့်ပါ။ ပြီးရင် 0.0.0.0/0 route ကို ဖျက်လိုက်ပြီး 203.0.113.9 အတွက် ဘာဖြစ်လဲ၊ တကယ့် host တစ်ခုဆိုရင် ဘာ error ပြမလဲ စဉ်းစားပါ။

You'll know it worked when: my address : 192.168.1.42/24 my subnet : 192.168.1.0/24 destination chosen route next hop action --------------- ---------------- --------------- ---------------- 192.168.1.7 192.168.1.0/24 direct (eth0) ARP for the host 10.20.30.5 10.20.30.0/24 192.168.1.253 ARP for the gateway 10.99.0.1 10.0.0.0/8 192.168.1.254 ARP for the gateway 203.0.113.9 0.0.0.0/0 192.168.1.1 ARP for the gateway 127.0.0.1 127.0.0.0/8 direct (lo) never leaves host 192.168.1.7 AND mask -> 192.168.1.0 same subnet as me? True 203.0.113.9 AND mask -> 203.0.113.0 same subnet as me? False 10.20.30.5 matched 3 routes; /24 won over the less specific ones

Routing နဲ့ Default Gateway | Thuta Learning