ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
b7dd4d
From 033998c21f01e7b7d91e4aa51a358f8016f3740a Mon Sep 17 00:00:00 2001
b7dd4d
From: Viktor Mihajlovski <mihajlov@linux.ibm.com>
b7dd4d
Date: Tue, 27 Apr 2021 15:25:16 +0200
b7dd4d
Subject: [PATCH] udev: allow onboard index up to 65535
b7dd4d
b7dd4d
The maximum allowed value of the sysfs device index entry was limited to
b7dd4d
16383 (2^14-1) to avoid the generation of unreasonable onboard interface
b7dd4d
names.
b7dd4d
For s390 the index can assume a value of up to 65535 (2^16-1) which is
b7dd4d
now allowed depending on the new naming flag NAMING_16BIT_INDEX.
b7dd4d
Larger index values are considered unreasonable and remain to be
b7dd4d
ignored.
b7dd4d
b7dd4d
Related: #1939914
b7dd4d
b7dd4d
(cherry picked from commit 5a7eb46c0206411d380543021291b4bca0b6f59f)
b7dd4d
---
b7dd4d
 man/systemd.net-naming-scheme.xml |  7 ++++++-
b7dd4d
 src/udev/udev-builtin-net_id.c    | 22 +++++++++++++++-------
b7dd4d
 2 files changed, 21 insertions(+), 8 deletions(-)
b7dd4d
b7dd4d
diff --git a/man/systemd.net-naming-scheme.xml b/man/systemd.net-naming-scheme.xml
b7dd4d
index e42c93eaad..a567483995 100644
b7dd4d
--- a/man/systemd.net-naming-scheme.xml
b7dd4d
+++ b/man/systemd.net-naming-scheme.xml
b7dd4d
@@ -320,7 +320,12 @@
b7dd4d
           of the <filename>function_id</filename> device attribute. This attribute is now used to build the
b7dd4d
           <varname>ID_NET_NAME_SLOT</varname>. Before that, all slot names were parsed as decimal
b7dd4d
           numbers, which could either result in an incorrect value of the <varname>ID_NET_NAME_SLOT</varname>
b7dd4d
-          property or none at all.</para></listitem>
b7dd4d
+          property or none at all.</para>
b7dd4d
+
b7dd4d
+          <para>Some firmware and hypervisor implementations report unreasonable high numbers for the onboard
b7dd4d
+          index. To prevent the generation of bogus onbard interface names, index numbers greater than 16381
b7dd4d
+          (2^14-1) were ignored. For s390 PCI devices index values up to 65535 (2^16-1) are valid. To account
b7dd4d
+          for that, the limit is increased to now 65535.</para></listitem>
b7dd4d
         </varlistentry>
b7dd4d
 
b7dd4d
         <para>Note that <constant>latest</constant> may be used to denote the latest scheme known to this
b7dd4d
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
b7dd4d
index ba7638fcb8..df84acf27c 100644
b7dd4d
--- a/src/udev/udev-builtin-net_id.c
b7dd4d
+++ b/src/udev/udev-builtin-net_id.c
b7dd4d
@@ -104,7 +104,8 @@
b7dd4d
 #include "udev.h"
b7dd4d
 #include "udev-util.h"
b7dd4d
 
b7dd4d
-#define ONBOARD_INDEX_MAX (16*1024-1)
b7dd4d
+#define ONBOARD_14BIT_INDEX_MAX ((1U << 14) - 1)
b7dd4d
+#define ONBOARD_16BIT_INDEX_MAX ((1U << 16) - 1)
b7dd4d
 
b7dd4d
 /* So here's the deal: net_id is supposed to be an excercise in providing stable names for network devices. However, we
b7dd4d
  * also want to keep updating the naming scheme used in future versions of net_id. These two goals of course are
b7dd4d
@@ -127,6 +128,7 @@ typedef enum NamingSchemeFlags {
b7dd4d
         NAMING_NPAR_ARI        = 1 << 1, /* Use NPAR "ARI", see 6bc04997b6eab35d1cb9fa73889892702c27be09 */
b7dd4d
         NAMING_BRIDGE_NO_SLOT  = 1 << 9, /* Don't use PCI hotplug slot information if the corresponding device is a PCI bridge */
b7dd4d
         NAMING_SLOT_FUNCTION_ID = 1 << 10, /* Use function_id if present to identify PCI hotplug slots */
b7dd4d
+        NAMING_16BIT_INDEX      = 1 << 11, /* Allow full 16-bit for the onboard index */
b7dd4d
 
b7dd4d
         /* And now the masks that combine the features above */
b7dd4d
         NAMING_V238 = 0,
b7dd4d
@@ -138,7 +140,7 @@ typedef enum NamingSchemeFlags {
b7dd4d
         NAMING_RHEL_8_4 = NAMING_V239|NAMING_BRIDGE_NO_SLOT,
b7dd4d
         NAMING_RHEL_8_5 = NAMING_RHEL_8_4,
b7dd4d
         NAMING_RHEL_8_6 = NAMING_RHEL_8_4,
b7dd4d
-        NAMING_RHEL_8_7 = NAMING_RHEL_8_4|NAMING_SLOT_FUNCTION_ID,
b7dd4d
+        NAMING_RHEL_8_7 = NAMING_RHEL_8_4|NAMING_SLOT_FUNCTION_ID|NAMING_16BIT_INDEX,
b7dd4d
 
b7dd4d
         _NAMING_SCHEME_FLAGS_INVALID = -1,
b7dd4d
 } NamingSchemeFlags;
b7dd4d
@@ -326,6 +328,16 @@ out_unref:
b7dd4d
         return r;
b7dd4d
 }
b7dd4d
 
b7dd4d
+static bool is_valid_onboard_index(unsigned long idx) {
b7dd4d
+        /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to
b7dd4d
+         * report for example). Let's define a cut-off where we don't consider the index reliable anymore. We
b7dd4d
+         * pick some arbitrary cut-off, which is somewhere beyond the realistic number of physical network
b7dd4d
+         * interface a system might have. Ideally the kernel would already filter this crap for us, but it
b7dd4d
+         * doesn't currently. The initial cut-off value (2^14-1) was too conservative for s390 PCI which
b7dd4d
+         * allows for index values up 2^16-1 which is now enabled with the NAMING_16BIT_INDEX naming flag. */
b7dd4d
+        return idx <= (naming_scheme_has(NAMING_16BIT_INDEX) ? ONBOARD_16BIT_INDEX_MAX : ONBOARD_14BIT_INDEX_MAX);
b7dd4d
+}
b7dd4d
+
b7dd4d
 /* retrieve on-board index number and label from firmware */
b7dd4d
 static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
b7dd4d
         unsigned dev_port = 0;
b7dd4d
@@ -346,11 +358,7 @@ static int dev_pci_onboard(struct udev_device *dev, struct netnames *names) {
b7dd4d
         if (idx <= 0)
b7dd4d
                 return -EINVAL;
b7dd4d
 
b7dd4d
-        /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
b7dd4d
-         * example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
b7dd4d
-         * cut-off, which is somewhere beyond the realistic number of physical network interface a system might
b7dd4d
-         * have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
b7dd4d
-        if (idx > ONBOARD_INDEX_MAX)
b7dd4d
+        if (!is_valid_onboard_index(idx))
b7dd4d
                 return -ENOENT;
b7dd4d
 
b7dd4d
         /* kernel provided port index for multiple ports on a single PCI function */