Skip to contentSkip to conversation

net/net: Change the default mac address of nic

PATCH Superseded Mike Qiu 1 participant 2 messages Oct 14, 2013 Networking

lore
Share & tools
lore

Mike Qiu

author
#0 lore raw

PATCH v2 1/1 net/net: Change the default mac address of nic

The default mac address is 52:54:00:12:34:56 + index, this will cause problem that when we boot up more than one guest with all mac addresses unset by default, assume that each guest has one nic. In this situation, all the guest's nic has the same mac address.

This patch is to solve this bug.

---
 net/net.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/net.c +6 -3
index c330c9a..2796d04 100644
@@ -21,6 +21,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+#include <time.h>
+
 #include "config-host.h"
 
 #include "net/net.h"
@@ -147,12 +149,13 @@ void qemu_macaddr_default_if_unset(MACAddr *macaddr)
 
     if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
         return;
+    srand((unsigned)time(NULL));
     macaddr->a[0] = 0x52;
     macaddr->a[1] = 0x54;
     macaddr->a[2] = 0x00;
-    macaddr->a[3] = 0x12;
-    macaddr->a[4] = 0x34;
-    macaddr->a[5] = 0x56 + index++;
+    macaddr->a[3] = rand() % 256;
+    macaddr->a[4] = rand() % 256;
+    macaddr->a[5] = rand() % 256 + index++;
 }
 
 /**
Signature · 1 line
1.8.2.1
author
#1 lore raw

On 10/14/2013 03:30 PM, Mike Qiu wrote:

Sorry for the first version make a mistake of the sender.

There is no difference with v1.

Thanks
Mike

Quoted 42 lines

The default mac address is 52:54:00:12:34:56 + index, this will cause problem that when we boot up more than one guest with all mac addresses unset by default, assume that each guest has one nic. In this situation, all the guest's nic has the same mac address.

This patch is to solve this bug.

---
  net/net.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
net/net.c +6 -3
index c330c9a..2796d04 100644
@@ -21,6 +21,8 @@
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   * THE SOFTWARE.
   */
+#include <time.h>
+
  #include "config-host.h"
 
  #include "net/net.h"
@@ -147,12 +149,13 @@ void qemu_macaddr_default_if_unset(MACAddr *macaddr)
 
      if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
          return;
+    srand((unsigned)time(NULL));
      macaddr->a[0] = 0x52;
      macaddr->a[1] = 0x54;
      macaddr->a[2] = 0x00;
-    macaddr->a[3] = 0x12;
-    macaddr->a[4] = 0x34;
-    macaddr->a[5] = 0x56 + index++;
+    macaddr->a[3] = rand() % 256;
+    macaddr->a[4] = rand() % 256;
+    macaddr->a[5] = rand() % 256 + index++;
  }
 
  /**