RIP, OSPF & BGP
By the end of this lesson you will be able to explain what an autonomous system is, compare RIP, OSPF and BGP, calculate OSPF costs, follow how BGP picks and rejects paths, and choose between routes from different protocols using administrative distance.
The last lesson gave you the algorithms. This lesson shows the real protocols built on them, the ones running inside college networks, company networks and the internet core.
Autonomous systems and two kinds of routing
The internet is made of tens of thousands of autonomous systems (AS). An AS is a network, or group of networks, run by one organisation with one routing policy. Each ISP, large company, cloud provider and university network can be an AS. Each has a number called an ASN.
This splits routing into two jobs:
- Interior gateway protocols (IGPs) route inside one AS. The goal is the fastest path. Examples: RIP, OSPF, IS-IS, EIGRP.
- Exterior gateway protocols (EGPs) route between ASes. The goal is to follow business policy. The only one in use is BGP.
Think of Indian Railways zones. Inside the Northern Railway zone, staff choose the quickest route between stations (IGP). Between zones, routes follow agreements between the zones (EGP).
RIP: the simple distance vector protocol
RIP (Routing Information Protocol; version 2 is RFC 2453) is the classic distance vector protocol.
- Metric: hop count (number of routers to cross). Every link counts 1, whatever its speed.
- Maximum: 15 hops. A metric of 16 means unreachable, which stops count to infinity quickly.
- Sends its whole table to neighbours every 30 seconds, using UDP port 520.
- Uses split horizon and poison reverse.
RIP is easy to understand, which is why it is taught. But hop count ignores speed: RIP prefers a path with two slow links over one with three fast links. And it converges slowly. Today you will mostly meet it in labs and exams.
paths = {
"via fibre": [1000, 1000, 1000], # link speeds in Mbps, 3 hops
"via old DSL": [8, 8], # 2 hops
}
for name, links in paths.items():
print(f"{name:<12} RIP metric = {len(links)} hops")
best = min(paths, key=lambda p: len(paths[p]))
print("RIP chooses:", best)via fibre RIP metric = 3 hops via old DSL RIP metric = 2 hops RIP chooses: via old DSL
RIP picks the slow path because it has fewer hops. OSPF fixes this.
OSPF: link state for real networks
OSPF (Open Shortest Path First; OSPFv2 for IPv4 is RFC 2328, OSPFv3 for IPv6 is RFC 5340) is the most widely used open IGP.
- Each router floods LSAs describing its links, builds the full map, and runs Dijkstra.
- Neighbours are found and checked with Hello packets (by default every 10 seconds on Ethernet; a neighbour is declared down after 40 seconds of silence).
- OSPF runs directly over IP (protocol number 89), not over TCP or UDP.
- Updates are sent only when something changes, plus a slow refresh, so it is quiet and fast to converge.
OSPF cost depends on bandwidth: cost = reference bandwidth / interface bandwidth. On Cisco routers the default reference is 100 Mbps, and costs below 1 are rounded up to 1. That default makes 100 Mbps, 1 Gbps and 10 Gbps links all cost 1, so administrators usually raise the reference bandwidth.
def ospf_cost(link_mbps, reference_mbps=100):
return max(1, reference_mbps // link_mbps)
links = [10, 100, 1000, 10000]
print("Reference 100 Mbps: ", [ospf_cost(b) for b in links])
print("Reference 100000 Mbps:", [ospf_cost(b, 100000) for b in links])
fibre = 3 * ospf_cost(1000, 100000)
dsl = 2 * ospf_cost(8, 100000)
print("Fibre path cost:", fibre, " DSL path cost:", dsl)Reference 100 Mbps: [10, 1, 1, 1] Reference 100000 Mbps: [10000, 1000, 100, 10] Fibre path cost: 300 DSL path cost: 25000
With a sensible reference, the three fast fibre links (300) clearly beat the two slow DSL links (25,000).
Areas. Flooding every LSA to every router does not scale to thousands of routers. OSPF splits a large network into areas. All areas connect to the backbone, area 0. Routers inside an area know their own area in detail but see other areas only as summaries. A router at the edge of an area is an ABR (area border router).
On Ethernet segments OSPF elects a DR (designated router) and a BDR (backup) so that routers exchange LSAs through the DR instead of with every other router.
Here is a minimal OSPF set-up on a Cisco router, to give you a feel for it:
router ospf 1
router-id 1.1.1.1
auto-cost reference-bandwidth 100000
network 10.20.30.0 0.0.0.255 area 0The 0.0.0.255 is a wildcard mask, the inverse of a subnet mask.
Other IGPs in brief
- IS-IS: another link state protocol, popular inside large ISP networks.
- EIGRP: an advanced distance vector protocol created by Cisco, later published as RFC 7868. Common in Cisco-only networks.
BGP: the protocol that runs the internet
BGP (Border Gateway Protocol, version 4 in RFC 4271) connects autonomous systems. When an Indian ISP exchanges traffic with another ISP, a cloud provider or an internet exchange point, BGP decides which routes are shared and used.
- BGP is a path vector protocol. Each route carries the AS_PATH: the list of ASNs it has passed through.
- BGP neighbours (called peers) connect over TCP port 179.
- eBGP runs between different ASes; iBGP runs between routers inside the same AS.
- Route choice is driven by policy: which customer pays, which link is cheaper, which partner is trusted. Among other rules, shorter AS paths are preferred when policy does not decide.
- If a router sees its own ASN in an AS_PATH, it rejects the route. That simple rule prevents loops.
Here is a simplified model of the AS path rules. The ASNs are from the range reserved for documentation (64496 to 64511, RFC 5398).
MY_ASN = 64500
offers = {
"203.0.113.0/24 via peer A": [64501, 64510],
"203.0.113.0/24 via peer B": [64502, 64505, 64510],
"203.0.113.0/24 via peer C": [64503, 64500, 64510],
}
valid = {}
for name, path in offers.items():
if MY_ASN in path:
print(f"{name}: rejected, loop (contains my AS {MY_ASN})")
else:
valid[name] = path
print(f"{name}: AS_PATH {path}, length {len(path)}")
best = min(valid, key=lambda n: len(valid[n]))
print("Best (shortest AS_PATH):", best)203.0.113.0/24 via peer A: AS_PATH [64501, 64510], length 2 203.0.113.0/24 via peer B: AS_PATH [64502, 64505, 64510], length 3 203.0.113.0/24 via peer C: rejected, loop (contains my AS 64500) Best (shortest AS_PATH): 203.0.113.0/24 via peer A
Real BGP checks several attributes before AS path length, such as local preference set by the administrator. But loop rejection and "shorter is better" capture the core idea.
Administrative distance
A router may learn the same prefix from several protocols. Which does it trust? Cisco routers use administrative distance (AD): lower is more trusted. Other vendors use similar "preference" values with different numbers.
| Source | Cisco default AD |
|---|---|
| Connected interface | 0 |
| Static route | 1 |
| eBGP | 20 |
| EIGRP (internal) | 90 |
| OSPF | 110 |
| IS-IS | 115 |
| RIP | 120 |
| iBGP | 200 |
AD is compared first, between protocols. The metric (hop count, OSPF cost) is compared only between routes from the same protocol. Longest prefix match still comes before both: a /24 from RIP beats a /16 from OSPF, because they are different prefixes.
Common mistakes
- Saying BGP chooses the fastest path. BGP chooses by policy first; speed is not one of its normal attributes.
- Comparing metrics across protocols. An OSPF cost of 20 and a RIP hop count of 2 cannot be compared; AD decides.
- Forgetting that RIP's 16 means unreachable. A RIP network can be at most 15 hops wide.
- Thinking OSPF uses TCP. OSPF runs directly on IP (protocol 89); BGP uses TCP port 179; RIP uses UDP port 520.
Key points
- An autonomous system is a network under one routing policy, identified by an ASN.
- IGPs route inside an AS (RIP, OSPF, IS-IS, EIGRP); BGP routes between ASes.
- RIP: distance vector, hop count, 15 hops maximum, updates every 30 seconds over UDP 520.
- OSPF: link state, Dijkstra, cost from bandwidth, areas around backbone area 0.
- BGP: path vector over TCP 179, policy-driven, rejects paths that contain its own ASN.
- Administrative distance picks between protocols; metric picks within one protocol.
Exercise
Use ospf_cost with a reference of 10,000 Mbps to compare two paths from Pune to Mumbai: path 1 has links of 1,000, 1,000 and 100 Mbps; path 2 has links of 10,000, 10,000, 10,000 and 1,000 Mbps. Which path does OSPF choose, and which would RIP choose?
Show solution
def ospf_cost(link_mbps, reference_mbps=10000):
return max(1, reference_mbps // link_mbps)
path1 = [1000, 1000, 100]
path2 = [10000, 10000, 10000, 1000]
for name, p in [("Path 1", path1), ("Path 2", path2)]:
print(name, "OSPF cost", sum(ospf_cost(b) for b in p), "RIP hops", len(p))Path 1 OSPF cost 120 RIP hops 3 Path 2 OSPF cost 13 RIP hops 4
OSPF picks path 2 (cost 13), because its links are much faster. RIP would pick path 1, because it has fewer hops, even though one of its links is only 100 Mbps.
Quick Quiz
1. Which protocol is used to route between different autonomous systems on the internet?
BGP is the exterior gateway protocol of the internet.
2. What metric does RIP use?
RIP counts routers, with 16 meaning unreachable.
3. In OSPF, what is area 0?
All OSPF areas must connect to backbone area 0.
4. A router learns 10.1.0.0/16 from OSPF (AD 110) and from RIP (AD 120). Which route is installed?
Same prefix, so the lower administrative distance (OSPF) wins.
Learn Computer Networks with a mentor
Free lessons get you started. Our Computer Science Fundamentals training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
