chiark / gitweb /
bus: fix hello ioctl buffer size calculation
[elogind.git] / src / udev / net / link-config.c
index 37918d35e3e07cda73ad539a82363b76444ec0ba..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);
@@ -149,9 +150,12 @@ void link_config_ctx_free(link_config_ctx *ctx) {
 
 static int load_link(link_config_ctx *ctx, const char *filename) {
         link_config *link;
-        FILE *file;
+        _cleanup_fclose_ FILE *file;
         int r;
 
+        assert(ctx);
+        assert(filename);
+
         file = fopen(filename, "re");
         if (!file) {
                 if (errno == ENOENT)
@@ -202,7 +206,7 @@ static bool enable_name_policy(void) {
                 return true;
 
         FOREACH_WORD_QUOTED(w, l, line, state)
-                if (l == sizeof("net.ifnames=0") - 1 && strneq(w, "net.ifnames=0", l))
+                if (strneq(w, "net.ifnames=0", l))
                         return false;
 
         return true;
@@ -298,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");
@@ -320,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 */