13 Ocak 2020 Pazartesi

DHCP - Dynamic Host Configuration Protocol

DHCP Aşama ve Mesajları
DHCP mesajlarının açıklamaları burada.

Mesaj Akışı
Resim olarak şöyle.


Metin olarak şöyle.
0.0.0.0 255.255.255.255  DHCP    344 DHCP Discover - Transaction ID 0x1caf7b43
192.168.1.1 192.168.1.10 DHCP    344 DHCP Offer    - Transaction ID 0x1caf7b43
0.0.0.0 255.255.255.255  DHCP    344 DHCP Request  - Transaction ID 0x1caf7b43
192.168.1.1 192.168.1.10 DHCP    344 DHCP ACK      - Transaction ID 0x1caf7b43
DHCP UDP Kullanır
Açıklaması şöyle.
DHCP uses UDP as its transport protocol. DHCP messages that a client sends to a server are sent to well-known port 67 (UDP—Bootstrap Protocol and DHCP). DHCP Messages  that a server sends to a client are sent to port 68.
DHCP Sunucusunun IP Adresi Statik Olmalıdır
Açıklaması şöyle.
The DHCP server must have a static IP - this is because of a chicken-egg problem:

- During the DHCP process, the server communicates its IP address to the client
- If the server is also the client, this step can't succeed (as there is not yet an address to communicate).

1. DHCP Keşfi - Discover
DHCP kullanıcısı broadcast olarak "bir DHCP sunucusu var ise bana cevap versin" mesajı yollar. Bu mesajda kullanıcı en son IP adresini de söyleyebilir.

2. DHCP Teklifi - Offer
Açıklaması şöyle.
DHCP offers can be sent as unicast or broadcast. The layer-3 address doesn't really matter if it is unicast or broadcast because the frame is being delivered on the LAN by the layer-2 address.
Bir DHCP keşif mesajını almış olan DHCP sunucusu, ilgili kullanıcıya unicast olarak bir IP adresi teklifi verir. Bu etabın İngilizcesi DHCP Offer olarak anılır. İçeriği şu şekildedir:
  • IP adresi
  • Ağ maskesi
  • Tüm ağa yayın adresi
  • Ağdaki routerların listesi
  • Alan adı
  • DNS sunucularının listesi
  • Teklifin son kullanım zamanı
Cisco 7200 Router cihazında DHCP son kullanım zamanı (lease) 1 saat olarak ayarlanmak istenirse şöyle yapılır.
Router(dhcp-config)#lease 0 1
Komutun seçenekleri şöyle
lease {days [hours] [minutes] | infinite}

Not 1: DHCP teklif mesajları tüm bu bilgileri içermek zorunda değildir. Genelde IP adresi, ağ maskesi, DNS sunucularının listesi ve son kullanım zamanı yollanacaktır.
Not 2: Kullanıcı birden çok teklifi değerlendirmek zorunda kalabilir. Yani farklı IP aralıklarına sahip DHCP sunucuları aynı ağda bulunabilirler.

DHCP sunucusu MAC adresine aynı IP'yi vermek üzere ayarlanabilir. Böylece cihaz yeniden başlatılsa bile IP adresi değişmemiş olur.

Server-side Conflict Detection 
DHCP sunucusu IP adresi atamadan önce adresin gerçekten kullanılıp kullanılmadığını kontrol etmek için ICMP Echo (ping) isteği gönderebilir.
The server should use whatever information is available in the configuration information repository to choose an address to reuse. For example, the server may choose the least recently assigned address. As a consistency check, the allocating server SHOULD probe the reused address before allocating the address, e.g., with an ICMP echo request, and the client SHOULD probe the newly received address, e.g., with ARP.
ARP yerine Ping kullanılmasının sebebi şöyle. Yani DHCP sunucusu ile istemci aynı ağda olmayabilir.
The simple reason that the DHCP server uses ICMP rather than ARP is that there is no guarantee the DHCP server is on the same subnet as the client (because you could have a centralized DHCP server with relay agents).
Windows DHCP sunucularında bu ping işlemi tavsiye edilmiyor. Açıklaması şöyle
Assuming Windows DHCP server, the server-side conflict detection using ICMP echo requests (ping) is disabled by default, and it's recommended not to use it except in some rare circumstances:
Sebebi ise IP adresi alma süresini uzatması. Açıklaması şöyle.
If your network includes legacy DHCP clients (clients running a version of Windows earlier than Windows 2000), you can use server-side conflict detection provided by the DHCP Server service under specific circumstances. For example, this feature might be useful during failure recovery when scopes are deleted and recreated. For more information, see DHCP Troubleshooting.

By default, the DHCP service does not perform any conflict detection. To enable conflict detection, increase the number of ping attempts that the DHCP service performs for each address before leasing that address to a client. Note that for each additional conflict detection attempt that the DHCP service performs, additional seconds are added to the time needed to negotiate leases for DHCP clients.
Poisontap saldırısı ağ maskesi olarak /0 kullanarak tüm trafiği yönlendirmeye dayanır

3. DHCP İsteği - Request
Teklifi almış olan kullanıcı, broadcast olarak hangi DHCP teklifini kabul ettiğine dair bir DHCP istek mesajı yollar. İngilizcesi DHCP Request'tir. Broadcast olmasının açıklaması şöyle.
The servers receive the DHCPREQUEST broadcast from the client. Those servers not selected by the DHCPREQUEST message use the message as notification that the client has declined that server's offer.
Client-side Conflict Detection 
Belirtilen IP adresini kullanmaya başlamadan önce client bu adresin boşta olduğunu kontrol etmek için ARP isteği gönderir. Aslında RFC 5227 her türlü IP adresini kullanmaya başlamadan önce bunun yapılmasını istiyor. Açıklaması şöyle
2.1. Probing an Address

Before beginning to use an IPv4 address (whether received from manual configuration, DHCP, or some other means), a host implementing this specification MUST test to see if the address is already in use, by broadcasting ARP Probe packets. This also applies when a network interface transitions from an inactive to an active state, when a computer awakes from sleep, when a link-state change signals that an Ethernet cable has been connected, when an 802.11 wireless interface associates with a new base station, or when any other change in connectivity occurs where a host becomes actively connected to a logical link.

A host MUST NOT perform this check periodically as a matter of course. This would be a waste of network bandwidth, and is unnecessary due to the ability of hosts to passively discover conflicts, as described in Section 2.4.
RFC 5227 eğer ARP sonucu başarısızsa DHCPDECLINE mesajı göndermeyi söylüyor. Açıklaması şöyle
... the existing Address Resolution Protocol (ARP) provides an easy way for a host to detect this kind of misconfiguration and report it to the user. The DHCP specification [RFC2131] briefly mentions the role of ARP in detecting misconfiguration, as illustrated in the following three excerpts from RFC 2131:

the client SHOULD probe the newly received address, e.g., with ARP

The client SHOULD perform a final check on the parameters (e.g., ARP for allocated network address)

If the client detects that the address is already in use (e.g., through the use of ARP), the client MUST send a DHCPDECLINE message to the server

4. DHCP Onayı - Acknowledge
İstek kabul edildiğinde, DHCP sunucusu ilgili kullanıcıya DHCP onay mesajı (İngilizcede DHCP Acknowledge) yollar. Bu etaba gelindiğinde, ilgili kullanıcının DHCP teklifinde verilen ayarları yapmış olduğu varsayılır.

5. DHCP Bırakma - Release
IP adresine artık ihtiyacı olmayan bir kullanıcı, DHCP sunucusuna bu mesajı yollayacaktır. Bu sayede, o IP adresi başka bir kullanıcıya verilebilir. İngilizcesi DHCP Release'dir.

DHCPv4 Options
100 ve 101 Timezone bilgisini göndermek için kullanılır.

DHCPv6 Options
41 ve 21 Timezone bilgisini göndermek için kullanılır.

DHCP Relay
Eğer DHCP sunucusuna broadcast ile erişme imkanı yoksa, DHCP Relay kullanılabilir.

DHCP Sunucusuna Erişilemiyorsa
İşletim sistemi "Link Local" olarak adlandırılan bir IP adresi alır. Link Local Address yazısına taşıdım

İki Tane DHCP Sunucusu
Aynı dağdaki farklı IP bloklarına hizmet eden iki DHCP sunucusu olabilir.

DHCP Sunucusunu Bulma
Windows'ta ipconfig /all ile hangi sunucudan IP adresi aldığımız öğrenilebilir.

DHCP Etkinleştirme
C#
Örnek
private static void SetDHCP()
{
  ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  ManagementObjectCollection moc = mc.GetInstances();

  foreach (ManagementObject mo in moc) {
    // Make sure this is a IP enabled device. 
    if ((bool)mo("IPEnabled")) {
      ManagementBaseObject newDNS = mo.
        GetMethodParameters("SetDNSServerSearchOrder");
      newDNS("DNSServerSearchOrder") = null;
      ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null);
      ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder",
        newDNS, null);
    }
  }
}






Hiç yorum yok:

Yorum Gönder