From 9bf3b53533cdc9b95c921b71da755401f223f765 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 22 Dec 2013 19:59:12 +0100 Subject: shared: switch our hash table implementation over to SipHash SipHash appears to be the new gold standard for hashing smaller strings for hashtables these days, so let's make use of it. --- src/udev/net/link-config.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'src/udev') diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c index 9b91d9d145..e6618b8cbf 100644 --- a/src/udev/net/link-config.c +++ b/src/udev/net/link-config.c @@ -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 */ -- cgit v1.2.3-54-g00ecf