Thursday, June 21, 2012

Learn DHCP in 5 Minutes...

DHCP is everywhere needed where a Device has to get automatically an ip address to be able to communicate with other devices.

How can the Client find a DHCP Server?
When a pc boots up, and it has no static IP address set, it will "look up" for an IP-Address.
The pc will try to discover a DHCP Server on the local subnet (DHCPDISCOVER) via broadcast message.

Which Broadcast-IP Address will the PC use for that? 192.168.255.255? NOT AT ALL! 
The pc in that moment DOES NOT KNOW which network or subnetwork is in use in the local network, so the pc also can not know what is the Broadcast-IP of that network!

So the PC will send out DHCP Discover packets via broadcast to the address 255.255.255.255 (or in hex FF-FF-FF-FF-FF-FF).
So that Broadcast will reach ALL devices in the local network.

If there is a DHCP on the local network, the DHCP may answer with a DHCP Reply and offer an free and valid IP-Address to the client.
But the DHCP Server can do much more - it will offer the whole network configuration to the client if configured - incl. DNS-Server IPs, WINS-Server IPs, DHCP Lease Time, Default Gateway - and (if configured and needed) also several other options, depending on the GOAL and NEED.
DHCP is very powerfull, you can also use vendor classes and / or user classes to configure some special attributes needed for any purpose.
With user classes you can give a command on a client pc, and according to that command the dhcp server will provide "special configuration" to all clients which have this command set as they will send special request attributes to the DHCP Server in the DHCP discover packets.

Which Ports does DHCP use?

DHCP Server "listens" on Port UDP 67.
DHCP Client receives dhcp offers on Port UDP 68.

So - source port of the dhcp discover is UDP68 (Client), and destination Port is UDP 67 (DHCP Server Listener Port).
The DHCP answers to the discover with source port UDP 67 (DHCP Server) and destination Port UDP 68 (Client).

Which DHCP Messages exist?

DHCPDISCOVER
Client sends dhcp discover packets into the local subnet to ask ANYONE on the local subnet for an ip address.

DHCPOFFER
The DHCP Server "offers" an ip and some other options to requesting clients.

DHCPREQUEST
The client - after it has received a valid ip address from a dhcp server - sends dhcprequest as broadcast to the local subnet to "inform" eventually other existing dhcp servers that it has an ip and it does not need another ip. That dhcprequest contains the ip of the dhcp server which provided a ip to the client.
These other dhcps will withdraw any offers that they might have made to the client.

DHCPACKNOWLEDGEMENT
Finally, the dhcp server which provided an ip to the client will provide some additional information to the client, if configured, for example lease time etc.


What if there are more then one DHCP Servers in the local network??

But what if there is ANOTHER DHCP Server in the local net?
How does our Client know the RIGHT and legitimate DHCP Server?

Well - the Client will take ANY DHCP Offer it gets! So if you have 3 DHCP Servers in your local lan, the FIRST DHCP (or the fastest..) will be the one which will give an IP Address to the client.
So it could disturb a network if there are "illegal" DHCP Servers seriously!

To prevent "illegal" dhcp servers from disturbing the network, there are several technics...

1. configure DHCP Snooping on the switches to allow DHCP offers ONLY on configured and allowed uplink ports
or
2. use dhcp classes (user class or vendor class) so that only DHCP offers which fit to the class and configuration will be accepted by the client
or
3. if you find a user which provides illegal dhcp services, nail him on a cross so that the other users are warned...

What if the DHCP Server is located on another subnetwork?
Oups! As Clients use broadcasts to get a IP from DHCP Server - and broadcasts are NOT forwarded by routers normally, how can the client get an ip from dhcp server?
Well, thats what "DHCP Relay Agents" are designed for. A DHCP Relay Agent is a device on the local subnet, which "collects" dhcp discover broadcasts, and encapsulates them in a unicast message which can be forwarded to the DHCP Server in another subnet.
We can also use so called "ip helpers" on cisco devices, thats the same function like a dhcp relay agent.

What if the DHCP Server is unreachable?

Well, all the clients which still did get a IP-Address from the DHCP Server as it still WAS reachable, will still be able to communicate with other devices.
When the HALF DHCP Lease Time is over, a client contacts again DHCP Server to renew the lease. If the DHCP Server is not reachable, the client will hold the given IP till the lease time is over.

When the DHCP Lease Time on the Client expires - then its FINITO! The client loses its ip configuration.
Windows PCs have a feature called APIPA (Automatic private IP Address).
When a Windows Client can not get an ip from a DHCP Server, the OS provides the client with an IP from the Microsoft-IP Range 169.254.X.X.
The windoes clients on the network "negotiate" with each others so that no other pc on the local subnet gets the same ip of this 169.254. Range.
So the windows clients will still be able to communicate - but only with other clients in the local subnet, which also did get automatically a ip from the 169.254 Range.

How to configure redundant DHCP Service on the LAN?

Nothing easier then this!
Install and configure your DHCP Server. When its ready - just duplicate the DHCP Server.
You only have to "split" the DHCP Scope Ranges so that both DHCP Servers can not offer the SAME IP-Address to different clients, otherwise you will get IP conflicts!

So, first DHCP could provide IP Range 192.168.1.1 - 192.168.127.254 
and the second DHCP could provide IP Range 192.168.128.1 - 192.168.255.254 to the clients (for example..). 

No comments: