]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-network/network-util.c
net-condition: introduce struct NetMatch
[thirdparty/systemd.git] / src / libsystemd / sd-network / network-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-id128.h"
4
5 #include "alloc-util.h"
6 #include "arphrd-list.h"
7 #include "device-util.h"
8 #include "fd-util.h"
9 #include "network-util.h"
10 #include "siphash24.h"
11 #include "sparse-endian.h"
12 #include "string-table.h"
13 #include "strv.h"
14
15 bool network_is_online(void) {
16 _cleanup_free_ char *carrier_state = NULL, *addr_state = NULL;
17 int r;
18
19 r = sd_network_get_carrier_state(&carrier_state);
20 if (r < 0) /* if we don't know anything, we consider the system online */
21 return true;
22
23 r = sd_network_get_address_state(&addr_state);
24 if (r < 0) /* if we don't know anything, we consider the system online */
25 return true;
26
27 if (STR_IN_SET(carrier_state, "degraded-carrier", "carrier") &&
28 STR_IN_SET(addr_state, "routable", "degraded"))
29 return true;
30
31 return false;
32 }
33
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",
44 };
45
46 DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);
47
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",
55 };
56
57 DEFINE_STRING_TABLE_LOOKUP(link_carrier_state, LinkCarrierState);
58
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",
63 };
64
65 DEFINE_STRING_TABLE_LOOKUP(link_address_state, LinkAddressState);
66
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;
70 const char *p;
71
72 assert(str);
73 assert(out);
74
75 p = strchr(str, ':');
76 if (p) {
77 min = strndup(str, p - str);
78
79 if (!isempty(p + 1)) {
80 range.max = link_operstate_from_string(p + 1);
81 if (range.max < 0)
82 return -EINVAL;
83 }
84 } else
85 min = strdup(str);
86
87 if (!min)
88 return -ENOMEM;
89
90 if (!isempty(min)) {
91 range.min = link_operstate_from_string(min);
92 if (range.min < 0)
93 return -EINVAL;
94 }
95
96 /* Fail on empty strings. */
97 if (range.min == _LINK_OPERSTATE_INVALID && range.max == _LINK_OPERSTATE_INVALID)
98 return -EINVAL;
99
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;
104
105 if (range.min > range.max)
106 return -EINVAL;
107
108 *out = range;
109
110 return 0;
111 }
112
113 char *link_get_type_string(sd_device *device, unsigned short iftype) {
114 const char *t;
115 char *p;
116
117 if (device &&
118 sd_device_get_devtype(device, &t) >= 0 &&
119 !isempty(t))
120 return strdup(t);
121
122 t = arphrd_to_name(iftype);
123 if (!t)
124 return NULL;
125
126 p = strdup(t);
127 if (!p)
128 return NULL;
129
130 return ascii_strlower(p);
131 }
132
133 const char *net_get_name_persistent(sd_device *device) {
134 const char *name, *field;
135
136 assert(device);
137
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)
141 return name;
142
143 return NULL;
144 }
145
146 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
147
148 int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result) {
149 size_t l, sz = 0;
150 const char *name;
151 int r;
152 uint8_t *v;
153
154 assert(device);
155
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);
161 if (!name)
162 return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
163 "No stable identifying information found");
164
165 log_device_debug(device, "Using \"%s\" as stable identifying information", name);
166 l = strlen(name);
167 sz = sizeof(sd_id128_t) + l;
168 v = newa(uint8_t, sz);
169
170 /* Fetch some persistent data unique to this machine */
171 r = sd_id128_get_machine((sd_id128_t*) v);
172 if (r < 0)
173 return r;
174 memcpy(v + sizeof(sd_id128_t), name, l);
175
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));
179 return 0;
180 }