platform: refactor comparing for all-zero,all-ones MAC address in nmp_utils_ethtool_get_permanent_address()

Don't like the static fields.

Also, don't assert against return values from the ethtool call.
And check that the length is positive.
This commit is contained in:
Thomas Haller 2016-05-20 17:53:58 +02:00
parent d7b4733d3e
commit a4a75b638f

View file

@ -144,8 +144,7 @@ nmp_utils_ethtool_get_permanent_address (const char *ifname,
struct ethtool_perm_addr e;
guint8 _extra_data[NM_UTILS_HWADDR_LEN_MAX + 1];
} edata;
static const guint8 zeros[NM_UTILS_HWADDR_LEN_MAX] = { 0 };
static guint8 ones[NM_UTILS_HWADDR_LEN_MAX] = { 0 };
guint i;
if (!ifname)
return FALSE;
@ -157,18 +156,23 @@ nmp_utils_ethtool_get_permanent_address (const char *ifname,
if (!ethtool_get (ifname, &edata.e))
return FALSE;
g_assert (edata.e.size <= NM_UTILS_HWADDR_LEN_MAX);
/* Some drivers might return a permanent address of all zeros.
* Reject that (rh#1264024) */
if (memcmp (edata.e.data, zeros, edata.e.size) == 0)
if (edata.e.size > NM_UTILS_HWADDR_LEN_MAX)
return FALSE;
if (edata.e.size < 1)
return FALSE;
/* Some drivers return a permanent address of all ones. Reject that too */
if (G_UNLIKELY (ones[0] != 0xFF))
memset (ones, 0xFF, sizeof (ones));
if (memcmp (edata.e.data, ones, edata.e.size) == 0)
if (NM_IN_SET (edata.e.data[0], 0, 0xFF)) {
/* Some drivers might return a permanent address of all zeros.
* Reject that (rh#1264024)
*
* Some drivers return a permanent address of all ones. Reject that too */
for (i = 1; i < edata.e.size; i++) {
if (edata.e.data[0] != edata.e.data[i])
goto not_all_0or1;
}
return FALSE;
}
not_all_0or1:
memcpy (buf, edata.e.data, edata.e.size);
*length = edata.e.size;