DHCP Client and Server

Document revision:2.5 (Mon Jul 19 07:06:08 GMT 2004)
Applies to: V2.8

General Information

Summary

The DHCP (Dynamic Host Configuration Protocol) is needed for easy distribution of IP addresses in a network. The MikroTik RouterOS implementation includes both - server and client parts and is compliant with RFC2131.

General usage of DHCP:

IP addresses can be bound to MAC addresses using static lease feature.

DHCP server can be used with MikroTik RouterOS HotSpot feature to authenticate and account DHCP clients. See the HotSpot Manual for more information.

Quick Setup Guide

This example will show you how to setup DHCP-Server and DHCP-Client on MikroTik RouterOS.

Specifications

Packages required: dhcp
License required: Level1
Submenu level: /ip dhcp-client, /ip dhcp-server, /ip dhcp-relay
Standards and Technologies: DHCP

Description

The DHCP protocol gives and allocates IP addresses to IP clients. DHCP is basically insecure and should only be used in trusted networks. DHCP server always listens on UDP 67 port, DHCP client - on UDP 68 port. The initial negotiation involves communication between broadcast addresses (on some phases sender will use source address of 0.0.0.0 and/or destination address of 255.255.255.255). You should be aware of this when building firewall.

Additional Resources

DHCP Client Setup

Submenu level: /ip dhcp-client

Description

The MikroTik RouterOS DHCP client may be enabled on one Ethernet-like interface at a time. The client will accept an address, netmask, default gateway, and two dns server addresses. The received IP address will be added to the interface with the respective netmask. The default gateway will be added to the routing table as a dynamic entry. Should the DHCP client be disabled or not renew an address, the dynamic default route will be removed. If there is already a default route installed prior the DHCP client obtains one, the route obtained by the DHCP client would be shown as invalid.

Property Description

add-default-route (yes | no; default: yes) - whether to add the default route to the gateway specified by the DHCP server

client-id (text) - corresponds to the settings suggested by the network administrator or ISP. Commonly it is set to the client's MAC address, but it may as well be any test string

enabled (yes | no; default: no) - whether the DHCP client is enabled

host-name (text) - the host name of the client

interface (name; default: (unknown)) - any Ethernet-like interface (this includes wireless and EoIP tunnels)

use-peer-dns (yes | no; default: yes) - whether to accept the DNS settings advertized by DHCP server (they will appear in /ip dns submenu)

Command Description

renew - renew current leases. If the renew operation was not successful, client tries to reinitialize lease (i.e. it starts lease request procedure (rebind) as if it had not received an IP address yet)

Notes

If host-name property is not specified, client's system identity will be sent in the respective field of DHCP request.

If client-id property is not specified, client's MAC address will be sent in the respective field of DHCP request.

If use-peer-dns property is enabled, the DHCP client will uncoditionally rewrite the settings in /ip dns submenu. In case two or more DNS servers were received, first two of them are set as primary and secondary servers respectively. In case one DNS server was received, it is put as primary server, and the secondary server is left intact.

Example

To enable DHCP client on ether1 interface:

[admin@MikroTik] ip dhcp-client> set enabled=yes interface=ether1
[admin@MikroTik] ip dhcp-client> print
              enabled: yes
            interface: ether1
            host-name: ""
            client-id: ""
    add-default-route: yes
         use-peer-dns: yes
[admin@MikroTik] ip dhcp-client>
      

DHCP Client Lease

Submenu level: /ip dhcp-client lease

Description

This submenu shows the actual IP address lease received by the client

Property Description

address (read-only: IP address mask) - the address received

dhcp-server (read-only: IP address) - IP address of the DHCP server that have given out the current lease

expires (read-only: text) - expiration time of the lease

gateway (read-only: IP address) - the gateway address received

primary-dns (read-only: IP address) - the address of the primary DNS server received

secondary-dns (read-only: IP address) - the address of the secondary DNS server received

status (read-only: "" | searching... | requesting... | bound | renewing... | rebinding...) - the current state of DHCP client:
"" - DHCP client is not enabled
searching... - the DHCP client is searching for DHCP server, but has not yet received an offer
requesting... - the DHCP client has received an offer from a DHCP server, and requesting an IP address now
bound - the DHCP client has received an IP address (status bound should also appear on the DHCP server)
renewing... - the DHCP client is trying to renew the lease
rebinding... - the renew operation has failed, and lease time is over, so the DHCP client is trying to request an IP address once again

Example

To check the obtained lease:

[admin@MikroTik] ip dhcp-client lease> print
         status: bounded
        address: 80.232.241.15/21
    dhcp-server: 10.1.0.172
        expires: oct/20/2002 09:43:50
        gateway: 80.232.240.1
    primary-dns: 195.13.160.52
  secondary-dns: 195.122.1.59
[admin@MikroTik] ip dhcp-client lease>
      

DHCP Server Setup

Submenu level: /ip dhcp-server

Description

The router supports an individual server for each Ethernet-like interface. The MikroTik RouterOS DHCP server supports the basic functions of giving each requesting client an IP address/netmask lease, default gateway, domain name, DNS-server(s) and WINS-server(s) (for Windows clients) information (set up in the DHCP networks submenu)

In order DHCP server to work, you must set up also IP pools (do not inlude the DHCP server's IP address into the pool range) and DHCP networks.

Property Description

add-arp (yes | no; default: no) - whether to add dynamic ARP entry:
no - either ARP mode should be enabled on that interface or static ARP entries should be administratively defined in /ip arp submenu

address-pool (name | static-only; default: static-only) - IP pool, from which to take IP addresses for clients
static-only - allow only the clients that have a static lease (i.e. no dynamic addresses will be given to clients, only the ones added in lease submenu)

authoritative (yes | no; default: no) - whether the DHCP server is the only one DHCP server for that network

interface (name) - Ethernet-like interface name

lease-time (time; default: 72h) - the time that a client may use an address. The client will try to renew this address after a half of this time and will request a new address after time limit expires

name (name) - reference name

relay (IP address; default: 0.0.0.0) - the IP address of the relay this DHCP server should process requests from:
0.0.0.0 - the DHCP server will be used only for direct requests from clients (no DHCP really allowed)
255.255.255.255 - the DHCP server should be used for any incomming request from a DHCP relay except for those, which are processed by another DHCP server that exists in the /ip dhcp-server submenu

src-address (IP address; default: 0.0.0.0) - the address which the DHCP client must send requests to in order to renew an IP address lease. If there is only one static address on the DHCP server interface and the source-address is left as 0.0.0.0, then the static address will be used. If there are multiple addresses on the interface, an address in the same subnet as the range of given addresses should be used

Notes

If using both - Universal Client and DHCP Server on the same interface, client will only receive a DHCP lease in case it is directly reachable by its MAC address through that interface (some wireless bridges may change client's MAC address).

If authoritative property is set to yes, the DHCP server is sending rejects for the leases it cannot bind or renew. It also may (although not always) help to prevent the users of the network to run illicitly their own DHCP servers disturbing the proper way this network should be functioning.

If relay property of a DHCP server is not set to 0.0.0.0 the DHCP server will not respond to the direct requests from clients.

Example

To add a DHCP server to the ether1 interface, lending IP addresses from dhcp-clients IP pool for 2 hours:

[admin@MikroTik] ip dhcp-server> add name=dhcp-office disabled=no \
\... address-pool=dhcp-clients interface=ether1 lease-time=2h
[admin@MikroTik] ip dhcp-server> print
Flags: X - disabled, I - invalid
  #   NAME         INTERFACE    RELAY           ADDRESS-POOL LEASE-TIME ADD-ARP
  0   dhcp-office  ether1                       dhcp-clients 2h         no

[admin@MikroTik] ip dhcp-server>
      

DHCP Networks

Submenu level: /ip dhcp-server network

Property Description

address (IP address mask) - the network DHCP server(s) will lend addresses from

boot-file-name (text) - Boot file name

dns-server (text) - the DHCP client will use these as the default DNS servers. Two comma-separated DNS servers can be specified to be used by DHCP client as primary and secondary DNS servers

domain (text) - the DHCP client will use this as the 'DNS domain' setting for the network adapter

gateway (IP address; default: 0.0.0.0) - the default gateway to be used by DHCP clients

netmask (integer: 0..32; default: 0) - the actual network mask to be used by DHCP client
0 - netmask from network address is to be used

next-server (IP address) - IP address of next server to use in bootstrap

wins-server (text) - the Windows DHCP client will use these as the default WINS servers. Two comma-separated WINS servers can be specified to be used by DHCP client as primary and secondary WINS servers

Notes

The address field uses netmask to specify the range of addresses the given entry is valid for. The actual netmask clients will be using is specified in netmask property.

DHCP Leases

Submenu level: /ip dhcp-server lease

Description

DHCP server lease submenu is used to monitor and manage server's leases. The issued leases is showed here as dynamic entries. You can also add static leases to issue the definite client (determined by MAC address) the specified IP address.

Generally, the DHCP lease it allocated as follows:

  1. an unused lease is in waiting state
  2. if a client asks for an IP address, the server chooses one
  3. if the client will receive statically assigned address, the lease becomes offered, and then bound with the respective lease time
  4. if the client will receive a dynamic address (taken from an IP address pool), the router sends a ping packet and waits for answer for 0.5 seconds. During this time, the lease is marked testing
  5. in case, the address does not respond, the lease becomes offered, and then bound with the respective lease time
  6. in other case, the lease becomes busy for the lease time (there is a command to retest all busy addresses), and the client's request remains unanswered (the client will try again shortly)

Then a client may free the leased address. Then the dynamic lease is removed, and the allocated address is returned to the address pool. But the static lease becomes busy until the client will reacquire the address.

Note that the IP addresses assigned statically are not probed.

Property Description

address (IP address; default: 0.0.0.0) - lended IP address for the client

expires-after (read-only: time) - time until lease expires

lease-time (time; default: 0s) - time that the client may use an address
0s - lease will never expire

mac-address (MAC address; default: 00:00:00:00:00:00) - MAC address of the client. It is the base for static lease assignment

rx-rate (integer; default: 0) - maximal receive bitrate to the client (for users it is upload bitrate))
0 - no limitation

server (read-only: name) - server name which serves this client

status (read-only: waiting | testing | busy | offered | bound) - lease status:
waiting - not used static lease
testing - testing whether this address is used or not (only for dynamic leases) by pinging it with timeout of 0.5s
busy - this address is assigned statically to a client or already exists in the network, so it can not be leased
offered - server has offered this lease to a client, but did not receive confirmation from the client
bound - server has received client's confirmation that it accepts offered address, it is using it now and will free the address not later, than the lease time will be over

tx-rate (integer; default: 0) - maximal transmit bitrate to the client (for users it is download bitrate))
0 - no limitation

Command Description

check-status - Check status of a given busy dynamic lease, and free it in case of no response

Notes

Even though client address may be changed (with adding a new item) in lease print list, it will not change for the client. It is true for any changes in the DHCP server configuration because of the nature of the DHCP protocol. Client tries to renew assigned IP address only when half a lease time is past (it tries to renew several times). Only when full lease time is past and IP address was not renewed, new lease is asked (rebind operation).

the deault mac-address value will never work! You should specify a correct MAC address there.

Example

To assign 10.5.2.100 static IP address for the existing DHCP client (shown in the lease table as item #0):

[admin@MikroTik] ip dhcp-server lease> print
Flags: X - disabled, H - hotspot, D - dynamic
  #   ADDRESS         MAC-ADDRESS       EXPIRES-AFTER SERVER         STATUS
  0 D 10.5.2.90       00:04:EA:C6:0E:40 1h48m59s      switch         bound
  1 D 10.5.2.91       00:04:EA:99:63:C0 1h42m51s      switch         bound
[admin@MikroTik] ip dhcp-server lease> add copy-from=0 address=10.5.2.100
[admin@MikroTik] ip dhcp-server lease> print
Flags: X - disabled, H - hotspot, D - dynamic
  #   ADDRESS         MAC-ADDRESS       EXPIRES-AFTER SERVER         STATUS
  1 D 10.5.2.91       00:04:EA:99:63:C0 1h42m18s      switch         bound
  2   10.5.2.100      00:04:EA:C6:0E:40 1h48m26s      switch         bound
[admin@MikroTik] ip dhcp-server lease>
       

DHCP Relay

Submenu level: /ip dhcp-relay

Description

DHCP Relay is just a proxy that is able to receive a DHCP request and resend it to the real DHCP server

Property Description

dhcp-server (text) - list of DHCP servers' IP addresses which should be the DHCP requests forwarded to

interface (name) - interface name the DHCP relay will be working on

local-address (IP address; default: 0.0.0.0) - the unique IP address of this DHCP relay needed for DHCP server to distinguish relays:
0.0.0.0 - the IP address will be chosen automatically

name (name) - descriptive name for relay

Notes

DHCP relay does not choose the particular DHCP server in the dhcp-server list, it just sent to all the listed servers.

Example

To add a DHCP relay named relay on ether1 interface resending all received requests to the 10.0.0.1 DHCP server:

[admin@MikroTik] ip dhcp-relay> add name=relay interface=ether1 \
\... dhcp-server=10.0.0.1 disabled=no
[admin@MikroTik] ip dhcp-relay> print
Flags: X - disabled, I - invalid
  #   NAME                            INTERFACE DHCP-SERVER     LOCAL-ADDRESS
  0   relay                           ether1    10.0.0.1        0.0.0.0

[admin@MikroTik] ip dhcp-relay>
      

Question&Answer-Based Setup

Command name: /ip dhcp-server setup

Questions

dhcp server interface (name) - interface to run DHCP server on

dhcp address space (IP address mask; default: 192.168.0.0/24) - network the DHCP server will lease to the clients

gateway (IP address; default: 0.0.0.0) - the default gateway of the leased network

dhcp relay (IP address; default: 0.0.0.0) - the IP address of the DHCP relay between the DHCP server and the DHCP clients

addresses to give out (text) - the pool of IP addresses DHCP server should lease to the clients

dns servers (IP address) - IP address of the appropriate DNS server to be propagated to the DHCP clients

lease time (time; default: 3d) - the time the lease will be valid

Notes

Depending on current settings and answers to the previous questions, default values of following questions may be different. Some questions may disappear if they become redundant (for example, there is no use of asking for 'relay' when the server will lend the directly connected network)

Example

To configure DHCP server on ether1 interface to lend addresses from 10.0.0.2 to 10.0.0.254 which belong to the 10.0.0.0/24 network with 10.0.0.1 gateway and 159.148.60.2 DNS server for the time of 3 days:

[admin@MikroTik] ip dhcp-server> setup
Select interface to run DHCP server on

dhcp server interface: ether1
Select network for DHCP addresses

dhcp address space: 10.0.0.0/24
Select gateway for given network

gateway for dhcp network: 10.0.0.1
Select pool of ip addresses given out by DHCP server

addresses to give out: 10.0.0.2-10.0.0.254
Select DNS servers

dns servers: 159.148.60.2
Select lease time

lease time: 3d
[admin@MikroTik] ip dhcp-server>
      

The wizard has made the following configuration based on the answers above:

[admin@MikroTik] ip dhcp-server> print
Flags: X - disabled, I - invalid
  #   NAME            INTERFACE RELAY           ADDRESS-POOL LEASE-TIME ADD-ARP
  0   dhcp1           ether1    0.0.0.0         dhcp_pool1   3d         no

[admin@MikroTik] ip dhcp-server> network print
  # ADDRESS            GATEWAY         DNS-SERVER      WINS-SERVER     DOMAIN
  0 10.0.0.0/24        10.0.0.1        159.148.60.2

[admin@MikroTik] ip dhcp-server> /ip pool print
  # NAME                                        RANGES
  0 dhcp_pool1                                  10.0.0.2-10.0.0.254

[admin@MikroTik] ip dhcp-server>