chiark / gitweb /
shared: switch our hash table implementation over to SipHash
[elogind.git] / src / udev / net / link-config.c
index 9b91d9d145884f98b778467de76c97d66f20a248..e6618b8cbfc99d742c463c9739eaca8074e41b35 100644 (file)
@@ -39,6 +39,7 @@
 #include "hashmap.h"
 #include "rtnl-util.h"
 #include "net-util.h"
+#include "siphash24.h"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
@@ -301,17 +302,18 @@ static bool mac_is_permanent(struct udev_device *device) {
         return type == 0;
 }
 
+#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
+
 static int get_mac(struct udev_device *device, bool want_random, struct ether_addr *mac) {
-        unsigned int seed;
-        int r, i;
+        int r;
 
         if (want_random)
-                seed = random_u();
+                random_bytes(mac->ether_addr_octet, ETH_ALEN);
         else {
                 const char *name;
-                sd_id128_t machine;
-                char machineid_buf[33];
-                const char *seed_str;
+                uint8_t result[8];
+                size_t l, sz;
+                uint8_t *v;
 
                 /* fetch some persistent data unique (on this machine) to this device */
                 name = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
@@ -323,22 +325,24 @@ static int get_mac(struct udev_device *device, bool want_random, struct ether_ad
                                         return -ENOENT;
                         }
                 }
+
+                l = strlen(name);
+                sz = sizeof(sd_id128_t) + l;
+                v = alloca(sz);
+
                 /* fetch some persistent data unique to this machine */
-                r = sd_id128_get_machine(&machine);
+                r = sd_id128_get_machine((sd_id128_t*) v);
                 if (r < 0)
                         return r;
+                memcpy(v + sizeof(sd_id128_t), name, l);
 
-                /* combine the data */
-                seed_str = strappenda(name, sd_id128_to_string(machine, machineid_buf));
-
-                /* hash to get seed */
-                seed = string_hash_func(seed_str);
-        }
-
-        srandom(seed);
+                /* Let's hash the machine ID plus the device name. We
+                 * use a fixed, but originally randomly created hash
+                 * key here. */
+                siphash24(result, v, sz, HASH_KEY.bytes);
 
-        for (i = 0; i < ETH_ALEN; i++) {
-                mac->ether_addr_octet[i] = random();
+                assert_cc(ETH_ALEN <= sizeof(result));
+                memcpy(mac->ether_addr_octet, result, ETH_ALEN);
         }
 
         /* see eth_random_addr in the kernel */