Blob Blame History Raw
From 083e415a4908e2cdf7a9bf627d0aa637a7f2b04f Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Thu, 27 Sep 2018 15:39:29 +0200
Subject: [PATCH 1/7] all/trivial: rename hexstr<>bin conversion functions

"bin2str" and "str2bin" are not very clear. These strings are
hex-strings. Rename.

(cherry picked from commit 6714440669d27a59a6e0bcadd642c28f0c9d5737)
(cherry picked from commit cc93cf46adb8957ff21eb295c7680954ee1aabc2)
---
 libnm-core/nm-core-internal.h                 | 18 +++++------
 libnm-core/nm-utils.c                         | 32 +++++++++----------
 src/devices/nm-device.c                       | 12 +++----
 src/dhcp/nm-dhcp-utils.c                      |  2 +-
 .../plugins/ifcfg-rh/nms-ifcfg-rh-reader.c    |  2 +-
 5 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index cd2a77a71..be95ba0e1 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -212,15 +212,15 @@ guint nm_setting_ethtool_init_features (NMSettingEthtool *setting,
 guint8 *_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length);
 const char *nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_case, char *buf, gsize buf_len);
 
-char *_nm_utils_bin2str (gconstpointer addr, gsize length, gboolean upper_case);
-void _nm_utils_bin2str_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out);
-
-guint8 *_nm_utils_str2bin_full (const char *asc,
-                                gboolean delimiter_required,
-                                const char *delimiter_candidates,
-                                guint8 *buffer,
-                                gsize buffer_length,
-                                gsize *out_len);
+char *_nm_utils_bin2hexstr (gconstpointer addr, gsize length, gboolean upper_case);
+void _nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out);
+
+guint8 *_nm_utils_hexstr2bin_full (const char *asc,
+                                   gboolean delimiter_required,
+                                   const char *delimiter_candidates,
+                                   guint8 *buffer,
+                                   gsize buffer_length,
+                                   gsize *out_len);
 
 GSList *    _nm_utils_hash_values_to_slist (GHashTable *hash);
 
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index d4cbde1df..962cf8b8f 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -3613,12 +3613,12 @@ nm_utils_hwaddr_len (int type)
 }
 
 guint8 *
-_nm_utils_str2bin_full (const char *asc,
-                        gboolean delimiter_required,
-                        const char *delimiter_candidates,
-                        guint8 *buffer,
-                        gsize buffer_length,
-                        gsize *out_len)
+_nm_utils_hexstr2bin_full (const char *asc,
+                           gboolean delimiter_required,
+                           const char *delimiter_candidates,
+                           guint8 *buffer,
+                           gsize buffer_length,
+                           gsize *out_len)
 {
 	const char *in = asc;
 	guint8 *out = buffer;
@@ -3688,7 +3688,7 @@ _nm_utils_str2bin_full (const char *asc,
 	return buffer;
 }
 
-#define hwaddr_aton(asc, buffer, buffer_length, out_len) _nm_utils_str2bin_full ((asc), TRUE, ":-", (buffer), (buffer_length), (out_len))
+#define hwaddr_aton(asc, buffer, buffer_length, out_len) _nm_utils_hexstr2bin_full ((asc), TRUE, ":-", (buffer), (buffer_length), (out_len))
 
 /**
  * nm_utils_hexstr2bin:
@@ -3714,7 +3714,7 @@ nm_utils_hexstr2bin (const char *hex)
 
 	buffer_length = strlen (hex) / 2 + 3;
 	buffer = g_malloc (buffer_length);
-	if (!_nm_utils_str2bin_full (hex, FALSE, ":", buffer, buffer_length, &len)) {
+	if (!_nm_utils_hexstr2bin_full (hex, FALSE, ":", buffer, buffer_length, &len)) {
 		g_free (buffer);
 		return NULL;
 	}
@@ -3815,7 +3815,7 @@ nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length)
 }
 
 void
-_nm_utils_bin2str_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out)
+_nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out)
 {
 	const guint8 *in = addr;
 	const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef";
@@ -3865,7 +3865,7 @@ nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len)
 	g_return_val_if_fail (final_len < 0 || (gsize) final_len < buflen, NULL);
 
 	result = g_malloc (buflen);
-	_nm_utils_bin2str_full (src, len, '\0', FALSE, result);
+	_nm_utils_bin2hexstr_full (src, len, '\0', FALSE, result);
 
 	/* Cut converted key off at the correct length for this cipher type */
 	if (final_len >= 0 && (gsize) final_len < buflen)
@@ -3892,7 +3892,7 @@ nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length)
 	g_return_val_if_fail (length > 0, g_strdup (""));
 
 	result = g_malloc (length * 3);
-	_nm_utils_bin2str_full (addr, length, ':', TRUE, result);
+	_nm_utils_bin2hexstr_full (addr, length, ':', TRUE, result);
 	return result;
 }
 
@@ -3905,12 +3905,12 @@ nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_cas
 	if (buf_len < addr_len * 3)
 		g_return_val_if_reached (NULL);
 
-	_nm_utils_bin2str_full (addr, addr_len, ':', upper_case, buf);
+	_nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
 	return buf;
 }
 
 /**
- * _nm_utils_bin2str:
+ * _nm_utils_bin2hexstr:
  * @addr: (type guint8) (array length=length): a binary hardware address
  * @length: the length of @addr
  * @upper_case: the case for the hexadecimal digits.
@@ -3920,7 +3920,7 @@ nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_cas
  * Return value: (transfer full): the textual form of @addr
  */
 char *
-_nm_utils_bin2str (gconstpointer addr, gsize length, gboolean upper_case)
+_nm_utils_bin2hexstr (gconstpointer addr, gsize length, gboolean upper_case)
 {
 	char *result;
 
@@ -3928,7 +3928,7 @@ _nm_utils_bin2str (gconstpointer addr, gsize length, gboolean upper_case)
 	g_return_val_if_fail (length > 0, g_strdup (""));
 
 	result = g_malloc (length * 3);
-	_nm_utils_bin2str_full (addr, length, ':', upper_case, result);
+	_nm_utils_bin2hexstr_full (addr, length, ':', upper_case, result);
 	return result;
 }
 
@@ -4596,7 +4596,7 @@ _nm_utils_dhcp_duid_valid (const char *duid, GBytes **out_duid_bin)
 		return TRUE;
 	}
 
-	if (_nm_utils_str2bin_full (duid, FALSE, ":", duid_arr, sizeof (duid_arr), &duid_len)) {
+	if (_nm_utils_hexstr2bin_full (duid, FALSE, ":", duid_arr, sizeof (duid_arr), &duid_len)) {
 		/* MAX DUID length is 128 octects + the type code (2 octects). */
 		if (   duid_len > 2
 		    && duid_len <= (128 + 2)) {
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index 51751ecf3..2de575cff 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -14349,9 +14349,9 @@ nm_device_spawn_iface_helper (NMDevice *self)
 			if (client_id) {
 				g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid"));
 				g_ptr_array_add (argv,
-				                 _nm_utils_bin2str (g_bytes_get_data (client_id, NULL),
-				                                    g_bytes_get_size (client_id),
-				                                    FALSE));
+				                 _nm_utils_bin2hexstr (g_bytes_get_data (client_id, NULL),
+				                                       g_bytes_get_size (client_id),
+				                                       FALSE));
 			}
 
 			hostname = nm_dhcp_client_get_hostname (priv->dhcp4.client);
@@ -14389,9 +14389,9 @@ nm_device_spawn_iface_helper (NMDevice *self)
 		if (nm_device_get_ip_iface_identifier (self, &iid, FALSE)) {
 			g_ptr_array_add (argv, g_strdup ("--iid"));
 			g_ptr_array_add (argv,
-			                 _nm_utils_bin2str (iid.id_u8,
-			                                    sizeof (NMUtilsIPv6IfaceId),
-			                                    FALSE));
+			                 _nm_utils_bin2hexstr (iid.id_u8,
+			                                       sizeof (NMUtilsIPv6IfaceId),
+			                                       FALSE));
 		}
 
 		g_ptr_array_add (argv, g_strdup ("--addr-gen-mode"));
diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c
index 6bbc670b2..f6d97b5ff 100644
--- a/src/dhcp/nm-dhcp-utils.c
+++ b/src/dhcp/nm-dhcp-utils.c
@@ -729,7 +729,7 @@ nm_dhcp_utils_duid_to_string (GBytes *duid)
 	g_return_val_if_fail (duid != NULL, NULL);
 
 	data = g_bytes_get_data (duid, &len);
-	return _nm_utils_bin2str (data, len, FALSE);
+	return _nm_utils_bin2hexstr (data, len, FALSE);
 }
 
 /**
diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
index 09a379914..f8c4d18d4 100644
--- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
+++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
@@ -189,7 +189,7 @@ _secret_password_raw_to_bytes (const char *ifcfg_key,
 		password_raw += 2;
 
 	secret = nm_secret_buf_new (strlen (password_raw) / 2 + 3);
-	if (!_nm_utils_str2bin_full (password_raw, FALSE, ":", secret->bin, secret->len, &len)) {
+	if (!_nm_utils_hexstr2bin_full (password_raw, FALSE, ":", secret->bin, secret->len, &len)) {
 		g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
 		             "Invalid hex password in %s",
 		             ifcfg_key);
-- 
2.19.1


From 3305ae13a18b39b3cde94ee80e882a48610b677e Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Thu, 27 Sep 2018 15:47:35 +0200
Subject: [PATCH 2/7] libnm: return output buffer from
 _nm_utils_bin2hexstr_full()

It's just more convenient, as it allows better chaining.

Also, allow passing %NULL as @out buffer. It's clear how
large the output buffer must be, so for convenience let the
function (optionally) allocate a new buffer.

This behavior of whether to
  - take @out, fill it, and return @out
  - take no @out, allocate new buffer, fill and and return it
is slightly error prone. But it was already error prone before, when
it would accept an input buffer without explicit buffer length. I think
this makes it more safe, because in the common case the caller can avoid
pre-allocating a buffer of the right size and the function gets it
right.

(cherry picked from commit 21df8d38effc8d9a36d7f2ac5fb674ddbf848a25)
(cherry picked from commit f4973558dc7036b54b23c69ae57123ef26ba8d0c)
---
 libnm-core/nm-core-internal.h |  2 +-
 libnm-core/nm-utils.c         | 53 +++++++++++++++++++++++++----------
 2 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index be95ba0e1..c3be9acef 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -213,7 +213,7 @@ guint8 *_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_le
 const char *nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_case, char *buf, gsize buf_len);
 
 char *_nm_utils_bin2hexstr (gconstpointer addr, gsize length, gboolean upper_case);
-void _nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out);
+char *_nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out);
 
 guint8 *_nm_utils_hexstr2bin_full (const char *asc,
                                    gboolean delimiter_required,
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index 962cf8b8f..ef0fd4084 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -3814,16 +3814,46 @@ nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length)
 	return buffer;
 }
 
-void
-_nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out)
+/**
+ * _nm_utils_bin2hexstr_full:
+ * @addr: pointer of @length bytes.
+ * @length: number of bytes in @addr
+ * @delimiter: either '\0', otherwise the output string will have the
+ *   given delimiter character between each two hex numbers.
+ * @upper_case: if TRUE, use upper case ASCII characters for hex.
+ * @out: if %NULL, the function will allocate a new buffer of
+ *   either (@length*2+1) or (@length*3) bytes, depending on whether
+ *   a @delimiter is specified. In that case, the allocated buffer will
+ *   be returned and must be freed by the caller.
+ *   If not %NULL, the buffer must already be preallocated and contain
+ *   at least (@length*2+1) or (@length*3) bytes, depending on the delimiter.
+ *
+ * Returns: the binary value converted to a hex string. If @out is given,
+ *   this always returns @out. If @out is %NULL, a newly allocated string
+ *   is returned.
+ */
+char *
+_nm_utils_bin2hexstr_full (gconstpointer addr,
+                           gsize length,
+                           char delimiter,
+                           gboolean upper_case,
+                           char *out)
 {
 	const guint8 *in = addr;
 	const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef";
+	char *out0;
 
 	nm_assert (addr);
-	nm_assert (out);
 	nm_assert (length > 0);
 
+	if (out)
+		out0 = out;
+	else {
+		out0 = out = g_new (char, delimiter == '\0'
+		                          ? length * 2 + 1
+		                          : length * 3);
+	}
+
 	/* @out must contain at least @length*3 bytes if @delimiter is set,
 	 * otherwise, @length*2+1. */
 
@@ -3840,6 +3870,7 @@ _nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimite
 	}
 
 	*out = 0;
+	return out0;
 }
 
 /**
@@ -3865,6 +3896,7 @@ nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len)
 	g_return_val_if_fail (final_len < 0 || (gsize) final_len < buflen, NULL);
 
 	result = g_malloc (buflen);
+
 	_nm_utils_bin2hexstr_full (src, len, '\0', FALSE, result);
 
 	/* Cut converted key off at the correct length for this cipher type */
@@ -3886,14 +3918,10 @@ nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len)
 char *
 nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length)
 {
-	char *result;
-
 	g_return_val_if_fail (addr, g_strdup (""));
 	g_return_val_if_fail (length > 0, g_strdup (""));
 
-	result = g_malloc (length * 3);
-	_nm_utils_bin2hexstr_full (addr, length, ':', TRUE, result);
-	return result;
+	return _nm_utils_bin2hexstr_full (addr, length, ':', TRUE, NULL);
 }
 
 const char *
@@ -3905,8 +3933,7 @@ nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_cas
 	if (buf_len < addr_len * 3)
 		g_return_val_if_reached (NULL);
 
-	_nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
-	return buf;
+	return _nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
 }
 
 /**
@@ -3922,14 +3949,10 @@ nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_cas
 char *
 _nm_utils_bin2hexstr (gconstpointer addr, gsize length, gboolean upper_case)
 {
-	char *result;
-
 	g_return_val_if_fail (addr, g_strdup (""));
 	g_return_val_if_fail (length > 0, g_strdup (""));
 
-	result = g_malloc (length * 3);
-	_nm_utils_bin2hexstr_full (addr, length, ':', upper_case, result);
-	return result;
+	return _nm_utils_bin2hexstr_full (addr, length, ':', upper_case, NULL);
 }
 
 /**
-- 
2.19.1


From 10792c768f1074f4803cba9840d2662793d69c38 Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Thu, 27 Sep 2018 16:05:29 +0200
Subject: [PATCH 3/7] all: drop _nm_utils_bin2hexstr()

We already have nm_utils_bin2hexstr() and _nm_utils_bin2hexstr_full().
This is confusing.

  - nm_utils_bin2hexstr() is public API of libnm. Also, it has
    a last argument @final_len to truncate the string at that
    length.
    It uses no delimiter and lower-case characters.

  - _nm_utils_bin2hexstr_full() does not do any truncation, but
    it has options to specify a delimiter, the character case,
    and to update a given buffer in-place. Also, like
    nm_utils_bin2hexstr() and _nm_utils_bin2hexstr() it can
    allocate a new buffer on demand.

  - _nm_utils_bin2hexstr() would use ':' as delimiter and make
    the case configurable. Also, it would always allocate the returned
    buffer.

It's too much and confusing. Drop _nm_utils_bin2hexstr() which is internal
API and just a wrapper around _nm_utils_bin2hexstr_full().

(cherry picked from commit b537c0388a0ca7320d01c9ba2c6b5b4fe45f267f)
(cherry picked from commit 2a8bef4454d6b1e8aeded8bbd2122538a5da41a4)
---
 libnm-core/nm-core-internal.h |  1 -
 libnm-core/nm-utils.c         | 19 -------------------
 src/devices/nm-device.c       | 16 ++++++++++------
 src/dhcp/nm-dhcp-utils.c      |  4 ++--
 4 files changed, 12 insertions(+), 28 deletions(-)

diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index c3be9acef..9edd332d5 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -212,7 +212,6 @@ guint nm_setting_ethtool_init_features (NMSettingEthtool *setting,
 guint8 *_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length);
 const char *nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_case, char *buf, gsize buf_len);
 
-char *_nm_utils_bin2hexstr (gconstpointer addr, gsize length, gboolean upper_case);
 char *_nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out);
 
 guint8 *_nm_utils_hexstr2bin_full (const char *asc,
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index ef0fd4084..457189698 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -3936,25 +3936,6 @@ nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_cas
 	return _nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
 }
 
-/**
- * _nm_utils_bin2hexstr:
- * @addr: (type guint8) (array length=length): a binary hardware address
- * @length: the length of @addr
- * @upper_case: the case for the hexadecimal digits.
- *
- * Converts @addr to textual form.
- *
- * Return value: (transfer full): the textual form of @addr
- */
-char *
-_nm_utils_bin2hexstr (gconstpointer addr, gsize length, gboolean upper_case)
-{
-	g_return_val_if_fail (addr, g_strdup (""));
-	g_return_val_if_fail (length > 0, g_strdup (""));
-
-	return _nm_utils_bin2hexstr_full (addr, length, ':', upper_case, NULL);
-}
-
 /**
  * nm_utils_hwaddr_valid:
  * @asc: the ASCII representation of a hardware address
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c
index 2de575cff..c6d591db8 100644
--- a/src/devices/nm-device.c
+++ b/src/devices/nm-device.c
@@ -14349,9 +14349,11 @@ nm_device_spawn_iface_helper (NMDevice *self)
 			if (client_id) {
 				g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid"));
 				g_ptr_array_add (argv,
-				                 _nm_utils_bin2hexstr (g_bytes_get_data (client_id, NULL),
-				                                       g_bytes_get_size (client_id),
-				                                       FALSE));
+				                 _nm_utils_bin2hexstr_full (g_bytes_get_data (client_id, NULL),
+				                                            g_bytes_get_size (client_id),
+				                                            ':',
+				                                            FALSE,
+				                                            NULL));
 			}
 
 			hostname = nm_dhcp_client_get_hostname (priv->dhcp4.client);
@@ -14389,9 +14391,11 @@ nm_device_spawn_iface_helper (NMDevice *self)
 		if (nm_device_get_ip_iface_identifier (self, &iid, FALSE)) {
 			g_ptr_array_add (argv, g_strdup ("--iid"));
 			g_ptr_array_add (argv,
-			                 _nm_utils_bin2hexstr (iid.id_u8,
-			                                       sizeof (NMUtilsIPv6IfaceId),
-			                                       FALSE));
+			                 _nm_utils_bin2hexstr_full (iid.id_u8,
+			                                            sizeof (NMUtilsIPv6IfaceId),
+			                                            ':',
+			                                            FALSE,
+			                                            NULL));
 		}
 
 		g_ptr_array_add (argv, g_strdup ("--addr-gen-mode"));
diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c
index f6d97b5ff..9b1653b8f 100644
--- a/src/dhcp/nm-dhcp-utils.c
+++ b/src/dhcp/nm-dhcp-utils.c
@@ -726,10 +726,10 @@ nm_dhcp_utils_duid_to_string (GBytes *duid)
 	gconstpointer data;
 	gsize len;
 
-	g_return_val_if_fail (duid != NULL, NULL);
+	g_return_val_if_fail (duid, NULL);
 
 	data = g_bytes_get_data (duid, &len);
-	return _nm_utils_bin2hexstr (data, len, FALSE);
+	return _nm_utils_bin2hexstr_full (data, len, ':', FALSE, NULL);
 }
 
 /**
-- 
2.19.1


From 7135449107363ab356db4d4e2afc45bab4952978 Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Thu, 27 Sep 2018 16:29:55 +0200
Subject: [PATCH 4/7] libnm: cleanup _nm_utils_hexstr2bin*() helper

Add 3 variants of _nm_utils_hexstr2bin*():

  - _nm_utils_hexstr2bin_full(), which takes a preallocated
    buffer and fills it.
  - _nm_utils_hexstr2bin_alloc() which returns a malloc'ed
    buffer
  - _nm_utils_hexstr2bin_buf(), which fills a preallocated
    buffer of a specific size.

(cherry picked from commit be6c7fa5f66c0fa155d703996a5a858c88b8230c)
(cherry picked from commit 5dc8a14576d2ea9732518586027df6b0c3c8784f)
---
 libnm-core/nm-core-internal.h                 |  16 ++-
 libnm-core/nm-utils.c                         | 123 +++++++++++++-----
 .../plugins/ifcfg-rh/nms-ifcfg-rh-reader.c    |   2 +-
 3 files changed, 105 insertions(+), 36 deletions(-)

diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index 9edd332d5..761461084 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -214,13 +214,25 @@ const char *nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboole
 
 char *_nm_utils_bin2hexstr_full (gconstpointer addr, gsize length, const char delimiter, gboolean upper_case, char *out);
 
-guint8 *_nm_utils_hexstr2bin_full (const char *asc,
+guint8 *_nm_utils_hexstr2bin_full (const char *hexstr,
+                                   gboolean allow_0x_prefix,
                                    gboolean delimiter_required,
                                    const char *delimiter_candidates,
+                                   gsize required_len,
                                    guint8 *buffer,
-                                   gsize buffer_length,
+                                   gsize buffer_len,
                                    gsize *out_len);
 
+#define _nm_utils_hexstr2bin_buf(hexstr, allow_0x_prefix, delimiter_required, delimiter_candidates, buffer) \
+    _nm_utils_hexstr2bin_full ((hexstr), (allow_0x_prefix), (delimiter_required), (delimiter_candidates), G_N_ELEMENTS (buffer), (buffer), G_N_ELEMENTS (buffer), NULL)
+
+guint8 *_nm_utils_hexstr2bin_alloc (const char *hexstr,
+                                    gboolean allow_0x_prefix,
+                                    gboolean delimiter_required,
+                                    const char *delimiter_candidates,
+                                    gsize required_len,
+                                    gsize *out_len);
+
 GSList *    _nm_utils_hash_values_to_slist (GHashTable *hash);
 
 GHashTable *_nm_utils_copy_strdict (GHashTable *strdict);
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index 457189698..59b7f0432 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -3613,60 +3613,68 @@ nm_utils_hwaddr_len (int type)
 }
 
 guint8 *
-_nm_utils_hexstr2bin_full (const char *asc,
+_nm_utils_hexstr2bin_full (const char *hexstr,
+                           gboolean allow_0x_prefix,
                            gboolean delimiter_required,
                            const char *delimiter_candidates,
+                           gsize required_len,
                            guint8 *buffer,
-                           gsize buffer_length,
+                           gsize buffer_len,
                            gsize *out_len)
 {
-	const char *in = asc;
+	const char *in = hexstr;
 	guint8 *out = buffer;
 	gboolean delimiter_has = TRUE;
 	guint8 delimiter = '\0';
+	gsize len;
 
-	nm_assert (asc);
+	nm_assert (hexstr);
 	nm_assert (buffer);
-	nm_assert (buffer_length);
-	nm_assert (out_len);
+	nm_assert (required_len > 0 || out_len);
+
+	if (   allow_0x_prefix
+	    && in[0] == '0'
+	    && in[1] == 'x')
+		in += 2;
 
 	while (TRUE) {
 		const guint8 d1 = in[0];
 		guint8 d2;
+		int i1, i2;
 
-		if (!g_ascii_isxdigit (d1))
-			return NULL;
-
-#define HEXVAL(c) ((c) <= '9' ? (c) - '0' : ((c) & 0x4F) - ('A' - 10))
+		i1 = nm_utils_hexchar_to_int (d1);
+		if (i1 < 0)
+			goto fail;
 
 		/* If there's no leading zero (ie "aa:b:cc") then fake it */
 		d2 = in[1];
-		if (d2 && g_ascii_isxdigit (d2)) {
-			*out++ = (HEXVAL (d1) << 4) + HEXVAL (d2);
+		if (   d2
+		    && (i2 = nm_utils_hexchar_to_int (d2)) >= 0) {
+			*out++ = (i1 << 4) + i2;
 			d2 = in[2];
 			if (!d2)
 				break;
 			in += 2;
 		} else {
 			/* Fake leading zero */
-			*out++ = HEXVAL (d1);
+			*out++ = i1;
 			if (!d2) {
 				if (!delimiter_has) {
 					/* when using no delimiter, there must be pairs of hex chars */
-					return NULL;
+					goto fail;
 				}
 				break;
 			}
 			in += 1;
 		}
 
-		if (--buffer_length == 0)
-			return NULL;
+		if (--buffer_len == 0)
+			goto fail;
 
 		if (delimiter_has) {
 			if (d2 != delimiter) {
 				if (delimiter)
-					return NULL;
+					goto fail;
 				if (delimiter_candidates) {
 					while (delimiter_candidates[0]) {
 						if (delimiter_candidates++[0] == d2)
@@ -3675,7 +3683,7 @@ _nm_utils_hexstr2bin_full (const char *asc,
 				}
 				if (!delimiter) {
 					if (delimiter_required)
-						return NULL;
+						goto fail;
 					delimiter_has = FALSE;
 					continue;
 				}
@@ -3684,11 +3692,66 @@ _nm_utils_hexstr2bin_full (const char *asc,
 		}
 	}
 
-	*out_len = out - buffer;
-	return buffer;
+	len = out - buffer;
+	if (   required_len == 0
+	    || len == required_len) {
+		NM_SET_OUT (out_len, len);
+		return buffer;
+	}
+
+fail:
+	NM_SET_OUT (out_len, 0);
+	return NULL;
 }
 
-#define hwaddr_aton(asc, buffer, buffer_length, out_len) _nm_utils_hexstr2bin_full ((asc), TRUE, ":-", (buffer), (buffer_length), (out_len))
+guint8 *
+_nm_utils_hexstr2bin_alloc (const char *hexstr,
+                            gboolean allow_0x_prefix,
+                            gboolean delimiter_required,
+                            const char *delimiter_candidates,
+                            gsize required_len,
+                            gsize *out_len)
+{
+	guint8 *buffer;
+	gsize buffer_len, len;
+
+	g_return_val_if_fail (hexstr, NULL);
+
+	nm_assert (required_len > 0 || out_len);
+
+	if (   allow_0x_prefix
+	    && hexstr[0] == '0'
+	    && hexstr[1] == 'x')
+		hexstr += 2;
+
+	if (!hexstr[0])
+		goto fail;
+
+	if (required_len > 0)
+		buffer_len = required_len;
+	else
+		buffer_len = strlen (hexstr) / 2 + 3;
+
+	buffer = g_malloc (buffer_len);
+
+	if (_nm_utils_hexstr2bin_full (hexstr,
+	                               FALSE,
+	                               delimiter_required,
+	                               delimiter_candidates,
+	                               required_len,
+	                               buffer,
+	                               buffer_len,
+	                               &len)) {
+		NM_SET_OUT (out_len, len);
+		return buffer;
+	}
+
+	g_free (buffer);
+
+fail:
+	NM_SET_OUT (out_len, 0);
+	return NULL;
+}
 
 /**
  * nm_utils_hexstr2bin:
@@ -3705,23 +3768,17 @@ GBytes *
 nm_utils_hexstr2bin (const char *hex)
 {
 	guint8 *buffer;
-	gsize buffer_length, len;
-
-	g_return_val_if_fail (hex != NULL, NULL);
-
-	if (hex[0] == '0' && hex[1] == 'x')
-		hex += 2;
+	gsize len;
 
-	buffer_length = strlen (hex) / 2 + 3;
-	buffer = g_malloc (buffer_length);
-	if (!_nm_utils_hexstr2bin_full (hex, FALSE, ":", buffer, buffer_length, &len)) {
-		g_free (buffer);
+	buffer = _nm_utils_hexstr2bin_alloc (hex, TRUE, FALSE, ":", 0, &len);
+	if (!buffer)
 		return NULL;
-	}
 	buffer = g_realloc (buffer, len);
 	return g_bytes_new_take (buffer, len);
 }
 
+#define hwaddr_aton(asc, buffer, buffer_len, out_len) _nm_utils_hexstr2bin_full ((asc), FALSE, TRUE, ":-", 0, (buffer), (buffer_len), (out_len))
+
 /**
  * nm_utils_hwaddr_atoba:
  * @asc: the ASCII representation of a hardware address
@@ -4600,7 +4657,7 @@ _nm_utils_dhcp_duid_valid (const char *duid, GBytes **out_duid_bin)
 		return TRUE;
 	}
 
-	if (_nm_utils_hexstr2bin_full (duid, FALSE, ":", duid_arr, sizeof (duid_arr), &duid_len)) {
+	if (_nm_utils_hexstr2bin_full (duid, FALSE, FALSE, ":", 0, duid_arr, sizeof (duid_arr), &duid_len)) {
 		/* MAX DUID length is 128 octects + the type code (2 octects). */
 		if (   duid_len > 2
 		    && duid_len <= (128 + 2)) {
diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
index f8c4d18d4..70fffb888 100644
--- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
+++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c
@@ -189,7 +189,7 @@ _secret_password_raw_to_bytes (const char *ifcfg_key,
 		password_raw += 2;
 
 	secret = nm_secret_buf_new (strlen (password_raw) / 2 + 3);
-	if (!_nm_utils_hexstr2bin_full (password_raw, FALSE, ":", secret->bin, secret->len, &len)) {
+	if (!_nm_utils_hexstr2bin_full (password_raw, FALSE, FALSE, ":", 0, secret->bin, secret->len, &len)) {
 		g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
 		             "Invalid hex password in %s",
 		             ifcfg_key);
-- 
2.19.1


From 944ce0bf973a47007cc0b79f8d6636985f1156fb Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Thu, 18 Oct 2018 08:17:52 +0200
Subject: [PATCH 5/7] dhcp: log client-id of DHCP instance

(cherry picked from commit 2af1dc1d287334dcd02d8282fcfdbe3a8e031363)
(cherry picked from commit 16bde2d1eca80ddaade01e35b0c6fc34bc937fe2)
---
 src/dhcp/nm-dhcp-client.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/dhcp/nm-dhcp-client.c b/src/dhcp/nm-dhcp-client.c
index 16db83068..143d26184 100644
--- a/src/dhcp/nm-dhcp-client.c
+++ b/src/dhcp/nm-dhcp-client.c
@@ -230,6 +230,15 @@ _set_client_id (NMDhcpClient *self, GBytes *client_id, gboolean take)
 	priv->client_id = client_id;
 	if (!take && client_id)
 		g_bytes_ref (client_id);
+
+	{
+		gs_free char *s = NULL;
+
+		_LOGT ("client-id: %s",
+		       priv->client_id
+		         ? (s = nm_dhcp_utils_duid_to_string (priv->client_id))
+		         : "default");
+	}
 }
 
 void
-- 
2.19.1


From 6724db6016f558e23772c5c92d7ea1be6c080219 Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Wed, 3 Oct 2018 21:50:27 +0200
Subject: [PATCH 6/7] core: add "nm-sd-utils.h" to access system internal
 helper

We have a fork of a lot of useful systemd helper code.
However, until now we shyed away from using it aside from
the bits that we really need.

That means, although we have some really nice implementations
in our source-tree, we didn't use them. Either we were missing
them, or we had to re-implement them.

Add "nm-sd-utils.h" header to very carefully make internal
systemd API accessible to the rest of core.

This is not intended as a vehicle to access all of internal
API. Instead, this must be used with care, and only a hand picked
selection of functions must be exposed. Use with caution, but where it
makes sense.

(cherry picked from commit eece5aff0992c9076a9b5513e2a893e37e4999ea)
(cherry picked from commit 7494145649aa81155bcfc71fc84a923fdec605e3)
---
 Makefile.am                       |  2 ++
 src/systemd/meson.build           |  3 +-
 src/systemd/nm-sd-utils.c         | 45 ++++++++++++++++++++++++++++++
 src/systemd/nm-sd-utils.h         | 33 ++++++++++++++++++++++
 src/systemd/src/basic/path-util.c |  2 --
 src/tests/test-systemd.c          | 46 +++++++++++++++++++++++++++++++
 6 files changed, 128 insertions(+), 3 deletions(-)
 create mode 100644 src/systemd/nm-sd-utils.c
 create mode 100644 src/systemd/nm-sd-utils.h

diff --git a/Makefile.am b/Makefile.am
index 5b0feefa4..24b460bd0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1390,6 +1390,8 @@ src_libsystemd_nm_la_libadd = \
 src_libsystemd_nm_la_SOURCES = \
 	src/systemd/nm-sd.c \
 	src/systemd/nm-sd.h \
+	src/systemd/nm-sd-utils.c \
+	src/systemd/nm-sd-utils.h \
 	src/systemd/sd-adapt/nm-sd-adapt.c \
 	src/systemd/sd-adapt/nm-sd-adapt.h \
 	src/systemd/sd-adapt/architecture.h \
diff --git a/src/systemd/meson.build b/src/systemd/meson.build
index 870721b0d..1bf1ea41d 100644
--- a/src/systemd/meson.build
+++ b/src/systemd/meson.build
@@ -49,7 +49,8 @@ sources = files(
   'src/libsystemd/sd-id128/id128-util.c',
   'src/libsystemd/sd-id128/sd-id128.c',
   'src/shared/dns-domain.c',
-  'nm-sd.c'
+  'nm-sd.c',
+  'nm-sd-utils.c',
 )
 
 incs = [
diff --git a/src/systemd/nm-sd-utils.c b/src/systemd/nm-sd-utils.c
new file mode 100644
index 000000000..914b4b448
--- /dev/null
+++ b/src/systemd/nm-sd-utils.c
@@ -0,0 +1,45 @@
+/* This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ */
+
+#include "nm-default.h"
+
+#include "nm-sd-utils.h"
+
+#include "nm-sd-adapt.h"
+
+#include "path-util.h"
+
+/*****************************************************************************/
+
+gboolean
+nm_sd_utils_path_equal (const char *a, const char *b)
+{
+	return path_equal (a, b);
+}
+
+char *
+nm_sd_utils_path_simplify (char *path, gboolean kill_dots)
+{
+	return path_simplify (path, kill_dots);
+}
+
+const char *
+nm_sd_utils_path_startswith (const char *path, const char *prefix)
+{
+	return path_startswith (path, prefix);
+}
diff --git a/src/systemd/nm-sd-utils.h b/src/systemd/nm-sd-utils.h
new file mode 100644
index 000000000..8ca920e27
--- /dev/null
+++ b/src/systemd/nm-sd-utils.h
@@ -0,0 +1,33 @@
+/* This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ */
+
+#ifndef __NM_SD_UTILS_H__
+#define __NM_SD_UTILS_H__
+
+/*****************************************************************************/
+
+gboolean nm_sd_utils_path_equal (const char *a, const char *b);
+
+char *nm_sd_utils_path_simplify (char *path, gboolean kill_dots);
+
+const char *nm_sd_utils_path_startswith (const char *path, const char *prefix);
+
+/*****************************************************************************/
+
+#endif /* __NM_SD_UTILS_H__ */
+
diff --git a/src/systemd/src/basic/path-util.c b/src/systemd/src/basic/path-util.c
index 5202eae3c..3656a0110 100644
--- a/src/systemd/src/basic/path-util.c
+++ b/src/systemd/src/basic/path-util.c
@@ -380,7 +380,6 @@ char *path_simplify(char *path, bool kill_dots) {
         return path;
 }
 
-#if 0 /* NM_IGNORED */
 char* path_startswith(const char *path, const char *prefix) {
         assert(path);
         assert(prefix);
@@ -423,7 +422,6 @@ char* path_startswith(const char *path, const char *prefix) {
                 prefix += b;
         }
 }
-#endif /* NM_IGNORED */
 
 int path_compare(const char *a, const char *b) {
         int d;
diff --git a/src/tests/test-systemd.c b/src/tests/test-systemd.c
index ab5fed22a..4660bd0d1 100644
--- a/src/tests/test-systemd.c
+++ b/src/tests/test-systemd.c
@@ -20,6 +20,7 @@
 #include "nm-default.h"
 
 #include "systemd/nm-sd.h"
+#include "systemd/nm-sd-utils.h"
 
 #include "nm-test-utils-core.h"
 
@@ -173,6 +174,50 @@ test_sd_event (void)
 
 /*****************************************************************************/
 
+static void
+test_path_equal (void)
+{
+#define _path_equal_check1(path, kill_dots, expected) \
+	G_STMT_START { \
+		const gboolean _kill_dots = (kill_dots); \
+		const char *_path0 = (path); \
+		const char *_expected = (expected); \
+		gs_free char *_path = g_strdup (_path0); \
+		const char *_path_result; \
+		\
+		if (   !_kill_dots \
+		    && !nm_sd_utils_path_equal (_path0, _expected)) \
+			g_error ("Paths \"%s\" and \"%s\" don't compare equal", _path0, _expected); \
+		\
+		_path_result = nm_sd_utils_path_simplify (_path, _kill_dots); \
+		g_assert (_path_result == _path); \
+		g_assert_cmpstr (_path, ==, _expected); \
+	} G_STMT_END
+
+#define _path_equal_check(path, expected_no_kill_dots, expected_kill_dots) \
+	G_STMT_START { \
+		_path_equal_check1 (path, FALSE, expected_no_kill_dots); \
+		_path_equal_check1 (path, TRUE,  expected_kill_dots ?: expected_no_kill_dots); \
+	} G_STMT_END
+
+	_path_equal_check ("",                  "",                NULL);
+	_path_equal_check (".",                 ".",               "");
+	_path_equal_check ("..",                "..",              NULL);
+	_path_equal_check ("/..",               "/..",             NULL);
+	_path_equal_check ("//..",              "/..",             NULL);
+	_path_equal_check ("/.",                "/.",              "/");
+	_path_equal_check ("./",                ".",               "");
+	_path_equal_check ("./.",               "./.",             "");
+	_path_equal_check (".///.",             "./.",             "");
+	_path_equal_check (".///./",            "./.",             "");
+	_path_equal_check (".////",             ".",               "");
+	_path_equal_check ("//..//foo/",        "/../foo",         NULL);
+	_path_equal_check ("///foo//./bar/.",   "/foo/./bar/.",    "/foo/bar");
+	_path_equal_check (".//./foo//./bar/.", "././foo/./bar/.", "foo/bar");
+}
+
+/*****************************************************************************/
+
 NMTST_DEFINE ();
 
 int
@@ -183,6 +228,7 @@ main (int argc, char **argv)
 	g_test_add_func ("/systemd/dhcp/create", test_dhcp_create);
 	g_test_add_func ("/systemd/lldp/create", test_lldp_create);
 	g_test_add_func ("/systemd/sd-event", test_sd_event);
+	g_test_add_func ("/systemd/test_path_equal", test_path_equal);
 
 	return g_test_run ();
 }
-- 
2.19.1


From af9de51249ee7a750248f43bed38e0344e8bee9f Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Mon, 12 Nov 2018 15:23:36 +0100
Subject: [PATCH 7/7] build: avoid header conflict for <linux/if.h> and
 <net/if.h> with "nm-platform.h"

In the past, the headers "linux/if.h" and "net/if.h" were incompatible.
That means, we can either include one or the other, but not both.
This is fixed in the meantime, however the issue still exists when
building against older kernel/glibc.

That means, including one of these headers from a header file
is problematic. In particular if it's a header like "nm-platform.h",
which itself is dragged in by many other headers.

Avoid that by not including these headers from "platform.h", but instead
from the source files where needed (or possibly from less popular header
files).

Currently there is no problem. However, this allows an unknowing user to
include <net/if.h> at the same time with "nm-platform.h", which is easy
to get wrong.

(cherry picked from commit 37e47fbdabd6439c022a5fc3b72176aa6c4e569f)
(cherry picked from commit aa7acb0ae3046d142a5469b50a9464bc9e6543ac)
---
 src/devices/nm-device-infiniband.c        |  1 +
 src/devices/nm-device-ip-tunnel.c         |  1 +
 src/devices/nm-device-macvlan.c           |  1 +
 src/devices/nm-device-wpan.c              |  1 +
 src/dhcp/nm-dhcp-dhclient-utils.c         |  1 +
 src/ndisc/nm-ndisc.h                      |  1 +
 src/nm-test-utils-core.h                  |  4 ++--
 src/platform/nm-fake-platform.c           |  1 +
 src/platform/nm-platform-utils.c          |  1 +
 src/platform/nm-platform.c                |  1 +
 src/platform/nm-platform.h                | 20 ++++++++++----------
 src/platform/nmp-object.c                 |  1 +
 src/platform/tests/test-common.h          |  3 +++
 src/platform/wifi/nm-wifi-utils-nl80211.c |  1 +
 src/platform/wpan/nm-wpan-utils.c         |  2 ++
 src/tests/test-ip6-config.c               |  1 +
 16 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c
index 41fac157d..5138b6846 100644
--- a/src/devices/nm-device-infiniband.c
+++ b/src/devices/nm-device-infiniband.c
@@ -22,6 +22,7 @@
 
 #include "nm-device-infiniband.h"
 
+#include <linux/if.h>
 #include <linux/if_infiniband.h>
 
 #include "NetworkManagerUtils.h"
diff --git a/src/devices/nm-device-ip-tunnel.c b/src/devices/nm-device-ip-tunnel.c
index 568403c62..1c7e6d51e 100644
--- a/src/devices/nm-device-ip-tunnel.c
+++ b/src/devices/nm-device-ip-tunnel.c
@@ -27,6 +27,7 @@
 #include <linux/if.h>
 #include <linux/ip.h>
 #include <linux/if_tunnel.h>
+#include <linux/ip6_tunnel.h>
 
 #include "nm-device-private.h"
 #include "nm-manager.h"
diff --git a/src/devices/nm-device-macvlan.c b/src/devices/nm-device-macvlan.c
index ff386c82e..2b2121545 100644
--- a/src/devices/nm-device-macvlan.c
+++ b/src/devices/nm-device-macvlan.c
@@ -23,6 +23,7 @@
 #include "nm-device-macvlan.h"
 
 #include <string.h>
+#include <linux/if_link.h>
 
 #include "nm-device-private.h"
 #include "settings/nm-settings.h"
diff --git a/src/devices/nm-device-wpan.c b/src/devices/nm-device-wpan.c
index f910b0b89..05b507d58 100644
--- a/src/devices/nm-device-wpan.c
+++ b/src/devices/nm-device-wpan.c
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#include <linux/if.h>
 
 #include "nm-act-request.h"
 #include "nm-device-private.h"
diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c
index d8f8dd98d..105511974 100644
--- a/src/dhcp/nm-dhcp-dhclient-utils.c
+++ b/src/dhcp/nm-dhcp-dhclient-utils.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <arpa/inet.h>
+#include <net/if.h>
 
 #include "nm-utils/nm-dedup-multi.h"
 
diff --git a/src/ndisc/nm-ndisc.h b/src/ndisc/nm-ndisc.h
index fdc5615f5..290587546 100644
--- a/src/ndisc/nm-ndisc.h
+++ b/src/ndisc/nm-ndisc.h
@@ -23,6 +23,7 @@
 
 #include <stdlib.h>
 #include <netinet/in.h>
+#include <linux/if_addr.h>
 
 #include "nm-setting-ip6-config.h"
 #include "NetworkManagerUtils.h"
diff --git a/src/nm-test-utils-core.h b/src/nm-test-utils-core.h
index 5e89cb39f..dbfe71ada 100644
--- a/src/nm-test-utils-core.h
+++ b/src/nm-test-utils-core.h
@@ -88,8 +88,8 @@ nmtst_platform_ip4_address_full (const char *address, const char *peer_address,
 {
 	NMPlatformIP4Address *addr = nmtst_platform_ip4_address (address, peer_address, plen);
 
-	G_STATIC_ASSERT (IFNAMSIZ == sizeof (addr->label));
-	g_assert (!label || strlen (label) < IFNAMSIZ);
+	G_STATIC_ASSERT (NMP_IFNAMSIZ == sizeof (addr->label));
+	g_assert (!label || strlen (label) < NMP_IFNAMSIZ);
 
 	addr->ifindex = ifindex;
 	addr->addr_source = source;
diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c
index 82f9e2fb2..ef69b391b 100644
--- a/src/platform/nm-fake-platform.c
+++ b/src/platform/nm-fake-platform.c
@@ -26,6 +26,7 @@
 #include <unistd.h>
 #include <netinet/icmp6.h>
 #include <netinet/in.h>
+#include <linux/if.h>
 #include <linux/rtnetlink.h>
 
 #include "nm-utils.h"
diff --git a/src/platform/nm-platform-utils.c b/src/platform/nm-platform-utils.c
index 216b15478..cc43b27fb 100644
--- a/src/platform/nm-platform-utils.c
+++ b/src/platform/nm-platform-utils.c
@@ -29,6 +29,7 @@
 #include <linux/ethtool.h>
 #include <linux/sockios.h>
 #include <linux/mii.h>
+#include <linux/if.h>
 #include <linux/version.h>
 #include <linux/rtnetlink.h>
 #include <fcntl.h>
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
index 7ddcf41ae..52ec4fec6 100644
--- a/src/platform/nm-platform.c
+++ b/src/platform/nm-platform.c
@@ -31,6 +31,7 @@
 #include <netdb.h>
 #include <string.h>
 #include <linux/ip.h>
+#include <linux/if.h>
 #include <linux/if_tun.h>
 #include <linux/if_tunnel.h>
 #include <linux/rtnetlink.h>
diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h
index 11495aff4..f7a23616b 100644
--- a/src/platform/nm-platform.h
+++ b/src/platform/nm-platform.h
@@ -21,12 +21,6 @@
 #ifndef __NETWORKMANAGER_PLATFORM_H__
 #define __NETWORKMANAGER_PLATFORM_H__
 
-#include <netinet/in.h>
-#include <linux/if.h>
-#include <linux/if_addr.h>
-#include <linux/if_link.h>
-#include <linux/ip6_tunnel.h>
-
 #include "nm-dbus-interface.h"
 #include "nm-core-types-internal.h"
 
@@ -53,6 +47,12 @@
 
 /*****************************************************************************/
 
+/* IFNAMSIZ is both defined in <linux/if.h> and <net/if.h>. In the past, these
+ * headers conflicted, so we cannot simply include either of them in a header-file.*/
+#define NMP_IFNAMSIZ 16
+
+/*****************************************************************************/
+
 struct udev_device;
 
 typedef gboolean (*NMPObjectPredicateFunc) (const NMPObject *obj,
@@ -208,7 +208,7 @@ typedef enum {
 
 struct _NMPlatformLink {
 	__NMPlatformObject_COMMON;
-	char name[IFNAMSIZ];
+	char name[NMP_IFNAMSIZ];
 	NMLinkType type;
 
 	/* rtnl_link_get_type(), IFLA_INFO_KIND. */
@@ -355,7 +355,7 @@ struct _NMPlatformIP4Address {
 	 * */
 	in_addr_t peer_address;  /* PTP peer address */
 
-	char label[IFNAMSIZ];
+	char label[NMP_IFNAMSIZ];
 };
 
 /**
@@ -1105,12 +1105,12 @@ const char *nm_platform_error_to_string (NMPlatformError error,
 	((const char *) NULL), -1, (path)
 
 #define NMP_SYSCTL_PATHID_NETDIR_unsafe(dirfd, ifname, path) \
-	nm_sprintf_bufa (NM_STRLEN ("net:/sys/class/net//\0") + IFNAMSIZ + strlen (path), \
+	nm_sprintf_bufa (NM_STRLEN ("net:/sys/class/net//\0") + NMP_IFNAMSIZ + strlen (path), \
 	                 "net:/sys/class/net/%s/%s", (ifname), (path)), \
 	(dirfd), (path)
 
 #define NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, path) \
-	nm_sprintf_bufa (NM_STRLEN ("net:/sys/class/net//"path"/\0") + IFNAMSIZ, \
+	nm_sprintf_bufa (NM_STRLEN ("net:/sys/class/net//"path"/\0") + NMP_IFNAMSIZ, \
 	                 "net:/sys/class/net/%s/%s", (ifname), path), \
 	(dirfd), (""path"")
 
diff --git a/src/platform/nmp-object.c b/src/platform/nmp-object.c
index fdc27440c..f7fa6cb3c 100644
--- a/src/platform/nmp-object.c
+++ b/src/platform/nmp-object.c
@@ -24,6 +24,7 @@
 
 #include <unistd.h>
 #include <linux/rtnetlink.h>
+#include <linux/if.h>
 #include <libudev.h>
 
 #include "nm-utils.h"
diff --git a/src/platform/tests/test-common.h b/src/platform/tests/test-common.h
index 1baadfa11..7e81baeac 100644
--- a/src/platform/tests/test-common.h
+++ b/src/platform/tests/test-common.h
@@ -21,6 +21,9 @@
 #include <syslog.h>
 #include <string.h>
 #include <arpa/inet.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+#include <linux/ip6_tunnel.h>
 
 #include "platform/nm-platform.h"
 #include "platform/nmp-object.h"
diff --git a/src/platform/wifi/nm-wifi-utils-nl80211.c b/src/platform/wifi/nm-wifi-utils-nl80211.c
index b3bb2bb6e..39e3f9718 100644
--- a/src/platform/wifi/nm-wifi-utils-nl80211.c
+++ b/src/platform/wifi/nm-wifi-utils-nl80211.c
@@ -30,6 +30,7 @@
 #include <net/ethernet.h>
 #include <unistd.h>
 #include <linux/nl80211.h>
+#include <linux/if.h>
 
 #include "platform/nm-netlink.h"
 #include "nm-wifi-utils-private.h"
diff --git a/src/platform/wpan/nm-wpan-utils.c b/src/platform/wpan/nm-wpan-utils.c
index 0544539ac..882c4eec2 100644
--- a/src/platform/wpan/nm-wpan-utils.c
+++ b/src/platform/wpan/nm-wpan-utils.c
@@ -21,6 +21,8 @@
 
 #include "nm-wpan-utils.h"
 
+#include <linux/if.h>
+
 #include "platform/linux/nl802154.h"
 #include "platform/nm-netlink.h"
 
diff --git a/src/tests/test-ip6-config.c b/src/tests/test-ip6-config.c
index a03d89b07..5b2550683 100644
--- a/src/tests/test-ip6-config.c
+++ b/src/tests/test-ip6-config.c
@@ -22,6 +22,7 @@
 
 #include <string.h>
 #include <arpa/inet.h>
+#include <linux/if_addr.h>
 
 #include "nm-ip6-config.h"
 
-- 
2.19.1