ROS DHCP Solihin
ROS DHCP Solihin
DHCP Client
Summary
DHCP Options
Properties
Configuration Examples
Simple DHCP client
Lease script example
Resolve default gateway when 'router' (option3) is from a different subnet
DHCPv6 Client
Summary
Properties
Script
IAID
Configuration Examples
Simple DHCPv6 client
Use received prefix for local RA
DHCP Server
Summary
DHCP Server Properties
Leases
Menu specific commands
Store Configuration
Rate limiting
Network
RADIUS Support
Alerts
DHCP Options
DHCP Option Sets
Example
Vendor Classes
Example
Generic matcher
Configuration Examples
Setup
Manual configuration
DHCPv6 Server
Summary
General
DHCPv6 Server Properties
Bindings
Rate limiting
RADIUS Support
Configuration Example
Enabling IPv6 Prefix delegation
DHCP Relay
Summary
Properties
Configuration Example
DHCP Client
Summary
/ip dhcp-client
The DHCP (Dynamic Host Configuration Protocol) is used for the 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 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 to the DHCP client obtaining one, the route obtained by the DHCP client would be shown as invalid.
option 1 - SUBNET_MASK,
option 3 - GATEWAY_LIST,
option 6 - TAG_DNS_LIST,
option 33 - STATIC_ROUTE,
option 42 - NTP_LIST,
option 121 - CLASSLESS_ROUTE,
DHCP Options
DHCP client has the possibility to set up options that are sent to the DHCP server. For example, hostname and MAC address. The syntax is the same as
for DHCP server options.
HOSTNAME;
CLIENT_MAC - client interface MAC address;
CLIENT_DUID - client DIUD of the router, same as used for the DHCPv6 client. In conformance with RFC4361
clientid_duid 61 0xff$(CLIENT_DUID)
clientid 61 0x01$(CLIENT_MAC)
hostname 12 $(HOSTNAME)
Properties
Property Description
add-default-route (yes | Whether to install default route in routing table received from DHCP server. By default, the RouterOS client complies with
no | special-classless; RFC and ignores option 3 if classless option 121 is received. To force the client not to ignore option 3 set special-classless.
Default: yes) This parameter is available in v6rc12+
yes - adds classless route if received, if not then add default route (old behavior)
special-classless - adds both classless routes if received and a default route (MS style)
client-id (string; Default: ) Corresponds to the settings suggested by the network administrator or ISP. If not specified, the client's MAC address will be
sent
host-name (string; The hostname of the client is sent to a DHCP server. If not specified, the client's system identity will be used.
Default: )
interface (string; The interface on which the DHCP client will be running.
Default: )
script (script; Default: ) Execute script when DHCP client obtains a new lease or loses an existing one. This parameter is available in v6.39rc33+
These are available variables that are accessible for the event script:
Example >>
use-peer-dns (yes | no; Whether to accept the DNS settings advertised by DHCP Server. (Will override the settings put in the /ip dns submenu.
Default: yes)
use-peer-ntp (yes | no; Whether to accept the NTP settings advertised by DHCP Server. (Will override the settings put in the /system ntp
Default: yes) client submenu)
Read-only properties
Property Description
address (IP/Netmask) IP address and netmask, which is assigned to DHCP Client from the Server
expires-after (time) A time when the lease expires (specified by the DHCP server).
gateway (IP) The IP address of the gateway which is assigned by the DHCP server
netmask (IP)
primary-dns (IP) The IP address of the first DNS resolver, which was assigned by the DHCP
server
primary-ntp (IP) The IP address of the primary NTP server, assigned by the DHCP server
secondary-dns (IP) The IP address of the second DNS resolver, assigned by the DHCP server
secondary-ntp (IP) The IP address of the secondary NTP server, assigned by the DHCP server
status (bound | error | rebinding... | requesting... | searching... | Shows the status of the DHCP Client
stopped)
Property Description
release (nu Release current binding and restart the DHCP client
mbers)
renew (num Renew current leases. If the renewal operation was not successful, the client tries to reinitialize the lease (i.e. it starts the lease request
bers) procedure (rebind) as if it had not received an IP address yet)
Configuration Examples
If the interface used by the DHCP client is part of the VRF configuration, then the default route and other received routes from the DHCP server
will be added to the VRF routing table.
/ip dhcp-client
add add-default-route=no dhcp-options=hostname,clientid disabled=no interface=ether2 script="{\r\
\n :local rmark \"WAN1\"\r\
\n :local count [/ip route print count-only where comment=\"WAN1\"]\r\
\n :if (\$bound=1) do={\r\
\n :if (\$count = 0) do={\r\
\n /ip route add gateway=\$\"gateway-address\" comment=\"WAN1\" routing-mark=\$rmark\r\
\n } else={\r\
\n :if (\$count = 1) do={\r\
\n :local test [/ip route find where comment=\"WAN1\"]\r\
\n :if ([/ip route get \$test gateway] != \$\"gateway-address\") do={\r\
\n /ip route set \$test gateway=\$\"gateway-address\"\r\
\n }\r\
\n } else={\r\
\n :error \"Multiple routes found\"\r\
\n }\r\
\n }\r\
\n } else={\r\
\n /ip route remove [find comment=\"WAN1\"]\r\
\n }\r\
\n}\r\
\n"
To fix this we need to add /32 route to resolve the gateway over ether1, which can be done by the running script below each time the DHCP client gets an
address
/system script add name="dhcpL" source={ /ip address add address=($"lease-address" . "/32") network=$"gateway-
address" interface=$interface }
Now we can further extend the script, to check if the address already exists, and remove the old one if changes are needed
DHCPv6 Client
Summary
Sub-menu: /ipv6 dhcp-client
DHCP-client in RouterOS is capable of being a DHCPv6-client and DHCP-PD client. So it is able to get a prefix from the DHCP-PD server as well as the
DHCPv6 stateful address from the DHCPv6 server.
Properties
Property Description
add-default-route (ye Whether to add default IPv6 route after a client connects.
s | no; Default: no)
interface (string; The interface on which the DHCPv6 client will be running.
Default: )
pool-name (string; Name of the IPv6 pool in which received IPv6 prefix will be added
Default: )
pool-prefix-length (st Prefix length parameter that will be set for IPv6 pool in which received IPv6 prefix is added. Prefix length must be greater than
ring; Default: ) the length of the received prefix, otherwise, prefix-length will be set to received prefix length + 8 bits.
request (prefix, to choose if the DHCPv6 request will ask for the address or the IPv6 prefix, or both.
address; Default: )
script (string; Run this script on the DHCP-client status change. Available variables:
Default: )
pd-valid - if the prefix is acquired by the client;
pd-prefix - the prefix acquired by the client if any;
na-valid - if the address is acquired by the client;
na-address - the address acquired by the client if any.
options - array of received options (only ROSv7)
use-peer-dns (yes | Whether to accept the DNS settings advertised by the IPv6 DHCP Server.
no; Default: yes)
Read-only properties
Property Description
duid (string) Auto-generated DUID that is sent to the server. DUID is generated using one of the
MAC addresses available on the router.
expires-after (time) A time when the IPv6 prefix expires (specified by the DHCPv6 server).
prefix (IPv6 prefix) Shows received IPv6 prefix from DHCPv6-PD server
status (stopped | searching | requesting... | bound | renewing Shows the status of DHCPv6 Client:
| rebinding | error | stopping)
stopped - dhcpv6 client is stopped
searching - sending "solicit" and trying to get "advertise"
requesting - sent "request" waiting for "reply"
bound - received "reply". Prefix assigned.
renewing - sent "renew", waiting for "reply"
rebinding - sent "rebind", waiting for "reply"
error - reply was not received in time or some other error occurred.
stopping - sent "release"
Property Description
renew (nu Renew current leases. If the renewal operation was not successful, the client tries to reinitialize the lease (i.e. it starts the lease
mbers) request procedure (rebind) as if it had not received an IP address yet)
Script
It is possible to add a script that will be executed when a prefix or an address is acquired and applied or expires and is removed using the DHCP client.
There are separated sets of variables that will have the value set by the client depending on prefix or address status change as the client can acquire both
and each of them can have a different effect on the router configuration.
IAID
To determine what IAID will be used, convert the internal ID of an interface on which the DHCP client is running from hex to decimal.
For example, the DHCP client is running on interface PPPoE-out1. To get internal ID use the following command:
Configuration Examples
Detailed print should show status of the client and we can verify if prefix is received
Notice that server gave us prefix 2a02:610:7501:ff04::/62 . And it should be also added to ipv6 pools
It works! Now you can use this pool, for example, for pppoe clients.
Configuration
R1
/ipv6 route
add gateway=fe80::1:1%to-ISP
/ipv6 pool
add name=myPool prefix=2001:db8::/62 prefix-length=64
/ipv6 dhcp-server
add address-pool=myPool disabled=no interface=to-CE-routers lease-time=3m name=server1
CE1
/ipv6 dhcp-client
add interface=to-R1 request=prefix pool-name=my-ipv6
/ipv6 address
add address=::1/64 from-pool=my-ipv6 interface=to-clients advertise=yes
CE2
/ipv6 dhcp-client
add interface=to-R1 request=prefix pool-name=my-ipv6
/ipv6 address add address=::1/64 from-pool=my-ipv6 interface=to-clients advertise=yes
After configuration is complete we can verify that each CE router received its own prefix
On server:
On client:
We can also see that IPv6 address was automatically added from the prefix pool:
Summary
The DHCP (Dynamic Host Configuration Protocol) is used for the 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 for the DHCP server to work, IP pools must also be configured (do not include the DHCP server's own IP address into the pool range) and the
DHCP networks.
It is also possible to hand out leases for DHCP clients using the RADIUS server; the supported parameters for a RADIUS server are as follows:
Access-Request:
Access-Accept:
DHCP server requires a real interface to receive raw ethernet packets. If the interface is a Bridge interface, then the Bridge must have a real
interface attached as a port to that bridge which will receive the raw ethernet packets. It cannot function correctly on a dummy (empty bridge)
interface.
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 entri
es should be administratively defined in /ip arp submenu.
address-pool (string | static- IP pool, from which to take IP addresses for the clients. If set to static-only, then only the clients that have a static lease
only; Default: static-only) (added in the lease submenu) will be allowed.
allow-dual-stack-queue (yes Creates a single simple queue entry for both IPv4 and IPv6 addresses, and uses the MAC address and DUID for
| no; Default: yes) identification. Requires IPv6 DHCP Server to have this option enabled as well to work properly.
always-broadcast (yes | no; Always send replies as broadcasts even if the destination IP is known. Will add additional load on L2 network.
Default: no)
authoritative (after-10sec- Option changes the way how a server responds to DHCP requests:
delay | after-2sec-delay | yes
| no; Default: yes) yes - replies to clients' requests for an address that is not available from this server, DHCP server will send a
negative acknowledgment (DHCPNAK);
no - DHCP server ignores clients' requests for addresses that are not available from this server;
after-10sec-delay - requests with "secs < 10" will be processed as in "no" setting case and requests with "secs >=
10" will be processed as in "yes" case;
after-2sec-delay - requests with "secs < 2" will be processed as in "no" setting case and requests with "secs >= 2"
will be processed as in "yes" case;
If all requests with "secs < x" should be ignored, then delay-threshold=x setting should be used.
client-mac-limit (integer | Specifies whether to limit a specific number of clients per single MAC address or leave unlimited. Note that this setting
unlimited; Default: unlimited) should not be used in relay setups.
conflict-detection (yes | no; Allows disabling/enabling conflict detection. If the option is enabled, then whenever the server tries to assign a lease it
Default: yes) will send ICMP and ARP messages to detect whether such an address in the network already exists. If any of the above
get a reply address is considered already used.
delay-threshold (time | none; If the sec's field in the DHCP packet is smaller than the delay threshold, then this packet is ignored. If set to none - there
Default: none) is no threshold (all DHCP packets are processed)
dhcp-option-set (name | none Use a custom set of DHCP options defined in the option sets menu.
; Default: none)
insert-queue-before (bottom Specify where to place dynamic simple queue entries for static DCHP leases with a rate-limit parameter set.
| first | name; Default: first)
interface (string; Default: ) The interface on which the DHCP server will be running.
lease-script (string; Default: "" A script that will be executed after a lease is assigned or de-assigned. Internal "global" variables that can be used in the
) script:
lease-time (time; Default: 30m The time that a client may use the assigned address. The client will try to renew this address after half of this time and
) will request a new address after the time limit expires.
parent-queue (string | none; A dynamically created queue for this lease will be configured as a child queue of the specified parent queue.
Default: none)
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 relay allowed)
255.255.255.255 - the DHCP server should be used for any incoming request from a DHCP relay except for those,
which are processed by another DHCP server that exists in the /ip dhcp-server submenu.
server-address (IP; Default: 0 The IP address of the server to use in the next step of the client's bootstrap process (For example, to assign a specific
.0.0.0) server address in case several addresses are assigned to the interface)
use-framed-as-classless (yes Forward RADIUS Framed-Route as a DHCP Classless-Static-Route to DHCP-client. Whenever both Framed-Route and
| no; Default: yes) Classless-Static-Route are received Classless-Static-Route is preferred.
Leases
Sub-menu: /ip dhcp-server lease
DHCP server lease submenu is used to monitor and manage server leases. The issued leases are shown here as dynamic entries. You can also add static
leases to issue a specific IP address to a particular client (identified by MAC address).
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 reacquires the address.
Property Description
address (IP; Default: 0.0.0.0) Specify IP address (or ip pool) for static lease. If set to 0.0.0.0 - a pool from the DHCP server will be used
address-list (string; Default: none) Address list to which address will be added if the lease is bound.
allow-dual-stack-queue (yes | no; Creates a single simple queue entry for both IPv4 and IPv6 addresses, and uses the MAC address and DUID
Default: yes) for identification. Requires IPv6 DHCP Server to have this option enabled as well to work properly.
client-id (string; Default: none) If specified, must match the DHCP 'client identifier' option of the request
dhcp-option (string; Default: none) Add additional DHCP options from option list.
dhcp-option-set (string; Default: n Add an additional set of DHCP options.
one)
insert-queue-before (bottom | Specify where to place dynamic simple queue entries for static DCHP leases with rate-limit parameter set.
first | name; Default: first)
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: If specified, must match the MAC address of the client
00:00:00:00:00)
parent-queue (string | none; A dynamically created queue for this lease will be configured as a child queue of the specified parent queue.
Default: none)
queue-type (default, ethernet- Queue type that can be assigned to the specific lease
default, multi-queue-ethernet-
default, pcq-download-default,
synchronous-default, default-
small, hotspot-default, only-
hardware-queue, pcq-upload-
default, wireless-default)
rate-limit (integer[/integer] [integer Adds a dynamic simple queue to limit IP's bandwidth to a specified rate. Requires the lease to be static. Format
[/integer] [integer[/integer] [integer is: rx-rate[/tx-rate] [rx-burst-rate[/tx-burst-rate] [rx-burst-threshold[/tx-burst-threshold] [rx-burst-time[/tx-burst-
[/integer]]]];; Default: ) 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.
routes ([dst-address/mask] Routes that appear on the server when the client is connected. It is possible to specify multiple routes
[gateway] [distance]; Default: none separated by commas. This setting will be ignored for OpenVPN.
)
use-src-mac (yes | no; Default: no) When this option is set server uses the source MAC address instead of the received CHADDR to assign the
address.
check-status (id) Check the status of a given busy (status is conflict or declined) dynamic lease, and free it in case of no response
Store Configuration
Sub-menu: /ip dhcp-server config
This sub-menu allows the configuration of how often the DHCP leases will be stored on disk. If they would be saved on a disk on every lease change, a lot
of disk writes would happen which 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. Additionally, leases are always stored on disk on graceful shutdown and reboot.
Manual changes to leases - addition/removal of a static lease, removal of a dynamic lease will cause changes to be pushed for this lease to storage.
Rate limiting
It is possible to set the bandwidth to a specific IPv4 address by using DHCPv4 leases. This can be done by setting a rate limit on the DHCPv4 lease itself,
by doing this a dynamic simple queue rule will be added for the IPv4 address that corresponds to the DHCPv4 lease. By using the rate-limit parameter you
can conveniently limit a user's bandwidth.
For any queues to work properly, the traffic must not be FastTracked, make sure your Firewall does not FastTrack traffic that you want to limit.
First, make the DHCPv4 lease static, otherwise, it will not be possible to set a rate limit to a DHCPv4 lease:
[admin@MikroTik] > /ip dhcp-server lease print
Flags: X - disabled, R - radius, D - dynamic, B - blocked
# ADDRESS MAC-ADDRESS HOST-NAME SERVER RATE-
LIMIT STATUS
0 D 192.168.88.254 6C:3B:6B:7C:41:3E MikroTik
DHCPv4_Server bound
Then you can set a rate to a DHCPv4 lease that will create a new dynamic simple queue entry:
By default allow-dual-stack-queue is enabled, this will add a single dynamic simple queue entry for both DCHPv6 binding and DHCPv4 lease,
without this option enabled separate dynamic simple queue entries will be added for IPv6 and IPv4.
If allow-dual-stack-queue is enabled, then a single dynamic simple queue entry will be created containing both IPv4 and IPv6 addresses:
Network
Sub-menu: /ip dhcp-server network
Properties
Property Description
address (IP the network DHCP server(s) will lease addresses from
/netmask;
Default: )
caps-manager ( A comma-separated list of IP addresses for one or more CAPsMAN system managers. DHCP Option 138 (capwap) will be used.
string; Default: )
dhcp-option (stri Add additional DHCP options from the option list.
ng; Default: )
dns-none (yes | If set, then DHCP Server will not pass dynamic DNS servers configured on the router to the DHCP clients if no DNS Server in DN
no; Default: no) S-server is set. By default, if there are no DNS servers configured, then the dynamic DNS Servers will be passed to DHCP clients.
dns-server (string the DHCP client will use these as the default DNS servers. Two comma-separated DNS servers can be specified to be used by
; Default: ) the DHCP client as primary and secondary DNS servers
domain (string; The DHCP client will use this as the 'DNS domain' setting for the network adapter.
Default: )
netmask (intege The actual network mask is to be used by the DHCP client. If set to '0' - netmask from network address will be used.
r: 0..32; Default:
0)
ntp-server (IP; the DHCP client will use these as the default NTP servers. Two comma-separated NTP servers can be specified to be used by
Default: ) the DHCP client as primary and secondary NTP servers
wins-server (IP; The Windows DHCP client will use these as the default WINS servers. Two comma-separated WINS servers can be specified to
Default: ) be used by the DHCP client as primary and secondary WINS servers
RADIUS Support
Since RouterOS v6.43 it is possible to use RADIUS to assign a rate limit per lease, to do so you need to pass the Mikrotik-Rate-Limit attribute from your
RADIUS Server for your lease. To achieve this you first need to set your DHCPv4 Server to use RADIUS for assigning leases. Below is an example of how
to set it up:
/radius
add address=10.0.0.1 secret=VERYsecret123 service=dhcp
/ip dhcp-server
set dhcp1 use-radius=yes
After that, you need to tell your RADIUS Server to pass the Mikrotik-Rate-Limit attribute. In case you are using FreeRADIUS with MySQL, then you need to
add appropriate entries into radcheck and radreply tables for a MAC address, that is being used for your DHCPv4 Client. Below is an example for table
entries:
Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
Alerts
To find any rogue DHCP servers as soon as they appear in your network, the DHCP Alert tool can be used. It will monitor the interface for all DHCP replies
and check if this reply comes from a valid DHCP server. If a reply from an unknown DHCP server is detected, an alert gets triggered:
As DHCP replies can be unicast, the rogue DHCP detector may not receive any offer to other DHCP clients at all. To deal with this, the rogue DHCP
detector acts as a DHCP client as well - it sends out DHCP discover requests once a minute.
The DHCP alert is not recommended on devices that are configured as DHCP clients. Since the alert itself generates DHCP discovery packets,
it can affect the operation of the DHCP client itself. Use this feature only on devices that are DHCP servers or using a static IP address.
Properties
Property Description
alert-timeout (none | time; Time after which the alert will be forgotten. If after that time the same server is detected, a new alert will be generated.
Default: 1h) 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.
Read-only properties
Property Description
unknown-server (string) List of MAC addresses of detected unknown DHCP servers. The server is removed from this list after alert-timeout
Property Description
DHCP Options
Sub-menu: /ip dhcp-server option
With the help of the DHCP Option list, it is possible to define additional custom options for DHCP Server to advertise. Option precedence is as follows:
radius,
lease,
server,
network.
This is the order in which the client option request will be filled in.
According to the DHCP protocol, a parameter is returned to the DHCP client only if it requests this parameter, specifying the respective code in the DHCP
request Parameter-List (code 55) attribute. If the code is not included in the Parameter-List attribute, the DHCP server will not send it to the DHCP client,
but since RouterOS v7.1rc5 it is possible to force the DHCP option from the server-side even if the DHCP-client does not request such parameter:
ip/dhcp-server/option/set force=yes
Properties
Property Description
code (integer:1..254; dhcp option code. All codes are available at http://www.iana.org/assignments/bootp-dhcp-parameters
Default: )
name (string; Default: ) Descriptive name of the option
value (string; Default: ) Parameter's value. Available data types for options are:
Now it is also possible to combine data types into one, for example: "0x01'vards'$(HOSTNAME)"
raw-value (HEX string ) Read-only field which shows raw DHCP option value (the format actually sent out)
This menu allows combining multiple options in option sets, which later can be used to override the default DHCP server option set.
Example
Classless Route
A classless route adds a specified route in the clients routing table. In our example, it will add
dst-address=160.0.0.0/24 gateway=10.1.101.1
dst-address=0.0.0.0/0 gateway=10.1.101.1
According to RFC 3442: The first part is the netmask ("18" = netmask /24). Second part is significant part of destination network ("A00000" = 160.0.0).
Third part is IP address of gateway ("0A016501" = 10.1.101.1). Then There are parts of the default route, destination netmask (0x00 = 0.0.0.0/0) followed
by default route (0x0A016501 = 10.1.101.1)
Result:
A much more robust way would be to use built-in variables, the previous example can be rewritten as:
/ip dhcp-server option
add name=classless code=121 value="0x18A00000\$(NETWORK_GATEWAY)0x00\$(NETWORK_GATEWAY)"
Vendor Classes
Since the 6.45beta6 version RouterOS support vendor class, ID matcher. The vendor class is used by DHCP clients to optionally identify the vendor and
configuration.
Example
In the following configuration example, we will give an IP address from a particular pool for an Android-based mobile phone. We will use the RouterBOARD
with a default configuration
/ip pool
add name=default-dhcp ranges=192.168.88.10-192.168.88.254
add name=pool-for-VID ranges=172.16.16.10-172.16.16.120
/ip dhcp-server
add address-pool=default-dhcp disabled=no interface=bridge name=defconf
/ip dhcp-server network
add address=192.168.88.0/24 comment=defconf gateway=192.168.88.1
/ip dhcp-server vendor-class-id
add address-pool=pool-for-VID name=samsung server=defconf vid=android-dhcp-9
Connect your mobile phone to the device to receive an IP address from the 172.16.16.0 network
If you do not know your devices Vendor Class ID, you can turn on DHCP debug logs with /system logging add topics=dhcp. Then in the logging
entries, you will see Class-ID
10:30:31 dhcp,debug,packet defconf received request with id 4238230732 from 0.0.0.0
10:30:31 dhcp,debug,packet secs = 3
10:30:31 dhcp,debug,packet ciaddr = 0.0.0.0
10:30:31 dhcp,debug,packet chaddr = 30:07:4D:F5:07:49
10:30:31 dhcp,debug,packet Msg-Type = request
10:30:31 dhcp,debug,packet Client-Id = 01-30-07-4D-F5-07-49
10:30:31 dhcp,debug,packet Address-Request = 172.16.16.120
10:30:31 dhcp,debug,packet Server-Id = 192.168.88.1
10:30:31 dhcp,debug,packet Max-DHCP-Message-Size = 1500
10:30:31 dhcp,debug,packet Class-Id = "android-dhcp-9"
10:30:31 dhcp,debug,packet Host-Name = "Galaxy-S8"
10:30:31 dhcp,debug,packet Parameter-List = Subnet-Mask,Router,Domain-Server,Domain-Name,Interface-MTU,
Broadcast-Address,Address-Time,Ren
ewal-Time,Rebinding-Time,Vendor-Specific
10:30:31 dhcp,info defconf assigned 172.16.16.120 to 30:07:4D:F5:07:49
10:30:31 dhcp,debug,packet defconf sending ack with id 4238230732 to 172.16.16.120
10:30:31 dhcp,debug,packet ciaddr = 0.0.0.0
10:30:31 dhcp,debug,packet yiaddr = 172.16.16.120
10:30:31 dhcp,debug,packet siaddr = 192.168.88.1
10:30:31 dhcp,debug,packet chaddr = 30:07:4D:F5:07:49
10:30:31 dhcp,debug,packet Msg-Type = ack
10:30:31 dhcp,debug,packet Server-Id = 192.168.88.1
10:30:31 dhcp,debug,packet Address-Time = 600
10:30:31 dhcp,debug,packet Domain-Server = 192.168.88.1,10.155.0.1,10.155.0.126
Generic matcher
Since RouterOS 7.4beta4 (2022-Jun-15 14:04) the vendor-id matcher is converted to a generic matcher. The genric matcher allows matching any of the
DHCP options.
Configuration Examples
Setup
To simply configure DHCP server you can use a setup command.
Then you use setup a command which will automatically ask necessary parameters:
[admin@MikroTik] > /ip dhcp-server setup
Select interface to run DHCP server on
Manual configuration
To configure the DHCP server manually to respond to local requests you have to configure the following:
An IP pool for addresses to be given out, make sure that your gateway/DHCP server address is not part of the pool.
A network indicating subnets that DHCP-server will lease addresses from, among other information, like a gateway, DNS-server, NTP-server,
DHCP options, etc.
In our case, the device itself is serving as the gateway, so we'll add the address to the bridge interface:
And finally, add DHCP Server, here we will add the previously created address pool, and specify on which interface the DHCP server should work
on
DHCPv6 Server
Summary
Standards: RFC 3315, RFC 3633
Single DUID is used for client and server identification, only IAID will vary between clients corresponding to their assigned interface.
Client binding creates a dynamic pool with a timeout set to binding's expiration time (note that now dynamic pools can have a timeout), which will be
updated every time binding gets renewed.
When a client is bound to a prefix, the DHCP server adds routing information to know how to reach the assigned prefix.
Client bindings in the server do not show MAC address anymore (as it was in v5.8), DUID (hex) and IAID are used instead. After upgrade, MAC addresses
will be converted to DUIDs automatically, but due to unknown DUID type and unknown IAID, they should be further updated by the user;
RouterOS DHCPv6 server can only delegate IPv6 prefixes, not addresses.
General
Sub-menu: /ipv6 dhcp-server
Property Description
address-pool (enum | static- IPv6 pool, from which to take IPv6 prefix for the clients.
only; Default: static-only)
allow-dual-stack-queue (yes Creates a single simple queue entry for both IPv4 and IPv6 addresses, and uses the MAC address and DUID for
| no; Default: yes) identification. Requires IPv6 DHCP Server to have this option enabled as well to work properly.
binding-script (string; A script that will be executed after binding is assigned or de-assigned. Internal "global" variables that can be used in the
Default: ) script:
dhcp-option (string; Default: n Add additional DHCP options from option list.
one)
disabled (yes | no; Default: no Whether DHCP-PD server participates in the prefix assignment process.
)
lease-time (time; Default: 3d) The time that a client may use the assigned address. The client will try to renew this address after half of this time and
will request a new address after the time limit expires.
Read-only Properties
Property Description
Bindings
Sub-menu: /ipv6 dhcp-server binding
DUID is used only for dynamic bindings, so if it changes then the client will receive a different prefix than previously.
Property Description
address (IPv6 IPv6 prefix that will be assigned to the client
prefix; Default: )
allow-dual-stack- Creates a single simple queue entry for both IPv4 and IPv6 addresses, uses the MAC address and DUID for identification. Requires I
queue (yes | no; Pv4 DHCP Server to have this option enabled as well to work properly.
Default: yes)
dhcp-option (stri Add additional DHCP options from the option list.
ng; Default: )
duid (hex string; DUID value. Should be specified only in hexadecimal format.
Default: )
iaid (integer [0.. Identity Association Identifier, part of the Client ID.
4294967295];
Default: )
prefix-pool (string Prefix pool that is being advertised to the DHCPv6 Client.
; Default: )
rate-limit (integer Adds a dynamic simple queue to limit IP's bandwidth to a specified rate. Requires the lease to be static. Format is: rx-rate[/tx-rate] [rx-
[/integer] [integer burst-rate[/tx-burst-rate] [rx-burst-threshold[/tx-burst-threshold] [rx-burst-time[/tx-burst-time]]]]. All rates should be numbers with
[/integer] [integer 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-
[/integer] [integer 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
[/integer]]]]; tx-rate is used as burst thresholds. If both rx-burst-time and tx-burst-time are not specified, 1s is used as default.
Default: )
server (string | Name of the server. If set to all, then binding applies to all created DHCP-PD servers.
all; Default: all)
Read-only properties
Property Description
last-seen (time) Time period since the client was last seen.
Property Description
Rate limiting
It is possible to set the bandwidth to a specific IPv6 address by using DHCPv6 bindings. This can be done by setting a rate limit on the DHCPv6 binding
itself, by doing this a dynamic simple queue rule will be added for the IPv6 address that corresponds to the DHCPv6 binding. By using the rate-limit
the parameter you can conveniently limit a user's bandwidth.
For any queues to work properly, the traffic must not be FastTracked, make sure your Firewall does not FastTrack traffic that you want to limit.
First, make the DHCPv6 binding static, otherwise, it will not be possible to set a rate limit to a DHCPv6 binding:
Then you need can set a rate to a DHCPv6 binding that will create a new dynamic simple queue entry:
By default allow-dual-stack-queue is enabled, this will add a single dynamic simple queue entry for both DCHPv6 binding and DHCPv4
lease, without this option enabled separate dynamic simple queue entries will be added for IPv6 and IPv4.
If allow-dual-stack-queue is enabled, then a single dynamic simple queue entry will be created containing both IPv4 and IPv6 addresses:
[admin@MikroTik] > /queue simple print
Flags: X - disabled, I - invalid, D - dynamic
0 D name="dhcp-ds<6C:3B:6B:7C:41:3E>" target=192.168.1.200/32,fdb4:4de7:a3f8:418c::/66 parent=none packet-
marks="" priority=8/8 queue=default
-small/default-small limit-at=10M/10M max-limit=10M/10M
burst-limit=0/0 burst-threshold=0/0 burst-time=0s/0s bucket-size=0.1/0.1
RADIUS Support
Since RouterOS v6.43 it is possible to use RADIUS to assign a rate-limit per DHCPv6 binding, to do so you need to pass the Mikrotik-Rate-Limit attribute
from your RADIUS Server for your DHCPv6 binding. To achieve this you first need to set your DHCPv6 Server to use RADIUS for assigning bindings.
Below is an example of how to set it up:
/radius
add address=10.0.0.1 secret=VERYsecret123 service=dhcp
/ipv6 dhcp-server
set dhcp1 use-radius=yes
After that, you need to tell your RADIUS Server to pass the Mikrotik-Rate-Limit attribute. In case you are using FreeRADIUS with MySQL, then you need to
add appropriate entries into radcheck and radreply tables for a MAC address, that is being used for your DHCPv6 Client. Below is an example for table
entries:
By default allow-dual-stack-queue is enabled and will add a single dynamic queue entry if the MAC address from the IPv4 lease (or DUID, if the
DHCPv4 Client supports Node-specific Client Identifiers from RFC4361), but DUID from DHCPv6 Client is not always based on the
MAC address from the interface on which the DHCPv6 client is running on, DUID is generated on a per-device basis. For this reason, a single
dynamic queue entry might not be created, separate dynamic queue entries might be created instead.
Configuration Example
Notice that prefix-length is 62 bits, which means that clients will receive /62 prefixes from the /60 pool.
install wide-dhcpv6-client
edit "/etc/wide-dhcpv6/dhcp6c.conf" as above
You can use also RouterOS as a DHCP-PD client.
interface eth2{
send ia-pd 0;
};
id-assoc pd {
prefix-interface eth3{
sla-id 1;
sla-len 2;
};
};
mrz@bumba:/media/aaa$ ip -6 addr
..
2: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qlen 1000
inet6 2001:db8:7501:1:200:ff:fe00:0/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::224:1dff:fe17:81f7/64 scope link
valid_lft forever preferred_lft forever
You can make binding to specific client static so that it always receives the same prefix:
DHCP-PD also installs a route to assigned prefix into IPv6 routing table:
DHCP Relay
Summary
Sub-menu: /ip dhcp-relay
The purpose of the DHCP relay is to act as a proxy between DHCP clients and the DHCP server. It is useful in networks where the DHCP server is not on
the same broadcast domain as the DHCP client.
DHCP relay does not choose the particular DHCP server in the DHCP-server list, it just sends the incoming request to all the listed servers.
Properties
Property Description
add-relay-info (yes | Adds DHCP relay agent information if enabled according to RFC 3046. Agent Circuit ID Sub-option contains mac address of an
no; Default: no) interface, Agent Remote ID Sub-option contains MAC address of the client from which request was received.
delay-threshold (time If secs field in DHCP packet is smaller than delay-threshold, then this packet is ignored
| none; Default: none)
dhcp-server (string; List of DHCP servers' IP addresses which should the DHCP requests be forwarded to
Default: )
interface (string; Interface name the DHCP relay will be working on.
Default: )
local-address (IP; The unique IP address of this DHCP relay needed for DHCP server to distinguish relays. If set to 0.0.0.0 - the IP address will
Default: 0.0.0.0) be chosen automatically
relay-info-remote-id (s specified string will be used to construct Option 82 instead of client's MAC address. Option 82 consist of: interface from which
tring; Default: ) packets was received + client mac address or relay-info-remote-id
Configuration Example
Let us consider that you have several IP networks 'behind' other routers, but you want to keep all DHCP servers on a single router. To do this, you need a
DHCP relay on your network which will relay DHCP requests from clients to the DHCP server.
This example will show you how to configure a DHCP server and a DHCP relay that serves 2 IP networks - 192.168.1.0/24 and 192.168.2.0/24 that are
behind a router DHCP-Relay.
IP Address Configuration
IP addresses of DHCP-Server:
IP addresses of DHCP-Relay:
[admin@DHCP-Relay] ip address> print
Flags: X - disabled, I - invalid, D - dynamic
# ADDRESS NETWORK BROADCAST INTERFACE
0 192.168.0.2/24 192.168.0.0 192.168.0.255 To-DHCP-Server
1 192.168.1.1/24 192.168.1.0 192.168.1.255 Local1
2 192.168.2.1/24 192.168.2.0 192.168.2.255 Local2
[admin@DHCP-Relay] ip address>
To setup 2 DHCP Servers on the DHCP-Server router add 2 pools. For networks 192.168.1.0/24 and 192.168.2.0: