Another role of the network layer is to provide routing of packets between hosts. A host could send a packet to:
- Itself - This is a special IP address of 127.0.0.1 which is referred to as the loopback interface. This loopback address is automatically assigned to a host when TCP/IP is running. The ability for a host to send a packet to itself using network functionality is useful for testing purposes. Any IP within the network 127.0.0.0/8 refers to the local host.
- Local host - This is a host on the same network as the sending host. The hosts share the same network address.
- Remote host - This is a host on a remote network. The hosts do not share the same network address.
Hosts require a local routing table to ensure that network layer packets are directed to the correct destination network. The local table of the host typically contains:
- Direct connection - This is a route to the loopback interface.
- Local network route - The network which the host is connected to is automatically populated in the host routing table.
- Local default route - The default gateway is added to the host routing table and represents all other routes. The default gateway is either configured manually or learned dynamically and creates the local default route.
The default gateway is the device that routes traffic from the local network to devices on remote networks. It is often used to connect a local network to the Internet. The network is determined by the IP address and subnet mask combination. If a host is sending a packet to a device that is configured with the same IP network as the host device, the packet is simply forwarded out of the host interface to the destination device directly. A default gateway is not needed in this situation.
However, if the host is sending a packet to a device on a different IP network, then the host must forward the packet to the default gateway, because a host device cannot communicate directly with devices outside of the local network. The default gateway, which is most often a router, maintains routing table entries for all of its directly connected networks, as well as entries of remote networks it may know about. The router is responsible for determining the best path to reach all of those destinations. The default gateway address is the IP address of the router’s network interface connected to the local network.
The figure highlights areas associated with the entries in the local host routing table. In this example, the default gateway address is: 192.168.10.1.