What is a routing table?
A routing table is a data structure that contains information about the network paths available for packet forwarding. It keeps track of the IP addresses, masks, and next hop routers (or gateways) that should be used to forward packets.
Key components of a routing table:
- Destination: The IP address or range of IP addresses that the route applies to.
- Gateway (Next Hop): The IP address of the router that should be used to forward packets to the destination network.
- Interface: The network interface (e.g., eth0, wlan0) that should be used to forward packets to the destination network.
Types of routing tables:
- Kernel routing table: Maintained by the Linux kernel and used for forwarding packets.
- User space routing table: Used by user-space applications, such as
ip route command, to manage routing information.
Example routing table entry:
Suppose we have a network with two subnets:
| Subnet | Gateway |
| --- | --- |
| 192.168.1.0/24 | 10.0.0.254 |
| 10.0.0.0/16 | 10.0.0.253 |
In this example, the routing table has two entries:
- Destination:
192.168.1.0/24
* Gateway:
10.0.0.254 (next hop router)
- Destination:
10.0.0.0/16
* Gateway:
10.0.0.253 (next hop router)
How the routing table is used:
When a packet arrives at an interface, the kernel checks the destination IP address against the routing table entries to determine which next hop router should be used for forwarding.
For example, if a packet with source IP 192.168.1.100 and destination IP 8.8.8.8 arrives on the eth0 interface (which is connected to the subnet 10.0.0.0/16), the kernel will use the routing table entry:
+ Gateway:
10.0.0.253The packet will then be forwarded to the next hop router at IP address 10.0.0.253, which will further forward the packet to its final destination on the global internet.
Note that this is a simplified example, and in real-world scenarios, routing tables can become more complex with multiple routes, different network protocols (e.g., IPv4, IPv6), and various network topologies.