1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-10 01:17:44 +03:00

netif-util: split net_get_unique_predictable_data() into two

This commit is contained in:
Yu Watanabe 2021-11-08 11:01:46 +09:00
parent 043b193f6e
commit 0295b2fd1d
2 changed files with 25 additions and 6 deletions

View 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;
}

View 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);