Default Gateway Working Principle, Bitwise AND Calculation and ARP

A client must determine whether the destination is in its own network before it sends data to an IP address. If the destination is in the same subnet, the data is sent directly. If it is in a different subnet and no more specific route is defined for the destination, the packet is delivered to the default gateway defined on the client. Below, I use scenarios to explain how this decision is made on the IPv4 side, how the ARP process is shaped by this decision and how Windows runs the same calculation in the background.

The decision is based on the bitwise AND calculation performed between the IP address and the subnet mask. If both corresponding bits are 1, the result is 1; in all other cases it is 0. The client first ANDs its own IP address with its own subnet mask and obtains the network ID of the subnet it is connected to. It repeats the same operation for the destination IP address, again using its own subnet mask. The client does not know the subnet mask of the destination; its own mask is used on both sides of the calculation.

If the 2 results are the same, the destination is in the same subnet. The client resolves the MAC address of the destination with ARP and forwards the data directly at Layer 2. If the results are different, the destination is in another subnet and the data cannot be sent directly. If no separate route is defined for the destination, the packet goes to the default gateway first.

The default gateway is the first routing point that the client uses to reach destinations outside its own network. This address is usually the IP address assigned to the LAN interface of a router or firewall device, and it is in the same subnet as the client. When the client sees that the destination does not match its own subnet, it sends the packet to this interface. The device that receives the packet determines the best route to the destination and the egress interface from its own routing table. If traffic leaving for the internet matches a source NAT rule on the device, the source private IP is translated to the public IP specified in the rule and the packet is sent to the external network. Traffic that does not match the rule is routed without any change to its addresses.

Default gateway

The Routing Table and the Default Route on the Router Side

The router or firewall device defined as the default gateway is more than an exit address for the client; it is the point where routing decisions are made. The device compares the destination IP address of every incoming packet with the routes installed in the routing table. If the destination matches more than 1 route, the route with the longest prefix is selected, that is, the route that describes the destination with the narrowest scope. This rule is called longest prefix match.

Installing a route in the table and forwarding a packet over the installed routes are separate decisions. In Cisco IOS, if the same destination prefix is learned from different sources, for example from both a static route and a routing protocol, administrative distance determines which source is installed in the table, and the source with the lowest value is preferred. The metric is the value that each protocol uses to select the best path among its own routes; the metric values of different protocols cannot be compared with each other. If ECMP is enabled, multiple equal-cost next hops to the same destination can be used together.

The default route is defined as 0.0.0.0/0. Because its prefix length is 0, it matches every destination IP address, but because it is the least specific route in the table, it is used when no more specific match exists. As a result, traffic to destinations that do not match a more specific route is forwarded over the default route.

Defining a default gateway on the client side is not enough on its own. The device that receives the packet must also have a correctly configured routing table with which it can evaluate the destination. If the destination matches no route and no default route is defined, the router drops the packet and, in most cases, an ICMP Destination Unreachable message is returned to the source.

The Difference and Relationship Between LAN and WAN

A LAN (Local Area Network) is the local network structure in which the devices in a building or on a campus communicate at high speed and with low latency. A LAN can consist of a single subnet, or it can contain multiple subnets separated by VLANs. Clients in the same subnet have the same network ID and communicate through the switch by using MAC addresses. This communication takes place at Layer 2, and address resolution is done with an ARP broadcast.

A WAN (Wide Area Network) is a network environment with a wider coverage area that connects independent LANs to each other. When a client in the LAN wants to reach a destination on the WAN, it first sends the data to its own default gateway. Routing rules and access control take effect at the transition between the LAN and the WAN. NAT, however, is not applied at every transition; it is applied when the traffic matches a NAT rule. All of these operations are performed on routing devices such as a router or firewall, not on the client.

A Virtual Local Area Network (VLAN) is a separate broadcast domain at Layer 2. Devices in the same VLAN and the same IPv4 subnet communicate directly without using a gateway. In the common design, a separate IP subnet is assigned to each VLAN, and a Layer 3 gateway is defined in the relevant VLAN so that the clients can reach the other subnets. For example, if 192.168.10.1 is used for VLAN 10 and 192.168.20.1 for VLAN 20, communication between these 2 VLANs takes place through a device that can perform routing. These addresses are defined as an SVI (Switch Virtual Interface) on a Layer 3 switch and as a subinterface on a router. Whether the destination is on the WAN or in a neighbouring VLAN, the rule does not change for the client: if the destination is outside its own subnet and there is no more specific route, the packet goes to the default gateway.

The client uses the default route when there is no more specific route for the destination. If the default gateway address that this route points to is wrong, access over that path fails. The client's communication with the devices in its own subnet is not affected, and neither are the destinations it reaches through another valid route, for example the directly connected route of a second interface or a route added by a VPN connection.

Definitions alone are not enough to see how this structure works. In the scenarios below, I examine step by step how the relationship between the client and the default gateway works within the same network and between different networks.

⚡ Scenario-1: Default Gateway Process in the Same Network

1- The IP address of the PC-1 computer is 10.10.10.72, and the IP address of the PC-60 computer that it wants to communicate with is 10.10.10.107. PC-1 puts both IP addresses through a bitwise AND operation with its own subnet mask of 255.255.255.0 and checks whether the result is the same as the network ID written in the routing table below.

Default gateway

Bitwise AND Calculation

Bitwise AND is a bit-level comparison method used to determine which network ID an IP address belongs to. The IP address and the subnet mask are written one below the other in binary format, and the corresponding bits are put through the AND operation. If both bits are 1, the result is 1; in all other cases it is 0. The resulting binary value gives the network ID that the address belongs to.

The operating system uses this result to decide whether the packet stays within the local network (LAN) or leaves through the default gateway. The calculation is performed by the TCP/IP stack, which runs in kernel mode. If the destination is in the same network, the MAC address of the destination is resolved through ARP and communication is established directly. Otherwise, the packet is forwarded to the default gateway. The NIC (Network Interface Card) does not take part in this decision; the network stack determines the route, the next hop and the destination MAC address, and the NIC transmits the data delivered to it onto the physical medium.

Now I use an example to show step by step how the IP addresses are ANDed with the subnet mask and which network ID is produced. Readers who want to examine the deeper technical side of the topic can also read my article titled Network ID Calculation with Bitwise AND.

Value 1st octet 2nd octet 3rd octet 4th octet
PC-1 IP address 10 10 10 72
PC-1 binary value 00001010 00001010 00001010 01001000
PC-60 IP address 10 10 10 107
PC-60 binary value 00001010 00001010 00001010 01101011
PC-1 subnet mask value 255 255 255 0
Subnet mask binary value 11111111 11111111 11111111 00000000

1.1- I write these binary values one below the other and perform the AND operation. The rule is always as follows:

IP bit Subnet mask bit AND result
1 1 1
1 0 0
0 1 0
0 0 0

1.2- The bitwise AND calculation below produces the value 10.10.10.0 for both the 10.10.10.72 and 10.10.10.107 IP addresses. Both addresses are in the 10.10.10.0/24 network, that is, in the same network. The subnet mask used in the PC-60 calculation is PC-1's own subnet mask value, not the subnet mask of PC-60.

PC-1 bitwise AND operation 1st octet 2nd octet 3rd octet 4th octet
IP address 00001010 00001010 00001010 01001000
Subnet mask 11111111 11111111 11111111 00000000
Result 00001010 00001010 00001010 00000000
Network ID 10 10 10 0
PC-60 bitwise AND operation 1st octet 2nd octet 3rd octet 4th octet
IP address 00001010 00001010 00001010 01101011
Subnet mask (PC-1) 11111111 11111111 11111111 00000000
Result 00001010 00001010 00001010 00000000
Network ID 10 10 10 0

2- As a result of this bitwise AND calculation, PC-1 determined that it is in the same network as PC-60. In our scenario, PC-1 communicates with PC-60 for the first time, so its ARP cache has no entry for this address. PC-1 therefore creates an ARP request. It writes 10.10.10.107, the IP address of PC-60, in the Target IP field of the ARP message. Because it does not yet know the MAC address it is looking for, the Target MAC field of the ARP message remains 00:00:00:00:00:00. The Destination MAC field of the Ethernet frame that carries the message is set to FF:FF:FF:FF:FF:FF, which is the broadcast MAC address.

Default gateway

PC-1 ARP request frame contents
Default gateway

2.1- PC-1 decides that the destination is in its own network according to the route in row 2 of the routing table. This route shows that the 10.10.10.0/24 network can be reached directly, without using the default gateway.

Default gateway

3- Because the ARP request was sent as a broadcast, it reached all devices in the same network. PC-60 accepted the ARP request because the Target IP field contained its own address, 10.10.10.107. The same frame also reached the interface that has the default gateway IP address, 10.10.10.254. The default gateway received the frame, but because the address in the Target IP field did not belong to it, it discarded the frame without generating a reply.

Default gateway

4- PC-60 sends the ARP reply message that contains its own MAC address directly to PC-1 as a unicast, not as a broadcast. PC-1 saves this information in its ARP cache and forwards the data packet directly, with the MAC address of PC-60 written in the Destination MAC field. The default gateway is not used at any stage in this scenario.

⚡ Scenario-2: Default Gateway Process in a Different Network #1

1- In this scenario, PC-1, which has the IP address 10.10.10.72 in the 10.10.10.0/24 network, communicates this time with PC-55, which has the IP address 172.16.0.70 in the 172.16.0.0/24 network. PC-1 again puts the destination IP address through a bitwise AND operation with its own subnet mask value, 255.255.255.0, and compares the result with its own network ID.

Default gateway

2- The bitwise AND calculation gives the result 172.16.0.0 for the destination. Because PC-1's own network ID is 10.10.10.0, PC-1 concludes that it is not in the same network as PC-55. In this scenario, PC-1 has already written the MAC address of the default gateway to its ARP cache. It therefore prepares the data packet without needing the ARP process. It writes the IP address of PC-55 in the Destination IP field of the IP header and the default gateway MAC address in the Destination MAC field of the Ethernet frame, and it sends the packet to the default gateway so that the packet can leave its own network.

PC-1 data packet frame contents
Default gateway

PC-1 ARP cache contents
Default gateway

2.1- PC-1 decides that it must forward the data packet to the default gateway according to the route in row 1 of the routing table. The default route is used because the destination matches none of the more specific routes in the table.

Default gateway

3- After the data packet reaches the LAN interface of the firewall or router device, all remaining processing belongs to this device. The device looks up the destination IP address in its own routing table and determines the egress interface. When it forwards the packet, it decreases the TTL value in the IP header by 1 and rewrites the Layer 2 header. The Source MAC field of the new frame contains the MAC address of the device's own egress interface, and the Destination MAC field contains the MAC address of the destination or of the next router. If the packet does not match a NAT rule, the Source IP and Destination IP fields do not change along the path.

The destination network can be a different network separated by a VLAN, or it can be a different network on the internet. The behaviour here is determined by the routing and access rules written on the routing device.

Default gateway

Default gateway

⚡ Scenario-3: Default Gateway Process in a Different Network #2

1- The difference between this scenario and the previous one is that the ARP cache of PC-1 is empty. This lets me cover how the communication between the client and the routing device proceeds before the data packet is forwarded to the destination network. PC-1, which has the IP address 10.10.10.72 in the 10.10.10.0/24 network, again communicates with PC-55, which has the IP address 172.16.0.70 in the 172.16.0.0/24 network, and it checks whether the destination is in its own network with the same bitwise AND method.

Default gateway

2- As a result of the bitwise AND calculation, PC-1 concludes that it is not in the same network as PC-55, which has the IP address 172.16.0.70. It must send the packet to the default gateway, but the MAC address of the default gateway is not recorded in its ARP cache. PC-1 therefore creates an ARP request. The point to note here is that the Target IP field of the ARP message contains the IP address of the default gateway, not the IP address of PC-55. The Destination MAC field of the Ethernet frame is set to FF:FF:FF:FF:FF:FF, which is the broadcast MAC address.

PC-1 ARP request frame contents
Default gateway

PC-1 ARP cache contents
Default gateway

2.1- The ARP request is created for the default gateway and not for PC-55 because of the default route in row 1 of the routing table. This route shows the default gateway as the next hop on the path to the destination, and PC-1 must resolve the MAC address of this hop.

Default gateway

3- Because the Destination MAC field of the frame contains the broadcast address, the switch forwards the ARP request to all devices in the same network.

Default gateway

Default gateway

4- The default gateway accepted the request because the Target IP field of the ARP request contained 10.10.10.254. The other devices in the network discarded the frame without generating a reply because the address did not belong to them.

Default gateway

5- The default gateway responded to the ARP request with an ARP reply. With this reply, PC-1 learned the default gateway MAC address and saved it in its ARP cache.

Default gateway ARP reply frame contents
Default gateway

PC-1 ARP cache contents
Default gateway

6- PC-1 has completed the ARP process in its own network and can now send the data packet. It writes the IP address of PC-55, which is in the 172.16.0.0/24 network, in the Destination IP field of the IP header and the default gateway MAC address that it has just learned in the Destination MAC field of the Ethernet frame, and it sends the packet to the default gateway. From this point, the process continues in the same way as in Scenario-2.

PC-1 data packet frame contents
Default gateway

7- The firewall or router device that receives the data packet on its LAN interface looks up the destination IP address in its own routing table, rewrites the Layer 2 header and forwards the packet to the destination network, as I described in step 3 of Scenario-2.

Default gateway

Default gateway

In all 3 scenarios, the deciding factor is the subnet comparison made at the IP level, not whether the destination is reachable at that moment. The routing decision is made first by looking at the IP header, and then the Ethernet frame that fits this decision is created. If the destination is in a different subnet, the Destination MAC field of the frame contains the MAC address of the default gateway, not of the destination device. The Destination IP in the IP header shows the actual destination in every case. Layer 3 makes the decision, and Layer 2 carries the packet to the next hop according to this decision.

Now let us look more closely at which configuration data Windows uses for this calculation and how it uses that data when it makes the routing decision.

The Bitwise AND Calculation Process in Windows

In the scenarios, I described the decision as a comparison of 2 network IDs. Windows reaches the same result through the routing table. In the route determination process that Microsoft describes, the destination IP address is put through a bitwise AND calculation with the netmask value of the routes in the table. If the result is the same as the Network Destination value of the route, that route is considered a match for the destination. Among the matching routes, the route with the most 1 bits in its netmask is selected, that is, the route that describes the destination with the narrowest scope. If there is a tie, the route with the lowest metric value is used.

When an IP address and subnet mask are assigned to the client, Windows automatically adds an on-link route for this subnet to the routing table. Row 2 in Scenario-1 is this route. If the destination matches this route and the table has no other route that describes the destination with a narrower scope, the packet is sent directly without using the default gateway. The default gateway definition is added to the table as the 0.0.0.0/0 route. Because the result is 0.0.0.0 when any IP address is ANDed with 0.0.0.0, this route matches every destination, but it is selected when no more specific route exists.

The selected route gives 2 pieces of information: the next address to which the packet is sent (the next hop) and the interface to use. For an on-link route, the next hop is the destination itself; for the default route, it is the default gateway. If the ARP cache has no MAC entry for the next hop address, the ARP process is triggered and the MAC address is resolved.

Windows does not repeat this lookup from the beginning for every packet. The next hop and interface information determined for a destination is written to the destination cache (called the route cache in older Microsoft documentation), and this entry is used for subsequent packets to the same destination. You can view the entries with the netsh interface ipv4 show destinationcache command.

1. Persistent IP Configuration Is Stored in the Registry

When an IP address, subnet mask or default gateway is defined manually on a NIC from the connection properties, or when this information comes through DHCP, the values are kept in the registry under the following path:


HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
You can copy this path and paste it directly into Regedit to access the {GUID} values directly.

The ANDing process in Windows

Under this path there is a separate {GUID} key for each network interface. In a manual configuration, the values are kept under the names IPAddress, SubnetMask and DefaultGateway. The values that come from DHCP are kept under the names DhcpIPAddress, DhcpSubnetMask and DhcpDefaultGateway. The registry here is the place where the configuration is stored; the routing decision is made from the active configuration of the system, not from these entries.

With the PowerShell command below, you can display the IP, default gateway and DNS information of the interface you select. The command does not read the registry directly; it takes the data from the active TCP/IP configuration. The second part returns the GUID value of your network interface card and shows which key you need to look at in the registry.


Get-NetIPConfiguration -InterfaceAlias "Wi-Fi"; (Get-NetAdapter -InterfaceAlias "Wi-Fi").InterfaceGuid
Replace the "Wi-Fi" value in the commands with the name of your own adapter. You can list the adapter names with the Get-NetAdapter command.

The ANDing process in Windows

This command does not show the subnet mask value as a separate field. To see the prefix length of the IPv4 address, you can use the command below. If the PrefixLength value in the output is 24, this corresponds to the subnet mask value 255.255.255.0.


Get-NetIPAddress -InterfaceAlias "Wi-Fi" -AddressFamily IPv4 | Select-Object IPAddress,PrefixLength

2. The Component That Makes the Decision: tcpip.sys

In Windows, the TCP/IP protocols are implemented by the kernel-mode protocol driver named tcpip.sys. The file is located on disk in the C:\Windows\System32\drivers directory.

The ANDing process in Windows

Windows keeps the IP configuration at 2 separate levels: persistent (PersistentStore) and active (ActiveStore). The persistent configuration is preserved across restarts and is copied to the ActiveStore when the computer restarts. The active configuration is the one used in route selection.

3. How Is This Information Kept and Used in RAM?

The active configuration consists of the information that the running system uses at that moment, such as the IP address, prefix length, default gateway and interface index. The routing table, ARP cache and destination cache are also part of the running system, and the routing decision is made from this data.

The active and persistent configurations do not always have to be the same. For example, when the New-NetIPAddress command is run with the -PolicyStore ActiveStore parameter, the address is added to the active configuration and is not stored persistently. This address is used in route selection even though it is not in the persistent records.

4. The Routing Decision and the Bitwise AND Operation Start Exactly Here

When an application wants to send data to an IP address over TCP, the OSI transport layer creates a TCP segment. The OSI network layer encapsulates this segment in an IPv4 packet and determines the route to use for the destination. Whether the destination is in the same network becomes clear at this stage. In the example below, the IP address of the client is 10.10.10.10, the subnet mask value is 255.255.255.0 and the destination IP address is 10.10.20.5.

Value IP address Subnet mask Bitwise AND result
source_ip 10.10.10.10 255.255.255.0 10.10.10.0
destination_ip 10.10.20.5 255.255.255.0 10.10.20.0

If the results are equal, the destination is in the same network. The destination matches the on-link route. The ARP process starts for the destination itself, and the MAC address of the destination is resolved.

If the results are different, the destination is in another network. In this example, 10.10.10.0 and 10.10.20.0 do not match. If no more specific route is defined for the destination, the default route is selected and the MAC address of the default gateway is resolved with ARP.

You can use the command below to see which route and which source IP address Windows selects for a specific destination. If the NextHop value in the output is 0.0.0.0, the destination is on-link and the packet is sent directly. If it shows an IP address, the packet is delivered to that address, that is, to the default gateway or to another defined router.


Find-NetRoute -RemoteIPAddress "10.10.20.5"

5. The NIC Is Not a Decision Maker at Any Point in This Process

The NIC (Network Interface Card) is not involved in any of the routing decisions described above. The TCP/IP stack determines the route, the next hop and the destination MAC address. The NIC transmits the data delivered to it onto the physical medium.

Operation Performed by
Route selection for the destination IP address and determination of the next hop Operating system (TCP/IP stack)
Resolution of the next hop MAC address with ARP Operating system (TCP/IP stack)
Determination of the destination MAC address Operating system (TCP/IP stack)
If Large Send Offload is enabled, splitting the large TCP packet into segments and creating the headers of each segment from the template NIC
Transmission of the frame onto the physical medium NIC

When Large Send Offload is enabled, the TCP/IP stack delivers a single large TCP packet to the NIC. The NIC splits this packet into segments and creates the MAC, IP and TCP headers of each segment by using the headers in the delivered packet as a template. With Checksum Offload, the checksum calculation is left to the hardware. These features are intended to reduce the load on the processor and do not change the routing decision: the Destination IP and Destination MAC values in the template are already written according to the decision made by the TCP/IP stack.

The bitwise AND calculation is the first step of this decision process. Before the packet leaves the computer, the operating system uses the active configuration and the routing table entries to calculate where the destination is and determines the next hop. The NIC is the last component, and it carries the result of this decision onto the physical medium.

This article covered how a client decides, based on the destination IP address, whether to send the packet directly or through the default gateway, the bitwise AND calculation, how the ARP process proceeds in same-network and different-network scenarios, and how Windows makes this decision through the routing table.

I hope you find it useful...

No comments on this article yet. Be the first to comment!

750 characters available.
Security code
Comments are published once approved.
Your email is only used to let you know when your comment is approved. It is never published.