Monday, June 30, 2008

Choose a "Good" MAC address

Please choose a “good” MAC address for your testing if you want to change it. In Linux 2.6.24.4, do not use “X1:XX:XX:XX:XX:XX” as your MAC address. It’s not allowed.

The following snippet code is copy from kernel. It explains everything

#include <stdio.h>
#include <stdlib.h>

/* Linux Kernel */
static inline int is_zero_ether_addr(const u_char *addr)
{
   return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
}

static inline int is_multicast_ether_addr(const u_char *addr)
{
   return (0x01 & addr[0]);
}

static inline int is_valid_ether_addr(const u_char *addr)
{
   return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
}

int main (int argc, char **argv) {
   int ret;

   ret = is_valid_ether_addr("11:22:33:44:55:66");
   printf("ret = %d\n", ret);; ret = 0, Not a valid MAC address

   ret = is_valid_ether_addr("01:22:33:44:55:66");
   printf("ret = %d\n", ret);; ret = 0, Not a valid MAC address

   ret = is_valid_ether_addr("00:1B:21:11:22:33");
   printf("ret = %d\n", ret);; ret = 1, This is a valid MAC address

   return;
}

How to prevent to set up the invalud MAC address? Please call "is_valid_ether_addr" in network device driver.

No comments: