modified on 1 lip 2010 at 11:26 ••• 39 812 views

DHCP Client, Server and Relay

Z MikroTik Wiki

Spis treści

Informacje Ogólne

Wprowadzenie

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.

Szybka konfiguracja

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

  • Setup of a DHCP-Server
1. Create an IP address pool:
   /ip pool add name=dhcp-pool ranges=172.16.0.10-172.16.0.20
2. 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
3. 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)
1. Add the DHCP client:
   /ip dhcp-client add interface=wlan1 use-peer-dns=yes \
        add-default-route=yes disabled=no
2. Check whether you have obtained a lease:
   [admin@Server] ip dhcp-client> print detail
   Flags: X - disabled, I - invalid
   0   interface=wlan1 add-default-route=yes use-peer-dns=yes 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-after=2d23:58:52
   [admin@Server] ip dhcp-client>

Specyfikacja

Packages required: dhcp

License required: Level1

Submenu level: /ip dhcp-client, /ip dhcp-server, /ip dhcp-relay

Standards and Technologies: DHCP

Opis

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.

Dodatkowe materiały

   * ISC Dynamic Host Configuration Protocol (DHCP)
   * DHCP mini-HOWTO
   * ISC DHCP FAQ 

Konfiguracja Klienta DHCP

Submenu level: /ip dhcp-client

Opis

The MikroTik RouterOS DHCP client may be enabled on any 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.

Properties

Sub-menu: /ip dhcp-client


Property Description
add-default-route (yes | no; Default: yes) Whether to install default route in routing table received from dhcp server.
client-id (string; Default: ) Corresponds to the settings suggested by the network administrator or ISP. If not specified, client's MAC address will be sent.
default-route-distance (integer:0..255; Default: ) Distance of default route. Applicable if add-default-route is set to yes.
host-name (string; Default: ) Tthe host name of the client sent to a DHCP server. If not specified, client's system identity will be used.
interface (name; Default: ) Interface on which DHCP client will be running.
use-peer-dns (yes | no; Default: yes) Whether to accept the DNS settings advertised by DHCP Server. (Will override the settings put in the /ip dns submenu
use-peer-ntp (yes | no; Default: yes) Whether to accept the NTP settings advertised by DHCP Server. (Will override the settings put in the /system ntp client submenu)


Status

Command /ip dhcp-client print detail will show current status of dhcp client and read-only properties listed in table below:

Property Description
address (IP/mask) IP address and netmask, which is assigned to DHCP Client from the Server
dhcp-server (IP) IP address of the DHCP server
expires-after (time) Time, when the lease expires (specified by the DHCP server)
gateway (IP) IP address of the gateway which is assigned by DHCP server
invalid (yes | no) Shows whether configuration is invalid.
netmask (IP)
primary-dns (IP) IP address of the primary DNS server, assigned by the DHCP server
primary-ntp (IP) IP address of the primary NTP server, assigned by the DHCP server
secondary-dns (IP) IP address of the secondary DNS server, assigned by the DHCP server
secondary-ntp (IP) IP address of the secondary NTP server, assigned by the DHCP server
status (bound | error | rebinding... | requesting... | searching... | stopped) Shows the status of DHCP Client


Menu specific commands

Property Description
release (id) Release current binding and restart DHCP client
renew (id) 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)


Basic examples

Add a DHCP client on ether1 interface:

/ip dhcp-client add interface=ether1 disabled=no
[admin@MikroTik] ip dhcp-client> print detail
Flags: X - disabled, I - invalid 
 0   interface=ether1 add-default-route=yes use-peer-dns=yes use-peer-ntp=yes
     status=bound address=192.168.0.65/24 gateway=192.168.0.1
     dhcp-server=192.168.0.1 primary-dns=192.168.0.1 primary-ntp=192.168.0.1
     expires-after=9m44s 
[admin@MikroTik] ip dhcp-client>

Konfiguracja Serwera DHCP

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 RFC 2131.

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 include the DHCP server's own IP address into the pool range) and DHCP networks.

It is also possible to hand out leases for DHCP clients using the RADIUS server, here are listed the parameters for used in RADIUS server.

Access-Request:

  • NAS-Identifier - router identity
  • NAS-IP-Address - IP address of the router itself
  • NAS-Port - unique session ID
  • NAS-Port-Type - Ethernet
  • Calling-Station-Id - client identifier (active-client-id)
  • Framed-IP-Address - IP address of the client (active-address)
  • Called-Station-Id - name of DHCP server
  • User-Name - MAC address of the client (active-mac-address)
  • Password - ""

Access-Accept:

  • Framed-IP-Address - IP address that will be assigned to client
  • Framed-Pool - ip pool from which to assign ip address to client
  • Rate-Limit - Datarate limitation for DHCP clients. Format is: rx-rate[/tx-rate] [rx-burst-rate[/tx-burst-rate] [rx-burst-threshold[/tx-burst-threshold] [rx-burst-time[/tx-burst-time][priority] [rx-rate-min[/tx-rate-min]]]]. All rates should be numbers with optional 'k' (1,000s) or 'M' (1,000,000s). If tx-rate is not specified, rx-rate is as tx-rate too. Same goes for tx-burst-rate and tx-burst-threshold and tx-burst-time. If both rx-burst-threshold and tx-burst-threshold are not specified (but burst-rate is specified), rx-rate and tx-rate are used as burst thresholds. If both rx-burst-time and tx-burst-time are not specified, 1s is used as default. Priority takes values 1..8, where 1 implies the highest priority, but 8 - the lowest. If rx-rate-min and tx-rate-min are not specified rx-rate and tx-rate values are used. The rx-rate-min and tx-rate-min values can not exceed rx-rate and tx-rate values.
  • Ascend-Data-Rate - tx/rx data rate limitation if multiple attributes are provided, first limits tx data rate, second - rx data rate. If used together with Ascend-Xmit-Rate, specifies rx rate. 0 if unlimited
  • Ascend-Xmit-Rate - tx data rate limitation. It may be used to specify tx limit only instead of sending two sequential Ascend-Data-Rate attributes (in that case Ascend-Data-Rate will specify the receive rate). 0 if unlimited
  • Session-Timeout - max lease time (lease-time)


General

Sub-menu: /ip dhcp-server


Property Description
add-arp (yes | no; Default: no) Whether to add dynamic ARP entry. If set to no either ARP mode should be enabled on that interface or static ARP entries should be administratively defined in /ip arp submenu
address-pool (string | static-only; Default: static-only) IP pool, from which to take IP addresses for clients. If set to static-only, then 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) will be allowed
always-broadcast (yes | no; Default: no) Always send replies as broadcasts
authoritative (after-10sec-delay | after-2sec-delay | no | yes; Default: after-2sec-delay) Whether the DHCP server is the only one DHCP server for the network:
  • after-10sec-delay - to clients request for an address, dhcp server will wait 10 seconds and if there is another request from the client after this period of time, then dhcp server will offer the address to the client or will send DHCPNAK, if the requested address is not available from this server
  • after-2sec-delay - to clients request for an address, dhcp server will wait 2 seconds and if there is another request from the client after this period of time, then dhcp server will offer the address to the client or will send DHCPNAK, if the requested address is not available from this server
  • yes - to clients request for an address that is not available from this server, dhcp server will send negative acknowledgment (DHCPNAK)
  • no - dhcp server ignores clients requests for addresses that are not available from this server
boot-support (none | static | dynamic; Default: static) Support for BOOTP clients
  • none - do not respond to BOOTP requests
  • static - offer only static leases to BOOTP clients
  • dynamic - offer static and dynamic leases for BOOTP clients
delay-threshold (time; Default: none) If secs field in DHCP packet is smaller than delay-threshold, then this packet is ignored. If set to none - there is no threshold (all DHCP packets are processed)
interface (string; Default: ) Interface on which server will be running
lease-time (time; Default: 72h) the time that a client may use the assigned 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 (string; Default: ) Reference name
relay (IP; 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; 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
use-radius (yes | no; Default: no) Whether to use RADIUS server for dynamic leases

Menu specific commands

Property Description
setup () Release current binding and restart DHCP client


Server configuration

Sub-menu: /ip dhcp-server config


Leases are always stored on disk on graceful shutdown and reboot. If they would be saved on disk on every lease change, a lot of disk writes would happen. There are no problems if it happens on a hard drive, but is very bad for Compact Flash (especially, if lease times are very short). To minimize writes on disk, all changes are saved on disk every store-leases-disk seconds. If this time will be very short (immediately), then no changes will be lost even in case of hard reboots and power losts. But, on CF there may be too many writes in case of short lease times (as in case of hotspot). If this time will be very long (never), then there will be no writes on disk, but information about active leases may be lost in case of power loss. In these cases dhcp server may give out the same ip address to another client, if first one will not respond to ping requests.

store-leases-disk (time | immediately | never; Default: 5min) How frequently lease changes should be stored on disk

Networks

Sub-menu: /ip dhcp-server network

Property Description
address (IP/netmask; Default: ) the network DHCP server(s) will lend addresses from
boot-file-name (string; Default: ) Boot file name
dhcp-option (string; Default: ) Add additional DHCP options from option list.
dns-server (string; Default: ) 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 (string; Default: ) The DHCP client will use this as the 'DNS domain' setting for the network adapter.
gateway (IP; Default: 0.0.0.0) The default gateway to be used by DHCP Client.
netmask (integer: 0..32; Default: 0) The actual network mask to be used by DHCP client. If set to '0' - netmask from network address will be used.
next-server (IP; Default: ) IP address of next server to use in bootstrap.
ntp-server (IP; Default: ) the DHCP client will use these as the default NTP servers. Two comma-separated NTP servers can be specified to be used by DHCP client as primary and secondary NTP servers
wins-server (IP; Default: ) 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

Leases

Sub-menu: /ip dhcp-server lease


DHCP server lease submenu is used to monitor and manage server's leases. The issued leases are showed here as dynamic entries. You can also add static leases to issue a particular client (identified by MAC address) the desired 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)

A client may free the leased address. 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.

Plik:Icon-note.png

Przypis: that the IP addresses assigned statically are not probed.



Properties

Property Description
address (IP; Default: ) Specify ip address (or ip pool) for static lease. If set to 0.0.0.0 - pool from server will be used
always-broadcast (yes | no; Default: ) Send all repies as broadcasts
block-access (yes | no; Default: no) Block access for this client
client-id (string; Default: ) If specified, must match DHCP 'client identifier' option of the request
lease-time (time; Default: 0s) Time that the client may use the address. If set to 0s lease will never expire.
mac-address (MAC; Default: 00:00:00:00:00:00) If specified, must match the MAC address of the client
src-mac-address (MAC; Default: ) Source MAC address
use-src-mac (MAC; Default: ) Use this source MAC address instead


Read only properties

Property Description
active-address (IP) Actual IP address for this lease
active-client-id (string) Actual client-id of the client
active-mac-address (MAC) Actual MAC address of the client
active-server (list) Actual dhcp server, which serves this client
agent-circuit-id (string) Circuit ID of DHCP relay agent
agent-remote-id (string) Remote ID, set by DHCP relay agent
blocked ( flag ) Whether the lease is blocked
expires-after (time) Time until lease expires
host-name (text) Shows host name option from last received DHCP request
radius (yes | no) Shows, whether this dynamic lease is authenticated by RADIUS or not
rate-limit (string) Sets rate limit for active lease. Format is: rx-rate[/tx-rate] [rx-burst-rate[/tx-burst-rate] [rx-burst-threshold[/tx-burst-threshold] [rx-burst-time[/tx-burst-time]]]]. All rates should be numbers with optional 'k' (1,000s) or 'M' (1,000,000s). If tx-rate is not specified, rx-rate is as tx-rate too. Same goes for tx-burst-rate and tx-burst-threshold and tx-burst-time. If both rx-burst-threshold and tx-burst-threshold are not specified (but burst-rate is specified), rx-rate and tx-rate is used as burst thresholds. If both rx-burst-time and tx-burst-time are not specified, 1s is used as default
server (string) Server name which serves this client
status (waiting | testing | authorizing | 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
  • authorizing - waiting for response from radius server
  • 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


Menu specific commands

Property Description
check-status (id) Check status of a given busy dynamic lease, and free it in case of no response
make-static (id) Convert a dynamic lease to a static one


Alerts

Sub-menu: /ip dhcp-server alert


To find any rogue DHCP servers as soon as they appear in your network, DHCP Alert tool can be used. It will monitor ethernet for all DHCP replies and check, whether this reply comes from a valid DHCP server. If reply from unknown DHCP server is detected, alert gets triggered:

[admin@MikroTik] ip dhcp-server alert>/log print
00:34:23 dhcp,critical,error,warning,info,debug dhcp alert on Public:
    discovered unknown dhcp server, mac 00:02:29:60:36:E7, ip 10.5.8.236
[admin@MikroTik] ip dhcp-server alert>

When the system alerts about a rogue DHCP server, it can execute a custom script.

As DHCP replies can be unicast, rogue dhcp detector may not receive any offer to other dhcp clients at all. To deal with this, rogue dhcp detector acts as a dhcp client as well - it sends out dhcp discover requests once a minute

Properties

Property Description
alert-timeout (none | time; Default: none) Time, after which alert will be forgotten. If after that time the same server will be detected, new alert will be generated. If set to none timeout will never expire.
interface (string; Default: ) Interface, on which to run rogue DHCP server finder.
on-alert (string; Default: ) Script to run, when an unknown DHCP server is detected.
valid-server (string; Default: ) List of MAC addresses of valid DHCP servers.


Read only properties

Property Description
unknown-server (string) List of MAC addresses of detected unknown DHCP servers. Server is removed from this list after alert-timeout


Menu specific commands

Property Description
reset-alert (id) Clear all alerts on an interface

DHCP Options

Sub-menu: /ip dhcp-server option


With help of DHCP Option list, it is possible to define additional custom options for DHCP Server to advertise.

According to the DHCP protocol, a parameter is returned to the DHCP client only if it requests this parameter, specifying the respective code in DHCP request Parameter-List (code 55) attribute. If the code is not included in Parameter-List attribute, DHCP server will not send it to the DHCP client.

Properties

Property Description
code (integer:1..254; Default: ) dhcp option code. All codes are available at http://www.iana.org/assignments/bootp-dhcp-parameters
name (string; Default: ) Descriptive name of the option
value (string; Default: ) Parameter's value in form of a string. If the string begins with "0x", it is assumed as a hexadecimal value

Example

Classless route adds specified route in clients routing table. In our example it will add dst-address=160.0.0.0/24 gateway=10.1.101.1

/ip dhcp-server option
add code=121 name=classless value=0x18A000000A016501000A016501
/ip dhcp-server network
set 0 dhcp-option=classless

Result:

[admin@MikroTik] /ip route> print
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf,
m - mme, B - blackhole, U - unreachable, P - prohibit
 #      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
 0 ADS  0.0.0.0/0                          10.1.101.1         0
 1 ADS  160.0.0.0/24                       10.1.101.1         0

Basic examples

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.20
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.20

[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>