Dynamic Host Control Protocol (DHCP) is used to provide devices in a network with the configuration parameters. Typically devices need configuration to operate, such as IP address, DNS server address, NTP server details, etc. DHCP allows the devices to talk to a central server, called as DHCP server, and retrieve the configuration from the server. These devices are known as DHCP clients.
Retrieving configuration from a centralized server has the advantage of having to make changes in only one location. Each device does not have to be manually configured. This becomes important when there are large number of devices in a network or devices may connect to the network temporarily. However, having a central server for configuration also introduces a single node of failure. Though there can be multiple servers configured, so that if one server goes down other one can pick up the slack.
A DHCP client can request for the configuration when it boots up or can request for the configuration when it needs to connect to the network. The way DHCP protocol has been defined, the client does not need to know the details of the server. A DHCP client will broadcast a request on its subnet when it needs information, and one or more DHCP servers present in the network will respond back.
Usually, the DHCP server is also built such a way that it also retains information across reboots, so if goes down and is restored back up the DHCP client information is not required to be configured again.
DHCP protocol is based on UDP protocol with port 67 as the DHCP server port and port 68 as the DHCP client port.
Though various parameters can be configured in DHCP, but the most important parameter that is configured by DHCP is IP address. DHCP supports three mechanisms for IP address allocation - dynamic, automatic, and static allocation.
A DHCP message has the following format.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| op (1) | htype (1) | hlen (1) | hops (1) |
+---------------+---------------+---------------+---------------+
| xid (4) |
+-------------------------------+-------------------------------+
| secs (2) | flags (2) |
+-------------------------------+-------------------------------+
| ciaddr (4) |
+---------------------------------------------------------------+
| yiaddr (4) |
+---------------------------------------------------------------+
| siaddr (4) |
+---------------------------------------------------------------+
| giaddr (4) |
+---------------------------------------------------------------+
| |
| chaddr (16) |
| |
| |
+---------------------------------------------------------------+
| |
| sname (64) |
+---------------------------------------------------------------+
| |
| file (128) |
+---------------------------------------------------------------+
| |
| options (variable) |
+---------------------------------------------------------------+
The DHCP message contains several fields for specifying the configuration. However, in order to avoid modifying the core DHCP message everytime a new configuration parameter is required, a variable length field "options" was added to the end of DHCP message. In the options field various the parameters may be specified. Each option has a code, and this code appears in the options field followed by its value. More than one option can be specified in the options field. If a new configuration parameter needs to be added, then a new (unused) code is assigned to it. Existing options and their formats are defined in RFC2132
One such options that is mandatory in the DHCP message is - "DHCP message type" which enables the client and server to determine the DHCP message type. DHCP defines following message types:
Another option that is mandatory to be sent by servers is server identifier, which is an identifier that uniquely identifes the server.
When the client receives a DHCPACK message with the configuration parameters, it verifies the parameters. It is possible that the IP address assigned to it is already being used by some other client (which can be determined by sending a arp request.) In this case the client which received this invalid configuration will send DHCPDECLINE message to DHCP server and restart the configuration process starting from DHCPDISCOVER.
The client may choose to relinquish its configuration parameters. This is possible in scenario when a client is shutting down, or getting disconnected from the network and it wants to tell the server that it can assign its configuration/ip address to some other client. In this case the client will send DHCPRELEASE message to the server. Once, a client has released a configuration/IP-address it needs to reacquire the configuration parameters it needs to use them.
It is possible that a client remembers a previous configuration and may request the DHCP server to provide the same configuration again. In this scenario it may skip some of the configuration messages:
A typical network may have hunderds of devices being managed by the same DHCP server. When a client sends a message, it needs to send an unique identifier so that the server can know which client is sending the request. The first thing that comes to mind is that the hardware identifier (such as mac address) can be used to identify the client. If the DHCP client chooses to identify itself with the hardware address, then it needs to fill this value in "chadd" (client hardware address) of the DHCP message. On the server side the configuration would be provided for this hardware address.
Instead of hardware address, client can also use a string identifier (such as hostname, or manufacturer serial number, or the home address of the customer.) This string identifier needs to be sent in the "client identifier" option. On the server side the configuration would be done corresponding to this identifier itself.
Whatever the DHCP client chooses to identify itself with, it should make sure that it uses the same value to identify itself for the duration it has the configuration.
DHCP has a logical concept of bindings. Binding is association of a client with its configuration at the server's end. When in the beginning the cleint has not contacted the server, the client has an inactive binding. Once the client retrieves and accepts the information from the server, the binding becomes active. If the client releases the configuration or does not renew the configuration, then the binding is considered to be expired.
When the server provides the configuration to a client, it is provided for a limited period of time. This time period is called the lease of the configuration. After the lease period has expired the DHCP client needs to stop using the configuration and reacquire the configuration. It is however possible for client to renew the lease before its expiry, so that it doesn't have to reacquire the configuration again.
When the client sends a message, it specifies a DHCP lease duration in its message (DHCPDISCOVER or DHCPREQUEST.) However, this is only a suggested or requested value and server would actually determine the lease period. The server can choose any value for the lease but it should be less than or equal to the suggested/requested value, but not greater than that. The server would send the actual lease duration in the "DHCP lease" field in DHCPACK message. The DHCP lease is specified in seconds.
DHCP client can try to renew its lease before its expiry as well. DHCP client can send DHCPREQUEST message to the DHCP server for renewing and extending its lease. In the DHCPREQUEST message, the client will fill the suggested value for the DHCP lease. The server will then send the the actual lease duration on the DHCPACK message (making sure that the provided lease time is either equal or less than the suggested lease duration.) This new lease duration is the new lease period starting from the time this DHCPACK was received.
The DHCP client can send the DHCPREQUEST for lease renewal at two times - T1 (renewal Timer) and T2 (Rebinding timer). T1 and T2 are time intervals after the lease starting time, when a client can try for lease renewal. The client tries at time T1 for lease renewal and if it is unsuccessful only then it tries again at time T2. T1 and T2 can be configured by DHCP server via DHCP options, and if the server does not configure these then client should assume the default values of (0.5 * lease duration) for T1 and (0.875 * lease duration) for T2. This means that if DHCP client received a DHCP lease of 100 seconds, then it should try for lease renewal at 50 seconds (0.5 * 100) first. If the client is not successful in renewing the lease, then it should try again at 87.5 seconds.
However, if the DHCP client gets a DHCPACK response from DHCP server at 50 seconds, with a lease duration of 100 seconds, then the timers need to be reset since the new lease has started. Now, the client should send the next lease renal message at 50 seconds from the point it received the DHCPACK message.
It is possible that the client has the IP address already (because of manual provisioning or other means) and just wants to get the other configuration parameters from the server. In this case it can send a DHCPINFORM message to the server. DHCP server in this case would send a DHCPACK message with the configuration parameters. In this scenario, the client doesn't need to have an active/exsiting binding with the server.