summaryrefslogtreecommitdiff
path: root/src/shared/netif-util.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-11-06 10:24:09 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-11-09 08:24:10 +0900
commitb5cc5591faec4117162099c92bc84d1120e6e234 (patch)
tree8840588af08a910a71561c8f6f39d90c61bec2a9 /src/shared/netif-util.c
parent91961fff43d0a3565812e5eeeb237de45242e06a (diff)
downloadsystemd-b5cc5591faec4117162099c92bc84d1120e6e234.tar.gz
netif-util: move several functions from network-util.[ch] to shared/netif-util.[ch]
These functions are not relevant to sd-network, and only used by networkd, networkctl, and udevd.
Diffstat (limited to 'src/shared/netif-util.c')
-rw-r--r--src/shared/netif-util.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/shared/netif-util.c b/src/shared/netif-util.c
new file mode 100644
index 0000000000..fb81460c91
--- /dev/null
+++ b/src/shared/netif-util.c
@@ -0,0 +1,86 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "sd-id128.h"
+
+#include "arphrd-util.h"
+#include "device-util.h"
+#include "netif-util.h"
+#include "siphash24.h"
+#include "sparse-endian.h"
+#include "strv.h"
+
+int link_get_type_string(sd_device *device, unsigned short iftype, char **ret) {
+ const char *t;
+ char *p;
+
+ if (device &&
+ sd_device_get_devtype(device, &t) >= 0 &&
+ !isempty(t)) {
+ p = strdup(t);
+ if (!p)
+ return -ENOMEM;
+
+ *ret = p;
+ return 0;
+ }
+
+ t = arphrd_to_name(iftype);
+ if (!t)
+ return -ENOENT;
+
+ p = strdup(t);
+ if (!p)
+ return -ENOMEM;
+
+ *ret = ascii_strlower(p);
+ return 0;
+}
+
+const char *net_get_name_persistent(sd_device *device) {
+ const char *name, *field;
+
+ assert(device);
+
+ /* fetch some persistent data unique (on this machine) to this device */
+ FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
+ if (sd_device_get_property_value(device, field, &name) >= 0)
+ return name;
+
+ return NULL;
+}
+
+#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
+
+int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *ret) {
+ size_t l, sz = 0;
+ const char *name;
+ int r;
+ uint8_t *v;
+
+ assert(device);
+
+ /* net_get_name_persistent() will return one of the device names based on stable information about
+ * the device. If this is not available, we fall back to using the actual device name. */
+ name = net_get_name_persistent(device);
+ if (!name && use_sysname)
+ (void) sd_device_get_sysname(device, &name);
+ if (!name)
+ return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
+ "No stable identifying information found");
+
+ log_device_debug(device, "Using \"%s\" as stable identifying information", name);
+ l = strlen(name);
+ sz = sizeof(sd_id128_t) + l;
+ v = newa(uint8_t, sz);
+
+ /* Fetch some persistent data unique to this machine */
+ r = sd_id128_get_machine((sd_id128_t*) v);
+ if (r < 0)
+ return r;
+ memcpy(v + sizeof(sd_id128_t), name, l);
+
+ /* Let's hash the machine ID plus the device name. We use
+ * a fixed, but originally randomly created hash key here. */
+ *ret = htole64(siphash24(v, sz, HASH_KEY.bytes));
+ return 0;
+}