Convert MAC address to IPv6 AutoConfig

This script converts any MAC addresses to IPv6 autoconfiguration address (that get assigned if you turn on IPv6 → ND for an interface. I’m thinking of grabbing DHCP host names (from IPv4) along with their MAC addresses and adding any entry in DNS with that host’s IPv6 address.

Any thoughts or suggestions?

# Convert a mac address (48 bit) to a unique interface identifier (64 bit) as per
# RFC 2462 IPv6 Stateless Address Autoconfiguration

# Set autocfgupper64 to the 64-bit "prefix" of the address
# Set macaddrs to an array of MAC address to be converted
# autocfglower64 automatically generates the last 64 bits

:local autocfgupper64 ""
:local autocfglower64 ""
:local macaddrs ""


:global HexDigits {
  "0000"; "0001"; "0010"; "0011";
  "0100"; "0101"; "0110"; "0111";
  "1000"; "1001"; "1010"; "1011";
  "1100"; "1101"; "1110"; "1111"
}

:foreach macaddress in=$macaddrs do={
    :local autocfgaddr ""
# stip off colons from mac address
    :local tmpmac ""
    :for index from=0 to=[:len $macaddress] do={
      :local tmpchar [:pick $macaddress $index ($index + 1)]
      :if ($tmpchar != ":") do={
        :set tmpmac ($tmpmac . $tmpchar)
      }
    }

# convert character 2 to binary and
# swap bit 7 from mac address (character 2)
    :local char2 [:pick $tmpmac 1 2]
    :local char2hex -1
      :if ($char2 = "a"  || $char2 = "A") do={:set char2hex 10}
      :if ($char2 = "b"  || $char2 = "B") do={:set char2hex 11}
      :if ($char2 = "c"  || $char2 = "C") do={:set char2hex 12}
      :if ($char2 = "d"  || $char2 = "D") do={:set char2hex 13}
      :if ($char2 = "e"  || $char2 = "E") do={:set char2hex 14}
      :if ($char2 = "f"  || $char2 = "F") do={:set char2hex 15}
    :if ($char2hex = -1) do={
      :set char2hex [:tonum $char2]
    }

# get binary value for char2
    :local char2bin [:tostr [:pick $HexDigits $char2hex]]
# swap bit 7 (char 3)
    :local bit7 [:pick $char2bin 2 3]

    :if ($bit7 = "1") do={
      :set char2bin ([:pick $char2bin 0 1] .\
                             [:pick $char2bin 1 2] .\
                             "0" .\
                             [:pick $char2bin 3 4])
    }
    :if ($bit7 = "0") do={
      :set char2bin ([:pick $char2bin 0 1] .\
                             [:pick $char2bin 1 2] .\
                             "1" .\
                             [:pick $char2bin 3 4])
    }

# convert new binary number to hex number
    :set char2hex [:tonum [:find $HexDigits $char2bin]]

    :if ($char2hex > 9) do={
      :if ($char2hex = 10) do={:set char2 "A"}
      :if ($char2hex = 11) do={:set char2 "B"}
      :if ($char2hex = 12) do={:set char2 "C"}
      :if ($char2hex = 13) do={:set char2 "D"}
      :if ($char2hex = 14) do={:set char2 "E"}
      :if ($char2hex = 15) do={:set char2 "F"}
    } else={
      :set char2 [:tostr $char2hex]
    }

# compose AutoConfiguration address with new char and
# inserting FFFE between char 6 and 7
    :set autocfglower64 ""
    :for x from=0 to=[:len $tmpmac] do={
      :local insert ""

      :if ($x = 1) do={ :set insert $char2 }
      :if ($x = 6) do={ :set insert ("FF:FE" . [:pick $tmpmac $x ($x + 1)]) }
      :if ($x = 4 || $x = 8) do={
          :set insert (":" . [:pick $tmpmac $x ($x + 1)])
        }

        :if ($insert = "") do={
          :set autocfglower64 ($autocfglower64 . [:pick $tmpmac $x ($x + 1)])
        } else={
          :set autocfglower64 ($autocfglower64 . $insert)
        }
    }

# preprend autocfgupper64
    :set autocfgaddr ($autocfgupper64 . ":" . $autocfglower64)

    :put $autocfgaddr
}

I’m trying to get an IPv6 network and smooth migration.

the suggestion will be to add it to the Wiki =)

ive recently been blazing down the ipv6 trail as well, but cannot figure out what you would use this script for? What exact does it do / or solve / or make better for me?

This is just a base script. I have a bunch of DHCP clients with host names, MAC addresses, and IPv4 addresses that they’re assigned. I’m currently using this script to create DNS entries on the Mikrotik for host name resolution:
http://wiki.mikrotik.com/wiki/Setting_static_DNS_record_for_each_DHCP_lease

With that, I’m working on the same thing, but creating DNS entries using IPv6 address and host names from DHCP. IPv6 auto configures a client without DHCP, but the address is derived from lthe MAC address of the client. So now with the host name and MAC address from DHCP I can generate DNS entries using host names and IPv6 address.

Migration from IPv4 to IPv6 with DNS resolution made easier.