Compare commits

...

10 commits

Author SHA1 Message Date
Christian Hesse
ea4b5553c2 Merge branch 'netmask6' into next
This branch is a follow-up on 9ceed0926a
with clean solution. Read on for details...

The data type `ip6-prefix` used to hold what it was named for - an IPv6
prefix:

    [user@mikrotik] > :put 2001:db8::dead:beef/32
    2001:db8::/32

This changed with RouterOS 6.21beta2, which now allows that exact same
data type to hold something like "address with prefix length attached":

    [user@mikrotik] > :put 2001:db8::dead:beef/32
    2001:db8::dead:beef/32

My scripts (namely `fw-addr-lists`) relied on the old behaviour and broke.
The commit mentioned above was just a quick workaround, with rough edges,
and it could still fail.

Sadly RouterOS does not support bit shifting on IPv6 data types, so a
(completely) mathematical solution is out of scope.

This branch implements a new and better workaround, see the first commit
of branch (6ad6f9aa08) for details.

I opened a support ticket / feature request on this topic, let's see
what results it brings...

https://help.mikrotik.com/servicedesk/servicedesk/customer/portal/1/SUP-201881
2025-10-22 19:05:38 +02:00
Christian Hesse
b80b872e55 mod/ipcalc: support IPv6
Well, some of these values do not make a lot of sense for IPv6...
Something to be cleaned up later.
2025-10-19 19:55:12 +02:00
Christian Hesse
ea05b69f7c fw-addr-lists: use $NetMask6 2025-10-19 19:55:12 +02:00
Christian Hesse
d7a6eb1d00 global-functions: $NetMask6: implement simple caching
The calculation is quite complex for something that needs to be done
frequently, for example by `fw-addr-lists`. The number of possible
netmasks is limited, so let's cache the results that were calculated
already.
2025-10-19 19:52:42 +02:00
Christian Hesse
6ad6f9aa08 global-functions: introduce $NetMask6
RouterOS does not support bit shifting on IPv6 data types, so we have to
split the problem:

 * each 16 bit block is calculated separately, as number
 * the complete netmask is assembled in a loop, as string
 * the final string is casted to correct data type
2025-10-19 19:48:14 +02:00
Christian Hesse
c62f236251 Merge branch 'netmask4' into next 2025-10-19 19:26:51 +02:00
Christian Hesse
47309e5c03 fw-addr-lists: normalize IPv4 addresses 2025-10-16 15:43:43 +02:00
Christian Hesse
9fa11cb79a mod/ipcalc: use $NetMask4 2025-10-16 13:03:54 +02:00
Christian Hesse
def540c965 global-functions: introduce $NetMask4 2025-10-16 10:47:19 +02:00
Christian Hesse
025b492783 global-functions: remove trailing space 2025-10-16 10:34:09 +02:00
3 changed files with 74 additions and 14 deletions

View file

@ -22,10 +22,12 @@
:global EitherOr; :global EitherOr;
:global FetchHuge; :global FetchHuge;
:global HumanReadableNum; :global HumanReadableNum;
:global IfThenElse;
:global LogPrint; :global LogPrint;
:global LogPrintOnce; :global LogPrintOnce;
:global LogPrintVerbose; :global LogPrintVerbose;
:global MIN; :global NetMask4;
:global NetMask6;
:global ScriptLock; :global ScriptLock;
:global WaitFullyConnected; :global WaitFullyConnected;
@ -114,8 +116,13 @@
:do { :do {
:local Branch; :local Branch;
:if ($Address ~ "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}(/[0-9]{1,2})?\$") do={ :if ($Address ~ "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}(/[0-9]{1,2})?\$") do={
:if ($Address ~ "/32\$") do={ :local Net $Address;
:set Address [ :pick $Address 0 ([ :len $Address ] - 3) ]; :local CIDR 32;
:local Slash [ :find $Address "/" ];
:if ([ :typeof $Slash ] = "num") do={
:set Net [ :toip [ :pick $Address 0 $Slash ] ]
:set CIDR [ :pick $Address ($Slash + 1) [ :len $Address ] ];
:set Address [ :tostr (([ :toip $Net ] & [ $NetMask4 $CIDR ]) . [ $IfThenElse ($CIDR < 32) ("/" . $CIDR) ]) ];
} }
:set Branch [ $GetBranch $Address ]; :set Branch [ $GetBranch $Address ];
:set ($IPv4Addresses->$Branch->$Address) $TimeOut; :set ($IPv4Addresses->$Branch->$Address) $TimeOut;
@ -123,13 +130,13 @@
} }
:if ($Address ~ "^[0-9a-zA-Z]*:[0-9a-zA-Z:\\.]+(/[0-9]{1,3})?\$") do={ :if ($Address ~ "^[0-9a-zA-Z]*:[0-9a-zA-Z:\\.]+(/[0-9]{1,3})?\$") do={
:local Net $Address; :local Net $Address;
:local Cidr 64; :local CIDR 128;
:local Slash [ :find $Address "/" ]; :local Slash [ :find $Address "/" ];
:if ([ :typeof $Slash ] = "num") do={ :if ([ :typeof $Slash ] = "num") do={
:set Net [ :toip6 [ :pick $Address 0 $Slash ] ] :set Net [ :toip6 [ :pick $Address 0 $Slash ] ]
:set Cidr [ $MIN [ :pick $Address ($Slash + 1) [ :len $Address ] ] 64 ]; :set CIDR [ :pick $Address ($Slash + 1) [ :len $Address ] ];
} }
:set Address (([ :toip6 $Net ] & ffff:ffff:ffff:ffff::) . "/" . $Cidr); :set Address (([ :toip6 $Net ] & [ $NetMask6 $CIDR ]) . "/" . $CIDR);
:set Branch [ $GetBranch $Address ]; :set Branch [ $GetBranch $Address ];
:set ($IPv6Addresses->$Branch->$Address) $TimeOut; :set ($IPv6Addresses->$Branch->$Address) $TimeOut;
:error true; :error true;

View file

@ -61,6 +61,8 @@
:global MAX; :global MAX;
:global MIN; :global MIN;
:global MkDir; :global MkDir;
:global NetMask4;
:global NetMask6;
:global NotificationFunctions; :global NotificationFunctions;
:global ParseDate; :global ParseDate;
:global ParseKeyValueStore; :global ParseKeyValueStore;
@ -990,6 +992,43 @@
:return true; :return true;
} }
# return an IPv4 netmask for CIDR
:set NetMask4 do={
:local CIDR [ :tonum $1 ];
:return ((255.255.255.255 << (32 - $CIDR)) & 255.255.255.255);
}
# return an IPv6 netmask for CIDR
:set NetMask6 do={
:local FuncName $0;
:local CIDR [ :tostr $1 ];
:global IfThenElse;
:global MAX;
:global MIN;
:global NetMask6Cache;
:if ([ :typeof ($NetMask6Cache->$CIDR) ] = "ip6") do={
:return ($NetMask6Cache->$CIDR);
}
:if ([ :typeof $NetMask6Cache ] = "nothing") do={
:set NetMask6Cache ({});
}
:local Mask "";
:for I from=0 to=7 do={
:set Mask ($Mask . \
[ :convert from=num to=hex (0xffff - (0xffff >> [ :tonum [ $MIN [ $MAX ($CIDR - (16 * $I)) 0 ] 16 ] ])) ] . \
[ $IfThenElse ($I < 7) ":" ]);
}
:set Mask [ :toip6 $Mask ];
:set ($NetMask6Cache->$CIDR) $Mask;
:return $Mask;
}
# prepare NotificationFunctions array # prepare NotificationFunctions array
:if ([ :typeof $NotificationFunctions ] != "array") do={ :if ([ :typeof $NotificationFunctions ] != "array") do={
:set NotificationFunctions ({}); :set NotificationFunctions ({});

View file

@ -34,20 +34,34 @@
# calculate and return netmask, network, min host, max host and broadcast # calculate and return netmask, network, min host, max host and broadcast
:set IPCalcReturn do={ :set IPCalcReturn do={
:local Input [ :tostr $1 ]; :local Input [ :tostr $1 ];
:local Address [ :toip [ :pick $Input 0 [ :find $Input "/" ] ] ];
:local Bits [ :tonum [ :pick $Input ([ :find $Input "/" ] + 1) [ :len $Input ] ] ];
:local Mask ((255.255.255.255 << (32 - $Bits)) & 255.255.255.255);
:local Return { :global NetMask4;
:global NetMask6;
:local Address [ :pick $Input 0 [ :find $Input "/" ] ];
:local Bits [ :tonum [ :pick $Input ([ :find $Input "/" ] + 1) [ :len $Input ] ] ];
:local Mask;
:local One;
:if ([ :typeof [ :toip $Address ] ] = "ip") do={
:set Address [ :toip $Address ];
:set Mask [ $NetMask4 $Bits ];
:set One 0.0.0.1;
} else={
:set Address [ :toip6 $Address ];
:set Mask [ $NetMask6 $Bits ];
:set One ::1;
}
:local Return ({
"address"=$Address; "address"=$Address;
"netmask"=$Mask; "netmask"=$Mask;
"networkaddress"=($Address & $Mask); "networkaddress"=($Address & $Mask);
"networkbits"=$Bits; "networkbits"=$Bits;
"network"=(($Address & $Mask) . "/" . $Bits); "network"=(($Address & $Mask) . "/" . $Bits);
"hostmin"=(($Address & $Mask) | 0.0.0.1); "hostmin"=(($Address & $Mask) | $One);
"hostmax"=(($Address | ~$Mask) ^ 0.0.0.1); "hostmax"=(($Address | ~$Mask) ^ $One);
"broadcast"=($Address | ~$Mask); "broadcast"=($Address | ~$Mask);
} });
:return $Return; :return $Return;
} }