LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   set eth0 mac address in smc91xx eeprom via C code (http://www.linuxquestions.org/questions/programming-9/set-eth0-mac-address-in-smc91xx-eeprom-via-c-code-802812/)

shockproof 04-19-2010 05:49 AM

set eth0 mac address in smc91xx eeprom via C code
 
Hi guys.
I'm looking for any snippet to manage MAC address via C code in Linux.
I tried to write some code to change eth0 mac address writing it in smc911xx eeprom.

What I did is the following..


#... some includes...
...
int skfd;
struct ifreq ifr;

struct ethtool_eeprom eeprom;

void main()
{
unsigned char buff[100];
unsigned char *data_buff;
eeprom.len = 7;
eeprom.offset = 0;

if((skfd = socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("Error opening socket!!!");
exit(1);
}
strncpy(ifr.ifr_name,"eth0",IFNAMSIZ);

if(ioctl(skfd,SIOCGIFFLAGS,&ifr)<0)
{
perror("Error get ioctl!!!");
exit(1);
}
ifr.ifr_flags &= ~IFF_UP;
if(ioctl(skfd,SIOCSIFFLAGS,&ifr)<0)
{
perror("Error set ioctl!!!");
exit(1);
}

eeprom.cmd = ETHTOOL_SEEPROM;
memcpy(buff,&eeprom,sizeof(eeprom));
data_buff = buff + sizeof(eeprom);
data_buff[0] = 0xA5;
/*New mac address */
data_buff[1] = 0x00;
data_buff[2] = 0x00;
data_buff[3] = 0x22;
data_buff[4] = 0x33;
data_buff[5] = 0x44;
data_buff[6] = 0x66;
ifr.ifr_data = buff;

ioctl(skfd,SIOCETHTOOL,&ifr);

if(ioctl(skfd,SIOCGIFFLAGS,&ifr)<0)
{
perror("Error get ioctl!!!");
exit(1);
}
ifr.ifr_flags |= (IFF_UP|IFF_RUNNING);

if(ioctl(skfd,SIOCSIFFLAGS,&ifr)<0)
{
perror("Error set ioctl!!!");
exit(1);
}


}


Please help me, if you can!
Best regards.

DragonSlayer48DX 05-06-2010 05:45 PM

Hi, and welcome to LQ!

Thank you for marking your thread as "solved" when you found a solution to your problem. Would you mind posting the solution to help others in the future? Thanks.

Cheers


All times are GMT -5. The time now is 08:49 AM.