# Routing

<mark style="color:blue;">**What is a Router?**</mark>

A router is a device that forwards packets between different networks. It works at Layer 3 (Network Layer) of the OSI model and makes decisions based on the destination IP address of each packet. In simple terms, a router connects separate networks, such as LANs, WANs, or the Internet, and finds the best way for data to travel. Router core functions is:

* *Packet forwarding*: Routes traffic from one network to another.
* *Routing table management*: Keeps track of network paths and their metrics.
* *NAT/PAT*: Maps private IPs to public IPs in situations with limited IPv4 addresses.
* *Security*: Applies ACLs, firewall rules, or filters out malicious traffic.
* *Dynamic adjustment*: Changes routing in real time if the network topology changes.

***Diagram***

This shows the main role of a router: connecting different networks.

```
+--------------------+          +----------+          +----------+          +-----------------+
| LAN                |----------| Router A |----------| Internet |----------| Router B        |
| 192.168.1.0/24     |          |          |          |          |          | LAN 10.0.0.0/24 |
+--------------------+          +----------+          +----------+          +-----------------+
```

<mark style="color:blue;">**Static vs Dynamic Routing**</mark>

The main difference is how the router learns and keeps track of the paths in its routing table.

**Static Routing**

Static routes are set up manually by a network administrator. A static route defines the destination network, subnet mask, and the next-hop IP address or outgoing interface.

<sup>*Advantages:*</sup>

* Predictable, no protocol overhead
* Simple to troubleshoot in small networks
* Secure (cannot be modified by other routers)

<sup>*Disadvantages:*</sup>

* Manual updates required for topology changes
* Not scalable for large networks
* Can lead to human errors

*Example Static Routing Table:*

| Destination Network | Subnet Mask   | Next Hop    | Interface |
| ------------------- | ------------- | ----------- | --------- |
| 192.168.2.0         | 255.255.255.0 | 192.168.1.2 | eth0      |
| 10.0.0.0            | 255.0.0.0     | 192.168.1.3 | eth1      |
| 0.0.0.0             | 0.0.0.0       | 192.168.1.1 | eth0      |

**Dynamic Routing**

Dynamic routing uses protocols to automatically share network information. Routers communicate to discover routes, share updates, and adapt to network failures.

<sup>*Advantages:*</sup>

* Scales well in large networks
* Changes automatically with topology modifications
* Lowers administrative workload

<sup>*Disadvantages:*</sup>

* Uses CPU, memory, and bandwidth for routing updates
* Can share misconfigurations if not well designed
* Requires careful protocol selection for stability

<mark style="color:blue;">**Routing Protocols**</mark>

Routers use specific protocols to exchange routing information automatically

| Protocol | Type            | Metric/Feature                  | Use Case                  |
| -------- | --------------- | ------------------------------- | ------------------------- |
| RIP      | Distance-vector | Hop count (max 15)              | Small networks            |
| OSPF     | Link-state      | SPF algorithm, fast convergence | Enterprise networks       |
| BGP      | Path-vector     | Policies & AS paths             | Internet backbone         |
| EIGRP    | Hybrid          | Bandwidth, delay, reliability   | Cisco enterprise networks |

**RIP (Routing Information Protocol)**

* **Type:** Distance-vector protocol.
* **Operation:** Sends its entire routing table to neighbors every 30 seconds.
* **Metric:** Uses hop count (the number of routers to cross).
* **Limitation:** Maximum of 15 hops; slow to respond and prone to loops. Best for small, simple networks.

**OSPF (Open Shortest Path First)**

* **Type:** Link-state protocol.
* **Operation:** Routers exchange LSAs (Link-State Advertisements) to create a complete map of the network topology in a Link-State Database.
* **Algorithm:** Uses Dijkstra's SPF algorithm to find the shortest path to each network.
* **Benefit:** Fast response and efficient path selection, making it good for medium and large networks.

**BGP (Border Gateway Protocol)**

* **Type:** Path-vector protocol.
* **Operation:** Used to connect different Autonomous Systems (AS) on the Internet. An AS is a network under a single administrative control (e.g., an ISP).
* **Decision Basis:** Routing is based on policies, rules, and paths, not just simple metrics.
* **Role:** The main protocol that connects the global Internet.

<mark style="color:blue;">**Routing Tables**</mark>

A router's routing table contains the information needed for forwarding decisions.

* Destination network: Where the packet should go
* Subnet mask: Network part of the IP
* Next hop: Router to send traffic to
* Interface: Outgoing port
* Metric: Cost of route (lower values are preferred)

***Example Routing Table:***

| Destination | Subnet Mask   | Next Hop    | Interface | Metric |
| ----------- | ------------- | ----------- | --------- | ------ |
| 192.168.1.0 | 255.255.255.0 | 0.0.0.0     | eth0      | 0      |
| 192.168.2.0 | 255.255.255.0 | 192.168.1.2 | eth1      | 10     |
| 10.0.0.0    | 255.0.0.0     | 192.168.1.3 | eth1      | 5      |
| 0.0.0.0     | 0.0.0.0       | 192.168.1.1 | eth0      | 100    |

The route 0.0.0.0/0 is the default route, covering all traffic to unknown destinations, usually directing it to the Internet.

***Packet Path Example:***

```
[PC A: 192.168.1.10] --> [Router A] --> [Router B] --> [PC B: 10.0.0.5]
```

1. A sends a packet to PC B. It sees that 10.0.0.5 is not part of its local network (192.168.1.0/24), so it forwards the packet to its default gateway, Router A.
2. Router A checks its routing table. It finds an entry for the network 10.0.0.0/8 and sees it can reach that via Router B. It forwards the packet to Router B.
3. Router B gets the packet, looks at its own table, and sees that the specific PC (10.0.0.5) is on a network directly connected to it. It delivers the packet straight to PC B.

<mark style="color:blue;">**OSPF Routing Example**</mark>

Scenario: 3 routers forming a backbone

* Router A: 192.168.1.0/24
* Router B: 192.168.2.0/24
* Router C: 10.0.0.0/24

**OSPF Steps:**

1. **Neighbor Discovery:** Routers exchange Hello packets to discover OSPF neighbors on their directly connected networks
2. **Link-State Advertisement (LSA):** Routers send LSAs to describe their connected networks and the state of their links
3. **Link-State Database (LSDB):** Each router gathers all LSAs into a synchronized LSDB, forming a complete map of the OSPF area
4. **SPF Calculation:** Each router runs the Dijkstra SPF algorithm on the LSDB to independently calculate the shortest paths to every network
5. **Routing Table Update:** The best paths from the SPF tree are added to the router's IP routing table.

*Router B Table:*

| Network     | Next Hop | Metric |
| ----------- | -------- | ------ |
| 192.168.1.0 | Router A | 1      |
| 192.168.2.0 | Local    | 0      |
| 10.0.0.0    | Router C | 1      |

<mark style="color:blue;">**Routing Loops and Convergence**</mark>

* **Routing Loops:** Happen when a packet is endlessly passed between routers in a cycle, never reaching its destination. Prevention methods are important:
  * **TTL (Time-to-Live):** A counter in the IP header that decreases with each hop; the packet is discarded if TTL reaches zero
  * **Split Horizon:** A rule that stops a route from being advertised back out the interface it was learned from
  * **Route Poisoning:** Marking a failed route as "unreachable" and advertising it to prevent other routers from using it
* **Convergence Delay:** The time taken for all routers in a network to update their routing tables after a topology change. Fast convergence is vital for network stability. OSPF converges faster than RIP because it uses a more efficient update process.

***Traceroute Example***

Comman&#x64;**:** `traceroute 10.0.0.5`

```
traceroute to 10.0.0.5
1  192.168.1.1  0.5 ms  # Your local router (Gateway)
2  192.168.1.2  1.2 ms  # Your ISP's router
3  192.168.2.1  2.0 ms  # A router further in the network
4  10.0.0.5     2.5 ms  # The final destination
```

This shows the path a packet takes and is useful for troubleshooting routing issues.

<mark style="color:blue;">**Hybrid Routing Networks**</mark>

Real-world networks often mix static and dynamic routing to take advantage of both approaches:

* Static Routes are used for important, stable paths (e.g., to a central server farm or a specific security subnet), where predictability is essential
* Dynamic Routes (OSPF, EIGRP) serve the core network and inter-office links to provide automatic adjustment and scalability

This hybrid setup minimizes protocol overhead when possible while ensuring reliability and flexibility across the network.

***Hybrid Routing Example***

```
[LAN 192.168.1.0/24] ---[Router A]--- [Internet] ---[Router B]--- [LAN 10.0.0.0/24]
    |                       |                         |
Static Route             OSPF/BGP                  Dynamic Routes
```

In this example, a static route may be used inside the local LAN for a specific server, while BGP is used to exchange routes with the Internet and OSPF is used within the larger corporate network.
