diff --git a/debian/changelog b/debian/changelog index 820ebe7..3bd56fd 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +usb-moded (0.57) unstable; urgency=low + + * Add random mac generator so host based network tools always get the same mac + + -- Philippe De Swert Thu, 17 Jan 2013 19:50:37 +0200 + usb-moded (0.56) unstable; urgency=low * Fix possible compiltation issue with N900 support and android which are mutually exclusive diff --git a/src/Makefile.am b/src/Makefile.am index 354b922..9e60a6f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,7 +26,9 @@ usb_moded_SOURCES = \ usb_moded-network.c \ usb_moded-network.h \ usb_moded-modesetting.c \ - usb_moded-modesetting.h + usb_moded-modesetting.h \ + usb_moded-mac.c \ + usb_moded-mac.h if !ANDROID usb_moded_SOURCES += \ diff --git a/src/usb_moded-mac.c b/src/usb_moded-mac.c new file mode 100644 index 0000000..05dd9a7 --- /dev/null +++ b/src/usb_moded-mac.c @@ -0,0 +1,56 @@ +/** + @file usb_moded-mac.c + + Copyright (C) 2013 Jolla. All rights reserved. + + @author: Philippe De Swert + + This program is free software; you can redistribute it and/or + modify it under the terms of the Lesser GNU General Public License + version 2 as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the Lesser GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#include +#include "usb_moded-mac.h" + +static void random_ether_addr(unsigned char *addr) +{ + FILE *random; + + random = fopen("/dev/urandom", "r"); + fread(addr, 1, 6, random); + fclose(random); + + addr [0] &= 0xfe; /* clear multicast bit */ + addr [0] |= 0x02; /* set local assignment bit (IEEE802) */ +} + +void generate_random_mac (void) +{ + unsigned char addr[6]; + int i; + FILE *g_ether; + + printf("Getting random usb ethernet mac\n"); + random_ether_addr(addr); + + g_ether = fopen("/etc/modprobe.d/g_ether.conf", "w"); + fprintf(g_ether, "options g_ether host_addr="); + + for(i=0; i<5; i++) + { + fprintf(g_ether, "%02x:",addr[i]); + } + fprintf(g_ether, "%02x",addr[i]); + fclose(g_ether); +} diff --git a/src/usb_moded-mac.h b/src/usb_moded-mac.h new file mode 100644 index 0000000..741fd55 --- /dev/null +++ b/src/usb_moded-mac.h @@ -0,0 +1,23 @@ +/** + @file usb_moded-mac.h + + Copyright (C) 2013 Jolla. All rights reserved. + + @author: Philippe De Swert + + This program is free software; you can redistribute it and/or + modify it under the terms of the Lesser GNU General Public License + version 2 as published by the Free Software Foundation. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the Lesser GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +void generate_random_mac (void); diff --git a/src/usb_moded.c b/src/usb_moded.c index f68d278..59a6cfe 100644 --- a/src/usb_moded.c +++ b/src/usb_moded.c @@ -49,6 +49,7 @@ #include "usb_moded-config.h" #include "usb_moded-config-private.h" #include "usb_moded-network.h" +#include "usb_moded-mac.h" /* global definitions */ @@ -180,7 +181,7 @@ void set_usb_connected_state(void) #ifdef MEEGOLOCK int act_dead = 0; /* check if we are in acting dead or not, /tmp/USER will not exist in acting dead */ - act_dead = access("/tmp/USER", R_OK); + act_dead = accesa("/tmp/USER", R_OK); if(mode_to_set && !export && !act_dead) #else if(mode_to_set) @@ -455,6 +456,10 @@ static void usb_moded_init(void) ctx = kmod_new(NULL, NULL); kmod_load_resources(ctx); + if(!access("/etc/modprobe.d/g_ether.conf", F_OK)) + { + generate_random_mac(); + } /* TODO: add more start-up clean-up and init here if needed */ }