Labs / IP Addressing & NAT / Addresses and subnets

Lab 2 of 5 · Addresses and subnets

Subnet masks: why a machine on the same switch is out of reach

Find the setting that decides who a machine counts as a neighbor.

Start this lab 7 steps

The idea

Priya plugs a new label printer in right beside the till, the bakery's cash register, but the till can't reach it. Find the setting that stops the till, and print the first label.

The ping stops inside the till
The ping stops inside the till
A mask cuts addresses into blocks
A mask cuts addresses into blocks
Add the new mask, then remove the old
Add the new mask, then remove the old
The question arrives, and no answer comes back
The question arrives, and no answer comes back
Both masks agree, so the label prints
Both masks agree, so the label prints
A narrow mask and a gateway make a detour
A narrow mask and a gateway make a detour
Count in steps of 16
Count in steps of 16

Steps

  1. Ask the till to reach the label printer

    Every machine on a network has an IP address, four numbers that name it. The till's is 10.0.1.10, the label printer's is 10.0.1.70, and one Switch joins them. A switch is a box that moves traffic between the machines plugged into it.

    ping sends a test message, called a packet, and waits for a reply. Type it in the till's terminal, the window where you type commands to a machine.

    -c 1 sends one packet, and -W 2 waits 2 seconds for the reply. The lab records what reaches the label printer.

    till:~# ping -c 1 -W 2 10.0.1.70
  2. Read the till's subnet mask

    An address looks complete, but it holds two things: which network a machine is on, and which machine it is. A phone number works the same way, with an area code followed by the line. Nothing in the four numbers says where the first part stops, so each machine needs a second setting to mark the split: the subnet mask.

    The mask cuts addresses into blocks, each called a Subnet and prefix length, and a machine talks directly only to its own block. In this lab, block and subnet mean the same thing.

    Read the mask on eth0, the till's network card, the part that joins a machine to a network: look for Mask: in the output. Then read the till's routes, its list of where it can send traffic, with ip route. Finish with ipcalc, a calculator for addresses, on the label printer's address.

    till:~# ifconfig eth0
    till:~# ip route
    till:~# ipcalc -n 10.0.1.70 255.255.255.192
  3. Give the till a wider mask

    You've found the fault: the till's block stops at .63, and the label printer is at .70.

    The mask 255.255.255.0 makes one block of all 256 addresses, which holds both. In bits it's 24 ones and 8 zeros, so ip writes it as /24. A smaller number after the slash means a wider block.

    On a real network, a Dynamic Host Configuration Protocol (DHCP) server, the machine that hands out addresses, usually sets the mask. A machine with a fixed address, like a till or a printer, often gets its mask typed in once by whoever sets it up. So a wrong mask can sit there until somebody reads it.

    ip can't edit a mask in place, but a card can hold several addresses. Add the till's address again with the wider mask, then remove the copy with the old mask.

    till:~# ip -echo addr add 10.0.1.10/24 dev eth0
    till:~# ip -echo addr del 10.0.1.10/26 dev eth0
  4. Try the label printer again

    The till now counts .70 as a neighbor, so it will send this time. A plain switch, the kind in a home or a small shop, doesn't read IP addresses, so reaching a neighbor takes one more stage. The switch moves traffic by a different name, the hardware address each network card carries, which ifconfig showed after HWaddr.

    So the till must find the label printer's hardware address first, using the Address Resolution Protocol (ARP). The till sends the question to every machine on the switch: who has 10.0.1.70? The machine that holds the address replies with its hardware address, and only then can the ping go.

    If nobody answers, the till asks up to three times, a second apart. The lab records what reaches the label printer. Then read what the till learned with ip neigh, which lists the hardware addresses a machine has found.

    till:~# ping -c 1 -W 2 10.0.1.70
    till:~# ip neigh show 10.0.1.70
  5. Fix the other end and print a label

    The label printer's block still runs from .64 to .127. People forget this rule: every machine on one network needs the same mask.

    Take two neighbors that disagree, with no gateway. The one with the narrower mask can't answer a machine outside its block, but it still answers those inside. So .70 on /26 would work with .80 and fail with .10, and the fault looks random.

    Give the label printer the wide mask the same way: add first, then remove. Then ask for a label from the till. curl fetches a web page and shows it as text, and this printer prints a label when anything opens its /print page.

    labelPrinter:~# ip -echo addr add 10.0.1.70/24 dev eth0
    labelPrinter:~# ip -echo addr del 10.0.1.70/26 dev eth0
    till:~# curl -s http://10.0.1.70/print
  6. Watch the till choose who to ask

    A mask does a second job: it tells a machine what is far away. For every packet, the till checks its routes. With one card, that comes down to one question: is this address in my block?

    If it is, the till asks for that machine on the switch. If it isn't, the till hands the packet to its default gateway, the router that carries traffic out of the network. On an ordinary network, a gateway must sit inside the machine's own block, or the machine can't reach it.

    The flour supplier at 10.0.2.80 is beyond bakeryRouter. The lab gives the till a default gateway, 10.0.1.2, which is bakeryRouter. Open the supplier's page, and the lab records who the till asks for.

    Then the lab puts the till's old /26 mask back for one test, and leaves the gateway in place. You are about to repeat half of the first fault, because the label printer keeps /24. Ask the till how it would reach the label printer with ip route get, then ask for a label.

    till:~# curl -s http://10.0.2.80/
    till:~# ip route get 10.0.1.70
    till:~# curl -s http://10.0.1.70/print
  7. Read a mask you haven't seen

    You don't need to write bits every time, because a rule gives the same answer:

    1. Read the mask from the left and find the first number that isn't 255.
    2. Subtract that number from 256, and you have the block size in that place.
    3. Start at 0 and go up by that size until you pass the address. The last start at or below the address opens the block, and the next start minus 1 ends it.

    Priya's new shelf scale comes set to 10.0.1.35, with the mask 255.255.255.240. Work out its block before she plugs it in, then check your answer with ipcalc. -b adds the block's last address.

    Her card company then sends a card reader set to 10.0.37.5, with the mask 255.255.240.0. This mask ends in 0, which is why the rule starts from the left. Run ipcalc on that one too.

    till:~# ipcalc -n -b 10.0.1.35 255.255.255.240
    till:~# ipcalc -n -b 10.0.37.5 255.255.240.0

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab