# Static routing

## Static routing

{% embed url="<https://youtu.be/M7F_ljN0IdA>" %}

<figure><img src="https://935233489-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUGyWMBzIPoBvXNyR5UOW%2Fuploads%2FtLJuJxOfFCpOvMOVKc68%2FRoutingnetwork.png?alt=media&#x26;token=ae46e612-39f8-4914-9259-113609b1c00a" alt=""><figcaption></figcaption></figure>

### Layer3 overview

Note: The colored boxes represent a LAN whereas 4 routers connected together represents a WAN (Wide Area Network - one that extends over a large geographical area).

* PC1 wants to send a packet to PC4.
* The packet has source (PC1) and destination (PC4) IP address.
* PC1 asks itself if the destination IP address is in the same network.
* NO, becuase both are in different networks (Class C address). So PC1 can not forward the packet directly to PC4.
* Since the destination is in a different network, a different LAN, routing logic tells PC1 to send the packet to the 'default gateway'.

> The default gateway is the device to which the host will forward data that is destined for another network.

* To reach other networks, the end host, PC1, will send the packet to its network's router, R1, the default gateway.
* After receiving the packet, R1 will compare the packet's destination IP address to the routing table.

> Each router keeps a routing table, which stores a list of destinations and how to reach the destination.

* Assuming R1 has an entry in its routing table for the 192.168.4.0/24 network, the entry will look like : 192.168.4.0/24 via 192.168.12.2, Gi0/0 ('destination' via 'nexthop', 'interface'). Consider we configured R1 to send it via R2.
* Now the packet has reached R2 and it follows the same process to forward to the packet to R4.
* In R4, the entry in the routing table will look like : 192.168.4.0/24 is directly connected, Gi0/2.

In-depth process on how a packet travels with Layer3 and Layer2 steps - [Life of a Packet](https://neerajcysec.gitbook.io/notes/networking/ccna-notes/life-of-a-packet)

### Packet journey

#### **Routing table**

> show ip route

* Done in Privileged exec mode.
* PC1#show ip route -> displays the routing table.
* Below the codes, 2 routes are displayed (ignore the first line without code - it is a classful address).
* C and L to the left of the 2 routes display the type of route.

> Connected route (C) - the network address the interface is connected to. Local route (L) - the actual IP address on the interface (with a /32 mask - used to identify a specific address).

* If you configure an IP address on an interface and enable the interface, the connected and local routes of that interface are added AUTOMATICALLY.
* But the host (PC1) does not have a route to the destination, which is not a problem. An end host like PC does not need to know the route to every destination. All it needs is a default gateway to which it can send any traffic destined for a location outside the local network.
* On a Cisco router, the default gateway is called 'gateway of last resort', which is not set yet.

#### **Default route**

> A default route is a route that matches ALL possible destinations.

* To configure the 'gateway of last resort' on a Cisco router, you must configure a **default route**.
* It is used only if a more specific route match isn't found in the routing table.
* When a router looks up a route to a destination in its routing table, it looks for the most specific match and chooses that route. ^mostspeificroute
* The default route is the LEAST specific possible route.
  * It uses an IP address of 0.0.0.0 with a mask of 0.0.0.0
  * To set the default route, configure a route to 0.0.0.0/0 (the /0 means that there is no network portion of the address and all the octets are 'not fixed'. covers the range of 0.0.0.0 through 255.255.255.255 - all possible addresses).

#### **Configuring a static route**

> A static route is a route you manually configure yourself.

* Done in Global-config mode

> ip route *destination-address* *mask* *next-hop*

* To set PC1's default route to R1 -> PC1(config)#ip route 0.0.0.0 0.0.0.0 192.168.1.254.
* Now check the routing table (PC1(config)#do show ip route) -> 'Gateway of last resort is 192.168.1.254 to network 0.0.0.0'.
* The default route also appears with the other routes at the bottom with a code 'S\*' at the left (S - static, \* - candidate default).
* Since PC1 now has a default gateway configured, which is R1, it sends the packet there.

#### **Configuring R1**

* R1#show ip route
* It shows the routing table with the connections for 3 networks and its connected and local routes.
* But the destination network of the packet is not in the routing table.

> Switches **flood** frames with unknown destinations (destinations not in the MAC address table). Routers **drop** packets with unknown destinations.

* Unless you configure a route on R1, the ping (packet which is sent) will not succeed.

> ip route *destination-address* *mask* *exit-interface*

* Instead of specifying the IP address as the next hop, you can just specify the interface out of which R1 should send the packet -> R1(config)#ip route 192.168.4.0 255.255.255.0 g0/0
* We don't need a route to the exact destination. Since the destination address '...4.1' is included in the '...4.0/24' range, it matches.
* '192.168.4.0/24 is directly connected, Gi0/0' is added to the routing table with the code 'S'.
* This static route is not added as the 'gateway of last resort', because it is for a specific route, not for 0.0.0.0 which matches all networks.
* Now the packet reaches R2.

#### **Configuring R2**

* R2(config)#ip route 192.168.4.0 255.255.255.0 192.168.24.4
* You can specify the interface the packet gets out or the next-hop IP address (R4 here).
* '192.168.4.0/24 \[1/0] via 192.168.24.4' is added to the routing table.
* Then R2 looks for the closest match for the 'next-hop address' (R4's address) we specified.
* R4 is directly connected to R2. So it will be shown in the routing table of R2.
* So R2 will forward the packet out of the interface which is connected to R4.

#### **Routing table on R4**

* Since R4's g0/2 interface has an IP address of 192.168.4.254 configured, the 192.168.4.0/24 network has automatically been added to the routing table as a **connected route**.
* So R4 sends the packet out of its g0/2 interface and SW4 forwards it to PC4.

### Two-way reachability

* Now use the command 'ping 192.168.4.1' on PC1 to send a ping to PC4 -> the ping fails.
* The packet from PC1 was able to reach PC4, as R1, R2, R4 have routes to the destination network (192.168.4.0/24).
* However, to send a reply back, PC4 sends the packet with the destination address as '192.168.1.1'. R4 and R2 have no route to reach the destination network.

**Configuring**

* Configure the default route on PC4.
* Configure on R4 the static route to 192.168.1.0/24 network with the next-hop address as R2's IP address.
* Configure on R2 the static route to 192.168.1.0/24 network with the next-hop address as R1's IP address.

### Note

* When a router looks up a destination address in its routing table, it looks for the most [specific matching route](#default-route).
* Most specific = longest prefix length (/32 > /24 > /16 > /8 > /0)

**Example 1**

* A ping is issued on R1 -> ping 192.168.4.1
* Before finding the MOST SPECIFIC route, find the matching routes to the destination address from the routing table.

<figure><img src="https://935233489-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUGyWMBzIPoBvXNyR5UOW%2Fuploads%2FFIWRSwuTvXnOBpKAjidb%2FRoutingnetwork1.png?alt=media&#x26;token=58118d12-30b7-4da3-b5bd-868d7d49005f" alt=""><figcaption></figcaption></figure>

* 192.168.4.1 is an exact match, so it is also the most specific match.
* 192.168.4.0/24 also matches. Although the last octet is different, it isn't covered by the mask, so it doesn't matter.
* 192.0.0.0/8 also matches. /8 means only the first octet is covered by the mask, the other 3 can vary and they will still match.
* But 192.168.4.1 is the MOST SPECIFIC MATCH because it has the exact address and it also has the longest prefix length.

**Example 2**

<figure><img src="https://935233489-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUGyWMBzIPoBvXNyR5UOW%2Fuploads%2FcL1G4dDCEL2ugPH5VwMe%2Froutingnetwork2.png?alt=media&#x26;token=2ffc703f-1730-4ed4-b1f8-0f55f031fad3" alt=""><figcaption></figcaption></figure>

* Here the most specific match that the router will choose is 192.168.4.0/24, because it has the longer prefix length.

**Example 3**

<figure><img src="https://935233489-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUGyWMBzIPoBvXNyR5UOW%2Fuploads%2FJOPqtShZln7bcQouMx40%2Froutingnetwork3.png?alt=media&#x26;token=9d8278c4-bdb3-4ad1-9086-6345b823abfb" alt=""><figcaption></figcaption></figure>

### Quiz

1. The IP address configured on a router interface will appear in the routing table as what kind of route?
   * Local (since they use a /32 mask, it specifies the exact address configured on the interface).
2. The command to manually configure a static route
   * ip route *destination-address* *mask* *next-hop|exit interface*.

### Lab

**Filter**

* R1#show running-config | include ip route -> shows only the iproute part of the running config

**Delete the configuration statement**

* R1#show running-config | include ip route -> shows the configuration statement -> copy it.
* Goto global config mode. Paste the statement and type 'no' at the start (deletes the configuration statement).

If you do not delete the wrong route when adding a correct one, both will be kept in the routing table and the router will **load balance** over the 2 routes -> sending some packets through both the interfaces. Sometimes load balancing is a good thing. But when a route is completely wrong, you should remove it.

However when configuring IP address, when you enter a new one, it will **overwrite** the old one, unlike IP routes.
