Default Gateways
Published: 2023-11-12 • Reading time: 6 min
#networking #routingWhen I talked about IP addresses, I mentioned default gateways, but didn't go into any details about them at the time. The default gateway is one of the three basic network interface configuration elements of IP networking along with the IP address and the subnet mask. A default gateway is usually, but not always necessary to have a working IP network configuration. As I stated last time, you might know you need to configure a default gateway, but not why.
The default gateway is a host, identified by its IP address, on the local network to which you send packets when the destination address is not on the local network. The default gateway will then, hopefully, forward the packets toward the destination network. But what does all that mean?
I covered IP addresses and subnet masks first, because I think you need to understand a subnet mask to fully understand how a network host figures out what to do with the default gateway. As I mentioned in the IP addresses post, an IP address actually contains two parts:
- Network number
- Node number
The subnet mask is used to determine how to divide the 32-bit IP address into the two parts.
Within a local area network (LAN), all the hosts should be configured with the same network number and have a unique node number. If your local network is 192.168.1.0/24
, the network number is 192.168.1
and the final octet indicates the node number.
Local Network Communication
Let's start an example with two hosts:
- Host A
- IP Address =
192.168.1.10
- Subnet Mask =
255.255.255.0
- IP Address =
- Host B
- IP Address =
192.168.1.20
- Subnet Mask =
255.255.255.0
- IP Address =
Both hosts are using a subnet mask of 255.255.255.0
, so they are both on the 192.168.1
network. Being on the same network, these two hosts can communicate with each other without the help of any intermediate host.
Here's a diagram of network Network 192.168.1, Host A, and Host B:
flowchart TD N1(Network 192.168.1) --- |.10| A[Host A] N1 --- |.20| B[Host B]
Non-local Network Communication
Let's add another host to the first two:
- Host Z
- IP Address =
192.168.2.10
- Subnet Mask =
255.255.255.0
- IP Address =
Host Z is configured with the subnet mask 255.255.255.0
, but notice that the network number is 192.168.2
and the node number is 10
. This means that Host Z is on a different network than Host A and Host B. If it wasn't on a different network, it's node number would conflict with Host A. Two hosts on the same network with the same node number cannot communicate and typically interfere with one another in such a way that neither can communicate reliably.
Here's an updated diagram with the Network 192.168.2 added:
flowchart TD N1(Network 192.168.1) --- |.10| A[Host A] N1 --- |.20| B[Host B] N2(Network 192.168.2) --- |.10| Z[Host Z]
Note that there's currently no path from Host A or Host B to communicate with Host Z. For Host A or Host B to communicate with Host Z we need a couple of additions. First, an intermediate system is required to route packets between the networks 192.168.1
and 192.168.2
as those two networks aren't connected. Next, Host A and Host B need to know how to send packets to other networks.
Adding an Intermediate System
Let's solve the first problem by adding a new intermediate system between the two networks. We'll call this Router 1. It will need an IP address on each of the two networks. We'll give it node number .1
for each network.
- Router 1
- Network 192.168.1
- IP Address =
192.168.1.1
- Subnet Mask =
255.255.255.0
- IP Address =
- Network 192.168.2
- IP Address =
192.168.2.1
- Subnet Mask =
255.255.255.0
- IP Address =
- Network 192.168.1
Let's add Router 1 to the diagram:
flowchart TD R1((Router 1)) --- |.1| N1 R1 --- |.1| N2 N1(Network 192.168.1) --- |.10| A[Host A] N1 --- |.20| B[Host B] N2(Network 192.168.2) --- |.10| Z[Host Z]
At this point, Host A, Host B, and Host Z still don't know how to communicate outside of their local network. They don't know about Router 1. In order for the hosts to talk to hosts outside of their local network they must minimally have a default gateway configured. The default gateway is also called the gateway of last resort. A host will send packets to the default gateway when it doesn't have any other more specific routing information configured.
Adding a Default Gateway
Let's update the configuration on Host A and give it a default gateway of 192.168.1.1
:
- Host A
- IP Address =
192.168.1.10
- Subnet Mask =
255.255.255.0
- Default Gateway =
192.168.1.1
- IP Address =
When Host A needs to send packets to Host Z, the IP stack in Host A, lacking more specific routing information, will send the packets to the default gateway Router 1. Router 1 will forward the packets from Host A to Host Z. The default gateway configured on Host A has served it's purpose.
There's only one problem. We didn't give Host Z a default gateway. So while Host A can send data to Host Z, there's not enough configuration on Host Z for it to send replies to Host A. Likewise, Host B also cannot communicate with Host Z.
Finishing the Host Configuration
In order to complete the host configuration, all the hosts need to have a default gateway.
- Host A
- IP Address =
192.168.1.10
- Subnet Mask =
255.255.255.0
- Default Gateway =
192.168.1.1
- IP Address =
- Host B
- IP Address =
192.168.1.20
- Subnet Mask =
255.255.255.0
- Default Gateway =
192.168.1.1
- IP Address =
- Host Z
- IP Address =
192.168.2.10
- Subnet Mask =
255.255.255.0
- Default Gateway =
192.168.2.1
- IP Address =
Now, as long as Router 1 is kind enough to forward packets between Network 192.168.1 and Network 192.168.2 the three hosts can freely communicate back and forth.
Summary
So now you can see what a default gateway does: it tells a hosts where to send network packets when it's not on your local network. A default gateway is a minimum configuration element for internetwork routing with Internet Protocol. What I haven't yet discussed but I mentioned was "more specific routing information." A default gateway is the last resort when you don't have better information. Some other time I'll discuss how you can get more specific routing information.