# Open Shortest Path First (OSPF)

* **Open Shortest Path First (OSPF)** is a link-state routing protocol that is used to find the best path between the source and the destination router using its own Shortest Path First.
    
* The protocol **recalculates routes when a link fails and the network topology changes,** Using the **Dijkstra(dike·struh) algorithm**, and minimizes the routing protocol traffic that it generates.
    
* It provides a multi-level hierarchy called “area routing” so that information about the topology within the defined area of the AS is hidden from routes outside this area. This enables an additional level of **routing protection** and **reduction in routing protocol traffic**.
    
* All protocol **exchanges can be authenticated** so that only **trusted routers can join in the routing exchanges for the AS**
    

## Benefits of OSPF:

* Fast convergence.
    
* Efficient use of network resources.
    
* Support for large, complex networks.
    
* Ability to route IPv4 and IPv6 traffic (OSPFv2 for IPv4, OSPFv3 for IPv6).
    
* Redundancy and fault tolerance.
    
* VLSM (Classless Routing) and route summarization
    

## OSPF Concepts

### Area

* An **area** is a logical collection of OSPF networks, routers, and links that have the same area identification.
    
* The most important area in OSPF is the **backbone area**, also known as **area 0**. The backbone area is the central area that interconnects all other areas.
    
* In a large network, a single-area design can have negative effects:
    
* The SFP algorithm takes more time to calculate routes and it causes the algorithm to require more processing power.
    
* Any small change in the network causes every router to flood LSAs and run the SPF algorithm again.
    
* By dividing a large OSPF network into several smaller areas, you can avoid the above negative effects.
    

### Neighbour

* Routers running OSPF need to establish a neighbour relationship before exchanging routing updates, Neighbours are dynamically discovered by sending **Hello packets** out of each OSPF-enabled interface.
    
* **Hello Packets**:
    
    OSPF uses **multicast addresses** to communicate and exchange Hello Packets between routers
    
* **224.0.0.5:** All OSPF routers.
    
* **224.0.0.6**: All OSPF designated routers (DRs) and backup designated routers (BDRs).
    
* **Hello Interval**:
    
    * The OSPF router sends a hello message on an interface. It is **10 seconds** by default.
        
* **Dead Interval**:
    
    * The Dead interval is four times the Hello interval, this means a Dead interval of **40 seconds**
        
    * If a router does not receive at least one Hello packet from a neighbour within the Dead interval, the neighbour is considered down.
        

![ospf_example_topology.jpg](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1a65974c-2e09-43af-886c-5f166ac8cc6d/ospf_example_topology.jpg align="left")

## ABR and ASBR

* **Area Border Router(ABR):** a router that connects two or more OSPF areas, one of which must be the backbone area (area 0).
    
* **Autonomous System Border Router(ASBR):** a router that **connects an OSPF domain to an external network or another routable domain**, it is responsible for importing and redistribution routes from non-OSPF sources into the OSPF routing source.
    

![1729506083948.jpeg](https://prod-files-secure.s3.us-west-2.amazonaws.com/b247563b-1adc-47ed-99d5-2439b5d28ac4/1ebdb827-1896-40d4-a080-2f5b244131f0/1729506083948.jpeg align="left")

![Screenshot 2023-02-21 104354.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/61dd3ae7-63ff-4c83-98b2-66def43b4e15/Screenshot_2023-02-21_104354.png align="left")

## Designated Router and Backup Designated Router

* **Designated Router (DR) and Backup Designated Router(BDR)** is to act as **a central point for exchanging of OSPF information between multiple routers on the same network**. Each non-DR and non-BDR router only exchanges routing information with the DR and BDR, Instead of exchanging updates with every router on the segment. This significantly reduces the amount of OSPF updates sent across the network.
    

![Screenshot 2023-02-21 110657.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/81063bb1-6c7e-4309-8674-3d36e90bab7b/Screenshot_2023-02-21_110657.png align="left")

### DR and BDR Election

* Upon the segment, each router will go through an election process, to elect A DR and BDR.
    

There are two rules used to determine who is elected:

1- **OSPF priority** is a value between 0 and 255 that may be assigned to every router interface participating in OSPF. The **default priority is 1**. A priority of 0 means that the router is not eligible to become DR or BDR.

```jsx
R1(config)#interface fastEthernet 0/0
R1(config-if)#ip ospf priority 200
```

2- **Router ID** is a **32-bit** number that uniquely identifies every OSPF router.

* Manual Configuration.
    
* A highest IP address on a loopback address.
    
* A highest IP address on a physical address.
    
* OSPF Router ID should not be changed after the OSPF process has started and the OSPF neighborships have been established. **If you change the OSPF Router ID, We need to either reload the IOS** or use the command (**clear ip ospf process**), for OSPF Router ID change to take effect. reloading the IOS or using the command \[**clear ip ospf process**\] **can cause a temporary network outage.**
    

## Router ID Selection algorithm works as below:

1. If manually configured OSPF Router ID in OSPF process, it is selected as the OSPF Router ID.
    

```jsx
R1# conf t 
R1#(config) router ospf 100(OSPF process number).
R1#(config-router)# router-id 1.1.1.1
```

**Note:** the router step-by-step checks each point if the router-id is configured manually other steps skipped

## Loopback interface

* Loopback interfaces are logical(**virtual**) interfaces. using loopback interfaces with OSPF **configuration ensures that an interface is always active for** OSPF processes.
    
* It is always up/up (unless you manually shut it down).
    
* It is not dependent on a physical interface.
    
* They can be used for diagnostic purposes as well as OSPF configuration. The reason to configure a loopback interface on a router is because if you don’t. the highest active IP address on a router at the time of bootup will become that router’s RID, The RID is used to advertise the routes as well as e**lect the DR and BDR.**
    

## Loopback Interface Configuration

```jsx
R1#conf t 
R1(config)#interface loopback 0 
R1(config-if)#ip address 10.0.0.1 255.255.255.0
R1(config-if)#exit
```

&lt;aside&gt; 💡

Always manually configure the Router ID using the `router-id` command for stability and control.

&lt;/aside&gt;

## OSPF Process ID

* The process ID is the ID of the OSPF process to which the interface belongs. the process ID is local to the router, and **two OSPF neighbouring routers can have different OSPF process IDs**. (Not like EIGRP). Cisco IOS software can run **multiple OSPF processes on the same router,** The Router ID should be a **positive integer,** for example, **the process ID is 1**
    

## **Link State Advertisement (LSAs)**

* LSA(**Link State Advertisement**) packets are sent between routers within the same area of origin and do not leave the area. An OSPF router uses LSA packets to describe its own interfaces but also carries information about its neighbours to adjacent routers in the same area.
    

| LSA Type 1: | Router LSA |
| --- | --- |
| LSA Type 2: | Network LSA |
| LSA Type 3: | Summary LSA |
| LSA Type 4: | Summary ASBR LSA |
| LSA Type 5: | Autonomous system external LSA |
| LSA Type 6: | Multicast OSPF LSA |
| LSA Type 7: | Not-so-stubby area LSA |
| LSA Type 8: | External attribute LSA for BGP |

## OSPF Concepts: Main LSAs

| LSA Type | name | Generated by | Description |
| --- | --- | --- | --- |
| LSA Type 1: | Router LSA | Generated by each internal router within an area, per area link | Flooded within an single area only. |
| LSA Type 2: | Network LSA | Generated by the DR | Flooded within a single area only. |
| LSA Type 3: | Summary LSA | Generated by the BDR | Describes inter-area routes |
| LSA Type 4: | Summary ASBR LSA | Generated by the ABR. | Describes the path to the ASBR. |
| LSA Type 5: | Autonomous system external LSA | Generated by ASBR. | Describes the redistributed routes that have been injected into the OSPF domain from an external AS by The ASBR |
| LSA Type 7: | Not-so-stubby area(NSSAs) LSA | Generated by the ASBR, within a not-so-stubby area. | Describes external routes. sent from ASBR to ABR and converted into a type 5 by the ABR, which then floods to its areas. |

![ospf wireshark .png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0b56cc60-fb96-4f55-a544-ceeb026afa91/ospf_wireshark_.png align="left")

&lt;aside&gt; 💡

* **Hello messages** are used for establishing and maintaining OSPF neighbour relationships.
    
* **LSAs** are used to share detailed routing and topology information within the OSPF domain. &lt;/aside&gt;
    

## Cost

* Open Shortest Path First(OSPF) uses **cost** as the value of metric and uses a reference bandwidth of **100** Mbps for cost calculation. The formula to calculate the cost is Reference Bandwidth divided by interface bandwidth. for example, in the case of 10 Mbps Ethernet, OSPF metric cost value is **100 Mbps / 10 Mbps**.
    

![image.png](https://prod-files-secure.s3.us-west-2.amazonaws.com/b247563b-1adc-47ed-99d5-2439b5d28ac4/407972e9-d770-4bc4-b5cb-f9a72319c06e/image.png align="left")

### Modify the reference bandwidth

```jsx
R1(config-router) **auto-cost reference-bandwidth** megabits-per-second
```

### Manually Configure the cost of an interface:

```jsx
R1(config-if)**ip ospf cost** cost
```

### Modify the interface bandwidth:

```jsx
R1(config-if)**bandwith** kilobits-per-second
```

Which of these commands can be used to make a FastEthernet interface have an OSPF cost of 100?

* R(config-router)# auto-cost reference bandwidth 1000
    

**Reference bandwidth**/**Interface bandwidth** = cost

10000/100=100

### **Example Scenario**

**Scenario**: You have two paths from Router A to Router D:

1. Path 1: A → B → D (10 Mbps and 100 Mbps links, respectively).
    
2. Path 2: A → C → D (100 Mbps links only).
    

**Default Cost Calculation**:

* Path 1: Cost = 10 (A → B) + 1 (B → D) = **11**.
    
* Path 2: Cost = 1 (A → C) + 1 (C → D) = **2**.
    

**Result**:

* OSPF chooses **Path 2** because it has a lower total cost.
    

&lt;aside&gt; 💡

the SPF algorithm uses the cost of **the outgoing interfaces** toward the destination subnet, including the cost of the interface that connects to the subnet. The logic is illustrated in the following diagram.

&lt;/aside&gt;

![intra-area-adding-cost-logic.svg](attachment:e95eabe2-f5ef-482c-982d-e75f076c055c:intra-area-adding-cost-logic.svg align="left")

![Screenshot 2023-02-23 140910.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/479d1d6a-87c0-4e35-90a6-18e76d3a0b76/Screenshot_2023-02-23_140910.png align="left")

## Wild Card Mask

* Wildcard masks are used to specify a range of network addresses. They are usually used with routing protocols **(Such as OSPF) and access lists**.
    
* A Wildcard mask is **32-bit** long. it is a sort of inverted subnet mask, with the zero bits indicate that the corresponding bit position must match the same bit position in the IP address.
    

![Screenshot 2023-03-01 095726.png](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/375711ff-e16b-46ea-bc7a-7fb3bf1f65a2/Screenshot_2023-03-01_095726.png align="left")

Note: change all **0** to **255** and all **255** to **0**, in the second one last number **255-192=063**

### OSPF States

* **Down State**: OSPF is activated on the router, The router sends out Hello packets to the multicast address 224.0.0.5. It doesn’t know about any OSPF neighbours yet, so the current neighbour state is **Down**.
    
* **Init State:** A Hello packet has been received from a neighbour, but the receiving router has not yet been listed in the neighbour's Hello Packet. At this stage, Communication is **One-way.**
    

![image.png](attachment:a98444c8-eceb-4af8-a7b2-fb30cf6ea083:image.png align="left")

* **2-Way State**: The Router Hello packet now includes the neighbour’s Router ID, and the neighbour’s Hello packets include the local router ID, Two-way communication is established.
    

![image.png](attachment:bfa7ec67-e1bb-46d0-8d9a-d7d10e7ff790:image.png align="left")

* **ExStart State**: The Master-slave relationship is negotiated, Where one router becomes the master and initiates databases. The routers with lower Router-ID will become the slave.
    
* **Exchange State**: The routers exchange **DBDs** which contain a list of the **LSAs** in their **LSDB**.
    
* **Loading State:** Routers send **Link State Request(LSR)** messages to request that their neighbours send them any LSAs they don’t have. LSAs are sent in **Link State Update (LSU**) messages.
    
* **Full State:** The Routers are fully synchronized and have established an adjacent. they continue to send and listen for Hello Packets.
    

| ID | Name | Purpose |
| --- | --- | --- |
| 1 | **Hello** | Neighbor Discovery and Maintenance |
| 2 | **Database Description** | Summary of the LSDB of the router. |
| 3 | **Link-State Request (LSR)** | Request specific LSAs from the neighbor. |
| 4 | **Link-State Update (LSU)** | Sends specific LSAs to the neighbor. |
| 5 | **Link -State Acknowledgement(LSAck)** | Used to acknowledge that the router received a massage. |

```jsx
R1(config)# router ospf 1
R1(config-router)# network 192.168.1.1 0.0.0.0 area 0

**VS**

R1(config)# router ospf 1
R1(config-router)# network 192.168.1.0 0.0.0.255 area 0

```

* Use `192.168.1.0 0.0.0.255` when enabling OSPF on all interfaces within the `192.168.1.0/24` subnet.
    
* Use `192.168.1.1 0.0.0.0` when enabling OSPF on a specific interface with the IP address `192.168.1.1`.
    

**Tip**: To reduce manual configuration effort, use **subnet-based configuration** (`192.168.1.0 0.0.0.255`) for a larger network or dynamic environment. For specific control or precision, use **interface-based configuration** (`192.168.1.1 0.0.0.0`).
