1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "arphrd-list.h"
7 #include "device-util.h"
9 #include "network-util.h"
10 #include "siphash24.h"
11 #include "sparse-endian.h"
12 #include "string-table.h"
15 bool network_is_online(void) {
16 _cleanup_free_
char *carrier_state
= NULL
, *addr_state
= NULL
;
19 r
= sd_network_get_carrier_state(&carrier_state
);
20 if (r
< 0) /* if we don't know anything, we consider the system online */
23 r
= sd_network_get_address_state(&addr_state
);
24 if (r
< 0) /* if we don't know anything, we consider the system online */
27 if (STR_IN_SET(carrier_state
, "degraded-carrier", "carrier") &&
28 STR_IN_SET(addr_state
, "routable", "degraded"))
34 static const char* const link_operstate_table
[_LINK_OPERSTATE_MAX
] = {
35 [LINK_OPERSTATE_MISSING
] = "missing",
36 [LINK_OPERSTATE_OFF
] = "off",
37 [LINK_OPERSTATE_NO_CARRIER
] = "no-carrier",
38 [LINK_OPERSTATE_DORMANT
] = "dormant",
39 [LINK_OPERSTATE_DEGRADED_CARRIER
] = "degraded-carrier",
40 [LINK_OPERSTATE_CARRIER
] = "carrier",
41 [LINK_OPERSTATE_DEGRADED
] = "degraded",
42 [LINK_OPERSTATE_ENSLAVED
] = "enslaved",
43 [LINK_OPERSTATE_ROUTABLE
] = "routable",
46 DEFINE_STRING_TABLE_LOOKUP(link_operstate
, LinkOperationalState
);
48 static const char* const link_carrier_state_table
[_LINK_CARRIER_STATE_MAX
] = {
49 [LINK_CARRIER_STATE_OFF
] = "off",
50 [LINK_CARRIER_STATE_NO_CARRIER
] = "no-carrier",
51 [LINK_CARRIER_STATE_DORMANT
] = "dormant",
52 [LINK_CARRIER_STATE_DEGRADED_CARRIER
] = "degraded-carrier",
53 [LINK_CARRIER_STATE_CARRIER
] = "carrier",
54 [LINK_CARRIER_STATE_ENSLAVED
] = "enslaved",
57 DEFINE_STRING_TABLE_LOOKUP(link_carrier_state
, LinkCarrierState
);
59 static const char* const link_address_state_table
[_LINK_ADDRESS_STATE_MAX
] = {
60 [LINK_ADDRESS_STATE_OFF
] = "off",
61 [LINK_ADDRESS_STATE_DEGRADED
] = "degraded",
62 [LINK_ADDRESS_STATE_ROUTABLE
] = "routable",
65 DEFINE_STRING_TABLE_LOOKUP(link_address_state
, LinkAddressState
);
67 int parse_operational_state_range(const char *str
, LinkOperationalStateRange
*out
) {
68 LinkOperationalStateRange range
= { _LINK_OPERSTATE_INVALID
, _LINK_OPERSTATE_INVALID
};
69 _cleanup_free_
const char *min
= NULL
;
77 min
= strndup(str
, p
- str
);
79 if (!isempty(p
+ 1)) {
80 range
.max
= link_operstate_from_string(p
+ 1);
91 range
.min
= link_operstate_from_string(min
);
96 /* Fail on empty strings. */
97 if (range
.min
== _LINK_OPERSTATE_INVALID
&& range
.max
== _LINK_OPERSTATE_INVALID
)
100 if (range
.min
== _LINK_OPERSTATE_INVALID
)
101 range
.min
= LINK_OPERSTATE_MISSING
;
102 if (range
.max
== _LINK_OPERSTATE_INVALID
)
103 range
.max
= LINK_OPERSTATE_ROUTABLE
;
105 if (range
.min
> range
.max
)
113 char *link_get_type_string(sd_device
*device
, unsigned short iftype
) {
118 sd_device_get_devtype(device
, &t
) >= 0 &&
122 t
= arphrd_to_name(iftype
);
130 return ascii_strlower(p
);
133 const char *net_get_name_persistent(sd_device
*device
) {
134 const char *name
, *field
;
138 /* fetch some persistent data unique (on this machine) to this device */
139 FOREACH_STRING(field
, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC")
140 if (sd_device_get_property_value(device
, field
, &name
) >= 0)
146 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
148 int net_get_unique_predictable_data(sd_device
*device
, bool use_sysname
, uint64_t *result
) {
156 /* net_get_name_persistent() will return one of the device names based on stable information about
157 * the device. If this is not available, we fall back to using the actual device name. */
158 name
= net_get_name_persistent(device
);
159 if (!name
&& use_sysname
)
160 (void) sd_device_get_sysname(device
, &name
);
162 return log_device_debug_errno(device
, SYNTHETIC_ERRNO(ENODATA
),
163 "No stable identifying information found");
165 log_device_debug(device
, "Using \"%s\" as stable identifying information", name
);
167 sz
= sizeof(sd_id128_t
) + l
;
168 v
= newa(uint8_t, sz
);
170 /* Fetch some persistent data unique to this machine */
171 r
= sd_id128_get_machine((sd_id128_t
*) v
);
174 memcpy(v
+ sizeof(sd_id128_t
), name
, l
);
176 /* Let's hash the machine ID plus the device name. We use
177 * a fixed, but originally randomly created hash key here. */
178 *result
= htole64(siphash24(v
, sz
, HASH_KEY
.bytes
));