[ovs-dev] [PATCH] ovs-vsctl: check if the device name is valid

Cong Wang xiyou.wangcong at gmail.com
Wed Oct 24 09:45:36 UTC 2012


Before waiting for the kernel to reject an invalid name, we
can actually check it before going into the kernel. The code
is stolen from linux kernel function dev_valid_name(),
but it should apply to non-Linux arch as well, because
IFNAMSIZ is POSIX and other errors are obvious.

After this patch I got:

# ovs-vsctl add-port ovsbr0 12345678901234567890
ovs-vsctl: cannot create a port named 12345678901234567890 because the name is not valid
# ovs-vsctl add-br 12345678901234567890
ovs-vsctl: cannot create a bridge named 12345678901234567890 because the name is not valid

Cc: Ben Pfaff <blp at nicira.com>
Signed-off-by: Cong Wang <xiyou.wangcong at gmail.com>

---
diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index fda3a89..3015f8c 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <net/if.h>
 
 #include "command-line.h"
 #include "compiler.h"
@@ -992,6 +993,28 @@ vsctl_context_populate_cache(struct vsctl_context *ctx)
     sset_destroy(&bridges);
 }
 
+/* Stolen from Linux kernel net/core/dev.c::dev_valid_name()
+ * Catch the most obvious errors and enforce the POSIX limit IFNAMSIZ
+ */
+static int
+is_valid_name(const char *name)
+{
+    if (*name == '\0')
+        return 0;
+    if (strlen(name) >= IFNAMSIZ)
+        return  0;
+    if (!strcmp(name, ".") || !strcmp(name, ".."))
+        return 0;
+
+    while (*name) {
+        if (*name == '/' || isspace(*name))
+            return 0;
+        name++;
+    }
+
+    return 1;
+}
+
 static void
 check_conflicts(struct vsctl_context *ctx, const char *name,
                 char *msg)
@@ -1001,6 +1024,10 @@ check_conflicts(struct vsctl_context *ctx, const char *name,
 
     verify_ports(ctx);
 
+    if (!is_valid_name(name)) {
+        vsctl_fatal("%s because the name is not valid", msg);
+    }
+
     if (shash_find(&ctx->bridges, name)) {
         vsctl_fatal("%s because a bridge named %s already exists",
                     msg, name);



More information about the dev mailing list