Saturday, February 4, 2012

Dynamic Host Configuration Protocol - DHCP - How It Works

OK, now when we know what is for, lets explain in simple words how DHCP works. There are few types of messages associated with DHCP; DHCP discover, DHCP offer, DHCP request, DHCP acknowledgement, DHCP information and DHCP releasing. For IP address association first four types of messages are intensively used.


Lets say you bought new computer this morning and you went home to plug it into your ADSL router, then you opened your browser and voila, you are on Internet. What happened when you plugged you computer to network?

DHCP client (your computer) sends DHCP discover message when you connect LAN cable to router, if DHCP server or relay agent is available on network it responds with DHCP offer message that contains IP address and lease time. After that, DHCP client accepts or declines offered IP address with DHCP request message, if accepted DHCP server sends DHCP acknowledgement or, in case of decline, new DHCP offer message.

DHCP negotiation

Technical details

As we stated before, DHCP uses ports 67 and 68 for IPv4 communication, most of communication is connectionless and is performed via broadcast or unicast for machines which already have assigned IP address. Today DHCP server isn't strictly standalone machine somewhere in server room. DHCP server are integrated in SOHO routers, in switches, they co-exist on same servers with some other network services.

Depending on implementation, the DHCP server may have three methods of allocating IP-addresses:
  1. dynamic allocation -  administrator sets a pool of IP addresses with lease period, client in networks are set to request IP address from DHCP server during network initialization. 
  2. automatic allocation - the servers permanently assigns IP addresses from defined pool like in dynamic allocation, but it also keeps record of previous assignments. So if some IP address was assigned to this particular computer/device (IP address is associated with devices MAC address) and lease period is still valid, that same computer will get its old IP address. If lease time expired, than IP address is released and that computer will get the first available address.
  3. static allocation - administrator defines IP address - MAC address pairs, manually. Only computer with that MAC address can get associated IP address. There is no lease time, nothing.
  4. hybrid allocation policies - A variety of hybrid address allocation policies are possible with DHCP. With one common policy, the administrator registers a list of known client identifiers for which DHCP service is allowed, but the administrator does not assign fixed IP addresses to those clients. Those clients can then acquire IP addresses dynamically wherever they are connected. This allows the administrator to limit the use of DHCP to registered clients, but it saves the administrator the trouble of updating the DHCP server every time a client moves.

DHCP Message details

 NOTE:  There are few other types of messages beside this listed below, but we will include them in some other tutorial.

DHCP discover
 
The client broadcasts messages on the physical subnet to discover available DHCP servers. Network administrators can configure a local router to forward DHCP packets to a DHCP server from a different subnet. This client-implementation creates a UDP packet with the broadcast destination of 255.255.255.255 or the specific subnet broadcast address.

A DHCP client can also request its last-known IP address. If the client remains connected to a network for which this IP is valid, the server may grant the request. Otherwise, it depends whether the server is set up as authoritative or not. An authoritative server will deny the request, making the client ask for a new IP address immediately. A non-authoritative server simply ignores the request, leading to an implementation-dependent timeout for the client to give up on the request and ask for a new IP address.

DHCP offer

After the server receives the DHCPDISCOVER message from the client, it finds an address to assign to the client and puts it in a DHCPOFFER message. The server also includes in the DHCPOFFER message other configuration parameters for the client, as defined by the server’s configuration file. After the server has completed the DHCPOFFER message, it sends the message back to the client.

DHCP request

In response to the offer Client requests the server. The client replies DHCPREQUEST, unicast to the server, requesting the offered address A client can receive DHCP offers from multiple servers, but it will accept only one DHCP offer. Based on the Transaction ID field in the request, servers are informed whose offer the client has accepted. 

When other DHCP servers receive this message, they withdraw any offers that they might have made to the client and return the offered address to the pool of available addresses. In some cases DHCP request message is broadcast, instead of being unicast to a particular DHCP server, because the DHCP client has still not received an IP address. Also, this way one message can let all other DHCP servers know that another server will be supplying the IP address without missing any of the servers with a series of unicast messages.

DHCP acknowledgement

When the DHCP server receives the DHCPREQUEST message from the client, the configuration process enters its final phase. The acknowledgement phase involves sending a DHCPACK packet to the client. 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 protocol expects the DHCP client to configure its network interface with the negotiated parameters.

These are some basic information on DHCP and its mechanism.
Stay with us in next series of tutorials.

No comments:

Post a Comment