]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
netif-util: split net_get_unique_predictable_data() into two 21272/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 8 Nov 2021 02:01:46 +0000 (11:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 8 Nov 2021 23:24:10 +0000 (08:24 +0900)
src/shared/netif-util.c
src/shared/netif-util.h

index b26a127c38501eb68444bbab03ccecf429958299..6500841e7ea96fb850559f1e6804d75590064915 100644 (file)
@@ -1,7 +1,5 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
-#include "sd-id128.h"
-
 #include "arphrd-util.h"
 #include "device-util.h"
 #include "netif-util.h"
@@ -49,15 +47,14 @@ const char *net_get_persistent_name(sd_device *device) {
         return NULL;
 }
 
+/* Used when generating hardware address by udev, and IPv4LL seed by networkd. */
 #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);
+        assert(ret);
 
         /* net_get_persistent_name() 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. */
@@ -69,6 +66,25 @@ int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_
                                               "No stable identifying information found");
 
         log_device_debug(device, "Using \"%s\" as stable identifying information", name);
+
+        return net_get_unique_predictable_data_from_name(name, NULL, ret);
+}
+
+int net_get_unique_predictable_data_from_name(
+                const char *name,
+                const sd_id128_t *key,
+                uint64_t *ret) {
+
+        size_t l, sz;
+        uint8_t *v;
+        int r;
+
+        assert(name);
+        assert(ret);
+
+        if (!key)
+                key = &HASH_KEY;
+
         l = strlen(name);
         sz = sizeof(sd_id128_t) + l;
         v = newa(uint8_t, sz);
@@ -77,10 +93,11 @@ int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_
         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));
+        *ret = htole64(siphash24(v, sz, key->bytes));
         return 0;
 }
index d96a64b9344c0266d8dc32feb918218238bb935b..f239f042b58d9219f08e0b8d5ecf30846b2c22b2 100644 (file)
@@ -5,7 +5,9 @@
 #include <stdbool.h>
 
 #include "sd-device.h"
+#include "sd-id128.h"
 
 int net_get_type_string(sd_device *device, uint16_t iftype, char **ret);
 const char *net_get_persistent_name(sd_device *device);
 int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *ret);
+int net_get_unique_predictable_data_from_name(const char *name, const sd_id128_t *key, uint64_t *ret);