routing_table - dwilson2547/wiki_demo GitHub Wiki
- Routing Table
- 1. What is a Routing Table?
- 2. Components of a Routing Table Entry
- 3. How Routing Tables Are Generated
- 4. Routing Table Generation: Step-by-Step
- 5. How Routing Tables Are Used
- 6. Routing Table Examples
- 7. Dynamic Routing Protocols in Depth
- 8. Advanced Routing Table Concepts
- 9. Routing Table Maintenance
- 10. Practical Example: Tracing a Packet
- 11. Tools to Inspect Routing Tables
- 12. Summary: How Routing Tables Are Built
A routing table is a database stored in a router or networked device that lists:
- Destination networks (where packets need to go).
- Next hops (the next device to forward the packet to).
- Interfaces (which physical or logical interface to use).
- Metrics (cost or priority of the route).
- Route types (directly connected, static, or dynamically learned).
Each entry in a routing table typically includes:
Field | Description |
---|---|
Destination Network | The IP address of the destination network (e.g., 192.168.1.0/24 ). |
Subnet Mask | Defines the network portion of the destination (e.g., 255.255.255.0 or /24 ). |
Next Hop | The IP address of the next router or gateway (e.g., 10.0.0.1 ). |
Interface | The outgoing interface (e.g., eth0 , GigabitEthernet0/1 ). |
Metric | The cost or priority of the route (e.g., 10 for OSPF, 1 for directly connected networks). |
Route Type | How the route was learned (e.g., connected, static, OSPF, BGP, RIP). |
- Definition: Routes to networks that are directly connected to the router’s interfaces.
-
How It’s Added:
- When an interface is configured with an IP address and subnet mask, the router automatically adds a directly connected route to its routing table.
- Example: If
eth0
is configured with192.168.1.1/24
, the router adds a route for192.168.1.0/24
viaeth0
.
-
Example Entry:
Destination: 192.168.1.0/24 Next Hop: 0.0.0.0 (directly connected) Interface: eth0
- Definition: Routes that are manually configured by a network administrator.
-
How It’s Added:
- Administrators use commands like
ip route
(Linux) orroute add
(Windows) to add static routes. - Example (Linux):
This adds a route to
ip route add 10.0.0.0/24 via 192.168.1.2 dev eth0
10.0.0.0/24
via the next hop192.168.1.2
using interfaceeth0
.
- Administrators use commands like
-
Use Cases:
- Small networks.
- Routes to specific networks that don’t change often.
- Backup routes.
-
Example Entry:
Destination: 10.0.0.0/24 Next Hop: 192.168.1.2 Interface: eth0
Dynamic routes are automatically learned and updated using routing protocols. These protocols allow routers to exchange routing information and adapt to network changes (e.g., link failures).
-
Interior Gateway Protocols (IGPs):
- Used within a single autonomous system (AS).
- Examples: OSPF (Open Shortest Path First), RIP (Routing Information Protocol), EIGRP (Enhanced Interior Gateway Routing Protocol).
-
Exterior Gateway Protocols (EGPs):
- Used between different autonomous systems.
- Example: BGP (Border Gateway Protocol).
-
Neighbor Discovery:
- Routers using the same routing protocol discover each other by exchanging hello packets.
- Example: OSPF routers send hello packets every 10 seconds to establish adjacencies.
-
Link-State or Distance-Vector Updates:
-
Link-State Protocols (e.g., OSPF):
- Routers exchange Link-State Advertisements (LSAs) to build a complete map of the network (topology database).
- Each router runs the Dijkstra algorithm to calculate the shortest path to every network.
-
Distance-Vector Protocols (e.g., RIP):
- Routers share their entire routing table with neighbors.
- Each router updates its table based on the distance (hop count) to each network.
-
Link-State Protocols (e.g., OSPF):
-
Best Path Selection:
- Routers use metrics like hop count (RIP), bandwidth (OSPF), or path attributes (BGP) to determine the best path.
- Example: OSPF prefers paths with the lowest cost (inversely proportional to bandwidth).
-
Routing Table Update:
- The best paths are added to the routing table.
- Example OSPF entry:
Destination: 172.16.0.0/16 Next Hop: 192.168.2.2 Interface: eth1 Metric: 10
- Router A and Router B establish an OSPF adjacency.
-
Router B sends an LSA advertising network
172.16.0.0/16
with a cost of10
. -
Router A receives the LSA, runs Dijkstra’s algorithm, and adds the route to its routing table:
Destination: 172.16.0.0/16 Next Hop: 192.168.2.2 (Router B) Interface: eth1 Metric: 10
- Definition: A route used when no other route matches the destination. Typically points to the gateway of last resort (e.g., ISP’s router).
-
How It’s Added:
- Manually configured as a static route:
ip route add default via 192.168.1.1
- Or learned dynamically (e.g., via DHCP or BGP).
- Manually configured as a static route:
-
Example Entry:
Destination: 0.0.0.0/0 Next Hop: 192.168.1.1 Interface: eth0
-
Initialize Interfaces:
- The router loads configurations for its interfaces (e.g.,
eth0
,eth1
). - Directly connected routes are added to the routing table.
- The router loads configurations for its interfaces (e.g.,
-
Load Static Routes:
- Manually configured static routes are added.
-
Establish Routing Protocol Adjacencies:
- The router sends hello packets to discover neighbors (e.g., OSPF or EIGRP neighbors).
-
Exchange Routing Information:
- The router exchanges LSAs (OSPF) or routing tables (RIP) with neighbors.
-
Run Path Selection Algorithms:
- The router calculates the best paths using Dijkstra (OSPF) or Bellman-Ford (RIP).
-
Update Routing Table:
- Best paths are added to the routing table.
-
Forward Packets:
- The router uses the routing table to forward incoming packets.
[Router A] ---- [Router B] ---- [Router C]
eth0: 192.168.1.1/24 eth0: 192.168.1.2/24
eth1: 10.0.0.1/24 eth1: 10.0.0.2/24 eth0: 172.16.0.1/16
-
Router A is connected to
192.168.1.0/24
and10.0.0.0/24
. -
Router B is connected to
192.168.1.0/24
,10.0.0.0/24
, and172.16.0.0/16
(via Router C). -
Router C is connected to
172.16.0.0/16
.
-
Directly Connected Routes:
192.168.1.0/24 via eth0 10.0.0.0/24 via eth1
-
OSPF Learned Route:
- Router B advertises
172.16.0.0/16
with a cost of10
. - Router A adds:
172.16.0.0/16 via 10.0.0.2 (Router B), eth1, cost 10
- Router B advertises
When a router receives a packet:
- Extract Destination IP: The router looks at the destination IP in the packet header.
-
Longest Prefix Match: The router searches its routing table for the most specific match (longest subnet mask) for the destination IP.
- Example: A packet to
172.16.5.10
matches172.16.0.0/16
in the routing table.
- Example: A packet to
-
Forward the Packet:
- If a match is found, the packet is forwarded to the next hop via the specified interface.
- If no match is found, the packet is forwarded using the default route (if one exists).
- If no default route exists, the packet is dropped.
View the routing table using:
ip route show
or
route -n
Example Output:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.16.0.0 10.0.0.2 255.255.0.0 UG 10 0 0 eth1
-
0.0.0.0
: Default route via192.168.1.1
. -
10.0.0.0/24
: Directly connected viaeth1
. -
172.16.0.0/16
: Learned via OSPF, next hop10.0.0.2
.
View the routing table using:
show ip route
Example Output:
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2
Gateway of last resort is 192.168.1.1 to network 0.0.0.0
S* 0.0.0.0/0 [1/0] via 192.168.1.1
10.0.0.0/24 is subnetted, 1 subnets
C 10.0.0.0 is directly connected, Ethernet1
C 192.168.1.0/24 is directly connected, Ethernet0
O 172.16.0.0/16 [110/10] via 10.0.0.2, 00:00:10, Ethernet1
-
S*
: Static default route via192.168.1.1
. -
C
: Directly connected routes. -
O
: OSPF-learned route for172.16.0.0/16
.
- Type: Link-state protocol.
-
How It Works:
- Routers exchange LSAs to build a topology database.
- Each router runs the Dijkstra algorithm to calculate the shortest path to every network.
- Routes are added to the routing table based on the lowest cost (e.g.,
cost = 100Mbps / bandwidth
).
-
Example Metric:
-
10.0.0.0/24
viaeth1
, cost10
.
-
- Type: Distance-vector protocol.
-
How It Works:
- Routers share their entire routing table with neighbors every 30 seconds.
- Routes are selected based on the lowest hop count (maximum 15 hops).
- If a route’s hop count exceeds 15, it is considered unreachable.
-
Example Metric:
-
172.16.0.0/16
via10.0.0.2
, hop count2
.
-
- Type: Path-vector protocol (used between autonomous systems).
-
How It Works:
- Routers exchange path attributes (e.g., AS path, next hop) to determine the best route.
- BGP uses policies (e.g., prefer shorter AS paths) to select routes.
- BGP does not use traditional metrics like cost or hop count.
-
Example Route:
-
203.0.113.0/24
via198.51.100.2
, AS path65001 65002
.
-
- Definition: A measure of trustworthiness for routes from different sources.
- Purpose: If multiple routes exist for the same destination, the route with the lowest administrative distance is preferred.
-
Example Values:
- Directly connected:
0
- Static route:
1
- EIGRP:
90
- OSPF:
110
- RIP:
120
- BGP:
20
(external),200
(internal)
- Directly connected:
- Definition: The process of sharing routes between different routing protocols.
-
Example: Redistributing OSPF routes into BGP:
router bgp 65001 redistribute ospf 1
- Use Case: Connecting an OSPF network to the internet via BGP.
- Definition: Routing decisions based on policies (e.g., source IP, protocol) rather than just destination IP.
-
Example: Route traffic from
192.168.1.0/24
viaISP1
and traffic from10.0.0.0/24
viaISP2
. -
Configuration (Linux):
ip rule add from 192.168.1.0/24 lookup 100 ip route add default via 198.51.100.1 dev eth0 table 100
- Aging Out Routes: Dynamic routes are removed if not refreshed (e.g., RIP routes time out after 180 seconds).
- Link Failures: If a link fails, routing protocols recalculate paths and update the routing table.
- Manual Updates: Administrators can manually add, remove, or modify routes.
- Your device (
192.168.1.10
) sends a packet to203.0.113.5
. -
Routing Table on Your Router:
0.0.0.0/0 via 198.51.100.1 (ISP) 192.168.1.0/24 via eth0
-
Steps:
- Your device sends the packet to the router (
192.168.1.1
). - The router matches
203.0.113.5
to the default route (0.0.0.0/0
). - The router forwards the packet to
198.51.100.1
(ISP’s gateway). - The ISP routes the packet toward
203.0.113.5
using BGP.
- Your device sends the packet to the router (
-
Linux/macOS:
ip route netstat -rn traceroute 203.0.113.5
-
Windows:
route print tracert 203.0.113.5
-
Cisco IOS:
show ip route show ip ospf database
- Directly Connected Routes: Added automatically for local interfaces.
- Static Routes: Manually configured by administrators.
- Dynamic Routes: Learned via routing protocols (OSPF, RIP, BGP).
- Default Route: Used for destinations with no specific match.
- Best Path Selection: Based on metrics, administrative distance, and policies.
- Forwarding: Packets are forwarded using the longest prefix match.