121 lines
4.4 KiB
Markdown
121 lines
4.4 KiB
Markdown
Create 6rd interface:
|
|
|
|
```routeros
|
|
/interface 6to4 add !keepalive name=6rd-wan-centurylink remote-address=205.171.2.64
|
|
```
|
|
|
|
Add to WAN interface list:
|
|
|
|
```routeros
|
|
/interface list member add interface=6rd-wan-centurylink list=WAN
|
|
```
|
|
|
|
Add route:
|
|
|
|
```routeros
|
|
/ipv6 route add disabled=no dst-address=2000::/3 gateway=6rd-wan-centurylink routing-table=main suppress-hw-offload=no
|
|
```
|
|
|
|
Add script named like `update-6rd`. Edit the first two variables and then run the script to create IPv6 pool.
|
|
|
|
```routeros
|
|
# Configuration
|
|
:local ipv4interface "vlan201-wan"; # Name of the WAN interface to get IPv4 address from
|
|
:local ipv6pool "pool-6rd-centurylink"; # Name of IPv6 pool
|
|
|
|
# Helper function to convert decimal (0-255) to a two-character hex string
|
|
:local decToHex do={
|
|
:local hexChars "0123456789abcdef";
|
|
:local num [:tonum $1];
|
|
:local high ($num / 16);
|
|
:local low ($num - ($high * 16));
|
|
:return ([:pick $hexChars $high ($high + 1)] . [:pick $hexChars $low ($low + 1)]);
|
|
}
|
|
|
|
# 1. Get the current IPv4 address from the interface
|
|
:local ipInfo [/ip address find interface=$ipv4interface];
|
|
:if ([:len $ipInfo] = 0) do={
|
|
:log error "6RD Script: No IPv4 address found on interface $ipv4interface";
|
|
:error "No IPv4 address found";
|
|
}
|
|
:local fullIp [/ip address get [:pick $ipInfo 0] address];
|
|
|
|
# 2. Strip the CIDR subnet mask (e.g., /24) from the IP
|
|
:local slashPos [:find $fullIp "/"];
|
|
:local ipv4 [:pick $fullIp 0 $slashPos];
|
|
|
|
# 3. Parse the IPv4 address into 4 separate octets
|
|
:local dot1 [:find $ipv4 "." 0];
|
|
:local dot2 [:find $ipv4 "." ($dot1 + 1)];
|
|
:local dot3 [:find $ipv4 "." ($dot2 + 1)];
|
|
|
|
:local octet1 [:pick $ipv4 0 $dot1];
|
|
:local octet2 [:pick $ipv4 ($dot1 + 1) $dot2];
|
|
:local octet3 [:pick $ipv4 ($dot2 + 1) $dot3];
|
|
:local octet4 [:pick $ipv4 ($dot3 + 1) [:len $ipv4]];
|
|
|
|
# 4. Convert each octet to Hexadecimal
|
|
:local hex1 [$decToHex $octet1];
|
|
:local hex2 [$decToHex $octet2];
|
|
:local hex3 [$decToHex $octet3];
|
|
:local hex4 [$decToHex $octet4];
|
|
|
|
# 5. Construct the 6RD IPv6 Prefix
|
|
:local newPrefix "2602:$(hex1):$(hex2)$(hex3):$(hex4)00::/56";
|
|
|
|
# 6. Apply to the IPv6 Pool
|
|
:local poolExists [/ipv6 pool find name=$ipv6pool];
|
|
|
|
:if ([:len $poolExists] > 0) do={
|
|
# Pool exists, check if the prefix needs updating to prevent flash writes
|
|
:local currentPrefix [/ipv6 pool get $poolExists prefix];
|
|
:if ($currentPrefix != $newPrefix) do={
|
|
:log info "6RD Script: IP changed. Updating pool from $currentPrefix to $newPrefix";
|
|
|
|
# Find all IPv6 addresses tied to this pool
|
|
:local poolAddrs [/ipv6 address find from-pool=$ipv6pool];
|
|
|
|
# Disable them so the pool can be modified
|
|
:if ([:len $poolAddrs] > 0) do={
|
|
:log info "6RD Script: Disabling associated IPv6 addresses...";
|
|
/ipv6 address disable $poolAddrs;
|
|
# Give RouterOS a second to flush the "in use" status
|
|
:delay 1s;
|
|
}
|
|
|
|
# Update the pool prefix
|
|
/ipv6 pool set $poolExists prefix=$newPrefix prefix-length=64;
|
|
:log info "6RD Script: Updated $ipv6pool prefix successfully.";
|
|
|
|
# Re-enable the addresses (RouterOS will automatically recompute the new IPs from the new prefix)
|
|
:if ([:len $poolAddrs] > 0) do={
|
|
:log info "6RD Script: Re-enabling associated IPv6 addresses...";
|
|
/ipv6 address enable $poolAddrs;
|
|
}
|
|
} else={
|
|
:log debug "6RD Script: IP has not changed. Pool prefix is up to date.";
|
|
}
|
|
} else={
|
|
# Pool doesn't exist, create it
|
|
/ipv6 pool add name=$ipv6pool prefix=$newPrefix prefix-length=64;
|
|
:log info "6RD Script: Created $ipv6pool with prefix $newPrefix";
|
|
}
|
|
```
|
|
|
|
Add addresses from pool to 6rd interface and to each LAN interface, example:
|
|
|
|
```routeros
|
|
/ipv6 address add address=::/64 from-pool=pool-6rd-centurylink eui-64=yes interface=6rd-wan-centurylink
|
|
/ipv6 address add address=::/64 from-pool=pool-6rd-centurylink eui-64=yes interface=vlan142-lan
|
|
/ipv6 address add address=::/64 from-pool=pool-6rd-centurylink eui-64=yes interface=vlan300-lan2
|
|
```
|
|
|
|
In DHCPv4 client add script to run script:
|
|
|
|
```routeros
|
|
:if ($bound = 1) do={
|
|
/system script run update-6rd
|
|
}
|
|
```
|
|
|
|
When DHCP client binds to IPv4 address, script will run and generate new IPv6 /56 prefix and update IPv6 address pool. It disables all addresses using the pool, updates the pool, and re-enables the addresses. Mikrotik automatically chooses /64 subnets from the pool. You can use 16 subnets. |