Sub topic 10
Relay agents: one server for many subnets
This chapter assumes you know how the Discover, Offer, Request, Acknowledge (DORA) exchange works. It also assumes you understand IPv4 subnets and masks, and that routers do not forward broadcasts by default. You should also know what a scope (an address pool for one subnet) is.
The problem: broadcasts stop at the router
A DHCP client that has no address sends its Discover as a broadcast to 255.255.255.255 from source 0.0.0.0, using UDP port 68 to port 67. A broadcast like this stays inside the local network segment (the broadcast domain). A router drops it.
That is a problem when a network has many subnets. Suppose you have a VLAN for staff, one for guests and one for printers, each with its own subnet. A DHCP server sits on a server subnet. Clients in the other VLANs cannot reach it with a broadcast.
There are two ways to solve this:
- Run a DHCP server in every subnet. This works, but you then manage many servers, many configurations and many sets of logs.
- Run one central server (or a redundant pair) and have a relay agent carry the requests across the routers.
A relay agent is a device or service on the client's subnet that listens for DHCP broadcasts, converts them into unicast packets, and sends them to a known DHCP server. It also carries the server's replies back. The relay is usually a feature of the router or layer 3 switch that is already the default gateway for the subnet. On Cisco devices the feature is called ip helper-address. Other vendors use names such as "DHCP relay" or "DHCP forwarding". On Linux you can run dhcrelay from ISC DHCP.
How the relay changes the exchange
The client does not know a relay exists. It sends the same broadcast Discover as before. The relay then does the following:
- It receives the broadcast on the interface that faces the client.
- It fills in the giaddr field (gateway IP address) in the DHCP message with its own IP address on that interface. This is the address the relay holds on the client's subnet.
- It sends the message as a unicast UDP packet from its own address to the server's address, using destination port 67.
- The server reads giaddr and uses it to decide which scope to allocate from.
- The server sends its reply by unicast to the giaddr address, on port 67.
- The relay receives the reply and delivers it to the client, as a broadcast or as a unicast to the client's hardware address, depending on the broadcast flag in the message.
The giaddr field is the key piece. A DHCP message has no other field that tells the server where the client is, because the client's own source address is 0.0.0.0 during the first exchange.
How the server picks a scope
The server compares giaddr to the scopes it has configured. If giaddr is 10.0.2.1 and one scope covers 10.0.2.0/24, the server allocates from that scope. It also uses that scope's options, so the client gets the right mask, router and lease time for its own subnet.
The router option (option 3) in each scope should normally match the relay's address on that subnet, since the relay is usually the gateway. A common mistake is copying a scope and forgetting to change the router option.
If the server has no scope that contains giaddr, it cannot serve the request. Most servers log this and stay silent, so the client sees no Offer.
Here is a dnsmasq example. One server serves two subnets. It does not listen on the client subnets directly. It receives relayed packets.
# /etc/dnsmasq.conf (central server, 10.0.0.10)
interface=eth0
# Scope for subnet 10.0.1.0/24 (staff VLAN)
dhcp-range=set:staff,10.0.1.100,10.0.1.200,255.255.255.0,12h
dhcp-option=tag:staff,option:router,10.0.1.1
dhcp-option=tag:staff,option:dns-server,10.0.0.53
# Scope for subnet 10.0.2.0/24 (guest VLAN)
dhcp-range=set:guest,10.0.2.100,10.0.2.200,255.255.255.0,1h
dhcp-option=tag:guest,option:router,10.0.2.1
dhcp-option=tag:guest,option:dns-server,10.0.0.53dnsmasq picks the matching range from the giaddr of a relayed request, so each client gets options that fit its subnet. Notice that the guest scope uses a shorter lease.
Configuring a relay
On a Cisco router or layer 3 switch, you set the helper address on the interface that faces the clients:
interface Vlan20
ip address 10.0.2.1 255.255.255.0
ip helper-address 10.0.0.10On Linux, run the ISC relay and name the server and the interface it should listen on:
sudo dhcrelay -i eth1 10.0.0.10Here eth1 is the interface that faces the clients and 10.0.0.10 is the DHCP server. The relay host must have an IP address on that subnet, and it must have a route to the server.
You can list more than one server address. The relay then sends a copy of each request to every server. This gives redundancy, and the client uses the first Offer it accepts.
A warning about `ip helper-address` on Cisco
By default ip helper-address forwards more than DHCP. It also forwards several other UDP broadcast services, such as TFTP, DNS, NetBIOS name service and TACACS. If you only want DHCP, restrict the forwarded ports with no ip forward-protocol udp for the services you do not need.
Relay information: option 82
A relay can add extra data to the request. This is option 82, the DHCP relay agent information option. It contains sub-options. Two common ones are:
- Circuit ID: identifies the port or VLAN on which the request arrived.
- Remote ID: identifies the relay device itself, for example its MAC address or hostname.
The server can use this information to make decisions. For example, it can give a fixed address to whatever device is plugged into a specific switch port, or it can log where a lease was issued. The server copies option 82 into its reply so that the relay can use it to deliver the reply to the right port, then removes it before the client sees it.
Option 82 is also a security issue. If a client sends its own option 82, a relay should not trust it. Switches that add option 82 usually drop incoming packets from client ports that already contain it. This is covered under DHCP snooping in a later chapter.
What happens after the lease is granted
The relay matters mostly for the first exchange and for rebinding. After the client has an address, it knows the server's address from option 54 (server identifier). At T1 it sends its renewal as a unicast directly to the server. The router forwards this like any other IP packet, so no relay is needed.
At T2, the client broadcasts a rebinding request to any server. This request does need the relay, because it is a broadcast again. A relay must therefore stay configured for the whole life of the network, not just for the first boot.
Lease-time arithmetic is unaffected by the relay. A 24-hour lease still gives T1 at 43200 s and T2 at 75600 s.
| Message | Client to server path | Needs relay? |
|---|---|---|
| Discover | Broadcast | Yes |
| Request (selecting) | Broadcast | Yes |
| Request (renewing at T1) | Unicast to server | No |
| Request (rebinding at T2) | Broadcast | Yes |
| Release | Unicast to server | No |
Common failures
| Symptom | Likely cause |
|---|---|
| Client gets a 169.254.x.x address | Relay is missing, server is unreachable, or the server has no scope for giaddr |
| Client gets an address from the wrong subnet | giaddr matches the wrong scope, or the relay is set on the wrong interface |
| Client gets the right address but cannot leave the subnet | Router option in the scope does not match the gateway |
| Server sees requests but the client never gets an Offer | The return path is blocked, for example a firewall dropping UDP 67 to the relay |
| Two servers answer | A second server exists on the client subnet, or the relay lists two servers and both reply |
To diagnose, capture on both sides of the relay. On the server, you should see unicast packets with a non-zero giaddr:
sudo tcpdump -n -i eth0 -vv 'udp and (port 67 or port 68)'In the verbose output, look for a line such as Gateway-IP 10.0.2.1. If it shows 0.0.0.0 for a request that came from another subnet, the packet was not relayed. If you see no packets at all, check the route and any firewall between the relay and the server.
Design decisions
- Central server with relays: fewer systems to manage, one place for reservations and logs. The server and the routers become critical. Run two servers using failover or split scopes.
- Server per subnet: no dependency on relays or on the WAN. Suitable for small branch offices, where a router can also act as the DHCP server.
- Router as relay at branch sites: a branch router forwards requests to a data center server. Clients cannot get leases while the WAN link is down, although existing leases keep working until they expire. Use longer leases at such sites, or place a local server there.
Exercise
A client on subnet 10.0.5.0/24 uses a relay at 10.0.5.1 and a central server at 10.0.0.10. The server has scopes for 10.0.1.0/24 and 10.0.2.0/24 only. What does the client get, and what would you check on the server and why?
Show answer
The client gets no Offer and eventually falls back to a 169.254.x.x address. The server has no scope that contains giaddr (10.0.5.1), so it cannot allocate an address. Check the server log and a packet capture to confirm it received the relayed Discover with giaddr 10.0.5.1. Then add a scope for 10.0.5.0/24 with router option 10.0.5.1.
Exercise
A client holds a 24-hour lease from a server across a router. Which of its messages need the relay, and at what times?
Show answer
The initial Discover and Request need the relay because they are broadcasts. The renewal at T1 (43200 s) is a unicast to the server, so it does not need the relay. The rebinding at T2 (75600 s) is a broadcast, so it needs the relay again.