# Introduction to Routing
## What is Routing
Routing is the process of selecting the best path for data packets to travel across one or more networks from a source device to a destination device. Routing is performed by routers, which are dedicated network devices operating at the Network layer of the OSI model. Routers examine the destination IP address of each incoming packet, consult their routing tables, and forward the packet to the appropriate next-hop device along the path to the destination. Without routing, communication between devices on different networks would be impossible.
## How Routing Works
When a router receives a packet, it extracts the destination IP address from the packet header. It then searches its routing table for the entry that best matches the destination address. The best match is the entry with the longest network prefix that matches the destination. If multiple entries match, the one with the longest prefix is preferred because it is most specific. This is called longest prefix matching. The router then forwards the packet to the next-hop address or out the interface specified in the matching routing table entry.
## Routing Table
A routing table is a database maintained by each router that contains information about known networks and how to reach them. Each entry in the routing table typically contains the network destination address and prefix length, the next-hop IP address or outgoing interface, a metric indicating the cost of the route, and the source of the route, such as directly connected, static, or a specific routing protocol. Routers populate their routing tables through direct connections, static configuration, or dynamic routing protocols.
## Direct Routing vs Indirect Routing
When the destination is on a directly connected network, the router can deliver the packet directly to the destination device without forwarding it to another router. This is called direct routing. The router uses ARP to determine the MAC address of the destination device and creates a frame addressed directly to it. When the destination is on a remote network, the router must forward the packet to another router that is closer to the destination. This is called indirect routing. The packet is passed from router to router until it reaches a router directly connected to the destination network.
## Administrative Distance
When a router has multiple routes to the same destination from different sources, it uses a value called administrative distance to select the preferred route. Administrative distance represents the trustworthiness of the routing source. Directly connected routes have the lowest administrative distance and are always preferred. Static routes have a low administrative distance. Dynamic routing protocols have varying administrative distances, with interior protocols generally having lower values than exterior protocols.Back to Subject