LAN NetworksDiagnostics

Default Gateway: How a Device Gets Out of Its Own Network

Local or remote with the AND of address and mask, the routing table with route print and ip route, a gateway on the same subnet, ARP towards the gateway, DHCP option 3, one gateway per VLAN, metrics and static routes, common mistakes.

  • Category: LAN Networks · Diagnostics
  • Published:
  • Editorial team: Alba Technology Academy
  • Reading time: about 13 minutes

The PC prints happily on the network printer, but it cannot open a single website. Or the opposite: it browses the Internet, but cannot reach the server that sits in another VLAN of the same office. The cable is fine, there is an IP address, the switch LED is blinking. In cases like these the culprit is almost always the same: the default gateway, the way out of the local network. Let us see how a device decides whether a destination is near or far, what its routing table contains, why the gateway must be on the same subnet, and which configuration mistakes produce exactly those symptoms.

What the default gateway is

An IPv4 local network is a subnet: a set of addresses sharing the same network part, for example 192.168.1.0/24. Devices on the same subnet talk to each other directly, through the switch or over Wi-Fi. For everything else they need an intermediary: a router, which connects different networks and forwards packets from one to another.

The default gateway is the router to which a device hands every packet addressed to a network it does not know. At home it is the provider's router; in a small business it is often the firewall, for example an OPNsense box whose LAN interface has the address 192.168.1.1. RFC 1122, which sets out the requirements for Internet hosts, describes exactly this behaviour: the host sends packets for its own network directly and entrusts all the others to a gateway.

The name says it all: it is the route the device takes by default, when it has no more precise instructions. On the vast majority of PCs, printers and smartphones it is also the only route configured.

Local or remote: the decision with the mask

Before sending a packet, the device must answer one question: is the destination on my subnet? To answer it, it uses its own address and its own subnet mask, as explained in the article on IP addresses and subnet masks. The operation is a bitwise AND: the bits of the address are compared with those of the mask, and only the bits matching a 1 in the mask are kept. The result is the network address.

Take a PC with the address 192.168.1.20/24, that is with the mask 255.255.255.0. Its network is 192.168.1.0. Let us look at two destinations: the printer 192.168.1.50 and an Internet server, 192.0.2.80.

ValueDecimalBinary
Mask /24255.255.255.011111111.11111111.11111111.00000000
PC192.168.1.2011000000.10101000.00000001.00010100
PC's network192.168.1.011000000.10101000.00000001.00000000
Printer192.168.1.5011000000.10101000.00000001.00110010
Printer AND mask192.168.1.011000000.10101000.00000001.00000000
Server192.0.2.8011000000.00000000.00000010.01010000
Server AND mask192.0.2.011000000.00000000.00000010.00000000
  • Printer: 192.168.1.50 AND 255.255.255.0 = 192.168.1.0, the same network as the PC. The destination is local: the PC reaches it directly.
  • Server: 192.0.2.80 AND 255.255.255.0 = 192.0.2.0, a different network. The destination is remote: the PC hands the packet to the default gateway, 192.168.1.1.

The PC does not know where 192.0.2.80 is, and it does not need to: it only needs to know that it is not on its own network and whom to give the packet to. The rest of the journey is the routers' job.

Local or remote destination The PC 192.168.1.20/24 applies the mask 255.255.255.0 to the destination. For 192.168.1.50 the result is 192.168.1.0, the same as its own network: direct delivery to the printer. For 192.0.2.80 the result is 192.0.2.0, a different network: the packet goes to the gateway 192.168.1.1. PC 192.168.1.20/24 · network 192.168.1.0 destination AND 255.255.255.0 = ? Printer 192.168.1.50 Server 192.0.2.80 192.168.1.50 AND 255.255.255.0 = 192.168.1.0 192.0.2.80 AND 255.255.255.0 = 192.0.2.0 equal to 192.168.1.0 → local ARP for 192.168.1.50 direct delivery through the switch not 192.168.1.0 → remote ARP for the gateway 192.168.1.1 the packet goes to the gateway
Figure 1 — The mask chooses the route: same network, direct delivery; different network, delivery to the gateway.

The PC's routing table

The decision is not written as a single “if”. Every operating system keeps a routing table: a list of destination networks, each with the route to take. For every packet the system looks for the most specific entry containing the destination, that is the one with the longest mask (longest prefix match). The default gateway is the entry 0.0.0.0/0: with a mask of zero bits it contains any address, and it is also the least specific, so it applies only when no other entry matches.

On Windows the table is shown by route print -4. An indicative extract (headings change with the language, metrics with the network adapter):

C:\> route print -4
IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.1.1     192.168.1.20     25
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    331
      192.168.1.0    255.255.255.0         On-link      192.168.1.20    281
     192.168.1.20  255.255.255.255         On-link      192.168.1.20    281
    192.168.1.255  255.255.255.255         On-link      192.168.1.20    281

The first line is the default gateway. The third says that the network 192.168.1.0/24 is On-link, meaning it can be reached directly on the interface, with no gateway. The lines with the mask 255.255.255.255 concern the PC's own address and the network's broadcast address.

On Linux the same content, more compactly, is shown by ip route; ip route get shows which entry would be used for a given destination:

$ ip route
default via 192.168.1.1 dev eth0 proto dhcp metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.20 metric 100

$ ip route get 192.168.1.50
192.168.1.50 dev eth0 src 192.168.1.20 uid 1000
    cache

$ ip route get 192.0.2.80
192.0.2.80 via 192.168.1.1 dev eth0 src 192.168.1.20 uid 1000
    cache

The difference is plain to see: towards the printer there is no via, so delivery is direct; towards the server there is via 192.168.1.1, the gateway. The default entry is simply Linux's name for 0.0.0.0/0.

The gateway must be on the same subnet

The gateway is the first step of the journey, and the PC must be able to reach it directly, with no other router in between. That is why the gateway's address must belong to the device's subnet. With the address 192.168.1.20/24, a gateway of 192.168.1.1 is fine; a gateway of 192.168.0.1 is not, because 192.168.0.1 AND 255.255.255.0 = 192.168.0.0, another network.

Systems react to this mistake in different ways. Linux refuses the command that adds a gateway it cannot reach directly; other systems and some devices accept it, perhaps with a warning, and then fail to use it. The practical result is the same: the local network works, everything else does not.

The gateway usually has the first usable address of the network (192.168.1.1) or the last one (192.168.1.254). That is only a convention: what matters is that the address configured on the clients really is the router's interface address on that network.

ARP towards the gateway: the MAC changes, the IP does not

Once it has decided that the destination is remote, the PC must deliver the packet to the gateway. But on an Ethernet or Wi-Fi network frames are delivered to MAC addresses. So the PC uses ARP to find the gateway's MAC, unless it already has it in its cache, and builds the frame like this:

  • destination MAC: the gateway's, for example 00:00:5e:00:53:01;
  • destination IP: still 192.0.2.80, the final destination.

This point often causes confusion: the PC does not put the gateway's IP address into the packet. The gateway's IP is only used to find its MAC. The firewall receives the frame because it is addressed to its MAC, reads the destination IP, looks up its own routing table and forwards the packet towards the WAN, in a new frame and, on a network with private IPv4 addresses, with the address translation performed by NAT.

The frame towards the gateway The PC 192.168.1.20 sends a frame through the switch to the firewall 192.168.1.1, which forwards it towards the Internet. On the local network the destination MAC is the gateway's, 00:00:5e:00:53:01, while the destination IP is 192.0.2.80. After the firewall the frame is new, and the destination IP is still 192.0.2.80. An animated dot follows the route. PC192.168.1.20 Switch Firewall · gateway192.168.1.1 Internet Local network: frame from the PC to the gateway MAC dst 00:00:5e:00:53:01 (gateway) IP src 192.168.1.20 IP dst 192.0.2.80 (unchanged) After the firewall new frame towards the provider's router IP dst 192.0.2.80 The MAC changes at every hop, the destination IP does not (NAT changes only the source IP).
Figure 2 — Simplified diagram. The PC addresses the frame to the gateway's MAC, but the IP packet is still addressed to the server.

Who provides the gateway: DHCP

On most networks nobody types the gateway in by hand: the DHCP server supplies it, together with the address, the mask and the DNS servers. In the DHCP message the gateway travels in option 3, called Router in RFC 2132, which can also hold several addresses in order of preference. On Windows you read it with ipconfig /all, on the “Default Gateway” line; on Linux with ip route, on the default line.

This has two practical consequences:

  • a mistake in the DHCP configuration spreads to every device that renews its address: a wrong gateway in the DHCP scope leaves the whole office without Internet, a little at a time, as the leases are renewed;
  • devices with a static address (printers, NAS units, cameras, servers) have the gateway written into their own configuration. If the firewall is replaced and its address changes, those devices must be updated one by one.

If no DHCP server answers, Windows assigns itself a 169.254.x.x address with no gateway: the local network only partly works and the Internet cannot be reached.

One VLAN, one subnet, one gateway

When the network is divided into VLANs, each VLAN is a subnet of its own and has its own gateway. A VLAN's gateway is the address of the router's interface in that VLAN. In a small business those interfaces can live in two places:

  • on the firewall, with one VLAN interface for each network over a trunk link to the switch: this is the most common setup with OPNsense, and it allows all traffic between VLANs to be filtered by the rules;
  • on a layer 3 switch, with one virtual interface per VLAN (often called an SVI or VLAN interface): routing between VLANs happens inside the switch, very quickly, and the firewall remains the gateway to the Internet. In this case any filtering between VLANs is done with the switch's access control lists.
VLANUseSubnetGateway
10Offices192.168.10.0/24192.168.10.1
20Servers and printers192.168.20.0/24192.168.20.1
30Guests192.168.30.0/24192.168.30.1

An office PC, 192.168.10.25/24, that wants to reach the server 192.168.20.10 reasons exactly as before: the destination is on another network, so the packet goes to the gateway 192.168.10.1. The router forwards it into VLAN 20. The server's reply takes the opposite route, through its gateway, 192.168.20.1.

One gateway for each VLAN The firewall, or a layer 3 switch, has three interfaces: 192.168.10.1 for office VLAN 10, 192.168.20.1 for VLAN 20 with servers and printers, 192.168.30.1 for guest VLAN 30. The PC 192.168.10.25 uses the gateway 192.168.10.1, the server 192.168.20.10 uses 192.168.20.1. Traffic between VLANs passes through the router, where the rules are applied. Firewall or L3 switch · rules between VLANs 192.168.10.1192.168.20.1192.168.30.1 VLAN 10 · offices PC 192.168.10.25/24 gateway 192.168.10.1 VLAN 20 · servers server 192.168.20.10/24 gateway 192.168.20.1 VLAN 30 · guests phone 192.168.30.40/24 gateway 192.168.30.1 PC → server Each device uses its own VLAN's gateway; inter-VLAN traffic passes through the router.
Figure 3 — Example addresses. Each VLAN has its own subnet and its own gateway, on the same device.

This is where the second symptom from the introduction comes from: “the Internet works, the other VLAN does not”. The gateway is right, because the PC gets out, but something stops the traffic between the networks. The three most frequent causes:

  • the firewall rules allow traffic to the Internet but not to VLAN 20. Often this is intentional: the guest network must not see the servers. If the traffic is needed, add a precise rule, from that network to that server and on those ports;
  • the destination device has no gateway, or the wrong one: it receives the request but does not know how to reply to a network other than its own. This happens with printers and cameras configured by hand without a gateway, which work perfectly from their own VLAN;
  • the operating system firewall on the destination accepts traffic only from the local subnet: some default Windows rules, such as those for file sharing, are restricted in this way.

Several gateways and metrics

A device can have several entries towards the same destination, for example two default gateways on two different network adapters. In that case the metric decides: a number expressing the cost of the route, where the lowest value wins. Windows usually works out the interface metric from the adapter's speed (the Metric column of route print); Linux receives it from the network manager or from DHCP.

If the networks to be reached through a second router are few and known, the clean solution is not a second default gateway but a static route for that network. For example, if the router 192.168.1.254 leads to the network 10.20.0.0/16 of a linked branch office:

C:\> route -p add 10.20.0.0 mask 255.255.0.0 192.168.1.254

$ sudo ip route add 10.20.0.0/16 via 192.168.1.254

The default gateway stays 192.168.1.1 for everything else. On Windows the -p option makes the route persistent; on Linux a route added this way is lost on reboot unless it is written into the network configuration. In many cases it is tidier to add the route on the gateway itself, so the clients need not be touched.

IPv6 has a default gateway too, but it is usually announced by the router with Router Advertisement messages, and its address is a link-local address starting with fe80::. The differences are described in the article on IPv6.

Troubleshooting: four checks

When a device reaches the local network but nothing beyond it, these checks, in this order, almost always pinpoint the problem. The full procedure, from the cable to the browser, is in the article The Internet isn't working: step-by-step troubleshooting.

  1. Read the configuration: ipconfig /all or ip route. Is there a gateway? Is it on the same subnet as the address, with the right mask?
  2. Test the gateway: ping 192.168.1.1. If it does not answer, check arp -a as well: if the gateway appears with its MAC, the device is there and may simply be configured not to answer pings.
  3. Test beyond the gateway: ping 192.0.2.80, a numeric address, to rule out DNS.
  4. Follow the route: tracert 192.0.2.80 (on Linux traceroute). The first hop must be the gateway; if the first hop is an unexpected address, the PC is taking another route. How to read the results is explained in the article on ping and traceroute.

Common mistakes

  • Wrong or missing gateway. A static address configured with an old gateway, for example 192.168.1.254 after the router was replaced by a firewall at 192.168.1.1, or with no gateway at all. The symptom is the classic one: the printer and shared folders on the same network work, the Internet does not. Fix the gateway, or go back to DHCP.
  • A wrong mask that makes a remote address look local. A PC at 192.168.10.25 with the mask 255.255.0.0 instead of 255.255.255.0 considers the whole 192.168.0.0/16 network local. To reach the server 192.168.20.10 it does not go through the gateway: it looks for it with ARP in its own VLAN, gets no answer and fails. The Internet works, on the other hand, because 192.0.2.80 is outside even a /16.
  • Two default gateways on two adapters. A laptop plugged into the office cable and, at the same time, connected to the guest Wi-Fi or to a phone hotspot has two default routes. The lowest metric wins, and it can change with the signal or the adapter's speed: sometimes internal resources can be reached, sometimes not. Keep a single default gateway and, where needed, static routes for the other networks.
  • A gateway on another subnet. Address 192.168.1.20/24 and gateway 192.168.0.1: the PC cannot reach the gateway directly and traffic to the outside never leaves. It often happens after a change of addressing plan carried out only halfway.
  • A firewall that does not route between VLANs. The VLAN interfaces exist, but the rules allowing traffic between them are missing, or the destination device has no gateway to reply with. A ping to the firewall's interface in the other VLAN works, a ping to the device does not. On OPNsense rules are set per interface, and a newly created interface allows no traffic until a rule is added to it (menus may vary with the version).
  • Confusing the gateway with DNS. At home the router often acts as both gateway and DNS server on the same address. If a ping to a numeric address works and a ping to a name does not, the gateway is fine and the problem is DNS.

Practical takeaways

  • Remember the rule: same subnet, direct delivery; different subnet, delivery to the gateway. The mask decides.
  • Always check address, mask and gateway together: the gateway must be on the same subnet as the address.
  • Let DHCP hand out the gateway and, for devices with a static address, record the gateway in the network documentation.
  • With VLANs, give every network its own gateway on the firewall or on the layer 3 switch, and write explicit rules for traffic between the networks.
  • Keep one default gateway per device; for other networks use static routes, preferably on the gateway.
  • When troubleshooting, go in order: configuration, ping to the gateway, ping to an external numeric address, traceroute.

Readers who want to go deeper into addressing, routing and VLANs will find these topics developed step by step in the volumes of the RETI LAN series.

The video

Default Gateway: How a Device Gets Out of Its Own Network — Animation without voice-over, with on-screen text in English and sound effects.

Technical sources

  • RFC 1122 — Requirements for Internet Hosts: Communication Layers — Host behaviour when sending packets: direct delivery to destinations on its own network, delivery to a gateway for the others, choice of the default gateway.
  • RFC 4632 e RFC 950 — Classless Inter-domain Routing (CIDR); Internet Standard Subnetting Procedure — Subnet mask, prefix notation and longest prefix match when choosing a route.
  • RFC 826 — An Ethernet Address Resolution Protocol — Resolution of the gateway's MAC address before frames are sent towards off-subnet destinations.
  • RFC 2132 — DHCP Options and BOOTP Vendor Extensions — Option 3 (Router): list of routers on the client's subnet, in order of preference.
  • RFC 4861 — Neighbor Discovery for IP version 6 — In IPv6, routers announce themselves with Router Advertisement messages and the default gateway is a link-local address.
  • Documentazione dei sistemi operativi — route e ipconfig (Windows), ip-route (Linux, iproute2) — Reading the routing table, adding static routes and interface metrics. Output format, headings and metric values depend on the system, its language and its version.
  • RFC 1918, RFC 5737 e RFC 7042 — Addresses used in the examples: private networks 192.168.x.0/24 and 10.20.0.0/16, the documentation address 192.0.2.80 and MAC addresses 00:00:5e:00:53:xx reserved for documentation.

Standards and official documentation available from the respective organisations.

OPNsense® is a registered trademark. Alba Technology does not represent the OPNsense project.