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 assignment in LAN, cable-modem, and wireless systems
- Obtaining IP settings on cable-modem systems
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.
Setup of a DHCP-Server.
Create an IP address pool
/ip pool add name=dhcp-pool1 ranges=172.16.0.10-172.16.0.20
Add a DHCP network which will concern to the network 172.16.0.0/12 and will distribute a gateway with IP address 172.16.0.1 to DHCP clients:
/ip dhcp-server network add address=172.16.0.0/12 gateway=172.16.0.1
Finally, add a DHCP server:
/ip dhcp-server add interface=wlan1 address-pool=dhcp-pool
Setup of the DHCP-Client (which will get a lease from the DHCP server, configured above).
Add the DHCP client:
/ip dhcp-client set interface=wlan1 enabled=yes
Check whether you have obtained a lease:
[admin@DHCP-Client] ip dhcp-client lease> print status: bound address: 172.16.0.20/12 gateway: 172.16.0.1 dhcp-server: 192.168.0.1 primary-dns: 159.148.147.194 expires: jun/24/2004 19:11:12 [admin@DHCP-Client] ip dhcp-client lease>
Specifications
Packages required: dhcpLicense 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-clientDescription
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 serverclient-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 enabledhost-name (text) - the host name of the clientinterface (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 leaseDescription
This submenu shows the actual IP address lease received by the client
Property Description
address (read-only: IP address mask) - the address receiveddhcp-server (read-only: IP address) - IP address of the DHCP server that have given out the current leaseexpires (read-only: text) - expiration time of the leasegateway (read-only: IP address) - the gateway address receivedprimary-dns (read-only: IP address) - the address of the primary DNS server receivedsecondary-dns (read-only: IP address) - the address of the secondary DNS server receivedstatus (read-only: "" | searching... | requesting... | bound | renewing... | rebinding...) - the current state of DHCP client: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-serverDescription
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: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
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 networkProperty Description
address (IP address mask) - the network DHCP server(s) will lend addresses fromboot-file-name (text) - Boot file namedns-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 serversdomain (text) - the DHCP client will use this as the 'DNS domain' setting for the network adaptergateway (IP address; default: 0.0.0.0) - the default gateway to be used by DHCP clientsnetmask (integer: 0..32; default: 0) - the actual network mask to be used by DHCP clientNotes
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 leaseDescription
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:
- an unused lease is in waiting state
- if a client asks for an IP address, the server chooses one
- if the client will receive statically assigned address, the lease becomes offered, and then bound with the respective lease time
- 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
- in case, the address does not respond, the lease becomes offered, and then bound with the respective lease time
- 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 clientexpires-after (read-only: time) - time until lease expireslease-time (time; default: 0s) - time that the client may use an addresstesting - 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
Command Description
check-status - Check status of a given busy dynamic lease, and free it in case of no responseNotes
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-relayDescription
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 tointerface (name) - interface name the DHCP relay will be working onlocal-address (IP address; default: 0.0.0.0) - the unique IP address of this DHCP relay needed for DHCP server to distinguish relays: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 setupQuestions
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 validNotes
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>
