[PATCH] Add the check of the format of MAC address on virsh attach-interface

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi

Even if specified MAC address is invalid,
network interface is attached to the guest.
And attached network interface cannot communicate. 

This patch checks the format of MAC address,
and virsh become error when it is invalid.

Signed-off-by: Masayuki Sunou <fj1826dm@xxxxxxxxxxxxxxxxx>

Thanks,
Masayuki Sunou.

--------------------------------------------------------------------------------
Index: include/libvirt/virterror.h
===================================================================
RCS file: /data/cvs/libvirt/include/libvirt/virterror.h,v
retrieving revision 1.26
diff -u -p -r1.26 virterror.h
--- include/libvirt/virterror.h	6 Jul 2007 14:56:15 -0000	1.26
+++ include/libvirt/virterror.h	12 Jul 2007 02:00:54 -0000
@@ -127,6 +127,7 @@ typedef enum {
     VIR_WAR_NO_NETWORK, /* failed to start network */
     VIR_ERR_NO_DOMAIN, /* domain not found or unexpectedly disappeared */
     VIR_ERR_NO_NETWORK, /* network not found */
+    VIR_ERR_INVALID_MAC, /* invalid MAC adress */
 } virErrorNumber;
 
 /**
Index: src/virterror.c
===================================================================
RCS file: /data/cvs/libvirt/src/virterror.c,v
retrieving revision 1.28
diff -u -p -r1.28 virterror.c
--- src/virterror.c	6 Jul 2007 14:56:15 -0000	1.28
+++ src/virterror.c	12 Jul 2007 02:00:54 -0000
@@ -646,6 +646,12 @@ __virErrorMsg(virErrorNumber error, cons
 	    else
 		errmsg = _("Network not found: %s");
 	    break;
+    case VIR_ERR_INVALID_MAC:
+	    if (info == NULL)
+		errmsg = _("invalid MAC adress");
+	    else
+		errmsg = _("invalid MAC adress: %s");
+	    break;
     }
     return (errmsg);
 }
Index: src/xml.c
===================================================================
RCS file: /data/cvs/libvirt/src/xml.c,v
retrieving revision 1.81
diff -u -p -r1.81 xml.c
--- src/xml.c	11 Jul 2007 08:41:11 -0000	1.81
+++ src/xml.c	12 Jul 2007 02:00:55 -0000
@@ -934,8 +934,19 @@ virDomainParseXMLIfDesc(virConnectPtr co
     }
 
     virBufferAdd(buf, "(vif ", 5);
-    if (mac != NULL)
+    if (mac != NULL) {
+        unsigned int addr[12];
+        int ret = sscanf((const char *)mac, "%01x%01x:%01x%01x:%01x%01x:%01x%01x:%01x%01x:%01x%01x",
+                (unsigned int*)&addr[0], (unsigned int*)&addr[1],  (unsigned int*)&addr[2],
+                (unsigned int*)&addr[3], (unsigned int*)&addr[4],  (unsigned int*)&addr[5],
+                (unsigned int*)&addr[6], (unsigned int*)&addr[7],  (unsigned int*)&addr[8],
+                (unsigned int*)&addr[9], (unsigned int*)&addr[10], (unsigned int*)&addr[11]);
+        if (ret != 12 || strlen(mac) != 17) {
+            virXMLError(conn, VIR_ERR_INVALID_MAC, (const char *) mac, 0);
+            goto error;
+        }
         virBufferVSprintf(buf, "(mac '%s')", (const char *) mac);
+    }
     if (source != NULL) {
         if (typ == 0)
             virBufferVSprintf(buf, "(bridge '%s')", (const char *) source);

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]