Chapters

The default gateway

Chapter 4 ended with a decision: same network, send directly; different network, send to the gateway. This chapter is about that gateway: what it is, where your laptop learns about it, and what happens after the packet reaches it.

Addressing and local delivery · chapter 5 of 34 · 5 min read

Where we are

Each chapter so far uses the one before it, so the order matters:

  1. IP structurech 2
  2. Network vs hostch 3
  3. Subnet maskch 4
  4. Default gatewaych 5 · here
  5. MAC and ARPcoming

The two outcomes of the mask check

Same network

Send straight to the target, using its MAC address (found with ARP, coming soon).

Laptop → printer 192.168.1.60. Easy.

Different network

We have an address, but no direct path to it.

Laptop → kade-api. DNS gave us 34.87.120.15, but that machine is not on our network, so we cannot reach it ourselves.

The first case is solved. The second is the interesting one: how do you send something to an address you cannot reach?

The default gateway

The answer is the default gateway. On a home network, that is your router. When a device does not know how to reach an IP directly, it hands the packet to its default gateway and in effect says: "I don't know where this is, but I assume you do. Please pass it on."

home LAN · 192.168.1.0/24Laptop .23Printer .60local: no gatewayPhone .40Smart TV .52Home routergateway 192.168.1.1the only way outInternetISP + many routerskade-api34.87.120.15

Amber: anything for outside 192.168.1.0/24 goes through one door. Green: local traffic never touches it.

Wide by design - scroll it sideways.

Where the gateway lives: the routing table

Your laptop keeps a small routing table: a list of "for these addresses, send this way". For the home WiFi it has just two lines that matter:

DestinationSend toMeaning
192.168.1.0/24directly, on en0My own networkthe "same network" case from chapter 4
0.0.0.0/0 shown as "default"192.168.1.1Everything elsethe default gateway
0.0.0.0/0 matches every address (chapter 3), so it is the fallback. When several lines match, the most specific one wins: for 192.168.1.60, the /24 line beats the /0 line. This rule is called longest prefix match, and the routing chapter goes deeper.

So the mask check from chapter 4 and this table are two views of the same thing. The table is what the OS actually looks at.

What the gateway does next

The router has a routing table too, and it also has a default gateway: the ISP's router. That router has its own, and so on. Each device only needs to know its next step. Nobody holds the full path.

Laptopgw → 192.168.1.1Home routergw → 203.0.113.1ISP router203.0.113.1More routershop by hopkade-api34.87.120.15The big routers in the middle of the internet have no default gateway at all: they hold a route to every network. That comes in the routing chapter.

Wide by design - scroll it sideways.

The home router's own table looks like this:

DestinationSend toMeaning
192.168.1.0/24directly, LAN sideThe home network
203.0.113.0/24directly, WAN sideThe small network shared with the ISP router
0.0.0.0/0203.0.113.1Everything else: the ISP

The destination IP does not change

A common mistake is to think the laptop sends the packet "to 192.168.1.1". It does not. The packet still says kade-api. Only the frame, the outer wrap from chapter 1, is addressed to the router:

FieldValuePoints to
Destination IP34.87.120.15kade-api, the real targetstays the same the whole way
Destination MAC3c:84:6a:10:ee:01The home router, the next stepchanges at every hop

Where the gateway setting comes from

You do not set it yourself. The laptop got it from DHCP when it joined the WiFi (chapter 3), in the same offer as its IP and mask. It is usually the router's own address, very often the first usable one (.1), though some routers use .254.

One rule is fixed: the gateway must be on your own network. The laptop has to reach the gateway directly, because it is the one step it cannot hand to anyone else. A laptop on 192.168.1.0/24 with its gateway set to 192.168.2.1 cannot send anything outside its network.

When the gateway is the problem

"The printer works but websites don't" is a classic gateway-shaped problem. Check one step at a time, closest first:

#CommandIf it worksIf it fails
1ip routemacOS: netstat -rn · Windows: route printA default via 192.168.1.1 line exists. Go to 2.No default line: the laptop has no gateway. Reconnect so DHCP runs again.
2ping 192.168.1.1The laptop can reach its gateway. Go to 3.Wrong gateway, or the router itself is down.
3ping 1.1.1.1The router can reach the internet. Go to 4.The router's WAN side or the ISP is down. Not your laptop.
4ping api.kade.lkEverything works, including DNS.Only names fail: a DNS problem, not a gateway one.
If even the printer fails and your IP is 169.254.x.x, DHCP never answered (chapter 2), so there is no gateway either.

In the cloud

kade-api's default gateway is 10.10.1.1, one of the 4 addresses GCP keeps in each subnet (chapter 3). GCP hands it out with DHCP, just like the home router. But it is not a box. In GCP there is no physical router and no shared cable: the network is software running on every machine that hosts VMs, and 10.10.1.1 is an address that software answers for. You cannot log in to it or see it. GCP even sets things up so that the VM sends almost everything to the gateway, including traffic to its own subnet (GCP chapter 1.2 shows how).

kade-vpc · 10.10.0.0/16kade-api10.10.1.10 · sn-appGateway 10.10.1.1software, not a boxVPC route table10.10.1.0/24 → the VPC network10.10.2.0/24 → the VPC network0.0.0.0/0 → default-internet-gatewaykade-db10.10.2.5 · sn-data/24 wins8.8.8.8the internetneeds a public address:external IP or Cloud NAT

kade-api hands every packet to 10.10.1.1. The VPC's route table then decides where it really goes: a packet for kade-db stays inside the VPC; a packet for the internet goes to the default internet gateway, and needs a public address to get any further.

Wide by design - scroll it sideways.

The VPC's route table is created for you:

DestinationNext hopMeaning
10.10.1.0/24the VPC networkSubnet route for sn-appadded automatically for each subnet
10.10.2.0/24the VPC networkSubnet route for sn-data
0.0.0.0/0default-internet-gatewayEverything else: the internetthe VPC's "default gateway"

It works like the laptop's table: the most specific match wins. A packet from kade-api to kade-db (10.10.2.5) matches two routes:

#RouteDoes 10.10.2.5 fall in it?Prefix
110.10.1.0/24No
210.10.2.0/24Yes/24
30.0.0.0/0Yes, it matches every address/0

/24 is more specific than /0, so it wins and the packet stays inside the VPC. 0.0.0.0/0 has zero network bits, so it matches every address. That is why it is the default route: it is used only when nothing more specific matches (chapter 12 goes deeper).

On GCP1.2. The VM sends everything to the gateway4. The VPC route table5.1. Firewalls inside a subnet3. How an external IP works7. The full picture

Summary

#IdeaIn one lineNew terms
1The problemWe know the target's IP but it is not on our network
2The answerHand it to the default gateway: "you know the way"default gateway
3Routing tableOwn network → direct; 0.0.0.0/0 → gateway; most specific match winsrouting table, default route, longest prefix match
4A chainEvery router has its own next step; nobody knows the full pathnext hop
5AddressesDestination IP stays kade-api; only the MAC points to the gateway
6SetupComes from DHCP; must be inside your own network
7DebuggingCheck route → gateway → internet → DNS, closest first
8CloudGateway .1 is software; the VPC route table decides; a route, a firewall rule and a public address must all line upsubnet route

Try it yourself

Your gateway from DHCP

ipconfig getoption en0 router

Your routing table

ip route

Every router on the way

  • What to look for: Hop 1 is your gateway (192.168.1.1). Hop 2 is usually the ISP.
  • Runs on: Windows: tracert 1.1.1.1
  • From section: 5. What the gateway does next
traceroute 1.1.1.1

A VPC's route table

  • What to look for: Subnet routes plus the default route with next hop default-internet-gateway.
  • From section: 9. In the cloud
gcloud compute routes list --filter="network:kade-vpc"

Spotted a mistake, or something unclear? Suggest an edit on GitHub