Switch vs router

Last time, DHCP (Dynamic Host Configuration Protocol) handed a device an address and, tucked in alongside it, a default gateway. We swept straight past it to talk about options. Time to go back and ask the obvious question: what does that gateway actually do with your traffic the moment it leaves your device?

Almost everything else in networking quietly assumes you already know. This is the article that stops waving its hands about it.

Switch vs router

The quickest way to keep these straight is to think about where each one makes its decisions.

A switch moves traffic within a single network. Every device it's dealing with is on the same subnet, and it forwards frames using MAC (Media Access Control) addresses at Layer 2.

A router moves traffic between different networks: other subnets, other sites, or out to the internet. It makes its decisions using Internet Protocol (IP) addresses at Layer 3.

That's genuinely the whole distinction. If two devices are on the same subnet, a switch alone gets them talking and a router isn't part of the conversation. The moment a device wants something on a different subnet, which includes anything outside its own virtual LAN (VLAN), it has no choice but to hand the packet to a router and let that router work out the next step.

The default gateway, properly explained

This is what Option 3 from the DHCP article was quietly setting up. The default gateway is simply the router a device should use for anything that clearly isn't on its own local subnet.

Before sending anything, the device compares the destination address against its own address and subnet mask. If the destination is local, it sends the traffic directly. If it isn't, it hands the packet to the gateway and lets that deal with the rest of the journey.

Leave out the gateway, or get it wrong, and a device can talk happily to everything on its own subnet while being unable to reach anything else. That's the classic "the network's fine but nothing works" ticket, and it sends people looking in all the wrong places first.

What's inside a routing table

A router's whole job runs off one structure: the routing table. Every entry is essentially a rule saying that traffic for this network should go out of this interface, towards this next hop.

You can see one on your own machine. On Linux or a Mac, ip route or netstat -rn will show it. On Windows, use route print. A tiny one looks like this:

default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50

Each entry has:

Every table also has a default route, usually written as 0.0.0.0/0. It's the catch-all: if nothing more specific matches, send it here. On a home or office router, that entry is what points towards your internet service provider (ISP), and it's how a router says "I've no specific rule for this, so it's probably headed for the wider internet."

How a router picks a route

This is the part that catches people out the first time. A routing table can contain several entries that all match the same destination: a broad one and a much more specific one. So which wins?

The rule is longest prefix match. The router always prefers the most specific matching route, meaning the one with the longest prefix, over a broader one, whatever the metric says.

Say a table contains these two routes:

A packet for 10.0.5.20 matches both. The /24 is more specific, so it goes via router B, every time. Metric only comes into play as a tiebreaker when two entries are equally specific.

It's how a router can hold a broad default route pointing at the internet plus a handful of specific routes for internal networks, and never mix the two up.

Static vs dynamic routing

There are two ways entries get into that table.

Static routes are configured by hand. They're reliable and predictable, but they don't adapt: if a link goes down, a static route has no idea it should route around the problem.

Dynamic routing uses a routing protocol, with OSPF (Open Shortest Path First) and BGP (Border Gateway Protocol) being the big names. Routers talk to each other, share what they know about the network, and update their tables automatically when something changes, such as a link failing. Those protocols are dense enough to deserve their own articles, so this is only a signpost.

Most home and small-office setups need little more than a single default route, because there's only one sensible way out to the internet. Larger networks with more than one possible path lean on dynamic routing precisely because they need to adapt without a human noticing and fixing it.

Summary

Switches move traffic within a network, and routers move it between networks. Every router relies on a routing table full of "send this destination out this way" entries, and it always prefers the most specific match available. Static routes are hand-configured and simple. Dynamic routing adapts automatically, at the cost of real complexity.

With that in place, we can go back to addressing, because IPv6 (Internet Protocol version 6) doesn't hand out addresses the way DHCP does. Next up: SLAAC vs DHCPv6.

Further reading