04: Routing Protocols: OSPF and BGP
Course: Networking - Security Advanced · Updated: October 2026 Literature: K&R chapter 5: intra-AS routing (OSPF), routing among ISPs (BGP) Related lab: Lab: Routing algorithms and protocols Prerequisite: Article 03: Routing algorithms
Learning objectives
After studying this article you can:
- explain why the Internet uses hierarchical routing with Autonomous Systems (AS)
- describe how OSPF works: link-state advertisements, areas, authentication
- describe how BGP works: eBGP/iBGP, path attributes, route selection, policy
- explain hot-potato routing and why policy is more important than performance between ASes
- explain how BGP hijacking works and how RPKI helps against it
1. Why hierarchical routing?
Running Dijkstra over all routers on the Internet is impossible:
- Scale: there are billions of destinations, and the forwarding tables and link-state floods would be enormous
- Administrative autonomy: every network (ISP, company, university) wants to control its own routing and keep its topology private
The solution is to group routers into Autonomous Systems (AS), each identified by an AS number (ASN). For example, SURF (the Dutch research network) is AS1103 and Google is AS15169.
| Intra-AS routing (IGP) | Inter-AS routing (EGP) | |
|---|---|---|
| Scope | Inside one organisation | Between organisations |
| Goal | Performance (shortest path) | Policy (who may carry whose traffic) |
| Protocols | OSPF, IS-IS, RIP, EIGRP | BGP (the only one) |
2. OSPF: Open Shortest Path First
OSPF (RFC 2328 for IPv4, RFC 5340 for IPv6 = OSPFv3) is an open link-state protocol.
How it works
- Neighbour discovery: routers send Hello packets (multicast 224.0.0.5) and form adjacencies.
- Flooding: every router floods Link-State Advertisements (LSAs) about its links to all routers in the area.
- Database: every router builds the same Link-State Database (LSDB), which is a map of the area.
- Computation: every router runs Dijkstra to compute the shortest-path tree with itself as the root.
- Refresh: LSAs are re-flooded when something changes, and at least every 30 minutes.
OSPF messages are carried directly over IP (protocol number 89), not over TCP or UDP.
Features
- Link costs are set by the administrator. By default the cost is based on bandwidth (reference bandwidth / interface bandwidth).
- Equal-Cost Multi-Path (ECMP): traffic can be spread over several paths with the same cost.
- Authentication: all OSPF messages can be authenticated. Modern implementations use HMAC-SHA.
- Hierarchy with areas:
1 2 3 4 5 6 7 8 9 10 | |
- Internal routers: know the full topology of their own area only
- Area Border Routers (ABR): summarise routes between an area and the backbone
- Backbone (area 0): connects all the areas
- AS Boundary Router (ASBR): connects to other ASes
IS-IS is OSPF's "sibling". It is also a link-state protocol and is popular with large ISPs, because it is independent of IP and scales very well.
3. BGP: Border Gateway Protocol
BGP (version 4, RFC 4271) is the glue that holds the Internet together. It gives each AS a way to:
- learn which destination prefixes (e.g.
145.18.0.0/16) are reachable through its neighbouring ASes (eBGP, external BGP) - spread that reachability information to all routers inside the AS (iBGP, internal BGP)
- choose "good" routes based on reachability and policy
- announce its own prefixes to the rest of the Internet
3.1 BGP sessions
- BGP routers ("BGP speakers") exchange messages over TCP port 179.
- Message types:
OPEN,UPDATE(announce a new path or withdraw an old one),KEEPALIVE,NOTIFICATION(errors, closing the session). - BGP is a path-vector protocol. It doesn't advertise a cost. It advertises the full list of ASes a route passes through.
3.2 Path attributes
A BGP route = prefix + attributes. The most important attributes are:
| Attribute | Meaning |
|---|---|
| AS-PATH | The list of ASes the announcement has passed through, e.g. AS3356 AS1103. It is used for loop detection: a router rejects a route that already contains its own ASN. |
| NEXT-HOP | The IP address of the router interface where the path to the next AS begins |
| LOCAL-PREF | A local preference (set by policy inside the AS). Higher is better. |
| MED | Multi-Exit Discriminator: a hint to the neighbour about which entry point to use |
| COMMUNITY | Tags used to signal policy (e.g. "don't export to peers" or "blackhole this prefix") |
3.3 Route selection
When a router has several routes to the same prefix, it applies these rules in order:
- Highest LOCAL-PREF (policy decision)
- Shortest AS-PATH
- Closest NEXT-HOP router: hot-potato routing
- Other tie-breakers (MED, eBGP over iBGP, lowest router ID, ...)
Hot-potato routing: get the traffic out of your own AS as quickly (cheaply) as possible, no matter how long the rest of the path is. Each AS optimises selfishly, which makes Internet paths asymmetric.
3.4 Policy: the business relationships
ASes have business relationships that decide what they announce:
- Customer → provider: the customer pays the provider for transit to the whole Internet.
- Peer ↔ peer: two ASes exchange traffic between their own customers for free (often at an Internet Exchange such as AMS-IX in Amsterdam).
Typical rule (valley-free routing): an AS does not announce routes it learned from one peer or provider to another peer or provider. If it did, it would carry other people's traffic for free.
1 2 3 4 5 6 7 | |
3.5 Anycast
The same prefix can be announced from many places. BGP then sends each user to the "nearest" copy. This is used by the DNS root servers, public resolvers (1.1.1.1, 8.8.8.8, 9.9.9.9) and CDNs.
4. How BGP has evolved
| Topic | Status in 2026 |
|---|---|
| Size | The global routing table holds more than 1 million IPv4 prefixes and more than 200,000 IPv6 prefixes, and it keeps growing. |
| RPKI / Route Origin Validation | Most large networks now drop RPKI-invalid routes. Most of the IPv4 address space is covered by ROAs. |
| ASPA | Autonomous System Provider Authorization. It checks whether the AS-PATH is plausible, which protects against route leaks. It is being standardised in the IETF and implemented in router and validator software. |
| BGP roles (RFC 9234) | Routers configure the relationship (provider/customer/peer) explicitly, so leaks are detected automatically. |
| BGP inside data centres | eBGP is the IGP of choice in large data centres (RFC 7938). |
| BGP Flowspec | Distributes firewall rules through BGP to stop DDoS traffic quickly. |
5. Summary
- The Internet is a network of Autonomous Systems. OSPF/IS-IS route inside an AS, and BGP routes between ASes.
- OSPF is link-state: LSAs, LSDB, Dijkstra, areas and authentication.
- BGP is path-vector: AS-PATH, NEXT-HOP, LOCAL-PREF, hot-potato routing and policy.
- BGP was designed on trust. That is the root of its security problems.
References
Videos
- Jim Kurose: OSPF and BGP (chapter 5 video lectures): https://gaia.cs.umass.edu/kurose_ross/lectures.php
- Cloudflare: "How the Internet works: BGP" (Cloudflare TV and blog videos): https://www.cloudflare.com/learning/security/glossary/what-is-bgp/
- Computerphile: "BGP: The Internet's Address Book" / "Facebook's Outage": https://www.youtube.com/@Computerphile
- NANOG: conference talks on BGP and RPKI: https://www.youtube.com/@TeamNANOG
- Jeremy's IT Lab: OSPF (CCNA course): https://www.youtube.com/@JeremysITLab
Articles
- Cloudflare Learning Center, What is BGP? and What is an autonomous system?: https://www.cloudflare.com/learning/security/glossary/what-is-bgp/
- Cloudflare blog, Understanding how Facebook disappeared from the Internet (2021): https://blog.cloudflare.com/october-2021-facebook-outage/
- Geoff Huston (APNIC), BGP in 2025 and earlier yearly reports: https://blog.apnic.net/
- RIPE NCC Academy, free online courses on BGP and RPKI: https://academy.ripe.net/
- AMS-IX, how an Internet Exchange works: https://www.ams-ix.net/
Standards
- RFC 2328: OSPF Version 2 · RFC 5340: OSPF for IPv6
- RFC 4271: A Border Gateway Protocol 4 (BGP-4)
- RFC 6480 / 6811: RPKI and BGP Prefix Origin Validation
- RFC 9234: Route Leak Prevention and Detection Using Roles in UPDATE and OPEN Messages
Tools
- bgp.tools and bgp.he.net: look up ASNs, prefixes and peers: https://bgp.tools/, https://bgp.he.net/
- RIPEstat: routing history of any prefix: https://stat.ripe.net/
- Looking glasses (e.g. RIPE RIS, Route Views): see BGP from other networks' point of view: https://www.routeviews.org/
- FRRouting / BIRD: open-source routing software for labs: https://frrouting.org/, https://bird.network.cz/
Cyber Security
Why it matters
BGP has no built-in authentication of the information it carries. Any AS can announce any prefix, and its neighbours will usually believe it. The result:
| Incident | What happened |
|---|---|
| Pakistan Telecom vs YouTube (2008) | Pakistan tried to block YouTube nationally by announcing a more specific YouTube prefix. The announcement leaked to the world and YouTube was unreachable worldwide for about 2 hours. |
| China Telecom (2010) | About 15% of Internet prefixes, including US government and military networks, were briefly routed through China. |
| Amazon Route 53 / MyEtherWallet (2018) | Attackers hijacked Amazon DNS prefixes, redirected users to a fake cryptocurrency wallet site and stole about $150,000. |
| Facebook outage (2021) | Not an attack: a configuration error withdrew Facebook's own BGP routes. Facebook, Instagram and WhatsApp were offline for about 6 hours. It showed how fragile BGP is. |
| Celer Bridge / KLAYswap (2022) | BGP hijacks were used to steal cryptocurrency by redirecting traffic to look-alike servers. |
Types of BGP attacks and errors:
- Prefix hijack: announcing someone else's prefix
- Sub-prefix hijack: announcing a more specific prefix (e.g. a /24 inside a /16). Longest-prefix match makes it win everywhere.
- Route leak: re-announcing routes against policy (e.g. a customer announces the routes of provider A to provider B)
- AS-path manipulation: forging the path to make a route look legitimate
- Session attacks: TCP RST or SYN floods against port 179 (see article 01)
Inside an organisation, OSPF without authentication can be attacked by anyone with access to a routing segment. They can inject LSAs and redirect traffic.
How to make it safe
- Sign your prefixes with RPKI: create ROAs (Route Origin Authorizations) at your Regional Internet Registry (in Europe: RIPE NCC). A ROA states "prefix X may be originated by AS Y".
- Run Route Origin Validation (ROV): drop RPKI-invalid routes on your routers, using a validator such as Routinator (NLnet Labs, a Dutch organisation) or rpki-client.
- Filter strictly: accept only the prefixes your customers are entitled to announce (IRR-based prefix lists,
bgpq4). Set max-prefix limits per session. - Join MANRS: Mutually Agreed Norms for Routing Security, a set of four actions: filtering, anti-spoofing, coordination and global validation.
- Protect BGP sessions: use TCP-AO or at least the TCP MD5 option, GTSM (TTL security, RFC 5082), and allow port 179 only from known neighbours.
- Prepare for ASPA and BGP roles to detect route leaks.
- Monitor your prefixes: use services that alert you when someone else announces your address space (e.g. BGPalerter, Cloudflare Radar route-leak detection, RIPEstat).
- OSPF: use HMAC-SHA authentication, passive interfaces and area design to limit the damage.
- Assume BGP can be hijacked: always use TLS with certificate validation (and DNSSEC), so that a redirected user gets a certificate error instead of a fake site.
Extra references
- MANRS, Mutually Agreed Norms for Routing Security: https://manrs.org/
- Cloudflare, Is BGP safe yet? (test whether your ISP validates RPKI): https://isbgpsafeyet.com/
- RIPE NCC, RPKI documentation: https://www.ripe.net/manage-ips-and-asns/resource-management/rpki/
- NLnet Labs Routinator: https://nlnetlabs.nl/projects/routing/routinator/
- BGPalerter (open-source hijack monitoring): https://github.com/nttgin/BGPalerter
- Cloudflare Radar, Routing (live hijack and leak detection): https://radar.cloudflare.com/routing
- NIST SP 800-189: Resilient Interdomain Traffic Exchange: BGP Security and DDoS Mitigation