DHCP in 37 pictures: The invisible person who gives you your IP address

DHCP in 37 pictures: The invisible person who gives you your IP address

DHCP appears

A computer or mobile phone needs an IP address to access the Internet. Liu has two computers and two mobile phones, Xiaomei has a laptop, a tablet and two mobile phones, and Lao Wang, Ali and Minmin also have several terminal devices. If you manually configure an IP address for each device, it will be very cumbersome and inconvenient. Especially for mobile phones, laptops, tablets and other devices, every time you move to a new place and connect to a different network, you have to re-set the IP address, which is really too troublesome.

So there is the DHCP protocol, which automatically configures the network parameters of the device, including IP address, subnet mask, gateway address, DNS server, etc., instead of manual configuration. It can also unify IP address allocation and facilitate network management.

Introduction to DHCP

The DHCP protocol is developed from the BOOTP protocol. However, BOOTP runs in a relatively static environment. Each device is configured with a dedicated BOOTP parameter file, which will remain unchanged for a long time. DHCP extends BOOTP in the following two aspects:

  • DHCP allows devices to obtain IP addresses dynamically, rather than statically assigning each host address.
  • DHCP can distribute other configuration parameters, and the client obtains all the configuration information it needs with just one message.

After Liu and his team's devices use the DHCP function, they can perform TCP/IP communication as long as they are connected to the network. For routers and switches, parameters such as IP addresses are usually configured manually.

DHCP is a network protocol in Client/Server mode, where the DHCP Client submits a configuration request to the DHCP Server, and the DHCP Server returns the configuration information allocated to the DHCP Client. The Client and Server here are applications that can run on computers, servers, routers and other devices.

For example:

After Liu's computer is turned on, the DHCP Client automatically runs. The DHCP Client actively makes requests to the DHCP Server on other devices. The DHCP Server returns the corresponding IP configuration information according to the pre-configured strategy. The DHCP Client uses the obtained IP configuration information to communicate with other devices.

DHCP allocation mechanism

DHCP provides two address allocation mechanisms, and different allocation strategies can be selected for different clients according to network requirements.

  • Dynamic allocation mechanism: A limited-time IP address is allocated to the client through DHCP. If the client does not renew the IP address in time, the address may be used by other clients after the expiration date. Most clients use this type of dynamically allocated address.
  • Static allocation mechanism: A fixed IP address is allocated to a specific client through DHCP. A fixed IP address can be used permanently, and the client is usually a printer, server, and other devices.

In actual situations, we find that after the DHCP Client is restarted, it can still obtain the same IP address. When the DHCP Server assigns an IP address to the DHCP Client, it uses the following sequence:

  1. The IP address that is statically bound to the MAC address of the DHCP Client in the DHCP Server.
  2. IP address that has been used by the DHCP Client.
  3. The first available IP address found.

If no available IP address is found, the system will query the IP addresses that have expired or are in conflict. If found, the IP address will be allocated. Otherwise, an error message will be issued.

DHCP System Composition

The DHCP system consists of DHCP Server, DHCP Client, DHCP Relay, etc.

  • DHCP Server
    DHCP Server provides network parameters to DHCP Client. It is usually a server or network device (router or layer 3 switch) that provides DHCP service functions. For example, a wireless router used at home.
  • DHCP Client
    The DHCP Client obtains network parameters through the DHCP Server, usually a host or network device, such as Liu's computer and Xiaomei's mobile phone.
  • DHCP Relay
    Normally, DHCP uses broadcasting to implement message interaction, and DHCP services are limited to local network segments. If DHCP needs to be implemented across network segments, DHCP Relay technology can be used to implement it.
    A device that forwards cross-segment DHCP messages between the DHCP Server and the DHCP Client, usually a Layer 3 network device.

Basic DHCP Process

DHCP protocol messages are encapsulated in UDP mode. The listening port number of the DHCP Server is 67, and the listening port number of the DHCP Client is 68. DHCP devices interact with each other by sending and receiving messages on UDP port 67 and UDP port 68. The basic workflow of DHCP is divided into four stages, namely the discovery stage, the provision stage, the request stage, and the confirmation stage. We assume that Liu's PC is a new computer. The following describes how the PC obtains an IP address through DHCP for the first time .

For the convenience of description, DHCP Server is referred to as Little S and DHCP Client is referred to as Little C.

1. Discovery

Xiao C broadcasts a DHCP Discover message in the local network segment, aiming to find Xiao S who can assign an IP address.

2. Provision phase

After receiving the DHCP Discover message, the local network segment's Xiao S responds with a DHCP Offer message, which contains the available IP address and other network parameters.

Xiao C determines whether the Offer message is sent to him by comparing the xid fields in the Discover message and the Offer message to see if they are the same .

3. Request phase

Xiao C will receive the DHCP Offer message sent by Xiao S. If there are multiple Xiao Ss, each Xiao S will respond to the DHCP Offer message. Usually Xiao C will select the Offer message received first and broadcast a DHCP Request message to indicate which Xiao S is selected, and the rest of the Xiao Ss will be left out.

If Xiao C does not receive a DHCP Offer message after a certain period of time, it will resend a DHCP Discover message.

4. Confirmation stage

After receiving the DHCP Request broadcast message, Xiao S sends a DHCP Ack message as a response, which contains Xiao C's network parameters. The parameters of the DHCP Ack message and the previous DHCP Offer message cannot conflict, otherwise Xiao S will respond with a DHCP Nak message.

When Xiao C receives the DHCP Ac k message, it will send a free ARP message for detection, with the destination address being the obtained IP address. If the detection shows that this address is not in use, Xiao C will use this address and complete the configuration.

DHCP Lease

From the perspective of the DHCP protocol, Xiao S has the ownership of the IP address, while Xiao C only has the right to use the IP address. Every time Xiao S assigns an IP address to Xiao C, it will agree on a lease period for the IP address, which is usually 24 hours. During the lease period, Xiao C can use the corresponding IP address. When the lease period expires, Xiao C will no longer be able to use the IP address. Of course, Xiao C can apply to renew the lease of the IP address before the lease period expires.

Time T1 is when the lease is halfway through, and time T2 is when the lease is 87.5%. At time T1, Xiao C will unicast a DHCP Request message to Xiao S to request renewal of the IP address. If Xiao C receives a DHCP Ack response message, it means that the renewal is successful.

If Xiao C does not receive a DHCP Ack response message until time T2, it will broadcast a DHCP Request message to continue requesting to renew the IP address. If Xiao C receives a DHCP Ack response message, it means that the renewal is successful.

If Xiao C does not receive a DHCP Ack response message before the lease expires, he must stop using the original IP address. Xiao C will start from the discovery phase and apply for a new IP address.

DHCP Relay

In the process of dynamically obtaining an IP address, a broadcast message is used, so DHCP is only applicable when Little C and Little S are in the same subnet. If a Little S is configured for each network segment, this is obviously a waste.

In fact, there is also a role called DHCP Relay. Xiao C communicates with Xiao S across network segments through DHCP Relay to obtain an IP address. In this way, Xiao C on multiple subnets can use the same Xiao S, which saves costs and facilitates centralized management.

The working principle of DHCP Relay is as follows:

  1. Xiao C sends a DHCP Discover or DHCP Request broadcast message. After receiving it, the network device with DHCP Relay function unicasts the message to the specified Xiao S according to the configuration;
  2. Xiao S allocates an IP address and sends it to the DHCP Relay through unicast. The DHCP Relay then broadcasts the configuration information to Xiao C, completing the dynamic configuration of Xiao C.

DHCP protocol message

The previous content mentioned some DHCP message types. Now let's talk about the 8 main DHCP message types. The 5 common message types are: DHCP Discover, DHCP Offer, DHCP Request, DHCP Ack and DHCP Release. The 3 less used message types are: DHCP Nak, DHCP Decline and DHCP Inform.

  • DHCP Discover message is the first message sent by a DHCP client when it first accesses the network. It is the first message sent in the DHCP interaction process and is used to find the DHCP server's request message.
  • DHCP Offer message is used by DHCP Server to respond to DHCP Discover message and carries network parameters, including: IP address, subnet mask, default gateway, DNS server, etc.
  • DHCP Request message is sent by DHCP Client and has three usage scenarios:
  • Select the corresponding DHCP Server according to the policy and respond with a DHCP Offer message;
  • The DHCP Client is not accessing the network for the first time, and directly sends a DHCP Request message to apply for the previously used IP address and other parameters;
  • When the IP address lease expires, a DHCP Request is sent to renew the lease.
  • DHCP Ack message is the response message of DHCP Server to DHCP Request message for final confirmation. Only after receiving this message, DHCP Client can obtain IP address and corresponding network parameters.
  • DHCP Nak message is also the response message of DHCP Server to DHCP Request message. When all the parameters in the DHCP Request message are correct, it responds with DHCP Ack message, otherwise it responds with DHCP Nak message to tell DHCP Client not to use the obtained IP address.
  • DHCP Decline message When the DHCP Client receives the DHCP Ack message, it will also send a free ARP message to confirm whether the requested IP address has been used on the network. If the IP address has been used by other clients, the DHCP Client will send a DHCP Decline message to reject the assigned IP address and apply for an address from the DHCP Server again.
  • DHCP Release message When a DHCP Client wants to release the IP address it has obtained, it sends a DHCP Release message to the DHCP Server. After receiving the message, the DHCP Server can assign the IP address to other Clients.
  • DHCP Inform Message
    After the DHCP Client obtains the IP address manually, if it wants to obtain more network parameters from the DHCP Server, such as the default gateway address and DNS server address, the DHCP Client sends a DHCP Inform message to the DHCP Server to apply.

DHCP State Machine

If we string together the eight messages with different functions, we get the entire DHCP protocol interaction process. The four stages mentioned above (discovery, provision, request, and confirmation) cannot be fully displayed, so we need to use the DHCP protocol state machine. The state indicates the type of message used in the next step, and the state transition is through the reception, transmission, or timeout of the message. The following is the client state machine.

The client starts from the INIT state and broadcasts a DHCP Discover message. In the Selection state, it receives a DHCP Offer message and decides which address and server to use. After making a choice, it enters the Request state through a DHCP Request message. If the assigned address is inconsistent with the previously used address, it responds to a DHCP Nak message to reject it and returns to the INIT state; if the assigned address is already occupied, it responds to a DHCP Decline message to reject it and also returns to the INIT state. Usually, it receives a required address, responds to a DHCP Ack message, obtains the lease timeout values ​​T1 and T2, and enters the Binding state. At this time, the address can be used until the lease expires. When T1 expires, it enters the Renewal state and applies for renewal. If the renewal is successful, it can receive a DHCP Ack message and return to the Binding state; if the renewal is unsuccessful, it will apply for renewal again when T2 expires. If the lease eventually expires, the Client will be prohibited from using the leased address and return to the INIT state.

DHCP Network Practice

The DHCP protocol was created to solve network problems. Now we will simulate the actual environment, perform hands-on operations, and verify the function of DHCP.

DHCP Server Example

Network topology

Experimental requirements

  • RT (router) configures DHCP Server, and PC dynamically obtains IP address and other network parameters.

Procedure

Configuration ideas:

  1. Enable the DHCP function on RT.
  2. Create an address pool.
  3. Configure the address pool related parameters.
  4. Reference the address pool under the RT interface to implement the DHCP Server function.

Configuration commands:

Before using the DHCP function, you must enable the DHCP function. In the system view, use the dhcp enable command to enable the DHCP function.

To dynamically allocate IP addresses, you need to have multiple allocable IP addresses. Use the ip pool ip-pool-name command to create a global address pool. ip-pool-name indicates the name of the address pool.

In the global address pool view, use the network ip-address [ mask { mask | mask-length } ] command to configure the allocatable IP address segment. mask { mask | mask-length } indicates the subnet mask. The mask length is usually determined based on the number of devices.

With the IP address and subnet mask, plus the default gateway, the terminal devices can communicate with each other on the network. In the global address pool view, use the command gateway-list ip-address to configure the gateway IP address.

The network is interconnected, but the website cannot be accessed normally. You need to configure a DNS server for domain name resolution. In the global address pool view, use dns-list ip-address to configure the IP address of the DNS server.

In address pool configuration, the commonly used optional commands are as follows, which can be selectively set according to needs.

lease { day day [ hour hour [ minute minute ] ] | unlimited }: Configure the lease period of the IP address. The default lease period is 1 day.

excluded-ip-address start-ip-address [ end-ip-address ]: Set the IP addresses that are not allocated in the allocatable address pool. For example, if the address pool is 192.168.100.0/24, you can set 192.168.100.1 - 192.168.100.10 to exclude them from address allocation.

static-bind ip-address ip-address mac-address mac-address: Assign a fixed IP address to the Client.

After configuring the address pool, you need to reference it so that the DHCP Server function can be used normally. To configure the reference under the RT interface, use the command dhcp select global.

Functional Verification

After the configuration is completed , the DHCP function theoretically works normally. In actual situations, you can use the display ip pool name pool-name used command to view the configuration of the address pool and address allocation.

Check the DHCP Server address allocation status on RT.

At the same time, the dynamic address acquisition status is also checked on the PC for two-way verification.

By capturing packets, you can also see the detailed process of DHCP message interaction, and at the same time verify whether the theoretical knowledge is correct.

Here we find that the packet capture we see is a DHCP Offer unicast message, while in the previous introduction, the DHCP Offer is a broadcast message. In fact, the DHCP Offer message may be unicast or broadcast. DHCP has a broadcast bit in the flag field of the message. If the client supports receiving Offer unicast messages, the client will set the broadcast bit in the sent message to 0, otherwise it will be 1.

DHCP Relay Example

Network topology

Experimental requirements
  • The DHCP Client and DHCP Server are in different network segments. The DHCP Client obtains network parameters such as the IP address through the DHCP Relay.
Procedure
  1. PC (DHCP Client) turns on the DHCP function.

  1. RT1 (DHCP Relay) uses the dhcp select relay command to enable the DHCP Relay function, and uses the dhcp relay server-ip ip-address command on the G0/0/1 interface to configure the IP address of the DHCP Server.

  1. RT2 (DHCP Server) enables the DHCP function, creates an address pool and configures related parameters, and references the address pool under the interface to implement the DHCP Server function.

Functional Verification

The PC can obtain the IP configuration, and the obtained configuration is correct.

Capture packets on G0/0/1 of RT1 to view the message interaction process between the DHCP Client and DHCP Relay.

Capture packets on G0/0/0 of RT1 to view the message interaction process between the DHCP Server and DHCP Relay.

DHCP message format

If you want to learn more about the DHCP protocol, you need to look at its message format. DHCP devices implement protocol functions by identifying message contents.

  • op (operation type): indicates the format of the message. When the value is 1, it indicates the client's request message; when the value is 2, it indicates the server's response message.
  • htype (hardware type): Different hardware types have different values. The most common one is Ethernet, with a value of 1.
  • hlen (hardware address length): indicates the length of the hardware address. The value of Ethernet is 6, which is the length of the MAC address.
  • Hops : The number of DHCP relays that a DHCP message passes through.
  • Xid (interaction ID): A random value taken by the DHCP client. When receiving a response message from the DHCP server, the client checks whether the xid value is the same to determine whether the message is sent to itself.
  • secs (client startup seconds): records the usage time of the IP address.
  • flags : Broadcast response flag. When the value is 0, it means that the server sends the response message in unicast form; when the value is 1, the server sends the response message in broadcast form.
  • ciaddr (client IP address): The IP address of the client, which can be an assigned address, an address in use, or 0.0.0.0. 0.0.0.0 is when the client has no address in the initial state, and is only used for temporary communication, and is not a valid address.
  • yiaddr (your IP address): When the server sends a response message, it fills this field with the IP address assigned to the client.
  • siaddr (server IP address): The IP address used to identify the server.
  • giaddr (relay device IP address): indicates the IP address of the DHCP relay. The server determines the network segment address of the client by identifying this field, and then selects the appropriate address pool to allocate the IP address of the network segment to the client.
  • chaddr (client hardware address): used to identify the client's hardware address. When the client sends a broadcast discovery message, this field is its own hardware address.
  • sname (server name): Optional, the DHCP server fills in this field.
  • file (boot file name): Optional, the DHCP server fills in this field.
  • options (optional): optional, DHCP client obtains network parameters, DHCP server provides network parameters, both use this field. There are many contents, such as: lease, subnet mask, default gateway address, DNS server address, etc.

With the DHCP message format, you can understand the message content obtained by packet capture.

DHCP Discover message:

DHCP Offer message:

DHCP Request message:

DHCP Ack message:

<<:  Is the time ripe for “rate experience billing” in 5G multi-dimensional billing?

>>:  6G Proposal: Entering a new stage and meeting new challenges

Recommend

IPv6, the savior of the Internet of Things industry?

On November 26, 2019, all IPv4 addresses were all...

Interviewer: What process will be executed after entering the URL?

After entering the URL in the browser, it will pe...

How to better migrate data centers

Migrating a data center is no small feat, and it ...

Network literacy post: Fix the missing gateway address in 10 seconds

A few days ago, I received a request for help fro...

Big data changes both ends of the web hosting market

Big data has transformed both ends of the web hos...

From entry to mastery: Application and best practices of Ansible Shell modules

Ansible is a powerful automated operation and mai...