Labs / Routing / Routes by hand

Lab 4 of 4 · Routes by hand

DHCP relaying: one address server for every hall

Carry a broadcast across a router, so one server feeds every network.

Start this lab 6 steps

The idea

A conference opens the east hall tomorrow, and its badge scanner gets no address, while the main hall's scanner gets one from the same server. Find what the main hall has and the east hall lacks, and get the scanner online before the doors open.

Same request, one hall answered
Same request, one hall answered
The router hears both broadcasts and passes on one
The router hears both broadcasts and passes on one
One more card on the relay's command line
One more card on the relay's command line
One request, two addresses on the router
One request, two addresses on the router
A renewal is routed, a first request is not
A renewal is routed, a first request is not
The relay adds a second note
The relay adds a second note

Steps

  1. Ask for an address

    A machine can't do much on a network until it has an address, and nobody wants to type one into every badge scanner. DHCP, the Dynamic Host Configuration Protocol, does that job instead: a machine wakes up, asks the network for its settings, and a server lends it an address for a fixed time, called a lease.

    This venue runs one such server, addressServer, in the server room next to badgeSystem, the program every scanner reports to. venueRouter joins three networks, mainHall, eastHall and serverRoom, with one network card in each, named after that network. mainScanner and eastScanner are the same model, set up the same way.

    The main hall opened last month and it works. Ask for a lease from mainScanner, then from eastScanner.

    mainScanner:~# dhcp-renew
    eastScanner:~# dhcp-renew
  2. Follow the broadcast

    Put yourself in the scanner's place. It has no address to send from, it doesn't know the server's address, and it has no gateway to hand anything to. It can't write a normal packet, because every field it would need is the thing it's asking for.

    So it shouts. It sends a broadcast from 0.0.0.0, which means "I have no address yet", to 255.255.255.255, which means "everyone on this network". Every machine on the network hears it, and any DHCP server among them answers.

    "Among them" is the catch, because addressServer isn't on the east hall's network. A router forwards a packet by looking up its destination in its routing table, and 255.255.255.255 never gets that far: the standard tells routers to keep such a packet and never pass it on, whatever their table says. Without that rule, every broadcast from every machine would spread across every network in the building.

    The lab records DHCP traffic on venueRouter with tcpdump, a tool that prints the packets a machine's cards see, while eastScanner asks again. Its filter is ports 67 and 68, the two DHCP uses. Then you'll drop mainScanner's lease, so that it starts from nothing as eastScanner does, and ask again while the lab records that too.

    eastScanner:~# dhcp-renew
    mainScanner:~# dhcp-release
    mainScanner:~# dhcp-renew
  3. Relay the east hall

    The relay on venueRouter is a program called dhcrelay, and it takes its settings from its command line, so ask Linux to print that line. Check the other end before you change anything: if addressServer has no addresses for the east hall, a relay won't help. Its settings are in /etc/dnsmasq.conf, because the DHCP server here is a small program called dnsmasq.

    Then fix it. Stop the relay and start it again with the east hall added. The main hall has no relay for those few seconds, and for a reason you'll see shortly, its scanners won't notice.

    After that, ask for a lease on eastScanner and open the badge system.

    venueRouter:~# pgrep -af ^dhcrelay
    addressServer:~# grep east /etc/dnsmasq.conf
    venueRouter:~# pkill dhcrelay
    venueRouter:~# dhcrelay -4 -id mainHall -id eastHall -iu serverRoom 10.0.3.10
    eastScanner:~# dhcp-renew
    eastScanner:~# curl -s -m 3 http://10.0.3.20/
  4. Read the relay's note

    The scanner works, and something about it should bother you. addressServer has one card, in 10.0.3.0/24, and it lent out 10.0.2.10, in a network it has no part in.

    It holds settings for each hall, and every request reaches it through that one card, on behalf of a scanner with no address. So how did it pick the east hall's settings?

    The relay tells it. A DHCP packet has a field for this, called giaddr (gateway IP address) in the standard, and a client always leaves it empty. When the relay passes a request on, it writes in the address of the card that heard the broadcast.

    Hand back eastScanner's lease and ask again, so that a full exchange crosses the relay, while the lab records on addressServer. The -v option makes tcpdump print every field that's filled in, so each packet fills about fifteen lines, and the scanner's Discover comes first.

    eastScanner:~# dhcp-release
    eastScanner:~# dhcp-renew
  5. Stop the relay

    Now picture the relay dying at 2 a.m. Who notices?

    You've already seen the numbers that decide it. In the last recording, the server's offer has RN (58), length 4: 1800 and RB (59), length 4: 3150, the two timers of every lease.

    At half the lease, the renewal time, a machine asks its server for more time. At seven eighths, the rebinding time, if that server still hasn't answered, the machine asks any server that will listen.

    A renewing machine differs from a starting one in the way that matters here. It has an address, a gateway and the server's address, which is everything eastScanner lacked in the first step.

    You're about to stop the relay on purpose, and you'll start it again in the last step. Stop it, renew on eastScanner while the lab records on addressServer, then hand the lease back and ask again, which stands in for a scanner restarting.

    venueRouter:~# pkill dhcrelay
    eastScanner:~# dhcp-renew
    eastScanner:~# dhcp-release
    eastScanner:~# dhcp-renew
  6. Name the card that asked

    The note tells the server which network a request came from, and sometimes that isn't enough. A hotel may put 300 rooms on one network, so Gateway-IP is the same for all of them, and the front desk wants to know which room is asking.

    Option 82, the relay agent information option, is a second note for that. The relay adds it to the request, naming the line the request came in on, and the server can use that to choose settings or to limit each line to one lease. It came from companies selling internet access over cable and phone lines, who need to tie a lease to a customer's physical line.

    Start the relay again, this time with -a, which adds option 82. eastScanner has kept asking all this while, so its own retry may get a lease as soon as the relay is back. Clear whatever it holds and ask again while the lab records on addressServer, so that a full exchange crosses the relay.

    venueRouter:~# dhcrelay -4 -a -id mainHall -id eastHall -iu serverRoom 10.0.3.10
    eastScanner:~# dhcp-release
    eastScanner:~# dhcp-renew

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