2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-08-12 16:58:56 +02:00
|
|
|
|
2020-10-28 17:04:08 +01:00
|
|
|
#include "sd-id128.h"
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2020-10-28 16:14:22 +01:00
|
|
|
#include "arphrd-list.h"
|
2020-10-28 17:04:08 +01:00
|
|
|
#include "device-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2014-08-12 16:58:56 +02:00
|
|
|
#include "network-util.h"
|
2020-10-28 17:04:08 +01:00
|
|
|
#include "siphash24.h"
|
|
|
|
#include "sparse-endian.h"
|
2019-03-05 06:39:57 +01:00
|
|
|
#include "string-table.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "strv.h"
|
2014-08-12 16:58:56 +02:00
|
|
|
|
|
|
|
bool network_is_online(void) {
|
2019-06-09 22:36:32 +02:00
|
|
|
_cleanup_free_ char *carrier_state = NULL, *addr_state = NULL;
|
2014-08-12 16:58:56 +02:00
|
|
|
int r;
|
|
|
|
|
2019-06-09 22:36:32 +02:00
|
|
|
r = sd_network_get_carrier_state(&carrier_state);
|
2014-08-12 16:58:56 +02:00
|
|
|
if (r < 0) /* if we don't know anything, we consider the system online */
|
|
|
|
return true;
|
|
|
|
|
2019-06-09 22:36:32 +02:00
|
|
|
r = sd_network_get_address_state(&addr_state);
|
|
|
|
if (r < 0) /* if we don't know anything, we consider the system online */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (STR_IN_SET(carrier_state, "degraded-carrier", "carrier") &&
|
|
|
|
STR_IN_SET(addr_state, "routable", "degraded"))
|
2014-08-12 16:58:56 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-05 06:39:57 +01:00
|
|
|
|
|
|
|
static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
|
2020-01-14 14:08:03 +01:00
|
|
|
[LINK_OPERSTATE_MISSING] = "missing",
|
2019-03-05 06:41:56 +01:00
|
|
|
[LINK_OPERSTATE_OFF] = "off",
|
|
|
|
[LINK_OPERSTATE_NO_CARRIER] = "no-carrier",
|
|
|
|
[LINK_OPERSTATE_DORMANT] = "dormant",
|
|
|
|
[LINK_OPERSTATE_DEGRADED_CARRIER] = "degraded-carrier",
|
|
|
|
[LINK_OPERSTATE_CARRIER] = "carrier",
|
|
|
|
[LINK_OPERSTATE_DEGRADED] = "degraded",
|
|
|
|
[LINK_OPERSTATE_ENSLAVED] = "enslaved",
|
|
|
|
[LINK_OPERSTATE_ROUTABLE] = "routable",
|
2019-03-05 06:39:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);
|
2019-06-16 02:03:25 +02:00
|
|
|
|
|
|
|
static const char* const link_carrier_state_table[_LINK_CARRIER_STATE_MAX] = {
|
|
|
|
[LINK_CARRIER_STATE_OFF] = "off",
|
|
|
|
[LINK_CARRIER_STATE_NO_CARRIER] = "no-carrier",
|
|
|
|
[LINK_CARRIER_STATE_DORMANT] = "dormant",
|
|
|
|
[LINK_CARRIER_STATE_DEGRADED_CARRIER] = "degraded-carrier",
|
|
|
|
[LINK_CARRIER_STATE_CARRIER] = "carrier",
|
|
|
|
[LINK_CARRIER_STATE_ENSLAVED] = "enslaved",
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(link_carrier_state, LinkCarrierState);
|
|
|
|
|
|
|
|
static const char* const link_address_state_table[_LINK_ADDRESS_STATE_MAX] = {
|
|
|
|
[LINK_ADDRESS_STATE_OFF] = "off",
|
|
|
|
[LINK_ADDRESS_STATE_DEGRADED] = "degraded",
|
|
|
|
[LINK_ADDRESS_STATE_ROUTABLE] = "routable",
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_STRING_TABLE_LOOKUP(link_address_state, LinkAddressState);
|
2020-01-09 21:31:50 +01:00
|
|
|
|
|
|
|
int parse_operational_state_range(const char *str, LinkOperationalStateRange *out) {
|
|
|
|
LinkOperationalStateRange range = { _LINK_OPERSTATE_INVALID, _LINK_OPERSTATE_INVALID };
|
|
|
|
_cleanup_free_ const char *min = NULL;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
assert(str);
|
|
|
|
assert(out);
|
|
|
|
|
|
|
|
p = strchr(str, ':');
|
|
|
|
if (p) {
|
|
|
|
min = strndup(str, p - str);
|
|
|
|
|
|
|
|
if (!isempty(p + 1)) {
|
|
|
|
range.max = link_operstate_from_string(p + 1);
|
|
|
|
if (range.max < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
min = strdup(str);
|
|
|
|
|
|
|
|
if (!min)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (!isempty(min)) {
|
|
|
|
range.min = link_operstate_from_string(min);
|
|
|
|
if (range.min < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail on empty strings. */
|
|
|
|
if (range.min == _LINK_OPERSTATE_INVALID && range.max == _LINK_OPERSTATE_INVALID)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (range.min == _LINK_OPERSTATE_INVALID)
|
2020-01-14 14:08:03 +01:00
|
|
|
range.min = LINK_OPERSTATE_MISSING;
|
2020-01-09 21:31:50 +01:00
|
|
|
if (range.max == _LINK_OPERSTATE_INVALID)
|
|
|
|
range.max = LINK_OPERSTATE_ROUTABLE;
|
|
|
|
|
|
|
|
if (range.min > range.max)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*out = range;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-28 16:14:22 +01:00
|
|
|
|
|
|
|
char *link_get_type_string(sd_device *device, unsigned short iftype) {
|
|
|
|
const char *t;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (device &&
|
|
|
|
sd_device_get_devtype(device, &t) >= 0 &&
|
|
|
|
!isempty(t))
|
|
|
|
return strdup(t);
|
|
|
|
|
|
|
|
t = arphrd_to_name(iftype);
|
|
|
|
if (!t)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
p = strdup(t);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return ascii_strlower(p);
|
|
|
|
}
|
2020-10-28 17:04:08 +01:00
|
|
|
|
|
|
|
const char *net_get_name_persistent(sd_device *device) {
|
|
|
|
const char *name, *field;
|
|
|
|
|
|
|
|
assert(device);
|
|
|
|
|
|
|
|
/* fetch some persistent data unique (on this machine) to this device */
|
|
|
|
FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
|
|
|
|
if (sd_device_get_property_value(device, field, &name) >= 0)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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 *result) {
|
|
|
|
size_t l, sz = 0;
|
|
|
|
const char *name;
|
|
|
|
int r;
|
|
|
|
uint8_t *v;
|
|
|
|
|
|
|
|
assert(device);
|
|
|
|
|
|
|
|
/* net_get_name_persistent() 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. */
|
|
|
|
name = net_get_name_persistent(device);
|
|
|
|
if (!name && use_sysname)
|
|
|
|
(void) sd_device_get_sysname(device, &name);
|
|
|
|
if (!name)
|
|
|
|
return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
|
|
|
|
"No stable identifying information found");
|
|
|
|
|
|
|
|
log_device_debug(device, "Using \"%s\" as stable identifying information", name);
|
|
|
|
l = strlen(name);
|
|
|
|
sz = sizeof(sd_id128_t) + l;
|
|
|
|
v = newa(uint8_t, sz);
|
|
|
|
|
|
|
|
/* Fetch some persistent data unique to this machine */
|
|
|
|
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. */
|
|
|
|
*result = htole64(siphash24(v, sz, HASH_KEY.bytes));
|
|
|
|
return 0;
|
|
|
|
}
|