SkillPilotAI

BGP and internet routing

BGP (Border Gateway Protocol) is the routing protocol that glues the internet together: thousands of independent networks (autonomous systems) use it to tell each other which IP prefixes they can reach. Knowing how it picks a path, and how it can be hijacked, explains many real internet outages.

51.1 Why is it used?

  • The internet is a network of thousands of independent networks; each one sets its own routing rules.
  • One such network is an autonomous system (AS), identified by a unique AS number (ASN).
  • Your request from a phone goes: your ISP's AS -> a transit provider's AS -> the content company's AS -> its server.
  • BGP is how these ASes announce their IP prefixes to each other and choose paths between them.
  • Every AS announces its own prefixes, e.g. Google's AS15169 announces 8.8.8.0/24.

51.2 AS numbers (ASN)

PointDetail
Who gives themRegional registries: APNIC (Asia), ARIN, RIPE...
SizeStarted as 16 bit; 32 bit ASNs are common today
Private range64512-65534 (labs, internal networks)
Prefix announceAS15169 -> 8.8.8.0/24

51.3 How it works: eBGP and iBGP

  • Border routers of two ASes open a BGP session over TCP port 179.
  • The owner AS sends an UPDATE: "prefix 192.168.71.128/26 is with me".
  • Each AS that passes the route to another AS prepends its own ASN to the AS path.
  • eBGP (external BGP): a session between two different ASes; it adds its own ASN to the AS path.
  • iBGP (internal BGP): a session between routers of the same AS (a big ISP has many border routers); it does NOT change the AS path.
  • Because iBGP does not change the AS path, it needs a full mesh or route reflectors to stop loops.
A BGP UPDATE travelling across ASes
  1. 1.AS 65004 (content) announces 192.168.71.128/26 over TCP 179

  2. 2.AS 65002 (transit) receives it, AS path becomes 65002 65004

  3. 3.Your ISP's R1-A (AS 65001) learns it over eBGP

  4. 4.R1-A gives it to R1-B over iBGP: AS path unchanged

  5. 5.R1-B sends it to customer AS 65009 over eBGP: AS path 65001 65002 65004

eBGP prepends the ASN, iBGP keeps the AS path as it is

51.4 BGP path selection

When R1 has two routes for the same prefix (65002 65004 vs 65003 65005 65004), BGP runs these checks in order:

OrderAttributeWhich wins
1Local preferenceHigher wins (your own policy)
2AS path lengthShorter wins
3OriginIGP > EGP > incomplete
4MEDLower wins
5eBGP vs iBGPExternal route preferred
6IGP cost, router IDTie breakers
How R1 picks the best route
  1. 1.Route A via AS 65002: AS path 65002 65004

  2. 2.Route B via AS 65003: AS path 65003 65005 65004

  3. 3.Check 1, local preference: equal (default)

  4. 4.Check 2, AS path: route A is shorter

  5. 5.Best = route A; traffic goes R1 -> AS 65002 -> AS 65004 -> server

  6. 6.Set local-preference 200 on route B and B wins instead (checked first)

51.5 BGP hijacks, route leaks and protection

  • Hijack: an AS (by mistake or deliberately) announces a more specific part of someone else's prefix, e.g. 192.168.71.128/27 inside the real /26.
  • Routers always follow longest prefix match, so traffic goes to the hijacker instead of the real server.
  • Real example: in 2008 a more specific announcement from Pakistan Telecom took YouTube down globally for around two hours.
  • Route leak: an AS announces routes learned from one provider to another provider, and traffic starts flowing through a small network.
  • Prefix filters: a customer is only allowed to announce its own prefixes.
  • RPKI + ROA: a signed ROA says which AS may originate which prefix; routers drop invalid origins.
Hijack with a more specific prefix
  1. 1.AS 65066 (hijacker) announces 192.168.71.128/27

  2. 2.The fake UPDATE goes through AS 65003 to your ISP (AS path 65003 65066)

  3. 3.R1 now has /26 (real) and /27 (fake)

  4. 4.Longest prefix match picks the /27

  5. 5.Your traffic reaches the hijacker, not the real server

  6. 6.Fix: AS 65003 filters its customer's routes; the /27 disappears

51.6 Practical: AS lookup with whois

Terminal
whois 8.8.8.8 | grep -E \  '^(NetRange|CIDR|NetName|Organization)'whois AS15169 | grep -E \  '^(ASNumber|ASName|OrgName)'whois -h whois.radb.net 8.8.8.8
Output
$ whois 8.8.8.8 | grep -E ...NetRange:       8.8.8.0 - 8.8.8.255CIDR:           8.8.8.0/24NetName:        GOGLOrganization:   Google LLC (GOGL)$ whois AS15169 | grep -E ...ASNumber:       15169ASName:         GOOGLEOrgName:        Google LLC$ whois -h whois.radb.net 8.8.8.8route:          8.8.8.0/24origin:         AS15169descr:          Googlemnt-by:         MAINT-AS15169source:         RADBrpki-ov-state:  valid
  • whois on an IP gives the block (8.8.8.0/24) and its owner (Google LLC).
  • whois AS15169 gives the AS name GOOGLE.
  • The RADB route object shows origin AS15169 and rpki-ov-state valid: the announcement is correct as per RPKI.

51.7 Practical: ASNs over DNS and in a traceroute

Terminal
dig +short \  8.8.8.8.origin.asn.cymru.com TXTdig +short AS15169.asn.cymru.com TXTmtr -rwz -c 5 8.8.8.8
Output
$ dig +short 8.8.8.8.origin.asn.cymru.com TXT"15169 | 8.8.8.0/24 | US | arin | 2023-12-28"$ dig +short AS15169.asn.cymru.com TXT"15169 | US | arin | 2000-03-30 | GOOGLE - Google LLC, US"$ mtr -rwz -c 5 8.8.8.8HOST: 612aa994a95f          Loss%   Snt   Last   Avg  Best  Wrst StDev  1. AS???    172.17.0.1     0.0%     5    0.1   0.1   0.1   0.2   0.0  2. AS15169  8.8.8.8        0.0%     5   27.6  38.5  27.6  44.7   6.9
  • Team Cymru's DNS service returns the origin AS + prefix of an IP, and the AS name + registry.
  • mtr -z shows the AS number next to every hop (AS??? = private/unknown).
  • Docker Desktop's NAT hides the hops in between, but the last hop clearly shows AS15169.

51.8 Practical: FRRouting lab, eBGP sessions

Six FRR routers in Docker, each with a private ASN. R1's config and session summary:

Output
router bgp 65001 bgp router-id 1.1.1.1 no bgp ebgp-requires-policy neighbor 192.168.71.3 remote-as 65002 neighbor 192.168.71.11 remote-as 65003 address-family ipv4 unicast  redistribute connected
Output
$ vtysh -c 'show bgp summary'BGP router identifier 1.1.1.1, local AS number 65001 VRF default vrf-id 0Neighbor        V         AS   MsgRcvd   MsgSent   TblVer  InQ OutQ  Up/Down State/PfxRcd   PfxSnt Desc192.168.71.3    4      65002        10         9        7    0    0 00:00:30            4        7 N/A192.168.71.11   4      65003        10         9        7    0    0 00:00:27            5        7 N/ATotal number of neighbors 2
  • router bgp 65001 with two neighbours: AS 65002 and AS 65003.
  • FRR does not accept eBGP routes without a policy by default; the lab turns this off with no bgp ebgp-requires-policy.
  • PfxRcd = how many prefixes came from each neighbour.

51.9 Practical: best path, then local preference

Terminal
vtysh -c 'show ip bgp' | grep -A3 128/26vtysh -c 'show ip bgp 192.168.71.128/26'traceroute -n -q 1 -w 1 192.168.71.129
Output
$ vtysh -c 'show ip bgp' | grep -A3 128/26 *  192.168.71.128/26                    192.168.71.11                          0 65003 65005 65004 i *>                  192.168.71.3                           0 65002 65004 i$ vtysh -c 'show ip bgp 192.168.71.128/26'Paths: (2 available, best #2, table default)  65003 65005 65004    192.168.71.11 from 192.168.71.11 (3.3.3.3)      Origin IGP, valid, external  65002 65004    192.168.71.3 from 192.168.71.3 (2.2.2.2)      Origin IGP, valid, external, best (AS Path)$ traceroute -n -q 1 -w 1 192.168.71.129 1  192.168.71.3  0.013 ms 2  192.168.71.129  0.009 ms
  • *> marks the best path; the detail view shows the reason: best (AS Path).
  • The traceroute's first hop is 192.168.71.3 (R2), so traffic takes the shorter path.
Output
route-map PREF-B permit 10 set local-preference 200router bgp 65001 address-family ipv4 unicast  neighbor 192.168.71.11 route-map PREF-B in
Output
$ vtysh -c 'show ip bgp' | grep -A3 128/26 *> 192.168.71.128/26                    192.168.71.11                 200      0 65003 65005 65004 i *                   192.168.71.3                           0 65002 65004 i$ traceroute -n -q 1 -w 1 192.168.71.129 1  192.168.71.11  0.015 ms 2  192.168.71.27  0.008 ms 3  192.168.71.129  0.018 ms
  • The route map sets local preference 200 on routes from AS 65003.
  • The longer path 65003 65005 65004 becomes best because local preference is checked before AS path; traceroute now has three hops via R3 and R5.

51.10 Practical: hijack and the prefix-filter fix

Terminal
ip link add fake0 type dummyip addr add 192.168.71.129/27 dev fake0ip link set fake0 upvtysh -c 'conf t' -c 'router bgp 65066' \  -c 'address-family ipv4 unicast' \  -c 'network 192.168.71.128/27'
Output
$ vtysh -c 'show ip bgp' | grep -A3 '128/2' *  192.168.71.128/26                    192.168.71.11                          0 65003 65005 65004 i *>                  192.168.71.3                           0 65002 65004 i *> 192.168.71.128/27                    192.168.71.11                          0 65003 65066 i$ traceroute -n -q 1 -w 1 192.168.71.129 1  192.168.71.11  0.012 ms 2  192.168.71.129  0.016 msr6$ ip -br -4 addr show fake0fake0            UNKNOWN        192.168.71.129/27
  • R6 (AS 65066) puts 192.168.71.129 on a dummy interface and announces the /27.
  • R1 gets a new /27 entry with AS path 65003 65066; even 192.168.71.129 replies, but from the hijacker.
Output
ip prefix-list R6 deny 192.168.71.128/26 ge 26ip prefix-list R6 permit 0.0.0.0/0 le 32router bgp 65003 address-family ipv4 unicast  neighbor 192.168.71.43 prefix-list R6 in
Output
$ vtysh -c 'show ip bgp' | grep -A3 '128/2' *  192.168.71.128/26                    192.168.71.11                          0 65003 65005 65004 i *>                  192.168.71.3                           0 65002 65004 i$ traceroute -n -q 1 -w 1 192.168.71.129 1  192.168.71.3  0.012 ms 2  192.168.71.129  0.015 ms
  • The fix is on R3 (transit AS 65003), filtering routes from its customer R6.
  • deny 192.168.71.128/26 ge 26 blocks the /26 and every more specific prefix inside it; the second rule permits the rest.
  • The fake /27 is removed and traffic goes through R2 to the real server again.

51.11 Common mistakes

  • Myth: BGP picks the fastest path. Reality: it does not measure latency or bandwidth at all, only policy and attributes like AS path.
  • That is why Delhi-to-Delhi traffic sometimes goes via Singapore: business agreements make that the best route.
  • Thinking AS path decides first: local preference is checked before AS path.
  • Forgetting that a more specific prefix always wins, which is exactly what makes hijacks work.

51.12 Interview tips

  • eBGP vs iBGP: eBGP runs between different ASes and adds its own ASN to the AS path; iBGP runs inside one AS, keeps the AS path, so needs full mesh or route reflectors.
  • BGP runs over TCP port 179.
  • Path selection order to remember: local preference -> AS path -> origin -> MED -> eBGP over iBGP -> IGP cost / router ID.
  • Hijack = more specific prefix + longest prefix match; protection = prefix filters + RPKI/ROA.
  • Use whois and mtr -z to find the AS of any IP.