When an IPv4 router learns one prefix from a static route, OSPF and BGP, which wins, and how does administrative distance differ from a metric?
answer
- longest prefix comes first
- a trust ranking between sources
- metrics compare within one protocol
- lower preference value wins
- the numbers are vendor defaults
basics
~20 sOnly routes for the same prefix compete. Among them the router keeps the source with the best administrative distance, an implementation-defined trust ranking where lower wins; a metric ranks routes only within one protocol. With common defaults the static route wins.
solid answer
~50 sFirst comes prefix length: a longer matching prefix beats a shorter one whatever its source, so what follows applies only to routes for the *same* prefix. Among those, metrics are no use, because an OSPF cost and a BGP path share no unit. So implementations rank **sources**: RFC 1812 section 5.2.4.4 describes an *administrative preference*, an integer from 0 to 255 where lower is preferred and 255 means never use; most implementations call it **administrative distance**. One widely used set of defaults ranks connected 0, static 1, external BGP 20, OSPF 110 and internal BGP 200, so the static route wins. Those numbers are vendor choices, not protocol rules. The **metric** then chooses only among routes from the winning protocol. Losing routes stay in their own protocol's table and take over if the winner disappears.
go deeper
Remember the order of the decision: the most specific prefix first, then the most trusted source, then the best metric inside one protocol.
Explain why metrics cannot be compared across protocols, what the 0 to 255 preference scale means including 255, and that the per-source numbers are implementation defaults.
Diagnose why a route lost by walking prefix, preference and metric in order, and explain how mismatched preferences combined with redistribution produce loops or suboptimal paths.
Decide when to override default preferences across a network and how to keep overrides consistent, so no router's local choice contradicts what its neighbours assume.
## One prefix, several sources Picture router R holding three candidate routes for `203.0.113.0/24` at the same moment: a **static** route an operator configured toward `192.0.2.9`, a route learned from **OSPF** (an interior routing protocol) with cost 20, and a route learned from an external **BGP** neighbour. Only one of them can drive forwarding for that prefix. Choosing it takes three filters applied in a fixed order. ## Filter 1: prefix length decides before anything else RFC 1812, the IPv4 router requirements, defines next-hop selection as a series of **pruning rules** applied to the forwarding table. *Basic Match* keeps the routes whose prefix covers the destination; *Longest Match* keeps only those with the longest prefix length. So if OSPF also carried `203.0.113.128/25`, a packet to `203.0.113.200` would follow the OSPF `/25`, however the three `/24` routes rank. Source preference never lets a shorter prefix beat a longer one. Everything below applies only to routes for **exactly the same prefix and length**. ## Filter 2: which source to trust An OSPF cost of 20 and a BGP path through three autonomous systems cannot be compared: different algorithms measure them in different units. RFC 1812 acknowledges this. Its *Best Metric* rule compares metrics only between routes from the **same routing domain**, and it leaves the choice across domains to a *Vendor Policy* rule. Section 5.2.4.4 suggests the mechanism nearly every implementation uses, **administrative preference**: - each route carries an integer from 0 to 255; - **0 is the most preferred** value and 254 the least; - **255 means the route is never used**; - the router keeps only the most-preferred candidates. Most implementations call this value **administrative distance** and assign a default per source. The values are configuration defaults chosen by each implementation, not protocol constants: | Source | One widely used family of defaults | Another family's defaults | |---|---|---| | Connected | 0 | 0 | | Static | 1 | 5 | | OSPF (internal) | 110 | 10 | | External BGP | 20 | 170 | | Internal BGP | 200 | 170 | In both families the static route beats OSPF and BGP for this prefix. But look at OSPF against external BGP: the first family prefers the BGP route, the second the OSPF route. That is why 'which one wins' is never answered by an RFC; it is answered by the router's configuration. RFC 1812 section 7.4 even says that whether a static route beats a dynamic one SHOULD be configurable for each static route. ## Filter 3: the metric, inside one protocol Once one source has won, that protocol's own metric chooses among its routes to the prefix: lowest OSPF cost, fewest RIP hops, BGP's attribute-by-attribute decision process. RFC 1812 actually lists Best Metric before Vendor Policy, but since metrics are compared only within one domain the outcome is the same. If two routes from the same source tie on metric, the router may install both and split traffic between them, which is **equal-cost multipath**. ## What happens to the losers Losing does not mean forgetting. Each protocol keeps its own route in its own database, and only the winner is installed in the forwarding table. When the winner disappears (the static route is deleted, its interface goes down, the BGP session drops), the next-best source's route is installed without anything being relearned. This is what makes a **floating static route** work: a static route deliberately given a *worse* preference than the dynamic route, so that it waits as a backup. ## Preference is local, and can loop Administrative distance never travels in a routing update. RFC 1812 warns that preference 'is not safe in that it can easily be misused to create routing loops', because no protocol checks that neighbouring routers' preferences agree. The classic case is **redistribution**, injecting routes from one protocol into another: 1. Router A learns a prefix from RIP and redistributes it into OSPF. 2. Router B, which runs both protocols, now hears the prefix through OSPF, a source it ranks better than RIP, and prefers the path back through A. 3. If B also redistributes OSPF into RIP, the prefix can be fed back toward where it came from. RFC 1812 section 7.6 requires routers doing two-way exchange between interior protocols to provide some loop-avoidance mechanism; the usual tools are route tags, permit and deny lists, and split horizon. ## Where answers go wrong - Applying administrative distance across different prefix lengths. - Quoting '110 for OSPF' as if a standard fixed it. - Comparing an OSPF cost with a BGP attribute as if they shared a scale. - Assuming the losing routes are discarded rather than held in reserve.
- If the static route for that prefix is then deleted, which route takes over, and does anything have to be relearned?The best remaining candidate by preference is installed: with the first common family of defaults the external BGP route, with the other family the OSPF route. Nothing is relearned, because both protocols kept their routes in their own tables while the static route won. A floating static route uses the same hand-over from the other side.
- How can administrative distance contribute to a routing loop when two border routers redistribute between protocols?Router A redistributes a RIP route into OSPF. Router B, running both, hears it through OSPF, which it ranks above RIP, and prefers the path back through A; if B also redistributes OSPF into RIP, the route can circle. Preference is local and unchecked between neighbours, so RFC 1812 requires loop-avoidance for two-way exchange, typically route tags or filters.
saying these in an interview costs you the question
- A static /16 beats an OSPF /24 for addresses in the /24 because static routes have lower distance.
- RFC 1812 fixes the administrative distance of OSPF at 110.
- A low OSPF cost beats a BGP route because metrics are compared across protocols.
- Administrative distance is advertised to neighbours inside routing updates.
- Routes that lose on administrative distance are discarded and must be relearned.