summary refs log tree commit diff
path: root/shared/nm-glib-aux
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2020-02-19 23:31:24 +0100
committerMichael Biebl <biebl@debian.org>2020-02-19 23:31:24 +0100
commite536d40eaea5dcdc0743b0a5e8e17faa46608a50 (patch)
tree0749832d8655215dac973866b4e724f3aea35448 /shared/nm-glib-aux
parentf3c6d0765dff885e168b94f28e06ecc640315a74 (diff)
New upstream version 1.22.8 upstream/1.22.8
Diffstat (limited to 'shared/nm-glib-aux')
-rw-r--r--shared/nm-glib-aux/nm-glib.h4
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.c101
-rw-r--r--shared/nm-glib-aux/nm-shared-utils.h13
3 files changed, 116 insertions, 2 deletions
diff --git a/shared/nm-glib-aux/nm-glib.h b/shared/nm-glib-aux/nm-glib.h
index dfb75bf0..26d0eacd 100644
--- a/shared/nm-glib-aux/nm-glib.h
+++ b/shared/nm-glib-aux/nm-glib.h
@@ -569,9 +569,9 @@ _nm_g_value_unset (GValue *value)
 
 /*****************************************************************************/
 
-#if !GLIB_CHECK_VERSION (2, 57, 2)
+/* G_SOURCE_FUNC was added in 2.57.2. */
+#undef G_SOURCE_FUNC
 #define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f))
-#endif
 
 /*****************************************************************************/
 
diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c
index 8e1c8b58..d47c465c 100644
--- a/shared/nm-glib-aux/nm-shared-utils.c
+++ b/shared/nm-glib-aux/nm-shared-utils.c
@@ -12,6 +12,7 @@
 #include <fcntl.h>
 #include <sys/syscall.h>
 #include <glib-unix.h>
+#include <net/if.h>
 
 #include "nm-errno.h"
 
@@ -2506,6 +2507,7 @@ nm_utils_hash_values_to_array (GHashTable *hash,
 		                   user_data);
 	}
 
+	NM_SET_OUT (out_len, len);
 	return arr;
 }
 
@@ -3987,3 +3989,102 @@ nm_utils_g_main_context_create_integrate_source (GMainContext *inner_context)
 
 	return &ctx_src->source;
 }
+
+gboolean
+nm_utils_ifname_valid_kernel (const char *name, GError **error)
+{
+	int i;
+
+	/* This function follows kernel's interface validation
+	 * function dev_valid_name() in net/core/dev.c.
+	 */
+
+	if (!name) {
+		g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+		                     _("interface name is missing"));
+		return FALSE;
+	}
+
+	if (name[0] == '\0') {
+		g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+		                     _("interface name is too short"));
+		return FALSE;
+	}
+
+	if (   name[0] == '.'
+	    && (   name[1] == '\0'
+	        || (   name[1] == '.'
+	            && name[2] == '\0'))) {
+		g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+		                     _("interface name is reserved"));
+		return FALSE;
+	}
+
+	for (i = 0; i < IFNAMSIZ; i++) {
+		char ch = name[i];
+
+		if (ch == '\0')
+			return TRUE;
+		if (   NM_IN_SET (ch, '/', ':')
+		    || g_ascii_isspace (ch)) {
+			g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+			                     _("interface name contains an invalid character"));
+			return FALSE;
+		}
+	}
+
+	g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+	                     _("interface name is longer than 15 characters"));
+	return FALSE;
+}
+
+static gboolean
+_nm_utils_ifname_valid_ovs (const char* name, GError **error)
+{
+	const char *ch;
+
+	/* OVS actually accepts a wider range of chars (all printable UTF-8 chars),
+	 NetworkManager restricts this to ASCII char as it's a safer option for
+	 now since OVS is not well documented on this matter.
+	 */
+	for (ch = name; *ch; ++ch) {
+		if (   *ch == '\\'
+		    || *ch == '/'
+		    || !g_ascii_isgraph (*ch)) {
+			g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+			                     _("interface name must be alphanumerical with "
+			                       "no forward or backward slashes"));
+			return FALSE;
+		}
+	};
+	return TRUE;
+}
+
+gboolean
+nm_utils_ifname_valid (const char* name,
+                       NMUtilsIfaceType type,
+                       GError **error)
+{
+	g_return_val_if_fail (!error || !(*error), FALSE);
+
+	if (!name || !(name[0])) {
+		g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+		                     _("interface name must not be empty"));
+		return FALSE;
+	}
+
+	if (!g_utf8_validate (name, -1, NULL)) {
+		g_set_error_literal (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+		                     _("interface name must be UTF-8 encoded"));
+		return FALSE;
+	}
+
+	switch (type) {
+	case NMU_IFACE_KERNEL:
+		return nm_utils_ifname_valid_kernel (name, error);
+	case NMU_IFACE_OVS:
+		return _nm_utils_ifname_valid_ovs (name, error);
+	}
+
+	g_return_val_if_reached (FALSE);
+}
diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h
index 3ec19ee1..740e61d0 100644
--- a/shared/nm-glib-aux/nm-shared-utils.h
+++ b/shared/nm-glib-aux/nm-shared-utils.h
@@ -1440,4 +1440,17 @@ guint nm_utils_parse_debug_string (const char *string,
                                    const GDebugKey *keys,
                                    guint nkeys);
 
+/*****************************************************************************/
+
+typedef enum {
+	NMU_IFACE_KERNEL = 0,
+	NMU_IFACE_OVS,
+} NMUtilsIfaceType;
+
+gboolean nm_utils_ifname_valid_kernel (const char *name, GError **error);
+
+gboolean nm_utils_ifname_valid (const char* name,
+                                NMUtilsIfaceType type,
+                                GError **error);
+
 #endif /* __NM_SHARED_UTILS_H__ */