Getting Dynamic IP with DHCP

Avocado Aun
11 min readMay 18, 2020

Part I. DHCP Intuition

Introduction

Remember the first thing that happened in your lecture classes in the new trimester? Yeah! That part where you need to talk about yourself for ice breaking. Duh, why do I pay top dollars just to introduce myself to some new strangers?

Well, to be fair the ice breaking is a common place because it allows these new strangers to know you a little bit better; your lecturer to know how to address your name and perhaps it gives you a the extra courage you need to talk to the girl next to you. This is very similar to computer networks, where your name is replaced with IP address for the machines.

Dynamic Host Configuration Protocol (DHCP)

The Dynamic Host Configuration Protocol (DHCP) is a network management protocol used on Internet Protocol networks whereby a DHCP server dynamically assigns an IP address and other network configuration parameters to each device on a network so they can communicate with other IP networks.

DHCP is designed to simplify IP address assignment. Prior to DHCP, IP address need to be manually assigned to each individual host. Static IP is not scalable when the number of hosts in a LAN increases. Soon, DHCP is introduced to automatically assigned IP address to any new hosts joining a LAN. DHCP is a service, for it to works, it needs to run on a server; which is called a DHCP server. In simpler network, the router of the network acts as the DHCP server (instead of needing a dedicated server sitting there just to provide IPs).

Getting a dynamic IP with DHCP

So what do we need to use the DHCP service? — DHCP works out of the box. DHCP is pre-installed with major Operating System as part of the TCP/IP stack. This means that anytime you connect a LAN cable to the RJ45 port on your machine; it should automatically asked for an IP address. In fact, DHCP is made seamless so that the addressing mechanism is transparent to the end users. Bulk of DHCP challenges is on the configuration of DHCP server; which is normally the job scope of network admin. In home network, the DHCP server is normally pre-configured by the ISP or the device manufacturer.

However, in some rare cases where a machine failed to get a dynamic IP, this could point to (1) mis-configuration of the DHCP server or (2) firewall/ACL is blocking udp/67 or udp/68.

In any LAN that supports DHCP, there are DHCP clients and DHCP server(s). DHCP client is the host that newly joins a network who is asking for an IP address. DHCP server is the address provider who assign any available IP address in the configured IP pool to the authorised DHCP clients. For example, when you sign in to McDonald’s WiFi on an iPhone; after the network authentication; the iPhone immediately send ‘DORA’ to ask for an IP. This ‘DORA’ request is eventually intercepted by the router. Then, the router will assign a valid IP to the iPhone. Here, the iPhone act as the DHCP client; meanwhile, the router act as the DHCP server.

DHCP DORA

DORA, as in DORA the explorer? Yes, you heard it right. The whole DHCP operations worked because of DORA; which is designed to ‘explore’ the DHCP requests and answer these queries accordingly. In theory, DHCP is ‘powered’ by 4 types of message exchanges; which is part of the DHCP operations. We will explain DORA process in general, then we provide an intuitive real world example on DHCP DORA.

DHCP DORA — overview

(1) DHCP ‘D’iscover (client → server)

When a host first join a new network, it needs an IP address to start communicating. We refer to the host here as the client. The client first sends (broadcast) a DHCP DISCOVER message to everyone connected in the same LAN. The purpose of this message is to ‘find and locate the DHCP server’ in hope that there is a DHCP server in this LAN, thus, the name discover. DHCP DISCOVER is received by all the hosts in the LAN, but only the DHCP server (or the router acting as the DHCP server) would process and reply to this request. When the DHCP server received a DHCP DISCOVER, it is informed that some host in the LAN needs an IP’. Then, the DHCP server will reply with the DHCP OFFER.

(2) DHCP ‘O’ffer (server → client)

When the DHCP server received a DHCP DISCOVER, the server then checks for an IP address that is available in the IP pool. Remember that by this time, there might already be other hosts in the network and some of the IP is currently being used by other devices. DHCP server selects an IP that is not ‘leased’ to any host during the DORA. This address preferably an IP previousl ynot leased to other hosts (that happened to be offline at the moment). For example, in the LAN 192.168.1.0/24; the usable IP address range is 192.168.1.1 → 192.168.1.254. These 254 addresses are configured into a DHCP IP pool and can be randomly assigned to any hosts provided that they are not used. In some cases, the IP pool in the DHCP server is not the full usable IP range for security reasons. After ‘picking’ an address, the selected address is encapsulated into an IP header and broadcasted as DHCP OFFER. The key information in OFFER is an IPv4 address; for example; the server ‘offers’ the IP address 192.168.1.10 to the client. Next, client needs to decide whether to take up this offer.

Note that DHCP OFFER is a broadcast message. This means that all the hosts in the LAN will receive this packet, but only the DHCP client that sent the DISCOVER earlier will process it. You might wonder:

(A) why is OFFER a broadcast message? The server could have just send the OFFER directly to the client since the server knows who’s sending the DISCOVER — this is partly true, but at the time being, the client still does not have an IP address (currently it is 0.0.0.0 for client’s IP). Without a valid IPv4 host IP, the server do not know the destination IP to send this OFFER back to the client. (B) How does the client know this is the DHCP OFFER is destined for it? There could also be other hosts asking for IP address — the client check the DHCP header to match the DHCP ‘transaction identifier’; which is a number to differentiate different pairs of {DISCOVER ← → OFFER}.

(3) DHCP ‘R’equest (client → server)

When the client receive the OFFER, it first checks the IPv4 address offered by the DHCP server. Then, it decides whether to accept it; which means it will start using this given IP address in a while (until the next lease renewal). DHCP requests is important for a LAN with multiple DHCP servers. Remember that DHCP DISCOVER is received by all the hosts, but only DHCP will react. Now, if there is 2 DHCP servers in the same LAN; this means that, at this stage, the client should received 2 DHCP OFFER(s); one from each DHCP server. Based on IP rules, one NIC can only be assigned with 1 IP. Here, the client need to decide which (only 1) OFFER to take up. If the user is Chinese, then you probably want to use IP 192.168.8.8 rather than 192.168.4.4.

Next, the DHCP client replies with an DHCP REQUEST message (broadcast) to the LAN. Why is it still a broadcast? That’s because at this stage, despite being offered an IP; the host still does not have a valid IP. The host must keep using 0.0.0.0 until the full DORA operations are completed. On the servers side, the DHCP server finds out if their offer is accepted based on the server identification option in the request. For the DHCP servers that are rejected, they withdraw the offers and return the offered IP address to the pool of available addresses.

(4) DHCP ‘A’cknowledge (server → client)

Before the DHCP client can start using the IP address it ‘accepted’ from the offer earlier, it must first get a DHCP acknowledgement (ACK) from the original server that offered that IP. Acknowledgement is a nod of approval to a previous request that is common in stateful data communication. DHCPACK is important for two reasons. (a) it informs the client to formally start using this IP (b)it also informed clients about other important network information like the default router IP address and the DNS server IP of the network. Remember that dynamic IP address is technically being leased to clients, but not assigned. This means that DHCP client must renew the DHCP lease to reuse the same IP address after the lease time as stated in the DHCPACK packet.

This packet includes the lease duration and any other configuration information that the client might have requested. At this point, the IP configuration process is completed. The DHCP client can now update its IP from 0.0.0.0 to the newly assigned IPv4 address.

Here’s a short video to summarize the DORA operations:

DHCP DORA made simple

Renewing IP with DHCP Release/Renew

There are other DHCP operations that are not part of DORA. DORA is compulsory for any hosts joining a new network (unless static IP is used). Meanwhile, DHCP release and renew are optionals. While DORA is pervasive, both release and renew must be explicitly executed.

DHCP Release

DHCP release is sent by the client → server to inform the server that it no longer need the IP address that it is currently using. This explicitly inform the server that this particular IP address is now vacant and is freed up for other hosts to use. After issuing a release, the host will imeediately get disconnected from the network until the next round of DORA happens. As client devices usually do not know when users may unplug them from the network, the protocol does not mandate the sending of DHCP Release.

DHCP Renew

DHCP renew is used to reinitiate the DORA operation explicilty. There is no need to issue a renew when a host joins a new network. DHCP renew is only needed if a host want to change its current IP address to some other new IP. DHCP renew is normally a follow up to release. After releasing an IP, the DHCP renew will allows the DORA to negotiate for a new IP. If a renew is issued before releasing, the release is implicitly issued by the system. Most of the times, DHCP renew would not result in a change of IP (since DHCP remembers the MAC address of host and would reassign the same IP back to the same back). DHCP renew is also useful in network troubleshooting, to refresh some IP information is outdated due to configuration changes (like new DNS server in the LAN).

DHCP in the real network

Now let’s take a look at the DORA operation in the story mode. Consider a DHCP client (on the left) and a DHCP server (on the right) in the LAN 1. DHCP_client joins LAN 1. We refers to the DHCP_client as PC1 from here onwards.

(A) DHCP Discover

PC1 joins a new network. PC1 currently does not have any IP address. So, PC1 is using 0.0.0.0 as its source IP. PC1 needs an IP address. PC1 ask everyone on the LAN, “hey, who is the DHCP server here? I need an IP”.

To do so, PC1 sends a DHCPDISC to all the host in LAN1 to 255.255.255.0 with port UDP/67 (the DHCP server port number). Only the DHCP server responds to the DHCPDISC with DHCPOFFER message. The other hosts in LAN1 simply discard this packet.

(B) DHCP Offer

The ‘DHCP server’ received PC1’s DHCPDISC. ‘DHCP server’ checks for available IP address in its IP pool. DHCP also checked if PC1 has previously joined this network. The ‘DHCP server’ then encapsulate the IP address that it is offering to PC1 to the DHCP offer packet. For example, the IP 192.168.1.100 is offered to PC1.

Here, the DHCP offer packet is sent from 10.0.0.100 (which is the IP address of the DHCP server) to 255.255.255.255 (everyone in LAN1). The server is asking “hey PC1, do you want to use 192.168.1.100?”. DHCP offer is still a broadcast message despite the server know this is destined for PC1. This is because at the moment PC1 still does not has a valid IP address (so unicast back to PC1 is not yet possible).

(C) DHCP Request

When PC1 received the DHCP offer with the IP 192.168.1.100; it will decide whether to take up this offer. If there is only 1 DHCP in the LAN, the offer is immediately taken up. If there are more than 1 DHCP servers, then only one offer is selected.

Here, PC1 decided to use 192.168.1.100. PC1. PC1 says “hey, I like 192.168.1.100; can I start using this IP?”. To do so, PC1 sends a DHCPREQ back to the server (still on a broadcast) to 255.255.255.0. Notice that PC1 source IP is still 0.0.0.0 at this stage. Remember that PC1 cannot use the IP address yet until its request is confirmed by the server. PC1 also has the option to add in additional requests (like how long to keep this IP etc) in this message.

(D) DHCP Acknowledge

The ‘DHCP server‘”’ received the DHCPRREQ from PC1. DHCP servers knows that PC1 has agreed to use 192.168.1.100 as its IP address in LAN1. Here, the server says “hey, I got your request. Okay, now you can start using 192.168.1.100 for 3 days”; and “if you want to go online, your router is at 192.168.1.254 and the DNS server in LAN1 is 192.168.1.10”.

The DHCPACK is sent back to PC1 at 255.255.255.255. When PC1 received this message, it changes it’s IP address from 0.0.0.0 to 192.168.1.100. Besides that, PC1 will set the default gateway IP to 192.168.1.254 and the DNS server IP to 192.168.1.10. This IP can be used for up to 3 days, as specified in the DHCPACK. After that lease duration, PC1 need to repeat the whole DORA process again to renew its IP. From here onwards, the next packet that PC1 sends out will be using 192.168.1.100 as its source IP.

Go to Part II — Configure DHCP in a Cisco Router

--

--

Avocado Aun

I’m just a little boy, lost in the tech world. But remember, love is a riddle, and life with tech is more amazing than ever