Aikido, programming, system administration, and other things I find interesting

Generating EUID IPv6 address from a MAC

When you set up radvd (router advertisement daemon) on a network, all the computers that support IPv6 will adopt addresses in the advertised address space – except the machine that actually advertises the address prefix (thr router, if you will).

You can assign an address to the interface yourself, any address will do. For instance, you could make it ::1 so that you always know where the router is, or you could compose an amusing string out of letters and numbers (a-f, 0 for o, 1 for i or l, etc). Or you can assign it the UID address based on the interface’s MAC (like the other interfaces get). The algorithm to calculate the automatic address from the MAC is implemented in this Perl script:

#!/usr/bin/perl -w
use Term::ReadLine;
sub mac_euid {
   my $mac = shift;
   my ($b1,$b2,$b3,$b4,$b5,$b6) =
   $mac=~/^([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2})$/;
   $b1 = hex($b1) ^ 2;
   return sprintf "%x%x:%xff:fe%x:%x%x\n",$b1,hex($b2),hex($b3),hex($b4),
   hex($b5),hex($b6);
}
my $term = Term::ReadLine->new('MAC to EUID');
my $prompt = "Enter MAC:";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
   print "Invalid MAC $_\n"
   unless ($_=~/^([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2}):([A-Fa-f0-9]{2})$/i);
   my $res = mac_euid($_);
   print sprintf "EUID = %s\n",$res;
}

If the address is being configured in /etc/network/interfaces (Debian and Ubuntu based computers), you need four lines of configuration (prefix is the prefix for the subnet you are working for, and euid is the EUID part generated from the MAC):

up ip -6 addr add (prefix):(euid)/128 dev ethX
up ip -6 route add (prefix)::0/64 dev ethX
down ip -6 addr del (prefix):(euid)/128 dev ethX
down ip -6 route del (prefix)::0/64 dev ethX

 

Related Posts

Why is my munin slow and how to speed it up

At $work we are monitoring a network of hundreds of servers, and that means that we end up recording hundreds of thousands of variable values every five minutes. After a while, the server started slowing down, taking more than 300 seconds to collect the data. Since it has a whole-system lock, that means the next […]

Read More

A munin plugin to monitor each CPU core separately

Monitoring each core separately may seem like a waste – after all, we have an overall CPU usage already available under “system” in munin, isn’t that enough? It turns out that it isn’t. Sometimes, when using top on a multicore/multicpu machine, you can see a process pegged at 100%, while other processes are comfortably using […]

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *