Difference between revisions of "Manual:IP/DHCP Client"

From MikroTik Wiki
Jump to: navigation, search
(IPv6)
(Properties)
 
(26 intermediate revisions by 6 users not shown)
Line 11: Line 11:
 
* option 33 - STATIC_ROUTE,
 
* option 33 - STATIC_ROUTE,
 
* option 42 - NTP_LIST,
 
* option 42 - NTP_LIST,
* option 122 - CLASSLESS_ROUTE,
+
* option 121 - CLASSLESS_ROUTE,
  
  
 +
 +
===Option===
 +
 +
DHCP client has a possibility to set up options that are sent to DHCP server. For example, hostname and MAC address. Syntax is same as for [[Manual:IP/DHCP_Server#DHCP_Options|DHCP server options]].
 +
 +
{{Note| This feature is available since RouterOS 6.0}}
 +
 +
 +
Currently, there are three variables that can be used in 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
 +
 +
DHCP client default options include these default Options:
 +
 +
{|
 +
!|Name
 +
!|code
 +
!|value
 +
|-
 +
||clientid_duid
 +
||61
 +
||0xff$(CLIENT_DUID)
 +
|-
 +
||clientid
 +
||61
 +
||0x01$(CLIENT_MAC)
 +
|-
 +
||hostname
 +
||12
 +
||$(HOSTNAME)
 +
|}
  
 
===IPv6===
 
===IPv6===
Line 29: Line 61:
 
</pre>
 
</pre>
  
After interface is added, you can use rint" or "print detail" command to see what parameters DHCP client acquired:  
+
After interface is added, you can use "print" or "print detail" command to see what parameters DHCP client acquired:  
 
<pre>
 
<pre>
 
[admin@MikroTik] ip dhcp-client> print detail
 
[admin@MikroTik] ip dhcp-client> print detail
Line 53: Line 85:
 
{{Mr-arg-table
 
{{Mr-arg-table
 
|arg=add-default-route
 
|arg=add-default-route
|type=yes {{!}} no
+
|type=yes {{!}} no {{!}} special-classless
 
|default=yes
 
|default=yes
|desc=Whether to install default route in routing table received from dhcp server.
+
|desc=Whether to install default route in routing table received from dhcp server. By default RouterOS client complies to RFC and ignores option 3 if classless option 121 is received. To force client not to ignore option 3 set ''special-classless''. 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 route if received and default route  (MS style)
 +
 
 
}}
 
}}
  
Line 66: Line 102:
  
 
{{Mr-arg-table
 
{{Mr-arg-table
|arg=default-route-distance
+
|arg=comment
|type=integer:0..255
+
|type=string
 
|default=
 
|default=
|desc=Distance of default route. Applicable if <code>add-default-route</code> is set to <code>yes</code>.
+
|desc=Short description of the client
 
}}
 
}}
  
 
{{Mr-arg-table
 
{{Mr-arg-table
|arg=host-name
+
|arg=default-route-distance
|type=string
+
|type=integer:0..255
 
|default=
 
|default=
|desc=Host name of the client sent to a DHCP server. If not specified, client's system identity will be used.
+
|desc=Distance of default route. Applicable if <code>add-default-route</code> is set to <code>yes</code>.
 
}}
 
}}
  
 
{{Mr-arg-table
 
{{Mr-arg-table
|arg=ipv4
+
|arg=disabled
 
|type=yes {{!}} no
 
|type=yes {{!}} no
 
|default=yes
 
|default=yes
|desc=Client will receive IPv4 address from the server if this parameter is set.
+
|desc=
 
}}
 
}}
  
{{Mr-arg-table
 
|arg=ipv6-pd
 
|type=yes {{!}} no
 
|default=no
 
|desc=Client will receive delegated prefix from [[M:IP/DHCP_Server#IPv6 | DHCPv6-PD server]].
 
}}
 
  
 
{{Mr-arg-table
 
{{Mr-arg-table
|arg=pool-name
+
|arg=host-name
 
|type=string
 
|type=string
 
|default=
 
|default=
|desc=Name of the [[M:IPv6/Pool | IPv6 pool]] in which received IPv6 prefix will be added. Applicable if '''ipv6-pd''' is enabled.
+
|desc=Host name of the client sent to a DHCP server. If not specified, client's system identity will be used.
 
}}
 
}}
  
 
{{Mr-arg-table
 
{{Mr-arg-table
|arg=pool-prefix-length
+
|arg=interface
 
|type=string
 
|type=string
 
|default=
 
|default=
|desc=Prefix length parameter that will be set for [[M:IPv6/Pool | IPv6 pool]] in which received IPv6 prefix is added. Applicable if '''ipv6-pd''' is enabled. Prefix length must be greater than the length of received prefix, otherwise prefix-length will be set to received prefix length + 8 bits.
+
|desc=Interface on which DHCP client will be running.
 
}}
 
}}
  
 
{{Mr-arg-table
 
{{Mr-arg-table
|arg=interface
+
|arg=script
|type=string
+
|type=script
 
|default=
 
|default=
|desc=Interface on which DHCP client will be running.
+
|desc=Execute script on status change. This parameter is available in v6.39rc33+ These are available variables that are accessible for the event script:
 +
* <var>bound</var> - 1 - lease is added/changed; 0 - lease is removed
 +
* <var>server-address</var> - server address
 +
* <var>lease-address</var> - lease address provided by server
 +
* <var>interface</var> - name of interface on which client is configured
 +
* <var>gateway-address</var> - gateway address provided by server
 +
* <var>vendor-specific</var> - stores value of option 43 received from DHCP server
 +
* <var>lease-options</var> - array of received options
 +
 
 +
[[M:IP/DHCP_Client#Lease_script_example | <code>Example >></code>]]
 
}}
 
}}
  
Line 154: Line 193:
 
|type=time
 
|type=time
 
|desc=Time when the lease expires (specified by the DHCP server).
 
|desc=Time when the lease expires (specified by the DHCP server).
}}
 
 
{{Mr-arg-ro-table
 
|arg=expires-after-v6
 
|type=time
 
|desc=Time when the IPv6 prefix expires (specified by the DHCP server).
 
 
}}
 
}}
  
Line 179: Line 212:
 
|type=IP
 
|type=IP
 
|desc=
 
|desc=
}}
 
 
{{Mr-arg-ro-table
 
|arg=prefix
 
|type=IPv6 prefix
 
|desc=Shows received IPv6 prefix from DHCPv6-PD server
 
 
}}
 
}}
  
Line 190: Line 217:
 
|arg=primary-dns
 
|arg=primary-dns
 
|type=IP
 
|type=IP
|desc=IP address of the primary DNS server, assigned by the DHCP server
+
|desc=IP address of the first DNS resolver, that was assigned by the DHCP server
 
}}
 
}}
  
Line 202: Line 229:
 
|arg=secondary-dns
 
|arg=secondary-dns
 
|type=IP
 
|type=IP
|desc=IP address of the secondary DNS server, assigned by the DHCP server
+
|desc=IP address of the second DNS resolver, assigned by the DHCP server
 
}}
 
}}
  
Line 211: Line 238:
 
}}
 
}}
  
{{Mr-arg-ro-table
+
{{Mr-arg-ro-table-end
 
|arg=status
 
|arg=status
 
|type=bound {{!}} error {{!}} rebinding... {{!}} requesting... {{!}} searching... {{!}} stopped
 
|type=bound {{!}} error {{!}} rebinding... {{!}} requesting... {{!}} searching... {{!}} stopped
 
|desc=Shows the status of DHCP Client
 
|desc=Shows the status of DHCP Client
}}
 
 
{{Mr-arg-ro-table-end
 
|arg=v6-status
 
|type=stopped {{!}} searching {{!}} requesting... {{!}} bound {{!}} renewing {{!}} rebinding {{!}} error {{!}} stopping
 
|desc=Shows the status of DHCPv6 Client:
 
* '''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 ocurred.
 
* '''stopping''' - sent "release"
 
 
}}
 
}}
  
Line 251: Line 264:
  
  
==Setup Examples==
 
  
===IPv6-PD setup example===
+
==Examples==
 +
===Lease script example===
  
This simple example demonstrates how to enable dhcp client to receive IPv6 prefix and add it to the pool.
+
Since RouterOS v6.39rc33, it is possible to execute a script when DHCP client obtains new lease or loses existing. This is an example script that automatically adds a default route with routing-mark=WAN1 and removes it when the lease expires or is removed.
 
 
For DHCPv6 client to work we just need to specify four parameters:
 
 
<pre>
 
<pre>
/ip dhcp-client add ipv6-pd=yes pool-name=ipv6 pool-prefix-length=64 interface=ether13
+
/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"
 
</pre>
 
</pre>
  
  
Detailed print should show status of the client and we can verify if prefix is received
+
===Resolve default gateway when 'router' (option3) is from different subnet===
 +
 
 +
In some cases administrators tend to set  'router' option which cannot be resolved with offered IP's subnet.
 +
For example, DHCP server offers 192.168.88.100/24 to the client and option 3 is set to 172.16.1.1. This will result in unresolved default route:
 
<pre>
 
<pre>
[admin@MikroTik] /ip dhcp-client> print detail
+
#      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
Flags: X - disabled, I - invalid
+
  0 DS  0.0.0.0/0                          172.16.1.1             1
  0   interface=ether13 ipv4=yes add-default-route=yes default-route-distance=1
+
1 ADC  192.168.88.0/24    192.168.88.100  ether1
    use-peer-dns=yes use-peer-ntp=yes ipv6-pd=yes pool-name="ipv6" pool-prefix-length=64
+
</pre>
    status=bound address=10.5.101.9/24 gateway=10.5.101.1 dhcp-server=10.5.101.1
 
    primary-dns=10.5.101.1 expires-after=2d23h45m27s v6-status=bound
 
    prefix=2001:db8:7501:ff14::/62 expires-after-v6=2d23h45m34s
 
  
 +
To fix this we need to add /32 route to resolve the gateway over ether1, which can be done by running script below each time DHCP client gets an address
 +
<pre>
 +
/system script add name="dhcpL" source={ /ip address add address=($"lease-address" . "/32") network=$"gateway-address" interface=$interface }
 
</pre>
 
</pre>
  
Notice that server gave us prefix 2001:db8:7501:ff14::/62. And it should be also added to ipv6 pools
+
Now we can further extend the script, to check if address already exist, and remove the old one if changes are needed
 
 
 
<pre>
 
<pre>
[admin@MikroTik] /ipv6 pool> print
+
/system script add name="dhcpL" source={
Flags: D - dynamic
+
   /ip address {
#   NAME                        PREFIX                                      PREFIX-LENGTH
+
    :local ipId [find where comment="dhcpL address"]
0 D ipv6                        2a02:610:7501:ff14::/62                                64
+
    :if ($ipId != "") do={
</pre>
+
      :if (!([get $ipId address] = ($"lease-address" . "/32") && [get $ipId network]=$"gateway-address" )) do={
 
+
        remove $ipId;
It works!
+
        add address=($"lease-address" . "/32") network=$"gateway-address" \
Now you can use this pool, for example, for pppoe clients.
+
          interface=$interface comment="dhcpL address"
 
+
      }
 +
    } else={
 +
      add address=($"lease-address" . "/32") network=$"gateway-address" \
 +
        interface=$interface comment="dhcpL address"
 +
    }
 +
  }
 +
}
 +
</pre>
  
 
{{cont}}
 
{{cont}}

Latest revision as of 14:11, 21 January 2020

Version.png

Applies to RouterOS: v3, v4 +

Summary

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.

RouterOS DHCP cilent asks for following options:

  • 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,


Option

DHCP client has a possibility to set up options that are sent to DHCP server. For example, hostname and MAC address. Syntax is same as for DHCP server options.

Icon-note.png

Note: This feature is available since RouterOS 6.0



Currently, there are three variables that can be used in 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

DHCP client default options include these default Options:

Name code value
clientid_duid 61 0xff$(CLIENT_DUID)
clientid 61 0x01$(CLIENT_MAC)
hostname 12 $(HOSTNAME)

IPv6

Starting from v5.8 DHCP Client can receive delegated prefixes from DHCPv6 server. Currently received prefix is added to IPv6 pool, which later can be used for example in pppoe server configuration. Starting from v5.9, DHCPv6 client configuration was moved to /ipv6 sub-menu. Read-more >>

Quick setup example

Add a DHCP client on ether1 interface:

/ip dhcp-client add interface=ether1 disabled=no

After interface is added, you can use "print" or "print detail" command to see what parameters DHCP client acquired:

[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>
Icon-note.png

Note: If interface used by DHCP client is part of VRF configuration, then default route and other received routes from DHCP server will be added to VRF routing table.


Properties

Sub-menu: /ip dhcp-client


Property Description
add-default-route (yes | no | special-classless; Default: yes) Whether to install default route in routing table received from dhcp server. By default RouterOS client complies to RFC and ignores option 3 if classless option 121 is received. To force client not to ignore option 3 set special-classless. 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 route if received and default route (MS style)
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
comment (string; Default: ) Short description of the client
default-route-distance (integer:0..255; Default: ) Distance of default route. Applicable if add-default-route is set to yes.
disabled (yes | no; Default: yes)
host-name (string; Default: ) Host name of the client sent to a DHCP server. If not specified, client's system identity will be used.
interface (string; Default: ) Interface on which DHCP client will be running.
script (script; Default: ) Execute script on status change. This parameter is available in v6.39rc33+ These are available variables that are accessible for the event script:
  • bound - 1 - lease is added/changed; 0 - lease is removed
  • server-address - server address
  • lease-address - lease address provided by server
  • interface - name of interface on which client is configured
  • gateway-address - gateway address provided by server
  • vendor-specific - stores value of option 43 received from DHCP server
  • lease-options - array of received options
Example >>
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/Netmask) 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 first DNS resolver, that was 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 second DNS resolver, 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 (numbers) Release current binding and restart DHCP client
renew (numbers) 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)


Examples

Lease script example

Since RouterOS v6.39rc33, it is possible to execute a script when DHCP client obtains new lease or loses existing. This is an example script that automatically adds a default route with routing-mark=WAN1 and removes it when the lease expires or is removed.

/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"


Resolve default gateway when 'router' (option3) is from different subnet

In some cases administrators tend to set 'router' option which cannot be resolved with offered IP's subnet. For example, DHCP server offers 192.168.88.100/24 to the client and option 3 is set to 172.16.1.1. This will result in unresolved default route:

 #      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
 0  DS  0.0.0.0/0                          172.16.1.1              1
 1 ADC  192.168.88.0/24    192.168.88.100  ether1 

To fix this we need to add /32 route to resolve the gateway over ether1, which can be done by running script below each time 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 address already exist, and remove the old one if changes are needed

/system script add name="dhcpL" source={ 
  /ip address {
    :local ipId [find where comment="dhcpL address"]
    :if ($ipId != "") do={
      :if (!([get $ipId address] = ($"lease-address" . "/32") && [get $ipId network]=$"gateway-address" )) do={
        remove $ipId;
        add address=($"lease-address" . "/32") network=$"gateway-address" \
          interface=$interface comment="dhcpL address"
      }
    } else={
      add address=($"lease-address" . "/32") network=$"gateway-address" \
        interface=$interface comment="dhcpL address"
    }
  }
}

[ Top | Back to Content ]