]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/net/link-config.c
ethtool: move function
[thirdparty/systemd.git] / src / udev / net / link-config.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
af6f0d42 2
01234e1f 3#include <linux/netdevice.h>
43b3a5ef 4#include <netinet/ether.h>
a0b191b7 5#include <unistd.h>
43b3a5ef 6
e5eadf53 7#include "sd-device.h"
07630cea 8#include "sd-netlink.h"
af6f0d42 9
b5efdb8a 10#include "alloc-util.h"
07630cea
LP
11#include "conf-files.h"
12#include "conf-parser.h"
dc0d4078 13#include "def.h"
e0e789c1 14#include "device-private.h"
b220632c 15#include "device-util.h"
a5010333 16#include "ethtool-util.h"
3ffd4af2 17#include "fd-util.h"
07630cea 18#include "link-config.h"
af6f0d42 19#include "log.h"
0a970718 20#include "memory-util.h"
7e19cc54 21#include "net-condition.h"
b355d0c9 22#include "netif-naming-scheme.h"
1c4baffc 23#include "netlink-util.h"
dc7f6c9b 24#include "network-util.h"
6bedfcbb 25#include "parse-util.h"
b0c82192 26#include "path-lookup.h"
07630cea 27#include "path-util.h"
4e731273 28#include "proc-cmdline.h"
3df3e884 29#include "random-util.h"
8fcde012 30#include "stat-util.h"
8b43440b 31#include "string-table.h"
07630cea
LP
32#include "string-util.h"
33#include "strv.h"
fc27088a 34#include "utf8.h"
af6f0d42 35
afca7ac1 36struct LinkConfigContext {
ce01c07f 37 LIST_HEAD(LinkConfig, links);
a5010333 38 int ethtool_fd;
f6194225 39 bool enable_name_policy;
1c4baffc 40 sd_netlink *rtnl;
dc0d4078 41 usec_t network_dirs_ts_usec;
af6f0d42
TG
42};
43
ce01c07f 44static LinkConfig* link_config_free(LinkConfig *link) {
9a4b012e 45 if (!link)
75db809a 46 return NULL;
5b9d4dc0 47
9a4b012e
TG
48 free(link->filename);
49
5722fb89 50 net_match_clear(&link->match);
c4f58dea 51 condition_free_list(link->conditions);
9a4b012e
TG
52
53 free(link->description);
54 free(link->mac);
55 free(link->name_policy);
56 free(link->name);
a5053a15 57 strv_free(link->alternative_names);
ef1d2c07 58 free(link->alternative_names_policy);
9a4b012e
TG
59 free(link->alias);
60
75db809a 61 return mfree(link);
af6f0d42
TG
62}
63
ce01c07f 64DEFINE_TRIVIAL_CLEANUP_FUNC(LinkConfig*, link_config_free);
9a4b012e 65
afca7ac1 66static void link_configs_free(LinkConfigContext *ctx) {
ce01c07f 67 LinkConfig *link, *link_next;
af6f0d42
TG
68
69 if (!ctx)
70 return;
71
9a4b012e
TG
72 LIST_FOREACH_SAFE(links, link, link_next, ctx->links)
73 link_config_free(link);
af6f0d42
TG
74}
75
afca7ac1 76LinkConfigContext *link_config_ctx_free(LinkConfigContext *ctx) {
af6f0d42 77 if (!ctx)
75db809a 78 return NULL;
af6f0d42 79
03e334a1 80 safe_close(ctx->ethtool_fd);
1c4baffc 81 sd_netlink_unref(ctx->rtnl);
af6f0d42 82 link_configs_free(ctx);
75db809a 83 return mfree(ctx);
af6f0d42
TG
84}
85
afca7ac1
YW
86int link_config_ctx_new(LinkConfigContext **ret) {
87 _cleanup_(link_config_ctx_freep) LinkConfigContext *ctx = NULL;
9a4b012e
TG
88
89 if (!ret)
90 return -EINVAL;
91
afca7ac1 92 ctx = new(LinkConfigContext, 1);
9a4b012e
TG
93 if (!ctx)
94 return -ENOMEM;
95
afca7ac1
YW
96 *ctx = (LinkConfigContext) {
97 .ethtool_fd = -1,
98 .enable_name_policy = true,
99 };
9a4b012e 100
1cc6c93a 101 *ret = TAKE_PTR(ctx);
9a4b012e
TG
102
103 return 0;
104}
105
afca7ac1 106int link_load_one(LinkConfigContext *ctx, const char *filename) {
ce01c07f 107 _cleanup_(link_config_freep) LinkConfig *link = NULL;
6cdab9f1 108 _cleanup_free_ char *name = NULL;
e406e8a2 109 const char *dropin_dirname;
79a60834 110 size_t i;
af6f0d42
TG
111 int r;
112
187dc6e5
TA
113 assert(ctx);
114 assert(filename);
115
e8e2788d
YW
116 r = null_or_empty_path(filename);
117 if (r == -ENOENT)
118 return 0;
119 if (r < 0)
120 return r;
121 if (r > 0) {
ed88bcfb
ZJS
122 log_debug("Skipping empty file: %s", filename);
123 return 0;
124 }
125
6cdab9f1
YW
126 name = strdup(filename);
127 if (!name)
128 return -ENOMEM;
129
ce01c07f 130 link = new(LinkConfig, 1);
ecb08ec6 131 if (!link)
e8a42907 132 return -ENOMEM;
af6f0d42 133
ce01c07f 134 *link = (LinkConfig) {
6cdab9f1 135 .filename = TAKE_PTR(name),
54ed9f88 136 .mac_address_policy = _MAC_ADDRESS_POLICY_INVALID,
c50404ae 137 .wol = UINT32_MAX, /* UINT32_MAX means do not change WOL setting. */
6cdab9f1
YW
138 .duplex = _DUP_INVALID,
139 .port = _NET_DEV_PORT_INVALID,
140 .autonegotiation = -1,
a34811e4
YW
141 .rx_flow_control = -1,
142 .tx_flow_control = -1,
143 .autoneg_flow_control = -1,
ef4a91a7 144 .txqueuelen = UINT32_MAX,
6cdab9f1 145 };
5fde13d7 146
79a60834 147 for (i = 0; i < ELEMENTSOF(link->features); i++)
cc2ff878 148 link->features[i] = -1;
50725d10 149
e406e8a2
YW
150 dropin_dirname = strjoina(basename(filename), ".d");
151 r = config_parse_many(
152 STRV_MAKE_CONST(filename),
153 (const char* const*) CONF_PATHS_STRV("systemd/network"),
154 dropin_dirname,
155 "Match\0Link\0",
156 config_item_perf_lookup, link_config_gperf_lookup,
157 CONFIG_PARSE_WARN, link, NULL);
36f822c4 158 if (r < 0)
ecb08ec6 159 return r;
af6f0d42 160
5722fb89 161 if (net_match_is_empty(&link->match) && !link->conditions) {
dade7349
ZJS
162 log_warning("%s: No valid settings found in the [Match] section, ignoring file. "
163 "To match all interfaces, add OriginalName=* in the [Match] section.",
84ea567e 164 filename);
dade7349
ZJS
165 return 0;
166 }
84ea567e 167
a0b191b7 168 if (!condition_test_list(link->conditions, environ, NULL, NULL, NULL)) {
176d9c0e
YW
169 log_debug("%s: Conditions do not match the system environment, skipping.", filename);
170 return 0;
171 }
172
a7a12bf4
YW
173 if (IN_SET(link->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM) && link->mac) {
174 log_warning("%s: MACAddress= in [Link] section will be ignored when MACAddressPolicy= "
175 "is set to \"persistent\" or \"random\".",
176 filename);
177 link->mac = mfree(link->mac);
178 }
179
6cdab9f1 180 log_debug("Parsed configuration file %s", filename);
af6f0d42 181
4b4a6c9b 182 LIST_PREPEND(links, ctx->links, TAKE_PTR(link));
af6f0d42 183 return 0;
af6f0d42
TG
184}
185
f6194225 186static bool enable_name_policy(void) {
1d84ad94 187 bool b;
f6194225 188
1d84ad94 189 return proc_cmdline_get_bool("net.ifnames", &b) <= 0 || b;
f6194225
TG
190}
191
015b097c 192static int link_unsigned_attribute(sd_device *device, const char *attr, unsigned *type) {
0b189e8f
ZJS
193 const char *s;
194 int r;
195
015b097c 196 r = sd_device_get_sysattr_value(device, attr, &s);
0b189e8f 197 if (r < 0)
015b097c 198 return log_device_debug_errno(device, r, "Failed to query %s: %m", attr);
0b189e8f
ZJS
199
200 r = safe_atou(s, type);
201 if (r < 0)
015b097c 202 return log_device_warning_errno(device, r, "Failed to parse %s \"%s\": %m", attr, s);
0b189e8f 203
015b097c 204 log_device_debug(device, "Device has %s=%u", attr, *type);
0b189e8f
ZJS
205 return 0;
206}
207
afca7ac1 208int link_config_load(LinkConfigContext *ctx) {
b9c54c46 209 _cleanup_strv_free_ char **files = NULL;
edf029b7 210 char **f;
a39f92d3 211 int r;
af6f0d42
TG
212
213 link_configs_free(ctx);
214
f6194225
TG
215 if (!enable_name_policy()) {
216 ctx->enable_name_policy = false;
3f85ef0f 217 log_info("Network interface NamePolicy= disabled on kernel command line, ignoring.");
f6194225
TG
218 }
219
97f2d76d 220 /* update timestamp */
dc0d4078 221 paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, true);
af6f0d42 222
dc0d4078 223 r = conf_files_list_strv(&files, ".link", NULL, 0, NETWORK_DIRS);
f647962d
MS
224 if (r < 0)
225 return log_error_errno(r, "failed to enumerate link files: %m");
af6f0d42
TG
226
227 STRV_FOREACH_BACKWARDS(f, files) {
a378400b 228 r = link_load_one(ctx, *f);
af6f0d42 229 if (r < 0)
e8a42907 230 log_error_errno(r, "Failed to load %s, ignoring: %m", *f);
af6f0d42
TG
231 }
232
233 return 0;
234}
235
afca7ac1 236bool link_config_should_reload(LinkConfigContext *ctx) {
dc0d4078 237 return paths_check_timestamp(NETWORK_DIRS, &ctx->network_dirs_ts_usec, false);
af6f0d42
TG
238}
239
ce01c07f 240int link_config_get(LinkConfigContext *ctx, sd_device *device, LinkConfig **ret) {
f5767302 241 unsigned name_assign_type = NET_NAME_UNKNOWN;
4bb7cc82 242 struct ether_addr permanent_mac = {};
f25e642b 243 unsigned short iftype;
ce01c07f 244 LinkConfig *link;
4bb7cc82 245 const char *name;
70f32a26 246 unsigned flags;
ef62949a 247 int ifindex, r;
af6f0d42 248
3b64e4d4
TG
249 assert(ctx);
250 assert(device);
251 assert(ret);
252
4bb7cc82
YW
253 r = sd_device_get_sysname(device, &name);
254 if (r < 0)
255 return r;
256
ef62949a
YW
257 r = sd_device_get_ifindex(device, &ifindex);
258 if (r < 0)
259 return r;
260
70f32a26 261 r = rtnl_get_link_info(&ctx->rtnl, ifindex, &iftype, &flags);
ef62949a
YW
262 if (r < 0)
263 return r;
264
70f32a26
YW
265 /* Do not configure loopback interfaces by .link files. */
266 if (flags & IFF_LOOPBACK)
267 return -ENOENT;
268
4bb7cc82
YW
269 r = ethtool_get_permanent_macaddr(&ctx->ethtool_fd, name, &permanent_mac);
270 if (r < 0)
271 log_device_debug_errno(device, r, "Failed to get permanent MAC address, ignoring: %m");
272
f5767302
YW
273 (void) link_unsigned_attribute(device, "name_assign_type", &name_assign_type);
274
af6f0d42 275 LIST_FOREACH(links, link, ctx->links) {
1a3caa49
YW
276 r = net_match_config(&link->match, device, NULL, &permanent_mac, NULL, iftype, NULL, NULL, 0, NULL, NULL);
277 if (r < 0)
278 return r;
279 if (r == 0)
280 continue;
281
282 if (link->match.ifname && !strv_contains(link->match.ifname, "*") && name_assign_type == NET_NAME_ENUM)
283 log_device_warning(device, "Config file %s is applied to device based on potentially unpredictable interface name.",
284 link->filename);
285 else
286 log_device_debug(device, "Config file %s is applied", link->filename);
287
288 *ret = link;
289 return 0;
af6f0d42
TG
290 }
291
292 return -ENOENT;
293}
294
ce01c07f 295static int link_config_apply_ethtool_settings(int *ethtool_fd, const LinkConfig *config, sd_device *device) {
2e17fed5
YW
296 const char *name;
297 int r;
298
299 assert(ethtool_fd);
300 assert(config);
301 assert(device);
302
303 r = sd_device_get_sysname(device, &name);
304 if (r < 0)
305 return log_device_error_errno(device, r, "Failed to get sysname: %m");
306
307 r = ethtool_set_glinksettings(ethtool_fd, name,
308 config->autonegotiation, config->advertise,
309 config->speed, config->duplex, config->port);
310 if (r < 0) {
a7994dd3
YW
311 if (config->autonegotiation >= 0)
312 log_device_warning_errno(device, r, "Could not %s auto negotiation, ignoring: %m",
313 enable_disable(config->autonegotiation));
2e17fed5
YW
314
315 if (!eqzero(config->advertise))
a7994dd3
YW
316 log_device_warning_errno(device, r, "Could not set advertise mode, ignoring: %m");
317
318 if (config->speed > 0)
319 log_device_warning_errno(device, r, "Could not set speed to %"PRIu64"Mbps, ignoring: %m",
320 DIV_ROUND_UP(config->speed, 1000000));
321
322 if (config->duplex >= 0)
323 log_device_warning_errno(device, r, "Could not set duplex to %s, ignoring: %m",
324 duplex_to_string(config->duplex));
325
326 if (config->port >= 0)
327 log_device_warning_errno(device, r, "Could not set port to '%s', ignoring: %m",
328 port_to_string(config->port));
2e17fed5
YW
329 }
330
331 r = ethtool_set_wol(ethtool_fd, name, config->wol);
c50404ae
YW
332 if (r < 0) {
333 _cleanup_free_ char *str = NULL;
334
335 (void) wol_options_to_string_alloc(config->wol, &str);
a7994dd3 336 log_device_warning_errno(device, r, "Could not set WakeOnLan to %s, ignoring: %m",
c50404ae
YW
337 strna(str));
338 }
2e17fed5
YW
339
340 r = ethtool_set_features(ethtool_fd, name, config->features);
341 if (r < 0)
342 log_device_warning_errno(device, r, "Could not set offload features, ignoring: %m");
343
80662eec
YW
344 r = ethtool_set_channels(ethtool_fd, name, &config->channels);
345 if (r < 0)
346 log_device_warning_errno(device, r, "Could not set channels, ignoring: %m");
2e17fed5 347
80662eec
YW
348 r = ethtool_set_nic_buffer_size(ethtool_fd, name, &config->ring);
349 if (r < 0)
350 log_device_warning_errno(device, r, "Could not set ring buffer, ignoring: %m");
2e17fed5 351
80662eec
YW
352 r = ethtool_set_flow_control(ethtool_fd, name, config->rx_flow_control, config->tx_flow_control, config->autoneg_flow_control);
353 if (r < 0)
354 log_device_warning_errno(device, r, "Could not set flow control, ignoring: %m");
2e17fed5 355
6c35ea5e
DDM
356 r = ethtool_set_nic_coalesce_settings(ethtool_fd, name, &config->coalesce);
357 if (r < 0)
358 log_device_warning_errno(device, r, "Could not set coalesce settings, ignoring: %m");
359
2e17fed5
YW
360 return 0;
361}
362
54ed9f88 363static int get_mac(sd_device *device, MACAddressPolicy policy, struct ether_addr *mac) {
015b097c 364 unsigned addr_type;
54ed9f88 365 bool want_random = policy == MAC_ADDRESS_POLICY_RANDOM;
f1ac7002 366 int r;
16b9b87a 367
54ed9f88 368 assert(IN_SET(policy, MAC_ADDRESS_POLICY_RANDOM, MAC_ADDRESS_POLICY_PERSISTENT));
3c9b8860 369
015b097c 370 r = link_unsigned_attribute(device, "addr_assign_type", &addr_type);
f1ac7002 371 if (r < 0)
015b097c
ZJS
372 return r;
373 switch (addr_type) {
374 case NET_ADDR_SET:
9d9fed9e
ZJS
375 log_device_debug(device, "MAC on the device already set by userspace");
376 return 0;
015b097c 377 case NET_ADDR_STOLEN:
9d9fed9e
ZJS
378 log_device_debug(device, "MAC on the device already set based on another device");
379 return 0;
015b097c
ZJS
380 case NET_ADDR_RANDOM:
381 case NET_ADDR_PERM:
382 break;
383 default:
09c69eca
YW
384 log_device_warning(device, "Unknown addr_assign_type %u, ignoring", addr_type);
385 return 0;
015b097c 386 }
04b67d49 387
9d9fed9e
ZJS
388 if (want_random == (addr_type == NET_ADDR_RANDOM)) {
389 log_device_debug(device, "MAC on the device already matches policy *%s*",
390 mac_address_policy_to_string(policy));
391 return 0;
392 }
16b9b87a 393
015b097c
ZJS
394 if (want_random) {
395 log_device_debug(device, "Using random bytes to generate MAC");
550c8784
LP
396
397 /* We require genuine randomness here, since we want to make sure we won't collide with other
398 * systems booting up at the very same time. We do allow RDRAND however, since this is not
399 * cryptographic key material. */
a745117d
LP
400 r = genuine_random_bytes(mac->ether_addr_octet, ETH_ALEN, RANDOM_ALLOW_RDRAND);
401 if (r < 0)
402 return log_device_error_errno(device, r, "Failed to acquire random data to generate MAC: %m");
015b097c 403 } else {
dbe81cbd 404 uint64_t result;
9bf3b535 405
96848152
ZJS
406 r = net_get_unique_predictable_data(device,
407 naming_scheme_has(NAMING_STABLE_VIRTUAL_MACS),
408 &result);
16b9b87a 409 if (r < 0)
015b097c 410 return log_device_warning_errno(device, r, "Could not generate persistent MAC: %m");
16b9b87a 411
96848152 412 log_device_debug(device, "Using generated persistent MAC address");
9bf3b535 413 assert_cc(ETH_ALEN <= sizeof(result));
dbe81cbd 414 memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
16b9b87a
TG
415 }
416
417 /* see eth_random_addr in the kernel */
3c9b8860
TG
418 mac->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
419 mac->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
015b097c 420 return 1;
16b9b87a
TG
421}
422
ce01c07f 423static int link_config_apply_rtnl_settings(sd_netlink **rtnl, const LinkConfig *config, sd_device *device) {
2e17fed5
YW
424 struct ether_addr generated_mac, *mac = NULL;
425 int ifindex, r;
af6f0d42 426
2e17fed5 427 assert(rtnl);
3e137a1b
TG
428 assert(config);
429 assert(device);
3e137a1b 430
2e17fed5 431 r = sd_device_get_ifindex(device, &ifindex);
e5eadf53 432 if (r < 0)
2e17fed5 433 return log_device_error_errno(device, r, "Could not find ifindex: %m");
af6f0d42 434
2e17fed5
YW
435 if (IN_SET(config->mac_address_policy, MAC_ADDRESS_POLICY_PERSISTENT, MAC_ADDRESS_POLICY_RANDOM)) {
436 if (get_mac(device, config->mac_address_policy, &generated_mac) > 0)
437 mac = &generated_mac;
438 } else
439 mac = config->mac;
af6f0d42 440
face9fcc
YW
441 r = rtnl_set_link_properties(rtnl, ifindex, config->alias, mac,
442 config->txqueues, config->rxqueues, config->txqueuelen,
443 config->mtu, config->gso_max_size, config->gso_max_segments);
50725d10 444 if (r < 0)
face9fcc
YW
445 log_device_warning_errno(device, r,
446 "Could not set Alias=, MACAddress=, "
447 "TransmitQueues=, ReceiveQueues=, TransmitQueueLength=, MTU=, "
448 "GenericSegmentOffloadMaxBytes= or GenericSegmentOffloadMaxSegments=, "
449 "ignoring: %m");
50725d10 450
2e17fed5
YW
451 return 0;
452}
224ded67 453
ce01c07f 454static int link_config_generate_new_name(const LinkConfigContext *ctx, const LinkConfig *config, sd_device *device, const char **ret_name) {
2e17fed5 455 unsigned name_type = NET_NAME_UNKNOWN;
2e17fed5 456 int r;
a34811e4 457
2e17fed5
YW
458 assert(ctx);
459 assert(config);
460 assert(device);
461 assert(ret_name);
43b3a5ef 462
015b097c 463 (void) link_unsigned_attribute(device, "name_assign_type", &name_type);
0b189e8f 464
73d2bb08
ZJS
465 if (IN_SET(name_type, NET_NAME_USER, NET_NAME_RENAMED)
466 && !naming_scheme_has(NAMING_ALLOW_RERENAMES)) {
467 log_device_debug(device, "Device already has a name given by userspace, not renaming.");
468 goto no_rename;
469 }
470
0b189e8f 471 if (ctx->enable_name_policy && config->name_policy)
78f8849f 472 for (NamePolicy *p = config->name_policy; *p != _NAMEPOLICY_INVALID; p++) {
5cdb3f70
YW
473 const char *new_name = NULL;
474 NamePolicy policy = *p;
0b189e8f
ZJS
475
476 switch (policy) {
477 case NAMEPOLICY_KERNEL:
478 if (name_type != NET_NAME_PREDICTABLE)
479 continue;
480
481 /* The kernel claims to have given a predictable name, keep it. */
482 log_device_debug(device, "Policy *%s*: keeping predictable kernel name",
483 name_policy_to_string(policy));
484 goto no_rename;
3907446f
ZJS
485 case NAMEPOLICY_KEEP:
486 if (!IN_SET(name_type, NET_NAME_USER, NET_NAME_RENAMED))
487 continue;
488
489 log_device_debug(device, "Policy *%s*: keeping existing userspace name",
490 name_policy_to_string(policy));
491 goto no_rename;
0b189e8f
ZJS
492 case NAMEPOLICY_DATABASE:
493 (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &new_name);
494 break;
495 case NAMEPOLICY_ONBOARD:
496 (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &new_name);
497 break;
498 case NAMEPOLICY_SLOT:
499 (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &new_name);
500 break;
501 case NAMEPOLICY_PATH:
502 (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &new_name);
503 break;
504 case NAMEPOLICY_MAC:
505 (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &new_name);
506 break;
507 default:
04499a70 508 assert_not_reached();
5fde13d7 509 }
5cdb3f70
YW
510 if (ifname_valid(new_name)) {
511 log_device_debug(device, "Policy *%s* yields \"%s\".", name_policy_to_string(policy), new_name);
512 *ret_name = new_name;
513 return 0;
514 }
daeb71a3 515 }
daeb71a3 516
2e17fed5
YW
517 if (config->name) {
518 log_device_debug(device, "Policies didn't yield a name, using specified Name=%s.", config->name);
519 *ret_name = config->name;
520 return 0;
521 }
16b9b87a 522
2e17fed5
YW
523 log_device_debug(device, "Policies didn't yield a name and Name= is not given, not renaming.");
524no_rename:
525 r = sd_device_get_sysname(device, ret_name);
f647962d 526 if (r < 0)
2e17fed5
YW
527 return log_device_error_errno(device, r, "Failed to get sysname: %m");
528
529 return 0;
530}
531
ce01c07f 532static int link_config_apply_alternative_names(sd_netlink **rtnl, const LinkConfig *config, sd_device *device, const char *new_name) {
2e17fed5
YW
533 _cleanup_strv_free_ char **altnames = NULL, **current_altnames = NULL;
534 const char *current_name;
535 int ifindex, r;
536
537 assert(rtnl);
538 assert(config);
539 assert(device);
540
541 r = sd_device_get_sysname(device, &current_name);
542 if (r < 0)
543 return log_device_error_errno(device, r, "Failed to get sysname: %m");
544
545 r = sd_device_get_ifindex(device, &ifindex);
546 if (r < 0)
547 return log_device_error_errno(device, r, "Could not find ifindex: %m");
43b3a5ef 548
ef1d2c07
YW
549 if (config->alternative_names) {
550 altnames = strv_copy(config->alternative_names);
551 if (!altnames)
552 return log_oom();
553 }
554
555 if (config->alternative_names_policy)
556 for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) {
61fd7d67 557 const char *n = NULL;
ef1d2c07
YW
558
559 switch (*p) {
560 case NAMEPOLICY_DATABASE:
561 (void) sd_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE", &n);
562 break;
563 case NAMEPOLICY_ONBOARD:
564 (void) sd_device_get_property_value(device, "ID_NET_NAME_ONBOARD", &n);
565 break;
566 case NAMEPOLICY_SLOT:
567 (void) sd_device_get_property_value(device, "ID_NET_NAME_SLOT", &n);
568 break;
569 case NAMEPOLICY_PATH:
570 (void) sd_device_get_property_value(device, "ID_NET_NAME_PATH", &n);
571 break;
572 case NAMEPOLICY_MAC:
573 (void) sd_device_get_property_value(device, "ID_NET_NAME_MAC", &n);
574 break;
575 default:
04499a70 576 assert_not_reached();
ef1d2c07
YW
577 }
578 if (!isempty(n)) {
579 r = strv_extend(&altnames, n);
580 if (r < 0)
581 return log_oom();
582 }
583 }
584
585 if (new_name)
586 strv_remove(altnames, new_name);
2e17fed5 587 strv_remove(altnames, current_name);
97fdae33 588
2e17fed5 589 r = rtnl_get_link_alternative_names(rtnl, ifindex, &current_altnames);
97fdae33 590 if (r < 0)
2e17fed5 591 log_device_debug_errno(device, r, "Failed to get alternative names, ignoring: %m");
97fdae33
YW
592
593 char **p;
594 STRV_FOREACH(p, current_altnames)
595 strv_remove(altnames, *p);
596
ef1d2c07 597 strv_uniq(altnames);
4d016e96 598 strv_sort(altnames);
2e17fed5
YW
599 r = rtnl_set_link_alternative_names(rtnl, ifindex, altnames);
600 if (r < 0)
601 log_device_full_errno(device, r == -EOPNOTSUPP ? LOG_DEBUG : LOG_WARNING, r,
602 "Could not set AlternativeName= or apply AlternativeNamesPolicy=, ignoring: %m");
603
604 return 0;
605}
a5053a15 606
ce01c07f 607int link_config_apply(LinkConfigContext *ctx, const LinkConfig *config, sd_device *device, const char **ret_name) {
2e17fed5 608 const char *new_name;
a1130022 609 sd_device_action_t a;
2e17fed5
YW
610 int r;
611
612 assert(ctx);
613 assert(config);
614 assert(device);
615 assert(ret_name);
616
a1130022 617 r = sd_device_get_action(device, &a);
e0e789c1
YW
618 if (r < 0)
619 return log_device_error_errno(device, r, "Failed to get ACTION= property: %m");
620
a1130022 621 if (!IN_SET(a, SD_DEVICE_ADD, SD_DEVICE_BIND, SD_DEVICE_MOVE)) {
e0e789c1
YW
622 log_device_debug(device, "Skipping to apply .link settings on '%s' uevent.", device_action_to_string(a));
623
624 r = sd_device_get_sysname(device, ret_name);
625 if (r < 0)
626 return log_device_error_errno(device, r, "Failed to get sysname: %m");
627
628 return 0;
629 }
630
2e17fed5
YW
631 r = link_config_apply_ethtool_settings(&ctx->ethtool_fd, config, device);
632 if (r < 0)
633 return r;
634
635 r = link_config_apply_rtnl_settings(&ctx->rtnl, config, device);
636 if (r < 0)
637 return r;
638
a1130022 639 if (a == SD_DEVICE_MOVE) {
e0e789c1
YW
640 log_device_debug(device, "Skipping to apply Name= and NamePolicy= on '%s' uevent.", device_action_to_string(a));
641
642 r = sd_device_get_sysname(device, &new_name);
643 if (r < 0)
644 return log_device_error_errno(device, r, "Failed to get sysname: %m");
645 } else {
646 r = link_config_generate_new_name(ctx, config, device, &new_name);
647 if (r < 0)
648 return r;
649 }
2e17fed5
YW
650
651 r = link_config_apply_alternative_names(&ctx->rtnl, config, device, new_name);
652 if (r < 0)
653 return r;
d95b83b8 654
2e17fed5 655 *ret_name = new_name;
af6f0d42
TG
656 return 0;
657}
be32eb9b 658
afca7ac1 659int link_get_driver(LinkConfigContext *ctx, sd_device *device, char **ret) {
847a8a5f 660 const char *name;
a7f7d1bd 661 char *driver = NULL;
847a8a5f
TG
662 int r;
663
e5eadf53
YW
664 r = sd_device_get_sysname(device, &name);
665 if (r < 0)
666 return r;
847a8a5f 667
aedca892 668 r = ethtool_get_driver(&ctx->ethtool_fd, name, &driver);
847a8a5f
TG
669 if (r < 0)
670 return r;
671
672 *ret = driver;
673 return 0;
674}
675
fc27088a
YW
676int config_parse_ifalias(
677 const char *unit,
678 const char *filename,
679 unsigned line,
680 const char *section,
681 unsigned section_line,
682 const char *lvalue,
683 int ltype,
684 const char *rvalue,
685 void *data,
686 void *userdata) {
687
688 char **s = data;
689
690 assert(filename);
691 assert(lvalue);
692 assert(rvalue);
693 assert(data);
694
695 if (!isempty(rvalue)) {
696 *s = mfree(*s);
697 return 0;
698 }
699
700 if (!ascii_is_valid(rvalue)) {
701 log_syntax(unit, LOG_WARNING, filename, line, 0,
702 "Interface alias is not ASCII clean, ignoring assignment: %s", rvalue);
703 return 0;
704 }
705
706 if (strlen(rvalue) >= IFALIASZ) {
707 log_syntax(unit, LOG_WARNING, filename, line, 0,
708 "Interface alias is too long, ignoring assignment: %s", rvalue);
709 return 0;
710 }
711
b3f9c17a 712 return free_and_strdup_warn(s, rvalue);
fc27088a
YW
713}
714
face9fcc
YW
715int config_parse_rx_tx_queues(
716 const char *unit,
717 const char *filename,
718 unsigned line,
719 const char *section,
720 unsigned section_line,
721 const char *lvalue,
722 int ltype,
723 const char *rvalue,
724 void *data,
725 void *userdata) {
726
727 uint32_t k, *v = data;
728 int r;
729
730 if (isempty(rvalue)) {
731 *v = 0;
732 return 0;
733 }
734
735 r = safe_atou32(rvalue, &k);
736 if (r < 0) {
737 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue);
738 return 0;
739 }
740 if (k == 0 || k > 4096) {
741 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue);
742 return 0;
743 }
744
745 *v = k;
746 return 0;
747}
748
ef4a91a7
749int config_parse_txqueuelen(
750 const char *unit,
751 const char *filename,
752 unsigned line,
753 const char *section,
754 unsigned section_line,
755 const char *lvalue,
756 int ltype,
757 const char *rvalue,
758 void *data,
759 void *userdata) {
760
761 uint32_t k, *v = data;
762 int r;
763
764 if (isempty(rvalue)) {
765 *v = UINT32_MAX;
766 return 0;
767 }
768
769 r = safe_atou32(rvalue, &k);
770 if (r < 0) {
771 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s=, ignoring assignment: %s.", lvalue, rvalue);
772 return 0;
773 }
774 if (k == UINT32_MAX) {
775 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid %s=, ignoring assignment: %s.", lvalue, rvalue);
776 return 0;
777 }
778
779 *v = k;
780 return 0;
781}
782
54ed9f88
ZJS
783static const char* const mac_address_policy_table[_MAC_ADDRESS_POLICY_MAX] = {
784 [MAC_ADDRESS_POLICY_PERSISTENT] = "persistent",
785 [MAC_ADDRESS_POLICY_RANDOM] = "random",
786 [MAC_ADDRESS_POLICY_NONE] = "none",
be32eb9b
TG
787};
788
54ed9f88 789DEFINE_STRING_TABLE_LOOKUP(mac_address_policy, MACAddressPolicy);
d03cb6b8
YW
790DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(
791 config_parse_mac_address_policy,
792 mac_address_policy,
793 MACAddressPolicy,
794 MAC_ADDRESS_POLICY_NONE,
795 "Failed to parse MAC address policy");
be32eb9b 796
2c5859af 797static const char* const name_policy_table[_NAMEPOLICY_MAX] = {
04b67d49 798 [NAMEPOLICY_KERNEL] = "kernel",
3907446f 799 [NAMEPOLICY_KEEP] = "keep",
e51660ae 800 [NAMEPOLICY_DATABASE] = "database",
be32eb9b
TG
801 [NAMEPOLICY_ONBOARD] = "onboard",
802 [NAMEPOLICY_SLOT] = "slot",
803 [NAMEPOLICY_PATH] = "path",
3907446f 804 [NAMEPOLICY_MAC] = "mac",
be32eb9b
TG
805};
806
807DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
464cf22f
TG
808DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
809 _NAMEPOLICY_INVALID,
810 "Failed to parse interface name policy");
ef1d2c07
YW
811
812static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = {
813 [NAMEPOLICY_DATABASE] = "database",
814 [NAMEPOLICY_ONBOARD] = "onboard",
815 [NAMEPOLICY_SLOT] = "slot",
816 [NAMEPOLICY_PATH] = "path",
817 [NAMEPOLICY_MAC] = "mac",
818};
819
820DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);
821DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy,
822 _NAMEPOLICY_INVALID,
823 "Failed to parse alternative names policy");