# Routing Fundamentals

Routing is the process of selecting paths for traffic to travel between different networks. Routers make forwarding decisions based on routing tables.

## Routing vs Switching

| Aspect | Switching (Layer 2) | Routing (Layer 3) |
|---|---|---|
| **Forwarding based on** | MAC address | IP address |
| **Domain** | Same broadcast domain (within VLAN) | Between different networks |
| **Device** | Switch | Router / Layer 3 switch |
| **Purpose** | Local delivery | Inter-network delivery |

## How Routing Works

1. **Packet arrives** at router interface
2. **Router checks destination IP** against routing table
3. **Longest prefix match** determines best route
4. **Router decrements TTL** and recalculates checksum
5. **Forwards to next hop** with new Layer 2 frame

## Routing Table

A routing table contains information about known networks and how to reach them.

| Entry Type | Description | Example |
|---|---|---|
| **Connected (C)** | Directly connected networks | 192.168.1.0/24 is directly connected |
| **Local (L)** | Router's own interface IPs | Host routes for interfaces |
| **Static (S)** | Manually configured routes | ip route 10.0.0.0/8 192.168.1.2 |
| **Dynamic** | Learned via routing protocols | OSPF, BGP, EIGRP routes |

## Routing Methods

| Method | Description | Pros | Cons |
|---|---|---|---|
| **Static Routing** | Manually configured routes | Simple, secure, no overhead | Doesn't scale, manual updates |
| **Default Routing** | Route for all unknown destinations (0.0.0.0/0) | Simple for stub networks | Less specific control |
| **Dynamic Routing** | Routers share routes automatically | Scalable, adapts to changes | More complex, protocol overhead |

## Longest Prefix Match

Routers always choose the route with the longest matching prefix (most specific route).

**Example routing table:**
- 10.0.0.0/8 via 192.168.1.1
- 10.1.1.0/24 via 192.168.1.2

For packet to 10.1.1.5, the /24 route is more specific → use 192.168.1.2

## Static Routing Example

```cisco
! Static route to remote network via next hop
ip route 192.168.50.0 255.255.255.0 10.0.0.2

! Default route
ip route 0.0.0.0 0.0.0.0 10.0.0.1

! Verify
show ip route
```

```bash
# Linux
ip route add 192.168.50.0/24 via 10.0.0.2
ip route add default via 10.0.0.1
ip route show
```

## Dynamic Routing Protocols

| Protocol | Type | Metric | Use Case |
|---|---|---|---|
| **RIP** | Distance Vector | Hop count (max 15) | Legacy/small networks |
| **OSPF** | Link State | Cost (based on bandwidth) | Enterprise/internal networks |
| **EIGRP** | Hybrid (Cisco proprietary historically) | Composite (bandwidth + delay) | Cisco environments |
| **IS-IS** | Link State | Cost | Service provider networks |
| **BGP** | Path Vector | AS-Path, attributes (policy-based) | Internet routing between ASes |

### Distance Vector vs Link State

| Aspect | Distance Vector | Link State |
|---|---|---|
| **View** | Knows distance to networks, not full topology | Has complete topology map |
| **Updates** | Periodic full updates or triggered | Floods link-state updates, builds SPF tree |
| **Convergence** | Slower | Faster |
| **Examples** | RIP | OSPF, IS-IS |
| **Resource Usage** | Less CPU/memory | More CPU/memory |

## Administrative Distance (AD)

When multiple routing sources learn same route, router chooses based on Administrative Distance (trustworthiness).

| Source | AD |
|---|---|
| **Connected** | 0 (most trusted) |
| **Static** | 1 |
| **eBGP** | 20 |
| **EIGRP (internal)** | 90 |
| **OSPF** | 110 |
| **IS-IS** | 115 |
| **RIP** | 120 |
| **iBGP** | 200 |
| **Unknown** | 255 (not used) |

## Routing in Cloud

In cloud environments (AWS VPC, etc.), routing works differently:
- **Route Tables** define where traffic goes
- **Internet Gateway (IGW)** for internet access
- **NAT Gateway** for outbound from private subnets
- **VPC Peering** between VPCs
- **Transit Gateway** for complex connectivity
- **BGP** used with Direct Connect/Cloud VPN

## Troubleshooting

```bash
# View routing table
ip route show  # Linux
show ip route   # Cisco

# Trace path
traceroute 8.8.8.8
mtr 8.8.8.8

# Check connectivity
ping 8.8.8.8

# Check if routing
ip route get 8.8.8.8
```

| Issue | Debug Steps |
|---|---|
| **Can't reach remote network** | Check routing table, next hop reachable, no ACL blocking |
| **Asymmetric routing** | Check routes in both directions |
| **Routing loop** | TTL exceeded, traceroute shows loop |
| **Wrong path taken** | Check metric/AD, longest prefix match |

## Related Notes

- [[Routing/OSPF-Fundamentals.md]] — Link-state IGP
- [[Routing/BGP-And-Internet-Routing.md]] — Path-vector, internet routing
- [[Switching-LAN/VLANs-Trunking.md]] — Inter-VLAN routing
- [[Cloud-Modern-Networking/VPC-Fundamentals.md]] — Cloud routing
- [[Foundations/Addressing-Subnetting-CIDR.md]] — IP addressing

## Sources

* Cisco CCNP ENARSI materials  
* RFC 2328 (OSPF)  
* RFC 4271 (BGP)  
* Kurose & Ross - *Computer Networking*  
