DHCP Protocol Deep Dive: IP Address Acquisition, Redundancy Strategies & Gateway Integration


2 views

When a device boots up without a configured IP address, it initiates the DHCP discovery process through four carefully orchestrated steps:

// Simplified DHCP packet flow in pseudocode
1. DHCPDISCOVER:
   src_ip = 0.0.0.0
   dst_ip = 255.255.255.255
   packet_type = DISCOVER

2. DHCPOFFER:
   src_ip = DHCP_SERVER_IP
   dst_ip = 255.255.255.255
   offered_ip = 192.168.1.100
   subnet_mask = 255.255.255.0
   lease_time = 86400

3. DHCPREQUEST:
   src_ip = 0.0.0.0
   dst_ip = 255.255.255.255
   requested_ip = 192.168.1.100

4. DHCPACK:
   src_ip = DHCP_SERVER_IP
   dst_ip = 255.255.255.255
   confirmed_ip = 192.168.1.100
   gateway = 192.168.1.1
   dns_servers = [8.8.8.8, 8.8.4.4]

The initial DHCPDISCOVER packet uses broadcast (255.255.255.255) because the client has no network configuration. Layer 2 Ethernet frames carry these broadcasts using:

  • Destination MAC: FF:FF:FF:FF:FF:FF
  • Source MAC: Client's MAC address
  • EtherType: 0x0800 (IPv4)

DHCP servers respond only if they're on the same broadcast domain or have DHCP relay agents configured.

The default gateway becomes critical after address assignment. DHCP servers typically provide these three network fundamentals:

// Typical DHCP options (option codes)
option routers 192.168.1.1;    // Default gateway
option subnet-mask 255.255.255.0;
option domain-name-servers 8.8.8.8, 8.8.4.4;

For enterprise reliability, consider these approaches:

# Linux ISC DHCPD failover configuration
failover peer "dhcp-failover" {
  primary;
  address dhcp-primary.example.com;
  port 647;
  peer address dhcp-secondary.example.com;
  max-response-delay 60;
  max-unacked-updates 10;
  load balance max seconds 3;
}

For Windows environments, DHCP failover can be configured through PowerShell:

Add-DhcpServerv4Failover -Name "DHCP-Failover" -ScopeId 192.168.1.0 
  -PartnerServer "backup-dhcp.example.com" -ComputerName "primary-dhcp.example.com" 
  -MaxClientLeadTime 1:00:00 -AutoStateTransition $true -StateSwitchInterval 1:00:00

Use tcpdump/wireshark to analyze DHCP traffic:

tcpdump -i eth0 -vvv -s 1500 'port 67 or port 68'

Key fields to monitor:

  • Transaction ID (xid)
  • Client identifier (chaddr)
  • Requested IP address (ciaddr)
  • Server identifier (siaddr)

When a device connects to a network, it initiates a four-step process called DORA (Discover, Offer, Request, Acknowledge):

// Simplified DHCP packet structure example
typedef struct dhcp_packet {
    uint8_t op;         // Message type (1=request, 2=reply)
    uint8_t htype;      // Hardware address type
    uint8_t hlen;       // Hardware address length
    uint8_t hops;       // Hops count
    uint32_t xid;       // Transaction ID
    uint16_t secs;      // Seconds elapsed
    uint16_t flags;     // Flags
    uint32_t ciaddr;    // Client IP address
    uint32_t yiaddr;    // Your (client) IP address
    uint32_t siaddr;    // Next server IP address
    uint32_t giaddr;    // Gateway IP address
    uint8_t chaddr[16]; // Client hardware address
    char sname[64];     // Server host name
    char file[128];     // Boot file name
    uint8_t options[312]; // Optional parameters
} dhcp_packet_t;

The initial DHCP Discover packet is sent as a broadcast (255.255.255.255) using UDP port 67. This ensures all DHCP servers on the local network segment receive the request, even when the client has no IP address.

// Python example showing DHCP discovery
import socket

def send_dhcp_discover():
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    sock.bind(('0.0.0.0', 68))
    # Build DHCP Discover packet here
    sock.sendto(dhcp_discover_packet, ('255.255.255.255', 67))

For high availability, consider these approaches:

  1. Split-Scope DHCP: Divide the IP range between two servers (80/20 split)
  2. DHCP Failover Protocol: Active-passive or active-active synchronization
  3. Virtual IP with Keepalived: Floating IP between servers
# Example ISC DHCPd failover configuration
failover peer "dhcp-failover" {
  primary; # or secondary
  address dhcp1.example.com;
  port 647;
  peer address dhcp2.example.com;
  peer port 647;
  max-response-delay 60;
  max-unacked-updates 10;
  mclt 3600;
  split 128;
  load balance max seconds 3;
}

The DHCP server includes the gateway (option 3) in its response. For redundancy, you can specify multiple gateways or implement VRRP/HSRP:

# Cisco router VRRP configuration example
interface GigabitEthernet0/0
 vrrp 1 ip 192.168.1.1
 vrrp 1 priority 105
 vrrp 1 preempt
 vrrp 1 authentication md5 key-string mypassword
  • Configure lease times appropriately (shorter for mobile devices)
  • Implement DHCP snooping on network switches
  • Monitor DHCP server logs and statistics
  • Test failover scenarios regularly