What is the purpose of the DHCP protocol?
Dynamic Host Configuration Protocol (DHCP) is a network protocol that allows you to automatically assign IP addresses and other configuration parameters to devices connected to the network. It simplifies management and reduces the likelihood of conflicts by assigning the same address to two devices.
An IP address is an identifier of a resource in a TCP/IP network. It consists of four blocks (octets) of digits separated by dots: for example, 98.139.100.15 is the website address. yahoo.com . Resources in this context are network devices and web pages.
Computers, phones, etc. can send and receive data within a local network if their address is unique within that network. To exchange data on the Internet, the sending device must have an address unique across the Internet in the corresponding field of the IP packet. It is also called public or “white”.
It can be either assigned directly to the device or converted from a local address. The conversion is performed by the router or firewall using the NAT method.
When accessing the network, the device receives an IP address. The static address can be manually assigned by the administrator or assigned by the DHCP server. Dynamic is assigned only by the DHCP server
The standard for the DHCP protocol
The principles, rules, and functions of the DHCP protocol are documented in RFC 2131, released in 1997. It replaced RFC 2141 from 1993.
The RFC (Request for Comments) is a series of publications on the main standards and protocols of the Internet. For example, there are RFCs on the IP protocol, DNS infrastructure concepts and objects, and a unique URL resource identifier.
Since 1969, such documents have been issued by the IETF (Internet Engineering Task Force) and other organizations for the technical development and development of standards for the Internet. Some RFCs are accepted by the IETF as Internet standards.
Even earlier, in 1985, the predecessor of the DHCP protocol BOOTP (Bootstrap Protocol, RFC 951) appeared.
How does DHCP work?
When a device connects to a network that has a DHCP server, it requests an IP address from it.
This server stores a list of IP addresses to be issued, which is called a pool. It includes one or more ranges of consecutive addresses.
The server assigns an IP address and other network configuration parameters to the new device, such as the default gateway address, rental time, and others (these parameters are described in detail in the DHCP Options section).
Rental time
IP addresses are issued for a certain period of time, which is called lease time. Its duration is determined by the network administrator.
If the lease expires and the DHCP client continues to use the address, it sends a request to extend the lease (this process is described in detail in the Procedure for assigning an IP address section). At the same time, users usually do not notice the disconnection. If the client disconnects from the network, its address is returned to the pool and assigned to new devices.
For example, a DHCP server in a coffee shop, where new devices are constantly connected to the Wi-Fi network, can have a rental time of one hour. In such busy public networks, it is impractical to assign IP addresses for a longer period of time, otherwise their supply will quickly be exhausted.
At the same time, in a corporate Wi-Fi network, the rental period can be 8-12 hours, since the same employees stay inside the building with their phones and laptops throughout the working day.
And for office computers that are permanently connected to the network via an Ethernet cable, it is reasonable to set rental periods of one week or more. If there are dozens or hundreds of such jobs in the organization, frequent requests from customers for lease extensions will increase network traffic and slow down the network.
The DHCP architecture
The DHCP protocol uses a client-server communication model. It includes a DHCP server, clients, and sometimes relay agents.
The DHCP server
This is the name of the program that automatically issues addresses to clients, and the equipment in the local network on which this program runs: a computer or router in an organization or an Internet service provider.
In a local network, one or more such servers are assigned, which, upon request, connect client devices to the network and give them IP addresses from a pool determined by the administrator.
Most of the pool is allocated for dynamic assignment (assignment methods are described in detail in the Address Assignment Methods section): devices are connected to the network for a limited period, which can be extended after completion.
Often, several addresses are reserved for static purposes. For example, network printers, network storage, and IP cameras must be turned on all the time and accessible at the same addresses.
Some addresses or ranges may be excluded from the pool, for example, if the administrator has already assigned them to certain devices manually.
The DHCP client
A DHCP client is a program that accesses network settings using the DHCP protocol. Upon connection, the client requests the IP address and other network configuration parameters from the server using DHCP messages. Configuration data is transmitted in the options fields, which are described in detail in the DHCP Options section.
The DHCP Relay Agent
If the network consists of several subnets, then installing a DHCP server in each of them is irrational – it is more convenient to administer one network management center than several. At the same time, a relay agent is configured in each subnet where there is no server, since DHCP messages do not pass through routers from one subnet to another.
Such an agent is a program hosted on a switch or router that has access to a DHCP server configured to accept network connection requests.
DHCP Options
To transfer additional network configuration parameters in the DHCP protocol, standard options are used, and sometimes less common ones.
Standard options
Option 001: Subnet mask
The value of this parameter determines the size of the subnet managed by the DHCP server, that is, the number of devices that can receive the address. It depends on which network equipment — the switch or the router — the data from the client will be sent to.
To access a resource outside the subnet, such as the Internet, the device needs a router. And if he accesses a node in his subnet — for example, a torrent server – then they are connected by a switch.
Option 003: Router (default gateway)
A gateway is a node that serves as an access point to another network for network devices. This is usually a router.
When the default gateway address is specified, devices on the LAN can communicate with external networks. When you need to access a resource outside the local network, the transition chain begins with the default gateway.
Option 006: DNS server
This option allows you to specify the DNS server address so that devices on the local network can access it to resolve (convert) domain names to IP addresses. You can specify the primary and backup addresses here. Such a server can be located inside this network or outside it.
Option 051: rental time
This option specifies the maximum amount of time in seconds that the client can use the address before the lease is extended.
Option 082: Information for configuring the relay agent
This option specifies the data that is transmitted by agents, not by clients. In relation to the DHCP server to which the agent forwards messages, the client is remote because it is located on a different subnet.
The most important sub-options in it are:
082.1 Port Identifier — information for identifying the port to which the remote client is connected.;
082.2 ID of the remote client.
Less common options
The following are examples of options that are less frequently assigned to connected client devices.
Option 012: Client’s hostname
If necessary, the administrator can find out the name of the manufacturer by the MAC address of the device, and by the value of this parameter, you can find out more information, such as its type, model, and functions. Identifying a device is especially important if it does not have a specific user.
The value of this parameter — for example, galaxywatch — is specified during the manufacture of the device (later the user can change the hostname at his discretion). The device can send it to the server in a connection request: “I need an IP address in the name of galaxywatch.”
Not all servers support this option. In addition, it may be disabled for security reasons.
Option 015: Domain name
This option allows you to transfer the domain name to the device, which will be automatically added when accessing resources whose name does not contain a domain part. This way, you can use simple resource names without specifying the full domain name.
For example, if a computer receives an option from the DHCP server that specifies the astrahosting domain, then when accessing the files resource from the host, the DNS query will automatically go to the address files.astrahosting.
Option 042: list of NTP servers in order of preference
NTP is a network protocol for synchronizing a computer’s internal clock with a reference clock, which is what clocks on NTP servers are considered to be. Option 42 is used to synchronize the time on the DHCP client with the time on such an accurate time server.
Option 101: time zone as a string
This parameter specifies the time zone in which the local network is located. It is designated by the time zone database standard (tz database): for example, Asia/Tokyo.
Ways to assign addresses
The DHCP server assigns IP addresses to clients in one of three ways:
Dynamic assignment
This is the most common way to assign addresses. The IP address and other network configuration parameters are assigned to each client upon request for a rental period determined by the administrator. When this period expires, the client requests this configuration from the server again.
This method is reasonable to use when devices need temporary access, as well as in cases where new devices are constantly being connected to the network and addresses can quickly run out.
Static (manual) assignment
When the device address should not change — for example, if it is a network printer— a static assignment is usually used.
The administrator creates a distribution table on the DHCP server: adds the MAC addresses that need a static address to it, and assigns an IP address to each.
When receiving a new network connection request, the DHCP server checks whether the corresponding MAC is in the table. If it is present there, the server provides this client with the appropriate network configuration.
The MAC address of such a client is assigned a permanent IP address in this network. On subsequent connections, it will always receive this address.
This method of assignment differs from the manual configuration of each client only in that the address information is stored centrally and therefore does not need to be changed on each device where the settings are reset.
Automatic assignment
For this method, the administrator allocates a special range of IP addresses. When the device is connected to the network for the first time, it receives the first available address and other network settings from this range. A table of IP and MAC address correspondences is created on the server, and subsequently all devices in the table receive the addresses that were assigned to them when they were first connected. At the same time, the rental time is not limited.
This method differs from static assignment in that the administrator is not involved in compiling this table — it is created on the server automatically as new devices are connected.
The difference from the dynamic method is that the rental time is unlimited, and once the client receives an address on the network, he no longer applies for it.
Procedure for assigning an IP address
The client’s connection to the network, or initialization, consists of four main stages, the initial letters of which form the abbreviation DORA — these four stages are usually called by this abbreviation.
The interaction of the client device with the server continues at the stage of lease renewal and ends with disconnection from the network.
1. DHCPDISCOVER — detection
The new device is trying to connect to the network (for example, via cable or Wi-Fi). It doesn’t have an IP address yet, so it sends messages from its MAC address. His first broadcast (addressed to all MAC addresses on the LAN) message is DHCPDISCOVER: “I need an IP address. Are there any DHCP servers here?”.
2. DHCPOFFER — offer
By the same broadcast, one or more servers send a DHCPOFFER message to the new client, offering to rent one of the IP addresses of this network: “Here is the IP address and other configuration parameters.”
3. DHCPREQUEST request
The client selects an offer from one of the servers (usually the first one received) and sends a DHCPREQUEST message to all servers about the accepted address.: “I have selected an IP address from this DHCP server.”
4. DHCPACK — confirmation
The selected server sends a DHCPACK message to all devices on the network: “The device with this MAC address has received this network configuration.” This message contains the IP address of the new client, the rental time, the gateway address from which access to external addresses begins, and the DNS server address for resolving domain names on the Internet.
With the received address, the new client can access both devices on the local network and resources on the Internet.
5. DHCPREQUEST, DHCPACK and DHCPNAK — Extension
When half of the lease period expires (T1), the client sends a DHCPREQUEST message to the server, requesting an extension of the lease.
If the server accepts the request, it sends a DHCPACK message to the client in response, and its lease time starts over.
If the response to the request is not received, the client sends the request after another 25% of the rental period has expired. If 87.5% of the lease period (T2) passes and no response is received from the source server, the client starts sending broadcast requests and from that moment on can receive an address from any DHCP server.
The server may reject the extension request with a DHCPNAK message. After that, the client resends the DHCPDISCOVER broadcast message.
When the rental period ends, the client starts the DORA process anew.
6. DHCPRELEASE — completion
The client completes the lease by sending a DHCPRELEASE message to the server. After that, the IP address of the disconnected client is returned to the pool.
Advantages and disadvantages of the DHCP protocol
Advantages
Using the DHCP protocol has significant advantages over manually assigning IP addresses.
- Network connections are handled without administrator involvement on a single device, a DHCP server, which reduces the labor required to administer the network.
- Much fewer errors are allowed, such as typos or address conflicts, than with manual configuration.:
- Portable devices connected to a Wi-Fi network, such as laptops of students moving from one classroom to another, instantly change their address when they move to other network segments, and users hardly notice any connection breaks.
Disadvantages
There are also some difficulties associated with using the DHCP protocol that need to be taken into account.
- If only one DHCP server is configured on the network, it can be a single point of failure: new clients will not be able to get IP addresses. Customers who have already received their addresses will use them until the end of the lease period.
- If the managed network is divided into subnets, and the server is installed only in one of the subnets, then relay agents must be configured in the rest, since DHCP packets do not pass from one subnet to another.
- There are security risks for the network where the DHCP server is configured. An attacker can set up his own DHCP server on a local network with an incorrect network configuration so that client devices lose access to the network, or organize a stream of fake DHCPREQUEST messages in order to quickly exhaust the pool of addresses on the server. It is necessary to take into account the possibility of such attacks and take measures to protect the local network.
Interaction of the DHCP and DNS protocols
DNS (Domain Name System) is a hierarchical, decentralized naming system for computers, services, and other objects on a network connected to the Internet or a private network. DNS works like a telephone directory: it allows you to correlate domain names that are convenient for people to remember, and numeric IP addresses that are convenient to use for computer management.
DNS consists of thousands of servers running in a single system. If one server cannot resolve the name or IP, that is, convert the name to an address or vice versa, it accesses another, which in turn accesses the next, etc.
To find the necessary resources by their domain names, the devices access the DNS server. Its address is included in the set of network parameters that they receive from the DHCP server when they connect. A DNS server is a program, just like a DHCP server, and can run on the same computer or router as it.
To access external addresses, the DNS server does not have to be located inside the local network, and you can use an external service. Often, a free public server is used as an external DNS server, for example, from Google or CloudFlare.
The DHCP protocol, without a doubt, simplifies network management. It also has drawbacks; security threats require special attention. At the same time, the basic principles and architecture of DHCP do not change, and modern networks rarely do without using this protocol.
