Sub topic 4
How a client talks before it has an address: UDP 68 to 67 and broadcast
Prerequisites
This chapter assumes you know the following:
- IPv4 addresses and subnet masks.
- What a default gateway and a DNS server do.
- What a LAN, a router and a broadcast are.
- The basic client-server model.
It also assumes you have read the previous chapters. They explain that a host needs an address, mask, gateway and DNS server, and that DHCP hands these out as leases. This chapter covers the transport problem underneath: how a DHCP message travels when the sender has no usable IP address and does not know the server's address.
The bootstrapping problem
A normal IP packet needs a source address and a destination address. A host that has just joined a network has neither a configured address nor the address of a DHCP server. Two questions follow:
- What source address does the client put in its first packet?
- Where does it send that packet?
DHCP answers both with conventions that are valid at the IP layer but only used in this situation:
- The client uses
0.0.0.0as its source address. This means "this host, on this network, address unknown". It is never valid as a destination. - The client uses
255.255.255.255as its destination. This is the limited broadcast address. Routers do not forward it, so it reaches every host on the local link and nothing beyond.
Any DHCP server on the same link receives the packet. The client never has to know who the server is.
Why UDP and not TCP
DHCP runs over UDP. TCP needs a handshake between two known unicast endpoints, and a client without an address cannot take part in one. TCP also cannot broadcast. UDP is connectionless, so a single datagram can be sent to the broadcast address.
The cost is that UDP gives no delivery guarantee. DHCP handles this itself. A client that gets no answer retransmits, and it waits a growing, randomized interval between attempts. This avoids a burst of simultaneous retries after a power outage, when many clients restart at once. RFC 2131 suggests starting at about 4 seconds, doubling each time up to 64 seconds, and adding a random offset of up to one second.
The two well-known ports
DHCP uses two fixed UDP ports, inherited from BOOTP:
| Role | UDP port | Used by |
|---|---|---|
| Server (and relay agent) | 67 | Listens here for client messages |
| Client | 68 | Listens here for server replies |
The client sends from source port 68 to destination port 67. The server normally replies from port 67 to destination port 68.
Using a fixed client port is unusual, since most clients pick a random ephemeral port. It is needed here because the server may reply by broadcast, and a broadcast reaches every host on the link. Each host's network stack must know which port to deliver it to. Port 68 tells the stack the datagram is for a DHCP client process.
What the first packet looks like
The first message is a Discover. Its IP and UDP headers look like this:
| Field | Value |
|---|---|
| Source IP | 0.0.0.0 |
| Destination IP | 255.255.255.255 |
| Source UDP port | 68 |
| Destination UDP port | 67 |
| Layer 2 source | the client's MAC address |
| Layer 2 destination | ff:ff:ff:ff:ff:ff |
The layer 2 destination is the Ethernet broadcast address. A switch floods a frame with this destination out of every port in the same VLAN, which is how the frame reaches the server.
The DHCP payload carries the identity of the client. It is the old BOOTP message format with a variable options area added. Two fields matter for this chapter:
chaddrholds the client hardware address. The server uses it, with the client identifier option if present, to know who is asking.xidis a random 32-bit transaction ID that the client picks. Every reply in the same exchange carries the same value, so the client can match replies to its own request and ignore replies meant for other clients.
The broadcast flag: how the server replies
A reply poses the reverse problem. The server wants to send the client an offered address, but the client does not own that address yet. The server cannot rely on normal ARP resolution for an address the client has not configured.
The client chooses between two reply styles using the flags field of its request. Its top bit is the BROADCAST flag.
- Flag clear (0): the server may send the reply as a unicast frame. The IP destination is the offered address (
yiaddr), and the Ethernet destination is the client's MAC address fromchaddr. The server writes this ARP-free entry directly. Only the target client's NIC accepts the frame, which reduces noise on the link. - Flag set (1): the server must broadcast the reply to
255.255.255.255(or the subnet broadcast address) at layer 2ff:ff:ff:ff:ff:ff. Every host sees it, but only the one whosexidandchaddrmatch uses it.
Why would a client set the flag? Some IP stacks cannot accept a unicast IP packet addressed to an address that is not yet configured on the interface. They discard it before the DHCP client sees it. Such clients set the flag and receive broadcasts instead. A client that can receive unicast before configuration leaves the flag clear.
Where the exchange stays unicast
Not every DHCP message is a broadcast. After the client has a lease, it knows the server's address and holds a valid IP address of its own. The renewal request at T1 is sent as a unicast from client port 68 to server port 67. The details are in the lease lifecycle chapter. The broadcast and 0.0.0.0 behavior is limited to the period when the client has no configuration, and to rebinding at T2, when the original server has stopped answering and any server may respond.
A diagram of the first exchange
Seeing it on the wire
You can capture this traffic with the command used throughout the tutorial:
sudo tcpdump -n -i eth0 -vv 'udp and (port 67 or port 68)'The -n flag turns off name resolution, -i eth0 selects the interface, and -vv prints the decoded DHCP fields. The filter matches either port, so it catches both client and server traffic. With a client booting in another terminal, a Discover line looks like this in outline:
IP 0.0.0.0.68 > 255.255.255.255.67: BOOTP/DHCP, Request from aa:bb:cc:dd:ee:ff, length 300
Client-Ethernet-Address aa:bb:cc:dd:ee:ff
DHCP-Message Option 53, length 1: DiscoverCheck three things: the source is 0.0.0.0 on port 68, the destination is 255.255.255.255 on port 67, and the message type option says Discover. Tcpdump labels the BOOTP operation as "Request" because BOOTP's op field has only two values, request and reply. The DHCP message type option (53) is what tells Discover, Request and Inform apart.
Consequences and gotchas
Broadcasts stop at the router
A limited broadcast never crosses a router. If the DHCP server sits on a different subnet from the client, the Discover will not reach it. Two solutions exist: run a server on every subnet, or configure a relay agent on the router. The relay listens on port 67, then forwards the request by unicast to the server. Relay agents have their own chapter.
The server may be listening on the wrong interface
A server has to bind to the interface that faces the clients. In the dnsmasq example from this tutorial, interface=eth0 does this. If the server listens on another interface, it never sees the Discover, and the capture on the client side shows Discovers with no replies.
Host firewalls
A host firewall on the server must allow inbound UDP 67. A firewall on a client must allow inbound UDP 68, or replies will be dropped. Because the client has no address when the first reply arrives, rules that match on the client's IP address will not work for this step. Match on the port and interface instead.
Port 68 is exclusive on many systems
Because the client port is fixed, only one DHCP client process can usually bind it for a given interface. Running two DHCP clients on the same machine, such as a leftover dhclient alongside NetworkManager's built-in client, can cause one to fail or to steal the other's replies. Check for duplicates if a Linux machine misbehaves.
Virtual machines and bridges
Bridged virtual machines send their own Discover with their own virtual MAC address, and the bridge floods it. This is why a VM on a bridged adapter can receive an address from the office DHCP server. On a Wi-Fi interface some hypervisors rewrite the MAC or block the broadcast, so the VM then gets no offer.
Summary table
| Question | Answer |
|---|---|
| Client source IP before configuration | 0.0.0.0 |
| First message destination IP | 255.255.255.255 |
| Client port | UDP 68 |
| Server and relay port | UDP 67 |
| First message layer 2 destination | ff:ff:ff:ff:ff:ff |
| What matches a reply to a request | xid, plus chaddr |
| What controls the reply style | BROADCAST flag in `flags` |
| Does it cross a router? | No, not without a relay agent |
Exercise
A client's Discover leaves its network card, but a server on the next subnet never answers. A tcpdump on the client shows 0.0.0.0.68 > 255.255.255.255.67 repeated with growing gaps, and no replies. Give the cause, explain why the gaps grow, and name one fix.
Show answer
The destination 255.255.255.255 is a limited broadcast, which routers do not forward, so the Discover never leaves the client's subnet and the server on the other subnet cannot see it. The gaps grow because the client retransmits with exponential backoff (about 4 s, 8 s, 16 s and so on, up to 64 s, with a random offset) to avoid flooding the network. Fixes: place a DHCP server on the client's subnet, or configure a relay agent (for example ip helper-address on the router interface) that forwards the request by unicast to the server.
Exercise
Why does the client use the fixed source port 68 instead of a random ephemeral port, and what does the BROADCAST flag change about the server's reply?
Show answer
Replies may be broadcast and so reach every host on the link, and each host must know which port to deliver the datagram to. A fixed port 68 identifies it as a DHCP client message. The BROADCAST flag tells the server how to address the reply. With the flag clear, the server may unicast the reply to the offered address and the client's MAC address. With the flag set, the server broadcasts the reply, which is needed for clients whose IP stacks drop unicast packets sent to an address they have not configured yet.