Community discussions

MikroTik App
 
rmauer
just joined
Topic Author
Posts: 4
Joined: Thu Nov 14, 2019 9:32 pm

/32 DHCP Leases

Mon Mar 09, 2020 4:57 pm

I wrote a script that "basically" allows the /32 DHCP leases to work on routeros.
So this stems from using PPPoE and loving the /32 assignment of public IP addresses and really wanting to have that assignment in a dhcp/no encapsulation/no protocol overhead/no client side config form.
It is possible to setup /32 networking without this script. This script automates the adding and removing of the ip address needed on routeros to correctly route to and from said host.
When I started looking up how to do this I was unable to find information on exactly how so now that I have figured out a way to make this work I wanted to share it, and get feedback to hopefully make this better. Because I am 100% sure it can be done better.
There are a few things to setup on the router:
  • DHCP server
    • address-pool static only (I haven't tested using a pool don't, see why it wouldn't work though)
      source-address - used in the script to set the local address for the IP address entry. (there might be a better way, I haven't found it yet)
    Create DHCP option 121, this gives the host a route. Needed for networking to work.
I use a blanket config for all my sites, the config covers my entire IP range. I then use radius to assign the IP.
This allows for standardized configurations as you will see below.
But the principals apply the same if you wanted to make a network entry for a single IP. Just use /32 instead of /24
  • Create a Network entry in the dhcp server for the Network This needs to be rather specific for for the 69.69.69.69/24 range:
    • Network: 69.69.69.69/24 (intended address range for this statement, typically our public ip block)
      Gateway: 10.10.10.1 (DHCP server Src address)
      NetMask: 32 (this is critical to not breaking the internet for the client)
      DNS servers: (dhcp src or whatever you want to use)
      DHCP Options: Default-Route (this is option 121 that we setup)
Here is the script that adds a IP address entry based on the server and lease info. I went through several versions and landed on this one because of its "portability"
Meaning, in my network at least, I could slap this on any router and start handing our /32 leases.
Its important to remember that routeros can, without this script, assign /32 leases. But in order for routing to work a IP address needs to be added to the router. That is what the script does.
####Adds a IP address entry based on dhcp server src-address
/ip dhcp-server
:local interface;
:do { :set interface "$[ get $leaseServerName interface ]" }  on-error={}
## uncomment to log interface selection
#:log info ("DHCP Server interface : $interface ");
:local gateway;
:do { :set gateway "$[ get  $leaseServerName src-address ]" } on-error={}
## uncomment to log gateway selection
#:log info ("DHCP Server Gateway : $gateway ");

/ip dhcp-server network
:local cidr;
:do { :set cidr [:put "$leaseActIP/32"]}


/ip dhcp-server lease
:local hostname;
:do { :set hostname "$[ get [ find where server=$leaseServerName && address=$leaseActIP && mac-address=$leaseActMAC ] host-name ]" }  on-error={}

## set comment and log info to display
:local forLease "$hostname / $leaseActIP / $leaseActMAC / $interface"

/ip address;
:log info ("Add IP address at DHCP Release : $forLease ");
:if ($leaseBound = "1") do={
add address=$gateway network=$leaseActIP interface=$interface comment=$forLease;
} else={
:log info ("Remove IP address at DHCP Release : $forLease ");
:foreach n in [find] do={
:if ([get $n network] = $leaseActIP) do={
remove $n;
}
}
}
####
I attached a very basic config for a ccr-1009 that implements everything above.
32-dhcp-test.rsc
I didn't include instructions for radius tie in, but its pretty straight forward I think and setup can vary based on the server.
Another important note: I have seen issues using long lease times, the script doesn't always add the IP address back in upon renewal if the lease is set too long.
I tested times of 10s, 10m, 10h, 1d, 2d, 3d. At 3 day lease times is were I saw the problem above. When that happens it takes the client offline. So far shortening the lease time fixes it.
So does static entries, but who wants to maintain all of those. Why don't we go back to static routing........
You do not have the required permissions to view the files attached to this post.
 
RodrigoSteinhorst
just joined
Posts: 3
Joined: Wed Dec 29, 2021 11:03 pm

Re: /32 DHCP Leases

Thu Dec 30, 2021 2:55 am

Which version was this script tested?
I couldn't get it to work on 6.48
 
SpiritVII
just joined
Posts: 6
Joined: Thu Jan 21, 2021 9:30 am

Re: /32 DHCP Leases

Thu Feb 03, 2022 6:30 pm

Which version was this script tested?
I couldn't get it to work on 6.48
This is indicated at the begening into the header :
# mar/09/2020 09:32:45 by RouterOS 6.46.4

Who is online

Users browsing this forum: diamuxin and 17 guests