Networking for Security, lesson 4 of 12
Subnets and gateways
Lesson 3 gave every device an IP address. That address has two halves: which network, and which device on it. A second setting, the subnet mask, says where the halves divide, and a third, the gateway, says where to send anything meant for somewhere else. Get these three right and a device can talk to the world. Get one wrong and it can talk to its neighbors only, or to nobody.
Neighbor or stranger?
A camera at 192.168.1.10 has a packet to send. Change where it is going and the camera's settings, and watch the decision.
The everyday picture
Think of a telephone number such as 415 555 0142. The first three digits are the area code. If you are calling someone with the same area code, the local exchange connects you directly. If the area code is different, the local exchange cannot reach that phone itself, so it hands the call to a long-distance line, which knows the way.
Now imagine that the number of digits in the area code could vary from town to town. To place a call you would need one more fact: how many of the leading digits are the area code.
An IP address works like this. The area code is the network part. The setting that says how long the area code is, is the subnet mask. The long-distance line is the gateway.
The subnet mask
A subnet is one local network: a group of devices that can reach each other directly through switches, with no router in between. All of them share the same network part in their addresses.
The subnet mask marks how much of the address is the network part. It looks like an address, for example 255.255.255.0, but it is really a ruler. Lesson 3 showed that each of the four numbers is eight bits. In a mask, a bit set to 1 means "this bit belongs to the network part". The number 255 is eight ones. So 255.255.255.0 says: the first three numbers are the network, and the last number is the device.
There is a shorter way to write the same thing. Count the ones and put the count after a slash. 255.255.255.0 has 24 ones, so it is written /24 and read "slash twenty-four". The trade calls this number the prefix length. You will see addresses written as 192.168.1.10/24, which gives the address and the mask in one go.
| Mask | Short form | Addresses in the subnet | Usable by devices |
|---|---|---|---|
| 255.255.0.0 | /16 | 65,536 | 65,534 |
| 255.255.254.0 | /23 | 512 | 510 |
| 255.255.255.0 | /24 | 256 | 254 |
| 255.255.255.128 | /25 | 128 | 126 |
| 255.255.255.192 | /26 | 64 | 62 |
| 255.255.255.224 | /27 | 32 | 30 |
| 255.255.255.240 | /28 | 16 | 14 |
Each step down the table moves the dividing line one bit to the right and halves the subnet. The /24 is by far the most common on security jobs, because the dividing line falls neatly on a dot and the sums are easy.
What is inside a subnet
Take the subnet that holds 192.168.1.10 with a /24 mask. It runs from 192.168.1.0 to 192.168.1.255, which is 256 addresses. Two of them are reserved:
- The first, 192.168.1.0, is the network address. It is the name of the subnet itself, used in plans and router settings. No device may have it.
- The last, 192.168.1.255, is the broadcast address. A packet sent to it goes to every device on the subnet, as you saw in lesson 1. No device may have it either.
That leaves 192.168.1.1 to 192.168.1.254, or 254 addresses for devices. The rule for any subnet is the same: count the addresses, then subtract two.
Here is a smaller one, worked through. With a /26 mask, 26 bits are network and 6 are left for devices. Six bits can count from 0 to 63, so each subnet holds 64 addresses. The subnets therefore start at 0, 64, 128 and 192. The address 192.168.1.100 falls in the one that starts at 64. Its network address is 192.168.1.64, its broadcast address is 192.168.1.127, and devices may use .65 to .126, which is 62 addresses.
Check yourself
A recorder is 10.20.30.5 with mask 255.255.255.0. Which of these devices is on its subnet?
The gateway
Before a device sends any packet, it runs one test. It lays its mask over its own address and over the destination address, and compares the network parts. The drawing at the top shows this test bit by bit.
- The network parts match. The destination is a neighbor. The device finds its MAC address with ARP and sends the packet through the switch, as in lesson 3.
- They differ. The destination is somewhere else. The device cannot reach it directly, so it sends the packet to the default gateway and trusts it to pass the packet on.
The default gateway is simply the address of the router on this subnet. A router is a device with a connection to two or more networks, and an address on each. It reads the destination IP address of each packet and sends it out toward the right network. By custom the router takes the first usable address in the subnet, such as 192.168.1.1, though some sites use the last. Never assume. Ask.
Two rules follow. The gateway address must be inside the device's own subnet, or the device could never reach it. And a device with no gateway set can still talk to all its neighbors perfectly well. It only fails when something from outside the subnet tries to hold a conversation with it, because its replies have nowhere to go.
So every network device needs three settings that agree with each other and with the site:
| Setting | Example | If it is wrong |
|---|---|---|
| IP address | 192.168.1.10 | The device is unreachable, or it clashes with another device |
| Subnet mask | 255.255.255.0 | The device misjudges who is a neighbor. Some destinations work and others mysteriously do not |
| Default gateway | 192.168.1.1 | Local devices work. Anything on another subnet, or remote viewing, fails |
Why networks are divided
A building could put every device in one enormous subnet. There are three good reasons not to.
- Broadcasts. Every broadcast reaches every device in the subnet, and each one must stop and read it. Routers do not pass broadcasts on. Smaller subnets mean less background chatter for each device.
- Control. Traffic between subnets has to pass through a router, and a router or firewall can be told what to allow. Putting cameras in their own subnet gives the site a single checkpoint between the cameras and the office computers. Lesson 8 builds on this.
- Order. A plan such as "cameras are 10.10.20.x, door controllers are 10.10.30.x" tells you what a device is from its address alone, and makes faults quicker to find.
Size a subnet
To choose a subnet, count the devices, allow for growth, add one for the gateway, and pick the smallest size that fits. Remember the two reserved addresses.
How big a subnet does this system need?
Enter the devices that will share one subnet.
On a customer's network you do not choose the subnet. The network's owner gives you one. Your job is to tell them how many addresses you need, including growth, before they pick.
Three examples
Simple: works here, not there
A new camera at 192.168.1.50 shows a perfect picture on the recorder in the same closet. The manager's computer, on the office subnet 192.168.5.0/24, cannot open it. The camera's gateway setting had been left blank. Requests from the manager's computer reach the camera through the router, but the camera sees a reply address outside its own subnet, has no gateway to hand it to, and drops the reply. Entering 192.168.1.1 as the gateway fixes it.
Medium: the mask that was too generous
A door controller at 192.168.1.30 must report to an access control server at 192.168.2.20 in the next building. It never connects, though the controller reaches devices near it. Its mask had been typed as 255.255.0.0.
With that mask the controller compares only the first two numbers, 192.168, sees a match, and decides the server is a neighbor. It asks for the server's MAC address by broadcast. The broadcast stops at the router, nobody answers, and the gateway is never tried. Correcting the mask to 255.255.255.0 makes the controller see that 192.168.2 differs from 192.168.1, and it sends the packet to the gateway as it should. Try this case in the drawing at the top.
Advanced: the subnet that filled up
A campus video system was given 192.168.10.0/24 years ago. It now has 236 cameras, 6 recorders, 4 workstations and the gateway: 247 of 254 addresses. A new wing needs 40 more cameras.
There are two honest routes. The network owner can widen the subnet to a /23, which holds 510 devices, but then every existing device needs its mask changed, and all 290 devices hear each other's broadcasts. Or the new wing gets its own subnet, say 192.168.11.0/26 with 62 usable addresses, and the router joins the two. The second route touches nothing that already works and keeps a fault in one wing from disturbing the other. It was the one chosen. The cost is that the recorders now reach those cameras through the router, so the router must be able to carry the video. Lesson 7 does that sum.
From the field
- Get all three settings in writing from whoever owns the network: the address range you may use, the mask and the gateway. Do not copy them from a nearby device and hope.
- Ping the gateway first. When a device cannot be reached from elsewhere, check from a laptop on its subnet that the gateway answers. If it does not, the fault is not in your device.
- Set your laptop into the device's subnet to configure a new unit at its factory address. Give the laptop a spare address with the same network part, make the change, then set the laptop back to automatic.
- Keep the ends of the range free. Leave .1 and .254 for routers and switches even if nobody has claimed them yet.
- Watch for the same subnet at two sites. If two buildings both use 192.168.1.0/24 and are later joined, neither can reach the other. Ask about future links before choosing a range.
Common mistakes
- Leaving the gateway blank because "it works on the recorder", then finding remote viewing fails.
- Accepting a device's default mask without checking it against the site's.
- Giving a device the network address or the broadcast address of its subnet.
- Entering a gateway that is not in the device's own subnet.
- Filling a subnet to the last address, with no room for a replacement unit to be set up beside the old one.
- Assuming that devices on two subnets can talk just because they share a switch. Without a router between them, they cannot.
Summary
- A subnet is a group of devices that reach each other directly. They share the network part of their addresses.
- The subnet mask says how many leading bits are the network part. 255.255.255.0 is the same as /24.
- The first address in a subnet names the network and the last is the broadcast. Devices use the rest: 254 in a /24.
- A device sends to neighbors directly and sends everything else to its default gateway, the router on its subnet.
- Address, mask and gateway must agree with each other and with the site.
- Dividing a network limits broadcasts, creates a checkpoint between groups of devices, and keeps the plan readable.
Exercises
Answer each one yourself first, then open the answer.
Exercise 1. A camera is 10.10.20.77 with mask 255.255.255.0. Give its network address, its broadcast address, and the range devices may use.
Show answer
Exercise 2. A system has 50 cameras, 2 recorders and a gateway, and should allow 25 percent growth. Will a /26 do? If not, what is the smallest subnet that will?
Show answer
Exercise 3. With a /26 mask, are 192.168.1.60 and 192.168.1.70 on the same subnet?
Show answer
Exercise 4 (think, don't memorize). A recorder can be viewed from every computer in its own building, but not from head office, which is on a different subnet. Head office can view other recorders at the same site. Which setting on the recorder do you check first, and why does the fault show only from far away?