DHCP

Done

It allows devices to automatically/dynamically learn their:

  • IP Address
  • Subnet Mask
  • Default Gateway
  • DNS Servers

“Client Devices” such as PCs, smartphones and etc., usually rely on DHCP. However, routers, switches, servers and etc., usually are manually configured.

In SOHO networks the router will usually also act as the DHCP Server. However, in large networks it will usually be in a Windows/Linux server.

💡
NOTE: When you issue the ipconfig /all command in Windows, you can see a “(preferred)” keyword next to the IP Address. This means that the device was assigned that IP address before, so it asked if it could receive that same IP address after it expired. It is present in the Option 50 in the DHCP Discovery Message.

Lease time → DHCP servers lease IP addresses to devices, and usually they are not permanent. Therefore, the device must give in its IP Address after the Lease Time expires.

ipconfig /release → It tells Windows to release its IP Address. It does that by sending a DHCP Release message to the DHCP Server. It will stay without configuration.

ipconfig /renew → It asks the DHCP Server for configuration.

🚨
IMPORTANT: Unlike usual services, the source port is not ephemeral!
💡
NOTE: Search for “DHCP Magic Cookie”

D.O.R.A. messages

Discover

It is the first message, it the DHCP Client asks if there is any DHCP server in the network, saying that it requires an IP Address.

Offer

In response to the Discover message, the DHCP Server(s) “offer” an IP Address that is available to be used.

Request

It is send by the DHCP Client, saying that it accepted the IP Address and from which DHCP Server.

Ack

It is a response from the DHCP Server, telling the DHCP Client that it`s okay to use that IP Address.

💡
NOTE: If the response from the DHCP Server is gonna be Unicast or Broadcast is determined by the Bootp flags field in the DHCP Discover and Request messages sent by the DHCP Client. This is because some devices only support broadcast messages before they configure an IP Address.
💡
NOTE: There is also the following DHCP messages: Server → NACK = It is the opposite of Ack, and is used to deny the Request message of the client Client → Release = It is used to free an IP Address Decline = It is issued when it receives more than one Offer message, so it declines the offered IP addresses.

DHCP Relay Agent

In a large network, the DHCP server will be centralized and can be situated in a different subnet than the possible DHCP client. Therefore, the routers located in the subnets that may have potential DHCP Clients must act as DHCP Relay agents, because broadcast messages (Discover and Request) are not forwarded by routers.

How does it work?

When acting as a DHCP Relay Agent, the router will receive DORA messages and change the IP Header (yes, change the IP Header) to use its own IP Address as the Source (IP Address of the Interface it received it) and in the ‘giaddr’ (Gateway IP Address) field of the DHCP message and the DHCP Server IP Address as the Destination IP Address. It basically turns Broadcast messages from the client into Unicast messages, acting as an intermediary. And the DHCP Server will send an Offer or Ack to the IP address specified in the ‘giaddr’ field.

💡
NOTE: The reason the DHCP server sends responses to the 'giaddr' address and not the source IP address in the IP header is due to the way DHCP relay works. The 'giaddr' field is specifically designed to instruct the DHCP server where to send its responses when a DHCP request is relayed. This becomes particularly important in scenarios where the source IP address of the relayed DHCP request is the IP address of a WLC's virtual interface, but the DHCP server needs to send its responses to the dynamic interface's IP address.

But how does the DHCP Server knows what subnet the DHCP Client must be configured to?

It knows by the Relay Agent IP Address as the source, it being that subnet`s interface IP Address.

💡
NOTE: There is a field in the DHCP messages, that is the DHCP Client MAC Address. Therefore, if a DHCP Server receives an DHCP message with the Source MAC Address (Ethernet Header) different than the CHADDR (DHCP message Client`s MAC Address) it will know it was received via a Relay Agent.

DHCP Server configuration in Cisco IOS

Exclude IP Addresses from the pools

This command excludes a range of IP Addresses from being offered by the DHCP Server.

configure terminal
ip dhcp excluded-address {first-ip-address} {last-ip-address}

Enable/Disable DHCP message processing

configure terminal
[no] service dhcp

Create DHCP Pool

It creates a pool and labels it. This pool creates a collection of information that will be offered to the DHCP Client.

configure terminal
ip dhcp pool {pool-name}

This also enters DHCP Config mode for that Pool.

Specify the Subnet and the Mask

Router(dhcp-config)#network {subnet-id} {subnet-mask | prefix length}

Specify the Default Gateway

Router(dhcp-config)#default-router {ip-address}

Specify the DNS Server

Router(dhcp-config)#dns-server {ip-address}

Specify the Domain name

Router(dhcp-config)#domain-name {name}

Configure Lease Time

Router(dhcp-config)#lease {{day} [hour] [minutes] | infinite}

Clear Bindings

clear ip dhcp bindings *

DHCP Relay Configuration

  1. Enter Interface configuration mode
    configure terminal
    interface g0/0 // It has to be the interface on the subnet that will receive the IP Addresses offered by the DHCP Server
  1. Enable DHCP Relay and point to the DHCP Server
    ip helper-address {dhcp-server-ip}

DHCP Client Configuration

configure terminal
interface {interface-id}
ip address dhcp

Show commands

Show all the DHCP Clients

It shows information about each DHCP Client that was assigned an IP Address

show ip dhcp binding

Show the Pools settings

show ip dhcp pool

Show Operational Statistics for DHCP Server

show ip dhcp server statistics

Verify the Relay configuration

show ip interface {interface-id}

SUMMARY

Curiosities

DHCP Request without Discover and Offer

The DHCP process usually begins with a DHCPDISCOVER message from the client when it first connects to the network. However, a client may also directly send a DHCPREQUEST message under certain circumstances. Let's consider a few scenarios:

  1. Initial DHCP process: A new client or a client on a new network will start with a DHCPDISCOVER message, as it doesn't yet have a valid IP address for the current network.
  1. Renewing a lease: When a client is trying to renew its lease, it can directly send a DHCPREQUEST to the server, requesting to extend its lease on the current IP address. This request is sent unicast directly to the server that granted the lease, rather than as a broadcast.
  1. Rebooting or reconnecting: If a client device reboots or temporarily disconnects from the network, but it's still within its lease period, it may send a DHCPREQUEST message directly to the server upon reconnection, requesting the same IP address it had before.
  1. Receiving a NACK or no response: If the client receives a DHCP NACK in response to its DHCPREQUEST, or if it doesn't receive a response after a certain period of time, it will start the process over with a DHCPDISCOVER message.

So, while the process usually starts with DHCPDISCOVER, there are situations where a client might send a DHCPREQUEST without sending a DHCPDISCOVER first, particularly when renewing a lease or after a temporary disconnection. However, if the client's request is denied (with a DHCP NACK) or ignored, it will then revert to sending a DHCPDISCOVER to start the process anew.