Debian Codegraph @

src/shared/netif-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if.h>
4 #include <linux/if_arp.h>
5
6 #include "sd-device.h"
7 #include "sd-id128.h"
8
9 #include "alloc-util.h"
10 #include "arphrd-util.h"
11 #include "device-util.h"
12 #include "ether-addr-util.h"
13 #include "hexdecoct.h"
14 #include "log-link.h"
15 #include "memory-util.h"
16 #include "netif-naming-scheme.h"
17 #include "netif-util.h"
18 #include "siphash24.h"
19 #include "sparse-endian.h"
20 #include "string-util.h"
21 #include "strv.h"
22
23 #define SHORTEN_IFNAME_HASH_KEY SD_ID128_MAKE(e1,90,a4,04,a8,ef,4b,51,8c,cc,c3,3a,9f,11,fc,a2)
24
25 bool netif_has_carrier(uint8_t operstate, unsigned flags) {
26 /* see Documentation/networking/operstates.txt in the kernel sources */
27
28 if (operstate == IF_OPER_UP)
29 return true;
30
31 if (operstate != IF_OPER_UNKNOWN)
32 return false;
33
34 /* operstate may not be implemented, so fall back to flags */
35 return FLAGS_SET(flags, IFF_LOWER_UP | IFF_RUNNING) &&
36 !FLAGS_SET(flags, IFF_DORMANT);
37 }
38
39 int net_get_type_string(sd_device *device, uint16_t iftype, char **ret) {
40 const char *t;
41 char *p;
42
43 assert(ret);
44
45 if (device &&
46 sd_device_get_devtype(device, &t) >= 0 &&
47 !isempty(t))
48 return strdup_to(ret, t);
49
50 t = arphrd_to_name(iftype);
51 if (!t)
52 return -ENOENT;
53
54 p = strdup(t);
55 if (!p)
56 return -ENOMEM;
57
58 *ret = ascii_strlower(p);
59 return 0;
60 }
61
62 const char* net_get_persistent_name(sd_device *device) {
63 assert(device);
64
65 /* fetch some persistent data unique (on this machine) to this device */
66 FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
67 const char *name;
68
69 if (sd_device_get_property_value(device, field, &name) >= 0)
70 return name;
71 }
72
73 return NULL;
74 }
75
76 /* Used when generating hardware address by udev, and IPv4LL seed by networkd. */
77 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
78
79 int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *ret) {
80 const char *name;
81
82 assert(device);
83 assert(ret);
84
85 /* net_get_persistent_name() will return one of the device names based on stable information about
86 * the device. If this is not available, we fall back to using the actual device name. */
87 name = net_get_persistent_name(device);
88 if (!name && use_sysname)
89 (void) sd_device_get_sysname(device, &name);
90 if (!name)
91 return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENODATA),
92 "No stable identifying information found");
93
94 log_device_debug(device, "Using \"%s\" as stable identifying information", name);
95
96 return net_get_unique_predictable_data_from_name(name, &HASH_KEY, ret);
97 }
98
99 int net_get_unique_predictable_data_from_name(
100 const char *name,
101 const sd_id128_t *key,
102 uint64_t *ret) {
103
104 size_t l, sz;
105 uint8_t *v;
106 int r;
107
108 assert(name);
109 assert(key);
110 assert(ret);
111
112 l = strlen(name);
113 sz = sizeof(sd_id128_t) + l;
114 v = newa(uint8_t, sz);
115
116 /* Fetch some persistent data unique to this machine */
117 r = sd_id128_get_machine((sd_id128_t*) v);
118 if (r < 0)
119 return r;
120
121 memcpy(v + sizeof(sd_id128_t), name, l);
122
123 /* Let's hash the machine ID plus the device name. We use
124 * a fixed, but originally randomly created hash key here. */
125 *ret = htole64(siphash24(v, sz, key->bytes));
126 return 0;
127 }
128
129 typedef struct Link {
130 const char *ifname;
131 } Link;
132
133 int net_verify_hardware_address(
134 const char *ifname,
135 bool is_static,
136 uint16_t iftype,
137 const struct hw_addr_data *ib_hw_addr, /* current or parent HW address */
138 struct hw_addr_data *new_hw_addr) {
139
140 Link link = { .ifname = ifname };
141
142 assert(new_hw_addr);
143
144 if (new_hw_addr->length == 0)
145 return 0;
146
147 if (new_hw_addr->length != arphrd_to_hw_addr_len(iftype)) {
148 if (is_static)
149 log_link_warning(&link,
150 "Specified MAC address with invalid length (%zu, expected %zu), refusing.",
151 new_hw_addr->length, arphrd_to_hw_addr_len(iftype));
152 return -EINVAL;
153 }
154
155 switch (iftype) {
156 case ARPHRD_ETHER:
157 /* see eth_random_addr() in the kernel */
158
159 if (ether_addr_is_null(&new_hw_addr->ether)) {
160 if (is_static)
161 log_link_warning(&link, "Specified MAC address is null, refusing.");
162 return -EINVAL;
163 }
164
165 if (ether_addr_is_broadcast(&new_hw_addr->ether)) {
166 if (is_static)
167 log_link_warning(&link, "Specified MAC address is broadcast, refusing.");
168 return -EINVAL;
169 }
170
171 if (ether_addr_is_multicast(&new_hw_addr->ether)) {
172 if (is_static)
173 log_link_warning(&link, "Specified MAC address has the multicast bit set, clearing the bit.");
174
175 new_hw_addr->bytes[0] &= 0xfe;
176 }
177
178 if (!is_static && !ether_addr_is_local(&new_hw_addr->ether))
179 /* Adjust local assignment bit when the MAC address is generated randomly. */
180 new_hw_addr->bytes[0] |= 0x02;
181
182 break;
183
184 case ARPHRD_INFINIBAND:
185 /* see ipoib_check_lladdr() in the kernel */
186
187 assert(ib_hw_addr);
188 assert(ib_hw_addr->length == INFINIBAND_ALEN);
189
190 if (is_static &&
191 (!memeqzero(new_hw_addr->bytes, INFINIBAND_ALEN - 8) ||
192 memcmp(new_hw_addr->bytes, ib_hw_addr->bytes, INFINIBAND_ALEN - 8) != 0))
193 log_link_warning(&link, "Only the last 8 bytes of the InifniBand MAC address can be changed, ignoring the first 12 bytes.");
194
195 if (memeqzero(new_hw_addr->bytes + INFINIBAND_ALEN - 8, 8)) {
196 if (is_static)
197 log_link_warning(&link, "The last 8 bytes of the InfiniBand MAC address cannot be null, refusing.");
198 return -EINVAL;
199 }
200
201 memcpy(new_hw_addr->bytes, ib_hw_addr->bytes, INFINIBAND_ALEN - 8);
202 break;
203
204 default:
205 if (is_static)
206 log_link_warning(&link, "Unsupported interface type %s%u to set MAC address, refusing.",
207 strna(arphrd_to_name(iftype)), iftype);
208 return -EINVAL;
209 }
210
211 return 0;
212 }
213
214 int net_generate_mac(
215 const char *machine_name,
216 struct ether_addr *mac,
217 sd_id128_t hash_key,
218 uint64_t idx) {
219
220 uint64_t result;
221 size_t l, sz;
222 uint8_t *v, *i;
223 int r;
224
225 l = strlen(machine_name);
226 sz = sizeof(sd_id128_t) + l;
227 if (idx > 0)
228 sz += sizeof(idx);
229
230 v = newa(uint8_t, sz);
231
232 /* fetch some persistent data unique to the host */
233 r = sd_id128_get_machine((sd_id128_t*) v);
234 if (r < 0)
235 return r;
236
237 /* combine with some data unique (on this host) to this
238 * container instance */
239 i = mempcpy(v + sizeof(sd_id128_t), machine_name, l);
240 if (idx > 0) {
241 idx = htole64(idx);
242 memcpy(i, &idx, sizeof(idx));
243 }
244
245 /* Let's hash the host machine ID plus the container name. We
246 * use a fixed, but originally randomly created hash key here. */
247 result = htole64(siphash24(v, sz, hash_key.bytes));
248
249 assert_cc(ETH_ALEN <= sizeof(result));
250 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
251
252 ether_addr_mark_random(mac);
253
254 return 0;
255 }
256
257 int net_shorten_ifname(char *ifname, bool check_naming_scheme) {
258 char new_ifname[IFNAMSIZ];
259
260 assert(ifname);
261
262 if (strlen(ifname) < IFNAMSIZ) /* Name is short enough */
263 return 0;
264
265 if (!check_naming_scheme || naming_scheme_has(NAMING_NSPAWN_LONG_HASH)) {
266 uint64_t h;
267
268 /* Calculate 64-bit hash value */
269 h = siphash24(ifname, strlen(ifname), SHORTEN_IFNAME_HASH_KEY.bytes);
270
271 /* Set the final four bytes (i.e. 32-bit) to the lower 24bit of the hash, encoded in url-safe base64 */
272 memcpy(new_ifname, ifname, IFNAMSIZ - 5);
273 new_ifname[IFNAMSIZ - 5] = urlsafe_base64char(h >> 18);
274 new_ifname[IFNAMSIZ - 4] = urlsafe_base64char(h >> 12);
275 new_ifname[IFNAMSIZ - 3] = urlsafe_base64char(h >> 6);
276 new_ifname[IFNAMSIZ - 2] = urlsafe_base64char(h);
277 } else
278 /* On old nspawn versions we just truncated the name, provide compatibility */
279 memcpy(new_ifname, ifname, IFNAMSIZ-1);
280
281 new_ifname[IFNAMSIZ - 1] = 0;
282
283 /* Log the incident to make it more discoverable */
284 log_warning("Network interface name '%s' has been changed to '%s' to fit length constraints.", ifname, new_ifname);
285
286 strcpy(ifname, new_ifname);
287 return 1;
288 }